e46b3a44470de4a35f6be0de3b7f5df7ddb8df71
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-list-identifiers nil
546   "Regexp that matches list identifiers to be removed from subject.
547 This can also be a list of regexps."
548   :group 'gnus-summary-format
549   :group 'gnus-article-hiding
550   :type '(choice (const :tag "none" nil)
551                  (regexp :value ".*")
552                  (repeat :value (".*") regexp)))
553
554 (defcustom gnus-summary-mark-below 0
555   "*Mark all articles with a score below this variable as read.
556 This variable is local to each summary buffer and usually set by the
557 score file."
558   :group 'gnus-score-default
559   :type 'integer)
560
561 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
562   "*List of functions used for sorting articles in the summary buffer.
563 This variable is only used when not using a threaded display."
564   :group 'gnus-summary-sort
565   :type '(repeat (choice (function-item gnus-article-sort-by-number)
566                          (function-item gnus-article-sort-by-author)
567                          (function-item gnus-article-sort-by-subject)
568                          (function-item gnus-article-sort-by-date)
569                          (function-item gnus-article-sort-by-score)
570                          (function :tag "other"))))
571
572 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
573   "*List of functions used for sorting threads in the summary buffer.
574 By default, threads are sorted by article number.
575
576 Each function takes two threads and return non-nil if the first thread
577 should be sorted before the other.  If you use more than one function,
578 the primary sort function should be the last.  You should probably
579 always include `gnus-thread-sort-by-number' in the list of sorting
580 functions -- preferably first.
581
582 Ready-made functions include `gnus-thread-sort-by-number',
583 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
584 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
585 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
586   :group 'gnus-summary-sort
587   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
588                          (function-item gnus-thread-sort-by-author)
589                          (function-item gnus-thread-sort-by-subject)
590                          (function-item gnus-thread-sort-by-date)
591                          (function-item gnus-thread-sort-by-score)
592                          (function-item gnus-thread-sort-by-total-score)
593                          (function :tag "other"))))
594
595 (defcustom gnus-thread-score-function '+
596   "*Function used for calculating the total score of a thread.
597
598 The function is called with the scores of the article and each
599 subthread and should then return the score of the thread.
600
601 Some functions you can use are `+', `max', or `min'."
602   :group 'gnus-summary-sort
603   :type 'function)
604
605 (defcustom gnus-summary-expunge-below nil
606   "All articles that have a score less than this variable will be expunged.
607 This variable is local to the summary buffers."
608   :group 'gnus-score-default
609   :type '(choice (const :tag "off" nil)
610                  integer))
611
612 (defcustom gnus-thread-expunge-below nil
613   "All threads that have a total score less than this variable will be expunged.
614 See `gnus-thread-score-function' for en explanation of what a
615 \"thread score\" is.
616
617 This variable is local to the summary buffers."
618   :group 'gnus-threading
619   :group 'gnus-score-default
620   :type '(choice (const :tag "off" nil)
621                  integer))
622
623 (defcustom gnus-summary-mode-hook nil
624   "*A hook for Gnus summary mode.
625 This hook is run before any variables are set in the summary buffer."
626   :group 'gnus-summary-various
627   :type 'hook)
628
629 (defcustom gnus-summary-menu-hook nil
630   "*Hook run after the creation of the summary mode menu."
631   :group 'gnus-summary-visual
632   :type 'hook)
633
634 (defcustom gnus-summary-exit-hook nil
635   "*A hook called on exit from the summary buffer.
636 It will be called with point in the group buffer."
637   :group 'gnus-summary-exit
638   :type 'hook)
639
640 (defcustom gnus-summary-prepare-hook nil
641   "*A hook called after the summary buffer has been generated.
642 If you want to modify the summary buffer, you can use this hook."
643   :group 'gnus-summary-various
644   :type 'hook)
645
646 (defcustom gnus-summary-prepared-hook nil
647   "*A hook called as the last thing after the summary buffer has been generated."
648   :group 'gnus-summary-various
649   :type 'hook)
650
651 (defcustom gnus-summary-generate-hook nil
652   "*A hook run just before generating the summary buffer.
653 This hook is commonly used to customize threading variables and the
654 like."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-select-group-hook nil
659   "*A hook called when a newsgroup is selected.
660
661 If you'd like to simplify subjects like the
662 `gnus-summary-next-same-subject' command does, you can use the
663 following hook:
664
665  (setq gnus-select-group-hook
666       (list
667         (lambda ()
668           (mapcar (lambda (header)
669                      (mail-header-set-subject
670                       header
671                       (gnus-simplify-subject
672                        (mail-header-subject header) 're-only)))
673                   gnus-newsgroup-headers))))"
674   :group 'gnus-group-select
675   :type 'hook)
676
677 (defcustom gnus-select-article-hook nil
678   "*A hook called when an article is selected."
679   :group 'gnus-summary-choose
680   :type 'hook)
681
682 (defcustom gnus-visual-mark-article-hook
683   (list 'gnus-highlight-selected-summary)
684   "*Hook run after selecting an article in the summary buffer.
685 It is meant to be used for highlighting the article in some way.  It
686 is not run if `gnus-visual' is nil."
687   :group 'gnus-summary-visual
688   :type 'hook)
689
690 (defcustom gnus-parse-headers-hook nil
691   "*A hook called before parsing the headers."
692   :group 'gnus-various
693   :type 'hook)
694
695 (defcustom gnus-exit-group-hook nil
696   "*A hook called when exiting (not quitting) summary mode."
697   :group 'gnus-various
698   :type 'hook)
699
700 (defcustom gnus-summary-update-hook
701   (list 'gnus-summary-highlight-line)
702   "*A hook called when a summary line is changed.
703 The hook will not be called if `gnus-visual' is nil.
704
705 The default function `gnus-summary-highlight-line' will
706 highlight the line according to the `gnus-summary-highlight'
707 variable."
708   :group 'gnus-summary-visual
709   :type 'hook)
710
711 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
712   "*A hook called when an article is selected for the first time.
713 The hook is intended to mark an article as read (or unread)
714 automatically when it is selected."
715   :group 'gnus-summary-choose
716   :type 'hook)
717
718 (defcustom gnus-group-no-more-groups-hook nil
719   "*A hook run when returning to group mode having no more (unread) groups."
720   :group 'gnus-group-select
721   :type 'hook)
722
723 (defcustom gnus-ps-print-hook nil
724   "*A hook run before ps-printing something from Gnus."
725   :group 'gnus-summary
726   :type 'hook)
727
728 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
729   "Face used for highlighting the current article in the summary buffer."
730   :group 'gnus-summary-visual
731   :type 'face)
732
733 (defcustom gnus-summary-highlight
734   '(((= mark gnus-canceled-mark)
735      . gnus-summary-cancelled-face)
736     ((and (> score default)
737           (or (= mark gnus-dormant-mark)
738               (= mark gnus-ticked-mark)))
739      . gnus-summary-high-ticked-face)
740     ((and (< score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-low-ticked-face)
744     ((or (= mark gnus-dormant-mark)
745          (= mark gnus-ticked-mark))
746      . gnus-summary-normal-ticked-face)
747     ((and (> score default) (= mark gnus-ancient-mark))
748      . gnus-summary-high-ancient-face)
749     ((and (< score default) (= mark gnus-ancient-mark))
750      . gnus-summary-low-ancient-face)
751     ((= mark gnus-ancient-mark)
752      . gnus-summary-normal-ancient-face)
753     ((and (> score default) (= mark gnus-unread-mark))
754      . gnus-summary-high-unread-face)
755     ((and (< score default) (= mark gnus-unread-mark))
756      . gnus-summary-low-unread-face)
757     ((= mark gnus-unread-mark)
758      . gnus-summary-normal-unread-face)
759     ((and (> score default) (memq mark (list gnus-downloadable-mark
760                                              gnus-undownloaded-mark)))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (memq mark (list gnus-downloadable-mark
763                                              gnus-undownloaded-mark)))
764      . gnus-summary-low-unread-face)
765     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
766      . gnus-summary-normal-unread-face)
767     ((> score default)
768      . gnus-summary-high-read-face)
769     ((< score default)
770      . gnus-summary-low-read-face)
771     (t
772      . gnus-summary-normal-read-face))
773   "*Controls the highlighting of summary buffer lines.
774
775 A list of (FORM . FACE) pairs.  When deciding how a a particular
776 summary line should be displayed, each form is evaluated.  The content
777 of the face field after the first true form is used.  You can change
778 how those summary lines are displayed, by editing the face field.
779
780 You can use the following variables in the FORM field.
781
782 score:   The articles score
783 default: The default article score.
784 below:   The score below which articles are automatically marked as read.
785 mark:    The articles mark."
786   :group 'gnus-summary-visual
787   :type '(repeat (cons (sexp :tag "Form" nil)
788                        face)))
789
790 (defcustom gnus-alter-header-function nil
791   "Function called to allow alteration of article header structures.
792 The function is called with one parameter, the article header vector,
793 which it may alter in any way.")
794
795 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
796   "Variable that says which function should be used to decode a string with encoded words.")
797
798 (defcustom gnus-extra-headers nil
799   "*Extra headers to parse."
800   :group 'gnus-summary
801   :type '(repeat symbol))
802
803 (defcustom gnus-ignored-from-addresses
804   (and user-mail-address (regexp-quote user-mail-address))
805   "*Regexp of From headers that may be suppressed in favor of To headers."
806   :group 'gnus-summary
807   :type 'regexp)
808
809 (defcustom gnus-group-charset-alist
810   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
811     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
812     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
813     ("^relcom\\>" koi8-r)
814     ("^fido7\\>" koi8-r)
815     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
816     ("^israel\\>" iso-8859-1)
817     ("^han\\>" euc-kr)
818     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
819     (".*" iso-8859-1))
820   "Alist of regexps (to match group names) and default charsets to be used when reading."
821   :type '(repeat (list (regexp :tag "Group")
822                        (symbol :tag "Charset")))
823   :group 'gnus-charset)
824
825 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
826   "List of charsets that should be ignored.
827 When these charsets are used in the \"charset\" parameter, the
828 default charset will be used instead."
829   :type '(repeat symbol)
830   :group 'gnus-charset)
831
832 (defcustom gnus-group-ignored-charsets-alist 
833   '(("alt\\.chinese\\.text" iso-8859-1))
834   "Alist of regexps (to match group names) and charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat (cons (regexp :tag "Group")
838                        (repeat symbol)))
839   :group 'gnus-charset)
840
841 (defcustom gnus-group-highlight-words-alist nil
842   "Alist of group regexps and highlight regexps.
843 This variable uses the same syntax as `gnus-emphasis-alist'."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat (list (regexp :tag "Highlight regexp")
846                                      (number :tag "Group for entire word" 0)
847                                      (number :tag "Group for displayed part" 0)
848                                      (symbol :tag "Face" 
849                                              gnus-emphasis-highlight-words)))))
850   :group 'gnus-summary-visual)
851
852 ;;; Internal variables
853
854 (defvar gnus-article-mime-handles nil)
855 (defvar gnus-article-decoded-p nil)
856 (defvar gnus-scores-exclude-files nil)
857 (defvar gnus-page-broken nil)
858 (defvar gnus-inhibit-mime-unbuttonizing nil)
859
860 (defvar gnus-original-article nil)
861 (defvar gnus-article-internal-prepare-hook nil)
862 (defvar gnus-newsgroup-process-stack nil)
863
864 (defvar gnus-thread-indent-array nil)
865 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
866 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
867   "Function called to sort the articles within a thread after it has 
868 been gathered together.")
869
870 ;; Avoid highlighting in kill files.
871 (defvar gnus-summary-inhibit-highlight nil)
872 (defvar gnus-newsgroup-selected-overlay nil)
873 (defvar gnus-inhibit-limiting nil)
874 (defvar gnus-newsgroup-adaptive-score-file nil)
875 (defvar gnus-current-score-file nil)
876 (defvar gnus-current-move-group nil)
877 (defvar gnus-current-copy-group nil)
878 (defvar gnus-current-crosspost-group nil)
879
880 (defvar gnus-newsgroup-dependencies nil)
881 (defvar gnus-newsgroup-adaptive nil)
882 (defvar gnus-summary-display-article-function nil)
883 (defvar gnus-summary-highlight-line-function nil
884   "Function called after highlighting a summary line.")
885
886 (defvar gnus-summary-line-format-alist
887   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
888     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
889     (?s gnus-tmp-subject-or-nil ?s)
890     (?n gnus-tmp-name ?s)
891     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
892         ?s)
893     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
894             gnus-tmp-from) ?s)
895     (?F gnus-tmp-from ?s)
896     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
897     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
898     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
899     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
900     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
901     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
902     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
903     (?L gnus-tmp-lines ?d)
904     (?I gnus-tmp-indentation ?s)
905     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
906     (?R gnus-tmp-replied ?c)
907     (?\[ gnus-tmp-opening-bracket ?c)
908     (?\] gnus-tmp-closing-bracket ?c)
909     (?\> (make-string gnus-tmp-level ? ) ?s)
910     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
911     (?i gnus-tmp-score ?d)
912     (?z gnus-tmp-score-char ?c)
913     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
914     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
915     (?U gnus-tmp-unread ?c)
916     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
917     (?t (gnus-summary-number-of-articles-in-thread
918          (and (boundp 'thread) (car thread)) gnus-tmp-level)
919         ?d)
920     (?e (gnus-summary-number-of-articles-in-thread
921          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
922         ?c)
923     (?u gnus-tmp-user-defined ?s)
924     (?P (gnus-pick-line-number) ?d))
925   "An alist of format specifications that can appear in summary lines,
926 and what variables they correspond with, along with the type of the
927 variable (string, integer, character, etc).")
928
929 (defvar gnus-summary-dummy-line-format-alist
930   `((?S gnus-tmp-subject ?s)
931     (?N gnus-tmp-number ?d)
932     (?u gnus-tmp-user-defined ?s)))
933
934 (defvar gnus-summary-mode-line-format-alist
935   `((?G gnus-tmp-group-name ?s)
936     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
937     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
938     (?A gnus-tmp-article-number ?d)
939     (?Z gnus-tmp-unread-and-unselected ?s)
940     (?V gnus-version ?s)
941     (?U gnus-tmp-unread-and-unticked ?d)
942     (?S gnus-tmp-subject ?s)
943     (?e gnus-tmp-unselected ?d)
944     (?u gnus-tmp-user-defined ?s)
945     (?d (length gnus-newsgroup-dormant) ?d)
946     (?t (length gnus-newsgroup-marked) ?d)
947     (?r (length gnus-newsgroup-reads) ?d)
948     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
949     (?E gnus-newsgroup-expunged-tally ?d)
950     (?s (gnus-current-score-file-nondirectory) ?s)))
951
952 (defvar gnus-last-search-regexp nil
953   "Default regexp for article search command.")
954
955 (defvar gnus-last-shell-command nil
956   "Default shell command on article.")
957
958 (defvar gnus-newsgroup-begin nil)
959 (defvar gnus-newsgroup-end nil)
960 (defvar gnus-newsgroup-last-rmail nil)
961 (defvar gnus-newsgroup-last-mail nil)
962 (defvar gnus-newsgroup-last-folder nil)
963 (defvar gnus-newsgroup-last-file nil)
964 (defvar gnus-newsgroup-auto-expire nil)
965 (defvar gnus-newsgroup-active nil)
966
967 (defvar gnus-newsgroup-data nil)
968 (defvar gnus-newsgroup-data-reverse nil)
969 (defvar gnus-newsgroup-limit nil)
970 (defvar gnus-newsgroup-limits nil)
971
972 (defvar gnus-newsgroup-unreads nil
973   "List of unread articles in the current newsgroup.")
974
975 (defvar gnus-newsgroup-unselected nil
976   "List of unselected unread articles in the current newsgroup.")
977
978 (defvar gnus-newsgroup-reads nil
979   "Alist of read articles and article marks in the current newsgroup.")
980
981 (defvar gnus-newsgroup-expunged-tally nil)
982
983 (defvar gnus-newsgroup-marked nil
984   "List of ticked articles in the current newsgroup (a subset of unread art).")
985
986 (defvar gnus-newsgroup-killed nil
987   "List of ranges of articles that have been through the scoring process.")
988
989 (defvar gnus-newsgroup-cached nil
990   "List of articles that come from the article cache.")
991
992 (defvar gnus-newsgroup-saved nil
993   "List of articles that have been saved.")
994
995 (defvar gnus-newsgroup-kill-headers nil)
996
997 (defvar gnus-newsgroup-replied nil
998   "List of articles that have been replied to in the current newsgroup.")
999
1000 (defvar gnus-newsgroup-expirable nil
1001   "List of articles in the current newsgroup that can be expired.")
1002
1003 (defvar gnus-newsgroup-processable nil
1004   "List of articles in the current newsgroup that can be processed.")
1005
1006 (defvar gnus-newsgroup-downloadable nil
1007   "List of articles in the current newsgroup that can be processed.")
1008
1009 (defvar gnus-newsgroup-undownloaded nil
1010   "List of articles in the current newsgroup that haven't been downloaded..")
1011
1012 (defvar gnus-newsgroup-unsendable nil
1013   "List of articles in the current newsgroup that won't be sent.")
1014
1015 (defvar gnus-newsgroup-bookmarks nil
1016   "List of articles in the current newsgroup that have bookmarks.")
1017
1018 (defvar gnus-newsgroup-dormant nil
1019   "List of dormant articles in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-scored nil
1022   "List of scored articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-headers nil
1025   "List of article headers in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-threads nil)
1028
1029 (defvar gnus-newsgroup-prepared nil
1030   "Whether the current group has been prepared properly.")
1031
1032 (defvar gnus-newsgroup-ancient nil
1033   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1034
1035 (defvar gnus-newsgroup-sparse nil)
1036
1037 (defvar gnus-current-article nil)
1038 (defvar gnus-article-current nil)
1039 (defvar gnus-current-headers nil)
1040 (defvar gnus-have-all-headers nil)
1041 (defvar gnus-last-article nil)
1042 (defvar gnus-newsgroup-history nil)
1043 (defvar gnus-newsgroup-charset nil)
1044 (defvar gnus-newsgroup-ephemeral-charset nil)
1045 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1046
1047 (defconst gnus-summary-local-variables
1048   '(gnus-newsgroup-name
1049     gnus-newsgroup-begin gnus-newsgroup-end
1050     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1051     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1052     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1053     gnus-newsgroup-unselected gnus-newsgroup-marked
1054     gnus-newsgroup-reads gnus-newsgroup-saved
1055     gnus-newsgroup-replied gnus-newsgroup-expirable
1056     gnus-newsgroup-processable gnus-newsgroup-killed
1057     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1058     gnus-newsgroup-unsendable
1059     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1060     gnus-newsgroup-headers gnus-newsgroup-threads
1061     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1062     gnus-current-article gnus-current-headers gnus-have-all-headers
1063     gnus-last-article gnus-article-internal-prepare-hook
1064     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1065     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1066     gnus-thread-expunge-below
1067     gnus-score-alist gnus-current-score-file
1068     (gnus-summary-expunge-below . global)
1069     (gnus-summary-mark-below . global)
1070     gnus-newsgroup-active gnus-scores-exclude-files
1071     gnus-newsgroup-history gnus-newsgroup-ancient
1072     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1073     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1074     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1075     (gnus-newsgroup-expunged-tally . 0)
1076     gnus-cache-removable-articles gnus-newsgroup-cached
1077     gnus-newsgroup-data gnus-newsgroup-data-reverse
1078     gnus-newsgroup-limit gnus-newsgroup-limits
1079     gnus-newsgroup-charset)
1080   "Variables that are buffer-local to the summary buffers.")
1081
1082 ;; Byte-compiler warning.
1083 (defvar gnus-article-mode-map)
1084
1085 ;; MIME stuff.
1086
1087 (defvar gnus-decode-encoded-word-methods
1088   '(mail-decode-encoded-word-string)
1089   "List of methods used to decode encoded words.
1090
1091 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1092 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1093 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1094 whose names match REGEXP.
1095
1096 For example:
1097 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1098  mail-decode-encoded-word-string
1099  (\"chinese\" . rfc1843-decode-string))
1100 ")
1101
1102 (defvar gnus-decode-encoded-word-methods-cache nil)
1103
1104 (defun gnus-multi-decode-encoded-word-string (string)
1105   "Apply the functions from `gnus-encoded-word-methods' that match."
1106   (unless (and gnus-decode-encoded-word-methods-cache
1107                (eq gnus-newsgroup-name
1108                    (car gnus-decode-encoded-word-methods-cache)))
1109     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1110     (mapc '(lambda (x)
1111              (if (symbolp x)
1112                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1113                (if (and gnus-newsgroup-name
1114                         (string-match (car x) gnus-newsgroup-name))
1115                    (nconc gnus-decode-encoded-word-methods-cache
1116                           (list (cdr x))))))
1117           gnus-decode-encoded-word-methods))
1118   (let ((xlist gnus-decode-encoded-word-methods-cache))
1119     (pop xlist)
1120     (while xlist
1121       (setq string (funcall (pop xlist) string))))
1122   string)
1123
1124 ;; Subject simplification.
1125
1126 (defun gnus-simplify-whitespace (str)
1127   "Remove excessive whitespace."
1128   (let ((mystr str))
1129     ;; Multiple spaces.
1130     (while (string-match "[ \t][ \t]+" mystr)
1131       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1132                           " "
1133                           (substring mystr (match-end 0)))))
1134     ;; Leading spaces.
1135     (when (string-match "^[ \t]+" mystr)
1136       (setq mystr (substring mystr (match-end 0))))
1137     ;; Trailing spaces.
1138     (when (string-match "[ \t]+$" mystr)
1139       (setq mystr (substring mystr 0 (match-beginning 0))))
1140     mystr))
1141
1142 (defsubst gnus-simplify-subject-re (subject)
1143   "Remove \"Re:\" from subject lines."
1144   (if (string-match "^[Rr][Ee]: *" subject)
1145       (substring subject (match-end 0))
1146     subject))
1147
1148 (defun gnus-simplify-subject (subject &optional re-only)
1149   "Remove `Re:' and words in parentheses.
1150 If RE-ONLY is non-nil, strip leading `Re:'s only."
1151   (let ((case-fold-search t))           ;Ignore case.
1152     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1153     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1154       (setq subject (substring subject (match-end 0))))
1155     ;; Remove uninteresting prefixes.
1156     (when (and (not re-only)
1157                gnus-simplify-ignored-prefixes
1158                (string-match gnus-simplify-ignored-prefixes subject))
1159       (setq subject (substring subject (match-end 0))))
1160     ;; Remove words in parentheses from end.
1161     (unless re-only
1162       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1163         (setq subject (substring subject 0 (match-beginning 0)))))
1164     ;; Return subject string.
1165     subject))
1166
1167 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1168 ;; all whitespace.
1169 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1170   (goto-char (point-min))
1171   (while (re-search-forward regexp nil t)
1172       (replace-match (or newtext ""))))
1173
1174 (defun gnus-simplify-buffer-fuzzy ()
1175   "Simplify string in the buffer fuzzily.
1176 The string in the accessible portion of the current buffer is simplified.
1177 It is assumed to be a single-line subject.
1178 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1179 matter is removed.  Additional things can be deleted by setting
1180 gnus-simplify-subject-fuzzy-regexp."
1181   (let ((case-fold-search t)
1182         (modified-tick))
1183     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1184
1185     (while (not (eq modified-tick (buffer-modified-tick)))
1186       (setq modified-tick (buffer-modified-tick))
1187       (cond
1188        ((listp gnus-simplify-subject-fuzzy-regexp)
1189         (mapcar 'gnus-simplify-buffer-fuzzy-step
1190                 gnus-simplify-subject-fuzzy-regexp))
1191        (gnus-simplify-subject-fuzzy-regexp
1192         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1193       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1194       (gnus-simplify-buffer-fuzzy-step
1195        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1196       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1197
1198     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1199     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1200     (gnus-simplify-buffer-fuzzy-step " $")
1201     (gnus-simplify-buffer-fuzzy-step "^ +")))
1202
1203 (defun gnus-simplify-subject-fuzzy (subject)
1204   "Simplify a subject string fuzzily.
1205 See `gnus-simplify-buffer-fuzzy' for details."
1206   (save-excursion
1207     (gnus-set-work-buffer)
1208     (let ((case-fold-search t))
1209       ;; Remove uninteresting prefixes.
1210       (when (and gnus-simplify-ignored-prefixes
1211                  (string-match gnus-simplify-ignored-prefixes subject))
1212         (setq subject (substring subject (match-end 0))))
1213       (insert subject)
1214       (inline (gnus-simplify-buffer-fuzzy))
1215       (buffer-string))))
1216
1217 (defsubst gnus-simplify-subject-fully (subject)
1218   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1219   (cond
1220    (gnus-simplify-subject-functions
1221     (gnus-map-function gnus-simplify-subject-functions subject))
1222    ((null gnus-summary-gather-subject-limit)
1223     (gnus-simplify-subject-re subject))
1224    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1225     (gnus-simplify-subject-fuzzy subject))
1226    ((numberp gnus-summary-gather-subject-limit)
1227     (gnus-limit-string (gnus-simplify-subject-re subject)
1228                        gnus-summary-gather-subject-limit))
1229    (t
1230     subject)))
1231
1232 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1233   "Check whether two subjects are equal.
1234 If optional argument simple-first is t, first argument is already
1235 simplified."
1236   (cond
1237    ((null simple-first)
1238     (equal (gnus-simplify-subject-fully s1)
1239            (gnus-simplify-subject-fully s2)))
1240    (t
1241     (equal s1
1242            (gnus-simplify-subject-fully s2)))))
1243
1244 (defun gnus-summary-bubble-group ()
1245   "Increase the score of the current group.
1246 This is a handy function to add to `gnus-summary-exit-hook' to
1247 increase the score of each group you read."
1248   (gnus-group-add-score gnus-newsgroup-name))
1249
1250 \f
1251 ;;;
1252 ;;; Gnus summary mode
1253 ;;;
1254
1255 (put 'gnus-summary-mode 'mode-class 'special)
1256
1257 (when t
1258   ;; Non-orthogonal keys
1259
1260   (gnus-define-keys gnus-summary-mode-map
1261     " " gnus-summary-next-page
1262     "\177" gnus-summary-prev-page
1263     [delete] gnus-summary-prev-page
1264     [backspace] gnus-summary-prev-page
1265     "\r" gnus-summary-scroll-up
1266     "\M-\r" gnus-summary-scroll-down
1267     "n" gnus-summary-next-unread-article
1268     "p" gnus-summary-prev-unread-article
1269     "N" gnus-summary-next-article
1270     "P" gnus-summary-prev-article
1271     "\M-\C-n" gnus-summary-next-same-subject
1272     "\M-\C-p" gnus-summary-prev-same-subject
1273     "\M-n" gnus-summary-next-unread-subject
1274     "\M-p" gnus-summary-prev-unread-subject
1275     "." gnus-summary-first-unread-article
1276     "," gnus-summary-best-unread-article
1277     "\M-s" gnus-summary-search-article-forward
1278     "\M-r" gnus-summary-search-article-backward
1279     "<" gnus-summary-beginning-of-article
1280     ">" gnus-summary-end-of-article
1281     "j" gnus-summary-goto-article
1282     "^" gnus-summary-refer-parent-article
1283     "\M-^" gnus-summary-refer-article
1284     "u" gnus-summary-tick-article-forward
1285     "!" gnus-summary-tick-article-forward
1286     "U" gnus-summary-tick-article-backward
1287     "d" gnus-summary-mark-as-read-forward
1288     "D" gnus-summary-mark-as-read-backward
1289     "E" gnus-summary-mark-as-expirable
1290     "\M-u" gnus-summary-clear-mark-forward
1291     "\M-U" gnus-summary-clear-mark-backward
1292     "k" gnus-summary-kill-same-subject-and-select
1293     "\C-k" gnus-summary-kill-same-subject
1294     "\M-\C-k" gnus-summary-kill-thread
1295     "\M-\C-l" gnus-summary-lower-thread
1296     "e" gnus-summary-edit-article
1297     "#" gnus-summary-mark-as-processable
1298     "\M-#" gnus-summary-unmark-as-processable
1299     "\M-\C-t" gnus-summary-toggle-threads
1300     "\M-\C-s" gnus-summary-show-thread
1301     "\M-\C-h" gnus-summary-hide-thread
1302     "\M-\C-f" gnus-summary-next-thread
1303     "\M-\C-b" gnus-summary-prev-thread
1304     "\M-\C-u" gnus-summary-up-thread
1305     "\M-\C-d" gnus-summary-down-thread
1306     "&" gnus-summary-execute-command
1307     "c" gnus-summary-catchup-and-exit
1308     "\C-w" gnus-summary-mark-region-as-read
1309     "\C-t" gnus-summary-toggle-truncation
1310     "?" gnus-summary-mark-as-dormant
1311     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1312     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1313     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1314     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1315     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1316     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1317     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1318     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1319     "=" gnus-summary-expand-window
1320     "\C-x\C-s" gnus-summary-reselect-current-group
1321     "\M-g" gnus-summary-rescan-group
1322     "w" gnus-summary-stop-page-breaking
1323     "\C-c\C-r" gnus-summary-caesar-message
1324     "f" gnus-summary-followup
1325     "F" gnus-summary-followup-with-original
1326     "C" gnus-summary-cancel-article
1327     "r" gnus-summary-reply
1328     "R" gnus-summary-reply-with-original
1329     "\C-c\C-f" gnus-summary-mail-forward
1330     "o" gnus-summary-save-article
1331     "\C-o" gnus-summary-save-article-mail
1332     "|" gnus-summary-pipe-output
1333     "\M-k" gnus-summary-edit-local-kill
1334     "\M-K" gnus-summary-edit-global-kill
1335     ;; "V" gnus-version
1336     "\C-c\C-d" gnus-summary-describe-group
1337     "q" gnus-summary-exit
1338     "Q" gnus-summary-exit-no-update
1339     "\C-c\C-i" gnus-info-find-node
1340     gnus-mouse-2 gnus-mouse-pick-article
1341     "m" gnus-summary-mail-other-window
1342     "a" gnus-summary-post-news
1343     "x" gnus-summary-limit-to-unread
1344     "s" gnus-summary-isearch-article
1345     "t" gnus-summary-toggle-header
1346     "g" gnus-summary-show-article
1347     "l" gnus-summary-goto-last-article
1348     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1349     "\C-d" gnus-summary-enter-digest-group
1350     "\M-\C-d" gnus-summary-read-document
1351     "\M-\C-e" gnus-summary-edit-parameters
1352     "\M-\C-g" gnus-summary-customize-parameters
1353     "\C-c\C-b" gnus-bug
1354     "*" gnus-cache-enter-article
1355     "\M-*" gnus-cache-remove-article
1356     "\M-&" gnus-summary-universal-argument
1357     "\C-l" gnus-recenter
1358     "I" gnus-summary-increase-score
1359     "L" gnus-summary-lower-score
1360     "\M-i" gnus-symbolic-argument
1361     "h" gnus-summary-select-article-buffer
1362
1363     "b" gnus-article-view-part
1364     "\M-t" gnus-summary-toggle-display-buttonized
1365
1366     "V" gnus-summary-score-map
1367     "X" gnus-uu-extract-map
1368     "S" gnus-summary-send-map)
1369
1370   ;; Sort of orthogonal keymap
1371   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1372     "t" gnus-summary-tick-article-forward
1373     "!" gnus-summary-tick-article-forward
1374     "d" gnus-summary-mark-as-read-forward
1375     "r" gnus-summary-mark-as-read-forward
1376     "c" gnus-summary-clear-mark-forward
1377     " " gnus-summary-clear-mark-forward
1378     "e" gnus-summary-mark-as-expirable
1379     "x" gnus-summary-mark-as-expirable
1380     "?" gnus-summary-mark-as-dormant
1381     "b" gnus-summary-set-bookmark
1382     "B" gnus-summary-remove-bookmark
1383     "#" gnus-summary-mark-as-processable
1384     "\M-#" gnus-summary-unmark-as-processable
1385     "S" gnus-summary-limit-include-expunged
1386     "C" gnus-summary-catchup
1387     "H" gnus-summary-catchup-to-here
1388     "\C-c" gnus-summary-catchup-all
1389     "k" gnus-summary-kill-same-subject-and-select
1390     "K" gnus-summary-kill-same-subject
1391     "P" gnus-uu-mark-map)
1392
1393   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1394     "c" gnus-summary-clear-above
1395     "u" gnus-summary-tick-above
1396     "m" gnus-summary-mark-above
1397     "k" gnus-summary-kill-below)
1398
1399   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1400     "/" gnus-summary-limit-to-subject
1401     "n" gnus-summary-limit-to-articles
1402     "w" gnus-summary-pop-limit
1403     "s" gnus-summary-limit-to-subject
1404     "a" gnus-summary-limit-to-author
1405     "u" gnus-summary-limit-to-unread
1406     "m" gnus-summary-limit-to-marks
1407     "M" gnus-summary-limit-exclude-marks
1408     "v" gnus-summary-limit-to-score
1409     "*" gnus-summary-limit-include-cached
1410     "D" gnus-summary-limit-include-dormant
1411     "T" gnus-summary-limit-include-thread
1412     "d" gnus-summary-limit-exclude-dormant
1413     "t" gnus-summary-limit-to-age
1414     "x" gnus-summary-limit-to-extra 
1415     "E" gnus-summary-limit-include-expunged
1416     "c" gnus-summary-limit-exclude-childless-dormant
1417     "C" gnus-summary-limit-mark-excluded-as-read)
1418
1419   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1420     "n" gnus-summary-next-unread-article
1421     "p" gnus-summary-prev-unread-article
1422     "N" gnus-summary-next-article
1423     "P" gnus-summary-prev-article
1424     "\C-n" gnus-summary-next-same-subject
1425     "\C-p" gnus-summary-prev-same-subject
1426     "\M-n" gnus-summary-next-unread-subject
1427     "\M-p" gnus-summary-prev-unread-subject
1428     "f" gnus-summary-first-unread-article
1429     "b" gnus-summary-best-unread-article
1430     "j" gnus-summary-goto-article
1431     "g" gnus-summary-goto-subject
1432     "l" gnus-summary-goto-last-article
1433     "o" gnus-summary-pop-article)
1434
1435   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1436     "k" gnus-summary-kill-thread
1437     "l" gnus-summary-lower-thread
1438     "i" gnus-summary-raise-thread
1439     "T" gnus-summary-toggle-threads
1440     "t" gnus-summary-rethread-current
1441     "^" gnus-summary-reparent-thread
1442     "s" gnus-summary-show-thread
1443     "S" gnus-summary-show-all-threads
1444     "h" gnus-summary-hide-thread
1445     "H" gnus-summary-hide-all-threads
1446     "n" gnus-summary-next-thread
1447     "p" gnus-summary-prev-thread
1448     "u" gnus-summary-up-thread
1449     "o" gnus-summary-top-thread
1450     "d" gnus-summary-down-thread
1451     "#" gnus-uu-mark-thread
1452     "\M-#" gnus-uu-unmark-thread)
1453
1454   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1455     "g" gnus-summary-prepare
1456     "c" gnus-summary-insert-cached-articles)
1457
1458   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1459     "c" gnus-summary-catchup-and-exit
1460     "C" gnus-summary-catchup-all-and-exit
1461     "E" gnus-summary-exit-no-update
1462     "Q" gnus-summary-exit
1463     "Z" gnus-summary-exit
1464     "n" gnus-summary-catchup-and-goto-next-group
1465     "R" gnus-summary-reselect-current-group
1466     "G" gnus-summary-rescan-group
1467     "N" gnus-summary-next-group
1468     "s" gnus-summary-save-newsrc
1469     "P" gnus-summary-prev-group)
1470
1471   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1472     " " gnus-summary-next-page
1473     "n" gnus-summary-next-page
1474     "\177" gnus-summary-prev-page
1475     [delete] gnus-summary-prev-page
1476     "p" gnus-summary-prev-page
1477     "\r" gnus-summary-scroll-up
1478     "\M-\r" gnus-summary-scroll-down
1479     "<" gnus-summary-beginning-of-article
1480     ">" gnus-summary-end-of-article
1481     "b" gnus-summary-beginning-of-article
1482     "e" gnus-summary-end-of-article
1483     "^" gnus-summary-refer-parent-article
1484     "r" gnus-summary-refer-parent-article
1485     "D" gnus-summary-enter-digest-group
1486     "R" gnus-summary-refer-references
1487     "T" gnus-summary-refer-thread
1488     "g" gnus-summary-show-article
1489     "s" gnus-summary-isearch-article
1490     "P" gnus-summary-print-article
1491     "t" gnus-article-babel)
1492
1493   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1494     "b" gnus-article-add-buttons
1495     "B" gnus-article-add-buttons-to-head
1496     "o" gnus-article-treat-overstrike
1497     "e" gnus-article-emphasize
1498     "w" gnus-article-fill-cited-article
1499     "Q" gnus-article-fill-long-lines
1500     "C" gnus-article-capitalize-sentences
1501     "c" gnus-article-remove-cr
1502     "q" gnus-article-de-quoted-unreadable
1503     "f" gnus-article-display-x-face
1504     "l" gnus-summary-stop-page-breaking
1505     "r" gnus-summary-caesar-message
1506     "t" gnus-article-hide-headers
1507     "v" gnus-summary-verbose-headers
1508     "H" gnus-article-strip-headers-in-body
1509     "d" gnus-article-treat-dumbquotes)
1510
1511   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1512     "a" gnus-article-hide
1513     "h" gnus-article-hide-headers
1514     "b" gnus-article-hide-boring-headers
1515     "s" gnus-article-hide-signature
1516     "c" gnus-article-hide-citation
1517     "C" gnus-article-hide-citation-in-followups
1518     "l" gnus-article-hide-list-identifiers
1519     "p" gnus-article-hide-pgp
1520     "B" gnus-article-strip-banner
1521     "P" gnus-article-hide-pem
1522     "\C-c" gnus-article-hide-citation-maybe)
1523
1524   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1525     "a" gnus-article-highlight
1526     "h" gnus-article-highlight-headers
1527     "c" gnus-article-highlight-citation
1528     "s" gnus-article-highlight-signature)
1529
1530   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1531     "w" gnus-article-decode-mime-words
1532     "c" gnus-article-decode-charset
1533     "v" gnus-mime-view-all-parts
1534     "b" gnus-article-view-part)
1535
1536   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1537     "z" gnus-article-date-ut
1538     "u" gnus-article-date-ut
1539     "l" gnus-article-date-local
1540     "e" gnus-article-date-lapsed
1541     "o" gnus-article-date-original
1542     "i" gnus-article-date-iso8601
1543     "s" gnus-article-date-user)
1544
1545   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1546     "t" gnus-article-remove-trailing-blank-lines
1547     "l" gnus-article-strip-leading-blank-lines
1548     "m" gnus-article-strip-multiple-blank-lines
1549     "a" gnus-article-strip-blank-lines
1550     "A" gnus-article-strip-all-blank-lines
1551     "s" gnus-article-strip-leading-space
1552     "e" gnus-article-strip-trailing-space)
1553
1554   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1555     "v" gnus-version
1556     "f" gnus-summary-fetch-faq
1557     "d" gnus-summary-describe-group
1558     "h" gnus-summary-describe-briefly
1559     "i" gnus-info-find-node)
1560
1561   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1562     "e" gnus-summary-expire-articles
1563     "\M-\C-e" gnus-summary-expire-articles-now
1564     "\177" gnus-summary-delete-article
1565     [delete] gnus-summary-delete-article
1566     [backspace] gnus-summary-delete-article
1567     "m" gnus-summary-move-article
1568     "r" gnus-summary-respool-article
1569     "w" gnus-summary-edit-article
1570     "c" gnus-summary-copy-article
1571     "B" gnus-summary-crosspost-article
1572     "q" gnus-summary-respool-query
1573     "t" gnus-summary-respool-trace
1574     "i" gnus-summary-import-article
1575     "p" gnus-summary-article-posted-p)
1576
1577   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1578     "o" gnus-summary-save-article
1579     "m" gnus-summary-save-article-mail
1580     "F" gnus-summary-write-article-file
1581     "r" gnus-summary-save-article-rmail
1582     "f" gnus-summary-save-article-file
1583     "b" gnus-summary-save-article-body-file
1584     "h" gnus-summary-save-article-folder
1585     "v" gnus-summary-save-article-vm
1586     "p" gnus-summary-pipe-output
1587     "s" gnus-soup-add-article)
1588
1589   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1590     "b" gnus-summary-display-buttonized
1591     "m" gnus-summary-repair-multipart
1592     "v" gnus-article-view-part
1593     "o" gnus-article-save-part
1594     "c" gnus-article-copy-part
1595     "e" gnus-article-externalize-part
1596     "i" gnus-article-inline-part
1597     "|" gnus-article-pipe-part)
1598   )
1599
1600 (defun gnus-summary-make-menu-bar ()
1601   (gnus-turn-off-edit-menu 'summary)
1602
1603   (unless (boundp 'gnus-summary-misc-menu)
1604
1605     (easy-menu-define
1606      gnus-summary-kill-menu gnus-summary-mode-map ""
1607      (cons
1608       "Score"
1609       (nconc
1610        (list
1611         ["Enter score..." gnus-summary-score-entry t]
1612         ["Customize" gnus-score-customize t])
1613        (gnus-make-score-map 'increase)
1614        (gnus-make-score-map 'lower)
1615        '(("Mark"
1616           ["Kill below" gnus-summary-kill-below t]
1617           ["Mark above" gnus-summary-mark-above t]
1618           ["Tick above" gnus-summary-tick-above t]
1619           ["Clear above" gnus-summary-clear-above t])
1620          ["Current score" gnus-summary-current-score t]
1621          ["Set score" gnus-summary-set-score t]
1622          ["Switch current score file..." gnus-score-change-score-file t]
1623          ["Set mark below..." gnus-score-set-mark-below t]
1624          ["Set expunge below..." gnus-score-set-expunge-below t]
1625          ["Edit current score file" gnus-score-edit-current-scores t]
1626          ["Edit score file" gnus-score-edit-file t]
1627          ["Trace score" gnus-score-find-trace t]
1628          ["Find words" gnus-score-find-favourite-words t]
1629          ["Rescore buffer" gnus-summary-rescore t]
1630          ["Increase score..." gnus-summary-increase-score t]
1631          ["Lower score..." gnus-summary-lower-score t]))))
1632
1633     ;; Define both the Article menu in the summary buffer and the equivalent
1634     ;; Commands menu in the article buffer here for consistency.
1635     (let ((innards
1636            '(("Hide"
1637               ["All" gnus-article-hide t]
1638               ["Headers" gnus-article-hide-headers t]
1639               ["Signature" gnus-article-hide-signature t]
1640               ["Citation" gnus-article-hide-citation t]
1641               ["List identifiers" gnus-article-hide-list-identifiers t]
1642               ["PGP" gnus-article-hide-pgp t]
1643               ["Banner" gnus-article-strip-banner t]
1644               ["Boring headers" gnus-article-hide-boring-headers t])
1645              ("Highlight"
1646               ["All" gnus-article-highlight t]
1647               ["Headers" gnus-article-highlight-headers t]
1648               ["Signature" gnus-article-highlight-signature t]
1649               ["Citation" gnus-article-highlight-citation t])
1650              ("MIME"
1651               ["Words" gnus-article-decode-mime-words t]
1652               ["Charset" gnus-article-decode-charset t]
1653               ["QP" gnus-article-de-quoted-unreadable t]
1654               ["View all" gnus-mime-view-all-parts t])
1655              ("Date"
1656               ["Local" gnus-article-date-local t]
1657               ["ISO8601" gnus-article-date-iso8601 t]
1658               ["UT" gnus-article-date-ut t]
1659               ["Original" gnus-article-date-original t]
1660               ["Lapsed" gnus-article-date-lapsed t]
1661               ["User-defined" gnus-article-date-user t])
1662              ("Washing"
1663               ("Remove Blanks"
1664                ["Leading" gnus-article-strip-leading-blank-lines t]
1665                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1666                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1667                ["All of the above" gnus-article-strip-blank-lines t]
1668                ["All" gnus-article-strip-all-blank-lines t]
1669                ["Leading space" gnus-article-strip-leading-space t]
1670                ["Trailing space" gnus-article-strip-trailing-space t])
1671               ["Overstrike" gnus-article-treat-overstrike t]
1672               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1673               ["Emphasis" gnus-article-emphasize t]
1674               ["Word wrap" gnus-article-fill-cited-article t]
1675               ["Fill long lines" gnus-article-fill-long-lines t]
1676               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1677               ["CR" gnus-article-remove-cr t]
1678               ["Show X-Face" gnus-article-display-x-face t]
1679               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1680               ["Rot 13" gnus-summary-caesar-message t]
1681               ["Unix pipe" gnus-summary-pipe-message t]
1682               ["Add buttons" gnus-article-add-buttons t]
1683               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1684               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1685               ["Verbose header" gnus-summary-verbose-headers t]
1686               ["Toggle header" gnus-summary-toggle-header t])
1687              ("Output"
1688               ["Save in default format" gnus-summary-save-article t]
1689               ["Save in file" gnus-summary-save-article-file t]
1690               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1691               ["Save in MH folder" gnus-summary-save-article-folder t]
1692               ["Save in VM folder" gnus-summary-save-article-vm t]
1693               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1694               ["Save body in file" gnus-summary-save-article-body-file t]
1695               ["Pipe through a filter" gnus-summary-pipe-output t]
1696               ["Add to SOUP packet" gnus-soup-add-article t]
1697               ["Print" gnus-summary-print-article t])
1698              ("Backend"
1699               ["Respool article..." gnus-summary-respool-article t]
1700               ["Move article..." gnus-summary-move-article
1701                (gnus-check-backend-function
1702                 'request-move-article gnus-newsgroup-name)]
1703               ["Copy article..." gnus-summary-copy-article t]
1704               ["Crosspost article..." gnus-summary-crosspost-article
1705                (gnus-check-backend-function
1706                 'request-replace-article gnus-newsgroup-name)]
1707               ["Import file..." gnus-summary-import-article t]
1708               ["Check if posted" gnus-summary-article-posted-p t]
1709               ["Edit article" gnus-summary-edit-article
1710                (not (gnus-group-read-only-p))]
1711               ["Delete article" gnus-summary-delete-article
1712                (gnus-check-backend-function
1713                 'request-expire-articles gnus-newsgroup-name)]
1714               ["Query respool" gnus-summary-respool-query t]
1715               ["Trace respool" gnus-summary-respool-trace t]
1716               ["Delete expirable articles" gnus-summary-expire-articles-now
1717                (gnus-check-backend-function
1718                 'request-expire-articles gnus-newsgroup-name)])
1719              ("Extract"
1720               ["Uudecode" gnus-uu-decode-uu t]
1721               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1722               ["Unshar" gnus-uu-decode-unshar t]
1723               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1724               ["Save" gnus-uu-decode-save t]
1725               ["Binhex" gnus-uu-decode-binhex t]
1726               ["Postscript" gnus-uu-decode-postscript t])
1727              ("Cache"
1728               ["Enter article" gnus-cache-enter-article t]
1729               ["Remove article" gnus-cache-remove-article t])
1730              ["Translate" gnus-article-babel t]
1731              ["Select article buffer" gnus-summary-select-article-buffer t]
1732              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1733              ["Isearch article..." gnus-summary-isearch-article t]
1734              ["Beginning of the article" gnus-summary-beginning-of-article t]
1735              ["End of the article" gnus-summary-end-of-article t]
1736              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1737              ["Fetch referenced articles" gnus-summary-refer-references t]
1738              ["Fetch current thread" gnus-summary-refer-thread t]
1739              ["Fetch article with id..." gnus-summary-refer-article t]
1740              ["Redisplay" gnus-summary-show-article t])))
1741       (easy-menu-define
1742        gnus-summary-article-menu gnus-summary-mode-map ""
1743        (cons "Article" innards))
1744
1745       (easy-menu-define
1746        gnus-article-commands-menu gnus-article-mode-map ""
1747        (cons "Commands" innards)))
1748
1749     (easy-menu-define
1750      gnus-summary-thread-menu gnus-summary-mode-map ""
1751      '("Threads"
1752        ["Toggle threading" gnus-summary-toggle-threads t]
1753        ["Hide threads" gnus-summary-hide-all-threads t]
1754        ["Show threads" gnus-summary-show-all-threads t]
1755        ["Hide thread" gnus-summary-hide-thread t]
1756        ["Show thread" gnus-summary-show-thread t]
1757        ["Go to next thread" gnus-summary-next-thread t]
1758        ["Go to previous thread" gnus-summary-prev-thread t]
1759        ["Go down thread" gnus-summary-down-thread t]
1760        ["Go up thread" gnus-summary-up-thread t]
1761        ["Top of thread" gnus-summary-top-thread t]
1762        ["Mark thread as read" gnus-summary-kill-thread t]
1763        ["Lower thread score" gnus-summary-lower-thread t]
1764        ["Raise thread score" gnus-summary-raise-thread t]
1765        ["Rethread current" gnus-summary-rethread-current t]
1766        ))
1767
1768     (easy-menu-define
1769      gnus-summary-post-menu gnus-summary-mode-map ""
1770      '("Post"
1771        ["Post an article" gnus-summary-post-news t]
1772        ["Followup" gnus-summary-followup t]
1773        ["Followup and yank" gnus-summary-followup-with-original t]
1774        ["Supersede article" gnus-summary-supersede-article t]
1775        ["Cancel article" gnus-summary-cancel-article t]
1776        ["Reply" gnus-summary-reply t]
1777        ["Reply and yank" gnus-summary-reply-with-original t]
1778        ["Wide reply" gnus-summary-wide-reply t]
1779        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1780        ["Mail forward" gnus-summary-mail-forward t]
1781        ["Post forward" gnus-summary-post-forward t]
1782        ["Digest and mail" gnus-uu-digest-mail-forward t]
1783        ["Digest and post" gnus-uu-digest-post-forward t]
1784        ["Resend message" gnus-summary-resend-message t]
1785        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1786        ["Send a mail" gnus-summary-mail-other-window t]
1787        ["Uuencode and post" gnus-uu-post-news t]
1788        ["Followup via news" gnus-summary-followup-to-mail t]
1789        ["Followup via news and yank"
1790         gnus-summary-followup-to-mail-with-original t]
1791        ;;("Draft"
1792        ;;["Send" gnus-summary-send-draft t]
1793        ;;["Send bounced" gnus-resend-bounced-mail t])
1794        ))
1795
1796     (easy-menu-define
1797      gnus-summary-misc-menu gnus-summary-mode-map ""
1798      '("Misc"
1799        ("Mark Read"
1800         ["Mark as read" gnus-summary-mark-as-read-forward t]
1801         ["Mark same subject and select"
1802          gnus-summary-kill-same-subject-and-select t]
1803         ["Mark same subject" gnus-summary-kill-same-subject t]
1804         ["Catchup" gnus-summary-catchup t]
1805         ["Catchup all" gnus-summary-catchup-all t]
1806         ["Catchup to here" gnus-summary-catchup-to-here t]
1807         ["Catchup region" gnus-summary-mark-region-as-read t]
1808         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1809        ("Mark Various"
1810         ["Tick" gnus-summary-tick-article-forward t]
1811         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1812         ["Remove marks" gnus-summary-clear-mark-forward t]
1813         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1814         ["Set bookmark" gnus-summary-set-bookmark t]
1815         ["Remove bookmark" gnus-summary-remove-bookmark t])
1816        ("Mark Limit"
1817         ["Marks..." gnus-summary-limit-to-marks t]
1818         ["Subject..." gnus-summary-limit-to-subject t]
1819         ["Author..." gnus-summary-limit-to-author t]
1820         ["Age..." gnus-summary-limit-to-age t]
1821         ["Extra..." gnus-summary-limit-to-extra t]
1822         ["Score" gnus-summary-limit-to-score t]
1823         ["Unread" gnus-summary-limit-to-unread t]
1824         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1825         ["Articles" gnus-summary-limit-to-articles t]
1826         ["Pop limit" gnus-summary-pop-limit t]
1827         ["Show dormant" gnus-summary-limit-include-dormant t]
1828         ["Hide childless dormant"
1829          gnus-summary-limit-exclude-childless-dormant t]
1830         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1831         ["Hide marked" gnus-summary-limit-exclude-marks t]
1832         ["Show expunged" gnus-summary-show-all-expunged t])
1833        ("Process Mark"
1834         ["Set mark" gnus-summary-mark-as-processable t]
1835         ["Remove mark" gnus-summary-unmark-as-processable t]
1836         ["Remove all marks" gnus-summary-unmark-all-processable t]
1837         ["Mark above" gnus-uu-mark-over t]
1838         ["Mark series" gnus-uu-mark-series t]
1839         ["Mark region" gnus-uu-mark-region t]
1840         ["Unmark region" gnus-uu-unmark-region t]
1841         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1842         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1843         ["Mark all" gnus-uu-mark-all t]
1844         ["Mark buffer" gnus-uu-mark-buffer t]
1845         ["Mark sparse" gnus-uu-mark-sparse t]
1846         ["Mark thread" gnus-uu-mark-thread t]
1847         ["Unmark thread" gnus-uu-unmark-thread t]
1848         ("Process Mark Sets"
1849          ["Kill" gnus-summary-kill-process-mark t]
1850          ["Yank" gnus-summary-yank-process-mark
1851           gnus-newsgroup-process-stack]
1852          ["Save" gnus-summary-save-process-mark t]))
1853        ("Scroll article"
1854         ["Page forward" gnus-summary-next-page t]
1855         ["Page backward" gnus-summary-prev-page t]
1856         ["Line forward" gnus-summary-scroll-up t])
1857        ("Move"
1858         ["Next unread article" gnus-summary-next-unread-article t]
1859         ["Previous unread article" gnus-summary-prev-unread-article t]
1860         ["Next article" gnus-summary-next-article t]
1861         ["Previous article" gnus-summary-prev-article t]
1862         ["Next unread subject" gnus-summary-next-unread-subject t]
1863         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1864         ["Next article same subject" gnus-summary-next-same-subject t]
1865         ["Previous article same subject" gnus-summary-prev-same-subject t]
1866         ["First unread article" gnus-summary-first-unread-article t]
1867         ["Best unread article" gnus-summary-best-unread-article t]
1868         ["Go to subject number..." gnus-summary-goto-subject t]
1869         ["Go to article number..." gnus-summary-goto-article t]
1870         ["Go to the last article" gnus-summary-goto-last-article t]
1871         ["Pop article off history" gnus-summary-pop-article t])
1872        ("Sort"
1873         ["Sort by number" gnus-summary-sort-by-number t]
1874         ["Sort by author" gnus-summary-sort-by-author t]
1875         ["Sort by subject" gnus-summary-sort-by-subject t]
1876         ["Sort by date" gnus-summary-sort-by-date t]
1877         ["Sort by score" gnus-summary-sort-by-score t]
1878         ["Sort by lines" gnus-summary-sort-by-lines t]
1879         ["Sort by characters" gnus-summary-sort-by-chars t])
1880        ("Help"
1881         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1882         ["Describe group" gnus-summary-describe-group t]
1883         ["Read manual" gnus-info-find-node t])
1884        ("Modes"
1885         ["Pick and read" gnus-pick-mode t]
1886         ["Binary" gnus-binary-mode t])
1887        ("Regeneration"
1888         ["Regenerate" gnus-summary-prepare t]
1889         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1890         ["Toggle threading" gnus-summary-toggle-threads t])
1891        ["Filter articles..." gnus-summary-execute-command t]
1892        ["Run command on subjects..." gnus-summary-universal-argument t]
1893        ["Search articles forward..." gnus-summary-search-article-forward t]
1894        ["Search articles backward..." gnus-summary-search-article-backward t]
1895        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1896        ["Expand window" gnus-summary-expand-window t]
1897        ["Expire expirable articles" gnus-summary-expire-articles
1898         (gnus-check-backend-function
1899          'request-expire-articles gnus-newsgroup-name)]
1900        ["Edit local kill file" gnus-summary-edit-local-kill t]
1901        ["Edit main kill file" gnus-summary-edit-global-kill t]
1902        ["Edit group parameters" gnus-summary-edit-parameters t]
1903        ["Customize group parameters" gnus-summary-customize-parameters t]
1904        ["Send a bug report" gnus-bug t]
1905        ("Exit"
1906         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1907         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1908         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1909         ["Exit group" gnus-summary-exit t]
1910         ["Exit group without updating" gnus-summary-exit-no-update t]
1911         ["Exit and goto next group" gnus-summary-next-group t]
1912         ["Exit and goto prev group" gnus-summary-prev-group t]
1913         ["Reselect group" gnus-summary-reselect-current-group t]
1914         ["Rescan group" gnus-summary-rescan-group t]
1915         ["Update dribble" gnus-summary-save-newsrc t])))
1916
1917     (gnus-run-hooks 'gnus-summary-menu-hook)))
1918
1919 (defun gnus-score-set-default (var value)
1920   "A version of set that updates the GNU Emacs menu-bar."
1921   (set var value)
1922   ;; It is the message that forces the active status to be updated.
1923   (message ""))
1924
1925 (defun gnus-make-score-map (type)
1926   "Make a summary score map of type TYPE."
1927   (if t
1928       nil
1929     (let ((headers '(("author" "from" string)
1930                      ("subject" "subject" string)
1931                      ("article body" "body" string)
1932                      ("article head" "head" string)
1933                      ("xref" "xref" string)
1934                      ("extra header" "extra" string)
1935                      ("lines" "lines" number)
1936                      ("followups to author" "followup" string)))
1937           (types '((number ("less than" <)
1938                            ("greater than" >)
1939                            ("equal" =))
1940                    (string ("substring" s)
1941                            ("exact string" e)
1942                            ("fuzzy string" f)
1943                            ("regexp" r))))
1944           (perms '(("temporary" (current-time-string))
1945                    ("permanent" nil)
1946                    ("immediate" now)))
1947           header)
1948       (list
1949        (apply
1950         'nconc
1951         (list
1952          (if (eq type 'lower)
1953              "Lower score"
1954            "Increase score"))
1955         (let (outh)
1956           (while headers
1957             (setq header (car headers))
1958             (setq outh
1959                   (cons
1960                    (apply
1961                     'nconc
1962                     (list (car header))
1963                     (let ((ts (cdr (assoc (nth 2 header) types)))
1964                           outt)
1965                       (while ts
1966                         (setq outt
1967                               (cons
1968                                (apply
1969                                 'nconc
1970                                 (list (caar ts))
1971                                 (let ((ps perms)
1972                                       outp)
1973                                   (while ps
1974                                     (setq outp
1975                                           (cons
1976                                            (vector
1977                                             (caar ps)
1978                                             (list
1979                                              'gnus-summary-score-entry
1980                                              (nth 1 header)
1981                                              (if (or (string= (nth 1 header)
1982                                                               "head")
1983                                                      (string= (nth 1 header)
1984                                                               "body"))
1985                                                  ""
1986                                                (list 'gnus-summary-header
1987                                                      (nth 1 header)))
1988                                              (list 'quote (nth 1 (car ts)))
1989                                              (list 'gnus-score-default nil)
1990                                              (nth 1 (car ps))
1991                                              t)
1992                                             t)
1993                                            outp))
1994                                     (setq ps (cdr ps)))
1995                                   (list (nreverse outp))))
1996                                outt))
1997                         (setq ts (cdr ts)))
1998                       (list (nreverse outt))))
1999                    outh))
2000             (setq headers (cdr headers)))
2001           (list (nreverse outh))))))))
2002
2003 \f
2004
2005 (defun gnus-summary-mode (&optional group)
2006   "Major mode for reading articles.
2007
2008 All normal editing commands are switched off.
2009 \\<gnus-summary-mode-map>
2010 Each line in this buffer represents one article.  To read an
2011 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2012 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2013 respectively.
2014
2015 You can also post articles and send mail from this buffer.  To
2016 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2017 of an article, type `\\[gnus-summary-reply]'.
2018
2019 There are approx. one gazillion commands you can execute in this
2020 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2021
2022 The following commands are available:
2023
2024 \\{gnus-summary-mode-map}"
2025   (interactive)
2026   (when (gnus-visual-p 'summary-menu 'menu)
2027     (gnus-summary-make-menu-bar))
2028   (kill-all-local-variables)
2029   (gnus-summary-make-local-variables)
2030   (gnus-make-thread-indent-array)
2031   (gnus-simplify-mode-line)
2032   (setq major-mode 'gnus-summary-mode)
2033   (setq mode-name "Summary")
2034   (make-local-variable 'minor-mode-alist)
2035   (use-local-map gnus-summary-mode-map)
2036   (buffer-disable-undo)
2037   (setq buffer-read-only t)             ;Disable modification
2038   (setq truncate-lines t)
2039   (setq selective-display t)
2040   (setq selective-display-ellipses t)   ;Display `...'
2041   (gnus-summary-set-display-table)
2042   (gnus-set-default-directory)
2043   (setq gnus-newsgroup-name group)
2044   (make-local-variable 'gnus-summary-line-format)
2045   (make-local-variable 'gnus-summary-line-format-spec)
2046   (make-local-variable 'gnus-summary-dummy-line-format)
2047   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2048   (make-local-variable 'gnus-summary-mark-positions)
2049   (make-local-hook 'pre-command-hook)
2050   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2051   (gnus-run-hooks 'gnus-summary-mode-hook)
2052   (mm-enable-multibyte)
2053   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2054   (gnus-update-summary-mark-positions))
2055
2056 (defun gnus-summary-make-local-variables ()
2057   "Make all the local summary buffer variables."
2058   (let (global)
2059     (dolist (local gnus-summary-local-variables)
2060       (if (consp local)
2061           (progn
2062             (if (eq (cdr local) 'global)
2063                 ;; Copy the global value of the variable.
2064                 (setq global (symbol-value (car local)))
2065               ;; Use the value from the list.
2066               (setq global (eval (cdr local))))
2067             (set (make-local-variable (car local)) global))
2068         ;; Simple nil-valued local variable.
2069         (set (make-local-variable local) nil)))))
2070
2071 (defun gnus-summary-clear-local-variables ()
2072   (let ((locals gnus-summary-local-variables))
2073     (while locals
2074       (if (consp (car locals))
2075           (and (vectorp (caar locals))
2076                (set (caar locals) nil))
2077         (and (vectorp (car locals))
2078              (set (car locals) nil)))
2079       (setq locals (cdr locals)))))
2080
2081 ;; Summary data functions.
2082
2083 (defmacro gnus-data-number (data)
2084   `(car ,data))
2085
2086 (defmacro gnus-data-set-number (data number)
2087   `(setcar ,data ,number))
2088
2089 (defmacro gnus-data-mark (data)
2090   `(nth 1 ,data))
2091
2092 (defmacro gnus-data-set-mark (data mark)
2093   `(setcar (nthcdr 1 ,data) ,mark))
2094
2095 (defmacro gnus-data-pos (data)
2096   `(nth 2 ,data))
2097
2098 (defmacro gnus-data-set-pos (data pos)
2099   `(setcar (nthcdr 2 ,data) ,pos))
2100
2101 (defmacro gnus-data-header (data)
2102   `(nth 3 ,data))
2103
2104 (defmacro gnus-data-set-header (data header)
2105   `(setf (nth 3 ,data) ,header))
2106
2107 (defmacro gnus-data-level (data)
2108   `(nth 4 ,data))
2109
2110 (defmacro gnus-data-unread-p (data)
2111   `(= (nth 1 ,data) gnus-unread-mark))
2112
2113 (defmacro gnus-data-read-p (data)
2114   `(/= (nth 1 ,data) gnus-unread-mark))
2115
2116 (defmacro gnus-data-pseudo-p (data)
2117   `(consp (nth 3 ,data)))
2118
2119 (defmacro gnus-data-find (number)
2120   `(assq ,number gnus-newsgroup-data))
2121
2122 (defmacro gnus-data-find-list (number &optional data)
2123   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2124      (memq (assq ,number bdata)
2125            bdata)))
2126
2127 (defmacro gnus-data-make (number mark pos header level)
2128   `(list ,number ,mark ,pos ,header ,level))
2129
2130 (defun gnus-data-enter (after-article number mark pos header level offset)
2131   (let ((data (gnus-data-find-list after-article)))
2132     (unless data
2133       (error "No such article: %d" after-article))
2134     (setcdr data (cons (gnus-data-make number mark pos header level)
2135                        (cdr data)))
2136     (setq gnus-newsgroup-data-reverse nil)
2137     (gnus-data-update-list (cddr data) offset)))
2138
2139 (defun gnus-data-enter-list (after-article list &optional offset)
2140   (when list
2141     (let ((data (and after-article (gnus-data-find-list after-article)))
2142           (ilist list))
2143       (if (not (or data
2144                    after-article))
2145           (let ((odata gnus-newsgroup-data))
2146             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2147             (when offset
2148               (gnus-data-update-list odata offset)))
2149         ;; Find the last element in the list to be spliced into the main
2150         ;; list.
2151         (while (cdr list)
2152           (setq list (cdr list)))
2153         (if (not data)
2154             (progn
2155               (setcdr list gnus-newsgroup-data)
2156               (setq gnus-newsgroup-data ilist)
2157               (when offset
2158                 (gnus-data-update-list (cdr list) offset)))
2159           (setcdr list (cdr data))
2160           (setcdr data ilist)
2161           (when offset
2162             (gnus-data-update-list (cdr list) offset))))
2163       (setq gnus-newsgroup-data-reverse nil))))
2164
2165 (defun gnus-data-remove (article &optional offset)
2166   (let ((data gnus-newsgroup-data))
2167     (if (= (gnus-data-number (car data)) article)
2168         (progn
2169           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2170                 gnus-newsgroup-data-reverse nil)
2171           (when offset
2172             (gnus-data-update-list gnus-newsgroup-data offset)))
2173       (while (cdr data)
2174         (when (= (gnus-data-number (cadr data)) article)
2175           (setcdr data (cddr data))
2176           (when offset
2177             (gnus-data-update-list (cdr data) offset))
2178           (setq data nil
2179                 gnus-newsgroup-data-reverse nil))
2180         (setq data (cdr data))))))
2181
2182 (defmacro gnus-data-list (backward)
2183   `(if ,backward
2184        (or gnus-newsgroup-data-reverse
2185            (setq gnus-newsgroup-data-reverse
2186                  (reverse gnus-newsgroup-data)))
2187      gnus-newsgroup-data))
2188
2189 (defun gnus-data-update-list (data offset)
2190   "Add OFFSET to the POS of all data entries in DATA."
2191   (setq gnus-newsgroup-data-reverse nil)
2192   (while data
2193     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2194     (setq data (cdr data))))
2195
2196 (defun gnus-summary-article-pseudo-p (article)
2197   "Say whether this article is a pseudo article or not."
2198   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2199
2200 (defmacro gnus-summary-article-sparse-p (article)
2201   "Say whether this article is a sparse article or not."
2202   `(memq ,article gnus-newsgroup-sparse))
2203
2204 (defmacro gnus-summary-article-ancient-p (article)
2205   "Say whether this article is a sparse article or not."
2206   `(memq ,article gnus-newsgroup-ancient))
2207
2208 (defun gnus-article-parent-p (number)
2209   "Say whether this article is a parent or not."
2210   (let ((data (gnus-data-find-list number)))
2211     (and (cdr data)                     ; There has to be an article after...
2212          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2213             (gnus-data-level (nth 1 data))))))
2214
2215 (defun gnus-article-children (number)
2216   "Return a list of all children to NUMBER."
2217   (let* ((data (gnus-data-find-list number))
2218          (level (gnus-data-level (car data)))
2219          children)
2220     (setq data (cdr data))
2221     (while (and data
2222                 (= (gnus-data-level (car data)) (1+ level)))
2223       (push (gnus-data-number (car data)) children)
2224       (setq data (cdr data)))
2225     children))
2226
2227 (defmacro gnus-summary-skip-intangible ()
2228   "If the current article is intangible, then jump to a different article."
2229   '(let ((to (get-text-property (point) 'gnus-intangible)))
2230      (and to (gnus-summary-goto-subject to))))
2231
2232 (defmacro gnus-summary-article-intangible-p ()
2233   "Say whether this article is intangible or not."
2234   '(get-text-property (point) 'gnus-intangible))
2235
2236 (defun gnus-article-read-p (article)
2237   "Say whether ARTICLE is read or not."
2238   (not (or (memq article gnus-newsgroup-marked)
2239            (memq article gnus-newsgroup-unreads)
2240            (memq article gnus-newsgroup-unselected)
2241            (memq article gnus-newsgroup-dormant))))
2242
2243 ;; Some summary mode macros.
2244
2245 (defmacro gnus-summary-article-number ()
2246   "The article number of the article on the current line.
2247 If there isn's an article number here, then we return the current
2248 article number."
2249   '(progn
2250      (gnus-summary-skip-intangible)
2251      (or (get-text-property (point) 'gnus-number)
2252          (gnus-summary-last-subject))))
2253
2254 (defmacro gnus-summary-article-header (&optional number)
2255   "Return the header of article NUMBER."
2256   `(gnus-data-header (gnus-data-find
2257                       ,(or number '(gnus-summary-article-number)))))
2258
2259 (defmacro gnus-summary-thread-level (&optional number)
2260   "Return the level of thread that starts with article NUMBER."
2261   `(if (and (eq gnus-summary-make-false-root 'dummy)
2262             (get-text-property (point) 'gnus-intangible))
2263        0
2264      (gnus-data-level (gnus-data-find
2265                        ,(or number '(gnus-summary-article-number))))))
2266
2267 (defmacro gnus-summary-article-mark (&optional number)
2268   "Return the mark of article NUMBER."
2269   `(gnus-data-mark (gnus-data-find
2270                     ,(or number '(gnus-summary-article-number)))))
2271
2272 (defmacro gnus-summary-article-pos (&optional number)
2273   "Return the position of the line of article NUMBER."
2274   `(gnus-data-pos (gnus-data-find
2275                    ,(or number '(gnus-summary-article-number)))))
2276
2277 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2278 (defmacro gnus-summary-article-subject (&optional number)
2279   "Return current subject string or nil if nothing."
2280   `(let ((headers
2281           ,(if number
2282                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2283              '(gnus-data-header (assq (gnus-summary-article-number)
2284                                       gnus-newsgroup-data)))))
2285      (and headers
2286           (vectorp headers)
2287           (mail-header-subject headers))))
2288
2289 (defmacro gnus-summary-article-score (&optional number)
2290   "Return current article score."
2291   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2292                   gnus-newsgroup-scored))
2293        gnus-summary-default-score 0))
2294
2295 (defun gnus-summary-article-children (&optional number)
2296   "Return a list of article numbers that are children of article NUMBER."
2297   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2298          (level (gnus-data-level (car data)))
2299          l children)
2300     (while (and (setq data (cdr data))
2301                 (> (setq l (gnus-data-level (car data))) level))
2302       (and (= (1+ level) l)
2303            (push (gnus-data-number (car data))
2304                  children)))
2305     (nreverse children)))
2306
2307 (defun gnus-summary-article-parent (&optional number)
2308   "Return the article number of the parent of article NUMBER."
2309   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2310                                     (gnus-data-list t)))
2311          (level (gnus-data-level (car data))))
2312     (if (zerop level)
2313         ()                              ; This is a root.
2314       ;; We search until we find an article with a level less than
2315       ;; this one.  That function has to be the parent.
2316       (while (and (setq data (cdr data))
2317                   (not (< (gnus-data-level (car data)) level))))
2318       (and data (gnus-data-number (car data))))))
2319
2320 (defun gnus-unread-mark-p (mark)
2321   "Say whether MARK is the unread mark."
2322   (= mark gnus-unread-mark))
2323
2324 (defun gnus-read-mark-p (mark)
2325   "Say whether MARK is one of the marks that mark as read.
2326 This is all marks except unread, ticked, dormant, and expirable."
2327   (not (or (= mark gnus-unread-mark)
2328            (= mark gnus-ticked-mark)
2329            (= mark gnus-dormant-mark)
2330            (= mark gnus-expirable-mark))))
2331
2332 (defmacro gnus-article-mark (number)
2333   "Return the MARK of article NUMBER.
2334 This macro should only be used when computing the mark the \"first\"
2335 time; i.e., when generating the summary lines.  After that,
2336 `gnus-summary-article-mark' should be used to examine the
2337 marks of articles."
2338   `(cond
2339     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2340     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2341     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2342     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2343     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2344     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2345     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2346     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2347            gnus-ancient-mark))))
2348
2349 ;; Saving hidden threads.
2350
2351 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2352 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2353
2354 (defmacro gnus-save-hidden-threads (&rest forms)
2355   "Save hidden threads, eval FORMS, and restore the hidden threads."
2356   (let ((config (make-symbol "config")))
2357     `(let ((,config (gnus-hidden-threads-configuration)))
2358        (unwind-protect
2359            (save-excursion
2360              ,@forms)
2361          (gnus-restore-hidden-threads-configuration ,config)))))
2362
2363 (defun gnus-data-compute-positions ()
2364   "Compute the positions of all articles."
2365   (setq gnus-newsgroup-data-reverse nil)
2366   (let ((data gnus-newsgroup-data))
2367     (save-excursion
2368       (gnus-save-hidden-threads
2369         (gnus-summary-show-all-threads)
2370         (goto-char (point-min))
2371         (while data
2372           (while (get-text-property (point) 'gnus-intangible)
2373             (forward-line 1))
2374           (gnus-data-set-pos (car data) (+ (point) 3))
2375           (setq data (cdr data))
2376           (forward-line 1))))))
2377
2378 (defun gnus-hidden-threads-configuration ()
2379   "Return the current hidden threads configuration."
2380   (save-excursion
2381     (let (config)
2382       (goto-char (point-min))
2383       (while (search-forward "\r" nil t)
2384         (push (1- (point)) config))
2385       config)))
2386
2387 (defun gnus-restore-hidden-threads-configuration (config)
2388   "Restore hidden threads configuration from CONFIG."
2389   (let (point buffer-read-only)
2390     (while (setq point (pop config))
2391       (when (and (< point (point-max))
2392                  (goto-char point)
2393                  (eq (char-after) ?\n))
2394         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2395
2396 ;; Various summary mode internalish functions.
2397
2398 (defun gnus-mouse-pick-article (e)
2399   (interactive "e")
2400   (mouse-set-point e)
2401   (gnus-summary-next-page nil t))
2402
2403 (defun gnus-summary-set-display-table ()
2404   ;; Change the display table.  Odd characters have a tendency to mess
2405   ;; up nicely formatted displays - we make all possible glyphs
2406   ;; display only a single character.
2407
2408   ;; We start from the standard display table, if any.
2409   (let ((table (or (copy-sequence standard-display-table)
2410                    (make-display-table)))
2411         (i 32))
2412     ;; Nix out all the control chars...
2413     (while (>= (setq i (1- i)) 0)
2414       (aset table i [??]))
2415     ;; ... but not newline and cr, of course.  (cr is necessary for the
2416     ;; selective display).
2417     (aset table ?\n nil)
2418     (aset table ?\r nil)
2419     ;; We keep TAB as well.
2420     (aset table ?\t nil)
2421     ;; We nix out any glyphs over 126 that are not set already.
2422     (let ((i 256))
2423       (while (>= (setq i (1- i)) 127)
2424         ;; Only modify if the entry is nil.
2425         (unless (aref table i)
2426           (aset table i [??]))))
2427     (setq buffer-display-table table)))
2428
2429 (defun gnus-summary-setup-buffer (group)
2430   "Initialize summary buffer."
2431   (let ((buffer (concat "*Summary " group "*")))
2432     (if (get-buffer buffer)
2433         (progn
2434           (set-buffer buffer)
2435           (setq gnus-summary-buffer (current-buffer))
2436           (not gnus-newsgroup-prepared))
2437       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2438       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2439       (gnus-summary-mode group)
2440       (when gnus-carpal
2441         (gnus-carpal-setup-buffer 'summary))
2442       (unless gnus-single-article-buffer
2443         (make-local-variable 'gnus-article-buffer)
2444         (make-local-variable 'gnus-article-current)
2445         (make-local-variable 'gnus-original-article-buffer))
2446       (setq gnus-newsgroup-name group)
2447       t)))
2448
2449 (defun gnus-set-global-variables ()
2450   ;; Set the global equivalents of the summary buffer-local variables
2451   ;; to the latest values they had.  These reflect the summary buffer
2452   ;; that was in action when the last article was fetched.
2453   (when (eq major-mode 'gnus-summary-mode)
2454     (setq gnus-summary-buffer (current-buffer))
2455     (let ((name gnus-newsgroup-name)
2456           (marked gnus-newsgroup-marked)
2457           (unread gnus-newsgroup-unreads)
2458           (headers gnus-current-headers)
2459           (data gnus-newsgroup-data)
2460           (summary gnus-summary-buffer)
2461           (article-buffer gnus-article-buffer)
2462           (original gnus-original-article-buffer)
2463           (gac gnus-article-current)
2464           (reffed gnus-reffed-article-number)
2465           (score-file gnus-current-score-file)
2466           (default-charset gnus-newsgroup-charset))
2467       (save-excursion
2468         (set-buffer gnus-group-buffer)
2469         (setq gnus-newsgroup-name name
2470               gnus-newsgroup-marked marked
2471               gnus-newsgroup-unreads unread
2472               gnus-current-headers headers
2473               gnus-newsgroup-data data
2474               gnus-article-current gac
2475               gnus-summary-buffer summary
2476               gnus-article-buffer article-buffer
2477               gnus-original-article-buffer original
2478               gnus-reffed-article-number reffed
2479               gnus-current-score-file score-file
2480               gnus-newsgroup-charset default-charset)
2481         ;; The article buffer also has local variables.
2482         (when (gnus-buffer-live-p gnus-article-buffer)
2483           (set-buffer gnus-article-buffer)
2484           (setq gnus-summary-buffer summary))))))
2485
2486 (defun gnus-summary-article-unread-p (article)
2487   "Say whether ARTICLE is unread or not."
2488   (memq article gnus-newsgroup-unreads))
2489
2490 (defun gnus-summary-first-article-p (&optional article)
2491   "Return whether ARTICLE is the first article in the buffer."
2492   (if (not (setq article (or article (gnus-summary-article-number))))
2493       nil
2494     (eq article (caar gnus-newsgroup-data))))
2495
2496 (defun gnus-summary-last-article-p (&optional article)
2497   "Return whether ARTICLE is the last article in the buffer."
2498   (if (not (setq article (or article (gnus-summary-article-number))))
2499       ;; All non-existent numbers are the last article.  :-)
2500       t
2501     (not (cdr (gnus-data-find-list article)))))
2502
2503 (defun gnus-make-thread-indent-array ()
2504   (let ((n 200))
2505     (unless (and gnus-thread-indent-array
2506                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2507       (setq gnus-thread-indent-array (make-vector 201 "")
2508             gnus-thread-indent-array-level gnus-thread-indent-level)
2509       (while (>= n 0)
2510         (aset gnus-thread-indent-array n
2511               (make-string (* n gnus-thread-indent-level) ? ))
2512         (setq n (1- n))))))
2513
2514 (defun gnus-update-summary-mark-positions ()
2515   "Compute where the summary marks are to go."
2516   (save-excursion
2517     (when (gnus-buffer-exists-p gnus-summary-buffer)
2518       (set-buffer gnus-summary-buffer))
2519     (let ((gnus-replied-mark 129)
2520           (gnus-score-below-mark 130)
2521           (gnus-score-over-mark 130)
2522           (gnus-download-mark 131)
2523           (spec gnus-summary-line-format-spec)
2524           gnus-visual pos)
2525       (save-excursion
2526         (gnus-set-work-buffer)
2527         (let ((gnus-summary-line-format-spec spec)
2528               (gnus-newsgroup-downloadable '((0 . t))))
2529           (gnus-summary-insert-line
2530            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2531           (goto-char (point-min))
2532           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2533                                              (- (point) 2)))))
2534           (goto-char (point-min))
2535           (push (cons 'replied (and (search-forward "\201" nil t)
2536                                     (- (point) 2)))
2537                 pos)
2538           (goto-char (point-min))
2539           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2540                 pos)
2541           (goto-char (point-min))
2542           (push (cons 'download
2543                       (and (search-forward "\203" nil t) (- (point) 2)))
2544                 pos)))
2545       (setq gnus-summary-mark-positions pos))))
2546
2547 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2548   "Insert a dummy root in the summary buffer."
2549   (beginning-of-line)
2550   (gnus-add-text-properties
2551    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2552    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2553
2554 (defun gnus-summary-from-or-to-or-newsgroups (header)
2555   (let ((to (cdr (assq 'To (mail-header-extra header))))
2556         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2557         (mail-parse-charset gnus-newsgroup-charset)
2558         (mail-parse-ignored-charsets 
2559          (save-excursion (set-buffer gnus-summary-buffer)
2560                          gnus-newsgroup-ignored-charsets)))
2561     (cond
2562      ((and to
2563            gnus-ignored-from-addresses
2564            (string-match gnus-ignored-from-addresses
2565                          (mail-header-from header)))
2566       (concat "-> "
2567               (or (car (funcall gnus-extract-address-components
2568                                 (funcall
2569                                  gnus-decode-encoded-word-function to)))
2570                   (funcall gnus-decode-encoded-word-function to))))
2571      ((and newsgroups
2572            gnus-ignored-from-addresses
2573            (string-match gnus-ignored-from-addresses
2574                          (mail-header-from header)))
2575       (concat "=> " newsgroups))
2576      (t
2577       (or (car (funcall gnus-extract-address-components
2578                         (mail-header-from header)))
2579           (mail-header-from header))))))
2580
2581 (defun gnus-summary-insert-line (gnus-tmp-header
2582                                  gnus-tmp-level gnus-tmp-current
2583                                  gnus-tmp-unread gnus-tmp-replied
2584                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2585                                  &optional gnus-tmp-dummy gnus-tmp-score
2586                                  gnus-tmp-process)
2587   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2588          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2589          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2590          (gnus-tmp-score-char
2591           (if (or (null gnus-summary-default-score)
2592                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2593                       gnus-summary-zcore-fuzz))
2594               ? ;Whitespace
2595             (if (< gnus-tmp-score gnus-summary-default-score)
2596                 gnus-score-below-mark gnus-score-over-mark)))
2597          (gnus-tmp-replied
2598           (cond (gnus-tmp-process gnus-process-mark)
2599                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2600                  gnus-cached-mark)
2601                 (gnus-tmp-replied gnus-replied-mark)
2602                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2603                  gnus-saved-mark)
2604                 (t gnus-unread-mark)))
2605          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2606          (gnus-tmp-name
2607           (cond
2608            ((string-match "<[^>]+> *$" gnus-tmp-from)
2609             (let ((beg (match-beginning 0)))
2610               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2611                        (substring gnus-tmp-from (1+ (match-beginning 0))
2612                                   (1- (match-end 0))))
2613                   (substring gnus-tmp-from 0 beg))))
2614            ((string-match "(.+)" gnus-tmp-from)
2615             (substring gnus-tmp-from
2616                        (1+ (match-beginning 0)) (1- (match-end 0))))
2617            (t gnus-tmp-from)))
2618          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2619          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2620          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2621          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2622          (buffer-read-only nil))
2623     (when (string= gnus-tmp-name "")
2624       (setq gnus-tmp-name gnus-tmp-from))
2625     (unless (numberp gnus-tmp-lines)
2626       (setq gnus-tmp-lines 0))
2627     (gnus-put-text-property
2628      (point)
2629      (progn (eval gnus-summary-line-format-spec) (point))
2630      'gnus-number gnus-tmp-number)
2631     (when (gnus-visual-p 'summary-highlight 'highlight)
2632       (forward-line -1)
2633       (gnus-run-hooks 'gnus-summary-update-hook)
2634       (forward-line 1))))
2635
2636 (defun gnus-summary-update-line (&optional dont-update)
2637   ;; Update summary line after change.
2638   (when (and gnus-summary-default-score
2639              (not gnus-summary-inhibit-highlight))
2640     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2641            (article (gnus-summary-article-number))
2642            (score (gnus-summary-article-score article)))
2643       (unless dont-update
2644         (if (and gnus-summary-mark-below
2645                  (< (gnus-summary-article-score)
2646                     gnus-summary-mark-below))
2647             ;; This article has a low score, so we mark it as read.
2648             (when (memq article gnus-newsgroup-unreads)
2649               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2650           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2651             ;; This article was previously marked as read on account
2652             ;; of a low score, but now it has risen, so we mark it as
2653             ;; unread.
2654             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2655         (gnus-summary-update-mark
2656          (if (or (null gnus-summary-default-score)
2657                  (<= (abs (- score gnus-summary-default-score))
2658                      gnus-summary-zcore-fuzz))
2659              ? ;Whitespace
2660            (if (< score gnus-summary-default-score)
2661                gnus-score-below-mark gnus-score-over-mark))
2662          'score))
2663       ;; Do visual highlighting.
2664       (when (gnus-visual-p 'summary-highlight 'highlight)
2665         (gnus-run-hooks 'gnus-summary-update-hook)))))
2666
2667 (defvar gnus-tmp-new-adopts nil)
2668
2669 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2670   "Return the number of articles in THREAD.
2671 This may be 0 in some cases -- if none of the articles in
2672 the thread are to be displayed."
2673   (let* ((number
2674           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2675           (cond
2676            ((not (listp thread))
2677             1)
2678            ((and (consp thread) (cdr thread))
2679             (apply
2680              '+ 1 (mapcar
2681                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2682            ((null thread)
2683             1)
2684            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2685             1)
2686            (t 0))))
2687     (when (and level (zerop level) gnus-tmp-new-adopts)
2688       (incf number
2689             (apply '+ (mapcar
2690                        'gnus-summary-number-of-articles-in-thread
2691                        gnus-tmp-new-adopts))))
2692     (if char
2693         (if (> number 1) gnus-not-empty-thread-mark
2694           gnus-empty-thread-mark)
2695       number)))
2696
2697 (defun gnus-summary-set-local-parameters (group)
2698   "Go through the local params of GROUP and set all variable specs in that list."
2699   (let ((params (gnus-group-find-parameter group))
2700         elem)
2701     (while params
2702       (setq elem (car params)
2703             params (cdr params))
2704       (and (consp elem)                 ; Has to be a cons.
2705            (consp (cdr elem))           ; The cdr has to be a list.
2706            (symbolp (car elem))         ; Has to be a symbol in there.
2707            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2708            (ignore-errors               ; So we set it.
2709              (make-local-variable (car elem))
2710              (set (car elem) (eval (nth 1 elem))))))))
2711
2712 (defun gnus-summary-read-group (group &optional show-all no-article
2713                                       kill-buffer no-display backward
2714                                       select-articles)
2715   "Start reading news in newsgroup GROUP.
2716 If SHOW-ALL is non-nil, already read articles are also listed.
2717 If NO-ARTICLE is non-nil, no article is selected initially.
2718 If NO-DISPLAY, don't generate a summary buffer."
2719   (let (result)
2720     (while (and group
2721                 (null (setq result
2722                             (let ((gnus-auto-select-next nil))
2723                               (or (gnus-summary-read-group-1
2724                                    group show-all no-article
2725                                    kill-buffer no-display
2726                                    select-articles)
2727                                   (setq show-all nil
2728                                         select-articles nil)))))
2729                 (eq gnus-auto-select-next 'quietly))
2730       (set-buffer gnus-group-buffer)
2731       ;; The entry function called above goes to the next
2732       ;; group automatically, so we go two groups back
2733       ;; if we are searching for the previous group.
2734       (when backward
2735         (gnus-group-prev-unread-group 2))
2736       (if (not (equal group (gnus-group-group-name)))
2737           (setq group (gnus-group-group-name))
2738         (setq group nil)))
2739     result))
2740
2741 (defun gnus-summary-read-group-1 (group show-all no-article
2742                                         kill-buffer no-display
2743                                         &optional select-articles)
2744   ;; Killed foreign groups can't be entered.
2745   (when (and (not (gnus-group-native-p group))
2746              (not (gnus-gethash group gnus-newsrc-hashtb)))
2747     (error "Dead non-native groups can't be entered"))
2748   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2749   (let* ((new-group (gnus-summary-setup-buffer group))
2750          (quit-config (gnus-group-quit-config group))
2751          (did-select (and new-group (gnus-select-newsgroup
2752                                      group show-all select-articles))))
2753     (cond
2754      ;; This summary buffer exists already, so we just select it.
2755      ((not new-group)
2756       (gnus-set-global-variables)
2757       (when kill-buffer
2758         (gnus-kill-or-deaden-summary kill-buffer))
2759       (gnus-configure-windows 'summary 'force)
2760       (gnus-set-mode-line 'summary)
2761       (gnus-summary-position-point)
2762       (message "")
2763       t)
2764      ;; We couldn't select this group.
2765      ((null did-select)
2766       (when (and (eq major-mode 'gnus-summary-mode)
2767                  (not (equal (current-buffer) kill-buffer)))
2768         (kill-buffer (current-buffer))
2769         (if (not quit-config)
2770             (progn
2771               ;; Update the info -- marks might need to be removed,
2772               ;; for instance.
2773               (gnus-summary-update-info)
2774               (set-buffer gnus-group-buffer)
2775               (gnus-group-jump-to-group group)
2776               (gnus-group-next-unread-group 1))
2777           (gnus-handle-ephemeral-exit quit-config)))
2778       (gnus-message 3 "Can't select group")
2779       nil)
2780      ;; The user did a `C-g' while prompting for number of articles,
2781      ;; so we exit this group.
2782      ((eq did-select 'quit)
2783       (and (eq major-mode 'gnus-summary-mode)
2784            (not (equal (current-buffer) kill-buffer))
2785            (kill-buffer (current-buffer)))
2786       (when kill-buffer
2787         (gnus-kill-or-deaden-summary kill-buffer))
2788       (if (not quit-config)
2789           (progn
2790             (set-buffer gnus-group-buffer)
2791             (gnus-group-jump-to-group group)
2792             (gnus-group-next-unread-group 1)
2793             (gnus-configure-windows 'group 'force))
2794         (gnus-handle-ephemeral-exit quit-config))
2795       ;; Finally signal the quit.
2796       (signal 'quit nil))
2797      ;; The group was successfully selected.
2798      (t
2799       (gnus-set-global-variables)
2800       ;; Save the active value in effect when the group was entered.
2801       (setq gnus-newsgroup-active
2802             (gnus-copy-sequence
2803              (gnus-active gnus-newsgroup-name)))
2804       ;; You can change the summary buffer in some way with this hook.
2805       (gnus-run-hooks 'gnus-select-group-hook)
2806       ;; Set any local variables in the group parameters.
2807       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2808       (gnus-update-format-specifications
2809        nil 'summary 'summary-mode 'summary-dummy)
2810       (gnus-update-summary-mark-positions)
2811       ;; Do score processing.
2812       (when gnus-use-scoring
2813         (gnus-possibly-score-headers))
2814       ;; Check whether to fill in the gaps in the threads.
2815       (when gnus-build-sparse-threads
2816         (gnus-build-sparse-threads))
2817       ;; Find the initial limit.
2818       (if gnus-show-threads
2819           (if show-all
2820               (let ((gnus-newsgroup-dormant nil))
2821                 (gnus-summary-initial-limit show-all))
2822             (gnus-summary-initial-limit show-all))
2823         (setq gnus-newsgroup-limit
2824               (mapcar
2825                (lambda (header) (mail-header-number header))
2826                gnus-newsgroup-headers)))
2827       ;; Generate the summary buffer.
2828       (unless no-display
2829         (gnus-summary-prepare))
2830       (when gnus-use-trees
2831         (gnus-tree-open group)
2832         (setq gnus-summary-highlight-line-function
2833               'gnus-tree-highlight-article))
2834       ;; If the summary buffer is empty, but there are some low-scored
2835       ;; articles or some excluded dormants, we include these in the
2836       ;; buffer.
2837       (when (and (zerop (buffer-size))
2838                  (not no-display))
2839         (cond (gnus-newsgroup-dormant
2840                (gnus-summary-limit-include-dormant))
2841               ((and gnus-newsgroup-scored show-all)
2842                (gnus-summary-limit-include-expunged t))))
2843       ;; Function `gnus-apply-kill-file' must be called in this hook.
2844       (gnus-run-hooks 'gnus-apply-kill-hook)
2845       (if (and (zerop (buffer-size))
2846                (not no-display))
2847           (progn
2848             ;; This newsgroup is empty.
2849             (gnus-summary-catchup-and-exit nil t)
2850             (gnus-message 6 "No unread news")
2851             (when kill-buffer
2852               (gnus-kill-or-deaden-summary kill-buffer))
2853             ;; Return nil from this function.
2854             nil)
2855         ;; Hide conversation thread subtrees.  We cannot do this in
2856         ;; gnus-summary-prepare-hook since kill processing may not
2857         ;; work with hidden articles.
2858         (and gnus-show-threads
2859              gnus-thread-hide-subtree
2860              (gnus-summary-hide-all-threads))
2861         (when kill-buffer
2862           (gnus-kill-or-deaden-summary kill-buffer))
2863         ;; Show first unread article if requested.
2864         (if (and (not no-article)
2865                  (not no-display)
2866                  gnus-newsgroup-unreads
2867                  gnus-auto-select-first)
2868             (progn
2869               (gnus-configure-windows 'summary)
2870               (cond
2871                ((eq gnus-auto-select-first 'best)
2872                 (gnus-summary-best-unread-article))
2873                ((eq gnus-auto-select-first t)
2874                 (gnus-summary-first-unread-article))
2875                ((gnus-functionp gnus-auto-select-first)
2876                 (funcall gnus-auto-select-first))))
2877           ;; Don't select any articles, just move point to the first
2878           ;; article in the group.
2879           (goto-char (point-min))
2880           (gnus-summary-position-point)
2881           (gnus-configure-windows 'summary 'force)
2882           (gnus-set-mode-line 'summary))
2883         (when (get-buffer-window gnus-group-buffer t)
2884           ;; Gotta use windows, because recenter does weird stuff if
2885           ;; the current buffer ain't the displayed window.
2886           (let ((owin (selected-window)))
2887             (select-window (get-buffer-window gnus-group-buffer t))
2888             (when (gnus-group-goto-group group)
2889               (recenter))
2890             (select-window owin)))
2891         ;; Mark this buffer as "prepared".
2892         (setq gnus-newsgroup-prepared t)
2893         (gnus-run-hooks 'gnus-summary-prepared-hook)
2894         t)))))
2895
2896 (defun gnus-summary-prepare ()
2897   "Generate the summary buffer."
2898   (interactive)
2899   (let ((buffer-read-only nil))
2900     (erase-buffer)
2901     (setq gnus-newsgroup-data nil
2902           gnus-newsgroup-data-reverse nil)
2903     (gnus-run-hooks 'gnus-summary-generate-hook)
2904     ;; Generate the buffer, either with threads or without.
2905     (when gnus-newsgroup-headers
2906       (gnus-summary-prepare-threads
2907        (if gnus-show-threads
2908            (gnus-sort-gathered-threads
2909             (funcall gnus-summary-thread-gathering-function
2910                      (gnus-sort-threads
2911                       (gnus-cut-threads (gnus-make-threads)))))
2912          ;; Unthreaded display.
2913          (gnus-sort-articles gnus-newsgroup-headers))))
2914     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2915     ;; Call hooks for modifying summary buffer.
2916     (goto-char (point-min))
2917     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2918
2919 (defsubst gnus-general-simplify-subject (subject)
2920   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2921   (setq subject
2922         (cond
2923          ;; Truncate the subject.
2924          (gnus-simplify-subject-functions
2925           (gnus-map-function gnus-simplify-subject-functions subject))
2926          ((numberp gnus-summary-gather-subject-limit)
2927           (setq subject (gnus-simplify-subject-re subject))
2928           (if (> (length subject) gnus-summary-gather-subject-limit)
2929               (substring subject 0 gnus-summary-gather-subject-limit)
2930             subject))
2931          ;; Fuzzily simplify it.
2932          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2933           (gnus-simplify-subject-fuzzy subject))
2934          ;; Just remove the leading "Re:".
2935          (t
2936           (gnus-simplify-subject-re subject))))
2937
2938   (if (and gnus-summary-gather-exclude-subject
2939            (string-match gnus-summary-gather-exclude-subject subject))
2940       nil                               ; This article shouldn't be gathered
2941     subject))
2942
2943 (defun gnus-summary-simplify-subject-query ()
2944   "Query where the respool algorithm would put this article."
2945   (interactive)
2946   (gnus-summary-select-article)
2947   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2948
2949 (defun gnus-gather-threads-by-subject (threads)
2950   "Gather threads by looking at Subject headers."
2951   (if (not gnus-summary-make-false-root)
2952       threads
2953     (let ((hashtb (gnus-make-hashtable 1024))
2954           (prev threads)
2955           (result threads)
2956           subject hthread whole-subject)
2957       (while threads
2958         (setq subject (gnus-general-simplify-subject
2959                        (setq whole-subject (mail-header-subject
2960                                             (caar threads)))))
2961         (when subject
2962           (if (setq hthread (gnus-gethash subject hashtb))
2963               (progn
2964                 ;; We enter a dummy root into the thread, if we
2965                 ;; haven't done that already.
2966                 (unless (stringp (caar hthread))
2967                   (setcar hthread (list whole-subject (car hthread))))
2968                 ;; We add this new gathered thread to this gathered
2969                 ;; thread.
2970                 (setcdr (car hthread)
2971                         (nconc (cdar hthread) (list (car threads))))
2972                 ;; Remove it from the list of threads.
2973                 (setcdr prev (cdr threads))
2974                 (setq threads prev))
2975             ;; Enter this thread into the hash table.
2976             (gnus-sethash subject threads hashtb)))
2977         (setq prev threads)
2978         (setq threads (cdr threads)))
2979       result)))
2980
2981 (defun gnus-gather-threads-by-references (threads)
2982   "Gather threads by looking at References headers."
2983   (let ((idhashtb (gnus-make-hashtable 1024))
2984         (thhashtb (gnus-make-hashtable 1024))
2985         (prev threads)
2986         (result threads)
2987         ids references id gthread gid entered ref)
2988     (while threads
2989       (when (setq references (mail-header-references (caar threads)))
2990         (setq id (mail-header-id (caar threads))
2991               ids (gnus-split-references references)
2992               entered nil)
2993         (while (setq ref (pop ids))
2994           (setq ids (delete ref ids))
2995           (if (not (setq gid (gnus-gethash ref idhashtb)))
2996               (progn
2997                 (gnus-sethash ref id idhashtb)
2998                 (gnus-sethash id threads thhashtb))
2999             (setq gthread (gnus-gethash gid thhashtb))
3000             (unless entered
3001               ;; We enter a dummy root into the thread, if we
3002               ;; haven't done that already.
3003               (unless (stringp (caar gthread))
3004                 (setcar gthread (list (mail-header-subject (caar gthread))
3005                                       (car gthread))))
3006               ;; We add this new gathered thread to this gathered
3007               ;; thread.
3008               (setcdr (car gthread)
3009                       (nconc (cdar gthread) (list (car threads)))))
3010             ;; Add it into the thread hash table.
3011             (gnus-sethash id gthread thhashtb)
3012             (setq entered t)
3013             ;; Remove it from the list of threads.
3014             (setcdr prev (cdr threads))
3015             (setq threads prev))))
3016       (setq prev threads)
3017       (setq threads (cdr threads)))
3018     result))
3019
3020 (defun gnus-sort-gathered-threads (threads)
3021   "Sort subtreads inside each gathered thread by article number."
3022   (let ((result threads))
3023     (while threads
3024       (when (stringp (caar threads))
3025         (setcdr (car threads)
3026                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3027       (setq threads (cdr threads)))
3028     result))
3029
3030 (defun gnus-thread-loop-p (root thread)
3031   "Say whether ROOT is in THREAD."
3032   (let ((stack (list thread))
3033         (infloop 0)
3034         th)
3035     (while (setq thread (pop stack))
3036       (setq th (cdr thread))
3037       (while (and th
3038                   (not (eq (caar th) root)))
3039         (pop th))
3040       (if th
3041           ;; We have found a loop.
3042           (let (ref-dep)
3043             (setcdr thread (delq (car th) (cdr thread)))
3044             (if (boundp (setq ref-dep (intern "none"
3045                                               gnus-newsgroup-dependencies)))
3046                 (setcdr (symbol-value ref-dep)
3047                         (nconc (cdr (symbol-value ref-dep))
3048                                (list (car th))))
3049               (set ref-dep (list nil (car th))))
3050             (setq infloop 1
3051                   stack nil))
3052         ;; Push all the subthreads onto the stack.
3053         (push (cdr thread) stack)))
3054     infloop))
3055
3056 (defun gnus-make-threads ()
3057   "Go through the dependency hashtb and find the roots.  Return all threads."
3058   (let (threads)
3059     (while (catch 'infloop
3060              (mapatoms
3061               (lambda (refs)
3062                 ;; Deal with self-referencing References loops.
3063                 (when (and (car (symbol-value refs))
3064                            (not (zerop
3065                                  (apply
3066                                   '+
3067                                   (mapcar
3068                                    (lambda (thread)
3069                                      (gnus-thread-loop-p
3070                                       (car (symbol-value refs)) thread))
3071                                    (cdr (symbol-value refs)))))))
3072                   (setq threads nil)
3073                   (throw 'infloop t))
3074                 (unless (car (symbol-value refs))
3075                   ;; These threads do not refer back to any other articles,
3076                   ;; so they're roots.
3077                   (setq threads (append (cdr (symbol-value refs)) threads))))
3078               gnus-newsgroup-dependencies)))
3079     threads))
3080
3081 ;; Build the thread tree.
3082 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3083   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3084
3085 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3086 if it was already present.
3087
3088 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3089 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3090 Message-IDs will be renamed be renamed to a unique Message-ID before
3091 being entered.
3092
3093 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3094   (let* ((id (mail-header-id header))
3095          (id-dep (and id (intern id dependencies)))
3096          ref ref-dep ref-header)
3097     ;; Enter this `header' in the `dependencies' table.
3098     (cond
3099      ((not id-dep)
3100       (setq header nil))
3101      ;; The first two cases do the normal part: enter a new `header'
3102      ;; in the `dependencies' table.
3103      ((not (boundp id-dep))
3104       (set id-dep (list header)))
3105      ((null (car (symbol-value id-dep)))
3106       (setcar (symbol-value id-dep) header))
3107
3108      ;; From here the `header' was already present in the
3109      ;; `dependencies' table.
3110      (force-new
3111       ;; Overrides an existing entry;
3112       ;; just set the header part of the entry.
3113       (setcar (symbol-value id-dep) header))
3114
3115      ;; Renames the existing `header' to a unique Message-ID.
3116      ((not gnus-summary-ignore-duplicates)
3117       ;; An article with this Message-ID has already been seen.
3118       ;; We rename the Message-ID.
3119       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3120            (list header))
3121       (mail-header-set-id header id))
3122
3123      ;; The last case ignores an existing entry, except it adds any
3124      ;; additional Xrefs (in case the two articles came from different
3125      ;; servers.
3126      ;; Also sets `header' to `nil' meaning that the `dependencies'
3127      ;; table was *not* modified.
3128      (t
3129       (mail-header-set-xref
3130        (car (symbol-value id-dep))
3131        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3132                    "")
3133                (or (mail-header-xref header) "")))
3134       (setq header nil)))
3135
3136     (when header
3137       ;; First check if that we are not creating a References loop.
3138       (setq ref (gnus-parent-id (mail-header-references header)))
3139       (while (and ref
3140                   (setq ref-dep (intern-soft ref dependencies))
3141                   (boundp ref-dep)
3142                   (setq ref-header (car (symbol-value ref-dep))))
3143         (if (string= id ref)
3144             ;; Yuk!  This is a reference loop.  Make the article be a
3145             ;; root article.
3146             (progn
3147               (mail-header-set-references (car (symbol-value id-dep)) "none")
3148               (setq ref nil))
3149           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3150       (setq ref (gnus-parent-id (mail-header-references header)))
3151       (setq ref-dep (intern (or ref "none") dependencies))
3152       (if (boundp ref-dep)
3153           (setcdr (symbol-value ref-dep)
3154                   (nconc (cdr (symbol-value ref-dep))
3155                          (list (symbol-value id-dep))))
3156         (set ref-dep (list nil (symbol-value id-dep)))))
3157     header))
3158
3159 (defun gnus-build-sparse-threads ()
3160   (let ((headers gnus-newsgroup-headers)
3161         (gnus-summary-ignore-duplicates t)
3162         header references generation relations
3163         subject child end new-child date)
3164     ;; First we create an alist of generations/relations, where
3165     ;; generations is how much we trust the relation, and the relation
3166     ;; is parent/child.
3167     (gnus-message 7 "Making sparse threads...")
3168     (save-excursion
3169       (nnheader-set-temp-buffer " *gnus sparse threads*")
3170       (while (setq header (pop headers))
3171         (when (and (setq references (mail-header-references header))
3172                    (not (string= references "")))
3173           (insert references)
3174           (setq child (mail-header-id header)
3175                 subject (mail-header-subject header)
3176                 date (mail-header-date header)
3177                 generation 0)
3178           (while (search-backward ">" nil t)
3179             (setq end (1+ (point)))
3180             (when (search-backward "<" nil t)
3181               (setq new-child (buffer-substring (point) end))
3182               (push (list (incf generation)
3183                           child (setq child new-child)
3184                           subject date)
3185                     relations)))
3186           (when child
3187             (push (list (1+ generation) child nil subject) relations))
3188           (erase-buffer)))
3189       (kill-buffer (current-buffer)))
3190     ;; Sort over trustworthiness.
3191     (mapcar
3192      (lambda (relation)
3193        (when (gnus-dependencies-add-header
3194               (make-full-mail-header
3195                gnus-reffed-article-number
3196                (nth 3 relation) "" (or (nth 4 relation) "")
3197                (nth 1 relation)
3198                (or (nth 2 relation) "") 0 0 "")
3199               gnus-newsgroup-dependencies nil)
3200          (push gnus-reffed-article-number gnus-newsgroup-limit)
3201          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3202          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3203                gnus-newsgroup-reads)
3204          (decf gnus-reffed-article-number)))
3205      (sort relations 'car-less-than-car))
3206     (gnus-message 7 "Making sparse threads...done")))
3207
3208 (defun gnus-build-old-threads ()
3209   ;; Look at all the articles that refer back to old articles, and
3210   ;; fetch the headers for the articles that aren't there.  This will
3211   ;; build complete threads - if the roots haven't been expired by the
3212   ;; server, that is.
3213   (let (id heads)
3214     (mapatoms
3215      (lambda (refs)
3216        (when (not (car (symbol-value refs)))
3217          (setq heads (cdr (symbol-value refs)))
3218          (while heads
3219            (if (memq (mail-header-number (caar heads))
3220                      gnus-newsgroup-dormant)
3221                (setq heads (cdr heads))
3222              (setq id (symbol-name refs))
3223              (while (and (setq id (gnus-build-get-header id))
3224                          (not (car (gnus-id-to-thread id)))))
3225              (setq heads nil)))))
3226      gnus-newsgroup-dependencies)))
3227
3228 ;; This function has to be called with point after the article number
3229 ;; on the beginning of the line.
3230 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3231   (let ((eol (gnus-point-at-eol))
3232         (buffer (current-buffer))
3233         header)
3234
3235     ;; overview: [num subject from date id refs chars lines misc]
3236     (unwind-protect
3237         (progn
3238           (narrow-to-region (point) eol)
3239           (unless (eobp)
3240             (forward-char))
3241
3242           (setq header
3243                 (make-full-mail-header
3244                  number                         ; number
3245                  (funcall gnus-decode-encoded-word-function
3246                           (nnheader-nov-field)) ; subject
3247                  (funcall gnus-decode-encoded-word-function
3248                           (nnheader-nov-field)) ; from
3249                  (nnheader-nov-field)           ; date
3250                  (nnheader-nov-read-message-id) ; id
3251                  (nnheader-nov-field)           ; refs
3252                  (nnheader-nov-read-integer)    ; chars
3253                  (nnheader-nov-read-integer)    ; lines
3254                  (unless (eobp)
3255                    (nnheader-nov-field))                ; misc
3256                  (nnheader-nov-parse-extra))))  ; extra
3257
3258       (widen))
3259
3260     (when gnus-alter-header-function
3261       (funcall gnus-alter-header-function header))
3262     (gnus-dependencies-add-header header dependencies force-new)))
3263
3264 (defun gnus-build-get-header (id)
3265   ;; Look through the buffer of NOV lines and find the header to
3266   ;; ID.  Enter this line into the dependencies hash table, and return
3267   ;; the id of the parent article (if any).
3268   (let ((deps gnus-newsgroup-dependencies)
3269         found header)
3270     (prog1
3271         (save-excursion
3272           (set-buffer nntp-server-buffer)
3273           (let ((case-fold-search nil))
3274             (goto-char (point-min))
3275             (while (and (not found)
3276                         (search-forward id nil t))
3277               (beginning-of-line)
3278               (setq found (looking-at
3279                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3280                                    (regexp-quote id))))
3281               (or found (beginning-of-line 2)))
3282             (when found
3283               (beginning-of-line)
3284               (and
3285                (setq header (gnus-nov-parse-line
3286                              (read (current-buffer)) deps))
3287                (gnus-parent-id (mail-header-references header))))))
3288       (when header
3289         (let ((number (mail-header-number header)))
3290           (push number gnus-newsgroup-limit)
3291           (push header gnus-newsgroup-headers)
3292           (if (memq number gnus-newsgroup-unselected)
3293               (progn
3294                 (push number gnus-newsgroup-unreads)
3295                 (setq gnus-newsgroup-unselected
3296                       (delq number gnus-newsgroup-unselected)))
3297             (push number gnus-newsgroup-ancient)))))))
3298
3299 (defun gnus-build-all-threads ()
3300   "Read all the headers."
3301   (let ((gnus-summary-ignore-duplicates t)
3302         (dependencies gnus-newsgroup-dependencies)
3303         header article)
3304     (save-excursion
3305       (set-buffer nntp-server-buffer)
3306       (let ((case-fold-search nil))
3307         (goto-char (point-min))
3308         (while (not (eobp))
3309           (ignore-errors
3310             (setq article (read (current-buffer))
3311                   header (gnus-nov-parse-line
3312                           article dependencies)))
3313           (when header
3314             (save-excursion
3315               (set-buffer gnus-summary-buffer)
3316               (push header gnus-newsgroup-headers)
3317               (if (memq (setq article (mail-header-number header))
3318                         gnus-newsgroup-unselected)
3319                   (progn
3320                     (push article gnus-newsgroup-unreads)
3321                     (setq gnus-newsgroup-unselected
3322                           (delq article gnus-newsgroup-unselected)))
3323                 (push article gnus-newsgroup-ancient)))
3324             (forward-line 1)))))))
3325
3326 (defun gnus-summary-update-article-line (article header)
3327   "Update the line for ARTICLE using HEADERS."
3328   (let* ((id (mail-header-id header))
3329          (thread (gnus-id-to-thread id)))
3330     (unless thread
3331       (error "Article in no thread"))
3332     ;; Update the thread.
3333     (setcar thread header)
3334     (gnus-summary-goto-subject article)
3335     (let* ((datal (gnus-data-find-list article))
3336            (data (car datal))
3337            (length (when (cdr datal)
3338                      (- (gnus-data-pos data)
3339                         (gnus-data-pos (cadr datal)))))
3340            (buffer-read-only nil)
3341            (level (gnus-summary-thread-level)))
3342       (gnus-delete-line)
3343       (gnus-summary-insert-line
3344        header level nil (gnus-article-mark article)
3345        (memq article gnus-newsgroup-replied)
3346        (memq article gnus-newsgroup-expirable)
3347        ;; Only insert the Subject string when it's different
3348        ;; from the previous Subject string.
3349        (if (gnus-subject-equal
3350             (condition-case ()
3351                 (mail-header-subject
3352                  (gnus-data-header
3353                   (cadr
3354                    (gnus-data-find-list
3355                     article
3356                     (gnus-data-list t)))))
3357               ;; Error on the side of excessive subjects.
3358               (error ""))
3359             (mail-header-subject header))
3360            ""
3361          (mail-header-subject header))
3362        nil (cdr (assq article gnus-newsgroup-scored))
3363        (memq article gnus-newsgroup-processable))
3364       (when length
3365         (gnus-data-update-list
3366          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3367
3368 (defun gnus-summary-update-article (article &optional iheader)
3369   "Update ARTICLE in the summary buffer."
3370   (set-buffer gnus-summary-buffer)
3371   (let* ((header (gnus-summary-article-header article))
3372          (id (mail-header-id header))
3373          (data (gnus-data-find article))
3374          (thread (gnus-id-to-thread id))
3375          (references (mail-header-references header))
3376          (parent
3377           (gnus-id-to-thread
3378            (or (gnus-parent-id
3379                 (when (and references
3380                            (not (equal "" references)))
3381                   references))
3382                "none")))
3383          (buffer-read-only nil)
3384          (old (car thread)))
3385     (when thread
3386       (unless iheader
3387         (setcar thread nil)
3388         (when parent
3389           (delq thread parent)))
3390       (if (gnus-summary-insert-subject id header)
3391           ;; Set the (possibly) new article number in the data structure.
3392           (gnus-data-set-number data (gnus-id-to-article id))
3393         (setcar thread old)
3394         nil))))
3395
3396 (defun gnus-rebuild-thread (id &optional line)
3397   "Rebuild the thread containing ID.
3398 If LINE, insert the rebuilt thread starting on line LINE."
3399   (let ((buffer-read-only nil)
3400         old-pos current thread data)
3401     (if (not gnus-show-threads)
3402         (setq thread (list (car (gnus-id-to-thread id))))
3403       ;; Get the thread this article is part of.
3404       (setq thread (gnus-remove-thread id)))
3405     (setq old-pos (gnus-point-at-bol))
3406     (setq current (save-excursion
3407                     (and (zerop (forward-line -1))
3408                          (gnus-summary-article-number))))
3409     ;; If this is a gathered thread, we have to go some re-gathering.
3410     (when (stringp (car thread))
3411       (let ((subject (car thread))
3412             roots thr)
3413         (setq thread (cdr thread))
3414         (while thread
3415           (unless (memq (setq thr (gnus-id-to-thread
3416                                    (gnus-root-id
3417                                     (mail-header-id (caar thread)))))
3418                         roots)
3419             (push thr roots))
3420           (setq thread (cdr thread)))
3421         ;; We now have all (unique) roots.
3422         (if (= (length roots) 1)
3423             ;; All the loose roots are now one solid root.
3424             (setq thread (car roots))
3425           (setq thread (cons subject (gnus-sort-threads roots))))))
3426     (let (threads)
3427       ;; We then insert this thread into the summary buffer.
3428       (when line
3429         (goto-char (point-min))
3430         (forward-line (1- line)))
3431       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3432         (if gnus-show-threads
3433             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3434           (gnus-summary-prepare-unthreaded thread))
3435         (setq data (nreverse gnus-newsgroup-data))
3436         (setq threads gnus-newsgroup-threads))
3437       ;; We splice the new data into the data structure.
3438       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3439       ;;!!! then we want to insert at the beginning of the buffer.
3440       ;;!!! That happens to be true with Gnus now, but that may
3441       ;;!!! change in the future.  Perhaps.
3442       (gnus-data-enter-list
3443        (if line nil current) data (- (point) old-pos))
3444       (setq gnus-newsgroup-threads
3445             (nconc threads gnus-newsgroup-threads))
3446       (gnus-data-compute-positions))))
3447
3448 (defun gnus-number-to-header (number)
3449   "Return the header for article NUMBER."
3450   (let ((headers gnus-newsgroup-headers))
3451     (while (and headers
3452                 (not (= number (mail-header-number (car headers)))))
3453       (pop headers))
3454     (when headers
3455       (car headers))))
3456
3457 (defun gnus-parent-headers (in-headers &optional generation)
3458   "Return the headers of the GENERATIONeth parent of HEADERS."
3459   (unless generation
3460     (setq generation 1))
3461   (let ((parent t)
3462         (headers in-headers)
3463         references)
3464     (while (and parent
3465                 (not (zerop generation))
3466                 (setq references (mail-header-references headers)))
3467       (setq headers (if (and references
3468                              (setq parent (gnus-parent-id references)))
3469                         (car (gnus-id-to-thread parent))
3470                       nil))
3471       (decf generation))
3472     (and (not (eq headers in-headers))
3473          headers)))
3474
3475 (defun gnus-id-to-thread (id)
3476   "Return the (sub-)thread where ID appears."
3477   (gnus-gethash id gnus-newsgroup-dependencies))
3478
3479 (defun gnus-id-to-article (id)
3480   "Return the article number of ID."
3481   (let ((thread (gnus-id-to-thread id)))
3482     (when (and thread
3483                (car thread))
3484       (mail-header-number (car thread)))))
3485
3486 (defun gnus-id-to-header (id)
3487   "Return the article headers of ID."
3488   (car (gnus-id-to-thread id)))
3489
3490 (defun gnus-article-displayed-root-p (article)
3491   "Say whether ARTICLE is a root(ish) article."
3492   (let ((level (gnus-summary-thread-level article))
3493         (refs (mail-header-references  (gnus-summary-article-header article)))
3494         particle)
3495     (cond
3496      ((null level) nil)
3497      ((zerop level) t)
3498      ((null refs) t)
3499      ((null (gnus-parent-id refs)) t)
3500      ((and (= 1 level)
3501            (null (setq particle (gnus-id-to-article
3502                                  (gnus-parent-id refs))))
3503            (null (gnus-summary-thread-level particle)))))))
3504
3505 (defun gnus-root-id (id)
3506   "Return the id of the root of the thread where ID appears."
3507   (let (last-id prev)
3508     (while (and id (setq prev (car (gnus-id-to-thread id))))
3509       (setq last-id id
3510             id (gnus-parent-id (mail-header-references prev))))
3511     last-id))
3512
3513 (defun gnus-articles-in-thread (thread)
3514   "Return the list of articles in THREAD."
3515   (cons (mail-header-number (car thread))
3516         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3517
3518 (defun gnus-remove-thread (id &optional dont-remove)
3519   "Remove the thread that has ID in it."
3520   (let (headers thread last-id)
3521     ;; First go up in this thread until we find the root.
3522     (setq last-id (gnus-root-id id)
3523           headers (message-flatten-list (gnus-id-to-thread last-id)))
3524     ;; We have now found the real root of this thread.  It might have
3525     ;; been gathered into some loose thread, so we have to search
3526     ;; through the threads to find the thread we wanted.
3527     (let ((threads gnus-newsgroup-threads)
3528           sub)
3529       (while threads
3530         (setq sub (car threads))
3531         (if (stringp (car sub))
3532             ;; This is a gathered thread, so we look at the roots
3533             ;; below it to find whether this article is in this
3534             ;; gathered root.
3535             (progn
3536               (setq sub (cdr sub))
3537               (while sub
3538                 (when (member (caar sub) headers)
3539                   (setq thread (car threads)
3540                         threads nil
3541                         sub nil))
3542                 (setq sub (cdr sub))))
3543           ;; It's an ordinary thread, so we check it.
3544           (when (eq (car sub) (car headers))
3545             (setq thread sub
3546                   threads nil)))
3547         (setq threads (cdr threads)))
3548       ;; If this article is in no thread, then it's a root.
3549       (if thread
3550           (unless dont-remove
3551             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3552         (setq thread (gnus-id-to-thread last-id)))
3553       (when thread
3554         (prog1
3555             thread                      ; We return this thread.
3556           (unless dont-remove
3557             (if (stringp (car thread))
3558                 (progn
3559                   ;; If we use dummy roots, then we have to remove the
3560                   ;; dummy root as well.
3561                   (when (eq gnus-summary-make-false-root 'dummy)
3562                     ;; We go to the dummy root by going to
3563                     ;; the first sub-"thread", and then one line up.
3564                     (gnus-summary-goto-article
3565                      (mail-header-number (caadr thread)))
3566                     (forward-line -1)
3567                     (gnus-delete-line)
3568                     (gnus-data-compute-positions))
3569                   (setq thread (cdr thread))
3570                   (while thread
3571                     (gnus-remove-thread-1 (car thread))
3572                     (setq thread (cdr thread))))
3573               (gnus-summary-show-all-threads)
3574               (gnus-remove-thread-1 thread))))))))
3575
3576 (defun gnus-remove-thread-1 (thread)
3577   "Remove the thread THREAD recursively."
3578   (let ((number (mail-header-number (pop thread)))
3579         d)
3580     (setq thread (reverse thread))
3581     (while thread
3582       (gnus-remove-thread-1 (pop thread)))
3583     (when (setq d (gnus-data-find number))
3584       (goto-char (gnus-data-pos d))
3585       (gnus-data-remove
3586        number
3587        (- (gnus-point-at-bol)
3588           (prog1
3589               (1+ (gnus-point-at-eol))
3590             (gnus-delete-line)))))))
3591
3592 (defun gnus-sort-threads (threads)
3593   "Sort THREADS."
3594   (if (not gnus-thread-sort-functions)
3595       threads
3596     (gnus-message 8 "Sorting threads...")
3597     (prog1
3598         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3599       (gnus-message 8 "Sorting threads...done"))))
3600
3601 (defun gnus-sort-articles (articles)
3602   "Sort ARTICLES."
3603   (when gnus-article-sort-functions
3604     (gnus-message 7 "Sorting articles...")
3605     (prog1
3606         (setq gnus-newsgroup-headers
3607               (sort articles (gnus-make-sort-function
3608                               gnus-article-sort-functions)))
3609       (gnus-message 7 "Sorting articles...done"))))
3610
3611 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3612 (defmacro gnus-thread-header (thread)
3613   ;; Return header of first article in THREAD.
3614   ;; Note that THREAD must never, ever be anything else than a variable -
3615   ;; using some other form will lead to serious barfage.
3616   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3617   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3618   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3619         (vector thread) 2))
3620
3621 (defsubst gnus-article-sort-by-number (h1 h2)
3622   "Sort articles by article number."
3623   (< (mail-header-number h1)
3624      (mail-header-number h2)))
3625
3626 (defun gnus-thread-sort-by-number (h1 h2)
3627   "Sort threads by root article number."
3628   (gnus-article-sort-by-number
3629    (gnus-thread-header h1) (gnus-thread-header h2)))
3630
3631 (defsubst gnus-article-sort-by-lines (h1 h2)
3632   "Sort articles by article Lines header."
3633   (< (mail-header-lines h1)
3634      (mail-header-lines h2)))
3635
3636 (defun gnus-thread-sort-by-lines (h1 h2)
3637   "Sort threads by root article Lines header."
3638   (gnus-article-sort-by-lines
3639    (gnus-thread-header h1) (gnus-thread-header h2)))
3640
3641 (defsubst gnus-article-sort-by-chars (h1 h2)
3642   "Sort articles by octet length."
3643   (< (mail-header-chars h1)
3644      (mail-header-chars h2)))
3645
3646 (defun gnus-thread-sort-by-chars (h1 h2)
3647   "Sort threads by root article octet length."
3648   (gnus-article-sort-by-chars
3649    (gnus-thread-header h1) (gnus-thread-header h2)))
3650
3651 (defsubst gnus-article-sort-by-author (h1 h2)
3652   "Sort articles by root author."
3653   (string-lessp
3654    (let ((extract (funcall
3655                    gnus-extract-address-components
3656                    (mail-header-from h1))))
3657      (or (car extract) (cadr extract) ""))
3658    (let ((extract (funcall
3659                    gnus-extract-address-components
3660                    (mail-header-from h2))))
3661      (or (car extract) (cadr extract) ""))))
3662
3663 (defun gnus-thread-sort-by-author (h1 h2)
3664   "Sort threads by root author."
3665   (gnus-article-sort-by-author
3666    (gnus-thread-header h1)  (gnus-thread-header h2)))
3667
3668 (defsubst gnus-article-sort-by-subject (h1 h2)
3669   "Sort articles by root subject."
3670   (string-lessp
3671    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3672    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3673
3674 (defun gnus-thread-sort-by-subject (h1 h2)
3675   "Sort threads by root subject."
3676   (gnus-article-sort-by-subject
3677    (gnus-thread-header h1) (gnus-thread-header h2)))
3678
3679 (defsubst gnus-article-sort-by-date (h1 h2)
3680   "Sort articles by root article date."
3681   (time-less-p
3682    (gnus-date-get-time (mail-header-date h1))
3683    (gnus-date-get-time (mail-header-date h2))))
3684
3685 (defun gnus-thread-sort-by-date (h1 h2)
3686   "Sort threads by root article date."
3687   (gnus-article-sort-by-date
3688    (gnus-thread-header h1) (gnus-thread-header h2)))
3689
3690 (defsubst gnus-article-sort-by-score (h1 h2)
3691   "Sort articles by root article score.
3692 Unscored articles will be counted as having a score of zero."
3693   (> (or (cdr (assq (mail-header-number h1)
3694                     gnus-newsgroup-scored))
3695          gnus-summary-default-score 0)
3696      (or (cdr (assq (mail-header-number h2)
3697                     gnus-newsgroup-scored))
3698          gnus-summary-default-score 0)))
3699
3700 (defun gnus-thread-sort-by-score (h1 h2)
3701   "Sort threads by root article score."
3702   (gnus-article-sort-by-score
3703    (gnus-thread-header h1) (gnus-thread-header h2)))
3704
3705 (defun gnus-thread-sort-by-total-score (h1 h2)
3706   "Sort threads by the sum of all scores in the thread.
3707 Unscored articles will be counted as having a score of zero."
3708   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3709
3710 (defun gnus-thread-total-score (thread)
3711   ;; This function find the total score of THREAD.
3712   (cond ((null thread)
3713          0)
3714         ((consp thread)
3715          (if (stringp (car thread))
3716              (apply gnus-thread-score-function 0
3717                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3718            (gnus-thread-total-score-1 thread)))
3719         (t
3720          (gnus-thread-total-score-1 (list thread)))))
3721
3722 (defun gnus-thread-total-score-1 (root)
3723   ;; This function find the total score of the thread below ROOT.
3724   (setq root (car root))
3725   (apply gnus-thread-score-function
3726          (or (append
3727               (mapcar 'gnus-thread-total-score
3728                       (cdr (gnus-id-to-thread (mail-header-id root))))
3729               (when (> (mail-header-number root) 0)
3730                 (list (or (cdr (assq (mail-header-number root)
3731                                      gnus-newsgroup-scored))
3732                           gnus-summary-default-score 0))))
3733              (list gnus-summary-default-score)
3734              '(0))))
3735
3736 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3737 (defvar gnus-tmp-prev-subject nil)
3738 (defvar gnus-tmp-false-parent nil)
3739 (defvar gnus-tmp-root-expunged nil)
3740 (defvar gnus-tmp-dummy-line nil)
3741
3742 (defvar gnus-tmp-header)
3743 (defun gnus-extra-header (type &optional header)
3744   "Return the extra header of TYPE."
3745   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3746       ""))
3747
3748 (defun gnus-summary-prepare-threads (threads)
3749   "Prepare summary buffer from THREADS and indentation LEVEL.
3750 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3751 or a straight list of headers."
3752   (gnus-message 7 "Generating summary...")
3753
3754   (setq gnus-newsgroup-threads threads)
3755   (beginning-of-line)
3756
3757   (let ((gnus-tmp-level 0)
3758         (default-score (or gnus-summary-default-score 0))
3759         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3760         thread number subject stack state gnus-tmp-gathered beg-match
3761         new-roots gnus-tmp-new-adopts thread-end
3762         gnus-tmp-header gnus-tmp-unread
3763         gnus-tmp-replied gnus-tmp-subject-or-nil
3764         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3765         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3766         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3767
3768     (setq gnus-tmp-prev-subject nil)
3769
3770     (if (vectorp (car threads))
3771         ;; If this is a straight (sic) list of headers, then a
3772         ;; threaded summary display isn't required, so we just create
3773         ;; an unthreaded one.
3774         (gnus-summary-prepare-unthreaded threads)
3775
3776       ;; Do the threaded display.
3777
3778       (while (or threads stack gnus-tmp-new-adopts new-roots)
3779
3780         (if (and (= gnus-tmp-level 0)
3781                  (or (not stack)
3782                      (= (caar stack) 0))
3783                  (not gnus-tmp-false-parent)
3784                  (or gnus-tmp-new-adopts new-roots))
3785             (if gnus-tmp-new-adopts
3786                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3787                       thread (list (car gnus-tmp-new-adopts))
3788                       gnus-tmp-header (caar thread)
3789                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3790               (when new-roots
3791                 (setq thread (list (car new-roots))
3792                       gnus-tmp-header (caar thread)
3793                       new-roots (cdr new-roots))))
3794
3795           (if threads
3796               ;; If there are some threads, we do them before the
3797               ;; threads on the stack.
3798               (setq thread threads
3799                     gnus-tmp-header (caar thread))
3800             ;; There were no current threads, so we pop something off
3801             ;; the stack.
3802             (setq state (car stack)
3803                   gnus-tmp-level (car state)
3804                   thread (cdr state)
3805                   stack (cdr stack)
3806                   gnus-tmp-header (caar thread))))
3807
3808         (setq gnus-tmp-false-parent nil)
3809         (setq gnus-tmp-root-expunged nil)
3810         (setq thread-end nil)
3811
3812         (if (stringp gnus-tmp-header)
3813             ;; The header is a dummy root.
3814             (cond
3815              ((eq gnus-summary-make-false-root 'adopt)
3816               ;; We let the first article adopt the rest.
3817               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3818                                                (cddar thread)))
3819               (setq gnus-tmp-gathered
3820                     (nconc (mapcar
3821                             (lambda (h) (mail-header-number (car h)))
3822                             (cddar thread))
3823                            gnus-tmp-gathered))
3824               (setq thread (cons (list (caar thread)
3825                                        (cadar thread))
3826                                  (cdr thread)))
3827               (setq gnus-tmp-level -1
3828                     gnus-tmp-false-parent t))
3829              ((eq gnus-summary-make-false-root 'empty)
3830               ;; We print adopted articles with empty subject fields.
3831               (setq gnus-tmp-gathered
3832                     (nconc (mapcar
3833                             (lambda (h) (mail-header-number (car h)))
3834                             (cddar thread))
3835                            gnus-tmp-gathered))
3836               (setq gnus-tmp-level -1))
3837              ((eq gnus-summary-make-false-root 'dummy)
3838               ;; We remember that we probably want to output a dummy
3839               ;; root.
3840               (setq gnus-tmp-dummy-line gnus-tmp-header)
3841               (setq gnus-tmp-prev-subject gnus-tmp-header))
3842              (t
3843               ;; We do not make a root for the gathered
3844               ;; sub-threads at all.
3845               (setq gnus-tmp-level -1)))
3846
3847           (setq number (mail-header-number gnus-tmp-header)
3848                 subject (mail-header-subject gnus-tmp-header))
3849
3850           (cond
3851            ;; If the thread has changed subject, we might want to make
3852            ;; this subthread into a root.
3853            ((and (null gnus-thread-ignore-subject)
3854                  (not (zerop gnus-tmp-level))
3855                  gnus-tmp-prev-subject
3856                  (not (inline
3857                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3858             (setq new-roots (nconc new-roots (list (car thread)))
3859                   thread-end t
3860                   gnus-tmp-header nil))
3861            ;; If the article lies outside the current limit,
3862            ;; then we do not display it.
3863            ((not (memq number gnus-newsgroup-limit))
3864             (setq gnus-tmp-gathered
3865                   (nconc (mapcar
3866                           (lambda (h) (mail-header-number (car h)))
3867                           (cdar thread))
3868                          gnus-tmp-gathered))
3869             (setq gnus-tmp-new-adopts (if (cdar thread)
3870                                           (append gnus-tmp-new-adopts
3871                                                   (cdar thread))
3872                                         gnus-tmp-new-adopts)
3873                   thread-end t
3874                   gnus-tmp-header nil)
3875             (when (zerop gnus-tmp-level)
3876               (setq gnus-tmp-root-expunged t)))
3877            ;; Perhaps this article is to be marked as read?
3878            ((and gnus-summary-mark-below
3879                  (< (or (cdr (assq number gnus-newsgroup-scored))
3880                         default-score)
3881                     gnus-summary-mark-below)
3882                  ;; Don't touch sparse articles.
3883                  (not (gnus-summary-article-sparse-p number))
3884                  (not (gnus-summary-article-ancient-p number)))
3885             (setq gnus-newsgroup-unreads
3886                   (delq number gnus-newsgroup-unreads))
3887             (if gnus-newsgroup-auto-expire
3888                 (push number gnus-newsgroup-expirable)
3889               (push (cons number gnus-low-score-mark)
3890                     gnus-newsgroup-reads))))
3891
3892           (when gnus-tmp-header
3893             ;; We may have an old dummy line to output before this
3894             ;; article.
3895             (when (and gnus-tmp-dummy-line
3896                        (gnus-subject-equal
3897                         gnus-tmp-dummy-line
3898                         (mail-header-subject gnus-tmp-header)))
3899               (gnus-summary-insert-dummy-line
3900                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3901               (setq gnus-tmp-dummy-line nil))
3902
3903             ;; Compute the mark.
3904             (setq gnus-tmp-unread (gnus-article-mark number))
3905
3906             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3907                                   gnus-tmp-header gnus-tmp-level)
3908                   gnus-newsgroup-data)
3909
3910             ;; Actually insert the line.
3911             (setq
3912              gnus-tmp-subject-or-nil
3913              (cond
3914               ((and gnus-thread-ignore-subject
3915                     gnus-tmp-prev-subject
3916                     (not (inline (gnus-subject-equal
3917                                   gnus-tmp-prev-subject subject))))
3918                subject)
3919               ((zerop gnus-tmp-level)
3920                (if (and (eq gnus-summary-make-false-root 'empty)
3921                         (memq number gnus-tmp-gathered)
3922                         gnus-tmp-prev-subject
3923                         (inline (gnus-subject-equal
3924                                  gnus-tmp-prev-subject subject)))
3925                    gnus-summary-same-subject
3926                  subject))
3927               (t gnus-summary-same-subject)))
3928             (if (and (eq gnus-summary-make-false-root 'adopt)
3929                      (= gnus-tmp-level 1)
3930                      (memq number gnus-tmp-gathered))
3931                 (setq gnus-tmp-opening-bracket ?\<
3932                       gnus-tmp-closing-bracket ?\>)
3933               (setq gnus-tmp-opening-bracket ?\[
3934                     gnus-tmp-closing-bracket ?\]))
3935             (setq
3936              gnus-tmp-indentation
3937              (aref gnus-thread-indent-array gnus-tmp-level)
3938              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3939              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3940                                 gnus-summary-default-score 0)
3941              gnus-tmp-score-char
3942              (if (or (null gnus-summary-default-score)
3943                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3944                          gnus-summary-zcore-fuzz))
3945                  ? ;Whitespace
3946                (if (< gnus-tmp-score gnus-summary-default-score)
3947                    gnus-score-below-mark gnus-score-over-mark))
3948              gnus-tmp-replied
3949              (cond ((memq number gnus-newsgroup-processable)
3950                     gnus-process-mark)
3951                    ((memq number gnus-newsgroup-cached)
3952                     gnus-cached-mark)
3953                    ((memq number gnus-newsgroup-replied)
3954                     gnus-replied-mark)
3955                    ((memq number gnus-newsgroup-saved)
3956                     gnus-saved-mark)
3957                    (t gnus-unread-mark))
3958              gnus-tmp-from (mail-header-from gnus-tmp-header)
3959              gnus-tmp-name
3960              (cond
3961               ((string-match "<[^>]+> *$" gnus-tmp-from)
3962                (setq beg-match (match-beginning 0))
3963                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3964                         (substring gnus-tmp-from (1+ (match-beginning 0))
3965                                    (1- (match-end 0))))
3966                    (substring gnus-tmp-from 0 beg-match)))
3967               ((string-match "(.+)" gnus-tmp-from)
3968                (substring gnus-tmp-from
3969                           (1+ (match-beginning 0)) (1- (match-end 0))))
3970               (t gnus-tmp-from)))
3971             (when (string= gnus-tmp-name "")
3972               (setq gnus-tmp-name gnus-tmp-from))
3973             (unless (numberp gnus-tmp-lines)
3974               (setq gnus-tmp-lines 0))
3975             (gnus-put-text-property
3976              (point)
3977              (progn (eval gnus-summary-line-format-spec) (point))
3978              'gnus-number number)
3979             (when gnus-visual-p
3980               (forward-line -1)
3981               (gnus-run-hooks 'gnus-summary-update-hook)
3982               (forward-line 1))
3983
3984             (setq gnus-tmp-prev-subject subject)))
3985
3986         (when (nth 1 thread)
3987           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3988         (incf gnus-tmp-level)
3989         (setq threads (if thread-end nil (cdar thread)))
3990         (unless threads
3991           (setq gnus-tmp-level 0)))))
3992   (gnus-message 7 "Generating summary...done"))
3993
3994 (defun gnus-summary-prepare-unthreaded (headers)
3995   "Generate an unthreaded summary buffer based on HEADERS."
3996   (let (header number mark)
3997
3998     (beginning-of-line)
3999
4000     (while headers
4001       ;; We may have to root out some bad articles...
4002       (when (memq (setq number (mail-header-number
4003                                 (setq header (pop headers))))
4004                   gnus-newsgroup-limit)
4005         ;; Mark article as read when it has a low score.
4006         (when (and gnus-summary-mark-below
4007                    (< (or (cdr (assq number gnus-newsgroup-scored))
4008                           gnus-summary-default-score 0)
4009                       gnus-summary-mark-below)
4010                    (not (gnus-summary-article-ancient-p number)))
4011           (setq gnus-newsgroup-unreads
4012                 (delq number gnus-newsgroup-unreads))
4013           (if gnus-newsgroup-auto-expire
4014               (push number gnus-newsgroup-expirable)
4015             (push (cons number gnus-low-score-mark)
4016                   gnus-newsgroup-reads)))
4017
4018         (setq mark (gnus-article-mark number))
4019         (push (gnus-data-make number mark (1+ (point)) header 0)
4020               gnus-newsgroup-data)
4021         (gnus-summary-insert-line
4022          header 0 number
4023          mark (memq number gnus-newsgroup-replied)
4024          (memq number gnus-newsgroup-expirable)
4025          (mail-header-subject header) nil
4026          (cdr (assq number gnus-newsgroup-scored))
4027          (memq number gnus-newsgroup-processable))))))
4028
4029 (defun gnus-summary-remove-list-identifiers ()
4030   "Remove list identifiers in `gnus-list-identifiers' from articles in
4031 the current group."
4032   (let ((regexp (if (stringp gnus-list-identifiers)
4033                     gnus-list-identifiers
4034                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4035     (when regexp
4036       (dolist (header gnus-newsgroup-headers)
4037         (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4038                             (mail-header-subject header))
4039           (mail-header-set-subject
4040            header (concat (substring (mail-header-subject header)
4041                                      0 (match-beginning 2))
4042                           (substring (mail-header-subject header)
4043                                      (match-end 2)))))))))
4044
4045 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4046   "Select newsgroup GROUP.
4047 If READ-ALL is non-nil, all articles in the group are selected.
4048 If SELECT-ARTICLES, only select those articles from GROUP."
4049   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4050          ;;!!! Dirty hack; should be removed.
4051          (gnus-summary-ignore-duplicates
4052           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4053               t
4054             gnus-summary-ignore-duplicates))
4055          (info (nth 2 entry))
4056          articles fetched-articles cached)
4057
4058     (unless (gnus-check-server
4059              (setq gnus-current-select-method
4060                    (gnus-find-method-for-group group)))
4061       (error "Couldn't open server"))
4062
4063     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4064         (gnus-activate-group group)     ; Or we can activate it...
4065         (progn                          ; Or we bug out.
4066           (when (equal major-mode 'gnus-summary-mode)
4067             (kill-buffer (current-buffer)))
4068           (error "Couldn't request group %s: %s"
4069                  group (gnus-status-message group))))
4070
4071     (unless (gnus-request-group group t)
4072       (when (equal major-mode 'gnus-summary-mode)
4073         (kill-buffer (current-buffer)))
4074       (error "Couldn't request group %s: %s"
4075              group (gnus-status-message group)))
4076
4077     (setq gnus-newsgroup-name group)
4078     (setq gnus-newsgroup-unselected nil)
4079     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4080     (gnus-summary-setup-default-charset)
4081
4082     ;; Adjust and set lists of article marks.
4083     (when info
4084       (gnus-adjust-marked-articles info))
4085
4086     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4087     (when (gnus-virtual-group-p group)
4088       (setq cached gnus-newsgroup-cached))
4089
4090     (setq gnus-newsgroup-unreads
4091           (gnus-set-difference
4092            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4093            gnus-newsgroup-dormant))
4094
4095     (setq gnus-newsgroup-processable nil)
4096
4097     (gnus-update-read-articles group gnus-newsgroup-unreads)
4098
4099     (if (setq articles select-articles)
4100         (setq gnus-newsgroup-unselected
4101               (gnus-sorted-intersection
4102                gnus-newsgroup-unreads
4103                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4104       (setq articles (gnus-articles-to-read group read-all)))
4105
4106     (cond
4107      ((null articles)
4108       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4109       'quit)
4110      ((eq articles 0) nil)
4111      (t
4112       ;; Init the dependencies hash table.
4113       (setq gnus-newsgroup-dependencies
4114             (gnus-make-hashtable (length articles)))
4115       (gnus-set-global-variables)
4116       ;; Retrieve the headers and read them in.
4117       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4118       (setq gnus-newsgroup-headers
4119             (if (eq 'nov
4120                     (setq gnus-headers-retrieved-by
4121                           (gnus-retrieve-headers
4122                            articles gnus-newsgroup-name
4123                            ;; We might want to fetch old headers, but
4124                            ;; not if there is only 1 article.
4125                            (and (or (and
4126                                      (not (eq gnus-fetch-old-headers 'some))
4127                                      (not (numberp gnus-fetch-old-headers)))
4128                                     (> (length articles) 1))
4129                                 gnus-fetch-old-headers))))
4130                 (gnus-get-newsgroup-headers-xover
4131                  articles nil nil gnus-newsgroup-name t)
4132               (gnus-get-newsgroup-headers)))
4133       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4134
4135       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4136       (when cached
4137         (setq gnus-newsgroup-cached cached))
4138
4139       ;; Suppress duplicates?
4140       (when gnus-suppress-duplicates
4141         (gnus-dup-suppress-articles))
4142
4143       ;; Set the initial limit.
4144       (setq gnus-newsgroup-limit (copy-sequence articles))
4145       ;; Remove canceled articles from the list of unread articles.
4146       (setq gnus-newsgroup-unreads
4147             (gnus-set-sorted-intersection
4148              gnus-newsgroup-unreads
4149              (setq fetched-articles
4150                    (mapcar (lambda (headers) (mail-header-number headers))
4151                            gnus-newsgroup-headers))))
4152       ;; Removed marked articles that do not exist.
4153       (gnus-update-missing-marks
4154        (gnus-sorted-complement fetched-articles articles))
4155       ;; We might want to build some more threads first.
4156       (when (and gnus-fetch-old-headers
4157                  (eq gnus-headers-retrieved-by 'nov))
4158         (if (eq gnus-fetch-old-headers 'invisible)
4159             (gnus-build-all-threads)
4160           (gnus-build-old-threads)))
4161       ;; Let the Gnus agent mark articles as read.
4162       (when gnus-agent
4163         (gnus-agent-get-undownloaded-list))
4164       ;; Remove list identifiers from subject
4165       (when gnus-list-identifiers
4166         (gnus-summary-remove-list-identifiers))
4167       ;; Check whether auto-expire is to be done in this group.
4168       (setq gnus-newsgroup-auto-expire
4169             (gnus-group-auto-expirable-p group))
4170       ;; Set up the article buffer now, if necessary.
4171       (unless gnus-single-article-buffer
4172         (gnus-article-setup-buffer))
4173       ;; First and last article in this newsgroup.
4174       (when gnus-newsgroup-headers
4175         (setq gnus-newsgroup-begin
4176               (mail-header-number (car gnus-newsgroup-headers))
4177               gnus-newsgroup-end
4178               (mail-header-number
4179                (gnus-last-element gnus-newsgroup-headers))))
4180       ;; GROUP is successfully selected.
4181       (or gnus-newsgroup-headers t)))))
4182
4183 (defun gnus-articles-to-read (group &optional read-all)
4184   ;; Find out what articles the user wants to read.
4185   (let* ((articles
4186           ;; Select all articles if `read-all' is non-nil, or if there
4187           ;; are no unread articles.
4188           (if (or read-all
4189                   (and (zerop (length gnus-newsgroup-marked))
4190                        (zerop (length gnus-newsgroup-unreads)))
4191                   (eq (gnus-group-find-parameter group 'display)
4192                       'all))
4193               (or
4194                (gnus-uncompress-range (gnus-active group))
4195                (gnus-cache-articles-in-group group))
4196             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4197                           (copy-sequence gnus-newsgroup-unreads))
4198                   '<)))
4199          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4200          (scored (length scored-list))
4201          (number (length articles))
4202          (marked (+ (length gnus-newsgroup-marked)
4203                     (length gnus-newsgroup-dormant)))
4204          (select
4205           (cond
4206            ((numberp read-all)
4207             read-all)
4208            (t
4209             (condition-case ()
4210                 (cond
4211                  ((and (or (<= scored marked) (= scored number))
4212                        (numberp gnus-large-newsgroup)
4213                        (> number gnus-large-newsgroup))
4214                   (let ((input
4215                          (read-string
4216                           (format
4217                            "How many articles from %s (default %d): "
4218                            (gnus-limit-string gnus-newsgroup-name 35)
4219                            number))))
4220                     (if (string-match "^[ \t]*$" input) number input)))
4221                  ((and (> scored marked) (< scored number)
4222                        (> (- scored number) 20))
4223                   (let ((input
4224                          (read-string
4225                           (format "%s %s (%d scored, %d total): "
4226                                   "How many articles from"
4227                                   group scored number))))
4228                     (if (string-match "^[ \t]*$" input)
4229                         number input)))
4230                  (t number))
4231               (quit nil))))))
4232     (setq select (if (stringp select) (string-to-number select) select))
4233     (if (or (null select) (zerop select))
4234         select
4235       (if (and (not (zerop scored)) (<= (abs select) scored))
4236           (progn
4237             (setq articles (sort scored-list '<))
4238             (setq number (length articles)))
4239         (setq articles (copy-sequence articles)))
4240
4241       (when (< (abs select) number)
4242         (if (< select 0)
4243             ;; Select the N oldest articles.
4244             (setcdr (nthcdr (1- (abs select)) articles) nil)
4245           ;; Select the N most recent articles.
4246           (setq articles (nthcdr (- number select) articles))))
4247       (setq gnus-newsgroup-unselected
4248             (gnus-sorted-intersection
4249              gnus-newsgroup-unreads
4250              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4251       articles)))
4252
4253 (defun gnus-killed-articles (killed articles)
4254   (let (out)
4255     (while articles
4256       (when (inline (gnus-member-of-range (car articles) killed))
4257         (push (car articles) out))
4258       (setq articles (cdr articles)))
4259     out))
4260
4261 (defun gnus-uncompress-marks (marks)
4262   "Uncompress the mark ranges in MARKS."
4263   (let ((uncompressed '(score bookmark))
4264         out)
4265     (while marks
4266       (if (memq (caar marks) uncompressed)
4267           (push (car marks) out)
4268         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4269       (setq marks (cdr marks)))
4270     out))
4271
4272 (defun gnus-adjust-marked-articles (info)
4273   "Set all article lists and remove all marks that are no longer valid."
4274   (let* ((marked-lists (gnus-info-marks info))
4275          (active (gnus-active (gnus-info-group info)))
4276          (min (car active))
4277          (max (cdr active))
4278          (types gnus-article-mark-lists)
4279          (uncompressed '(score bookmark killed))
4280          marks var articles article mark)
4281
4282     (while marked-lists
4283       (setq marks (pop marked-lists))
4284       (set (setq var (intern (format "gnus-newsgroup-%s"
4285                                      (car (rassq (setq mark (car marks))
4286                                                  types)))))
4287            (if (memq (car marks) uncompressed) (cdr marks)
4288              (gnus-uncompress-range (cdr marks))))
4289
4290       (setq articles (symbol-value var))
4291
4292       ;; All articles have to be subsets of the active articles.
4293       (cond
4294        ;; Adjust "simple" lists.
4295        ((memq mark '(tick dormant expire reply save))
4296         (while articles
4297           (when (or (< (setq article (pop articles)) min) (> article max))
4298             (set var (delq article (symbol-value var))))))
4299        ;; Adjust assocs.
4300        ((memq mark uncompressed)
4301         (when (not (listp (cdr (symbol-value var))))
4302           (set var (list (symbol-value var))))
4303         (when (not (listp (cdr articles)))
4304           (setq articles (list articles)))
4305         (while articles
4306           (when (or (not (consp (setq article (pop articles))))
4307                     (< (car article) min)
4308                     (> (car article) max))
4309             (set var (delq article (symbol-value var))))))))))
4310
4311 (defun gnus-update-missing-marks (missing)
4312   "Go through the list of MISSING articles and remove them from the mark lists."
4313   (when missing
4314     (let ((types gnus-article-mark-lists)
4315           var m)
4316       ;; Go through all types.
4317       (while types
4318         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4319         (when (symbol-value var)
4320           ;; This list has articles.  So we delete all missing articles
4321           ;; from it.
4322           (setq m missing)
4323           (while m
4324             (set var (delq (pop m) (symbol-value var)))))))))
4325
4326 (defun gnus-update-marks ()
4327   "Enter the various lists of marked articles into the newsgroup info list."
4328   (let ((types gnus-article-mark-lists)
4329         (info (gnus-get-info gnus-newsgroup-name))
4330         (uncompressed '(score bookmark killed))
4331         type list newmarked symbol delta-marks)
4332     (when info
4333       ;; Add all marks lists to the list of marks lists.
4334       (while (setq type (pop types))
4335         (setq list (symbol-value
4336                           (setq symbol
4337                                 (intern (format "gnus-newsgroup-%s"
4338                                                 (car type))))))
4339
4340         (when list
4341           ;; Get rid of the entries of the articles that have the
4342           ;; default score.
4343           (when (and (eq (cdr type) 'score)
4344                      gnus-save-score
4345                      list)
4346             (let* ((arts list)
4347                    (prev (cons nil list))
4348                    (all prev))
4349               (while arts
4350                 (if (or (not (consp (car arts)))
4351                         (= (cdar arts) gnus-summary-default-score))
4352                     (setcdr prev (cdr arts))
4353                   (setq prev arts))
4354                 (setq arts (cdr arts)))
4355               (setq list (cdr all)))))
4356
4357        (or (memq (cdr type) uncompressed)
4358            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4359        
4360        (when (gnus-check-backend-function 'request-set-mark
4361                                           gnus-newsgroup-name)
4362          ;; uncompressed:s are not proper flags (they are cons cells)
4363          ;; cache is a internal gnus flag
4364          (unless (memq (cdr type) (cons 'cache uncompressed))
4365            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4366                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4367                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4368              (if add
4369                  (push (list add 'add (list (cdr type))) delta-marks))
4370              (if del
4371                  (push (list del 'del (list (cdr type))) delta-marks)))))
4372           
4373         (when list
4374          (push (cons (cdr type) list) newmarked)))
4375
4376       (when delta-marks
4377         (unless (gnus-check-group gnus-newsgroup-name)
4378           (error "Can't open server for %s" gnus-newsgroup-name))
4379         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4380           
4381       ;; Enter these new marks into the info of the group.
4382       (if (nthcdr 3 info)
4383           (setcar (nthcdr 3 info) newmarked)
4384         ;; Add the marks lists to the end of the info.
4385         (when newmarked
4386           (setcdr (nthcdr 2 info) (list newmarked))))
4387
4388       ;; Cut off the end of the info if there's nothing else there.
4389       (let ((i 5))
4390         (while (and (> i 2)
4391                     (not (nth i info)))
4392           (when (nthcdr (decf i) info)
4393             (setcdr (nthcdr i info) nil)))))))
4394
4395 (defun gnus-set-mode-line (where)
4396   "This function sets the mode line of the article or summary buffers.
4397 If WHERE is `summary', the summary mode line format will be used."
4398   ;; Is this mode line one we keep updated?
4399   (when (and (memq where gnus-updated-mode-lines)
4400              (symbol-value
4401               (intern (format "gnus-%s-mode-line-format-spec" where))))
4402     (let (mode-string)
4403       (save-excursion
4404         ;; We evaluate this in the summary buffer since these
4405         ;; variables are buffer-local to that buffer.
4406         (set-buffer gnus-summary-buffer)
4407         ;; We bind all these variables that are used in the `eval' form
4408         ;; below.
4409         (let* ((mformat (symbol-value
4410                          (intern
4411                           (format "gnus-%s-mode-line-format-spec" where))))
4412                (gnus-tmp-group-name gnus-newsgroup-name)
4413                (gnus-tmp-article-number (or gnus-current-article 0))
4414                (gnus-tmp-unread gnus-newsgroup-unreads)
4415                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4416                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4417                (gnus-tmp-unread-and-unselected
4418                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4419                             (zerop gnus-tmp-unselected))
4420                        "")
4421                       ((zerop gnus-tmp-unselected)
4422                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4423                       (t (format "{%d(+%d) more}"
4424                                  gnus-tmp-unread-and-unticked
4425                                  gnus-tmp-unselected))))
4426                (gnus-tmp-subject
4427                 (if (and gnus-current-headers
4428                          (vectorp gnus-current-headers))
4429                     (gnus-mode-string-quote
4430                      (mail-header-subject gnus-current-headers))
4431                   ""))
4432                bufname-length max-len
4433                gnus-tmp-header);; passed as argument to any user-format-funcs
4434           (setq mode-string (eval mformat))
4435           (setq bufname-length (if (string-match "%b" mode-string)
4436                                    (- (length
4437                                        (buffer-name
4438                                         (if (eq where 'summary)
4439                                             nil
4440                                           (get-buffer gnus-article-buffer))))
4441                                       2)
4442                                  0))
4443           (setq max-len (max 4 (if gnus-mode-non-string-length
4444                                    (- (window-width)
4445                                       gnus-mode-non-string-length
4446                                       bufname-length)
4447                                  (length mode-string))))
4448           ;; We might have to chop a bit of the string off...
4449           (when (> (length mode-string) max-len)
4450             (setq mode-string
4451                   (concat (truncate-string-to-width mode-string (- max-len 3))
4452                           "...")))
4453           ;; Pad the mode string a bit.
4454           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4455       ;; Update the mode line.
4456       (setq mode-line-buffer-identification
4457             (gnus-mode-line-buffer-identification (list mode-string)))
4458       (set-buffer-modified-p t))))
4459
4460 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4461   "Go through the HEADERS list and add all Xrefs to a hash table.
4462 The resulting hash table is returned, or nil if no Xrefs were found."
4463   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4464          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4465          (xref-hashtb (gnus-make-hashtable))
4466          start group entry number xrefs header)
4467     (while headers
4468       (setq header (pop headers))
4469       (when (and (setq xrefs (mail-header-xref header))
4470                  (not (memq (setq number (mail-header-number header))
4471                             unreads)))
4472         (setq start 0)
4473         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4474           (setq start (match-end 0))
4475           (setq group (if prefix
4476                           (concat prefix (substring xrefs (match-beginning 1)
4477                                                     (match-end 1)))
4478                         (substring xrefs (match-beginning 1) (match-end 1))))
4479           (setq number
4480                 (string-to-int (substring xrefs (match-beginning 2)
4481                                           (match-end 2))))
4482           (if (setq entry (gnus-gethash group xref-hashtb))
4483               (setcdr entry (cons number (cdr entry)))
4484             (gnus-sethash group (cons number nil) xref-hashtb)))))
4485     (and start xref-hashtb)))
4486
4487 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4488   "Look through all the headers and mark the Xrefs as read."
4489   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4490         name entry info xref-hashtb idlist method nth4)
4491     (save-excursion
4492       (set-buffer gnus-group-buffer)
4493       (when (setq xref-hashtb
4494                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4495         (mapatoms
4496          (lambda (group)
4497            (unless (string= from-newsgroup (setq name (symbol-name group)))
4498              (setq idlist (symbol-value group))
4499              ;; Dead groups are not updated.
4500              (and (prog1
4501                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4502                             info (nth 2 entry))
4503                     (when (stringp (setq nth4 (gnus-info-method info)))
4504                       (setq nth4 (gnus-server-to-method nth4))))
4505                   ;; Only do the xrefs if the group has the same
4506                   ;; select method as the group we have just read.
4507                   (or (gnus-methods-equal-p
4508                        nth4 (gnus-find-method-for-group from-newsgroup))
4509                       virtual
4510                       (equal nth4 (setq method (gnus-find-method-for-group
4511                                                 from-newsgroup)))
4512                       (and (equal (car nth4) (car method))
4513                            (equal (nth 1 nth4) (nth 1 method))))
4514                   gnus-use-cross-reference
4515                   (or (not (eq gnus-use-cross-reference t))
4516                       virtual
4517                       ;; Only do cross-references on subscribed
4518                       ;; groups, if that is what is wanted.
4519                       (<= (gnus-info-level info) gnus-level-subscribed))
4520                   (gnus-group-make-articles-read name idlist))))
4521          xref-hashtb)))))
4522
4523 (defun gnus-compute-read-articles (group articles)
4524   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4525          (info (nth 2 entry))
4526          (active (gnus-active group))
4527          ninfo)
4528     (when entry
4529       ;; First peel off all invalid article numbers.
4530       (when active
4531         (let ((ids articles)
4532               id first)
4533           (while (setq id (pop ids))
4534             (when (and first (> id (cdr active)))
4535               ;; We'll end up in this situation in one particular
4536               ;; obscure situation.  If you re-scan a group and get
4537               ;; a new article that is cross-posted to a different
4538               ;; group that has not been re-scanned, you might get
4539               ;; crossposted article that has a higher number than
4540               ;; Gnus believes possible.  So we re-activate this
4541               ;; group as well.  This might mean doing the
4542               ;; crossposting thingy will *increase* the number
4543               ;; of articles in some groups.  Tsk, tsk.
4544               (setq active (or (gnus-activate-group group) active)))
4545             (when (or (> id (cdr active))
4546                       (< id (car active)))
4547               (setq articles (delq id articles))))))
4548       ;; If the read list is nil, we init it.
4549       (if (and active
4550                (null (gnus-info-read info))
4551                (> (car active) 1))
4552           (setq ninfo (cons 1 (1- (car active))))
4553         (setq ninfo (gnus-info-read info)))
4554       ;; Then we add the read articles to the range.
4555       (gnus-add-to-range
4556        ninfo (setq articles (sort articles '<))))))
4557
4558 (defun gnus-group-make-articles-read (group articles)
4559   "Update the info of GROUP to say that ARTICLES are read."
4560   (let* ((num 0)
4561          (entry (gnus-gethash group gnus-newsrc-hashtb))
4562          (info (nth 2 entry))
4563          (active (gnus-active group))
4564          range)
4565     (when entry
4566       (setq range (gnus-compute-read-articles group articles))
4567       (save-excursion
4568         (set-buffer gnus-group-buffer)
4569         (gnus-undo-register
4570           `(progn
4571              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4572              (gnus-info-set-read ',info ',(gnus-info-read info))
4573              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4574              (gnus-group-update-group ,group t))))
4575       ;; Add the read articles to the range.
4576       (gnus-info-set-read info range)
4577       ;; Then we have to re-compute how many unread
4578       ;; articles there are in this group.
4579       (when active
4580         (cond
4581          ((not range)
4582           (setq num (- (1+ (cdr active)) (car active))))
4583          ((not (listp (cdr range)))
4584           (setq num (- (cdr active) (- (1+ (cdr range))
4585                                        (car range)))))
4586          (t
4587           (while range
4588             (if (numberp (car range))
4589                 (setq num (1+ num))
4590               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4591             (setq range (cdr range)))
4592           (setq num (- (cdr active) num))))
4593         ;; Update the number of unread articles.
4594         (setcar entry num)
4595         ;; Update the group buffer.
4596         (gnus-group-update-group group t)))))
4597
4598 (defvar gnus-newsgroup-none-id 0)
4599
4600 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4601   (let ((cur nntp-server-buffer)
4602         (dependencies
4603          (or dependencies
4604              (save-excursion (set-buffer gnus-summary-buffer)
4605                              gnus-newsgroup-dependencies)))
4606         headers id end ref
4607         (mail-parse-charset gnus-newsgroup-charset)
4608         (mail-parse-ignored-charsets 
4609              (save-excursion (condition-case nil
4610                                  (set-buffer gnus-summary-buffer)
4611                                (error))
4612                              gnus-newsgroup-ignored-charsets)))
4613     (save-excursion
4614       (set-buffer nntp-server-buffer)
4615       ;; Translate all TAB characters into SPACE characters.
4616       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4617       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4618       (gnus-run-hooks 'gnus-parse-headers-hook)
4619       (let ((case-fold-search t)
4620             in-reply-to header p lines chars)
4621         (goto-char (point-min))
4622         ;; Search to the beginning of the next header.  Error messages
4623         ;; do not begin with 2 or 3.
4624         (while (re-search-forward "^[23][0-9]+ " nil t)
4625           (setq id nil
4626                 ref nil)
4627           ;; This implementation of this function, with nine
4628           ;; search-forwards instead of the one re-search-forward and
4629           ;; a case (which basically was the old function) is actually
4630           ;; about twice as fast, even though it looks messier.  You
4631           ;; can't have everything, I guess.  Speed and elegance
4632           ;; doesn't always go hand in hand.
4633           (setq
4634            header
4635            (vector
4636             ;; Number.
4637             (prog1
4638                 (read cur)
4639               (end-of-line)
4640               (setq p (point))
4641               (narrow-to-region (point)
4642                                 (or (and (search-forward "\n.\n" nil t)
4643                                          (- (point) 2))
4644                                     (point))))
4645             ;; Subject.
4646             (progn
4647               (goto-char p)
4648               (if (search-forward "\nsubject: " nil t)
4649                   (funcall gnus-decode-encoded-word-function
4650                            (nnheader-header-value))
4651                 "(none)"))
4652             ;; From.
4653             (progn
4654               (goto-char p)
4655               (if (search-forward "\nfrom: " nil t)
4656                   (funcall gnus-decode-encoded-word-function
4657                            (nnheader-header-value))
4658                 "(nobody)"))
4659             ;; Date.
4660             (progn
4661               (goto-char p)
4662               (if (search-forward "\ndate: " nil t)
4663                   (nnheader-header-value) ""))
4664             ;; Message-ID.
4665             (progn
4666               (goto-char p)
4667               (setq id (if (re-search-forward
4668                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4669                            ;; We do it this way to make sure the Message-ID
4670                            ;; is (somewhat) syntactically valid.
4671                            (buffer-substring (match-beginning 1)
4672                                              (match-end 1))
4673                          ;; If there was no message-id, we just fake one
4674                          ;; to make subsequent routines simpler.
4675                          (nnheader-generate-fake-message-id))))
4676             ;; References.
4677             (progn
4678               (goto-char p)
4679               (if (search-forward "\nreferences: " nil t)
4680                   (progn
4681                     (setq end (point))
4682                     (prog1
4683                         (nnheader-header-value)
4684                       (setq ref
4685                             (buffer-substring
4686                              (progn
4687                                (end-of-line)
4688                                (search-backward ">" end t)
4689                                (1+ (point)))
4690                              (progn
4691                                (search-backward "<" end t)
4692                                (point))))))
4693                 ;; Get the references from the in-reply-to header if there
4694                 ;; were no references and the in-reply-to header looks
4695                 ;; promising.
4696                 (if (and (search-forward "\nin-reply-to: " nil t)
4697                          (setq in-reply-to (nnheader-header-value))
4698                          (string-match "<[^>]+>" in-reply-to))
4699                     (let (ref2)
4700                       (setq ref (substring in-reply-to (match-beginning 0)
4701                                            (match-end 0)))
4702                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4703                         (setq ref2 (substring in-reply-to (match-beginning 0)
4704                                               (match-end 0)))
4705                         (when (> (length ref2) (length ref))
4706                           (setq ref ref2)))
4707                       ref)
4708                   (setq ref nil))))
4709             ;; Chars.
4710             (progn
4711               (goto-char p)
4712               (if (search-forward "\nchars: " nil t)
4713                   (if (numberp (setq chars (ignore-errors (read cur))))
4714                       chars 0)
4715                 0))
4716             ;; Lines.
4717             (progn
4718               (goto-char p)
4719               (if (search-forward "\nlines: " nil t)
4720                   (if (numberp (setq lines (ignore-errors (read cur))))
4721                       lines 0)
4722                 0))
4723             ;; Xref.
4724             (progn
4725               (goto-char p)
4726               (and (search-forward "\nxref: " nil t)
4727                    (nnheader-header-value)))
4728             ;; Extra.
4729             (when gnus-extra-headers
4730               (let ((extra gnus-extra-headers)
4731                     out)
4732                 (while extra
4733                   (goto-char p)
4734                   (when (search-forward
4735                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4736                     (push (cons (car extra) (nnheader-header-value))
4737                           out))
4738                   (pop extra))
4739                 out))))
4740           (when (equal id ref)
4741             (setq ref nil))
4742
4743           (when gnus-alter-header-function
4744             (funcall gnus-alter-header-function header)
4745             (setq id (mail-header-id header)
4746                   ref (gnus-parent-id (mail-header-references header))))
4747
4748           (when (setq header
4749                       (gnus-dependencies-add-header
4750                        header dependencies force-new))
4751             (push header headers))
4752           (goto-char (point-max))
4753           (widen))
4754         (nreverse headers)))))
4755
4756 ;; Goes through the xover lines and returns a list of vectors
4757 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4758                                                   force-new dependencies
4759                                                   group also-fetch-heads)
4760   "Parse the news overview data in the server buffer, and return a
4761 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4762   ;; Get the Xref when the users reads the articles since most/some
4763   ;; NNTP servers do not include Xrefs when using XOVER.
4764   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4765   (let ((mail-parse-charset gnus-newsgroup-charset)
4766         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4767         (cur nntp-server-buffer)
4768         (dependencies (or dependencies gnus-newsgroup-dependencies))
4769         number headers header)
4770     (save-excursion
4771       (set-buffer nntp-server-buffer)
4772       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4773       ;; Allow the user to mangle the headers before parsing them.
4774       (gnus-run-hooks 'gnus-parse-headers-hook)
4775       (goto-char (point-min))
4776       (while (not (eobp))
4777         (condition-case ()
4778             (while (and sequence (not (eobp)))
4779               (setq number (read cur))
4780               (while (and sequence
4781                           (< (car sequence) number))
4782                 (setq sequence (cdr sequence)))
4783               (and sequence
4784                    (eq number (car sequence))
4785                    (progn
4786                      (setq sequence (cdr sequence))
4787                      (setq header (inline
4788                                     (gnus-nov-parse-line
4789                                      number dependencies force-new))))
4790                    (push header headers))
4791               (forward-line 1))
4792           (error
4793            (gnus-error 4 "Strange nov line (%d)"
4794                        (count-lines (point-min) (point)))))
4795         (forward-line 1))
4796       ;; A common bug in inn is that if you have posted an article and
4797       ;; then retrieves the active file, it will answer correctly --
4798       ;; the new article is included.  However, a NOV entry for the
4799       ;; article may not have been generated yet, so this may fail.
4800       ;; We work around this problem by retrieving the last few
4801       ;; headers using HEAD.
4802       (if (or (not also-fetch-heads)
4803               (not sequence))
4804           ;; We (probably) got all the headers.
4805           (nreverse headers)
4806         (let ((gnus-nov-is-evil t))
4807           (nconc
4808            (nreverse headers)
4809            (when (gnus-retrieve-headers sequence group)
4810              (gnus-get-newsgroup-headers))))))))
4811
4812 (defun gnus-article-get-xrefs ()
4813   "Fill in the Xref value in `gnus-current-headers', if necessary.
4814 This is meant to be called in `gnus-article-internal-prepare-hook'."
4815   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4816                                  gnus-current-headers)))
4817     (or (not gnus-use-cross-reference)
4818         (not headers)
4819         (and (mail-header-xref headers)
4820              (not (string= (mail-header-xref headers) "")))
4821         (let ((case-fold-search t)
4822               xref)
4823           (save-restriction
4824             (nnheader-narrow-to-headers)
4825             (goto-char (point-min))
4826             (when (or (and (eq (downcase (char-after)) ?x)
4827                            (looking-at "Xref:"))
4828                       (search-forward "\nXref:" nil t))
4829               (goto-char (1+ (match-end 0)))
4830               (setq xref (buffer-substring (point)
4831                                            (progn (end-of-line) (point))))
4832               (mail-header-set-xref headers xref)))))))
4833
4834 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4835   "Find article ID and insert the summary line for that article.
4836 OLD-HEADER can either be a header or a line number to insert
4837 the subject line on."
4838   (let* ((line (and (numberp old-header) old-header))
4839          (old-header (and (vectorp old-header) old-header))
4840          (header (cond ((and old-header use-old-header)
4841                         old-header)
4842                        ((and (numberp id)
4843                              (gnus-number-to-header id))
4844                         (gnus-number-to-header id))
4845                        (t
4846                         (gnus-read-header id))))
4847          (number (and (numberp id) id))
4848          d)
4849     (when header
4850       ;; Rebuild the thread that this article is part of and go to the
4851       ;; article we have fetched.
4852       (when (and (not gnus-show-threads)
4853                  old-header)
4854         (when (and number
4855                    (setq d (gnus-data-find (mail-header-number old-header))))
4856           (goto-char (gnus-data-pos d))
4857           (gnus-data-remove
4858            number
4859            (- (gnus-point-at-bol)
4860               (prog1
4861                   (1+ (gnus-point-at-eol))
4862                 (gnus-delete-line))))))
4863       (when old-header
4864         (mail-header-set-number header (mail-header-number old-header)))
4865       (setq gnus-newsgroup-sparse
4866             (delq (setq number (mail-header-number header))
4867                   gnus-newsgroup-sparse))
4868       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4869       (push number gnus-newsgroup-limit)
4870       (gnus-rebuild-thread (mail-header-id header) line)
4871       (gnus-summary-goto-subject number nil t))
4872     (when (and (numberp number)
4873                (> number 0))
4874       ;; We have to update the boundaries even if we can't fetch the
4875       ;; article if ID is a number -- so that the next `P' or `N'
4876       ;; command will fetch the previous (or next) article even
4877       ;; if the one we tried to fetch this time has been canceled.
4878       (when (> number gnus-newsgroup-end)
4879         (setq gnus-newsgroup-end number))
4880       (when (< number gnus-newsgroup-begin)
4881         (setq gnus-newsgroup-begin number))
4882       (setq gnus-newsgroup-unselected
4883             (delq number gnus-newsgroup-unselected)))
4884     ;; Report back a success?
4885     (and header (mail-header-number header))))
4886
4887 ;;; Process/prefix in the summary buffer
4888
4889 (defun gnus-summary-work-articles (n)
4890   "Return a list of articles to be worked upon.
4891 The prefix argument, the list of process marked articles, and the
4892 current article will be taken into consideration."
4893   (save-excursion
4894     (set-buffer gnus-summary-buffer)
4895     (cond
4896      (n
4897       ;; A numerical prefix has been given.
4898       (setq n (prefix-numeric-value n))
4899       (let ((backward (< n 0))
4900             (n (abs (prefix-numeric-value n)))
4901             articles article)
4902         (save-excursion
4903           (while
4904               (and (> n 0)
4905                    (push (setq article (gnus-summary-article-number))
4906                          articles)
4907                    (if backward
4908                        (gnus-summary-find-prev nil article)
4909                      (gnus-summary-find-next nil article)))
4910             (decf n)))
4911         (nreverse articles)))
4912      ((and (gnus-region-active-p) (mark))
4913       (message "region active")
4914       ;; Work on the region between point and mark.
4915       (let ((max (max (point) (mark)))
4916             articles article)
4917         (save-excursion
4918           (goto-char (min (min (point) (mark))))
4919           (while
4920               (and
4921                (push (setq article (gnus-summary-article-number)) articles)
4922                (gnus-summary-find-next nil article)
4923                (< (point) max)))
4924           (nreverse articles))))
4925      (gnus-newsgroup-processable
4926       ;; There are process-marked articles present.
4927       ;; Save current state.
4928       (gnus-summary-save-process-mark)
4929       ;; Return the list.
4930       (reverse gnus-newsgroup-processable))
4931      (t
4932       ;; Just return the current article.
4933       (list (gnus-summary-article-number))))))
4934
4935 (defmacro gnus-summary-iterate (arg &rest forms)
4936   "Iterate over the process/prefixed articles and do FORMS.
4937 ARG is the interactive prefix given to the command.  FORMS will be
4938 executed with point over the summary line of the articles."
4939   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4940     `(let ((,articles (gnus-summary-work-articles ,arg)))
4941        (while ,articles
4942          (gnus-summary-goto-subject (car ,articles))
4943          ,@forms
4944          (pop ,articles)))))
4945
4946 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4947 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4948
4949 (defun gnus-summary-save-process-mark ()
4950   "Push the current set of process marked articles on the stack."
4951   (interactive)
4952   (push (copy-sequence gnus-newsgroup-processable)
4953         gnus-newsgroup-process-stack))
4954
4955 (defun gnus-summary-kill-process-mark ()
4956   "Push the current set of process marked articles on the stack and unmark."
4957   (interactive)
4958   (gnus-summary-save-process-mark)
4959   (gnus-summary-unmark-all-processable))
4960
4961 (defun gnus-summary-yank-process-mark ()
4962   "Pop the last process mark state off the stack and restore it."
4963   (interactive)
4964   (unless gnus-newsgroup-process-stack
4965     (error "Empty mark stack"))
4966   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4967
4968 (defun gnus-summary-process-mark-set (set)
4969   "Make SET into the current process marked articles."
4970   (gnus-summary-unmark-all-processable)
4971   (while set
4972     (gnus-summary-set-process-mark (pop set))))
4973
4974 ;;; Searching and stuff
4975
4976 (defun gnus-summary-search-group (&optional backward use-level)
4977   "Search for next unread newsgroup.
4978 If optional argument BACKWARD is non-nil, search backward instead."
4979   (save-excursion
4980     (set-buffer gnus-group-buffer)
4981     (when (gnus-group-search-forward
4982            backward nil (if use-level (gnus-group-group-level) nil))
4983       (gnus-group-group-name))))
4984
4985 (defun gnus-summary-best-group (&optional exclude-group)
4986   "Find the name of the best unread group.
4987 If EXCLUDE-GROUP, do not go to this group."
4988   (save-excursion
4989     (set-buffer gnus-group-buffer)
4990     (save-excursion
4991       (gnus-group-best-unread-group exclude-group))))
4992
4993 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4994   (if backward (gnus-summary-find-prev)
4995     (let* ((dummy (gnus-summary-article-intangible-p))
4996            (article (or article (gnus-summary-article-number)))
4997            (arts (gnus-data-find-list article))
4998            result)
4999       (when (and (not dummy)
5000                  (or (not gnus-summary-check-current)
5001                      (not unread)
5002                      (not (gnus-data-unread-p (car arts)))))
5003         (setq arts (cdr arts)))
5004       (when (setq result
5005                   (if unread
5006                       (progn
5007                         (while arts
5008                           (when (or (and undownloaded
5009                                          (eq gnus-undownloaded-mark
5010                                              (gnus-data-mark (car arts))))
5011                                     (gnus-data-unread-p (car arts)))
5012                             (setq result (car arts)
5013                                   arts nil))
5014                           (setq arts (cdr arts)))
5015                         result)
5016                     (car arts)))
5017         (goto-char (gnus-data-pos result))
5018         (gnus-data-number result)))))
5019
5020 (defun gnus-summary-find-prev (&optional unread article)
5021   (let* ((eobp (eobp))
5022          (article (or article (gnus-summary-article-number)))
5023          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5024          result)
5025     (when (and (not eobp)
5026                (or (not gnus-summary-check-current)
5027                    (not unread)
5028                    (not (gnus-data-unread-p (car arts)))))
5029       (setq arts (cdr arts)))
5030     (when (setq result
5031                 (if unread
5032                     (progn
5033                       (while arts
5034                         (when (gnus-data-unread-p (car arts))
5035                           (setq result (car arts)
5036                                 arts nil))
5037                         (setq arts (cdr arts)))
5038                       result)
5039                   (car arts)))
5040       (goto-char (gnus-data-pos result))
5041       (gnus-data-number result))))
5042
5043 (defun gnus-summary-find-subject (subject &optional unread backward article)
5044   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5045          (article (or article (gnus-summary-article-number)))
5046          (articles (gnus-data-list backward))
5047          (arts (gnus-data-find-list article articles))
5048          result)
5049     (when (or (not gnus-summary-check-current)
5050               (not unread)
5051               (not (gnus-data-unread-p (car arts))))
5052       (setq arts (cdr arts)))
5053     (while arts
5054       (and (or (not unread)
5055                (gnus-data-unread-p (car arts)))
5056            (vectorp (gnus-data-header (car arts)))
5057            (gnus-subject-equal
5058             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5059            (setq result (car arts)
5060                  arts nil))
5061       (setq arts (cdr arts)))
5062     (and result
5063          (goto-char (gnus-data-pos result))
5064          (gnus-data-number result))))
5065
5066 (defun gnus-summary-search-forward (&optional unread subject backward)
5067   "Search forward for an article.
5068 If UNREAD, look for unread articles.  If SUBJECT, look for
5069 articles with that subject.  If BACKWARD, search backward instead."
5070   (cond (subject (gnus-summary-find-subject subject unread backward))
5071         (backward (gnus-summary-find-prev unread))
5072         (t (gnus-summary-find-next unread))))
5073
5074 (defun gnus-recenter (&optional n)
5075   "Center point in window and redisplay frame.
5076 Also do horizontal recentering."
5077   (interactive "P")
5078   (when (and gnus-auto-center-summary
5079              (not (eq gnus-auto-center-summary 'vertical)))
5080     (gnus-horizontal-recenter))
5081   (recenter n))
5082
5083 (defun gnus-summary-recenter ()
5084   "Center point in the summary window.
5085 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5086 displayed, no centering will be performed."
5087   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5088   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5089   (let* ((top (cond ((< (window-height) 4) 0)
5090                     ((< (window-height) 7) 1)
5091                     (t (if (numberp gnus-auto-center-summary)
5092                            gnus-auto-center-summary
5093                          2))))
5094          (height (1- (window-height)))
5095          (bottom (save-excursion (goto-char (point-max))
5096                                  (forward-line (- height))
5097                                  (point)))
5098          (window (get-buffer-window (current-buffer))))
5099     ;; The user has to want it.
5100     (when gnus-auto-center-summary
5101       (when (get-buffer-window gnus-article-buffer)
5102         ;; Only do recentering when the article buffer is displayed,
5103         ;; Set the window start to either `bottom', which is the biggest
5104         ;; possible valid number, or the second line from the top,
5105         ;; whichever is the least.
5106         (set-window-start
5107          window (min bottom (save-excursion
5108                               (forward-line (- top)) (point)))))
5109       ;; Do horizontal recentering while we're at it.
5110       (when (and (get-buffer-window (current-buffer) t)
5111                  (not (eq gnus-auto-center-summary 'vertical)))
5112         (let ((selected (selected-window)))
5113           (select-window (get-buffer-window (current-buffer) t))
5114           (gnus-summary-position-point)
5115           (gnus-horizontal-recenter)
5116           (select-window selected))))))
5117
5118 (defun gnus-summary-jump-to-group (newsgroup)
5119   "Move point to NEWSGROUP in group mode buffer."
5120   ;; Keep update point of group mode buffer if visible.
5121   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5122       (save-window-excursion
5123         ;; Take care of tree window mode.
5124         (when (get-buffer-window gnus-group-buffer)
5125           (pop-to-buffer gnus-group-buffer))
5126         (gnus-group-jump-to-group newsgroup))
5127     (save-excursion
5128       ;; Take care of tree window mode.
5129       (if (get-buffer-window gnus-group-buffer)
5130           (pop-to-buffer gnus-group-buffer)
5131         (set-buffer gnus-group-buffer))
5132       (gnus-group-jump-to-group newsgroup))))
5133
5134 ;; This function returns a list of article numbers based on the
5135 ;; difference between the ranges of read articles in this group and
5136 ;; the range of active articles.
5137 (defun gnus-list-of-unread-articles (group)
5138   (let* ((read (gnus-info-read (gnus-get-info group)))
5139          (active (or (gnus-active group) (gnus-activate-group group)))
5140          (last (cdr active))
5141          first nlast unread)
5142     ;; If none are read, then all are unread.
5143     (if (not read)
5144         (setq first (car active))
5145       ;; If the range of read articles is a single range, then the
5146       ;; first unread article is the article after the last read
5147       ;; article.  Sounds logical, doesn't it?
5148       (if (not (listp (cdr read)))
5149           (setq first (max (car active) (1+ (cdr read))))
5150         ;; `read' is a list of ranges.
5151         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5152                                   (caar read)))
5153                   1)
5154           (setq first (car active)))
5155         (while read
5156           (when first
5157             (while (< first nlast)
5158               (push first unread)
5159               (setq first (1+ first))))
5160           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5161           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5162           (setq read (cdr read)))))
5163     ;; And add the last unread articles.
5164     (while (<= first last)
5165       (push first unread)
5166       (setq first (1+ first)))
5167     ;; Return the list of unread articles.
5168     (delq 0 (nreverse unread))))
5169
5170 (defun gnus-list-of-read-articles (group)
5171   "Return a list of unread, unticked and non-dormant articles."
5172   (let* ((info (gnus-get-info group))
5173          (marked (gnus-info-marks info))
5174          (active (gnus-active group)))
5175     (and info active
5176          (gnus-set-difference
5177           (gnus-sorted-complement
5178            (gnus-uncompress-range active)
5179            (gnus-list-of-unread-articles group))
5180           (append
5181            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5182            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5183
5184 ;; Various summary commands
5185
5186 (defun gnus-summary-select-article-buffer ()
5187   "Reconfigure windows to show article buffer."
5188   (interactive)
5189   (if (not (gnus-buffer-live-p gnus-article-buffer))
5190       (error "There is no article buffer for this summary buffer")
5191     (gnus-configure-windows 'article)
5192     (select-window (get-buffer-window gnus-article-buffer))))
5193
5194 (defun gnus-summary-universal-argument (arg)
5195   "Perform any operation on all articles that are process/prefixed."
5196   (interactive "P")
5197   (let ((articles (gnus-summary-work-articles arg))
5198         func article)
5199     (if (eq
5200          (setq
5201           func
5202           (key-binding
5203            (read-key-sequence
5204             (substitute-command-keys
5205              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5206              ))))
5207          'undefined)
5208         (gnus-error 1 "Undefined key")
5209       (save-excursion
5210         (while articles
5211           (gnus-summary-goto-subject (setq article (pop articles)))
5212           (let (gnus-newsgroup-processable)
5213             (command-execute func))
5214           (gnus-summary-remove-process-mark article)))))
5215   (gnus-summary-position-point))
5216
5217 (defun gnus-summary-toggle-truncation (&optional arg)
5218   "Toggle truncation of summary lines.
5219 With arg, turn line truncation on iff arg is positive."
5220   (interactive "P")
5221   (setq truncate-lines
5222         (if (null arg) (not truncate-lines)
5223           (> (prefix-numeric-value arg) 0)))
5224   (redraw-display))
5225
5226 (defun gnus-summary-reselect-current-group (&optional all rescan)
5227   "Exit and then reselect the current newsgroup.
5228 The prefix argument ALL means to select all articles."
5229   (interactive "P")
5230   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5231     (error "Ephemeral groups can't be reselected"))
5232   (let ((current-subject (gnus-summary-article-number))
5233         (group gnus-newsgroup-name))
5234     (setq gnus-newsgroup-begin nil)
5235     (gnus-summary-exit)
5236     ;; We have to adjust the point of group mode buffer because
5237     ;; point was moved to the next unread newsgroup by exiting.
5238     (gnus-summary-jump-to-group group)
5239     (when rescan
5240       (save-excursion
5241         (gnus-group-get-new-news-this-group 1)))
5242     (gnus-group-read-group all t)
5243     (gnus-summary-goto-subject current-subject nil t)))
5244
5245 (defun gnus-summary-rescan-group (&optional all)
5246   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5247   (interactive "P")
5248   (gnus-summary-reselect-current-group all t))
5249
5250 (defun gnus-summary-update-info (&optional non-destructive)
5251   (save-excursion
5252     (let ((group gnus-newsgroup-name))
5253       (when group
5254         (when gnus-newsgroup-kill-headers
5255           (setq gnus-newsgroup-killed
5256                 (gnus-compress-sequence
5257                  (nconc
5258                   (gnus-set-sorted-intersection
5259                    (gnus-uncompress-range gnus-newsgroup-killed)
5260                    (setq gnus-newsgroup-unselected
5261                          (sort gnus-newsgroup-unselected '<)))
5262                   (setq gnus-newsgroup-unreads
5263                         (sort gnus-newsgroup-unreads '<)))
5264                  t)))
5265         (unless (listp (cdr gnus-newsgroup-killed))
5266           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5267         (let ((headers gnus-newsgroup-headers))
5268           ;; Set the new ranges of read articles.
5269           (save-excursion
5270             (set-buffer gnus-group-buffer)
5271             (gnus-undo-force-boundary))
5272           (gnus-update-read-articles
5273            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5274           ;; Set the current article marks.
5275           (let ((gnus-newsgroup-scored
5276                  (if (and (not gnus-save-score)
5277                           (not non-destructive))
5278                      nil
5279                    gnus-newsgroup-scored)))
5280             (save-excursion
5281               (gnus-update-marks)))
5282           ;; Do the cross-ref thing.
5283           (when gnus-use-cross-reference
5284             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5285           ;; Do not switch windows but change the buffer to work.
5286           (set-buffer gnus-group-buffer)
5287           (unless (gnus-ephemeral-group-p group)
5288             (gnus-group-update-group group)))))))
5289
5290 (defun gnus-summary-save-newsrc (&optional force)
5291   "Save the current number of read/marked articles in the dribble buffer.
5292 The dribble buffer will then be saved.
5293 If FORCE (the prefix), also save the .newsrc file(s)."
5294   (interactive "P")
5295   (gnus-summary-update-info t)
5296   (if force
5297       (gnus-save-newsrc-file)
5298     (gnus-dribble-save)))
5299
5300 (defun gnus-summary-exit (&optional temporary)
5301   "Exit reading current newsgroup, and then return to group selection mode.
5302 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5303   (interactive)
5304   (gnus-set-global-variables)
5305   (when (gnus-buffer-live-p gnus-article-buffer)
5306     (save-excursion
5307       (set-buffer gnus-article-buffer)
5308       (mm-destroy-parts gnus-article-mime-handles)))
5309   (gnus-kill-save-kill-buffer)
5310   (gnus-async-halt-prefetch)
5311   (let* ((group gnus-newsgroup-name)
5312          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5313          (mode major-mode)
5314          (group-point nil)
5315          (buf (current-buffer)))
5316     (unless quit-config
5317       ;; Do adaptive scoring, and possibly save score files.
5318       (when gnus-newsgroup-adaptive
5319         (let ((gnus-newsgroup-adaptive gnus-use-adaptive-scoring))
5320           (gnus-score-adaptive)))
5321       (when gnus-use-scoring
5322         (gnus-score-save)))
5323     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5324     ;; If we have several article buffers, we kill them at exit.
5325     (unless gnus-single-article-buffer
5326       (gnus-kill-buffer gnus-original-article-buffer)
5327       (setq gnus-article-current nil))
5328     (when gnus-use-cache
5329       (gnus-cache-possibly-remove-articles)
5330       (gnus-cache-save-buffers))
5331     (gnus-async-prefetch-remove-group group)
5332     (when gnus-suppress-duplicates
5333       (gnus-dup-enter-articles))
5334     (when gnus-use-trees
5335       (gnus-tree-close group))
5336     ;; Remove entries for this group.
5337     (nnmail-purge-split-history (gnus-group-real-name group))
5338     ;; Make all changes in this group permanent.
5339     (unless quit-config
5340       (gnus-run-hooks 'gnus-exit-group-hook)
5341       (gnus-summary-update-info))
5342     (gnus-close-group group)
5343     ;; Make sure where we were, and go to next newsgroup.
5344     (set-buffer gnus-group-buffer)
5345     (unless quit-config
5346       (gnus-group-jump-to-group group))
5347     (gnus-run-hooks 'gnus-summary-exit-hook)
5348     (unless (or quit-config
5349                 ;; If this group has disappeared from the summary
5350                 ;; buffer, don't skip forwards.
5351                 (not (string= group (gnus-group-group-name))))
5352       (gnus-group-next-unread-group 1))
5353     (setq group-point (point))
5354     (if temporary
5355         nil                             ;Nothing to do.
5356       ;; If we have several article buffers, we kill them at exit.
5357       (unless gnus-single-article-buffer
5358         (gnus-kill-buffer gnus-article-buffer)
5359         (gnus-kill-buffer gnus-original-article-buffer)
5360         (setq gnus-article-current nil))
5361       (set-buffer buf)
5362       (if (not gnus-kill-summary-on-exit)
5363           (gnus-deaden-summary)
5364         ;; We set all buffer-local variables to nil.  It is unclear why
5365         ;; this is needed, but if we don't, buffer-local variables are
5366         ;; not garbage-collected, it seems.  This would the lead to en
5367         ;; ever-growing Emacs.
5368         (gnus-summary-clear-local-variables)
5369         (when (get-buffer gnus-article-buffer)
5370           (bury-buffer gnus-article-buffer))
5371         ;; We clear the global counterparts of the buffer-local
5372         ;; variables as well, just to be on the safe side.
5373         (set-buffer gnus-group-buffer)
5374         (gnus-summary-clear-local-variables)
5375         ;; Return to group mode buffer.
5376         (when (eq mode 'gnus-summary-mode)
5377           (gnus-kill-buffer buf)))
5378       (setq gnus-current-select-method gnus-select-method)
5379       (pop-to-buffer gnus-group-buffer)
5380       (if (not quit-config)
5381           (progn
5382             (goto-char group-point)
5383             (gnus-configure-windows 'group 'force))
5384         (gnus-handle-ephemeral-exit quit-config))
5385       ;; Clear the current group name.
5386       (unless quit-config
5387         (setq gnus-newsgroup-name nil)))))
5388
5389 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5390 (defun gnus-summary-exit-no-update (&optional no-questions)
5391   "Quit reading current newsgroup without updating read article info."
5392   (interactive)
5393   (let* ((group gnus-newsgroup-name)
5394          (quit-config (gnus-group-quit-config group)))
5395     (when (or no-questions
5396               gnus-expert-user
5397               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5398       (gnus-async-halt-prefetch)
5399       (mapcar 'funcall
5400               (delq 'gnus-summary-expire-articles
5401                     (copy-list gnus-summary-prepare-exit-hook)))
5402       (when (gnus-buffer-live-p gnus-article-buffer)
5403         (save-excursion
5404           (set-buffer gnus-article-buffer)
5405           (mm-destroy-parts gnus-article-mime-handles)))
5406       ;; If we have several article buffers, we kill them at exit.
5407       (unless gnus-single-article-buffer
5408         (gnus-kill-buffer gnus-article-buffer)
5409         (gnus-kill-buffer gnus-original-article-buffer)
5410         (setq gnus-article-current nil))
5411       (if (not gnus-kill-summary-on-exit)
5412           (gnus-deaden-summary)
5413         (gnus-close-group group)
5414         (gnus-summary-clear-local-variables)
5415         (set-buffer gnus-group-buffer)
5416         (gnus-summary-clear-local-variables)
5417         (when (get-buffer gnus-summary-buffer)
5418           (kill-buffer gnus-summary-buffer)))
5419       (unless gnus-single-article-buffer
5420         (setq gnus-article-current nil))
5421       (when gnus-use-trees
5422         (gnus-tree-close group))
5423       (gnus-async-prefetch-remove-group group)
5424       (when (get-buffer gnus-article-buffer)
5425         (bury-buffer gnus-article-buffer))
5426       ;; Return to the group buffer.
5427       (gnus-configure-windows 'group 'force)
5428       ;; Clear the current group name.
5429       (setq gnus-newsgroup-name nil)
5430       (when (equal (gnus-group-group-name) group)
5431         (gnus-group-next-unread-group 1))
5432       (when quit-config
5433         (gnus-handle-ephemeral-exit quit-config)))))
5434
5435 (defun gnus-handle-ephemeral-exit (quit-config)
5436   "Handle movement when leaving an ephemeral group.
5437 The state which existed when entering the ephemeral is reset."
5438   (if (not (buffer-name (car quit-config)))
5439       (gnus-configure-windows 'group 'force)
5440     (set-buffer (car quit-config))
5441     (cond ((eq major-mode 'gnus-summary-mode)
5442            (gnus-set-global-variables))
5443           ((eq major-mode 'gnus-article-mode)
5444            (save-excursion
5445              ;; The `gnus-summary-buffer' variable may point
5446              ;; to the old summary buffer when using a single
5447              ;; article buffer.
5448              (unless (gnus-buffer-live-p gnus-summary-buffer)
5449                (set-buffer gnus-group-buffer))
5450              (set-buffer gnus-summary-buffer)
5451              (gnus-set-global-variables))))
5452     (if (or (eq (cdr quit-config) 'article)
5453             (eq (cdr quit-config) 'pick))
5454         (progn
5455           ;; The current article may be from the ephemeral group
5456           ;; thus it is best that we reload this article
5457           (gnus-summary-show-article)
5458           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5459               (gnus-configure-windows 'pick 'force)
5460             (gnus-configure-windows (cdr quit-config) 'force)))
5461       (gnus-configure-windows (cdr quit-config) 'force))
5462     (when (eq major-mode 'gnus-summary-mode)
5463       (gnus-summary-next-subject 1 nil t)
5464       (gnus-summary-recenter)
5465       (gnus-summary-position-point))))
5466
5467 ;;; Dead summaries.
5468
5469 (defvar gnus-dead-summary-mode-map nil)
5470
5471 (unless gnus-dead-summary-mode-map
5472   (setq gnus-dead-summary-mode-map (make-keymap))
5473   (suppress-keymap gnus-dead-summary-mode-map)
5474   (substitute-key-definition
5475    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5476   (let ((keys '("\C-d" "\r" "\177" [delete])))
5477     (while keys
5478       (define-key gnus-dead-summary-mode-map
5479         (pop keys) 'gnus-summary-wake-up-the-dead))))
5480
5481 (defvar gnus-dead-summary-mode nil
5482   "Minor mode for Gnus summary buffers.")
5483
5484 (defun gnus-dead-summary-mode (&optional arg)
5485   "Minor mode for Gnus summary buffers."
5486   (interactive "P")
5487   (when (eq major-mode 'gnus-summary-mode)
5488     (make-local-variable 'gnus-dead-summary-mode)
5489     (setq gnus-dead-summary-mode
5490           (if (null arg) (not gnus-dead-summary-mode)
5491             (> (prefix-numeric-value arg) 0)))
5492     (when gnus-dead-summary-mode
5493       (gnus-add-minor-mode
5494        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5495
5496 (defun gnus-deaden-summary ()
5497   "Make the current summary buffer into a dead summary buffer."
5498   ;; Kill any previous dead summary buffer.
5499   (when (and gnus-dead-summary
5500              (buffer-name gnus-dead-summary))
5501     (save-excursion
5502       (set-buffer gnus-dead-summary)
5503       (when gnus-dead-summary-mode
5504         (kill-buffer (current-buffer)))))
5505   ;; Make this the current dead summary.
5506   (setq gnus-dead-summary (current-buffer))
5507   (gnus-dead-summary-mode 1)
5508   (let ((name (buffer-name)))
5509     (when (string-match "Summary" name)
5510       (rename-buffer
5511        (concat (substring name 0 (match-beginning 0)) "Dead "
5512                (substring name (match-beginning 0)))
5513        t))))
5514
5515 (defun gnus-kill-or-deaden-summary (buffer)
5516   "Kill or deaden the summary BUFFER."
5517   (save-excursion
5518     (when (and (buffer-name buffer)
5519                (not gnus-single-article-buffer))
5520       (save-excursion
5521         (set-buffer buffer)
5522         (gnus-kill-buffer gnus-article-buffer)
5523         (gnus-kill-buffer gnus-original-article-buffer)))
5524     (cond (gnus-kill-summary-on-exit
5525            (when (and gnus-use-trees
5526                       (gnus-buffer-exists-p buffer))
5527              (save-excursion
5528                (set-buffer buffer)
5529                (gnus-tree-close gnus-newsgroup-name)))
5530            (gnus-kill-buffer buffer))
5531           ((gnus-buffer-exists-p buffer)
5532            (save-excursion
5533              (set-buffer buffer)
5534              (gnus-deaden-summary))))))
5535
5536 (defun gnus-summary-wake-up-the-dead (&rest args)
5537   "Wake up the dead summary buffer."
5538   (interactive)
5539   (gnus-dead-summary-mode -1)
5540   (let ((name (buffer-name)))
5541     (when (string-match "Dead " name)
5542       (rename-buffer
5543        (concat (substring name 0 (match-beginning 0))
5544                (substring name (match-end 0)))
5545        t)))
5546   (gnus-message 3 "This dead summary is now alive again"))
5547
5548 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5549 (defun gnus-summary-fetch-faq (&optional faq-dir)
5550   "Fetch the FAQ for the current group.
5551 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5552 in."
5553   (interactive
5554    (list
5555     (when current-prefix-arg
5556       (completing-read
5557        "Faq dir: " (and (listp gnus-group-faq-directory)
5558                         (mapcar (lambda (file) (list file))
5559                                 gnus-group-faq-directory))))))
5560   (let (gnus-faq-buffer)
5561     (when (setq gnus-faq-buffer
5562                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5563       (gnus-configure-windows 'summary-faq))))
5564
5565 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5566 (defun gnus-summary-describe-group (&optional force)
5567   "Describe the current newsgroup."
5568   (interactive "P")
5569   (gnus-group-describe-group force gnus-newsgroup-name))
5570
5571 (defun gnus-summary-describe-briefly ()
5572   "Describe summary mode commands briefly."
5573   (interactive)
5574   (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")))
5575
5576 ;; Walking around group mode buffer from summary mode.
5577
5578 (defun gnus-summary-next-group (&optional no-article target-group backward)
5579   "Exit current newsgroup and then select next unread newsgroup.
5580 If prefix argument NO-ARTICLE is non-nil, no article is selected
5581 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5582 previous group instead."
5583   (interactive "P")
5584   ;; Stop pre-fetching.
5585   (gnus-async-halt-prefetch)
5586   (let ((current-group gnus-newsgroup-name)
5587         (current-buffer (current-buffer))
5588         entered)
5589     ;; First we semi-exit this group to update Xrefs and all variables.
5590     ;; We can't do a real exit, because the window conf must remain
5591     ;; the same in case the user is prompted for info, and we don't
5592     ;; want the window conf to change before that...
5593     (gnus-summary-exit t)
5594     (while (not entered)
5595       ;; Then we find what group we are supposed to enter.
5596       (set-buffer gnus-group-buffer)
5597       (gnus-group-jump-to-group current-group)
5598       (setq target-group
5599             (or target-group
5600                 (if (eq gnus-keep-same-level 'best)
5601                     (gnus-summary-best-group gnus-newsgroup-name)
5602                   (gnus-summary-search-group backward gnus-keep-same-level))))
5603       (if (not target-group)
5604           ;; There are no further groups, so we return to the group
5605           ;; buffer.
5606           (progn
5607             (gnus-message 5 "Returning to the group buffer")
5608             (setq entered t)
5609             (when (gnus-buffer-live-p current-buffer)
5610               (set-buffer current-buffer)
5611               (gnus-summary-exit))
5612             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5613         ;; We try to enter the target group.
5614         (gnus-group-jump-to-group target-group)
5615         (let ((unreads (gnus-group-group-unread)))
5616           (if (and (or (eq t unreads)
5617                        (and unreads (not (zerop unreads))))
5618                    (gnus-summary-read-group
5619                     target-group nil no-article
5620                     (and (buffer-name current-buffer) current-buffer)
5621                     nil backward))
5622               (setq entered t)
5623             (setq current-group target-group
5624                   target-group nil)))))))
5625
5626 (defun gnus-summary-prev-group (&optional no-article)
5627   "Exit current newsgroup and then select previous unread newsgroup.
5628 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5629   (interactive "P")
5630   (gnus-summary-next-group no-article nil t))
5631
5632 ;; Walking around summary lines.
5633
5634 (defun gnus-summary-first-subject (&optional unread undownloaded)
5635   "Go to the first unread subject.
5636 If UNREAD is non-nil, go to the first unread article.
5637 Returns the article selected or nil if there are no unread articles."
5638   (interactive "P")
5639   (prog1
5640       (cond
5641        ;; Empty summary.
5642        ((null gnus-newsgroup-data)
5643         (gnus-message 3 "No articles in the group")
5644         nil)
5645        ;; Pick the first article.
5646        ((not unread)
5647         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5648         (gnus-data-number (car gnus-newsgroup-data)))
5649        ;; No unread articles.
5650        ((null gnus-newsgroup-unreads)
5651         (gnus-message 3 "No more unread articles")
5652         nil)
5653        ;; Find the first unread article.
5654        (t
5655         (let ((data gnus-newsgroup-data))
5656           (while (and data
5657                       (and (not (and undownloaded
5658                                      (eq gnus-undownloaded-mark
5659                                          (gnus-data-mark (car data)))))
5660                            (not (gnus-data-unread-p (car data)))))
5661             (setq data (cdr data)))
5662           (when data
5663             (goto-char (gnus-data-pos (car data)))
5664             (gnus-data-number (car data))))))
5665     (gnus-summary-position-point)))
5666
5667 (defun gnus-summary-next-subject (n &optional unread dont-display)
5668   "Go to next N'th summary line.
5669 If N is negative, go to the previous N'th subject line.
5670 If UNREAD is non-nil, only unread articles are selected.
5671 The difference between N and the actual number of steps taken is
5672 returned."
5673   (interactive "p")
5674   (let ((backward (< n 0))
5675         (n (abs n)))
5676     (while (and (> n 0)
5677                 (if backward
5678                     (gnus-summary-find-prev unread)
5679                   (gnus-summary-find-next unread)))
5680       (gnus-summary-show-thread)
5681       (setq n (1- n)))
5682     (when (/= 0 n)
5683       (gnus-message 7 "No more%s articles"
5684                     (if unread " unread" "")))
5685     (unless dont-display
5686       (gnus-summary-recenter)
5687       (gnus-summary-position-point))
5688     n))
5689
5690 (defun gnus-summary-next-unread-subject (n)
5691   "Go to next N'th unread summary line."
5692   (interactive "p")
5693   (gnus-summary-next-subject n t))
5694
5695 (defun gnus-summary-prev-subject (n &optional unread)
5696   "Go to previous N'th summary line.
5697 If optional argument UNREAD is non-nil, only unread article is selected."
5698   (interactive "p")
5699   (gnus-summary-next-subject (- n) unread))
5700
5701 (defun gnus-summary-prev-unread-subject (n)
5702   "Go to previous N'th unread summary line."
5703   (interactive "p")
5704   (gnus-summary-next-subject (- n) t))
5705
5706 (defun gnus-summary-goto-subject (article &optional force silent)
5707   "Go the subject line of ARTICLE.
5708 If FORCE, also allow jumping to articles not currently shown."
5709   (interactive "nArticle number: ")
5710   (let ((b (point))
5711         (data (gnus-data-find article)))
5712     ;; We read in the article if we have to.
5713     (and (not data)
5714          force
5715          (gnus-summary-insert-subject
5716           article
5717           (if (or (numberp force) (vectorp force)) force)
5718           t)
5719          (setq data (gnus-data-find article)))
5720     (goto-char b)
5721     (if (not data)
5722         (progn
5723           (unless silent
5724             (gnus-message 3 "Can't find article %d" article))
5725           nil)
5726       (goto-char (gnus-data-pos data))
5727       (gnus-summary-position-point)
5728       article)))
5729
5730 ;; Walking around summary lines with displaying articles.
5731
5732 (defun gnus-summary-expand-window (&optional arg)
5733   "Make the summary buffer take up the entire Emacs frame.
5734 Given a prefix, will force an `article' buffer configuration."
5735   (interactive "P")
5736   (if arg
5737       (gnus-configure-windows 'article 'force)
5738     (gnus-configure-windows 'summary 'force)))
5739
5740 (defun gnus-summary-display-article (article &optional all-header)
5741   "Display ARTICLE in article buffer."
5742   (gnus-set-global-variables)
5743   (if (null article)
5744       nil
5745     (prog1
5746         (if gnus-summary-display-article-function
5747             (funcall gnus-summary-display-article-function article all-header)
5748           (gnus-article-prepare article all-header))
5749       (gnus-run-hooks 'gnus-select-article-hook)
5750       (when (and gnus-current-article
5751                  (not (zerop gnus-current-article)))
5752         (gnus-summary-goto-subject gnus-current-article))
5753       (gnus-summary-recenter)
5754       (when (and gnus-use-trees gnus-show-threads)
5755         (gnus-possibly-generate-tree article)
5756         (gnus-highlight-selected-tree article))
5757       ;; Successfully display article.
5758       (gnus-article-set-window-start
5759        (cdr (assq article gnus-newsgroup-bookmarks))))))
5760
5761 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5762   "Select the current article.
5763 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5764 non-nil, the article will be re-fetched even if it already present in
5765 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5766 be displayed."
5767   ;; Make sure we are in the summary buffer to work around bbdb bug.
5768   (unless (eq major-mode 'gnus-summary-mode)
5769     (set-buffer gnus-summary-buffer))
5770   (let ((article (or article (gnus-summary-article-number)))
5771         (all-headers (not (not all-headers))) ;Must be T or NIL.
5772         gnus-summary-display-article-function
5773         did)
5774     (and (not pseudo)
5775          (gnus-summary-article-pseudo-p article)
5776          (error "This is a pseudo-article"))
5777     (prog1
5778         (save-excursion
5779           (set-buffer gnus-summary-buffer)
5780           (if (or (and gnus-single-article-buffer
5781                        (or (null gnus-current-article)
5782                            (null gnus-article-current)
5783                            (null (get-buffer gnus-article-buffer))
5784                            (not (eq article (cdr gnus-article-current)))
5785                            (not (equal (car gnus-article-current)
5786                                        gnus-newsgroup-name))))
5787                   (and (not gnus-single-article-buffer)
5788                        (or (null gnus-current-article)
5789                            (not (eq gnus-current-article article))))
5790                   force)
5791               ;; The requested article is different from the current article.
5792               (prog1
5793                   (gnus-summary-display-article article all-headers)
5794                 (setq did article)
5795                 (when (or all-headers gnus-show-all-headers)
5796                   (gnus-article-show-all-headers)))
5797             (when (or all-headers gnus-show-all-headers)
5798               (gnus-article-show-all-headers))
5799             'old))
5800       (when did
5801         (gnus-article-set-window-start
5802          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5803
5804 (defun gnus-summary-set-current-mark (&optional current-mark)
5805   "Obsolete function."
5806   nil)
5807
5808 (defun gnus-summary-next-article (&optional unread subject backward push)
5809   "Select the next article.
5810 If UNREAD, only unread articles are selected.
5811 If SUBJECT, only articles with SUBJECT are selected.
5812 If BACKWARD, the previous article is selected instead of the next."
5813   (interactive "P")
5814   (cond
5815    ;; Is there such an article?
5816    ((and (gnus-summary-search-forward unread subject backward)
5817          (or (gnus-summary-display-article (gnus-summary-article-number))
5818              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5819     (gnus-summary-position-point))
5820    ;; If not, we try the first unread, if that is wanted.
5821    ((and subject
5822          gnus-auto-select-same
5823          (gnus-summary-first-unread-article))
5824     (gnus-summary-position-point)
5825     (gnus-message 6 "Wrapped"))
5826    ;; Try to get next/previous article not displayed in this group.
5827    ((and gnus-auto-extend-newsgroup
5828          (not unread) (not subject))
5829     (gnus-summary-goto-article
5830      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5831      nil (count-lines (point-min) (point))))
5832    ;; Go to next/previous group.
5833    (t
5834     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5835       (gnus-summary-jump-to-group gnus-newsgroup-name))
5836     (let ((cmd last-command-char)
5837           (point
5838            (save-excursion
5839              (set-buffer gnus-group-buffer)
5840              (point)))
5841           (group
5842            (if (eq gnus-keep-same-level 'best)
5843                (gnus-summary-best-group gnus-newsgroup-name)
5844              (gnus-summary-search-group backward gnus-keep-same-level))))
5845       ;; For some reason, the group window gets selected.  We change
5846       ;; it back.
5847       (select-window (get-buffer-window (current-buffer)))
5848       ;; Select next unread newsgroup automagically.
5849       (cond
5850        ((or (not gnus-auto-select-next)
5851             (not cmd))
5852         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5853        ((or (eq gnus-auto-select-next 'quietly)
5854             (and (eq gnus-auto-select-next 'slightly-quietly)
5855                  push)
5856             (and (eq gnus-auto-select-next 'almost-quietly)
5857                  (gnus-summary-last-article-p)))
5858         ;; Select quietly.
5859         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5860             (gnus-summary-exit)
5861           (gnus-message 7 "No more%s articles (%s)..."
5862                         (if unread " unread" "")
5863                         (if group (concat "selecting " group)
5864                           "exiting"))
5865           (gnus-summary-next-group nil group backward)))
5866        (t
5867         (when (gnus-key-press-event-p last-input-event)
5868           (gnus-summary-walk-group-buffer
5869            gnus-newsgroup-name cmd unread backward point))))))))
5870
5871 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5872   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5873                       (?\C-p (gnus-group-prev-unread-group 1))))
5874         (cursor-in-echo-area t)
5875         keve key group ended)
5876     (save-excursion
5877       (set-buffer gnus-group-buffer)
5878       (goto-char start)
5879       (setq group
5880             (if (eq gnus-keep-same-level 'best)
5881                 (gnus-summary-best-group gnus-newsgroup-name)
5882               (gnus-summary-search-group backward gnus-keep-same-level))))
5883     (while (not ended)
5884       (gnus-message
5885        5 "No more%s articles%s" (if unread " unread" "")
5886        (if (and group
5887                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5888            (format " (Type %s for %s [%s])"
5889                    (single-key-description cmd) group
5890                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5891          (format " (Type %s to exit %s)"
5892                  (single-key-description cmd)
5893                  gnus-newsgroup-name)))
5894       ;; Confirm auto selection.
5895       (setq key (car (setq keve (gnus-read-event-char))))
5896       (setq ended t)
5897       (cond
5898        ((assq key keystrokes)
5899         (let ((obuf (current-buffer)))
5900           (switch-to-buffer gnus-group-buffer)
5901           (when group
5902             (gnus-group-jump-to-group group))
5903           (eval (cadr (assq key keystrokes)))
5904           (setq group (gnus-group-group-name))
5905           (switch-to-buffer obuf))
5906         (setq ended nil))
5907        ((equal key cmd)
5908         (if (or (not group)
5909                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5910             (gnus-summary-exit)
5911           (gnus-summary-next-group nil group backward)))
5912        (t
5913         (push (cdr keve) unread-command-events))))))
5914
5915 (defun gnus-summary-next-unread-article ()
5916   "Select unread article after current one."
5917   (interactive)
5918   (gnus-summary-next-article
5919    (or (not (eq gnus-summary-goto-unread 'never))
5920        (gnus-summary-last-article-p (gnus-summary-article-number)))
5921    (and gnus-auto-select-same
5922         (gnus-summary-article-subject))))
5923
5924 (defun gnus-summary-prev-article (&optional unread subject)
5925   "Select the article after the current one.
5926 If UNREAD is non-nil, only unread articles are selected."
5927   (interactive "P")
5928   (gnus-summary-next-article unread subject t))
5929
5930 (defun gnus-summary-prev-unread-article ()
5931   "Select unread article before current one."
5932   (interactive)
5933   (gnus-summary-prev-article
5934    (or (not (eq gnus-summary-goto-unread 'never))
5935        (gnus-summary-first-article-p (gnus-summary-article-number)))
5936    (and gnus-auto-select-same
5937         (gnus-summary-article-subject))))
5938
5939 (defun gnus-summary-next-page (&optional lines circular)
5940   "Show next page of the selected article.
5941 If at the end of the current article, select the next article.
5942 LINES says how many lines should be scrolled up.
5943
5944 If CIRCULAR is non-nil, go to the start of the article instead of
5945 selecting the next article when reaching the end of the current
5946 article."
5947   (interactive "P")
5948   (setq gnus-summary-buffer (current-buffer))
5949   (gnus-set-global-variables)
5950   (let ((article (gnus-summary-article-number))
5951         (article-window (get-buffer-window gnus-article-buffer t))
5952         endp)
5953     ;; If the buffer is empty, we have no article.
5954     (unless article
5955       (error "No article to select"))
5956     (gnus-configure-windows 'article)
5957     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5958         (if (and (eq gnus-summary-goto-unread 'never)
5959                  (not (gnus-summary-last-article-p article)))
5960             (gnus-summary-next-article)
5961           (gnus-summary-next-unread-article))
5962       (if (or (null gnus-current-article)
5963               (null gnus-article-current)
5964               (/= article (cdr gnus-article-current))
5965               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5966           ;; Selected subject is different from current article's.
5967           (gnus-summary-display-article article)
5968         (when article-window
5969           (gnus-eval-in-buffer-window gnus-article-buffer
5970             (setq endp (gnus-article-next-page lines)))
5971           (when endp
5972             (cond (circular
5973                    (gnus-summary-beginning-of-article))
5974                   (lines
5975                    (gnus-message 3 "End of message"))
5976                   ((null lines)
5977                    (if (and (eq gnus-summary-goto-unread 'never)
5978                             (not (gnus-summary-last-article-p article)))
5979                        (gnus-summary-next-article)
5980                      (gnus-summary-next-unread-article))))))))
5981     (gnus-summary-recenter)
5982     (gnus-summary-position-point)))
5983
5984 (defun gnus-summary-prev-page (&optional lines move)
5985   "Show previous page of selected article.
5986 Argument LINES specifies lines to be scrolled down.
5987 If MOVE, move to the previous unread article if point is at
5988 the beginning of the buffer."
5989   (interactive "P")
5990   (let ((article (gnus-summary-article-number))
5991         (article-window (get-buffer-window gnus-article-buffer t))
5992         endp)
5993     (gnus-configure-windows 'article)
5994     (if (or (null gnus-current-article)
5995             (null gnus-article-current)
5996             (/= article (cdr gnus-article-current))
5997             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5998         ;; Selected subject is different from current article's.
5999         (gnus-summary-display-article article)
6000       (gnus-summary-recenter)
6001       (when article-window
6002         (gnus-eval-in-buffer-window gnus-article-buffer
6003           (setq endp (gnus-article-prev-page lines)))
6004         (when (and move endp)
6005           (cond (lines
6006                  (gnus-message 3 "Beginning of message"))
6007                 ((null lines)
6008                  (if (and (eq gnus-summary-goto-unread 'never)
6009                           (not (gnus-summary-first-article-p article)))
6010                      (gnus-summary-prev-article)
6011                    (gnus-summary-prev-unread-article))))))))
6012   (gnus-summary-position-point))
6013
6014 (defun gnus-summary-prev-page-or-article (&optional lines)
6015   "Show previous page of selected article.
6016 Argument LINES specifies lines to be scrolled down.
6017 If at the beginning of the article, go to the next article."
6018   (interactive "P")
6019   (gnus-summary-prev-page lines t))
6020
6021 (defun gnus-summary-scroll-up (lines)
6022   "Scroll up (or down) one line current article.
6023 Argument LINES specifies lines to be scrolled up (or down if negative)."
6024   (interactive "p")
6025   (gnus-configure-windows 'article)
6026   (gnus-summary-show-thread)
6027   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6028     (gnus-eval-in-buffer-window gnus-article-buffer
6029       (cond ((> lines 0)
6030              (when (gnus-article-next-page lines)
6031                (gnus-message 3 "End of message")))
6032             ((< lines 0)
6033              (gnus-article-prev-page (- lines))))))
6034   (gnus-summary-recenter)
6035   (gnus-summary-position-point))
6036
6037 (defun gnus-summary-scroll-down (lines)
6038   "Scroll down (or up) one line current article.
6039 Argument LINES specifies lines to be scrolled down (or up if negative)."
6040   (interactive "p")
6041   (gnus-summary-scroll-up (- lines)))
6042
6043 (defun gnus-summary-next-same-subject ()
6044   "Select next article which has the same subject as current one."
6045   (interactive)
6046   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6047
6048 (defun gnus-summary-prev-same-subject ()
6049   "Select previous article which has the same subject as current one."
6050   (interactive)
6051   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6052
6053 (defun gnus-summary-next-unread-same-subject ()
6054   "Select next unread article which has the same subject as current one."
6055   (interactive)
6056   (gnus-summary-next-article t (gnus-summary-article-subject)))
6057
6058 (defun gnus-summary-prev-unread-same-subject ()
6059   "Select previous unread article which has the same subject as current one."
6060   (interactive)
6061   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6062
6063 (defun gnus-summary-first-unread-article ()
6064   "Select the first unread article.
6065 Return nil if there are no unread articles."
6066   (interactive)
6067   (prog1
6068       (when (gnus-summary-first-subject t)
6069         (gnus-summary-show-thread)
6070         (gnus-summary-first-subject t)
6071         (gnus-summary-display-article (gnus-summary-article-number)))
6072     (gnus-summary-position-point)))
6073
6074 (defun gnus-summary-first-unread-subject ()
6075   "Place the point on the subject line of the first unread article.
6076 Return nil if there are no unread articles."
6077   (interactive)
6078   (prog1
6079       (when (gnus-summary-first-subject t)
6080         (gnus-summary-show-thread)
6081         (gnus-summary-first-subject t))
6082     (gnus-summary-position-point)))
6083
6084 (defun gnus-summary-first-article ()
6085   "Select the first article.
6086 Return nil if there are no articles."
6087   (interactive)
6088   (prog1
6089       (when (gnus-summary-first-subject)
6090         (gnus-summary-show-thread)
6091         (gnus-summary-first-subject)
6092         (gnus-summary-display-article (gnus-summary-article-number)))
6093     (gnus-summary-position-point)))
6094
6095 (defun gnus-summary-best-unread-article ()
6096   "Select the unread article with the highest score."
6097   (interactive)
6098   (let ((best -1000000)
6099         (data gnus-newsgroup-data)
6100         article score)
6101     (while data
6102       (and (gnus-data-unread-p (car data))
6103            (> (setq score
6104                     (gnus-summary-article-score (gnus-data-number (car data))))
6105               best)
6106            (setq best score
6107                  article (gnus-data-number (car data))))
6108       (setq data (cdr data)))
6109     (prog1
6110         (if article
6111             (gnus-summary-goto-article article)
6112           (error "No unread articles"))
6113       (gnus-summary-position-point))))
6114
6115 (defun gnus-summary-last-subject ()
6116   "Go to the last displayed subject line in the group."
6117   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6118     (when article
6119       (gnus-summary-goto-subject article))))
6120
6121 (defun gnus-summary-goto-article (article &optional all-headers force)
6122   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6123 If ALL-HEADERS is non-nil, no header lines are hidden.
6124 If FORCE, go to the article even if it isn't displayed.  If FORCE
6125 is a number, it is the line the article is to be displayed on."
6126   (interactive
6127    (list
6128     (completing-read
6129      "Article number or Message-ID: "
6130      (mapcar (lambda (number) (list (int-to-string number)))
6131              gnus-newsgroup-limit))
6132     current-prefix-arg
6133     t))
6134   (prog1
6135       (if (and (stringp article)
6136                (string-match "@" article))
6137           (gnus-summary-refer-article article)
6138         (when (stringp article)
6139           (setq article (string-to-number article)))
6140         (if (gnus-summary-goto-subject article force)
6141             (gnus-summary-display-article article all-headers)
6142           (gnus-message 4 "Couldn't go to article %s" article) nil))
6143     (gnus-summary-position-point)))
6144
6145 (defun gnus-summary-goto-last-article ()
6146   "Go to the previously read article."
6147   (interactive)
6148   (prog1
6149       (when gnus-last-article
6150         (gnus-summary-goto-article gnus-last-article nil t))
6151     (gnus-summary-position-point)))
6152
6153 (defun gnus-summary-pop-article (number)
6154   "Pop one article off the history and go to the previous.
6155 NUMBER articles will be popped off."
6156   (interactive "p")
6157   (let (to)
6158     (setq gnus-newsgroup-history
6159           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6160     (if to
6161         (gnus-summary-goto-article (car to) nil t)
6162       (error "Article history empty")))
6163   (gnus-summary-position-point))
6164
6165 ;; Summary commands and functions for limiting the summary buffer.
6166
6167 (defun gnus-summary-limit-to-articles (n)
6168   "Limit the summary buffer to the next N articles.
6169 If not given a prefix, use the process marked articles instead."
6170   (interactive "P")
6171   (prog1
6172       (let ((articles (gnus-summary-work-articles n)))
6173         (setq gnus-newsgroup-processable nil)
6174         (gnus-summary-limit articles))
6175     (gnus-summary-position-point)))
6176
6177 (defun gnus-summary-pop-limit (&optional total)
6178   "Restore the previous limit.
6179 If given a prefix, remove all limits."
6180   (interactive "P")
6181   (when total
6182     (setq gnus-newsgroup-limits
6183           (list (mapcar (lambda (h) (mail-header-number h))
6184                         gnus-newsgroup-headers))))
6185   (unless gnus-newsgroup-limits
6186     (error "No limit to pop"))
6187   (prog1
6188       (gnus-summary-limit nil 'pop)
6189     (gnus-summary-position-point)))
6190
6191 (defun gnus-summary-limit-to-subject (subject &optional header)
6192   "Limit the summary buffer to articles that have subjects that match a regexp."
6193   (interactive "sLimit to subject (regexp): ")
6194   (unless header
6195     (setq header "subject"))
6196   (when (not (equal "" subject))
6197     (prog1
6198         (let ((articles (gnus-summary-find-matching
6199                          (or header "subject") subject 'all)))
6200           (unless articles
6201             (error "Found no matches for \"%s\"" subject))
6202           (gnus-summary-limit articles))
6203       (gnus-summary-position-point))))
6204
6205 (defun gnus-summary-limit-to-author (from)
6206   "Limit the summary buffer to articles that have authors that match a regexp."
6207   (interactive "sLimit to author (regexp): ")
6208   (gnus-summary-limit-to-subject from "from"))
6209
6210 (defun gnus-summary-limit-to-age (age &optional younger-p)
6211   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6212 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6213 articles that are younger than AGE days."
6214   (interactive
6215    (let ((younger current-prefix-arg)
6216          (days-got nil)
6217          days)
6218      (while (not days-got)
6219        (setq days (if younger
6220                       (read-string "Limit to articles within (in days): ")
6221                     (read-string "Limit to articles old than (in days): ")))
6222        (when (> (length days) 0)
6223          (setq days (read days)))
6224        (if (numberp days)
6225            (setq days-got t)
6226          (message "Please enter a number.")
6227          (sleep-for 1)))
6228      (list days younger)))
6229   (prog1
6230       (let ((data gnus-newsgroup-data)
6231             (cutoff (days-to-time age))
6232             articles d date is-younger)
6233         (while (setq d (pop data))
6234           (when (and (vectorp (gnus-data-header d))
6235                      (setq date (mail-header-date (gnus-data-header d))))
6236             (setq is-younger (time-less-p
6237                               (time-since (date-to-time date))
6238                               cutoff))
6239             (when (if younger-p
6240                       is-younger
6241                     (not is-younger))
6242               (push (gnus-data-number d) articles))))
6243         (gnus-summary-limit (nreverse articles)))
6244     (gnus-summary-position-point)))
6245
6246 (defun gnus-summary-limit-to-extra (header regexp)
6247   "Limit the summary buffer to articles that match an 'extra' header."
6248   (interactive
6249    (let ((header
6250           (intern
6251            (gnus-completing-read
6252             (symbol-name (car gnus-extra-headers))      
6253             "Limit extra header:"       
6254             (mapcar (lambda (x) 
6255                       (cons (symbol-name x) x))
6256                     gnus-extra-headers)
6257             nil                 
6258             t))))
6259      (list header
6260            (read-string (format "Limit to header %s (regexp): " header)))))
6261   (when (not (equal "" regexp))
6262     (prog1
6263         (let ((articles (gnus-summary-find-matching
6264                          (cons 'extra header) regexp 'all)))
6265           (unless articles
6266             (error "Found no matches for \"%s\"" regexp))
6267           (gnus-summary-limit articles))
6268       (gnus-summary-position-point))))
6269
6270 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6271 (make-obsolete
6272  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6273
6274 (defun gnus-summary-limit-to-unread (&optional all)
6275   "Limit the summary buffer to articles that are not marked as read.
6276 If ALL is non-nil, limit strictly to unread articles."
6277   (interactive "P")
6278   (if all
6279       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6280     (gnus-summary-limit-to-marks
6281      ;; Concat all the marks that say that an article is read and have
6282      ;; those removed.
6283      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6284            gnus-killed-mark gnus-kill-file-mark
6285            gnus-low-score-mark gnus-expirable-mark
6286            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6287            gnus-duplicate-mark gnus-souped-mark)
6288      'reverse)))
6289
6290 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6291 (make-obsolete 'gnus-summary-delete-marked-with
6292                'gnus-summary-limit-exlude-marks)
6293
6294 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6295   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6296 If REVERSE, limit the summary buffer to articles that are marked
6297 with MARKS.  MARKS can either be a string of marks or a list of marks.
6298 Returns how many articles were removed."
6299   (interactive "sMarks: ")
6300   (gnus-summary-limit-to-marks marks t))
6301
6302 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6303   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6304 If REVERSE (the prefix), limit the summary buffer to articles that are
6305 not marked with MARKS.  MARKS can either be a string of marks or a
6306 list of marks.
6307 Returns how many articles were removed."
6308   (interactive "sMarks: \nP")
6309   (prog1
6310       (let ((data gnus-newsgroup-data)
6311             (marks (if (listp marks) marks
6312                      (append marks nil))) ; Transform to list.
6313             articles)
6314         (while data
6315           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6316                   (memq (gnus-data-mark (car data)) marks))
6317             (push (gnus-data-number (car data)) articles))
6318           (setq data (cdr data)))
6319         (gnus-summary-limit articles))
6320     (gnus-summary-position-point)))
6321
6322 (defun gnus-summary-limit-to-score (&optional score)
6323   "Limit to articles with score at or above SCORE."
6324   (interactive "P")
6325   (setq score (if score
6326                   (prefix-numeric-value score)
6327                 (or gnus-summary-default-score 0)))
6328   (let ((data gnus-newsgroup-data)
6329         articles)
6330     (while data
6331       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6332                 score)
6333         (push (gnus-data-number (car data)) articles))
6334       (setq data (cdr data)))
6335     (prog1
6336         (gnus-summary-limit articles)
6337       (gnus-summary-position-point))))
6338
6339 (defun gnus-summary-limit-include-thread (id)
6340   "Display all the hidden articles that in the current thread."
6341   (interactive (list (mail-header-id (gnus-summary-article-header))))
6342   (let ((articles (gnus-articles-in-thread
6343                    (gnus-id-to-thread (gnus-root-id id)))))
6344     (prog1
6345         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6346       (gnus-summary-position-point))))
6347
6348 (defun gnus-summary-limit-include-dormant ()
6349   "Display all the hidden articles that are marked as dormant.
6350 Note that this command only works on a subset of the articles currently
6351 fetched for this group."
6352   (interactive)
6353   (unless gnus-newsgroup-dormant
6354     (error "There are no dormant articles in this group"))
6355   (prog1
6356       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6357     (gnus-summary-position-point)))
6358
6359 (defun gnus-summary-limit-exclude-dormant ()
6360   "Hide all dormant articles."
6361   (interactive)
6362   (prog1
6363       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6364     (gnus-summary-position-point)))
6365
6366 (defun gnus-summary-limit-exclude-childless-dormant ()
6367   "Hide all dormant articles that have no children."
6368   (interactive)
6369   (let ((data (gnus-data-list t))
6370         articles d children)
6371     ;; Find all articles that are either not dormant or have
6372     ;; children.
6373     (while (setq d (pop data))
6374       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6375                 (and (setq children
6376                            (gnus-article-children (gnus-data-number d)))
6377                      (let (found)
6378                        (while children
6379                          (when (memq (car children) articles)
6380                            (setq children nil
6381                                  found t))
6382                          (pop children))
6383                        found)))
6384         (push (gnus-data-number d) articles)))
6385     ;; Do the limiting.
6386     (prog1
6387         (gnus-summary-limit articles)
6388       (gnus-summary-position-point))))
6389
6390 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6391   "Mark all unread excluded articles as read.
6392 If ALL, mark even excluded ticked and dormants as read."
6393   (interactive "P")
6394   (let ((articles (gnus-sorted-complement
6395                    (sort
6396                     (mapcar (lambda (h) (mail-header-number h))
6397                             gnus-newsgroup-headers)
6398                     '<)
6399                    (sort gnus-newsgroup-limit '<)))
6400         article)
6401     (setq gnus-newsgroup-unreads
6402           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6403     (if all
6404         (setq gnus-newsgroup-dormant nil
6405               gnus-newsgroup-marked nil
6406               gnus-newsgroup-reads
6407               (nconc
6408                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6409                gnus-newsgroup-reads))
6410       (while (setq article (pop articles))
6411         (unless (or (memq article gnus-newsgroup-dormant)
6412                     (memq article gnus-newsgroup-marked))
6413           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6414
6415 (defun gnus-summary-limit (articles &optional pop)
6416   (if pop
6417       ;; We pop the previous limit off the stack and use that.
6418       (setq articles (car gnus-newsgroup-limits)
6419             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6420     ;; We use the new limit, so we push the old limit on the stack.
6421     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6422   ;; Set the limit.
6423   (setq gnus-newsgroup-limit articles)
6424   (let ((total (length gnus-newsgroup-data))
6425         (data (gnus-data-find-list (gnus-summary-article-number)))
6426         (gnus-summary-mark-below nil)   ; Inhibit this.
6427         found)
6428     ;; This will do all the work of generating the new summary buffer
6429     ;; according to the new limit.
6430     (gnus-summary-prepare)
6431     ;; Hide any threads, possibly.
6432     (and gnus-show-threads
6433          gnus-thread-hide-subtree
6434          (gnus-summary-hide-all-threads))
6435     ;; Try to return to the article you were at, or one in the
6436     ;; neighborhood.
6437     (when data
6438       ;; We try to find some article after the current one.
6439       (while data
6440         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6441           (setq data nil
6442                 found t))
6443         (setq data (cdr data))))
6444     (unless found
6445       ;; If there is no data, that means that we were after the last
6446       ;; article.  The same goes when we can't find any articles
6447       ;; after the current one.
6448       (goto-char (point-max))
6449       (gnus-summary-find-prev))
6450     (gnus-set-mode-line 'summary)
6451     ;; We return how many articles were removed from the summary
6452     ;; buffer as a result of the new limit.
6453     (- total (length gnus-newsgroup-data))))
6454
6455 (defsubst gnus-invisible-cut-children (threads)
6456   (let ((num 0))
6457     (while threads
6458       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6459         (incf num))
6460       (pop threads))
6461     (< num 2)))
6462
6463 (defsubst gnus-cut-thread (thread)
6464   "Go forwards in the thread until we find an article that we want to display."
6465   (when (or (eq gnus-fetch-old-headers 'some)
6466             (eq gnus-fetch-old-headers 'invisible)
6467             (eq gnus-build-sparse-threads 'some)
6468             (eq gnus-build-sparse-threads 'more))
6469     ;; Deal with old-fetched headers and sparse threads.
6470     (while (and
6471             thread
6472             (or
6473              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6474              (gnus-summary-article-ancient-p
6475               (mail-header-number (car thread))))
6476             (if (or (<= (length (cdr thread)) 1)
6477                     (eq gnus-fetch-old-headers 'invisible))
6478                 (setq gnus-newsgroup-limit
6479                       (delq (mail-header-number (car thread))
6480                             gnus-newsgroup-limit)
6481                       thread (cadr thread))
6482               (when (gnus-invisible-cut-children (cdr thread))
6483                 (let ((th (cdr thread)))
6484                   (while th
6485                     (if (memq (mail-header-number (caar th))
6486                               gnus-newsgroup-limit)
6487                         (setq thread (car th)
6488                               th nil)
6489                       (setq th (cdr th))))))))))
6490   thread)
6491
6492 (defun gnus-cut-threads (threads)
6493   "Cut off all uninteresting articles from the beginning of threads."
6494   (when (or (eq gnus-fetch-old-headers 'some)
6495             (eq gnus-fetch-old-headers 'invisible)
6496             (eq gnus-build-sparse-threads 'some)
6497             (eq gnus-build-sparse-threads 'more))
6498     (let ((th threads))
6499       (while th
6500         (setcar th (gnus-cut-thread (car th)))
6501         (setq th (cdr th)))))
6502   ;; Remove nixed out threads.
6503   (delq nil threads))
6504
6505 (defun gnus-summary-initial-limit (&optional show-if-empty)
6506   "Figure out what the initial limit is supposed to be on group entry.
6507 This entails weeding out unwanted dormants, low-scored articles,
6508 fetch-old-headers verbiage, and so on."
6509   ;; Most groups have nothing to remove.
6510   (if (or gnus-inhibit-limiting
6511           (and (null gnus-newsgroup-dormant)
6512                (not (eq gnus-fetch-old-headers 'some))
6513                (not (eq gnus-fetch-old-headers 'invisible))
6514                (null gnus-summary-expunge-below)
6515                (not (eq gnus-build-sparse-threads 'some))
6516                (not (eq gnus-build-sparse-threads 'more))
6517                (null gnus-thread-expunge-below)
6518                (not gnus-use-nocem)))
6519       ()                                ; Do nothing.
6520     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6521     (setq gnus-newsgroup-limit nil)
6522     (mapatoms
6523      (lambda (node)
6524        (unless (car (symbol-value node))
6525          ;; These threads have no parents -- they are roots.
6526          (let ((nodes (cdr (symbol-value node)))
6527                thread)
6528            (while nodes
6529              (if (and gnus-thread-expunge-below
6530                       (< (gnus-thread-total-score (car nodes))
6531                          gnus-thread-expunge-below))
6532                  (gnus-expunge-thread (pop nodes))
6533                (setq thread (pop nodes))
6534                (gnus-summary-limit-children thread))))))
6535      gnus-newsgroup-dependencies)
6536     ;; If this limitation resulted in an empty group, we might
6537     ;; pop the previous limit and use it instead.
6538     (when (and (not gnus-newsgroup-limit)
6539                show-if-empty)
6540       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6541     gnus-newsgroup-limit))
6542
6543 (defun gnus-summary-limit-children (thread)
6544   "Return 1 if this subthread is visible and 0 if it is not."
6545   ;; First we get the number of visible children to this thread.  This
6546   ;; is done by recursing down the thread using this function, so this
6547   ;; will really go down to a leaf article first, before slowly
6548   ;; working its way up towards the root.
6549   (when thread
6550     (let ((children
6551            (if (cdr thread)
6552                (apply '+ (mapcar 'gnus-summary-limit-children
6553                                  (cdr thread)))
6554              0))
6555           (number (mail-header-number (car thread)))
6556           score)
6557       (if (and
6558            (not (memq number gnus-newsgroup-marked))
6559            (or
6560             ;; If this article is dormant and has absolutely no visible
6561             ;; children, then this article isn't visible.
6562             (and (memq number gnus-newsgroup-dormant)
6563                  (zerop children))
6564             ;; If this is "fetch-old-headered" and there is no
6565             ;; visible children, then we don't want this article.
6566             (and (eq gnus-fetch-old-headers 'some)
6567                  (gnus-summary-article-ancient-p number)
6568                  (zerop children))
6569             ;; If this is "fetch-old-headered" and `invisible', then
6570             ;; we don't want this article.
6571             (and (eq gnus-fetch-old-headers 'invisible)
6572                  (gnus-summary-article-ancient-p number))
6573             ;; If this is a sparsely inserted article with no children,
6574             ;; we don't want it.
6575             (and (eq gnus-build-sparse-threads 'some)
6576                  (gnus-summary-article-sparse-p number)
6577                  (zerop children))
6578             ;; If we use expunging, and this article is really
6579             ;; low-scored, then we don't want this article.
6580             (when (and gnus-summary-expunge-below
6581                        (< (setq score
6582                                 (or (cdr (assq number gnus-newsgroup-scored))
6583                                     gnus-summary-default-score))
6584                           gnus-summary-expunge-below))
6585               ;; We increase the expunge-tally here, but that has
6586               ;; nothing to do with the limits, really.
6587               (incf gnus-newsgroup-expunged-tally)
6588               ;; We also mark as read here, if that's wanted.
6589               (when (and gnus-summary-mark-below
6590                          (< score gnus-summary-mark-below))
6591                 (setq gnus-newsgroup-unreads
6592                       (delq number gnus-newsgroup-unreads))
6593                 (if gnus-newsgroup-auto-expire
6594                     (push number gnus-newsgroup-expirable)
6595                   (push (cons number gnus-low-score-mark)
6596                         gnus-newsgroup-reads)))
6597               t)
6598             ;; Check NoCeM things.
6599             (if (and gnus-use-nocem
6600                      (gnus-nocem-unwanted-article-p
6601                       (mail-header-id (car thread))))
6602                 (progn
6603                   (setq gnus-newsgroup-unreads
6604                         (delq number gnus-newsgroup-unreads))
6605                   t))))
6606           ;; Nope, invisible article.
6607           0
6608         ;; Ok, this article is to be visible, so we add it to the limit
6609         ;; and return 1.
6610         (push number gnus-newsgroup-limit)
6611         1))))
6612
6613 (defun gnus-expunge-thread (thread)
6614   "Mark all articles in THREAD as read."
6615   (let* ((number (mail-header-number (car thread))))
6616     (incf gnus-newsgroup-expunged-tally)
6617     ;; We also mark as read here, if that's wanted.
6618     (setq gnus-newsgroup-unreads
6619           (delq number gnus-newsgroup-unreads))
6620     (if gnus-newsgroup-auto-expire
6621         (push number gnus-newsgroup-expirable)
6622       (push (cons number gnus-low-score-mark)
6623             gnus-newsgroup-reads)))
6624   ;; Go recursively through all subthreads.
6625   (mapcar 'gnus-expunge-thread (cdr thread)))
6626
6627 ;; Summary article oriented commands
6628
6629 (defun gnus-summary-refer-parent-article (n)
6630   "Refer parent article N times.
6631 If N is negative, go to ancestor -N instead.
6632 The difference between N and the number of articles fetched is returned."
6633   (interactive "p")
6634   (let ((skip 1)
6635         error header ref)
6636     (when (not (natnump n))
6637       (setq skip (abs n)
6638             n 1))
6639     (while (and (> n 0)
6640                 (not error))
6641       (setq header (gnus-summary-article-header))
6642       (if (and (eq (mail-header-number header)
6643                    (cdr gnus-article-current))
6644                (equal gnus-newsgroup-name
6645                       (car gnus-article-current)))
6646           ;; If we try to find the parent of the currently
6647           ;; displayed article, then we take a look at the actual
6648           ;; References header, since this is slightly more
6649           ;; reliable than the References field we got from the
6650           ;; server.
6651           (save-excursion
6652             (set-buffer gnus-original-article-buffer)
6653             (nnheader-narrow-to-headers)
6654             (unless (setq ref (message-fetch-field "references"))
6655               (setq ref (message-fetch-field "in-reply-to")))
6656             (widen))
6657         (setq ref
6658               ;; It's not the current article, so we take a bet on
6659               ;; the value we got from the server.
6660               (mail-header-references header)))
6661       (if (and ref
6662                (not (equal ref "")))
6663           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6664             (gnus-message 1 "Couldn't find parent"))
6665         (gnus-message 1 "No references in article %d"
6666                       (gnus-summary-article-number))
6667         (setq error t))
6668       (decf n))
6669     (gnus-summary-position-point)
6670     n))
6671
6672 (defun gnus-summary-refer-references ()
6673   "Fetch all articles mentioned in the References header.
6674 Return the number of articles fetched."
6675   (interactive)
6676   (let ((ref (mail-header-references (gnus-summary-article-header)))
6677         (current (gnus-summary-article-number))
6678         (n 0))
6679     (if (or (not ref)
6680             (equal ref ""))
6681         (error "No References in the current article")
6682       ;; For each Message-ID in the References header...
6683       (while (string-match "<[^>]*>" ref)
6684         (incf n)
6685         ;; ... fetch that article.
6686         (gnus-summary-refer-article
6687          (prog1 (match-string 0 ref)
6688            (setq ref (substring ref (match-end 0))))))
6689       (gnus-summary-goto-subject current)
6690       (gnus-summary-position-point)
6691       n)))
6692
6693 (defun gnus-summary-refer-thread (&optional limit)
6694   "Fetch all articles in the current thread.
6695 If LIMIT (the numerical prefix), fetch that many old headers instead
6696 of what's specified by the `gnus-refer-thread-limit' variable."
6697   (interactive "P")
6698   (let ((id (mail-header-id (gnus-summary-article-header)))
6699         (limit (if limit (prefix-numeric-value limit)
6700                  gnus-refer-thread-limit)))
6701     ;; We want to fetch LIMIT *old* headers, but we also have to
6702     ;; re-fetch all the headers in the current buffer, because many of
6703     ;; them may be undisplayed.  So we adjust LIMIT.
6704     (when (numberp limit)
6705       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6706     (unless (eq gnus-fetch-old-headers 'invisible)
6707       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6708       ;; Retrieve the headers and read them in.
6709       (if (eq (gnus-retrieve-headers
6710                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6711               'nov)
6712           (gnus-build-all-threads)
6713         (error "Can't fetch thread from backends that don't support NOV"))
6714       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6715     (gnus-summary-limit-include-thread id)))
6716
6717 (defun gnus-summary-refer-article (message-id &optional arg)
6718   "Fetch an article specified by MESSAGE-ID.
6719 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6720 or `gnus-select-method', no matter what backend the article comes from."
6721   (interactive "sMessage-ID: \nP")
6722   (when (and (stringp message-id)
6723              (not (zerop (length message-id))))
6724     ;; Construct the correct Message-ID if necessary.
6725     ;; Suggested by tale@pawl.rpi.edu.
6726     (unless (string-match "^<" message-id)
6727       (setq message-id (concat "<" message-id)))
6728     (unless (string-match ">$" message-id)
6729       (setq message-id (concat message-id ">")))
6730     (let* ((header (gnus-id-to-header message-id))
6731            (sparse (and header
6732                         (gnus-summary-article-sparse-p
6733                          (mail-header-number header))
6734                         (memq (mail-header-number header)
6735                               gnus-newsgroup-limit))))
6736       (cond
6737        ;; If the article is present in the buffer we just go to it.
6738        ((and header
6739              (or (not (gnus-summary-article-sparse-p
6740                        (mail-header-number header)))
6741                  sparse))
6742         (prog1
6743             (gnus-summary-goto-article
6744              (mail-header-number header) nil t)
6745           (when sparse
6746             (gnus-summary-update-article (mail-header-number header)))))
6747        (t
6748         ;; We fetch the article
6749         (let ((gnus-override-method
6750                (cond ((gnus-news-group-p gnus-newsgroup-name)
6751                       gnus-refer-article-method)
6752                      (arg
6753                       (or gnus-refer-article-method gnus-select-method))
6754                      (t nil)))
6755               number)
6756           ;; Start the special refer-article method, if necessary.
6757           (when (and gnus-refer-article-method
6758                      (gnus-news-group-p gnus-newsgroup-name))
6759             (gnus-check-server gnus-refer-article-method))
6760           ;; Fetch the header, and display the article.
6761           (if (setq number (gnus-summary-insert-subject message-id))
6762               (gnus-summary-select-article nil nil nil number)
6763             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6764
6765 (defun gnus-summary-edit-parameters ()
6766   "Edit the group parameters of the current group."
6767   (interactive)
6768   (gnus-group-edit-group gnus-newsgroup-name 'params))
6769
6770 (defun gnus-summary-customize-parameters ()
6771   "Customize the group parameters of the current group."
6772   (interactive)
6773   (gnus-group-customize gnus-newsgroup-name))
6774
6775 (defun gnus-summary-enter-digest-group (&optional force)
6776   "Enter an nndoc group based on the current article.
6777 If FORCE, force a digest interpretation.  If not, try
6778 to guess what the document format is."
6779   (interactive "P")
6780   (let ((conf gnus-current-window-configuration))
6781     (save-excursion
6782       (gnus-summary-select-article))
6783     (setq gnus-current-window-configuration conf)
6784     (let* ((name (format "%s-%d"
6785                          (gnus-group-prefixed-name
6786                           gnus-newsgroup-name (list 'nndoc ""))
6787                          (save-excursion
6788                            (set-buffer gnus-summary-buffer)
6789                            gnus-current-article)))
6790            (ogroup gnus-newsgroup-name)
6791            (params (append (gnus-info-params (gnus-get-info ogroup))
6792                            (list (cons 'to-group ogroup))
6793                            (list (cons 'save-article-group ogroup))))
6794            (case-fold-search t)
6795            (buf (current-buffer))
6796            dig)
6797       (save-excursion
6798         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6799         (insert-buffer-substring gnus-original-article-buffer)
6800         ;; Remove lines that may lead nndoc to misinterpret the
6801         ;; document type.
6802         (narrow-to-region
6803          (goto-char (point-min))
6804          (or (search-forward "\n\n" nil t) (point)))
6805         (goto-char (point-min))
6806         (delete-matching-lines "^Path:\\|^From ")
6807         (widen))
6808       (unwind-protect
6809           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6810                     (gnus-newsgroup-ephemeral-ignored-charsets
6811                      gnus-newsgroup-ignored-charsets))
6812                 (gnus-group-read-ephemeral-group
6813                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6814                               (nndoc-article-type
6815                                ,(if force 'digest 'guess))) t))
6816               ;; Make all postings to this group go to the parent group.
6817               (nconc (gnus-info-params (gnus-get-info name))
6818                      params)
6819             ;; Couldn't select this doc group.
6820             (switch-to-buffer buf)
6821             (gnus-set-global-variables)
6822             (gnus-configure-windows 'summary)
6823             (gnus-message 3 "Article couldn't be entered?"))
6824         (kill-buffer dig)))))
6825
6826 (defun gnus-summary-read-document (n)
6827   "Open a new group based on the current article(s).
6828 This will allow you to read digests and other similar
6829 documents as newsgroups.
6830 Obeys the standard process/prefix convention."
6831   (interactive "P")
6832   (let* ((articles (gnus-summary-work-articles n))
6833          (ogroup gnus-newsgroup-name)
6834          (params (append (gnus-info-params (gnus-get-info ogroup))
6835                          (list (cons 'to-group ogroup))))
6836          article group egroup groups vgroup)
6837     (while (setq article (pop articles))
6838       (setq group (format "%s-%d" gnus-newsgroup-name article))
6839       (gnus-summary-remove-process-mark article)
6840       (when (gnus-summary-display-article article)
6841         (save-excursion
6842           (with-temp-buffer
6843             (insert-buffer-substring gnus-original-article-buffer)
6844             ;; Remove some headers that may lead nndoc to make
6845             ;; the wrong guess.
6846             (message-narrow-to-head)
6847             (goto-char (point-min))
6848             (delete-matching-lines "^\\(Path\\):\\|^From ")
6849             (widen)
6850             (if (setq egroup
6851                       (gnus-group-read-ephemeral-group
6852                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6853                                      (nndoc-article-type guess))
6854                        t nil t))
6855                 (progn
6856                   ;; Make all postings to this group go to the parent group.
6857                   (nconc (gnus-info-params (gnus-get-info egroup))
6858                          params)
6859                   (push egroup groups))
6860               ;; Couldn't select this doc group.
6861               (gnus-error 3 "Article couldn't be entered"))))))
6862     ;; Now we have selected all the documents.
6863     (cond
6864      ((not groups)
6865       (error "None of the articles could be interpreted as documents"))
6866      ((gnus-group-read-ephemeral-group
6867        (setq vgroup (format
6868                      "nnvirtual:%s-%s" gnus-newsgroup-name
6869                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6870        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6871        t
6872        (cons (current-buffer) 'summary)))
6873      (t
6874       (error "Couldn't select virtual nndoc group")))))
6875
6876 (defun gnus-summary-isearch-article (&optional regexp-p)
6877   "Do incremental search forward on the current article.
6878 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6879   (interactive "P")
6880   (gnus-summary-select-article)
6881   (gnus-configure-windows 'article)
6882   (gnus-eval-in-buffer-window gnus-article-buffer
6883     (save-restriction
6884       (widen)
6885       (isearch-forward regexp-p))))
6886
6887 (defun gnus-summary-search-article-forward (regexp &optional backward)
6888   "Search for an article containing REGEXP forward.
6889 If BACKWARD, search backward instead."
6890   (interactive
6891    (list (read-string
6892           (format "Search article %s (regexp%s): "
6893                   (if current-prefix-arg "backward" "forward")
6894                   (if gnus-last-search-regexp
6895                       (concat ", default " gnus-last-search-regexp)
6896                     "")))
6897          current-prefix-arg))
6898   (if (string-equal regexp "")
6899       (setq regexp (or gnus-last-search-regexp ""))
6900     (setq gnus-last-search-regexp regexp))
6901   (if (gnus-summary-search-article regexp backward)
6902       (gnus-summary-show-thread)
6903     (error "Search failed: \"%s\"" regexp)))
6904
6905 (defun gnus-summary-search-article-backward (regexp)
6906   "Search for an article containing REGEXP backward."
6907   (interactive
6908    (list (read-string
6909           (format "Search article backward (regexp%s): "
6910                   (if gnus-last-search-regexp
6911                       (concat ", default " gnus-last-search-regexp)
6912                     "")))))
6913   (gnus-summary-search-article-forward regexp 'backward))
6914
6915 (defun gnus-summary-search-article (regexp &optional backward)
6916   "Search for an article containing REGEXP.
6917 Optional argument BACKWARD means do search for backward.
6918 `gnus-select-article-hook' is not called during the search."
6919   ;; We have to require this here to make sure that the following
6920   ;; dynamic binding isn't shadowed by autoloading.
6921   (require 'gnus-async)
6922   (require 'gnus-art)
6923   (let ((gnus-select-article-hook nil)  ;Disable hook.
6924         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6925         (gnus-use-article-prefetch nil)
6926         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6927         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6928         (sum (current-buffer))
6929         (gnus-display-mime-function nil)
6930         (found nil)
6931         point)
6932     (gnus-save-hidden-threads
6933       (gnus-summary-select-article)
6934       (set-buffer gnus-article-buffer)
6935       (when backward
6936         (forward-line -1))
6937       (while (not found)
6938         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6939         (if (if backward
6940                 (re-search-backward regexp nil t)
6941               (re-search-forward regexp nil t))
6942             ;; We found the regexp.
6943             (progn
6944               (setq found 'found)
6945               (beginning-of-line)
6946               (set-window-start
6947                (get-buffer-window (current-buffer))
6948                (point))
6949               (forward-line 1)
6950               (set-buffer sum)
6951               (setq point (point)))
6952           ;; We didn't find it, so we go to the next article.
6953           (set-buffer sum)
6954           (setq found 'not)
6955           (while (eq found 'not)
6956             (if (not (if backward (gnus-summary-find-prev)
6957                        (gnus-summary-find-next)))
6958                 ;; No more articles.
6959                 (setq found t)
6960               ;; Select the next article and adjust point.
6961               (unless (gnus-summary-article-sparse-p
6962                        (gnus-summary-article-number))
6963                 (setq found nil)
6964                 (gnus-summary-select-article)
6965                 (set-buffer gnus-article-buffer)
6966                 (widen)
6967                 (goto-char (if backward (point-max) (point-min))))))))
6968       (gnus-message 7 ""))
6969     ;; Return whether we found the regexp.
6970     (when (eq found 'found)
6971       (goto-char point)
6972       (gnus-summary-show-thread)
6973       (gnus-summary-goto-subject gnus-current-article)
6974       (gnus-summary-position-point)
6975       t)))
6976
6977 (defun gnus-summary-find-matching (header regexp &optional backward unread
6978                                           not-case-fold)
6979   "Return a list of all articles that match REGEXP on HEADER.
6980 The search stars on the current article and goes forwards unless
6981 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6982 If UNREAD is non-nil, only unread articles will
6983 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6984 in the comparisons."
6985   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6986                 (gnus-data-find-list
6987                  (gnus-summary-article-number) (gnus-data-list backward))))
6988         (case-fold-search (not not-case-fold))
6989         articles d func)
6990     (if (consp header)
6991         (if (eq (car header) 'extra)
6992             (setq func
6993                   `(lambda (h)
6994                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6995                          "")))
6996           (error "%s is an invalid header" header))
6997       (unless (fboundp (intern (concat "mail-header-" header)))
6998         (error "%s is not a valid header" header))
6999       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7000     (while data
7001       (setq d (car data))
7002       (and (or (not unread)             ; We want all articles...
7003                (gnus-data-unread-p d))  ; Or just unreads.
7004            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7005            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7006            (push (gnus-data-number d) articles)) ; Success!
7007       (setq data (cdr data)))
7008     (nreverse articles)))
7009
7010 (defun gnus-summary-execute-command (header regexp command &optional backward)
7011   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7012 If HEADER is an empty string (or nil), the match is done on the entire
7013 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7014   (interactive
7015    (list (let ((completion-ignore-case t))
7016            (completing-read
7017             "Header name: "
7018             (mapcar (lambda (string) (list string))
7019                     '("Number" "Subject" "From" "Lines" "Date"
7020                       "Message-ID" "Xref" "References" "Body"))
7021             nil 'require-match))
7022          (read-string "Regexp: ")
7023          (read-key-sequence "Command: ")
7024          current-prefix-arg))
7025   (when (equal header "Body")
7026     (setq header ""))
7027   ;; Hidden thread subtrees must be searched as well.
7028   (gnus-summary-show-all-threads)
7029   ;; We don't want to change current point nor window configuration.
7030   (save-excursion
7031     (save-window-excursion
7032       (gnus-message 6 "Executing %s..." (key-description command))
7033       ;; We'd like to execute COMMAND interactively so as to give arguments.
7034       (gnus-execute header regexp
7035                     `(call-interactively ',(key-binding command))
7036                     backward)
7037       (gnus-message 6 "Executing %s...done" (key-description command)))))
7038
7039 (defun gnus-summary-beginning-of-article ()
7040   "Scroll the article back to the beginning."
7041   (interactive)
7042   (gnus-summary-select-article)
7043   (gnus-configure-windows 'article)
7044   (gnus-eval-in-buffer-window gnus-article-buffer
7045     (widen)
7046     (goto-char (point-min))
7047     (when gnus-page-broken
7048       (gnus-narrow-to-page))))
7049
7050 (defun gnus-summary-end-of-article ()
7051   "Scroll to the end of the article."
7052   (interactive)
7053   (gnus-summary-select-article)
7054   (gnus-configure-windows 'article)
7055   (gnus-eval-in-buffer-window gnus-article-buffer
7056     (widen)
7057     (goto-char (point-max))
7058     (recenter -3)
7059     (when gnus-page-broken
7060       (gnus-narrow-to-page))))
7061
7062 (defun gnus-summary-print-article (&optional filename n)
7063   "Generate and print a PostScript image of the N next (mail) articles.
7064
7065 If N is negative, print the N previous articles.  If N is nil and articles
7066 have been marked with the process mark, print these instead.
7067
7068 If the optional first argument FILENAME is nil, send the image to the
7069 printer.  If FILENAME is a string, save the PostScript image in a file with
7070 that name.  If FILENAME is a number, prompt the user for the name of the file
7071 to save in."
7072   (interactive (list (ps-print-preprint current-prefix-arg)
7073                      current-prefix-arg))
7074   (dolist (article (gnus-summary-work-articles n))
7075     (gnus-summary-select-article nil nil 'pseudo article)
7076     (gnus-eval-in-buffer-window gnus-article-buffer
7077       (let ((buffer (generate-new-buffer " *print*")))
7078         (unwind-protect
7079             (progn
7080               (copy-to-buffer buffer (point-min) (point-max))
7081               (set-buffer buffer)
7082               (gnus-article-delete-invisible-text)
7083               (let ((ps-left-header
7084                      (list
7085                       (concat "("
7086                               (mail-header-subject gnus-current-headers) ")")
7087                       (concat "("
7088                               (mail-header-from gnus-current-headers) ")")))
7089                     (ps-right-header
7090                      (list
7091                       "/pagenumberstring load"
7092                       (concat "("
7093                               (mail-header-date gnus-current-headers) ")"))))
7094                 (gnus-run-hooks 'gnus-ps-print-hook)
7095                 (save-excursion
7096                   (ps-print-buffer-with-faces filename))))
7097           (kill-buffer buffer))))))
7098
7099 (defun gnus-summary-show-article (&optional arg)
7100   "Force re-fetching of the current article.
7101 If ARG (the prefix) is non-nil, show the raw article without any
7102 article massaging functions being run."
7103   (interactive "P")
7104   (if (not arg)
7105       ;; Select the article the normal way.
7106       (gnus-summary-select-article nil 'force)
7107     ;; We have to require this here to make sure that the following
7108     ;; dynamic binding isn't shadowed by autoloading.
7109     (require 'gnus-async)
7110     (require 'gnus-art)
7111     ;; Bind the article treatment functions to nil.
7112     (let ((gnus-have-all-headers t)
7113           gnus-article-prepare-hook
7114           gnus-article-decode-hook
7115           gnus-display-mime-function
7116           gnus-break-pages
7117           gnus-visual)
7118       ;; Destroy any MIME parts.
7119       (when (gnus-buffer-live-p gnus-article-buffer)
7120         (save-excursion
7121           (set-buffer gnus-article-buffer)
7122           (mm-destroy-parts gnus-article-mime-handles)))
7123       (gnus-summary-select-article nil 'force)))
7124   (gnus-summary-goto-subject gnus-current-article)
7125   (gnus-summary-position-point))
7126
7127 (defun gnus-summary-verbose-headers (&optional arg)
7128   "Toggle permanent full header display.
7129 If ARG is a positive number, turn header display on.
7130 If ARG is a negative number, turn header display off."
7131   (interactive "P")
7132   (setq gnus-show-all-headers
7133         (cond ((or (not (numberp arg))
7134                    (zerop arg))
7135                (not gnus-show-all-headers))
7136               ((natnump arg)
7137                t)))
7138   (gnus-summary-show-article))
7139
7140 (defun gnus-summary-toggle-header (&optional arg)
7141   "Show the headers if they are hidden, or hide them if they are shown.
7142 If ARG is a positive number, show the entire header.
7143 If ARG is a negative number, hide the unwanted header lines."
7144   (interactive "P")
7145   (save-excursion
7146     (set-buffer gnus-article-buffer)
7147     (save-restriction
7148       (let* ((buffer-read-only nil)
7149              (inhibit-point-motion-hooks t)
7150              hidden e)
7151         (save-restriction 
7152           (article-narrow-to-head)
7153           (setq hidden (gnus-article-hidden-text-p 'headers)))
7154         (goto-char (point-min))
7155         (when (search-forward "\n\n" nil t)
7156           (delete-region (point-min) (1- (point))))
7157         (goto-char (point-min))
7158         (save-excursion
7159           (set-buffer gnus-original-article-buffer)
7160           (goto-char (point-min))
7161           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7162         (insert-buffer-substring gnus-original-article-buffer 1 e)
7163         (save-restriction
7164           (narrow-to-region (point-min) (point))
7165           (article-decode-encoded-words)
7166           (if (or hidden
7167                   (and (numberp arg) (< arg 0)))
7168               (let ((gnus-treat-hide-headers nil)
7169                     (gnus-treat-hide-boring-headers nil))
7170                 (gnus-treat-article 'head))
7171             (gnus-treat-article 'head)))))))
7172
7173 (defun gnus-summary-show-all-headers ()
7174   "Make all header lines visible."
7175   (interactive)
7176   (gnus-article-show-all-headers))
7177
7178 (defun gnus-summary-caesar-message (&optional arg)
7179   "Caesar rotate the current article by 13.
7180 The numerical prefix specifies how many places to rotate each letter
7181 forward."
7182   (interactive "P")
7183   (gnus-summary-select-article)
7184   (let ((mail-header-separator ""))
7185     (gnus-eval-in-buffer-window gnus-article-buffer
7186       (save-restriction
7187         (widen)
7188         (let ((start (window-start))
7189               buffer-read-only)
7190           (message-caesar-buffer-body arg)
7191           (set-window-start (get-buffer-window (current-buffer)) start))))))
7192
7193 (defun gnus-summary-stop-page-breaking ()
7194   "Stop page breaking in the current article."
7195   (interactive)
7196   (gnus-summary-select-article)
7197   (gnus-eval-in-buffer-window gnus-article-buffer
7198     (widen)
7199     (when (gnus-visual-p 'page-marker)
7200       (let ((buffer-read-only nil))
7201         (gnus-remove-text-with-property 'gnus-prev)
7202         (gnus-remove-text-with-property 'gnus-next))
7203       (setq gnus-page-broken nil))))
7204
7205 (defun gnus-summary-move-article (&optional n to-newsgroup
7206                                             select-method action)
7207   "Move the current article to a different newsgroup.
7208 If N is a positive number, move the N next articles.
7209 If N is a negative number, move the N previous articles.
7210 If N is nil and any articles have been marked with the process mark,
7211 move those articles instead.
7212 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7213 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7214 re-spool using this method.
7215
7216 For this function to work, both the current newsgroup and the
7217 newsgroup that you want to move to have to support the `request-move'
7218 and `request-accept' functions."
7219   (interactive "P")
7220   (unless action
7221     (setq action 'move))
7222   ;; Disable marking as read.
7223   (let (gnus-mark-article-hook)
7224     (save-window-excursion
7225       (gnus-summary-select-article)))
7226   ;; Check whether the source group supports the required functions.
7227   (cond ((and (eq action 'move)
7228               (not (gnus-check-backend-function
7229                     'request-move-article gnus-newsgroup-name)))
7230          (error "The current group does not support article moving"))
7231         ((and (eq action 'crosspost)
7232               (not (gnus-check-backend-function
7233                     'request-replace-article gnus-newsgroup-name)))
7234          (error "The current group does not support article editing")))
7235   (let ((articles (gnus-summary-work-articles n))
7236         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7237         (names '((move "Move" "Moving")
7238                  (copy "Copy" "Copying")
7239                  (crosspost "Crosspost" "Crossposting")))
7240         (copy-buf (save-excursion
7241                     (nnheader-set-temp-buffer " *copy article*")))
7242         art-group to-method new-xref article to-groups)
7243     (unless (assq action names)
7244       (error "Unknown action %s" action))
7245     ;; Read the newsgroup name.
7246     (when (and (not to-newsgroup)
7247                (not select-method))
7248       (setq to-newsgroup
7249             (gnus-read-move-group-name
7250              (cadr (assq action names))
7251              (symbol-value (intern (format "gnus-current-%s-group" action)))
7252              articles prefix))
7253       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7254     (setq to-method (or select-method
7255                         (gnus-group-name-to-method to-newsgroup)))
7256     ;; Check the method we are to move this article to...
7257     (unless (gnus-check-backend-function
7258              'request-accept-article (car to-method))
7259       (error "%s does not support article copying" (car to-method)))
7260     (unless (gnus-check-server to-method)
7261       (error "Can't open server %s" (car to-method)))
7262     (gnus-message 6 "%s to %s: %s..."
7263                   (caddr (assq action names))
7264                   (or (car select-method) to-newsgroup) articles)
7265     (while articles
7266       (setq article (pop articles))
7267       (setq
7268        art-group
7269        (cond
7270         ;; Move the article.
7271         ((eq action 'move)
7272          ;; Remove this article from future suppression.
7273          (gnus-dup-unsuppress-article article)
7274          (gnus-request-move-article
7275           article                       ; Article to move
7276           gnus-newsgroup-name           ; From newsgroup
7277           (nth 1 (gnus-find-method-for-group
7278                   gnus-newsgroup-name)) ; Server
7279           (list 'gnus-request-accept-article
7280                 to-newsgroup (list 'quote select-method)
7281                 (not articles) t)               ; Accept form
7282           (not articles)))              ; Only save nov last time
7283         ;; Copy the article.
7284         ((eq action 'copy)
7285          (save-excursion
7286            (set-buffer copy-buf)
7287            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7288              (gnus-request-accept-article
7289               to-newsgroup select-method (not articles) t))))
7290         ;; Crosspost the article.
7291         ((eq action 'crosspost)
7292          (let ((xref (message-tokenize-header
7293                       (mail-header-xref (gnus-summary-article-header article))
7294                       " ")))
7295            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7296                                   ":" article))
7297            (unless xref
7298              (setq xref (list (system-name))))
7299            (setq new-xref
7300                  (concat
7301                   (mapconcat 'identity
7302                              (delete "Xref:" (delete new-xref xref))
7303                              " ")
7304                   " " new-xref))
7305            (save-excursion
7306              (set-buffer copy-buf)
7307              ;; First put the article in the destination group.
7308              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7309              (when (consp (setq art-group
7310                                 (gnus-request-accept-article
7311                                  to-newsgroup select-method (not articles))))
7312                (setq new-xref (concat new-xref " " (car art-group)
7313                                       ":" (cdr art-group)))
7314                ;; Now we have the new Xrefs header, so we insert
7315                ;; it and replace the new article.
7316                (nnheader-replace-header "Xref" new-xref)
7317                (gnus-request-replace-article
7318                 (cdr art-group) to-newsgroup (current-buffer))
7319                art-group))))))
7320       (cond
7321        ((not art-group)
7322        (gnus-message 1 "Couldn't %s article %s: %s"
7323                      (cadr (assq action names)) article
7324                      (nnheader-get-report (car to-method))))
7325        ((and (eq art-group 'junk)
7326              (eq action 'move))
7327         (gnus-summary-mark-article article gnus-canceled-mark)
7328         (gnus-message 4 "Deleted article %s" article))
7329        (t
7330         (let* ((pto-group (gnus-group-prefixed-name
7331                            (car art-group) to-method))
7332                (entry
7333                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7334                (info (nth 2 entry))
7335                (to-group (gnus-info-group info))
7336                to-marks)
7337           ;; Update the group that has been moved to.
7338           (when (and info
7339                      (memq action '(move copy)))
7340             (unless (member to-group to-groups)
7341               (push to-group to-groups))
7342
7343             (unless (memq article gnus-newsgroup-unreads)
7344               (push 'read to-marks)
7345               (gnus-info-set-read
7346                info (gnus-add-to-range (gnus-info-read info)
7347                                        (list (cdr art-group)))))
7348
7349             ;; Copy any marks over to the new group.
7350             (let ((marks gnus-article-mark-lists)
7351                   (to-article (cdr art-group)))
7352
7353               ;; See whether the article is to be put in the cache.
7354               (when gnus-use-cache
7355                 (gnus-cache-possibly-enter-article
7356                  to-group to-article
7357                  (memq article gnus-newsgroup-marked)
7358                  (memq article gnus-newsgroup-dormant)
7359                  (memq article gnus-newsgroup-unreads)))
7360
7361               (when (and (equal to-group gnus-newsgroup-name)
7362                          (not (memq article gnus-newsgroup-unreads)))
7363                 ;; Mark this article as read in this group.
7364                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7365                 (setcdr (gnus-active to-group) to-article)
7366                 (setcdr gnus-newsgroup-active to-article))
7367
7368               (while marks
7369                 (when (memq article (symbol-value
7370                                      (intern (format "gnus-newsgroup-%s"
7371                                                      (caar marks)))))
7372                   (push (cdar marks) to-marks)
7373                   ;; If the other group is the same as this group,
7374                   ;; then we have to add the mark to the list.
7375                   (when (equal to-group gnus-newsgroup-name)
7376                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7377                          (cons to-article
7378                                (symbol-value
7379                                 (intern (format "gnus-newsgroup-%s"
7380                                                 (caar marks)))))))
7381                   ;; Copy the marks to other group.
7382                   (gnus-add-marked-articles
7383                    to-group (cdar marks) (list to-article) info))
7384                 (setq marks (cdr marks)))
7385
7386               (gnus-request-set-mark to-group (list (list (list to-article)
7387                                                           'set
7388                                                           to-marks)))
7389
7390               (gnus-dribble-enter
7391                (concat "(gnus-group-set-info '"
7392                        (gnus-prin1-to-string (gnus-get-info to-group))
7393                        ")"))))
7394
7395           ;; Update the Xref header in this article to point to
7396           ;; the new crossposted article we have just created.
7397           (when (eq action 'crosspost)
7398             (save-excursion
7399               (set-buffer copy-buf)
7400               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7401               (nnheader-replace-header "Xref" new-xref)
7402               (gnus-request-replace-article
7403                article gnus-newsgroup-name (current-buffer)))))
7404
7405         ;;;!!!Why is this necessary?
7406         (set-buffer gnus-summary-buffer)
7407
7408         (gnus-summary-goto-subject article)
7409         (when (eq action 'move)
7410           (gnus-summary-mark-article article gnus-canceled-mark))))
7411       (gnus-summary-remove-process-mark article))
7412     ;; Re-activate all groups that have been moved to.
7413     (while to-groups
7414       (save-excursion
7415         (set-buffer gnus-group-buffer)
7416         (when (gnus-group-goto-group (car to-groups) t)
7417           (gnus-group-get-new-news-this-group 1 t))
7418         (pop to-groups)))
7419
7420     (gnus-kill-buffer copy-buf)
7421     (gnus-summary-position-point)
7422     (gnus-set-mode-line 'summary)))
7423
7424 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7425   "Move the current article to a different newsgroup.
7426 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7427 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7428 re-spool using this method."
7429   (interactive "P")
7430   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7431
7432 (defun gnus-summary-crosspost-article (&optional n)
7433   "Crosspost the current article to some other group."
7434   (interactive "P")
7435   (gnus-summary-move-article n nil nil 'crosspost))
7436
7437 (defcustom gnus-summary-respool-default-method nil
7438   "Default method for respooling an article.
7439 If nil, use to the current newsgroup method."
7440   :type '(choice (gnus-select-method :value (nnml ""))
7441                  (const nil))
7442   :group 'gnus-summary-mail)
7443
7444 (defun gnus-summary-respool-article (&optional n method)
7445   "Respool the current article.
7446 The article will be squeezed through the mail spooling process again,
7447 which means that it will be put in some mail newsgroup or other
7448 depending on `nnmail-split-methods'.
7449 If N is a positive number, respool the N next articles.
7450 If N is a negative number, respool the N previous articles.
7451 If N is nil and any articles have been marked with the process mark,
7452 respool those articles instead.
7453
7454 Respooling can be done both from mail groups and \"real\" newsgroups.
7455 In the former case, the articles in question will be moved from the
7456 current group into whatever groups they are destined to.  In the
7457 latter case, they will be copied into the relevant groups."
7458   (interactive
7459    (list current-prefix-arg
7460          (let* ((methods (gnus-methods-using 'respool))
7461                 (methname
7462                  (symbol-name (or gnus-summary-respool-default-method
7463                                   (car (gnus-find-method-for-group
7464                                         gnus-newsgroup-name)))))
7465                 (method
7466                  (gnus-completing-read
7467                   methname "What backend do you want to use when respooling?"
7468                   methods nil t nil 'gnus-mail-method-history))
7469                 ms)
7470            (cond
7471             ((zerop (length (setq ms (gnus-servers-using-backend
7472                                       (intern method)))))
7473              (list (intern method) ""))
7474             ((= 1 (length ms))
7475              (car ms))
7476             (t
7477              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7478                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7479                            ms-alist))))))))
7480   (unless method
7481     (error "No method given for respooling"))
7482   (if (assoc (symbol-name
7483               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7484              (gnus-methods-using 'respool))
7485       (gnus-summary-move-article n nil method)
7486     (gnus-summary-copy-article n nil method)))
7487
7488 (defun gnus-summary-import-article (file)
7489   "Import an arbitrary file into a mail newsgroup."
7490   (interactive "fImport file: ")
7491   (let ((group gnus-newsgroup-name)
7492         (now (current-time))
7493         atts lines)
7494     (unless (gnus-check-backend-function 'request-accept-article group)
7495       (error "%s does not support article importing" group))
7496     (or (file-readable-p file)
7497         (not (file-regular-p file))
7498         (error "Can't read %s" file))
7499     (save-excursion
7500       (set-buffer (gnus-get-buffer-create " *import file*"))
7501       (erase-buffer)
7502       (insert-file-contents file)
7503       (goto-char (point-min))
7504       (unless (nnheader-article-p)
7505         ;; This doesn't look like an article, so we fudge some headers.
7506         (setq atts (file-attributes file)
7507               lines (count-lines (point-min) (point-max)))
7508         (insert "From: " (read-string "From: ") "\n"
7509                 "Subject: " (read-string "Subject: ") "\n"
7510                 "Date: " (message-make-date (nth 5 atts))
7511                 "\n"
7512                 "Message-ID: " (message-make-message-id) "\n"
7513                 "Lines: " (int-to-string lines) "\n"
7514                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7515       (gnus-request-accept-article group nil t)
7516       (kill-buffer (current-buffer)))))
7517
7518 (defun gnus-summary-article-posted-p ()
7519   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7520 This will be the case if the article has both been mailed and posted."
7521   (interactive)
7522   (let ((id (mail-header-references (gnus-summary-article-header)))
7523         (gnus-override-method
7524          (or gnus-refer-article-method gnus-select-method)))
7525     (if (gnus-request-head id "")
7526         (gnus-message 2 "The current message was found on %s"
7527                       gnus-override-method)
7528       (gnus-message 2 "The current message couldn't be found on %s"
7529                     gnus-override-method)
7530       nil)))
7531
7532 (defun gnus-summary-expire-articles (&optional now)
7533   "Expire all articles that are marked as expirable in the current group."
7534   (interactive)
7535   (when (gnus-check-backend-function
7536          'request-expire-articles gnus-newsgroup-name)
7537     ;; This backend supports expiry.
7538     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7539            (expirable (if total
7540                           (progn
7541                             ;; We need to update the info for
7542                             ;; this group for `gnus-list-of-read-articles'
7543                             ;; to give us the right answer.
7544                             (gnus-run-hooks 'gnus-exit-group-hook)
7545                             (gnus-summary-update-info)
7546                             (gnus-list-of-read-articles gnus-newsgroup-name))
7547                         (setq gnus-newsgroup-expirable
7548                               (sort gnus-newsgroup-expirable '<))))
7549            (expiry-wait (if now 'immediate
7550                           (gnus-group-find-parameter
7551                            gnus-newsgroup-name 'expiry-wait)))
7552            es)
7553       (when expirable
7554         ;; There are expirable articles in this group, so we run them
7555         ;; through the expiry process.
7556         (gnus-message 6 "Expiring articles...")
7557         (unless (gnus-check-group gnus-newsgroup-name)
7558           (error "Can't open server for %s" gnus-newsgroup-name))
7559         ;; The list of articles that weren't expired is returned.
7560         (save-excursion
7561           (if expiry-wait
7562               (let ((nnmail-expiry-wait-function nil)
7563                     (nnmail-expiry-wait expiry-wait))
7564                 (setq es (gnus-request-expire-articles
7565                           expirable gnus-newsgroup-name)))
7566             (setq es (gnus-request-expire-articles
7567                       expirable gnus-newsgroup-name))))
7568         (unless total
7569           (setq gnus-newsgroup-expirable es))
7570         ;; We go through the old list of expirable, and mark all
7571         ;; really expired articles as nonexistent.
7572         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7573           (let ((gnus-use-cache nil))
7574             (while expirable
7575               (unless (memq (car expirable) es)
7576                 (when (gnus-data-find (car expirable))
7577                   (gnus-summary-mark-article
7578                    (car expirable) gnus-canceled-mark)))
7579               (setq expirable (cdr expirable)))))
7580         (gnus-message 6 "Expiring articles...done")))))
7581
7582 (defun gnus-summary-expire-articles-now ()
7583   "Expunge all expirable articles in the current group.
7584 This means that *all* articles that are marked as expirable will be
7585 deleted forever, right now."
7586   (interactive)
7587   (or gnus-expert-user
7588       (gnus-yes-or-no-p
7589        "Are you really, really, really sure you want to delete all these messages? ")
7590       (error "Phew!"))
7591   (gnus-summary-expire-articles t))
7592
7593 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7594 (defun gnus-summary-delete-article (&optional n)
7595   "Delete the N next (mail) articles.
7596 This command actually deletes articles.  This is not a marking
7597 command.  The article will disappear forever from your life, never to
7598 return.
7599 If N is negative, delete backwards.
7600 If N is nil and articles have been marked with the process mark,
7601 delete these instead."
7602   (interactive "P")
7603   (unless (gnus-check-backend-function 'request-expire-articles
7604                                        gnus-newsgroup-name)
7605     (error "The current newsgroup does not support article deletion"))
7606   ;; Compute the list of articles to delete.
7607   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7608         not-deleted)
7609     (if (and gnus-novice-user
7610              (not (gnus-yes-or-no-p
7611                    (format "Do you really want to delete %s forever? "
7612                            (if (> (length articles) 1)
7613                                (format "these %s articles" (length articles))
7614                              "this article")))))
7615         ()
7616       ;; Delete the articles.
7617       (setq not-deleted (gnus-request-expire-articles
7618                          articles gnus-newsgroup-name 'force))
7619       (while articles
7620         (gnus-summary-remove-process-mark (car articles))
7621         ;; The backend might not have been able to delete the article
7622         ;; after all.
7623         (unless (memq (car articles) not-deleted)
7624           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7625         (setq articles (cdr articles)))
7626       (when not-deleted
7627         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7628     (gnus-summary-position-point)
7629     (gnus-set-mode-line 'summary)
7630     not-deleted))
7631
7632 (defun gnus-summary-edit-article (&optional force)
7633   "Edit the current article.
7634 This will have permanent effect only in mail groups.
7635 If FORCE is non-nil, allow editing of articles even in read-only
7636 groups."
7637   (interactive "P")
7638   (save-excursion
7639     (set-buffer gnus-summary-buffer)
7640     (let ((mail-parse-charset gnus-newsgroup-charset)
7641           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7642       (gnus-set-global-variables)
7643       (when (and (not force)
7644                  (gnus-group-read-only-p))
7645         (error "The current newsgroup does not support article editing"))
7646       (gnus-summary-show-article t)
7647       (gnus-article-edit-article
7648        'mime-to-mml
7649        `(lambda (no-highlight)
7650           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7651                 (mail-parse-ignored-charsets 
7652                  ',gnus-newsgroup-ignored-charsets))
7653             (mml-to-mime)
7654             (gnus-summary-edit-article-done
7655              ,(or (mail-header-references gnus-current-headers) "")
7656              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7657
7658 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7659
7660 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7661                                                  no-highlight)
7662   "Make edits to the current article permanent."
7663   (interactive)
7664   ;; Replace the article.
7665   (let ((buf (current-buffer)))
7666     (with-temp-buffer
7667       (insert-buffer-substring buf)
7668       (if (and (not read-only)
7669                (not (gnus-request-replace-article
7670                      (cdr gnus-article-current) (car gnus-article-current)
7671                      (current-buffer) t)))
7672           (error "Couldn't replace article")
7673         ;; Update the summary buffer.
7674         (if (and references
7675                  (equal (message-tokenize-header references " ")
7676                         (message-tokenize-header
7677                          (or (message-fetch-field "references") "") " ")))
7678             ;; We only have to update this line.
7679             (save-excursion
7680               (save-restriction
7681                 (message-narrow-to-head)
7682                 (let ((head (buffer-string))
7683                       header)
7684                   (with-temp-buffer
7685                     (insert (format "211 %d Article retrieved.\n"
7686                                     (cdr gnus-article-current)))
7687                     (insert head)
7688                     (insert ".\n")
7689                     (let ((nntp-server-buffer (current-buffer)))
7690                       (setq header (car (gnus-get-newsgroup-headers
7691                                          (save-excursion
7692                                            (set-buffer gnus-summary-buffer)
7693                                            gnus-newsgroup-dependencies)
7694                                          t))))
7695                     (save-excursion
7696                       (set-buffer gnus-summary-buffer)
7697                       (gnus-data-set-header
7698                        (gnus-data-find (cdr gnus-article-current))
7699                        header)
7700                       (gnus-summary-update-article-line
7701                        (cdr gnus-article-current) header))))))
7702           ;; Update threads.
7703           (set-buffer (or buffer gnus-summary-buffer))
7704           (gnus-summary-update-article (cdr gnus-article-current)))
7705         ;; Prettify the article buffer again.
7706         (unless no-highlight
7707           (save-excursion
7708             (set-buffer gnus-article-buffer)
7709             ;;;!!! Fix this -- article should be rehighlighted.
7710             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7711             (set-buffer gnus-original-article-buffer)
7712             (gnus-request-article
7713              (cdr gnus-article-current)
7714              (car gnus-article-current) (current-buffer))))
7715         ;; Prettify the summary buffer line.
7716         (when (gnus-visual-p 'summary-highlight 'highlight)
7717           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7718
7719 (defun gnus-summary-edit-wash (key)
7720   "Perform editing command KEY in the article buffer."
7721   (interactive
7722    (list
7723     (progn
7724       (message "%s" (concat (this-command-keys) "- "))
7725       (read-char))))
7726   (message "")
7727   (gnus-summary-edit-article)
7728   (execute-kbd-macro (concat (this-command-keys) key))
7729   (gnus-article-edit-done))
7730
7731 ;;; Respooling
7732
7733 (defun gnus-summary-respool-query (&optional silent trace)
7734   "Query where the respool algorithm would put this article."
7735   (interactive)
7736   (let (gnus-mark-article-hook)
7737     (gnus-summary-select-article)
7738     (save-excursion
7739       (set-buffer gnus-original-article-buffer)
7740       (save-restriction
7741         (message-narrow-to-head)
7742         (let ((groups (nnmail-article-group 'identity trace)))
7743           (unless silent
7744             (if groups
7745                 (message "This message would go to %s"
7746                          (mapconcat 'car groups ", "))
7747               (message "This message would go to no groups"))
7748             groups))))))
7749
7750 (defun gnus-summary-respool-trace ()
7751   "Trace where the respool algorithm would put this article.
7752 Display a buffer showing all fancy splitting patterns which matched."
7753   (interactive)
7754   (gnus-summary-respool-query nil t))
7755
7756 ;; Summary marking commands.
7757
7758 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7759   "Mark articles which has the same subject as read, and then select the next.
7760 If UNMARK is positive, remove any kind of mark.
7761 If UNMARK is negative, tick articles."
7762   (interactive "P")
7763   (when unmark
7764     (setq unmark (prefix-numeric-value unmark)))
7765   (let ((count
7766          (gnus-summary-mark-same-subject
7767           (gnus-summary-article-subject) unmark)))
7768     ;; Select next unread article.  If auto-select-same mode, should
7769     ;; select the first unread article.
7770     (gnus-summary-next-article t (and gnus-auto-select-same
7771                                       (gnus-summary-article-subject)))
7772     (gnus-message 7 "%d article%s marked as %s"
7773                   count (if (= count 1) " is" "s are")
7774                   (if unmark "unread" "read"))))
7775
7776 (defun gnus-summary-kill-same-subject (&optional unmark)
7777   "Mark articles which has the same subject as read.
7778 If UNMARK is positive, remove any kind of mark.
7779 If UNMARK is negative, tick articles."
7780   (interactive "P")
7781   (when unmark
7782     (setq unmark (prefix-numeric-value unmark)))
7783   (let ((count
7784          (gnus-summary-mark-same-subject
7785           (gnus-summary-article-subject) unmark)))
7786     ;; If marked as read, go to next unread subject.
7787     (when (null unmark)
7788       ;; Go to next unread subject.
7789       (gnus-summary-next-subject 1 t))
7790     (gnus-message 7 "%d articles are marked as %s"
7791                   count (if unmark "unread" "read"))))
7792
7793 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7794   "Mark articles with same SUBJECT as read, and return marked number.
7795 If optional argument UNMARK is positive, remove any kinds of marks.
7796 If optional argument UNMARK is negative, mark articles as unread instead."
7797   (let ((count 1))
7798     (save-excursion
7799       (cond
7800        ((null unmark)                   ; Mark as read.
7801         (while (and
7802                 (progn
7803                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7804                   (gnus-summary-show-thread) t)
7805                 (gnus-summary-find-subject subject))
7806           (setq count (1+ count))))
7807        ((> unmark 0)                    ; Tick.
7808         (while (and
7809                 (progn
7810                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7811                   (gnus-summary-show-thread) t)
7812                 (gnus-summary-find-subject subject))
7813           (setq count (1+ count))))
7814        (t                               ; Mark as unread.
7815         (while (and
7816                 (progn
7817                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7818                   (gnus-summary-show-thread) t)
7819                 (gnus-summary-find-subject subject))
7820           (setq count (1+ count)))))
7821       (gnus-set-mode-line 'summary)
7822       ;; Return the number of marked articles.
7823       count)))
7824
7825 (defun gnus-summary-mark-as-processable (n &optional unmark)
7826   "Set the process mark on the next N articles.
7827 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7828 the process mark instead.  The difference between N and the actual
7829 number of articles marked is returned."
7830   (interactive "p")
7831   (let ((backward (< n 0))
7832         (n (abs n)))
7833     (while (and
7834             (> n 0)
7835             (if unmark
7836                 (gnus-summary-remove-process-mark
7837                  (gnus-summary-article-number))
7838               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7839             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7840       (setq n (1- n)))
7841     (when (/= 0 n)
7842       (gnus-message 7 "No more articles"))
7843     (gnus-summary-recenter)
7844     (gnus-summary-position-point)
7845     n))
7846
7847 (defun gnus-summary-unmark-as-processable (n)
7848   "Remove the process mark from the next N articles.
7849 If N is negative, unmark backward instead.  The difference between N and
7850 the actual number of articles unmarked is returned."
7851   (interactive "p")
7852   (gnus-summary-mark-as-processable n t))
7853
7854 (defun gnus-summary-unmark-all-processable ()
7855   "Remove the process mark from all articles."
7856   (interactive)
7857   (save-excursion
7858     (while gnus-newsgroup-processable
7859       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7860   (gnus-summary-position-point))
7861
7862 (defun gnus-summary-mark-as-expirable (n)
7863   "Mark N articles forward as expirable.
7864 If N is negative, mark backward instead.  The difference between N and
7865 the actual number of articles marked is returned."
7866   (interactive "p")
7867   (gnus-summary-mark-forward n gnus-expirable-mark))
7868
7869 (defun gnus-summary-mark-article-as-replied (article)
7870   "Mark ARTICLE replied and update the summary line."
7871   (push article gnus-newsgroup-replied)
7872   (let ((buffer-read-only nil))
7873     (when (gnus-summary-goto-subject article nil t)
7874       (gnus-summary-update-secondary-mark article))))
7875
7876 (defun gnus-summary-set-bookmark (article)
7877   "Set a bookmark in current article."
7878   (interactive (list (gnus-summary-article-number)))
7879   (when (or (not (get-buffer gnus-article-buffer))
7880             (not gnus-current-article)
7881             (not gnus-article-current)
7882             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7883     (error "No current article selected"))
7884   ;; Remove old bookmark, if one exists.
7885   (let ((old (assq article gnus-newsgroup-bookmarks)))
7886     (when old
7887       (setq gnus-newsgroup-bookmarks
7888             (delq old gnus-newsgroup-bookmarks))))
7889   ;; Set the new bookmark, which is on the form
7890   ;; (article-number . line-number-in-body).
7891   (push
7892    (cons article
7893          (save-excursion
7894            (set-buffer gnus-article-buffer)
7895            (count-lines
7896             (min (point)
7897                  (save-excursion
7898                    (goto-char (point-min))
7899                    (search-forward "\n\n" nil t)
7900                    (point)))
7901             (point))))
7902    gnus-newsgroup-bookmarks)
7903   (gnus-message 6 "A bookmark has been added to the current article."))
7904
7905 (defun gnus-summary-remove-bookmark (article)
7906   "Remove the bookmark from the current article."
7907   (interactive (list (gnus-summary-article-number)))
7908   ;; Remove old bookmark, if one exists.
7909   (let ((old (assq article gnus-newsgroup-bookmarks)))
7910     (if old
7911         (progn
7912           (setq gnus-newsgroup-bookmarks
7913                 (delq old gnus-newsgroup-bookmarks))
7914           (gnus-message 6 "Removed bookmark."))
7915       (gnus-message 6 "No bookmark in current article."))))
7916
7917 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7918 (defun gnus-summary-mark-as-dormant (n)
7919   "Mark N articles forward as dormant.
7920 If N is negative, mark backward instead.  The difference between N and
7921 the actual number of articles marked is returned."
7922   (interactive "p")
7923   (gnus-summary-mark-forward n gnus-dormant-mark))
7924
7925 (defun gnus-summary-set-process-mark (article)
7926   "Set the process mark on ARTICLE and update the summary line."
7927   (setq gnus-newsgroup-processable
7928         (cons article
7929               (delq article gnus-newsgroup-processable)))
7930   (when (gnus-summary-goto-subject article)
7931     (gnus-summary-show-thread)
7932     (gnus-summary-goto-subject article)
7933     (gnus-summary-update-secondary-mark article)))
7934
7935 (defun gnus-summary-remove-process-mark (article)
7936   "Remove the process mark from ARTICLE and update the summary line."
7937   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7938   (when (gnus-summary-goto-subject article)
7939     (gnus-summary-show-thread)
7940     (gnus-summary-goto-subject article)
7941     (gnus-summary-update-secondary-mark article)))
7942
7943 (defun gnus-summary-set-saved-mark (article)
7944   "Set the process mark on ARTICLE and update the summary line."
7945   (push article gnus-newsgroup-saved)
7946   (when (gnus-summary-goto-subject article)
7947     (gnus-summary-update-secondary-mark article)))
7948
7949 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7950   "Mark N articles as read forwards.
7951 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7952 The difference between N and the actual number of articles marked is
7953 returned."
7954   (interactive "p")
7955   (gnus-summary-show-thread)
7956   (let ((backward (< n 0))
7957         (gnus-summary-goto-unread
7958          (and gnus-summary-goto-unread
7959               (not (eq gnus-summary-goto-unread 'never))
7960               (not (memq mark (list gnus-unread-mark
7961                                     gnus-ticked-mark gnus-dormant-mark)))))
7962         (n (abs n))
7963         (mark (or mark gnus-del-mark)))
7964     (while (and (> n 0)
7965                 (gnus-summary-mark-article nil mark no-expire)
7966                 (zerop (gnus-summary-next-subject
7967                         (if backward -1 1)
7968                         (and gnus-summary-goto-unread
7969                              (not (eq gnus-summary-goto-unread 'never)))
7970                         t)))
7971       (setq n (1- n)))
7972     (when (/= 0 n)
7973       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7974     (gnus-summary-recenter)
7975     (gnus-summary-position-point)
7976     (gnus-set-mode-line 'summary)
7977     n))
7978
7979 (defun gnus-summary-mark-article-as-read (mark)
7980   "Mark the current article quickly as read with MARK."
7981   (let ((article (gnus-summary-article-number)))
7982     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7983     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7984     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7985     (push (cons article mark) gnus-newsgroup-reads)
7986     ;; Possibly remove from cache, if that is used.
7987     (when gnus-use-cache
7988       (gnus-cache-enter-remove-article article))
7989     ;; Allow the backend to change the mark.
7990     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7991     ;; Check for auto-expiry.
7992     (when (and gnus-newsgroup-auto-expire
7993                (memq mark gnus-auto-expirable-marks))
7994       (setq mark gnus-expirable-mark)
7995       ;; Let the backend know about the mark change.
7996       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7997       (push article gnus-newsgroup-expirable))
7998     ;; Set the mark in the buffer.
7999     (gnus-summary-update-mark mark 'unread)
8000     t))
8001
8002 (defun gnus-summary-mark-article-as-unread (mark)
8003   "Mark the current article quickly as unread with MARK."
8004   (let* ((article (gnus-summary-article-number))
8005          (old-mark (gnus-summary-article-mark article)))
8006     ;; Allow the backend to change the mark.
8007     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8008     (if (eq mark old-mark)
8009         t
8010       (if (<= article 0)
8011           (progn
8012             (gnus-error 1 "Can't mark negative article numbers")
8013             nil)
8014         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8015         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8016         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8017         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8018         (cond ((= mark gnus-ticked-mark)
8019                (push article gnus-newsgroup-marked))
8020               ((= mark gnus-dormant-mark)
8021                (push article gnus-newsgroup-dormant))
8022               (t
8023                (push article gnus-newsgroup-unreads)))
8024         (gnus-pull article gnus-newsgroup-reads)
8025
8026         ;; See whether the article is to be put in the cache.
8027         (and gnus-use-cache
8028              (vectorp (gnus-summary-article-header article))
8029              (save-excursion
8030                (gnus-cache-possibly-enter-article
8031                 gnus-newsgroup-name article
8032                 (= mark gnus-ticked-mark)
8033                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8034
8035         ;; Fix the mark.
8036         (gnus-summary-update-mark mark 'unread)
8037         t))))
8038
8039 (defun gnus-summary-mark-article (&optional article mark no-expire)
8040   "Mark ARTICLE with MARK.  MARK can be any character.
8041 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8042 `??' (dormant) and `?E' (expirable).
8043 If MARK is nil, then the default character `?r' is used.
8044 If ARTICLE is nil, then the article on the current line will be
8045 marked."
8046   ;; The mark might be a string.
8047   (when (stringp mark)
8048     (setq mark (aref mark 0)))
8049   ;; If no mark is given, then we check auto-expiring.
8050   (when (null mark)
8051     (setq mark gnus-del-mark))
8052   (when (and (not no-expire)
8053              gnus-newsgroup-auto-expire
8054              (memq mark gnus-auto-expirable-marks))
8055     (setq mark gnus-expirable-mark))
8056   (let ((article (or article (gnus-summary-article-number)))
8057         (old-mark (gnus-summary-article-mark article)))
8058     ;; Allow the backend to change the mark.
8059     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8060     (if (eq mark old-mark)
8061         t
8062       (unless article
8063         (error "No article on current line"))
8064       (if (not (if (or (= mark gnus-unread-mark)
8065                        (= mark gnus-ticked-mark)
8066                        (= mark gnus-dormant-mark))
8067                    (gnus-mark-article-as-unread article mark)
8068                  (gnus-mark-article-as-read article mark)))
8069           t
8070         ;; See whether the article is to be put in the cache.
8071         (and gnus-use-cache
8072              (not (= mark gnus-canceled-mark))
8073              (vectorp (gnus-summary-article-header article))
8074              (save-excursion
8075                (gnus-cache-possibly-enter-article
8076                 gnus-newsgroup-name article
8077                 (= mark gnus-ticked-mark)
8078                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8079
8080         (when (gnus-summary-goto-subject article nil t)
8081           (let ((buffer-read-only nil))
8082             (gnus-summary-show-thread)
8083             ;; Fix the mark.
8084             (gnus-summary-update-mark mark 'unread)
8085             t))))))
8086
8087 (defun gnus-summary-update-secondary-mark (article)
8088   "Update the secondary (read, process, cache) mark."
8089   (gnus-summary-update-mark
8090    (cond ((memq article gnus-newsgroup-processable)
8091           gnus-process-mark)
8092          ((memq article gnus-newsgroup-cached)
8093           gnus-cached-mark)
8094          ((memq article gnus-newsgroup-replied)
8095           gnus-replied-mark)
8096          ((memq article gnus-newsgroup-saved)
8097           gnus-saved-mark)
8098          (t gnus-unread-mark))
8099    'replied)
8100   (when (gnus-visual-p 'summary-highlight 'highlight)
8101     (gnus-run-hooks 'gnus-summary-update-hook))
8102   t)
8103
8104 (defun gnus-summary-update-mark (mark type)
8105   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8106         (buffer-read-only nil))
8107     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8108     (when forward
8109       (when (looking-at "\r")
8110         (incf forward))
8111       (when (<= (+ forward (point)) (point-max))
8112         ;; Go to the right position on the line.
8113         (goto-char (+ forward (point)))
8114         ;; Replace the old mark with the new mark.
8115         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8116         ;; Optionally update the marks by some user rule.
8117         (when (eq type 'unread)
8118           (gnus-data-set-mark
8119            (gnus-data-find (gnus-summary-article-number)) mark)
8120           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8121
8122 (defun gnus-mark-article-as-read (article &optional mark)
8123   "Enter ARTICLE in the pertinent lists and remove it from others."
8124   ;; Make the article expirable.
8125   (let ((mark (or mark gnus-del-mark)))
8126     (if (= mark gnus-expirable-mark)
8127         (push article gnus-newsgroup-expirable)
8128       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8129     ;; Remove from unread and marked lists.
8130     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8131     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8132     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8133     (push (cons article mark) gnus-newsgroup-reads)
8134     ;; Possibly remove from cache, if that is used.
8135     (when gnus-use-cache
8136       (gnus-cache-enter-remove-article article))
8137     t))
8138
8139 (defun gnus-mark-article-as-unread (article &optional mark)
8140   "Enter ARTICLE in the pertinent lists and remove it from others."
8141   (let ((mark (or mark gnus-ticked-mark)))
8142     (if (<= article 0)
8143         (progn
8144           (gnus-error 1 "Can't mark negative article numbers")
8145           nil)
8146       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8147             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8148             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8149             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8150
8151       ;; Unsuppress duplicates?
8152       (when gnus-suppress-duplicates
8153         (gnus-dup-unsuppress-article article))
8154
8155       (cond ((= mark gnus-ticked-mark)
8156              (push article gnus-newsgroup-marked))
8157             ((= mark gnus-dormant-mark)
8158              (push article gnus-newsgroup-dormant))
8159             (t
8160              (push article gnus-newsgroup-unreads)))
8161       (gnus-pull article gnus-newsgroup-reads)
8162       t)))
8163
8164 (defalias 'gnus-summary-mark-as-unread-forward
8165   'gnus-summary-tick-article-forward)
8166 (make-obsolete 'gnus-summary-mark-as-unread-forward
8167                'gnus-summary-tick-article-forward)
8168 (defun gnus-summary-tick-article-forward (n)
8169   "Tick N articles forwards.
8170 If N is negative, tick backwards instead.
8171 The difference between N and the number of articles ticked is returned."
8172   (interactive "p")
8173   (gnus-summary-mark-forward n gnus-ticked-mark))
8174
8175 (defalias 'gnus-summary-mark-as-unread-backward
8176   'gnus-summary-tick-article-backward)
8177 (make-obsolete 'gnus-summary-mark-as-unread-backward
8178                'gnus-summary-tick-article-backward)
8179 (defun gnus-summary-tick-article-backward (n)
8180   "Tick N articles backwards.
8181 The difference between N and the number of articles ticked is returned."
8182   (interactive "p")
8183   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8184
8185 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8186 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8187 (defun gnus-summary-tick-article (&optional article clear-mark)
8188   "Mark current article as unread.
8189 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8190 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8191   (interactive)
8192   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8193                                        gnus-ticked-mark)))
8194
8195 (defun gnus-summary-mark-as-read-forward (n)
8196   "Mark N articles as read forwards.
8197 If N is negative, mark backwards instead.
8198 The difference between N and the actual number of articles marked is
8199 returned."
8200   (interactive "p")
8201   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8202
8203 (defun gnus-summary-mark-as-read-backward (n)
8204   "Mark the N articles as read backwards.
8205 The difference between N and the actual number of articles marked is
8206 returned."
8207   (interactive "p")
8208   (gnus-summary-mark-forward
8209    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8210
8211 (defun gnus-summary-mark-as-read (&optional article mark)
8212   "Mark current article as read.
8213 ARTICLE specifies the article to be marked as read.
8214 MARK specifies a string to be inserted at the beginning of the line."
8215   (gnus-summary-mark-article article mark))
8216
8217 (defun gnus-summary-clear-mark-forward (n)
8218   "Clear marks from N articles forward.
8219 If N is negative, clear backward instead.
8220 The difference between N and the number of marks cleared is returned."
8221   (interactive "p")
8222   (gnus-summary-mark-forward n gnus-unread-mark))
8223
8224 (defun gnus-summary-clear-mark-backward (n)
8225   "Clear marks from N articles backward.
8226 The difference between N and the number of marks cleared is returned."
8227   (interactive "p")
8228   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8229
8230 (defun gnus-summary-mark-unread-as-read ()
8231   "Intended to be used by `gnus-summary-mark-article-hook'."
8232   (when (memq gnus-current-article gnus-newsgroup-unreads)
8233     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8234
8235 (defun gnus-summary-mark-read-and-unread-as-read ()
8236   "Intended to be used by `gnus-summary-mark-article-hook'."
8237   (let ((mark (gnus-summary-article-mark)))
8238     (when (or (gnus-unread-mark-p mark)
8239               (gnus-read-mark-p mark))
8240       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8241
8242 (defun gnus-summary-mark-region-as-read (point mark all)
8243   "Mark all unread articles between point and mark as read.
8244 If given a prefix, mark all articles between point and mark as read,
8245 even ticked and dormant ones."
8246   (interactive "r\nP")
8247   (save-excursion
8248     (let (article)
8249       (goto-char point)
8250       (beginning-of-line)
8251       (while (and
8252               (< (point) mark)
8253               (progn
8254                 (when (or all
8255                           (memq (setq article (gnus-summary-article-number))
8256                                 gnus-newsgroup-unreads))
8257                   (gnus-summary-mark-article article gnus-del-mark))
8258                 t)
8259               (gnus-summary-find-next))))))
8260
8261 (defun gnus-summary-mark-below (score mark)
8262   "Mark articles with score less than SCORE with MARK."
8263   (interactive "P\ncMark: ")
8264   (setq score (if score
8265                   (prefix-numeric-value score)
8266                 (or gnus-summary-default-score 0)))
8267   (save-excursion
8268     (set-buffer gnus-summary-buffer)
8269     (goto-char (point-min))
8270     (while
8271         (progn
8272           (and (< (gnus-summary-article-score) score)
8273                (gnus-summary-mark-article nil mark))
8274           (gnus-summary-find-next)))))
8275
8276 (defun gnus-summary-kill-below (&optional score)
8277   "Mark articles with score below SCORE as read."
8278   (interactive "P")
8279   (gnus-summary-mark-below score gnus-killed-mark))
8280
8281 (defun gnus-summary-clear-above (&optional score)
8282   "Clear all marks from articles with score above SCORE."
8283   (interactive "P")
8284   (gnus-summary-mark-above score gnus-unread-mark))
8285
8286 (defun gnus-summary-tick-above (&optional score)
8287   "Tick all articles with score above SCORE."
8288   (interactive "P")
8289   (gnus-summary-mark-above score gnus-ticked-mark))
8290
8291 (defun gnus-summary-mark-above (score mark)
8292   "Mark articles with score over SCORE with MARK."
8293   (interactive "P\ncMark: ")
8294   (setq score (if score
8295                   (prefix-numeric-value score)
8296                 (or gnus-summary-default-score 0)))
8297   (save-excursion
8298     (set-buffer gnus-summary-buffer)
8299     (goto-char (point-min))
8300     (while (and (progn
8301                   (when (> (gnus-summary-article-score) score)
8302                     (gnus-summary-mark-article nil mark))
8303                   t)
8304                 (gnus-summary-find-next)))))
8305
8306 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8307 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8308 (defun gnus-summary-limit-include-expunged (&optional no-error)
8309   "Display all the hidden articles that were expunged for low scores."
8310   (interactive)
8311   (let ((buffer-read-only nil))
8312     (let ((scored gnus-newsgroup-scored)
8313           headers h)
8314       (while scored
8315         (unless (gnus-summary-goto-subject (caar scored))
8316           (and (setq h (gnus-summary-article-header (caar scored)))
8317                (< (cdar scored) gnus-summary-expunge-below)
8318                (push h headers)))
8319         (setq scored (cdr scored)))
8320       (if (not headers)
8321           (when (not no-error)
8322             (error "No expunged articles hidden"))
8323         (goto-char (point-min))
8324         (gnus-summary-prepare-unthreaded (nreverse headers))
8325         (goto-char (point-min))
8326         (gnus-summary-position-point)
8327         t))))
8328
8329 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8330   "Mark all unread articles in this newsgroup as read.
8331 If prefix argument ALL is non-nil, ticked and dormant articles will
8332 also be marked as read.
8333 If QUIETLY is non-nil, no questions will be asked.
8334 If TO-HERE is non-nil, it should be a point in the buffer.  All
8335 articles before this point will be marked as read.
8336 Note that this function will only catch up the unread article
8337 in the current summary buffer limitation.
8338 The number of articles marked as read is returned."
8339   (interactive "P")
8340   (prog1
8341       (save-excursion
8342         (when (or quietly
8343                   (not gnus-interactive-catchup) ;Without confirmation?
8344                   gnus-expert-user
8345                   (gnus-y-or-n-p
8346                    (if all
8347                        "Mark absolutely all articles as read? "
8348                      "Mark all unread articles as read? ")))
8349           (if (and not-mark
8350                    (not gnus-newsgroup-adaptive)
8351                    (not gnus-newsgroup-auto-expire)
8352                    (not gnus-suppress-duplicates)
8353                    (or (not gnus-use-cache)
8354                        (eq gnus-use-cache 'passive)))
8355               (progn
8356                 (when all
8357                   (setq gnus-newsgroup-marked nil
8358                         gnus-newsgroup-dormant nil))
8359                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8360             ;; We actually mark all articles as canceled, which we
8361             ;; have to do when using auto-expiry or adaptive scoring.
8362             (gnus-summary-show-all-threads)
8363             (when (gnus-summary-first-subject (not all) t)
8364               (while (and
8365                       (if to-here (< (point) to-here) t)
8366                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8367                       (gnus-summary-find-next (not all) nil nil t))))
8368             (gnus-set-mode-line 'summary))
8369           t))
8370     (gnus-summary-position-point)))
8371
8372 (defun gnus-summary-catchup-to-here (&optional all)
8373   "Mark all unticked articles before the current one as read.
8374 If ALL is non-nil, also mark ticked and dormant articles as read."
8375   (interactive "P")
8376   (save-excursion
8377     (gnus-save-hidden-threads
8378       (let ((beg (point)))
8379         ;; We check that there are unread articles.
8380         (when (or all (gnus-summary-find-prev))
8381           (gnus-summary-catchup all t beg)))))
8382   (gnus-summary-position-point))
8383
8384 (defun gnus-summary-catchup-all (&optional quietly)
8385   "Mark all articles in this newsgroup as read."
8386   (interactive "P")
8387   (gnus-summary-catchup t quietly))
8388
8389 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8390   "Mark all unread articles in this group as read, then exit.
8391 If prefix argument ALL is non-nil, all articles are marked as read."
8392   (interactive "P")
8393   (when (gnus-summary-catchup all quietly nil 'fast)
8394     ;; Select next newsgroup or exit.
8395     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8396              (eq gnus-auto-select-next 'quietly))
8397         (gnus-summary-next-group nil)
8398       (gnus-summary-exit))))
8399
8400 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8401   "Mark all articles in this newsgroup as read, and then exit."
8402   (interactive "P")
8403   (gnus-summary-catchup-and-exit t quietly))
8404
8405 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8406 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8407   "Mark all articles in this group as read and select the next group.
8408 If given a prefix, mark all articles, unread as well as ticked, as
8409 read."
8410   (interactive "P")
8411   (save-excursion
8412     (gnus-summary-catchup all))
8413   (gnus-summary-next-article t nil nil t))
8414
8415 ;; Thread-based commands.
8416
8417 (defun gnus-summary-articles-in-thread (&optional article)
8418   "Return a list of all articles in the current thread.
8419 If ARTICLE is non-nil, return all articles in the thread that starts
8420 with that article."
8421   (let* ((article (or article (gnus-summary-article-number)))
8422          (data (gnus-data-find-list article))
8423          (top-level (gnus-data-level (car data)))
8424          (top-subject
8425           (cond ((null gnus-thread-operation-ignore-subject)
8426                  (gnus-simplify-subject-re
8427                   (mail-header-subject (gnus-data-header (car data)))))
8428                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8429                  (gnus-simplify-subject-fuzzy
8430                   (mail-header-subject (gnus-data-header (car data)))))
8431                 (t nil)))
8432          (end-point (save-excursion
8433                       (if (gnus-summary-go-to-next-thread)
8434                           (point) (point-max))))
8435          articles)
8436     (while (and data
8437                 (< (gnus-data-pos (car data)) end-point))
8438       (when (or (not top-subject)
8439                 (string= top-subject
8440                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8441                              (gnus-simplify-subject-fuzzy
8442                               (mail-header-subject
8443                                (gnus-data-header (car data))))
8444                            (gnus-simplify-subject-re
8445                             (mail-header-subject
8446                              (gnus-data-header (car data)))))))
8447         (push (gnus-data-number (car data)) articles))
8448       (unless (and (setq data (cdr data))
8449                    (> (gnus-data-level (car data)) top-level))
8450         (setq data nil)))
8451     ;; Return the list of articles.
8452     (nreverse articles)))
8453
8454 (defun gnus-summary-rethread-current ()
8455   "Rethread the thread the current article is part of."
8456   (interactive)
8457   (let* ((gnus-show-threads t)
8458          (article (gnus-summary-article-number))
8459          (id (mail-header-id (gnus-summary-article-header)))
8460          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8461     (unless id
8462       (error "No article on the current line"))
8463     (gnus-rebuild-thread id)
8464     (gnus-summary-goto-subject article)))
8465
8466 (defun gnus-summary-reparent-thread ()
8467   "Make the current article child of the marked (or previous) article.
8468
8469 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8470 is non-nil or the Subject: of both articles are the same."
8471   (interactive)
8472   (unless (not (gnus-group-read-only-p))
8473     (error "The current newsgroup does not support article editing"))
8474   (unless (<= (length gnus-newsgroup-processable) 1)
8475     (error "No more than one article may be marked"))
8476   (save-window-excursion
8477     (let ((gnus-article-buffer " *reparent*")
8478           (current-article (gnus-summary-article-number))
8479           ;; First grab the marked article, otherwise one line up.
8480           (parent-article (if (not (null gnus-newsgroup-processable))
8481                               (car gnus-newsgroup-processable)
8482                             (save-excursion
8483                               (if (eq (forward-line -1) 0)
8484                                   (gnus-summary-article-number)
8485                                 (error "Beginning of summary buffer"))))))
8486       (unless (not (eq current-article parent-article))
8487         (error "An article may not be self-referential"))
8488       (let ((message-id (mail-header-id
8489                          (gnus-summary-article-header parent-article))))
8490         (unless (and message-id (not (equal message-id "")))
8491           (error "No message-id in desired parent"))
8492         (gnus-with-article current-article
8493           (goto-char (point-min))
8494           (if (re-search-forward "^References: " nil t)
8495               (progn
8496                 (re-search-forward "^[^ \t]" nil t)
8497                 (forward-line -1)
8498                 (end-of-line)
8499                 (insert " " message-id))
8500             (insert "References: " message-id "\n")))
8501         (set-buffer gnus-summary-buffer)
8502         (gnus-summary-unmark-all-processable)
8503         (gnus-summary-update-article current-article)
8504         (gnus-summary-rethread-current)
8505         (gnus-message 3 "Article %d is now the child of article %d"
8506                       current-article parent-article)))))
8507
8508 (defun gnus-summary-toggle-threads (&optional arg)
8509   "Toggle showing conversation threads.
8510 If ARG is positive number, turn showing conversation threads on."
8511   (interactive "P")
8512   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8513     (setq gnus-show-threads
8514           (if (null arg) (not gnus-show-threads)
8515             (> (prefix-numeric-value arg) 0)))
8516     (gnus-summary-prepare)
8517     (gnus-summary-goto-subject current)
8518     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8519     (gnus-summary-position-point)))
8520
8521 (defun gnus-summary-show-all-threads ()
8522   "Show all threads."
8523   (interactive)
8524   (save-excursion
8525     (let ((buffer-read-only nil))
8526       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8527   (gnus-summary-position-point))
8528
8529 (defun gnus-summary-show-thread ()
8530   "Show thread subtrees.
8531 Returns nil if no thread was there to be shown."
8532   (interactive)
8533   (let ((buffer-read-only nil)
8534         (orig (point))
8535         ;; first goto end then to beg, to have point at beg after let
8536         (end (progn (end-of-line) (point)))
8537         (beg (progn (beginning-of-line) (point))))
8538     (prog1
8539         ;; Any hidden lines here?
8540         (search-forward "\r" end t)
8541       (subst-char-in-region beg end ?\^M ?\n t)
8542       (goto-char orig)
8543       (gnus-summary-position-point))))
8544
8545 (defun gnus-summary-hide-all-threads ()
8546   "Hide all thread subtrees."
8547   (interactive)
8548   (save-excursion
8549     (goto-char (point-min))
8550     (gnus-summary-hide-thread)
8551     (while (zerop (gnus-summary-next-thread 1 t))
8552       (gnus-summary-hide-thread)))
8553   (gnus-summary-position-point))
8554
8555 (defun gnus-summary-hide-thread ()
8556   "Hide thread subtrees.
8557 Returns nil if no threads were there to be hidden."
8558   (interactive)
8559   (let ((buffer-read-only nil)
8560         (start (point))
8561         (article (gnus-summary-article-number)))
8562     (goto-char start)
8563     ;; Go forward until either the buffer ends or the subthread
8564     ;; ends.
8565     (when (and (not (eobp))
8566                (or (zerop (gnus-summary-next-thread 1 t))
8567                    (goto-char (point-max))))
8568       (prog1
8569           (if (and (> (point) start)
8570                    (search-backward "\n" start t))
8571               (progn
8572                 (subst-char-in-region start (point) ?\n ?\^M)
8573                 (gnus-summary-goto-subject article))
8574             (goto-char start)
8575             nil)
8576         ;;(gnus-summary-position-point)
8577         ))))
8578
8579 (defun gnus-summary-go-to-next-thread (&optional previous)
8580   "Go to the same level (or less) next thread.
8581 If PREVIOUS is non-nil, go to previous thread instead.
8582 Return the article number moved to, or nil if moving was impossible."
8583   (let ((level (gnus-summary-thread-level))
8584         (way (if previous -1 1))
8585         (beg (point)))
8586     (forward-line way)
8587     (while (and (not (eobp))
8588                 (< level (gnus-summary-thread-level)))
8589       (forward-line way))
8590     (if (eobp)
8591         (progn
8592           (goto-char beg)
8593           nil)
8594       (setq beg (point))
8595       (prog1
8596           (gnus-summary-article-number)
8597         (goto-char beg)))))
8598
8599 (defun gnus-summary-next-thread (n &optional silent)
8600   "Go to the same level next N'th thread.
8601 If N is negative, search backward instead.
8602 Returns the difference between N and the number of skips actually
8603 done.
8604
8605 If SILENT, don't output messages."
8606   (interactive "p")
8607   (let ((backward (< n 0))
8608         (n (abs n)))
8609     (while (and (> n 0)
8610                 (gnus-summary-go-to-next-thread backward))
8611       (decf n))
8612     (unless silent
8613       (gnus-summary-position-point))
8614     (when (and (not silent) (/= 0 n))
8615       (gnus-message 7 "No more threads"))
8616     n))
8617
8618 (defun gnus-summary-prev-thread (n)
8619   "Go to the same level previous N'th thread.
8620 Returns the difference between N and the number of skips actually
8621 done."
8622   (interactive "p")
8623   (gnus-summary-next-thread (- n)))
8624
8625 (defun gnus-summary-go-down-thread ()
8626   "Go down one level in the current thread."
8627   (let ((children (gnus-summary-article-children)))
8628     (when children
8629       (gnus-summary-goto-subject (car children)))))
8630
8631 (defun gnus-summary-go-up-thread ()
8632   "Go up one level in the current thread."
8633   (let ((parent (gnus-summary-article-parent)))
8634     (when parent
8635       (gnus-summary-goto-subject parent))))
8636
8637 (defun gnus-summary-down-thread (n)
8638   "Go down thread N steps.
8639 If N is negative, go up instead.
8640 Returns the difference between N and how many steps down that were
8641 taken."
8642   (interactive "p")
8643   (let ((up (< n 0))
8644         (n (abs n)))
8645     (while (and (> n 0)
8646                 (if up (gnus-summary-go-up-thread)
8647                   (gnus-summary-go-down-thread)))
8648       (setq n (1- n)))
8649     (gnus-summary-position-point)
8650     (when (/= 0 n)
8651       (gnus-message 7 "Can't go further"))
8652     n))
8653
8654 (defun gnus-summary-up-thread (n)
8655   "Go up thread N steps.
8656 If N is negative, go up instead.
8657 Returns the difference between N and how many steps down that were
8658 taken."
8659   (interactive "p")
8660   (gnus-summary-down-thread (- n)))
8661
8662 (defun gnus-summary-top-thread ()
8663   "Go to the top of the thread."
8664   (interactive)
8665   (while (gnus-summary-go-up-thread))
8666   (gnus-summary-article-number))
8667
8668 (defun gnus-summary-kill-thread (&optional unmark)
8669   "Mark articles under current thread as read.
8670 If the prefix argument is positive, remove any kinds of marks.
8671 If the prefix argument is negative, tick articles instead."
8672   (interactive "P")
8673   (when unmark
8674     (setq unmark (prefix-numeric-value unmark)))
8675   (let ((articles (gnus-summary-articles-in-thread)))
8676     (save-excursion
8677       ;; Expand the thread.
8678       (gnus-summary-show-thread)
8679       ;; Mark all the articles.
8680       (while articles
8681         (gnus-summary-goto-subject (car articles))
8682         (cond ((null unmark)
8683                (gnus-summary-mark-article-as-read gnus-killed-mark))
8684               ((> unmark 0)
8685                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8686               (t
8687                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8688         (setq articles (cdr articles))))
8689     ;; Hide killed subtrees.
8690     (and (null unmark)
8691          gnus-thread-hide-killed
8692          (gnus-summary-hide-thread))
8693     ;; If marked as read, go to next unread subject.
8694     (when (null unmark)
8695       ;; Go to next unread subject.
8696       (gnus-summary-next-subject 1 t)))
8697   (gnus-set-mode-line 'summary))
8698
8699 ;; Summary sorting commands
8700
8701 (defun gnus-summary-sort-by-number (&optional reverse)
8702   "Sort the summary buffer by article number.
8703 Argument REVERSE means reverse order."
8704   (interactive "P")
8705   (gnus-summary-sort 'number reverse))
8706
8707 (defun gnus-summary-sort-by-author (&optional reverse)
8708   "Sort the summary buffer by author name alphabetically.
8709 If case-fold-search is non-nil, case of letters is ignored.
8710 Argument REVERSE means reverse order."
8711   (interactive "P")
8712   (gnus-summary-sort 'author reverse))
8713
8714 (defun gnus-summary-sort-by-subject (&optional reverse)
8715   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8716 If case-fold-search is non-nil, case of letters is ignored.
8717 Argument REVERSE means reverse order."
8718   (interactive "P")
8719   (gnus-summary-sort 'subject reverse))
8720
8721 (defun gnus-summary-sort-by-date (&optional reverse)
8722   "Sort the summary buffer by date.
8723 Argument REVERSE means reverse order."
8724   (interactive "P")
8725   (gnus-summary-sort 'date reverse))
8726
8727 (defun gnus-summary-sort-by-score (&optional reverse)
8728   "Sort the summary buffer by score.
8729 Argument REVERSE means reverse order."
8730   (interactive "P")
8731   (gnus-summary-sort 'score reverse))
8732
8733 (defun gnus-summary-sort-by-lines (&optional reverse)
8734   "Sort the summary buffer by the number of lines.
8735 Argument REVERSE means reverse order."
8736   (interactive "P")
8737   (gnus-summary-sort 'lines reverse))
8738
8739 (defun gnus-summary-sort-by-chars (&optional reverse)
8740   "Sort the summary buffer by article length.
8741 Argument REVERSE means reverse order."
8742   (interactive "P")
8743   (gnus-summary-sort 'chars reverse))   
8744
8745 (defun gnus-summary-sort (predicate reverse)
8746   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8747   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8748          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8749          (gnus-thread-sort-functions
8750           (if (not reverse)
8751               thread
8752             `(lambda (t1 t2)
8753                (,thread t2 t1))))
8754          (gnus-article-sort-functions
8755           (if (not reverse)
8756               article
8757             `(lambda (t1 t2)
8758                (,article t2 t1))))
8759          (buffer-read-only)
8760          (gnus-summary-prepare-hook nil))
8761     ;; We do the sorting by regenerating the threads.
8762     (gnus-summary-prepare)
8763     ;; Hide subthreads if needed.
8764     (when (and gnus-show-threads gnus-thread-hide-subtree)
8765       (gnus-summary-hide-all-threads))))
8766
8767 ;; Summary saving commands.
8768
8769 (defun gnus-summary-save-article (&optional n not-saved)
8770   "Save the current article using the default saver function.
8771 If N is a positive number, save the N next articles.
8772 If N is a negative number, save the N previous articles.
8773 If N is nil and any articles have been marked with the process mark,
8774 save those articles instead.
8775 The variable `gnus-default-article-saver' specifies the saver function."
8776   (interactive "P")
8777   (let* ((articles (gnus-summary-work-articles n))
8778          (save-buffer (save-excursion
8779                         (nnheader-set-temp-buffer " *Gnus Save*")))
8780          (num (length articles))
8781          header file)
8782     (dolist (article articles)
8783       (setq header (gnus-summary-article-header article))
8784       (if (not (vectorp header))
8785           ;; This is a pseudo-article.
8786           (if (assq 'name header)
8787               (gnus-copy-file (cdr (assq 'name header)))
8788             (gnus-message 1 "Article %d is unsaveable" article))
8789         ;; This is a real article.
8790         (save-window-excursion
8791           (gnus-summary-select-article t nil nil article))
8792         (save-excursion
8793           (set-buffer save-buffer)
8794           (erase-buffer)
8795           (insert-buffer-substring gnus-original-article-buffer))
8796         (setq file (gnus-article-save save-buffer file num))
8797         (gnus-summary-remove-process-mark article)
8798         (unless not-saved
8799           (gnus-summary-set-saved-mark article))))
8800     (gnus-kill-buffer save-buffer)
8801     (gnus-summary-position-point)
8802     (gnus-set-mode-line 'summary)
8803     n))
8804
8805 (defun gnus-summary-pipe-output (&optional arg)
8806   "Pipe the current article to a subprocess.
8807 If N is a positive number, pipe the N next articles.
8808 If N is a negative number, pipe the N previous articles.
8809 If N is nil and any articles have been marked with the process mark,
8810 pipe those articles instead."
8811   (interactive "P")
8812   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8813     (gnus-summary-save-article arg t))
8814   (gnus-configure-windows 'pipe))
8815
8816 (defun gnus-summary-save-article-mail (&optional arg)
8817   "Append the current article to an mail file.
8818 If N is a positive number, save the N next articles.
8819 If N is a negative number, save the N previous articles.
8820 If N is nil and any articles have been marked with the process mark,
8821 save those articles instead."
8822   (interactive "P")
8823   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8824     (gnus-summary-save-article arg)))
8825
8826 (defun gnus-summary-save-article-rmail (&optional arg)
8827   "Append the current article to an rmail file.
8828 If N is a positive number, save the N next articles.
8829 If N is a negative number, save the N previous articles.
8830 If N is nil and any articles have been marked with the process mark,
8831 save those articles instead."
8832   (interactive "P")
8833   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8834     (gnus-summary-save-article arg)))
8835
8836 (defun gnus-summary-save-article-file (&optional arg)
8837   "Append the current article to a file.
8838 If N is a positive number, save the N next articles.
8839 If N is a negative number, save the N previous articles.
8840 If N is nil and any articles have been marked with the process mark,
8841 save those articles instead."
8842   (interactive "P")
8843   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8844     (gnus-summary-save-article arg)))
8845
8846 (defun gnus-summary-write-article-file (&optional arg)
8847   "Write the current article to a file, deleting the previous file.
8848 If N is a positive number, save the N next articles.
8849 If N is a negative number, save the N previous articles.
8850 If N is nil and any articles have been marked with the process mark,
8851 save those articles instead."
8852   (interactive "P")
8853   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8854     (gnus-summary-save-article arg)))
8855
8856 (defun gnus-summary-save-article-body-file (&optional arg)
8857   "Append the current article body to a file.
8858 If N is a positive number, save the N next articles.
8859 If N is a negative number, save the N previous articles.
8860 If N is nil and any articles have been marked with the process mark,
8861 save those articles instead."
8862   (interactive "P")
8863   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8864     (gnus-summary-save-article arg)))
8865
8866 (defun gnus-summary-pipe-message (program)
8867   "Pipe the current article through PROGRAM."
8868   (interactive "sProgram: ")
8869   (gnus-summary-select-article)
8870   (let ((mail-header-separator ""))
8871     (gnus-eval-in-buffer-window gnus-article-buffer
8872       (save-restriction
8873         (widen)
8874         (let ((start (window-start))
8875               buffer-read-only)
8876           (message-pipe-buffer-body program)
8877           (set-window-start (get-buffer-window (current-buffer)) start))))))
8878
8879 (defun gnus-get-split-value (methods)
8880   "Return a value based on the split METHODS."
8881   (let (split-name method result match)
8882     (when methods
8883       (save-excursion
8884         (set-buffer gnus-original-article-buffer)
8885         (save-restriction
8886           (nnheader-narrow-to-headers)
8887           (while methods
8888             (goto-char (point-min))
8889             (setq method (pop methods))
8890             (setq match (car method))
8891             (when (cond
8892                    ((stringp match)
8893                     ;; Regular expression.
8894                     (ignore-errors
8895                       (re-search-forward match nil t)))
8896                    ((gnus-functionp match)
8897                     ;; Function.
8898                     (save-restriction
8899                       (widen)
8900                       (setq result (funcall match gnus-newsgroup-name))))
8901                    ((consp match)
8902                     ;; Form.
8903                     (save-restriction
8904                       (widen)
8905                       (setq result (eval match)))))
8906               (setq split-name (append (cdr method) split-name))
8907               (cond ((stringp result)
8908                      (push (expand-file-name
8909                             result gnus-article-save-directory)
8910                            split-name))
8911                     ((consp result)
8912                      (setq split-name (append result split-name)))))))))
8913     (nreverse split-name)))
8914
8915 (defun gnus-valid-move-group-p (group)
8916   (and (boundp group)
8917        (symbol-name group)
8918        (symbol-value group)
8919        (gnus-get-function (gnus-find-method-for-group
8920                            (symbol-name group)) 'request-accept-article t)))
8921
8922 (defun gnus-read-move-group-name (prompt default articles prefix)
8923   "Read a group name."
8924   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8925          (minibuffer-confirm-incomplete nil) ; XEmacs
8926          (prom
8927           (format "%s %s to:"
8928                   prompt
8929                   (if (> (length articles) 1)
8930                       (format "these %d articles" (length articles))
8931                     "this article")))
8932          (to-newsgroup
8933           (cond
8934            ((null split-name)
8935             (gnus-completing-read default prom
8936                                   gnus-active-hashtb
8937                                   'gnus-valid-move-group-p
8938                                   nil prefix
8939                                   'gnus-group-history))
8940            ((= 1 (length split-name))
8941             (gnus-completing-read (car split-name) prom
8942                                   gnus-active-hashtb
8943                                   'gnus-valid-move-group-p
8944                                   nil nil
8945                                   'gnus-group-history))
8946            (t
8947             (gnus-completing-read nil prom
8948                                   (mapcar (lambda (el) (list el))
8949                                           (nreverse split-name))
8950                                   nil nil nil
8951                                   'gnus-group-history)))))
8952     (when to-newsgroup
8953       (if (or (string= to-newsgroup "")
8954               (string= to-newsgroup prefix))
8955           (setq to-newsgroup default))
8956       (unless to-newsgroup
8957         (error "No group name entered"))
8958       (or (gnus-active to-newsgroup)
8959           (gnus-activate-group to-newsgroup)
8960           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8961                                      to-newsgroup))
8962               (or (and (gnus-request-create-group
8963                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8964                        (gnus-activate-group to-newsgroup nil nil
8965                                             (gnus-group-name-to-method
8966                                              to-newsgroup)))
8967                   (error "Couldn't create group %s" to-newsgroup)))
8968           (error "No such group: %s" to-newsgroup)))
8969     to-newsgroup))
8970
8971 (defun gnus-summary-save-parts (type dir n reverse)
8972   "Save parts matching TYPE to DIR.
8973 If REVERSE, save parts that do not match TYPE."
8974   (interactive
8975    (list (read-string "Save parts of type: " "image/.*")
8976          (read-file-name "Save to directory: " t nil t)
8977          current-prefix-arg))
8978   (gnus-summary-iterate n
8979     (let ((gnus-display-mime-function nil)
8980           (gnus-inhibit-treatment t))
8981       (gnus-summary-select-article))
8982     (save-excursion
8983       (set-buffer gnus-article-buffer)
8984       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8985         (when handles
8986           (gnus-summary-save-parts-1 type dir handles reverse)
8987           (mm-destroy-parts handles))))))
8988
8989 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8990   (if (stringp (car handle))
8991       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8992               (cdr handle))
8993     (when (if reverse
8994               (not (string-match type (mm-handle-media-type handle)))
8995             (string-match type (mm-handle-media-type handle)))
8996       (let ((file (expand-file-name
8997                    (file-name-nondirectory
8998                     (or
8999                      (mail-content-type-get
9000                       (mm-handle-disposition handle) 'filename)
9001                      (concat gnus-newsgroup-name "." gnus-current-article)))
9002                    dir)))
9003         (unless (file-exists-p file)
9004           (mm-save-part-to-file handle file))))))
9005
9006 ;; Summary extract commands
9007
9008 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9009   (let ((buffer-read-only nil)
9010         (article (gnus-summary-article-number))
9011         after-article b e)
9012     (unless (gnus-summary-goto-subject article)
9013       (error "No such article: %d" article))
9014     (gnus-summary-position-point)
9015     ;; If all commands are to be bunched up on one line, we collect
9016     ;; them here.
9017     (unless gnus-view-pseudos-separately
9018       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9019             files action)
9020         (while ps
9021           (setq action (cdr (assq 'action (car ps))))
9022           (setq files (list (cdr (assq 'name (car ps)))))
9023           (while (and ps (cdr ps)
9024                       (string= (or action "1")
9025                                (or (cdr (assq 'action (cadr ps))) "2")))
9026             (push (cdr (assq 'name (cadr ps))) files)
9027             (setcdr ps (cddr ps)))
9028           (when files
9029             (when (not (string-match "%s" action))
9030               (push " " files))
9031             (push " " files)
9032             (when (assq 'execute (car ps))
9033               (setcdr (assq 'execute (car ps))
9034                       (funcall (if (string-match "%s" action)
9035                                    'format 'concat)
9036                                action
9037                                (mapconcat
9038                                 (lambda (f)
9039                                   (if (equal f " ")
9040                                       f
9041                                     (mm-quote-arg f)))
9042                                 files " ")))))
9043           (setq ps (cdr ps)))))
9044     (if (and gnus-view-pseudos (not not-view))
9045         (while pslist
9046           (when (assq 'execute (car pslist))
9047             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9048                                   (eq gnus-view-pseudos 'not-confirm)))
9049           (setq pslist (cdr pslist)))
9050       (save-excursion
9051         (while pslist
9052           (setq after-article (or (cdr (assq 'article (car pslist)))
9053                                   (gnus-summary-article-number)))
9054           (gnus-summary-goto-subject after-article)
9055           (forward-line 1)
9056           (setq b (point))
9057           (insert "    " (file-name-nondirectory
9058                           (cdr (assq 'name (car pslist))))
9059                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9060           (setq e (point))
9061           (forward-line -1)             ; back to `b'
9062           (gnus-add-text-properties
9063            b (1- e) (list 'gnus-number gnus-reffed-article-number
9064                           gnus-mouse-face-prop gnus-mouse-face))
9065           (gnus-data-enter
9066            after-article gnus-reffed-article-number
9067            gnus-unread-mark b (car pslist) 0 (- e b))
9068           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9069           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9070           (setq pslist (cdr pslist)))))))
9071
9072 (defun gnus-pseudos< (p1 p2)
9073   (let ((c1 (cdr (assq 'action p1)))
9074         (c2 (cdr (assq 'action p2))))
9075     (and c1 c2 (string< c1 c2))))
9076
9077 (defun gnus-request-pseudo-article (props)
9078   (cond ((assq 'execute props)
9079          (gnus-execute-command (cdr (assq 'execute props)))))
9080   (let ((gnus-current-article (gnus-summary-article-number)))
9081     (gnus-run-hooks 'gnus-mark-article-hook)))
9082
9083 (defun gnus-execute-command (command &optional automatic)
9084   (save-excursion
9085     (gnus-article-setup-buffer)
9086     (set-buffer gnus-article-buffer)
9087     (setq buffer-read-only nil)
9088     (let ((command (if automatic command
9089                      (read-string "Command: " (cons command 0)))))
9090       (erase-buffer)
9091       (insert "$ " command "\n\n")
9092       (if gnus-view-pseudo-asynchronously
9093           (start-process "gnus-execute" (current-buffer) shell-file-name
9094                          shell-command-switch command)
9095         (call-process shell-file-name nil t nil
9096                       shell-command-switch command)))))
9097
9098 ;; Summary kill commands.
9099
9100 (defun gnus-summary-edit-global-kill (article)
9101   "Edit the \"global\" kill file."
9102   (interactive (list (gnus-summary-article-number)))
9103   (gnus-group-edit-global-kill article))
9104
9105 (defun gnus-summary-edit-local-kill ()
9106   "Edit a local kill file applied to the current newsgroup."
9107   (interactive)
9108   (setq gnus-current-headers (gnus-summary-article-header))
9109   (gnus-group-edit-local-kill
9110    (gnus-summary-article-number) gnus-newsgroup-name))
9111
9112 ;;; Header reading.
9113
9114 (defun gnus-read-header (id &optional header)
9115   "Read the headers of article ID and enter them into the Gnus system."
9116   (let ((group gnus-newsgroup-name)
9117         (gnus-override-method
9118          (and (gnus-news-group-p gnus-newsgroup-name)
9119               gnus-refer-article-method))
9120         where)
9121     ;; First we check to see whether the header in question is already
9122     ;; fetched.
9123     (if (stringp id)
9124         ;; This is a Message-ID.
9125         (setq header (or header (gnus-id-to-header id)))
9126       ;; This is an article number.
9127       (setq header (or header (gnus-summary-article-header id))))
9128     (if (and header
9129              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9130         ;; We have found the header.
9131         header
9132       ;; If this is a sparse article, we have to nix out its
9133       ;; previous entry in the thread hashtb.
9134       (when (and header
9135                  (gnus-summary-article-sparse-p (mail-header-number header)))
9136         (let* ((parent (gnus-parent-id (mail-header-references header)))
9137                (thread (and parent (gnus-id-to-thread parent))))
9138           (when thread
9139             (delq (assq header thread) thread))))
9140       ;; We have to really fetch the header to this article.
9141       (save-excursion
9142         (set-buffer nntp-server-buffer)
9143         (when (setq where (gnus-request-head id group))
9144           (nnheader-fold-continuation-lines)
9145           (goto-char (point-max))
9146           (insert ".\n")
9147           (goto-char (point-min))
9148           (insert "211 ")
9149           (princ (cond
9150                   ((numberp id) id)
9151                   ((cdr where) (cdr where))
9152                   (header (mail-header-number header))
9153                   (t gnus-reffed-article-number))
9154                  (current-buffer))
9155           (insert " Article retrieved.\n"))
9156         (if (or (not where)
9157                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9158             ()                          ; Malformed head.
9159           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9160             (when (and (stringp id)
9161                        (not (string= (gnus-group-real-name group)
9162                                      (car where))))
9163               ;; If we fetched by Message-ID and the article came
9164               ;; from a different group, we fudge some bogus article
9165               ;; numbers for this article.
9166               (mail-header-set-number header gnus-reffed-article-number))
9167             (save-excursion
9168               (set-buffer gnus-summary-buffer)
9169               (decf gnus-reffed-article-number)
9170               (gnus-remove-header (mail-header-number header))
9171               (push header gnus-newsgroup-headers)
9172               (setq gnus-current-headers header)
9173               (push (mail-header-number header) gnus-newsgroup-limit)))
9174           header)))))
9175
9176 (defun gnus-remove-header (number)
9177   "Remove header NUMBER from `gnus-newsgroup-headers'."
9178   (if (and gnus-newsgroup-headers
9179            (= number (mail-header-number (car gnus-newsgroup-headers))))
9180       (pop gnus-newsgroup-headers)
9181     (let ((headers gnus-newsgroup-headers))
9182       (while (and (cdr headers)
9183                   (not (= number (mail-header-number (cadr headers)))))
9184         (pop headers))
9185       (when (cdr headers)
9186         (setcdr headers (cddr headers))))))
9187
9188 ;;;
9189 ;;; summary highlights
9190 ;;;
9191
9192 (defun gnus-highlight-selected-summary ()
9193   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9194   ;; Highlight selected article in summary buffer
9195   (when gnus-summary-selected-face
9196     (save-excursion
9197       (let* ((beg (progn (beginning-of-line) (point)))
9198              (end (progn (end-of-line) (point)))
9199              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9200              (from (if (get-text-property beg gnus-mouse-face-prop)
9201                        beg
9202                      (or (next-single-property-change
9203                           beg gnus-mouse-face-prop nil end)
9204                          beg)))
9205              (to
9206               (if (= from end)
9207                   (- from 2)
9208                 (or (next-single-property-change
9209                      from gnus-mouse-face-prop nil end)
9210                     end))))
9211         ;; If no mouse-face prop on line we will have to = from = end,
9212         ;; so we highlight the entire line instead.
9213         (when (= (+ to 2) from)
9214           (setq from beg)
9215           (setq to end))
9216         (if gnus-newsgroup-selected-overlay
9217             ;; Move old overlay.
9218             (gnus-move-overlay
9219              gnus-newsgroup-selected-overlay from to (current-buffer))
9220           ;; Create new overlay.
9221           (gnus-overlay-put
9222            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9223            'face gnus-summary-selected-face))))))
9224
9225 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9226 (defun gnus-summary-highlight-line ()
9227   "Highlight current line according to `gnus-summary-highlight'."
9228   (let* ((list gnus-summary-highlight)
9229          (p (point))
9230          (end (progn (end-of-line) (point)))
9231          ;; now find out where the line starts and leave point there.
9232          (beg (progn (beginning-of-line) (point)))
9233          (article (gnus-summary-article-number))
9234          (score (or (cdr (assq (or article gnus-current-article)
9235                                gnus-newsgroup-scored))
9236                     gnus-summary-default-score 0))
9237          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9238          (inhibit-read-only t))
9239     ;; Eval the cars of the lists until we find a match.
9240     (let ((default gnus-summary-default-score))
9241       (while (and list
9242                   (not (eval (caar list))))
9243         (setq list (cdr list))))
9244     (let ((face (cdar list)))
9245       (unless (eq face (get-text-property beg 'face))
9246         (gnus-put-text-property-excluding-characters-with-faces
9247          beg end 'face
9248          (setq face (if (boundp face) (symbol-value face) face)))
9249         (when gnus-summary-highlight-line-function
9250           (funcall gnus-summary-highlight-line-function article face))))
9251     (goto-char p)))
9252
9253 (defun gnus-update-read-articles (group unread &optional compute)
9254   "Update the list of read articles in GROUP."
9255   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9256          (entry (gnus-gethash group gnus-newsrc-hashtb))
9257          (info (nth 2 entry))
9258          (prev 1)
9259          (unread (sort (copy-sequence unread) '<))
9260          read)
9261     (if (or (not info) (not active))
9262         ;; There is no info on this group if it was, in fact,
9263         ;; killed.  Gnus stores no information on killed groups, so
9264         ;; there's nothing to be done.
9265         ;; One could store the information somewhere temporarily,
9266         ;; perhaps...  Hmmm...
9267         ()
9268       ;; Remove any negative articles numbers.
9269       (while (and unread (< (car unread) 0))
9270         (setq unread (cdr unread)))
9271       ;; Remove any expired article numbers
9272       (while (and unread (< (car unread) (car active)))
9273         (setq unread (cdr unread)))
9274       ;; Compute the ranges of read articles by looking at the list of
9275       ;; unread articles.
9276       (while unread
9277         (when (/= (car unread) prev)
9278           (push (if (= prev (1- (car unread))) prev
9279                   (cons prev (1- (car unread))))
9280                 read))
9281         (setq prev (1+ (car unread)))
9282         (setq unread (cdr unread)))
9283       (when (<= prev (cdr active))
9284         (push (cons prev (cdr active)) read))
9285       (setq read (if (> (length read) 1) (nreverse read) read))
9286       (if compute
9287           read
9288         (save-excursion
9289           (set-buffer gnus-group-buffer)
9290           (gnus-undo-register
9291             `(progn
9292                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9293                (gnus-info-set-read ',info ',(gnus-info-read info))
9294                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9295                (gnus-group-update-group ,group t))))
9296        ;; Propagate the read marks to the backend.
9297        (if (gnus-check-backend-function 'request-set-mark group)
9298            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9299                  (add (gnus-remove-from-range read (gnus-info-read info))))
9300              (when (or add del)
9301                (unless (gnus-check-group group)
9302                  (error "Can't open server for %s" group))
9303                (gnus-request-set-mark
9304                 group (delq nil (list (if add (list add 'add '(read)))
9305                                       (if del (list del 'del '(read)))))))))
9306         ;; Enter this list into the group info.
9307         (gnus-info-set-read info read)
9308         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9309         (gnus-get-unread-articles-in-group info (gnus-active group))
9310         t))))
9311
9312 (defun gnus-offer-save-summaries ()
9313   "Offer to save all active summary buffers."
9314   (save-excursion
9315     (let ((buflist (buffer-list))
9316           buffers bufname)
9317       ;; Go through all buffers and find all summaries.
9318       (while buflist
9319         (and (setq bufname (buffer-name (car buflist)))
9320              (string-match "Summary" bufname)
9321              (save-excursion
9322                (set-buffer bufname)
9323                ;; We check that this is, indeed, a summary buffer.
9324                (and (eq major-mode 'gnus-summary-mode)
9325                     ;; Also make sure this isn't bogus.
9326                     gnus-newsgroup-prepared
9327                     ;; Also make sure that this isn't a dead summary buffer.
9328                     (not gnus-dead-summary-mode)))
9329              (push bufname buffers))
9330         (setq buflist (cdr buflist)))
9331       ;; Go through all these summary buffers and offer to save them.
9332       (when buffers
9333         (map-y-or-n-p
9334          "Update summary buffer %s? "
9335          (lambda (buf)
9336            (switch-to-buffer buf)
9337            (gnus-summary-exit))
9338          buffers)))))
9339
9340 (defun gnus-summary-setup-default-charset ()
9341   "Setup newsgroup default charset."
9342   (if (equal gnus-newsgroup-name "nndraft:drafts")
9343       (setq gnus-newsgroup-charset nil)
9344   (let* ((name (and gnus-newsgroup-name
9345                    (gnus-group-real-name gnus-newsgroup-name)))
9346          (ignored-charsets 
9347           (or gnus-newsgroup-ephemeral-ignored-charsets
9348               (append
9349                (and gnus-newsgroup-name
9350                     (or (gnus-group-find-parameter gnus-newsgroup-name
9351                                                    'ignored-charsets t)
9352                         (let ((alist gnus-group-ignored-charsets-alist)
9353                            elem (charsets nil))
9354                           (while (setq elem (pop alist))
9355                             (when (and name
9356                                        (string-match (car elem) name))
9357                               (setq alist nil
9358                                     charsets (cdr elem))))
9359                           charsets))))
9360               gnus-newsgroup-ignored-charsets)))
9361     (setq gnus-newsgroup-charset
9362           (or gnus-newsgroup-ephemeral-charset
9363               (and gnus-newsgroup-name
9364                    (or (gnus-group-find-parameter gnus-newsgroup-name
9365                                                   'charset)
9366                        (let ((alist gnus-group-charset-alist)
9367                              elem (charset nil))
9368                          (while (setq elem (pop alist))
9369                            (when (and name
9370                                       (string-match (car elem) name))
9371                              (setq alist nil
9372                                    charset (cadr elem))))
9373                          charset)))
9374               gnus-default-charset))
9375     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9376          ignored-charsets))))
9377
9378 ;;;
9379 ;;; Mime Commands
9380 ;;;
9381
9382 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9383   "Display the current article buffer fully MIME-buttonized.
9384 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9385 treated as multipart/mixed."
9386   (interactive "P")
9387   (require 'gnus-art)
9388   (let ((gnus-unbuttonized-mime-types nil)
9389         (gnus-mime-display-multipart-as-mixed show-all-parts))
9390     (gnus-summary-show-article)))
9391
9392 (defun gnus-summary-repair-multipart (article)
9393   "Add a Content-Type header to a multipart article without one."
9394   (interactive (list (gnus-summary-article-number)))
9395   (gnus-with-article article
9396     (message-narrow-to-head)
9397     (goto-char (point-max))
9398     (widen)
9399     (when (search-forward "\n--" nil t)
9400       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9401         (message-narrow-to-head)
9402         (message-remove-header "Mime-Version")
9403         (message-remove-header "Content-Type")
9404         (goto-char (point-max))
9405         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9406                         separator))
9407         (insert "Mime-Version: 1.0\n")
9408         (widen))))
9409   (let (gnus-mark-article-hook)
9410     (gnus-summary-select-article t t nil article)))
9411
9412 (defun gnus-summary-toggle-display-buttonized ()
9413   "Toggle the buttonizing of the article buffer."
9414   (interactive)
9415   (require 'gnus-art)
9416   (if (setq gnus-inhibit-mime-unbuttonizing
9417             (not gnus-inhibit-mime-unbuttonizing))
9418       (let ((gnus-unbuttonized-mime-types nil))
9419         (gnus-summary-show-article))
9420     (gnus-summary-show-article)))
9421
9422 ;;;
9423 ;;; with article
9424 ;;;
9425
9426 (defmacro gnus-with-article (article &rest forms)
9427   "Select ARTICLE and perform FORMS in the original article buffer.
9428 Then replace the article with the result."
9429   `(progn
9430      ;; We don't want the article to be marked as read.
9431      (let (gnus-mark-article-hook)
9432        (gnus-summary-select-article t t nil ,article))
9433      (set-buffer gnus-original-article-buffer)
9434      ,@forms
9435      (if (not (gnus-check-backend-function
9436                'request-replace-article (car gnus-article-current)))
9437          (gnus-message 5 "Read-only group; not replacing")
9438        (unless (gnus-request-replace-article
9439                 ,article (car gnus-article-current)
9440                 (current-buffer) t)
9441          (error "Couldn't replace article")))
9442      ;; The cache and backlog have to be flushed somewhat.
9443      (when gnus-keep-backlog
9444        (gnus-backlog-remove-article
9445         (car gnus-article-current) (cdr gnus-article-current)))
9446      (when gnus-use-cache
9447        (gnus-cache-update-article
9448         (car gnus-article-current) (cdr gnus-article-current)))))
9449
9450 (put 'gnus-with-article 'lisp-indent-function 1)
9451 (put 'gnus-with-article 'edebug-form-spec '(form body))
9452
9453 ;;;
9454 ;;; Generic summary marking commands
9455 ;;;
9456
9457 (defvar gnus-summary-marking-alist
9458   '((read gnus-del-mark "d")
9459     (unread gnus-unread-mark "u")
9460     (ticked gnus-ticked-mark "!")
9461     (dormant gnus-dormant-mark "?")
9462     (expirable gnus-expirable-mark "e"))
9463   "An alist of names/marks/keystrokes.")
9464
9465 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9466 (defvar gnus-summary-mark-map)
9467
9468 (defun gnus-summary-make-all-marking-commands ()
9469   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9470   (dolist (elem gnus-summary-marking-alist)
9471     (apply 'gnus-summary-make-marking-command elem)))
9472
9473 (defun gnus-summary-make-marking-command (name mark keystroke)
9474   (let ((map (make-sparse-keymap)))
9475     (define-key gnus-summary-generic-mark-map keystroke map)
9476     (dolist (lway `((next "next" next nil "n")
9477                     (next-unread "next unread" next t "N")
9478                     (prev "previous" prev nil "p")
9479                     (prev-unread "previous unread" prev t "P")
9480                     (nomove "" nil nil ,keystroke)))
9481       (let ((func (gnus-summary-make-marking-command-1
9482                    mark (car lway) lway name)))
9483         (setq func (eval func))
9484         (define-key map (nth 4 lway) func)))))
9485       
9486 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9487   `(defun ,(intern
9488             (format "gnus-summary-put-mark-as-%s%s"
9489                     name (if (eq way 'nomove)
9490                              ""
9491                            (concat "-" (symbol-name way)))))
9492      (n)
9493      ,(format
9494        "Mark the current article as %s%s.
9495 If N, the prefix, then repeat N times.
9496 If N is negative, move in reverse order.
9497 The difference between N and the actual number of articles marked is
9498 returned."
9499        name (cadr lway))
9500      (interactive "p")
9501      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9502     
9503 (defun gnus-summary-generic-mark (n mark move unread)
9504   "Mark N articles with MARK."
9505   (unless (eq major-mode 'gnus-summary-mode)
9506     (error "This command can only be used in the summary buffer"))
9507   (gnus-summary-show-thread)
9508   (let ((nummove
9509          (cond
9510           ((eq move 'next) 1)
9511           ((eq move 'prev) -1)
9512           (t 0))))
9513     (if (zerop nummove)
9514         (setq n 1)
9515       (when (< n 0)
9516         (setq n (abs n)
9517               nummove (* -1 nummove))))
9518     (while (and (> n 0)
9519                 (gnus-summary-mark-article nil mark)
9520                 (zerop (gnus-summary-next-subject nummove unread t)))
9521       (setq n (1- n)))
9522     (when (/= 0 n)
9523       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9524     (gnus-summary-recenter)
9525     (gnus-summary-position-point)
9526     (gnus-set-mode-line 'summary)
9527     n))
9528
9529 (gnus-summary-make-all-marking-commands)
9530
9531 (gnus-ems-redefine)
9532
9533 (provide 'gnus-sum)
9534
9535 (run-hooks 'gnus-sum-load-hook)
9536
9537 ;;; gnus-sum.el ends here