Importing Pterodactyl Gnus v0.93.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-threading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-group-charset-alist
801   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
802     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
803     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
804     ("^relcom\\>" koi8-r)
805     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
806     ("^israel\\>" iso-8859-1)
807     ("^han\\>" euc-kr)
808     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
809     (".*" iso-8859-1))
810   "Alist of regexps (to match group names) and default charsets to be used when reading."
811   :type '(repeat (list (regexp :tag "Group")
812                        (symbol :tag "Charset")))
813   :group 'gnus-charset)
814
815 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
816   "List of charsets that should be ignored.
817 When these charsets are used in the \"charset\" parameter, the
818 default charset will be used instead."
819   :type '(repeat symbol)
820   :group 'gnus-charset)
821
822 (defcustom gnus-group-ignored-charsets-alist 
823   '(("alt\\.chinese\\.text" iso-8859-1))
824   "Alist of regexps (to match group names) and charsets that should be ignored.
825 When these charsets are used in the \"charset\" parameter, the
826 default charset will be used instead."
827   :type '(repeat (cons (regexp :tag "Group")
828                        (repeat symbol)))
829   :group 'gnus-charset)
830
831 (defcustom gnus-group-highlight-words-alist nil
832   "Alist of group regexps and highlight regexps.
833 This variable uses the same syntax as `gnus-emphasis-alist'."
834   :type '(repeat (cons (regexp :tag "Group")
835                        (repeat (list (regexp :tag "Highlight regexp")
836                                      (number :tag "Group for entire word" 0)
837                                      (number :tag "Group for displayed part" 0)
838                                      (symbol :tag "Face" 
839                                              gnus-emphasis-highlight-words)))))
840   :group 'gnus-summary-visual)
841
842 ;;; Internal variables
843
844 (defvar gnus-article-mime-handles nil)
845 (defvar gnus-article-decoded-p nil)
846 (defvar gnus-scores-exclude-files nil)
847 (defvar gnus-page-broken nil)
848 (defvar gnus-inhibit-mime-unbuttonizing nil)
849
850 (defvar gnus-original-article nil)
851 (defvar gnus-article-internal-prepare-hook nil)
852 (defvar gnus-newsgroup-process-stack nil)
853
854 (defvar gnus-thread-indent-array nil)
855 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
856
857 ;; Avoid highlighting in kill files.
858 (defvar gnus-summary-inhibit-highlight nil)
859 (defvar gnus-newsgroup-selected-overlay nil)
860 (defvar gnus-inhibit-limiting nil)
861 (defvar gnus-newsgroup-adaptive-score-file nil)
862 (defvar gnus-current-score-file nil)
863 (defvar gnus-current-move-group nil)
864 (defvar gnus-current-copy-group nil)
865 (defvar gnus-current-crosspost-group nil)
866
867 (defvar gnus-newsgroup-dependencies nil)
868 (defvar gnus-newsgroup-adaptive nil)
869 (defvar gnus-summary-display-article-function nil)
870 (defvar gnus-summary-highlight-line-function nil
871   "Function called after highlighting a summary line.")
872
873 (defvar gnus-summary-line-format-alist
874   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
875     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
876     (?s gnus-tmp-subject-or-nil ?s)
877     (?n gnus-tmp-name ?s)
878     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
879         ?s)
880     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
881             gnus-tmp-from) ?s)
882     (?F gnus-tmp-from ?s)
883     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
884     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
885     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
886     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
887     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
888     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
889     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
890     (?L gnus-tmp-lines ?d)
891     (?I gnus-tmp-indentation ?s)
892     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
893     (?R gnus-tmp-replied ?c)
894     (?\[ gnus-tmp-opening-bracket ?c)
895     (?\] gnus-tmp-closing-bracket ?c)
896     (?\> (make-string gnus-tmp-level ? ) ?s)
897     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
898     (?i gnus-tmp-score ?d)
899     (?z gnus-tmp-score-char ?c)
900     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
901     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
902     (?U gnus-tmp-unread ?c)
903     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
904     (?t (gnus-summary-number-of-articles-in-thread
905          (and (boundp 'thread) (car thread)) gnus-tmp-level)
906         ?d)
907     (?e (gnus-summary-number-of-articles-in-thread
908          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
909         ?c)
910     (?u gnus-tmp-user-defined ?s)
911     (?P (gnus-pick-line-number) ?d))
912   "An alist of format specifications that can appear in summary lines,
913 and what variables they correspond with, along with the type of the
914 variable (string, integer, character, etc).")
915
916 (defvar gnus-summary-dummy-line-format-alist
917   `((?S gnus-tmp-subject ?s)
918     (?N gnus-tmp-number ?d)
919     (?u gnus-tmp-user-defined ?s)))
920
921 (defvar gnus-summary-mode-line-format-alist
922   `((?G gnus-tmp-group-name ?s)
923     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
924     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
925     (?A gnus-tmp-article-number ?d)
926     (?Z gnus-tmp-unread-and-unselected ?s)
927     (?V gnus-version ?s)
928     (?U gnus-tmp-unread-and-unticked ?d)
929     (?S gnus-tmp-subject ?s)
930     (?e gnus-tmp-unselected ?d)
931     (?u gnus-tmp-user-defined ?s)
932     (?d (length gnus-newsgroup-dormant) ?d)
933     (?t (length gnus-newsgroup-marked) ?d)
934     (?r (length gnus-newsgroup-reads) ?d)
935     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
936     (?E gnus-newsgroup-expunged-tally ?d)
937     (?s (gnus-current-score-file-nondirectory) ?s)))
938
939 (defvar gnus-last-search-regexp nil
940   "Default regexp for article search command.")
941
942 (defvar gnus-last-shell-command nil
943   "Default shell command on article.")
944
945 (defvar gnus-newsgroup-begin nil)
946 (defvar gnus-newsgroup-end nil)
947 (defvar gnus-newsgroup-last-rmail nil)
948 (defvar gnus-newsgroup-last-mail nil)
949 (defvar gnus-newsgroup-last-folder nil)
950 (defvar gnus-newsgroup-last-file nil)
951 (defvar gnus-newsgroup-auto-expire nil)
952 (defvar gnus-newsgroup-active nil)
953
954 (defvar gnus-newsgroup-data nil)
955 (defvar gnus-newsgroup-data-reverse nil)
956 (defvar gnus-newsgroup-limit nil)
957 (defvar gnus-newsgroup-limits nil)
958
959 (defvar gnus-newsgroup-unreads nil
960   "List of unread articles in the current newsgroup.")
961
962 (defvar gnus-newsgroup-unselected nil
963   "List of unselected unread articles in the current newsgroup.")
964
965 (defvar gnus-newsgroup-reads nil
966   "Alist of read articles and article marks in the current newsgroup.")
967
968 (defvar gnus-newsgroup-expunged-tally nil)
969
970 (defvar gnus-newsgroup-marked nil
971   "List of ticked articles in the current newsgroup (a subset of unread art).")
972
973 (defvar gnus-newsgroup-killed nil
974   "List of ranges of articles that have been through the scoring process.")
975
976 (defvar gnus-newsgroup-cached nil
977   "List of articles that come from the article cache.")
978
979 (defvar gnus-newsgroup-saved nil
980   "List of articles that have been saved.")
981
982 (defvar gnus-newsgroup-kill-headers nil)
983
984 (defvar gnus-newsgroup-replied nil
985   "List of articles that have been replied to in the current newsgroup.")
986
987 (defvar gnus-newsgroup-expirable nil
988   "List of articles in the current newsgroup that can be expired.")
989
990 (defvar gnus-newsgroup-processable nil
991   "List of articles in the current newsgroup that can be processed.")
992
993 (defvar gnus-newsgroup-downloadable nil
994   "List of articles in the current newsgroup that can be processed.")
995
996 (defvar gnus-newsgroup-undownloaded nil
997   "List of articles in the current newsgroup that haven't been downloaded..")
998
999 (defvar gnus-newsgroup-unsendable nil
1000   "List of articles in the current newsgroup that won't be sent.")
1001
1002 (defvar gnus-newsgroup-bookmarks nil
1003   "List of articles in the current newsgroup that have bookmarks.")
1004
1005 (defvar gnus-newsgroup-dormant nil
1006   "List of dormant articles in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-scored nil
1009   "List of scored articles in the current newsgroup.")
1010
1011 (defvar gnus-newsgroup-headers nil
1012   "List of article headers in the current newsgroup.")
1013
1014 (defvar gnus-newsgroup-threads nil)
1015
1016 (defvar gnus-newsgroup-prepared nil
1017   "Whether the current group has been prepared properly.")
1018
1019 (defvar gnus-newsgroup-ancient nil
1020   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1021
1022 (defvar gnus-newsgroup-sparse nil)
1023
1024 (defvar gnus-current-article nil)
1025 (defvar gnus-article-current nil)
1026 (defvar gnus-current-headers nil)
1027 (defvar gnus-have-all-headers nil)
1028 (defvar gnus-last-article nil)
1029 (defvar gnus-newsgroup-history nil)
1030 (defvar gnus-newsgroup-charset nil)
1031 (defvar gnus-newsgroup-ephemeral-charset nil)
1032 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1033
1034 (defconst gnus-summary-local-variables
1035   '(gnus-newsgroup-name
1036     gnus-newsgroup-begin gnus-newsgroup-end
1037     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1038     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1039     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1040     gnus-newsgroup-unselected gnus-newsgroup-marked
1041     gnus-newsgroup-reads gnus-newsgroup-saved
1042     gnus-newsgroup-replied gnus-newsgroup-expirable
1043     gnus-newsgroup-processable gnus-newsgroup-killed
1044     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1045     gnus-newsgroup-unsendable
1046     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1047     gnus-newsgroup-headers gnus-newsgroup-threads
1048     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1049     gnus-current-article gnus-current-headers gnus-have-all-headers
1050     gnus-last-article gnus-article-internal-prepare-hook
1051     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1052     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1053     gnus-thread-expunge-below
1054     gnus-score-alist gnus-current-score-file
1055     (gnus-summary-expunge-below . global)
1056     (gnus-summary-mark-below . global)
1057     gnus-newsgroup-active gnus-scores-exclude-files
1058     gnus-newsgroup-history gnus-newsgroup-ancient
1059     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1060     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1061     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1062     (gnus-newsgroup-expunged-tally . 0)
1063     gnus-cache-removable-articles gnus-newsgroup-cached
1064     gnus-newsgroup-data gnus-newsgroup-data-reverse
1065     gnus-newsgroup-limit gnus-newsgroup-limits
1066     gnus-newsgroup-charset)
1067   "Variables that are buffer-local to the summary buffers.")
1068
1069 ;; Byte-compiler warning.
1070 (defvar gnus-article-mode-map)
1071
1072 ;; MIME stuff.
1073
1074 (defvar gnus-decode-encoded-word-methods
1075   '(mail-decode-encoded-word-string)
1076   "List of methods used to decode encoded words.
1077
1078 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1079 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1080 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1081 whose names match REGEXP.
1082
1083 For example:
1084 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1085  mail-decode-encoded-word-string
1086  (\"chinese\" . rfc1843-decode-string))
1087 ")
1088
1089 (defvar gnus-decode-encoded-word-methods-cache nil)
1090
1091 (defun gnus-multi-decode-encoded-word-string (string)
1092   "Apply the functions from `gnus-encoded-word-methods' that match."
1093   (unless (and gnus-decode-encoded-word-methods-cache
1094                (eq gnus-newsgroup-name
1095                    (car gnus-decode-encoded-word-methods-cache)))
1096     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1097     (mapc '(lambda (x)
1098              (if (symbolp x)
1099                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1100                (if (and gnus-newsgroup-name
1101                         (string-match (car x) gnus-newsgroup-name))
1102                    (nconc gnus-decode-encoded-word-methods-cache
1103                           (list (cdr x))))))
1104           gnus-decode-encoded-word-methods))
1105   (let ((xlist gnus-decode-encoded-word-methods-cache))
1106     (pop xlist)
1107     (while xlist
1108       (setq string (funcall (pop xlist) string))))
1109   string)
1110
1111 ;; Subject simplification.
1112
1113 (defun gnus-simplify-whitespace (str)
1114   "Remove excessive whitespace."
1115   (let ((mystr str))
1116     ;; Multiple spaces.
1117     (while (string-match "[ \t][ \t]+" mystr)
1118       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1119                           " "
1120                           (substring mystr (match-end 0)))))
1121     ;; Leading spaces.
1122     (when (string-match "^[ \t]+" mystr)
1123       (setq mystr (substring mystr (match-end 0))))
1124     ;; Trailing spaces.
1125     (when (string-match "[ \t]+$" mystr)
1126       (setq mystr (substring mystr 0 (match-beginning 0))))
1127     mystr))
1128
1129 (defsubst gnus-simplify-subject-re (subject)
1130   "Remove \"Re:\" from subject lines."
1131   (if (string-match "^[Rr][Ee]: *" subject)
1132       (substring subject (match-end 0))
1133     subject))
1134
1135 (defun gnus-simplify-subject (subject &optional re-only)
1136   "Remove `Re:' and words in parentheses.
1137 If RE-ONLY is non-nil, strip leading `Re:'s only."
1138   (let ((case-fold-search t))           ;Ignore case.
1139     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1140     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1141       (setq subject (substring subject (match-end 0))))
1142     ;; Remove uninteresting prefixes.
1143     (when (and (not re-only)
1144                gnus-simplify-ignored-prefixes
1145                (string-match gnus-simplify-ignored-prefixes subject))
1146       (setq subject (substring subject (match-end 0))))
1147     ;; Remove words in parentheses from end.
1148     (unless re-only
1149       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1150         (setq subject (substring subject 0 (match-beginning 0)))))
1151     ;; Return subject string.
1152     subject))
1153
1154 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1155 ;; all whitespace.
1156 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1157   (goto-char (point-min))
1158   (while (re-search-forward regexp nil t)
1159       (replace-match (or newtext ""))))
1160
1161 (defun gnus-simplify-buffer-fuzzy ()
1162   "Simplify string in the buffer fuzzily.
1163 The string in the accessible portion of the current buffer is simplified.
1164 It is assumed to be a single-line subject.
1165 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1166 matter is removed.  Additional things can be deleted by setting
1167 gnus-simplify-subject-fuzzy-regexp."
1168   (let ((case-fold-search t)
1169         (modified-tick))
1170     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1171
1172     (while (not (eq modified-tick (buffer-modified-tick)))
1173       (setq modified-tick (buffer-modified-tick))
1174       (cond
1175        ((listp gnus-simplify-subject-fuzzy-regexp)
1176         (mapcar 'gnus-simplify-buffer-fuzzy-step
1177                 gnus-simplify-subject-fuzzy-regexp))
1178        (gnus-simplify-subject-fuzzy-regexp
1179         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1180       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1181       (gnus-simplify-buffer-fuzzy-step
1182        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1183       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1184
1185     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1186     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1187     (gnus-simplify-buffer-fuzzy-step " $")
1188     (gnus-simplify-buffer-fuzzy-step "^ +")))
1189
1190 (defun gnus-simplify-subject-fuzzy (subject)
1191   "Simplify a subject string fuzzily.
1192 See `gnus-simplify-buffer-fuzzy' for details."
1193   (save-excursion
1194     (gnus-set-work-buffer)
1195     (let ((case-fold-search t))
1196       ;; Remove uninteresting prefixes.
1197       (when (and gnus-simplify-ignored-prefixes
1198                  (string-match gnus-simplify-ignored-prefixes subject))
1199         (setq subject (substring subject (match-end 0))))
1200       (insert subject)
1201       (inline (gnus-simplify-buffer-fuzzy))
1202       (buffer-string))))
1203
1204 (defsubst gnus-simplify-subject-fully (subject)
1205   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1206   (cond
1207    (gnus-simplify-subject-functions
1208     (gnus-map-function gnus-simplify-subject-functions subject))
1209    ((null gnus-summary-gather-subject-limit)
1210     (gnus-simplify-subject-re subject))
1211    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1212     (gnus-simplify-subject-fuzzy subject))
1213    ((numberp gnus-summary-gather-subject-limit)
1214     (gnus-limit-string (gnus-simplify-subject-re subject)
1215                        gnus-summary-gather-subject-limit))
1216    (t
1217     subject)))
1218
1219 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1220   "Check whether two subjects are equal.
1221 If optional argument simple-first is t, first argument is already
1222 simplified."
1223   (cond
1224    ((null simple-first)
1225     (equal (gnus-simplify-subject-fully s1)
1226            (gnus-simplify-subject-fully s2)))
1227    (t
1228     (equal s1
1229            (gnus-simplify-subject-fully s2)))))
1230
1231 (defun gnus-summary-bubble-group ()
1232   "Increase the score of the current group.
1233 This is a handy function to add to `gnus-summary-exit-hook' to
1234 increase the score of each group you read."
1235   (gnus-group-add-score gnus-newsgroup-name))
1236
1237 \f
1238 ;;;
1239 ;;; Gnus summary mode
1240 ;;;
1241
1242 (put 'gnus-summary-mode 'mode-class 'special)
1243
1244 (when t
1245   ;; Non-orthogonal keys
1246
1247   (gnus-define-keys gnus-summary-mode-map
1248     " " gnus-summary-next-page
1249     "\177" gnus-summary-prev-page
1250     [delete] gnus-summary-prev-page
1251     [backspace] gnus-summary-prev-page
1252     "\r" gnus-summary-scroll-up
1253     "\M-\r" gnus-summary-scroll-down
1254     "n" gnus-summary-next-unread-article
1255     "p" gnus-summary-prev-unread-article
1256     "N" gnus-summary-next-article
1257     "P" gnus-summary-prev-article
1258     "\M-\C-n" gnus-summary-next-same-subject
1259     "\M-\C-p" gnus-summary-prev-same-subject
1260     "\M-n" gnus-summary-next-unread-subject
1261     "\M-p" gnus-summary-prev-unread-subject
1262     "." gnus-summary-first-unread-article
1263     "," gnus-summary-best-unread-article
1264     "\M-s" gnus-summary-search-article-forward
1265     "\M-r" gnus-summary-search-article-backward
1266     "<" gnus-summary-beginning-of-article
1267     ">" gnus-summary-end-of-article
1268     "j" gnus-summary-goto-article
1269     "^" gnus-summary-refer-parent-article
1270     "\M-^" gnus-summary-refer-article
1271     "u" gnus-summary-tick-article-forward
1272     "!" gnus-summary-tick-article-forward
1273     "U" gnus-summary-tick-article-backward
1274     "d" gnus-summary-mark-as-read-forward
1275     "D" gnus-summary-mark-as-read-backward
1276     "E" gnus-summary-mark-as-expirable
1277     "\M-u" gnus-summary-clear-mark-forward
1278     "\M-U" gnus-summary-clear-mark-backward
1279     "k" gnus-summary-kill-same-subject-and-select
1280     "\C-k" gnus-summary-kill-same-subject
1281     "\M-\C-k" gnus-summary-kill-thread
1282     "\M-\C-l" gnus-summary-lower-thread
1283     "e" gnus-summary-edit-article
1284     "#" gnus-summary-mark-as-processable
1285     "\M-#" gnus-summary-unmark-as-processable
1286     "\M-\C-t" gnus-summary-toggle-threads
1287     "\M-\C-s" gnus-summary-show-thread
1288     "\M-\C-h" gnus-summary-hide-thread
1289     "\M-\C-f" gnus-summary-next-thread
1290     "\M-\C-b" gnus-summary-prev-thread
1291     "\M-\C-u" gnus-summary-up-thread
1292     "\M-\C-d" gnus-summary-down-thread
1293     "&" gnus-summary-execute-command
1294     "c" gnus-summary-catchup-and-exit
1295     "\C-w" gnus-summary-mark-region-as-read
1296     "\C-t" gnus-summary-toggle-truncation
1297     "?" gnus-summary-mark-as-dormant
1298     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1299     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1300     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1301     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1302     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1303     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1304     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1305     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1306     "=" gnus-summary-expand-window
1307     "\C-x\C-s" gnus-summary-reselect-current-group
1308     "\M-g" gnus-summary-rescan-group
1309     "w" gnus-summary-stop-page-breaking
1310     "\C-c\C-r" gnus-summary-caesar-message
1311     "f" gnus-summary-followup
1312     "F" gnus-summary-followup-with-original
1313     "C" gnus-summary-cancel-article
1314     "r" gnus-summary-reply
1315     "R" gnus-summary-reply-with-original
1316     "\C-c\C-f" gnus-summary-mail-forward
1317     "o" gnus-summary-save-article
1318     "\C-o" gnus-summary-save-article-mail
1319     "|" gnus-summary-pipe-output
1320     "\M-k" gnus-summary-edit-local-kill
1321     "\M-K" gnus-summary-edit-global-kill
1322     ;; "V" gnus-version
1323     "\C-c\C-d" gnus-summary-describe-group
1324     "q" gnus-summary-exit
1325     "Q" gnus-summary-exit-no-update
1326     "\C-c\C-i" gnus-info-find-node
1327     gnus-mouse-2 gnus-mouse-pick-article
1328     "m" gnus-summary-mail-other-window
1329     "a" gnus-summary-post-news
1330     "x" gnus-summary-limit-to-unread
1331     "s" gnus-summary-isearch-article
1332     "t" gnus-summary-toggle-header
1333     "g" gnus-summary-show-article
1334     "l" gnus-summary-goto-last-article
1335     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1336     "\C-d" gnus-summary-enter-digest-group
1337     "\M-\C-d" gnus-summary-read-document
1338     "\M-\C-e" gnus-summary-edit-parameters
1339     "\M-\C-g" gnus-summary-customize-parameters
1340     "\C-c\C-b" gnus-bug
1341     "*" gnus-cache-enter-article
1342     "\M-*" gnus-cache-remove-article
1343     "\M-&" gnus-summary-universal-argument
1344     "\C-l" gnus-recenter
1345     "I" gnus-summary-increase-score
1346     "L" gnus-summary-lower-score
1347     "\M-i" gnus-symbolic-argument
1348     "h" gnus-summary-select-article-buffer
1349
1350     "b" gnus-article-view-part
1351     "\M-t" gnus-summary-toggle-display-buttonized
1352
1353     "V" gnus-summary-score-map
1354     "X" gnus-uu-extract-map
1355     "S" gnus-summary-send-map)
1356
1357   ;; Sort of orthogonal keymap
1358   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1359     "t" gnus-summary-tick-article-forward
1360     "!" gnus-summary-tick-article-forward
1361     "d" gnus-summary-mark-as-read-forward
1362     "r" gnus-summary-mark-as-read-forward
1363     "c" gnus-summary-clear-mark-forward
1364     " " gnus-summary-clear-mark-forward
1365     "e" gnus-summary-mark-as-expirable
1366     "x" gnus-summary-mark-as-expirable
1367     "?" gnus-summary-mark-as-dormant
1368     "b" gnus-summary-set-bookmark
1369     "B" gnus-summary-remove-bookmark
1370     "#" gnus-summary-mark-as-processable
1371     "\M-#" gnus-summary-unmark-as-processable
1372     "S" gnus-summary-limit-include-expunged
1373     "C" gnus-summary-catchup
1374     "H" gnus-summary-catchup-to-here
1375     "\C-c" gnus-summary-catchup-all
1376     "k" gnus-summary-kill-same-subject-and-select
1377     "K" gnus-summary-kill-same-subject
1378     "P" gnus-uu-mark-map)
1379
1380   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1381     "c" gnus-summary-clear-above
1382     "u" gnus-summary-tick-above
1383     "m" gnus-summary-mark-above
1384     "k" gnus-summary-kill-below)
1385
1386   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1387     "/" gnus-summary-limit-to-subject
1388     "n" gnus-summary-limit-to-articles
1389     "w" gnus-summary-pop-limit
1390     "s" gnus-summary-limit-to-subject
1391     "a" gnus-summary-limit-to-author
1392     "u" gnus-summary-limit-to-unread
1393     "m" gnus-summary-limit-to-marks
1394     "M" gnus-summary-limit-exclude-marks
1395     "v" gnus-summary-limit-to-score
1396     "*" gnus-summary-limit-include-cached
1397     "D" gnus-summary-limit-include-dormant
1398     "T" gnus-summary-limit-include-thread
1399     "d" gnus-summary-limit-exclude-dormant
1400     "t" gnus-summary-limit-to-age
1401     "x" gnus-summary-limit-to-extra 
1402     "E" gnus-summary-limit-include-expunged
1403     "c" gnus-summary-limit-exclude-childless-dormant
1404     "C" gnus-summary-limit-mark-excluded-as-read)
1405
1406   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1407     "n" gnus-summary-next-unread-article
1408     "p" gnus-summary-prev-unread-article
1409     "N" gnus-summary-next-article
1410     "P" gnus-summary-prev-article
1411     "\C-n" gnus-summary-next-same-subject
1412     "\C-p" gnus-summary-prev-same-subject
1413     "\M-n" gnus-summary-next-unread-subject
1414     "\M-p" gnus-summary-prev-unread-subject
1415     "f" gnus-summary-first-unread-article
1416     "b" gnus-summary-best-unread-article
1417     "j" gnus-summary-goto-article
1418     "g" gnus-summary-goto-subject
1419     "l" gnus-summary-goto-last-article
1420     "o" gnus-summary-pop-article)
1421
1422   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1423     "k" gnus-summary-kill-thread
1424     "l" gnus-summary-lower-thread
1425     "i" gnus-summary-raise-thread
1426     "T" gnus-summary-toggle-threads
1427     "t" gnus-summary-rethread-current
1428     "^" gnus-summary-reparent-thread
1429     "s" gnus-summary-show-thread
1430     "S" gnus-summary-show-all-threads
1431     "h" gnus-summary-hide-thread
1432     "H" gnus-summary-hide-all-threads
1433     "n" gnus-summary-next-thread
1434     "p" gnus-summary-prev-thread
1435     "u" gnus-summary-up-thread
1436     "o" gnus-summary-top-thread
1437     "d" gnus-summary-down-thread
1438     "#" gnus-uu-mark-thread
1439     "\M-#" gnus-uu-unmark-thread)
1440
1441   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1442     "g" gnus-summary-prepare
1443     "c" gnus-summary-insert-cached-articles)
1444
1445   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1446     "c" gnus-summary-catchup-and-exit
1447     "C" gnus-summary-catchup-all-and-exit
1448     "E" gnus-summary-exit-no-update
1449     "Q" gnus-summary-exit
1450     "Z" gnus-summary-exit
1451     "n" gnus-summary-catchup-and-goto-next-group
1452     "R" gnus-summary-reselect-current-group
1453     "G" gnus-summary-rescan-group
1454     "N" gnus-summary-next-group
1455     "s" gnus-summary-save-newsrc
1456     "P" gnus-summary-prev-group)
1457
1458   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1459     " " gnus-summary-next-page
1460     "n" gnus-summary-next-page
1461     "\177" gnus-summary-prev-page
1462     [delete] gnus-summary-prev-page
1463     "p" gnus-summary-prev-page
1464     "\r" gnus-summary-scroll-up
1465     "\M-\r" gnus-summary-scroll-down
1466     "<" gnus-summary-beginning-of-article
1467     ">" gnus-summary-end-of-article
1468     "b" gnus-summary-beginning-of-article
1469     "e" gnus-summary-end-of-article
1470     "^" gnus-summary-refer-parent-article
1471     "r" gnus-summary-refer-parent-article
1472     "D" gnus-summary-enter-digest-group
1473     "R" gnus-summary-refer-references
1474     "T" gnus-summary-refer-thread
1475     "g" gnus-summary-show-article
1476     "s" gnus-summary-isearch-article
1477     "P" gnus-summary-print-article
1478     "t" gnus-article-babel)
1479
1480   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1481     "b" gnus-article-add-buttons
1482     "B" gnus-article-add-buttons-to-head
1483     "o" gnus-article-treat-overstrike
1484     "e" gnus-article-emphasize
1485     "w" gnus-article-fill-cited-article
1486     "Q" gnus-article-fill-long-lines
1487     "C" gnus-article-capitalize-sentences
1488     "c" gnus-article-remove-cr
1489     "q" gnus-article-de-quoted-unreadable
1490     "f" gnus-article-display-x-face
1491     "l" gnus-summary-stop-page-breaking
1492     "r" gnus-summary-caesar-message
1493     "t" gnus-article-hide-headers
1494     "v" gnus-summary-verbose-headers
1495     "H" gnus-article-strip-headers-in-body
1496     "d" gnus-article-treat-dumbquotes)
1497
1498   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1499     "a" gnus-article-hide
1500     "h" gnus-article-hide-headers
1501     "b" gnus-article-hide-boring-headers
1502     "s" gnus-article-hide-signature
1503     "c" gnus-article-hide-citation
1504     "C" gnus-article-hide-citation-in-followups
1505     "p" gnus-article-hide-pgp
1506     "B" gnus-article-strip-banner
1507     "P" gnus-article-hide-pem
1508     "\C-c" gnus-article-hide-citation-maybe)
1509
1510   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1511     "a" gnus-article-highlight
1512     "h" gnus-article-highlight-headers
1513     "c" gnus-article-highlight-citation
1514     "s" gnus-article-highlight-signature)
1515
1516   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1517     "w" gnus-article-decode-mime-words
1518     "c" gnus-article-decode-charset
1519     "v" gnus-mime-view-all-parts
1520     "b" gnus-article-view-part)
1521
1522   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1523     "z" gnus-article-date-ut
1524     "u" gnus-article-date-ut
1525     "l" gnus-article-date-local
1526     "e" gnus-article-date-lapsed
1527     "o" gnus-article-date-original
1528     "i" gnus-article-date-iso8601
1529     "s" gnus-article-date-user)
1530
1531   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1532     "t" gnus-article-remove-trailing-blank-lines
1533     "l" gnus-article-strip-leading-blank-lines
1534     "m" gnus-article-strip-multiple-blank-lines
1535     "a" gnus-article-strip-blank-lines
1536     "A" gnus-article-strip-all-blank-lines
1537     "s" gnus-article-strip-leading-space
1538     "e" gnus-article-strip-trailing-space)
1539
1540   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1541     "v" gnus-version
1542     "f" gnus-summary-fetch-faq
1543     "d" gnus-summary-describe-group
1544     "h" gnus-summary-describe-briefly
1545     "i" gnus-info-find-node)
1546
1547   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1548     "e" gnus-summary-expire-articles
1549     "\M-\C-e" gnus-summary-expire-articles-now
1550     "\177" gnus-summary-delete-article
1551     [delete] gnus-summary-delete-article
1552     [backspace] gnus-summary-delete-article
1553     "m" gnus-summary-move-article
1554     "r" gnus-summary-respool-article
1555     "w" gnus-summary-edit-article
1556     "c" gnus-summary-copy-article
1557     "B" gnus-summary-crosspost-article
1558     "q" gnus-summary-respool-query
1559     "t" gnus-summary-respool-trace
1560     "i" gnus-summary-import-article
1561     "p" gnus-summary-article-posted-p)
1562
1563   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1564     "o" gnus-summary-save-article
1565     "m" gnus-summary-save-article-mail
1566     "F" gnus-summary-write-article-file
1567     "r" gnus-summary-save-article-rmail
1568     "f" gnus-summary-save-article-file
1569     "b" gnus-summary-save-article-body-file
1570     "h" gnus-summary-save-article-folder
1571     "v" gnus-summary-save-article-vm
1572     "p" gnus-summary-pipe-output
1573     "s" gnus-soup-add-article)
1574
1575   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1576     "b" gnus-summary-display-buttonized
1577     "m" gnus-summary-repair-multipart
1578     "v" gnus-article-view-part
1579     "o" gnus-article-save-part
1580     "c" gnus-article-copy-part
1581     "e" gnus-article-externalize-part
1582     "i" gnus-article-inline-part
1583     "|" gnus-article-pipe-part)
1584   )
1585
1586 (defun gnus-summary-make-menu-bar ()
1587   (gnus-turn-off-edit-menu 'summary)
1588
1589   (unless (boundp 'gnus-summary-misc-menu)
1590
1591     (easy-menu-define
1592      gnus-summary-kill-menu gnus-summary-mode-map ""
1593      (cons
1594       "Score"
1595       (nconc
1596        (list
1597         ["Enter score..." gnus-summary-score-entry t]
1598         ["Customize" gnus-score-customize t])
1599        (gnus-make-score-map 'increase)
1600        (gnus-make-score-map 'lower)
1601        '(("Mark"
1602           ["Kill below" gnus-summary-kill-below t]
1603           ["Mark above" gnus-summary-mark-above t]
1604           ["Tick above" gnus-summary-tick-above t]
1605           ["Clear above" gnus-summary-clear-above t])
1606          ["Current score" gnus-summary-current-score t]
1607          ["Set score" gnus-summary-set-score t]
1608          ["Switch current score file..." gnus-score-change-score-file t]
1609          ["Set mark below..." gnus-score-set-mark-below t]
1610          ["Set expunge below..." gnus-score-set-expunge-below t]
1611          ["Edit current score file" gnus-score-edit-current-scores t]
1612          ["Edit score file" gnus-score-edit-file t]
1613          ["Trace score" gnus-score-find-trace t]
1614          ["Find words" gnus-score-find-favourite-words t]
1615          ["Rescore buffer" gnus-summary-rescore t]
1616          ["Increase score..." gnus-summary-increase-score t]
1617          ["Lower score..." gnus-summary-lower-score t]))))
1618
1619     ;; Define both the Article menu in the summary buffer and the equivalent
1620     ;; Commands menu in the article buffer here for consistency.
1621     (let ((innards
1622            '(("Hide"
1623               ["All" gnus-article-hide t]
1624               ["Headers" gnus-article-hide-headers t]
1625               ["Signature" gnus-article-hide-signature t]
1626               ["Citation" gnus-article-hide-citation t]
1627               ["PGP" gnus-article-hide-pgp t]
1628               ["Banner" gnus-article-strip-banner t]
1629               ["Boring headers" gnus-article-hide-boring-headers t])
1630              ("Highlight"
1631               ["All" gnus-article-highlight t]
1632               ["Headers" gnus-article-highlight-headers t]
1633               ["Signature" gnus-article-highlight-signature t]
1634               ["Citation" gnus-article-highlight-citation t])
1635              ("MIME"
1636               ["Words" gnus-article-decode-mime-words t]
1637               ["Charset" gnus-article-decode-charset t]
1638               ["QP" gnus-article-de-quoted-unreadable t]
1639               ["View all" gnus-mime-view-all-parts t])
1640              ("Date"
1641               ["Local" gnus-article-date-local t]
1642               ["ISO8601" gnus-article-date-iso8601 t]
1643               ["UT" gnus-article-date-ut t]
1644               ["Original" gnus-article-date-original t]
1645               ["Lapsed" gnus-article-date-lapsed t]
1646               ["User-defined" gnus-article-date-user t])
1647              ("Washing"
1648               ("Remove Blanks"
1649                ["Leading" gnus-article-strip-leading-blank-lines t]
1650                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1651                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1652                ["All of the above" gnus-article-strip-blank-lines t]
1653                ["All" gnus-article-strip-all-blank-lines t]
1654                ["Leading space" gnus-article-strip-leading-space t]
1655                ["Trailing space" gnus-article-strip-trailing-space t])
1656               ["Overstrike" gnus-article-treat-overstrike t]
1657               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1658               ["Emphasis" gnus-article-emphasize t]
1659               ["Word wrap" gnus-article-fill-cited-article t]
1660               ["Fill long lines" gnus-article-fill-long-lines t]
1661               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1662               ["CR" gnus-article-remove-cr t]
1663               ["Show X-Face" gnus-article-display-x-face t]
1664               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1665               ["Rot 13" gnus-summary-caesar-message t]
1666               ["Unix pipe" gnus-summary-pipe-message t]
1667               ["Add buttons" gnus-article-add-buttons t]
1668               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1669               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1670               ["Verbose header" gnus-summary-verbose-headers t]
1671               ["Toggle header" gnus-summary-toggle-header t])
1672              ("Output"
1673               ["Save in default format" gnus-summary-save-article t]
1674               ["Save in file" gnus-summary-save-article-file t]
1675               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1676               ["Save in MH folder" gnus-summary-save-article-folder t]
1677               ["Save in VM folder" gnus-summary-save-article-vm t]
1678               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1679               ["Save body in file" gnus-summary-save-article-body-file t]
1680               ["Pipe through a filter" gnus-summary-pipe-output t]
1681               ["Add to SOUP packet" gnus-soup-add-article t]
1682               ["Print" gnus-summary-print-article t])
1683              ("Backend"
1684               ["Respool article..." gnus-summary-respool-article t]
1685               ["Move article..." gnus-summary-move-article
1686                (gnus-check-backend-function
1687                 'request-move-article gnus-newsgroup-name)]
1688               ["Copy article..." gnus-summary-copy-article t]
1689               ["Crosspost article..." gnus-summary-crosspost-article
1690                (gnus-check-backend-function
1691                 'request-replace-article gnus-newsgroup-name)]
1692               ["Import file..." gnus-summary-import-article t]
1693               ["Check if posted" gnus-summary-article-posted-p t]
1694               ["Edit article" gnus-summary-edit-article
1695                (not (gnus-group-read-only-p))]
1696               ["Delete article" gnus-summary-delete-article
1697                (gnus-check-backend-function
1698                 'request-expire-articles gnus-newsgroup-name)]
1699               ["Query respool" gnus-summary-respool-query t]
1700               ["Trace respool" gnus-summary-respool-trace t]
1701               ["Delete expirable articles" gnus-summary-expire-articles-now
1702                (gnus-check-backend-function
1703                 'request-expire-articles gnus-newsgroup-name)])
1704              ("Extract"
1705               ["Uudecode" gnus-uu-decode-uu t]
1706               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1707               ["Unshar" gnus-uu-decode-unshar t]
1708               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1709               ["Save" gnus-uu-decode-save t]
1710               ["Binhex" gnus-uu-decode-binhex t]
1711               ["Postscript" gnus-uu-decode-postscript t])
1712              ("Cache"
1713               ["Enter article" gnus-cache-enter-article t]
1714               ["Remove article" gnus-cache-remove-article t])
1715              ["Translate" gnus-article-babel t]
1716              ["Select article buffer" gnus-summary-select-article-buffer t]
1717              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1718              ["Isearch article..." gnus-summary-isearch-article t]
1719              ["Beginning of the article" gnus-summary-beginning-of-article t]
1720              ["End of the article" gnus-summary-end-of-article t]
1721              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1722              ["Fetch referenced articles" gnus-summary-refer-references t]
1723              ["Fetch current thread" gnus-summary-refer-thread t]
1724              ["Fetch article with id..." gnus-summary-refer-article t]
1725              ["Redisplay" gnus-summary-show-article t])))
1726       (easy-menu-define
1727        gnus-summary-article-menu gnus-summary-mode-map ""
1728        (cons "Article" innards))
1729
1730       (easy-menu-define
1731        gnus-article-commands-menu gnus-article-mode-map ""
1732        (cons "Commands" innards)))
1733
1734     (easy-menu-define
1735      gnus-summary-thread-menu gnus-summary-mode-map ""
1736      '("Threads"
1737        ["Toggle threading" gnus-summary-toggle-threads t]
1738        ["Hide threads" gnus-summary-hide-all-threads t]
1739        ["Show threads" gnus-summary-show-all-threads t]
1740        ["Hide thread" gnus-summary-hide-thread t]
1741        ["Show thread" gnus-summary-show-thread t]
1742        ["Go to next thread" gnus-summary-next-thread t]
1743        ["Go to previous thread" gnus-summary-prev-thread t]
1744        ["Go down thread" gnus-summary-down-thread t]
1745        ["Go up thread" gnus-summary-up-thread t]
1746        ["Top of thread" gnus-summary-top-thread t]
1747        ["Mark thread as read" gnus-summary-kill-thread t]
1748        ["Lower thread score" gnus-summary-lower-thread t]
1749        ["Raise thread score" gnus-summary-raise-thread t]
1750        ["Rethread current" gnus-summary-rethread-current t]
1751        ))
1752
1753     (easy-menu-define
1754      gnus-summary-post-menu gnus-summary-mode-map ""
1755      '("Post"
1756        ["Post an article" gnus-summary-post-news t]
1757        ["Followup" gnus-summary-followup t]
1758        ["Followup and yank" gnus-summary-followup-with-original t]
1759        ["Supersede article" gnus-summary-supersede-article t]
1760        ["Cancel article" gnus-summary-cancel-article t]
1761        ["Reply" gnus-summary-reply t]
1762        ["Reply and yank" gnus-summary-reply-with-original t]
1763        ["Wide reply" gnus-summary-wide-reply t]
1764        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1765        ["Mail forward" gnus-summary-mail-forward t]
1766        ["Post forward" gnus-summary-post-forward t]
1767        ["Digest and mail" gnus-uu-digest-mail-forward t]
1768        ["Digest and post" gnus-uu-digest-post-forward t]
1769        ["Resend message" gnus-summary-resend-message t]
1770        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1771        ["Send a mail" gnus-summary-mail-other-window t]
1772        ["Uuencode and post" gnus-uu-post-news t]
1773        ["Followup via news" gnus-summary-followup-to-mail t]
1774        ["Followup via news and yank"
1775         gnus-summary-followup-to-mail-with-original t]
1776        ;;("Draft"
1777        ;;["Send" gnus-summary-send-draft t]
1778        ;;["Send bounced" gnus-resend-bounced-mail t])
1779        ))
1780
1781     (easy-menu-define
1782      gnus-summary-misc-menu gnus-summary-mode-map ""
1783      '("Misc"
1784        ("Mark Read"
1785         ["Mark as read" gnus-summary-mark-as-read-forward t]
1786         ["Mark same subject and select"
1787          gnus-summary-kill-same-subject-and-select t]
1788         ["Mark same subject" gnus-summary-kill-same-subject t]
1789         ["Catchup" gnus-summary-catchup t]
1790         ["Catchup all" gnus-summary-catchup-all t]
1791         ["Catchup to here" gnus-summary-catchup-to-here t]
1792         ["Catchup region" gnus-summary-mark-region-as-read t]
1793         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1794        ("Mark Various"
1795         ["Tick" gnus-summary-tick-article-forward t]
1796         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1797         ["Remove marks" gnus-summary-clear-mark-forward t]
1798         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1799         ["Set bookmark" gnus-summary-set-bookmark t]
1800         ["Remove bookmark" gnus-summary-remove-bookmark t])
1801        ("Mark Limit"
1802         ["Marks..." gnus-summary-limit-to-marks t]
1803         ["Subject..." gnus-summary-limit-to-subject t]
1804         ["Author..." gnus-summary-limit-to-author t]
1805         ["Age..." gnus-summary-limit-to-age t]
1806         ["Extra..." gnus-summary-limit-to-extra t]
1807         ["Score" gnus-summary-limit-to-score t]
1808         ["Unread" gnus-summary-limit-to-unread t]
1809         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1810         ["Articles" gnus-summary-limit-to-articles t]
1811         ["Pop limit" gnus-summary-pop-limit t]
1812         ["Show dormant" gnus-summary-limit-include-dormant t]
1813         ["Hide childless dormant"
1814          gnus-summary-limit-exclude-childless-dormant t]
1815         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1816         ["Hide marked" gnus-summary-limit-exclude-marks t]
1817         ["Show expunged" gnus-summary-show-all-expunged t])
1818        ("Process Mark"
1819         ["Set mark" gnus-summary-mark-as-processable t]
1820         ["Remove mark" gnus-summary-unmark-as-processable t]
1821         ["Remove all marks" gnus-summary-unmark-all-processable t]
1822         ["Mark above" gnus-uu-mark-over t]
1823         ["Mark series" gnus-uu-mark-series t]
1824         ["Mark region" gnus-uu-mark-region t]
1825         ["Unmark region" gnus-uu-unmark-region t]
1826         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1827         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1828         ["Mark all" gnus-uu-mark-all t]
1829         ["Mark buffer" gnus-uu-mark-buffer t]
1830         ["Mark sparse" gnus-uu-mark-sparse t]
1831         ["Mark thread" gnus-uu-mark-thread t]
1832         ["Unmark thread" gnus-uu-unmark-thread t]
1833         ("Process Mark Sets"
1834          ["Kill" gnus-summary-kill-process-mark t]
1835          ["Yank" gnus-summary-yank-process-mark
1836           gnus-newsgroup-process-stack]
1837          ["Save" gnus-summary-save-process-mark t]))
1838        ("Scroll article"
1839         ["Page forward" gnus-summary-next-page t]
1840         ["Page backward" gnus-summary-prev-page t]
1841         ["Line forward" gnus-summary-scroll-up t])
1842        ("Move"
1843         ["Next unread article" gnus-summary-next-unread-article t]
1844         ["Previous unread article" gnus-summary-prev-unread-article t]
1845         ["Next article" gnus-summary-next-article t]
1846         ["Previous article" gnus-summary-prev-article t]
1847         ["Next unread subject" gnus-summary-next-unread-subject t]
1848         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1849         ["Next article same subject" gnus-summary-next-same-subject t]
1850         ["Previous article same subject" gnus-summary-prev-same-subject t]
1851         ["First unread article" gnus-summary-first-unread-article t]
1852         ["Best unread article" gnus-summary-best-unread-article t]
1853         ["Go to subject number..." gnus-summary-goto-subject t]
1854         ["Go to article number..." gnus-summary-goto-article t]
1855         ["Go to the last article" gnus-summary-goto-last-article t]
1856         ["Pop article off history" gnus-summary-pop-article t])
1857        ("Sort"
1858         ["Sort by number" gnus-summary-sort-by-number t]
1859         ["Sort by author" gnus-summary-sort-by-author t]
1860         ["Sort by subject" gnus-summary-sort-by-subject t]
1861         ["Sort by date" gnus-summary-sort-by-date t]
1862         ["Sort by score" gnus-summary-sort-by-score t]
1863         ["Sort by lines" gnus-summary-sort-by-lines t]
1864         ["Sort by characters" gnus-summary-sort-by-chars t])
1865        ("Help"
1866         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1867         ["Describe group" gnus-summary-describe-group t]
1868         ["Read manual" gnus-info-find-node t])
1869        ("Modes"
1870         ["Pick and read" gnus-pick-mode t]
1871         ["Binary" gnus-binary-mode t])
1872        ("Regeneration"
1873         ["Regenerate" gnus-summary-prepare t]
1874         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1875         ["Toggle threading" gnus-summary-toggle-threads t])
1876        ["Filter articles..." gnus-summary-execute-command t]
1877        ["Run command on subjects..." gnus-summary-universal-argument t]
1878        ["Search articles forward..." gnus-summary-search-article-forward t]
1879        ["Search articles backward..." gnus-summary-search-article-backward t]
1880        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1881        ["Expand window" gnus-summary-expand-window t]
1882        ["Expire expirable articles" gnus-summary-expire-articles
1883         (gnus-check-backend-function
1884          'request-expire-articles gnus-newsgroup-name)]
1885        ["Edit local kill file" gnus-summary-edit-local-kill t]
1886        ["Edit main kill file" gnus-summary-edit-global-kill t]
1887        ["Edit group parameters" gnus-summary-edit-parameters t]
1888        ["Customize group parameters" gnus-summary-customize-parameters t]
1889        ["Send a bug report" gnus-bug t]
1890        ("Exit"
1891         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1892         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1893         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1894         ["Exit group" gnus-summary-exit t]
1895         ["Exit group without updating" gnus-summary-exit-no-update t]
1896         ["Exit and goto next group" gnus-summary-next-group t]
1897         ["Exit and goto prev group" gnus-summary-prev-group t]
1898         ["Reselect group" gnus-summary-reselect-current-group t]
1899         ["Rescan group" gnus-summary-rescan-group t]
1900         ["Update dribble" gnus-summary-save-newsrc t])))
1901
1902     (gnus-run-hooks 'gnus-summary-menu-hook)))
1903
1904 (defun gnus-score-set-default (var value)
1905   "A version of set that updates the GNU Emacs menu-bar."
1906   (set var value)
1907   ;; It is the message that forces the active status to be updated.
1908   (message ""))
1909
1910 (defun gnus-make-score-map (type)
1911   "Make a summary score map of type TYPE."
1912   (if t
1913       nil
1914     (let ((headers '(("author" "from" string)
1915                      ("subject" "subject" string)
1916                      ("article body" "body" string)
1917                      ("article head" "head" string)
1918                      ("xref" "xref" string)
1919                      ("extra header" "extra" string)
1920                      ("lines" "lines" number)
1921                      ("followups to author" "followup" string)))
1922           (types '((number ("less than" <)
1923                            ("greater than" >)
1924                            ("equal" =))
1925                    (string ("substring" s)
1926                            ("exact string" e)
1927                            ("fuzzy string" f)
1928                            ("regexp" r))))
1929           (perms '(("temporary" (current-time-string))
1930                    ("permanent" nil)
1931                    ("immediate" now)))
1932           header)
1933       (list
1934        (apply
1935         'nconc
1936         (list
1937          (if (eq type 'lower)
1938              "Lower score"
1939            "Increase score"))
1940         (let (outh)
1941           (while headers
1942             (setq header (car headers))
1943             (setq outh
1944                   (cons
1945                    (apply
1946                     'nconc
1947                     (list (car header))
1948                     (let ((ts (cdr (assoc (nth 2 header) types)))
1949                           outt)
1950                       (while ts
1951                         (setq outt
1952                               (cons
1953                                (apply
1954                                 'nconc
1955                                 (list (caar ts))
1956                                 (let ((ps perms)
1957                                       outp)
1958                                   (while ps
1959                                     (setq outp
1960                                           (cons
1961                                            (vector
1962                                             (caar ps)
1963                                             (list
1964                                              'gnus-summary-score-entry
1965                                              (nth 1 header)
1966                                              (if (or (string= (nth 1 header)
1967                                                               "head")
1968                                                      (string= (nth 1 header)
1969                                                               "body"))
1970                                                  ""
1971                                                (list 'gnus-summary-header
1972                                                      (nth 1 header)))
1973                                              (list 'quote (nth 1 (car ts)))
1974                                              (list 'gnus-score-default nil)
1975                                              (nth 1 (car ps))
1976                                              t)
1977                                             t)
1978                                            outp))
1979                                     (setq ps (cdr ps)))
1980                                   (list (nreverse outp))))
1981                                outt))
1982                         (setq ts (cdr ts)))
1983                       (list (nreverse outt))))
1984                    outh))
1985             (setq headers (cdr headers)))
1986           (list (nreverse outh))))))))
1987
1988 \f
1989
1990 (defun gnus-summary-mode (&optional group)
1991   "Major mode for reading articles.
1992
1993 All normal editing commands are switched off.
1994 \\<gnus-summary-mode-map>
1995 Each line in this buffer represents one article.  To read an
1996 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1997 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1998 respectively.
1999
2000 You can also post articles and send mail from this buffer.  To
2001 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2002 of an article, type `\\[gnus-summary-reply]'.
2003
2004 There are approx. one gazillion commands you can execute in this
2005 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2006
2007 The following commands are available:
2008
2009 \\{gnus-summary-mode-map}"
2010   (interactive)
2011   (when (gnus-visual-p 'summary-menu 'menu)
2012     (gnus-summary-make-menu-bar))
2013   (kill-all-local-variables)
2014   (gnus-summary-make-local-variables)
2015   (gnus-make-thread-indent-array)
2016   (gnus-simplify-mode-line)
2017   (setq major-mode 'gnus-summary-mode)
2018   (setq mode-name "Summary")
2019   (make-local-variable 'minor-mode-alist)
2020   (use-local-map gnus-summary-mode-map)
2021   (buffer-disable-undo)
2022   (setq buffer-read-only t)             ;Disable modification
2023   (setq truncate-lines t)
2024   (setq selective-display t)
2025   (setq selective-display-ellipses t)   ;Display `...'
2026   (gnus-summary-set-display-table)
2027   (gnus-set-default-directory)
2028   (setq gnus-newsgroup-name group)
2029   (make-local-variable 'gnus-summary-line-format)
2030   (make-local-variable 'gnus-summary-line-format-spec)
2031   (make-local-variable 'gnus-summary-dummy-line-format)
2032   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2033   (make-local-variable 'gnus-summary-mark-positions)
2034   (make-local-hook 'pre-command-hook)
2035   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2036   (gnus-run-hooks 'gnus-summary-mode-hook)
2037   (mm-enable-multibyte)
2038   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2039   (gnus-update-summary-mark-positions))
2040
2041 (defun gnus-summary-make-local-variables ()
2042   "Make all the local summary buffer variables."
2043   (let (global)
2044     (dolist (local gnus-summary-local-variables)
2045       (if (consp local)
2046           (progn
2047             (if (eq (cdr local) 'global)
2048                 ;; Copy the global value of the variable.
2049                 (setq global (symbol-value (car local)))
2050               ;; Use the value from the list.
2051               (setq global (eval (cdr local))))
2052             (set (make-local-variable (car local)) global))
2053         ;; Simple nil-valued local variable.
2054         (set (make-local-variable local) nil)))))
2055
2056 (defun gnus-summary-clear-local-variables ()
2057   (let ((locals gnus-summary-local-variables))
2058     (while locals
2059       (if (consp (car locals))
2060           (and (vectorp (caar locals))
2061                (set (caar locals) nil))
2062         (and (vectorp (car locals))
2063              (set (car locals) nil)))
2064       (setq locals (cdr locals)))))
2065
2066 ;; Summary data functions.
2067
2068 (defmacro gnus-data-number (data)
2069   `(car ,data))
2070
2071 (defmacro gnus-data-set-number (data number)
2072   `(setcar ,data ,number))
2073
2074 (defmacro gnus-data-mark (data)
2075   `(nth 1 ,data))
2076
2077 (defmacro gnus-data-set-mark (data mark)
2078   `(setcar (nthcdr 1 ,data) ,mark))
2079
2080 (defmacro gnus-data-pos (data)
2081   `(nth 2 ,data))
2082
2083 (defmacro gnus-data-set-pos (data pos)
2084   `(setcar (nthcdr 2 ,data) ,pos))
2085
2086 (defmacro gnus-data-header (data)
2087   `(nth 3 ,data))
2088
2089 (defmacro gnus-data-set-header (data header)
2090   `(setf (nth 3 ,data) ,header))
2091
2092 (defmacro gnus-data-level (data)
2093   `(nth 4 ,data))
2094
2095 (defmacro gnus-data-unread-p (data)
2096   `(= (nth 1 ,data) gnus-unread-mark))
2097
2098 (defmacro gnus-data-read-p (data)
2099   `(/= (nth 1 ,data) gnus-unread-mark))
2100
2101 (defmacro gnus-data-pseudo-p (data)
2102   `(consp (nth 3 ,data)))
2103
2104 (defmacro gnus-data-find (number)
2105   `(assq ,number gnus-newsgroup-data))
2106
2107 (defmacro gnus-data-find-list (number &optional data)
2108   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2109      (memq (assq ,number bdata)
2110            bdata)))
2111
2112 (defmacro gnus-data-make (number mark pos header level)
2113   `(list ,number ,mark ,pos ,header ,level))
2114
2115 (defun gnus-data-enter (after-article number mark pos header level offset)
2116   (let ((data (gnus-data-find-list after-article)))
2117     (unless data
2118       (error "No such article: %d" after-article))
2119     (setcdr data (cons (gnus-data-make number mark pos header level)
2120                        (cdr data)))
2121     (setq gnus-newsgroup-data-reverse nil)
2122     (gnus-data-update-list (cddr data) offset)))
2123
2124 (defun gnus-data-enter-list (after-article list &optional offset)
2125   (when list
2126     (let ((data (and after-article (gnus-data-find-list after-article)))
2127           (ilist list))
2128       (if (not (or data
2129                    after-article))
2130           (let ((odata gnus-newsgroup-data))
2131             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2132             (when offset
2133               (gnus-data-update-list odata offset)))
2134         ;; Find the last element in the list to be spliced into the main
2135         ;; list.
2136         (while (cdr list)
2137           (setq list (cdr list)))
2138         (if (not data)
2139             (progn
2140               (setcdr list gnus-newsgroup-data)
2141               (setq gnus-newsgroup-data ilist)
2142               (when offset
2143                 (gnus-data-update-list (cdr list) offset)))
2144           (setcdr list (cdr data))
2145           (setcdr data ilist)
2146           (when offset
2147             (gnus-data-update-list (cdr list) offset))))
2148       (setq gnus-newsgroup-data-reverse nil))))
2149
2150 (defun gnus-data-remove (article &optional offset)
2151   (let ((data gnus-newsgroup-data))
2152     (if (= (gnus-data-number (car data)) article)
2153         (progn
2154           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2155                 gnus-newsgroup-data-reverse nil)
2156           (when offset
2157             (gnus-data-update-list gnus-newsgroup-data offset)))
2158       (while (cdr data)
2159         (when (= (gnus-data-number (cadr data)) article)
2160           (setcdr data (cddr data))
2161           (when offset
2162             (gnus-data-update-list (cdr data) offset))
2163           (setq data nil
2164                 gnus-newsgroup-data-reverse nil))
2165         (setq data (cdr data))))))
2166
2167 (defmacro gnus-data-list (backward)
2168   `(if ,backward
2169        (or gnus-newsgroup-data-reverse
2170            (setq gnus-newsgroup-data-reverse
2171                  (reverse gnus-newsgroup-data)))
2172      gnus-newsgroup-data))
2173
2174 (defun gnus-data-update-list (data offset)
2175   "Add OFFSET to the POS of all data entries in DATA."
2176   (setq gnus-newsgroup-data-reverse nil)
2177   (while data
2178     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2179     (setq data (cdr data))))
2180
2181 (defun gnus-summary-article-pseudo-p (article)
2182   "Say whether this article is a pseudo article or not."
2183   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2184
2185 (defmacro gnus-summary-article-sparse-p (article)
2186   "Say whether this article is a sparse article or not."
2187   `(memq ,article gnus-newsgroup-sparse))
2188
2189 (defmacro gnus-summary-article-ancient-p (article)
2190   "Say whether this article is a sparse article or not."
2191   `(memq ,article gnus-newsgroup-ancient))
2192
2193 (defun gnus-article-parent-p (number)
2194   "Say whether this article is a parent or not."
2195   (let ((data (gnus-data-find-list number)))
2196     (and (cdr data)                     ; There has to be an article after...
2197          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2198             (gnus-data-level (nth 1 data))))))
2199
2200 (defun gnus-article-children (number)
2201   "Return a list of all children to NUMBER."
2202   (let* ((data (gnus-data-find-list number))
2203          (level (gnus-data-level (car data)))
2204          children)
2205     (setq data (cdr data))
2206     (while (and data
2207                 (= (gnus-data-level (car data)) (1+ level)))
2208       (push (gnus-data-number (car data)) children)
2209       (setq data (cdr data)))
2210     children))
2211
2212 (defmacro gnus-summary-skip-intangible ()
2213   "If the current article is intangible, then jump to a different article."
2214   '(let ((to (get-text-property (point) 'gnus-intangible)))
2215      (and to (gnus-summary-goto-subject to))))
2216
2217 (defmacro gnus-summary-article-intangible-p ()
2218   "Say whether this article is intangible or not."
2219   '(get-text-property (point) 'gnus-intangible))
2220
2221 (defun gnus-article-read-p (article)
2222   "Say whether ARTICLE is read or not."
2223   (not (or (memq article gnus-newsgroup-marked)
2224            (memq article gnus-newsgroup-unreads)
2225            (memq article gnus-newsgroup-unselected)
2226            (memq article gnus-newsgroup-dormant))))
2227
2228 ;; Some summary mode macros.
2229
2230 (defmacro gnus-summary-article-number ()
2231   "The article number of the article on the current line.
2232 If there isn's an article number here, then we return the current
2233 article number."
2234   '(progn
2235      (gnus-summary-skip-intangible)
2236      (or (get-text-property (point) 'gnus-number)
2237          (gnus-summary-last-subject))))
2238
2239 (defmacro gnus-summary-article-header (&optional number)
2240   "Return the header of article NUMBER."
2241   `(gnus-data-header (gnus-data-find
2242                       ,(or number '(gnus-summary-article-number)))))
2243
2244 (defmacro gnus-summary-thread-level (&optional number)
2245   "Return the level of thread that starts with article NUMBER."
2246   `(if (and (eq gnus-summary-make-false-root 'dummy)
2247             (get-text-property (point) 'gnus-intangible))
2248        0
2249      (gnus-data-level (gnus-data-find
2250                        ,(or number '(gnus-summary-article-number))))))
2251
2252 (defmacro gnus-summary-article-mark (&optional number)
2253   "Return the mark of article NUMBER."
2254   `(gnus-data-mark (gnus-data-find
2255                     ,(or number '(gnus-summary-article-number)))))
2256
2257 (defmacro gnus-summary-article-pos (&optional number)
2258   "Return the position of the line of article NUMBER."
2259   `(gnus-data-pos (gnus-data-find
2260                    ,(or number '(gnus-summary-article-number)))))
2261
2262 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2263 (defmacro gnus-summary-article-subject (&optional number)
2264   "Return current subject string or nil if nothing."
2265   `(let ((headers
2266           ,(if number
2267                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2268              '(gnus-data-header (assq (gnus-summary-article-number)
2269                                       gnus-newsgroup-data)))))
2270      (and headers
2271           (vectorp headers)
2272           (mail-header-subject headers))))
2273
2274 (defmacro gnus-summary-article-score (&optional number)
2275   "Return current article score."
2276   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2277                   gnus-newsgroup-scored))
2278        gnus-summary-default-score 0))
2279
2280 (defun gnus-summary-article-children (&optional number)
2281   "Return a list of article numbers that are children of article NUMBER."
2282   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2283          (level (gnus-data-level (car data)))
2284          l children)
2285     (while (and (setq data (cdr data))
2286                 (> (setq l (gnus-data-level (car data))) level))
2287       (and (= (1+ level) l)
2288            (push (gnus-data-number (car data))
2289                  children)))
2290     (nreverse children)))
2291
2292 (defun gnus-summary-article-parent (&optional number)
2293   "Return the article number of the parent of article NUMBER."
2294   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2295                                     (gnus-data-list t)))
2296          (level (gnus-data-level (car data))))
2297     (if (zerop level)
2298         ()                              ; This is a root.
2299       ;; We search until we find an article with a level less than
2300       ;; this one.  That function has to be the parent.
2301       (while (and (setq data (cdr data))
2302                   (not (< (gnus-data-level (car data)) level))))
2303       (and data (gnus-data-number (car data))))))
2304
2305 (defun gnus-unread-mark-p (mark)
2306   "Say whether MARK is the unread mark."
2307   (= mark gnus-unread-mark))
2308
2309 (defun gnus-read-mark-p (mark)
2310   "Say whether MARK is one of the marks that mark as read.
2311 This is all marks except unread, ticked, dormant, and expirable."
2312   (not (or (= mark gnus-unread-mark)
2313            (= mark gnus-ticked-mark)
2314            (= mark gnus-dormant-mark)
2315            (= mark gnus-expirable-mark))))
2316
2317 (defmacro gnus-article-mark (number)
2318   "Return the MARK of article NUMBER.
2319 This macro should only be used when computing the mark the \"first\"
2320 time; i.e., when generating the summary lines.  After that,
2321 `gnus-summary-article-mark' should be used to examine the
2322 marks of articles."
2323   `(cond
2324     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2325     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2326     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2327     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2328     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2329     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2330     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2331     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2332            gnus-ancient-mark))))
2333
2334 ;; Saving hidden threads.
2335
2336 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2337 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2338
2339 (defmacro gnus-save-hidden-threads (&rest forms)
2340   "Save hidden threads, eval FORMS, and restore the hidden threads."
2341   (let ((config (make-symbol "config")))
2342     `(let ((,config (gnus-hidden-threads-configuration)))
2343        (unwind-protect
2344            (save-excursion
2345              ,@forms)
2346          (gnus-restore-hidden-threads-configuration ,config)))))
2347
2348 (defun gnus-data-compute-positions ()
2349   "Compute the positions of all articles."
2350   (setq gnus-newsgroup-data-reverse nil)
2351   (let ((data gnus-newsgroup-data))
2352     (save-excursion
2353       (gnus-save-hidden-threads
2354         (gnus-summary-show-all-threads)
2355         (goto-char (point-min))
2356         (while data
2357           (while (get-text-property (point) 'gnus-intangible)
2358             (forward-line 1))
2359           (gnus-data-set-pos (car data) (+ (point) 3))
2360           (setq data (cdr data))
2361           (forward-line 1))))))
2362
2363 (defun gnus-hidden-threads-configuration ()
2364   "Return the current hidden threads configuration."
2365   (save-excursion
2366     (let (config)
2367       (goto-char (point-min))
2368       (while (search-forward "\r" nil t)
2369         (push (1- (point)) config))
2370       config)))
2371
2372 (defun gnus-restore-hidden-threads-configuration (config)
2373   "Restore hidden threads configuration from CONFIG."
2374   (let (point buffer-read-only)
2375     (while (setq point (pop config))
2376       (when (and (< point (point-max))
2377                  (goto-char point)
2378                  (eq (char-after) ?\n))
2379         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2380
2381 ;; Various summary mode internalish functions.
2382
2383 (defun gnus-mouse-pick-article (e)
2384   (interactive "e")
2385   (mouse-set-point e)
2386   (gnus-summary-next-page nil t))
2387
2388 (defun gnus-summary-set-display-table ()
2389   ;; Change the display table.  Odd characters have a tendency to mess
2390   ;; up nicely formatted displays - we make all possible glyphs
2391   ;; display only a single character.
2392
2393   ;; We start from the standard display table, if any.
2394   (let ((table (or (copy-sequence standard-display-table)
2395                    (make-display-table)))
2396         (i 32))
2397     ;; Nix out all the control chars...
2398     (while (>= (setq i (1- i)) 0)
2399       (aset table i [??]))
2400     ;; ... but not newline and cr, of course.  (cr is necessary for the
2401     ;; selective display).
2402     (aset table ?\n nil)
2403     (aset table ?\r nil)
2404     ;; We keep TAB as well.
2405     (aset table ?\t nil)
2406     ;; We nix out any glyphs over 126 that are not set already.
2407     (let ((i 256))
2408       (while (>= (setq i (1- i)) 127)
2409         ;; Only modify if the entry is nil.
2410         (unless (aref table i)
2411           (aset table i [??]))))
2412     (setq buffer-display-table table)))
2413
2414 (defun gnus-summary-setup-buffer (group)
2415   "Initialize summary buffer."
2416   (let ((buffer (concat "*Summary " group "*")))
2417     (if (get-buffer buffer)
2418         (progn
2419           (set-buffer buffer)
2420           (setq gnus-summary-buffer (current-buffer))
2421           (not gnus-newsgroup-prepared))
2422       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2423       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2424       (gnus-summary-mode group)
2425       (when gnus-carpal
2426         (gnus-carpal-setup-buffer 'summary))
2427       (unless gnus-single-article-buffer
2428         (make-local-variable 'gnus-article-buffer)
2429         (make-local-variable 'gnus-article-current)
2430         (make-local-variable 'gnus-original-article-buffer))
2431       (setq gnus-newsgroup-name group)
2432       t)))
2433
2434 (defun gnus-set-global-variables ()
2435   ;; Set the global equivalents of the summary buffer-local variables
2436   ;; to the latest values they had.  These reflect the summary buffer
2437   ;; that was in action when the last article was fetched.
2438   (when (eq major-mode 'gnus-summary-mode)
2439     (setq gnus-summary-buffer (current-buffer))
2440     (let ((name gnus-newsgroup-name)
2441           (marked gnus-newsgroup-marked)
2442           (unread gnus-newsgroup-unreads)
2443           (headers gnus-current-headers)
2444           (data gnus-newsgroup-data)
2445           (summary gnus-summary-buffer)
2446           (article-buffer gnus-article-buffer)
2447           (original gnus-original-article-buffer)
2448           (gac gnus-article-current)
2449           (reffed gnus-reffed-article-number)
2450           (score-file gnus-current-score-file)
2451           (default-charset gnus-newsgroup-charset))
2452       (save-excursion
2453         (set-buffer gnus-group-buffer)
2454         (setq gnus-newsgroup-name name
2455               gnus-newsgroup-marked marked
2456               gnus-newsgroup-unreads unread
2457               gnus-current-headers headers
2458               gnus-newsgroup-data data
2459               gnus-article-current gac
2460               gnus-summary-buffer summary
2461               gnus-article-buffer article-buffer
2462               gnus-original-article-buffer original
2463               gnus-reffed-article-number reffed
2464               gnus-current-score-file score-file
2465               gnus-newsgroup-charset default-charset)
2466         ;; The article buffer also has local variables.
2467         (when (gnus-buffer-live-p gnus-article-buffer)
2468           (set-buffer gnus-article-buffer)
2469           (setq gnus-summary-buffer summary))))))
2470
2471 (defun gnus-summary-article-unread-p (article)
2472   "Say whether ARTICLE is unread or not."
2473   (memq article gnus-newsgroup-unreads))
2474
2475 (defun gnus-summary-first-article-p (&optional article)
2476   "Return whether ARTICLE is the first article in the buffer."
2477   (if (not (setq article (or article (gnus-summary-article-number))))
2478       nil
2479     (eq article (caar gnus-newsgroup-data))))
2480
2481 (defun gnus-summary-last-article-p (&optional article)
2482   "Return whether ARTICLE is the last article in the buffer."
2483   (if (not (setq article (or article (gnus-summary-article-number))))
2484       ;; All non-existent numbers are the last article.  :-)
2485       t
2486     (not (cdr (gnus-data-find-list article)))))
2487
2488 (defun gnus-make-thread-indent-array ()
2489   (let ((n 200))
2490     (unless (and gnus-thread-indent-array
2491                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2492       (setq gnus-thread-indent-array (make-vector 201 "")
2493             gnus-thread-indent-array-level gnus-thread-indent-level)
2494       (while (>= n 0)
2495         (aset gnus-thread-indent-array n
2496               (make-string (* n gnus-thread-indent-level) ? ))
2497         (setq n (1- n))))))
2498
2499 (defun gnus-update-summary-mark-positions ()
2500   "Compute where the summary marks are to go."
2501   (save-excursion
2502     (when (gnus-buffer-exists-p gnus-summary-buffer)
2503       (set-buffer gnus-summary-buffer))
2504     (let ((gnus-replied-mark 129)
2505           (gnus-score-below-mark 130)
2506           (gnus-score-over-mark 130)
2507           (gnus-download-mark 131)
2508           (spec gnus-summary-line-format-spec)
2509           gnus-visual pos)
2510       (save-excursion
2511         (gnus-set-work-buffer)
2512         (let ((gnus-summary-line-format-spec spec)
2513               (gnus-newsgroup-downloadable '((0 . t))))
2514           (gnus-summary-insert-line
2515            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2516           (goto-char (point-min))
2517           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2518                                              (- (point) 2)))))
2519           (goto-char (point-min))
2520           (push (cons 'replied (and (search-forward "\201" nil t)
2521                                     (- (point) 2)))
2522                 pos)
2523           (goto-char (point-min))
2524           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2525                 pos)
2526           (goto-char (point-min))
2527           (push (cons 'download
2528                       (and (search-forward "\203" nil t) (- (point) 2)))
2529                 pos)))
2530       (setq gnus-summary-mark-positions pos))))
2531
2532 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2533   "Insert a dummy root in the summary buffer."
2534   (beginning-of-line)
2535   (gnus-add-text-properties
2536    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2537    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2538
2539 (defun gnus-summary-from-or-to-or-newsgroups (header)
2540   (let ((to (cdr (assq 'To (mail-header-extra header))))
2541         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2542         (mail-parse-charset gnus-newsgroup-charset)
2543         (mail-parse-ignored-charsets 
2544          (save-excursion (set-buffer gnus-summary-buffer)
2545                          gnus-newsgroup-ignored-charsets)))
2546     (cond
2547      ((and to
2548            gnus-ignored-from-addresses
2549            (string-match gnus-ignored-from-addresses
2550                          (mail-header-from header)))
2551       (concat "-> "
2552               (or (car (funcall gnus-extract-address-components
2553                                 (funcall
2554                                  gnus-decode-encoded-word-function to)))
2555                   (funcall gnus-decode-encoded-word-function to))))
2556      ((and newsgroups
2557            gnus-ignored-from-addresses
2558            (string-match gnus-ignored-from-addresses
2559                          (mail-header-from header)))
2560       (concat "=> " newsgroups))
2561      (t
2562       (or (car (funcall gnus-extract-address-components
2563                         (mail-header-from header)))
2564           (mail-header-from header))))))
2565
2566 (defun gnus-summary-insert-line (gnus-tmp-header
2567                                  gnus-tmp-level gnus-tmp-current
2568                                  gnus-tmp-unread gnus-tmp-replied
2569                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2570                                  &optional gnus-tmp-dummy gnus-tmp-score
2571                                  gnus-tmp-process)
2572   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2573          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2574          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2575          (gnus-tmp-score-char
2576           (if (or (null gnus-summary-default-score)
2577                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2578                       gnus-summary-zcore-fuzz))
2579               ? ;Whitespace
2580             (if (< gnus-tmp-score gnus-summary-default-score)
2581                 gnus-score-below-mark gnus-score-over-mark)))
2582          (gnus-tmp-replied
2583           (cond (gnus-tmp-process gnus-process-mark)
2584                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2585                  gnus-cached-mark)
2586                 (gnus-tmp-replied gnus-replied-mark)
2587                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2588                  gnus-saved-mark)
2589                 (t gnus-unread-mark)))
2590          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2591          (gnus-tmp-name
2592           (cond
2593            ((string-match "<[^>]+> *$" gnus-tmp-from)
2594             (let ((beg (match-beginning 0)))
2595               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2596                        (substring gnus-tmp-from (1+ (match-beginning 0))
2597                                   (1- (match-end 0))))
2598                   (substring gnus-tmp-from 0 beg))))
2599            ((string-match "(.+)" gnus-tmp-from)
2600             (substring gnus-tmp-from
2601                        (1+ (match-beginning 0)) (1- (match-end 0))))
2602            (t gnus-tmp-from)))
2603          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2604          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2605          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2606          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2607          (buffer-read-only nil))
2608     (when (string= gnus-tmp-name "")
2609       (setq gnus-tmp-name gnus-tmp-from))
2610     (unless (numberp gnus-tmp-lines)
2611       (setq gnus-tmp-lines 0))
2612     (gnus-put-text-property
2613      (point)
2614      (progn (eval gnus-summary-line-format-spec) (point))
2615      'gnus-number gnus-tmp-number)
2616     (when (gnus-visual-p 'summary-highlight 'highlight)
2617       (forward-line -1)
2618       (gnus-run-hooks 'gnus-summary-update-hook)
2619       (forward-line 1))))
2620
2621 (defun gnus-summary-update-line (&optional dont-update)
2622   ;; Update summary line after change.
2623   (when (and gnus-summary-default-score
2624              (not gnus-summary-inhibit-highlight))
2625     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2626            (article (gnus-summary-article-number))
2627            (score (gnus-summary-article-score article)))
2628       (unless dont-update
2629         (if (and gnus-summary-mark-below
2630                  (< (gnus-summary-article-score)
2631                     gnus-summary-mark-below))
2632             ;; This article has a low score, so we mark it as read.
2633             (when (memq article gnus-newsgroup-unreads)
2634               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2635           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2636             ;; This article was previously marked as read on account
2637             ;; of a low score, but now it has risen, so we mark it as
2638             ;; unread.
2639             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2640         (gnus-summary-update-mark
2641          (if (or (null gnus-summary-default-score)
2642                  (<= (abs (- score gnus-summary-default-score))
2643                      gnus-summary-zcore-fuzz))
2644              ? ;Whitespace
2645            (if (< score gnus-summary-default-score)
2646                gnus-score-below-mark gnus-score-over-mark))
2647          'score))
2648       ;; Do visual highlighting.
2649       (when (gnus-visual-p 'summary-highlight 'highlight)
2650         (gnus-run-hooks 'gnus-summary-update-hook)))))
2651
2652 (defvar gnus-tmp-new-adopts nil)
2653
2654 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2655   "Return the number of articles in THREAD.
2656 This may be 0 in some cases -- if none of the articles in
2657 the thread are to be displayed."
2658   (let* ((number
2659           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2660           (cond
2661            ((not (listp thread))
2662             1)
2663            ((and (consp thread) (cdr thread))
2664             (apply
2665              '+ 1 (mapcar
2666                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2667            ((null thread)
2668             1)
2669            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2670             1)
2671            (t 0))))
2672     (when (and level (zerop level) gnus-tmp-new-adopts)
2673       (incf number
2674             (apply '+ (mapcar
2675                        'gnus-summary-number-of-articles-in-thread
2676                        gnus-tmp-new-adopts))))
2677     (if char
2678         (if (> number 1) gnus-not-empty-thread-mark
2679           gnus-empty-thread-mark)
2680       number)))
2681
2682 (defun gnus-summary-set-local-parameters (group)
2683   "Go through the local params of GROUP and set all variable specs in that list."
2684   (let ((params (gnus-group-find-parameter group))
2685         elem)
2686     (while params
2687       (setq elem (car params)
2688             params (cdr params))
2689       (and (consp elem)                 ; Has to be a cons.
2690            (consp (cdr elem))           ; The cdr has to be a list.
2691            (symbolp (car elem))         ; Has to be a symbol in there.
2692            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2693            (ignore-errors               ; So we set it.
2694              (make-local-variable (car elem))
2695              (set (car elem) (eval (nth 1 elem))))))))
2696
2697 (defun gnus-summary-read-group (group &optional show-all no-article
2698                                       kill-buffer no-display backward
2699                                       select-articles)
2700   "Start reading news in newsgroup GROUP.
2701 If SHOW-ALL is non-nil, already read articles are also listed.
2702 If NO-ARTICLE is non-nil, no article is selected initially.
2703 If NO-DISPLAY, don't generate a summary buffer."
2704   (let (result)
2705     (while (and group
2706                 (null (setq result
2707                             (let ((gnus-auto-select-next nil))
2708                               (or (gnus-summary-read-group-1
2709                                    group show-all no-article
2710                                    kill-buffer no-display
2711                                    select-articles)
2712                                   (setq show-all nil
2713                                         select-articles nil)))))
2714                 (eq gnus-auto-select-next 'quietly))
2715       (set-buffer gnus-group-buffer)
2716       ;; The entry function called above goes to the next
2717       ;; group automatically, so we go two groups back
2718       ;; if we are searching for the previous group.
2719       (when backward
2720         (gnus-group-prev-unread-group 2))
2721       (if (not (equal group (gnus-group-group-name)))
2722           (setq group (gnus-group-group-name))
2723         (setq group nil)))
2724     result))
2725
2726 (defun gnus-summary-read-group-1 (group show-all no-article
2727                                         kill-buffer no-display
2728                                         &optional select-articles)
2729   ;; Killed foreign groups can't be entered.
2730   (when (and (not (gnus-group-native-p group))
2731              (not (gnus-gethash group gnus-newsrc-hashtb)))
2732     (error "Dead non-native groups can't be entered"))
2733   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2734   (let* ((new-group (gnus-summary-setup-buffer group))
2735          (quit-config (gnus-group-quit-config group))
2736          (did-select (and new-group (gnus-select-newsgroup
2737                                      group show-all select-articles))))
2738     (cond
2739      ;; This summary buffer exists already, so we just select it.
2740      ((not new-group)
2741       (gnus-set-global-variables)
2742       (when kill-buffer
2743         (gnus-kill-or-deaden-summary kill-buffer))
2744       (gnus-configure-windows 'summary 'force)
2745       (gnus-set-mode-line 'summary)
2746       (gnus-summary-position-point)
2747       (message "")
2748       t)
2749      ;; We couldn't select this group.
2750      ((null did-select)
2751       (when (and (eq major-mode 'gnus-summary-mode)
2752                  (not (equal (current-buffer) kill-buffer)))
2753         (kill-buffer (current-buffer))
2754         (if (not quit-config)
2755             (progn
2756               ;; Update the info -- marks might need to be removed,
2757               ;; for instance.
2758               (gnus-summary-update-info)
2759               (set-buffer gnus-group-buffer)
2760               (gnus-group-jump-to-group group)
2761               (gnus-group-next-unread-group 1))
2762           (gnus-handle-ephemeral-exit quit-config)))
2763       (gnus-message 3 "Can't select group")
2764       nil)
2765      ;; The user did a `C-g' while prompting for number of articles,
2766      ;; so we exit this group.
2767      ((eq did-select 'quit)
2768       (and (eq major-mode 'gnus-summary-mode)
2769            (not (equal (current-buffer) kill-buffer))
2770            (kill-buffer (current-buffer)))
2771       (when kill-buffer
2772         (gnus-kill-or-deaden-summary kill-buffer))
2773       (if (not quit-config)
2774           (progn
2775             (set-buffer gnus-group-buffer)
2776             (gnus-group-jump-to-group group)
2777             (gnus-group-next-unread-group 1)
2778             (gnus-configure-windows 'group 'force))
2779         (gnus-handle-ephemeral-exit quit-config))
2780       ;; Finally signal the quit.
2781       (signal 'quit nil))
2782      ;; The group was successfully selected.
2783      (t
2784       (gnus-set-global-variables)
2785       ;; Save the active value in effect when the group was entered.
2786       (setq gnus-newsgroup-active
2787             (gnus-copy-sequence
2788              (gnus-active gnus-newsgroup-name)))
2789       ;; You can change the summary buffer in some way with this hook.
2790       (gnus-run-hooks 'gnus-select-group-hook)
2791       ;; Set any local variables in the group parameters.
2792       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2793       (gnus-update-format-specifications
2794        nil 'summary 'summary-mode 'summary-dummy)
2795       ;; Do score processing.
2796       (when gnus-use-scoring
2797         (gnus-possibly-score-headers))
2798       ;; Check whether to fill in the gaps in the threads.
2799       (when gnus-build-sparse-threads
2800         (gnus-build-sparse-threads))
2801       ;; Find the initial limit.
2802       (if gnus-show-threads
2803           (if show-all
2804               (let ((gnus-newsgroup-dormant nil))
2805                 (gnus-summary-initial-limit show-all))
2806             (gnus-summary-initial-limit show-all))
2807         (setq gnus-newsgroup-limit
2808               (mapcar
2809                (lambda (header) (mail-header-number header))
2810                gnus-newsgroup-headers)))
2811       ;; Generate the summary buffer.
2812       (unless no-display
2813         (gnus-summary-prepare))
2814       (when gnus-use-trees
2815         (gnus-tree-open group)
2816         (setq gnus-summary-highlight-line-function
2817               'gnus-tree-highlight-article))
2818       ;; If the summary buffer is empty, but there are some low-scored
2819       ;; articles or some excluded dormants, we include these in the
2820       ;; buffer.
2821       (when (and (zerop (buffer-size))
2822                  (not no-display))
2823         (cond (gnus-newsgroup-dormant
2824                (gnus-summary-limit-include-dormant))
2825               ((and gnus-newsgroup-scored show-all)
2826                (gnus-summary-limit-include-expunged t))))
2827       ;; Function `gnus-apply-kill-file' must be called in this hook.
2828       (gnus-run-hooks 'gnus-apply-kill-hook)
2829       (if (and (zerop (buffer-size))
2830                (not no-display))
2831           (progn
2832             ;; This newsgroup is empty.
2833             (gnus-summary-catchup-and-exit nil t)
2834             (gnus-message 6 "No unread news")
2835             (when kill-buffer
2836               (gnus-kill-or-deaden-summary kill-buffer))
2837             ;; Return nil from this function.
2838             nil)
2839         ;; Hide conversation thread subtrees.  We cannot do this in
2840         ;; gnus-summary-prepare-hook since kill processing may not
2841         ;; work with hidden articles.
2842         (and gnus-show-threads
2843              gnus-thread-hide-subtree
2844              (gnus-summary-hide-all-threads))
2845         (when kill-buffer
2846           (gnus-kill-or-deaden-summary kill-buffer))
2847         ;; Show first unread article if requested.
2848         (if (and (not no-article)
2849                  (not no-display)
2850                  gnus-newsgroup-unreads
2851                  gnus-auto-select-first)
2852             (progn
2853               (gnus-configure-windows 'summary)
2854               (cond
2855                ((eq gnus-auto-select-first 'best)
2856                 (gnus-summary-best-unread-article))
2857                ((eq gnus-auto-select-first t)
2858                 (gnus-summary-first-unread-article))
2859                ((gnus-functionp gnus-auto-select-first)
2860                 (funcall gnus-auto-select-first))))
2861           ;; Don't select any articles, just move point to the first
2862           ;; article in the group.
2863           (goto-char (point-min))
2864           (gnus-summary-position-point)
2865           (gnus-configure-windows 'summary 'force)
2866           (gnus-set-mode-line 'summary))
2867         (when (get-buffer-window gnus-group-buffer t)
2868           ;; Gotta use windows, because recenter does weird stuff if
2869           ;; the current buffer ain't the displayed window.
2870           (let ((owin (selected-window)))
2871             (select-window (get-buffer-window gnus-group-buffer t))
2872             (when (gnus-group-goto-group group)
2873               (recenter))
2874             (select-window owin)))
2875         ;; Mark this buffer as "prepared".
2876         (setq gnus-newsgroup-prepared t)
2877         (gnus-run-hooks 'gnus-summary-prepared-hook)
2878         t)))))
2879
2880 (defun gnus-summary-prepare ()
2881   "Generate the summary buffer."
2882   (interactive)
2883   (let ((buffer-read-only nil))
2884     (erase-buffer)
2885     (setq gnus-newsgroup-data nil
2886           gnus-newsgroup-data-reverse nil)
2887     (gnus-run-hooks 'gnus-summary-generate-hook)
2888     ;; Generate the buffer, either with threads or without.
2889     (when gnus-newsgroup-headers
2890       (gnus-summary-prepare-threads
2891        (if gnus-show-threads
2892            (gnus-sort-gathered-threads
2893             (funcall gnus-summary-thread-gathering-function
2894                      (gnus-sort-threads
2895                       (gnus-cut-threads (gnus-make-threads)))))
2896          ;; Unthreaded display.
2897          (gnus-sort-articles gnus-newsgroup-headers))))
2898     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2899     ;; Call hooks for modifying summary buffer.
2900     (goto-char (point-min))
2901     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2902
2903 (defsubst gnus-general-simplify-subject (subject)
2904   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2905   (setq subject
2906         (cond
2907          ;; Truncate the subject.
2908          (gnus-simplify-subject-functions
2909           (gnus-map-function gnus-simplify-subject-functions subject))
2910          ((numberp gnus-summary-gather-subject-limit)
2911           (setq subject (gnus-simplify-subject-re subject))
2912           (if (> (length subject) gnus-summary-gather-subject-limit)
2913               (substring subject 0 gnus-summary-gather-subject-limit)
2914             subject))
2915          ;; Fuzzily simplify it.
2916          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2917           (gnus-simplify-subject-fuzzy subject))
2918          ;; Just remove the leading "Re:".
2919          (t
2920           (gnus-simplify-subject-re subject))))
2921
2922   (if (and gnus-summary-gather-exclude-subject
2923            (string-match gnus-summary-gather-exclude-subject subject))
2924       nil                               ; This article shouldn't be gathered
2925     subject))
2926
2927 (defun gnus-summary-simplify-subject-query ()
2928   "Query where the respool algorithm would put this article."
2929   (interactive)
2930   (gnus-summary-select-article)
2931   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2932
2933 (defun gnus-gather-threads-by-subject (threads)
2934   "Gather threads by looking at Subject headers."
2935   (if (not gnus-summary-make-false-root)
2936       threads
2937     (let ((hashtb (gnus-make-hashtable 1024))
2938           (prev threads)
2939           (result threads)
2940           subject hthread whole-subject)
2941       (while threads
2942         (setq subject (gnus-general-simplify-subject
2943                        (setq whole-subject (mail-header-subject
2944                                             (caar threads)))))
2945         (when subject
2946           (if (setq hthread (gnus-gethash subject hashtb))
2947               (progn
2948                 ;; We enter a dummy root into the thread, if we
2949                 ;; haven't done that already.
2950                 (unless (stringp (caar hthread))
2951                   (setcar hthread (list whole-subject (car hthread))))
2952                 ;; We add this new gathered thread to this gathered
2953                 ;; thread.
2954                 (setcdr (car hthread)
2955                         (nconc (cdar hthread) (list (car threads))))
2956                 ;; Remove it from the list of threads.
2957                 (setcdr prev (cdr threads))
2958                 (setq threads prev))
2959             ;; Enter this thread into the hash table.
2960             (gnus-sethash subject threads hashtb)))
2961         (setq prev threads)
2962         (setq threads (cdr threads)))
2963       result)))
2964
2965 (defun gnus-gather-threads-by-references (threads)
2966   "Gather threads by looking at References headers."
2967   (let ((idhashtb (gnus-make-hashtable 1024))
2968         (thhashtb (gnus-make-hashtable 1024))
2969         (prev threads)
2970         (result threads)
2971         ids references id gthread gid entered ref)
2972     (while threads
2973       (when (setq references (mail-header-references (caar threads)))
2974         (setq id (mail-header-id (caar threads))
2975               ids (gnus-split-references references)
2976               entered nil)
2977         (while (setq ref (pop ids))
2978           (setq ids (delete ref ids))
2979           (if (not (setq gid (gnus-gethash ref idhashtb)))
2980               (progn
2981                 (gnus-sethash ref id idhashtb)
2982                 (gnus-sethash id threads thhashtb))
2983             (setq gthread (gnus-gethash gid thhashtb))
2984             (unless entered
2985               ;; We enter a dummy root into the thread, if we
2986               ;; haven't done that already.
2987               (unless (stringp (caar gthread))
2988                 (setcar gthread (list (mail-header-subject (caar gthread))
2989                                       (car gthread))))
2990               ;; We add this new gathered thread to this gathered
2991               ;; thread.
2992               (setcdr (car gthread)
2993                       (nconc (cdar gthread) (list (car threads)))))
2994             ;; Add it into the thread hash table.
2995             (gnus-sethash id gthread thhashtb)
2996             (setq entered t)
2997             ;; Remove it from the list of threads.
2998             (setcdr prev (cdr threads))
2999             (setq threads prev))))
3000       (setq prev threads)
3001       (setq threads (cdr threads)))
3002     result))
3003
3004 (defun gnus-sort-gathered-threads (threads)
3005   "Sort subtreads inside each gathered thread by article number."
3006   (let ((result threads))
3007     (while threads
3008       (when (stringp (caar threads))
3009         (setcdr (car threads)
3010                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
3011       (setq threads (cdr threads)))
3012     result))
3013
3014 (defun gnus-thread-loop-p (root thread)
3015   "Say whether ROOT is in THREAD."
3016   (let ((stack (list thread))
3017         (infloop 0)
3018         th)
3019     (while (setq thread (pop stack))
3020       (setq th (cdr thread))
3021       (while (and th
3022                   (not (eq (caar th) root)))
3023         (pop th))
3024       (if th
3025           ;; We have found a loop.
3026           (let (ref-dep)
3027             (setcdr thread (delq (car th) (cdr thread)))
3028             (if (boundp (setq ref-dep (intern "none"
3029                                               gnus-newsgroup-dependencies)))
3030                 (setcdr (symbol-value ref-dep)
3031                         (nconc (cdr (symbol-value ref-dep))
3032                                (list (car th))))
3033               (set ref-dep (list nil (car th))))
3034             (setq infloop 1
3035                   stack nil))
3036         ;; Push all the subthreads onto the stack.
3037         (push (cdr thread) stack)))
3038     infloop))
3039
3040 (defun gnus-make-threads ()
3041   "Go through the dependency hashtb and find the roots.  Return all threads."
3042   (let (threads)
3043     (while (catch 'infloop
3044              (mapatoms
3045               (lambda (refs)
3046                 ;; Deal with self-referencing References loops.
3047                 (when (and (car (symbol-value refs))
3048                            (not (zerop
3049                                  (apply
3050                                   '+
3051                                   (mapcar
3052                                    (lambda (thread)
3053                                      (gnus-thread-loop-p
3054                                       (car (symbol-value refs)) thread))
3055                                    (cdr (symbol-value refs)))))))
3056                   (setq threads nil)
3057                   (throw 'infloop t))
3058                 (unless (car (symbol-value refs))
3059                   ;; These threads do not refer back to any other articles,
3060                   ;; so they're roots.
3061                   (setq threads (append (cdr (symbol-value refs)) threads))))
3062               gnus-newsgroup-dependencies)))
3063     threads))
3064
3065 ;; Build the thread tree.
3066 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3067   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3068
3069 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3070 if it was already present.
3071
3072 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3073 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3074 Message-IDs will be renamed be renamed to a unique Message-ID before
3075 being entered.
3076
3077 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3078   (let* ((id (mail-header-id header))
3079          (id-dep (and id (intern id dependencies)))
3080          ref ref-dep ref-header)
3081     ;; Enter this `header' in the `dependencies' table.
3082     (cond
3083      ((not id-dep)
3084       (setq header nil))
3085      ;; The first two cases do the normal part: enter a new `header'
3086      ;; in the `dependencies' table.
3087      ((not (boundp id-dep))
3088       (set id-dep (list header)))
3089      ((null (car (symbol-value id-dep)))
3090       (setcar (symbol-value id-dep) header))
3091
3092      ;; From here the `header' was already present in the
3093      ;; `dependencies' table.
3094      (force-new
3095       ;; Overrides an existing entry;
3096       ;; just set the header part of the entry.
3097       (setcar (symbol-value id-dep) header))
3098
3099      ;; Renames the existing `header' to a unique Message-ID.
3100      ((not gnus-summary-ignore-duplicates)
3101       ;; An article with this Message-ID has already been seen.
3102       ;; We rename the Message-ID.
3103       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3104            (list header))
3105       (mail-header-set-id header id))
3106
3107      ;; The last case ignores an existing entry, except it adds any
3108      ;; additional Xrefs (in case the two articles came from different
3109      ;; servers.
3110      ;; Also sets `header' to `nil' meaning that the `dependencies'
3111      ;; table was *not* modified.
3112      (t
3113       (mail-header-set-xref
3114        (car (symbol-value id-dep))
3115        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3116                    "")
3117                (or (mail-header-xref header) "")))
3118       (setq header nil)))
3119
3120     (when header
3121       ;; First check if that we are not creating a References loop.
3122       (setq ref (gnus-parent-id (mail-header-references header)))
3123       (while (and ref
3124                   (setq ref-dep (intern-soft ref dependencies))
3125                   (boundp ref-dep)
3126                   (setq ref-header (car (symbol-value ref-dep))))
3127         (if (string= id ref)
3128             ;; Yuk!  This is a reference loop.  Make the article be a
3129             ;; root article.
3130             (progn
3131               (mail-header-set-references (car (symbol-value id-dep)) "none")
3132               (setq ref nil))
3133           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3134       (setq ref (gnus-parent-id (mail-header-references header)))
3135       (setq ref-dep (intern (or ref "none") dependencies))
3136       (if (boundp ref-dep)
3137           (setcdr (symbol-value ref-dep)
3138                   (nconc (cdr (symbol-value ref-dep))
3139                          (list (symbol-value id-dep))))
3140         (set ref-dep (list nil (symbol-value id-dep)))))
3141     header))
3142
3143 (defun gnus-build-sparse-threads ()
3144   (let ((headers gnus-newsgroup-headers)
3145         (gnus-summary-ignore-duplicates t)
3146         header references generation relations
3147         subject child end new-child date)
3148     ;; First we create an alist of generations/relations, where
3149     ;; generations is how much we trust the relation, and the relation
3150     ;; is parent/child.
3151     (gnus-message 7 "Making sparse threads...")
3152     (save-excursion
3153       (nnheader-set-temp-buffer " *gnus sparse threads*")
3154       (while (setq header (pop headers))
3155         (when (and (setq references (mail-header-references header))
3156                    (not (string= references "")))
3157           (insert references)
3158           (setq child (mail-header-id header)
3159                 subject (mail-header-subject header)
3160                 date (mail-header-date header)
3161                 generation 0)
3162           (while (search-backward ">" nil t)
3163             (setq end (1+ (point)))
3164             (when (search-backward "<" nil t)
3165               (setq new-child (buffer-substring (point) end))
3166               (push (list (incf generation)
3167                           child (setq child new-child)
3168                           subject date)
3169                     relations)))
3170           (when child
3171             (push (list (1+ generation) child nil subject) relations))
3172           (erase-buffer)))
3173       (kill-buffer (current-buffer)))
3174     ;; Sort over trustworthiness.
3175     (mapcar
3176      (lambda (relation)
3177        (when (gnus-dependencies-add-header
3178               (make-full-mail-header
3179                gnus-reffed-article-number
3180                (nth 3 relation) "" (or (nth 4 relation) "")
3181                (nth 1 relation)
3182                (or (nth 2 relation) "") 0 0 "")
3183               gnus-newsgroup-dependencies nil)
3184          (push gnus-reffed-article-number gnus-newsgroup-limit)
3185          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3186          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3187                gnus-newsgroup-reads)
3188          (decf gnus-reffed-article-number)))
3189      (sort relations 'car-less-than-car))
3190     (gnus-message 7 "Making sparse threads...done")))
3191
3192 (defun gnus-build-old-threads ()
3193   ;; Look at all the articles that refer back to old articles, and
3194   ;; fetch the headers for the articles that aren't there.  This will
3195   ;; build complete threads - if the roots haven't been expired by the
3196   ;; server, that is.
3197   (let (id heads)
3198     (mapatoms
3199      (lambda (refs)
3200        (when (not (car (symbol-value refs)))
3201          (setq heads (cdr (symbol-value refs)))
3202          (while heads
3203            (if (memq (mail-header-number (caar heads))
3204                      gnus-newsgroup-dormant)
3205                (setq heads (cdr heads))
3206              (setq id (symbol-name refs))
3207              (while (and (setq id (gnus-build-get-header id))
3208                          (not (car (gnus-id-to-thread id)))))
3209              (setq heads nil)))))
3210      gnus-newsgroup-dependencies)))
3211
3212 ;; This function has to be called with point after the article number
3213 ;; on the beginning of the line.
3214 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3215   (let ((eol (gnus-point-at-eol))
3216         (buffer (current-buffer))
3217         header)
3218
3219     ;; overview: [num subject from date id refs chars lines misc]
3220     (unwind-protect
3221         (progn
3222           (narrow-to-region (point) eol)
3223           (unless (eobp)
3224             (forward-char))
3225
3226           (setq header
3227                 (make-full-mail-header
3228                  number                         ; number
3229                  (funcall gnus-decode-encoded-word-function
3230                           (nnheader-nov-field)) ; subject
3231                  (funcall gnus-decode-encoded-word-function
3232                           (nnheader-nov-field)) ; from
3233                  (nnheader-nov-field)           ; date
3234                  (nnheader-nov-read-message-id) ; id
3235                  (nnheader-nov-field)           ; refs
3236                  (nnheader-nov-read-integer)    ; chars
3237                  (nnheader-nov-read-integer)    ; lines
3238                  (unless (eobp)
3239                    (nnheader-nov-field))                ; misc
3240                  (nnheader-nov-parse-extra))))  ; extra
3241
3242       (widen))
3243
3244     (when gnus-alter-header-function
3245       (funcall gnus-alter-header-function header))
3246     (gnus-dependencies-add-header header dependencies force-new)))
3247
3248 (defun gnus-build-get-header (id)
3249   ;; Look through the buffer of NOV lines and find the header to
3250   ;; ID.  Enter this line into the dependencies hash table, and return
3251   ;; the id of the parent article (if any).
3252   (let ((deps gnus-newsgroup-dependencies)
3253         found header)
3254     (prog1
3255         (save-excursion
3256           (set-buffer nntp-server-buffer)
3257           (let ((case-fold-search nil))
3258             (goto-char (point-min))
3259             (while (and (not found)
3260                         (search-forward id nil t))
3261               (beginning-of-line)
3262               (setq found (looking-at
3263                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3264                                    (regexp-quote id))))
3265               (or found (beginning-of-line 2)))
3266             (when found
3267               (beginning-of-line)
3268               (and
3269                (setq header (gnus-nov-parse-line
3270                              (read (current-buffer)) deps))
3271                (gnus-parent-id (mail-header-references header))))))
3272       (when header
3273         (let ((number (mail-header-number header)))
3274           (push number gnus-newsgroup-limit)
3275           (push header gnus-newsgroup-headers)
3276           (if (memq number gnus-newsgroup-unselected)
3277               (progn
3278                 (push number gnus-newsgroup-unreads)
3279                 (setq gnus-newsgroup-unselected
3280                       (delq number gnus-newsgroup-unselected)))
3281             (push number gnus-newsgroup-ancient)))))))
3282
3283 (defun gnus-build-all-threads ()
3284   "Read all the headers."
3285   (let ((gnus-summary-ignore-duplicates t)
3286         (dependencies gnus-newsgroup-dependencies)
3287         header article)
3288     (save-excursion
3289       (set-buffer nntp-server-buffer)
3290       (let ((case-fold-search nil))
3291         (goto-char (point-min))
3292         (while (not (eobp))
3293           (ignore-errors
3294             (setq article (read (current-buffer))
3295                   header (gnus-nov-parse-line
3296                           article dependencies)))
3297           (when header
3298             (save-excursion
3299               (set-buffer gnus-summary-buffer)
3300               (push header gnus-newsgroup-headers)
3301               (if (memq (setq article (mail-header-number header))
3302                         gnus-newsgroup-unselected)
3303                   (progn
3304                     (push article gnus-newsgroup-unreads)
3305                     (setq gnus-newsgroup-unselected
3306                           (delq article gnus-newsgroup-unselected)))
3307                 (push article gnus-newsgroup-ancient)))
3308             (forward-line 1)))))))
3309
3310 (defun gnus-summary-update-article-line (article header)
3311   "Update the line for ARTICLE using HEADERS."
3312   (let* ((id (mail-header-id header))
3313          (thread (gnus-id-to-thread id)))
3314     (unless thread
3315       (error "Article in no thread"))
3316     ;; Update the thread.
3317     (setcar thread header)
3318     (gnus-summary-goto-subject article)
3319     (let* ((datal (gnus-data-find-list article))
3320            (data (car datal))
3321            (length (when (cdr datal)
3322                      (- (gnus-data-pos data)
3323                         (gnus-data-pos (cadr datal)))))
3324            (buffer-read-only nil)
3325            (level (gnus-summary-thread-level)))
3326       (gnus-delete-line)
3327       (gnus-summary-insert-line
3328        header level nil (gnus-article-mark article)
3329        (memq article gnus-newsgroup-replied)
3330        (memq article gnus-newsgroup-expirable)
3331        ;; Only insert the Subject string when it's different
3332        ;; from the previous Subject string.
3333        (if (gnus-subject-equal
3334             (condition-case ()
3335                 (mail-header-subject
3336                  (gnus-data-header
3337                   (cadr
3338                    (gnus-data-find-list
3339                     article
3340                     (gnus-data-list t)))))
3341               ;; Error on the side of excessive subjects.
3342               (error ""))
3343             (mail-header-subject header))
3344            ""
3345          (mail-header-subject header))
3346        nil (cdr (assq article gnus-newsgroup-scored))
3347        (memq article gnus-newsgroup-processable))
3348       (when length
3349         (gnus-data-update-list
3350          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3351
3352 (defun gnus-summary-update-article (article &optional iheader)
3353   "Update ARTICLE in the summary buffer."
3354   (set-buffer gnus-summary-buffer)
3355   (let* ((header (gnus-summary-article-header article))
3356          (id (mail-header-id header))
3357          (data (gnus-data-find article))
3358          (thread (gnus-id-to-thread id))
3359          (references (mail-header-references header))
3360          (parent
3361           (gnus-id-to-thread
3362            (or (gnus-parent-id
3363                 (when (and references
3364                            (not (equal "" references)))
3365                   references))
3366                "none")))
3367          (buffer-read-only nil)
3368          (old (car thread)))
3369     (when thread
3370       (unless iheader
3371         (setcar thread nil)
3372         (when parent
3373           (delq thread parent)))
3374       (if (gnus-summary-insert-subject id header)
3375           ;; Set the (possibly) new article number in the data structure.
3376           (gnus-data-set-number data (gnus-id-to-article id))
3377         (setcar thread old)
3378         nil))))
3379
3380 (defun gnus-rebuild-thread (id &optional line)
3381   "Rebuild the thread containing ID.
3382 If LINE, insert the rebuilt thread starting on line LINE."
3383   (let ((buffer-read-only nil)
3384         old-pos current thread data)
3385     (if (not gnus-show-threads)
3386         (setq thread (list (car (gnus-id-to-thread id))))
3387       ;; Get the thread this article is part of.
3388       (setq thread (gnus-remove-thread id)))
3389     (setq old-pos (gnus-point-at-bol))
3390     (setq current (save-excursion
3391                     (and (zerop (forward-line -1))
3392                          (gnus-summary-article-number))))
3393     ;; If this is a gathered thread, we have to go some re-gathering.
3394     (when (stringp (car thread))
3395       (let ((subject (car thread))
3396             roots thr)
3397         (setq thread (cdr thread))
3398         (while thread
3399           (unless (memq (setq thr (gnus-id-to-thread
3400                                    (gnus-root-id
3401                                     (mail-header-id (caar thread)))))
3402                         roots)
3403             (push thr roots))
3404           (setq thread (cdr thread)))
3405         ;; We now have all (unique) roots.
3406         (if (= (length roots) 1)
3407             ;; All the loose roots are now one solid root.
3408             (setq thread (car roots))
3409           (setq thread (cons subject (gnus-sort-threads roots))))))
3410     (let (threads)
3411       ;; We then insert this thread into the summary buffer.
3412       (when line
3413         (goto-char (point-min))
3414         (forward-line (1- line)))
3415       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3416         (if gnus-show-threads
3417             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3418           (gnus-summary-prepare-unthreaded thread))
3419         (setq data (nreverse gnus-newsgroup-data))
3420         (setq threads gnus-newsgroup-threads))
3421       ;; We splice the new data into the data structure.
3422       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3423       ;;!!! then we want to insert at the beginning of the buffer.
3424       ;;!!! That happens to be true with Gnus now, but that may
3425       ;;!!! change in the future.  Perhaps.
3426       (gnus-data-enter-list
3427        (if line nil current) data (- (point) old-pos))
3428       (setq gnus-newsgroup-threads
3429             (nconc threads gnus-newsgroup-threads))
3430       (gnus-data-compute-positions))))
3431
3432 (defun gnus-number-to-header (number)
3433   "Return the header for article NUMBER."
3434   (let ((headers gnus-newsgroup-headers))
3435     (while (and headers
3436                 (not (= number (mail-header-number (car headers)))))
3437       (pop headers))
3438     (when headers
3439       (car headers))))
3440
3441 (defun gnus-parent-headers (in-headers &optional generation)
3442   "Return the headers of the GENERATIONeth parent of HEADERS."
3443   (unless generation
3444     (setq generation 1))
3445   (let ((parent t)
3446         (headers in-headers)
3447         references)
3448     (while (and parent
3449                 (not (zerop generation))
3450                 (setq references (mail-header-references headers)))
3451       (setq headers (if (and references
3452                              (setq parent (gnus-parent-id references)))
3453                         (car (gnus-id-to-thread parent))
3454                       nil))
3455       (decf generation))
3456     (and (not (eq headers in-headers))
3457          headers)))
3458
3459 (defun gnus-id-to-thread (id)
3460   "Return the (sub-)thread where ID appears."
3461   (gnus-gethash id gnus-newsgroup-dependencies))
3462
3463 (defun gnus-id-to-article (id)
3464   "Return the article number of ID."
3465   (let ((thread (gnus-id-to-thread id)))
3466     (when (and thread
3467                (car thread))
3468       (mail-header-number (car thread)))))
3469
3470 (defun gnus-id-to-header (id)
3471   "Return the article headers of ID."
3472   (car (gnus-id-to-thread id)))
3473
3474 (defun gnus-article-displayed-root-p (article)
3475   "Say whether ARTICLE is a root(ish) article."
3476   (let ((level (gnus-summary-thread-level article))
3477         (refs (mail-header-references  (gnus-summary-article-header article)))
3478         particle)
3479     (cond
3480      ((null level) nil)
3481      ((zerop level) t)
3482      ((null refs) t)
3483      ((null (gnus-parent-id refs)) t)
3484      ((and (= 1 level)
3485            (null (setq particle (gnus-id-to-article
3486                                  (gnus-parent-id refs))))
3487            (null (gnus-summary-thread-level particle)))))))
3488
3489 (defun gnus-root-id (id)
3490   "Return the id of the root of the thread where ID appears."
3491   (let (last-id prev)
3492     (while (and id (setq prev (car (gnus-id-to-thread id))))
3493       (setq last-id id
3494             id (gnus-parent-id (mail-header-references prev))))
3495     last-id))
3496
3497 (defun gnus-articles-in-thread (thread)
3498   "Return the list of articles in THREAD."
3499   (cons (mail-header-number (car thread))
3500         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3501
3502 (defun gnus-remove-thread (id &optional dont-remove)
3503   "Remove the thread that has ID in it."
3504   (let (headers thread last-id)
3505     ;; First go up in this thread until we find the root.
3506     (setq last-id (gnus-root-id id)
3507           headers (message-flatten-list (gnus-id-to-thread last-id)))
3508     ;; We have now found the real root of this thread.  It might have
3509     ;; been gathered into some loose thread, so we have to search
3510     ;; through the threads to find the thread we wanted.
3511     (let ((threads gnus-newsgroup-threads)
3512           sub)
3513       (while threads
3514         (setq sub (car threads))
3515         (if (stringp (car sub))
3516             ;; This is a gathered thread, so we look at the roots
3517             ;; below it to find whether this article is in this
3518             ;; gathered root.
3519             (progn
3520               (setq sub (cdr sub))
3521               (while sub
3522                 (when (member (caar sub) headers)
3523                   (setq thread (car threads)
3524                         threads nil
3525                         sub nil))
3526                 (setq sub (cdr sub))))
3527           ;; It's an ordinary thread, so we check it.
3528           (when (eq (car sub) (car headers))
3529             (setq thread sub
3530                   threads nil)))
3531         (setq threads (cdr threads)))
3532       ;; If this article is in no thread, then it's a root.
3533       (if thread
3534           (unless dont-remove
3535             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3536         (setq thread (gnus-id-to-thread last-id)))
3537       (when thread
3538         (prog1
3539             thread                      ; We return this thread.
3540           (unless dont-remove
3541             (if (stringp (car thread))
3542                 (progn
3543                   ;; If we use dummy roots, then we have to remove the
3544                   ;; dummy root as well.
3545                   (when (eq gnus-summary-make-false-root 'dummy)
3546                     ;; We go to the dummy root by going to
3547                     ;; the first sub-"thread", and then one line up.
3548                     (gnus-summary-goto-article
3549                      (mail-header-number (caadr thread)))
3550                     (forward-line -1)
3551                     (gnus-delete-line)
3552                     (gnus-data-compute-positions))
3553                   (setq thread (cdr thread))
3554                   (while thread
3555                     (gnus-remove-thread-1 (car thread))
3556                     (setq thread (cdr thread))))
3557               (gnus-summary-show-all-threads)
3558               (gnus-remove-thread-1 thread))))))))
3559
3560 (defun gnus-remove-thread-1 (thread)
3561   "Remove the thread THREAD recursively."
3562   (let ((number (mail-header-number (pop thread)))
3563         d)
3564     (setq thread (reverse thread))
3565     (while thread
3566       (gnus-remove-thread-1 (pop thread)))
3567     (when (setq d (gnus-data-find number))
3568       (goto-char (gnus-data-pos d))
3569       (gnus-data-remove
3570        number
3571        (- (gnus-point-at-bol)
3572           (prog1
3573               (1+ (gnus-point-at-eol))
3574             (gnus-delete-line)))))))
3575
3576 (defun gnus-sort-threads (threads)
3577   "Sort THREADS."
3578   (if (not gnus-thread-sort-functions)
3579       threads
3580     (gnus-message 8 "Sorting threads...")
3581     (prog1
3582         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3583       (gnus-message 8 "Sorting threads...done"))))
3584
3585 (defun gnus-sort-articles (articles)
3586   "Sort ARTICLES."
3587   (when gnus-article-sort-functions
3588     (gnus-message 7 "Sorting articles...")
3589     (prog1
3590         (setq gnus-newsgroup-headers
3591               (sort articles (gnus-make-sort-function
3592                               gnus-article-sort-functions)))
3593       (gnus-message 7 "Sorting articles...done"))))
3594
3595 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3596 (defmacro gnus-thread-header (thread)
3597   ;; Return header of first article in THREAD.
3598   ;; Note that THREAD must never, ever be anything else than a variable -
3599   ;; using some other form will lead to serious barfage.
3600   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3601   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3602   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3603         (vector thread) 2))
3604
3605 (defsubst gnus-article-sort-by-number (h1 h2)
3606   "Sort articles by article number."
3607   (< (mail-header-number h1)
3608      (mail-header-number h2)))
3609
3610 (defun gnus-thread-sort-by-number (h1 h2)
3611   "Sort threads by root article number."
3612   (gnus-article-sort-by-number
3613    (gnus-thread-header h1) (gnus-thread-header h2)))
3614
3615 (defsubst gnus-article-sort-by-lines (h1 h2)
3616   "Sort articles by article Lines header."
3617   (< (mail-header-lines h1)
3618      (mail-header-lines h2)))
3619
3620 (defun gnus-thread-sort-by-lines (h1 h2)
3621   "Sort threads by root article Lines header."
3622   (gnus-article-sort-by-lines
3623    (gnus-thread-header h1) (gnus-thread-header h2)))
3624
3625 (defsubst gnus-article-sort-by-chars (h1 h2)
3626   "Sort articles by octet length."
3627   (< (mail-header-chars h1)
3628      (mail-header-chars h2)))
3629
3630 (defun gnus-thread-sort-by-chars (h1 h2)
3631   "Sort threads by root article octet length."
3632   (gnus-article-sort-by-chars
3633    (gnus-thread-header h1) (gnus-thread-header h2)))
3634
3635 (defsubst gnus-article-sort-by-author (h1 h2)
3636   "Sort articles by root author."
3637   (string-lessp
3638    (let ((extract (funcall
3639                    gnus-extract-address-components
3640                    (mail-header-from h1))))
3641      (or (car extract) (cadr extract) ""))
3642    (let ((extract (funcall
3643                    gnus-extract-address-components
3644                    (mail-header-from h2))))
3645      (or (car extract) (cadr extract) ""))))
3646
3647 (defun gnus-thread-sort-by-author (h1 h2)
3648   "Sort threads by root author."
3649   (gnus-article-sort-by-author
3650    (gnus-thread-header h1)  (gnus-thread-header h2)))
3651
3652 (defsubst gnus-article-sort-by-subject (h1 h2)
3653   "Sort articles by root subject."
3654   (string-lessp
3655    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3656    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3657
3658 (defun gnus-thread-sort-by-subject (h1 h2)
3659   "Sort threads by root subject."
3660   (gnus-article-sort-by-subject
3661    (gnus-thread-header h1) (gnus-thread-header h2)))
3662
3663 (defsubst gnus-article-sort-by-date (h1 h2)
3664   "Sort articles by root article date."
3665   (time-less-p
3666    (gnus-date-get-time (mail-header-date h1))
3667    (gnus-date-get-time (mail-header-date h2))))
3668
3669 (defun gnus-thread-sort-by-date (h1 h2)
3670   "Sort threads by root article date."
3671   (gnus-article-sort-by-date
3672    (gnus-thread-header h1) (gnus-thread-header h2)))
3673
3674 (defsubst gnus-article-sort-by-score (h1 h2)
3675   "Sort articles by root article score.
3676 Unscored articles will be counted as having a score of zero."
3677   (> (or (cdr (assq (mail-header-number h1)
3678                     gnus-newsgroup-scored))
3679          gnus-summary-default-score 0)
3680      (or (cdr (assq (mail-header-number h2)
3681                     gnus-newsgroup-scored))
3682          gnus-summary-default-score 0)))
3683
3684 (defun gnus-thread-sort-by-score (h1 h2)
3685   "Sort threads by root article score."
3686   (gnus-article-sort-by-score
3687    (gnus-thread-header h1) (gnus-thread-header h2)))
3688
3689 (defun gnus-thread-sort-by-total-score (h1 h2)
3690   "Sort threads by the sum of all scores in the thread.
3691 Unscored articles will be counted as having a score of zero."
3692   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3693
3694 (defun gnus-thread-total-score (thread)
3695   ;; This function find the total score of THREAD.
3696   (cond ((null thread)
3697          0)
3698         ((consp thread)
3699          (if (stringp (car thread))
3700              (apply gnus-thread-score-function 0
3701                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3702            (gnus-thread-total-score-1 thread)))
3703         (t
3704          (gnus-thread-total-score-1 (list thread)))))
3705
3706 (defun gnus-thread-total-score-1 (root)
3707   ;; This function find the total score of the thread below ROOT.
3708   (setq root (car root))
3709   (apply gnus-thread-score-function
3710          (or (append
3711               (mapcar 'gnus-thread-total-score
3712                       (cdr (gnus-id-to-thread (mail-header-id root))))
3713               (when (> (mail-header-number root) 0)
3714                 (list (or (cdr (assq (mail-header-number root)
3715                                      gnus-newsgroup-scored))
3716                           gnus-summary-default-score 0))))
3717              (list gnus-summary-default-score)
3718              '(0))))
3719
3720 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3721 (defvar gnus-tmp-prev-subject nil)
3722 (defvar gnus-tmp-false-parent nil)
3723 (defvar gnus-tmp-root-expunged nil)
3724 (defvar gnus-tmp-dummy-line nil)
3725
3726 (defvar gnus-tmp-header)
3727 (defun gnus-extra-header (type &optional header)
3728   "Return the extra header of TYPE."
3729   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3730       ""))
3731
3732 (defun gnus-summary-prepare-threads (threads)
3733   "Prepare summary buffer from THREADS and indentation LEVEL.
3734 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3735 or a straight list of headers."
3736   (gnus-message 7 "Generating summary...")
3737
3738   (setq gnus-newsgroup-threads threads)
3739   (beginning-of-line)
3740
3741   (let ((gnus-tmp-level 0)
3742         (default-score (or gnus-summary-default-score 0))
3743         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3744         thread number subject stack state gnus-tmp-gathered beg-match
3745         new-roots gnus-tmp-new-adopts thread-end
3746         gnus-tmp-header gnus-tmp-unread
3747         gnus-tmp-replied gnus-tmp-subject-or-nil
3748         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3749         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3750         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3751
3752     (setq gnus-tmp-prev-subject nil)
3753
3754     (if (vectorp (car threads))
3755         ;; If this is a straight (sic) list of headers, then a
3756         ;; threaded summary display isn't required, so we just create
3757         ;; an unthreaded one.
3758         (gnus-summary-prepare-unthreaded threads)
3759
3760       ;; Do the threaded display.
3761
3762       (while (or threads stack gnus-tmp-new-adopts new-roots)
3763
3764         (if (and (= gnus-tmp-level 0)
3765                  (or (not stack)
3766                      (= (caar stack) 0))
3767                  (not gnus-tmp-false-parent)
3768                  (or gnus-tmp-new-adopts new-roots))
3769             (if gnus-tmp-new-adopts
3770                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3771                       thread (list (car gnus-tmp-new-adopts))
3772                       gnus-tmp-header (caar thread)
3773                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3774               (when new-roots
3775                 (setq thread (list (car new-roots))
3776                       gnus-tmp-header (caar thread)
3777                       new-roots (cdr new-roots))))
3778
3779           (if threads
3780               ;; If there are some threads, we do them before the
3781               ;; threads on the stack.
3782               (setq thread threads
3783                     gnus-tmp-header (caar thread))
3784             ;; There were no current threads, so we pop something off
3785             ;; the stack.
3786             (setq state (car stack)
3787                   gnus-tmp-level (car state)
3788                   thread (cdr state)
3789                   stack (cdr stack)
3790                   gnus-tmp-header (caar thread))))
3791
3792         (setq gnus-tmp-false-parent nil)
3793         (setq gnus-tmp-root-expunged nil)
3794         (setq thread-end nil)
3795
3796         (if (stringp gnus-tmp-header)
3797             ;; The header is a dummy root.
3798             (cond
3799              ((eq gnus-summary-make-false-root 'adopt)
3800               ;; We let the first article adopt the rest.
3801               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3802                                                (cddar thread)))
3803               (setq gnus-tmp-gathered
3804                     (nconc (mapcar
3805                             (lambda (h) (mail-header-number (car h)))
3806                             (cddar thread))
3807                            gnus-tmp-gathered))
3808               (setq thread (cons (list (caar thread)
3809                                        (cadar thread))
3810                                  (cdr thread)))
3811               (setq gnus-tmp-level -1
3812                     gnus-tmp-false-parent t))
3813              ((eq gnus-summary-make-false-root 'empty)
3814               ;; We print adopted articles with empty subject fields.
3815               (setq gnus-tmp-gathered
3816                     (nconc (mapcar
3817                             (lambda (h) (mail-header-number (car h)))
3818                             (cddar thread))
3819                            gnus-tmp-gathered))
3820               (setq gnus-tmp-level -1))
3821              ((eq gnus-summary-make-false-root 'dummy)
3822               ;; We remember that we probably want to output a dummy
3823               ;; root.
3824               (setq gnus-tmp-dummy-line gnus-tmp-header)
3825               (setq gnus-tmp-prev-subject gnus-tmp-header))
3826              (t
3827               ;; We do not make a root for the gathered
3828               ;; sub-threads at all.
3829               (setq gnus-tmp-level -1)))
3830
3831           (setq number (mail-header-number gnus-tmp-header)
3832                 subject (mail-header-subject gnus-tmp-header))
3833
3834           (cond
3835            ;; If the thread has changed subject, we might want to make
3836            ;; this subthread into a root.
3837            ((and (null gnus-thread-ignore-subject)
3838                  (not (zerop gnus-tmp-level))
3839                  gnus-tmp-prev-subject
3840                  (not (inline
3841                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3842             (setq new-roots (nconc new-roots (list (car thread)))
3843                   thread-end t
3844                   gnus-tmp-header nil))
3845            ;; If the article lies outside the current limit,
3846            ;; then we do not display it.
3847            ((not (memq number gnus-newsgroup-limit))
3848             (setq gnus-tmp-gathered
3849                   (nconc (mapcar
3850                           (lambda (h) (mail-header-number (car h)))
3851                           (cdar thread))
3852                          gnus-tmp-gathered))
3853             (setq gnus-tmp-new-adopts (if (cdar thread)
3854                                           (append gnus-tmp-new-adopts
3855                                                   (cdar thread))
3856                                         gnus-tmp-new-adopts)
3857                   thread-end t
3858                   gnus-tmp-header nil)
3859             (when (zerop gnus-tmp-level)
3860               (setq gnus-tmp-root-expunged t)))
3861            ;; Perhaps this article is to be marked as read?
3862            ((and gnus-summary-mark-below
3863                  (< (or (cdr (assq number gnus-newsgroup-scored))
3864                         default-score)
3865                     gnus-summary-mark-below)
3866                  ;; Don't touch sparse articles.
3867                  (not (gnus-summary-article-sparse-p number))
3868                  (not (gnus-summary-article-ancient-p number)))
3869             (setq gnus-newsgroup-unreads
3870                   (delq number gnus-newsgroup-unreads))
3871             (if gnus-newsgroup-auto-expire
3872                 (push number gnus-newsgroup-expirable)
3873               (push (cons number gnus-low-score-mark)
3874                     gnus-newsgroup-reads))))
3875
3876           (when gnus-tmp-header
3877             ;; We may have an old dummy line to output before this
3878             ;; article.
3879             (when (and gnus-tmp-dummy-line
3880                        (gnus-subject-equal
3881                         gnus-tmp-dummy-line
3882                         (mail-header-subject gnus-tmp-header)))
3883               (gnus-summary-insert-dummy-line
3884                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3885               (setq gnus-tmp-dummy-line nil))
3886
3887             ;; Compute the mark.
3888             (setq gnus-tmp-unread (gnus-article-mark number))
3889
3890             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3891                                   gnus-tmp-header gnus-tmp-level)
3892                   gnus-newsgroup-data)
3893
3894             ;; Actually insert the line.
3895             (setq
3896              gnus-tmp-subject-or-nil
3897              (cond
3898               ((and gnus-thread-ignore-subject
3899                     gnus-tmp-prev-subject
3900                     (not (inline (gnus-subject-equal
3901                                   gnus-tmp-prev-subject subject))))
3902                subject)
3903               ((zerop gnus-tmp-level)
3904                (if (and (eq gnus-summary-make-false-root 'empty)
3905                         (memq number gnus-tmp-gathered)
3906                         gnus-tmp-prev-subject
3907                         (inline (gnus-subject-equal
3908                                  gnus-tmp-prev-subject subject)))
3909                    gnus-summary-same-subject
3910                  subject))
3911               (t gnus-summary-same-subject)))
3912             (if (and (eq gnus-summary-make-false-root 'adopt)
3913                      (= gnus-tmp-level 1)
3914                      (memq number gnus-tmp-gathered))
3915                 (setq gnus-tmp-opening-bracket ?\<
3916                       gnus-tmp-closing-bracket ?\>)
3917               (setq gnus-tmp-opening-bracket ?\[
3918                     gnus-tmp-closing-bracket ?\]))
3919             (setq
3920              gnus-tmp-indentation
3921              (aref gnus-thread-indent-array gnus-tmp-level)
3922              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3923              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3924                                 gnus-summary-default-score 0)
3925              gnus-tmp-score-char
3926              (if (or (null gnus-summary-default-score)
3927                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3928                          gnus-summary-zcore-fuzz))
3929                  ? ;Whitespace
3930                (if (< gnus-tmp-score gnus-summary-default-score)
3931                    gnus-score-below-mark gnus-score-over-mark))
3932              gnus-tmp-replied
3933              (cond ((memq number gnus-newsgroup-processable)
3934                     gnus-process-mark)
3935                    ((memq number gnus-newsgroup-cached)
3936                     gnus-cached-mark)
3937                    ((memq number gnus-newsgroup-replied)
3938                     gnus-replied-mark)
3939                    ((memq number gnus-newsgroup-saved)
3940                     gnus-saved-mark)
3941                    (t gnus-unread-mark))
3942              gnus-tmp-from (mail-header-from gnus-tmp-header)
3943              gnus-tmp-name
3944              (cond
3945               ((string-match "<[^>]+> *$" gnus-tmp-from)
3946                (setq beg-match (match-beginning 0))
3947                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3948                         (substring gnus-tmp-from (1+ (match-beginning 0))
3949                                    (1- (match-end 0))))
3950                    (substring gnus-tmp-from 0 beg-match)))
3951               ((string-match "(.+)" gnus-tmp-from)
3952                (substring gnus-tmp-from
3953                           (1+ (match-beginning 0)) (1- (match-end 0))))
3954               (t gnus-tmp-from)))
3955             (when (string= gnus-tmp-name "")
3956               (setq gnus-tmp-name gnus-tmp-from))
3957             (unless (numberp gnus-tmp-lines)
3958               (setq gnus-tmp-lines 0))
3959             (gnus-put-text-property
3960              (point)
3961              (progn (eval gnus-summary-line-format-spec) (point))
3962              'gnus-number number)
3963             (when gnus-visual-p
3964               (forward-line -1)
3965               (gnus-run-hooks 'gnus-summary-update-hook)
3966               (forward-line 1))
3967
3968             (setq gnus-tmp-prev-subject subject)))
3969
3970         (when (nth 1 thread)
3971           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3972         (incf gnus-tmp-level)
3973         (setq threads (if thread-end nil (cdar thread)))
3974         (unless threads
3975           (setq gnus-tmp-level 0)))))
3976   (gnus-message 7 "Generating summary...done"))
3977
3978 (defun gnus-summary-prepare-unthreaded (headers)
3979   "Generate an unthreaded summary buffer based on HEADERS."
3980   (let (header number mark)
3981
3982     (beginning-of-line)
3983
3984     (while headers
3985       ;; We may have to root out some bad articles...
3986       (when (memq (setq number (mail-header-number
3987                                 (setq header (pop headers))))
3988                   gnus-newsgroup-limit)
3989         ;; Mark article as read when it has a low score.
3990         (when (and gnus-summary-mark-below
3991                    (< (or (cdr (assq number gnus-newsgroup-scored))
3992                           gnus-summary-default-score 0)
3993                       gnus-summary-mark-below)
3994                    (not (gnus-summary-article-ancient-p number)))
3995           (setq gnus-newsgroup-unreads
3996                 (delq number gnus-newsgroup-unreads))
3997           (if gnus-newsgroup-auto-expire
3998               (push number gnus-newsgroup-expirable)
3999             (push (cons number gnus-low-score-mark)
4000                   gnus-newsgroup-reads)))
4001
4002         (setq mark (gnus-article-mark number))
4003         (push (gnus-data-make number mark (1+ (point)) header 0)
4004               gnus-newsgroup-data)
4005         (gnus-summary-insert-line
4006          header 0 number
4007          mark (memq number gnus-newsgroup-replied)
4008          (memq number gnus-newsgroup-expirable)
4009          (mail-header-subject header) nil
4010          (cdr (assq number gnus-newsgroup-scored))
4011          (memq number gnus-newsgroup-processable))))))
4012
4013 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4014   "Select newsgroup GROUP.
4015 If READ-ALL is non-nil, all articles in the group are selected.
4016 If SELECT-ARTICLES, only select those articles from GROUP."
4017   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4018          ;;!!! Dirty hack; should be removed.
4019          (gnus-summary-ignore-duplicates
4020           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4021               t
4022             gnus-summary-ignore-duplicates))
4023          (info (nth 2 entry))
4024          articles fetched-articles cached)
4025
4026     (unless (gnus-check-server
4027              (setq gnus-current-select-method
4028                    (gnus-find-method-for-group group)))
4029       (error "Couldn't open server"))
4030
4031     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4032         (gnus-activate-group group)     ; Or we can activate it...
4033         (progn                          ; Or we bug out.
4034           (when (equal major-mode 'gnus-summary-mode)
4035             (kill-buffer (current-buffer)))
4036           (error "Couldn't request group %s: %s"
4037                  group (gnus-status-message group))))
4038
4039     (unless (gnus-request-group group t)
4040       (when (equal major-mode 'gnus-summary-mode)
4041         (kill-buffer (current-buffer)))
4042       (error "Couldn't request group %s: %s"
4043              group (gnus-status-message group)))
4044
4045     (setq gnus-newsgroup-name group)
4046     (setq gnus-newsgroup-unselected nil)
4047     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4048     (gnus-summary-setup-default-charset)
4049
4050     ;; Adjust and set lists of article marks.
4051     (when info
4052       (gnus-adjust-marked-articles info))
4053
4054     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4055     (when (gnus-virtual-group-p group)
4056       (setq cached gnus-newsgroup-cached))
4057
4058     (setq gnus-newsgroup-unreads
4059           (gnus-set-difference
4060            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4061            gnus-newsgroup-dormant))
4062
4063     (setq gnus-newsgroup-processable nil)
4064
4065     (gnus-update-read-articles group gnus-newsgroup-unreads)
4066
4067     (if (setq articles select-articles)
4068         (setq gnus-newsgroup-unselected
4069               (gnus-sorted-intersection
4070                gnus-newsgroup-unreads
4071                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4072       (setq articles (gnus-articles-to-read group read-all)))
4073
4074     (cond
4075      ((null articles)
4076       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4077       'quit)
4078      ((eq articles 0) nil)
4079      (t
4080       ;; Init the dependencies hash table.
4081       (setq gnus-newsgroup-dependencies
4082             (gnus-make-hashtable (length articles)))
4083       (gnus-set-global-variables)
4084       ;; Retrieve the headers and read them in.
4085       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4086       (setq gnus-newsgroup-headers
4087             (if (eq 'nov
4088                     (setq gnus-headers-retrieved-by
4089                           (gnus-retrieve-headers
4090                            articles gnus-newsgroup-name
4091                            ;; We might want to fetch old headers, but
4092                            ;; not if there is only 1 article.
4093                            (and (or (and
4094                                      (not (eq gnus-fetch-old-headers 'some))
4095                                      (not (numberp gnus-fetch-old-headers)))
4096                                     (> (length articles) 1))
4097                                 gnus-fetch-old-headers))))
4098                 (gnus-get-newsgroup-headers-xover
4099                  articles nil nil gnus-newsgroup-name t)
4100               (gnus-get-newsgroup-headers)))
4101       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4102
4103       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4104       (when cached
4105         (setq gnus-newsgroup-cached cached))
4106
4107       ;; Suppress duplicates?
4108       (when gnus-suppress-duplicates
4109         (gnus-dup-suppress-articles))
4110
4111       ;; Set the initial limit.
4112       (setq gnus-newsgroup-limit (copy-sequence articles))
4113       ;; Remove canceled articles from the list of unread articles.
4114       (setq gnus-newsgroup-unreads
4115             (gnus-set-sorted-intersection
4116              gnus-newsgroup-unreads
4117              (setq fetched-articles
4118                    (mapcar (lambda (headers) (mail-header-number headers))
4119                            gnus-newsgroup-headers))))
4120       ;; Removed marked articles that do not exist.
4121       (gnus-update-missing-marks
4122        (gnus-sorted-complement fetched-articles articles))
4123       ;; We might want to build some more threads first.
4124       (when (and gnus-fetch-old-headers
4125                  (eq gnus-headers-retrieved-by 'nov))
4126         (if (eq gnus-fetch-old-headers 'invisible)
4127             (gnus-build-all-threads)
4128           (gnus-build-old-threads)))
4129       ;; Let the Gnus agent mark articles as read.
4130       (when gnus-agent
4131         (gnus-agent-get-undownloaded-list))
4132       ;; Check whether auto-expire is to be done in this group.
4133       (setq gnus-newsgroup-auto-expire
4134             (gnus-group-auto-expirable-p group))
4135       ;; Set up the article buffer now, if necessary.
4136       (unless gnus-single-article-buffer
4137         (gnus-article-setup-buffer))
4138       ;; First and last article in this newsgroup.
4139       (when gnus-newsgroup-headers
4140         (setq gnus-newsgroup-begin
4141               (mail-header-number (car gnus-newsgroup-headers))
4142               gnus-newsgroup-end
4143               (mail-header-number
4144                (gnus-last-element gnus-newsgroup-headers))))
4145       ;; GROUP is successfully selected.
4146       (or gnus-newsgroup-headers t)))))
4147
4148 (defun gnus-articles-to-read (group &optional read-all)
4149   ;; Find out what articles the user wants to read.
4150   (let* ((articles
4151           ;; Select all articles if `read-all' is non-nil, or if there
4152           ;; are no unread articles.
4153           (if (or read-all
4154                   (and (zerop (length gnus-newsgroup-marked))
4155                        (zerop (length gnus-newsgroup-unreads)))
4156                   (eq (gnus-group-find-parameter group 'display)
4157                       'all))
4158               (gnus-uncompress-range (gnus-active group))
4159             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4160                           (copy-sequence gnus-newsgroup-unreads))
4161                   '<)))
4162          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4163          (scored (length scored-list))
4164          (number (length articles))
4165          (marked (+ (length gnus-newsgroup-marked)
4166                     (length gnus-newsgroup-dormant)))
4167          (select
4168           (cond
4169            ((numberp read-all)
4170             read-all)
4171            (t
4172             (condition-case ()
4173                 (cond
4174                  ((and (or (<= scored marked) (= scored number))
4175                        (numberp gnus-large-newsgroup)
4176                        (> number gnus-large-newsgroup))
4177                   (let ((input
4178                          (read-string
4179                           (format
4180                            "How many articles from %s (default %d): "
4181                            (gnus-limit-string gnus-newsgroup-name 35)
4182                            number))))
4183                     (if (string-match "^[ \t]*$" input) number input)))
4184                  ((and (> scored marked) (< scored number)
4185                        (> (- scored number) 20))
4186                   (let ((input
4187                          (read-string
4188                           (format "%s %s (%d scored, %d total): "
4189                                   "How many articles from"
4190                                   group scored number))))
4191                     (if (string-match "^[ \t]*$" input)
4192                         number input)))
4193                  (t number))
4194               (quit nil))))))
4195     (setq select (if (stringp select) (string-to-number select) select))
4196     (if (or (null select) (zerop select))
4197         select
4198       (if (and (not (zerop scored)) (<= (abs select) scored))
4199           (progn
4200             (setq articles (sort scored-list '<))
4201             (setq number (length articles)))
4202         (setq articles (copy-sequence articles)))
4203
4204       (when (< (abs select) number)
4205         (if (< select 0)
4206             ;; Select the N oldest articles.
4207             (setcdr (nthcdr (1- (abs select)) articles) nil)
4208           ;; Select the N most recent articles.
4209           (setq articles (nthcdr (- number select) articles))))
4210       (setq gnus-newsgroup-unselected
4211             (gnus-sorted-intersection
4212              gnus-newsgroup-unreads
4213              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4214       articles)))
4215
4216 (defun gnus-killed-articles (killed articles)
4217   (let (out)
4218     (while articles
4219       (when (inline (gnus-member-of-range (car articles) killed))
4220         (push (car articles) out))
4221       (setq articles (cdr articles)))
4222     out))
4223
4224 (defun gnus-uncompress-marks (marks)
4225   "Uncompress the mark ranges in MARKS."
4226   (let ((uncompressed '(score bookmark))
4227         out)
4228     (while marks
4229       (if (memq (caar marks) uncompressed)
4230           (push (car marks) out)
4231         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4232       (setq marks (cdr marks)))
4233     out))
4234
4235 (defun gnus-adjust-marked-articles (info)
4236   "Set all article lists and remove all marks that are no longer valid."
4237   (let* ((marked-lists (gnus-info-marks info))
4238          (active (gnus-active (gnus-info-group info)))
4239          (min (car active))
4240          (max (cdr active))
4241          (types gnus-article-mark-lists)
4242          (uncompressed '(score bookmark killed))
4243          marks var articles article mark)
4244
4245     (while marked-lists
4246       (setq marks (pop marked-lists))
4247       (set (setq var (intern (format "gnus-newsgroup-%s"
4248                                      (car (rassq (setq mark (car marks))
4249                                                  types)))))
4250            (if (memq (car marks) uncompressed) (cdr marks)
4251              (gnus-uncompress-range (cdr marks))))
4252
4253       (setq articles (symbol-value var))
4254
4255       ;; All articles have to be subsets of the active articles.
4256       (cond
4257        ;; Adjust "simple" lists.
4258        ((memq mark '(tick dormant expire reply save))
4259         (while articles
4260           (when (or (< (setq article (pop articles)) min) (> article max))
4261             (set var (delq article (symbol-value var))))))
4262        ;; Adjust assocs.
4263        ((memq mark uncompressed)
4264         (when (not (listp (cdr (symbol-value var))))
4265           (set var (list (symbol-value var))))
4266         (when (not (listp (cdr articles)))
4267           (setq articles (list articles)))
4268         (while articles
4269           (when (or (not (consp (setq article (pop articles))))
4270                     (< (car article) min)
4271                     (> (car article) max))
4272             (set var (delq article (symbol-value var))))))))))
4273
4274 (defun gnus-update-missing-marks (missing)
4275   "Go through the list of MISSING articles and remove them from the mark lists."
4276   (when missing
4277     (let ((types gnus-article-mark-lists)
4278           var m)
4279       ;; Go through all types.
4280       (while types
4281         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4282         (when (symbol-value var)
4283           ;; This list has articles.  So we delete all missing articles
4284           ;; from it.
4285           (setq m missing)
4286           (while m
4287             (set var (delq (pop m) (symbol-value var)))))))))
4288
4289 (defun gnus-update-marks ()
4290   "Enter the various lists of marked articles into the newsgroup info list."
4291   (let ((types gnus-article-mark-lists)
4292         (info (gnus-get-info gnus-newsgroup-name))
4293         (uncompressed '(score bookmark killed))
4294         type list newmarked symbol delta-marks)
4295     (when info
4296       ;; Add all marks lists to the list of marks lists.
4297       (while (setq type (pop types))
4298         (setq list (symbol-value
4299                           (setq symbol
4300                                 (intern (format "gnus-newsgroup-%s"
4301                                                 (car type))))))
4302
4303         (when list
4304           ;; Get rid of the entries of the articles that have the
4305           ;; default score.
4306           (when (and (eq (cdr type) 'score)
4307                      gnus-save-score
4308                      list)
4309             (let* ((arts list)
4310                    (prev (cons nil list))
4311                    (all prev))
4312               (while arts
4313                 (if (or (not (consp (car arts)))
4314                         (= (cdar arts) gnus-summary-default-score))
4315                     (setcdr prev (cdr arts))
4316                   (setq prev arts))
4317                 (setq arts (cdr arts)))
4318               (setq list (cdr all)))))
4319
4320        (or (memq (cdr type) uncompressed)
4321            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4322        
4323        (when (gnus-check-backend-function 'request-set-mark
4324                                           gnus-newsgroup-name)
4325          ;; uncompressed:s are not proper flags (they are cons cells)
4326          ;; cache is a internal gnus flag
4327          (unless (memq (cdr type) (cons 'cache uncompressed))
4328            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4329                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4330                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4331              (if add
4332                  (push (list add 'add (list (cdr type))) delta-marks))
4333              (if del
4334                  (push (list del 'del (list (cdr type))) delta-marks)))))
4335           
4336         (when list
4337          (push (cons (cdr type) list) newmarked)))
4338
4339       (when delta-marks
4340         (unless (gnus-check-group gnus-newsgroup-name)
4341           (error "Can't open server for %s" gnus-newsgroup-name))
4342         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4343           
4344       ;; Enter these new marks into the info of the group.
4345       (if (nthcdr 3 info)
4346           (setcar (nthcdr 3 info) newmarked)
4347         ;; Add the marks lists to the end of the info.
4348         (when newmarked
4349           (setcdr (nthcdr 2 info) (list newmarked))))
4350
4351       ;; Cut off the end of the info if there's nothing else there.
4352       (let ((i 5))
4353         (while (and (> i 2)
4354                     (not (nth i info)))
4355           (when (nthcdr (decf i) info)
4356             (setcdr (nthcdr i info) nil)))))))
4357
4358 (defun gnus-set-mode-line (where)
4359   "This function sets the mode line of the article or summary buffers.
4360 If WHERE is `summary', the summary mode line format will be used."
4361   ;; Is this mode line one we keep updated?
4362   (when (and (memq where gnus-updated-mode-lines)
4363              (symbol-value
4364               (intern (format "gnus-%s-mode-line-format-spec" where))))
4365     (let (mode-string)
4366       (save-excursion
4367         ;; We evaluate this in the summary buffer since these
4368         ;; variables are buffer-local to that buffer.
4369         (set-buffer gnus-summary-buffer)
4370         ;; We bind all these variables that are used in the `eval' form
4371         ;; below.
4372         (let* ((mformat (symbol-value
4373                          (intern
4374                           (format "gnus-%s-mode-line-format-spec" where))))
4375                (gnus-tmp-group-name gnus-newsgroup-name)
4376                (gnus-tmp-article-number (or gnus-current-article 0))
4377                (gnus-tmp-unread gnus-newsgroup-unreads)
4378                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4379                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4380                (gnus-tmp-unread-and-unselected
4381                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4382                             (zerop gnus-tmp-unselected))
4383                        "")
4384                       ((zerop gnus-tmp-unselected)
4385                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4386                       (t (format "{%d(+%d) more}"
4387                                  gnus-tmp-unread-and-unticked
4388                                  gnus-tmp-unselected))))
4389                (gnus-tmp-subject
4390                 (if (and gnus-current-headers
4391                          (vectorp gnus-current-headers))
4392                     (gnus-mode-string-quote
4393                      (mail-header-subject gnus-current-headers))
4394                   ""))
4395                bufname-length max-len
4396                gnus-tmp-header);; passed as argument to any user-format-funcs
4397           (setq mode-string (eval mformat))
4398           (setq bufname-length (if (string-match "%b" mode-string)
4399                                    (- (length
4400                                        (buffer-name
4401                                         (if (eq where 'summary)
4402                                             nil
4403                                           (get-buffer gnus-article-buffer))))
4404                                       2)
4405                                  0))
4406           (setq max-len (max 4 (if gnus-mode-non-string-length
4407                                    (- (window-width)
4408                                       gnus-mode-non-string-length
4409                                       bufname-length)
4410                                  (length mode-string))))
4411           ;; We might have to chop a bit of the string off...
4412           (when (> (length mode-string) max-len)
4413             (setq mode-string
4414                   (concat (truncate-string-to-width mode-string (- max-len 3))
4415                           "...")))
4416           ;; Pad the mode string a bit.
4417           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4418       ;; Update the mode line.
4419       (setq mode-line-buffer-identification
4420             (gnus-mode-line-buffer-identification (list mode-string)))
4421       (set-buffer-modified-p t))))
4422
4423 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4424   "Go through the HEADERS list and add all Xrefs to a hash table.
4425 The resulting hash table is returned, or nil if no Xrefs were found."
4426   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4427          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4428          (xref-hashtb (gnus-make-hashtable))
4429          start group entry number xrefs header)
4430     (while headers
4431       (setq header (pop headers))
4432       (when (and (setq xrefs (mail-header-xref header))
4433                  (not (memq (setq number (mail-header-number header))
4434                             unreads)))
4435         (setq start 0)
4436         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4437           (setq start (match-end 0))
4438           (setq group (if prefix
4439                           (concat prefix (substring xrefs (match-beginning 1)
4440                                                     (match-end 1)))
4441                         (substring xrefs (match-beginning 1) (match-end 1))))
4442           (setq number
4443                 (string-to-int (substring xrefs (match-beginning 2)
4444                                           (match-end 2))))
4445           (if (setq entry (gnus-gethash group xref-hashtb))
4446               (setcdr entry (cons number (cdr entry)))
4447             (gnus-sethash group (cons number nil) xref-hashtb)))))
4448     (and start xref-hashtb)))
4449
4450 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4451   "Look through all the headers and mark the Xrefs as read."
4452   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4453         name entry info xref-hashtb idlist method nth4)
4454     (save-excursion
4455       (set-buffer gnus-group-buffer)
4456       (when (setq xref-hashtb
4457                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4458         (mapatoms
4459          (lambda (group)
4460            (unless (string= from-newsgroup (setq name (symbol-name group)))
4461              (setq idlist (symbol-value group))
4462              ;; Dead groups are not updated.
4463              (and (prog1
4464                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4465                             info (nth 2 entry))
4466                     (when (stringp (setq nth4 (gnus-info-method info)))
4467                       (setq nth4 (gnus-server-to-method nth4))))
4468                   ;; Only do the xrefs if the group has the same
4469                   ;; select method as the group we have just read.
4470                   (or (gnus-methods-equal-p
4471                        nth4 (gnus-find-method-for-group from-newsgroup))
4472                       virtual
4473                       (equal nth4 (setq method (gnus-find-method-for-group
4474                                                 from-newsgroup)))
4475                       (and (equal (car nth4) (car method))
4476                            (equal (nth 1 nth4) (nth 1 method))))
4477                   gnus-use-cross-reference
4478                   (or (not (eq gnus-use-cross-reference t))
4479                       virtual
4480                       ;; Only do cross-references on subscribed
4481                       ;; groups, if that is what is wanted.
4482                       (<= (gnus-info-level info) gnus-level-subscribed))
4483                   (gnus-group-make-articles-read name idlist))))
4484          xref-hashtb)))))
4485
4486 (defun gnus-compute-read-articles (group articles)
4487   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4488          (info (nth 2 entry))
4489          (active (gnus-active group))
4490          ninfo)
4491     (when entry
4492       ;; First peel off all invalid article numbers.
4493       (when active
4494         (let ((ids articles)
4495               id first)
4496           (while (setq id (pop ids))
4497             (when (and first (> id (cdr active)))
4498               ;; We'll end up in this situation in one particular
4499               ;; obscure situation.  If you re-scan a group and get
4500               ;; a new article that is cross-posted to a different
4501               ;; group that has not been re-scanned, you might get
4502               ;; crossposted article that has a higher number than
4503               ;; Gnus believes possible.  So we re-activate this
4504               ;; group as well.  This might mean doing the
4505               ;; crossposting thingy will *increase* the number
4506               ;; of articles in some groups.  Tsk, tsk.
4507               (setq active (or (gnus-activate-group group) active)))
4508             (when (or (> id (cdr active))
4509                       (< id (car active)))
4510               (setq articles (delq id articles))))))
4511       ;; If the read list is nil, we init it.
4512       (if (and active
4513                (null (gnus-info-read info))
4514                (> (car active) 1))
4515           (setq ninfo (cons 1 (1- (car active))))
4516         (setq ninfo (gnus-info-read info)))
4517       ;; Then we add the read articles to the range.
4518       (gnus-add-to-range
4519        ninfo (setq articles (sort articles '<))))))
4520
4521 (defun gnus-group-make-articles-read (group articles)
4522   "Update the info of GROUP to say that ARTICLES are read."
4523   (let* ((num 0)
4524          (entry (gnus-gethash group gnus-newsrc-hashtb))
4525          (info (nth 2 entry))
4526          (active (gnus-active group))
4527          range)
4528     (when entry
4529       (setq range (gnus-compute-read-articles group articles))
4530       (save-excursion
4531         (set-buffer gnus-group-buffer)
4532         (gnus-undo-register
4533           `(progn
4534              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4535              (gnus-info-set-read ',info ',(gnus-info-read info))
4536              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4537              (gnus-group-update-group ,group t))))
4538       ;; Add the read articles to the range.
4539       (gnus-info-set-read info range)
4540       ;; Then we have to re-compute how many unread
4541       ;; articles there are in this group.
4542       (when active
4543         (cond
4544          ((not range)
4545           (setq num (- (1+ (cdr active)) (car active))))
4546          ((not (listp (cdr range)))
4547           (setq num (- (cdr active) (- (1+ (cdr range))
4548                                        (car range)))))
4549          (t
4550           (while range
4551             (if (numberp (car range))
4552                 (setq num (1+ num))
4553               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4554             (setq range (cdr range)))
4555           (setq num (- (cdr active) num))))
4556         ;; Update the number of unread articles.
4557         (setcar entry num)
4558         ;; Update the group buffer.
4559         (gnus-group-update-group group t)))))
4560
4561 (defvar gnus-newsgroup-none-id 0)
4562
4563 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4564   (let ((cur nntp-server-buffer)
4565         (dependencies
4566          (or dependencies
4567              (save-excursion (set-buffer gnus-summary-buffer)
4568                              gnus-newsgroup-dependencies)))
4569         headers id end ref
4570         (mail-parse-charset gnus-newsgroup-charset)
4571         (mail-parse-ignored-charsets 
4572              (save-excursion (set-buffer gnus-summary-buffer)
4573                              gnus-newsgroup-ignored-charsets)))
4574     (save-excursion
4575       (set-buffer nntp-server-buffer)
4576       ;; Translate all TAB characters into SPACE characters.
4577       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4578       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4579       (gnus-run-hooks 'gnus-parse-headers-hook)
4580       (let ((case-fold-search t)
4581             in-reply-to header p lines chars)
4582         (goto-char (point-min))
4583         ;; Search to the beginning of the next header.  Error messages
4584         ;; do not begin with 2 or 3.
4585         (while (re-search-forward "^[23][0-9]+ " nil t)
4586           (setq id nil
4587                 ref nil)
4588           ;; This implementation of this function, with nine
4589           ;; search-forwards instead of the one re-search-forward and
4590           ;; a case (which basically was the old function) is actually
4591           ;; about twice as fast, even though it looks messier.  You
4592           ;; can't have everything, I guess.  Speed and elegance
4593           ;; doesn't always go hand in hand.
4594           (setq
4595            header
4596            (vector
4597             ;; Number.
4598             (prog1
4599                 (read cur)
4600               (end-of-line)
4601               (setq p (point))
4602               (narrow-to-region (point)
4603                                 (or (and (search-forward "\n.\n" nil t)
4604                                          (- (point) 2))
4605                                     (point))))
4606             ;; Subject.
4607             (progn
4608               (goto-char p)
4609               (if (search-forward "\nsubject: " nil t)
4610                   (funcall gnus-decode-encoded-word-function
4611                            (nnheader-header-value))
4612                 "(none)"))
4613             ;; From.
4614             (progn
4615               (goto-char p)
4616               (if (search-forward "\nfrom: " nil t)
4617                   (funcall gnus-decode-encoded-word-function
4618                            (nnheader-header-value))
4619                 "(nobody)"))
4620             ;; Date.
4621             (progn
4622               (goto-char p)
4623               (if (search-forward "\ndate: " nil t)
4624                   (nnheader-header-value) ""))
4625             ;; Message-ID.
4626             (progn
4627               (goto-char p)
4628               (setq id (if (re-search-forward
4629                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4630                            ;; We do it this way to make sure the Message-ID
4631                            ;; is (somewhat) syntactically valid.
4632                            (buffer-substring (match-beginning 1)
4633                                              (match-end 1))
4634                          ;; If there was no message-id, we just fake one
4635                          ;; to make subsequent routines simpler.
4636                          (nnheader-generate-fake-message-id))))
4637             ;; References.
4638             (progn
4639               (goto-char p)
4640               (if (search-forward "\nreferences: " nil t)
4641                   (progn
4642                     (setq end (point))
4643                     (prog1
4644                         (nnheader-header-value)
4645                       (setq ref
4646                             (buffer-substring
4647                              (progn
4648                                (end-of-line)
4649                                (search-backward ">" end t)
4650                                (1+ (point)))
4651                              (progn
4652                                (search-backward "<" end t)
4653                                (point))))))
4654                 ;; Get the references from the in-reply-to header if there
4655                 ;; were no references and the in-reply-to header looks
4656                 ;; promising.
4657                 (if (and (search-forward "\nin-reply-to: " nil t)
4658                          (setq in-reply-to (nnheader-header-value))
4659                          (string-match "<[^>]+>" in-reply-to))
4660                     (let (ref2)
4661                       (setq ref (substring in-reply-to (match-beginning 0)
4662                                            (match-end 0)))
4663                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4664                         (setq ref2 (substring in-reply-to (match-beginning 0)
4665                                               (match-end 0)))
4666                         (when (> (length ref2) (length ref))
4667                           (setq ref ref2)))
4668                       ref)
4669                   (setq ref nil))))
4670             ;; Chars.
4671             (progn
4672               (goto-char p)
4673               (if (search-forward "\nchars: " nil t)
4674                   (if (numberp (setq chars (ignore-errors (read cur))))
4675                       chars 0)
4676                 0))
4677             ;; Lines.
4678             (progn
4679               (goto-char p)
4680               (if (search-forward "\nlines: " nil t)
4681                   (if (numberp (setq lines (ignore-errors (read cur))))
4682                       lines 0)
4683                 0))
4684             ;; Xref.
4685             (progn
4686               (goto-char p)
4687               (and (search-forward "\nxref: " nil t)
4688                    (nnheader-header-value)))
4689             ;; Extra.
4690             (when gnus-extra-headers
4691               (let ((extra gnus-extra-headers)
4692                     out)
4693                 (while extra
4694                   (goto-char p)
4695                   (when (search-forward
4696                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4697                     (push (cons (car extra) (nnheader-header-value))
4698                           out))
4699                   (pop extra))
4700                 out))))
4701           (when (equal id ref)
4702             (setq ref nil))
4703
4704           (when gnus-alter-header-function
4705             (funcall gnus-alter-header-function header)
4706             (setq id (mail-header-id header)
4707                   ref (gnus-parent-id (mail-header-references header))))
4708
4709           (when (setq header
4710                       (gnus-dependencies-add-header
4711                        header dependencies force-new))
4712             (push header headers))
4713           (goto-char (point-max))
4714           (widen))
4715         (nreverse headers)))))
4716
4717 ;; Goes through the xover lines and returns a list of vectors
4718 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4719                                                   force-new dependencies
4720                                                   group also-fetch-heads)
4721   "Parse the news overview data in the server buffer, and return a
4722 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4723   ;; Get the Xref when the users reads the articles since most/some
4724   ;; NNTP servers do not include Xrefs when using XOVER.
4725   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4726   (let ((mail-parse-charset gnus-newsgroup-charset)
4727         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4728         (cur nntp-server-buffer)
4729         (dependencies (or dependencies gnus-newsgroup-dependencies))
4730         number headers header)
4731     (save-excursion
4732       (set-buffer nntp-server-buffer)
4733       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4734       ;; Allow the user to mangle the headers before parsing them.
4735       (gnus-run-hooks 'gnus-parse-headers-hook)
4736       (goto-char (point-min))
4737       (while (not (eobp))
4738         (condition-case ()
4739             (while (and sequence (not (eobp)))
4740               (setq number (read cur))
4741               (while (and sequence
4742                           (< (car sequence) number))
4743                 (setq sequence (cdr sequence)))
4744               (and sequence
4745                    (eq number (car sequence))
4746                    (progn
4747                      (setq sequence (cdr sequence))
4748                      (setq header (inline
4749                                     (gnus-nov-parse-line
4750                                      number dependencies force-new))))
4751                    (push header headers))
4752               (forward-line 1))
4753           (error
4754            (gnus-error 4 "Strange nov line (%d)"
4755                        (count-lines (point-min) (point)))))
4756         (forward-line 1))
4757       ;; A common bug in inn is that if you have posted an article and
4758       ;; then retrieves the active file, it will answer correctly --
4759       ;; the new article is included.  However, a NOV entry for the
4760       ;; article may not have been generated yet, so this may fail.
4761       ;; We work around this problem by retrieving the last few
4762       ;; headers using HEAD.
4763       (if (or (not also-fetch-heads)
4764               (not sequence))
4765           ;; We (probably) got all the headers.
4766           (nreverse headers)
4767         (let ((gnus-nov-is-evil t))
4768           (nconc
4769            (nreverse headers)
4770            (when (gnus-retrieve-headers sequence group)
4771              (gnus-get-newsgroup-headers))))))))
4772
4773 (defun gnus-article-get-xrefs ()
4774   "Fill in the Xref value in `gnus-current-headers', if necessary.
4775 This is meant to be called in `gnus-article-internal-prepare-hook'."
4776   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4777                                  gnus-current-headers)))
4778     (or (not gnus-use-cross-reference)
4779         (not headers)
4780         (and (mail-header-xref headers)
4781              (not (string= (mail-header-xref headers) "")))
4782         (let ((case-fold-search t)
4783               xref)
4784           (save-restriction
4785             (nnheader-narrow-to-headers)
4786             (goto-char (point-min))
4787             (when (or (and (eq (downcase (char-after)) ?x)
4788                            (looking-at "Xref:"))
4789                       (search-forward "\nXref:" nil t))
4790               (goto-char (1+ (match-end 0)))
4791               (setq xref (buffer-substring (point)
4792                                            (progn (end-of-line) (point))))
4793               (mail-header-set-xref headers xref)))))))
4794
4795 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4796   "Find article ID and insert the summary line for that article.
4797 OLD-HEADER can either be a header or a line number to insert
4798 the subject line on."
4799   (let* ((line (and (numberp old-header) old-header))
4800          (old-header (and (vectorp old-header) old-header))
4801          (header (cond ((and old-header use-old-header)
4802                         old-header)
4803                        ((and (numberp id)
4804                              (gnus-number-to-header id))
4805                         (gnus-number-to-header id))
4806                        (t
4807                         (gnus-read-header id))))
4808          (number (and (numberp id) id))
4809          d)
4810     (when header
4811       ;; Rebuild the thread that this article is part of and go to the
4812       ;; article we have fetched.
4813       (when (and (not gnus-show-threads)
4814                  old-header)
4815         (when (and number
4816                    (setq d (gnus-data-find (mail-header-number old-header))))
4817           (goto-char (gnus-data-pos d))
4818           (gnus-data-remove
4819            number
4820            (- (gnus-point-at-bol)
4821               (prog1
4822                   (1+ (gnus-point-at-eol))
4823                 (gnus-delete-line))))))
4824       (when old-header
4825         (mail-header-set-number header (mail-header-number old-header)))
4826       (setq gnus-newsgroup-sparse
4827             (delq (setq number (mail-header-number header))
4828                   gnus-newsgroup-sparse))
4829       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4830       (push number gnus-newsgroup-limit)
4831       (gnus-rebuild-thread (mail-header-id header) line)
4832       (gnus-summary-goto-subject number nil t))
4833     (when (and (numberp number)
4834                (> number 0))
4835       ;; We have to update the boundaries even if we can't fetch the
4836       ;; article if ID is a number -- so that the next `P' or `N'
4837       ;; command will fetch the previous (or next) article even
4838       ;; if the one we tried to fetch this time has been canceled.
4839       (when (> number gnus-newsgroup-end)
4840         (setq gnus-newsgroup-end number))
4841       (when (< number gnus-newsgroup-begin)
4842         (setq gnus-newsgroup-begin number))
4843       (setq gnus-newsgroup-unselected
4844             (delq number gnus-newsgroup-unselected)))
4845     ;; Report back a success?
4846     (and header (mail-header-number header))))
4847
4848 ;;; Process/prefix in the summary buffer
4849
4850 (defun gnus-summary-work-articles (n)
4851   "Return a list of articles to be worked upon.
4852 The prefix argument, the list of process marked articles, and the
4853 current article will be taken into consideration."
4854   (save-excursion
4855     (set-buffer gnus-summary-buffer)
4856     (cond
4857      (n
4858       ;; A numerical prefix has been given.
4859       (setq n (prefix-numeric-value n))
4860       (let ((backward (< n 0))
4861             (n (abs (prefix-numeric-value n)))
4862             articles article)
4863         (save-excursion
4864           (while
4865               (and (> n 0)
4866                    (push (setq article (gnus-summary-article-number))
4867                          articles)
4868                    (if backward
4869                        (gnus-summary-find-prev nil article)
4870                      (gnus-summary-find-next nil article)))
4871             (decf n)))
4872         (nreverse articles)))
4873      ((and (gnus-region-active-p) (mark))
4874       (message "region active")
4875       ;; Work on the region between point and mark.
4876       (let ((max (max (point) (mark)))
4877             articles article)
4878         (save-excursion
4879           (goto-char (min (min (point) (mark))))
4880           (while
4881               (and
4882                (push (setq article (gnus-summary-article-number)) articles)
4883                (gnus-summary-find-next nil article)
4884                (< (point) max)))
4885           (nreverse articles))))
4886      (gnus-newsgroup-processable
4887       ;; There are process-marked articles present.
4888       ;; Save current state.
4889       (gnus-summary-save-process-mark)
4890       ;; Return the list.
4891       (reverse gnus-newsgroup-processable))
4892      (t
4893       ;; Just return the current article.
4894       (list (gnus-summary-article-number))))))
4895
4896 (defmacro gnus-summary-iterate (arg &rest forms)
4897   "Iterate over the process/prefixed articles and do FORMS.
4898 ARG is the interactive prefix given to the command.  FORMS will be
4899 executed with point over the summary line of the articles."
4900   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4901     `(let ((,articles (gnus-summary-work-articles ,arg)))
4902        (while ,articles
4903          (gnus-summary-goto-subject (car ,articles))
4904          ,@forms
4905          (pop ,articles)))))
4906
4907 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4908 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4909
4910 (defun gnus-summary-save-process-mark ()
4911   "Push the current set of process marked articles on the stack."
4912   (interactive)
4913   (push (copy-sequence gnus-newsgroup-processable)
4914         gnus-newsgroup-process-stack))
4915
4916 (defun gnus-summary-kill-process-mark ()
4917   "Push the current set of process marked articles on the stack and unmark."
4918   (interactive)
4919   (gnus-summary-save-process-mark)
4920   (gnus-summary-unmark-all-processable))
4921
4922 (defun gnus-summary-yank-process-mark ()
4923   "Pop the last process mark state off the stack and restore it."
4924   (interactive)
4925   (unless gnus-newsgroup-process-stack
4926     (error "Empty mark stack"))
4927   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4928
4929 (defun gnus-summary-process-mark-set (set)
4930   "Make SET into the current process marked articles."
4931   (gnus-summary-unmark-all-processable)
4932   (while set
4933     (gnus-summary-set-process-mark (pop set))))
4934
4935 ;;; Searching and stuff
4936
4937 (defun gnus-summary-search-group (&optional backward use-level)
4938   "Search for next unread newsgroup.
4939 If optional argument BACKWARD is non-nil, search backward instead."
4940   (save-excursion
4941     (set-buffer gnus-group-buffer)
4942     (when (gnus-group-search-forward
4943            backward nil (if use-level (gnus-group-group-level) nil))
4944       (gnus-group-group-name))))
4945
4946 (defun gnus-summary-best-group (&optional exclude-group)
4947   "Find the name of the best unread group.
4948 If EXCLUDE-GROUP, do not go to this group."
4949   (save-excursion
4950     (set-buffer gnus-group-buffer)
4951     (save-excursion
4952       (gnus-group-best-unread-group exclude-group))))
4953
4954 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4955   (if backward (gnus-summary-find-prev)
4956     (let* ((dummy (gnus-summary-article-intangible-p))
4957            (article (or article (gnus-summary-article-number)))
4958            (arts (gnus-data-find-list article))
4959            result)
4960       (when (and (not dummy)
4961                  (or (not gnus-summary-check-current)
4962                      (not unread)
4963                      (not (gnus-data-unread-p (car arts)))))
4964         (setq arts (cdr arts)))
4965       (when (setq result
4966                   (if unread
4967                       (progn
4968                         (while arts
4969                           (when (or (and undownloaded
4970                                          (eq gnus-undownloaded-mark
4971                                              (gnus-data-mark (car arts))))
4972                                     (gnus-data-unread-p (car arts)))
4973                             (setq result (car arts)
4974                                   arts nil))
4975                           (setq arts (cdr arts)))
4976                         result)
4977                     (car arts)))
4978         (goto-char (gnus-data-pos result))
4979         (gnus-data-number result)))))
4980
4981 (defun gnus-summary-find-prev (&optional unread article)
4982   (let* ((eobp (eobp))
4983          (article (or article (gnus-summary-article-number)))
4984          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4985          result)
4986     (when (and (not eobp)
4987                (or (not gnus-summary-check-current)
4988                    (not unread)
4989                    (not (gnus-data-unread-p (car arts)))))
4990       (setq arts (cdr arts)))
4991     (when (setq result
4992                 (if unread
4993                     (progn
4994                       (while arts
4995                         (when (gnus-data-unread-p (car arts))
4996                           (setq result (car arts)
4997                                 arts nil))
4998                         (setq arts (cdr arts)))
4999                       result)
5000                   (car arts)))
5001       (goto-char (gnus-data-pos result))
5002       (gnus-data-number result))))
5003
5004 (defun gnus-summary-find-subject (subject &optional unread backward article)
5005   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5006          (article (or article (gnus-summary-article-number)))
5007          (articles (gnus-data-list backward))
5008          (arts (gnus-data-find-list article articles))
5009          result)
5010     (when (or (not gnus-summary-check-current)
5011               (not unread)
5012               (not (gnus-data-unread-p (car arts))))
5013       (setq arts (cdr arts)))
5014     (while arts
5015       (and (or (not unread)
5016                (gnus-data-unread-p (car arts)))
5017            (vectorp (gnus-data-header (car arts)))
5018            (gnus-subject-equal
5019             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5020            (setq result (car arts)
5021                  arts nil))
5022       (setq arts (cdr arts)))
5023     (and result
5024          (goto-char (gnus-data-pos result))
5025          (gnus-data-number result))))
5026
5027 (defun gnus-summary-search-forward (&optional unread subject backward)
5028   "Search forward for an article.
5029 If UNREAD, look for unread articles.  If SUBJECT, look for
5030 articles with that subject.  If BACKWARD, search backward instead."
5031   (cond (subject (gnus-summary-find-subject subject unread backward))
5032         (backward (gnus-summary-find-prev unread))
5033         (t (gnus-summary-find-next unread))))
5034
5035 (defun gnus-recenter (&optional n)
5036   "Center point in window and redisplay frame.
5037 Also do horizontal recentering."
5038   (interactive "P")
5039   (when (and gnus-auto-center-summary
5040              (not (eq gnus-auto-center-summary 'vertical)))
5041     (gnus-horizontal-recenter))
5042   (recenter n))
5043
5044 (defun gnus-summary-recenter ()
5045   "Center point in the summary window.
5046 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5047 displayed, no centering will be performed."
5048   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5049   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5050   (let* ((top (cond ((< (window-height) 4) 0)
5051                     ((< (window-height) 7) 1)
5052                     (t (if (numberp gnus-auto-center-summary)
5053                            gnus-auto-center-summary
5054                          2))))
5055          (height (1- (window-height)))
5056          (bottom (save-excursion (goto-char (point-max))
5057                                  (forward-line (- height))
5058                                  (point)))
5059          (window (get-buffer-window (current-buffer))))
5060     ;; The user has to want it.
5061     (when gnus-auto-center-summary
5062       (when (get-buffer-window gnus-article-buffer)
5063         ;; Only do recentering when the article buffer is displayed,
5064         ;; Set the window start to either `bottom', which is the biggest
5065         ;; possible valid number, or the second line from the top,
5066         ;; whichever is the least.
5067         (set-window-start
5068          window (min bottom (save-excursion
5069                               (forward-line (- top)) (point)))))
5070       ;; Do horizontal recentering while we're at it.
5071       (when (and (get-buffer-window (current-buffer) t)
5072                  (not (eq gnus-auto-center-summary 'vertical)))
5073         (let ((selected (selected-window)))
5074           (select-window (get-buffer-window (current-buffer) t))
5075           (gnus-summary-position-point)
5076           (gnus-horizontal-recenter)
5077           (select-window selected))))))
5078
5079 (defun gnus-summary-jump-to-group (newsgroup)
5080   "Move point to NEWSGROUP in group mode buffer."
5081   ;; Keep update point of group mode buffer if visible.
5082   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5083       (save-window-excursion
5084         ;; Take care of tree window mode.
5085         (when (get-buffer-window gnus-group-buffer)
5086           (pop-to-buffer gnus-group-buffer))
5087         (gnus-group-jump-to-group newsgroup))
5088     (save-excursion
5089       ;; Take care of tree window mode.
5090       (if (get-buffer-window gnus-group-buffer)
5091           (pop-to-buffer gnus-group-buffer)
5092         (set-buffer gnus-group-buffer))
5093       (gnus-group-jump-to-group newsgroup))))
5094
5095 ;; This function returns a list of article numbers based on the
5096 ;; difference between the ranges of read articles in this group and
5097 ;; the range of active articles.
5098 (defun gnus-list-of-unread-articles (group)
5099   (let* ((read (gnus-info-read (gnus-get-info group)))
5100          (active (or (gnus-active group) (gnus-activate-group group)))
5101          (last (cdr active))
5102          first nlast unread)
5103     ;; If none are read, then all are unread.
5104     (if (not read)
5105         (setq first (car active))
5106       ;; If the range of read articles is a single range, then the
5107       ;; first unread article is the article after the last read
5108       ;; article.  Sounds logical, doesn't it?
5109       (if (not (listp (cdr read)))
5110           (setq first (max (car active) (1+ (cdr read))))
5111         ;; `read' is a list of ranges.
5112         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5113                                   (caar read)))
5114                   1)
5115           (setq first (car active)))
5116         (while read
5117           (when first
5118             (while (< first nlast)
5119               (push first unread)
5120               (setq first (1+ first))))
5121           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5122           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5123           (setq read (cdr read)))))
5124     ;; And add the last unread articles.
5125     (while (<= first last)
5126       (push first unread)
5127       (setq first (1+ first)))
5128     ;; Return the list of unread articles.
5129     (delq 0 (nreverse unread))))
5130
5131 (defun gnus-list-of-read-articles (group)
5132   "Return a list of unread, unticked and non-dormant articles."
5133   (let* ((info (gnus-get-info group))
5134          (marked (gnus-info-marks info))
5135          (active (gnus-active group)))
5136     (and info active
5137          (gnus-set-difference
5138           (gnus-sorted-complement
5139            (gnus-uncompress-range active)
5140            (gnus-list-of-unread-articles group))
5141           (append
5142            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5143            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5144
5145 ;; Various summary commands
5146
5147 (defun gnus-summary-select-article-buffer ()
5148   "Reconfigure windows to show article buffer."
5149   (interactive)
5150   (if (not (gnus-buffer-live-p gnus-article-buffer))
5151       (error "There is no article buffer for this summary buffer")
5152     (gnus-configure-windows 'article)
5153     (select-window (get-buffer-window gnus-article-buffer))))
5154
5155 (defun gnus-summary-universal-argument (arg)
5156   "Perform any operation on all articles that are process/prefixed."
5157   (interactive "P")
5158   (let ((articles (gnus-summary-work-articles arg))
5159         func article)
5160     (if (eq
5161          (setq
5162           func
5163           (key-binding
5164            (read-key-sequence
5165             (substitute-command-keys
5166              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5167              ))))
5168          'undefined)
5169         (gnus-error 1 "Undefined key")
5170       (save-excursion
5171         (while articles
5172           (gnus-summary-goto-subject (setq article (pop articles)))
5173           (let (gnus-newsgroup-processable)
5174             (command-execute func))
5175           (gnus-summary-remove-process-mark article)))))
5176   (gnus-summary-position-point))
5177
5178 (defun gnus-summary-toggle-truncation (&optional arg)
5179   "Toggle truncation of summary lines.
5180 With arg, turn line truncation on iff arg is positive."
5181   (interactive "P")
5182   (setq truncate-lines
5183         (if (null arg) (not truncate-lines)
5184           (> (prefix-numeric-value arg) 0)))
5185   (redraw-display))
5186
5187 (defun gnus-summary-reselect-current-group (&optional all rescan)
5188   "Exit and then reselect the current newsgroup.
5189 The prefix argument ALL means to select all articles."
5190   (interactive "P")
5191   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5192     (error "Ephemeral groups can't be reselected"))
5193   (let ((current-subject (gnus-summary-article-number))
5194         (group gnus-newsgroup-name))
5195     (setq gnus-newsgroup-begin nil)
5196     (gnus-summary-exit)
5197     ;; We have to adjust the point of group mode buffer because
5198     ;; point was moved to the next unread newsgroup by exiting.
5199     (gnus-summary-jump-to-group group)
5200     (when rescan
5201       (save-excursion
5202         (gnus-group-get-new-news-this-group 1)))
5203     (gnus-group-read-group all t)
5204     (gnus-summary-goto-subject current-subject nil t)))
5205
5206 (defun gnus-summary-rescan-group (&optional all)
5207   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5208   (interactive "P")
5209   (gnus-summary-reselect-current-group all t))
5210
5211 (defun gnus-summary-update-info (&optional non-destructive)
5212   (save-excursion
5213     (let ((group gnus-newsgroup-name))
5214       (when group
5215         (when gnus-newsgroup-kill-headers
5216           (setq gnus-newsgroup-killed
5217                 (gnus-compress-sequence
5218                  (nconc
5219                   (gnus-set-sorted-intersection
5220                    (gnus-uncompress-range gnus-newsgroup-killed)
5221                    (setq gnus-newsgroup-unselected
5222                          (sort gnus-newsgroup-unselected '<)))
5223                   (setq gnus-newsgroup-unreads
5224                         (sort gnus-newsgroup-unreads '<)))
5225                  t)))
5226         (unless (listp (cdr gnus-newsgroup-killed))
5227           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5228         (let ((headers gnus-newsgroup-headers))
5229           ;; Set the new ranges of read articles.
5230           (save-excursion
5231             (set-buffer gnus-group-buffer)
5232             (gnus-undo-force-boundary))
5233           (gnus-update-read-articles
5234            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5235           ;; Set the current article marks.
5236           (let ((gnus-newsgroup-scored
5237                  (if (and (not gnus-save-score)
5238                           (not non-destructive))
5239                      nil
5240                    gnus-newsgroup-scored)))
5241             (save-excursion
5242               (gnus-update-marks)))
5243           ;; Do the cross-ref thing.
5244           (when gnus-use-cross-reference
5245             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5246           ;; Do not switch windows but change the buffer to work.
5247           (set-buffer gnus-group-buffer)
5248           (unless (gnus-ephemeral-group-p group)
5249             (gnus-group-update-group group)))))))
5250
5251 (defun gnus-summary-save-newsrc (&optional force)
5252   "Save the current number of read/marked articles in the dribble buffer.
5253 The dribble buffer will then be saved.
5254 If FORCE (the prefix), also save the .newsrc file(s)."
5255   (interactive "P")
5256   (gnus-summary-update-info t)
5257   (if force
5258       (gnus-save-newsrc-file)
5259     (gnus-dribble-save)))
5260
5261 (defun gnus-summary-exit (&optional temporary)
5262   "Exit reading current newsgroup, and then return to group selection mode.
5263 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5264   (interactive)
5265   (gnus-set-global-variables)
5266   (when (gnus-buffer-live-p gnus-article-buffer)
5267     (save-excursion
5268       (set-buffer gnus-article-buffer)
5269       (mm-destroy-parts gnus-article-mime-handles)))
5270   (gnus-kill-save-kill-buffer)
5271   (gnus-async-halt-prefetch)
5272   (let* ((group gnus-newsgroup-name)
5273          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5274          (mode major-mode)
5275          (group-point nil)
5276          (buf (current-buffer)))
5277     (unless quit-config
5278       ;; Do adaptive scoring, and possibly save score files.
5279       (when gnus-newsgroup-adaptive
5280         (gnus-score-adaptive))
5281       (when gnus-use-scoring
5282         (gnus-score-save)))
5283     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5284     ;; If we have several article buffers, we kill them at exit.
5285     (unless gnus-single-article-buffer
5286       (gnus-kill-buffer gnus-original-article-buffer)
5287       (setq gnus-article-current nil))
5288     (when gnus-use-cache
5289       (gnus-cache-possibly-remove-articles)
5290       (gnus-cache-save-buffers))
5291     (gnus-async-prefetch-remove-group group)
5292     (when gnus-suppress-duplicates
5293       (gnus-dup-enter-articles))
5294     (when gnus-use-trees
5295       (gnus-tree-close group))
5296     ;; Remove entries for this group.
5297     (nnmail-purge-split-history (gnus-group-real-name group))
5298     ;; Make all changes in this group permanent.
5299     (unless quit-config
5300       (gnus-run-hooks 'gnus-exit-group-hook)
5301       (gnus-summary-update-info))
5302     (gnus-close-group group)
5303     ;; Make sure where we were, and go to next newsgroup.
5304     (set-buffer gnus-group-buffer)
5305     (unless quit-config
5306       (gnus-group-jump-to-group group))
5307     (gnus-run-hooks 'gnus-summary-exit-hook)
5308     (unless (or quit-config
5309                 ;; If this group has disappeared from the summary
5310                 ;; buffer, don't skip forwards.
5311                 (not (string= group (gnus-group-group-name))))
5312       (gnus-group-next-unread-group 1))
5313     (setq group-point (point))
5314     (if temporary
5315         nil                             ;Nothing to do.
5316       ;; If we have several article buffers, we kill them at exit.
5317       (unless gnus-single-article-buffer
5318         (gnus-kill-buffer gnus-article-buffer)
5319         (gnus-kill-buffer gnus-original-article-buffer)
5320         (setq gnus-article-current nil))
5321       (set-buffer buf)
5322       (if (not gnus-kill-summary-on-exit)
5323           (gnus-deaden-summary)
5324         ;; We set all buffer-local variables to nil.  It is unclear why
5325         ;; this is needed, but if we don't, buffer-local variables are
5326         ;; not garbage-collected, it seems.  This would the lead to en
5327         ;; ever-growing Emacs.
5328         (gnus-summary-clear-local-variables)
5329         (when (get-buffer gnus-article-buffer)
5330           (bury-buffer gnus-article-buffer))
5331         ;; We clear the global counterparts of the buffer-local
5332         ;; variables as well, just to be on the safe side.
5333         (set-buffer gnus-group-buffer)
5334         (gnus-summary-clear-local-variables)
5335         ;; Return to group mode buffer.
5336         (when (eq mode 'gnus-summary-mode)
5337           (gnus-kill-buffer buf)))
5338       (setq gnus-current-select-method gnus-select-method)
5339       (pop-to-buffer gnus-group-buffer)
5340       (if (not quit-config)
5341           (progn
5342             (goto-char group-point)
5343             (gnus-configure-windows 'group 'force))
5344         (gnus-handle-ephemeral-exit quit-config))
5345       ;; Clear the current group name.
5346       (unless quit-config
5347         (setq gnus-newsgroup-name nil)))))
5348
5349 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5350 (defun gnus-summary-exit-no-update (&optional no-questions)
5351   "Quit reading current newsgroup without updating read article info."
5352   (interactive)
5353   (let* ((group gnus-newsgroup-name)
5354          (quit-config (gnus-group-quit-config group)))
5355     (when (or no-questions
5356               gnus-expert-user
5357               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5358       (gnus-async-halt-prefetch)
5359       (mapcar 'funcall
5360               (delq 'gnus-summary-expire-articles
5361                     (copy-list gnus-summary-prepare-exit-hook)))
5362       (when (gnus-buffer-live-p gnus-article-buffer)
5363         (save-excursion
5364           (set-buffer gnus-article-buffer)
5365           (mm-destroy-parts gnus-article-mime-handles)))
5366       ;; If we have several article buffers, we kill them at exit.
5367       (unless gnus-single-article-buffer
5368         (gnus-kill-buffer gnus-article-buffer)
5369         (gnus-kill-buffer gnus-original-article-buffer)
5370         (setq gnus-article-current nil))
5371       (if (not gnus-kill-summary-on-exit)
5372           (gnus-deaden-summary)
5373         (gnus-close-group group)
5374         (gnus-summary-clear-local-variables)
5375         (set-buffer gnus-group-buffer)
5376         (gnus-summary-clear-local-variables)
5377         (when (get-buffer gnus-summary-buffer)
5378           (kill-buffer gnus-summary-buffer)))
5379       (unless gnus-single-article-buffer
5380         (setq gnus-article-current nil))
5381       (when gnus-use-trees
5382         (gnus-tree-close group))
5383       (gnus-async-prefetch-remove-group group)
5384       (when (get-buffer gnus-article-buffer)
5385         (bury-buffer gnus-article-buffer))
5386       ;; Return to the group buffer.
5387       (gnus-configure-windows 'group 'force)
5388       ;; Clear the current group name.
5389       (setq gnus-newsgroup-name nil)
5390       (when (equal (gnus-group-group-name) group)
5391         (gnus-group-next-unread-group 1))
5392       (when quit-config
5393         (gnus-handle-ephemeral-exit quit-config)))))
5394
5395 (defun gnus-handle-ephemeral-exit (quit-config)
5396   "Handle movement when leaving an ephemeral group.
5397 The state which existed when entering the ephemeral is reset."
5398   (if (not (buffer-name (car quit-config)))
5399       (gnus-configure-windows 'group 'force)
5400     (set-buffer (car quit-config))
5401     (cond ((eq major-mode 'gnus-summary-mode)
5402            (gnus-set-global-variables))
5403           ((eq major-mode 'gnus-article-mode)
5404            (save-excursion
5405              ;; The `gnus-summary-buffer' variable may point
5406              ;; to the old summary buffer when using a single
5407              ;; article buffer.
5408              (unless (gnus-buffer-live-p gnus-summary-buffer)
5409                (set-buffer gnus-group-buffer))
5410              (set-buffer gnus-summary-buffer)
5411              (gnus-set-global-variables))))
5412     (if (or (eq (cdr quit-config) 'article)
5413             (eq (cdr quit-config) 'pick))
5414         (progn
5415           ;; The current article may be from the ephemeral group
5416           ;; thus it is best that we reload this article
5417           (gnus-summary-show-article)
5418           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5419               (gnus-configure-windows 'pick 'force)
5420             (gnus-configure-windows (cdr quit-config) 'force)))
5421       (gnus-configure-windows (cdr quit-config) 'force))
5422     (when (eq major-mode 'gnus-summary-mode)
5423       (gnus-summary-next-subject 1 nil t)
5424       (gnus-summary-recenter)
5425       (gnus-summary-position-point))))
5426
5427 ;;; Dead summaries.
5428
5429 (defvar gnus-dead-summary-mode-map nil)
5430
5431 (unless gnus-dead-summary-mode-map
5432   (setq gnus-dead-summary-mode-map (make-keymap))
5433   (suppress-keymap gnus-dead-summary-mode-map)
5434   (substitute-key-definition
5435    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5436   (let ((keys '("\C-d" "\r" "\177" [delete])))
5437     (while keys
5438       (define-key gnus-dead-summary-mode-map
5439         (pop keys) 'gnus-summary-wake-up-the-dead))))
5440
5441 (defvar gnus-dead-summary-mode nil
5442   "Minor mode for Gnus summary buffers.")
5443
5444 (defun gnus-dead-summary-mode (&optional arg)
5445   "Minor mode for Gnus summary buffers."
5446   (interactive "P")
5447   (when (eq major-mode 'gnus-summary-mode)
5448     (make-local-variable 'gnus-dead-summary-mode)
5449     (setq gnus-dead-summary-mode
5450           (if (null arg) (not gnus-dead-summary-mode)
5451             (> (prefix-numeric-value arg) 0)))
5452     (when gnus-dead-summary-mode
5453       (gnus-add-minor-mode
5454        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5455
5456 (defun gnus-deaden-summary ()
5457   "Make the current summary buffer into a dead summary buffer."
5458   ;; Kill any previous dead summary buffer.
5459   (when (and gnus-dead-summary
5460              (buffer-name gnus-dead-summary))
5461     (save-excursion
5462       (set-buffer gnus-dead-summary)
5463       (when gnus-dead-summary-mode
5464         (kill-buffer (current-buffer)))))
5465   ;; Make this the current dead summary.
5466   (setq gnus-dead-summary (current-buffer))
5467   (gnus-dead-summary-mode 1)
5468   (let ((name (buffer-name)))
5469     (when (string-match "Summary" name)
5470       (rename-buffer
5471        (concat (substring name 0 (match-beginning 0)) "Dead "
5472                (substring name (match-beginning 0)))
5473        t))))
5474
5475 (defun gnus-kill-or-deaden-summary (buffer)
5476   "Kill or deaden the summary BUFFER."
5477   (save-excursion
5478     (when (and (buffer-name buffer)
5479                (not gnus-single-article-buffer))
5480       (save-excursion
5481         (set-buffer buffer)
5482         (gnus-kill-buffer gnus-article-buffer)
5483         (gnus-kill-buffer gnus-original-article-buffer)))
5484     (cond (gnus-kill-summary-on-exit
5485            (when (and gnus-use-trees
5486                       (gnus-buffer-exists-p buffer))
5487              (save-excursion
5488                (set-buffer buffer)
5489                (gnus-tree-close gnus-newsgroup-name)))
5490            (gnus-kill-buffer buffer))
5491           ((gnus-buffer-exists-p buffer)
5492            (save-excursion
5493              (set-buffer buffer)
5494              (gnus-deaden-summary))))))
5495
5496 (defun gnus-summary-wake-up-the-dead (&rest args)
5497   "Wake up the dead summary buffer."
5498   (interactive)
5499   (gnus-dead-summary-mode -1)
5500   (let ((name (buffer-name)))
5501     (when (string-match "Dead " name)
5502       (rename-buffer
5503        (concat (substring name 0 (match-beginning 0))
5504                (substring name (match-end 0)))
5505        t)))
5506   (gnus-message 3 "This dead summary is now alive again"))
5507
5508 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5509 (defun gnus-summary-fetch-faq (&optional faq-dir)
5510   "Fetch the FAQ for the current group.
5511 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5512 in."
5513   (interactive
5514    (list
5515     (when current-prefix-arg
5516       (completing-read
5517        "Faq dir: " (and (listp gnus-group-faq-directory)
5518                         (mapcar (lambda (file) (list file))
5519                                 gnus-group-faq-directory))))))
5520   (let (gnus-faq-buffer)
5521     (when (setq gnus-faq-buffer
5522                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5523       (gnus-configure-windows 'summary-faq))))
5524
5525 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5526 (defun gnus-summary-describe-group (&optional force)
5527   "Describe the current newsgroup."
5528   (interactive "P")
5529   (gnus-group-describe-group force gnus-newsgroup-name))
5530
5531 (defun gnus-summary-describe-briefly ()
5532   "Describe summary mode commands briefly."
5533   (interactive)
5534   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5535
5536 ;; Walking around group mode buffer from summary mode.
5537
5538 (defun gnus-summary-next-group (&optional no-article target-group backward)
5539   "Exit current newsgroup and then select next unread newsgroup.
5540 If prefix argument NO-ARTICLE is non-nil, no article is selected
5541 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5542 previous group instead."
5543   (interactive "P")
5544   ;; Stop pre-fetching.
5545   (gnus-async-halt-prefetch)
5546   (let ((current-group gnus-newsgroup-name)
5547         (current-buffer (current-buffer))
5548         entered)
5549     ;; First we semi-exit this group to update Xrefs and all variables.
5550     ;; We can't do a real exit, because the window conf must remain
5551     ;; the same in case the user is prompted for info, and we don't
5552     ;; want the window conf to change before that...
5553     (gnus-summary-exit t)
5554     (while (not entered)
5555       ;; Then we find what group we are supposed to enter.
5556       (set-buffer gnus-group-buffer)
5557       (gnus-group-jump-to-group current-group)
5558       (setq target-group
5559             (or target-group
5560                 (if (eq gnus-keep-same-level 'best)
5561                     (gnus-summary-best-group gnus-newsgroup-name)
5562                   (gnus-summary-search-group backward gnus-keep-same-level))))
5563       (if (not target-group)
5564           ;; There are no further groups, so we return to the group
5565           ;; buffer.
5566           (progn
5567             (gnus-message 5 "Returning to the group buffer")
5568             (setq entered t)
5569             (when (gnus-buffer-live-p current-buffer)
5570               (set-buffer current-buffer)
5571               (gnus-summary-exit))
5572             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5573         ;; We try to enter the target group.
5574         (gnus-group-jump-to-group target-group)
5575         (let ((unreads (gnus-group-group-unread)))
5576           (if (and (or (eq t unreads)
5577                        (and unreads (not (zerop unreads))))
5578                    (gnus-summary-read-group
5579                     target-group nil no-article
5580                     (and (buffer-name current-buffer) current-buffer)
5581                     nil backward))
5582               (setq entered t)
5583             (setq current-group target-group
5584                   target-group nil)))))))
5585
5586 (defun gnus-summary-prev-group (&optional no-article)
5587   "Exit current newsgroup and then select previous unread newsgroup.
5588 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5589   (interactive "P")
5590   (gnus-summary-next-group no-article nil t))
5591
5592 ;; Walking around summary lines.
5593
5594 (defun gnus-summary-first-subject (&optional unread undownloaded)
5595   "Go to the first unread subject.
5596 If UNREAD is non-nil, go to the first unread article.
5597 Returns the article selected or nil if there are no unread articles."
5598   (interactive "P")
5599   (prog1
5600       (cond
5601        ;; Empty summary.
5602        ((null gnus-newsgroup-data)
5603         (gnus-message 3 "No articles in the group")
5604         nil)
5605        ;; Pick the first article.
5606        ((not unread)
5607         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5608         (gnus-data-number (car gnus-newsgroup-data)))
5609        ;; No unread articles.
5610        ((null gnus-newsgroup-unreads)
5611         (gnus-message 3 "No more unread articles")
5612         nil)
5613        ;; Find the first unread article.
5614        (t
5615         (let ((data gnus-newsgroup-data))
5616           (while (and data
5617                       (and (not (and undownloaded
5618                                      (eq gnus-undownloaded-mark
5619                                          (gnus-data-mark (car data)))))
5620                            (not (gnus-data-unread-p (car data)))))
5621             (setq data (cdr data)))
5622           (when data
5623             (goto-char (gnus-data-pos (car data)))
5624             (gnus-data-number (car data))))))
5625     (gnus-summary-position-point)))
5626
5627 (defun gnus-summary-next-subject (n &optional unread dont-display)
5628   "Go to next N'th summary line.
5629 If N is negative, go to the previous N'th subject line.
5630 If UNREAD is non-nil, only unread articles are selected.
5631 The difference between N and the actual number of steps taken is
5632 returned."
5633   (interactive "p")
5634   (let ((backward (< n 0))
5635         (n (abs n)))
5636     (while (and (> n 0)
5637                 (if backward
5638                     (gnus-summary-find-prev unread)
5639                   (gnus-summary-find-next unread)))
5640       (gnus-summary-show-thread)
5641       (setq n (1- n)))
5642     (when (/= 0 n)
5643       (gnus-message 7 "No more%s articles"
5644                     (if unread " unread" "")))
5645     (unless dont-display
5646       (gnus-summary-recenter)
5647       (gnus-summary-position-point))
5648     n))
5649
5650 (defun gnus-summary-next-unread-subject (n)
5651   "Go to next N'th unread summary line."
5652   (interactive "p")
5653   (gnus-summary-next-subject n t))
5654
5655 (defun gnus-summary-prev-subject (n &optional unread)
5656   "Go to previous N'th summary line.
5657 If optional argument UNREAD is non-nil, only unread article is selected."
5658   (interactive "p")
5659   (gnus-summary-next-subject (- n) unread))
5660
5661 (defun gnus-summary-prev-unread-subject (n)
5662   "Go to previous N'th unread summary line."
5663   (interactive "p")
5664   (gnus-summary-next-subject (- n) t))
5665
5666 (defun gnus-summary-goto-subject (article &optional force silent)
5667   "Go the subject line of ARTICLE.
5668 If FORCE, also allow jumping to articles not currently shown."
5669   (interactive "nArticle number: ")
5670   (let ((b (point))
5671         (data (gnus-data-find article)))
5672     ;; We read in the article if we have to.
5673     (and (not data)
5674          force
5675          (gnus-summary-insert-subject
5676           article
5677           (if (or (numberp force) (vectorp force)) force)
5678           t)
5679          (setq data (gnus-data-find article)))
5680     (goto-char b)
5681     (if (not data)
5682         (progn
5683           (unless silent
5684             (gnus-message 3 "Can't find article %d" article))
5685           nil)
5686       (goto-char (gnus-data-pos data))
5687       (gnus-summary-position-point)
5688       article)))
5689
5690 ;; Walking around summary lines with displaying articles.
5691
5692 (defun gnus-summary-expand-window (&optional arg)
5693   "Make the summary buffer take up the entire Emacs frame.
5694 Given a prefix, will force an `article' buffer configuration."
5695   (interactive "P")
5696   (if arg
5697       (gnus-configure-windows 'article 'force)
5698     (gnus-configure-windows 'summary 'force)))
5699
5700 (defun gnus-summary-display-article (article &optional all-header)
5701   "Display ARTICLE in article buffer."
5702   (gnus-set-global-variables)
5703   (if (null article)
5704       nil
5705     (prog1
5706         (if gnus-summary-display-article-function
5707             (funcall gnus-summary-display-article-function article all-header)
5708           (gnus-article-prepare article all-header))
5709       (gnus-run-hooks 'gnus-select-article-hook)
5710       (when (and gnus-current-article
5711                  (not (zerop gnus-current-article)))
5712         (gnus-summary-goto-subject gnus-current-article))
5713       (gnus-summary-recenter)
5714       (when (and gnus-use-trees gnus-show-threads)
5715         (gnus-possibly-generate-tree article)
5716         (gnus-highlight-selected-tree article))
5717       ;; Successfully display article.
5718       (gnus-article-set-window-start
5719        (cdr (assq article gnus-newsgroup-bookmarks))))))
5720
5721 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5722   "Select the current article.
5723 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5724 non-nil, the article will be re-fetched even if it already present in
5725 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5726 be displayed."
5727   ;; Make sure we are in the summary buffer to work around bbdb bug.
5728   (unless (eq major-mode 'gnus-summary-mode)
5729     (set-buffer gnus-summary-buffer))
5730   (let ((article (or article (gnus-summary-article-number)))
5731         (all-headers (not (not all-headers))) ;Must be T or NIL.
5732         gnus-summary-display-article-function
5733         did)
5734     (and (not pseudo)
5735          (gnus-summary-article-pseudo-p article)
5736          (error "This is a pseudo-article"))
5737     (prog1
5738         (save-excursion
5739           (set-buffer gnus-summary-buffer)
5740           (if (or (and gnus-single-article-buffer
5741                        (or (null gnus-current-article)
5742                            (null gnus-article-current)
5743                            (null (get-buffer gnus-article-buffer))
5744                            (not (eq article (cdr gnus-article-current)))
5745                            (not (equal (car gnus-article-current)
5746                                        gnus-newsgroup-name))))
5747                   (and (not gnus-single-article-buffer)
5748                        (or (null gnus-current-article)
5749                            (not (eq gnus-current-article article))))
5750                   force)
5751               ;; The requested article is different from the current article.
5752               (prog1
5753                   (gnus-summary-display-article article all-headers)
5754                 (setq did article)
5755                 (when (or all-headers gnus-show-all-headers)
5756                   (gnus-article-show-all-headers)))
5757             (when (or all-headers gnus-show-all-headers)
5758               (gnus-article-show-all-headers))
5759             'old))
5760       (when did
5761         (gnus-article-set-window-start
5762          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5763
5764 (defun gnus-summary-set-current-mark (&optional current-mark)
5765   "Obsolete function."
5766   nil)
5767
5768 (defun gnus-summary-next-article (&optional unread subject backward push)
5769   "Select the next article.
5770 If UNREAD, only unread articles are selected.
5771 If SUBJECT, only articles with SUBJECT are selected.
5772 If BACKWARD, the previous article is selected instead of the next."
5773   (interactive "P")
5774   (cond
5775    ;; Is there such an article?
5776    ((and (gnus-summary-search-forward unread subject backward)
5777          (or (gnus-summary-display-article (gnus-summary-article-number))
5778              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5779     (gnus-summary-position-point))
5780    ;; If not, we try the first unread, if that is wanted.
5781    ((and subject
5782          gnus-auto-select-same
5783          (gnus-summary-first-unread-article))
5784     (gnus-summary-position-point)
5785     (gnus-message 6 "Wrapped"))
5786    ;; Try to get next/previous article not displayed in this group.
5787    ((and gnus-auto-extend-newsgroup
5788          (not unread) (not subject))
5789     (gnus-summary-goto-article
5790      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5791      nil (count-lines (point-min) (point))))
5792    ;; Go to next/previous group.
5793    (t
5794     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5795       (gnus-summary-jump-to-group gnus-newsgroup-name))
5796     (let ((cmd last-command-char)
5797           (point
5798            (save-excursion
5799              (set-buffer gnus-group-buffer)
5800              (point)))
5801           (group
5802            (if (eq gnus-keep-same-level 'best)
5803                (gnus-summary-best-group gnus-newsgroup-name)
5804              (gnus-summary-search-group backward gnus-keep-same-level))))
5805       ;; For some reason, the group window gets selected.  We change
5806       ;; it back.
5807       (select-window (get-buffer-window (current-buffer)))
5808       ;; Select next unread newsgroup automagically.
5809       (cond
5810        ((or (not gnus-auto-select-next)
5811             (not cmd))
5812         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5813        ((or (eq gnus-auto-select-next 'quietly)
5814             (and (eq gnus-auto-select-next 'slightly-quietly)
5815                  push)
5816             (and (eq gnus-auto-select-next 'almost-quietly)
5817                  (gnus-summary-last-article-p)))
5818         ;; Select quietly.
5819         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5820             (gnus-summary-exit)
5821           (gnus-message 7 "No more%s articles (%s)..."
5822                         (if unread " unread" "")
5823                         (if group (concat "selecting " group)
5824                           "exiting"))
5825           (gnus-summary-next-group nil group backward)))
5826        (t
5827         (when (gnus-key-press-event-p last-input-event)
5828           (gnus-summary-walk-group-buffer
5829            gnus-newsgroup-name cmd unread backward point))))))))
5830
5831 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5832   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5833                       (?\C-p (gnus-group-prev-unread-group 1))))
5834         (cursor-in-echo-area t)
5835         keve key group ended)
5836     (save-excursion
5837       (set-buffer gnus-group-buffer)
5838       (goto-char start)
5839       (setq group
5840             (if (eq gnus-keep-same-level 'best)
5841                 (gnus-summary-best-group gnus-newsgroup-name)
5842               (gnus-summary-search-group backward gnus-keep-same-level))))
5843     (while (not ended)
5844       (gnus-message
5845        5 "No more%s articles%s" (if unread " unread" "")
5846        (if (and group
5847                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5848            (format " (Type %s for %s [%s])"
5849                    (single-key-description cmd) group
5850                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5851          (format " (Type %s to exit %s)"
5852                  (single-key-description cmd)
5853                  gnus-newsgroup-name)))
5854       ;; Confirm auto selection.
5855       (setq key (car (setq keve (gnus-read-event-char))))
5856       (setq ended t)
5857       (cond
5858        ((assq key keystrokes)
5859         (let ((obuf (current-buffer)))
5860           (switch-to-buffer gnus-group-buffer)
5861           (when group
5862             (gnus-group-jump-to-group group))
5863           (eval (cadr (assq key keystrokes)))
5864           (setq group (gnus-group-group-name))
5865           (switch-to-buffer obuf))
5866         (setq ended nil))
5867        ((equal key cmd)
5868         (if (or (not group)
5869                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5870             (gnus-summary-exit)
5871           (gnus-summary-next-group nil group backward)))
5872        (t
5873         (push (cdr keve) unread-command-events))))))
5874
5875 (defun gnus-summary-next-unread-article ()
5876   "Select unread article after current one."
5877   (interactive)
5878   (gnus-summary-next-article
5879    (or (not (eq gnus-summary-goto-unread 'never))
5880        (gnus-summary-last-article-p (gnus-summary-article-number)))
5881    (and gnus-auto-select-same
5882         (gnus-summary-article-subject))))
5883
5884 (defun gnus-summary-prev-article (&optional unread subject)
5885   "Select the article after the current one.
5886 If UNREAD is non-nil, only unread articles are selected."
5887   (interactive "P")
5888   (gnus-summary-next-article unread subject t))
5889
5890 (defun gnus-summary-prev-unread-article ()
5891   "Select unread article before current one."
5892   (interactive)
5893   (gnus-summary-prev-article
5894    (or (not (eq gnus-summary-goto-unread 'never))
5895        (gnus-summary-first-article-p (gnus-summary-article-number)))
5896    (and gnus-auto-select-same
5897         (gnus-summary-article-subject))))
5898
5899 (defun gnus-summary-next-page (&optional lines circular)
5900   "Show next page of the selected article.
5901 If at the end of the current article, select the next article.
5902 LINES says how many lines should be scrolled up.
5903
5904 If CIRCULAR is non-nil, go to the start of the article instead of
5905 selecting the next article when reaching the end of the current
5906 article."
5907   (interactive "P")
5908   (setq gnus-summary-buffer (current-buffer))
5909   (gnus-set-global-variables)
5910   (let ((article (gnus-summary-article-number))
5911         (article-window (get-buffer-window gnus-article-buffer t))
5912         endp)
5913     ;; If the buffer is empty, we have no article.
5914     (unless article
5915       (error "No article to select"))
5916     (gnus-configure-windows 'article)
5917     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5918         (if (and (eq gnus-summary-goto-unread 'never)
5919                  (not (gnus-summary-last-article-p article)))
5920             (gnus-summary-next-article)
5921           (gnus-summary-next-unread-article))
5922       (if (or (null gnus-current-article)
5923               (null gnus-article-current)
5924               (/= article (cdr gnus-article-current))
5925               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5926           ;; Selected subject is different from current article's.
5927           (gnus-summary-display-article article)
5928         (when article-window
5929           (gnus-eval-in-buffer-window gnus-article-buffer
5930             (setq endp (gnus-article-next-page lines)))
5931           (when endp
5932             (cond (circular
5933                    (gnus-summary-beginning-of-article))
5934                   (lines
5935                    (gnus-message 3 "End of message"))
5936                   ((null lines)
5937                    (if (and (eq gnus-summary-goto-unread 'never)
5938                             (not (gnus-summary-last-article-p article)))
5939                        (gnus-summary-next-article)
5940                      (gnus-summary-next-unread-article))))))))
5941     (gnus-summary-recenter)
5942     (gnus-summary-position-point)))
5943
5944 (defun gnus-summary-prev-page (&optional lines move)
5945   "Show previous page of selected article.
5946 Argument LINES specifies lines to be scrolled down.
5947 If MOVE, move to the previous unread article if point is at
5948 the beginning of the buffer."
5949   (interactive "P")
5950   (let ((article (gnus-summary-article-number))
5951         (article-window (get-buffer-window gnus-article-buffer t))
5952         endp)
5953     (gnus-configure-windows 'article)
5954     (if (or (null gnus-current-article)
5955             (null gnus-article-current)
5956             (/= article (cdr gnus-article-current))
5957             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5958         ;; Selected subject is different from current article's.
5959         (gnus-summary-display-article article)
5960       (gnus-summary-recenter)
5961       (when article-window
5962         (gnus-eval-in-buffer-window gnus-article-buffer
5963           (setq endp (gnus-article-prev-page lines)))
5964         (when (and move endp)
5965           (cond (lines
5966                  (gnus-message 3 "Beginning of message"))
5967                 ((null lines)
5968                  (if (and (eq gnus-summary-goto-unread 'never)
5969                           (not (gnus-summary-first-article-p article)))
5970                      (gnus-summary-prev-article)
5971                    (gnus-summary-prev-unread-article))))))))
5972   (gnus-summary-position-point))
5973
5974 (defun gnus-summary-prev-page-or-article (&optional lines)
5975   "Show previous page of selected article.
5976 Argument LINES specifies lines to be scrolled down.
5977 If at the beginning of the article, go to the next article."
5978   (interactive "P")
5979   (gnus-summary-prev-page lines t))
5980
5981 (defun gnus-summary-scroll-up (lines)
5982   "Scroll up (or down) one line current article.
5983 Argument LINES specifies lines to be scrolled up (or down if negative)."
5984   (interactive "p")
5985   (gnus-configure-windows 'article)
5986   (gnus-summary-show-thread)
5987   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5988     (gnus-eval-in-buffer-window gnus-article-buffer
5989       (cond ((> lines 0)
5990              (when (gnus-article-next-page lines)
5991                (gnus-message 3 "End of message")))
5992             ((< lines 0)
5993              (gnus-article-prev-page (- lines))))))
5994   (gnus-summary-recenter)
5995   (gnus-summary-position-point))
5996
5997 (defun gnus-summary-scroll-down (lines)
5998   "Scroll down (or up) one line current article.
5999 Argument LINES specifies lines to be scrolled down (or up if negative)."
6000   (interactive "p")
6001   (gnus-summary-scroll-up (- lines)))
6002
6003 (defun gnus-summary-next-same-subject ()
6004   "Select next article which has the same subject as current one."
6005   (interactive)
6006   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6007
6008 (defun gnus-summary-prev-same-subject ()
6009   "Select previous article which has the same subject as current one."
6010   (interactive)
6011   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6012
6013 (defun gnus-summary-next-unread-same-subject ()
6014   "Select next unread article which has the same subject as current one."
6015   (interactive)
6016   (gnus-summary-next-article t (gnus-summary-article-subject)))
6017
6018 (defun gnus-summary-prev-unread-same-subject ()
6019   "Select previous unread article which has the same subject as current one."
6020   (interactive)
6021   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6022
6023 (defun gnus-summary-first-unread-article ()
6024   "Select the first unread article.
6025 Return nil if there are no unread articles."
6026   (interactive)
6027   (prog1
6028       (when (gnus-summary-first-subject t)
6029         (gnus-summary-show-thread)
6030         (gnus-summary-first-subject t)
6031         (gnus-summary-display-article (gnus-summary-article-number)))
6032     (gnus-summary-position-point)))
6033
6034 (defun gnus-summary-first-unread-subject ()
6035   "Place the point on the subject line of the first unread article.
6036 Return nil if there are no unread articles."
6037   (interactive)
6038   (prog1
6039       (when (gnus-summary-first-subject t)
6040         (gnus-summary-show-thread)
6041         (gnus-summary-first-subject t))
6042     (gnus-summary-position-point)))
6043
6044 (defun gnus-summary-first-article ()
6045   "Select the first article.
6046 Return nil if there are no articles."
6047   (interactive)
6048   (prog1
6049       (when (gnus-summary-first-subject)
6050         (gnus-summary-show-thread)
6051         (gnus-summary-first-subject)
6052         (gnus-summary-display-article (gnus-summary-article-number)))
6053     (gnus-summary-position-point)))
6054
6055 (defun gnus-summary-best-unread-article ()
6056   "Select the unread article with the highest score."
6057   (interactive)
6058   (let ((best -1000000)
6059         (data gnus-newsgroup-data)
6060         article score)
6061     (while data
6062       (and (gnus-data-unread-p (car data))
6063            (> (setq score
6064                     (gnus-summary-article-score (gnus-data-number (car data))))
6065               best)
6066            (setq best score
6067                  article (gnus-data-number (car data))))
6068       (setq data (cdr data)))
6069     (prog1
6070         (if article
6071             (gnus-summary-goto-article article)
6072           (error "No unread articles"))
6073       (gnus-summary-position-point))))
6074
6075 (defun gnus-summary-last-subject ()
6076   "Go to the last displayed subject line in the group."
6077   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6078     (when article
6079       (gnus-summary-goto-subject article))))
6080
6081 (defun gnus-summary-goto-article (article &optional all-headers force)
6082   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6083 If ALL-HEADERS is non-nil, no header lines are hidden.
6084 If FORCE, go to the article even if it isn't displayed.  If FORCE
6085 is a number, it is the line the article is to be displayed on."
6086   (interactive
6087    (list
6088     (completing-read
6089      "Article number or Message-ID: "
6090      (mapcar (lambda (number) (list (int-to-string number)))
6091              gnus-newsgroup-limit))
6092     current-prefix-arg
6093     t))
6094   (prog1
6095       (if (and (stringp article)
6096                (string-match "@" article))
6097           (gnus-summary-refer-article article)
6098         (when (stringp article)
6099           (setq article (string-to-number article)))
6100         (if (gnus-summary-goto-subject article force)
6101             (gnus-summary-display-article article all-headers)
6102           (gnus-message 4 "Couldn't go to article %s" article) nil))
6103     (gnus-summary-position-point)))
6104
6105 (defun gnus-summary-goto-last-article ()
6106   "Go to the previously read article."
6107   (interactive)
6108   (prog1
6109       (when gnus-last-article
6110         (gnus-summary-goto-article gnus-last-article nil t))
6111     (gnus-summary-position-point)))
6112
6113 (defun gnus-summary-pop-article (number)
6114   "Pop one article off the history and go to the previous.
6115 NUMBER articles will be popped off."
6116   (interactive "p")
6117   (let (to)
6118     (setq gnus-newsgroup-history
6119           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6120     (if to
6121         (gnus-summary-goto-article (car to) nil t)
6122       (error "Article history empty")))
6123   (gnus-summary-position-point))
6124
6125 ;; Summary commands and functions for limiting the summary buffer.
6126
6127 (defun gnus-summary-limit-to-articles (n)
6128   "Limit the summary buffer to the next N articles.
6129 If not given a prefix, use the process marked articles instead."
6130   (interactive "P")
6131   (prog1
6132       (let ((articles (gnus-summary-work-articles n)))
6133         (setq gnus-newsgroup-processable nil)
6134         (gnus-summary-limit articles))
6135     (gnus-summary-position-point)))
6136
6137 (defun gnus-summary-pop-limit (&optional total)
6138   "Restore the previous limit.
6139 If given a prefix, remove all limits."
6140   (interactive "P")
6141   (when total
6142     (setq gnus-newsgroup-limits
6143           (list (mapcar (lambda (h) (mail-header-number h))
6144                         gnus-newsgroup-headers))))
6145   (unless gnus-newsgroup-limits
6146     (error "No limit to pop"))
6147   (prog1
6148       (gnus-summary-limit nil 'pop)
6149     (gnus-summary-position-point)))
6150
6151 (defun gnus-summary-limit-to-subject (subject &optional header)
6152   "Limit the summary buffer to articles that have subjects that match a regexp."
6153   (interactive "sLimit to subject (regexp): ")
6154   (unless header
6155     (setq header "subject"))
6156   (when (not (equal "" subject))
6157     (prog1
6158         (let ((articles (gnus-summary-find-matching
6159                          (or header "subject") subject 'all)))
6160           (unless articles
6161             (error "Found no matches for \"%s\"" subject))
6162           (gnus-summary-limit articles))
6163       (gnus-summary-position-point))))
6164
6165 (defun gnus-summary-limit-to-author (from)
6166   "Limit the summary buffer to articles that have authors that match a regexp."
6167   (interactive "sLimit to author (regexp): ")
6168   (gnus-summary-limit-to-subject from "from"))
6169
6170 (defun gnus-summary-limit-to-age (age &optional younger-p)
6171   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6172 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6173 articles that are younger than AGE days."
6174   (interactive "nLimit to articles older than (in days): \nP")
6175   (prog1
6176       (let ((data gnus-newsgroup-data)
6177             (cutoff (days-to-time age))
6178             articles d date is-younger)
6179         (while (setq d (pop data))
6180           (when (and (vectorp (gnus-data-header d))
6181                      (setq date (mail-header-date (gnus-data-header d))))
6182             (setq is-younger (time-less-p
6183                               (time-since (date-to-time date))
6184                               cutoff))
6185             (when (if younger-p
6186                       is-younger
6187                     (not is-younger))
6188               (push (gnus-data-number d) articles))))
6189         (gnus-summary-limit (nreverse articles)))
6190     (gnus-summary-position-point)))
6191
6192 (defun gnus-summary-limit-to-extra (header regexp)
6193   "Limit the summary buffer to articles that match an 'extra' header."
6194   (interactive
6195    (let ((header
6196           (intern
6197            (gnus-completing-read
6198             (symbol-name (car gnus-extra-headers))      
6199             "Limit extra header:"       
6200             (mapcar (lambda (x) 
6201                       (cons (symbol-name x) x))
6202                     gnus-extra-headers)
6203             nil                 
6204             t))))
6205      (list header
6206            (read-string (format "Limit to header %s (regexp): " header)))))
6207   (when (not (equal "" regexp))
6208     (prog1
6209         (let ((articles (gnus-summary-find-matching
6210                          (cons 'extra header) regexp 'all)))
6211           (unless articles
6212             (error "Found no matches for \"%s\"" regexp))
6213           (gnus-summary-limit articles))
6214       (gnus-summary-position-point))))
6215
6216 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6217 (make-obsolete
6218  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6219
6220 (defun gnus-summary-limit-to-unread (&optional all)
6221   "Limit the summary buffer to articles that are not marked as read.
6222 If ALL is non-nil, limit strictly to unread articles."
6223   (interactive "P")
6224   (if all
6225       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6226     (gnus-summary-limit-to-marks
6227      ;; Concat all the marks that say that an article is read and have
6228      ;; those removed.
6229      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6230            gnus-killed-mark gnus-kill-file-mark
6231            gnus-low-score-mark gnus-expirable-mark
6232            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6233            gnus-duplicate-mark gnus-souped-mark)
6234      'reverse)))
6235
6236 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6237 (make-obsolete 'gnus-summary-delete-marked-with
6238                'gnus-summary-limit-exlude-marks)
6239
6240 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6241   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6242 If REVERSE, limit the summary buffer to articles that are marked
6243 with MARKS.  MARKS can either be a string of marks or a list of marks.
6244 Returns how many articles were removed."
6245   (interactive "sMarks: ")
6246   (gnus-summary-limit-to-marks marks t))
6247
6248 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6249   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6250 If REVERSE (the prefix), limit the summary buffer to articles that are
6251 not marked with MARKS.  MARKS can either be a string of marks or a
6252 list of marks.
6253 Returns how many articles were removed."
6254   (interactive "sMarks: \nP")
6255   (prog1
6256       (let ((data gnus-newsgroup-data)
6257             (marks (if (listp marks) marks
6258                      (append marks nil))) ; Transform to list.
6259             articles)
6260         (while data
6261           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6262                   (memq (gnus-data-mark (car data)) marks))
6263             (push (gnus-data-number (car data)) articles))
6264           (setq data (cdr data)))
6265         (gnus-summary-limit articles))
6266     (gnus-summary-position-point)))
6267
6268 (defun gnus-summary-limit-to-score (&optional score)
6269   "Limit to articles with score at or above SCORE."
6270   (interactive "P")
6271   (setq score (if score
6272                   (prefix-numeric-value score)
6273                 (or gnus-summary-default-score 0)))
6274   (let ((data gnus-newsgroup-data)
6275         articles)
6276     (while data
6277       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6278                 score)
6279         (push (gnus-data-number (car data)) articles))
6280       (setq data (cdr data)))
6281     (prog1
6282         (gnus-summary-limit articles)
6283       (gnus-summary-position-point))))
6284
6285 (defun gnus-summary-limit-include-thread (id)
6286   "Display all the hidden articles that in the current thread."
6287   (interactive (list (mail-header-id (gnus-summary-article-header))))
6288   (let ((articles (gnus-articles-in-thread
6289                    (gnus-id-to-thread (gnus-root-id id)))))
6290     (prog1
6291         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6292       (gnus-summary-position-point))))
6293
6294 (defun gnus-summary-limit-include-dormant ()
6295   "Display all the hidden articles that are marked as dormant.
6296 Note that this command only works on a subset of the articles currently
6297 fetched for this group."
6298   (interactive)
6299   (unless gnus-newsgroup-dormant
6300     (error "There are no dormant articles in this group"))
6301   (prog1
6302       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6303     (gnus-summary-position-point)))
6304
6305 (defun gnus-summary-limit-exclude-dormant ()
6306   "Hide all dormant articles."
6307   (interactive)
6308   (prog1
6309       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6310     (gnus-summary-position-point)))
6311
6312 (defun gnus-summary-limit-exclude-childless-dormant ()
6313   "Hide all dormant articles that have no children."
6314   (interactive)
6315   (let ((data (gnus-data-list t))
6316         articles d children)
6317     ;; Find all articles that are either not dormant or have
6318     ;; children.
6319     (while (setq d (pop data))
6320       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6321                 (and (setq children
6322                            (gnus-article-children (gnus-data-number d)))
6323                      (let (found)
6324                        (while children
6325                          (when (memq (car children) articles)
6326                            (setq children nil
6327                                  found t))
6328                          (pop children))
6329                        found)))
6330         (push (gnus-data-number d) articles)))
6331     ;; Do the limiting.
6332     (prog1
6333         (gnus-summary-limit articles)
6334       (gnus-summary-position-point))))
6335
6336 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6337   "Mark all unread excluded articles as read.
6338 If ALL, mark even excluded ticked and dormants as read."
6339   (interactive "P")
6340   (let ((articles (gnus-sorted-complement
6341                    (sort
6342                     (mapcar (lambda (h) (mail-header-number h))
6343                             gnus-newsgroup-headers)
6344                     '<)
6345                    (sort gnus-newsgroup-limit '<)))
6346         article)
6347     (setq gnus-newsgroup-unreads
6348           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6349     (if all
6350         (setq gnus-newsgroup-dormant nil
6351               gnus-newsgroup-marked nil
6352               gnus-newsgroup-reads
6353               (nconc
6354                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6355                gnus-newsgroup-reads))
6356       (while (setq article (pop articles))
6357         (unless (or (memq article gnus-newsgroup-dormant)
6358                     (memq article gnus-newsgroup-marked))
6359           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6360
6361 (defun gnus-summary-limit (articles &optional pop)
6362   (if pop
6363       ;; We pop the previous limit off the stack and use that.
6364       (setq articles (car gnus-newsgroup-limits)
6365             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6366     ;; We use the new limit, so we push the old limit on the stack.
6367     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6368   ;; Set the limit.
6369   (setq gnus-newsgroup-limit articles)
6370   (let ((total (length gnus-newsgroup-data))
6371         (data (gnus-data-find-list (gnus-summary-article-number)))
6372         (gnus-summary-mark-below nil)   ; Inhibit this.
6373         found)
6374     ;; This will do all the work of generating the new summary buffer
6375     ;; according to the new limit.
6376     (gnus-summary-prepare)
6377     ;; Hide any threads, possibly.
6378     (and gnus-show-threads
6379          gnus-thread-hide-subtree
6380          (gnus-summary-hide-all-threads))
6381     ;; Try to return to the article you were at, or one in the
6382     ;; neighborhood.
6383     (when data
6384       ;; We try to find some article after the current one.
6385       (while data
6386         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6387           (setq data nil
6388                 found t))
6389         (setq data (cdr data))))
6390     (unless found
6391       ;; If there is no data, that means that we were after the last
6392       ;; article.  The same goes when we can't find any articles
6393       ;; after the current one.
6394       (goto-char (point-max))
6395       (gnus-summary-find-prev))
6396     (gnus-set-mode-line 'summary)
6397     ;; We return how many articles were removed from the summary
6398     ;; buffer as a result of the new limit.
6399     (- total (length gnus-newsgroup-data))))
6400
6401 (defsubst gnus-invisible-cut-children (threads)
6402   (let ((num 0))
6403     (while threads
6404       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6405         (incf num))
6406       (pop threads))
6407     (< num 2)))
6408
6409 (defsubst gnus-cut-thread (thread)
6410   "Go forwards in the thread until we find an article that we want to display."
6411   (when (or (eq gnus-fetch-old-headers 'some)
6412             (eq gnus-fetch-old-headers 'invisible)
6413             (eq gnus-build-sparse-threads 'some)
6414             (eq gnus-build-sparse-threads 'more))
6415     ;; Deal with old-fetched headers and sparse threads.
6416     (while (and
6417             thread
6418             (or
6419              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6420              (gnus-summary-article-ancient-p
6421               (mail-header-number (car thread))))
6422             (if (or (<= (length (cdr thread)) 1)
6423                     (eq gnus-fetch-old-headers 'invisible))
6424                 (setq gnus-newsgroup-limit
6425                       (delq (mail-header-number (car thread))
6426                             gnus-newsgroup-limit)
6427                       thread (cadr thread))
6428               (when (gnus-invisible-cut-children (cdr thread))
6429                 (let ((th (cdr thread)))
6430                   (while th
6431                     (if (memq (mail-header-number (caar th))
6432                               gnus-newsgroup-limit)
6433                         (setq thread (car th)
6434                               th nil)
6435                       (setq th (cdr th))))))))))
6436   thread)
6437
6438 (defun gnus-cut-threads (threads)
6439   "Cut off all uninteresting articles from the beginning of threads."
6440   (when (or (eq gnus-fetch-old-headers 'some)
6441             (eq gnus-fetch-old-headers 'invisible)
6442             (eq gnus-build-sparse-threads 'some)
6443             (eq gnus-build-sparse-threads 'more))
6444     (let ((th threads))
6445       (while th
6446         (setcar th (gnus-cut-thread (car th)))
6447         (setq th (cdr th)))))
6448   ;; Remove nixed out threads.
6449   (delq nil threads))
6450
6451 (defun gnus-summary-initial-limit (&optional show-if-empty)
6452   "Figure out what the initial limit is supposed to be on group entry.
6453 This entails weeding out unwanted dormants, low-scored articles,
6454 fetch-old-headers verbiage, and so on."
6455   ;; Most groups have nothing to remove.
6456   (if (or gnus-inhibit-limiting
6457           (and (null gnus-newsgroup-dormant)
6458                (not (eq gnus-fetch-old-headers 'some))
6459                (not (eq gnus-fetch-old-headers 'invisible))
6460                (null gnus-summary-expunge-below)
6461                (not (eq gnus-build-sparse-threads 'some))
6462                (not (eq gnus-build-sparse-threads 'more))
6463                (null gnus-thread-expunge-below)
6464                (not gnus-use-nocem)))
6465       ()                                ; Do nothing.
6466     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6467     (setq gnus-newsgroup-limit nil)
6468     (mapatoms
6469      (lambda (node)
6470        (unless (car (symbol-value node))
6471          ;; These threads have no parents -- they are roots.
6472          (let ((nodes (cdr (symbol-value node)))
6473                thread)
6474            (while nodes
6475              (if (and gnus-thread-expunge-below
6476                       (< (gnus-thread-total-score (car nodes))
6477                          gnus-thread-expunge-below))
6478                  (gnus-expunge-thread (pop nodes))
6479                (setq thread (pop nodes))
6480                (gnus-summary-limit-children thread))))))
6481      gnus-newsgroup-dependencies)
6482     ;; If this limitation resulted in an empty group, we might
6483     ;; pop the previous limit and use it instead.
6484     (when (and (not gnus-newsgroup-limit)
6485                show-if-empty)
6486       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6487     gnus-newsgroup-limit))
6488
6489 (defun gnus-summary-limit-children (thread)
6490   "Return 1 if this subthread is visible and 0 if it is not."
6491   ;; First we get the number of visible children to this thread.  This
6492   ;; is done by recursing down the thread using this function, so this
6493   ;; will really go down to a leaf article first, before slowly
6494   ;; working its way up towards the root.
6495   (when thread
6496     (let ((children
6497            (if (cdr thread)
6498                (apply '+ (mapcar 'gnus-summary-limit-children
6499                                  (cdr thread)))
6500              0))
6501           (number (mail-header-number (car thread)))
6502           score)
6503       (if (and
6504            (not (memq number gnus-newsgroup-marked))
6505            (or
6506             ;; If this article is dormant and has absolutely no visible
6507             ;; children, then this article isn't visible.
6508             (and (memq number gnus-newsgroup-dormant)
6509                  (zerop children))
6510             ;; If this is "fetch-old-headered" and there is no
6511             ;; visible children, then we don't want this article.
6512             (and (eq gnus-fetch-old-headers 'some)
6513                  (gnus-summary-article-ancient-p number)
6514                  (zerop children))
6515             ;; If this is "fetch-old-headered" and `invisible', then
6516             ;; we don't want this article.
6517             (and (eq gnus-fetch-old-headers 'invisible)
6518                  (gnus-summary-article-ancient-p number))
6519             ;; If this is a sparsely inserted article with no children,
6520             ;; we don't want it.
6521             (and (eq gnus-build-sparse-threads 'some)
6522                  (gnus-summary-article-sparse-p number)
6523                  (zerop children))
6524             ;; If we use expunging, and this article is really
6525             ;; low-scored, then we don't want this article.
6526             (when (and gnus-summary-expunge-below
6527                        (< (setq score
6528                                 (or (cdr (assq number gnus-newsgroup-scored))
6529                                     gnus-summary-default-score))
6530                           gnus-summary-expunge-below))
6531               ;; We increase the expunge-tally here, but that has
6532               ;; nothing to do with the limits, really.
6533               (incf gnus-newsgroup-expunged-tally)
6534               ;; We also mark as read here, if that's wanted.
6535               (when (and gnus-summary-mark-below
6536                          (< score gnus-summary-mark-below))
6537                 (setq gnus-newsgroup-unreads
6538                       (delq number gnus-newsgroup-unreads))
6539                 (if gnus-newsgroup-auto-expire
6540                     (push number gnus-newsgroup-expirable)
6541                   (push (cons number gnus-low-score-mark)
6542                         gnus-newsgroup-reads)))
6543               t)
6544             ;; Check NoCeM things.
6545             (if (and gnus-use-nocem
6546                      (gnus-nocem-unwanted-article-p
6547                       (mail-header-id (car thread))))
6548                 (progn
6549                   (setq gnus-newsgroup-unreads
6550                         (delq number gnus-newsgroup-unreads))
6551                   t))))
6552           ;; Nope, invisible article.
6553           0
6554         ;; Ok, this article is to be visible, so we add it to the limit
6555         ;; and return 1.
6556         (push number gnus-newsgroup-limit)
6557         1))))
6558
6559 (defun gnus-expunge-thread (thread)
6560   "Mark all articles in THREAD as read."
6561   (let* ((number (mail-header-number (car thread))))
6562     (incf gnus-newsgroup-expunged-tally)
6563     ;; We also mark as read here, if that's wanted.
6564     (setq gnus-newsgroup-unreads
6565           (delq number gnus-newsgroup-unreads))
6566     (if gnus-newsgroup-auto-expire
6567         (push number gnus-newsgroup-expirable)
6568       (push (cons number gnus-low-score-mark)
6569             gnus-newsgroup-reads)))
6570   ;; Go recursively through all subthreads.
6571   (mapcar 'gnus-expunge-thread (cdr thread)))
6572
6573 ;; Summary article oriented commands
6574
6575 (defun gnus-summary-refer-parent-article (n)
6576   "Refer parent article N times.
6577 If N is negative, go to ancestor -N instead.
6578 The difference between N and the number of articles fetched is returned."
6579   (interactive "p")
6580   (let ((skip 1)
6581         error header ref)
6582     (when (not (natnump n))
6583       (setq skip (abs n)
6584             n 1))
6585     (while (and (> n 0)
6586                 (not error))
6587       (setq header (gnus-summary-article-header))
6588       (if (and (eq (mail-header-number header)
6589                    (cdr gnus-article-current))
6590                (equal gnus-newsgroup-name
6591                       (car gnus-article-current)))
6592           ;; If we try to find the parent of the currently
6593           ;; displayed article, then we take a look at the actual
6594           ;; References header, since this is slightly more
6595           ;; reliable than the References field we got from the
6596           ;; server.
6597           (save-excursion
6598             (set-buffer gnus-original-article-buffer)
6599             (nnheader-narrow-to-headers)
6600             (unless (setq ref (message-fetch-field "references"))
6601               (setq ref (message-fetch-field "in-reply-to")))
6602             (widen))
6603         (setq ref
6604               ;; It's not the current article, so we take a bet on
6605               ;; the value we got from the server.
6606               (mail-header-references header)))
6607       (if (and ref
6608                (not (equal ref "")))
6609           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6610             (gnus-message 1 "Couldn't find parent"))
6611         (gnus-message 1 "No references in article %d"
6612                       (gnus-summary-article-number))
6613         (setq error t))
6614       (decf n))
6615     (gnus-summary-position-point)
6616     n))
6617
6618 (defun gnus-summary-refer-references ()
6619   "Fetch all articles mentioned in the References header.
6620 Return the number of articles fetched."
6621   (interactive)
6622   (let ((ref (mail-header-references (gnus-summary-article-header)))
6623         (current (gnus-summary-article-number))
6624         (n 0))
6625     (if (or (not ref)
6626             (equal ref ""))
6627         (error "No References in the current article")
6628       ;; For each Message-ID in the References header...
6629       (while (string-match "<[^>]*>" ref)
6630         (incf n)
6631         ;; ... fetch that article.
6632         (gnus-summary-refer-article
6633          (prog1 (match-string 0 ref)
6634            (setq ref (substring ref (match-end 0))))))
6635       (gnus-summary-goto-subject current)
6636       (gnus-summary-position-point)
6637       n)))
6638
6639 (defun gnus-summary-refer-thread (&optional limit)
6640   "Fetch all articles in the current thread.
6641 If LIMIT (the numerical prefix), fetch that many old headers instead
6642 of what's specified by the `gnus-refer-thread-limit' variable."
6643   (interactive "P")
6644   (let ((id (mail-header-id (gnus-summary-article-header)))
6645         (limit (if limit (prefix-numeric-value limit)
6646                  gnus-refer-thread-limit)))
6647     ;; We want to fetch LIMIT *old* headers, but we also have to
6648     ;; re-fetch all the headers in the current buffer, because many of
6649     ;; them may be undisplayed.  So we adjust LIMIT.
6650     (when (numberp limit)
6651       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6652     (unless (eq gnus-fetch-old-headers 'invisible)
6653       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6654       ;; Retrieve the headers and read them in.
6655       (if (eq (gnus-retrieve-headers
6656                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6657               'nov)
6658           (gnus-build-all-threads)
6659         (error "Can't fetch thread from backends that don't support NOV"))
6660       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6661     (gnus-summary-limit-include-thread id)))
6662
6663 (defun gnus-summary-refer-article (message-id &optional arg)
6664   "Fetch an article specified by MESSAGE-ID.
6665 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6666 or `gnus-select-method', no matter what backend the article comes from."
6667   (interactive "sMessage-ID: \nP")
6668   (when (and (stringp message-id)
6669              (not (zerop (length message-id))))
6670     ;; Construct the correct Message-ID if necessary.
6671     ;; Suggested by tale@pawl.rpi.edu.
6672     (unless (string-match "^<" message-id)
6673       (setq message-id (concat "<" message-id)))
6674     (unless (string-match ">$" message-id)
6675       (setq message-id (concat message-id ">")))
6676     (let* ((header (gnus-id-to-header message-id))
6677            (sparse (and header
6678                         (gnus-summary-article-sparse-p
6679                          (mail-header-number header))
6680                         (memq (mail-header-number header)
6681                               gnus-newsgroup-limit))))
6682       (cond
6683        ;; If the article is present in the buffer we just go to it.
6684        ((and header
6685              (or (not (gnus-summary-article-sparse-p
6686                        (mail-header-number header)))
6687                  sparse))
6688         (prog1
6689             (gnus-summary-goto-article
6690              (mail-header-number header) nil t)
6691           (when sparse
6692             (gnus-summary-update-article (mail-header-number header)))))
6693        (t
6694         ;; We fetch the article
6695         (let ((gnus-override-method
6696                (cond ((gnus-news-group-p gnus-newsgroup-name)
6697                       gnus-refer-article-method)
6698                      (arg
6699                       (or gnus-refer-article-method gnus-select-method))
6700                      (t nil)))
6701               number)
6702           ;; Start the special refer-article method, if necessary.
6703           (when (and gnus-refer-article-method
6704                      (gnus-news-group-p gnus-newsgroup-name))
6705             (gnus-check-server gnus-refer-article-method))
6706           ;; Fetch the header, and display the article.
6707           (if (setq number (gnus-summary-insert-subject message-id))
6708               (gnus-summary-select-article nil nil nil number)
6709             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6710
6711 (defun gnus-summary-edit-parameters ()
6712   "Edit the group parameters of the current group."
6713   (interactive)
6714   (gnus-group-edit-group gnus-newsgroup-name 'params))
6715
6716 (defun gnus-summary-customize-parameters ()
6717   "Customize the group parameters of the current group."
6718   (interactive)
6719   (gnus-group-customize gnus-newsgroup-name))
6720
6721 (defun gnus-summary-enter-digest-group (&optional force)
6722   "Enter an nndoc group based on the current article.
6723 If FORCE, force a digest interpretation.  If not, try
6724 to guess what the document format is."
6725   (interactive "P")
6726   (let ((conf gnus-current-window-configuration))
6727     (save-excursion
6728       (gnus-summary-select-article))
6729     (setq gnus-current-window-configuration conf)
6730     (let* ((name (format "%s-%d"
6731                          (gnus-group-prefixed-name
6732                           gnus-newsgroup-name (list 'nndoc ""))
6733                          (save-excursion
6734                            (set-buffer gnus-summary-buffer)
6735                            gnus-current-article)))
6736            (ogroup gnus-newsgroup-name)
6737            (params (append (gnus-info-params (gnus-get-info ogroup))
6738                            (list (cons 'to-group ogroup))
6739                            (list (cons 'save-article-group ogroup))))
6740            (case-fold-search t)
6741            (buf (current-buffer))
6742            dig)
6743       (save-excursion
6744         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6745         (insert-buffer-substring gnus-original-article-buffer)
6746         ;; Remove lines that may lead nndoc to misinterpret the
6747         ;; document type.
6748         (narrow-to-region
6749          (goto-char (point-min))
6750          (or (search-forward "\n\n" nil t) (point)))
6751         (goto-char (point-min))
6752         (delete-matching-lines "^Path:\\|^From ")
6753         (widen))
6754       (unwind-protect
6755           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6756                     (gnus-newsgroup-ephemeral-ignored-charsets
6757                      gnus-newsgroup-ignored-charsets))
6758                 (gnus-group-read-ephemeral-group
6759                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6760                               (nndoc-article-type
6761                                ,(if force 'digest 'guess))) t))
6762               ;; Make all postings to this group go to the parent group.
6763               (nconc (gnus-info-params (gnus-get-info name))
6764                      params)
6765             ;; Couldn't select this doc group.
6766             (switch-to-buffer buf)
6767             (gnus-set-global-variables)
6768             (gnus-configure-windows 'summary)
6769             (gnus-message 3 "Article couldn't be entered?"))
6770         (kill-buffer dig)))))
6771
6772 (defun gnus-summary-read-document (n)
6773   "Open a new group based on the current article(s).
6774 This will allow you to read digests and other similar
6775 documents as newsgroups.
6776 Obeys the standard process/prefix convention."
6777   (interactive "P")
6778   (let* ((articles (gnus-summary-work-articles n))
6779          (ogroup gnus-newsgroup-name)
6780          (params (append (gnus-info-params (gnus-get-info ogroup))
6781                          (list (cons 'to-group ogroup))))
6782          article group egroup groups vgroup)
6783     (while (setq article (pop articles))
6784       (setq group (format "%s-%d" gnus-newsgroup-name article))
6785       (gnus-summary-remove-process-mark article)
6786       (when (gnus-summary-display-article article)
6787         (save-excursion
6788           (with-temp-buffer
6789             (insert-buffer-substring gnus-original-article-buffer)
6790             ;; Remove some headers that may lead nndoc to make
6791             ;; the wrong guess.
6792             (message-narrow-to-head)
6793             (goto-char (point-min))
6794             (delete-matching-lines "^\\(Path\\):\\|^From ")
6795             (widen)
6796             (if (setq egroup
6797                       (gnus-group-read-ephemeral-group
6798                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6799                                      (nndoc-article-type guess))
6800                        t nil t))
6801                 (progn
6802                   ;; Make all postings to this group go to the parent group.
6803                   (nconc (gnus-info-params (gnus-get-info egroup))
6804                          params)
6805                   (push egroup groups))
6806               ;; Couldn't select this doc group.
6807               (gnus-error 3 "Article couldn't be entered"))))))
6808     ;; Now we have selected all the documents.
6809     (cond
6810      ((not groups)
6811       (error "None of the articles could be interpreted as documents"))
6812      ((gnus-group-read-ephemeral-group
6813        (setq vgroup (format
6814                      "nnvirtual:%s-%s" gnus-newsgroup-name
6815                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6816        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6817        t
6818        (cons (current-buffer) 'summary)))
6819      (t
6820       (error "Couldn't select virtual nndoc group")))))
6821
6822 (defun gnus-summary-isearch-article (&optional regexp-p)
6823   "Do incremental search forward on the current article.
6824 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6825   (interactive "P")
6826   (gnus-summary-select-article)
6827   (gnus-configure-windows 'article)
6828   (gnus-eval-in-buffer-window gnus-article-buffer
6829     (save-restriction
6830       (widen)
6831       (isearch-forward regexp-p))))
6832
6833 (defun gnus-summary-search-article-forward (regexp &optional backward)
6834   "Search for an article containing REGEXP forward.
6835 If BACKWARD, search backward instead."
6836   (interactive
6837    (list (read-string
6838           (format "Search article %s (regexp%s): "
6839                   (if current-prefix-arg "backward" "forward")
6840                   (if gnus-last-search-regexp
6841                       (concat ", default " gnus-last-search-regexp)
6842                     "")))
6843          current-prefix-arg))
6844   (if (string-equal regexp "")
6845       (setq regexp (or gnus-last-search-regexp ""))
6846     (setq gnus-last-search-regexp regexp))
6847   (if (gnus-summary-search-article regexp backward)
6848       (gnus-summary-show-thread)
6849     (error "Search failed: \"%s\"" regexp)))
6850
6851 (defun gnus-summary-search-article-backward (regexp)
6852   "Search for an article containing REGEXP backward."
6853   (interactive
6854    (list (read-string
6855           (format "Search article backward (regexp%s): "
6856                   (if gnus-last-search-regexp
6857                       (concat ", default " gnus-last-search-regexp)
6858                     "")))))
6859   (gnus-summary-search-article-forward regexp 'backward))
6860
6861 (defun gnus-summary-search-article (regexp &optional backward)
6862   "Search for an article containing REGEXP.
6863 Optional argument BACKWARD means do search for backward.
6864 `gnus-select-article-hook' is not called during the search."
6865   ;; We have to require this here to make sure that the following
6866   ;; dynamic binding isn't shadowed by autoloading.
6867   (require 'gnus-async)
6868   (require 'gnus-art)
6869   (let ((gnus-select-article-hook nil)  ;Disable hook.
6870         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6871         (gnus-use-article-prefetch nil)
6872         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6873         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6874         (sum (current-buffer))
6875         (gnus-display-mime-function nil)
6876         (found nil)
6877         point)
6878     (gnus-save-hidden-threads
6879       (gnus-summary-select-article)
6880       (set-buffer gnus-article-buffer)
6881       (when backward
6882         (forward-line -1))
6883       (while (not found)
6884         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6885         (if (if backward
6886                 (re-search-backward regexp nil t)
6887               (re-search-forward regexp nil t))
6888             ;; We found the regexp.
6889             (progn
6890               (setq found 'found)
6891               (beginning-of-line)
6892               (set-window-start
6893                (get-buffer-window (current-buffer))
6894                (point))
6895               (forward-line 1)
6896               (set-buffer sum)
6897               (setq point (point)))
6898           ;; We didn't find it, so we go to the next article.
6899           (set-buffer sum)
6900           (setq found 'not)
6901           (while (eq found 'not)
6902             (if (not (if backward (gnus-summary-find-prev)
6903                        (gnus-summary-find-next)))
6904                 ;; No more articles.
6905                 (setq found t)
6906               ;; Select the next article and adjust point.
6907               (unless (gnus-summary-article-sparse-p
6908                        (gnus-summary-article-number))
6909                 (setq found nil)
6910                 (gnus-summary-select-article)
6911                 (set-buffer gnus-article-buffer)
6912                 (widen)
6913                 (goto-char (if backward (point-max) (point-min))))))))
6914       (gnus-message 7 ""))
6915     ;; Return whether we found the regexp.
6916     (when (eq found 'found)
6917       (goto-char point)
6918       (gnus-summary-show-thread)
6919       (gnus-summary-goto-subject gnus-current-article)
6920       (gnus-summary-position-point)
6921       t)))
6922
6923 (defun gnus-summary-find-matching (header regexp &optional backward unread
6924                                           not-case-fold)
6925   "Return a list of all articles that match REGEXP on HEADER.
6926 The search stars on the current article and goes forwards unless
6927 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6928 If UNREAD is non-nil, only unread articles will
6929 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6930 in the comparisons."
6931   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6932                 (gnus-data-find-list
6933                  (gnus-summary-article-number) (gnus-data-list backward))))
6934         (case-fold-search (not not-case-fold))
6935         articles d func)
6936     (if (consp header)
6937         (if (eq (car header) 'extra)
6938             (setq func
6939                   `(lambda (h)
6940                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6941                          "")))
6942           (error "%s is an invalid header" header))
6943       (unless (fboundp (intern (concat "mail-header-" header)))
6944         (error "%s is not a valid header" header))
6945       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6946     (while data
6947       (setq d (car data))
6948       (and (or (not unread)             ; We want all articles...
6949                (gnus-data-unread-p d))  ; Or just unreads.
6950            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6951            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6952            (push (gnus-data-number d) articles)) ; Success!
6953       (setq data (cdr data)))
6954     (nreverse articles)))
6955
6956 (defun gnus-summary-execute-command (header regexp command &optional backward)
6957   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6958 If HEADER is an empty string (or nil), the match is done on the entire
6959 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6960   (interactive
6961    (list (let ((completion-ignore-case t))
6962            (completing-read
6963             "Header name: "
6964             (mapcar (lambda (string) (list string))
6965                     '("Number" "Subject" "From" "Lines" "Date"
6966                       "Message-ID" "Xref" "References" "Body"))
6967             nil 'require-match))
6968          (read-string "Regexp: ")
6969          (read-key-sequence "Command: ")
6970          current-prefix-arg))
6971   (when (equal header "Body")
6972     (setq header ""))
6973   ;; Hidden thread subtrees must be searched as well.
6974   (gnus-summary-show-all-threads)
6975   ;; We don't want to change current point nor window configuration.
6976   (save-excursion
6977     (save-window-excursion
6978       (gnus-message 6 "Executing %s..." (key-description command))
6979       ;; We'd like to execute COMMAND interactively so as to give arguments.
6980       (gnus-execute header regexp
6981                     `(call-interactively ',(key-binding command))
6982                     backward)
6983       (gnus-message 6 "Executing %s...done" (key-description command)))))
6984
6985 (defun gnus-summary-beginning-of-article ()
6986   "Scroll the article back to the beginning."
6987   (interactive)
6988   (gnus-summary-select-article)
6989   (gnus-configure-windows 'article)
6990   (gnus-eval-in-buffer-window gnus-article-buffer
6991     (widen)
6992     (goto-char (point-min))
6993     (when gnus-page-broken
6994       (gnus-narrow-to-page))))
6995
6996 (defun gnus-summary-end-of-article ()
6997   "Scroll to the end of the article."
6998   (interactive)
6999   (gnus-summary-select-article)
7000   (gnus-configure-windows 'article)
7001   (gnus-eval-in-buffer-window gnus-article-buffer
7002     (widen)
7003     (goto-char (point-max))
7004     (recenter -3)
7005     (when gnus-page-broken
7006       (gnus-narrow-to-page))))
7007
7008 (defun gnus-summary-print-article (&optional filename n)
7009   "Generate and print a PostScript image of the N next (mail) articles.
7010
7011 If N is negative, print the N previous articles.  If N is nil and articles
7012 have been marked with the process mark, print these instead.
7013
7014 If the optional first argument FILENAME is nil, send the image to the
7015 printer.  If FILENAME is a string, save the PostScript image in a file with
7016 that name.  If FILENAME is a number, prompt the user for the name of the file
7017 to save in."
7018   (interactive (list (ps-print-preprint current-prefix-arg)
7019                      current-prefix-arg))
7020   (dolist (article (gnus-summary-work-articles n))
7021     (gnus-summary-select-article nil nil 'pseudo article)
7022     (gnus-eval-in-buffer-window gnus-article-buffer
7023       (let ((buffer (generate-new-buffer " *print*")))
7024         (unwind-protect
7025             (progn
7026               (copy-to-buffer buffer (point-min) (point-max))
7027               (set-buffer buffer)
7028               (gnus-article-delete-invisible-text)
7029               (let ((ps-left-header
7030                      (list
7031                       (concat "("
7032                               (mail-header-subject gnus-current-headers) ")")
7033                       (concat "("
7034                               (mail-header-from gnus-current-headers) ")")))
7035                     (ps-right-header
7036                      (list
7037                       "/pagenumberstring load"
7038                       (concat "("
7039                               (mail-header-date gnus-current-headers) ")"))))
7040                 (gnus-run-hooks 'gnus-ps-print-hook)
7041                 (save-excursion
7042                   (ps-print-buffer-with-faces filename))))
7043           (kill-buffer buffer))))))
7044
7045 (defun gnus-summary-show-article (&optional arg)
7046   "Force re-fetching of the current article.
7047 If ARG (the prefix) is non-nil, show the raw article without any
7048 article massaging functions being run."
7049   (interactive "P")
7050   (if (not arg)
7051       ;; Select the article the normal way.
7052       (gnus-summary-select-article nil 'force)
7053     ;; We have to require this here to make sure that the following
7054     ;; dynamic binding isn't shadowed by autoloading.
7055     (require 'gnus-async)
7056     (require 'gnus-art)
7057     ;; Bind the article treatment functions to nil.
7058     (let ((gnus-have-all-headers t)
7059           gnus-article-prepare-hook
7060           gnus-article-decode-hook
7061           gnus-display-mime-function
7062           gnus-break-pages
7063           gnus-visual)
7064       ;; Destroy any MIME parts.
7065       (when (gnus-buffer-live-p gnus-article-buffer)
7066         (save-excursion
7067           (set-buffer gnus-article-buffer)
7068           (mm-destroy-parts gnus-article-mime-handles)))
7069       (gnus-summary-select-article nil 'force)))
7070   (gnus-summary-goto-subject gnus-current-article)
7071   (gnus-summary-position-point))
7072
7073 (defun gnus-summary-verbose-headers (&optional arg)
7074   "Toggle permanent full header display.
7075 If ARG is a positive number, turn header display on.
7076 If ARG is a negative number, turn header display off."
7077   (interactive "P")
7078   (setq gnus-show-all-headers
7079         (cond ((or (not (numberp arg))
7080                    (zerop arg))
7081                (not gnus-show-all-headers))
7082               ((natnump arg)
7083                t)))
7084   (gnus-summary-show-article))
7085
7086 (defun gnus-summary-toggle-header (&optional arg)
7087   "Show the headers if they are hidden, or hide them if they are shown.
7088 If ARG is a positive number, show the entire header.
7089 If ARG is a negative number, hide the unwanted header lines."
7090   (interactive "P")
7091   (save-excursion
7092     (set-buffer gnus-article-buffer)
7093     (save-restriction
7094       (let* ((buffer-read-only nil)
7095              (inhibit-point-motion-hooks t)
7096              hidden e)
7097         (save-restriction 
7098           (article-narrow-to-head)
7099           (setq hidden (gnus-article-hidden-text-p 'headers)))
7100         (goto-char (point-min))
7101         (when (search-forward "\n\n" nil t)
7102           (delete-region (point-min) (1- (point))))
7103         (goto-char (point-min))
7104         (save-excursion
7105           (set-buffer gnus-original-article-buffer)
7106           (goto-char (point-min))
7107           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7108         (insert-buffer-substring gnus-original-article-buffer 1 e)
7109         (save-restriction
7110           (narrow-to-region (point-min) (point))
7111           (article-decode-encoded-words)
7112           (if (or hidden
7113                   (and (numberp arg) (< arg 0)))
7114               (let ((gnus-treat-hide-headers nil)
7115                     (gnus-treat-hide-boring-headers nil))
7116                 (gnus-treat-article 'head))
7117             (gnus-treat-article 'head)))))))
7118
7119 (defun gnus-summary-show-all-headers ()
7120   "Make all header lines visible."
7121   (interactive)
7122   (gnus-article-show-all-headers))
7123
7124 (defun gnus-summary-caesar-message (&optional arg)
7125   "Caesar rotate the current article by 13.
7126 The numerical prefix specifies how many places to rotate each letter
7127 forward."
7128   (interactive "P")
7129   (gnus-summary-select-article)
7130   (let ((mail-header-separator ""))
7131     (gnus-eval-in-buffer-window gnus-article-buffer
7132       (save-restriction
7133         (widen)
7134         (let ((start (window-start))
7135               buffer-read-only)
7136           (message-caesar-buffer-body arg)
7137           (set-window-start (get-buffer-window (current-buffer)) start))))))
7138
7139 (defun gnus-summary-stop-page-breaking ()
7140   "Stop page breaking in the current article."
7141   (interactive)
7142   (gnus-summary-select-article)
7143   (gnus-eval-in-buffer-window gnus-article-buffer
7144     (widen)
7145     (when (gnus-visual-p 'page-marker)
7146       (let ((buffer-read-only nil))
7147         (gnus-remove-text-with-property 'gnus-prev)
7148         (gnus-remove-text-with-property 'gnus-next))
7149       (setq gnus-page-broken nil))))
7150
7151 (defun gnus-summary-move-article (&optional n to-newsgroup
7152                                             select-method action)
7153   "Move the current article to a different newsgroup.
7154 If N is a positive number, move the N next articles.
7155 If N is a negative number, move the N previous articles.
7156 If N is nil and any articles have been marked with the process mark,
7157 move those articles instead.
7158 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7159 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7160 re-spool using this method.
7161
7162 For this function to work, both the current newsgroup and the
7163 newsgroup that you want to move to have to support the `request-move'
7164 and `request-accept' functions."
7165   (interactive "P")
7166   (unless action
7167     (setq action 'move))
7168   ;; Disable marking as read.
7169   (let (gnus-mark-article-hook)
7170     (save-window-excursion
7171       (gnus-summary-select-article)))
7172   ;; Check whether the source group supports the required functions.
7173   (cond ((and (eq action 'move)
7174               (not (gnus-check-backend-function
7175                     'request-move-article gnus-newsgroup-name)))
7176          (error "The current group does not support article moving"))
7177         ((and (eq action 'crosspost)
7178               (not (gnus-check-backend-function
7179                     'request-replace-article gnus-newsgroup-name)))
7180          (error "The current group does not support article editing")))
7181   (let ((articles (gnus-summary-work-articles n))
7182         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7183         (names '((move "Move" "Moving")
7184                  (copy "Copy" "Copying")
7185                  (crosspost "Crosspost" "Crossposting")))
7186         (copy-buf (save-excursion
7187                     (nnheader-set-temp-buffer " *copy article*")))
7188         art-group to-method new-xref article to-groups)
7189     (unless (assq action names)
7190       (error "Unknown action %s" action))
7191     ;; Read the newsgroup name.
7192     (when (and (not to-newsgroup)
7193                (not select-method))
7194       (setq to-newsgroup
7195             (gnus-read-move-group-name
7196              (cadr (assq action names))
7197              (symbol-value (intern (format "gnus-current-%s-group" action)))
7198              articles prefix))
7199       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7200     (setq to-method (or select-method
7201                         (gnus-group-name-to-method to-newsgroup)))
7202     ;; Check the method we are to move this article to...
7203     (unless (gnus-check-backend-function
7204              'request-accept-article (car to-method))
7205       (error "%s does not support article copying" (car to-method)))
7206     (unless (gnus-check-server to-method)
7207       (error "Can't open server %s" (car to-method)))
7208     (gnus-message 6 "%s to %s: %s..."
7209                   (caddr (assq action names))
7210                   (or (car select-method) to-newsgroup) articles)
7211     (while articles
7212       (setq article (pop articles))
7213       (setq
7214        art-group
7215        (cond
7216         ;; Move the article.
7217         ((eq action 'move)
7218          ;; Remove this article from future suppression.
7219          (gnus-dup-unsuppress-article article)
7220          (gnus-request-move-article
7221           article                       ; Article to move
7222           gnus-newsgroup-name           ; From newsgroup
7223           (nth 1 (gnus-find-method-for-group
7224                   gnus-newsgroup-name)) ; Server
7225           (list 'gnus-request-accept-article
7226                 to-newsgroup (list 'quote select-method)
7227                 (not articles) t)               ; Accept form
7228           (not articles)))              ; Only save nov last time
7229         ;; Copy the article.
7230         ((eq action 'copy)
7231          (save-excursion
7232            (set-buffer copy-buf)
7233            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7234              (gnus-request-accept-article
7235               to-newsgroup select-method (not articles) t))))
7236         ;; Crosspost the article.
7237         ((eq action 'crosspost)
7238          (let ((xref (message-tokenize-header
7239                       (mail-header-xref (gnus-summary-article-header article))
7240                       " ")))
7241            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7242                                   ":" article))
7243            (unless xref
7244              (setq xref (list (system-name))))
7245            (setq new-xref
7246                  (concat
7247                   (mapconcat 'identity
7248                              (delete "Xref:" (delete new-xref xref))
7249                              " ")
7250                   " " new-xref))
7251            (save-excursion
7252              (set-buffer copy-buf)
7253              ;; First put the article in the destination group.
7254              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7255              (when (consp (setq art-group
7256                                 (gnus-request-accept-article
7257                                  to-newsgroup select-method (not articles))))
7258                (setq new-xref (concat new-xref " " (car art-group)
7259                                       ":" (cdr art-group)))
7260                ;; Now we have the new Xrefs header, so we insert
7261                ;; it and replace the new article.
7262                (nnheader-replace-header "Xref" new-xref)
7263                (gnus-request-replace-article
7264                 (cdr art-group) to-newsgroup (current-buffer))
7265                art-group))))))
7266       (cond
7267        ((not art-group)
7268         (gnus-message 1 "Couldn't %s article %s"
7269                       (cadr (assq action names)) article))
7270        ((and (eq art-group 'junk)
7271              (eq action 'move))
7272         (gnus-summary-mark-article article gnus-canceled-mark)
7273         (gnus-message 4 "Deleted article %s" article))
7274        (t
7275         (let* ((pto-group (gnus-group-prefixed-name
7276                            (car art-group) to-method))
7277                (entry
7278                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7279                (info (nth 2 entry))
7280                (to-group (gnus-info-group info))
7281                to-marks)
7282           ;; Update the group that has been moved to.
7283           (when (and info
7284                      (memq action '(move copy)))
7285             (unless (member to-group to-groups)
7286               (push to-group to-groups))
7287
7288             (unless (memq article gnus-newsgroup-unreads)
7289               (push 'read to-marks)
7290               (gnus-info-set-read
7291                info (gnus-add-to-range (gnus-info-read info)
7292                                        (list (cdr art-group)))))
7293
7294             ;; Copy any marks over to the new group.
7295             (let ((marks gnus-article-mark-lists)
7296                   (to-article (cdr art-group)))
7297
7298               ;; See whether the article is to be put in the cache.
7299               (when gnus-use-cache
7300                 (gnus-cache-possibly-enter-article
7301                  to-group to-article
7302                  (memq article gnus-newsgroup-marked)
7303                  (memq article gnus-newsgroup-dormant)
7304                  (memq article gnus-newsgroup-unreads)))
7305
7306               (when (and (equal to-group gnus-newsgroup-name)
7307                          (not (memq article gnus-newsgroup-unreads)))
7308                 ;; Mark this article as read in this group.
7309                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7310                 (setcdr (gnus-active to-group) to-article)
7311                 (setcdr gnus-newsgroup-active to-article))
7312
7313               (while marks
7314                 (when (memq article (symbol-value
7315                                      (intern (format "gnus-newsgroup-%s"
7316                                                      (caar marks)))))
7317                   (push (cdar marks) to-marks)
7318                   ;; If the other group is the same as this group,
7319                   ;; then we have to add the mark to the list.
7320                   (when (equal to-group gnus-newsgroup-name)
7321                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7322                          (cons to-article
7323                                (symbol-value
7324                                 (intern (format "gnus-newsgroup-%s"
7325                                                 (caar marks)))))))
7326                   ;; Copy the marks to other group.
7327                   (gnus-add-marked-articles
7328                    to-group (cdar marks) (list to-article) info))
7329                 (setq marks (cdr marks)))
7330
7331               (gnus-request-set-mark to-group (list (list (list to-article)
7332                                                           'set
7333                                                           to-marks)))
7334
7335               (gnus-dribble-enter
7336                (concat "(gnus-group-set-info '"
7337                        (gnus-prin1-to-string (gnus-get-info to-group))
7338                        ")"))))
7339
7340           ;; Update the Xref header in this article to point to
7341           ;; the new crossposted article we have just created.
7342           (when (eq action 'crosspost)
7343             (save-excursion
7344               (set-buffer copy-buf)
7345               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7346               (nnheader-replace-header "Xref" new-xref)
7347               (gnus-request-replace-article
7348                article gnus-newsgroup-name (current-buffer)))))
7349
7350         ;;;!!!Why is this necessary?
7351         (set-buffer gnus-summary-buffer)
7352
7353         (gnus-summary-goto-subject article)
7354         (when (eq action 'move)
7355           (gnus-summary-mark-article article gnus-canceled-mark))))
7356       (gnus-summary-remove-process-mark article))
7357     ;; Re-activate all groups that have been moved to.
7358     (while to-groups
7359       (save-excursion
7360         (set-buffer gnus-group-buffer)
7361         (when (gnus-group-goto-group (car to-groups) t)
7362           (gnus-group-get-new-news-this-group 1 t))
7363         (pop to-groups)))
7364
7365     (gnus-kill-buffer copy-buf)
7366     (gnus-summary-position-point)
7367     (gnus-set-mode-line 'summary)))
7368
7369 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7370   "Move the current article to a different newsgroup.
7371 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7372 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7373 re-spool using this method."
7374   (interactive "P")
7375   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7376
7377 (defun gnus-summary-crosspost-article (&optional n)
7378   "Crosspost the current article to some other group."
7379   (interactive "P")
7380   (gnus-summary-move-article n nil nil 'crosspost))
7381
7382 (defcustom gnus-summary-respool-default-method nil
7383   "Default method for respooling an article.
7384 If nil, use to the current newsgroup method."
7385   :type '(choice (gnus-select-method :value (nnml ""))
7386                  (const nil))
7387   :group 'gnus-summary-mail)
7388
7389 (defun gnus-summary-respool-article (&optional n method)
7390   "Respool the current article.
7391 The article will be squeezed through the mail spooling process again,
7392 which means that it will be put in some mail newsgroup or other
7393 depending on `nnmail-split-methods'.
7394 If N is a positive number, respool the N next articles.
7395 If N is a negative number, respool the N previous articles.
7396 If N is nil and any articles have been marked with the process mark,
7397 respool those articles instead.
7398
7399 Respooling can be done both from mail groups and \"real\" newsgroups.
7400 In the former case, the articles in question will be moved from the
7401 current group into whatever groups they are destined to.  In the
7402 latter case, they will be copied into the relevant groups."
7403   (interactive
7404    (list current-prefix-arg
7405          (let* ((methods (gnus-methods-using 'respool))
7406                 (methname
7407                  (symbol-name (or gnus-summary-respool-default-method
7408                                   (car (gnus-find-method-for-group
7409                                         gnus-newsgroup-name)))))
7410                 (method
7411                  (gnus-completing-read
7412                   methname "What backend do you want to use when respooling?"
7413                   methods nil t nil 'gnus-mail-method-history))
7414                 ms)
7415            (cond
7416             ((zerop (length (setq ms (gnus-servers-using-backend
7417                                       (intern method)))))
7418              (list (intern method) ""))
7419             ((= 1 (length ms))
7420              (car ms))
7421             (t
7422              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7423                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7424                            ms-alist))))))))
7425   (unless method
7426     (error "No method given for respooling"))
7427   (if (assoc (symbol-name
7428               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7429              (gnus-methods-using 'respool))
7430       (gnus-summary-move-article n nil method)
7431     (gnus-summary-copy-article n nil method)))
7432
7433 (defun gnus-summary-import-article (file)
7434   "Import an arbitrary file into a mail newsgroup."
7435   (interactive "fImport file: ")
7436   (let ((group gnus-newsgroup-name)
7437         (now (current-time))
7438         atts lines)
7439     (unless (gnus-check-backend-function 'request-accept-article group)
7440       (error "%s does not support article importing" group))
7441     (or (file-readable-p file)
7442         (not (file-regular-p file))
7443         (error "Can't read %s" file))
7444     (save-excursion
7445       (set-buffer (gnus-get-buffer-create " *import file*"))
7446       (erase-buffer)
7447       (insert-file-contents file)
7448       (goto-char (point-min))
7449       (unless (nnheader-article-p)
7450         ;; This doesn't look like an article, so we fudge some headers.
7451         (setq atts (file-attributes file)
7452               lines (count-lines (point-min) (point-max)))
7453         (insert "From: " (read-string "From: ") "\n"
7454                 "Subject: " (read-string "Subject: ") "\n"
7455                 "Date: " (message-make-date (nth 5 atts))
7456                 "\n"
7457                 "Message-ID: " (message-make-message-id) "\n"
7458                 "Lines: " (int-to-string lines) "\n"
7459                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7460       (gnus-request-accept-article group nil t)
7461       (kill-buffer (current-buffer)))))
7462
7463 (defun gnus-summary-article-posted-p ()
7464   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7465 This will be the case if the article has both been mailed and posted."
7466   (interactive)
7467   (let ((id (mail-header-references (gnus-summary-article-header)))
7468         (gnus-override-method
7469          (or gnus-refer-article-method gnus-select-method)))
7470     (if (gnus-request-head id "")
7471         (gnus-message 2 "The current message was found on %s"
7472                       gnus-override-method)
7473       (gnus-message 2 "The current message couldn't be found on %s"
7474                     gnus-override-method)
7475       nil)))
7476
7477 (defun gnus-summary-expire-articles (&optional now)
7478   "Expire all articles that are marked as expirable in the current group."
7479   (interactive)
7480   (when (gnus-check-backend-function
7481          'request-expire-articles gnus-newsgroup-name)
7482     ;; This backend supports expiry.
7483     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7484            (expirable (if total
7485                           (progn
7486                             ;; We need to update the info for
7487                             ;; this group for `gnus-list-of-read-articles'
7488                             ;; to give us the right answer.
7489                             (gnus-run-hooks 'gnus-exit-group-hook)
7490                             (gnus-summary-update-info)
7491                             (gnus-list-of-read-articles gnus-newsgroup-name))
7492                         (setq gnus-newsgroup-expirable
7493                               (sort gnus-newsgroup-expirable '<))))
7494            (expiry-wait (if now 'immediate
7495                           (gnus-group-find-parameter
7496                            gnus-newsgroup-name 'expiry-wait)))
7497            es)
7498       (when expirable
7499         ;; There are expirable articles in this group, so we run them
7500         ;; through the expiry process.
7501         (gnus-message 6 "Expiring articles...")
7502         (unless (gnus-check-group gnus-newsgroup-name)
7503           (error "Can't open server for %s" gnus-newsgroup-name))
7504         ;; The list of articles that weren't expired is returned.
7505         (save-excursion
7506           (if expiry-wait
7507               (let ((nnmail-expiry-wait-function nil)
7508                     (nnmail-expiry-wait expiry-wait))
7509                 (setq es (gnus-request-expire-articles
7510                           expirable gnus-newsgroup-name)))
7511             (setq es (gnus-request-expire-articles
7512                       expirable gnus-newsgroup-name))))
7513         (unless total
7514           (setq gnus-newsgroup-expirable es))
7515         ;; We go through the old list of expirable, and mark all
7516         ;; really expired articles as nonexistent.
7517         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7518           (let ((gnus-use-cache nil))
7519             (while expirable
7520               (unless (memq (car expirable) es)
7521                 (when (gnus-data-find (car expirable))
7522                   (gnus-summary-mark-article
7523                    (car expirable) gnus-canceled-mark)))
7524               (setq expirable (cdr expirable)))))
7525         (gnus-message 6 "Expiring articles...done")))))
7526
7527 (defun gnus-summary-expire-articles-now ()
7528   "Expunge all expirable articles in the current group.
7529 This means that *all* articles that are marked as expirable will be
7530 deleted forever, right now."
7531   (interactive)
7532   (or gnus-expert-user
7533       (gnus-yes-or-no-p
7534        "Are you really, really, really sure you want to delete all these messages? ")
7535       (error "Phew!"))
7536   (gnus-summary-expire-articles t))
7537
7538 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7539 (defun gnus-summary-delete-article (&optional n)
7540   "Delete the N next (mail) articles.
7541 This command actually deletes articles.  This is not a marking
7542 command.  The article will disappear forever from your life, never to
7543 return.
7544 If N is negative, delete backwards.
7545 If N is nil and articles have been marked with the process mark,
7546 delete these instead."
7547   (interactive "P")
7548   (unless (gnus-check-backend-function 'request-expire-articles
7549                                        gnus-newsgroup-name)
7550     (error "The current newsgroup does not support article deletion"))
7551   ;; Compute the list of articles to delete.
7552   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7553         not-deleted)
7554     (if (and gnus-novice-user
7555              (not (gnus-yes-or-no-p
7556                    (format "Do you really want to delete %s forever? "
7557                            (if (> (length articles) 1)
7558                                (format "these %s articles" (length articles))
7559                              "this article")))))
7560         ()
7561       ;; Delete the articles.
7562       (setq not-deleted (gnus-request-expire-articles
7563                          articles gnus-newsgroup-name 'force))
7564       (while articles
7565         (gnus-summary-remove-process-mark (car articles))
7566         ;; The backend might not have been able to delete the article
7567         ;; after all.
7568         (unless (memq (car articles) not-deleted)
7569           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7570         (setq articles (cdr articles)))
7571       (when not-deleted
7572         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7573     (gnus-summary-position-point)
7574     (gnus-set-mode-line 'summary)
7575     not-deleted))
7576
7577 (defun gnus-summary-edit-article (&optional force)
7578   "Edit the current article.
7579 This will have permanent effect only in mail groups.
7580 If FORCE is non-nil, allow editing of articles even in read-only
7581 groups."
7582   (interactive "P")
7583   (save-excursion
7584     (set-buffer gnus-summary-buffer)
7585     (let ((mail-parse-charset gnus-newsgroup-charset)
7586           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7587       (gnus-set-global-variables)
7588       (when (and (not force)
7589                  (gnus-group-read-only-p))
7590         (error "The current newsgroup does not support article editing"))
7591       (gnus-summary-show-article t)
7592       (gnus-article-edit-article
7593        'mime-to-mml
7594        `(lambda (no-highlight)
7595           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7596                 (mail-parse-ignored-charsets 
7597                  ',gnus-newsgroup-ignored-charsets))
7598             (mml-to-mime)
7599             (gnus-summary-edit-article-done
7600              ,(or (mail-header-references gnus-current-headers) "")
7601              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7602
7603 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7604
7605 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7606                                                  no-highlight)
7607   "Make edits to the current article permanent."
7608   (interactive)
7609   ;; Replace the article.
7610   (let ((buf (current-buffer)))
7611     (with-temp-buffer
7612       (insert-buffer-substring buf)
7613       (if (and (not read-only)
7614                (not (gnus-request-replace-article
7615                      (cdr gnus-article-current) (car gnus-article-current)
7616                      (current-buffer) t)))
7617           (error "Couldn't replace article")
7618         ;; Update the summary buffer.
7619         (if (and references
7620                  (equal (message-tokenize-header references " ")
7621                         (message-tokenize-header
7622                          (or (message-fetch-field "references") "") " ")))
7623             ;; We only have to update this line.
7624             (save-excursion
7625               (save-restriction
7626                 (message-narrow-to-head)
7627                 (let ((head (buffer-string))
7628                       header)
7629                   (with-temp-buffer
7630                     (insert (format "211 %d Article retrieved.\n"
7631                                     (cdr gnus-article-current)))
7632                     (insert head)
7633                     (insert ".\n")
7634                     (let ((nntp-server-buffer (current-buffer)))
7635                       (setq header (car (gnus-get-newsgroup-headers
7636                                          (save-excursion
7637                                            (set-buffer gnus-summary-buffer)
7638                                            gnus-newsgroup-dependencies)
7639                                          t))))
7640                     (save-excursion
7641                       (set-buffer gnus-summary-buffer)
7642                       (gnus-data-set-header
7643                        (gnus-data-find (cdr gnus-article-current))
7644                        header)
7645                       (gnus-summary-update-article-line
7646                        (cdr gnus-article-current) header))))))
7647           ;; Update threads.
7648           (set-buffer (or buffer gnus-summary-buffer))
7649           (gnus-summary-update-article (cdr gnus-article-current)))
7650         ;; Prettify the article buffer again.
7651         (unless no-highlight
7652           (save-excursion
7653             (set-buffer gnus-article-buffer)
7654             ;;;!!! Fix this -- article should be rehighlighted.
7655             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7656             (set-buffer gnus-original-article-buffer)
7657             (gnus-request-article
7658              (cdr gnus-article-current)
7659              (car gnus-article-current) (current-buffer))))
7660         ;; Prettify the summary buffer line.
7661         (when (gnus-visual-p 'summary-highlight 'highlight)
7662           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7663
7664 (defun gnus-summary-edit-wash (key)
7665   "Perform editing command KEY in the article buffer."
7666   (interactive
7667    (list
7668     (progn
7669       (message "%s" (concat (this-command-keys) "- "))
7670       (read-char))))
7671   (message "")
7672   (gnus-summary-edit-article)
7673   (execute-kbd-macro (concat (this-command-keys) key))
7674   (gnus-article-edit-done))
7675
7676 ;;; Respooling
7677
7678 (defun gnus-summary-respool-query (&optional silent trace)
7679   "Query where the respool algorithm would put this article."
7680   (interactive)
7681   (let (gnus-mark-article-hook)
7682     (gnus-summary-select-article)
7683     (save-excursion
7684       (set-buffer gnus-original-article-buffer)
7685       (save-restriction
7686         (message-narrow-to-head)
7687         (let ((groups (nnmail-article-group 'identity trace)))
7688           (unless silent
7689             (if groups
7690                 (message "This message would go to %s"
7691                          (mapconcat 'car groups ", "))
7692               (message "This message would go to no groups"))
7693             groups))))))
7694
7695 (defun gnus-summary-respool-trace ()
7696   "Trace where the respool algorithm would put this article.
7697 Display a buffer showing all fancy splitting patterns which matched."
7698   (interactive)
7699   (gnus-summary-respool-query nil t))
7700
7701 ;; Summary marking commands.
7702
7703 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7704   "Mark articles which has the same subject as read, and then select the next.
7705 If UNMARK is positive, remove any kind of mark.
7706 If UNMARK is negative, tick articles."
7707   (interactive "P")
7708   (when unmark
7709     (setq unmark (prefix-numeric-value unmark)))
7710   (let ((count
7711          (gnus-summary-mark-same-subject
7712           (gnus-summary-article-subject) unmark)))
7713     ;; Select next unread article.  If auto-select-same mode, should
7714     ;; select the first unread article.
7715     (gnus-summary-next-article t (and gnus-auto-select-same
7716                                       (gnus-summary-article-subject)))
7717     (gnus-message 7 "%d article%s marked as %s"
7718                   count (if (= count 1) " is" "s are")
7719                   (if unmark "unread" "read"))))
7720
7721 (defun gnus-summary-kill-same-subject (&optional unmark)
7722   "Mark articles which has the same subject as read.
7723 If UNMARK is positive, remove any kind of mark.
7724 If UNMARK is negative, tick articles."
7725   (interactive "P")
7726   (when unmark
7727     (setq unmark (prefix-numeric-value unmark)))
7728   (let ((count
7729          (gnus-summary-mark-same-subject
7730           (gnus-summary-article-subject) unmark)))
7731     ;; If marked as read, go to next unread subject.
7732     (when (null unmark)
7733       ;; Go to next unread subject.
7734       (gnus-summary-next-subject 1 t))
7735     (gnus-message 7 "%d articles are marked as %s"
7736                   count (if unmark "unread" "read"))))
7737
7738 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7739   "Mark articles with same SUBJECT as read, and return marked number.
7740 If optional argument UNMARK is positive, remove any kinds of marks.
7741 If optional argument UNMARK is negative, mark articles as unread instead."
7742   (let ((count 1))
7743     (save-excursion
7744       (cond
7745        ((null unmark)                   ; Mark as read.
7746         (while (and
7747                 (progn
7748                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7749                   (gnus-summary-show-thread) t)
7750                 (gnus-summary-find-subject subject))
7751           (setq count (1+ count))))
7752        ((> unmark 0)                    ; Tick.
7753         (while (and
7754                 (progn
7755                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7756                   (gnus-summary-show-thread) t)
7757                 (gnus-summary-find-subject subject))
7758           (setq count (1+ count))))
7759        (t                               ; Mark as unread.
7760         (while (and
7761                 (progn
7762                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7763                   (gnus-summary-show-thread) t)
7764                 (gnus-summary-find-subject subject))
7765           (setq count (1+ count)))))
7766       (gnus-set-mode-line 'summary)
7767       ;; Return the number of marked articles.
7768       count)))
7769
7770 (defun gnus-summary-mark-as-processable (n &optional unmark)
7771   "Set the process mark on the next N articles.
7772 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7773 the process mark instead.  The difference between N and the actual
7774 number of articles marked is returned."
7775   (interactive "p")
7776   (let ((backward (< n 0))
7777         (n (abs n)))
7778     (while (and
7779             (> n 0)
7780             (if unmark
7781                 (gnus-summary-remove-process-mark
7782                  (gnus-summary-article-number))
7783               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7784             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7785       (setq n (1- n)))
7786     (when (/= 0 n)
7787       (gnus-message 7 "No more articles"))
7788     (gnus-summary-recenter)
7789     (gnus-summary-position-point)
7790     n))
7791
7792 (defun gnus-summary-unmark-as-processable (n)
7793   "Remove the process mark from the next N articles.
7794 If N is negative, unmark backward instead.  The difference between N and
7795 the actual number of articles unmarked is returned."
7796   (interactive "p")
7797   (gnus-summary-mark-as-processable n t))
7798
7799 (defun gnus-summary-unmark-all-processable ()
7800   "Remove the process mark from all articles."
7801   (interactive)
7802   (save-excursion
7803     (while gnus-newsgroup-processable
7804       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7805   (gnus-summary-position-point))
7806
7807 (defun gnus-summary-mark-as-expirable (n)
7808   "Mark N articles forward as expirable.
7809 If N is negative, mark backward instead.  The difference between N and
7810 the actual number of articles marked is returned."
7811   (interactive "p")
7812   (gnus-summary-mark-forward n gnus-expirable-mark))
7813
7814 (defun gnus-summary-mark-article-as-replied (article)
7815   "Mark ARTICLE replied and update the summary line."
7816   (push article gnus-newsgroup-replied)
7817   (let ((buffer-read-only nil))
7818     (when (gnus-summary-goto-subject article nil t)
7819       (gnus-summary-update-secondary-mark article))))
7820
7821 (defun gnus-summary-set-bookmark (article)
7822   "Set a bookmark in current article."
7823   (interactive (list (gnus-summary-article-number)))
7824   (when (or (not (get-buffer gnus-article-buffer))
7825             (not gnus-current-article)
7826             (not gnus-article-current)
7827             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7828     (error "No current article selected"))
7829   ;; Remove old bookmark, if one exists.
7830   (let ((old (assq article gnus-newsgroup-bookmarks)))
7831     (when old
7832       (setq gnus-newsgroup-bookmarks
7833             (delq old gnus-newsgroup-bookmarks))))
7834   ;; Set the new bookmark, which is on the form
7835   ;; (article-number . line-number-in-body).
7836   (push
7837    (cons article
7838          (save-excursion
7839            (set-buffer gnus-article-buffer)
7840            (count-lines
7841             (min (point)
7842                  (save-excursion
7843                    (goto-char (point-min))
7844                    (search-forward "\n\n" nil t)
7845                    (point)))
7846             (point))))
7847    gnus-newsgroup-bookmarks)
7848   (gnus-message 6 "A bookmark has been added to the current article."))
7849
7850 (defun gnus-summary-remove-bookmark (article)
7851   "Remove the bookmark from the current article."
7852   (interactive (list (gnus-summary-article-number)))
7853   ;; Remove old bookmark, if one exists.
7854   (let ((old (assq article gnus-newsgroup-bookmarks)))
7855     (if old
7856         (progn
7857           (setq gnus-newsgroup-bookmarks
7858                 (delq old gnus-newsgroup-bookmarks))
7859           (gnus-message 6 "Removed bookmark."))
7860       (gnus-message 6 "No bookmark in current article."))))
7861
7862 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7863 (defun gnus-summary-mark-as-dormant (n)
7864   "Mark N articles forward as dormant.
7865 If N is negative, mark backward instead.  The difference between N and
7866 the actual number of articles marked is returned."
7867   (interactive "p")
7868   (gnus-summary-mark-forward n gnus-dormant-mark))
7869
7870 (defun gnus-summary-set-process-mark (article)
7871   "Set the process mark on ARTICLE and update the summary line."
7872   (setq gnus-newsgroup-processable
7873         (cons article
7874               (delq article gnus-newsgroup-processable)))
7875   (when (gnus-summary-goto-subject article)
7876     (gnus-summary-show-thread)
7877     (gnus-summary-goto-subject article)
7878     (gnus-summary-update-secondary-mark article)))
7879
7880 (defun gnus-summary-remove-process-mark (article)
7881   "Remove the process mark from ARTICLE and update the summary line."
7882   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7883   (when (gnus-summary-goto-subject article)
7884     (gnus-summary-show-thread)
7885     (gnus-summary-goto-subject article)
7886     (gnus-summary-update-secondary-mark article)))
7887
7888 (defun gnus-summary-set-saved-mark (article)
7889   "Set the process mark on ARTICLE and update the summary line."
7890   (push article gnus-newsgroup-saved)
7891   (when (gnus-summary-goto-subject article)
7892     (gnus-summary-update-secondary-mark article)))
7893
7894 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7895   "Mark N articles as read forwards.
7896 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7897 The difference between N and the actual number of articles marked is
7898 returned."
7899   (interactive "p")
7900   (gnus-summary-show-thread)
7901   (let ((backward (< n 0))
7902         (gnus-summary-goto-unread
7903          (and gnus-summary-goto-unread
7904               (not (eq gnus-summary-goto-unread 'never))
7905               (not (memq mark (list gnus-unread-mark
7906                                     gnus-ticked-mark gnus-dormant-mark)))))
7907         (n (abs n))
7908         (mark (or mark gnus-del-mark)))
7909     (while (and (> n 0)
7910                 (gnus-summary-mark-article nil mark no-expire)
7911                 (zerop (gnus-summary-next-subject
7912                         (if backward -1 1)
7913                         (and gnus-summary-goto-unread
7914                              (not (eq gnus-summary-goto-unread 'never)))
7915                         t)))
7916       (setq n (1- n)))
7917     (when (/= 0 n)
7918       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7919     (gnus-summary-recenter)
7920     (gnus-summary-position-point)
7921     (gnus-set-mode-line 'summary)
7922     n))
7923
7924 (defun gnus-summary-mark-article-as-read (mark)
7925   "Mark the current article quickly as read with MARK."
7926   (let ((article (gnus-summary-article-number)))
7927     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7928     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7929     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7930     (push (cons article mark) gnus-newsgroup-reads)
7931     ;; Possibly remove from cache, if that is used.
7932     (when gnus-use-cache
7933       (gnus-cache-enter-remove-article article))
7934     ;; Allow the backend to change the mark.
7935     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7936     ;; Check for auto-expiry.
7937     (when (and gnus-newsgroup-auto-expire
7938                (memq mark gnus-auto-expirable-marks))
7939       (setq mark gnus-expirable-mark)
7940       ;; Let the backend know about the mark change.
7941       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7942       (push article gnus-newsgroup-expirable))
7943     ;; Set the mark in the buffer.
7944     (gnus-summary-update-mark mark 'unread)
7945     t))
7946
7947 (defun gnus-summary-mark-article-as-unread (mark)
7948   "Mark the current article quickly as unread with MARK."
7949   (let* ((article (gnus-summary-article-number))
7950          (old-mark (gnus-summary-article-mark article)))
7951     ;; Allow the backend to change the mark.
7952     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7953     (if (eq mark old-mark)
7954         t
7955       (if (<= article 0)
7956           (progn
7957             (gnus-error 1 "Can't mark negative article numbers")
7958             nil)
7959         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7960         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7961         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7962         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7963         (cond ((= mark gnus-ticked-mark)
7964                (push article gnus-newsgroup-marked))
7965               ((= mark gnus-dormant-mark)
7966                (push article gnus-newsgroup-dormant))
7967               (t
7968                (push article gnus-newsgroup-unreads)))
7969         (gnus-pull article gnus-newsgroup-reads)
7970
7971         ;; See whether the article is to be put in the cache.
7972         (and gnus-use-cache
7973              (vectorp (gnus-summary-article-header article))
7974              (save-excursion
7975                (gnus-cache-possibly-enter-article
7976                 gnus-newsgroup-name article
7977                 (= mark gnus-ticked-mark)
7978                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7979
7980         ;; Fix the mark.
7981         (gnus-summary-update-mark mark 'unread)
7982         t))))
7983
7984 (defun gnus-summary-mark-article (&optional article mark no-expire)
7985   "Mark ARTICLE with MARK.  MARK can be any character.
7986 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7987 `??' (dormant) and `?E' (expirable).
7988 If MARK is nil, then the default character `?r' is used.
7989 If ARTICLE is nil, then the article on the current line will be
7990 marked."
7991   ;; The mark might be a string.
7992   (when (stringp mark)
7993     (setq mark (aref mark 0)))
7994   ;; If no mark is given, then we check auto-expiring.
7995   (when (null mark)
7996     (setq mark gnus-del-mark))
7997   (when (and (not no-expire)
7998              gnus-newsgroup-auto-expire
7999              (memq mark gnus-auto-expirable-marks))
8000     (setq mark gnus-expirable-mark))
8001   (let ((article (or article (gnus-summary-article-number)))
8002         (old-mark (gnus-summary-article-mark article)))
8003     ;; Allow the backend to change the mark.
8004     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8005     (if (eq mark old-mark)
8006         t
8007       (unless article
8008         (error "No article on current line"))
8009       (if (not (if (or (= mark gnus-unread-mark)
8010                        (= mark gnus-ticked-mark)
8011                        (= mark gnus-dormant-mark))
8012                    (gnus-mark-article-as-unread article mark)
8013                  (gnus-mark-article-as-read article mark)))
8014           t
8015         ;; See whether the article is to be put in the cache.
8016         (and gnus-use-cache
8017              (not (= mark gnus-canceled-mark))
8018              (vectorp (gnus-summary-article-header article))
8019              (save-excursion
8020                (gnus-cache-possibly-enter-article
8021                 gnus-newsgroup-name article
8022                 (= mark gnus-ticked-mark)
8023                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8024
8025         (when (gnus-summary-goto-subject article nil t)
8026           (let ((buffer-read-only nil))
8027             (gnus-summary-show-thread)
8028             ;; Fix the mark.
8029             (gnus-summary-update-mark mark 'unread)
8030             t))))))
8031
8032 (defun gnus-summary-update-secondary-mark (article)
8033   "Update the secondary (read, process, cache) mark."
8034   (gnus-summary-update-mark
8035    (cond ((memq article gnus-newsgroup-processable)
8036           gnus-process-mark)
8037          ((memq article gnus-newsgroup-cached)
8038           gnus-cached-mark)
8039          ((memq article gnus-newsgroup-replied)
8040           gnus-replied-mark)
8041          ((memq article gnus-newsgroup-saved)
8042           gnus-saved-mark)
8043          (t gnus-unread-mark))
8044    'replied)
8045   (when (gnus-visual-p 'summary-highlight 'highlight)
8046     (gnus-run-hooks 'gnus-summary-update-hook))
8047   t)
8048
8049 (defun gnus-summary-update-mark (mark type)
8050   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8051         (buffer-read-only nil))
8052     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8053     (when forward
8054       (when (looking-at "\r")
8055         (incf forward))
8056       (when (<= (+ forward (point)) (point-max))
8057         ;; Go to the right position on the line.
8058         (goto-char (+ forward (point)))
8059         ;; Replace the old mark with the new mark.
8060         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8061         ;; Optionally update the marks by some user rule.
8062         (when (eq type 'unread)
8063           (gnus-data-set-mark
8064            (gnus-data-find (gnus-summary-article-number)) mark)
8065           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8066
8067 (defun gnus-mark-article-as-read (article &optional mark)
8068   "Enter ARTICLE in the pertinent lists and remove it from others."
8069   ;; Make the article expirable.
8070   (let ((mark (or mark gnus-del-mark)))
8071     (if (= mark gnus-expirable-mark)
8072         (push article gnus-newsgroup-expirable)
8073       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8074     ;; Remove from unread and marked lists.
8075     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8076     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8077     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8078     (push (cons article mark) gnus-newsgroup-reads)
8079     ;; Possibly remove from cache, if that is used.
8080     (when gnus-use-cache
8081       (gnus-cache-enter-remove-article article))
8082     t))
8083
8084 (defun gnus-mark-article-as-unread (article &optional mark)
8085   "Enter ARTICLE in the pertinent lists and remove it from others."
8086   (let ((mark (or mark gnus-ticked-mark)))
8087     (if (<= article 0)
8088         (progn
8089           (gnus-error 1 "Can't mark negative article numbers")
8090           nil)
8091       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8092             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8093             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8094             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8095
8096       ;; Unsuppress duplicates?
8097       (when gnus-suppress-duplicates
8098         (gnus-dup-unsuppress-article article))
8099
8100       (cond ((= mark gnus-ticked-mark)
8101              (push article gnus-newsgroup-marked))
8102             ((= mark gnus-dormant-mark)
8103              (push article gnus-newsgroup-dormant))
8104             (t
8105              (push article gnus-newsgroup-unreads)))
8106       (gnus-pull article gnus-newsgroup-reads)
8107       t)))
8108
8109 (defalias 'gnus-summary-mark-as-unread-forward
8110   'gnus-summary-tick-article-forward)
8111 (make-obsolete 'gnus-summary-mark-as-unread-forward
8112                'gnus-summary-tick-article-forward)
8113 (defun gnus-summary-tick-article-forward (n)
8114   "Tick N articles forwards.
8115 If N is negative, tick backwards instead.
8116 The difference between N and the number of articles ticked is returned."
8117   (interactive "p")
8118   (gnus-summary-mark-forward n gnus-ticked-mark))
8119
8120 (defalias 'gnus-summary-mark-as-unread-backward
8121   'gnus-summary-tick-article-backward)
8122 (make-obsolete 'gnus-summary-mark-as-unread-backward
8123                'gnus-summary-tick-article-backward)
8124 (defun gnus-summary-tick-article-backward (n)
8125   "Tick N articles backwards.
8126 The difference between N and the number of articles ticked is returned."
8127   (interactive "p")
8128   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8129
8130 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8131 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8132 (defun gnus-summary-tick-article (&optional article clear-mark)
8133   "Mark current article as unread.
8134 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8135 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8136   (interactive)
8137   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8138                                        gnus-ticked-mark)))
8139
8140 (defun gnus-summary-mark-as-read-forward (n)
8141   "Mark N articles as read forwards.
8142 If N is negative, mark backwards instead.
8143 The difference between N and the actual number of articles marked is
8144 returned."
8145   (interactive "p")
8146   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8147
8148 (defun gnus-summary-mark-as-read-backward (n)
8149   "Mark the N articles as read backwards.
8150 The difference between N and the actual number of articles marked is
8151 returned."
8152   (interactive "p")
8153   (gnus-summary-mark-forward
8154    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8155
8156 (defun gnus-summary-mark-as-read (&optional article mark)
8157   "Mark current article as read.
8158 ARTICLE specifies the article to be marked as read.
8159 MARK specifies a string to be inserted at the beginning of the line."
8160   (gnus-summary-mark-article article mark))
8161
8162 (defun gnus-summary-clear-mark-forward (n)
8163   "Clear marks from N articles forward.
8164 If N is negative, clear backward instead.
8165 The difference between N and the number of marks cleared is returned."
8166   (interactive "p")
8167   (gnus-summary-mark-forward n gnus-unread-mark))
8168
8169 (defun gnus-summary-clear-mark-backward (n)
8170   "Clear marks from N articles backward.
8171 The difference between N and the number of marks cleared is returned."
8172   (interactive "p")
8173   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8174
8175 (defun gnus-summary-mark-unread-as-read ()
8176   "Intended to be used by `gnus-summary-mark-article-hook'."
8177   (when (memq gnus-current-article gnus-newsgroup-unreads)
8178     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8179
8180 (defun gnus-summary-mark-read-and-unread-as-read ()
8181   "Intended to be used by `gnus-summary-mark-article-hook'."
8182   (let ((mark (gnus-summary-article-mark)))
8183     (when (or (gnus-unread-mark-p mark)
8184               (gnus-read-mark-p mark))
8185       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8186
8187 (defun gnus-summary-mark-region-as-read (point mark all)
8188   "Mark all unread articles between point and mark as read.
8189 If given a prefix, mark all articles between point and mark as read,
8190 even ticked and dormant ones."
8191   (interactive "r\nP")
8192   (save-excursion
8193     (let (article)
8194       (goto-char point)
8195       (beginning-of-line)
8196       (while (and
8197               (< (point) mark)
8198               (progn
8199                 (when (or all
8200                           (memq (setq article (gnus-summary-article-number))
8201                                 gnus-newsgroup-unreads))
8202                   (gnus-summary-mark-article article gnus-del-mark))
8203                 t)
8204               (gnus-summary-find-next))))))
8205
8206 (defun gnus-summary-mark-below (score mark)
8207   "Mark articles with score less than SCORE with MARK."
8208   (interactive "P\ncMark: ")
8209   (setq score (if score
8210                   (prefix-numeric-value score)
8211                 (or gnus-summary-default-score 0)))
8212   (save-excursion
8213     (set-buffer gnus-summary-buffer)
8214     (goto-char (point-min))
8215     (while
8216         (progn
8217           (and (< (gnus-summary-article-score) score)
8218                (gnus-summary-mark-article nil mark))
8219           (gnus-summary-find-next)))))
8220
8221 (defun gnus-summary-kill-below (&optional score)
8222   "Mark articles with score below SCORE as read."
8223   (interactive "P")
8224   (gnus-summary-mark-below score gnus-killed-mark))
8225
8226 (defun gnus-summary-clear-above (&optional score)
8227   "Clear all marks from articles with score above SCORE."
8228   (interactive "P")
8229   (gnus-summary-mark-above score gnus-unread-mark))
8230
8231 (defun gnus-summary-tick-above (&optional score)
8232   "Tick all articles with score above SCORE."
8233   (interactive "P")
8234   (gnus-summary-mark-above score gnus-ticked-mark))
8235
8236 (defun gnus-summary-mark-above (score mark)
8237   "Mark articles with score over SCORE with MARK."
8238   (interactive "P\ncMark: ")
8239   (setq score (if score
8240                   (prefix-numeric-value score)
8241                 (or gnus-summary-default-score 0)))
8242   (save-excursion
8243     (set-buffer gnus-summary-buffer)
8244     (goto-char (point-min))
8245     (while (and (progn
8246                   (when (> (gnus-summary-article-score) score)
8247                     (gnus-summary-mark-article nil mark))
8248                   t)
8249                 (gnus-summary-find-next)))))
8250
8251 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8252 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8253 (defun gnus-summary-limit-include-expunged (&optional no-error)
8254   "Display all the hidden articles that were expunged for low scores."
8255   (interactive)
8256   (let ((buffer-read-only nil))
8257     (let ((scored gnus-newsgroup-scored)
8258           headers h)
8259       (while scored
8260         (unless (gnus-summary-goto-subject (caar scored))
8261           (and (setq h (gnus-summary-article-header (caar scored)))
8262                (< (cdar scored) gnus-summary-expunge-below)
8263                (push h headers)))
8264         (setq scored (cdr scored)))
8265       (if (not headers)
8266           (when (not no-error)
8267             (error "No expunged articles hidden"))
8268         (goto-char (point-min))
8269         (gnus-summary-prepare-unthreaded (nreverse headers))
8270         (goto-char (point-min))
8271         (gnus-summary-position-point)
8272         t))))
8273
8274 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8275   "Mark all unread articles in this newsgroup as read.
8276 If prefix argument ALL is non-nil, ticked and dormant articles will
8277 also be marked as read.
8278 If QUIETLY is non-nil, no questions will be asked.
8279 If TO-HERE is non-nil, it should be a point in the buffer.  All
8280 articles before this point will be marked as read.
8281 Note that this function will only catch up the unread article
8282 in the current summary buffer limitation.
8283 The number of articles marked as read is returned."
8284   (interactive "P")
8285   (prog1
8286       (save-excursion
8287         (when (or quietly
8288                   (not gnus-interactive-catchup) ;Without confirmation?
8289                   gnus-expert-user
8290                   (gnus-y-or-n-p
8291                    (if all
8292                        "Mark absolutely all articles as read? "
8293                      "Mark all unread articles as read? ")))
8294           (if (and not-mark
8295                    (not gnus-newsgroup-adaptive)
8296                    (not gnus-newsgroup-auto-expire)
8297                    (not gnus-suppress-duplicates)
8298                    (or (not gnus-use-cache)
8299                        (eq gnus-use-cache 'passive)))
8300               (progn
8301                 (when all
8302                   (setq gnus-newsgroup-marked nil
8303                         gnus-newsgroup-dormant nil))
8304                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8305             ;; We actually mark all articles as canceled, which we
8306             ;; have to do when using auto-expiry or adaptive scoring.
8307             (gnus-summary-show-all-threads)
8308             (when (gnus-summary-first-subject (not all) t)
8309               (while (and
8310                       (if to-here (< (point) to-here) t)
8311                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8312                       (gnus-summary-find-next (not all) nil nil t))))
8313             (gnus-set-mode-line 'summary))
8314           t))
8315     (gnus-summary-position-point)))
8316
8317 (defun gnus-summary-catchup-to-here (&optional all)
8318   "Mark all unticked articles before the current one as read.
8319 If ALL is non-nil, also mark ticked and dormant articles as read."
8320   (interactive "P")
8321   (save-excursion
8322     (gnus-save-hidden-threads
8323       (let ((beg (point)))
8324         ;; We check that there are unread articles.
8325         (when (or all (gnus-summary-find-prev))
8326           (gnus-summary-catchup all t beg)))))
8327   (gnus-summary-position-point))
8328
8329 (defun gnus-summary-catchup-all (&optional quietly)
8330   "Mark all articles in this newsgroup as read."
8331   (interactive "P")
8332   (gnus-summary-catchup t quietly))
8333
8334 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8335   "Mark all unread articles in this group as read, then exit.
8336 If prefix argument ALL is non-nil, all articles are marked as read."
8337   (interactive "P")
8338   (when (gnus-summary-catchup all quietly nil 'fast)
8339     ;; Select next newsgroup or exit.
8340     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8341              (eq gnus-auto-select-next 'quietly))
8342         (gnus-summary-next-group nil)
8343       (gnus-summary-exit))))
8344
8345 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8346   "Mark all articles in this newsgroup as read, and then exit."
8347   (interactive "P")
8348   (gnus-summary-catchup-and-exit t quietly))
8349
8350 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8351 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8352   "Mark all articles in this group as read and select the next group.
8353 If given a prefix, mark all articles, unread as well as ticked, as
8354 read."
8355   (interactive "P")
8356   (save-excursion
8357     (gnus-summary-catchup all))
8358   (gnus-summary-next-article t nil nil t))
8359
8360 ;; Thread-based commands.
8361
8362 (defun gnus-summary-articles-in-thread (&optional article)
8363   "Return a list of all articles in the current thread.
8364 If ARTICLE is non-nil, return all articles in the thread that starts
8365 with that article."
8366   (let* ((article (or article (gnus-summary-article-number)))
8367          (data (gnus-data-find-list article))
8368          (top-level (gnus-data-level (car data)))
8369          (top-subject
8370           (cond ((null gnus-thread-operation-ignore-subject)
8371                  (gnus-simplify-subject-re
8372                   (mail-header-subject (gnus-data-header (car data)))))
8373                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8374                  (gnus-simplify-subject-fuzzy
8375                   (mail-header-subject (gnus-data-header (car data)))))
8376                 (t nil)))
8377          (end-point (save-excursion
8378                       (if (gnus-summary-go-to-next-thread)
8379                           (point) (point-max))))
8380          articles)
8381     (while (and data
8382                 (< (gnus-data-pos (car data)) end-point))
8383       (when (or (not top-subject)
8384                 (string= top-subject
8385                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8386                              (gnus-simplify-subject-fuzzy
8387                               (mail-header-subject
8388                                (gnus-data-header (car data))))
8389                            (gnus-simplify-subject-re
8390                             (mail-header-subject
8391                              (gnus-data-header (car data)))))))
8392         (push (gnus-data-number (car data)) articles))
8393       (unless (and (setq data (cdr data))
8394                    (> (gnus-data-level (car data)) top-level))
8395         (setq data nil)))
8396     ;; Return the list of articles.
8397     (nreverse articles)))
8398
8399 (defun gnus-summary-rethread-current ()
8400   "Rethread the thread the current article is part of."
8401   (interactive)
8402   (let* ((gnus-show-threads t)
8403          (article (gnus-summary-article-number))
8404          (id (mail-header-id (gnus-summary-article-header)))
8405          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8406     (unless id
8407       (error "No article on the current line"))
8408     (gnus-rebuild-thread id)
8409     (gnus-summary-goto-subject article)))
8410
8411 (defun gnus-summary-reparent-thread ()
8412   "Make the current article child of the marked (or previous) article.
8413
8414 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8415 is non-nil or the Subject: of both articles are the same."
8416   (interactive)
8417   (unless (not (gnus-group-read-only-p))
8418     (error "The current newsgroup does not support article editing"))
8419   (unless (<= (length gnus-newsgroup-processable) 1)
8420     (error "No more than one article may be marked"))
8421   (save-window-excursion
8422     (let ((gnus-article-buffer " *reparent*")
8423           (current-article (gnus-summary-article-number))
8424           ;; First grab the marked article, otherwise one line up.
8425           (parent-article (if (not (null gnus-newsgroup-processable))
8426                               (car gnus-newsgroup-processable)
8427                             (save-excursion
8428                               (if (eq (forward-line -1) 0)
8429                                   (gnus-summary-article-number)
8430                                 (error "Beginning of summary buffer"))))))
8431       (unless (not (eq current-article parent-article))
8432         (error "An article may not be self-referential"))
8433       (let ((message-id (mail-header-id
8434                          (gnus-summary-article-header parent-article))))
8435         (unless (and message-id (not (equal message-id "")))
8436           (error "No message-id in desired parent"))
8437         (gnus-with-article current-article
8438           (goto-char (point-min))
8439           (if (re-search-forward "^References: " nil t)
8440               (progn
8441                 (re-search-forward "^[^ \t]" nil t)
8442                 (forward-line -1)
8443                 (end-of-line)
8444                 (insert " " message-id))
8445             (insert "References: " message-id "\n")))
8446         (set-buffer gnus-summary-buffer)
8447         (gnus-summary-unmark-all-processable)
8448         (gnus-summary-update-article current-article)
8449         (gnus-summary-rethread-current)
8450         (gnus-message 3 "Article %d is now the child of article %d"
8451                       current-article parent-article)))))
8452
8453 (defun gnus-summary-toggle-threads (&optional arg)
8454   "Toggle showing conversation threads.
8455 If ARG is positive number, turn showing conversation threads on."
8456   (interactive "P")
8457   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8458     (setq gnus-show-threads
8459           (if (null arg) (not gnus-show-threads)
8460             (> (prefix-numeric-value arg) 0)))
8461     (gnus-summary-prepare)
8462     (gnus-summary-goto-subject current)
8463     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8464     (gnus-summary-position-point)))
8465
8466 (defun gnus-summary-show-all-threads ()
8467   "Show all threads."
8468   (interactive)
8469   (save-excursion
8470     (let ((buffer-read-only nil))
8471       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8472   (gnus-summary-position-point))
8473
8474 (defun gnus-summary-show-thread ()
8475   "Show thread subtrees.
8476 Returns nil if no thread was there to be shown."
8477   (interactive)
8478   (let ((buffer-read-only nil)
8479         (orig (point))
8480         ;; first goto end then to beg, to have point at beg after let
8481         (end (progn (end-of-line) (point)))
8482         (beg (progn (beginning-of-line) (point))))
8483     (prog1
8484         ;; Any hidden lines here?
8485         (search-forward "\r" end t)
8486       (subst-char-in-region beg end ?\^M ?\n t)
8487       (goto-char orig)
8488       (gnus-summary-position-point))))
8489
8490 (defun gnus-summary-hide-all-threads ()
8491   "Hide all thread subtrees."
8492   (interactive)
8493   (save-excursion
8494     (goto-char (point-min))
8495     (gnus-summary-hide-thread)
8496     (while (zerop (gnus-summary-next-thread 1 t))
8497       (gnus-summary-hide-thread)))
8498   (gnus-summary-position-point))
8499
8500 (defun gnus-summary-hide-thread ()
8501   "Hide thread subtrees.
8502 Returns nil if no threads were there to be hidden."
8503   (interactive)
8504   (let ((buffer-read-only nil)
8505         (start (point))
8506         (article (gnus-summary-article-number)))
8507     (goto-char start)
8508     ;; Go forward until either the buffer ends or the subthread
8509     ;; ends.
8510     (when (and (not (eobp))
8511                (or (zerop (gnus-summary-next-thread 1 t))
8512                    (goto-char (point-max))))
8513       (prog1
8514           (if (and (> (point) start)
8515                    (search-backward "\n" start t))
8516               (progn
8517                 (subst-char-in-region start (point) ?\n ?\^M)
8518                 (gnus-summary-goto-subject article))
8519             (goto-char start)
8520             nil)
8521         ;;(gnus-summary-position-point)
8522         ))))
8523
8524 (defun gnus-summary-go-to-next-thread (&optional previous)
8525   "Go to the same level (or less) next thread.
8526 If PREVIOUS is non-nil, go to previous thread instead.
8527 Return the article number moved to, or nil if moving was impossible."
8528   (let ((level (gnus-summary-thread-level))
8529         (way (if previous -1 1))
8530         (beg (point)))
8531     (forward-line way)
8532     (while (and (not (eobp))
8533                 (< level (gnus-summary-thread-level)))
8534       (forward-line way))
8535     (if (eobp)
8536         (progn
8537           (goto-char beg)
8538           nil)
8539       (setq beg (point))
8540       (prog1
8541           (gnus-summary-article-number)
8542         (goto-char beg)))))
8543
8544 (defun gnus-summary-next-thread (n &optional silent)
8545   "Go to the same level next N'th thread.
8546 If N is negative, search backward instead.
8547 Returns the difference between N and the number of skips actually
8548 done.
8549
8550 If SILENT, don't output messages."
8551   (interactive "p")
8552   (let ((backward (< n 0))
8553         (n (abs n)))
8554     (while (and (> n 0)
8555                 (gnus-summary-go-to-next-thread backward))
8556       (decf n))
8557     (unless silent
8558       (gnus-summary-position-point))
8559     (when (and (not silent) (/= 0 n))
8560       (gnus-message 7 "No more threads"))
8561     n))
8562
8563 (defun gnus-summary-prev-thread (n)
8564   "Go to the same level previous N'th thread.
8565 Returns the difference between N and the number of skips actually
8566 done."
8567   (interactive "p")
8568   (gnus-summary-next-thread (- n)))
8569
8570 (defun gnus-summary-go-down-thread ()
8571   "Go down one level in the current thread."
8572   (let ((children (gnus-summary-article-children)))
8573     (when children
8574       (gnus-summary-goto-subject (car children)))))
8575
8576 (defun gnus-summary-go-up-thread ()
8577   "Go up one level in the current thread."
8578   (let ((parent (gnus-summary-article-parent)))
8579     (when parent
8580       (gnus-summary-goto-subject parent))))
8581
8582 (defun gnus-summary-down-thread (n)
8583   "Go down thread N steps.
8584 If N is negative, go up instead.
8585 Returns the difference between N and how many steps down that were
8586 taken."
8587   (interactive "p")
8588   (let ((up (< n 0))
8589         (n (abs n)))
8590     (while (and (> n 0)
8591                 (if up (gnus-summary-go-up-thread)
8592                   (gnus-summary-go-down-thread)))
8593       (setq n (1- n)))
8594     (gnus-summary-position-point)
8595     (when (/= 0 n)
8596       (gnus-message 7 "Can't go further"))
8597     n))
8598
8599 (defun gnus-summary-up-thread (n)
8600   "Go up thread N steps.
8601 If N is negative, go up instead.
8602 Returns the difference between N and how many steps down that were
8603 taken."
8604   (interactive "p")
8605   (gnus-summary-down-thread (- n)))
8606
8607 (defun gnus-summary-top-thread ()
8608   "Go to the top of the thread."
8609   (interactive)
8610   (while (gnus-summary-go-up-thread))
8611   (gnus-summary-article-number))
8612
8613 (defun gnus-summary-kill-thread (&optional unmark)
8614   "Mark articles under current thread as read.
8615 If the prefix argument is positive, remove any kinds of marks.
8616 If the prefix argument is negative, tick articles instead."
8617   (interactive "P")
8618   (when unmark
8619     (setq unmark (prefix-numeric-value unmark)))
8620   (let ((articles (gnus-summary-articles-in-thread)))
8621     (save-excursion
8622       ;; Expand the thread.
8623       (gnus-summary-show-thread)
8624       ;; Mark all the articles.
8625       (while articles
8626         (gnus-summary-goto-subject (car articles))
8627         (cond ((null unmark)
8628                (gnus-summary-mark-article-as-read gnus-killed-mark))
8629               ((> unmark 0)
8630                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8631               (t
8632                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8633         (setq articles (cdr articles))))
8634     ;; Hide killed subtrees.
8635     (and (null unmark)
8636          gnus-thread-hide-killed
8637          (gnus-summary-hide-thread))
8638     ;; If marked as read, go to next unread subject.
8639     (when (null unmark)
8640       ;; Go to next unread subject.
8641       (gnus-summary-next-subject 1 t)))
8642   (gnus-set-mode-line 'summary))
8643
8644 ;; Summary sorting commands
8645
8646 (defun gnus-summary-sort-by-number (&optional reverse)
8647   "Sort the summary buffer by article number.
8648 Argument REVERSE means reverse order."
8649   (interactive "P")
8650   (gnus-summary-sort 'number reverse))
8651
8652 (defun gnus-summary-sort-by-author (&optional reverse)
8653   "Sort the summary buffer by author name alphabetically.
8654 If case-fold-search is non-nil, case of letters is ignored.
8655 Argument REVERSE means reverse order."
8656   (interactive "P")
8657   (gnus-summary-sort 'author reverse))
8658
8659 (defun gnus-summary-sort-by-subject (&optional reverse)
8660   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8661 If case-fold-search is non-nil, case of letters is ignored.
8662 Argument REVERSE means reverse order."
8663   (interactive "P")
8664   (gnus-summary-sort 'subject reverse))
8665
8666 (defun gnus-summary-sort-by-date (&optional reverse)
8667   "Sort the summary buffer by date.
8668 Argument REVERSE means reverse order."
8669   (interactive "P")
8670   (gnus-summary-sort 'date reverse))
8671
8672 (defun gnus-summary-sort-by-score (&optional reverse)
8673   "Sort the summary buffer by score.
8674 Argument REVERSE means reverse order."
8675   (interactive "P")
8676   (gnus-summary-sort 'score reverse))
8677
8678 (defun gnus-summary-sort-by-lines (&optional reverse)
8679   "Sort the summary buffer by the number of lines.
8680 Argument REVERSE means reverse order."
8681   (interactive "P")
8682   (gnus-summary-sort 'lines reverse))
8683
8684 (defun gnus-summary-sort-by-chars (&optional reverse)
8685   "Sort the summary buffer by article length.
8686 Argument REVERSE means reverse order."
8687   (interactive "P")
8688   (gnus-summary-sort 'chars reverse))   
8689
8690 (defun gnus-summary-sort (predicate reverse)
8691   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8692   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8693          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8694          (gnus-thread-sort-functions
8695           (if (not reverse)
8696               thread
8697             `(lambda (t1 t2)
8698                (,thread t2 t1))))
8699          (gnus-article-sort-functions
8700           (if (not reverse)
8701               article
8702             `(lambda (t1 t2)
8703                (,article t2 t1))))
8704          (buffer-read-only)
8705          (gnus-summary-prepare-hook nil))
8706     ;; We do the sorting by regenerating the threads.
8707     (gnus-summary-prepare)
8708     ;; Hide subthreads if needed.
8709     (when (and gnus-show-threads gnus-thread-hide-subtree)
8710       (gnus-summary-hide-all-threads))))
8711
8712 ;; Summary saving commands.
8713
8714 (defun gnus-summary-save-article (&optional n not-saved)
8715   "Save the current article using the default saver function.
8716 If N is a positive number, save the N next articles.
8717 If N is a negative number, save the N previous articles.
8718 If N is nil and any articles have been marked with the process mark,
8719 save those articles instead.
8720 The variable `gnus-default-article-saver' specifies the saver function."
8721   (interactive "P")
8722   (let* ((articles (gnus-summary-work-articles n))
8723          (save-buffer (save-excursion
8724                         (nnheader-set-temp-buffer " *Gnus Save*")))
8725          (num (length articles))
8726          header file)
8727     (dolist (article articles)
8728       (setq header (gnus-summary-article-header article))
8729       (if (not (vectorp header))
8730           ;; This is a pseudo-article.
8731           (if (assq 'name header)
8732               (gnus-copy-file (cdr (assq 'name header)))
8733             (gnus-message 1 "Article %d is unsaveable" article))
8734         ;; This is a real article.
8735         (save-window-excursion
8736           (gnus-summary-select-article t nil nil article))
8737         (save-excursion
8738           (set-buffer save-buffer)
8739           (erase-buffer)
8740           (insert-buffer-substring gnus-original-article-buffer))
8741         (setq file (gnus-article-save save-buffer file num))
8742         (gnus-summary-remove-process-mark article)
8743         (unless not-saved
8744           (gnus-summary-set-saved-mark article))))
8745     (gnus-kill-buffer save-buffer)
8746     (gnus-summary-position-point)
8747     (gnus-set-mode-line 'summary)
8748     n))
8749
8750 (defun gnus-summary-pipe-output (&optional arg)
8751   "Pipe the current article to a subprocess.
8752 If N is a positive number, pipe the N next articles.
8753 If N is a negative number, pipe the N previous articles.
8754 If N is nil and any articles have been marked with the process mark,
8755 pipe those articles instead."
8756   (interactive "P")
8757   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8758     (gnus-summary-save-article arg t))
8759   (gnus-configure-windows 'pipe))
8760
8761 (defun gnus-summary-save-article-mail (&optional arg)
8762   "Append the current article to an mail file.
8763 If N is a positive number, save the N next articles.
8764 If N is a negative number, save the N previous articles.
8765 If N is nil and any articles have been marked with the process mark,
8766 save those articles instead."
8767   (interactive "P")
8768   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8769     (gnus-summary-save-article arg)))
8770
8771 (defun gnus-summary-save-article-rmail (&optional arg)
8772   "Append the current article to an rmail file.
8773 If N is a positive number, save the N next articles.
8774 If N is a negative number, save the N previous articles.
8775 If N is nil and any articles have been marked with the process mark,
8776 save those articles instead."
8777   (interactive "P")
8778   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8779     (gnus-summary-save-article arg)))
8780
8781 (defun gnus-summary-save-article-file (&optional arg)
8782   "Append the current article to a file.
8783 If N is a positive number, save the N next articles.
8784 If N is a negative number, save the N previous articles.
8785 If N is nil and any articles have been marked with the process mark,
8786 save those articles instead."
8787   (interactive "P")
8788   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8789     (gnus-summary-save-article arg)))
8790
8791 (defun gnus-summary-write-article-file (&optional arg)
8792   "Write the current article to a file, deleting the previous file.
8793 If N is a positive number, save the N next articles.
8794 If N is a negative number, save the N previous articles.
8795 If N is nil and any articles have been marked with the process mark,
8796 save those articles instead."
8797   (interactive "P")
8798   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8799     (gnus-summary-save-article arg)))
8800
8801 (defun gnus-summary-save-article-body-file (&optional arg)
8802   "Append the current article body to a file.
8803 If N is a positive number, save the N next articles.
8804 If N is a negative number, save the N previous articles.
8805 If N is nil and any articles have been marked with the process mark,
8806 save those articles instead."
8807   (interactive "P")
8808   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8809     (gnus-summary-save-article arg)))
8810
8811 (defun gnus-summary-pipe-message (program)
8812   "Pipe the current article through PROGRAM."
8813   (interactive "sProgram: ")
8814   (gnus-summary-select-article)
8815   (let ((mail-header-separator ""))
8816     (gnus-eval-in-buffer-window gnus-article-buffer
8817       (save-restriction
8818         (widen)
8819         (let ((start (window-start))
8820               buffer-read-only)
8821           (message-pipe-buffer-body program)
8822           (set-window-start (get-buffer-window (current-buffer)) start))))))
8823
8824 (defun gnus-get-split-value (methods)
8825   "Return a value based on the split METHODS."
8826   (let (split-name method result match)
8827     (when methods
8828       (save-excursion
8829         (set-buffer gnus-original-article-buffer)
8830         (save-restriction
8831           (nnheader-narrow-to-headers)
8832           (while methods
8833             (goto-char (point-min))
8834             (setq method (pop methods))
8835             (setq match (car method))
8836             (when (cond
8837                    ((stringp match)
8838                     ;; Regular expression.
8839                     (ignore-errors
8840                       (re-search-forward match nil t)))
8841                    ((gnus-functionp match)
8842                     ;; Function.
8843                     (save-restriction
8844                       (widen)
8845                       (setq result (funcall match gnus-newsgroup-name))))
8846                    ((consp match)
8847                     ;; Form.
8848                     (save-restriction
8849                       (widen)
8850                       (setq result (eval match)))))
8851               (setq split-name (append (cdr method) split-name))
8852               (cond ((stringp result)
8853                      (push (expand-file-name
8854                             result gnus-article-save-directory)
8855                            split-name))
8856                     ((consp result)
8857                      (setq split-name (append result split-name)))))))))
8858     (nreverse split-name)))
8859
8860 (defun gnus-valid-move-group-p (group)
8861   (and (boundp group)
8862        (symbol-name group)
8863        (symbol-value group)
8864        (gnus-get-function (gnus-find-method-for-group
8865                            (symbol-name group)) 'request-accept-article t)))
8866
8867 (defun gnus-read-move-group-name (prompt default articles prefix)
8868   "Read a group name."
8869   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8870          (minibuffer-confirm-incomplete nil) ; XEmacs
8871          (prom
8872           (format "%s %s to:"
8873                   prompt
8874                   (if (> (length articles) 1)
8875                       (format "these %d articles" (length articles))
8876                     "this article")))
8877          (to-newsgroup
8878           (cond
8879            ((null split-name)
8880             (gnus-completing-read default prom
8881                                   gnus-active-hashtb
8882                                   'gnus-valid-move-group-p
8883                                   nil prefix
8884                                   'gnus-group-history))
8885            ((= 1 (length split-name))
8886             (gnus-completing-read (car split-name) prom
8887                                   gnus-active-hashtb
8888                                   'gnus-valid-move-group-p
8889                                   nil nil
8890                                   'gnus-group-history))
8891            (t
8892             (gnus-completing-read nil prom
8893                                   (mapcar (lambda (el) (list el))
8894                                           (nreverse split-name))
8895                                   nil nil nil
8896                                   'gnus-group-history)))))
8897     (when to-newsgroup
8898       (if (or (string= to-newsgroup "")
8899               (string= to-newsgroup prefix))
8900           (setq to-newsgroup default))
8901       (unless to-newsgroup
8902         (error "No group name entered"))
8903       (or (gnus-active to-newsgroup)
8904           (gnus-activate-group to-newsgroup)
8905           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8906                                      to-newsgroup))
8907               (or (and (gnus-request-create-group
8908                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8909                        (gnus-activate-group to-newsgroup nil nil
8910                                             (gnus-group-name-to-method
8911                                              to-newsgroup)))
8912                   (error "Couldn't create group %s" to-newsgroup)))
8913           (error "No such group: %s" to-newsgroup)))
8914     to-newsgroup))
8915
8916 (defun gnus-summary-save-parts (type dir n reverse)
8917   "Save parts matching TYPE to DIR.
8918 If REVERSE, save parts that do not match TYPE."
8919   (interactive
8920    (list (read-string "Save parts of type: " "image/.*")
8921          (read-file-name "Save to directory: " t nil t)
8922          current-prefix-arg))
8923   (gnus-summary-iterate n
8924     (let ((gnus-display-mime-function nil)
8925           (gnus-inhibit-treatment t))
8926       (gnus-summary-select-article))
8927     (save-excursion
8928       (set-buffer gnus-article-buffer)
8929       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8930         (when handles
8931           (gnus-summary-save-parts-1 type dir handles reverse)
8932           (mm-destroy-parts handles))))))
8933
8934 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8935   (if (stringp (car handle))
8936       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8937               (cdr handle))
8938     (when (if reverse
8939               (not (string-match type (mm-handle-media-type handle)))
8940             (string-match type (mm-handle-media-type handle)))
8941       (let ((file (expand-file-name
8942                    (file-name-nondirectory
8943                     (or
8944                      (mail-content-type-get
8945                       (mm-handle-disposition handle) 'filename)
8946                      (concat gnus-newsgroup-name "." gnus-current-article)))
8947                    dir)))
8948         (unless (file-exists-p file)
8949           (mm-save-part-to-file handle file))))))
8950
8951 ;; Summary extract commands
8952
8953 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8954   (let ((buffer-read-only nil)
8955         (article (gnus-summary-article-number))
8956         after-article b e)
8957     (unless (gnus-summary-goto-subject article)
8958       (error "No such article: %d" article))
8959     (gnus-summary-position-point)
8960     ;; If all commands are to be bunched up on one line, we collect
8961     ;; them here.
8962     (unless gnus-view-pseudos-separately
8963       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8964             files action)
8965         (while ps
8966           (setq action (cdr (assq 'action (car ps))))
8967           (setq files (list (cdr (assq 'name (car ps)))))
8968           (while (and ps (cdr ps)
8969                       (string= (or action "1")
8970                                (or (cdr (assq 'action (cadr ps))) "2")))
8971             (push (cdr (assq 'name (cadr ps))) files)
8972             (setcdr ps (cddr ps)))
8973           (when files
8974             (when (not (string-match "%s" action))
8975               (push " " files))
8976             (push " " files)
8977             (when (assq 'execute (car ps))
8978               (setcdr (assq 'execute (car ps))
8979                       (funcall (if (string-match "%s" action)
8980                                    'format 'concat)
8981                                action
8982                                (mapconcat
8983                                 (lambda (f)
8984                                   (if (equal f " ")
8985                                       f
8986                                     (mm-quote-arg f)))
8987                                 files " ")))))
8988           (setq ps (cdr ps)))))
8989     (if (and gnus-view-pseudos (not not-view))
8990         (while pslist
8991           (when (assq 'execute (car pslist))
8992             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8993                                   (eq gnus-view-pseudos 'not-confirm)))
8994           (setq pslist (cdr pslist)))
8995       (save-excursion
8996         (while pslist
8997           (setq after-article (or (cdr (assq 'article (car pslist)))
8998                                   (gnus-summary-article-number)))
8999           (gnus-summary-goto-subject after-article)
9000           (forward-line 1)
9001           (setq b (point))
9002           (insert "    " (file-name-nondirectory
9003                           (cdr (assq 'name (car pslist))))
9004                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9005           (setq e (point))
9006           (forward-line -1)             ; back to `b'
9007           (gnus-add-text-properties
9008            b (1- e) (list 'gnus-number gnus-reffed-article-number
9009                           gnus-mouse-face-prop gnus-mouse-face))
9010           (gnus-data-enter
9011            after-article gnus-reffed-article-number
9012            gnus-unread-mark b (car pslist) 0 (- e b))
9013           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9014           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9015           (setq pslist (cdr pslist)))))))
9016
9017 (defun gnus-pseudos< (p1 p2)
9018   (let ((c1 (cdr (assq 'action p1)))
9019         (c2 (cdr (assq 'action p2))))
9020     (and c1 c2 (string< c1 c2))))
9021
9022 (defun gnus-request-pseudo-article (props)
9023   (cond ((assq 'execute props)
9024          (gnus-execute-command (cdr (assq 'execute props)))))
9025   (let ((gnus-current-article (gnus-summary-article-number)))
9026     (gnus-run-hooks 'gnus-mark-article-hook)))
9027
9028 (defun gnus-execute-command (command &optional automatic)
9029   (save-excursion
9030     (gnus-article-setup-buffer)
9031     (set-buffer gnus-article-buffer)
9032     (setq buffer-read-only nil)
9033     (let ((command (if automatic command
9034                      (read-string "Command: " (cons command 0)))))
9035       (erase-buffer)
9036       (insert "$ " command "\n\n")
9037       (if gnus-view-pseudo-asynchronously
9038           (start-process "gnus-execute" (current-buffer) shell-file-name
9039                          shell-command-switch command)
9040         (call-process shell-file-name nil t nil
9041                       shell-command-switch command)))))
9042
9043 ;; Summary kill commands.
9044
9045 (defun gnus-summary-edit-global-kill (article)
9046   "Edit the \"global\" kill file."
9047   (interactive (list (gnus-summary-article-number)))
9048   (gnus-group-edit-global-kill article))
9049
9050 (defun gnus-summary-edit-local-kill ()
9051   "Edit a local kill file applied to the current newsgroup."
9052   (interactive)
9053   (setq gnus-current-headers (gnus-summary-article-header))
9054   (gnus-group-edit-local-kill
9055    (gnus-summary-article-number) gnus-newsgroup-name))
9056
9057 ;;; Header reading.
9058
9059 (defun gnus-read-header (id &optional header)
9060   "Read the headers of article ID and enter them into the Gnus system."
9061   (let ((group gnus-newsgroup-name)
9062         (gnus-override-method
9063          (and (gnus-news-group-p gnus-newsgroup-name)
9064               gnus-refer-article-method))
9065         where)
9066     ;; First we check to see whether the header in question is already
9067     ;; fetched.
9068     (if (stringp id)
9069         ;; This is a Message-ID.
9070         (setq header (or header (gnus-id-to-header id)))
9071       ;; This is an article number.
9072       (setq header (or header (gnus-summary-article-header id))))
9073     (if (and header
9074              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9075         ;; We have found the header.
9076         header
9077       ;; If this is a sparse article, we have to nix out its
9078       ;; previous entry in the thread hashtb.
9079       (when (and header
9080                  (gnus-summary-article-sparse-p (mail-header-number header)))
9081         (let* ((parent (gnus-parent-id (mail-header-references header)))
9082                (thread (and parent (gnus-id-to-thread parent))))
9083           (when thread
9084             (delq (assq header thread) thread))))
9085       ;; We have to really fetch the header to this article.
9086       (save-excursion
9087         (set-buffer nntp-server-buffer)
9088         (when (setq where (gnus-request-head id group))
9089           (nnheader-fold-continuation-lines)
9090           (goto-char (point-max))
9091           (insert ".\n")
9092           (goto-char (point-min))
9093           (insert "211 ")
9094           (princ (cond
9095                   ((numberp id) id)
9096                   ((cdr where) (cdr where))
9097                   (header (mail-header-number header))
9098                   (t gnus-reffed-article-number))
9099                  (current-buffer))
9100           (insert " Article retrieved.\n"))
9101         (if (or (not where)
9102                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9103             ()                          ; Malformed head.
9104           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9105             (when (and (stringp id)
9106                        (not (string= (gnus-group-real-name group)
9107                                      (car where))))
9108               ;; If we fetched by Message-ID and the article came
9109               ;; from a different group, we fudge some bogus article
9110               ;; numbers for this article.
9111               (mail-header-set-number header gnus-reffed-article-number))
9112             (save-excursion
9113               (set-buffer gnus-summary-buffer)
9114               (decf gnus-reffed-article-number)
9115               (gnus-remove-header (mail-header-number header))
9116               (push header gnus-newsgroup-headers)
9117               (setq gnus-current-headers header)
9118               (push (mail-header-number header) gnus-newsgroup-limit)))
9119           header)))))
9120
9121 (defun gnus-remove-header (number)
9122   "Remove header NUMBER from `gnus-newsgroup-headers'."
9123   (if (and gnus-newsgroup-headers
9124            (= number (mail-header-number (car gnus-newsgroup-headers))))
9125       (pop gnus-newsgroup-headers)
9126     (let ((headers gnus-newsgroup-headers))
9127       (while (and (cdr headers)
9128                   (not (= number (mail-header-number (cadr headers)))))
9129         (pop headers))
9130       (when (cdr headers)
9131         (setcdr headers (cddr headers))))))
9132
9133 ;;;
9134 ;;; summary highlights
9135 ;;;
9136
9137 (defun gnus-highlight-selected-summary ()
9138   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9139   ;; Highlight selected article in summary buffer
9140   (when gnus-summary-selected-face
9141     (save-excursion
9142       (let* ((beg (progn (beginning-of-line) (point)))
9143              (end (progn (end-of-line) (point)))
9144              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9145              (from (if (get-text-property beg gnus-mouse-face-prop)
9146                        beg
9147                      (or (next-single-property-change
9148                           beg gnus-mouse-face-prop nil end)
9149                          beg)))
9150              (to
9151               (if (= from end)
9152                   (- from 2)
9153                 (or (next-single-property-change
9154                      from gnus-mouse-face-prop nil end)
9155                     end))))
9156         ;; If no mouse-face prop on line we will have to = from = end,
9157         ;; so we highlight the entire line instead.
9158         (when (= (+ to 2) from)
9159           (setq from beg)
9160           (setq to end))
9161         (if gnus-newsgroup-selected-overlay
9162             ;; Move old overlay.
9163             (gnus-move-overlay
9164              gnus-newsgroup-selected-overlay from to (current-buffer))
9165           ;; Create new overlay.
9166           (gnus-overlay-put
9167            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9168            'face gnus-summary-selected-face))))))
9169
9170 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9171 (defun gnus-summary-highlight-line ()
9172   "Highlight current line according to `gnus-summary-highlight'."
9173   (let* ((list gnus-summary-highlight)
9174          (p (point))
9175          (end (progn (end-of-line) (point)))
9176          ;; now find out where the line starts and leave point there.
9177          (beg (progn (beginning-of-line) (point)))
9178          (article (gnus-summary-article-number))
9179          (score (or (cdr (assq (or article gnus-current-article)
9180                                gnus-newsgroup-scored))
9181                     gnus-summary-default-score 0))
9182          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9183          (inhibit-read-only t))
9184     ;; Eval the cars of the lists until we find a match.
9185     (let ((default gnus-summary-default-score))
9186       (while (and list
9187                   (not (eval (caar list))))
9188         (setq list (cdr list))))
9189     (let ((face (cdar list)))
9190       (unless (eq face (get-text-property beg 'face))
9191         (gnus-put-text-property-excluding-characters-with-faces
9192          beg end 'face
9193          (setq face (if (boundp face) (symbol-value face) face)))
9194         (when gnus-summary-highlight-line-function
9195           (funcall gnus-summary-highlight-line-function article face))))
9196     (goto-char p)))
9197
9198 (defun gnus-update-read-articles (group unread &optional compute)
9199   "Update the list of read articles in GROUP."
9200   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9201          (entry (gnus-gethash group gnus-newsrc-hashtb))
9202          (info (nth 2 entry))
9203          (prev 1)
9204          (unread (sort (copy-sequence unread) '<))
9205          read)
9206     (if (or (not info) (not active))
9207         ;; There is no info on this group if it was, in fact,
9208         ;; killed.  Gnus stores no information on killed groups, so
9209         ;; there's nothing to be done.
9210         ;; One could store the information somewhere temporarily,
9211         ;; perhaps...  Hmmm...
9212         ()
9213       ;; Remove any negative articles numbers.
9214       (while (and unread (< (car unread) 0))
9215         (setq unread (cdr unread)))
9216       ;; Remove any expired article numbers
9217       (while (and unread (< (car unread) (car active)))
9218         (setq unread (cdr unread)))
9219       ;; Compute the ranges of read articles by looking at the list of
9220       ;; unread articles.
9221       (while unread
9222         (when (/= (car unread) prev)
9223           (push (if (= prev (1- (car unread))) prev
9224                   (cons prev (1- (car unread))))
9225                 read))
9226         (setq prev (1+ (car unread)))
9227         (setq unread (cdr unread)))
9228       (when (<= prev (cdr active))
9229         (push (cons prev (cdr active)) read))
9230       (setq read (if (> (length read) 1) (nreverse read) read))
9231       (if compute
9232           read
9233         (save-excursion
9234           (set-buffer gnus-group-buffer)
9235           (gnus-undo-register
9236             `(progn
9237                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9238                (gnus-info-set-read ',info ',(gnus-info-read info))
9239                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9240                (gnus-group-update-group ,group t))))
9241        ;; Propagate the read marks to the backend.
9242        (if (gnus-check-backend-function 'request-set-mark group)
9243            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9244                  (add (gnus-remove-from-range read (gnus-info-read info))))
9245              (when (or add del)
9246                (unless (gnus-check-group group)
9247                  (error "Can't open server for %s" group))
9248                (gnus-request-set-mark
9249                 group (delq nil (list (if add (list add 'add '(read)))
9250                                       (if del (list del 'del '(read)))))))))
9251         ;; Enter this list into the group info.
9252         (gnus-info-set-read info read)
9253         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9254         (gnus-get-unread-articles-in-group info (gnus-active group))
9255         t))))
9256
9257 (defun gnus-offer-save-summaries ()
9258   "Offer to save all active summary buffers."
9259   (save-excursion
9260     (let ((buflist (buffer-list))
9261           buffers bufname)
9262       ;; Go through all buffers and find all summaries.
9263       (while buflist
9264         (and (setq bufname (buffer-name (car buflist)))
9265              (string-match "Summary" bufname)
9266              (save-excursion
9267                (set-buffer bufname)
9268                ;; We check that this is, indeed, a summary buffer.
9269                (and (eq major-mode 'gnus-summary-mode)
9270                     ;; Also make sure this isn't bogus.
9271                     gnus-newsgroup-prepared
9272                     ;; Also make sure that this isn't a dead summary buffer.
9273                     (not gnus-dead-summary-mode)))
9274              (push bufname buffers))
9275         (setq buflist (cdr buflist)))
9276       ;; Go through all these summary buffers and offer to save them.
9277       (when buffers
9278         (map-y-or-n-p
9279          "Update summary buffer %s? "
9280          (lambda (buf)
9281            (switch-to-buffer buf)
9282            (gnus-summary-exit))
9283          buffers)))))
9284
9285 (defun gnus-summary-setup-default-charset ()
9286   "Setup newsgroup default charset."
9287   (if (equal gnus-newsgroup-name "nndraft:drafts")
9288       (setq gnus-newsgroup-charset nil)
9289   (let* ((name (and gnus-newsgroup-name
9290                    (gnus-group-real-name gnus-newsgroup-name)))
9291          (ignored-charsets 
9292           (or gnus-newsgroup-ephemeral-ignored-charsets
9293               (append
9294                (and gnus-newsgroup-name
9295                     (or (gnus-group-find-parameter gnus-newsgroup-name
9296                                                    'ignored-charsets t)
9297                         (let ((alist gnus-group-ignored-charsets-alist)
9298                            elem (charsets nil))
9299                           (while (setq elem (pop alist))
9300                             (when (and name
9301                                        (string-match (car elem) name))
9302                               (setq alist nil
9303                                     charsets (cdr elem))))
9304                           charsets))))
9305               gnus-newsgroup-ignored-charsets)))
9306     (setq gnus-newsgroup-charset
9307           (or gnus-newsgroup-ephemeral-charset
9308               (and gnus-newsgroup-name
9309                    (or (gnus-group-find-parameter gnus-newsgroup-name
9310                                                   'charset)
9311                        (let ((alist gnus-group-charset-alist)
9312                              elem (charset nil))
9313                          (while (setq elem (pop alist))
9314                            (when (and name
9315                                       (string-match (car elem) name))
9316                              (setq alist nil
9317                                    charset (cadr elem))))
9318                          charset)))
9319               gnus-default-charset))
9320     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9321          ignored-charsets))))
9322
9323 ;;;
9324 ;;; Mime Commands
9325 ;;;
9326
9327 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9328   "Display the current article buffer fully MIME-buttonized.
9329 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9330 treated as multipart/mixed."
9331   (interactive "P")
9332   (require 'gnus-art)
9333   (let ((gnus-unbuttonized-mime-types nil)
9334         (gnus-mime-display-multipart-as-mixed show-all-parts))
9335     (gnus-summary-show-article)))
9336
9337 (defun gnus-summary-repair-multipart (article)
9338   "Add a Content-Type header to a multipart article without one."
9339   (interactive (list (gnus-summary-article-number)))
9340   (gnus-with-article article
9341     (message-narrow-to-head)
9342     (goto-char (point-max))
9343     (widen)
9344     (when (search-forward "\n--" nil t)
9345       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9346         (message-narrow-to-head)
9347         (message-remove-header "Mime-Version")
9348         (message-remove-header "Content-Type")
9349         (goto-char (point-max))
9350         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9351                         separator))
9352         (insert "Mime-Version: 1.0\n")
9353         (widen))))
9354   (let (gnus-mark-article-hook)
9355     (gnus-summary-select-article t t nil article)))
9356
9357 (defun gnus-summary-toggle-display-buttonized ()
9358   "Toggle the buttonizing of the article buffer."
9359   (interactive)
9360   (require 'gnus-art)
9361   (if (setq gnus-inhibit-mime-unbuttonizing
9362             (not gnus-inhibit-mime-unbuttonizing))
9363       (let ((gnus-unbuttonized-mime-types nil))
9364         (gnus-summary-show-article))
9365     (gnus-summary-show-article)))
9366
9367 ;;;
9368 ;;; with article
9369 ;;;
9370
9371 (defmacro gnus-with-article (article &rest forms)
9372   "Select ARTICLE and perform FORMS in the original article buffer.
9373 Then replace the article with the result."
9374   `(progn
9375      ;; We don't want the article to be marked as read.
9376      (let (gnus-mark-article-hook)
9377        (gnus-summary-select-article t t nil ,article))
9378      (set-buffer gnus-original-article-buffer)
9379      ,@forms
9380      (if (not (gnus-check-backend-function
9381                'request-replace-article (car gnus-article-current)))
9382          (gnus-message 5 "Read-only group; not replacing")
9383        (unless (gnus-request-replace-article
9384                 ,article (car gnus-article-current)
9385                 (current-buffer) t)
9386          (error "Couldn't replace article")))
9387      ;; The cache and backlog have to be flushed somewhat.
9388      (when gnus-keep-backlog
9389        (gnus-backlog-remove-article
9390         (car gnus-article-current) (cdr gnus-article-current)))
9391      (when gnus-use-cache
9392        (gnus-cache-update-article
9393         (car gnus-article-current) (cdr gnus-article-current)))))
9394
9395 (put 'gnus-with-article 'lisp-indent-function 1)
9396 (put 'gnus-with-article 'edebug-form-spec '(form body))
9397
9398 ;;;
9399 ;;; Generic summary marking commands
9400 ;;;
9401
9402 (defvar gnus-summary-marking-alist
9403   '((read gnus-del-mark "d")
9404     (unread gnus-unread-mark "u")
9405     (ticked gnus-ticked-mark "!")
9406     (dormant gnus-dormant-mark "?")
9407     (expirable gnus-expirable-mark "e"))
9408   "An alist of names/marks/keystrokes.")
9409
9410 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9411 (defvar gnus-summary-mark-map)
9412
9413 (defun gnus-summary-make-all-marking-commands ()
9414   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9415   (dolist (elem gnus-summary-marking-alist)
9416     (apply 'gnus-summary-make-marking-command elem)))
9417
9418 (defun gnus-summary-make-marking-command (name mark keystroke)
9419   (let ((map (make-sparse-keymap)))
9420     (define-key gnus-summary-generic-mark-map keystroke map)
9421     (dolist (lway `((next "next" next nil "n")
9422                     (next-unread "next unread" next t "N")
9423                     (prev "previous" prev nil "p")
9424                     (prev-unread "previous unread" prev t "P")
9425                     (nomove "" nil nil ,keystroke)))
9426       (let ((func (gnus-summary-make-marking-command-1
9427                    mark (car lway) lway name)))
9428         (setq func (eval func))
9429         (define-key map (nth 4 lway) func)))))
9430       
9431 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9432   `(defun ,(intern
9433             (format "gnus-summary-put-mark-as-%s%s"
9434                     name (if (eq way 'nomove)
9435                              ""
9436                            (concat "-" (symbol-name way)))))
9437      (n)
9438      ,(format
9439        "Mark the current article as %s%s.
9440 If N, the prefix, then repeat N times.
9441 If N is negative, move in reverse order.
9442 The difference between N and the actual number of articles marked is
9443 returned."
9444        name (cadr lway))
9445      (interactive "p")
9446      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9447     
9448 (defun gnus-summary-generic-mark (n mark move unread)
9449   "Mark N articles with MARK."
9450   (unless (eq major-mode 'gnus-summary-mode)
9451     (error "This command can only be used in the summary buffer"))
9452   (gnus-summary-show-thread)
9453   (let ((nummove
9454          (cond
9455           ((eq move 'next) 1)
9456           ((eq move 'prev) -1)
9457           (t 0))))
9458     (if (zerop nummove)
9459         (setq n 1)
9460       (when (< n 0)
9461         (setq n (abs n)
9462               nummove (* -1 nummove))))
9463     (while (and (> n 0)
9464                 (gnus-summary-mark-article nil mark)
9465                 (zerop (gnus-summary-next-subject nummove unread t)))
9466       (setq n (1- n)))
9467     (when (/= 0 n)
9468       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9469     (gnus-summary-recenter)
9470     (gnus-summary-position-point)
9471     (gnus-set-mode-line 'summary)
9472     n))
9473
9474 (gnus-summary-make-all-marking-commands)
9475
9476 (gnus-ems-redefine)
9477
9478 (provide 'gnus-sum)
9479
9480 (run-hooks 'gnus-sum-load-hook)
9481
9482 ;;; gnus-sum.el ends here