Importing Gnus v5.8.2.
[elisp/gnus.git-] / 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-list-identifiers nil
546   "Regexp that matches list identifiers to be removed from subject.
547 This can also be a list of regexps."
548   :group 'gnus-summary-format
549   :group 'gnus-article-hiding
550   :type '(choice (const :tag "none" nil)
551                  (regexp :value ".*")
552                  (repeat :value (".*") regexp)))
553
554 (defcustom gnus-summary-mark-below 0
555   "*Mark all articles with a score below this variable as read.
556 This variable is local to each summary buffer and usually set by the
557 score file."
558   :group 'gnus-score-default
559   :type 'integer)
560
561 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
562   "*List of functions used for sorting articles in the summary buffer.
563 This variable is only used when not using a threaded display."
564   :group 'gnus-summary-sort
565   :type '(repeat (choice (function-item gnus-article-sort-by-number)
566                          (function-item gnus-article-sort-by-author)
567                          (function-item gnus-article-sort-by-subject)
568                          (function-item gnus-article-sort-by-date)
569                          (function-item gnus-article-sort-by-score)
570                          (function :tag "other"))))
571
572 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
573   "*List of functions used for sorting threads in the summary buffer.
574 By default, threads are sorted by article number.
575
576 Each function takes two threads and return non-nil if the first thread
577 should be sorted before the other.  If you use more than one function,
578 the primary sort function should be the last.  You should probably
579 always include `gnus-thread-sort-by-number' in the list of sorting
580 functions -- preferably first.
581
582 Ready-made functions include `gnus-thread-sort-by-number',
583 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
584 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
585 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
586   :group 'gnus-summary-sort
587   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
588                          (function-item gnus-thread-sort-by-author)
589                          (function-item gnus-thread-sort-by-subject)
590                          (function-item gnus-thread-sort-by-date)
591                          (function-item gnus-thread-sort-by-score)
592                          (function-item gnus-thread-sort-by-total-score)
593                          (function :tag "other"))))
594
595 (defcustom gnus-thread-score-function '+
596   "*Function used for calculating the total score of a thread.
597
598 The function is called with the scores of the article and each
599 subthread and should then return the score of the thread.
600
601 Some functions you can use are `+', `max', or `min'."
602   :group 'gnus-summary-sort
603   :type 'function)
604
605 (defcustom gnus-summary-expunge-below nil
606   "All articles that have a score less than this variable will be expunged.
607 This variable is local to the summary buffers."
608   :group 'gnus-score-default
609   :type '(choice (const :tag "off" nil)
610                  integer))
611
612 (defcustom gnus-thread-expunge-below nil
613   "All threads that have a total score less than this variable will be expunged.
614 See `gnus-thread-score-function' for en explanation of what a
615 \"thread score\" is.
616
617 This variable is local to the summary buffers."
618   :group 'gnus-threading
619   :group 'gnus-score-default
620   :type '(choice (const :tag "off" nil)
621                  integer))
622
623 (defcustom gnus-summary-mode-hook nil
624   "*A hook for Gnus summary mode.
625 This hook is run before any variables are set in the summary buffer."
626   :group 'gnus-summary-various
627   :type 'hook)
628
629 (defcustom gnus-summary-menu-hook nil
630   "*Hook run after the creation of the summary mode menu."
631   :group 'gnus-summary-visual
632   :type 'hook)
633
634 (defcustom gnus-summary-exit-hook nil
635   "*A hook called on exit from the summary buffer.
636 It will be called with point in the group buffer."
637   :group 'gnus-summary-exit
638   :type 'hook)
639
640 (defcustom gnus-summary-prepare-hook nil
641   "*A hook called after the summary buffer has been generated.
642 If you want to modify the summary buffer, you can use this hook."
643   :group 'gnus-summary-various
644   :type 'hook)
645
646 (defcustom gnus-summary-prepared-hook nil
647   "*A hook called as the last thing after the summary buffer has been generated."
648   :group 'gnus-summary-various
649   :type 'hook)
650
651 (defcustom gnus-summary-generate-hook nil
652   "*A hook run just before generating the summary buffer.
653 This hook is commonly used to customize threading variables and the
654 like."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-select-group-hook nil
659   "*A hook called when a newsgroup is selected.
660
661 If you'd like to simplify subjects like the
662 `gnus-summary-next-same-subject' command does, you can use the
663 following hook:
664
665  (setq gnus-select-group-hook
666       (list
667         (lambda ()
668           (mapcar (lambda (header)
669                      (mail-header-set-subject
670                       header
671                       (gnus-simplify-subject
672                        (mail-header-subject header) 're-only)))
673                   gnus-newsgroup-headers))))"
674   :group 'gnus-group-select
675   :type 'hook)
676
677 (defcustom gnus-select-article-hook nil
678   "*A hook called when an article is selected."
679   :group 'gnus-summary-choose
680   :type 'hook)
681
682 (defcustom gnus-visual-mark-article-hook
683   (list 'gnus-highlight-selected-summary)
684   "*Hook run after selecting an article in the summary buffer.
685 It is meant to be used for highlighting the article in some way.  It
686 is not run if `gnus-visual' is nil."
687   :group 'gnus-summary-visual
688   :type 'hook)
689
690 (defcustom gnus-parse-headers-hook nil
691   "*A hook called before parsing the headers."
692   :group 'gnus-various
693   :type 'hook)
694
695 (defcustom gnus-exit-group-hook nil
696   "*A hook called when exiting (not quitting) summary mode."
697   :group 'gnus-various
698   :type 'hook)
699
700 (defcustom gnus-summary-update-hook
701   (list 'gnus-summary-highlight-line)
702   "*A hook called when a summary line is changed.
703 The hook will not be called if `gnus-visual' is nil.
704
705 The default function `gnus-summary-highlight-line' will
706 highlight the line according to the `gnus-summary-highlight'
707 variable."
708   :group 'gnus-summary-visual
709   :type 'hook)
710
711 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
712   "*A hook called when an article is selected for the first time.
713 The hook is intended to mark an article as read (or unread)
714 automatically when it is selected."
715   :group 'gnus-summary-choose
716   :type 'hook)
717
718 (defcustom gnus-group-no-more-groups-hook nil
719   "*A hook run when returning to group mode having no more (unread) groups."
720   :group 'gnus-group-select
721   :type 'hook)
722
723 (defcustom gnus-ps-print-hook nil
724   "*A hook run before ps-printing something from Gnus."
725   :group 'gnus-summary
726   :type 'hook)
727
728 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
729   "Face used for highlighting the current article in the summary buffer."
730   :group 'gnus-summary-visual
731   :type 'face)
732
733 (defcustom gnus-summary-highlight
734   '(((= mark gnus-canceled-mark)
735      . gnus-summary-cancelled-face)
736     ((and (> score default)
737           (or (= mark gnus-dormant-mark)
738               (= mark gnus-ticked-mark)))
739      . gnus-summary-high-ticked-face)
740     ((and (< score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-low-ticked-face)
744     ((or (= mark gnus-dormant-mark)
745          (= mark gnus-ticked-mark))
746      . gnus-summary-normal-ticked-face)
747     ((and (> score default) (= mark gnus-ancient-mark))
748      . gnus-summary-high-ancient-face)
749     ((and (< score default) (= mark gnus-ancient-mark))
750      . gnus-summary-low-ancient-face)
751     ((= mark gnus-ancient-mark)
752      . gnus-summary-normal-ancient-face)
753     ((and (> score default) (= mark gnus-unread-mark))
754      . gnus-summary-high-unread-face)
755     ((and (< score default) (= mark gnus-unread-mark))
756      . gnus-summary-low-unread-face)
757     ((= mark gnus-unread-mark)
758      . gnus-summary-normal-unread-face)
759     ((and (> score default) (memq mark (list gnus-downloadable-mark
760                                              gnus-undownloaded-mark)))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (memq mark (list gnus-downloadable-mark
763                                              gnus-undownloaded-mark)))
764      . gnus-summary-low-unread-face)
765     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
766      . gnus-summary-normal-unread-face)
767     ((> score default)
768      . gnus-summary-high-read-face)
769     ((< score default)
770      . gnus-summary-low-read-face)
771     (t
772      . gnus-summary-normal-read-face))
773   "*Controls the highlighting of summary buffer lines.
774
775 A list of (FORM . FACE) pairs.  When deciding how a a particular
776 summary line should be displayed, each form is evaluated.  The content
777 of the face field after the first true form is used.  You can change
778 how those summary lines are displayed, by editing the face field.
779
780 You can use the following variables in the FORM field.
781
782 score:   The articles score
783 default: The default article score.
784 below:   The score below which articles are automatically marked as read.
785 mark:    The articles mark."
786   :group 'gnus-summary-visual
787   :type '(repeat (cons (sexp :tag "Form" nil)
788                        face)))
789
790 (defcustom gnus-alter-header-function nil
791   "Function called to allow alteration of article header structures.
792 The function is called with one parameter, the article header vector,
793 which it may alter in any way.")
794
795 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
796   "Variable that says which function should be used to decode a string with encoded words.")
797
798 (defcustom gnus-extra-headers nil
799   "*Extra headers to parse."
800   :group 'gnus-summary
801   :type '(repeat symbol))
802
803 (defcustom gnus-ignored-from-addresses
804   (and user-mail-address (regexp-quote user-mail-address))
805   "*Regexp of From headers that may be suppressed in favor of To headers."
806   :group 'gnus-summary
807   :type 'regexp)
808
809 (defcustom gnus-group-charset-alist
810   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
811     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
812     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
813     ("^relcom\\>" koi8-r)
814     ("^fido7\\>" koi8-r)
815     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
816     ("^israel\\>" iso-8859-1)
817     ("^han\\>" euc-kr)
818     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
819     (".*" iso-8859-1))
820   "Alist of regexps (to match group names) and default charsets to be used when reading."
821   :type '(repeat (list (regexp :tag "Group")
822                        (symbol :tag "Charset")))
823   :group 'gnus-charset)
824
825 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
826   "List of charsets that should be ignored.
827 When these charsets are used in the \"charset\" parameter, the
828 default charset will be used instead."
829   :type '(repeat symbol)
830   :group 'gnus-charset)
831
832 (defcustom gnus-group-ignored-charsets-alist 
833   '(("alt\\.chinese\\.text" iso-8859-1))
834   "Alist of regexps (to match group names) and charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat (cons (regexp :tag "Group")
838                        (repeat symbol)))
839   :group 'gnus-charset)
840
841 (defcustom gnus-group-highlight-words-alist nil
842   "Alist of group regexps and highlight regexps.
843 This variable uses the same syntax as `gnus-emphasis-alist'."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat (list (regexp :tag "Highlight regexp")
846                                      (number :tag "Group for entire word" 0)
847                                      (number :tag "Group for displayed part" 0)
848                                      (symbol :tag "Face" 
849                                              gnus-emphasis-highlight-words)))))
850   :group 'gnus-summary-visual)
851
852 (defcustom gnus-summary-show-article-charset-alist
853   nil
854   "Alist of number and charset.
855 The article will be shown with the charset corresponding to the
856 numbered argument.
857 For example: ((1 . cn-gb-2312) (2 . big5))."
858   :type '(repeat (cons (number :tag "Argument" 1)
859                        (symbol :tag "Charset")))
860   :group 'gnus-charset)
861
862
863 ;;; Internal variables
864
865 (defvar gnus-article-mime-handles nil)
866 (defvar gnus-article-decoded-p nil)
867 (defvar gnus-scores-exclude-files nil)
868 (defvar gnus-page-broken nil)
869 (defvar gnus-inhibit-mime-unbuttonizing nil)
870
871 (defvar gnus-original-article nil)
872 (defvar gnus-article-internal-prepare-hook nil)
873 (defvar gnus-newsgroup-process-stack nil)
874
875 (defvar gnus-thread-indent-array nil)
876 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
877 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
878   "Function called to sort the articles within a thread after it has 
879 been gathered together.")
880
881 ;; Avoid highlighting in kill files.
882 (defvar gnus-summary-inhibit-highlight nil)
883 (defvar gnus-newsgroup-selected-overlay nil)
884 (defvar gnus-inhibit-limiting nil)
885 (defvar gnus-newsgroup-adaptive-score-file nil)
886 (defvar gnus-current-score-file nil)
887 (defvar gnus-current-move-group nil)
888 (defvar gnus-current-copy-group nil)
889 (defvar gnus-current-crosspost-group nil)
890
891 (defvar gnus-newsgroup-dependencies nil)
892 (defvar gnus-newsgroup-adaptive nil)
893 (defvar gnus-summary-display-article-function nil)
894 (defvar gnus-summary-highlight-line-function nil
895   "Function called after highlighting a summary line.")
896
897 (defvar gnus-summary-line-format-alist
898   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
899     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
900     (?s gnus-tmp-subject-or-nil ?s)
901     (?n gnus-tmp-name ?s)
902     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
903         ?s)
904     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
905             gnus-tmp-from) ?s)
906     (?F gnus-tmp-from ?s)
907     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
908     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
909     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
910     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
911     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
912     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
913     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
914     (?L gnus-tmp-lines ?d)
915     (?I gnus-tmp-indentation ?s)
916     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
917     (?R gnus-tmp-replied ?c)
918     (?\[ gnus-tmp-opening-bracket ?c)
919     (?\] gnus-tmp-closing-bracket ?c)
920     (?\> (make-string gnus-tmp-level ? ) ?s)
921     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
922     (?i gnus-tmp-score ?d)
923     (?z gnus-tmp-score-char ?c)
924     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
925     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
926     (?U gnus-tmp-unread ?c)
927     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
928     (?t (gnus-summary-number-of-articles-in-thread
929          (and (boundp 'thread) (car thread)) gnus-tmp-level)
930         ?d)
931     (?e (gnus-summary-number-of-articles-in-thread
932          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
933         ?c)
934     (?u gnus-tmp-user-defined ?s)
935     (?P (gnus-pick-line-number) ?d))
936   "An alist of format specifications that can appear in summary lines,
937 and what variables they correspond with, along with the type of the
938 variable (string, integer, character, etc).")
939
940 (defvar gnus-summary-dummy-line-format-alist
941   `((?S gnus-tmp-subject ?s)
942     (?N gnus-tmp-number ?d)
943     (?u gnus-tmp-user-defined ?s)))
944
945 (defvar gnus-summary-mode-line-format-alist
946   `((?G gnus-tmp-group-name ?s)
947     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
948     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
949     (?A gnus-tmp-article-number ?d)
950     (?Z gnus-tmp-unread-and-unselected ?s)
951     (?V gnus-version ?s)
952     (?U gnus-tmp-unread-and-unticked ?d)
953     (?S gnus-tmp-subject ?s)
954     (?e gnus-tmp-unselected ?d)
955     (?u gnus-tmp-user-defined ?s)
956     (?d (length gnus-newsgroup-dormant) ?d)
957     (?t (length gnus-newsgroup-marked) ?d)
958     (?r (length gnus-newsgroup-reads) ?d)
959     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
960     (?E gnus-newsgroup-expunged-tally ?d)
961     (?s (gnus-current-score-file-nondirectory) ?s)))
962
963 (defvar gnus-last-search-regexp nil
964   "Default regexp for article search command.")
965
966 (defvar gnus-last-shell-command nil
967   "Default shell command on article.")
968
969 (defvar gnus-newsgroup-begin nil)
970 (defvar gnus-newsgroup-end nil)
971 (defvar gnus-newsgroup-last-rmail nil)
972 (defvar gnus-newsgroup-last-mail nil)
973 (defvar gnus-newsgroup-last-folder nil)
974 (defvar gnus-newsgroup-last-file nil)
975 (defvar gnus-newsgroup-auto-expire nil)
976 (defvar gnus-newsgroup-active nil)
977
978 (defvar gnus-newsgroup-data nil)
979 (defvar gnus-newsgroup-data-reverse nil)
980 (defvar gnus-newsgroup-limit nil)
981 (defvar gnus-newsgroup-limits nil)
982
983 (defvar gnus-newsgroup-unreads nil
984   "List of unread articles in the current newsgroup.")
985
986 (defvar gnus-newsgroup-unselected nil
987   "List of unselected unread articles in the current newsgroup.")
988
989 (defvar gnus-newsgroup-reads nil
990   "Alist of read articles and article marks in the current newsgroup.")
991
992 (defvar gnus-newsgroup-expunged-tally nil)
993
994 (defvar gnus-newsgroup-marked nil
995   "List of ticked articles in the current newsgroup (a subset of unread art).")
996
997 (defvar gnus-newsgroup-killed nil
998   "List of ranges of articles that have been through the scoring process.")
999
1000 (defvar gnus-newsgroup-cached nil
1001   "List of articles that come from the article cache.")
1002
1003 (defvar gnus-newsgroup-saved nil
1004   "List of articles that have been saved.")
1005
1006 (defvar gnus-newsgroup-kill-headers nil)
1007
1008 (defvar gnus-newsgroup-replied nil
1009   "List of articles that have been replied to in the current newsgroup.")
1010
1011 (defvar gnus-newsgroup-expirable nil
1012   "List of articles in the current newsgroup that can be expired.")
1013
1014 (defvar gnus-newsgroup-processable nil
1015   "List of articles in the current newsgroup that can be processed.")
1016
1017 (defvar gnus-newsgroup-downloadable nil
1018   "List of articles in the current newsgroup that can be processed.")
1019
1020 (defvar gnus-newsgroup-undownloaded nil
1021   "List of articles in the current newsgroup that haven't been downloaded..")
1022
1023 (defvar gnus-newsgroup-unsendable nil
1024   "List of articles in the current newsgroup that won't be sent.")
1025
1026 (defvar gnus-newsgroup-bookmarks nil
1027   "List of articles in the current newsgroup that have bookmarks.")
1028
1029 (defvar gnus-newsgroup-dormant nil
1030   "List of dormant articles in the current newsgroup.")
1031
1032 (defvar gnus-newsgroup-scored nil
1033   "List of scored articles in the current newsgroup.")
1034
1035 (defvar gnus-newsgroup-headers nil
1036   "List of article headers in the current newsgroup.")
1037
1038 (defvar gnus-newsgroup-threads nil)
1039
1040 (defvar gnus-newsgroup-prepared nil
1041   "Whether the current group has been prepared properly.")
1042
1043 (defvar gnus-newsgroup-ancient nil
1044   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1045
1046 (defvar gnus-newsgroup-sparse nil)
1047
1048 (defvar gnus-current-article nil)
1049 (defvar gnus-article-current nil)
1050 (defvar gnus-current-headers nil)
1051 (defvar gnus-have-all-headers nil)
1052 (defvar gnus-last-article nil)
1053 (defvar gnus-newsgroup-history nil)
1054 (defvar gnus-newsgroup-charset nil)
1055 (defvar gnus-newsgroup-ephemeral-charset nil)
1056 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1057
1058 (defconst gnus-summary-local-variables
1059   '(gnus-newsgroup-name
1060     gnus-newsgroup-begin gnus-newsgroup-end
1061     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1062     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1063     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1064     gnus-newsgroup-unselected gnus-newsgroup-marked
1065     gnus-newsgroup-reads gnus-newsgroup-saved
1066     gnus-newsgroup-replied gnus-newsgroup-expirable
1067     gnus-newsgroup-processable gnus-newsgroup-killed
1068     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1069     gnus-newsgroup-unsendable
1070     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1071     gnus-newsgroup-headers gnus-newsgroup-threads
1072     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1073     gnus-current-article gnus-current-headers gnus-have-all-headers
1074     gnus-last-article gnus-article-internal-prepare-hook
1075     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1076     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1077     gnus-thread-expunge-below
1078     gnus-score-alist gnus-current-score-file
1079     (gnus-summary-expunge-below . global)
1080     (gnus-summary-mark-below . global)
1081     gnus-newsgroup-active gnus-scores-exclude-files
1082     gnus-newsgroup-history gnus-newsgroup-ancient
1083     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1084     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1085     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1086     (gnus-newsgroup-expunged-tally . 0)
1087     gnus-cache-removable-articles gnus-newsgroup-cached
1088     gnus-newsgroup-data gnus-newsgroup-data-reverse
1089     gnus-newsgroup-limit gnus-newsgroup-limits
1090     gnus-newsgroup-charset)
1091   "Variables that are buffer-local to the summary buffers.")
1092
1093 ;; Byte-compiler warning.
1094 (defvar gnus-article-mode-map)
1095
1096 ;; MIME stuff.
1097
1098 (defvar gnus-decode-encoded-word-methods
1099   '(mail-decode-encoded-word-string)
1100   "List of methods used to decode encoded words.
1101
1102 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1103 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1104 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1105 whose names match REGEXP.
1106
1107 For example:
1108 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1109  mail-decode-encoded-word-string
1110  (\"chinese\" . rfc1843-decode-string))
1111 ")
1112
1113 (defvar gnus-decode-encoded-word-methods-cache nil)
1114
1115 (defun gnus-multi-decode-encoded-word-string (string)
1116   "Apply the functions from `gnus-encoded-word-methods' that match."
1117   (unless (and gnus-decode-encoded-word-methods-cache
1118                (eq gnus-newsgroup-name
1119                    (car gnus-decode-encoded-word-methods-cache)))
1120     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1121     (mapc '(lambda (x)
1122              (if (symbolp x)
1123                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1124                (if (and gnus-newsgroup-name
1125                         (string-match (car x) gnus-newsgroup-name))
1126                    (nconc gnus-decode-encoded-word-methods-cache
1127                           (list (cdr x))))))
1128           gnus-decode-encoded-word-methods))
1129   (let ((xlist gnus-decode-encoded-word-methods-cache))
1130     (pop xlist)
1131     (while xlist
1132       (setq string (funcall (pop xlist) string))))
1133   string)
1134
1135 ;; Subject simplification.
1136
1137 (defun gnus-simplify-whitespace (str)
1138   "Remove excessive whitespace."
1139   (let ((mystr str))
1140     ;; Multiple spaces.
1141     (while (string-match "[ \t][ \t]+" mystr)
1142       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1143                           " "
1144                           (substring mystr (match-end 0)))))
1145     ;; Leading spaces.
1146     (when (string-match "^[ \t]+" mystr)
1147       (setq mystr (substring mystr (match-end 0))))
1148     ;; Trailing spaces.
1149     (when (string-match "[ \t]+$" mystr)
1150       (setq mystr (substring mystr 0 (match-beginning 0))))
1151     mystr))
1152
1153 (defsubst gnus-simplify-subject-re (subject)
1154   "Remove \"Re:\" from subject lines."
1155   (if (string-match "^[Rr][Ee]: *" subject)
1156       (substring subject (match-end 0))
1157     subject))
1158
1159 (defun gnus-simplify-subject (subject &optional re-only)
1160   "Remove `Re:' and words in parentheses.
1161 If RE-ONLY is non-nil, strip leading `Re:'s only."
1162   (let ((case-fold-search t))           ;Ignore case.
1163     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1164     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1165       (setq subject (substring subject (match-end 0))))
1166     ;; Remove uninteresting prefixes.
1167     (when (and (not re-only)
1168                gnus-simplify-ignored-prefixes
1169                (string-match gnus-simplify-ignored-prefixes subject))
1170       (setq subject (substring subject (match-end 0))))
1171     ;; Remove words in parentheses from end.
1172     (unless re-only
1173       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1174         (setq subject (substring subject 0 (match-beginning 0)))))
1175     ;; Return subject string.
1176     subject))
1177
1178 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1179 ;; all whitespace.
1180 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1181   (goto-char (point-min))
1182   (while (re-search-forward regexp nil t)
1183       (replace-match (or newtext ""))))
1184
1185 (defun gnus-simplify-buffer-fuzzy ()
1186   "Simplify string in the buffer fuzzily.
1187 The string in the accessible portion of the current buffer is simplified.
1188 It is assumed to be a single-line subject.
1189 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1190 matter is removed.  Additional things can be deleted by setting
1191 gnus-simplify-subject-fuzzy-regexp."
1192   (let ((case-fold-search t)
1193         (modified-tick))
1194     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1195
1196     (while (not (eq modified-tick (buffer-modified-tick)))
1197       (setq modified-tick (buffer-modified-tick))
1198       (cond
1199        ((listp gnus-simplify-subject-fuzzy-regexp)
1200         (mapcar 'gnus-simplify-buffer-fuzzy-step
1201                 gnus-simplify-subject-fuzzy-regexp))
1202        (gnus-simplify-subject-fuzzy-regexp
1203         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1204       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1205       (gnus-simplify-buffer-fuzzy-step
1206        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1207       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1208
1209     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1210     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1211     (gnus-simplify-buffer-fuzzy-step " $")
1212     (gnus-simplify-buffer-fuzzy-step "^ +")))
1213
1214 (defun gnus-simplify-subject-fuzzy (subject)
1215   "Simplify a subject string fuzzily.
1216 See `gnus-simplify-buffer-fuzzy' for details."
1217   (save-excursion
1218     (gnus-set-work-buffer)
1219     (let ((case-fold-search t))
1220       ;; Remove uninteresting prefixes.
1221       (when (and gnus-simplify-ignored-prefixes
1222                  (string-match gnus-simplify-ignored-prefixes subject))
1223         (setq subject (substring subject (match-end 0))))
1224       (insert subject)
1225       (inline (gnus-simplify-buffer-fuzzy))
1226       (buffer-string))))
1227
1228 (defsubst gnus-simplify-subject-fully (subject)
1229   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1230   (cond
1231    (gnus-simplify-subject-functions
1232     (gnus-map-function gnus-simplify-subject-functions subject))
1233    ((null gnus-summary-gather-subject-limit)
1234     (gnus-simplify-subject-re subject))
1235    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1236     (gnus-simplify-subject-fuzzy subject))
1237    ((numberp gnus-summary-gather-subject-limit)
1238     (gnus-limit-string (gnus-simplify-subject-re subject)
1239                        gnus-summary-gather-subject-limit))
1240    (t
1241     subject)))
1242
1243 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1244   "Check whether two subjects are equal.
1245 If optional argument simple-first is t, first argument is already
1246 simplified."
1247   (cond
1248    ((null simple-first)
1249     (equal (gnus-simplify-subject-fully s1)
1250            (gnus-simplify-subject-fully s2)))
1251    (t
1252     (equal s1
1253            (gnus-simplify-subject-fully s2)))))
1254
1255 (defun gnus-summary-bubble-group ()
1256   "Increase the score of the current group.
1257 This is a handy function to add to `gnus-summary-exit-hook' to
1258 increase the score of each group you read."
1259   (gnus-group-add-score gnus-newsgroup-name))
1260
1261 \f
1262 ;;;
1263 ;;; Gnus summary mode
1264 ;;;
1265
1266 (put 'gnus-summary-mode 'mode-class 'special)
1267
1268 (when t
1269   ;; Non-orthogonal keys
1270
1271   (gnus-define-keys gnus-summary-mode-map
1272     " " gnus-summary-next-page
1273     "\177" gnus-summary-prev-page
1274     [delete] gnus-summary-prev-page
1275     [backspace] gnus-summary-prev-page
1276     "\r" gnus-summary-scroll-up
1277     "\M-\r" gnus-summary-scroll-down
1278     "n" gnus-summary-next-unread-article
1279     "p" gnus-summary-prev-unread-article
1280     "N" gnus-summary-next-article
1281     "P" gnus-summary-prev-article
1282     "\M-\C-n" gnus-summary-next-same-subject
1283     "\M-\C-p" gnus-summary-prev-same-subject
1284     "\M-n" gnus-summary-next-unread-subject
1285     "\M-p" gnus-summary-prev-unread-subject
1286     "." gnus-summary-first-unread-article
1287     "," gnus-summary-best-unread-article
1288     "\M-s" gnus-summary-search-article-forward
1289     "\M-r" gnus-summary-search-article-backward
1290     "<" gnus-summary-beginning-of-article
1291     ">" gnus-summary-end-of-article
1292     "j" gnus-summary-goto-article
1293     "^" gnus-summary-refer-parent-article
1294     "\M-^" gnus-summary-refer-article
1295     "u" gnus-summary-tick-article-forward
1296     "!" gnus-summary-tick-article-forward
1297     "U" gnus-summary-tick-article-backward
1298     "d" gnus-summary-mark-as-read-forward
1299     "D" gnus-summary-mark-as-read-backward
1300     "E" gnus-summary-mark-as-expirable
1301     "\M-u" gnus-summary-clear-mark-forward
1302     "\M-U" gnus-summary-clear-mark-backward
1303     "k" gnus-summary-kill-same-subject-and-select
1304     "\C-k" gnus-summary-kill-same-subject
1305     "\M-\C-k" gnus-summary-kill-thread
1306     "\M-\C-l" gnus-summary-lower-thread
1307     "e" gnus-summary-edit-article
1308     "#" gnus-summary-mark-as-processable
1309     "\M-#" gnus-summary-unmark-as-processable
1310     "\M-\C-t" gnus-summary-toggle-threads
1311     "\M-\C-s" gnus-summary-show-thread
1312     "\M-\C-h" gnus-summary-hide-thread
1313     "\M-\C-f" gnus-summary-next-thread
1314     "\M-\C-b" gnus-summary-prev-thread
1315     "\M-\C-u" gnus-summary-up-thread
1316     "\M-\C-d" gnus-summary-down-thread
1317     "&" gnus-summary-execute-command
1318     "c" gnus-summary-catchup-and-exit
1319     "\C-w" gnus-summary-mark-region-as-read
1320     "\C-t" gnus-summary-toggle-truncation
1321     "?" gnus-summary-mark-as-dormant
1322     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1323     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1324     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1325     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1326     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1327     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1328     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1329     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1330     "=" gnus-summary-expand-window
1331     "\C-x\C-s" gnus-summary-reselect-current-group
1332     "\M-g" gnus-summary-rescan-group
1333     "w" gnus-summary-stop-page-breaking
1334     "\C-c\C-r" gnus-summary-caesar-message
1335     "f" gnus-summary-followup
1336     "F" gnus-summary-followup-with-original
1337     "C" gnus-summary-cancel-article
1338     "r" gnus-summary-reply
1339     "R" gnus-summary-reply-with-original
1340     "\C-c\C-f" gnus-summary-mail-forward
1341     "o" gnus-summary-save-article
1342     "\C-o" gnus-summary-save-article-mail
1343     "|" gnus-summary-pipe-output
1344     "\M-k" gnus-summary-edit-local-kill
1345     "\M-K" gnus-summary-edit-global-kill
1346     ;; "V" gnus-version
1347     "\C-c\C-d" gnus-summary-describe-group
1348     "q" gnus-summary-exit
1349     "Q" gnus-summary-exit-no-update
1350     "\C-c\C-i" gnus-info-find-node
1351     gnus-mouse-2 gnus-mouse-pick-article
1352     "m" gnus-summary-mail-other-window
1353     "a" gnus-summary-post-news
1354     "x" gnus-summary-limit-to-unread
1355     "s" gnus-summary-isearch-article
1356     "t" gnus-summary-toggle-header
1357     "g" gnus-summary-show-article
1358     "l" gnus-summary-goto-last-article
1359     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1360     "\C-d" gnus-summary-enter-digest-group
1361     "\M-\C-d" gnus-summary-read-document
1362     "\M-\C-e" gnus-summary-edit-parameters
1363     "\M-\C-g" gnus-summary-customize-parameters
1364     "\C-c\C-b" gnus-bug
1365     "*" gnus-cache-enter-article
1366     "\M-*" gnus-cache-remove-article
1367     "\M-&" gnus-summary-universal-argument
1368     "\C-l" gnus-recenter
1369     "I" gnus-summary-increase-score
1370     "L" gnus-summary-lower-score
1371     "\M-i" gnus-symbolic-argument
1372     "h" gnus-summary-select-article-buffer
1373
1374     "b" gnus-article-view-part
1375     "\M-t" gnus-summary-toggle-display-buttonized
1376
1377     "V" gnus-summary-score-map
1378     "X" gnus-uu-extract-map
1379     "S" gnus-summary-send-map)
1380
1381   ;; Sort of orthogonal keymap
1382   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1383     "t" gnus-summary-tick-article-forward
1384     "!" gnus-summary-tick-article-forward
1385     "d" gnus-summary-mark-as-read-forward
1386     "r" gnus-summary-mark-as-read-forward
1387     "c" gnus-summary-clear-mark-forward
1388     " " gnus-summary-clear-mark-forward
1389     "e" gnus-summary-mark-as-expirable
1390     "x" gnus-summary-mark-as-expirable
1391     "?" gnus-summary-mark-as-dormant
1392     "b" gnus-summary-set-bookmark
1393     "B" gnus-summary-remove-bookmark
1394     "#" gnus-summary-mark-as-processable
1395     "\M-#" gnus-summary-unmark-as-processable
1396     "S" gnus-summary-limit-include-expunged
1397     "C" gnus-summary-catchup
1398     "H" gnus-summary-catchup-to-here
1399     "\C-c" gnus-summary-catchup-all
1400     "k" gnus-summary-kill-same-subject-and-select
1401     "K" gnus-summary-kill-same-subject
1402     "P" gnus-uu-mark-map)
1403
1404   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1405     "c" gnus-summary-clear-above
1406     "u" gnus-summary-tick-above
1407     "m" gnus-summary-mark-above
1408     "k" gnus-summary-kill-below)
1409
1410   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1411     "/" gnus-summary-limit-to-subject
1412     "n" gnus-summary-limit-to-articles
1413     "w" gnus-summary-pop-limit
1414     "s" gnus-summary-limit-to-subject
1415     "a" gnus-summary-limit-to-author
1416     "u" gnus-summary-limit-to-unread
1417     "m" gnus-summary-limit-to-marks
1418     "M" gnus-summary-limit-exclude-marks
1419     "v" gnus-summary-limit-to-score
1420     "*" gnus-summary-limit-include-cached
1421     "D" gnus-summary-limit-include-dormant
1422     "T" gnus-summary-limit-include-thread
1423     "d" gnus-summary-limit-exclude-dormant
1424     "t" gnus-summary-limit-to-age
1425     "x" gnus-summary-limit-to-extra 
1426     "E" gnus-summary-limit-include-expunged
1427     "c" gnus-summary-limit-exclude-childless-dormant
1428     "C" gnus-summary-limit-mark-excluded-as-read)
1429
1430   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1431     "n" gnus-summary-next-unread-article
1432     "p" gnus-summary-prev-unread-article
1433     "N" gnus-summary-next-article
1434     "P" gnus-summary-prev-article
1435     "\C-n" gnus-summary-next-same-subject
1436     "\C-p" gnus-summary-prev-same-subject
1437     "\M-n" gnus-summary-next-unread-subject
1438     "\M-p" gnus-summary-prev-unread-subject
1439     "f" gnus-summary-first-unread-article
1440     "b" gnus-summary-best-unread-article
1441     "j" gnus-summary-goto-article
1442     "g" gnus-summary-goto-subject
1443     "l" gnus-summary-goto-last-article
1444     "o" gnus-summary-pop-article)
1445
1446   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1447     "k" gnus-summary-kill-thread
1448     "l" gnus-summary-lower-thread
1449     "i" gnus-summary-raise-thread
1450     "T" gnus-summary-toggle-threads
1451     "t" gnus-summary-rethread-current
1452     "^" gnus-summary-reparent-thread
1453     "s" gnus-summary-show-thread
1454     "S" gnus-summary-show-all-threads
1455     "h" gnus-summary-hide-thread
1456     "H" gnus-summary-hide-all-threads
1457     "n" gnus-summary-next-thread
1458     "p" gnus-summary-prev-thread
1459     "u" gnus-summary-up-thread
1460     "o" gnus-summary-top-thread
1461     "d" gnus-summary-down-thread
1462     "#" gnus-uu-mark-thread
1463     "\M-#" gnus-uu-unmark-thread)
1464
1465   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1466     "g" gnus-summary-prepare
1467     "c" gnus-summary-insert-cached-articles)
1468
1469   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1470     "c" gnus-summary-catchup-and-exit
1471     "C" gnus-summary-catchup-all-and-exit
1472     "E" gnus-summary-exit-no-update
1473     "Q" gnus-summary-exit
1474     "Z" gnus-summary-exit
1475     "n" gnus-summary-catchup-and-goto-next-group
1476     "R" gnus-summary-reselect-current-group
1477     "G" gnus-summary-rescan-group
1478     "N" gnus-summary-next-group
1479     "s" gnus-summary-save-newsrc
1480     "P" gnus-summary-prev-group)
1481
1482   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1483     " " gnus-summary-next-page
1484     "n" gnus-summary-next-page
1485     "\177" gnus-summary-prev-page
1486     [delete] gnus-summary-prev-page
1487     "p" gnus-summary-prev-page
1488     "\r" gnus-summary-scroll-up
1489     "\M-\r" gnus-summary-scroll-down
1490     "<" gnus-summary-beginning-of-article
1491     ">" gnus-summary-end-of-article
1492     "b" gnus-summary-beginning-of-article
1493     "e" gnus-summary-end-of-article
1494     "^" gnus-summary-refer-parent-article
1495     "r" gnus-summary-refer-parent-article
1496     "D" gnus-summary-enter-digest-group
1497     "R" gnus-summary-refer-references
1498     "T" gnus-summary-refer-thread
1499     "g" gnus-summary-show-article
1500     "s" gnus-summary-isearch-article
1501     "P" gnus-summary-print-article
1502     "t" gnus-article-babel)
1503
1504   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1505     "b" gnus-article-add-buttons
1506     "B" gnus-article-add-buttons-to-head
1507     "o" gnus-article-treat-overstrike
1508     "e" gnus-article-emphasize
1509     "w" gnus-article-fill-cited-article
1510     "Q" gnus-article-fill-long-lines
1511     "C" gnus-article-capitalize-sentences
1512     "c" gnus-article-remove-cr
1513     "q" gnus-article-de-quoted-unreadable
1514     "f" gnus-article-display-x-face
1515     "l" gnus-summary-stop-page-breaking
1516     "r" gnus-summary-caesar-message
1517     "t" gnus-article-hide-headers
1518     "v" gnus-summary-verbose-headers
1519     "H" gnus-article-strip-headers-in-body
1520     "d" gnus-article-treat-dumbquotes)
1521
1522   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1523     "a" gnus-article-hide
1524     "h" gnus-article-hide-headers
1525     "b" gnus-article-hide-boring-headers
1526     "s" gnus-article-hide-signature
1527     "c" gnus-article-hide-citation
1528     "C" gnus-article-hide-citation-in-followups
1529     "l" gnus-article-hide-list-identifiers
1530     "p" gnus-article-hide-pgp
1531     "B" gnus-article-strip-banner
1532     "P" gnus-article-hide-pem
1533     "\C-c" gnus-article-hide-citation-maybe)
1534
1535   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1536     "a" gnus-article-highlight
1537     "h" gnus-article-highlight-headers
1538     "c" gnus-article-highlight-citation
1539     "s" gnus-article-highlight-signature)
1540
1541   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1542     "w" gnus-article-decode-mime-words
1543     "c" gnus-article-decode-charset
1544     "v" gnus-mime-view-all-parts
1545     "b" gnus-article-view-part)
1546
1547   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1548     "z" gnus-article-date-ut
1549     "u" gnus-article-date-ut
1550     "l" gnus-article-date-local
1551     "e" gnus-article-date-lapsed
1552     "o" gnus-article-date-original
1553     "i" gnus-article-date-iso8601
1554     "s" gnus-article-date-user)
1555
1556   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1557     "t" gnus-article-remove-trailing-blank-lines
1558     "l" gnus-article-strip-leading-blank-lines
1559     "m" gnus-article-strip-multiple-blank-lines
1560     "a" gnus-article-strip-blank-lines
1561     "A" gnus-article-strip-all-blank-lines
1562     "s" gnus-article-strip-leading-space
1563     "e" gnus-article-strip-trailing-space)
1564
1565   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1566     "v" gnus-version
1567     "f" gnus-summary-fetch-faq
1568     "d" gnus-summary-describe-group
1569     "h" gnus-summary-describe-briefly
1570     "i" gnus-info-find-node)
1571
1572   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1573     "e" gnus-summary-expire-articles
1574     "\M-\C-e" gnus-summary-expire-articles-now
1575     "\177" gnus-summary-delete-article
1576     [delete] gnus-summary-delete-article
1577     [backspace] gnus-summary-delete-article
1578     "m" gnus-summary-move-article
1579     "r" gnus-summary-respool-article
1580     "w" gnus-summary-edit-article
1581     "c" gnus-summary-copy-article
1582     "B" gnus-summary-crosspost-article
1583     "q" gnus-summary-respool-query
1584     "t" gnus-summary-respool-trace
1585     "i" gnus-summary-import-article
1586     "p" gnus-summary-article-posted-p)
1587
1588   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1589     "o" gnus-summary-save-article
1590     "m" gnus-summary-save-article-mail
1591     "F" gnus-summary-write-article-file
1592     "r" gnus-summary-save-article-rmail
1593     "f" gnus-summary-save-article-file
1594     "b" gnus-summary-save-article-body-file
1595     "h" gnus-summary-save-article-folder
1596     "v" gnus-summary-save-article-vm
1597     "p" gnus-summary-pipe-output
1598     "s" gnus-soup-add-article)
1599
1600   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1601     "b" gnus-summary-display-buttonized
1602     "m" gnus-summary-repair-multipart
1603     "v" gnus-article-view-part
1604     "o" gnus-article-save-part
1605     "c" gnus-article-copy-part
1606     "e" gnus-article-externalize-part
1607     "i" gnus-article-inline-part
1608     "|" gnus-article-pipe-part)
1609   )
1610
1611 (defun gnus-summary-make-menu-bar ()
1612   (gnus-turn-off-edit-menu 'summary)
1613
1614   (unless (boundp 'gnus-summary-misc-menu)
1615
1616     (easy-menu-define
1617      gnus-summary-kill-menu gnus-summary-mode-map ""
1618      (cons
1619       "Score"
1620       (nconc
1621        (list
1622         ["Enter score..." gnus-summary-score-entry t]
1623         ["Customize" gnus-score-customize t])
1624        (gnus-make-score-map 'increase)
1625        (gnus-make-score-map 'lower)
1626        '(("Mark"
1627           ["Kill below" gnus-summary-kill-below t]
1628           ["Mark above" gnus-summary-mark-above t]
1629           ["Tick above" gnus-summary-tick-above t]
1630           ["Clear above" gnus-summary-clear-above t])
1631          ["Current score" gnus-summary-current-score t]
1632          ["Set score" gnus-summary-set-score t]
1633          ["Switch current score file..." gnus-score-change-score-file t]
1634          ["Set mark below..." gnus-score-set-mark-below t]
1635          ["Set expunge below..." gnus-score-set-expunge-below t]
1636          ["Edit current score file" gnus-score-edit-current-scores t]
1637          ["Edit score file" gnus-score-edit-file t]
1638          ["Trace score" gnus-score-find-trace t]
1639          ["Find words" gnus-score-find-favourite-words t]
1640          ["Rescore buffer" gnus-summary-rescore t]
1641          ["Increase score..." gnus-summary-increase-score t]
1642          ["Lower score..." gnus-summary-lower-score t]))))
1643
1644     ;; Define both the Article menu in the summary buffer and the equivalent
1645     ;; Commands menu in the article buffer here for consistency.
1646     (let ((innards
1647            '(("Hide"
1648               ["All" gnus-article-hide t]
1649               ["Headers" gnus-article-hide-headers t]
1650               ["Signature" gnus-article-hide-signature t]
1651               ["Citation" gnus-article-hide-citation t]
1652               ["List identifiers" gnus-article-hide-list-identifiers t]
1653               ["PGP" gnus-article-hide-pgp t]
1654               ["Banner" gnus-article-strip-banner t]
1655               ["Boring headers" gnus-article-hide-boring-headers t])
1656              ("Highlight"
1657               ["All" gnus-article-highlight t]
1658               ["Headers" gnus-article-highlight-headers t]
1659               ["Signature" gnus-article-highlight-signature t]
1660               ["Citation" gnus-article-highlight-citation t])
1661              ("MIME"
1662               ["Words" gnus-article-decode-mime-words t]
1663               ["Charset" gnus-article-decode-charset t]
1664               ["QP" gnus-article-de-quoted-unreadable t]
1665               ["View all" gnus-mime-view-all-parts t])
1666              ("Date"
1667               ["Local" gnus-article-date-local t]
1668               ["ISO8601" gnus-article-date-iso8601 t]
1669               ["UT" gnus-article-date-ut t]
1670               ["Original" gnus-article-date-original t]
1671               ["Lapsed" gnus-article-date-lapsed t]
1672               ["User-defined" gnus-article-date-user t])
1673              ("Washing"
1674               ("Remove Blanks"
1675                ["Leading" gnus-article-strip-leading-blank-lines t]
1676                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1677                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1678                ["All of the above" gnus-article-strip-blank-lines t]
1679                ["All" gnus-article-strip-all-blank-lines t]
1680                ["Leading space" gnus-article-strip-leading-space t]
1681                ["Trailing space" gnus-article-strip-trailing-space t])
1682               ["Overstrike" gnus-article-treat-overstrike t]
1683               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1684               ["Emphasis" gnus-article-emphasize t]
1685               ["Word wrap" gnus-article-fill-cited-article t]
1686               ["Fill long lines" gnus-article-fill-long-lines t]
1687               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1688               ["CR" gnus-article-remove-cr t]
1689               ["Show X-Face" gnus-article-display-x-face t]
1690               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1691               ["Rot 13" gnus-summary-caesar-message t]
1692               ["Unix pipe" gnus-summary-pipe-message t]
1693               ["Add buttons" gnus-article-add-buttons t]
1694               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1695               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1696               ["Verbose header" gnus-summary-verbose-headers t]
1697               ["Toggle header" gnus-summary-toggle-header t])
1698              ("Output"
1699               ["Save in default format" gnus-summary-save-article t]
1700               ["Save in file" gnus-summary-save-article-file t]
1701               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1702               ["Save in MH folder" gnus-summary-save-article-folder t]
1703               ["Save in VM folder" gnus-summary-save-article-vm t]
1704               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1705               ["Save body in file" gnus-summary-save-article-body-file t]
1706               ["Pipe through a filter" gnus-summary-pipe-output t]
1707               ["Add to SOUP packet" gnus-soup-add-article t]
1708               ["Print" gnus-summary-print-article t])
1709              ("Backend"
1710               ["Respool article..." gnus-summary-respool-article t]
1711               ["Move article..." gnus-summary-move-article
1712                (gnus-check-backend-function
1713                 'request-move-article gnus-newsgroup-name)]
1714               ["Copy article..." gnus-summary-copy-article t]
1715               ["Crosspost article..." gnus-summary-crosspost-article
1716                (gnus-check-backend-function
1717                 'request-replace-article gnus-newsgroup-name)]
1718               ["Import file..." gnus-summary-import-article t]
1719               ["Check if posted" gnus-summary-article-posted-p t]
1720               ["Edit article" gnus-summary-edit-article
1721                (not (gnus-group-read-only-p))]
1722               ["Delete article" gnus-summary-delete-article
1723                (gnus-check-backend-function
1724                 'request-expire-articles gnus-newsgroup-name)]
1725               ["Query respool" gnus-summary-respool-query t]
1726               ["Trace respool" gnus-summary-respool-trace t]
1727               ["Delete expirable articles" gnus-summary-expire-articles-now
1728                (gnus-check-backend-function
1729                 'request-expire-articles gnus-newsgroup-name)])
1730              ("Extract"
1731               ["Uudecode" gnus-uu-decode-uu t]
1732               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1733               ["Unshar" gnus-uu-decode-unshar t]
1734               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1735               ["Save" gnus-uu-decode-save t]
1736               ["Binhex" gnus-uu-decode-binhex t]
1737               ["Postscript" gnus-uu-decode-postscript t])
1738              ("Cache"
1739               ["Enter article" gnus-cache-enter-article t]
1740               ["Remove article" gnus-cache-remove-article t])
1741              ["Translate" gnus-article-babel t]
1742              ["Select article buffer" gnus-summary-select-article-buffer t]
1743              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1744              ["Isearch article..." gnus-summary-isearch-article t]
1745              ["Beginning of the article" gnus-summary-beginning-of-article t]
1746              ["End of the article" gnus-summary-end-of-article t]
1747              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1748              ["Fetch referenced articles" gnus-summary-refer-references t]
1749              ["Fetch current thread" gnus-summary-refer-thread t]
1750              ["Fetch article with id..." gnus-summary-refer-article t]
1751              ["Redisplay" gnus-summary-show-article t])))
1752       (easy-menu-define
1753        gnus-summary-article-menu gnus-summary-mode-map ""
1754        (cons "Article" innards))
1755
1756       (easy-menu-define
1757        gnus-article-commands-menu gnus-article-mode-map ""
1758        (cons "Commands" innards)))
1759
1760     (easy-menu-define
1761      gnus-summary-thread-menu gnus-summary-mode-map ""
1762      '("Threads"
1763        ["Toggle threading" gnus-summary-toggle-threads t]
1764        ["Hide threads" gnus-summary-hide-all-threads t]
1765        ["Show threads" gnus-summary-show-all-threads t]
1766        ["Hide thread" gnus-summary-hide-thread t]
1767        ["Show thread" gnus-summary-show-thread t]
1768        ["Go to next thread" gnus-summary-next-thread t]
1769        ["Go to previous thread" gnus-summary-prev-thread t]
1770        ["Go down thread" gnus-summary-down-thread t]
1771        ["Go up thread" gnus-summary-up-thread t]
1772        ["Top of thread" gnus-summary-top-thread t]
1773        ["Mark thread as read" gnus-summary-kill-thread t]
1774        ["Lower thread score" gnus-summary-lower-thread t]
1775        ["Raise thread score" gnus-summary-raise-thread t]
1776        ["Rethread current" gnus-summary-rethread-current t]
1777        ))
1778
1779     (easy-menu-define
1780      gnus-summary-post-menu gnus-summary-mode-map ""
1781      '("Post"
1782        ["Post an article" gnus-summary-post-news t]
1783        ["Followup" gnus-summary-followup t]
1784        ["Followup and yank" gnus-summary-followup-with-original t]
1785        ["Supersede article" gnus-summary-supersede-article t]
1786        ["Cancel article" gnus-summary-cancel-article t]
1787        ["Reply" gnus-summary-reply t]
1788        ["Reply and yank" gnus-summary-reply-with-original t]
1789        ["Wide reply" gnus-summary-wide-reply t]
1790        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1791        ["Mail forward" gnus-summary-mail-forward t]
1792        ["Post forward" gnus-summary-post-forward t]
1793        ["Digest and mail" gnus-uu-digest-mail-forward t]
1794        ["Digest and post" gnus-uu-digest-post-forward t]
1795        ["Resend message" gnus-summary-resend-message t]
1796        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1797        ["Send a mail" gnus-summary-mail-other-window t]
1798        ["Uuencode and post" gnus-uu-post-news t]
1799        ["Followup via news" gnus-summary-followup-to-mail t]
1800        ["Followup via news and yank"
1801         gnus-summary-followup-to-mail-with-original t]
1802        ;;("Draft"
1803        ;;["Send" gnus-summary-send-draft t]
1804        ;;["Send bounced" gnus-resend-bounced-mail t])
1805        ))
1806
1807     (easy-menu-define
1808      gnus-summary-misc-menu gnus-summary-mode-map ""
1809      '("Misc"
1810        ("Mark Read"
1811         ["Mark as read" gnus-summary-mark-as-read-forward t]
1812         ["Mark same subject and select"
1813          gnus-summary-kill-same-subject-and-select t]
1814         ["Mark same subject" gnus-summary-kill-same-subject t]
1815         ["Catchup" gnus-summary-catchup t]
1816         ["Catchup all" gnus-summary-catchup-all t]
1817         ["Catchup to here" gnus-summary-catchup-to-here t]
1818         ["Catchup region" gnus-summary-mark-region-as-read t]
1819         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1820        ("Mark Various"
1821         ["Tick" gnus-summary-tick-article-forward t]
1822         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1823         ["Remove marks" gnus-summary-clear-mark-forward t]
1824         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1825         ["Set bookmark" gnus-summary-set-bookmark t]
1826         ["Remove bookmark" gnus-summary-remove-bookmark t])
1827        ("Mark Limit"
1828         ["Marks..." gnus-summary-limit-to-marks t]
1829         ["Subject..." gnus-summary-limit-to-subject t]
1830         ["Author..." gnus-summary-limit-to-author t]
1831         ["Age..." gnus-summary-limit-to-age t]
1832         ["Extra..." gnus-summary-limit-to-extra t]
1833         ["Score" gnus-summary-limit-to-score t]
1834         ["Unread" gnus-summary-limit-to-unread t]
1835         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1836         ["Articles" gnus-summary-limit-to-articles t]
1837         ["Pop limit" gnus-summary-pop-limit t]
1838         ["Show dormant" gnus-summary-limit-include-dormant t]
1839         ["Hide childless dormant"
1840          gnus-summary-limit-exclude-childless-dormant t]
1841         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1842         ["Hide marked" gnus-summary-limit-exclude-marks t]
1843         ["Show expunged" gnus-summary-show-all-expunged t])
1844        ("Process Mark"
1845         ["Set mark" gnus-summary-mark-as-processable t]
1846         ["Remove mark" gnus-summary-unmark-as-processable t]
1847         ["Remove all marks" gnus-summary-unmark-all-processable t]
1848         ["Mark above" gnus-uu-mark-over t]
1849         ["Mark series" gnus-uu-mark-series t]
1850         ["Mark region" gnus-uu-mark-region t]
1851         ["Unmark region" gnus-uu-unmark-region t]
1852         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1853         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1854         ["Mark all" gnus-uu-mark-all t]
1855         ["Mark buffer" gnus-uu-mark-buffer t]
1856         ["Mark sparse" gnus-uu-mark-sparse t]
1857         ["Mark thread" gnus-uu-mark-thread t]
1858         ["Unmark thread" gnus-uu-unmark-thread t]
1859         ("Process Mark Sets"
1860          ["Kill" gnus-summary-kill-process-mark t]
1861          ["Yank" gnus-summary-yank-process-mark
1862           gnus-newsgroup-process-stack]
1863          ["Save" gnus-summary-save-process-mark t]))
1864        ("Scroll article"
1865         ["Page forward" gnus-summary-next-page t]
1866         ["Page backward" gnus-summary-prev-page t]
1867         ["Line forward" gnus-summary-scroll-up t])
1868        ("Move"
1869         ["Next unread article" gnus-summary-next-unread-article t]
1870         ["Previous unread article" gnus-summary-prev-unread-article t]
1871         ["Next article" gnus-summary-next-article t]
1872         ["Previous article" gnus-summary-prev-article t]
1873         ["Next unread subject" gnus-summary-next-unread-subject t]
1874         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1875         ["Next article same subject" gnus-summary-next-same-subject t]
1876         ["Previous article same subject" gnus-summary-prev-same-subject t]
1877         ["First unread article" gnus-summary-first-unread-article t]
1878         ["Best unread article" gnus-summary-best-unread-article t]
1879         ["Go to subject number..." gnus-summary-goto-subject t]
1880         ["Go to article number..." gnus-summary-goto-article t]
1881         ["Go to the last article" gnus-summary-goto-last-article t]
1882         ["Pop article off history" gnus-summary-pop-article t])
1883        ("Sort"
1884         ["Sort by number" gnus-summary-sort-by-number t]
1885         ["Sort by author" gnus-summary-sort-by-author t]
1886         ["Sort by subject" gnus-summary-sort-by-subject t]
1887         ["Sort by date" gnus-summary-sort-by-date t]
1888         ["Sort by score" gnus-summary-sort-by-score t]
1889         ["Sort by lines" gnus-summary-sort-by-lines t]
1890         ["Sort by characters" gnus-summary-sort-by-chars t])
1891        ("Help"
1892         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1893         ["Describe group" gnus-summary-describe-group t]
1894         ["Read manual" gnus-info-find-node t])
1895        ("Modes"
1896         ["Pick and read" gnus-pick-mode t]
1897         ["Binary" gnus-binary-mode t])
1898        ("Regeneration"
1899         ["Regenerate" gnus-summary-prepare t]
1900         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1901         ["Toggle threading" gnus-summary-toggle-threads t])
1902        ["Filter articles..." gnus-summary-execute-command t]
1903        ["Run command on subjects..." gnus-summary-universal-argument t]
1904        ["Search articles forward..." gnus-summary-search-article-forward t]
1905        ["Search articles backward..." gnus-summary-search-article-backward t]
1906        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1907        ["Expand window" gnus-summary-expand-window t]
1908        ["Expire expirable articles" gnus-summary-expire-articles
1909         (gnus-check-backend-function
1910          'request-expire-articles gnus-newsgroup-name)]
1911        ["Edit local kill file" gnus-summary-edit-local-kill t]
1912        ["Edit main kill file" gnus-summary-edit-global-kill t]
1913        ["Edit group parameters" gnus-summary-edit-parameters t]
1914        ["Customize group parameters" gnus-summary-customize-parameters t]
1915        ["Send a bug report" gnus-bug t]
1916        ("Exit"
1917         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1918         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1919         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1920         ["Exit group" gnus-summary-exit t]
1921         ["Exit group without updating" gnus-summary-exit-no-update t]
1922         ["Exit and goto next group" gnus-summary-next-group t]
1923         ["Exit and goto prev group" gnus-summary-prev-group t]
1924         ["Reselect group" gnus-summary-reselect-current-group t]
1925         ["Rescan group" gnus-summary-rescan-group t]
1926         ["Update dribble" gnus-summary-save-newsrc t])))
1927
1928     (gnus-run-hooks 'gnus-summary-menu-hook)))
1929
1930 (defun gnus-score-set-default (var value)
1931   "A version of set that updates the GNU Emacs menu-bar."
1932   (set var value)
1933   ;; It is the message that forces the active status to be updated.
1934   (message ""))
1935
1936 (defun gnus-make-score-map (type)
1937   "Make a summary score map of type TYPE."
1938   (if t
1939       nil
1940     (let ((headers '(("author" "from" string)
1941                      ("subject" "subject" string)
1942                      ("article body" "body" string)
1943                      ("article head" "head" string)
1944                      ("xref" "xref" string)
1945                      ("extra header" "extra" string)
1946                      ("lines" "lines" number)
1947                      ("followups to author" "followup" string)))
1948           (types '((number ("less than" <)
1949                            ("greater than" >)
1950                            ("equal" =))
1951                    (string ("substring" s)
1952                            ("exact string" e)
1953                            ("fuzzy string" f)
1954                            ("regexp" r))))
1955           (perms '(("temporary" (current-time-string))
1956                    ("permanent" nil)
1957                    ("immediate" now)))
1958           header)
1959       (list
1960        (apply
1961         'nconc
1962         (list
1963          (if (eq type 'lower)
1964              "Lower score"
1965            "Increase score"))
1966         (let (outh)
1967           (while headers
1968             (setq header (car headers))
1969             (setq outh
1970                   (cons
1971                    (apply
1972                     'nconc
1973                     (list (car header))
1974                     (let ((ts (cdr (assoc (nth 2 header) types)))
1975                           outt)
1976                       (while ts
1977                         (setq outt
1978                               (cons
1979                                (apply
1980                                 'nconc
1981                                 (list (caar ts))
1982                                 (let ((ps perms)
1983                                       outp)
1984                                   (while ps
1985                                     (setq outp
1986                                           (cons
1987                                            (vector
1988                                             (caar ps)
1989                                             (list
1990                                              'gnus-summary-score-entry
1991                                              (nth 1 header)
1992                                              (if (or (string= (nth 1 header)
1993                                                               "head")
1994                                                      (string= (nth 1 header)
1995                                                               "body"))
1996                                                  ""
1997                                                (list 'gnus-summary-header
1998                                                      (nth 1 header)))
1999                                              (list 'quote (nth 1 (car ts)))
2000                                              (list 'gnus-score-default nil)
2001                                              (nth 1 (car ps))
2002                                              t)
2003                                             t)
2004                                            outp))
2005                                     (setq ps (cdr ps)))
2006                                   (list (nreverse outp))))
2007                                outt))
2008                         (setq ts (cdr ts)))
2009                       (list (nreverse outt))))
2010                    outh))
2011             (setq headers (cdr headers)))
2012           (list (nreverse outh))))))))
2013
2014 \f
2015
2016 (defun gnus-summary-mode (&optional group)
2017   "Major mode for reading articles.
2018
2019 All normal editing commands are switched off.
2020 \\<gnus-summary-mode-map>
2021 Each line in this buffer represents one article.  To read an
2022 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2023 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2024 respectively.
2025
2026 You can also post articles and send mail from this buffer.  To
2027 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2028 of an article, type `\\[gnus-summary-reply]'.
2029
2030 There are approx. one gazillion commands you can execute in this
2031 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2032
2033 The following commands are available:
2034
2035 \\{gnus-summary-mode-map}"
2036   (interactive)
2037   (when (gnus-visual-p 'summary-menu 'menu)
2038     (gnus-summary-make-menu-bar))
2039   (kill-all-local-variables)
2040   (gnus-summary-make-local-variables)
2041   (gnus-make-thread-indent-array)
2042   (gnus-simplify-mode-line)
2043   (setq major-mode 'gnus-summary-mode)
2044   (setq mode-name "Summary")
2045   (make-local-variable 'minor-mode-alist)
2046   (use-local-map gnus-summary-mode-map)
2047   (buffer-disable-undo)
2048   (setq buffer-read-only t)             ;Disable modification
2049   (setq truncate-lines t)
2050   (setq selective-display t)
2051   (setq selective-display-ellipses t)   ;Display `...'
2052   (gnus-summary-set-display-table)
2053   (gnus-set-default-directory)
2054   (setq gnus-newsgroup-name group)
2055   (make-local-variable 'gnus-summary-line-format)
2056   (make-local-variable 'gnus-summary-line-format-spec)
2057   (make-local-variable 'gnus-summary-dummy-line-format)
2058   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2059   (make-local-variable 'gnus-summary-mark-positions)
2060   (make-local-hook 'pre-command-hook)
2061   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2062   (gnus-run-hooks 'gnus-summary-mode-hook)
2063   (mm-enable-multibyte)
2064   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2065   (gnus-update-summary-mark-positions))
2066
2067 (defun gnus-summary-make-local-variables ()
2068   "Make all the local summary buffer variables."
2069   (let (global)
2070     (dolist (local gnus-summary-local-variables)
2071       (if (consp local)
2072           (progn
2073             (if (eq (cdr local) 'global)
2074                 ;; Copy the global value of the variable.
2075                 (setq global (symbol-value (car local)))
2076               ;; Use the value from the list.
2077               (setq global (eval (cdr local))))
2078             (set (make-local-variable (car local)) global))
2079         ;; Simple nil-valued local variable.
2080         (set (make-local-variable local) nil)))))
2081
2082 (defun gnus-summary-clear-local-variables ()
2083   (let ((locals gnus-summary-local-variables))
2084     (while locals
2085       (if (consp (car locals))
2086           (and (vectorp (caar locals))
2087                (set (caar locals) nil))
2088         (and (vectorp (car locals))
2089              (set (car locals) nil)))
2090       (setq locals (cdr locals)))))
2091
2092 ;; Summary data functions.
2093
2094 (defmacro gnus-data-number (data)
2095   `(car ,data))
2096
2097 (defmacro gnus-data-set-number (data number)
2098   `(setcar ,data ,number))
2099
2100 (defmacro gnus-data-mark (data)
2101   `(nth 1 ,data))
2102
2103 (defmacro gnus-data-set-mark (data mark)
2104   `(setcar (nthcdr 1 ,data) ,mark))
2105
2106 (defmacro gnus-data-pos (data)
2107   `(nth 2 ,data))
2108
2109 (defmacro gnus-data-set-pos (data pos)
2110   `(setcar (nthcdr 2 ,data) ,pos))
2111
2112 (defmacro gnus-data-header (data)
2113   `(nth 3 ,data))
2114
2115 (defmacro gnus-data-set-header (data header)
2116   `(setf (nth 3 ,data) ,header))
2117
2118 (defmacro gnus-data-level (data)
2119   `(nth 4 ,data))
2120
2121 (defmacro gnus-data-unread-p (data)
2122   `(= (nth 1 ,data) gnus-unread-mark))
2123
2124 (defmacro gnus-data-read-p (data)
2125   `(/= (nth 1 ,data) gnus-unread-mark))
2126
2127 (defmacro gnus-data-pseudo-p (data)
2128   `(consp (nth 3 ,data)))
2129
2130 (defmacro gnus-data-find (number)
2131   `(assq ,number gnus-newsgroup-data))
2132
2133 (defmacro gnus-data-find-list (number &optional data)
2134   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2135      (memq (assq ,number bdata)
2136            bdata)))
2137
2138 (defmacro gnus-data-make (number mark pos header level)
2139   `(list ,number ,mark ,pos ,header ,level))
2140
2141 (defun gnus-data-enter (after-article number mark pos header level offset)
2142   (let ((data (gnus-data-find-list after-article)))
2143     (unless data
2144       (error "No such article: %d" after-article))
2145     (setcdr data (cons (gnus-data-make number mark pos header level)
2146                        (cdr data)))
2147     (setq gnus-newsgroup-data-reverse nil)
2148     (gnus-data-update-list (cddr data) offset)))
2149
2150 (defun gnus-data-enter-list (after-article list &optional offset)
2151   (when list
2152     (let ((data (and after-article (gnus-data-find-list after-article)))
2153           (ilist list))
2154       (if (not (or data
2155                    after-article))
2156           (let ((odata gnus-newsgroup-data))
2157             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2158             (when offset
2159               (gnus-data-update-list odata offset)))
2160         ;; Find the last element in the list to be spliced into the main
2161         ;; list.
2162         (while (cdr list)
2163           (setq list (cdr list)))
2164         (if (not data)
2165             (progn
2166               (setcdr list gnus-newsgroup-data)
2167               (setq gnus-newsgroup-data ilist)
2168               (when offset
2169                 (gnus-data-update-list (cdr list) offset)))
2170           (setcdr list (cdr data))
2171           (setcdr data ilist)
2172           (when offset
2173             (gnus-data-update-list (cdr list) offset))))
2174       (setq gnus-newsgroup-data-reverse nil))))
2175
2176 (defun gnus-data-remove (article &optional offset)
2177   (let ((data gnus-newsgroup-data))
2178     (if (= (gnus-data-number (car data)) article)
2179         (progn
2180           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2181                 gnus-newsgroup-data-reverse nil)
2182           (when offset
2183             (gnus-data-update-list gnus-newsgroup-data offset)))
2184       (while (cdr data)
2185         (when (= (gnus-data-number (cadr data)) article)
2186           (setcdr data (cddr data))
2187           (when offset
2188             (gnus-data-update-list (cdr data) offset))
2189           (setq data nil
2190                 gnus-newsgroup-data-reverse nil))
2191         (setq data (cdr data))))))
2192
2193 (defmacro gnus-data-list (backward)
2194   `(if ,backward
2195        (or gnus-newsgroup-data-reverse
2196            (setq gnus-newsgroup-data-reverse
2197                  (reverse gnus-newsgroup-data)))
2198      gnus-newsgroup-data))
2199
2200 (defun gnus-data-update-list (data offset)
2201   "Add OFFSET to the POS of all data entries in DATA."
2202   (setq gnus-newsgroup-data-reverse nil)
2203   (while data
2204     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2205     (setq data (cdr data))))
2206
2207 (defun gnus-summary-article-pseudo-p (article)
2208   "Say whether this article is a pseudo article or not."
2209   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2210
2211 (defmacro gnus-summary-article-sparse-p (article)
2212   "Say whether this article is a sparse article or not."
2213   `(memq ,article gnus-newsgroup-sparse))
2214
2215 (defmacro gnus-summary-article-ancient-p (article)
2216   "Say whether this article is a sparse article or not."
2217   `(memq ,article gnus-newsgroup-ancient))
2218
2219 (defun gnus-article-parent-p (number)
2220   "Say whether this article is a parent or not."
2221   (let ((data (gnus-data-find-list number)))
2222     (and (cdr data)                     ; There has to be an article after...
2223          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2224             (gnus-data-level (nth 1 data))))))
2225
2226 (defun gnus-article-children (number)
2227   "Return a list of all children to NUMBER."
2228   (let* ((data (gnus-data-find-list number))
2229          (level (gnus-data-level (car data)))
2230          children)
2231     (setq data (cdr data))
2232     (while (and data
2233                 (= (gnus-data-level (car data)) (1+ level)))
2234       (push (gnus-data-number (car data)) children)
2235       (setq data (cdr data)))
2236     children))
2237
2238 (defmacro gnus-summary-skip-intangible ()
2239   "If the current article is intangible, then jump to a different article."
2240   '(let ((to (get-text-property (point) 'gnus-intangible)))
2241      (and to (gnus-summary-goto-subject to))))
2242
2243 (defmacro gnus-summary-article-intangible-p ()
2244   "Say whether this article is intangible or not."
2245   '(get-text-property (point) 'gnus-intangible))
2246
2247 (defun gnus-article-read-p (article)
2248   "Say whether ARTICLE is read or not."
2249   (not (or (memq article gnus-newsgroup-marked)
2250            (memq article gnus-newsgroup-unreads)
2251            (memq article gnus-newsgroup-unselected)
2252            (memq article gnus-newsgroup-dormant))))
2253
2254 ;; Some summary mode macros.
2255
2256 (defmacro gnus-summary-article-number ()
2257   "The article number of the article on the current line.
2258 If there isn's an article number here, then we return the current
2259 article number."
2260   '(progn
2261      (gnus-summary-skip-intangible)
2262      (or (get-text-property (point) 'gnus-number)
2263          (gnus-summary-last-subject))))
2264
2265 (defmacro gnus-summary-article-header (&optional number)
2266   "Return the header of article NUMBER."
2267   `(gnus-data-header (gnus-data-find
2268                       ,(or number '(gnus-summary-article-number)))))
2269
2270 (defmacro gnus-summary-thread-level (&optional number)
2271   "Return the level of thread that starts with article NUMBER."
2272   `(if (and (eq gnus-summary-make-false-root 'dummy)
2273             (get-text-property (point) 'gnus-intangible))
2274        0
2275      (gnus-data-level (gnus-data-find
2276                        ,(or number '(gnus-summary-article-number))))))
2277
2278 (defmacro gnus-summary-article-mark (&optional number)
2279   "Return the mark of article NUMBER."
2280   `(gnus-data-mark (gnus-data-find
2281                     ,(or number '(gnus-summary-article-number)))))
2282
2283 (defmacro gnus-summary-article-pos (&optional number)
2284   "Return the position of the line of article NUMBER."
2285   `(gnus-data-pos (gnus-data-find
2286                    ,(or number '(gnus-summary-article-number)))))
2287
2288 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2289 (defmacro gnus-summary-article-subject (&optional number)
2290   "Return current subject string or nil if nothing."
2291   `(let ((headers
2292           ,(if number
2293                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2294              '(gnus-data-header (assq (gnus-summary-article-number)
2295                                       gnus-newsgroup-data)))))
2296      (and headers
2297           (vectorp headers)
2298           (mail-header-subject headers))))
2299
2300 (defmacro gnus-summary-article-score (&optional number)
2301   "Return current article score."
2302   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2303                   gnus-newsgroup-scored))
2304        gnus-summary-default-score 0))
2305
2306 (defun gnus-summary-article-children (&optional number)
2307   "Return a list of article numbers that are children of article NUMBER."
2308   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2309          (level (gnus-data-level (car data)))
2310          l children)
2311     (while (and (setq data (cdr data))
2312                 (> (setq l (gnus-data-level (car data))) level))
2313       (and (= (1+ level) l)
2314            (push (gnus-data-number (car data))
2315                  children)))
2316     (nreverse children)))
2317
2318 (defun gnus-summary-article-parent (&optional number)
2319   "Return the article number of the parent of article NUMBER."
2320   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2321                                     (gnus-data-list t)))
2322          (level (gnus-data-level (car data))))
2323     (if (zerop level)
2324         ()                              ; This is a root.
2325       ;; We search until we find an article with a level less than
2326       ;; this one.  That function has to be the parent.
2327       (while (and (setq data (cdr data))
2328                   (not (< (gnus-data-level (car data)) level))))
2329       (and data (gnus-data-number (car data))))))
2330
2331 (defun gnus-unread-mark-p (mark)
2332   "Say whether MARK is the unread mark."
2333   (= mark gnus-unread-mark))
2334
2335 (defun gnus-read-mark-p (mark)
2336   "Say whether MARK is one of the marks that mark as read.
2337 This is all marks except unread, ticked, dormant, and expirable."
2338   (not (or (= mark gnus-unread-mark)
2339            (= mark gnus-ticked-mark)
2340            (= mark gnus-dormant-mark)
2341            (= mark gnus-expirable-mark))))
2342
2343 (defmacro gnus-article-mark (number)
2344   "Return the MARK of article NUMBER.
2345 This macro should only be used when computing the mark the \"first\"
2346 time; i.e., when generating the summary lines.  After that,
2347 `gnus-summary-article-mark' should be used to examine the
2348 marks of articles."
2349   `(cond
2350     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2351     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2352     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2353     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2354     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2355     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2356     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2357     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2358            gnus-ancient-mark))))
2359
2360 ;; Saving hidden threads.
2361
2362 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2363 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2364
2365 (defmacro gnus-save-hidden-threads (&rest forms)
2366   "Save hidden threads, eval FORMS, and restore the hidden threads."
2367   (let ((config (make-symbol "config")))
2368     `(let ((,config (gnus-hidden-threads-configuration)))
2369        (unwind-protect
2370            (save-excursion
2371              ,@forms)
2372          (gnus-restore-hidden-threads-configuration ,config)))))
2373
2374 (defun gnus-data-compute-positions ()
2375   "Compute the positions of all articles."
2376   (setq gnus-newsgroup-data-reverse nil)
2377   (let ((data gnus-newsgroup-data))
2378     (save-excursion
2379       (gnus-save-hidden-threads
2380         (gnus-summary-show-all-threads)
2381         (goto-char (point-min))
2382         (while data
2383           (while (get-text-property (point) 'gnus-intangible)
2384             (forward-line 1))
2385           (gnus-data-set-pos (car data) (+ (point) 3))
2386           (setq data (cdr data))
2387           (forward-line 1))))))
2388
2389 (defun gnus-hidden-threads-configuration ()
2390   "Return the current hidden threads configuration."
2391   (save-excursion
2392     (let (config)
2393       (goto-char (point-min))
2394       (while (search-forward "\r" nil t)
2395         (push (1- (point)) config))
2396       config)))
2397
2398 (defun gnus-restore-hidden-threads-configuration (config)
2399   "Restore hidden threads configuration from CONFIG."
2400   (let (point buffer-read-only)
2401     (while (setq point (pop config))
2402       (when (and (< point (point-max))
2403                  (goto-char point)
2404                  (eq (char-after) ?\n))
2405         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2406
2407 ;; Various summary mode internalish functions.
2408
2409 (defun gnus-mouse-pick-article (e)
2410   (interactive "e")
2411   (mouse-set-point e)
2412   (gnus-summary-next-page nil t))
2413
2414 (defun gnus-summary-set-display-table ()
2415   ;; Change the display table.  Odd characters have a tendency to mess
2416   ;; up nicely formatted displays - we make all possible glyphs
2417   ;; display only a single character.
2418
2419   ;; We start from the standard display table, if any.
2420   (let ((table (or (copy-sequence standard-display-table)
2421                    (make-display-table)))
2422         (i 32))
2423     ;; Nix out all the control chars...
2424     (while (>= (setq i (1- i)) 0)
2425       (aset table i [??]))
2426     ;; ... but not newline and cr, of course.  (cr is necessary for the
2427     ;; selective display).
2428     (aset table ?\n nil)
2429     (aset table ?\r nil)
2430     ;; We keep TAB as well.
2431     (aset table ?\t nil)
2432     ;; We nix out any glyphs over 126 that are not set already.
2433     (let ((i 256))
2434       (while (>= (setq i (1- i)) 127)
2435         ;; Only modify if the entry is nil.
2436         (unless (aref table i)
2437           (aset table i [??]))))
2438     (setq buffer-display-table table)))
2439
2440 (defun gnus-summary-setup-buffer (group)
2441   "Initialize summary buffer."
2442   (let ((buffer (concat "*Summary " group "*")))
2443     (if (get-buffer buffer)
2444         (progn
2445           (set-buffer buffer)
2446           (setq gnus-summary-buffer (current-buffer))
2447           (not gnus-newsgroup-prepared))
2448       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2449       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2450       (gnus-summary-mode group)
2451       (when gnus-carpal
2452         (gnus-carpal-setup-buffer 'summary))
2453       (unless gnus-single-article-buffer
2454         (make-local-variable 'gnus-article-buffer)
2455         (make-local-variable 'gnus-article-current)
2456         (make-local-variable 'gnus-original-article-buffer))
2457       (setq gnus-newsgroup-name group)
2458       t)))
2459
2460 (defun gnus-set-global-variables ()
2461   ;; Set the global equivalents of the summary buffer-local variables
2462   ;; to the latest values they had.  These reflect the summary buffer
2463   ;; that was in action when the last article was fetched.
2464   (when (eq major-mode 'gnus-summary-mode)
2465     (setq gnus-summary-buffer (current-buffer))
2466     (let ((name gnus-newsgroup-name)
2467           (marked gnus-newsgroup-marked)
2468           (unread gnus-newsgroup-unreads)
2469           (headers gnus-current-headers)
2470           (data gnus-newsgroup-data)
2471           (summary gnus-summary-buffer)
2472           (article-buffer gnus-article-buffer)
2473           (original gnus-original-article-buffer)
2474           (gac gnus-article-current)
2475           (reffed gnus-reffed-article-number)
2476           (score-file gnus-current-score-file)
2477           (default-charset gnus-newsgroup-charset))
2478       (save-excursion
2479         (set-buffer gnus-group-buffer)
2480         (setq gnus-newsgroup-name name
2481               gnus-newsgroup-marked marked
2482               gnus-newsgroup-unreads unread
2483               gnus-current-headers headers
2484               gnus-newsgroup-data data
2485               gnus-article-current gac
2486               gnus-summary-buffer summary
2487               gnus-article-buffer article-buffer
2488               gnus-original-article-buffer original
2489               gnus-reffed-article-number reffed
2490               gnus-current-score-file score-file
2491               gnus-newsgroup-charset default-charset)
2492         ;; The article buffer also has local variables.
2493         (when (gnus-buffer-live-p gnus-article-buffer)
2494           (set-buffer gnus-article-buffer)
2495           (setq gnus-summary-buffer summary))))))
2496
2497 (defun gnus-summary-article-unread-p (article)
2498   "Say whether ARTICLE is unread or not."
2499   (memq article gnus-newsgroup-unreads))
2500
2501 (defun gnus-summary-first-article-p (&optional article)
2502   "Return whether ARTICLE is the first article in the buffer."
2503   (if (not (setq article (or article (gnus-summary-article-number))))
2504       nil
2505     (eq article (caar gnus-newsgroup-data))))
2506
2507 (defun gnus-summary-last-article-p (&optional article)
2508   "Return whether ARTICLE is the last article in the buffer."
2509   (if (not (setq article (or article (gnus-summary-article-number))))
2510       ;; All non-existent numbers are the last article.  :-)
2511       t
2512     (not (cdr (gnus-data-find-list article)))))
2513
2514 (defun gnus-make-thread-indent-array ()
2515   (let ((n 200))
2516     (unless (and gnus-thread-indent-array
2517                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2518       (setq gnus-thread-indent-array (make-vector 201 "")
2519             gnus-thread-indent-array-level gnus-thread-indent-level)
2520       (while (>= n 0)
2521         (aset gnus-thread-indent-array n
2522               (make-string (* n gnus-thread-indent-level) ? ))
2523         (setq n (1- n))))))
2524
2525 (defun gnus-update-summary-mark-positions ()
2526   "Compute where the summary marks are to go."
2527   (save-excursion
2528     (when (gnus-buffer-exists-p gnus-summary-buffer)
2529       (set-buffer gnus-summary-buffer))
2530     (let ((gnus-replied-mark 129)
2531           (gnus-score-below-mark 130)
2532           (gnus-score-over-mark 130)
2533           (gnus-download-mark 131)
2534           (spec gnus-summary-line-format-spec)
2535           gnus-visual pos)
2536       (save-excursion
2537         (gnus-set-work-buffer)
2538         (let ((gnus-summary-line-format-spec spec)
2539               (gnus-newsgroup-downloadable '((0 . t))))
2540           (gnus-summary-insert-line
2541            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2542           (goto-char (point-min))
2543           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2544                                              (- (point) 2)))))
2545           (goto-char (point-min))
2546           (push (cons 'replied (and (search-forward "\201" nil t)
2547                                     (- (point) 2)))
2548                 pos)
2549           (goto-char (point-min))
2550           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2551                 pos)
2552           (goto-char (point-min))
2553           (push (cons 'download
2554                       (and (search-forward "\203" nil t) (- (point) 2)))
2555                 pos)))
2556       (setq gnus-summary-mark-positions pos))))
2557
2558 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2559   "Insert a dummy root in the summary buffer."
2560   (beginning-of-line)
2561   (gnus-add-text-properties
2562    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2563    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2564
2565 (defun gnus-summary-from-or-to-or-newsgroups (header)
2566   (let ((to (cdr (assq 'To (mail-header-extra header))))
2567         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2568         (mail-parse-charset gnus-newsgroup-charset)
2569         (mail-parse-ignored-charsets 
2570          (save-excursion (set-buffer gnus-summary-buffer)
2571                          gnus-newsgroup-ignored-charsets)))
2572     (cond
2573      ((and to
2574            gnus-ignored-from-addresses
2575            (string-match gnus-ignored-from-addresses
2576                          (mail-header-from header)))
2577       (concat "-> "
2578               (or (car (funcall gnus-extract-address-components
2579                                 (funcall
2580                                  gnus-decode-encoded-word-function to)))
2581                   (funcall gnus-decode-encoded-word-function to))))
2582      ((and newsgroups
2583            gnus-ignored-from-addresses
2584            (string-match gnus-ignored-from-addresses
2585                          (mail-header-from header)))
2586       (concat "=> " newsgroups))
2587      (t
2588       (or (car (funcall gnus-extract-address-components
2589                         (mail-header-from header)))
2590           (mail-header-from header))))))
2591
2592 (defun gnus-summary-insert-line (gnus-tmp-header
2593                                  gnus-tmp-level gnus-tmp-current
2594                                  gnus-tmp-unread gnus-tmp-replied
2595                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2596                                  &optional gnus-tmp-dummy gnus-tmp-score
2597                                  gnus-tmp-process)
2598   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2599          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2600          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2601          (gnus-tmp-score-char
2602           (if (or (null gnus-summary-default-score)
2603                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2604                       gnus-summary-zcore-fuzz))
2605               ? ;Whitespace
2606             (if (< gnus-tmp-score gnus-summary-default-score)
2607                 gnus-score-below-mark gnus-score-over-mark)))
2608          (gnus-tmp-replied
2609           (cond (gnus-tmp-process gnus-process-mark)
2610                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2611                  gnus-cached-mark)
2612                 (gnus-tmp-replied gnus-replied-mark)
2613                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2614                  gnus-saved-mark)
2615                 (t gnus-unread-mark)))
2616          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2617          (gnus-tmp-name
2618           (cond
2619            ((string-match "<[^>]+> *$" gnus-tmp-from)
2620             (let ((beg (match-beginning 0)))
2621               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2622                        (substring gnus-tmp-from (1+ (match-beginning 0))
2623                                   (1- (match-end 0))))
2624                   (substring gnus-tmp-from 0 beg))))
2625            ((string-match "(.+)" gnus-tmp-from)
2626             (substring gnus-tmp-from
2627                        (1+ (match-beginning 0)) (1- (match-end 0))))
2628            (t gnus-tmp-from)))
2629          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2630          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2631          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2632          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2633          (buffer-read-only nil))
2634     (when (string= gnus-tmp-name "")
2635       (setq gnus-tmp-name gnus-tmp-from))
2636     (unless (numberp gnus-tmp-lines)
2637       (setq gnus-tmp-lines 0))
2638     (gnus-put-text-property
2639      (point)
2640      (progn (eval gnus-summary-line-format-spec) (point))
2641      'gnus-number gnus-tmp-number)
2642     (when (gnus-visual-p 'summary-highlight 'highlight)
2643       (forward-line -1)
2644       (gnus-run-hooks 'gnus-summary-update-hook)
2645       (forward-line 1))))
2646
2647 (defun gnus-summary-update-line (&optional dont-update)
2648   ;; Update summary line after change.
2649   (when (and gnus-summary-default-score
2650              (not gnus-summary-inhibit-highlight))
2651     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2652            (article (gnus-summary-article-number))
2653            (score (gnus-summary-article-score article)))
2654       (unless dont-update
2655         (if (and gnus-summary-mark-below
2656                  (< (gnus-summary-article-score)
2657                     gnus-summary-mark-below))
2658             ;; This article has a low score, so we mark it as read.
2659             (when (memq article gnus-newsgroup-unreads)
2660               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2661           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2662             ;; This article was previously marked as read on account
2663             ;; of a low score, but now it has risen, so we mark it as
2664             ;; unread.
2665             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2666         (gnus-summary-update-mark
2667          (if (or (null gnus-summary-default-score)
2668                  (<= (abs (- score gnus-summary-default-score))
2669                      gnus-summary-zcore-fuzz))
2670              ? ;Whitespace
2671            (if (< score gnus-summary-default-score)
2672                gnus-score-below-mark gnus-score-over-mark))
2673          'score))
2674       ;; Do visual highlighting.
2675       (when (gnus-visual-p 'summary-highlight 'highlight)
2676         (gnus-run-hooks 'gnus-summary-update-hook)))))
2677
2678 (defvar gnus-tmp-new-adopts nil)
2679
2680 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2681   "Return the number of articles in THREAD.
2682 This may be 0 in some cases -- if none of the articles in
2683 the thread are to be displayed."
2684   (let* ((number
2685           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2686           (cond
2687            ((not (listp thread))
2688             1)
2689            ((and (consp thread) (cdr thread))
2690             (apply
2691              '+ 1 (mapcar
2692                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2693            ((null thread)
2694             1)
2695            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2696             1)
2697            (t 0))))
2698     (when (and level (zerop level) gnus-tmp-new-adopts)
2699       (incf number
2700             (apply '+ (mapcar
2701                        'gnus-summary-number-of-articles-in-thread
2702                        gnus-tmp-new-adopts))))
2703     (if char
2704         (if (> number 1) gnus-not-empty-thread-mark
2705           gnus-empty-thread-mark)
2706       number)))
2707
2708 (defun gnus-summary-set-local-parameters (group)
2709   "Go through the local params of GROUP and set all variable specs in that list."
2710   (let ((params (gnus-group-find-parameter group))
2711         elem)
2712     (while params
2713       (setq elem (car params)
2714             params (cdr params))
2715       (and (consp elem)                 ; Has to be a cons.
2716            (consp (cdr elem))           ; The cdr has to be a list.
2717            (symbolp (car elem))         ; Has to be a symbol in there.
2718            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2719            (ignore-errors               ; So we set it.
2720              (make-local-variable (car elem))
2721              (set (car elem) (eval (nth 1 elem))))))))
2722
2723 (defun gnus-summary-read-group (group &optional show-all no-article
2724                                       kill-buffer no-display backward
2725                                       select-articles)
2726   "Start reading news in newsgroup GROUP.
2727 If SHOW-ALL is non-nil, already read articles are also listed.
2728 If NO-ARTICLE is non-nil, no article is selected initially.
2729 If NO-DISPLAY, don't generate a summary buffer."
2730   (let (result)
2731     (while (and group
2732                 (null (setq result
2733                             (let ((gnus-auto-select-next nil))
2734                               (or (gnus-summary-read-group-1
2735                                    group show-all no-article
2736                                    kill-buffer no-display
2737                                    select-articles)
2738                                   (setq show-all nil
2739                                         select-articles nil)))))
2740                 (eq gnus-auto-select-next 'quietly))
2741       (set-buffer gnus-group-buffer)
2742       ;; The entry function called above goes to the next
2743       ;; group automatically, so we go two groups back
2744       ;; if we are searching for the previous group.
2745       (when backward
2746         (gnus-group-prev-unread-group 2))
2747       (if (not (equal group (gnus-group-group-name)))
2748           (setq group (gnus-group-group-name))
2749         (setq group nil)))
2750     result))
2751
2752 (defun gnus-summary-read-group-1 (group show-all no-article
2753                                         kill-buffer no-display
2754                                         &optional select-articles)
2755   ;; Killed foreign groups can't be entered.
2756   (when (and (not (gnus-group-native-p group))
2757              (not (gnus-gethash group gnus-newsrc-hashtb)))
2758     (error "Dead non-native groups can't be entered"))
2759   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2760   (let* ((new-group (gnus-summary-setup-buffer group))
2761          (quit-config (gnus-group-quit-config group))
2762          (did-select (and new-group (gnus-select-newsgroup
2763                                      group show-all select-articles))))
2764     (cond
2765      ;; This summary buffer exists already, so we just select it.
2766      ((not new-group)
2767       (gnus-set-global-variables)
2768       (when kill-buffer
2769         (gnus-kill-or-deaden-summary kill-buffer))
2770       (gnus-configure-windows 'summary 'force)
2771       (gnus-set-mode-line 'summary)
2772       (gnus-summary-position-point)
2773       (message "")
2774       t)
2775      ;; We couldn't select this group.
2776      ((null did-select)
2777       (when (and (eq major-mode 'gnus-summary-mode)
2778                  (not (equal (current-buffer) kill-buffer)))
2779         (kill-buffer (current-buffer))
2780         (if (not quit-config)
2781             (progn
2782               ;; Update the info -- marks might need to be removed,
2783               ;; for instance.
2784               (gnus-summary-update-info)
2785               (set-buffer gnus-group-buffer)
2786               (gnus-group-jump-to-group group)
2787               (gnus-group-next-unread-group 1))
2788           (gnus-handle-ephemeral-exit quit-config)))
2789       (gnus-message 3 "Can't select group")
2790       nil)
2791      ;; The user did a `C-g' while prompting for number of articles,
2792      ;; so we exit this group.
2793      ((eq did-select 'quit)
2794       (and (eq major-mode 'gnus-summary-mode)
2795            (not (equal (current-buffer) kill-buffer))
2796            (kill-buffer (current-buffer)))
2797       (when kill-buffer
2798         (gnus-kill-or-deaden-summary kill-buffer))
2799       (if (not quit-config)
2800           (progn
2801             (set-buffer gnus-group-buffer)
2802             (gnus-group-jump-to-group group)
2803             (gnus-group-next-unread-group 1)
2804             (gnus-configure-windows 'group 'force))
2805         (gnus-handle-ephemeral-exit quit-config))
2806       ;; Finally signal the quit.
2807       (signal 'quit nil))
2808      ;; The group was successfully selected.
2809      (t
2810       (gnus-set-global-variables)
2811       ;; Save the active value in effect when the group was entered.
2812       (setq gnus-newsgroup-active
2813             (gnus-copy-sequence
2814              (gnus-active gnus-newsgroup-name)))
2815       ;; You can change the summary buffer in some way with this hook.
2816       (gnus-run-hooks 'gnus-select-group-hook)
2817       ;; Set any local variables in the group parameters.
2818       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2819       (gnus-update-format-specifications
2820        nil 'summary 'summary-mode 'summary-dummy)
2821       (gnus-update-summary-mark-positions)
2822       ;; Do score processing.
2823       (when gnus-use-scoring
2824         (gnus-possibly-score-headers))
2825       ;; Check whether to fill in the gaps in the threads.
2826       (when gnus-build-sparse-threads
2827         (gnus-build-sparse-threads))
2828       ;; Find the initial limit.
2829       (if gnus-show-threads
2830           (if show-all
2831               (let ((gnus-newsgroup-dormant nil))
2832                 (gnus-summary-initial-limit show-all))
2833             (gnus-summary-initial-limit show-all))
2834         ;; When untreaded, all articles are always shown.
2835         (setq gnus-newsgroup-limit
2836               (mapcar
2837                (lambda (header) (mail-header-number header))
2838                gnus-newsgroup-headers)))
2839       ;; Generate the summary buffer.
2840       (unless no-display
2841         (gnus-summary-prepare))
2842       (when gnus-use-trees
2843         (gnus-tree-open group)
2844         (setq gnus-summary-highlight-line-function
2845               'gnus-tree-highlight-article))
2846       ;; If the summary buffer is empty, but there are some low-scored
2847       ;; articles or some excluded dormants, we include these in the
2848       ;; buffer.
2849       (when (and (zerop (buffer-size))
2850                  (not no-display))
2851         (cond (gnus-newsgroup-dormant
2852                (gnus-summary-limit-include-dormant))
2853               ((and gnus-newsgroup-scored show-all)
2854                (gnus-summary-limit-include-expunged t))))
2855       ;; Function `gnus-apply-kill-file' must be called in this hook.
2856       (gnus-run-hooks 'gnus-apply-kill-hook)
2857       (if (and (zerop (buffer-size))
2858                (not no-display))
2859           (progn
2860             ;; This newsgroup is empty.
2861             (gnus-summary-catchup-and-exit nil t)
2862             (gnus-message 6 "No unread news")
2863             (when kill-buffer
2864               (gnus-kill-or-deaden-summary kill-buffer))
2865             ;; Return nil from this function.
2866             nil)
2867         ;; Hide conversation thread subtrees.  We cannot do this in
2868         ;; gnus-summary-prepare-hook since kill processing may not
2869         ;; work with hidden articles.
2870         (and gnus-show-threads
2871              gnus-thread-hide-subtree
2872              (gnus-summary-hide-all-threads))
2873         (when kill-buffer
2874           (gnus-kill-or-deaden-summary kill-buffer))
2875         ;; Show first unread article if requested.
2876         (if (and (not no-article)
2877                  (not no-display)
2878                  gnus-newsgroup-unreads
2879                  gnus-auto-select-first)
2880             (progn
2881               (gnus-configure-windows 'summary)
2882               (cond
2883                ((eq gnus-auto-select-first 'best)
2884                 (gnus-summary-best-unread-article))
2885                ((eq gnus-auto-select-first t)
2886                 (gnus-summary-first-unread-article))
2887                ((gnus-functionp gnus-auto-select-first)
2888                 (funcall gnus-auto-select-first))))
2889           ;; Don't select any articles, just move point to the first
2890           ;; article in the group.
2891           (goto-char (point-min))
2892           (gnus-summary-position-point)
2893           (gnus-configure-windows 'summary 'force)
2894           (gnus-set-mode-line 'summary))
2895         (when (get-buffer-window gnus-group-buffer t)
2896           ;; Gotta use windows, because recenter does weird stuff if
2897           ;; the current buffer ain't the displayed window.
2898           (let ((owin (selected-window)))
2899             (select-window (get-buffer-window gnus-group-buffer t))
2900             (when (gnus-group-goto-group group)
2901               (recenter))
2902             (select-window owin)))
2903         ;; Mark this buffer as "prepared".
2904         (setq gnus-newsgroup-prepared t)
2905         (gnus-run-hooks 'gnus-summary-prepared-hook)
2906         t)))))
2907
2908 (defun gnus-summary-prepare ()
2909   "Generate the summary buffer."
2910   (interactive)
2911   (let ((buffer-read-only nil))
2912     (erase-buffer)
2913     (setq gnus-newsgroup-data nil
2914           gnus-newsgroup-data-reverse nil)
2915     (gnus-run-hooks 'gnus-summary-generate-hook)
2916     ;; Generate the buffer, either with threads or without.
2917     (when gnus-newsgroup-headers
2918       (gnus-summary-prepare-threads
2919        (if gnus-show-threads
2920            (gnus-sort-gathered-threads
2921             (funcall gnus-summary-thread-gathering-function
2922                      (gnus-sort-threads
2923                       (gnus-cut-threads (gnus-make-threads)))))
2924          ;; Unthreaded display.
2925          (gnus-sort-articles gnus-newsgroup-headers))))
2926     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2927     ;; Call hooks for modifying summary buffer.
2928     (goto-char (point-min))
2929     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2930
2931 (defsubst gnus-general-simplify-subject (subject)
2932   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2933   (setq subject
2934         (cond
2935          ;; Truncate the subject.
2936          (gnus-simplify-subject-functions
2937           (gnus-map-function gnus-simplify-subject-functions subject))
2938          ((numberp gnus-summary-gather-subject-limit)
2939           (setq subject (gnus-simplify-subject-re subject))
2940           (if (> (length subject) gnus-summary-gather-subject-limit)
2941               (substring subject 0 gnus-summary-gather-subject-limit)
2942             subject))
2943          ;; Fuzzily simplify it.
2944          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2945           (gnus-simplify-subject-fuzzy subject))
2946          ;; Just remove the leading "Re:".
2947          (t
2948           (gnus-simplify-subject-re subject))))
2949
2950   (if (and gnus-summary-gather-exclude-subject
2951            (string-match gnus-summary-gather-exclude-subject subject))
2952       nil                               ; This article shouldn't be gathered
2953     subject))
2954
2955 (defun gnus-summary-simplify-subject-query ()
2956   "Query where the respool algorithm would put this article."
2957   (interactive)
2958   (gnus-summary-select-article)
2959   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2960
2961 (defun gnus-gather-threads-by-subject (threads)
2962   "Gather threads by looking at Subject headers."
2963   (if (not gnus-summary-make-false-root)
2964       threads
2965     (let ((hashtb (gnus-make-hashtable 1024))
2966           (prev threads)
2967           (result threads)
2968           subject hthread whole-subject)
2969       (while threads
2970         (setq subject (gnus-general-simplify-subject
2971                        (setq whole-subject (mail-header-subject
2972                                             (caar threads)))))
2973         (when subject
2974           (if (setq hthread (gnus-gethash subject hashtb))
2975               (progn
2976                 ;; We enter a dummy root into the thread, if we
2977                 ;; haven't done that already.
2978                 (unless (stringp (caar hthread))
2979                   (setcar hthread (list whole-subject (car hthread))))
2980                 ;; We add this new gathered thread to this gathered
2981                 ;; thread.
2982                 (setcdr (car hthread)
2983                         (nconc (cdar hthread) (list (car threads))))
2984                 ;; Remove it from the list of threads.
2985                 (setcdr prev (cdr threads))
2986                 (setq threads prev))
2987             ;; Enter this thread into the hash table.
2988             (gnus-sethash subject threads hashtb)))
2989         (setq prev threads)
2990         (setq threads (cdr threads)))
2991       result)))
2992
2993 (defun gnus-gather-threads-by-references (threads)
2994   "Gather threads by looking at References headers."
2995   (let ((idhashtb (gnus-make-hashtable 1024))
2996         (thhashtb (gnus-make-hashtable 1024))
2997         (prev threads)
2998         (result threads)
2999         ids references id gthread gid entered ref)
3000     (while threads
3001       (when (setq references (mail-header-references (caar threads)))
3002         (setq id (mail-header-id (caar threads))
3003               ids (gnus-split-references references)
3004               entered nil)
3005         (while (setq ref (pop ids))
3006           (setq ids (delete ref ids))
3007           (if (not (setq gid (gnus-gethash ref idhashtb)))
3008               (progn
3009                 (gnus-sethash ref id idhashtb)
3010                 (gnus-sethash id threads thhashtb))
3011             (setq gthread (gnus-gethash gid thhashtb))
3012             (unless entered
3013               ;; We enter a dummy root into the thread, if we
3014               ;; haven't done that already.
3015               (unless (stringp (caar gthread))
3016                 (setcar gthread (list (mail-header-subject (caar gthread))
3017                                       (car gthread))))
3018               ;; We add this new gathered thread to this gathered
3019               ;; thread.
3020               (setcdr (car gthread)
3021                       (nconc (cdar gthread) (list (car threads)))))
3022             ;; Add it into the thread hash table.
3023             (gnus-sethash id gthread thhashtb)
3024             (setq entered t)
3025             ;; Remove it from the list of threads.
3026             (setcdr prev (cdr threads))
3027             (setq threads prev))))
3028       (setq prev threads)
3029       (setq threads (cdr threads)))
3030     result))
3031
3032 (defun gnus-sort-gathered-threads (threads)
3033   "Sort subtreads inside each gathered thread by article number."
3034   (let ((result threads))
3035     (while threads
3036       (when (stringp (caar threads))
3037         (setcdr (car threads)
3038                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3039       (setq threads (cdr threads)))
3040     result))
3041
3042 (defun gnus-thread-loop-p (root thread)
3043   "Say whether ROOT is in THREAD."
3044   (let ((stack (list thread))
3045         (infloop 0)
3046         th)
3047     (while (setq thread (pop stack))
3048       (setq th (cdr thread))
3049       (while (and th
3050                   (not (eq (caar th) root)))
3051         (pop th))
3052       (if th
3053           ;; We have found a loop.
3054           (let (ref-dep)
3055             (setcdr thread (delq (car th) (cdr thread)))
3056             (if (boundp (setq ref-dep (intern "none"
3057                                               gnus-newsgroup-dependencies)))
3058                 (setcdr (symbol-value ref-dep)
3059                         (nconc (cdr (symbol-value ref-dep))
3060                                (list (car th))))
3061               (set ref-dep (list nil (car th))))
3062             (setq infloop 1
3063                   stack nil))
3064         ;; Push all the subthreads onto the stack.
3065         (push (cdr thread) stack)))
3066     infloop))
3067
3068 (defun gnus-make-threads ()
3069   "Go through the dependency hashtb and find the roots.  Return all threads."
3070   (let (threads)
3071     (while (catch 'infloop
3072              (mapatoms
3073               (lambda (refs)
3074                 ;; Deal with self-referencing References loops.
3075                 (when (and (car (symbol-value refs))
3076                            (not (zerop
3077                                  (apply
3078                                   '+
3079                                   (mapcar
3080                                    (lambda (thread)
3081                                      (gnus-thread-loop-p
3082                                       (car (symbol-value refs)) thread))
3083                                    (cdr (symbol-value refs)))))))
3084                   (setq threads nil)
3085                   (throw 'infloop t))
3086                 (unless (car (symbol-value refs))
3087                   ;; These threads do not refer back to any other articles,
3088                   ;; so they're roots.
3089                   (setq threads (append (cdr (symbol-value refs)) threads))))
3090               gnus-newsgroup-dependencies)))
3091     threads))
3092
3093 ;; Build the thread tree.
3094 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3095   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3096
3097 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3098 if it was already present.
3099
3100 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3101 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3102 Message-IDs will be renamed be renamed to a unique Message-ID before
3103 being entered.
3104
3105 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3106   (let* ((id (mail-header-id header))
3107          (id-dep (and id (intern id dependencies)))
3108          ref ref-dep ref-header)
3109     ;; Enter this `header' in the `dependencies' table.
3110     (cond
3111      ((not id-dep)
3112       (setq header nil))
3113      ;; The first two cases do the normal part: enter a new `header'
3114      ;; in the `dependencies' table.
3115      ((not (boundp id-dep))
3116       (set id-dep (list header)))
3117      ((null (car (symbol-value id-dep)))
3118       (setcar (symbol-value id-dep) header))
3119
3120      ;; From here the `header' was already present in the
3121      ;; `dependencies' table.
3122      (force-new
3123       ;; Overrides an existing entry;
3124       ;; just set the header part of the entry.
3125       (setcar (symbol-value id-dep) header))
3126
3127      ;; Renames the existing `header' to a unique Message-ID.
3128      ((not gnus-summary-ignore-duplicates)
3129       ;; An article with this Message-ID has already been seen.
3130       ;; We rename the Message-ID.
3131       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3132            (list header))
3133       (mail-header-set-id header id))
3134
3135      ;; The last case ignores an existing entry, except it adds any
3136      ;; additional Xrefs (in case the two articles came from different
3137      ;; servers.
3138      ;; Also sets `header' to `nil' meaning that the `dependencies'
3139      ;; table was *not* modified.
3140      (t
3141       (mail-header-set-xref
3142        (car (symbol-value id-dep))
3143        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3144                    "")
3145                (or (mail-header-xref header) "")))
3146       (setq header nil)))
3147
3148     (when header
3149       ;; First check if that we are not creating a References loop.
3150       (setq ref (gnus-parent-id (mail-header-references header)))
3151       (while (and ref
3152                   (setq ref-dep (intern-soft ref dependencies))
3153                   (boundp ref-dep)
3154                   (setq ref-header (car (symbol-value ref-dep))))
3155         (if (string= id ref)
3156             ;; Yuk!  This is a reference loop.  Make the article be a
3157             ;; root article.
3158             (progn
3159               (mail-header-set-references (car (symbol-value id-dep)) "none")
3160               (setq ref nil))
3161           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3162       (setq ref (gnus-parent-id (mail-header-references header)))
3163       (setq ref-dep (intern (or ref "none") dependencies))
3164       (if (boundp ref-dep)
3165           (setcdr (symbol-value ref-dep)
3166                   (nconc (cdr (symbol-value ref-dep))
3167                          (list (symbol-value id-dep))))
3168         (set ref-dep (list nil (symbol-value id-dep)))))
3169     header))
3170
3171 (defun gnus-build-sparse-threads ()
3172   (let ((headers gnus-newsgroup-headers)
3173         (mail-parse-charset gnus-newsgroup-charset)
3174         (gnus-summary-ignore-duplicates t)
3175         header references generation relations
3176         subject child end new-child date)
3177     ;; First we create an alist of generations/relations, where
3178     ;; generations is how much we trust the relation, and the relation
3179     ;; is parent/child.
3180     (gnus-message 7 "Making sparse threads...")
3181     (save-excursion
3182       (nnheader-set-temp-buffer " *gnus sparse threads*")
3183       (while (setq header (pop headers))
3184         (when (and (setq references (mail-header-references header))
3185                    (not (string= references "")))
3186           (insert references)
3187           (setq child (mail-header-id header)
3188                 subject (mail-header-subject header)
3189                 date (mail-header-date header)
3190                 generation 0)
3191           (while (search-backward ">" nil t)
3192             (setq end (1+ (point)))
3193             (when (search-backward "<" nil t)
3194               (setq new-child (buffer-substring (point) end))
3195               (push (list (incf generation)
3196                           child (setq child new-child)
3197                           subject date)
3198                     relations)))
3199           (when child
3200             (push (list (1+ generation) child nil subject) relations))
3201           (erase-buffer)))
3202       (kill-buffer (current-buffer)))
3203     ;; Sort over trustworthiness.
3204     (mapcar
3205      (lambda (relation)
3206        (when (gnus-dependencies-add-header
3207               (make-full-mail-header
3208                gnus-reffed-article-number
3209                (nth 3 relation) "" (or (nth 4 relation) "")
3210                (nth 1 relation)
3211                (or (nth 2 relation) "") 0 0 "")
3212               gnus-newsgroup-dependencies nil)
3213          (push gnus-reffed-article-number gnus-newsgroup-limit)
3214          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3215          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3216                gnus-newsgroup-reads)
3217          (decf gnus-reffed-article-number)))
3218      (sort relations 'car-less-than-car))
3219     (gnus-message 7 "Making sparse threads...done")))
3220
3221 (defun gnus-build-old-threads ()
3222   ;; Look at all the articles that refer back to old articles, and
3223   ;; fetch the headers for the articles that aren't there.  This will
3224   ;; build complete threads - if the roots haven't been expired by the
3225   ;; server, that is.
3226   (let ((mail-parse-charset gnus-newsgroup-charset)
3227         id heads)
3228     (mapatoms
3229      (lambda (refs)
3230        (when (not (car (symbol-value refs)))
3231          (setq heads (cdr (symbol-value refs)))
3232          (while heads
3233            (if (memq (mail-header-number (caar heads))
3234                      gnus-newsgroup-dormant)
3235                (setq heads (cdr heads))
3236              (setq id (symbol-name refs))
3237              (while (and (setq id (gnus-build-get-header id))
3238                          (not (car (gnus-id-to-thread id)))))
3239              (setq heads nil)))))
3240      gnus-newsgroup-dependencies)))
3241
3242 ;; This function has to be called with point after the article number
3243 ;; on the beginning of the line.
3244 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3245   (let ((eol (gnus-point-at-eol))
3246         (buffer (current-buffer))
3247         header)
3248
3249     ;; overview: [num subject from date id refs chars lines misc]
3250     (unwind-protect
3251         (progn
3252           (narrow-to-region (point) eol)
3253           (unless (eobp)
3254             (forward-char))
3255
3256           (setq header
3257                 (make-full-mail-header
3258                  number                         ; number
3259                  (funcall gnus-decode-encoded-word-function
3260                           (nnheader-nov-field)) ; subject
3261                  (funcall gnus-decode-encoded-word-function
3262                           (nnheader-nov-field)) ; from
3263                  (nnheader-nov-field)           ; date
3264                  (nnheader-nov-read-message-id) ; id
3265                  (nnheader-nov-field)           ; refs
3266                  (nnheader-nov-read-integer)    ; chars
3267                  (nnheader-nov-read-integer)    ; lines
3268                  (unless (eobp)
3269                    (nnheader-nov-field))                ; misc
3270                  (nnheader-nov-parse-extra))))  ; extra
3271
3272       (widen))
3273
3274     (when gnus-alter-header-function
3275       (funcall gnus-alter-header-function header))
3276     (gnus-dependencies-add-header header dependencies force-new)))
3277
3278 (defun gnus-build-get-header (id)
3279   ;; Look through the buffer of NOV lines and find the header to
3280   ;; ID.  Enter this line into the dependencies hash table, and return
3281   ;; the id of the parent article (if any).
3282   (let ((deps gnus-newsgroup-dependencies)
3283         found header)
3284     (prog1
3285         (save-excursion
3286           (set-buffer nntp-server-buffer)
3287           (let ((case-fold-search nil))
3288             (goto-char (point-min))
3289             (while (and (not found)
3290                         (search-forward id nil t))
3291               (beginning-of-line)
3292               (setq found (looking-at
3293                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3294                                    (regexp-quote id))))
3295               (or found (beginning-of-line 2)))
3296             (when found
3297               (beginning-of-line)
3298               (and
3299                (setq header (gnus-nov-parse-line
3300                              (read (current-buffer)) deps))
3301                (gnus-parent-id (mail-header-references header))))))
3302       (when header
3303         (let ((number (mail-header-number header)))
3304           (push number gnus-newsgroup-limit)
3305           (push header gnus-newsgroup-headers)
3306           (if (memq number gnus-newsgroup-unselected)
3307               (progn
3308                 (push number gnus-newsgroup-unreads)
3309                 (setq gnus-newsgroup-unselected
3310                       (delq number gnus-newsgroup-unselected)))
3311             (push number gnus-newsgroup-ancient)))))))
3312
3313 (defun gnus-build-all-threads ()
3314   "Read all the headers."
3315   (let ((gnus-summary-ignore-duplicates t)
3316         (mail-parse-charset gnus-newsgroup-charset)
3317         (dependencies gnus-newsgroup-dependencies)
3318         header article)
3319     (save-excursion
3320       (set-buffer nntp-server-buffer)
3321       (let ((case-fold-search nil))
3322         (goto-char (point-min))
3323         (while (not (eobp))
3324           (ignore-errors
3325             (setq article (read (current-buffer))
3326                   header (gnus-nov-parse-line article dependencies)))
3327           (when header
3328             (save-excursion
3329               (set-buffer gnus-summary-buffer)
3330               (push header gnus-newsgroup-headers)
3331               (if (memq (setq article (mail-header-number header))
3332                         gnus-newsgroup-unselected)
3333                   (progn
3334                     (push article gnus-newsgroup-unreads)
3335                     (setq gnus-newsgroup-unselected
3336                           (delq article gnus-newsgroup-unselected)))
3337                 (push article gnus-newsgroup-ancient)))
3338             (forward-line 1)))))))
3339
3340 (defun gnus-summary-update-article-line (article header)
3341   "Update the line for ARTICLE using HEADERS."
3342   (let* ((id (mail-header-id header))
3343          (thread (gnus-id-to-thread id)))
3344     (unless thread
3345       (error "Article in no thread"))
3346     ;; Update the thread.
3347     (setcar thread header)
3348     (gnus-summary-goto-subject article)
3349     (let* ((datal (gnus-data-find-list article))
3350            (data (car datal))
3351            (length (when (cdr datal)
3352                      (- (gnus-data-pos data)
3353                         (gnus-data-pos (cadr datal)))))
3354            (buffer-read-only nil)
3355            (level (gnus-summary-thread-level)))
3356       (gnus-delete-line)
3357       (gnus-summary-insert-line
3358        header level nil (gnus-article-mark article)
3359        (memq article gnus-newsgroup-replied)
3360        (memq article gnus-newsgroup-expirable)
3361        ;; Only insert the Subject string when it's different
3362        ;; from the previous Subject string.
3363        (if (gnus-subject-equal
3364             (condition-case ()
3365                 (mail-header-subject
3366                  (gnus-data-header
3367                   (cadr
3368                    (gnus-data-find-list
3369                     article
3370                     (gnus-data-list t)))))
3371               ;; Error on the side of excessive subjects.
3372               (error ""))
3373             (mail-header-subject header))
3374            ""
3375          (mail-header-subject header))
3376        nil (cdr (assq article gnus-newsgroup-scored))
3377        (memq article gnus-newsgroup-processable))
3378       (when length
3379         (gnus-data-update-list
3380          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3381
3382 (defun gnus-summary-update-article (article &optional iheader)
3383   "Update ARTICLE in the summary buffer."
3384   (set-buffer gnus-summary-buffer)
3385   (let* ((header (gnus-summary-article-header article))
3386          (id (mail-header-id header))
3387          (data (gnus-data-find article))
3388          (thread (gnus-id-to-thread id))
3389          (references (mail-header-references header))
3390          (parent
3391           (gnus-id-to-thread
3392            (or (gnus-parent-id
3393                 (when (and references
3394                            (not (equal "" references)))
3395                   references))
3396                "none")))
3397          (buffer-read-only nil)
3398          (old (car thread)))
3399     (when thread
3400       (unless iheader
3401         (setcar thread nil)
3402         (when parent
3403           (delq thread parent)))
3404       (if (gnus-summary-insert-subject id header)
3405           ;; Set the (possibly) new article number in the data structure.
3406           (gnus-data-set-number data (gnus-id-to-article id))
3407         (setcar thread old)
3408         nil))))
3409
3410 (defun gnus-rebuild-thread (id &optional line)
3411   "Rebuild the thread containing ID.
3412 If LINE, insert the rebuilt thread starting on line LINE."
3413   (let ((buffer-read-only nil)
3414         old-pos current thread data)
3415     (if (not gnus-show-threads)
3416         (setq thread (list (car (gnus-id-to-thread id))))
3417       ;; Get the thread this article is part of.
3418       (setq thread (gnus-remove-thread id)))
3419     (setq old-pos (gnus-point-at-bol))
3420     (setq current (save-excursion
3421                     (and (zerop (forward-line -1))
3422                          (gnus-summary-article-number))))
3423     ;; If this is a gathered thread, we have to go some re-gathering.
3424     (when (stringp (car thread))
3425       (let ((subject (car thread))
3426             roots thr)
3427         (setq thread (cdr thread))
3428         (while thread
3429           (unless (memq (setq thr (gnus-id-to-thread
3430                                    (gnus-root-id
3431                                     (mail-header-id (caar thread)))))
3432                         roots)
3433             (push thr roots))
3434           (setq thread (cdr thread)))
3435         ;; We now have all (unique) roots.
3436         (if (= (length roots) 1)
3437             ;; All the loose roots are now one solid root.
3438             (setq thread (car roots))
3439           (setq thread (cons subject (gnus-sort-threads roots))))))
3440     (let (threads)
3441       ;; We then insert this thread into the summary buffer.
3442       (when line
3443         (goto-char (point-min))
3444         (forward-line (1- line)))
3445       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3446         (if gnus-show-threads
3447             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3448           (gnus-summary-prepare-unthreaded thread))
3449         (setq data (nreverse gnus-newsgroup-data))
3450         (setq threads gnus-newsgroup-threads))
3451       ;; We splice the new data into the data structure.
3452       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3453       ;;!!! then we want to insert at the beginning of the buffer.
3454       ;;!!! That happens to be true with Gnus now, but that may
3455       ;;!!! change in the future.  Perhaps.
3456       (gnus-data-enter-list
3457        (if line nil current) data (- (point) old-pos))
3458       (setq gnus-newsgroup-threads
3459             (nconc threads gnus-newsgroup-threads))
3460       (gnus-data-compute-positions))))
3461
3462 (defun gnus-number-to-header (number)
3463   "Return the header for article NUMBER."
3464   (let ((headers gnus-newsgroup-headers))
3465     (while (and headers
3466                 (not (= number (mail-header-number (car headers)))))
3467       (pop headers))
3468     (when headers
3469       (car headers))))
3470
3471 (defun gnus-parent-headers (in-headers &optional generation)
3472   "Return the headers of the GENERATIONeth parent of HEADERS."
3473   (unless generation
3474     (setq generation 1))
3475   (let ((parent t)
3476         (headers in-headers)
3477         references)
3478     (while (and parent
3479                 (not (zerop generation))
3480                 (setq references (mail-header-references headers)))
3481       (setq headers (if (and references
3482                              (setq parent (gnus-parent-id references)))
3483                         (car (gnus-id-to-thread parent))
3484                       nil))
3485       (decf generation))
3486     (and (not (eq headers in-headers))
3487          headers)))
3488
3489 (defun gnus-id-to-thread (id)
3490   "Return the (sub-)thread where ID appears."
3491   (gnus-gethash id gnus-newsgroup-dependencies))
3492
3493 (defun gnus-id-to-article (id)
3494   "Return the article number of ID."
3495   (let ((thread (gnus-id-to-thread id)))
3496     (when (and thread
3497                (car thread))
3498       (mail-header-number (car thread)))))
3499
3500 (defun gnus-id-to-header (id)
3501   "Return the article headers of ID."
3502   (car (gnus-id-to-thread id)))
3503
3504 (defun gnus-article-displayed-root-p (article)
3505   "Say whether ARTICLE is a root(ish) article."
3506   (let ((level (gnus-summary-thread-level article))
3507         (refs (mail-header-references  (gnus-summary-article-header article)))
3508         particle)
3509     (cond
3510      ((null level) nil)
3511      ((zerop level) t)
3512      ((null refs) t)
3513      ((null (gnus-parent-id refs)) t)
3514      ((and (= 1 level)
3515            (null (setq particle (gnus-id-to-article
3516                                  (gnus-parent-id refs))))
3517            (null (gnus-summary-thread-level particle)))))))
3518
3519 (defun gnus-root-id (id)
3520   "Return the id of the root of the thread where ID appears."
3521   (let (last-id prev)
3522     (while (and id (setq prev (car (gnus-id-to-thread id))))
3523       (setq last-id id
3524             id (gnus-parent-id (mail-header-references prev))))
3525     last-id))
3526
3527 (defun gnus-articles-in-thread (thread)
3528   "Return the list of articles in THREAD."
3529   (cons (mail-header-number (car thread))
3530         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3531
3532 (defun gnus-remove-thread (id &optional dont-remove)
3533   "Remove the thread that has ID in it."
3534   (let (headers thread last-id)
3535     ;; First go up in this thread until we find the root.
3536     (setq last-id (gnus-root-id id)
3537           headers (message-flatten-list (gnus-id-to-thread last-id)))
3538     ;; We have now found the real root of this thread.  It might have
3539     ;; been gathered into some loose thread, so we have to search
3540     ;; through the threads to find the thread we wanted.
3541     (let ((threads gnus-newsgroup-threads)
3542           sub)
3543       (while threads
3544         (setq sub (car threads))
3545         (if (stringp (car sub))
3546             ;; This is a gathered thread, so we look at the roots
3547             ;; below it to find whether this article is in this
3548             ;; gathered root.
3549             (progn
3550               (setq sub (cdr sub))
3551               (while sub
3552                 (when (member (caar sub) headers)
3553                   (setq thread (car threads)
3554                         threads nil
3555                         sub nil))
3556                 (setq sub (cdr sub))))
3557           ;; It's an ordinary thread, so we check it.
3558           (when (eq (car sub) (car headers))
3559             (setq thread sub
3560                   threads nil)))
3561         (setq threads (cdr threads)))
3562       ;; If this article is in no thread, then it's a root.
3563       (if thread
3564           (unless dont-remove
3565             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3566         (setq thread (gnus-id-to-thread last-id)))
3567       (when thread
3568         (prog1
3569             thread                      ; We return this thread.
3570           (unless dont-remove
3571             (if (stringp (car thread))
3572                 (progn
3573                   ;; If we use dummy roots, then we have to remove the
3574                   ;; dummy root as well.
3575                   (when (eq gnus-summary-make-false-root 'dummy)
3576                     ;; We go to the dummy root by going to
3577                     ;; the first sub-"thread", and then one line up.
3578                     (gnus-summary-goto-article
3579                      (mail-header-number (caadr thread)))
3580                     (forward-line -1)
3581                     (gnus-delete-line)
3582                     (gnus-data-compute-positions))
3583                   (setq thread (cdr thread))
3584                   (while thread
3585                     (gnus-remove-thread-1 (car thread))
3586                     (setq thread (cdr thread))))
3587               (gnus-summary-show-all-threads)
3588               (gnus-remove-thread-1 thread))))))))
3589
3590 (defun gnus-remove-thread-1 (thread)
3591   "Remove the thread THREAD recursively."
3592   (let ((number (mail-header-number (pop thread)))
3593         d)
3594     (setq thread (reverse thread))
3595     (while thread
3596       (gnus-remove-thread-1 (pop thread)))
3597     (when (setq d (gnus-data-find number))
3598       (goto-char (gnus-data-pos d))
3599       (gnus-data-remove
3600        number
3601        (- (gnus-point-at-bol)
3602           (prog1
3603               (1+ (gnus-point-at-eol))
3604             (gnus-delete-line)))))))
3605
3606 (defun gnus-sort-threads (threads)
3607   "Sort THREADS."
3608   (if (not gnus-thread-sort-functions)
3609       threads
3610     (gnus-message 8 "Sorting threads...")
3611     (prog1
3612         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3613       (gnus-message 8 "Sorting threads...done"))))
3614
3615 (defun gnus-sort-articles (articles)
3616   "Sort ARTICLES."
3617   (when gnus-article-sort-functions
3618     (gnus-message 7 "Sorting articles...")
3619     (prog1
3620         (setq gnus-newsgroup-headers
3621               (sort articles (gnus-make-sort-function
3622                               gnus-article-sort-functions)))
3623       (gnus-message 7 "Sorting articles...done"))))
3624
3625 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3626 (defmacro gnus-thread-header (thread)
3627   ;; Return header of first article in THREAD.
3628   ;; Note that THREAD must never, ever be anything else than a variable -
3629   ;; using some other form will lead to serious barfage.
3630   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3631   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3632   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3633         (vector thread) 2))
3634
3635 (defsubst gnus-article-sort-by-number (h1 h2)
3636   "Sort articles by article number."
3637   (< (mail-header-number h1)
3638      (mail-header-number h2)))
3639
3640 (defun gnus-thread-sort-by-number (h1 h2)
3641   "Sort threads by root article number."
3642   (gnus-article-sort-by-number
3643    (gnus-thread-header h1) (gnus-thread-header h2)))
3644
3645 (defsubst gnus-article-sort-by-lines (h1 h2)
3646   "Sort articles by article Lines header."
3647   (< (mail-header-lines h1)
3648      (mail-header-lines h2)))
3649
3650 (defun gnus-thread-sort-by-lines (h1 h2)
3651   "Sort threads by root article Lines header."
3652   (gnus-article-sort-by-lines
3653    (gnus-thread-header h1) (gnus-thread-header h2)))
3654
3655 (defsubst gnus-article-sort-by-chars (h1 h2)
3656   "Sort articles by octet length."
3657   (< (mail-header-chars h1)
3658      (mail-header-chars h2)))
3659
3660 (defun gnus-thread-sort-by-chars (h1 h2)
3661   "Sort threads by root article octet length."
3662   (gnus-article-sort-by-chars
3663    (gnus-thread-header h1) (gnus-thread-header h2)))
3664
3665 (defsubst gnus-article-sort-by-author (h1 h2)
3666   "Sort articles by root author."
3667   (string-lessp
3668    (let ((extract (funcall
3669                    gnus-extract-address-components
3670                    (mail-header-from h1))))
3671      (or (car extract) (cadr extract) ""))
3672    (let ((extract (funcall
3673                    gnus-extract-address-components
3674                    (mail-header-from h2))))
3675      (or (car extract) (cadr extract) ""))))
3676
3677 (defun gnus-thread-sort-by-author (h1 h2)
3678   "Sort threads by root author."
3679   (gnus-article-sort-by-author
3680    (gnus-thread-header h1)  (gnus-thread-header h2)))
3681
3682 (defsubst gnus-article-sort-by-subject (h1 h2)
3683   "Sort articles by root subject."
3684   (string-lessp
3685    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3686    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3687
3688 (defun gnus-thread-sort-by-subject (h1 h2)
3689   "Sort threads by root subject."
3690   (gnus-article-sort-by-subject
3691    (gnus-thread-header h1) (gnus-thread-header h2)))
3692
3693 (defsubst gnus-article-sort-by-date (h1 h2)
3694   "Sort articles by root article date."
3695   (time-less-p
3696    (gnus-date-get-time (mail-header-date h1))
3697    (gnus-date-get-time (mail-header-date h2))))
3698
3699 (defun gnus-thread-sort-by-date (h1 h2)
3700   "Sort threads by root article date."
3701   (gnus-article-sort-by-date
3702    (gnus-thread-header h1) (gnus-thread-header h2)))
3703
3704 (defsubst gnus-article-sort-by-score (h1 h2)
3705   "Sort articles by root article score.
3706 Unscored articles will be counted as having a score of zero."
3707   (> (or (cdr (assq (mail-header-number h1)
3708                     gnus-newsgroup-scored))
3709          gnus-summary-default-score 0)
3710      (or (cdr (assq (mail-header-number h2)
3711                     gnus-newsgroup-scored))
3712          gnus-summary-default-score 0)))
3713
3714 (defun gnus-thread-sort-by-score (h1 h2)
3715   "Sort threads by root article score."
3716   (gnus-article-sort-by-score
3717    (gnus-thread-header h1) (gnus-thread-header h2)))
3718
3719 (defun gnus-thread-sort-by-total-score (h1 h2)
3720   "Sort threads by the sum of all scores in the thread.
3721 Unscored articles will be counted as having a score of zero."
3722   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3723
3724 (defun gnus-thread-total-score (thread)
3725   ;; This function find the total score of THREAD.
3726   (cond ((null thread)
3727          0)
3728         ((consp thread)
3729          (if (stringp (car thread))
3730              (apply gnus-thread-score-function 0
3731                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3732            (gnus-thread-total-score-1 thread)))
3733         (t
3734          (gnus-thread-total-score-1 (list thread)))))
3735
3736 (defun gnus-thread-total-score-1 (root)
3737   ;; This function find the total score of the thread below ROOT.
3738   (setq root (car root))
3739   (apply gnus-thread-score-function
3740          (or (append
3741               (mapcar 'gnus-thread-total-score
3742                       (cdr (gnus-id-to-thread (mail-header-id root))))
3743               (when (> (mail-header-number root) 0)
3744                 (list (or (cdr (assq (mail-header-number root)
3745                                      gnus-newsgroup-scored))
3746                           gnus-summary-default-score 0))))
3747              (list gnus-summary-default-score)
3748              '(0))))
3749
3750 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3751 (defvar gnus-tmp-prev-subject nil)
3752 (defvar gnus-tmp-false-parent nil)
3753 (defvar gnus-tmp-root-expunged nil)
3754 (defvar gnus-tmp-dummy-line nil)
3755
3756 (defvar gnus-tmp-header)
3757 (defun gnus-extra-header (type &optional header)
3758   "Return the extra header of TYPE."
3759   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3760       ""))
3761
3762 (defun gnus-summary-prepare-threads (threads)
3763   "Prepare summary buffer from THREADS and indentation LEVEL.
3764 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3765 or a straight list of headers."
3766   (gnus-message 7 "Generating summary...")
3767
3768   (setq gnus-newsgroup-threads threads)
3769   (beginning-of-line)
3770
3771   (let ((gnus-tmp-level 0)
3772         (default-score (or gnus-summary-default-score 0))
3773         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3774         thread number subject stack state gnus-tmp-gathered beg-match
3775         new-roots gnus-tmp-new-adopts thread-end
3776         gnus-tmp-header gnus-tmp-unread
3777         gnus-tmp-replied gnus-tmp-subject-or-nil
3778         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3779         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3780         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3781
3782     (setq gnus-tmp-prev-subject nil)
3783
3784     (if (vectorp (car threads))
3785         ;; If this is a straight (sic) list of headers, then a
3786         ;; threaded summary display isn't required, so we just create
3787         ;; an unthreaded one.
3788         (gnus-summary-prepare-unthreaded threads)
3789
3790       ;; Do the threaded display.
3791
3792       (while (or threads stack gnus-tmp-new-adopts new-roots)
3793
3794         (if (and (= gnus-tmp-level 0)
3795                  (or (not stack)
3796                      (= (caar stack) 0))
3797                  (not gnus-tmp-false-parent)
3798                  (or gnus-tmp-new-adopts new-roots))
3799             (if gnus-tmp-new-adopts
3800                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3801                       thread (list (car gnus-tmp-new-adopts))
3802                       gnus-tmp-header (caar thread)
3803                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3804               (when new-roots
3805                 (setq thread (list (car new-roots))
3806                       gnus-tmp-header (caar thread)
3807                       new-roots (cdr new-roots))))
3808
3809           (if threads
3810               ;; If there are some threads, we do them before the
3811               ;; threads on the stack.
3812               (setq thread threads
3813                     gnus-tmp-header (caar thread))
3814             ;; There were no current threads, so we pop something off
3815             ;; the stack.
3816             (setq state (car stack)
3817                   gnus-tmp-level (car state)
3818                   thread (cdr state)
3819                   stack (cdr stack)
3820                   gnus-tmp-header (caar thread))))
3821
3822         (setq gnus-tmp-false-parent nil)
3823         (setq gnus-tmp-root-expunged nil)
3824         (setq thread-end nil)
3825
3826         (if (stringp gnus-tmp-header)
3827             ;; The header is a dummy root.
3828             (cond
3829              ((eq gnus-summary-make-false-root 'adopt)
3830               ;; We let the first article adopt the rest.
3831               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3832                                                (cddar thread)))
3833               (setq gnus-tmp-gathered
3834                     (nconc (mapcar
3835                             (lambda (h) (mail-header-number (car h)))
3836                             (cddar thread))
3837                            gnus-tmp-gathered))
3838               (setq thread (cons (list (caar thread)
3839                                        (cadar thread))
3840                                  (cdr thread)))
3841               (setq gnus-tmp-level -1
3842                     gnus-tmp-false-parent t))
3843              ((eq gnus-summary-make-false-root 'empty)
3844               ;; We print adopted articles with empty subject fields.
3845               (setq gnus-tmp-gathered
3846                     (nconc (mapcar
3847                             (lambda (h) (mail-header-number (car h)))
3848                             (cddar thread))
3849                            gnus-tmp-gathered))
3850               (setq gnus-tmp-level -1))
3851              ((eq gnus-summary-make-false-root 'dummy)
3852               ;; We remember that we probably want to output a dummy
3853               ;; root.
3854               (setq gnus-tmp-dummy-line gnus-tmp-header)
3855               (setq gnus-tmp-prev-subject gnus-tmp-header))
3856              (t
3857               ;; We do not make a root for the gathered
3858               ;; sub-threads at all.
3859               (setq gnus-tmp-level -1)))
3860
3861           (setq number (mail-header-number gnus-tmp-header)
3862                 subject (mail-header-subject gnus-tmp-header))
3863
3864           (cond
3865            ;; If the thread has changed subject, we might want to make
3866            ;; this subthread into a root.
3867            ((and (null gnus-thread-ignore-subject)
3868                  (not (zerop gnus-tmp-level))
3869                  gnus-tmp-prev-subject
3870                  (not (inline
3871                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3872             (setq new-roots (nconc new-roots (list (car thread)))
3873                   thread-end t
3874                   gnus-tmp-header nil))
3875            ;; If the article lies outside the current limit,
3876            ;; then we do not display it.
3877            ((not (memq number gnus-newsgroup-limit))
3878             (setq gnus-tmp-gathered
3879                   (nconc (mapcar
3880                           (lambda (h) (mail-header-number (car h)))
3881                           (cdar thread))
3882                          gnus-tmp-gathered))
3883             (setq gnus-tmp-new-adopts (if (cdar thread)
3884                                           (append gnus-tmp-new-adopts
3885                                                   (cdar thread))
3886                                         gnus-tmp-new-adopts)
3887                   thread-end t
3888                   gnus-tmp-header nil)
3889             (when (zerop gnus-tmp-level)
3890               (setq gnus-tmp-root-expunged t)))
3891            ;; Perhaps this article is to be marked as read?
3892            ((and gnus-summary-mark-below
3893                  (< (or (cdr (assq number gnus-newsgroup-scored))
3894                         default-score)
3895                     gnus-summary-mark-below)
3896                  ;; Don't touch sparse articles.
3897                  (not (gnus-summary-article-sparse-p number))
3898                  (not (gnus-summary-article-ancient-p number)))
3899             (setq gnus-newsgroup-unreads
3900                   (delq number gnus-newsgroup-unreads))
3901             (if gnus-newsgroup-auto-expire
3902                 (push number gnus-newsgroup-expirable)
3903               (push (cons number gnus-low-score-mark)
3904                     gnus-newsgroup-reads))))
3905
3906           (when gnus-tmp-header
3907             ;; We may have an old dummy line to output before this
3908             ;; article.
3909             (when (and gnus-tmp-dummy-line
3910                        (gnus-subject-equal
3911                         gnus-tmp-dummy-line
3912                         (mail-header-subject gnus-tmp-header)))
3913               (gnus-summary-insert-dummy-line
3914                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3915               (setq gnus-tmp-dummy-line nil))
3916
3917             ;; Compute the mark.
3918             (setq gnus-tmp-unread (gnus-article-mark number))
3919
3920             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3921                                   gnus-tmp-header gnus-tmp-level)
3922                   gnus-newsgroup-data)
3923
3924             ;; Actually insert the line.
3925             (setq
3926              gnus-tmp-subject-or-nil
3927              (cond
3928               ((and gnus-thread-ignore-subject
3929                     gnus-tmp-prev-subject
3930                     (not (inline (gnus-subject-equal
3931                                   gnus-tmp-prev-subject subject))))
3932                subject)
3933               ((zerop gnus-tmp-level)
3934                (if (and (eq gnus-summary-make-false-root 'empty)
3935                         (memq number gnus-tmp-gathered)
3936                         gnus-tmp-prev-subject
3937                         (inline (gnus-subject-equal
3938                                  gnus-tmp-prev-subject subject)))
3939                    gnus-summary-same-subject
3940                  subject))
3941               (t gnus-summary-same-subject)))
3942             (if (and (eq gnus-summary-make-false-root 'adopt)
3943                      (= gnus-tmp-level 1)
3944                      (memq number gnus-tmp-gathered))
3945                 (setq gnus-tmp-opening-bracket ?\<
3946                       gnus-tmp-closing-bracket ?\>)
3947               (setq gnus-tmp-opening-bracket ?\[
3948                     gnus-tmp-closing-bracket ?\]))
3949             (setq
3950              gnus-tmp-indentation
3951              (aref gnus-thread-indent-array gnus-tmp-level)
3952              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3953              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3954                                 gnus-summary-default-score 0)
3955              gnus-tmp-score-char
3956              (if (or (null gnus-summary-default-score)
3957                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3958                          gnus-summary-zcore-fuzz))
3959                  ? ;Whitespace
3960                (if (< gnus-tmp-score gnus-summary-default-score)
3961                    gnus-score-below-mark gnus-score-over-mark))
3962              gnus-tmp-replied
3963              (cond ((memq number gnus-newsgroup-processable)
3964                     gnus-process-mark)
3965                    ((memq number gnus-newsgroup-cached)
3966                     gnus-cached-mark)
3967                    ((memq number gnus-newsgroup-replied)
3968                     gnus-replied-mark)
3969                    ((memq number gnus-newsgroup-saved)
3970                     gnus-saved-mark)
3971                    (t gnus-unread-mark))
3972              gnus-tmp-from (mail-header-from gnus-tmp-header)
3973              gnus-tmp-name
3974              (cond
3975               ((string-match "<[^>]+> *$" gnus-tmp-from)
3976                (setq beg-match (match-beginning 0))
3977                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3978                         (substring gnus-tmp-from (1+ (match-beginning 0))
3979                                    (1- (match-end 0))))
3980                    (substring gnus-tmp-from 0 beg-match)))
3981               ((string-match "(.+)" gnus-tmp-from)
3982                (substring gnus-tmp-from
3983                           (1+ (match-beginning 0)) (1- (match-end 0))))
3984               (t gnus-tmp-from)))
3985             (when (string= gnus-tmp-name "")
3986               (setq gnus-tmp-name gnus-tmp-from))
3987             (unless (numberp gnus-tmp-lines)
3988               (setq gnus-tmp-lines 0))
3989             (gnus-put-text-property
3990              (point)
3991              (progn (eval gnus-summary-line-format-spec) (point))
3992              'gnus-number number)
3993             (when gnus-visual-p
3994               (forward-line -1)
3995               (gnus-run-hooks 'gnus-summary-update-hook)
3996               (forward-line 1))
3997
3998             (setq gnus-tmp-prev-subject subject)))
3999
4000         (when (nth 1 thread)
4001           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4002         (incf gnus-tmp-level)
4003         (setq threads (if thread-end nil (cdar thread)))
4004         (unless threads
4005           (setq gnus-tmp-level 0)))))
4006   (gnus-message 7 "Generating summary...done"))
4007
4008 (defun gnus-summary-prepare-unthreaded (headers)
4009   "Generate an unthreaded summary buffer based on HEADERS."
4010   (let (header number mark)
4011
4012     (beginning-of-line)
4013
4014     (while headers
4015       ;; We may have to root out some bad articles...
4016       (when (memq (setq number (mail-header-number
4017                                 (setq header (pop headers))))
4018                   gnus-newsgroup-limit)
4019         ;; Mark article as read when it has a low score.
4020         (when (and gnus-summary-mark-below
4021                    (< (or (cdr (assq number gnus-newsgroup-scored))
4022                           gnus-summary-default-score 0)
4023                       gnus-summary-mark-below)
4024                    (not (gnus-summary-article-ancient-p number)))
4025           (setq gnus-newsgroup-unreads
4026                 (delq number gnus-newsgroup-unreads))
4027           (if gnus-newsgroup-auto-expire
4028               (push number gnus-newsgroup-expirable)
4029             (push (cons number gnus-low-score-mark)
4030                   gnus-newsgroup-reads)))
4031
4032         (setq mark (gnus-article-mark number))
4033         (push (gnus-data-make number mark (1+ (point)) header 0)
4034               gnus-newsgroup-data)
4035         (gnus-summary-insert-line
4036          header 0 number
4037          mark (memq number gnus-newsgroup-replied)
4038          (memq number gnus-newsgroup-expirable)
4039          (mail-header-subject header) nil
4040          (cdr (assq number gnus-newsgroup-scored))
4041          (memq number gnus-newsgroup-processable))))))
4042
4043 (defun gnus-summary-remove-list-identifiers ()
4044   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4045   (let ((regexp (if (stringp gnus-list-identifiers)
4046                     gnus-list-identifiers
4047                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4048     (dolist (header gnus-newsgroup-headers)
4049       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4050                           (mail-header-subject header))
4051         (mail-header-set-subject
4052          header (concat (substring (mail-header-subject header)
4053                                    0 (match-beginning 2))
4054                         (substring (mail-header-subject header)
4055                                    (match-end 2))))))))
4056
4057 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4058   "Select newsgroup GROUP.
4059 If READ-ALL is non-nil, all articles in the group are selected.
4060 If SELECT-ARTICLES, only select those articles from GROUP."
4061   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4062          ;;!!! Dirty hack; should be removed.
4063          (gnus-summary-ignore-duplicates
4064           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4065               t
4066             gnus-summary-ignore-duplicates))
4067          (info (nth 2 entry))
4068          articles fetched-articles cached)
4069
4070     (unless (gnus-check-server
4071              (setq gnus-current-select-method
4072                    (gnus-find-method-for-group group)))
4073       (error "Couldn't open server"))
4074
4075     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4076         (gnus-activate-group group)     ; Or we can activate it...
4077         (progn                          ; Or we bug out.
4078           (when (equal major-mode 'gnus-summary-mode)
4079             (kill-buffer (current-buffer)))
4080           (error "Couldn't request group %s: %s"
4081                  group (gnus-status-message group))))
4082
4083     (unless (gnus-request-group group t)
4084       (when (equal major-mode 'gnus-summary-mode)
4085         (kill-buffer (current-buffer)))
4086       (error "Couldn't request group %s: %s"
4087              group (gnus-status-message group)))
4088
4089     (setq gnus-newsgroup-name group)
4090     (setq gnus-newsgroup-unselected nil)
4091     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4092     (gnus-summary-setup-default-charset)
4093
4094     ;; Adjust and set lists of article marks.
4095     (when info
4096       (gnus-adjust-marked-articles info))
4097
4098     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4099     (when (gnus-virtual-group-p group)
4100       (setq cached gnus-newsgroup-cached))
4101
4102     (setq gnus-newsgroup-unreads
4103           (gnus-set-difference
4104            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4105            gnus-newsgroup-dormant))
4106
4107     (setq gnus-newsgroup-processable nil)
4108
4109     (gnus-update-read-articles group gnus-newsgroup-unreads)
4110
4111     (if (setq articles select-articles)
4112         (setq gnus-newsgroup-unselected
4113               (gnus-sorted-intersection
4114                gnus-newsgroup-unreads
4115                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4116       (setq articles (gnus-articles-to-read group read-all)))
4117
4118     (cond
4119      ((null articles)
4120       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4121       'quit)
4122      ((eq articles 0) nil)
4123      (t
4124       ;; Init the dependencies hash table.
4125       (setq gnus-newsgroup-dependencies
4126             (gnus-make-hashtable (length articles)))
4127       (gnus-set-global-variables)
4128       ;; Retrieve the headers and read them in.
4129       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4130       (setq gnus-newsgroup-headers
4131             (if (eq 'nov
4132                     (setq gnus-headers-retrieved-by
4133                           (gnus-retrieve-headers
4134                            articles gnus-newsgroup-name
4135                            ;; We might want to fetch old headers, but
4136                            ;; not if there is only 1 article.
4137                            (and (or (and
4138                                      (not (eq gnus-fetch-old-headers 'some))
4139                                      (not (numberp gnus-fetch-old-headers)))
4140                                     (> (length articles) 1))
4141                                 gnus-fetch-old-headers))))
4142                 (gnus-get-newsgroup-headers-xover
4143                  articles nil nil gnus-newsgroup-name t)
4144               (gnus-get-newsgroup-headers)))
4145       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4146
4147       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4148       (when cached
4149         (setq gnus-newsgroup-cached cached))
4150
4151       ;; Suppress duplicates?
4152       (when gnus-suppress-duplicates
4153         (gnus-dup-suppress-articles))
4154
4155       ;; Set the initial limit.
4156       (setq gnus-newsgroup-limit (copy-sequence articles))
4157       ;; Remove canceled articles from the list of unread articles.
4158       (setq gnus-newsgroup-unreads
4159             (gnus-set-sorted-intersection
4160              gnus-newsgroup-unreads
4161              (setq fetched-articles
4162                    (mapcar (lambda (headers) (mail-header-number headers))
4163                            gnus-newsgroup-headers))))
4164       ;; Removed marked articles that do not exist.
4165       (gnus-update-missing-marks
4166        (gnus-sorted-complement fetched-articles articles))
4167       ;; We might want to build some more threads first.
4168       (when (and gnus-fetch-old-headers
4169                  (eq gnus-headers-retrieved-by 'nov))
4170         (if (eq gnus-fetch-old-headers 'invisible)
4171             (gnus-build-all-threads)
4172           (gnus-build-old-threads)))
4173       ;; Let the Gnus agent mark articles as read.
4174       (when gnus-agent
4175         (gnus-agent-get-undownloaded-list))
4176       ;; Remove list identifiers from subject
4177       (when gnus-list-identifiers
4178         (gnus-summary-remove-list-identifiers))
4179       ;; Check whether auto-expire is to be done in this group.
4180       (setq gnus-newsgroup-auto-expire
4181             (gnus-group-auto-expirable-p group))
4182       ;; Set up the article buffer now, if necessary.
4183       (unless gnus-single-article-buffer
4184         (gnus-article-setup-buffer))
4185       ;; First and last article in this newsgroup.
4186       (when gnus-newsgroup-headers
4187         (setq gnus-newsgroup-begin
4188               (mail-header-number (car gnus-newsgroup-headers))
4189               gnus-newsgroup-end
4190               (mail-header-number
4191                (gnus-last-element gnus-newsgroup-headers))))
4192       ;; GROUP is successfully selected.
4193       (or gnus-newsgroup-headers t)))))
4194
4195 (defun gnus-articles-to-read (group &optional read-all)
4196   ;; Find out what articles the user wants to read.
4197   (let* ((articles
4198           ;; Select all articles if `read-all' is non-nil, or if there
4199           ;; are no unread articles.
4200           (if (or read-all
4201                   (and (zerop (length gnus-newsgroup-marked))
4202                        (zerop (length gnus-newsgroup-unreads)))
4203                   (eq (gnus-group-find-parameter group 'display)
4204                       'all))
4205               (or
4206                (gnus-uncompress-range (gnus-active group))
4207                (gnus-cache-articles-in-group group))
4208             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4209                           (copy-sequence gnus-newsgroup-unreads))
4210                   '<)))
4211          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4212          (scored (length scored-list))
4213          (number (length articles))
4214          (marked (+ (length gnus-newsgroup-marked)
4215                     (length gnus-newsgroup-dormant)))
4216          (select
4217           (cond
4218            ((numberp read-all)
4219             read-all)
4220            (t
4221             (condition-case ()
4222                 (cond
4223                  ((and (or (<= scored marked) (= scored number))
4224                        (numberp gnus-large-newsgroup)
4225                        (> number gnus-large-newsgroup))
4226                   (let ((input
4227                          (read-string
4228                           (format
4229                            "How many articles from %s (default %d): "
4230                            (gnus-limit-string gnus-newsgroup-name 35)
4231                            number))))
4232                     (if (string-match "^[ \t]*$" input) number input)))
4233                  ((and (> scored marked) (< scored number)
4234                        (> (- scored number) 20))
4235                   (let ((input
4236                          (read-string
4237                           (format "%s %s (%d scored, %d total): "
4238                                   "How many articles from"
4239                                   group scored number))))
4240                     (if (string-match "^[ \t]*$" input)
4241                         number input)))
4242                  (t number))
4243               (quit nil))))))
4244     (setq select (if (stringp select) (string-to-number select) select))
4245     (if (or (null select) (zerop select))
4246         select
4247       (if (and (not (zerop scored)) (<= (abs select) scored))
4248           (progn
4249             (setq articles (sort scored-list '<))
4250             (setq number (length articles)))
4251         (setq articles (copy-sequence articles)))
4252
4253       (when (< (abs select) number)
4254         (if (< select 0)
4255             ;; Select the N oldest articles.
4256             (setcdr (nthcdr (1- (abs select)) articles) nil)
4257           ;; Select the N most recent articles.
4258           (setq articles (nthcdr (- number select) articles))))
4259       (setq gnus-newsgroup-unselected
4260             (gnus-sorted-intersection
4261              gnus-newsgroup-unreads
4262              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4263       articles)))
4264
4265 (defun gnus-killed-articles (killed articles)
4266   (let (out)
4267     (while articles
4268       (when (inline (gnus-member-of-range (car articles) killed))
4269         (push (car articles) out))
4270       (setq articles (cdr articles)))
4271     out))
4272
4273 (defun gnus-uncompress-marks (marks)
4274   "Uncompress the mark ranges in MARKS."
4275   (let ((uncompressed '(score bookmark))
4276         out)
4277     (while marks
4278       (if (memq (caar marks) uncompressed)
4279           (push (car marks) out)
4280         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4281       (setq marks (cdr marks)))
4282     out))
4283
4284 (defun gnus-adjust-marked-articles (info)
4285   "Set all article lists and remove all marks that are no longer valid."
4286   (let* ((marked-lists (gnus-info-marks info))
4287          (active (gnus-active (gnus-info-group info)))
4288          (min (car active))
4289          (max (cdr active))
4290          (types gnus-article-mark-lists)
4291          (uncompressed '(score bookmark killed))
4292          marks var articles article mark)
4293
4294     (while marked-lists
4295       (setq marks (pop marked-lists))
4296       (set (setq var (intern (format "gnus-newsgroup-%s"
4297                                      (car (rassq (setq mark (car marks))
4298                                                  types)))))
4299            (if (memq (car marks) uncompressed) (cdr marks)
4300              (gnus-uncompress-range (cdr marks))))
4301
4302       (setq articles (symbol-value var))
4303
4304       ;; All articles have to be subsets of the active articles.
4305       (cond
4306        ;; Adjust "simple" lists.
4307        ((memq mark '(tick dormant expire reply save))
4308         (while articles
4309           (when (or (< (setq article (pop articles)) min) (> article max))
4310             (set var (delq article (symbol-value var))))))
4311        ;; Adjust assocs.
4312        ((memq mark uncompressed)
4313         (when (not (listp (cdr (symbol-value var))))
4314           (set var (list (symbol-value var))))
4315         (when (not (listp (cdr articles)))
4316           (setq articles (list articles)))
4317         (while articles
4318           (when (or (not (consp (setq article (pop articles))))
4319                     (< (car article) min)
4320                     (> (car article) max))
4321             (set var (delq article (symbol-value var))))))))))
4322
4323 (defun gnus-update-missing-marks (missing)
4324   "Go through the list of MISSING articles and remove them from the mark lists."
4325   (when missing
4326     (let ((types gnus-article-mark-lists)
4327           var m)
4328       ;; Go through all types.
4329       (while types
4330         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4331         (when (symbol-value var)
4332           ;; This list has articles.  So we delete all missing articles
4333           ;; from it.
4334           (setq m missing)
4335           (while m
4336             (set var (delq (pop m) (symbol-value var)))))))))
4337
4338 (defun gnus-update-marks ()
4339   "Enter the various lists of marked articles into the newsgroup info list."
4340   (let ((types gnus-article-mark-lists)
4341         (info (gnus-get-info gnus-newsgroup-name))
4342         (uncompressed '(score bookmark killed))
4343         type list newmarked symbol delta-marks)
4344     (when info
4345       ;; Add all marks lists to the list of marks lists.
4346       (while (setq type (pop types))
4347         (setq list (symbol-value
4348                           (setq symbol
4349                                 (intern (format "gnus-newsgroup-%s"
4350                                                 (car type))))))
4351
4352         (when list
4353           ;; Get rid of the entries of the articles that have the
4354           ;; default score.
4355           (when (and (eq (cdr type) 'score)
4356                      gnus-save-score
4357                      list)
4358             (let* ((arts list)
4359                    (prev (cons nil list))
4360                    (all prev))
4361               (while arts
4362                 (if (or (not (consp (car arts)))
4363                         (= (cdar arts) gnus-summary-default-score))
4364                     (setcdr prev (cdr arts))
4365                   (setq prev arts))
4366                 (setq arts (cdr arts)))
4367               (setq list (cdr all)))))
4368
4369        (or (memq (cdr type) uncompressed)
4370            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4371        
4372        (when (gnus-check-backend-function 'request-set-mark
4373                                           gnus-newsgroup-name)
4374          ;; uncompressed:s are not proper flags (they are cons cells)
4375          ;; cache is a internal gnus flag
4376          (unless (memq (cdr type) (cons 'cache uncompressed))
4377            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4378                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4379                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4380              (if add
4381                  (push (list add 'add (list (cdr type))) delta-marks))
4382              (if del
4383                  (push (list del 'del (list (cdr type))) delta-marks)))))
4384           
4385         (when list
4386          (push (cons (cdr type) list) newmarked)))
4387
4388       (when delta-marks
4389         (unless (gnus-check-group gnus-newsgroup-name)
4390           (error "Can't open server for %s" gnus-newsgroup-name))
4391         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4392           
4393       ;; Enter these new marks into the info of the group.
4394       (if (nthcdr 3 info)
4395           (setcar (nthcdr 3 info) newmarked)
4396         ;; Add the marks lists to the end of the info.
4397         (when newmarked
4398           (setcdr (nthcdr 2 info) (list newmarked))))
4399
4400       ;; Cut off the end of the info if there's nothing else there.
4401       (let ((i 5))
4402         (while (and (> i 2)
4403                     (not (nth i info)))
4404           (when (nthcdr (decf i) info)
4405             (setcdr (nthcdr i info) nil)))))))
4406
4407 (defun gnus-set-mode-line (where)
4408   "This function sets the mode line of the article or summary buffers.
4409 If WHERE is `summary', the summary mode line format will be used."
4410   ;; Is this mode line one we keep updated?
4411   (when (and (memq where gnus-updated-mode-lines)
4412              (symbol-value
4413               (intern (format "gnus-%s-mode-line-format-spec" where))))
4414     (let (mode-string)
4415       (save-excursion
4416         ;; We evaluate this in the summary buffer since these
4417         ;; variables are buffer-local to that buffer.
4418         (set-buffer gnus-summary-buffer)
4419         ;; We bind all these variables that are used in the `eval' form
4420         ;; below.
4421         (let* ((mformat (symbol-value
4422                          (intern
4423                           (format "gnus-%s-mode-line-format-spec" where))))
4424                (gnus-tmp-group-name gnus-newsgroup-name)
4425                (gnus-tmp-article-number (or gnus-current-article 0))
4426                (gnus-tmp-unread gnus-newsgroup-unreads)
4427                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4428                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4429                (gnus-tmp-unread-and-unselected
4430                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4431                             (zerop gnus-tmp-unselected))
4432                        "")
4433                       ((zerop gnus-tmp-unselected)
4434                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4435                       (t (format "{%d(+%d) more}"
4436                                  gnus-tmp-unread-and-unticked
4437                                  gnus-tmp-unselected))))
4438                (gnus-tmp-subject
4439                 (if (and gnus-current-headers
4440                          (vectorp gnus-current-headers))
4441                     (gnus-mode-string-quote
4442                      (mail-header-subject gnus-current-headers))
4443                   ""))
4444                bufname-length max-len
4445                gnus-tmp-header);; passed as argument to any user-format-funcs
4446           (setq mode-string (eval mformat))
4447           (setq bufname-length (if (string-match "%b" mode-string)
4448                                    (- (length
4449                                        (buffer-name
4450                                         (if (eq where 'summary)
4451                                             nil
4452                                           (get-buffer gnus-article-buffer))))
4453                                       2)
4454                                  0))
4455           (setq max-len (max 4 (if gnus-mode-non-string-length
4456                                    (- (window-width)
4457                                       gnus-mode-non-string-length
4458                                       bufname-length)
4459                                  (length mode-string))))
4460           ;; We might have to chop a bit of the string off...
4461           (when (> (length mode-string) max-len)
4462             (setq mode-string
4463                   (concat (truncate-string-to-width mode-string (- max-len 3))
4464                           "...")))
4465           ;; Pad the mode string a bit.
4466           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4467       ;; Update the mode line.
4468       (setq mode-line-buffer-identification
4469             (gnus-mode-line-buffer-identification (list mode-string)))
4470       (set-buffer-modified-p t))))
4471
4472 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4473   "Go through the HEADERS list and add all Xrefs to a hash table.
4474 The resulting hash table is returned, or nil if no Xrefs were found."
4475   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4476          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4477          (xref-hashtb (gnus-make-hashtable))
4478          start group entry number xrefs header)
4479     (while headers
4480       (setq header (pop headers))
4481       (when (and (setq xrefs (mail-header-xref header))
4482                  (not (memq (setq number (mail-header-number header))
4483                             unreads)))
4484         (setq start 0)
4485         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4486           (setq start (match-end 0))
4487           (setq group (if prefix
4488                           (concat prefix (substring xrefs (match-beginning 1)
4489                                                     (match-end 1)))
4490                         (substring xrefs (match-beginning 1) (match-end 1))))
4491           (setq number
4492                 (string-to-int (substring xrefs (match-beginning 2)
4493                                           (match-end 2))))
4494           (if (setq entry (gnus-gethash group xref-hashtb))
4495               (setcdr entry (cons number (cdr entry)))
4496             (gnus-sethash group (cons number nil) xref-hashtb)))))
4497     (and start xref-hashtb)))
4498
4499 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4500   "Look through all the headers and mark the Xrefs as read."
4501   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4502         name entry info xref-hashtb idlist method nth4)
4503     (save-excursion
4504       (set-buffer gnus-group-buffer)
4505       (when (setq xref-hashtb
4506                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4507         (mapatoms
4508          (lambda (group)
4509            (unless (string= from-newsgroup (setq name (symbol-name group)))
4510              (setq idlist (symbol-value group))
4511              ;; Dead groups are not updated.
4512              (and (prog1
4513                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4514                             info (nth 2 entry))
4515                     (when (stringp (setq nth4 (gnus-info-method info)))
4516                       (setq nth4 (gnus-server-to-method nth4))))
4517                   ;; Only do the xrefs if the group has the same
4518                   ;; select method as the group we have just read.
4519                   (or (gnus-methods-equal-p
4520                        nth4 (gnus-find-method-for-group from-newsgroup))
4521                       virtual
4522                       (equal nth4 (setq method (gnus-find-method-for-group
4523                                                 from-newsgroup)))
4524                       (and (equal (car nth4) (car method))
4525                            (equal (nth 1 nth4) (nth 1 method))))
4526                   gnus-use-cross-reference
4527                   (or (not (eq gnus-use-cross-reference t))
4528                       virtual
4529                       ;; Only do cross-references on subscribed
4530                       ;; groups, if that is what is wanted.
4531                       (<= (gnus-info-level info) gnus-level-subscribed))
4532                   (gnus-group-make-articles-read name idlist))))
4533          xref-hashtb)))))
4534
4535 (defun gnus-compute-read-articles (group articles)
4536   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4537          (info (nth 2 entry))
4538          (active (gnus-active group))
4539          ninfo)
4540     (when entry
4541       ;; First peel off all invalid article numbers.
4542       (when active
4543         (let ((ids articles)
4544               id first)
4545           (while (setq id (pop ids))
4546             (when (and first (> id (cdr active)))
4547               ;; We'll end up in this situation in one particular
4548               ;; obscure situation.  If you re-scan a group and get
4549               ;; a new article that is cross-posted to a different
4550               ;; group that has not been re-scanned, you might get
4551               ;; crossposted article that has a higher number than
4552               ;; Gnus believes possible.  So we re-activate this
4553               ;; group as well.  This might mean doing the
4554               ;; crossposting thingy will *increase* the number
4555               ;; of articles in some groups.  Tsk, tsk.
4556               (setq active (or (gnus-activate-group group) active)))
4557             (when (or (> id (cdr active))
4558                       (< id (car active)))
4559               (setq articles (delq id articles))))))
4560       ;; If the read list is nil, we init it.
4561       (if (and active
4562                (null (gnus-info-read info))
4563                (> (car active) 1))
4564           (setq ninfo (cons 1 (1- (car active))))
4565         (setq ninfo (gnus-info-read info)))
4566       ;; Then we add the read articles to the range.
4567       (gnus-add-to-range
4568        ninfo (setq articles (sort articles '<))))))
4569
4570 (defun gnus-group-make-articles-read (group articles)
4571   "Update the info of GROUP to say that ARTICLES are read."
4572   (let* ((num 0)
4573          (entry (gnus-gethash group gnus-newsrc-hashtb))
4574          (info (nth 2 entry))
4575          (active (gnus-active group))
4576          range)
4577     (when entry
4578       (setq range (gnus-compute-read-articles group articles))
4579       (save-excursion
4580         (set-buffer gnus-group-buffer)
4581         (gnus-undo-register
4582           `(progn
4583              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4584              (gnus-info-set-read ',info ',(gnus-info-read info))
4585              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4586              (gnus-group-update-group ,group t))))
4587       ;; Add the read articles to the range.
4588       (gnus-info-set-read info range)
4589       ;; Then we have to re-compute how many unread
4590       ;; articles there are in this group.
4591       (when active
4592         (cond
4593          ((not range)
4594           (setq num (- (1+ (cdr active)) (car active))))
4595          ((not (listp (cdr range)))
4596           (setq num (- (cdr active) (- (1+ (cdr range))
4597                                        (car range)))))
4598          (t
4599           (while range
4600             (if (numberp (car range))
4601                 (setq num (1+ num))
4602               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4603             (setq range (cdr range)))
4604           (setq num (- (cdr active) num))))
4605         ;; Update the number of unread articles.
4606         (setcar entry num)
4607         ;; Update the group buffer.
4608         (gnus-group-update-group group t)))))
4609
4610 (defvar gnus-newsgroup-none-id 0)
4611
4612 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4613   (let ((cur nntp-server-buffer)
4614         (dependencies
4615          (or dependencies
4616              (save-excursion (set-buffer gnus-summary-buffer)
4617                              gnus-newsgroup-dependencies)))
4618         headers id end ref
4619         (mail-parse-charset gnus-newsgroup-charset)
4620         (mail-parse-ignored-charsets 
4621              (save-excursion (condition-case nil
4622                                  (set-buffer gnus-summary-buffer)
4623                                (error))
4624                              gnus-newsgroup-ignored-charsets)))
4625     (save-excursion
4626       (set-buffer nntp-server-buffer)
4627       ;; Translate all TAB characters into SPACE characters.
4628       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4629       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4630       (gnus-run-hooks 'gnus-parse-headers-hook)
4631       (let ((case-fold-search t)
4632             in-reply-to header p lines chars)
4633         (goto-char (point-min))
4634         ;; Search to the beginning of the next header.  Error messages
4635         ;; do not begin with 2 or 3.
4636         (while (re-search-forward "^[23][0-9]+ " nil t)
4637           (setq id nil
4638                 ref nil)
4639           ;; This implementation of this function, with nine
4640           ;; search-forwards instead of the one re-search-forward and
4641           ;; a case (which basically was the old function) is actually
4642           ;; about twice as fast, even though it looks messier.  You
4643           ;; can't have everything, I guess.  Speed and elegance
4644           ;; doesn't always go hand in hand.
4645           (setq
4646            header
4647            (vector
4648             ;; Number.
4649             (prog1
4650                 (read cur)
4651               (end-of-line)
4652               (setq p (point))
4653               (narrow-to-region (point)
4654                                 (or (and (search-forward "\n.\n" nil t)
4655                                          (- (point) 2))
4656                                     (point))))
4657             ;; Subject.
4658             (progn
4659               (goto-char p)
4660               (if (search-forward "\nsubject: " nil t)
4661                   (funcall gnus-decode-encoded-word-function
4662                            (nnheader-header-value))
4663                 "(none)"))
4664             ;; From.
4665             (progn
4666               (goto-char p)
4667               (if (search-forward "\nfrom: " nil t)
4668                   (funcall gnus-decode-encoded-word-function
4669                            (nnheader-header-value))
4670                 "(nobody)"))
4671             ;; Date.
4672             (progn
4673               (goto-char p)
4674               (if (search-forward "\ndate: " nil t)
4675                   (nnheader-header-value) ""))
4676             ;; Message-ID.
4677             (progn
4678               (goto-char p)
4679               (setq id (if (re-search-forward
4680                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4681                            ;; We do it this way to make sure the Message-ID
4682                            ;; is (somewhat) syntactically valid.
4683                            (buffer-substring (match-beginning 1)
4684                                              (match-end 1))
4685                          ;; If there was no message-id, we just fake one
4686                          ;; to make subsequent routines simpler.
4687                          (nnheader-generate-fake-message-id))))
4688             ;; References.
4689             (progn
4690               (goto-char p)
4691               (if (search-forward "\nreferences: " nil t)
4692                   (progn
4693                     (setq end (point))
4694                     (prog1
4695                         (nnheader-header-value)
4696                       (setq ref
4697                             (buffer-substring
4698                              (progn
4699                                (end-of-line)
4700                                (search-backward ">" end t)
4701                                (1+ (point)))
4702                              (progn
4703                                (search-backward "<" end t)
4704                                (point))))))
4705                 ;; Get the references from the in-reply-to header if there
4706                 ;; were no references and the in-reply-to header looks
4707                 ;; promising.
4708                 (if (and (search-forward "\nin-reply-to: " nil t)
4709                          (setq in-reply-to (nnheader-header-value))
4710                          (string-match "<[^>]+>" in-reply-to))
4711                     (let (ref2)
4712                       (setq ref (substring in-reply-to (match-beginning 0)
4713                                            (match-end 0)))
4714                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4715                         (setq ref2 (substring in-reply-to (match-beginning 0)
4716                                               (match-end 0)))
4717                         (when (> (length ref2) (length ref))
4718                           (setq ref ref2)))
4719                       ref)
4720                   (setq ref nil))))
4721             ;; Chars.
4722             (progn
4723               (goto-char p)
4724               (if (search-forward "\nchars: " nil t)
4725                   (if (numberp (setq chars (ignore-errors (read cur))))
4726                       chars 0)
4727                 0))
4728             ;; Lines.
4729             (progn
4730               (goto-char p)
4731               (if (search-forward "\nlines: " nil t)
4732                   (if (numberp (setq lines (ignore-errors (read cur))))
4733                       lines 0)
4734                 0))
4735             ;; Xref.
4736             (progn
4737               (goto-char p)
4738               (and (search-forward "\nxref: " nil t)
4739                    (nnheader-header-value)))
4740             ;; Extra.
4741             (when gnus-extra-headers
4742               (let ((extra gnus-extra-headers)
4743                     out)
4744                 (while extra
4745                   (goto-char p)
4746                   (when (search-forward
4747                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4748                     (push (cons (car extra) (nnheader-header-value))
4749                           out))
4750                   (pop extra))
4751                 out))))
4752           (when (equal id ref)
4753             (setq ref nil))
4754
4755           (when gnus-alter-header-function
4756             (funcall gnus-alter-header-function header)
4757             (setq id (mail-header-id header)
4758                   ref (gnus-parent-id (mail-header-references header))))
4759
4760           (when (setq header
4761                       (gnus-dependencies-add-header
4762                        header dependencies force-new))
4763             (push header headers))
4764           (goto-char (point-max))
4765           (widen))
4766         (nreverse headers)))))
4767
4768 ;; Goes through the xover lines and returns a list of vectors
4769 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4770                                                   force-new dependencies
4771                                                   group also-fetch-heads)
4772   "Parse the news overview data in the server buffer, and return a
4773 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4774   ;; Get the Xref when the users reads the articles since most/some
4775   ;; NNTP servers do not include Xrefs when using XOVER.
4776   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4777   (let ((mail-parse-charset gnus-newsgroup-charset)
4778         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4779         (cur nntp-server-buffer)
4780         (dependencies (or dependencies gnus-newsgroup-dependencies))
4781         number headers header)
4782     (save-excursion
4783       (set-buffer nntp-server-buffer)
4784       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4785       ;; Allow the user to mangle the headers before parsing them.
4786       (gnus-run-hooks 'gnus-parse-headers-hook)
4787       (goto-char (point-min))
4788       (while (not (eobp))
4789         (condition-case ()
4790             (while (and sequence (not (eobp)))
4791               (setq number (read cur))
4792               (while (and sequence
4793                           (< (car sequence) number))
4794                 (setq sequence (cdr sequence)))
4795               (and sequence
4796                    (eq number (car sequence))
4797                    (progn
4798                      (setq sequence (cdr sequence))
4799                      (setq header (inline
4800                                     (gnus-nov-parse-line
4801                                      number dependencies force-new))))
4802                    (push header headers))
4803               (forward-line 1))
4804           (error
4805            (gnus-error 4 "Strange nov line (%d)"
4806                        (count-lines (point-min) (point)))))
4807         (forward-line 1))
4808       ;; A common bug in inn is that if you have posted an article and
4809       ;; then retrieves the active file, it will answer correctly --
4810       ;; the new article is included.  However, a NOV entry for the
4811       ;; article may not have been generated yet, so this may fail.
4812       ;; We work around this problem by retrieving the last few
4813       ;; headers using HEAD.
4814       (if (or (not also-fetch-heads)
4815               (not sequence))
4816           ;; We (probably) got all the headers.
4817           (nreverse headers)
4818         (let ((gnus-nov-is-evil t))
4819           (nconc
4820            (nreverse headers)
4821            (when (gnus-retrieve-headers sequence group)
4822              (gnus-get-newsgroup-headers))))))))
4823
4824 (defun gnus-article-get-xrefs ()
4825   "Fill in the Xref value in `gnus-current-headers', if necessary.
4826 This is meant to be called in `gnus-article-internal-prepare-hook'."
4827   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4828                                  gnus-current-headers)))
4829     (or (not gnus-use-cross-reference)
4830         (not headers)
4831         (and (mail-header-xref headers)
4832              (not (string= (mail-header-xref headers) "")))
4833         (let ((case-fold-search t)
4834               xref)
4835           (save-restriction
4836             (nnheader-narrow-to-headers)
4837             (goto-char (point-min))
4838             (when (or (and (not (eobp))
4839                            (eq (downcase (char-after)) ?x)
4840                            (looking-at "Xref:"))
4841                       (search-forward "\nXref:" nil t))
4842               (goto-char (1+ (match-end 0)))
4843               (setq xref (buffer-substring (point)
4844                                            (progn (end-of-line) (point))))
4845               (mail-header-set-xref headers xref)))))))
4846
4847 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4848   "Find article ID and insert the summary line for that article.
4849 OLD-HEADER can either be a header or a line number to insert
4850 the subject line on."
4851   (let* ((line (and (numberp old-header) old-header))
4852          (old-header (and (vectorp old-header) old-header))
4853          (header (cond ((and old-header use-old-header)
4854                         old-header)
4855                        ((and (numberp id)
4856                              (gnus-number-to-header id))
4857                         (gnus-number-to-header id))
4858                        (t
4859                         (gnus-read-header id))))
4860          (number (and (numberp id) id))
4861          d)
4862     (when header
4863       ;; Rebuild the thread that this article is part of and go to the
4864       ;; article we have fetched.
4865       (when (and (not gnus-show-threads)
4866                  old-header)
4867         (when (and number
4868                    (setq d (gnus-data-find (mail-header-number old-header))))
4869           (goto-char (gnus-data-pos d))
4870           (gnus-data-remove
4871            number
4872            (- (gnus-point-at-bol)
4873               (prog1
4874                   (1+ (gnus-point-at-eol))
4875                 (gnus-delete-line))))))
4876       (when old-header
4877         (mail-header-set-number header (mail-header-number old-header)))
4878       (setq gnus-newsgroup-sparse
4879             (delq (setq number (mail-header-number header))
4880                   gnus-newsgroup-sparse))
4881       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4882       (push number gnus-newsgroup-limit)
4883       (gnus-rebuild-thread (mail-header-id header) line)
4884       (gnus-summary-goto-subject number nil t))
4885     (when (and (numberp number)
4886                (> number 0))
4887       ;; We have to update the boundaries even if we can't fetch the
4888       ;; article if ID is a number -- so that the next `P' or `N'
4889       ;; command will fetch the previous (or next) article even
4890       ;; if the one we tried to fetch this time has been canceled.
4891       (when (> number gnus-newsgroup-end)
4892         (setq gnus-newsgroup-end number))
4893       (when (< number gnus-newsgroup-begin)
4894         (setq gnus-newsgroup-begin number))
4895       (setq gnus-newsgroup-unselected
4896             (delq number gnus-newsgroup-unselected)))
4897     ;; Report back a success?
4898     (and header (mail-header-number header))))
4899
4900 ;;; Process/prefix in the summary buffer
4901
4902 (defun gnus-summary-work-articles (n)
4903   "Return a list of articles to be worked upon.
4904 The prefix argument, the list of process marked articles, and the
4905 current article will be taken into consideration."
4906   (save-excursion
4907     (set-buffer gnus-summary-buffer)
4908     (cond
4909      (n
4910       ;; A numerical prefix has been given.
4911       (setq n (prefix-numeric-value n))
4912       (let ((backward (< n 0))
4913             (n (abs (prefix-numeric-value n)))
4914             articles article)
4915         (save-excursion
4916           (while
4917               (and (> n 0)
4918                    (push (setq article (gnus-summary-article-number))
4919                          articles)
4920                    (if backward
4921                        (gnus-summary-find-prev nil article)
4922                      (gnus-summary-find-next nil article)))
4923             (decf n)))
4924         (nreverse articles)))
4925      ((and (gnus-region-active-p) (mark))
4926       (message "region active")
4927       ;; Work on the region between point and mark.
4928       (let ((max (max (point) (mark)))
4929             articles article)
4930         (save-excursion
4931           (goto-char (min (min (point) (mark))))
4932           (while
4933               (and
4934                (push (setq article (gnus-summary-article-number)) articles)
4935                (gnus-summary-find-next nil article)
4936                (< (point) max)))
4937           (nreverse articles))))
4938      (gnus-newsgroup-processable
4939       ;; There are process-marked articles present.
4940       ;; Save current state.
4941       (gnus-summary-save-process-mark)
4942       ;; Return the list.
4943       (reverse gnus-newsgroup-processable))
4944      (t
4945       ;; Just return the current article.
4946       (list (gnus-summary-article-number))))))
4947
4948 (defmacro gnus-summary-iterate (arg &rest forms)
4949   "Iterate over the process/prefixed articles and do FORMS.
4950 ARG is the interactive prefix given to the command.  FORMS will be
4951 executed with point over the summary line of the articles."
4952   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4953     `(let ((,articles (gnus-summary-work-articles ,arg)))
4954        (while ,articles
4955          (gnus-summary-goto-subject (car ,articles))
4956          ,@forms
4957          (pop ,articles)))))
4958
4959 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4960 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4961
4962 (defun gnus-summary-save-process-mark ()
4963   "Push the current set of process marked articles on the stack."
4964   (interactive)
4965   (push (copy-sequence gnus-newsgroup-processable)
4966         gnus-newsgroup-process-stack))
4967
4968 (defun gnus-summary-kill-process-mark ()
4969   "Push the current set of process marked articles on the stack and unmark."
4970   (interactive)
4971   (gnus-summary-save-process-mark)
4972   (gnus-summary-unmark-all-processable))
4973
4974 (defun gnus-summary-yank-process-mark ()
4975   "Pop the last process mark state off the stack and restore it."
4976   (interactive)
4977   (unless gnus-newsgroup-process-stack
4978     (error "Empty mark stack"))
4979   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4980
4981 (defun gnus-summary-process-mark-set (set)
4982   "Make SET into the current process marked articles."
4983   (gnus-summary-unmark-all-processable)
4984   (while set
4985     (gnus-summary-set-process-mark (pop set))))
4986
4987 ;;; Searching and stuff
4988
4989 (defun gnus-summary-search-group (&optional backward use-level)
4990   "Search for next unread newsgroup.
4991 If optional argument BACKWARD is non-nil, search backward instead."
4992   (save-excursion
4993     (set-buffer gnus-group-buffer)
4994     (when (gnus-group-search-forward
4995            backward nil (if use-level (gnus-group-group-level) nil))
4996       (gnus-group-group-name))))
4997
4998 (defun gnus-summary-best-group (&optional exclude-group)
4999   "Find the name of the best unread group.
5000 If EXCLUDE-GROUP, do not go to this group."
5001   (save-excursion
5002     (set-buffer gnus-group-buffer)
5003     (save-excursion
5004       (gnus-group-best-unread-group exclude-group))))
5005
5006 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5007   (if backward (gnus-summary-find-prev)
5008     (let* ((dummy (gnus-summary-article-intangible-p))
5009            (article (or article (gnus-summary-article-number)))
5010            (arts (gnus-data-find-list article))
5011            result)
5012       (when (and (not dummy)
5013                  (or (not gnus-summary-check-current)
5014                      (not unread)
5015                      (not (gnus-data-unread-p (car arts)))))
5016         (setq arts (cdr arts)))
5017       (when (setq result
5018                   (if unread
5019                       (progn
5020                         (while arts
5021                           (when (or (and undownloaded
5022                                          (eq gnus-undownloaded-mark
5023                                              (gnus-data-mark (car arts))))
5024                                     (gnus-data-unread-p (car arts)))
5025                             (setq result (car arts)
5026                                   arts nil))
5027                           (setq arts (cdr arts)))
5028                         result)
5029                     (car arts)))
5030         (goto-char (gnus-data-pos result))
5031         (gnus-data-number result)))))
5032
5033 (defun gnus-summary-find-prev (&optional unread article)
5034   (let* ((eobp (eobp))
5035          (article (or article (gnus-summary-article-number)))
5036          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5037          result)
5038     (when (and (not eobp)
5039                (or (not gnus-summary-check-current)
5040                    (not unread)
5041                    (not (gnus-data-unread-p (car arts)))))
5042       (setq arts (cdr arts)))
5043     (when (setq result
5044                 (if unread
5045                     (progn
5046                       (while arts
5047                         (when (gnus-data-unread-p (car arts))
5048                           (setq result (car arts)
5049                                 arts nil))
5050                         (setq arts (cdr arts)))
5051                       result)
5052                   (car arts)))
5053       (goto-char (gnus-data-pos result))
5054       (gnus-data-number result))))
5055
5056 (defun gnus-summary-find-subject (subject &optional unread backward article)
5057   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5058          (article (or article (gnus-summary-article-number)))
5059          (articles (gnus-data-list backward))
5060          (arts (gnus-data-find-list article articles))
5061          result)
5062     (when (or (not gnus-summary-check-current)
5063               (not unread)
5064               (not (gnus-data-unread-p (car arts))))
5065       (setq arts (cdr arts)))
5066     (while arts
5067       (and (or (not unread)
5068                (gnus-data-unread-p (car arts)))
5069            (vectorp (gnus-data-header (car arts)))
5070            (gnus-subject-equal
5071             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5072            (setq result (car arts)
5073                  arts nil))
5074       (setq arts (cdr arts)))
5075     (and result
5076          (goto-char (gnus-data-pos result))
5077          (gnus-data-number result))))
5078
5079 (defun gnus-summary-search-forward (&optional unread subject backward)
5080   "Search forward for an article.
5081 If UNREAD, look for unread articles.  If SUBJECT, look for
5082 articles with that subject.  If BACKWARD, search backward instead."
5083   (cond (subject (gnus-summary-find-subject subject unread backward))
5084         (backward (gnus-summary-find-prev unread))
5085         (t (gnus-summary-find-next unread))))
5086
5087 (defun gnus-recenter (&optional n)
5088   "Center point in window and redisplay frame.
5089 Also do horizontal recentering."
5090   (interactive "P")
5091   (when (and gnus-auto-center-summary
5092              (not (eq gnus-auto-center-summary 'vertical)))
5093     (gnus-horizontal-recenter))
5094   (recenter n))
5095
5096 (defun gnus-summary-recenter ()
5097   "Center point in the summary window.
5098 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5099 displayed, no centering will be performed."
5100   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5101   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5102   (let* ((top (cond ((< (window-height) 4) 0)
5103                     ((< (window-height) 7) 1)
5104                     (t (if (numberp gnus-auto-center-summary)
5105                            gnus-auto-center-summary
5106                          2))))
5107          (height (1- (window-height)))
5108          (bottom (save-excursion (goto-char (point-max))
5109                                  (forward-line (- height))
5110                                  (point)))
5111          (window (get-buffer-window (current-buffer))))
5112     ;; The user has to want it.
5113     (when gnus-auto-center-summary
5114       (when (get-buffer-window gnus-article-buffer)
5115         ;; Only do recentering when the article buffer is displayed,
5116         ;; Set the window start to either `bottom', which is the biggest
5117         ;; possible valid number, or the second line from the top,
5118         ;; whichever is the least.
5119         (set-window-start
5120          window (min bottom (save-excursion
5121                               (forward-line (- top)) (point)))
5122          t))
5123       ;; Do horizontal recentering while we're at it.
5124       (when (and (get-buffer-window (current-buffer) t)
5125                  (not (eq gnus-auto-center-summary 'vertical)))
5126         (let ((selected (selected-window)))
5127           (select-window (get-buffer-window (current-buffer) t))
5128           (gnus-summary-position-point)
5129           (gnus-horizontal-recenter)
5130           (select-window selected))))))
5131
5132 (defun gnus-summary-jump-to-group (newsgroup)
5133   "Move point to NEWSGROUP in group mode buffer."
5134   ;; Keep update point of group mode buffer if visible.
5135   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5136       (save-window-excursion
5137         ;; Take care of tree window mode.
5138         (when (get-buffer-window gnus-group-buffer)
5139           (pop-to-buffer gnus-group-buffer))
5140         (gnus-group-jump-to-group newsgroup))
5141     (save-excursion
5142       ;; Take care of tree window mode.
5143       (if (get-buffer-window gnus-group-buffer)
5144           (pop-to-buffer gnus-group-buffer)
5145         (set-buffer gnus-group-buffer))
5146       (gnus-group-jump-to-group newsgroup))))
5147
5148 ;; This function returns a list of article numbers based on the
5149 ;; difference between the ranges of read articles in this group and
5150 ;; the range of active articles.
5151 (defun gnus-list-of-unread-articles (group)
5152   (let* ((read (gnus-info-read (gnus-get-info group)))
5153          (active (or (gnus-active group) (gnus-activate-group group)))
5154          (last (cdr active))
5155          first nlast unread)
5156     ;; If none are read, then all are unread.
5157     (if (not read)
5158         (setq first (car active))
5159       ;; If the range of read articles is a single range, then the
5160       ;; first unread article is the article after the last read
5161       ;; article.  Sounds logical, doesn't it?
5162       (if (not (listp (cdr read)))
5163           (setq first (max (car active) (1+ (cdr read))))
5164         ;; `read' is a list of ranges.
5165         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5166                                   (caar read)))
5167                   1)
5168           (setq first (car active)))
5169         (while read
5170           (when first
5171             (while (< first nlast)
5172               (push first unread)
5173               (setq first (1+ first))))
5174           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5175           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5176           (setq read (cdr read)))))
5177     ;; And add the last unread articles.
5178     (while (<= first last)
5179       (push first unread)
5180       (setq first (1+ first)))
5181     ;; Return the list of unread articles.
5182     (delq 0 (nreverse unread))))
5183
5184 (defun gnus-list-of-read-articles (group)
5185   "Return a list of unread, unticked and non-dormant articles."
5186   (let* ((info (gnus-get-info group))
5187          (marked (gnus-info-marks info))
5188          (active (gnus-active group)))
5189     (and info active
5190          (gnus-set-difference
5191           (gnus-sorted-complement
5192            (gnus-uncompress-range active)
5193            (gnus-list-of-unread-articles group))
5194           (append
5195            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5196            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5197
5198 ;; Various summary commands
5199
5200 (defun gnus-summary-select-article-buffer ()
5201   "Reconfigure windows to show article buffer."
5202   (interactive)
5203   (if (not (gnus-buffer-live-p gnus-article-buffer))
5204       (error "There is no article buffer for this summary buffer")
5205     (gnus-configure-windows 'article)
5206     (select-window (get-buffer-window gnus-article-buffer))))
5207
5208 (defun gnus-summary-universal-argument (arg)
5209   "Perform any operation on all articles that are process/prefixed."
5210   (interactive "P")
5211   (let ((articles (gnus-summary-work-articles arg))
5212         func article)
5213     (if (eq
5214          (setq
5215           func
5216           (key-binding
5217            (read-key-sequence
5218             (substitute-command-keys
5219              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5220              ))))
5221          'undefined)
5222         (gnus-error 1 "Undefined key")
5223       (save-excursion
5224         (while articles
5225           (gnus-summary-goto-subject (setq article (pop articles)))
5226           (let (gnus-newsgroup-processable)
5227             (command-execute func))
5228           (gnus-summary-remove-process-mark article)))))
5229   (gnus-summary-position-point))
5230
5231 (defun gnus-summary-toggle-truncation (&optional arg)
5232   "Toggle truncation of summary lines.
5233 With arg, turn line truncation on iff arg is positive."
5234   (interactive "P")
5235   (setq truncate-lines
5236         (if (null arg) (not truncate-lines)
5237           (> (prefix-numeric-value arg) 0)))
5238   (redraw-display))
5239
5240 (defun gnus-summary-reselect-current-group (&optional all rescan)
5241   "Exit and then reselect the current newsgroup.
5242 The prefix argument ALL means to select all articles."
5243   (interactive "P")
5244   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5245     (error "Ephemeral groups can't be reselected"))
5246   (let ((current-subject (gnus-summary-article-number))
5247         (group gnus-newsgroup-name))
5248     (setq gnus-newsgroup-begin nil)
5249     (gnus-summary-exit)
5250     ;; We have to adjust the point of group mode buffer because
5251     ;; point was moved to the next unread newsgroup by exiting.
5252     (gnus-summary-jump-to-group group)
5253     (when rescan
5254       (save-excursion
5255         (gnus-group-get-new-news-this-group 1)))
5256     (gnus-group-read-group all t)
5257     (gnus-summary-goto-subject current-subject nil t)))
5258
5259 (defun gnus-summary-rescan-group (&optional all)
5260   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5261   (interactive "P")
5262   (gnus-summary-reselect-current-group all t))
5263
5264 (defun gnus-summary-update-info (&optional non-destructive)
5265   (save-excursion
5266     (let ((group gnus-newsgroup-name))
5267       (when group
5268         (when gnus-newsgroup-kill-headers
5269           (setq gnus-newsgroup-killed
5270                 (gnus-compress-sequence
5271                  (nconc
5272                   (gnus-set-sorted-intersection
5273                    (gnus-uncompress-range gnus-newsgroup-killed)
5274                    (setq gnus-newsgroup-unselected
5275                          (sort gnus-newsgroup-unselected '<)))
5276                   (setq gnus-newsgroup-unreads
5277                         (sort gnus-newsgroup-unreads '<)))
5278                  t)))
5279         (unless (listp (cdr gnus-newsgroup-killed))
5280           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5281         (let ((headers gnus-newsgroup-headers))
5282           ;; Set the new ranges of read articles.
5283           (save-excursion
5284             (set-buffer gnus-group-buffer)
5285             (gnus-undo-force-boundary))
5286           (gnus-update-read-articles
5287            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5288           ;; Set the current article marks.
5289           (let ((gnus-newsgroup-scored
5290                  (if (and (not gnus-save-score)
5291                           (not non-destructive))
5292                      nil
5293                    gnus-newsgroup-scored)))
5294             (save-excursion
5295               (gnus-update-marks)))
5296           ;; Do the cross-ref thing.
5297           (when gnus-use-cross-reference
5298             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5299           ;; Do not switch windows but change the buffer to work.
5300           (set-buffer gnus-group-buffer)
5301           (unless (gnus-ephemeral-group-p group)
5302             (gnus-group-update-group group)))))))
5303
5304 (defun gnus-summary-save-newsrc (&optional force)
5305   "Save the current number of read/marked articles in the dribble buffer.
5306 The dribble buffer will then be saved.
5307 If FORCE (the prefix), also save the .newsrc file(s)."
5308   (interactive "P")
5309   (gnus-summary-update-info t)
5310   (if force
5311       (gnus-save-newsrc-file)
5312     (gnus-dribble-save)))
5313
5314 (defun gnus-summary-exit (&optional temporary)
5315   "Exit reading current newsgroup, and then return to group selection mode.
5316 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5317   (interactive)
5318   (gnus-set-global-variables)
5319   (when (gnus-buffer-live-p gnus-article-buffer)
5320     (save-excursion
5321       (set-buffer gnus-article-buffer)
5322       (mm-destroy-parts gnus-article-mime-handles)))
5323   (gnus-kill-save-kill-buffer)
5324   (gnus-async-halt-prefetch)
5325   (let* ((group gnus-newsgroup-name)
5326          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5327          (mode major-mode)
5328          (group-point nil)
5329          (buf (current-buffer)))
5330     (unless quit-config
5331       ;; Do adaptive scoring, and possibly save score files.
5332       (when gnus-newsgroup-adaptive
5333         (gnus-score-adaptive))
5334       (when gnus-use-scoring
5335         (gnus-score-save)))
5336     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5337     ;; If we have several article buffers, we kill them at exit.
5338     (unless gnus-single-article-buffer
5339       (gnus-kill-buffer gnus-original-article-buffer)
5340       (setq gnus-article-current nil))
5341     (when gnus-use-cache
5342       (gnus-cache-possibly-remove-articles)
5343       (gnus-cache-save-buffers))
5344     (gnus-async-prefetch-remove-group group)
5345     (when gnus-suppress-duplicates
5346       (gnus-dup-enter-articles))
5347     (when gnus-use-trees
5348       (gnus-tree-close group))
5349     ;; Remove entries for this group.
5350     (nnmail-purge-split-history (gnus-group-real-name group))
5351     ;; Make all changes in this group permanent.
5352     (unless quit-config
5353       (gnus-run-hooks 'gnus-exit-group-hook)
5354       (gnus-summary-update-info))
5355     (gnus-close-group group)
5356     ;; Make sure where we were, and go to next newsgroup.
5357     (set-buffer gnus-group-buffer)
5358     (unless quit-config
5359       (gnus-group-jump-to-group group))
5360     (gnus-run-hooks 'gnus-summary-exit-hook)
5361     (unless (or quit-config
5362                 ;; If this group has disappeared from the summary
5363                 ;; buffer, don't skip forwards.
5364                 (not (string= group (gnus-group-group-name))))
5365       (gnus-group-next-unread-group 1))
5366     (setq group-point (point))
5367     (if temporary
5368         nil                             ;Nothing to do.
5369       ;; If we have several article buffers, we kill them at exit.
5370       (unless gnus-single-article-buffer
5371         (gnus-kill-buffer gnus-article-buffer)
5372         (gnus-kill-buffer gnus-original-article-buffer)
5373         (setq gnus-article-current nil))
5374       (set-buffer buf)
5375       (if (not gnus-kill-summary-on-exit)
5376           (gnus-deaden-summary)
5377         ;; We set all buffer-local variables to nil.  It is unclear why
5378         ;; this is needed, but if we don't, buffer-local variables are
5379         ;; not garbage-collected, it seems.  This would the lead to en
5380         ;; ever-growing Emacs.
5381         (gnus-summary-clear-local-variables)
5382         (when (get-buffer gnus-article-buffer)
5383           (bury-buffer gnus-article-buffer))
5384         ;; We clear the global counterparts of the buffer-local
5385         ;; variables as well, just to be on the safe side.
5386         (set-buffer gnus-group-buffer)
5387         (gnus-summary-clear-local-variables)
5388         ;; Return to group mode buffer.
5389         (when (eq mode 'gnus-summary-mode)
5390           (gnus-kill-buffer buf)))
5391       (setq gnus-current-select-method gnus-select-method)
5392       (pop-to-buffer gnus-group-buffer)
5393       (if (not quit-config)
5394           (progn
5395             (goto-char group-point)
5396             (gnus-configure-windows 'group 'force))
5397         (gnus-handle-ephemeral-exit quit-config))
5398       ;; Clear the current group name.
5399       (unless quit-config
5400         (setq gnus-newsgroup-name nil)))))
5401
5402 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5403 (defun gnus-summary-exit-no-update (&optional no-questions)
5404   "Quit reading current newsgroup without updating read article info."
5405   (interactive)
5406   (let* ((group gnus-newsgroup-name)
5407          (quit-config (gnus-group-quit-config group)))
5408     (when (or no-questions
5409               gnus-expert-user
5410               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5411       (gnus-async-halt-prefetch)
5412       (mapcar 'funcall
5413               (delq 'gnus-summary-expire-articles
5414                     (copy-list gnus-summary-prepare-exit-hook)))
5415       (when (gnus-buffer-live-p gnus-article-buffer)
5416         (save-excursion
5417           (set-buffer gnus-article-buffer)
5418           (mm-destroy-parts gnus-article-mime-handles)))
5419       ;; If we have several article buffers, we kill them at exit.
5420       (unless gnus-single-article-buffer
5421         (gnus-kill-buffer gnus-article-buffer)
5422         (gnus-kill-buffer gnus-original-article-buffer)
5423         (setq gnus-article-current nil))
5424       (if (not gnus-kill-summary-on-exit)
5425           (gnus-deaden-summary)
5426         (gnus-close-group group)
5427         (gnus-summary-clear-local-variables)
5428         (set-buffer gnus-group-buffer)
5429         (gnus-summary-clear-local-variables)
5430         (when (get-buffer gnus-summary-buffer)
5431           (kill-buffer gnus-summary-buffer)))
5432       (unless gnus-single-article-buffer
5433         (setq gnus-article-current nil))
5434       (when gnus-use-trees
5435         (gnus-tree-close group))
5436       (gnus-async-prefetch-remove-group group)
5437       (when (get-buffer gnus-article-buffer)
5438         (bury-buffer gnus-article-buffer))
5439       ;; Return to the group buffer.
5440       (gnus-configure-windows 'group 'force)
5441       ;; Clear the current group name.
5442       (setq gnus-newsgroup-name nil)
5443       (when (equal (gnus-group-group-name) group)
5444         (gnus-group-next-unread-group 1))
5445       (when quit-config
5446         (gnus-handle-ephemeral-exit quit-config)))))
5447
5448 (defun gnus-handle-ephemeral-exit (quit-config)
5449   "Handle movement when leaving an ephemeral group.
5450 The state which existed when entering the ephemeral is reset."
5451   (if (not (buffer-name (car quit-config)))
5452       (gnus-configure-windows 'group 'force)
5453     (set-buffer (car quit-config))
5454     (cond ((eq major-mode 'gnus-summary-mode)
5455            (gnus-set-global-variables))
5456           ((eq major-mode 'gnus-article-mode)
5457            (save-excursion
5458              ;; The `gnus-summary-buffer' variable may point
5459              ;; to the old summary buffer when using a single
5460              ;; article buffer.
5461              (unless (gnus-buffer-live-p gnus-summary-buffer)
5462                (set-buffer gnus-group-buffer))
5463              (set-buffer gnus-summary-buffer)
5464              (gnus-set-global-variables))))
5465     (if (or (eq (cdr quit-config) 'article)
5466             (eq (cdr quit-config) 'pick))
5467         (progn
5468           ;; The current article may be from the ephemeral group
5469           ;; thus it is best that we reload this article
5470           (gnus-summary-show-article)
5471           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5472               (gnus-configure-windows 'pick 'force)
5473             (gnus-configure-windows (cdr quit-config) 'force)))
5474       (gnus-configure-windows (cdr quit-config) 'force))
5475     (when (eq major-mode 'gnus-summary-mode)
5476       (gnus-summary-next-subject 1 nil t)
5477       (gnus-summary-recenter)
5478       (gnus-summary-position-point))))
5479
5480 ;;; Dead summaries.
5481
5482 (defvar gnus-dead-summary-mode-map nil)
5483
5484 (unless gnus-dead-summary-mode-map
5485   (setq gnus-dead-summary-mode-map (make-keymap))
5486   (suppress-keymap gnus-dead-summary-mode-map)
5487   (substitute-key-definition
5488    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5489   (let ((keys '("\C-d" "\r" "\177" [delete])))
5490     (while keys
5491       (define-key gnus-dead-summary-mode-map
5492         (pop keys) 'gnus-summary-wake-up-the-dead))))
5493
5494 (defvar gnus-dead-summary-mode nil
5495   "Minor mode for Gnus summary buffers.")
5496
5497 (defun gnus-dead-summary-mode (&optional arg)
5498   "Minor mode for Gnus summary buffers."
5499   (interactive "P")
5500   (when (eq major-mode 'gnus-summary-mode)
5501     (make-local-variable 'gnus-dead-summary-mode)
5502     (setq gnus-dead-summary-mode
5503           (if (null arg) (not gnus-dead-summary-mode)
5504             (> (prefix-numeric-value arg) 0)))
5505     (when gnus-dead-summary-mode
5506       (gnus-add-minor-mode
5507        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5508
5509 (defun gnus-deaden-summary ()
5510   "Make the current summary buffer into a dead summary buffer."
5511   ;; Kill any previous dead summary buffer.
5512   (when (and gnus-dead-summary
5513              (buffer-name gnus-dead-summary))
5514     (save-excursion
5515       (set-buffer gnus-dead-summary)
5516       (when gnus-dead-summary-mode
5517         (kill-buffer (current-buffer)))))
5518   ;; Make this the current dead summary.
5519   (setq gnus-dead-summary (current-buffer))
5520   (gnus-dead-summary-mode 1)
5521   (let ((name (buffer-name)))
5522     (when (string-match "Summary" name)
5523       (rename-buffer
5524        (concat (substring name 0 (match-beginning 0)) "Dead "
5525                (substring name (match-beginning 0)))
5526        t))))
5527
5528 (defun gnus-kill-or-deaden-summary (buffer)
5529   "Kill or deaden the summary BUFFER."
5530   (save-excursion
5531     (when (and (buffer-name buffer)
5532                (not gnus-single-article-buffer))
5533       (save-excursion
5534         (set-buffer buffer)
5535         (gnus-kill-buffer gnus-article-buffer)
5536         (gnus-kill-buffer gnus-original-article-buffer)))
5537     (cond (gnus-kill-summary-on-exit
5538            (when (and gnus-use-trees
5539                       (gnus-buffer-exists-p buffer))
5540              (save-excursion
5541                (set-buffer buffer)
5542                (gnus-tree-close gnus-newsgroup-name)))
5543            (gnus-kill-buffer buffer))
5544           ((gnus-buffer-exists-p buffer)
5545            (save-excursion
5546              (set-buffer buffer)
5547              (gnus-deaden-summary))))))
5548
5549 (defun gnus-summary-wake-up-the-dead (&rest args)
5550   "Wake up the dead summary buffer."
5551   (interactive)
5552   (gnus-dead-summary-mode -1)
5553   (let ((name (buffer-name)))
5554     (when (string-match "Dead " name)
5555       (rename-buffer
5556        (concat (substring name 0 (match-beginning 0))
5557                (substring name (match-end 0)))
5558        t)))
5559   (gnus-message 3 "This dead summary is now alive again"))
5560
5561 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5562 (defun gnus-summary-fetch-faq (&optional faq-dir)
5563   "Fetch the FAQ for the current group.
5564 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5565 in."
5566   (interactive
5567    (list
5568     (when current-prefix-arg
5569       (completing-read
5570        "Faq dir: " (and (listp gnus-group-faq-directory)
5571                         (mapcar (lambda (file) (list file))
5572                                 gnus-group-faq-directory))))))
5573   (let (gnus-faq-buffer)
5574     (when (setq gnus-faq-buffer
5575                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5576       (gnus-configure-windows 'summary-faq))))
5577
5578 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5579 (defun gnus-summary-describe-group (&optional force)
5580   "Describe the current newsgroup."
5581   (interactive "P")
5582   (gnus-group-describe-group force gnus-newsgroup-name))
5583
5584 (defun gnus-summary-describe-briefly ()
5585   "Describe summary mode commands briefly."
5586   (interactive)
5587   (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")))
5588
5589 ;; Walking around group mode buffer from summary mode.
5590
5591 (defun gnus-summary-next-group (&optional no-article target-group backward)
5592   "Exit current newsgroup and then select next unread newsgroup.
5593 If prefix argument NO-ARTICLE is non-nil, no article is selected
5594 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5595 previous group instead."
5596   (interactive "P")
5597   ;; Stop pre-fetching.
5598   (gnus-async-halt-prefetch)
5599   (let ((current-group gnus-newsgroup-name)
5600         (current-buffer (current-buffer))
5601         entered)
5602     ;; First we semi-exit this group to update Xrefs and all variables.
5603     ;; We can't do a real exit, because the window conf must remain
5604     ;; the same in case the user is prompted for info, and we don't
5605     ;; want the window conf to change before that...
5606     (gnus-summary-exit t)
5607     (while (not entered)
5608       ;; Then we find what group we are supposed to enter.
5609       (set-buffer gnus-group-buffer)
5610       (gnus-group-jump-to-group current-group)
5611       (setq target-group
5612             (or target-group
5613                 (if (eq gnus-keep-same-level 'best)
5614                     (gnus-summary-best-group gnus-newsgroup-name)
5615                   (gnus-summary-search-group backward gnus-keep-same-level))))
5616       (if (not target-group)
5617           ;; There are no further groups, so we return to the group
5618           ;; buffer.
5619           (progn
5620             (gnus-message 5 "Returning to the group buffer")
5621             (setq entered t)
5622             (when (gnus-buffer-live-p current-buffer)
5623               (set-buffer current-buffer)
5624               (gnus-summary-exit))
5625             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5626         ;; We try to enter the target group.
5627         (gnus-group-jump-to-group target-group)
5628         (let ((unreads (gnus-group-group-unread)))
5629           (if (and (or (eq t unreads)
5630                        (and unreads (not (zerop unreads))))
5631                    (gnus-summary-read-group
5632                     target-group nil no-article
5633                     (and (buffer-name current-buffer) current-buffer)
5634                     nil backward))
5635               (setq entered t)
5636             (setq current-group target-group
5637                   target-group nil)))))))
5638
5639 (defun gnus-summary-prev-group (&optional no-article)
5640   "Exit current newsgroup and then select previous unread newsgroup.
5641 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5642   (interactive "P")
5643   (gnus-summary-next-group no-article nil t))
5644
5645 ;; Walking around summary lines.
5646
5647 (defun gnus-summary-first-subject (&optional unread undownloaded)
5648   "Go to the first unread subject.
5649 If UNREAD is non-nil, go to the first unread article.
5650 Returns the article selected or nil if there are no unread articles."
5651   (interactive "P")
5652   (prog1
5653       (cond
5654        ;; Empty summary.
5655        ((null gnus-newsgroup-data)
5656         (gnus-message 3 "No articles in the group")
5657         nil)
5658        ;; Pick the first article.
5659        ((not unread)
5660         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5661         (gnus-data-number (car gnus-newsgroup-data)))
5662        ;; No unread articles.
5663        ((null gnus-newsgroup-unreads)
5664         (gnus-message 3 "No more unread articles")
5665         nil)
5666        ;; Find the first unread article.
5667        (t
5668         (let ((data gnus-newsgroup-data))
5669           (while (and data
5670                       (and (not (and undownloaded
5671                                      (eq gnus-undownloaded-mark
5672                                          (gnus-data-mark (car data)))))
5673                            (not (gnus-data-unread-p (car data)))))
5674             (setq data (cdr data)))
5675           (when data
5676             (goto-char (gnus-data-pos (car data)))
5677             (gnus-data-number (car data))))))
5678     (gnus-summary-position-point)))
5679
5680 (defun gnus-summary-next-subject (n &optional unread dont-display)
5681   "Go to next N'th summary line.
5682 If N is negative, go to the previous N'th subject line.
5683 If UNREAD is non-nil, only unread articles are selected.
5684 The difference between N and the actual number of steps taken is
5685 returned."
5686   (interactive "p")
5687   (let ((backward (< n 0))
5688         (n (abs n)))
5689     (while (and (> n 0)
5690                 (if backward
5691                     (gnus-summary-find-prev unread)
5692                   (gnus-summary-find-next unread)))
5693       (gnus-summary-show-thread)
5694       (setq n (1- n)))
5695     (when (/= 0 n)
5696       (gnus-message 7 "No more%s articles"
5697                     (if unread " unread" "")))
5698     (unless dont-display
5699       (gnus-summary-recenter)
5700       (gnus-summary-position-point))
5701     n))
5702
5703 (defun gnus-summary-next-unread-subject (n)
5704   "Go to next N'th unread summary line."
5705   (interactive "p")
5706   (gnus-summary-next-subject n t))
5707
5708 (defun gnus-summary-prev-subject (n &optional unread)
5709   "Go to previous N'th summary line.
5710 If optional argument UNREAD is non-nil, only unread article is selected."
5711   (interactive "p")
5712   (gnus-summary-next-subject (- n) unread))
5713
5714 (defun gnus-summary-prev-unread-subject (n)
5715   "Go to previous N'th unread summary line."
5716   (interactive "p")
5717   (gnus-summary-next-subject (- n) t))
5718
5719 (defun gnus-summary-goto-subject (article &optional force silent)
5720   "Go the subject line of ARTICLE.
5721 If FORCE, also allow jumping to articles not currently shown."
5722   (interactive "nArticle number: ")
5723   (let ((b (point))
5724         (data (gnus-data-find article)))
5725     ;; We read in the article if we have to.
5726     (and (not data)
5727          force
5728          (gnus-summary-insert-subject
5729           article
5730           (if (or (numberp force) (vectorp force)) force)
5731           t)
5732          (setq data (gnus-data-find article)))
5733     (goto-char b)
5734     (if (not data)
5735         (progn
5736           (unless silent
5737             (gnus-message 3 "Can't find article %d" article))
5738           nil)
5739       (goto-char (gnus-data-pos data))
5740       (gnus-summary-position-point)
5741       article)))
5742
5743 ;; Walking around summary lines with displaying articles.
5744
5745 (defun gnus-summary-expand-window (&optional arg)
5746   "Make the summary buffer take up the entire Emacs frame.
5747 Given a prefix, will force an `article' buffer configuration."
5748   (interactive "P")
5749   (if arg
5750       (gnus-configure-windows 'article 'force)
5751     (gnus-configure-windows 'summary 'force)))
5752
5753 (defun gnus-summary-display-article (article &optional all-header)
5754   "Display ARTICLE in article buffer."
5755   (gnus-set-global-variables)
5756   (if (null article)
5757       nil
5758     (prog1
5759         (if gnus-summary-display-article-function
5760             (funcall gnus-summary-display-article-function article all-header)
5761           (gnus-article-prepare article all-header))
5762       (gnus-run-hooks 'gnus-select-article-hook)
5763       (when (and gnus-current-article
5764                  (not (zerop gnus-current-article)))
5765         (gnus-summary-goto-subject gnus-current-article))
5766       (gnus-summary-recenter)
5767       (when (and gnus-use-trees gnus-show-threads)
5768         (gnus-possibly-generate-tree article)
5769         (gnus-highlight-selected-tree article))
5770       ;; Successfully display article.
5771       (gnus-article-set-window-start
5772        (cdr (assq article gnus-newsgroup-bookmarks))))))
5773
5774 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5775   "Select the current article.
5776 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5777 non-nil, the article will be re-fetched even if it already present in
5778 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5779 be displayed."
5780   ;; Make sure we are in the summary buffer to work around bbdb bug.
5781   (unless (eq major-mode 'gnus-summary-mode)
5782     (set-buffer gnus-summary-buffer))
5783   (let ((article (or article (gnus-summary-article-number)))
5784         (all-headers (not (not all-headers))) ;Must be T or NIL.
5785         gnus-summary-display-article-function
5786         did)
5787     (and (not pseudo)
5788          (gnus-summary-article-pseudo-p article)
5789          (error "This is a pseudo-article"))
5790     (prog1
5791         (save-excursion
5792           (set-buffer gnus-summary-buffer)
5793           (if (or (and gnus-single-article-buffer
5794                        (or (null gnus-current-article)
5795                            (null gnus-article-current)
5796                            (null (get-buffer gnus-article-buffer))
5797                            (not (eq article (cdr gnus-article-current)))
5798                            (not (equal (car gnus-article-current)
5799                                        gnus-newsgroup-name))))
5800                   (and (not gnus-single-article-buffer)
5801                        (or (null gnus-current-article)
5802                            (not (eq gnus-current-article article))))
5803                   force)
5804               ;; The requested article is different from the current article.
5805               (prog1
5806                   (gnus-summary-display-article article all-headers)
5807                 (setq did article)
5808                 (when (or all-headers gnus-show-all-headers)
5809                   (gnus-article-show-all-headers)))
5810             (when (or all-headers gnus-show-all-headers)
5811               (gnus-article-show-all-headers))
5812             'old))
5813       (when did
5814         (gnus-article-set-window-start
5815          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5816
5817 (defun gnus-summary-set-current-mark (&optional current-mark)
5818   "Obsolete function."
5819   nil)
5820
5821 (defun gnus-summary-next-article (&optional unread subject backward push)
5822   "Select the next article.
5823 If UNREAD, only unread articles are selected.
5824 If SUBJECT, only articles with SUBJECT are selected.
5825 If BACKWARD, the previous article is selected instead of the next."
5826   (interactive "P")
5827   (cond
5828    ;; Is there such an article?
5829    ((and (gnus-summary-search-forward unread subject backward)
5830          (or (gnus-summary-display-article (gnus-summary-article-number))
5831              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5832     (gnus-summary-position-point))
5833    ;; If not, we try the first unread, if that is wanted.
5834    ((and subject
5835          gnus-auto-select-same
5836          (gnus-summary-first-unread-article))
5837     (gnus-summary-position-point)
5838     (gnus-message 6 "Wrapped"))
5839    ;; Try to get next/previous article not displayed in this group.
5840    ((and gnus-auto-extend-newsgroup
5841          (not unread) (not subject))
5842     (gnus-summary-goto-article
5843      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5844      nil (count-lines (point-min) (point))))
5845    ;; Go to next/previous group.
5846    (t
5847     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5848       (gnus-summary-jump-to-group gnus-newsgroup-name))
5849     (let ((cmd last-command-char)
5850           (point
5851            (save-excursion
5852              (set-buffer gnus-group-buffer)
5853              (point)))
5854           (group
5855            (if (eq gnus-keep-same-level 'best)
5856                (gnus-summary-best-group gnus-newsgroup-name)
5857              (gnus-summary-search-group backward gnus-keep-same-level))))
5858       ;; For some reason, the group window gets selected.  We change
5859       ;; it back.
5860       (select-window (get-buffer-window (current-buffer)))
5861       ;; Select next unread newsgroup automagically.
5862       (cond
5863        ((or (not gnus-auto-select-next)
5864             (not cmd))
5865         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5866        ((or (eq gnus-auto-select-next 'quietly)
5867             (and (eq gnus-auto-select-next 'slightly-quietly)
5868                  push)
5869             (and (eq gnus-auto-select-next 'almost-quietly)
5870                  (gnus-summary-last-article-p)))
5871         ;; Select quietly.
5872         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5873             (gnus-summary-exit)
5874           (gnus-message 7 "No more%s articles (%s)..."
5875                         (if unread " unread" "")
5876                         (if group (concat "selecting " group)
5877                           "exiting"))
5878           (gnus-summary-next-group nil group backward)))
5879        (t
5880         (when (gnus-key-press-event-p last-input-event)
5881           (gnus-summary-walk-group-buffer
5882            gnus-newsgroup-name cmd unread backward point))))))))
5883
5884 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5885   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5886                       (?\C-p (gnus-group-prev-unread-group 1))))
5887         (cursor-in-echo-area t)
5888         keve key group ended)
5889     (save-excursion
5890       (set-buffer gnus-group-buffer)
5891       (goto-char start)
5892       (setq group
5893             (if (eq gnus-keep-same-level 'best)
5894                 (gnus-summary-best-group gnus-newsgroup-name)
5895               (gnus-summary-search-group backward gnus-keep-same-level))))
5896     (while (not ended)
5897       (gnus-message
5898        5 "No more%s articles%s" (if unread " unread" "")
5899        (if (and group
5900                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5901            (format " (Type %s for %s [%s])"
5902                    (single-key-description cmd) group
5903                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5904          (format " (Type %s to exit %s)"
5905                  (single-key-description cmd)
5906                  gnus-newsgroup-name)))
5907       ;; Confirm auto selection.
5908       (setq key (car (setq keve (gnus-read-event-char))))
5909       (setq ended t)
5910       (cond
5911        ((assq key keystrokes)
5912         (let ((obuf (current-buffer)))
5913           (switch-to-buffer gnus-group-buffer)
5914           (when group
5915             (gnus-group-jump-to-group group))
5916           (eval (cadr (assq key keystrokes)))
5917           (setq group (gnus-group-group-name))
5918           (switch-to-buffer obuf))
5919         (setq ended nil))
5920        ((equal key cmd)
5921         (if (or (not group)
5922                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5923             (gnus-summary-exit)
5924           (gnus-summary-next-group nil group backward)))
5925        (t
5926         (push (cdr keve) unread-command-events))))))
5927
5928 (defun gnus-summary-next-unread-article ()
5929   "Select unread article after current one."
5930   (interactive)
5931   (gnus-summary-next-article
5932    (or (not (eq gnus-summary-goto-unread 'never))
5933        (gnus-summary-last-article-p (gnus-summary-article-number)))
5934    (and gnus-auto-select-same
5935         (gnus-summary-article-subject))))
5936
5937 (defun gnus-summary-prev-article (&optional unread subject)
5938   "Select the article after the current one.
5939 If UNREAD is non-nil, only unread articles are selected."
5940   (interactive "P")
5941   (gnus-summary-next-article unread subject t))
5942
5943 (defun gnus-summary-prev-unread-article ()
5944   "Select unread article before current one."
5945   (interactive)
5946   (gnus-summary-prev-article
5947    (or (not (eq gnus-summary-goto-unread 'never))
5948        (gnus-summary-first-article-p (gnus-summary-article-number)))
5949    (and gnus-auto-select-same
5950         (gnus-summary-article-subject))))
5951
5952 (defun gnus-summary-next-page (&optional lines circular)
5953   "Show next page of the selected article.
5954 If at the end of the current article, select the next article.
5955 LINES says how many lines should be scrolled up.
5956
5957 If CIRCULAR is non-nil, go to the start of the article instead of
5958 selecting the next article when reaching the end of the current
5959 article."
5960   (interactive "P")
5961   (setq gnus-summary-buffer (current-buffer))
5962   (gnus-set-global-variables)
5963   (let ((article (gnus-summary-article-number))
5964         (article-window (get-buffer-window gnus-article-buffer t))
5965         endp)
5966     ;; If the buffer is empty, we have no article.
5967     (unless article
5968       (error "No article to select"))
5969     (gnus-configure-windows 'article)
5970     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5971         (if (and (eq gnus-summary-goto-unread 'never)
5972                  (not (gnus-summary-last-article-p article)))
5973             (gnus-summary-next-article)
5974           (gnus-summary-next-unread-article))
5975       (if (or (null gnus-current-article)
5976               (null gnus-article-current)
5977               (/= article (cdr gnus-article-current))
5978               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5979           ;; Selected subject is different from current article's.
5980           (gnus-summary-display-article article)
5981         (when article-window
5982           (gnus-eval-in-buffer-window gnus-article-buffer
5983             (setq endp (gnus-article-next-page lines)))
5984           (when endp
5985             (cond (circular
5986                    (gnus-summary-beginning-of-article))
5987                   (lines
5988                    (gnus-message 3 "End of message"))
5989                   ((null lines)
5990                    (if (and (eq gnus-summary-goto-unread 'never)
5991                             (not (gnus-summary-last-article-p article)))
5992                        (gnus-summary-next-article)
5993                      (gnus-summary-next-unread-article))))))))
5994     (gnus-summary-recenter)
5995     (gnus-summary-position-point)))
5996
5997 (defun gnus-summary-prev-page (&optional lines move)
5998   "Show previous page of selected article.
5999 Argument LINES specifies lines to be scrolled down.
6000 If MOVE, move to the previous unread article if point is at
6001 the beginning of the buffer."
6002   (interactive "P")
6003   (let ((article (gnus-summary-article-number))
6004         (article-window (get-buffer-window gnus-article-buffer t))
6005         endp)
6006     (gnus-configure-windows 'article)
6007     (if (or (null gnus-current-article)
6008             (null gnus-article-current)
6009             (/= article (cdr gnus-article-current))
6010             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6011         ;; Selected subject is different from current article's.
6012         (gnus-summary-display-article article)
6013       (gnus-summary-recenter)
6014       (when article-window
6015         (gnus-eval-in-buffer-window gnus-article-buffer
6016           (setq endp (gnus-article-prev-page lines)))
6017         (when (and move endp)
6018           (cond (lines
6019                  (gnus-message 3 "Beginning of message"))
6020                 ((null lines)
6021                  (if (and (eq gnus-summary-goto-unread 'never)
6022                           (not (gnus-summary-first-article-p article)))
6023                      (gnus-summary-prev-article)
6024                    (gnus-summary-prev-unread-article))))))))
6025   (gnus-summary-position-point))
6026
6027 (defun gnus-summary-prev-page-or-article (&optional lines)
6028   "Show previous page of selected article.
6029 Argument LINES specifies lines to be scrolled down.
6030 If at the beginning of the article, go to the next article."
6031   (interactive "P")
6032   (gnus-summary-prev-page lines t))
6033
6034 (defun gnus-summary-scroll-up (lines)
6035   "Scroll up (or down) one line current article.
6036 Argument LINES specifies lines to be scrolled up (or down if negative)."
6037   (interactive "p")
6038   (gnus-configure-windows 'article)
6039   (gnus-summary-show-thread)
6040   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6041     (gnus-eval-in-buffer-window gnus-article-buffer
6042       (cond ((> lines 0)
6043              (when (gnus-article-next-page lines)
6044                (gnus-message 3 "End of message")))
6045             ((< lines 0)
6046              (gnus-article-prev-page (- lines))))))
6047   (gnus-summary-recenter)
6048   (gnus-summary-position-point))
6049
6050 (defun gnus-summary-scroll-down (lines)
6051   "Scroll down (or up) one line current article.
6052 Argument LINES specifies lines to be scrolled down (or up if negative)."
6053   (interactive "p")
6054   (gnus-summary-scroll-up (- lines)))
6055
6056 (defun gnus-summary-next-same-subject ()
6057   "Select next article which has the same subject as current one."
6058   (interactive)
6059   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6060
6061 (defun gnus-summary-prev-same-subject ()
6062   "Select previous article which has the same subject as current one."
6063   (interactive)
6064   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6065
6066 (defun gnus-summary-next-unread-same-subject ()
6067   "Select next unread article which has the same subject as current one."
6068   (interactive)
6069   (gnus-summary-next-article t (gnus-summary-article-subject)))
6070
6071 (defun gnus-summary-prev-unread-same-subject ()
6072   "Select previous unread article which has the same subject as current one."
6073   (interactive)
6074   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6075
6076 (defun gnus-summary-first-unread-article ()
6077   "Select the first unread article.
6078 Return nil if there are no unread articles."
6079   (interactive)
6080   (prog1
6081       (when (gnus-summary-first-subject t)
6082         (gnus-summary-show-thread)
6083         (gnus-summary-first-subject t)
6084         (gnus-summary-display-article (gnus-summary-article-number)))
6085     (gnus-summary-position-point)))
6086
6087 (defun gnus-summary-first-unread-subject ()
6088   "Place the point on the subject line of the first unread article.
6089 Return nil if there are no unread articles."
6090   (interactive)
6091   (prog1
6092       (when (gnus-summary-first-subject t)
6093         (gnus-summary-show-thread)
6094         (gnus-summary-first-subject t))
6095     (gnus-summary-position-point)))
6096
6097 (defun gnus-summary-first-article ()
6098   "Select the first article.
6099 Return nil if there are no articles."
6100   (interactive)
6101   (prog1
6102       (when (gnus-summary-first-subject)
6103         (gnus-summary-show-thread)
6104         (gnus-summary-first-subject)
6105         (gnus-summary-display-article (gnus-summary-article-number)))
6106     (gnus-summary-position-point)))
6107
6108 (defun gnus-summary-best-unread-article ()
6109   "Select the unread article with the highest score."
6110   (interactive)
6111   (let ((best -1000000)
6112         (data gnus-newsgroup-data)
6113         article score)
6114     (while data
6115       (and (gnus-data-unread-p (car data))
6116            (> (setq score
6117                     (gnus-summary-article-score (gnus-data-number (car data))))
6118               best)
6119            (setq best score
6120                  article (gnus-data-number (car data))))
6121       (setq data (cdr data)))
6122     (prog1
6123         (if article
6124             (gnus-summary-goto-article article)
6125           (error "No unread articles"))
6126       (gnus-summary-position-point))))
6127
6128 (defun gnus-summary-last-subject ()
6129   "Go to the last displayed subject line in the group."
6130   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6131     (when article
6132       (gnus-summary-goto-subject article))))
6133
6134 (defun gnus-summary-goto-article (article &optional all-headers force)
6135   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6136 If ALL-HEADERS is non-nil, no header lines are hidden.
6137 If FORCE, go to the article even if it isn't displayed.  If FORCE
6138 is a number, it is the line the article is to be displayed on."
6139   (interactive
6140    (list
6141     (completing-read
6142      "Article number or Message-ID: "
6143      (mapcar (lambda (number) (list (int-to-string number)))
6144              gnus-newsgroup-limit))
6145     current-prefix-arg
6146     t))
6147   (prog1
6148       (if (and (stringp article)
6149                (string-match "@" article))
6150           (gnus-summary-refer-article article)
6151         (when (stringp article)
6152           (setq article (string-to-number article)))
6153         (if (gnus-summary-goto-subject article force)
6154             (gnus-summary-display-article article all-headers)
6155           (gnus-message 4 "Couldn't go to article %s" article) nil))
6156     (gnus-summary-position-point)))
6157
6158 (defun gnus-summary-goto-last-article ()
6159   "Go to the previously read article."
6160   (interactive)
6161   (prog1
6162       (when gnus-last-article
6163         (gnus-summary-goto-article gnus-last-article nil t))
6164     (gnus-summary-position-point)))
6165
6166 (defun gnus-summary-pop-article (number)
6167   "Pop one article off the history and go to the previous.
6168 NUMBER articles will be popped off."
6169   (interactive "p")
6170   (let (to)
6171     (setq gnus-newsgroup-history
6172           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6173     (if to
6174         (gnus-summary-goto-article (car to) nil t)
6175       (error "Article history empty")))
6176   (gnus-summary-position-point))
6177
6178 ;; Summary commands and functions for limiting the summary buffer.
6179
6180 (defun gnus-summary-limit-to-articles (n)
6181   "Limit the summary buffer to the next N articles.
6182 If not given a prefix, use the process marked articles instead."
6183   (interactive "P")
6184   (prog1
6185       (let ((articles (gnus-summary-work-articles n)))
6186         (setq gnus-newsgroup-processable nil)
6187         (gnus-summary-limit articles))
6188     (gnus-summary-position-point)))
6189
6190 (defun gnus-summary-pop-limit (&optional total)
6191   "Restore the previous limit.
6192 If given a prefix, remove all limits."
6193   (interactive "P")
6194   (when total
6195     (setq gnus-newsgroup-limits
6196           (list (mapcar (lambda (h) (mail-header-number h))
6197                         gnus-newsgroup-headers))))
6198   (unless gnus-newsgroup-limits
6199     (error "No limit to pop"))
6200   (prog1
6201       (gnus-summary-limit nil 'pop)
6202     (gnus-summary-position-point)))
6203
6204 (defun gnus-summary-limit-to-subject (subject &optional header)
6205   "Limit the summary buffer to articles that have subjects that match a regexp."
6206   (interactive "sLimit to subject (regexp): ")
6207   (unless header
6208     (setq header "subject"))
6209   (when (not (equal "" subject))
6210     (prog1
6211         (let ((articles (gnus-summary-find-matching
6212                          (or header "subject") subject 'all)))
6213           (unless articles
6214             (error "Found no matches for \"%s\"" subject))
6215           (gnus-summary-limit articles))
6216       (gnus-summary-position-point))))
6217
6218 (defun gnus-summary-limit-to-author (from)
6219   "Limit the summary buffer to articles that have authors that match a regexp."
6220   (interactive "sLimit to author (regexp): ")
6221   (gnus-summary-limit-to-subject from "from"))
6222
6223 (defun gnus-summary-limit-to-age (age &optional younger-p)
6224   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6225 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6226 articles that are younger than AGE days."
6227   (interactive
6228    (let ((younger current-prefix-arg)
6229          (days-got nil)
6230          days)
6231      (while (not days-got)
6232        (setq days (if younger
6233                       (read-string "Limit to articles within (in days): ")
6234                     (read-string "Limit to articles old than (in days): ")))
6235        (when (> (length days) 0)
6236          (setq days (read days)))
6237        (if (numberp days)
6238            (setq days-got t)
6239          (message "Please enter a number.")
6240          (sleep-for 1)))
6241      (list days younger)))
6242   (prog1
6243       (let ((data gnus-newsgroup-data)
6244             (cutoff (days-to-time age))
6245             articles d date is-younger)
6246         (while (setq d (pop data))
6247           (when (and (vectorp (gnus-data-header d))
6248                      (setq date (mail-header-date (gnus-data-header d))))
6249             (setq is-younger (time-less-p
6250                               (time-since (condition-case ()
6251                                               (date-to-time date)
6252                                             (error '(0 0))))
6253                               cutoff))
6254             (when (if younger-p
6255                       is-younger
6256                     (not is-younger))
6257               (push (gnus-data-number d) articles))))
6258         (gnus-summary-limit (nreverse articles)))
6259     (gnus-summary-position-point)))
6260
6261 (defun gnus-summary-limit-to-extra (header regexp)
6262   "Limit the summary buffer to articles that match an 'extra' header."
6263   (interactive
6264    (let ((header
6265           (intern
6266            (gnus-completing-read
6267             (symbol-name (car gnus-extra-headers))      
6268             "Limit extra header:"       
6269             (mapcar (lambda (x) 
6270                       (cons (symbol-name x) x))
6271                     gnus-extra-headers)
6272             nil                 
6273             t))))
6274      (list header
6275            (read-string (format "Limit to header %s (regexp): " header)))))
6276   (when (not (equal "" regexp))
6277     (prog1
6278         (let ((articles (gnus-summary-find-matching
6279                          (cons 'extra header) regexp 'all)))
6280           (unless articles
6281             (error "Found no matches for \"%s\"" regexp))
6282           (gnus-summary-limit articles))
6283       (gnus-summary-position-point))))
6284
6285 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6286 (make-obsolete
6287  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6288
6289 (defun gnus-summary-limit-to-unread (&optional all)
6290   "Limit the summary buffer to articles that are not marked as read.
6291 If ALL is non-nil, limit strictly to unread articles."
6292   (interactive "P")
6293   (if all
6294       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6295     (gnus-summary-limit-to-marks
6296      ;; Concat all the marks that say that an article is read and have
6297      ;; those removed.
6298      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6299            gnus-killed-mark gnus-kill-file-mark
6300            gnus-low-score-mark gnus-expirable-mark
6301            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6302            gnus-duplicate-mark gnus-souped-mark)
6303      'reverse)))
6304
6305 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6306 (make-obsolete 'gnus-summary-delete-marked-with
6307                'gnus-summary-limit-exlude-marks)
6308
6309 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6310   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6311 If REVERSE, limit the summary buffer to articles that are marked
6312 with MARKS.  MARKS can either be a string of marks or a list of marks.
6313 Returns how many articles were removed."
6314   (interactive "sMarks: ")
6315   (gnus-summary-limit-to-marks marks t))
6316
6317 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6318   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6319 If REVERSE (the prefix), limit the summary buffer to articles that are
6320 not marked with MARKS.  MARKS can either be a string of marks or a
6321 list of marks.
6322 Returns how many articles were removed."
6323   (interactive "sMarks: \nP")
6324   (prog1
6325       (let ((data gnus-newsgroup-data)
6326             (marks (if (listp marks) marks
6327                      (append marks nil))) ; Transform to list.
6328             articles)
6329         (while data
6330           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6331                   (memq (gnus-data-mark (car data)) marks))
6332             (push (gnus-data-number (car data)) articles))
6333           (setq data (cdr data)))
6334         (gnus-summary-limit articles))
6335     (gnus-summary-position-point)))
6336
6337 (defun gnus-summary-limit-to-score (&optional score)
6338   "Limit to articles with score at or above SCORE."
6339   (interactive "P")
6340   (setq score (if score
6341                   (prefix-numeric-value score)
6342                 (or gnus-summary-default-score 0)))
6343   (let ((data gnus-newsgroup-data)
6344         articles)
6345     (while data
6346       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6347                 score)
6348         (push (gnus-data-number (car data)) articles))
6349       (setq data (cdr data)))
6350     (prog1
6351         (gnus-summary-limit articles)
6352       (gnus-summary-position-point))))
6353
6354 (defun gnus-summary-limit-include-thread (id)
6355   "Display all the hidden articles that in the current thread."
6356   (interactive (list (mail-header-id (gnus-summary-article-header))))
6357   (let ((articles (gnus-articles-in-thread
6358                    (gnus-id-to-thread (gnus-root-id id)))))
6359     (prog1
6360         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6361       (gnus-summary-position-point))))
6362
6363 (defun gnus-summary-limit-include-dormant ()
6364   "Display all the hidden articles that are marked as dormant.
6365 Note that this command only works on a subset of the articles currently
6366 fetched for this group."
6367   (interactive)
6368   (unless gnus-newsgroup-dormant
6369     (error "There are no dormant articles in this group"))
6370   (prog1
6371       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6372     (gnus-summary-position-point)))
6373
6374 (defun gnus-summary-limit-exclude-dormant ()
6375   "Hide all dormant articles."
6376   (interactive)
6377   (prog1
6378       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6379     (gnus-summary-position-point)))
6380
6381 (defun gnus-summary-limit-exclude-childless-dormant ()
6382   "Hide all dormant articles that have no children."
6383   (interactive)
6384   (let ((data (gnus-data-list t))
6385         articles d children)
6386     ;; Find all articles that are either not dormant or have
6387     ;; children.
6388     (while (setq d (pop data))
6389       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6390                 (and (setq children
6391                            (gnus-article-children (gnus-data-number d)))
6392                      (let (found)
6393                        (while children
6394                          (when (memq (car children) articles)
6395                            (setq children nil
6396                                  found t))
6397                          (pop children))
6398                        found)))
6399         (push (gnus-data-number d) articles)))
6400     ;; Do the limiting.
6401     (prog1
6402         (gnus-summary-limit articles)
6403       (gnus-summary-position-point))))
6404
6405 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6406   "Mark all unread excluded articles as read.
6407 If ALL, mark even excluded ticked and dormants as read."
6408   (interactive "P")
6409   (let ((articles (gnus-sorted-complement
6410                    (sort
6411                     (mapcar (lambda (h) (mail-header-number h))
6412                             gnus-newsgroup-headers)
6413                     '<)
6414                    (sort gnus-newsgroup-limit '<)))
6415         article)
6416     (setq gnus-newsgroup-unreads
6417           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6418     (if all
6419         (setq gnus-newsgroup-dormant nil
6420               gnus-newsgroup-marked nil
6421               gnus-newsgroup-reads
6422               (nconc
6423                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6424                gnus-newsgroup-reads))
6425       (while (setq article (pop articles))
6426         (unless (or (memq article gnus-newsgroup-dormant)
6427                     (memq article gnus-newsgroup-marked))
6428           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6429
6430 (defun gnus-summary-limit (articles &optional pop)
6431   (if pop
6432       ;; We pop the previous limit off the stack and use that.
6433       (setq articles (car gnus-newsgroup-limits)
6434             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6435     ;; We use the new limit, so we push the old limit on the stack.
6436     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6437   ;; Set the limit.
6438   (setq gnus-newsgroup-limit articles)
6439   (let ((total (length gnus-newsgroup-data))
6440         (data (gnus-data-find-list (gnus-summary-article-number)))
6441         (gnus-summary-mark-below nil)   ; Inhibit this.
6442         found)
6443     ;; This will do all the work of generating the new summary buffer
6444     ;; according to the new limit.
6445     (gnus-summary-prepare)
6446     ;; Hide any threads, possibly.
6447     (and gnus-show-threads
6448          gnus-thread-hide-subtree
6449          (gnus-summary-hide-all-threads))
6450     ;; Try to return to the article you were at, or one in the
6451     ;; neighborhood.
6452     (when data
6453       ;; We try to find some article after the current one.
6454       (while data
6455         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6456           (setq data nil
6457                 found t))
6458         (setq data (cdr data))))
6459     (unless found
6460       ;; If there is no data, that means that we were after the last
6461       ;; article.  The same goes when we can't find any articles
6462       ;; after the current one.
6463       (goto-char (point-max))
6464       (gnus-summary-find-prev))
6465     (gnus-set-mode-line 'summary)
6466     ;; We return how many articles were removed from the summary
6467     ;; buffer as a result of the new limit.
6468     (- total (length gnus-newsgroup-data))))
6469
6470 (defsubst gnus-invisible-cut-children (threads)
6471   (let ((num 0))
6472     (while threads
6473       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6474         (incf num))
6475       (pop threads))
6476     (< num 2)))
6477
6478 (defsubst gnus-cut-thread (thread)
6479   "Go forwards in the thread until we find an article that we want to display."
6480   (when (or (eq gnus-fetch-old-headers 'some)
6481             (eq gnus-fetch-old-headers 'invisible)
6482             (numberp gnus-fetch-old-headers)
6483             (eq gnus-build-sparse-threads 'some)
6484             (eq gnus-build-sparse-threads 'more))
6485     ;; Deal with old-fetched headers and sparse threads.
6486     (while (and
6487             thread
6488             (or
6489              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6490              (gnus-summary-article-ancient-p
6491               (mail-header-number (car thread))))
6492             (if (or (<= (length (cdr thread)) 1)
6493                     (eq gnus-fetch-old-headers 'invisible))
6494                 (setq gnus-newsgroup-limit
6495                       (delq (mail-header-number (car thread))
6496                             gnus-newsgroup-limit)
6497                       thread (cadr thread))
6498               (when (gnus-invisible-cut-children (cdr thread))
6499                 (let ((th (cdr thread)))
6500                   (while th
6501                     (if (memq (mail-header-number (caar th))
6502                               gnus-newsgroup-limit)
6503                         (setq thread (car th)
6504                               th nil)
6505                       (setq th (cdr th))))))))))
6506   thread)
6507
6508 (defun gnus-cut-threads (threads)
6509   "Cut off all uninteresting articles from the beginning of threads."
6510   (when (or (eq gnus-fetch-old-headers 'some)
6511             (eq gnus-fetch-old-headers 'invisible)
6512             (numberp gnus-fetch-old-headers)
6513             (eq gnus-build-sparse-threads 'some)
6514             (eq gnus-build-sparse-threads 'more))
6515     (let ((th threads))
6516       (while th
6517         (setcar th (gnus-cut-thread (car th)))
6518         (setq th (cdr th)))))
6519   ;; Remove nixed out threads.
6520   (delq nil threads))
6521
6522 (defun gnus-summary-initial-limit (&optional show-if-empty)
6523   "Figure out what the initial limit is supposed to be on group entry.
6524 This entails weeding out unwanted dormants, low-scored articles,
6525 fetch-old-headers verbiage, and so on."
6526   ;; Most groups have nothing to remove.
6527   (if (or gnus-inhibit-limiting
6528           (and (null gnus-newsgroup-dormant)
6529                (not (eq gnus-fetch-old-headers 'some))
6530                (not (numberp gnus-fetch-old-headers))
6531                (not (eq gnus-fetch-old-headers 'invisible))
6532                (null gnus-summary-expunge-below)
6533                (not (eq gnus-build-sparse-threads 'some))
6534                (not (eq gnus-build-sparse-threads 'more))
6535                (null gnus-thread-expunge-below)
6536                (not gnus-use-nocem)))
6537       ()                                ; Do nothing.
6538     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6539     (setq gnus-newsgroup-limit nil)
6540     (mapatoms
6541      (lambda (node)
6542        (unless (car (symbol-value node))
6543          ;; These threads have no parents -- they are roots.
6544          (let ((nodes (cdr (symbol-value node)))
6545                thread)
6546            (while nodes
6547              (if (and gnus-thread-expunge-below
6548                       (< (gnus-thread-total-score (car nodes))
6549                          gnus-thread-expunge-below))
6550                  (gnus-expunge-thread (pop nodes))
6551                (setq thread (pop nodes))
6552                (gnus-summary-limit-children thread))))))
6553      gnus-newsgroup-dependencies)
6554     ;; If this limitation resulted in an empty group, we might
6555     ;; pop the previous limit and use it instead.
6556     (when (and (not gnus-newsgroup-limit)
6557                show-if-empty)
6558       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6559     gnus-newsgroup-limit))
6560
6561 (defun gnus-summary-limit-children (thread)
6562   "Return 1 if this subthread is visible and 0 if it is not."
6563   ;; First we get the number of visible children to this thread.  This
6564   ;; is done by recursing down the thread using this function, so this
6565   ;; will really go down to a leaf article first, before slowly
6566   ;; working its way up towards the root.
6567   (when thread
6568     (let ((children
6569            (if (cdr thread)
6570                (apply '+ (mapcar 'gnus-summary-limit-children
6571                                  (cdr thread)))
6572              0))
6573           (number (mail-header-number (car thread)))
6574           score)
6575       (if (and
6576            (not (memq number gnus-newsgroup-marked))
6577            (or
6578             ;; If this article is dormant and has absolutely no visible
6579             ;; children, then this article isn't visible.
6580             (and (memq number gnus-newsgroup-dormant)
6581                  (zerop children))
6582             ;; If this is "fetch-old-headered" and there is no
6583             ;; visible children, then we don't want this article.
6584             (and (or (eq gnus-fetch-old-headers 'some)
6585                      (numberp gnus-fetch-old-headers))
6586                  (gnus-summary-article-ancient-p number)
6587                  (zerop children))
6588             ;; If this is "fetch-old-headered" and `invisible', then
6589             ;; we don't want this article.
6590             (and (eq gnus-fetch-old-headers 'invisible)
6591                  (gnus-summary-article-ancient-p number))
6592             ;; If this is a sparsely inserted article with no children,
6593             ;; we don't want it.
6594             (and (eq gnus-build-sparse-threads 'some)
6595                  (gnus-summary-article-sparse-p number)
6596                  (zerop children))
6597             ;; If we use expunging, and this article is really
6598             ;; low-scored, then we don't want this article.
6599             (when (and gnus-summary-expunge-below
6600                        (< (setq score
6601                                 (or (cdr (assq number gnus-newsgroup-scored))
6602                                     gnus-summary-default-score))
6603                           gnus-summary-expunge-below))
6604               ;; We increase the expunge-tally here, but that has
6605               ;; nothing to do with the limits, really.
6606               (incf gnus-newsgroup-expunged-tally)
6607               ;; We also mark as read here, if that's wanted.
6608               (when (and gnus-summary-mark-below
6609                          (< score gnus-summary-mark-below))
6610                 (setq gnus-newsgroup-unreads
6611                       (delq number gnus-newsgroup-unreads))
6612                 (if gnus-newsgroup-auto-expire
6613                     (push number gnus-newsgroup-expirable)
6614                   (push (cons number gnus-low-score-mark)
6615                         gnus-newsgroup-reads)))
6616               t)
6617             ;; Check NoCeM things.
6618             (if (and gnus-use-nocem
6619                      (gnus-nocem-unwanted-article-p
6620                       (mail-header-id (car thread))))
6621                 (progn
6622                   (setq gnus-newsgroup-unreads
6623                         (delq number gnus-newsgroup-unreads))
6624                   t))))
6625           ;; Nope, invisible article.
6626           0
6627         ;; Ok, this article is to be visible, so we add it to the limit
6628         ;; and return 1.
6629         (push number gnus-newsgroup-limit)
6630         1))))
6631
6632 (defun gnus-expunge-thread (thread)
6633   "Mark all articles in THREAD as read."
6634   (let* ((number (mail-header-number (car thread))))
6635     (incf gnus-newsgroup-expunged-tally)
6636     ;; We also mark as read here, if that's wanted.
6637     (setq gnus-newsgroup-unreads
6638           (delq number gnus-newsgroup-unreads))
6639     (if gnus-newsgroup-auto-expire
6640         (push number gnus-newsgroup-expirable)
6641       (push (cons number gnus-low-score-mark)
6642             gnus-newsgroup-reads)))
6643   ;; Go recursively through all subthreads.
6644   (mapcar 'gnus-expunge-thread (cdr thread)))
6645
6646 ;; Summary article oriented commands
6647
6648 (defun gnus-summary-refer-parent-article (n)
6649   "Refer parent article N times.
6650 If N is negative, go to ancestor -N instead.
6651 The difference between N and the number of articles fetched is returned."
6652   (interactive "p")
6653   (let ((skip 1)
6654         error header ref)
6655     (when (not (natnump n))
6656       (setq skip (abs n)
6657             n 1))
6658     (while (and (> n 0)
6659                 (not error))
6660       (setq header (gnus-summary-article-header))
6661       (if (and (eq (mail-header-number header)
6662                    (cdr gnus-article-current))
6663                (equal gnus-newsgroup-name
6664                       (car gnus-article-current)))
6665           ;; If we try to find the parent of the currently
6666           ;; displayed article, then we take a look at the actual
6667           ;; References header, since this is slightly more
6668           ;; reliable than the References field we got from the
6669           ;; server.
6670           (save-excursion
6671             (set-buffer gnus-original-article-buffer)
6672             (nnheader-narrow-to-headers)
6673             (unless (setq ref (message-fetch-field "references"))
6674               (setq ref (message-fetch-field "in-reply-to")))
6675             (widen))
6676         (setq ref
6677               ;; It's not the current article, so we take a bet on
6678               ;; the value we got from the server.
6679               (mail-header-references header)))
6680       (if (and ref
6681                (not (equal ref "")))
6682           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6683             (gnus-message 1 "Couldn't find parent"))
6684         (gnus-message 1 "No references in article %d"
6685                       (gnus-summary-article-number))
6686         (setq error t))
6687       (decf n))
6688     (gnus-summary-position-point)
6689     n))
6690
6691 (defun gnus-summary-refer-references ()
6692   "Fetch all articles mentioned in the References header.
6693 Return the number of articles fetched."
6694   (interactive)
6695   (let ((ref (mail-header-references (gnus-summary-article-header)))
6696         (current (gnus-summary-article-number))
6697         (n 0))
6698     (if (or (not ref)
6699             (equal ref ""))
6700         (error "No References in the current article")
6701       ;; For each Message-ID in the References header...
6702       (while (string-match "<[^>]*>" ref)
6703         (incf n)
6704         ;; ... fetch that article.
6705         (gnus-summary-refer-article
6706          (prog1 (match-string 0 ref)
6707            (setq ref (substring ref (match-end 0))))))
6708       (gnus-summary-goto-subject current)
6709       (gnus-summary-position-point)
6710       n)))
6711
6712 (defun gnus-summary-refer-thread (&optional limit)
6713   "Fetch all articles in the current thread.
6714 If LIMIT (the numerical prefix), fetch that many old headers instead
6715 of what's specified by the `gnus-refer-thread-limit' variable."
6716   (interactive "P")
6717   (let ((id (mail-header-id (gnus-summary-article-header)))
6718         (limit (if limit (prefix-numeric-value limit)
6719                  gnus-refer-thread-limit)))
6720     ;; We want to fetch LIMIT *old* headers, but we also have to
6721     ;; re-fetch all the headers in the current buffer, because many of
6722     ;; them may be undisplayed.  So we adjust LIMIT.
6723     (when (numberp limit)
6724       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6725     (unless (eq gnus-fetch-old-headers 'invisible)
6726       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6727       ;; Retrieve the headers and read them in.
6728       (if (eq (gnus-retrieve-headers
6729                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6730               'nov)
6731           (gnus-build-all-threads)
6732         (error "Can't fetch thread from backends that don't support NOV"))
6733       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6734     (gnus-summary-limit-include-thread id)))
6735
6736 (defun gnus-summary-refer-article (message-id)
6737   "Fetch an article specified by MESSAGE-ID."
6738   (interactive "sMessage-ID: ")
6739   (when (and (stringp message-id)
6740              (not (zerop (length message-id))))
6741     ;; Construct the correct Message-ID if necessary.
6742     ;; Suggested by tale@pawl.rpi.edu.
6743     (unless (string-match "^<" message-id)
6744       (setq message-id (concat "<" message-id)))
6745     (unless (string-match ">$" message-id)
6746       (setq message-id (concat message-id ">")))
6747     (let* ((header (gnus-id-to-header message-id))
6748            (sparse (and header
6749                         (gnus-summary-article-sparse-p
6750                          (mail-header-number header))
6751                         (memq (mail-header-number header)
6752                               gnus-newsgroup-limit)))
6753            number)
6754       (cond
6755        ;; If the article is present in the buffer we just go to it.
6756        ((and header
6757              (or (not (gnus-summary-article-sparse-p
6758                        (mail-header-number header)))
6759                  sparse))
6760         (prog1
6761             (gnus-summary-goto-article
6762              (mail-header-number header) nil t)
6763           (when sparse
6764             (gnus-summary-update-article (mail-header-number header)))))
6765        (t
6766         ;; We fetch the article.
6767         (catch 'found
6768           (dolist (gnus-override-method
6769                    (cond ((null gnus-refer-article-method)
6770                           (list 'current gnus-select-method))
6771                          ((consp (car gnus-refer-article-method))
6772                           gnus-refer-article-method)
6773                          (t
6774                           (list gnus-refer-article-method))))
6775             (when (eq 'current gnus-override-method)
6776               (setq gnus-override-method gnus-current-select-method))
6777             (gnus-check-server gnus-override-method)
6778             ;; Fetch the header, and display the article.
6779             (when (setq number (gnus-summary-insert-subject message-id))
6780               (gnus-summary-select-article nil nil nil number)
6781               (throw 'found t)))
6782           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6783
6784 (defun gnus-summary-edit-parameters ()
6785   "Edit the group parameters of the current group."
6786   (interactive)
6787   (gnus-group-edit-group gnus-newsgroup-name 'params))
6788
6789 (defun gnus-summary-customize-parameters ()
6790   "Customize the group parameters of the current group."
6791   (interactive)
6792   (gnus-group-customize gnus-newsgroup-name))
6793
6794 (defun gnus-summary-enter-digest-group (&optional force)
6795   "Enter an nndoc group based on the current article.
6796 If FORCE, force a digest interpretation.  If not, try
6797 to guess what the document format is."
6798   (interactive "P")
6799   (let ((conf gnus-current-window-configuration))
6800     (save-excursion
6801       (gnus-summary-select-article))
6802     (setq gnus-current-window-configuration conf)
6803     (let* ((name (format "%s-%d"
6804                          (gnus-group-prefixed-name
6805                           gnus-newsgroup-name (list 'nndoc ""))
6806                          (save-excursion
6807                            (set-buffer gnus-summary-buffer)
6808                            gnus-current-article)))
6809            (ogroup gnus-newsgroup-name)
6810            (params (append (gnus-info-params (gnus-get-info ogroup))
6811                            (list (cons 'to-group ogroup))
6812                            (list (cons 'save-article-group ogroup))))
6813            (case-fold-search t)
6814            (buf (current-buffer))
6815            dig to-address)
6816       (save-excursion
6817         (set-buffer gnus-original-article-buffer)
6818         ;; Have the digest group inherit the main mail address of
6819         ;; the parent article.
6820         (when (setq to-address (or (message-fetch-field "reply-to")
6821                                    (message-fetch-field "from")))
6822           (setq params (append (list (cons 'to-address to-address)))))
6823         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6824         (insert-buffer-substring gnus-original-article-buffer)
6825         ;; Remove lines that may lead nndoc to misinterpret the
6826         ;; document type.
6827         (narrow-to-region
6828          (goto-char (point-min))
6829          (or (search-forward "\n\n" nil t) (point)))
6830         (goto-char (point-min))
6831         (delete-matching-lines "^Path:\\|^From ")
6832         (widen))
6833       (unwind-protect
6834           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6835                     (gnus-newsgroup-ephemeral-ignored-charsets
6836                      gnus-newsgroup-ignored-charsets))
6837                 (gnus-group-read-ephemeral-group
6838                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6839                               (nndoc-article-type
6840                                ,(if force 'mbox 'guess))) t))
6841               ;; Make all postings to this group go to the parent group.
6842               (nconc (gnus-info-params (gnus-get-info name))
6843                      params)
6844             ;; Couldn't select this doc group.
6845             (switch-to-buffer buf)
6846             (gnus-set-global-variables)
6847             (gnus-configure-windows 'summary)
6848             (gnus-message 3 "Article couldn't be entered?"))
6849         (kill-buffer dig)))))
6850
6851 (defun gnus-summary-read-document (n)
6852   "Open a new group based on the current article(s).
6853 This will allow you to read digests and other similar
6854 documents as newsgroups.
6855 Obeys the standard process/prefix convention."
6856   (interactive "P")
6857   (let* ((articles (gnus-summary-work-articles n))
6858          (ogroup gnus-newsgroup-name)
6859          (params (append (gnus-info-params (gnus-get-info ogroup))
6860                          (list (cons 'to-group ogroup))))
6861          article group egroup groups vgroup)
6862     (while (setq article (pop articles))
6863       (setq group (format "%s-%d" gnus-newsgroup-name article))
6864       (gnus-summary-remove-process-mark article)
6865       (when (gnus-summary-display-article article)
6866         (save-excursion
6867           (with-temp-buffer
6868             (insert-buffer-substring gnus-original-article-buffer)
6869             ;; Remove some headers that may lead nndoc to make
6870             ;; the wrong guess.
6871             (message-narrow-to-head)
6872             (goto-char (point-min))
6873             (delete-matching-lines "^\\(Path\\):\\|^From ")
6874             (widen)
6875             (if (setq egroup
6876                       (gnus-group-read-ephemeral-group
6877                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6878                                      (nndoc-article-type guess))
6879                        t nil t))
6880                 (progn
6881                   ;; Make all postings to this group go to the parent group.
6882                   (nconc (gnus-info-params (gnus-get-info egroup))
6883                          params)
6884                   (push egroup groups))
6885               ;; Couldn't select this doc group.
6886               (gnus-error 3 "Article couldn't be entered"))))))
6887     ;; Now we have selected all the documents.
6888     (cond
6889      ((not groups)
6890       (error "None of the articles could be interpreted as documents"))
6891      ((gnus-group-read-ephemeral-group
6892        (setq vgroup (format
6893                      "nnvirtual:%s-%s" gnus-newsgroup-name
6894                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6895        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6896        t
6897        (cons (current-buffer) 'summary)))
6898      (t
6899       (error "Couldn't select virtual nndoc group")))))
6900
6901 (defun gnus-summary-isearch-article (&optional regexp-p)
6902   "Do incremental search forward on the current article.
6903 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6904   (interactive "P")
6905   (gnus-summary-select-article)
6906   (gnus-configure-windows 'article)
6907   (gnus-eval-in-buffer-window gnus-article-buffer
6908     (save-restriction
6909       (widen)
6910       (isearch-forward regexp-p))))
6911
6912 (defun gnus-summary-search-article-forward (regexp &optional backward)
6913   "Search for an article containing REGEXP forward.
6914 If BACKWARD, search backward instead."
6915   (interactive
6916    (list (read-string
6917           (format "Search article %s (regexp%s): "
6918                   (if current-prefix-arg "backward" "forward")
6919                   (if gnus-last-search-regexp
6920                       (concat ", default " gnus-last-search-regexp)
6921                     "")))
6922          current-prefix-arg))
6923   (if (string-equal regexp "")
6924       (setq regexp (or gnus-last-search-regexp ""))
6925     (setq gnus-last-search-regexp regexp))
6926   (if (gnus-summary-search-article regexp backward)
6927       (gnus-summary-show-thread)
6928     (error "Search failed: \"%s\"" regexp)))
6929
6930 (defun gnus-summary-search-article-backward (regexp)
6931   "Search for an article containing REGEXP backward."
6932   (interactive
6933    (list (read-string
6934           (format "Search article backward (regexp%s): "
6935                   (if gnus-last-search-regexp
6936                       (concat ", default " gnus-last-search-regexp)
6937                     "")))))
6938   (gnus-summary-search-article-forward regexp 'backward))
6939
6940 (defun gnus-summary-search-article (regexp &optional backward)
6941   "Search for an article containing REGEXP.
6942 Optional argument BACKWARD means do search for backward.
6943 `gnus-select-article-hook' is not called during the search."
6944   ;; We have to require this here to make sure that the following
6945   ;; dynamic binding isn't shadowed by autoloading.
6946   (require 'gnus-async)
6947   (require 'gnus-art)
6948   (let ((gnus-select-article-hook nil)  ;Disable hook.
6949         (gnus-article-prepare-hook nil)
6950         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6951         (gnus-use-article-prefetch nil)
6952         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6953         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6954         (sum (current-buffer))
6955         (gnus-display-mime-function nil)
6956         (found nil)
6957         point)
6958     (gnus-save-hidden-threads
6959       (gnus-summary-select-article)
6960       (set-buffer gnus-article-buffer)
6961       (when backward
6962         (forward-line -1))
6963       (while (not found)
6964         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6965         (if (if backward
6966                 (re-search-backward regexp nil t)
6967               (re-search-forward regexp nil t))
6968             ;; We found the regexp.
6969             (progn
6970               (setq found 'found)
6971               (beginning-of-line)
6972               (set-window-start
6973                (get-buffer-window (current-buffer))
6974                (point))
6975               (forward-line 1)
6976               (set-window-point
6977                (get-buffer-window (current-buffer))
6978                (point))
6979               (set-buffer sum)
6980               (setq point (point)))
6981           ;; We didn't find it, so we go to the next article.
6982           (set-buffer sum)
6983           (setq found 'not)
6984           (while (eq found 'not)
6985             (if (not (if backward (gnus-summary-find-prev)
6986                        (gnus-summary-find-next)))
6987                 ;; No more articles.
6988                 (setq found t)
6989               ;; Select the next article and adjust point.
6990               (unless (gnus-summary-article-sparse-p
6991                        (gnus-summary-article-number))
6992                 (setq found nil)
6993                 (gnus-summary-select-article)
6994                 (set-buffer gnus-article-buffer)
6995                 (widen)
6996                 (goto-char (if backward (point-max) (point-min))))))))
6997       (gnus-message 7 ""))
6998     ;; Return whether we found the regexp.
6999     (when (eq found 'found)
7000       (goto-char point)
7001       (gnus-summary-show-thread)
7002       (gnus-summary-goto-subject gnus-current-article)
7003       (gnus-summary-position-point)
7004       t)))
7005
7006 (defun gnus-summary-find-matching (header regexp &optional backward unread
7007                                           not-case-fold)
7008   "Return a list of all articles that match REGEXP on HEADER.
7009 The search stars on the current article and goes forwards unless
7010 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7011 If UNREAD is non-nil, only unread articles will
7012 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7013 in the comparisons."
7014   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7015                 (gnus-data-find-list
7016                  (gnus-summary-article-number) (gnus-data-list backward))))
7017         (case-fold-search (not not-case-fold))
7018         articles d func)
7019     (if (consp header)
7020         (if (eq (car header) 'extra)
7021             (setq func
7022                   `(lambda (h)
7023                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7024                          "")))
7025           (error "%s is an invalid header" header))
7026       (unless (fboundp (intern (concat "mail-header-" header)))
7027         (error "%s is not a valid header" header))
7028       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7029     (while data
7030       (setq d (car data))
7031       (and (or (not unread)             ; We want all articles...
7032                (gnus-data-unread-p d))  ; Or just unreads.
7033            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7034            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7035            (push (gnus-data-number d) articles)) ; Success!
7036       (setq data (cdr data)))
7037     (nreverse articles)))
7038
7039 (defun gnus-summary-execute-command (header regexp command &optional backward)
7040   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7041 If HEADER is an empty string (or nil), the match is done on the entire
7042 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7043   (interactive
7044    (list (let ((completion-ignore-case t))
7045            (completing-read
7046             "Header name: "
7047             (mapcar (lambda (string) (list string))
7048                     '("Number" "Subject" "From" "Lines" "Date"
7049                       "Message-ID" "Xref" "References" "Body"))
7050             nil 'require-match))
7051          (read-string "Regexp: ")
7052          (read-key-sequence "Command: ")
7053          current-prefix-arg))
7054   (when (equal header "Body")
7055     (setq header ""))
7056   ;; Hidden thread subtrees must be searched as well.
7057   (gnus-summary-show-all-threads)
7058   ;; We don't want to change current point nor window configuration.
7059   (save-excursion
7060     (save-window-excursion
7061       (gnus-message 6 "Executing %s..." (key-description command))
7062       ;; We'd like to execute COMMAND interactively so as to give arguments.
7063       (gnus-execute header regexp
7064                     `(call-interactively ',(key-binding command))
7065                     backward)
7066       (gnus-message 6 "Executing %s...done" (key-description command)))))
7067
7068 (defun gnus-summary-beginning-of-article ()
7069   "Scroll the article back to the beginning."
7070   (interactive)
7071   (gnus-summary-select-article)
7072   (gnus-configure-windows 'article)
7073   (gnus-eval-in-buffer-window gnus-article-buffer
7074     (widen)
7075     (goto-char (point-min))
7076     (when gnus-page-broken
7077       (gnus-narrow-to-page))))
7078
7079 (defun gnus-summary-end-of-article ()
7080   "Scroll to the end of the article."
7081   (interactive)
7082   (gnus-summary-select-article)
7083   (gnus-configure-windows 'article)
7084   (gnus-eval-in-buffer-window gnus-article-buffer
7085     (widen)
7086     (goto-char (point-max))
7087     (recenter -3)
7088     (when gnus-page-broken
7089       (gnus-narrow-to-page))))
7090
7091 (defun gnus-summary-print-article (&optional filename n)
7092   "Generate and print a PostScript image of the N next (mail) articles.
7093
7094 If N is negative, print the N previous articles.  If N is nil and articles
7095 have been marked with the process mark, print these instead.
7096
7097 If the optional first argument FILENAME is nil, send the image to the
7098 printer.  If FILENAME is a string, save the PostScript image in a file with
7099 that name.  If FILENAME is a number, prompt the user for the name of the file
7100 to save in."
7101   (interactive (list (ps-print-preprint current-prefix-arg)
7102                      current-prefix-arg))
7103   (dolist (article (gnus-summary-work-articles n))
7104     (gnus-summary-select-article nil nil 'pseudo article)
7105     (gnus-eval-in-buffer-window gnus-article-buffer
7106       (let ((buffer (generate-new-buffer " *print*")))
7107         (unwind-protect
7108             (progn
7109               (copy-to-buffer buffer (point-min) (point-max))
7110               (set-buffer buffer)
7111               (gnus-article-delete-invisible-text)
7112               (let ((ps-left-header
7113                      (list
7114                       (concat "("
7115                               (mail-header-subject gnus-current-headers) ")")
7116                       (concat "("
7117                               (mail-header-from gnus-current-headers) ")")))
7118                     (ps-right-header
7119                      (list
7120                       "/pagenumberstring load"
7121                       (concat "("
7122                               (mail-header-date gnus-current-headers) ")"))))
7123                 (gnus-run-hooks 'gnus-ps-print-hook)
7124                 (save-excursion
7125                   (ps-print-buffer-with-faces filename))))
7126           (kill-buffer buffer))))))
7127
7128 (defun gnus-summary-show-article (&optional arg)
7129   "Force re-fetching of the current article.
7130 If ARG (the prefix) is a number, show the article with the charset 
7131 defined in `gnus-summary-show-article-charset-alist', or the charset
7132 inputed.
7133 If ARG (the prefix) is non-nil and not a number, show the raw article 
7134 without any article massaging functions being run."
7135   (interactive "P")
7136   (cond 
7137    ((numberp arg)
7138     (let ((gnus-newsgroup-charset 
7139            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7140                (read-coding-system "Charset: ")))
7141           (gnus-newsgroup-ignored-charsets 'gnus-all))
7142       (gnus-summary-select-article nil 'force)))
7143    ((not arg)
7144     ;; Select the article the normal way.
7145     (gnus-summary-select-article nil 'force))
7146    (t
7147     ;; We have to require this here to make sure that the following
7148     ;; dynamic binding isn't shadowed by autoloading.
7149     (require 'gnus-async)
7150     (require 'gnus-art)
7151     ;; Bind the article treatment functions to nil.
7152     (let ((gnus-have-all-headers t)
7153           gnus-article-prepare-hook
7154           gnus-article-decode-hook
7155           gnus-display-mime-function
7156           gnus-break-pages)
7157       ;; Destroy any MIME parts.
7158       (when (gnus-buffer-live-p gnus-article-buffer)
7159         (save-excursion
7160           (set-buffer gnus-article-buffer)
7161           (mm-destroy-parts gnus-article-mime-handles)))
7162       (gnus-summary-select-article nil 'force))))
7163   (gnus-summary-goto-subject gnus-current-article)
7164   (gnus-summary-position-point))
7165
7166 (defun gnus-summary-verbose-headers (&optional arg)
7167   "Toggle permanent full header display.
7168 If ARG is a positive number, turn header display on.
7169 If ARG is a negative number, turn header display off."
7170   (interactive "P")
7171   (setq gnus-show-all-headers
7172         (cond ((or (not (numberp arg))
7173                    (zerop arg))
7174                (not gnus-show-all-headers))
7175               ((natnump arg)
7176                t)))
7177   (gnus-summary-show-article))
7178
7179 (defun gnus-summary-toggle-header (&optional arg)
7180   "Show the headers if they are hidden, or hide them if they are shown.
7181 If ARG is a positive number, show the entire header.
7182 If ARG is a negative number, hide the unwanted header lines."
7183   (interactive "P")
7184   (save-excursion
7185     (set-buffer gnus-article-buffer)
7186     (save-restriction
7187       (let* ((buffer-read-only nil)
7188              (inhibit-point-motion-hooks t)
7189              hidden e)
7190         (setq hidden
7191               (if (numberp arg)
7192                   (>= arg 0)
7193                 (save-restriction 
7194                   (article-narrow-to-head)
7195                   (gnus-article-hidden-text-p 'headers))))
7196         (goto-char (point-min))
7197         (when (search-forward "\n\n" nil t)
7198           (delete-region (point-min) (1- (point))))
7199         (goto-char (point-min))
7200         (save-excursion
7201           (set-buffer gnus-original-article-buffer)
7202           (goto-char (point-min))
7203           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7204         (insert-buffer-substring gnus-original-article-buffer 1 e)
7205         (save-restriction
7206           (narrow-to-region (point-min) (point))
7207           (article-decode-encoded-words)
7208           (if  hidden
7209               (let ((gnus-treat-hide-headers nil)
7210                     (gnus-treat-hide-boring-headers nil))
7211                 (gnus-treat-article 'head))
7212             (gnus-treat-article 'head)))))))
7213
7214 (defun gnus-summary-show-all-headers ()
7215   "Make all header lines visible."
7216   (interactive)
7217   (gnus-article-show-all-headers))
7218
7219 (defun gnus-summary-caesar-message (&optional arg)
7220   "Caesar rotate the current article by 13.
7221 The numerical prefix specifies how many places to rotate each letter
7222 forward."
7223   (interactive "P")
7224   (gnus-summary-select-article)
7225   (let ((mail-header-separator ""))
7226     (gnus-eval-in-buffer-window gnus-article-buffer
7227       (save-restriction
7228         (widen)
7229         (let ((start (window-start))
7230               buffer-read-only)
7231           (message-caesar-buffer-body arg)
7232           (set-window-start (get-buffer-window (current-buffer)) start))))))
7233
7234 (defun gnus-summary-stop-page-breaking ()
7235   "Stop page breaking in the current article."
7236   (interactive)
7237   (gnus-summary-select-article)
7238   (gnus-eval-in-buffer-window gnus-article-buffer
7239     (widen)
7240     (when (gnus-visual-p 'page-marker)
7241       (let ((buffer-read-only nil))
7242         (gnus-remove-text-with-property 'gnus-prev)
7243         (gnus-remove-text-with-property 'gnus-next))
7244       (setq gnus-page-broken nil))))
7245
7246 (defun gnus-summary-move-article (&optional n to-newsgroup
7247                                             select-method action)
7248   "Move the current article to a different newsgroup.
7249 If N is a positive number, move the N next articles.
7250 If N is a negative number, move the N previous articles.
7251 If N is nil and any articles have been marked with the process mark,
7252 move those articles instead.
7253 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7254 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7255 re-spool using this method.
7256
7257 For this function to work, both the current newsgroup and the
7258 newsgroup that you want to move to have to support the `request-move'
7259 and `request-accept' functions."
7260   (interactive "P")
7261   (unless action
7262     (setq action 'move))
7263   ;; Disable marking as read.
7264   (let (gnus-mark-article-hook)
7265     (save-window-excursion
7266       (gnus-summary-select-article)))
7267   ;; Check whether the source group supports the required functions.
7268   (cond ((and (eq action 'move)
7269               (not (gnus-check-backend-function
7270                     'request-move-article gnus-newsgroup-name)))
7271          (error "The current group does not support article moving"))
7272         ((and (eq action 'crosspost)
7273               (not (gnus-check-backend-function
7274                     'request-replace-article gnus-newsgroup-name)))
7275          (error "The current group does not support article editing")))
7276   (let ((articles (gnus-summary-work-articles n))
7277         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7278         (names '((move "Move" "Moving")
7279                  (copy "Copy" "Copying")
7280                  (crosspost "Crosspost" "Crossposting")))
7281         (copy-buf (save-excursion
7282                     (nnheader-set-temp-buffer " *copy article*")))
7283         art-group to-method new-xref article to-groups)
7284     (unless (assq action names)
7285       (error "Unknown action %s" action))
7286     ;; Read the newsgroup name.
7287     (when (and (not to-newsgroup)
7288                (not select-method))
7289       (setq to-newsgroup
7290             (gnus-read-move-group-name
7291              (cadr (assq action names))
7292              (symbol-value (intern (format "gnus-current-%s-group" action)))
7293              articles prefix))
7294       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7295     (setq to-method (or select-method
7296                         (gnus-group-name-to-method to-newsgroup)))
7297     ;; Check the method we are to move this article to...
7298     (unless (gnus-check-backend-function
7299              'request-accept-article (car to-method))
7300       (error "%s does not support article copying" (car to-method)))
7301     (unless (gnus-check-server to-method)
7302       (error "Can't open server %s" (car to-method)))
7303     (gnus-message 6 "%s to %s: %s..."
7304                   (caddr (assq action names))
7305                   (or (car select-method) to-newsgroup) articles)
7306     (while articles
7307       (setq article (pop articles))
7308       (setq
7309        art-group
7310        (cond
7311         ;; Move the article.
7312         ((eq action 'move)
7313          ;; Remove this article from future suppression.
7314          (gnus-dup-unsuppress-article article)
7315          (gnus-request-move-article
7316           article                       ; Article to move
7317           gnus-newsgroup-name           ; From newsgroup
7318           (nth 1 (gnus-find-method-for-group
7319                   gnus-newsgroup-name)) ; Server
7320           (list 'gnus-request-accept-article
7321                 to-newsgroup (list 'quote select-method)
7322                 (not articles) t)               ; Accept form
7323           (not articles)))              ; Only save nov last time
7324         ;; Copy the article.
7325         ((eq action 'copy)
7326          (save-excursion
7327            (set-buffer copy-buf)
7328            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7329              (gnus-request-accept-article
7330               to-newsgroup select-method (not articles) t))))
7331         ;; Crosspost the article.
7332         ((eq action 'crosspost)
7333          (let ((xref (message-tokenize-header
7334                       (mail-header-xref (gnus-summary-article-header article))
7335                       " ")))
7336            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7337                                   ":" article))
7338            (unless xref
7339              (setq xref (list (system-name))))
7340            (setq new-xref
7341                  (concat
7342                   (mapconcat 'identity
7343                              (delete "Xref:" (delete new-xref xref))
7344                              " ")
7345                   " " new-xref))
7346            (save-excursion
7347              (set-buffer copy-buf)
7348              ;; First put the article in the destination group.
7349              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7350              (when (consp (setq art-group
7351                                 (gnus-request-accept-article
7352                                  to-newsgroup select-method (not articles))))
7353                (setq new-xref (concat new-xref " " (car art-group)
7354                                       ":" (cdr art-group)))
7355                ;; Now we have the new Xrefs header, so we insert
7356                ;; it and replace the new article.
7357                (nnheader-replace-header "Xref" new-xref)
7358                (gnus-request-replace-article
7359                 (cdr art-group) to-newsgroup (current-buffer))
7360                art-group))))))
7361       (cond
7362        ((not art-group)
7363        (gnus-message 1 "Couldn't %s article %s: %s"
7364                      (cadr (assq action names)) article
7365                      (nnheader-get-report (car to-method))))
7366        ((and (eq art-group 'junk)
7367              (eq action 'move))
7368         (gnus-summary-mark-article article gnus-canceled-mark)
7369         (gnus-message 4 "Deleted article %s" article))
7370        (t
7371         (let* ((pto-group (gnus-group-prefixed-name
7372                            (car art-group) to-method))
7373                (entry
7374                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7375                (info (nth 2 entry))
7376                (to-group (gnus-info-group info))
7377                to-marks)
7378           ;; Update the group that has been moved to.
7379           (when (and info
7380                      (memq action '(move copy)))
7381             (unless (member to-group to-groups)
7382               (push to-group to-groups))
7383
7384             (unless (memq article gnus-newsgroup-unreads)
7385               (push 'read to-marks)
7386               (gnus-info-set-read
7387                info (gnus-add-to-range (gnus-info-read info)
7388                                        (list (cdr art-group)))))
7389
7390             ;; Copy any marks over to the new group.
7391             (let ((marks gnus-article-mark-lists)
7392                   (to-article (cdr art-group)))
7393
7394               ;; See whether the article is to be put in the cache.
7395               (when gnus-use-cache
7396                 (gnus-cache-possibly-enter-article
7397                  to-group to-article
7398                  (memq article gnus-newsgroup-marked)
7399                  (memq article gnus-newsgroup-dormant)
7400                  (memq article gnus-newsgroup-unreads)))
7401
7402               (when (and (equal to-group gnus-newsgroup-name)
7403                          (not (memq article gnus-newsgroup-unreads)))
7404                 ;; Mark this article as read in this group.
7405                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7406                 (setcdr (gnus-active to-group) to-article)
7407                 (setcdr gnus-newsgroup-active to-article))
7408
7409               (while marks
7410                 (when (memq article (symbol-value
7411                                      (intern (format "gnus-newsgroup-%s"
7412                                                      (caar marks)))))
7413                   (push (cdar marks) to-marks)
7414                   ;; If the other group is the same as this group,
7415                   ;; then we have to add the mark to the list.
7416                   (when (equal to-group gnus-newsgroup-name)
7417                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7418                          (cons to-article
7419                                (symbol-value
7420                                 (intern (format "gnus-newsgroup-%s"
7421                                                 (caar marks)))))))
7422                   ;; Copy the marks to other group.
7423                   (gnus-add-marked-articles
7424                    to-group (cdar marks) (list to-article) info))
7425                 (setq marks (cdr marks)))
7426
7427               (gnus-request-set-mark to-group (list (list (list to-article)
7428                                                           'set
7429                                                           to-marks)))
7430
7431               (gnus-dribble-enter
7432                (concat "(gnus-group-set-info '"
7433                        (gnus-prin1-to-string (gnus-get-info to-group))
7434                        ")"))))
7435
7436           ;; Update the Xref header in this article to point to
7437           ;; the new crossposted article we have just created.
7438           (when (eq action 'crosspost)
7439             (save-excursion
7440               (set-buffer copy-buf)
7441               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7442               (nnheader-replace-header "Xref" new-xref)
7443               (gnus-request-replace-article
7444                article gnus-newsgroup-name (current-buffer)))))
7445
7446         ;;;!!!Why is this necessary?
7447         (set-buffer gnus-summary-buffer)
7448
7449         (gnus-summary-goto-subject article)
7450         (when (eq action 'move)
7451           (gnus-summary-mark-article article gnus-canceled-mark))))
7452       (gnus-summary-remove-process-mark article))
7453     ;; Re-activate all groups that have been moved to.
7454     (while to-groups
7455       (save-excursion
7456         (set-buffer gnus-group-buffer)
7457         (when (gnus-group-goto-group (car to-groups) t)
7458           (gnus-group-get-new-news-this-group 1 t))
7459         (pop to-groups)))
7460
7461     (gnus-kill-buffer copy-buf)
7462     (gnus-summary-position-point)
7463     (gnus-set-mode-line 'summary)))
7464
7465 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7466   "Move the current article to a different newsgroup.
7467 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7468 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7469 re-spool using this method."
7470   (interactive "P")
7471   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7472
7473 (defun gnus-summary-crosspost-article (&optional n)
7474   "Crosspost the current article to some other group."
7475   (interactive "P")
7476   (gnus-summary-move-article n nil nil 'crosspost))
7477
7478 (defcustom gnus-summary-respool-default-method nil
7479   "Default method for respooling an article.
7480 If nil, use to the current newsgroup method."
7481   :type '(choice (gnus-select-method :value (nnml ""))
7482                  (const nil))
7483   :group 'gnus-summary-mail)
7484
7485 (defun gnus-summary-respool-article (&optional n method)
7486   "Respool the current article.
7487 The article will be squeezed through the mail spooling process again,
7488 which means that it will be put in some mail newsgroup or other
7489 depending on `nnmail-split-methods'.
7490 If N is a positive number, respool the N next articles.
7491 If N is a negative number, respool the N previous articles.
7492 If N is nil and any articles have been marked with the process mark,
7493 respool those articles instead.
7494
7495 Respooling can be done both from mail groups and \"real\" newsgroups.
7496 In the former case, the articles in question will be moved from the
7497 current group into whatever groups they are destined to.  In the
7498 latter case, they will be copied into the relevant groups."
7499   (interactive
7500    (list current-prefix-arg
7501          (let* ((methods (gnus-methods-using 'respool))
7502                 (methname
7503                  (symbol-name (or gnus-summary-respool-default-method
7504                                   (car (gnus-find-method-for-group
7505                                         gnus-newsgroup-name)))))
7506                 (method
7507                  (gnus-completing-read
7508                   methname "What backend do you want to use when respooling?"
7509                   methods nil t nil 'gnus-mail-method-history))
7510                 ms)
7511            (cond
7512             ((zerop (length (setq ms (gnus-servers-using-backend
7513                                       (intern method)))))
7514              (list (intern method) ""))
7515             ((= 1 (length ms))
7516              (car ms))
7517             (t
7518              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7519                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7520                            ms-alist))))))))
7521   (unless method
7522     (error "No method given for respooling"))
7523   (if (assoc (symbol-name
7524               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7525              (gnus-methods-using 'respool))
7526       (gnus-summary-move-article n nil method)
7527     (gnus-summary-copy-article n nil method)))
7528
7529 (defun gnus-summary-import-article (file)
7530   "Import an arbitrary file into a mail newsgroup."
7531   (interactive "fImport file: ")
7532   (let ((group gnus-newsgroup-name)
7533         (now (current-time))
7534         atts lines)
7535     (unless (gnus-check-backend-function 'request-accept-article group)
7536       (error "%s does not support article importing" group))
7537     (or (file-readable-p file)
7538         (not (file-regular-p file))
7539         (error "Can't read %s" file))
7540     (save-excursion
7541       (set-buffer (gnus-get-buffer-create " *import file*"))
7542       (erase-buffer)
7543       (insert-file-contents file)
7544       (goto-char (point-min))
7545       (unless (nnheader-article-p)
7546         ;; This doesn't look like an article, so we fudge some headers.
7547         (setq atts (file-attributes file)
7548               lines (count-lines (point-min) (point-max)))
7549         (insert "From: " (read-string "From: ") "\n"
7550                 "Subject: " (read-string "Subject: ") "\n"
7551                 "Date: " (message-make-date (nth 5 atts))
7552                 "\n"
7553                 "Message-ID: " (message-make-message-id) "\n"
7554                 "Lines: " (int-to-string lines) "\n"
7555                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7556       (gnus-request-accept-article group nil t)
7557       (kill-buffer (current-buffer)))))
7558
7559 (defun gnus-summary-article-posted-p ()
7560   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7561 This will be the case if the article has both been mailed and posted."
7562   (interactive)
7563   (let ((id (mail-header-references (gnus-summary-article-header)))
7564         (gnus-override-method
7565          (or gnus-refer-article-method gnus-select-method)))
7566     (if (gnus-request-head id "")
7567         (gnus-message 2 "The current message was found on %s"
7568                       gnus-override-method)
7569       (gnus-message 2 "The current message couldn't be found on %s"
7570                     gnus-override-method)
7571       nil)))
7572
7573 (defun gnus-summary-expire-articles (&optional now)
7574   "Expire all articles that are marked as expirable in the current group."
7575   (interactive)
7576   (when (gnus-check-backend-function
7577          'request-expire-articles gnus-newsgroup-name)
7578     ;; This backend supports expiry.
7579     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7580            (expirable (if total
7581                           (progn
7582                             ;; We need to update the info for
7583                             ;; this group for `gnus-list-of-read-articles'
7584                             ;; to give us the right answer.
7585                             (gnus-run-hooks 'gnus-exit-group-hook)
7586                             (gnus-summary-update-info)
7587                             (gnus-list-of-read-articles gnus-newsgroup-name))
7588                         (setq gnus-newsgroup-expirable
7589                               (sort gnus-newsgroup-expirable '<))))
7590            (expiry-wait (if now 'immediate
7591                           (gnus-group-find-parameter
7592                            gnus-newsgroup-name 'expiry-wait)))
7593            es)
7594       (when expirable
7595         ;; There are expirable articles in this group, so we run them
7596         ;; through the expiry process.
7597         (gnus-message 6 "Expiring articles...")
7598         (unless (gnus-check-group gnus-newsgroup-name)
7599           (error "Can't open server for %s" gnus-newsgroup-name))
7600         ;; The list of articles that weren't expired is returned.
7601         (save-excursion
7602           (if expiry-wait
7603               (let ((nnmail-expiry-wait-function nil)
7604                     (nnmail-expiry-wait expiry-wait))
7605                 (setq es (gnus-request-expire-articles
7606                           expirable gnus-newsgroup-name)))
7607             (setq es (gnus-request-expire-articles
7608                       expirable gnus-newsgroup-name))))
7609         (unless total
7610           (setq gnus-newsgroup-expirable es))
7611         ;; We go through the old list of expirable, and mark all
7612         ;; really expired articles as nonexistent.
7613         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7614           (let ((gnus-use-cache nil))
7615             (while expirable
7616               (unless (memq (car expirable) es)
7617                 (when (gnus-data-find (car expirable))
7618                   (gnus-summary-mark-article
7619                    (car expirable) gnus-canceled-mark)))
7620               (setq expirable (cdr expirable)))))
7621         (gnus-message 6 "Expiring articles...done")))))
7622
7623 (defun gnus-summary-expire-articles-now ()
7624   "Expunge all expirable articles in the current group.
7625 This means that *all* articles that are marked as expirable will be
7626 deleted forever, right now."
7627   (interactive)
7628   (or gnus-expert-user
7629       (gnus-yes-or-no-p
7630        "Are you really, really, really sure you want to delete all these messages? ")
7631       (error "Phew!"))
7632   (gnus-summary-expire-articles t))
7633
7634 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7635 (defun gnus-summary-delete-article (&optional n)
7636   "Delete the N next (mail) articles.
7637 This command actually deletes articles.  This is not a marking
7638 command.  The article will disappear forever from your life, never to
7639 return.
7640 If N is negative, delete backwards.
7641 If N is nil and articles have been marked with the process mark,
7642 delete these instead."
7643   (interactive "P")
7644   (unless (gnus-check-backend-function 'request-expire-articles
7645                                        gnus-newsgroup-name)
7646     (error "The current newsgroup does not support article deletion"))
7647   ;; Compute the list of articles to delete.
7648   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7649         not-deleted)
7650     (if (and gnus-novice-user
7651              (not (gnus-yes-or-no-p
7652                    (format "Do you really want to delete %s forever? "
7653                            (if (> (length articles) 1)
7654                                (format "these %s articles" (length articles))
7655                              "this article")))))
7656         ()
7657       ;; Delete the articles.
7658       (setq not-deleted (gnus-request-expire-articles
7659                          articles gnus-newsgroup-name 'force))
7660       (while articles
7661         (gnus-summary-remove-process-mark (car articles))
7662         ;; The backend might not have been able to delete the article
7663         ;; after all.
7664         (unless (memq (car articles) not-deleted)
7665           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7666         (setq articles (cdr articles)))
7667       (when not-deleted
7668         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7669     (gnus-summary-position-point)
7670     (gnus-set-mode-line 'summary)
7671     not-deleted))
7672
7673 (defun gnus-summary-edit-article (&optional force)
7674   "Edit the current article.
7675 This will have permanent effect only in mail groups.
7676 If FORCE is non-nil, allow editing of articles even in read-only
7677 groups."
7678   (interactive "P")
7679   (save-excursion
7680     (set-buffer gnus-summary-buffer)
7681     (let ((mail-parse-charset gnus-newsgroup-charset)
7682           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7683       (gnus-set-global-variables)
7684       (when (and (not force)
7685                  (gnus-group-read-only-p))
7686         (error "The current newsgroup does not support article editing"))
7687       (gnus-summary-show-article t)
7688       (gnus-article-edit-article
7689        'mime-to-mml
7690        `(lambda (no-highlight)
7691           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7692                 (mail-parse-ignored-charsets 
7693                  ',gnus-newsgroup-ignored-charsets))
7694             (mml-to-mime)
7695             (gnus-summary-edit-article-done
7696              ,(or (mail-header-references gnus-current-headers) "")
7697              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7698
7699 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7700
7701 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7702                                                  no-highlight)
7703   "Make edits to the current article permanent."
7704   (interactive)
7705   ;; Replace the article.
7706   (let ((buf (current-buffer)))
7707     (with-temp-buffer
7708       (insert-buffer-substring buf)
7709       (if (and (not read-only)
7710                (not (gnus-request-replace-article
7711                      (cdr gnus-article-current) (car gnus-article-current)
7712                      (current-buffer) t)))
7713           (error "Couldn't replace article")
7714         ;; Update the summary buffer.
7715         (if (and references
7716                  (equal (message-tokenize-header references " ")
7717                         (message-tokenize-header
7718                          (or (message-fetch-field "references") "") " ")))
7719             ;; We only have to update this line.
7720             (save-excursion
7721               (save-restriction
7722                 (message-narrow-to-head)
7723                 (let ((head (buffer-string))
7724                       header)
7725                   (with-temp-buffer
7726                     (insert (format "211 %d Article retrieved.\n"
7727                                     (cdr gnus-article-current)))
7728                     (insert head)
7729                     (insert ".\n")
7730                     (let ((nntp-server-buffer (current-buffer)))
7731                       (setq header (car (gnus-get-newsgroup-headers
7732                                          (save-excursion
7733                                            (set-buffer gnus-summary-buffer)
7734                                            gnus-newsgroup-dependencies)
7735                                          t))))
7736                     (save-excursion
7737                       (set-buffer gnus-summary-buffer)
7738                       (gnus-data-set-header
7739                        (gnus-data-find (cdr gnus-article-current))
7740                        header)
7741                       (gnus-summary-update-article-line
7742                        (cdr gnus-article-current) header))))))
7743           ;; Update threads.
7744           (set-buffer (or buffer gnus-summary-buffer))
7745           (gnus-summary-update-article (cdr gnus-article-current)))
7746         ;; Prettify the article buffer again.
7747         (unless no-highlight
7748           (save-excursion
7749             (set-buffer gnus-article-buffer)
7750             ;;;!!! Fix this -- article should be rehighlighted.
7751             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7752             (set-buffer gnus-original-article-buffer)
7753             (gnus-request-article
7754              (cdr gnus-article-current)
7755              (car gnus-article-current) (current-buffer))))
7756         ;; Prettify the summary buffer line.
7757         (when (gnus-visual-p 'summary-highlight 'highlight)
7758           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7759
7760 (defun gnus-summary-edit-wash (key)
7761   "Perform editing command KEY in the article buffer."
7762   (interactive
7763    (list
7764     (progn
7765       (message "%s" (concat (this-command-keys) "- "))
7766       (read-char))))
7767   (message "")
7768   (gnus-summary-edit-article)
7769   (execute-kbd-macro (concat (this-command-keys) key))
7770   (gnus-article-edit-done))
7771
7772 ;;; Respooling
7773
7774 (defun gnus-summary-respool-query (&optional silent trace)
7775   "Query where the respool algorithm would put this article."
7776   (interactive)
7777   (let (gnus-mark-article-hook)
7778     (gnus-summary-select-article)
7779     (save-excursion
7780       (set-buffer gnus-original-article-buffer)
7781       (save-restriction
7782         (message-narrow-to-head)
7783         (let ((groups (nnmail-article-group 'identity trace)))
7784           (unless silent
7785             (if groups
7786                 (message "This message would go to %s"
7787                          (mapconcat 'car groups ", "))
7788               (message "This message would go to no groups"))
7789             groups))))))
7790
7791 (defun gnus-summary-respool-trace ()
7792   "Trace where the respool algorithm would put this article.
7793 Display a buffer showing all fancy splitting patterns which matched."
7794   (interactive)
7795   (gnus-summary-respool-query nil t))
7796
7797 ;; Summary marking commands.
7798
7799 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7800   "Mark articles which has the same subject as read, and then select the next.
7801 If UNMARK is positive, remove any kind of mark.
7802 If UNMARK is negative, tick articles."
7803   (interactive "P")
7804   (when unmark
7805     (setq unmark (prefix-numeric-value unmark)))
7806   (let ((count
7807          (gnus-summary-mark-same-subject
7808           (gnus-summary-article-subject) unmark)))
7809     ;; Select next unread article.  If auto-select-same mode, should
7810     ;; select the first unread article.
7811     (gnus-summary-next-article t (and gnus-auto-select-same
7812                                       (gnus-summary-article-subject)))
7813     (gnus-message 7 "%d article%s marked as %s"
7814                   count (if (= count 1) " is" "s are")
7815                   (if unmark "unread" "read"))))
7816
7817 (defun gnus-summary-kill-same-subject (&optional unmark)
7818   "Mark articles which has the same subject as read.
7819 If UNMARK is positive, remove any kind of mark.
7820 If UNMARK is negative, tick articles."
7821   (interactive "P")
7822   (when unmark
7823     (setq unmark (prefix-numeric-value unmark)))
7824   (let ((count
7825          (gnus-summary-mark-same-subject
7826           (gnus-summary-article-subject) unmark)))
7827     ;; If marked as read, go to next unread subject.
7828     (when (null unmark)
7829       ;; Go to next unread subject.
7830       (gnus-summary-next-subject 1 t))
7831     (gnus-message 7 "%d articles are marked as %s"
7832                   count (if unmark "unread" "read"))))
7833
7834 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7835   "Mark articles with same SUBJECT as read, and return marked number.
7836 If optional argument UNMARK is positive, remove any kinds of marks.
7837 If optional argument UNMARK is negative, mark articles as unread instead."
7838   (let ((count 1))
7839     (save-excursion
7840       (cond
7841        ((null unmark)                   ; Mark as read.
7842         (while (and
7843                 (progn
7844                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7845                   (gnus-summary-show-thread) t)
7846                 (gnus-summary-find-subject subject))
7847           (setq count (1+ count))))
7848        ((> unmark 0)                    ; Tick.
7849         (while (and
7850                 (progn
7851                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7852                   (gnus-summary-show-thread) t)
7853                 (gnus-summary-find-subject subject))
7854           (setq count (1+ count))))
7855        (t                               ; Mark as unread.
7856         (while (and
7857                 (progn
7858                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7859                   (gnus-summary-show-thread) t)
7860                 (gnus-summary-find-subject subject))
7861           (setq count (1+ count)))))
7862       (gnus-set-mode-line 'summary)
7863       ;; Return the number of marked articles.
7864       count)))
7865
7866 (defun gnus-summary-mark-as-processable (n &optional unmark)
7867   "Set the process mark on the next N articles.
7868 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7869 the process mark instead.  The difference between N and the actual
7870 number of articles marked is returned."
7871   (interactive "p")
7872   (let ((backward (< n 0))
7873         (n (abs n)))
7874     (while (and
7875             (> n 0)
7876             (if unmark
7877                 (gnus-summary-remove-process-mark
7878                  (gnus-summary-article-number))
7879               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7880             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7881       (setq n (1- n)))
7882     (when (/= 0 n)
7883       (gnus-message 7 "No more articles"))
7884     (gnus-summary-recenter)
7885     (gnus-summary-position-point)
7886     n))
7887
7888 (defun gnus-summary-unmark-as-processable (n)
7889   "Remove the process mark from the next N articles.
7890 If N is negative, unmark backward instead.  The difference between N and
7891 the actual number of articles unmarked is returned."
7892   (interactive "p")
7893   (gnus-summary-mark-as-processable n t))
7894
7895 (defun gnus-summary-unmark-all-processable ()
7896   "Remove the process mark from all articles."
7897   (interactive)
7898   (save-excursion
7899     (while gnus-newsgroup-processable
7900       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7901   (gnus-summary-position-point))
7902
7903 (defun gnus-summary-mark-as-expirable (n)
7904   "Mark N articles forward as expirable.
7905 If N is negative, mark backward instead.  The difference between N and
7906 the actual number of articles marked is returned."
7907   (interactive "p")
7908   (gnus-summary-mark-forward n gnus-expirable-mark))
7909
7910 (defun gnus-summary-mark-article-as-replied (article)
7911   "Mark ARTICLE replied and update the summary line."
7912   (push article gnus-newsgroup-replied)
7913   (let ((buffer-read-only nil))
7914     (when (gnus-summary-goto-subject article nil t)
7915       (gnus-summary-update-secondary-mark article))))
7916
7917 (defun gnus-summary-set-bookmark (article)
7918   "Set a bookmark in current article."
7919   (interactive (list (gnus-summary-article-number)))
7920   (when (or (not (get-buffer gnus-article-buffer))
7921             (not gnus-current-article)
7922             (not gnus-article-current)
7923             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7924     (error "No current article selected"))
7925   ;; Remove old bookmark, if one exists.
7926   (let ((old (assq article gnus-newsgroup-bookmarks)))
7927     (when old
7928       (setq gnus-newsgroup-bookmarks
7929             (delq old gnus-newsgroup-bookmarks))))
7930   ;; Set the new bookmark, which is on the form
7931   ;; (article-number . line-number-in-body).
7932   (push
7933    (cons article
7934          (save-excursion
7935            (set-buffer gnus-article-buffer)
7936            (count-lines
7937             (min (point)
7938                  (save-excursion
7939                    (goto-char (point-min))
7940                    (search-forward "\n\n" nil t)
7941                    (point)))
7942             (point))))
7943    gnus-newsgroup-bookmarks)
7944   (gnus-message 6 "A bookmark has been added to the current article."))
7945
7946 (defun gnus-summary-remove-bookmark (article)
7947   "Remove the bookmark from the current article."
7948   (interactive (list (gnus-summary-article-number)))
7949   ;; Remove old bookmark, if one exists.
7950   (let ((old (assq article gnus-newsgroup-bookmarks)))
7951     (if old
7952         (progn
7953           (setq gnus-newsgroup-bookmarks
7954                 (delq old gnus-newsgroup-bookmarks))
7955           (gnus-message 6 "Removed bookmark."))
7956       (gnus-message 6 "No bookmark in current article."))))
7957
7958 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7959 (defun gnus-summary-mark-as-dormant (n)
7960   "Mark N articles forward as dormant.
7961 If N is negative, mark backward instead.  The difference between N and
7962 the actual number of articles marked is returned."
7963   (interactive "p")
7964   (gnus-summary-mark-forward n gnus-dormant-mark))
7965
7966 (defun gnus-summary-set-process-mark (article)
7967   "Set the process mark on ARTICLE and update the summary line."
7968   (setq gnus-newsgroup-processable
7969         (cons article
7970               (delq article gnus-newsgroup-processable)))
7971   (when (gnus-summary-goto-subject article)
7972     (gnus-summary-show-thread)
7973     (gnus-summary-goto-subject article)
7974     (gnus-summary-update-secondary-mark article)))
7975
7976 (defun gnus-summary-remove-process-mark (article)
7977   "Remove the process mark from ARTICLE and update the summary line."
7978   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7979   (when (gnus-summary-goto-subject article)
7980     (gnus-summary-show-thread)
7981     (gnus-summary-goto-subject article)
7982     (gnus-summary-update-secondary-mark article)))
7983
7984 (defun gnus-summary-set-saved-mark (article)
7985   "Set the process mark on ARTICLE and update the summary line."
7986   (push article gnus-newsgroup-saved)
7987   (when (gnus-summary-goto-subject article)
7988     (gnus-summary-update-secondary-mark article)))
7989
7990 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7991   "Mark N articles as read forwards.
7992 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7993 The difference between N and the actual number of articles marked is
7994 returned."
7995   (interactive "p")
7996   (gnus-summary-show-thread)
7997   (let ((backward (< n 0))
7998         (gnus-summary-goto-unread
7999          (and gnus-summary-goto-unread
8000               (not (eq gnus-summary-goto-unread 'never))
8001               (not (memq mark (list gnus-unread-mark
8002                                     gnus-ticked-mark gnus-dormant-mark)))))
8003         (n (abs n))
8004         (mark (or mark gnus-del-mark)))
8005     (while (and (> n 0)
8006                 (gnus-summary-mark-article nil mark no-expire)
8007                 (zerop (gnus-summary-next-subject
8008                         (if backward -1 1)
8009                         (and gnus-summary-goto-unread
8010                              (not (eq gnus-summary-goto-unread 'never)))
8011                         t)))
8012       (setq n (1- n)))
8013     (when (/= 0 n)
8014       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8015     (gnus-summary-recenter)
8016     (gnus-summary-position-point)
8017     (gnus-set-mode-line 'summary)
8018     n))
8019
8020 (defun gnus-summary-mark-article-as-read (mark)
8021   "Mark the current article quickly as read with MARK."
8022   (let ((article (gnus-summary-article-number)))
8023     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8024     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8025     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8026     (push (cons article mark) gnus-newsgroup-reads)
8027     ;; Possibly remove from cache, if that is used.
8028     (when gnus-use-cache
8029       (gnus-cache-enter-remove-article article))
8030     ;; Allow the backend to change the mark.
8031     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8032     ;; Check for auto-expiry.
8033     (when (and gnus-newsgroup-auto-expire
8034                (memq mark gnus-auto-expirable-marks))
8035       (setq mark gnus-expirable-mark)
8036       ;; Let the backend know about the mark change.
8037       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8038       (push article gnus-newsgroup-expirable))
8039     ;; Set the mark in the buffer.
8040     (gnus-summary-update-mark mark 'unread)
8041     t))
8042
8043 (defun gnus-summary-mark-article-as-unread (mark)
8044   "Mark the current article quickly as unread with MARK."
8045   (let* ((article (gnus-summary-article-number))
8046          (old-mark (gnus-summary-article-mark article)))
8047     ;; Allow the backend to change the mark.
8048     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8049     (if (eq mark old-mark)
8050         t
8051       (if (<= article 0)
8052           (progn
8053             (gnus-error 1 "Can't mark negative article numbers")
8054             nil)
8055         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8056         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8057         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8058         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8059         (cond ((= mark gnus-ticked-mark)
8060                (push article gnus-newsgroup-marked))
8061               ((= mark gnus-dormant-mark)
8062                (push article gnus-newsgroup-dormant))
8063               (t
8064                (push article gnus-newsgroup-unreads)))
8065         (gnus-pull article gnus-newsgroup-reads)
8066
8067         ;; See whether the article is to be put in the cache.
8068         (and gnus-use-cache
8069              (vectorp (gnus-summary-article-header article))
8070              (save-excursion
8071                (gnus-cache-possibly-enter-article
8072                 gnus-newsgroup-name article
8073                 (= mark gnus-ticked-mark)
8074                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8075
8076         ;; Fix the mark.
8077         (gnus-summary-update-mark mark 'unread)
8078         t))))
8079
8080 (defun gnus-summary-mark-article (&optional article mark no-expire)
8081   "Mark ARTICLE with MARK.  MARK can be any character.
8082 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8083 `??' (dormant) and `?E' (expirable).
8084 If MARK is nil, then the default character `?r' is used.
8085 If ARTICLE is nil, then the article on the current line will be
8086 marked."
8087   ;; The mark might be a string.
8088   (when (stringp mark)
8089     (setq mark (aref mark 0)))
8090   ;; If no mark is given, then we check auto-expiring.
8091   (when (null mark)
8092     (setq mark gnus-del-mark))
8093   (when (and (not no-expire)
8094              gnus-newsgroup-auto-expire
8095              (memq mark gnus-auto-expirable-marks))
8096     (setq mark gnus-expirable-mark))
8097   (let ((article (or article (gnus-summary-article-number)))
8098         (old-mark (gnus-summary-article-mark article)))
8099     ;; Allow the backend to change the mark.
8100     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8101     (if (eq mark old-mark)
8102         t
8103       (unless article
8104         (error "No article on current line"))
8105       (if (not (if (or (= mark gnus-unread-mark)
8106                        (= mark gnus-ticked-mark)
8107                        (= mark gnus-dormant-mark))
8108                    (gnus-mark-article-as-unread article mark)
8109                  (gnus-mark-article-as-read article mark)))
8110           t
8111         ;; See whether the article is to be put in the cache.
8112         (and gnus-use-cache
8113              (not (= mark gnus-canceled-mark))
8114              (vectorp (gnus-summary-article-header article))
8115              (save-excursion
8116                (gnus-cache-possibly-enter-article
8117                 gnus-newsgroup-name article
8118                 (= mark gnus-ticked-mark)
8119                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8120
8121         (when (gnus-summary-goto-subject article nil t)
8122           (let ((buffer-read-only nil))
8123             (gnus-summary-show-thread)
8124             ;; Fix the mark.
8125             (gnus-summary-update-mark mark 'unread)
8126             t))))))
8127
8128 (defun gnus-summary-update-secondary-mark (article)
8129   "Update the secondary (read, process, cache) mark."
8130   (gnus-summary-update-mark
8131    (cond ((memq article gnus-newsgroup-processable)
8132           gnus-process-mark)
8133          ((memq article gnus-newsgroup-cached)
8134           gnus-cached-mark)
8135          ((memq article gnus-newsgroup-replied)
8136           gnus-replied-mark)
8137          ((memq article gnus-newsgroup-saved)
8138           gnus-saved-mark)
8139          (t gnus-unread-mark))
8140    'replied)
8141   (when (gnus-visual-p 'summary-highlight 'highlight)
8142     (gnus-run-hooks 'gnus-summary-update-hook))
8143   t)
8144
8145 (defun gnus-summary-update-mark (mark type)
8146   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8147         (buffer-read-only nil))
8148     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8149     (when forward
8150       (when (looking-at "\r")
8151         (incf forward))
8152       (when (<= (+ forward (point)) (point-max))
8153         ;; Go to the right position on the line.
8154         (goto-char (+ forward (point)))
8155         ;; Replace the old mark with the new mark.
8156         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8157         ;; Optionally update the marks by some user rule.
8158         (when (eq type 'unread)
8159           (gnus-data-set-mark
8160            (gnus-data-find (gnus-summary-article-number)) mark)
8161           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8162
8163 (defun gnus-mark-article-as-read (article &optional mark)
8164   "Enter ARTICLE in the pertinent lists and remove it from others."
8165   ;; Make the article expirable.
8166   (let ((mark (or mark gnus-del-mark)))
8167     (if (= mark gnus-expirable-mark)
8168         (push article gnus-newsgroup-expirable)
8169       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8170     ;; Remove from unread and marked lists.
8171     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8172     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8173     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8174     (push (cons article mark) gnus-newsgroup-reads)
8175     ;; Possibly remove from cache, if that is used.
8176     (when gnus-use-cache
8177       (gnus-cache-enter-remove-article article))
8178     t))
8179
8180 (defun gnus-mark-article-as-unread (article &optional mark)
8181   "Enter ARTICLE in the pertinent lists and remove it from others."
8182   (let ((mark (or mark gnus-ticked-mark)))
8183     (if (<= article 0)
8184         (progn
8185           (gnus-error 1 "Can't mark negative article numbers")
8186           nil)
8187       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8188             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8189             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8190             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8191
8192       ;; Unsuppress duplicates?
8193       (when gnus-suppress-duplicates
8194         (gnus-dup-unsuppress-article article))
8195
8196       (cond ((= mark gnus-ticked-mark)
8197              (push article gnus-newsgroup-marked))
8198             ((= mark gnus-dormant-mark)
8199              (push article gnus-newsgroup-dormant))
8200             (t
8201              (push article gnus-newsgroup-unreads)))
8202       (gnus-pull article gnus-newsgroup-reads)
8203       t)))
8204
8205 (defalias 'gnus-summary-mark-as-unread-forward
8206   'gnus-summary-tick-article-forward)
8207 (make-obsolete 'gnus-summary-mark-as-unread-forward
8208                'gnus-summary-tick-article-forward)
8209 (defun gnus-summary-tick-article-forward (n)
8210   "Tick N articles forwards.
8211 If N is negative, tick backwards instead.
8212 The difference between N and the number of articles ticked is returned."
8213   (interactive "p")
8214   (gnus-summary-mark-forward n gnus-ticked-mark))
8215
8216 (defalias 'gnus-summary-mark-as-unread-backward
8217   'gnus-summary-tick-article-backward)
8218 (make-obsolete 'gnus-summary-mark-as-unread-backward
8219                'gnus-summary-tick-article-backward)
8220 (defun gnus-summary-tick-article-backward (n)
8221   "Tick N articles backwards.
8222 The difference between N and the number of articles ticked is returned."
8223   (interactive "p")
8224   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8225
8226 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8227 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8228 (defun gnus-summary-tick-article (&optional article clear-mark)
8229   "Mark current article as unread.
8230 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8231 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8232   (interactive)
8233   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8234                                        gnus-ticked-mark)))
8235
8236 (defun gnus-summary-mark-as-read-forward (n)
8237   "Mark N articles as read forwards.
8238 If N is negative, mark backwards instead.
8239 The difference between N and the actual number of articles marked is
8240 returned."
8241   (interactive "p")
8242   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8243
8244 (defun gnus-summary-mark-as-read-backward (n)
8245   "Mark the N articles as read backwards.
8246 The difference between N and the actual number of articles marked is
8247 returned."
8248   (interactive "p")
8249   (gnus-summary-mark-forward
8250    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8251
8252 (defun gnus-summary-mark-as-read (&optional article mark)
8253   "Mark current article as read.
8254 ARTICLE specifies the article to be marked as read.
8255 MARK specifies a string to be inserted at the beginning of the line."
8256   (gnus-summary-mark-article article mark))
8257
8258 (defun gnus-summary-clear-mark-forward (n)
8259   "Clear marks from N articles forward.
8260 If N is negative, clear backward instead.
8261 The difference between N and the number of marks cleared is returned."
8262   (interactive "p")
8263   (gnus-summary-mark-forward n gnus-unread-mark))
8264
8265 (defun gnus-summary-clear-mark-backward (n)
8266   "Clear marks from N articles backward.
8267 The difference between N and the number of marks cleared is returned."
8268   (interactive "p")
8269   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8270
8271 (defun gnus-summary-mark-unread-as-read ()
8272   "Intended to be used by `gnus-summary-mark-article-hook'."
8273   (when (memq gnus-current-article gnus-newsgroup-unreads)
8274     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8275
8276 (defun gnus-summary-mark-read-and-unread-as-read ()
8277   "Intended to be used by `gnus-summary-mark-article-hook'."
8278   (let ((mark (gnus-summary-article-mark)))
8279     (when (or (gnus-unread-mark-p mark)
8280               (gnus-read-mark-p mark))
8281       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8282
8283 (defun gnus-summary-mark-region-as-read (point mark all)
8284   "Mark all unread articles between point and mark as read.
8285 If given a prefix, mark all articles between point and mark as read,
8286 even ticked and dormant ones."
8287   (interactive "r\nP")
8288   (save-excursion
8289     (let (article)
8290       (goto-char point)
8291       (beginning-of-line)
8292       (while (and
8293               (< (point) mark)
8294               (progn
8295                 (when (or all
8296                           (memq (setq article (gnus-summary-article-number))
8297                                 gnus-newsgroup-unreads))
8298                   (gnus-summary-mark-article article gnus-del-mark))
8299                 t)
8300               (gnus-summary-find-next))))))
8301
8302 (defun gnus-summary-mark-below (score mark)
8303   "Mark articles with score less than SCORE with MARK."
8304   (interactive "P\ncMark: ")
8305   (setq score (if score
8306                   (prefix-numeric-value score)
8307                 (or gnus-summary-default-score 0)))
8308   (save-excursion
8309     (set-buffer gnus-summary-buffer)
8310     (goto-char (point-min))
8311     (while
8312         (progn
8313           (and (< (gnus-summary-article-score) score)
8314                (gnus-summary-mark-article nil mark))
8315           (gnus-summary-find-next)))))
8316
8317 (defun gnus-summary-kill-below (&optional score)
8318   "Mark articles with score below SCORE as read."
8319   (interactive "P")
8320   (gnus-summary-mark-below score gnus-killed-mark))
8321
8322 (defun gnus-summary-clear-above (&optional score)
8323   "Clear all marks from articles with score above SCORE."
8324   (interactive "P")
8325   (gnus-summary-mark-above score gnus-unread-mark))
8326
8327 (defun gnus-summary-tick-above (&optional score)
8328   "Tick all articles with score above SCORE."
8329   (interactive "P")
8330   (gnus-summary-mark-above score gnus-ticked-mark))
8331
8332 (defun gnus-summary-mark-above (score mark)
8333   "Mark articles with score over SCORE with MARK."
8334   (interactive "P\ncMark: ")
8335   (setq score (if score
8336                   (prefix-numeric-value score)
8337                 (or gnus-summary-default-score 0)))
8338   (save-excursion
8339     (set-buffer gnus-summary-buffer)
8340     (goto-char (point-min))
8341     (while (and (progn
8342                   (when (> (gnus-summary-article-score) score)
8343                     (gnus-summary-mark-article nil mark))
8344                   t)
8345                 (gnus-summary-find-next)))))
8346
8347 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8348 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8349 (defun gnus-summary-limit-include-expunged (&optional no-error)
8350   "Display all the hidden articles that were expunged for low scores."
8351   (interactive)
8352   (let ((buffer-read-only nil))
8353     (let ((scored gnus-newsgroup-scored)
8354           headers h)
8355       (while scored
8356         (unless (gnus-summary-goto-subject (caar scored))
8357           (and (setq h (gnus-summary-article-header (caar scored)))
8358                (< (cdar scored) gnus-summary-expunge-below)
8359                (push h headers)))
8360         (setq scored (cdr scored)))
8361       (if (not headers)
8362           (when (not no-error)
8363             (error "No expunged articles hidden"))
8364         (goto-char (point-min))
8365         (gnus-summary-prepare-unthreaded (nreverse headers))
8366         (goto-char (point-min))
8367         (gnus-summary-position-point)
8368         t))))
8369
8370 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8371   "Mark all unread articles in this newsgroup as read.
8372 If prefix argument ALL is non-nil, ticked and dormant articles will
8373 also be marked as read.
8374 If QUIETLY is non-nil, no questions will be asked.
8375 If TO-HERE is non-nil, it should be a point in the buffer.  All
8376 articles before this point will be marked as read.
8377 Note that this function will only catch up the unread article
8378 in the current summary buffer limitation.
8379 The number of articles marked as read is returned."
8380   (interactive "P")
8381   (prog1
8382       (save-excursion
8383         (when (or quietly
8384                   (not gnus-interactive-catchup) ;Without confirmation?
8385                   gnus-expert-user
8386                   (gnus-y-or-n-p
8387                    (if all
8388                        "Mark absolutely all articles as read? "
8389                      "Mark all unread articles as read? ")))
8390           (if (and not-mark
8391                    (not gnus-newsgroup-adaptive)
8392                    (not gnus-newsgroup-auto-expire)
8393                    (not gnus-suppress-duplicates)
8394                    (or (not gnus-use-cache)
8395                        (eq gnus-use-cache 'passive)))
8396               (progn
8397                 (when all
8398                   (setq gnus-newsgroup-marked nil
8399                         gnus-newsgroup-dormant nil))
8400                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8401             ;; We actually mark all articles as canceled, which we
8402             ;; have to do when using auto-expiry or adaptive scoring.
8403             (gnus-summary-show-all-threads)
8404             (when (gnus-summary-first-subject (not all) t)
8405               (while (and
8406                       (if to-here (< (point) to-here) t)
8407                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8408                       (gnus-summary-find-next (not all) nil nil t))))
8409             (gnus-set-mode-line 'summary))
8410           t))
8411     (gnus-summary-position-point)))
8412
8413 (defun gnus-summary-catchup-to-here (&optional all)
8414   "Mark all unticked articles before the current one as read.
8415 If ALL is non-nil, also mark ticked and dormant articles as read."
8416   (interactive "P")
8417   (save-excursion
8418     (gnus-save-hidden-threads
8419       (let ((beg (point)))
8420         ;; We check that there are unread articles.
8421         (when (or all (gnus-summary-find-prev))
8422           (gnus-summary-catchup all t beg)))))
8423   (gnus-summary-position-point))
8424
8425 (defun gnus-summary-catchup-all (&optional quietly)
8426   "Mark all articles in this newsgroup as read."
8427   (interactive "P")
8428   (gnus-summary-catchup t quietly))
8429
8430 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8431   "Mark all unread articles in this group as read, then exit.
8432 If prefix argument ALL is non-nil, all articles are marked as read."
8433   (interactive "P")
8434   (when (gnus-summary-catchup all quietly nil 'fast)
8435     ;; Select next newsgroup or exit.
8436     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8437              (eq gnus-auto-select-next 'quietly))
8438         (gnus-summary-next-group nil)
8439       (gnus-summary-exit))))
8440
8441 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8442   "Mark all articles in this newsgroup as read, and then exit."
8443   (interactive "P")
8444   (gnus-summary-catchup-and-exit t quietly))
8445
8446 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8447   "Mark all articles in this group as read and select the next group.
8448 If given a prefix, mark all articles, unread as well as ticked, as
8449 read."
8450   (interactive "P")
8451   (save-excursion
8452     (gnus-summary-catchup all))
8453   (gnus-summary-next-group))
8454
8455 ;; Thread-based commands.
8456
8457 (defun gnus-summary-articles-in-thread (&optional article)
8458   "Return a list of all articles in the current thread.
8459 If ARTICLE is non-nil, return all articles in the thread that starts
8460 with that article."
8461   (let* ((article (or article (gnus-summary-article-number)))
8462          (data (gnus-data-find-list article))
8463          (top-level (gnus-data-level (car data)))
8464          (top-subject
8465           (cond ((null gnus-thread-operation-ignore-subject)
8466                  (gnus-simplify-subject-re
8467                   (mail-header-subject (gnus-data-header (car data)))))
8468                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8469                  (gnus-simplify-subject-fuzzy
8470                   (mail-header-subject (gnus-data-header (car data)))))
8471                 (t nil)))
8472          (end-point (save-excursion
8473                       (if (gnus-summary-go-to-next-thread)
8474                           (point) (point-max))))
8475          articles)
8476     (while (and data
8477                 (< (gnus-data-pos (car data)) end-point))
8478       (when (or (not top-subject)
8479                 (string= top-subject
8480                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8481                              (gnus-simplify-subject-fuzzy
8482                               (mail-header-subject
8483                                (gnus-data-header (car data))))
8484                            (gnus-simplify-subject-re
8485                             (mail-header-subject
8486                              (gnus-data-header (car data)))))))
8487         (push (gnus-data-number (car data)) articles))
8488       (unless (and (setq data (cdr data))
8489                    (> (gnus-data-level (car data)) top-level))
8490         (setq data nil)))
8491     ;; Return the list of articles.
8492     (nreverse articles)))
8493
8494 (defun gnus-summary-rethread-current ()
8495   "Rethread the thread the current article is part of."
8496   (interactive)
8497   (let* ((gnus-show-threads t)
8498          (article (gnus-summary-article-number))
8499          (id (mail-header-id (gnus-summary-article-header)))
8500          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8501     (unless id
8502       (error "No article on the current line"))
8503     (gnus-rebuild-thread id)
8504     (gnus-summary-goto-subject article)))
8505
8506 (defun gnus-summary-reparent-thread ()
8507   "Make the current article child of the marked (or previous) article.
8508
8509 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8510 is non-nil or the Subject: of both articles are the same."
8511   (interactive)
8512   (unless (not (gnus-group-read-only-p))
8513     (error "The current newsgroup does not support article editing"))
8514   (unless (<= (length gnus-newsgroup-processable) 1)
8515     (error "No more than one article may be marked"))
8516   (save-window-excursion
8517     (let ((gnus-article-buffer " *reparent*")
8518           (current-article (gnus-summary-article-number))
8519           ;; First grab the marked article, otherwise one line up.
8520           (parent-article (if (not (null gnus-newsgroup-processable))
8521                               (car gnus-newsgroup-processable)
8522                             (save-excursion
8523                               (if (eq (forward-line -1) 0)
8524                                   (gnus-summary-article-number)
8525                                 (error "Beginning of summary buffer"))))))
8526       (unless (not (eq current-article parent-article))
8527         (error "An article may not be self-referential"))
8528       (let ((message-id (mail-header-id
8529                          (gnus-summary-article-header parent-article))))
8530         (unless (and message-id (not (equal message-id "")))
8531           (error "No message-id in desired parent"))
8532         (gnus-with-article current-article
8533           (goto-char (point-min))
8534           (if (re-search-forward "^References: " nil t)
8535               (progn
8536                 (re-search-forward "^[^ \t]" nil t)
8537                 (forward-line -1)
8538                 (end-of-line)
8539                 (insert " " message-id))
8540             (insert "References: " message-id "\n")))
8541         (set-buffer gnus-summary-buffer)
8542         (gnus-summary-unmark-all-processable)
8543         (gnus-summary-update-article current-article)
8544         (gnus-summary-rethread-current)
8545         (gnus-message 3 "Article %d is now the child of article %d"
8546                       current-article parent-article)))))
8547
8548 (defun gnus-summary-toggle-threads (&optional arg)
8549   "Toggle showing conversation threads.
8550 If ARG is positive number, turn showing conversation threads on."
8551   (interactive "P")
8552   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8553     (setq gnus-show-threads
8554           (if (null arg) (not gnus-show-threads)
8555             (> (prefix-numeric-value arg) 0)))
8556     (gnus-summary-prepare)
8557     (gnus-summary-goto-subject current)
8558     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8559     (gnus-summary-position-point)))
8560
8561 (defun gnus-summary-show-all-threads ()
8562   "Show all threads."
8563   (interactive)
8564   (save-excursion
8565     (let ((buffer-read-only nil))
8566       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8567   (gnus-summary-position-point))
8568
8569 (defun gnus-summary-show-thread ()
8570   "Show thread subtrees.
8571 Returns nil if no thread was there to be shown."
8572   (interactive)
8573   (let ((buffer-read-only nil)
8574         (orig (point))
8575         ;; first goto end then to beg, to have point at beg after let
8576         (end (progn (end-of-line) (point)))
8577         (beg (progn (beginning-of-line) (point))))
8578     (prog1
8579         ;; Any hidden lines here?
8580         (search-forward "\r" end t)
8581       (subst-char-in-region beg end ?\^M ?\n t)
8582       (goto-char orig)
8583       (gnus-summary-position-point))))
8584
8585 (defun gnus-summary-hide-all-threads ()
8586   "Hide all thread subtrees."
8587   (interactive)
8588   (save-excursion
8589     (goto-char (point-min))
8590     (gnus-summary-hide-thread)
8591     (while (zerop (gnus-summary-next-thread 1 t))
8592       (gnus-summary-hide-thread)))
8593   (gnus-summary-position-point))
8594
8595 (defun gnus-summary-hide-thread ()
8596   "Hide thread subtrees.
8597 Returns nil if no threads were there to be hidden."
8598   (interactive)
8599   (let ((buffer-read-only nil)
8600         (start (point))
8601         (article (gnus-summary-article-number)))
8602     (goto-char start)
8603     ;; Go forward until either the buffer ends or the subthread
8604     ;; ends.
8605     (when (and (not (eobp))
8606                (or (zerop (gnus-summary-next-thread 1 t))
8607                    (goto-char (point-max))))
8608       (prog1
8609           (if (and (> (point) start)
8610                    (search-backward "\n" start t))
8611               (progn
8612                 (subst-char-in-region start (point) ?\n ?\^M)
8613                 (gnus-summary-goto-subject article))
8614             (goto-char start)
8615             nil)
8616         ;;(gnus-summary-position-point)
8617         ))))
8618
8619 (defun gnus-summary-go-to-next-thread (&optional previous)
8620   "Go to the same level (or less) next thread.
8621 If PREVIOUS is non-nil, go to previous thread instead.
8622 Return the article number moved to, or nil if moving was impossible."
8623   (let ((level (gnus-summary-thread-level))
8624         (way (if previous -1 1))
8625         (beg (point)))
8626     (forward-line way)
8627     (while (and (not (eobp))
8628                 (< level (gnus-summary-thread-level)))
8629       (forward-line way))
8630     (if (eobp)
8631         (progn
8632           (goto-char beg)
8633           nil)
8634       (setq beg (point))
8635       (prog1
8636           (gnus-summary-article-number)
8637         (goto-char beg)))))
8638
8639 (defun gnus-summary-next-thread (n &optional silent)
8640   "Go to the same level next N'th thread.
8641 If N is negative, search backward instead.
8642 Returns the difference between N and the number of skips actually
8643 done.
8644
8645 If SILENT, don't output messages."
8646   (interactive "p")
8647   (let ((backward (< n 0))
8648         (n (abs n)))
8649     (while (and (> n 0)
8650                 (gnus-summary-go-to-next-thread backward))
8651       (decf n))
8652     (unless silent
8653       (gnus-summary-position-point))
8654     (when (and (not silent) (/= 0 n))
8655       (gnus-message 7 "No more threads"))
8656     n))
8657
8658 (defun gnus-summary-prev-thread (n)
8659   "Go to the same level previous N'th thread.
8660 Returns the difference between N and the number of skips actually
8661 done."
8662   (interactive "p")
8663   (gnus-summary-next-thread (- n)))
8664
8665 (defun gnus-summary-go-down-thread ()
8666   "Go down one level in the current thread."
8667   (let ((children (gnus-summary-article-children)))
8668     (when children
8669       (gnus-summary-goto-subject (car children)))))
8670
8671 (defun gnus-summary-go-up-thread ()
8672   "Go up one level in the current thread."
8673   (let ((parent (gnus-summary-article-parent)))
8674     (when parent
8675       (gnus-summary-goto-subject parent))))
8676
8677 (defun gnus-summary-down-thread (n)
8678   "Go down thread N steps.
8679 If N is negative, go up instead.
8680 Returns the difference between N and how many steps down that were
8681 taken."
8682   (interactive "p")
8683   (let ((up (< n 0))
8684         (n (abs n)))
8685     (while (and (> n 0)
8686                 (if up (gnus-summary-go-up-thread)
8687                   (gnus-summary-go-down-thread)))
8688       (setq n (1- n)))
8689     (gnus-summary-position-point)
8690     (when (/= 0 n)
8691       (gnus-message 7 "Can't go further"))
8692     n))
8693
8694 (defun gnus-summary-up-thread (n)
8695   "Go up thread N steps.
8696 If N is negative, go up instead.
8697 Returns the difference between N and how many steps down that were
8698 taken."
8699   (interactive "p")
8700   (gnus-summary-down-thread (- n)))
8701
8702 (defun gnus-summary-top-thread ()
8703   "Go to the top of the thread."
8704   (interactive)
8705   (while (gnus-summary-go-up-thread))
8706   (gnus-summary-article-number))
8707
8708 (defun gnus-summary-kill-thread (&optional unmark)
8709   "Mark articles under current thread as read.
8710 If the prefix argument is positive, remove any kinds of marks.
8711 If the prefix argument is negative, tick articles instead."
8712   (interactive "P")
8713   (when unmark
8714     (setq unmark (prefix-numeric-value unmark)))
8715   (let ((articles (gnus-summary-articles-in-thread)))
8716     (save-excursion
8717       ;; Expand the thread.
8718       (gnus-summary-show-thread)
8719       ;; Mark all the articles.
8720       (while articles
8721         (gnus-summary-goto-subject (car articles))
8722         (cond ((null unmark)
8723                (gnus-summary-mark-article-as-read gnus-killed-mark))
8724               ((> unmark 0)
8725                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8726               (t
8727                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8728         (setq articles (cdr articles))))
8729     ;; Hide killed subtrees.
8730     (and (null unmark)
8731          gnus-thread-hide-killed
8732          (gnus-summary-hide-thread))
8733     ;; If marked as read, go to next unread subject.
8734     (when (null unmark)
8735       ;; Go to next unread subject.
8736       (gnus-summary-next-subject 1 t)))
8737   (gnus-set-mode-line 'summary))
8738
8739 ;; Summary sorting commands
8740
8741 (defun gnus-summary-sort-by-number (&optional reverse)
8742   "Sort the summary buffer by article number.
8743 Argument REVERSE means reverse order."
8744   (interactive "P")
8745   (gnus-summary-sort 'number reverse))
8746
8747 (defun gnus-summary-sort-by-author (&optional reverse)
8748   "Sort the summary buffer by author name alphabetically.
8749 If case-fold-search is non-nil, case of letters is ignored.
8750 Argument REVERSE means reverse order."
8751   (interactive "P")
8752   (gnus-summary-sort 'author reverse))
8753
8754 (defun gnus-summary-sort-by-subject (&optional reverse)
8755   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8756 If case-fold-search is non-nil, case of letters is ignored.
8757 Argument REVERSE means reverse order."
8758   (interactive "P")
8759   (gnus-summary-sort 'subject reverse))
8760
8761 (defun gnus-summary-sort-by-date (&optional reverse)
8762   "Sort the summary buffer by date.
8763 Argument REVERSE means reverse order."
8764   (interactive "P")
8765   (gnus-summary-sort 'date reverse))
8766
8767 (defun gnus-summary-sort-by-score (&optional reverse)
8768   "Sort the summary buffer by score.
8769 Argument REVERSE means reverse order."
8770   (interactive "P")
8771   (gnus-summary-sort 'score reverse))
8772
8773 (defun gnus-summary-sort-by-lines (&optional reverse)
8774   "Sort the summary buffer by the number of lines.
8775 Argument REVERSE means reverse order."
8776   (interactive "P")
8777   (gnus-summary-sort 'lines reverse))
8778
8779 (defun gnus-summary-sort-by-chars (&optional reverse)
8780   "Sort the summary buffer by article length.
8781 Argument REVERSE means reverse order."
8782   (interactive "P")
8783   (gnus-summary-sort 'chars reverse))   
8784
8785 (defun gnus-summary-sort (predicate reverse)
8786   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8787   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8788          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8789          (gnus-thread-sort-functions
8790           (if (not reverse)
8791               thread
8792             `(lambda (t1 t2)
8793                (,thread t2 t1))))
8794          (gnus-article-sort-functions
8795           (if (not reverse)
8796               article
8797             `(lambda (t1 t2)
8798                (,article t2 t1))))
8799          (buffer-read-only)
8800          (gnus-summary-prepare-hook nil))
8801     ;; We do the sorting by regenerating the threads.
8802     (gnus-summary-prepare)
8803     ;; Hide subthreads if needed.
8804     (when (and gnus-show-threads gnus-thread-hide-subtree)
8805       (gnus-summary-hide-all-threads))))
8806
8807 ;; Summary saving commands.
8808
8809 (defun gnus-summary-save-article (&optional n not-saved)
8810   "Save the current article using the default saver function.
8811 If N is a positive number, save the N next articles.
8812 If N is a negative number, save the N previous articles.
8813 If N is nil and any articles have been marked with the process mark,
8814 save those articles instead.
8815 The variable `gnus-default-article-saver' specifies the saver function."
8816   (interactive "P")
8817   (let* ((articles (gnus-summary-work-articles n))
8818          (save-buffer (save-excursion
8819                         (nnheader-set-temp-buffer " *Gnus Save*")))
8820          (num (length articles))
8821          header file)
8822     (dolist (article articles)
8823       (setq header (gnus-summary-article-header article))
8824       (if (not (vectorp header))
8825           ;; This is a pseudo-article.
8826           (if (assq 'name header)
8827               (gnus-copy-file (cdr (assq 'name header)))
8828             (gnus-message 1 "Article %d is unsaveable" article))
8829         ;; This is a real article.
8830         (save-window-excursion
8831           (gnus-summary-select-article t nil nil article))
8832         (save-excursion
8833           (set-buffer save-buffer)
8834           (erase-buffer)
8835           (insert-buffer-substring gnus-original-article-buffer))
8836         (setq file (gnus-article-save save-buffer file num))
8837         (gnus-summary-remove-process-mark article)
8838         (unless not-saved
8839           (gnus-summary-set-saved-mark article))))
8840     (gnus-kill-buffer save-buffer)
8841     (gnus-summary-position-point)
8842     (gnus-set-mode-line 'summary)
8843     n))
8844
8845 (defun gnus-summary-pipe-output (&optional arg)
8846   "Pipe the current article to a subprocess.
8847 If N is a positive number, pipe the N next articles.
8848 If N is a negative number, pipe the N previous articles.
8849 If N is nil and any articles have been marked with the process mark,
8850 pipe those articles instead."
8851   (interactive "P")
8852   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8853     (gnus-summary-save-article arg t))
8854   (gnus-configure-windows 'pipe))
8855
8856 (defun gnus-summary-save-article-mail (&optional arg)
8857   "Append the current article to an mail file.
8858 If N is a positive number, save the N next articles.
8859 If N is a negative number, save the N previous articles.
8860 If N is nil and any articles have been marked with the process mark,
8861 save those articles instead."
8862   (interactive "P")
8863   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8864     (gnus-summary-save-article arg)))
8865
8866 (defun gnus-summary-save-article-rmail (&optional arg)
8867   "Append the current article to an rmail file.
8868 If N is a positive number, save the N next articles.
8869 If N is a negative number, save the N previous articles.
8870 If N is nil and any articles have been marked with the process mark,
8871 save those articles instead."
8872   (interactive "P")
8873   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8874     (gnus-summary-save-article arg)))
8875
8876 (defun gnus-summary-save-article-file (&optional arg)
8877   "Append the current article to a file.
8878 If N is a positive number, save the N next articles.
8879 If N is a negative number, save the N previous articles.
8880 If N is nil and any articles have been marked with the process mark,
8881 save those articles instead."
8882   (interactive "P")
8883   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8884     (gnus-summary-save-article arg)))
8885
8886 (defun gnus-summary-write-article-file (&optional arg)
8887   "Write the current article to a file, deleting the previous file.
8888 If N is a positive number, save the N next articles.
8889 If N is a negative number, save the N previous articles.
8890 If N is nil and any articles have been marked with the process mark,
8891 save those articles instead."
8892   (interactive "P")
8893   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8894     (gnus-summary-save-article arg)))
8895
8896 (defun gnus-summary-save-article-body-file (&optional arg)
8897   "Append the current article body to a file.
8898 If N is a positive number, save the N next articles.
8899 If N is a negative number, save the N previous articles.
8900 If N is nil and any articles have been marked with the process mark,
8901 save those articles instead."
8902   (interactive "P")
8903   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8904     (gnus-summary-save-article arg)))
8905
8906 (defun gnus-summary-pipe-message (program)
8907   "Pipe the current article through PROGRAM."
8908   (interactive "sProgram: ")
8909   (gnus-summary-select-article)
8910   (let ((mail-header-separator ""))
8911     (gnus-eval-in-buffer-window gnus-article-buffer
8912       (save-restriction
8913         (widen)
8914         (let ((start (window-start))
8915               buffer-read-only)
8916           (message-pipe-buffer-body program)
8917           (set-window-start (get-buffer-window (current-buffer)) start))))))
8918
8919 (defun gnus-get-split-value (methods)
8920   "Return a value based on the split METHODS."
8921   (let (split-name method result match)
8922     (when methods
8923       (save-excursion
8924         (set-buffer gnus-original-article-buffer)
8925         (save-restriction
8926           (nnheader-narrow-to-headers)
8927           (while methods
8928             (goto-char (point-min))
8929             (setq method (pop methods))
8930             (setq match (car method))
8931             (when (cond
8932                    ((stringp match)
8933                     ;; Regular expression.
8934                     (ignore-errors
8935                       (re-search-forward match nil t)))
8936                    ((gnus-functionp match)
8937                     ;; Function.
8938                     (save-restriction
8939                       (widen)
8940                       (setq result (funcall match gnus-newsgroup-name))))
8941                    ((consp match)
8942                     ;; Form.
8943                     (save-restriction
8944                       (widen)
8945                       (setq result (eval match)))))
8946               (setq split-name (append (cdr method) split-name))
8947               (cond ((stringp result)
8948                      (push (expand-file-name
8949                             result gnus-article-save-directory)
8950                            split-name))
8951                     ((consp result)
8952                      (setq split-name (append result split-name)))))))))
8953     (nreverse split-name)))
8954
8955 (defun gnus-valid-move-group-p (group)
8956   (and (boundp group)
8957        (symbol-name group)
8958        (symbol-value group)
8959        (gnus-get-function (gnus-find-method-for-group
8960                            (symbol-name group)) 'request-accept-article t)))
8961
8962 (defun gnus-read-move-group-name (prompt default articles prefix)
8963   "Read a group name."
8964   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8965          (minibuffer-confirm-incomplete nil) ; XEmacs
8966          (prom
8967           (format "%s %s to:"
8968                   prompt
8969                   (if (> (length articles) 1)
8970                       (format "these %d articles" (length articles))
8971                     "this article")))
8972          (to-newsgroup
8973           (cond
8974            ((null split-name)
8975             (gnus-completing-read default prom
8976                                   gnus-active-hashtb
8977                                   'gnus-valid-move-group-p
8978                                   nil prefix
8979                                   'gnus-group-history))
8980            ((= 1 (length split-name))
8981             (gnus-completing-read (car split-name) prom
8982                                   gnus-active-hashtb
8983                                   'gnus-valid-move-group-p
8984                                   nil nil
8985                                   'gnus-group-history))
8986            (t
8987             (gnus-completing-read nil prom
8988                                   (mapcar (lambda (el) (list el))
8989                                           (nreverse split-name))
8990                                   nil nil nil
8991                                   'gnus-group-history)))))
8992     (when to-newsgroup
8993       (if (or (string= to-newsgroup "")
8994               (string= to-newsgroup prefix))
8995           (setq to-newsgroup default))
8996       (unless to-newsgroup
8997         (error "No group name entered"))
8998       (or (gnus-active to-newsgroup)
8999           (gnus-activate-group to-newsgroup)
9000           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9001                                      to-newsgroup))
9002               (or (and (gnus-request-create-group
9003                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9004                        (gnus-activate-group
9005                         to-newsgroup nil nil
9006                         (gnus-group-name-to-method to-newsgroup))
9007                        (gnus-subscribe-group to-newsgroup))
9008                   (error "Couldn't create group %s" to-newsgroup)))
9009           (error "No such group: %s" to-newsgroup)))
9010     to-newsgroup))
9011
9012 (defun gnus-summary-save-parts (type dir n reverse)
9013   "Save parts matching TYPE to DIR.
9014 If REVERSE, save parts that do not match TYPE."
9015   (interactive
9016    (list (read-string "Save parts of type: " "image/.*")
9017          (read-file-name "Save to directory: " t nil t)
9018          current-prefix-arg))
9019   (gnus-summary-iterate n
9020     (let ((gnus-display-mime-function nil)
9021           (gnus-inhibit-treatment t))
9022       (gnus-summary-select-article))
9023     (save-excursion
9024       (set-buffer gnus-article-buffer)
9025       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9026         (when handles
9027           (gnus-summary-save-parts-1 type dir handles reverse)
9028           (mm-destroy-parts handles))))))
9029
9030 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9031   (if (stringp (car handle))
9032       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9033               (cdr handle))
9034     (when (if reverse
9035               (not (string-match type (mm-handle-media-type handle)))
9036             (string-match type (mm-handle-media-type handle)))
9037       (let ((file (expand-file-name
9038                    (file-name-nondirectory
9039                     (or
9040                      (mail-content-type-get
9041                       (mm-handle-disposition handle) 'filename)
9042                      (concat gnus-newsgroup-name "." gnus-current-article)))
9043                    dir)))
9044         (unless (file-exists-p file)
9045           (mm-save-part-to-file handle file))))))
9046
9047 ;; Summary extract commands
9048
9049 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9050   (let ((buffer-read-only nil)
9051         (article (gnus-summary-article-number))
9052         after-article b e)
9053     (unless (gnus-summary-goto-subject article)
9054       (error "No such article: %d" article))
9055     (gnus-summary-position-point)
9056     ;; If all commands are to be bunched up on one line, we collect
9057     ;; them here.
9058     (unless gnus-view-pseudos-separately
9059       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9060             files action)
9061         (while ps
9062           (setq action (cdr (assq 'action (car ps))))
9063           (setq files (list (cdr (assq 'name (car ps)))))
9064           (while (and ps (cdr ps)
9065                       (string= (or action "1")
9066                                (or (cdr (assq 'action (cadr ps))) "2")))
9067             (push (cdr (assq 'name (cadr ps))) files)
9068             (setcdr ps (cddr ps)))
9069           (when files
9070             (when (not (string-match "%s" action))
9071               (push " " files))
9072             (push " " files)
9073             (when (assq 'execute (car ps))
9074               (setcdr (assq 'execute (car ps))
9075                       (funcall (if (string-match "%s" action)
9076                                    'format 'concat)
9077                                action
9078                                (mapconcat
9079                                 (lambda (f)
9080                                   (if (equal f " ")
9081                                       f
9082                                     (mm-quote-arg f)))
9083                                 files " ")))))
9084           (setq ps (cdr ps)))))
9085     (if (and gnus-view-pseudos (not not-view))
9086         (while pslist
9087           (when (assq 'execute (car pslist))
9088             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9089                                   (eq gnus-view-pseudos 'not-confirm)))
9090           (setq pslist (cdr pslist)))
9091       (save-excursion
9092         (while pslist
9093           (setq after-article (or (cdr (assq 'article (car pslist)))
9094                                   (gnus-summary-article-number)))
9095           (gnus-summary-goto-subject after-article)
9096           (forward-line 1)
9097           (setq b (point))
9098           (insert "    " (file-name-nondirectory
9099                           (cdr (assq 'name (car pslist))))
9100                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9101           (setq e (point))
9102           (forward-line -1)             ; back to `b'
9103           (gnus-add-text-properties
9104            b (1- e) (list 'gnus-number gnus-reffed-article-number
9105                           gnus-mouse-face-prop gnus-mouse-face))
9106           (gnus-data-enter
9107            after-article gnus-reffed-article-number
9108            gnus-unread-mark b (car pslist) 0 (- e b))
9109           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9110           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9111           (setq pslist (cdr pslist)))))))
9112
9113 (defun gnus-pseudos< (p1 p2)
9114   (let ((c1 (cdr (assq 'action p1)))
9115         (c2 (cdr (assq 'action p2))))
9116     (and c1 c2 (string< c1 c2))))
9117
9118 (defun gnus-request-pseudo-article (props)
9119   (cond ((assq 'execute props)
9120          (gnus-execute-command (cdr (assq 'execute props)))))
9121   (let ((gnus-current-article (gnus-summary-article-number)))
9122     (gnus-run-hooks 'gnus-mark-article-hook)))
9123
9124 (defun gnus-execute-command (command &optional automatic)
9125   (save-excursion
9126     (gnus-article-setup-buffer)
9127     (set-buffer gnus-article-buffer)
9128     (setq buffer-read-only nil)
9129     (let ((command (if automatic command
9130                      (read-string "Command: " (cons command 0)))))
9131       (erase-buffer)
9132       (insert "$ " command "\n\n")
9133       (if gnus-view-pseudo-asynchronously
9134           (start-process "gnus-execute" (current-buffer) shell-file-name
9135                          shell-command-switch command)
9136         (call-process shell-file-name nil t nil
9137                       shell-command-switch command)))))
9138
9139 ;; Summary kill commands.
9140
9141 (defun gnus-summary-edit-global-kill (article)
9142   "Edit the \"global\" kill file."
9143   (interactive (list (gnus-summary-article-number)))
9144   (gnus-group-edit-global-kill article))
9145
9146 (defun gnus-summary-edit-local-kill ()
9147   "Edit a local kill file applied to the current newsgroup."
9148   (interactive)
9149   (setq gnus-current-headers (gnus-summary-article-header))
9150   (gnus-group-edit-local-kill
9151    (gnus-summary-article-number) gnus-newsgroup-name))
9152
9153 ;;; Header reading.
9154
9155 (defun gnus-read-header (id &optional header)
9156   "Read the headers of article ID and enter them into the Gnus system."
9157   (let ((group gnus-newsgroup-name)
9158         (gnus-override-method
9159          (and (gnus-news-group-p gnus-newsgroup-name)
9160               gnus-refer-article-method))
9161         where)
9162     ;; First we check to see whether the header in question is already
9163     ;; fetched.
9164     (if (stringp id)
9165         ;; This is a Message-ID.
9166         (setq header (or header (gnus-id-to-header id)))
9167       ;; This is an article number.
9168       (setq header (or header (gnus-summary-article-header id))))
9169     (if (and header
9170              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9171         ;; We have found the header.
9172         header
9173       ;; If this is a sparse article, we have to nix out its
9174       ;; previous entry in the thread hashtb.
9175       (when (and header
9176                  (gnus-summary-article-sparse-p (mail-header-number header)))
9177         (let* ((parent (gnus-parent-id (mail-header-references header)))
9178                (thread (and parent (gnus-id-to-thread parent))))
9179           (when thread
9180             (delq (assq header thread) thread))))
9181       ;; We have to really fetch the header to this article.
9182       (save-excursion
9183         (set-buffer nntp-server-buffer)
9184         (when (setq where (gnus-request-head id group))
9185           (nnheader-fold-continuation-lines)
9186           (goto-char (point-max))
9187           (insert ".\n")
9188           (goto-char (point-min))
9189           (insert "211 ")
9190           (princ (cond
9191                   ((numberp id) id)
9192                   ((cdr where) (cdr where))
9193                   (header (mail-header-number header))
9194                   (t gnus-reffed-article-number))
9195                  (current-buffer))
9196           (insert " Article retrieved.\n"))
9197         (if (or (not where)
9198                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9199             ()                          ; Malformed head.
9200           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9201             (when (and (stringp id)
9202                        (not (string= (gnus-group-real-name group)
9203                                      (car where))))
9204               ;; If we fetched by Message-ID and the article came
9205               ;; from a different group, we fudge some bogus article
9206               ;; numbers for this article.
9207               (mail-header-set-number header gnus-reffed-article-number))
9208             (save-excursion
9209               (set-buffer gnus-summary-buffer)
9210               (decf gnus-reffed-article-number)
9211               (gnus-remove-header (mail-header-number header))
9212               (push header gnus-newsgroup-headers)
9213               (setq gnus-current-headers header)
9214               (push (mail-header-number header) gnus-newsgroup-limit)))
9215           header)))))
9216
9217 (defun gnus-remove-header (number)
9218   "Remove header NUMBER from `gnus-newsgroup-headers'."
9219   (if (and gnus-newsgroup-headers
9220            (= number (mail-header-number (car gnus-newsgroup-headers))))
9221       (pop gnus-newsgroup-headers)
9222     (let ((headers gnus-newsgroup-headers))
9223       (while (and (cdr headers)
9224                   (not (= number (mail-header-number (cadr headers)))))
9225         (pop headers))
9226       (when (cdr headers)
9227         (setcdr headers (cddr headers))))))
9228
9229 ;;;
9230 ;;; summary highlights
9231 ;;;
9232
9233 (defun gnus-highlight-selected-summary ()
9234   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9235   ;; Highlight selected article in summary buffer
9236   (when gnus-summary-selected-face
9237     (save-excursion
9238       (let* ((beg (progn (beginning-of-line) (point)))
9239              (end (progn (end-of-line) (point)))
9240              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9241              (from (if (get-text-property beg gnus-mouse-face-prop)
9242                        beg
9243                      (or (next-single-property-change
9244                           beg gnus-mouse-face-prop nil end)
9245                          beg)))
9246              (to
9247               (if (= from end)
9248                   (- from 2)
9249                 (or (next-single-property-change
9250                      from gnus-mouse-face-prop nil end)
9251                     end))))
9252         ;; If no mouse-face prop on line we will have to = from = end,
9253         ;; so we highlight the entire line instead.
9254         (when (= (+ to 2) from)
9255           (setq from beg)
9256           (setq to end))
9257         (if gnus-newsgroup-selected-overlay
9258             ;; Move old overlay.
9259             (gnus-move-overlay
9260              gnus-newsgroup-selected-overlay from to (current-buffer))
9261           ;; Create new overlay.
9262           (gnus-overlay-put
9263            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9264            'face gnus-summary-selected-face))))))
9265
9266 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9267 (defun gnus-summary-highlight-line ()
9268   "Highlight current line according to `gnus-summary-highlight'."
9269   (let* ((list gnus-summary-highlight)
9270          (p (point))
9271          (end (progn (end-of-line) (point)))
9272          ;; now find out where the line starts and leave point there.
9273          (beg (progn (beginning-of-line) (point)))
9274          (article (gnus-summary-article-number))
9275          (score (or (cdr (assq (or article gnus-current-article)
9276                                gnus-newsgroup-scored))
9277                     gnus-summary-default-score 0))
9278          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9279          (inhibit-read-only t))
9280     ;; Eval the cars of the lists until we find a match.
9281     (let ((default gnus-summary-default-score))
9282       (while (and list
9283                   (not (eval (caar list))))
9284         (setq list (cdr list))))
9285     (let ((face (cdar list)))
9286       (unless (eq face (get-text-property beg 'face))
9287         (gnus-put-text-property-excluding-characters-with-faces
9288          beg end 'face
9289          (setq face (if (boundp face) (symbol-value face) face)))
9290         (when gnus-summary-highlight-line-function
9291           (funcall gnus-summary-highlight-line-function article face))))
9292     (goto-char p)))
9293
9294 (defun gnus-update-read-articles (group unread &optional compute)
9295   "Update the list of read articles in GROUP."
9296   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9297          (entry (gnus-gethash group gnus-newsrc-hashtb))
9298          (info (nth 2 entry))
9299          (prev 1)
9300          (unread (sort (copy-sequence unread) '<))
9301          read)
9302     (if (or (not info) (not active))
9303         ;; There is no info on this group if it was, in fact,
9304         ;; killed.  Gnus stores no information on killed groups, so
9305         ;; there's nothing to be done.
9306         ;; One could store the information somewhere temporarily,
9307         ;; perhaps...  Hmmm...
9308         ()
9309       ;; Remove any negative articles numbers.
9310       (while (and unread (< (car unread) 0))
9311         (setq unread (cdr unread)))
9312       ;; Remove any expired article numbers
9313       (while (and unread (< (car unread) (car active)))
9314         (setq unread (cdr unread)))
9315       ;; Compute the ranges of read articles by looking at the list of
9316       ;; unread articles.
9317       (while unread
9318         (when (/= (car unread) prev)
9319           (push (if (= prev (1- (car unread))) prev
9320                   (cons prev (1- (car unread))))
9321                 read))
9322         (setq prev (1+ (car unread)))
9323         (setq unread (cdr unread)))
9324       (when (<= prev (cdr active))
9325         (push (cons prev (cdr active)) read))
9326       (setq read (if (> (length read) 1) (nreverse read) read))
9327       (if compute
9328           read
9329         (save-excursion
9330           (set-buffer gnus-group-buffer)
9331           (gnus-undo-register
9332             `(progn
9333                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9334                (gnus-info-set-read ',info ',(gnus-info-read info))
9335                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9336                (gnus-group-update-group ,group t))))
9337        ;; Propagate the read marks to the backend.
9338        (if (gnus-check-backend-function 'request-set-mark group)
9339            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9340                  (add (gnus-remove-from-range read (gnus-info-read info))))
9341              (when (or add del)
9342                (unless (gnus-check-group group)
9343                  (error "Can't open server for %s" group))
9344                (gnus-request-set-mark
9345                 group (delq nil (list (if add (list add 'add '(read)))
9346                                       (if del (list del 'del '(read)))))))))
9347         ;; Enter this list into the group info.
9348         (gnus-info-set-read info read)
9349         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9350         (gnus-get-unread-articles-in-group info (gnus-active group))
9351         t))))
9352
9353 (defun gnus-offer-save-summaries ()
9354   "Offer to save all active summary buffers."
9355   (save-excursion
9356     (let ((buflist (buffer-list))
9357           buffers bufname)
9358       ;; Go through all buffers and find all summaries.
9359       (while buflist
9360         (and (setq bufname (buffer-name (car buflist)))
9361              (string-match "Summary" bufname)
9362              (save-excursion
9363                (set-buffer bufname)
9364                ;; We check that this is, indeed, a summary buffer.
9365                (and (eq major-mode 'gnus-summary-mode)
9366                     ;; Also make sure this isn't bogus.
9367                     gnus-newsgroup-prepared
9368                     ;; Also make sure that this isn't a dead summary buffer.
9369                     (not gnus-dead-summary-mode)))
9370              (push bufname buffers))
9371         (setq buflist (cdr buflist)))
9372       ;; Go through all these summary buffers and offer to save them.
9373       (when buffers
9374         (map-y-or-n-p
9375          "Update summary buffer %s? "
9376          (lambda (buf)
9377            (switch-to-buffer buf)
9378            (gnus-summary-exit))
9379          buffers)))))
9380
9381 (defun gnus-summary-setup-default-charset ()
9382   "Setup newsgroup default charset."
9383   (if (equal gnus-newsgroup-name "nndraft:drafts")
9384       (setq gnus-newsgroup-charset nil)
9385   (let* ((name (and gnus-newsgroup-name
9386                    (gnus-group-real-name gnus-newsgroup-name)))
9387          (ignored-charsets 
9388           (or gnus-newsgroup-ephemeral-ignored-charsets
9389               (append
9390                (and gnus-newsgroup-name
9391                     (or (gnus-group-find-parameter gnus-newsgroup-name
9392                                                    'ignored-charsets t)
9393                         (let ((alist gnus-group-ignored-charsets-alist)
9394                            elem (charsets nil))
9395                           (while (setq elem (pop alist))
9396                             (when (and name
9397                                        (string-match (car elem) name))
9398                               (setq alist nil
9399                                     charsets (cdr elem))))
9400                           charsets))))
9401               gnus-newsgroup-ignored-charsets)))
9402     (setq gnus-newsgroup-charset
9403           (or gnus-newsgroup-ephemeral-charset
9404               (and gnus-newsgroup-name
9405                    (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9406                        (let ((alist gnus-group-charset-alist)
9407                              elem charset)
9408                          (while (setq elem (pop alist))
9409                            (when (and name
9410                                       (string-match (car elem) name))
9411                              (setq alist nil
9412                                    charset (cadr elem))))
9413                          charset)))
9414               gnus-default-charset))
9415     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9416          ignored-charsets))))
9417
9418 ;;;
9419 ;;; Mime Commands
9420 ;;;
9421
9422 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9423   "Display the current article buffer fully MIME-buttonized.
9424 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9425 treated as multipart/mixed."
9426   (interactive "P")
9427   (require 'gnus-art)
9428   (let ((gnus-unbuttonized-mime-types nil)
9429         (gnus-mime-display-multipart-as-mixed show-all-parts))
9430     (gnus-summary-show-article)))
9431
9432 (defun gnus-summary-repair-multipart (article)
9433   "Add a Content-Type header to a multipart article without one."
9434   (interactive (list (gnus-summary-article-number)))
9435   (gnus-with-article article
9436     (message-narrow-to-head)
9437     (goto-char (point-max))
9438     (widen)
9439     (when (search-forward "\n--" nil t)
9440       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9441         (message-narrow-to-head)
9442         (message-remove-header "Mime-Version")
9443         (message-remove-header "Content-Type")
9444         (goto-char (point-max))
9445         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9446                         separator))
9447         (insert "Mime-Version: 1.0\n")
9448         (widen))))
9449   (let (gnus-mark-article-hook)
9450     (gnus-summary-select-article t t nil article)))
9451
9452 (defun gnus-summary-toggle-display-buttonized ()
9453   "Toggle the buttonizing of the article buffer."
9454   (interactive)
9455   (require 'gnus-art)
9456   (if (setq gnus-inhibit-mime-unbuttonizing
9457             (not gnus-inhibit-mime-unbuttonizing))
9458       (let ((gnus-unbuttonized-mime-types nil))
9459         (gnus-summary-show-article))
9460     (gnus-summary-show-article)))
9461
9462 ;;;
9463 ;;; with article
9464 ;;;
9465
9466 (defmacro gnus-with-article (article &rest forms)
9467   "Select ARTICLE and perform FORMS in the original article buffer.
9468 Then replace the article with the result."
9469   `(progn
9470      ;; We don't want the article to be marked as read.
9471      (let (gnus-mark-article-hook)
9472        (gnus-summary-select-article t t nil ,article))
9473      (set-buffer gnus-original-article-buffer)
9474      ,@forms
9475      (if (not (gnus-check-backend-function
9476                'request-replace-article (car gnus-article-current)))
9477          (gnus-message 5 "Read-only group; not replacing")
9478        (unless (gnus-request-replace-article
9479                 ,article (car gnus-article-current)
9480                 (current-buffer) t)
9481          (error "Couldn't replace article")))
9482      ;; The cache and backlog have to be flushed somewhat.
9483      (when gnus-keep-backlog
9484        (gnus-backlog-remove-article
9485         (car gnus-article-current) (cdr gnus-article-current)))
9486      (when gnus-use-cache
9487        (gnus-cache-update-article
9488         (car gnus-article-current) (cdr gnus-article-current)))))
9489
9490 (put 'gnus-with-article 'lisp-indent-function 1)
9491 (put 'gnus-with-article 'edebug-form-spec '(form body))
9492
9493 ;;;
9494 ;;; Generic summary marking commands
9495 ;;;
9496
9497 (defvar gnus-summary-marking-alist
9498   '((read gnus-del-mark "d")
9499     (unread gnus-unread-mark "u")
9500     (ticked gnus-ticked-mark "!")
9501     (dormant gnus-dormant-mark "?")
9502     (expirable gnus-expirable-mark "e"))
9503   "An alist of names/marks/keystrokes.")
9504
9505 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9506 (defvar gnus-summary-mark-map)
9507
9508 (defun gnus-summary-make-all-marking-commands ()
9509   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9510   (dolist (elem gnus-summary-marking-alist)
9511     (apply 'gnus-summary-make-marking-command elem)))
9512
9513 (defun gnus-summary-make-marking-command (name mark keystroke)
9514   (let ((map (make-sparse-keymap)))
9515     (define-key gnus-summary-generic-mark-map keystroke map)
9516     (dolist (lway `((next "next" next nil "n")
9517                     (next-unread "next unread" next t "N")
9518                     (prev "previous" prev nil "p")
9519                     (prev-unread "previous unread" prev t "P")
9520                     (nomove "" nil nil ,keystroke)))
9521       (let ((func (gnus-summary-make-marking-command-1
9522                    mark (car lway) lway name)))
9523         (setq func (eval func))
9524         (define-key map (nth 4 lway) func)))))
9525       
9526 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9527   `(defun ,(intern
9528             (format "gnus-summary-put-mark-as-%s%s"
9529                     name (if (eq way 'nomove)
9530                              ""
9531                            (concat "-" (symbol-name way)))))
9532      (n)
9533      ,(format
9534        "Mark the current article as %s%s.
9535 If N, the prefix, then repeat N times.
9536 If N is negative, move in reverse order.
9537 The difference between N and the actual number of articles marked is
9538 returned."
9539        name (cadr lway))
9540      (interactive "p")
9541      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9542     
9543 (defun gnus-summary-generic-mark (n mark move unread)
9544   "Mark N articles with MARK."
9545   (unless (eq major-mode 'gnus-summary-mode)
9546     (error "This command can only be used in the summary buffer"))
9547   (gnus-summary-show-thread)
9548   (let ((nummove
9549          (cond
9550           ((eq move 'next) 1)
9551           ((eq move 'prev) -1)
9552           (t 0))))
9553     (if (zerop nummove)
9554         (setq n 1)
9555       (when (< n 0)
9556         (setq n (abs n)
9557               nummove (* -1 nummove))))
9558     (while (and (> n 0)
9559                 (gnus-summary-mark-article nil mark)
9560                 (zerop (gnus-summary-next-subject nummove unread t)))
9561       (setq n (1- n)))
9562     (when (/= 0 n)
9563       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9564     (gnus-summary-recenter)
9565     (gnus-summary-position-point)
9566     (gnus-set-mode-line 'summary)
9567     n))
9568
9569 (gnus-summary-make-all-marking-commands)
9570
9571 (gnus-ems-redefine)
9572
9573 (provide 'gnus-sum)
9574
9575 (run-hooks 'gnus-sum-load-hook)
9576
9577 ;;; gnus-sum.el ends here