Importing Pterodactyl Gnus v0.97.
[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         (gnus-score-adaptive))
5320       (when gnus-use-scoring
5321         (gnus-score-save)))
5322     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5323     ;; If we have several article buffers, we kill them at exit.
5324     (unless gnus-single-article-buffer
5325       (gnus-kill-buffer gnus-original-article-buffer)
5326       (setq gnus-article-current nil))
5327     (when gnus-use-cache
5328       (gnus-cache-possibly-remove-articles)
5329       (gnus-cache-save-buffers))
5330     (gnus-async-prefetch-remove-group group)
5331     (when gnus-suppress-duplicates
5332       (gnus-dup-enter-articles))
5333     (when gnus-use-trees
5334       (gnus-tree-close group))
5335     ;; Remove entries for this group.
5336     (nnmail-purge-split-history (gnus-group-real-name group))
5337     ;; Make all changes in this group permanent.
5338     (unless quit-config
5339       (gnus-run-hooks 'gnus-exit-group-hook)
5340       (gnus-summary-update-info))
5341     (gnus-close-group group)
5342     ;; Make sure where we were, and go to next newsgroup.
5343     (set-buffer gnus-group-buffer)
5344     (unless quit-config
5345       (gnus-group-jump-to-group group))
5346     (gnus-run-hooks 'gnus-summary-exit-hook)
5347     (unless (or quit-config
5348                 ;; If this group has disappeared from the summary
5349                 ;; buffer, don't skip forwards.
5350                 (not (string= group (gnus-group-group-name))))
5351       (gnus-group-next-unread-group 1))
5352     (setq group-point (point))
5353     (if temporary
5354         nil                             ;Nothing to do.
5355       ;; If we have several article buffers, we kill them at exit.
5356       (unless gnus-single-article-buffer
5357         (gnus-kill-buffer gnus-article-buffer)
5358         (gnus-kill-buffer gnus-original-article-buffer)
5359         (setq gnus-article-current nil))
5360       (set-buffer buf)
5361       (if (not gnus-kill-summary-on-exit)
5362           (gnus-deaden-summary)
5363         ;; We set all buffer-local variables to nil.  It is unclear why
5364         ;; this is needed, but if we don't, buffer-local variables are
5365         ;; not garbage-collected, it seems.  This would the lead to en
5366         ;; ever-growing Emacs.
5367         (gnus-summary-clear-local-variables)
5368         (when (get-buffer gnus-article-buffer)
5369           (bury-buffer gnus-article-buffer))
5370         ;; We clear the global counterparts of the buffer-local
5371         ;; variables as well, just to be on the safe side.
5372         (set-buffer gnus-group-buffer)
5373         (gnus-summary-clear-local-variables)
5374         ;; Return to group mode buffer.
5375         (when (eq mode 'gnus-summary-mode)
5376           (gnus-kill-buffer buf)))
5377       (setq gnus-current-select-method gnus-select-method)
5378       (pop-to-buffer gnus-group-buffer)
5379       (if (not quit-config)
5380           (progn
5381             (goto-char group-point)
5382             (gnus-configure-windows 'group 'force))
5383         (gnus-handle-ephemeral-exit quit-config))
5384       ;; Clear the current group name.
5385       (unless quit-config
5386         (setq gnus-newsgroup-name nil)))))
5387
5388 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5389 (defun gnus-summary-exit-no-update (&optional no-questions)
5390   "Quit reading current newsgroup without updating read article info."
5391   (interactive)
5392   (let* ((group gnus-newsgroup-name)
5393          (quit-config (gnus-group-quit-config group)))
5394     (when (or no-questions
5395               gnus-expert-user
5396               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5397       (gnus-async-halt-prefetch)
5398       (mapcar 'funcall
5399               (delq 'gnus-summary-expire-articles
5400                     (copy-list gnus-summary-prepare-exit-hook)))
5401       (when (gnus-buffer-live-p gnus-article-buffer)
5402         (save-excursion
5403           (set-buffer gnus-article-buffer)
5404           (mm-destroy-parts gnus-article-mime-handles)))
5405       ;; If we have several article buffers, we kill them at exit.
5406       (unless gnus-single-article-buffer
5407         (gnus-kill-buffer gnus-article-buffer)
5408         (gnus-kill-buffer gnus-original-article-buffer)
5409         (setq gnus-article-current nil))
5410       (if (not gnus-kill-summary-on-exit)
5411           (gnus-deaden-summary)
5412         (gnus-close-group group)
5413         (gnus-summary-clear-local-variables)
5414         (set-buffer gnus-group-buffer)
5415         (gnus-summary-clear-local-variables)
5416         (when (get-buffer gnus-summary-buffer)
5417           (kill-buffer gnus-summary-buffer)))
5418       (unless gnus-single-article-buffer
5419         (setq gnus-article-current nil))
5420       (when gnus-use-trees
5421         (gnus-tree-close group))
5422       (gnus-async-prefetch-remove-group group)
5423       (when (get-buffer gnus-article-buffer)
5424         (bury-buffer gnus-article-buffer))
5425       ;; Return to the group buffer.
5426       (gnus-configure-windows 'group 'force)
5427       ;; Clear the current group name.
5428       (setq gnus-newsgroup-name nil)
5429       (when (equal (gnus-group-group-name) group)
5430         (gnus-group-next-unread-group 1))
5431       (when quit-config
5432         (gnus-handle-ephemeral-exit quit-config)))))
5433
5434 (defun gnus-handle-ephemeral-exit (quit-config)
5435   "Handle movement when leaving an ephemeral group.
5436 The state which existed when entering the ephemeral is reset."
5437   (if (not (buffer-name (car quit-config)))
5438       (gnus-configure-windows 'group 'force)
5439     (set-buffer (car quit-config))
5440     (cond ((eq major-mode 'gnus-summary-mode)
5441            (gnus-set-global-variables))
5442           ((eq major-mode 'gnus-article-mode)
5443            (save-excursion
5444              ;; The `gnus-summary-buffer' variable may point
5445              ;; to the old summary buffer when using a single
5446              ;; article buffer.
5447              (unless (gnus-buffer-live-p gnus-summary-buffer)
5448                (set-buffer gnus-group-buffer))
5449              (set-buffer gnus-summary-buffer)
5450              (gnus-set-global-variables))))
5451     (if (or (eq (cdr quit-config) 'article)
5452             (eq (cdr quit-config) 'pick))
5453         (progn
5454           ;; The current article may be from the ephemeral group
5455           ;; thus it is best that we reload this article
5456           (gnus-summary-show-article)
5457           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5458               (gnus-configure-windows 'pick 'force)
5459             (gnus-configure-windows (cdr quit-config) 'force)))
5460       (gnus-configure-windows (cdr quit-config) 'force))
5461     (when (eq major-mode 'gnus-summary-mode)
5462       (gnus-summary-next-subject 1 nil t)
5463       (gnus-summary-recenter)
5464       (gnus-summary-position-point))))
5465
5466 ;;; Dead summaries.
5467
5468 (defvar gnus-dead-summary-mode-map nil)
5469
5470 (unless gnus-dead-summary-mode-map
5471   (setq gnus-dead-summary-mode-map (make-keymap))
5472   (suppress-keymap gnus-dead-summary-mode-map)
5473   (substitute-key-definition
5474    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5475   (let ((keys '("\C-d" "\r" "\177" [delete])))
5476     (while keys
5477       (define-key gnus-dead-summary-mode-map
5478         (pop keys) 'gnus-summary-wake-up-the-dead))))
5479
5480 (defvar gnus-dead-summary-mode nil
5481   "Minor mode for Gnus summary buffers.")
5482
5483 (defun gnus-dead-summary-mode (&optional arg)
5484   "Minor mode for Gnus summary buffers."
5485   (interactive "P")
5486   (when (eq major-mode 'gnus-summary-mode)
5487     (make-local-variable 'gnus-dead-summary-mode)
5488     (setq gnus-dead-summary-mode
5489           (if (null arg) (not gnus-dead-summary-mode)
5490             (> (prefix-numeric-value arg) 0)))
5491     (when gnus-dead-summary-mode
5492       (gnus-add-minor-mode
5493        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5494
5495 (defun gnus-deaden-summary ()
5496   "Make the current summary buffer into a dead summary buffer."
5497   ;; Kill any previous dead summary buffer.
5498   (when (and gnus-dead-summary
5499              (buffer-name gnus-dead-summary))
5500     (save-excursion
5501       (set-buffer gnus-dead-summary)
5502       (when gnus-dead-summary-mode
5503         (kill-buffer (current-buffer)))))
5504   ;; Make this the current dead summary.
5505   (setq gnus-dead-summary (current-buffer))
5506   (gnus-dead-summary-mode 1)
5507   (let ((name (buffer-name)))
5508     (when (string-match "Summary" name)
5509       (rename-buffer
5510        (concat (substring name 0 (match-beginning 0)) "Dead "
5511                (substring name (match-beginning 0)))
5512        t))))
5513
5514 (defun gnus-kill-or-deaden-summary (buffer)
5515   "Kill or deaden the summary BUFFER."
5516   (save-excursion
5517     (when (and (buffer-name buffer)
5518                (not gnus-single-article-buffer))
5519       (save-excursion
5520         (set-buffer buffer)
5521         (gnus-kill-buffer gnus-article-buffer)
5522         (gnus-kill-buffer gnus-original-article-buffer)))
5523     (cond (gnus-kill-summary-on-exit
5524            (when (and gnus-use-trees
5525                       (gnus-buffer-exists-p buffer))
5526              (save-excursion
5527                (set-buffer buffer)
5528                (gnus-tree-close gnus-newsgroup-name)))
5529            (gnus-kill-buffer buffer))
5530           ((gnus-buffer-exists-p buffer)
5531            (save-excursion
5532              (set-buffer buffer)
5533              (gnus-deaden-summary))))))
5534
5535 (defun gnus-summary-wake-up-the-dead (&rest args)
5536   "Wake up the dead summary buffer."
5537   (interactive)
5538   (gnus-dead-summary-mode -1)
5539   (let ((name (buffer-name)))
5540     (when (string-match "Dead " name)
5541       (rename-buffer
5542        (concat (substring name 0 (match-beginning 0))
5543                (substring name (match-end 0)))
5544        t)))
5545   (gnus-message 3 "This dead summary is now alive again"))
5546
5547 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5548 (defun gnus-summary-fetch-faq (&optional faq-dir)
5549   "Fetch the FAQ for the current group.
5550 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5551 in."
5552   (interactive
5553    (list
5554     (when current-prefix-arg
5555       (completing-read
5556        "Faq dir: " (and (listp gnus-group-faq-directory)
5557                         (mapcar (lambda (file) (list file))
5558                                 gnus-group-faq-directory))))))
5559   (let (gnus-faq-buffer)
5560     (when (setq gnus-faq-buffer
5561                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5562       (gnus-configure-windows 'summary-faq))))
5563
5564 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5565 (defun gnus-summary-describe-group (&optional force)
5566   "Describe the current newsgroup."
5567   (interactive "P")
5568   (gnus-group-describe-group force gnus-newsgroup-name))
5569
5570 (defun gnus-summary-describe-briefly ()
5571   "Describe summary mode commands briefly."
5572   (interactive)
5573   (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")))
5574
5575 ;; Walking around group mode buffer from summary mode.
5576
5577 (defun gnus-summary-next-group (&optional no-article target-group backward)
5578   "Exit current newsgroup and then select next unread newsgroup.
5579 If prefix argument NO-ARTICLE is non-nil, no article is selected
5580 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5581 previous group instead."
5582   (interactive "P")
5583   ;; Stop pre-fetching.
5584   (gnus-async-halt-prefetch)
5585   (let ((current-group gnus-newsgroup-name)
5586         (current-buffer (current-buffer))
5587         entered)
5588     ;; First we semi-exit this group to update Xrefs and all variables.
5589     ;; We can't do a real exit, because the window conf must remain
5590     ;; the same in case the user is prompted for info, and we don't
5591     ;; want the window conf to change before that...
5592     (gnus-summary-exit t)
5593     (while (not entered)
5594       ;; Then we find what group we are supposed to enter.
5595       (set-buffer gnus-group-buffer)
5596       (gnus-group-jump-to-group current-group)
5597       (setq target-group
5598             (or target-group
5599                 (if (eq gnus-keep-same-level 'best)
5600                     (gnus-summary-best-group gnus-newsgroup-name)
5601                   (gnus-summary-search-group backward gnus-keep-same-level))))
5602       (if (not target-group)
5603           ;; There are no further groups, so we return to the group
5604           ;; buffer.
5605           (progn
5606             (gnus-message 5 "Returning to the group buffer")
5607             (setq entered t)
5608             (when (gnus-buffer-live-p current-buffer)
5609               (set-buffer current-buffer)
5610               (gnus-summary-exit))
5611             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5612         ;; We try to enter the target group.
5613         (gnus-group-jump-to-group target-group)
5614         (let ((unreads (gnus-group-group-unread)))
5615           (if (and (or (eq t unreads)
5616                        (and unreads (not (zerop unreads))))
5617                    (gnus-summary-read-group
5618                     target-group nil no-article
5619                     (and (buffer-name current-buffer) current-buffer)
5620                     nil backward))
5621               (setq entered t)
5622             (setq current-group target-group
5623                   target-group nil)))))))
5624
5625 (defun gnus-summary-prev-group (&optional no-article)
5626   "Exit current newsgroup and then select previous unread newsgroup.
5627 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5628   (interactive "P")
5629   (gnus-summary-next-group no-article nil t))
5630
5631 ;; Walking around summary lines.
5632
5633 (defun gnus-summary-first-subject (&optional unread undownloaded)
5634   "Go to the first unread subject.
5635 If UNREAD is non-nil, go to the first unread article.
5636 Returns the article selected or nil if there are no unread articles."
5637   (interactive "P")
5638   (prog1
5639       (cond
5640        ;; Empty summary.
5641        ((null gnus-newsgroup-data)
5642         (gnus-message 3 "No articles in the group")
5643         nil)
5644        ;; Pick the first article.
5645        ((not unread)
5646         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5647         (gnus-data-number (car gnus-newsgroup-data)))
5648        ;; No unread articles.
5649        ((null gnus-newsgroup-unreads)
5650         (gnus-message 3 "No more unread articles")
5651         nil)
5652        ;; Find the first unread article.
5653        (t
5654         (let ((data gnus-newsgroup-data))
5655           (while (and data
5656                       (and (not (and undownloaded
5657                                      (eq gnus-undownloaded-mark
5658                                          (gnus-data-mark (car data)))))
5659                            (not (gnus-data-unread-p (car data)))))
5660             (setq data (cdr data)))
5661           (when data
5662             (goto-char (gnus-data-pos (car data)))
5663             (gnus-data-number (car data))))))
5664     (gnus-summary-position-point)))
5665
5666 (defun gnus-summary-next-subject (n &optional unread dont-display)
5667   "Go to next N'th summary line.
5668 If N is negative, go to the previous N'th subject line.
5669 If UNREAD is non-nil, only unread articles are selected.
5670 The difference between N and the actual number of steps taken is
5671 returned."
5672   (interactive "p")
5673   (let ((backward (< n 0))
5674         (n (abs n)))
5675     (while (and (> n 0)
5676                 (if backward
5677                     (gnus-summary-find-prev unread)
5678                   (gnus-summary-find-next unread)))
5679       (gnus-summary-show-thread)
5680       (setq n (1- n)))
5681     (when (/= 0 n)
5682       (gnus-message 7 "No more%s articles"
5683                     (if unread " unread" "")))
5684     (unless dont-display
5685       (gnus-summary-recenter)
5686       (gnus-summary-position-point))
5687     n))
5688
5689 (defun gnus-summary-next-unread-subject (n)
5690   "Go to next N'th unread summary line."
5691   (interactive "p")
5692   (gnus-summary-next-subject n t))
5693
5694 (defun gnus-summary-prev-subject (n &optional unread)
5695   "Go to previous N'th summary line.
5696 If optional argument UNREAD is non-nil, only unread article is selected."
5697   (interactive "p")
5698   (gnus-summary-next-subject (- n) unread))
5699
5700 (defun gnus-summary-prev-unread-subject (n)
5701   "Go to previous N'th unread summary line."
5702   (interactive "p")
5703   (gnus-summary-next-subject (- n) t))
5704
5705 (defun gnus-summary-goto-subject (article &optional force silent)
5706   "Go the subject line of ARTICLE.
5707 If FORCE, also allow jumping to articles not currently shown."
5708   (interactive "nArticle number: ")
5709   (let ((b (point))
5710         (data (gnus-data-find article)))
5711     ;; We read in the article if we have to.
5712     (and (not data)
5713          force
5714          (gnus-summary-insert-subject
5715           article
5716           (if (or (numberp force) (vectorp force)) force)
5717           t)
5718          (setq data (gnus-data-find article)))
5719     (goto-char b)
5720     (if (not data)
5721         (progn
5722           (unless silent
5723             (gnus-message 3 "Can't find article %d" article))
5724           nil)
5725       (goto-char (gnus-data-pos data))
5726       (gnus-summary-position-point)
5727       article)))
5728
5729 ;; Walking around summary lines with displaying articles.
5730
5731 (defun gnus-summary-expand-window (&optional arg)
5732   "Make the summary buffer take up the entire Emacs frame.
5733 Given a prefix, will force an `article' buffer configuration."
5734   (interactive "P")
5735   (if arg
5736       (gnus-configure-windows 'article 'force)
5737     (gnus-configure-windows 'summary 'force)))
5738
5739 (defun gnus-summary-display-article (article &optional all-header)
5740   "Display ARTICLE in article buffer."
5741   (gnus-set-global-variables)
5742   (if (null article)
5743       nil
5744     (prog1
5745         (if gnus-summary-display-article-function
5746             (funcall gnus-summary-display-article-function article all-header)
5747           (gnus-article-prepare article all-header))
5748       (gnus-run-hooks 'gnus-select-article-hook)
5749       (when (and gnus-current-article
5750                  (not (zerop gnus-current-article)))
5751         (gnus-summary-goto-subject gnus-current-article))
5752       (gnus-summary-recenter)
5753       (when (and gnus-use-trees gnus-show-threads)
5754         (gnus-possibly-generate-tree article)
5755         (gnus-highlight-selected-tree article))
5756       ;; Successfully display article.
5757       (gnus-article-set-window-start
5758        (cdr (assq article gnus-newsgroup-bookmarks))))))
5759
5760 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5761   "Select the current article.
5762 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5763 non-nil, the article will be re-fetched even if it already present in
5764 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5765 be displayed."
5766   ;; Make sure we are in the summary buffer to work around bbdb bug.
5767   (unless (eq major-mode 'gnus-summary-mode)
5768     (set-buffer gnus-summary-buffer))
5769   (let ((article (or article (gnus-summary-article-number)))
5770         (all-headers (not (not all-headers))) ;Must be T or NIL.
5771         gnus-summary-display-article-function
5772         did)
5773     (and (not pseudo)
5774          (gnus-summary-article-pseudo-p article)
5775          (error "This is a pseudo-article"))
5776     (prog1
5777         (save-excursion
5778           (set-buffer gnus-summary-buffer)
5779           (if (or (and gnus-single-article-buffer
5780                        (or (null gnus-current-article)
5781                            (null gnus-article-current)
5782                            (null (get-buffer gnus-article-buffer))
5783                            (not (eq article (cdr gnus-article-current)))
5784                            (not (equal (car gnus-article-current)
5785                                        gnus-newsgroup-name))))
5786                   (and (not gnus-single-article-buffer)
5787                        (or (null gnus-current-article)
5788                            (not (eq gnus-current-article article))))
5789                   force)
5790               ;; The requested article is different from the current article.
5791               (prog1
5792                   (gnus-summary-display-article article all-headers)
5793                 (setq did article)
5794                 (when (or all-headers gnus-show-all-headers)
5795                   (gnus-article-show-all-headers)))
5796             (when (or all-headers gnus-show-all-headers)
5797               (gnus-article-show-all-headers))
5798             'old))
5799       (when did
5800         (gnus-article-set-window-start
5801          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5802
5803 (defun gnus-summary-set-current-mark (&optional current-mark)
5804   "Obsolete function."
5805   nil)
5806
5807 (defun gnus-summary-next-article (&optional unread subject backward push)
5808   "Select the next article.
5809 If UNREAD, only unread articles are selected.
5810 If SUBJECT, only articles with SUBJECT are selected.
5811 If BACKWARD, the previous article is selected instead of the next."
5812   (interactive "P")
5813   (cond
5814    ;; Is there such an article?
5815    ((and (gnus-summary-search-forward unread subject backward)
5816          (or (gnus-summary-display-article (gnus-summary-article-number))
5817              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5818     (gnus-summary-position-point))
5819    ;; If not, we try the first unread, if that is wanted.
5820    ((and subject
5821          gnus-auto-select-same
5822          (gnus-summary-first-unread-article))
5823     (gnus-summary-position-point)
5824     (gnus-message 6 "Wrapped"))
5825    ;; Try to get next/previous article not displayed in this group.
5826    ((and gnus-auto-extend-newsgroup
5827          (not unread) (not subject))
5828     (gnus-summary-goto-article
5829      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5830      nil (count-lines (point-min) (point))))
5831    ;; Go to next/previous group.
5832    (t
5833     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5834       (gnus-summary-jump-to-group gnus-newsgroup-name))
5835     (let ((cmd last-command-char)
5836           (point
5837            (save-excursion
5838              (set-buffer gnus-group-buffer)
5839              (point)))
5840           (group
5841            (if (eq gnus-keep-same-level 'best)
5842                (gnus-summary-best-group gnus-newsgroup-name)
5843              (gnus-summary-search-group backward gnus-keep-same-level))))
5844       ;; For some reason, the group window gets selected.  We change
5845       ;; it back.
5846       (select-window (get-buffer-window (current-buffer)))
5847       ;; Select next unread newsgroup automagically.
5848       (cond
5849        ((or (not gnus-auto-select-next)
5850             (not cmd))
5851         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5852        ((or (eq gnus-auto-select-next 'quietly)
5853             (and (eq gnus-auto-select-next 'slightly-quietly)
5854                  push)
5855             (and (eq gnus-auto-select-next 'almost-quietly)
5856                  (gnus-summary-last-article-p)))
5857         ;; Select quietly.
5858         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5859             (gnus-summary-exit)
5860           (gnus-message 7 "No more%s articles (%s)..."
5861                         (if unread " unread" "")
5862                         (if group (concat "selecting " group)
5863                           "exiting"))
5864           (gnus-summary-next-group nil group backward)))
5865        (t
5866         (when (gnus-key-press-event-p last-input-event)
5867           (gnus-summary-walk-group-buffer
5868            gnus-newsgroup-name cmd unread backward point))))))))
5869
5870 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5871   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5872                       (?\C-p (gnus-group-prev-unread-group 1))))
5873         (cursor-in-echo-area t)
5874         keve key group ended)
5875     (save-excursion
5876       (set-buffer gnus-group-buffer)
5877       (goto-char start)
5878       (setq group
5879             (if (eq gnus-keep-same-level 'best)
5880                 (gnus-summary-best-group gnus-newsgroup-name)
5881               (gnus-summary-search-group backward gnus-keep-same-level))))
5882     (while (not ended)
5883       (gnus-message
5884        5 "No more%s articles%s" (if unread " unread" "")
5885        (if (and group
5886                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5887            (format " (Type %s for %s [%s])"
5888                    (single-key-description cmd) group
5889                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5890          (format " (Type %s to exit %s)"
5891                  (single-key-description cmd)
5892                  gnus-newsgroup-name)))
5893       ;; Confirm auto selection.
5894       (setq key (car (setq keve (gnus-read-event-char))))
5895       (setq ended t)
5896       (cond
5897        ((assq key keystrokes)
5898         (let ((obuf (current-buffer)))
5899           (switch-to-buffer gnus-group-buffer)
5900           (when group
5901             (gnus-group-jump-to-group group))
5902           (eval (cadr (assq key keystrokes)))
5903           (setq group (gnus-group-group-name))
5904           (switch-to-buffer obuf))
5905         (setq ended nil))
5906        ((equal key cmd)
5907         (if (or (not group)
5908                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5909             (gnus-summary-exit)
5910           (gnus-summary-next-group nil group backward)))
5911        (t
5912         (push (cdr keve) unread-command-events))))))
5913
5914 (defun gnus-summary-next-unread-article ()
5915   "Select unread article after current one."
5916   (interactive)
5917   (gnus-summary-next-article
5918    (or (not (eq gnus-summary-goto-unread 'never))
5919        (gnus-summary-last-article-p (gnus-summary-article-number)))
5920    (and gnus-auto-select-same
5921         (gnus-summary-article-subject))))
5922
5923 (defun gnus-summary-prev-article (&optional unread subject)
5924   "Select the article after the current one.
5925 If UNREAD is non-nil, only unread articles are selected."
5926   (interactive "P")
5927   (gnus-summary-next-article unread subject t))
5928
5929 (defun gnus-summary-prev-unread-article ()
5930   "Select unread article before current one."
5931   (interactive)
5932   (gnus-summary-prev-article
5933    (or (not (eq gnus-summary-goto-unread 'never))
5934        (gnus-summary-first-article-p (gnus-summary-article-number)))
5935    (and gnus-auto-select-same
5936         (gnus-summary-article-subject))))
5937
5938 (defun gnus-summary-next-page (&optional lines circular)
5939   "Show next page of the selected article.
5940 If at the end of the current article, select the next article.
5941 LINES says how many lines should be scrolled up.
5942
5943 If CIRCULAR is non-nil, go to the start of the article instead of
5944 selecting the next article when reaching the end of the current
5945 article."
5946   (interactive "P")
5947   (setq gnus-summary-buffer (current-buffer))
5948   (gnus-set-global-variables)
5949   (let ((article (gnus-summary-article-number))
5950         (article-window (get-buffer-window gnus-article-buffer t))
5951         endp)
5952     ;; If the buffer is empty, we have no article.
5953     (unless article
5954       (error "No article to select"))
5955     (gnus-configure-windows 'article)
5956     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5957         (if (and (eq gnus-summary-goto-unread 'never)
5958                  (not (gnus-summary-last-article-p article)))
5959             (gnus-summary-next-article)
5960           (gnus-summary-next-unread-article))
5961       (if (or (null gnus-current-article)
5962               (null gnus-article-current)
5963               (/= article (cdr gnus-article-current))
5964               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5965           ;; Selected subject is different from current article's.
5966           (gnus-summary-display-article article)
5967         (when article-window
5968           (gnus-eval-in-buffer-window gnus-article-buffer
5969             (setq endp (gnus-article-next-page lines)))
5970           (when endp
5971             (cond (circular
5972                    (gnus-summary-beginning-of-article))
5973                   (lines
5974                    (gnus-message 3 "End of message"))
5975                   ((null lines)
5976                    (if (and (eq gnus-summary-goto-unread 'never)
5977                             (not (gnus-summary-last-article-p article)))
5978                        (gnus-summary-next-article)
5979                      (gnus-summary-next-unread-article))))))))
5980     (gnus-summary-recenter)
5981     (gnus-summary-position-point)))
5982
5983 (defun gnus-summary-prev-page (&optional lines move)
5984   "Show previous page of selected article.
5985 Argument LINES specifies lines to be scrolled down.
5986 If MOVE, move to the previous unread article if point is at
5987 the beginning of the buffer."
5988   (interactive "P")
5989   (let ((article (gnus-summary-article-number))
5990         (article-window (get-buffer-window gnus-article-buffer t))
5991         endp)
5992     (gnus-configure-windows 'article)
5993     (if (or (null gnus-current-article)
5994             (null gnus-article-current)
5995             (/= article (cdr gnus-article-current))
5996             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5997         ;; Selected subject is different from current article's.
5998         (gnus-summary-display-article article)
5999       (gnus-summary-recenter)
6000       (when article-window
6001         (gnus-eval-in-buffer-window gnus-article-buffer
6002           (setq endp (gnus-article-prev-page lines)))
6003         (when (and move endp)
6004           (cond (lines
6005                  (gnus-message 3 "Beginning of message"))
6006                 ((null lines)
6007                  (if (and (eq gnus-summary-goto-unread 'never)
6008                           (not (gnus-summary-first-article-p article)))
6009                      (gnus-summary-prev-article)
6010                    (gnus-summary-prev-unread-article))))))))
6011   (gnus-summary-position-point))
6012
6013 (defun gnus-summary-prev-page-or-article (&optional lines)
6014   "Show previous page of selected article.
6015 Argument LINES specifies lines to be scrolled down.
6016 If at the beginning of the article, go to the next article."
6017   (interactive "P")
6018   (gnus-summary-prev-page lines t))
6019
6020 (defun gnus-summary-scroll-up (lines)
6021   "Scroll up (or down) one line current article.
6022 Argument LINES specifies lines to be scrolled up (or down if negative)."
6023   (interactive "p")
6024   (gnus-configure-windows 'article)
6025   (gnus-summary-show-thread)
6026   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6027     (gnus-eval-in-buffer-window gnus-article-buffer
6028       (cond ((> lines 0)
6029              (when (gnus-article-next-page lines)
6030                (gnus-message 3 "End of message")))
6031             ((< lines 0)
6032              (gnus-article-prev-page (- lines))))))
6033   (gnus-summary-recenter)
6034   (gnus-summary-position-point))
6035
6036 (defun gnus-summary-scroll-down (lines)
6037   "Scroll down (or up) one line current article.
6038 Argument LINES specifies lines to be scrolled down (or up if negative)."
6039   (interactive "p")
6040   (gnus-summary-scroll-up (- lines)))
6041
6042 (defun gnus-summary-next-same-subject ()
6043   "Select next article which has the same subject as current one."
6044   (interactive)
6045   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6046
6047 (defun gnus-summary-prev-same-subject ()
6048   "Select previous article which has the same subject as current one."
6049   (interactive)
6050   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6051
6052 (defun gnus-summary-next-unread-same-subject ()
6053   "Select next unread article which has the same subject as current one."
6054   (interactive)
6055   (gnus-summary-next-article t (gnus-summary-article-subject)))
6056
6057 (defun gnus-summary-prev-unread-same-subject ()
6058   "Select previous unread article which has the same subject as current one."
6059   (interactive)
6060   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6061
6062 (defun gnus-summary-first-unread-article ()
6063   "Select the first unread article.
6064 Return nil if there are no unread articles."
6065   (interactive)
6066   (prog1
6067       (when (gnus-summary-first-subject t)
6068         (gnus-summary-show-thread)
6069         (gnus-summary-first-subject t)
6070         (gnus-summary-display-article (gnus-summary-article-number)))
6071     (gnus-summary-position-point)))
6072
6073 (defun gnus-summary-first-unread-subject ()
6074   "Place the point on the subject line of the first unread article.
6075 Return nil if there are no unread articles."
6076   (interactive)
6077   (prog1
6078       (when (gnus-summary-first-subject t)
6079         (gnus-summary-show-thread)
6080         (gnus-summary-first-subject t))
6081     (gnus-summary-position-point)))
6082
6083 (defun gnus-summary-first-article ()
6084   "Select the first article.
6085 Return nil if there are no articles."
6086   (interactive)
6087   (prog1
6088       (when (gnus-summary-first-subject)
6089         (gnus-summary-show-thread)
6090         (gnus-summary-first-subject)
6091         (gnus-summary-display-article (gnus-summary-article-number)))
6092     (gnus-summary-position-point)))
6093
6094 (defun gnus-summary-best-unread-article ()
6095   "Select the unread article with the highest score."
6096   (interactive)
6097   (let ((best -1000000)
6098         (data gnus-newsgroup-data)
6099         article score)
6100     (while data
6101       (and (gnus-data-unread-p (car data))
6102            (> (setq score
6103                     (gnus-summary-article-score (gnus-data-number (car data))))
6104               best)
6105            (setq best score
6106                  article (gnus-data-number (car data))))
6107       (setq data (cdr data)))
6108     (prog1
6109         (if article
6110             (gnus-summary-goto-article article)
6111           (error "No unread articles"))
6112       (gnus-summary-position-point))))
6113
6114 (defun gnus-summary-last-subject ()
6115   "Go to the last displayed subject line in the group."
6116   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6117     (when article
6118       (gnus-summary-goto-subject article))))
6119
6120 (defun gnus-summary-goto-article (article &optional all-headers force)
6121   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6122 If ALL-HEADERS is non-nil, no header lines are hidden.
6123 If FORCE, go to the article even if it isn't displayed.  If FORCE
6124 is a number, it is the line the article is to be displayed on."
6125   (interactive
6126    (list
6127     (completing-read
6128      "Article number or Message-ID: "
6129      (mapcar (lambda (number) (list (int-to-string number)))
6130              gnus-newsgroup-limit))
6131     current-prefix-arg
6132     t))
6133   (prog1
6134       (if (and (stringp article)
6135                (string-match "@" article))
6136           (gnus-summary-refer-article article)
6137         (when (stringp article)
6138           (setq article (string-to-number article)))
6139         (if (gnus-summary-goto-subject article force)
6140             (gnus-summary-display-article article all-headers)
6141           (gnus-message 4 "Couldn't go to article %s" article) nil))
6142     (gnus-summary-position-point)))
6143
6144 (defun gnus-summary-goto-last-article ()
6145   "Go to the previously read article."
6146   (interactive)
6147   (prog1
6148       (when gnus-last-article
6149         (gnus-summary-goto-article gnus-last-article nil t))
6150     (gnus-summary-position-point)))
6151
6152 (defun gnus-summary-pop-article (number)
6153   "Pop one article off the history and go to the previous.
6154 NUMBER articles will be popped off."
6155   (interactive "p")
6156   (let (to)
6157     (setq gnus-newsgroup-history
6158           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6159     (if to
6160         (gnus-summary-goto-article (car to) nil t)
6161       (error "Article history empty")))
6162   (gnus-summary-position-point))
6163
6164 ;; Summary commands and functions for limiting the summary buffer.
6165
6166 (defun gnus-summary-limit-to-articles (n)
6167   "Limit the summary buffer to the next N articles.
6168 If not given a prefix, use the process marked articles instead."
6169   (interactive "P")
6170   (prog1
6171       (let ((articles (gnus-summary-work-articles n)))
6172         (setq gnus-newsgroup-processable nil)
6173         (gnus-summary-limit articles))
6174     (gnus-summary-position-point)))
6175
6176 (defun gnus-summary-pop-limit (&optional total)
6177   "Restore the previous limit.
6178 If given a prefix, remove all limits."
6179   (interactive "P")
6180   (when total
6181     (setq gnus-newsgroup-limits
6182           (list (mapcar (lambda (h) (mail-header-number h))
6183                         gnus-newsgroup-headers))))
6184   (unless gnus-newsgroup-limits
6185     (error "No limit to pop"))
6186   (prog1
6187       (gnus-summary-limit nil 'pop)
6188     (gnus-summary-position-point)))
6189
6190 (defun gnus-summary-limit-to-subject (subject &optional header)
6191   "Limit the summary buffer to articles that have subjects that match a regexp."
6192   (interactive "sLimit to subject (regexp): ")
6193   (unless header
6194     (setq header "subject"))
6195   (when (not (equal "" subject))
6196     (prog1
6197         (let ((articles (gnus-summary-find-matching
6198                          (or header "subject") subject 'all)))
6199           (unless articles
6200             (error "Found no matches for \"%s\"" subject))
6201           (gnus-summary-limit articles))
6202       (gnus-summary-position-point))))
6203
6204 (defun gnus-summary-limit-to-author (from)
6205   "Limit the summary buffer to articles that have authors that match a regexp."
6206   (interactive "sLimit to author (regexp): ")
6207   (gnus-summary-limit-to-subject from "from"))
6208
6209 (defun gnus-summary-limit-to-age (age &optional younger-p)
6210   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6211 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6212 articles that are younger than AGE days."
6213   (interactive
6214    (let ((younger current-prefix-arg)
6215          (days-got nil)
6216          days)
6217      (while (not days-got)
6218        (setq days (if younger
6219                       (read-string "Limit to articles within (in days): ")
6220                     (read-string "Limit to articles old than (in days): ")))
6221        (when (> (length days) 0)
6222          (setq days (read days)))
6223        (if (numberp days)
6224            (setq days-got t)
6225          (message "Please enter a number.")
6226          (sleep-for 1)))
6227      (list days younger)))
6228   (prog1
6229       (let ((data gnus-newsgroup-data)
6230             (cutoff (days-to-time age))
6231             articles d date is-younger)
6232         (while (setq d (pop data))
6233           (when (and (vectorp (gnus-data-header d))
6234                      (setq date (mail-header-date (gnus-data-header d))))
6235             (setq is-younger (time-less-p
6236                               (time-since (date-to-time date))
6237                               cutoff))
6238             (when (if younger-p
6239                       is-younger
6240                     (not is-younger))
6241               (push (gnus-data-number d) articles))))
6242         (gnus-summary-limit (nreverse articles)))
6243     (gnus-summary-position-point)))
6244
6245 (defun gnus-summary-limit-to-extra (header regexp)
6246   "Limit the summary buffer to articles that match an 'extra' header."
6247   (interactive
6248    (let ((header
6249           (intern
6250            (gnus-completing-read
6251             (symbol-name (car gnus-extra-headers))      
6252             "Limit extra header:"       
6253             (mapcar (lambda (x) 
6254                       (cons (symbol-name x) x))
6255                     gnus-extra-headers)
6256             nil                 
6257             t))))
6258      (list header
6259            (read-string (format "Limit to header %s (regexp): " header)))))
6260   (when (not (equal "" regexp))
6261     (prog1
6262         (let ((articles (gnus-summary-find-matching
6263                          (cons 'extra header) regexp 'all)))
6264           (unless articles
6265             (error "Found no matches for \"%s\"" regexp))
6266           (gnus-summary-limit articles))
6267       (gnus-summary-position-point))))
6268
6269 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6270 (make-obsolete
6271  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6272
6273 (defun gnus-summary-limit-to-unread (&optional all)
6274   "Limit the summary buffer to articles that are not marked as read.
6275 If ALL is non-nil, limit strictly to unread articles."
6276   (interactive "P")
6277   (if all
6278       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6279     (gnus-summary-limit-to-marks
6280      ;; Concat all the marks that say that an article is read and have
6281      ;; those removed.
6282      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6283            gnus-killed-mark gnus-kill-file-mark
6284            gnus-low-score-mark gnus-expirable-mark
6285            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6286            gnus-duplicate-mark gnus-souped-mark)
6287      'reverse)))
6288
6289 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6290 (make-obsolete 'gnus-summary-delete-marked-with
6291                'gnus-summary-limit-exlude-marks)
6292
6293 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6294   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6295 If REVERSE, limit the summary buffer to articles that are marked
6296 with MARKS.  MARKS can either be a string of marks or a list of marks.
6297 Returns how many articles were removed."
6298   (interactive "sMarks: ")
6299   (gnus-summary-limit-to-marks marks t))
6300
6301 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6302   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6303 If REVERSE (the prefix), limit the summary buffer to articles that are
6304 not marked with MARKS.  MARKS can either be a string of marks or a
6305 list of marks.
6306 Returns how many articles were removed."
6307   (interactive "sMarks: \nP")
6308   (prog1
6309       (let ((data gnus-newsgroup-data)
6310             (marks (if (listp marks) marks
6311                      (append marks nil))) ; Transform to list.
6312             articles)
6313         (while data
6314           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6315                   (memq (gnus-data-mark (car data)) marks))
6316             (push (gnus-data-number (car data)) articles))
6317           (setq data (cdr data)))
6318         (gnus-summary-limit articles))
6319     (gnus-summary-position-point)))
6320
6321 (defun gnus-summary-limit-to-score (&optional score)
6322   "Limit to articles with score at or above SCORE."
6323   (interactive "P")
6324   (setq score (if score
6325                   (prefix-numeric-value score)
6326                 (or gnus-summary-default-score 0)))
6327   (let ((data gnus-newsgroup-data)
6328         articles)
6329     (while data
6330       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6331                 score)
6332         (push (gnus-data-number (car data)) articles))
6333       (setq data (cdr data)))
6334     (prog1
6335         (gnus-summary-limit articles)
6336       (gnus-summary-position-point))))
6337
6338 (defun gnus-summary-limit-include-thread (id)
6339   "Display all the hidden articles that in the current thread."
6340   (interactive (list (mail-header-id (gnus-summary-article-header))))
6341   (let ((articles (gnus-articles-in-thread
6342                    (gnus-id-to-thread (gnus-root-id id)))))
6343     (prog1
6344         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6345       (gnus-summary-position-point))))
6346
6347 (defun gnus-summary-limit-include-dormant ()
6348   "Display all the hidden articles that are marked as dormant.
6349 Note that this command only works on a subset of the articles currently
6350 fetched for this group."
6351   (interactive)
6352   (unless gnus-newsgroup-dormant
6353     (error "There are no dormant articles in this group"))
6354   (prog1
6355       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6356     (gnus-summary-position-point)))
6357
6358 (defun gnus-summary-limit-exclude-dormant ()
6359   "Hide all dormant articles."
6360   (interactive)
6361   (prog1
6362       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6363     (gnus-summary-position-point)))
6364
6365 (defun gnus-summary-limit-exclude-childless-dormant ()
6366   "Hide all dormant articles that have no children."
6367   (interactive)
6368   (let ((data (gnus-data-list t))
6369         articles d children)
6370     ;; Find all articles that are either not dormant or have
6371     ;; children.
6372     (while (setq d (pop data))
6373       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6374                 (and (setq children
6375                            (gnus-article-children (gnus-data-number d)))
6376                      (let (found)
6377                        (while children
6378                          (when (memq (car children) articles)
6379                            (setq children nil
6380                                  found t))
6381                          (pop children))
6382                        found)))
6383         (push (gnus-data-number d) articles)))
6384     ;; Do the limiting.
6385     (prog1
6386         (gnus-summary-limit articles)
6387       (gnus-summary-position-point))))
6388
6389 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6390   "Mark all unread excluded articles as read.
6391 If ALL, mark even excluded ticked and dormants as read."
6392   (interactive "P")
6393   (let ((articles (gnus-sorted-complement
6394                    (sort
6395                     (mapcar (lambda (h) (mail-header-number h))
6396                             gnus-newsgroup-headers)
6397                     '<)
6398                    (sort gnus-newsgroup-limit '<)))
6399         article)
6400     (setq gnus-newsgroup-unreads
6401           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6402     (if all
6403         (setq gnus-newsgroup-dormant nil
6404               gnus-newsgroup-marked nil
6405               gnus-newsgroup-reads
6406               (nconc
6407                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6408                gnus-newsgroup-reads))
6409       (while (setq article (pop articles))
6410         (unless (or (memq article gnus-newsgroup-dormant)
6411                     (memq article gnus-newsgroup-marked))
6412           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6413
6414 (defun gnus-summary-limit (articles &optional pop)
6415   (if pop
6416       ;; We pop the previous limit off the stack and use that.
6417       (setq articles (car gnus-newsgroup-limits)
6418             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6419     ;; We use the new limit, so we push the old limit on the stack.
6420     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6421   ;; Set the limit.
6422   (setq gnus-newsgroup-limit articles)
6423   (let ((total (length gnus-newsgroup-data))
6424         (data (gnus-data-find-list (gnus-summary-article-number)))
6425         (gnus-summary-mark-below nil)   ; Inhibit this.
6426         found)
6427     ;; This will do all the work of generating the new summary buffer
6428     ;; according to the new limit.
6429     (gnus-summary-prepare)
6430     ;; Hide any threads, possibly.
6431     (and gnus-show-threads
6432          gnus-thread-hide-subtree
6433          (gnus-summary-hide-all-threads))
6434     ;; Try to return to the article you were at, or one in the
6435     ;; neighborhood.
6436     (when data
6437       ;; We try to find some article after the current one.
6438       (while data
6439         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6440           (setq data nil
6441                 found t))
6442         (setq data (cdr data))))
6443     (unless found
6444       ;; If there is no data, that means that we were after the last
6445       ;; article.  The same goes when we can't find any articles
6446       ;; after the current one.
6447       (goto-char (point-max))
6448       (gnus-summary-find-prev))
6449     (gnus-set-mode-line 'summary)
6450     ;; We return how many articles were removed from the summary
6451     ;; buffer as a result of the new limit.
6452     (- total (length gnus-newsgroup-data))))
6453
6454 (defsubst gnus-invisible-cut-children (threads)
6455   (let ((num 0))
6456     (while threads
6457       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6458         (incf num))
6459       (pop threads))
6460     (< num 2)))
6461
6462 (defsubst gnus-cut-thread (thread)
6463   "Go forwards in the thread until we find an article that we want to display."
6464   (when (or (eq gnus-fetch-old-headers 'some)
6465             (eq gnus-fetch-old-headers 'invisible)
6466             (eq gnus-build-sparse-threads 'some)
6467             (eq gnus-build-sparse-threads 'more))
6468     ;; Deal with old-fetched headers and sparse threads.
6469     (while (and
6470             thread
6471             (or
6472              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6473              (gnus-summary-article-ancient-p
6474               (mail-header-number (car thread))))
6475             (if (or (<= (length (cdr thread)) 1)
6476                     (eq gnus-fetch-old-headers 'invisible))
6477                 (setq gnus-newsgroup-limit
6478                       (delq (mail-header-number (car thread))
6479                             gnus-newsgroup-limit)
6480                       thread (cadr thread))
6481               (when (gnus-invisible-cut-children (cdr thread))
6482                 (let ((th (cdr thread)))
6483                   (while th
6484                     (if (memq (mail-header-number (caar th))
6485                               gnus-newsgroup-limit)
6486                         (setq thread (car th)
6487                               th nil)
6488                       (setq th (cdr th))))))))))
6489   thread)
6490
6491 (defun gnus-cut-threads (threads)
6492   "Cut off all uninteresting articles from the beginning of threads."
6493   (when (or (eq gnus-fetch-old-headers 'some)
6494             (eq gnus-fetch-old-headers 'invisible)
6495             (eq gnus-build-sparse-threads 'some)
6496             (eq gnus-build-sparse-threads 'more))
6497     (let ((th threads))
6498       (while th
6499         (setcar th (gnus-cut-thread (car th)))
6500         (setq th (cdr th)))))
6501   ;; Remove nixed out threads.
6502   (delq nil threads))
6503
6504 (defun gnus-summary-initial-limit (&optional show-if-empty)
6505   "Figure out what the initial limit is supposed to be on group entry.
6506 This entails weeding out unwanted dormants, low-scored articles,
6507 fetch-old-headers verbiage, and so on."
6508   ;; Most groups have nothing to remove.
6509   (if (or gnus-inhibit-limiting
6510           (and (null gnus-newsgroup-dormant)
6511                (not (eq gnus-fetch-old-headers 'some))
6512                (not (eq gnus-fetch-old-headers 'invisible))
6513                (null gnus-summary-expunge-below)
6514                (not (eq gnus-build-sparse-threads 'some))
6515                (not (eq gnus-build-sparse-threads 'more))
6516                (null gnus-thread-expunge-below)
6517                (not gnus-use-nocem)))
6518       ()                                ; Do nothing.
6519     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6520     (setq gnus-newsgroup-limit nil)
6521     (mapatoms
6522      (lambda (node)
6523        (unless (car (symbol-value node))
6524          ;; These threads have no parents -- they are roots.
6525          (let ((nodes (cdr (symbol-value node)))
6526                thread)
6527            (while nodes
6528              (if (and gnus-thread-expunge-below
6529                       (< (gnus-thread-total-score (car nodes))
6530                          gnus-thread-expunge-below))
6531                  (gnus-expunge-thread (pop nodes))
6532                (setq thread (pop nodes))
6533                (gnus-summary-limit-children thread))))))
6534      gnus-newsgroup-dependencies)
6535     ;; If this limitation resulted in an empty group, we might
6536     ;; pop the previous limit and use it instead.
6537     (when (and (not gnus-newsgroup-limit)
6538                show-if-empty)
6539       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6540     gnus-newsgroup-limit))
6541
6542 (defun gnus-summary-limit-children (thread)
6543   "Return 1 if this subthread is visible and 0 if it is not."
6544   ;; First we get the number of visible children to this thread.  This
6545   ;; is done by recursing down the thread using this function, so this
6546   ;; will really go down to a leaf article first, before slowly
6547   ;; working its way up towards the root.
6548   (when thread
6549     (let ((children
6550            (if (cdr thread)
6551                (apply '+ (mapcar 'gnus-summary-limit-children
6552                                  (cdr thread)))
6553              0))
6554           (number (mail-header-number (car thread)))
6555           score)
6556       (if (and
6557            (not (memq number gnus-newsgroup-marked))
6558            (or
6559             ;; If this article is dormant and has absolutely no visible
6560             ;; children, then this article isn't visible.
6561             (and (memq number gnus-newsgroup-dormant)
6562                  (zerop children))
6563             ;; If this is "fetch-old-headered" and there is no
6564             ;; visible children, then we don't want this article.
6565             (and (eq gnus-fetch-old-headers 'some)
6566                  (gnus-summary-article-ancient-p number)
6567                  (zerop children))
6568             ;; If this is "fetch-old-headered" and `invisible', then
6569             ;; we don't want this article.
6570             (and (eq gnus-fetch-old-headers 'invisible)
6571                  (gnus-summary-article-ancient-p number))
6572             ;; If this is a sparsely inserted article with no children,
6573             ;; we don't want it.
6574             (and (eq gnus-build-sparse-threads 'some)
6575                  (gnus-summary-article-sparse-p number)
6576                  (zerop children))
6577             ;; If we use expunging, and this article is really
6578             ;; low-scored, then we don't want this article.
6579             (when (and gnus-summary-expunge-below
6580                        (< (setq score
6581                                 (or (cdr (assq number gnus-newsgroup-scored))
6582                                     gnus-summary-default-score))
6583                           gnus-summary-expunge-below))
6584               ;; We increase the expunge-tally here, but that has
6585               ;; nothing to do with the limits, really.
6586               (incf gnus-newsgroup-expunged-tally)
6587               ;; We also mark as read here, if that's wanted.
6588               (when (and gnus-summary-mark-below
6589                          (< score gnus-summary-mark-below))
6590                 (setq gnus-newsgroup-unreads
6591                       (delq number gnus-newsgroup-unreads))
6592                 (if gnus-newsgroup-auto-expire
6593                     (push number gnus-newsgroup-expirable)
6594                   (push (cons number gnus-low-score-mark)
6595                         gnus-newsgroup-reads)))
6596               t)
6597             ;; Check NoCeM things.
6598             (if (and gnus-use-nocem
6599                      (gnus-nocem-unwanted-article-p
6600                       (mail-header-id (car thread))))
6601                 (progn
6602                   (setq gnus-newsgroup-unreads
6603                         (delq number gnus-newsgroup-unreads))
6604                   t))))
6605           ;; Nope, invisible article.
6606           0
6607         ;; Ok, this article is to be visible, so we add it to the limit
6608         ;; and return 1.
6609         (push number gnus-newsgroup-limit)
6610         1))))
6611
6612 (defun gnus-expunge-thread (thread)
6613   "Mark all articles in THREAD as read."
6614   (let* ((number (mail-header-number (car thread))))
6615     (incf gnus-newsgroup-expunged-tally)
6616     ;; We also mark as read here, if that's wanted.
6617     (setq gnus-newsgroup-unreads
6618           (delq number gnus-newsgroup-unreads))
6619     (if gnus-newsgroup-auto-expire
6620         (push number gnus-newsgroup-expirable)
6621       (push (cons number gnus-low-score-mark)
6622             gnus-newsgroup-reads)))
6623   ;; Go recursively through all subthreads.
6624   (mapcar 'gnus-expunge-thread (cdr thread)))
6625
6626 ;; Summary article oriented commands
6627
6628 (defun gnus-summary-refer-parent-article (n)
6629   "Refer parent article N times.
6630 If N is negative, go to ancestor -N instead.
6631 The difference between N and the number of articles fetched is returned."
6632   (interactive "p")
6633   (let ((skip 1)
6634         error header ref)
6635     (when (not (natnump n))
6636       (setq skip (abs n)
6637             n 1))
6638     (while (and (> n 0)
6639                 (not error))
6640       (setq header (gnus-summary-article-header))
6641       (if (and (eq (mail-header-number header)
6642                    (cdr gnus-article-current))
6643                (equal gnus-newsgroup-name
6644                       (car gnus-article-current)))
6645           ;; If we try to find the parent of the currently
6646           ;; displayed article, then we take a look at the actual
6647           ;; References header, since this is slightly more
6648           ;; reliable than the References field we got from the
6649           ;; server.
6650           (save-excursion
6651             (set-buffer gnus-original-article-buffer)
6652             (nnheader-narrow-to-headers)
6653             (unless (setq ref (message-fetch-field "references"))
6654               (setq ref (message-fetch-field "in-reply-to")))
6655             (widen))
6656         (setq ref
6657               ;; It's not the current article, so we take a bet on
6658               ;; the value we got from the server.
6659               (mail-header-references header)))
6660       (if (and ref
6661                (not (equal ref "")))
6662           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6663             (gnus-message 1 "Couldn't find parent"))
6664         (gnus-message 1 "No references in article %d"
6665                       (gnus-summary-article-number))
6666         (setq error t))
6667       (decf n))
6668     (gnus-summary-position-point)
6669     n))
6670
6671 (defun gnus-summary-refer-references ()
6672   "Fetch all articles mentioned in the References header.
6673 Return the number of articles fetched."
6674   (interactive)
6675   (let ((ref (mail-header-references (gnus-summary-article-header)))
6676         (current (gnus-summary-article-number))
6677         (n 0))
6678     (if (or (not ref)
6679             (equal ref ""))
6680         (error "No References in the current article")
6681       ;; For each Message-ID in the References header...
6682       (while (string-match "<[^>]*>" ref)
6683         (incf n)
6684         ;; ... fetch that article.
6685         (gnus-summary-refer-article
6686          (prog1 (match-string 0 ref)
6687            (setq ref (substring ref (match-end 0))))))
6688       (gnus-summary-goto-subject current)
6689       (gnus-summary-position-point)
6690       n)))
6691
6692 (defun gnus-summary-refer-thread (&optional limit)
6693   "Fetch all articles in the current thread.
6694 If LIMIT (the numerical prefix), fetch that many old headers instead
6695 of what's specified by the `gnus-refer-thread-limit' variable."
6696   (interactive "P")
6697   (let ((id (mail-header-id (gnus-summary-article-header)))
6698         (limit (if limit (prefix-numeric-value limit)
6699                  gnus-refer-thread-limit)))
6700     ;; We want to fetch LIMIT *old* headers, but we also have to
6701     ;; re-fetch all the headers in the current buffer, because many of
6702     ;; them may be undisplayed.  So we adjust LIMIT.
6703     (when (numberp limit)
6704       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6705     (unless (eq gnus-fetch-old-headers 'invisible)
6706       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6707       ;; Retrieve the headers and read them in.
6708       (if (eq (gnus-retrieve-headers
6709                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6710               'nov)
6711           (gnus-build-all-threads)
6712         (error "Can't fetch thread from backends that don't support NOV"))
6713       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6714     (gnus-summary-limit-include-thread id)))
6715
6716 (defun gnus-summary-refer-article (message-id &optional arg)
6717   "Fetch an article specified by MESSAGE-ID.
6718 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6719 or `gnus-select-method', no matter what backend the article comes from."
6720   (interactive "sMessage-ID: \nP")
6721   (when (and (stringp message-id)
6722              (not (zerop (length message-id))))
6723     ;; Construct the correct Message-ID if necessary.
6724     ;; Suggested by tale@pawl.rpi.edu.
6725     (unless (string-match "^<" message-id)
6726       (setq message-id (concat "<" message-id)))
6727     (unless (string-match ">$" message-id)
6728       (setq message-id (concat message-id ">")))
6729     (let* ((header (gnus-id-to-header message-id))
6730            (sparse (and header
6731                         (gnus-summary-article-sparse-p
6732                          (mail-header-number header))
6733                         (memq (mail-header-number header)
6734                               gnus-newsgroup-limit))))
6735       (cond
6736        ;; If the article is present in the buffer we just go to it.
6737        ((and header
6738              (or (not (gnus-summary-article-sparse-p
6739                        (mail-header-number header)))
6740                  sparse))
6741         (prog1
6742             (gnus-summary-goto-article
6743              (mail-header-number header) nil t)
6744           (when sparse
6745             (gnus-summary-update-article (mail-header-number header)))))
6746        (t
6747         ;; We fetch the article
6748         (let ((gnus-override-method
6749                (cond ((gnus-news-group-p gnus-newsgroup-name)
6750                       gnus-refer-article-method)
6751                      (arg
6752                       (or gnus-refer-article-method gnus-select-method))
6753                      (t nil)))
6754               number)
6755           ;; Start the special refer-article method, if necessary.
6756           (when (and gnus-refer-article-method
6757                      (gnus-news-group-p gnus-newsgroup-name))
6758             (gnus-check-server gnus-refer-article-method))
6759           ;; Fetch the header, and display the article.
6760           (if (setq number (gnus-summary-insert-subject message-id))
6761               (gnus-summary-select-article nil nil nil number)
6762             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6763
6764 (defun gnus-summary-edit-parameters ()
6765   "Edit the group parameters of the current group."
6766   (interactive)
6767   (gnus-group-edit-group gnus-newsgroup-name 'params))
6768
6769 (defun gnus-summary-customize-parameters ()
6770   "Customize the group parameters of the current group."
6771   (interactive)
6772   (gnus-group-customize gnus-newsgroup-name))
6773
6774 (defun gnus-summary-enter-digest-group (&optional force)
6775   "Enter an nndoc group based on the current article.
6776 If FORCE, force a digest interpretation.  If not, try
6777 to guess what the document format is."
6778   (interactive "P")
6779   (let ((conf gnus-current-window-configuration))
6780     (save-excursion
6781       (gnus-summary-select-article))
6782     (setq gnus-current-window-configuration conf)
6783     (let* ((name (format "%s-%d"
6784                          (gnus-group-prefixed-name
6785                           gnus-newsgroup-name (list 'nndoc ""))
6786                          (save-excursion
6787                            (set-buffer gnus-summary-buffer)
6788                            gnus-current-article)))
6789            (ogroup gnus-newsgroup-name)
6790            (params (append (gnus-info-params (gnus-get-info ogroup))
6791                            (list (cons 'to-group ogroup))
6792                            (list (cons 'save-article-group ogroup))))
6793            (case-fold-search t)
6794            (buf (current-buffer))
6795            dig)
6796       (save-excursion
6797         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6798         (insert-buffer-substring gnus-original-article-buffer)
6799         ;; Remove lines that may lead nndoc to misinterpret the
6800         ;; document type.
6801         (narrow-to-region
6802          (goto-char (point-min))
6803          (or (search-forward "\n\n" nil t) (point)))
6804         (goto-char (point-min))
6805         (delete-matching-lines "^Path:\\|^From ")
6806         (widen))
6807       (unwind-protect
6808           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6809                     (gnus-newsgroup-ephemeral-ignored-charsets
6810                      gnus-newsgroup-ignored-charsets))
6811                 (gnus-group-read-ephemeral-group
6812                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6813                               (nndoc-article-type
6814                                ,(if force 'mbox 'guess))) t))
6815               ;; Make all postings to this group go to the parent group.
6816               (nconc (gnus-info-params (gnus-get-info name))
6817                      params)
6818             ;; Couldn't select this doc group.
6819             (switch-to-buffer buf)
6820             (gnus-set-global-variables)
6821             (gnus-configure-windows 'summary)
6822             (gnus-message 3 "Article couldn't be entered?"))
6823         (kill-buffer dig)))))
6824
6825 (defun gnus-summary-read-document (n)
6826   "Open a new group based on the current article(s).
6827 This will allow you to read digests and other similar
6828 documents as newsgroups.
6829 Obeys the standard process/prefix convention."
6830   (interactive "P")
6831   (let* ((articles (gnus-summary-work-articles n))
6832          (ogroup gnus-newsgroup-name)
6833          (params (append (gnus-info-params (gnus-get-info ogroup))
6834                          (list (cons 'to-group ogroup))))
6835          article group egroup groups vgroup)
6836     (while (setq article (pop articles))
6837       (setq group (format "%s-%d" gnus-newsgroup-name article))
6838       (gnus-summary-remove-process-mark article)
6839       (when (gnus-summary-display-article article)
6840         (save-excursion
6841           (with-temp-buffer
6842             (insert-buffer-substring gnus-original-article-buffer)
6843             ;; Remove some headers that may lead nndoc to make
6844             ;; the wrong guess.
6845             (message-narrow-to-head)
6846             (goto-char (point-min))
6847             (delete-matching-lines "^\\(Path\\):\\|^From ")
6848             (widen)
6849             (if (setq egroup
6850                       (gnus-group-read-ephemeral-group
6851                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6852                                      (nndoc-article-type guess))
6853                        t nil t))
6854                 (progn
6855                   ;; Make all postings to this group go to the parent group.
6856                   (nconc (gnus-info-params (gnus-get-info egroup))
6857                          params)
6858                   (push egroup groups))
6859               ;; Couldn't select this doc group.
6860               (gnus-error 3 "Article couldn't be entered"))))))
6861     ;; Now we have selected all the documents.
6862     (cond
6863      ((not groups)
6864       (error "None of the articles could be interpreted as documents"))
6865      ((gnus-group-read-ephemeral-group
6866        (setq vgroup (format
6867                      "nnvirtual:%s-%s" gnus-newsgroup-name
6868                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6869        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6870        t
6871        (cons (current-buffer) 'summary)))
6872      (t
6873       (error "Couldn't select virtual nndoc group")))))
6874
6875 (defun gnus-summary-isearch-article (&optional regexp-p)
6876   "Do incremental search forward on the current article.
6877 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6878   (interactive "P")
6879   (gnus-summary-select-article)
6880   (gnus-configure-windows 'article)
6881   (gnus-eval-in-buffer-window gnus-article-buffer
6882     (save-restriction
6883       (widen)
6884       (isearch-forward regexp-p))))
6885
6886 (defun gnus-summary-search-article-forward (regexp &optional backward)
6887   "Search for an article containing REGEXP forward.
6888 If BACKWARD, search backward instead."
6889   (interactive
6890    (list (read-string
6891           (format "Search article %s (regexp%s): "
6892                   (if current-prefix-arg "backward" "forward")
6893                   (if gnus-last-search-regexp
6894                       (concat ", default " gnus-last-search-regexp)
6895                     "")))
6896          current-prefix-arg))
6897   (if (string-equal regexp "")
6898       (setq regexp (or gnus-last-search-regexp ""))
6899     (setq gnus-last-search-regexp regexp))
6900   (if (gnus-summary-search-article regexp backward)
6901       (gnus-summary-show-thread)
6902     (error "Search failed: \"%s\"" regexp)))
6903
6904 (defun gnus-summary-search-article-backward (regexp)
6905   "Search for an article containing REGEXP backward."
6906   (interactive
6907    (list (read-string
6908           (format "Search article backward (regexp%s): "
6909                   (if gnus-last-search-regexp
6910                       (concat ", default " gnus-last-search-regexp)
6911                     "")))))
6912   (gnus-summary-search-article-forward regexp 'backward))
6913
6914 (defun gnus-summary-search-article (regexp &optional backward)
6915   "Search for an article containing REGEXP.
6916 Optional argument BACKWARD means do search for backward.
6917 `gnus-select-article-hook' is not called during the search."
6918   ;; We have to require this here to make sure that the following
6919   ;; dynamic binding isn't shadowed by autoloading.
6920   (require 'gnus-async)
6921   (require 'gnus-art)
6922   (let ((gnus-select-article-hook nil)  ;Disable hook.
6923         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6924         (gnus-use-article-prefetch nil)
6925         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6926         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6927         (sum (current-buffer))
6928         (gnus-display-mime-function nil)
6929         (found nil)
6930         point)
6931     (gnus-save-hidden-threads
6932       (gnus-summary-select-article)
6933       (set-buffer gnus-article-buffer)
6934       (when backward
6935         (forward-line -1))
6936       (while (not found)
6937         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6938         (if (if backward
6939                 (re-search-backward regexp nil t)
6940               (re-search-forward regexp nil t))
6941             ;; We found the regexp.
6942             (progn
6943               (setq found 'found)
6944               (beginning-of-line)
6945               (set-window-start
6946                (get-buffer-window (current-buffer))
6947                (point))
6948               (forward-line 1)
6949               (set-buffer sum)
6950               (setq point (point)))
6951           ;; We didn't find it, so we go to the next article.
6952           (set-buffer sum)
6953           (setq found 'not)
6954           (while (eq found 'not)
6955             (if (not (if backward (gnus-summary-find-prev)
6956                        (gnus-summary-find-next)))
6957                 ;; No more articles.
6958                 (setq found t)
6959               ;; Select the next article and adjust point.
6960               (unless (gnus-summary-article-sparse-p
6961                        (gnus-summary-article-number))
6962                 (setq found nil)
6963                 (gnus-summary-select-article)
6964                 (set-buffer gnus-article-buffer)
6965                 (widen)
6966                 (goto-char (if backward (point-max) (point-min))))))))
6967       (gnus-message 7 ""))
6968     ;; Return whether we found the regexp.
6969     (when (eq found 'found)
6970       (goto-char point)
6971       (gnus-summary-show-thread)
6972       (gnus-summary-goto-subject gnus-current-article)
6973       (gnus-summary-position-point)
6974       t)))
6975
6976 (defun gnus-summary-find-matching (header regexp &optional backward unread
6977                                           not-case-fold)
6978   "Return a list of all articles that match REGEXP on HEADER.
6979 The search stars on the current article and goes forwards unless
6980 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6981 If UNREAD is non-nil, only unread articles will
6982 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6983 in the comparisons."
6984   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6985                 (gnus-data-find-list
6986                  (gnus-summary-article-number) (gnus-data-list backward))))
6987         (case-fold-search (not not-case-fold))
6988         articles d func)
6989     (if (consp header)
6990         (if (eq (car header) 'extra)
6991             (setq func
6992                   `(lambda (h)
6993                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6994                          "")))
6995           (error "%s is an invalid header" header))
6996       (unless (fboundp (intern (concat "mail-header-" header)))
6997         (error "%s is not a valid header" header))
6998       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6999     (while data
7000       (setq d (car data))
7001       (and (or (not unread)             ; We want all articles...
7002                (gnus-data-unread-p d))  ; Or just unreads.
7003            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7004            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7005            (push (gnus-data-number d) articles)) ; Success!
7006       (setq data (cdr data)))
7007     (nreverse articles)))
7008
7009 (defun gnus-summary-execute-command (header regexp command &optional backward)
7010   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7011 If HEADER is an empty string (or nil), the match is done on the entire
7012 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7013   (interactive
7014    (list (let ((completion-ignore-case t))
7015            (completing-read
7016             "Header name: "
7017             (mapcar (lambda (string) (list string))
7018                     '("Number" "Subject" "From" "Lines" "Date"
7019                       "Message-ID" "Xref" "References" "Body"))
7020             nil 'require-match))
7021          (read-string "Regexp: ")
7022          (read-key-sequence "Command: ")
7023          current-prefix-arg))
7024   (when (equal header "Body")
7025     (setq header ""))
7026   ;; Hidden thread subtrees must be searched as well.
7027   (gnus-summary-show-all-threads)
7028   ;; We don't want to change current point nor window configuration.
7029   (save-excursion
7030     (save-window-excursion
7031       (gnus-message 6 "Executing %s..." (key-description command))
7032       ;; We'd like to execute COMMAND interactively so as to give arguments.
7033       (gnus-execute header regexp
7034                     `(call-interactively ',(key-binding command))
7035                     backward)
7036       (gnus-message 6 "Executing %s...done" (key-description command)))))
7037
7038 (defun gnus-summary-beginning-of-article ()
7039   "Scroll the article back to the beginning."
7040   (interactive)
7041   (gnus-summary-select-article)
7042   (gnus-configure-windows 'article)
7043   (gnus-eval-in-buffer-window gnus-article-buffer
7044     (widen)
7045     (goto-char (point-min))
7046     (when gnus-page-broken
7047       (gnus-narrow-to-page))))
7048
7049 (defun gnus-summary-end-of-article ()
7050   "Scroll to the end of the article."
7051   (interactive)
7052   (gnus-summary-select-article)
7053   (gnus-configure-windows 'article)
7054   (gnus-eval-in-buffer-window gnus-article-buffer
7055     (widen)
7056     (goto-char (point-max))
7057     (recenter -3)
7058     (when gnus-page-broken
7059       (gnus-narrow-to-page))))
7060
7061 (defun gnus-summary-print-article (&optional filename n)
7062   "Generate and print a PostScript image of the N next (mail) articles.
7063
7064 If N is negative, print the N previous articles.  If N is nil and articles
7065 have been marked with the process mark, print these instead.
7066
7067 If the optional first argument FILENAME is nil, send the image to the
7068 printer.  If FILENAME is a string, save the PostScript image in a file with
7069 that name.  If FILENAME is a number, prompt the user for the name of the file
7070 to save in."
7071   (interactive (list (ps-print-preprint current-prefix-arg)
7072                      current-prefix-arg))
7073   (dolist (article (gnus-summary-work-articles n))
7074     (gnus-summary-select-article nil nil 'pseudo article)
7075     (gnus-eval-in-buffer-window gnus-article-buffer
7076       (let ((buffer (generate-new-buffer " *print*")))
7077         (unwind-protect
7078             (progn
7079               (copy-to-buffer buffer (point-min) (point-max))
7080               (set-buffer buffer)
7081               (gnus-article-delete-invisible-text)
7082               (let ((ps-left-header
7083                      (list
7084                       (concat "("
7085                               (mail-header-subject gnus-current-headers) ")")
7086                       (concat "("
7087                               (mail-header-from gnus-current-headers) ")")))
7088                     (ps-right-header
7089                      (list
7090                       "/pagenumberstring load"
7091                       (concat "("
7092                               (mail-header-date gnus-current-headers) ")"))))
7093                 (gnus-run-hooks 'gnus-ps-print-hook)
7094                 (save-excursion
7095                   (ps-print-buffer-with-faces filename))))
7096           (kill-buffer buffer))))))
7097
7098 (defun gnus-summary-show-article (&optional arg)
7099   "Force re-fetching of the current article.
7100 If ARG (the prefix) is non-nil, show the raw article without any
7101 article massaging functions being run."
7102   (interactive "P")
7103   (if (not arg)
7104       ;; Select the article the normal way.
7105       (gnus-summary-select-article nil 'force)
7106     ;; We have to require this here to make sure that the following
7107     ;; dynamic binding isn't shadowed by autoloading.
7108     (require 'gnus-async)
7109     (require 'gnus-art)
7110     ;; Bind the article treatment functions to nil.
7111     (let ((gnus-have-all-headers t)
7112           gnus-article-prepare-hook
7113           gnus-article-decode-hook
7114           gnus-display-mime-function
7115           gnus-break-pages
7116           gnus-visual)
7117       ;; Destroy any MIME parts.
7118       (when (gnus-buffer-live-p gnus-article-buffer)
7119         (save-excursion
7120           (set-buffer gnus-article-buffer)
7121           (mm-destroy-parts gnus-article-mime-handles)))
7122       (gnus-summary-select-article nil 'force)))
7123   (gnus-summary-goto-subject gnus-current-article)
7124   (gnus-summary-position-point))
7125
7126 (defun gnus-summary-verbose-headers (&optional arg)
7127   "Toggle permanent full header display.
7128 If ARG is a positive number, turn header display on.
7129 If ARG is a negative number, turn header display off."
7130   (interactive "P")
7131   (setq gnus-show-all-headers
7132         (cond ((or (not (numberp arg))
7133                    (zerop arg))
7134                (not gnus-show-all-headers))
7135               ((natnump arg)
7136                t)))
7137   (gnus-summary-show-article))
7138
7139 (defun gnus-summary-toggle-header (&optional arg)
7140   "Show the headers if they are hidden, or hide them if they are shown.
7141 If ARG is a positive number, show the entire header.
7142 If ARG is a negative number, hide the unwanted header lines."
7143   (interactive "P")
7144   (save-excursion
7145     (set-buffer gnus-article-buffer)
7146     (save-restriction
7147       (let* ((buffer-read-only nil)
7148              (inhibit-point-motion-hooks t)
7149              hidden e)
7150         (save-restriction 
7151           (article-narrow-to-head)
7152           (setq hidden (gnus-article-hidden-text-p 'headers)))
7153         (goto-char (point-min))
7154         (when (search-forward "\n\n" nil t)
7155           (delete-region (point-min) (1- (point))))
7156         (goto-char (point-min))
7157         (save-excursion
7158           (set-buffer gnus-original-article-buffer)
7159           (goto-char (point-min))
7160           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7161         (insert-buffer-substring gnus-original-article-buffer 1 e)
7162         (save-restriction
7163           (narrow-to-region (point-min) (point))
7164           (article-decode-encoded-words)
7165           (if (or hidden
7166                   (and (numberp arg) (< arg 0)))
7167               (let ((gnus-treat-hide-headers nil)
7168                     (gnus-treat-hide-boring-headers nil))
7169                 (gnus-treat-article 'head))
7170             (gnus-treat-article 'head)))))))
7171
7172 (defun gnus-summary-show-all-headers ()
7173   "Make all header lines visible."
7174   (interactive)
7175   (gnus-article-show-all-headers))
7176
7177 (defun gnus-summary-caesar-message (&optional arg)
7178   "Caesar rotate the current article by 13.
7179 The numerical prefix specifies how many places to rotate each letter
7180 forward."
7181   (interactive "P")
7182   (gnus-summary-select-article)
7183   (let ((mail-header-separator ""))
7184     (gnus-eval-in-buffer-window gnus-article-buffer
7185       (save-restriction
7186         (widen)
7187         (let ((start (window-start))
7188               buffer-read-only)
7189           (message-caesar-buffer-body arg)
7190           (set-window-start (get-buffer-window (current-buffer)) start))))))
7191
7192 (defun gnus-summary-stop-page-breaking ()
7193   "Stop page breaking in the current article."
7194   (interactive)
7195   (gnus-summary-select-article)
7196   (gnus-eval-in-buffer-window gnus-article-buffer
7197     (widen)
7198     (when (gnus-visual-p 'page-marker)
7199       (let ((buffer-read-only nil))
7200         (gnus-remove-text-with-property 'gnus-prev)
7201         (gnus-remove-text-with-property 'gnus-next))
7202       (setq gnus-page-broken nil))))
7203
7204 (defun gnus-summary-move-article (&optional n to-newsgroup
7205                                             select-method action)
7206   "Move the current article to a different newsgroup.
7207 If N is a positive number, move the N next articles.
7208 If N is a negative number, move the N previous articles.
7209 If N is nil and any articles have been marked with the process mark,
7210 move those articles instead.
7211 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7212 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7213 re-spool using this method.
7214
7215 For this function to work, both the current newsgroup and the
7216 newsgroup that you want to move to have to support the `request-move'
7217 and `request-accept' functions."
7218   (interactive "P")
7219   (unless action
7220     (setq action 'move))
7221   ;; Disable marking as read.
7222   (let (gnus-mark-article-hook)
7223     (save-window-excursion
7224       (gnus-summary-select-article)))
7225   ;; Check whether the source group supports the required functions.
7226   (cond ((and (eq action 'move)
7227               (not (gnus-check-backend-function
7228                     'request-move-article gnus-newsgroup-name)))
7229          (error "The current group does not support article moving"))
7230         ((and (eq action 'crosspost)
7231               (not (gnus-check-backend-function
7232                     'request-replace-article gnus-newsgroup-name)))
7233          (error "The current group does not support article editing")))
7234   (let ((articles (gnus-summary-work-articles n))
7235         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7236         (names '((move "Move" "Moving")
7237                  (copy "Copy" "Copying")
7238                  (crosspost "Crosspost" "Crossposting")))
7239         (copy-buf (save-excursion
7240                     (nnheader-set-temp-buffer " *copy article*")))
7241         art-group to-method new-xref article to-groups)
7242     (unless (assq action names)
7243       (error "Unknown action %s" action))
7244     ;; Read the newsgroup name.
7245     (when (and (not to-newsgroup)
7246                (not select-method))
7247       (setq to-newsgroup
7248             (gnus-read-move-group-name
7249              (cadr (assq action names))
7250              (symbol-value (intern (format "gnus-current-%s-group" action)))
7251              articles prefix))
7252       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7253     (setq to-method (or select-method
7254                         (gnus-group-name-to-method to-newsgroup)))
7255     ;; Check the method we are to move this article to...
7256     (unless (gnus-check-backend-function
7257              'request-accept-article (car to-method))
7258       (error "%s does not support article copying" (car to-method)))
7259     (unless (gnus-check-server to-method)
7260       (error "Can't open server %s" (car to-method)))
7261     (gnus-message 6 "%s to %s: %s..."
7262                   (caddr (assq action names))
7263                   (or (car select-method) to-newsgroup) articles)
7264     (while articles
7265       (setq article (pop articles))
7266       (setq
7267        art-group
7268        (cond
7269         ;; Move the article.
7270         ((eq action 'move)
7271          ;; Remove this article from future suppression.
7272          (gnus-dup-unsuppress-article article)
7273          (gnus-request-move-article
7274           article                       ; Article to move
7275           gnus-newsgroup-name           ; From newsgroup
7276           (nth 1 (gnus-find-method-for-group
7277                   gnus-newsgroup-name)) ; Server
7278           (list 'gnus-request-accept-article
7279                 to-newsgroup (list 'quote select-method)
7280                 (not articles) t)               ; Accept form
7281           (not articles)))              ; Only save nov last time
7282         ;; Copy the article.
7283         ((eq action 'copy)
7284          (save-excursion
7285            (set-buffer copy-buf)
7286            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7287              (gnus-request-accept-article
7288               to-newsgroup select-method (not articles) t))))
7289         ;; Crosspost the article.
7290         ((eq action 'crosspost)
7291          (let ((xref (message-tokenize-header
7292                       (mail-header-xref (gnus-summary-article-header article))
7293                       " ")))
7294            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7295                                   ":" article))
7296            (unless xref
7297              (setq xref (list (system-name))))
7298            (setq new-xref
7299                  (concat
7300                   (mapconcat 'identity
7301                              (delete "Xref:" (delete new-xref xref))
7302                              " ")
7303                   " " new-xref))
7304            (save-excursion
7305              (set-buffer copy-buf)
7306              ;; First put the article in the destination group.
7307              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7308              (when (consp (setq art-group
7309                                 (gnus-request-accept-article
7310                                  to-newsgroup select-method (not articles))))
7311                (setq new-xref (concat new-xref " " (car art-group)
7312                                       ":" (cdr art-group)))
7313                ;; Now we have the new Xrefs header, so we insert
7314                ;; it and replace the new article.
7315                (nnheader-replace-header "Xref" new-xref)
7316                (gnus-request-replace-article
7317                 (cdr art-group) to-newsgroup (current-buffer))
7318                art-group))))))
7319       (cond
7320        ((not art-group)
7321        (gnus-message 1 "Couldn't %s article %s: %s"
7322                      (cadr (assq action names)) article
7323                      (nnheader-get-report (car to-method))))
7324        ((and (eq art-group 'junk)
7325              (eq action 'move))
7326         (gnus-summary-mark-article article gnus-canceled-mark)
7327         (gnus-message 4 "Deleted article %s" article))
7328        (t
7329         (let* ((pto-group (gnus-group-prefixed-name
7330                            (car art-group) to-method))
7331                (entry
7332                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7333                (info (nth 2 entry))
7334                (to-group (gnus-info-group info))
7335                to-marks)
7336           ;; Update the group that has been moved to.
7337           (when (and info
7338                      (memq action '(move copy)))
7339             (unless (member to-group to-groups)
7340               (push to-group to-groups))
7341
7342             (unless (memq article gnus-newsgroup-unreads)
7343               (push 'read to-marks)
7344               (gnus-info-set-read
7345                info (gnus-add-to-range (gnus-info-read info)
7346                                        (list (cdr art-group)))))
7347
7348             ;; Copy any marks over to the new group.
7349             (let ((marks gnus-article-mark-lists)
7350                   (to-article (cdr art-group)))
7351
7352               ;; See whether the article is to be put in the cache.
7353               (when gnus-use-cache
7354                 (gnus-cache-possibly-enter-article
7355                  to-group to-article
7356                  (memq article gnus-newsgroup-marked)
7357                  (memq article gnus-newsgroup-dormant)
7358                  (memq article gnus-newsgroup-unreads)))
7359
7360               (when (and (equal to-group gnus-newsgroup-name)
7361                          (not (memq article gnus-newsgroup-unreads)))
7362                 ;; Mark this article as read in this group.
7363                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7364                 (setcdr (gnus-active to-group) to-article)
7365                 (setcdr gnus-newsgroup-active to-article))
7366
7367               (while marks
7368                 (when (memq article (symbol-value
7369                                      (intern (format "gnus-newsgroup-%s"
7370                                                      (caar marks)))))
7371                   (push (cdar marks) to-marks)
7372                   ;; If the other group is the same as this group,
7373                   ;; then we have to add the mark to the list.
7374                   (when (equal to-group gnus-newsgroup-name)
7375                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7376                          (cons to-article
7377                                (symbol-value
7378                                 (intern (format "gnus-newsgroup-%s"
7379                                                 (caar marks)))))))
7380                   ;; Copy the marks to other group.
7381                   (gnus-add-marked-articles
7382                    to-group (cdar marks) (list to-article) info))
7383                 (setq marks (cdr marks)))
7384
7385               (gnus-request-set-mark to-group (list (list (list to-article)
7386                                                           'set
7387                                                           to-marks)))
7388
7389               (gnus-dribble-enter
7390                (concat "(gnus-group-set-info '"
7391                        (gnus-prin1-to-string (gnus-get-info to-group))
7392                        ")"))))
7393
7394           ;; Update the Xref header in this article to point to
7395           ;; the new crossposted article we have just created.
7396           (when (eq action 'crosspost)
7397             (save-excursion
7398               (set-buffer copy-buf)
7399               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7400               (nnheader-replace-header "Xref" new-xref)
7401               (gnus-request-replace-article
7402                article gnus-newsgroup-name (current-buffer)))))
7403
7404         ;;;!!!Why is this necessary?
7405         (set-buffer gnus-summary-buffer)
7406
7407         (gnus-summary-goto-subject article)
7408         (when (eq action 'move)
7409           (gnus-summary-mark-article article gnus-canceled-mark))))
7410       (gnus-summary-remove-process-mark article))
7411     ;; Re-activate all groups that have been moved to.
7412     (while to-groups
7413       (save-excursion
7414         (set-buffer gnus-group-buffer)
7415         (when (gnus-group-goto-group (car to-groups) t)
7416           (gnus-group-get-new-news-this-group 1 t))
7417         (pop to-groups)))
7418
7419     (gnus-kill-buffer copy-buf)
7420     (gnus-summary-position-point)
7421     (gnus-set-mode-line 'summary)))
7422
7423 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7424   "Move the current article to a different newsgroup.
7425 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7426 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7427 re-spool using this method."
7428   (interactive "P")
7429   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7430
7431 (defun gnus-summary-crosspost-article (&optional n)
7432   "Crosspost the current article to some other group."
7433   (interactive "P")
7434   (gnus-summary-move-article n nil nil 'crosspost))
7435
7436 (defcustom gnus-summary-respool-default-method nil
7437   "Default method for respooling an article.
7438 If nil, use to the current newsgroup method."
7439   :type '(choice (gnus-select-method :value (nnml ""))
7440                  (const nil))
7441   :group 'gnus-summary-mail)
7442
7443 (defun gnus-summary-respool-article (&optional n method)
7444   "Respool the current article.
7445 The article will be squeezed through the mail spooling process again,
7446 which means that it will be put in some mail newsgroup or other
7447 depending on `nnmail-split-methods'.
7448 If N is a positive number, respool the N next articles.
7449 If N is a negative number, respool the N previous articles.
7450 If N is nil and any articles have been marked with the process mark,
7451 respool those articles instead.
7452
7453 Respooling can be done both from mail groups and \"real\" newsgroups.
7454 In the former case, the articles in question will be moved from the
7455 current group into whatever groups they are destined to.  In the
7456 latter case, they will be copied into the relevant groups."
7457   (interactive
7458    (list current-prefix-arg
7459          (let* ((methods (gnus-methods-using 'respool))
7460                 (methname
7461                  (symbol-name (or gnus-summary-respool-default-method
7462                                   (car (gnus-find-method-for-group
7463                                         gnus-newsgroup-name)))))
7464                 (method
7465                  (gnus-completing-read
7466                   methname "What backend do you want to use when respooling?"
7467                   methods nil t nil 'gnus-mail-method-history))
7468                 ms)
7469            (cond
7470             ((zerop (length (setq ms (gnus-servers-using-backend
7471                                       (intern method)))))
7472              (list (intern method) ""))
7473             ((= 1 (length ms))
7474              (car ms))
7475             (t
7476              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7477                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7478                            ms-alist))))))))
7479   (unless method
7480     (error "No method given for respooling"))
7481   (if (assoc (symbol-name
7482               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7483              (gnus-methods-using 'respool))
7484       (gnus-summary-move-article n nil method)
7485     (gnus-summary-copy-article n nil method)))
7486
7487 (defun gnus-summary-import-article (file)
7488   "Import an arbitrary file into a mail newsgroup."
7489   (interactive "fImport file: ")
7490   (let ((group gnus-newsgroup-name)
7491         (now (current-time))
7492         atts lines)
7493     (unless (gnus-check-backend-function 'request-accept-article group)
7494       (error "%s does not support article importing" group))
7495     (or (file-readable-p file)
7496         (not (file-regular-p file))
7497         (error "Can't read %s" file))
7498     (save-excursion
7499       (set-buffer (gnus-get-buffer-create " *import file*"))
7500       (erase-buffer)
7501       (insert-file-contents file)
7502       (goto-char (point-min))
7503       (unless (nnheader-article-p)
7504         ;; This doesn't look like an article, so we fudge some headers.
7505         (setq atts (file-attributes file)
7506               lines (count-lines (point-min) (point-max)))
7507         (insert "From: " (read-string "From: ") "\n"
7508                 "Subject: " (read-string "Subject: ") "\n"
7509                 "Date: " (message-make-date (nth 5 atts))
7510                 "\n"
7511                 "Message-ID: " (message-make-message-id) "\n"
7512                 "Lines: " (int-to-string lines) "\n"
7513                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7514       (gnus-request-accept-article group nil t)
7515       (kill-buffer (current-buffer)))))
7516
7517 (defun gnus-summary-article-posted-p ()
7518   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7519 This will be the case if the article has both been mailed and posted."
7520   (interactive)
7521   (let ((id (mail-header-references (gnus-summary-article-header)))
7522         (gnus-override-method
7523          (or gnus-refer-article-method gnus-select-method)))
7524     (if (gnus-request-head id "")
7525         (gnus-message 2 "The current message was found on %s"
7526                       gnus-override-method)
7527       (gnus-message 2 "The current message couldn't be found on %s"
7528                     gnus-override-method)
7529       nil)))
7530
7531 (defun gnus-summary-expire-articles (&optional now)
7532   "Expire all articles that are marked as expirable in the current group."
7533   (interactive)
7534   (when (gnus-check-backend-function
7535          'request-expire-articles gnus-newsgroup-name)
7536     ;; This backend supports expiry.
7537     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7538            (expirable (if total
7539                           (progn
7540                             ;; We need to update the info for
7541                             ;; this group for `gnus-list-of-read-articles'
7542                             ;; to give us the right answer.
7543                             (gnus-run-hooks 'gnus-exit-group-hook)
7544                             (gnus-summary-update-info)
7545                             (gnus-list-of-read-articles gnus-newsgroup-name))
7546                         (setq gnus-newsgroup-expirable
7547                               (sort gnus-newsgroup-expirable '<))))
7548            (expiry-wait (if now 'immediate
7549                           (gnus-group-find-parameter
7550                            gnus-newsgroup-name 'expiry-wait)))
7551            es)
7552       (when expirable
7553         ;; There are expirable articles in this group, so we run them
7554         ;; through the expiry process.
7555         (gnus-message 6 "Expiring articles...")
7556         (unless (gnus-check-group gnus-newsgroup-name)
7557           (error "Can't open server for %s" gnus-newsgroup-name))
7558         ;; The list of articles that weren't expired is returned.
7559         (save-excursion
7560           (if expiry-wait
7561               (let ((nnmail-expiry-wait-function nil)
7562                     (nnmail-expiry-wait expiry-wait))
7563                 (setq es (gnus-request-expire-articles
7564                           expirable gnus-newsgroup-name)))
7565             (setq es (gnus-request-expire-articles
7566                       expirable gnus-newsgroup-name))))
7567         (unless total
7568           (setq gnus-newsgroup-expirable es))
7569         ;; We go through the old list of expirable, and mark all
7570         ;; really expired articles as nonexistent.
7571         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7572           (let ((gnus-use-cache nil))
7573             (while expirable
7574               (unless (memq (car expirable) es)
7575                 (when (gnus-data-find (car expirable))
7576                   (gnus-summary-mark-article
7577                    (car expirable) gnus-canceled-mark)))
7578               (setq expirable (cdr expirable)))))
7579         (gnus-message 6 "Expiring articles...done")))))
7580
7581 (defun gnus-summary-expire-articles-now ()
7582   "Expunge all expirable articles in the current group.
7583 This means that *all* articles that are marked as expirable will be
7584 deleted forever, right now."
7585   (interactive)
7586   (or gnus-expert-user
7587       (gnus-yes-or-no-p
7588        "Are you really, really, really sure you want to delete all these messages? ")
7589       (error "Phew!"))
7590   (gnus-summary-expire-articles t))
7591
7592 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7593 (defun gnus-summary-delete-article (&optional n)
7594   "Delete the N next (mail) articles.
7595 This command actually deletes articles.  This is not a marking
7596 command.  The article will disappear forever from your life, never to
7597 return.
7598 If N is negative, delete backwards.
7599 If N is nil and articles have been marked with the process mark,
7600 delete these instead."
7601   (interactive "P")
7602   (unless (gnus-check-backend-function 'request-expire-articles
7603                                        gnus-newsgroup-name)
7604     (error "The current newsgroup does not support article deletion"))
7605   ;; Compute the list of articles to delete.
7606   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7607         not-deleted)
7608     (if (and gnus-novice-user
7609              (not (gnus-yes-or-no-p
7610                    (format "Do you really want to delete %s forever? "
7611                            (if (> (length articles) 1)
7612                                (format "these %s articles" (length articles))
7613                              "this article")))))
7614         ()
7615       ;; Delete the articles.
7616       (setq not-deleted (gnus-request-expire-articles
7617                          articles gnus-newsgroup-name 'force))
7618       (while articles
7619         (gnus-summary-remove-process-mark (car articles))
7620         ;; The backend might not have been able to delete the article
7621         ;; after all.
7622         (unless (memq (car articles) not-deleted)
7623           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7624         (setq articles (cdr articles)))
7625       (when not-deleted
7626         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7627     (gnus-summary-position-point)
7628     (gnus-set-mode-line 'summary)
7629     not-deleted))
7630
7631 (defun gnus-summary-edit-article (&optional force)
7632   "Edit the current article.
7633 This will have permanent effect only in mail groups.
7634 If FORCE is non-nil, allow editing of articles even in read-only
7635 groups."
7636   (interactive "P")
7637   (save-excursion
7638     (set-buffer gnus-summary-buffer)
7639     (let ((mail-parse-charset gnus-newsgroup-charset)
7640           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7641       (gnus-set-global-variables)
7642       (when (and (not force)
7643                  (gnus-group-read-only-p))
7644         (error "The current newsgroup does not support article editing"))
7645       (gnus-summary-show-article t)
7646       (gnus-article-edit-article
7647        'mime-to-mml
7648        `(lambda (no-highlight)
7649           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7650                 (mail-parse-ignored-charsets 
7651                  ',gnus-newsgroup-ignored-charsets))
7652             (mml-to-mime)
7653             (gnus-summary-edit-article-done
7654              ,(or (mail-header-references gnus-current-headers) "")
7655              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7656
7657 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7658
7659 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7660                                                  no-highlight)
7661   "Make edits to the current article permanent."
7662   (interactive)
7663   ;; Replace the article.
7664   (let ((buf (current-buffer)))
7665     (with-temp-buffer
7666       (insert-buffer-substring buf)
7667       (if (and (not read-only)
7668                (not (gnus-request-replace-article
7669                      (cdr gnus-article-current) (car gnus-article-current)
7670                      (current-buffer) t)))
7671           (error "Couldn't replace article")
7672         ;; Update the summary buffer.
7673         (if (and references
7674                  (equal (message-tokenize-header references " ")
7675                         (message-tokenize-header
7676                          (or (message-fetch-field "references") "") " ")))
7677             ;; We only have to update this line.
7678             (save-excursion
7679               (save-restriction
7680                 (message-narrow-to-head)
7681                 (let ((head (buffer-string))
7682                       header)
7683                   (with-temp-buffer
7684                     (insert (format "211 %d Article retrieved.\n"
7685                                     (cdr gnus-article-current)))
7686                     (insert head)
7687                     (insert ".\n")
7688                     (let ((nntp-server-buffer (current-buffer)))
7689                       (setq header (car (gnus-get-newsgroup-headers
7690                                          (save-excursion
7691                                            (set-buffer gnus-summary-buffer)
7692                                            gnus-newsgroup-dependencies)
7693                                          t))))
7694                     (save-excursion
7695                       (set-buffer gnus-summary-buffer)
7696                       (gnus-data-set-header
7697                        (gnus-data-find (cdr gnus-article-current))
7698                        header)
7699                       (gnus-summary-update-article-line
7700                        (cdr gnus-article-current) header))))))
7701           ;; Update threads.
7702           (set-buffer (or buffer gnus-summary-buffer))
7703           (gnus-summary-update-article (cdr gnus-article-current)))
7704         ;; Prettify the article buffer again.
7705         (unless no-highlight
7706           (save-excursion
7707             (set-buffer gnus-article-buffer)
7708             ;;;!!! Fix this -- article should be rehighlighted.
7709             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7710             (set-buffer gnus-original-article-buffer)
7711             (gnus-request-article
7712              (cdr gnus-article-current)
7713              (car gnus-article-current) (current-buffer))))
7714         ;; Prettify the summary buffer line.
7715         (when (gnus-visual-p 'summary-highlight 'highlight)
7716           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7717
7718 (defun gnus-summary-edit-wash (key)
7719   "Perform editing command KEY in the article buffer."
7720   (interactive
7721    (list
7722     (progn
7723       (message "%s" (concat (this-command-keys) "- "))
7724       (read-char))))
7725   (message "")
7726   (gnus-summary-edit-article)
7727   (execute-kbd-macro (concat (this-command-keys) key))
7728   (gnus-article-edit-done))
7729
7730 ;;; Respooling
7731
7732 (defun gnus-summary-respool-query (&optional silent trace)
7733   "Query where the respool algorithm would put this article."
7734   (interactive)
7735   (let (gnus-mark-article-hook)
7736     (gnus-summary-select-article)
7737     (save-excursion
7738       (set-buffer gnus-original-article-buffer)
7739       (save-restriction
7740         (message-narrow-to-head)
7741         (let ((groups (nnmail-article-group 'identity trace)))
7742           (unless silent
7743             (if groups
7744                 (message "This message would go to %s"
7745                          (mapconcat 'car groups ", "))
7746               (message "This message would go to no groups"))
7747             groups))))))
7748
7749 (defun gnus-summary-respool-trace ()
7750   "Trace where the respool algorithm would put this article.
7751 Display a buffer showing all fancy splitting patterns which matched."
7752   (interactive)
7753   (gnus-summary-respool-query nil t))
7754
7755 ;; Summary marking commands.
7756
7757 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7758   "Mark articles which has the same subject as read, and then select the next.
7759 If UNMARK is positive, remove any kind of mark.
7760 If UNMARK is negative, tick articles."
7761   (interactive "P")
7762   (when unmark
7763     (setq unmark (prefix-numeric-value unmark)))
7764   (let ((count
7765          (gnus-summary-mark-same-subject
7766           (gnus-summary-article-subject) unmark)))
7767     ;; Select next unread article.  If auto-select-same mode, should
7768     ;; select the first unread article.
7769     (gnus-summary-next-article t (and gnus-auto-select-same
7770                                       (gnus-summary-article-subject)))
7771     (gnus-message 7 "%d article%s marked as %s"
7772                   count (if (= count 1) " is" "s are")
7773                   (if unmark "unread" "read"))))
7774
7775 (defun gnus-summary-kill-same-subject (&optional unmark)
7776   "Mark articles which has the same subject as read.
7777 If UNMARK is positive, remove any kind of mark.
7778 If UNMARK is negative, tick articles."
7779   (interactive "P")
7780   (when unmark
7781     (setq unmark (prefix-numeric-value unmark)))
7782   (let ((count
7783          (gnus-summary-mark-same-subject
7784           (gnus-summary-article-subject) unmark)))
7785     ;; If marked as read, go to next unread subject.
7786     (when (null unmark)
7787       ;; Go to next unread subject.
7788       (gnus-summary-next-subject 1 t))
7789     (gnus-message 7 "%d articles are marked as %s"
7790                   count (if unmark "unread" "read"))))
7791
7792 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7793   "Mark articles with same SUBJECT as read, and return marked number.
7794 If optional argument UNMARK is positive, remove any kinds of marks.
7795 If optional argument UNMARK is negative, mark articles as unread instead."
7796   (let ((count 1))
7797     (save-excursion
7798       (cond
7799        ((null unmark)                   ; Mark as read.
7800         (while (and
7801                 (progn
7802                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7803                   (gnus-summary-show-thread) t)
7804                 (gnus-summary-find-subject subject))
7805           (setq count (1+ count))))
7806        ((> unmark 0)                    ; Tick.
7807         (while (and
7808                 (progn
7809                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7810                   (gnus-summary-show-thread) t)
7811                 (gnus-summary-find-subject subject))
7812           (setq count (1+ count))))
7813        (t                               ; Mark as unread.
7814         (while (and
7815                 (progn
7816                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7817                   (gnus-summary-show-thread) t)
7818                 (gnus-summary-find-subject subject))
7819           (setq count (1+ count)))))
7820       (gnus-set-mode-line 'summary)
7821       ;; Return the number of marked articles.
7822       count)))
7823
7824 (defun gnus-summary-mark-as-processable (n &optional unmark)
7825   "Set the process mark on the next N articles.
7826 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7827 the process mark instead.  The difference between N and the actual
7828 number of articles marked is returned."
7829   (interactive "p")
7830   (let ((backward (< n 0))
7831         (n (abs n)))
7832     (while (and
7833             (> n 0)
7834             (if unmark
7835                 (gnus-summary-remove-process-mark
7836                  (gnus-summary-article-number))
7837               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7838             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7839       (setq n (1- n)))
7840     (when (/= 0 n)
7841       (gnus-message 7 "No more articles"))
7842     (gnus-summary-recenter)
7843     (gnus-summary-position-point)
7844     n))
7845
7846 (defun gnus-summary-unmark-as-processable (n)
7847   "Remove the process mark from the next N articles.
7848 If N is negative, unmark backward instead.  The difference between N and
7849 the actual number of articles unmarked is returned."
7850   (interactive "p")
7851   (gnus-summary-mark-as-processable n t))
7852
7853 (defun gnus-summary-unmark-all-processable ()
7854   "Remove the process mark from all articles."
7855   (interactive)
7856   (save-excursion
7857     (while gnus-newsgroup-processable
7858       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7859   (gnus-summary-position-point))
7860
7861 (defun gnus-summary-mark-as-expirable (n)
7862   "Mark N articles forward as expirable.
7863 If N is negative, mark backward instead.  The difference between N and
7864 the actual number of articles marked is returned."
7865   (interactive "p")
7866   (gnus-summary-mark-forward n gnus-expirable-mark))
7867
7868 (defun gnus-summary-mark-article-as-replied (article)
7869   "Mark ARTICLE replied and update the summary line."
7870   (push article gnus-newsgroup-replied)
7871   (let ((buffer-read-only nil))
7872     (when (gnus-summary-goto-subject article nil t)
7873       (gnus-summary-update-secondary-mark article))))
7874
7875 (defun gnus-summary-set-bookmark (article)
7876   "Set a bookmark in current article."
7877   (interactive (list (gnus-summary-article-number)))
7878   (when (or (not (get-buffer gnus-article-buffer))
7879             (not gnus-current-article)
7880             (not gnus-article-current)
7881             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7882     (error "No current article selected"))
7883   ;; Remove old bookmark, if one exists.
7884   (let ((old (assq article gnus-newsgroup-bookmarks)))
7885     (when old
7886       (setq gnus-newsgroup-bookmarks
7887             (delq old gnus-newsgroup-bookmarks))))
7888   ;; Set the new bookmark, which is on the form
7889   ;; (article-number . line-number-in-body).
7890   (push
7891    (cons article
7892          (save-excursion
7893            (set-buffer gnus-article-buffer)
7894            (count-lines
7895             (min (point)
7896                  (save-excursion
7897                    (goto-char (point-min))
7898                    (search-forward "\n\n" nil t)
7899                    (point)))
7900             (point))))
7901    gnus-newsgroup-bookmarks)
7902   (gnus-message 6 "A bookmark has been added to the current article."))
7903
7904 (defun gnus-summary-remove-bookmark (article)
7905   "Remove the bookmark from the current article."
7906   (interactive (list (gnus-summary-article-number)))
7907   ;; Remove old bookmark, if one exists.
7908   (let ((old (assq article gnus-newsgroup-bookmarks)))
7909     (if old
7910         (progn
7911           (setq gnus-newsgroup-bookmarks
7912                 (delq old gnus-newsgroup-bookmarks))
7913           (gnus-message 6 "Removed bookmark."))
7914       (gnus-message 6 "No bookmark in current article."))))
7915
7916 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7917 (defun gnus-summary-mark-as-dormant (n)
7918   "Mark N articles forward as dormant.
7919 If N is negative, mark backward instead.  The difference between N and
7920 the actual number of articles marked is returned."
7921   (interactive "p")
7922   (gnus-summary-mark-forward n gnus-dormant-mark))
7923
7924 (defun gnus-summary-set-process-mark (article)
7925   "Set the process mark on ARTICLE and update the summary line."
7926   (setq gnus-newsgroup-processable
7927         (cons article
7928               (delq article gnus-newsgroup-processable)))
7929   (when (gnus-summary-goto-subject article)
7930     (gnus-summary-show-thread)
7931     (gnus-summary-goto-subject article)
7932     (gnus-summary-update-secondary-mark article)))
7933
7934 (defun gnus-summary-remove-process-mark (article)
7935   "Remove the process mark from ARTICLE and update the summary line."
7936   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7937   (when (gnus-summary-goto-subject article)
7938     (gnus-summary-show-thread)
7939     (gnus-summary-goto-subject article)
7940     (gnus-summary-update-secondary-mark article)))
7941
7942 (defun gnus-summary-set-saved-mark (article)
7943   "Set the process mark on ARTICLE and update the summary line."
7944   (push article gnus-newsgroup-saved)
7945   (when (gnus-summary-goto-subject article)
7946     (gnus-summary-update-secondary-mark article)))
7947
7948 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7949   "Mark N articles as read forwards.
7950 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7951 The difference between N and the actual number of articles marked is
7952 returned."
7953   (interactive "p")
7954   (gnus-summary-show-thread)
7955   (let ((backward (< n 0))
7956         (gnus-summary-goto-unread
7957          (and gnus-summary-goto-unread
7958               (not (eq gnus-summary-goto-unread 'never))
7959               (not (memq mark (list gnus-unread-mark
7960                                     gnus-ticked-mark gnus-dormant-mark)))))
7961         (n (abs n))
7962         (mark (or mark gnus-del-mark)))
7963     (while (and (> n 0)
7964                 (gnus-summary-mark-article nil mark no-expire)
7965                 (zerop (gnus-summary-next-subject
7966                         (if backward -1 1)
7967                         (and gnus-summary-goto-unread
7968                              (not (eq gnus-summary-goto-unread 'never)))
7969                         t)))
7970       (setq n (1- n)))
7971     (when (/= 0 n)
7972       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7973     (gnus-summary-recenter)
7974     (gnus-summary-position-point)
7975     (gnus-set-mode-line 'summary)
7976     n))
7977
7978 (defun gnus-summary-mark-article-as-read (mark)
7979   "Mark the current article quickly as read with MARK."
7980   (let ((article (gnus-summary-article-number)))
7981     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7982     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7983     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7984     (push (cons article mark) gnus-newsgroup-reads)
7985     ;; Possibly remove from cache, if that is used.
7986     (when gnus-use-cache
7987       (gnus-cache-enter-remove-article article))
7988     ;; Allow the backend to change the mark.
7989     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7990     ;; Check for auto-expiry.
7991     (when (and gnus-newsgroup-auto-expire
7992                (memq mark gnus-auto-expirable-marks))
7993       (setq mark gnus-expirable-mark)
7994       ;; Let the backend know about the mark change.
7995       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7996       (push article gnus-newsgroup-expirable))
7997     ;; Set the mark in the buffer.
7998     (gnus-summary-update-mark mark 'unread)
7999     t))
8000
8001 (defun gnus-summary-mark-article-as-unread (mark)
8002   "Mark the current article quickly as unread with MARK."
8003   (let* ((article (gnus-summary-article-number))
8004          (old-mark (gnus-summary-article-mark article)))
8005     ;; Allow the backend to change the mark.
8006     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8007     (if (eq mark old-mark)
8008         t
8009       (if (<= article 0)
8010           (progn
8011             (gnus-error 1 "Can't mark negative article numbers")
8012             nil)
8013         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8014         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8015         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8016         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8017         (cond ((= mark gnus-ticked-mark)
8018                (push article gnus-newsgroup-marked))
8019               ((= mark gnus-dormant-mark)
8020                (push article gnus-newsgroup-dormant))
8021               (t
8022                (push article gnus-newsgroup-unreads)))
8023         (gnus-pull article gnus-newsgroup-reads)
8024
8025         ;; See whether the article is to be put in the cache.
8026         (and gnus-use-cache
8027              (vectorp (gnus-summary-article-header article))
8028              (save-excursion
8029                (gnus-cache-possibly-enter-article
8030                 gnus-newsgroup-name article
8031                 (= mark gnus-ticked-mark)
8032                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8033
8034         ;; Fix the mark.
8035         (gnus-summary-update-mark mark 'unread)
8036         t))))
8037
8038 (defun gnus-summary-mark-article (&optional article mark no-expire)
8039   "Mark ARTICLE with MARK.  MARK can be any character.
8040 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8041 `??' (dormant) and `?E' (expirable).
8042 If MARK is nil, then the default character `?r' is used.
8043 If ARTICLE is nil, then the article on the current line will be
8044 marked."
8045   ;; The mark might be a string.
8046   (when (stringp mark)
8047     (setq mark (aref mark 0)))
8048   ;; If no mark is given, then we check auto-expiring.
8049   (when (null mark)
8050     (setq mark gnus-del-mark))
8051   (when (and (not no-expire)
8052              gnus-newsgroup-auto-expire
8053              (memq mark gnus-auto-expirable-marks))
8054     (setq mark gnus-expirable-mark))
8055   (let ((article (or article (gnus-summary-article-number)))
8056         (old-mark (gnus-summary-article-mark article)))
8057     ;; Allow the backend to change the mark.
8058     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8059     (if (eq mark old-mark)
8060         t
8061       (unless article
8062         (error "No article on current line"))
8063       (if (not (if (or (= mark gnus-unread-mark)
8064                        (= mark gnus-ticked-mark)
8065                        (= mark gnus-dormant-mark))
8066                    (gnus-mark-article-as-unread article mark)
8067                  (gnus-mark-article-as-read article mark)))
8068           t
8069         ;; See whether the article is to be put in the cache.
8070         (and gnus-use-cache
8071              (not (= mark gnus-canceled-mark))
8072              (vectorp (gnus-summary-article-header article))
8073              (save-excursion
8074                (gnus-cache-possibly-enter-article
8075                 gnus-newsgroup-name article
8076                 (= mark gnus-ticked-mark)
8077                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8078
8079         (when (gnus-summary-goto-subject article nil t)
8080           (let ((buffer-read-only nil))
8081             (gnus-summary-show-thread)
8082             ;; Fix the mark.
8083             (gnus-summary-update-mark mark 'unread)
8084             t))))))
8085
8086 (defun gnus-summary-update-secondary-mark (article)
8087   "Update the secondary (read, process, cache) mark."
8088   (gnus-summary-update-mark
8089    (cond ((memq article gnus-newsgroup-processable)
8090           gnus-process-mark)
8091          ((memq article gnus-newsgroup-cached)
8092           gnus-cached-mark)
8093          ((memq article gnus-newsgroup-replied)
8094           gnus-replied-mark)
8095          ((memq article gnus-newsgroup-saved)
8096           gnus-saved-mark)
8097          (t gnus-unread-mark))
8098    'replied)
8099   (when (gnus-visual-p 'summary-highlight 'highlight)
8100     (gnus-run-hooks 'gnus-summary-update-hook))
8101   t)
8102
8103 (defun gnus-summary-update-mark (mark type)
8104   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8105         (buffer-read-only nil))
8106     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8107     (when forward
8108       (when (looking-at "\r")
8109         (incf forward))
8110       (when (<= (+ forward (point)) (point-max))
8111         ;; Go to the right position on the line.
8112         (goto-char (+ forward (point)))
8113         ;; Replace the old mark with the new mark.
8114         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8115         ;; Optionally update the marks by some user rule.
8116         (when (eq type 'unread)
8117           (gnus-data-set-mark
8118            (gnus-data-find (gnus-summary-article-number)) mark)
8119           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8120
8121 (defun gnus-mark-article-as-read (article &optional mark)
8122   "Enter ARTICLE in the pertinent lists and remove it from others."
8123   ;; Make the article expirable.
8124   (let ((mark (or mark gnus-del-mark)))
8125     (if (= mark gnus-expirable-mark)
8126         (push article gnus-newsgroup-expirable)
8127       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8128     ;; Remove from unread and marked lists.
8129     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8130     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8131     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8132     (push (cons article mark) gnus-newsgroup-reads)
8133     ;; Possibly remove from cache, if that is used.
8134     (when gnus-use-cache
8135       (gnus-cache-enter-remove-article article))
8136     t))
8137
8138 (defun gnus-mark-article-as-unread (article &optional mark)
8139   "Enter ARTICLE in the pertinent lists and remove it from others."
8140   (let ((mark (or mark gnus-ticked-mark)))
8141     (if (<= article 0)
8142         (progn
8143           (gnus-error 1 "Can't mark negative article numbers")
8144           nil)
8145       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8146             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8147             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8148             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8149
8150       ;; Unsuppress duplicates?
8151       (when gnus-suppress-duplicates
8152         (gnus-dup-unsuppress-article article))
8153
8154       (cond ((= mark gnus-ticked-mark)
8155              (push article gnus-newsgroup-marked))
8156             ((= mark gnus-dormant-mark)
8157              (push article gnus-newsgroup-dormant))
8158             (t
8159              (push article gnus-newsgroup-unreads)))
8160       (gnus-pull article gnus-newsgroup-reads)
8161       t)))
8162
8163 (defalias 'gnus-summary-mark-as-unread-forward
8164   'gnus-summary-tick-article-forward)
8165 (make-obsolete 'gnus-summary-mark-as-unread-forward
8166                'gnus-summary-tick-article-forward)
8167 (defun gnus-summary-tick-article-forward (n)
8168   "Tick N articles forwards.
8169 If N is negative, tick backwards instead.
8170 The difference between N and the number of articles ticked is returned."
8171   (interactive "p")
8172   (gnus-summary-mark-forward n gnus-ticked-mark))
8173
8174 (defalias 'gnus-summary-mark-as-unread-backward
8175   'gnus-summary-tick-article-backward)
8176 (make-obsolete 'gnus-summary-mark-as-unread-backward
8177                'gnus-summary-tick-article-backward)
8178 (defun gnus-summary-tick-article-backward (n)
8179   "Tick N articles backwards.
8180 The difference between N and the number of articles ticked is returned."
8181   (interactive "p")
8182   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8183
8184 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8185 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8186 (defun gnus-summary-tick-article (&optional article clear-mark)
8187   "Mark current article as unread.
8188 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8189 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8190   (interactive)
8191   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8192                                        gnus-ticked-mark)))
8193
8194 (defun gnus-summary-mark-as-read-forward (n)
8195   "Mark N articles as read forwards.
8196 If N is negative, mark backwards instead.
8197 The difference between N and the actual number of articles marked is
8198 returned."
8199   (interactive "p")
8200   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8201
8202 (defun gnus-summary-mark-as-read-backward (n)
8203   "Mark the N articles as read backwards.
8204 The difference between N and the actual number of articles marked is
8205 returned."
8206   (interactive "p")
8207   (gnus-summary-mark-forward
8208    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8209
8210 (defun gnus-summary-mark-as-read (&optional article mark)
8211   "Mark current article as read.
8212 ARTICLE specifies the article to be marked as read.
8213 MARK specifies a string to be inserted at the beginning of the line."
8214   (gnus-summary-mark-article article mark))
8215
8216 (defun gnus-summary-clear-mark-forward (n)
8217   "Clear marks from N articles forward.
8218 If N is negative, clear backward instead.
8219 The difference between N and the number of marks cleared is returned."
8220   (interactive "p")
8221   (gnus-summary-mark-forward n gnus-unread-mark))
8222
8223 (defun gnus-summary-clear-mark-backward (n)
8224   "Clear marks from N articles backward.
8225 The difference between N and the number of marks cleared is returned."
8226   (interactive "p")
8227   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8228
8229 (defun gnus-summary-mark-unread-as-read ()
8230   "Intended to be used by `gnus-summary-mark-article-hook'."
8231   (when (memq gnus-current-article gnus-newsgroup-unreads)
8232     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8233
8234 (defun gnus-summary-mark-read-and-unread-as-read ()
8235   "Intended to be used by `gnus-summary-mark-article-hook'."
8236   (let ((mark (gnus-summary-article-mark)))
8237     (when (or (gnus-unread-mark-p mark)
8238               (gnus-read-mark-p mark))
8239       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8240
8241 (defun gnus-summary-mark-region-as-read (point mark all)
8242   "Mark all unread articles between point and mark as read.
8243 If given a prefix, mark all articles between point and mark as read,
8244 even ticked and dormant ones."
8245   (interactive "r\nP")
8246   (save-excursion
8247     (let (article)
8248       (goto-char point)
8249       (beginning-of-line)
8250       (while (and
8251               (< (point) mark)
8252               (progn
8253                 (when (or all
8254                           (memq (setq article (gnus-summary-article-number))
8255                                 gnus-newsgroup-unreads))
8256                   (gnus-summary-mark-article article gnus-del-mark))
8257                 t)
8258               (gnus-summary-find-next))))))
8259
8260 (defun gnus-summary-mark-below (score mark)
8261   "Mark articles with score less than SCORE with MARK."
8262   (interactive "P\ncMark: ")
8263   (setq score (if score
8264                   (prefix-numeric-value score)
8265                 (or gnus-summary-default-score 0)))
8266   (save-excursion
8267     (set-buffer gnus-summary-buffer)
8268     (goto-char (point-min))
8269     (while
8270         (progn
8271           (and (< (gnus-summary-article-score) score)
8272                (gnus-summary-mark-article nil mark))
8273           (gnus-summary-find-next)))))
8274
8275 (defun gnus-summary-kill-below (&optional score)
8276   "Mark articles with score below SCORE as read."
8277   (interactive "P")
8278   (gnus-summary-mark-below score gnus-killed-mark))
8279
8280 (defun gnus-summary-clear-above (&optional score)
8281   "Clear all marks from articles with score above SCORE."
8282   (interactive "P")
8283   (gnus-summary-mark-above score gnus-unread-mark))
8284
8285 (defun gnus-summary-tick-above (&optional score)
8286   "Tick all articles with score above SCORE."
8287   (interactive "P")
8288   (gnus-summary-mark-above score gnus-ticked-mark))
8289
8290 (defun gnus-summary-mark-above (score mark)
8291   "Mark articles with score over SCORE with MARK."
8292   (interactive "P\ncMark: ")
8293   (setq score (if score
8294                   (prefix-numeric-value score)
8295                 (or gnus-summary-default-score 0)))
8296   (save-excursion
8297     (set-buffer gnus-summary-buffer)
8298     (goto-char (point-min))
8299     (while (and (progn
8300                   (when (> (gnus-summary-article-score) score)
8301                     (gnus-summary-mark-article nil mark))
8302                   t)
8303                 (gnus-summary-find-next)))))
8304
8305 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8306 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8307 (defun gnus-summary-limit-include-expunged (&optional no-error)
8308   "Display all the hidden articles that were expunged for low scores."
8309   (interactive)
8310   (let ((buffer-read-only nil))
8311     (let ((scored gnus-newsgroup-scored)
8312           headers h)
8313       (while scored
8314         (unless (gnus-summary-goto-subject (caar scored))
8315           (and (setq h (gnus-summary-article-header (caar scored)))
8316                (< (cdar scored) gnus-summary-expunge-below)
8317                (push h headers)))
8318         (setq scored (cdr scored)))
8319       (if (not headers)
8320           (when (not no-error)
8321             (error "No expunged articles hidden"))
8322         (goto-char (point-min))
8323         (gnus-summary-prepare-unthreaded (nreverse headers))
8324         (goto-char (point-min))
8325         (gnus-summary-position-point)
8326         t))))
8327
8328 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8329   "Mark all unread articles in this newsgroup as read.
8330 If prefix argument ALL is non-nil, ticked and dormant articles will
8331 also be marked as read.
8332 If QUIETLY is non-nil, no questions will be asked.
8333 If TO-HERE is non-nil, it should be a point in the buffer.  All
8334 articles before this point will be marked as read.
8335 Note that this function will only catch up the unread article
8336 in the current summary buffer limitation.
8337 The number of articles marked as read is returned."
8338   (interactive "P")
8339   (prog1
8340       (save-excursion
8341         (when (or quietly
8342                   (not gnus-interactive-catchup) ;Without confirmation?
8343                   gnus-expert-user
8344                   (gnus-y-or-n-p
8345                    (if all
8346                        "Mark absolutely all articles as read? "
8347                      "Mark all unread articles as read? ")))
8348           (if (and not-mark
8349                    (not gnus-newsgroup-adaptive)
8350                    (not gnus-newsgroup-auto-expire)
8351                    (not gnus-suppress-duplicates)
8352                    (or (not gnus-use-cache)
8353                        (eq gnus-use-cache 'passive)))
8354               (progn
8355                 (when all
8356                   (setq gnus-newsgroup-marked nil
8357                         gnus-newsgroup-dormant nil))
8358                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8359             ;; We actually mark all articles as canceled, which we
8360             ;; have to do when using auto-expiry or adaptive scoring.
8361             (gnus-summary-show-all-threads)
8362             (when (gnus-summary-first-subject (not all) t)
8363               (while (and
8364                       (if to-here (< (point) to-here) t)
8365                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8366                       (gnus-summary-find-next (not all) nil nil t))))
8367             (gnus-set-mode-line 'summary))
8368           t))
8369     (gnus-summary-position-point)))
8370
8371 (defun gnus-summary-catchup-to-here (&optional all)
8372   "Mark all unticked articles before the current one as read.
8373 If ALL is non-nil, also mark ticked and dormant articles as read."
8374   (interactive "P")
8375   (save-excursion
8376     (gnus-save-hidden-threads
8377       (let ((beg (point)))
8378         ;; We check that there are unread articles.
8379         (when (or all (gnus-summary-find-prev))
8380           (gnus-summary-catchup all t beg)))))
8381   (gnus-summary-position-point))
8382
8383 (defun gnus-summary-catchup-all (&optional quietly)
8384   "Mark all articles in this newsgroup as read."
8385   (interactive "P")
8386   (gnus-summary-catchup t quietly))
8387
8388 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8389   "Mark all unread articles in this group as read, then exit.
8390 If prefix argument ALL is non-nil, all articles are marked as read."
8391   (interactive "P")
8392   (when (gnus-summary-catchup all quietly nil 'fast)
8393     ;; Select next newsgroup or exit.
8394     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8395              (eq gnus-auto-select-next 'quietly))
8396         (gnus-summary-next-group nil)
8397       (gnus-summary-exit))))
8398
8399 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8400   "Mark all articles in this newsgroup as read, and then exit."
8401   (interactive "P")
8402   (gnus-summary-catchup-and-exit t quietly))
8403
8404 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8405 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8406   "Mark all articles in this group as read and select the next group.
8407 If given a prefix, mark all articles, unread as well as ticked, as
8408 read."
8409   (interactive "P")
8410   (save-excursion
8411     (gnus-summary-catchup all))
8412   (gnus-summary-next-group t nil nil))
8413
8414 ;; Thread-based commands.
8415
8416 (defun gnus-summary-articles-in-thread (&optional article)
8417   "Return a list of all articles in the current thread.
8418 If ARTICLE is non-nil, return all articles in the thread that starts
8419 with that article."
8420   (let* ((article (or article (gnus-summary-article-number)))
8421          (data (gnus-data-find-list article))
8422          (top-level (gnus-data-level (car data)))
8423          (top-subject
8424           (cond ((null gnus-thread-operation-ignore-subject)
8425                  (gnus-simplify-subject-re
8426                   (mail-header-subject (gnus-data-header (car data)))))
8427                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8428                  (gnus-simplify-subject-fuzzy
8429                   (mail-header-subject (gnus-data-header (car data)))))
8430                 (t nil)))
8431          (end-point (save-excursion
8432                       (if (gnus-summary-go-to-next-thread)
8433                           (point) (point-max))))
8434          articles)
8435     (while (and data
8436                 (< (gnus-data-pos (car data)) end-point))
8437       (when (or (not top-subject)
8438                 (string= top-subject
8439                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8440                              (gnus-simplify-subject-fuzzy
8441                               (mail-header-subject
8442                                (gnus-data-header (car data))))
8443                            (gnus-simplify-subject-re
8444                             (mail-header-subject
8445                              (gnus-data-header (car data)))))))
8446         (push (gnus-data-number (car data)) articles))
8447       (unless (and (setq data (cdr data))
8448                    (> (gnus-data-level (car data)) top-level))
8449         (setq data nil)))
8450     ;; Return the list of articles.
8451     (nreverse articles)))
8452
8453 (defun gnus-summary-rethread-current ()
8454   "Rethread the thread the current article is part of."
8455   (interactive)
8456   (let* ((gnus-show-threads t)
8457          (article (gnus-summary-article-number))
8458          (id (mail-header-id (gnus-summary-article-header)))
8459          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8460     (unless id
8461       (error "No article on the current line"))
8462     (gnus-rebuild-thread id)
8463     (gnus-summary-goto-subject article)))
8464
8465 (defun gnus-summary-reparent-thread ()
8466   "Make the current article child of the marked (or previous) article.
8467
8468 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8469 is non-nil or the Subject: of both articles are the same."
8470   (interactive)
8471   (unless (not (gnus-group-read-only-p))
8472     (error "The current newsgroup does not support article editing"))
8473   (unless (<= (length gnus-newsgroup-processable) 1)
8474     (error "No more than one article may be marked"))
8475   (save-window-excursion
8476     (let ((gnus-article-buffer " *reparent*")
8477           (current-article (gnus-summary-article-number))
8478           ;; First grab the marked article, otherwise one line up.
8479           (parent-article (if (not (null gnus-newsgroup-processable))
8480                               (car gnus-newsgroup-processable)
8481                             (save-excursion
8482                               (if (eq (forward-line -1) 0)
8483                                   (gnus-summary-article-number)
8484                                 (error "Beginning of summary buffer"))))))
8485       (unless (not (eq current-article parent-article))
8486         (error "An article may not be self-referential"))
8487       (let ((message-id (mail-header-id
8488                          (gnus-summary-article-header parent-article))))
8489         (unless (and message-id (not (equal message-id "")))
8490           (error "No message-id in desired parent"))
8491         (gnus-with-article current-article
8492           (goto-char (point-min))
8493           (if (re-search-forward "^References: " nil t)
8494               (progn
8495                 (re-search-forward "^[^ \t]" nil t)
8496                 (forward-line -1)
8497                 (end-of-line)
8498                 (insert " " message-id))
8499             (insert "References: " message-id "\n")))
8500         (set-buffer gnus-summary-buffer)
8501         (gnus-summary-unmark-all-processable)
8502         (gnus-summary-update-article current-article)
8503         (gnus-summary-rethread-current)
8504         (gnus-message 3 "Article %d is now the child of article %d"
8505                       current-article parent-article)))))
8506
8507 (defun gnus-summary-toggle-threads (&optional arg)
8508   "Toggle showing conversation threads.
8509 If ARG is positive number, turn showing conversation threads on."
8510   (interactive "P")
8511   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8512     (setq gnus-show-threads
8513           (if (null arg) (not gnus-show-threads)
8514             (> (prefix-numeric-value arg) 0)))
8515     (gnus-summary-prepare)
8516     (gnus-summary-goto-subject current)
8517     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8518     (gnus-summary-position-point)))
8519
8520 (defun gnus-summary-show-all-threads ()
8521   "Show all threads."
8522   (interactive)
8523   (save-excursion
8524     (let ((buffer-read-only nil))
8525       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8526   (gnus-summary-position-point))
8527
8528 (defun gnus-summary-show-thread ()
8529   "Show thread subtrees.
8530 Returns nil if no thread was there to be shown."
8531   (interactive)
8532   (let ((buffer-read-only nil)
8533         (orig (point))
8534         ;; first goto end then to beg, to have point at beg after let
8535         (end (progn (end-of-line) (point)))
8536         (beg (progn (beginning-of-line) (point))))
8537     (prog1
8538         ;; Any hidden lines here?
8539         (search-forward "\r" end t)
8540       (subst-char-in-region beg end ?\^M ?\n t)
8541       (goto-char orig)
8542       (gnus-summary-position-point))))
8543
8544 (defun gnus-summary-hide-all-threads ()
8545   "Hide all thread subtrees."
8546   (interactive)
8547   (save-excursion
8548     (goto-char (point-min))
8549     (gnus-summary-hide-thread)
8550     (while (zerop (gnus-summary-next-thread 1 t))
8551       (gnus-summary-hide-thread)))
8552   (gnus-summary-position-point))
8553
8554 (defun gnus-summary-hide-thread ()
8555   "Hide thread subtrees.
8556 Returns nil if no threads were there to be hidden."
8557   (interactive)
8558   (let ((buffer-read-only nil)
8559         (start (point))
8560         (article (gnus-summary-article-number)))
8561     (goto-char start)
8562     ;; Go forward until either the buffer ends or the subthread
8563     ;; ends.
8564     (when (and (not (eobp))
8565                (or (zerop (gnus-summary-next-thread 1 t))
8566                    (goto-char (point-max))))
8567       (prog1
8568           (if (and (> (point) start)
8569                    (search-backward "\n" start t))
8570               (progn
8571                 (subst-char-in-region start (point) ?\n ?\^M)
8572                 (gnus-summary-goto-subject article))
8573             (goto-char start)
8574             nil)
8575         ;;(gnus-summary-position-point)
8576         ))))
8577
8578 (defun gnus-summary-go-to-next-thread (&optional previous)
8579   "Go to the same level (or less) next thread.
8580 If PREVIOUS is non-nil, go to previous thread instead.
8581 Return the article number moved to, or nil if moving was impossible."
8582   (let ((level (gnus-summary-thread-level))
8583         (way (if previous -1 1))
8584         (beg (point)))
8585     (forward-line way)
8586     (while (and (not (eobp))
8587                 (< level (gnus-summary-thread-level)))
8588       (forward-line way))
8589     (if (eobp)
8590         (progn
8591           (goto-char beg)
8592           nil)
8593       (setq beg (point))
8594       (prog1
8595           (gnus-summary-article-number)
8596         (goto-char beg)))))
8597
8598 (defun gnus-summary-next-thread (n &optional silent)
8599   "Go to the same level next N'th thread.
8600 If N is negative, search backward instead.
8601 Returns the difference between N and the number of skips actually
8602 done.
8603
8604 If SILENT, don't output messages."
8605   (interactive "p")
8606   (let ((backward (< n 0))
8607         (n (abs n)))
8608     (while (and (> n 0)
8609                 (gnus-summary-go-to-next-thread backward))
8610       (decf n))
8611     (unless silent
8612       (gnus-summary-position-point))
8613     (when (and (not silent) (/= 0 n))
8614       (gnus-message 7 "No more threads"))
8615     n))
8616
8617 (defun gnus-summary-prev-thread (n)
8618   "Go to the same level previous N'th thread.
8619 Returns the difference between N and the number of skips actually
8620 done."
8621   (interactive "p")
8622   (gnus-summary-next-thread (- n)))
8623
8624 (defun gnus-summary-go-down-thread ()
8625   "Go down one level in the current thread."
8626   (let ((children (gnus-summary-article-children)))
8627     (when children
8628       (gnus-summary-goto-subject (car children)))))
8629
8630 (defun gnus-summary-go-up-thread ()
8631   "Go up one level in the current thread."
8632   (let ((parent (gnus-summary-article-parent)))
8633     (when parent
8634       (gnus-summary-goto-subject parent))))
8635
8636 (defun gnus-summary-down-thread (n)
8637   "Go down thread N steps.
8638 If N is negative, go up instead.
8639 Returns the difference between N and how many steps down that were
8640 taken."
8641   (interactive "p")
8642   (let ((up (< n 0))
8643         (n (abs n)))
8644     (while (and (> n 0)
8645                 (if up (gnus-summary-go-up-thread)
8646                   (gnus-summary-go-down-thread)))
8647       (setq n (1- n)))
8648     (gnus-summary-position-point)
8649     (when (/= 0 n)
8650       (gnus-message 7 "Can't go further"))
8651     n))
8652
8653 (defun gnus-summary-up-thread (n)
8654   "Go up thread N steps.
8655 If N is negative, go up instead.
8656 Returns the difference between N and how many steps down that were
8657 taken."
8658   (interactive "p")
8659   (gnus-summary-down-thread (- n)))
8660
8661 (defun gnus-summary-top-thread ()
8662   "Go to the top of the thread."
8663   (interactive)
8664   (while (gnus-summary-go-up-thread))
8665   (gnus-summary-article-number))
8666
8667 (defun gnus-summary-kill-thread (&optional unmark)
8668   "Mark articles under current thread as read.
8669 If the prefix argument is positive, remove any kinds of marks.
8670 If the prefix argument is negative, tick articles instead."
8671   (interactive "P")
8672   (when unmark
8673     (setq unmark (prefix-numeric-value unmark)))
8674   (let ((articles (gnus-summary-articles-in-thread)))
8675     (save-excursion
8676       ;; Expand the thread.
8677       (gnus-summary-show-thread)
8678       ;; Mark all the articles.
8679       (while articles
8680         (gnus-summary-goto-subject (car articles))
8681         (cond ((null unmark)
8682                (gnus-summary-mark-article-as-read gnus-killed-mark))
8683               ((> unmark 0)
8684                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8685               (t
8686                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8687         (setq articles (cdr articles))))
8688     ;; Hide killed subtrees.
8689     (and (null unmark)
8690          gnus-thread-hide-killed
8691          (gnus-summary-hide-thread))
8692     ;; If marked as read, go to next unread subject.
8693     (when (null unmark)
8694       ;; Go to next unread subject.
8695       (gnus-summary-next-subject 1 t)))
8696   (gnus-set-mode-line 'summary))
8697
8698 ;; Summary sorting commands
8699
8700 (defun gnus-summary-sort-by-number (&optional reverse)
8701   "Sort the summary buffer by article number.
8702 Argument REVERSE means reverse order."
8703   (interactive "P")
8704   (gnus-summary-sort 'number reverse))
8705
8706 (defun gnus-summary-sort-by-author (&optional reverse)
8707   "Sort the summary buffer by author name alphabetically.
8708 If case-fold-search is non-nil, case of letters is ignored.
8709 Argument REVERSE means reverse order."
8710   (interactive "P")
8711   (gnus-summary-sort 'author reverse))
8712
8713 (defun gnus-summary-sort-by-subject (&optional reverse)
8714   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8715 If case-fold-search is non-nil, case of letters is ignored.
8716 Argument REVERSE means reverse order."
8717   (interactive "P")
8718   (gnus-summary-sort 'subject reverse))
8719
8720 (defun gnus-summary-sort-by-date (&optional reverse)
8721   "Sort the summary buffer by date.
8722 Argument REVERSE means reverse order."
8723   (interactive "P")
8724   (gnus-summary-sort 'date reverse))
8725
8726 (defun gnus-summary-sort-by-score (&optional reverse)
8727   "Sort the summary buffer by score.
8728 Argument REVERSE means reverse order."
8729   (interactive "P")
8730   (gnus-summary-sort 'score reverse))
8731
8732 (defun gnus-summary-sort-by-lines (&optional reverse)
8733   "Sort the summary buffer by the number of lines.
8734 Argument REVERSE means reverse order."
8735   (interactive "P")
8736   (gnus-summary-sort 'lines reverse))
8737
8738 (defun gnus-summary-sort-by-chars (&optional reverse)
8739   "Sort the summary buffer by article length.
8740 Argument REVERSE means reverse order."
8741   (interactive "P")
8742   (gnus-summary-sort 'chars reverse))   
8743
8744 (defun gnus-summary-sort (predicate reverse)
8745   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8746   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8747          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8748          (gnus-thread-sort-functions
8749           (if (not reverse)
8750               thread
8751             `(lambda (t1 t2)
8752                (,thread t2 t1))))
8753          (gnus-article-sort-functions
8754           (if (not reverse)
8755               article
8756             `(lambda (t1 t2)
8757                (,article t2 t1))))
8758          (buffer-read-only)
8759          (gnus-summary-prepare-hook nil))
8760     ;; We do the sorting by regenerating the threads.
8761     (gnus-summary-prepare)
8762     ;; Hide subthreads if needed.
8763     (when (and gnus-show-threads gnus-thread-hide-subtree)
8764       (gnus-summary-hide-all-threads))))
8765
8766 ;; Summary saving commands.
8767
8768 (defun gnus-summary-save-article (&optional n not-saved)
8769   "Save the current article using the default saver function.
8770 If N is a positive number, save the N next articles.
8771 If N is a negative number, save the N previous articles.
8772 If N is nil and any articles have been marked with the process mark,
8773 save those articles instead.
8774 The variable `gnus-default-article-saver' specifies the saver function."
8775   (interactive "P")
8776   (let* ((articles (gnus-summary-work-articles n))
8777          (save-buffer (save-excursion
8778                         (nnheader-set-temp-buffer " *Gnus Save*")))
8779          (num (length articles))
8780          header file)
8781     (dolist (article articles)
8782       (setq header (gnus-summary-article-header article))
8783       (if (not (vectorp header))
8784           ;; This is a pseudo-article.
8785           (if (assq 'name header)
8786               (gnus-copy-file (cdr (assq 'name header)))
8787             (gnus-message 1 "Article %d is unsaveable" article))
8788         ;; This is a real article.
8789         (save-window-excursion
8790           (gnus-summary-select-article t nil nil article))
8791         (save-excursion
8792           (set-buffer save-buffer)
8793           (erase-buffer)
8794           (insert-buffer-substring gnus-original-article-buffer))
8795         (setq file (gnus-article-save save-buffer file num))
8796         (gnus-summary-remove-process-mark article)
8797         (unless not-saved
8798           (gnus-summary-set-saved-mark article))))
8799     (gnus-kill-buffer save-buffer)
8800     (gnus-summary-position-point)
8801     (gnus-set-mode-line 'summary)
8802     n))
8803
8804 (defun gnus-summary-pipe-output (&optional arg)
8805   "Pipe the current article to a subprocess.
8806 If N is a positive number, pipe the N next articles.
8807 If N is a negative number, pipe the N previous articles.
8808 If N is nil and any articles have been marked with the process mark,
8809 pipe those articles instead."
8810   (interactive "P")
8811   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8812     (gnus-summary-save-article arg t))
8813   (gnus-configure-windows 'pipe))
8814
8815 (defun gnus-summary-save-article-mail (&optional arg)
8816   "Append the current article to an mail file.
8817 If N is a positive number, save the N next articles.
8818 If N is a negative number, save the N previous articles.
8819 If N is nil and any articles have been marked with the process mark,
8820 save those articles instead."
8821   (interactive "P")
8822   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8823     (gnus-summary-save-article arg)))
8824
8825 (defun gnus-summary-save-article-rmail (&optional arg)
8826   "Append the current article to an rmail file.
8827 If N is a positive number, save the N next articles.
8828 If N is a negative number, save the N previous articles.
8829 If N is nil and any articles have been marked with the process mark,
8830 save those articles instead."
8831   (interactive "P")
8832   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8833     (gnus-summary-save-article arg)))
8834
8835 (defun gnus-summary-save-article-file (&optional arg)
8836   "Append the current article to a file.
8837 If N is a positive number, save the N next articles.
8838 If N is a negative number, save the N previous articles.
8839 If N is nil and any articles have been marked with the process mark,
8840 save those articles instead."
8841   (interactive "P")
8842   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8843     (gnus-summary-save-article arg)))
8844
8845 (defun gnus-summary-write-article-file (&optional arg)
8846   "Write the current article to a file, deleting the previous file.
8847 If N is a positive number, save the N next articles.
8848 If N is a negative number, save the N previous articles.
8849 If N is nil and any articles have been marked with the process mark,
8850 save those articles instead."
8851   (interactive "P")
8852   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8853     (gnus-summary-save-article arg)))
8854
8855 (defun gnus-summary-save-article-body-file (&optional arg)
8856   "Append the current article body to a file.
8857 If N is a positive number, save the N next articles.
8858 If N is a negative number, save the N previous articles.
8859 If N is nil and any articles have been marked with the process mark,
8860 save those articles instead."
8861   (interactive "P")
8862   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8863     (gnus-summary-save-article arg)))
8864
8865 (defun gnus-summary-pipe-message (program)
8866   "Pipe the current article through PROGRAM."
8867   (interactive "sProgram: ")
8868   (gnus-summary-select-article)
8869   (let ((mail-header-separator ""))
8870     (gnus-eval-in-buffer-window gnus-article-buffer
8871       (save-restriction
8872         (widen)
8873         (let ((start (window-start))
8874               buffer-read-only)
8875           (message-pipe-buffer-body program)
8876           (set-window-start (get-buffer-window (current-buffer)) start))))))
8877
8878 (defun gnus-get-split-value (methods)
8879   "Return a value based on the split METHODS."
8880   (let (split-name method result match)
8881     (when methods
8882       (save-excursion
8883         (set-buffer gnus-original-article-buffer)
8884         (save-restriction
8885           (nnheader-narrow-to-headers)
8886           (while methods
8887             (goto-char (point-min))
8888             (setq method (pop methods))
8889             (setq match (car method))
8890             (when (cond
8891                    ((stringp match)
8892                     ;; Regular expression.
8893                     (ignore-errors
8894                       (re-search-forward match nil t)))
8895                    ((gnus-functionp match)
8896                     ;; Function.
8897                     (save-restriction
8898                       (widen)
8899                       (setq result (funcall match gnus-newsgroup-name))))
8900                    ((consp match)
8901                     ;; Form.
8902                     (save-restriction
8903                       (widen)
8904                       (setq result (eval match)))))
8905               (setq split-name (append (cdr method) split-name))
8906               (cond ((stringp result)
8907                      (push (expand-file-name
8908                             result gnus-article-save-directory)
8909                            split-name))
8910                     ((consp result)
8911                      (setq split-name (append result split-name)))))))))
8912     (nreverse split-name)))
8913
8914 (defun gnus-valid-move-group-p (group)
8915   (and (boundp group)
8916        (symbol-name group)
8917        (symbol-value group)
8918        (gnus-get-function (gnus-find-method-for-group
8919                            (symbol-name group)) 'request-accept-article t)))
8920
8921 (defun gnus-read-move-group-name (prompt default articles prefix)
8922   "Read a group name."
8923   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8924          (minibuffer-confirm-incomplete nil) ; XEmacs
8925          (prom
8926           (format "%s %s to:"
8927                   prompt
8928                   (if (> (length articles) 1)
8929                       (format "these %d articles" (length articles))
8930                     "this article")))
8931          (to-newsgroup
8932           (cond
8933            ((null split-name)
8934             (gnus-completing-read default prom
8935                                   gnus-active-hashtb
8936                                   'gnus-valid-move-group-p
8937                                   nil prefix
8938                                   'gnus-group-history))
8939            ((= 1 (length split-name))
8940             (gnus-completing-read (car split-name) prom
8941                                   gnus-active-hashtb
8942                                   'gnus-valid-move-group-p
8943                                   nil nil
8944                                   'gnus-group-history))
8945            (t
8946             (gnus-completing-read nil prom
8947                                   (mapcar (lambda (el) (list el))
8948                                           (nreverse split-name))
8949                                   nil nil nil
8950                                   'gnus-group-history)))))
8951     (when to-newsgroup
8952       (if (or (string= to-newsgroup "")
8953               (string= to-newsgroup prefix))
8954           (setq to-newsgroup default))
8955       (unless to-newsgroup
8956         (error "No group name entered"))
8957       (or (gnus-active to-newsgroup)
8958           (gnus-activate-group to-newsgroup)
8959           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8960                                      to-newsgroup))
8961               (or (and (gnus-request-create-group
8962                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8963                        (gnus-activate-group to-newsgroup nil nil
8964                                             (gnus-group-name-to-method
8965                                              to-newsgroup)))
8966                   (error "Couldn't create group %s" to-newsgroup)))
8967           (error "No such group: %s" to-newsgroup)))
8968     to-newsgroup))
8969
8970 (defun gnus-summary-save-parts (type dir n reverse)
8971   "Save parts matching TYPE to DIR.
8972 If REVERSE, save parts that do not match TYPE."
8973   (interactive
8974    (list (read-string "Save parts of type: " "image/.*")
8975          (read-file-name "Save to directory: " t nil t)
8976          current-prefix-arg))
8977   (gnus-summary-iterate n
8978     (let ((gnus-display-mime-function nil)
8979           (gnus-inhibit-treatment t))
8980       (gnus-summary-select-article))
8981     (save-excursion
8982       (set-buffer gnus-article-buffer)
8983       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8984         (when handles
8985           (gnus-summary-save-parts-1 type dir handles reverse)
8986           (mm-destroy-parts handles))))))
8987
8988 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8989   (if (stringp (car handle))
8990       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8991               (cdr handle))
8992     (when (if reverse
8993               (not (string-match type (mm-handle-media-type handle)))
8994             (string-match type (mm-handle-media-type handle)))
8995       (let ((file (expand-file-name
8996                    (file-name-nondirectory
8997                     (or
8998                      (mail-content-type-get
8999                       (mm-handle-disposition handle) 'filename)
9000                      (concat gnus-newsgroup-name "." gnus-current-article)))
9001                    dir)))
9002         (unless (file-exists-p file)
9003           (mm-save-part-to-file handle file))))))
9004
9005 ;; Summary extract commands
9006
9007 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9008   (let ((buffer-read-only nil)
9009         (article (gnus-summary-article-number))
9010         after-article b e)
9011     (unless (gnus-summary-goto-subject article)
9012       (error "No such article: %d" article))
9013     (gnus-summary-position-point)
9014     ;; If all commands are to be bunched up on one line, we collect
9015     ;; them here.
9016     (unless gnus-view-pseudos-separately
9017       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9018             files action)
9019         (while ps
9020           (setq action (cdr (assq 'action (car ps))))
9021           (setq files (list (cdr (assq 'name (car ps)))))
9022           (while (and ps (cdr ps)
9023                       (string= (or action "1")
9024                                (or (cdr (assq 'action (cadr ps))) "2")))
9025             (push (cdr (assq 'name (cadr ps))) files)
9026             (setcdr ps (cddr ps)))
9027           (when files
9028             (when (not (string-match "%s" action))
9029               (push " " files))
9030             (push " " files)
9031             (when (assq 'execute (car ps))
9032               (setcdr (assq 'execute (car ps))
9033                       (funcall (if (string-match "%s" action)
9034                                    'format 'concat)
9035                                action
9036                                (mapconcat
9037                                 (lambda (f)
9038                                   (if (equal f " ")
9039                                       f
9040                                     (mm-quote-arg f)))
9041                                 files " ")))))
9042           (setq ps (cdr ps)))))
9043     (if (and gnus-view-pseudos (not not-view))
9044         (while pslist
9045           (when (assq 'execute (car pslist))
9046             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9047                                   (eq gnus-view-pseudos 'not-confirm)))
9048           (setq pslist (cdr pslist)))
9049       (save-excursion
9050         (while pslist
9051           (setq after-article (or (cdr (assq 'article (car pslist)))
9052                                   (gnus-summary-article-number)))
9053           (gnus-summary-goto-subject after-article)
9054           (forward-line 1)
9055           (setq b (point))
9056           (insert "    " (file-name-nondirectory
9057                           (cdr (assq 'name (car pslist))))
9058                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9059           (setq e (point))
9060           (forward-line -1)             ; back to `b'
9061           (gnus-add-text-properties
9062            b (1- e) (list 'gnus-number gnus-reffed-article-number
9063                           gnus-mouse-face-prop gnus-mouse-face))
9064           (gnus-data-enter
9065            after-article gnus-reffed-article-number
9066            gnus-unread-mark b (car pslist) 0 (- e b))
9067           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9068           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9069           (setq pslist (cdr pslist)))))))
9070
9071 (defun gnus-pseudos< (p1 p2)
9072   (let ((c1 (cdr (assq 'action p1)))
9073         (c2 (cdr (assq 'action p2))))
9074     (and c1 c2 (string< c1 c2))))
9075
9076 (defun gnus-request-pseudo-article (props)
9077   (cond ((assq 'execute props)
9078          (gnus-execute-command (cdr (assq 'execute props)))))
9079   (let ((gnus-current-article (gnus-summary-article-number)))
9080     (gnus-run-hooks 'gnus-mark-article-hook)))
9081
9082 (defun gnus-execute-command (command &optional automatic)
9083   (save-excursion
9084     (gnus-article-setup-buffer)
9085     (set-buffer gnus-article-buffer)
9086     (setq buffer-read-only nil)
9087     (let ((command (if automatic command
9088                      (read-string "Command: " (cons command 0)))))
9089       (erase-buffer)
9090       (insert "$ " command "\n\n")
9091       (if gnus-view-pseudo-asynchronously
9092           (start-process "gnus-execute" (current-buffer) shell-file-name
9093                          shell-command-switch command)
9094         (call-process shell-file-name nil t nil
9095                       shell-command-switch command)))))
9096
9097 ;; Summary kill commands.
9098
9099 (defun gnus-summary-edit-global-kill (article)
9100   "Edit the \"global\" kill file."
9101   (interactive (list (gnus-summary-article-number)))
9102   (gnus-group-edit-global-kill article))
9103
9104 (defun gnus-summary-edit-local-kill ()
9105   "Edit a local kill file applied to the current newsgroup."
9106   (interactive)
9107   (setq gnus-current-headers (gnus-summary-article-header))
9108   (gnus-group-edit-local-kill
9109    (gnus-summary-article-number) gnus-newsgroup-name))
9110
9111 ;;; Header reading.
9112
9113 (defun gnus-read-header (id &optional header)
9114   "Read the headers of article ID and enter them into the Gnus system."
9115   (let ((group gnus-newsgroup-name)
9116         (gnus-override-method
9117          (and (gnus-news-group-p gnus-newsgroup-name)
9118               gnus-refer-article-method))
9119         where)
9120     ;; First we check to see whether the header in question is already
9121     ;; fetched.
9122     (if (stringp id)
9123         ;; This is a Message-ID.
9124         (setq header (or header (gnus-id-to-header id)))
9125       ;; This is an article number.
9126       (setq header (or header (gnus-summary-article-header id))))
9127     (if (and header
9128              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9129         ;; We have found the header.
9130         header
9131       ;; If this is a sparse article, we have to nix out its
9132       ;; previous entry in the thread hashtb.
9133       (when (and header
9134                  (gnus-summary-article-sparse-p (mail-header-number header)))
9135         (let* ((parent (gnus-parent-id (mail-header-references header)))
9136                (thread (and parent (gnus-id-to-thread parent))))
9137           (when thread
9138             (delq (assq header thread) thread))))
9139       ;; We have to really fetch the header to this article.
9140       (save-excursion
9141         (set-buffer nntp-server-buffer)
9142         (when (setq where (gnus-request-head id group))
9143           (nnheader-fold-continuation-lines)
9144           (goto-char (point-max))
9145           (insert ".\n")
9146           (goto-char (point-min))
9147           (insert "211 ")
9148           (princ (cond
9149                   ((numberp id) id)
9150                   ((cdr where) (cdr where))
9151                   (header (mail-header-number header))
9152                   (t gnus-reffed-article-number))
9153                  (current-buffer))
9154           (insert " Article retrieved.\n"))
9155         (if (or (not where)
9156                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9157             ()                          ; Malformed head.
9158           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9159             (when (and (stringp id)
9160                        (not (string= (gnus-group-real-name group)
9161                                      (car where))))
9162               ;; If we fetched by Message-ID and the article came
9163               ;; from a different group, we fudge some bogus article
9164               ;; numbers for this article.
9165               (mail-header-set-number header gnus-reffed-article-number))
9166             (save-excursion
9167               (set-buffer gnus-summary-buffer)
9168               (decf gnus-reffed-article-number)
9169               (gnus-remove-header (mail-header-number header))
9170               (push header gnus-newsgroup-headers)
9171               (setq gnus-current-headers header)
9172               (push (mail-header-number header) gnus-newsgroup-limit)))
9173           header)))))
9174
9175 (defun gnus-remove-header (number)
9176   "Remove header NUMBER from `gnus-newsgroup-headers'."
9177   (if (and gnus-newsgroup-headers
9178            (= number (mail-header-number (car gnus-newsgroup-headers))))
9179       (pop gnus-newsgroup-headers)
9180     (let ((headers gnus-newsgroup-headers))
9181       (while (and (cdr headers)
9182                   (not (= number (mail-header-number (cadr headers)))))
9183         (pop headers))
9184       (when (cdr headers)
9185         (setcdr headers (cddr headers))))))
9186
9187 ;;;
9188 ;;; summary highlights
9189 ;;;
9190
9191 (defun gnus-highlight-selected-summary ()
9192   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9193   ;; Highlight selected article in summary buffer
9194   (when gnus-summary-selected-face
9195     (save-excursion
9196       (let* ((beg (progn (beginning-of-line) (point)))
9197              (end (progn (end-of-line) (point)))
9198              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9199              (from (if (get-text-property beg gnus-mouse-face-prop)
9200                        beg
9201                      (or (next-single-property-change
9202                           beg gnus-mouse-face-prop nil end)
9203                          beg)))
9204              (to
9205               (if (= from end)
9206                   (- from 2)
9207                 (or (next-single-property-change
9208                      from gnus-mouse-face-prop nil end)
9209                     end))))
9210         ;; If no mouse-face prop on line we will have to = from = end,
9211         ;; so we highlight the entire line instead.
9212         (when (= (+ to 2) from)
9213           (setq from beg)
9214           (setq to end))
9215         (if gnus-newsgroup-selected-overlay
9216             ;; Move old overlay.
9217             (gnus-move-overlay
9218              gnus-newsgroup-selected-overlay from to (current-buffer))
9219           ;; Create new overlay.
9220           (gnus-overlay-put
9221            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9222            'face gnus-summary-selected-face))))))
9223
9224 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9225 (defun gnus-summary-highlight-line ()
9226   "Highlight current line according to `gnus-summary-highlight'."
9227   (let* ((list gnus-summary-highlight)
9228          (p (point))
9229          (end (progn (end-of-line) (point)))
9230          ;; now find out where the line starts and leave point there.
9231          (beg (progn (beginning-of-line) (point)))
9232          (article (gnus-summary-article-number))
9233          (score (or (cdr (assq (or article gnus-current-article)
9234                                gnus-newsgroup-scored))
9235                     gnus-summary-default-score 0))
9236          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9237          (inhibit-read-only t))
9238     ;; Eval the cars of the lists until we find a match.
9239     (let ((default gnus-summary-default-score))
9240       (while (and list
9241                   (not (eval (caar list))))
9242         (setq list (cdr list))))
9243     (let ((face (cdar list)))
9244       (unless (eq face (get-text-property beg 'face))
9245         (gnus-put-text-property-excluding-characters-with-faces
9246          beg end 'face
9247          (setq face (if (boundp face) (symbol-value face) face)))
9248         (when gnus-summary-highlight-line-function
9249           (funcall gnus-summary-highlight-line-function article face))))
9250     (goto-char p)))
9251
9252 (defun gnus-update-read-articles (group unread &optional compute)
9253   "Update the list of read articles in GROUP."
9254   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9255          (entry (gnus-gethash group gnus-newsrc-hashtb))
9256          (info (nth 2 entry))
9257          (prev 1)
9258          (unread (sort (copy-sequence unread) '<))
9259          read)
9260     (if (or (not info) (not active))
9261         ;; There is no info on this group if it was, in fact,
9262         ;; killed.  Gnus stores no information on killed groups, so
9263         ;; there's nothing to be done.
9264         ;; One could store the information somewhere temporarily,
9265         ;; perhaps...  Hmmm...
9266         ()
9267       ;; Remove any negative articles numbers.
9268       (while (and unread (< (car unread) 0))
9269         (setq unread (cdr unread)))
9270       ;; Remove any expired article numbers
9271       (while (and unread (< (car unread) (car active)))
9272         (setq unread (cdr unread)))
9273       ;; Compute the ranges of read articles by looking at the list of
9274       ;; unread articles.
9275       (while unread
9276         (when (/= (car unread) prev)
9277           (push (if (= prev (1- (car unread))) prev
9278                   (cons prev (1- (car unread))))
9279                 read))
9280         (setq prev (1+ (car unread)))
9281         (setq unread (cdr unread)))
9282       (when (<= prev (cdr active))
9283         (push (cons prev (cdr active)) read))
9284       (setq read (if (> (length read) 1) (nreverse read) read))
9285       (if compute
9286           read
9287         (save-excursion
9288           (set-buffer gnus-group-buffer)
9289           (gnus-undo-register
9290             `(progn
9291                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9292                (gnus-info-set-read ',info ',(gnus-info-read info))
9293                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9294                (gnus-group-update-group ,group t))))
9295        ;; Propagate the read marks to the backend.
9296        (if (gnus-check-backend-function 'request-set-mark group)
9297            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9298                  (add (gnus-remove-from-range read (gnus-info-read info))))
9299              (when (or add del)
9300                (unless (gnus-check-group group)
9301                  (error "Can't open server for %s" group))
9302                (gnus-request-set-mark
9303                 group (delq nil (list (if add (list add 'add '(read)))
9304                                       (if del (list del 'del '(read)))))))))
9305         ;; Enter this list into the group info.
9306         (gnus-info-set-read info read)
9307         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9308         (gnus-get-unread-articles-in-group info (gnus-active group))
9309         t))))
9310
9311 (defun gnus-offer-save-summaries ()
9312   "Offer to save all active summary buffers."
9313   (save-excursion
9314     (let ((buflist (buffer-list))
9315           buffers bufname)
9316       ;; Go through all buffers and find all summaries.
9317       (while buflist
9318         (and (setq bufname (buffer-name (car buflist)))
9319              (string-match "Summary" bufname)
9320              (save-excursion
9321                (set-buffer bufname)
9322                ;; We check that this is, indeed, a summary buffer.
9323                (and (eq major-mode 'gnus-summary-mode)
9324                     ;; Also make sure this isn't bogus.
9325                     gnus-newsgroup-prepared
9326                     ;; Also make sure that this isn't a dead summary buffer.
9327                     (not gnus-dead-summary-mode)))
9328              (push bufname buffers))
9329         (setq buflist (cdr buflist)))
9330       ;; Go through all these summary buffers and offer to save them.
9331       (when buffers
9332         (map-y-or-n-p
9333          "Update summary buffer %s? "
9334          (lambda (buf)
9335            (switch-to-buffer buf)
9336            (gnus-summary-exit))
9337          buffers)))))
9338
9339 (defun gnus-summary-setup-default-charset ()
9340   "Setup newsgroup default charset."
9341   (if (equal gnus-newsgroup-name "nndraft:drafts")
9342       (setq gnus-newsgroup-charset nil)
9343   (let* ((name (and gnus-newsgroup-name
9344                    (gnus-group-real-name gnus-newsgroup-name)))
9345          (ignored-charsets 
9346           (or gnus-newsgroup-ephemeral-ignored-charsets
9347               (append
9348                (and gnus-newsgroup-name
9349                     (or (gnus-group-find-parameter gnus-newsgroup-name
9350                                                    'ignored-charsets t)
9351                         (let ((alist gnus-group-ignored-charsets-alist)
9352                            elem (charsets nil))
9353                           (while (setq elem (pop alist))
9354                             (when (and name
9355                                        (string-match (car elem) name))
9356                               (setq alist nil
9357                                     charsets (cdr elem))))
9358                           charsets))))
9359               gnus-newsgroup-ignored-charsets)))
9360     (setq gnus-newsgroup-charset
9361           (or gnus-newsgroup-ephemeral-charset
9362               (and gnus-newsgroup-name
9363                    (or (gnus-group-find-parameter gnus-newsgroup-name
9364                                                   'charset)
9365                        (let ((alist gnus-group-charset-alist)
9366                              elem (charset nil))
9367                          (while (setq elem (pop alist))
9368                            (when (and name
9369                                       (string-match (car elem) name))
9370                              (setq alist nil
9371                                    charset (cadr elem))))
9372                          charset)))
9373               gnus-default-charset))
9374     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9375          ignored-charsets))))
9376
9377 ;;;
9378 ;;; Mime Commands
9379 ;;;
9380
9381 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9382   "Display the current article buffer fully MIME-buttonized.
9383 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9384 treated as multipart/mixed."
9385   (interactive "P")
9386   (require 'gnus-art)
9387   (let ((gnus-unbuttonized-mime-types nil)
9388         (gnus-mime-display-multipart-as-mixed show-all-parts))
9389     (gnus-summary-show-article)))
9390
9391 (defun gnus-summary-repair-multipart (article)
9392   "Add a Content-Type header to a multipart article without one."
9393   (interactive (list (gnus-summary-article-number)))
9394   (gnus-with-article article
9395     (message-narrow-to-head)
9396     (goto-char (point-max))
9397     (widen)
9398     (when (search-forward "\n--" nil t)
9399       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9400         (message-narrow-to-head)
9401         (message-remove-header "Mime-Version")
9402         (message-remove-header "Content-Type")
9403         (goto-char (point-max))
9404         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9405                         separator))
9406         (insert "Mime-Version: 1.0\n")
9407         (widen))))
9408   (let (gnus-mark-article-hook)
9409     (gnus-summary-select-article t t nil article)))
9410
9411 (defun gnus-summary-toggle-display-buttonized ()
9412   "Toggle the buttonizing of the article buffer."
9413   (interactive)
9414   (require 'gnus-art)
9415   (if (setq gnus-inhibit-mime-unbuttonizing
9416             (not gnus-inhibit-mime-unbuttonizing))
9417       (let ((gnus-unbuttonized-mime-types nil))
9418         (gnus-summary-show-article))
9419     (gnus-summary-show-article)))
9420
9421 ;;;
9422 ;;; with article
9423 ;;;
9424
9425 (defmacro gnus-with-article (article &rest forms)
9426   "Select ARTICLE and perform FORMS in the original article buffer.
9427 Then replace the article with the result."
9428   `(progn
9429      ;; We don't want the article to be marked as read.
9430      (let (gnus-mark-article-hook)
9431        (gnus-summary-select-article t t nil ,article))
9432      (set-buffer gnus-original-article-buffer)
9433      ,@forms
9434      (if (not (gnus-check-backend-function
9435                'request-replace-article (car gnus-article-current)))
9436          (gnus-message 5 "Read-only group; not replacing")
9437        (unless (gnus-request-replace-article
9438                 ,article (car gnus-article-current)
9439                 (current-buffer) t)
9440          (error "Couldn't replace article")))
9441      ;; The cache and backlog have to be flushed somewhat.
9442      (when gnus-keep-backlog
9443        (gnus-backlog-remove-article
9444         (car gnus-article-current) (cdr gnus-article-current)))
9445      (when gnus-use-cache
9446        (gnus-cache-update-article
9447         (car gnus-article-current) (cdr gnus-article-current)))))
9448
9449 (put 'gnus-with-article 'lisp-indent-function 1)
9450 (put 'gnus-with-article 'edebug-form-spec '(form body))
9451
9452 ;;;
9453 ;;; Generic summary marking commands
9454 ;;;
9455
9456 (defvar gnus-summary-marking-alist
9457   '((read gnus-del-mark "d")
9458     (unread gnus-unread-mark "u")
9459     (ticked gnus-ticked-mark "!")
9460     (dormant gnus-dormant-mark "?")
9461     (expirable gnus-expirable-mark "e"))
9462   "An alist of names/marks/keystrokes.")
9463
9464 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9465 (defvar gnus-summary-mark-map)
9466
9467 (defun gnus-summary-make-all-marking-commands ()
9468   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9469   (dolist (elem gnus-summary-marking-alist)
9470     (apply 'gnus-summary-make-marking-command elem)))
9471
9472 (defun gnus-summary-make-marking-command (name mark keystroke)
9473   (let ((map (make-sparse-keymap)))
9474     (define-key gnus-summary-generic-mark-map keystroke map)
9475     (dolist (lway `((next "next" next nil "n")
9476                     (next-unread "next unread" next t "N")
9477                     (prev "previous" prev nil "p")
9478                     (prev-unread "previous unread" prev t "P")
9479                     (nomove "" nil nil ,keystroke)))
9480       (let ((func (gnus-summary-make-marking-command-1
9481                    mark (car lway) lway name)))
9482         (setq func (eval func))
9483         (define-key map (nth 4 lway) func)))))
9484       
9485 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9486   `(defun ,(intern
9487             (format "gnus-summary-put-mark-as-%s%s"
9488                     name (if (eq way 'nomove)
9489                              ""
9490                            (concat "-" (symbol-name way)))))
9491      (n)
9492      ,(format
9493        "Mark the current article as %s%s.
9494 If N, the prefix, then repeat N times.
9495 If N is negative, move in reverse order.
9496 The difference between N and the actual number of articles marked is
9497 returned."
9498        name (cadr lway))
9499      (interactive "p")
9500      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9501     
9502 (defun gnus-summary-generic-mark (n mark move unread)
9503   "Mark N articles with MARK."
9504   (unless (eq major-mode 'gnus-summary-mode)
9505     (error "This command can only be used in the summary buffer"))
9506   (gnus-summary-show-thread)
9507   (let ((nummove
9508          (cond
9509           ((eq move 'next) 1)
9510           ((eq move 'prev) -1)
9511           (t 0))))
9512     (if (zerop nummove)
9513         (setq n 1)
9514       (when (< n 0)
9515         (setq n (abs n)
9516               nummove (* -1 nummove))))
9517     (while (and (> n 0)
9518                 (gnus-summary-mark-article nil mark)
9519                 (zerop (gnus-summary-next-subject nummove unread t)))
9520       (setq n (1- n)))
9521     (when (/= 0 n)
9522       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9523     (gnus-summary-recenter)
9524     (gnus-summary-position-point)
9525     (gnus-set-mode-line 'summary)
9526     n))
9527
9528 (gnus-summary-make-all-marking-commands)
9529
9530 (gnus-ems-redefine)
9531
9532 (provide 'gnus-sum)
9533
9534 (run-hooks 'gnus-sum-load-hook)
9535
9536 ;;; gnus-sum.el ends here