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