8c85fb3b6b984323b80ca696ff6e32b589359011
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-threading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-group-charset-alist
801   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
802     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
803     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
804     ("^relcom\\>" koi8-r)
805     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
806     ("^israel\\>" iso-8859-1)
807     ("^han\\>" euc-kr)
808     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
809     (".*" iso-8859-1))
810   "Alist of regexps (to match group names) and default charsets to be used when reading."
811   :type '(repeat (list (regexp :tag "Group")
812                        (symbol :tag "Charset")))
813   :group 'gnus-charset)
814
815 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
816   "List of charsets that should be ignored.
817 When these charsets are used in the \"charset\" parameter, the
818 default charset will be used instead."
819   :type '(repeat symbol)
820   :group 'gnus-charset)
821
822 (defcustom gnus-group-highlight-words-alist nil
823   "Alist of group regexps and highlight regexps.
824 This variable uses the same syntax as `gnus-emphasis-alist'."
825   :type '(repeat (cons (regexp :tag "Group")
826                        (repeat (list (regexp :tag "Highlight regexp")
827                                      (number :tag "Group for entire word" 0)
828                                      (number :tag "Group for displayed part" 0)
829                                      (symbol :tag "Face" 
830                                              gnus-emphasis-highlight-words)))))
831   :group 'gnus-summary-visual)
832
833 ;;; Internal variables
834
835 (defvar gnus-article-mime-handles nil)
836 (defvar gnus-article-decoded-p nil)
837 (defvar gnus-scores-exclude-files nil)
838 (defvar gnus-page-broken nil)
839 (defvar gnus-inhibit-mime-unbuttonizing nil)
840
841 (defvar gnus-original-article nil)
842 (defvar gnus-article-internal-prepare-hook nil)
843 (defvar gnus-newsgroup-process-stack nil)
844
845 (defvar gnus-thread-indent-array nil)
846 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
847
848 ;; Avoid highlighting in kill files.
849 (defvar gnus-summary-inhibit-highlight nil)
850 (defvar gnus-newsgroup-selected-overlay nil)
851 (defvar gnus-inhibit-limiting nil)
852 (defvar gnus-newsgroup-adaptive-score-file nil)
853 (defvar gnus-current-score-file nil)
854 (defvar gnus-current-move-group nil)
855 (defvar gnus-current-copy-group nil)
856 (defvar gnus-current-crosspost-group nil)
857
858 (defvar gnus-newsgroup-dependencies nil)
859 (defvar gnus-newsgroup-adaptive nil)
860 (defvar gnus-summary-display-article-function nil)
861 (defvar gnus-summary-highlight-line-function nil
862   "Function called after highlighting a summary line.")
863
864 (defvar gnus-summary-line-format-alist
865   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
866     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
867     (?s gnus-tmp-subject-or-nil ?s)
868     (?n gnus-tmp-name ?s)
869     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
870         ?s)
871     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
872             gnus-tmp-from) ?s)
873     (?F gnus-tmp-from ?s)
874     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
875     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
876     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
877     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
878     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
879     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
880     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
881     (?L gnus-tmp-lines ?d)
882     (?I gnus-tmp-indentation ?s)
883     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
884     (?R gnus-tmp-replied ?c)
885     (?\[ gnus-tmp-opening-bracket ?c)
886     (?\] gnus-tmp-closing-bracket ?c)
887     (?\> (make-string gnus-tmp-level ? ) ?s)
888     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
889     (?i gnus-tmp-score ?d)
890     (?z gnus-tmp-score-char ?c)
891     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
892     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
893     (?U gnus-tmp-unread ?c)
894     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
895     (?t (gnus-summary-number-of-articles-in-thread
896          (and (boundp 'thread) (car thread)) gnus-tmp-level)
897         ?d)
898     (?e (gnus-summary-number-of-articles-in-thread
899          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
900         ?c)
901     (?u gnus-tmp-user-defined ?s)
902     (?P (gnus-pick-line-number) ?d))
903   "An alist of format specifications that can appear in summary lines,
904 and what variables they correspond with, along with the type of the
905 variable (string, integer, character, etc).")
906
907 (defvar gnus-summary-dummy-line-format-alist
908   `((?S gnus-tmp-subject ?s)
909     (?N gnus-tmp-number ?d)
910     (?u gnus-tmp-user-defined ?s)))
911
912 (defvar gnus-summary-mode-line-format-alist
913   `((?G gnus-tmp-group-name ?s)
914     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
915     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
916     (?A gnus-tmp-article-number ?d)
917     (?Z gnus-tmp-unread-and-unselected ?s)
918     (?V gnus-version ?s)
919     (?U gnus-tmp-unread-and-unticked ?d)
920     (?S gnus-tmp-subject ?s)
921     (?e gnus-tmp-unselected ?d)
922     (?u gnus-tmp-user-defined ?s)
923     (?d (length gnus-newsgroup-dormant) ?d)
924     (?t (length gnus-newsgroup-marked) ?d)
925     (?r (length gnus-newsgroup-reads) ?d)
926     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
927     (?E gnus-newsgroup-expunged-tally ?d)
928     (?s (gnus-current-score-file-nondirectory) ?s)))
929
930 (defvar gnus-last-search-regexp nil
931   "Default regexp for article search command.")
932
933 (defvar gnus-last-shell-command nil
934   "Default shell command on article.")
935
936 (defvar gnus-newsgroup-begin nil)
937 (defvar gnus-newsgroup-end nil)
938 (defvar gnus-newsgroup-last-rmail nil)
939 (defvar gnus-newsgroup-last-mail nil)
940 (defvar gnus-newsgroup-last-folder nil)
941 (defvar gnus-newsgroup-last-file nil)
942 (defvar gnus-newsgroup-auto-expire nil)
943 (defvar gnus-newsgroup-active nil)
944
945 (defvar gnus-newsgroup-data nil)
946 (defvar gnus-newsgroup-data-reverse nil)
947 (defvar gnus-newsgroup-limit nil)
948 (defvar gnus-newsgroup-limits nil)
949
950 (defvar gnus-newsgroup-unreads nil
951   "List of unread articles in the current newsgroup.")
952
953 (defvar gnus-newsgroup-unselected nil
954   "List of unselected unread articles in the current newsgroup.")
955
956 (defvar gnus-newsgroup-reads nil
957   "Alist of read articles and article marks in the current newsgroup.")
958
959 (defvar gnus-newsgroup-expunged-tally nil)
960
961 (defvar gnus-newsgroup-marked nil
962   "List of ticked articles in the current newsgroup (a subset of unread art).")
963
964 (defvar gnus-newsgroup-killed nil
965   "List of ranges of articles that have been through the scoring process.")
966
967 (defvar gnus-newsgroup-cached nil
968   "List of articles that come from the article cache.")
969
970 (defvar gnus-newsgroup-saved nil
971   "List of articles that have been saved.")
972
973 (defvar gnus-newsgroup-kill-headers nil)
974
975 (defvar gnus-newsgroup-replied nil
976   "List of articles that have been replied to in the current newsgroup.")
977
978 (defvar gnus-newsgroup-expirable nil
979   "List of articles in the current newsgroup that can be expired.")
980
981 (defvar gnus-newsgroup-processable nil
982   "List of articles in the current newsgroup that can be processed.")
983
984 (defvar gnus-newsgroup-downloadable nil
985   "List of articles in the current newsgroup that can be processed.")
986
987 (defvar gnus-newsgroup-undownloaded nil
988   "List of articles in the current newsgroup that haven't been downloaded..")
989
990 (defvar gnus-newsgroup-unsendable nil
991   "List of articles in the current newsgroup that won't be sent.")
992
993 (defvar gnus-newsgroup-bookmarks nil
994   "List of articles in the current newsgroup that have bookmarks.")
995
996 (defvar gnus-newsgroup-dormant nil
997   "List of dormant articles in the current newsgroup.")
998
999 (defvar gnus-newsgroup-scored nil
1000   "List of scored articles in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-headers nil
1003   "List of article headers in the current newsgroup.")
1004
1005 (defvar gnus-newsgroup-threads nil)
1006
1007 (defvar gnus-newsgroup-prepared nil
1008   "Whether the current group has been prepared properly.")
1009
1010 (defvar gnus-newsgroup-ancient nil
1011   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1012
1013 (defvar gnus-newsgroup-sparse nil)
1014
1015 (defvar gnus-current-article nil)
1016 (defvar gnus-article-current nil)
1017 (defvar gnus-current-headers nil)
1018 (defvar gnus-have-all-headers nil)
1019 (defvar gnus-last-article nil)
1020 (defvar gnus-newsgroup-history nil)
1021 (defvar gnus-newsgroup-charset nil)
1022
1023 (defconst gnus-summary-local-variables
1024   '(gnus-newsgroup-name
1025     gnus-newsgroup-begin gnus-newsgroup-end
1026     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1027     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1028     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1029     gnus-newsgroup-unselected gnus-newsgroup-marked
1030     gnus-newsgroup-reads gnus-newsgroup-saved
1031     gnus-newsgroup-replied gnus-newsgroup-expirable
1032     gnus-newsgroup-processable gnus-newsgroup-killed
1033     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1034     gnus-newsgroup-unsendable
1035     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1036     gnus-newsgroup-headers gnus-newsgroup-threads
1037     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1038     gnus-current-article gnus-current-headers gnus-have-all-headers
1039     gnus-last-article gnus-article-internal-prepare-hook
1040     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1041     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1042     gnus-thread-expunge-below
1043     gnus-score-alist gnus-current-score-file
1044     (gnus-summary-expunge-below . global)
1045     (gnus-summary-mark-below . global)
1046     gnus-newsgroup-active gnus-scores-exclude-files
1047     gnus-newsgroup-history gnus-newsgroup-ancient
1048     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1049     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1050     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1051     (gnus-newsgroup-expunged-tally . 0)
1052     gnus-cache-removable-articles gnus-newsgroup-cached
1053     gnus-newsgroup-data gnus-newsgroup-data-reverse
1054     gnus-newsgroup-limit gnus-newsgroup-limits
1055     gnus-newsgroup-charset)
1056   "Variables that are buffer-local to the summary buffers.")
1057
1058 ;; Byte-compiler warning.
1059 (defvar gnus-article-mode-map)
1060
1061 ;; MIME stuff.
1062
1063 (defvar gnus-decode-encoded-word-methods
1064   '(mail-decode-encoded-word-string)
1065   "List of methods used to decode encoded words.
1066
1067 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1068 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1069 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1070 whose names match REGEXP.
1071
1072 For example:
1073 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1074  mail-decode-encoded-word-string
1075  (\"chinese\" . rfc1843-decode-string))
1076 ")
1077
1078 (defvar gnus-decode-encoded-word-methods-cache nil)
1079
1080 (defun gnus-multi-decode-encoded-word-string (string)
1081   "Apply the functions from `gnus-encoded-word-methods' that match."
1082   (unless (and gnus-decode-encoded-word-methods-cache
1083                (eq gnus-newsgroup-name
1084                    (car gnus-decode-encoded-word-methods-cache)))
1085     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1086     (mapc '(lambda (x)
1087              (if (symbolp x)
1088                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1089                (if (and gnus-newsgroup-name
1090                         (string-match (car x) gnus-newsgroup-name))
1091                    (nconc gnus-decode-encoded-word-methods-cache
1092                           (list (cdr x))))))
1093           gnus-decode-encoded-word-methods))
1094   (let ((xlist gnus-decode-encoded-word-methods-cache))
1095     (pop xlist)
1096     (while xlist
1097       (setq string (funcall (pop xlist) string))))
1098   string)
1099
1100 ;; Subject simplification.
1101
1102 (defun gnus-simplify-whitespace (str)
1103   "Remove excessive whitespace."
1104   (let ((mystr str))
1105     ;; Multiple spaces.
1106     (while (string-match "[ \t][ \t]+" mystr)
1107       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1108                           " "
1109                           (substring mystr (match-end 0)))))
1110     ;; Leading spaces.
1111     (when (string-match "^[ \t]+" mystr)
1112       (setq mystr (substring mystr (match-end 0))))
1113     ;; Trailing spaces.
1114     (when (string-match "[ \t]+$" mystr)
1115       (setq mystr (substring mystr 0 (match-beginning 0))))
1116     mystr))
1117
1118 (defsubst gnus-simplify-subject-re (subject)
1119   "Remove \"Re:\" from subject lines."
1120   (if (string-match "^[Rr][Ee]: *" subject)
1121       (substring subject (match-end 0))
1122     subject))
1123
1124 (defun gnus-simplify-subject (subject &optional re-only)
1125   "Remove `Re:' and words in parentheses.
1126 If RE-ONLY is non-nil, strip leading `Re:'s only."
1127   (let ((case-fold-search t))           ;Ignore case.
1128     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1129     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1130       (setq subject (substring subject (match-end 0))))
1131     ;; Remove uninteresting prefixes.
1132     (when (and (not re-only)
1133                gnus-simplify-ignored-prefixes
1134                (string-match gnus-simplify-ignored-prefixes subject))
1135       (setq subject (substring subject (match-end 0))))
1136     ;; Remove words in parentheses from end.
1137     (unless re-only
1138       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1139         (setq subject (substring subject 0 (match-beginning 0)))))
1140     ;; Return subject string.
1141     subject))
1142
1143 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1144 ;; all whitespace.
1145 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1146   (goto-char (point-min))
1147   (while (re-search-forward regexp nil t)
1148       (replace-match (or newtext ""))))
1149
1150 (defun gnus-simplify-buffer-fuzzy ()
1151   "Simplify string in the buffer fuzzily.
1152 The string in the accessible portion of the current buffer is simplified.
1153 It is assumed to be a single-line subject.
1154 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1155 matter is removed.  Additional things can be deleted by setting
1156 gnus-simplify-subject-fuzzy-regexp."
1157   (let ((case-fold-search t)
1158         (modified-tick))
1159     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1160
1161     (while (not (eq modified-tick (buffer-modified-tick)))
1162       (setq modified-tick (buffer-modified-tick))
1163       (cond
1164        ((listp gnus-simplify-subject-fuzzy-regexp)
1165         (mapcar 'gnus-simplify-buffer-fuzzy-step
1166                 gnus-simplify-subject-fuzzy-regexp))
1167        (gnus-simplify-subject-fuzzy-regexp
1168         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1169       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1170       (gnus-simplify-buffer-fuzzy-step
1171        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1172       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1173
1174     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1175     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1176     (gnus-simplify-buffer-fuzzy-step " $")
1177     (gnus-simplify-buffer-fuzzy-step "^ +")))
1178
1179 (defun gnus-simplify-subject-fuzzy (subject)
1180   "Simplify a subject string fuzzily.
1181 See `gnus-simplify-buffer-fuzzy' for details."
1182   (save-excursion
1183     (gnus-set-work-buffer)
1184     (let ((case-fold-search t))
1185       ;; Remove uninteresting prefixes.
1186       (when (and gnus-simplify-ignored-prefixes
1187                  (string-match gnus-simplify-ignored-prefixes subject))
1188         (setq subject (substring subject (match-end 0))))
1189       (insert subject)
1190       (inline (gnus-simplify-buffer-fuzzy))
1191       (buffer-string))))
1192
1193 (defsubst gnus-simplify-subject-fully (subject)
1194   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1195   (cond
1196    (gnus-simplify-subject-functions
1197     (gnus-map-function gnus-simplify-subject-functions subject))
1198    ((null gnus-summary-gather-subject-limit)
1199     (gnus-simplify-subject-re subject))
1200    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1201     (gnus-simplify-subject-fuzzy subject))
1202    ((numberp gnus-summary-gather-subject-limit)
1203     (gnus-limit-string (gnus-simplify-subject-re subject)
1204                        gnus-summary-gather-subject-limit))
1205    (t
1206     subject)))
1207
1208 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1209   "Check whether two subjects are equal.
1210 If optional argument simple-first is t, first argument is already
1211 simplified."
1212   (cond
1213    ((null simple-first)
1214     (equal (gnus-simplify-subject-fully s1)
1215            (gnus-simplify-subject-fully s2)))
1216    (t
1217     (equal s1
1218            (gnus-simplify-subject-fully s2)))))
1219
1220 (defun gnus-summary-bubble-group ()
1221   "Increase the score of the current group.
1222 This is a handy function to add to `gnus-summary-exit-hook' to
1223 increase the score of each group you read."
1224   (gnus-group-add-score gnus-newsgroup-name))
1225
1226 \f
1227 ;;;
1228 ;;; Gnus summary mode
1229 ;;;
1230
1231 (put 'gnus-summary-mode 'mode-class 'special)
1232
1233 (when t
1234   ;; Non-orthogonal keys
1235
1236   (gnus-define-keys gnus-summary-mode-map
1237     " " gnus-summary-next-page
1238     "\177" gnus-summary-prev-page
1239     [delete] gnus-summary-prev-page
1240     [backspace] gnus-summary-prev-page
1241     "\r" gnus-summary-scroll-up
1242     "\M-\r" gnus-summary-scroll-down
1243     "n" gnus-summary-next-unread-article
1244     "p" gnus-summary-prev-unread-article
1245     "N" gnus-summary-next-article
1246     "P" gnus-summary-prev-article
1247     "\M-\C-n" gnus-summary-next-same-subject
1248     "\M-\C-p" gnus-summary-prev-same-subject
1249     "\M-n" gnus-summary-next-unread-subject
1250     "\M-p" gnus-summary-prev-unread-subject
1251     "." gnus-summary-first-unread-article
1252     "," gnus-summary-best-unread-article
1253     "\M-s" gnus-summary-search-article-forward
1254     "\M-r" gnus-summary-search-article-backward
1255     "<" gnus-summary-beginning-of-article
1256     ">" gnus-summary-end-of-article
1257     "j" gnus-summary-goto-article
1258     "^" gnus-summary-refer-parent-article
1259     "\M-^" gnus-summary-refer-article
1260     "u" gnus-summary-tick-article-forward
1261     "!" gnus-summary-tick-article-forward
1262     "U" gnus-summary-tick-article-backward
1263     "d" gnus-summary-mark-as-read-forward
1264     "D" gnus-summary-mark-as-read-backward
1265     "E" gnus-summary-mark-as-expirable
1266     "\M-u" gnus-summary-clear-mark-forward
1267     "\M-U" gnus-summary-clear-mark-backward
1268     "k" gnus-summary-kill-same-subject-and-select
1269     "\C-k" gnus-summary-kill-same-subject
1270     "\M-\C-k" gnus-summary-kill-thread
1271     "\M-\C-l" gnus-summary-lower-thread
1272     "e" gnus-summary-edit-article
1273     "#" gnus-summary-mark-as-processable
1274     "\M-#" gnus-summary-unmark-as-processable
1275     "\M-\C-t" gnus-summary-toggle-threads
1276     "\M-\C-s" gnus-summary-show-thread
1277     "\M-\C-h" gnus-summary-hide-thread
1278     "\M-\C-f" gnus-summary-next-thread
1279     "\M-\C-b" gnus-summary-prev-thread
1280     "\M-\C-u" gnus-summary-up-thread
1281     "\M-\C-d" gnus-summary-down-thread
1282     "&" gnus-summary-execute-command
1283     "c" gnus-summary-catchup-and-exit
1284     "\C-w" gnus-summary-mark-region-as-read
1285     "\C-t" gnus-summary-toggle-truncation
1286     "?" gnus-summary-mark-as-dormant
1287     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1288     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1289     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1290     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1291     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1292     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1293     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1294     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1295     "=" gnus-summary-expand-window
1296     "\C-x\C-s" gnus-summary-reselect-current-group
1297     "\M-g" gnus-summary-rescan-group
1298     "w" gnus-summary-stop-page-breaking
1299     "\C-c\C-r" gnus-summary-caesar-message
1300     "f" gnus-summary-followup
1301     "F" gnus-summary-followup-with-original
1302     "C" gnus-summary-cancel-article
1303     "r" gnus-summary-reply
1304     "R" gnus-summary-reply-with-original
1305     "\C-c\C-f" gnus-summary-mail-forward
1306     "o" gnus-summary-save-article
1307     "\C-o" gnus-summary-save-article-mail
1308     "|" gnus-summary-pipe-output
1309     "\M-k" gnus-summary-edit-local-kill
1310     "\M-K" gnus-summary-edit-global-kill
1311     ;; "V" gnus-version
1312     "\C-c\C-d" gnus-summary-describe-group
1313     "q" gnus-summary-exit
1314     "Q" gnus-summary-exit-no-update
1315     "\C-c\C-i" gnus-info-find-node
1316     gnus-mouse-2 gnus-mouse-pick-article
1317     "m" gnus-summary-mail-other-window
1318     "a" gnus-summary-post-news
1319     "x" gnus-summary-limit-to-unread
1320     "s" gnus-summary-isearch-article
1321     "t" gnus-summary-toggle-header
1322     "g" gnus-summary-show-article
1323     "l" gnus-summary-goto-last-article
1324     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1325     "\C-d" gnus-summary-enter-digest-group
1326     "\M-\C-d" gnus-summary-read-document
1327     "\M-\C-e" gnus-summary-edit-parameters
1328     "\M-\C-g" gnus-summary-customize-parameters
1329     "\C-c\C-b" gnus-bug
1330     "*" gnus-cache-enter-article
1331     "\M-*" gnus-cache-remove-article
1332     "\M-&" gnus-summary-universal-argument
1333     "\C-l" gnus-recenter
1334     "I" gnus-summary-increase-score
1335     "L" gnus-summary-lower-score
1336     "\M-i" gnus-symbolic-argument
1337     "h" gnus-summary-select-article-buffer
1338
1339     "b" gnus-article-view-part
1340     "\M-t" gnus-summary-toggle-display-buttonized
1341
1342     "V" gnus-summary-score-map
1343     "X" gnus-uu-extract-map
1344     "S" gnus-summary-send-map)
1345
1346   ;; Sort of orthogonal keymap
1347   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1348     "t" gnus-summary-tick-article-forward
1349     "!" gnus-summary-tick-article-forward
1350     "d" gnus-summary-mark-as-read-forward
1351     "r" gnus-summary-mark-as-read-forward
1352     "c" gnus-summary-clear-mark-forward
1353     " " gnus-summary-clear-mark-forward
1354     "e" gnus-summary-mark-as-expirable
1355     "x" gnus-summary-mark-as-expirable
1356     "?" gnus-summary-mark-as-dormant
1357     "b" gnus-summary-set-bookmark
1358     "B" gnus-summary-remove-bookmark
1359     "#" gnus-summary-mark-as-processable
1360     "\M-#" gnus-summary-unmark-as-processable
1361     "S" gnus-summary-limit-include-expunged
1362     "C" gnus-summary-catchup
1363     "H" gnus-summary-catchup-to-here
1364     "\C-c" gnus-summary-catchup-all
1365     "k" gnus-summary-kill-same-subject-and-select
1366     "K" gnus-summary-kill-same-subject
1367     "P" gnus-uu-mark-map)
1368
1369   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1370     "c" gnus-summary-clear-above
1371     "u" gnus-summary-tick-above
1372     "m" gnus-summary-mark-above
1373     "k" gnus-summary-kill-below)
1374
1375   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1376     "/" gnus-summary-limit-to-subject
1377     "n" gnus-summary-limit-to-articles
1378     "w" gnus-summary-pop-limit
1379     "s" gnus-summary-limit-to-subject
1380     "a" gnus-summary-limit-to-author
1381     "u" gnus-summary-limit-to-unread
1382     "m" gnus-summary-limit-to-marks
1383     "M" gnus-summary-limit-exclude-marks
1384     "v" gnus-summary-limit-to-score
1385     "*" gnus-summary-limit-include-cached
1386     "D" gnus-summary-limit-include-dormant
1387     "T" gnus-summary-limit-include-thread
1388     "d" gnus-summary-limit-exclude-dormant
1389     "t" gnus-summary-limit-to-age
1390     "x" gnus-summary-limit-to-extra 
1391     "E" gnus-summary-limit-include-expunged
1392     "c" gnus-summary-limit-exclude-childless-dormant
1393     "C" gnus-summary-limit-mark-excluded-as-read)
1394
1395   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1396     "n" gnus-summary-next-unread-article
1397     "p" gnus-summary-prev-unread-article
1398     "N" gnus-summary-next-article
1399     "P" gnus-summary-prev-article
1400     "\C-n" gnus-summary-next-same-subject
1401     "\C-p" gnus-summary-prev-same-subject
1402     "\M-n" gnus-summary-next-unread-subject
1403     "\M-p" gnus-summary-prev-unread-subject
1404     "f" gnus-summary-first-unread-article
1405     "b" gnus-summary-best-unread-article
1406     "j" gnus-summary-goto-article
1407     "g" gnus-summary-goto-subject
1408     "l" gnus-summary-goto-last-article
1409     "o" gnus-summary-pop-article)
1410
1411   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1412     "k" gnus-summary-kill-thread
1413     "l" gnus-summary-lower-thread
1414     "i" gnus-summary-raise-thread
1415     "T" gnus-summary-toggle-threads
1416     "t" gnus-summary-rethread-current
1417     "^" gnus-summary-reparent-thread
1418     "s" gnus-summary-show-thread
1419     "S" gnus-summary-show-all-threads
1420     "h" gnus-summary-hide-thread
1421     "H" gnus-summary-hide-all-threads
1422     "n" gnus-summary-next-thread
1423     "p" gnus-summary-prev-thread
1424     "u" gnus-summary-up-thread
1425     "o" gnus-summary-top-thread
1426     "d" gnus-summary-down-thread
1427     "#" gnus-uu-mark-thread
1428     "\M-#" gnus-uu-unmark-thread)
1429
1430   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1431     "g" gnus-summary-prepare
1432     "c" gnus-summary-insert-cached-articles)
1433
1434   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1435     "c" gnus-summary-catchup-and-exit
1436     "C" gnus-summary-catchup-all-and-exit
1437     "E" gnus-summary-exit-no-update
1438     "Q" gnus-summary-exit
1439     "Z" gnus-summary-exit
1440     "n" gnus-summary-catchup-and-goto-next-group
1441     "R" gnus-summary-reselect-current-group
1442     "G" gnus-summary-rescan-group
1443     "N" gnus-summary-next-group
1444     "s" gnus-summary-save-newsrc
1445     "P" gnus-summary-prev-group)
1446
1447   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1448     " " gnus-summary-next-page
1449     "n" gnus-summary-next-page
1450     "\177" gnus-summary-prev-page
1451     [delete] gnus-summary-prev-page
1452     "p" gnus-summary-prev-page
1453     "\r" gnus-summary-scroll-up
1454     "\M-\r" gnus-summary-scroll-down
1455     "<" gnus-summary-beginning-of-article
1456     ">" gnus-summary-end-of-article
1457     "b" gnus-summary-beginning-of-article
1458     "e" gnus-summary-end-of-article
1459     "^" gnus-summary-refer-parent-article
1460     "r" gnus-summary-refer-parent-article
1461     "D" gnus-summary-enter-digest-group
1462     "R" gnus-summary-refer-references
1463     "T" gnus-summary-refer-thread
1464     "g" gnus-summary-show-article
1465     "s" gnus-summary-isearch-article
1466     "P" gnus-summary-print-article
1467     "t" gnus-article-babel)
1468
1469   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1470     "b" gnus-article-add-buttons
1471     "B" gnus-article-add-buttons-to-head
1472     "o" gnus-article-treat-overstrike
1473     "e" gnus-article-emphasize
1474     "w" gnus-article-fill-cited-article
1475     "Q" gnus-article-fill-long-lines
1476     "C" gnus-article-capitalize-sentences
1477     "c" gnus-article-remove-cr
1478     "q" gnus-article-de-quoted-unreadable
1479     "f" gnus-article-display-x-face
1480     "l" gnus-summary-stop-page-breaking
1481     "r" gnus-summary-caesar-message
1482     "t" gnus-article-hide-headers
1483     "v" gnus-summary-verbose-headers
1484     "h" gnus-article-treat-html
1485     "H" gnus-article-strip-headers-in-body
1486     "d" gnus-article-treat-dumbquotes)
1487
1488   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1489     "a" gnus-article-hide
1490     "h" gnus-article-hide-headers
1491     "b" gnus-article-hide-boring-headers
1492     "s" gnus-article-hide-signature
1493     "c" gnus-article-hide-citation
1494     "C" gnus-article-hide-citation-in-followups
1495     "p" gnus-article-hide-pgp
1496     "B" gnus-article-strip-banner
1497     "P" gnus-article-hide-pem
1498     "\C-c" gnus-article-hide-citation-maybe)
1499
1500   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1501     "a" gnus-article-highlight
1502     "h" gnus-article-highlight-headers
1503     "c" gnus-article-highlight-citation
1504     "s" gnus-article-highlight-signature)
1505
1506   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1507     "w" gnus-article-decode-mime-words
1508     "c" gnus-article-decode-charset
1509     "v" gnus-mime-view-all-parts
1510     "b" gnus-article-view-part)
1511
1512   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1513     "z" gnus-article-date-ut
1514     "u" gnus-article-date-ut
1515     "l" gnus-article-date-local
1516     "e" gnus-article-date-lapsed
1517     "o" gnus-article-date-original
1518     "i" gnus-article-date-iso8601
1519     "s" gnus-article-date-user)
1520
1521   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1522     "t" gnus-article-remove-trailing-blank-lines
1523     "l" gnus-article-strip-leading-blank-lines
1524     "m" gnus-article-strip-multiple-blank-lines
1525     "a" gnus-article-strip-blank-lines
1526     "A" gnus-article-strip-all-blank-lines
1527     "s" gnus-article-strip-leading-space
1528     "e" gnus-article-strip-trailing-space)
1529
1530   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1531     "v" gnus-version
1532     "f" gnus-summary-fetch-faq
1533     "d" gnus-summary-describe-group
1534     "h" gnus-summary-describe-briefly
1535     "i" gnus-info-find-node)
1536
1537   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1538     "e" gnus-summary-expire-articles
1539     "\M-\C-e" gnus-summary-expire-articles-now
1540     "\177" gnus-summary-delete-article
1541     [delete] gnus-summary-delete-article
1542     "m" gnus-summary-move-article
1543     "r" gnus-summary-respool-article
1544     "w" gnus-summary-edit-article
1545     "c" gnus-summary-copy-article
1546     "B" gnus-summary-crosspost-article
1547     "q" gnus-summary-respool-query
1548     "t" gnus-summary-respool-trace
1549     "i" gnus-summary-import-article
1550     "p" gnus-summary-article-posted-p)
1551
1552   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1553     "o" gnus-summary-save-article
1554     "m" gnus-summary-save-article-mail
1555     "F" gnus-summary-write-article-file
1556     "r" gnus-summary-save-article-rmail
1557     "f" gnus-summary-save-article-file
1558     "b" gnus-summary-save-article-body-file
1559     "h" gnus-summary-save-article-folder
1560     "v" gnus-summary-save-article-vm
1561     "p" gnus-summary-pipe-output
1562     "s" gnus-soup-add-article)
1563
1564   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1565     "b" gnus-summary-display-buttonized
1566     "m" gnus-summary-repair-multipart
1567     "v" gnus-article-view-part
1568     "o" gnus-article-save-part
1569     "c" gnus-article-copy-part
1570     "e" gnus-article-externalize-part
1571     "i" gnus-article-inline-part
1572     "|" gnus-article-pipe-part)
1573   )
1574
1575 (defun gnus-summary-make-menu-bar ()
1576   (gnus-turn-off-edit-menu 'summary)
1577
1578   (unless (boundp 'gnus-summary-misc-menu)
1579
1580     (easy-menu-define
1581      gnus-summary-kill-menu gnus-summary-mode-map ""
1582      (cons
1583       "Score"
1584       (nconc
1585        (list
1586         ["Enter score..." gnus-summary-score-entry t]
1587         ["Customize" gnus-score-customize t])
1588        (gnus-make-score-map 'increase)
1589        (gnus-make-score-map 'lower)
1590        '(("Mark"
1591           ["Kill below" gnus-summary-kill-below t]
1592           ["Mark above" gnus-summary-mark-above t]
1593           ["Tick above" gnus-summary-tick-above t]
1594           ["Clear above" gnus-summary-clear-above t])
1595          ["Current score" gnus-summary-current-score t]
1596          ["Set score" gnus-summary-set-score t]
1597          ["Switch current score file..." gnus-score-change-score-file t]
1598          ["Set mark below..." gnus-score-set-mark-below t]
1599          ["Set expunge below..." gnus-score-set-expunge-below t]
1600          ["Edit current score file" gnus-score-edit-current-scores t]
1601          ["Edit score file" gnus-score-edit-file t]
1602          ["Trace score" gnus-score-find-trace t]
1603          ["Find words" gnus-score-find-favourite-words t]
1604          ["Rescore buffer" gnus-summary-rescore t]
1605          ["Increase score..." gnus-summary-increase-score t]
1606          ["Lower score..." gnus-summary-lower-score t]))))
1607
1608     ;; Define both the Article menu in the summary buffer and the equivalent
1609     ;; Commands menu in the article buffer here for consistency.
1610     (let ((innards
1611            '(("Hide"
1612               ["All" gnus-article-hide t]
1613               ["Headers" gnus-article-hide-headers t]
1614               ["Signature" gnus-article-hide-signature t]
1615               ["Citation" gnus-article-hide-citation t]
1616               ["PGP" gnus-article-hide-pgp t]
1617               ["Banner" gnus-article-strip-banner t]
1618               ["Boring headers" gnus-article-hide-boring-headers t])
1619              ("Highlight"
1620               ["All" gnus-article-highlight t]
1621               ["Headers" gnus-article-highlight-headers t]
1622               ["Signature" gnus-article-highlight-signature t]
1623               ["Citation" gnus-article-highlight-citation t])
1624              ("MIME"
1625               ["Words" gnus-article-decode-mime-words t]
1626               ["Charset" gnus-article-decode-charset t]
1627               ["QP" gnus-article-de-quoted-unreadable t]
1628               ["View all" gnus-mime-view-all-parts t])
1629              ("Date"
1630               ["Local" gnus-article-date-local t]
1631               ["ISO8601" gnus-article-date-iso8601 t]
1632               ["UT" gnus-article-date-ut t]
1633               ["Original" gnus-article-date-original t]
1634               ["Lapsed" gnus-article-date-lapsed t]
1635               ["User-defined" gnus-article-date-user t])
1636              ("Washing"
1637               ("Remove Blanks"
1638                ["Leading" gnus-article-strip-leading-blank-lines t]
1639                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1640                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1641                ["All of the above" gnus-article-strip-blank-lines t]
1642                ["All" gnus-article-strip-all-blank-lines t]
1643                ["Leading space" gnus-article-strip-leading-space t]
1644                ["Trailing space" gnus-article-strip-trailing-space t])
1645               ["Overstrike" gnus-article-treat-overstrike t]
1646               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1647               ["Emphasis" gnus-article-emphasize t]
1648               ["Word wrap" gnus-article-fill-cited-article t]
1649               ["Fill long lines" gnus-article-fill-long-lines t]
1650               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1651               ["CR" gnus-article-remove-cr t]
1652               ["Show X-Face" gnus-article-display-x-face t]
1653               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1654               ["UnHTMLize" gnus-article-treat-html t]
1655               ["Rot 13" gnus-summary-caesar-message t]
1656               ["Unix pipe" gnus-summary-pipe-message t]
1657               ["Add buttons" gnus-article-add-buttons t]
1658               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1659               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1660               ["Verbose header" gnus-summary-verbose-headers t]
1661               ["Toggle header" gnus-summary-toggle-header t])
1662              ("Output"
1663               ["Save in default format" gnus-summary-save-article t]
1664               ["Save in file" gnus-summary-save-article-file t]
1665               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1666               ["Save in MH folder" gnus-summary-save-article-folder t]
1667               ["Save in VM folder" gnus-summary-save-article-vm t]
1668               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1669               ["Save body in file" gnus-summary-save-article-body-file t]
1670               ["Pipe through a filter" gnus-summary-pipe-output t]
1671               ["Add to SOUP packet" gnus-soup-add-article t]
1672               ["Print" gnus-summary-print-article t])
1673              ("Backend"
1674               ["Respool article..." gnus-summary-respool-article t]
1675               ["Move article..." gnus-summary-move-article
1676                (gnus-check-backend-function
1677                 'request-move-article gnus-newsgroup-name)]
1678               ["Copy article..." gnus-summary-copy-article t]
1679               ["Crosspost article..." gnus-summary-crosspost-article
1680                (gnus-check-backend-function
1681                 'request-replace-article gnus-newsgroup-name)]
1682               ["Import file..." gnus-summary-import-article t]
1683               ["Check if posted" gnus-summary-article-posted-p t]
1684               ["Edit article" gnus-summary-edit-article
1685                (not (gnus-group-read-only-p))]
1686               ["Delete article" gnus-summary-delete-article
1687                (gnus-check-backend-function
1688                 'request-expire-articles gnus-newsgroup-name)]
1689               ["Query respool" gnus-summary-respool-query t]
1690               ["Trace respool" gnus-summary-respool-trace t]
1691               ["Delete expirable articles" gnus-summary-expire-articles-now
1692                (gnus-check-backend-function
1693                 'request-expire-articles gnus-newsgroup-name)])
1694              ("Extract"
1695               ["Uudecode" gnus-uu-decode-uu t]
1696               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1697               ["Unshar" gnus-uu-decode-unshar t]
1698               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1699               ["Save" gnus-uu-decode-save t]
1700               ["Binhex" gnus-uu-decode-binhex t]
1701               ["Postscript" gnus-uu-decode-postscript t])
1702              ("Cache"
1703               ["Enter article" gnus-cache-enter-article t]
1704               ["Remove article" gnus-cache-remove-article t])
1705              ["Translate" gnus-article-babel t]
1706              ["Select article buffer" gnus-summary-select-article-buffer t]
1707              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1708              ["Isearch article..." gnus-summary-isearch-article t]
1709              ["Beginning of the article" gnus-summary-beginning-of-article t]
1710              ["End of the article" gnus-summary-end-of-article t]
1711              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1712              ["Fetch referenced articles" gnus-summary-refer-references t]
1713              ["Fetch current thread" gnus-summary-refer-thread t]
1714              ["Fetch article with id..." gnus-summary-refer-article t]
1715              ["Redisplay" gnus-summary-show-article t])))
1716       (easy-menu-define
1717        gnus-summary-article-menu gnus-summary-mode-map ""
1718        (cons "Article" innards))
1719
1720       (easy-menu-define
1721        gnus-article-commands-menu gnus-article-mode-map ""
1722        (cons "Commands" innards)))
1723
1724     (easy-menu-define
1725      gnus-summary-thread-menu gnus-summary-mode-map ""
1726      '("Threads"
1727        ["Toggle threading" gnus-summary-toggle-threads t]
1728        ["Hide threads" gnus-summary-hide-all-threads t]
1729        ["Show threads" gnus-summary-show-all-threads t]
1730        ["Hide thread" gnus-summary-hide-thread t]
1731        ["Show thread" gnus-summary-show-thread t]
1732        ["Go to next thread" gnus-summary-next-thread t]
1733        ["Go to previous thread" gnus-summary-prev-thread t]
1734        ["Go down thread" gnus-summary-down-thread t]
1735        ["Go up thread" gnus-summary-up-thread t]
1736        ["Top of thread" gnus-summary-top-thread t]
1737        ["Mark thread as read" gnus-summary-kill-thread t]
1738        ["Lower thread score" gnus-summary-lower-thread t]
1739        ["Raise thread score" gnus-summary-raise-thread t]
1740        ["Rethread current" gnus-summary-rethread-current t]
1741        ))
1742
1743     (easy-menu-define
1744      gnus-summary-post-menu gnus-summary-mode-map ""
1745      '("Post"
1746        ["Post an article" gnus-summary-post-news t]
1747        ["Followup" gnus-summary-followup t]
1748        ["Followup and yank" gnus-summary-followup-with-original t]
1749        ["Supersede article" gnus-summary-supersede-article t]
1750        ["Cancel article" gnus-summary-cancel-article t]
1751        ["Reply" gnus-summary-reply t]
1752        ["Reply and yank" gnus-summary-reply-with-original t]
1753        ["Wide reply" gnus-summary-wide-reply t]
1754        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1755        ["Mail forward" gnus-summary-mail-forward t]
1756        ["Post forward" gnus-summary-post-forward t]
1757        ["Digest and mail" gnus-uu-digest-mail-forward t]
1758        ["Digest and post" gnus-uu-digest-post-forward t]
1759        ["Resend message" gnus-summary-resend-message t]
1760        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1761        ["Send a mail" gnus-summary-mail-other-window t]
1762        ["Uuencode and post" gnus-uu-post-news t]
1763        ["Followup via news" gnus-summary-followup-to-mail t]
1764        ["Followup via news and yank"
1765         gnus-summary-followup-to-mail-with-original t]
1766        ;;("Draft"
1767        ;;["Send" gnus-summary-send-draft t]
1768        ;;["Send bounced" gnus-resend-bounced-mail t])
1769        ))
1770
1771     (easy-menu-define
1772      gnus-summary-misc-menu gnus-summary-mode-map ""
1773      '("Misc"
1774        ("Mark Read"
1775         ["Mark as read" gnus-summary-mark-as-read-forward t]
1776         ["Mark same subject and select"
1777          gnus-summary-kill-same-subject-and-select t]
1778         ["Mark same subject" gnus-summary-kill-same-subject t]
1779         ["Catchup" gnus-summary-catchup t]
1780         ["Catchup all" gnus-summary-catchup-all t]
1781         ["Catchup to here" gnus-summary-catchup-to-here t]
1782         ["Catchup region" gnus-summary-mark-region-as-read t]
1783         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1784        ("Mark Various"
1785         ["Tick" gnus-summary-tick-article-forward t]
1786         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1787         ["Remove marks" gnus-summary-clear-mark-forward t]
1788         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1789         ["Set bookmark" gnus-summary-set-bookmark t]
1790         ["Remove bookmark" gnus-summary-remove-bookmark t])
1791        ("Mark Limit"
1792         ["Marks..." gnus-summary-limit-to-marks t]
1793         ["Subject..." gnus-summary-limit-to-subject t]
1794         ["Author..." gnus-summary-limit-to-author t]
1795         ["Age..." gnus-summary-limit-to-age t]
1796         ["Extra..." gnus-summary-limit-to-extra t]
1797         ["Score" gnus-summary-limit-to-score t]
1798         ["Unread" gnus-summary-limit-to-unread t]
1799         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1800         ["Articles" gnus-summary-limit-to-articles t]
1801         ["Pop limit" gnus-summary-pop-limit t]
1802         ["Show dormant" gnus-summary-limit-include-dormant t]
1803         ["Hide childless dormant"
1804          gnus-summary-limit-exclude-childless-dormant t]
1805         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1806         ["Hide marked" gnus-summary-limit-exclude-marks t]
1807         ["Show expunged" gnus-summary-show-all-expunged t])
1808        ("Process Mark"
1809         ["Set mark" gnus-summary-mark-as-processable t]
1810         ["Remove mark" gnus-summary-unmark-as-processable t]
1811         ["Remove all marks" gnus-summary-unmark-all-processable t]
1812         ["Mark above" gnus-uu-mark-over t]
1813         ["Mark series" gnus-uu-mark-series t]
1814         ["Mark region" gnus-uu-mark-region t]
1815         ["Unmark region" gnus-uu-unmark-region t]
1816         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1817         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1818         ["Mark all" gnus-uu-mark-all t]
1819         ["Mark buffer" gnus-uu-mark-buffer t]
1820         ["Mark sparse" gnus-uu-mark-sparse t]
1821         ["Mark thread" gnus-uu-mark-thread t]
1822         ["Unmark thread" gnus-uu-unmark-thread t]
1823         ("Process Mark Sets"
1824          ["Kill" gnus-summary-kill-process-mark t]
1825          ["Yank" gnus-summary-yank-process-mark
1826           gnus-newsgroup-process-stack]
1827          ["Save" gnus-summary-save-process-mark t]))
1828        ("Scroll article"
1829         ["Page forward" gnus-summary-next-page t]
1830         ["Page backward" gnus-summary-prev-page t]
1831         ["Line forward" gnus-summary-scroll-up t])
1832        ("Move"
1833         ["Next unread article" gnus-summary-next-unread-article t]
1834         ["Previous unread article" gnus-summary-prev-unread-article t]
1835         ["Next article" gnus-summary-next-article t]
1836         ["Previous article" gnus-summary-prev-article t]
1837         ["Next unread subject" gnus-summary-next-unread-subject t]
1838         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1839         ["Next article same subject" gnus-summary-next-same-subject t]
1840         ["Previous article same subject" gnus-summary-prev-same-subject t]
1841         ["First unread article" gnus-summary-first-unread-article t]
1842         ["Best unread article" gnus-summary-best-unread-article t]
1843         ["Go to subject number..." gnus-summary-goto-subject t]
1844         ["Go to article number..." gnus-summary-goto-article t]
1845         ["Go to the last article" gnus-summary-goto-last-article t]
1846         ["Pop article off history" gnus-summary-pop-article t])
1847        ("Sort"
1848         ["Sort by number" gnus-summary-sort-by-number t]
1849         ["Sort by author" gnus-summary-sort-by-author t]
1850         ["Sort by subject" gnus-summary-sort-by-subject t]
1851         ["Sort by date" gnus-summary-sort-by-date t]
1852         ["Sort by score" gnus-summary-sort-by-score t]
1853         ["Sort by lines" gnus-summary-sort-by-lines t]
1854         ["Sort by characters" gnus-summary-sort-by-chars t])
1855        ("Help"
1856         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1857         ["Describe group" gnus-summary-describe-group t]
1858         ["Read manual" gnus-info-find-node t])
1859        ("Modes"
1860         ["Pick and read" gnus-pick-mode t]
1861         ["Binary" gnus-binary-mode t])
1862        ("Regeneration"
1863         ["Regenerate" gnus-summary-prepare t]
1864         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1865         ["Toggle threading" gnus-summary-toggle-threads t])
1866        ["Filter articles..." gnus-summary-execute-command t]
1867        ["Run command on subjects..." gnus-summary-universal-argument t]
1868        ["Search articles forward..." gnus-summary-search-article-forward t]
1869        ["Search articles backward..." gnus-summary-search-article-backward t]
1870        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1871        ["Expand window" gnus-summary-expand-window t]
1872        ["Expire expirable articles" gnus-summary-expire-articles
1873         (gnus-check-backend-function
1874          'request-expire-articles gnus-newsgroup-name)]
1875        ["Edit local kill file" gnus-summary-edit-local-kill t]
1876        ["Edit main kill file" gnus-summary-edit-global-kill t]
1877        ["Edit group parameters" gnus-summary-edit-parameters t]
1878        ["Customize group parameters" gnus-summary-customize-parameters t]
1879        ["Send a bug report" gnus-bug t]
1880        ("Exit"
1881         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1882         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1883         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1884         ["Exit group" gnus-summary-exit t]
1885         ["Exit group without updating" gnus-summary-exit-no-update t]
1886         ["Exit and goto next group" gnus-summary-next-group t]
1887         ["Exit and goto prev group" gnus-summary-prev-group t]
1888         ["Reselect group" gnus-summary-reselect-current-group t]
1889         ["Rescan group" gnus-summary-rescan-group t]
1890         ["Update dribble" gnus-summary-save-newsrc t])))
1891
1892     (gnus-run-hooks 'gnus-summary-menu-hook)))
1893
1894 (defun gnus-score-set-default (var value)
1895   "A version of set that updates the GNU Emacs menu-bar."
1896   (set var value)
1897   ;; It is the message that forces the active status to be updated.
1898   (message ""))
1899
1900 (defun gnus-make-score-map (type)
1901   "Make a summary score map of type TYPE."
1902   (if t
1903       nil
1904     (let ((headers '(("author" "from" string)
1905                      ("subject" "subject" string)
1906                      ("article body" "body" string)
1907                      ("article head" "head" string)
1908                      ("xref" "xref" string)
1909                      ("extra header" "extra" string)
1910                      ("lines" "lines" number)
1911                      ("followups to author" "followup" string)))
1912           (types '((number ("less than" <)
1913                            ("greater than" >)
1914                            ("equal" =))
1915                    (string ("substring" s)
1916                            ("exact string" e)
1917                            ("fuzzy string" f)
1918                            ("regexp" r))))
1919           (perms '(("temporary" (current-time-string))
1920                    ("permanent" nil)
1921                    ("immediate" now)))
1922           header)
1923       (list
1924        (apply
1925         'nconc
1926         (list
1927          (if (eq type 'lower)
1928              "Lower score"
1929            "Increase score"))
1930         (let (outh)
1931           (while headers
1932             (setq header (car headers))
1933             (setq outh
1934                   (cons
1935                    (apply
1936                     'nconc
1937                     (list (car header))
1938                     (let ((ts (cdr (assoc (nth 2 header) types)))
1939                           outt)
1940                       (while ts
1941                         (setq outt
1942                               (cons
1943                                (apply
1944                                 'nconc
1945                                 (list (caar ts))
1946                                 (let ((ps perms)
1947                                       outp)
1948                                   (while ps
1949                                     (setq outp
1950                                           (cons
1951                                            (vector
1952                                             (caar ps)
1953                                             (list
1954                                              'gnus-summary-score-entry
1955                                              (nth 1 header)
1956                                              (if (or (string= (nth 1 header)
1957                                                               "head")
1958                                                      (string= (nth 1 header)
1959                                                               "body"))
1960                                                  ""
1961                                                (list 'gnus-summary-header
1962                                                      (nth 1 header)))
1963                                              (list 'quote (nth 1 (car ts)))
1964                                              (list 'gnus-score-default nil)
1965                                              (nth 1 (car ps))
1966                                              t)
1967                                             t)
1968                                            outp))
1969                                     (setq ps (cdr ps)))
1970                                   (list (nreverse outp))))
1971                                outt))
1972                         (setq ts (cdr ts)))
1973                       (list (nreverse outt))))
1974                    outh))
1975             (setq headers (cdr headers)))
1976           (list (nreverse outh))))))))
1977
1978 \f
1979
1980 (defun gnus-summary-mode (&optional group)
1981   "Major mode for reading articles.
1982
1983 All normal editing commands are switched off.
1984 \\<gnus-summary-mode-map>
1985 Each line in this buffer represents one article.  To read an
1986 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1987 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1988 respectively.
1989
1990 You can also post articles and send mail from this buffer.  To
1991 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1992 of an article, type `\\[gnus-summary-reply]'.
1993
1994 There are approx. one gazillion commands you can execute in this
1995 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1996
1997 The following commands are available:
1998
1999 \\{gnus-summary-mode-map}"
2000   (interactive)
2001   (when (gnus-visual-p 'summary-menu 'menu)
2002     (gnus-summary-make-menu-bar))
2003   (kill-all-local-variables)
2004   (gnus-summary-make-local-variables)
2005   (gnus-make-thread-indent-array)
2006   (gnus-simplify-mode-line)
2007   (setq major-mode 'gnus-summary-mode)
2008   (setq mode-name "Summary")
2009   (make-local-variable 'minor-mode-alist)
2010   (use-local-map gnus-summary-mode-map)
2011   (buffer-disable-undo)
2012   (setq buffer-read-only t)             ;Disable modification
2013   (setq truncate-lines t)
2014   (setq selective-display t)
2015   (setq selective-display-ellipses t)   ;Display `...'
2016   (gnus-summary-set-display-table)
2017   (gnus-set-default-directory)
2018   (setq gnus-newsgroup-name group)
2019   (make-local-variable 'gnus-summary-line-format)
2020   (make-local-variable 'gnus-summary-line-format-spec)
2021   (make-local-variable 'gnus-summary-dummy-line-format)
2022   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2023   (make-local-variable 'gnus-summary-mark-positions)
2024   (make-local-hook 'pre-command-hook)
2025   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2026   (gnus-run-hooks 'gnus-summary-mode-hook)
2027   (mm-enable-multibyte)
2028   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2029   (gnus-update-summary-mark-positions))
2030
2031 (defun gnus-summary-make-local-variables ()
2032   "Make all the local summary buffer variables."
2033   (let (global)
2034     (dolist (local gnus-summary-local-variables)
2035       (if (consp local)
2036           (progn
2037             (if (eq (cdr local) 'global)
2038                 ;; Copy the global value of the variable.
2039                 (setq global (symbol-value (car local)))
2040               ;; Use the value from the list.
2041               (setq global (eval (cdr local))))
2042             (set (make-local-variable (car local)) global))
2043         ;; Simple nil-valued local variable.
2044         (set (make-local-variable local) nil)))))
2045
2046 (defun gnus-summary-clear-local-variables ()
2047   (let ((locals gnus-summary-local-variables))
2048     (while locals
2049       (if (consp (car locals))
2050           (and (vectorp (caar locals))
2051                (set (caar locals) nil))
2052         (and (vectorp (car locals))
2053              (set (car locals) nil)))
2054       (setq locals (cdr locals)))))
2055
2056 ;; Summary data functions.
2057
2058 (defmacro gnus-data-number (data)
2059   `(car ,data))
2060
2061 (defmacro gnus-data-set-number (data number)
2062   `(setcar ,data ,number))
2063
2064 (defmacro gnus-data-mark (data)
2065   `(nth 1 ,data))
2066
2067 (defmacro gnus-data-set-mark (data mark)
2068   `(setcar (nthcdr 1 ,data) ,mark))
2069
2070 (defmacro gnus-data-pos (data)
2071   `(nth 2 ,data))
2072
2073 (defmacro gnus-data-set-pos (data pos)
2074   `(setcar (nthcdr 2 ,data) ,pos))
2075
2076 (defmacro gnus-data-header (data)
2077   `(nth 3 ,data))
2078
2079 (defmacro gnus-data-set-header (data header)
2080   `(setf (nth 3 ,data) ,header))
2081
2082 (defmacro gnus-data-level (data)
2083   `(nth 4 ,data))
2084
2085 (defmacro gnus-data-unread-p (data)
2086   `(= (nth 1 ,data) gnus-unread-mark))
2087
2088 (defmacro gnus-data-read-p (data)
2089   `(/= (nth 1 ,data) gnus-unread-mark))
2090
2091 (defmacro gnus-data-pseudo-p (data)
2092   `(consp (nth 3 ,data)))
2093
2094 (defmacro gnus-data-find (number)
2095   `(assq ,number gnus-newsgroup-data))
2096
2097 (defmacro gnus-data-find-list (number &optional data)
2098   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2099      (memq (assq ,number bdata)
2100            bdata)))
2101
2102 (defmacro gnus-data-make (number mark pos header level)
2103   `(list ,number ,mark ,pos ,header ,level))
2104
2105 (defun gnus-data-enter (after-article number mark pos header level offset)
2106   (let ((data (gnus-data-find-list after-article)))
2107     (unless data
2108       (error "No such article: %d" after-article))
2109     (setcdr data (cons (gnus-data-make number mark pos header level)
2110                        (cdr data)))
2111     (setq gnus-newsgroup-data-reverse nil)
2112     (gnus-data-update-list (cddr data) offset)))
2113
2114 (defun gnus-data-enter-list (after-article list &optional offset)
2115   (when list
2116     (let ((data (and after-article (gnus-data-find-list after-article)))
2117           (ilist list))
2118       (if (not (or data
2119                    after-article))
2120           (let ((odata gnus-newsgroup-data))
2121             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2122             (when offset
2123               (gnus-data-update-list odata offset)))
2124         ;; Find the last element in the list to be spliced into the main
2125         ;; list.
2126         (while (cdr list)
2127           (setq list (cdr list)))
2128         (if (not data)
2129             (progn
2130               (setcdr list gnus-newsgroup-data)
2131               (setq gnus-newsgroup-data ilist)
2132               (when offset
2133                 (gnus-data-update-list (cdr list) offset)))
2134           (setcdr list (cdr data))
2135           (setcdr data ilist)
2136           (when offset
2137             (gnus-data-update-list (cdr list) offset))))
2138       (setq gnus-newsgroup-data-reverse nil))))
2139
2140 (defun gnus-data-remove (article &optional offset)
2141   (let ((data gnus-newsgroup-data))
2142     (if (= (gnus-data-number (car data)) article)
2143         (progn
2144           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2145                 gnus-newsgroup-data-reverse nil)
2146           (when offset
2147             (gnus-data-update-list gnus-newsgroup-data offset)))
2148       (while (cdr data)
2149         (when (= (gnus-data-number (cadr data)) article)
2150           (setcdr data (cddr data))
2151           (when offset
2152             (gnus-data-update-list (cdr data) offset))
2153           (setq data nil
2154                 gnus-newsgroup-data-reverse nil))
2155         (setq data (cdr data))))))
2156
2157 (defmacro gnus-data-list (backward)
2158   `(if ,backward
2159        (or gnus-newsgroup-data-reverse
2160            (setq gnus-newsgroup-data-reverse
2161                  (reverse gnus-newsgroup-data)))
2162      gnus-newsgroup-data))
2163
2164 (defun gnus-data-update-list (data offset)
2165   "Add OFFSET to the POS of all data entries in DATA."
2166   (setq gnus-newsgroup-data-reverse nil)
2167   (while data
2168     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2169     (setq data (cdr data))))
2170
2171 (defun gnus-summary-article-pseudo-p (article)
2172   "Say whether this article is a pseudo article or not."
2173   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2174
2175 (defmacro gnus-summary-article-sparse-p (article)
2176   "Say whether this article is a sparse article or not."
2177   `(memq ,article gnus-newsgroup-sparse))
2178
2179 (defmacro gnus-summary-article-ancient-p (article)
2180   "Say whether this article is a sparse article or not."
2181   `(memq ,article gnus-newsgroup-ancient))
2182
2183 (defun gnus-article-parent-p (number)
2184   "Say whether this article is a parent or not."
2185   (let ((data (gnus-data-find-list number)))
2186     (and (cdr data)                     ; There has to be an article after...
2187          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2188             (gnus-data-level (nth 1 data))))))
2189
2190 (defun gnus-article-children (number)
2191   "Return a list of all children to NUMBER."
2192   (let* ((data (gnus-data-find-list number))
2193          (level (gnus-data-level (car data)))
2194          children)
2195     (setq data (cdr data))
2196     (while (and data
2197                 (= (gnus-data-level (car data)) (1+ level)))
2198       (push (gnus-data-number (car data)) children)
2199       (setq data (cdr data)))
2200     children))
2201
2202 (defmacro gnus-summary-skip-intangible ()
2203   "If the current article is intangible, then jump to a different article."
2204   '(let ((to (get-text-property (point) 'gnus-intangible)))
2205      (and to (gnus-summary-goto-subject to))))
2206
2207 (defmacro gnus-summary-article-intangible-p ()
2208   "Say whether this article is intangible or not."
2209   '(get-text-property (point) 'gnus-intangible))
2210
2211 (defun gnus-article-read-p (article)
2212   "Say whether ARTICLE is read or not."
2213   (not (or (memq article gnus-newsgroup-marked)
2214            (memq article gnus-newsgroup-unreads)
2215            (memq article gnus-newsgroup-unselected)
2216            (memq article gnus-newsgroup-dormant))))
2217
2218 ;; Some summary mode macros.
2219
2220 (defmacro gnus-summary-article-number ()
2221   "The article number of the article on the current line.
2222 If there isn's an article number here, then we return the current
2223 article number."
2224   '(progn
2225      (gnus-summary-skip-intangible)
2226      (or (get-text-property (point) 'gnus-number)
2227          (gnus-summary-last-subject))))
2228
2229 (defmacro gnus-summary-article-header (&optional number)
2230   "Return the header of article NUMBER."
2231   `(gnus-data-header (gnus-data-find
2232                       ,(or number '(gnus-summary-article-number)))))
2233
2234 (defmacro gnus-summary-thread-level (&optional number)
2235   "Return the level of thread that starts with article NUMBER."
2236   `(if (and (eq gnus-summary-make-false-root 'dummy)
2237             (get-text-property (point) 'gnus-intangible))
2238        0
2239      (gnus-data-level (gnus-data-find
2240                        ,(or number '(gnus-summary-article-number))))))
2241
2242 (defmacro gnus-summary-article-mark (&optional number)
2243   "Return the mark of article NUMBER."
2244   `(gnus-data-mark (gnus-data-find
2245                     ,(or number '(gnus-summary-article-number)))))
2246
2247 (defmacro gnus-summary-article-pos (&optional number)
2248   "Return the position of the line of article NUMBER."
2249   `(gnus-data-pos (gnus-data-find
2250                    ,(or number '(gnus-summary-article-number)))))
2251
2252 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2253 (defmacro gnus-summary-article-subject (&optional number)
2254   "Return current subject string or nil if nothing."
2255   `(let ((headers
2256           ,(if number
2257                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2258              '(gnus-data-header (assq (gnus-summary-article-number)
2259                                       gnus-newsgroup-data)))))
2260      (and headers
2261           (vectorp headers)
2262           (mail-header-subject headers))))
2263
2264 (defmacro gnus-summary-article-score (&optional number)
2265   "Return current article score."
2266   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2267                   gnus-newsgroup-scored))
2268        gnus-summary-default-score 0))
2269
2270 (defun gnus-summary-article-children (&optional number)
2271   "Return a list of article numbers that are children of article NUMBER."
2272   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2273          (level (gnus-data-level (car data)))
2274          l children)
2275     (while (and (setq data (cdr data))
2276                 (> (setq l (gnus-data-level (car data))) level))
2277       (and (= (1+ level) l)
2278            (push (gnus-data-number (car data))
2279                  children)))
2280     (nreverse children)))
2281
2282 (defun gnus-summary-article-parent (&optional number)
2283   "Return the article number of the parent of article NUMBER."
2284   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2285                                     (gnus-data-list t)))
2286          (level (gnus-data-level (car data))))
2287     (if (zerop level)
2288         ()                              ; This is a root.
2289       ;; We search until we find an article with a level less than
2290       ;; this one.  That function has to be the parent.
2291       (while (and (setq data (cdr data))
2292                   (not (< (gnus-data-level (car data)) level))))
2293       (and data (gnus-data-number (car data))))))
2294
2295 (defun gnus-unread-mark-p (mark)
2296   "Say whether MARK is the unread mark."
2297   (= mark gnus-unread-mark))
2298
2299 (defun gnus-read-mark-p (mark)
2300   "Say whether MARK is one of the marks that mark as read.
2301 This is all marks except unread, ticked, dormant, and expirable."
2302   (not (or (= mark gnus-unread-mark)
2303            (= mark gnus-ticked-mark)
2304            (= mark gnus-dormant-mark)
2305            (= mark gnus-expirable-mark))))
2306
2307 (defmacro gnus-article-mark (number)
2308   "Return the MARK of article NUMBER.
2309 This macro should only be used when computing the mark the \"first\"
2310 time; i.e., when generating the summary lines.  After that,
2311 `gnus-summary-article-mark' should be used to examine the
2312 marks of articles."
2313   `(cond
2314     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2315     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2316     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2317     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2318     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2319     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2320     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2321     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2322            gnus-ancient-mark))))
2323
2324 ;; Saving hidden threads.
2325
2326 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2327 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2328
2329 (defmacro gnus-save-hidden-threads (&rest forms)
2330   "Save hidden threads, eval FORMS, and restore the hidden threads."
2331   (let ((config (make-symbol "config")))
2332     `(let ((,config (gnus-hidden-threads-configuration)))
2333        (unwind-protect
2334            (save-excursion
2335              ,@forms)
2336          (gnus-restore-hidden-threads-configuration ,config)))))
2337
2338 (defun gnus-data-compute-positions ()
2339   "Compute the positions of all articles."
2340   (setq gnus-newsgroup-data-reverse nil)
2341   (let ((data gnus-newsgroup-data))
2342     (save-excursion
2343       (gnus-save-hidden-threads
2344         (gnus-summary-show-all-threads)
2345         (goto-char (point-min))
2346         (while data
2347           (while (get-text-property (point) 'gnus-intangible)
2348             (forward-line 1))
2349           (gnus-data-set-pos (car data) (+ (point) 3))
2350           (setq data (cdr data))
2351           (forward-line 1))))))
2352
2353 (defun gnus-hidden-threads-configuration ()
2354   "Return the current hidden threads configuration."
2355   (save-excursion
2356     (let (config)
2357       (goto-char (point-min))
2358       (while (search-forward "\r" nil t)
2359         (push (1- (point)) config))
2360       config)))
2361
2362 (defun gnus-restore-hidden-threads-configuration (config)
2363   "Restore hidden threads configuration from CONFIG."
2364   (let (point buffer-read-only)
2365     (while (setq point (pop config))
2366       (when (and (< point (point-max))
2367                  (goto-char point)
2368                  (eq (char-after) ?\n))
2369         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2370
2371 ;; Various summary mode internalish functions.
2372
2373 (defun gnus-mouse-pick-article (e)
2374   (interactive "e")
2375   (mouse-set-point e)
2376   (gnus-summary-next-page nil t))
2377
2378 (defun gnus-summary-set-display-table ()
2379   ;; Change the display table.  Odd characters have a tendency to mess
2380   ;; up nicely formatted displays - we make all possible glyphs
2381   ;; display only a single character.
2382
2383   ;; We start from the standard display table, if any.
2384   (let ((table (or (copy-sequence standard-display-table)
2385                    (make-display-table)))
2386         (i 32))
2387     ;; Nix out all the control chars...
2388     (while (>= (setq i (1- i)) 0)
2389       (aset table i [??]))
2390     ;; ... but not newline and cr, of course.  (cr is necessary for the
2391     ;; selective display).
2392     (aset table ?\n nil)
2393     (aset table ?\r nil)
2394     ;; We keep TAB as well.
2395     (aset table ?\t nil)
2396     ;; We nix out any glyphs over 126 that are not set already.
2397     (let ((i 256))
2398       (while (>= (setq i (1- i)) 127)
2399         ;; Only modify if the entry is nil.
2400         (unless (aref table i)
2401           (aset table i [??]))))
2402     (setq buffer-display-table table)))
2403
2404 (defun gnus-summary-setup-buffer (group)
2405   "Initialize summary buffer."
2406   (let ((buffer (concat "*Summary " group "*")))
2407     (if (get-buffer buffer)
2408         (progn
2409           (set-buffer buffer)
2410           (setq gnus-summary-buffer (current-buffer))
2411           (not gnus-newsgroup-prepared))
2412       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2413       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2414       (gnus-summary-mode group)
2415       (when gnus-carpal
2416         (gnus-carpal-setup-buffer 'summary))
2417       (unless gnus-single-article-buffer
2418         (make-local-variable 'gnus-article-buffer)
2419         (make-local-variable 'gnus-article-current)
2420         (make-local-variable 'gnus-original-article-buffer))
2421       (setq gnus-newsgroup-name group)
2422       t)))
2423
2424 (defun gnus-set-global-variables ()
2425   ;; Set the global equivalents of the summary buffer-local variables
2426   ;; to the latest values they had.  These reflect the summary buffer
2427   ;; that was in action when the last article was fetched.
2428   (when (eq major-mode 'gnus-summary-mode)
2429     (setq gnus-summary-buffer (current-buffer))
2430     (let ((name gnus-newsgroup-name)
2431           (marked gnus-newsgroup-marked)
2432           (unread gnus-newsgroup-unreads)
2433           (headers gnus-current-headers)
2434           (data gnus-newsgroup-data)
2435           (summary gnus-summary-buffer)
2436           (article-buffer gnus-article-buffer)
2437           (original gnus-original-article-buffer)
2438           (gac gnus-article-current)
2439           (reffed gnus-reffed-article-number)
2440           (score-file gnus-current-score-file)
2441           (default-charset gnus-newsgroup-charset))
2442       (save-excursion
2443         (set-buffer gnus-group-buffer)
2444         (setq gnus-newsgroup-name name
2445               gnus-newsgroup-marked marked
2446               gnus-newsgroup-unreads unread
2447               gnus-current-headers headers
2448               gnus-newsgroup-data data
2449               gnus-article-current gac
2450               gnus-summary-buffer summary
2451               gnus-article-buffer article-buffer
2452               gnus-original-article-buffer original
2453               gnus-reffed-article-number reffed
2454               gnus-current-score-file score-file
2455               gnus-newsgroup-charset default-charset)
2456         ;; The article buffer also has local variables.
2457         (when (gnus-buffer-live-p gnus-article-buffer)
2458           (set-buffer gnus-article-buffer)
2459           (setq gnus-summary-buffer summary))))))
2460
2461 (defun gnus-summary-article-unread-p (article)
2462   "Say whether ARTICLE is unread or not."
2463   (memq article gnus-newsgroup-unreads))
2464
2465 (defun gnus-summary-first-article-p (&optional article)
2466   "Return whether ARTICLE is the first article in the buffer."
2467   (if (not (setq article (or article (gnus-summary-article-number))))
2468       nil
2469     (eq article (caar gnus-newsgroup-data))))
2470
2471 (defun gnus-summary-last-article-p (&optional article)
2472   "Return whether ARTICLE is the last article in the buffer."
2473   (if (not (setq article (or article (gnus-summary-article-number))))
2474       ;; All non-existent numbers are the last article.  :-)
2475       t
2476     (not (cdr (gnus-data-find-list article)))))
2477
2478 (defun gnus-make-thread-indent-array ()
2479   (let ((n 200))
2480     (unless (and gnus-thread-indent-array
2481                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2482       (setq gnus-thread-indent-array (make-vector 201 "")
2483             gnus-thread-indent-array-level gnus-thread-indent-level)
2484       (while (>= n 0)
2485         (aset gnus-thread-indent-array n
2486               (make-string (* n gnus-thread-indent-level) ? ))
2487         (setq n (1- n))))))
2488
2489 (defun gnus-update-summary-mark-positions ()
2490   "Compute where the summary marks are to go."
2491   (save-excursion
2492     (when (gnus-buffer-exists-p gnus-summary-buffer)
2493       (set-buffer gnus-summary-buffer))
2494     (let ((gnus-replied-mark 129)
2495           (gnus-score-below-mark 130)
2496           (gnus-score-over-mark 130)
2497           (gnus-download-mark 131)
2498           (spec gnus-summary-line-format-spec)
2499           gnus-visual pos)
2500       (save-excursion
2501         (gnus-set-work-buffer)
2502         (let ((gnus-summary-line-format-spec spec)
2503               (gnus-newsgroup-downloadable '((0 . t))))
2504           (gnus-summary-insert-line
2505            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2506           (goto-char (point-min))
2507           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2508                                              (- (point) 2)))))
2509           (goto-char (point-min))
2510           (push (cons 'replied (and (search-forward "\201" nil t)
2511                                     (- (point) 2)))
2512                 pos)
2513           (goto-char (point-min))
2514           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2515                 pos)
2516           (goto-char (point-min))
2517           (push (cons 'download
2518                       (and (search-forward "\203" nil t) (- (point) 2)))
2519                 pos)))
2520       (setq gnus-summary-mark-positions pos))))
2521
2522 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2523   "Insert a dummy root in the summary buffer."
2524   (beginning-of-line)
2525   (gnus-add-text-properties
2526    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2527    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2528
2529 (defun gnus-summary-from-or-to-or-newsgroups (header)
2530   (let ((to (cdr (assq 'To (mail-header-extra header))))
2531         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2532         (mail-parse-charset gnus-newsgroup-charset))
2533     (cond
2534      ((and to
2535            gnus-ignored-from-addresses
2536            (string-match gnus-ignored-from-addresses
2537                          (mail-header-from header)))
2538       (concat "-> "
2539               (or (car (funcall gnus-extract-address-components
2540                                 (funcall
2541                                  gnus-decode-encoded-word-function to)))
2542                   (funcall gnus-decode-encoded-word-function to))))
2543      ((and newsgroups
2544            gnus-ignored-from-addresses
2545            (string-match gnus-ignored-from-addresses
2546                          (mail-header-from header)))
2547       (concat "=> " newsgroups))
2548      (t
2549       (or (car (funcall gnus-extract-address-components
2550                         (mail-header-from header)))
2551           (mail-header-from header))))))
2552
2553 (defun gnus-summary-insert-line (gnus-tmp-header
2554                                  gnus-tmp-level gnus-tmp-current
2555                                  gnus-tmp-unread gnus-tmp-replied
2556                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2557                                  &optional gnus-tmp-dummy gnus-tmp-score
2558                                  gnus-tmp-process)
2559   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2560          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2561          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2562          (gnus-tmp-score-char
2563           (if (or (null gnus-summary-default-score)
2564                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2565                       gnus-summary-zcore-fuzz))
2566               ? ;Whitespace
2567             (if (< gnus-tmp-score gnus-summary-default-score)
2568                 gnus-score-below-mark gnus-score-over-mark)))
2569          (gnus-tmp-replied
2570           (cond (gnus-tmp-process gnus-process-mark)
2571                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2572                  gnus-cached-mark)
2573                 (gnus-tmp-replied gnus-replied-mark)
2574                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2575                  gnus-saved-mark)
2576                 (t gnus-unread-mark)))
2577          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2578          (gnus-tmp-name
2579           (cond
2580            ((string-match "<[^>]+> *$" gnus-tmp-from)
2581             (let ((beg (match-beginning 0)))
2582               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2583                        (substring gnus-tmp-from (1+ (match-beginning 0))
2584                                   (1- (match-end 0))))
2585                   (substring gnus-tmp-from 0 beg))))
2586            ((string-match "(.+)" gnus-tmp-from)
2587             (substring gnus-tmp-from
2588                        (1+ (match-beginning 0)) (1- (match-end 0))))
2589            (t gnus-tmp-from)))
2590          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2591          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2592          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2593          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2594          (buffer-read-only nil))
2595     (when (string= gnus-tmp-name "")
2596       (setq gnus-tmp-name gnus-tmp-from))
2597     (unless (numberp gnus-tmp-lines)
2598       (setq gnus-tmp-lines 0))
2599     (gnus-put-text-property
2600      (point)
2601      (progn (eval gnus-summary-line-format-spec) (point))
2602      'gnus-number gnus-tmp-number)
2603     (when (gnus-visual-p 'summary-highlight 'highlight)
2604       (forward-line -1)
2605       (gnus-run-hooks 'gnus-summary-update-hook)
2606       (forward-line 1))))
2607
2608 (defun gnus-summary-update-line (&optional dont-update)
2609   ;; Update summary line after change.
2610   (when (and gnus-summary-default-score
2611              (not gnus-summary-inhibit-highlight))
2612     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2613            (article (gnus-summary-article-number))
2614            (score (gnus-summary-article-score article)))
2615       (unless dont-update
2616         (if (and gnus-summary-mark-below
2617                  (< (gnus-summary-article-score)
2618                     gnus-summary-mark-below))
2619             ;; This article has a low score, so we mark it as read.
2620             (when (memq article gnus-newsgroup-unreads)
2621               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2622           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2623             ;; This article was previously marked as read on account
2624             ;; of a low score, but now it has risen, so we mark it as
2625             ;; unread.
2626             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2627         (gnus-summary-update-mark
2628          (if (or (null gnus-summary-default-score)
2629                  (<= (abs (- score gnus-summary-default-score))
2630                      gnus-summary-zcore-fuzz))
2631              ? ;Whitespace
2632            (if (< score gnus-summary-default-score)
2633                gnus-score-below-mark gnus-score-over-mark))
2634          'score))
2635       ;; Do visual highlighting.
2636       (when (gnus-visual-p 'summary-highlight 'highlight)
2637         (gnus-run-hooks 'gnus-summary-update-hook)))))
2638
2639 (defvar gnus-tmp-new-adopts nil)
2640
2641 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2642   "Return the number of articles in THREAD.
2643 This may be 0 in some cases -- if none of the articles in
2644 the thread are to be displayed."
2645   (let* ((number
2646           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2647           (cond
2648            ((not (listp thread))
2649             1)
2650            ((and (consp thread) (cdr thread))
2651             (apply
2652              '+ 1 (mapcar
2653                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2654            ((null thread)
2655             1)
2656            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2657             1)
2658            (t 0))))
2659     (when (and level (zerop level) gnus-tmp-new-adopts)
2660       (incf number
2661             (apply '+ (mapcar
2662                        'gnus-summary-number-of-articles-in-thread
2663                        gnus-tmp-new-adopts))))
2664     (if char
2665         (if (> number 1) gnus-not-empty-thread-mark
2666           gnus-empty-thread-mark)
2667       number)))
2668
2669 (defun gnus-summary-set-local-parameters (group)
2670   "Go through the local params of GROUP and set all variable specs in that list."
2671   (let ((params (gnus-group-find-parameter group))
2672         elem)
2673     (while params
2674       (setq elem (car params)
2675             params (cdr params))
2676       (and (consp elem)                 ; Has to be a cons.
2677            (consp (cdr elem))           ; The cdr has to be a list.
2678            (symbolp (car elem))         ; Has to be a symbol in there.
2679            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2680            (ignore-errors               ; So we set it.
2681              (make-local-variable (car elem))
2682              (set (car elem) (eval (nth 1 elem))))))))
2683
2684 (defun gnus-summary-read-group (group &optional show-all no-article
2685                                       kill-buffer no-display backward
2686                                       select-articles)
2687   "Start reading news in newsgroup GROUP.
2688 If SHOW-ALL is non-nil, already read articles are also listed.
2689 If NO-ARTICLE is non-nil, no article is selected initially.
2690 If NO-DISPLAY, don't generate a summary buffer."
2691   (let (result)
2692     (while (and group
2693                 (null (setq result
2694                             (let ((gnus-auto-select-next nil))
2695                               (or (gnus-summary-read-group-1
2696                                    group show-all no-article
2697                                    kill-buffer no-display
2698                                    select-articles)
2699                                   (setq show-all nil
2700                                         select-articles nil)))))
2701                 (eq gnus-auto-select-next 'quietly))
2702       (set-buffer gnus-group-buffer)
2703       ;; The entry function called above goes to the next
2704       ;; group automatically, so we go two groups back
2705       ;; if we are searching for the previous group.
2706       (when backward
2707         (gnus-group-prev-unread-group 2))
2708       (if (not (equal group (gnus-group-group-name)))
2709           (setq group (gnus-group-group-name))
2710         (setq group nil)))
2711     result))
2712
2713 (defun gnus-summary-read-group-1 (group show-all no-article
2714                                         kill-buffer no-display
2715                                         &optional select-articles)
2716   ;; Killed foreign groups can't be entered.
2717   (when (and (not (gnus-group-native-p group))
2718              (not (gnus-gethash group gnus-newsrc-hashtb)))
2719     (error "Dead non-native groups can't be entered"))
2720   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2721   (let* ((new-group (gnus-summary-setup-buffer group))
2722          (quit-config (gnus-group-quit-config group))
2723          (did-select (and new-group (gnus-select-newsgroup
2724                                      group show-all select-articles))))
2725     (cond
2726      ;; This summary buffer exists already, so we just select it.
2727      ((not new-group)
2728       (gnus-set-global-variables)
2729       (when kill-buffer
2730         (gnus-kill-or-deaden-summary kill-buffer))
2731       (gnus-configure-windows 'summary 'force)
2732       (gnus-set-mode-line 'summary)
2733       (gnus-summary-position-point)
2734       (message "")
2735       t)
2736      ;; We couldn't select this group.
2737      ((null did-select)
2738       (when (and (eq major-mode 'gnus-summary-mode)
2739                  (not (equal (current-buffer) kill-buffer)))
2740         (kill-buffer (current-buffer))
2741         (if (not quit-config)
2742             (progn
2743               ;; Update the info -- marks might need to be removed,
2744               ;; for instance.
2745               (gnus-summary-update-info)
2746               (set-buffer gnus-group-buffer)
2747               (gnus-group-jump-to-group group)
2748               (gnus-group-next-unread-group 1))
2749           (gnus-handle-ephemeral-exit quit-config)))
2750       (gnus-message 3 "Can't select group")
2751       nil)
2752      ;; The user did a `C-g' while prompting for number of articles,
2753      ;; so we exit this group.
2754      ((eq did-select 'quit)
2755       (and (eq major-mode 'gnus-summary-mode)
2756            (not (equal (current-buffer) kill-buffer))
2757            (kill-buffer (current-buffer)))
2758       (when kill-buffer
2759         (gnus-kill-or-deaden-summary kill-buffer))
2760       (if (not quit-config)
2761           (progn
2762             (set-buffer gnus-group-buffer)
2763             (gnus-group-jump-to-group group)
2764             (gnus-group-next-unread-group 1)
2765             (gnus-configure-windows 'group 'force))
2766         (gnus-handle-ephemeral-exit quit-config))
2767       ;; Finally signal the quit.
2768       (signal 'quit nil))
2769      ;; The group was successfully selected.
2770      (t
2771       (gnus-set-global-variables)
2772       ;; Save the active value in effect when the group was entered.
2773       (setq gnus-newsgroup-active
2774             (gnus-copy-sequence
2775              (gnus-active gnus-newsgroup-name)))
2776       ;; You can change the summary buffer in some way with this hook.
2777       (gnus-run-hooks 'gnus-select-group-hook)
2778       ;; Set any local variables in the group parameters.
2779       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2780       (gnus-update-format-specifications
2781        nil 'summary 'summary-mode 'summary-dummy)
2782       ;; Do score processing.
2783       (when gnus-use-scoring
2784         (gnus-possibly-score-headers))
2785       ;; Check whether to fill in the gaps in the threads.
2786       (when gnus-build-sparse-threads
2787         (gnus-build-sparse-threads))
2788       ;; Find the initial limit.
2789       (if gnus-show-threads
2790           (if show-all
2791               (let ((gnus-newsgroup-dormant nil))
2792                 (gnus-summary-initial-limit show-all))
2793             (gnus-summary-initial-limit show-all))
2794         (setq gnus-newsgroup-limit
2795               (mapcar
2796                (lambda (header) (mail-header-number header))
2797                gnus-newsgroup-headers)))
2798       ;; Generate the summary buffer.
2799       (unless no-display
2800         (gnus-summary-prepare))
2801       (when gnus-use-trees
2802         (gnus-tree-open group)
2803         (setq gnus-summary-highlight-line-function
2804               'gnus-tree-highlight-article))
2805       ;; If the summary buffer is empty, but there are some low-scored
2806       ;; articles or some excluded dormants, we include these in the
2807       ;; buffer.
2808       (when (and (zerop (buffer-size))
2809                  (not no-display))
2810         (cond (gnus-newsgroup-dormant
2811                (gnus-summary-limit-include-dormant))
2812               ((and gnus-newsgroup-scored show-all)
2813                (gnus-summary-limit-include-expunged t))))
2814       ;; Function `gnus-apply-kill-file' must be called in this hook.
2815       (gnus-run-hooks 'gnus-apply-kill-hook)
2816       (if (and (zerop (buffer-size))
2817                (not no-display))
2818           (progn
2819             ;; This newsgroup is empty.
2820             (gnus-summary-catchup-and-exit nil t)
2821             (gnus-message 6 "No unread news")
2822             (when kill-buffer
2823               (gnus-kill-or-deaden-summary kill-buffer))
2824             ;; Return nil from this function.
2825             nil)
2826         ;; Hide conversation thread subtrees.  We cannot do this in
2827         ;; gnus-summary-prepare-hook since kill processing may not
2828         ;; work with hidden articles.
2829         (and gnus-show-threads
2830              gnus-thread-hide-subtree
2831              (gnus-summary-hide-all-threads))
2832         (when kill-buffer
2833           (gnus-kill-or-deaden-summary kill-buffer))
2834         ;; Show first unread article if requested.
2835         (if (and (not no-article)
2836                  (not no-display)
2837                  gnus-newsgroup-unreads
2838                  gnus-auto-select-first)
2839             (progn
2840               (gnus-configure-windows 'summary)
2841               (cond
2842                ((eq gnus-auto-select-first 'best)
2843                 (gnus-summary-best-unread-article))
2844                ((eq gnus-auto-select-first t)
2845                 (gnus-summary-first-unread-article))
2846                ((gnus-functionp gnus-auto-select-first)
2847                 (funcall gnus-auto-select-first))))
2848           ;; Don't select any articles, just move point to the first
2849           ;; article in the group.
2850           (goto-char (point-min))
2851           (gnus-summary-position-point)
2852           (gnus-configure-windows 'summary 'force)
2853           (gnus-set-mode-line 'summary))
2854         (when (get-buffer-window gnus-group-buffer t)
2855           ;; Gotta use windows, because recenter does weird stuff if
2856           ;; the current buffer ain't the displayed window.
2857           (let ((owin (selected-window)))
2858             (select-window (get-buffer-window gnus-group-buffer t))
2859             (when (gnus-group-goto-group group)
2860               (recenter))
2861             (select-window owin)))
2862         ;; Mark this buffer as "prepared".
2863         (setq gnus-newsgroup-prepared t)
2864         (gnus-run-hooks 'gnus-summary-prepared-hook)
2865         t)))))
2866
2867 (defun gnus-summary-prepare ()
2868   "Generate the summary buffer."
2869   (interactive)
2870   (let ((buffer-read-only nil))
2871     (erase-buffer)
2872     (setq gnus-newsgroup-data nil
2873           gnus-newsgroup-data-reverse nil)
2874     (gnus-run-hooks 'gnus-summary-generate-hook)
2875     ;; Generate the buffer, either with threads or without.
2876     (when gnus-newsgroup-headers
2877       (gnus-summary-prepare-threads
2878        (if gnus-show-threads
2879            (gnus-sort-gathered-threads
2880             (funcall gnus-summary-thread-gathering-function
2881                      (gnus-sort-threads
2882                       (gnus-cut-threads (gnus-make-threads)))))
2883          ;; Unthreaded display.
2884          (gnus-sort-articles gnus-newsgroup-headers))))
2885     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2886     ;; Call hooks for modifying summary buffer.
2887     (goto-char (point-min))
2888     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2889
2890 (defsubst gnus-general-simplify-subject (subject)
2891   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2892   (setq subject
2893         (cond
2894          ;; Truncate the subject.
2895          (gnus-simplify-subject-functions
2896           (gnus-map-function gnus-simplify-subject-functions subject))
2897          ((numberp gnus-summary-gather-subject-limit)
2898           (setq subject (gnus-simplify-subject-re subject))
2899           (if (> (length subject) gnus-summary-gather-subject-limit)
2900               (substring subject 0 gnus-summary-gather-subject-limit)
2901             subject))
2902          ;; Fuzzily simplify it.
2903          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2904           (gnus-simplify-subject-fuzzy subject))
2905          ;; Just remove the leading "Re:".
2906          (t
2907           (gnus-simplify-subject-re subject))))
2908
2909   (if (and gnus-summary-gather-exclude-subject
2910            (string-match gnus-summary-gather-exclude-subject subject))
2911       nil                               ; This article shouldn't be gathered
2912     subject))
2913
2914 (defun gnus-summary-simplify-subject-query ()
2915   "Query where the respool algorithm would put this article."
2916   (interactive)
2917   (gnus-summary-select-article)
2918   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2919
2920 (defun gnus-gather-threads-by-subject (threads)
2921   "Gather threads by looking at Subject headers."
2922   (if (not gnus-summary-make-false-root)
2923       threads
2924     (let ((hashtb (gnus-make-hashtable 1024))
2925           (prev threads)
2926           (result threads)
2927           subject hthread whole-subject)
2928       (while threads
2929         (setq subject (gnus-general-simplify-subject
2930                        (setq whole-subject (mail-header-subject
2931                                             (caar threads)))))
2932         (when subject
2933           (if (setq hthread (gnus-gethash subject hashtb))
2934               (progn
2935                 ;; We enter a dummy root into the thread, if we
2936                 ;; haven't done that already.
2937                 (unless (stringp (caar hthread))
2938                   (setcar hthread (list whole-subject (car hthread))))
2939                 ;; We add this new gathered thread to this gathered
2940                 ;; thread.
2941                 (setcdr (car hthread)
2942                         (nconc (cdar hthread) (list (car threads))))
2943                 ;; Remove it from the list of threads.
2944                 (setcdr prev (cdr threads))
2945                 (setq threads prev))
2946             ;; Enter this thread into the hash table.
2947             (gnus-sethash subject threads hashtb)))
2948         (setq prev threads)
2949         (setq threads (cdr threads)))
2950       result)))
2951
2952 (defun gnus-gather-threads-by-references (threads)
2953   "Gather threads by looking at References headers."
2954   (let ((idhashtb (gnus-make-hashtable 1024))
2955         (thhashtb (gnus-make-hashtable 1024))
2956         (prev threads)
2957         (result threads)
2958         ids references id gthread gid entered ref)
2959     (while threads
2960       (when (setq references (mail-header-references (caar threads)))
2961         (setq id (mail-header-id (caar threads))
2962               ids (gnus-split-references references)
2963               entered nil)
2964         (while (setq ref (pop ids))
2965           (setq ids (delete ref ids))
2966           (if (not (setq gid (gnus-gethash ref idhashtb)))
2967               (progn
2968                 (gnus-sethash ref id idhashtb)
2969                 (gnus-sethash id threads thhashtb))
2970             (setq gthread (gnus-gethash gid thhashtb))
2971             (unless entered
2972               ;; We enter a dummy root into the thread, if we
2973               ;; haven't done that already.
2974               (unless (stringp (caar gthread))
2975                 (setcar gthread (list (mail-header-subject (caar gthread))
2976                                       (car gthread))))
2977               ;; We add this new gathered thread to this gathered
2978               ;; thread.
2979               (setcdr (car gthread)
2980                       (nconc (cdar gthread) (list (car threads)))))
2981             ;; Add it into the thread hash table.
2982             (gnus-sethash id gthread thhashtb)
2983             (setq entered t)
2984             ;; Remove it from the list of threads.
2985             (setcdr prev (cdr threads))
2986             (setq threads prev))))
2987       (setq prev threads)
2988       (setq threads (cdr threads)))
2989     result))
2990
2991 (defun gnus-sort-gathered-threads (threads)
2992   "Sort subtreads inside each gathered thread by article number."
2993   (let ((result threads))
2994     (while threads
2995       (when (stringp (caar threads))
2996         (setcdr (car threads)
2997                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2998       (setq threads (cdr threads)))
2999     result))
3000
3001 (defun gnus-thread-loop-p (root thread)
3002   "Say whether ROOT is in THREAD."
3003   (let ((stack (list thread))
3004         (infloop 0)
3005         th)
3006     (while (setq thread (pop stack))
3007       (setq th (cdr thread))
3008       (while (and th
3009                   (not (eq (caar th) root)))
3010         (pop th))
3011       (if th
3012           ;; We have found a loop.
3013           (let (ref-dep)
3014             (setcdr thread (delq (car th) (cdr thread)))
3015             (if (boundp (setq ref-dep (intern "none"
3016                                               gnus-newsgroup-dependencies)))
3017                 (setcdr (symbol-value ref-dep)
3018                         (nconc (cdr (symbol-value ref-dep))
3019                                (list (car th))))
3020               (set ref-dep (list nil (car th))))
3021             (setq infloop 1
3022                   stack nil))
3023         ;; Push all the subthreads onto the stack.
3024         (push (cdr thread) stack)))
3025     infloop))
3026
3027 (defun gnus-make-threads ()
3028   "Go through the dependency hashtb and find the roots.  Return all threads."
3029   (let (threads)
3030     (while (catch 'infloop
3031              (mapatoms
3032               (lambda (refs)
3033                 ;; Deal with self-referencing References loops.
3034                 (when (and (car (symbol-value refs))
3035                            (not (zerop
3036                                  (apply
3037                                   '+
3038                                   (mapcar
3039                                    (lambda (thread)
3040                                      (gnus-thread-loop-p
3041                                       (car (symbol-value refs)) thread))
3042                                    (cdr (symbol-value refs)))))))
3043                   (setq threads nil)
3044                   (throw 'infloop t))
3045                 (unless (car (symbol-value refs))
3046                   ;; These threads do not refer back to any other articles,
3047                   ;; so they're roots.
3048                   (setq threads (append (cdr (symbol-value refs)) threads))))
3049               gnus-newsgroup-dependencies)))
3050     threads))
3051
3052 ;; Build the thread tree.
3053 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3054   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3055
3056 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3057 if it was already present.
3058
3059 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3060 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3061 Message-IDs will be renamed be renamed to a unique Message-ID before
3062 being entered.
3063
3064 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3065   (let* ((id (mail-header-id header))
3066          (id-dep (and id (intern id dependencies)))
3067          ref ref-dep ref-header)
3068     ;; Enter this `header' in the `dependencies' table.
3069     (cond
3070      ((not id-dep)
3071       (setq header nil))
3072      ;; The first two cases do the normal part: enter a new `header'
3073      ;; in the `dependencies' table.
3074      ((not (boundp id-dep))
3075       (set id-dep (list header)))
3076      ((null (car (symbol-value id-dep)))
3077       (setcar (symbol-value id-dep) header))
3078
3079      ;; From here the `header' was already present in the
3080      ;; `dependencies' table.
3081      (force-new
3082       ;; Overrides an existing entry;
3083       ;; just set the header part of the entry.
3084       (setcar (symbol-value id-dep) header))
3085
3086      ;; Renames the existing `header' to a unique Message-ID.
3087      ((not gnus-summary-ignore-duplicates)
3088       ;; An article with this Message-ID has already been seen.
3089       ;; We rename the Message-ID.
3090       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3091            (list header))
3092       (mail-header-set-id header id))
3093
3094      ;; The last case ignores an existing entry, except it adds any
3095      ;; additional Xrefs (in case the two articles came from different
3096      ;; servers.
3097      ;; Also sets `header' to `nil' meaning that the `dependencies'
3098      ;; table was *not* modified.
3099      (t
3100       (mail-header-set-xref
3101        (car (symbol-value id-dep))
3102        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3103                    "")
3104                (or (mail-header-xref header) "")))
3105       (setq header nil)))
3106
3107     (when header
3108       ;; First check if that we are not creating a References loop.
3109       (setq ref (gnus-parent-id (mail-header-references header)))
3110       (while (and ref
3111                   (setq ref-dep (intern-soft ref dependencies))
3112                   (boundp ref-dep)
3113                   (setq ref-header (car (symbol-value ref-dep))))
3114         (if (string= id ref)
3115             ;; Yuk!  This is a reference loop.  Make the article be a
3116             ;; root article.
3117             (progn
3118               (mail-header-set-references (car (symbol-value id-dep)) "none")
3119               (setq ref nil))
3120           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3121       (setq ref (gnus-parent-id (mail-header-references header)))
3122       (setq ref-dep (intern (or ref "none") dependencies))
3123       (if (boundp ref-dep)
3124           (setcdr (symbol-value ref-dep)
3125                   (nconc (cdr (symbol-value ref-dep))
3126                          (list (symbol-value id-dep))))
3127         (set ref-dep (list nil (symbol-value id-dep)))))
3128     header))
3129
3130 (defun gnus-build-sparse-threads ()
3131   (let ((headers gnus-newsgroup-headers)
3132         (gnus-summary-ignore-duplicates t)
3133         header references generation relations
3134         subject child end new-child date)
3135     ;; First we create an alist of generations/relations, where
3136     ;; generations is how much we trust the relation, and the relation
3137     ;; is parent/child.
3138     (gnus-message 7 "Making sparse threads...")
3139     (save-excursion
3140       (nnheader-set-temp-buffer " *gnus sparse threads*")
3141       (while (setq header (pop headers))
3142         (when (and (setq references (mail-header-references header))
3143                    (not (string= references "")))
3144           (insert references)
3145           (setq child (mail-header-id header)
3146                 subject (mail-header-subject header)
3147                 date (mail-header-date header)
3148                 generation 0)
3149           (while (search-backward ">" nil t)
3150             (setq end (1+ (point)))
3151             (when (search-backward "<" nil t)
3152               (setq new-child (buffer-substring (point) end))
3153               (push (list (incf generation)
3154                           child (setq child new-child)
3155                           subject date)
3156                     relations)))
3157           (when child
3158             (push (list (1+ generation) child nil subject) relations))
3159           (erase-buffer)))
3160       (kill-buffer (current-buffer)))
3161     ;; Sort over trustworthiness.
3162     (mapcar
3163      (lambda (relation)
3164        (when (gnus-dependencies-add-header
3165               (make-full-mail-header
3166                gnus-reffed-article-number
3167                (nth 3 relation) "" (or (nth 4 relation) "")
3168                (nth 1 relation)
3169                (or (nth 2 relation) "") 0 0 "")
3170               gnus-newsgroup-dependencies nil)
3171          (push gnus-reffed-article-number gnus-newsgroup-limit)
3172          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3173          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3174                gnus-newsgroup-reads)
3175          (decf gnus-reffed-article-number)))
3176      (sort relations 'car-less-than-car))
3177     (gnus-message 7 "Making sparse threads...done")))
3178
3179 (defun gnus-build-old-threads ()
3180   ;; Look at all the articles that refer back to old articles, and
3181   ;; fetch the headers for the articles that aren't there.  This will
3182   ;; build complete threads - if the roots haven't been expired by the
3183   ;; server, that is.
3184   (let (id heads)
3185     (mapatoms
3186      (lambda (refs)
3187        (when (not (car (symbol-value refs)))
3188          (setq heads (cdr (symbol-value refs)))
3189          (while heads
3190            (if (memq (mail-header-number (caar heads))
3191                      gnus-newsgroup-dormant)
3192                (setq heads (cdr heads))
3193              (setq id (symbol-name refs))
3194              (while (and (setq id (gnus-build-get-header id))
3195                          (not (car (gnus-id-to-thread id)))))
3196              (setq heads nil)))))
3197      gnus-newsgroup-dependencies)))
3198
3199 ;; This function has to be called with point after the article number
3200 ;; on the beginning of the line.
3201 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3202   (let ((eol (gnus-point-at-eol))
3203         (buffer (current-buffer))
3204         header)
3205
3206     ;; overview: [num subject from date id refs chars lines misc]
3207     (unwind-protect
3208         (progn
3209           (narrow-to-region (point) eol)
3210           (unless (eobp)
3211             (forward-char))
3212
3213           (setq header
3214                 (make-full-mail-header
3215                  number                         ; number
3216                  (funcall gnus-decode-encoded-word-function
3217                           (nnheader-nov-field)) ; subject
3218                  (funcall gnus-decode-encoded-word-function
3219                           (nnheader-nov-field)) ; from
3220                  (nnheader-nov-field)           ; date
3221                  (nnheader-nov-read-message-id) ; id
3222                  (nnheader-nov-field)           ; refs
3223                  (nnheader-nov-read-integer)    ; chars
3224                  (nnheader-nov-read-integer)    ; lines
3225                  (unless (eobp)
3226                    (nnheader-nov-field))                ; misc
3227                  (nnheader-nov-parse-extra))))  ; extra
3228
3229       (widen))
3230
3231     (when gnus-alter-header-function
3232       (funcall gnus-alter-header-function header))
3233     (gnus-dependencies-add-header header dependencies force-new)))
3234
3235 (defun gnus-build-get-header (id)
3236   ;; Look through the buffer of NOV lines and find the header to
3237   ;; ID.  Enter this line into the dependencies hash table, and return
3238   ;; the id of the parent article (if any).
3239   (let ((deps gnus-newsgroup-dependencies)
3240         found header)
3241     (prog1
3242         (save-excursion
3243           (set-buffer nntp-server-buffer)
3244           (let ((case-fold-search nil))
3245             (goto-char (point-min))
3246             (while (and (not found)
3247                         (search-forward id nil t))
3248               (beginning-of-line)
3249               (setq found (looking-at
3250                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3251                                    (regexp-quote id))))
3252               (or found (beginning-of-line 2)))
3253             (when found
3254               (beginning-of-line)
3255               (and
3256                (setq header (gnus-nov-parse-line
3257                              (read (current-buffer)) deps))
3258                (gnus-parent-id (mail-header-references header))))))
3259       (when header
3260         (let ((number (mail-header-number header)))
3261           (push number gnus-newsgroup-limit)
3262           (push header gnus-newsgroup-headers)
3263           (if (memq number gnus-newsgroup-unselected)
3264               (progn
3265                 (push number gnus-newsgroup-unreads)
3266                 (setq gnus-newsgroup-unselected
3267                       (delq number gnus-newsgroup-unselected)))
3268             (push number gnus-newsgroup-ancient)))))))
3269
3270 (defun gnus-build-all-threads ()
3271   "Read all the headers."
3272   (let ((gnus-summary-ignore-duplicates t)
3273         (dependencies gnus-newsgroup-dependencies)
3274         header article)
3275     (save-excursion
3276       (set-buffer nntp-server-buffer)
3277       (let ((case-fold-search nil))
3278         (goto-char (point-min))
3279         (while (not (eobp))
3280           (ignore-errors
3281             (setq article (read (current-buffer))
3282                   header (gnus-nov-parse-line
3283                           article dependencies)))
3284           (when header
3285             (save-excursion
3286               (set-buffer gnus-summary-buffer)
3287               (push header gnus-newsgroup-headers)
3288               (if (memq (setq article (mail-header-number header))
3289                         gnus-newsgroup-unselected)
3290                   (progn
3291                     (push article gnus-newsgroup-unreads)
3292                     (setq gnus-newsgroup-unselected
3293                           (delq article gnus-newsgroup-unselected)))
3294                 (push article gnus-newsgroup-ancient)))
3295             (forward-line 1)))))))
3296
3297 (defun gnus-summary-update-article-line (article header)
3298   "Update the line for ARTICLE using HEADERS."
3299   (let* ((id (mail-header-id header))
3300          (thread (gnus-id-to-thread id)))
3301     (unless thread
3302       (error "Article in no thread"))
3303     ;; Update the thread.
3304     (setcar thread header)
3305     (gnus-summary-goto-subject article)
3306     (let* ((datal (gnus-data-find-list article))
3307            (data (car datal))
3308            (length (when (cdr datal)
3309                      (- (gnus-data-pos data)
3310                         (gnus-data-pos (cadr datal)))))
3311            (buffer-read-only nil)
3312            (level (gnus-summary-thread-level)))
3313       (gnus-delete-line)
3314       (gnus-summary-insert-line
3315        header level nil (gnus-article-mark article)
3316        (memq article gnus-newsgroup-replied)
3317        (memq article gnus-newsgroup-expirable)
3318        ;; Only insert the Subject string when it's different
3319        ;; from the previous Subject string.
3320        (if (gnus-subject-equal
3321             (condition-case ()
3322                 (mail-header-subject
3323                  (gnus-data-header
3324                   (cadr
3325                    (gnus-data-find-list
3326                     article
3327                     (gnus-data-list t)))))
3328               ;; Error on the side of excessive subjects.
3329               (error ""))
3330             (mail-header-subject header))
3331            ""
3332          (mail-header-subject header))
3333        nil (cdr (assq article gnus-newsgroup-scored))
3334        (memq article gnus-newsgroup-processable))
3335       (when length
3336         (gnus-data-update-list
3337          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3338
3339 (defun gnus-summary-update-article (article &optional iheader)
3340   "Update ARTICLE in the summary buffer."
3341   (set-buffer gnus-summary-buffer)
3342   (let* ((header (gnus-summary-article-header article))
3343          (id (mail-header-id header))
3344          (data (gnus-data-find article))
3345          (thread (gnus-id-to-thread id))
3346          (references (mail-header-references header))
3347          (parent
3348           (gnus-id-to-thread
3349            (or (gnus-parent-id
3350                 (when (and references
3351                            (not (equal "" references)))
3352                   references))
3353                "none")))
3354          (buffer-read-only nil)
3355          (old (car thread)))
3356     (when thread
3357       (unless iheader
3358         (setcar thread nil)
3359         (when parent
3360           (delq thread parent)))
3361       (if (gnus-summary-insert-subject id header)
3362           ;; Set the (possibly) new article number in the data structure.
3363           (gnus-data-set-number data (gnus-id-to-article id))
3364         (setcar thread old)
3365         nil))))
3366
3367 (defun gnus-rebuild-thread (id &optional line)
3368   "Rebuild the thread containing ID.
3369 If LINE, insert the rebuilt thread starting on line LINE."
3370   (let ((buffer-read-only nil)
3371         old-pos current thread data)
3372     (if (not gnus-show-threads)
3373         (setq thread (list (car (gnus-id-to-thread id))))
3374       ;; Get the thread this article is part of.
3375       (setq thread (gnus-remove-thread id)))
3376     (setq old-pos (gnus-point-at-bol))
3377     (setq current (save-excursion
3378                     (and (zerop (forward-line -1))
3379                          (gnus-summary-article-number))))
3380     ;; If this is a gathered thread, we have to go some re-gathering.
3381     (when (stringp (car thread))
3382       (let ((subject (car thread))
3383             roots thr)
3384         (setq thread (cdr thread))
3385         (while thread
3386           (unless (memq (setq thr (gnus-id-to-thread
3387                                    (gnus-root-id
3388                                     (mail-header-id (caar thread)))))
3389                         roots)
3390             (push thr roots))
3391           (setq thread (cdr thread)))
3392         ;; We now have all (unique) roots.
3393         (if (= (length roots) 1)
3394             ;; All the loose roots are now one solid root.
3395             (setq thread (car roots))
3396           (setq thread (cons subject (gnus-sort-threads roots))))))
3397     (let (threads)
3398       ;; We then insert this thread into the summary buffer.
3399       (when line
3400         (goto-char (point-min))
3401         (forward-line (1- line)))
3402       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3403         (if gnus-show-threads
3404             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3405           (gnus-summary-prepare-unthreaded thread))
3406         (setq data (nreverse gnus-newsgroup-data))
3407         (setq threads gnus-newsgroup-threads))
3408       ;; We splice the new data into the data structure.
3409       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3410       ;;!!! then we want to insert at the beginning of the buffer.
3411       ;;!!! That happens to be true with Gnus now, but that may
3412       ;;!!! change in the future.  Perhaps.
3413       (gnus-data-enter-list
3414        (if line nil current) data (- (point) old-pos))
3415       (setq gnus-newsgroup-threads
3416             (nconc threads gnus-newsgroup-threads))
3417       (gnus-data-compute-positions))))
3418
3419 (defun gnus-number-to-header (number)
3420   "Return the header for article NUMBER."
3421   (let ((headers gnus-newsgroup-headers))
3422     (while (and headers
3423                 (not (= number (mail-header-number (car headers)))))
3424       (pop headers))
3425     (when headers
3426       (car headers))))
3427
3428 (defun gnus-parent-headers (in-headers &optional generation)
3429   "Return the headers of the GENERATIONeth parent of HEADERS."
3430   (unless generation
3431     (setq generation 1))
3432   (let ((parent t)
3433         (headers in-headers)
3434         references)
3435     (while (and parent
3436                 (not (zerop generation))
3437                 (setq references (mail-header-references headers)))
3438       (setq headers (if (and references
3439                              (setq parent (gnus-parent-id references)))
3440                         (car (gnus-id-to-thread parent))
3441                       nil))
3442       (decf generation))
3443     (and (not (eq headers in-headers))
3444          headers)))
3445
3446 (defun gnus-id-to-thread (id)
3447   "Return the (sub-)thread where ID appears."
3448   (gnus-gethash id gnus-newsgroup-dependencies))
3449
3450 (defun gnus-id-to-article (id)
3451   "Return the article number of ID."
3452   (let ((thread (gnus-id-to-thread id)))
3453     (when (and thread
3454                (car thread))
3455       (mail-header-number (car thread)))))
3456
3457 (defun gnus-id-to-header (id)
3458   "Return the article headers of ID."
3459   (car (gnus-id-to-thread id)))
3460
3461 (defun gnus-article-displayed-root-p (article)
3462   "Say whether ARTICLE is a root(ish) article."
3463   (let ((level (gnus-summary-thread-level article))
3464         (refs (mail-header-references  (gnus-summary-article-header article)))
3465         particle)
3466     (cond
3467      ((null level) nil)
3468      ((zerop level) t)
3469      ((null refs) t)
3470      ((null (gnus-parent-id refs)) t)
3471      ((and (= 1 level)
3472            (null (setq particle (gnus-id-to-article
3473                                  (gnus-parent-id refs))))
3474            (null (gnus-summary-thread-level particle)))))))
3475
3476 (defun gnus-root-id (id)
3477   "Return the id of the root of the thread where ID appears."
3478   (let (last-id prev)
3479     (while (and id (setq prev (car (gnus-id-to-thread id))))
3480       (setq last-id id
3481             id (gnus-parent-id (mail-header-references prev))))
3482     last-id))
3483
3484 (defun gnus-articles-in-thread (thread)
3485   "Return the list of articles in THREAD."
3486   (cons (mail-header-number (car thread))
3487         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3488
3489 (defun gnus-remove-thread (id &optional dont-remove)
3490   "Remove the thread that has ID in it."
3491   (let (headers thread last-id)
3492     ;; First go up in this thread until we find the root.
3493     (setq last-id (gnus-root-id id)
3494           headers (message-flatten-list (gnus-id-to-thread last-id)))
3495     ;; We have now found the real root of this thread.  It might have
3496     ;; been gathered into some loose thread, so we have to search
3497     ;; through the threads to find the thread we wanted.
3498     (let ((threads gnus-newsgroup-threads)
3499           sub)
3500       (while threads
3501         (setq sub (car threads))
3502         (if (stringp (car sub))
3503             ;; This is a gathered thread, so we look at the roots
3504             ;; below it to find whether this article is in this
3505             ;; gathered root.
3506             (progn
3507               (setq sub (cdr sub))
3508               (while sub
3509                 (when (member (caar sub) headers)
3510                   (setq thread (car threads)
3511                         threads nil
3512                         sub nil))
3513                 (setq sub (cdr sub))))
3514           ;; It's an ordinary thread, so we check it.
3515           (when (eq (car sub) (car headers))
3516             (setq thread sub
3517                   threads nil)))
3518         (setq threads (cdr threads)))
3519       ;; If this article is in no thread, then it's a root.
3520       (if thread
3521           (unless dont-remove
3522             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3523         (setq thread (gnus-id-to-thread last-id)))
3524       (when thread
3525         (prog1
3526             thread                      ; We return this thread.
3527           (unless dont-remove
3528             (if (stringp (car thread))
3529                 (progn
3530                   ;; If we use dummy roots, then we have to remove the
3531                   ;; dummy root as well.
3532                   (when (eq gnus-summary-make-false-root 'dummy)
3533                     ;; We go to the dummy root by going to
3534                     ;; the first sub-"thread", and then one line up.
3535                     (gnus-summary-goto-article
3536                      (mail-header-number (caadr thread)))
3537                     (forward-line -1)
3538                     (gnus-delete-line)
3539                     (gnus-data-compute-positions))
3540                   (setq thread (cdr thread))
3541                   (while thread
3542                     (gnus-remove-thread-1 (car thread))
3543                     (setq thread (cdr thread))))
3544               (gnus-summary-show-all-threads)
3545               (gnus-remove-thread-1 thread))))))))
3546
3547 (defun gnus-remove-thread-1 (thread)
3548   "Remove the thread THREAD recursively."
3549   (let ((number (mail-header-number (pop thread)))
3550         d)
3551     (setq thread (reverse thread))
3552     (while thread
3553       (gnus-remove-thread-1 (pop thread)))
3554     (when (setq d (gnus-data-find number))
3555       (goto-char (gnus-data-pos d))
3556       (gnus-data-remove
3557        number
3558        (- (gnus-point-at-bol)
3559           (prog1
3560               (1+ (gnus-point-at-eol))
3561             (gnus-delete-line)))))))
3562
3563 (defun gnus-sort-threads (threads)
3564   "Sort THREADS."
3565   (if (not gnus-thread-sort-functions)
3566       threads
3567     (gnus-message 8 "Sorting threads...")
3568     (prog1
3569         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3570       (gnus-message 8 "Sorting threads...done"))))
3571
3572 (defun gnus-sort-articles (articles)
3573   "Sort ARTICLES."
3574   (when gnus-article-sort-functions
3575     (gnus-message 7 "Sorting articles...")
3576     (prog1
3577         (setq gnus-newsgroup-headers
3578               (sort articles (gnus-make-sort-function
3579                               gnus-article-sort-functions)))
3580       (gnus-message 7 "Sorting articles...done"))))
3581
3582 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3583 (defmacro gnus-thread-header (thread)
3584   ;; Return header of first article in THREAD.
3585   ;; Note that THREAD must never, ever be anything else than a variable -
3586   ;; using some other form will lead to serious barfage.
3587   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3588   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3589   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3590         (vector thread) 2))
3591
3592 (defsubst gnus-article-sort-by-number (h1 h2)
3593   "Sort articles by article number."
3594   (< (mail-header-number h1)
3595      (mail-header-number h2)))
3596
3597 (defun gnus-thread-sort-by-number (h1 h2)
3598   "Sort threads by root article number."
3599   (gnus-article-sort-by-number
3600    (gnus-thread-header h1) (gnus-thread-header h2)))
3601
3602 (defsubst gnus-article-sort-by-lines (h1 h2)
3603   "Sort articles by article Lines header."
3604   (< (mail-header-lines h1)
3605      (mail-header-lines h2)))
3606
3607 (defun gnus-thread-sort-by-lines (h1 h2)
3608   "Sort threads by root article Lines header."
3609   (gnus-article-sort-by-lines
3610    (gnus-thread-header h1) (gnus-thread-header h2)))
3611
3612 (defsubst gnus-article-sort-by-chars (h1 h2)
3613   "Sort articles by octet length."
3614   (< (mail-header-chars h1)
3615      (mail-header-chars h2)))
3616
3617 (defun gnus-thread-sort-by-chars (h1 h2)
3618   "Sort threads by root article octet length."
3619   (gnus-article-sort-by-chars
3620    (gnus-thread-header h1) (gnus-thread-header h2)))
3621
3622 (defsubst gnus-article-sort-by-author (h1 h2)
3623   "Sort articles by root author."
3624   (string-lessp
3625    (let ((extract (funcall
3626                    gnus-extract-address-components
3627                    (mail-header-from h1))))
3628      (or (car extract) (cadr extract) ""))
3629    (let ((extract (funcall
3630                    gnus-extract-address-components
3631                    (mail-header-from h2))))
3632      (or (car extract) (cadr extract) ""))))
3633
3634 (defun gnus-thread-sort-by-author (h1 h2)
3635   "Sort threads by root author."
3636   (gnus-article-sort-by-author
3637    (gnus-thread-header h1)  (gnus-thread-header h2)))
3638
3639 (defsubst gnus-article-sort-by-subject (h1 h2)
3640   "Sort articles by root subject."
3641   (string-lessp
3642    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3643    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3644
3645 (defun gnus-thread-sort-by-subject (h1 h2)
3646   "Sort threads by root subject."
3647   (gnus-article-sort-by-subject
3648    (gnus-thread-header h1) (gnus-thread-header h2)))
3649
3650 (defsubst gnus-article-sort-by-date (h1 h2)
3651   "Sort articles by root article date."
3652   (time-less-p
3653    (gnus-date-get-time (mail-header-date h1))
3654    (gnus-date-get-time (mail-header-date h2))))
3655
3656 (defun gnus-thread-sort-by-date (h1 h2)
3657   "Sort threads by root article date."
3658   (gnus-article-sort-by-date
3659    (gnus-thread-header h1) (gnus-thread-header h2)))
3660
3661 (defsubst gnus-article-sort-by-score (h1 h2)
3662   "Sort articles by root article score.
3663 Unscored articles will be counted as having a score of zero."
3664   (> (or (cdr (assq (mail-header-number h1)
3665                     gnus-newsgroup-scored))
3666          gnus-summary-default-score 0)
3667      (or (cdr (assq (mail-header-number h2)
3668                     gnus-newsgroup-scored))
3669          gnus-summary-default-score 0)))
3670
3671 (defun gnus-thread-sort-by-score (h1 h2)
3672   "Sort threads by root article score."
3673   (gnus-article-sort-by-score
3674    (gnus-thread-header h1) (gnus-thread-header h2)))
3675
3676 (defun gnus-thread-sort-by-total-score (h1 h2)
3677   "Sort threads by the sum of all scores in the thread.
3678 Unscored articles will be counted as having a score of zero."
3679   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3680
3681 (defun gnus-thread-total-score (thread)
3682   ;; This function find the total score of THREAD.
3683   (cond ((null thread)
3684          0)
3685         ((consp thread)
3686          (if (stringp (car thread))
3687              (apply gnus-thread-score-function 0
3688                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3689            (gnus-thread-total-score-1 thread)))
3690         (t
3691          (gnus-thread-total-score-1 (list thread)))))
3692
3693 (defun gnus-thread-total-score-1 (root)
3694   ;; This function find the total score of the thread below ROOT.
3695   (setq root (car root))
3696   (apply gnus-thread-score-function
3697          (or (append
3698               (mapcar 'gnus-thread-total-score
3699                       (cdr (gnus-id-to-thread (mail-header-id root))))
3700               (when (> (mail-header-number root) 0)
3701                 (list (or (cdr (assq (mail-header-number root)
3702                                      gnus-newsgroup-scored))
3703                           gnus-summary-default-score 0))))
3704              (list gnus-summary-default-score)
3705              '(0))))
3706
3707 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3708 (defvar gnus-tmp-prev-subject nil)
3709 (defvar gnus-tmp-false-parent nil)
3710 (defvar gnus-tmp-root-expunged nil)
3711 (defvar gnus-tmp-dummy-line nil)
3712
3713 (defvar gnus-tmp-header)
3714 (defun gnus-extra-header (type &optional header)
3715   "Return the extra header of TYPE."
3716   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3717       ""))
3718
3719 (defun gnus-summary-prepare-threads (threads)
3720   "Prepare summary buffer from THREADS and indentation LEVEL.
3721 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3722 or a straight list of headers."
3723   (gnus-message 7 "Generating summary...")
3724
3725   (setq gnus-newsgroup-threads threads)
3726   (beginning-of-line)
3727
3728   (let ((gnus-tmp-level 0)
3729         (default-score (or gnus-summary-default-score 0))
3730         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3731         thread number subject stack state gnus-tmp-gathered beg-match
3732         new-roots gnus-tmp-new-adopts thread-end
3733         gnus-tmp-header gnus-tmp-unread
3734         gnus-tmp-replied gnus-tmp-subject-or-nil
3735         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3736         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3737         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3738
3739     (setq gnus-tmp-prev-subject nil)
3740
3741     (if (vectorp (car threads))
3742         ;; If this is a straight (sic) list of headers, then a
3743         ;; threaded summary display isn't required, so we just create
3744         ;; an unthreaded one.
3745         (gnus-summary-prepare-unthreaded threads)
3746
3747       ;; Do the threaded display.
3748
3749       (while (or threads stack gnus-tmp-new-adopts new-roots)
3750
3751         (if (and (= gnus-tmp-level 0)
3752                  (or (not stack)
3753                      (= (caar stack) 0))
3754                  (not gnus-tmp-false-parent)
3755                  (or gnus-tmp-new-adopts new-roots))
3756             (if gnus-tmp-new-adopts
3757                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3758                       thread (list (car gnus-tmp-new-adopts))
3759                       gnus-tmp-header (caar thread)
3760                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3761               (when new-roots
3762                 (setq thread (list (car new-roots))
3763                       gnus-tmp-header (caar thread)
3764                       new-roots (cdr new-roots))))
3765
3766           (if threads
3767               ;; If there are some threads, we do them before the
3768               ;; threads on the stack.
3769               (setq thread threads
3770                     gnus-tmp-header (caar thread))
3771             ;; There were no current threads, so we pop something off
3772             ;; the stack.
3773             (setq state (car stack)
3774                   gnus-tmp-level (car state)
3775                   thread (cdr state)
3776                   stack (cdr stack)
3777                   gnus-tmp-header (caar thread))))
3778
3779         (setq gnus-tmp-false-parent nil)
3780         (setq gnus-tmp-root-expunged nil)
3781         (setq thread-end nil)
3782
3783         (if (stringp gnus-tmp-header)
3784             ;; The header is a dummy root.
3785             (cond
3786              ((eq gnus-summary-make-false-root 'adopt)
3787               ;; We let the first article adopt the rest.
3788               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3789                                                (cddar thread)))
3790               (setq gnus-tmp-gathered
3791                     (nconc (mapcar
3792                             (lambda (h) (mail-header-number (car h)))
3793                             (cddar thread))
3794                            gnus-tmp-gathered))
3795               (setq thread (cons (list (caar thread)
3796                                        (cadar thread))
3797                                  (cdr thread)))
3798               (setq gnus-tmp-level -1
3799                     gnus-tmp-false-parent t))
3800              ((eq gnus-summary-make-false-root 'empty)
3801               ;; We print adopted articles with empty subject fields.
3802               (setq gnus-tmp-gathered
3803                     (nconc (mapcar
3804                             (lambda (h) (mail-header-number (car h)))
3805                             (cddar thread))
3806                            gnus-tmp-gathered))
3807               (setq gnus-tmp-level -1))
3808              ((eq gnus-summary-make-false-root 'dummy)
3809               ;; We remember that we probably want to output a dummy
3810               ;; root.
3811               (setq gnus-tmp-dummy-line gnus-tmp-header)
3812               (setq gnus-tmp-prev-subject gnus-tmp-header))
3813              (t
3814               ;; We do not make a root for the gathered
3815               ;; sub-threads at all.
3816               (setq gnus-tmp-level -1)))
3817
3818           (setq number (mail-header-number gnus-tmp-header)
3819                 subject (mail-header-subject gnus-tmp-header))
3820
3821           (cond
3822            ;; If the thread has changed subject, we might want to make
3823            ;; this subthread into a root.
3824            ((and (null gnus-thread-ignore-subject)
3825                  (not (zerop gnus-tmp-level))
3826                  gnus-tmp-prev-subject
3827                  (not (inline
3828                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3829             (setq new-roots (nconc new-roots (list (car thread)))
3830                   thread-end t
3831                   gnus-tmp-header nil))
3832            ;; If the article lies outside the current limit,
3833            ;; then we do not display it.
3834            ((not (memq number gnus-newsgroup-limit))
3835             (setq gnus-tmp-gathered
3836                   (nconc (mapcar
3837                           (lambda (h) (mail-header-number (car h)))
3838                           (cdar thread))
3839                          gnus-tmp-gathered))
3840             (setq gnus-tmp-new-adopts (if (cdar thread)
3841                                           (append gnus-tmp-new-adopts
3842                                                   (cdar thread))
3843                                         gnus-tmp-new-adopts)
3844                   thread-end t
3845                   gnus-tmp-header nil)
3846             (when (zerop gnus-tmp-level)
3847               (setq gnus-tmp-root-expunged t)))
3848            ;; Perhaps this article is to be marked as read?
3849            ((and gnus-summary-mark-below
3850                  (< (or (cdr (assq number gnus-newsgroup-scored))
3851                         default-score)
3852                     gnus-summary-mark-below)
3853                  ;; Don't touch sparse articles.
3854                  (not (gnus-summary-article-sparse-p number))
3855                  (not (gnus-summary-article-ancient-p number)))
3856             (setq gnus-newsgroup-unreads
3857                   (delq number gnus-newsgroup-unreads))
3858             (if gnus-newsgroup-auto-expire
3859                 (push number gnus-newsgroup-expirable)
3860               (push (cons number gnus-low-score-mark)
3861                     gnus-newsgroup-reads))))
3862
3863           (when gnus-tmp-header
3864             ;; We may have an old dummy line to output before this
3865             ;; article.
3866             (when (and gnus-tmp-dummy-line
3867                        (gnus-subject-equal
3868                         gnus-tmp-dummy-line
3869                         (mail-header-subject gnus-tmp-header)))
3870               (gnus-summary-insert-dummy-line
3871                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3872               (setq gnus-tmp-dummy-line nil))
3873
3874             ;; Compute the mark.
3875             (setq gnus-tmp-unread (gnus-article-mark number))
3876
3877             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3878                                   gnus-tmp-header gnus-tmp-level)
3879                   gnus-newsgroup-data)
3880
3881             ;; Actually insert the line.
3882             (setq
3883              gnus-tmp-subject-or-nil
3884              (cond
3885               ((and gnus-thread-ignore-subject
3886                     gnus-tmp-prev-subject
3887                     (not (inline (gnus-subject-equal
3888                                   gnus-tmp-prev-subject subject))))
3889                subject)
3890               ((zerop gnus-tmp-level)
3891                (if (and (eq gnus-summary-make-false-root 'empty)
3892                         (memq number gnus-tmp-gathered)
3893                         gnus-tmp-prev-subject
3894                         (inline (gnus-subject-equal
3895                                  gnus-tmp-prev-subject subject)))
3896                    gnus-summary-same-subject
3897                  subject))
3898               (t gnus-summary-same-subject)))
3899             (if (and (eq gnus-summary-make-false-root 'adopt)
3900                      (= gnus-tmp-level 1)
3901                      (memq number gnus-tmp-gathered))
3902                 (setq gnus-tmp-opening-bracket ?\<
3903                       gnus-tmp-closing-bracket ?\>)
3904               (setq gnus-tmp-opening-bracket ?\[
3905                     gnus-tmp-closing-bracket ?\]))
3906             (setq
3907              gnus-tmp-indentation
3908              (aref gnus-thread-indent-array gnus-tmp-level)
3909              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3910              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3911                                 gnus-summary-default-score 0)
3912              gnus-tmp-score-char
3913              (if (or (null gnus-summary-default-score)
3914                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3915                          gnus-summary-zcore-fuzz))
3916                  ? ;Whitespace
3917                (if (< gnus-tmp-score gnus-summary-default-score)
3918                    gnus-score-below-mark gnus-score-over-mark))
3919              gnus-tmp-replied
3920              (cond ((memq number gnus-newsgroup-processable)
3921                     gnus-process-mark)
3922                    ((memq number gnus-newsgroup-cached)
3923                     gnus-cached-mark)
3924                    ((memq number gnus-newsgroup-replied)
3925                     gnus-replied-mark)
3926                    ((memq number gnus-newsgroup-saved)
3927                     gnus-saved-mark)
3928                    (t gnus-unread-mark))
3929              gnus-tmp-from (mail-header-from gnus-tmp-header)
3930              gnus-tmp-name
3931              (cond
3932               ((string-match "<[^>]+> *$" gnus-tmp-from)
3933                (setq beg-match (match-beginning 0))
3934                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3935                         (substring gnus-tmp-from (1+ (match-beginning 0))
3936                                    (1- (match-end 0))))
3937                    (substring gnus-tmp-from 0 beg-match)))
3938               ((string-match "(.+)" gnus-tmp-from)
3939                (substring gnus-tmp-from
3940                           (1+ (match-beginning 0)) (1- (match-end 0))))
3941               (t gnus-tmp-from)))
3942             (when (string= gnus-tmp-name "")
3943               (setq gnus-tmp-name gnus-tmp-from))
3944             (unless (numberp gnus-tmp-lines)
3945               (setq gnus-tmp-lines 0))
3946             (gnus-put-text-property
3947              (point)
3948              (progn (eval gnus-summary-line-format-spec) (point))
3949              'gnus-number number)
3950             (when gnus-visual-p
3951               (forward-line -1)
3952               (gnus-run-hooks 'gnus-summary-update-hook)
3953               (forward-line 1))
3954
3955             (setq gnus-tmp-prev-subject subject)))
3956
3957         (when (nth 1 thread)
3958           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3959         (incf gnus-tmp-level)
3960         (setq threads (if thread-end nil (cdar thread)))
3961         (unless threads
3962           (setq gnus-tmp-level 0)))))
3963   (gnus-message 7 "Generating summary...done"))
3964
3965 (defun gnus-summary-prepare-unthreaded (headers)
3966   "Generate an unthreaded summary buffer based on HEADERS."
3967   (let (header number mark)
3968
3969     (beginning-of-line)
3970
3971     (while headers
3972       ;; We may have to root out some bad articles...
3973       (when (memq (setq number (mail-header-number
3974                                 (setq header (pop headers))))
3975                   gnus-newsgroup-limit)
3976         ;; Mark article as read when it has a low score.
3977         (when (and gnus-summary-mark-below
3978                    (< (or (cdr (assq number gnus-newsgroup-scored))
3979                           gnus-summary-default-score 0)
3980                       gnus-summary-mark-below)
3981                    (not (gnus-summary-article-ancient-p number)))
3982           (setq gnus-newsgroup-unreads
3983                 (delq number gnus-newsgroup-unreads))
3984           (if gnus-newsgroup-auto-expire
3985               (push number gnus-newsgroup-expirable)
3986             (push (cons number gnus-low-score-mark)
3987                   gnus-newsgroup-reads)))
3988
3989         (setq mark (gnus-article-mark number))
3990         (push (gnus-data-make number mark (1+ (point)) header 0)
3991               gnus-newsgroup-data)
3992         (gnus-summary-insert-line
3993          header 0 number
3994          mark (memq number gnus-newsgroup-replied)
3995          (memq number gnus-newsgroup-expirable)
3996          (mail-header-subject header) nil
3997          (cdr (assq number gnus-newsgroup-scored))
3998          (memq number gnus-newsgroup-processable))))))
3999
4000 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4001   "Select newsgroup GROUP.
4002 If READ-ALL is non-nil, all articles in the group are selected.
4003 If SELECT-ARTICLES, only select those articles from GROUP."
4004   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4005          ;;!!! Dirty hack; should be removed.
4006          (gnus-summary-ignore-duplicates
4007           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4008               t
4009             gnus-summary-ignore-duplicates))
4010          (info (nth 2 entry))
4011          articles fetched-articles cached)
4012
4013     (unless (gnus-check-server
4014              (setq gnus-current-select-method
4015                    (gnus-find-method-for-group group)))
4016       (error "Couldn't open server"))
4017
4018     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4019         (gnus-activate-group group)     ; Or we can activate it...
4020         (progn                          ; Or we bug out.
4021           (when (equal major-mode 'gnus-summary-mode)
4022             (kill-buffer (current-buffer)))
4023           (error "Couldn't request group %s: %s"
4024                  group (gnus-status-message group))))
4025
4026     (unless (gnus-request-group group t)
4027       (when (equal major-mode 'gnus-summary-mode)
4028         (kill-buffer (current-buffer)))
4029       (error "Couldn't request group %s: %s"
4030              group (gnus-status-message group)))
4031
4032     (setq gnus-newsgroup-name group)
4033     (setq gnus-newsgroup-unselected nil)
4034     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4035     (gnus-summary-setup-default-charset)
4036
4037     ;; Adjust and set lists of article marks.
4038     (when info
4039       (gnus-adjust-marked-articles info))
4040
4041     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4042     (when (gnus-virtual-group-p group)
4043       (setq cached gnus-newsgroup-cached))
4044
4045     (setq gnus-newsgroup-unreads
4046           (gnus-set-difference
4047            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4048            gnus-newsgroup-dormant))
4049
4050     (setq gnus-newsgroup-processable nil)
4051
4052     (gnus-update-read-articles group gnus-newsgroup-unreads)
4053
4054     (if (setq articles select-articles)
4055         (setq gnus-newsgroup-unselected
4056               (gnus-sorted-intersection
4057                gnus-newsgroup-unreads
4058                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4059       (setq articles (gnus-articles-to-read group read-all)))
4060
4061     (cond
4062      ((null articles)
4063       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4064       'quit)
4065      ((eq articles 0) nil)
4066      (t
4067       ;; Init the dependencies hash table.
4068       (setq gnus-newsgroup-dependencies
4069             (gnus-make-hashtable (length articles)))
4070       (gnus-set-global-variables)
4071       ;; Retrieve the headers and read them in.
4072       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4073       (setq gnus-newsgroup-headers
4074             (if (eq 'nov
4075                     (setq gnus-headers-retrieved-by
4076                           (gnus-retrieve-headers
4077                            articles gnus-newsgroup-name
4078                            ;; We might want to fetch old headers, but
4079                            ;; not if there is only 1 article.
4080                            (and (or (and
4081                                      (not (eq gnus-fetch-old-headers 'some))
4082                                      (not (numberp gnus-fetch-old-headers)))
4083                                     (> (length articles) 1))
4084                                 gnus-fetch-old-headers))))
4085                 (gnus-get-newsgroup-headers-xover
4086                  articles nil nil gnus-newsgroup-name t)
4087               (gnus-get-newsgroup-headers)))
4088       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4089
4090       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4091       (when cached
4092         (setq gnus-newsgroup-cached cached))
4093
4094       ;; Suppress duplicates?
4095       (when gnus-suppress-duplicates
4096         (gnus-dup-suppress-articles))
4097
4098       ;; Set the initial limit.
4099       (setq gnus-newsgroup-limit (copy-sequence articles))
4100       ;; Remove canceled articles from the list of unread articles.
4101       (setq gnus-newsgroup-unreads
4102             (gnus-set-sorted-intersection
4103              gnus-newsgroup-unreads
4104              (setq fetched-articles
4105                    (mapcar (lambda (headers) (mail-header-number headers))
4106                            gnus-newsgroup-headers))))
4107       ;; Removed marked articles that do not exist.
4108       (gnus-update-missing-marks
4109        (gnus-sorted-complement fetched-articles articles))
4110       ;; We might want to build some more threads first.
4111       (when (and gnus-fetch-old-headers
4112                  (eq gnus-headers-retrieved-by 'nov))
4113         (if (eq gnus-fetch-old-headers 'invisible)
4114             (gnus-build-all-threads)
4115           (gnus-build-old-threads)))
4116       ;; Let the Gnus agent mark articles as read.
4117       (when gnus-agent
4118         (gnus-agent-get-undownloaded-list))
4119       ;; Check whether auto-expire is to be done in this group.
4120       (setq gnus-newsgroup-auto-expire
4121             (gnus-group-auto-expirable-p group))
4122       ;; Set up the article buffer now, if necessary.
4123       (unless gnus-single-article-buffer
4124         (gnus-article-setup-buffer))
4125       ;; First and last article in this newsgroup.
4126       (when gnus-newsgroup-headers
4127         (setq gnus-newsgroup-begin
4128               (mail-header-number (car gnus-newsgroup-headers))
4129               gnus-newsgroup-end
4130               (mail-header-number
4131                (gnus-last-element gnus-newsgroup-headers))))
4132       ;; GROUP is successfully selected.
4133       (or gnus-newsgroup-headers t)))))
4134
4135 (defun gnus-articles-to-read (group &optional read-all)
4136   ;; Find out what articles the user wants to read.
4137   (let* ((articles
4138           ;; Select all articles if `read-all' is non-nil, or if there
4139           ;; are no unread articles.
4140           (if (or read-all
4141                   (and (zerop (length gnus-newsgroup-marked))
4142                        (zerop (length gnus-newsgroup-unreads)))
4143                   (eq (gnus-group-find-parameter group 'display)
4144                       'all))
4145               (gnus-uncompress-range (gnus-active group))
4146             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4147                           (copy-sequence gnus-newsgroup-unreads))
4148                   '<)))
4149          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4150          (scored (length scored-list))
4151          (number (length articles))
4152          (marked (+ (length gnus-newsgroup-marked)
4153                     (length gnus-newsgroup-dormant)))
4154          (select
4155           (cond
4156            ((numberp read-all)
4157             read-all)
4158            (t
4159             (condition-case ()
4160                 (cond
4161                  ((and (or (<= scored marked) (= scored number))
4162                        (numberp gnus-large-newsgroup)
4163                        (> number gnus-large-newsgroup))
4164                   (let ((input
4165                          (read-string
4166                           (format
4167                            "How many articles from %s (default %d): "
4168                            (gnus-limit-string gnus-newsgroup-name 35)
4169                            number))))
4170                     (if (string-match "^[ \t]*$" input) number input)))
4171                  ((and (> scored marked) (< scored number)
4172                        (> (- scored number) 20))
4173                   (let ((input
4174                          (read-string
4175                           (format "%s %s (%d scored, %d total): "
4176                                   "How many articles from"
4177                                   group scored number))))
4178                     (if (string-match "^[ \t]*$" input)
4179                         number input)))
4180                  (t number))
4181               (quit nil))))))
4182     (setq select (if (stringp select) (string-to-number select) select))
4183     (if (or (null select) (zerop select))
4184         select
4185       (if (and (not (zerop scored)) (<= (abs select) scored))
4186           (progn
4187             (setq articles (sort scored-list '<))
4188             (setq number (length articles)))
4189         (setq articles (copy-sequence articles)))
4190
4191       (when (< (abs select) number)
4192         (if (< select 0)
4193             ;; Select the N oldest articles.
4194             (setcdr (nthcdr (1- (abs select)) articles) nil)
4195           ;; Select the N most recent articles.
4196           (setq articles (nthcdr (- number select) articles))))
4197       (setq gnus-newsgroup-unselected
4198             (gnus-sorted-intersection
4199              gnus-newsgroup-unreads
4200              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4201       articles)))
4202
4203 (defun gnus-killed-articles (killed articles)
4204   (let (out)
4205     (while articles
4206       (when (inline (gnus-member-of-range (car articles) killed))
4207         (push (car articles) out))
4208       (setq articles (cdr articles)))
4209     out))
4210
4211 (defun gnus-uncompress-marks (marks)
4212   "Uncompress the mark ranges in MARKS."
4213   (let ((uncompressed '(score bookmark))
4214         out)
4215     (while marks
4216       (if (memq (caar marks) uncompressed)
4217           (push (car marks) out)
4218         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4219       (setq marks (cdr marks)))
4220     out))
4221
4222 (defun gnus-adjust-marked-articles (info)
4223   "Set all article lists and remove all marks that are no longer valid."
4224   (let* ((marked-lists (gnus-info-marks info))
4225          (active (gnus-active (gnus-info-group info)))
4226          (min (car active))
4227          (max (cdr active))
4228          (types gnus-article-mark-lists)
4229          (uncompressed '(score bookmark killed))
4230          marks var articles article mark)
4231
4232     (while marked-lists
4233       (setq marks (pop marked-lists))
4234       (set (setq var (intern (format "gnus-newsgroup-%s"
4235                                      (car (rassq (setq mark (car marks))
4236                                                  types)))))
4237            (if (memq (car marks) uncompressed) (cdr marks)
4238              (gnus-uncompress-range (cdr marks))))
4239
4240       (setq articles (symbol-value var))
4241
4242       ;; All articles have to be subsets of the active articles.
4243       (cond
4244        ;; Adjust "simple" lists.
4245        ((memq mark '(tick dormant expire reply save))
4246         (while articles
4247           (when (or (< (setq article (pop articles)) min) (> article max))
4248             (set var (delq article (symbol-value var))))))
4249        ;; Adjust assocs.
4250        ((memq mark uncompressed)
4251         (when (not (listp (cdr (symbol-value var))))
4252           (set var (list (symbol-value var))))
4253         (when (not (listp (cdr articles)))
4254           (setq articles (list articles)))
4255         (while articles
4256           (when (or (not (consp (setq article (pop articles))))
4257                     (< (car article) min)
4258                     (> (car article) max))
4259             (set var (delq article (symbol-value var))))))))))
4260
4261 (defun gnus-update-missing-marks (missing)
4262   "Go through the list of MISSING articles and remove them from the mark lists."
4263   (when missing
4264     (let ((types gnus-article-mark-lists)
4265           var m)
4266       ;; Go through all types.
4267       (while types
4268         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4269         (when (symbol-value var)
4270           ;; This list has articles.  So we delete all missing articles
4271           ;; from it.
4272           (setq m missing)
4273           (while m
4274             (set var (delq (pop m) (symbol-value var)))))))))
4275
4276 (defun gnus-update-marks ()
4277   "Enter the various lists of marked articles into the newsgroup info list."
4278   (let ((types gnus-article-mark-lists)
4279         (info (gnus-get-info gnus-newsgroup-name))
4280         (uncompressed '(score bookmark killed))
4281         type list newmarked symbol delta-marks)
4282     (when info
4283       ;; Add all marks lists to the list of marks lists.
4284       (while (setq type (pop types))
4285         (setq list (symbol-value
4286                           (setq symbol
4287                                 (intern (format "gnus-newsgroup-%s"
4288                                                 (car type))))))
4289
4290         (when list
4291           ;; Get rid of the entries of the articles that have the
4292           ;; default score.
4293           (when (and (eq (cdr type) 'score)
4294                      gnus-save-score
4295                      list)
4296             (let* ((arts list)
4297                    (prev (cons nil list))
4298                    (all prev))
4299               (while arts
4300                 (if (or (not (consp (car arts)))
4301                         (= (cdar arts) gnus-summary-default-score))
4302                     (setcdr prev (cdr arts))
4303                   (setq prev arts))
4304                 (setq arts (cdr arts)))
4305               (setq list (cdr all)))))
4306
4307        (or (memq (cdr type) uncompressed)
4308            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4309        
4310        (when (gnus-check-backend-function 'request-set-mark
4311                                           gnus-newsgroup-name)
4312          ;; uncompressed:s are not proper flags (they are cons cells)
4313          ;; cache is a internal gnus flag
4314          (unless (memq (cdr type) (cons 'cache uncompressed))
4315            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4316                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4317                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4318              (if add
4319                  (push (list add 'add (list (cdr type))) delta-marks))
4320              (if del
4321                  (push (list del 'del (list (cdr type))) delta-marks)))))
4322           
4323         (when list
4324          (push (cons (cdr type) list) newmarked)))
4325
4326       (when delta-marks
4327         (unless (gnus-check-group gnus-newsgroup-name)
4328           (error "Can't open server for %s" gnus-newsgroup-name))
4329         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4330           
4331       ;; Enter these new marks into the info of the group.
4332       (if (nthcdr 3 info)
4333           (setcar (nthcdr 3 info) newmarked)
4334         ;; Add the marks lists to the end of the info.
4335         (when newmarked
4336           (setcdr (nthcdr 2 info) (list newmarked))))
4337
4338       ;; Cut off the end of the info if there's nothing else there.
4339       (let ((i 5))
4340         (while (and (> i 2)
4341                     (not (nth i info)))
4342           (when (nthcdr (decf i) info)
4343             (setcdr (nthcdr i info) nil)))))))
4344
4345 (defun gnus-set-mode-line (where)
4346   "This function sets the mode line of the article or summary buffers.
4347 If WHERE is `summary', the summary mode line format will be used."
4348   ;; Is this mode line one we keep updated?
4349   (when (and (memq where gnus-updated-mode-lines)
4350              (symbol-value
4351               (intern (format "gnus-%s-mode-line-format-spec" where))))
4352     (let (mode-string)
4353       (save-excursion
4354         ;; We evaluate this in the summary buffer since these
4355         ;; variables are buffer-local to that buffer.
4356         (set-buffer gnus-summary-buffer)
4357         ;; We bind all these variables that are used in the `eval' form
4358         ;; below.
4359         (let* ((mformat (symbol-value
4360                          (intern
4361                           (format "gnus-%s-mode-line-format-spec" where))))
4362                (gnus-tmp-group-name gnus-newsgroup-name)
4363                (gnus-tmp-article-number (or gnus-current-article 0))
4364                (gnus-tmp-unread gnus-newsgroup-unreads)
4365                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4366                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4367                (gnus-tmp-unread-and-unselected
4368                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4369                             (zerop gnus-tmp-unselected))
4370                        "")
4371                       ((zerop gnus-tmp-unselected)
4372                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4373                       (t (format "{%d(+%d) more}"
4374                                  gnus-tmp-unread-and-unticked
4375                                  gnus-tmp-unselected))))
4376                (gnus-tmp-subject
4377                 (if (and gnus-current-headers
4378                          (vectorp gnus-current-headers))
4379                     (gnus-mode-string-quote
4380                      (mail-header-subject gnus-current-headers))
4381                   ""))
4382                bufname-length max-len
4383                gnus-tmp-header);; passed as argument to any user-format-funcs
4384           (setq mode-string (eval mformat))
4385           (setq bufname-length (if (string-match "%b" mode-string)
4386                                    (- (length
4387                                        (buffer-name
4388                                         (if (eq where 'summary)
4389                                             nil
4390                                           (get-buffer gnus-article-buffer))))
4391                                       2)
4392                                  0))
4393           (setq max-len (max 4 (if gnus-mode-non-string-length
4394                                    (- (window-width)
4395                                       gnus-mode-non-string-length
4396                                       bufname-length)
4397                                  (length mode-string))))
4398           ;; We might have to chop a bit of the string off...
4399           (when (> (length mode-string) max-len)
4400             (setq mode-string
4401                   (concat (truncate-string-to-width mode-string (- max-len 3))
4402                           "...")))
4403           ;; Pad the mode string a bit.
4404           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4405       ;; Update the mode line.
4406       (setq mode-line-buffer-identification
4407             (gnus-mode-line-buffer-identification (list mode-string)))
4408       (set-buffer-modified-p t))))
4409
4410 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4411   "Go through the HEADERS list and add all Xrefs to a hash table.
4412 The resulting hash table is returned, or nil if no Xrefs were found."
4413   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4414          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4415          (xref-hashtb (gnus-make-hashtable))
4416          start group entry number xrefs header)
4417     (while headers
4418       (setq header (pop headers))
4419       (when (and (setq xrefs (mail-header-xref header))
4420                  (not (memq (setq number (mail-header-number header))
4421                             unreads)))
4422         (setq start 0)
4423         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4424           (setq start (match-end 0))
4425           (setq group (if prefix
4426                           (concat prefix (substring xrefs (match-beginning 1)
4427                                                     (match-end 1)))
4428                         (substring xrefs (match-beginning 1) (match-end 1))))
4429           (setq number
4430                 (string-to-int (substring xrefs (match-beginning 2)
4431                                           (match-end 2))))
4432           (if (setq entry (gnus-gethash group xref-hashtb))
4433               (setcdr entry (cons number (cdr entry)))
4434             (gnus-sethash group (cons number nil) xref-hashtb)))))
4435     (and start xref-hashtb)))
4436
4437 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4438   "Look through all the headers and mark the Xrefs as read."
4439   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4440         name entry info xref-hashtb idlist method nth4)
4441     (save-excursion
4442       (set-buffer gnus-group-buffer)
4443       (when (setq xref-hashtb
4444                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4445         (mapatoms
4446          (lambda (group)
4447            (unless (string= from-newsgroup (setq name (symbol-name group)))
4448              (setq idlist (symbol-value group))
4449              ;; Dead groups are not updated.
4450              (and (prog1
4451                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4452                             info (nth 2 entry))
4453                     (when (stringp (setq nth4 (gnus-info-method info)))
4454                       (setq nth4 (gnus-server-to-method nth4))))
4455                   ;; Only do the xrefs if the group has the same
4456                   ;; select method as the group we have just read.
4457                   (or (gnus-methods-equal-p
4458                        nth4 (gnus-find-method-for-group from-newsgroup))
4459                       virtual
4460                       (equal nth4 (setq method (gnus-find-method-for-group
4461                                                 from-newsgroup)))
4462                       (and (equal (car nth4) (car method))
4463                            (equal (nth 1 nth4) (nth 1 method))))
4464                   gnus-use-cross-reference
4465                   (or (not (eq gnus-use-cross-reference t))
4466                       virtual
4467                       ;; Only do cross-references on subscribed
4468                       ;; groups, if that is what is wanted.
4469                       (<= (gnus-info-level info) gnus-level-subscribed))
4470                   (gnus-group-make-articles-read name idlist))))
4471          xref-hashtb)))))
4472
4473 (defun gnus-compute-read-articles (group articles)
4474   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4475          (info (nth 2 entry))
4476          (active (gnus-active group))
4477          ninfo)
4478     (when entry
4479       ;; First peel off all invalid article numbers.
4480       (when active
4481         (let ((ids articles)
4482               id first)
4483           (while (setq id (pop ids))
4484             (when (and first (> id (cdr active)))
4485               ;; We'll end up in this situation in one particular
4486               ;; obscure situation.  If you re-scan a group and get
4487               ;; a new article that is cross-posted to a different
4488               ;; group that has not been re-scanned, you might get
4489               ;; crossposted article that has a higher number than
4490               ;; Gnus believes possible.  So we re-activate this
4491               ;; group as well.  This might mean doing the
4492               ;; crossposting thingy will *increase* the number
4493               ;; of articles in some groups.  Tsk, tsk.
4494               (setq active (or (gnus-activate-group group) active)))
4495             (when (or (> id (cdr active))
4496                       (< id (car active)))
4497               (setq articles (delq id articles))))))
4498       ;; If the read list is nil, we init it.
4499       (if (and active
4500                (null (gnus-info-read info))
4501                (> (car active) 1))
4502           (setq ninfo (cons 1 (1- (car active))))
4503         (setq ninfo (gnus-info-read info)))
4504       ;; Then we add the read articles to the range.
4505       (gnus-add-to-range
4506        ninfo (setq articles (sort articles '<))))))
4507
4508 (defun gnus-group-make-articles-read (group articles)
4509   "Update the info of GROUP to say that ARTICLES are read."
4510   (let* ((num 0)
4511          (entry (gnus-gethash group gnus-newsrc-hashtb))
4512          (info (nth 2 entry))
4513          (active (gnus-active group))
4514          range)
4515     (when entry
4516       (setq range (gnus-compute-read-articles group articles))
4517       (save-excursion
4518         (set-buffer gnus-group-buffer)
4519         (gnus-undo-register
4520           `(progn
4521              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4522              (gnus-info-set-read ',info ',(gnus-info-read info))
4523              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4524              (gnus-group-update-group ,group t))))
4525       ;; Add the read articles to the range.
4526       (gnus-info-set-read info range)
4527       ;; Then we have to re-compute how many unread
4528       ;; articles there are in this group.
4529       (when active
4530         (cond
4531          ((not range)
4532           (setq num (- (1+ (cdr active)) (car active))))
4533          ((not (listp (cdr range)))
4534           (setq num (- (cdr active) (- (1+ (cdr range))
4535                                        (car range)))))
4536          (t
4537           (while range
4538             (if (numberp (car range))
4539                 (setq num (1+ num))
4540               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4541             (setq range (cdr range)))
4542           (setq num (- (cdr active) num))))
4543         ;; Update the number of unread articles.
4544         (setcar entry num)
4545         ;; Update the group buffer.
4546         (gnus-group-update-group group t)))))
4547
4548 (defvar gnus-newsgroup-none-id 0)
4549
4550 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4551   (let ((cur nntp-server-buffer)
4552         (dependencies
4553          (or dependencies
4554              (save-excursion (set-buffer gnus-summary-buffer)
4555                              gnus-newsgroup-dependencies)))
4556         headers id end ref
4557         (mail-parse-charset gnus-newsgroup-charset))
4558     (save-excursion
4559       (set-buffer nntp-server-buffer)
4560       ;; Translate all TAB characters into SPACE characters.
4561       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4562       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4563       (gnus-run-hooks 'gnus-parse-headers-hook)
4564       (let ((case-fold-search t)
4565             in-reply-to header p lines chars)
4566         (goto-char (point-min))
4567         ;; Search to the beginning of the next header.  Error messages
4568         ;; do not begin with 2 or 3.
4569         (while (re-search-forward "^[23][0-9]+ " nil t)
4570           (setq id nil
4571                 ref nil)
4572           ;; This implementation of this function, with nine
4573           ;; search-forwards instead of the one re-search-forward and
4574           ;; a case (which basically was the old function) is actually
4575           ;; about twice as fast, even though it looks messier.  You
4576           ;; can't have everything, I guess.  Speed and elegance
4577           ;; doesn't always go hand in hand.
4578           (setq
4579            header
4580            (vector
4581             ;; Number.
4582             (prog1
4583                 (read cur)
4584               (end-of-line)
4585               (setq p (point))
4586               (narrow-to-region (point)
4587                                 (or (and (search-forward "\n.\n" nil t)
4588                                          (- (point) 2))
4589                                     (point))))
4590             ;; Subject.
4591             (progn
4592               (goto-char p)
4593               (if (search-forward "\nsubject: " nil t)
4594                   (funcall gnus-decode-encoded-word-function
4595                            (nnheader-header-value))
4596                 "(none)"))
4597             ;; From.
4598             (progn
4599               (goto-char p)
4600               (if (search-forward "\nfrom: " nil t)
4601                   (funcall gnus-decode-encoded-word-function
4602                            (nnheader-header-value))
4603                 "(nobody)"))
4604             ;; Date.
4605             (progn
4606               (goto-char p)
4607               (if (search-forward "\ndate: " nil t)
4608                   (nnheader-header-value) ""))
4609             ;; Message-ID.
4610             (progn
4611               (goto-char p)
4612               (setq id (if (re-search-forward
4613                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4614                            ;; We do it this way to make sure the Message-ID
4615                            ;; is (somewhat) syntactically valid.
4616                            (buffer-substring (match-beginning 1)
4617                                              (match-end 1))
4618                          ;; If there was no message-id, we just fake one
4619                          ;; to make subsequent routines simpler.
4620                          (nnheader-generate-fake-message-id))))
4621             ;; References.
4622             (progn
4623               (goto-char p)
4624               (if (search-forward "\nreferences: " nil t)
4625                   (progn
4626                     (setq end (point))
4627                     (prog1
4628                         (nnheader-header-value)
4629                       (setq ref
4630                             (buffer-substring
4631                              (progn
4632                                (end-of-line)
4633                                (search-backward ">" end t)
4634                                (1+ (point)))
4635                              (progn
4636                                (search-backward "<" end t)
4637                                (point))))))
4638                 ;; Get the references from the in-reply-to header if there
4639                 ;; were no references and the in-reply-to header looks
4640                 ;; promising.
4641                 (if (and (search-forward "\nin-reply-to: " nil t)
4642                          (setq in-reply-to (nnheader-header-value))
4643                          (string-match "<[^>]+>" in-reply-to))
4644                     (let (ref2)
4645                       (setq ref (substring in-reply-to (match-beginning 0)
4646                                            (match-end 0)))
4647                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4648                         (setq ref2 (substring in-reply-to (match-beginning 0)
4649                                               (match-end 0)))
4650                         (when (> (length ref2) (length ref))
4651                           (setq ref ref2)))
4652                       ref)
4653                   (setq ref nil))))
4654             ;; Chars.
4655             (progn
4656               (goto-char p)
4657               (if (search-forward "\nchars: " nil t)
4658                   (if (numberp (setq chars (ignore-errors (read cur))))
4659                       chars 0)
4660                 0))
4661             ;; Lines.
4662             (progn
4663               (goto-char p)
4664               (if (search-forward "\nlines: " nil t)
4665                   (if (numberp (setq lines (ignore-errors (read cur))))
4666                       lines 0)
4667                 0))
4668             ;; Xref.
4669             (progn
4670               (goto-char p)
4671               (and (search-forward "\nxref: " nil t)
4672                    (nnheader-header-value)))
4673             ;; Extra.
4674             (when gnus-extra-headers
4675               (let ((extra gnus-extra-headers)
4676                     out)
4677                 (while extra
4678                   (goto-char p)
4679                   (when (search-forward
4680                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4681                     (push (cons (car extra) (nnheader-header-value))
4682                           out))
4683                   (pop extra))
4684                 out))))
4685           (when (equal id ref)
4686             (setq ref nil))
4687
4688           (when gnus-alter-header-function
4689             (funcall gnus-alter-header-function header)
4690             (setq id (mail-header-id header)
4691                   ref (gnus-parent-id (mail-header-references header))))
4692
4693           (when (setq header
4694                       (gnus-dependencies-add-header
4695                        header dependencies force-new))
4696             (push header headers))
4697           (goto-char (point-max))
4698           (widen))
4699         (nreverse headers)))))
4700
4701 ;; Goes through the xover lines and returns a list of vectors
4702 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4703                                                   force-new dependencies
4704                                                   group also-fetch-heads)
4705   "Parse the news overview data in the server buffer, and return a
4706 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4707   ;; Get the Xref when the users reads the articles since most/some
4708   ;; NNTP servers do not include Xrefs when using XOVER.
4709   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4710   (let ((mail-parse-charset gnus-newsgroup-charset)
4711         (cur nntp-server-buffer)
4712         (dependencies (or dependencies gnus-newsgroup-dependencies))
4713         number headers header)
4714     (save-excursion
4715       (set-buffer nntp-server-buffer)
4716       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4717       ;; Allow the user to mangle the headers before parsing them.
4718       (gnus-run-hooks 'gnus-parse-headers-hook)
4719       (goto-char (point-min))
4720       (while (not (eobp))
4721         (condition-case ()
4722             (while (and sequence (not (eobp)))
4723               (setq number (read cur))
4724               (while (and sequence
4725                           (< (car sequence) number))
4726                 (setq sequence (cdr sequence)))
4727               (and sequence
4728                    (eq number (car sequence))
4729                    (progn
4730                      (setq sequence (cdr sequence))
4731                      (setq header (inline
4732                                     (gnus-nov-parse-line
4733                                      number dependencies force-new))))
4734                    (push header headers))
4735               (forward-line 1))
4736           (error
4737            (gnus-error 4 "Strange nov line (%d)"
4738                        (count-lines (point-min) (point)))))
4739         (forward-line 1))
4740       ;; A common bug in inn is that if you have posted an article and
4741       ;; then retrieves the active file, it will answer correctly --
4742       ;; the new article is included.  However, a NOV entry for the
4743       ;; article may not have been generated yet, so this may fail.
4744       ;; We work around this problem by retrieving the last few
4745       ;; headers using HEAD.
4746       (if (or (not also-fetch-heads)
4747               (not sequence))
4748           ;; We (probably) got all the headers.
4749           (nreverse headers)
4750         (let ((gnus-nov-is-evil t))
4751           (nconc
4752            (nreverse headers)
4753            (when (gnus-retrieve-headers sequence group)
4754              (gnus-get-newsgroup-headers))))))))
4755
4756 (defun gnus-article-get-xrefs ()
4757   "Fill in the Xref value in `gnus-current-headers', if necessary.
4758 This is meant to be called in `gnus-article-internal-prepare-hook'."
4759   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4760                                  gnus-current-headers)))
4761     (or (not gnus-use-cross-reference)
4762         (not headers)
4763         (and (mail-header-xref headers)
4764              (not (string= (mail-header-xref headers) "")))
4765         (let ((case-fold-search t)
4766               xref)
4767           (save-restriction
4768             (nnheader-narrow-to-headers)
4769             (goto-char (point-min))
4770             (when (or (and (eq (downcase (char-after)) ?x)
4771                            (looking-at "Xref:"))
4772                       (search-forward "\nXref:" nil t))
4773               (goto-char (1+ (match-end 0)))
4774               (setq xref (buffer-substring (point)
4775                                            (progn (end-of-line) (point))))
4776               (mail-header-set-xref headers xref)))))))
4777
4778 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4779   "Find article ID and insert the summary line for that article.
4780 OLD-HEADER can either be a header or a line number to insert
4781 the subject line on."
4782   (let* ((line (and (numberp old-header) old-header))
4783          (old-header (and (vectorp old-header) old-header))
4784          (header (cond ((and old-header use-old-header)
4785                         old-header)
4786                        ((and (numberp id)
4787                              (gnus-number-to-header id))
4788                         (gnus-number-to-header id))
4789                        (t
4790                         (gnus-read-header id))))
4791          (number (and (numberp id) id))
4792          d)
4793     (when header
4794       ;; Rebuild the thread that this article is part of and go to the
4795       ;; article we have fetched.
4796       (when (and (not gnus-show-threads)
4797                  old-header)
4798         (when (and number
4799                    (setq d (gnus-data-find (mail-header-number old-header))))
4800           (goto-char (gnus-data-pos d))
4801           (gnus-data-remove
4802            number
4803            (- (gnus-point-at-bol)
4804               (prog1
4805                   (1+ (gnus-point-at-eol))
4806                 (gnus-delete-line))))))
4807       (when old-header
4808         (mail-header-set-number header (mail-header-number old-header)))
4809       (setq gnus-newsgroup-sparse
4810             (delq (setq number (mail-header-number header))
4811                   gnus-newsgroup-sparse))
4812       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4813       (push number gnus-newsgroup-limit)
4814       (gnus-rebuild-thread (mail-header-id header) line)
4815       (gnus-summary-goto-subject number nil t))
4816     (when (and (numberp number)
4817                (> number 0))
4818       ;; We have to update the boundaries even if we can't fetch the
4819       ;; article if ID is a number -- so that the next `P' or `N'
4820       ;; command will fetch the previous (or next) article even
4821       ;; if the one we tried to fetch this time has been canceled.
4822       (when (> number gnus-newsgroup-end)
4823         (setq gnus-newsgroup-end number))
4824       (when (< number gnus-newsgroup-begin)
4825         (setq gnus-newsgroup-begin number))
4826       (setq gnus-newsgroup-unselected
4827             (delq number gnus-newsgroup-unselected)))
4828     ;; Report back a success?
4829     (and header (mail-header-number header))))
4830
4831 ;;; Process/prefix in the summary buffer
4832
4833 (defun gnus-summary-work-articles (n)
4834   "Return a list of articles to be worked upon.
4835 The prefix argument, the list of process marked articles, and the
4836 current article will be taken into consideration."
4837   (save-excursion
4838     (set-buffer gnus-summary-buffer)
4839     (cond
4840      (n
4841       ;; A numerical prefix has been given.
4842       (setq n (prefix-numeric-value n))
4843       (let ((backward (< n 0))
4844             (n (abs (prefix-numeric-value n)))
4845             articles article)
4846         (save-excursion
4847           (while
4848               (and (> n 0)
4849                    (push (setq article (gnus-summary-article-number))
4850                          articles)
4851                    (if backward
4852                        (gnus-summary-find-prev nil article)
4853                      (gnus-summary-find-next nil article)))
4854             (decf n)))
4855         (nreverse articles)))
4856      ((and (gnus-region-active-p) (mark))
4857       (message "region active")
4858       ;; Work on the region between point and mark.
4859       (let ((max (max (point) (mark)))
4860             articles article)
4861         (save-excursion
4862           (goto-char (min (min (point) (mark))))
4863           (while
4864               (and
4865                (push (setq article (gnus-summary-article-number)) articles)
4866                (gnus-summary-find-next nil article)
4867                (< (point) max)))
4868           (nreverse articles))))
4869      (gnus-newsgroup-processable
4870       ;; There are process-marked articles present.
4871       ;; Save current state.
4872       (gnus-summary-save-process-mark)
4873       ;; Return the list.
4874       (reverse gnus-newsgroup-processable))
4875      (t
4876       ;; Just return the current article.
4877       (list (gnus-summary-article-number))))))
4878
4879 (defmacro gnus-summary-iterate (arg &rest forms)
4880   "Iterate over the process/prefixed articles and do FORMS.
4881 ARG is the interactive prefix given to the command.  FORMS will be
4882 executed with point over the summary line of the articles."
4883   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4884     `(let ((,articles (gnus-summary-work-articles ,arg)))
4885        (while ,articles
4886          (gnus-summary-goto-subject (car ,articles))
4887          ,@forms
4888          (pop ,articles)))))
4889
4890 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4891 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4892
4893 (defun gnus-summary-save-process-mark ()
4894   "Push the current set of process marked articles on the stack."
4895   (interactive)
4896   (push (copy-sequence gnus-newsgroup-processable)
4897         gnus-newsgroup-process-stack))
4898
4899 (defun gnus-summary-kill-process-mark ()
4900   "Push the current set of process marked articles on the stack and unmark."
4901   (interactive)
4902   (gnus-summary-save-process-mark)
4903   (gnus-summary-unmark-all-processable))
4904
4905 (defun gnus-summary-yank-process-mark ()
4906   "Pop the last process mark state off the stack and restore it."
4907   (interactive)
4908   (unless gnus-newsgroup-process-stack
4909     (error "Empty mark stack"))
4910   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4911
4912 (defun gnus-summary-process-mark-set (set)
4913   "Make SET into the current process marked articles."
4914   (gnus-summary-unmark-all-processable)
4915   (while set
4916     (gnus-summary-set-process-mark (pop set))))
4917
4918 ;;; Searching and stuff
4919
4920 (defun gnus-summary-search-group (&optional backward use-level)
4921   "Search for next unread newsgroup.
4922 If optional argument BACKWARD is non-nil, search backward instead."
4923   (save-excursion
4924     (set-buffer gnus-group-buffer)
4925     (when (gnus-group-search-forward
4926            backward nil (if use-level (gnus-group-group-level) nil))
4927       (gnus-group-group-name))))
4928
4929 (defun gnus-summary-best-group (&optional exclude-group)
4930   "Find the name of the best unread group.
4931 If EXCLUDE-GROUP, do not go to this group."
4932   (save-excursion
4933     (set-buffer gnus-group-buffer)
4934     (save-excursion
4935       (gnus-group-best-unread-group exclude-group))))
4936
4937 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4938   (if backward (gnus-summary-find-prev)
4939     (let* ((dummy (gnus-summary-article-intangible-p))
4940            (article (or article (gnus-summary-article-number)))
4941            (arts (gnus-data-find-list article))
4942            result)
4943       (when (and (not dummy)
4944                  (or (not gnus-summary-check-current)
4945                      (not unread)
4946                      (not (gnus-data-unread-p (car arts)))))
4947         (setq arts (cdr arts)))
4948       (when (setq result
4949                   (if unread
4950                       (progn
4951                         (while arts
4952                           (when (or (and undownloaded
4953                                          (eq gnus-undownloaded-mark
4954                                              (gnus-data-mark (car arts))))
4955                                     (gnus-data-unread-p (car arts)))
4956                             (setq result (car arts)
4957                                   arts nil))
4958                           (setq arts (cdr arts)))
4959                         result)
4960                     (car arts)))
4961         (goto-char (gnus-data-pos result))
4962         (gnus-data-number result)))))
4963
4964 (defun gnus-summary-find-prev (&optional unread article)
4965   (let* ((eobp (eobp))
4966          (article (or article (gnus-summary-article-number)))
4967          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4968          result)
4969     (when (and (not eobp)
4970                (or (not gnus-summary-check-current)
4971                    (not unread)
4972                    (not (gnus-data-unread-p (car arts)))))
4973       (setq arts (cdr arts)))
4974     (when (setq result
4975                 (if unread
4976                     (progn
4977                       (while arts
4978                         (when (gnus-data-unread-p (car arts))
4979                           (setq result (car arts)
4980                                 arts nil))
4981                         (setq arts (cdr arts)))
4982                       result)
4983                   (car arts)))
4984       (goto-char (gnus-data-pos result))
4985       (gnus-data-number result))))
4986
4987 (defun gnus-summary-find-subject (subject &optional unread backward article)
4988   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4989          (article (or article (gnus-summary-article-number)))
4990          (articles (gnus-data-list backward))
4991          (arts (gnus-data-find-list article articles))
4992          result)
4993     (when (or (not gnus-summary-check-current)
4994               (not unread)
4995               (not (gnus-data-unread-p (car arts))))
4996       (setq arts (cdr arts)))
4997     (while arts
4998       (and (or (not unread)
4999                (gnus-data-unread-p (car arts)))
5000            (vectorp (gnus-data-header (car arts)))
5001            (gnus-subject-equal
5002             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5003            (setq result (car arts)
5004                  arts nil))
5005       (setq arts (cdr arts)))
5006     (and result
5007          (goto-char (gnus-data-pos result))
5008          (gnus-data-number result))))
5009
5010 (defun gnus-summary-search-forward (&optional unread subject backward)
5011   "Search forward for an article.
5012 If UNREAD, look for unread articles.  If SUBJECT, look for
5013 articles with that subject.  If BACKWARD, search backward instead."
5014   (cond (subject (gnus-summary-find-subject subject unread backward))
5015         (backward (gnus-summary-find-prev unread))
5016         (t (gnus-summary-find-next unread))))
5017
5018 (defun gnus-recenter (&optional n)
5019   "Center point in window and redisplay frame.
5020 Also do horizontal recentering."
5021   (interactive "P")
5022   (when (and gnus-auto-center-summary
5023              (not (eq gnus-auto-center-summary 'vertical)))
5024     (gnus-horizontal-recenter))
5025   (recenter n))
5026
5027 (defun gnus-summary-recenter ()
5028   "Center point in the summary window.
5029 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5030 displayed, no centering will be performed."
5031   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5032   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5033   (let* ((top (cond ((< (window-height) 4) 0)
5034                     ((< (window-height) 7) 1)
5035                     (t (if (numberp gnus-auto-center-summary)
5036                            gnus-auto-center-summary
5037                          2))))
5038          (height (1- (window-height)))
5039          (bottom (save-excursion (goto-char (point-max))
5040                                  (forward-line (- height))
5041                                  (point)))
5042          (window (get-buffer-window (current-buffer))))
5043     ;; The user has to want it.
5044     (when gnus-auto-center-summary
5045       (when (get-buffer-window gnus-article-buffer)
5046         ;; Only do recentering when the article buffer is displayed,
5047         ;; Set the window start to either `bottom', which is the biggest
5048         ;; possible valid number, or the second line from the top,
5049         ;; whichever is the least.
5050         (set-window-start
5051          window (min bottom (save-excursion
5052                               (forward-line (- top)) (point)))))
5053       ;; Do horizontal recentering while we're at it.
5054       (when (and (get-buffer-window (current-buffer) t)
5055                  (not (eq gnus-auto-center-summary 'vertical)))
5056         (let ((selected (selected-window)))
5057           (select-window (get-buffer-window (current-buffer) t))
5058           (gnus-summary-position-point)
5059           (gnus-horizontal-recenter)
5060           (select-window selected))))))
5061
5062 (defun gnus-summary-jump-to-group (newsgroup)
5063   "Move point to NEWSGROUP in group mode buffer."
5064   ;; Keep update point of group mode buffer if visible.
5065   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5066       (save-window-excursion
5067         ;; Take care of tree window mode.
5068         (when (get-buffer-window gnus-group-buffer)
5069           (pop-to-buffer gnus-group-buffer))
5070         (gnus-group-jump-to-group newsgroup))
5071     (save-excursion
5072       ;; Take care of tree window mode.
5073       (if (get-buffer-window gnus-group-buffer)
5074           (pop-to-buffer gnus-group-buffer)
5075         (set-buffer gnus-group-buffer))
5076       (gnus-group-jump-to-group newsgroup))))
5077
5078 ;; This function returns a list of article numbers based on the
5079 ;; difference between the ranges of read articles in this group and
5080 ;; the range of active articles.
5081 (defun gnus-list-of-unread-articles (group)
5082   (let* ((read (gnus-info-read (gnus-get-info group)))
5083          (active (or (gnus-active group) (gnus-activate-group group)))
5084          (last (cdr active))
5085          first nlast unread)
5086     ;; If none are read, then all are unread.
5087     (if (not read)
5088         (setq first (car active))
5089       ;; If the range of read articles is a single range, then the
5090       ;; first unread article is the article after the last read
5091       ;; article.  Sounds logical, doesn't it?
5092       (if (not (listp (cdr read)))
5093           (setq first (max (car active) (1+ (cdr read))))
5094         ;; `read' is a list of ranges.
5095         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5096                                   (caar read)))
5097                   1)
5098           (setq first (car active)))
5099         (while read
5100           (when first
5101             (while (< first nlast)
5102               (push first unread)
5103               (setq first (1+ first))))
5104           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5105           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5106           (setq read (cdr read)))))
5107     ;; And add the last unread articles.
5108     (while (<= first last)
5109       (push first unread)
5110       (setq first (1+ first)))
5111     ;; Return the list of unread articles.
5112     (delq 0 (nreverse unread))))
5113
5114 (defun gnus-list-of-read-articles (group)
5115   "Return a list of unread, unticked and non-dormant articles."
5116   (let* ((info (gnus-get-info group))
5117          (marked (gnus-info-marks info))
5118          (active (gnus-active group)))
5119     (and info active
5120          (gnus-set-difference
5121           (gnus-sorted-complement
5122            (gnus-uncompress-range active)
5123            (gnus-list-of-unread-articles group))
5124           (append
5125            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5126            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5127
5128 ;; Various summary commands
5129
5130 (defun gnus-summary-select-article-buffer ()
5131   "Reconfigure windows to show article buffer."
5132   (interactive)
5133   (if (not (gnus-buffer-live-p gnus-article-buffer))
5134       (error "There is no article buffer for this summary buffer")
5135     (gnus-configure-windows 'article)
5136     (select-window (get-buffer-window gnus-article-buffer))))
5137
5138 (defun gnus-summary-universal-argument (arg)
5139   "Perform any operation on all articles that are process/prefixed."
5140   (interactive "P")
5141   (let ((articles (gnus-summary-work-articles arg))
5142         func article)
5143     (if (eq
5144          (setq
5145           func
5146           (key-binding
5147            (read-key-sequence
5148             (substitute-command-keys
5149              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5150              ))))
5151          'undefined)
5152         (gnus-error 1 "Undefined key")
5153       (save-excursion
5154         (while articles
5155           (gnus-summary-goto-subject (setq article (pop articles)))
5156           (let (gnus-newsgroup-processable)
5157             (command-execute func))
5158           (gnus-summary-remove-process-mark article)))))
5159   (gnus-summary-position-point))
5160
5161 (defun gnus-summary-toggle-truncation (&optional arg)
5162   "Toggle truncation of summary lines.
5163 With arg, turn line truncation on iff arg is positive."
5164   (interactive "P")
5165   (setq truncate-lines
5166         (if (null arg) (not truncate-lines)
5167           (> (prefix-numeric-value arg) 0)))
5168   (redraw-display))
5169
5170 (defun gnus-summary-reselect-current-group (&optional all rescan)
5171   "Exit and then reselect the current newsgroup.
5172 The prefix argument ALL means to select all articles."
5173   (interactive "P")
5174   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5175     (error "Ephemeral groups can't be reselected"))
5176   (let ((current-subject (gnus-summary-article-number))
5177         (group gnus-newsgroup-name))
5178     (setq gnus-newsgroup-begin nil)
5179     (gnus-summary-exit)
5180     ;; We have to adjust the point of group mode buffer because
5181     ;; point was moved to the next unread newsgroup by exiting.
5182     (gnus-summary-jump-to-group group)
5183     (when rescan
5184       (save-excursion
5185         (gnus-group-get-new-news-this-group 1)))
5186     (gnus-group-read-group all t)
5187     (gnus-summary-goto-subject current-subject nil t)))
5188
5189 (defun gnus-summary-rescan-group (&optional all)
5190   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5191   (interactive "P")
5192   (gnus-summary-reselect-current-group all t))
5193
5194 (defun gnus-summary-update-info (&optional non-destructive)
5195   (save-excursion
5196     (let ((group gnus-newsgroup-name))
5197       (when group
5198         (when gnus-newsgroup-kill-headers
5199           (setq gnus-newsgroup-killed
5200                 (gnus-compress-sequence
5201                  (nconc
5202                   (gnus-set-sorted-intersection
5203                    (gnus-uncompress-range gnus-newsgroup-killed)
5204                    (setq gnus-newsgroup-unselected
5205                          (sort gnus-newsgroup-unselected '<)))
5206                   (setq gnus-newsgroup-unreads
5207                         (sort gnus-newsgroup-unreads '<)))
5208                  t)))
5209         (unless (listp (cdr gnus-newsgroup-killed))
5210           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5211         (let ((headers gnus-newsgroup-headers))
5212           ;; Set the new ranges of read articles.
5213           (save-excursion
5214             (set-buffer gnus-group-buffer)
5215             (gnus-undo-force-boundary))
5216           (gnus-update-read-articles
5217            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5218           ;; Set the current article marks.
5219           (let ((gnus-newsgroup-scored
5220                  (if (and (not gnus-save-score)
5221                           (not non-destructive))
5222                      nil
5223                    gnus-newsgroup-scored)))
5224             (save-excursion
5225               (gnus-update-marks)))
5226           ;; Do the cross-ref thing.
5227           (when gnus-use-cross-reference
5228             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5229           ;; Do not switch windows but change the buffer to work.
5230           (set-buffer gnus-group-buffer)
5231           (unless (gnus-ephemeral-group-p group)
5232             (gnus-group-update-group group)))))))
5233
5234 (defun gnus-summary-save-newsrc (&optional force)
5235   "Save the current number of read/marked articles in the dribble buffer.
5236 The dribble buffer will then be saved.
5237 If FORCE (the prefix), also save the .newsrc file(s)."
5238   (interactive "P")
5239   (gnus-summary-update-info t)
5240   (if force
5241       (gnus-save-newsrc-file)
5242     (gnus-dribble-save)))
5243
5244 (defun gnus-summary-exit (&optional temporary)
5245   "Exit reading current newsgroup, and then return to group selection mode.
5246 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5247   (interactive)
5248   (gnus-set-global-variables)
5249   (when (gnus-buffer-live-p gnus-article-buffer)
5250     (save-excursion
5251       (set-buffer gnus-article-buffer)
5252       (mm-destroy-parts gnus-article-mime-handles)))
5253   (gnus-kill-save-kill-buffer)
5254   (gnus-async-halt-prefetch)
5255   (let* ((group gnus-newsgroup-name)
5256          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5257          (mode major-mode)
5258          (group-point nil)
5259          (buf (current-buffer)))
5260     (unless quit-config
5261       ;; Do adaptive scoring, and possibly save score files.
5262       (when gnus-newsgroup-adaptive
5263         (gnus-score-adaptive))
5264       (when gnus-use-scoring
5265         (gnus-score-save)))
5266     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5267     ;; If we have several article buffers, we kill them at exit.
5268     (unless gnus-single-article-buffer
5269       (gnus-kill-buffer gnus-original-article-buffer)
5270       (setq gnus-article-current nil))
5271     (when gnus-use-cache
5272       (gnus-cache-possibly-remove-articles)
5273       (gnus-cache-save-buffers))
5274     (gnus-async-prefetch-remove-group group)
5275     (when gnus-suppress-duplicates
5276       (gnus-dup-enter-articles))
5277     (when gnus-use-trees
5278       (gnus-tree-close group))
5279     ;; Remove entries for this group.
5280     (nnmail-purge-split-history (gnus-group-real-name group))
5281     ;; Make all changes in this group permanent.
5282     (unless quit-config
5283       (gnus-run-hooks 'gnus-exit-group-hook)
5284       (gnus-summary-update-info))
5285     (gnus-close-group group)
5286     ;; Make sure where we were, and go to next newsgroup.
5287     (set-buffer gnus-group-buffer)
5288     (unless quit-config
5289       (gnus-group-jump-to-group group))
5290     (gnus-run-hooks 'gnus-summary-exit-hook)
5291     (unless (or quit-config
5292                 ;; If this group has disappeared from the summary
5293                 ;; buffer, don't skip forwards.
5294                 (not (string= group (gnus-group-group-name))))
5295       (gnus-group-next-unread-group 1))
5296     (setq group-point (point))
5297     (if temporary
5298         nil                             ;Nothing to do.
5299       ;; If we have several article buffers, we kill them at exit.
5300       (unless gnus-single-article-buffer
5301         (gnus-kill-buffer gnus-article-buffer)
5302         (gnus-kill-buffer gnus-original-article-buffer)
5303         (setq gnus-article-current nil))
5304       (set-buffer buf)
5305       (if (not gnus-kill-summary-on-exit)
5306           (gnus-deaden-summary)
5307         ;; We set all buffer-local variables to nil.  It is unclear why
5308         ;; this is needed, but if we don't, buffer-local variables are
5309         ;; not garbage-collected, it seems.  This would the lead to en
5310         ;; ever-growing Emacs.
5311         (gnus-summary-clear-local-variables)
5312         (when (get-buffer gnus-article-buffer)
5313           (bury-buffer gnus-article-buffer))
5314         ;; We clear the global counterparts of the buffer-local
5315         ;; variables as well, just to be on the safe side.
5316         (set-buffer gnus-group-buffer)
5317         (gnus-summary-clear-local-variables)
5318         ;; Return to group mode buffer.
5319         (when (eq mode 'gnus-summary-mode)
5320           (gnus-kill-buffer buf)))
5321       (setq gnus-current-select-method gnus-select-method)
5322       (pop-to-buffer gnus-group-buffer)
5323       (if (not quit-config)
5324           (progn
5325             (goto-char group-point)
5326             (gnus-configure-windows 'group 'force))
5327         (gnus-handle-ephemeral-exit quit-config))
5328       ;; Clear the current group name.
5329       (unless quit-config
5330         (setq gnus-newsgroup-name nil)))))
5331
5332 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5333 (defun gnus-summary-exit-no-update (&optional no-questions)
5334   "Quit reading current newsgroup without updating read article info."
5335   (interactive)
5336   (let* ((group gnus-newsgroup-name)
5337          (quit-config (gnus-group-quit-config group)))
5338     (when (or no-questions
5339               gnus-expert-user
5340               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5341       (gnus-async-halt-prefetch)
5342       (mapcar 'funcall
5343               (delq 'gnus-summary-expire-articles
5344                     (copy-list gnus-summary-prepare-exit-hook)))
5345       (when (gnus-buffer-live-p gnus-article-buffer)
5346         (save-excursion
5347           (set-buffer gnus-article-buffer)
5348           (mm-destroy-parts gnus-article-mime-handles)))
5349       ;; If we have several article buffers, we kill them at exit.
5350       (unless gnus-single-article-buffer
5351         (gnus-kill-buffer gnus-article-buffer)
5352         (gnus-kill-buffer gnus-original-article-buffer)
5353         (setq gnus-article-current nil))
5354       (if (not gnus-kill-summary-on-exit)
5355           (gnus-deaden-summary)
5356         (gnus-close-group group)
5357         (gnus-summary-clear-local-variables)
5358         (set-buffer gnus-group-buffer)
5359         (gnus-summary-clear-local-variables)
5360         (when (get-buffer gnus-summary-buffer)
5361           (kill-buffer gnus-summary-buffer)))
5362       (unless gnus-single-article-buffer
5363         (setq gnus-article-current nil))
5364       (when gnus-use-trees
5365         (gnus-tree-close group))
5366       (gnus-async-prefetch-remove-group group)
5367       (when (get-buffer gnus-article-buffer)
5368         (bury-buffer gnus-article-buffer))
5369       ;; Return to the group buffer.
5370       (gnus-configure-windows 'group 'force)
5371       ;; Clear the current group name.
5372       (setq gnus-newsgroup-name nil)
5373       (when (equal (gnus-group-group-name) group)
5374         (gnus-group-next-unread-group 1))
5375       (when quit-config
5376         (gnus-handle-ephemeral-exit quit-config)))))
5377
5378 (defun gnus-handle-ephemeral-exit (quit-config)
5379   "Handle movement when leaving an ephemeral group.
5380 The state which existed when entering the ephemeral is reset."
5381   (if (not (buffer-name (car quit-config)))
5382       (gnus-configure-windows 'group 'force)
5383     (set-buffer (car quit-config))
5384     (cond ((eq major-mode 'gnus-summary-mode)
5385            (gnus-set-global-variables))
5386           ((eq major-mode 'gnus-article-mode)
5387            (save-excursion
5388              ;; The `gnus-summary-buffer' variable may point
5389              ;; to the old summary buffer when using a single
5390              ;; article buffer.
5391              (unless (gnus-buffer-live-p gnus-summary-buffer)
5392                (set-buffer gnus-group-buffer))
5393              (set-buffer gnus-summary-buffer)
5394              (gnus-set-global-variables))))
5395     (if (or (eq (cdr quit-config) 'article)
5396             (eq (cdr quit-config) 'pick))
5397         (progn
5398           ;; The current article may be from the ephemeral group
5399           ;; thus it is best that we reload this article
5400           (gnus-summary-show-article)
5401           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5402               (gnus-configure-windows 'pick 'force)
5403             (gnus-configure-windows (cdr quit-config) 'force)))
5404       (gnus-configure-windows (cdr quit-config) 'force))
5405     (when (eq major-mode 'gnus-summary-mode)
5406       (gnus-summary-next-subject 1 nil t)
5407       (gnus-summary-recenter)
5408       (gnus-summary-position-point))))
5409
5410 ;;; Dead summaries.
5411
5412 (defvar gnus-dead-summary-mode-map nil)
5413
5414 (unless gnus-dead-summary-mode-map
5415   (setq gnus-dead-summary-mode-map (make-keymap))
5416   (suppress-keymap gnus-dead-summary-mode-map)
5417   (substitute-key-definition
5418    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5419   (let ((keys '("\C-d" "\r" "\177" [delete])))
5420     (while keys
5421       (define-key gnus-dead-summary-mode-map
5422         (pop keys) 'gnus-summary-wake-up-the-dead))))
5423
5424 (defvar gnus-dead-summary-mode nil
5425   "Minor mode for Gnus summary buffers.")
5426
5427 (defun gnus-dead-summary-mode (&optional arg)
5428   "Minor mode for Gnus summary buffers."
5429   (interactive "P")
5430   (when (eq major-mode 'gnus-summary-mode)
5431     (make-local-variable 'gnus-dead-summary-mode)
5432     (setq gnus-dead-summary-mode
5433           (if (null arg) (not gnus-dead-summary-mode)
5434             (> (prefix-numeric-value arg) 0)))
5435     (when gnus-dead-summary-mode
5436       (gnus-add-minor-mode
5437        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5438
5439 (defun gnus-deaden-summary ()
5440   "Make the current summary buffer into a dead summary buffer."
5441   ;; Kill any previous dead summary buffer.
5442   (when (and gnus-dead-summary
5443              (buffer-name gnus-dead-summary))
5444     (save-excursion
5445       (set-buffer gnus-dead-summary)
5446       (when gnus-dead-summary-mode
5447         (kill-buffer (current-buffer)))))
5448   ;; Make this the current dead summary.
5449   (setq gnus-dead-summary (current-buffer))
5450   (gnus-dead-summary-mode 1)
5451   (let ((name (buffer-name)))
5452     (when (string-match "Summary" name)
5453       (rename-buffer
5454        (concat (substring name 0 (match-beginning 0)) "Dead "
5455                (substring name (match-beginning 0)))
5456        t))))
5457
5458 (defun gnus-kill-or-deaden-summary (buffer)
5459   "Kill or deaden the summary BUFFER."
5460   (save-excursion
5461     (when (and (buffer-name buffer)
5462                (not gnus-single-article-buffer))
5463       (save-excursion
5464         (set-buffer buffer)
5465         (gnus-kill-buffer gnus-article-buffer)
5466         (gnus-kill-buffer gnus-original-article-buffer)))
5467     (cond (gnus-kill-summary-on-exit
5468            (when (and gnus-use-trees
5469                       (gnus-buffer-exists-p buffer))
5470              (save-excursion
5471                (set-buffer buffer)
5472                (gnus-tree-close gnus-newsgroup-name)))
5473            (gnus-kill-buffer buffer))
5474           ((gnus-buffer-exists-p buffer)
5475            (save-excursion
5476              (set-buffer buffer)
5477              (gnus-deaden-summary))))))
5478
5479 (defun gnus-summary-wake-up-the-dead (&rest args)
5480   "Wake up the dead summary buffer."
5481   (interactive)
5482   (gnus-dead-summary-mode -1)
5483   (let ((name (buffer-name)))
5484     (when (string-match "Dead " name)
5485       (rename-buffer
5486        (concat (substring name 0 (match-beginning 0))
5487                (substring name (match-end 0)))
5488        t)))
5489   (gnus-message 3 "This dead summary is now alive again"))
5490
5491 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5492 (defun gnus-summary-fetch-faq (&optional faq-dir)
5493   "Fetch the FAQ for the current group.
5494 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5495 in."
5496   (interactive
5497    (list
5498     (when current-prefix-arg
5499       (completing-read
5500        "Faq dir: " (and (listp gnus-group-faq-directory)
5501                         (mapcar (lambda (file) (list file))
5502                                 gnus-group-faq-directory))))))
5503   (let (gnus-faq-buffer)
5504     (when (setq gnus-faq-buffer
5505                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5506       (gnus-configure-windows 'summary-faq))))
5507
5508 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5509 (defun gnus-summary-describe-group (&optional force)
5510   "Describe the current newsgroup."
5511   (interactive "P")
5512   (gnus-group-describe-group force gnus-newsgroup-name))
5513
5514 (defun gnus-summary-describe-briefly ()
5515   "Describe summary mode commands briefly."
5516   (interactive)
5517   (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")))
5518
5519 ;; Walking around group mode buffer from summary mode.
5520
5521 (defun gnus-summary-next-group (&optional no-article target-group backward)
5522   "Exit current newsgroup and then select next unread newsgroup.
5523 If prefix argument NO-ARTICLE is non-nil, no article is selected
5524 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5525 previous group instead."
5526   (interactive "P")
5527   ;; Stop pre-fetching.
5528   (gnus-async-halt-prefetch)
5529   (let ((current-group gnus-newsgroup-name)
5530         (current-buffer (current-buffer))
5531         entered)
5532     ;; First we semi-exit this group to update Xrefs and all variables.
5533     ;; We can't do a real exit, because the window conf must remain
5534     ;; the same in case the user is prompted for info, and we don't
5535     ;; want the window conf to change before that...
5536     (gnus-summary-exit t)
5537     (while (not entered)
5538       ;; Then we find what group we are supposed to enter.
5539       (set-buffer gnus-group-buffer)
5540       (gnus-group-jump-to-group current-group)
5541       (setq target-group
5542             (or target-group
5543                 (if (eq gnus-keep-same-level 'best)
5544                     (gnus-summary-best-group gnus-newsgroup-name)
5545                   (gnus-summary-search-group backward gnus-keep-same-level))))
5546       (if (not target-group)
5547           ;; There are no further groups, so we return to the group
5548           ;; buffer.
5549           (progn
5550             (gnus-message 5 "Returning to the group buffer")
5551             (setq entered t)
5552             (when (gnus-buffer-live-p current-buffer)
5553               (set-buffer current-buffer)
5554               (gnus-summary-exit))
5555             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5556         ;; We try to enter the target group.
5557         (gnus-group-jump-to-group target-group)
5558         (let ((unreads (gnus-group-group-unread)))
5559           (if (and (or (eq t unreads)
5560                        (and unreads (not (zerop unreads))))
5561                    (gnus-summary-read-group
5562                     target-group nil no-article
5563                     (and (buffer-name current-buffer) current-buffer)
5564                     nil backward))
5565               (setq entered t)
5566             (setq current-group target-group
5567                   target-group nil)))))))
5568
5569 (defun gnus-summary-prev-group (&optional no-article)
5570   "Exit current newsgroup and then select previous unread newsgroup.
5571 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5572   (interactive "P")
5573   (gnus-summary-next-group no-article nil t))
5574
5575 ;; Walking around summary lines.
5576
5577 (defun gnus-summary-first-subject (&optional unread undownloaded)
5578   "Go to the first unread subject.
5579 If UNREAD is non-nil, go to the first unread article.
5580 Returns the article selected or nil if there are no unread articles."
5581   (interactive "P")
5582   (prog1
5583       (cond
5584        ;; Empty summary.
5585        ((null gnus-newsgroup-data)
5586         (gnus-message 3 "No articles in the group")
5587         nil)
5588        ;; Pick the first article.
5589        ((not unread)
5590         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5591         (gnus-data-number (car gnus-newsgroup-data)))
5592        ;; No unread articles.
5593        ((null gnus-newsgroup-unreads)
5594         (gnus-message 3 "No more unread articles")
5595         nil)
5596        ;; Find the first unread article.
5597        (t
5598         (let ((data gnus-newsgroup-data))
5599           (while (and data
5600                       (and (not (and undownloaded
5601                                      (eq gnus-undownloaded-mark
5602                                          (gnus-data-mark (car data)))))
5603                            (not (gnus-data-unread-p (car data)))))
5604             (setq data (cdr data)))
5605           (when data
5606             (goto-char (gnus-data-pos (car data)))
5607             (gnus-data-number (car data))))))
5608     (gnus-summary-position-point)))
5609
5610 (defun gnus-summary-next-subject (n &optional unread dont-display)
5611   "Go to next N'th summary line.
5612 If N is negative, go to the previous N'th subject line.
5613 If UNREAD is non-nil, only unread articles are selected.
5614 The difference between N and the actual number of steps taken is
5615 returned."
5616   (interactive "p")
5617   (let ((backward (< n 0))
5618         (n (abs n)))
5619     (while (and (> n 0)
5620                 (if backward
5621                     (gnus-summary-find-prev unread)
5622                   (gnus-summary-find-next unread)))
5623       (gnus-summary-show-thread)
5624       (setq n (1- n)))
5625     (when (/= 0 n)
5626       (gnus-message 7 "No more%s articles"
5627                     (if unread " unread" "")))
5628     (unless dont-display
5629       (gnus-summary-recenter)
5630       (gnus-summary-position-point))
5631     n))
5632
5633 (defun gnus-summary-next-unread-subject (n)
5634   "Go to next N'th unread summary line."
5635   (interactive "p")
5636   (gnus-summary-next-subject n t))
5637
5638 (defun gnus-summary-prev-subject (n &optional unread)
5639   "Go to previous N'th summary line.
5640 If optional argument UNREAD is non-nil, only unread article is selected."
5641   (interactive "p")
5642   (gnus-summary-next-subject (- n) unread))
5643
5644 (defun gnus-summary-prev-unread-subject (n)
5645   "Go to previous N'th unread summary line."
5646   (interactive "p")
5647   (gnus-summary-next-subject (- n) t))
5648
5649 (defun gnus-summary-goto-subject (article &optional force silent)
5650   "Go the subject line of ARTICLE.
5651 If FORCE, also allow jumping to articles not currently shown."
5652   (interactive "nArticle number: ")
5653   (let ((b (point))
5654         (data (gnus-data-find article)))
5655     ;; We read in the article if we have to.
5656     (and (not data)
5657          force
5658          (gnus-summary-insert-subject
5659           article
5660           (if (or (numberp force) (vectorp force)) force)
5661           t)
5662          (setq data (gnus-data-find article)))
5663     (goto-char b)
5664     (if (not data)
5665         (progn
5666           (unless silent
5667             (gnus-message 3 "Can't find article %d" article))
5668           nil)
5669       (goto-char (gnus-data-pos data))
5670       (gnus-summary-position-point)
5671       article)))
5672
5673 ;; Walking around summary lines with displaying articles.
5674
5675 (defun gnus-summary-expand-window (&optional arg)
5676   "Make the summary buffer take up the entire Emacs frame.
5677 Given a prefix, will force an `article' buffer configuration."
5678   (interactive "P")
5679   (if arg
5680       (gnus-configure-windows 'article 'force)
5681     (gnus-configure-windows 'summary 'force)))
5682
5683 (defun gnus-summary-display-article (article &optional all-header)
5684   "Display ARTICLE in article buffer."
5685   (gnus-set-global-variables)
5686   (if (null article)
5687       nil
5688     (prog1
5689         (if gnus-summary-display-article-function
5690             (funcall gnus-summary-display-article-function article all-header)
5691           (gnus-article-prepare article all-header))
5692       (gnus-run-hooks 'gnus-select-article-hook)
5693       (when (and gnus-current-article
5694                  (not (zerop gnus-current-article)))
5695         (gnus-summary-goto-subject gnus-current-article))
5696       (gnus-summary-recenter)
5697       (when (and gnus-use-trees gnus-show-threads)
5698         (gnus-possibly-generate-tree article)
5699         (gnus-highlight-selected-tree article))
5700       ;; Successfully display article.
5701       (gnus-article-set-window-start
5702        (cdr (assq article gnus-newsgroup-bookmarks))))))
5703
5704 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5705   "Select the current article.
5706 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5707 non-nil, the article will be re-fetched even if it already present in
5708 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5709 be displayed."
5710   ;; Make sure we are in the summary buffer to work around bbdb bug.
5711   (unless (eq major-mode 'gnus-summary-mode)
5712     (set-buffer gnus-summary-buffer))
5713   (let ((article (or article (gnus-summary-article-number)))
5714         (all-headers (not (not all-headers))) ;Must be T or NIL.
5715         gnus-summary-display-article-function
5716         did)
5717     (and (not pseudo)
5718          (gnus-summary-article-pseudo-p article)
5719          (error "This is a pseudo-article"))
5720     (prog1
5721         (save-excursion
5722           (set-buffer gnus-summary-buffer)
5723           (if (or (and gnus-single-article-buffer
5724                        (or (null gnus-current-article)
5725                            (null gnus-article-current)
5726                            (null (get-buffer gnus-article-buffer))
5727                            (not (eq article (cdr gnus-article-current)))
5728                            (not (equal (car gnus-article-current)
5729                                        gnus-newsgroup-name))))
5730                   (and (not gnus-single-article-buffer)
5731                        (or (null gnus-current-article)
5732                            (not (eq gnus-current-article article))))
5733                   force)
5734               ;; The requested article is different from the current article.
5735               (prog1
5736                   (gnus-summary-display-article article all-headers)
5737                 (setq did article)
5738                 (when (or all-headers gnus-show-all-headers)
5739                   (gnus-article-show-all-headers)))
5740             (when (or all-headers gnus-show-all-headers)
5741               (gnus-article-show-all-headers))
5742             'old))
5743       (when did
5744         (gnus-article-set-window-start
5745          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5746
5747 (defun gnus-summary-set-current-mark (&optional current-mark)
5748   "Obsolete function."
5749   nil)
5750
5751 (defun gnus-summary-next-article (&optional unread subject backward push)
5752   "Select the next article.
5753 If UNREAD, only unread articles are selected.
5754 If SUBJECT, only articles with SUBJECT are selected.
5755 If BACKWARD, the previous article is selected instead of the next."
5756   (interactive "P")
5757   (cond
5758    ;; Is there such an article?
5759    ((and (gnus-summary-search-forward unread subject backward)
5760          (or (gnus-summary-display-article (gnus-summary-article-number))
5761              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5762     (gnus-summary-position-point))
5763    ;; If not, we try the first unread, if that is wanted.
5764    ((and subject
5765          gnus-auto-select-same
5766          (gnus-summary-first-unread-article))
5767     (gnus-summary-position-point)
5768     (gnus-message 6 "Wrapped"))
5769    ;; Try to get next/previous article not displayed in this group.
5770    ((and gnus-auto-extend-newsgroup
5771          (not unread) (not subject))
5772     (gnus-summary-goto-article
5773      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5774      nil (count-lines (point-min) (point))))
5775    ;; Go to next/previous group.
5776    (t
5777     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5778       (gnus-summary-jump-to-group gnus-newsgroup-name))
5779     (let ((cmd last-command-char)
5780           (point
5781            (save-excursion
5782              (set-buffer gnus-group-buffer)
5783              (point)))
5784           (group
5785            (if (eq gnus-keep-same-level 'best)
5786                (gnus-summary-best-group gnus-newsgroup-name)
5787              (gnus-summary-search-group backward gnus-keep-same-level))))
5788       ;; For some reason, the group window gets selected.  We change
5789       ;; it back.
5790       (select-window (get-buffer-window (current-buffer)))
5791       ;; Select next unread newsgroup automagically.
5792       (cond
5793        ((or (not gnus-auto-select-next)
5794             (not cmd))
5795         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5796        ((or (eq gnus-auto-select-next 'quietly)
5797             (and (eq gnus-auto-select-next 'slightly-quietly)
5798                  push)
5799             (and (eq gnus-auto-select-next 'almost-quietly)
5800                  (gnus-summary-last-article-p)))
5801         ;; Select quietly.
5802         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5803             (gnus-summary-exit)
5804           (gnus-message 7 "No more%s articles (%s)..."
5805                         (if unread " unread" "")
5806                         (if group (concat "selecting " group)
5807                           "exiting"))
5808           (gnus-summary-next-group nil group backward)))
5809        (t
5810         (when (gnus-key-press-event-p last-input-event)
5811           (gnus-summary-walk-group-buffer
5812            gnus-newsgroup-name cmd unread backward point))))))))
5813
5814 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5815   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5816                       (?\C-p (gnus-group-prev-unread-group 1))))
5817         (cursor-in-echo-area t)
5818         keve key group ended)
5819     (save-excursion
5820       (set-buffer gnus-group-buffer)
5821       (goto-char start)
5822       (setq group
5823             (if (eq gnus-keep-same-level 'best)
5824                 (gnus-summary-best-group gnus-newsgroup-name)
5825               (gnus-summary-search-group backward gnus-keep-same-level))))
5826     (while (not ended)
5827       (gnus-message
5828        5 "No more%s articles%s" (if unread " unread" "")
5829        (if (and group
5830                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5831            (format " (Type %s for %s [%s])"
5832                    (single-key-description cmd) group
5833                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5834          (format " (Type %s to exit %s)"
5835                  (single-key-description cmd)
5836                  gnus-newsgroup-name)))
5837       ;; Confirm auto selection.
5838       (setq key (car (setq keve (gnus-read-event-char))))
5839       (setq ended t)
5840       (cond
5841        ((assq key keystrokes)
5842         (let ((obuf (current-buffer)))
5843           (switch-to-buffer gnus-group-buffer)
5844           (when group
5845             (gnus-group-jump-to-group group))
5846           (eval (cadr (assq key keystrokes)))
5847           (setq group (gnus-group-group-name))
5848           (switch-to-buffer obuf))
5849         (setq ended nil))
5850        ((equal key cmd)
5851         (if (or (not group)
5852                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5853             (gnus-summary-exit)
5854           (gnus-summary-next-group nil group backward)))
5855        (t
5856         (push (cdr keve) unread-command-events))))))
5857
5858 (defun gnus-summary-next-unread-article ()
5859   "Select unread article after current one."
5860   (interactive)
5861   (gnus-summary-next-article
5862    (or (not (eq gnus-summary-goto-unread 'never))
5863        (gnus-summary-last-article-p (gnus-summary-article-number)))
5864    (and gnus-auto-select-same
5865         (gnus-summary-article-subject))))
5866
5867 (defun gnus-summary-prev-article (&optional unread subject)
5868   "Select the article after the current one.
5869 If UNREAD is non-nil, only unread articles are selected."
5870   (interactive "P")
5871   (gnus-summary-next-article unread subject t))
5872
5873 (defun gnus-summary-prev-unread-article ()
5874   "Select unread article before current one."
5875   (interactive)
5876   (gnus-summary-prev-article
5877    (or (not (eq gnus-summary-goto-unread 'never))
5878        (gnus-summary-first-article-p (gnus-summary-article-number)))
5879    (and gnus-auto-select-same
5880         (gnus-summary-article-subject))))
5881
5882 (defun gnus-summary-next-page (&optional lines circular)
5883   "Show next page of the selected article.
5884 If at the end of the current article, select the next article.
5885 LINES says how many lines should be scrolled up.
5886
5887 If CIRCULAR is non-nil, go to the start of the article instead of
5888 selecting the next article when reaching the end of the current
5889 article."
5890   (interactive "P")
5891   (setq gnus-summary-buffer (current-buffer))
5892   (gnus-set-global-variables)
5893   (let ((article (gnus-summary-article-number))
5894         (article-window (get-buffer-window gnus-article-buffer t))
5895         endp)
5896     ;; If the buffer is empty, we have no article.
5897     (unless article
5898       (error "No article to select"))
5899     (gnus-configure-windows 'article)
5900     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5901         (if (and (eq gnus-summary-goto-unread 'never)
5902                  (not (gnus-summary-last-article-p article)))
5903             (gnus-summary-next-article)
5904           (gnus-summary-next-unread-article))
5905       (if (or (null gnus-current-article)
5906               (null gnus-article-current)
5907               (/= article (cdr gnus-article-current))
5908               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5909           ;; Selected subject is different from current article's.
5910           (gnus-summary-display-article article)
5911         (when article-window
5912           (gnus-eval-in-buffer-window gnus-article-buffer
5913             (setq endp (gnus-article-next-page lines)))
5914           (when endp
5915             (cond (circular
5916                    (gnus-summary-beginning-of-article))
5917                   (lines
5918                    (gnus-message 3 "End of message"))
5919                   ((null lines)
5920                    (if (and (eq gnus-summary-goto-unread 'never)
5921                             (not (gnus-summary-last-article-p article)))
5922                        (gnus-summary-next-article)
5923                      (gnus-summary-next-unread-article))))))))
5924     (gnus-summary-recenter)
5925     (gnus-summary-position-point)))
5926
5927 (defun gnus-summary-prev-page (&optional lines move)
5928   "Show previous page of selected article.
5929 Argument LINES specifies lines to be scrolled down.
5930 If MOVE, move to the previous unread article if point is at
5931 the beginning of the buffer."
5932   (interactive "P")
5933   (let ((article (gnus-summary-article-number))
5934         (article-window (get-buffer-window gnus-article-buffer t))
5935         endp)
5936     (gnus-configure-windows 'article)
5937     (if (or (null gnus-current-article)
5938             (null gnus-article-current)
5939             (/= article (cdr gnus-article-current))
5940             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5941         ;; Selected subject is different from current article's.
5942         (gnus-summary-display-article article)
5943       (gnus-summary-recenter)
5944       (when article-window
5945         (gnus-eval-in-buffer-window gnus-article-buffer
5946           (setq endp (gnus-article-prev-page lines)))
5947         (when (and move endp)
5948           (cond (lines
5949                  (gnus-message 3 "Beginning of message"))
5950                 ((null lines)
5951                  (if (and (eq gnus-summary-goto-unread 'never)
5952                           (not (gnus-summary-first-article-p article)))
5953                      (gnus-summary-prev-article)
5954                    (gnus-summary-prev-unread-article))))))))
5955   (gnus-summary-position-point))
5956
5957 (defun gnus-summary-prev-page-or-article (&optional lines)
5958   "Show previous page of selected article.
5959 Argument LINES specifies lines to be scrolled down.
5960 If at the beginning of the article, go to the next article."
5961   (interactive "P")
5962   (gnus-summary-prev-page lines t))
5963
5964 (defun gnus-summary-scroll-up (lines)
5965   "Scroll up (or down) one line current article.
5966 Argument LINES specifies lines to be scrolled up (or down if negative)."
5967   (interactive "p")
5968   (gnus-configure-windows 'article)
5969   (gnus-summary-show-thread)
5970   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5971     (gnus-eval-in-buffer-window gnus-article-buffer
5972       (cond ((> lines 0)
5973              (when (gnus-article-next-page lines)
5974                (gnus-message 3 "End of message")))
5975             ((< lines 0)
5976              (gnus-article-prev-page (- lines))))))
5977   (gnus-summary-recenter)
5978   (gnus-summary-position-point))
5979
5980 (defun gnus-summary-scroll-down (lines)
5981   "Scroll down (or up) one line current article.
5982 Argument LINES specifies lines to be scrolled down (or up if negative)."
5983   (interactive "p")
5984   (gnus-summary-scroll-up (- lines)))
5985
5986 (defun gnus-summary-next-same-subject ()
5987   "Select next article which has the same subject as current one."
5988   (interactive)
5989   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5990
5991 (defun gnus-summary-prev-same-subject ()
5992   "Select previous article which has the same subject as current one."
5993   (interactive)
5994   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5995
5996 (defun gnus-summary-next-unread-same-subject ()
5997   "Select next unread article which has the same subject as current one."
5998   (interactive)
5999   (gnus-summary-next-article t (gnus-summary-article-subject)))
6000
6001 (defun gnus-summary-prev-unread-same-subject ()
6002   "Select previous unread article which has the same subject as current one."
6003   (interactive)
6004   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6005
6006 (defun gnus-summary-first-unread-article ()
6007   "Select the first unread article.
6008 Return nil if there are no unread articles."
6009   (interactive)
6010   (prog1
6011       (when (gnus-summary-first-subject t)
6012         (gnus-summary-show-thread)
6013         (gnus-summary-first-subject t)
6014         (gnus-summary-display-article (gnus-summary-article-number)))
6015     (gnus-summary-position-point)))
6016
6017 (defun gnus-summary-first-unread-subject ()
6018   "Place the point on the subject line of the first unread article.
6019 Return nil if there are no unread articles."
6020   (interactive)
6021   (prog1
6022       (when (gnus-summary-first-subject t)
6023         (gnus-summary-show-thread)
6024         (gnus-summary-first-subject t))
6025     (gnus-summary-position-point)))
6026
6027 (defun gnus-summary-first-article ()
6028   "Select the first article.
6029 Return nil if there are no articles."
6030   (interactive)
6031   (prog1
6032       (when (gnus-summary-first-subject)
6033         (gnus-summary-show-thread)
6034         (gnus-summary-first-subject)
6035         (gnus-summary-display-article (gnus-summary-article-number)))
6036     (gnus-summary-position-point)))
6037
6038 (defun gnus-summary-best-unread-article ()
6039   "Select the unread article with the highest score."
6040   (interactive)
6041   (let ((best -1000000)
6042         (data gnus-newsgroup-data)
6043         article score)
6044     (while data
6045       (and (gnus-data-unread-p (car data))
6046            (> (setq score
6047                     (gnus-summary-article-score (gnus-data-number (car data))))
6048               best)
6049            (setq best score
6050                  article (gnus-data-number (car data))))
6051       (setq data (cdr data)))
6052     (prog1
6053         (if article
6054             (gnus-summary-goto-article article)
6055           (error "No unread articles"))
6056       (gnus-summary-position-point))))
6057
6058 (defun gnus-summary-last-subject ()
6059   "Go to the last displayed subject line in the group."
6060   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6061     (when article
6062       (gnus-summary-goto-subject article))))
6063
6064 (defun gnus-summary-goto-article (article &optional all-headers force)
6065   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6066 If ALL-HEADERS is non-nil, no header lines are hidden.
6067 If FORCE, go to the article even if it isn't displayed.  If FORCE
6068 is a number, it is the line the article is to be displayed on."
6069   (interactive
6070    (list
6071     (completing-read
6072      "Article number or Message-ID: "
6073      (mapcar (lambda (number) (list (int-to-string number)))
6074              gnus-newsgroup-limit))
6075     current-prefix-arg
6076     t))
6077   (prog1
6078       (if (and (stringp article)
6079                (string-match "@" article))
6080           (gnus-summary-refer-article article)
6081         (when (stringp article)
6082           (setq article (string-to-number article)))
6083         (if (gnus-summary-goto-subject article force)
6084             (gnus-summary-display-article article all-headers)
6085           (gnus-message 4 "Couldn't go to article %s" article) nil))
6086     (gnus-summary-position-point)))
6087
6088 (defun gnus-summary-goto-last-article ()
6089   "Go to the previously read article."
6090   (interactive)
6091   (prog1
6092       (when gnus-last-article
6093         (gnus-summary-goto-article gnus-last-article nil t))
6094     (gnus-summary-position-point)))
6095
6096 (defun gnus-summary-pop-article (number)
6097   "Pop one article off the history and go to the previous.
6098 NUMBER articles will be popped off."
6099   (interactive "p")
6100   (let (to)
6101     (setq gnus-newsgroup-history
6102           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6103     (if to
6104         (gnus-summary-goto-article (car to) nil t)
6105       (error "Article history empty")))
6106   (gnus-summary-position-point))
6107
6108 ;; Summary commands and functions for limiting the summary buffer.
6109
6110 (defun gnus-summary-limit-to-articles (n)
6111   "Limit the summary buffer to the next N articles.
6112 If not given a prefix, use the process marked articles instead."
6113   (interactive "P")
6114   (prog1
6115       (let ((articles (gnus-summary-work-articles n)))
6116         (setq gnus-newsgroup-processable nil)
6117         (gnus-summary-limit articles))
6118     (gnus-summary-position-point)))
6119
6120 (defun gnus-summary-pop-limit (&optional total)
6121   "Restore the previous limit.
6122 If given a prefix, remove all limits."
6123   (interactive "P")
6124   (when total
6125     (setq gnus-newsgroup-limits
6126           (list (mapcar (lambda (h) (mail-header-number h))
6127                         gnus-newsgroup-headers))))
6128   (unless gnus-newsgroup-limits
6129     (error "No limit to pop"))
6130   (prog1
6131       (gnus-summary-limit nil 'pop)
6132     (gnus-summary-position-point)))
6133
6134 (defun gnus-summary-limit-to-subject (subject &optional header)
6135   "Limit the summary buffer to articles that have subjects that match a regexp."
6136   (interactive "sLimit to subject (regexp): ")
6137   (unless header
6138     (setq header "subject"))
6139   (when (not (equal "" subject))
6140     (prog1
6141         (let ((articles (gnus-summary-find-matching
6142                          (or header "subject") subject 'all)))
6143           (unless articles
6144             (error "Found no matches for \"%s\"" subject))
6145           (gnus-summary-limit articles))
6146       (gnus-summary-position-point))))
6147
6148 (defun gnus-summary-limit-to-author (from)
6149   "Limit the summary buffer to articles that have authors that match a regexp."
6150   (interactive "sLimit to author (regexp): ")
6151   (gnus-summary-limit-to-subject from "from"))
6152
6153 (defun gnus-summary-limit-to-age (age &optional younger-p)
6154   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6155 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6156 articles that are younger than AGE days."
6157   (interactive "nLimit to articles older than (in days): \nP")
6158   (prog1
6159       (let ((data gnus-newsgroup-data)
6160             (cutoff (days-to-time age))
6161             articles d date is-younger)
6162         (while (setq d (pop data))
6163           (when (and (vectorp (gnus-data-header d))
6164                      (setq date (mail-header-date (gnus-data-header d))))
6165             (setq is-younger (time-less-p
6166                               (time-since (date-to-time date))
6167                               cutoff))
6168             (when (if younger-p
6169                       is-younger
6170                     (not is-younger))
6171               (push (gnus-data-number d) articles))))
6172         (gnus-summary-limit (nreverse articles)))
6173     (gnus-summary-position-point)))
6174
6175 (defun gnus-summary-limit-to-extra (header regexp)
6176   "Limit the summary buffer to articles that match an 'extra' header."
6177   (interactive
6178    (let ((header
6179           (intern
6180            (gnus-completing-read
6181             (symbol-name (car gnus-extra-headers))      
6182             "Score extra header:"       
6183             (mapcar (lambda (x) 
6184                       (cons (symbol-name x) x))
6185                     gnus-extra-headers)
6186             nil                 
6187             t))))
6188      (list header
6189            (read-string (format "Limit to header %s (regexp): " header)))))
6190   (when (not (equal "" regexp))
6191     (prog1
6192         (let ((articles (gnus-summary-find-matching
6193                          (cons 'extra header) regexp 'all)))
6194           (unless articles
6195             (error "Found no matches for \"%s\"" regexp))
6196           (gnus-summary-limit articles))
6197       (gnus-summary-position-point))))
6198
6199 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6200 (make-obsolete
6201  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6202
6203 (defun gnus-summary-limit-to-unread (&optional all)
6204   "Limit the summary buffer to articles that are not marked as read.
6205 If ALL is non-nil, limit strictly to unread articles."
6206   (interactive "P")
6207   (if all
6208       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6209     (gnus-summary-limit-to-marks
6210      ;; Concat all the marks that say that an article is read and have
6211      ;; those removed.
6212      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6213            gnus-killed-mark gnus-kill-file-mark
6214            gnus-low-score-mark gnus-expirable-mark
6215            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6216            gnus-duplicate-mark gnus-souped-mark)
6217      'reverse)))
6218
6219 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6220 (make-obsolete 'gnus-summary-delete-marked-with
6221                'gnus-summary-limit-exlude-marks)
6222
6223 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6224   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6225 If REVERSE, limit the summary buffer to articles that are marked
6226 with MARKS.  MARKS can either be a string of marks or a list of marks.
6227 Returns how many articles were removed."
6228   (interactive "sMarks: ")
6229   (gnus-summary-limit-to-marks marks t))
6230
6231 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6232   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6233 If REVERSE (the prefix), limit the summary buffer to articles that are
6234 not marked with MARKS.  MARKS can either be a string of marks or a
6235 list of marks.
6236 Returns how many articles were removed."
6237   (interactive "sMarks: \nP")
6238   (prog1
6239       (let ((data gnus-newsgroup-data)
6240             (marks (if (listp marks) marks
6241                      (append marks nil))) ; Transform to list.
6242             articles)
6243         (while data
6244           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6245                   (memq (gnus-data-mark (car data)) marks))
6246             (push (gnus-data-number (car data)) articles))
6247           (setq data (cdr data)))
6248         (gnus-summary-limit articles))
6249     (gnus-summary-position-point)))
6250
6251 (defun gnus-summary-limit-to-score (&optional score)
6252   "Limit to articles with score at or above SCORE."
6253   (interactive "P")
6254   (setq score (if score
6255                   (prefix-numeric-value score)
6256                 (or gnus-summary-default-score 0)))
6257   (let ((data gnus-newsgroup-data)
6258         articles)
6259     (while data
6260       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6261                 score)
6262         (push (gnus-data-number (car data)) articles))
6263       (setq data (cdr data)))
6264     (prog1
6265         (gnus-summary-limit articles)
6266       (gnus-summary-position-point))))
6267
6268 (defun gnus-summary-limit-include-thread (id)
6269   "Display all the hidden articles that in the current thread."
6270   (interactive (list (mail-header-id (gnus-summary-article-header))))
6271   (let ((articles (gnus-articles-in-thread
6272                    (gnus-id-to-thread (gnus-root-id id)))))
6273     (prog1
6274         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6275       (gnus-summary-position-point))))
6276
6277 (defun gnus-summary-limit-include-dormant ()
6278   "Display all the hidden articles that are marked as dormant.
6279 Note that this command only works on a subset of the articles currently
6280 fetched for this group."
6281   (interactive)
6282   (unless gnus-newsgroup-dormant
6283     (error "There are no dormant articles in this group"))
6284   (prog1
6285       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6286     (gnus-summary-position-point)))
6287
6288 (defun gnus-summary-limit-exclude-dormant ()
6289   "Hide all dormant articles."
6290   (interactive)
6291   (prog1
6292       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6293     (gnus-summary-position-point)))
6294
6295 (defun gnus-summary-limit-exclude-childless-dormant ()
6296   "Hide all dormant articles that have no children."
6297   (interactive)
6298   (let ((data (gnus-data-list t))
6299         articles d children)
6300     ;; Find all articles that are either not dormant or have
6301     ;; children.
6302     (while (setq d (pop data))
6303       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6304                 (and (setq children
6305                            (gnus-article-children (gnus-data-number d)))
6306                      (let (found)
6307                        (while children
6308                          (when (memq (car children) articles)
6309                            (setq children nil
6310                                  found t))
6311                          (pop children))
6312                        found)))
6313         (push (gnus-data-number d) articles)))
6314     ;; Do the limiting.
6315     (prog1
6316         (gnus-summary-limit articles)
6317       (gnus-summary-position-point))))
6318
6319 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6320   "Mark all unread excluded articles as read.
6321 If ALL, mark even excluded ticked and dormants as read."
6322   (interactive "P")
6323   (let ((articles (gnus-sorted-complement
6324                    (sort
6325                     (mapcar (lambda (h) (mail-header-number h))
6326                             gnus-newsgroup-headers)
6327                     '<)
6328                    (sort gnus-newsgroup-limit '<)))
6329         article)
6330     (setq gnus-newsgroup-unreads
6331           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6332     (if all
6333         (setq gnus-newsgroup-dormant nil
6334               gnus-newsgroup-marked nil
6335               gnus-newsgroup-reads
6336               (nconc
6337                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6338                gnus-newsgroup-reads))
6339       (while (setq article (pop articles))
6340         (unless (or (memq article gnus-newsgroup-dormant)
6341                     (memq article gnus-newsgroup-marked))
6342           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6343
6344 (defun gnus-summary-limit (articles &optional pop)
6345   (if pop
6346       ;; We pop the previous limit off the stack and use that.
6347       (setq articles (car gnus-newsgroup-limits)
6348             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6349     ;; We use the new limit, so we push the old limit on the stack.
6350     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6351   ;; Set the limit.
6352   (setq gnus-newsgroup-limit articles)
6353   (let ((total (length gnus-newsgroup-data))
6354         (data (gnus-data-find-list (gnus-summary-article-number)))
6355         (gnus-summary-mark-below nil)   ; Inhibit this.
6356         found)
6357     ;; This will do all the work of generating the new summary buffer
6358     ;; according to the new limit.
6359     (gnus-summary-prepare)
6360     ;; Hide any threads, possibly.
6361     (and gnus-show-threads
6362          gnus-thread-hide-subtree
6363          (gnus-summary-hide-all-threads))
6364     ;; Try to return to the article you were at, or one in the
6365     ;; neighborhood.
6366     (when data
6367       ;; We try to find some article after the current one.
6368       (while data
6369         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6370           (setq data nil
6371                 found t))
6372         (setq data (cdr data))))
6373     (unless found
6374       ;; If there is no data, that means that we were after the last
6375       ;; article.  The same goes when we can't find any articles
6376       ;; after the current one.
6377       (goto-char (point-max))
6378       (gnus-summary-find-prev))
6379     (gnus-set-mode-line 'summary)
6380     ;; We return how many articles were removed from the summary
6381     ;; buffer as a result of the new limit.
6382     (- total (length gnus-newsgroup-data))))
6383
6384 (defsubst gnus-invisible-cut-children (threads)
6385   (let ((num 0))
6386     (while threads
6387       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6388         (incf num))
6389       (pop threads))
6390     (< num 2)))
6391
6392 (defsubst gnus-cut-thread (thread)
6393   "Go forwards in the thread until we find an article that we want to display."
6394   (when (or (eq gnus-fetch-old-headers 'some)
6395             (eq gnus-fetch-old-headers 'invisible)
6396             (eq gnus-build-sparse-threads 'some)
6397             (eq gnus-build-sparse-threads 'more))
6398     ;; Deal with old-fetched headers and sparse threads.
6399     (while (and
6400             thread
6401             (or
6402              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6403              (gnus-summary-article-ancient-p
6404               (mail-header-number (car thread))))
6405             (if (or (<= (length (cdr thread)) 1)
6406                     (eq gnus-fetch-old-headers 'invisible))
6407                 (setq gnus-newsgroup-limit
6408                       (delq (mail-header-number (car thread))
6409                             gnus-newsgroup-limit)
6410                       thread (cadr thread))
6411               (when (gnus-invisible-cut-children (cdr thread))
6412                 (let ((th (cdr thread)))
6413                   (while th
6414                     (if (memq (mail-header-number (caar th))
6415                               gnus-newsgroup-limit)
6416                         (setq thread (car th)
6417                               th nil)
6418                       (setq th (cdr th))))))))))
6419   thread)
6420
6421 (defun gnus-cut-threads (threads)
6422   "Cut off all uninteresting articles from the beginning of threads."
6423   (when (or (eq gnus-fetch-old-headers 'some)
6424             (eq gnus-fetch-old-headers 'invisible)
6425             (eq gnus-build-sparse-threads 'some)
6426             (eq gnus-build-sparse-threads 'more))
6427     (let ((th threads))
6428       (while th
6429         (setcar th (gnus-cut-thread (car th)))
6430         (setq th (cdr th)))))
6431   ;; Remove nixed out threads.
6432   (delq nil threads))
6433
6434 (defun gnus-summary-initial-limit (&optional show-if-empty)
6435   "Figure out what the initial limit is supposed to be on group entry.
6436 This entails weeding out unwanted dormants, low-scored articles,
6437 fetch-old-headers verbiage, and so on."
6438   ;; Most groups have nothing to remove.
6439   (if (or gnus-inhibit-limiting
6440           (and (null gnus-newsgroup-dormant)
6441                (not (eq gnus-fetch-old-headers 'some))
6442                (not (eq gnus-fetch-old-headers 'invisible))
6443                (null gnus-summary-expunge-below)
6444                (not (eq gnus-build-sparse-threads 'some))
6445                (not (eq gnus-build-sparse-threads 'more))
6446                (null gnus-thread-expunge-below)
6447                (not gnus-use-nocem)))
6448       ()                                ; Do nothing.
6449     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6450     (setq gnus-newsgroup-limit nil)
6451     (mapatoms
6452      (lambda (node)
6453        (unless (car (symbol-value node))
6454          ;; These threads have no parents -- they are roots.
6455          (let ((nodes (cdr (symbol-value node)))
6456                thread)
6457            (while nodes
6458              (if (and gnus-thread-expunge-below
6459                       (< (gnus-thread-total-score (car nodes))
6460                          gnus-thread-expunge-below))
6461                  (gnus-expunge-thread (pop nodes))
6462                (setq thread (pop nodes))
6463                (gnus-summary-limit-children thread))))))
6464      gnus-newsgroup-dependencies)
6465     ;; If this limitation resulted in an empty group, we might
6466     ;; pop the previous limit and use it instead.
6467     (when (and (not gnus-newsgroup-limit)
6468                show-if-empty)
6469       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6470     gnus-newsgroup-limit))
6471
6472 (defun gnus-summary-limit-children (thread)
6473   "Return 1 if this subthread is visible and 0 if it is not."
6474   ;; First we get the number of visible children to this thread.  This
6475   ;; is done by recursing down the thread using this function, so this
6476   ;; will really go down to a leaf article first, before slowly
6477   ;; working its way up towards the root.
6478   (when thread
6479     (let ((children
6480            (if (cdr thread)
6481                (apply '+ (mapcar 'gnus-summary-limit-children
6482                                  (cdr thread)))
6483              0))
6484           (number (mail-header-number (car thread)))
6485           score)
6486       (if (and
6487            (not (memq number gnus-newsgroup-marked))
6488            (or
6489             ;; If this article is dormant and has absolutely no visible
6490             ;; children, then this article isn't visible.
6491             (and (memq number gnus-newsgroup-dormant)
6492                  (zerop children))
6493             ;; If this is "fetch-old-headered" and there is no
6494             ;; visible children, then we don't want this article.
6495             (and (eq gnus-fetch-old-headers 'some)
6496                  (gnus-summary-article-ancient-p number)
6497                  (zerop children))
6498             ;; If this is "fetch-old-headered" and `invisible', then
6499             ;; we don't want this article.
6500             (and (eq gnus-fetch-old-headers 'invisible)
6501                  (gnus-summary-article-ancient-p number))
6502             ;; If this is a sparsely inserted article with no children,
6503             ;; we don't want it.
6504             (and (eq gnus-build-sparse-threads 'some)
6505                  (gnus-summary-article-sparse-p number)
6506                  (zerop children))
6507             ;; If we use expunging, and this article is really
6508             ;; low-scored, then we don't want this article.
6509             (when (and gnus-summary-expunge-below
6510                        (< (setq score
6511                                 (or (cdr (assq number gnus-newsgroup-scored))
6512                                     gnus-summary-default-score))
6513                           gnus-summary-expunge-below))
6514               ;; We increase the expunge-tally here, but that has
6515               ;; nothing to do with the limits, really.
6516               (incf gnus-newsgroup-expunged-tally)
6517               ;; We also mark as read here, if that's wanted.
6518               (when (and gnus-summary-mark-below
6519                          (< score gnus-summary-mark-below))
6520                 (setq gnus-newsgroup-unreads
6521                       (delq number gnus-newsgroup-unreads))
6522                 (if gnus-newsgroup-auto-expire
6523                     (push number gnus-newsgroup-expirable)
6524                   (push (cons number gnus-low-score-mark)
6525                         gnus-newsgroup-reads)))
6526               t)
6527             ;; Check NoCeM things.
6528             (if (and gnus-use-nocem
6529                      (gnus-nocem-unwanted-article-p
6530                       (mail-header-id (car thread))))
6531                 (progn
6532                   (setq gnus-newsgroup-unreads
6533                         (delq number gnus-newsgroup-unreads))
6534                   t))))
6535           ;; Nope, invisible article.
6536           0
6537         ;; Ok, this article is to be visible, so we add it to the limit
6538         ;; and return 1.
6539         (push number gnus-newsgroup-limit)
6540         1))))
6541
6542 (defun gnus-expunge-thread (thread)
6543   "Mark all articles in THREAD as read."
6544   (let* ((number (mail-header-number (car thread))))
6545     (incf gnus-newsgroup-expunged-tally)
6546     ;; We also mark as read here, if that's wanted.
6547     (setq gnus-newsgroup-unreads
6548           (delq number gnus-newsgroup-unreads))
6549     (if gnus-newsgroup-auto-expire
6550         (push number gnus-newsgroup-expirable)
6551       (push (cons number gnus-low-score-mark)
6552             gnus-newsgroup-reads)))
6553   ;; Go recursively through all subthreads.
6554   (mapcar 'gnus-expunge-thread (cdr thread)))
6555
6556 ;; Summary article oriented commands
6557
6558 (defun gnus-summary-refer-parent-article (n)
6559   "Refer parent article N times.
6560 If N is negative, go to ancestor -N instead.
6561 The difference between N and the number of articles fetched is returned."
6562   (interactive "p")
6563   (let ((skip 1)
6564         error header ref)
6565     (when (not (natnump n))
6566       (setq skip (abs n)
6567             n 1))
6568     (while (and (> n 0)
6569                 (not error))
6570       (setq header (gnus-summary-article-header))
6571       (if (and (eq (mail-header-number header)
6572                    (cdr gnus-article-current))
6573                (equal gnus-newsgroup-name
6574                       (car gnus-article-current)))
6575           ;; If we try to find the parent of the currently
6576           ;; displayed article, then we take a look at the actual
6577           ;; References header, since this is slightly more
6578           ;; reliable than the References field we got from the
6579           ;; server.
6580           (save-excursion
6581             (set-buffer gnus-original-article-buffer)
6582             (nnheader-narrow-to-headers)
6583             (unless (setq ref (message-fetch-field "references"))
6584               (setq ref (message-fetch-field "in-reply-to")))
6585             (widen))
6586         (setq ref
6587               ;; It's not the current article, so we take a bet on
6588               ;; the value we got from the server.
6589               (mail-header-references header)))
6590       (if (and ref
6591                (not (equal ref "")))
6592           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6593             (gnus-message 1 "Couldn't find parent"))
6594         (gnus-message 1 "No references in article %d"
6595                       (gnus-summary-article-number))
6596         (setq error t))
6597       (decf n))
6598     (gnus-summary-position-point)
6599     n))
6600
6601 (defun gnus-summary-refer-references ()
6602   "Fetch all articles mentioned in the References header.
6603 Return the number of articles fetched."
6604   (interactive)
6605   (let ((ref (mail-header-references (gnus-summary-article-header)))
6606         (current (gnus-summary-article-number))
6607         (n 0))
6608     (if (or (not ref)
6609             (equal ref ""))
6610         (error "No References in the current article")
6611       ;; For each Message-ID in the References header...
6612       (while (string-match "<[^>]*>" ref)
6613         (incf n)
6614         ;; ... fetch that article.
6615         (gnus-summary-refer-article
6616          (prog1 (match-string 0 ref)
6617            (setq ref (substring ref (match-end 0))))))
6618       (gnus-summary-goto-subject current)
6619       (gnus-summary-position-point)
6620       n)))
6621
6622 (defun gnus-summary-refer-thread (&optional limit)
6623   "Fetch all articles in the current thread.
6624 If LIMIT (the numerical prefix), fetch that many old headers instead
6625 of what's specified by the `gnus-refer-thread-limit' variable."
6626   (interactive "P")
6627   (let ((id (mail-header-id (gnus-summary-article-header)))
6628         (limit (if limit (prefix-numeric-value limit)
6629                  gnus-refer-thread-limit)))
6630     ;; We want to fetch LIMIT *old* headers, but we also have to
6631     ;; re-fetch all the headers in the current buffer, because many of
6632     ;; them may be undisplayed.  So we adjust LIMIT.
6633     (when (numberp limit)
6634       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6635     (unless (eq gnus-fetch-old-headers 'invisible)
6636       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6637       ;; Retrieve the headers and read them in.
6638       (if (eq (gnus-retrieve-headers
6639                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6640               'nov)
6641           (gnus-build-all-threads)
6642         (error "Can't fetch thread from backends that don't support NOV"))
6643       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6644     (gnus-summary-limit-include-thread id)))
6645
6646 (defun gnus-summary-refer-article (message-id &optional arg)
6647   "Fetch an article specified by MESSAGE-ID.
6648 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6649 or `gnus-select-method', no matter what backend the article comes from."
6650   (interactive "sMessage-ID: \nP")
6651   (when (and (stringp message-id)
6652              (not (zerop (length message-id))))
6653     ;; Construct the correct Message-ID if necessary.
6654     ;; Suggested by tale@pawl.rpi.edu.
6655     (unless (string-match "^<" message-id)
6656       (setq message-id (concat "<" message-id)))
6657     (unless (string-match ">$" message-id)
6658       (setq message-id (concat message-id ">")))
6659     (let* ((header (gnus-id-to-header message-id))
6660            (sparse (and header
6661                         (gnus-summary-article-sparse-p
6662                          (mail-header-number header))
6663                         (memq (mail-header-number header)
6664                               gnus-newsgroup-limit))))
6665       (cond
6666        ;; If the article is present in the buffer we just go to it.
6667        ((and header
6668              (or (not (gnus-summary-article-sparse-p
6669                        (mail-header-number header)))
6670                  sparse))
6671         (prog1
6672             (gnus-summary-goto-article
6673              (mail-header-number header) nil t)
6674           (when sparse
6675             (gnus-summary-update-article (mail-header-number header)))))
6676        (t
6677         ;; We fetch the article
6678         (let ((gnus-override-method
6679                (cond ((gnus-news-group-p gnus-newsgroup-name)
6680                       gnus-refer-article-method)
6681                      (arg
6682                       (or gnus-refer-article-method gnus-select-method))
6683                      (t nil)))
6684               number)
6685           ;; Start the special refer-article method, if necessary.
6686           (when (and gnus-refer-article-method
6687                      (gnus-news-group-p gnus-newsgroup-name))
6688             (gnus-check-server gnus-refer-article-method))
6689           ;; Fetch the header, and display the article.
6690           (if (setq number (gnus-summary-insert-subject message-id))
6691               (gnus-summary-select-article nil nil nil number)
6692             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6693
6694 (defun gnus-summary-edit-parameters ()
6695   "Edit the group parameters of the current group."
6696   (interactive)
6697   (gnus-group-edit-group gnus-newsgroup-name 'params))
6698
6699 (defun gnus-summary-customize-parameters ()
6700   "Customize the group parameters of the current group."
6701   (interactive)
6702   (gnus-group-customize gnus-newsgroup-name))
6703
6704 (defun gnus-summary-enter-digest-group (&optional force)
6705   "Enter an nndoc group based on the current article.
6706 If FORCE, force a digest interpretation.  If not, try
6707 to guess what the document format is."
6708   (interactive "P")
6709   (let ((conf gnus-current-window-configuration))
6710     (save-excursion
6711       (gnus-summary-select-article))
6712     (setq gnus-current-window-configuration conf)
6713     (let* ((name (format "%s-%d"
6714                          (gnus-group-prefixed-name
6715                           gnus-newsgroup-name (list 'nndoc ""))
6716                          (save-excursion
6717                            (set-buffer gnus-summary-buffer)
6718                            gnus-current-article)))
6719            (ogroup gnus-newsgroup-name)
6720            (params (append (gnus-info-params (gnus-get-info ogroup))
6721                            (list (cons 'to-group ogroup))
6722                            (list (cons 'save-article-group ogroup))))
6723            (case-fold-search t)
6724            (buf (current-buffer))
6725            dig)
6726       (save-excursion
6727         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6728         (insert-buffer-substring gnus-original-article-buffer)
6729         ;; Remove lines that may lead nndoc to misinterpret the
6730         ;; document type.
6731         (narrow-to-region
6732          (goto-char (point-min))
6733          (or (search-forward "\n\n" nil t) (point)))
6734         (goto-char (point-min))
6735         (delete-matching-lines "^\\(Path\\):\\|^From ")
6736         (widen))
6737       (unwind-protect
6738           (if (gnus-group-read-ephemeral-group
6739                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6740                             (nndoc-article-type
6741                              ,(if force 'digest 'guess))) t)
6742               ;; Make all postings to this group go to the parent group.
6743               (nconc (gnus-info-params (gnus-get-info name))
6744                      params)
6745             ;; Couldn't select this doc group.
6746             (switch-to-buffer buf)
6747             (gnus-set-global-variables)
6748             (gnus-configure-windows 'summary)
6749             (gnus-message 3 "Article couldn't be entered?"))
6750         (kill-buffer dig)))))
6751
6752 (defun gnus-summary-read-document (n)
6753   "Open a new group based on the current article(s).
6754 This will allow you to read digests and other similar
6755 documents as newsgroups.
6756 Obeys the standard process/prefix convention."
6757   (interactive "P")
6758   (let* ((articles (gnus-summary-work-articles n))
6759          (ogroup gnus-newsgroup-name)
6760          (params (append (gnus-info-params (gnus-get-info ogroup))
6761                          (list (cons 'to-group ogroup))))
6762          article group egroup groups vgroup)
6763     (while (setq article (pop articles))
6764       (setq group (format "%s-%d" gnus-newsgroup-name article))
6765       (gnus-summary-remove-process-mark article)
6766       (when (gnus-summary-display-article article)
6767         (save-excursion
6768           (with-temp-buffer
6769             (insert-buffer-substring gnus-original-article-buffer)
6770             ;; Remove some headers that may lead nndoc to make
6771             ;; the wrong guess.
6772             (message-narrow-to-head)
6773             (goto-char (point-min))
6774             (delete-matching-lines "^\\(Path\\):\\|^From ")
6775             (widen)
6776             (if (setq egroup
6777                       (gnus-group-read-ephemeral-group
6778                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6779                                      (nndoc-article-type guess))
6780                        t nil t))
6781                 (progn
6782                   ;; Make all postings to this group go to the parent group.
6783                   (nconc (gnus-info-params (gnus-get-info egroup))
6784                          params)
6785                   (push egroup groups))
6786               ;; Couldn't select this doc group.
6787               (gnus-error 3 "Article couldn't be entered"))))))
6788     ;; Now we have selected all the documents.
6789     (cond
6790      ((not groups)
6791       (error "None of the articles could be interpreted as documents"))
6792      ((gnus-group-read-ephemeral-group
6793        (setq vgroup (format
6794                      "nnvirtual:%s-%s" gnus-newsgroup-name
6795                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6796        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6797        t
6798        (cons (current-buffer) 'summary)))
6799      (t
6800       (error "Couldn't select virtual nndoc group")))))
6801
6802 (defun gnus-summary-isearch-article (&optional regexp-p)
6803   "Do incremental search forward on the current article.
6804 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6805   (interactive "P")
6806   (gnus-summary-select-article)
6807   (gnus-configure-windows 'article)
6808   (gnus-eval-in-buffer-window gnus-article-buffer
6809     (save-restriction
6810       (widen)
6811       (isearch-forward regexp-p))))
6812
6813 (defun gnus-summary-search-article-forward (regexp &optional backward)
6814   "Search for an article containing REGEXP forward.
6815 If BACKWARD, search backward instead."
6816   (interactive
6817    (list (read-string
6818           (format "Search article %s (regexp%s): "
6819                   (if current-prefix-arg "backward" "forward")
6820                   (if gnus-last-search-regexp
6821                       (concat ", default " gnus-last-search-regexp)
6822                     "")))
6823          current-prefix-arg))
6824   (if (string-equal regexp "")
6825       (setq regexp (or gnus-last-search-regexp ""))
6826     (setq gnus-last-search-regexp regexp))
6827   (if (gnus-summary-search-article regexp backward)
6828       (gnus-summary-show-thread)
6829     (error "Search failed: \"%s\"" regexp)))
6830
6831 (defun gnus-summary-search-article-backward (regexp)
6832   "Search for an article containing REGEXP backward."
6833   (interactive
6834    (list (read-string
6835           (format "Search article backward (regexp%s): "
6836                   (if gnus-last-search-regexp
6837                       (concat ", default " gnus-last-search-regexp)
6838                     "")))))
6839   (gnus-summary-search-article-forward regexp 'backward))
6840
6841 (defun gnus-summary-search-article (regexp &optional backward)
6842   "Search for an article containing REGEXP.
6843 Optional argument BACKWARD means do search for backward.
6844 `gnus-select-article-hook' is not called during the search."
6845   ;; We have to require this here to make sure that the following
6846   ;; dynamic binding isn't shadowed by autoloading.
6847   (require 'gnus-async)
6848   (require 'gnus-art)
6849   (let ((gnus-select-article-hook nil)  ;Disable hook.
6850         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6851         (gnus-use-article-prefetch nil)
6852         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6853         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6854         (sum (current-buffer))
6855         (gnus-display-mime-function nil)
6856         (found nil)
6857         point)
6858     (gnus-save-hidden-threads
6859       (gnus-summary-select-article)
6860       (set-buffer gnus-article-buffer)
6861       (when backward
6862         (forward-line -1))
6863       (while (not found)
6864         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6865         (if (if backward
6866                 (re-search-backward regexp nil t)
6867               (re-search-forward regexp nil t))
6868             ;; We found the regexp.
6869             (progn
6870               (setq found 'found)
6871               (beginning-of-line)
6872               (set-window-start
6873                (get-buffer-window (current-buffer))
6874                (point))
6875               (forward-line 1)
6876               (set-buffer sum)
6877               (setq point (point)))
6878           ;; We didn't find it, so we go to the next article.
6879           (set-buffer sum)
6880           (setq found 'not)
6881           (while (eq found 'not)
6882             (if (not (if backward (gnus-summary-find-prev)
6883                        (gnus-summary-find-next)))
6884                 ;; No more articles.
6885                 (setq found t)
6886               ;; Select the next article and adjust point.
6887               (unless (gnus-summary-article-sparse-p
6888                        (gnus-summary-article-number))
6889                 (setq found nil)
6890                 (gnus-summary-select-article)
6891                 (set-buffer gnus-article-buffer)
6892                 (widen)
6893                 (goto-char (if backward (point-max) (point-min))))))))
6894       (gnus-message 7 ""))
6895     ;; Return whether we found the regexp.
6896     (when (eq found 'found)
6897       (goto-char point)
6898       (gnus-summary-show-thread)
6899       (gnus-summary-goto-subject gnus-current-article)
6900       (gnus-summary-position-point)
6901       t)))
6902
6903 (defun gnus-summary-find-matching (header regexp &optional backward unread
6904                                           not-case-fold)
6905   "Return a list of all articles that match REGEXP on HEADER.
6906 The search stars on the current article and goes forwards unless
6907 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6908 If UNREAD is non-nil, only unread articles will
6909 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6910 in the comparisons."
6911   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6912                 (gnus-data-find-list
6913                  (gnus-summary-article-number) (gnus-data-list backward))))
6914         (case-fold-search (not not-case-fold))
6915         articles d func)
6916     (if (consp header)
6917         (if (eq (car header) 'extra)
6918             (setq func
6919                   `(lambda (h)
6920                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6921                          "")))
6922           (error "%s is an invalid header" header))
6923       (unless (fboundp (intern (concat "mail-header-" header)))
6924         (error "%s is not a valid header" header))
6925       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6926     (while data
6927       (setq d (car data))
6928       (and (or (not unread)             ; We want all articles...
6929                (gnus-data-unread-p d))  ; Or just unreads.
6930            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6931            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6932            (push (gnus-data-number d) articles)) ; Success!
6933       (setq data (cdr data)))
6934     (nreverse articles)))
6935
6936 (defun gnus-summary-execute-command (header regexp command &optional backward)
6937   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6938 If HEADER is an empty string (or nil), the match is done on the entire
6939 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6940   (interactive
6941    (list (let ((completion-ignore-case t))
6942            (completing-read
6943             "Header name: "
6944             (mapcar (lambda (string) (list string))
6945                     '("Number" "Subject" "From" "Lines" "Date"
6946                       "Message-ID" "Xref" "References" "Body"))
6947             nil 'require-match))
6948          (read-string "Regexp: ")
6949          (read-key-sequence "Command: ")
6950          current-prefix-arg))
6951   (when (equal header "Body")
6952     (setq header ""))
6953   ;; Hidden thread subtrees must be searched as well.
6954   (gnus-summary-show-all-threads)
6955   ;; We don't want to change current point nor window configuration.
6956   (save-excursion
6957     (save-window-excursion
6958       (gnus-message 6 "Executing %s..." (key-description command))
6959       ;; We'd like to execute COMMAND interactively so as to give arguments.
6960       (gnus-execute header regexp
6961                     `(call-interactively ',(key-binding command))
6962                     backward)
6963       (gnus-message 6 "Executing %s...done" (key-description command)))))
6964
6965 (defun gnus-summary-beginning-of-article ()
6966   "Scroll the article back to the beginning."
6967   (interactive)
6968   (gnus-summary-select-article)
6969   (gnus-configure-windows 'article)
6970   (gnus-eval-in-buffer-window gnus-article-buffer
6971     (widen)
6972     (goto-char (point-min))
6973     (when gnus-page-broken
6974       (gnus-narrow-to-page))))
6975
6976 (defun gnus-summary-end-of-article ()
6977   "Scroll to the end of the article."
6978   (interactive)
6979   (gnus-summary-select-article)
6980   (gnus-configure-windows 'article)
6981   (gnus-eval-in-buffer-window gnus-article-buffer
6982     (widen)
6983     (goto-char (point-max))
6984     (recenter -3)
6985     (when gnus-page-broken
6986       (gnus-narrow-to-page))))
6987
6988 (defun gnus-summary-print-article (&optional filename n)
6989   "Generate and print a PostScript image of the N next (mail) articles.
6990
6991 If N is negative, print the N previous articles.  If N is nil and articles
6992 have been marked with the process mark, print these instead.
6993
6994 If the optional first argument FILENAME is nil, send the image to the
6995 printer.  If FILENAME is a string, save the PostScript image in a file with
6996 that name.  If FILENAME is a number, prompt the user for the name of the file
6997 to save in."
6998   (interactive (list (ps-print-preprint current-prefix-arg)
6999                      current-prefix-arg))
7000   (dolist (article (gnus-summary-work-articles n))
7001     (gnus-summary-select-article nil nil 'pseudo article)
7002     (gnus-eval-in-buffer-window gnus-article-buffer
7003       (let ((buffer (generate-new-buffer " *print*")))
7004         (unwind-protect
7005             (progn
7006               (copy-to-buffer buffer (point-min) (point-max))
7007               (set-buffer buffer)
7008               (gnus-article-delete-invisible-text)
7009               (let ((ps-left-header
7010                      (list
7011                       (concat "("
7012                               (mail-header-subject gnus-current-headers) ")")
7013                       (concat "("
7014                               (mail-header-from gnus-current-headers) ")")))
7015                     (ps-right-header
7016                      (list
7017                       "/pagenumberstring load"
7018                       (concat "("
7019                               (mail-header-date gnus-current-headers) ")"))))
7020                 (gnus-run-hooks 'gnus-ps-print-hook)
7021                 (save-excursion
7022                   (ps-print-buffer-with-faces filename))))
7023           (kill-buffer buffer))))))
7024
7025 (defun gnus-summary-show-article (&optional arg)
7026   "Force re-fetching of the current article.
7027 If ARG (the prefix) is non-nil, show the raw article without any
7028 article massaging functions being run."
7029   (interactive "P")
7030   (if (not arg)
7031       ;; Select the article the normal way.
7032       (gnus-summary-select-article nil 'force)
7033     ;; We have to require this here to make sure that the following
7034     ;; dynamic binding isn't shadowed by autoloading.
7035     (require 'gnus-async)
7036     (require 'gnus-art)
7037     ;; Bind the article treatment functions to nil.
7038     (let ((gnus-have-all-headers t)
7039           gnus-article-prepare-hook
7040           gnus-article-decode-hook
7041           gnus-display-mime-function
7042           gnus-break-pages
7043           gnus-visual)
7044       ;; Destroy any MIME parts.
7045       (when (gnus-buffer-live-p gnus-article-buffer)
7046         (save-excursion
7047           (set-buffer gnus-article-buffer)
7048           (mm-destroy-parts gnus-article-mime-handles)))
7049       (gnus-summary-select-article nil 'force)))
7050   (gnus-summary-goto-subject gnus-current-article)
7051   (gnus-summary-position-point))
7052
7053 (defun gnus-summary-verbose-headers (&optional arg)
7054   "Toggle permanent full header display.
7055 If ARG is a positive number, turn header display on.
7056 If ARG is a negative number, turn header display off."
7057   (interactive "P")
7058   (setq gnus-show-all-headers
7059         (cond ((or (not (numberp arg))
7060                    (zerop arg))
7061                (not gnus-show-all-headers))
7062               ((natnump arg)
7063                t)))
7064   (gnus-summary-show-article))
7065
7066 (defun gnus-summary-toggle-header (&optional arg)
7067   "Show the headers if they are hidden, or hide them if they are shown.
7068 If ARG is a positive number, show the entire header.
7069 If ARG is a negative number, hide the unwanted header lines."
7070   (interactive "P")
7071   (save-excursion
7072     (set-buffer gnus-article-buffer)
7073     (save-restriction
7074       (let* ((buffer-read-only nil)
7075              (inhibit-point-motion-hooks t)
7076              hidden e)
7077         (save-restriction 
7078           (article-narrow-to-head)
7079           (setq hidden (gnus-article-hidden-text-p 'headers)))
7080         (goto-char (point-min))
7081         (when (search-forward "\n\n" nil t)
7082           (delete-region (point-min) (1- (point))))
7083         (goto-char (point-min))
7084         (save-excursion
7085           (set-buffer gnus-original-article-buffer)
7086           (goto-char (point-min))
7087           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7088         (insert-buffer-substring gnus-original-article-buffer 1 e)
7089         (save-restriction
7090           (narrow-to-region (point-min) (point))
7091           (article-decode-encoded-words)
7092           (if (or hidden
7093                   (and (numberp arg) (< arg 0)))
7094               (let ((gnus-treat-hide-headers nil)
7095                     (gnus-treat-hide-boring-headers nil))
7096                 (gnus-treat-article 'head))
7097             (gnus-treat-article 'head)))))))
7098
7099 (defun gnus-summary-show-all-headers ()
7100   "Make all header lines visible."
7101   (interactive)
7102   (gnus-article-show-all-headers))
7103
7104 (defun gnus-summary-caesar-message (&optional arg)
7105   "Caesar rotate the current article by 13.
7106 The numerical prefix specifies how many places to rotate each letter
7107 forward."
7108   (interactive "P")
7109   (gnus-summary-select-article)
7110   (let ((mail-header-separator ""))
7111     (gnus-eval-in-buffer-window gnus-article-buffer
7112       (save-restriction
7113         (widen)
7114         (let ((start (window-start))
7115               buffer-read-only)
7116           (message-caesar-buffer-body arg)
7117           (set-window-start (get-buffer-window (current-buffer)) start))))))
7118
7119 (defun gnus-summary-stop-page-breaking ()
7120   "Stop page breaking in the current article."
7121   (interactive)
7122   (gnus-summary-select-article)
7123   (gnus-eval-in-buffer-window gnus-article-buffer
7124     (widen)
7125     (when (gnus-visual-p 'page-marker)
7126       (let ((buffer-read-only nil))
7127         (gnus-remove-text-with-property 'gnus-prev)
7128         (gnus-remove-text-with-property 'gnus-next))
7129       (setq gnus-page-broken nil))))
7130
7131 (defun gnus-summary-move-article (&optional n to-newsgroup
7132                                             select-method action)
7133   "Move the current article to a different newsgroup.
7134 If N is a positive number, move the N next articles.
7135 If N is a negative number, move the N previous articles.
7136 If N is nil and any articles have been marked with the process mark,
7137 move those articles instead.
7138 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7139 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7140 re-spool using this method.
7141
7142 For this function to work, both the current newsgroup and the
7143 newsgroup that you want to move to have to support the `request-move'
7144 and `request-accept' functions."
7145   (interactive "P")
7146   (unless action
7147     (setq action 'move))
7148   ;; Disable marking as read.
7149   (let (gnus-mark-article-hook)
7150     (save-window-excursion
7151       (gnus-summary-select-article)))
7152   ;; Check whether the source group supports the required functions.
7153   (cond ((and (eq action 'move)
7154               (not (gnus-check-backend-function
7155                     'request-move-article gnus-newsgroup-name)))
7156          (error "The current group does not support article moving"))
7157         ((and (eq action 'crosspost)
7158               (not (gnus-check-backend-function
7159                     'request-replace-article gnus-newsgroup-name)))
7160          (error "The current group does not support article editing")))
7161   (let ((articles (gnus-summary-work-articles n))
7162         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7163         (names '((move "Move" "Moving")
7164                  (copy "Copy" "Copying")
7165                  (crosspost "Crosspost" "Crossposting")))
7166         (copy-buf (save-excursion
7167                     (nnheader-set-temp-buffer " *copy article*")))
7168         art-group to-method new-xref article to-groups)
7169     (unless (assq action names)
7170       (error "Unknown action %s" action))
7171     ;; Read the newsgroup name.
7172     (when (and (not to-newsgroup)
7173                (not select-method))
7174       (setq to-newsgroup
7175             (gnus-read-move-group-name
7176              (cadr (assq action names))
7177              (symbol-value (intern (format "gnus-current-%s-group" action)))
7178              articles prefix))
7179       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7180     (setq to-method (or select-method
7181                         (gnus-group-name-to-method to-newsgroup)))
7182     ;; Check the method we are to move this article to...
7183     (unless (gnus-check-backend-function
7184              'request-accept-article (car to-method))
7185       (error "%s does not support article copying" (car to-method)))
7186     (unless (gnus-check-server to-method)
7187       (error "Can't open server %s" (car to-method)))
7188     (gnus-message 6 "%s to %s: %s..."
7189                   (caddr (assq action names))
7190                   (or (car select-method) to-newsgroup) articles)
7191     (while articles
7192       (setq article (pop articles))
7193       (setq
7194        art-group
7195        (cond
7196         ;; Move the article.
7197         ((eq action 'move)
7198          ;; Remove this article from future suppression.
7199          (gnus-dup-unsuppress-article article)
7200          (gnus-request-move-article
7201           article                       ; Article to move
7202           gnus-newsgroup-name           ; From newsgroup
7203           (nth 1 (gnus-find-method-for-group
7204                   gnus-newsgroup-name)) ; Server
7205           (list 'gnus-request-accept-article
7206                 to-newsgroup (list 'quote select-method)
7207                 (not articles) t)               ; Accept form
7208           (not articles)))              ; Only save nov last time
7209         ;; Copy the article.
7210         ((eq action 'copy)
7211          (save-excursion
7212            (set-buffer copy-buf)
7213            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7214              (gnus-request-accept-article
7215               to-newsgroup select-method (not articles) t))))
7216         ;; Crosspost the article.
7217         ((eq action 'crosspost)
7218          (let ((xref (message-tokenize-header
7219                       (mail-header-xref (gnus-summary-article-header article))
7220                       " ")))
7221            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7222                                   ":" article))
7223            (unless xref
7224              (setq xref (list (system-name))))
7225            (setq new-xref
7226                  (concat
7227                   (mapconcat 'identity
7228                              (delete "Xref:" (delete new-xref xref))
7229                              " ")
7230                   " " new-xref))
7231            (save-excursion
7232              (set-buffer copy-buf)
7233              ;; First put the article in the destination group.
7234              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7235              (when (consp (setq art-group
7236                                 (gnus-request-accept-article
7237                                  to-newsgroup select-method (not articles))))
7238                (setq new-xref (concat new-xref " " (car art-group)
7239                                       ":" (cdr art-group)))
7240                ;; Now we have the new Xrefs header, so we insert
7241                ;; it and replace the new article.
7242                (nnheader-replace-header "Xref" new-xref)
7243                (gnus-request-replace-article
7244                 (cdr art-group) to-newsgroup (current-buffer))
7245                art-group))))))
7246       (cond
7247        ((not art-group)
7248         (gnus-message 1 "Couldn't %s article %s"
7249                       (cadr (assq action names)) article))
7250        ((and (eq art-group 'junk)
7251              (eq action 'move))
7252         (gnus-summary-mark-article article gnus-canceled-mark)
7253         (gnus-message 4 "Deleted article %s" article))
7254        (t
7255         (let* ((pto-group (gnus-group-prefixed-name
7256                            (car art-group) to-method))
7257                (entry
7258                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7259                (info (nth 2 entry))
7260                (to-group (gnus-info-group info)))
7261           ;; Update the group that has been moved to.
7262           (when (and info
7263                      (memq action '(move copy)))
7264             (unless (member to-group to-groups)
7265               (push to-group to-groups))
7266
7267             (unless (memq article gnus-newsgroup-unreads)
7268               (gnus-info-set-read
7269                info (gnus-add-to-range (gnus-info-read info)
7270                                        (list (cdr art-group)))))
7271
7272             ;; Copy any marks over to the new group.
7273             (let ((marks gnus-article-mark-lists)
7274                   (to-article (cdr art-group)))
7275
7276               ;; See whether the article is to be put in the cache.
7277               (when gnus-use-cache
7278                 (gnus-cache-possibly-enter-article
7279                  to-group to-article
7280                  (memq article gnus-newsgroup-marked)
7281                  (memq article gnus-newsgroup-dormant)
7282                  (memq article gnus-newsgroup-unreads)))
7283
7284               (when (and (equal to-group gnus-newsgroup-name)
7285                          (not (memq article gnus-newsgroup-unreads)))
7286                 ;; Mark this article as read in this group.
7287                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7288                 (setcdr (gnus-active to-group) to-article)
7289                 (setcdr gnus-newsgroup-active to-article))
7290
7291               (while marks
7292                 (when (memq article (symbol-value
7293                                      (intern (format "gnus-newsgroup-%s"
7294                                                      (caar marks)))))
7295                   ;; If the other group is the same as this group,
7296                   ;; then we have to add the mark to the list.
7297                   (when (equal to-group gnus-newsgroup-name)
7298                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7299                          (cons to-article
7300                                (symbol-value
7301                                 (intern (format "gnus-newsgroup-%s"
7302                                                 (caar marks)))))))
7303                   ;; Copy the marks to other group.
7304                   (gnus-add-marked-articles
7305                    to-group (cdar marks) (list to-article) info))
7306                 (setq marks (cdr marks)))
7307
7308               (gnus-dribble-enter
7309                (concat "(gnus-group-set-info '"
7310                        (gnus-prin1-to-string (gnus-get-info to-group))
7311                        ")"))))
7312
7313           ;; Update the Xref header in this article to point to
7314           ;; the new crossposted article we have just created.
7315           (when (eq action 'crosspost)
7316             (save-excursion
7317               (set-buffer copy-buf)
7318               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7319               (nnheader-replace-header "Xref" new-xref)
7320               (gnus-request-replace-article
7321                article gnus-newsgroup-name (current-buffer)))))
7322
7323         ;;;!!!Why is this necessary?
7324         (set-buffer gnus-summary-buffer)
7325
7326         (gnus-summary-goto-subject article)
7327         (when (eq action 'move)
7328           (gnus-summary-mark-article article gnus-canceled-mark))))
7329       (gnus-summary-remove-process-mark article))
7330     ;; Re-activate all groups that have been moved to.
7331     (while to-groups
7332       (save-excursion
7333         (set-buffer gnus-group-buffer)
7334         (when (gnus-group-goto-group (car to-groups) t)
7335           (gnus-group-get-new-news-this-group 1 t))
7336         (pop to-groups)))
7337
7338     (gnus-kill-buffer copy-buf)
7339     (gnus-summary-position-point)
7340     (gnus-set-mode-line 'summary)))
7341
7342 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7343   "Move the current article to a different newsgroup.
7344 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7345 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7346 re-spool using this method."
7347   (interactive "P")
7348   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7349
7350 (defun gnus-summary-crosspost-article (&optional n)
7351   "Crosspost the current article to some other group."
7352   (interactive "P")
7353   (gnus-summary-move-article n nil nil 'crosspost))
7354
7355 (defcustom gnus-summary-respool-default-method nil
7356   "Default method for respooling an article.
7357 If nil, use to the current newsgroup method."
7358   :type '(choice (gnus-select-method :value (nnml ""))
7359                  (const nil))
7360   :group 'gnus-summary-mail)
7361
7362 (defun gnus-summary-respool-article (&optional n method)
7363   "Respool the current article.
7364 The article will be squeezed through the mail spooling process again,
7365 which means that it will be put in some mail newsgroup or other
7366 depending on `nnmail-split-methods'.
7367 If N is a positive number, respool the N next articles.
7368 If N is a negative number, respool the N previous articles.
7369 If N is nil and any articles have been marked with the process mark,
7370 respool those articles instead.
7371
7372 Respooling can be done both from mail groups and \"real\" newsgroups.
7373 In the former case, the articles in question will be moved from the
7374 current group into whatever groups they are destined to.  In the
7375 latter case, they will be copied into the relevant groups."
7376   (interactive
7377    (list current-prefix-arg
7378          (let* ((methods (gnus-methods-using 'respool))
7379                 (methname
7380                  (symbol-name (or gnus-summary-respool-default-method
7381                                   (car (gnus-find-method-for-group
7382                                         gnus-newsgroup-name)))))
7383                 (method
7384                  (gnus-completing-read
7385                   methname "What backend do you want to use when respooling?"
7386                   methods nil t nil 'gnus-mail-method-history))
7387                 ms)
7388            (cond
7389             ((zerop (length (setq ms (gnus-servers-using-backend
7390                                       (intern method)))))
7391              (list (intern method) ""))
7392             ((= 1 (length ms))
7393              (car ms))
7394             (t
7395              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7396                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7397                            ms-alist))))))))
7398   (unless method
7399     (error "No method given for respooling"))
7400   (if (assoc (symbol-name
7401               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7402              (gnus-methods-using 'respool))
7403       (gnus-summary-move-article n nil method)
7404     (gnus-summary-copy-article n nil method)))
7405
7406 (defun gnus-summary-import-article (file)
7407   "Import an arbitrary file into a mail newsgroup."
7408   (interactive "fImport file: ")
7409   (let ((group gnus-newsgroup-name)
7410         (now (current-time))
7411         atts lines)
7412     (unless (gnus-check-backend-function 'request-accept-article group)
7413       (error "%s does not support article importing" group))
7414     (or (file-readable-p file)
7415         (not (file-regular-p file))
7416         (error "Can't read %s" file))
7417     (save-excursion
7418       (set-buffer (gnus-get-buffer-create " *import file*"))
7419       (erase-buffer)
7420       (insert-file-contents file)
7421       (goto-char (point-min))
7422       (unless (nnheader-article-p)
7423         ;; This doesn't look like an article, so we fudge some headers.
7424         (setq atts (file-attributes file)
7425               lines (count-lines (point-min) (point-max)))
7426         (insert "From: " (read-string "From: ") "\n"
7427                 "Subject: " (read-string "Subject: ") "\n"
7428                 "Date: " (message-make-date (nth 5 atts))
7429                 "\n"
7430                 "Message-ID: " (message-make-message-id) "\n"
7431                 "Lines: " (int-to-string lines) "\n"
7432                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7433       (gnus-request-accept-article group nil t)
7434       (kill-buffer (current-buffer)))))
7435
7436 (defun gnus-summary-article-posted-p ()
7437   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7438 This will be the case if the article has both been mailed and posted."
7439   (interactive)
7440   (let ((id (mail-header-references (gnus-summary-article-header)))
7441         (gnus-override-method
7442          (or gnus-refer-article-method gnus-select-method)))
7443     (if (gnus-request-head id "")
7444         (gnus-message 2 "The current message was found on %s"
7445                       gnus-override-method)
7446       (gnus-message 2 "The current message couldn't be found on %s"
7447                     gnus-override-method)
7448       nil)))
7449
7450 (defun gnus-summary-expire-articles (&optional now)
7451   "Expire all articles that are marked as expirable in the current group."
7452   (interactive)
7453   (when (gnus-check-backend-function
7454          'request-expire-articles gnus-newsgroup-name)
7455     ;; This backend supports expiry.
7456     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7457            (expirable (if total
7458                           (progn
7459                             ;; We need to update the info for
7460                             ;; this group for `gnus-list-of-read-articles'
7461                             ;; to give us the right answer.
7462                             (gnus-run-hooks 'gnus-exit-group-hook)
7463                             (gnus-summary-update-info)
7464                             (gnus-list-of-read-articles gnus-newsgroup-name))
7465                         (setq gnus-newsgroup-expirable
7466                               (sort gnus-newsgroup-expirable '<))))
7467            (expiry-wait (if now 'immediate
7468                           (gnus-group-find-parameter
7469                            gnus-newsgroup-name 'expiry-wait)))
7470            es)
7471       (when expirable
7472         ;; There are expirable articles in this group, so we run them
7473         ;; through the expiry process.
7474         (gnus-message 6 "Expiring articles...")
7475         (unless (gnus-check-group gnus-newsgroup-name)
7476           (error "Can't open server for %s" gnus-newsgroup-name))
7477         ;; The list of articles that weren't expired is returned.
7478         (save-excursion
7479           (if expiry-wait
7480               (let ((nnmail-expiry-wait-function nil)
7481                     (nnmail-expiry-wait expiry-wait))
7482                 (setq es (gnus-request-expire-articles
7483                           expirable gnus-newsgroup-name)))
7484             (setq es (gnus-request-expire-articles
7485                       expirable gnus-newsgroup-name))))
7486         (unless total
7487           (setq gnus-newsgroup-expirable es))
7488         ;; We go through the old list of expirable, and mark all
7489         ;; really expired articles as nonexistent.
7490         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7491           (let ((gnus-use-cache nil))
7492             (while expirable
7493               (unless (memq (car expirable) es)
7494                 (when (gnus-data-find (car expirable))
7495                   (gnus-summary-mark-article
7496                    (car expirable) gnus-canceled-mark)))
7497               (setq expirable (cdr expirable)))))
7498         (gnus-message 6 "Expiring articles...done")))))
7499
7500 (defun gnus-summary-expire-articles-now ()
7501   "Expunge all expirable articles in the current group.
7502 This means that *all* articles that are marked as expirable will be
7503 deleted forever, right now."
7504   (interactive)
7505   (or gnus-expert-user
7506       (gnus-yes-or-no-p
7507        "Are you really, really, really sure you want to delete all these messages? ")
7508       (error "Phew!"))
7509   (gnus-summary-expire-articles t))
7510
7511 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7512 (defun gnus-summary-delete-article (&optional n)
7513   "Delete the N next (mail) articles.
7514 This command actually deletes articles.  This is not a marking
7515 command.  The article will disappear forever from your life, never to
7516 return.
7517 If N is negative, delete backwards.
7518 If N is nil and articles have been marked with the process mark,
7519 delete these instead."
7520   (interactive "P")
7521   (unless (gnus-check-backend-function 'request-expire-articles
7522                                        gnus-newsgroup-name)
7523     (error "The current newsgroup does not support article deletion"))
7524   ;; Compute the list of articles to delete.
7525   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7526         not-deleted)
7527     (if (and gnus-novice-user
7528              (not (gnus-yes-or-no-p
7529                    (format "Do you really want to delete %s forever? "
7530                            (if (> (length articles) 1)
7531                                (format "these %s articles" (length articles))
7532                              "this article")))))
7533         ()
7534       ;; Delete the articles.
7535       (setq not-deleted (gnus-request-expire-articles
7536                          articles gnus-newsgroup-name 'force))
7537       (while articles
7538         (gnus-summary-remove-process-mark (car articles))
7539         ;; The backend might not have been able to delete the article
7540         ;; after all.
7541         (unless (memq (car articles) not-deleted)
7542           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7543         (setq articles (cdr articles)))
7544       (when not-deleted
7545         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7546     (gnus-summary-position-point)
7547     (gnus-set-mode-line 'summary)
7548     not-deleted))
7549
7550 (defun gnus-summary-edit-article (&optional force)
7551   "Edit the current article.
7552 This will have permanent effect only in mail groups.
7553 If FORCE is non-nil, allow editing of articles even in read-only
7554 groups."
7555   (interactive "P")
7556   (let ((mail-parse-charset gnus-newsgroup-charset))
7557     (save-excursion
7558       (set-buffer gnus-summary-buffer)
7559       (gnus-set-global-variables)
7560       (when (and (not force)
7561                  (gnus-group-read-only-p))
7562         (error "The current newsgroup does not support article editing"))
7563       (gnus-summary-show-article t)
7564       (gnus-article-edit-article
7565        'mime-to-mml
7566        `(lambda (no-highlight)
7567           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7568             (mml-to-mime)
7569             (gnus-summary-edit-article-done
7570              ,(or (mail-header-references gnus-current-headers) "")
7571              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7572
7573 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7574
7575 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7576                                                  no-highlight)
7577   "Make edits to the current article permanent."
7578   (interactive)
7579   ;; Replace the article.
7580   (let ((buf (current-buffer)))
7581     (with-temp-buffer
7582       (insert-buffer-substring buf)
7583       (if (and (not read-only)
7584                (not (gnus-request-replace-article
7585                      (cdr gnus-article-current) (car gnus-article-current)
7586                      (current-buffer) t)))
7587           (error "Couldn't replace article")
7588         ;; Update the summary buffer.
7589         (if (and references
7590                  (equal (message-tokenize-header references " ")
7591                         (message-tokenize-header
7592                          (or (message-fetch-field "references") "") " ")))
7593             ;; We only have to update this line.
7594             (save-excursion
7595               (save-restriction
7596                 (message-narrow-to-head)
7597                 (let ((head (buffer-string))
7598                       header)
7599                   (with-temp-buffer
7600                     (insert (format "211 %d Article retrieved.\n"
7601                                     (cdr gnus-article-current)))
7602                     (insert head)
7603                     (insert ".\n")
7604                     (let ((nntp-server-buffer (current-buffer)))
7605                       (setq header (car (gnus-get-newsgroup-headers
7606                                          (save-excursion
7607                                            (set-buffer gnus-summary-buffer)
7608                                            gnus-newsgroup-dependencies)
7609                                          t))))
7610                     (save-excursion
7611                       (set-buffer gnus-summary-buffer)
7612                       (gnus-data-set-header
7613                        (gnus-data-find (cdr gnus-article-current))
7614                        header)
7615                       (gnus-summary-update-article-line
7616                        (cdr gnus-article-current) header))))))
7617           ;; Update threads.
7618           (set-buffer (or buffer gnus-summary-buffer))
7619           (gnus-summary-update-article (cdr gnus-article-current)))
7620         ;; Prettify the article buffer again.
7621         (unless no-highlight
7622           (save-excursion
7623             (set-buffer gnus-article-buffer)
7624             ;;;!!! Fix this -- article should be rehighlighted.
7625             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7626             (set-buffer gnus-original-article-buffer)
7627             (gnus-request-article
7628              (cdr gnus-article-current)
7629              (car gnus-article-current) (current-buffer))))
7630         ;; Prettify the summary buffer line.
7631         (when (gnus-visual-p 'summary-highlight 'highlight)
7632           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7633
7634 (defun gnus-summary-edit-wash (key)
7635   "Perform editing command KEY in the article buffer."
7636   (interactive
7637    (list
7638     (progn
7639       (message "%s" (concat (this-command-keys) "- "))
7640       (read-char))))
7641   (message "")
7642   (gnus-summary-edit-article)
7643   (execute-kbd-macro (concat (this-command-keys) key))
7644   (gnus-article-edit-done))
7645
7646 ;;; Respooling
7647
7648 (defun gnus-summary-respool-query (&optional silent trace)
7649   "Query where the respool algorithm would put this article."
7650   (interactive)
7651   (let (gnus-mark-article-hook)
7652     (gnus-summary-select-article)
7653     (save-excursion
7654       (set-buffer gnus-original-article-buffer)
7655       (save-restriction
7656         (message-narrow-to-head)
7657         (let ((groups (nnmail-article-group 'identity trace)))
7658           (unless silent
7659             (if groups
7660                 (message "This message would go to %s"
7661                          (mapconcat 'car groups ", "))
7662               (message "This message would go to no groups"))
7663             groups))))))
7664
7665 (defun gnus-summary-respool-trace ()
7666   "Trace where the respool algorithm would put this article.
7667 Display a buffer showing all fancy splitting patterns which matched."
7668   (interactive)
7669   (gnus-summary-respool-query nil t))
7670
7671 ;; Summary marking commands.
7672
7673 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7674   "Mark articles which has the same subject as read, and then select the next.
7675 If UNMARK is positive, remove any kind of mark.
7676 If UNMARK is negative, tick articles."
7677   (interactive "P")
7678   (when unmark
7679     (setq unmark (prefix-numeric-value unmark)))
7680   (let ((count
7681          (gnus-summary-mark-same-subject
7682           (gnus-summary-article-subject) unmark)))
7683     ;; Select next unread article.  If auto-select-same mode, should
7684     ;; select the first unread article.
7685     (gnus-summary-next-article t (and gnus-auto-select-same
7686                                       (gnus-summary-article-subject)))
7687     (gnus-message 7 "%d article%s marked as %s"
7688                   count (if (= count 1) " is" "s are")
7689                   (if unmark "unread" "read"))))
7690
7691 (defun gnus-summary-kill-same-subject (&optional unmark)
7692   "Mark articles which has the same subject as read.
7693 If UNMARK is positive, remove any kind of mark.
7694 If UNMARK is negative, tick articles."
7695   (interactive "P")
7696   (when unmark
7697     (setq unmark (prefix-numeric-value unmark)))
7698   (let ((count
7699          (gnus-summary-mark-same-subject
7700           (gnus-summary-article-subject) unmark)))
7701     ;; If marked as read, go to next unread subject.
7702     (when (null unmark)
7703       ;; Go to next unread subject.
7704       (gnus-summary-next-subject 1 t))
7705     (gnus-message 7 "%d articles are marked as %s"
7706                   count (if unmark "unread" "read"))))
7707
7708 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7709   "Mark articles with same SUBJECT as read, and return marked number.
7710 If optional argument UNMARK is positive, remove any kinds of marks.
7711 If optional argument UNMARK is negative, mark articles as unread instead."
7712   (let ((count 1))
7713     (save-excursion
7714       (cond
7715        ((null unmark)                   ; Mark as read.
7716         (while (and
7717                 (progn
7718                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7719                   (gnus-summary-show-thread) t)
7720                 (gnus-summary-find-subject subject))
7721           (setq count (1+ count))))
7722        ((> unmark 0)                    ; Tick.
7723         (while (and
7724                 (progn
7725                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7726                   (gnus-summary-show-thread) t)
7727                 (gnus-summary-find-subject subject))
7728           (setq count (1+ count))))
7729        (t                               ; Mark as unread.
7730         (while (and
7731                 (progn
7732                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7733                   (gnus-summary-show-thread) t)
7734                 (gnus-summary-find-subject subject))
7735           (setq count (1+ count)))))
7736       (gnus-set-mode-line 'summary)
7737       ;; Return the number of marked articles.
7738       count)))
7739
7740 (defun gnus-summary-mark-as-processable (n &optional unmark)
7741   "Set the process mark on the next N articles.
7742 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7743 the process mark instead.  The difference between N and the actual
7744 number of articles marked is returned."
7745   (interactive "p")
7746   (let ((backward (< n 0))
7747         (n (abs n)))
7748     (while (and
7749             (> n 0)
7750             (if unmark
7751                 (gnus-summary-remove-process-mark
7752                  (gnus-summary-article-number))
7753               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7754             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7755       (setq n (1- n)))
7756     (when (/= 0 n)
7757       (gnus-message 7 "No more articles"))
7758     (gnus-summary-recenter)
7759     (gnus-summary-position-point)
7760     n))
7761
7762 (defun gnus-summary-unmark-as-processable (n)
7763   "Remove the process mark from the next N articles.
7764 If N is negative, unmark backward instead.  The difference between N and
7765 the actual number of articles unmarked is returned."
7766   (interactive "p")
7767   (gnus-summary-mark-as-processable n t))
7768
7769 (defun gnus-summary-unmark-all-processable ()
7770   "Remove the process mark from all articles."
7771   (interactive)
7772   (save-excursion
7773     (while gnus-newsgroup-processable
7774       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7775   (gnus-summary-position-point))
7776
7777 (defun gnus-summary-mark-as-expirable (n)
7778   "Mark N articles forward as expirable.
7779 If N is negative, mark backward instead.  The difference between N and
7780 the actual number of articles marked is returned."
7781   (interactive "p")
7782   (gnus-summary-mark-forward n gnus-expirable-mark))
7783
7784 (defun gnus-summary-mark-article-as-replied (article)
7785   "Mark ARTICLE replied and update the summary line."
7786   (push article gnus-newsgroup-replied)
7787   (let ((buffer-read-only nil))
7788     (when (gnus-summary-goto-subject article nil t)
7789       (gnus-summary-update-secondary-mark article))))
7790
7791 (defun gnus-summary-set-bookmark (article)
7792   "Set a bookmark in current article."
7793   (interactive (list (gnus-summary-article-number)))
7794   (when (or (not (get-buffer gnus-article-buffer))
7795             (not gnus-current-article)
7796             (not gnus-article-current)
7797             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7798     (error "No current article selected"))
7799   ;; Remove old bookmark, if one exists.
7800   (let ((old (assq article gnus-newsgroup-bookmarks)))
7801     (when old
7802       (setq gnus-newsgroup-bookmarks
7803             (delq old gnus-newsgroup-bookmarks))))
7804   ;; Set the new bookmark, which is on the form
7805   ;; (article-number . line-number-in-body).
7806   (push
7807    (cons article
7808          (save-excursion
7809            (set-buffer gnus-article-buffer)
7810            (count-lines
7811             (min (point)
7812                  (save-excursion
7813                    (goto-char (point-min))
7814                    (search-forward "\n\n" nil t)
7815                    (point)))
7816             (point))))
7817    gnus-newsgroup-bookmarks)
7818   (gnus-message 6 "A bookmark has been added to the current article."))
7819
7820 (defun gnus-summary-remove-bookmark (article)
7821   "Remove the bookmark from the current article."
7822   (interactive (list (gnus-summary-article-number)))
7823   ;; Remove old bookmark, if one exists.
7824   (let ((old (assq article gnus-newsgroup-bookmarks)))
7825     (if old
7826         (progn
7827           (setq gnus-newsgroup-bookmarks
7828                 (delq old gnus-newsgroup-bookmarks))
7829           (gnus-message 6 "Removed bookmark."))
7830       (gnus-message 6 "No bookmark in current article."))))
7831
7832 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7833 (defun gnus-summary-mark-as-dormant (n)
7834   "Mark N articles forward as dormant.
7835 If N is negative, mark backward instead.  The difference between N and
7836 the actual number of articles marked is returned."
7837   (interactive "p")
7838   (gnus-summary-mark-forward n gnus-dormant-mark))
7839
7840 (defun gnus-summary-set-process-mark (article)
7841   "Set the process mark on ARTICLE and update the summary line."
7842   (setq gnus-newsgroup-processable
7843         (cons article
7844               (delq article gnus-newsgroup-processable)))
7845   (when (gnus-summary-goto-subject article)
7846     (gnus-summary-show-thread)
7847     (gnus-summary-goto-subject article)
7848     (gnus-summary-update-secondary-mark article)))
7849
7850 (defun gnus-summary-remove-process-mark (article)
7851   "Remove the process mark from ARTICLE and update the summary line."
7852   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7853   (when (gnus-summary-goto-subject article)
7854     (gnus-summary-show-thread)
7855     (gnus-summary-goto-subject article)
7856     (gnus-summary-update-secondary-mark article)))
7857
7858 (defun gnus-summary-set-saved-mark (article)
7859   "Set the process mark on ARTICLE and update the summary line."
7860   (push article gnus-newsgroup-saved)
7861   (when (gnus-summary-goto-subject article)
7862     (gnus-summary-update-secondary-mark article)))
7863
7864 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7865   "Mark N articles as read forwards.
7866 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7867 The difference between N and the actual number of articles marked is
7868 returned."
7869   (interactive "p")
7870   (gnus-summary-show-thread)
7871   (let ((backward (< n 0))
7872         (gnus-summary-goto-unread
7873          (and gnus-summary-goto-unread
7874               (not (eq gnus-summary-goto-unread 'never))
7875               (not (memq mark (list gnus-unread-mark
7876                                     gnus-ticked-mark gnus-dormant-mark)))))
7877         (n (abs n))
7878         (mark (or mark gnus-del-mark)))
7879     (while (and (> n 0)
7880                 (gnus-summary-mark-article nil mark no-expire)
7881                 (zerop (gnus-summary-next-subject
7882                         (if backward -1 1)
7883                         (and gnus-summary-goto-unread
7884                              (not (eq gnus-summary-goto-unread 'never)))
7885                         t)))
7886       (setq n (1- n)))
7887     (when (/= 0 n)
7888       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7889     (gnus-summary-recenter)
7890     (gnus-summary-position-point)
7891     (gnus-set-mode-line 'summary)
7892     n))
7893
7894 (defun gnus-summary-mark-article-as-read (mark)
7895   "Mark the current article quickly as read with MARK."
7896   (let ((article (gnus-summary-article-number)))
7897     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7898     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7899     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7900     (push (cons article mark) gnus-newsgroup-reads)
7901     ;; Possibly remove from cache, if that is used.
7902     (when gnus-use-cache
7903       (gnus-cache-enter-remove-article article))
7904     ;; Allow the backend to change the mark.
7905     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7906     ;; Check for auto-expiry.
7907     (when (and gnus-newsgroup-auto-expire
7908                (memq mark gnus-auto-expirable-marks))
7909       (setq mark gnus-expirable-mark)
7910       ;; Let the backend know about the mark change.
7911       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7912       (push article gnus-newsgroup-expirable))
7913     ;; Set the mark in the buffer.
7914     (gnus-summary-update-mark mark 'unread)
7915     t))
7916
7917 (defun gnus-summary-mark-article-as-unread (mark)
7918   "Mark the current article quickly as unread with MARK."
7919   (let* ((article (gnus-summary-article-number))
7920          (old-mark (gnus-summary-article-mark article)))
7921     ;; Allow the backend to change the mark.
7922     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7923     (if (eq mark old-mark)
7924         t
7925       (if (<= article 0)
7926           (progn
7927             (gnus-error 1 "Can't mark negative article numbers")
7928             nil)
7929         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7930         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7931         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7932         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7933         (cond ((= mark gnus-ticked-mark)
7934                (push article gnus-newsgroup-marked))
7935               ((= mark gnus-dormant-mark)
7936                (push article gnus-newsgroup-dormant))
7937               (t
7938                (push article gnus-newsgroup-unreads)))
7939         (gnus-pull article gnus-newsgroup-reads)
7940
7941         ;; See whether the article is to be put in the cache.
7942         (and gnus-use-cache
7943              (vectorp (gnus-summary-article-header article))
7944              (save-excursion
7945                (gnus-cache-possibly-enter-article
7946                 gnus-newsgroup-name article
7947                 (= mark gnus-ticked-mark)
7948                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7949
7950         ;; Fix the mark.
7951         (gnus-summary-update-mark mark 'unread)
7952         t))))
7953
7954 (defun gnus-summary-mark-article (&optional article mark no-expire)
7955   "Mark ARTICLE with MARK.  MARK can be any character.
7956 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7957 `??' (dormant) and `?E' (expirable).
7958 If MARK is nil, then the default character `?r' is used.
7959 If ARTICLE is nil, then the article on the current line will be
7960 marked."
7961   ;; The mark might be a string.
7962   (when (stringp mark)
7963     (setq mark (aref mark 0)))
7964   ;; If no mark is given, then we check auto-expiring.
7965   (when (null mark)
7966     (setq mark gnus-del-mark))
7967   (when (and (not no-expire)
7968              gnus-newsgroup-auto-expire
7969              (memq mark gnus-auto-expirable-marks))
7970     (setq mark gnus-expirable-mark))
7971   (let ((article (or article (gnus-summary-article-number)))
7972         (old-mark (gnus-summary-article-mark article)))
7973     ;; Allow the backend to change the mark.
7974     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7975     (if (eq mark old-mark)
7976         t
7977       (unless article
7978         (error "No article on current line"))
7979       (if (not (if (or (= mark gnus-unread-mark)
7980                        (= mark gnus-ticked-mark)
7981                        (= mark gnus-dormant-mark))
7982                    (gnus-mark-article-as-unread article mark)
7983                  (gnus-mark-article-as-read article mark)))
7984           t
7985         ;; See whether the article is to be put in the cache.
7986         (and gnus-use-cache
7987              (not (= mark gnus-canceled-mark))
7988              (vectorp (gnus-summary-article-header article))
7989              (save-excursion
7990                (gnus-cache-possibly-enter-article
7991                 gnus-newsgroup-name article
7992                 (= mark gnus-ticked-mark)
7993                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7994
7995         (when (gnus-summary-goto-subject article nil t)
7996           (let ((buffer-read-only nil))
7997             (gnus-summary-show-thread)
7998             ;; Fix the mark.
7999             (gnus-summary-update-mark mark 'unread)
8000             t))))))
8001
8002 (defun gnus-summary-update-secondary-mark (article)
8003   "Update the secondary (read, process, cache) mark."
8004   (gnus-summary-update-mark
8005    (cond ((memq article gnus-newsgroup-processable)
8006           gnus-process-mark)
8007          ((memq article gnus-newsgroup-cached)
8008           gnus-cached-mark)
8009          ((memq article gnus-newsgroup-replied)
8010           gnus-replied-mark)
8011          ((memq article gnus-newsgroup-saved)
8012           gnus-saved-mark)
8013          (t gnus-unread-mark))
8014    'replied)
8015   (when (gnus-visual-p 'summary-highlight 'highlight)
8016     (gnus-run-hooks 'gnus-summary-update-hook))
8017   t)
8018
8019 (defun gnus-summary-update-mark (mark type)
8020   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8021         (buffer-read-only nil))
8022     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8023     (when forward
8024       (when (looking-at "\r")
8025         (incf forward))
8026       (when (<= (+ forward (point)) (point-max))
8027         ;; Go to the right position on the line.
8028         (goto-char (+ forward (point)))
8029         ;; Replace the old mark with the new mark.
8030         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8031         ;; Optionally update the marks by some user rule.
8032         (when (eq type 'unread)
8033           (gnus-data-set-mark
8034            (gnus-data-find (gnus-summary-article-number)) mark)
8035           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8036
8037 (defun gnus-mark-article-as-read (article &optional mark)
8038   "Enter ARTICLE in the pertinent lists and remove it from others."
8039   ;; Make the article expirable.
8040   (let ((mark (or mark gnus-del-mark)))
8041     (if (= mark gnus-expirable-mark)
8042         (push article gnus-newsgroup-expirable)
8043       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8044     ;; Remove from unread and marked lists.
8045     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8046     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8047     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8048     (push (cons article mark) gnus-newsgroup-reads)
8049     ;; Possibly remove from cache, if that is used.
8050     (when gnus-use-cache
8051       (gnus-cache-enter-remove-article article))
8052     t))
8053
8054 (defun gnus-mark-article-as-unread (article &optional mark)
8055   "Enter ARTICLE in the pertinent lists and remove it from others."
8056   (let ((mark (or mark gnus-ticked-mark)))
8057     (if (<= article 0)
8058         (progn
8059           (gnus-error 1 "Can't mark negative article numbers")
8060           nil)
8061       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8062             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8063             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8064             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8065
8066       ;; Unsuppress duplicates?
8067       (when gnus-suppress-duplicates
8068         (gnus-dup-unsuppress-article article))
8069
8070       (cond ((= mark gnus-ticked-mark)
8071              (push article gnus-newsgroup-marked))
8072             ((= mark gnus-dormant-mark)
8073              (push article gnus-newsgroup-dormant))
8074             (t
8075              (push article gnus-newsgroup-unreads)))
8076       (gnus-pull article gnus-newsgroup-reads)
8077       t)))
8078
8079 (defalias 'gnus-summary-mark-as-unread-forward
8080   'gnus-summary-tick-article-forward)
8081 (make-obsolete 'gnus-summary-mark-as-unread-forward
8082                'gnus-summary-tick-article-forward)
8083 (defun gnus-summary-tick-article-forward (n)
8084   "Tick N articles forwards.
8085 If N is negative, tick backwards instead.
8086 The difference between N and the number of articles ticked is returned."
8087   (interactive "p")
8088   (gnus-summary-mark-forward n gnus-ticked-mark))
8089
8090 (defalias 'gnus-summary-mark-as-unread-backward
8091   'gnus-summary-tick-article-backward)
8092 (make-obsolete 'gnus-summary-mark-as-unread-backward
8093                'gnus-summary-tick-article-backward)
8094 (defun gnus-summary-tick-article-backward (n)
8095   "Tick N articles backwards.
8096 The difference between N and the number of articles ticked is returned."
8097   (interactive "p")
8098   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8099
8100 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8101 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8102 (defun gnus-summary-tick-article (&optional article clear-mark)
8103   "Mark current article as unread.
8104 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8105 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8106   (interactive)
8107   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8108                                        gnus-ticked-mark)))
8109
8110 (defun gnus-summary-mark-as-read-forward (n)
8111   "Mark N articles as read forwards.
8112 If N is negative, mark backwards instead.
8113 The difference between N and the actual number of articles marked is
8114 returned."
8115   (interactive "p")
8116   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8117
8118 (defun gnus-summary-mark-as-read-backward (n)
8119   "Mark the N articles as read backwards.
8120 The difference between N and the actual number of articles marked is
8121 returned."
8122   (interactive "p")
8123   (gnus-summary-mark-forward
8124    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8125
8126 (defun gnus-summary-mark-as-read (&optional article mark)
8127   "Mark current article as read.
8128 ARTICLE specifies the article to be marked as read.
8129 MARK specifies a string to be inserted at the beginning of the line."
8130   (gnus-summary-mark-article article mark))
8131
8132 (defun gnus-summary-clear-mark-forward (n)
8133   "Clear marks from N articles forward.
8134 If N is negative, clear backward instead.
8135 The difference between N and the number of marks cleared is returned."
8136   (interactive "p")
8137   (gnus-summary-mark-forward n gnus-unread-mark))
8138
8139 (defun gnus-summary-clear-mark-backward (n)
8140   "Clear marks from N articles backward.
8141 The difference between N and the number of marks cleared is returned."
8142   (interactive "p")
8143   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8144
8145 (defun gnus-summary-mark-unread-as-read ()
8146   "Intended to be used by `gnus-summary-mark-article-hook'."
8147   (when (memq gnus-current-article gnus-newsgroup-unreads)
8148     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8149
8150 (defun gnus-summary-mark-read-and-unread-as-read ()
8151   "Intended to be used by `gnus-summary-mark-article-hook'."
8152   (let ((mark (gnus-summary-article-mark)))
8153     (when (or (gnus-unread-mark-p mark)
8154               (gnus-read-mark-p mark))
8155       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8156
8157 (defun gnus-summary-mark-region-as-read (point mark all)
8158   "Mark all unread articles between point and mark as read.
8159 If given a prefix, mark all articles between point and mark as read,
8160 even ticked and dormant ones."
8161   (interactive "r\nP")
8162   (save-excursion
8163     (let (article)
8164       (goto-char point)
8165       (beginning-of-line)
8166       (while (and
8167               (< (point) mark)
8168               (progn
8169                 (when (or all
8170                           (memq (setq article (gnus-summary-article-number))
8171                                 gnus-newsgroup-unreads))
8172                   (gnus-summary-mark-article article gnus-del-mark))
8173                 t)
8174               (gnus-summary-find-next))))))
8175
8176 (defun gnus-summary-mark-below (score mark)
8177   "Mark articles with score less than SCORE with MARK."
8178   (interactive "P\ncMark: ")
8179   (setq score (if score
8180                   (prefix-numeric-value score)
8181                 (or gnus-summary-default-score 0)))
8182   (save-excursion
8183     (set-buffer gnus-summary-buffer)
8184     (goto-char (point-min))
8185     (while
8186         (progn
8187           (and (< (gnus-summary-article-score) score)
8188                (gnus-summary-mark-article nil mark))
8189           (gnus-summary-find-next)))))
8190
8191 (defun gnus-summary-kill-below (&optional score)
8192   "Mark articles with score below SCORE as read."
8193   (interactive "P")
8194   (gnus-summary-mark-below score gnus-killed-mark))
8195
8196 (defun gnus-summary-clear-above (&optional score)
8197   "Clear all marks from articles with score above SCORE."
8198   (interactive "P")
8199   (gnus-summary-mark-above score gnus-unread-mark))
8200
8201 (defun gnus-summary-tick-above (&optional score)
8202   "Tick all articles with score above SCORE."
8203   (interactive "P")
8204   (gnus-summary-mark-above score gnus-ticked-mark))
8205
8206 (defun gnus-summary-mark-above (score mark)
8207   "Mark articles with score over SCORE with MARK."
8208   (interactive "P\ncMark: ")
8209   (setq score (if score
8210                   (prefix-numeric-value score)
8211                 (or gnus-summary-default-score 0)))
8212   (save-excursion
8213     (set-buffer gnus-summary-buffer)
8214     (goto-char (point-min))
8215     (while (and (progn
8216                   (when (> (gnus-summary-article-score) score)
8217                     (gnus-summary-mark-article nil mark))
8218                   t)
8219                 (gnus-summary-find-next)))))
8220
8221 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8222 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8223 (defun gnus-summary-limit-include-expunged (&optional no-error)
8224   "Display all the hidden articles that were expunged for low scores."
8225   (interactive)
8226   (let ((buffer-read-only nil))
8227     (let ((scored gnus-newsgroup-scored)
8228           headers h)
8229       (while scored
8230         (unless (gnus-summary-goto-subject (caar scored))
8231           (and (setq h (gnus-summary-article-header (caar scored)))
8232                (< (cdar scored) gnus-summary-expunge-below)
8233                (push h headers)))
8234         (setq scored (cdr scored)))
8235       (if (not headers)
8236           (when (not no-error)
8237             (error "No expunged articles hidden"))
8238         (goto-char (point-min))
8239         (gnus-summary-prepare-unthreaded (nreverse headers))
8240         (goto-char (point-min))
8241         (gnus-summary-position-point)
8242         t))))
8243
8244 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8245   "Mark all unread articles in this newsgroup as read.
8246 If prefix argument ALL is non-nil, ticked and dormant articles will
8247 also be marked as read.
8248 If QUIETLY is non-nil, no questions will be asked.
8249 If TO-HERE is non-nil, it should be a point in the buffer.  All
8250 articles before this point will be marked as read.
8251 Note that this function will only catch up the unread article
8252 in the current summary buffer limitation.
8253 The number of articles marked as read is returned."
8254   (interactive "P")
8255   (prog1
8256       (save-excursion
8257         (when (or quietly
8258                   (not gnus-interactive-catchup) ;Without confirmation?
8259                   gnus-expert-user
8260                   (gnus-y-or-n-p
8261                    (if all
8262                        "Mark absolutely all articles as read? "
8263                      "Mark all unread articles as read? ")))
8264           (if (and not-mark
8265                    (not gnus-newsgroup-adaptive)
8266                    (not gnus-newsgroup-auto-expire)
8267                    (not gnus-suppress-duplicates)
8268                    (or (not gnus-use-cache)
8269                        (eq gnus-use-cache 'passive)))
8270               (progn
8271                 (when all
8272                   (setq gnus-newsgroup-marked nil
8273                         gnus-newsgroup-dormant nil))
8274                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8275             ;; We actually mark all articles as canceled, which we
8276             ;; have to do when using auto-expiry or adaptive scoring.
8277             (gnus-summary-show-all-threads)
8278             (when (gnus-summary-first-subject (not all) t)
8279               (while (and
8280                       (if to-here (< (point) to-here) t)
8281                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8282                       (gnus-summary-find-next (not all) nil nil t))))
8283             (gnus-set-mode-line 'summary))
8284           t))
8285     (gnus-summary-position-point)))
8286
8287 (defun gnus-summary-catchup-to-here (&optional all)
8288   "Mark all unticked articles before the current one as read.
8289 If ALL is non-nil, also mark ticked and dormant articles as read."
8290   (interactive "P")
8291   (save-excursion
8292     (gnus-save-hidden-threads
8293       (let ((beg (point)))
8294         ;; We check that there are unread articles.
8295         (when (or all (gnus-summary-find-prev))
8296           (gnus-summary-catchup all t beg)))))
8297   (gnus-summary-position-point))
8298
8299 (defun gnus-summary-catchup-all (&optional quietly)
8300   "Mark all articles in this newsgroup as read."
8301   (interactive "P")
8302   (gnus-summary-catchup t quietly))
8303
8304 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8305   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8306 If prefix argument ALL is non-nil, all articles are marked as read."
8307   (interactive "P")
8308   (when (gnus-summary-catchup all quietly nil 'fast)
8309     ;; Select next newsgroup or exit.
8310     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8311              (eq gnus-auto-select-next 'quietly))
8312         (gnus-summary-next-group nil)
8313       (gnus-summary-exit))))
8314
8315 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8316   "Mark all articles in this newsgroup as read, and then exit."
8317   (interactive "P")
8318   (gnus-summary-catchup-and-exit t quietly))
8319
8320 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8321 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8322   "Mark all articles in this group as read and select the next group.
8323 If given a prefix, mark all articles, unread as well as ticked, as
8324 read."
8325   (interactive "P")
8326   (save-excursion
8327     (gnus-summary-catchup all))
8328   (gnus-summary-next-article t nil nil t))
8329
8330 ;; Thread-based commands.
8331
8332 (defun gnus-summary-articles-in-thread (&optional article)
8333   "Return a list of all articles in the current thread.
8334 If ARTICLE is non-nil, return all articles in the thread that starts
8335 with that article."
8336   (let* ((article (or article (gnus-summary-article-number)))
8337          (data (gnus-data-find-list article))
8338          (top-level (gnus-data-level (car data)))
8339          (top-subject
8340           (cond ((null gnus-thread-operation-ignore-subject)
8341                  (gnus-simplify-subject-re
8342                   (mail-header-subject (gnus-data-header (car data)))))
8343                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8344                  (gnus-simplify-subject-fuzzy
8345                   (mail-header-subject (gnus-data-header (car data)))))
8346                 (t nil)))
8347          (end-point (save-excursion
8348                       (if (gnus-summary-go-to-next-thread)
8349                           (point) (point-max))))
8350          articles)
8351     (while (and data
8352                 (< (gnus-data-pos (car data)) end-point))
8353       (when (or (not top-subject)
8354                 (string= top-subject
8355                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8356                              (gnus-simplify-subject-fuzzy
8357                               (mail-header-subject
8358                                (gnus-data-header (car data))))
8359                            (gnus-simplify-subject-re
8360                             (mail-header-subject
8361                              (gnus-data-header (car data)))))))
8362         (push (gnus-data-number (car data)) articles))
8363       (unless (and (setq data (cdr data))
8364                    (> (gnus-data-level (car data)) top-level))
8365         (setq data nil)))
8366     ;; Return the list of articles.
8367     (nreverse articles)))
8368
8369 (defun gnus-summary-rethread-current ()
8370   "Rethread the thread the current article is part of."
8371   (interactive)
8372   (let* ((gnus-show-threads t)
8373          (article (gnus-summary-article-number))
8374          (id (mail-header-id (gnus-summary-article-header)))
8375          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8376     (unless id
8377       (error "No article on the current line"))
8378     (gnus-rebuild-thread id)
8379     (gnus-summary-goto-subject article)))
8380
8381 (defun gnus-summary-reparent-thread ()
8382   "Make the current article child of the marked (or previous) article.
8383
8384 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8385 is non-nil or the Subject: of both articles are the same."
8386   (interactive)
8387   (unless (not (gnus-group-read-only-p))
8388     (error "The current newsgroup does not support article editing"))
8389   (unless (<= (length gnus-newsgroup-processable) 1)
8390     (error "No more than one article may be marked"))
8391   (save-window-excursion
8392     (let ((gnus-article-buffer " *reparent*")
8393           (current-article (gnus-summary-article-number))
8394           ;; First grab the marked article, otherwise one line up.
8395           (parent-article (if (not (null gnus-newsgroup-processable))
8396                               (car gnus-newsgroup-processable)
8397                             (save-excursion
8398                               (if (eq (forward-line -1) 0)
8399                                   (gnus-summary-article-number)
8400                                 (error "Beginning of summary buffer"))))))
8401       (unless (not (eq current-article parent-article))
8402         (error "An article may not be self-referential"))
8403       (let ((message-id (mail-header-id
8404                          (gnus-summary-article-header parent-article))))
8405         (unless (and message-id (not (equal message-id "")))
8406           (error "No message-id in desired parent"))
8407         (gnus-with-article current-article
8408           (goto-char (point-min))
8409           (if (re-search-forward "^References: " nil t)
8410               (progn
8411                 (re-search-forward "^[^ \t]" nil t)
8412                 (forward-line -1)
8413                 (end-of-line)
8414                 (insert " " message-id))
8415             (insert "References: " message-id "\n")))
8416         (set-buffer gnus-summary-buffer)
8417         (gnus-summary-unmark-all-processable)
8418         (gnus-summary-update-article current-article)
8419         (gnus-summary-rethread-current)
8420         (gnus-message 3 "Article %d is now the child of article %d"
8421                       current-article parent-article)))))
8422
8423 (defun gnus-summary-toggle-threads (&optional arg)
8424   "Toggle showing conversation threads.
8425 If ARG is positive number, turn showing conversation threads on."
8426   (interactive "P")
8427   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8428     (setq gnus-show-threads
8429           (if (null arg) (not gnus-show-threads)
8430             (> (prefix-numeric-value arg) 0)))
8431     (gnus-summary-prepare)
8432     (gnus-summary-goto-subject current)
8433     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8434     (gnus-summary-position-point)))
8435
8436 (defun gnus-summary-show-all-threads ()
8437   "Show all threads."
8438   (interactive)
8439   (save-excursion
8440     (let ((buffer-read-only nil))
8441       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8442   (gnus-summary-position-point))
8443
8444 (defun gnus-summary-show-thread ()
8445   "Show thread subtrees.
8446 Returns nil if no thread was there to be shown."
8447   (interactive)
8448   (let ((buffer-read-only nil)
8449         (orig (point))
8450         ;; first goto end then to beg, to have point at beg after let
8451         (end (progn (end-of-line) (point)))
8452         (beg (progn (beginning-of-line) (point))))
8453     (prog1
8454         ;; Any hidden lines here?
8455         (search-forward "\r" end t)
8456       (subst-char-in-region beg end ?\^M ?\n t)
8457       (goto-char orig)
8458       (gnus-summary-position-point))))
8459
8460 (defun gnus-summary-hide-all-threads ()
8461   "Hide all thread subtrees."
8462   (interactive)
8463   (save-excursion
8464     (goto-char (point-min))
8465     (gnus-summary-hide-thread)
8466     (while (zerop (gnus-summary-next-thread 1 t))
8467       (gnus-summary-hide-thread)))
8468   (gnus-summary-position-point))
8469
8470 (defun gnus-summary-hide-thread ()
8471   "Hide thread subtrees.
8472 Returns nil if no threads were there to be hidden."
8473   (interactive)
8474   (let ((buffer-read-only nil)
8475         (start (point))
8476         (article (gnus-summary-article-number)))
8477     (goto-char start)
8478     ;; Go forward until either the buffer ends or the subthread
8479     ;; ends.
8480     (when (and (not (eobp))
8481                (or (zerop (gnus-summary-next-thread 1 t))
8482                    (goto-char (point-max))))
8483       (prog1
8484           (if (and (> (point) start)
8485                    (search-backward "\n" start t))
8486               (progn
8487                 (subst-char-in-region start (point) ?\n ?\^M)
8488                 (gnus-summary-goto-subject article))
8489             (goto-char start)
8490             nil)
8491         ;;(gnus-summary-position-point)
8492         ))))
8493
8494 (defun gnus-summary-go-to-next-thread (&optional previous)
8495   "Go to the same level (or less) next thread.
8496 If PREVIOUS is non-nil, go to previous thread instead.
8497 Return the article number moved to, or nil if moving was impossible."
8498   (let ((level (gnus-summary-thread-level))
8499         (way (if previous -1 1))
8500         (beg (point)))
8501     (forward-line way)
8502     (while (and (not (eobp))
8503                 (< level (gnus-summary-thread-level)))
8504       (forward-line way))
8505     (if (eobp)
8506         (progn
8507           (goto-char beg)
8508           nil)
8509       (setq beg (point))
8510       (prog1
8511           (gnus-summary-article-number)
8512         (goto-char beg)))))
8513
8514 (defun gnus-summary-next-thread (n &optional silent)
8515   "Go to the same level next N'th thread.
8516 If N is negative, search backward instead.
8517 Returns the difference between N and the number of skips actually
8518 done.
8519
8520 If SILENT, don't output messages."
8521   (interactive "p")
8522   (let ((backward (< n 0))
8523         (n (abs n)))
8524     (while (and (> n 0)
8525                 (gnus-summary-go-to-next-thread backward))
8526       (decf n))
8527     (unless silent
8528       (gnus-summary-position-point))
8529     (when (and (not silent) (/= 0 n))
8530       (gnus-message 7 "No more threads"))
8531     n))
8532
8533 (defun gnus-summary-prev-thread (n)
8534   "Go to the same level previous N'th thread.
8535 Returns the difference between N and the number of skips actually
8536 done."
8537   (interactive "p")
8538   (gnus-summary-next-thread (- n)))
8539
8540 (defun gnus-summary-go-down-thread ()
8541   "Go down one level in the current thread."
8542   (let ((children (gnus-summary-article-children)))
8543     (when children
8544       (gnus-summary-goto-subject (car children)))))
8545
8546 (defun gnus-summary-go-up-thread ()
8547   "Go up one level in the current thread."
8548   (let ((parent (gnus-summary-article-parent)))
8549     (when parent
8550       (gnus-summary-goto-subject parent))))
8551
8552 (defun gnus-summary-down-thread (n)
8553   "Go down thread N steps.
8554 If N is negative, go up instead.
8555 Returns the difference between N and how many steps down that were
8556 taken."
8557   (interactive "p")
8558   (let ((up (< n 0))
8559         (n (abs n)))
8560     (while (and (> n 0)
8561                 (if up (gnus-summary-go-up-thread)
8562                   (gnus-summary-go-down-thread)))
8563       (setq n (1- n)))
8564     (gnus-summary-position-point)
8565     (when (/= 0 n)
8566       (gnus-message 7 "Can't go further"))
8567     n))
8568
8569 (defun gnus-summary-up-thread (n)
8570   "Go up thread N steps.
8571 If N is negative, go up instead.
8572 Returns the difference between N and how many steps down that were
8573 taken."
8574   (interactive "p")
8575   (gnus-summary-down-thread (- n)))
8576
8577 (defun gnus-summary-top-thread ()
8578   "Go to the top of the thread."
8579   (interactive)
8580   (while (gnus-summary-go-up-thread))
8581   (gnus-summary-article-number))
8582
8583 (defun gnus-summary-kill-thread (&optional unmark)
8584   "Mark articles under current thread as read.
8585 If the prefix argument is positive, remove any kinds of marks.
8586 If the prefix argument is negative, tick articles instead."
8587   (interactive "P")
8588   (when unmark
8589     (setq unmark (prefix-numeric-value unmark)))
8590   (let ((articles (gnus-summary-articles-in-thread)))
8591     (save-excursion
8592       ;; Expand the thread.
8593       (gnus-summary-show-thread)
8594       ;; Mark all the articles.
8595       (while articles
8596         (gnus-summary-goto-subject (car articles))
8597         (cond ((null unmark)
8598                (gnus-summary-mark-article-as-read gnus-killed-mark))
8599               ((> unmark 0)
8600                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8601               (t
8602                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8603         (setq articles (cdr articles))))
8604     ;; Hide killed subtrees.
8605     (and (null unmark)
8606          gnus-thread-hide-killed
8607          (gnus-summary-hide-thread))
8608     ;; If marked as read, go to next unread subject.
8609     (when (null unmark)
8610       ;; Go to next unread subject.
8611       (gnus-summary-next-subject 1 t)))
8612   (gnus-set-mode-line 'summary))
8613
8614 ;; Summary sorting commands
8615
8616 (defun gnus-summary-sort-by-number (&optional reverse)
8617   "Sort the summary buffer by article number.
8618 Argument REVERSE means reverse order."
8619   (interactive "P")
8620   (gnus-summary-sort 'number reverse))
8621
8622 (defun gnus-summary-sort-by-author (&optional reverse)
8623   "Sort the summary buffer by author name alphabetically.
8624 If case-fold-search is non-nil, case of letters is ignored.
8625 Argument REVERSE means reverse order."
8626   (interactive "P")
8627   (gnus-summary-sort 'author reverse))
8628
8629 (defun gnus-summary-sort-by-subject (&optional reverse)
8630   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8631 If case-fold-search is non-nil, case of letters is ignored.
8632 Argument REVERSE means reverse order."
8633   (interactive "P")
8634   (gnus-summary-sort 'subject reverse))
8635
8636 (defun gnus-summary-sort-by-date (&optional reverse)
8637   "Sort the summary buffer by date.
8638 Argument REVERSE means reverse order."
8639   (interactive "P")
8640   (gnus-summary-sort 'date reverse))
8641
8642 (defun gnus-summary-sort-by-score (&optional reverse)
8643   "Sort the summary buffer by score.
8644 Argument REVERSE means reverse order."
8645   (interactive "P")
8646   (gnus-summary-sort 'score reverse))
8647
8648 (defun gnus-summary-sort-by-lines (&optional reverse)
8649   "Sort the summary buffer by the number of lines.
8650 Argument REVERSE means reverse order."
8651   (interactive "P")
8652   (gnus-summary-sort 'lines reverse))
8653
8654 (defun gnus-summary-sort-by-chars (&optional reverse)
8655   "Sort the summary buffer by article length.
8656 Argument REVERSE means reverse order."
8657   (interactive "P")
8658   (gnus-summary-sort 'chars reverse))   
8659
8660 (defun gnus-summary-sort (predicate reverse)
8661   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8662   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8663          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8664          (gnus-thread-sort-functions
8665           (if (not reverse)
8666               thread
8667             `(lambda (t1 t2)
8668                (,thread t2 t1))))
8669          (gnus-article-sort-functions
8670           (if (not reverse)
8671               article
8672             `(lambda (t1 t2)
8673                (,article t2 t1))))
8674          (buffer-read-only)
8675          (gnus-summary-prepare-hook nil))
8676     ;; We do the sorting by regenerating the threads.
8677     (gnus-summary-prepare)
8678     ;; Hide subthreads if needed.
8679     (when (and gnus-show-threads gnus-thread-hide-subtree)
8680       (gnus-summary-hide-all-threads))))
8681
8682 ;; Summary saving commands.
8683
8684 (defun gnus-summary-save-article (&optional n not-saved)
8685   "Save the current article using the default saver function.
8686 If N is a positive number, save the N next articles.
8687 If N is a negative number, save the N previous articles.
8688 If N is nil and any articles have been marked with the process mark,
8689 save those articles instead.
8690 The variable `gnus-default-article-saver' specifies the saver function."
8691   (interactive "P")
8692   (let* ((articles (gnus-summary-work-articles n))
8693          (save-buffer (save-excursion
8694                         (nnheader-set-temp-buffer " *Gnus Save*")))
8695          (num (length articles))
8696          header file)
8697     (dolist (article articles)
8698       (setq header (gnus-summary-article-header article))
8699       (if (not (vectorp header))
8700           ;; This is a pseudo-article.
8701           (if (assq 'name header)
8702               (gnus-copy-file (cdr (assq 'name header)))
8703             (gnus-message 1 "Article %d is unsaveable" article))
8704         ;; This is a real article.
8705         (save-window-excursion
8706           (gnus-summary-select-article t nil nil article))
8707         (save-excursion
8708           (set-buffer save-buffer)
8709           (erase-buffer)
8710           (insert-buffer-substring gnus-original-article-buffer))
8711         (setq file (gnus-article-save save-buffer file num))
8712         (gnus-summary-remove-process-mark article)
8713         (unless not-saved
8714           (gnus-summary-set-saved-mark article))))
8715     (gnus-kill-buffer save-buffer)
8716     (gnus-summary-position-point)
8717     (gnus-set-mode-line 'summary)
8718     n))
8719
8720 (defun gnus-summary-pipe-output (&optional arg)
8721   "Pipe the current article to a subprocess.
8722 If N is a positive number, pipe the N next articles.
8723 If N is a negative number, pipe the N previous articles.
8724 If N is nil and any articles have been marked with the process mark,
8725 pipe those articles instead."
8726   (interactive "P")
8727   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8728     (gnus-summary-save-article arg t))
8729   (gnus-configure-windows 'pipe))
8730
8731 (defun gnus-summary-save-article-mail (&optional arg)
8732   "Append the current article to an mail file.
8733 If N is a positive number, save the N next articles.
8734 If N is a negative number, save the N previous articles.
8735 If N is nil and any articles have been marked with the process mark,
8736 save those articles instead."
8737   (interactive "P")
8738   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8739     (gnus-summary-save-article arg)))
8740
8741 (defun gnus-summary-save-article-rmail (&optional arg)
8742   "Append the current article to an rmail file.
8743 If N is a positive number, save the N next articles.
8744 If N is a negative number, save the N previous articles.
8745 If N is nil and any articles have been marked with the process mark,
8746 save those articles instead."
8747   (interactive "P")
8748   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8749     (gnus-summary-save-article arg)))
8750
8751 (defun gnus-summary-save-article-file (&optional arg)
8752   "Append the current article to a file.
8753 If N is a positive number, save the N next articles.
8754 If N is a negative number, save the N previous articles.
8755 If N is nil and any articles have been marked with the process mark,
8756 save those articles instead."
8757   (interactive "P")
8758   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8759     (gnus-summary-save-article arg)))
8760
8761 (defun gnus-summary-write-article-file (&optional arg)
8762   "Write the current article to a file, deleting the previous file.
8763 If N is a positive number, save the N next articles.
8764 If N is a negative number, save the N previous articles.
8765 If N is nil and any articles have been marked with the process mark,
8766 save those articles instead."
8767   (interactive "P")
8768   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8769     (gnus-summary-save-article arg)))
8770
8771 (defun gnus-summary-save-article-body-file (&optional arg)
8772   "Append the current article body to a file.
8773 If N is a positive number, save the N next articles.
8774 If N is a negative number, save the N previous articles.
8775 If N is nil and any articles have been marked with the process mark,
8776 save those articles instead."
8777   (interactive "P")
8778   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8779     (gnus-summary-save-article arg)))
8780
8781 (defun gnus-summary-pipe-message (program)
8782   "Pipe the current article through PROGRAM."
8783   (interactive "sProgram: ")
8784   (gnus-summary-select-article)
8785   (let ((mail-header-separator ""))
8786     (gnus-eval-in-buffer-window gnus-article-buffer
8787       (save-restriction
8788         (widen)
8789         (let ((start (window-start))
8790               buffer-read-only)
8791           (message-pipe-buffer-body program)
8792           (set-window-start (get-buffer-window (current-buffer)) start))))))
8793
8794 (defun gnus-get-split-value (methods)
8795   "Return a value based on the split METHODS."
8796   (let (split-name method result match)
8797     (when methods
8798       (save-excursion
8799         (set-buffer gnus-original-article-buffer)
8800         (save-restriction
8801           (nnheader-narrow-to-headers)
8802           (while methods
8803             (goto-char (point-min))
8804             (setq method (pop methods))
8805             (setq match (car method))
8806             (when (cond
8807                    ((stringp match)
8808                     ;; Regular expression.
8809                     (ignore-errors
8810                       (re-search-forward match nil t)))
8811                    ((gnus-functionp match)
8812                     ;; Function.
8813                     (save-restriction
8814                       (widen)
8815                       (setq result (funcall match gnus-newsgroup-name))))
8816                    ((consp match)
8817                     ;; Form.
8818                     (save-restriction
8819                       (widen)
8820                       (setq result (eval match)))))
8821               (setq split-name (append (cdr method) split-name))
8822               (cond ((stringp result)
8823                      (push (expand-file-name
8824                             result gnus-article-save-directory)
8825                            split-name))
8826                     ((consp result)
8827                      (setq split-name (append result split-name)))))))))
8828     (nreverse split-name)))
8829
8830 (defun gnus-valid-move-group-p (group)
8831   (and (boundp group)
8832        (symbol-name group)
8833        (symbol-value group)
8834        (gnus-get-function (gnus-find-method-for-group
8835                            (symbol-name group)) 'request-accept-article t)))
8836
8837 (defun gnus-read-move-group-name (prompt default articles prefix)
8838   "Read a group name."
8839   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8840          (minibuffer-confirm-incomplete nil) ; XEmacs
8841          (prom
8842           (format "%s %s to:"
8843                   prompt
8844                   (if (> (length articles) 1)
8845                       (format "these %d articles" (length articles))
8846                     "this article")))
8847          (to-newsgroup
8848           (cond
8849            ((null split-name)
8850             (gnus-completing-read default prom
8851                                   gnus-active-hashtb
8852                                   'gnus-valid-move-group-p
8853                                   nil prefix
8854                                   'gnus-group-history))
8855            ((= 1 (length split-name))
8856             (gnus-completing-read (car split-name) prom
8857                                   gnus-active-hashtb
8858                                   'gnus-valid-move-group-p
8859                                   nil nil
8860                                   'gnus-group-history))
8861            (t
8862             (gnus-completing-read nil prom
8863                                   (mapcar (lambda (el) (list el))
8864                                           (nreverse split-name))
8865                                   nil nil nil
8866                                   'gnus-group-history)))))
8867     (when to-newsgroup
8868       (if (or (string= to-newsgroup "")
8869               (string= to-newsgroup prefix))
8870           (setq to-newsgroup default))
8871       (unless to-newsgroup
8872         (error "No group name entered"))
8873       (or (gnus-active to-newsgroup)
8874           (gnus-activate-group to-newsgroup)
8875           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8876                                      to-newsgroup))
8877               (or (and (gnus-request-create-group
8878                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8879                        (gnus-activate-group to-newsgroup nil nil
8880                                             (gnus-group-name-to-method
8881                                              to-newsgroup)))
8882                   (error "Couldn't create group %s" to-newsgroup)))
8883           (error "No such group: %s" to-newsgroup)))
8884     to-newsgroup))
8885
8886 (defun gnus-summary-save-parts (type dir n reverse)
8887   "Save parts matching TYPE to DIR.
8888 If REVERSE, save parts that do not match TYPE."
8889   (interactive
8890    (list (read-string "Save parts of type: " "image/.*")
8891          (read-file-name "Save to directory: " t nil t)
8892          current-prefix-arg))
8893   (gnus-summary-iterate n
8894     (let ((gnus-display-mime-function nil)
8895           (gnus-inhibit-treatment t))
8896       (gnus-summary-select-article))
8897     (save-excursion
8898       (set-buffer gnus-article-buffer)
8899       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8900         (when handles
8901           (gnus-summary-save-parts-1 type dir handles reverse)
8902           (mm-destroy-parts handles))))))
8903
8904 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8905   (if (stringp (car handle))
8906       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8907               (cdr handle))
8908     (when (if reverse
8909               (not (string-match type (car (mm-handle-type handle))))
8910             (string-match type (car (mm-handle-type handle))))
8911       (let ((file (expand-file-name
8912                    (file-name-nondirectory
8913                     (or
8914                      (mail-content-type-get
8915                       (mm-handle-disposition handle) 'filename)
8916                      (concat gnus-newsgroup-name "." gnus-current-article)))
8917                    dir)))
8918         (unless (file-exists-p file)
8919           (mm-save-part-to-file handle file))))))
8920
8921 ;; Summary extract commands
8922
8923 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8924   (let ((buffer-read-only nil)
8925         (article (gnus-summary-article-number))
8926         after-article b e)
8927     (unless (gnus-summary-goto-subject article)
8928       (error "No such article: %d" article))
8929     (gnus-summary-position-point)
8930     ;; If all commands are to be bunched up on one line, we collect
8931     ;; them here.
8932     (unless gnus-view-pseudos-separately
8933       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8934             files action)
8935         (while ps
8936           (setq action (cdr (assq 'action (car ps))))
8937           (setq files (list (cdr (assq 'name (car ps)))))
8938           (while (and ps (cdr ps)
8939                       (string= (or action "1")
8940                                (or (cdr (assq 'action (cadr ps))) "2")))
8941             (push (cdr (assq 'name (cadr ps))) files)
8942             (setcdr ps (cddr ps)))
8943           (when files
8944             (when (not (string-match "%s" action))
8945               (push " " files))
8946             (push " " files)
8947             (when (assq 'execute (car ps))
8948               (setcdr (assq 'execute (car ps))
8949                       (funcall (if (string-match "%s" action)
8950                                    'format 'concat)
8951                                action
8952                                (mapconcat
8953                                 (lambda (f)
8954                                   (if (equal f " ")
8955                                       f
8956                                     (mm-quote-arg f)))
8957                                 files " ")))))
8958           (setq ps (cdr ps)))))
8959     (if (and gnus-view-pseudos (not not-view))
8960         (while pslist
8961           (when (assq 'execute (car pslist))
8962             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8963                                   (eq gnus-view-pseudos 'not-confirm)))
8964           (setq pslist (cdr pslist)))
8965       (save-excursion
8966         (while pslist
8967           (setq after-article (or (cdr (assq 'article (car pslist)))
8968                                   (gnus-summary-article-number)))
8969           (gnus-summary-goto-subject after-article)
8970           (forward-line 1)
8971           (setq b (point))
8972           (insert "    " (file-name-nondirectory
8973                           (cdr (assq 'name (car pslist))))
8974                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8975           (setq e (point))
8976           (forward-line -1)             ; back to `b'
8977           (gnus-add-text-properties
8978            b (1- e) (list 'gnus-number gnus-reffed-article-number
8979                           gnus-mouse-face-prop gnus-mouse-face))
8980           (gnus-data-enter
8981            after-article gnus-reffed-article-number
8982            gnus-unread-mark b (car pslist) 0 (- e b))
8983           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8984           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8985           (setq pslist (cdr pslist)))))))
8986
8987 (defun gnus-pseudos< (p1 p2)
8988   (let ((c1 (cdr (assq 'action p1)))
8989         (c2 (cdr (assq 'action p2))))
8990     (and c1 c2 (string< c1 c2))))
8991
8992 (defun gnus-request-pseudo-article (props)
8993   (cond ((assq 'execute props)
8994          (gnus-execute-command (cdr (assq 'execute props)))))
8995   (let ((gnus-current-article (gnus-summary-article-number)))
8996     (gnus-run-hooks 'gnus-mark-article-hook)))
8997
8998 (defun gnus-execute-command (command &optional automatic)
8999   (save-excursion
9000     (gnus-article-setup-buffer)
9001     (set-buffer gnus-article-buffer)
9002     (setq buffer-read-only nil)
9003     (let ((command (if automatic command
9004                      (read-string "Command: " (cons command 0)))))
9005       (erase-buffer)
9006       (insert "$ " command "\n\n")
9007       (if gnus-view-pseudo-asynchronously
9008           (start-process "gnus-execute" (current-buffer) shell-file-name
9009                          shell-command-switch command)
9010         (call-process shell-file-name nil t nil
9011                       shell-command-switch command)))))
9012
9013 ;; Summary kill commands.
9014
9015 (defun gnus-summary-edit-global-kill (article)
9016   "Edit the \"global\" kill file."
9017   (interactive (list (gnus-summary-article-number)))
9018   (gnus-group-edit-global-kill article))
9019
9020 (defun gnus-summary-edit-local-kill ()
9021   "Edit a local kill file applied to the current newsgroup."
9022   (interactive)
9023   (setq gnus-current-headers (gnus-summary-article-header))
9024   (gnus-group-edit-local-kill
9025    (gnus-summary-article-number) gnus-newsgroup-name))
9026
9027 ;;; Header reading.
9028
9029 (defun gnus-read-header (id &optional header)
9030   "Read the headers of article ID and enter them into the Gnus system."
9031   (let ((group gnus-newsgroup-name)
9032         (gnus-override-method
9033          (and (gnus-news-group-p gnus-newsgroup-name)
9034               gnus-refer-article-method))
9035         where)
9036     ;; First we check to see whether the header in question is already
9037     ;; fetched.
9038     (if (stringp id)
9039         ;; This is a Message-ID.
9040         (setq header (or header (gnus-id-to-header id)))
9041       ;; This is an article number.
9042       (setq header (or header (gnus-summary-article-header id))))
9043     (if (and header
9044              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9045         ;; We have found the header.
9046         header
9047       ;; If this is a sparse article, we have to nix out its
9048       ;; previous entry in the thread hashtb.
9049       (when (and header
9050                  (gnus-summary-article-sparse-p (mail-header-number header)))
9051         (let* ((parent (gnus-parent-id (mail-header-references header)))
9052                (thread (and parent (gnus-id-to-thread parent))))
9053           (when thread
9054             (delq (assq header thread) thread))))
9055       ;; We have to really fetch the header to this article.
9056       (save-excursion
9057         (set-buffer nntp-server-buffer)
9058         (when (setq where (gnus-request-head id group))
9059           (nnheader-fold-continuation-lines)
9060           (goto-char (point-max))
9061           (insert ".\n")
9062           (goto-char (point-min))
9063           (insert "211 ")
9064           (princ (cond
9065                   ((numberp id) id)
9066                   ((cdr where) (cdr where))
9067                   (header (mail-header-number header))
9068                   (t gnus-reffed-article-number))
9069                  (current-buffer))
9070           (insert " Article retrieved.\n"))
9071         (if (or (not where)
9072                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9073             ()                          ; Malformed head.
9074           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9075             (when (and (stringp id)
9076                        (not (string= (gnus-group-real-name group)
9077                                      (car where))))
9078               ;; If we fetched by Message-ID and the article came
9079               ;; from a different group, we fudge some bogus article
9080               ;; numbers for this article.
9081               (mail-header-set-number header gnus-reffed-article-number))
9082             (save-excursion
9083               (set-buffer gnus-summary-buffer)
9084               (decf gnus-reffed-article-number)
9085               (gnus-remove-header (mail-header-number header))
9086               (push header gnus-newsgroup-headers)
9087               (setq gnus-current-headers header)
9088               (push (mail-header-number header) gnus-newsgroup-limit)))
9089           header)))))
9090
9091 (defun gnus-remove-header (number)
9092   "Remove header NUMBER from `gnus-newsgroup-headers'."
9093   (if (and gnus-newsgroup-headers
9094            (= number (mail-header-number (car gnus-newsgroup-headers))))
9095       (pop gnus-newsgroup-headers)
9096     (let ((headers gnus-newsgroup-headers))
9097       (while (and (cdr headers)
9098                   (not (= number (mail-header-number (cadr headers)))))
9099         (pop headers))
9100       (when (cdr headers)
9101         (setcdr headers (cddr headers))))))
9102
9103 ;;;
9104 ;;; summary highlights
9105 ;;;
9106
9107 (defun gnus-highlight-selected-summary ()
9108   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9109   ;; Highlight selected article in summary buffer
9110   (when gnus-summary-selected-face
9111     (save-excursion
9112       (let* ((beg (progn (beginning-of-line) (point)))
9113              (end (progn (end-of-line) (point)))
9114              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9115              (from (if (get-text-property beg gnus-mouse-face-prop)
9116                        beg
9117                      (or (next-single-property-change
9118                           beg gnus-mouse-face-prop nil end)
9119                          beg)))
9120              (to
9121               (if (= from end)
9122                   (- from 2)
9123                 (or (next-single-property-change
9124                      from gnus-mouse-face-prop nil end)
9125                     end))))
9126         ;; If no mouse-face prop on line we will have to = from = end,
9127         ;; so we highlight the entire line instead.
9128         (when (= (+ to 2) from)
9129           (setq from beg)
9130           (setq to end))
9131         (if gnus-newsgroup-selected-overlay
9132             ;; Move old overlay.
9133             (gnus-move-overlay
9134              gnus-newsgroup-selected-overlay from to (current-buffer))
9135           ;; Create new overlay.
9136           (gnus-overlay-put
9137            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9138            'face gnus-summary-selected-face))))))
9139
9140 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9141 (defun gnus-summary-highlight-line ()
9142   "Highlight current line according to `gnus-summary-highlight'."
9143   (let* ((list gnus-summary-highlight)
9144          (p (point))
9145          (end (progn (end-of-line) (point)))
9146          ;; now find out where the line starts and leave point there.
9147          (beg (progn (beginning-of-line) (point)))
9148          (article (gnus-summary-article-number))
9149          (score (or (cdr (assq (or article gnus-current-article)
9150                                gnus-newsgroup-scored))
9151                     gnus-summary-default-score 0))
9152          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9153          (inhibit-read-only t))
9154     ;; Eval the cars of the lists until we find a match.
9155     (let ((default gnus-summary-default-score))
9156       (while (and list
9157                   (not (eval (caar list))))
9158         (setq list (cdr list))))
9159     (let ((face (cdar list)))
9160       (unless (eq face (get-text-property beg 'face))
9161         (gnus-put-text-property-excluding-characters-with-faces
9162          beg end 'face
9163          (setq face (if (boundp face) (symbol-value face) face)))
9164         (when gnus-summary-highlight-line-function
9165           (funcall gnus-summary-highlight-line-function article face))))
9166     (goto-char p)))
9167
9168 (defun gnus-update-read-articles (group unread &optional compute)
9169   "Update the list of read articles in GROUP."
9170   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9171          (entry (gnus-gethash group gnus-newsrc-hashtb))
9172          (info (nth 2 entry))
9173          (prev 1)
9174          (unread (sort (copy-sequence unread) '<))
9175          read)
9176     (if (or (not info) (not active))
9177         ;; There is no info on this group if it was, in fact,
9178         ;; killed.  Gnus stores no information on killed groups, so
9179         ;; there's nothing to be done.
9180         ;; One could store the information somewhere temporarily,
9181         ;; perhaps...  Hmmm...
9182         ()
9183       ;; Remove any negative articles numbers.
9184       (while (and unread (< (car unread) 0))
9185         (setq unread (cdr unread)))
9186       ;; Remove any expired article numbers
9187       (while (and unread (< (car unread) (car active)))
9188         (setq unread (cdr unread)))
9189       ;; Compute the ranges of read articles by looking at the list of
9190       ;; unread articles.
9191       (while unread
9192         (when (/= (car unread) prev)
9193           (push (if (= prev (1- (car unread))) prev
9194                   (cons prev (1- (car unread))))
9195                 read))
9196         (setq prev (1+ (car unread)))
9197         (setq unread (cdr unread)))
9198       (when (<= prev (cdr active))
9199         (push (cons prev (cdr active)) read))
9200       (setq read (if (> (length read) 1) (nreverse read) read))
9201       (if compute
9202           read
9203         (save-excursion
9204           (set-buffer gnus-group-buffer)
9205           (gnus-undo-register
9206             `(progn
9207                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9208                (gnus-info-set-read ',info ',(gnus-info-read info))
9209                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9210                (gnus-group-update-group ,group t))))
9211        ;; Propagate the read marks to the backend.
9212        (if (gnus-check-backend-function 'request-set-mark group)
9213            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9214                  (add (gnus-remove-from-range read (gnus-info-read info))))
9215              (when (or add del)
9216                (unless (gnus-check-group group)
9217                  (error "Can't open server for %s" group))
9218                (gnus-request-set-mark
9219                 group (delq nil (list (if add (list add 'add '(read)))
9220                                       (if del (list del 'del '(read)))))))))
9221         ;; Enter this list into the group info.
9222         (gnus-info-set-read info read)
9223         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9224         (gnus-get-unread-articles-in-group info (gnus-active group))
9225         t))))
9226
9227 (defun gnus-offer-save-summaries ()
9228   "Offer to save all active summary buffers."
9229   (save-excursion
9230     (let ((buflist (buffer-list))
9231           buffers bufname)
9232       ;; Go through all buffers and find all summaries.
9233       (while buflist
9234         (and (setq bufname (buffer-name (car buflist)))
9235              (string-match "Summary" bufname)
9236              (save-excursion
9237                (set-buffer bufname)
9238                ;; We check that this is, indeed, a summary buffer.
9239                (and (eq major-mode 'gnus-summary-mode)
9240                     ;; Also make sure this isn't bogus.
9241                     gnus-newsgroup-prepared
9242                     ;; Also make sure that this isn't a dead summary buffer.
9243                     (not gnus-dead-summary-mode)))
9244              (push bufname buffers))
9245         (setq buflist (cdr buflist)))
9246       ;; Go through all these summary buffers and offer to save them.
9247       (when buffers
9248         (map-y-or-n-p
9249          "Update summary buffer %s? "
9250          (lambda (buf)
9251            (switch-to-buffer buf)
9252            (gnus-summary-exit))
9253          buffers)))))
9254
9255 (defun gnus-summary-setup-default-charset ()
9256   "Setup newsgroup default charset."
9257   (let ((name (and gnus-newsgroup-name
9258                    (gnus-group-real-name gnus-newsgroup-name))))
9259     (setq gnus-newsgroup-charset
9260           (or (and gnus-newsgroup-name
9261                    (or (gnus-group-find-parameter gnus-newsgroup-name
9262                                                   'charset)
9263                        (let ((alist gnus-group-charset-alist)
9264                              elem (charset nil))
9265                          (while (setq elem (pop alist))
9266                            (when (and name
9267                                       (string-match (car elem) name))
9268                              (setq alist nil
9269                                    charset (cadr elem))))
9270                          charset)))
9271               gnus-default-charset))))
9272
9273 ;;;
9274 ;;; Mime Commands
9275 ;;;
9276
9277 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9278   "Display the current article buffer fully MIME-buttonized.
9279 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9280 treated as multipart/mixed."
9281   (interactive "P")
9282   (require 'gnus-art)
9283   (let ((gnus-unbuttonized-mime-types nil)
9284         (gnus-mime-display-multipart-as-mixed show-all-parts))
9285     (gnus-summary-show-article)))
9286
9287 (defun gnus-summary-repair-multipart (article)
9288   "Add a Content-Type header to a multipart article without one."
9289   (interactive (list (gnus-summary-article-number)))
9290   (gnus-with-article article
9291     (message-narrow-to-head)
9292     (goto-char (point-max))
9293     (widen)
9294     (when (search-forward "\n--" nil t)
9295       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9296         (message-narrow-to-head)
9297         (message-remove-header "Mime-Version")
9298         (message-remove-header "Content-Type")
9299         (goto-char (point-max))
9300         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9301                         separator))
9302         (insert "Mime-Version: 1.0\n")
9303         (widen))))
9304   (let (gnus-mark-article-hook)
9305     (gnus-summary-select-article t t nil article)))
9306
9307 (defun gnus-summary-toggle-display-buttonized ()
9308   "Toggle the buttonizing of the article buffer."
9309   (interactive)
9310   (require 'gnus-art)
9311   (if (setq gnus-inhibit-mime-unbuttonizing
9312             (not gnus-inhibit-mime-unbuttonizing))
9313       (let ((gnus-unbuttonized-mime-types nil))
9314         (gnus-summary-show-article))
9315     (gnus-summary-show-article)))
9316
9317 ;;;
9318 ;;; with article
9319 ;;;
9320
9321 (defmacro gnus-with-article (article &rest forms)
9322   "Select ARTICLE and perform FORMS in the original article buffer.
9323 Then replace the article with the result."
9324   `(progn
9325      ;; We don't want the article to be marked as read.
9326      (let (gnus-mark-article-hook)
9327        (gnus-summary-select-article t t nil ,article))
9328      (set-buffer gnus-original-article-buffer)
9329      ,@forms
9330      (if (not (gnus-check-backend-function
9331                'request-replace-article (car gnus-article-current)))
9332          (gnus-message 5 "Read-only group; not replacing")
9333        (unless (gnus-request-replace-article
9334                 ,article (car gnus-article-current)
9335                 (current-buffer) t)
9336          (error "Couldn't replace article")))
9337      ;; The cache and backlog have to be flushed somewhat.
9338      (when gnus-keep-backlog
9339        (gnus-backlog-remove-article
9340         (car gnus-article-current) (cdr gnus-article-current)))
9341      (when gnus-use-cache
9342        (gnus-cache-update-article
9343         (car gnus-article-current) (cdr gnus-article-current)))))
9344
9345 (put 'gnus-with-article 'lisp-indent-function 1)
9346 (put 'gnus-with-article 'edebug-form-spec '(form body))
9347
9348 ;;;
9349 ;;; Generic summary marking commands
9350 ;;;
9351
9352 (defvar gnus-summary-marking-alist
9353   '((read gnus-del-mark "d")
9354     (unread gnus-unread-mark "u")
9355     (ticked gnus-ticked-mark "!")
9356     (dormant gnus-dormant-mark "?")
9357     (expirable gnus-expirable-mark "e"))
9358   "An alist of names/marks/keystrokes.")
9359
9360 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9361 (defvar gnus-summary-mark-map)
9362
9363 (defun gnus-summary-make-all-marking-commands ()
9364   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9365   (dolist (elem gnus-summary-marking-alist)
9366     (apply 'gnus-summary-make-marking-command elem)))
9367
9368 (defun gnus-summary-make-marking-command (name mark keystroke)
9369   (let ((map (make-sparse-keymap)))
9370     (define-key gnus-summary-generic-mark-map keystroke map)
9371     (dolist (lway `((next "next" next nil "n")
9372                     (next-unread "next unread" next t "N")
9373                     (prev "previous" prev nil "p")
9374                     (prev-unread "previous unread" prev t "P")
9375                     (nomove "" nil nil ,keystroke)))
9376       (let ((func (gnus-summary-make-marking-command-1
9377                    mark (car lway) lway name)))
9378         (setq func (eval func))
9379         (define-key map (nth 4 lway) func)))))
9380       
9381 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9382   `(defun ,(intern
9383             (format "gnus-summary-put-mark-as-%s%s"
9384                     name (if (eq way 'nomove)
9385                              ""
9386                            (concat "-" (symbol-name way)))))
9387      (n)
9388      ,(format
9389        "Mark the current article as %s%s.
9390 If N, the prefix, then repeat N times.
9391 If N is negative, move in reverse order.
9392 The difference between N and the actual number of articles marked is
9393 returned."
9394        name (cadr lway))
9395      (interactive "p")
9396      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9397     
9398 (defun gnus-summary-generic-mark (n mark move unread)
9399   "Mark N articles with MARK."
9400   (unless (eq major-mode 'gnus-summary-mode)
9401     (error "This command can only be used in the summary buffer"))
9402   (gnus-summary-show-thread)
9403   (let ((nummove
9404          (cond
9405           ((eq move 'next) 1)
9406           ((eq move 'prev) -1)
9407           (t 0))))
9408     (if (zerop nummove)
9409         (setq n 1)
9410       (when (< n 0)
9411         (setq n (abs n)
9412               nummove (* -1 nummove))))
9413     (while (and (> n 0)
9414                 (gnus-summary-mark-article nil mark)
9415                 (zerop (gnus-summary-next-subject nummove unread t)))
9416       (setq n (1- n)))
9417     (when (/= 0 n)
9418       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9419     (gnus-summary-recenter)
9420     (gnus-summary-position-point)
9421     (gnus-set-mode-line 'summary)
9422     n))
9423
9424 (gnus-summary-make-all-marking-commands)
9425
9426 (gnus-ems-redefine)
9427
9428 (provide 'gnus-sum)
9429
9430 (run-hooks 'gnus-sum-load-hook)
9431
9432 ;;; gnus-sum.el ends here