* gnus-sum.el (gnus-summary-next-subject): Don't show the thread
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ?  ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ?  ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-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 been gathered together.")
879
880 ;; Avoid highlighting in kill files.
881 (defvar gnus-summary-inhibit-highlight nil)
882 (defvar gnus-newsgroup-selected-overlay nil)
883 (defvar gnus-inhibit-limiting nil)
884 (defvar gnus-newsgroup-adaptive-score-file nil)
885 (defvar gnus-current-score-file nil)
886 (defvar gnus-current-move-group nil)
887 (defvar gnus-current-copy-group nil)
888 (defvar gnus-current-crosspost-group nil)
889
890 (defvar gnus-newsgroup-dependencies nil)
891 (defvar gnus-newsgroup-adaptive nil)
892 (defvar gnus-summary-display-article-function nil)
893 (defvar gnus-summary-highlight-line-function nil
894   "Function called after highlighting a summary line.")
895
896 (defvar gnus-summary-line-format-alist
897   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
898     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
899     (?s gnus-tmp-subject-or-nil ?s)
900     (?n gnus-tmp-name ?s)
901     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
902         ?s)
903     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
904             gnus-tmp-from) ?s)
905     (?F gnus-tmp-from ?s)
906     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
907     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
908     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
909     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
910     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
911     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
912     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
913     (?L gnus-tmp-lines ?d)
914     (?I gnus-tmp-indentation ?s)
915     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
916     (?R gnus-tmp-replied ?c)
917     (?\[ gnus-tmp-opening-bracket ?c)
918     (?\] gnus-tmp-closing-bracket ?c)
919     (?\> (make-string gnus-tmp-level ? ) ?s)
920     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
921     (?i gnus-tmp-score ?d)
922     (?z gnus-tmp-score-char ?c)
923     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
924     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
925     (?U gnus-tmp-unread ?c)
926     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
927     (?t (gnus-summary-number-of-articles-in-thread
928          (and (boundp 'thread) (car thread)) gnus-tmp-level)
929         ?d)
930     (?e (gnus-summary-number-of-articles-in-thread
931          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
932         ?c)
933     (?u gnus-tmp-user-defined ?s)
934     (?P (gnus-pick-line-number) ?d))
935   "An alist of format specifications that can appear in summary lines,
936 and what variables they correspond with, along with the type of the
937 variable (string, integer, character, etc).")
938
939 (defvar gnus-summary-dummy-line-format-alist
940   `((?S gnus-tmp-subject ?s)
941     (?N gnus-tmp-number ?d)
942     (?u gnus-tmp-user-defined ?s)))
943
944 (defvar gnus-summary-mode-line-format-alist
945   `((?G gnus-tmp-group-name ?s)
946     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
947     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
948     (?A gnus-tmp-article-number ?d)
949     (?Z gnus-tmp-unread-and-unselected ?s)
950     (?V gnus-version ?s)
951     (?U gnus-tmp-unread-and-unticked ?d)
952     (?S gnus-tmp-subject ?s)
953     (?e gnus-tmp-unselected ?d)
954     (?u gnus-tmp-user-defined ?s)
955     (?d (length gnus-newsgroup-dormant) ?d)
956     (?t (length gnus-newsgroup-marked) ?d)
957     (?r (length gnus-newsgroup-reads) ?d)
958     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
959     (?E gnus-newsgroup-expunged-tally ?d)
960     (?s (gnus-current-score-file-nondirectory) ?s)))
961
962 (defvar gnus-last-search-regexp nil
963   "Default regexp for article search command.")
964
965 (defvar gnus-last-shell-command nil
966   "Default shell command on article.")
967
968 (defvar gnus-newsgroup-begin nil)
969 (defvar gnus-newsgroup-end nil)
970 (defvar gnus-newsgroup-last-rmail nil)
971 (defvar gnus-newsgroup-last-mail nil)
972 (defvar gnus-newsgroup-last-folder nil)
973 (defvar gnus-newsgroup-last-file nil)
974 (defvar gnus-newsgroup-auto-expire nil)
975 (defvar gnus-newsgroup-active nil)
976
977 (defvar gnus-newsgroup-data nil)
978 (defvar gnus-newsgroup-data-reverse nil)
979 (defvar gnus-newsgroup-limit nil)
980 (defvar gnus-newsgroup-limits nil)
981
982 (defvar gnus-newsgroup-unreads nil
983   "List of unread articles in the current newsgroup.")
984
985 (defvar gnus-newsgroup-unselected nil
986   "List of unselected unread articles in the current newsgroup.")
987
988 (defvar gnus-newsgroup-reads nil
989   "Alist of read articles and article marks in the current newsgroup.")
990
991 (defvar gnus-newsgroup-expunged-tally nil)
992
993 (defvar gnus-newsgroup-marked nil
994   "List of ticked articles in the current newsgroup (a subset of unread art).")
995
996 (defvar gnus-newsgroup-killed nil
997   "List of ranges of articles that have been through the scoring process.")
998
999 (defvar gnus-newsgroup-cached nil
1000   "List of articles that come from the article cache.")
1001
1002 (defvar gnus-newsgroup-saved nil
1003   "List of articles that have been saved.")
1004
1005 (defvar gnus-newsgroup-kill-headers nil)
1006
1007 (defvar gnus-newsgroup-replied nil
1008   "List of articles that have been replied to in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-expirable nil
1011   "List of articles in the current newsgroup that can be expired.")
1012
1013 (defvar gnus-newsgroup-processable nil
1014   "List of articles in the current newsgroup that can be processed.")
1015
1016 (defvar gnus-newsgroup-downloadable nil
1017   "List of articles in the current newsgroup that can be processed.")
1018
1019 (defvar gnus-newsgroup-undownloaded nil
1020   "List of articles in the current newsgroup that haven't been downloaded..")
1021
1022 (defvar gnus-newsgroup-unsendable nil
1023   "List of articles in the current newsgroup that won't be sent.")
1024
1025 (defvar gnus-newsgroup-bookmarks nil
1026   "List of articles in the current newsgroup that have bookmarks.")
1027
1028 (defvar gnus-newsgroup-dormant nil
1029   "List of dormant articles in the current newsgroup.")
1030
1031 (defvar gnus-newsgroup-scored nil
1032   "List of scored articles in the current newsgroup.")
1033
1034 (defvar gnus-newsgroup-headers nil
1035   "List of article headers in the current newsgroup.")
1036
1037 (defvar gnus-newsgroup-threads nil)
1038
1039 (defvar gnus-newsgroup-prepared nil
1040   "Whether the current group has been prepared properly.")
1041
1042 (defvar gnus-newsgroup-ancient nil
1043   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1044
1045 (defvar gnus-newsgroup-sparse nil)
1046
1047 (defvar gnus-current-article nil)
1048 (defvar gnus-article-current nil)
1049 (defvar gnus-current-headers nil)
1050 (defvar gnus-have-all-headers nil)
1051 (defvar gnus-last-article nil)
1052 (defvar gnus-newsgroup-history nil)
1053 (defvar gnus-newsgroup-charset nil)
1054 (defvar gnus-newsgroup-ephemeral-charset nil)
1055 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1056
1057 (defconst gnus-summary-local-variables
1058   '(gnus-newsgroup-name
1059     gnus-newsgroup-begin gnus-newsgroup-end
1060     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1061     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1062     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1063     gnus-newsgroup-unselected gnus-newsgroup-marked
1064     gnus-newsgroup-reads gnus-newsgroup-saved
1065     gnus-newsgroup-replied gnus-newsgroup-expirable
1066     gnus-newsgroup-processable gnus-newsgroup-killed
1067     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1068     gnus-newsgroup-unsendable
1069     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1070     gnus-newsgroup-headers gnus-newsgroup-threads
1071     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1072     gnus-current-article gnus-current-headers gnus-have-all-headers
1073     gnus-last-article gnus-article-internal-prepare-hook
1074     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1075     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1076     gnus-thread-expunge-below
1077     gnus-score-alist gnus-current-score-file
1078     (gnus-summary-expunge-below . global)
1079     (gnus-summary-mark-below . global)
1080     gnus-newsgroup-active gnus-scores-exclude-files
1081     gnus-newsgroup-history gnus-newsgroup-ancient
1082     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1083     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1084     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1085     (gnus-newsgroup-expunged-tally . 0)
1086     gnus-cache-removable-articles gnus-newsgroup-cached
1087     gnus-newsgroup-data gnus-newsgroup-data-reverse
1088     gnus-newsgroup-limit gnus-newsgroup-limits
1089     gnus-newsgroup-charset)
1090   "Variables that are buffer-local to the summary buffers.")
1091
1092 ;; Byte-compiler warning.
1093 (defvar gnus-article-mode-map)
1094
1095 ;; MIME stuff.
1096
1097 (defvar gnus-decode-encoded-word-methods
1098   '(mail-decode-encoded-word-string)
1099   "List of methods used to decode encoded words.
1100
1101 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1102 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1103 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1104 whose names match REGEXP.
1105
1106 For example:
1107 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1108  mail-decode-encoded-word-string
1109  (\"chinese\" . rfc1843-decode-string))
1110 ")
1111
1112 (defvar gnus-decode-encoded-word-methods-cache nil)
1113
1114 (defun gnus-multi-decode-encoded-word-string (string)
1115   "Apply the functions from `gnus-encoded-word-methods' that match."
1116   (unless (and gnus-decode-encoded-word-methods-cache
1117                (eq gnus-newsgroup-name
1118                    (car gnus-decode-encoded-word-methods-cache)))
1119     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1120     (mapcar (lambda (x)
1121               (if (symbolp x)
1122                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1123                 (if (and gnus-newsgroup-name
1124                          (string-match (car x) gnus-newsgroup-name))
1125                     (nconc gnus-decode-encoded-word-methods-cache
1126                            (list (cdr x))))))
1127           gnus-decode-encoded-word-methods))
1128   (let ((xlist gnus-decode-encoded-word-methods-cache))
1129     (pop xlist)
1130     (while xlist
1131       (setq string (funcall (pop xlist) string))))
1132   string)
1133
1134 ;; Subject simplification.
1135
1136 (defun gnus-simplify-whitespace (str)
1137   "Remove excessive whitespace."
1138   (let ((mystr str))
1139     ;; Multiple spaces.
1140     (while (string-match "[ \t][ \t]+" mystr)
1141       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1142                           " "
1143                           (substring mystr (match-end 0)))))
1144     ;; Leading spaces.
1145     (when (string-match "^[ \t]+" mystr)
1146       (setq mystr (substring mystr (match-end 0))))
1147     ;; Trailing spaces.
1148     (when (string-match "[ \t]+$" mystr)
1149       (setq mystr (substring mystr 0 (match-beginning 0))))
1150     mystr))
1151
1152 (defsubst gnus-simplify-subject-re (subject)
1153   "Remove \"Re:\" from subject lines."
1154   (if (string-match "^[Rr][Ee]: *" subject)
1155       (substring subject (match-end 0))
1156     subject))
1157
1158 (defun gnus-simplify-subject (subject &optional re-only)
1159   "Remove `Re:' and words in parentheses.
1160 If RE-ONLY is non-nil, strip leading `Re:'s only."
1161   (let ((case-fold-search t))           ;Ignore case.
1162     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1163     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1164       (setq subject (substring subject (match-end 0))))
1165     ;; Remove uninteresting prefixes.
1166     (when (and (not re-only)
1167                gnus-simplify-ignored-prefixes
1168                (string-match gnus-simplify-ignored-prefixes subject))
1169       (setq subject (substring subject (match-end 0))))
1170     ;; Remove words in parentheses from end.
1171     (unless re-only
1172       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1173         (setq subject (substring subject 0 (match-beginning 0)))))
1174     ;; Return subject string.
1175     subject))
1176
1177 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1178 ;; all whitespace.
1179 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1180   (goto-char (point-min))
1181   (while (re-search-forward regexp nil t)
1182     (replace-match (or newtext ""))))
1183
1184 (defun gnus-simplify-buffer-fuzzy ()
1185   "Simplify string in the buffer fuzzily.
1186 The string in the accessible portion of the current buffer is simplified.
1187 It is assumed to be a single-line subject.
1188 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1189 matter is removed.  Additional things can be deleted by setting
1190 gnus-simplify-subject-fuzzy-regexp."
1191   (let ((case-fold-search t)
1192         (modified-tick))
1193     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1194
1195     (while (not (eq modified-tick (buffer-modified-tick)))
1196       (setq modified-tick (buffer-modified-tick))
1197       (cond
1198        ((listp gnus-simplify-subject-fuzzy-regexp)
1199         (mapcar 'gnus-simplify-buffer-fuzzy-step
1200                 gnus-simplify-subject-fuzzy-regexp))
1201        (gnus-simplify-subject-fuzzy-regexp
1202         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1203       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1204       (gnus-simplify-buffer-fuzzy-step
1205        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1206       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1207
1208     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1209     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1210     (gnus-simplify-buffer-fuzzy-step " $")
1211     (gnus-simplify-buffer-fuzzy-step "^ +")))
1212
1213 (defun gnus-simplify-subject-fuzzy (subject)
1214   "Simplify a subject string fuzzily.
1215 See `gnus-simplify-buffer-fuzzy' for details."
1216   (save-excursion
1217     (gnus-set-work-buffer)
1218     (let ((case-fold-search t))
1219       ;; Remove uninteresting prefixes.
1220       (when (and gnus-simplify-ignored-prefixes
1221                  (string-match gnus-simplify-ignored-prefixes subject))
1222         (setq subject (substring subject (match-end 0))))
1223       (insert subject)
1224       (inline (gnus-simplify-buffer-fuzzy))
1225       (buffer-string))))
1226
1227 (defsubst gnus-simplify-subject-fully (subject)
1228   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1229   (cond
1230    (gnus-simplify-subject-functions
1231     (gnus-map-function gnus-simplify-subject-functions subject))
1232    ((null gnus-summary-gather-subject-limit)
1233     (gnus-simplify-subject-re subject))
1234    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1235     (gnus-simplify-subject-fuzzy subject))
1236    ((numberp gnus-summary-gather-subject-limit)
1237     (gnus-limit-string (gnus-simplify-subject-re subject)
1238                        gnus-summary-gather-subject-limit))
1239    (t
1240     subject)))
1241
1242 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1243   "Check whether two subjects are equal.
1244 If optional argument simple-first is t, first argument is already
1245 simplified."
1246   (cond
1247    ((null simple-first)
1248     (equal (gnus-simplify-subject-fully s1)
1249            (gnus-simplify-subject-fully s2)))
1250    (t
1251     (equal s1
1252            (gnus-simplify-subject-fully s2)))))
1253
1254 (defun gnus-summary-bubble-group ()
1255   "Increase the score of the current group.
1256 This is a handy function to add to `gnus-summary-exit-hook' to
1257 increase the score of each group you read."
1258   (gnus-group-add-score gnus-newsgroup-name))
1259
1260 \f
1261 ;;;
1262 ;;; Gnus summary mode
1263 ;;;
1264
1265 (put 'gnus-summary-mode 'mode-class 'special)
1266
1267 (when t
1268   ;; Non-orthogonal keys
1269
1270   (gnus-define-keys gnus-summary-mode-map
1271     " " gnus-summary-next-page
1272     "\177" gnus-summary-prev-page
1273     [delete] gnus-summary-prev-page
1274     [backspace] gnus-summary-prev-page
1275     "\r" gnus-summary-scroll-up
1276     "\M-\r" gnus-summary-scroll-down
1277     "n" gnus-summary-next-unread-article
1278     "p" gnus-summary-prev-unread-article
1279     "N" gnus-summary-next-article
1280     "P" gnus-summary-prev-article
1281     "\M-\C-n" gnus-summary-next-same-subject
1282     "\M-\C-p" gnus-summary-prev-same-subject
1283     "\M-n" gnus-summary-next-unread-subject
1284     "\M-p" gnus-summary-prev-unread-subject
1285     "." gnus-summary-first-unread-article
1286     "," gnus-summary-best-unread-article
1287     "\M-s" gnus-summary-search-article-forward
1288     "\M-r" gnus-summary-search-article-backward
1289     "<" gnus-summary-beginning-of-article
1290     ">" gnus-summary-end-of-article
1291     "j" gnus-summary-goto-article
1292     "^" gnus-summary-refer-parent-article
1293     "\M-^" gnus-summary-refer-article
1294     "u" gnus-summary-tick-article-forward
1295     "!" gnus-summary-tick-article-forward
1296     "U" gnus-summary-tick-article-backward
1297     "d" gnus-summary-mark-as-read-forward
1298     "D" gnus-summary-mark-as-read-backward
1299     "E" gnus-summary-mark-as-expirable
1300     "\M-u" gnus-summary-clear-mark-forward
1301     "\M-U" gnus-summary-clear-mark-backward
1302     "k" gnus-summary-kill-same-subject-and-select
1303     "\C-k" gnus-summary-kill-same-subject
1304     "\M-\C-k" gnus-summary-kill-thread
1305     "\M-\C-l" gnus-summary-lower-thread
1306     "e" gnus-summary-edit-article
1307     "#" gnus-summary-mark-as-processable
1308     "\M-#" gnus-summary-unmark-as-processable
1309     "\M-\C-t" gnus-summary-toggle-threads
1310     "\M-\C-s" gnus-summary-show-thread
1311     "\M-\C-h" gnus-summary-hide-thread
1312     "\M-\C-f" gnus-summary-next-thread
1313     "\M-\C-b" gnus-summary-prev-thread
1314     "\M-\C-u" gnus-summary-up-thread
1315     "\M-\C-d" gnus-summary-down-thread
1316     "&" gnus-summary-execute-command
1317     "c" gnus-summary-catchup-and-exit
1318     "\C-w" gnus-summary-mark-region-as-read
1319     "\C-t" gnus-summary-toggle-truncation
1320     "?" gnus-summary-mark-as-dormant
1321     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1322     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1323     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1324     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1325     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1326     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1327     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1328     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1329     "=" gnus-summary-expand-window
1330     "\C-x\C-s" gnus-summary-reselect-current-group
1331     "\M-g" gnus-summary-rescan-group
1332     "w" gnus-summary-stop-page-breaking
1333     "\C-c\C-r" gnus-summary-caesar-message
1334     "f" gnus-summary-followup
1335     "F" gnus-summary-followup-with-original
1336     "C" gnus-summary-cancel-article
1337     "r" gnus-summary-reply
1338     "R" gnus-summary-reply-with-original
1339     "\C-c\C-f" gnus-summary-mail-forward
1340     "o" gnus-summary-save-article
1341     "\C-o" gnus-summary-save-article-mail
1342     "|" gnus-summary-pipe-output
1343     "\M-k" gnus-summary-edit-local-kill
1344     "\M-K" gnus-summary-edit-global-kill
1345     ;; "V" gnus-version
1346     "\C-c\C-d" gnus-summary-describe-group
1347     "q" gnus-summary-exit
1348     "Q" gnus-summary-exit-no-update
1349     "\C-c\C-i" gnus-info-find-node
1350     gnus-mouse-2 gnus-mouse-pick-article
1351     "m" gnus-summary-mail-other-window
1352     "a" gnus-summary-post-news
1353     "x" gnus-summary-limit-to-unread
1354     "s" gnus-summary-isearch-article
1355     "t" gnus-summary-toggle-header
1356     "g" gnus-summary-show-article
1357     "l" gnus-summary-goto-last-article
1358     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1359     "\C-d" gnus-summary-enter-digest-group
1360     "\M-\C-d" gnus-summary-read-document
1361     "\M-\C-e" gnus-summary-edit-parameters
1362     "\M-\C-g" gnus-summary-customize-parameters
1363     "\C-c\C-b" gnus-bug
1364     "*" gnus-cache-enter-article
1365     "\M-*" gnus-cache-remove-article
1366     "\M-&" gnus-summary-universal-argument
1367     "\C-l" gnus-recenter
1368     "I" gnus-summary-increase-score
1369     "L" gnus-summary-lower-score
1370     "\M-i" gnus-symbolic-argument
1371     "h" gnus-summary-select-article-buffer
1372
1373     "b" gnus-article-view-part
1374     "\M-t" gnus-summary-toggle-display-buttonized
1375
1376     "V" gnus-summary-score-map
1377     "X" gnus-uu-extract-map
1378     "S" gnus-summary-send-map)
1379
1380   ;; Sort of orthogonal keymap
1381   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1382     "t" gnus-summary-tick-article-forward
1383     "!" gnus-summary-tick-article-forward
1384     "d" gnus-summary-mark-as-read-forward
1385     "r" gnus-summary-mark-as-read-forward
1386     "c" gnus-summary-clear-mark-forward
1387     " " gnus-summary-clear-mark-forward
1388     "e" gnus-summary-mark-as-expirable
1389     "x" gnus-summary-mark-as-expirable
1390     "?" gnus-summary-mark-as-dormant
1391     "b" gnus-summary-set-bookmark
1392     "B" gnus-summary-remove-bookmark
1393     "#" gnus-summary-mark-as-processable
1394     "\M-#" gnus-summary-unmark-as-processable
1395     "S" gnus-summary-limit-include-expunged
1396     "C" gnus-summary-catchup
1397     "H" gnus-summary-catchup-to-here
1398     "\C-c" gnus-summary-catchup-all
1399     "k" gnus-summary-kill-same-subject-and-select
1400     "K" gnus-summary-kill-same-subject
1401     "P" gnus-uu-mark-map)
1402
1403   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1404     "c" gnus-summary-clear-above
1405     "u" gnus-summary-tick-above
1406     "m" gnus-summary-mark-above
1407     "k" gnus-summary-kill-below)
1408
1409   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1410     "/" gnus-summary-limit-to-subject
1411     "n" gnus-summary-limit-to-articles
1412     "w" gnus-summary-pop-limit
1413     "s" gnus-summary-limit-to-subject
1414     "a" gnus-summary-limit-to-author
1415     "u" gnus-summary-limit-to-unread
1416     "m" gnus-summary-limit-to-marks
1417     "M" gnus-summary-limit-exclude-marks
1418     "v" gnus-summary-limit-to-score
1419     "*" gnus-summary-limit-include-cached
1420     "D" gnus-summary-limit-include-dormant
1421     "T" gnus-summary-limit-include-thread
1422     "d" gnus-summary-limit-exclude-dormant
1423     "t" gnus-summary-limit-to-age
1424     "x" gnus-summary-limit-to-extra 
1425     "E" gnus-summary-limit-include-expunged
1426     "c" gnus-summary-limit-exclude-childless-dormant
1427     "C" gnus-summary-limit-mark-excluded-as-read)
1428
1429   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1430     "n" gnus-summary-next-unread-article
1431     "p" gnus-summary-prev-unread-article
1432     "N" gnus-summary-next-article
1433     "P" gnus-summary-prev-article
1434     "\C-n" gnus-summary-next-same-subject
1435     "\C-p" gnus-summary-prev-same-subject
1436     "\M-n" gnus-summary-next-unread-subject
1437     "\M-p" gnus-summary-prev-unread-subject
1438     "f" gnus-summary-first-unread-article
1439     "b" gnus-summary-best-unread-article
1440     "j" gnus-summary-goto-article
1441     "g" gnus-summary-goto-subject
1442     "l" gnus-summary-goto-last-article
1443     "o" gnus-summary-pop-article)
1444
1445   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1446     "k" gnus-summary-kill-thread
1447     "l" gnus-summary-lower-thread
1448     "i" gnus-summary-raise-thread
1449     "T" gnus-summary-toggle-threads
1450     "t" gnus-summary-rethread-current
1451     "^" gnus-summary-reparent-thread
1452     "s" gnus-summary-show-thread
1453     "S" gnus-summary-show-all-threads
1454     "h" gnus-summary-hide-thread
1455     "H" gnus-summary-hide-all-threads
1456     "n" gnus-summary-next-thread
1457     "p" gnus-summary-prev-thread
1458     "u" gnus-summary-up-thread
1459     "o" gnus-summary-top-thread
1460     "d" gnus-summary-down-thread
1461     "#" gnus-uu-mark-thread
1462     "\M-#" gnus-uu-unmark-thread)
1463
1464   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1465     "g" gnus-summary-prepare
1466     "c" gnus-summary-insert-cached-articles)
1467
1468   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1469     "c" gnus-summary-catchup-and-exit
1470     "C" gnus-summary-catchup-all-and-exit
1471     "E" gnus-summary-exit-no-update
1472     "Q" gnus-summary-exit
1473     "Z" gnus-summary-exit
1474     "n" gnus-summary-catchup-and-goto-next-group
1475     "R" gnus-summary-reselect-current-group
1476     "G" gnus-summary-rescan-group
1477     "N" gnus-summary-next-group
1478     "s" gnus-summary-save-newsrc
1479     "P" gnus-summary-prev-group)
1480
1481   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1482     " " gnus-summary-next-page
1483     "n" gnus-summary-next-page
1484     "\177" gnus-summary-prev-page
1485     [delete] gnus-summary-prev-page
1486     "p" gnus-summary-prev-page
1487     "\r" gnus-summary-scroll-up
1488     "\M-\r" gnus-summary-scroll-down
1489     "<" gnus-summary-beginning-of-article
1490     ">" gnus-summary-end-of-article
1491     "b" gnus-summary-beginning-of-article
1492     "e" gnus-summary-end-of-article
1493     "^" gnus-summary-refer-parent-article
1494     "r" gnus-summary-refer-parent-article
1495     "D" gnus-summary-enter-digest-group
1496     "R" gnus-summary-refer-references
1497     "T" gnus-summary-refer-thread
1498     "g" gnus-summary-show-article
1499     "s" gnus-summary-isearch-article
1500     "P" gnus-summary-print-article
1501     "t" gnus-article-babel)
1502
1503   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1504     "b" gnus-article-add-buttons
1505     "B" gnus-article-add-buttons-to-head
1506     "o" gnus-article-treat-overstrike
1507     "e" gnus-article-emphasize
1508     "w" gnus-article-fill-cited-article
1509     "Q" gnus-article-fill-long-lines
1510     "C" gnus-article-capitalize-sentences
1511     "c" gnus-article-remove-cr
1512     "q" gnus-article-de-quoted-unreadable
1513     "f" gnus-article-display-x-face
1514     "l" gnus-summary-stop-page-breaking
1515     "r" gnus-summary-caesar-message
1516     "t" gnus-article-hide-headers
1517     "v" gnus-summary-verbose-headers
1518     "H" gnus-article-strip-headers-in-body
1519     "d" gnus-article-treat-dumbquotes)
1520
1521   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1522     "a" gnus-article-hide
1523     "h" gnus-article-hide-headers
1524     "b" gnus-article-hide-boring-headers
1525     "s" gnus-article-hide-signature
1526     "c" gnus-article-hide-citation
1527     "C" gnus-article-hide-citation-in-followups
1528     "l" gnus-article-hide-list-identifiers
1529     "p" gnus-article-hide-pgp
1530     "B" gnus-article-strip-banner
1531     "P" gnus-article-hide-pem
1532     "\C-c" gnus-article-hide-citation-maybe)
1533
1534   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1535     "a" gnus-article-highlight
1536     "h" gnus-article-highlight-headers
1537     "c" gnus-article-highlight-citation
1538     "s" gnus-article-highlight-signature)
1539
1540   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1541     "w" gnus-article-decode-mime-words
1542     "c" gnus-article-decode-charset
1543     "v" gnus-mime-view-all-parts
1544     "b" gnus-article-view-part)
1545
1546   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1547     "z" gnus-article-date-ut
1548     "u" gnus-article-date-ut
1549     "l" gnus-article-date-local
1550     "e" gnus-article-date-lapsed
1551     "o" gnus-article-date-original
1552     "i" gnus-article-date-iso8601
1553     "s" gnus-article-date-user)
1554
1555   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1556     "t" gnus-article-remove-trailing-blank-lines
1557     "l" gnus-article-strip-leading-blank-lines
1558     "m" gnus-article-strip-multiple-blank-lines
1559     "a" gnus-article-strip-blank-lines
1560     "A" gnus-article-strip-all-blank-lines
1561     "s" gnus-article-strip-leading-space
1562     "e" gnus-article-strip-trailing-space)
1563
1564   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1565     "v" gnus-version
1566     "f" gnus-summary-fetch-faq
1567     "d" gnus-summary-describe-group
1568     "h" gnus-summary-describe-briefly
1569     "i" gnus-info-find-node)
1570
1571   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1572     "e" gnus-summary-expire-articles
1573     "\M-\C-e" gnus-summary-expire-articles-now
1574     "\177" gnus-summary-delete-article
1575     [delete] gnus-summary-delete-article
1576     [backspace] gnus-summary-delete-article
1577     "m" gnus-summary-move-article
1578     "r" gnus-summary-respool-article
1579     "w" gnus-summary-edit-article
1580     "c" gnus-summary-copy-article
1581     "B" gnus-summary-crosspost-article
1582     "q" gnus-summary-respool-query
1583     "t" gnus-summary-respool-trace
1584     "i" gnus-summary-import-article
1585     "p" gnus-summary-article-posted-p)
1586
1587   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1588     "o" gnus-summary-save-article
1589     "m" gnus-summary-save-article-mail
1590     "F" gnus-summary-write-article-file
1591     "r" gnus-summary-save-article-rmail
1592     "f" gnus-summary-save-article-file
1593     "b" gnus-summary-save-article-body-file
1594     "h" gnus-summary-save-article-folder
1595     "v" gnus-summary-save-article-vm
1596     "p" gnus-summary-pipe-output
1597     "s" gnus-soup-add-article)
1598
1599   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1600     "b" gnus-summary-display-buttonized
1601     "m" gnus-summary-repair-multipart
1602     "v" gnus-article-view-part
1603     "o" gnus-article-save-part
1604     "c" gnus-article-copy-part
1605     "e" gnus-article-externalize-part
1606     "i" gnus-article-inline-part
1607     "|" gnus-article-pipe-part))
1608
1609 (defun gnus-summary-make-menu-bar ()
1610   (gnus-turn-off-edit-menu 'summary)
1611
1612   (unless (boundp 'gnus-summary-misc-menu)
1613
1614     (easy-menu-define
1615      gnus-summary-kill-menu gnus-summary-mode-map ""
1616      (cons
1617       "Score"
1618       (nconc
1619        (list
1620         ["Enter score..." gnus-summary-score-entry t]
1621         ["Customize" gnus-score-customize t])
1622        (gnus-make-score-map 'increase)
1623        (gnus-make-score-map 'lower)
1624        '(("Mark"
1625           ["Kill below" gnus-summary-kill-below t]
1626           ["Mark above" gnus-summary-mark-above t]
1627           ["Tick above" gnus-summary-tick-above t]
1628           ["Clear above" gnus-summary-clear-above t])
1629          ["Current score" gnus-summary-current-score t]
1630          ["Set score" gnus-summary-set-score t]
1631          ["Switch current score file..." gnus-score-change-score-file t]
1632          ["Set mark below..." gnus-score-set-mark-below t]
1633          ["Set expunge below..." gnus-score-set-expunge-below t]
1634          ["Edit current score file" gnus-score-edit-current-scores t]
1635          ["Edit score file" gnus-score-edit-file t]
1636          ["Trace score" gnus-score-find-trace t]
1637          ["Find words" gnus-score-find-favourite-words t]
1638          ["Rescore buffer" gnus-summary-rescore t]
1639          ["Increase score..." gnus-summary-increase-score t]
1640          ["Lower score..." gnus-summary-lower-score t]))))
1641
1642     ;; Define both the Article menu in the summary buffer and the equivalent
1643     ;; Commands menu in the article buffer here for consistency.
1644     (let ((innards
1645            '(("Hide"
1646               ["All" gnus-article-hide t]
1647               ["Headers" gnus-article-hide-headers t]
1648               ["Signature" gnus-article-hide-signature t]
1649               ["Citation" gnus-article-hide-citation t]
1650               ["List identifiers" gnus-article-hide-list-identifiers t]
1651               ["PGP" gnus-article-hide-pgp t]
1652               ["Banner" gnus-article-strip-banner t]
1653               ["Boring headers" gnus-article-hide-boring-headers t])
1654              ("Highlight"
1655               ["All" gnus-article-highlight t]
1656               ["Headers" gnus-article-highlight-headers t]
1657               ["Signature" gnus-article-highlight-signature t]
1658               ["Citation" gnus-article-highlight-citation t])
1659              ("MIME"
1660               ["Words" gnus-article-decode-mime-words t]
1661               ["Charset" gnus-article-decode-charset t]
1662               ["QP" gnus-article-de-quoted-unreadable t]
1663               ["View all" gnus-mime-view-all-parts t])
1664              ("Date"
1665               ["Local" gnus-article-date-local t]
1666               ["ISO8601" gnus-article-date-iso8601 t]
1667               ["UT" gnus-article-date-ut t]
1668               ["Original" gnus-article-date-original t]
1669               ["Lapsed" gnus-article-date-lapsed t]
1670               ["User-defined" gnus-article-date-user t])
1671              ("Washing"
1672               ("Remove Blanks"
1673                ["Leading" gnus-article-strip-leading-blank-lines t]
1674                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1675                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1676                ["All of the above" gnus-article-strip-blank-lines t]
1677                ["All" gnus-article-strip-all-blank-lines t]
1678                ["Leading space" gnus-article-strip-leading-space t]
1679                ["Trailing space" gnus-article-strip-trailing-space t])
1680               ["Overstrike" gnus-article-treat-overstrike t]
1681               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1682               ["Emphasis" gnus-article-emphasize t]
1683               ["Word wrap" gnus-article-fill-cited-article t]
1684               ["Fill long lines" gnus-article-fill-long-lines t]
1685               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1686               ["CR" gnus-article-remove-cr t]
1687               ["Show X-Face" gnus-article-display-x-face t]
1688               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1689               ["Rot 13" gnus-summary-caesar-message t]
1690               ["Unix pipe" gnus-summary-pipe-message t]
1691               ["Add buttons" gnus-article-add-buttons t]
1692               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1693               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1694               ["Verbose header" gnus-summary-verbose-headers t]
1695               ["Toggle header" gnus-summary-toggle-header t]
1696               ["HZ" gnus-article-decode-HZ t])
1697              ("Output"
1698               ["Save in default format" gnus-summary-save-article t]
1699               ["Save in file" gnus-summary-save-article-file t]
1700               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1701               ["Save in MH folder" gnus-summary-save-article-folder t]
1702               ["Save in VM folder" gnus-summary-save-article-vm t]
1703               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1704               ["Save body in file" gnus-summary-save-article-body-file t]
1705               ["Pipe through a filter" gnus-summary-pipe-output t]
1706               ["Add to SOUP packet" gnus-soup-add-article t]
1707               ["Print" gnus-summary-print-article t])
1708              ("Backend"
1709               ["Respool article..." gnus-summary-respool-article t]
1710               ["Move article..." gnus-summary-move-article
1711                (gnus-check-backend-function
1712                 'request-move-article gnus-newsgroup-name)]
1713               ["Copy article..." gnus-summary-copy-article t]
1714               ["Crosspost article..." gnus-summary-crosspost-article
1715                (gnus-check-backend-function
1716                 'request-replace-article gnus-newsgroup-name)]
1717               ["Import file..." gnus-summary-import-article t]
1718               ["Check if posted" gnus-summary-article-posted-p t]
1719               ["Edit article" gnus-summary-edit-article
1720                (not (gnus-group-read-only-p))]
1721               ["Delete article" gnus-summary-delete-article
1722                (gnus-check-backend-function
1723                 'request-expire-articles gnus-newsgroup-name)]
1724               ["Query respool" gnus-summary-respool-query t]
1725               ["Trace respool" gnus-summary-respool-trace t]
1726               ["Delete expirable articles" gnus-summary-expire-articles-now
1727                (gnus-check-backend-function
1728                 'request-expire-articles gnus-newsgroup-name)])
1729              ("Extract"
1730               ["Uudecode" gnus-uu-decode-uu t]
1731               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1732               ["Unshar" gnus-uu-decode-unshar t]
1733               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1734               ["Save" gnus-uu-decode-save t]
1735               ["Binhex" gnus-uu-decode-binhex t]
1736               ["Postscript" gnus-uu-decode-postscript t])
1737              ("Cache"
1738               ["Enter article" gnus-cache-enter-article t]
1739               ["Remove article" gnus-cache-remove-article t])
1740              ["Translate" gnus-article-babel t]
1741              ["Select article buffer" gnus-summary-select-article-buffer t]
1742              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1743              ["Isearch article..." gnus-summary-isearch-article t]
1744              ["Beginning of the article" gnus-summary-beginning-of-article t]
1745              ["End of the article" gnus-summary-end-of-article t]
1746              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1747              ["Fetch referenced articles" gnus-summary-refer-references t]
1748              ["Fetch current thread" gnus-summary-refer-thread t]
1749              ["Fetch article with id..." gnus-summary-refer-article t]
1750              ["Redisplay" gnus-summary-show-article t])))
1751       (easy-menu-define
1752        gnus-summary-article-menu gnus-summary-mode-map ""
1753        (cons "Article" innards))
1754
1755       (easy-menu-define
1756        gnus-article-commands-menu gnus-article-mode-map ""
1757        (cons "Commands" innards)))
1758
1759     (easy-menu-define
1760      gnus-summary-thread-menu gnus-summary-mode-map ""
1761      '("Threads"
1762        ["Toggle threading" gnus-summary-toggle-threads t]
1763        ["Hide threads" gnus-summary-hide-all-threads t]
1764        ["Show threads" gnus-summary-show-all-threads t]
1765        ["Hide thread" gnus-summary-hide-thread t]
1766        ["Show thread" gnus-summary-show-thread t]
1767        ["Go to next thread" gnus-summary-next-thread t]
1768        ["Go to previous thread" gnus-summary-prev-thread t]
1769        ["Go down thread" gnus-summary-down-thread t]
1770        ["Go up thread" gnus-summary-up-thread t]
1771        ["Top of thread" gnus-summary-top-thread t]
1772        ["Mark thread as read" gnus-summary-kill-thread t]
1773        ["Lower thread score" gnus-summary-lower-thread t]
1774        ["Raise thread score" gnus-summary-raise-thread t]
1775        ["Rethread current" gnus-summary-rethread-current t]))
1776
1777     (easy-menu-define
1778      gnus-summary-post-menu gnus-summary-mode-map ""
1779      '("Post"
1780        ["Post an article" gnus-summary-post-news t]
1781        ["Followup" gnus-summary-followup t]
1782        ["Followup and yank" gnus-summary-followup-with-original t]
1783        ["Supersede article" gnus-summary-supersede-article t]
1784        ["Cancel article" gnus-summary-cancel-article t]
1785        ["Reply" gnus-summary-reply t]
1786        ["Reply and yank" gnus-summary-reply-with-original t]
1787        ["Wide reply" gnus-summary-wide-reply t]
1788        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1789        ["Mail forward" gnus-summary-mail-forward t]
1790        ["Post forward" gnus-summary-post-forward t]
1791        ["Digest and mail" gnus-uu-digest-mail-forward t]
1792        ["Digest and post" gnus-uu-digest-post-forward t]
1793        ["Resend message" gnus-summary-resend-message t]
1794        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1795        ["Send a mail" gnus-summary-mail-other-window t]
1796        ["Uuencode and post" gnus-uu-post-news t]
1797        ["Followup via news" gnus-summary-followup-to-mail t]
1798        ["Followup via news and yank"
1799         gnus-summary-followup-to-mail-with-original t]
1800        ;;("Draft"
1801        ;;["Send" gnus-summary-send-draft t]
1802        ;;["Send bounced" gnus-resend-bounced-mail t])
1803        ))
1804
1805     (easy-menu-define
1806      gnus-summary-misc-menu gnus-summary-mode-map ""
1807      '("Misc"
1808        ("Mark Read"
1809         ["Mark as read" gnus-summary-mark-as-read-forward t]
1810         ["Mark same subject and select"
1811          gnus-summary-kill-same-subject-and-select t]
1812         ["Mark same subject" gnus-summary-kill-same-subject t]
1813         ["Catchup" gnus-summary-catchup t]
1814         ["Catchup all" gnus-summary-catchup-all t]
1815         ["Catchup to here" gnus-summary-catchup-to-here t]
1816         ["Catchup region" gnus-summary-mark-region-as-read t]
1817         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1818        ("Mark Various"
1819         ["Tick" gnus-summary-tick-article-forward t]
1820         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1821         ["Remove marks" gnus-summary-clear-mark-forward t]
1822         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1823         ["Set bookmark" gnus-summary-set-bookmark t]
1824         ["Remove bookmark" gnus-summary-remove-bookmark t])
1825        ("Mark Limit"
1826         ["Marks..." gnus-summary-limit-to-marks t]
1827         ["Subject..." gnus-summary-limit-to-subject t]
1828         ["Author..." gnus-summary-limit-to-author t]
1829         ["Age..." gnus-summary-limit-to-age t]
1830         ["Extra..." gnus-summary-limit-to-extra t]
1831         ["Score" gnus-summary-limit-to-score t]
1832         ["Unread" gnus-summary-limit-to-unread t]
1833         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1834         ["Articles" gnus-summary-limit-to-articles t]
1835         ["Pop limit" gnus-summary-pop-limit t]
1836         ["Show dormant" gnus-summary-limit-include-dormant t]
1837         ["Hide childless dormant"
1838          gnus-summary-limit-exclude-childless-dormant t]
1839         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1840         ["Hide marked" gnus-summary-limit-exclude-marks t]
1841         ["Show expunged" gnus-summary-show-all-expunged t])
1842        ("Process Mark"
1843         ["Set mark" gnus-summary-mark-as-processable t]
1844         ["Remove mark" gnus-summary-unmark-as-processable t]
1845         ["Remove all marks" gnus-summary-unmark-all-processable t]
1846         ["Mark above" gnus-uu-mark-over t]
1847         ["Mark series" gnus-uu-mark-series t]
1848         ["Mark region" gnus-uu-mark-region t]
1849         ["Unmark region" gnus-uu-unmark-region t]
1850         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1851         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1852         ["Mark all" gnus-uu-mark-all t]
1853         ["Mark buffer" gnus-uu-mark-buffer t]
1854         ["Mark sparse" gnus-uu-mark-sparse t]
1855         ["Mark thread" gnus-uu-mark-thread t]
1856         ["Unmark thread" gnus-uu-unmark-thread t]
1857         ("Process Mark Sets"
1858          ["Kill" gnus-summary-kill-process-mark t]
1859          ["Yank" gnus-summary-yank-process-mark
1860           gnus-newsgroup-process-stack]
1861          ["Save" gnus-summary-save-process-mark t]))
1862        ("Scroll article"
1863         ["Page forward" gnus-summary-next-page t]
1864         ["Page backward" gnus-summary-prev-page t]
1865         ["Line forward" gnus-summary-scroll-up t])
1866        ("Move"
1867         ["Next unread article" gnus-summary-next-unread-article t]
1868         ["Previous unread article" gnus-summary-prev-unread-article t]
1869         ["Next article" gnus-summary-next-article t]
1870         ["Previous article" gnus-summary-prev-article t]
1871         ["Next unread subject" gnus-summary-next-unread-subject t]
1872         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1873         ["Next article same subject" gnus-summary-next-same-subject t]
1874         ["Previous article same subject" gnus-summary-prev-same-subject t]
1875         ["First unread article" gnus-summary-first-unread-article t]
1876         ["Best unread article" gnus-summary-best-unread-article t]
1877         ["Go to subject number..." gnus-summary-goto-subject t]
1878         ["Go to article number..." gnus-summary-goto-article t]
1879         ["Go to the last article" gnus-summary-goto-last-article t]
1880         ["Pop article off history" gnus-summary-pop-article t])
1881        ("Sort"
1882         ["Sort by number" gnus-summary-sort-by-number t]
1883         ["Sort by author" gnus-summary-sort-by-author t]
1884         ["Sort by subject" gnus-summary-sort-by-subject t]
1885         ["Sort by date" gnus-summary-sort-by-date t]
1886         ["Sort by score" gnus-summary-sort-by-score t]
1887         ["Sort by lines" gnus-summary-sort-by-lines t]
1888         ["Sort by characters" gnus-summary-sort-by-chars t])
1889        ("Help"
1890         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1891         ["Describe group" gnus-summary-describe-group t]
1892         ["Read manual" gnus-info-find-node t])
1893        ("Modes"
1894         ["Pick and read" gnus-pick-mode t]
1895         ["Binary" gnus-binary-mode t])
1896        ("Regeneration"
1897         ["Regenerate" gnus-summary-prepare t]
1898         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1899         ["Toggle threading" gnus-summary-toggle-threads t])
1900        ["Filter articles..." gnus-summary-execute-command t]
1901        ["Run command on subjects..." gnus-summary-universal-argument t]
1902        ["Search articles forward..." gnus-summary-search-article-forward t]
1903        ["Search articles backward..." gnus-summary-search-article-backward t]
1904        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1905        ["Expand window" gnus-summary-expand-window t]
1906        ["Expire expirable articles" gnus-summary-expire-articles
1907         (gnus-check-backend-function
1908          'request-expire-articles gnus-newsgroup-name)]
1909        ["Edit local kill file" gnus-summary-edit-local-kill t]
1910        ["Edit main kill file" gnus-summary-edit-global-kill t]
1911        ["Edit group parameters" gnus-summary-edit-parameters t]
1912        ["Customize group parameters" gnus-summary-customize-parameters t]
1913        ["Send a bug report" gnus-bug t]
1914        ("Exit"
1915         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1916         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1917         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1918         ["Exit group" gnus-summary-exit t]
1919         ["Exit group without updating" gnus-summary-exit-no-update t]
1920         ["Exit and goto next group" gnus-summary-next-group t]
1921         ["Exit and goto prev group" gnus-summary-prev-group t]
1922         ["Reselect group" gnus-summary-reselect-current-group t]
1923         ["Rescan group" gnus-summary-rescan-group t]
1924         ["Update dribble" gnus-summary-save-newsrc t])))
1925
1926     (gnus-run-hooks 'gnus-summary-menu-hook)))
1927
1928 (defun gnus-score-set-default (var value)
1929   "A version of set that updates the GNU Emacs menu-bar."
1930   (set var value)
1931   ;; It is the message that forces the active status to be updated.
1932   (message ""))
1933
1934 (defun gnus-make-score-map (type)
1935   "Make a summary score map of type TYPE."
1936   (if t
1937       nil
1938     (let ((headers '(("author" "from" string)
1939                      ("subject" "subject" string)
1940                      ("article body" "body" string)
1941                      ("article head" "head" string)
1942                      ("xref" "xref" string)
1943                      ("extra header" "extra" string)
1944                      ("lines" "lines" number)
1945                      ("followups to author" "followup" string)))
1946           (types '((number ("less than" <)
1947                            ("greater than" >)
1948                            ("equal" =))
1949                    (string ("substring" s)
1950                            ("exact string" e)
1951                            ("fuzzy string" f)
1952                            ("regexp" r))))
1953           (perms '(("temporary" (current-time-string))
1954                    ("permanent" nil)
1955                    ("immediate" now)))
1956           header)
1957       (list
1958        (apply
1959         'nconc
1960         (list
1961          (if (eq type 'lower)
1962              "Lower score"
1963            "Increase score"))
1964         (let (outh)
1965           (while headers
1966             (setq header (car headers))
1967             (setq outh
1968                   (cons
1969                    (apply
1970                     'nconc
1971                     (list (car header))
1972                     (let ((ts (cdr (assoc (nth 2 header) types)))
1973                           outt)
1974                       (while ts
1975                         (setq outt
1976                               (cons
1977                                (apply
1978                                 'nconc
1979                                 (list (caar ts))
1980                                 (let ((ps perms)
1981                                       outp)
1982                                   (while ps
1983                                     (setq outp
1984                                           (cons
1985                                            (vector
1986                                             (caar ps)
1987                                             (list
1988                                              'gnus-summary-score-entry
1989                                              (nth 1 header)
1990                                              (if (or (string= (nth 1 header)
1991                                                               "head")
1992                                                      (string= (nth 1 header)
1993                                                               "body"))
1994                                                  ""
1995                                                (list 'gnus-summary-header
1996                                                      (nth 1 header)))
1997                                              (list 'quote (nth 1 (car ts)))
1998                                              (list 'gnus-score-default nil)
1999                                              (nth 1 (car ps))
2000                                              t)
2001                                             t)
2002                                            outp))
2003                                     (setq ps (cdr ps)))
2004                                   (list (nreverse outp))))
2005                                outt))
2006                         (setq ts (cdr ts)))
2007                       (list (nreverse outt))))
2008                    outh))
2009             (setq headers (cdr headers)))
2010           (list (nreverse outh))))))))
2011
2012 \f
2013
2014 (defun gnus-summary-mode (&optional group)
2015   "Major mode for reading articles.
2016
2017 All normal editing commands are switched off.
2018 \\<gnus-summary-mode-map>
2019 Each line in this buffer represents one article.  To read an
2020 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2021 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2022 respectively.
2023
2024 You can also post articles and send mail from this buffer.  To
2025 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2026 of an article, type `\\[gnus-summary-reply]'.
2027
2028 There are approx. one gazillion commands you can execute in this
2029 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2030
2031 The following commands are available:
2032
2033 \\{gnus-summary-mode-map}"
2034   (interactive)
2035   (when (gnus-visual-p 'summary-menu 'menu)
2036     (gnus-summary-make-menu-bar))
2037   (kill-all-local-variables)
2038   (gnus-summary-make-local-variables)
2039   (gnus-make-thread-indent-array)
2040   (gnus-simplify-mode-line)
2041   (setq major-mode 'gnus-summary-mode)
2042   (setq mode-name "Summary")
2043   (make-local-variable 'minor-mode-alist)
2044   (use-local-map gnus-summary-mode-map)
2045   (buffer-disable-undo)
2046   (setq buffer-read-only t)             ;Disable modification
2047   (setq truncate-lines t)
2048   (setq selective-display t)
2049   (setq selective-display-ellipses t)   ;Display `...'
2050   (gnus-summary-set-display-table)
2051   (gnus-set-default-directory)
2052   (setq gnus-newsgroup-name group)
2053   (make-local-variable 'gnus-summary-line-format)
2054   (make-local-variable 'gnus-summary-line-format-spec)
2055   (make-local-variable 'gnus-summary-dummy-line-format)
2056   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2057   (make-local-variable 'gnus-summary-mark-positions)
2058   (make-local-hook 'pre-command-hook)
2059   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2060   (gnus-run-hooks 'gnus-summary-mode-hook)
2061   (mm-enable-multibyte)
2062   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2063   (gnus-update-summary-mark-positions))
2064
2065 (defun gnus-summary-make-local-variables ()
2066   "Make all the local summary buffer variables."
2067   (let (global)
2068     (dolist (local gnus-summary-local-variables)
2069       (if (consp local)
2070           (progn
2071             (if (eq (cdr local) 'global)
2072                 ;; Copy the global value of the variable.
2073                 (setq global (symbol-value (car local)))
2074               ;; Use the value from the list.
2075               (setq global (eval (cdr local))))
2076             (set (make-local-variable (car local)) global))
2077         ;; Simple nil-valued local variable.
2078         (set (make-local-variable local) nil)))))
2079
2080 (defun gnus-summary-clear-local-variables ()
2081   (let ((locals gnus-summary-local-variables))
2082     (while locals
2083       (if (consp (car locals))
2084           (and (vectorp (caar locals))
2085                (set (caar locals) nil))
2086         (and (vectorp (car locals))
2087              (set (car locals) nil)))
2088       (setq locals (cdr locals)))))
2089
2090 ;; Summary data functions.
2091
2092 (defmacro gnus-data-number (data)
2093   `(car ,data))
2094
2095 (defmacro gnus-data-set-number (data number)
2096   `(setcar ,data ,number))
2097
2098 (defmacro gnus-data-mark (data)
2099   `(nth 1 ,data))
2100
2101 (defmacro gnus-data-set-mark (data mark)
2102   `(setcar (nthcdr 1 ,data) ,mark))
2103
2104 (defmacro gnus-data-pos (data)
2105   `(nth 2 ,data))
2106
2107 (defmacro gnus-data-set-pos (data pos)
2108   `(setcar (nthcdr 2 ,data) ,pos))
2109
2110 (defmacro gnus-data-header (data)
2111   `(nth 3 ,data))
2112
2113 (defmacro gnus-data-set-header (data header)
2114   `(setf (nth 3 ,data) ,header))
2115
2116 (defmacro gnus-data-level (data)
2117   `(nth 4 ,data))
2118
2119 (defmacro gnus-data-unread-p (data)
2120   `(= (nth 1 ,data) gnus-unread-mark))
2121
2122 (defmacro gnus-data-read-p (data)
2123   `(/= (nth 1 ,data) gnus-unread-mark))
2124
2125 (defmacro gnus-data-pseudo-p (data)
2126   `(consp (nth 3 ,data)))
2127
2128 (defmacro gnus-data-find (number)
2129   `(assq ,number gnus-newsgroup-data))
2130
2131 (defmacro gnus-data-find-list (number &optional data)
2132   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2133      (memq (assq ,number bdata)
2134            bdata)))
2135
2136 (defmacro gnus-data-make (number mark pos header level)
2137   `(list ,number ,mark ,pos ,header ,level))
2138
2139 (defun gnus-data-enter (after-article number mark pos header level offset)
2140   (let ((data (gnus-data-find-list after-article)))
2141     (unless data
2142       (error "No such article: %d" after-article))
2143     (setcdr data (cons (gnus-data-make number mark pos header level)
2144                        (cdr data)))
2145     (setq gnus-newsgroup-data-reverse nil)
2146     (gnus-data-update-list (cddr data) offset)))
2147
2148 (defun gnus-data-enter-list (after-article list &optional offset)
2149   (when list
2150     (let ((data (and after-article (gnus-data-find-list after-article)))
2151           (ilist list))
2152       (if (not (or data
2153                    after-article))
2154           (let ((odata gnus-newsgroup-data))
2155             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2156             (when offset
2157               (gnus-data-update-list odata offset)))
2158         ;; Find the last element in the list to be spliced into the main
2159         ;; list.
2160         (while (cdr list)
2161           (setq list (cdr list)))
2162         (if (not data)
2163             (progn
2164               (setcdr list gnus-newsgroup-data)
2165               (setq gnus-newsgroup-data ilist)
2166               (when offset
2167                 (gnus-data-update-list (cdr list) offset)))
2168           (setcdr list (cdr data))
2169           (setcdr data ilist)
2170           (when offset
2171             (gnus-data-update-list (cdr list) offset))))
2172       (setq gnus-newsgroup-data-reverse nil))))
2173
2174 (defun gnus-data-remove (article &optional offset)
2175   (let ((data gnus-newsgroup-data))
2176     (if (= (gnus-data-number (car data)) article)
2177         (progn
2178           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2179                 gnus-newsgroup-data-reverse nil)
2180           (when offset
2181             (gnus-data-update-list gnus-newsgroup-data offset)))
2182       (while (cdr data)
2183         (when (= (gnus-data-number (cadr data)) article)
2184           (setcdr data (cddr data))
2185           (when offset
2186             (gnus-data-update-list (cdr data) offset))
2187           (setq data nil
2188                 gnus-newsgroup-data-reverse nil))
2189         (setq data (cdr data))))))
2190
2191 (defmacro gnus-data-list (backward)
2192   `(if ,backward
2193        (or gnus-newsgroup-data-reverse
2194            (setq gnus-newsgroup-data-reverse
2195                  (reverse gnus-newsgroup-data)))
2196      gnus-newsgroup-data))
2197
2198 (defun gnus-data-update-list (data offset)
2199   "Add OFFSET to the POS of all data entries in DATA."
2200   (setq gnus-newsgroup-data-reverse nil)
2201   (while data
2202     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2203     (setq data (cdr data))))
2204
2205 (defun gnus-summary-article-pseudo-p (article)
2206   "Say whether this article is a pseudo article or not."
2207   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2208
2209 (defmacro gnus-summary-article-sparse-p (article)
2210   "Say whether this article is a sparse article or not."
2211   `(memq ,article gnus-newsgroup-sparse))
2212
2213 (defmacro gnus-summary-article-ancient-p (article)
2214   "Say whether this article is a sparse article or not."
2215   `(memq ,article gnus-newsgroup-ancient))
2216
2217 (defun gnus-article-parent-p (number)
2218   "Say whether this article is a parent or not."
2219   (let ((data (gnus-data-find-list number)))
2220     (and (cdr data)                     ; There has to be an article after...
2221          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2222             (gnus-data-level (nth 1 data))))))
2223
2224 (defun gnus-article-children (number)
2225   "Return a list of all children to NUMBER."
2226   (let* ((data (gnus-data-find-list number))
2227          (level (gnus-data-level (car data)))
2228          children)
2229     (setq data (cdr data))
2230     (while (and data
2231                 (= (gnus-data-level (car data)) (1+ level)))
2232       (push (gnus-data-number (car data)) children)
2233       (setq data (cdr data)))
2234     children))
2235
2236 (defmacro gnus-summary-skip-intangible ()
2237   "If the current article is intangible, then jump to a different article."
2238   '(let ((to (get-text-property (point) 'gnus-intangible)))
2239      (and to (gnus-summary-goto-subject to))))
2240
2241 (defmacro gnus-summary-article-intangible-p ()
2242   "Say whether this article is intangible or not."
2243   '(get-text-property (point) 'gnus-intangible))
2244
2245 (defun gnus-article-read-p (article)
2246   "Say whether ARTICLE is read or not."
2247   (not (or (memq article gnus-newsgroup-marked)
2248            (memq article gnus-newsgroup-unreads)
2249            (memq article gnus-newsgroup-unselected)
2250            (memq article gnus-newsgroup-dormant))))
2251
2252 ;; Some summary mode macros.
2253
2254 (defmacro gnus-summary-article-number ()
2255   "The article number of the article on the current line.
2256 If there isn's an article number here, then we return the current
2257 article number."
2258   '(progn
2259      (gnus-summary-skip-intangible)
2260      (or (get-text-property (point) 'gnus-number)
2261          (gnus-summary-last-subject))))
2262
2263 (defmacro gnus-summary-article-header (&optional number)
2264   "Return the header of article NUMBER."
2265   `(gnus-data-header (gnus-data-find
2266                       ,(or number '(gnus-summary-article-number)))))
2267
2268 (defmacro gnus-summary-thread-level (&optional number)
2269   "Return the level of thread that starts with article NUMBER."
2270   `(if (and (eq gnus-summary-make-false-root 'dummy)
2271             (get-text-property (point) 'gnus-intangible))
2272        0
2273      (gnus-data-level (gnus-data-find
2274                        ,(or number '(gnus-summary-article-number))))))
2275
2276 (defmacro gnus-summary-article-mark (&optional number)
2277   "Return the mark of article NUMBER."
2278   `(gnus-data-mark (gnus-data-find
2279                     ,(or number '(gnus-summary-article-number)))))
2280
2281 (defmacro gnus-summary-article-pos (&optional number)
2282   "Return the position of the line of article NUMBER."
2283   `(gnus-data-pos (gnus-data-find
2284                    ,(or number '(gnus-summary-article-number)))))
2285
2286 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2287 (defmacro gnus-summary-article-subject (&optional number)
2288   "Return current subject string or nil if nothing."
2289   `(let ((headers
2290           ,(if number
2291                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2292              '(gnus-data-header (assq (gnus-summary-article-number)
2293                                       gnus-newsgroup-data)))))
2294      (and headers
2295           (vectorp headers)
2296           (mail-header-subject headers))))
2297
2298 (defmacro gnus-summary-article-score (&optional number)
2299   "Return current article score."
2300   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2301                   gnus-newsgroup-scored))
2302        gnus-summary-default-score 0))
2303
2304 (defun gnus-summary-article-children (&optional number)
2305   "Return a list of article numbers that are children of article NUMBER."
2306   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2307          (level (gnus-data-level (car data)))
2308          l children)
2309     (while (and (setq data (cdr data))
2310                 (> (setq l (gnus-data-level (car data))) level))
2311       (and (= (1+ level) l)
2312            (push (gnus-data-number (car data))
2313                  children)))
2314     (nreverse children)))
2315
2316 (defun gnus-summary-article-parent (&optional number)
2317   "Return the article number of the parent of article NUMBER."
2318   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2319                                     (gnus-data-list t)))
2320          (level (gnus-data-level (car data))))
2321     (if (zerop level)
2322         ()                              ; This is a root.
2323       ;; We search until we find an article with a level less than
2324       ;; this one.  That function has to be the parent.
2325       (while (and (setq data (cdr data))
2326                   (not (< (gnus-data-level (car data)) level))))
2327       (and data (gnus-data-number (car data))))))
2328
2329 (defun gnus-unread-mark-p (mark)
2330   "Say whether MARK is the unread mark."
2331   (= mark gnus-unread-mark))
2332
2333 (defun gnus-read-mark-p (mark)
2334   "Say whether MARK is one of the marks that mark as read.
2335 This is all marks except unread, ticked, dormant, and expirable."
2336   (not (or (= mark gnus-unread-mark)
2337            (= mark gnus-ticked-mark)
2338            (= mark gnus-dormant-mark)
2339            (= mark gnus-expirable-mark))))
2340
2341 (defmacro gnus-article-mark (number)
2342   "Return the MARK of article NUMBER.
2343 This macro should only be used when computing the mark the \"first\"
2344 time; i.e., when generating the summary lines.  After that,
2345 `gnus-summary-article-mark' should be used to examine the
2346 marks of articles."
2347   `(cond
2348     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2349     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2350     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2351     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2352     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2353     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2354     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2355     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2356            gnus-ancient-mark))))
2357
2358 ;; Saving hidden threads.
2359
2360 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2361 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2362
2363 (defmacro gnus-save-hidden-threads (&rest forms)
2364   "Save hidden threads, eval FORMS, and restore the hidden threads."
2365   (let ((config (make-symbol "config")))
2366     `(let ((,config (gnus-hidden-threads-configuration)))
2367        (unwind-protect
2368            (save-excursion
2369              ,@forms)
2370          (gnus-restore-hidden-threads-configuration ,config)))))
2371
2372 (defun gnus-data-compute-positions ()
2373   "Compute the positions of all articles."
2374   (setq gnus-newsgroup-data-reverse nil)
2375   (let ((data gnus-newsgroup-data))
2376     (save-excursion
2377       (gnus-save-hidden-threads
2378         (gnus-summary-show-all-threads)
2379         (goto-char (point-min))
2380         (while data
2381           (while (get-text-property (point) 'gnus-intangible)
2382             (forward-line 1))
2383           (gnus-data-set-pos (car data) (+ (point) 3))
2384           (setq data (cdr data))
2385           (forward-line 1))))))
2386
2387 (defun gnus-hidden-threads-configuration ()
2388   "Return the current hidden threads configuration."
2389   (save-excursion
2390     (let (config)
2391       (goto-char (point-min))
2392       (while (search-forward "\r" nil t)
2393         (push (1- (point)) config))
2394       config)))
2395
2396 (defun gnus-restore-hidden-threads-configuration (config)
2397   "Restore hidden threads configuration from CONFIG."
2398   (let (point buffer-read-only)
2399     (while (setq point (pop config))
2400       (when (and (< point (point-max))
2401                  (goto-char point)
2402                  (eq (char-after) ?\n))
2403         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2404
2405 ;; Various summary mode internalish functions.
2406
2407 (defun gnus-mouse-pick-article (e)
2408   (interactive "e")
2409   (mouse-set-point e)
2410   (gnus-summary-next-page nil t))
2411
2412 (defun gnus-summary-set-display-table ()
2413   ;; Change the display table.  Odd characters have a tendency to mess
2414   ;; up nicely formatted displays - we make all possible glyphs
2415   ;; display only a single character.
2416
2417   ;; We start from the standard display table, if any.
2418   (let ((table (or (copy-sequence standard-display-table)
2419                    (make-display-table)))
2420         (i 32))
2421     ;; Nix out all the control chars...
2422     (while (>= (setq i (1- i)) 0)
2423       (aset table i [??]))
2424     ;; ... but not newline and cr, of course.  (cr is necessary for the
2425     ;; selective display).
2426     (aset table ?\n nil)
2427     (aset table ?\r nil)
2428     ;; We keep TAB as well.
2429     (aset table ?\t nil)
2430     ;; We nix out any glyphs over 126 that are not set already.
2431     (let ((i 256))
2432       (while (>= (setq i (1- i)) 127)
2433         ;; Only modify if the entry is nil.
2434         (unless (aref table i)
2435           (aset table i [??]))))
2436     (setq buffer-display-table table)))
2437
2438 (defun gnus-summary-setup-buffer (group)
2439   "Initialize summary buffer."
2440   (let ((buffer (concat "*Summary " group "*")))
2441     (if (get-buffer buffer)
2442         (progn
2443           (set-buffer buffer)
2444           (setq gnus-summary-buffer (current-buffer))
2445           (not gnus-newsgroup-prepared))
2446       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2447       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2448       (gnus-summary-mode group)
2449       (when gnus-carpal
2450         (gnus-carpal-setup-buffer 'summary))
2451       (unless gnus-single-article-buffer
2452         (make-local-variable 'gnus-article-buffer)
2453         (make-local-variable 'gnus-article-current)
2454         (make-local-variable 'gnus-original-article-buffer))
2455       (setq gnus-newsgroup-name group)
2456       t)))
2457
2458 (defun gnus-set-global-variables ()
2459   ;; Set the global equivalents of the summary buffer-local variables
2460   ;; to the latest values they had.  These reflect the summary buffer
2461   ;; that was in action when the last article was fetched.
2462   (when (eq major-mode 'gnus-summary-mode)
2463     (setq gnus-summary-buffer (current-buffer))
2464     (let ((name gnus-newsgroup-name)
2465           (marked gnus-newsgroup-marked)
2466           (unread gnus-newsgroup-unreads)
2467           (headers gnus-current-headers)
2468           (data gnus-newsgroup-data)
2469           (summary gnus-summary-buffer)
2470           (article-buffer gnus-article-buffer)
2471           (original gnus-original-article-buffer)
2472           (gac gnus-article-current)
2473           (reffed gnus-reffed-article-number)
2474           (score-file gnus-current-score-file)
2475           (default-charset gnus-newsgroup-charset))
2476       (save-excursion
2477         (set-buffer gnus-group-buffer)
2478         (setq gnus-newsgroup-name name
2479               gnus-newsgroup-marked marked
2480               gnus-newsgroup-unreads unread
2481               gnus-current-headers headers
2482               gnus-newsgroup-data data
2483               gnus-article-current gac
2484               gnus-summary-buffer summary
2485               gnus-article-buffer article-buffer
2486               gnus-original-article-buffer original
2487               gnus-reffed-article-number reffed
2488               gnus-current-score-file score-file
2489               gnus-newsgroup-charset default-charset)
2490         ;; The article buffer also has local variables.
2491         (when (gnus-buffer-live-p gnus-article-buffer)
2492           (set-buffer gnus-article-buffer)
2493           (setq gnus-summary-buffer summary))))))
2494
2495 (defun gnus-summary-article-unread-p (article)
2496   "Say whether ARTICLE is unread or not."
2497   (memq article gnus-newsgroup-unreads))
2498
2499 (defun gnus-summary-first-article-p (&optional article)
2500   "Return whether ARTICLE is the first article in the buffer."
2501   (if (not (setq article (or article (gnus-summary-article-number))))
2502       nil
2503     (eq article (caar gnus-newsgroup-data))))
2504
2505 (defun gnus-summary-last-article-p (&optional article)
2506   "Return whether ARTICLE is the last article in the buffer."
2507   (if (not (setq article (or article (gnus-summary-article-number))))
2508       ;; All non-existent numbers are the last article.  :-)
2509       t
2510     (not (cdr (gnus-data-find-list article)))))
2511
2512 (defun gnus-make-thread-indent-array ()
2513   (let ((n 200))
2514     (unless (and gnus-thread-indent-array
2515                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2516       (setq gnus-thread-indent-array (make-vector 201 "")
2517             gnus-thread-indent-array-level gnus-thread-indent-level)
2518       (while (>= n 0)
2519         (aset gnus-thread-indent-array n
2520               (make-string (* n gnus-thread-indent-level) ? ))
2521         (setq n (1- n))))))
2522
2523 (defun gnus-update-summary-mark-positions ()
2524   "Compute where the summary marks are to go."
2525   (save-excursion
2526     (when (gnus-buffer-exists-p gnus-summary-buffer)
2527       (set-buffer gnus-summary-buffer))
2528     (let ((gnus-replied-mark 129)
2529           (gnus-score-below-mark 130)
2530           (gnus-score-over-mark 130)
2531           (gnus-download-mark 131)
2532           (spec gnus-summary-line-format-spec)
2533           gnus-visual pos)
2534       (save-excursion
2535         (gnus-set-work-buffer)
2536         (let ((gnus-summary-line-format-spec spec)
2537               (gnus-newsgroup-downloadable '((0 . t))))
2538           (gnus-summary-insert-line
2539            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2540           (goto-char (point-min))
2541           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2542                                              (- (point) 2)))))
2543           (goto-char (point-min))
2544           (push (cons 'replied (and (search-forward "\201" nil t)
2545                                     (- (point) 2)))
2546                 pos)
2547           (goto-char (point-min))
2548           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2549                 pos)
2550           (goto-char (point-min))
2551           (push (cons 'download
2552                       (and (search-forward "\203" nil t) (- (point) 2)))
2553                 pos)))
2554       (setq gnus-summary-mark-positions pos))))
2555
2556 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2557   "Insert a dummy root in the summary buffer."
2558   (beginning-of-line)
2559   (gnus-add-text-properties
2560    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2561    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2562
2563 (defun gnus-summary-from-or-to-or-newsgroups (header)
2564   (let ((to (cdr (assq 'To (mail-header-extra header))))
2565         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2566         (mail-parse-charset gnus-newsgroup-charset)
2567         (mail-parse-ignored-charsets 
2568          (save-excursion (set-buffer gnus-summary-buffer)
2569                          gnus-newsgroup-ignored-charsets)))
2570     (cond
2571      ((and to
2572            gnus-ignored-from-addresses
2573            (string-match gnus-ignored-from-addresses
2574                          (mail-header-from header)))
2575       (concat "-> "
2576               (or (car (funcall gnus-extract-address-components
2577                                 (funcall
2578                                  gnus-decode-encoded-word-function to)))
2579                   (funcall gnus-decode-encoded-word-function to))))
2580      ((and newsgroups
2581            gnus-ignored-from-addresses
2582            (string-match gnus-ignored-from-addresses
2583                          (mail-header-from header)))
2584       (concat "=> " newsgroups))
2585      (t
2586       (or (car (funcall gnus-extract-address-components
2587                         (mail-header-from header)))
2588           (mail-header-from header))))))
2589
2590 (defun gnus-summary-insert-line (gnus-tmp-header
2591                                  gnus-tmp-level gnus-tmp-current
2592                                  gnus-tmp-unread gnus-tmp-replied
2593                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2594                                  &optional gnus-tmp-dummy gnus-tmp-score
2595                                  gnus-tmp-process)
2596   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2597          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2598          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2599          (gnus-tmp-score-char
2600           (if (or (null gnus-summary-default-score)
2601                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2602                       gnus-summary-zcore-fuzz))
2603               ?  ;Whitespace
2604             (if (< gnus-tmp-score gnus-summary-default-score)
2605                 gnus-score-below-mark gnus-score-over-mark)))
2606          (gnus-tmp-replied
2607           (cond (gnus-tmp-process gnus-process-mark)
2608                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2609                  gnus-cached-mark)
2610                 (gnus-tmp-replied gnus-replied-mark)
2611                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2612                  gnus-saved-mark)
2613                 (t gnus-unread-mark)))
2614          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2615          (gnus-tmp-name
2616           (cond
2617            ((string-match "<[^>]+> *$" gnus-tmp-from)
2618             (let ((beg (match-beginning 0)))
2619               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2620                        (substring gnus-tmp-from (1+ (match-beginning 0))
2621                                   (1- (match-end 0))))
2622                   (substring gnus-tmp-from 0 beg))))
2623            ((string-match "(.+)" gnus-tmp-from)
2624             (substring gnus-tmp-from
2625                        (1+ (match-beginning 0)) (1- (match-end 0))))
2626            (t gnus-tmp-from)))
2627          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2628          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2629          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2630          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2631          (buffer-read-only nil))
2632     (when (string= gnus-tmp-name "")
2633       (setq gnus-tmp-name gnus-tmp-from))
2634     (unless (numberp gnus-tmp-lines)
2635       (setq gnus-tmp-lines 0))
2636     (gnus-put-text-property
2637      (point)
2638      (progn (eval gnus-summary-line-format-spec) (point))
2639      'gnus-number gnus-tmp-number)
2640     (when (gnus-visual-p 'summary-highlight 'highlight)
2641       (forward-line -1)
2642       (gnus-run-hooks 'gnus-summary-update-hook)
2643       (forward-line 1))))
2644
2645 (defun gnus-summary-update-line (&optional dont-update)
2646   ;; Update summary line after change.
2647   (when (and gnus-summary-default-score
2648              (not gnus-summary-inhibit-highlight))
2649     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2650            (article (gnus-summary-article-number))
2651            (score (gnus-summary-article-score article)))
2652       (unless dont-update
2653         (if (and gnus-summary-mark-below
2654                  (< (gnus-summary-article-score)
2655                     gnus-summary-mark-below))
2656             ;; This article has a low score, so we mark it as read.
2657             (when (memq article gnus-newsgroup-unreads)
2658               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2659           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2660             ;; This article was previously marked as read on account
2661             ;; of a low score, but now it has risen, so we mark it as
2662             ;; unread.
2663             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2664         (gnus-summary-update-mark
2665          (if (or (null gnus-summary-default-score)
2666                  (<= (abs (- score gnus-summary-default-score))
2667                      gnus-summary-zcore-fuzz))
2668              ?  ;Whitespace
2669            (if (< score gnus-summary-default-score)
2670                gnus-score-below-mark gnus-score-over-mark))
2671          'score))
2672       ;; Do visual highlighting.
2673       (when (gnus-visual-p 'summary-highlight 'highlight)
2674         (gnus-run-hooks 'gnus-summary-update-hook)))))
2675
2676 (defvar gnus-tmp-new-adopts nil)
2677
2678 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2679   "Return the number of articles in THREAD.
2680 This may be 0 in some cases -- if none of the articles in
2681 the thread are to be displayed."
2682   (let* ((number
2683           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2684           (cond
2685            ((not (listp thread))
2686             1)
2687            ((and (consp thread) (cdr thread))
2688             (apply
2689              '+ 1 (mapcar
2690                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2691            ((null thread)
2692             1)
2693            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2694             1)
2695            (t 0))))
2696     (when (and level (zerop level) gnus-tmp-new-adopts)
2697       (incf number
2698             (apply '+ (mapcar
2699                        'gnus-summary-number-of-articles-in-thread
2700                        gnus-tmp-new-adopts))))
2701     (if char
2702         (if (> number 1) gnus-not-empty-thread-mark
2703           gnus-empty-thread-mark)
2704       number)))
2705
2706 (defun gnus-summary-set-local-parameters (group)
2707   "Go through the local params of GROUP and set all variable specs in that list."
2708   (let ((params (gnus-group-find-parameter group))
2709         elem)
2710     (while params
2711       (setq elem (car params)
2712             params (cdr params))
2713       (and (consp elem)                 ; Has to be a cons.
2714            (consp (cdr elem))           ; The cdr has to be a list.
2715            (symbolp (car elem))         ; Has to be a symbol in there.
2716            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2717            (ignore-errors               ; So we set it.
2718              (make-local-variable (car elem))
2719              (set (car elem) (eval (nth 1 elem))))))))
2720
2721 (defun gnus-summary-read-group (group &optional show-all no-article
2722                                       kill-buffer no-display backward
2723                                       select-articles)
2724   "Start reading news in newsgroup GROUP.
2725 If SHOW-ALL is non-nil, already read articles are also listed.
2726 If NO-ARTICLE is non-nil, no article is selected initially.
2727 If NO-DISPLAY, don't generate a summary buffer."
2728   (let (result)
2729     (while (and group
2730                 (null (setq result
2731                             (let ((gnus-auto-select-next nil))
2732                               (or (gnus-summary-read-group-1
2733                                    group show-all no-article
2734                                    kill-buffer no-display
2735                                    select-articles)
2736                                   (setq show-all nil
2737                                         select-articles nil)))))
2738                 (eq gnus-auto-select-next 'quietly))
2739       (set-buffer gnus-group-buffer)
2740       ;; The entry function called above goes to the next
2741       ;; group automatically, so we go two groups back
2742       ;; if we are searching for the previous group.
2743       (when backward
2744         (gnus-group-prev-unread-group 2))
2745       (if (not (equal group (gnus-group-group-name)))
2746           (setq group (gnus-group-group-name))
2747         (setq group nil)))
2748     result))
2749
2750 (defun gnus-summary-read-group-1 (group show-all no-article
2751                                         kill-buffer no-display
2752                                         &optional select-articles)
2753   ;; Killed foreign groups can't be entered.
2754   (when (and (not (gnus-group-native-p group))
2755              (not (gnus-gethash group gnus-newsrc-hashtb)))
2756     (error "Dead non-native groups can't be entered"))
2757   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2758   (let* ((new-group (gnus-summary-setup-buffer group))
2759          (quit-config (gnus-group-quit-config group))
2760          (did-select (and new-group (gnus-select-newsgroup
2761                                      group show-all select-articles))))
2762     (cond
2763      ;; This summary buffer exists already, so we just select it.
2764      ((not new-group)
2765       (gnus-set-global-variables)
2766       (when kill-buffer
2767         (gnus-kill-or-deaden-summary kill-buffer))
2768       (gnus-configure-windows 'summary 'force)
2769       (gnus-set-mode-line 'summary)
2770       (gnus-summary-position-point)
2771       (message "")
2772       t)
2773      ;; We couldn't select this group.
2774      ((null did-select)
2775       (when (and (eq major-mode 'gnus-summary-mode)
2776                  (not (equal (current-buffer) kill-buffer)))
2777         (kill-buffer (current-buffer))
2778         (if (not quit-config)
2779             (progn
2780               ;; Update the info -- marks might need to be removed,
2781               ;; for instance.
2782               (gnus-summary-update-info)
2783               (set-buffer gnus-group-buffer)
2784               (gnus-group-jump-to-group group)
2785               (gnus-group-next-unread-group 1))
2786           (gnus-handle-ephemeral-exit quit-config)))
2787       (gnus-message 3 "Can't select group")
2788       nil)
2789      ;; The user did a `C-g' while prompting for number of articles,
2790      ;; so we exit this group.
2791      ((eq did-select 'quit)
2792       (and (eq major-mode 'gnus-summary-mode)
2793            (not (equal (current-buffer) kill-buffer))
2794            (kill-buffer (current-buffer)))
2795       (when kill-buffer
2796         (gnus-kill-or-deaden-summary kill-buffer))
2797       (if (not quit-config)
2798           (progn
2799             (set-buffer gnus-group-buffer)
2800             (gnus-group-jump-to-group group)
2801             (gnus-group-next-unread-group 1)
2802             (gnus-configure-windows 'group 'force))
2803         (gnus-handle-ephemeral-exit quit-config))
2804       ;; Finally signal the quit.
2805       (signal 'quit nil))
2806      ;; The group was successfully selected.
2807      (t
2808       (gnus-set-global-variables)
2809       ;; Save the active value in effect when the group was entered.
2810       (setq gnus-newsgroup-active
2811             (gnus-copy-sequence
2812              (gnus-active gnus-newsgroup-name)))
2813       ;; You can change the summary buffer in some way with this hook.
2814       (gnus-run-hooks 'gnus-select-group-hook)
2815       ;; Set any local variables in the group parameters.
2816       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2817       (gnus-update-format-specifications
2818        nil 'summary 'summary-mode 'summary-dummy)
2819       (gnus-update-summary-mark-positions)
2820       ;; Do score processing.
2821       (when gnus-use-scoring
2822         (gnus-possibly-score-headers))
2823       ;; Check whether to fill in the gaps in the threads.
2824       (when gnus-build-sparse-threads
2825         (gnus-build-sparse-threads))
2826       ;; Find the initial limit.
2827       (if gnus-show-threads
2828           (if show-all
2829               (let ((gnus-newsgroup-dormant nil))
2830                 (gnus-summary-initial-limit show-all))
2831             (gnus-summary-initial-limit show-all))
2832         ;; When untreaded, all articles are always shown.
2833         (setq gnus-newsgroup-limit
2834               (mapcar
2835                (lambda (header) (mail-header-number header))
2836                gnus-newsgroup-headers)))
2837       ;; Generate the summary buffer.
2838       (unless no-display
2839         (gnus-summary-prepare))
2840       (when gnus-use-trees
2841         (gnus-tree-open group)
2842         (setq gnus-summary-highlight-line-function
2843               'gnus-tree-highlight-article))
2844       ;; If the summary buffer is empty, but there are some low-scored
2845       ;; articles or some excluded dormants, we include these in the
2846       ;; buffer.
2847       (when (and (zerop (buffer-size))
2848                  (not no-display))
2849         (cond (gnus-newsgroup-dormant
2850                (gnus-summary-limit-include-dormant))
2851               ((and gnus-newsgroup-scored show-all)
2852                (gnus-summary-limit-include-expunged t))))
2853       ;; Function `gnus-apply-kill-file' must be called in this hook.
2854       (gnus-run-hooks 'gnus-apply-kill-hook)
2855       (if (and (zerop (buffer-size))
2856                (not no-display))
2857           (progn
2858             ;; This newsgroup is empty.
2859             (gnus-summary-catchup-and-exit nil t)
2860             (gnus-message 6 "No unread news")
2861             (when kill-buffer
2862               (gnus-kill-or-deaden-summary kill-buffer))
2863             ;; Return nil from this function.
2864             nil)
2865         ;; Hide conversation thread subtrees.  We cannot do this in
2866         ;; gnus-summary-prepare-hook since kill processing may not
2867         ;; work with hidden articles.
2868         (and gnus-show-threads
2869              gnus-thread-hide-subtree
2870              (gnus-summary-hide-all-threads))
2871         (when kill-buffer
2872           (gnus-kill-or-deaden-summary kill-buffer))
2873         ;; Show first unread article if requested.
2874         (if (and (not no-article)
2875                  (not no-display)
2876                  gnus-newsgroup-unreads
2877                  gnus-auto-select-first)
2878             (progn
2879               (gnus-configure-windows 'summary)
2880               (cond
2881                ((eq gnus-auto-select-first 'best)
2882                 (gnus-summary-best-unread-article))
2883                ((eq gnus-auto-select-first t)
2884                 (gnus-summary-first-unread-article))
2885                ((gnus-functionp gnus-auto-select-first)
2886                 (funcall gnus-auto-select-first))))
2887           ;; Don't select any articles, just move point to the first
2888           ;; article in the group.
2889           (goto-char (point-min))
2890           (gnus-summary-position-point)
2891           (gnus-configure-windows 'summary 'force)
2892           (gnus-set-mode-line 'summary))
2893         (when (get-buffer-window gnus-group-buffer t)
2894           ;; Gotta use windows, because recenter does weird stuff if
2895           ;; the current buffer ain't the displayed window.
2896           (let ((owin (selected-window)))
2897             (select-window (get-buffer-window gnus-group-buffer t))
2898             (when (gnus-group-goto-group group)
2899               (recenter))
2900             (select-window owin)))
2901         ;; Mark this buffer as "prepared".
2902         (setq gnus-newsgroup-prepared t)
2903         (gnus-run-hooks 'gnus-summary-prepared-hook)
2904         t)))))
2905
2906 (defun gnus-summary-prepare ()
2907   "Generate the summary buffer."
2908   (interactive)
2909   (let ((buffer-read-only nil))
2910     (erase-buffer)
2911     (setq gnus-newsgroup-data nil
2912           gnus-newsgroup-data-reverse nil)
2913     (gnus-run-hooks 'gnus-summary-generate-hook)
2914     ;; Generate the buffer, either with threads or without.
2915     (when gnus-newsgroup-headers
2916       (gnus-summary-prepare-threads
2917        (if gnus-show-threads
2918            (gnus-sort-gathered-threads
2919             (funcall gnus-summary-thread-gathering-function
2920                      (gnus-sort-threads
2921                       (gnus-cut-threads (gnus-make-threads)))))
2922          ;; Unthreaded display.
2923          (gnus-sort-articles gnus-newsgroup-headers))))
2924     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2925     ;; Call hooks for modifying summary buffer.
2926     (goto-char (point-min))
2927     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2928
2929 (defsubst gnus-general-simplify-subject (subject)
2930   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2931   (setq subject
2932         (cond
2933          ;; Truncate the subject.
2934          (gnus-simplify-subject-functions
2935           (gnus-map-function gnus-simplify-subject-functions subject))
2936          ((numberp gnus-summary-gather-subject-limit)
2937           (setq subject (gnus-simplify-subject-re subject))
2938           (if (> (length subject) gnus-summary-gather-subject-limit)
2939               (substring subject 0 gnus-summary-gather-subject-limit)
2940             subject))
2941          ;; Fuzzily simplify it.
2942          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2943           (gnus-simplify-subject-fuzzy subject))
2944          ;; Just remove the leading "Re:".
2945          (t
2946           (gnus-simplify-subject-re subject))))
2947
2948   (if (and gnus-summary-gather-exclude-subject
2949            (string-match gnus-summary-gather-exclude-subject subject))
2950       nil                               ; This article shouldn't be gathered
2951     subject))
2952
2953 (defun gnus-summary-simplify-subject-query ()
2954   "Query where the respool algorithm would put this article."
2955   (interactive)
2956   (gnus-summary-select-article)
2957   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2958
2959 (defun gnus-gather-threads-by-subject (threads)
2960   "Gather threads by looking at Subject headers."
2961   (if (not gnus-summary-make-false-root)
2962       threads
2963     (let ((hashtb (gnus-make-hashtable 1024))
2964           (prev threads)
2965           (result threads)
2966           subject hthread whole-subject)
2967       (while threads
2968         (setq subject (gnus-general-simplify-subject
2969                        (setq whole-subject (mail-header-subject
2970                                             (caar threads)))))
2971         (when subject
2972           (if (setq hthread (gnus-gethash subject hashtb))
2973               (progn
2974                 ;; We enter a dummy root into the thread, if we
2975                 ;; haven't done that already.
2976                 (unless (stringp (caar hthread))
2977                   (setcar hthread (list whole-subject (car hthread))))
2978                 ;; We add this new gathered thread to this gathered
2979                 ;; thread.
2980                 (setcdr (car hthread)
2981                         (nconc (cdar hthread) (list (car threads))))
2982                 ;; Remove it from the list of threads.
2983                 (setcdr prev (cdr threads))
2984                 (setq threads prev))
2985             ;; Enter this thread into the hash table.
2986             (gnus-sethash subject threads hashtb)))
2987         (setq prev threads)
2988         (setq threads (cdr threads)))
2989       result)))
2990
2991 (defun gnus-gather-threads-by-references (threads)
2992   "Gather threads by looking at References headers."
2993   (let ((idhashtb (gnus-make-hashtable 1024))
2994         (thhashtb (gnus-make-hashtable 1024))
2995         (prev threads)
2996         (result threads)
2997         ids references id gthread gid entered ref)
2998     (while threads
2999       (when (setq references (mail-header-references (caar threads)))
3000         (setq id (mail-header-id (caar threads))
3001               ids (gnus-split-references references)
3002               entered nil)
3003         (while (setq ref (pop ids))
3004           (setq ids (delete ref ids))
3005           (if (not (setq gid (gnus-gethash ref idhashtb)))
3006               (progn
3007                 (gnus-sethash ref id idhashtb)
3008                 (gnus-sethash id threads thhashtb))
3009             (setq gthread (gnus-gethash gid thhashtb))
3010             (unless entered
3011               ;; We enter a dummy root into the thread, if we
3012               ;; haven't done that already.
3013               (unless (stringp (caar gthread))
3014                 (setcar gthread (list (mail-header-subject (caar gthread))
3015                                       (car gthread))))
3016               ;; We add this new gathered thread to this gathered
3017               ;; thread.
3018               (setcdr (car gthread)
3019                       (nconc (cdar gthread) (list (car threads)))))
3020             ;; Add it into the thread hash table.
3021             (gnus-sethash id gthread thhashtb)
3022             (setq entered t)
3023             ;; Remove it from the list of threads.
3024             (setcdr prev (cdr threads))
3025             (setq threads prev))))
3026       (setq prev threads)
3027       (setq threads (cdr threads)))
3028     result))
3029
3030 (defun gnus-sort-gathered-threads (threads)
3031   "Sort subtreads inside each gathered thread by article number."
3032   (let ((result threads))
3033     (while threads
3034       (when (stringp (caar threads))
3035         (setcdr (car threads)
3036                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3037       (setq threads (cdr threads)))
3038     result))
3039
3040 (defun gnus-thread-loop-p (root thread)
3041   "Say whether ROOT is in THREAD."
3042   (let ((stack (list thread))
3043         (infloop 0)
3044         th)
3045     (while (setq thread (pop stack))
3046       (setq th (cdr thread))
3047       (while (and th
3048                   (not (eq (caar th) root)))
3049         (pop th))
3050       (if th
3051           ;; We have found a loop.
3052           (let (ref-dep)
3053             (setcdr thread (delq (car th) (cdr thread)))
3054             (if (boundp (setq ref-dep (intern "none"
3055                                               gnus-newsgroup-dependencies)))
3056                 (setcdr (symbol-value ref-dep)
3057                         (nconc (cdr (symbol-value ref-dep))
3058                                (list (car th))))
3059               (set ref-dep (list nil (car th))))
3060             (setq infloop 1
3061                   stack nil))
3062         ;; Push all the subthreads onto the stack.
3063         (push (cdr thread) stack)))
3064     infloop))
3065
3066 (defun gnus-make-threads ()
3067   "Go through the dependency hashtb and find the roots.  Return all threads."
3068   (let (threads)
3069     (while (catch 'infloop
3070              (mapatoms
3071               (lambda (refs)
3072                 ;; Deal with self-referencing References loops.
3073                 (when (and (car (symbol-value refs))
3074                            (not (zerop
3075                                  (apply
3076                                   '+
3077                                   (mapcar
3078                                    (lambda (thread)
3079                                      (gnus-thread-loop-p
3080                                       (car (symbol-value refs)) thread))
3081                                    (cdr (symbol-value refs)))))))
3082                   (setq threads nil)
3083                   (throw 'infloop t))
3084                 (unless (car (symbol-value refs))
3085                   ;; These threads do not refer back to any other articles,
3086                   ;; so they're roots.
3087                   (setq threads (append (cdr (symbol-value refs)) threads))))
3088               gnus-newsgroup-dependencies)))
3089     threads))
3090
3091 ;; Build the thread tree.
3092 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3093   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3094
3095 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3096 if it was already present.
3097
3098 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3099 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3100 Message-IDs will be renamed be renamed to a unique Message-ID before
3101 being entered.
3102
3103 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3104   (let* ((id (mail-header-id header))
3105          (id-dep (and id (intern id dependencies)))
3106          ref ref-dep ref-header)
3107     ;; Enter this `header' in the `dependencies' table.
3108     (cond
3109      ((not id-dep)
3110       (setq header nil))
3111      ;; The first two cases do the normal part: enter a new `header'
3112      ;; in the `dependencies' table.
3113      ((not (boundp id-dep))
3114       (set id-dep (list header)))
3115      ((null (car (symbol-value id-dep)))
3116       (setcar (symbol-value id-dep) header))
3117
3118      ;; From here the `header' was already present in the
3119      ;; `dependencies' table.
3120      (force-new
3121       ;; Overrides an existing entry;
3122       ;; just set the header part of the entry.
3123       (setcar (symbol-value id-dep) header))
3124
3125      ;; Renames the existing `header' to a unique Message-ID.
3126      ((not gnus-summary-ignore-duplicates)
3127       ;; An article with this Message-ID has already been seen.
3128       ;; We rename the Message-ID.
3129       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3130            (list header))
3131       (mail-header-set-id header id))
3132
3133      ;; The last case ignores an existing entry, except it adds any
3134      ;; additional Xrefs (in case the two articles came from different
3135      ;; servers.
3136      ;; Also sets `header' to `nil' meaning that the `dependencies'
3137      ;; table was *not* modified.
3138      (t
3139       (mail-header-set-xref
3140        (car (symbol-value id-dep))
3141        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3142                    "")
3143                (or (mail-header-xref header) "")))
3144       (setq header nil)))
3145
3146     (when header
3147       ;; First check if that we are not creating a References loop.
3148       (setq ref (gnus-parent-id (mail-header-references header)))
3149       (while (and ref
3150                   (setq ref-dep (intern-soft ref dependencies))
3151                   (boundp ref-dep)
3152                   (setq ref-header (car (symbol-value ref-dep))))
3153         (if (string= id ref)
3154             ;; Yuk!  This is a reference loop.  Make the article be a
3155             ;; root article.
3156             (progn
3157               (mail-header-set-references (car (symbol-value id-dep)) "none")
3158               (setq ref nil))
3159           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3160       (setq ref (gnus-parent-id (mail-header-references header)))
3161       (setq ref-dep (intern (or ref "none") dependencies))
3162       (if (boundp ref-dep)
3163           (setcdr (symbol-value ref-dep)
3164                   (nconc (cdr (symbol-value ref-dep))
3165                          (list (symbol-value id-dep))))
3166         (set ref-dep (list nil (symbol-value id-dep)))))
3167     header))
3168
3169 (defun gnus-build-sparse-threads ()
3170   (let ((headers gnus-newsgroup-headers)
3171         (mail-parse-charset gnus-newsgroup-charset)
3172         (gnus-summary-ignore-duplicates t)
3173         header references generation relations
3174         subject child end new-child date)
3175     ;; First we create an alist of generations/relations, where
3176     ;; generations is how much we trust the relation, and the relation
3177     ;; is parent/child.
3178     (gnus-message 7 "Making sparse threads...")
3179     (save-excursion
3180       (nnheader-set-temp-buffer " *gnus sparse threads*")
3181       (while (setq header (pop headers))
3182         (when (and (setq references (mail-header-references header))
3183                    (not (string= references "")))
3184           (insert references)
3185           (setq child (mail-header-id header)
3186                 subject (mail-header-subject header)
3187                 date (mail-header-date header)
3188                 generation 0)
3189           (while (search-backward ">" nil t)
3190             (setq end (1+ (point)))
3191             (when (search-backward "<" nil t)
3192               (setq new-child (buffer-substring (point) end))
3193               (push (list (incf generation)
3194                           child (setq child new-child)
3195                           subject date)
3196                     relations)))
3197           (when child
3198             (push (list (1+ generation) child nil subject) relations))
3199           (erase-buffer)))
3200       (kill-buffer (current-buffer)))
3201     ;; Sort over trustworthiness.
3202     (mapcar
3203      (lambda (relation)
3204        (when (gnus-dependencies-add-header
3205               (make-full-mail-header
3206                gnus-reffed-article-number
3207                (nth 3 relation) "" (or (nth 4 relation) "")
3208                (nth 1 relation)
3209                (or (nth 2 relation) "") 0 0 "")
3210               gnus-newsgroup-dependencies nil)
3211          (push gnus-reffed-article-number gnus-newsgroup-limit)
3212          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3213          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3214                gnus-newsgroup-reads)
3215          (decf gnus-reffed-article-number)))
3216      (sort relations 'car-less-than-car))
3217     (gnus-message 7 "Making sparse threads...done")))
3218
3219 (defun gnus-build-old-threads ()
3220   ;; Look at all the articles that refer back to old articles, and
3221   ;; fetch the headers for the articles that aren't there.  This will
3222   ;; build complete threads - if the roots haven't been expired by the
3223   ;; server, that is.
3224   (let ((mail-parse-charset gnus-newsgroup-charset)
3225         id heads)
3226     (mapatoms
3227      (lambda (refs)
3228        (when (not (car (symbol-value refs)))
3229          (setq heads (cdr (symbol-value refs)))
3230          (while heads
3231            (if (memq (mail-header-number (caar heads))
3232                      gnus-newsgroup-dormant)
3233                (setq heads (cdr heads))
3234              (setq id (symbol-name refs))
3235              (while (and (setq id (gnus-build-get-header id))
3236                          (not (car (gnus-id-to-thread id)))))
3237              (setq heads nil)))))
3238      gnus-newsgroup-dependencies)))
3239
3240 ;; This function has to be called with point after the article number
3241 ;; on the beginning of the line.
3242 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3243   (let ((eol (gnus-point-at-eol))
3244         (buffer (current-buffer))
3245         header)
3246
3247     ;; overview: [num subject from date id refs chars lines misc]
3248     (unwind-protect
3249         (progn
3250           (narrow-to-region (point) eol)
3251           (unless (eobp)
3252             (forward-char))
3253
3254           (setq header
3255                 (make-full-mail-header
3256                  number                 ; number
3257                  (funcall gnus-decode-encoded-word-function
3258                           (nnheader-nov-field)) ; subject
3259                  (funcall gnus-decode-encoded-word-function
3260                           (nnheader-nov-field)) ; from
3261                  (nnheader-nov-field)   ; date
3262                  (nnheader-nov-read-message-id) ; id
3263                  (nnheader-nov-field)   ; refs
3264                  (nnheader-nov-read-integer) ; chars
3265                  (nnheader-nov-read-integer) ; lines
3266                  (unless (eobp)
3267                    (nnheader-nov-field)) ; misc
3268                  (nnheader-nov-parse-extra)))) ; extra
3269
3270       (widen))
3271
3272     (when gnus-alter-header-function
3273       (funcall gnus-alter-header-function header))
3274     (gnus-dependencies-add-header header dependencies force-new)))
3275
3276 (defun gnus-build-get-header (id)
3277   ;; Look through the buffer of NOV lines and find the header to
3278   ;; ID.  Enter this line into the dependencies hash table, and return
3279   ;; the id of the parent article (if any).
3280   (let ((deps gnus-newsgroup-dependencies)
3281         found header)
3282     (prog1
3283         (save-excursion
3284           (set-buffer nntp-server-buffer)
3285           (let ((case-fold-search nil))
3286             (goto-char (point-min))
3287             (while (and (not found)
3288                         (search-forward id nil t))
3289               (beginning-of-line)
3290               (setq found (looking-at
3291                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3292                                    (regexp-quote id))))
3293               (or found (beginning-of-line 2)))
3294             (when found
3295               (beginning-of-line)
3296               (and
3297                (setq header (gnus-nov-parse-line
3298                              (read (current-buffer)) deps))
3299                (gnus-parent-id (mail-header-references header))))))
3300       (when header
3301         (let ((number (mail-header-number header)))
3302           (push number gnus-newsgroup-limit)
3303           (push header gnus-newsgroup-headers)
3304           (if (memq number gnus-newsgroup-unselected)
3305               (progn
3306                 (push number gnus-newsgroup-unreads)
3307                 (setq gnus-newsgroup-unselected
3308                       (delq number gnus-newsgroup-unselected)))
3309             (push number gnus-newsgroup-ancient)))))))
3310
3311 (defun gnus-build-all-threads ()
3312   "Read all the headers."
3313   (let ((gnus-summary-ignore-duplicates t)
3314         (mail-parse-charset gnus-newsgroup-charset)
3315         (dependencies gnus-newsgroup-dependencies)
3316         header article)
3317     (save-excursion
3318       (set-buffer nntp-server-buffer)
3319       (let ((case-fold-search nil))
3320         (goto-char (point-min))
3321         (while (not (eobp))
3322           (ignore-errors
3323             (setq article (read (current-buffer))
3324                   header (gnus-nov-parse-line article dependencies)))
3325           (when header
3326             (save-excursion
3327               (set-buffer gnus-summary-buffer)
3328               (push header gnus-newsgroup-headers)
3329               (if (memq (setq article (mail-header-number header))
3330                         gnus-newsgroup-unselected)
3331                   (progn
3332                     (push article gnus-newsgroup-unreads)
3333                     (setq gnus-newsgroup-unselected
3334                           (delq article gnus-newsgroup-unselected)))
3335                 (push article gnus-newsgroup-ancient)))
3336             (forward-line 1)))))))
3337
3338 (defun gnus-summary-update-article-line (article header)
3339   "Update the line for ARTICLE using HEADERS."
3340   (let* ((id (mail-header-id header))
3341          (thread (gnus-id-to-thread id)))
3342     (unless thread
3343       (error "Article in no thread"))
3344     ;; Update the thread.
3345     (setcar thread header)
3346     (gnus-summary-goto-subject article)
3347     (let* ((datal (gnus-data-find-list article))
3348            (data (car datal))
3349            (length (when (cdr datal)
3350                      (- (gnus-data-pos data)
3351                         (gnus-data-pos (cadr datal)))))
3352            (buffer-read-only nil)
3353            (level (gnus-summary-thread-level)))
3354       (gnus-delete-line)
3355       (gnus-summary-insert-line
3356        header level nil (gnus-article-mark article)
3357        (memq article gnus-newsgroup-replied)
3358        (memq article gnus-newsgroup-expirable)
3359        ;; Only insert the Subject string when it's different
3360        ;; from the previous Subject string.
3361        (if (gnus-subject-equal
3362             (condition-case ()
3363                 (mail-header-subject
3364                  (gnus-data-header
3365                   (cadr
3366                    (gnus-data-find-list
3367                     article
3368                     (gnus-data-list t)))))
3369               ;; Error on the side of excessive subjects.
3370               (error ""))
3371             (mail-header-subject header))
3372            ""
3373          (mail-header-subject header))
3374        nil (cdr (assq article gnus-newsgroup-scored))
3375        (memq article gnus-newsgroup-processable))
3376       (when length
3377         (gnus-data-update-list
3378          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3379
3380 (defun gnus-summary-update-article (article &optional iheader)
3381   "Update ARTICLE in the summary buffer."
3382   (set-buffer gnus-summary-buffer)
3383   (let* ((header (gnus-summary-article-header article))
3384          (id (mail-header-id header))
3385          (data (gnus-data-find article))
3386          (thread (gnus-id-to-thread id))
3387          (references (mail-header-references header))
3388          (parent
3389           (gnus-id-to-thread
3390            (or (gnus-parent-id
3391                 (when (and references
3392                            (not (equal "" references)))
3393                   references))
3394                "none")))
3395          (buffer-read-only nil)
3396          (old (car thread)))
3397     (when thread
3398       (unless iheader
3399         (setcar thread nil)
3400         (when parent
3401           (delq thread parent)))
3402       (if (gnus-summary-insert-subject id header)
3403           ;; Set the (possibly) new article number in the data structure.
3404           (gnus-data-set-number data (gnus-id-to-article id))
3405         (setcar thread old)
3406         nil))))
3407
3408 (defun gnus-rebuild-thread (id &optional line)
3409   "Rebuild the thread containing ID.
3410 If LINE, insert the rebuilt thread starting on line LINE."
3411   (let ((buffer-read-only nil)
3412         old-pos current thread data)
3413     (if (not gnus-show-threads)
3414         (setq thread (list (car (gnus-id-to-thread id))))
3415       ;; Get the thread this article is part of.
3416       (setq thread (gnus-remove-thread id)))
3417     (setq old-pos (gnus-point-at-bol))
3418     (setq current (save-excursion
3419                     (and (zerop (forward-line -1))
3420                          (gnus-summary-article-number))))
3421     ;; If this is a gathered thread, we have to go some re-gathering.
3422     (when (stringp (car thread))
3423       (let ((subject (car thread))
3424             roots thr)
3425         (setq thread (cdr thread))
3426         (while thread
3427           (unless (memq (setq thr (gnus-id-to-thread
3428                                    (gnus-root-id
3429                                     (mail-header-id (caar thread)))))
3430                         roots)
3431             (push thr roots))
3432           (setq thread (cdr thread)))
3433         ;; We now have all (unique) roots.
3434         (if (= (length roots) 1)
3435             ;; All the loose roots are now one solid root.
3436             (setq thread (car roots))
3437           (setq thread (cons subject (gnus-sort-threads roots))))))
3438     (let (threads)
3439       ;; We then insert this thread into the summary buffer.
3440       (when line
3441         (goto-char (point-min))
3442         (forward-line (1- line)))
3443       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3444         (if gnus-show-threads
3445             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3446           (gnus-summary-prepare-unthreaded thread))
3447         (setq data (nreverse gnus-newsgroup-data))
3448         (setq threads gnus-newsgroup-threads))
3449       ;; We splice the new data into the data structure.
3450       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3451       ;;!!! then we want to insert at the beginning of the buffer.
3452       ;;!!! That happens to be true with Gnus now, but that may
3453       ;;!!! change in the future.  Perhaps.
3454       (gnus-data-enter-list
3455        (if line nil current) data (- (point) old-pos))
3456       (setq gnus-newsgroup-threads
3457             (nconc threads gnus-newsgroup-threads))
3458       (gnus-data-compute-positions))))
3459
3460 (defun gnus-number-to-header (number)
3461   "Return the header for article NUMBER."
3462   (let ((headers gnus-newsgroup-headers))
3463     (while (and headers
3464                 (not (= number (mail-header-number (car headers)))))
3465       (pop headers))
3466     (when headers
3467       (car headers))))
3468
3469 (defun gnus-parent-headers (in-headers &optional generation)
3470   "Return the headers of the GENERATIONeth parent of HEADERS."
3471   (unless generation
3472     (setq generation 1))
3473   (let ((parent t)
3474         (headers in-headers)
3475         references)
3476     (while (and parent
3477                 (not (zerop generation))
3478                 (setq references (mail-header-references headers)))
3479       (setq headers (if (and references
3480                              (setq parent (gnus-parent-id references)))
3481                         (car (gnus-id-to-thread parent))
3482                       nil))
3483       (decf generation))
3484     (and (not (eq headers in-headers))
3485          headers)))
3486
3487 (defun gnus-id-to-thread (id)
3488   "Return the (sub-)thread where ID appears."
3489   (gnus-gethash id gnus-newsgroup-dependencies))
3490
3491 (defun gnus-id-to-article (id)
3492   "Return the article number of ID."
3493   (let ((thread (gnus-id-to-thread id)))
3494     (when (and thread
3495                (car thread))
3496       (mail-header-number (car thread)))))
3497
3498 (defun gnus-id-to-header (id)
3499   "Return the article headers of ID."
3500   (car (gnus-id-to-thread id)))
3501
3502 (defun gnus-article-displayed-root-p (article)
3503   "Say whether ARTICLE is a root(ish) article."
3504   (let ((level (gnus-summary-thread-level article))
3505         (refs (mail-header-references  (gnus-summary-article-header article)))
3506         particle)
3507     (cond
3508      ((null level) nil)
3509      ((zerop level) t)
3510      ((null refs) t)
3511      ((null (gnus-parent-id refs)) t)
3512      ((and (= 1 level)
3513            (null (setq particle (gnus-id-to-article
3514                                  (gnus-parent-id refs))))
3515            (null (gnus-summary-thread-level particle)))))))
3516
3517 (defun gnus-root-id (id)
3518   "Return the id of the root of the thread where ID appears."
3519   (let (last-id prev)
3520     (while (and id (setq prev (car (gnus-id-to-thread id))))
3521       (setq last-id id
3522             id (gnus-parent-id (mail-header-references prev))))
3523     last-id))
3524
3525 (defun gnus-articles-in-thread (thread)
3526   "Return the list of articles in THREAD."
3527   (cons (mail-header-number (car thread))
3528         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3529
3530 (defun gnus-remove-thread (id &optional dont-remove)
3531   "Remove the thread that has ID in it."
3532   (let (headers thread last-id)
3533     ;; First go up in this thread until we find the root.
3534     (setq last-id (gnus-root-id id)
3535           headers (message-flatten-list (gnus-id-to-thread last-id)))
3536     ;; We have now found the real root of this thread.  It might have
3537     ;; been gathered into some loose thread, so we have to search
3538     ;; through the threads to find the thread we wanted.
3539     (let ((threads gnus-newsgroup-threads)
3540           sub)
3541       (while threads
3542         (setq sub (car threads))
3543         (if (stringp (car sub))
3544             ;; This is a gathered thread, so we look at the roots
3545             ;; below it to find whether this article is in this
3546             ;; gathered root.
3547             (progn
3548               (setq sub (cdr sub))
3549               (while sub
3550                 (when (member (caar sub) headers)
3551                   (setq thread (car threads)
3552                         threads nil
3553                         sub nil))
3554                 (setq sub (cdr sub))))
3555           ;; It's an ordinary thread, so we check it.
3556           (when (eq (car sub) (car headers))
3557             (setq thread sub
3558                   threads nil)))
3559         (setq threads (cdr threads)))
3560       ;; If this article is in no thread, then it's a root.
3561       (if thread
3562           (unless dont-remove
3563             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3564         (setq thread (gnus-id-to-thread last-id)))
3565       (when thread
3566         (prog1
3567             thread                      ; We return this thread.
3568           (unless dont-remove
3569             (if (stringp (car thread))
3570                 (progn
3571                   ;; If we use dummy roots, then we have to remove the
3572                   ;; dummy root as well.
3573                   (when (eq gnus-summary-make-false-root 'dummy)
3574                     ;; We go to the dummy root by going to
3575                     ;; the first sub-"thread", and then one line up.
3576                     (gnus-summary-goto-article
3577                      (mail-header-number (caadr thread)))
3578                     (forward-line -1)
3579                     (gnus-delete-line)
3580                     (gnus-data-compute-positions))
3581                   (setq thread (cdr thread))
3582                   (while thread
3583                     (gnus-remove-thread-1 (car thread))
3584                     (setq thread (cdr thread))))
3585               (gnus-summary-show-all-threads)
3586               (gnus-remove-thread-1 thread))))))))
3587
3588 (defun gnus-remove-thread-1 (thread)
3589   "Remove the thread THREAD recursively."
3590   (let ((number (mail-header-number (pop thread)))
3591         d)
3592     (setq thread (reverse thread))
3593     (while thread
3594       (gnus-remove-thread-1 (pop thread)))
3595     (when (setq d (gnus-data-find number))
3596       (goto-char (gnus-data-pos d))
3597       (gnus-data-remove
3598        number
3599        (- (gnus-point-at-bol)
3600           (prog1
3601               (1+ (gnus-point-at-eol))
3602             (gnus-delete-line)))))))
3603
3604 (defun gnus-sort-threads (threads)
3605   "Sort THREADS."
3606   (if (not gnus-thread-sort-functions)
3607       threads
3608     (gnus-message 8 "Sorting threads...")
3609     (prog1
3610         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3611       (gnus-message 8 "Sorting threads...done"))))
3612
3613 (defun gnus-sort-articles (articles)
3614   "Sort ARTICLES."
3615   (when gnus-article-sort-functions
3616     (gnus-message 7 "Sorting articles...")
3617     (prog1
3618         (setq gnus-newsgroup-headers
3619               (sort articles (gnus-make-sort-function
3620                               gnus-article-sort-functions)))
3621       (gnus-message 7 "Sorting articles...done"))))
3622
3623 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3624 (defmacro gnus-thread-header (thread)
3625   ;; Return header of first article in THREAD.
3626   ;; Note that THREAD must never, ever be anything else than a variable -
3627   ;; using some other form will lead to serious barfage.
3628   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3629   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3630   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3631         (vector thread) 2))
3632
3633 (defsubst gnus-article-sort-by-number (h1 h2)
3634   "Sort articles by article number."
3635   (< (mail-header-number h1)
3636      (mail-header-number h2)))
3637
3638 (defun gnus-thread-sort-by-number (h1 h2)
3639   "Sort threads by root article number."
3640   (gnus-article-sort-by-number
3641    (gnus-thread-header h1) (gnus-thread-header h2)))
3642
3643 (defsubst gnus-article-sort-by-lines (h1 h2)
3644   "Sort articles by article Lines header."
3645   (< (mail-header-lines h1)
3646      (mail-header-lines h2)))
3647
3648 (defun gnus-thread-sort-by-lines (h1 h2)
3649   "Sort threads by root article Lines header."
3650   (gnus-article-sort-by-lines
3651    (gnus-thread-header h1) (gnus-thread-header h2)))
3652
3653 (defsubst gnus-article-sort-by-chars (h1 h2)
3654   "Sort articles by octet length."
3655   (< (mail-header-chars h1)
3656      (mail-header-chars h2)))
3657
3658 (defun gnus-thread-sort-by-chars (h1 h2)
3659   "Sort threads by root article octet length."
3660   (gnus-article-sort-by-chars
3661    (gnus-thread-header h1) (gnus-thread-header h2)))
3662
3663 (defsubst gnus-article-sort-by-author (h1 h2)
3664   "Sort articles by root author."
3665   (string-lessp
3666    (let ((extract (funcall
3667                    gnus-extract-address-components
3668                    (mail-header-from h1))))
3669      (or (car extract) (cadr extract) ""))
3670    (let ((extract (funcall
3671                    gnus-extract-address-components
3672                    (mail-header-from h2))))
3673      (or (car extract) (cadr extract) ""))))
3674
3675 (defun gnus-thread-sort-by-author (h1 h2)
3676   "Sort threads by root author."
3677   (gnus-article-sort-by-author
3678    (gnus-thread-header h1)  (gnus-thread-header h2)))
3679
3680 (defsubst gnus-article-sort-by-subject (h1 h2)
3681   "Sort articles by root subject."
3682   (string-lessp
3683    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3684    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3685
3686 (defun gnus-thread-sort-by-subject (h1 h2)
3687   "Sort threads by root subject."
3688   (gnus-article-sort-by-subject
3689    (gnus-thread-header h1) (gnus-thread-header h2)))
3690
3691 (defsubst gnus-article-sort-by-date (h1 h2)
3692   "Sort articles by root article date."
3693   (time-less-p
3694    (gnus-date-get-time (mail-header-date h1))
3695    (gnus-date-get-time (mail-header-date h2))))
3696
3697 (defun gnus-thread-sort-by-date (h1 h2)
3698   "Sort threads by root article date."
3699   (gnus-article-sort-by-date
3700    (gnus-thread-header h1) (gnus-thread-header h2)))
3701
3702 (defsubst gnus-article-sort-by-score (h1 h2)
3703   "Sort articles by root article score.
3704 Unscored articles will be counted as having a score of zero."
3705   (> (or (cdr (assq (mail-header-number h1)
3706                     gnus-newsgroup-scored))
3707          gnus-summary-default-score 0)
3708      (or (cdr (assq (mail-header-number h2)
3709                     gnus-newsgroup-scored))
3710          gnus-summary-default-score 0)))
3711
3712 (defun gnus-thread-sort-by-score (h1 h2)
3713   "Sort threads by root article score."
3714   (gnus-article-sort-by-score
3715    (gnus-thread-header h1) (gnus-thread-header h2)))
3716
3717 (defun gnus-thread-sort-by-total-score (h1 h2)
3718   "Sort threads by the sum of all scores in the thread.
3719 Unscored articles will be counted as having a score of zero."
3720   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3721
3722 (defun gnus-thread-total-score (thread)
3723   ;; This function find the total score of THREAD.
3724   (cond ((null thread)
3725          0)
3726         ((consp thread)
3727          (if (stringp (car thread))
3728              (apply gnus-thread-score-function 0
3729                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3730            (gnus-thread-total-score-1 thread)))
3731         (t
3732          (gnus-thread-total-score-1 (list thread)))))
3733
3734 (defun gnus-thread-total-score-1 (root)
3735   ;; This function find the total score of the thread below ROOT.
3736   (setq root (car root))
3737   (apply gnus-thread-score-function
3738          (or (append
3739               (mapcar 'gnus-thread-total-score
3740                       (cdr (gnus-id-to-thread (mail-header-id root))))
3741               (when (> (mail-header-number root) 0)
3742                 (list (or (cdr (assq (mail-header-number root)
3743                                      gnus-newsgroup-scored))
3744                           gnus-summary-default-score 0))))
3745              (list gnus-summary-default-score)
3746              '(0))))
3747
3748 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3749 (defvar gnus-tmp-prev-subject nil)
3750 (defvar gnus-tmp-false-parent nil)
3751 (defvar gnus-tmp-root-expunged nil)
3752 (defvar gnus-tmp-dummy-line nil)
3753
3754 (defvar gnus-tmp-header)
3755 (defun gnus-extra-header (type &optional header)
3756   "Return the extra header of TYPE."
3757   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3758       ""))
3759
3760 (defun gnus-summary-prepare-threads (threads)
3761   "Prepare summary buffer from THREADS and indentation LEVEL.
3762 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3763 or a straight list of headers."
3764   (gnus-message 7 "Generating summary...")
3765
3766   (setq gnus-newsgroup-threads threads)
3767   (beginning-of-line)
3768
3769   (let ((gnus-tmp-level 0)
3770         (default-score (or gnus-summary-default-score 0))
3771         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3772         thread number subject stack state gnus-tmp-gathered beg-match
3773         new-roots gnus-tmp-new-adopts thread-end
3774         gnus-tmp-header gnus-tmp-unread
3775         gnus-tmp-replied gnus-tmp-subject-or-nil
3776         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3777         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3778         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3779
3780     (setq gnus-tmp-prev-subject nil)
3781
3782     (if (vectorp (car threads))
3783         ;; If this is a straight (sic) list of headers, then a
3784         ;; threaded summary display isn't required, so we just create
3785         ;; an unthreaded one.
3786         (gnus-summary-prepare-unthreaded threads)
3787
3788       ;; Do the threaded display.
3789
3790       (while (or threads stack gnus-tmp-new-adopts new-roots)
3791
3792         (if (and (= gnus-tmp-level 0)
3793                  (or (not stack)
3794                      (= (caar stack) 0))
3795                  (not gnus-tmp-false-parent)
3796                  (or gnus-tmp-new-adopts new-roots))
3797             (if gnus-tmp-new-adopts
3798                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3799                       thread (list (car gnus-tmp-new-adopts))
3800                       gnus-tmp-header (caar thread)
3801                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3802               (when new-roots
3803                 (setq thread (list (car new-roots))
3804                       gnus-tmp-header (caar thread)
3805                       new-roots (cdr new-roots))))
3806
3807           (if threads
3808               ;; If there are some threads, we do them before the
3809               ;; threads on the stack.
3810               (setq thread threads
3811                     gnus-tmp-header (caar thread))
3812             ;; There were no current threads, so we pop something off
3813             ;; the stack.
3814             (setq state (car stack)
3815                   gnus-tmp-level (car state)
3816                   thread (cdr state)
3817                   stack (cdr stack)
3818                   gnus-tmp-header (caar thread))))
3819
3820         (setq gnus-tmp-false-parent nil)
3821         (setq gnus-tmp-root-expunged nil)
3822         (setq thread-end nil)
3823
3824         (if (stringp gnus-tmp-header)
3825             ;; The header is a dummy root.
3826             (cond
3827              ((eq gnus-summary-make-false-root 'adopt)
3828               ;; We let the first article adopt the rest.
3829               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3830                                                (cddar thread)))
3831               (setq gnus-tmp-gathered
3832                     (nconc (mapcar
3833                             (lambda (h) (mail-header-number (car h)))
3834                             (cddar thread))
3835                            gnus-tmp-gathered))
3836               (setq thread (cons (list (caar thread)
3837                                        (cadar thread))
3838                                  (cdr thread)))
3839               (setq gnus-tmp-level -1
3840                     gnus-tmp-false-parent t))
3841              ((eq gnus-summary-make-false-root 'empty)
3842               ;; We print adopted articles with empty subject fields.
3843               (setq gnus-tmp-gathered
3844                     (nconc (mapcar
3845                             (lambda (h) (mail-header-number (car h)))
3846                             (cddar thread))
3847                            gnus-tmp-gathered))
3848               (setq gnus-tmp-level -1))
3849              ((eq gnus-summary-make-false-root 'dummy)
3850               ;; We remember that we probably want to output a dummy
3851               ;; root.
3852               (setq gnus-tmp-dummy-line gnus-tmp-header)
3853               (setq gnus-tmp-prev-subject gnus-tmp-header))
3854              (t
3855               ;; We do not make a root for the gathered
3856               ;; sub-threads at all.
3857               (setq gnus-tmp-level -1)))
3858
3859           (setq number (mail-header-number gnus-tmp-header)
3860                 subject (mail-header-subject gnus-tmp-header))
3861
3862           (cond
3863            ;; If the thread has changed subject, we might want to make
3864            ;; this subthread into a root.
3865            ((and (null gnus-thread-ignore-subject)
3866                  (not (zerop gnus-tmp-level))
3867                  gnus-tmp-prev-subject
3868                  (not (inline
3869                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3870             (setq new-roots (nconc new-roots (list (car thread)))
3871                   thread-end t
3872                   gnus-tmp-header nil))
3873            ;; If the article lies outside the current limit,
3874            ;; then we do not display it.
3875            ((not (memq number gnus-newsgroup-limit))
3876             (setq gnus-tmp-gathered
3877                   (nconc (mapcar
3878                           (lambda (h) (mail-header-number (car h)))
3879                           (cdar thread))
3880                          gnus-tmp-gathered))
3881             (setq gnus-tmp-new-adopts (if (cdar thread)
3882                                           (append gnus-tmp-new-adopts
3883                                                   (cdar thread))
3884                                         gnus-tmp-new-adopts)
3885                   thread-end t
3886                   gnus-tmp-header nil)
3887             (when (zerop gnus-tmp-level)
3888               (setq gnus-tmp-root-expunged t)))
3889            ;; Perhaps this article is to be marked as read?
3890            ((and gnus-summary-mark-below
3891                  (< (or (cdr (assq number gnus-newsgroup-scored))
3892                         default-score)
3893                     gnus-summary-mark-below)
3894                  ;; Don't touch sparse articles.
3895                  (not (gnus-summary-article-sparse-p number))
3896                  (not (gnus-summary-article-ancient-p number)))
3897             (setq gnus-newsgroup-unreads
3898                   (delq number gnus-newsgroup-unreads))
3899             (if gnus-newsgroup-auto-expire
3900                 (push number gnus-newsgroup-expirable)
3901               (push (cons number gnus-low-score-mark)
3902                     gnus-newsgroup-reads))))
3903
3904           (when gnus-tmp-header
3905             ;; We may have an old dummy line to output before this
3906             ;; article.
3907             (when (and gnus-tmp-dummy-line
3908                        (gnus-subject-equal
3909                         gnus-tmp-dummy-line
3910                         (mail-header-subject gnus-tmp-header)))
3911               (gnus-summary-insert-dummy-line
3912                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3913               (setq gnus-tmp-dummy-line nil))
3914
3915             ;; Compute the mark.
3916             (setq gnus-tmp-unread (gnus-article-mark number))
3917
3918             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3919                                   gnus-tmp-header gnus-tmp-level)
3920                   gnus-newsgroup-data)
3921
3922             ;; Actually insert the line.
3923             (setq
3924              gnus-tmp-subject-or-nil
3925              (cond
3926               ((and gnus-thread-ignore-subject
3927                     gnus-tmp-prev-subject
3928                     (not (inline (gnus-subject-equal
3929                                   gnus-tmp-prev-subject subject))))
3930                subject)
3931               ((zerop gnus-tmp-level)
3932                (if (and (eq gnus-summary-make-false-root 'empty)
3933                         (memq number gnus-tmp-gathered)
3934                         gnus-tmp-prev-subject
3935                         (inline (gnus-subject-equal
3936                                  gnus-tmp-prev-subject subject)))
3937                    gnus-summary-same-subject
3938                  subject))
3939               (t gnus-summary-same-subject)))
3940             (if (and (eq gnus-summary-make-false-root 'adopt)
3941                      (= gnus-tmp-level 1)
3942                      (memq number gnus-tmp-gathered))
3943                 (setq gnus-tmp-opening-bracket ?\<
3944                       gnus-tmp-closing-bracket ?\>)
3945               (setq gnus-tmp-opening-bracket ?\[
3946                     gnus-tmp-closing-bracket ?\]))
3947             (setq
3948              gnus-tmp-indentation
3949              (aref gnus-thread-indent-array gnus-tmp-level)
3950              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3951              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3952                                 gnus-summary-default-score 0)
3953              gnus-tmp-score-char
3954              (if (or (null gnus-summary-default-score)
3955                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3956                          gnus-summary-zcore-fuzz))
3957                  ?  ;Whitespace
3958                (if (< gnus-tmp-score gnus-summary-default-score)
3959                    gnus-score-below-mark gnus-score-over-mark))
3960              gnus-tmp-replied
3961              (cond ((memq number gnus-newsgroup-processable)
3962                     gnus-process-mark)
3963                    ((memq number gnus-newsgroup-cached)
3964                     gnus-cached-mark)
3965                    ((memq number gnus-newsgroup-replied)
3966                     gnus-replied-mark)
3967                    ((memq number gnus-newsgroup-saved)
3968                     gnus-saved-mark)
3969                    (t gnus-unread-mark))
3970              gnus-tmp-from (mail-header-from gnus-tmp-header)
3971              gnus-tmp-name
3972              (cond
3973               ((string-match "<[^>]+> *$" gnus-tmp-from)
3974                (setq beg-match (match-beginning 0))
3975                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3976                         (substring gnus-tmp-from (1+ (match-beginning 0))
3977                                    (1- (match-end 0))))
3978                    (substring gnus-tmp-from 0 beg-match)))
3979               ((string-match "(.+)" gnus-tmp-from)
3980                (substring gnus-tmp-from
3981                           (1+ (match-beginning 0)) (1- (match-end 0))))
3982               (t gnus-tmp-from)))
3983             (when (string= gnus-tmp-name "")
3984               (setq gnus-tmp-name gnus-tmp-from))
3985             (unless (numberp gnus-tmp-lines)
3986               (setq gnus-tmp-lines 0))
3987             (gnus-put-text-property
3988              (point)
3989              (progn (eval gnus-summary-line-format-spec) (point))
3990              'gnus-number number)
3991             (when gnus-visual-p
3992               (forward-line -1)
3993               (gnus-run-hooks 'gnus-summary-update-hook)
3994               (forward-line 1))
3995
3996             (setq gnus-tmp-prev-subject subject)))
3997
3998         (when (nth 1 thread)
3999           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4000         (incf gnus-tmp-level)
4001         (setq threads (if thread-end nil (cdar thread)))
4002         (unless threads
4003           (setq gnus-tmp-level 0)))))
4004   (gnus-message 7 "Generating summary...done"))
4005
4006 (defun gnus-summary-prepare-unthreaded (headers)
4007   "Generate an unthreaded summary buffer based on HEADERS."
4008   (let (header number mark)
4009
4010     (beginning-of-line)
4011
4012     (while headers
4013       ;; We may have to root out some bad articles...
4014       (when (memq (setq number (mail-header-number
4015                                 (setq header (pop headers))))
4016                   gnus-newsgroup-limit)
4017         ;; Mark article as read when it has a low score.
4018         (when (and gnus-summary-mark-below
4019                    (< (or (cdr (assq number gnus-newsgroup-scored))
4020                           gnus-summary-default-score 0)
4021                       gnus-summary-mark-below)
4022                    (not (gnus-summary-article-ancient-p number)))
4023           (setq gnus-newsgroup-unreads
4024                 (delq number gnus-newsgroup-unreads))
4025           (if gnus-newsgroup-auto-expire
4026               (push number gnus-newsgroup-expirable)
4027             (push (cons number gnus-low-score-mark)
4028                   gnus-newsgroup-reads)))
4029
4030         (setq mark (gnus-article-mark number))
4031         (push (gnus-data-make number mark (1+ (point)) header 0)
4032               gnus-newsgroup-data)
4033         (gnus-summary-insert-line
4034          header 0 number
4035          mark (memq number gnus-newsgroup-replied)
4036          (memq number gnus-newsgroup-expirable)
4037          (mail-header-subject header) nil
4038          (cdr (assq number gnus-newsgroup-scored))
4039          (memq number gnus-newsgroup-processable))))))
4040
4041 (defun gnus-summary-remove-list-identifiers ()
4042   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4043   (let ((regexp (if (stringp gnus-list-identifiers)
4044                     gnus-list-identifiers
4045                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4046     (dolist (header gnus-newsgroup-headers)
4047       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4048                           (mail-header-subject header))
4049         (mail-header-set-subject
4050          header (concat (substring (mail-header-subject header)
4051                                    0 (match-beginning 2))
4052                         (substring (mail-header-subject header)
4053                                    (match-end 2))))))))
4054
4055 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4056   "Select newsgroup GROUP.
4057 If READ-ALL is non-nil, all articles in the group are selected.
4058 If SELECT-ARTICLES, only select those articles from GROUP."
4059   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4060          ;;!!! Dirty hack; should be removed.
4061          (gnus-summary-ignore-duplicates
4062           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4063               t
4064             gnus-summary-ignore-duplicates))
4065          (info (nth 2 entry))
4066          articles fetched-articles cached)
4067
4068     (unless (gnus-check-server
4069              (setq gnus-current-select-method
4070                    (gnus-find-method-for-group group)))
4071       (error "Couldn't open server"))
4072
4073     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4074         (gnus-activate-group group)     ; Or we can activate it...
4075         (progn                          ; Or we bug out.
4076           (when (equal major-mode 'gnus-summary-mode)
4077             (kill-buffer (current-buffer)))
4078           (error "Couldn't request group %s: %s"
4079                  group (gnus-status-message group))))
4080
4081     (unless (gnus-request-group group t)
4082       (when (equal major-mode 'gnus-summary-mode)
4083         (kill-buffer (current-buffer)))
4084       (error "Couldn't request group %s: %s"
4085              group (gnus-status-message group)))
4086
4087     (setq gnus-newsgroup-name group)
4088     (setq gnus-newsgroup-unselected nil)
4089     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4090     (gnus-summary-setup-default-charset)
4091
4092     ;; Adjust and set lists of article marks.
4093     (when info
4094       (gnus-adjust-marked-articles info))
4095
4096     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4097     (when (gnus-virtual-group-p group)
4098       (setq cached gnus-newsgroup-cached))
4099
4100     (setq gnus-newsgroup-unreads
4101           (gnus-set-difference
4102            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4103            gnus-newsgroup-dormant))
4104
4105     (setq gnus-newsgroup-processable nil)
4106
4107     (gnus-update-read-articles group gnus-newsgroup-unreads)
4108
4109     (if (setq articles select-articles)
4110         (setq gnus-newsgroup-unselected
4111               (gnus-sorted-intersection
4112                gnus-newsgroup-unreads
4113                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4114       (setq articles (gnus-articles-to-read group read-all)))
4115
4116     (cond
4117      ((null articles)
4118       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4119       'quit)
4120      ((eq articles 0) nil)
4121      (t
4122       ;; Init the dependencies hash table.
4123       (setq gnus-newsgroup-dependencies
4124             (gnus-make-hashtable (length articles)))
4125       (gnus-set-global-variables)
4126       ;; Retrieve the headers and read them in.
4127       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4128       (setq gnus-newsgroup-headers
4129             (if (eq 'nov
4130                     (setq gnus-headers-retrieved-by
4131                           (gnus-retrieve-headers
4132                            articles gnus-newsgroup-name
4133                            ;; We might want to fetch old headers, but
4134                            ;; not if there is only 1 article.
4135                            (and (or (and
4136                                      (not (eq gnus-fetch-old-headers 'some))
4137                                      (not (numberp gnus-fetch-old-headers)))
4138                                     (> (length articles) 1))
4139                                 gnus-fetch-old-headers))))
4140                 (gnus-get-newsgroup-headers-xover
4141                  articles nil nil gnus-newsgroup-name t)
4142               (gnus-get-newsgroup-headers)))
4143       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4144
4145       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4146       (when cached
4147         (setq gnus-newsgroup-cached cached))
4148
4149       ;; Suppress duplicates?
4150       (when gnus-suppress-duplicates
4151         (gnus-dup-suppress-articles))
4152
4153       ;; Set the initial limit.
4154       (setq gnus-newsgroup-limit (copy-sequence articles))
4155       ;; Remove canceled articles from the list of unread articles.
4156       (setq gnus-newsgroup-unreads
4157             (gnus-set-sorted-intersection
4158              gnus-newsgroup-unreads
4159              (setq fetched-articles
4160                    (mapcar (lambda (headers) (mail-header-number headers))
4161                            gnus-newsgroup-headers))))
4162       ;; Removed marked articles that do not exist.
4163       (gnus-update-missing-marks
4164        (gnus-sorted-complement fetched-articles articles))
4165       ;; We might want to build some more threads first.
4166       (when (and gnus-fetch-old-headers
4167                  (eq gnus-headers-retrieved-by 'nov))
4168         (if (eq gnus-fetch-old-headers 'invisible)
4169             (gnus-build-all-threads)
4170           (gnus-build-old-threads)))
4171       ;; Let the Gnus agent mark articles as read.
4172       (when gnus-agent
4173         (gnus-agent-get-undownloaded-list))
4174       ;; Remove list identifiers from subject
4175       (when gnus-list-identifiers
4176         (gnus-summary-remove-list-identifiers))
4177       ;; Check whether auto-expire is to be done in this group.
4178       (setq gnus-newsgroup-auto-expire
4179             (gnus-group-auto-expirable-p group))
4180       ;; Set up the article buffer now, if necessary.
4181       (unless gnus-single-article-buffer
4182         (gnus-article-setup-buffer))
4183       ;; First and last article in this newsgroup.
4184       (when gnus-newsgroup-headers
4185         (setq gnus-newsgroup-begin
4186               (mail-header-number (car gnus-newsgroup-headers))
4187               gnus-newsgroup-end
4188               (mail-header-number
4189                (gnus-last-element gnus-newsgroup-headers))))
4190       ;; GROUP is successfully selected.
4191       (or gnus-newsgroup-headers t)))))
4192
4193 (defun gnus-articles-to-read (group &optional read-all)
4194   ;; Find out what articles the user wants to read.
4195   (let* ((articles
4196           ;; Select all articles if `read-all' is non-nil, or if there
4197           ;; are no unread articles.
4198           (if (or read-all
4199                   (and (zerop (length gnus-newsgroup-marked))
4200                        (zerop (length gnus-newsgroup-unreads)))
4201                   (eq (gnus-group-find-parameter group 'display)
4202                       'all))
4203               (or
4204                (gnus-uncompress-range (gnus-active group))
4205                (gnus-cache-articles-in-group group))
4206             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4207                           (copy-sequence gnus-newsgroup-unreads))
4208                   '<)))
4209          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4210          (scored (length scored-list))
4211          (number (length articles))
4212          (marked (+ (length gnus-newsgroup-marked)
4213                     (length gnus-newsgroup-dormant)))
4214          (select
4215           (cond
4216            ((numberp read-all)
4217             read-all)
4218            (t
4219             (condition-case ()
4220                 (cond
4221                  ((and (or (<= scored marked) (= scored number))
4222                        (numberp gnus-large-newsgroup)
4223                        (> number gnus-large-newsgroup))
4224                   (let ((input
4225                          (read-string
4226                           (format
4227                            "How many articles from %s (default %d): "
4228                            (gnus-limit-string gnus-newsgroup-name 35)
4229                            number))))
4230                     (if (string-match "^[ \t]*$" input) number input)))
4231                  ((and (> scored marked) (< scored number)
4232                        (> (- scored number) 20))
4233                   (let ((input
4234                          (read-string
4235                           (format "%s %s (%d scored, %d total): "
4236                                   "How many articles from"
4237                                   group scored number))))
4238                     (if (string-match "^[ \t]*$" input)
4239                         number input)))
4240                  (t number))
4241               (quit nil))))))
4242     (setq select (if (stringp select) (string-to-number select) select))
4243     (if (or (null select) (zerop select))
4244         select
4245       (if (and (not (zerop scored)) (<= (abs select) scored))
4246           (progn
4247             (setq articles (sort scored-list '<))
4248             (setq number (length articles)))
4249         (setq articles (copy-sequence articles)))
4250
4251       (when (< (abs select) number)
4252         (if (< select 0)
4253             ;; Select the N oldest articles.
4254             (setcdr (nthcdr (1- (abs select)) articles) nil)
4255           ;; Select the N most recent articles.
4256           (setq articles (nthcdr (- number select) articles))))
4257       (setq gnus-newsgroup-unselected
4258             (gnus-sorted-intersection
4259              gnus-newsgroup-unreads
4260              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4261       articles)))
4262
4263 (defun gnus-killed-articles (killed articles)
4264   (let (out)
4265     (while articles
4266       (when (inline (gnus-member-of-range (car articles) killed))
4267         (push (car articles) out))
4268       (setq articles (cdr articles)))
4269     out))
4270
4271 (defun gnus-uncompress-marks (marks)
4272   "Uncompress the mark ranges in MARKS."
4273   (let ((uncompressed '(score bookmark))
4274         out)
4275     (while marks
4276       (if (memq (caar marks) uncompressed)
4277           (push (car marks) out)
4278         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4279       (setq marks (cdr marks)))
4280     out))
4281
4282 (defun gnus-adjust-marked-articles (info)
4283   "Set all article lists and remove all marks that are no longer valid."
4284   (let* ((marked-lists (gnus-info-marks info))
4285          (active (gnus-active (gnus-info-group info)))
4286          (min (car active))
4287          (max (cdr active))
4288          (types gnus-article-mark-lists)
4289          (uncompressed '(score bookmark killed))
4290          marks var articles article mark)
4291
4292     (while marked-lists
4293       (setq marks (pop marked-lists))
4294       (set (setq var (intern (format "gnus-newsgroup-%s"
4295                                      (car (rassq (setq mark (car marks))
4296                                                  types)))))
4297            (if (memq (car marks) uncompressed) (cdr marks)
4298              (gnus-uncompress-range (cdr marks))))
4299
4300       (setq articles (symbol-value var))
4301
4302       ;; All articles have to be subsets of the active articles.
4303       (cond
4304        ;; Adjust "simple" lists.
4305        ((memq mark '(tick dormant expire reply save))
4306         (while articles
4307           (when (or (< (setq article (pop articles)) min) (> article max))
4308             (set var (delq article (symbol-value var))))))
4309        ;; Adjust assocs.
4310        ((memq mark uncompressed)
4311         (when (not (listp (cdr (symbol-value var))))
4312           (set var (list (symbol-value var))))
4313         (when (not (listp (cdr articles)))
4314           (setq articles (list articles)))
4315         (while articles
4316           (when (or (not (consp (setq article (pop articles))))
4317                     (< (car article) min)
4318                     (> (car article) max))
4319             (set var (delq article (symbol-value var))))))))))
4320
4321 (defun gnus-update-missing-marks (missing)
4322   "Go through the list of MISSING articles and remove them from the mark lists."
4323   (when missing
4324     (let ((types gnus-article-mark-lists)
4325           var m)
4326       ;; Go through all types.
4327       (while types
4328         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4329         (when (symbol-value var)
4330           ;; This list has articles.  So we delete all missing articles
4331           ;; from it.
4332           (setq m missing)
4333           (while m
4334             (set var (delq (pop m) (symbol-value var)))))))))
4335
4336 (defun gnus-update-marks ()
4337   "Enter the various lists of marked articles into the newsgroup info list."
4338   (let ((types gnus-article-mark-lists)
4339         (info (gnus-get-info gnus-newsgroup-name))
4340         (uncompressed '(score bookmark killed))
4341         type list newmarked symbol delta-marks)
4342     (when info
4343       ;; Add all marks lists to the list of marks lists.
4344       (while (setq type (pop types))
4345         (setq list (symbol-value
4346                     (setq symbol
4347                           (intern (format "gnus-newsgroup-%s"
4348                                           (car type))))))
4349
4350         (when list
4351           ;; Get rid of the entries of the articles that have the
4352           ;; default score.
4353           (when (and (eq (cdr type) 'score)
4354                      gnus-save-score
4355                      list)
4356             (let* ((arts list)
4357                    (prev (cons nil list))
4358                    (all prev))
4359               (while arts
4360                 (if (or (not (consp (car arts)))
4361                         (= (cdar arts) gnus-summary-default-score))
4362                     (setcdr prev (cdr arts))
4363                   (setq prev arts))
4364                 (setq arts (cdr arts)))
4365               (setq list (cdr all)))))
4366
4367         (or (memq (cdr type) uncompressed)
4368             (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4369        
4370         (when (gnus-check-backend-function 'request-set-mark
4371                                            gnus-newsgroup-name)
4372           ;; uncompressed:s are not proper flags (they are cons cells)
4373           ;; cache is a internal gnus flag
4374           (unless (memq (cdr type) (cons 'cache uncompressed))
4375             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4376                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4377                    (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4378               (if add
4379                   (push (list add 'add (list (cdr type))) delta-marks))
4380               (if del
4381                   (push (list del 'del (list (cdr type))) delta-marks)))))
4382           
4383         (when list
4384           (push (cons (cdr type) list) newmarked)))
4385
4386       (when delta-marks
4387         (unless (gnus-check-group gnus-newsgroup-name)
4388           (error "Can't open server for %s" gnus-newsgroup-name))
4389         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4390           
4391       ;; Enter these new marks into the info of the group.
4392       (if (nthcdr 3 info)
4393           (setcar (nthcdr 3 info) newmarked)
4394         ;; Add the marks lists to the end of the info.
4395         (when newmarked
4396           (setcdr (nthcdr 2 info) (list newmarked))))
4397
4398       ;; Cut off the end of the info if there's nothing else there.
4399       (let ((i 5))
4400         (while (and (> i 2)
4401                     (not (nth i info)))
4402           (when (nthcdr (decf i) info)
4403             (setcdr (nthcdr i info) nil)))))))
4404
4405 (defun gnus-set-mode-line (where)
4406   "This function sets the mode line of the article or summary buffers.
4407 If WHERE is `summary', the summary mode line format will be used."
4408   ;; Is this mode line one we keep updated?
4409   (when (and (memq where gnus-updated-mode-lines)
4410              (symbol-value
4411               (intern (format "gnus-%s-mode-line-format-spec" where))))
4412     (let (mode-string)
4413       (save-excursion
4414         ;; We evaluate this in the summary buffer since these
4415         ;; variables are buffer-local to that buffer.
4416         (set-buffer gnus-summary-buffer)
4417         ;; We bind all these variables that are used in the `eval' form
4418         ;; below.
4419         (let* ((mformat (symbol-value
4420                          (intern
4421                           (format "gnus-%s-mode-line-format-spec" where))))
4422                (gnus-tmp-group-name gnus-newsgroup-name)
4423                (gnus-tmp-article-number (or gnus-current-article 0))
4424                (gnus-tmp-unread gnus-newsgroup-unreads)
4425                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4426                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4427                (gnus-tmp-unread-and-unselected
4428                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4429                             (zerop gnus-tmp-unselected))
4430                        "")
4431                       ((zerop gnus-tmp-unselected)
4432                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4433                       (t (format "{%d(+%d) more}"
4434                                  gnus-tmp-unread-and-unticked
4435                                  gnus-tmp-unselected))))
4436                (gnus-tmp-subject
4437                 (if (and gnus-current-headers
4438                          (vectorp gnus-current-headers))
4439                     (gnus-mode-string-quote
4440                      (mail-header-subject gnus-current-headers))
4441                   ""))
4442                bufname-length max-len
4443                gnus-tmp-header);; passed as argument to any user-format-funcs
4444           (setq mode-string (eval mformat))
4445           (setq bufname-length (if (string-match "%b" mode-string)
4446                                    (- (length
4447                                        (buffer-name
4448                                         (if (eq where 'summary)
4449                                             nil
4450                                           (get-buffer gnus-article-buffer))))
4451                                       2)
4452                                  0))
4453           (setq max-len (max 4 (if gnus-mode-non-string-length
4454                                    (- (window-width)
4455                                       gnus-mode-non-string-length
4456                                       bufname-length)
4457                                  (length mode-string))))
4458           ;; We might have to chop a bit of the string off...
4459           (when (> (length mode-string) max-len)
4460             (setq mode-string
4461                   (concat (truncate-string-to-width mode-string (- max-len 3))
4462                           "...")))
4463           ;; Pad the mode string a bit.
4464           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4465       ;; Update the mode line.
4466       (setq mode-line-buffer-identification
4467             (gnus-mode-line-buffer-identification (list mode-string)))
4468       (set-buffer-modified-p t))))
4469
4470 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4471   "Go through the HEADERS list and add all Xrefs to a hash table.
4472 The resulting hash table is returned, or nil if no Xrefs were found."
4473   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4474          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4475          (xref-hashtb (gnus-make-hashtable))
4476          start group entry number xrefs header)
4477     (while headers
4478       (setq header (pop headers))
4479       (when (and (setq xrefs (mail-header-xref header))
4480                  (not (memq (setq number (mail-header-number header))
4481                             unreads)))
4482         (setq start 0)
4483         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4484           (setq start (match-end 0))
4485           (setq group (if prefix
4486                           (concat prefix (substring xrefs (match-beginning 1)
4487                                                     (match-end 1)))
4488                         (substring xrefs (match-beginning 1) (match-end 1))))
4489           (setq number
4490                 (string-to-int (substring xrefs (match-beginning 2)
4491                                           (match-end 2))))
4492           (if (setq entry (gnus-gethash group xref-hashtb))
4493               (setcdr entry (cons number (cdr entry)))
4494             (gnus-sethash group (cons number nil) xref-hashtb)))))
4495     (and start xref-hashtb)))
4496
4497 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4498   "Look through all the headers and mark the Xrefs as read."
4499   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4500         name entry info xref-hashtb idlist method nth4)
4501     (save-excursion
4502       (set-buffer gnus-group-buffer)
4503       (when (setq xref-hashtb
4504                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4505         (mapatoms
4506          (lambda (group)
4507            (unless (string= from-newsgroup (setq name (symbol-name group)))
4508              (setq idlist (symbol-value group))
4509              ;; Dead groups are not updated.
4510              (and (prog1
4511                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4512                             info (nth 2 entry))
4513                     (when (stringp (setq nth4 (gnus-info-method info)))
4514                       (setq nth4 (gnus-server-to-method nth4))))
4515                   ;; Only do the xrefs if the group has the same
4516                   ;; select method as the group we have just read.
4517                   (or (gnus-methods-equal-p
4518                        nth4 (gnus-find-method-for-group from-newsgroup))
4519                       virtual
4520                       (equal nth4 (setq method (gnus-find-method-for-group
4521                                                 from-newsgroup)))
4522                       (and (equal (car nth4) (car method))
4523                            (equal (nth 1 nth4) (nth 1 method))))
4524                   gnus-use-cross-reference
4525                   (or (not (eq gnus-use-cross-reference t))
4526                       virtual
4527                       ;; Only do cross-references on subscribed
4528                       ;; groups, if that is what is wanted.
4529                       (<= (gnus-info-level info) gnus-level-subscribed))
4530                   (gnus-group-make-articles-read name idlist))))
4531          xref-hashtb)))))
4532
4533 (defun gnus-compute-read-articles (group articles)
4534   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4535          (info (nth 2 entry))
4536          (active (gnus-active group))
4537          ninfo)
4538     (when entry
4539       ;; First peel off all invalid article numbers.
4540       (when active
4541         (let ((ids articles)
4542               id first)
4543           (while (setq id (pop ids))
4544             (when (and first (> id (cdr active)))
4545               ;; We'll end up in this situation in one particular
4546               ;; obscure situation.  If you re-scan a group and get
4547               ;; a new article that is cross-posted to a different
4548               ;; group that has not been re-scanned, you might get
4549               ;; crossposted article that has a higher number than
4550               ;; Gnus believes possible.  So we re-activate this
4551               ;; group as well.  This might mean doing the
4552               ;; crossposting thingy will *increase* the number
4553               ;; of articles in some groups.  Tsk, tsk.
4554               (setq active (or (gnus-activate-group group) active)))
4555             (when (or (> id (cdr active))
4556                       (< id (car active)))
4557               (setq articles (delq id articles))))))
4558       ;; If the read list is nil, we init it.
4559       (if (and active
4560                (null (gnus-info-read info))
4561                (> (car active) 1))
4562           (setq ninfo (cons 1 (1- (car active))))
4563         (setq ninfo (gnus-info-read info)))
4564       ;; Then we add the read articles to the range.
4565       (gnus-add-to-range
4566        ninfo (setq articles (sort articles '<))))))
4567
4568 (defun gnus-group-make-articles-read (group articles)
4569   "Update the info of GROUP to say that ARTICLES are read."
4570   (let* ((num 0)
4571          (entry (gnus-gethash group gnus-newsrc-hashtb))
4572          (info (nth 2 entry))
4573          (active (gnus-active group))
4574          range)
4575     (when entry
4576       (setq range (gnus-compute-read-articles group articles))
4577       (save-excursion
4578         (set-buffer gnus-group-buffer)
4579         (gnus-undo-register
4580           `(progn
4581              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4582              (gnus-info-set-read ',info ',(gnus-info-read info))
4583              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4584              (gnus-group-update-group ,group t))))
4585       ;; Add the read articles to the range.
4586       (gnus-info-set-read info range)
4587       ;; Then we have to re-compute how many unread
4588       ;; articles there are in this group.
4589       (when active
4590         (cond
4591          ((not range)
4592           (setq num (- (1+ (cdr active)) (car active))))
4593          ((not (listp (cdr range)))
4594           (setq num (- (cdr active) (- (1+ (cdr range))
4595                                        (car range)))))
4596          (t
4597           (while range
4598             (if (numberp (car range))
4599                 (setq num (1+ num))
4600               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4601             (setq range (cdr range)))
4602           (setq num (- (cdr active) num))))
4603         ;; Update the number of unread articles.
4604         (setcar entry num)
4605         ;; Update the group buffer.
4606         (gnus-group-update-group group t)))))
4607
4608 (defvar gnus-newsgroup-none-id 0)
4609
4610 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4611   (let ((cur nntp-server-buffer)
4612         (dependencies
4613          (or dependencies
4614              (save-excursion (set-buffer gnus-summary-buffer)
4615                              gnus-newsgroup-dependencies)))
4616         headers id end ref
4617         (mail-parse-charset gnus-newsgroup-charset)
4618         (mail-parse-ignored-charsets 
4619          (save-excursion (condition-case nil
4620                              (set-buffer gnus-summary-buffer)
4621                            (error))
4622                          gnus-newsgroup-ignored-charsets)))
4623     (save-excursion
4624       (set-buffer nntp-server-buffer)
4625       ;; Translate all TAB characters into SPACE characters.
4626       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4627       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4628       (gnus-run-hooks 'gnus-parse-headers-hook)
4629       (let ((case-fold-search t)
4630             in-reply-to header p lines chars)
4631         (goto-char (point-min))
4632         ;; Search to the beginning of the next header.  Error messages
4633         ;; do not begin with 2 or 3.
4634         (while (re-search-forward "^[23][0-9]+ " nil t)
4635           (setq id nil
4636                 ref nil)
4637           ;; This implementation of this function, with nine
4638           ;; search-forwards instead of the one re-search-forward and
4639           ;; a case (which basically was the old function) is actually
4640           ;; about twice as fast, even though it looks messier.  You
4641           ;; can't have everything, I guess.  Speed and elegance
4642           ;; doesn't always go hand in hand.
4643           (setq
4644            header
4645            (vector
4646             ;; Number.
4647             (prog1
4648                 (read cur)
4649               (end-of-line)
4650               (setq p (point))
4651               (narrow-to-region (point)
4652                                 (or (and (search-forward "\n.\n" nil t)
4653                                          (- (point) 2))
4654                                     (point))))
4655             ;; Subject.
4656             (progn
4657               (goto-char p)
4658               (if (search-forward "\nsubject: " nil t)
4659                   (funcall gnus-decode-encoded-word-function
4660                            (nnheader-header-value))
4661                 "(none)"))
4662             ;; From.
4663             (progn
4664               (goto-char p)
4665               (if (search-forward "\nfrom: " nil t)
4666                   (funcall gnus-decode-encoded-word-function
4667                            (nnheader-header-value))
4668                 "(nobody)"))
4669             ;; Date.
4670             (progn
4671               (goto-char p)
4672               (if (search-forward "\ndate: " nil t)
4673                   (nnheader-header-value) ""))
4674             ;; Message-ID.
4675             (progn
4676               (goto-char p)
4677               (setq id (if (re-search-forward
4678                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4679                            ;; We do it this way to make sure the Message-ID
4680                            ;; is (somewhat) syntactically valid.
4681                            (buffer-substring (match-beginning 1)
4682                                              (match-end 1))
4683                          ;; If there was no message-id, we just fake one
4684                          ;; to make subsequent routines simpler.
4685                          (nnheader-generate-fake-message-id))))
4686             ;; References.
4687             (progn
4688               (goto-char p)
4689               (if (search-forward "\nreferences: " nil t)
4690                   (progn
4691                     (setq end (point))
4692                     (prog1
4693                         (nnheader-header-value)
4694                       (setq ref
4695                             (buffer-substring
4696                              (progn
4697                                (end-of-line)
4698                                (search-backward ">" end t)
4699                                (1+ (point)))
4700                              (progn
4701                                (search-backward "<" end t)
4702                                (point))))))
4703                 ;; Get the references from the in-reply-to header if there
4704                 ;; were no references and the in-reply-to header looks
4705                 ;; promising.
4706                 (if (and (search-forward "\nin-reply-to: " nil t)
4707                          (setq in-reply-to (nnheader-header-value))
4708                          (string-match "<[^>]+>" in-reply-to))
4709                     (let (ref2)
4710                       (setq ref (substring in-reply-to (match-beginning 0)
4711                                            (match-end 0)))
4712                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4713                         (setq ref2 (substring in-reply-to (match-beginning 0)
4714                                               (match-end 0)))
4715                         (when (> (length ref2) (length ref))
4716                           (setq ref ref2)))
4717                       ref)
4718                   (setq ref nil))))
4719             ;; Chars.
4720             (progn
4721               (goto-char p)
4722               (if (search-forward "\nchars: " nil t)
4723                   (if (numberp (setq chars (ignore-errors (read cur))))
4724                       chars 0)
4725                 0))
4726             ;; Lines.
4727             (progn
4728               (goto-char p)
4729               (if (search-forward "\nlines: " nil t)
4730                   (if (numberp (setq lines (ignore-errors (read cur))))
4731                       lines 0)
4732                 0))
4733             ;; Xref.
4734             (progn
4735               (goto-char p)
4736               (and (search-forward "\nxref: " nil t)
4737                    (nnheader-header-value)))
4738             ;; Extra.
4739             (when gnus-extra-headers
4740               (let ((extra gnus-extra-headers)
4741                     out)
4742                 (while extra
4743                   (goto-char p)
4744                   (when (search-forward
4745                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4746                     (push (cons (car extra) (nnheader-header-value))
4747                           out))
4748                   (pop extra))
4749                 out))))
4750           (when (equal id ref)
4751             (setq ref nil))
4752
4753           (when gnus-alter-header-function
4754             (funcall gnus-alter-header-function header)
4755             (setq id (mail-header-id header)
4756                   ref (gnus-parent-id (mail-header-references header))))
4757
4758           (when (setq header
4759                       (gnus-dependencies-add-header
4760                        header dependencies force-new))
4761             (push header headers))
4762           (goto-char (point-max))
4763           (widen))
4764         (nreverse headers)))))
4765
4766 ;; Goes through the xover lines and returns a list of vectors
4767 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4768                                                   force-new dependencies
4769                                                   group also-fetch-heads)
4770   "Parse the news overview data in the server buffer, and return a
4771 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4772   ;; Get the Xref when the users reads the articles since most/some
4773   ;; NNTP servers do not include Xrefs when using XOVER.
4774   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4775   (let ((mail-parse-charset gnus-newsgroup-charset)
4776         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4777         (cur nntp-server-buffer)
4778         (dependencies (or dependencies gnus-newsgroup-dependencies))
4779         number headers header)
4780     (save-excursion
4781       (set-buffer nntp-server-buffer)
4782       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4783       ;; Allow the user to mangle the headers before parsing them.
4784       (gnus-run-hooks 'gnus-parse-headers-hook)
4785       (goto-char (point-min))
4786       (while (not (eobp))
4787         (condition-case ()
4788             (while (and sequence (not (eobp)))
4789               (setq number (read cur))
4790               (while (and sequence
4791                           (< (car sequence) number))
4792                 (setq sequence (cdr sequence)))
4793               (and sequence
4794                    (eq number (car sequence))
4795                    (progn
4796                      (setq sequence (cdr sequence))
4797                      (setq header (inline
4798                                     (gnus-nov-parse-line
4799                                      number dependencies force-new))))
4800                    (push header headers))
4801               (forward-line 1))
4802           (error
4803            (gnus-error 4 "Strange nov line (%d)"
4804                        (count-lines (point-min) (point)))))
4805         (forward-line 1))
4806       ;; A common bug in inn is that if you have posted an article and
4807       ;; then retrieves the active file, it will answer correctly --
4808       ;; the new article is included.  However, a NOV entry for the
4809       ;; article may not have been generated yet, so this may fail.
4810       ;; We work around this problem by retrieving the last few
4811       ;; headers using HEAD.
4812       (if (or (not also-fetch-heads)
4813               (not sequence))
4814           ;; We (probably) got all the headers.
4815           (nreverse headers)
4816         (let ((gnus-nov-is-evil t))
4817           (nconc
4818            (nreverse headers)
4819            (when (gnus-retrieve-headers sequence group)
4820              (gnus-get-newsgroup-headers))))))))
4821
4822 (defun gnus-article-get-xrefs ()
4823   "Fill in the Xref value in `gnus-current-headers', if necessary.
4824 This is meant to be called in `gnus-article-internal-prepare-hook'."
4825   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4826                                  gnus-current-headers)))
4827     (or (not gnus-use-cross-reference)
4828         (not headers)
4829         (and (mail-header-xref headers)
4830              (not (string= (mail-header-xref headers) "")))
4831         (let ((case-fold-search t)
4832               xref)
4833           (save-restriction
4834             (nnheader-narrow-to-headers)
4835             (goto-char (point-min))
4836             (when (or (and (not (eobp))
4837                            (eq (downcase (char-after)) ?x)
4838                            (looking-at "Xref:"))
4839                       (search-forward "\nXref:" nil t))
4840               (goto-char (1+ (match-end 0)))
4841               (setq xref (buffer-substring (point)
4842                                            (progn (end-of-line) (point))))
4843               (mail-header-set-xref headers xref)))))))
4844
4845 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4846   "Find article ID and insert the summary line for that article.
4847 OLD-HEADER can either be a header or a line number to insert
4848 the subject line on."
4849   (let* ((line (and (numberp old-header) old-header))
4850          (old-header (and (vectorp old-header) old-header))
4851          (header (cond ((and old-header use-old-header)
4852                         old-header)
4853                        ((and (numberp id)
4854                              (gnus-number-to-header id))
4855                         (gnus-number-to-header id))
4856                        (t
4857                         (gnus-read-header id))))
4858          (number (and (numberp id) id))
4859          d)
4860     (when header
4861       ;; Rebuild the thread that this article is part of and go to the
4862       ;; article we have fetched.
4863       (when (and (not gnus-show-threads)
4864                  old-header)
4865         (when (and number
4866                    (setq d (gnus-data-find (mail-header-number old-header))))
4867           (goto-char (gnus-data-pos d))
4868           (gnus-data-remove
4869            number
4870            (- (gnus-point-at-bol)
4871               (prog1
4872                   (1+ (gnus-point-at-eol))
4873                 (gnus-delete-line))))))
4874       (when old-header
4875         (mail-header-set-number header (mail-header-number old-header)))
4876       (setq gnus-newsgroup-sparse
4877             (delq (setq number (mail-header-number header))
4878                   gnus-newsgroup-sparse))
4879       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4880       (push number gnus-newsgroup-limit)
4881       (gnus-rebuild-thread (mail-header-id header) line)
4882       (gnus-summary-goto-subject number nil t))
4883     (when (and (numberp number)
4884                (> number 0))
4885       ;; We have to update the boundaries even if we can't fetch the
4886       ;; article if ID is a number -- so that the next `P' or `N'
4887       ;; command will fetch the previous (or next) article even
4888       ;; if the one we tried to fetch this time has been canceled.
4889       (when (> number gnus-newsgroup-end)
4890         (setq gnus-newsgroup-end number))
4891       (when (< number gnus-newsgroup-begin)
4892         (setq gnus-newsgroup-begin number))
4893       (setq gnus-newsgroup-unselected
4894             (delq number gnus-newsgroup-unselected)))
4895     ;; Report back a success?
4896     (and header (mail-header-number header))))
4897
4898 ;;; Process/prefix in the summary buffer
4899
4900 (defun gnus-summary-work-articles (n)
4901   "Return a list of articles to be worked upon.
4902 The prefix argument, the list of process marked articles, and the
4903 current article will be taken into consideration."
4904   (save-excursion
4905     (set-buffer gnus-summary-buffer)
4906     (cond
4907      (n
4908       ;; A numerical prefix has been given.
4909       (setq n (prefix-numeric-value n))
4910       (let ((backward (< n 0))
4911             (n (abs (prefix-numeric-value n)))
4912             articles article)
4913         (save-excursion
4914           (while
4915               (and (> n 0)
4916                    (push (setq article (gnus-summary-article-number))
4917                          articles)
4918                    (if backward
4919                        (gnus-summary-find-prev nil article)
4920                      (gnus-summary-find-next nil article)))
4921             (decf n)))
4922         (nreverse articles)))
4923      ((and (gnus-region-active-p) (mark))
4924       (message "region active")
4925       ;; Work on the region between point and mark.
4926       (let ((max (max (point) (mark)))
4927             articles article)
4928         (save-excursion
4929           (goto-char (min (min (point) (mark))))
4930           (while
4931               (and
4932                (push (setq article (gnus-summary-article-number)) articles)
4933                (gnus-summary-find-next nil article)
4934                (< (point) max)))
4935           (nreverse articles))))
4936      (gnus-newsgroup-processable
4937       ;; There are process-marked articles present.
4938       ;; Save current state.
4939       (gnus-summary-save-process-mark)
4940       ;; Return the list.
4941       (reverse gnus-newsgroup-processable))
4942      (t
4943       ;; Just return the current article.
4944       (list (gnus-summary-article-number))))))
4945
4946 (defmacro gnus-summary-iterate (arg &rest forms)
4947   "Iterate over the process/prefixed articles and do FORMS.
4948 ARG is the interactive prefix given to the command.  FORMS will be
4949 executed with point over the summary line of the articles."
4950   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4951     `(let ((,articles (gnus-summary-work-articles ,arg)))
4952        (while ,articles
4953          (gnus-summary-goto-subject (car ,articles))
4954          ,@forms
4955          (pop ,articles)))))
4956
4957 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4958 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4959
4960 (defun gnus-summary-save-process-mark ()
4961   "Push the current set of process marked articles on the stack."
4962   (interactive)
4963   (push (copy-sequence gnus-newsgroup-processable)
4964         gnus-newsgroup-process-stack))
4965
4966 (defun gnus-summary-kill-process-mark ()
4967   "Push the current set of process marked articles on the stack and unmark."
4968   (interactive)
4969   (gnus-summary-save-process-mark)
4970   (gnus-summary-unmark-all-processable))
4971
4972 (defun gnus-summary-yank-process-mark ()
4973   "Pop the last process mark state off the stack and restore it."
4974   (interactive)
4975   (unless gnus-newsgroup-process-stack
4976     (error "Empty mark stack"))
4977   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4978
4979 (defun gnus-summary-process-mark-set (set)
4980   "Make SET into the current process marked articles."
4981   (gnus-summary-unmark-all-processable)
4982   (while set
4983     (gnus-summary-set-process-mark (pop set))))
4984
4985 ;;; Searching and stuff
4986
4987 (defun gnus-summary-search-group (&optional backward use-level)
4988   "Search for next unread newsgroup.
4989 If optional argument BACKWARD is non-nil, search backward instead."
4990   (save-excursion
4991     (set-buffer gnus-group-buffer)
4992     (when (gnus-group-search-forward
4993            backward nil (if use-level (gnus-group-group-level) nil))
4994       (gnus-group-group-name))))
4995
4996 (defun gnus-summary-best-group (&optional exclude-group)
4997   "Find the name of the best unread group.
4998 If EXCLUDE-GROUP, do not go to this group."
4999   (save-excursion
5000     (set-buffer gnus-group-buffer)
5001     (save-excursion
5002       (gnus-group-best-unread-group exclude-group))))
5003
5004 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5005   (if backward (gnus-summary-find-prev)
5006     (let* ((dummy (gnus-summary-article-intangible-p))
5007            (article (or article (gnus-summary-article-number)))
5008            (arts (gnus-data-find-list article))
5009            result)
5010       (when (and (not dummy)
5011                  (or (not gnus-summary-check-current)
5012                      (not unread)
5013                      (not (gnus-data-unread-p (car arts)))))
5014         (setq arts (cdr arts)))
5015       (when (setq result
5016                   (if unread
5017                       (progn
5018                         (while arts
5019                           (when (or (and undownloaded
5020                                          (eq gnus-undownloaded-mark
5021                                              (gnus-data-mark (car arts))))
5022                                     (gnus-data-unread-p (car arts)))
5023                             (setq result (car arts)
5024                                   arts nil))
5025                           (setq arts (cdr arts)))
5026                         result)
5027                     (car arts)))
5028         (goto-char (gnus-data-pos result))
5029         (gnus-data-number result)))))
5030
5031 (defun gnus-summary-find-prev (&optional unread article)
5032   (let* ((eobp (eobp))
5033          (article (or article (gnus-summary-article-number)))
5034          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5035          result)
5036     (when (and (not eobp)
5037                (or (not gnus-summary-check-current)
5038                    (not unread)
5039                    (not (gnus-data-unread-p (car arts)))))
5040       (setq arts (cdr arts)))
5041     (when (setq result
5042                 (if unread
5043                     (progn
5044                       (while arts
5045                         (when (gnus-data-unread-p (car arts))
5046                           (setq result (car arts)
5047                                 arts nil))
5048                         (setq arts (cdr arts)))
5049                       result)
5050                   (car arts)))
5051       (goto-char (gnus-data-pos result))
5052       (gnus-data-number result))))
5053
5054 (defun gnus-summary-find-subject (subject &optional unread backward article)
5055   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5056          (article (or article (gnus-summary-article-number)))
5057          (articles (gnus-data-list backward))
5058          (arts (gnus-data-find-list article articles))
5059          result)
5060     (when (or (not gnus-summary-check-current)
5061               (not unread)
5062               (not (gnus-data-unread-p (car arts))))
5063       (setq arts (cdr arts)))
5064     (while arts
5065       (and (or (not unread)
5066                (gnus-data-unread-p (car arts)))
5067            (vectorp (gnus-data-header (car arts)))
5068            (gnus-subject-equal
5069             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5070            (setq result (car arts)
5071                  arts nil))
5072       (setq arts (cdr arts)))
5073     (and result
5074          (goto-char (gnus-data-pos result))
5075          (gnus-data-number result))))
5076
5077 (defun gnus-summary-search-forward (&optional unread subject backward)
5078   "Search forward for an article.
5079 If UNREAD, look for unread articles.  If SUBJECT, look for
5080 articles with that subject.  If BACKWARD, search backward instead."
5081   (cond (subject (gnus-summary-find-subject subject unread backward))
5082         (backward (gnus-summary-find-prev unread))
5083         (t (gnus-summary-find-next unread))))
5084
5085 (defun gnus-recenter (&optional n)
5086   "Center point in window and redisplay frame.
5087 Also do horizontal recentering."
5088   (interactive "P")
5089   (when (and gnus-auto-center-summary
5090              (not (eq gnus-auto-center-summary 'vertical)))
5091     (gnus-horizontal-recenter))
5092   (recenter n))
5093
5094 (defun gnus-summary-recenter ()
5095   "Center point in the summary window.
5096 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5097 displayed, no centering will be performed."
5098   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5099   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5100   (let* ((top (cond ((< (window-height) 4) 0)
5101                     ((< (window-height) 7) 1)
5102                     (t (if (numberp gnus-auto-center-summary)
5103                            gnus-auto-center-summary
5104                          2))))
5105          (height (1- (window-height)))
5106          (bottom (save-excursion (goto-char (point-max))
5107                                  (forward-line (- height))
5108                                  (point)))
5109          (window (get-buffer-window (current-buffer))))
5110     ;; The user has to want it.
5111     (when gnus-auto-center-summary
5112       (when (get-buffer-window gnus-article-buffer)
5113         ;; Only do recentering when the article buffer is displayed,
5114         ;; Set the window start to either `bottom', which is the biggest
5115         ;; possible valid number, or the second line from the top,
5116         ;; whichever is the least.
5117         (set-window-start
5118          window (min bottom (save-excursion
5119                               (forward-line (- top)) (point)))
5120          t))
5121       ;; Do horizontal recentering while we're at it.
5122       (when (and (get-buffer-window (current-buffer) t)
5123                  (not (eq gnus-auto-center-summary 'vertical)))
5124         (let ((selected (selected-window)))
5125           (select-window (get-buffer-window (current-buffer) t))
5126           (gnus-summary-position-point)
5127           (gnus-horizontal-recenter)
5128           (select-window selected))))))
5129
5130 (defun gnus-summary-jump-to-group (newsgroup)
5131   "Move point to NEWSGROUP in group mode buffer."
5132   ;; Keep update point of group mode buffer if visible.
5133   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5134       (save-window-excursion
5135         ;; Take care of tree window mode.
5136         (when (get-buffer-window gnus-group-buffer)
5137           (pop-to-buffer gnus-group-buffer))
5138         (gnus-group-jump-to-group newsgroup))
5139     (save-excursion
5140       ;; Take care of tree window mode.
5141       (if (get-buffer-window gnus-group-buffer)
5142           (pop-to-buffer gnus-group-buffer)
5143         (set-buffer gnus-group-buffer))
5144       (gnus-group-jump-to-group newsgroup))))
5145
5146 ;; This function returns a list of article numbers based on the
5147 ;; difference between the ranges of read articles in this group and
5148 ;; the range of active articles.
5149 (defun gnus-list-of-unread-articles (group)
5150   (let* ((read (gnus-info-read (gnus-get-info group)))
5151          (active (or (gnus-active group) (gnus-activate-group group)))
5152          (last (cdr active))
5153          first nlast unread)
5154     ;; If none are read, then all are unread.
5155     (if (not read)
5156         (setq first (car active))
5157       ;; If the range of read articles is a single range, then the
5158       ;; first unread article is the article after the last read
5159       ;; article.  Sounds logical, doesn't it?
5160       (if (and (not (listp (cdr read)))
5161                (or (< (car read) (car active))
5162                    (progn (setq read (list read))
5163                           nil)))
5164           (setq first (max (car active) (1+ (cdr read))))
5165         ;; `read' is a list of ranges.
5166         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5167                                   (caar read)))
5168                   1)
5169           (setq first (car active)))
5170         (while read
5171           (when first
5172             (while (< first nlast)
5173               (push first unread)
5174               (setq first (1+ first))))
5175           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5176           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5177           (setq read (cdr read)))))
5178     ;; And add the last unread articles.
5179     (while (<= first last)
5180       (push first unread)
5181       (setq first (1+ first)))
5182     ;; Return the list of unread articles.
5183     (delq 0 (nreverse unread))))
5184
5185 (defun gnus-list-of-read-articles (group)
5186   "Return a list of unread, unticked and non-dormant articles."
5187   (let* ((info (gnus-get-info group))
5188          (marked (gnus-info-marks info))
5189          (active (gnus-active group)))
5190     (and info active
5191          (gnus-set-difference
5192           (gnus-sorted-complement
5193            (gnus-uncompress-range active)
5194            (gnus-list-of-unread-articles group))
5195           (append
5196            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5197            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5198
5199 ;; Various summary commands
5200
5201 (defun gnus-summary-select-article-buffer ()
5202   "Reconfigure windows to show article buffer."
5203   (interactive)
5204   (if (not (gnus-buffer-live-p gnus-article-buffer))
5205       (error "There is no article buffer for this summary buffer")
5206     (gnus-configure-windows 'article)
5207     (select-window (get-buffer-window gnus-article-buffer))))
5208
5209 (defun gnus-summary-universal-argument (arg)
5210   "Perform any operation on all articles that are process/prefixed."
5211   (interactive "P")
5212   (let ((articles (gnus-summary-work-articles arg))
5213         func article)
5214     (if (eq
5215          (setq
5216           func
5217           (key-binding
5218            (read-key-sequence
5219             (substitute-command-keys
5220              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
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     (when gnus-use-cache
5350       (gnus-cache-write-active))
5351     ;; Remove entries for this group.
5352     (nnmail-purge-split-history (gnus-group-real-name group))
5353     ;; Make all changes in this group permanent.
5354     (unless quit-config
5355       (gnus-run-hooks 'gnus-exit-group-hook)
5356       (gnus-summary-update-info))
5357     (gnus-close-group group)
5358     ;; Make sure where we were, and go to next newsgroup.
5359     (set-buffer gnus-group-buffer)
5360     (unless quit-config
5361       (gnus-group-jump-to-group group))
5362     (gnus-run-hooks 'gnus-summary-exit-hook)
5363     (unless (or quit-config
5364                 ;; If this group has disappeared from the summary
5365                 ;; buffer, don't skip forwards.
5366                 (not (string= group (gnus-group-group-name))))
5367       (gnus-group-next-unread-group 1))
5368     (setq group-point (point))
5369     (if temporary
5370         nil                             ;Nothing to do.
5371       ;; If we have several article buffers, we kill them at exit.
5372       (unless gnus-single-article-buffer
5373         (gnus-kill-buffer gnus-article-buffer)
5374         (gnus-kill-buffer gnus-original-article-buffer)
5375         (setq gnus-article-current nil))
5376       (set-buffer buf)
5377       (if (not gnus-kill-summary-on-exit)
5378           (gnus-deaden-summary)
5379         ;; We set all buffer-local variables to nil.  It is unclear why
5380         ;; this is needed, but if we don't, buffer-local variables are
5381         ;; not garbage-collected, it seems.  This would the lead to en
5382         ;; ever-growing Emacs.
5383         (gnus-summary-clear-local-variables)
5384         (when (get-buffer gnus-article-buffer)
5385           (bury-buffer gnus-article-buffer))
5386         ;; We clear the global counterparts of the buffer-local
5387         ;; variables as well, just to be on the safe side.
5388         (set-buffer gnus-group-buffer)
5389         (gnus-summary-clear-local-variables)
5390         ;; Return to group mode buffer.
5391         (when (eq mode 'gnus-summary-mode)
5392           (gnus-kill-buffer buf)))
5393       (setq gnus-current-select-method gnus-select-method)
5394       (pop-to-buffer gnus-group-buffer)
5395       (if (not quit-config)
5396           (progn
5397             (goto-char group-point)
5398             (gnus-configure-windows 'group 'force))
5399         (gnus-handle-ephemeral-exit quit-config))
5400       ;; Clear the current group name.
5401       (unless quit-config
5402         (setq gnus-newsgroup-name nil)))))
5403
5404 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5405 (defun gnus-summary-exit-no-update (&optional no-questions)
5406   "Quit reading current newsgroup without updating read article info."
5407   (interactive)
5408   (let* ((group gnus-newsgroup-name)
5409          (quit-config (gnus-group-quit-config group)))
5410     (when (or no-questions
5411               gnus-expert-user
5412               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5413       (gnus-async-halt-prefetch)
5414       (mapcar 'funcall
5415               (delq 'gnus-summary-expire-articles
5416                     (copy-sequence gnus-summary-prepare-exit-hook)))
5417       (when (gnus-buffer-live-p gnus-article-buffer)
5418         (save-excursion
5419           (set-buffer gnus-article-buffer)
5420           (mm-destroy-parts gnus-article-mime-handles)))
5421       ;; If we have several article buffers, we kill them at exit.
5422       (unless gnus-single-article-buffer
5423         (gnus-kill-buffer gnus-article-buffer)
5424         (gnus-kill-buffer gnus-original-article-buffer)
5425         (setq gnus-article-current nil))
5426       (if (not gnus-kill-summary-on-exit)
5427           (gnus-deaden-summary)
5428         (gnus-close-group group)
5429         (gnus-summary-clear-local-variables)
5430         (set-buffer gnus-group-buffer)
5431         (gnus-summary-clear-local-variables)
5432         (when (get-buffer gnus-summary-buffer)
5433           (kill-buffer gnus-summary-buffer)))
5434       (unless gnus-single-article-buffer
5435         (setq gnus-article-current nil))
5436       (when gnus-use-trees
5437         (gnus-tree-close group))
5438       (gnus-async-prefetch-remove-group group)
5439       (when (get-buffer gnus-article-buffer)
5440         (bury-buffer gnus-article-buffer))
5441       ;; Return to the group buffer.
5442       (gnus-configure-windows 'group 'force)
5443       ;; Clear the current group name.
5444       (setq gnus-newsgroup-name nil)
5445       (when (equal (gnus-group-group-name) group)
5446         (gnus-group-next-unread-group 1))
5447       (when quit-config
5448         (gnus-handle-ephemeral-exit quit-config)))))
5449
5450 (defun gnus-handle-ephemeral-exit (quit-config)
5451   "Handle movement when leaving an ephemeral group.
5452 The state which existed when entering the ephemeral is reset."
5453   (if (not (buffer-name (car quit-config)))
5454       (gnus-configure-windows 'group 'force)
5455     (set-buffer (car quit-config))
5456     (cond ((eq major-mode 'gnus-summary-mode)
5457            (gnus-set-global-variables))
5458           ((eq major-mode 'gnus-article-mode)
5459            (save-excursion
5460              ;; The `gnus-summary-buffer' variable may point
5461              ;; to the old summary buffer when using a single
5462              ;; article buffer.
5463              (unless (gnus-buffer-live-p gnus-summary-buffer)
5464                (set-buffer gnus-group-buffer))
5465              (set-buffer gnus-summary-buffer)
5466              (gnus-set-global-variables))))
5467     (if (or (eq (cdr quit-config) 'article)
5468             (eq (cdr quit-config) 'pick))
5469         (progn
5470           ;; The current article may be from the ephemeral group
5471           ;; thus it is best that we reload this article
5472           (gnus-summary-show-article)
5473           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5474               (gnus-configure-windows 'pick 'force)
5475             (gnus-configure-windows (cdr quit-config) 'force)))
5476       (gnus-configure-windows (cdr quit-config) 'force))
5477     (when (eq major-mode 'gnus-summary-mode)
5478       (gnus-summary-next-subject 1 nil t)
5479       (gnus-summary-recenter)
5480       (gnus-summary-position-point))))
5481
5482 ;;; Dead summaries.
5483
5484 (defvar gnus-dead-summary-mode-map nil)
5485
5486 (unless gnus-dead-summary-mode-map
5487   (setq gnus-dead-summary-mode-map (make-keymap))
5488   (suppress-keymap gnus-dead-summary-mode-map)
5489   (substitute-key-definition
5490    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5491   (let ((keys '("\C-d" "\r" "\177" [delete])))
5492     (while keys
5493       (define-key gnus-dead-summary-mode-map
5494         (pop keys) 'gnus-summary-wake-up-the-dead))))
5495
5496 (defvar gnus-dead-summary-mode nil
5497   "Minor mode for Gnus summary buffers.")
5498
5499 (defun gnus-dead-summary-mode (&optional arg)
5500   "Minor mode for Gnus summary buffers."
5501   (interactive "P")
5502   (when (eq major-mode 'gnus-summary-mode)
5503     (make-local-variable 'gnus-dead-summary-mode)
5504     (setq gnus-dead-summary-mode
5505           (if (null arg) (not gnus-dead-summary-mode)
5506             (> (prefix-numeric-value arg) 0)))
5507     (when gnus-dead-summary-mode
5508       (gnus-add-minor-mode
5509        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5510
5511 (defun gnus-deaden-summary ()
5512   "Make the current summary buffer into a dead summary buffer."
5513   ;; Kill any previous dead summary buffer.
5514   (when (and gnus-dead-summary
5515              (buffer-name gnus-dead-summary))
5516     (save-excursion
5517       (set-buffer gnus-dead-summary)
5518       (when gnus-dead-summary-mode
5519         (kill-buffer (current-buffer)))))
5520   ;; Make this the current dead summary.
5521   (setq gnus-dead-summary (current-buffer))
5522   (gnus-dead-summary-mode 1)
5523   (let ((name (buffer-name)))
5524     (when (string-match "Summary" name)
5525       (rename-buffer
5526        (concat (substring name 0 (match-beginning 0)) "Dead "
5527                (substring name (match-beginning 0)))
5528        t))))
5529
5530 (defun gnus-kill-or-deaden-summary (buffer)
5531   "Kill or deaden the summary BUFFER."
5532   (save-excursion
5533     (when (and (buffer-name buffer)
5534                (not gnus-single-article-buffer))
5535       (save-excursion
5536         (set-buffer buffer)
5537         (gnus-kill-buffer gnus-article-buffer)
5538         (gnus-kill-buffer gnus-original-article-buffer)))
5539     (cond (gnus-kill-summary-on-exit
5540            (when (and gnus-use-trees
5541                       (gnus-buffer-exists-p buffer))
5542              (save-excursion
5543                (set-buffer buffer)
5544                (gnus-tree-close gnus-newsgroup-name)))
5545            (gnus-kill-buffer buffer))
5546           ((gnus-buffer-exists-p buffer)
5547            (save-excursion
5548              (set-buffer buffer)
5549              (gnus-deaden-summary))))))
5550
5551 (defun gnus-summary-wake-up-the-dead (&rest args)
5552   "Wake up the dead summary buffer."
5553   (interactive)
5554   (gnus-dead-summary-mode -1)
5555   (let ((name (buffer-name)))
5556     (when (string-match "Dead " name)
5557       (rename-buffer
5558        (concat (substring name 0 (match-beginning 0))
5559                (substring name (match-end 0)))
5560        t)))
5561   (gnus-message 3 "This dead summary is now alive again"))
5562
5563 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5564 (defun gnus-summary-fetch-faq (&optional faq-dir)
5565   "Fetch the FAQ for the current group.
5566 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5567 in."
5568   (interactive
5569    (list
5570     (when current-prefix-arg
5571       (completing-read
5572        "Faq dir: " (and (listp gnus-group-faq-directory)
5573                         (mapcar (lambda (file) (list file))
5574                                 gnus-group-faq-directory))))))
5575   (let (gnus-faq-buffer)
5576     (when (setq gnus-faq-buffer
5577                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5578       (gnus-configure-windows 'summary-faq))))
5579
5580 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5581 (defun gnus-summary-describe-group (&optional force)
5582   "Describe the current newsgroup."
5583   (interactive "P")
5584   (gnus-group-describe-group force gnus-newsgroup-name))
5585
5586 (defun gnus-summary-describe-briefly ()
5587   "Describe summary mode commands briefly."
5588   (interactive)
5589   (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")))
5590
5591 ;; Walking around group mode buffer from summary mode.
5592
5593 (defun gnus-summary-next-group (&optional no-article target-group backward)
5594   "Exit current newsgroup and then select next unread newsgroup.
5595 If prefix argument NO-ARTICLE is non-nil, no article is selected
5596 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5597 previous group instead."
5598   (interactive "P")
5599   ;; Stop pre-fetching.
5600   (gnus-async-halt-prefetch)
5601   (let ((current-group gnus-newsgroup-name)
5602         (current-buffer (current-buffer))
5603         entered)
5604     ;; First we semi-exit this group to update Xrefs and all variables.
5605     ;; We can't do a real exit, because the window conf must remain
5606     ;; the same in case the user is prompted for info, and we don't
5607     ;; want the window conf to change before that...
5608     (gnus-summary-exit t)
5609     (while (not entered)
5610       ;; Then we find what group we are supposed to enter.
5611       (set-buffer gnus-group-buffer)
5612       (gnus-group-jump-to-group current-group)
5613       (setq target-group
5614             (or target-group
5615                 (if (eq gnus-keep-same-level 'best)
5616                     (gnus-summary-best-group gnus-newsgroup-name)
5617                   (gnus-summary-search-group backward gnus-keep-same-level))))
5618       (if (not target-group)
5619           ;; There are no further groups, so we return to the group
5620           ;; buffer.
5621           (progn
5622             (gnus-message 5 "Returning to the group buffer")
5623             (setq entered t)
5624             (when (gnus-buffer-live-p current-buffer)
5625               (set-buffer current-buffer)
5626               (gnus-summary-exit))
5627             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5628         ;; We try to enter the target group.
5629         (gnus-group-jump-to-group target-group)
5630         (let ((unreads (gnus-group-group-unread)))
5631           (if (and (or (eq t unreads)
5632                        (and unreads (not (zerop unreads))))
5633                    (gnus-summary-read-group
5634                     target-group nil no-article
5635                     (and (buffer-name current-buffer) current-buffer)
5636                     nil backward))
5637               (setq entered t)
5638             (setq current-group target-group
5639                   target-group nil)))))))
5640
5641 (defun gnus-summary-prev-group (&optional no-article)
5642   "Exit current newsgroup and then select previous unread newsgroup.
5643 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5644   (interactive "P")
5645   (gnus-summary-next-group no-article nil t))
5646
5647 ;; Walking around summary lines.
5648
5649 (defun gnus-summary-first-subject (&optional unread undownloaded)
5650   "Go to the first unread subject.
5651 If UNREAD is non-nil, go to the first unread article.
5652 Returns the article selected or nil if there are no unread articles."
5653   (interactive "P")
5654   (prog1
5655       (cond
5656        ;; Empty summary.
5657        ((null gnus-newsgroup-data)
5658         (gnus-message 3 "No articles in the group")
5659         nil)
5660        ;; Pick the first article.
5661        ((not unread)
5662         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5663         (gnus-data-number (car gnus-newsgroup-data)))
5664        ;; No unread articles.
5665        ((null gnus-newsgroup-unreads)
5666         (gnus-message 3 "No more unread articles")
5667         nil)
5668        ;; Find the first unread article.
5669        (t
5670         (let ((data gnus-newsgroup-data))
5671           (while (and data
5672                       (and (not (and undownloaded
5673                                      (eq gnus-undownloaded-mark
5674                                          (gnus-data-mark (car data)))))
5675                            (not (gnus-data-unread-p (car data)))))
5676             (setq data (cdr data)))
5677           (when data
5678             (goto-char (gnus-data-pos (car data)))
5679             (gnus-data-number (car data))))))
5680     (gnus-summary-position-point)))
5681
5682 (defun gnus-summary-next-subject (n &optional unread dont-display)
5683   "Go to next N'th summary line.
5684 If N is negative, go to the previous N'th subject line.
5685 If UNREAD is non-nil, only unread articles are selected.
5686 The difference between N and the actual number of steps taken is
5687 returned."
5688   (interactive "p")
5689   (let ((backward (< n 0))
5690         (n (abs n)))
5691     (while (and (> n 0)
5692                 (if backward
5693                     (gnus-summary-find-prev unread)
5694                   (gnus-summary-find-next unread)))
5695       (unless (zerop (setq n (1- n)))
5696         (gnus-summary-show-thread)))
5697     (when (/= 0 n)
5698       (gnus-message 7 "No more%s articles"
5699                     (if unread " unread" "")))
5700     (unless dont-display
5701       (gnus-summary-recenter)
5702       (gnus-summary-position-point))
5703     n))
5704
5705 (defun gnus-summary-next-unread-subject (n)
5706   "Go to next N'th unread summary line."
5707   (interactive "p")
5708   (gnus-summary-next-subject n t))
5709
5710 (defun gnus-summary-prev-subject (n &optional unread)
5711   "Go to previous N'th summary line.
5712 If optional argument UNREAD is non-nil, only unread article is selected."
5713   (interactive "p")
5714   (gnus-summary-next-subject (- n) unread))
5715
5716 (defun gnus-summary-prev-unread-subject (n)
5717   "Go to previous N'th unread summary line."
5718   (interactive "p")
5719   (gnus-summary-next-subject (- n) t))
5720
5721 (defun gnus-summary-goto-subject (article &optional force silent)
5722   "Go the subject line of ARTICLE.
5723 If FORCE, also allow jumping to articles not currently shown."
5724   (interactive "nArticle number: ")
5725   (let ((b (point))
5726         (data (gnus-data-find article)))
5727     ;; We read in the article if we have to.
5728     (and (not data)
5729          force
5730          (gnus-summary-insert-subject
5731           article
5732           (if (or (numberp force) (vectorp force)) force)
5733           t)
5734          (setq data (gnus-data-find article)))
5735     (goto-char b)
5736     (if (not data)
5737         (progn
5738           (unless silent
5739             (gnus-message 3 "Can't find article %d" article))
5740           nil)
5741       (goto-char (gnus-data-pos data))
5742       (gnus-summary-position-point)
5743       article)))
5744
5745 ;; Walking around summary lines with displaying articles.
5746
5747 (defun gnus-summary-expand-window (&optional arg)
5748   "Make the summary buffer take up the entire Emacs frame.
5749 Given a prefix, will force an `article' buffer configuration."
5750   (interactive "P")
5751   (if arg
5752       (gnus-configure-windows 'article 'force)
5753     (gnus-configure-windows 'summary 'force)))
5754
5755 (defun gnus-summary-display-article (article &optional all-header)
5756   "Display ARTICLE in article buffer."
5757   (gnus-set-global-variables)
5758   (if (null article)
5759       nil
5760     (prog1
5761         (if gnus-summary-display-article-function
5762             (funcall gnus-summary-display-article-function article all-header)
5763           (gnus-article-prepare article all-header))
5764       (gnus-run-hooks 'gnus-select-article-hook)
5765       (when (and gnus-current-article
5766                  (not (zerop gnus-current-article)))
5767         (gnus-summary-goto-subject gnus-current-article))
5768       (gnus-summary-recenter)
5769       (when (and gnus-use-trees gnus-show-threads)
5770         (gnus-possibly-generate-tree article)
5771         (gnus-highlight-selected-tree article))
5772       ;; Successfully display article.
5773       (gnus-article-set-window-start
5774        (cdr (assq article gnus-newsgroup-bookmarks))))))
5775
5776 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5777   "Select the current article.
5778 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5779 non-nil, the article will be re-fetched even if it already present in
5780 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5781 be displayed."
5782   ;; Make sure we are in the summary buffer to work around bbdb bug.
5783   (unless (eq major-mode 'gnus-summary-mode)
5784     (set-buffer gnus-summary-buffer))
5785   (let ((article (or article (gnus-summary-article-number)))
5786         (all-headers (not (not all-headers))) ;Must be T or NIL.
5787         gnus-summary-display-article-function
5788         did)
5789     (and (not pseudo)
5790          (gnus-summary-article-pseudo-p article)
5791          (error "This is a pseudo-article"))
5792     (prog1
5793         (save-excursion
5794           (set-buffer gnus-summary-buffer)
5795           (if (or (and gnus-single-article-buffer
5796                        (or (null gnus-current-article)
5797                            (null gnus-article-current)
5798                            (null (get-buffer gnus-article-buffer))
5799                            (not (eq article (cdr gnus-article-current)))
5800                            (not (equal (car gnus-article-current)
5801                                        gnus-newsgroup-name))))
5802                   (and (not gnus-single-article-buffer)
5803                        (or (null gnus-current-article)
5804                            (not (eq gnus-current-article article))))
5805                   force)
5806               ;; The requested article is different from the current article.
5807               (prog1
5808                   (gnus-summary-display-article article all-headers)
5809                 (setq did article)
5810                 (when (or all-headers gnus-show-all-headers)
5811                   (gnus-article-show-all-headers)))
5812             (when (or all-headers gnus-show-all-headers)
5813               (gnus-article-show-all-headers))
5814             'old))
5815       (when did
5816         (gnus-article-set-window-start
5817          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5818
5819 (defun gnus-summary-set-current-mark (&optional current-mark)
5820   "Obsolete function."
5821   nil)
5822
5823 (defun gnus-summary-next-article (&optional unread subject backward push)
5824   "Select the next article.
5825 If UNREAD, only unread articles are selected.
5826 If SUBJECT, only articles with SUBJECT are selected.
5827 If BACKWARD, the previous article is selected instead of the next."
5828   (interactive "P")
5829   (cond
5830    ;; Is there such an article?
5831    ((and (gnus-summary-search-forward unread subject backward)
5832          (or (gnus-summary-display-article (gnus-summary-article-number))
5833              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5834     (gnus-summary-position-point))
5835    ;; If not, we try the first unread, if that is wanted.
5836    ((and subject
5837          gnus-auto-select-same
5838          (gnus-summary-first-unread-article))
5839     (gnus-summary-position-point)
5840     (gnus-message 6 "Wrapped"))
5841    ;; Try to get next/previous article not displayed in this group.
5842    ((and gnus-auto-extend-newsgroup
5843          (not unread) (not subject))
5844     (gnus-summary-goto-article
5845      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5846      nil (count-lines (point-min) (point))))
5847    ;; Go to next/previous group.
5848    (t
5849     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5850       (gnus-summary-jump-to-group gnus-newsgroup-name))
5851     (let ((cmd last-command-char)
5852           (point
5853            (save-excursion
5854              (set-buffer gnus-group-buffer)
5855              (point)))
5856           (group
5857            (if (eq gnus-keep-same-level 'best)
5858                (gnus-summary-best-group gnus-newsgroup-name)
5859              (gnus-summary-search-group backward gnus-keep-same-level))))
5860       ;; For some reason, the group window gets selected.  We change
5861       ;; it back.
5862       (select-window (get-buffer-window (current-buffer)))
5863       ;; Select next unread newsgroup automagically.
5864       (cond
5865        ((or (not gnus-auto-select-next)
5866             (not cmd))
5867         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5868        ((or (eq gnus-auto-select-next 'quietly)
5869             (and (eq gnus-auto-select-next 'slightly-quietly)
5870                  push)
5871             (and (eq gnus-auto-select-next 'almost-quietly)
5872                  (gnus-summary-last-article-p)))
5873         ;; Select quietly.
5874         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5875             (gnus-summary-exit)
5876           (gnus-message 7 "No more%s articles (%s)..."
5877                         (if unread " unread" "")
5878                         (if group (concat "selecting " group)
5879                           "exiting"))
5880           (gnus-summary-next-group nil group backward)))
5881        (t
5882         (when (gnus-key-press-event-p last-input-event)
5883           (gnus-summary-walk-group-buffer
5884            gnus-newsgroup-name cmd unread backward point))))))))
5885
5886 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5887   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5888                       (?\C-p (gnus-group-prev-unread-group 1))))
5889         (cursor-in-echo-area t)
5890         keve key group ended)
5891     (save-excursion
5892       (set-buffer gnus-group-buffer)
5893       (goto-char start)
5894       (setq group
5895             (if (eq gnus-keep-same-level 'best)
5896                 (gnus-summary-best-group gnus-newsgroup-name)
5897               (gnus-summary-search-group backward gnus-keep-same-level))))
5898     (while (not ended)
5899       (gnus-message
5900        5 "No more%s articles%s" (if unread " unread" "")
5901        (if (and group
5902                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5903            (format " (Type %s for %s [%s])"
5904                    (single-key-description cmd) group
5905                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5906          (format " (Type %s to exit %s)"
5907                  (single-key-description cmd)
5908                  gnus-newsgroup-name)))
5909       ;; Confirm auto selection.
5910       (setq key (car (setq keve (gnus-read-event-char))))
5911       (setq ended t)
5912       (cond
5913        ((assq key keystrokes)
5914         (let ((obuf (current-buffer)))
5915           (switch-to-buffer gnus-group-buffer)
5916           (when group
5917             (gnus-group-jump-to-group group))
5918           (eval (cadr (assq key keystrokes)))
5919           (setq group (gnus-group-group-name))
5920           (switch-to-buffer obuf))
5921         (setq ended nil))
5922        ((equal key cmd)
5923         (if (or (not group)
5924                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5925             (gnus-summary-exit)
5926           (gnus-summary-next-group nil group backward)))
5927        (t
5928         (push (cdr keve) unread-command-events))))))
5929
5930 (defun gnus-summary-next-unread-article ()
5931   "Select unread article after current one."
5932   (interactive)
5933   (gnus-summary-next-article
5934    (or (not (eq gnus-summary-goto-unread 'never))
5935        (gnus-summary-last-article-p (gnus-summary-article-number)))
5936    (and gnus-auto-select-same
5937         (gnus-summary-article-subject))))
5938
5939 (defun gnus-summary-prev-article (&optional unread subject)
5940   "Select the article after the current one.
5941 If UNREAD is non-nil, only unread articles are selected."
5942   (interactive "P")
5943   (gnus-summary-next-article unread subject t))
5944
5945 (defun gnus-summary-prev-unread-article ()
5946   "Select unread article before current one."
5947   (interactive)
5948   (gnus-summary-prev-article
5949    (or (not (eq gnus-summary-goto-unread 'never))
5950        (gnus-summary-first-article-p (gnus-summary-article-number)))
5951    (and gnus-auto-select-same
5952         (gnus-summary-article-subject))))
5953
5954 (defun gnus-summary-next-page (&optional lines circular)
5955   "Show next page of the selected article.
5956 If at the end of the current article, select the next article.
5957 LINES says how many lines should be scrolled up.
5958
5959 If CIRCULAR is non-nil, go to the start of the article instead of
5960 selecting the next article when reaching the end of the current
5961 article."
5962   (interactive "P")
5963   (setq gnus-summary-buffer (current-buffer))
5964   (gnus-set-global-variables)
5965   (let ((article (gnus-summary-article-number))
5966         (article-window (get-buffer-window gnus-article-buffer t))
5967         endp)
5968     ;; If the buffer is empty, we have no article.
5969     (unless article
5970       (error "No article to select"))
5971     (gnus-configure-windows 'article)
5972     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5973         (if (and (eq gnus-summary-goto-unread 'never)
5974                  (not (gnus-summary-last-article-p article)))
5975             (gnus-summary-next-article)
5976           (gnus-summary-next-unread-article))
5977       (if (or (null gnus-current-article)
5978               (null gnus-article-current)
5979               (/= article (cdr gnus-article-current))
5980               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5981           ;; Selected subject is different from current article's.
5982           (gnus-summary-display-article article)
5983         (when article-window
5984           (gnus-eval-in-buffer-window gnus-article-buffer
5985             (setq endp (gnus-article-next-page lines)))
5986           (when endp
5987             (cond (circular
5988                    (gnus-summary-beginning-of-article))
5989                   (lines
5990                    (gnus-message 3 "End of message"))
5991                   ((null lines)
5992                    (if (and (eq gnus-summary-goto-unread 'never)
5993                             (not (gnus-summary-last-article-p article)))
5994                        (gnus-summary-next-article)
5995                      (gnus-summary-next-unread-article))))))))
5996     (gnus-summary-recenter)
5997     (gnus-summary-position-point)))
5998
5999 (defun gnus-summary-prev-page (&optional lines move)
6000   "Show previous page of selected article.
6001 Argument LINES specifies lines to be scrolled down.
6002 If MOVE, move to the previous unread article if point is at
6003 the beginning of the buffer."
6004   (interactive "P")
6005   (let ((article (gnus-summary-article-number))
6006         (article-window (get-buffer-window gnus-article-buffer t))
6007         endp)
6008     (gnus-configure-windows 'article)
6009     (if (or (null gnus-current-article)
6010             (null gnus-article-current)
6011             (/= article (cdr gnus-article-current))
6012             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6013         ;; Selected subject is different from current article's.
6014         (gnus-summary-display-article article)
6015       (gnus-summary-recenter)
6016       (when article-window
6017         (gnus-eval-in-buffer-window gnus-article-buffer
6018           (setq endp (gnus-article-prev-page lines)))
6019         (when (and move endp)
6020           (cond (lines
6021                  (gnus-message 3 "Beginning of message"))
6022                 ((null lines)
6023                  (if (and (eq gnus-summary-goto-unread 'never)
6024                           (not (gnus-summary-first-article-p article)))
6025                      (gnus-summary-prev-article)
6026                    (gnus-summary-prev-unread-article))))))))
6027   (gnus-summary-position-point))
6028
6029 (defun gnus-summary-prev-page-or-article (&optional lines)
6030   "Show previous page of selected article.
6031 Argument LINES specifies lines to be scrolled down.
6032 If at the beginning of the article, go to the next article."
6033   (interactive "P")
6034   (gnus-summary-prev-page lines t))
6035
6036 (defun gnus-summary-scroll-up (lines)
6037   "Scroll up (or down) one line current article.
6038 Argument LINES specifies lines to be scrolled up (or down if negative)."
6039   (interactive "p")
6040   (gnus-configure-windows 'article)
6041   (gnus-summary-show-thread)
6042   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6043     (gnus-eval-in-buffer-window gnus-article-buffer
6044       (cond ((> lines 0)
6045              (when (gnus-article-next-page lines)
6046                (gnus-message 3 "End of message")))
6047             ((< lines 0)
6048              (gnus-article-prev-page (- lines))))))
6049   (gnus-summary-recenter)
6050   (gnus-summary-position-point))
6051
6052 (defun gnus-summary-scroll-down (lines)
6053   "Scroll down (or up) one line current article.
6054 Argument LINES specifies lines to be scrolled down (or up if negative)."
6055   (interactive "p")
6056   (gnus-summary-scroll-up (- lines)))
6057
6058 (defun gnus-summary-next-same-subject ()
6059   "Select next article which has the same subject as current one."
6060   (interactive)
6061   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6062
6063 (defun gnus-summary-prev-same-subject ()
6064   "Select previous article which has the same subject as current one."
6065   (interactive)
6066   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6067
6068 (defun gnus-summary-next-unread-same-subject ()
6069   "Select next unread article which has the same subject as current one."
6070   (interactive)
6071   (gnus-summary-next-article t (gnus-summary-article-subject)))
6072
6073 (defun gnus-summary-prev-unread-same-subject ()
6074   "Select previous unread article which has the same subject as current one."
6075   (interactive)
6076   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6077
6078 (defun gnus-summary-first-unread-article ()
6079   "Select the first unread article.
6080 Return nil if there are no unread articles."
6081   (interactive)
6082   (prog1
6083       (when (gnus-summary-first-subject t)
6084         (gnus-summary-show-thread)
6085         (gnus-summary-first-subject t)
6086         (gnus-summary-display-article (gnus-summary-article-number)))
6087     (gnus-summary-position-point)))
6088
6089 (defun gnus-summary-first-unread-subject ()
6090   "Place the point on the subject line of the first unread article.
6091 Return nil if there are no unread articles."
6092   (interactive)
6093   (prog1
6094       (when (gnus-summary-first-subject t)
6095         (gnus-summary-show-thread)
6096         (gnus-summary-first-subject t))
6097     (gnus-summary-position-point)))
6098
6099 (defun gnus-summary-first-article ()
6100   "Select the first article.
6101 Return nil if there are no articles."
6102   (interactive)
6103   (prog1
6104       (when (gnus-summary-first-subject)
6105         (gnus-summary-show-thread)
6106         (gnus-summary-first-subject)
6107         (gnus-summary-display-article (gnus-summary-article-number)))
6108     (gnus-summary-position-point)))
6109
6110 (defun gnus-summary-best-unread-article ()
6111   "Select the unread article with the highest score."
6112   (interactive)
6113   (let ((best -1000000)
6114         (data gnus-newsgroup-data)
6115         article score)
6116     (while data
6117       (and (gnus-data-unread-p (car data))
6118            (> (setq score
6119                     (gnus-summary-article-score (gnus-data-number (car data))))
6120               best)
6121            (setq best score
6122                  article (gnus-data-number (car data))))
6123       (setq data (cdr data)))
6124     (prog1
6125         (if article
6126             (gnus-summary-goto-article article)
6127           (error "No unread articles"))
6128       (gnus-summary-position-point))))
6129
6130 (defun gnus-summary-last-subject ()
6131   "Go to the last displayed subject line in the group."
6132   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6133     (when article
6134       (gnus-summary-goto-subject article))))
6135
6136 (defun gnus-summary-goto-article (article &optional all-headers force)
6137   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6138 If ALL-HEADERS is non-nil, no header lines are hidden.
6139 If FORCE, go to the article even if it isn't displayed.  If FORCE
6140 is a number, it is the line the article is to be displayed on."
6141   (interactive
6142    (list
6143     (completing-read
6144      "Article number or Message-ID: "
6145      (mapcar (lambda (number) (list (int-to-string number)))
6146              gnus-newsgroup-limit))
6147     current-prefix-arg
6148     t))
6149   (prog1
6150       (if (and (stringp article)
6151                (string-match "@" article))
6152           (gnus-summary-refer-article article)
6153         (when (stringp article)
6154           (setq article (string-to-number article)))
6155         (if (gnus-summary-goto-subject article force)
6156             (gnus-summary-display-article article all-headers)
6157           (gnus-message 4 "Couldn't go to article %s" article) nil))
6158     (gnus-summary-position-point)))
6159
6160 (defun gnus-summary-goto-last-article ()
6161   "Go to the previously read article."
6162   (interactive)
6163   (prog1
6164       (when gnus-last-article
6165         (gnus-summary-goto-article gnus-last-article nil t))
6166     (gnus-summary-position-point)))
6167
6168 (defun gnus-summary-pop-article (number)
6169   "Pop one article off the history and go to the previous.
6170 NUMBER articles will be popped off."
6171   (interactive "p")
6172   (let (to)
6173     (setq gnus-newsgroup-history
6174           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6175     (if to
6176         (gnus-summary-goto-article (car to) nil t)
6177       (error "Article history empty")))
6178   (gnus-summary-position-point))
6179
6180 ;; Summary commands and functions for limiting the summary buffer.
6181
6182 (defun gnus-summary-limit-to-articles (n)
6183   "Limit the summary buffer to the next N articles.
6184 If not given a prefix, use the process marked articles instead."
6185   (interactive "P")
6186   (prog1
6187       (let ((articles (gnus-summary-work-articles n)))
6188         (setq gnus-newsgroup-processable nil)
6189         (gnus-summary-limit articles))
6190     (gnus-summary-position-point)))
6191
6192 (defun gnus-summary-pop-limit (&optional total)
6193   "Restore the previous limit.
6194 If given a prefix, remove all limits."
6195   (interactive "P")
6196   (when total
6197     (setq gnus-newsgroup-limits
6198           (list (mapcar (lambda (h) (mail-header-number h))
6199                         gnus-newsgroup-headers))))
6200   (unless gnus-newsgroup-limits
6201     (error "No limit to pop"))
6202   (prog1
6203       (gnus-summary-limit nil 'pop)
6204     (gnus-summary-position-point)))
6205
6206 (defun gnus-summary-limit-to-subject (subject &optional header)
6207   "Limit the summary buffer to articles that have subjects that match a regexp."
6208   (interactive "sLimit to subject (regexp): ")
6209   (unless header
6210     (setq header "subject"))
6211   (when (not (equal "" subject))
6212     (prog1
6213         (let ((articles (gnus-summary-find-matching
6214                          (or header "subject") subject 'all)))
6215           (unless articles
6216             (error "Found no matches for \"%s\"" subject))
6217           (gnus-summary-limit articles))
6218       (gnus-summary-position-point))))
6219
6220 (defun gnus-summary-limit-to-author (from)
6221   "Limit the summary buffer to articles that have authors that match a regexp."
6222   (interactive "sLimit to author (regexp): ")
6223   (gnus-summary-limit-to-subject from "from"))
6224
6225 (defun gnus-summary-limit-to-age (age &optional younger-p)
6226   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6227 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6228 articles that are younger than AGE days."
6229   (interactive
6230    (let ((younger current-prefix-arg)
6231          (days-got nil)
6232          days)
6233      (while (not days-got)
6234        (setq days (if younger
6235                       (read-string "Limit to articles within (in days): ")
6236                     (read-string "Limit to articles old than (in days): ")))
6237        (when (> (length days) 0)
6238          (setq days (read days)))
6239        (if (numberp days)
6240            (setq days-got t)
6241          (message "Please enter a number.")
6242          (sleep-for 1)))
6243      (list days younger)))
6244   (prog1
6245       (let ((data gnus-newsgroup-data)
6246             (cutoff (days-to-time age))
6247             articles d date is-younger)
6248         (while (setq d (pop data))
6249           (when (and (vectorp (gnus-data-header d))
6250                      (setq date (mail-header-date (gnus-data-header d))))
6251             (setq is-younger (time-less-p
6252                               (time-since (condition-case ()
6253                                               (date-to-time date)
6254                                             (error '(0 0))))
6255                               cutoff))
6256             (when (if younger-p
6257                       is-younger
6258                     (not is-younger))
6259               (push (gnus-data-number d) articles))))
6260         (gnus-summary-limit (nreverse articles)))
6261     (gnus-summary-position-point)))
6262
6263 (defun gnus-summary-limit-to-extra (header regexp)
6264   "Limit the summary buffer to articles that match an 'extra' header."
6265   (interactive
6266    (let ((header
6267           (intern
6268            (gnus-completing-read
6269             (symbol-name (car gnus-extra-headers))      
6270             "Limit extra header:"       
6271             (mapcar (lambda (x) 
6272                       (cons (symbol-name x) x))
6273                     gnus-extra-headers)
6274             nil                 
6275             t))))
6276      (list header
6277            (read-string (format "Limit to header %s (regexp): " header)))))
6278   (when (not (equal "" regexp))
6279     (prog1
6280         (let ((articles (gnus-summary-find-matching
6281                          (cons 'extra header) regexp 'all)))
6282           (unless articles
6283             (error "Found no matches for \"%s\"" regexp))
6284           (gnus-summary-limit articles))
6285       (gnus-summary-position-point))))
6286
6287 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6288 (make-obsolete
6289  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6290
6291 (defun gnus-summary-limit-to-unread (&optional all)
6292   "Limit the summary buffer to articles that are not marked as read.
6293 If ALL is non-nil, limit strictly to unread articles."
6294   (interactive "P")
6295   (if all
6296       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6297     (gnus-summary-limit-to-marks
6298      ;; Concat all the marks that say that an article is read and have
6299      ;; those removed.
6300      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6301            gnus-killed-mark gnus-kill-file-mark
6302            gnus-low-score-mark gnus-expirable-mark
6303            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6304            gnus-duplicate-mark gnus-souped-mark)
6305      'reverse)))
6306
6307 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6308 (make-obsolete 'gnus-summary-delete-marked-with
6309                'gnus-summary-limit-exlude-marks)
6310
6311 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6312   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6313 If REVERSE, limit the summary buffer to articles that are marked
6314 with MARKS.  MARKS can either be a string of marks or a list of marks.
6315 Returns how many articles were removed."
6316   (interactive "sMarks: ")
6317   (gnus-summary-limit-to-marks marks t))
6318
6319 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6320   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6321 If REVERSE (the prefix), limit the summary buffer to articles that are
6322 not marked with MARKS.  MARKS can either be a string of marks or a
6323 list of marks.
6324 Returns how many articles were removed."
6325   (interactive "sMarks: \nP")
6326   (prog1
6327       (let ((data gnus-newsgroup-data)
6328             (marks (if (listp marks) marks
6329                      (append marks nil))) ; Transform to list.
6330             articles)
6331         (while data
6332           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6333                   (memq (gnus-data-mark (car data)) marks))
6334             (push (gnus-data-number (car data)) articles))
6335           (setq data (cdr data)))
6336         (gnus-summary-limit articles))
6337     (gnus-summary-position-point)))
6338
6339 (defun gnus-summary-limit-to-score (&optional score)
6340   "Limit to articles with score at or above SCORE."
6341   (interactive "P")
6342   (setq score (if score
6343                   (prefix-numeric-value score)
6344                 (or gnus-summary-default-score 0)))
6345   (let ((data gnus-newsgroup-data)
6346         articles)
6347     (while data
6348       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6349                 score)
6350         (push (gnus-data-number (car data)) articles))
6351       (setq data (cdr data)))
6352     (prog1
6353         (gnus-summary-limit articles)
6354       (gnus-summary-position-point))))
6355
6356 (defun gnus-summary-limit-include-thread (id)
6357   "Display all the hidden articles that in the current thread."
6358   (interactive (list (mail-header-id (gnus-summary-article-header))))
6359   (let ((articles (gnus-articles-in-thread
6360                    (gnus-id-to-thread (gnus-root-id id)))))
6361     (prog1
6362         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6363       (gnus-summary-position-point))))
6364
6365 (defun gnus-summary-limit-include-dormant ()
6366   "Display all the hidden articles that are marked as dormant.
6367 Note that this command only works on a subset of the articles currently
6368 fetched for this group."
6369   (interactive)
6370   (unless gnus-newsgroup-dormant
6371     (error "There are no dormant articles in this group"))
6372   (prog1
6373       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6374     (gnus-summary-position-point)))
6375
6376 (defun gnus-summary-limit-exclude-dormant ()
6377   "Hide all dormant articles."
6378   (interactive)
6379   (prog1
6380       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6381     (gnus-summary-position-point)))
6382
6383 (defun gnus-summary-limit-exclude-childless-dormant ()
6384   "Hide all dormant articles that have no children."
6385   (interactive)
6386   (let ((data (gnus-data-list t))
6387         articles d children)
6388     ;; Find all articles that are either not dormant or have
6389     ;; children.
6390     (while (setq d (pop data))
6391       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6392                 (and (setq children
6393                            (gnus-article-children (gnus-data-number d)))
6394                      (let (found)
6395                        (while children
6396                          (when (memq (car children) articles)
6397                            (setq children nil
6398                                  found t))
6399                          (pop children))
6400                        found)))
6401         (push (gnus-data-number d) articles)))
6402     ;; Do the limiting.
6403     (prog1
6404         (gnus-summary-limit articles)
6405       (gnus-summary-position-point))))
6406
6407 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6408   "Mark all unread excluded articles as read.
6409 If ALL, mark even excluded ticked and dormants as read."
6410   (interactive "P")
6411   (let ((articles (gnus-sorted-complement
6412                    (sort
6413                     (mapcar (lambda (h) (mail-header-number h))
6414                             gnus-newsgroup-headers)
6415                     '<)
6416                    (sort gnus-newsgroup-limit '<)))
6417         article)
6418     (setq gnus-newsgroup-unreads
6419           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6420     (if all
6421         (setq gnus-newsgroup-dormant nil
6422               gnus-newsgroup-marked nil
6423               gnus-newsgroup-reads
6424               (nconc
6425                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6426                gnus-newsgroup-reads))
6427       (while (setq article (pop articles))
6428         (unless (or (memq article gnus-newsgroup-dormant)
6429                     (memq article gnus-newsgroup-marked))
6430           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6431
6432 (defun gnus-summary-limit (articles &optional pop)
6433   (if pop
6434       ;; We pop the previous limit off the stack and use that.
6435       (setq articles (car gnus-newsgroup-limits)
6436             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6437     ;; We use the new limit, so we push the old limit on the stack.
6438     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6439   ;; Set the limit.
6440   (setq gnus-newsgroup-limit articles)
6441   (let ((total (length gnus-newsgroup-data))
6442         (data (gnus-data-find-list (gnus-summary-article-number)))
6443         (gnus-summary-mark-below nil)   ; Inhibit this.
6444         found)
6445     ;; This will do all the work of generating the new summary buffer
6446     ;; according to the new limit.
6447     (gnus-summary-prepare)
6448     ;; Hide any threads, possibly.
6449     (and gnus-show-threads
6450          gnus-thread-hide-subtree
6451          (gnus-summary-hide-all-threads))
6452     ;; Try to return to the article you were at, or one in the
6453     ;; neighborhood.
6454     (when data
6455       ;; We try to find some article after the current one.
6456       (while data
6457         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6458           (setq data nil
6459                 found t))
6460         (setq data (cdr data))))
6461     (unless found
6462       ;; If there is no data, that means that we were after the last
6463       ;; article.  The same goes when we can't find any articles
6464       ;; after the current one.
6465       (goto-char (point-max))
6466       (gnus-summary-find-prev))
6467     (gnus-set-mode-line 'summary)
6468     ;; We return how many articles were removed from the summary
6469     ;; buffer as a result of the new limit.
6470     (- total (length gnus-newsgroup-data))))
6471
6472 (defsubst gnus-invisible-cut-children (threads)
6473   (let ((num 0))
6474     (while threads
6475       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6476         (incf num))
6477       (pop threads))
6478     (< num 2)))
6479
6480 (defsubst gnus-cut-thread (thread)
6481   "Go forwards in the thread until we find an article that we want to display."
6482   (when (or (eq gnus-fetch-old-headers 'some)
6483             (eq gnus-fetch-old-headers 'invisible)
6484             (numberp gnus-fetch-old-headers)
6485             (eq gnus-build-sparse-threads 'some)
6486             (eq gnus-build-sparse-threads 'more))
6487     ;; Deal with old-fetched headers and sparse threads.
6488     (while (and
6489             thread
6490             (or
6491              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6492              (gnus-summary-article-ancient-p
6493               (mail-header-number (car thread))))
6494             (if (or (<= (length (cdr thread)) 1)
6495                     (eq gnus-fetch-old-headers 'invisible))
6496                 (setq gnus-newsgroup-limit
6497                       (delq (mail-header-number (car thread))
6498                             gnus-newsgroup-limit)
6499                       thread (cadr thread))
6500               (when (gnus-invisible-cut-children (cdr thread))
6501                 (let ((th (cdr thread)))
6502                   (while th
6503                     (if (memq (mail-header-number (caar th))
6504                               gnus-newsgroup-limit)
6505                         (setq thread (car th)
6506                               th nil)
6507                       (setq th (cdr th))))))))))
6508   thread)
6509
6510 (defun gnus-cut-threads (threads)
6511   "Cut off all uninteresting articles from the beginning of threads."
6512   (when (or (eq gnus-fetch-old-headers 'some)
6513             (eq gnus-fetch-old-headers 'invisible)
6514             (numberp gnus-fetch-old-headers)
6515             (eq gnus-build-sparse-threads 'some)
6516             (eq gnus-build-sparse-threads 'more))
6517     (let ((th threads))
6518       (while th
6519         (setcar th (gnus-cut-thread (car th)))
6520         (setq th (cdr th)))))
6521   ;; Remove nixed out threads.
6522   (delq nil threads))
6523
6524 (defun gnus-summary-initial-limit (&optional show-if-empty)
6525   "Figure out what the initial limit is supposed to be on group entry.
6526 This entails weeding out unwanted dormants, low-scored articles,
6527 fetch-old-headers verbiage, and so on."
6528   ;; Most groups have nothing to remove.
6529   (if (or gnus-inhibit-limiting
6530           (and (null gnus-newsgroup-dormant)
6531                (not (eq gnus-fetch-old-headers 'some))
6532                (not (numberp gnus-fetch-old-headers))
6533                (not (eq gnus-fetch-old-headers 'invisible))
6534                (null gnus-summary-expunge-below)
6535                (not (eq gnus-build-sparse-threads 'some))
6536                (not (eq gnus-build-sparse-threads 'more))
6537                (null gnus-thread-expunge-below)
6538                (not gnus-use-nocem)))
6539       ()                                ; Do nothing.
6540     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6541     (setq gnus-newsgroup-limit nil)
6542     (mapatoms
6543      (lambda (node)
6544        (unless (car (symbol-value node))
6545          ;; These threads have no parents -- they are roots.
6546          (let ((nodes (cdr (symbol-value node)))
6547                thread)
6548            (while nodes
6549              (if (and gnus-thread-expunge-below
6550                       (< (gnus-thread-total-score (car nodes))
6551                          gnus-thread-expunge-below))
6552                  (gnus-expunge-thread (pop nodes))
6553                (setq thread (pop nodes))
6554                (gnus-summary-limit-children thread))))))
6555      gnus-newsgroup-dependencies)
6556     ;; If this limitation resulted in an empty group, we might
6557     ;; pop the previous limit and use it instead.
6558     (when (and (not gnus-newsgroup-limit)
6559                show-if-empty)
6560       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6561     gnus-newsgroup-limit))
6562
6563 (defun gnus-summary-limit-children (thread)
6564   "Return 1 if this subthread is visible and 0 if it is not."
6565   ;; First we get the number of visible children to this thread.  This
6566   ;; is done by recursing down the thread using this function, so this
6567   ;; will really go down to a leaf article first, before slowly
6568   ;; working its way up towards the root.
6569   (when thread
6570     (let ((children
6571            (if (cdr thread)
6572                (apply '+ (mapcar 'gnus-summary-limit-children
6573                                  (cdr thread)))
6574              0))
6575           (number (mail-header-number (car thread)))
6576           score)
6577       (if (and
6578            (not (memq number gnus-newsgroup-marked))
6579            (or
6580             ;; If this article is dormant and has absolutely no visible
6581             ;; children, then this article isn't visible.
6582             (and (memq number gnus-newsgroup-dormant)
6583                  (zerop children))
6584             ;; If this is "fetch-old-headered" and there is no
6585             ;; visible children, then we don't want this article.
6586             (and (or (eq gnus-fetch-old-headers 'some)
6587                      (numberp gnus-fetch-old-headers))
6588                  (gnus-summary-article-ancient-p number)
6589                  (zerop children))
6590             ;; If this is "fetch-old-headered" and `invisible', then
6591             ;; we don't want this article.
6592             (and (eq gnus-fetch-old-headers 'invisible)
6593                  (gnus-summary-article-ancient-p number))
6594             ;; If this is a sparsely inserted article with no children,
6595             ;; we don't want it.
6596             (and (eq gnus-build-sparse-threads 'some)
6597                  (gnus-summary-article-sparse-p number)
6598                  (zerop children))
6599             ;; If we use expunging, and this article is really
6600             ;; low-scored, then we don't want this article.
6601             (when (and gnus-summary-expunge-below
6602                        (< (setq score
6603                                 (or (cdr (assq number gnus-newsgroup-scored))
6604                                     gnus-summary-default-score))
6605                           gnus-summary-expunge-below))
6606               ;; We increase the expunge-tally here, but that has
6607               ;; nothing to do with the limits, really.
6608               (incf gnus-newsgroup-expunged-tally)
6609               ;; We also mark as read here, if that's wanted.
6610               (when (and gnus-summary-mark-below
6611                          (< score gnus-summary-mark-below))
6612                 (setq gnus-newsgroup-unreads
6613                       (delq number gnus-newsgroup-unreads))
6614                 (if gnus-newsgroup-auto-expire
6615                     (push number gnus-newsgroup-expirable)
6616                   (push (cons number gnus-low-score-mark)
6617                         gnus-newsgroup-reads)))
6618               t)
6619             ;; Check NoCeM things.
6620             (if (and gnus-use-nocem
6621                      (gnus-nocem-unwanted-article-p
6622                       (mail-header-id (car thread))))
6623                 (progn
6624                   (setq gnus-newsgroup-unreads
6625                         (delq number gnus-newsgroup-unreads))
6626                   t))))
6627           ;; Nope, invisible article.
6628           0
6629         ;; Ok, this article is to be visible, so we add it to the limit
6630         ;; and return 1.
6631         (push number gnus-newsgroup-limit)
6632         1))))
6633
6634 (defun gnus-expunge-thread (thread)
6635   "Mark all articles in THREAD as read."
6636   (let* ((number (mail-header-number (car thread))))
6637     (incf gnus-newsgroup-expunged-tally)
6638     ;; We also mark as read here, if that's wanted.
6639     (setq gnus-newsgroup-unreads
6640           (delq number gnus-newsgroup-unreads))
6641     (if gnus-newsgroup-auto-expire
6642         (push number gnus-newsgroup-expirable)
6643       (push (cons number gnus-low-score-mark)
6644             gnus-newsgroup-reads)))
6645   ;; Go recursively through all subthreads.
6646   (mapcar 'gnus-expunge-thread (cdr thread)))
6647
6648 ;; Summary article oriented commands
6649
6650 (defun gnus-summary-refer-parent-article (n)
6651   "Refer parent article N times.
6652 If N is negative, go to ancestor -N instead.
6653 The difference between N and the number of articles fetched is returned."
6654   (interactive "p")
6655   (let ((skip 1)
6656         error header ref)
6657     (when (not (natnump n))
6658       (setq skip (abs n)
6659             n 1))
6660     (while (and (> n 0)
6661                 (not error))
6662       (setq header (gnus-summary-article-header))
6663       (if (and (eq (mail-header-number header)
6664                    (cdr gnus-article-current))
6665                (equal gnus-newsgroup-name
6666                       (car gnus-article-current)))
6667           ;; If we try to find the parent of the currently
6668           ;; displayed article, then we take a look at the actual
6669           ;; References header, since this is slightly more
6670           ;; reliable than the References field we got from the
6671           ;; server.
6672           (save-excursion
6673             (set-buffer gnus-original-article-buffer)
6674             (nnheader-narrow-to-headers)
6675             (unless (setq ref (message-fetch-field "references"))
6676               (setq ref (message-fetch-field "in-reply-to")))
6677             (widen))
6678         (setq ref
6679               ;; It's not the current article, so we take a bet on
6680               ;; the value we got from the server.
6681               (mail-header-references header)))
6682       (if (and ref
6683                (not (equal ref "")))
6684           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6685             (gnus-message 1 "Couldn't find parent"))
6686         (gnus-message 1 "No references in article %d"
6687                       (gnus-summary-article-number))
6688         (setq error t))
6689       (decf n))
6690     (gnus-summary-position-point)
6691     n))
6692
6693 (defun gnus-summary-refer-references ()
6694   "Fetch all articles mentioned in the References header.
6695 Return the number of articles fetched."
6696   (interactive)
6697   (let ((ref (mail-header-references (gnus-summary-article-header)))
6698         (current (gnus-summary-article-number))
6699         (n 0))
6700     (if (or (not ref)
6701             (equal ref ""))
6702         (error "No References in the current article")
6703       ;; For each Message-ID in the References header...
6704       (while (string-match "<[^>]*>" ref)
6705         (incf n)
6706         ;; ... fetch that article.
6707         (gnus-summary-refer-article
6708          (prog1 (match-string 0 ref)
6709            (setq ref (substring ref (match-end 0))))))
6710       (gnus-summary-goto-subject current)
6711       (gnus-summary-position-point)
6712       n)))
6713
6714 (defun gnus-summary-refer-thread (&optional limit)
6715   "Fetch all articles in the current thread.
6716 If LIMIT (the numerical prefix), fetch that many old headers instead
6717 of what's specified by the `gnus-refer-thread-limit' variable."
6718   (interactive "P")
6719   (let ((id (mail-header-id (gnus-summary-article-header)))
6720         (limit (if limit (prefix-numeric-value limit)
6721                  gnus-refer-thread-limit)))
6722     ;; We want to fetch LIMIT *old* headers, but we also have to
6723     ;; re-fetch all the headers in the current buffer, because many of
6724     ;; them may be undisplayed.  So we adjust LIMIT.
6725     (when (numberp limit)
6726       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6727     (unless (eq gnus-fetch-old-headers 'invisible)
6728       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6729       ;; Retrieve the headers and read them in.
6730       (if (eq (gnus-retrieve-headers
6731                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6732               'nov)
6733           (gnus-build-all-threads)
6734         (error "Can't fetch thread from backends that don't support NOV"))
6735       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6736     (gnus-summary-limit-include-thread id)))
6737
6738 (defun gnus-summary-refer-article (message-id)
6739   "Fetch an article specified by MESSAGE-ID."
6740   (interactive "sMessage-ID: ")
6741   (when (and (stringp message-id)
6742              (not (zerop (length message-id))))
6743     ;; Construct the correct Message-ID if necessary.
6744     ;; Suggested by tale@pawl.rpi.edu.
6745     (unless (string-match "^<" message-id)
6746       (setq message-id (concat "<" message-id)))
6747     (unless (string-match ">$" message-id)
6748       (setq message-id (concat message-id ">")))
6749     (let* ((header (gnus-id-to-header message-id))
6750            (sparse (and header
6751                         (gnus-summary-article-sparse-p
6752                          (mail-header-number header))
6753                         (memq (mail-header-number header)
6754                               gnus-newsgroup-limit)))
6755            number)
6756       (cond
6757        ;; If the article is present in the buffer we just go to it.
6758        ((and header
6759              (or (not (gnus-summary-article-sparse-p
6760                        (mail-header-number header)))
6761                  sparse))
6762         (prog1
6763             (gnus-summary-goto-article
6764              (mail-header-number header) nil t)
6765           (when sparse
6766             (gnus-summary-update-article (mail-header-number header)))))
6767        (t
6768         ;; We fetch the article.
6769         (catch 'found
6770           (dolist (gnus-override-method (gnus-refer-article-methods))
6771             (gnus-check-server gnus-override-method)
6772             ;; Fetch the header, and display the article.
6773             (when (setq number (gnus-summary-insert-subject message-id))
6774               (gnus-summary-select-article nil nil nil number)
6775               (throw 'found t)))
6776           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6777
6778 (defun gnus-refer-article-methods ()
6779   "Return a list of referrable methods."
6780   (cond
6781    ;; No method, so we default to current and native.
6782    ((null gnus-refer-article-method)
6783     (list gnus-current-select-method gnus-select-method))
6784    ;; Current.
6785    ((eq 'current gnus-refer-article-method)
6786     (list gnus-current-select-method))
6787    ;; List of select methods.
6788    ((not (stringp (cadr gnus-refer-article-method)))
6789     (let (out)
6790       (dolist (method gnus-refer-article-method)
6791         (push (if (eq 'current method)
6792                   gnus-current-select-method
6793                 method)
6794               out))
6795       (nreverse out)))
6796    ;; One single select method.
6797    (t
6798     (list gnus-refer-article-method))))
6799
6800 (defun gnus-summary-edit-parameters ()
6801   "Edit the group parameters of the current group."
6802   (interactive)
6803   (gnus-group-edit-group gnus-newsgroup-name 'params))
6804
6805 (defun gnus-summary-customize-parameters ()
6806   "Customize the group parameters of the current group."
6807   (interactive)
6808   (gnus-group-customize gnus-newsgroup-name))
6809
6810 (defun gnus-summary-enter-digest-group (&optional force)
6811   "Enter an nndoc group based on the current article.
6812 If FORCE, force a digest interpretation.  If not, try
6813 to guess what the document format is."
6814   (interactive "P")
6815   (let ((conf gnus-current-window-configuration))
6816     (save-excursion
6817       (gnus-summary-select-article))
6818     (setq gnus-current-window-configuration conf)
6819     (let* ((name (format "%s-%d"
6820                          (gnus-group-prefixed-name
6821                           gnus-newsgroup-name (list 'nndoc ""))
6822                          (save-excursion
6823                            (set-buffer gnus-summary-buffer)
6824                            gnus-current-article)))
6825            (ogroup gnus-newsgroup-name)
6826            (params (append (gnus-info-params (gnus-get-info ogroup))
6827                            (list (cons 'to-group ogroup))
6828                            (list (cons 'save-article-group ogroup))))
6829            (case-fold-search t)
6830            (buf (current-buffer))
6831            dig to-address)
6832       (save-excursion
6833         (set-buffer gnus-original-article-buffer)
6834         ;; Have the digest group inherit the main mail address of
6835         ;; the parent article.
6836         (when (setq to-address (or (message-fetch-field "reply-to")
6837                                    (message-fetch-field "from")))
6838           (setq params (append (list (cons 'to-address to-address)))))
6839         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6840         (insert-buffer-substring gnus-original-article-buffer)
6841         ;; Remove lines that may lead nndoc to misinterpret the
6842         ;; document type.
6843         (narrow-to-region
6844          (goto-char (point-min))
6845          (or (search-forward "\n\n" nil t) (point)))
6846         (goto-char (point-min))
6847         (delete-matching-lines "^Path:\\|^From ")
6848         (widen))
6849       (unwind-protect
6850           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6851                     (gnus-newsgroup-ephemeral-ignored-charsets
6852                      gnus-newsgroup-ignored-charsets))
6853                 (gnus-group-read-ephemeral-group
6854                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6855                               (nndoc-article-type
6856                                ,(if force 'mbox 'guess))) t))
6857               ;; Make all postings to this group go to the parent group.
6858               (nconc (gnus-info-params (gnus-get-info name))
6859                      params)
6860             ;; Couldn't select this doc group.
6861             (switch-to-buffer buf)
6862             (gnus-set-global-variables)
6863             (gnus-configure-windows 'summary)
6864             (gnus-message 3 "Article couldn't be entered?"))
6865         (kill-buffer dig)))))
6866
6867 (defun gnus-summary-read-document (n)
6868   "Open a new group based on the current article(s).
6869 This will allow you to read digests and other similar
6870 documents as newsgroups.
6871 Obeys the standard process/prefix convention."
6872   (interactive "P")
6873   (let* ((articles (gnus-summary-work-articles n))
6874          (ogroup gnus-newsgroup-name)
6875          (params (append (gnus-info-params (gnus-get-info ogroup))
6876                          (list (cons 'to-group ogroup))))
6877          article group egroup groups vgroup)
6878     (while (setq article (pop articles))
6879       (setq group (format "%s-%d" gnus-newsgroup-name article))
6880       (gnus-summary-remove-process-mark article)
6881       (when (gnus-summary-display-article article)
6882         (save-excursion
6883           (with-temp-buffer
6884             (insert-buffer-substring gnus-original-article-buffer)
6885             ;; Remove some headers that may lead nndoc to make
6886             ;; the wrong guess.
6887             (message-narrow-to-head)
6888             (goto-char (point-min))
6889             (delete-matching-lines "^\\(Path\\):\\|^From ")
6890             (widen)
6891             (if (setq egroup
6892                       (gnus-group-read-ephemeral-group
6893                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6894                                      (nndoc-article-type guess))
6895                        t nil t))
6896                 (progn
6897                   ;; Make all postings to this group go to the parent group.
6898                   (nconc (gnus-info-params (gnus-get-info egroup))
6899                          params)
6900                   (push egroup groups))
6901               ;; Couldn't select this doc group.
6902               (gnus-error 3 "Article couldn't be entered"))))))
6903     ;; Now we have selected all the documents.
6904     (cond
6905      ((not groups)
6906       (error "None of the articles could be interpreted as documents"))
6907      ((gnus-group-read-ephemeral-group
6908        (setq vgroup (format
6909                      "nnvirtual:%s-%s" gnus-newsgroup-name
6910                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6911        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6912        t
6913        (cons (current-buffer) 'summary)))
6914      (t
6915       (error "Couldn't select virtual nndoc group")))))
6916
6917 (defun gnus-summary-isearch-article (&optional regexp-p)
6918   "Do incremental search forward on the current article.
6919 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6920   (interactive "P")
6921   (gnus-summary-select-article)
6922   (gnus-configure-windows 'article)
6923   (gnus-eval-in-buffer-window gnus-article-buffer
6924     (save-restriction
6925       (widen)
6926       (isearch-forward regexp-p))))
6927
6928 (defun gnus-summary-search-article-forward (regexp &optional backward)
6929   "Search for an article containing REGEXP forward.
6930 If BACKWARD, search backward instead."
6931   (interactive
6932    (list (read-string
6933           (format "Search article %s (regexp%s): "
6934                   (if current-prefix-arg "backward" "forward")
6935                   (if gnus-last-search-regexp
6936                       (concat ", default " gnus-last-search-regexp)
6937                     "")))
6938          current-prefix-arg))
6939   (if (string-equal regexp "")
6940       (setq regexp (or gnus-last-search-regexp ""))
6941     (setq gnus-last-search-regexp regexp))
6942   (if (gnus-summary-search-article regexp backward)
6943       (gnus-summary-show-thread)
6944     (error "Search failed: \"%s\"" regexp)))
6945
6946 (defun gnus-summary-search-article-backward (regexp)
6947   "Search for an article containing REGEXP backward."
6948   (interactive
6949    (list (read-string
6950           (format "Search article backward (regexp%s): "
6951                   (if gnus-last-search-regexp
6952                       (concat ", default " gnus-last-search-regexp)
6953                     "")))))
6954   (gnus-summary-search-article-forward regexp 'backward))
6955
6956 (defun gnus-summary-search-article (regexp &optional backward)
6957   "Search for an article containing REGEXP.
6958 Optional argument BACKWARD means do search for backward.
6959 `gnus-select-article-hook' is not called during the search."
6960   ;; We have to require this here to make sure that the following
6961   ;; dynamic binding isn't shadowed by autoloading.
6962   (require 'gnus-async)
6963   (require 'gnus-art)
6964   (let ((gnus-select-article-hook nil)  ;Disable hook.
6965         (gnus-article-prepare-hook nil)
6966         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6967         (gnus-use-article-prefetch nil)
6968         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6969         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6970         (sum (current-buffer))
6971         (gnus-display-mime-function nil)
6972         (found nil)
6973         point)
6974     (gnus-save-hidden-threads
6975       (gnus-summary-select-article)
6976       (set-buffer gnus-article-buffer)
6977       (when backward
6978         (forward-line -1))
6979       (while (not found)
6980         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6981         (if (if backward
6982                 (re-search-backward regexp nil t)
6983               (re-search-forward regexp nil t))
6984             ;; We found the regexp.
6985             (progn
6986               (setq found 'found)
6987               (beginning-of-line)
6988               (set-window-start
6989                (get-buffer-window (current-buffer))
6990                (point))
6991               (forward-line 1)
6992               (set-window-point
6993                (get-buffer-window (current-buffer))
6994                (point))
6995               (set-buffer sum)
6996               (setq point (point)))
6997           ;; We didn't find it, so we go to the next article.
6998           (set-buffer sum)
6999           (setq found 'not)
7000           (while (eq found 'not)
7001             (if (not (if backward (gnus-summary-find-prev)
7002                        (gnus-summary-find-next)))
7003                 ;; No more articles.
7004                 (setq found t)
7005               ;; Select the next article and adjust point.
7006               (unless (gnus-summary-article-sparse-p
7007                        (gnus-summary-article-number))
7008                 (setq found nil)
7009                 (gnus-summary-select-article)
7010                 (set-buffer gnus-article-buffer)
7011                 (widen)
7012                 (goto-char (if backward (point-max) (point-min))))))))
7013       (gnus-message 7 ""))
7014     ;; Return whether we found the regexp.
7015     (when (eq found 'found)
7016       (goto-char point)
7017       (gnus-summary-show-thread)
7018       (gnus-summary-goto-subject gnus-current-article)
7019       (gnus-summary-position-point)
7020       t)))
7021
7022 (defun gnus-summary-find-matching (header regexp &optional backward unread
7023                                           not-case-fold)
7024   "Return a list of all articles that match REGEXP on HEADER.
7025 The search stars on the current article and goes forwards unless
7026 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7027 If UNREAD is non-nil, only unread articles will
7028 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7029 in the comparisons."
7030   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7031                 (gnus-data-find-list
7032                  (gnus-summary-article-number) (gnus-data-list backward))))
7033         (case-fold-search (not not-case-fold))
7034         articles d func)
7035     (if (consp header)
7036         (if (eq (car header) 'extra)
7037             (setq func
7038                   `(lambda (h)
7039                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7040                          "")))
7041           (error "%s is an invalid header" header))
7042       (unless (fboundp (intern (concat "mail-header-" header)))
7043         (error "%s is not a valid header" header))
7044       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7045     (while data
7046       (setq d (car data))
7047       (and (or (not unread)             ; We want all articles...
7048                (gnus-data-unread-p d))  ; Or just unreads.
7049            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7050            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7051            (push (gnus-data-number d) articles)) ; Success!
7052       (setq data (cdr data)))
7053     (nreverse articles)))
7054
7055 (defun gnus-summary-execute-command (header regexp command &optional backward)
7056   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7057 If HEADER is an empty string (or nil), the match is done on the entire
7058 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7059   (interactive
7060    (list (let ((completion-ignore-case t))
7061            (completing-read
7062             "Header name: "
7063             (mapcar (lambda (string) (list string))
7064                     '("Number" "Subject" "From" "Lines" "Date"
7065                       "Message-ID" "Xref" "References" "Body"))
7066             nil 'require-match))
7067          (read-string "Regexp: ")
7068          (read-key-sequence "Command: ")
7069          current-prefix-arg))
7070   (when (equal header "Body")
7071     (setq header ""))
7072   ;; Hidden thread subtrees must be searched as well.
7073   (gnus-summary-show-all-threads)
7074   ;; We don't want to change current point nor window configuration.
7075   (save-excursion
7076     (save-window-excursion
7077       (gnus-message 6 "Executing %s..." (key-description command))
7078       ;; We'd like to execute COMMAND interactively so as to give arguments.
7079       (gnus-execute header regexp
7080                     `(call-interactively ',(key-binding command))
7081                     backward)
7082       (gnus-message 6 "Executing %s...done" (key-description command)))))
7083
7084 (defun gnus-summary-beginning-of-article ()
7085   "Scroll the article back to the beginning."
7086   (interactive)
7087   (gnus-summary-select-article)
7088   (gnus-configure-windows 'article)
7089   (gnus-eval-in-buffer-window gnus-article-buffer
7090     (widen)
7091     (goto-char (point-min))
7092     (when gnus-page-broken
7093       (gnus-narrow-to-page))))
7094
7095 (defun gnus-summary-end-of-article ()
7096   "Scroll to the end of the article."
7097   (interactive)
7098   (gnus-summary-select-article)
7099   (gnus-configure-windows 'article)
7100   (gnus-eval-in-buffer-window gnus-article-buffer
7101     (widen)
7102     (goto-char (point-max))
7103     (recenter -3)
7104     (when gnus-page-broken
7105       (gnus-narrow-to-page))))
7106
7107 (defun gnus-summary-print-article (&optional filename n)
7108   "Generate and print a PostScript image of the N next (mail) articles.
7109
7110 If N is negative, print the N previous articles.  If N is nil and articles
7111 have been marked with the process mark, print these instead.
7112
7113 If the optional first argument FILENAME is nil, send the image to the
7114 printer.  If FILENAME is a string, save the PostScript image in a file with
7115 that name.  If FILENAME is a number, prompt the user for the name of the file
7116 to save in."
7117   (interactive (list (ps-print-preprint current-prefix-arg)
7118                      current-prefix-arg))
7119   (dolist (article (gnus-summary-work-articles n))
7120     (gnus-summary-select-article nil nil 'pseudo article)
7121     (gnus-eval-in-buffer-window gnus-article-buffer
7122       (let ((buffer (generate-new-buffer " *print*")))
7123         (unwind-protect
7124             (progn
7125               (copy-to-buffer buffer (point-min) (point-max))
7126               (set-buffer buffer)
7127               (gnus-article-delete-invisible-text)
7128               (let ((ps-left-header
7129                      (list
7130                       (concat "("
7131                               (mail-header-subject gnus-current-headers) ")")
7132                       (concat "("
7133                               (mail-header-from gnus-current-headers) ")")))
7134                     (ps-right-header
7135                      (list
7136                       "/pagenumberstring load"
7137                       (concat "("
7138                               (mail-header-date gnus-current-headers) ")"))))
7139                 (gnus-run-hooks 'gnus-ps-print-hook)
7140                 (save-excursion
7141                   (ps-print-buffer-with-faces filename))))
7142           (kill-buffer buffer))))))
7143
7144 (defun gnus-summary-show-article (&optional arg)
7145   "Force re-fetching of the current article.
7146 If ARG (the prefix) is a number, show the article with the charset 
7147 defined in `gnus-summary-show-article-charset-alist', or the charset
7148 inputed.
7149 If ARG (the prefix) is non-nil and not a number, show the raw article 
7150 without any article massaging functions being run."
7151   (interactive "P")
7152   (cond 
7153    ((numberp arg)
7154     (let ((gnus-newsgroup-charset 
7155            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7156                (read-coding-system "Charset: ")))
7157           (gnus-newsgroup-ignored-charsets 'gnus-all))
7158       (gnus-summary-select-article nil 'force)))
7159    ((not arg)
7160     ;; Select the article the normal way.
7161     (gnus-summary-select-article nil 'force))
7162    (t
7163     ;; We have to require this here to make sure that the following
7164     ;; dynamic binding isn't shadowed by autoloading.
7165     (require 'gnus-async)
7166     (require 'gnus-art)
7167     ;; Bind the article treatment functions to nil.
7168     (let ((gnus-have-all-headers t)
7169           gnus-article-prepare-hook
7170           gnus-article-decode-hook
7171           gnus-display-mime-function
7172           gnus-break-pages)
7173       ;; Destroy any MIME parts.
7174       (when (gnus-buffer-live-p gnus-article-buffer)
7175         (save-excursion
7176           (set-buffer gnus-article-buffer)
7177           (mm-destroy-parts gnus-article-mime-handles)))
7178       (gnus-summary-select-article nil 'force))))
7179   (gnus-summary-goto-subject gnus-current-article)
7180   (gnus-summary-position-point))
7181
7182 (defun gnus-summary-verbose-headers (&optional arg)
7183   "Toggle permanent full header display.
7184 If ARG is a positive number, turn header display on.
7185 If ARG is a negative number, turn header display off."
7186   (interactive "P")
7187   (setq gnus-show-all-headers
7188         (cond ((or (not (numberp arg))
7189                    (zerop arg))
7190                (not gnus-show-all-headers))
7191               ((natnump arg)
7192                t)))
7193   (gnus-summary-show-article))
7194
7195 (defun gnus-summary-toggle-header (&optional arg)
7196   "Show the headers if they are hidden, or hide them if they are shown.
7197 If ARG is a positive number, show the entire header.
7198 If ARG is a negative number, hide the unwanted header lines."
7199   (interactive "P")
7200   (save-excursion
7201     (set-buffer gnus-article-buffer)
7202     (save-restriction
7203       (let* ((buffer-read-only nil)
7204              (inhibit-point-motion-hooks t)
7205              hidden e)
7206         (setq hidden
7207               (if (numberp arg)
7208                   (>= arg 0)
7209                 (save-restriction 
7210                   (article-narrow-to-head)
7211                   (gnus-article-hidden-text-p 'headers))))
7212         (goto-char (point-min))
7213         (when (search-forward "\n\n" nil t)
7214           (delete-region (point-min) (1- (point))))
7215         (goto-char (point-min))
7216         (save-excursion
7217           (set-buffer gnus-original-article-buffer)
7218           (goto-char (point-min))
7219           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7220         (insert-buffer-substring gnus-original-article-buffer 1 e)
7221         (save-restriction
7222           (narrow-to-region (point-min) (point))
7223           (article-decode-encoded-words)
7224           (if  hidden
7225               (let ((gnus-treat-hide-headers nil)
7226                     (gnus-treat-hide-boring-headers nil))
7227                 (gnus-treat-article 'head))
7228             (gnus-treat-article 'head)))))))
7229
7230 (defun gnus-summary-show-all-headers ()
7231   "Make all header lines visible."
7232   (interactive)
7233   (gnus-article-show-all-headers))
7234
7235 (defun gnus-summary-caesar-message (&optional arg)
7236   "Caesar rotate the current article by 13.
7237 The numerical prefix specifies how many places to rotate each letter
7238 forward."
7239   (interactive "P")
7240   (gnus-summary-select-article)
7241   (let ((mail-header-separator ""))
7242     (gnus-eval-in-buffer-window gnus-article-buffer
7243       (save-restriction
7244         (widen)
7245         (let ((start (window-start))
7246               buffer-read-only)
7247           (message-caesar-buffer-body arg)
7248           (set-window-start (get-buffer-window (current-buffer)) start))))))
7249
7250 (defun gnus-summary-stop-page-breaking ()
7251   "Stop page breaking in the current article."
7252   (interactive)
7253   (gnus-summary-select-article)
7254   (gnus-eval-in-buffer-window gnus-article-buffer
7255     (widen)
7256     (when (gnus-visual-p 'page-marker)
7257       (let ((buffer-read-only nil))
7258         (gnus-remove-text-with-property 'gnus-prev)
7259         (gnus-remove-text-with-property 'gnus-next))
7260       (setq gnus-page-broken nil))))
7261
7262 (defun gnus-summary-move-article (&optional n to-newsgroup
7263                                             select-method action)
7264   "Move the current article to a different newsgroup.
7265 If N is a positive number, move the N next articles.
7266 If N is a negative number, move the N previous articles.
7267 If N is nil and any articles have been marked with the process mark,
7268 move those articles instead.
7269 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7270 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7271 re-spool using this method.
7272
7273 For this function to work, both the current newsgroup and the
7274 newsgroup that you want to move to have to support the `request-move'
7275 and `request-accept' functions."
7276   (interactive "P")
7277   (unless action
7278     (setq action 'move))
7279   ;; Disable marking as read.
7280   (let (gnus-mark-article-hook)
7281     (save-window-excursion
7282       (gnus-summary-select-article)))
7283   ;; Check whether the source group supports the required functions.
7284   (cond ((and (eq action 'move)
7285               (not (gnus-check-backend-function
7286                     'request-move-article gnus-newsgroup-name)))
7287          (error "The current group does not support article moving"))
7288         ((and (eq action 'crosspost)
7289               (not (gnus-check-backend-function
7290                     'request-replace-article gnus-newsgroup-name)))
7291          (error "The current group does not support article editing")))
7292   (let ((articles (gnus-summary-work-articles n))
7293         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7294         (names '((move "Move" "Moving")
7295                  (copy "Copy" "Copying")
7296                  (crosspost "Crosspost" "Crossposting")))
7297         (copy-buf (save-excursion
7298                     (nnheader-set-temp-buffer " *copy article*")))
7299         art-group to-method new-xref article to-groups)
7300     (unless (assq action names)
7301       (error "Unknown action %s" action))
7302     ;; Read the newsgroup name.
7303     (when (and (not to-newsgroup)
7304                (not select-method))
7305       (setq to-newsgroup
7306             (gnus-read-move-group-name
7307              (cadr (assq action names))
7308              (symbol-value (intern (format "gnus-current-%s-group" action)))
7309              articles prefix))
7310       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7311     (setq to-method (or select-method
7312                         (gnus-group-name-to-method to-newsgroup)))
7313     ;; Check the method we are to move this article to...
7314     (unless (gnus-check-backend-function
7315              'request-accept-article (car to-method))
7316       (error "%s does not support article copying" (car to-method)))
7317     (unless (gnus-check-server to-method)
7318       (error "Can't open server %s" (car to-method)))
7319     (gnus-message 6 "%s to %s: %s..."
7320                   (caddr (assq action names))
7321                   (or (car select-method) to-newsgroup) articles)
7322     (while articles
7323       (setq article (pop articles))
7324       (setq
7325        art-group
7326        (cond
7327         ;; Move the article.
7328         ((eq action 'move)
7329          ;; Remove this article from future suppression.
7330          (gnus-dup-unsuppress-article article)
7331          (gnus-request-move-article
7332           article                       ; Article to move
7333           gnus-newsgroup-name           ; From newsgroup
7334           (nth 1 (gnus-find-method-for-group
7335                   gnus-newsgroup-name)) ; Server
7336           (list 'gnus-request-accept-article
7337                 to-newsgroup (list 'quote select-method)
7338                 (not articles) t)       ; Accept form
7339           (not articles)))              ; Only save nov last time
7340         ;; Copy the article.
7341         ((eq action 'copy)
7342          (save-excursion
7343            (set-buffer copy-buf)
7344            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7345              (gnus-request-accept-article
7346               to-newsgroup select-method (not articles) t))))
7347         ;; Crosspost the article.
7348         ((eq action 'crosspost)
7349          (let ((xref (message-tokenize-header
7350                       (mail-header-xref (gnus-summary-article-header article))
7351                       " ")))
7352            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7353                                   ":" article))
7354            (unless xref
7355              (setq xref (list (system-name))))
7356            (setq new-xref
7357                  (concat
7358                   (mapconcat 'identity
7359                              (delete "Xref:" (delete new-xref xref))
7360                              " ")
7361                   " " new-xref))
7362            (save-excursion
7363              (set-buffer copy-buf)
7364              ;; First put the article in the destination group.
7365              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7366              (when (consp (setq art-group
7367                                 (gnus-request-accept-article
7368                                  to-newsgroup select-method (not articles))))
7369                (setq new-xref (concat new-xref " " (car art-group)
7370                                       ":" (cdr art-group)))
7371                ;; Now we have the new Xrefs header, so we insert
7372                ;; it and replace the new article.
7373                (nnheader-replace-header "Xref" new-xref)
7374                (gnus-request-replace-article
7375                 (cdr art-group) to-newsgroup (current-buffer))
7376                art-group))))))
7377       (cond
7378        ((not art-group)
7379         (gnus-message 1 "Couldn't %s article %s: %s"
7380                       (cadr (assq action names)) article
7381                       (nnheader-get-report (car to-method))))
7382        ((and (eq art-group 'junk)
7383              (eq action 'move))
7384         (gnus-summary-mark-article article gnus-canceled-mark)
7385         (gnus-message 4 "Deleted article %s" article))
7386        (t
7387         (let* ((pto-group (gnus-group-prefixed-name
7388                            (car art-group) to-method))
7389                (entry
7390                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7391                (info (nth 2 entry))
7392                (to-group (gnus-info-group info))
7393                to-marks)
7394           ;; Update the group that has been moved to.
7395           (when (and info
7396                      (memq action '(move copy)))
7397             (unless (member to-group to-groups)
7398               (push to-group to-groups))
7399
7400             (unless (memq article gnus-newsgroup-unreads)
7401               (push 'read to-marks)
7402               (gnus-info-set-read
7403                info (gnus-add-to-range (gnus-info-read info)
7404                                        (list (cdr art-group)))))
7405
7406             ;; Copy any marks over to the new group.
7407             (let ((marks gnus-article-mark-lists)
7408                   (to-article (cdr art-group)))
7409
7410               ;; See whether the article is to be put in the cache.
7411               (when gnus-use-cache
7412                 (gnus-cache-possibly-enter-article
7413                  to-group to-article
7414                  (memq article gnus-newsgroup-marked)
7415                  (memq article gnus-newsgroup-dormant)
7416                  (memq article gnus-newsgroup-unreads)))
7417
7418               (when (and (equal to-group gnus-newsgroup-name)
7419                          (not (memq article gnus-newsgroup-unreads)))
7420                 ;; Mark this article as read in this group.
7421                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7422                 (setcdr (gnus-active to-group) to-article)
7423                 (setcdr gnus-newsgroup-active to-article))
7424
7425               (while marks
7426                 (when (memq article (symbol-value
7427                                      (intern (format "gnus-newsgroup-%s"
7428                                                      (caar marks)))))
7429                   (push (cdar marks) to-marks)
7430                   ;; If the other group is the same as this group,
7431                   ;; then we have to add the mark to the list.
7432                   (when (equal to-group gnus-newsgroup-name)
7433                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7434                          (cons to-article
7435                                (symbol-value
7436                                 (intern (format "gnus-newsgroup-%s"
7437                                                 (caar marks)))))))
7438                   ;; Copy the marks to other group.
7439                   (gnus-add-marked-articles
7440                    to-group (cdar marks) (list to-article) info))
7441                 (setq marks (cdr marks)))
7442
7443               (gnus-request-set-mark to-group (list (list (list to-article)
7444                                                           'set
7445                                                           to-marks)))
7446
7447               (gnus-dribble-enter
7448                (concat "(gnus-group-set-info '"
7449                        (gnus-prin1-to-string (gnus-get-info to-group))
7450                        ")"))))
7451
7452           ;; Update the Xref header in this article to point to
7453           ;; the new crossposted article we have just created.
7454           (when (eq action 'crosspost)
7455             (save-excursion
7456               (set-buffer copy-buf)
7457               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7458               (nnheader-replace-header "Xref" new-xref)
7459               (gnus-request-replace-article
7460                article gnus-newsgroup-name (current-buffer)))))
7461
7462         ;;;!!!Why is this necessary?
7463         (set-buffer gnus-summary-buffer)
7464
7465         (gnus-summary-goto-subject article)
7466         (when (eq action 'move)
7467           (gnus-summary-mark-article article gnus-canceled-mark))))
7468       (gnus-summary-remove-process-mark article))
7469     ;; Re-activate all groups that have been moved to.
7470     (while to-groups
7471       (save-excursion
7472         (set-buffer gnus-group-buffer)
7473         (when (gnus-group-goto-group (car to-groups) t)
7474           (gnus-group-get-new-news-this-group 1 t))
7475         (pop to-groups)))
7476
7477     (gnus-kill-buffer copy-buf)
7478     (gnus-summary-position-point)
7479     (gnus-set-mode-line 'summary)))
7480
7481 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7482   "Move the current article to a different newsgroup.
7483 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7484 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7485 re-spool using this method."
7486   (interactive "P")
7487   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7488
7489 (defun gnus-summary-crosspost-article (&optional n)
7490   "Crosspost the current article to some other group."
7491   (interactive "P")
7492   (gnus-summary-move-article n nil nil 'crosspost))
7493
7494 (defcustom gnus-summary-respool-default-method nil
7495   "Default method for respooling an article.
7496 If nil, use to the current newsgroup method."
7497   :type '(choice (gnus-select-method :value (nnml ""))
7498                  (const nil))
7499   :group 'gnus-summary-mail)
7500
7501 (defun gnus-summary-respool-article (&optional n method)
7502   "Respool the current article.
7503 The article will be squeezed through the mail spooling process again,
7504 which means that it will be put in some mail newsgroup or other
7505 depending on `nnmail-split-methods'.
7506 If N is a positive number, respool the N next articles.
7507 If N is a negative number, respool the N previous articles.
7508 If N is nil and any articles have been marked with the process mark,
7509 respool those articles instead.
7510
7511 Respooling can be done both from mail groups and \"real\" newsgroups.
7512 In the former case, the articles in question will be moved from the
7513 current group into whatever groups they are destined to.  In the
7514 latter case, they will be copied into the relevant groups."
7515   (interactive
7516    (list current-prefix-arg
7517          (let* ((methods (gnus-methods-using 'respool))
7518                 (methname
7519                  (symbol-name (or gnus-summary-respool-default-method
7520                                   (car (gnus-find-method-for-group
7521                                         gnus-newsgroup-name)))))
7522                 (method
7523                  (gnus-completing-read
7524                   methname "What backend do you want to use when respooling?"
7525                   methods nil t nil 'gnus-mail-method-history))
7526                 ms)
7527            (cond
7528             ((zerop (length (setq ms (gnus-servers-using-backend
7529                                       (intern method)))))
7530              (list (intern method) ""))
7531             ((= 1 (length ms))
7532              (car ms))
7533             (t
7534              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7535                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7536                            ms-alist))))))))
7537   (unless method
7538     (error "No method given for respooling"))
7539   (if (assoc (symbol-name
7540               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7541              (gnus-methods-using 'respool))
7542       (gnus-summary-move-article n nil method)
7543     (gnus-summary-copy-article n nil method)))
7544
7545 (defun gnus-summary-import-article (file)
7546   "Import an arbitrary file into a mail newsgroup."
7547   (interactive "fImport file: ")
7548   (let ((group gnus-newsgroup-name)
7549         (now (current-time))
7550         atts lines)
7551     (unless (gnus-check-backend-function 'request-accept-article group)
7552       (error "%s does not support article importing" group))
7553     (or (file-readable-p file)
7554         (not (file-regular-p file))
7555         (error "Can't read %s" file))
7556     (save-excursion
7557       (set-buffer (gnus-get-buffer-create " *import file*"))
7558       (erase-buffer)
7559       (insert-file-contents file)
7560       (goto-char (point-min))
7561       (unless (nnheader-article-p)
7562         ;; This doesn't look like an article, so we fudge some headers.
7563         (setq atts (file-attributes file)
7564               lines (count-lines (point-min) (point-max)))
7565         (insert "From: " (read-string "From: ") "\n"
7566                 "Subject: " (read-string "Subject: ") "\n"
7567                 "Date: " (message-make-date (nth 5 atts))
7568                 "\n"
7569                 "Message-ID: " (message-make-message-id) "\n"
7570                 "Lines: " (int-to-string lines) "\n"
7571                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7572       (gnus-request-accept-article group nil t)
7573       (kill-buffer (current-buffer)))))
7574
7575 (defun gnus-summary-article-posted-p ()
7576   "Say whether the current (mail) article is available from news as well.
7577 This will be the case if the article has both been mailed and posted."
7578   (interactive)
7579   (let ((id (mail-header-references (gnus-summary-article-header)))
7580         (gnus-override-method (car (gnus-refer-article-methods))))
7581     (if (gnus-request-head id "")
7582         (gnus-message 2 "The current message was found on %s"
7583                       gnus-override-method)
7584       (gnus-message 2 "The current message couldn't be found on %s"
7585                     gnus-override-method)
7586       nil)))
7587
7588 (defun gnus-summary-expire-articles (&optional now)
7589   "Expire all articles that are marked as expirable in the current group."
7590   (interactive)
7591   (when (gnus-check-backend-function
7592          'request-expire-articles gnus-newsgroup-name)
7593     ;; This backend supports expiry.
7594     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7595            (expirable (if total
7596                           (progn
7597                             ;; We need to update the info for
7598                             ;; this group for `gnus-list-of-read-articles'
7599                             ;; to give us the right answer.
7600                             (gnus-run-hooks 'gnus-exit-group-hook)
7601                             (gnus-summary-update-info)
7602                             (gnus-list-of-read-articles gnus-newsgroup-name))
7603                         (setq gnus-newsgroup-expirable
7604                               (sort gnus-newsgroup-expirable '<))))
7605            (expiry-wait (if now 'immediate
7606                           (gnus-group-find-parameter
7607                            gnus-newsgroup-name 'expiry-wait)))
7608            es)
7609       (when expirable
7610         ;; There are expirable articles in this group, so we run them
7611         ;; through the expiry process.
7612         (gnus-message 6 "Expiring articles...")
7613         (unless (gnus-check-group gnus-newsgroup-name)
7614           (error "Can't open server for %s" gnus-newsgroup-name))
7615         ;; The list of articles that weren't expired is returned.
7616         (save-excursion
7617           (if expiry-wait
7618               (let ((nnmail-expiry-wait-function nil)
7619                     (nnmail-expiry-wait expiry-wait))
7620                 (setq es (gnus-request-expire-articles
7621                           expirable gnus-newsgroup-name)))
7622             (setq es (gnus-request-expire-articles
7623                       expirable gnus-newsgroup-name))))
7624         (unless total
7625           (setq gnus-newsgroup-expirable es))
7626         ;; We go through the old list of expirable, and mark all
7627         ;; really expired articles as nonexistent.
7628         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7629           (let ((gnus-use-cache nil))
7630             (while expirable
7631               (unless (memq (car expirable) es)
7632                 (when (gnus-data-find (car expirable))
7633                   (gnus-summary-mark-article
7634                    (car expirable) gnus-canceled-mark)))
7635               (setq expirable (cdr expirable)))))
7636         (gnus-message 6 "Expiring articles...done")))))
7637
7638 (defun gnus-summary-expire-articles-now ()
7639   "Expunge all expirable articles in the current group.
7640 This means that *all* articles that are marked as expirable will be
7641 deleted forever, right now."
7642   (interactive)
7643   (or gnus-expert-user
7644       (gnus-yes-or-no-p
7645        "Are you really, really, really sure you want to delete all these messages? ")
7646       (error "Phew!"))
7647   (gnus-summary-expire-articles t))
7648
7649 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7650 (defun gnus-summary-delete-article (&optional n)
7651   "Delete the N next (mail) articles.
7652 This command actually deletes articles.  This is not a marking
7653 command.  The article will disappear forever from your life, never to
7654 return.
7655 If N is negative, delete backwards.
7656 If N is nil and articles have been marked with the process mark,
7657 delete these instead."
7658   (interactive "P")
7659   (unless (gnus-check-backend-function 'request-expire-articles
7660                                        gnus-newsgroup-name)
7661     (error "The current newsgroup does not support article deletion"))
7662   ;; Compute the list of articles to delete.
7663   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7664         not-deleted)
7665     (if (and gnus-novice-user
7666              (not (gnus-yes-or-no-p
7667                    (format "Do you really want to delete %s forever? "
7668                            (if (> (length articles) 1)
7669                                (format "these %s articles" (length articles))
7670                              "this article")))))
7671         ()
7672       ;; Delete the articles.
7673       (setq not-deleted (gnus-request-expire-articles
7674                          articles gnus-newsgroup-name 'force))
7675       (while articles
7676         (gnus-summary-remove-process-mark (car articles))
7677         ;; The backend might not have been able to delete the article
7678         ;; after all.
7679         (unless (memq (car articles) not-deleted)
7680           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7681         (setq articles (cdr articles)))
7682       (when not-deleted
7683         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7684     (gnus-summary-position-point)
7685     (gnus-set-mode-line 'summary)
7686     not-deleted))
7687
7688 (defun gnus-summary-edit-article (&optional force)
7689   "Edit the current article.
7690 This will have permanent effect only in mail groups.
7691 If FORCE is non-nil, allow editing of articles even in read-only
7692 groups."
7693   (interactive "P")
7694   (save-excursion
7695     (set-buffer gnus-summary-buffer)
7696     (let ((mail-parse-charset gnus-newsgroup-charset)
7697           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7698       (gnus-set-global-variables)
7699       (when (and (not force)
7700                  (gnus-group-read-only-p))
7701         (error "The current newsgroup does not support article editing"))
7702       (gnus-summary-show-article t)
7703       (gnus-article-edit-article
7704        'mime-to-mml
7705        `(lambda (no-highlight)
7706           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7707                 (mail-parse-ignored-charsets 
7708                  ',gnus-newsgroup-ignored-charsets))
7709             (mml-to-mime)
7710             (gnus-summary-edit-article-done
7711              ,(or (mail-header-references gnus-current-headers) "")
7712              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7713
7714 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7715
7716 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7717                                                  no-highlight)
7718   "Make edits to the current article permanent."
7719   (interactive)
7720   ;; Replace the article.
7721   (let ((buf (current-buffer)))
7722     (with-temp-buffer
7723       (insert-buffer-substring buf)
7724       (if (and (not read-only)
7725                (not (gnus-request-replace-article
7726                      (cdr gnus-article-current) (car gnus-article-current)
7727                      (current-buffer) t)))
7728           (error "Couldn't replace article")
7729         ;; Update the summary buffer.
7730         (if (and references
7731                  (equal (message-tokenize-header references " ")
7732                         (message-tokenize-header
7733                          (or (message-fetch-field "references") "") " ")))
7734             ;; We only have to update this line.
7735             (save-excursion
7736               (save-restriction
7737                 (message-narrow-to-head)
7738                 (let ((head (buffer-string))
7739                       header)
7740                   (with-temp-buffer
7741                     (insert (format "211 %d Article retrieved.\n"
7742                                     (cdr gnus-article-current)))
7743                     (insert head)
7744                     (insert ".\n")
7745                     (let ((nntp-server-buffer (current-buffer)))
7746                       (setq header (car (gnus-get-newsgroup-headers
7747                                          (save-excursion
7748                                            (set-buffer gnus-summary-buffer)
7749                                            gnus-newsgroup-dependencies)
7750                                          t))))
7751                     (save-excursion
7752                       (set-buffer gnus-summary-buffer)
7753                       (gnus-data-set-header
7754                        (gnus-data-find (cdr gnus-article-current))
7755                        header)
7756                       (gnus-summary-update-article-line
7757                        (cdr gnus-article-current) header))))))
7758           ;; Update threads.
7759           (set-buffer (or buffer gnus-summary-buffer))
7760           (gnus-summary-update-article (cdr gnus-article-current)))
7761         ;; Prettify the article buffer again.
7762         (unless no-highlight
7763           (save-excursion
7764             (set-buffer gnus-article-buffer)
7765             ;;;!!! Fix this -- article should be rehighlighted.
7766             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7767             (set-buffer gnus-original-article-buffer)
7768             (gnus-request-article
7769              (cdr gnus-article-current)
7770              (car gnus-article-current) (current-buffer))))
7771         ;; Prettify the summary buffer line.
7772         (when (gnus-visual-p 'summary-highlight 'highlight)
7773           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7774
7775 (defun gnus-summary-edit-wash (key)
7776   "Perform editing command KEY in the article buffer."
7777   (interactive
7778    (list
7779     (progn
7780       (message "%s" (concat (this-command-keys) "- "))
7781       (read-char))))
7782   (message "")
7783   (gnus-summary-edit-article)
7784   (execute-kbd-macro (concat (this-command-keys) key))
7785   (gnus-article-edit-done))
7786
7787 ;;; Respooling
7788
7789 (defun gnus-summary-respool-query (&optional silent trace)
7790   "Query where the respool algorithm would put this article."
7791   (interactive)
7792   (let (gnus-mark-article-hook)
7793     (gnus-summary-select-article)
7794     (save-excursion
7795       (set-buffer gnus-original-article-buffer)
7796       (save-restriction
7797         (message-narrow-to-head)
7798         (let ((groups (nnmail-article-group 'identity trace)))
7799           (unless silent
7800             (if groups
7801                 (message "This message would go to %s"
7802                          (mapconcat 'car groups ", "))
7803               (message "This message would go to no groups"))
7804             groups))))))
7805
7806 (defun gnus-summary-respool-trace ()
7807   "Trace where the respool algorithm would put this article.
7808 Display a buffer showing all fancy splitting patterns which matched."
7809   (interactive)
7810   (gnus-summary-respool-query nil t))
7811
7812 ;; Summary marking commands.
7813
7814 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7815   "Mark articles which has the same subject as read, and then select the next.
7816 If UNMARK is positive, remove any kind of mark.
7817 If UNMARK is negative, tick articles."
7818   (interactive "P")
7819   (when unmark
7820     (setq unmark (prefix-numeric-value unmark)))
7821   (let ((count
7822          (gnus-summary-mark-same-subject
7823           (gnus-summary-article-subject) unmark)))
7824     ;; Select next unread article.  If auto-select-same mode, should
7825     ;; select the first unread article.
7826     (gnus-summary-next-article t (and gnus-auto-select-same
7827                                       (gnus-summary-article-subject)))
7828     (gnus-message 7 "%d article%s marked as %s"
7829                   count (if (= count 1) " is" "s are")
7830                   (if unmark "unread" "read"))))
7831
7832 (defun gnus-summary-kill-same-subject (&optional unmark)
7833   "Mark articles which has the same subject as read.
7834 If UNMARK is positive, remove any kind of mark.
7835 If UNMARK is negative, tick articles."
7836   (interactive "P")
7837   (when unmark
7838     (setq unmark (prefix-numeric-value unmark)))
7839   (let ((count
7840          (gnus-summary-mark-same-subject
7841           (gnus-summary-article-subject) unmark)))
7842     ;; If marked as read, go to next unread subject.
7843     (when (null unmark)
7844       ;; Go to next unread subject.
7845       (gnus-summary-next-subject 1 t))
7846     (gnus-message 7 "%d articles are marked as %s"
7847                   count (if unmark "unread" "read"))))
7848
7849 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7850   "Mark articles with same SUBJECT as read, and return marked number.
7851 If optional argument UNMARK is positive, remove any kinds of marks.
7852 If optional argument UNMARK is negative, mark articles as unread instead."
7853   (let ((count 1))
7854     (save-excursion
7855       (cond
7856        ((null unmark)                   ; Mark as read.
7857         (while (and
7858                 (progn
7859                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7860                   (gnus-summary-show-thread) t)
7861                 (gnus-summary-find-subject subject))
7862           (setq count (1+ count))))
7863        ((> unmark 0)                    ; Tick.
7864         (while (and
7865                 (progn
7866                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7867                   (gnus-summary-show-thread) t)
7868                 (gnus-summary-find-subject subject))
7869           (setq count (1+ count))))
7870        (t                               ; Mark as unread.
7871         (while (and
7872                 (progn
7873                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7874                   (gnus-summary-show-thread) t)
7875                 (gnus-summary-find-subject subject))
7876           (setq count (1+ count)))))
7877       (gnus-set-mode-line 'summary)
7878       ;; Return the number of marked articles.
7879       count)))
7880
7881 (defun gnus-summary-mark-as-processable (n &optional unmark)
7882   "Set the process mark on the next N articles.
7883 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7884 the process mark instead.  The difference between N and the actual
7885 number of articles marked is returned."
7886   (interactive "p")
7887   (let ((backward (< n 0))
7888         (n (abs n)))
7889     (while (and
7890             (> n 0)
7891             (if unmark
7892                 (gnus-summary-remove-process-mark
7893                  (gnus-summary-article-number))
7894               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7895             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7896       (setq n (1- n)))
7897     (when (/= 0 n)
7898       (gnus-message 7 "No more articles"))
7899     (gnus-summary-recenter)
7900     (gnus-summary-position-point)
7901     n))
7902
7903 (defun gnus-summary-unmark-as-processable (n)
7904   "Remove the process mark from the next N articles.
7905 If N is negative, unmark backward instead.  The difference between N and
7906 the actual number of articles unmarked is returned."
7907   (interactive "p")
7908   (gnus-summary-mark-as-processable n t))
7909
7910 (defun gnus-summary-unmark-all-processable ()
7911   "Remove the process mark from all articles."
7912   (interactive)
7913   (save-excursion
7914     (while gnus-newsgroup-processable
7915       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7916   (gnus-summary-position-point))
7917
7918 (defun gnus-summary-mark-as-expirable (n)
7919   "Mark N articles forward as expirable.
7920 If N is negative, mark backward instead.  The difference between N and
7921 the actual number of articles marked is returned."
7922   (interactive "p")
7923   (gnus-summary-mark-forward n gnus-expirable-mark))
7924
7925 (defun gnus-summary-mark-article-as-replied (article)
7926   "Mark ARTICLE replied and update the summary line."
7927   (push article gnus-newsgroup-replied)
7928   (let ((buffer-read-only nil))
7929     (when (gnus-summary-goto-subject article nil t)
7930       (gnus-summary-update-secondary-mark article))))
7931
7932 (defun gnus-summary-set-bookmark (article)
7933   "Set a bookmark in current article."
7934   (interactive (list (gnus-summary-article-number)))
7935   (when (or (not (get-buffer gnus-article-buffer))
7936             (not gnus-current-article)
7937             (not gnus-article-current)
7938             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7939     (error "No current article selected"))
7940   ;; Remove old bookmark, if one exists.
7941   (let ((old (assq article gnus-newsgroup-bookmarks)))
7942     (when old
7943       (setq gnus-newsgroup-bookmarks
7944             (delq old gnus-newsgroup-bookmarks))))
7945   ;; Set the new bookmark, which is on the form
7946   ;; (article-number . line-number-in-body).
7947   (push
7948    (cons article
7949          (save-excursion
7950            (set-buffer gnus-article-buffer)
7951            (count-lines
7952             (min (point)
7953                  (save-excursion
7954                    (goto-char (point-min))
7955                    (search-forward "\n\n" nil t)
7956                    (point)))
7957             (point))))
7958    gnus-newsgroup-bookmarks)
7959   (gnus-message 6 "A bookmark has been added to the current article."))
7960
7961 (defun gnus-summary-remove-bookmark (article)
7962   "Remove the bookmark from the current article."
7963   (interactive (list (gnus-summary-article-number)))
7964   ;; Remove old bookmark, if one exists.
7965   (let ((old (assq article gnus-newsgroup-bookmarks)))
7966     (if old
7967         (progn
7968           (setq gnus-newsgroup-bookmarks
7969                 (delq old gnus-newsgroup-bookmarks))
7970           (gnus-message 6 "Removed bookmark."))
7971       (gnus-message 6 "No bookmark in current article."))))
7972
7973 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7974 (defun gnus-summary-mark-as-dormant (n)
7975   "Mark N articles forward as dormant.
7976 If N is negative, mark backward instead.  The difference between N and
7977 the actual number of articles marked is returned."
7978   (interactive "p")
7979   (gnus-summary-mark-forward n gnus-dormant-mark))
7980
7981 (defun gnus-summary-set-process-mark (article)
7982   "Set the process mark on ARTICLE and update the summary line."
7983   (setq gnus-newsgroup-processable
7984         (cons article
7985               (delq article gnus-newsgroup-processable)))
7986   (when (gnus-summary-goto-subject article)
7987     (gnus-summary-show-thread)
7988     (gnus-summary-goto-subject article)
7989     (gnus-summary-update-secondary-mark article)))
7990
7991 (defun gnus-summary-remove-process-mark (article)
7992   "Remove the process mark from ARTICLE and update the summary line."
7993   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7994   (when (gnus-summary-goto-subject article)
7995     (gnus-summary-show-thread)
7996     (gnus-summary-goto-subject article)
7997     (gnus-summary-update-secondary-mark article)))
7998
7999 (defun gnus-summary-set-saved-mark (article)
8000   "Set the process mark on ARTICLE and update the summary line."
8001   (push article gnus-newsgroup-saved)
8002   (when (gnus-summary-goto-subject article)
8003     (gnus-summary-update-secondary-mark article)))
8004
8005 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8006   "Mark N articles as read forwards.
8007 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8008 The difference between N and the actual number of articles marked is
8009 returned."
8010   (interactive "p")
8011   (gnus-summary-show-thread)
8012   (let ((backward (< n 0))
8013         (gnus-summary-goto-unread
8014          (and gnus-summary-goto-unread
8015               (not (eq gnus-summary-goto-unread 'never))
8016               (not (memq mark (list gnus-unread-mark
8017                                     gnus-ticked-mark gnus-dormant-mark)))))
8018         (n (abs n))
8019         (mark (or mark gnus-del-mark)))
8020     (while (and (> n 0)
8021                 (gnus-summary-mark-article nil mark no-expire)
8022                 (zerop (gnus-summary-next-subject
8023                         (if backward -1 1)
8024                         (and gnus-summary-goto-unread
8025                              (not (eq gnus-summary-goto-unread 'never)))
8026                         t)))
8027       (setq n (1- n)))
8028     (when (/= 0 n)
8029       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8030     (gnus-summary-recenter)
8031     (gnus-summary-position-point)
8032     (gnus-set-mode-line 'summary)
8033     n))
8034
8035 (defun gnus-summary-mark-article-as-read (mark)
8036   "Mark the current article quickly as read with MARK."
8037   (let ((article (gnus-summary-article-number)))
8038     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8039     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8040     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8041     (push (cons article mark) gnus-newsgroup-reads)
8042     ;; Possibly remove from cache, if that is used.
8043     (when gnus-use-cache
8044       (gnus-cache-enter-remove-article article))
8045     ;; Allow the backend to change the mark.
8046     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8047     ;; Check for auto-expiry.
8048     (when (and gnus-newsgroup-auto-expire
8049                (memq mark gnus-auto-expirable-marks))
8050       (setq mark gnus-expirable-mark)
8051       ;; Let the backend know about the mark change.
8052       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8053       (push article gnus-newsgroup-expirable))
8054     ;; Set the mark in the buffer.
8055     (gnus-summary-update-mark mark 'unread)
8056     t))
8057
8058 (defun gnus-summary-mark-article-as-unread (mark)
8059   "Mark the current article quickly as unread with MARK."
8060   (let* ((article (gnus-summary-article-number))
8061          (old-mark (gnus-summary-article-mark article)))
8062     ;; Allow the backend to change the mark.
8063     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8064     (if (eq mark old-mark)
8065         t
8066       (if (<= article 0)
8067           (progn
8068             (gnus-error 1 "Can't mark negative article numbers")
8069             nil)
8070         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8071         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8072         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8073         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8074         (cond ((= mark gnus-ticked-mark)
8075                (push article gnus-newsgroup-marked))
8076               ((= mark gnus-dormant-mark)
8077                (push article gnus-newsgroup-dormant))
8078               (t
8079                (push article gnus-newsgroup-unreads)))
8080         (gnus-pull article gnus-newsgroup-reads)
8081
8082         ;; See whether the article is to be put in the cache.
8083         (and gnus-use-cache
8084              (vectorp (gnus-summary-article-header article))
8085              (save-excursion
8086                (gnus-cache-possibly-enter-article
8087                 gnus-newsgroup-name article
8088                 (= mark gnus-ticked-mark)
8089                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8090
8091         ;; Fix the mark.
8092         (gnus-summary-update-mark mark 'unread)
8093         t))))
8094
8095 (defun gnus-summary-mark-article (&optional article mark no-expire)
8096   "Mark ARTICLE with MARK.  MARK can be any character.
8097 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8098 `??' (dormant) and `?E' (expirable).
8099 If MARK is nil, then the default character `?r' is used.
8100 If ARTICLE is nil, then the article on the current line will be
8101 marked."
8102   ;; The mark might be a string.
8103   (when (stringp mark)
8104     (setq mark (aref mark 0)))
8105   ;; If no mark is given, then we check auto-expiring.
8106   (when (null mark)
8107     (setq mark gnus-del-mark))
8108   (when (and (not no-expire)
8109              gnus-newsgroup-auto-expire
8110              (memq mark gnus-auto-expirable-marks))
8111     (setq mark gnus-expirable-mark))
8112   (let ((article (or article (gnus-summary-article-number)))
8113         (old-mark (gnus-summary-article-mark article)))
8114     ;; Allow the backend to change the mark.
8115     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8116     (if (eq mark old-mark)
8117         t
8118       (unless article
8119         (error "No article on current line"))
8120       (if (not (if (or (= mark gnus-unread-mark)
8121                        (= mark gnus-ticked-mark)
8122                        (= mark gnus-dormant-mark))
8123                    (gnus-mark-article-as-unread article mark)
8124                  (gnus-mark-article-as-read article mark)))
8125           t
8126         ;; See whether the article is to be put in the cache.
8127         (and gnus-use-cache
8128              (not (= mark gnus-canceled-mark))
8129              (vectorp (gnus-summary-article-header article))
8130              (save-excursion
8131                (gnus-cache-possibly-enter-article
8132                 gnus-newsgroup-name article
8133                 (= mark gnus-ticked-mark)
8134                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8135
8136         (when (gnus-summary-goto-subject article nil t)
8137           (let ((buffer-read-only nil))
8138             (gnus-summary-show-thread)
8139             ;; Fix the mark.
8140             (gnus-summary-update-mark mark 'unread)
8141             t))))))
8142
8143 (defun gnus-summary-update-secondary-mark (article)
8144   "Update the secondary (read, process, cache) mark."
8145   (gnus-summary-update-mark
8146    (cond ((memq article gnus-newsgroup-processable)
8147           gnus-process-mark)
8148          ((memq article gnus-newsgroup-cached)
8149           gnus-cached-mark)
8150          ((memq article gnus-newsgroup-replied)
8151           gnus-replied-mark)
8152          ((memq article gnus-newsgroup-saved)
8153           gnus-saved-mark)
8154          (t gnus-unread-mark))
8155    'replied)
8156   (when (gnus-visual-p 'summary-highlight 'highlight)
8157     (gnus-run-hooks 'gnus-summary-update-hook))
8158   t)
8159
8160 (defun gnus-summary-update-mark (mark type)
8161   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8162         (buffer-read-only nil))
8163     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8164     (when forward
8165       (when (looking-at "\r")
8166         (incf forward))
8167       (when (<= (+ forward (point)) (point-max))
8168         ;; Go to the right position on the line.
8169         (goto-char (+ forward (point)))
8170         ;; Replace the old mark with the new mark.
8171         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8172         ;; Optionally update the marks by some user rule.
8173         (when (eq type 'unread)
8174           (gnus-data-set-mark
8175            (gnus-data-find (gnus-summary-article-number)) mark)
8176           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8177
8178 (defun gnus-mark-article-as-read (article &optional mark)
8179   "Enter ARTICLE in the pertinent lists and remove it from others."
8180   ;; Make the article expirable.
8181   (let ((mark (or mark gnus-del-mark)))
8182     (if (= mark gnus-expirable-mark)
8183         (push article gnus-newsgroup-expirable)
8184       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8185     ;; Remove from unread and marked lists.
8186     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8187     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8188     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8189     (push (cons article mark) gnus-newsgroup-reads)
8190     ;; Possibly remove from cache, if that is used.
8191     (when gnus-use-cache
8192       (gnus-cache-enter-remove-article article))
8193     t))
8194
8195 (defun gnus-mark-article-as-unread (article &optional mark)
8196   "Enter ARTICLE in the pertinent lists and remove it from others."
8197   (let ((mark (or mark gnus-ticked-mark)))
8198     (if (<= article 0)
8199         (progn
8200           (gnus-error 1 "Can't mark negative article numbers")
8201           nil)
8202       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8203             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8204             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8205             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8206
8207       ;; Unsuppress duplicates?
8208       (when gnus-suppress-duplicates
8209         (gnus-dup-unsuppress-article article))
8210
8211       (cond ((= mark gnus-ticked-mark)
8212              (push article gnus-newsgroup-marked))
8213             ((= mark gnus-dormant-mark)
8214              (push article gnus-newsgroup-dormant))
8215             (t
8216              (push article gnus-newsgroup-unreads)))
8217       (gnus-pull article gnus-newsgroup-reads)
8218       t)))
8219
8220 (defalias 'gnus-summary-mark-as-unread-forward
8221   'gnus-summary-tick-article-forward)
8222 (make-obsolete 'gnus-summary-mark-as-unread-forward
8223                'gnus-summary-tick-article-forward)
8224 (defun gnus-summary-tick-article-forward (n)
8225   "Tick N articles forwards.
8226 If N is negative, tick backwards instead.
8227 The difference between N and the number of articles ticked is returned."
8228   (interactive "p")
8229   (gnus-summary-mark-forward n gnus-ticked-mark))
8230
8231 (defalias 'gnus-summary-mark-as-unread-backward
8232   'gnus-summary-tick-article-backward)
8233 (make-obsolete 'gnus-summary-mark-as-unread-backward
8234                'gnus-summary-tick-article-backward)
8235 (defun gnus-summary-tick-article-backward (n)
8236   "Tick N articles backwards.
8237 The difference between N and the number of articles ticked is returned."
8238   (interactive "p")
8239   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8240
8241 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8242 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8243 (defun gnus-summary-tick-article (&optional article clear-mark)
8244   "Mark current article as unread.
8245 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8246 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8247   (interactive)
8248   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8249                                        gnus-ticked-mark)))
8250
8251 (defun gnus-summary-mark-as-read-forward (n)
8252   "Mark N articles as read forwards.
8253 If N is negative, mark backwards instead.
8254 The difference between N and the actual number of articles marked is
8255 returned."
8256   (interactive "p")
8257   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8258
8259 (defun gnus-summary-mark-as-read-backward (n)
8260   "Mark the N articles as read backwards.
8261 The difference between N and the actual number of articles marked is
8262 returned."
8263   (interactive "p")
8264   (gnus-summary-mark-forward
8265    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8266
8267 (defun gnus-summary-mark-as-read (&optional article mark)
8268   "Mark current article as read.
8269 ARTICLE specifies the article to be marked as read.
8270 MARK specifies a string to be inserted at the beginning of the line."
8271   (gnus-summary-mark-article article mark))
8272
8273 (defun gnus-summary-clear-mark-forward (n)
8274   "Clear marks from N articles forward.
8275 If N is negative, clear backward instead.
8276 The difference between N and the number of marks cleared is returned."
8277   (interactive "p")
8278   (gnus-summary-mark-forward n gnus-unread-mark))
8279
8280 (defun gnus-summary-clear-mark-backward (n)
8281   "Clear marks from N articles backward.
8282 The difference between N and the number of marks cleared is returned."
8283   (interactive "p")
8284   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8285
8286 (defun gnus-summary-mark-unread-as-read ()
8287   "Intended to be used by `gnus-summary-mark-article-hook'."
8288   (when (memq gnus-current-article gnus-newsgroup-unreads)
8289     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8290
8291 (defun gnus-summary-mark-read-and-unread-as-read ()
8292   "Intended to be used by `gnus-summary-mark-article-hook'."
8293   (let ((mark (gnus-summary-article-mark)))
8294     (when (or (gnus-unread-mark-p mark)
8295               (gnus-read-mark-p mark))
8296       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8297
8298 (defun gnus-summary-mark-region-as-read (point mark all)
8299   "Mark all unread articles between point and mark as read.
8300 If given a prefix, mark all articles between point and mark as read,
8301 even ticked and dormant ones."
8302   (interactive "r\nP")
8303   (save-excursion
8304     (let (article)
8305       (goto-char point)
8306       (beginning-of-line)
8307       (while (and
8308               (< (point) mark)
8309               (progn
8310                 (when (or all
8311                           (memq (setq article (gnus-summary-article-number))
8312                                 gnus-newsgroup-unreads))
8313                   (gnus-summary-mark-article article gnus-del-mark))
8314                 t)
8315               (gnus-summary-find-next))))))
8316
8317 (defun gnus-summary-mark-below (score mark)
8318   "Mark articles with score less than SCORE with MARK."
8319   (interactive "P\ncMark: ")
8320   (setq score (if score
8321                   (prefix-numeric-value score)
8322                 (or gnus-summary-default-score 0)))
8323   (save-excursion
8324     (set-buffer gnus-summary-buffer)
8325     (goto-char (point-min))
8326     (while
8327         (progn
8328           (and (< (gnus-summary-article-score) score)
8329                (gnus-summary-mark-article nil mark))
8330           (gnus-summary-find-next)))))
8331
8332 (defun gnus-summary-kill-below (&optional score)
8333   "Mark articles with score below SCORE as read."
8334   (interactive "P")
8335   (gnus-summary-mark-below score gnus-killed-mark))
8336
8337 (defun gnus-summary-clear-above (&optional score)
8338   "Clear all marks from articles with score above SCORE."
8339   (interactive "P")
8340   (gnus-summary-mark-above score gnus-unread-mark))
8341
8342 (defun gnus-summary-tick-above (&optional score)
8343   "Tick all articles with score above SCORE."
8344   (interactive "P")
8345   (gnus-summary-mark-above score gnus-ticked-mark))
8346
8347 (defun gnus-summary-mark-above (score mark)
8348   "Mark articles with score over SCORE with MARK."
8349   (interactive "P\ncMark: ")
8350   (setq score (if score
8351                   (prefix-numeric-value score)
8352                 (or gnus-summary-default-score 0)))
8353   (save-excursion
8354     (set-buffer gnus-summary-buffer)
8355     (goto-char (point-min))
8356     (while (and (progn
8357                   (when (> (gnus-summary-article-score) score)
8358                     (gnus-summary-mark-article nil mark))
8359                   t)
8360                 (gnus-summary-find-next)))))
8361
8362 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8363 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8364 (defun gnus-summary-limit-include-expunged (&optional no-error)
8365   "Display all the hidden articles that were expunged for low scores."
8366   (interactive)
8367   (let ((buffer-read-only nil))
8368     (let ((scored gnus-newsgroup-scored)
8369           headers h)
8370       (while scored
8371         (unless (gnus-summary-goto-subject (caar scored))
8372           (and (setq h (gnus-summary-article-header (caar scored)))
8373                (< (cdar scored) gnus-summary-expunge-below)
8374                (push h headers)))
8375         (setq scored (cdr scored)))
8376       (if (not headers)
8377           (when (not no-error)
8378             (error "No expunged articles hidden"))
8379         (goto-char (point-min))
8380         (gnus-summary-prepare-unthreaded (nreverse headers))
8381         (goto-char (point-min))
8382         (gnus-summary-position-point)
8383         t))))
8384
8385 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8386   "Mark all unread articles in this newsgroup as read.
8387 If prefix argument ALL is non-nil, ticked and dormant articles will
8388 also be marked as read.
8389 If QUIETLY is non-nil, no questions will be asked.
8390 If TO-HERE is non-nil, it should be a point in the buffer.  All
8391 articles before this point will be marked as read.
8392 Note that this function will only catch up the unread article
8393 in the current summary buffer limitation.
8394 The number of articles marked as read is returned."
8395   (interactive "P")
8396   (prog1
8397       (save-excursion
8398         (when (or quietly
8399                   (not gnus-interactive-catchup) ;Without confirmation?
8400                   gnus-expert-user
8401                   (gnus-y-or-n-p
8402                    (if all
8403                        "Mark absolutely all articles as read? "
8404                      "Mark all unread articles as read? ")))
8405           (if (and not-mark
8406                    (not gnus-newsgroup-adaptive)
8407                    (not gnus-newsgroup-auto-expire)
8408                    (not gnus-suppress-duplicates)
8409                    (or (not gnus-use-cache)
8410                        (eq gnus-use-cache 'passive)))
8411               (progn
8412                 (when all
8413                   (setq gnus-newsgroup-marked nil
8414                         gnus-newsgroup-dormant nil))
8415                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8416             ;; We actually mark all articles as canceled, which we
8417             ;; have to do when using auto-expiry or adaptive scoring.
8418             (gnus-summary-show-all-threads)
8419             (when (gnus-summary-first-subject (not all) t)
8420               (while (and
8421                       (if to-here (< (point) to-here) t)
8422                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8423                       (gnus-summary-find-next (not all) nil nil t))))
8424             (gnus-set-mode-line 'summary))
8425           t))
8426     (gnus-summary-position-point)))
8427
8428 (defun gnus-summary-catchup-to-here (&optional all)
8429   "Mark all unticked articles before the current one as read.
8430 If ALL is non-nil, also mark ticked and dormant articles as read."
8431   (interactive "P")
8432   (save-excursion
8433     (gnus-save-hidden-threads
8434       (let ((beg (point)))
8435         ;; We check that there are unread articles.
8436         (when (or all (gnus-summary-find-prev))
8437           (gnus-summary-catchup all t beg)))))
8438   (gnus-summary-position-point))
8439
8440 (defun gnus-summary-catchup-all (&optional quietly)
8441   "Mark all articles in this newsgroup as read."
8442   (interactive "P")
8443   (gnus-summary-catchup t quietly))
8444
8445 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8446   "Mark all unread articles in this group as read, then exit.
8447 If prefix argument ALL is non-nil, all articles are marked as read."
8448   (interactive "P")
8449   (when (gnus-summary-catchup all quietly nil 'fast)
8450     ;; Select next newsgroup or exit.
8451     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8452              (eq gnus-auto-select-next 'quietly))
8453         (gnus-summary-next-group nil)
8454       (gnus-summary-exit))))
8455
8456 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8457   "Mark all articles in this newsgroup as read, and then exit."
8458   (interactive "P")
8459   (gnus-summary-catchup-and-exit t quietly))
8460
8461 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8462   "Mark all articles in this group as read and select the next group.
8463 If given a prefix, mark all articles, unread as well as ticked, as
8464 read."
8465   (interactive "P")
8466   (save-excursion
8467     (gnus-summary-catchup all))
8468   (gnus-summary-next-group))
8469
8470 ;; Thread-based commands.
8471
8472 (defun gnus-summary-articles-in-thread (&optional article)
8473   "Return a list of all articles in the current thread.
8474 If ARTICLE is non-nil, return all articles in the thread that starts
8475 with that article."
8476   (let* ((article (or article (gnus-summary-article-number)))
8477          (data (gnus-data-find-list article))
8478          (top-level (gnus-data-level (car data)))
8479          (top-subject
8480           (cond ((null gnus-thread-operation-ignore-subject)
8481                  (gnus-simplify-subject-re
8482                   (mail-header-subject (gnus-data-header (car data)))))
8483                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8484                  (gnus-simplify-subject-fuzzy
8485                   (mail-header-subject (gnus-data-header (car data)))))
8486                 (t nil)))
8487          (end-point (save-excursion
8488                       (if (gnus-summary-go-to-next-thread)
8489                           (point) (point-max))))
8490          articles)
8491     (while (and data
8492                 (< (gnus-data-pos (car data)) end-point))
8493       (when (or (not top-subject)
8494                 (string= top-subject
8495                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8496                              (gnus-simplify-subject-fuzzy
8497                               (mail-header-subject
8498                                (gnus-data-header (car data))))
8499                            (gnus-simplify-subject-re
8500                             (mail-header-subject
8501                              (gnus-data-header (car data)))))))
8502         (push (gnus-data-number (car data)) articles))
8503       (unless (and (setq data (cdr data))
8504                    (> (gnus-data-level (car data)) top-level))
8505         (setq data nil)))
8506     ;; Return the list of articles.
8507     (nreverse articles)))
8508
8509 (defun gnus-summary-rethread-current ()
8510   "Rethread the thread the current article is part of."
8511   (interactive)
8512   (let* ((gnus-show-threads t)
8513          (article (gnus-summary-article-number))
8514          (id (mail-header-id (gnus-summary-article-header)))
8515          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8516     (unless id
8517       (error "No article on the current line"))
8518     (gnus-rebuild-thread id)
8519     (gnus-summary-goto-subject article)))
8520
8521 (defun gnus-summary-reparent-thread ()
8522   "Make the current article child of the marked (or previous) article.
8523
8524 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8525 is non-nil or the Subject: of both articles are the same."
8526   (interactive)
8527   (unless (not (gnus-group-read-only-p))
8528     (error "The current newsgroup does not support article editing"))
8529   (unless (<= (length gnus-newsgroup-processable) 1)
8530     (error "No more than one article may be marked"))
8531   (save-window-excursion
8532     (let ((gnus-article-buffer " *reparent*")
8533           (current-article (gnus-summary-article-number))
8534           ;; First grab the marked article, otherwise one line up.
8535           (parent-article (if (not (null gnus-newsgroup-processable))
8536                               (car gnus-newsgroup-processable)
8537                             (save-excursion
8538                               (if (eq (forward-line -1) 0)
8539                                   (gnus-summary-article-number)
8540                                 (error "Beginning of summary buffer"))))))
8541       (unless (not (eq current-article parent-article))
8542         (error "An article may not be self-referential"))
8543       (let ((message-id (mail-header-id
8544                          (gnus-summary-article-header parent-article))))
8545         (unless (and message-id (not (equal message-id "")))
8546           (error "No message-id in desired parent"))
8547         (gnus-with-article current-article
8548           (goto-char (point-min))
8549           (if (re-search-forward "^References: " nil t)
8550               (progn
8551                 (re-search-forward "^[^ \t]" nil t)
8552                 (forward-line -1)
8553                 (end-of-line)
8554                 (insert " " message-id))
8555             (insert "References: " message-id "\n")))
8556         (set-buffer gnus-summary-buffer)
8557         (gnus-summary-unmark-all-processable)
8558         (gnus-summary-update-article current-article)
8559         (gnus-summary-rethread-current)
8560         (gnus-message 3 "Article %d is now the child of article %d"
8561                       current-article parent-article)))))
8562
8563 (defun gnus-summary-toggle-threads (&optional arg)
8564   "Toggle showing conversation threads.
8565 If ARG is positive number, turn showing conversation threads on."
8566   (interactive "P")
8567   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8568     (setq gnus-show-threads
8569           (if (null arg) (not gnus-show-threads)
8570             (> (prefix-numeric-value arg) 0)))
8571     (gnus-summary-prepare)
8572     (gnus-summary-goto-subject current)
8573     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8574     (gnus-summary-position-point)))
8575
8576 (defun gnus-summary-show-all-threads ()
8577   "Show all threads."
8578   (interactive)
8579   (save-excursion
8580     (let ((buffer-read-only nil))
8581       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8582   (gnus-summary-position-point))
8583
8584 (defun gnus-summary-show-thread ()
8585   "Show thread subtrees.
8586 Returns nil if no thread was there to be shown."
8587   (interactive)
8588   (let ((buffer-read-only nil)
8589         (orig (point))
8590         ;; first goto end then to beg, to have point at beg after let
8591         (end (progn (end-of-line) (point)))
8592         (beg (progn (beginning-of-line) (point))))
8593     (prog1
8594         ;; Any hidden lines here?
8595         (search-forward "\r" end t)
8596       (subst-char-in-region beg end ?\^M ?\n t)
8597       (goto-char orig)
8598       (gnus-summary-position-point))))
8599
8600 (defun gnus-summary-hide-all-threads ()
8601   "Hide all thread subtrees."
8602   (interactive)
8603   (save-excursion
8604     (goto-char (point-min))
8605     (gnus-summary-hide-thread)
8606     (while (zerop (gnus-summary-next-thread 1 t))
8607       (gnus-summary-hide-thread)))
8608   (gnus-summary-position-point))
8609
8610 (defun gnus-summary-hide-thread ()
8611   "Hide thread subtrees.
8612 Returns nil if no threads were there to be hidden."
8613   (interactive)
8614   (let ((buffer-read-only nil)
8615         (start (point))
8616         (article (gnus-summary-article-number)))
8617     (goto-char start)
8618     ;; Go forward until either the buffer ends or the subthread
8619     ;; ends.
8620     (when (and (not (eobp))
8621                (or (zerop (gnus-summary-next-thread 1 t))
8622                    (goto-char (point-max))))
8623       (prog1
8624           (if (and (> (point) start)
8625                    (search-backward "\n" start t))
8626               (progn
8627                 (subst-char-in-region start (point) ?\n ?\^M)
8628                 (gnus-summary-goto-subject article))
8629             (goto-char start)
8630             nil)))))
8631
8632 (defun gnus-summary-go-to-next-thread (&optional previous)
8633   "Go to the same level (or less) next thread.
8634 If PREVIOUS is non-nil, go to previous thread instead.
8635 Return the article number moved to, or nil if moving was impossible."
8636   (let ((level (gnus-summary-thread-level))
8637         (way (if previous -1 1))
8638         (beg (point)))
8639     (forward-line way)
8640     (while (and (not (eobp))
8641                 (< level (gnus-summary-thread-level)))
8642       (forward-line way))
8643     (if (eobp)
8644         (progn
8645           (goto-char beg)
8646           nil)
8647       (setq beg (point))
8648       (prog1
8649           (gnus-summary-article-number)
8650         (goto-char beg)))))
8651
8652 (defun gnus-summary-next-thread (n &optional silent)
8653   "Go to the same level next N'th thread.
8654 If N is negative, search backward instead.
8655 Returns the difference between N and the number of skips actually
8656 done.
8657
8658 If SILENT, don't output messages."
8659   (interactive "p")
8660   (let ((backward (< n 0))
8661         (n (abs n)))
8662     (while (and (> n 0)
8663                 (gnus-summary-go-to-next-thread backward))
8664       (decf n))
8665     (unless silent
8666       (gnus-summary-position-point))
8667     (when (and (not silent) (/= 0 n))
8668       (gnus-message 7 "No more threads"))
8669     n))
8670
8671 (defun gnus-summary-prev-thread (n)
8672   "Go to the same level previous N'th thread.
8673 Returns the difference between N and the number of skips actually
8674 done."
8675   (interactive "p")
8676   (gnus-summary-next-thread (- n)))
8677
8678 (defun gnus-summary-go-down-thread ()
8679   "Go down one level in the current thread."
8680   (let ((children (gnus-summary-article-children)))
8681     (when children
8682       (gnus-summary-goto-subject (car children)))))
8683
8684 (defun gnus-summary-go-up-thread ()
8685   "Go up one level in the current thread."
8686   (let ((parent (gnus-summary-article-parent)))
8687     (when parent
8688       (gnus-summary-goto-subject parent))))
8689
8690 (defun gnus-summary-down-thread (n)
8691   "Go down thread N steps.
8692 If N is negative, go up instead.
8693 Returns the difference between N and how many steps down that were
8694 taken."
8695   (interactive "p")
8696   (let ((up (< n 0))
8697         (n (abs n)))
8698     (while (and (> n 0)
8699                 (if up (gnus-summary-go-up-thread)
8700                   (gnus-summary-go-down-thread)))
8701       (setq n (1- n)))
8702     (gnus-summary-position-point)
8703     (when (/= 0 n)
8704       (gnus-message 7 "Can't go further"))
8705     n))
8706
8707 (defun gnus-summary-up-thread (n)
8708   "Go up thread N steps.
8709 If N is negative, go up instead.
8710 Returns the difference between N and how many steps down that were
8711 taken."
8712   (interactive "p")
8713   (gnus-summary-down-thread (- n)))
8714
8715 (defun gnus-summary-top-thread ()
8716   "Go to the top of the thread."
8717   (interactive)
8718   (while (gnus-summary-go-up-thread))
8719   (gnus-summary-article-number))
8720
8721 (defun gnus-summary-kill-thread (&optional unmark)
8722   "Mark articles under current thread as read.
8723 If the prefix argument is positive, remove any kinds of marks.
8724 If the prefix argument is negative, tick articles instead."
8725   (interactive "P")
8726   (when unmark
8727     (setq unmark (prefix-numeric-value unmark)))
8728   (let ((articles (gnus-summary-articles-in-thread)))
8729     (save-excursion
8730       ;; Expand the thread.
8731       (gnus-summary-show-thread)
8732       ;; Mark all the articles.
8733       (while articles
8734         (gnus-summary-goto-subject (car articles))
8735         (cond ((null unmark)
8736                (gnus-summary-mark-article-as-read gnus-killed-mark))
8737               ((> unmark 0)
8738                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8739               (t
8740                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8741         (setq articles (cdr articles))))
8742     ;; Hide killed subtrees.
8743     (and (null unmark)
8744          gnus-thread-hide-killed
8745          (gnus-summary-hide-thread))
8746     ;; If marked as read, go to next unread subject.
8747     (when (null unmark)
8748       ;; Go to next unread subject.
8749       (gnus-summary-next-subject 1 t)))
8750   (gnus-set-mode-line 'summary))
8751
8752 ;; Summary sorting commands
8753
8754 (defun gnus-summary-sort-by-number (&optional reverse)
8755   "Sort the summary buffer by article number.
8756 Argument REVERSE means reverse order."
8757   (interactive "P")
8758   (gnus-summary-sort 'number reverse))
8759
8760 (defun gnus-summary-sort-by-author (&optional reverse)
8761   "Sort the summary buffer by author name alphabetically.
8762 If case-fold-search is non-nil, case of letters is ignored.
8763 Argument REVERSE means reverse order."
8764   (interactive "P")
8765   (gnus-summary-sort 'author reverse))
8766
8767 (defun gnus-summary-sort-by-subject (&optional reverse)
8768   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8769 If case-fold-search is non-nil, case of letters is ignored.
8770 Argument REVERSE means reverse order."
8771   (interactive "P")
8772   (gnus-summary-sort 'subject reverse))
8773
8774 (defun gnus-summary-sort-by-date (&optional reverse)
8775   "Sort the summary buffer by date.
8776 Argument REVERSE means reverse order."
8777   (interactive "P")
8778   (gnus-summary-sort 'date reverse))
8779
8780 (defun gnus-summary-sort-by-score (&optional reverse)
8781   "Sort the summary buffer by score.
8782 Argument REVERSE means reverse order."
8783   (interactive "P")
8784   (gnus-summary-sort 'score reverse))
8785
8786 (defun gnus-summary-sort-by-lines (&optional reverse)
8787   "Sort the summary buffer by the number of lines.
8788 Argument REVERSE means reverse order."
8789   (interactive "P")
8790   (gnus-summary-sort 'lines reverse))
8791
8792 (defun gnus-summary-sort-by-chars (&optional reverse)
8793   "Sort the summary buffer by article length.
8794 Argument REVERSE means reverse order."
8795   (interactive "P")
8796   (gnus-summary-sort 'chars reverse))   
8797
8798 (defun gnus-summary-sort (predicate reverse)
8799   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8800   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8801          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8802          (gnus-thread-sort-functions
8803           (if (not reverse)
8804               thread
8805             `(lambda (t1 t2)
8806                (,thread t2 t1))))
8807          (gnus-article-sort-functions
8808           (if (not reverse)
8809               article
8810             `(lambda (t1 t2)
8811                (,article t2 t1))))
8812          (buffer-read-only)
8813          (gnus-summary-prepare-hook nil))
8814     ;; We do the sorting by regenerating the threads.
8815     (gnus-summary-prepare)
8816     ;; Hide subthreads if needed.
8817     (when (and gnus-show-threads gnus-thread-hide-subtree)
8818       (gnus-summary-hide-all-threads))))
8819
8820 ;; Summary saving commands.
8821
8822 (defun gnus-summary-save-article (&optional n not-saved)
8823   "Save the current article using the default saver function.
8824 If N is a positive number, save the N next articles.
8825 If N is a negative number, save the N previous articles.
8826 If N is nil and any articles have been marked with the process mark,
8827 save those articles instead.
8828 The variable `gnus-default-article-saver' specifies the saver function."
8829   (interactive "P")
8830   (let* ((articles (gnus-summary-work-articles n))
8831          (save-buffer (save-excursion
8832                         (nnheader-set-temp-buffer " *Gnus Save*")))
8833          (num (length articles))
8834          header file)
8835     (dolist (article articles)
8836       (setq header (gnus-summary-article-header article))
8837       (if (not (vectorp header))
8838           ;; This is a pseudo-article.
8839           (if (assq 'name header)
8840               (gnus-copy-file (cdr (assq 'name header)))
8841             (gnus-message 1 "Article %d is unsaveable" article))
8842         ;; This is a real article.
8843         (save-window-excursion
8844           (gnus-summary-select-article t nil nil article))
8845         (save-excursion
8846           (set-buffer save-buffer)
8847           (erase-buffer)
8848           (insert-buffer-substring gnus-original-article-buffer))
8849         (setq file (gnus-article-save save-buffer file num))
8850         (gnus-summary-remove-process-mark article)
8851         (unless not-saved
8852           (gnus-summary-set-saved-mark article))))
8853     (gnus-kill-buffer save-buffer)
8854     (gnus-summary-position-point)
8855     (gnus-set-mode-line 'summary)
8856     n))
8857
8858 (defun gnus-summary-pipe-output (&optional arg)
8859   "Pipe the current article to a subprocess.
8860 If N is a positive number, pipe the N next articles.
8861 If N is a negative number, pipe the N previous articles.
8862 If N is nil and any articles have been marked with the process mark,
8863 pipe those articles instead."
8864   (interactive "P")
8865   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8866     (gnus-summary-save-article arg t))
8867   (gnus-configure-windows 'pipe))
8868
8869 (defun gnus-summary-save-article-mail (&optional arg)
8870   "Append the current article to an mail file.
8871 If N is a positive number, save the N next articles.
8872 If N is a negative number, save the N previous articles.
8873 If N is nil and any articles have been marked with the process mark,
8874 save those articles instead."
8875   (interactive "P")
8876   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8877     (gnus-summary-save-article arg)))
8878
8879 (defun gnus-summary-save-article-rmail (&optional arg)
8880   "Append the current article to an rmail file.
8881 If N is a positive number, save the N next articles.
8882 If N is a negative number, save the N previous articles.
8883 If N is nil and any articles have been marked with the process mark,
8884 save those articles instead."
8885   (interactive "P")
8886   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8887     (gnus-summary-save-article arg)))
8888
8889 (defun gnus-summary-save-article-file (&optional arg)
8890   "Append the current article to a file.
8891 If N is a positive number, save the N next articles.
8892 If N is a negative number, save the N previous articles.
8893 If N is nil and any articles have been marked with the process mark,
8894 save those articles instead."
8895   (interactive "P")
8896   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8897     (gnus-summary-save-article arg)))
8898
8899 (defun gnus-summary-write-article-file (&optional arg)
8900   "Write the current article to a file, deleting the previous file.
8901 If N is a positive number, save the N next articles.
8902 If N is a negative number, save the N previous articles.
8903 If N is nil and any articles have been marked with the process mark,
8904 save those articles instead."
8905   (interactive "P")
8906   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8907     (gnus-summary-save-article arg)))
8908
8909 (defun gnus-summary-save-article-body-file (&optional arg)
8910   "Append the current article body to a file.
8911 If N is a positive number, save the N next articles.
8912 If N is a negative number, save the N previous articles.
8913 If N is nil and any articles have been marked with the process mark,
8914 save those articles instead."
8915   (interactive "P")
8916   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8917     (gnus-summary-save-article arg)))
8918
8919 (defun gnus-summary-pipe-message (program)
8920   "Pipe the current article through PROGRAM."
8921   (interactive "sProgram: ")
8922   (gnus-summary-select-article)
8923   (let ((mail-header-separator ""))
8924     (gnus-eval-in-buffer-window gnus-article-buffer
8925       (save-restriction
8926         (widen)
8927         (let ((start (window-start))
8928               buffer-read-only)
8929           (message-pipe-buffer-body program)
8930           (set-window-start (get-buffer-window (current-buffer)) start))))))
8931
8932 (defun gnus-get-split-value (methods)
8933   "Return a value based on the split METHODS."
8934   (let (split-name method result match)
8935     (when methods
8936       (save-excursion
8937         (set-buffer gnus-original-article-buffer)
8938         (save-restriction
8939           (nnheader-narrow-to-headers)
8940           (while methods
8941             (goto-char (point-min))
8942             (setq method (pop methods))
8943             (setq match (car method))
8944             (when (cond
8945                    ((stringp match)
8946                     ;; Regular expression.
8947                     (ignore-errors
8948                       (re-search-forward match nil t)))
8949                    ((gnus-functionp match)
8950                     ;; Function.
8951                     (save-restriction
8952                       (widen)
8953                       (setq result (funcall match gnus-newsgroup-name))))
8954                    ((consp match)
8955                     ;; Form.
8956                     (save-restriction
8957                       (widen)
8958                       (setq result (eval match)))))
8959               (setq split-name (append (cdr method) split-name))
8960               (cond ((stringp result)
8961                      (push (expand-file-name
8962                             result gnus-article-save-directory)
8963                            split-name))
8964                     ((consp result)
8965                      (setq split-name (append result split-name)))))))))
8966     (nreverse split-name)))
8967
8968 (defun gnus-valid-move-group-p (group)
8969   (and (boundp group)
8970        (symbol-name group)
8971        (symbol-value group)
8972        (gnus-get-function (gnus-find-method-for-group
8973                            (symbol-name group)) 'request-accept-article t)))
8974
8975 (defun gnus-read-move-group-name (prompt default articles prefix)
8976   "Read a group name."
8977   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8978          (minibuffer-confirm-incomplete nil) ; XEmacs
8979          (prom
8980           (format "%s %s to:"
8981                   prompt
8982                   (if (> (length articles) 1)
8983                       (format "these %d articles" (length articles))
8984                     "this article")))
8985          (to-newsgroup
8986           (cond
8987            ((null split-name)
8988             (gnus-completing-read default prom
8989                                   gnus-active-hashtb
8990                                   'gnus-valid-move-group-p
8991                                   nil prefix
8992                                   'gnus-group-history))
8993            ((= 1 (length split-name))
8994             (gnus-completing-read (car split-name) prom
8995                                   gnus-active-hashtb
8996                                   'gnus-valid-move-group-p
8997                                   nil nil
8998                                   'gnus-group-history))
8999            (t
9000             (gnus-completing-read nil prom
9001                                   (mapcar (lambda (el) (list el))
9002                                           (nreverse split-name))
9003                                   nil nil nil
9004                                   'gnus-group-history)))))
9005     (when to-newsgroup
9006       (if (or (string= to-newsgroup "")
9007               (string= to-newsgroup prefix))
9008           (setq to-newsgroup default))
9009       (unless to-newsgroup
9010         (error "No group name entered"))
9011       (or (gnus-active to-newsgroup)
9012           (gnus-activate-group to-newsgroup)
9013           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9014                                      to-newsgroup))
9015               (or (and (gnus-request-create-group
9016                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9017                        (gnus-activate-group
9018                         to-newsgroup nil nil
9019                         (gnus-group-name-to-method to-newsgroup))
9020                        (gnus-subscribe-group to-newsgroup))
9021                   (error "Couldn't create group %s" to-newsgroup)))
9022           (error "No such group: %s" to-newsgroup)))
9023     to-newsgroup))
9024
9025 (defun gnus-summary-save-parts (type dir n reverse)
9026   "Save parts matching TYPE to DIR.
9027 If REVERSE, save parts that do not match TYPE."
9028   (interactive
9029    (list (read-string "Save parts of type: " "image/.*")
9030          (read-file-name "Save to directory: " t nil t)
9031          current-prefix-arg))
9032   (gnus-summary-iterate n
9033     (let ((gnus-display-mime-function nil)
9034           (gnus-inhibit-treatment t))
9035       (gnus-summary-select-article))
9036     (save-excursion
9037       (set-buffer gnus-article-buffer)
9038       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9039         (when handles
9040           (gnus-summary-save-parts-1 type dir handles reverse)
9041           (mm-destroy-parts handles))))))
9042
9043 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9044   (if (stringp (car handle))
9045       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9046               (cdr handle))
9047     (when (if reverse
9048               (not (string-match type (mm-handle-media-type handle)))
9049             (string-match type (mm-handle-media-type handle)))
9050       (let ((file (expand-file-name
9051                    (file-name-nondirectory
9052                     (or
9053                      (mail-content-type-get
9054                       (mm-handle-disposition handle) 'filename)
9055                      (concat gnus-newsgroup-name "." gnus-current-article)))
9056                    dir)))
9057         (unless (file-exists-p file)
9058           (mm-save-part-to-file handle file))))))
9059
9060 ;; Summary extract commands
9061
9062 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9063   (let ((buffer-read-only nil)
9064         (article (gnus-summary-article-number))
9065         after-article b e)
9066     (unless (gnus-summary-goto-subject article)
9067       (error "No such article: %d" article))
9068     (gnus-summary-position-point)
9069     ;; If all commands are to be bunched up on one line, we collect
9070     ;; them here.
9071     (unless gnus-view-pseudos-separately
9072       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9073             files action)
9074         (while ps
9075           (setq action (cdr (assq 'action (car ps))))
9076           (setq files (list (cdr (assq 'name (car ps)))))
9077           (while (and ps (cdr ps)
9078                       (string= (or action "1")
9079                                (or (cdr (assq 'action (cadr ps))) "2")))
9080             (push (cdr (assq 'name (cadr ps))) files)
9081             (setcdr ps (cddr ps)))
9082           (when files
9083             (when (not (string-match "%s" action))
9084               (push " " files))
9085             (push " " files)
9086             (when (assq 'execute (car ps))
9087               (setcdr (assq 'execute (car ps))
9088                       (funcall (if (string-match "%s" action)
9089                                    'format 'concat)
9090                                action
9091                                (mapconcat
9092                                 (lambda (f)
9093                                   (if (equal f " ")
9094                                       f
9095                                     (mm-quote-arg f)))
9096                                 files " ")))))
9097           (setq ps (cdr ps)))))
9098     (if (and gnus-view-pseudos (not not-view))
9099         (while pslist
9100           (when (assq 'execute (car pslist))
9101             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9102                                   (eq gnus-view-pseudos 'not-confirm)))
9103           (setq pslist (cdr pslist)))
9104       (save-excursion
9105         (while pslist
9106           (setq after-article (or (cdr (assq 'article (car pslist)))
9107                                   (gnus-summary-article-number)))
9108           (gnus-summary-goto-subject after-article)
9109           (forward-line 1)
9110           (setq b (point))
9111           (insert "    " (file-name-nondirectory
9112                           (cdr (assq 'name (car pslist))))
9113                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9114           (setq e (point))
9115           (forward-line -1)             ; back to `b'
9116           (gnus-add-text-properties
9117            b (1- e) (list 'gnus-number gnus-reffed-article-number
9118                           gnus-mouse-face-prop gnus-mouse-face))
9119           (gnus-data-enter
9120            after-article gnus-reffed-article-number
9121            gnus-unread-mark b (car pslist) 0 (- e b))
9122           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9123           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9124           (setq pslist (cdr pslist)))))))
9125
9126 (defun gnus-pseudos< (p1 p2)
9127   (let ((c1 (cdr (assq 'action p1)))
9128         (c2 (cdr (assq 'action p2))))
9129     (and c1 c2 (string< c1 c2))))
9130
9131 (defun gnus-request-pseudo-article (props)
9132   (cond ((assq 'execute props)
9133          (gnus-execute-command (cdr (assq 'execute props)))))
9134   (let ((gnus-current-article (gnus-summary-article-number)))
9135     (gnus-run-hooks 'gnus-mark-article-hook)))
9136
9137 (defun gnus-execute-command (command &optional automatic)
9138   (save-excursion
9139     (gnus-article-setup-buffer)
9140     (set-buffer gnus-article-buffer)
9141     (setq buffer-read-only nil)
9142     (let ((command (if automatic command
9143                      (read-string "Command: " (cons command 0)))))
9144       (erase-buffer)
9145       (insert "$ " command "\n\n")
9146       (if gnus-view-pseudo-asynchronously
9147           (start-process "gnus-execute" (current-buffer) shell-file-name
9148                          shell-command-switch command)
9149         (call-process shell-file-name nil t nil
9150                       shell-command-switch command)))))
9151
9152 ;; Summary kill commands.
9153
9154 (defun gnus-summary-edit-global-kill (article)
9155   "Edit the \"global\" kill file."
9156   (interactive (list (gnus-summary-article-number)))
9157   (gnus-group-edit-global-kill article))
9158
9159 (defun gnus-summary-edit-local-kill ()
9160   "Edit a local kill file applied to the current newsgroup."
9161   (interactive)
9162   (setq gnus-current-headers (gnus-summary-article-header))
9163   (gnus-group-edit-local-kill
9164    (gnus-summary-article-number) gnus-newsgroup-name))
9165
9166 ;;; Header reading.
9167
9168 (defun gnus-read-header (id &optional header)
9169   "Read the headers of article ID and enter them into the Gnus system."
9170   (let ((group gnus-newsgroup-name)
9171         (gnus-override-method
9172          (or
9173           gnus-override-method
9174           (and (gnus-news-group-p gnus-newsgroup-name)
9175                (car (gnus-refer-article-methods)))))
9176         where)
9177     ;; First we check to see whether the header in question is already
9178     ;; fetched.
9179     (if (stringp id)
9180         ;; This is a Message-ID.
9181         (setq header (or header (gnus-id-to-header id)))
9182       ;; This is an article number.
9183       (setq header (or header (gnus-summary-article-header id))))
9184     (if (and header
9185              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9186         ;; We have found the header.
9187         header
9188       ;; If this is a sparse article, we have to nix out its
9189       ;; previous entry in the thread hashtb.
9190       (when (and header
9191                  (gnus-summary-article-sparse-p (mail-header-number header)))
9192         (let* ((parent (gnus-parent-id (mail-header-references header)))
9193                (thread (and parent (gnus-id-to-thread parent))))
9194           (when thread
9195             (delq (assq header thread) thread))))
9196       ;; We have to really fetch the header to this article.
9197       (save-excursion
9198         (set-buffer nntp-server-buffer)
9199         (when (setq where (gnus-request-head id group))
9200           (nnheader-fold-continuation-lines)
9201           (goto-char (point-max))
9202           (insert ".\n")
9203           (goto-char (point-min))
9204           (insert "211 ")
9205           (princ (cond
9206                   ((numberp id) id)
9207                   ((cdr where) (cdr where))
9208                   (header (mail-header-number header))
9209                   (t gnus-reffed-article-number))
9210                  (current-buffer))
9211           (insert " Article retrieved.\n"))
9212         (if (or (not where)
9213                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9214             ()                          ; Malformed head.
9215           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9216             (when (and (stringp id)
9217                        (not (string= (gnus-group-real-name group)
9218                                      (car where))))
9219               ;; If we fetched by Message-ID and the article came
9220               ;; from a different group, we fudge some bogus article
9221               ;; numbers for this article.
9222               (mail-header-set-number header gnus-reffed-article-number))
9223             (save-excursion
9224               (set-buffer gnus-summary-buffer)
9225               (decf gnus-reffed-article-number)
9226               (gnus-remove-header (mail-header-number header))
9227               (push header gnus-newsgroup-headers)
9228               (setq gnus-current-headers header)
9229               (push (mail-header-number header) gnus-newsgroup-limit)))
9230           header)))))
9231
9232 (defun gnus-remove-header (number)
9233   "Remove header NUMBER from `gnus-newsgroup-headers'."
9234   (if (and gnus-newsgroup-headers
9235            (= number (mail-header-number (car gnus-newsgroup-headers))))
9236       (pop gnus-newsgroup-headers)
9237     (let ((headers gnus-newsgroup-headers))
9238       (while (and (cdr headers)
9239                   (not (= number (mail-header-number (cadr headers)))))
9240         (pop headers))
9241       (when (cdr headers)
9242         (setcdr headers (cddr headers))))))
9243
9244 ;;;
9245 ;;; summary highlights
9246 ;;;
9247
9248 (defun gnus-highlight-selected-summary ()
9249   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9250   ;; Highlight selected article in summary buffer
9251   (when gnus-summary-selected-face
9252     (save-excursion
9253       (let* ((beg (progn (beginning-of-line) (point)))
9254              (end (progn (end-of-line) (point)))
9255              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9256              (from (if (get-text-property beg gnus-mouse-face-prop)
9257                        beg
9258                      (or (next-single-property-change
9259                           beg gnus-mouse-face-prop nil end)
9260                          beg)))
9261              (to
9262               (if (= from end)
9263                   (- from 2)
9264                 (or (next-single-property-change
9265                      from gnus-mouse-face-prop nil end)
9266                     end))))
9267         ;; If no mouse-face prop on line we will have to = from = end,
9268         ;; so we highlight the entire line instead.
9269         (when (= (+ to 2) from)
9270           (setq from beg)
9271           (setq to end))
9272         (if gnus-newsgroup-selected-overlay
9273             ;; Move old overlay.
9274             (gnus-move-overlay
9275              gnus-newsgroup-selected-overlay from to (current-buffer))
9276           ;; Create new overlay.
9277           (gnus-overlay-put
9278            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9279            'face gnus-summary-selected-face))))))
9280
9281 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9282 (defun gnus-summary-highlight-line ()
9283   "Highlight current line according to `gnus-summary-highlight'."
9284   (let* ((list gnus-summary-highlight)
9285          (p (point))
9286          (end (progn (end-of-line) (point)))
9287          ;; now find out where the line starts and leave point there.
9288          (beg (progn (beginning-of-line) (point)))
9289          (article (gnus-summary-article-number))
9290          (score (or (cdr (assq (or article gnus-current-article)
9291                                gnus-newsgroup-scored))
9292                     gnus-summary-default-score 0))
9293          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9294          (inhibit-read-only t))
9295     ;; Eval the cars of the lists until we find a match.
9296     (let ((default gnus-summary-default-score))
9297       (while (and list
9298                   (not (eval (caar list))))
9299         (setq list (cdr list))))
9300     (let ((face (cdar list)))
9301       (unless (eq face (get-text-property beg 'face))
9302         (gnus-put-text-property-excluding-characters-with-faces
9303          beg end 'face
9304          (setq face (if (boundp face) (symbol-value face) face)))
9305         (when gnus-summary-highlight-line-function
9306           (funcall gnus-summary-highlight-line-function article face))))
9307     (goto-char p)))
9308
9309 (defun gnus-update-read-articles (group unread &optional compute)
9310   "Update the list of read articles in GROUP."
9311   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9312          (entry (gnus-gethash group gnus-newsrc-hashtb))
9313          (info (nth 2 entry))
9314          (prev 1)
9315          (unread (sort (copy-sequence unread) '<))
9316          read)
9317     (if (or (not info) (not active))
9318         ;; There is no info on this group if it was, in fact,
9319         ;; killed.  Gnus stores no information on killed groups, so
9320         ;; there's nothing to be done.
9321         ;; One could store the information somewhere temporarily,
9322         ;; perhaps...  Hmmm...
9323         ()
9324       ;; Remove any negative articles numbers.
9325       (while (and unread (< (car unread) 0))
9326         (setq unread (cdr unread)))
9327       ;; Remove any expired article numbers
9328       (while (and unread (< (car unread) (car active)))
9329         (setq unread (cdr unread)))
9330       ;; Compute the ranges of read articles by looking at the list of
9331       ;; unread articles.
9332       (while unread
9333         (when (/= (car unread) prev)
9334           (push (if (= prev (1- (car unread))) prev
9335                   (cons prev (1- (car unread))))
9336                 read))
9337         (setq prev (1+ (car unread)))
9338         (setq unread (cdr unread)))
9339       (when (<= prev (cdr active))
9340         (push (cons prev (cdr active)) read))
9341       (setq read (if (> (length read) 1) (nreverse read) read))
9342       (if compute
9343           read
9344         (save-excursion
9345           (set-buffer gnus-group-buffer)
9346           (gnus-undo-register
9347             `(progn
9348                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9349                (gnus-info-set-read ',info ',(gnus-info-read info))
9350                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9351                (gnus-group-update-group ,group t))))
9352         ;; Propagate the read marks to the backend.
9353         (if (gnus-check-backend-function 'request-set-mark group)
9354             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9355                   (add (gnus-remove-from-range read (gnus-info-read info))))
9356               (when (or add del)
9357                 (unless (gnus-check-group group)
9358                   (error "Can't open server for %s" group))
9359                 (gnus-request-set-mark
9360                  group (delq nil (list (if add (list add 'add '(read)))
9361                                        (if del (list del 'del '(read)))))))))
9362         ;; Enter this list into the group info.
9363         (gnus-info-set-read info read)
9364         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9365         (gnus-get-unread-articles-in-group info (gnus-active group))
9366         t))))
9367
9368 (defun gnus-offer-save-summaries ()
9369   "Offer to save all active summary buffers."
9370   (save-excursion
9371     (let ((buflist (buffer-list))
9372           buffers bufname)
9373       ;; Go through all buffers and find all summaries.
9374       (while buflist
9375         (and (setq bufname (buffer-name (car buflist)))
9376              (string-match "Summary" bufname)
9377              (save-excursion
9378                (set-buffer bufname)
9379                ;; We check that this is, indeed, a summary buffer.
9380                (and (eq major-mode 'gnus-summary-mode)
9381                     ;; Also make sure this isn't bogus.
9382                     gnus-newsgroup-prepared
9383                     ;; Also make sure that this isn't a dead summary buffer.
9384                     (not gnus-dead-summary-mode)))
9385              (push bufname buffers))
9386         (setq buflist (cdr buflist)))
9387       ;; Go through all these summary buffers and offer to save them.
9388       (when buffers
9389         (map-y-or-n-p
9390          "Update summary buffer %s? "
9391          (lambda (buf)
9392            (switch-to-buffer buf)
9393            (gnus-summary-exit))
9394          buffers)))))
9395
9396 (defun gnus-summary-setup-default-charset ()
9397   "Setup newsgroup default charset."
9398   (if (equal gnus-newsgroup-name "nndraft:drafts")
9399       (setq gnus-newsgroup-charset nil)
9400     (let* ((name (and gnus-newsgroup-name
9401                       (gnus-group-real-name gnus-newsgroup-name)))
9402            (ignored-charsets 
9403             (or gnus-newsgroup-ephemeral-ignored-charsets
9404                 (append
9405                  (and gnus-newsgroup-name
9406                       (or (gnus-group-find-parameter gnus-newsgroup-name
9407                                                      'ignored-charsets t)
9408                           (let ((alist gnus-group-ignored-charsets-alist)
9409                                 elem (charsets nil))
9410                             (while (setq elem (pop alist))
9411                               (when (and name
9412                                          (string-match (car elem) name))
9413                                 (setq alist nil
9414                                       charsets (cdr elem))))
9415                             charsets)))
9416                  gnus-newsgroup-ignored-charsets))))
9417       (setq gnus-newsgroup-charset
9418             (or gnus-newsgroup-ephemeral-charset
9419                 (and gnus-newsgroup-name
9420                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9421                          (let ((alist gnus-group-charset-alist)
9422                                elem charset)
9423                            (while (setq elem (pop alist))
9424                              (when (and name
9425                                         (string-match (car elem) name))
9426                                (setq alist nil
9427                                      charset (cadr elem))))
9428                            charset)))
9429                 gnus-default-charset))
9430       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9431            ignored-charsets))))
9432
9433 ;;;
9434 ;;; Mime Commands
9435 ;;;
9436
9437 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9438   "Display the current article buffer fully MIME-buttonized.
9439 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9440 treated as multipart/mixed."
9441   (interactive "P")
9442   (require 'gnus-art)
9443   (let ((gnus-unbuttonized-mime-types nil)
9444         (gnus-mime-display-multipart-as-mixed show-all-parts))
9445     (gnus-summary-show-article)))
9446
9447 (defun gnus-summary-repair-multipart (article)
9448   "Add a Content-Type header to a multipart article without one."
9449   (interactive (list (gnus-summary-article-number)))
9450   (gnus-with-article article
9451     (message-narrow-to-head)
9452     (goto-char (point-max))
9453     (widen)
9454     (when (search-forward "\n--" nil t)
9455       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9456         (message-narrow-to-head)
9457         (message-remove-header "Mime-Version")
9458         (message-remove-header "Content-Type")
9459         (goto-char (point-max))
9460         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9461                         separator))
9462         (insert "Mime-Version: 1.0\n")
9463         (widen))))
9464   (let (gnus-mark-article-hook)
9465     (gnus-summary-select-article t t nil article)))
9466
9467 (defun gnus-summary-toggle-display-buttonized ()
9468   "Toggle the buttonizing of the article buffer."
9469   (interactive)
9470   (require 'gnus-art)
9471   (if (setq gnus-inhibit-mime-unbuttonizing
9472             (not gnus-inhibit-mime-unbuttonizing))
9473       (let ((gnus-unbuttonized-mime-types nil))
9474         (gnus-summary-show-article))
9475     (gnus-summary-show-article)))
9476
9477 ;;;
9478 ;;; with article
9479 ;;;
9480
9481 (defmacro gnus-with-article (article &rest forms)
9482   "Select ARTICLE and perform FORMS in the original article buffer.
9483 Then replace the article with the result."
9484   `(progn
9485      ;; We don't want the article to be marked as read.
9486      (let (gnus-mark-article-hook)
9487        (gnus-summary-select-article t t nil ,article))
9488      (set-buffer gnus-original-article-buffer)
9489      ,@forms
9490      (if (not (gnus-check-backend-function
9491                'request-replace-article (car gnus-article-current)))
9492          (gnus-message 5 "Read-only group; not replacing")
9493        (unless (gnus-request-replace-article
9494                 ,article (car gnus-article-current)
9495                 (current-buffer) t)
9496          (error "Couldn't replace article")))
9497      ;; The cache and backlog have to be flushed somewhat.
9498      (when gnus-keep-backlog
9499        (gnus-backlog-remove-article
9500         (car gnus-article-current) (cdr gnus-article-current)))
9501      (when gnus-use-cache
9502        (gnus-cache-update-article
9503         (car gnus-article-current) (cdr gnus-article-current)))))
9504
9505 (put 'gnus-with-article 'lisp-indent-function 1)
9506 (put 'gnus-with-article 'edebug-form-spec '(form body))
9507
9508 ;;;
9509 ;;; Generic summary marking commands
9510 ;;;
9511
9512 (defvar gnus-summary-marking-alist
9513   '((read gnus-del-mark "d")
9514     (unread gnus-unread-mark "u")
9515     (ticked gnus-ticked-mark "!")
9516     (dormant gnus-dormant-mark "?")
9517     (expirable gnus-expirable-mark "e"))
9518   "An alist of names/marks/keystrokes.")
9519
9520 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9521 (defvar gnus-summary-mark-map)
9522
9523 (defun gnus-summary-make-all-marking-commands ()
9524   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9525   (dolist (elem gnus-summary-marking-alist)
9526     (apply 'gnus-summary-make-marking-command elem)))
9527
9528 (defun gnus-summary-make-marking-command (name mark keystroke)
9529   (let ((map (make-sparse-keymap)))
9530     (define-key gnus-summary-generic-mark-map keystroke map)
9531     (dolist (lway `((next "next" next nil "n")
9532                     (next-unread "next unread" next t "N")
9533                     (prev "previous" prev nil "p")
9534                     (prev-unread "previous unread" prev t "P")
9535                     (nomove "" nil nil ,keystroke)))
9536       (let ((func (gnus-summary-make-marking-command-1
9537                    mark (car lway) lway name)))
9538         (setq func (eval func))
9539         (define-key map (nth 4 lway) func)))))
9540       
9541 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9542   `(defun ,(intern
9543             (format "gnus-summary-put-mark-as-%s%s"
9544                     name (if (eq way 'nomove)
9545                              ""
9546                            (concat "-" (symbol-name way)))))
9547      (n)
9548      ,(format
9549        "Mark the current article as %s%s.
9550 If N, the prefix, then repeat N times.
9551 If N is negative, move in reverse order.
9552 The difference between N and the actual number of articles marked is
9553 returned."
9554        name (cadr lway))
9555      (interactive "p")
9556      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9557     
9558 (defun gnus-summary-generic-mark (n mark move unread)
9559   "Mark N articles with MARK."
9560   (unless (eq major-mode 'gnus-summary-mode)
9561     (error "This command can only be used in the summary buffer"))
9562   (gnus-summary-show-thread)
9563   (let ((nummove
9564          (cond
9565           ((eq move 'next) 1)
9566           ((eq move 'prev) -1)
9567           (t 0))))
9568     (if (zerop nummove)
9569         (setq n 1)
9570       (when (< n 0)
9571         (setq n (abs n)
9572               nummove (* -1 nummove))))
9573     (while (and (> n 0)
9574                 (gnus-summary-mark-article nil mark)
9575                 (zerop (gnus-summary-next-subject nummove unread t)))
9576       (setq n (1- n)))
9577     (when (/= 0 n)
9578       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9579     (gnus-summary-recenter)
9580     (gnus-summary-position-point)
9581     (gnus-set-mode-line 'summary)
9582     n))
9583
9584 (gnus-summary-make-all-marking-commands)
9585
9586 (gnus-ems-redefine)
9587
9588 (provide 'gnus-sum)
9589
9590 (run-hooks 'gnus-sum-load-hook)
9591
9592 ;;; gnus-sum.el ends here