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