Sync up with pgnus-0.55
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, MIME
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mime-view)
39
40 ;; Avoid byte-compile warnings.
41 (eval-when-compile
42   (defvar gnus-article-decoded-p)
43   (defvar gnus-decode-encoded-word-function)
44   )
45
46 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
47 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
48
49 (defcustom gnus-kill-summary-on-exit t
50   "*If non-nil, kill the summary buffer when you exit from it.
51 If nil, the summary will become a \"*Dead Summary*\" buffer, and
52 it will be killed sometime later."
53   :group 'gnus-summary-exit
54   :type 'boolean)
55
56 (defcustom gnus-fetch-old-headers nil
57   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
58 If an unread article in the group refers to an older, already read (or
59 just marked as read) article, the old article will not normally be
60 displayed in the Summary buffer.  If this variable is non-nil, Gnus
61 will attempt to grab the headers to the old articles, and thereby
62 build complete threads.  If it has the value `some', only enough
63 headers to connect otherwise loose threads will be displayed.  This
64 variable can also be a number.  In that case, no more than that number
65 of old headers will be fetched.  If it has the value `invisible', all
66 old headers will be fetched, but none will be displayed.
67
68 The server has to support NOV for any of this to work."
69   :group 'gnus-thread
70   :type '(choice (const :tag "off" nil)
71                  (const some)
72                  number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-refer-thread-limit 200
76   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
77 If t, fetch all the available old headers."
78   :group 'gnus-thread
79   :type '(choice number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-summary-make-false-root 'adopt
83   "*nil means that Gnus won't gather loose threads.
84 If the root of a thread has expired or been read in a previous
85 session, the information necessary to build a complete thread has been
86 lost.  Instead of having many small sub-threads from this original thread
87 scattered all over the summary buffer, Gnus can gather them.
88
89 If non-nil, Gnus will try to gather all loose sub-threads from an
90 original thread into one large thread.
91
92 If this variable is non-nil, it should be one of `none', `adopt',
93 `dummy' or `empty'.
94
95 If this variable is `none', Gnus will not make a false root, but just
96 present the sub-threads after another.
97 If this variable is `dummy', Gnus will create a dummy root that will
98 have all the sub-threads as children.
99 If this variable is `adopt', Gnus will make one of the \"children\"
100 the parent and mark all the step-children as such.
101 If this variable is `empty', the \"children\" are printed with empty
102 subject fields.  (Or rather, they will be printed with a string
103 given by the `gnus-summary-same-subject' variable.)"
104   :group 'gnus-thread
105   :type '(choice (const :tag "off" nil)
106                  (const none)
107                  (const dummy)
108                  (const adopt)
109                  (const empty)))
110
111 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
112   "*A regexp to match subjects to be excluded from loose thread gathering.
113 As loose thread gathering is done on subjects only, that means that
114 there can be many false gatherings performed.  By rooting out certain
115 common subjects, gathering might become saner."
116   :group 'gnus-thread
117   :type 'regexp)
118
119 (defcustom gnus-summary-gather-subject-limit nil
120   "*Maximum length of subject comparisons when gathering loose threads.
121 Use nil to compare full subjects.  Setting this variable to a low
122 number will help gather threads that have been corrupted by
123 newsreaders chopping off subject lines, but it might also mean that
124 unrelated articles that have subject that happen to begin with the
125 same few characters will be incorrectly gathered.
126
127 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
128 comparing subjects."
129   :group 'gnus-thread
130   :type '(choice (const :tag "off" nil)
131                  (const fuzzy)
132                  (sexp :menu-tag "on" t)))
133
134 (defcustom gnus-simplify-subject-functions nil
135   "List of functions taking a string argument that simplify subjects.
136 The functions are applied recursively.
137
138 Useful functions to put in this list include: `gnus-simplify-subject-re',
139 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
140   :group 'gnus-thread
141   :type '(repeat function))
142
143 (defcustom gnus-simplify-ignored-prefixes nil
144   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  regexp))
148
149 (defcustom gnus-build-sparse-threads nil
150   "*If non-nil, fill in the gaps in threads.
151 If `some', only fill in the gaps that are needed to tie loose threads
152 together.  If `more', fill in all leaf nodes that Gnus can find.  If
153 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
154   :group 'gnus-thread
155   :type '(choice (const :tag "off" nil)
156                  (const some)
157                  (const more)
158                  (sexp :menu-tag "all" t)))
159
160 (defcustom gnus-summary-thread-gathering-function
161   'gnus-gather-threads-by-subject
162   "*Function used for gathering loose threads.
163 There are two pre-defined functions: `gnus-gather-threads-by-subject',
164 which only takes Subjects into consideration; and
165 `gnus-gather-threads-by-references', which compared the References
166 headers of the articles to find matches."
167   :group 'gnus-thread
168   :type '(radio (function-item gnus-gather-threads-by-subject)
169                 (function-item gnus-gather-threads-by-references)
170                 (function :tag "other")))
171
172 (defcustom gnus-summary-same-subject ""
173   "*String indicating that the current article has the same subject as the previous.
174 This variable will only be used if the value of
175 `gnus-summary-make-false-root' is `empty'."
176   :group 'gnus-summary-format
177   :type 'string)
178
179 (defcustom gnus-summary-goto-unread t
180   "*If t, marking commands will go to the next unread article.
181 If `never', commands that usually go to the next unread article, will
182 go to the next article, whether it is read or not.
183 If nil, only the marking commands will go to the next (un)read article."
184   :group 'gnus-summary-marks
185   :link '(custom-manual "(gnus)Setting Marks")
186   :type '(choice (const :tag "off" nil)
187                  (const never)
188                  (sexp :menu-tag "on" t)))
189
190 (defcustom gnus-summary-default-score 0
191   "*Default article score level.
192 All scores generated by the score files will be added to this score.
193 If this variable is nil, scoring will be disabled."
194   :group 'gnus-score-default
195   :type '(choice (const :tag "disable")
196                  integer))
197
198 (defcustom gnus-summary-zcore-fuzz 0
199   "*Fuzziness factor for the zcore in the summary buffer.
200 Articles with scores closer than this to `gnus-summary-default-score'
201 will not be marked."
202   :group 'gnus-summary-format
203   :type 'integer)
204
205 (defcustom gnus-simplify-subject-fuzzy-regexp nil
206   "*Strings to be removed when doing fuzzy matches.
207 This can either be a regular expression or list of regular expressions
208 that will be removed from subject strings if fuzzy subject
209 simplification is selected."
210   :group 'gnus-thread
211   :type '(repeat regexp))
212
213 (defcustom gnus-show-threads t
214   "*If non-nil, display threads in summary mode."
215   :group 'gnus-thread
216   :type 'boolean)
217
218 (defcustom gnus-thread-hide-subtree nil
219   "*If non-nil, hide all threads initially.
220 If threads are hidden, you have to run the command
221 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
222 to expose hidden threads."
223   :group 'gnus-thread
224   :type 'boolean)
225
226 (defcustom gnus-thread-hide-killed t
227   "*If non-nil, hide killed threads automatically."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-ignore-subject t
232   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
233 If nil, articles that have different subjects from their parents will
234 start separate threads."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-operation-ignore-subject t
239   "*If non-nil, subjects will be ignored when doing thread commands.
240 This affects commands like `gnus-summary-kill-thread' and
241 `gnus-summary-lower-thread'.
242
243 If this variable is nil, articles in the same thread with different
244 subjects will not be included in the operation in question.  If this
245 variable is `fuzzy', only articles that have subjects that are fuzzily
246 equal will be included."
247   :group 'gnus-thread
248   :type '(choice (const :tag "off" nil)
249                  (const fuzzy)
250                  (sexp :tag "on" t)))
251
252 (defcustom gnus-thread-indent-level 4
253   "*Number that says how much each sub-thread should be indented."
254   :group 'gnus-thread
255   :type 'integer)
256
257 (defcustom gnus-auto-extend-newsgroup t
258   "*If non-nil, extend newsgroup forward and backward when requested."
259   :group 'gnus-summary-choose
260   :type 'boolean)
261
262 (defcustom gnus-auto-select-first t
263   "*If nil, don't select the first unread article when entering a group.
264 If this variable is `best', select the highest-scored unread article
265 in the group.  If t, select the first unread article.
266
267 This variable can also be a function to place point on a likely
268 subject line.  Useful values include `gnus-summary-first-unread-subject', 
269 `gnus-summary-first-unread-article' and 
270 `gnus-summary-best-unread-article'.
271
272 If you want to prevent automatic selection of the first unread article
273 in some newsgroups, set the variable to nil in
274 `gnus-select-group-hook'."
275   :group 'gnus-group-select
276   :type '(choice (const :tag "none" nil)
277                  (const best)
278                  (sexp :menu-tag "first" t)
279                  (function-item gnus-summary-first-unread-subject)
280                  (function-item gnus-summary-first-unread-article)
281                  (function-item gnus-summary-best-unread-article)))
282
283 (defcustom gnus-auto-select-next t
284   "*If non-nil, offer to go to the next group from the end of the previous.
285 If the value is t and the next newsgroup is empty, Gnus will exit
286 summary mode and go back to group mode.  If the value is neither nil
287 nor t, Gnus will select the following unread newsgroup.  In
288 particular, if the value is the symbol `quietly', the next unread
289 newsgroup will be selected without any confirmation, and if it is
290 `almost-quietly', the next group will be selected without any
291 confirmation if you are located on the last article in the group.
292 Finally, if this variable is `slightly-quietly', the `Z n' command
293 will go to the next group without confirmation."
294   :group 'gnus-summary-maneuvering
295   :type '(choice (const :tag "off" nil)
296                  (const quietly)
297                  (const almost-quietly)
298                  (const slightly-quietly)
299                  (sexp :menu-tag "on" t)))
300
301 (defcustom gnus-auto-select-same nil
302   "*If non-nil, select the next article with the same subject.
303 If there are no more articles with the same subject, go to
304 the first unread article."
305   :group 'gnus-summary-maneuvering
306   :type 'boolean)
307
308 (defcustom gnus-summary-check-current nil
309   "*If non-nil, consider the current article when moving.
310 The \"unread\" movement commands will stay on the same line if the
311 current article is unread."
312   :group 'gnus-summary-maneuvering
313   :type 'boolean)
314
315 (defcustom gnus-auto-center-summary t
316   "*If non-nil, always center the current summary buffer.
317 In particular, if `vertical' do only vertical recentering.  If non-nil
318 and non-`vertical', do both horizontal and vertical recentering."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "none" nil)
321                  (const vertical)
322                  (integer :tag "height")
323                  (sexp :menu-tag "both" t)))
324
325 (defcustom gnus-show-all-headers nil
326   "*If non-nil, don't hide any headers."
327   :group 'gnus-article-hiding
328   :group 'gnus-article-headers
329   :type 'boolean)
330
331 (defcustom gnus-summary-ignore-duplicates nil
332   "*If non-nil, ignore articles with identical Message-ID headers."
333   :group 'gnus-summary
334   :type 'boolean)
335
336 (defcustom gnus-single-article-buffer t
337   "*If non-nil, display all articles in the same buffer.
338 If nil, each group will get its own article buffer."
339   :group 'gnus-article-various
340   :type 'boolean)
341
342 (defcustom gnus-break-pages t
343   "*If non-nil, do page breaking on articles.
344 The page delimiter is specified by the `gnus-page-delimiter'
345 variable."
346   :group 'gnus-article-various
347   :type 'boolean)
348
349 (defcustom gnus-show-mime t
350   "*If non-nil, do mime processing of articles.
351 The articles will simply be fed to the function given by
352 `gnus-article-display-method-for-mime'."
353   :group 'gnus-article-mime
354   :type 'boolean)
355
356 (defcustom gnus-move-split-methods nil
357   "*Variable used to suggest where articles are to be moved to.
358 It uses the same syntax as the `gnus-split-methods' variable."
359   :group 'gnus-summary-mail
360   :type '(repeat (choice (list :value (fun) function)
361                          (cons :value ("" "") regexp (repeat string))
362                          (sexp :value nil))))
363
364 (defcustom gnus-unread-mark ? 
365   "*Mark used for unread articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-ticked-mark ?!
370   "*Mark used for ticked articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-dormant-mark ??
375   "*Mark used for dormant articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-del-mark ?r
380   "*Mark used for del'd articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-read-mark ?R
385   "*Mark used for read articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-expirable-mark ?E
390   "*Mark used for expirable articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-killed-mark ?K
395   "*Mark used for killed articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-souped-mark ?F
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-kill-file-mark ?X
405   "*Mark used for articles killed by kill files."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-low-score-mark ?Y
410   "*Mark used for articles with a low score."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-catchup-mark ?C
415   "*Mark used for articles that are caught up."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-replied-mark ?A
420   "*Mark used for articles that have been replied to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-cached-mark ?*
425   "*Mark used for articles that are in the cache."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-saved-mark ?S
430   "*Mark used for articles that have been saved to."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-ancient-mark ?O
435   "*Mark used for ancient articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-sparse-mark ?Q
440   "*Mark used for sparsely reffed articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-canceled-mark ?G
445   "*Mark used for canceled articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-duplicate-mark ?M
450   "*Mark used for duplicate articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-undownloaded-mark ?@
455   "*Mark used for articles that weren't downloaded."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-downloadable-mark ?%
460   "*Mark used for articles that are to be downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-unsendable-mark ?=
465   "*Mark used for articles that won't be sent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-score-over-mark ?+
470   "*Score mark used for articles with high scores."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-below-mark ?-
475   "*Score mark used for articles with low scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-empty-thread-mark ? 
480   "*There is no thread under the article."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-not-empty-thread-mark ?=
485   "*There is a thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-view-pseudo-asynchronously nil
490   "*If non-nil, Gnus will view pseudo-articles asynchronously."
491   :group 'gnus-extract-view
492   :type 'boolean)
493
494 (defcustom gnus-auto-expirable-marks
495   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
496         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
497         gnus-souped-mark gnus-duplicate-mark)
498   "*The list of marks converted into expiration if a group is auto-expirable."
499   :group 'gnus-summary
500   :type '(repeat character))
501
502 (defcustom gnus-inhibit-user-auto-expire t
503   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
504   :group 'gnus-summary
505   :type 'boolean)
506
507 (defcustom gnus-view-pseudos nil
508   "*If `automatic', pseudo-articles will be viewed automatically.
509 If `not-confirm', pseudos will be viewed automatically, and the user
510 will not be asked to confirm the command."
511   :group 'gnus-extract-view
512   :type '(choice (const :tag "off" nil)
513                  (const automatic)
514                  (const not-confirm)))
515
516 (defcustom gnus-view-pseudos-separately t
517   "*If non-nil, one pseudo-article will be created for each file to be viewed.
518 If nil, all files that use the same viewing command will be given as a
519 list of parameters to that command."
520   :group 'gnus-extract-view
521   :type 'boolean)
522
523 (defcustom gnus-insert-pseudo-articles t
524   "*If non-nil, insert pseudo-articles when decoding articles."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-summary-dummy-line-format
529   "  %(:                          :%) %S\n"
530   "*The format specification for the dummy roots in the summary buffer.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions.
533
534 %S  The subject"
535   :group 'gnus-threading
536   :type 'string)
537
538 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
539   "*The format specification for the summary mode line.
540 It works along the same lines as a normal formatting string,
541 with some simple extensions:
542
543 %G  Group name
544 %p  Unprefixed group name
545 %A  Current article number
546 %z  Current article score
547 %V  Gnus version
548 %U  Number of unread articles in the group
549 %e  Number of unselected articles in the group
550 %Z  A string with unread/unselected article counts
551 %g  Shortish group name
552 %S  Subject of the current article
553 %u  User-defined spec
554 %s  Current score file name
555 %d  Number of dormant articles
556 %r  Number of articles that have been marked as read in this session
557 %E  Number of articles expunged by the score files"
558   :group 'gnus-summary-format
559   :type 'string)
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-treading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting (not quitting) summary mode."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-newsgroup-default-charset-alist 
814   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" . cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" . cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" . iso-2022-jp-2)
817     ("^relcom\\>" . koi8-r))
818   "Alist of Regexps (to match group names) and default charsets to be applied."
819   :type '(repeat (cons (regexp :tag "Group")
820                        (symbol :tag "Charset")))
821   :group 'gnus)
822
823 (defcustom gnus-newsgroup-iso-8859-1-forced-regexp 
824   "^tw\\>\\|^hk\\>\\|^cn\\>\\|\\<chinese\\>"
825   "Regexp of newsgroup in which ISO-8859-1 is forced to other charset."
826   :type 'regexp
827   :group 'gnus)
828
829 ;;; Internal variables
830
831 (defvar gnus-scores-exclude-files nil)
832 (defvar gnus-page-broken nil)
833 (defvar gnus-inhibit-mime-unbuttonizing nil)
834
835 (defvar gnus-original-article nil)
836 (defvar gnus-article-internal-prepare-hook nil)
837 (defvar gnus-newsgroup-process-stack nil)
838
839 (defvar gnus-thread-indent-array nil)
840 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
841
842 ;; Avoid highlighting in kill files.
843 (defvar gnus-summary-inhibit-highlight nil)
844 (defvar gnus-newsgroup-selected-overlay nil)
845 (defvar gnus-inhibit-limiting nil)
846 (defvar gnus-newsgroup-adaptive-score-file nil)
847 (defvar gnus-current-score-file nil)
848 (defvar gnus-current-move-group nil)
849 (defvar gnus-current-copy-group nil)
850 (defvar gnus-current-crosspost-group nil)
851
852 (defvar gnus-newsgroup-dependencies nil)
853 (defvar gnus-newsgroup-adaptive nil)
854 (defvar gnus-summary-display-article-function nil)
855 (defvar gnus-summary-highlight-line-function nil
856   "Function called after highlighting a summary line.")
857
858 (defvar gnus-summary-line-format-alist
859   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
860     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
861     (?s gnus-tmp-subject-or-nil ?s)
862     (?n gnus-tmp-name ?s)
863     (?A (std11-address-string
864          (car (mime-read-field 'From gnus-tmp-header))) ?s)
865     (?a (or (std11-full-name-string
866              (car (mime-read-field 'From gnus-tmp-header)))
867             gnus-tmp-from) ?s)
868     (?F gnus-tmp-from ?s)
869     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
870     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
871     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
872     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
873     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
874     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
875     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
876     (?L gnus-tmp-lines ?d)
877     (?I gnus-tmp-indentation ?s)
878     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
879     (?R gnus-tmp-replied ?c)
880     (?\[ gnus-tmp-opening-bracket ?c)
881     (?\] gnus-tmp-closing-bracket ?c)
882     (?\> (make-string gnus-tmp-level ? ) ?s)
883     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
884     (?i gnus-tmp-score ?d)
885     (?z gnus-tmp-score-char ?c)
886     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
887     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
888     (?U gnus-tmp-unread ?c)
889     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
890     (?t (gnus-summary-number-of-articles-in-thread
891          (and (boundp 'thread) (car thread)) gnus-tmp-level)
892         ?d)
893     (?e (gnus-summary-number-of-articles-in-thread
894          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
895         ?c)
896     (?u gnus-tmp-user-defined ?s)
897     (?P (gnus-pick-line-number) ?d))
898   "An alist of format specifications that can appear in summary lines,
899 and what variables they correspond with, along with the type of the
900 variable (string, integer, character, etc).")
901
902 (defvar gnus-summary-dummy-line-format-alist
903   `((?S gnus-tmp-subject ?s)
904     (?N gnus-tmp-number ?d)
905     (?u gnus-tmp-user-defined ?s)))
906
907 (defvar gnus-summary-mode-line-format-alist
908   `((?G gnus-tmp-group-name ?s)
909     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
910     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
911     (?A gnus-tmp-article-number ?d)
912     (?Z gnus-tmp-unread-and-unselected ?s)
913     (?V gnus-version ?s)
914     (?U gnus-tmp-unread-and-unticked ?d)
915     (?S gnus-tmp-subject ?s)
916     (?e gnus-tmp-unselected ?d)
917     (?u gnus-tmp-user-defined ?s)
918     (?d (length gnus-newsgroup-dormant) ?d)
919     (?t (length gnus-newsgroup-marked) ?d)
920     (?r (length gnus-newsgroup-reads) ?d)
921     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
922     (?E gnus-newsgroup-expunged-tally ?d)
923     (?s (gnus-current-score-file-nondirectory) ?s)))
924
925 (defvar gnus-last-search-regexp nil
926   "Default regexp for article search command.")
927
928 (defvar gnus-last-shell-command nil
929   "Default shell command on article.")
930
931 (defvar gnus-newsgroup-begin nil)
932 (defvar gnus-newsgroup-end nil)
933 (defvar gnus-newsgroup-last-rmail nil)
934 (defvar gnus-newsgroup-last-mail nil)
935 (defvar gnus-newsgroup-last-folder nil)
936 (defvar gnus-newsgroup-last-file nil)
937 (defvar gnus-newsgroup-auto-expire nil)
938 (defvar gnus-newsgroup-active nil)
939
940 (defvar gnus-newsgroup-data nil)
941 (defvar gnus-newsgroup-data-reverse nil)
942 (defvar gnus-newsgroup-limit nil)
943 (defvar gnus-newsgroup-limits nil)
944
945 (defvar gnus-newsgroup-unreads nil
946   "List of unread articles in the current newsgroup.")
947
948 (defvar gnus-newsgroup-unselected nil
949   "List of unselected unread articles in the current newsgroup.")
950
951 (defvar gnus-newsgroup-reads nil
952   "Alist of read articles and article marks in the current newsgroup.")
953
954 (defvar gnus-newsgroup-expunged-tally nil)
955
956 (defvar gnus-newsgroup-marked nil
957   "List of ticked articles in the current newsgroup (a subset of unread art).")
958
959 (defvar gnus-newsgroup-killed nil
960   "List of ranges of articles that have been through the scoring process.")
961
962 (defvar gnus-newsgroup-cached nil
963   "List of articles that come from the article cache.")
964
965 (defvar gnus-newsgroup-saved nil
966   "List of articles that have been saved.")
967
968 (defvar gnus-newsgroup-kill-headers nil)
969
970 (defvar gnus-newsgroup-replied nil
971   "List of articles that have been replied to in the current newsgroup.")
972
973 (defvar gnus-newsgroup-expirable nil
974   "List of articles in the current newsgroup that can be expired.")
975
976 (defvar gnus-newsgroup-processable nil
977   "List of articles in the current newsgroup that can be processed.")
978
979 (defvar gnus-newsgroup-downloadable nil
980   "List of articles in the current newsgroup that can be processed.")
981
982 (defvar gnus-newsgroup-undownloaded nil
983   "List of articles in the current newsgroup that haven't been downloaded..")
984
985 (defvar gnus-newsgroup-unsendable nil
986   "List of articles in the current newsgroup that won't be sent.")
987
988 (defvar gnus-newsgroup-bookmarks nil
989   "List of articles in the current newsgroup that have bookmarks.")
990
991 (defvar gnus-newsgroup-dormant nil
992   "List of dormant articles in the current newsgroup.")
993
994 (defvar gnus-newsgroup-scored nil
995   "List of scored articles in the current newsgroup.")
996
997 (defvar gnus-newsgroup-headers nil
998   "List of article headers in the current newsgroup.")
999
1000 (defvar gnus-newsgroup-threads nil)
1001
1002 (defvar gnus-newsgroup-prepared nil
1003   "Whether the current group has been prepared properly.")
1004
1005 (defvar gnus-newsgroup-ancient nil
1006   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-sparse nil)
1009
1010 (defvar gnus-current-article nil)
1011 (defvar gnus-article-current nil)
1012 (defvar gnus-current-headers nil)
1013 (defvar gnus-have-all-headers nil)
1014 (defvar gnus-last-article nil)
1015 (defvar gnus-newsgroup-history nil)
1016
1017 (defvar gnus-newsgroup-default-charset nil)
1018 (defvar gnus-newsgroup-iso-8859-1-forced nil)
1019
1020 (defconst gnus-summary-local-variables
1021   '(gnus-newsgroup-name
1022     gnus-newsgroup-begin gnus-newsgroup-end
1023     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1024     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1025     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1026     gnus-newsgroup-unselected gnus-newsgroup-marked
1027     gnus-newsgroup-reads gnus-newsgroup-saved
1028     gnus-newsgroup-replied gnus-newsgroup-expirable
1029     gnus-newsgroup-processable gnus-newsgroup-killed
1030     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1031     gnus-newsgroup-unsendable
1032     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1033     gnus-newsgroup-headers gnus-newsgroup-threads
1034     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1035     gnus-current-article gnus-current-headers gnus-have-all-headers
1036     gnus-last-article gnus-article-internal-prepare-hook
1037     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1038     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1039     gnus-thread-expunge-below
1040     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1041     (gnus-summary-mark-below . global)
1042     gnus-newsgroup-active gnus-scores-exclude-files
1043     gnus-newsgroup-history gnus-newsgroup-ancient
1044     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1045     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1046     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1047     (gnus-newsgroup-expunged-tally . 0)
1048     gnus-cache-removable-articles gnus-newsgroup-cached
1049     gnus-newsgroup-data gnus-newsgroup-data-reverse
1050     gnus-newsgroup-limit gnus-newsgroup-limits
1051     gnus-newsgroup-default-charset gnus-newsgroup-iso-8859-1-forced)
1052   "Variables that are buffer-local to the summary buffers.")
1053
1054 ;; Byte-compiler warning.
1055 (defvar gnus-article-mode-map)
1056
1057 ;; Subject simplification.
1058
1059 (defun gnus-simplify-whitespace (str)
1060   "Remove excessive whitespace."
1061   (let ((mystr str))
1062     ;; Multiple spaces.
1063     (while (string-match "[ \t][ \t]+" mystr)
1064       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1065                           " "
1066                           (substring mystr (match-end 0)))))
1067     ;; Leading spaces.
1068     (when (string-match "^[ \t]+" mystr)
1069       (setq mystr (substring mystr (match-end 0))))
1070     ;; Trailing spaces.
1071     (when (string-match "[ \t]+$" mystr)
1072       (setq mystr (substring mystr 0 (match-beginning 0))))
1073     mystr))
1074
1075 (defsubst gnus-simplify-subject-re (subject)
1076   "Remove \"Re:\" from subject lines."
1077   (if (string-match "^[Rr][Ee]: *" subject)
1078       (substring subject (match-end 0))
1079     subject))
1080
1081 (defun gnus-simplify-subject (subject &optional re-only)
1082   "Remove `Re:' and words in parentheses.
1083 If RE-ONLY is non-nil, strip leading `Re:'s only."
1084   (let ((case-fold-search t))           ;Ignore case.
1085     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1086     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1087       (setq subject (substring subject (match-end 0))))
1088     ;; Remove uninteresting prefixes.
1089     (when (and (not re-only)
1090                gnus-simplify-ignored-prefixes
1091                (string-match gnus-simplify-ignored-prefixes subject))
1092       (setq subject (substring subject (match-end 0))))
1093     ;; Remove words in parentheses from end.
1094     (unless re-only
1095       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1096         (setq subject (substring subject 0 (match-beginning 0)))))
1097     ;; Return subject string.
1098     subject))
1099
1100 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1101 ;; all whitespace.
1102 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1103   (goto-char (point-min))
1104   (while (re-search-forward regexp nil t)
1105       (replace-match (or newtext ""))))
1106
1107 (defun gnus-simplify-buffer-fuzzy ()
1108   "Simplify string in the buffer fuzzily.
1109 The string in the accessible portion of the current buffer is simplified.
1110 It is assumed to be a single-line subject.
1111 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1112 matter is removed.  Additional things can be deleted by setting
1113 gnus-simplify-subject-fuzzy-regexp."
1114   (let ((case-fold-search t)
1115         (modified-tick))
1116     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1117
1118     (while (not (eq modified-tick (buffer-modified-tick)))
1119       (setq modified-tick (buffer-modified-tick))
1120       (cond
1121        ((listp gnus-simplify-subject-fuzzy-regexp)
1122         (mapcar 'gnus-simplify-buffer-fuzzy-step
1123                 gnus-simplify-subject-fuzzy-regexp))
1124        (gnus-simplify-subject-fuzzy-regexp
1125         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1126       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1127       (gnus-simplify-buffer-fuzzy-step
1128        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1129       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1130
1131     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1132     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1133     (gnus-simplify-buffer-fuzzy-step " $")
1134     (gnus-simplify-buffer-fuzzy-step "^ +")))
1135
1136 (defun gnus-simplify-subject-fuzzy (subject)
1137   "Simplify a subject string fuzzily.
1138 See `gnus-simplify-buffer-fuzzy' for details."
1139   (save-excursion
1140     (gnus-set-work-buffer)
1141     (let ((case-fold-search t))
1142       ;; Remove uninteresting prefixes.
1143       (when (and gnus-simplify-ignored-prefixes
1144                  (string-match gnus-simplify-ignored-prefixes subject))
1145         (setq subject (substring subject (match-end 0))))
1146       (insert subject)
1147       (inline (gnus-simplify-buffer-fuzzy))
1148       (buffer-string))))
1149
1150 (defsubst gnus-simplify-subject-fully (subject)
1151   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1152   (cond
1153    (gnus-simplify-subject-functions
1154     (gnus-map-function gnus-simplify-subject-functions subject))
1155    ((null gnus-summary-gather-subject-limit)
1156     (gnus-simplify-subject-re subject))
1157    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1158     (gnus-simplify-subject-fuzzy subject))
1159    ((numberp gnus-summary-gather-subject-limit)
1160     (gnus-limit-string (gnus-simplify-subject-re subject)
1161                        gnus-summary-gather-subject-limit))
1162    (t
1163     subject)))
1164
1165 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1166   "Check whether two subjects are equal.
1167 If optional argument simple-first is t, first argument is already
1168 simplified."
1169   (cond
1170    ((null simple-first)
1171     (equal (gnus-simplify-subject-fully s1)
1172            (gnus-simplify-subject-fully s2)))
1173    (t
1174     (equal s1
1175            (gnus-simplify-subject-fully s2)))))
1176
1177 (defun gnus-summary-bubble-group ()
1178   "Increase the score of the current group.
1179 This is a handy function to add to `gnus-summary-exit-hook' to
1180 increase the score of each group you read."
1181   (gnus-group-add-score gnus-newsgroup-name))
1182
1183 \f
1184 ;;;
1185 ;;; Gnus summary mode
1186 ;;;
1187
1188 (put 'gnus-summary-mode 'mode-class 'special)
1189
1190 (when t
1191   ;; Non-orthogonal keys
1192
1193   (gnus-define-keys gnus-summary-mode-map
1194     " " gnus-summary-next-page
1195     "\177" gnus-summary-prev-page
1196     [delete] gnus-summary-prev-page
1197     [backspace] gnus-summary-prev-page
1198     "\r" gnus-summary-scroll-up
1199     "\M-\r" gnus-summary-scroll-down
1200     "n" gnus-summary-next-unread-article
1201     "p" gnus-summary-prev-unread-article
1202     "N" gnus-summary-next-article
1203     "P" gnus-summary-prev-article
1204     "\M-\C-n" gnus-summary-next-same-subject
1205     "\M-\C-p" gnus-summary-prev-same-subject
1206     "\M-n" gnus-summary-next-unread-subject
1207     "\M-p" gnus-summary-prev-unread-subject
1208     "." gnus-summary-first-unread-article
1209     "," gnus-summary-best-unread-article
1210     "\M-s" gnus-summary-search-article-forward
1211     "\M-r" gnus-summary-search-article-backward
1212     "<" gnus-summary-beginning-of-article
1213     ">" gnus-summary-end-of-article
1214     "j" gnus-summary-goto-article
1215     "^" gnus-summary-refer-parent-article
1216     "\M-^" gnus-summary-refer-article
1217     "u" gnus-summary-tick-article-forward
1218     "!" gnus-summary-tick-article-forward
1219     "U" gnus-summary-tick-article-backward
1220     "d" gnus-summary-mark-as-read-forward
1221     "D" gnus-summary-mark-as-read-backward
1222     "E" gnus-summary-mark-as-expirable
1223     "\M-u" gnus-summary-clear-mark-forward
1224     "\M-U" gnus-summary-clear-mark-backward
1225     "k" gnus-summary-kill-same-subject-and-select
1226     "\C-k" gnus-summary-kill-same-subject
1227     "\M-\C-k" gnus-summary-kill-thread
1228     "\M-\C-l" gnus-summary-lower-thread
1229     "e" gnus-summary-edit-article
1230     "#" gnus-summary-mark-as-processable
1231     "\M-#" gnus-summary-unmark-as-processable
1232     "\M-\C-t" gnus-summary-toggle-threads
1233     "\M-\C-s" gnus-summary-show-thread
1234     "\M-\C-h" gnus-summary-hide-thread
1235     "\M-\C-f" gnus-summary-next-thread
1236     "\M-\C-b" gnus-summary-prev-thread
1237     "\M-\C-u" gnus-summary-up-thread
1238     "\M-\C-d" gnus-summary-down-thread
1239     "&" gnus-summary-execute-command
1240     "c" gnus-summary-catchup-and-exit
1241     "\C-w" gnus-summary-mark-region-as-read
1242     "\C-t" gnus-summary-toggle-truncation
1243     "?" gnus-summary-mark-as-dormant
1244     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1245     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1246     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1247     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1248     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1249     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1250     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1251     "=" gnus-summary-expand-window
1252     "\C-x\C-s" gnus-summary-reselect-current-group
1253     "\M-g" gnus-summary-rescan-group
1254     "w" gnus-summary-stop-page-breaking
1255     "\C-c\C-r" gnus-summary-caesar-message
1256     "\M-t" gnus-summary-toggle-mime
1257     "f" gnus-summary-followup
1258     "F" gnus-summary-followup-with-original
1259     "C" gnus-summary-cancel-article
1260     "r" gnus-summary-reply
1261     "R" gnus-summary-reply-with-original
1262     "\C-c\C-f" gnus-summary-mail-forward
1263     "o" gnus-summary-save-article
1264     "\C-o" gnus-summary-save-article-mail
1265     "|" gnus-summary-pipe-output
1266     "\M-k" gnus-summary-edit-local-kill
1267     "\M-K" gnus-summary-edit-global-kill
1268     ;; "V" gnus-version
1269     "\C-c\C-d" gnus-summary-describe-group
1270     "q" gnus-summary-exit
1271     "Q" gnus-summary-exit-no-update
1272     "\C-c\C-i" gnus-info-find-node
1273     gnus-mouse-2 gnus-mouse-pick-article
1274     "m" gnus-summary-mail-other-window
1275     "a" gnus-summary-post-news
1276     "x" gnus-summary-limit-to-unread
1277     "s" gnus-summary-isearch-article
1278     "t" gnus-article-hide-headers
1279     "g" gnus-summary-show-article
1280     "l" gnus-summary-goto-last-article
1281     "v" gnus-summary-preview-mime-message
1282     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1283     "\C-d" gnus-summary-enter-digest-group
1284     "\M-\C-d" gnus-summary-read-document
1285     "\M-\C-e" gnus-summary-edit-parameters
1286     "\M-\C-g" gnus-summary-customize-parameters
1287     "\C-c\C-b" gnus-bug
1288     "*" gnus-cache-enter-article
1289     "\M-*" gnus-cache-remove-article
1290     "\M-&" gnus-summary-universal-argument
1291     "\C-l" gnus-recenter
1292     "I" gnus-summary-increase-score
1293     "L" gnus-summary-lower-score
1294     "\M-i" gnus-symbolic-argument
1295     "h" gnus-summary-select-article-buffer
1296     
1297     "V" gnus-summary-score-map
1298     "X" gnus-uu-extract-map
1299     "S" gnus-summary-send-map)
1300
1301   ;; Sort of orthogonal keymap
1302   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1303     "t" gnus-summary-tick-article-forward
1304     "!" gnus-summary-tick-article-forward
1305     "d" gnus-summary-mark-as-read-forward
1306     "r" gnus-summary-mark-as-read-forward
1307     "c" gnus-summary-clear-mark-forward
1308     " " gnus-summary-clear-mark-forward
1309     "e" gnus-summary-mark-as-expirable
1310     "x" gnus-summary-mark-as-expirable
1311     "?" gnus-summary-mark-as-dormant
1312     "b" gnus-summary-set-bookmark
1313     "B" gnus-summary-remove-bookmark
1314     "#" gnus-summary-mark-as-processable
1315     "\M-#" gnus-summary-unmark-as-processable
1316     "S" gnus-summary-limit-include-expunged
1317     "C" gnus-summary-catchup
1318     "H" gnus-summary-catchup-to-here
1319     "\C-c" gnus-summary-catchup-all
1320     "k" gnus-summary-kill-same-subject-and-select
1321     "K" gnus-summary-kill-same-subject
1322     "P" gnus-uu-mark-map)
1323
1324   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1325     "c" gnus-summary-clear-above
1326     "u" gnus-summary-tick-above
1327     "m" gnus-summary-mark-above
1328     "k" gnus-summary-kill-below)
1329
1330   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1331     "/" gnus-summary-limit-to-subject
1332     "n" gnus-summary-limit-to-articles
1333     "w" gnus-summary-pop-limit
1334     "s" gnus-summary-limit-to-subject
1335     "a" gnus-summary-limit-to-author
1336     "u" gnus-summary-limit-to-unread
1337     "m" gnus-summary-limit-to-marks
1338     "v" gnus-summary-limit-to-score
1339     "*" gnus-summary-limit-include-cached
1340     "D" gnus-summary-limit-include-dormant
1341     "T" gnus-summary-limit-include-thread
1342     "d" gnus-summary-limit-exclude-dormant
1343     "t" gnus-summary-limit-to-age
1344     "E" gnus-summary-limit-include-expunged
1345     "c" gnus-summary-limit-exclude-childless-dormant
1346     "C" gnus-summary-limit-mark-excluded-as-read)
1347
1348   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1349     "n" gnus-summary-next-unread-article
1350     "p" gnus-summary-prev-unread-article
1351     "N" gnus-summary-next-article
1352     "P" gnus-summary-prev-article
1353     "\C-n" gnus-summary-next-same-subject
1354     "\C-p" gnus-summary-prev-same-subject
1355     "\M-n" gnus-summary-next-unread-subject
1356     "\M-p" gnus-summary-prev-unread-subject
1357     "f" gnus-summary-first-unread-article
1358     "b" gnus-summary-best-unread-article
1359     "j" gnus-summary-goto-article
1360     "g" gnus-summary-goto-subject
1361     "l" gnus-summary-goto-last-article
1362     "o" gnus-summary-pop-article)
1363
1364   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1365     "k" gnus-summary-kill-thread
1366     "l" gnus-summary-lower-thread
1367     "i" gnus-summary-raise-thread
1368     "T" gnus-summary-toggle-threads
1369     "t" gnus-summary-rethread-current
1370     "^" gnus-summary-reparent-thread
1371     "s" gnus-summary-show-thread
1372     "S" gnus-summary-show-all-threads
1373     "h" gnus-summary-hide-thread
1374     "H" gnus-summary-hide-all-threads
1375     "n" gnus-summary-next-thread
1376     "p" gnus-summary-prev-thread
1377     "u" gnus-summary-up-thread
1378     "o" gnus-summary-top-thread
1379     "d" gnus-summary-down-thread
1380     "#" gnus-uu-mark-thread
1381     "\M-#" gnus-uu-unmark-thread)
1382
1383   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1384     "g" gnus-summary-prepare
1385     "c" gnus-summary-insert-cached-articles)
1386
1387   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1388     "c" gnus-summary-catchup-and-exit
1389     "C" gnus-summary-catchup-all-and-exit
1390     "E" gnus-summary-exit-no-update
1391     "Q" gnus-summary-exit
1392     "Z" gnus-summary-exit
1393     "n" gnus-summary-catchup-and-goto-next-group
1394     "R" gnus-summary-reselect-current-group
1395     "G" gnus-summary-rescan-group
1396     "N" gnus-summary-next-group
1397     "s" gnus-summary-save-newsrc
1398     "P" gnus-summary-prev-group)
1399
1400   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1401     " " gnus-summary-next-page
1402     "n" gnus-summary-next-page
1403     "\177" gnus-summary-prev-page
1404     [delete] gnus-summary-prev-page
1405     "p" gnus-summary-prev-page
1406     "\r" gnus-summary-scroll-up
1407     "\M-\r" gnus-summary-scroll-down
1408     "<" gnus-summary-beginning-of-article
1409     ">" gnus-summary-end-of-article
1410     "b" gnus-summary-beginning-of-article
1411     "e" gnus-summary-end-of-article
1412     "^" gnus-summary-refer-parent-article
1413     "r" gnus-summary-refer-parent-article
1414     "R" gnus-summary-refer-references
1415     "T" gnus-summary-refer-thread
1416     "g" gnus-summary-show-article
1417     "s" gnus-summary-isearch-article
1418     "P" gnus-summary-print-article)
1419
1420   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1421     "b" gnus-article-add-buttons
1422     "B" gnus-article-add-buttons-to-head
1423     "o" gnus-article-treat-overstrike
1424     "e" gnus-article-emphasize
1425     "w" gnus-article-fill-cited-article
1426     "c" gnus-article-remove-cr
1427     "f" gnus-article-display-x-face
1428     "l" gnus-summary-stop-page-breaking
1429     "r" gnus-summary-caesar-message
1430     "t" gnus-article-hide-headers
1431     "v" gnus-summary-verbose-headers
1432     "m" gnus-summary-toggle-mime
1433     "h" gnus-article-treat-html
1434     "d" gnus-article-treat-dumbquotes)
1435
1436   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1437     "a" gnus-article-hide
1438     "h" gnus-article-hide-headers
1439     "b" gnus-article-hide-boring-headers
1440     "s" gnus-article-hide-signature
1441     "c" gnus-article-hide-citation
1442     "C" gnus-article-hide-citation-in-followups
1443     "p" gnus-article-hide-pgp
1444     "P" gnus-article-hide-pem
1445     "\C-c" gnus-article-hide-citation-maybe)
1446
1447   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1448     "a" gnus-article-highlight
1449     "h" gnus-article-highlight-headers
1450     "c" gnus-article-highlight-citation
1451     "s" gnus-article-highlight-signature)
1452
1453   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1454     "z" gnus-article-date-ut
1455     "u" gnus-article-date-ut
1456     "l" gnus-article-date-local
1457     "e" gnus-article-date-lapsed
1458     "o" gnus-article-date-original
1459     "i" gnus-article-date-iso8601
1460     "s" gnus-article-date-user)
1461
1462   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1463     "t" gnus-article-remove-trailing-blank-lines
1464     "l" gnus-article-strip-leading-blank-lines
1465     "m" gnus-article-strip-multiple-blank-lines
1466     "a" gnus-article-strip-blank-lines
1467     "A" gnus-article-strip-all-blank-lines
1468     "s" gnus-article-strip-leading-space
1469     "e" gnus-article-strip-trailing-space)
1470
1471   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1472     "v" gnus-version
1473     "f" gnus-summary-fetch-faq
1474     "d" gnus-summary-describe-group
1475     "h" gnus-summary-describe-briefly
1476     "i" gnus-info-find-node)
1477
1478   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1479     "e" gnus-summary-expire-articles
1480     "\M-\C-e" gnus-summary-expire-articles-now
1481     "\177" gnus-summary-delete-article
1482     [delete] gnus-summary-delete-article
1483     "m" gnus-summary-move-article
1484     "r" gnus-summary-respool-article
1485     "w" gnus-summary-edit-article
1486     "c" gnus-summary-copy-article
1487     "B" gnus-summary-crosspost-article
1488     "q" gnus-summary-respool-query
1489     "t" gnus-summary-respool-trace
1490     "i" gnus-summary-import-article
1491     "p" gnus-summary-article-posted-p)
1492
1493   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1494     "o" gnus-summary-save-article
1495     "m" gnus-summary-save-article-mail
1496     "F" gnus-summary-write-article-file
1497     "r" gnus-summary-save-article-rmail
1498     "f" gnus-summary-save-article-file
1499     "b" gnus-summary-save-article-body-file
1500     "h" gnus-summary-save-article-folder
1501     "v" gnus-summary-save-article-vm
1502     "p" gnus-summary-pipe-output
1503     "s" gnus-soup-add-article)
1504
1505   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1506     "b" gnus-summary-display-buttonized
1507     "v" gnus-article-view-part
1508     "o" gnus-article-save-part
1509     "c" gnus-article-copy-part
1510     "e" gnus-article-externalize-part
1511     "|" gnus-article-pipe-part)
1512   )
1513
1514 (defun gnus-summary-make-menu-bar ()
1515   (gnus-turn-off-edit-menu 'summary)
1516
1517   (unless (boundp 'gnus-summary-misc-menu)
1518
1519     (easy-menu-define
1520      gnus-summary-kill-menu gnus-summary-mode-map ""
1521      (cons
1522       "Score"
1523       (nconc
1524        (list
1525         ["Enter score..." gnus-summary-score-entry t]
1526         ["Customize" gnus-score-customize t])
1527        (gnus-make-score-map 'increase)
1528        (gnus-make-score-map 'lower)
1529        '(("Mark"
1530           ["Kill below" gnus-summary-kill-below t]
1531           ["Mark above" gnus-summary-mark-above t]
1532           ["Tick above" gnus-summary-tick-above t]
1533           ["Clear above" gnus-summary-clear-above t])
1534          ["Current score" gnus-summary-current-score t]
1535          ["Set score" gnus-summary-set-score t]
1536          ["Switch current score file..." gnus-score-change-score-file t]
1537          ["Set mark below..." gnus-score-set-mark-below t]
1538          ["Set expunge below..." gnus-score-set-expunge-below t]
1539          ["Edit current score file" gnus-score-edit-current-scores t]
1540          ["Edit score file" gnus-score-edit-file t]
1541          ["Trace score" gnus-score-find-trace t]
1542          ["Find words" gnus-score-find-favourite-words t]
1543          ["Rescore buffer" gnus-summary-rescore t]
1544          ["Increase score..." gnus-summary-increase-score t]
1545          ["Lower score..." gnus-summary-lower-score t]))))
1546
1547     ;; Define both the Article menu in the summary buffer and the equivalent
1548     ;; Commands menu in the article buffer here for consistency.
1549     (let ((innards
1550            '(("Hide"
1551               ["All" gnus-article-hide t]
1552               ["Headers" gnus-article-hide-headers t]
1553               ["Signature" gnus-article-hide-signature t]
1554               ["Citation" gnus-article-hide-citation t]
1555               ["PGP" gnus-article-hide-pgp t]
1556               ["Boring headers" gnus-article-hide-boring-headers t])
1557              ("Highlight"
1558               ["All" gnus-article-highlight t]
1559               ["Headers" gnus-article-highlight-headers t]
1560               ["Signature" gnus-article-highlight-signature t]
1561               ["Citation" gnus-article-highlight-citation t])
1562              ("Date"
1563               ["Local" gnus-article-date-local t]
1564               ["ISO8601" gnus-article-date-iso8601 t]
1565               ["UT" gnus-article-date-ut t]
1566               ["Original" gnus-article-date-original t]
1567               ["Lapsed" gnus-article-date-lapsed t]
1568               ["User-defined" gnus-article-date-user t])
1569              ("Washing"
1570               ("Remove Blanks"
1571                ["Leading" gnus-article-strip-leading-blank-lines t]
1572                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1573                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1574                ["All of the above" gnus-article-strip-blank-lines t]
1575                ["All" gnus-article-strip-all-blank-lines t]
1576                ["Leading space" gnus-article-strip-leading-space t]
1577                ["Trailing space" gnus-article-strip-trailing-space t])
1578               ["Overstrike" gnus-article-treat-overstrike t]
1579               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1580               ["Emphasis" gnus-article-emphasize t]
1581               ["Word wrap" gnus-article-fill-cited-article t]
1582               ["CR" gnus-article-remove-cr t]
1583               ["Show X-Face" gnus-article-display-x-face t]
1584               ["UnHTMLize" gnus-article-treat-html t]
1585               ["Rot 13" gnus-summary-caesar-message t]
1586               ["Unix pipe" gnus-summary-pipe-message t]
1587               ["Add buttons" gnus-article-add-buttons t]
1588               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1589               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1590               ["Toggle MIME" gnus-summary-toggle-mime t]
1591               ["Verbose header" gnus-summary-verbose-headers t]
1592               ["Toggle header" gnus-summary-toggle-header t])
1593              ("Output"
1594               ["Save in default format" gnus-summary-save-article t]
1595               ["Save in file" gnus-summary-save-article-file t]
1596               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1597               ["Save in MH folder" gnus-summary-save-article-folder t]
1598               ["Save in VM folder" gnus-summary-save-article-vm t]
1599               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1600               ["Save body in file" gnus-summary-save-article-body-file t]
1601               ["Pipe through a filter" gnus-summary-pipe-output t]
1602               ["Add to SOUP packet" gnus-soup-add-article t]
1603               ["Print" gnus-summary-print-article t])
1604              ("Backend"
1605               ["Respool article..." gnus-summary-respool-article t]
1606               ["Move article..." gnus-summary-move-article
1607                (gnus-check-backend-function
1608                 'request-move-article gnus-newsgroup-name)]
1609               ["Copy article..." gnus-summary-copy-article t]
1610               ["Crosspost article..." gnus-summary-crosspost-article
1611                (gnus-check-backend-function
1612                 'request-replace-article gnus-newsgroup-name)]
1613               ["Import file..." gnus-summary-import-article t]
1614               ["Check if posted" gnus-summary-article-posted-p t]
1615               ["Edit article" gnus-summary-edit-article
1616                (not (gnus-group-read-only-p))]
1617               ["Delete article" gnus-summary-delete-article
1618                (gnus-check-backend-function
1619                 'request-expire-articles gnus-newsgroup-name)]
1620               ["Query respool" gnus-summary-respool-query t]
1621               ["Trace respool" gnus-summary-respool-trace t]
1622               ["Delete expirable articles" gnus-summary-expire-articles-now
1623                (gnus-check-backend-function
1624                 'request-expire-articles gnus-newsgroup-name)])
1625              ("Extract"
1626               ["Uudecode" gnus-uu-decode-uu t]
1627               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1628               ["Unshar" gnus-uu-decode-unshar t]
1629               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1630               ["Save" gnus-uu-decode-save t]
1631               ["Binhex" gnus-uu-decode-binhex t]
1632               ["Postscript" gnus-uu-decode-postscript t])
1633              ("Cache"
1634               ["Enter article" gnus-cache-enter-article t]
1635               ["Remove article" gnus-cache-remove-article t])
1636              ["Select article buffer" gnus-summary-select-article-buffer t]
1637              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1638              ["Isearch article..." gnus-summary-isearch-article t]
1639              ["Beginning of the article" gnus-summary-beginning-of-article t]
1640              ["End of the article" gnus-summary-end-of-article t]
1641              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1642              ["Fetch referenced articles" gnus-summary-refer-references t]
1643              ["Fetch current thread" gnus-summary-refer-thread t]
1644              ["Fetch article with id..." gnus-summary-refer-article t]
1645              ["Redisplay" gnus-summary-show-article t])))
1646       (easy-menu-define
1647        gnus-summary-article-menu gnus-summary-mode-map ""
1648        (cons "Article" innards))
1649
1650       (easy-menu-define
1651        gnus-article-commands-menu gnus-article-mode-map ""
1652        (cons "Commands" innards)))
1653
1654     (easy-menu-define
1655      gnus-summary-thread-menu gnus-summary-mode-map ""
1656      '("Threads"
1657        ["Toggle threading" gnus-summary-toggle-threads t]
1658        ["Hide threads" gnus-summary-hide-all-threads t]
1659        ["Show threads" gnus-summary-show-all-threads t]
1660        ["Hide thread" gnus-summary-hide-thread t]
1661        ["Show thread" gnus-summary-show-thread t]
1662        ["Go to next thread" gnus-summary-next-thread t]
1663        ["Go to previous thread" gnus-summary-prev-thread t]
1664        ["Go down thread" gnus-summary-down-thread t]
1665        ["Go up thread" gnus-summary-up-thread t]
1666        ["Top of thread" gnus-summary-top-thread t]
1667        ["Mark thread as read" gnus-summary-kill-thread t]
1668        ["Lower thread score" gnus-summary-lower-thread t]
1669        ["Raise thread score" gnus-summary-raise-thread t]
1670        ["Rethread current" gnus-summary-rethread-current t]
1671        ))
1672
1673     (easy-menu-define
1674      gnus-summary-post-menu gnus-summary-mode-map ""
1675      '("Post"
1676        ["Post an article" gnus-summary-post-news t]
1677        ["Followup" gnus-summary-followup t]
1678        ["Followup and yank" gnus-summary-followup-with-original t]
1679        ["Supersede article" gnus-summary-supersede-article t]
1680        ["Cancel article" gnus-summary-cancel-article t]
1681        ["Reply" gnus-summary-reply t]
1682        ["Reply and yank" gnus-summary-reply-with-original t]
1683        ["Wide reply" gnus-summary-wide-reply t]
1684        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1685        ["Mail forward" gnus-summary-mail-forward t]
1686        ["Post forward" gnus-summary-post-forward t]
1687        ["Digest and mail" gnus-summary-mail-digest t]
1688        ["Digest and post" gnus-summary-post-digest t]
1689        ["Resend message" gnus-summary-resend-message t]
1690        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1691        ["Send a mail" gnus-summary-mail-other-window t]
1692        ["Uuencode and post" gnus-uu-post-news t]
1693        ["Followup via news" gnus-summary-followup-to-mail t]
1694        ["Followup via news and yank"
1695         gnus-summary-followup-to-mail-with-original t]
1696        ;;("Draft"
1697        ;;["Send" gnus-summary-send-draft t]
1698        ;;["Send bounced" gnus-resend-bounced-mail t])
1699        ))
1700
1701     (easy-menu-define
1702      gnus-summary-misc-menu gnus-summary-mode-map ""
1703      '("Misc"
1704        ("Mark Read"
1705         ["Mark as read" gnus-summary-mark-as-read-forward t]
1706         ["Mark same subject and select"
1707          gnus-summary-kill-same-subject-and-select t]
1708         ["Mark same subject" gnus-summary-kill-same-subject t]
1709         ["Catchup" gnus-summary-catchup t]
1710         ["Catchup all" gnus-summary-catchup-all t]
1711         ["Catchup to here" gnus-summary-catchup-to-here t]
1712         ["Catchup region" gnus-summary-mark-region-as-read t]
1713         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1714        ("Mark Various"
1715         ["Tick" gnus-summary-tick-article-forward t]
1716         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1717         ["Remove marks" gnus-summary-clear-mark-forward t]
1718         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1719         ["Set bookmark" gnus-summary-set-bookmark t]
1720         ["Remove bookmark" gnus-summary-remove-bookmark t])
1721        ("Mark Limit"
1722         ["Marks..." gnus-summary-limit-to-marks t]
1723         ["Subject..." gnus-summary-limit-to-subject t]
1724         ["Author..." gnus-summary-limit-to-author t]
1725         ["Age..." gnus-summary-limit-to-age t]
1726         ["Score" gnus-summary-limit-to-score t]
1727         ["Unread" gnus-summary-limit-to-unread t]
1728         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1729         ["Articles" gnus-summary-limit-to-articles t]
1730         ["Pop limit" gnus-summary-pop-limit t]
1731         ["Show dormant" gnus-summary-limit-include-dormant t]
1732         ["Hide childless dormant"
1733          gnus-summary-limit-exclude-childless-dormant t]
1734         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1735         ["Show expunged" gnus-summary-show-all-expunged t])
1736        ("Process Mark"
1737         ["Set mark" gnus-summary-mark-as-processable t]
1738         ["Remove mark" gnus-summary-unmark-as-processable t]
1739         ["Remove all marks" gnus-summary-unmark-all-processable t]
1740         ["Mark above" gnus-uu-mark-over t]
1741         ["Mark series" gnus-uu-mark-series t]
1742         ["Mark region" gnus-uu-mark-region t]
1743         ["Unmark region" gnus-uu-unmark-region t]
1744         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1745         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1746         ["Mark all" gnus-uu-mark-all t]
1747         ["Mark buffer" gnus-uu-mark-buffer t]
1748         ["Mark sparse" gnus-uu-mark-sparse t]
1749         ["Mark thread" gnus-uu-mark-thread t]
1750         ["Unmark thread" gnus-uu-unmark-thread t]
1751         ("Process Mark Sets"
1752          ["Kill" gnus-summary-kill-process-mark t]
1753          ["Yank" gnus-summary-yank-process-mark
1754           gnus-newsgroup-process-stack]
1755          ["Save" gnus-summary-save-process-mark t]))
1756        ("Scroll article"
1757         ["Page forward" gnus-summary-next-page t]
1758         ["Page backward" gnus-summary-prev-page t]
1759         ["Line forward" gnus-summary-scroll-up t])
1760        ("Move"
1761         ["Next unread article" gnus-summary-next-unread-article t]
1762         ["Previous unread article" gnus-summary-prev-unread-article t]
1763         ["Next article" gnus-summary-next-article t]
1764         ["Previous article" gnus-summary-prev-article t]
1765         ["Next unread subject" gnus-summary-next-unread-subject t]
1766         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1767         ["Next article same subject" gnus-summary-next-same-subject t]
1768         ["Previous article same subject" gnus-summary-prev-same-subject t]
1769         ["First unread article" gnus-summary-first-unread-article t]
1770         ["Best unread article" gnus-summary-best-unread-article t]
1771         ["Go to subject number..." gnus-summary-goto-subject t]
1772         ["Go to article number..." gnus-summary-goto-article t]
1773         ["Go to the last article" gnus-summary-goto-last-article t]
1774         ["Pop article off history" gnus-summary-pop-article t])
1775        ("Sort"
1776         ["Sort by number" gnus-summary-sort-by-number t]
1777         ["Sort by author" gnus-summary-sort-by-author t]
1778         ["Sort by subject" gnus-summary-sort-by-subject t]
1779         ["Sort by date" gnus-summary-sort-by-date t]
1780         ["Sort by score" gnus-summary-sort-by-score t]
1781         ["Sort by lines" gnus-summary-sort-by-lines t])
1782        ("Help"
1783         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1784         ["Describe group" gnus-summary-describe-group t]
1785         ["Read manual" gnus-info-find-node t])
1786        ("Modes"
1787         ["Pick and read" gnus-pick-mode t]
1788         ["Binary" gnus-binary-mode t])
1789        ("Regeneration"
1790         ["Regenerate" gnus-summary-prepare t]
1791         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1792         ["Toggle threading" gnus-summary-toggle-threads t])
1793        ["Filter articles..." gnus-summary-execute-command t]
1794        ["Run command on subjects..." gnus-summary-universal-argument t]
1795        ["Search articles forward..." gnus-summary-search-article-forward t]
1796        ["Search articles backward..." gnus-summary-search-article-backward t]
1797        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1798        ["Expand window" gnus-summary-expand-window t]
1799        ["Expire expirable articles" gnus-summary-expire-articles
1800         (gnus-check-backend-function
1801          'request-expire-articles gnus-newsgroup-name)]
1802        ["Edit local kill file" gnus-summary-edit-local-kill t]
1803        ["Edit main kill file" gnus-summary-edit-global-kill t]
1804        ["Edit group parameters" gnus-summary-edit-parameters t]
1805        ["Customize group parameters" gnus-summary-customize-parameters t]
1806        ["Send a bug report" gnus-bug t]
1807        ("Exit"
1808         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1809         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1810         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1811         ["Exit group" gnus-summary-exit t]
1812         ["Exit group without updating" gnus-summary-exit-no-update t]
1813         ["Exit and goto next group" gnus-summary-next-group t]
1814         ["Exit and goto prev group" gnus-summary-prev-group t]
1815         ["Reselect group" gnus-summary-reselect-current-group t]
1816         ["Rescan group" gnus-summary-rescan-group t]
1817         ["Update dribble" gnus-summary-save-newsrc t])))
1818
1819     (gnus-run-hooks 'gnus-summary-menu-hook)))
1820
1821 (defun gnus-score-set-default (var value)
1822   "A version of set that updates the GNU Emacs menu-bar."
1823   (set var value)
1824   ;; It is the message that forces the active status to be updated.
1825   (message ""))
1826
1827 (defun gnus-make-score-map (type)
1828   "Make a summary score map of type TYPE."
1829   (if t
1830       nil
1831     (let ((headers '(("author" "from" string)
1832                      ("subject" "subject" string)
1833                      ("article body" "body" string)
1834                      ("article head" "head" string)
1835                      ("xref" "xref" string)
1836                      ("extra header" "extra" string)
1837                      ("lines" "lines" number)
1838                      ("followups to author" "followup" string)))
1839           (types '((number ("less than" <)
1840                            ("greater than" >)
1841                            ("equal" =))
1842                    (string ("substring" s)
1843                            ("exact string" e)
1844                            ("fuzzy string" f)
1845                            ("regexp" r))))
1846           (perms '(("temporary" (current-time-string))
1847                    ("permanent" nil)
1848                    ("immediate" now)))
1849           header)
1850       (list
1851        (apply
1852         'nconc
1853         (list
1854          (if (eq type 'lower)
1855              "Lower score"
1856            "Increase score"))
1857         (let (outh)
1858           (while headers
1859             (setq header (car headers))
1860             (setq outh
1861                   (cons
1862                    (apply
1863                     'nconc
1864                     (list (car header))
1865                     (let ((ts (cdr (assoc (nth 2 header) types)))
1866                           outt)
1867                       (while ts
1868                         (setq outt
1869                               (cons
1870                                (apply
1871                                 'nconc
1872                                 (list (caar ts))
1873                                 (let ((ps perms)
1874                                       outp)
1875                                   (while ps
1876                                     (setq outp
1877                                           (cons
1878                                            (vector
1879                                             (caar ps)
1880                                             (list
1881                                              'gnus-summary-score-entry
1882                                              (nth 1 header)
1883                                              (if (or (string= (nth 1 header)
1884                                                               "head")
1885                                                      (string= (nth 1 header)
1886                                                               "body"))
1887                                                  ""
1888                                                (list 'gnus-summary-header
1889                                                      (nth 1 header)))
1890                                              (list 'quote (nth 1 (car ts)))
1891                                              (list 'gnus-score-default nil)
1892                                              (nth 1 (car ps))
1893                                              t)
1894                                             t)
1895                                            outp))
1896                                     (setq ps (cdr ps)))
1897                                   (list (nreverse outp))))
1898                                outt))
1899                         (setq ts (cdr ts)))
1900                       (list (nreverse outt))))
1901                    outh))
1902             (setq headers (cdr headers)))
1903           (list (nreverse outh))))))))
1904
1905 \f
1906
1907 (defun gnus-summary-mode (&optional group)
1908   "Major mode for reading articles.
1909
1910 All normal editing commands are switched off.
1911 \\<gnus-summary-mode-map>
1912 Each line in this buffer represents one article.  To read an
1913 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1914 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1915 respectively.
1916
1917 You can also post articles and send mail from this buffer.  To
1918 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1919 of an article, type `\\[gnus-summary-reply]'.
1920
1921 There are approx. one gazillion commands you can execute in this
1922 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1923
1924 The following commands are available:
1925
1926 \\{gnus-summary-mode-map}"
1927   (interactive)
1928   (when (gnus-visual-p 'summary-menu 'menu)
1929     (gnus-summary-make-menu-bar))
1930   (kill-all-local-variables)
1931   (gnus-summary-make-local-variables)
1932   (gnus-make-thread-indent-array)
1933   (gnus-simplify-mode-line)
1934   (setq major-mode 'gnus-summary-mode)
1935   (setq mode-name "Summary")
1936   (make-local-variable 'minor-mode-alist)
1937   (use-local-map gnus-summary-mode-map)
1938   (buffer-disable-undo)
1939   (setq buffer-read-only t)             ;Disable modification
1940   (setq truncate-lines t)
1941   (setq selective-display t)
1942   (setq selective-display-ellipses t)   ;Display `...'
1943   (gnus-summary-set-display-table)
1944   (gnus-set-default-directory)
1945   (setq gnus-newsgroup-name group)
1946   (make-local-variable 'gnus-summary-line-format)
1947   (make-local-variable 'gnus-summary-line-format-spec)
1948   (make-local-variable 'gnus-summary-dummy-line-format)
1949   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1950   (make-local-variable 'gnus-summary-mark-positions)
1951   (make-local-hook 'post-command-hook)
1952   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1953   (make-local-hook 'pre-command-hook)
1954   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1955   (gnus-run-hooks 'gnus-summary-mode-hook)
1956   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1957   (gnus-update-summary-mark-positions))
1958
1959 (defun gnus-summary-make-local-variables ()
1960   "Make all the local summary buffer variables."
1961   (let ((locals gnus-summary-local-variables)
1962         global local)
1963     (while (setq local (pop locals))
1964       (if (consp local)
1965           (progn
1966             (if (eq (cdr local) 'global)
1967                 ;; Copy the global value of the variable.
1968                 (setq global (symbol-value (car local)))
1969               ;; Use the value from the list.
1970               (setq global (eval (cdr local))))
1971             (make-local-variable (car local))
1972             (set (car local) global))
1973         ;; Simple nil-valued local variable.
1974         (make-local-variable local)
1975         (set local nil)))))
1976
1977 (defun gnus-summary-clear-local-variables ()
1978   (let ((locals gnus-summary-local-variables))
1979     (while locals
1980       (if (consp (car locals))
1981           (and (vectorp (caar locals))
1982                (set (caar locals) nil))
1983         (and (vectorp (car locals))
1984              (set (car locals) nil)))
1985       (setq locals (cdr locals)))))
1986
1987 ;; Summary data functions.
1988
1989 (defmacro gnus-data-number (data)
1990   `(car ,data))
1991
1992 (defmacro gnus-data-set-number (data number)
1993   `(setcar ,data ,number))
1994
1995 (defmacro gnus-data-mark (data)
1996   `(nth 1 ,data))
1997
1998 (defmacro gnus-data-set-mark (data mark)
1999   `(setcar (nthcdr 1 ,data) ,mark))
2000
2001 (defmacro gnus-data-pos (data)
2002   `(nth 2 ,data))
2003
2004 (defmacro gnus-data-set-pos (data pos)
2005   `(setcar (nthcdr 2 ,data) ,pos))
2006
2007 (defmacro gnus-data-header (data)
2008   `(nth 3 ,data))
2009
2010 (defmacro gnus-data-set-header (data header)
2011   `(setf (nth 3 ,data) ,header))
2012
2013 (defmacro gnus-data-level (data)
2014   `(nth 4 ,data))
2015
2016 (defmacro gnus-data-unread-p (data)
2017   `(= (nth 1 ,data) gnus-unread-mark))
2018
2019 (defmacro gnus-data-read-p (data)
2020   `(/= (nth 1 ,data) gnus-unread-mark))
2021
2022 (defmacro gnus-data-pseudo-p (data)
2023   `(consp (nth 3 ,data)))
2024
2025 (defmacro gnus-data-find (number)
2026   `(assq ,number gnus-newsgroup-data))
2027
2028 (defmacro gnus-data-find-list (number &optional data)
2029   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2030      (memq (assq ,number bdata)
2031            bdata)))
2032
2033 (defmacro gnus-data-make (number mark pos header level)
2034   `(list ,number ,mark ,pos ,header ,level))
2035
2036 (defun gnus-data-enter (after-article number mark pos header level offset)
2037   (let ((data (gnus-data-find-list after-article)))
2038     (unless data
2039       (error "No such article: %d" after-article))
2040     (setcdr data (cons (gnus-data-make number mark pos header level)
2041                        (cdr data)))
2042     (setq gnus-newsgroup-data-reverse nil)
2043     (gnus-data-update-list (cddr data) offset)))
2044
2045 (defun gnus-data-enter-list (after-article list &optional offset)
2046   (when list
2047     (let ((data (and after-article (gnus-data-find-list after-article)))
2048           (ilist list))
2049       (if (not (or data
2050                    after-article))
2051           (let ((odata gnus-newsgroup-data))
2052             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2053             (when offset
2054               (gnus-data-update-list odata offset)))
2055         ;; Find the last element in the list to be spliced into the main
2056         ;; list.
2057         (while (cdr list)
2058           (setq list (cdr list)))
2059         (if (not data)
2060             (progn
2061               (setcdr list gnus-newsgroup-data)
2062               (setq gnus-newsgroup-data ilist)
2063               (when offset
2064                 (gnus-data-update-list (cdr list) offset)))
2065           (setcdr list (cdr data))
2066           (setcdr data ilist)
2067           (when offset
2068             (gnus-data-update-list (cdr list) offset))))
2069       (setq gnus-newsgroup-data-reverse nil))))
2070
2071 (defun gnus-data-remove (article &optional offset)
2072   (let ((data gnus-newsgroup-data))
2073     (if (= (gnus-data-number (car data)) article)
2074         (progn
2075           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2076                 gnus-newsgroup-data-reverse nil)
2077           (when offset
2078             (gnus-data-update-list gnus-newsgroup-data offset)))
2079       (while (cdr data)
2080         (when (= (gnus-data-number (cadr data)) article)
2081           (setcdr data (cddr data))
2082           (when offset
2083             (gnus-data-update-list (cdr data) offset))
2084           (setq data nil
2085                 gnus-newsgroup-data-reverse nil))
2086         (setq data (cdr data))))))
2087
2088 (defmacro gnus-data-list (backward)
2089   `(if ,backward
2090        (or gnus-newsgroup-data-reverse
2091            (setq gnus-newsgroup-data-reverse
2092                  (reverse gnus-newsgroup-data)))
2093      gnus-newsgroup-data))
2094
2095 (defun gnus-data-update-list (data offset)
2096   "Add OFFSET to the POS of all data entries in DATA."
2097   (setq gnus-newsgroup-data-reverse nil)
2098   (while data
2099     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2100     (setq data (cdr data))))
2101
2102 (defun gnus-summary-article-pseudo-p (article)
2103   "Say whether this article is a pseudo article or not."
2104   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2105
2106 (defmacro gnus-summary-article-sparse-p (article)
2107   "Say whether this article is a sparse article or not."
2108   `(memq ,article gnus-newsgroup-sparse))
2109
2110 (defmacro gnus-summary-article-ancient-p (article)
2111   "Say whether this article is a sparse article or not."
2112   `(memq ,article gnus-newsgroup-ancient))
2113
2114 (defun gnus-article-parent-p (number)
2115   "Say whether this article is a parent or not."
2116   (let ((data (gnus-data-find-list number)))
2117     (and (cdr data)                     ; There has to be an article after...
2118          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2119             (gnus-data-level (nth 1 data))))))
2120
2121 (defun gnus-article-children (number)
2122   "Return a list of all children to NUMBER."
2123   (let* ((data (gnus-data-find-list number))
2124          (level (gnus-data-level (car data)))
2125          children)
2126     (setq data (cdr data))
2127     (while (and data
2128                 (= (gnus-data-level (car data)) (1+ level)))
2129       (push (gnus-data-number (car data)) children)
2130       (setq data (cdr data)))
2131     children))
2132
2133 (defmacro gnus-summary-skip-intangible ()
2134   "If the current article is intangible, then jump to a different article."
2135   '(let ((to (get-text-property (point) 'gnus-intangible)))
2136      (and to (gnus-summary-goto-subject to))))
2137
2138 (defmacro gnus-summary-article-intangible-p ()
2139   "Say whether this article is intangible or not."
2140   '(get-text-property (point) 'gnus-intangible))
2141
2142 (defun gnus-article-read-p (article)
2143   "Say whether ARTICLE is read or not."
2144   (not (or (memq article gnus-newsgroup-marked)
2145            (memq article gnus-newsgroup-unreads)
2146            (memq article gnus-newsgroup-unselected)
2147            (memq article gnus-newsgroup-dormant))))
2148
2149 ;; Some summary mode macros.
2150
2151 (defmacro gnus-summary-article-number ()
2152   "The article number of the article on the current line.
2153 If there isn's an article number here, then we return the current
2154 article number."
2155   '(progn
2156      (gnus-summary-skip-intangible)
2157      (or (get-text-property (point) 'gnus-number)
2158          (gnus-summary-last-subject))))
2159
2160 (defmacro gnus-summary-article-header (&optional number)
2161   "Return the header of article NUMBER."
2162   `(gnus-data-header (gnus-data-find
2163                       ,(or number '(gnus-summary-article-number)))))
2164
2165 (defmacro gnus-summary-thread-level (&optional number)
2166   "Return the level of thread that starts with article NUMBER."
2167   `(if (and (eq gnus-summary-make-false-root 'dummy)
2168             (get-text-property (point) 'gnus-intangible))
2169        0
2170      (gnus-data-level (gnus-data-find
2171                        ,(or number '(gnus-summary-article-number))))))
2172
2173 (defmacro gnus-summary-article-mark (&optional number)
2174   "Return the mark of article NUMBER."
2175   `(gnus-data-mark (gnus-data-find
2176                     ,(or number '(gnus-summary-article-number)))))
2177
2178 (defmacro gnus-summary-article-pos (&optional number)
2179   "Return the position of the line of article NUMBER."
2180   `(gnus-data-pos (gnus-data-find
2181                    ,(or number '(gnus-summary-article-number)))))
2182
2183 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2184 (defmacro gnus-summary-article-subject (&optional number)
2185   "Return current subject string or nil if nothing."
2186   `(let ((headers
2187           ,(if number
2188                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2189              '(gnus-data-header (assq (gnus-summary-article-number)
2190                                       gnus-newsgroup-data)))))
2191      (and headers
2192           (vectorp headers)
2193           (mail-header-subject headers))))
2194
2195 (defmacro gnus-summary-article-score (&optional number)
2196   "Return current article score."
2197   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2198                   gnus-newsgroup-scored))
2199        gnus-summary-default-score 0))
2200
2201 (defun gnus-summary-article-children (&optional number)
2202   "Return a list of article numbers that are children of article NUMBER."
2203   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2204          (level (gnus-data-level (car data)))
2205          l children)
2206     (while (and (setq data (cdr data))
2207                 (> (setq l (gnus-data-level (car data))) level))
2208       (and (= (1+ level) l)
2209            (push (gnus-data-number (car data))
2210                  children)))
2211     (nreverse children)))
2212
2213 (defun gnus-summary-article-parent (&optional number)
2214   "Return the article number of the parent of article NUMBER."
2215   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2216                                     (gnus-data-list t)))
2217          (level (gnus-data-level (car data))))
2218     (if (zerop level)
2219         ()                              ; This is a root.
2220       ;; We search until we find an article with a level less than
2221       ;; this one.  That function has to be the parent.
2222       (while (and (setq data (cdr data))
2223                   (not (< (gnus-data-level (car data)) level))))
2224       (and data (gnus-data-number (car data))))))
2225
2226 (defun gnus-unread-mark-p (mark)
2227   "Say whether MARK is the unread mark."
2228   (= mark gnus-unread-mark))
2229
2230 (defun gnus-read-mark-p (mark)
2231   "Say whether MARK is one of the marks that mark as read.
2232 This is all marks except unread, ticked, dormant, and expirable."
2233   (not (or (= mark gnus-unread-mark)
2234            (= mark gnus-ticked-mark)
2235            (= mark gnus-dormant-mark)
2236            (= mark gnus-expirable-mark))))
2237
2238 (defmacro gnus-article-mark (number)
2239   "Return the MARK of article NUMBER.
2240 This macro should only be used when computing the mark the \"first\"
2241 time; i.e., when generating the summary lines.  After that,
2242 `gnus-summary-article-mark' should be used to examine the
2243 marks of articles."
2244   `(cond
2245     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2246     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2247     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2248     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2249     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2250     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2251     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2252     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2253            gnus-ancient-mark))))
2254
2255 ;; Saving hidden threads.
2256
2257 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2258 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2259
2260 (defmacro gnus-save-hidden-threads (&rest forms)
2261   "Save hidden threads, eval FORMS, and restore the hidden threads."
2262   (let ((config (make-symbol "config")))
2263     `(let ((,config (gnus-hidden-threads-configuration)))
2264        (unwind-protect
2265            (save-excursion
2266              ,@forms)
2267          (gnus-restore-hidden-threads-configuration ,config)))))
2268
2269 (defun gnus-data-compute-positions ()
2270   "Compute the positions of all articles."
2271   (setq gnus-newsgroup-data-reverse nil)
2272   (let ((data gnus-newsgroup-data))
2273     (save-excursion
2274       (gnus-save-hidden-threads
2275         (gnus-summary-show-all-threads)
2276         (goto-char (point-min))
2277         (while data
2278           (while (get-text-property (point) 'gnus-intangible)
2279             (forward-line 1))
2280           (gnus-data-set-pos (car data) (+ (point) 3))
2281           (setq data (cdr data))
2282           (forward-line 1))))))
2283
2284 (defun gnus-hidden-threads-configuration ()
2285   "Return the current hidden threads configuration."
2286   (save-excursion
2287     (let (config)
2288       (goto-char (point-min))
2289       (while (search-forward "\r" nil t)
2290         (push (1- (point)) config))
2291       config)))
2292
2293 (defun gnus-restore-hidden-threads-configuration (config)
2294   "Restore hidden threads configuration from CONFIG."
2295   (let (point buffer-read-only)
2296     (while (setq point (pop config))
2297       (when (and (< point (point-max))
2298                  (goto-char point)
2299                  (eq (char-after) ?\n))
2300         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2301
2302 ;; Various summary mode internalish functions.
2303
2304 (defun gnus-mouse-pick-article (e)
2305   (interactive "e")
2306   (mouse-set-point e)
2307   (gnus-summary-next-page nil t))
2308
2309 (defun gnus-summary-set-display-table ()
2310   ;; Change the display table.  Odd characters have a tendency to mess
2311   ;; up nicely formatted displays - we make all possible glyphs
2312   ;; display only a single character.
2313
2314   ;; We start from the standard display table, if any.
2315   (let ((table (or (copy-sequence standard-display-table)
2316                    (make-display-table)))
2317         (i 32))
2318     ;; Nix out all the control chars...
2319     (while (>= (setq i (1- i)) 0)
2320       (aset table i [??]))
2321     ;; ... but not newline and cr, of course.  (cr is necessary for the
2322     ;; selective display).
2323     (aset table ?\n nil)
2324     (aset table ?\r nil)
2325     ;; We keep TAB as well.
2326     (aset table ?\t nil)
2327     ;; We nix out any glyphs over 126 that are not set already.
2328     (let ((i 256))
2329       (while (>= (setq i (1- i)) 127)
2330         ;; Only modify if the entry is nil.
2331         (unless (aref table i)
2332           (aset table i [??]))))
2333     (setq buffer-display-table table)))
2334
2335 (defun gnus-summary-setup-buffer (group)
2336   "Initialize summary buffer."
2337   (let ((buffer (concat "*Summary " group "*")))
2338     (if (get-buffer buffer)
2339         (progn
2340           (set-buffer buffer)
2341           (setq gnus-summary-buffer (current-buffer))
2342           (not gnus-newsgroup-prepared))
2343       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2344       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2345       (gnus-summary-mode group)
2346       (when gnus-carpal
2347         (gnus-carpal-setup-buffer 'summary))
2348       (unless gnus-single-article-buffer
2349         (make-local-variable 'gnus-article-buffer)
2350         (make-local-variable 'gnus-article-current)
2351         (make-local-variable 'gnus-original-article-buffer))
2352       (setq gnus-newsgroup-name group)
2353       t)))
2354
2355 (defun gnus-set-global-variables ()
2356   ;; Set the global equivalents of the summary buffer-local variables
2357   ;; to the latest values they had.  These reflect the summary buffer
2358   ;; that was in action when the last article was fetched.
2359   (when (eq major-mode 'gnus-summary-mode)
2360     (setq gnus-summary-buffer (current-buffer))
2361     (let ((name gnus-newsgroup-name)
2362           (marked gnus-newsgroup-marked)
2363           (unread gnus-newsgroup-unreads)
2364           (headers gnus-current-headers)
2365           (data gnus-newsgroup-data)
2366           (summary gnus-summary-buffer)
2367           (article-buffer gnus-article-buffer)
2368           (original gnus-original-article-buffer)
2369           (gac gnus-article-current)
2370           (reffed gnus-reffed-article-number)
2371           (score-file gnus-current-score-file)
2372           (default-charset gnus-newsgroup-default-charset)
2373           (iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2374       (save-excursion
2375         (set-buffer gnus-group-buffer)
2376         (setq gnus-newsgroup-name name
2377               gnus-newsgroup-marked marked
2378               gnus-newsgroup-unreads unread
2379               gnus-current-headers headers
2380               gnus-newsgroup-data data
2381               gnus-article-current gac
2382               gnus-summary-buffer summary
2383               gnus-article-buffer article-buffer
2384               gnus-original-article-buffer original
2385               gnus-reffed-article-number reffed
2386               gnus-current-score-file score-file
2387               gnus-newsgroup-default-charset default-charset
2388               gnus-newsgroup-iso-8859-1-forced iso-8859-1-forced)
2389         ;; The article buffer also has local variables.
2390         (when (gnus-buffer-live-p gnus-article-buffer)
2391           (set-buffer gnus-article-buffer)
2392           (setq gnus-summary-buffer summary))))))
2393
2394 (defun gnus-summary-article-unread-p (article)
2395   "Say whether ARTICLE is unread or not."
2396   (memq article gnus-newsgroup-unreads))
2397
2398 (defun gnus-summary-first-article-p (&optional article)
2399   "Return whether ARTICLE is the first article in the buffer."
2400   (if (not (setq article (or article (gnus-summary-article-number))))
2401       nil
2402     (eq article (caar gnus-newsgroup-data))))
2403
2404 (defun gnus-summary-last-article-p (&optional article)
2405   "Return whether ARTICLE is the last article in the buffer."
2406   (if (not (setq article (or article (gnus-summary-article-number))))
2407       t                                 ; All non-existent numbers are the last article.  :-)
2408     (not (cdr (gnus-data-find-list article)))))
2409
2410 (defun gnus-make-thread-indent-array ()
2411   (let ((n 200))
2412     (unless (and gnus-thread-indent-array
2413                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2414       (setq gnus-thread-indent-array (make-vector 201 "")
2415             gnus-thread-indent-array-level gnus-thread-indent-level)
2416       (while (>= n 0)
2417         (aset gnus-thread-indent-array n
2418               (make-string (* n gnus-thread-indent-level) ? ))
2419         (setq n (1- n))))))
2420
2421 (defun gnus-update-summary-mark-positions ()
2422   "Compute where the summary marks are to go."
2423   (save-excursion
2424     (when (gnus-buffer-exists-p gnus-summary-buffer)
2425       (set-buffer gnus-summary-buffer))
2426     (let ((gnus-replied-mark 129)
2427           (gnus-score-below-mark 130)
2428           (gnus-score-over-mark 130)
2429           (gnus-download-mark 131)
2430           (spec gnus-summary-line-format-spec)
2431           gnus-visual pos)
2432       (save-excursion
2433         (gnus-set-work-buffer)
2434         (let ((gnus-summary-line-format-spec spec)
2435               (gnus-newsgroup-downloadable '((0 . t))))
2436           (gnus-summary-insert-line
2437            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2438            0 nil 128 t nil "" nil 1)
2439           (goto-char (point-min))
2440           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2441                                              (- (point) 2)))))
2442           (goto-char (point-min))
2443           (push (cons 'replied (and (search-forward "\201" nil t)
2444                                     (- (point) 2)))
2445                 pos)
2446           (goto-char (point-min))
2447           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2448                 pos)
2449           (goto-char (point-min))
2450           (push (cons 'download
2451                       (and (search-forward "\203" nil t) (- (point) 2)))
2452                 pos)))
2453       (setq gnus-summary-mark-positions pos))))
2454
2455 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2456   "Insert a dummy root in the summary buffer."
2457   (beginning-of-line)
2458   (gnus-add-text-properties
2459    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2460    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2461
2462 (defun gnus-summary-from-or-to-or-newsgroups (header)
2463   (let ((to (cdr (assq 'To (mail-header-extra header))))
2464         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2465         (rfc2047-default-charset gnus-newsgroup-default-charset)
2466         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2467     (cond
2468      ((and to
2469            gnus-ignored-from-addresses
2470            (string-match gnus-ignored-from-addresses
2471                          (mail-header-from header)))
2472       (concat "-> "
2473               (or (car (funcall gnus-extract-address-components
2474                                 (funcall
2475                                  gnus-decode-encoded-word-function to)))
2476                   (funcall gnus-decode-encoded-word-function to))))
2477      ((and newsgroups
2478            gnus-ignored-from-addresses
2479            (string-match gnus-ignored-from-addresses
2480                          (mail-header-from header)))
2481       (concat "=> " newsgroups))
2482      (t
2483       (or (car (funcall gnus-extract-address-components
2484                         (mail-header-from header)))
2485           (mail-header-from header))))))
2486
2487 (defun gnus-summary-insert-line (gnus-tmp-header
2488                                  gnus-tmp-level gnus-tmp-current
2489                                  gnus-tmp-unread gnus-tmp-replied
2490                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2491                                  &optional gnus-tmp-dummy gnus-tmp-score
2492                                  gnus-tmp-process)
2493   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2494          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2495          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2496          (gnus-tmp-score-char
2497           (if (or (null gnus-summary-default-score)
2498                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2499                       gnus-summary-zcore-fuzz))
2500               ? 
2501             (if (< gnus-tmp-score gnus-summary-default-score)
2502                 gnus-score-below-mark gnus-score-over-mark)))
2503          (gnus-tmp-replied
2504           (cond (gnus-tmp-process gnus-process-mark)
2505                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2506                  gnus-cached-mark)
2507                 (gnus-tmp-replied gnus-replied-mark)
2508                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2509                  gnus-saved-mark)
2510                 (t gnus-unread-mark)))
2511          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2512          (gnus-tmp-name
2513           (cond
2514            ((string-match "<[^>]+> *$" gnus-tmp-from)
2515             (let ((beg (match-beginning 0)))
2516               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2517                        (substring gnus-tmp-from (1+ (match-beginning 0))
2518                                   (1- (match-end 0))))
2519                   (substring gnus-tmp-from 0 beg))))
2520            ((string-match "(.+)" gnus-tmp-from)
2521             (substring gnus-tmp-from
2522                        (1+ (match-beginning 0)) (1- (match-end 0))))
2523            (t gnus-tmp-from)))
2524          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2525          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2526          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2527          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2528          (buffer-read-only nil))
2529     (when (string= gnus-tmp-name "")
2530       (setq gnus-tmp-name gnus-tmp-from))
2531     (unless (numberp gnus-tmp-lines)
2532       (setq gnus-tmp-lines 0))
2533     (gnus-put-text-property-excluding-characters-with-faces
2534      (point)
2535      (progn (eval gnus-summary-line-format-spec) (point))
2536      'gnus-number gnus-tmp-number)
2537     (when (gnus-visual-p 'summary-highlight 'highlight)
2538       (forward-line -1)
2539       (gnus-run-hooks 'gnus-summary-update-hook)
2540       (forward-line 1))))
2541
2542 (defun gnus-summary-update-line (&optional dont-update)
2543   ;; Update summary line after change.
2544   (when (and gnus-summary-default-score
2545              (not gnus-summary-inhibit-highlight))
2546     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2547            (article (gnus-summary-article-number))
2548            (score (gnus-summary-article-score article)))
2549       (unless dont-update
2550         (if (and gnus-summary-mark-below
2551                  (< (gnus-summary-article-score)
2552                     gnus-summary-mark-below))
2553             ;; This article has a low score, so we mark it as read.
2554             (when (memq article gnus-newsgroup-unreads)
2555               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2556           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2557             ;; This article was previously marked as read on account
2558             ;; of a low score, but now it has risen, so we mark it as
2559             ;; unread.
2560             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2561         (gnus-summary-update-mark
2562          (if (or (null gnus-summary-default-score)
2563                  (<= (abs (- score gnus-summary-default-score))
2564                      gnus-summary-zcore-fuzz))
2565              ? 
2566            (if (< score gnus-summary-default-score)
2567                gnus-score-below-mark gnus-score-over-mark))
2568          'score))
2569       ;; Do visual highlighting.
2570       (when (gnus-visual-p 'summary-highlight 'highlight)
2571         (gnus-run-hooks 'gnus-summary-update-hook)))))
2572
2573 (defvar gnus-tmp-new-adopts nil)
2574
2575 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2576   "Return the number of articles in THREAD.
2577 This may be 0 in some cases -- if none of the articles in
2578 the thread are to be displayed."
2579   (let* ((number
2580           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2581           (cond
2582            ((not (listp thread))
2583             1)
2584            ((and (consp thread) (cdr thread))
2585             (apply
2586              '+ 1 (mapcar
2587                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2588            ((null thread)
2589             1)
2590            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2591             1)
2592            (t 0))))
2593     (when (and level (zerop level) gnus-tmp-new-adopts)
2594       (incf number
2595             (apply '+ (mapcar
2596                        'gnus-summary-number-of-articles-in-thread
2597                        gnus-tmp-new-adopts))))
2598     (if char
2599         (if (> number 1) gnus-not-empty-thread-mark
2600           gnus-empty-thread-mark)
2601       number)))
2602
2603 (defun gnus-summary-set-local-parameters (group)
2604   "Go through the local params of GROUP and set all variable specs in that list."
2605   (let ((params (gnus-group-find-parameter group))
2606         elem)
2607     (while params
2608       (setq elem (car params)
2609             params (cdr params))
2610       (and (consp elem)                 ; Has to be a cons.
2611            (consp (cdr elem))           ; The cdr has to be a list.
2612            (symbolp (car elem))         ; Has to be a symbol in there.
2613            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2614            (ignore-errors               ; So we set it.
2615              (make-local-variable (car elem))
2616              (set (car elem) (eval (nth 1 elem))))))))
2617
2618 (defun gnus-summary-read-group (group &optional show-all no-article
2619                                       kill-buffer no-display backward
2620                                       select-articles)
2621   "Start reading news in newsgroup GROUP.
2622 If SHOW-ALL is non-nil, already read articles are also listed.
2623 If NO-ARTICLE is non-nil, no article is selected initially.
2624 If NO-DISPLAY, don't generate a summary buffer."
2625   (let (result)
2626     (while (and group
2627                 (null (setq result
2628                             (let ((gnus-auto-select-next nil))
2629                               (or (gnus-summary-read-group-1
2630                                    group show-all no-article
2631                                    kill-buffer no-display
2632                                    select-articles)
2633                                   (setq show-all nil
2634                                         select-articles nil)))))
2635                 (eq gnus-auto-select-next 'quietly))
2636       (set-buffer gnus-group-buffer)
2637       ;; The entry function called above goes to the next
2638       ;; group automatically, so we go two groups back
2639       ;; if we are searching for the previous group.
2640       (when backward
2641         (gnus-group-prev-unread-group 2))
2642       (if (not (equal group (gnus-group-group-name)))
2643           (setq group (gnus-group-group-name))
2644         (setq group nil)))
2645     result))
2646
2647 (defun gnus-summary-read-group-1 (group show-all no-article
2648                                         kill-buffer no-display
2649                                         &optional select-articles)
2650   ;; Killed foreign groups can't be entered.
2651   (when (and (not (gnus-group-native-p group))
2652              (not (gnus-gethash group gnus-newsrc-hashtb)))
2653     (error "Dead non-native groups can't be entered"))
2654   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2655   (let* ((new-group (gnus-summary-setup-buffer group))
2656          (quit-config (gnus-group-quit-config group))
2657          (did-select (and new-group (gnus-select-newsgroup
2658                                      group show-all select-articles))))
2659     (cond
2660      ;; This summary buffer exists already, so we just select it.
2661      ((not new-group)
2662       (gnus-set-global-variables)
2663       (when kill-buffer
2664         (gnus-kill-or-deaden-summary kill-buffer))
2665       (gnus-configure-windows 'summary 'force)
2666       (gnus-set-mode-line 'summary)
2667       (gnus-summary-position-point)
2668       (message "")
2669       t)
2670      ;; We couldn't select this group.
2671      ((null did-select)
2672       (when (and (eq major-mode 'gnus-summary-mode)
2673                  (not (equal (current-buffer) kill-buffer)))
2674         (kill-buffer (current-buffer))
2675         (if (not quit-config)
2676             (progn
2677               ;; Update the info -- marks might need to be removed,
2678               ;; for instance.
2679               (gnus-summary-update-info)
2680               (set-buffer gnus-group-buffer)
2681               (gnus-group-jump-to-group group)
2682               (gnus-group-next-unread-group 1))
2683           (gnus-handle-ephemeral-exit quit-config)))
2684       (gnus-message 3 "Can't select group")
2685       nil)
2686      ;; The user did a `C-g' while prompting for number of articles,
2687      ;; so we exit this group.
2688      ((eq did-select 'quit)
2689       (and (eq major-mode 'gnus-summary-mode)
2690            (not (equal (current-buffer) kill-buffer))
2691            (kill-buffer (current-buffer)))
2692       (when kill-buffer
2693         (gnus-kill-or-deaden-summary kill-buffer))
2694       (if (not quit-config)
2695           (progn
2696             (set-buffer gnus-group-buffer)
2697             (gnus-group-jump-to-group group)
2698             (gnus-group-next-unread-group 1)
2699             (gnus-configure-windows 'group 'force))
2700         (gnus-handle-ephemeral-exit quit-config))
2701       ;; Finally signal the quit.
2702       (signal 'quit nil))
2703      ;; The group was successfully selected.
2704      (t
2705       (gnus-set-global-variables)
2706       ;; Save the active value in effect when the group was entered.
2707       (setq gnus-newsgroup-active
2708             (gnus-copy-sequence
2709              (gnus-active gnus-newsgroup-name)))
2710       ;; You can change the summary buffer in some way with this hook.
2711       (gnus-run-hooks 'gnus-select-group-hook)
2712       ;; Set any local variables in the group parameters.
2713       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2714       (gnus-update-format-specifications
2715        nil 'summary 'summary-mode 'summary-dummy)
2716       ;; Do score processing.
2717       (when gnus-use-scoring
2718         (gnus-possibly-score-headers))
2719       ;; Check whether to fill in the gaps in the threads.
2720       (when gnus-build-sparse-threads
2721         (gnus-build-sparse-threads))
2722       ;; Find the initial limit.
2723       (if gnus-show-threads
2724           (if show-all
2725               (let ((gnus-newsgroup-dormant nil))
2726                 (gnus-summary-initial-limit show-all))
2727             (gnus-summary-initial-limit show-all))
2728         (setq gnus-newsgroup-limit
2729               (mapcar
2730                (lambda (header) (mail-header-number header))
2731                gnus-newsgroup-headers)))
2732       ;; Generate the summary buffer.
2733       (unless no-display
2734         (gnus-summary-prepare))
2735       (when gnus-use-trees
2736         (gnus-tree-open group)
2737         (setq gnus-summary-highlight-line-function
2738               'gnus-tree-highlight-article))
2739       ;; If the summary buffer is empty, but there are some low-scored
2740       ;; articles or some excluded dormants, we include these in the
2741       ;; buffer.
2742       (when (and (zerop (buffer-size))
2743                  (not no-display))
2744         (cond (gnus-newsgroup-dormant
2745                (gnus-summary-limit-include-dormant))
2746               ((and gnus-newsgroup-scored show-all)
2747                (gnus-summary-limit-include-expunged t))))
2748       ;; Function `gnus-apply-kill-file' must be called in this hook.
2749       (gnus-run-hooks 'gnus-apply-kill-hook)
2750       (if (and (zerop (buffer-size))
2751                (not no-display))
2752           (progn
2753             ;; This newsgroup is empty.
2754             (gnus-summary-catchup-and-exit nil t)
2755             (gnus-message 6 "No unread news")
2756             (when kill-buffer
2757               (gnus-kill-or-deaden-summary kill-buffer))
2758             ;; Return nil from this function.
2759             nil)
2760         ;; Hide conversation thread subtrees.  We cannot do this in
2761         ;; gnus-summary-prepare-hook since kill processing may not
2762         ;; work with hidden articles.
2763         (and gnus-show-threads
2764              gnus-thread-hide-subtree
2765              (gnus-summary-hide-all-threads))
2766         (when kill-buffer
2767           (gnus-kill-or-deaden-summary kill-buffer))
2768         ;; Show first unread article if requested.
2769         (if (and (not no-article)
2770                  (not no-display)
2771                  gnus-newsgroup-unreads
2772                  gnus-auto-select-first)
2773             (progn
2774               (gnus-configure-windows 'summary)
2775               (cond
2776                ((eq gnus-auto-select-first 'best)
2777                 (gnus-summary-best-unread-article))
2778                ((eq gnus-auto-select-first t)
2779                 (gnus-summary-first-unread-article))
2780                ((gnus-functionp gnus-auto-select-first)
2781                 (funcall gnus-auto-select-first))))
2782           ;; Don't select any articles, just move point to the first
2783           ;; article in the group.
2784           (goto-char (point-min))
2785           (gnus-summary-position-point)
2786           (gnus-configure-windows 'summary 'force)
2787           (gnus-set-mode-line 'summary))
2788         (when (get-buffer-window gnus-group-buffer t)
2789           ;; Gotta use windows, because recenter does weird stuff if
2790           ;; the current buffer ain't the displayed window.
2791           (let ((owin (selected-window)))
2792             (select-window (get-buffer-window gnus-group-buffer t))
2793             (when (gnus-group-goto-group group)
2794               (recenter))
2795             (select-window owin)))
2796         ;; Mark this buffer as "prepared".
2797         (setq gnus-newsgroup-prepared t)
2798         (gnus-run-hooks 'gnus-summary-prepared-hook)
2799         t)))))
2800
2801 (defun gnus-summary-prepare ()
2802   "Generate the summary buffer."
2803   (interactive)
2804   (let ((buffer-read-only nil))
2805     (erase-buffer)
2806     (setq gnus-newsgroup-data nil
2807           gnus-newsgroup-data-reverse nil)
2808     (gnus-run-hooks 'gnus-summary-generate-hook)
2809     ;; Generate the buffer, either with threads or without.
2810     (when gnus-newsgroup-headers
2811       (gnus-summary-prepare-threads
2812        (if gnus-show-threads
2813            (gnus-sort-gathered-threads
2814             (funcall gnus-summary-thread-gathering-function
2815                      (gnus-sort-threads
2816                       (gnus-cut-threads (gnus-make-threads)))))
2817          ;; Unthreaded display.
2818          (gnus-sort-articles gnus-newsgroup-headers))))
2819     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2820     ;; Call hooks for modifying summary buffer.
2821     (goto-char (point-min))
2822     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2823
2824 (defsubst gnus-general-simplify-subject (subject)
2825   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2826   (setq subject
2827         (cond
2828          ;; Truncate the subject.
2829          (gnus-simplify-subject-functions
2830           (gnus-map-function gnus-simplify-subject-functions subject))
2831          ((numberp gnus-summary-gather-subject-limit)
2832           (setq subject (gnus-simplify-subject-re subject))
2833           (if (> (length subject) gnus-summary-gather-subject-limit)
2834               (substring subject 0 gnus-summary-gather-subject-limit)
2835             subject))
2836          ;; Fuzzily simplify it.
2837          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2838           (gnus-simplify-subject-fuzzy subject))
2839          ;; Just remove the leading "Re:".
2840          (t
2841           (gnus-simplify-subject-re subject))))
2842
2843   (if (and gnus-summary-gather-exclude-subject
2844            (string-match gnus-summary-gather-exclude-subject subject))
2845       nil                               ; This article shouldn't be gathered
2846     subject))
2847
2848 (defun gnus-summary-simplify-subject-query ()
2849   "Query where the respool algorithm would put this article."
2850   (interactive)
2851   (gnus-summary-select-article)
2852   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2853
2854 (defun gnus-gather-threads-by-subject (threads)
2855   "Gather threads by looking at Subject headers."
2856   (if (not gnus-summary-make-false-root)
2857       threads
2858     (let ((hashtb (gnus-make-hashtable 1024))
2859           (prev threads)
2860           (result threads)
2861           subject hthread whole-subject)
2862       (while threads
2863         (setq subject (gnus-general-simplify-subject
2864                        (setq whole-subject (mail-header-subject
2865                                             (caar threads)))))
2866         (when subject
2867           (if (setq hthread (gnus-gethash subject hashtb))
2868               (progn
2869                 ;; We enter a dummy root into the thread, if we
2870                 ;; haven't done that already.
2871                 (unless (stringp (caar hthread))
2872                   (setcar hthread (list whole-subject (car hthread))))
2873                 ;; We add this new gathered thread to this gathered
2874                 ;; thread.
2875                 (setcdr (car hthread)
2876                         (nconc (cdar hthread) (list (car threads))))
2877                 ;; Remove it from the list of threads.
2878                 (setcdr prev (cdr threads))
2879                 (setq threads prev))
2880             ;; Enter this thread into the hash table.
2881             (gnus-sethash subject threads hashtb)))
2882         (setq prev threads)
2883         (setq threads (cdr threads)))
2884       result)))
2885
2886 (defun gnus-gather-threads-by-references (threads)
2887   "Gather threads by looking at References headers."
2888   (let ((idhashtb (gnus-make-hashtable 1024))
2889         (thhashtb (gnus-make-hashtable 1024))
2890         (prev threads)
2891         (result threads)
2892         ids references id gthread gid entered ref)
2893     (while threads
2894       (when (setq references (mail-header-references (caar threads)))
2895         (setq id (mail-header-id (caar threads))
2896               ids (gnus-split-references references)
2897               entered nil)
2898         (while (setq ref (pop ids))
2899           (setq ids (delete ref ids))
2900           (if (not (setq gid (gnus-gethash ref idhashtb)))
2901               (progn
2902                 (gnus-sethash ref id idhashtb)
2903                 (gnus-sethash id threads thhashtb))
2904             (setq gthread (gnus-gethash gid thhashtb))
2905             (unless entered
2906               ;; We enter a dummy root into the thread, if we
2907               ;; haven't done that already.
2908               (unless (stringp (caar gthread))
2909                 (setcar gthread (list (mail-header-subject (caar gthread))
2910                                       (car gthread))))
2911               ;; We add this new gathered thread to this gathered
2912               ;; thread.
2913               (setcdr (car gthread)
2914                       (nconc (cdar gthread) (list (car threads)))))
2915             ;; Add it into the thread hash table.
2916             (gnus-sethash id gthread thhashtb)
2917             (setq entered t)
2918             ;; Remove it from the list of threads.
2919             (setcdr prev (cdr threads))
2920             (setq threads prev))))
2921       (setq prev threads)
2922       (setq threads (cdr threads)))
2923     result))
2924
2925 (defun gnus-sort-gathered-threads (threads)
2926   "Sort subtreads inside each gathered thread by article number."
2927   (let ((result threads))
2928     (while threads
2929       (when (stringp (caar threads))
2930         (setcdr (car threads)
2931                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2932       (setq threads (cdr threads)))
2933     result))
2934
2935 (defun gnus-thread-loop-p (root thread)
2936   "Say whether ROOT is in THREAD."
2937   (let ((stack (list thread))
2938         (infloop 0)
2939         th)
2940     (while (setq thread (pop stack))
2941       (setq th (cdr thread))
2942       (while (and th
2943                   (not (eq (caar th) root)))
2944         (pop th))
2945       (if th
2946           ;; We have found a loop.
2947           (let (ref-dep)
2948             (setcdr thread (delq (car th) (cdr thread)))
2949             (if (boundp (setq ref-dep (intern "none"
2950                                               gnus-newsgroup-dependencies)))
2951                 (setcdr (symbol-value ref-dep)
2952                         (nconc (cdr (symbol-value ref-dep))
2953                                (list (car th))))
2954               (set ref-dep (list nil (car th))))
2955             (setq infloop 1
2956                   stack nil))
2957         ;; Push all the subthreads onto the stack.
2958         (push (cdr thread) stack)))
2959     infloop))
2960
2961 (defun gnus-make-threads ()
2962   "Go through the dependency hashtb and find the roots.  Return all threads."
2963   (let (threads)
2964     (while (catch 'infloop
2965              (mapatoms
2966               (lambda (refs)
2967                 ;; Deal with self-referencing References loops.
2968                 (when (and (car (symbol-value refs))
2969                            (not (zerop
2970                                  (apply
2971                                   '+
2972                                   (mapcar
2973                                    (lambda (thread)
2974                                      (gnus-thread-loop-p
2975                                       (car (symbol-value refs)) thread))
2976                                    (cdr (symbol-value refs)))))))
2977                   (setq threads nil)
2978                   (throw 'infloop t))
2979                 (unless (car (symbol-value refs))
2980                   ;; These threads do not refer back to any other articles,
2981                   ;; so they're roots.
2982                   (setq threads (append (cdr (symbol-value refs)) threads))))
2983               gnus-newsgroup-dependencies)))
2984     threads))
2985
2986 ;; Build the thread tree.
2987 (defsubst gnus-dependencies-add-header (header dependencies force-new)
2988   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2989
2990 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2991 if it was already present.
2992
2993 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2994 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2995 Message-IDs will be renamed be renamed to a unique Message-ID before
2996 being entered.
2997
2998 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2999   (let* ((id (mail-header-id header))
3000          (id-dep (and id (intern id dependencies)))
3001          ref ref-dep ref-header)
3002     ;; Enter this `header' in the `dependencies' table.
3003     (cond
3004      ((not id-dep)
3005       (setq header nil))
3006      ;; The first two cases do the normal part: enter a new `header'
3007      ;; in the `dependencies' table.
3008      ((not (boundp id-dep))
3009       (set id-dep (list header)))
3010      ((null (car (symbol-value id-dep)))
3011       (setcar (symbol-value id-dep) header))
3012
3013      ;; From here the `header' was already present in the
3014      ;; `dependencies' table.
3015      (force-new
3016       ;; Overrides an existing entry;
3017       ;; just set the header part of the entry.
3018       (setcar (symbol-value id-dep) header))
3019
3020      ;; Renames the existing `header' to a unique Message-ID.
3021      ((not gnus-summary-ignore-duplicates)
3022       ;; An article with this Message-ID has already been seen.
3023       ;; We rename the Message-ID.
3024       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3025            (list header))
3026       (mail-header-set-id header id))
3027
3028      ;; The last case ignores an existing entry, except it adds any
3029      ;; additional Xrefs (in case the two articles came from different
3030      ;; servers.
3031      ;; Also sets `header' to `nil' meaning that the `dependencies'
3032      ;; table was *not* modified.
3033      (t
3034       (mail-header-set-xref
3035        (car (symbol-value id-dep))
3036        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3037                    "")
3038                (or (mail-header-xref header) "")))
3039       (setq header nil)))
3040
3041     (when header
3042       ;; First check if that we are not creating a References loop.
3043       (setq ref (gnus-parent-id (mail-header-references header)))
3044       (while (and ref
3045                   (setq ref-dep (intern-soft ref dependencies))
3046                   (boundp ref-dep)
3047                   (setq ref-header (car (symbol-value ref-dep))))
3048         (if (string= id ref)
3049             ;; Yuk!  This is a reference loop.  Make the article be a
3050             ;; root article.
3051             (progn
3052               (mail-header-set-references (car (symbol-value id-dep)) "none")
3053               (setq ref nil))
3054           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3055       (setq ref (gnus-parent-id (mail-header-references header)))
3056       (setq ref-dep (intern (or ref "none") dependencies))
3057       (if (boundp ref-dep)
3058           (setcdr (symbol-value ref-dep)
3059                   (nconc (cdr (symbol-value ref-dep))
3060                          (list (symbol-value id-dep))))
3061         (set ref-dep (list nil (symbol-value id-dep)))))
3062     header))
3063
3064 (defun gnus-build-sparse-threads ()
3065   (let ((headers gnus-newsgroup-headers)
3066         (gnus-summary-ignore-duplicates t)
3067         header references generation relations
3068         subject child end new-child date)
3069     ;; First we create an alist of generations/relations, where
3070     ;; generations is how much we trust the relation, and the relation
3071     ;; is parent/child.
3072     (gnus-message 7 "Making sparse threads...")
3073     (save-excursion
3074       (nnheader-set-temp-buffer " *gnus sparse threads*")
3075       (while (setq header (pop headers))
3076         (when (and (setq references (mail-header-references header))
3077                    (not (string= references "")))
3078           (insert references)
3079           (setq child (mail-header-id header)
3080                 subject (mail-header-subject header)
3081                 date (mail-header-date header)
3082                 generation 0)
3083           (while (search-backward ">" nil t)
3084             (setq end (1+ (point)))
3085             (when (search-backward "<" nil t)
3086               (setq new-child (buffer-substring (point) end))
3087               (push (list (incf generation)
3088                           child (setq child new-child)
3089                           subject date)
3090                     relations)))
3091           (when child
3092             (push (list (1+ generation) child nil subject) relations))
3093           (erase-buffer)))
3094       (kill-buffer (current-buffer)))
3095     ;; Sort over trustworthiness.
3096     (mapcar
3097      (lambda (relation)
3098        (when (gnus-dependencies-add-header
3099               (make-full-mail-header
3100                gnus-reffed-article-number
3101                (nth 3 relation) "" (or (nth 4 relation) "")
3102                (nth 1 relation)
3103                (or (nth 2 relation) "") 0 0 "")
3104               gnus-newsgroup-dependencies nil)
3105          (push gnus-reffed-article-number gnus-newsgroup-limit)
3106          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3107          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3108                gnus-newsgroup-reads)
3109          (decf gnus-reffed-article-number)))
3110      (sort relations 'car-less-than-car))
3111     (gnus-message 7 "Making sparse threads...done")))
3112
3113 (defun gnus-build-old-threads ()
3114   ;; Look at all the articles that refer back to old articles, and
3115   ;; fetch the headers for the articles that aren't there.  This will
3116   ;; build complete threads - if the roots haven't been expired by the
3117   ;; server, that is.
3118   (let (id heads)
3119     (mapatoms
3120      (lambda (refs)
3121        (when (not (car (symbol-value refs)))
3122          (setq heads (cdr (symbol-value refs)))
3123          (while heads
3124            (if (memq (mail-header-number (caar heads))
3125                      gnus-newsgroup-dormant)
3126                (setq heads (cdr heads))
3127              (setq id (symbol-name refs))
3128              (while (and (setq id (gnus-build-get-header id))
3129                          (not (car (gnus-id-to-thread id)))))
3130              (setq heads nil)))))
3131      gnus-newsgroup-dependencies)))
3132
3133 (defmacro gnus-nov-read-integer ()
3134   '(prog1
3135        (if (eq (char-after) ?\t)
3136            0
3137          (let ((num (ignore-errors (read buffer))))
3138            (if (numberp num) num 0)))
3139      (unless (eobp)
3140        (search-forward "\t" eol 'move))))
3141
3142 (defmacro gnus-nov-skip-field ()
3143   '(search-forward "\t" eol 'move))
3144
3145 (defmacro gnus-nov-field ()
3146   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3147
3148 (defmacro gnus-nov-parse-extra ()
3149   '(let (out string)
3150      (while (not (memq (char-after) '(?\n nil)))
3151        (setq string (gnus-nov-field))
3152        (when (string-match "^\\([^ :]+\\): " string)
3153          (push (cons (intern (match-string 1 string))
3154                      (substring string (match-end 0)))
3155                out)))
3156      out))
3157
3158 ;; This function has to be called with point after the article number
3159 ;; on the beginning of the line.
3160 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3161   (let ((eol (gnus-point-at-eol))
3162         (buffer (current-buffer))
3163         header)
3164
3165     ;; overview: [num subject from date id refs chars lines misc]
3166     (unwind-protect
3167         (progn
3168           (narrow-to-region (point) eol)
3169           (unless (eobp)
3170             (forward-char))
3171
3172           (setq header
3173                 (make-full-mail-header
3174                  number                 ; number
3175                  (gnus-nov-field)       ; subject
3176                  (gnus-nov-field)       ; from
3177                  (gnus-nov-field)       ; date
3178                  (or (gnus-nov-field)
3179                      (nnheader-generate-fake-message-id)) ; id
3180                  (gnus-nov-field)       ; refs
3181                  (gnus-nov-read-integer) ; chars
3182                  (gnus-nov-read-integer) ; lines
3183                  (unless (eq (char-after) ?\n)
3184                    (gnus-nov-field))    ; misc
3185                  (gnus-nov-parse-extra)))) ; extra
3186
3187       (widen))
3188
3189     (when gnus-alter-header-function
3190       (funcall gnus-alter-header-function header))
3191     (gnus-dependencies-add-header header dependencies force-new)))
3192
3193 (defun gnus-build-get-header (id)
3194   ;; Look through the buffer of NOV lines and find the header to
3195   ;; ID.  Enter this line into the dependencies hash table, and return
3196   ;; the id of the parent article (if any).
3197   (let ((deps gnus-newsgroup-dependencies)
3198         found header)
3199     (prog1
3200         (save-excursion
3201           (set-buffer nntp-server-buffer)
3202           (let ((case-fold-search nil))
3203             (goto-char (point-min))
3204             (while (and (not found)
3205                         (search-forward id nil t))
3206               (beginning-of-line)
3207               (setq found (looking-at
3208                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3209                                    (regexp-quote id))))
3210               (or found (beginning-of-line 2)))
3211             (when found
3212               (beginning-of-line)
3213               (and
3214                (setq header (gnus-nov-parse-line
3215                              (read (current-buffer)) deps))
3216                (gnus-parent-id (mail-header-references header))))))
3217       (when header
3218         (let ((number (mail-header-number header)))
3219           (push number gnus-newsgroup-limit)
3220           (push header gnus-newsgroup-headers)
3221           (if (memq number gnus-newsgroup-unselected)
3222               (progn
3223                 (push number gnus-newsgroup-unreads)
3224                 (setq gnus-newsgroup-unselected
3225                       (delq number gnus-newsgroup-unselected)))
3226             (push number gnus-newsgroup-ancient)))))))
3227
3228 (defun gnus-build-all-threads ()
3229   "Read all the headers."
3230   (let ((gnus-summary-ignore-duplicates t)
3231         (dependencies gnus-newsgroup-dependencies)
3232         header article)
3233     (save-excursion
3234       (set-buffer nntp-server-buffer)
3235       (let ((case-fold-search nil))
3236         (goto-char (point-min))
3237         (while (not (eobp))
3238           (ignore-errors
3239             (setq article (read (current-buffer))
3240                   header (gnus-nov-parse-line
3241                           article dependencies)))
3242           (when header
3243             (save-excursion
3244               (set-buffer gnus-summary-buffer)
3245               (push header gnus-newsgroup-headers)
3246               (if (memq (setq article (mail-header-number header))
3247                         gnus-newsgroup-unselected)
3248                   (progn
3249                     (push article gnus-newsgroup-unreads)
3250                     (setq gnus-newsgroup-unselected
3251                           (delq article gnus-newsgroup-unselected)))
3252                 (push article gnus-newsgroup-ancient)))
3253             (forward-line 1)))))))
3254
3255 (defun gnus-summary-update-article-line (article header)
3256   "Update the line for ARTICLE using HEADERS."
3257   (let* ((id (mail-header-id header))
3258          (thread (gnus-id-to-thread id)))
3259     (unless thread
3260       (error "Article in no thread"))
3261     ;; Update the thread.
3262     (setcar thread header)
3263     (gnus-summary-goto-subject article)
3264     (let* ((datal (gnus-data-find-list article))
3265            (data (car datal))
3266            (length (when (cdr datal)
3267                      (- (gnus-data-pos data)
3268                         (gnus-data-pos (cadr datal)))))
3269            (buffer-read-only nil)
3270            (level (gnus-summary-thread-level)))
3271       (gnus-delete-line)
3272       (gnus-summary-insert-line
3273        header level nil (gnus-article-mark article)
3274        (memq article gnus-newsgroup-replied)
3275        (memq article gnus-newsgroup-expirable)
3276        ;; Only insert the Subject string when it's different
3277        ;; from the previous Subject string.
3278        (if (gnus-subject-equal
3279             (condition-case ()
3280                 (mail-header-subject
3281                  (gnus-data-header
3282                   (cadr
3283                    (gnus-data-find-list
3284                     article
3285                     (gnus-data-list t)))))
3286               ;; Error on the side of excessive subjects.
3287               (error ""))
3288             (mail-header-subject header))
3289            ""
3290          (mail-header-subject header))
3291        nil (cdr (assq article gnus-newsgroup-scored))
3292        (memq article gnus-newsgroup-processable))
3293       (when length
3294         (gnus-data-update-list
3295          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3296
3297 (defun gnus-summary-update-article (article &optional iheader)
3298   "Update ARTICLE in the summary buffer."
3299   (set-buffer gnus-summary-buffer)
3300   (let* ((header (gnus-summary-article-header article))
3301          (id (mail-header-id header))
3302          (data (gnus-data-find article))
3303          (thread (gnus-id-to-thread id))
3304          (references (mail-header-references header))
3305          (parent
3306           (gnus-id-to-thread
3307            (or (gnus-parent-id
3308                 (when (and references
3309                            (not (equal "" references)))
3310                   references))
3311                "none")))
3312          (buffer-read-only nil)
3313          (old (car thread)))
3314     (when thread
3315       (unless iheader
3316         (setcar thread nil)
3317         (when parent
3318           (delq thread parent)))
3319       (if (gnus-summary-insert-subject id header)
3320           ;; Set the (possibly) new article number in the data structure.
3321           (gnus-data-set-number data (gnus-id-to-article id))
3322         (setcar thread old)
3323         nil))))
3324
3325 (defun gnus-rebuild-thread (id &optional line)
3326   "Rebuild the thread containing ID.
3327 If LINE, insert the rebuilt thread starting on line LINE."
3328   (let ((buffer-read-only nil)
3329         old-pos current thread data)
3330     (if (not gnus-show-threads)
3331         (setq thread (list (car (gnus-id-to-thread id))))
3332       ;; Get the thread this article is part of.
3333       (setq thread (gnus-remove-thread id)))
3334     (setq old-pos (gnus-point-at-bol))
3335     (setq current (save-excursion
3336                     (and (zerop (forward-line -1))
3337                          (gnus-summary-article-number))))
3338     ;; If this is a gathered thread, we have to go some re-gathering.
3339     (when (stringp (car thread))
3340       (let ((subject (car thread))
3341             roots thr)
3342         (setq thread (cdr thread))
3343         (while thread
3344           (unless (memq (setq thr (gnus-id-to-thread
3345                                    (gnus-root-id
3346                                     (mail-header-id (caar thread)))))
3347                         roots)
3348             (push thr roots))
3349           (setq thread (cdr thread)))
3350         ;; We now have all (unique) roots.
3351         (if (= (length roots) 1)
3352             ;; All the loose roots are now one solid root.
3353             (setq thread (car roots))
3354           (setq thread (cons subject (gnus-sort-threads roots))))))
3355     (let (threads)
3356       ;; We then insert this thread into the summary buffer.
3357       (when line
3358         (goto-char (point-min))
3359         (forward-line (1- line)))
3360       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3361         (if gnus-show-threads
3362             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3363           (gnus-summary-prepare-unthreaded thread))
3364         (setq data (nreverse gnus-newsgroup-data))
3365         (setq threads gnus-newsgroup-threads))
3366       ;; We splice the new data into the data structure.
3367       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3368       ;;!!! then we want to insert at the beginning of the buffer.
3369       ;;!!! That happens to be true with Gnus now, but that may
3370       ;;!!! change in the future.  Perhaps.
3371       (gnus-data-enter-list
3372        (if line nil current) data (- (point) old-pos))
3373       (setq gnus-newsgroup-threads
3374             (nconc threads gnus-newsgroup-threads))
3375       (gnus-data-compute-positions))))
3376
3377 (defun gnus-number-to-header (number)
3378   "Return the header for article NUMBER."
3379   (let ((headers gnus-newsgroup-headers))
3380     (while (and headers
3381                 (not (= number (mail-header-number (car headers)))))
3382       (pop headers))
3383     (when headers
3384       (car headers))))
3385
3386 (defun gnus-parent-headers (in-headers &optional generation)
3387   "Return the headers of the GENERATIONeth parent of HEADERS."
3388   (unless generation
3389     (setq generation 1))
3390   (let ((parent t)
3391         (headers in-headers)
3392         references)
3393     (while (and parent
3394                 (not (zerop generation))
3395                 (setq references (mail-header-references headers)))
3396       (setq headers (if (and references
3397                              (setq parent (gnus-parent-id references)))
3398                         (car (gnus-id-to-thread parent))
3399                       nil))
3400       (decf generation))
3401     (and (not (eq headers in-headers))
3402          headers)))
3403
3404 (defun gnus-id-to-thread (id)
3405   "Return the (sub-)thread where ID appears."
3406   (gnus-gethash id gnus-newsgroup-dependencies))
3407
3408 (defun gnus-id-to-article (id)
3409   "Return the article number of ID."
3410   (let ((thread (gnus-id-to-thread id)))
3411     (when (and thread
3412                (car thread))
3413       (mail-header-number (car thread)))))
3414
3415 (defun gnus-id-to-header (id)
3416   "Return the article headers of ID."
3417   (car (gnus-id-to-thread id)))
3418
3419 (defun gnus-article-displayed-root-p (article)
3420   "Say whether ARTICLE is a root(ish) article."
3421   (let ((level (gnus-summary-thread-level article))
3422         (refs (mail-header-references  (gnus-summary-article-header article)))
3423         particle)
3424     (cond
3425      ((null level) nil)
3426      ((zerop level) t)
3427      ((null refs) t)
3428      ((null (gnus-parent-id refs)) t)
3429      ((and (= 1 level)
3430            (null (setq particle (gnus-id-to-article
3431                                  (gnus-parent-id refs))))
3432            (null (gnus-summary-thread-level particle)))))))
3433
3434 (defun gnus-root-id (id)
3435   "Return the id of the root of the thread where ID appears."
3436   (let (last-id prev)
3437     (while (and id (setq prev (car (gnus-id-to-thread id))))
3438       (setq last-id id
3439             id (gnus-parent-id (mail-header-references prev))))
3440     last-id))
3441
3442 (defun gnus-articles-in-thread (thread)
3443   "Return the list of articles in THREAD."
3444   (cons (mail-header-number (car thread))
3445         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3446
3447 (defun gnus-remove-thread (id &optional dont-remove)
3448   "Remove the thread that has ID in it."
3449   (let (headers thread last-id)
3450     ;; First go up in this thread until we find the root.
3451     (setq last-id (gnus-root-id id)
3452           headers (message-flatten-list (gnus-id-to-thread last-id)))
3453     ;; We have now found the real root of this thread.  It might have
3454     ;; been gathered into some loose thread, so we have to search
3455     ;; through the threads to find the thread we wanted.
3456     (let ((threads gnus-newsgroup-threads)
3457           sub)
3458       (while threads
3459         (setq sub (car threads))
3460         (if (stringp (car sub))
3461             ;; This is a gathered thread, so we look at the roots
3462             ;; below it to find whether this article is in this
3463             ;; gathered root.
3464             (progn
3465               (setq sub (cdr sub))
3466               (while sub
3467                 (when (member (caar sub) headers)
3468                   (setq thread (car threads)
3469                         threads nil
3470                         sub nil))
3471                 (setq sub (cdr sub))))
3472           ;; It's an ordinary thread, so we check it.
3473           (when (eq (car sub) (car headers))
3474             (setq thread sub
3475                   threads nil)))
3476         (setq threads (cdr threads)))
3477       ;; If this article is in no thread, then it's a root.
3478       (if thread
3479           (unless dont-remove
3480             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3481         (setq thread (gnus-id-to-thread last-id)))
3482       (when thread
3483         (prog1
3484             thread                      ; We return this thread.
3485           (unless dont-remove
3486             (if (stringp (car thread))
3487                 (progn
3488                   ;; If we use dummy roots, then we have to remove the
3489                   ;; dummy root as well.
3490                   (when (eq gnus-summary-make-false-root 'dummy)
3491                     ;; We go to the dummy root by going to
3492                     ;; the first sub-"thread", and then one line up.
3493                     (gnus-summary-goto-article
3494                      (mail-header-number (caadr thread)))
3495                     (forward-line -1)
3496                     (gnus-delete-line)
3497                     (gnus-data-compute-positions))
3498                   (setq thread (cdr thread))
3499                   (while thread
3500                     (gnus-remove-thread-1 (car thread))
3501                     (setq thread (cdr thread))))
3502               (gnus-summary-show-all-threads)
3503               (gnus-remove-thread-1 thread))))))))
3504
3505 (defun gnus-remove-thread-1 (thread)
3506   "Remove the thread THREAD recursively."
3507   (let ((number (mail-header-number (pop thread)))
3508         d)
3509     (setq thread (reverse thread))
3510     (while thread
3511       (gnus-remove-thread-1 (pop thread)))
3512     (when (setq d (gnus-data-find number))
3513       (goto-char (gnus-data-pos d))
3514       (gnus-data-remove
3515        number
3516        (- (gnus-point-at-bol)
3517           (prog1
3518               (1+ (gnus-point-at-eol))
3519             (gnus-delete-line)))))))
3520
3521 (defun gnus-sort-threads (threads)
3522   "Sort THREADS."
3523   (if (not gnus-thread-sort-functions)
3524       threads
3525     (gnus-message 8 "Sorting threads...")
3526     (prog1
3527         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3528       (gnus-message 8 "Sorting threads...done"))))
3529
3530 (defun gnus-sort-articles (articles)
3531   "Sort ARTICLES."
3532   (when gnus-article-sort-functions
3533     (gnus-message 7 "Sorting articles...")
3534     (prog1
3535         (setq gnus-newsgroup-headers
3536               (sort articles (gnus-make-sort-function
3537                               gnus-article-sort-functions)))
3538       (gnus-message 7 "Sorting articles...done"))))
3539
3540 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3541 (defmacro gnus-thread-header (thread)
3542   ;; Return header of first article in THREAD.
3543   ;; Note that THREAD must never, ever be anything else than a variable -
3544   ;; using some other form will lead to serious barfage.
3545   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3546   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3547   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3548         (vector thread) 2))
3549
3550 (defsubst gnus-article-sort-by-number (h1 h2)
3551   "Sort articles by article number."
3552   (< (mail-header-number h1)
3553      (mail-header-number h2)))
3554
3555 (defun gnus-thread-sort-by-number (h1 h2)
3556   "Sort threads by root article number."
3557   (gnus-article-sort-by-number
3558    (gnus-thread-header h1) (gnus-thread-header h2)))
3559
3560 (defsubst gnus-article-sort-by-lines (h1 h2)
3561   "Sort articles by article Lines header."
3562   (< (mail-header-lines h1)
3563      (mail-header-lines h2)))
3564
3565 (defun gnus-thread-sort-by-lines (h1 h2)
3566   "Sort threads by root article Lines header."
3567   (gnus-article-sort-by-lines
3568    (gnus-thread-header h1) (gnus-thread-header h2)))
3569
3570 (defsubst gnus-article-sort-by-author (h1 h2)
3571   "Sort articles by root author."
3572   (string-lessp
3573    (let ((addr (mime-read-field 'From h1)))
3574      (or (std11-full-name-string addr)
3575          (std11-address-string addr)
3576          ""))
3577    (let ((addr (mime-read-field 'From h2)))
3578      (or (std11-full-name-string addr)
3579          (std11-address-string addr)
3580          ""))
3581    ))
3582
3583 (defun gnus-thread-sort-by-author (h1 h2)
3584   "Sort threads by root author."
3585   (gnus-article-sort-by-author
3586    (gnus-thread-header h1)  (gnus-thread-header h2)))
3587
3588 (defsubst gnus-article-sort-by-subject (h1 h2)
3589   "Sort articles by root subject."
3590   (string-lessp
3591    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3592    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3593
3594 (defun gnus-thread-sort-by-subject (h1 h2)
3595   "Sort threads by root subject."
3596   (gnus-article-sort-by-subject
3597    (gnus-thread-header h1) (gnus-thread-header h2)))
3598
3599 (defsubst gnus-article-sort-by-date (h1 h2)
3600   "Sort articles by root article date."
3601   (time-less-p
3602    (gnus-date-get-time (mail-header-date h1))
3603    (gnus-date-get-time (mail-header-date h2))))
3604
3605 (defun gnus-thread-sort-by-date (h1 h2)
3606   "Sort threads by root article date."
3607   (gnus-article-sort-by-date
3608    (gnus-thread-header h1) (gnus-thread-header h2)))
3609
3610 (defsubst gnus-article-sort-by-score (h1 h2)
3611   "Sort articles by root article score.
3612 Unscored articles will be counted as having a score of zero."
3613   (> (or (cdr (assq (mail-header-number h1)
3614                     gnus-newsgroup-scored))
3615          gnus-summary-default-score 0)
3616      (or (cdr (assq (mail-header-number h2)
3617                     gnus-newsgroup-scored))
3618          gnus-summary-default-score 0)))
3619
3620 (defun gnus-thread-sort-by-score (h1 h2)
3621   "Sort threads by root article score."
3622   (gnus-article-sort-by-score
3623    (gnus-thread-header h1) (gnus-thread-header h2)))
3624
3625 (defun gnus-thread-sort-by-total-score (h1 h2)
3626   "Sort threads by the sum of all scores in the thread.
3627 Unscored articles will be counted as having a score of zero."
3628   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3629
3630 (defun gnus-thread-total-score (thread)
3631   ;;  This function find the total score of THREAD.
3632   (cond ((null thread)
3633          0)
3634         ((consp thread)
3635          (if (stringp (car thread))
3636              (apply gnus-thread-score-function 0
3637                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3638            (gnus-thread-total-score-1 thread)))
3639         (t
3640          (gnus-thread-total-score-1 (list thread)))))
3641
3642 (defun gnus-thread-total-score-1 (root)
3643   ;; This function find the total score of the thread below ROOT.
3644   (setq root (car root))
3645   (apply gnus-thread-score-function
3646          (or (append
3647               (mapcar 'gnus-thread-total-score
3648                       (cdr (gnus-id-to-thread (mail-header-id root))))
3649               (when (> (mail-header-number root) 0)
3650                 (list (or (cdr (assq (mail-header-number root)
3651                                      gnus-newsgroup-scored))
3652                           gnus-summary-default-score 0))))
3653              (list gnus-summary-default-score)
3654              '(0))))
3655
3656 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3657 (defvar gnus-tmp-prev-subject nil)
3658 (defvar gnus-tmp-false-parent nil)
3659 (defvar gnus-tmp-root-expunged nil)
3660 (defvar gnus-tmp-dummy-line nil)
3661
3662 (defvar gnus-tmp-header)
3663 (defun gnus-extra-header (type &optional header)
3664   "Return the extra header of TYPE."
3665   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3666       ""))
3667
3668 (defun gnus-summary-prepare-threads (threads)
3669   "Prepare summary buffer from THREADS and indentation LEVEL.
3670 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3671 or a straight list of headers."
3672   (gnus-message 7 "Generating summary...")
3673
3674   (setq gnus-newsgroup-threads threads)
3675   (beginning-of-line)
3676
3677   (let ((gnus-tmp-level 0)
3678         (default-score (or gnus-summary-default-score 0))
3679         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3680         thread number subject stack state gnus-tmp-gathered beg-match
3681         new-roots gnus-tmp-new-adopts thread-end
3682         gnus-tmp-header gnus-tmp-unread
3683         gnus-tmp-replied gnus-tmp-subject-or-nil
3684         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3685         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3686         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3687
3688     (setq gnus-tmp-prev-subject nil)
3689
3690     (if (vectorp (car threads))
3691         ;; If this is a straight (sic) list of headers, then a
3692         ;; threaded summary display isn't required, so we just create
3693         ;; an unthreaded one.
3694         (gnus-summary-prepare-unthreaded threads)
3695
3696       ;; Do the threaded display.
3697
3698       (while (or threads stack gnus-tmp-new-adopts new-roots)
3699
3700         (if (and (= gnus-tmp-level 0)
3701                  (or (not stack)
3702                      (= (caar stack) 0))
3703                  (not gnus-tmp-false-parent)
3704                  (or gnus-tmp-new-adopts new-roots))
3705             (if gnus-tmp-new-adopts
3706                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3707                       thread (list (car gnus-tmp-new-adopts))
3708                       gnus-tmp-header (caar thread)
3709                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3710               (when new-roots
3711                 (setq thread (list (car new-roots))
3712                       gnus-tmp-header (caar thread)
3713                       new-roots (cdr new-roots))))
3714
3715           (if threads
3716               ;; If there are some threads, we do them before the
3717               ;; threads on the stack.
3718               (setq thread threads
3719                     gnus-tmp-header (caar thread))
3720             ;; There were no current threads, so we pop something off
3721             ;; the stack.
3722             (setq state (car stack)
3723                   gnus-tmp-level (car state)
3724                   thread (cdr state)
3725                   stack (cdr stack)
3726                   gnus-tmp-header (caar thread))))
3727
3728         (setq gnus-tmp-false-parent nil)
3729         (setq gnus-tmp-root-expunged nil)
3730         (setq thread-end nil)
3731
3732         (if (stringp gnus-tmp-header)
3733             ;; The header is a dummy root.
3734             (cond
3735              ((eq gnus-summary-make-false-root 'adopt)
3736               ;; We let the first article adopt the rest.
3737               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3738                                                (cddar thread)))
3739               (setq gnus-tmp-gathered
3740                     (nconc (mapcar
3741                             (lambda (h) (mail-header-number (car h)))
3742                             (cddar thread))
3743                            gnus-tmp-gathered))
3744               (setq thread (cons (list (caar thread)
3745                                        (cadar thread))
3746                                  (cdr thread)))
3747               (setq gnus-tmp-level -1
3748                     gnus-tmp-false-parent t))
3749              ((eq gnus-summary-make-false-root 'empty)
3750               ;; We print adopted articles with empty subject fields.
3751               (setq gnus-tmp-gathered
3752                     (nconc (mapcar
3753                             (lambda (h) (mail-header-number (car h)))
3754                             (cddar thread))
3755                            gnus-tmp-gathered))
3756               (setq gnus-tmp-level -1))
3757              ((eq gnus-summary-make-false-root 'dummy)
3758               ;; We remember that we probably want to output a dummy
3759               ;; root.
3760               (setq gnus-tmp-dummy-line gnus-tmp-header)
3761               (setq gnus-tmp-prev-subject gnus-tmp-header))
3762              (t
3763               ;; We do not make a root for the gathered
3764               ;; sub-threads at all.
3765               (setq gnus-tmp-level -1)))
3766
3767           (setq number (mail-header-number gnus-tmp-header)
3768                 subject (mail-header-subject gnus-tmp-header))
3769
3770           (cond
3771            ;; If the thread has changed subject, we might want to make
3772            ;; this subthread into a root.
3773            ((and (null gnus-thread-ignore-subject)
3774                  (not (zerop gnus-tmp-level))
3775                  gnus-tmp-prev-subject
3776                  (not (inline
3777                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3778             (setq new-roots (nconc new-roots (list (car thread)))
3779                   thread-end t
3780                   gnus-tmp-header nil))
3781            ;; If the article lies outside the current limit,
3782            ;; then we do not display it.
3783            ((not (memq number gnus-newsgroup-limit))
3784             (setq gnus-tmp-gathered
3785                   (nconc (mapcar
3786                           (lambda (h) (mail-header-number (car h)))
3787                           (cdar thread))
3788                          gnus-tmp-gathered))
3789             (setq gnus-tmp-new-adopts (if (cdar thread)
3790                                           (append gnus-tmp-new-adopts
3791                                                   (cdar thread))
3792                                         gnus-tmp-new-adopts)
3793                   thread-end t
3794                   gnus-tmp-header nil)
3795             (when (zerop gnus-tmp-level)
3796               (setq gnus-tmp-root-expunged t)))
3797            ;; Perhaps this article is to be marked as read?
3798            ((and gnus-summary-mark-below
3799                  (< (or (cdr (assq number gnus-newsgroup-scored))
3800                         default-score)
3801                     gnus-summary-mark-below)
3802                  ;; Don't touch sparse articles.
3803                  (not (gnus-summary-article-sparse-p number))
3804                  (not (gnus-summary-article-ancient-p number)))
3805             (setq gnus-newsgroup-unreads
3806                   (delq number gnus-newsgroup-unreads))
3807             (if gnus-newsgroup-auto-expire
3808                 (push number gnus-newsgroup-expirable)
3809               (push (cons number gnus-low-score-mark)
3810                     gnus-newsgroup-reads))))
3811
3812           (when gnus-tmp-header
3813             ;; We may have an old dummy line to output before this
3814             ;; article.
3815             (when (and gnus-tmp-dummy-line
3816                        (gnus-subject-equal
3817                         gnus-tmp-dummy-line
3818                         (mail-header-subject gnus-tmp-header)))
3819               (gnus-summary-insert-dummy-line
3820                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3821               (setq gnus-tmp-dummy-line nil))
3822
3823             ;; Compute the mark.
3824             (setq gnus-tmp-unread (gnus-article-mark number))
3825
3826             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3827                                   gnus-tmp-header gnus-tmp-level)
3828                   gnus-newsgroup-data)
3829
3830             ;; Actually insert the line.
3831             (setq
3832              gnus-tmp-subject-or-nil
3833              (cond
3834               ((and gnus-thread-ignore-subject
3835                     gnus-tmp-prev-subject
3836                     (not (inline (gnus-subject-equal
3837                                   gnus-tmp-prev-subject subject))))
3838                subject)
3839               ((zerop gnus-tmp-level)
3840                (if (and (eq gnus-summary-make-false-root 'empty)
3841                         (memq number gnus-tmp-gathered)
3842                         gnus-tmp-prev-subject
3843                         (inline (gnus-subject-equal
3844                                  gnus-tmp-prev-subject subject)))
3845                    gnus-summary-same-subject
3846                  subject))
3847               (t gnus-summary-same-subject)))
3848             (if (and (eq gnus-summary-make-false-root 'adopt)
3849                      (= gnus-tmp-level 1)
3850                      (memq number gnus-tmp-gathered))
3851                 (setq gnus-tmp-opening-bracket ?\<
3852                       gnus-tmp-closing-bracket ?\>)
3853               (setq gnus-tmp-opening-bracket ?\[
3854                     gnus-tmp-closing-bracket ?\]))
3855             (setq
3856              gnus-tmp-indentation
3857              (aref gnus-thread-indent-array gnus-tmp-level)
3858              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3859              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3860                                 gnus-summary-default-score 0)
3861              gnus-tmp-score-char
3862              (if (or (null gnus-summary-default-score)
3863                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3864                          gnus-summary-zcore-fuzz))
3865                  ? 
3866                (if (< gnus-tmp-score gnus-summary-default-score)
3867                    gnus-score-below-mark gnus-score-over-mark))
3868              gnus-tmp-replied
3869              (cond ((memq number gnus-newsgroup-processable)
3870                     gnus-process-mark)
3871                    ((memq number gnus-newsgroup-cached)
3872                     gnus-cached-mark)
3873                    ((memq number gnus-newsgroup-replied)
3874                     gnus-replied-mark)
3875                    ((memq number gnus-newsgroup-saved)
3876                     gnus-saved-mark)
3877                    (t gnus-unread-mark))
3878              gnus-tmp-from (mail-header-from gnus-tmp-header)
3879              gnus-tmp-name
3880              (cond
3881               ((string-match "<[^>]+> *$" gnus-tmp-from)
3882                (setq beg-match (match-beginning 0))
3883                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3884                         (substring gnus-tmp-from (1+ (match-beginning 0))
3885                                    (1- (match-end 0))))
3886                    (substring gnus-tmp-from 0 beg-match)))
3887               ((string-match "(.+)" gnus-tmp-from)
3888                (substring gnus-tmp-from
3889                           (1+ (match-beginning 0)) (1- (match-end 0))))
3890               (t gnus-tmp-from)))
3891             (when (string= gnus-tmp-name "")
3892               (setq gnus-tmp-name gnus-tmp-from))
3893             (unless (numberp gnus-tmp-lines)
3894               (setq gnus-tmp-lines 0))
3895             (gnus-put-text-property
3896              (point)
3897              (progn (eval gnus-summary-line-format-spec) (point))
3898              'gnus-number number)
3899             (when gnus-visual-p
3900               (forward-line -1)
3901               (gnus-run-hooks 'gnus-summary-update-hook)
3902               (forward-line 1))
3903
3904             (setq gnus-tmp-prev-subject subject)))
3905
3906         (when (nth 1 thread)
3907           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3908         (incf gnus-tmp-level)
3909         (setq threads (if thread-end nil (cdar thread)))
3910         (unless threads
3911           (setq gnus-tmp-level 0)))))
3912   (gnus-message 7 "Generating summary...done"))
3913
3914 (defun gnus-summary-prepare-unthreaded (headers)
3915   "Generate an unthreaded summary buffer based on HEADERS."
3916   (let (header number mark)
3917
3918     (beginning-of-line)
3919
3920     (while headers
3921       ;; We may have to root out some bad articles...
3922       (when (memq (setq number (mail-header-number
3923                                 (setq header (pop headers))))
3924                   gnus-newsgroup-limit)
3925         ;; Mark article as read when it has a low score.
3926         (when (and gnus-summary-mark-below
3927                    (< (or (cdr (assq number gnus-newsgroup-scored))
3928                           gnus-summary-default-score 0)
3929                       gnus-summary-mark-below)
3930                    (not (gnus-summary-article-ancient-p number)))
3931           (setq gnus-newsgroup-unreads
3932                 (delq number gnus-newsgroup-unreads))
3933           (if gnus-newsgroup-auto-expire
3934               (push number gnus-newsgroup-expirable)
3935             (push (cons number gnus-low-score-mark)
3936                   gnus-newsgroup-reads)))
3937
3938         (setq mark (gnus-article-mark number))
3939         (push (gnus-data-make number mark (1+ (point)) header 0)
3940               gnus-newsgroup-data)
3941         (gnus-summary-insert-line
3942          header 0 number
3943          mark (memq number gnus-newsgroup-replied)
3944          (memq number gnus-newsgroup-expirable)
3945          (mail-header-subject header) nil
3946          (cdr (assq number gnus-newsgroup-scored))
3947          (memq number gnus-newsgroup-processable))))))
3948
3949 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3950   "Select newsgroup GROUP.
3951 If READ-ALL is non-nil, all articles in the group are selected.
3952 If SELECT-ARTICLES, only select those articles from GROUP."
3953   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3954          ;;!!! Dirty hack; should be removed.
3955          (gnus-summary-ignore-duplicates
3956           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3957               t
3958             gnus-summary-ignore-duplicates))
3959          (info (nth 2 entry))
3960          articles fetched-articles cached)
3961
3962     (unless (gnus-check-server
3963              (setq gnus-current-select-method
3964                    (gnus-find-method-for-group group)))
3965       (error "Couldn't open server"))
3966
3967     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3968         (gnus-activate-group group)     ; Or we can activate it...
3969         (progn                          ; Or we bug out.
3970           (when (equal major-mode 'gnus-summary-mode)
3971             (kill-buffer (current-buffer)))
3972           (error "Couldn't request group %s: %s"
3973                  group (gnus-status-message group))))
3974
3975     (unless (gnus-request-group group t)
3976       (when (equal major-mode 'gnus-summary-mode)
3977         (kill-buffer (current-buffer)))
3978       (error "Couldn't request group %s: %s"
3979              group (gnus-status-message group)))
3980
3981     (setq gnus-newsgroup-name group)
3982     (setq gnus-newsgroup-unselected nil)
3983     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3984
3985     ;; Adjust and set lists of article marks.
3986     (when info
3987       (gnus-adjust-marked-articles info))
3988
3989     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3990     (when (gnus-virtual-group-p group)
3991       (setq cached gnus-newsgroup-cached))
3992
3993     (setq gnus-newsgroup-unreads
3994           (gnus-set-difference
3995            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3996            gnus-newsgroup-dormant))
3997
3998     (setq gnus-newsgroup-processable nil)
3999
4000     (gnus-update-read-articles group gnus-newsgroup-unreads)
4001
4002     (if (setq articles select-articles)
4003         (setq gnus-newsgroup-unselected
4004               (gnus-sorted-intersection
4005                gnus-newsgroup-unreads
4006                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4007       (setq articles (gnus-articles-to-read group read-all)))
4008
4009     (cond
4010      ((null articles)
4011       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4012       'quit)
4013      ((eq articles 0) nil)
4014      (t
4015       ;; Init the dependencies hash table.
4016       (setq gnus-newsgroup-dependencies
4017             (gnus-make-hashtable (length articles)))
4018       (gnus-set-global-variables)
4019       ;; Retrieve the headers and read them in.
4020       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4021       (setq gnus-newsgroup-headers
4022             (if (eq 'nov
4023                     (setq gnus-headers-retrieved-by
4024                           (gnus-retrieve-headers
4025                            articles gnus-newsgroup-name
4026                            ;; We might want to fetch old headers, but
4027                            ;; not if there is only 1 article.
4028                            (and (or (and
4029                                      (not (eq gnus-fetch-old-headers 'some))
4030                                      (not (numberp gnus-fetch-old-headers)))
4031                                     (> (length articles) 1))
4032                                 gnus-fetch-old-headers))))
4033                 (gnus-get-newsgroup-headers-xover
4034                  articles nil nil gnus-newsgroup-name t)
4035               (gnus-get-newsgroup-headers)))
4036       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4037
4038       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4039       (when cached
4040         (setq gnus-newsgroup-cached cached))
4041
4042       ;; Suppress duplicates?
4043       (when gnus-suppress-duplicates
4044         (gnus-dup-suppress-articles))
4045
4046       ;; Set the initial limit.
4047       (setq gnus-newsgroup-limit (copy-sequence articles))
4048       ;; Remove canceled articles from the list of unread articles.
4049       (setq gnus-newsgroup-unreads
4050             (gnus-set-sorted-intersection
4051              gnus-newsgroup-unreads
4052              (setq fetched-articles
4053                    (mapcar (lambda (headers) (mail-header-number headers))
4054                            gnus-newsgroup-headers))))
4055       ;; Removed marked articles that do not exist.
4056       (gnus-update-missing-marks
4057        (gnus-sorted-complement fetched-articles articles))
4058       ;; We might want to build some more threads first.
4059       (when (and gnus-fetch-old-headers
4060                  (eq gnus-headers-retrieved-by 'nov))
4061         (if (eq gnus-fetch-old-headers 'invisible)
4062             (gnus-build-all-threads)
4063           (gnus-build-old-threads)))
4064       ;; Let the Gnus agent mark articles as read.
4065       (when gnus-agent
4066         (gnus-agent-get-undownloaded-list))
4067       ;; Check whether auto-expire is to be done in this group.
4068       (setq gnus-newsgroup-auto-expire
4069             (gnus-group-auto-expirable-p group))
4070       ;; Set up the article buffer now, if necessary.
4071       (unless gnus-single-article-buffer
4072         (gnus-article-setup-buffer))
4073       ;; First and last article in this newsgroup.
4074       (when gnus-newsgroup-headers
4075         (setq gnus-newsgroup-begin
4076               (mail-header-number (car gnus-newsgroup-headers))
4077               gnus-newsgroup-end
4078               (mail-header-number
4079                (gnus-last-element gnus-newsgroup-headers))))
4080       ;; GROUP is successfully selected.
4081       (or gnus-newsgroup-headers t)))))
4082
4083 (defun gnus-articles-to-read (group &optional read-all)
4084   ;; Find out what articles the user wants to read.
4085   (let* ((articles
4086           ;; Select all articles if `read-all' is non-nil, or if there
4087           ;; are no unread articles.
4088           (if (or read-all
4089                   (and (zerop (length gnus-newsgroup-marked))
4090                        (zerop (length gnus-newsgroup-unreads)))
4091                   (eq (gnus-group-find-parameter group 'display)
4092                       'all))
4093               (gnus-uncompress-range (gnus-active group))
4094             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4095                           (copy-sequence gnus-newsgroup-unreads))
4096                   '<)))
4097          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4098          (scored (length scored-list))
4099          (number (length articles))
4100          (marked (+ (length gnus-newsgroup-marked)
4101                     (length gnus-newsgroup-dormant)))
4102          (select
4103           (cond
4104            ((numberp read-all)
4105             read-all)
4106            (t
4107             (condition-case ()
4108                 (cond
4109                  ((and (or (<= scored marked) (= scored number))
4110                        (numberp gnus-large-newsgroup)
4111                        (> number gnus-large-newsgroup))
4112                   (let ((input
4113                          (read-string
4114                           (format
4115                            "How many articles from %s (default %d): "
4116                            (gnus-limit-string gnus-newsgroup-name 35)
4117                            number))))
4118                     (if (string-match "^[ \t]*$" input) number input)))
4119                  ((and (> scored marked) (< scored number)
4120                        (> (- scored number) 20))
4121                   (let ((input
4122                          (read-string
4123                           (format "%s %s (%d scored, %d total): "
4124                                   "How many articles from"
4125                                   group scored number))))
4126                     (if (string-match "^[ \t]*$" input)
4127                         number input)))
4128                  (t number))
4129               (quit nil))))))
4130     (setq select (if (stringp select) (string-to-number select) select))
4131     (if (or (null select) (zerop select))
4132         select
4133       (if (and (not (zerop scored)) (<= (abs select) scored))
4134           (progn
4135             (setq articles (sort scored-list '<))
4136             (setq number (length articles)))
4137         (setq articles (copy-sequence articles)))
4138
4139       (when (< (abs select) number)
4140         (if (< select 0)
4141             ;; Select the N oldest articles.
4142             (setcdr (nthcdr (1- (abs select)) articles) nil)
4143           ;; Select the N most recent articles.
4144           (setq articles (nthcdr (- number select) articles))))
4145       (setq gnus-newsgroup-unselected
4146             (gnus-sorted-intersection
4147              gnus-newsgroup-unreads
4148              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4149       articles)))
4150
4151 (defun gnus-killed-articles (killed articles)
4152   (let (out)
4153     (while articles
4154       (when (inline (gnus-member-of-range (car articles) killed))
4155         (push (car articles) out))
4156       (setq articles (cdr articles)))
4157     out))
4158
4159 (defun gnus-uncompress-marks (marks)
4160   "Uncompress the mark ranges in MARKS."
4161   (let ((uncompressed '(score bookmark))
4162         out)
4163     (while marks
4164       (if (memq (caar marks) uncompressed)
4165           (push (car marks) out)
4166         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4167       (setq marks (cdr marks)))
4168     out))
4169
4170 (defun gnus-adjust-marked-articles (info)
4171   "Set all article lists and remove all marks that are no longer legal."
4172   (let* ((marked-lists (gnus-info-marks info))
4173          (active (gnus-active (gnus-info-group info)))
4174          (min (car active))
4175          (max (cdr active))
4176          (types gnus-article-mark-lists)
4177          (uncompressed '(score bookmark killed))
4178          marks var articles article mark)
4179
4180     (while marked-lists
4181       (setq marks (pop marked-lists))
4182       (set (setq var (intern (format "gnus-newsgroup-%s"
4183                                      (car (rassq (setq mark (car marks))
4184                                                  types)))))
4185            (if (memq (car marks) uncompressed) (cdr marks)
4186              (gnus-uncompress-range (cdr marks))))
4187
4188       (setq articles (symbol-value var))
4189
4190       ;; All articles have to be subsets of the active articles.
4191       (cond
4192        ;; Adjust "simple" lists.
4193        ((memq mark '(tick dormant expire reply save))
4194         (while articles
4195           (when (or (< (setq article (pop articles)) min) (> article max))
4196             (set var (delq article (symbol-value var))))))
4197        ;; Adjust assocs.
4198        ((memq mark uncompressed)
4199         (when (not (listp (cdr (symbol-value var))))
4200           (set var (list (symbol-value var))))
4201         (when (not (listp (cdr articles)))
4202           (setq articles (list articles)))
4203         (while articles
4204           (when (or (not (consp (setq article (pop articles))))
4205                     (< (car article) min)
4206                     (> (car article) max))
4207             (set var (delq article (symbol-value var))))))))))
4208
4209 (defun gnus-update-missing-marks (missing)
4210   "Go through the list of MISSING articles and remove them from the mark lists."
4211   (when missing
4212     (let ((types gnus-article-mark-lists)
4213           var m)
4214       ;; Go through all types.
4215       (while types
4216         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4217         (when (symbol-value var)
4218           ;; This list has articles.  So we delete all missing articles
4219           ;; from it.
4220           (setq m missing)
4221           (while m
4222             (set var (delq (pop m) (symbol-value var)))))))))
4223
4224 (defun gnus-update-marks ()
4225   "Enter the various lists of marked articles into the newsgroup info list."
4226   (let ((types gnus-article-mark-lists)
4227         (info (gnus-get-info gnus-newsgroup-name))
4228         (uncompressed '(score bookmark killed))
4229         type list newmarked symbol delta-marks)
4230     (when info
4231       ;; Add all marks lists that are non-nil to the list of marks lists.
4232       (while (setq type (pop types))
4233         (when (setq list (symbol-value
4234                           (setq symbol
4235                                 (intern (format "gnus-newsgroup-%s"
4236                                                 (car type))))))
4237
4238           ;; Get rid of the entries of the articles that have the
4239           ;; default score.
4240           (when (and (eq (cdr type) 'score)
4241                      gnus-save-score
4242                      list)
4243             (let* ((arts list)
4244                    (prev (cons nil list))
4245                    (all prev))
4246               (while arts
4247                 (if (or (not (consp (car arts)))
4248                         (= (cdar arts) gnus-summary-default-score))
4249                     (setcdr prev (cdr arts))
4250                   (setq prev arts))
4251                 (setq arts (cdr arts)))
4252               (setq list (cdr all))))
4253
4254           (push (cons (cdr type)
4255                       (if (memq (cdr type) uncompressed) list
4256                         (gnus-compress-sequence
4257                          (set symbol (sort list '<)) t)))
4258                 newmarked)))
4259
4260       ;; Enter these new marks into the info of the group.
4261       (if (nthcdr 3 info)
4262           (setcar (nthcdr 3 info) newmarked)
4263         ;; Add the marks lists to the end of the info.
4264         (when newmarked
4265           (setcdr (nthcdr 2 info) (list newmarked))))
4266
4267       ;; Cut off the end of the info if there's nothing else there.
4268       (let ((i 5))
4269         (while (and (> i 2)
4270                     (not (nth i info)))
4271           (when (nthcdr (decf i) info)
4272             (setcdr (nthcdr i info) nil)))))))
4273
4274 (defun gnus-set-mode-line (where)
4275   "This function sets the mode line of the article or summary buffers.
4276 If WHERE is `summary', the summary mode line format will be used."
4277   ;; Is this mode line one we keep updated?
4278   (when (and (memq where gnus-updated-mode-lines)
4279              (symbol-value
4280               (intern (format "gnus-%s-mode-line-format-spec" where))))
4281     (let (mode-string)
4282       (save-excursion
4283         ;; We evaluate this in the summary buffer since these
4284         ;; variables are buffer-local to that buffer.
4285         (set-buffer gnus-summary-buffer)
4286         ;; We bind all these variables that are used in the `eval' form
4287         ;; below.
4288         (let* ((mformat (symbol-value
4289                          (intern
4290                           (format "gnus-%s-mode-line-format-spec" where))))
4291                (gnus-tmp-group-name gnus-newsgroup-name)
4292                (gnus-tmp-article-number (or gnus-current-article 0))
4293                (gnus-tmp-unread gnus-newsgroup-unreads)
4294                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4295                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4296                (gnus-tmp-unread-and-unselected
4297                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4298                             (zerop gnus-tmp-unselected))
4299                        "")
4300                       ((zerop gnus-tmp-unselected)
4301                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4302                       (t (format "{%d(+%d) more}"
4303                                  gnus-tmp-unread-and-unticked
4304                                  gnus-tmp-unselected))))
4305                (gnus-tmp-subject
4306                 (if (and gnus-current-headers
4307                          (vectorp gnus-current-headers))
4308                     (gnus-mode-string-quote
4309                      (mail-header-subject gnus-current-headers))
4310                   ""))
4311                bufname-length max-len
4312                gnus-tmp-header);; passed as argument to any user-format-funcs
4313           (setq mode-string (eval mformat))
4314           (setq bufname-length (if (string-match "%b" mode-string)
4315                                    (- (length
4316                                        (buffer-name
4317                                         (if (eq where 'summary)
4318                                             nil
4319                                           (get-buffer gnus-article-buffer))))
4320                                       2)
4321                                  0))
4322           (setq max-len (max 4 (if gnus-mode-non-string-length
4323                                    (- (window-width)
4324                                       gnus-mode-non-string-length
4325                                       bufname-length)
4326                                  (length mode-string))))
4327           ;; We might have to chop a bit of the string off...
4328           (when (> (length mode-string) max-len)
4329             (setq mode-string
4330                   (concat (gnus-truncate-string mode-string (- max-len 3))
4331                           "...")))
4332           ;; Pad the mode string a bit.
4333           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4334       ;; Update the mode line.
4335       (setq mode-line-buffer-identification
4336             (gnus-mode-line-buffer-identification (list mode-string)))
4337       (set-buffer-modified-p t))))
4338
4339 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4340   "Go through the HEADERS list and add all Xrefs to a hash table.
4341 The resulting hash table is returned, or nil if no Xrefs were found."
4342   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4343          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4344          (xref-hashtb (gnus-make-hashtable))
4345          start group entry number xrefs header)
4346     (while headers
4347       (setq header (pop headers))
4348       (when (and (setq xrefs (mail-header-xref header))
4349                  (not (memq (setq number (mail-header-number header))
4350                             unreads)))
4351         (setq start 0)
4352         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4353           (setq start (match-end 0))
4354           (setq group (if prefix
4355                           (concat prefix (substring xrefs (match-beginning 1)
4356                                                     (match-end 1)))
4357                         (substring xrefs (match-beginning 1) (match-end 1))))
4358           (setq number
4359                 (string-to-int (substring xrefs (match-beginning 2)
4360                                           (match-end 2))))
4361           (if (setq entry (gnus-gethash group xref-hashtb))
4362               (setcdr entry (cons number (cdr entry)))
4363             (gnus-sethash group (cons number nil) xref-hashtb)))))
4364     (and start xref-hashtb)))
4365
4366 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4367   "Look through all the headers and mark the Xrefs as read."
4368   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4369         name entry info xref-hashtb idlist method nth4)
4370     (save-excursion
4371       (set-buffer gnus-group-buffer)
4372       (when (setq xref-hashtb
4373                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4374         (mapatoms
4375          (lambda (group)
4376            (unless (string= from-newsgroup (setq name (symbol-name group)))
4377              (setq idlist (symbol-value group))
4378              ;; Dead groups are not updated.
4379              (and (prog1
4380                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4381                             info (nth 2 entry))
4382                     (when (stringp (setq nth4 (gnus-info-method info)))
4383                       (setq nth4 (gnus-server-to-method nth4))))
4384                   ;; Only do the xrefs if the group has the same
4385                   ;; select method as the group we have just read.
4386                   (or (gnus-methods-equal-p
4387                        nth4 (gnus-find-method-for-group from-newsgroup))
4388                       virtual
4389                       (equal nth4 (setq method (gnus-find-method-for-group
4390                                                 from-newsgroup)))
4391                       (and (equal (car nth4) (car method))
4392                            (equal (nth 1 nth4) (nth 1 method))))
4393                   gnus-use-cross-reference
4394                   (or (not (eq gnus-use-cross-reference t))
4395                       virtual
4396                       ;; Only do cross-references on subscribed
4397                       ;; groups, if that is what is wanted.
4398                       (<= (gnus-info-level info) gnus-level-subscribed))
4399                   (gnus-group-make-articles-read name idlist))))
4400          xref-hashtb)))))
4401
4402 (defun gnus-compute-read-articles (group articles)
4403   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4404          (info (nth 2 entry))
4405          (active (gnus-active group))
4406          ninfo)
4407     (when entry
4408       ;; First peel off all illegal article numbers.
4409       (when active
4410         (let ((ids articles)
4411               id first)
4412           (while (setq id (pop ids))
4413             (when (and first (> id (cdr active)))
4414               ;; We'll end up in this situation in one particular
4415               ;; obscure situation.  If you re-scan a group and get
4416               ;; a new article that is cross-posted to a different
4417               ;; group that has not been re-scanned, you might get
4418               ;; crossposted article that has a higher number than
4419               ;; Gnus believes possible.  So we re-activate this
4420               ;; group as well.  This might mean doing the
4421               ;; crossposting thingy will *increase* the number
4422               ;; of articles in some groups.  Tsk, tsk.
4423               (setq active (or (gnus-activate-group group) active)))
4424             (when (or (> id (cdr active))
4425                       (< id (car active)))
4426               (setq articles (delq id articles))))))
4427       ;; If the read list is nil, we init it.
4428       (if (and active
4429                (null (gnus-info-read info))
4430                (> (car active) 1))
4431           (setq ninfo (cons 1 (1- (car active))))
4432         (setq ninfo (gnus-info-read info)))
4433       ;; Then we add the read articles to the range.
4434       (gnus-add-to-range
4435        ninfo (setq articles (sort articles '<))))))
4436
4437 (defun gnus-group-make-articles-read (group articles)
4438   "Update the info of GROUP to say that ARTICLES are read."
4439   (let* ((num 0)
4440          (entry (gnus-gethash group gnus-newsrc-hashtb))
4441          (info (nth 2 entry))
4442          (active (gnus-active group))
4443          range)
4444     (when entry
4445       (setq range (gnus-compute-read-articles group articles))
4446       (save-excursion
4447         (set-buffer gnus-group-buffer)
4448         (gnus-undo-register
4449           `(progn
4450              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4451              (gnus-info-set-read ',info ',(gnus-info-read info))
4452              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4453              (gnus-group-update-group ,group t))))
4454       ;; Add the read articles to the range.
4455       (gnus-info-set-read info range)
4456       ;; Then we have to re-compute how many unread
4457       ;; articles there are in this group.
4458       (when active
4459         (cond
4460          ((not range)
4461           (setq num (- (1+ (cdr active)) (car active))))
4462          ((not (listp (cdr range)))
4463           (setq num (- (cdr active) (- (1+ (cdr range))
4464                                        (car range)))))
4465          (t
4466           (while range
4467             (if (numberp (car range))
4468                 (setq num (1+ num))
4469               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4470             (setq range (cdr range)))
4471           (setq num (- (cdr active) num))))
4472         ;; Update the number of unread articles.
4473         (setcar entry num)
4474         ;; Update the group buffer.
4475         (gnus-group-update-group group t)))))
4476
4477 (defun gnus-methods-equal-p (m1 m2)
4478   (let ((m1 (or m1 gnus-select-method))
4479         (m2 (or m2 gnus-select-method)))
4480     (or (equal m1 m2)
4481         (and (eq (car m1) (car m2))
4482              (or (not (memq 'address (assoc (symbol-name (car m1))
4483                                             gnus-valid-select-methods)))
4484                  (equal (nth 1 m1) (nth 1 m2)))))))
4485
4486 (defvar gnus-newsgroup-none-id 0)
4487
4488 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4489   (let ((cur nntp-server-buffer)
4490         (dependencies
4491          (or dependencies
4492              (save-excursion (set-buffer gnus-summary-buffer)
4493                              gnus-newsgroup-dependencies)))
4494         headers id end ref
4495         (rfc2047-default-charset gnus-newsgroup-default-charset)
4496         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
4497     (save-excursion
4498       (set-buffer nntp-server-buffer)
4499       ;; Translate all TAB characters into SPACE characters.
4500       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4501       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4502       (gnus-run-hooks 'gnus-parse-headers-hook)
4503       (let ((case-fold-search t)
4504             in-reply-to header p lines chars ctype)
4505         (goto-char (point-min))
4506         ;; Search to the beginning of the next header.  Error messages
4507         ;; do not begin with 2 or 3.
4508         (while (re-search-forward "^[23][0-9]+ " nil t)
4509           (setq id nil
4510                 ref nil)
4511           ;; This implementation of this function, with nine
4512           ;; search-forwards instead of the one re-search-forward and
4513           ;; a case (which basically was the old function) is actually
4514           ;; about twice as fast, even though it looks messier.  You
4515           ;; can't have everything, I guess.  Speed and elegance
4516           ;; doesn't always go hand in hand.
4517           (setq
4518            header
4519            (make-full-mail-header
4520             ;; Number.
4521             (prog1
4522                 (read cur)
4523               (end-of-line)
4524               (setq p (point))
4525               (narrow-to-region (point)
4526                                 (or (and (search-forward "\n.\n" nil t)
4527                                          (- (point) 2))
4528                                     (point))))
4529             ;; Subject.
4530             (progn
4531               (goto-char p)
4532               (if (search-forward "\nsubject: " nil t)
4533                   (buffer-substring (match-end 0) (std11-field-end))
4534                 "(none)"))
4535             ;; From.
4536             (progn
4537               (goto-char p)
4538               (if (search-forward "\nfrom: " nil t)
4539                   (buffer-substring (match-end 0) (std11-field-end))
4540                 "(nobody)"))
4541             ;; Date.
4542             (progn
4543               (goto-char p)
4544               (if (search-forward "\ndate: " nil t)
4545                   (buffer-substring (match-end 0) (std11-field-end))
4546                 ""))
4547             ;; Message-ID.
4548             (progn
4549               (goto-char p)
4550               (setq id (if (re-search-forward
4551                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4552                            ;; We do it this way to make sure the Message-ID
4553                            ;; is (somewhat) syntactically valid.
4554                            (buffer-substring (match-beginning 1)
4555                                              (match-end 1))
4556                          ;; If there was no message-id, we just fake one
4557                          ;; to make subsequent routines simpler.
4558                          (nnheader-generate-fake-message-id))))
4559             ;; References.
4560             (progn
4561               (goto-char p)
4562               (if (search-forward "\nreferences: " nil t)
4563                   (progn
4564                     (setq end (point))
4565                     (prog1
4566                         (buffer-substring (match-end 0) (std11-field-end))
4567                       (setq ref
4568                             (buffer-substring
4569                              (progn
4570                                ;; (end-of-line)
4571                                (search-backward ">" end t)
4572                                (1+ (point)))
4573                              (progn
4574                                (search-backward "<" end t)
4575                                (point))))))
4576                 ;; Get the references from the in-reply-to header if there
4577                 ;; were no references and the in-reply-to header looks
4578                 ;; promising.
4579                 (if (and (search-forward "\nin-reply-to: " nil t)
4580                          (setq in-reply-to
4581                                (buffer-substring (match-end 0)
4582                                                  (std11-field-end)))
4583                          (string-match "<[^>]+>" in-reply-to))
4584                     (let (ref2)
4585                       (setq ref (substring in-reply-to (match-beginning 0)
4586                                            (match-end 0)))
4587                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4588                         (setq ref2 (substring in-reply-to (match-beginning 0)
4589                                               (match-end 0)))
4590                         (when (> (length ref2) (length ref))
4591                           (setq ref ref2)))
4592                       ref)
4593                   (setq ref nil))))
4594             ;; Chars.
4595             (progn
4596               (goto-char p)
4597               (if (search-forward "\nchars: " nil t)
4598                   (if (numberp (setq chars (ignore-errors (read cur))))
4599                       chars 0)
4600                 0))
4601             ;; Lines.
4602             (progn
4603               (goto-char p)
4604               (if (search-forward "\nlines: " nil t)
4605                   (if (numberp (setq lines (ignore-errors (read cur))))
4606                       lines 0)
4607                 0))
4608             ;; Xref.
4609             (progn
4610               (goto-char p)
4611               (and (search-forward "\nxref: " nil t)
4612                    (buffer-substring (match-end 0) (std11-field-end))))
4613             ;; Extra.
4614             (when gnus-extra-headers
4615               (let ((extra gnus-extra-headers)
4616                     out)
4617                 (while extra
4618                   (goto-char p)
4619                   (when (search-forward
4620                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4621                     (push (cons (car extra)
4622                                 (buffer-substring (match-end 0)
4623                                                   (std11-field-end)))
4624                           out))
4625                   (pop extra))
4626                 out))))
4627           (goto-char p)
4628           (if (and (search-forward "\ncontent-type: " nil t)
4629                    (setq ctype
4630                          (buffer-substring (match-end 0) (std11-field-end))))
4631               (mime-entity-set-content-type-internal
4632                header (mime-parse-Content-Type ctype)))
4633           (when (equal id ref)
4634             (setq ref nil))
4635
4636           (when gnus-alter-header-function
4637             (funcall gnus-alter-header-function header)
4638             (setq id (mail-header-id header)
4639                   ref (gnus-parent-id (mail-header-references header))))
4640
4641           (when (setq header
4642                       (gnus-dependencies-add-header
4643                        header dependencies force-new))
4644             (push header headers))
4645           (goto-char (point-max))
4646           (widen))
4647         (nreverse headers)))))
4648
4649 ;; Goes through the xover lines and returns a list of vectors
4650 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4651                                                   force-new dependencies
4652                                                   group also-fetch-heads)
4653   "Parse the news overview data in the server buffer, and return a
4654 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4655   ;; Get the Xref when the users reads the articles since most/some
4656   ;; NNTP servers do not include Xrefs when using XOVER.
4657   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4658   (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
4659         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced)
4660         (cur nntp-server-buffer)
4661         (dependencies (or dependencies gnus-newsgroup-dependencies))
4662         number headers header)
4663     (save-excursion
4664       (set-buffer nntp-server-buffer)
4665       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4666       ;; Allow the user to mangle the headers before parsing them.
4667       (gnus-run-hooks 'gnus-parse-headers-hook)
4668       (goto-char (point-min))
4669       (while (not (eobp))
4670         (condition-case ()
4671             (while (and sequence (not (eobp)))
4672               (setq number (read cur))
4673               (while (and sequence
4674                           (< (car sequence) number))
4675                 (setq sequence (cdr sequence)))
4676               (and sequence
4677                    (eq number (car sequence))
4678                    (progn
4679                      (setq sequence (cdr sequence))
4680                      (setq header (inline
4681                                     (gnus-nov-parse-line
4682                                      number dependencies force-new))))
4683                    (push header headers))
4684               (forward-line 1))
4685           (error
4686            (gnus-error 4 "Strange nov line (%d)"
4687                        (count-lines (point-min) (point)))))
4688         (forward-line 1))
4689       ;; A common bug in inn is that if you have posted an article and
4690       ;; then retrieves the active file, it will answer correctly --
4691       ;; the new article is included.  However, a NOV entry for the
4692       ;; article may not have been generated yet, so this may fail.
4693       ;; We work around this problem by retrieving the last few
4694       ;; headers using HEAD.
4695       (if (or (not also-fetch-heads)
4696               (not sequence))
4697           ;; We (probably) got all the headers.
4698           (nreverse headers)
4699         (let ((gnus-nov-is-evil t))
4700           (nconc
4701            (nreverse headers)
4702            (when (gnus-retrieve-headers sequence group)
4703              (gnus-get-newsgroup-headers))))))))
4704
4705 (defun gnus-article-get-xrefs ()
4706   "Fill in the Xref value in `gnus-current-headers', if necessary.
4707 This is meant to be called in `gnus-article-internal-prepare-hook'."
4708   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4709                                  gnus-current-headers)))
4710     (or (not gnus-use-cross-reference)
4711         (not headers)
4712         (and (mail-header-xref headers)
4713              (not (string= (mail-header-xref headers) "")))
4714         (let ((case-fold-search t)
4715               xref)
4716           (save-restriction
4717             (nnheader-narrow-to-headers)
4718             (goto-char (point-min))
4719             (when (or (and (eq (downcase (char-after)) ?x)
4720                            (looking-at "Xref:"))
4721                       (search-forward "\nXref:" nil t))
4722               (goto-char (1+ (match-end 0)))
4723               (setq xref (buffer-substring (point)
4724                                            (progn (end-of-line) (point))))
4725               (mail-header-set-xref headers xref)))))))
4726
4727 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4728   "Find article ID and insert the summary line for that article.
4729 OLD-HEADER can either be a header or a line number to insert
4730 the subject line on."
4731   (let* ((line (and (numberp old-header) old-header))
4732          (old-header (and (vectorp old-header) old-header))
4733          (header (cond ((and old-header use-old-header)
4734                         old-header)
4735                        ((and (numberp id)
4736                              (gnus-number-to-header id))
4737                         (gnus-number-to-header id))
4738                        (t
4739                         (gnus-read-header id))))
4740          (number (and (numberp id) id))
4741          d)
4742     (when header
4743       ;; Rebuild the thread that this article is part of and go to the
4744       ;; article we have fetched.
4745       (when (and (not gnus-show-threads)
4746                  old-header)
4747         (when (and number
4748                    (setq d (gnus-data-find (mail-header-number old-header))))
4749           (goto-char (gnus-data-pos d))
4750           (gnus-data-remove
4751            number
4752            (- (gnus-point-at-bol)
4753               (prog1
4754                   (1+ (gnus-point-at-eol))
4755                 (gnus-delete-line))))))
4756       (when old-header
4757         (mail-header-set-number header (mail-header-number old-header)))
4758       (setq gnus-newsgroup-sparse
4759             (delq (setq number (mail-header-number header))
4760                   gnus-newsgroup-sparse))
4761       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4762       (push number gnus-newsgroup-limit)
4763       (gnus-rebuild-thread (mail-header-id header) line)
4764       (gnus-summary-goto-subject number nil t))
4765     (when (and (numberp number)
4766                (> number 0))
4767       ;; We have to update the boundaries even if we can't fetch the
4768       ;; article if ID is a number -- so that the next `P' or `N'
4769       ;; command will fetch the previous (or next) article even
4770       ;; if the one we tried to fetch this time has been canceled.
4771       (when (> number gnus-newsgroup-end)
4772         (setq gnus-newsgroup-end number))
4773       (when (< number gnus-newsgroup-begin)
4774         (setq gnus-newsgroup-begin number))
4775       (setq gnus-newsgroup-unselected
4776             (delq number gnus-newsgroup-unselected)))
4777     ;; Report back a success?
4778     (and header (mail-header-number header))))
4779
4780 ;;; Process/prefix in the summary buffer
4781
4782 (defun gnus-summary-work-articles (n)
4783   "Return a list of articles to be worked upon.
4784 The prefix argument, the list of process marked articles, and the
4785 current article will be taken into consideration."
4786   (save-excursion
4787     (set-buffer gnus-summary-buffer)
4788     (cond
4789      (n
4790       ;; A numerical prefix has been given.
4791       (setq n (prefix-numeric-value n))
4792       (let ((backward (< n 0))
4793             (n (abs (prefix-numeric-value n)))
4794             articles article)
4795         (save-excursion
4796           (while
4797               (and (> n 0)
4798                    (push (setq article (gnus-summary-article-number))
4799                          articles)
4800                    (if backward
4801                        (gnus-summary-find-prev nil article)
4802                      (gnus-summary-find-next nil article)))
4803             (decf n)))
4804         (nreverse articles)))
4805      ((and (gnus-region-active-p) (mark))
4806       (message "region active")
4807       ;; Work on the region between point and mark.
4808       (let ((max (max (point) (mark)))
4809             articles article)
4810         (save-excursion
4811           (goto-char (min (point) (mark)))
4812           (while
4813               (and
4814                (push (setq article (gnus-summary-article-number)) articles)
4815                (gnus-summary-find-next nil article)
4816                (< (point) max)))
4817           (nreverse articles))))
4818      (gnus-newsgroup-processable
4819       ;; There are process-marked articles present.
4820       ;; Save current state.
4821       (gnus-summary-save-process-mark)
4822       ;; Return the list.
4823       (reverse gnus-newsgroup-processable))
4824      (t
4825       ;; Just return the current article.
4826       (list (gnus-summary-article-number))))))
4827
4828 (defmacro gnus-summary-iterate (arg &rest forms)
4829   "Iterate over the process/prefixed articles and do FORMS.
4830 ARG is the interactive prefix given to the command.  FORMS will be
4831 executed with point over the summary line of the articles."
4832   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4833     `(let ((,articles (gnus-summary-work-articles ,arg)))
4834        (while ,articles
4835          (gnus-summary-goto-subject (car ,articles))
4836          ,@forms))))
4837
4838 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4839 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4840
4841 (defun gnus-summary-save-process-mark ()
4842   "Push the current set of process marked articles on the stack."
4843   (interactive)
4844   (push (copy-sequence gnus-newsgroup-processable)
4845         gnus-newsgroup-process-stack))
4846
4847 (defun gnus-summary-kill-process-mark ()
4848   "Push the current set of process marked articles on the stack and unmark."
4849   (interactive)
4850   (gnus-summary-save-process-mark)
4851   (gnus-summary-unmark-all-processable))
4852
4853 (defun gnus-summary-yank-process-mark ()
4854   "Pop the last process mark state off the stack and restore it."
4855   (interactive)
4856   (unless gnus-newsgroup-process-stack
4857     (error "Empty mark stack"))
4858   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4859
4860 (defun gnus-summary-process-mark-set (set)
4861   "Make SET into the current process marked articles."
4862   (gnus-summary-unmark-all-processable)
4863   (while set
4864     (gnus-summary-set-process-mark (pop set))))
4865
4866 ;;; Searching and stuff
4867
4868 (defun gnus-summary-search-group (&optional backward use-level)
4869   "Search for next unread newsgroup.
4870 If optional argument BACKWARD is non-nil, search backward instead."
4871   (save-excursion
4872     (set-buffer gnus-group-buffer)
4873     (when (gnus-group-search-forward
4874            backward nil (if use-level (gnus-group-group-level) nil))
4875       (gnus-group-group-name))))
4876
4877 (defun gnus-summary-best-group (&optional exclude-group)
4878   "Find the name of the best unread group.
4879 If EXCLUDE-GROUP, do not go to this group."
4880   (save-excursion
4881     (set-buffer gnus-group-buffer)
4882     (save-excursion
4883       (gnus-group-best-unread-group exclude-group))))
4884
4885 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4886   (if backward (gnus-summary-find-prev)
4887     (let* ((dummy (gnus-summary-article-intangible-p))
4888            (article (or article (gnus-summary-article-number)))
4889            (arts (gnus-data-find-list article))
4890            result)
4891       (when (and (not dummy)
4892                  (or (not gnus-summary-check-current)
4893                      (not unread)
4894                      (not (gnus-data-unread-p (car arts)))))
4895         (setq arts (cdr arts)))
4896       (when (setq result
4897                   (if unread
4898                       (progn
4899                         (while arts
4900                           (when (or (and undownloaded
4901                                          (eq gnus-undownloaded-mark
4902                                              (gnus-data-mark (car arts))))
4903                                     (gnus-data-unread-p (car arts)))
4904                             (setq result (car arts)
4905                                   arts nil))
4906                           (setq arts (cdr arts)))
4907                         result)
4908                     (car arts)))
4909         (goto-char (gnus-data-pos result))
4910         (gnus-data-number result)))))
4911
4912 (defun gnus-summary-find-prev (&optional unread article)
4913   (let* ((eobp (eobp))
4914          (article (or article (gnus-summary-article-number)))
4915          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4916          result)
4917     (when (and (not eobp)
4918                (or (not gnus-summary-check-current)
4919                    (not unread)
4920                    (not (gnus-data-unread-p (car arts)))))
4921       (setq arts (cdr arts)))
4922     (when (setq result
4923                 (if unread
4924                     (progn
4925                       (while arts
4926                         (when (gnus-data-unread-p (car arts))
4927                           (setq result (car arts)
4928                                 arts nil))
4929                         (setq arts (cdr arts)))
4930                       result)
4931                   (car arts)))
4932       (goto-char (gnus-data-pos result))
4933       (gnus-data-number result))))
4934
4935 (defun gnus-summary-find-subject (subject &optional unread backward article)
4936   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4937          (article (or article (gnus-summary-article-number)))
4938          (articles (gnus-data-list backward))
4939          (arts (gnus-data-find-list article articles))
4940          result)
4941     (when (or (not gnus-summary-check-current)
4942               (not unread)
4943               (not (gnus-data-unread-p (car arts))))
4944       (setq arts (cdr arts)))
4945     (while arts
4946       (and (or (not unread)
4947                (gnus-data-unread-p (car arts)))
4948            (vectorp (gnus-data-header (car arts)))
4949            (gnus-subject-equal
4950             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4951            (setq result (car arts)
4952                  arts nil))
4953       (setq arts (cdr arts)))
4954     (and result
4955          (goto-char (gnus-data-pos result))
4956          (gnus-data-number result))))
4957
4958 (defun gnus-summary-search-forward (&optional unread subject backward)
4959   "Search forward for an article.
4960 If UNREAD, look for unread articles.  If SUBJECT, look for
4961 articles with that subject.  If BACKWARD, search backward instead."
4962   (cond (subject (gnus-summary-find-subject subject unread backward))
4963         (backward (gnus-summary-find-prev unread))
4964         (t (gnus-summary-find-next unread))))
4965
4966 (defun gnus-recenter (&optional n)
4967   "Center point in window and redisplay frame.
4968 Also do horizontal recentering."
4969   (interactive "P")
4970   (when (and gnus-auto-center-summary
4971              (not (eq gnus-auto-center-summary 'vertical)))
4972     (gnus-horizontal-recenter))
4973   (recenter n))
4974
4975 (defun gnus-summary-recenter ()
4976   "Center point in the summary window.
4977 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4978 displayed, no centering will be performed."
4979   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4980   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4981   (let* ((top (cond ((< (window-height) 4) 0)
4982                     ((< (window-height) 7) 1)
4983                     (t (if (numberp gnus-auto-center-summary)
4984                            gnus-auto-center-summary
4985                          2))))
4986          (height (1- (window-height)))
4987          (bottom (save-excursion (goto-char (point-max))
4988                                  (forward-line (- height))
4989                                  (point)))
4990          (window (get-buffer-window (current-buffer))))
4991     ;; The user has to want it.
4992     (when gnus-auto-center-summary
4993       (when (get-buffer-window gnus-article-buffer)
4994         ;; Only do recentering when the article buffer is displayed,
4995         ;; Set the window start to either `bottom', which is the biggest
4996         ;; possible valid number, or the second line from the top,
4997         ;; whichever is the least.
4998         (set-window-start
4999          window (min bottom (save-excursion
5000                               (forward-line (- top)) (point)))))
5001       ;; Do horizontal recentering while we're at it.
5002       (when (and (get-buffer-window (current-buffer) t)
5003                  (not (eq gnus-auto-center-summary 'vertical)))
5004         (let ((selected (selected-window)))
5005           (select-window (get-buffer-window (current-buffer) t))
5006           (gnus-summary-position-point)
5007           (gnus-horizontal-recenter)
5008           (select-window selected))))))
5009
5010 (defun gnus-summary-jump-to-group (newsgroup)
5011   "Move point to NEWSGROUP in group mode buffer."
5012   ;; Keep update point of group mode buffer if visible.
5013   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5014       (save-window-excursion
5015         ;; Take care of tree window mode.
5016         (when (get-buffer-window gnus-group-buffer)
5017           (pop-to-buffer gnus-group-buffer))
5018         (gnus-group-jump-to-group newsgroup))
5019     (save-excursion
5020       ;; Take care of tree window mode.
5021       (if (get-buffer-window gnus-group-buffer)
5022           (pop-to-buffer gnus-group-buffer)
5023         (set-buffer gnus-group-buffer))
5024       (gnus-group-jump-to-group newsgroup))))
5025
5026 ;; This function returns a list of article numbers based on the
5027 ;; difference between the ranges of read articles in this group and
5028 ;; the range of active articles.
5029 (defun gnus-list-of-unread-articles (group)
5030   (let* ((read (gnus-info-read (gnus-get-info group)))
5031          (active (or (gnus-active group) (gnus-activate-group group)))
5032          (last (cdr active))
5033          first nlast unread)
5034     ;; If none are read, then all are unread.
5035     (if (not read)
5036         (setq first (car active))
5037       ;; If the range of read articles is a single range, then the
5038       ;; first unread article is the article after the last read
5039       ;; article.  Sounds logical, doesn't it?
5040       (if (not (listp (cdr read)))
5041           (setq first (max (car active) (1+ (cdr read))))
5042         ;; `read' is a list of ranges.
5043         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5044                                   (caar read)))
5045                   1)
5046           (setq first (car active)))
5047         (while read
5048           (when first
5049             (while (< first nlast)
5050               (push first unread)
5051               (setq first (1+ first))))
5052           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5053           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5054           (setq read (cdr read)))))
5055     ;; And add the last unread articles.
5056     (while (<= first last)
5057       (push first unread)
5058       (setq first (1+ first)))
5059     ;; Return the list of unread articles.
5060     (delq 0 (nreverse unread))))
5061
5062 (defun gnus-list-of-read-articles (group)
5063   "Return a list of unread, unticked and non-dormant articles."
5064   (let* ((info (gnus-get-info group))
5065          (marked (gnus-info-marks info))
5066          (active (gnus-active group)))
5067     (and info active
5068          (gnus-set-difference
5069           (gnus-sorted-complement
5070            (gnus-uncompress-range active)
5071            (gnus-list-of-unread-articles group))
5072           (append
5073            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5074            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5075
5076 ;; Various summary commands
5077
5078 (defun gnus-summary-select-article-buffer ()
5079   "Reconfigure windows to show article buffer."
5080   (interactive)
5081   (if (not (gnus-buffer-live-p gnus-article-buffer))
5082       (error "There is no article buffer for this summary buffer")
5083     (gnus-configure-windows 'article)
5084     (select-window (get-buffer-window gnus-article-buffer))))
5085
5086 (defun gnus-summary-universal-argument (arg)
5087   "Perform any operation on all articles that are process/prefixed."
5088   (interactive "P")
5089   (let ((articles (gnus-summary-work-articles arg))
5090         func article)
5091     (if (eq
5092          (setq
5093           func
5094           (key-binding
5095            (read-key-sequence
5096             (substitute-command-keys
5097              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5098              ))))
5099          'undefined)
5100         (gnus-error 1 "Undefined key")
5101       (save-excursion
5102         (while articles
5103           (gnus-summary-goto-subject (setq article (pop articles)))
5104           (let (gnus-newsgroup-processable)
5105             (command-execute func))
5106           (gnus-summary-remove-process-mark article)))))
5107   (gnus-summary-position-point))
5108
5109 (defun gnus-summary-toggle-truncation (&optional arg)
5110   "Toggle truncation of summary lines.
5111 With arg, turn line truncation on iff arg is positive."
5112   (interactive "P")
5113   (setq truncate-lines
5114         (if (null arg) (not truncate-lines)
5115           (> (prefix-numeric-value arg) 0)))
5116   (redraw-display))
5117
5118 (defun gnus-summary-reselect-current-group (&optional all rescan)
5119   "Exit and then reselect the current newsgroup.
5120 The prefix argument ALL means to select all articles."
5121   (interactive "P")
5122   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5123     (error "Ephemeral groups can't be reselected"))
5124   (let ((current-subject (gnus-summary-article-number))
5125         (group gnus-newsgroup-name))
5126     (setq gnus-newsgroup-begin nil)
5127     (gnus-summary-exit)
5128     ;; We have to adjust the point of group mode buffer because
5129     ;; point was moved to the next unread newsgroup by exiting.
5130     (gnus-summary-jump-to-group group)
5131     (when rescan
5132       (save-excursion
5133         (gnus-group-get-new-news-this-group 1)))
5134     (gnus-group-read-group all t)
5135     (gnus-summary-goto-subject current-subject nil t)))
5136
5137 (defun gnus-summary-rescan-group (&optional all)
5138   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5139   (interactive "P")
5140   (gnus-summary-reselect-current-group all t))
5141
5142 (defun gnus-summary-update-info (&optional non-destructive)
5143   (save-excursion
5144     (let ((group gnus-newsgroup-name))
5145       (when group
5146         (when gnus-newsgroup-kill-headers
5147           (setq gnus-newsgroup-killed
5148                 (gnus-compress-sequence
5149                  (nconc
5150                   (gnus-set-sorted-intersection
5151                    (gnus-uncompress-range gnus-newsgroup-killed)
5152                    (setq gnus-newsgroup-unselected
5153                          (sort gnus-newsgroup-unselected '<)))
5154                   (setq gnus-newsgroup-unreads
5155                         (sort gnus-newsgroup-unreads '<)))
5156                  t)))
5157         (unless (listp (cdr gnus-newsgroup-killed))
5158           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5159         (let ((headers gnus-newsgroup-headers))
5160           ;; Set the new ranges of read articles.
5161           (save-excursion
5162             (set-buffer gnus-group-buffer)
5163             (gnus-undo-force-boundary))
5164           (gnus-update-read-articles
5165            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5166           ;; Set the current article marks.
5167           (let ((gnus-newsgroup-scored
5168                  (if (and (not gnus-save-score)
5169                           (not non-destructive))
5170                      nil
5171                    gnus-newsgroup-scored)))
5172             (save-excursion
5173               (gnus-update-marks)))
5174           ;; Do the cross-ref thing.
5175           (when gnus-use-cross-reference
5176             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5177           ;; Do not switch windows but change the buffer to work.
5178           (set-buffer gnus-group-buffer)
5179           (unless (gnus-ephemeral-group-p group)
5180             (gnus-group-update-group group)))))))
5181
5182 (defun gnus-summary-save-newsrc (&optional force)
5183   "Save the current number of read/marked articles in the dribble buffer.
5184 The dribble buffer will then be saved.
5185 If FORCE (the prefix), also save the .newsrc file(s)."
5186   (interactive "P")
5187   (gnus-summary-update-info t)
5188   (if force
5189       (gnus-save-newsrc-file)
5190     (gnus-dribble-save)))
5191
5192 (defun gnus-summary-exit (&optional temporary)
5193   "Exit reading current newsgroup, and then return to group selection mode.
5194 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5195   (interactive)
5196   (gnus-set-global-variables)
5197   (gnus-kill-save-kill-buffer)
5198   (gnus-async-halt-prefetch)
5199   (let* ((group gnus-newsgroup-name)
5200          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5201          (mode major-mode)
5202          (group-point nil)
5203          (buf (current-buffer)))
5204     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5205     ;; If we have several article buffers, we kill them at exit.
5206     (unless gnus-single-article-buffer
5207       (gnus-kill-buffer gnus-original-article-buffer)
5208       (setq gnus-article-current nil))
5209     (when gnus-use-cache
5210       (gnus-cache-possibly-remove-articles)
5211       (gnus-cache-save-buffers))
5212     (gnus-async-prefetch-remove-group group)
5213     (when gnus-suppress-duplicates
5214       (gnus-dup-enter-articles))
5215     (when gnus-use-trees
5216       (gnus-tree-close group))
5217     ;; Remove entries for this group.
5218     (nnmail-purge-split-history (gnus-group-real-name group))
5219     ;; Make all changes in this group permanent.
5220     (unless quit-config
5221       (gnus-run-hooks 'gnus-exit-group-hook)
5222       (gnus-summary-update-info)
5223       ;; Do adaptive scoring, and possibly save score files.
5224       (when gnus-newsgroup-adaptive
5225         (gnus-score-adaptive))
5226       (when gnus-use-scoring
5227         (gnus-score-save)))
5228     (gnus-close-group group)
5229     ;; Make sure where we were, and go to next newsgroup.
5230     (set-buffer gnus-group-buffer)
5231     (unless quit-config
5232       (gnus-group-jump-to-group group))
5233     (gnus-run-hooks 'gnus-summary-exit-hook)
5234     (unless (or quit-config
5235                 ;; If this group has disappeared from the summary
5236                 ;; buffer, don't skip forwards.
5237                 (not (string= group (gnus-group-group-name))))
5238       (gnus-group-next-unread-group 1))
5239     (setq group-point (point))
5240     (if temporary
5241         nil                             ;Nothing to do.
5242       ;; If we have several article buffers, we kill them at exit.
5243       (unless gnus-single-article-buffer
5244         (gnus-kill-buffer gnus-article-buffer)
5245         (gnus-kill-buffer gnus-original-article-buffer)
5246         (setq gnus-article-current nil))
5247       (set-buffer buf)
5248       (if (not gnus-kill-summary-on-exit)
5249           (gnus-deaden-summary)
5250         ;; We set all buffer-local variables to nil.  It is unclear why
5251         ;; this is needed, but if we don't, buffer-local variables are
5252         ;; not garbage-collected, it seems.  This would the lead to en
5253         ;; ever-growing Emacs.
5254         (gnus-summary-clear-local-variables)
5255         (when (get-buffer gnus-article-buffer)
5256           (bury-buffer gnus-article-buffer))
5257         ;; We clear the global counterparts of the buffer-local
5258         ;; variables as well, just to be on the safe side.
5259         (set-buffer gnus-group-buffer)
5260         (gnus-summary-clear-local-variables)
5261         ;; Return to group mode buffer.
5262         (when (eq mode 'gnus-summary-mode)
5263           (gnus-kill-buffer buf)))
5264       (setq gnus-current-select-method gnus-select-method)
5265       (pop-to-buffer gnus-group-buffer)
5266       (if (not quit-config)
5267           (progn
5268             (goto-char group-point)
5269             (gnus-configure-windows 'group 'force))
5270         (gnus-handle-ephemeral-exit quit-config))
5271       ;; Clear the current group name.
5272       (unless quit-config
5273         (setq gnus-newsgroup-name nil)))))
5274
5275 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5276 (defun gnus-summary-exit-no-update (&optional no-questions)
5277   "Quit reading current newsgroup without updating read article info."
5278   (interactive)
5279   (let* ((group gnus-newsgroup-name)
5280          (quit-config (gnus-group-quit-config group)))
5281     (when (or no-questions
5282               gnus-expert-user
5283               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5284       (gnus-async-halt-prefetch)
5285       (gnus-run-hooks (delq 'gnus-summary-expire-articles
5286                             (copy-list gnus-summary-prepare-exit-hook)))
5287       ;; If we have several article buffers, we kill them at exit.
5288       (unless gnus-single-article-buffer
5289         (gnus-kill-buffer gnus-article-buffer)
5290         (gnus-kill-buffer gnus-original-article-buffer)
5291         (setq gnus-article-current nil))
5292       (if (not gnus-kill-summary-on-exit)
5293           (gnus-deaden-summary)
5294         (gnus-close-group group)
5295         (gnus-summary-clear-local-variables)
5296         (set-buffer gnus-group-buffer)
5297         (gnus-summary-clear-local-variables)
5298         (when (get-buffer gnus-summary-buffer)
5299           (kill-buffer gnus-summary-buffer)))
5300       (unless gnus-single-article-buffer
5301         (setq gnus-article-current nil))
5302       (when gnus-use-trees
5303         (gnus-tree-close group))
5304       (gnus-async-prefetch-remove-group group)
5305       (when (get-buffer gnus-article-buffer)
5306         (bury-buffer gnus-article-buffer))
5307       ;; Return to the group buffer.
5308       (gnus-configure-windows 'group 'force)
5309       ;; Clear the current group name.
5310       (setq gnus-newsgroup-name nil)
5311       (when (equal (gnus-group-group-name) group)
5312         (gnus-group-next-unread-group 1))
5313       (when quit-config
5314         (gnus-handle-ephemeral-exit quit-config)))))
5315
5316 (defun gnus-handle-ephemeral-exit (quit-config)
5317   "Handle movement when leaving an ephemeral group.
5318 The state which existed when entering the ephemeral is reset."
5319   (if (not (buffer-name (car quit-config)))
5320       (gnus-configure-windows 'group 'force)
5321     (set-buffer (car quit-config))
5322     (cond ((eq major-mode 'gnus-summary-mode)
5323            (gnus-set-global-variables))
5324           ((eq major-mode 'gnus-article-mode)
5325            (save-excursion
5326              ;; The `gnus-summary-buffer' variable may point
5327              ;; to the old summary buffer when using a single
5328              ;; article buffer.
5329              (unless (gnus-buffer-live-p gnus-summary-buffer)
5330                (set-buffer gnus-group-buffer))
5331              (set-buffer gnus-summary-buffer)
5332              (gnus-set-global-variables))))
5333     (if (or (eq (cdr quit-config) 'article)
5334             (eq (cdr quit-config) 'pick))
5335         (progn
5336           ;; The current article may be from the ephemeral group
5337           ;; thus it is best that we reload this article
5338           (gnus-summary-show-article)
5339           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5340               (gnus-configure-windows 'pick 'force)
5341             (gnus-configure-windows (cdr quit-config) 'force)))
5342       (gnus-configure-windows (cdr quit-config) 'force))
5343     (when (eq major-mode 'gnus-summary-mode)
5344       (gnus-summary-next-subject 1 nil t)
5345       (gnus-summary-recenter)
5346       (gnus-summary-position-point))))
5347
5348 (defun gnus-summary-preview-mime-message (arg)
5349   "MIME decode and play this message."
5350   (interactive "P")
5351   (or gnus-show-mime
5352       (let ((gnus-break-pages nil)
5353             (gnus-show-mime t))
5354         (gnus-summary-select-article t t)
5355         ))
5356   (select-window (get-buffer-window gnus-article-buffer))
5357   )
5358
5359 ;;; Dead summaries.
5360
5361 (defvar gnus-dead-summary-mode-map nil)
5362
5363 (unless gnus-dead-summary-mode-map
5364   (setq gnus-dead-summary-mode-map (make-keymap))
5365   (suppress-keymap gnus-dead-summary-mode-map)
5366   (substitute-key-definition
5367    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5368   (let ((keys '("\C-d" "\r" "\177" [delete])))
5369     (while keys
5370       (define-key gnus-dead-summary-mode-map
5371         (pop keys) 'gnus-summary-wake-up-the-dead))))
5372
5373 (defvar gnus-dead-summary-mode nil
5374   "Minor mode for Gnus summary buffers.")
5375
5376 (defun gnus-dead-summary-mode (&optional arg)
5377   "Minor mode for Gnus summary buffers."
5378   (interactive "P")
5379   (when (eq major-mode 'gnus-summary-mode)
5380     (make-local-variable 'gnus-dead-summary-mode)
5381     (setq gnus-dead-summary-mode
5382           (if (null arg) (not gnus-dead-summary-mode)
5383             (> (prefix-numeric-value arg) 0)))
5384     (when gnus-dead-summary-mode
5385       (gnus-add-minor-mode
5386        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5387
5388 (defun gnus-deaden-summary ()
5389   "Make the current summary buffer into a dead summary buffer."
5390   ;; Kill any previous dead summary buffer.
5391   (when (and gnus-dead-summary
5392              (buffer-name gnus-dead-summary))
5393     (save-excursion
5394       (set-buffer gnus-dead-summary)
5395       (when gnus-dead-summary-mode
5396         (kill-buffer (current-buffer)))))
5397   ;; Make this the current dead summary.
5398   (setq gnus-dead-summary (current-buffer))
5399   (gnus-dead-summary-mode 1)
5400   (let ((name (buffer-name)))
5401     (when (string-match "Summary" name)
5402       (rename-buffer
5403        (concat (substring name 0 (match-beginning 0)) "Dead "
5404                (substring name (match-beginning 0)))
5405        t))))
5406
5407 (defun gnus-kill-or-deaden-summary (buffer)
5408   "Kill or deaden the summary BUFFER."
5409   (save-excursion
5410     (when (and (buffer-name buffer)
5411                (not gnus-single-article-buffer))
5412       (save-excursion
5413         (set-buffer buffer)
5414         (gnus-kill-buffer gnus-article-buffer)
5415         (gnus-kill-buffer gnus-original-article-buffer)))
5416     (cond (gnus-kill-summary-on-exit
5417            (when (and gnus-use-trees
5418                       (gnus-buffer-exists-p buffer))
5419              (save-excursion
5420                (set-buffer buffer)
5421                (gnus-tree-close gnus-newsgroup-name)))
5422            (gnus-kill-buffer buffer))
5423           ((gnus-buffer-exists-p buffer)
5424            (save-excursion
5425              (set-buffer buffer)
5426              (gnus-deaden-summary))))))
5427
5428 (defun gnus-summary-wake-up-the-dead (&rest args)
5429   "Wake up the dead summary buffer."
5430   (interactive)
5431   (gnus-dead-summary-mode -1)
5432   (let ((name (buffer-name)))
5433     (when (string-match "Dead " name)
5434       (rename-buffer
5435        (concat (substring name 0 (match-beginning 0))
5436                (substring name (match-end 0)))
5437        t)))
5438   (gnus-message 3 "This dead summary is now alive again"))
5439
5440 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5441 (defun gnus-summary-fetch-faq (&optional faq-dir)
5442   "Fetch the FAQ for the current group.
5443 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5444 in."
5445   (interactive
5446    (list
5447     (when current-prefix-arg
5448       (completing-read
5449        "Faq dir: " (and (listp gnus-group-faq-directory)
5450                         (mapcar (lambda (file) (list file))
5451                                 gnus-group-faq-directory))))))
5452   (let (gnus-faq-buffer)
5453     (when (setq gnus-faq-buffer
5454                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5455       (gnus-configure-windows 'summary-faq))))
5456
5457 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5458 (defun gnus-summary-describe-group (&optional force)
5459   "Describe the current newsgroup."
5460   (interactive "P")
5461   (gnus-group-describe-group force gnus-newsgroup-name))
5462
5463 (defun gnus-summary-describe-briefly ()
5464   "Describe summary mode commands briefly."
5465   (interactive)
5466   (gnus-message 6
5467                 (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")))
5468
5469 ;; Walking around group mode buffer from summary mode.
5470
5471 (defun gnus-summary-next-group (&optional no-article target-group backward)
5472   "Exit current newsgroup and then select next unread newsgroup.
5473 If prefix argument NO-ARTICLE is non-nil, no article is selected
5474 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5475 previous group instead."
5476   (interactive "P")
5477   ;; Stop pre-fetching.
5478   (gnus-async-halt-prefetch)
5479   (let ((current-group gnus-newsgroup-name)
5480         (current-buffer (current-buffer))
5481         entered)
5482     ;; First we semi-exit this group to update Xrefs and all variables.
5483     ;; We can't do a real exit, because the window conf must remain
5484     ;; the same in case the user is prompted for info, and we don't
5485     ;; want the window conf to change before that...
5486     (gnus-summary-exit t)
5487     (while (not entered)
5488       ;; Then we find what group we are supposed to enter.
5489       (set-buffer gnus-group-buffer)
5490       (gnus-group-jump-to-group current-group)
5491       (setq target-group
5492             (or target-group
5493                 (if (eq gnus-keep-same-level 'best)
5494                     (gnus-summary-best-group gnus-newsgroup-name)
5495                   (gnus-summary-search-group backward gnus-keep-same-level))))
5496       (if (not target-group)
5497           ;; There are no further groups, so we return to the group
5498           ;; buffer.
5499           (progn
5500             (gnus-message 5 "Returning to the group buffer")
5501             (setq entered t)
5502             (when (gnus-buffer-live-p current-buffer)
5503               (set-buffer current-buffer)
5504               (gnus-summary-exit))
5505             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5506         ;; We try to enter the target group.
5507         (gnus-group-jump-to-group target-group)
5508         (let ((unreads (gnus-group-group-unread)))
5509           (if (and (or (eq t unreads)
5510                        (and unreads (not (zerop unreads))))
5511                    (gnus-summary-read-group
5512                     target-group nil no-article
5513                     (and (buffer-name current-buffer) current-buffer)
5514                     nil backward))
5515               (setq entered t)
5516             (setq current-group target-group
5517                   target-group nil)))))))
5518
5519 (defun gnus-summary-prev-group (&optional no-article)
5520   "Exit current newsgroup and then select previous unread newsgroup.
5521 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5522   (interactive "P")
5523   (gnus-summary-next-group no-article nil t))
5524
5525 ;; Walking around summary lines.
5526
5527 (defun gnus-summary-first-subject (&optional unread undownloaded)
5528   "Go to the first unread subject.
5529 If UNREAD is non-nil, go to the first unread article.
5530 Returns the article selected or nil if there are no unread articles."
5531   (interactive "P")
5532   (prog1
5533       (cond
5534        ;; Empty summary.
5535        ((null gnus-newsgroup-data)
5536         (gnus-message 3 "No articles in the group")
5537         nil)
5538        ;; Pick the first article.
5539        ((not unread)
5540         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5541         (gnus-data-number (car gnus-newsgroup-data)))
5542        ;; No unread articles.
5543        ((null gnus-newsgroup-unreads)
5544         (gnus-message 3 "No more unread articles")
5545         nil)
5546        ;; Find the first unread article.
5547        (t
5548         (let ((data gnus-newsgroup-data))
5549           (while (and data
5550                       (and (not (and undownloaded
5551                                      (eq gnus-undownloaded-mark
5552                                          (gnus-data-mark (car data)))))
5553                            (not (gnus-data-unread-p (car data)))))
5554             (setq data (cdr data)))
5555           (when data
5556             (goto-char (gnus-data-pos (car data)))
5557             (gnus-data-number (car data))))))
5558     (gnus-summary-position-point)))
5559
5560 (defun gnus-summary-next-subject (n &optional unread dont-display)
5561   "Go to next N'th summary line.
5562 If N is negative, go to the previous N'th subject line.
5563 If UNREAD is non-nil, only unread articles are selected.
5564 The difference between N and the actual number of steps taken is
5565 returned."
5566   (interactive "p")
5567   (let ((backward (< n 0))
5568         (n (abs n)))
5569     (while (and (> n 0)
5570                 (if backward
5571                     (gnus-summary-find-prev unread)
5572                   (gnus-summary-find-next unread)))
5573       (gnus-summary-show-thread)
5574       (setq n (1- n)))
5575     (when (/= 0 n)
5576       (gnus-message 7 "No more%s articles"
5577                     (if unread " unread" "")))
5578     (unless dont-display
5579       (gnus-summary-recenter)
5580       (gnus-summary-position-point))
5581     n))
5582
5583 (defun gnus-summary-next-unread-subject (n)
5584   "Go to next N'th unread summary line."
5585   (interactive "p")
5586   (gnus-summary-next-subject n t))
5587
5588 (defun gnus-summary-prev-subject (n &optional unread)
5589   "Go to previous N'th summary line.
5590 If optional argument UNREAD is non-nil, only unread article is selected."
5591   (interactive "p")
5592   (gnus-summary-next-subject (- n) unread))
5593
5594 (defun gnus-summary-prev-unread-subject (n)
5595   "Go to previous N'th unread summary line."
5596   (interactive "p")
5597   (gnus-summary-next-subject (- n) t))
5598
5599 (defun gnus-summary-goto-subject (article &optional force silent)
5600   "Go the subject line of ARTICLE.
5601 If FORCE, also allow jumping to articles not currently shown."
5602   (interactive "nArticle number: ")
5603   (let ((b (point))
5604         (data (gnus-data-find article)))
5605     ;; We read in the article if we have to.
5606     (and (not data)
5607          force
5608          (gnus-summary-insert-subject
5609           article
5610           (if (or (numberp force) (vectorp force)) force)
5611           t)
5612          (setq data (gnus-data-find article)))
5613     (goto-char b)
5614     (if (not data)
5615         (progn
5616           (unless silent
5617             (gnus-message 3 "Can't find article %d" article))
5618           nil)
5619       (goto-char (gnus-data-pos data))
5620       (gnus-summary-position-point)
5621       article)))
5622
5623 ;; Walking around summary lines with displaying articles.
5624
5625 (defun gnus-summary-expand-window (&optional arg)
5626   "Make the summary buffer take up the entire Emacs frame.
5627 Given a prefix, will force an `article' buffer configuration."
5628   (interactive "P")
5629   (if arg
5630       (gnus-configure-windows 'article 'force)
5631     (gnus-configure-windows 'summary 'force)))
5632
5633 (defun gnus-summary-display-article (article &optional all-header)
5634   "Display ARTICLE in article buffer."
5635   (gnus-set-global-variables)
5636   (if (null article)
5637       nil
5638     (prog1
5639         (if gnus-summary-display-article-function
5640             (funcall gnus-summary-display-article-function article all-header)
5641           (gnus-article-prepare article all-header))
5642       (gnus-run-hooks 'gnus-select-article-hook)
5643       (when (and gnus-current-article
5644                  (not (zerop gnus-current-article)))
5645         (gnus-summary-goto-subject gnus-current-article))
5646       (gnus-summary-recenter)
5647       (when (and gnus-use-trees gnus-show-threads)
5648         (gnus-possibly-generate-tree article)
5649         (gnus-highlight-selected-tree article))
5650       ;; Successfully display article.
5651       (gnus-article-set-window-start
5652        (cdr (assq article gnus-newsgroup-bookmarks))))))
5653
5654 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5655   "Select the current article.
5656 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5657 non-nil, the article will be re-fetched even if it already present in
5658 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5659 be displayed."
5660   ;; Make sure we are in the summary buffer to work around bbdb bug.
5661   (unless (eq major-mode 'gnus-summary-mode)
5662     (set-buffer gnus-summary-buffer))
5663   (let ((article (or article (gnus-summary-article-number)))
5664         (all-headers (not (not all-headers))) ;Must be T or NIL.
5665         gnus-summary-display-article-function
5666         did)
5667     (and (not pseudo)
5668          (gnus-summary-article-pseudo-p article)
5669          (error "This is a pseudo-article"))
5670     (prog1
5671         (save-excursion
5672           (set-buffer gnus-summary-buffer)
5673           (if (or (and gnus-single-article-buffer
5674                        (or (null gnus-current-article)
5675                            (null gnus-article-current)
5676                            (null (get-buffer gnus-article-buffer))
5677                            (not (eq article (cdr gnus-article-current)))
5678                            (not (equal (car gnus-article-current)
5679                                        gnus-newsgroup-name))))
5680                   (and (not gnus-single-article-buffer)
5681                        (or (null gnus-current-article)
5682                            (not (eq gnus-current-article article))))
5683                   force)
5684               ;; The requested article is different from the current article.
5685               (prog1
5686                   (gnus-summary-display-article article all-headers)
5687                 (setq did article))
5688             (when (or all-headers gnus-show-all-headers)
5689               (gnus-article-show-all-headers))
5690             'old))
5691       (when did
5692         (gnus-article-set-window-start
5693          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5694
5695 (defun gnus-summary-set-current-mark (&optional current-mark)
5696   "Obsolete function."
5697   nil)
5698
5699 (defun gnus-summary-next-article (&optional unread subject backward push)
5700   "Select the next article.
5701 If UNREAD, only unread articles are selected.
5702 If SUBJECT, only articles with SUBJECT are selected.
5703 If BACKWARD, the previous article is selected instead of the next."
5704   (interactive "P")
5705   (cond
5706    ;; Is there such an article?
5707    ((and (gnus-summary-search-forward unread subject backward)
5708          (or (gnus-summary-display-article (gnus-summary-article-number))
5709              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5710     (gnus-summary-position-point))
5711    ;; If not, we try the first unread, if that is wanted.
5712    ((and subject
5713          gnus-auto-select-same
5714          (gnus-summary-first-unread-article))
5715     (gnus-summary-position-point)
5716     (gnus-message 6 "Wrapped"))
5717    ;; Try to get next/previous article not displayed in this group.
5718    ((and gnus-auto-extend-newsgroup
5719          (not unread) (not subject))
5720     (gnus-summary-goto-article
5721      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5722      nil (count-lines (point-min) (point))))
5723    ;; Go to next/previous group.
5724    (t
5725     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5726       (gnus-summary-jump-to-group gnus-newsgroup-name))
5727     (let ((cmd last-command-char)
5728           (point
5729            (save-excursion
5730              (set-buffer gnus-group-buffer)
5731              (point)))
5732           (group
5733            (if (eq gnus-keep-same-level 'best)
5734                (gnus-summary-best-group gnus-newsgroup-name)
5735              (gnus-summary-search-group backward gnus-keep-same-level))))
5736       ;; For some reason, the group window gets selected.  We change
5737       ;; it back.
5738       (select-window (get-buffer-window (current-buffer)))
5739       ;; Select next unread newsgroup automagically.
5740       (cond
5741        ((or (not gnus-auto-select-next)
5742             (not cmd))
5743         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5744        ((or (eq gnus-auto-select-next 'quietly)
5745             (and (eq gnus-auto-select-next 'slightly-quietly)
5746                  push)
5747             (and (eq gnus-auto-select-next 'almost-quietly)
5748                  (gnus-summary-last-article-p)))
5749         ;; Select quietly.
5750         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5751             (gnus-summary-exit)
5752           (gnus-message 7 "No more%s articles (%s)..."
5753                         (if unread " unread" "")
5754                         (if group (concat "selecting " group)
5755                           "exiting"))
5756           (gnus-summary-next-group nil group backward)))
5757        (t
5758         (when (gnus-key-press-event-p last-input-event)
5759           (gnus-summary-walk-group-buffer
5760            gnus-newsgroup-name cmd unread backward point))))))))
5761
5762 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5763   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5764                       (?\C-p (gnus-group-prev-unread-group 1))))
5765         (cursor-in-echo-area t)
5766         keve key group ended)
5767     (save-excursion
5768       (set-buffer gnus-group-buffer)
5769       (goto-char start)
5770       (setq group
5771             (if (eq gnus-keep-same-level 'best)
5772                 (gnus-summary-best-group gnus-newsgroup-name)
5773               (gnus-summary-search-group backward gnus-keep-same-level))))
5774     (while (not ended)
5775       (gnus-message
5776        5 "No more%s articles%s" (if unread " unread" "")
5777        (if (and group
5778                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5779            (format " (Type %s for %s [%s])"
5780                    (single-key-description cmd) group
5781                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5782          (format " (Type %s to exit %s)"
5783                  (single-key-description cmd)
5784                  gnus-newsgroup-name)))
5785       ;; Confirm auto selection.
5786       (setq key (car (setq keve (gnus-read-event-char))))
5787       (setq ended t)
5788       (cond
5789        ((assq key keystrokes)
5790         (let ((obuf (current-buffer)))
5791           (switch-to-buffer gnus-group-buffer)
5792           (when group
5793             (gnus-group-jump-to-group group))
5794           (eval (cadr (assq key keystrokes)))
5795           (setq group (gnus-group-group-name))
5796           (switch-to-buffer obuf))
5797         (setq ended nil))
5798        ((equal key cmd)
5799         (if (or (not group)
5800                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5801             (gnus-summary-exit)
5802           (gnus-summary-next-group nil group backward)))
5803        (t
5804         (push (cdr keve) unread-command-events))))))
5805
5806 (defun gnus-summary-next-unread-article ()
5807   "Select unread article after current one."
5808   (interactive)
5809   (gnus-summary-next-article
5810    (or (not (eq gnus-summary-goto-unread 'never))
5811        (gnus-summary-last-article-p (gnus-summary-article-number)))
5812    (and gnus-auto-select-same
5813         (gnus-summary-article-subject))))
5814
5815 (defun gnus-summary-prev-article (&optional unread subject)
5816   "Select the article after the current one.
5817 If UNREAD is non-nil, only unread articles are selected."
5818   (interactive "P")
5819   (gnus-summary-next-article unread subject t))
5820
5821 (defun gnus-summary-prev-unread-article ()
5822   "Select unread article before current one."
5823   (interactive)
5824   (gnus-summary-prev-article
5825    (or (not (eq gnus-summary-goto-unread 'never))
5826        (gnus-summary-first-article-p (gnus-summary-article-number)))
5827    (and gnus-auto-select-same
5828         (gnus-summary-article-subject))))
5829
5830 (defun gnus-summary-next-page (&optional lines circular)
5831   "Show next page of the selected article.
5832 If at the end of the current article, select the next article.
5833 LINES says how many lines should be scrolled up.
5834
5835 If CIRCULAR is non-nil, go to the start of the article instead of
5836 selecting the next article when reaching the end of the current
5837 article."
5838   (interactive "P")
5839   (setq gnus-summary-buffer (current-buffer))
5840   (gnus-set-global-variables)
5841   (let ((article (gnus-summary-article-number))
5842         (article-window (get-buffer-window gnus-article-buffer t))
5843         endp)
5844     ;; If the buffer is empty, we have no article.
5845     (unless article
5846       (error "No article to select"))
5847     (gnus-configure-windows 'article)
5848     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5849         (if (and (eq gnus-summary-goto-unread 'never)
5850                  (not (gnus-summary-last-article-p article)))
5851             (gnus-summary-next-article)
5852           (gnus-summary-next-unread-article))
5853       (if (or (null gnus-current-article)
5854               (null gnus-article-current)
5855               (/= article (cdr gnus-article-current))
5856               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5857           ;; Selected subject is different from current article's.
5858           (gnus-summary-display-article article)
5859         (when article-window
5860           (gnus-eval-in-buffer-window gnus-article-buffer
5861             (setq endp (gnus-article-next-page lines)))
5862           (when endp
5863             (cond (circular
5864                    (gnus-summary-beginning-of-article))
5865                   (lines
5866                    (gnus-message 3 "End of message"))
5867                   ((null lines)
5868                    (if (and (eq gnus-summary-goto-unread 'never)
5869                             (not (gnus-summary-last-article-p article)))
5870                        (gnus-summary-next-article)
5871                      (gnus-summary-next-unread-article))))))))
5872     (gnus-summary-recenter)
5873     (gnus-summary-position-point)))
5874
5875 (defun gnus-summary-prev-page (&optional lines move)
5876   "Show previous page of selected article.
5877 Argument LINES specifies lines to be scrolled down.
5878 If MOVE, move to the previous unread article if point is at
5879 the beginning of the buffer."
5880   (interactive "P")
5881   (let ((article (gnus-summary-article-number))
5882         (article-window (get-buffer-window gnus-article-buffer t))
5883         endp)
5884     (gnus-configure-windows 'article)
5885     (if (or (null gnus-current-article)
5886             (null gnus-article-current)
5887             (/= article (cdr gnus-article-current))
5888             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5889         ;; Selected subject is different from current article's.
5890         (gnus-summary-display-article article)
5891       (gnus-summary-recenter)
5892       (when article-window
5893         (gnus-eval-in-buffer-window gnus-article-buffer
5894           (setq endp (gnus-article-prev-page lines)))
5895         (when (and move endp)
5896           (cond (lines
5897                  (gnus-message 3 "Beginning of message"))
5898                 ((null lines)
5899                  (if (and (eq gnus-summary-goto-unread 'never)
5900                           (not (gnus-summary-first-article-p article)))
5901                      (gnus-summary-prev-article)
5902                    (gnus-summary-prev-unread-article))))))))
5903   (gnus-summary-position-point))
5904
5905 (defun gnus-summary-prev-page-or-article (&optional lines)
5906   "Show previous page of selected article.
5907 Argument LINES specifies lines to be scrolled down.
5908 If at the beginning of the article, go to the next article."
5909   (interactive "P")
5910   (gnus-summary-prev-page lines t))
5911
5912 (defun gnus-summary-scroll-up (lines)
5913   "Scroll up (or down) one line current article.
5914 Argument LINES specifies lines to be scrolled up (or down if negative)."
5915   (interactive "p")
5916   (gnus-configure-windows 'article)
5917   (gnus-summary-show-thread)
5918   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5919     (gnus-eval-in-buffer-window gnus-article-buffer
5920       (cond ((> lines 0)
5921              (when (gnus-article-next-page lines)
5922                (gnus-message 3 "End of message")))
5923             ((< lines 0)
5924              (gnus-article-prev-page (- lines))))))
5925   (gnus-summary-recenter)
5926   (gnus-summary-position-point))
5927
5928 (defun gnus-summary-scroll-down (lines)
5929   "Scroll down (or up) one line current article.
5930 Argument LINES specifies lines to be scrolled down (or up if negative)."
5931   (interactive "p")
5932   (gnus-summary-scroll-up (- lines)))
5933
5934 (defun gnus-summary-next-same-subject ()
5935   "Select next article which has the same subject as current one."
5936   (interactive)
5937   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5938
5939 (defun gnus-summary-prev-same-subject ()
5940   "Select previous article which has the same subject as current one."
5941   (interactive)
5942   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5943
5944 (defun gnus-summary-next-unread-same-subject ()
5945   "Select next unread article which has the same subject as current one."
5946   (interactive)
5947   (gnus-summary-next-article t (gnus-summary-article-subject)))
5948
5949 (defun gnus-summary-prev-unread-same-subject ()
5950   "Select previous unread article which has the same subject as current one."
5951   (interactive)
5952   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5953
5954 (defun gnus-summary-first-unread-article ()
5955   "Select the first unread article.
5956 Return nil if there are no unread articles."
5957   (interactive)
5958   (prog1
5959       (when (gnus-summary-first-subject t)
5960         (gnus-summary-show-thread)
5961         (gnus-summary-first-subject t)
5962         (gnus-summary-display-article (gnus-summary-article-number)))
5963     (gnus-summary-position-point)))
5964
5965 (defun gnus-summary-first-unread-subject ()
5966   "Place the point on the subject line of the first unread article.
5967 Return nil if there are no unread articles."
5968   (interactive)
5969   (prog1
5970       (when (gnus-summary-first-subject t)
5971         (gnus-summary-show-thread)
5972         (gnus-summary-first-subject t))
5973     (gnus-summary-position-point)))
5974
5975 (defun gnus-summary-first-article ()
5976   "Select the first article.
5977 Return nil if there are no articles."
5978   (interactive)
5979   (prog1
5980       (when (gnus-summary-first-subject)
5981         (gnus-summary-show-thread)
5982         (gnus-summary-first-subject)
5983         (gnus-summary-display-article (gnus-summary-article-number)))
5984     (gnus-summary-position-point)))
5985
5986 (defun gnus-summary-best-unread-article ()
5987   "Select the unread article with the highest score."
5988   (interactive)
5989   (let ((best -1000000)
5990         (data gnus-newsgroup-data)
5991         article score)
5992     (while data
5993       (and (gnus-data-unread-p (car data))
5994            (> (setq score
5995                     (gnus-summary-article-score (gnus-data-number (car data))))
5996               best)
5997            (setq best score
5998                  article (gnus-data-number (car data))))
5999       (setq data (cdr data)))
6000     (prog1
6001         (if article
6002             (gnus-summary-goto-article article)
6003           (error "No unread articles"))
6004       (gnus-summary-position-point))))
6005
6006 (defun gnus-summary-last-subject ()
6007   "Go to the last displayed subject line in the group."
6008   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6009     (when article
6010       (gnus-summary-goto-subject article))))
6011
6012 (defun gnus-summary-goto-article (article &optional all-headers force)
6013   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6014 If ALL-HEADERS is non-nil, no header lines are hidden.
6015 If FORCE, go to the article even if it isn't displayed.  If FORCE
6016 is a number, it is the line the article is to be displayed on."
6017   (interactive
6018    (list
6019     (completing-read
6020      "Article number or Message-ID: "
6021      (mapcar (lambda (number) (list (int-to-string number)))
6022              gnus-newsgroup-limit))
6023     current-prefix-arg
6024     t))
6025   (prog1
6026       (if (and (stringp article)
6027                (string-match "@" article))
6028           (gnus-summary-refer-article article)
6029         (when (stringp article)
6030           (setq article (string-to-number article)))
6031         (if (gnus-summary-goto-subject article force)
6032             (gnus-summary-display-article article all-headers)
6033           (gnus-message 4 "Couldn't go to article %s" article) nil))
6034     (gnus-summary-position-point)))
6035
6036 (defun gnus-summary-goto-last-article ()
6037   "Go to the previously read article."
6038   (interactive)
6039   (prog1
6040       (when gnus-last-article
6041         (gnus-summary-goto-article gnus-last-article nil t))
6042     (gnus-summary-position-point)))
6043
6044 (defun gnus-summary-pop-article (number)
6045   "Pop one article off the history and go to the previous.
6046 NUMBER articles will be popped off."
6047   (interactive "p")
6048   (let (to)
6049     (setq gnus-newsgroup-history
6050           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6051     (if to
6052         (gnus-summary-goto-article (car to) nil t)
6053       (error "Article history empty")))
6054   (gnus-summary-position-point))
6055
6056 ;; Summary commands and functions for limiting the summary buffer.
6057
6058 (defun gnus-summary-limit-to-articles (n)
6059   "Limit the summary buffer to the next N articles.
6060 If not given a prefix, use the process marked articles instead."
6061   (interactive "P")
6062   (prog1
6063       (let ((articles (gnus-summary-work-articles n)))
6064         (setq gnus-newsgroup-processable nil)
6065         (gnus-summary-limit articles))
6066     (gnus-summary-position-point)))
6067
6068 (defun gnus-summary-pop-limit (&optional total)
6069   "Restore the previous limit.
6070 If given a prefix, remove all limits."
6071   (interactive "P")
6072   (when total
6073     (setq gnus-newsgroup-limits
6074           (list (mapcar (lambda (h) (mail-header-number h))
6075                         gnus-newsgroup-headers))))
6076   (unless gnus-newsgroup-limits
6077     (error "No limit to pop"))
6078   (prog1
6079       (gnus-summary-limit nil 'pop)
6080     (gnus-summary-position-point)))
6081
6082 (defun gnus-summary-limit-to-subject (subject &optional header)
6083   "Limit the summary buffer to articles that have subjects that match a regexp."
6084   (interactive "sLimit to subject (regexp): ")
6085   (unless header
6086     (setq header "subject"))
6087   (when (not (equal "" subject))
6088     (prog1
6089         (let ((articles (gnus-summary-find-matching
6090                          (or header "subject") subject 'all)))
6091           (unless articles
6092             (error "Found no matches for \"%s\"" subject))
6093           (gnus-summary-limit articles))
6094       (gnus-summary-position-point))))
6095
6096 (defun gnus-summary-limit-to-author (from)
6097   "Limit the summary buffer to articles that have authors that match a regexp."
6098   (interactive "sLimit to author (regexp): ")
6099   (gnus-summary-limit-to-subject from "from"))
6100
6101 (defun gnus-summary-limit-to-age (age &optional younger-p)
6102   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6103 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6104 articles that are younger than AGE days."
6105   (interactive "nTime in days: \nP")
6106   (prog1
6107       (let ((data gnus-newsgroup-data)
6108             (cutoff (days-to-time age))
6109             articles d date is-younger)
6110         (while (setq d (pop data))
6111           (when (and (vectorp (gnus-data-header d))
6112                      (setq date (mail-header-date (gnus-data-header d))))
6113             (setq is-younger (time-less-p
6114                               (time-since (date-to-time date))
6115                               cutoff))
6116             (when (if younger-p
6117                       is-younger
6118                     (not is-younger))
6119               (push (gnus-data-number d) articles))))
6120         (gnus-summary-limit (nreverse articles)))
6121     (gnus-summary-position-point)))
6122
6123 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6124 (make-obsolete
6125  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6126
6127 (defun gnus-summary-limit-to-unread (&optional all)
6128   "Limit the summary buffer to articles that are not marked as read.
6129 If ALL is non-nil, limit strictly to unread articles."
6130   (interactive "P")
6131   (if all
6132       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6133     (gnus-summary-limit-to-marks
6134      ;; Concat all the marks that say that an article is read and have
6135      ;; those removed.
6136      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6137            gnus-killed-mark gnus-kill-file-mark
6138            gnus-low-score-mark gnus-expirable-mark
6139            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6140            gnus-duplicate-mark gnus-souped-mark)
6141      'reverse)))
6142
6143 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6144 (make-obsolete 'gnus-summary-delete-marked-with
6145                'gnus-summary-limit-exlude-marks)
6146
6147 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6148   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6149 If REVERSE, limit the summary buffer to articles that are marked
6150 with MARKS.  MARKS can either be a string of marks or a list of marks.
6151 Returns how many articles were removed."
6152   (interactive "sMarks: ")
6153   (gnus-summary-limit-to-marks marks t))
6154
6155 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6156   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6157 If REVERSE (the prefix), limit the summary buffer to articles that are
6158 not marked with MARKS.  MARKS can either be a string of marks or a
6159 list of marks.
6160 Returns how many articles were removed."
6161   (interactive "sMarks: \nP")
6162   (prog1
6163       (let ((data gnus-newsgroup-data)
6164             (marks (if (listp marks) marks
6165                      (append marks nil))) ; Transform to list.
6166             articles)
6167         (while data
6168           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6169                   (memq (gnus-data-mark (car data)) marks))
6170             (push (gnus-data-number (car data)) articles))
6171           (setq data (cdr data)))
6172         (gnus-summary-limit articles))
6173     (gnus-summary-position-point)))
6174
6175 (defun gnus-summary-limit-to-score (&optional score)
6176   "Limit to articles with score at or above SCORE."
6177   (interactive "P")
6178   (setq score (if score
6179                   (prefix-numeric-value score)
6180                 (or gnus-summary-default-score 0)))
6181   (let ((data gnus-newsgroup-data)
6182         articles)
6183     (while data
6184       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6185                 score)
6186         (push (gnus-data-number (car data)) articles))
6187       (setq data (cdr data)))
6188     (prog1
6189         (gnus-summary-limit articles)
6190       (gnus-summary-position-point))))
6191
6192 (defun gnus-summary-limit-include-thread (id)
6193   "Display all the hidden articles that in the current thread."
6194   (interactive (list (mail-header-id (gnus-summary-article-header))))
6195   (let ((articles (gnus-articles-in-thread
6196                    (gnus-id-to-thread (gnus-root-id id)))))
6197     (prog1
6198         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6199       (gnus-summary-position-point))))
6200
6201 (defun gnus-summary-limit-include-dormant ()
6202   "Display all the hidden articles that are marked as dormant.
6203 Note that this command only works on a subset of the articles currently
6204 fetched for this group."
6205   (interactive)
6206   (unless gnus-newsgroup-dormant
6207     (error "There are no dormant articles in this group"))
6208   (prog1
6209       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6210     (gnus-summary-position-point)))
6211
6212 (defun gnus-summary-limit-exclude-dormant ()
6213   "Hide all dormant articles."
6214   (interactive)
6215   (prog1
6216       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6217     (gnus-summary-position-point)))
6218
6219 (defun gnus-summary-limit-exclude-childless-dormant ()
6220   "Hide all dormant articles that have no children."
6221   (interactive)
6222   (let ((data (gnus-data-list t))
6223         articles d children)
6224     ;; Find all articles that are either not dormant or have
6225     ;; children.
6226     (while (setq d (pop data))
6227       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6228                 (and (setq children
6229                            (gnus-article-children (gnus-data-number d)))
6230                      (let (found)
6231                        (while children
6232                          (when (memq (car children) articles)
6233                            (setq children nil
6234                                  found t))
6235                          (pop children))
6236                        found)))
6237         (push (gnus-data-number d) articles)))
6238     ;; Do the limiting.
6239     (prog1
6240         (gnus-summary-limit articles)
6241       (gnus-summary-position-point))))
6242
6243 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6244   "Mark all unread excluded articles as read.
6245 If ALL, mark even excluded ticked and dormants as read."
6246   (interactive "P")
6247   (let ((articles (gnus-sorted-complement
6248                    (sort
6249                     (mapcar (lambda (h) (mail-header-number h))
6250                             gnus-newsgroup-headers)
6251                     '<)
6252                    (sort gnus-newsgroup-limit '<)))
6253         article)
6254     (setq gnus-newsgroup-unreads
6255           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6256     (if all
6257         (setq gnus-newsgroup-dormant nil
6258               gnus-newsgroup-marked nil
6259               gnus-newsgroup-reads
6260               (nconc
6261                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6262                gnus-newsgroup-reads))
6263       (while (setq article (pop articles))
6264         (unless (or (memq article gnus-newsgroup-dormant)
6265                     (memq article gnus-newsgroup-marked))
6266           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6267
6268 (defun gnus-summary-limit (articles &optional pop)
6269   (if pop
6270       ;; We pop the previous limit off the stack and use that.
6271       (setq articles (car gnus-newsgroup-limits)
6272             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6273     ;; We use the new limit, so we push the old limit on the stack.
6274     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6275   ;; Set the limit.
6276   (setq gnus-newsgroup-limit articles)
6277   (let ((total (length gnus-newsgroup-data))
6278         (data (gnus-data-find-list (gnus-summary-article-number)))
6279         (gnus-summary-mark-below nil)   ; Inhibit this.
6280         found)
6281     ;; This will do all the work of generating the new summary buffer
6282     ;; according to the new limit.
6283     (gnus-summary-prepare)
6284     ;; Hide any threads, possibly.
6285     (and gnus-show-threads
6286          gnus-thread-hide-subtree
6287          (gnus-summary-hide-all-threads))
6288     ;; Try to return to the article you were at, or one in the
6289     ;; neighborhood.
6290     (when data
6291       ;; We try to find some article after the current one.
6292       (while data
6293         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6294           (setq data nil
6295                 found t))
6296         (setq data (cdr data))))
6297     (unless found
6298       ;; If there is no data, that means that we were after the last
6299       ;; article.  The same goes when we can't find any articles
6300       ;; after the current one.
6301       (goto-char (point-max))
6302       (gnus-summary-find-prev))
6303     (gnus-set-mode-line 'summary)
6304     ;; We return how many articles were removed from the summary
6305     ;; buffer as a result of the new limit.
6306     (- total (length gnus-newsgroup-data))))
6307
6308 (defsubst gnus-invisible-cut-children (threads)
6309   (let ((num 0))
6310     (while threads
6311       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6312         (incf num))
6313       (pop threads))
6314     (< num 2)))
6315
6316 (defsubst gnus-cut-thread (thread)
6317   "Go forwards in the thread until we find an article that we want to display."
6318   (when (or (eq gnus-fetch-old-headers 'some)
6319             (eq gnus-fetch-old-headers 'invisible)
6320             (eq gnus-build-sparse-threads 'some)
6321             (eq gnus-build-sparse-threads 'more))
6322     ;; Deal with old-fetched headers and sparse threads.
6323     (while (and
6324             thread
6325             (or
6326              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6327              (gnus-summary-article-ancient-p
6328               (mail-header-number (car thread))))
6329             (if (or (<= (length (cdr thread)) 1)
6330                     (eq gnus-fetch-old-headers 'invisible))
6331                 (setq gnus-newsgroup-limit
6332                       (delq (mail-header-number (car thread))
6333                             gnus-newsgroup-limit)
6334                       thread (cadr thread))
6335               (when (gnus-invisible-cut-children (cdr thread))
6336                 (let ((th (cdr thread)))
6337                   (while th
6338                     (if (memq (mail-header-number (caar th))
6339                               gnus-newsgroup-limit)
6340                         (setq thread (car th)
6341                               th nil)
6342                       (setq th (cdr th))))))))))
6343   thread)
6344
6345 (defun gnus-cut-threads (threads)
6346   "Cut off all uninteresting articles from the beginning of threads."
6347   (when (or (eq gnus-fetch-old-headers 'some)
6348             (eq gnus-fetch-old-headers 'invisible)
6349             (eq gnus-build-sparse-threads 'some)
6350             (eq gnus-build-sparse-threads 'more))
6351     (let ((th threads))
6352       (while th
6353         (setcar th (gnus-cut-thread (car th)))
6354         (setq th (cdr th)))))
6355   ;; Remove nixed out threads.
6356   (delq nil threads))
6357
6358 (defun gnus-summary-initial-limit (&optional show-if-empty)
6359   "Figure out what the initial limit is supposed to be on group entry.
6360 This entails weeding out unwanted dormants, low-scored articles,
6361 fetch-old-headers verbiage, and so on."
6362   ;; Most groups have nothing to remove.
6363   (if (or gnus-inhibit-limiting
6364           (and (null gnus-newsgroup-dormant)
6365                (not (eq gnus-fetch-old-headers 'some))
6366                (not (eq gnus-fetch-old-headers 'invisible))
6367                (null gnus-summary-expunge-below)
6368                (not (eq gnus-build-sparse-threads 'some))
6369                (not (eq gnus-build-sparse-threads 'more))
6370                (null gnus-thread-expunge-below)
6371                (not gnus-use-nocem)))
6372       ()                                ; Do nothing.
6373     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6374     (setq gnus-newsgroup-limit nil)
6375     (mapatoms
6376      (lambda (node)
6377        (unless (car (symbol-value node))
6378          ;; These threads have no parents -- they are roots.
6379          (let ((nodes (cdr (symbol-value node)))
6380                thread)
6381            (while nodes
6382              (if (and gnus-thread-expunge-below
6383                       (< (gnus-thread-total-score (car nodes))
6384                          gnus-thread-expunge-below))
6385                  (gnus-expunge-thread (pop nodes))
6386                (setq thread (pop nodes))
6387                (gnus-summary-limit-children thread))))))
6388      gnus-newsgroup-dependencies)
6389     ;; If this limitation resulted in an empty group, we might
6390     ;; pop the previous limit and use it instead.
6391     (when (and (not gnus-newsgroup-limit)
6392                show-if-empty)
6393       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6394     gnus-newsgroup-limit))
6395
6396 (defun gnus-summary-limit-children (thread)
6397   "Return 1 if this subthread is visible and 0 if it is not."
6398   ;; First we get the number of visible children to this thread.  This
6399   ;; is done by recursing down the thread using this function, so this
6400   ;; will really go down to a leaf article first, before slowly
6401   ;; working its way up towards the root.
6402   (when thread
6403     (let ((children
6404            (if (cdr thread)
6405                (apply '+ (mapcar 'gnus-summary-limit-children
6406                                  (cdr thread)))
6407              0))
6408           (number (mail-header-number (car thread)))
6409           score)
6410       (if (and
6411            (not (memq number gnus-newsgroup-marked))
6412            (or
6413             ;; If this article is dormant and has absolutely no visible
6414             ;; children, then this article isn't visible.
6415             (and (memq number gnus-newsgroup-dormant)
6416                  (zerop children))
6417             ;; If this is "fetch-old-headered" and there is no
6418             ;; visible children, then we don't want this article.
6419             (and (eq gnus-fetch-old-headers 'some)
6420                  (gnus-summary-article-ancient-p number)
6421                  (zerop children))
6422             ;; If this is "fetch-old-headered" and `invisible', then
6423             ;; we don't want this article.
6424             (and (eq gnus-fetch-old-headers 'invisible)
6425                  (gnus-summary-article-ancient-p number))
6426             ;; If this is a sparsely inserted article with no children,
6427             ;; we don't want it.
6428             (and (eq gnus-build-sparse-threads 'some)
6429                  (gnus-summary-article-sparse-p number)
6430                  (zerop children))
6431             ;; If we use expunging, and this article is really
6432             ;; low-scored, then we don't want this article.
6433             (when (and gnus-summary-expunge-below
6434                        (< (setq score
6435                                 (or (cdr (assq number gnus-newsgroup-scored))
6436                                     gnus-summary-default-score))
6437                           gnus-summary-expunge-below))
6438               ;; We increase the expunge-tally here, but that has
6439               ;; nothing to do with the limits, really.
6440               (incf gnus-newsgroup-expunged-tally)
6441               ;; We also mark as read here, if that's wanted.
6442               (when (and gnus-summary-mark-below
6443                          (< score gnus-summary-mark-below))
6444                 (setq gnus-newsgroup-unreads
6445                       (delq number gnus-newsgroup-unreads))
6446                 (if gnus-newsgroup-auto-expire
6447                     (push number gnus-newsgroup-expirable)
6448                   (push (cons number gnus-low-score-mark)
6449                         gnus-newsgroup-reads)))
6450               t)
6451             ;; Check NoCeM things.
6452             (if (and gnus-use-nocem
6453                      (gnus-nocem-unwanted-article-p
6454                       (mail-header-id (car thread))))
6455                 (progn
6456                   (setq gnus-newsgroup-unreads
6457                         (delq number gnus-newsgroup-unreads))
6458                   t))))
6459           ;; Nope, invisible article.
6460           0
6461         ;; Ok, this article is to be visible, so we add it to the limit
6462         ;; and return 1.
6463         (push number gnus-newsgroup-limit)
6464         1))))
6465
6466 (defun gnus-expunge-thread (thread)
6467   "Mark all articles in THREAD as read."
6468   (let* ((number (mail-header-number (car thread))))
6469     (incf gnus-newsgroup-expunged-tally)
6470     ;; We also mark as read here, if that's wanted.
6471     (setq gnus-newsgroup-unreads
6472           (delq number gnus-newsgroup-unreads))
6473     (if gnus-newsgroup-auto-expire
6474         (push number gnus-newsgroup-expirable)
6475       (push (cons number gnus-low-score-mark)
6476             gnus-newsgroup-reads)))
6477   ;; Go recursively through all subthreads.
6478   (mapcar 'gnus-expunge-thread (cdr thread)))
6479
6480 ;; Summary article oriented commands
6481
6482 (defun gnus-summary-refer-parent-article (n)
6483   "Refer parent article N times.
6484 If N is negative, go to ancestor -N instead.
6485 The difference between N and the number of articles fetched is returned."
6486   (interactive "p")
6487   (let ((skip 1)
6488         error header ref)
6489     (when (not (natnump n))
6490       (setq skip (abs n)
6491             n 1))
6492     (while (and (> n 0)
6493                 (not error))
6494       (setq header (gnus-summary-article-header))
6495       (if (and (eq (mail-header-number header)
6496                    (cdr gnus-article-current))
6497                (equal gnus-newsgroup-name
6498                       (car gnus-article-current)))
6499           ;; If we try to find the parent of the currently
6500           ;; displayed article, then we take a look at the actual
6501           ;; References header, since this is slightly more
6502           ;; reliable than the References field we got from the
6503           ;; server.
6504           (save-excursion
6505             (set-buffer gnus-original-article-buffer)
6506             (nnheader-narrow-to-headers)
6507             (unless (setq ref (message-fetch-field "references"))
6508               (setq ref (message-fetch-field "in-reply-to")))
6509             (widen))
6510         (setq ref
6511               ;; It's not the current article, so we take a bet on
6512               ;; the value we got from the server.
6513               (mail-header-references header)))
6514       (if (and ref
6515                (not (equal ref "")))
6516           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6517             (gnus-message 1 "Couldn't find parent"))
6518         (gnus-message 1 "No references in article %d"
6519                       (gnus-summary-article-number))
6520         (setq error t))
6521       (decf n))
6522     (gnus-summary-position-point)
6523     n))
6524
6525 (defun gnus-summary-refer-references ()
6526   "Fetch all articles mentioned in the References header.
6527 Return the number of articles fetched."
6528   (interactive)
6529   (let ((ref (mail-header-references (gnus-summary-article-header)))
6530         (current (gnus-summary-article-number))
6531         (n 0))
6532     (if (or (not ref)
6533             (equal ref ""))
6534         (error "No References in the current article")
6535       ;; For each Message-ID in the References header...
6536       (while (string-match "<[^>]*>" ref)
6537         (incf n)
6538         ;; ... fetch that article.
6539         (gnus-summary-refer-article
6540          (prog1 (match-string 0 ref)
6541            (setq ref (substring ref (match-end 0))))))
6542       (gnus-summary-goto-subject current)
6543       (gnus-summary-position-point)
6544       n)))
6545
6546 (defun gnus-summary-refer-thread (&optional limit)
6547   "Fetch all articles in the current thread.
6548 If LIMIT (the numerical prefix), fetch that many old headers instead
6549 of what's specified by the `gnus-refer-thread-limit' variable."
6550   (interactive "P")
6551   (let ((id (mail-header-id (gnus-summary-article-header)))
6552         (limit (if limit (prefix-numeric-value limit)
6553                  gnus-refer-thread-limit)))
6554     ;; We want to fetch LIMIT *old* headers, but we also have to
6555     ;; re-fetch all the headers in the current buffer, because many of
6556     ;; them may be undisplayed.  So we adjust LIMIT.
6557     (when (numberp limit)
6558       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6559     (unless (eq gnus-fetch-old-headers 'invisible)
6560       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6561       ;; Retrieve the headers and read them in.
6562       (if (eq (gnus-retrieve-headers
6563                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6564               'nov)
6565           (gnus-build-all-threads)
6566         (error "Can't fetch thread from backends that don't support NOV"))
6567       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6568     (gnus-summary-limit-include-thread id)))
6569
6570 (defun gnus-summary-refer-article (message-id &optional arg)
6571   "Fetch an article specified by MESSAGE-ID.
6572 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6573 or `gnus-select-method', no matter what backend the article comes from."
6574   (interactive "sMessage-ID: \nP")
6575   (when (and (stringp message-id)
6576              (not (zerop (length message-id))))
6577     ;; Construct the correct Message-ID if necessary.
6578     ;; Suggested by tale@pawl.rpi.edu.
6579     (unless (string-match "^<" message-id)
6580       (setq message-id (concat "<" message-id)))
6581     (unless (string-match ">$" message-id)
6582       (setq message-id (concat message-id ">")))
6583     (let* ((header (gnus-id-to-header message-id))
6584            (sparse (and header
6585                         (gnus-summary-article-sparse-p
6586                          (mail-header-number header))
6587                         (memq (mail-header-number header)
6588                               gnus-newsgroup-limit))))
6589       (cond
6590        ;; If the article is present in the buffer we just go to it.
6591        ((and header
6592              (or (not (gnus-summary-article-sparse-p
6593                        (mail-header-number header)))
6594                  sparse))
6595         (prog1
6596             (gnus-summary-goto-article
6597              (mail-header-number header) nil t)
6598           (when sparse
6599             (gnus-summary-update-article (mail-header-number header)))))
6600        (t
6601         ;; We fetch the article
6602         (let ((gnus-override-method
6603                (cond ((gnus-news-group-p gnus-newsgroup-name)
6604                       gnus-refer-article-method)
6605                      (arg
6606                       (or gnus-refer-article-method gnus-select-method))
6607                      (t nil)))
6608               number)
6609           ;; Start the special refer-article method, if necessary.
6610           (when (and gnus-refer-article-method
6611                      (gnus-news-group-p gnus-newsgroup-name))
6612             (gnus-check-server gnus-refer-article-method))
6613           ;; Fetch the header, and display the article.
6614           (if (setq number (gnus-summary-insert-subject message-id))
6615               (gnus-summary-select-article nil nil nil number)
6616             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6617
6618 (defun gnus-summary-edit-parameters ()
6619   "Edit the group parameters of the current group."
6620   (interactive)
6621   (gnus-group-edit-group gnus-newsgroup-name 'params))
6622
6623 (defun gnus-summary-customize-parameters ()
6624   "Customize the group parameters of the current group."
6625   (interactive)
6626   (gnus-group-customize gnus-newsgroup-name))
6627
6628 (defun gnus-summary-enter-digest-group (&optional force)
6629   "Enter an nndoc group based on the current article.
6630 If FORCE, force a digest interpretation.  If not, try
6631 to guess what the document format is."
6632   (interactive "P")
6633   (let ((conf gnus-current-window-configuration))
6634     (save-excursion
6635       (gnus-summary-select-article))
6636     (setq gnus-current-window-configuration conf)
6637     (let* ((name (format "%s-%d"
6638                          (gnus-group-prefixed-name
6639                           gnus-newsgroup-name (list 'nndoc ""))
6640                          (save-excursion
6641                            (set-buffer gnus-summary-buffer)
6642                            gnus-current-article)))
6643            (ogroup gnus-newsgroup-name)
6644            (params (append (gnus-info-params (gnus-get-info ogroup))
6645                            (list (cons 'to-group ogroup))
6646                            (list (cons 'save-article-group ogroup))))
6647            (case-fold-search t)
6648            (buf (current-buffer))
6649            dig)
6650       (save-excursion
6651         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6652         (insert-buffer-substring gnus-original-article-buffer)
6653         ;; Remove lines that may lead nndoc to misinterpret the
6654         ;; document type.
6655         (narrow-to-region
6656          (goto-char (point-min))
6657          (or (search-forward "\n\n" nil t) (point)))
6658         (goto-char (point-min))
6659         (delete-matching-lines "^\\(Path\\):\\|^From ")
6660         (widen))
6661       (unwind-protect
6662           (if (gnus-group-read-ephemeral-group
6663                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6664                             (nndoc-article-type
6665                              ,(if force 'digest 'guess))) t)
6666               ;; Make all postings to this group go to the parent group.
6667               (nconc (gnus-info-params (gnus-get-info name))
6668                      params)
6669             ;; Couldn't select this doc group.
6670             (switch-to-buffer buf)
6671             (gnus-set-global-variables)
6672             (gnus-configure-windows 'summary)
6673             (gnus-message 3 "Article couldn't be entered?"))
6674         (kill-buffer dig)))))
6675
6676 (defun gnus-summary-read-document (n)
6677   "Open a new group based on the current article(s).
6678 This will allow you to read digests and other similar
6679 documents as newsgroups.
6680 Obeys the standard process/prefix convention."
6681   (interactive "P")
6682   (let* ((articles (gnus-summary-work-articles n))
6683          (ogroup gnus-newsgroup-name)
6684          (params (append (gnus-info-params (gnus-get-info ogroup))
6685                          (list (cons 'to-group ogroup))))
6686          article group egroup groups vgroup)
6687     (while (setq article (pop articles))
6688       (setq group (format "%s-%d" gnus-newsgroup-name article))
6689       (gnus-summary-remove-process-mark article)
6690       (when (gnus-summary-display-article article)
6691         (save-excursion
6692           (with-temp-buffer
6693             (insert-buffer-substring gnus-original-article-buffer)
6694             ;; Remove some headers that may lead nndoc to make
6695             ;; the wrong guess.
6696             (message-narrow-to-head)
6697             (goto-char (point-min))
6698             (delete-matching-lines "^\\(Path\\):\\|^From ")
6699             (widen)
6700             (if (setq egroup
6701                       (gnus-group-read-ephemeral-group
6702                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6703                                      (nndoc-article-type guess))
6704                        t nil t))
6705                 (progn
6706                   ;; Make all postings to this group go to the parent group.
6707                   (nconc (gnus-info-params (gnus-get-info egroup))
6708                          params)
6709                   (push egroup groups))
6710               ;; Couldn't select this doc group.
6711               (gnus-error 3 "Article couldn't be entered"))))))
6712     ;; Now we have selected all the documents.
6713     (cond
6714      ((not groups)
6715       (error "None of the articles could be interpreted as documents"))
6716      ((gnus-group-read-ephemeral-group
6717        (setq vgroup (format
6718                      "nnvirtual:%s-%s" gnus-newsgroup-name
6719                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6720        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6721        t
6722        (cons (current-buffer) 'summary)))
6723      (t
6724       (error "Couldn't select virtual nndoc group")))))
6725
6726 (defun gnus-summary-isearch-article (&optional regexp-p)
6727   "Do incremental search forward on the current article.
6728 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6729   (interactive "P")
6730   (gnus-summary-select-article)
6731   (gnus-configure-windows 'article)
6732   (gnus-eval-in-buffer-window gnus-article-buffer
6733     (save-restriction
6734       (widen)
6735       (isearch-forward regexp-p))))
6736
6737 (defun gnus-summary-search-article-forward (regexp &optional backward)
6738   "Search for an article containing REGEXP forward.
6739 If BACKWARD, search backward instead."
6740   (interactive
6741    (list (read-string
6742           (format "Search article %s (regexp%s): "
6743                   (if current-prefix-arg "backward" "forward")
6744                   (if gnus-last-search-regexp
6745                       (concat ", default " gnus-last-search-regexp)
6746                     "")))
6747          current-prefix-arg))
6748   (if (string-equal regexp "")
6749       (setq regexp (or gnus-last-search-regexp ""))
6750     (setq gnus-last-search-regexp regexp))
6751   (if (gnus-summary-search-article regexp backward)
6752       (gnus-summary-show-thread)
6753     (error "Search failed: \"%s\"" regexp)))
6754
6755 (defun gnus-summary-search-article-backward (regexp)
6756   "Search for an article containing REGEXP backward."
6757   (interactive
6758    (list (read-string
6759           (format "Search article backward (regexp%s): "
6760                   (if gnus-last-search-regexp
6761                       (concat ", default " gnus-last-search-regexp)
6762                     "")))))
6763   (gnus-summary-search-article-forward regexp 'backward))
6764
6765 (defun gnus-summary-search-article (regexp &optional backward)
6766   "Search for an article containing REGEXP.
6767 Optional argument BACKWARD means do search for backward.
6768 `gnus-select-article-hook' is not called during the search."
6769   ;; We have to require this here to make sure that the following
6770   ;; dynamic binding isn't shadowed by autoloading.
6771   (require 'gnus-async)
6772   (require 'gnus-art)
6773   (let ((gnus-select-article-hook nil)  ;Disable hook.
6774         (gnus-article-display-hook nil)
6775         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6776         (gnus-use-article-prefetch nil)
6777         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6778         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6779         (sum (current-buffer))
6780         (found nil)
6781         point)
6782     (gnus-save-hidden-threads
6783       (gnus-summary-select-article)
6784       (set-buffer gnus-article-buffer)
6785       (when backward
6786         (forward-line -1))
6787       (while (not found)
6788         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6789         (if (if backward
6790                 (re-search-backward regexp nil t)
6791               (re-search-forward regexp nil t))
6792             ;; We found the regexp.
6793             (progn
6794               (setq found 'found)
6795               (beginning-of-line)
6796               (set-window-start
6797                (get-buffer-window (current-buffer))
6798                (point))
6799               (forward-line 1)
6800               (set-buffer sum)
6801               (setq point (point)))
6802           ;; We didn't find it, so we go to the next article.
6803           (set-buffer sum)
6804           (setq found 'not)
6805           (while (eq found 'not)
6806             (if (not (if backward (gnus-summary-find-prev)
6807                        (gnus-summary-find-next)))
6808                 ;; No more articles.
6809                 (setq found t)
6810               ;; Select the next article and adjust point.
6811               (unless (gnus-summary-article-sparse-p
6812                        (gnus-summary-article-number))
6813                 (setq found nil)
6814                 (gnus-summary-select-article)
6815                 (set-buffer gnus-article-buffer)
6816                 (widen)
6817                 (goto-char (if backward (point-max) (point-min))))))))
6818       (gnus-message 7 ""))
6819     ;; Return whether we found the regexp.
6820     (when (eq found 'found)
6821       (goto-char point)
6822       (gnus-summary-show-thread)
6823       (gnus-summary-goto-subject gnus-current-article)
6824       (gnus-summary-position-point)
6825       t)))
6826
6827 (defun gnus-summary-find-matching (header regexp &optional backward unread
6828                                           not-case-fold)
6829   "Return a list of all articles that match REGEXP on HEADER.
6830 The search stars on the current article and goes forwards unless
6831 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6832 If UNREAD is non-nil, only unread articles will
6833 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6834 in the comparisons."
6835   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6836                 (gnus-data-find-list
6837                  (gnus-summary-article-number) (gnus-data-list backward))))
6838         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6839         (case-fold-search (not not-case-fold))
6840         articles d)
6841     (unless (fboundp (intern (concat "mail-header-" header)))
6842       (error "%s is not a valid header" header))
6843     (while data
6844       (setq d (car data))
6845       (and (or (not unread)             ; We want all articles...
6846                (gnus-data-unread-p d))  ; Or just unreads.
6847            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6848            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6849            (push (gnus-data-number d) articles)) ; Success!
6850       (setq data (cdr data)))
6851     (nreverse articles)))
6852
6853 (defun gnus-summary-execute-command (header regexp command &optional backward)
6854   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6855 If HEADER is an empty string (or nil), the match is done on the entire
6856 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6857   (interactive
6858    (list (let ((completion-ignore-case t))
6859            (completing-read
6860             "Header name: "
6861             (mapcar (lambda (string) (list string))
6862                     '("Number" "Subject" "From" "Lines" "Date"
6863                       "Message-ID" "Xref" "References" "Body"))
6864             nil 'require-match))
6865          (read-string "Regexp: ")
6866          (read-key-sequence "Command: ")
6867          current-prefix-arg))
6868   (when (equal header "Body")
6869     (setq header ""))
6870   ;; Hidden thread subtrees must be searched as well.
6871   (gnus-summary-show-all-threads)
6872   ;; We don't want to change current point nor window configuration.
6873   (save-excursion
6874     (save-window-excursion
6875       (gnus-message 6 "Executing %s..." (key-description command))
6876       ;; We'd like to execute COMMAND interactively so as to give arguments.
6877       (gnus-execute header regexp
6878                     `(call-interactively ',(key-binding command))
6879                     backward)
6880       (gnus-message 6 "Executing %s...done" (key-description command)))))
6881
6882 (defun gnus-summary-beginning-of-article ()
6883   "Scroll the article back to the beginning."
6884   (interactive)
6885   (gnus-summary-select-article)
6886   (gnus-configure-windows 'article)
6887   (gnus-eval-in-buffer-window gnus-article-buffer
6888     (widen)
6889     (goto-char (point-min))
6890     (when gnus-page-broken
6891       (gnus-narrow-to-page))))
6892
6893 (defun gnus-summary-end-of-article ()
6894   "Scroll to the end of the article."
6895   (interactive)
6896   (gnus-summary-select-article)
6897   (gnus-configure-windows 'article)
6898   (gnus-eval-in-buffer-window gnus-article-buffer
6899     (widen)
6900     (goto-char (point-max))
6901     (recenter -3)
6902     (when gnus-page-broken
6903       (gnus-narrow-to-page))))
6904
6905 (defun gnus-summary-print-article (&optional filename n)
6906   "Generate and print a PostScript image of the N next (mail) articles.
6907
6908 If N is negative, print the N previous articles.  If N is nil and articles
6909 have been marked with the process mark, print these instead.
6910
6911 If the optional second argument FILENAME is nil, send the image to the
6912 printer.  If FILENAME is a string, save the PostScript image in a file with
6913 that name.  If FILENAME is a number, prompt the user for the name of the file
6914 to save in."
6915   (interactive (list (ps-print-preprint current-prefix-arg)
6916                      current-prefix-arg))
6917   (dolist (article (gnus-summary-work-articles n))
6918     (gnus-summary-select-article nil nil 'pseudo article)
6919     (gnus-eval-in-buffer-window gnus-article-buffer
6920       (let ((buffer (generate-new-buffer " *print*")))
6921         (unwind-protect
6922             (progn
6923               (copy-to-buffer buffer (point-min) (point-max))
6924               (set-buffer buffer)
6925               (gnus-article-delete-invisible-text)
6926               (let ((ps-left-header
6927                      (list
6928                       (concat "("
6929                               (mail-header-subject gnus-current-headers) ")")
6930                       (concat "("
6931                               (mail-header-from gnus-current-headers) ")")))
6932                     (ps-right-header
6933                      (list
6934                       "/pagenumberstring load"
6935                       (concat "("
6936                               (mail-header-date gnus-current-headers) ")"))))
6937                 (gnus-run-hooks 'gnus-ps-print-hook)
6938                 (save-excursion
6939                   (ps-print-buffer-with-faces filename))))
6940           (kill-buffer buffer))))))
6941
6942 (defun gnus-summary-show-article (&optional arg)
6943   "Force re-fetching of the current article.
6944 If ARG (the prefix) is non-nil, show the raw article without any
6945 article massaging functions being run."
6946   (interactive "P")
6947   (if (not arg)
6948       ;; Select the article the normal way.
6949       (gnus-summary-select-article nil 'force)
6950     ;; We have to require this here to make sure that the following
6951     ;; dynamic binding isn't shadowed by autoloading.
6952     (require 'gnus-async)
6953     (require 'gnus-art)
6954     ;; Bind the article treatment functions to nil.
6955     (let ((gnus-have-all-headers t)
6956           gnus-article-display-hook
6957           gnus-article-prepare-hook
6958           gnus-article-decode-hook
6959           gnus-break-pages
6960           gnus-show-mime
6961           gnus-visual)
6962       (gnus-summary-select-article nil 'force)))
6963   (gnus-summary-goto-subject gnus-current-article)
6964   (gnus-summary-position-point))
6965
6966 (defun gnus-summary-verbose-headers (&optional arg)
6967   "Toggle permanent full header display.
6968 If ARG is a positive number, turn header display on.
6969 If ARG is a negative number, turn header display off."
6970   (interactive "P")
6971   (setq gnus-show-all-headers
6972         (cond ((or (not (numberp arg))
6973                    (zerop arg))
6974                (not gnus-show-all-headers))
6975               ((natnump arg)
6976                t)))
6977   (gnus-summary-show-article))
6978
6979 (defun gnus-summary-toggle-header (&optional arg)
6980   "Show the headers if they are hidden, or hide them if they are shown.
6981 If ARG is a positive number, show the entire header.
6982 If ARG is a negative number, hide the unwanted header lines."
6983   (interactive "P")
6984   (save-excursion
6985     (set-buffer gnus-article-buffer)
6986     (let* ((buffer-read-only nil)
6987            (inhibit-point-motion-hooks t)
6988            (hidden (text-property-any
6989                     (goto-char (point-min)) (search-forward "\n\n")
6990                     'invisible t))
6991            e)
6992       (goto-char (point-min))
6993       (when (search-forward "\n\n" nil t)
6994         (delete-region (point-min) (1- (point))))
6995       (goto-char (point-min))
6996       (save-excursion
6997         (set-buffer gnus-original-article-buffer)
6998         (goto-char (point-min))
6999         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7000       (insert-buffer-substring gnus-original-article-buffer 1 e)
7001       (let ((article-inhibit-hiding t))
7002         (gnus-run-hooks 'gnus-article-display-hook))
7003       (when (or (not hidden) (and (numberp arg) (< arg 0)))
7004         (gnus-article-hide-headers)))))
7005
7006 (defun gnus-summary-show-all-headers ()
7007   "Make all header lines visible."
7008   (interactive)
7009   (gnus-article-show-all-headers))
7010
7011 (defun gnus-summary-toggle-mime (&optional arg)
7012   "Toggle MIME processing.
7013 If ARG is a positive number, turn MIME processing on."
7014   (interactive "P")
7015   (setq gnus-show-mime
7016         (if (null arg) (not gnus-show-mime)
7017           (> (prefix-numeric-value arg) 0)))
7018   (gnus-summary-select-article t 'force))
7019
7020 (defun gnus-summary-caesar-message (&optional arg)
7021   "Caesar rotate the current article by 13.
7022 The numerical prefix specifies how many places to rotate each letter
7023 forward."
7024   (interactive "P")
7025   (gnus-summary-select-article)
7026   (let ((mail-header-separator ""))
7027     (gnus-eval-in-buffer-window gnus-article-buffer
7028       (save-restriction
7029         (widen)
7030         (let ((start (window-start))
7031               buffer-read-only)
7032           (message-caesar-buffer-body arg)
7033           (set-window-start (get-buffer-window (current-buffer)) start))))))
7034
7035 (defun gnus-summary-stop-page-breaking ()
7036   "Stop page breaking in the current article."
7037   (interactive)
7038   (gnus-summary-select-article)
7039   (gnus-eval-in-buffer-window gnus-article-buffer
7040     (widen)
7041     (when (gnus-visual-p 'page-marker)
7042       (let ((buffer-read-only nil))
7043         (gnus-remove-text-with-property 'gnus-prev)
7044         (gnus-remove-text-with-property 'gnus-next))
7045       (setq gnus-page-broken nil))))
7046
7047 (defun gnus-summary-move-article (&optional n to-newsgroup
7048                                             select-method action)
7049   "Move the current article to a different newsgroup.
7050 If N is a positive number, move the N next articles.
7051 If N is a negative number, move the N previous articles.
7052 If N is nil and any articles have been marked with the process mark,
7053 move those articles instead.
7054 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7055 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7056 re-spool using this method.
7057
7058 For this function to work, both the current newsgroup and the
7059 newsgroup that you want to move to have to support the `request-move'
7060 and `request-accept' functions."
7061   (interactive "P")
7062   (unless action
7063     (setq action 'move))
7064   ;; Disable marking as read.
7065   (let (gnus-mark-article-hook)
7066     (save-window-excursion
7067       (gnus-summary-select-article)))
7068   ;; Check whether the source group supports the required functions.
7069   (cond ((and (eq action 'move)
7070               (not (gnus-check-backend-function
7071                     'request-move-article gnus-newsgroup-name)))
7072          (error "The current group does not support article moving"))
7073         ((and (eq action 'crosspost)
7074               (not (gnus-check-backend-function
7075                     'request-replace-article gnus-newsgroup-name)))
7076          (error "The current group does not support article editing")))
7077   (let ((articles (gnus-summary-work-articles n))
7078         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7079         (names '((move "Move" "Moving")
7080                  (copy "Copy" "Copying")
7081                  (crosspost "Crosspost" "Crossposting")))
7082         (copy-buf (save-excursion
7083                     (nnheader-set-temp-buffer " *copy article*")))
7084         art-group to-method new-xref article to-groups)
7085     (unless (assq action names)
7086       (error "Unknown action %s" action))
7087     ;; Read the newsgroup name.
7088     (when (and (not to-newsgroup)
7089                (not select-method))
7090       (setq to-newsgroup
7091             (gnus-read-move-group-name
7092              (cadr (assq action names))
7093              (symbol-value (intern (format "gnus-current-%s-group" action)))
7094              articles prefix))
7095       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7096     (setq to-method (or select-method
7097                         (gnus-group-name-to-method to-newsgroup)))
7098     ;; Check the method we are to move this article to...
7099     (unless (gnus-check-backend-function
7100              'request-accept-article (car to-method))
7101       (error "%s does not support article copying" (car to-method)))
7102     (unless (gnus-check-server to-method)
7103       (error "Can't open server %s" (car to-method)))
7104     (gnus-message 6 "%s to %s: %s..."
7105                   (caddr (assq action names))
7106                   (or (car select-method) to-newsgroup) articles)
7107     (while articles
7108       (setq article (pop articles))
7109       (setq
7110        art-group
7111        (cond
7112         ;; Move the article.
7113         ((eq action 'move)
7114          ;; Remove this article from future suppression.
7115          (gnus-dup-unsuppress-article article)
7116          (gnus-request-move-article
7117           article                       ; Article to move
7118           gnus-newsgroup-name           ; From newsgroup
7119           (nth 1 (gnus-find-method-for-group
7120                   gnus-newsgroup-name)) ; Server
7121           (list 'gnus-request-accept-article
7122                 to-newsgroup (list 'quote select-method)
7123                 (not articles) t)               ; Accept form
7124           (not articles)))              ; Only save nov last time
7125         ;; Copy the article.
7126         ((eq action 'copy)
7127          (save-excursion
7128            (set-buffer copy-buf)
7129            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7130              (gnus-request-accept-article
7131               to-newsgroup select-method (not articles) t))))
7132         ;; Crosspost the article.
7133         ((eq action 'crosspost)
7134          (let ((xref (message-tokenize-header
7135                       (mail-header-xref (gnus-summary-article-header article))
7136                       " ")))
7137            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7138                                   ":" article))
7139            (unless xref
7140              (setq xref (list (system-name))))
7141            (setq new-xref
7142                  (concat
7143                   (mapconcat 'identity
7144                              (delete "Xref:" (delete new-xref xref))
7145                              " ")
7146                   " " new-xref))
7147            (save-excursion
7148              (set-buffer copy-buf)
7149              ;; First put the article in the destination group.
7150              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7151              (when (consp (setq art-group
7152                                 (gnus-request-accept-article
7153                                  to-newsgroup select-method (not articles))))
7154                (setq new-xref (concat new-xref " " (car art-group)
7155                                       ":" (cdr art-group)))
7156                ;; Now we have the new Xrefs header, so we insert
7157                ;; it and replace the new article.
7158                (nnheader-replace-header "Xref" new-xref)
7159                (gnus-request-replace-article
7160                 (cdr art-group) to-newsgroup (current-buffer))
7161                art-group))))))
7162       (cond
7163        ((not art-group)
7164         (gnus-message 1 "Couldn't %s article %s"
7165                       (cadr (assq action names)) article))
7166        ((and (eq art-group 'junk)
7167              (eq action 'move))
7168         (gnus-summary-mark-article article gnus-canceled-mark)
7169         (gnus-message 4 "Deleted article %s" article))
7170        (t
7171         (let* ((pto-group (gnus-group-prefixed-name
7172                            (car art-group) to-method))
7173                (entry
7174                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7175                (info (nth 2 entry))
7176                (to-group (gnus-info-group info)))
7177           ;; Update the group that has been moved to.
7178           (when (and info
7179                      (memq action '(move copy)))
7180             (unless (member to-group to-groups)
7181               (push to-group to-groups))
7182
7183             (unless (memq article gnus-newsgroup-unreads)
7184               (gnus-info-set-read
7185                info (gnus-add-to-range (gnus-info-read info)
7186                                        (list (cdr art-group)))))
7187
7188             ;; Copy any marks over to the new group.
7189             (let ((marks gnus-article-mark-lists)
7190                   (to-article (cdr art-group)))
7191
7192               ;; See whether the article is to be put in the cache.
7193               (when gnus-use-cache
7194                 (gnus-cache-possibly-enter-article
7195                  to-group to-article
7196                  (let ((header (copy-sequence
7197                                 (gnus-summary-article-header article))))
7198                    (mail-header-set-number header to-article)
7199                    header)
7200                  (memq article gnus-newsgroup-marked)
7201                  (memq article gnus-newsgroup-dormant)
7202                  (memq article gnus-newsgroup-unreads)))
7203
7204               (when (and (equal to-group gnus-newsgroup-name)
7205                          (not (memq article gnus-newsgroup-unreads)))
7206                 ;; Mark this article as read in this group.
7207                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7208                 (setcdr (gnus-active to-group) to-article)
7209                 (setcdr gnus-newsgroup-active to-article))
7210
7211               (while marks
7212                 (when (memq article (symbol-value
7213                                      (intern (format "gnus-newsgroup-%s"
7214                                                      (caar marks)))))
7215                   ;; If the other group is the same as this group,
7216                   ;; then we have to add the mark to the list.
7217                   (when (equal to-group gnus-newsgroup-name)
7218                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7219                          (cons to-article
7220                                (symbol-value
7221                                 (intern (format "gnus-newsgroup-%s"
7222                                                 (caar marks)))))))
7223                   ;; Copy the marks to other group.
7224                   (gnus-add-marked-articles
7225                    to-group (cdar marks) (list to-article) info))
7226                 (setq marks (cdr marks)))
7227
7228               (gnus-dribble-enter
7229                (concat "(gnus-group-set-info '"
7230                        (gnus-prin1-to-string (gnus-get-info to-group))
7231                        ")"))))
7232
7233           ;; Update the Xref header in this article to point to
7234           ;; the new crossposted article we have just created.
7235           (when (eq action 'crosspost)
7236             (save-excursion
7237               (set-buffer copy-buf)
7238               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7239               (nnheader-replace-header "Xref" new-xref)
7240               (gnus-request-replace-article
7241                article gnus-newsgroup-name (current-buffer)))))
7242
7243         ;;;!!!Why is this necessary?
7244         (set-buffer gnus-summary-buffer)
7245
7246         (gnus-summary-goto-subject article)
7247         (when (eq action 'move)
7248           (gnus-summary-mark-article article gnus-canceled-mark))))
7249       (gnus-summary-remove-process-mark article))
7250     ;; Re-activate all groups that have been moved to.
7251     (while to-groups
7252       (save-excursion
7253         (set-buffer gnus-group-buffer)
7254         (when (gnus-group-goto-group (car to-groups) t)
7255           (gnus-group-get-new-news-this-group 1 t))
7256         (pop to-groups)))
7257
7258     (gnus-kill-buffer copy-buf)
7259     (gnus-summary-position-point)
7260     (gnus-set-mode-line 'summary)))
7261
7262 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7263   "Move the current article to a different newsgroup.
7264 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7265 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7266 re-spool using this method."
7267   (interactive "P")
7268   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7269
7270 (defun gnus-summary-crosspost-article (&optional n)
7271   "Crosspost the current article to some other group."
7272   (interactive "P")
7273   (gnus-summary-move-article n nil nil 'crosspost))
7274
7275 (defcustom gnus-summary-respool-default-method nil
7276   "Default method for respooling an article.
7277 If nil, use to the current newsgroup method."
7278   :type '(choice (gnus-select-method :value (nnml ""))
7279                  (const nil))
7280   :group 'gnus-summary-mail)
7281
7282 (defun gnus-summary-respool-article (&optional n method)
7283   "Respool the current article.
7284 The article will be squeezed through the mail spooling process again,
7285 which means that it will be put in some mail newsgroup or other
7286 depending on `nnmail-split-methods'.
7287 If N is a positive number, respool the N next articles.
7288 If N is a negative number, respool the N previous articles.
7289 If N is nil and any articles have been marked with the process mark,
7290 respool those articles instead.
7291
7292 Respooling can be done both from mail groups and \"real\" newsgroups.
7293 In the former case, the articles in question will be moved from the
7294 current group into whatever groups they are destined to.  In the
7295 latter case, they will be copied into the relevant groups."
7296   (interactive
7297    (list current-prefix-arg
7298          (let* ((methods (gnus-methods-using 'respool))
7299                 (methname
7300                  (symbol-name (or gnus-summary-respool-default-method
7301                                   (car (gnus-find-method-for-group
7302                                         gnus-newsgroup-name)))))
7303                 (method
7304                  (gnus-completing-read
7305                   methname "What backend do you want to use when respooling?"
7306                   methods nil t nil 'gnus-mail-method-history))
7307                 ms)
7308            (cond
7309             ((zerop (length (setq ms (gnus-servers-using-backend
7310                                       (intern method)))))
7311              (list (intern method) ""))
7312             ((= 1 (length ms))
7313              (car ms))
7314             (t
7315              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7316                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7317                            ms-alist))))))))
7318   (unless method
7319     (error "No method given for respooling"))
7320   (if (assoc (symbol-name
7321               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7322              (gnus-methods-using 'respool))
7323       (gnus-summary-move-article n nil method)
7324     (gnus-summary-copy-article n nil method)))
7325
7326 (defun gnus-summary-import-article (file)
7327   "Import an arbitrary file into a mail newsgroup."
7328   (interactive "fImport file: ")
7329   (let ((group gnus-newsgroup-name)
7330         (now (current-time))
7331         atts lines)
7332     (unless (gnus-check-backend-function 'request-accept-article group)
7333       (error "%s does not support article importing" group))
7334     (or (file-readable-p file)
7335         (not (file-regular-p file))
7336         (error "Can't read %s" file))
7337     (save-excursion
7338       (set-buffer (gnus-get-buffer-create " *import file*"))
7339       (erase-buffer)
7340       (nnheader-insert-file-contents file)
7341       (goto-char (point-min))
7342       (unless (nnheader-article-p)
7343         ;; This doesn't look like an article, so we fudge some headers.
7344         (setq atts (file-attributes file)
7345               lines (count-lines (point-min) (point-max)))
7346         (insert "From: " (read-string "From: ") "\n"
7347                 "Subject: " (read-string "Subject: ") "\n"
7348                 "Date: " (message-make-date (nth 5 atts))
7349                 "\n"
7350                 "Message-ID: " (message-make-message-id) "\n"
7351                 "Lines: " (int-to-string lines) "\n"
7352                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7353       (gnus-request-accept-article group nil t)
7354       (kill-buffer (current-buffer)))))
7355
7356 (defun gnus-summary-article-posted-p ()
7357   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7358 This will be the case if the article has both been mailed and posted."
7359   (interactive)
7360   (let ((id (mail-header-references (gnus-summary-article-header)))
7361         (gnus-override-method
7362          (or gnus-refer-article-method gnus-select-method)))
7363     (if (gnus-request-head id "")
7364         (gnus-message 2 "The current message was found on %s"
7365                       gnus-override-method)
7366       (gnus-message 2 "The current message couldn't be found on %s"
7367                     gnus-override-method)
7368       nil)))
7369
7370 (defun gnus-summary-expire-articles (&optional now)
7371   "Expire all articles that are marked as expirable in the current group."
7372   (interactive)
7373   (when (gnus-check-backend-function
7374          'request-expire-articles gnus-newsgroup-name)
7375     ;; This backend supports expiry.
7376     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7377            (expirable (if total
7378                           (progn
7379                             ;; We need to update the info for
7380                             ;; this group for `gnus-list-of-read-articles'
7381                             ;; to give us the right answer.
7382                             (gnus-run-hooks 'gnus-exit-group-hook)
7383                             (gnus-summary-update-info)
7384                             (gnus-list-of-read-articles gnus-newsgroup-name))
7385                         (setq gnus-newsgroup-expirable
7386                               (sort gnus-newsgroup-expirable '<))))
7387            (expiry-wait (if now 'immediate
7388                           (gnus-group-find-parameter
7389                            gnus-newsgroup-name 'expiry-wait)))
7390            es)
7391       (when expirable
7392         ;; There are expirable articles in this group, so we run them
7393         ;; through the expiry process.
7394         (gnus-message 6 "Expiring articles...")
7395         ;; The list of articles that weren't expired is returned.
7396         (save-excursion
7397           (if expiry-wait
7398               (let ((nnmail-expiry-wait-function nil)
7399                     (nnmail-expiry-wait expiry-wait))
7400                 (setq es (gnus-request-expire-articles
7401                           expirable gnus-newsgroup-name)))
7402             (setq es (gnus-request-expire-articles
7403                       expirable gnus-newsgroup-name))))
7404         (unless total
7405           (setq gnus-newsgroup-expirable es))
7406         ;; We go through the old list of expirable, and mark all
7407         ;; really expired articles as nonexistent.
7408         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7409           (let ((gnus-use-cache nil))
7410             (while expirable
7411               (unless (memq (car expirable) es)
7412                 (when (gnus-data-find (car expirable))
7413                   (gnus-summary-mark-article
7414                    (car expirable) gnus-canceled-mark)))
7415               (setq expirable (cdr expirable)))))
7416         (gnus-message 6 "Expiring articles...done")))))
7417
7418 (defun gnus-summary-expire-articles-now ()
7419   "Expunge all expirable articles in the current group.
7420 This means that *all* articles that are marked as expirable will be
7421 deleted forever, right now."
7422   (interactive)
7423   (or gnus-expert-user
7424       (gnus-yes-or-no-p
7425        "Are you really, really, really sure you want to delete all these messages? ")
7426       (error "Phew!"))
7427   (gnus-summary-expire-articles t))
7428
7429 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7430 (defun gnus-summary-delete-article (&optional n)
7431   "Delete the N next (mail) articles.
7432 This command actually deletes articles.  This is not a marking
7433 command.  The article will disappear forever from your life, never to
7434 return.
7435 If N is negative, delete backwards.
7436 If N is nil and articles have been marked with the process mark,
7437 delete these instead."
7438   (interactive "P")
7439   (unless (gnus-check-backend-function 'request-expire-articles
7440                                        gnus-newsgroup-name)
7441     (error "The current newsgroup does not support article deletion"))
7442   ;; Compute the list of articles to delete.
7443   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7444         not-deleted)
7445     (if (and gnus-novice-user
7446              (not (gnus-yes-or-no-p
7447                    (format "Do you really want to delete %s forever? "
7448                            (if (> (length articles) 1)
7449                                (format "these %s articles" (length articles))
7450                              "this article")))))
7451         ()
7452       ;; Delete the articles.
7453       (setq not-deleted (gnus-request-expire-articles
7454                          articles gnus-newsgroup-name 'force))
7455       (while articles
7456         (gnus-summary-remove-process-mark (car articles))
7457         ;; The backend might not have been able to delete the article
7458         ;; after all.
7459         (unless (memq (car articles) not-deleted)
7460           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7461         (setq articles (cdr articles)))
7462       (when not-deleted
7463         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7464     (gnus-summary-position-point)
7465     (gnus-set-mode-line 'summary)
7466     not-deleted))
7467
7468 (defun gnus-summary-edit-article (&optional force)
7469   "Edit the current article.
7470 This will have permanent effect only in mail groups.
7471 If FORCE is non-nil, allow editing of articles even in read-only
7472 groups."
7473   (interactive "P")
7474   (save-excursion
7475     (set-buffer gnus-summary-buffer)
7476     (gnus-set-global-variables)
7477     (when (and (not force)
7478                (gnus-group-read-only-p))
7479       (error "The current newsgroup does not support article editing"))
7480     ;; Select article if needed.
7481     (unless (eq (gnus-summary-article-number)
7482                 gnus-current-article)
7483       (gnus-summary-select-article t))
7484     (gnus-article-date-original)
7485     (gnus-article-edit-article
7486      `(lambda (no-highlight)
7487         (gnus-summary-edit-article-done
7488          ,(or (mail-header-references gnus-current-headers) "")
7489          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7490
7491 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7492
7493 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7494                                                  no-highlight)
7495   "Make edits to the current article permanent."
7496   (interactive)
7497   ;; Replace the article.
7498   (let ((buf (current-buffer)))
7499     (with-temp-buffer
7500       (insert-buffer buf)
7501       (if (and (not read-only)
7502                (not (gnus-request-replace-article
7503                      (cdr gnus-article-current) (car gnus-article-current)
7504                      (current-buffer))))
7505           (error "Couldn't replace article")
7506         ;; Update the summary buffer.
7507         (if (and references
7508                  (equal (message-tokenize-header references " ")
7509                         (message-tokenize-header
7510                          (or (message-fetch-field "references") "") " ")))
7511             ;; We only have to update this line.
7512             (save-excursion
7513               (save-restriction
7514                 (message-narrow-to-head)
7515                 (let ((head (buffer-string))
7516                       header)
7517                   (with-temp-buffer
7518                     (insert (format "211 %d Article retrieved.\n"
7519                                     (cdr gnus-article-current)))
7520                     (insert head)
7521                     (insert ".\n")
7522                     (let ((nntp-server-buffer (current-buffer)))
7523                       (setq header (car (gnus-get-newsgroup-headers
7524                                          (save-excursion
7525                                            (set-buffer gnus-summary-buffer)
7526                                            gnus-newsgroup-dependencies)
7527                                          t))))
7528                     (save-excursion
7529                       (set-buffer gnus-summary-buffer)
7530                       (gnus-data-set-header
7531                        (gnus-data-find (cdr gnus-article-current))
7532                        header)
7533                       (gnus-summary-update-article-line
7534                        (cdr gnus-article-current) header))))))
7535           ;; Update threads.
7536           (set-buffer (or buffer gnus-summary-buffer))
7537           (gnus-summary-update-article (cdr gnus-article-current)))
7538         ;; Prettify the article buffer again.
7539         (unless no-highlight
7540           (save-excursion
7541             (set-buffer gnus-article-buffer)
7542             (gnus-run-hooks 'gnus-article-display-hook)
7543             (set-buffer gnus-original-article-buffer)
7544             (gnus-request-article
7545              (cdr gnus-article-current)
7546              (car gnus-article-current) (current-buffer))))
7547         ;; Prettify the summary buffer line.
7548         (when (gnus-visual-p 'summary-highlight 'highlight)
7549           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7550
7551 (defun gnus-summary-edit-wash (key)
7552   "Perform editing command KEY in the article buffer."
7553   (interactive
7554    (list
7555     (progn
7556       (message "%s" (concat (this-command-keys) "- "))
7557       (read-char))))
7558   (message "")
7559   (gnus-summary-edit-article)
7560   (execute-kbd-macro (concat (this-command-keys) key))
7561   (gnus-article-edit-done))
7562
7563 ;;; Respooling
7564
7565 (defun gnus-summary-respool-query (&optional silent trace)
7566   "Query where the respool algorithm would put this article."
7567   (interactive)
7568   (let (gnus-mark-article-hook)
7569     (gnus-summary-select-article)
7570     (save-excursion
7571       (set-buffer gnus-original-article-buffer)
7572       (save-restriction
7573         (message-narrow-to-head)
7574         (let ((groups (nnmail-article-group 'identity trace)))
7575           (unless silent
7576             (if groups
7577                 (message "This message would go to %s"
7578                          (mapconcat 'car groups ", "))
7579               (message "This message would go to no groups"))
7580             groups))))))
7581
7582 (defun gnus-summary-respool-trace ()
7583   "Trace where the respool algorithm would put this article.
7584 Display a buffer showing all fancy splitting patterns which matched."
7585   (interactive)
7586   (gnus-summary-respool-query nil t))
7587
7588 ;; Summary marking commands.
7589
7590 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7591   "Mark articles which has the same subject as read, and then select the next.
7592 If UNMARK is positive, remove any kind of mark.
7593 If UNMARK is negative, tick articles."
7594   (interactive "P")
7595   (when unmark
7596     (setq unmark (prefix-numeric-value unmark)))
7597   (let ((count
7598          (gnus-summary-mark-same-subject
7599           (gnus-summary-article-subject) unmark)))
7600     ;; Select next unread article.  If auto-select-same mode, should
7601     ;; select the first unread article.
7602     (gnus-summary-next-article t (and gnus-auto-select-same
7603                                       (gnus-summary-article-subject)))
7604     (gnus-message 7 "%d article%s marked as %s"
7605                   count (if (= count 1) " is" "s are")
7606                   (if unmark "unread" "read"))))
7607
7608 (defun gnus-summary-kill-same-subject (&optional unmark)
7609   "Mark articles which has the same subject as read.
7610 If UNMARK is positive, remove any kind of mark.
7611 If UNMARK is negative, tick articles."
7612   (interactive "P")
7613   (when unmark
7614     (setq unmark (prefix-numeric-value unmark)))
7615   (let ((count
7616          (gnus-summary-mark-same-subject
7617           (gnus-summary-article-subject) unmark)))
7618     ;; If marked as read, go to next unread subject.
7619     (when (null unmark)
7620       ;; Go to next unread subject.
7621       (gnus-summary-next-subject 1 t))
7622     (gnus-message 7 "%d articles are marked as %s"
7623                   count (if unmark "unread" "read"))))
7624
7625 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7626   "Mark articles with same SUBJECT as read, and return marked number.
7627 If optional argument UNMARK is positive, remove any kinds of marks.
7628 If optional argument UNMARK is negative, mark articles as unread instead."
7629   (let ((count 1))
7630     (save-excursion
7631       (cond
7632        ((null unmark)                   ; Mark as read.
7633         (while (and
7634                 (progn
7635                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7636                   (gnus-summary-show-thread) t)
7637                 (gnus-summary-find-subject subject))
7638           (setq count (1+ count))))
7639        ((> unmark 0)                    ; Tick.
7640         (while (and
7641                 (progn
7642                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7643                   (gnus-summary-show-thread) t)
7644                 (gnus-summary-find-subject subject))
7645           (setq count (1+ count))))
7646        (t                               ; Mark as unread.
7647         (while (and
7648                 (progn
7649                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7650                   (gnus-summary-show-thread) t)
7651                 (gnus-summary-find-subject subject))
7652           (setq count (1+ count)))))
7653       (gnus-set-mode-line 'summary)
7654       ;; Return the number of marked articles.
7655       count)))
7656
7657 (defun gnus-summary-mark-as-processable (n &optional unmark)
7658   "Set the process mark on the next N articles.
7659 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7660 the process mark instead.  The difference between N and the actual
7661 number of articles marked is returned."
7662   (interactive "p")
7663   (let ((backward (< n 0))
7664         (n (abs n)))
7665     (while (and
7666             (> n 0)
7667             (if unmark
7668                 (gnus-summary-remove-process-mark
7669                  (gnus-summary-article-number))
7670               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7671             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7672       (setq n (1- n)))
7673     (when (/= 0 n)
7674       (gnus-message 7 "No more articles"))
7675     (gnus-summary-recenter)
7676     (gnus-summary-position-point)
7677     n))
7678
7679 (defun gnus-summary-unmark-as-processable (n)
7680   "Remove the process mark from the next N articles.
7681 If N is negative, unmark backward instead.  The difference between N and
7682 the actual number of articles unmarked is returned."
7683   (interactive "p")
7684   (gnus-summary-mark-as-processable n t))
7685
7686 (defun gnus-summary-unmark-all-processable ()
7687   "Remove the process mark from all articles."
7688   (interactive)
7689   (save-excursion
7690     (while gnus-newsgroup-processable
7691       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7692   (gnus-summary-position-point))
7693
7694 (defun gnus-summary-mark-as-expirable (n)
7695   "Mark N articles forward as expirable.
7696 If N is negative, mark backward instead.  The difference between N and
7697 the actual number of articles marked is returned."
7698   (interactive "p")
7699   (gnus-summary-mark-forward n gnus-expirable-mark))
7700
7701 (defun gnus-summary-mark-article-as-replied (article)
7702   "Mark ARTICLE replied and update the summary line."
7703   (push article gnus-newsgroup-replied)
7704   (let ((buffer-read-only nil))
7705     (when (gnus-summary-goto-subject article nil t)
7706       (gnus-summary-update-secondary-mark article))))
7707
7708 (defun gnus-summary-set-bookmark (article)
7709   "Set a bookmark in current article."
7710   (interactive (list (gnus-summary-article-number)))
7711   (when (or (not (get-buffer gnus-article-buffer))
7712             (not gnus-current-article)
7713             (not gnus-article-current)
7714             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7715     (error "No current article selected"))
7716   ;; Remove old bookmark, if one exists.
7717   (let ((old (assq article gnus-newsgroup-bookmarks)))
7718     (when old
7719       (setq gnus-newsgroup-bookmarks
7720             (delq old gnus-newsgroup-bookmarks))))
7721   ;; Set the new bookmark, which is on the form
7722   ;; (article-number . line-number-in-body).
7723   (push
7724    (cons article
7725          (save-excursion
7726            (set-buffer gnus-article-buffer)
7727            (count-lines
7728             (min (point)
7729                  (save-excursion
7730                    (goto-char (point-min))
7731                    (search-forward "\n\n" nil t)
7732                    (point)))
7733             (point))))
7734    gnus-newsgroup-bookmarks)
7735   (gnus-message 6 "A bookmark has been added to the current article."))
7736
7737 (defun gnus-summary-remove-bookmark (article)
7738   "Remove the bookmark from the current article."
7739   (interactive (list (gnus-summary-article-number)))
7740   ;; Remove old bookmark, if one exists.
7741   (let ((old (assq article gnus-newsgroup-bookmarks)))
7742     (if old
7743         (progn
7744           (setq gnus-newsgroup-bookmarks
7745                 (delq old gnus-newsgroup-bookmarks))
7746           (gnus-message 6 "Removed bookmark."))
7747       (gnus-message 6 "No bookmark in current article."))))
7748
7749 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7750 (defun gnus-summary-mark-as-dormant (n)
7751   "Mark N articles forward as dormant.
7752 If N is negative, mark backward instead.  The difference between N and
7753 the actual number of articles marked is returned."
7754   (interactive "p")
7755   (gnus-summary-mark-forward n gnus-dormant-mark))
7756
7757 (defun gnus-summary-set-process-mark (article)
7758   "Set the process mark on ARTICLE and update the summary line."
7759   (setq gnus-newsgroup-processable
7760         (cons article
7761               (delq article gnus-newsgroup-processable)))
7762   (when (gnus-summary-goto-subject article)
7763     (gnus-summary-show-thread)
7764     (gnus-summary-goto-subject article)
7765     (gnus-summary-update-secondary-mark article)))
7766
7767 (defun gnus-summary-remove-process-mark (article)
7768   "Remove the process mark from ARTICLE and update the summary line."
7769   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7770   (when (gnus-summary-goto-subject article)
7771     (gnus-summary-show-thread)
7772     (gnus-summary-goto-subject article)
7773     (gnus-summary-update-secondary-mark article)))
7774
7775 (defun gnus-summary-set-saved-mark (article)
7776   "Set the process mark on ARTICLE and update the summary line."
7777   (push article gnus-newsgroup-saved)
7778   (when (gnus-summary-goto-subject article)
7779     (gnus-summary-update-secondary-mark article)))
7780
7781 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7782   "Mark N articles as read forwards.
7783 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7784 The difference between N and the actual number of articles marked is
7785 returned."
7786   (interactive "p")
7787   (gnus-summary-show-thread)
7788   (let ((backward (< n 0))
7789         (gnus-summary-goto-unread
7790          (and gnus-summary-goto-unread
7791               (not (eq gnus-summary-goto-unread 'never))
7792               (not (memq mark (list gnus-unread-mark
7793                                     gnus-ticked-mark gnus-dormant-mark)))))
7794         (n (abs n))
7795         (mark (or mark gnus-del-mark)))
7796     (while (and (> n 0)
7797                 (gnus-summary-mark-article nil mark no-expire)
7798                 (zerop (gnus-summary-next-subject
7799                         (if backward -1 1)
7800                         (and gnus-summary-goto-unread
7801                              (not (eq gnus-summary-goto-unread 'never)))
7802                         t)))
7803       (setq n (1- n)))
7804     (when (/= 0 n)
7805       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7806     (gnus-summary-recenter)
7807     (gnus-summary-position-point)
7808     (gnus-set-mode-line 'summary)
7809     n))
7810
7811 (defun gnus-summary-mark-article-as-read (mark)
7812   "Mark the current article quickly as read with MARK."
7813   (let ((article (gnus-summary-article-number)))
7814     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7815     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7816     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7817     (push (cons article mark) gnus-newsgroup-reads)
7818     ;; Possibly remove from cache, if that is used.
7819     (when gnus-use-cache
7820       (gnus-cache-enter-remove-article article))
7821     ;; Allow the backend to change the mark.
7822     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7823     ;; Check for auto-expiry.
7824     (when (and gnus-newsgroup-auto-expire
7825                (memq mark gnus-auto-expirable-marks))
7826       (setq mark gnus-expirable-mark)
7827       ;; Let the backend know about the mark change.
7828       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7829       (push article gnus-newsgroup-expirable))
7830     ;; Set the mark in the buffer.
7831     (gnus-summary-update-mark mark 'unread)
7832     t))
7833
7834 (defun gnus-summary-mark-article-as-unread (mark)
7835   "Mark the current article quickly as unread with MARK."
7836   (let* ((article (gnus-summary-article-number))
7837          (old-mark (gnus-summary-article-mark article)))
7838     ;; Allow the backend to change the mark.
7839     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7840     (if (eq mark old-mark)
7841         t
7842       (if (<= article 0)
7843           (progn
7844             (gnus-error 1 "Can't mark negative article numbers")
7845             nil)
7846         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7847         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7848         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7849         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7850         (cond ((= mark gnus-ticked-mark)
7851                (push article gnus-newsgroup-marked))
7852               ((= mark gnus-dormant-mark)
7853                (push article gnus-newsgroup-dormant))
7854               (t
7855                (push article gnus-newsgroup-unreads)))
7856         (gnus-pull article gnus-newsgroup-reads)
7857
7858         ;; See whether the article is to be put in the cache.
7859         (and gnus-use-cache
7860              (vectorp (gnus-summary-article-header article))
7861              (save-excursion
7862                (gnus-cache-possibly-enter-article
7863                 gnus-newsgroup-name article
7864                 (gnus-summary-article-header article)
7865                 (= mark gnus-ticked-mark)
7866                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7867
7868         ;; Fix the mark.
7869         (gnus-summary-update-mark mark 'unread)
7870         t))))
7871
7872 (defun gnus-summary-mark-article (&optional article mark no-expire)
7873   "Mark ARTICLE with MARK.  MARK can be any character.
7874 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7875 `??' (dormant) and `?E' (expirable).
7876 If MARK is nil, then the default character `?r' is used.
7877 If ARTICLE is nil, then the article on the current line will be
7878 marked."
7879   ;; The mark might be a string.
7880   (when (stringp mark)
7881     (setq mark (aref mark 0)))
7882   ;; If no mark is given, then we check auto-expiring.
7883   (when (null mark)
7884     (setq mark gnus-del-mark))
7885   (when (and (not no-expire)
7886              gnus-newsgroup-auto-expire
7887              (memq mark gnus-auto-expirable-marks))
7888     (setq mark gnus-expirable-mark))
7889   (let ((article (or article (gnus-summary-article-number)))
7890         (old-mark (gnus-summary-article-mark article)))
7891     ;; Allow the backend to change the mark.
7892     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7893     (if (eq mark old-mark)
7894         t
7895       (unless article
7896         (error "No article on current line"))
7897       (if (not (if (or (= mark gnus-unread-mark)
7898                        (= mark gnus-ticked-mark)
7899                        (= mark gnus-dormant-mark))
7900                    (gnus-mark-article-as-unread article mark)
7901                  (gnus-mark-article-as-read article mark)))
7902           t
7903         ;; See whether the article is to be put in the cache.
7904         (and gnus-use-cache
7905              (not (= mark gnus-canceled-mark))
7906              (vectorp (gnus-summary-article-header article))
7907              (save-excursion
7908                (gnus-cache-possibly-enter-article
7909                 gnus-newsgroup-name article
7910                 (gnus-summary-article-header article)
7911                 (= mark gnus-ticked-mark)
7912                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7913
7914         (when (gnus-summary-goto-subject article nil t)
7915           (let ((buffer-read-only nil))
7916             (gnus-summary-show-thread)
7917             ;; Fix the mark.
7918             (gnus-summary-update-mark mark 'unread)
7919             t))))))
7920
7921 (defun gnus-summary-update-secondary-mark (article)
7922   "Update the secondary (read, process, cache) mark."
7923   (gnus-summary-update-mark
7924    (cond ((memq article gnus-newsgroup-processable)
7925           gnus-process-mark)
7926          ((memq article gnus-newsgroup-cached)
7927           gnus-cached-mark)
7928          ((memq article gnus-newsgroup-replied)
7929           gnus-replied-mark)
7930          ((memq article gnus-newsgroup-saved)
7931           gnus-saved-mark)
7932          (t gnus-unread-mark))
7933    'replied)
7934   (when (gnus-visual-p 'summary-highlight 'highlight)
7935     (gnus-run-hooks 'gnus-summary-update-hook))
7936   t)
7937
7938 (defun gnus-summary-update-mark (mark type)
7939   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7940         (buffer-read-only nil))
7941     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7942     (when forward
7943       (when (looking-at "\r")
7944         (incf forward))
7945       (when (<= (+ forward (point)) (point-max))
7946         ;; Go to the right position on the line.
7947         (goto-char (+ forward (point)))
7948         ;; Replace the old mark with the new mark.
7949         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7950         ;; Optionally update the marks by some user rule.
7951         (when (eq type 'unread)
7952           (gnus-data-set-mark
7953            (gnus-data-find (gnus-summary-article-number)) mark)
7954           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7955
7956 (defun gnus-mark-article-as-read (article &optional mark)
7957   "Enter ARTICLE in the pertinent lists and remove it from others."
7958   ;; Make the article expirable.
7959   (let ((mark (or mark gnus-del-mark)))
7960     (if (= mark gnus-expirable-mark)
7961         (push article gnus-newsgroup-expirable)
7962       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7963     ;; Remove from unread and marked lists.
7964     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7965     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7966     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7967     (push (cons article mark) gnus-newsgroup-reads)
7968     ;; Possibly remove from cache, if that is used.
7969     (when gnus-use-cache
7970       (gnus-cache-enter-remove-article article))
7971     t))
7972
7973 (defun gnus-mark-article-as-unread (article &optional mark)
7974   "Enter ARTICLE in the pertinent lists and remove it from others."
7975   (let ((mark (or mark gnus-ticked-mark)))
7976     (if (<= article 0)
7977         (progn
7978           (gnus-error 1 "Can't mark negative article numbers")
7979           nil)
7980       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7981             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7982             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7983             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7984
7985       ;; Unsuppress duplicates?
7986       (when gnus-suppress-duplicates
7987         (gnus-dup-unsuppress-article article))
7988
7989       (cond ((= mark gnus-ticked-mark)
7990              (push article gnus-newsgroup-marked))
7991             ((= mark gnus-dormant-mark)
7992              (push article gnus-newsgroup-dormant))
7993             (t
7994              (push article gnus-newsgroup-unreads)))
7995       (gnus-pull article gnus-newsgroup-reads)
7996       t)))
7997
7998 (defalias 'gnus-summary-mark-as-unread-forward
7999   'gnus-summary-tick-article-forward)
8000 (make-obsolete 'gnus-summary-mark-as-unread-forward
8001                'gnus-summary-tick-article-forward)
8002 (defun gnus-summary-tick-article-forward (n)
8003   "Tick N articles forwards.
8004 If N is negative, tick backwards instead.
8005 The difference between N and the number of articles ticked is returned."
8006   (interactive "p")
8007   (gnus-summary-mark-forward n gnus-ticked-mark))
8008
8009 (defalias 'gnus-summary-mark-as-unread-backward
8010   'gnus-summary-tick-article-backward)
8011 (make-obsolete 'gnus-summary-mark-as-unread-backward
8012                'gnus-summary-tick-article-backward)
8013 (defun gnus-summary-tick-article-backward (n)
8014   "Tick N articles backwards.
8015 The difference between N and the number of articles ticked is returned."
8016   (interactive "p")
8017   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8018
8019 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8020 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8021 (defun gnus-summary-tick-article (&optional article clear-mark)
8022   "Mark current article as unread.
8023 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8024 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8025   (interactive)
8026   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8027                                        gnus-ticked-mark)))
8028
8029 (defun gnus-summary-mark-as-read-forward (n)
8030   "Mark N articles as read forwards.
8031 If N is negative, mark backwards instead.
8032 The difference between N and the actual number of articles marked is
8033 returned."
8034   (interactive "p")
8035   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8036
8037 (defun gnus-summary-mark-as-read-backward (n)
8038   "Mark the N articles as read backwards.
8039 The difference between N and the actual number of articles marked is
8040 returned."
8041   (interactive "p")
8042   (gnus-summary-mark-forward (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8043
8044 (defun gnus-summary-mark-as-read (&optional article mark)
8045   "Mark current article as read.
8046 ARTICLE specifies the article to be marked as read.
8047 MARK specifies a string to be inserted at the beginning of the line."
8048   (gnus-summary-mark-article article mark))
8049
8050 (defun gnus-summary-clear-mark-forward (n)
8051   "Clear marks from N articles forward.
8052 If N is negative, clear backward instead.
8053 The difference between N and the number of marks cleared is returned."
8054   (interactive "p")
8055   (gnus-summary-mark-forward n gnus-unread-mark))
8056
8057 (defun gnus-summary-clear-mark-backward (n)
8058   "Clear marks from N articles backward.
8059 The difference between N and the number of marks cleared is returned."
8060   (interactive "p")
8061   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8062
8063 (defun gnus-summary-mark-unread-as-read ()
8064   "Intended to be used by `gnus-summary-mark-article-hook'."
8065   (when (memq gnus-current-article gnus-newsgroup-unreads)
8066     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8067
8068 (defun gnus-summary-mark-read-and-unread-as-read ()
8069   "Intended to be used by `gnus-summary-mark-article-hook'."
8070   (let ((mark (gnus-summary-article-mark)))
8071     (when (or (gnus-unread-mark-p mark)
8072               (gnus-read-mark-p mark))
8073       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8074
8075 (defun gnus-summary-mark-region-as-read (point mark all)
8076   "Mark all unread articles between point and mark as read.
8077 If given a prefix, mark all articles between point and mark as read,
8078 even ticked and dormant ones."
8079   (interactive "r\nP")
8080   (save-excursion
8081     (let (article)
8082       (goto-char point)
8083       (beginning-of-line)
8084       (while (and
8085               (< (point) mark)
8086               (progn
8087                 (when (or all
8088                           (memq (setq article (gnus-summary-article-number))
8089                                 gnus-newsgroup-unreads))
8090                   (gnus-summary-mark-article article gnus-del-mark))
8091                 t)
8092               (gnus-summary-find-next))))))
8093
8094 (defun gnus-summary-mark-below (score mark)
8095   "Mark articles with score less than SCORE with MARK."
8096   (interactive "P\ncMark: ")
8097   (setq score (if score
8098                   (prefix-numeric-value score)
8099                 (or gnus-summary-default-score 0)))
8100   (save-excursion
8101     (set-buffer gnus-summary-buffer)
8102     (goto-char (point-min))
8103     (while
8104         (progn
8105           (and (< (gnus-summary-article-score) score)
8106                (gnus-summary-mark-article nil mark))
8107           (gnus-summary-find-next)))))
8108
8109 (defun gnus-summary-kill-below (&optional score)
8110   "Mark articles with score below SCORE as read."
8111   (interactive "P")
8112   (gnus-summary-mark-below score gnus-killed-mark))
8113
8114 (defun gnus-summary-clear-above (&optional score)
8115   "Clear all marks from articles with score above SCORE."
8116   (interactive "P")
8117   (gnus-summary-mark-above score gnus-unread-mark))
8118
8119 (defun gnus-summary-tick-above (&optional score)
8120   "Tick all articles with score above SCORE."
8121   (interactive "P")
8122   (gnus-summary-mark-above score gnus-ticked-mark))
8123
8124 (defun gnus-summary-mark-above (score mark)
8125   "Mark articles with score over SCORE with MARK."
8126   (interactive "P\ncMark: ")
8127   (setq score (if score
8128                   (prefix-numeric-value score)
8129                 (or gnus-summary-default-score 0)))
8130   (save-excursion
8131     (set-buffer gnus-summary-buffer)
8132     (goto-char (point-min))
8133     (while (and (progn
8134                   (when (> (gnus-summary-article-score) score)
8135                     (gnus-summary-mark-article nil mark))
8136                   t)
8137                 (gnus-summary-find-next)))))
8138
8139 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8140 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8141 (defun gnus-summary-limit-include-expunged (&optional no-error)
8142   "Display all the hidden articles that were expunged for low scores."
8143   (interactive)
8144   (let ((buffer-read-only nil))
8145     (let ((scored gnus-newsgroup-scored)
8146           headers h)
8147       (while scored
8148         (unless (gnus-summary-goto-subject (caar scored))
8149           (and (setq h (gnus-summary-article-header (caar scored)))
8150                (< (cdar scored) gnus-summary-expunge-below)
8151                (push h headers)))
8152         (setq scored (cdr scored)))
8153       (if (not headers)
8154           (when (not no-error)
8155             (error "No expunged articles hidden"))
8156         (goto-char (point-min))
8157         (gnus-summary-prepare-unthreaded (nreverse headers))
8158         (goto-char (point-min))
8159         (gnus-summary-position-point)
8160         t))))
8161
8162 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8163   "Mark all unread articles in this newsgroup as read.
8164 If prefix argument ALL is non-nil, ticked and dormant articles will
8165 also be marked as read.
8166 If QUIETLY is non-nil, no questions will be asked.
8167 If TO-HERE is non-nil, it should be a point in the buffer.  All
8168 articles before this point will be marked as read.
8169 Note that this function will only catch up the unread article
8170 in the current summary buffer limitation.
8171 The number of articles marked as read is returned."
8172   (interactive "P")
8173   (prog1
8174       (save-excursion
8175         (when (or quietly
8176                   (not gnus-interactive-catchup) ;Without confirmation?
8177                   gnus-expert-user
8178                   (gnus-y-or-n-p
8179                    (if all
8180                        "Mark absolutely all articles as read? "
8181                      "Mark all unread articles as read? ")))
8182           (if (and not-mark
8183                    (not gnus-newsgroup-adaptive)
8184                    (not gnus-newsgroup-auto-expire)
8185                    (not gnus-suppress-duplicates)
8186                    (or (not gnus-use-cache)
8187                        (eq gnus-use-cache 'passive)))
8188               (progn
8189                 (when all
8190                   (setq gnus-newsgroup-marked nil
8191                         gnus-newsgroup-dormant nil))
8192                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8193             ;; We actually mark all articles as canceled, which we
8194             ;; have to do when using auto-expiry or adaptive scoring.
8195             (gnus-summary-show-all-threads)
8196             (when (gnus-summary-first-subject (not all) t)
8197               (while (and
8198                       (if to-here (< (point) to-here) t)
8199                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8200                       (gnus-summary-find-next (not all) nil nil t))))
8201             (gnus-set-mode-line 'summary))
8202           t))
8203     (gnus-summary-position-point)))
8204
8205 (defun gnus-summary-catchup-to-here (&optional all)
8206   "Mark all unticked articles before the current one as read.
8207 If ALL is non-nil, also mark ticked and dormant articles as read."
8208   (interactive "P")
8209   (save-excursion
8210     (gnus-save-hidden-threads
8211       (let ((beg (point)))
8212         ;; We check that there are unread articles.
8213         (when (or all (gnus-summary-find-prev))
8214           (gnus-summary-catchup all t beg)))))
8215   (gnus-summary-position-point))
8216
8217 (defun gnus-summary-catchup-all (&optional quietly)
8218   "Mark all articles in this newsgroup as read."
8219   (interactive "P")
8220   (gnus-summary-catchup t quietly))
8221
8222 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8223   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8224 If prefix argument ALL is non-nil, all articles are marked as read."
8225   (interactive "P")
8226   (when (gnus-summary-catchup all quietly nil 'fast)
8227     ;; Select next newsgroup or exit.
8228     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8229              (eq gnus-auto-select-next 'quietly))
8230         (gnus-summary-next-group nil)
8231       (gnus-summary-exit))))
8232
8233 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8234   "Mark all articles in this newsgroup as read, and then exit."
8235   (interactive "P")
8236   (gnus-summary-catchup-and-exit t quietly))
8237
8238 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8239 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8240   "Mark all articles in this group as read and select the next group.
8241 If given a prefix, mark all articles, unread as well as ticked, as
8242 read."
8243   (interactive "P")
8244   (save-excursion
8245     (gnus-summary-catchup all))
8246   (gnus-summary-next-article t nil nil t))
8247
8248 ;; Thread-based commands.
8249
8250 (defun gnus-summary-articles-in-thread (&optional article)
8251   "Return a list of all articles in the current thread.
8252 If ARTICLE is non-nil, return all articles in the thread that starts
8253 with that article."
8254   (let* ((article (or article (gnus-summary-article-number)))
8255          (data (gnus-data-find-list article))
8256          (top-level (gnus-data-level (car data)))
8257          (top-subject
8258           (cond ((null gnus-thread-operation-ignore-subject)
8259                  (gnus-simplify-subject-re
8260                   (mail-header-subject (gnus-data-header (car data)))))
8261                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8262                  (gnus-simplify-subject-fuzzy
8263                   (mail-header-subject (gnus-data-header (car data)))))
8264                 (t nil)))
8265          (end-point (save-excursion
8266                       (if (gnus-summary-go-to-next-thread)
8267                           (point) (point-max))))
8268          articles)
8269     (while (and data
8270                 (< (gnus-data-pos (car data)) end-point))
8271       (when (or (not top-subject)
8272                 (string= top-subject
8273                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8274                              (gnus-simplify-subject-fuzzy
8275                               (mail-header-subject
8276                                (gnus-data-header (car data))))
8277                            (gnus-simplify-subject-re
8278                             (mail-header-subject
8279                              (gnus-data-header (car data)))))))
8280         (push (gnus-data-number (car data)) articles))
8281       (unless (and (setq data (cdr data))
8282                    (> (gnus-data-level (car data)) top-level))
8283         (setq data nil)))
8284     ;; Return the list of articles.
8285     (nreverse articles)))
8286
8287 (defun gnus-summary-rethread-current ()
8288   "Rethread the thread the current article is part of."
8289   (interactive)
8290   (let* ((gnus-show-threads t)
8291          (article (gnus-summary-article-number))
8292          (id (mail-header-id (gnus-summary-article-header)))
8293          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8294     (unless id
8295       (error "No article on the current line"))
8296     (gnus-rebuild-thread id)
8297     (gnus-summary-goto-subject article)))
8298
8299 (defun gnus-summary-reparent-thread ()
8300   "Make the current article child of the marked (or previous) article.
8301
8302 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8303 is non-nil or the Subject: of both articles are the same."
8304   (interactive)
8305   (unless (not (gnus-group-read-only-p))
8306     (error "The current newsgroup does not support article editing"))
8307   (unless (<= (length gnus-newsgroup-processable) 1)
8308     (error "No more than one article may be marked"))
8309   (save-window-excursion
8310     (let ((gnus-article-buffer " *reparent*")
8311           (current-article (gnus-summary-article-number))
8312           ;; First grab the marked article, otherwise one line up.
8313           (parent-article (if (not (null gnus-newsgroup-processable))
8314                               (car gnus-newsgroup-processable)
8315                             (save-excursion
8316                               (if (eq (forward-line -1) 0)
8317                                   (gnus-summary-article-number)
8318                                 (error "Beginning of summary buffer"))))))
8319       (unless (not (eq current-article parent-article))
8320         (error "An article may not be self-referential"))
8321       (let ((message-id (mail-header-id
8322                          (gnus-summary-article-header parent-article))))
8323         (unless (and message-id (not (equal message-id "")))
8324           (error "No message-id in desired parent"))
8325         ;; We don't want the article to be marked as read.
8326         (let (gnus-mark-article-hook)
8327           (gnus-summary-select-article t t nil current-article))
8328         (set-buffer gnus-original-article-buffer)
8329         (let ((buf (format "%s" (buffer-string))))
8330           (with-temp-buffer
8331             (insert buf)
8332             (goto-char (point-min))
8333             (if (re-search-forward "^References: " nil t)
8334                 (progn
8335                   (re-search-forward "^[^ \t]" nil t)
8336                   (forward-line -1)
8337                   (end-of-line)
8338                   (insert " " message-id))
8339               (insert "References: " message-id "\n"))
8340             (unless (gnus-request-replace-article
8341                      current-article (car gnus-article-current)
8342                      (current-buffer))
8343               (error "Couldn't replace article"))))
8344         (set-buffer gnus-summary-buffer)
8345         (gnus-summary-unmark-all-processable)
8346         (gnus-summary-update-article current-article)
8347         (gnus-summary-rethread-current)
8348         (gnus-message 3 "Article %d is now the child of article %d"
8349                       current-article parent-article)))))
8350
8351 (defun gnus-summary-toggle-threads (&optional arg)
8352   "Toggle showing conversation threads.
8353 If ARG is positive number, turn showing conversation threads on."
8354   (interactive "P")
8355   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8356     (setq gnus-show-threads
8357           (if (null arg) (not gnus-show-threads)
8358             (> (prefix-numeric-value arg) 0)))
8359     (gnus-summary-prepare)
8360     (gnus-summary-goto-subject current)
8361     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8362     (gnus-summary-position-point)))
8363
8364 (defun gnus-summary-show-all-threads ()
8365   "Show all threads."
8366   (interactive)
8367   (save-excursion
8368     (let ((buffer-read-only nil))
8369       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8370   (gnus-summary-position-point))
8371
8372 (defun gnus-summary-show-thread ()
8373   "Show thread subtrees.
8374 Returns nil if no thread was there to be shown."
8375   (interactive)
8376   (let ((buffer-read-only nil)
8377         (orig (point))
8378         ;; first goto end then to beg, to have point at beg after let
8379         (end (progn (end-of-line) (point)))
8380         (beg (progn (beginning-of-line) (point))))
8381     (prog1
8382         ;; Any hidden lines here?
8383         (search-forward "\r" end t)
8384       (subst-char-in-region beg end ?\^M ?\n t)
8385       (goto-char orig)
8386       (gnus-summary-position-point))))
8387
8388 (defun gnus-summary-hide-all-threads ()
8389   "Hide all thread subtrees."
8390   (interactive)
8391   (save-excursion
8392     (goto-char (point-min))
8393     (gnus-summary-hide-thread)
8394     (while (zerop (gnus-summary-next-thread 1 t))
8395       (gnus-summary-hide-thread)))
8396   (gnus-summary-position-point))
8397
8398 (defun gnus-summary-hide-thread ()
8399   "Hide thread subtrees.
8400 Returns nil if no threads were there to be hidden."
8401   (interactive)
8402   (let ((buffer-read-only nil)
8403         (start (point))
8404         (article (gnus-summary-article-number)))
8405     (goto-char start)
8406     ;; Go forward until either the buffer ends or the subthread
8407     ;; ends.
8408     (when (and (not (eobp))
8409                (or (zerop (gnus-summary-next-thread 1 t))
8410                    (goto-char (point-max))))
8411       (prog1
8412           (if (and (> (point) start)
8413                    (search-backward "\n" start t))
8414               (progn
8415                 (subst-char-in-region start (point) ?\n ?\^M)
8416                 (gnus-summary-goto-subject article))
8417             (goto-char start)
8418             nil)
8419         ;;(gnus-summary-position-point)
8420         ))))
8421
8422 (defun gnus-summary-go-to-next-thread (&optional previous)
8423   "Go to the same level (or less) next thread.
8424 If PREVIOUS is non-nil, go to previous thread instead.
8425 Return the article number moved to, or nil if moving was impossible."
8426   (let ((level (gnus-summary-thread-level))
8427         (way (if previous -1 1))
8428         (beg (point)))
8429     (forward-line way)
8430     (while (and (not (eobp))
8431                 (< level (gnus-summary-thread-level)))
8432       (forward-line way))
8433     (if (eobp)
8434         (progn
8435           (goto-char beg)
8436           nil)
8437       (setq beg (point))
8438       (prog1
8439           (gnus-summary-article-number)
8440         (goto-char beg)))))
8441
8442 (defun gnus-summary-next-thread (n &optional silent)
8443   "Go to the same level next N'th thread.
8444 If N is negative, search backward instead.
8445 Returns the difference between N and the number of skips actually
8446 done.
8447
8448 If SILENT, don't output messages."
8449   (interactive "p")
8450   (let ((backward (< n 0))
8451         (n (abs n)))
8452     (while (and (> n 0)
8453                 (gnus-summary-go-to-next-thread backward))
8454       (decf n))
8455     (unless silent
8456       (gnus-summary-position-point))
8457     (when (and (not silent) (/= 0 n))
8458       (gnus-message 7 "No more threads"))
8459     n))
8460
8461 (defun gnus-summary-prev-thread (n)
8462   "Go to the same level previous N'th thread.
8463 Returns the difference between N and the number of skips actually
8464 done."
8465   (interactive "p")
8466   (gnus-summary-next-thread (- n)))
8467
8468 (defun gnus-summary-go-down-thread ()
8469   "Go down one level in the current thread."
8470   (let ((children (gnus-summary-article-children)))
8471     (when children
8472       (gnus-summary-goto-subject (car children)))))
8473
8474 (defun gnus-summary-go-up-thread ()
8475   "Go up one level in the current thread."
8476   (let ((parent (gnus-summary-article-parent)))
8477     (when parent
8478       (gnus-summary-goto-subject parent))))
8479
8480 (defun gnus-summary-down-thread (n)
8481   "Go down thread N steps.
8482 If N is negative, go up instead.
8483 Returns the difference between N and how many steps down that were
8484 taken."
8485   (interactive "p")
8486   (let ((up (< n 0))
8487         (n (abs n)))
8488     (while (and (> n 0)
8489                 (if up (gnus-summary-go-up-thread)
8490                   (gnus-summary-go-down-thread)))
8491       (setq n (1- n)))
8492     (gnus-summary-position-point)
8493     (when (/= 0 n)
8494       (gnus-message 7 "Can't go further"))
8495     n))
8496
8497 (defun gnus-summary-up-thread (n)
8498   "Go up thread N steps.
8499 If N is negative, go up instead.
8500 Returns the difference between N and how many steps down that were
8501 taken."
8502   (interactive "p")
8503   (gnus-summary-down-thread (- n)))
8504
8505 (defun gnus-summary-top-thread ()
8506   "Go to the top of the thread."
8507   (interactive)
8508   (while (gnus-summary-go-up-thread))
8509   (gnus-summary-article-number))
8510
8511 (defun gnus-summary-kill-thread (&optional unmark)
8512   "Mark articles under current thread as read.
8513 If the prefix argument is positive, remove any kinds of marks.
8514 If the prefix argument is negative, tick articles instead."
8515   (interactive "P")
8516   (when unmark
8517     (setq unmark (prefix-numeric-value unmark)))
8518   (let ((articles (gnus-summary-articles-in-thread)))
8519     (save-excursion
8520       ;; Expand the thread.
8521       (gnus-summary-show-thread)
8522       ;; Mark all the articles.
8523       (while articles
8524         (gnus-summary-goto-subject (car articles))
8525         (cond ((null unmark)
8526                (gnus-summary-mark-article-as-read gnus-killed-mark))
8527               ((> unmark 0)
8528                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8529               (t
8530                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8531         (setq articles (cdr articles))))
8532     ;; Hide killed subtrees.
8533     (and (null unmark)
8534          gnus-thread-hide-killed
8535          (gnus-summary-hide-thread))
8536     ;; If marked as read, go to next unread subject.
8537     (when (null unmark)
8538       ;; Go to next unread subject.
8539       (gnus-summary-next-subject 1 t)))
8540   (gnus-set-mode-line 'summary))
8541
8542 ;; Summary sorting commands
8543
8544 (defun gnus-summary-sort-by-number (&optional reverse)
8545   "Sort the summary buffer by article number.
8546 Argument REVERSE means reverse order."
8547   (interactive "P")
8548   (gnus-summary-sort 'number reverse))
8549
8550 (defun gnus-summary-sort-by-author (&optional reverse)
8551   "Sort the summary buffer by author name alphabetically.
8552 If case-fold-search is non-nil, case of letters is ignored.
8553 Argument REVERSE means reverse order."
8554   (interactive "P")
8555   (gnus-summary-sort 'author reverse))
8556
8557 (defun gnus-summary-sort-by-subject (&optional reverse)
8558   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8559 If case-fold-search is non-nil, case of letters is ignored.
8560 Argument REVERSE means reverse order."
8561   (interactive "P")
8562   (gnus-summary-sort 'subject reverse))
8563
8564 (defun gnus-summary-sort-by-date (&optional reverse)
8565   "Sort the summary buffer by date.
8566 Argument REVERSE means reverse order."
8567   (interactive "P")
8568   (gnus-summary-sort 'date reverse))
8569
8570 (defun gnus-summary-sort-by-score (&optional reverse)
8571   "Sort the summary buffer by score.
8572 Argument REVERSE means reverse order."
8573   (interactive "P")
8574   (gnus-summary-sort 'score reverse))
8575
8576 (defun gnus-summary-sort-by-lines (&optional reverse)
8577   "Sort the summary buffer by article length.
8578 Argument REVERSE means reverse order."
8579   (interactive "P")
8580   (gnus-summary-sort 'lines reverse))
8581
8582 (defun gnus-summary-sort (predicate reverse)
8583   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8584   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8585          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8586          (gnus-thread-sort-functions
8587           (if (not reverse)
8588               thread
8589             `(lambda (t1 t2)
8590                (,thread t2 t1))))
8591          (gnus-article-sort-functions
8592           (if (not reverse)
8593               article
8594             `(lambda (t1 t2)
8595                (,article t2 t1))))
8596          (buffer-read-only)
8597          (gnus-summary-prepare-hook nil))
8598     ;; We do the sorting by regenerating the threads.
8599     (gnus-summary-prepare)
8600     ;; Hide subthreads if needed.
8601     (when (and gnus-show-threads gnus-thread-hide-subtree)
8602       (gnus-summary-hide-all-threads))))
8603
8604 ;; Summary saving commands.
8605
8606 (defun gnus-summary-save-article (&optional n not-saved)
8607   "Save the current article using the default saver function.
8608 If N is a positive number, save the N next articles.
8609 If N is a negative number, save the N previous articles.
8610 If N is nil and any articles have been marked with the process mark,
8611 save those articles instead.
8612 The variable `gnus-default-article-saver' specifies the saver function."
8613   (interactive "P")
8614   (let* ((articles (gnus-summary-work-articles n))
8615          (save-buffer (save-excursion
8616                         (nnheader-set-temp-buffer " *Gnus Save*")))
8617          (num (length articles))
8618          header article file)
8619     (while articles
8620       (setq header (gnus-summary-article-header
8621                     (setq article (pop articles))))
8622       (if (not (vectorp header))
8623           ;; This is a pseudo-article.
8624           (if (assq 'name header)
8625               (gnus-copy-file (cdr (assq 'name header)))
8626             (gnus-message 1 "Article %d is unsaveable" article))
8627         ;; This is a real article.
8628         (save-window-excursion
8629           (gnus-summary-select-article t nil nil article))
8630         (save-excursion
8631           (set-buffer save-buffer)
8632           (erase-buffer)
8633           (insert-buffer-substring gnus-original-article-buffer))
8634         (setq file (gnus-article-save save-buffer file num))
8635         (gnus-summary-remove-process-mark article)
8636         (unless not-saved
8637           (gnus-summary-set-saved-mark article))))
8638     (gnus-kill-buffer save-buffer)
8639     (gnus-summary-position-point)
8640     (gnus-set-mode-line 'summary)
8641     n))
8642
8643 (defun gnus-summary-pipe-output (&optional arg)
8644   "Pipe the current article to a subprocess.
8645 If N is a positive number, pipe the N next articles.
8646 If N is a negative number, pipe the N previous articles.
8647 If N is nil and any articles have been marked with the process mark,
8648 pipe those articles instead."
8649   (interactive "P")
8650   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8651     (gnus-summary-save-article arg t))
8652   (gnus-configure-windows 'pipe))
8653
8654 (defun gnus-summary-save-article-mail (&optional arg)
8655   "Append the current article to an mail file.
8656 If N is a positive number, save the N next articles.
8657 If N is a negative number, save the N previous articles.
8658 If N is nil and any articles have been marked with the process mark,
8659 save those articles instead."
8660   (interactive "P")
8661   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8662     (gnus-summary-save-article arg)))
8663
8664 (defun gnus-summary-save-article-rmail (&optional arg)
8665   "Append the current article to an rmail file.
8666 If N is a positive number, save the N next articles.
8667 If N is a negative number, save the N previous articles.
8668 If N is nil and any articles have been marked with the process mark,
8669 save those articles instead."
8670   (interactive "P")
8671   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8672     (gnus-summary-save-article arg)))
8673
8674 (defun gnus-summary-save-article-file (&optional arg)
8675   "Append the current article to a file.
8676 If N is a positive number, save the N next articles.
8677 If N is a negative number, save the N previous articles.
8678 If N is nil and any articles have been marked with the process mark,
8679 save those articles instead."
8680   (interactive "P")
8681   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8682     (gnus-summary-save-article arg)))
8683
8684 (defun gnus-summary-write-article-file (&optional arg)
8685   "Write the current article to a file, deleting the previous file.
8686 If N is a positive number, save the N next articles.
8687 If N is a negative number, save the N previous articles.
8688 If N is nil and any articles have been marked with the process mark,
8689 save those articles instead."
8690   (interactive "P")
8691   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8692     (gnus-summary-save-article arg)))
8693
8694 (defun gnus-summary-save-article-body-file (&optional arg)
8695   "Append the current article body to a file.
8696 If N is a positive number, save the N next articles.
8697 If N is a negative number, save the N previous articles.
8698 If N is nil and any articles have been marked with the process mark,
8699 save those articles instead."
8700   (interactive "P")
8701   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8702     (gnus-summary-save-article arg)))
8703
8704 (defun gnus-summary-pipe-message (program)
8705   "Pipe the current article through PROGRAM."
8706   (interactive "sProgram: ")
8707   (gnus-summary-select-article)
8708   (let ((mail-header-separator ""))
8709     (gnus-eval-in-buffer-window gnus-article-buffer
8710       (save-restriction
8711         (widen)
8712         (let ((start (window-start))
8713               buffer-read-only)
8714           (message-pipe-buffer-body program)
8715           (set-window-start (get-buffer-window (current-buffer)) start))))))
8716
8717 (defun gnus-get-split-value (methods)
8718   "Return a value based on the split METHODS."
8719   (let (split-name method result match)
8720     (when methods
8721       (save-excursion
8722         (set-buffer gnus-original-article-buffer)
8723         (save-restriction
8724           (nnheader-narrow-to-headers)
8725           (while methods
8726             (goto-char (point-min))
8727             (setq method (pop methods))
8728             (setq match (car method))
8729             (when (cond
8730                    ((stringp match)
8731                     ;; Regular expression.
8732                     (ignore-errors
8733                       (re-search-forward match nil t)))
8734                    ((gnus-functionp match)
8735                     ;; Function.
8736                     (save-restriction
8737                       (widen)
8738                       (setq result (funcall match gnus-newsgroup-name))))
8739                    ((consp match)
8740                     ;; Form.
8741                     (save-restriction
8742                       (widen)
8743                       (setq result (eval match)))))
8744               (setq split-name (append (cdr method) split-name))
8745               (cond ((stringp result)
8746                      (push (expand-file-name
8747                             result gnus-article-save-directory)
8748                            split-name))
8749                     ((consp result)
8750                      (setq split-name (append result split-name)))))))))
8751     split-name))
8752
8753 (defun gnus-valid-move-group-p (group)
8754   (and (boundp group)
8755        (symbol-name group)
8756        (symbol-value group)
8757        (memq 'respool
8758              (assoc (symbol-name
8759                      (car (gnus-find-method-for-group
8760                            (symbol-name group))))
8761                     gnus-valid-select-methods))))
8762
8763 (defun gnus-read-move-group-name (prompt default articles prefix)
8764   "Read a group name."
8765   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8766          (minibuffer-confirm-incomplete nil) ; XEmacs
8767          (prom
8768           (format "%s %s to:"
8769                   prompt
8770                   (if (> (length articles) 1)
8771                       (format "these %d articles" (length articles))
8772                     "this article")))
8773          (to-newsgroup
8774           (cond
8775            ((null split-name)
8776             (gnus-completing-read default prom
8777                                   gnus-active-hashtb
8778                                   'gnus-valid-move-group-p
8779                                   nil prefix
8780                                   'gnus-group-history))
8781            ((= 1 (length split-name))
8782             (gnus-completing-read (car split-name) prom
8783                                   gnus-active-hashtb
8784                                   'gnus-valid-move-group-p
8785                                   nil nil
8786                                   'gnus-group-history))
8787            (t
8788             (gnus-completing-read nil prom
8789                                   (mapcar (lambda (el) (list el))
8790                                           (nreverse split-name))
8791                                   nil nil nil
8792                                   'gnus-group-history)))))
8793     (when to-newsgroup
8794       (if (or (string= to-newsgroup "")
8795               (string= to-newsgroup prefix))
8796           (setq to-newsgroup default))
8797       (unless to-newsgroup
8798         (error "No group name entered"))
8799       (or (gnus-active to-newsgroup)
8800           (gnus-activate-group to-newsgroup)
8801           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8802                                      to-newsgroup))
8803               (or (and (gnus-request-create-group
8804                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8805                        (gnus-activate-group to-newsgroup nil nil
8806                                             (gnus-group-name-to-method
8807                                              to-newsgroup)))
8808                   (error "Couldn't create group %s" to-newsgroup)))
8809           (error "No such group: %s" to-newsgroup)))
8810     to-newsgroup))
8811
8812 ;; Summary extract commands
8813
8814 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8815   (let ((buffer-read-only nil)
8816         (article (gnus-summary-article-number))
8817         after-article b e)
8818     (unless (gnus-summary-goto-subject article)
8819       (error "No such article: %d" article))
8820     (gnus-summary-position-point)
8821     ;; If all commands are to be bunched up on one line, we collect
8822     ;; them here.
8823     (unless gnus-view-pseudos-separately
8824       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8825             files action)
8826         (while ps
8827           (setq action (cdr (assq 'action (car ps))))
8828           (setq files (list (cdr (assq 'name (car ps)))))
8829           (while (and ps (cdr ps)
8830                       (string= (or action "1")
8831                                (or (cdr (assq 'action (cadr ps))) "2")))
8832             (push (cdr (assq 'name (cadr ps))) files)
8833             (setcdr ps (cddr ps)))
8834           (when files
8835             (when (not (string-match "%s" action))
8836               (push " " files))
8837             (push " " files)
8838             (when (assq 'execute (car ps))
8839               (setcdr (assq 'execute (car ps))
8840                       (funcall (if (string-match "%s" action)
8841                                    'format 'concat)
8842                                action
8843                                (mapconcat
8844                                 (lambda (f)
8845                                   (if (equal f " ")
8846                                       f
8847                                     (gnus-quote-arg-for-sh-or-csh f)))
8848                                 files " ")))))
8849           (setq ps (cdr ps)))))
8850     (if (and gnus-view-pseudos (not not-view))
8851         (while pslist
8852           (when (assq 'execute (car pslist))
8853             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8854                                   (eq gnus-view-pseudos 'not-confirm)))
8855           (setq pslist (cdr pslist)))
8856       (save-excursion
8857         (while pslist
8858           (setq after-article (or (cdr (assq 'article (car pslist)))
8859                                   (gnus-summary-article-number)))
8860           (gnus-summary-goto-subject after-article)
8861           (forward-line 1)
8862           (setq b (point))
8863           (insert "    " (file-name-nondirectory
8864                           (cdr (assq 'name (car pslist))))
8865                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8866           (setq e (point))
8867           (forward-line -1)             ; back to `b'
8868           (gnus-add-text-properties
8869            b (1- e) (list 'gnus-number gnus-reffed-article-number
8870                           gnus-mouse-face-prop gnus-mouse-face))
8871           (gnus-data-enter
8872            after-article gnus-reffed-article-number
8873            gnus-unread-mark b (car pslist) 0 (- e b))
8874           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8875           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8876           (setq pslist (cdr pslist)))))))
8877
8878 (defun gnus-pseudos< (p1 p2)
8879   (let ((c1 (cdr (assq 'action p1)))
8880         (c2 (cdr (assq 'action p2))))
8881     (and c1 c2 (string< c1 c2))))
8882
8883 (defun gnus-request-pseudo-article (props)
8884   (cond ((assq 'execute props)
8885          (gnus-execute-command (cdr (assq 'execute props)))))
8886   (let ((gnus-current-article (gnus-summary-article-number)))
8887     (gnus-run-hooks 'gnus-mark-article-hook)))
8888
8889 (defun gnus-execute-command (command &optional automatic)
8890   (save-excursion
8891     (gnus-article-setup-buffer)
8892     (set-buffer gnus-article-buffer)
8893     (setq buffer-read-only nil)
8894     (let ((command (if automatic command
8895                      (read-string "Command: " (cons command 0)))))
8896       (erase-buffer)
8897       (insert "$ " command "\n\n")
8898       (if gnus-view-pseudo-asynchronously
8899           (start-process "gnus-execute" (current-buffer) shell-file-name
8900                          shell-command-switch command)
8901         (call-process shell-file-name nil t nil
8902                       shell-command-switch command)))))
8903
8904 ;; Summary kill commands.
8905
8906 (defun gnus-summary-edit-global-kill (article)
8907   "Edit the \"global\" kill file."
8908   (interactive (list (gnus-summary-article-number)))
8909   (gnus-group-edit-global-kill article))
8910
8911 (defun gnus-summary-edit-local-kill ()
8912   "Edit a local kill file applied to the current newsgroup."
8913   (interactive)
8914   (setq gnus-current-headers (gnus-summary-article-header))
8915   (gnus-group-edit-local-kill
8916    (gnus-summary-article-number) gnus-newsgroup-name))
8917
8918 ;;; Header reading.
8919
8920 (defun gnus-read-header (id &optional header)
8921   "Read the headers of article ID and enter them into the Gnus system."
8922   (let ((group gnus-newsgroup-name)
8923         (gnus-override-method
8924          (and (gnus-news-group-p gnus-newsgroup-name)
8925               gnus-refer-article-method))
8926         where)
8927     ;; First we check to see whether the header in question is already
8928     ;; fetched.
8929     (if (stringp id)
8930         ;; This is a Message-ID.
8931         (setq header (or header (gnus-id-to-header id)))
8932       ;; This is an article number.
8933       (setq header (or header (gnus-summary-article-header id))))
8934     (if (and header
8935              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8936         ;; We have found the header.
8937         header
8938       ;; If this is a sparse article, we have to nix out its
8939       ;; previous entry in the thread hashtb.
8940       (when (and header
8941                  (gnus-summary-article-sparse-p (mail-header-number header)))
8942         (let* ((parent (gnus-parent-id (mail-header-references header)))
8943                (thread (and parent (gnus-id-to-thread parent))))
8944           (when thread
8945             (delq (assq header thread) thread))))
8946       ;; We have to really fetch the header to this article.
8947       (save-excursion
8948         (set-buffer nntp-server-buffer)
8949         (when (setq where (gnus-request-head id group))
8950           (nnheader-fold-continuation-lines)
8951           (goto-char (point-max))
8952           (insert ".\n")
8953           (goto-char (point-min))
8954           (insert "211 ")
8955           (princ (cond
8956                   ((numberp id) id)
8957                   ((cdr where) (cdr where))
8958                   (header (mail-header-number header))
8959                   (t gnus-reffed-article-number))
8960                  (current-buffer))
8961           (insert " Article retrieved.\n"))
8962         (if (or (not where)
8963                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8964             ()                          ; Malformed head.
8965           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8966             (when (and (stringp id)
8967                        (not (string= (gnus-group-real-name group)
8968                                      (car where))))
8969               ;; If we fetched by Message-ID and the article came
8970               ;; from a different group, we fudge some bogus article
8971               ;; numbers for this article.
8972               (mail-header-set-number header gnus-reffed-article-number))
8973             (save-excursion
8974               (set-buffer gnus-summary-buffer)
8975               (decf gnus-reffed-article-number)
8976               (gnus-remove-header (mail-header-number header))
8977               (push header gnus-newsgroup-headers)
8978               (setq gnus-current-headers header)
8979               (push (mail-header-number header) gnus-newsgroup-limit)))
8980           header)))))
8981
8982 (defun gnus-remove-header (number)
8983   "Remove header NUMBER from `gnus-newsgroup-headers'."
8984   (if (and gnus-newsgroup-headers
8985            (= number (mail-header-number (car gnus-newsgroup-headers))))
8986       (pop gnus-newsgroup-headers)
8987     (let ((headers gnus-newsgroup-headers))
8988       (while (and (cdr headers)
8989                   (not (= number (mail-header-number (cadr headers)))))
8990         (pop headers))
8991       (when (cdr headers)
8992         (setcdr headers (cddr headers))))))
8993
8994 ;;;
8995 ;;; summary highlights
8996 ;;;
8997
8998 (defun gnus-highlight-selected-summary ()
8999   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9000   ;; Highlight selected article in summary buffer
9001   (when gnus-summary-selected-face
9002     (save-excursion
9003       (let* ((beg (progn (beginning-of-line) (point)))
9004              (end (progn (end-of-line) (point)))
9005              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9006              (from (if (get-text-property beg gnus-mouse-face-prop)
9007                        beg
9008                      (or (next-single-property-change
9009                           beg gnus-mouse-face-prop nil end)
9010                          beg)))
9011              (to
9012               (if (= from end)
9013                   (- from 2)
9014                 (or (next-single-property-change
9015                      from gnus-mouse-face-prop nil end)
9016                     end))))
9017         ;; If no mouse-face prop on line we will have to = from = end,
9018         ;; so we highlight the entire line instead.
9019         (when (= (+ to 2) from)
9020           (setq from beg)
9021           (setq to end))
9022         (if gnus-newsgroup-selected-overlay
9023             ;; Move old overlay.
9024             (gnus-move-overlay
9025              gnus-newsgroup-selected-overlay from to (current-buffer))
9026           ;; Create new overlay.
9027           (gnus-overlay-put
9028            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9029            'face gnus-summary-selected-face))))))
9030
9031 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9032 (defun gnus-summary-highlight-line ()
9033   "Highlight current line according to `gnus-summary-highlight'."
9034   (let* ((list gnus-summary-highlight)
9035          (p (point))
9036          (end (progn (end-of-line) (point)))
9037          ;; now find out where the line starts and leave point there.
9038          (beg (progn (beginning-of-line) (point)))
9039          (article (gnus-summary-article-number))
9040          (score (or (cdr (assq (or article gnus-current-article)
9041                                gnus-newsgroup-scored))
9042                     gnus-summary-default-score 0))
9043          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9044          (inhibit-read-only t))
9045     ;; Eval the cars of the lists until we find a match.
9046     (let ((default gnus-summary-default-score))
9047       (while (and list
9048                   (not (eval (caar list))))
9049         (setq list (cdr list))))
9050     (let ((face (cdar list)))
9051       (unless (eq face (get-text-property beg 'face))
9052         (gnus-put-text-property-excluding-characters-with-faces
9053          beg end 'face
9054          (setq face (if (boundp face) (symbol-value face) face)))
9055         (when gnus-summary-highlight-line-function
9056           (funcall gnus-summary-highlight-line-function article face))))
9057     (goto-char p)))
9058
9059 (defun gnus-update-read-articles (group unread &optional compute)
9060   "Update the list of read articles in GROUP."
9061   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9062          (entry (gnus-gethash group gnus-newsrc-hashtb))
9063          (info (nth 2 entry))
9064          (prev 1)
9065          (unread (sort (copy-sequence unread) '<))
9066          read)
9067     (if (or (not info) (not active))
9068         ;; There is no info on this group if it was, in fact,
9069         ;; killed.  Gnus stores no information on killed groups, so
9070         ;; there's nothing to be done.
9071         ;; One could store the information somewhere temporarily,
9072         ;; perhaps...  Hmmm...
9073         ()
9074       ;; Remove any negative articles numbers.
9075       (while (and unread (< (car unread) 0))
9076         (setq unread (cdr unread)))
9077       ;; Remove any expired article numbers
9078       (while (and unread (< (car unread) (car active)))
9079         (setq unread (cdr unread)))
9080       ;; Compute the ranges of read articles by looking at the list of
9081       ;; unread articles.
9082       (while unread
9083         (when (/= (car unread) prev)
9084           (push (if (= prev (1- (car unread))) prev
9085                   (cons prev (1- (car unread))))
9086                 read))
9087         (setq prev (1+ (car unread)))
9088         (setq unread (cdr unread)))
9089       (when (<= prev (cdr active))
9090         (push (cons prev (cdr active)) read))
9091       (setq read (if (> (length read) 1) (nreverse read) read))
9092       (if compute
9093           read
9094         (save-excursion
9095           (set-buffer gnus-group-buffer)
9096           (gnus-undo-register
9097             `(progn
9098                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9099                (gnus-info-set-read ',info ',(gnus-info-read info))
9100                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9101                (gnus-group-update-group ,group t))))
9102         ;; Enter this list into the group info.
9103         (gnus-info-set-read info read)
9104         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9105         (gnus-get-unread-articles-in-group info (gnus-active group))
9106         t))))
9107
9108 (defun gnus-offer-save-summaries ()
9109   "Offer to save all active summary buffers."
9110   (save-excursion
9111     (let ((buflist (buffer-list))
9112           buffers bufname)
9113       ;; Go through all buffers and find all summaries.
9114       (while buflist
9115         (and (setq bufname (buffer-name (car buflist)))
9116              (string-match "Summary" bufname)
9117              (save-excursion
9118                (set-buffer bufname)
9119                ;; We check that this is, indeed, a summary buffer.
9120                (and (eq major-mode 'gnus-summary-mode)
9121                     ;; Also make sure this isn't bogus.
9122                     gnus-newsgroup-prepared
9123                     ;; Also make sure that this isn't a dead summary buffer.
9124                     (not gnus-dead-summary-mode)))
9125              (push bufname buffers))
9126         (setq buflist (cdr buflist)))
9127       ;; Go through all these summary buffers and offer to save them.
9128       (when buffers
9129         (map-y-or-n-p
9130          "Update summary buffer %s? "
9131          (lambda (buf)
9132            (switch-to-buffer buf)
9133            (gnus-summary-exit))
9134          buffers)))))
9135
9136
9137 ;;; @ for mime-partial
9138 ;;;
9139
9140 (defun gnus-request-partial-message ()
9141   (save-excursion
9142     (let ((number (gnus-summary-article-number))
9143           (group gnus-newsgroup-name)
9144           (mother gnus-article-buffer))
9145       (set-buffer (get-buffer-create " *Partial Article*"))
9146       (erase-buffer)
9147       (setq mime-preview-buffer mother)
9148       (gnus-request-article-this-buffer number group)
9149       (mime-parse-buffer)
9150       )))
9151
9152 (autoload 'mime-combine-message/partial-pieces-automatically
9153   "mime-partial"
9154   "Internal method to combine message/partial messages automatically.")
9155
9156 (mime-add-condition
9157  'action '((type . message)(subtype . partial)
9158            (major-mode . gnus-original-article-mode)
9159            (method . mime-combine-message/partial-pieces-automatically)
9160            (summary-buffer-exp . gnus-summary-buffer)
9161            (request-partial-message-method . gnus-request-partial-message)
9162            ))
9163
9164
9165 ;;; @ end
9166 ;;;
9167
9168 (defun gnus-newsgroup-setup-default-charset ()
9169   "Setup newsgroup default charset."
9170   (let ((name (and gnus-newsgroup-name 
9171                    (string-match "[^:]+$" gnus-newsgroup-name)
9172                    (match-string 0 gnus-newsgroup-name))))
9173     (setq gnus-newsgroup-default-charset
9174           (or (and gnus-newsgroup-name
9175                    (or (gnus-group-find-parameter 
9176                         gnus-newsgroup-name 'charset)
9177                        (let ((alist gnus-newsgroup-default-charset-alist) 
9178                              elem (charset nil))
9179                          (while alist
9180                            (if (and name
9181                                     (string-match 
9182                                      (car (setq elem (pop alist)))
9183                                      name))
9184                                (setq alist nil
9185                                      charset (cdr elem))))
9186                          charset)))
9187               rfc2047-default-charset))
9188     (setq gnus-newsgroup-iso-8859-1-forced 
9189           (and gnus-newsgroup-name
9190                (or (gnus-group-find-parameter
9191                     gnus-newsgroup-name 'iso-8859-1-forced)
9192                    (and name
9193                         (string-match  gnus-newsgroup-iso-8859-1-forced-regexp 
9194                                        name))))))
9195   (if (stringp gnus-newsgroup-default-charset)
9196       (setq gnus-newsgroup-default-charset
9197             (intern (downcase gnus-newsgroup-default-charset))))
9198   (setq gnus-newsgroup-iso-8859-1-forced
9199         (if (stringp gnus-newsgroup-iso-8859-1-forced)
9200             (intern (downcase gnus-newsgroup-iso-8859-1-forced))
9201           (and gnus-newsgroup-iso-8859-1-forced
9202                gnus-newsgroup-default-charset))))
9203   
9204 ;;;
9205 ;;; MIME Commands
9206 ;;;
9207
9208 (defun gnus-summary-display-buttonized ()
9209   "Display the current article buffer fully MIME-buttonized."
9210   (interactive)
9211   (require 'gnus-art)
9212   (let ((gnus-unbuttonized-mime-types nil))
9213     (gnus-summary-show-article)))
9214     
9215 (defun gnus-summary-toggle-display-buttonized ()
9216   "Toggle the buttonizing of the article buffer."
9217   (interactive)
9218   (require 'gnus-art)
9219   (if (setq gnus-inhibit-mime-unbuttonizing
9220             (not gnus-inhibit-mime-unbuttonizing))
9221       (let ((gnus-unbuttonized-mime-types nil))
9222         (gnus-summary-show-article))
9223     (gnus-summary-show-article)))
9224     
9225 (gnus-ems-redefine)
9226
9227 (provide 'gnus-sum)
9228
9229 (run-hooks 'gnus-sum-load-hook)
9230
9231 ;;; gnus-sum.el ends here