1999-02-11 Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   ;; Avoid byte-compile warnings.
44   (defvar gnus-article-decoded-p)
45   (defvar gnus-decode-encoded-word-function)
46   )
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
50
51 (defcustom gnus-kill-summary-on-exit t
52   "*If non-nil, kill the summary buffer when you exit from it.
53 If nil, the summary will become a \"*Dead Summary*\" buffer, and
54 it will be killed sometime later."
55   :group 'gnus-summary-exit
56   :type 'boolean)
57
58 (defcustom gnus-fetch-old-headers nil
59   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
60 If an unread article in the group refers to an older, already read (or
61 just marked as read) article, the old article will not normally be
62 displayed in the Summary buffer.  If this variable is non-nil, Gnus
63 will attempt to grab the headers to the old articles, and thereby
64 build complete threads.  If it has the value `some', only enough
65 headers to connect otherwise loose threads will be displayed.  This
66 variable can also be a number.  In that case, no more than that number
67 of old headers will be fetched.  If it has the value `invisible', all
68 old headers will be fetched, but none will be displayed.
69
70 The server has to support NOV for any of this to work."
71   :group 'gnus-thread
72   :type '(choice (const :tag "off" nil)
73                  (const some)
74                  number
75                  (sexp :menu-tag "other" t)))
76
77 (defcustom gnus-refer-thread-limit 200
78   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
79 If t, fetch all the available old headers."
80   :group 'gnus-thread
81   :type '(choice number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-summary-make-false-root 'adopt
85   "*nil means that Gnus won't gather loose threads.
86 If the root of a thread has expired or been read in a previous
87 session, the information necessary to build a complete thread has been
88 lost.  Instead of having many small sub-threads from this original thread
89 scattered all over the summary buffer, Gnus can gather them.
90
91 If non-nil, Gnus will try to gather all loose sub-threads from an
92 original thread into one large thread.
93
94 If this variable is non-nil, it should be one of `none', `adopt',
95 `dummy' or `empty'.
96
97 If this variable is `none', Gnus will not make a false root, but just
98 present the sub-threads after another.
99 If this variable is `dummy', Gnus will create a dummy root that will
100 have all the sub-threads as children.
101 If this variable is `adopt', Gnus will make one of the \"children\"
102 the parent and mark all the step-children as such.
103 If this variable is `empty', the \"children\" are printed with empty
104 subject fields.  (Or rather, they will be printed with a string
105 given by the `gnus-summary-same-subject' variable.)"
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const none)
109                  (const dummy)
110                  (const adopt)
111                  (const empty)))
112
113 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
114   "*A regexp to match subjects to be excluded from loose thread gathering.
115 As loose thread gathering is done on subjects only, that means that
116 there can be many false gatherings performed.  By rooting out certain
117 common subjects, gathering might become saner."
118   :group 'gnus-thread
119   :type 'regexp)
120
121 (defcustom gnus-summary-gather-subject-limit nil
122   "*Maximum length of subject comparisons when gathering loose threads.
123 Use nil to compare full subjects.  Setting this variable to a low
124 number will help gather threads that have been corrupted by
125 newsreaders chopping off subject lines, but it might also mean that
126 unrelated articles that have subject that happen to begin with the
127 same few characters will be incorrectly gathered.
128
129 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
130 comparing subjects."
131   :group 'gnus-thread
132   :type '(choice (const :tag "off" nil)
133                  (const fuzzy)
134                  (sexp :menu-tag "on" t)))
135
136 (defcustom gnus-simplify-subject-functions nil
137   "List of functions taking a string argument that simplify subjects.
138 The functions are applied recursively.
139
140 Useful functions to put in this list include: `gnus-simplify-subject-re',
141 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, marking commands will go to the next unread article.
183 If `never', commands that usually go to the next unread article, will
184 go to the next article, whether it is read or not.
185 If nil, only the marking commands will go to the next (un)read article."
186   :group 'gnus-summary-marks
187   :link '(custom-manual "(gnus)Setting Marks")
188   :type '(choice (const :tag "off" nil)
189                  (const never)
190                  (sexp :menu-tag "on" t)))
191
192 (defcustom gnus-summary-default-score 0
193   "*Default article score level.
194 All scores generated by the score files will be added to this score.
195 If this variable is nil, scoring will be disabled."
196   :group 'gnus-score-default
197   :type '(choice (const :tag "disable")
198                  integer))
199
200 (defcustom gnus-summary-zcore-fuzz 0
201   "*Fuzziness factor for the zcore in the summary buffer.
202 Articles with scores closer than this to `gnus-summary-default-score'
203 will not be marked."
204   :group 'gnus-summary-format
205   :type 'integer)
206
207 (defcustom gnus-simplify-subject-fuzzy-regexp nil
208   "*Strings to be removed when doing fuzzy matches.
209 This can either be a regular expression or list of regular expressions
210 that will be removed from subject strings if fuzzy subject
211 simplification is selected."
212   :group 'gnus-thread
213   :type '(repeat regexp))
214
215 (defcustom gnus-show-threads t
216   "*If non-nil, display threads in summary mode."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-subtree nil
221   "*If non-nil, hide all threads initially.
222 If threads are hidden, you have to run the command
223 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
224 to expose hidden threads."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-killed t
229   "*If non-nil, hide killed threads automatically."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-ignore-subject t
234   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
235 If nil, articles that have different subjects from their parents will
236 start separate threads."
237   :group 'gnus-thread
238   :type 'boolean)
239
240 (defcustom gnus-thread-operation-ignore-subject t
241   "*If non-nil, subjects will be ignored when doing thread commands.
242 This affects commands like `gnus-summary-kill-thread' and
243 `gnus-summary-lower-thread'.
244
245 If this variable is nil, articles in the same thread with different
246 subjects will not be included in the operation in question.  If this
247 variable is `fuzzy', only articles that have subjects that are fuzzily
248 equal will be included."
249   :group 'gnus-thread
250   :type '(choice (const :tag "off" nil)
251                  (const fuzzy)
252                  (sexp :tag "on" t)))
253
254 (defcustom gnus-thread-indent-level 4
255   "*Number that says how much each sub-thread should be indented."
256   :group 'gnus-thread
257   :type 'integer)
258
259 (defcustom gnus-auto-extend-newsgroup t
260   "*If non-nil, extend newsgroup forward and backward when requested."
261   :group 'gnus-summary-choose
262   :type 'boolean)
263
264 (defcustom gnus-auto-select-first t
265   "*If nil, don't select the first unread article when entering a group.
266 If this variable is `best', select the highest-scored unread article
267 in the group.  If t, select the first unread article.
268
269 This variable can also be a function to place point on a likely
270 subject line.  Useful values include `gnus-summary-first-unread-subject',
271 `gnus-summary-first-unread-article' and
272 `gnus-summary-best-unread-article'.
273
274 If you want to prevent automatic selection of the first unread article
275 in some newsgroups, set the variable to nil in
276 `gnus-select-group-hook'."
277   :group 'gnus-group-select
278   :type '(choice (const :tag "none" nil)
279                  (const best)
280                  (sexp :menu-tag "first" t)
281                  (function-item gnus-summary-first-unread-subject)
282                  (function-item gnus-summary-first-unread-article)
283                  (function-item gnus-summary-best-unread-article)))
284
285 (defcustom gnus-auto-select-next t
286   "*If non-nil, offer to go to the next group from the end of the previous.
287 If the value is t and the next newsgroup is empty, Gnus will exit
288 summary mode and go back to group mode.  If the value is neither nil
289 nor t, Gnus will select the following unread newsgroup.  In
290 particular, if the value is the symbol `quietly', the next unread
291 newsgroup will be selected without any confirmation, and if it is
292 `almost-quietly', the next group will be selected without any
293 confirmation if you are located on the last article in the group.
294 Finally, if this variable is `slightly-quietly', the `Z n' command
295 will go to the next group without confirmation."
296   :group 'gnus-summary-maneuvering
297   :type '(choice (const :tag "off" nil)
298                  (const quietly)
299                  (const almost-quietly)
300                  (const slightly-quietly)
301                  (sexp :menu-tag "on" t)))
302
303 (defcustom gnus-auto-select-same nil
304   "*If non-nil, select the next article with the same subject.
305 If there are no more articles with the same subject, go to
306 the first unread article."
307   :group 'gnus-summary-maneuvering
308   :type 'boolean)
309
310 (defcustom gnus-summary-check-current nil
311   "*If non-nil, consider the current article when moving.
312 The \"unread\" movement commands will stay on the same line if the
313 current article is unread."
314   :group 'gnus-summary-maneuvering
315   :type 'boolean)
316
317 (defcustom gnus-auto-center-summary t
318   "*If non-nil, always center the current summary buffer.
319 In particular, if `vertical' do only vertical recentering.  If non-nil
320 and non-`vertical', do both horizontal and vertical recentering."
321   :group 'gnus-summary-maneuvering
322   :type '(choice (const :tag "none" nil)
323                  (const vertical)
324                  (integer :tag "height")
325                  (sexp :menu-tag "both" t)))
326
327 (defcustom gnus-show-all-headers nil
328   "*If non-nil, don't hide any headers."
329   :group 'gnus-article-hiding
330   :group 'gnus-article-headers
331   :type 'boolean)
332
333 (defcustom gnus-summary-ignore-duplicates nil
334   "*If non-nil, ignore articles with identical Message-ID headers."
335   :group 'gnus-summary
336   :type 'boolean)
337
338 (defcustom gnus-single-article-buffer t
339   "*If non-nil, display all articles in the same buffer.
340 If nil, each group will get its own article buffer."
341   :group 'gnus-article-various
342   :type 'boolean)
343
344 (defcustom gnus-break-pages t
345   "*If non-nil, do page breaking on articles.
346 The page delimiter is specified by the `gnus-page-delimiter'
347 variable."
348   :group 'gnus-article-various
349   :type 'boolean)
350
351 (defcustom gnus-show-mime t
352   "*If non-nil, do mime processing of articles.
353 The articles will simply be fed to the function given by
354 `gnus-article-display-method-for-mime'."
355   :group 'gnus-article-mime
356   :type 'boolean)
357
358 (defcustom gnus-move-split-methods nil
359   "*Variable used to suggest where articles are to be moved to.
360 It uses the same syntax as the `gnus-split-methods' variable."
361   :group 'gnus-summary-mail
362   :type '(repeat (choice (list :value (fun) function)
363                          (cons :value ("" "") regexp (repeat string))
364                          (sexp :value nil))))
365
366 (defcustom gnus-unread-mark ? ;Whitespace
367   "*Mark used for unread articles."
368   :group 'gnus-summary-marks
369   :type 'character)
370
371 (defcustom gnus-ticked-mark ?!
372   "*Mark used for ticked articles."
373   :group 'gnus-summary-marks
374   :type 'character)
375
376 (defcustom gnus-dormant-mark ??
377   "*Mark used for dormant articles."
378   :group 'gnus-summary-marks
379   :type 'character)
380
381 (defcustom gnus-del-mark ?r
382   "*Mark used for del'd articles."
383   :group 'gnus-summary-marks
384   :type 'character)
385
386 (defcustom gnus-read-mark ?R
387   "*Mark used for read articles."
388   :group 'gnus-summary-marks
389   :type 'character)
390
391 (defcustom gnus-expirable-mark ?E
392   "*Mark used for expirable articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-killed-mark ?K
397   "*Mark used for killed articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-souped-mark ?F
402   "*Mark used for killed articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-kill-file-mark ?X
407   "*Mark used for articles killed by kill files."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-low-score-mark ?Y
412   "*Mark used for articles with a low score."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-catchup-mark ?C
417   "*Mark used for articles that are caught up."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-replied-mark ?A
422   "*Mark used for articles that have been replied to."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-cached-mark ?*
427   "*Mark used for articles that are in the cache."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-saved-mark ?S
432   "*Mark used for articles that have been saved to."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-ancient-mark ?O
437   "*Mark used for ancient articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-sparse-mark ?Q
442   "*Mark used for sparsely reffed articles."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-canceled-mark ?G
447   "*Mark used for canceled articles."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-duplicate-mark ?M
452   "*Mark used for duplicate articles."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-undownloaded-mark ?@
457   "*Mark used for articles that weren't downloaded."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-downloadable-mark ?%
462   "*Mark used for articles that are to be downloaded."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-unsendable-mark ?=
467   "*Mark used for articles that won't be sent."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-score-over-mark ?+
472   "*Score mark used for articles with high scores."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-score-below-mark ?-
477   "*Score mark used for articles with low scores."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-empty-thread-mark ? ;Whitespace
482   "*There is no thread under the article."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-not-empty-thread-mark ?=
487   "*There is a thread under the article."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-view-pseudo-asynchronously nil
492   "*If non-nil, Gnus will view pseudo-articles asynchronously."
493   :group 'gnus-extract-view
494   :type 'boolean)
495
496 (defcustom gnus-auto-expirable-marks
497   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
498         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
499         gnus-souped-mark gnus-duplicate-mark)
500   "*The list of marks converted into expiration if a group is auto-expirable."
501   :group 'gnus-summary
502   :type '(repeat character))
503
504 (defcustom gnus-inhibit-user-auto-expire t
505   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
506   :group 'gnus-summary
507   :type 'boolean)
508
509 (defcustom gnus-view-pseudos nil
510   "*If `automatic', pseudo-articles will be viewed automatically.
511 If `not-confirm', pseudos will be viewed automatically, and the user
512 will not be asked to confirm the command."
513   :group 'gnus-extract-view
514   :type '(choice (const :tag "off" nil)
515                  (const automatic)
516                  (const not-confirm)))
517
518 (defcustom gnus-view-pseudos-separately t
519   "*If non-nil, one pseudo-article will be created for each file to be viewed.
520 If nil, all files that use the same viewing command will be given as a
521 list of parameters to that command."
522   :group 'gnus-extract-view
523   :type 'boolean)
524
525 (defcustom gnus-insert-pseudo-articles t
526   "*If non-nil, insert pseudo-articles when decoding articles."
527   :group 'gnus-extract-view
528   :type 'boolean)
529
530 (defcustom gnus-summary-dummy-line-format
531   "  %(:                          :%) %S\n"
532   "*The format specification for the dummy roots in the summary buffer.
533 It works along the same lines as a normal formatting string,
534 with some simple extensions.
535
536 %S  The subject"
537   :group 'gnus-threading
538   :type 'string)
539
540 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
541   "*The format specification for the summary mode line.
542 It works along the same lines as a normal formatting string,
543 with some simple extensions:
544
545 %G  Group name
546 %p  Unprefixed group name
547 %A  Current article number
548 %z  Current article score
549 %V  Gnus version
550 %U  Number of unread articles in the group
551 %e  Number of unselected articles in the group
552 %Z  A string with unread/unselected article counts
553 %g  Shortish group name
554 %S  Subject of the current article
555 %u  User-defined spec
556 %s  Current score file name
557 %d  Number of dormant articles
558 %r  Number of articles that have been marked as read in this session
559 %E  Number of articles expunged by the score files"
560   :group 'gnus-summary-format
561   :type 'string)
562
563 (defcustom gnus-summary-mark-below 0
564   "*Mark all articles with a score below this variable as read.
565 This variable is local to each summary buffer and usually set by the
566 score file."
567   :group 'gnus-score-default
568   :type 'integer)
569
570 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
571   "*List of functions used for sorting articles in the summary buffer.
572 This variable is only used when not using a threaded display."
573   :group 'gnus-summary-sort
574   :type '(repeat (choice (function-item gnus-article-sort-by-number)
575                          (function-item gnus-article-sort-by-author)
576                          (function-item gnus-article-sort-by-subject)
577                          (function-item gnus-article-sort-by-date)
578                          (function-item gnus-article-sort-by-score)
579                          (function :tag "other"))))
580
581 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
582   "*List of functions used for sorting threads in the summary buffer.
583 By default, threads are sorted by article number.
584
585 Each function takes two threads and return non-nil if the first thread
586 should be sorted before the other.  If you use more than one function,
587 the primary sort function should be the last.  You should probably
588 always include `gnus-thread-sort-by-number' in the list of sorting
589 functions -- preferably first.
590
591 Ready-made functions include `gnus-thread-sort-by-number',
592 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
593 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
594 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
595   :group 'gnus-summary-sort
596   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
597                          (function-item gnus-thread-sort-by-author)
598                          (function-item gnus-thread-sort-by-subject)
599                          (function-item gnus-thread-sort-by-date)
600                          (function-item gnus-thread-sort-by-score)
601                          (function-item gnus-thread-sort-by-total-score)
602                          (function :tag "other"))))
603
604 (defcustom gnus-thread-score-function '+
605   "*Function used for calculating the total score of a thread.
606
607 The function is called with the scores of the article and each
608 subthread and should then return the score of the thread.
609
610 Some functions you can use are `+', `max', or `min'."
611   :group 'gnus-summary-sort
612   :type 'function)
613
614 (defcustom gnus-summary-expunge-below nil
615   "All articles that have a score less than this variable will be expunged.
616 This variable is local to the summary buffers."
617   :group 'gnus-score-default
618   :type '(choice (const :tag "off" nil)
619                  integer))
620
621 (defcustom gnus-thread-expunge-below nil
622   "All threads that have a total score less than this variable will be expunged.
623 See `gnus-thread-score-function' for en explanation of what a
624 \"thread score\" is.
625
626 This variable is local to the summary buffers."
627   :group 'gnus-threading
628   :group 'gnus-score-default
629   :type '(choice (const :tag "off" nil)
630                  integer))
631
632 (defcustom gnus-summary-mode-hook nil
633   "*A hook for Gnus summary mode.
634 This hook is run before any variables are set in the summary buffer."
635   :group 'gnus-summary-various
636   :type 'hook)
637
638 (defcustom gnus-summary-menu-hook nil
639   "*Hook run after the creation of the summary mode menu."
640   :group 'gnus-summary-visual
641   :type 'hook)
642
643 (defcustom gnus-summary-exit-hook nil
644   "*A hook called on exit from the summary buffer.
645 It will be called with point in the group buffer."
646   :group 'gnus-summary-exit
647   :type 'hook)
648
649 (defcustom gnus-summary-prepare-hook nil
650   "*A hook called after the summary buffer has been generated.
651 If you want to modify the summary buffer, you can use this hook."
652   :group 'gnus-summary-various
653   :type 'hook)
654
655 (defcustom gnus-summary-prepared-hook nil
656   "*A hook called as the last thing after the summary buffer has been generated."
657   :group 'gnus-summary-various
658   :type 'hook)
659
660 (defcustom gnus-summary-generate-hook nil
661   "*A hook run just before generating the summary buffer.
662 This hook is commonly used to customize threading variables and the
663 like."
664   :group 'gnus-summary-various
665   :type 'hook)
666
667 (defcustom gnus-select-group-hook nil
668   "*A hook called when a newsgroup is selected.
669
670 If you'd like to simplify subjects like the
671 `gnus-summary-next-same-subject' command does, you can use the
672 following hook:
673
674  (setq gnus-select-group-hook
675       (list
676         (lambda ()
677           (mapcar (lambda (header)
678                      (mail-header-set-subject
679                       header
680                       (gnus-simplify-subject
681                        (mail-header-subject header) 're-only)))
682                   gnus-newsgroup-headers))))"
683   :group 'gnus-group-select
684   :type 'hook)
685
686 (defcustom gnus-select-article-hook nil
687   "*A hook called when an article is selected."
688   :group 'gnus-summary-choose
689   :type 'hook)
690
691 (defcustom gnus-visual-mark-article-hook
692   (list 'gnus-highlight-selected-summary)
693   "*Hook run after selecting an article in the summary buffer.
694 It is meant to be used for highlighting the article in some way.  It
695 is not run if `gnus-visual' is nil."
696   :group 'gnus-summary-visual
697   :type 'hook)
698
699 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
700   "*A hook called before parsing the headers."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-exit-group-hook nil
705   "*A hook called when exiting (not quitting) summary mode."
706   :group 'gnus-various
707   :type 'hook)
708
709 (defcustom gnus-summary-update-hook
710   (list 'gnus-summary-highlight-line)
711   "*A hook called when a summary line is changed.
712 The hook will not be called if `gnus-visual' is nil.
713
714 The default function `gnus-summary-highlight-line' will
715 highlight the line according to the `gnus-summary-highlight'
716 variable."
717   :group 'gnus-summary-visual
718   :type 'hook)
719
720 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
721   "*A hook called when an article is selected for the first time.
722 The hook is intended to mark an article as read (or unread)
723 automatically when it is selected."
724   :group 'gnus-summary-choose
725   :type 'hook)
726
727 (defcustom gnus-group-no-more-groups-hook nil
728   "*A hook run when returning to group mode having no more (unread) groups."
729   :group 'gnus-group-select
730   :type 'hook)
731
732 (defcustom gnus-ps-print-hook nil
733   "*A hook run before ps-printing something from Gnus."
734   :group 'gnus-summary
735   :type 'hook)
736
737 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
738   "Face used for highlighting the current article in the summary buffer."
739   :group 'gnus-summary-visual
740   :type 'face)
741
742 (defcustom gnus-summary-highlight
743   '(((= mark gnus-canceled-mark)
744      . gnus-summary-cancelled-face)
745     ((and (> score default)
746           (or (= mark gnus-dormant-mark)
747               (= mark gnus-ticked-mark)))
748      . gnus-summary-high-ticked-face)
749     ((and (< score default)
750           (or (= mark gnus-dormant-mark)
751               (= mark gnus-ticked-mark)))
752      . gnus-summary-low-ticked-face)
753     ((or (= mark gnus-dormant-mark)
754          (= mark gnus-ticked-mark))
755      . gnus-summary-normal-ticked-face)
756     ((and (> score default) (= mark gnus-ancient-mark))
757      . gnus-summary-high-ancient-face)
758     ((and (< score default) (= mark gnus-ancient-mark))
759      . gnus-summary-low-ancient-face)
760     ((= mark gnus-ancient-mark)
761      . gnus-summary-normal-ancient-face)
762     ((and (> score default) (= mark gnus-unread-mark))
763      . gnus-summary-high-unread-face)
764     ((and (< score default) (= mark gnus-unread-mark))
765      . gnus-summary-low-unread-face)
766     ((= mark gnus-unread-mark)
767      . gnus-summary-normal-unread-face)
768     ((and (> score default) (memq mark (list gnus-downloadable-mark
769                                              gnus-undownloaded-mark)))
770      . gnus-summary-high-unread-face)
771     ((and (< score default) (memq mark (list gnus-downloadable-mark
772                                              gnus-undownloaded-mark)))
773      . gnus-summary-low-unread-face)
774     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
775      . gnus-summary-normal-unread-face)
776     ((> score default)
777      . gnus-summary-high-read-face)
778     ((< score default)
779      . gnus-summary-low-read-face)
780     (t
781      . gnus-summary-normal-read-face))
782   "*Controls the highlighting of summary buffer lines.
783
784 A list of (FORM . FACE) pairs.  When deciding how a a particular
785 summary line should be displayed, each form is evaluated.  The content
786 of the face field after the first true form is used.  You can change
787 how those summary lines are displayed, by editing the face field.
788
789 You can use the following variables in the FORM field.
790
791 score:   The articles score
792 default: The default article score.
793 below:   The score below which articles are automatically marked as read.
794 mark:    The articles mark."
795   :group 'gnus-summary-visual
796   :type '(repeat (cons (sexp :tag "Form" nil)
797                        face)))
798
799 (defcustom gnus-alter-header-function nil
800   "Function called to allow alteration of article header structures.
801 The function is called with one parameter, the article header vector,
802 which it may alter in any way.")
803
804 (defcustom gnus-extra-headers nil
805   "*Extra headers to parse."
806   :group 'gnus-summary
807   :type '(repeat symbol))
808
809 (defcustom gnus-ignored-from-addresses
810   (and user-mail-address (regexp-quote user-mail-address))
811   "*Regexp of From headers that may be suppressed in favor of To headers."
812   :group 'gnus-summary
813   :type 'regexp)
814
815 (defcustom gnus-group-charset-alist
816   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
817     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
818     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
819     ("^relcom\\>" koi8-r)
820     ("^\\(cz\\|hun\\|pl\\|sk\\)\\>" iso-8859-2)
821     ("^israel\\>" iso-8859-1)
822     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
823     (".*" iso-8859-1))
824   "Alist of regexps (to match group names) and default charsets to be used when reading."
825   :type '(repeat (list (regexp :tag "Group")
826                        (symbol :tag "Charset")))
827   :group 'gnus-charset)
828
829 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
830   "List of charsets that should be ignored.
831 When these charsets are used in the \"charset\" parameter, the
832 default charset will be used instead."
833   :type '(repeat symbol)
834   :group 'gnus-charset)
835
836 ;;; Internal variables
837
838 (defvar gnus-scores-exclude-files nil)
839 (defvar gnus-page-broken nil)
840 (defvar gnus-inhibit-mime-unbuttonizing nil)
841
842 (defvar gnus-original-article nil)
843 (defvar gnus-article-internal-prepare-hook nil)
844 (defvar gnus-newsgroup-process-stack nil)
845
846 (defvar gnus-thread-indent-array nil)
847 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
848
849 ;; Avoid highlighting in kill files.
850 (defvar gnus-summary-inhibit-highlight nil)
851 (defvar gnus-newsgroup-selected-overlay nil)
852 (defvar gnus-inhibit-limiting nil)
853 (defvar gnus-newsgroup-adaptive-score-file nil)
854 (defvar gnus-current-score-file nil)
855 (defvar gnus-current-move-group nil)
856 (defvar gnus-current-copy-group nil)
857 (defvar gnus-current-crosspost-group nil)
858
859 (defvar gnus-newsgroup-dependencies nil)
860 (defvar gnus-newsgroup-adaptive nil)
861 (defvar gnus-summary-display-article-function nil)
862 (defvar gnus-summary-highlight-line-function nil
863   "Function called after highlighting a summary line.")
864
865 (defvar gnus-summary-line-format-alist
866   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
867     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
868     (?s gnus-tmp-subject-or-nil ?s)
869     (?n gnus-tmp-name ?s)
870     (?A (std11-address-string
871          (car (mime-read-field 'From gnus-tmp-header))) ?s)
872     (?a (or (std11-full-name-string
873              (car (mime-read-field 'From gnus-tmp-header)))
874             gnus-tmp-from) ?s)
875     (?F gnus-tmp-from ?s)
876     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
877     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
878     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
879     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
880     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
881     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
882     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
883     (?L gnus-tmp-lines ?d)
884     (?I gnus-tmp-indentation ?s)
885     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
886     (?R gnus-tmp-replied ?c)
887     (?\[ gnus-tmp-opening-bracket ?c)
888     (?\] gnus-tmp-closing-bracket ?c)
889     (?\> (make-string gnus-tmp-level ? ) ?s)
890     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
891     (?i gnus-tmp-score ?d)
892     (?z gnus-tmp-score-char ?c)
893     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
894     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
895     (?U gnus-tmp-unread ?c)
896     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
897     (?t (gnus-summary-number-of-articles-in-thread
898          (and (boundp 'thread) (car thread)) gnus-tmp-level)
899         ?d)
900     (?e (gnus-summary-number-of-articles-in-thread
901          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
902         ?c)
903     (?u gnus-tmp-user-defined ?s)
904     (?P (gnus-pick-line-number) ?d))
905   "An alist of format specifications that can appear in summary lines,
906 and what variables they correspond with, along with the type of the
907 variable (string, integer, character, etc).")
908
909 (defvar gnus-summary-dummy-line-format-alist
910   `((?S gnus-tmp-subject ?s)
911     (?N gnus-tmp-number ?d)
912     (?u gnus-tmp-user-defined ?s)))
913
914 (defvar gnus-summary-mode-line-format-alist
915   `((?G gnus-tmp-group-name ?s)
916     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
917     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
918     (?A gnus-tmp-article-number ?d)
919     (?Z gnus-tmp-unread-and-unselected ?s)
920     (?V gnus-version ?s)
921     (?U gnus-tmp-unread-and-unticked ?d)
922     (?S gnus-tmp-subject ?s)
923     (?e gnus-tmp-unselected ?d)
924     (?u gnus-tmp-user-defined ?s)
925     (?d (length gnus-newsgroup-dormant) ?d)
926     (?t (length gnus-newsgroup-marked) ?d)
927     (?r (length gnus-newsgroup-reads) ?d)
928     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
929     (?E gnus-newsgroup-expunged-tally ?d)
930     (?s (gnus-current-score-file-nondirectory) ?s)))
931
932 (defvar gnus-last-search-regexp nil
933   "Default regexp for article search command.")
934
935 (defvar gnus-last-shell-command nil
936   "Default shell command on article.")
937
938 (defvar gnus-newsgroup-begin nil)
939 (defvar gnus-newsgroup-end nil)
940 (defvar gnus-newsgroup-last-rmail nil)
941 (defvar gnus-newsgroup-last-mail nil)
942 (defvar gnus-newsgroup-last-folder nil)
943 (defvar gnus-newsgroup-last-file nil)
944 (defvar gnus-newsgroup-auto-expire nil)
945 (defvar gnus-newsgroup-active nil)
946
947 (defvar gnus-newsgroup-data nil)
948 (defvar gnus-newsgroup-data-reverse nil)
949 (defvar gnus-newsgroup-limit nil)
950 (defvar gnus-newsgroup-limits nil)
951
952 (defvar gnus-newsgroup-unreads nil
953   "List of unread articles in the current newsgroup.")
954
955 (defvar gnus-newsgroup-unselected nil
956   "List of unselected unread articles in the current newsgroup.")
957
958 (defvar gnus-newsgroup-reads nil
959   "Alist of read articles and article marks in the current newsgroup.")
960
961 (defvar gnus-newsgroup-expunged-tally nil)
962
963 (defvar gnus-newsgroup-marked nil
964   "List of ticked articles in the current newsgroup (a subset of unread art).")
965
966 (defvar gnus-newsgroup-killed nil
967   "List of ranges of articles that have been through the scoring process.")
968
969 (defvar gnus-newsgroup-cached nil
970   "List of articles that come from the article cache.")
971
972 (defvar gnus-newsgroup-saved nil
973   "List of articles that have been saved.")
974
975 (defvar gnus-newsgroup-kill-headers nil)
976
977 (defvar gnus-newsgroup-replied nil
978   "List of articles that have been replied to in the current newsgroup.")
979
980 (defvar gnus-newsgroup-expirable nil
981   "List of articles in the current newsgroup that can be expired.")
982
983 (defvar gnus-newsgroup-processable nil
984   "List of articles in the current newsgroup that can be processed.")
985
986 (defvar gnus-newsgroup-downloadable nil
987   "List of articles in the current newsgroup that can be processed.")
988
989 (defvar gnus-newsgroup-undownloaded nil
990   "List of articles in the current newsgroup that haven't been downloaded..")
991
992 (defvar gnus-newsgroup-unsendable nil
993   "List of articles in the current newsgroup that won't be sent.")
994
995 (defvar gnus-newsgroup-bookmarks nil
996   "List of articles in the current newsgroup that have bookmarks.")
997
998 (defvar gnus-newsgroup-dormant nil
999   "List of dormant articles in the current newsgroup.")
1000
1001 (defvar gnus-newsgroup-scored nil
1002   "List of scored articles in the current newsgroup.")
1003
1004 (defvar gnus-newsgroup-headers nil
1005   "List of article headers in the current newsgroup.")
1006
1007 (defvar gnus-newsgroup-threads nil)
1008
1009 (defvar gnus-newsgroup-prepared nil
1010   "Whether the current group has been prepared properly.")
1011
1012 (defvar gnus-newsgroup-ancient nil
1013   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1014
1015 (defvar gnus-newsgroup-sparse nil)
1016
1017 (defvar gnus-current-article nil)
1018 (defvar gnus-article-current nil)
1019 (defvar gnus-current-headers nil)
1020 (defvar gnus-have-all-headers nil)
1021 (defvar gnus-last-article nil)
1022 (defvar gnus-newsgroup-history nil)
1023 (defvar gnus-newsgroup-charset nil)
1024
1025 (defconst gnus-summary-local-variables
1026   '(gnus-newsgroup-name
1027     gnus-newsgroup-begin gnus-newsgroup-end
1028     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1029     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1030     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1031     gnus-newsgroup-unselected gnus-newsgroup-marked
1032     gnus-newsgroup-reads gnus-newsgroup-saved
1033     gnus-newsgroup-replied gnus-newsgroup-expirable
1034     gnus-newsgroup-processable gnus-newsgroup-killed
1035     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1036     gnus-newsgroup-unsendable
1037     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1038     gnus-newsgroup-headers gnus-newsgroup-threads
1039     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1040     gnus-current-article gnus-current-headers gnus-have-all-headers
1041     gnus-last-article gnus-article-internal-prepare-hook
1042     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1043     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1044     gnus-thread-expunge-below
1045     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1046     (gnus-summary-mark-below . global)
1047     gnus-newsgroup-active gnus-scores-exclude-files
1048     gnus-newsgroup-history gnus-newsgroup-ancient
1049     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1050     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1051     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1052     (gnus-newsgroup-expunged-tally . 0)
1053     gnus-cache-removable-articles gnus-newsgroup-cached
1054     gnus-newsgroup-data gnus-newsgroup-data-reverse
1055     gnus-newsgroup-limit gnus-newsgroup-limits
1056     gnus-newsgroup-charset)
1057   "Variables that are buffer-local to the summary buffers.")
1058
1059 ;; Byte-compiler warning.
1060 (defvar gnus-article-mode-map)
1061
1062 ;; Subject simplification.
1063
1064 (defun gnus-simplify-whitespace (str)
1065   "Remove excessive whitespace."
1066   (let ((mystr str))
1067     ;; Multiple spaces.
1068     (while (string-match "[ \t][ \t]+" mystr)
1069       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1070                           " "
1071                           (substring mystr (match-end 0)))))
1072     ;; Leading spaces.
1073     (when (string-match "^[ \t]+" mystr)
1074       (setq mystr (substring mystr (match-end 0))))
1075     ;; Trailing spaces.
1076     (when (string-match "[ \t]+$" mystr)
1077       (setq mystr (substring mystr 0 (match-beginning 0))))
1078     mystr))
1079
1080 (defsubst gnus-simplify-subject-re (subject)
1081   "Remove \"Re:\" from subject lines."
1082   (if (string-match "^[Rr][Ee]: *" subject)
1083       (substring subject (match-end 0))
1084     subject))
1085
1086 (defun gnus-simplify-subject (subject &optional re-only)
1087   "Remove `Re:' and words in parentheses.
1088 If RE-ONLY is non-nil, strip leading `Re:'s only."
1089   (let ((case-fold-search t))           ;Ignore case.
1090     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1091     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1092       (setq subject (substring subject (match-end 0))))
1093     ;; Remove uninteresting prefixes.
1094     (when (and (not re-only)
1095                gnus-simplify-ignored-prefixes
1096                (string-match gnus-simplify-ignored-prefixes subject))
1097       (setq subject (substring subject (match-end 0))))
1098     ;; Remove words in parentheses from end.
1099     (unless re-only
1100       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1101         (setq subject (substring subject 0 (match-beginning 0)))))
1102     ;; Return subject string.
1103     subject))
1104
1105 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1106 ;; all whitespace.
1107 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1108   (goto-char (point-min))
1109   (while (re-search-forward regexp nil t)
1110       (replace-match (or newtext ""))))
1111
1112 (defun gnus-simplify-buffer-fuzzy ()
1113   "Simplify string in the buffer fuzzily.
1114 The string in the accessible portion of the current buffer is simplified.
1115 It is assumed to be a single-line subject.
1116 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1117 matter is removed.  Additional things can be deleted by setting
1118 gnus-simplify-subject-fuzzy-regexp."
1119   (let ((case-fold-search t)
1120         (modified-tick))
1121     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1122
1123     (while (not (eq modified-tick (buffer-modified-tick)))
1124       (setq modified-tick (buffer-modified-tick))
1125       (cond
1126        ((listp gnus-simplify-subject-fuzzy-regexp)
1127         (mapcar 'gnus-simplify-buffer-fuzzy-step
1128                 gnus-simplify-subject-fuzzy-regexp))
1129        (gnus-simplify-subject-fuzzy-regexp
1130         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1131       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1132       (gnus-simplify-buffer-fuzzy-step
1133        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1134       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1135
1136     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1137     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1138     (gnus-simplify-buffer-fuzzy-step " $")
1139     (gnus-simplify-buffer-fuzzy-step "^ +")))
1140
1141 (defun gnus-simplify-subject-fuzzy (subject)
1142   "Simplify a subject string fuzzily.
1143 See `gnus-simplify-buffer-fuzzy' for details."
1144   (save-excursion
1145     (gnus-set-work-buffer)
1146     (let ((case-fold-search t))
1147       ;; Remove uninteresting prefixes.
1148       (when (and gnus-simplify-ignored-prefixes
1149                  (string-match gnus-simplify-ignored-prefixes subject))
1150         (setq subject (substring subject (match-end 0))))
1151       (insert subject)
1152       (inline (gnus-simplify-buffer-fuzzy))
1153       (buffer-string))))
1154
1155 (defsubst gnus-simplify-subject-fully (subject)
1156   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1157   (cond
1158    (gnus-simplify-subject-functions
1159     (gnus-map-function gnus-simplify-subject-functions subject))
1160    ((null gnus-summary-gather-subject-limit)
1161     (gnus-simplify-subject-re subject))
1162    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1163     (gnus-simplify-subject-fuzzy subject))
1164    ((numberp gnus-summary-gather-subject-limit)
1165     (gnus-limit-string (gnus-simplify-subject-re subject)
1166                        gnus-summary-gather-subject-limit))
1167    (t
1168     subject)))
1169
1170 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1171   "Check whether two subjects are equal.
1172 If optional argument simple-first is t, first argument is already
1173 simplified."
1174   (cond
1175    ((null simple-first)
1176     (equal (gnus-simplify-subject-fully s1)
1177            (gnus-simplify-subject-fully s2)))
1178    (t
1179     (equal s1
1180            (gnus-simplify-subject-fully s2)))))
1181
1182 (defun gnus-summary-bubble-group ()
1183   "Increase the score of the current group.
1184 This is a handy function to add to `gnus-summary-exit-hook' to
1185 increase the score of each group you read."
1186   (gnus-group-add-score gnus-newsgroup-name))
1187
1188 \f
1189 ;;;
1190 ;;; Gnus summary mode
1191 ;;;
1192
1193 (put 'gnus-summary-mode 'mode-class 'special)
1194
1195 (when t
1196   ;; Non-orthogonal keys
1197
1198   (gnus-define-keys gnus-summary-mode-map
1199     " " gnus-summary-next-page
1200     "\177" gnus-summary-prev-page
1201     [delete] gnus-summary-prev-page
1202     [backspace] gnus-summary-prev-page
1203     "\r" gnus-summary-scroll-up
1204     "\M-\r" gnus-summary-scroll-down
1205     "n" gnus-summary-next-unread-article
1206     "p" gnus-summary-prev-unread-article
1207     "N" gnus-summary-next-article
1208     "P" gnus-summary-prev-article
1209     "\M-\C-n" gnus-summary-next-same-subject
1210     "\M-\C-p" gnus-summary-prev-same-subject
1211     "\M-n" gnus-summary-next-unread-subject
1212     "\M-p" gnus-summary-prev-unread-subject
1213     "." gnus-summary-first-unread-article
1214     "," gnus-summary-best-unread-article
1215     "\M-s" gnus-summary-search-article-forward
1216     "\M-r" gnus-summary-search-article-backward
1217     "<" gnus-summary-beginning-of-article
1218     ">" gnus-summary-end-of-article
1219     "j" gnus-summary-goto-article
1220     "^" gnus-summary-refer-parent-article
1221     "\M-^" gnus-summary-refer-article
1222     "u" gnus-summary-tick-article-forward
1223     "!" gnus-summary-tick-article-forward
1224     "U" gnus-summary-tick-article-backward
1225     "d" gnus-summary-mark-as-read-forward
1226     "D" gnus-summary-mark-as-read-backward
1227     "E" gnus-summary-mark-as-expirable
1228     "\M-u" gnus-summary-clear-mark-forward
1229     "\M-U" gnus-summary-clear-mark-backward
1230     "k" gnus-summary-kill-same-subject-and-select
1231     "\C-k" gnus-summary-kill-same-subject
1232     "\M-\C-k" gnus-summary-kill-thread
1233     "\M-\C-l" gnus-summary-lower-thread
1234     "e" gnus-summary-edit-article
1235     "#" gnus-summary-mark-as-processable
1236     "\M-#" gnus-summary-unmark-as-processable
1237     "\M-\C-t" gnus-summary-toggle-threads
1238     "\M-\C-s" gnus-summary-show-thread
1239     "\M-\C-h" gnus-summary-hide-thread
1240     "\M-\C-f" gnus-summary-next-thread
1241     "\M-\C-b" gnus-summary-prev-thread
1242     "\M-\C-u" gnus-summary-up-thread
1243     "\M-\C-d" gnus-summary-down-thread
1244     "&" gnus-summary-execute-command
1245     "c" gnus-summary-catchup-and-exit
1246     "\C-w" gnus-summary-mark-region-as-read
1247     "\C-t" gnus-summary-toggle-truncation
1248     "?" gnus-summary-mark-as-dormant
1249     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1250     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1251     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1252     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1253     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1254     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1255     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1256     "=" gnus-summary-expand-window
1257     "\C-x\C-s" gnus-summary-reselect-current-group
1258     "\M-g" gnus-summary-rescan-group
1259     "w" gnus-summary-stop-page-breaking
1260     "\C-c\C-r" gnus-summary-caesar-message
1261     "\M-t" gnus-summary-toggle-mime
1262     "f" gnus-summary-followup
1263     "F" gnus-summary-followup-with-original
1264     "C" gnus-summary-cancel-article
1265     "r" gnus-summary-reply
1266     "R" gnus-summary-reply-with-original
1267     "\C-c\C-f" gnus-summary-mail-forward
1268     "o" gnus-summary-save-article
1269     "\C-o" gnus-summary-save-article-mail
1270     "|" gnus-summary-pipe-output
1271     "\M-k" gnus-summary-edit-local-kill
1272     "\M-K" gnus-summary-edit-global-kill
1273     ;; "V" gnus-version
1274     "\C-c\C-d" gnus-summary-describe-group
1275     "q" gnus-summary-exit
1276     "Q" gnus-summary-exit-no-update
1277     "\C-c\C-i" gnus-info-find-node
1278     gnus-mouse-2 gnus-mouse-pick-article
1279     "m" gnus-summary-mail-other-window
1280     "a" gnus-summary-post-news
1281     "x" gnus-summary-limit-to-unread
1282     "s" gnus-summary-isearch-article
1283     "t" gnus-summary-toggle-header
1284     "g" gnus-summary-show-article
1285     "l" gnus-summary-goto-last-article
1286     "v" gnus-summary-preview-mime-message
1287     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1288     "\C-d" gnus-summary-enter-digest-group
1289     "\M-\C-d" gnus-summary-read-document
1290     "\M-\C-e" gnus-summary-edit-parameters
1291     "\M-\C-g" gnus-summary-customize-parameters
1292     "\C-c\C-b" gnus-bug
1293     "*" gnus-cache-enter-article
1294     "\M-*" gnus-cache-remove-article
1295     "\M-&" gnus-summary-universal-argument
1296     "\C-l" gnus-recenter
1297     "I" gnus-summary-increase-score
1298     "L" gnus-summary-lower-score
1299     "\M-i" gnus-symbolic-argument
1300     "h" gnus-summary-select-article-buffer
1301
1302     "V" gnus-summary-score-map
1303     "X" gnus-uu-extract-map
1304     "S" gnus-summary-send-map)
1305
1306   ;; Sort of orthogonal keymap
1307   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1308     "t" gnus-summary-tick-article-forward
1309     "!" gnus-summary-tick-article-forward
1310     "d" gnus-summary-mark-as-read-forward
1311     "r" gnus-summary-mark-as-read-forward
1312     "c" gnus-summary-clear-mark-forward
1313     " " gnus-summary-clear-mark-forward
1314     "e" gnus-summary-mark-as-expirable
1315     "x" gnus-summary-mark-as-expirable
1316     "?" gnus-summary-mark-as-dormant
1317     "b" gnus-summary-set-bookmark
1318     "B" gnus-summary-remove-bookmark
1319     "#" gnus-summary-mark-as-processable
1320     "\M-#" gnus-summary-unmark-as-processable
1321     "S" gnus-summary-limit-include-expunged
1322     "C" gnus-summary-catchup
1323     "H" gnus-summary-catchup-to-here
1324     "\C-c" gnus-summary-catchup-all
1325     "k" gnus-summary-kill-same-subject-and-select
1326     "K" gnus-summary-kill-same-subject
1327     "P" gnus-uu-mark-map)
1328
1329   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1330     "c" gnus-summary-clear-above
1331     "u" gnus-summary-tick-above
1332     "m" gnus-summary-mark-above
1333     "k" gnus-summary-kill-below)
1334
1335   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1336     "/" gnus-summary-limit-to-subject
1337     "n" gnus-summary-limit-to-articles
1338     "w" gnus-summary-pop-limit
1339     "s" gnus-summary-limit-to-subject
1340     "a" gnus-summary-limit-to-author
1341     "u" gnus-summary-limit-to-unread
1342     "m" gnus-summary-limit-to-marks
1343     "v" gnus-summary-limit-to-score
1344     "*" gnus-summary-limit-include-cached
1345     "D" gnus-summary-limit-include-dormant
1346     "T" gnus-summary-limit-include-thread
1347     "d" gnus-summary-limit-exclude-dormant
1348     "t" gnus-summary-limit-to-age
1349     "E" gnus-summary-limit-include-expunged
1350     "c" gnus-summary-limit-exclude-childless-dormant
1351     "C" gnus-summary-limit-mark-excluded-as-read)
1352
1353   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1354     "n" gnus-summary-next-unread-article
1355     "p" gnus-summary-prev-unread-article
1356     "N" gnus-summary-next-article
1357     "P" gnus-summary-prev-article
1358     "\C-n" gnus-summary-next-same-subject
1359     "\C-p" gnus-summary-prev-same-subject
1360     "\M-n" gnus-summary-next-unread-subject
1361     "\M-p" gnus-summary-prev-unread-subject
1362     "f" gnus-summary-first-unread-article
1363     "b" gnus-summary-best-unread-article
1364     "j" gnus-summary-goto-article
1365     "g" gnus-summary-goto-subject
1366     "l" gnus-summary-goto-last-article
1367     "o" gnus-summary-pop-article)
1368
1369   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1370     "k" gnus-summary-kill-thread
1371     "l" gnus-summary-lower-thread
1372     "i" gnus-summary-raise-thread
1373     "T" gnus-summary-toggle-threads
1374     "t" gnus-summary-rethread-current
1375     "^" gnus-summary-reparent-thread
1376     "s" gnus-summary-show-thread
1377     "S" gnus-summary-show-all-threads
1378     "h" gnus-summary-hide-thread
1379     "H" gnus-summary-hide-all-threads
1380     "n" gnus-summary-next-thread
1381     "p" gnus-summary-prev-thread
1382     "u" gnus-summary-up-thread
1383     "o" gnus-summary-top-thread
1384     "d" gnus-summary-down-thread
1385     "#" gnus-uu-mark-thread
1386     "\M-#" gnus-uu-unmark-thread)
1387
1388   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1389     "g" gnus-summary-prepare
1390     "c" gnus-summary-insert-cached-articles)
1391
1392   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1393     "c" gnus-summary-catchup-and-exit
1394     "C" gnus-summary-catchup-all-and-exit
1395     "E" gnus-summary-exit-no-update
1396     "Q" gnus-summary-exit
1397     "Z" gnus-summary-exit
1398     "n" gnus-summary-catchup-and-goto-next-group
1399     "R" gnus-summary-reselect-current-group
1400     "G" gnus-summary-rescan-group
1401     "N" gnus-summary-next-group
1402     "s" gnus-summary-save-newsrc
1403     "P" gnus-summary-prev-group)
1404
1405   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1406     " " gnus-summary-next-page
1407     "n" gnus-summary-next-page
1408     "\177" gnus-summary-prev-page
1409     [delete] gnus-summary-prev-page
1410     "p" gnus-summary-prev-page
1411     "\r" gnus-summary-scroll-up
1412     "\M-\r" gnus-summary-scroll-down
1413     "<" gnus-summary-beginning-of-article
1414     ">" gnus-summary-end-of-article
1415     "b" gnus-summary-beginning-of-article
1416     "e" gnus-summary-end-of-article
1417     "^" gnus-summary-refer-parent-article
1418     "r" gnus-summary-refer-parent-article
1419     "R" gnus-summary-refer-references
1420     "T" gnus-summary-refer-thread
1421     "g" gnus-summary-show-article
1422     "s" gnus-summary-isearch-article
1423     "P" gnus-summary-print-article)
1424
1425   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1426     "b" gnus-article-add-buttons
1427     "B" gnus-article-add-buttons-to-head
1428     "o" gnus-article-treat-overstrike
1429     "e" gnus-article-emphasize
1430     "w" gnus-article-fill-cited-article
1431     "Q" gnus-article-fill-long-lines
1432     "C" gnus-article-capitalize-sentences
1433     "c" gnus-article-remove-cr
1434     "f" gnus-article-display-x-face
1435     "l" gnus-summary-stop-page-breaking
1436     "r" gnus-summary-caesar-message
1437     "t" gnus-article-hide-headers
1438     "v" gnus-summary-verbose-headers
1439     "m" gnus-summary-toggle-mime
1440     "h" gnus-article-treat-html
1441     "d" gnus-article-treat-dumbquotes)
1442
1443   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1444     "a" gnus-article-hide
1445     "h" gnus-article-hide-headers
1446     "b" gnus-article-hide-boring-headers
1447     "s" gnus-article-hide-signature
1448     "c" gnus-article-hide-citation
1449     "C" gnus-article-hide-citation-in-followups
1450     "p" gnus-article-hide-pgp
1451     "B" gnus-article-strip-banner
1452     "P" gnus-article-hide-pem
1453     "\C-c" gnus-article-hide-citation-maybe)
1454
1455   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1456     "a" gnus-article-highlight
1457     "h" gnus-article-highlight-headers
1458     "c" gnus-article-highlight-citation
1459     "s" gnus-article-highlight-signature)
1460
1461   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1462     "z" gnus-article-date-ut
1463     "u" gnus-article-date-ut
1464     "l" gnus-article-date-local
1465     "e" gnus-article-date-lapsed
1466     "o" gnus-article-date-original
1467     "i" gnus-article-date-iso8601
1468     "s" gnus-article-date-user)
1469
1470   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1471     "t" gnus-article-remove-trailing-blank-lines
1472     "l" gnus-article-strip-leading-blank-lines
1473     "m" gnus-article-strip-multiple-blank-lines
1474     "a" gnus-article-strip-blank-lines
1475     "A" gnus-article-strip-all-blank-lines
1476     "s" gnus-article-strip-leading-space
1477     "e" gnus-article-strip-trailing-space)
1478
1479   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1480     "v" gnus-version
1481     "f" gnus-summary-fetch-faq
1482     "d" gnus-summary-describe-group
1483     "h" gnus-summary-describe-briefly
1484     "i" gnus-info-find-node)
1485
1486   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1487     "e" gnus-summary-expire-articles
1488     "\M-\C-e" gnus-summary-expire-articles-now
1489     "\177" gnus-summary-delete-article
1490     [delete] gnus-summary-delete-article
1491     "m" gnus-summary-move-article
1492     "r" gnus-summary-respool-article
1493     "w" gnus-summary-edit-article
1494     "c" gnus-summary-copy-article
1495     "B" gnus-summary-crosspost-article
1496     "q" gnus-summary-respool-query
1497     "t" gnus-summary-respool-trace
1498     "i" gnus-summary-import-article
1499     "p" gnus-summary-article-posted-p)
1500
1501   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1502     "o" gnus-summary-save-article
1503     "m" gnus-summary-save-article-mail
1504     "F" gnus-summary-write-article-file
1505     "r" gnus-summary-save-article-rmail
1506     "f" gnus-summary-save-article-file
1507     "b" gnus-summary-save-article-body-file
1508     "h" gnus-summary-save-article-folder
1509     "v" gnus-summary-save-article-vm
1510     "p" gnus-summary-pipe-output
1511     "s" gnus-soup-add-article)
1512
1513   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1514     "b" gnus-summary-display-buttonized
1515     "m" gnus-summary-repair-multipart
1516     "v" gnus-article-view-part
1517     "o" gnus-article-save-part
1518     "c" gnus-article-copy-part
1519     "e" gnus-article-externalize-part
1520     "i" gnus-article-inline-part
1521     "|" gnus-article-pipe-part)
1522   )
1523
1524 (defun gnus-summary-make-menu-bar ()
1525   (gnus-turn-off-edit-menu 'summary)
1526
1527   (unless (boundp 'gnus-summary-misc-menu)
1528
1529     (easy-menu-define
1530      gnus-summary-kill-menu gnus-summary-mode-map ""
1531      (cons
1532       "Score"
1533       (nconc
1534        (list
1535         ["Enter score..." gnus-summary-score-entry t]
1536         ["Customize" gnus-score-customize t])
1537        (gnus-make-score-map 'increase)
1538        (gnus-make-score-map 'lower)
1539        '(("Mark"
1540           ["Kill below" gnus-summary-kill-below t]
1541           ["Mark above" gnus-summary-mark-above t]
1542           ["Tick above" gnus-summary-tick-above t]
1543           ["Clear above" gnus-summary-clear-above t])
1544          ["Current score" gnus-summary-current-score t]
1545          ["Set score" gnus-summary-set-score t]
1546          ["Switch current score file..." gnus-score-change-score-file t]
1547          ["Set mark below..." gnus-score-set-mark-below t]
1548          ["Set expunge below..." gnus-score-set-expunge-below t]
1549          ["Edit current score file" gnus-score-edit-current-scores t]
1550          ["Edit score file" gnus-score-edit-file t]
1551          ["Trace score" gnus-score-find-trace t]
1552          ["Find words" gnus-score-find-favourite-words t]
1553          ["Rescore buffer" gnus-summary-rescore t]
1554          ["Increase score..." gnus-summary-increase-score t]
1555          ["Lower score..." gnus-summary-lower-score t]))))
1556
1557     ;; Define both the Article menu in the summary buffer and the equivalent
1558     ;; Commands menu in the article buffer here for consistency.
1559     (let ((innards
1560            '(("Hide"
1561               ["All" gnus-article-hide t]
1562               ["Headers" gnus-article-hide-headers t]
1563               ["Signature" gnus-article-hide-signature t]
1564               ["Citation" gnus-article-hide-citation t]
1565               ["PGP" gnus-article-hide-pgp t]
1566               ["Banner" gnus-article-strip-banner t]
1567               ["Boring headers" gnus-article-hide-boring-headers t])
1568              ("Highlight"
1569               ["All" gnus-article-highlight t]
1570               ["Headers" gnus-article-highlight-headers t]
1571               ["Signature" gnus-article-highlight-signature t]
1572               ["Citation" gnus-article-highlight-citation t])
1573              ("Date"
1574               ["Local" gnus-article-date-local t]
1575               ["ISO8601" gnus-article-date-iso8601 t]
1576               ["UT" gnus-article-date-ut t]
1577               ["Original" gnus-article-date-original t]
1578               ["Lapsed" gnus-article-date-lapsed t]
1579               ["User-defined" gnus-article-date-user t])
1580              ("Washing"
1581               ("Remove Blanks"
1582                ["Leading" gnus-article-strip-leading-blank-lines t]
1583                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1584                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1585                ["All of the above" gnus-article-strip-blank-lines t]
1586                ["All" gnus-article-strip-all-blank-lines t]
1587                ["Leading space" gnus-article-strip-leading-space t]
1588                ["Trailing space" gnus-article-strip-trailing-space t])
1589               ["Overstrike" gnus-article-treat-overstrike t]
1590               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1591               ["Emphasis" gnus-article-emphasize t]
1592               ["Word wrap" gnus-article-fill-cited-article t]
1593               ["Fill long lines" gnus-article-fill-long-lines t]
1594               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1595               ["CR" gnus-article-remove-cr t]
1596               ["Show X-Face" gnus-article-display-x-face t]
1597               ["UnHTMLize" gnus-article-treat-html t]
1598               ["Rot 13" gnus-summary-caesar-message t]
1599               ["Unix pipe" gnus-summary-pipe-message t]
1600               ["Add buttons" gnus-article-add-buttons t]
1601               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1602               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1603               ["Toggle MIME" gnus-summary-toggle-mime t]
1604               ["Verbose header" gnus-summary-verbose-headers t]
1605               ["Toggle header" gnus-summary-toggle-header t])
1606              ("Output"
1607               ["Save in default format" gnus-summary-save-article t]
1608               ["Save in file" gnus-summary-save-article-file t]
1609               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1610               ["Save in MH folder" gnus-summary-save-article-folder t]
1611               ["Save in VM folder" gnus-summary-save-article-vm t]
1612               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1613               ["Save body in file" gnus-summary-save-article-body-file t]
1614               ["Pipe through a filter" gnus-summary-pipe-output t]
1615               ["Add to SOUP packet" gnus-soup-add-article t]
1616               ["Print" gnus-summary-print-article t])
1617              ("Backend"
1618               ["Respool article..." gnus-summary-respool-article t]
1619               ["Move article..." gnus-summary-move-article
1620                (gnus-check-backend-function
1621                 'request-move-article gnus-newsgroup-name)]
1622               ["Copy article..." gnus-summary-copy-article t]
1623               ["Crosspost article..." gnus-summary-crosspost-article
1624                (gnus-check-backend-function
1625                 'request-replace-article gnus-newsgroup-name)]
1626               ["Import file..." gnus-summary-import-article t]
1627               ["Check if posted" gnus-summary-article-posted-p t]
1628               ["Edit article" gnus-summary-edit-article
1629                (not (gnus-group-read-only-p))]
1630               ["Delete article" gnus-summary-delete-article
1631                (gnus-check-backend-function
1632                 'request-expire-articles gnus-newsgroup-name)]
1633               ["Query respool" gnus-summary-respool-query t]
1634               ["Trace respool" gnus-summary-respool-trace t]
1635               ["Delete expirable articles" gnus-summary-expire-articles-now
1636                (gnus-check-backend-function
1637                 'request-expire-articles gnus-newsgroup-name)])
1638              ("Extract"
1639               ["Uudecode" gnus-uu-decode-uu t]
1640               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1641               ["Unshar" gnus-uu-decode-unshar t]
1642               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1643               ["Save" gnus-uu-decode-save t]
1644               ["Binhex" gnus-uu-decode-binhex t]
1645               ["Postscript" gnus-uu-decode-postscript t])
1646              ("Cache"
1647               ["Enter article" gnus-cache-enter-article t]
1648               ["Remove article" gnus-cache-remove-article t])
1649              ["Select article buffer" gnus-summary-select-article-buffer t]
1650              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1651              ["Isearch article..." gnus-summary-isearch-article t]
1652              ["Beginning of the article" gnus-summary-beginning-of-article t]
1653              ["End of the article" gnus-summary-end-of-article t]
1654              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1655              ["Fetch referenced articles" gnus-summary-refer-references t]
1656              ["Fetch current thread" gnus-summary-refer-thread t]
1657              ["Fetch article with id..." gnus-summary-refer-article t]
1658              ["Redisplay" gnus-summary-show-article t])))
1659       (easy-menu-define
1660        gnus-summary-article-menu gnus-summary-mode-map ""
1661        (cons "Article" innards))
1662
1663       (easy-menu-define
1664        gnus-article-commands-menu gnus-article-mode-map ""
1665        (cons "Commands" innards)))
1666
1667     (easy-menu-define
1668      gnus-summary-thread-menu gnus-summary-mode-map ""
1669      '("Threads"
1670        ["Toggle threading" gnus-summary-toggle-threads t]
1671        ["Hide threads" gnus-summary-hide-all-threads t]
1672        ["Show threads" gnus-summary-show-all-threads t]
1673        ["Hide thread" gnus-summary-hide-thread t]
1674        ["Show thread" gnus-summary-show-thread t]
1675        ["Go to next thread" gnus-summary-next-thread t]
1676        ["Go to previous thread" gnus-summary-prev-thread t]
1677        ["Go down thread" gnus-summary-down-thread t]
1678        ["Go up thread" gnus-summary-up-thread t]
1679        ["Top of thread" gnus-summary-top-thread t]
1680        ["Mark thread as read" gnus-summary-kill-thread t]
1681        ["Lower thread score" gnus-summary-lower-thread t]
1682        ["Raise thread score" gnus-summary-raise-thread t]
1683        ["Rethread current" gnus-summary-rethread-current t]
1684        ))
1685
1686     (easy-menu-define
1687      gnus-summary-post-menu gnus-summary-mode-map ""
1688      '("Post"
1689        ["Post an article" gnus-summary-post-news t]
1690        ["Followup" gnus-summary-followup t]
1691        ["Followup and yank" gnus-summary-followup-with-original t]
1692        ["Supersede article" gnus-summary-supersede-article t]
1693        ["Cancel article" gnus-summary-cancel-article t]
1694        ["Reply" gnus-summary-reply t]
1695        ["Reply and yank" gnus-summary-reply-with-original t]
1696        ["Wide reply" gnus-summary-wide-reply t]
1697        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1698        ["Mail forward" gnus-summary-mail-forward t]
1699        ["Post forward" gnus-summary-post-forward t]
1700        ["Digest and mail" gnus-summary-mail-digest t]
1701        ["Digest and post" gnus-summary-post-digest t]
1702        ["Resend message" gnus-summary-resend-message t]
1703        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1704        ["Send a mail" gnus-summary-mail-other-window t]
1705        ["Uuencode and post" gnus-uu-post-news t]
1706        ["Followup via news" gnus-summary-followup-to-mail t]
1707        ["Followup via news and yank"
1708         gnus-summary-followup-to-mail-with-original t]
1709        ;;("Draft"
1710        ;;["Send" gnus-summary-send-draft t]
1711        ;;["Send bounced" gnus-resend-bounced-mail t])
1712        ))
1713
1714     (easy-menu-define
1715      gnus-summary-misc-menu gnus-summary-mode-map ""
1716      '("Misc"
1717        ("Mark Read"
1718         ["Mark as read" gnus-summary-mark-as-read-forward t]
1719         ["Mark same subject and select"
1720          gnus-summary-kill-same-subject-and-select t]
1721         ["Mark same subject" gnus-summary-kill-same-subject t]
1722         ["Catchup" gnus-summary-catchup t]
1723         ["Catchup all" gnus-summary-catchup-all t]
1724         ["Catchup to here" gnus-summary-catchup-to-here t]
1725         ["Catchup region" gnus-summary-mark-region-as-read t]
1726         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1727        ("Mark Various"
1728         ["Tick" gnus-summary-tick-article-forward t]
1729         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1730         ["Remove marks" gnus-summary-clear-mark-forward t]
1731         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1732         ["Set bookmark" gnus-summary-set-bookmark t]
1733         ["Remove bookmark" gnus-summary-remove-bookmark t])
1734        ("Mark Limit"
1735         ["Marks..." gnus-summary-limit-to-marks t]
1736         ["Subject..." gnus-summary-limit-to-subject t]
1737         ["Author..." gnus-summary-limit-to-author t]
1738         ["Age..." gnus-summary-limit-to-age t]
1739         ["Score" gnus-summary-limit-to-score t]
1740         ["Unread" gnus-summary-limit-to-unread t]
1741         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1742         ["Articles" gnus-summary-limit-to-articles t]
1743         ["Pop limit" gnus-summary-pop-limit t]
1744         ["Show dormant" gnus-summary-limit-include-dormant t]
1745         ["Hide childless dormant"
1746          gnus-summary-limit-exclude-childless-dormant t]
1747         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1748         ["Show expunged" gnus-summary-show-all-expunged t])
1749        ("Process Mark"
1750         ["Set mark" gnus-summary-mark-as-processable t]
1751         ["Remove mark" gnus-summary-unmark-as-processable t]
1752         ["Remove all marks" gnus-summary-unmark-all-processable t]
1753         ["Mark above" gnus-uu-mark-over t]
1754         ["Mark series" gnus-uu-mark-series t]
1755         ["Mark region" gnus-uu-mark-region t]
1756         ["Unmark region" gnus-uu-unmark-region t]
1757         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1758         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1759         ["Mark all" gnus-uu-mark-all t]
1760         ["Mark buffer" gnus-uu-mark-buffer t]
1761         ["Mark sparse" gnus-uu-mark-sparse t]
1762         ["Mark thread" gnus-uu-mark-thread t]
1763         ["Unmark thread" gnus-uu-unmark-thread t]
1764         ("Process Mark Sets"
1765          ["Kill" gnus-summary-kill-process-mark t]
1766          ["Yank" gnus-summary-yank-process-mark
1767           gnus-newsgroup-process-stack]
1768          ["Save" gnus-summary-save-process-mark t]))
1769        ("Scroll article"
1770         ["Page forward" gnus-summary-next-page t]
1771         ["Page backward" gnus-summary-prev-page t]
1772         ["Line forward" gnus-summary-scroll-up t])
1773        ("Move"
1774         ["Next unread article" gnus-summary-next-unread-article t]
1775         ["Previous unread article" gnus-summary-prev-unread-article t]
1776         ["Next article" gnus-summary-next-article t]
1777         ["Previous article" gnus-summary-prev-article t]
1778         ["Next unread subject" gnus-summary-next-unread-subject t]
1779         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1780         ["Next article same subject" gnus-summary-next-same-subject t]
1781         ["Previous article same subject" gnus-summary-prev-same-subject t]
1782         ["First unread article" gnus-summary-first-unread-article t]
1783         ["Best unread article" gnus-summary-best-unread-article t]
1784         ["Go to subject number..." gnus-summary-goto-subject t]
1785         ["Go to article number..." gnus-summary-goto-article t]
1786         ["Go to the last article" gnus-summary-goto-last-article t]
1787         ["Pop article off history" gnus-summary-pop-article t])
1788        ("Sort"
1789         ["Sort by number" gnus-summary-sort-by-number t]
1790         ["Sort by author" gnus-summary-sort-by-author t]
1791         ["Sort by subject" gnus-summary-sort-by-subject t]
1792         ["Sort by date" gnus-summary-sort-by-date t]
1793         ["Sort by score" gnus-summary-sort-by-score t]
1794         ["Sort by lines" gnus-summary-sort-by-lines t])
1795        ("Help"
1796         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1797         ["Describe group" gnus-summary-describe-group t]
1798         ["Read manual" gnus-info-find-node t])
1799        ("Modes"
1800         ["Pick and read" gnus-pick-mode t]
1801         ["Binary" gnus-binary-mode t])
1802        ("Regeneration"
1803         ["Regenerate" gnus-summary-prepare t]
1804         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1805         ["Toggle threading" gnus-summary-toggle-threads t])
1806        ["Filter articles..." gnus-summary-execute-command t]
1807        ["Run command on subjects..." gnus-summary-universal-argument t]
1808        ["Search articles forward..." gnus-summary-search-article-forward t]
1809        ["Search articles backward..." gnus-summary-search-article-backward t]
1810        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1811        ["Expand window" gnus-summary-expand-window t]
1812        ["Expire expirable articles" gnus-summary-expire-articles
1813         (gnus-check-backend-function
1814          'request-expire-articles gnus-newsgroup-name)]
1815        ["Edit local kill file" gnus-summary-edit-local-kill t]
1816        ["Edit main kill file" gnus-summary-edit-global-kill t]
1817        ["Edit group parameters" gnus-summary-edit-parameters t]
1818        ["Customize group parameters" gnus-summary-customize-parameters t]
1819        ["Send a bug report" gnus-bug t]
1820        ("Exit"
1821         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1822         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1823         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1824         ["Exit group" gnus-summary-exit t]
1825         ["Exit group without updating" gnus-summary-exit-no-update t]
1826         ["Exit and goto next group" gnus-summary-next-group t]
1827         ["Exit and goto prev group" gnus-summary-prev-group t]
1828         ["Reselect group" gnus-summary-reselect-current-group t]
1829         ["Rescan group" gnus-summary-rescan-group t]
1830         ["Update dribble" gnus-summary-save-newsrc t])))
1831
1832     (gnus-run-hooks 'gnus-summary-menu-hook)))
1833
1834 (defun gnus-score-set-default (var value)
1835   "A version of set that updates the GNU Emacs menu-bar."
1836   (set var value)
1837   ;; It is the message that forces the active status to be updated.
1838   (message ""))
1839
1840 (defun gnus-make-score-map (type)
1841   "Make a summary score map of type TYPE."
1842   (if t
1843       nil
1844     (let ((headers '(("author" "from" string)
1845                      ("subject" "subject" string)
1846                      ("article body" "body" string)
1847                      ("article head" "head" string)
1848                      ("xref" "xref" string)
1849                      ("extra header" "extra" string)
1850                      ("lines" "lines" number)
1851                      ("followups to author" "followup" string)))
1852           (types '((number ("less than" <)
1853                            ("greater than" >)
1854                            ("equal" =))
1855                    (string ("substring" s)
1856                            ("exact string" e)
1857                            ("fuzzy string" f)
1858                            ("regexp" r))))
1859           (perms '(("temporary" (current-time-string))
1860                    ("permanent" nil)
1861                    ("immediate" now)))
1862           header)
1863       (list
1864        (apply
1865         'nconc
1866         (list
1867          (if (eq type 'lower)
1868              "Lower score"
1869            "Increase score"))
1870         (let (outh)
1871           (while headers
1872             (setq header (car headers))
1873             (setq outh
1874                   (cons
1875                    (apply
1876                     'nconc
1877                     (list (car header))
1878                     (let ((ts (cdr (assoc (nth 2 header) types)))
1879                           outt)
1880                       (while ts
1881                         (setq outt
1882                               (cons
1883                                (apply
1884                                 'nconc
1885                                 (list (caar ts))
1886                                 (let ((ps perms)
1887                                       outp)
1888                                   (while ps
1889                                     (setq outp
1890                                           (cons
1891                                            (vector
1892                                             (caar ps)
1893                                             (list
1894                                              'gnus-summary-score-entry
1895                                              (nth 1 header)
1896                                              (if (or (string= (nth 1 header)
1897                                                               "head")
1898                                                      (string= (nth 1 header)
1899                                                               "body"))
1900                                                  ""
1901                                                (list 'gnus-summary-header
1902                                                      (nth 1 header)))
1903                                              (list 'quote (nth 1 (car ts)))
1904                                              (list 'gnus-score-default nil)
1905                                              (nth 1 (car ps))
1906                                              t)
1907                                             t)
1908                                            outp))
1909                                     (setq ps (cdr ps)))
1910                                   (list (nreverse outp))))
1911                                outt))
1912                         (setq ts (cdr ts)))
1913                       (list (nreverse outt))))
1914                    outh))
1915             (setq headers (cdr headers)))
1916           (list (nreverse outh))))))))
1917
1918 \f
1919
1920 (defun gnus-summary-mode (&optional group)
1921   "Major mode for reading articles.
1922
1923 All normal editing commands are switched off.
1924 \\<gnus-summary-mode-map>
1925 Each line in this buffer represents one article.  To read an
1926 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1927 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1928 respectively.
1929
1930 You can also post articles and send mail from this buffer.  To
1931 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1932 of an article, type `\\[gnus-summary-reply]'.
1933
1934 There are approx. one gazillion commands you can execute in this
1935 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1936
1937 The following commands are available:
1938
1939 \\{gnus-summary-mode-map}"
1940   (interactive)
1941   (when (gnus-visual-p 'summary-menu 'menu)
1942     (gnus-summary-make-menu-bar))
1943   (kill-all-local-variables)
1944   (gnus-summary-make-local-variables)
1945   (gnus-make-thread-indent-array)
1946   (gnus-simplify-mode-line)
1947   (setq major-mode 'gnus-summary-mode)
1948   (setq mode-name "Summary")
1949   (make-local-variable 'minor-mode-alist)
1950   (use-local-map gnus-summary-mode-map)
1951   (buffer-disable-undo)
1952   (setq buffer-read-only t)             ;Disable modification
1953   (setq truncate-lines t)
1954   (setq selective-display t)
1955   (setq selective-display-ellipses t)   ;Display `...'
1956   (gnus-summary-set-display-table)
1957   (gnus-set-default-directory)
1958   (setq gnus-newsgroup-name group)
1959   (make-local-variable 'gnus-summary-line-format)
1960   (make-local-variable 'gnus-summary-line-format-spec)
1961   (make-local-variable 'gnus-summary-dummy-line-format)
1962   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1963   (make-local-variable 'gnus-summary-mark-positions)
1964   (make-local-hook 'pre-command-hook)
1965   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1966   (gnus-run-hooks 'gnus-summary-mode-hook)
1967   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1968   (gnus-update-summary-mark-positions))
1969
1970 (defun gnus-summary-make-local-variables ()
1971   "Make all the local summary buffer variables."
1972   (let (global)
1973     (dolist (local gnus-summary-local-variables)
1974       (if (consp local)
1975           (progn
1976             (if (eq (cdr local) 'global)
1977                 ;; Copy the global value of the variable.
1978                 (setq global (symbol-value (car local)))
1979               ;; Use the value from the list.
1980               (setq global (eval (cdr local))))
1981             (set (make-local-variable (car local)) global))
1982         ;; Simple nil-valued local variable.
1983         (set (make-local-variable local) nil)))))
1984
1985 (defun gnus-summary-clear-local-variables ()
1986   (let ((locals gnus-summary-local-variables))
1987     (while locals
1988       (if (consp (car locals))
1989           (and (vectorp (caar locals))
1990                (set (caar locals) nil))
1991         (and (vectorp (car locals))
1992              (set (car locals) nil)))
1993       (setq locals (cdr locals)))))
1994
1995 ;; Summary data functions.
1996
1997 (defmacro gnus-data-number (data)
1998   `(car ,data))
1999
2000 (defmacro gnus-data-set-number (data number)
2001   `(setcar ,data ,number))
2002
2003 (defmacro gnus-data-mark (data)
2004   `(nth 1 ,data))
2005
2006 (defmacro gnus-data-set-mark (data mark)
2007   `(setcar (nthcdr 1 ,data) ,mark))
2008
2009 (defmacro gnus-data-pos (data)
2010   `(nth 2 ,data))
2011
2012 (defmacro gnus-data-set-pos (data pos)
2013   `(setcar (nthcdr 2 ,data) ,pos))
2014
2015 (defmacro gnus-data-header (data)
2016   `(nth 3 ,data))
2017
2018 (defmacro gnus-data-set-header (data header)
2019   `(setf (nth 3 ,data) ,header))
2020
2021 (defmacro gnus-data-level (data)
2022   `(nth 4 ,data))
2023
2024 (defmacro gnus-data-unread-p (data)
2025   `(= (nth 1 ,data) gnus-unread-mark))
2026
2027 (defmacro gnus-data-read-p (data)
2028   `(/= (nth 1 ,data) gnus-unread-mark))
2029
2030 (defmacro gnus-data-pseudo-p (data)
2031   `(consp (nth 3 ,data)))
2032
2033 (defmacro gnus-data-find (number)
2034   `(assq ,number gnus-newsgroup-data))
2035
2036 (defmacro gnus-data-find-list (number &optional data)
2037   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2038      (memq (assq ,number bdata)
2039            bdata)))
2040
2041 (defmacro gnus-data-make (number mark pos header level)
2042   `(list ,number ,mark ,pos ,header ,level))
2043
2044 (defun gnus-data-enter (after-article number mark pos header level offset)
2045   (let ((data (gnus-data-find-list after-article)))
2046     (unless data
2047       (error "No such article: %d" after-article))
2048     (setcdr data (cons (gnus-data-make number mark pos header level)
2049                        (cdr data)))
2050     (setq gnus-newsgroup-data-reverse nil)
2051     (gnus-data-update-list (cddr data) offset)))
2052
2053 (defun gnus-data-enter-list (after-article list &optional offset)
2054   (when list
2055     (let ((data (and after-article (gnus-data-find-list after-article)))
2056           (ilist list))
2057       (if (not (or data
2058                    after-article))
2059           (let ((odata gnus-newsgroup-data))
2060             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2061             (when offset
2062               (gnus-data-update-list odata offset)))
2063         ;; Find the last element in the list to be spliced into the main
2064         ;; list.
2065         (while (cdr list)
2066           (setq list (cdr list)))
2067         (if (not data)
2068             (progn
2069               (setcdr list gnus-newsgroup-data)
2070               (setq gnus-newsgroup-data ilist)
2071               (when offset
2072                 (gnus-data-update-list (cdr list) offset)))
2073           (setcdr list (cdr data))
2074           (setcdr data ilist)
2075           (when offset
2076             (gnus-data-update-list (cdr list) offset))))
2077       (setq gnus-newsgroup-data-reverse nil))))
2078
2079 (defun gnus-data-remove (article &optional offset)
2080   (let ((data gnus-newsgroup-data))
2081     (if (= (gnus-data-number (car data)) article)
2082         (progn
2083           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2084                 gnus-newsgroup-data-reverse nil)
2085           (when offset
2086             (gnus-data-update-list gnus-newsgroup-data offset)))
2087       (while (cdr data)
2088         (when (= (gnus-data-number (cadr data)) article)
2089           (setcdr data (cddr data))
2090           (when offset
2091             (gnus-data-update-list (cdr data) offset))
2092           (setq data nil
2093                 gnus-newsgroup-data-reverse nil))
2094         (setq data (cdr data))))))
2095
2096 (defmacro gnus-data-list (backward)
2097   `(if ,backward
2098        (or gnus-newsgroup-data-reverse
2099            (setq gnus-newsgroup-data-reverse
2100                  (reverse gnus-newsgroup-data)))
2101      gnus-newsgroup-data))
2102
2103 (defun gnus-data-update-list (data offset)
2104   "Add OFFSET to the POS of all data entries in DATA."
2105   (setq gnus-newsgroup-data-reverse nil)
2106   (while data
2107     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2108     (setq data (cdr data))))
2109
2110 (defun gnus-summary-article-pseudo-p (article)
2111   "Say whether this article is a pseudo article or not."
2112   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2113
2114 (defmacro gnus-summary-article-sparse-p (article)
2115   "Say whether this article is a sparse article or not."
2116   `(memq ,article gnus-newsgroup-sparse))
2117
2118 (defmacro gnus-summary-article-ancient-p (article)
2119   "Say whether this article is a sparse article or not."
2120   `(memq ,article gnus-newsgroup-ancient))
2121
2122 (defun gnus-article-parent-p (number)
2123   "Say whether this article is a parent or not."
2124   (let ((data (gnus-data-find-list number)))
2125     (and (cdr data)                     ; There has to be an article after...
2126          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2127             (gnus-data-level (nth 1 data))))))
2128
2129 (defun gnus-article-children (number)
2130   "Return a list of all children to NUMBER."
2131   (let* ((data (gnus-data-find-list number))
2132          (level (gnus-data-level (car data)))
2133          children)
2134     (setq data (cdr data))
2135     (while (and data
2136                 (= (gnus-data-level (car data)) (1+ level)))
2137       (push (gnus-data-number (car data)) children)
2138       (setq data (cdr data)))
2139     children))
2140
2141 (defmacro gnus-summary-skip-intangible ()
2142   "If the current article is intangible, then jump to a different article."
2143   '(let ((to (get-text-property (point) 'gnus-intangible)))
2144      (and to (gnus-summary-goto-subject to))))
2145
2146 (defmacro gnus-summary-article-intangible-p ()
2147   "Say whether this article is intangible or not."
2148   '(get-text-property (point) 'gnus-intangible))
2149
2150 (defun gnus-article-read-p (article)
2151   "Say whether ARTICLE is read or not."
2152   (not (or (memq article gnus-newsgroup-marked)
2153            (memq article gnus-newsgroup-unreads)
2154            (memq article gnus-newsgroup-unselected)
2155            (memq article gnus-newsgroup-dormant))))
2156
2157 ;; Some summary mode macros.
2158
2159 (defmacro gnus-summary-article-number ()
2160   "The article number of the article on the current line.
2161 If there isn's an article number here, then we return the current
2162 article number."
2163   '(progn
2164      (gnus-summary-skip-intangible)
2165      (or (get-text-property (point) 'gnus-number)
2166          (gnus-summary-last-subject))))
2167
2168 (defmacro gnus-summary-article-header (&optional number)
2169   "Return the header of article NUMBER."
2170   `(gnus-data-header (gnus-data-find
2171                       ,(or number '(gnus-summary-article-number)))))
2172
2173 (defmacro gnus-summary-thread-level (&optional number)
2174   "Return the level of thread that starts with article NUMBER."
2175   `(if (and (eq gnus-summary-make-false-root 'dummy)
2176             (get-text-property (point) 'gnus-intangible))
2177        0
2178      (gnus-data-level (gnus-data-find
2179                        ,(or number '(gnus-summary-article-number))))))
2180
2181 (defmacro gnus-summary-article-mark (&optional number)
2182   "Return the mark of article NUMBER."
2183   `(gnus-data-mark (gnus-data-find
2184                     ,(or number '(gnus-summary-article-number)))))
2185
2186 (defmacro gnus-summary-article-pos (&optional number)
2187   "Return the position of the line of article NUMBER."
2188   `(gnus-data-pos (gnus-data-find
2189                    ,(or number '(gnus-summary-article-number)))))
2190
2191 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2192 (defmacro gnus-summary-article-subject (&optional number)
2193   "Return current subject string or nil if nothing."
2194   `(let ((headers
2195           ,(if number
2196                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2197              '(gnus-data-header (assq (gnus-summary-article-number)
2198                                       gnus-newsgroup-data)))))
2199      (and headers
2200           (vectorp headers)
2201           (mail-header-subject headers))))
2202
2203 (defmacro gnus-summary-article-score (&optional number)
2204   "Return current article score."
2205   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2206                   gnus-newsgroup-scored))
2207        gnus-summary-default-score 0))
2208
2209 (defun gnus-summary-article-children (&optional number)
2210   "Return a list of article numbers that are children of article NUMBER."
2211   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2212          (level (gnus-data-level (car data)))
2213          l children)
2214     (while (and (setq data (cdr data))
2215                 (> (setq l (gnus-data-level (car data))) level))
2216       (and (= (1+ level) l)
2217            (push (gnus-data-number (car data))
2218                  children)))
2219     (nreverse children)))
2220
2221 (defun gnus-summary-article-parent (&optional number)
2222   "Return the article number of the parent of article NUMBER."
2223   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2224                                     (gnus-data-list t)))
2225          (level (gnus-data-level (car data))))
2226     (if (zerop level)
2227         ()                              ; This is a root.
2228       ;; We search until we find an article with a level less than
2229       ;; this one.  That function has to be the parent.
2230       (while (and (setq data (cdr data))
2231                   (not (< (gnus-data-level (car data)) level))))
2232       (and data (gnus-data-number (car data))))))
2233
2234 (defun gnus-unread-mark-p (mark)
2235   "Say whether MARK is the unread mark."
2236   (= mark gnus-unread-mark))
2237
2238 (defun gnus-read-mark-p (mark)
2239   "Say whether MARK is one of the marks that mark as read.
2240 This is all marks except unread, ticked, dormant, and expirable."
2241   (not (or (= mark gnus-unread-mark)
2242            (= mark gnus-ticked-mark)
2243            (= mark gnus-dormant-mark)
2244            (= mark gnus-expirable-mark))))
2245
2246 (defmacro gnus-article-mark (number)
2247   "Return the MARK of article NUMBER.
2248 This macro should only be used when computing the mark the \"first\"
2249 time; i.e., when generating the summary lines.  After that,
2250 `gnus-summary-article-mark' should be used to examine the
2251 marks of articles."
2252   `(cond
2253     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2254     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2255     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2256     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2257     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2258     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2259     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2260     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2261            gnus-ancient-mark))))
2262
2263 ;; Saving hidden threads.
2264
2265 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2266 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2267
2268 (defmacro gnus-save-hidden-threads (&rest forms)
2269   "Save hidden threads, eval FORMS, and restore the hidden threads."
2270   (let ((config (make-symbol "config")))
2271     `(let ((,config (gnus-hidden-threads-configuration)))
2272        (unwind-protect
2273            (save-excursion
2274              ,@forms)
2275          (gnus-restore-hidden-threads-configuration ,config)))))
2276
2277 (defun gnus-data-compute-positions ()
2278   "Compute the positions of all articles."
2279   (setq gnus-newsgroup-data-reverse nil)
2280   (let ((data gnus-newsgroup-data))
2281     (save-excursion
2282       (gnus-save-hidden-threads
2283         (gnus-summary-show-all-threads)
2284         (goto-char (point-min))
2285         (while data
2286           (while (get-text-property (point) 'gnus-intangible)
2287             (forward-line 1))
2288           (gnus-data-set-pos (car data) (+ (point) 3))
2289           (setq data (cdr data))
2290           (forward-line 1))))))
2291
2292 (defun gnus-hidden-threads-configuration ()
2293   "Return the current hidden threads configuration."
2294   (save-excursion
2295     (let (config)
2296       (goto-char (point-min))
2297       (while (search-forward "\r" nil t)
2298         (push (1- (point)) config))
2299       config)))
2300
2301 (defun gnus-restore-hidden-threads-configuration (config)
2302   "Restore hidden threads configuration from CONFIG."
2303   (let (point buffer-read-only)
2304     (while (setq point (pop config))
2305       (when (and (< point (point-max))
2306                  (goto-char point)
2307                  (eq (char-after) ?\n))
2308         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2309
2310 ;; Various summary mode internalish functions.
2311
2312 (defun gnus-mouse-pick-article (e)
2313   (interactive "e")
2314   (mouse-set-point e)
2315   (gnus-summary-next-page nil t))
2316
2317 (defun gnus-summary-set-display-table ()
2318   ;; Change the display table.  Odd characters have a tendency to mess
2319   ;; up nicely formatted displays - we make all possible glyphs
2320   ;; display only a single character.
2321
2322   ;; We start from the standard display table, if any.
2323   (let ((table (or (copy-sequence standard-display-table)
2324                    (make-display-table)))
2325         (i 32))
2326     ;; Nix out all the control chars...
2327     (while (>= (setq i (1- i)) 0)
2328       (aset table i [??]))
2329     ;; ... but not newline and cr, of course.  (cr is necessary for the
2330     ;; selective display).
2331     (aset table ?\n nil)
2332     (aset table ?\r nil)
2333     ;; We keep TAB as well.
2334     (aset table ?\t nil)
2335     ;; We nix out any glyphs over 126 that are not set already.
2336     (let ((i 256))
2337       (while (>= (setq i (1- i)) 127)
2338         ;; Only modify if the entry is nil.
2339         (unless (aref table i)
2340           (aset table i [??]))))
2341     (setq buffer-display-table table)))
2342
2343 (defun gnus-summary-setup-buffer (group)
2344   "Initialize summary buffer."
2345   (let ((buffer (concat "*Summary " group "*")))
2346     (if (get-buffer buffer)
2347         (progn
2348           (set-buffer buffer)
2349           (setq gnus-summary-buffer (current-buffer))
2350           (not gnus-newsgroup-prepared))
2351       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2352       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2353       (gnus-summary-mode group)
2354       (when gnus-carpal
2355         (gnus-carpal-setup-buffer 'summary))
2356       (unless gnus-single-article-buffer
2357         (make-local-variable 'gnus-article-buffer)
2358         (make-local-variable 'gnus-article-current)
2359         (make-local-variable 'gnus-original-article-buffer))
2360       (setq gnus-newsgroup-name group)
2361       t)))
2362
2363 (defun gnus-set-global-variables ()
2364   ;; Set the global equivalents of the summary buffer-local variables
2365   ;; to the latest values they had.  These reflect the summary buffer
2366   ;; that was in action when the last article was fetched.
2367   (when (eq major-mode 'gnus-summary-mode)
2368     (setq gnus-summary-buffer (current-buffer))
2369     (let ((name gnus-newsgroup-name)
2370           (marked gnus-newsgroup-marked)
2371           (unread gnus-newsgroup-unreads)
2372           (headers gnus-current-headers)
2373           (data gnus-newsgroup-data)
2374           (summary gnus-summary-buffer)
2375           (article-buffer gnus-article-buffer)
2376           (original gnus-original-article-buffer)
2377           (gac gnus-article-current)
2378           (reffed gnus-reffed-article-number)
2379           (score-file gnus-current-score-file)
2380           (default-charset gnus-newsgroup-charset))
2381       (save-excursion
2382         (set-buffer gnus-group-buffer)
2383         (setq gnus-newsgroup-name name
2384               gnus-newsgroup-marked marked
2385               gnus-newsgroup-unreads unread
2386               gnus-current-headers headers
2387               gnus-newsgroup-data data
2388               gnus-article-current gac
2389               gnus-summary-buffer summary
2390               gnus-article-buffer article-buffer
2391               gnus-original-article-buffer original
2392               gnus-reffed-article-number reffed
2393               gnus-current-score-file score-file
2394               gnus-newsgroup-charset default-charset)
2395         ;; The article buffer also has local variables.
2396         (when (gnus-buffer-live-p gnus-article-buffer)
2397           (set-buffer gnus-article-buffer)
2398           (setq gnus-summary-buffer summary))))))
2399
2400 (defun gnus-summary-article-unread-p (article)
2401   "Say whether ARTICLE is unread or not."
2402   (memq article gnus-newsgroup-unreads))
2403
2404 (defun gnus-summary-first-article-p (&optional article)
2405   "Return whether ARTICLE is the first article in the buffer."
2406   (if (not (setq article (or article (gnus-summary-article-number))))
2407       nil
2408     (eq article (caar gnus-newsgroup-data))))
2409
2410 (defun gnus-summary-last-article-p (&optional article)
2411   "Return whether ARTICLE is the last article in the buffer."
2412   (if (not (setq article (or article (gnus-summary-article-number))))
2413       ;; All non-existent numbers are the last article.  :-)
2414       t
2415     (not (cdr (gnus-data-find-list article)))))
2416
2417 (defun gnus-make-thread-indent-array ()
2418   (let ((n 200))
2419     (unless (and gnus-thread-indent-array
2420                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2421       (setq gnus-thread-indent-array (make-vector 201 "")
2422             gnus-thread-indent-array-level gnus-thread-indent-level)
2423       (while (>= n 0)
2424         (aset gnus-thread-indent-array n
2425               (make-string (* n gnus-thread-indent-level) ? ))
2426         (setq n (1- n))))))
2427
2428 (defun gnus-update-summary-mark-positions ()
2429   "Compute where the summary marks are to go."
2430   (save-excursion
2431     (when (gnus-buffer-exists-p gnus-summary-buffer)
2432       (set-buffer gnus-summary-buffer))
2433     (let ((gnus-replied-mark 129)
2434           (gnus-score-below-mark 130)
2435           (gnus-score-over-mark 130)
2436           (gnus-download-mark 131)
2437           (spec gnus-summary-line-format-spec)
2438           gnus-visual pos)
2439       (save-excursion
2440         (gnus-set-work-buffer)
2441         (let ((gnus-summary-line-format-spec spec)
2442               (gnus-newsgroup-downloadable '((0 . t))))
2443           (gnus-summary-insert-line
2444            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2445            0 nil 128 t nil "" nil 1)
2446           (goto-char (point-min))
2447           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2448                                              (- (point) 2)))))
2449           (goto-char (point-min))
2450           (push (cons 'replied (and (search-forward "\201" nil t)
2451                                     (- (point) 2)))
2452                 pos)
2453           (goto-char (point-min))
2454           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2455                 pos)
2456           (goto-char (point-min))
2457           (push (cons 'download
2458                       (and (search-forward "\203" nil t) (- (point) 2)))
2459                 pos)))
2460       (setq gnus-summary-mark-positions pos))))
2461
2462 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2463   "Insert a dummy root in the summary buffer."
2464   (beginning-of-line)
2465   (gnus-add-text-properties
2466    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2467    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2468
2469 (defun gnus-summary-from-or-to-or-newsgroups (header)
2470   (let ((to (cdr (assq 'To (mail-header-extra header))))
2471         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2472         (mail-parse-charset gnus-newsgroup-charset))
2473     (cond
2474      ((and to
2475            gnus-ignored-from-addresses
2476            (string-match gnus-ignored-from-addresses
2477                          (mail-header-from header)))
2478       (concat "-> "
2479               (or (car (funcall gnus-extract-address-components
2480                                 (funcall
2481                                  gnus-decode-encoded-word-function to)))
2482                   (funcall gnus-decode-encoded-word-function to))))
2483      ((and newsgroups
2484            gnus-ignored-from-addresses
2485            (string-match gnus-ignored-from-addresses
2486                          (mail-header-from header)))
2487       (concat "=> " newsgroups))
2488      (t
2489       (or (car (funcall gnus-extract-address-components
2490                         (mail-header-from header)))
2491           (mail-header-from header))))))
2492
2493 (defun gnus-summary-insert-line (gnus-tmp-header
2494                                  gnus-tmp-level gnus-tmp-current
2495                                  gnus-tmp-unread gnus-tmp-replied
2496                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2497                                  &optional gnus-tmp-dummy gnus-tmp-score
2498                                  gnus-tmp-process)
2499   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2500          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2501          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2502          (gnus-tmp-score-char
2503           (if (or (null gnus-summary-default-score)
2504                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2505                       gnus-summary-zcore-fuzz))
2506               ? ;Whitespace
2507             (if (< gnus-tmp-score gnus-summary-default-score)
2508                 gnus-score-below-mark gnus-score-over-mark)))
2509          (gnus-tmp-replied
2510           (cond (gnus-tmp-process gnus-process-mark)
2511                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2512                  gnus-cached-mark)
2513                 (gnus-tmp-replied gnus-replied-mark)
2514                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2515                  gnus-saved-mark)
2516                 (t gnus-unread-mark)))
2517          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2518          (gnus-tmp-name
2519           (cond
2520            ((string-match "<[^>]+> *$" gnus-tmp-from)
2521             (let ((beg (match-beginning 0)))
2522               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2523                        (substring gnus-tmp-from (1+ (match-beginning 0))
2524                                   (1- (match-end 0))))
2525                   (substring gnus-tmp-from 0 beg))))
2526            ((string-match "(.+)" gnus-tmp-from)
2527             (substring gnus-tmp-from
2528                        (1+ (match-beginning 0)) (1- (match-end 0))))
2529            (t gnus-tmp-from)))
2530          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2531          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2532          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2533          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2534          (buffer-read-only nil))
2535     (when (string= gnus-tmp-name "")
2536       (setq gnus-tmp-name gnus-tmp-from))
2537     (unless (numberp gnus-tmp-lines)
2538       (setq gnus-tmp-lines 0))
2539     (gnus-put-text-property-excluding-characters-with-faces
2540      (point)
2541      (progn (eval gnus-summary-line-format-spec) (point))
2542      'gnus-number gnus-tmp-number)
2543     (when (gnus-visual-p 'summary-highlight 'highlight)
2544       (forward-line -1)
2545       (gnus-run-hooks 'gnus-summary-update-hook)
2546       (forward-line 1))))
2547
2548 (defun gnus-summary-update-line (&optional dont-update)
2549   ;; Update summary line after change.
2550   (when (and gnus-summary-default-score
2551              (not gnus-summary-inhibit-highlight))
2552     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2553            (article (gnus-summary-article-number))
2554            (score (gnus-summary-article-score article)))
2555       (unless dont-update
2556         (if (and gnus-summary-mark-below
2557                  (< (gnus-summary-article-score)
2558                     gnus-summary-mark-below))
2559             ;; This article has a low score, so we mark it as read.
2560             (when (memq article gnus-newsgroup-unreads)
2561               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2562           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2563             ;; This article was previously marked as read on account
2564             ;; of a low score, but now it has risen, so we mark it as
2565             ;; unread.
2566             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2567         (gnus-summary-update-mark
2568          (if (or (null gnus-summary-default-score)
2569                  (<= (abs (- score gnus-summary-default-score))
2570                      gnus-summary-zcore-fuzz))
2571              ? ;Whitespace
2572            (if (< score gnus-summary-default-score)
2573                gnus-score-below-mark gnus-score-over-mark))
2574          'score))
2575       ;; Do visual highlighting.
2576       (when (gnus-visual-p 'summary-highlight 'highlight)
2577         (gnus-run-hooks 'gnus-summary-update-hook)))))
2578
2579 (defvar gnus-tmp-new-adopts nil)
2580
2581 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2582   "Return the number of articles in THREAD.
2583 This may be 0 in some cases -- if none of the articles in
2584 the thread are to be displayed."
2585   (let* ((number
2586           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2587           (cond
2588            ((not (listp thread))
2589             1)
2590            ((and (consp thread) (cdr thread))
2591             (apply
2592              '+ 1 (mapcar
2593                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2594            ((null thread)
2595             1)
2596            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2597             1)
2598            (t 0))))
2599     (when (and level (zerop level) gnus-tmp-new-adopts)
2600       (incf number
2601             (apply '+ (mapcar
2602                        'gnus-summary-number-of-articles-in-thread
2603                        gnus-tmp-new-adopts))))
2604     (if char
2605         (if (> number 1) gnus-not-empty-thread-mark
2606           gnus-empty-thread-mark)
2607       number)))
2608
2609 (defun gnus-summary-set-local-parameters (group)
2610   "Go through the local params of GROUP and set all variable specs in that list."
2611   (let ((params (gnus-group-find-parameter group))
2612         elem)
2613     (while params
2614       (setq elem (car params)
2615             params (cdr params))
2616       (and (consp elem)                 ; Has to be a cons.
2617            (consp (cdr elem))           ; The cdr has to be a list.
2618            (symbolp (car elem))         ; Has to be a symbol in there.
2619            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2620            (ignore-errors               ; So we set it.
2621              (make-local-variable (car elem))
2622              (set (car elem) (eval (nth 1 elem))))))))
2623
2624 (defun gnus-summary-read-group (group &optional show-all no-article
2625                                       kill-buffer no-display backward
2626                                       select-articles)
2627   "Start reading news in newsgroup GROUP.
2628 If SHOW-ALL is non-nil, already read articles are also listed.
2629 If NO-ARTICLE is non-nil, no article is selected initially.
2630 If NO-DISPLAY, don't generate a summary buffer."
2631   (let (result)
2632     (while (and group
2633                 (null (setq result
2634                             (let ((gnus-auto-select-next nil))
2635                               (or (gnus-summary-read-group-1
2636                                    group show-all no-article
2637                                    kill-buffer no-display
2638                                    select-articles)
2639                                   (setq show-all nil
2640                                         select-articles nil)))))
2641                 (eq gnus-auto-select-next 'quietly))
2642       (set-buffer gnus-group-buffer)
2643       ;; The entry function called above goes to the next
2644       ;; group automatically, so we go two groups back
2645       ;; if we are searching for the previous group.
2646       (when backward
2647         (gnus-group-prev-unread-group 2))
2648       (if (not (equal group (gnus-group-group-name)))
2649           (setq group (gnus-group-group-name))
2650         (setq group nil)))
2651     result))
2652
2653 (defun gnus-summary-read-group-1 (group show-all no-article
2654                                         kill-buffer no-display
2655                                         &optional select-articles)
2656   ;; Killed foreign groups can't be entered.
2657   (when (and (not (gnus-group-native-p group))
2658              (not (gnus-gethash group gnus-newsrc-hashtb)))
2659     (error "Dead non-native groups can't be entered"))
2660   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2661   (let* ((new-group (gnus-summary-setup-buffer group))
2662          (quit-config (gnus-group-quit-config group))
2663          (did-select (and new-group (gnus-select-newsgroup
2664                                      group show-all select-articles))))
2665     (cond
2666      ;; This summary buffer exists already, so we just select it.
2667      ((not new-group)
2668       (gnus-set-global-variables)
2669       (when kill-buffer
2670         (gnus-kill-or-deaden-summary kill-buffer))
2671       (gnus-configure-windows 'summary 'force)
2672       (gnus-set-mode-line 'summary)
2673       (gnus-summary-position-point)
2674       (message "")
2675       t)
2676      ;; We couldn't select this group.
2677      ((null did-select)
2678       (when (and (eq major-mode 'gnus-summary-mode)
2679                  (not (equal (current-buffer) kill-buffer)))
2680         (kill-buffer (current-buffer))
2681         (if (not quit-config)
2682             (progn
2683               ;; Update the info -- marks might need to be removed,
2684               ;; for instance.
2685               (gnus-summary-update-info)
2686               (set-buffer gnus-group-buffer)
2687               (gnus-group-jump-to-group group)
2688               (gnus-group-next-unread-group 1))
2689           (gnus-handle-ephemeral-exit quit-config)))
2690       (gnus-message 3 "Can't select group")
2691       nil)
2692      ;; The user did a `C-g' while prompting for number of articles,
2693      ;; so we exit this group.
2694      ((eq did-select 'quit)
2695       (and (eq major-mode 'gnus-summary-mode)
2696            (not (equal (current-buffer) kill-buffer))
2697            (kill-buffer (current-buffer)))
2698       (when kill-buffer
2699         (gnus-kill-or-deaden-summary kill-buffer))
2700       (if (not quit-config)
2701           (progn
2702             (set-buffer gnus-group-buffer)
2703             (gnus-group-jump-to-group group)
2704             (gnus-group-next-unread-group 1)
2705             (gnus-configure-windows 'group 'force))
2706         (gnus-handle-ephemeral-exit quit-config))
2707       ;; Finally signal the quit.
2708       (signal 'quit nil))
2709      ;; The group was successfully selected.
2710      (t
2711       (gnus-set-global-variables)
2712       ;; Save the active value in effect when the group was entered.
2713       (setq gnus-newsgroup-active
2714             (gnus-copy-sequence
2715              (gnus-active gnus-newsgroup-name)))
2716       ;; You can change the summary buffer in some way with this hook.
2717       (gnus-run-hooks 'gnus-select-group-hook)
2718       ;; Set any local variables in the group parameters.
2719       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2720       (gnus-update-format-specifications
2721        nil 'summary 'summary-mode 'summary-dummy)
2722       ;; Do score processing.
2723       (when gnus-use-scoring
2724         (gnus-possibly-score-headers))
2725       ;; Check whether to fill in the gaps in the threads.
2726       (when gnus-build-sparse-threads
2727         (gnus-build-sparse-threads))
2728       ;; Find the initial limit.
2729       (if gnus-show-threads
2730           (if show-all
2731               (let ((gnus-newsgroup-dormant nil))
2732                 (gnus-summary-initial-limit show-all))
2733             (gnus-summary-initial-limit show-all))
2734         (setq gnus-newsgroup-limit
2735               (mapcar
2736                (lambda (header) (mail-header-number header))
2737                gnus-newsgroup-headers)))
2738       ;; Generate the summary buffer.
2739       (unless no-display
2740         (gnus-summary-prepare))
2741       (when gnus-use-trees
2742         (gnus-tree-open group)
2743         (setq gnus-summary-highlight-line-function
2744               'gnus-tree-highlight-article))
2745       ;; If the summary buffer is empty, but there are some low-scored
2746       ;; articles or some excluded dormants, we include these in the
2747       ;; buffer.
2748       (when (and (zerop (buffer-size))
2749                  (not no-display))
2750         (cond (gnus-newsgroup-dormant
2751                (gnus-summary-limit-include-dormant))
2752               ((and gnus-newsgroup-scored show-all)
2753                (gnus-summary-limit-include-expunged t))))
2754       ;; Function `gnus-apply-kill-file' must be called in this hook.
2755       (gnus-run-hooks 'gnus-apply-kill-hook)
2756       (if (and (zerop (buffer-size))
2757                (not no-display))
2758           (progn
2759             ;; This newsgroup is empty.
2760             (gnus-summary-catchup-and-exit nil t)
2761             (gnus-message 6 "No unread news")
2762             (when kill-buffer
2763               (gnus-kill-or-deaden-summary kill-buffer))
2764             ;; Return nil from this function.
2765             nil)
2766         ;; Hide conversation thread subtrees.  We cannot do this in
2767         ;; gnus-summary-prepare-hook since kill processing may not
2768         ;; work with hidden articles.
2769         (and gnus-show-threads
2770              gnus-thread-hide-subtree
2771              (gnus-summary-hide-all-threads))
2772         (when kill-buffer
2773           (gnus-kill-or-deaden-summary kill-buffer))
2774         ;; Show first unread article if requested.
2775         (if (and (not no-article)
2776                  (not no-display)
2777                  gnus-newsgroup-unreads
2778                  gnus-auto-select-first)
2779             (progn
2780               (gnus-configure-windows 'summary)
2781               (cond
2782                ((eq gnus-auto-select-first 'best)
2783                 (gnus-summary-best-unread-article))
2784                ((eq gnus-auto-select-first t)
2785                 (gnus-summary-first-unread-article))
2786                ((gnus-functionp gnus-auto-select-first)
2787                 (funcall gnus-auto-select-first))))
2788           ;; Don't select any articles, just move point to the first
2789           ;; article in the group.
2790           (goto-char (point-min))
2791           (gnus-summary-position-point)
2792           (gnus-configure-windows 'summary 'force)
2793           (gnus-set-mode-line 'summary))
2794         (when (get-buffer-window gnus-group-buffer t)
2795           ;; Gotta use windows, because recenter does weird stuff if
2796           ;; the current buffer ain't the displayed window.
2797           (let ((owin (selected-window)))
2798             (select-window (get-buffer-window gnus-group-buffer t))
2799             (when (gnus-group-goto-group group)
2800               (recenter))
2801             (select-window owin)))
2802         ;; Mark this buffer as "prepared".
2803         (setq gnus-newsgroup-prepared t)
2804         (gnus-run-hooks 'gnus-summary-prepared-hook)
2805         t)))))
2806
2807 (defun gnus-summary-prepare ()
2808   "Generate the summary buffer."
2809   (interactive)
2810   (let ((buffer-read-only nil))
2811     (erase-buffer)
2812     (setq gnus-newsgroup-data nil
2813           gnus-newsgroup-data-reverse nil)
2814     (gnus-run-hooks 'gnus-summary-generate-hook)
2815     ;; Generate the buffer, either with threads or without.
2816     (when gnus-newsgroup-headers
2817       (gnus-summary-prepare-threads
2818        (if gnus-show-threads
2819            (gnus-sort-gathered-threads
2820             (funcall gnus-summary-thread-gathering-function
2821                      (gnus-sort-threads
2822                       (gnus-cut-threads (gnus-make-threads)))))
2823          ;; Unthreaded display.
2824          (gnus-sort-articles gnus-newsgroup-headers))))
2825     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2826     ;; Call hooks for modifying summary buffer.
2827     (goto-char (point-min))
2828     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2829
2830 (defsubst gnus-general-simplify-subject (subject)
2831   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2832   (setq subject
2833         (cond
2834          ;; Truncate the subject.
2835          (gnus-simplify-subject-functions
2836           (gnus-map-function gnus-simplify-subject-functions subject))
2837          ((numberp gnus-summary-gather-subject-limit)
2838           (setq subject (gnus-simplify-subject-re subject))
2839           (if (> (length subject) gnus-summary-gather-subject-limit)
2840               (substring subject 0 gnus-summary-gather-subject-limit)
2841             subject))
2842          ;; Fuzzily simplify it.
2843          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2844           (gnus-simplify-subject-fuzzy subject))
2845          ;; Just remove the leading "Re:".
2846          (t
2847           (gnus-simplify-subject-re subject))))
2848
2849   (if (and gnus-summary-gather-exclude-subject
2850            (string-match gnus-summary-gather-exclude-subject subject))
2851       nil                               ; This article shouldn't be gathered
2852     subject))
2853
2854 (defun gnus-summary-simplify-subject-query ()
2855   "Query where the respool algorithm would put this article."
2856   (interactive)
2857   (gnus-summary-select-article)
2858   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2859
2860 (defun gnus-gather-threads-by-subject (threads)
2861   "Gather threads by looking at Subject headers."
2862   (if (not gnus-summary-make-false-root)
2863       threads
2864     (let ((hashtb (gnus-make-hashtable 1024))
2865           (prev threads)
2866           (result threads)
2867           subject hthread whole-subject)
2868       (while threads
2869         (setq subject (gnus-general-simplify-subject
2870                        (setq whole-subject (mail-header-subject
2871                                             (caar threads)))))
2872         (when subject
2873           (if (setq hthread (gnus-gethash subject hashtb))
2874               (progn
2875                 ;; We enter a dummy root into the thread, if we
2876                 ;; haven't done that already.
2877                 (unless (stringp (caar hthread))
2878                   (setcar hthread (list whole-subject (car hthread))))
2879                 ;; We add this new gathered thread to this gathered
2880                 ;; thread.
2881                 (setcdr (car hthread)
2882                         (nconc (cdar hthread) (list (car threads))))
2883                 ;; Remove it from the list of threads.
2884                 (setcdr prev (cdr threads))
2885                 (setq threads prev))
2886             ;; Enter this thread into the hash table.
2887             (gnus-sethash subject threads hashtb)))
2888         (setq prev threads)
2889         (setq threads (cdr threads)))
2890       result)))
2891
2892 (defun gnus-gather-threads-by-references (threads)
2893   "Gather threads by looking at References headers."
2894   (let ((idhashtb (gnus-make-hashtable 1024))
2895         (thhashtb (gnus-make-hashtable 1024))
2896         (prev threads)
2897         (result threads)
2898         ids references id gthread gid entered ref)
2899     (while threads
2900       (when (setq references (mail-header-references (caar threads)))
2901         (setq id (mail-header-id (caar threads))
2902               ids (gnus-split-references references)
2903               entered nil)
2904         (while (setq ref (pop ids))
2905           (setq ids (delete ref ids))
2906           (if (not (setq gid (gnus-gethash ref idhashtb)))
2907               (progn
2908                 (gnus-sethash ref id idhashtb)
2909                 (gnus-sethash id threads thhashtb))
2910             (setq gthread (gnus-gethash gid thhashtb))
2911             (unless entered
2912               ;; We enter a dummy root into the thread, if we
2913               ;; haven't done that already.
2914               (unless (stringp (caar gthread))
2915                 (setcar gthread (list (mail-header-subject (caar gthread))
2916                                       (car gthread))))
2917               ;; We add this new gathered thread to this gathered
2918               ;; thread.
2919               (setcdr (car gthread)
2920                       (nconc (cdar gthread) (list (car threads)))))
2921             ;; Add it into the thread hash table.
2922             (gnus-sethash id gthread thhashtb)
2923             (setq entered t)
2924             ;; Remove it from the list of threads.
2925             (setcdr prev (cdr threads))
2926             (setq threads prev))))
2927       (setq prev threads)
2928       (setq threads (cdr threads)))
2929     result))
2930
2931 (defun gnus-sort-gathered-threads (threads)
2932   "Sort subtreads inside each gathered thread by article number."
2933   (let ((result threads))
2934     (while threads
2935       (when (stringp (caar threads))
2936         (setcdr (car threads)
2937                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2938       (setq threads (cdr threads)))
2939     result))
2940
2941 (defun gnus-thread-loop-p (root thread)
2942   "Say whether ROOT is in THREAD."
2943   (let ((stack (list thread))
2944         (infloop 0)
2945         th)
2946     (while (setq thread (pop stack))
2947       (setq th (cdr thread))
2948       (while (and th
2949                   (not (eq (caar th) root)))
2950         (pop th))
2951       (if th
2952           ;; We have found a loop.
2953           (let (ref-dep)
2954             (setcdr thread (delq (car th) (cdr thread)))
2955             (if (boundp (setq ref-dep (intern "none"
2956                                               gnus-newsgroup-dependencies)))
2957                 (setcdr (symbol-value ref-dep)
2958                         (nconc (cdr (symbol-value ref-dep))
2959                                (list (car th))))
2960               (set ref-dep (list nil (car th))))
2961             (setq infloop 1
2962                   stack nil))
2963         ;; Push all the subthreads onto the stack.
2964         (push (cdr thread) stack)))
2965     infloop))
2966
2967 (defun gnus-make-threads ()
2968   "Go through the dependency hashtb and find the roots.  Return all threads."
2969   (let (threads)
2970     (while (catch 'infloop
2971              (mapatoms
2972               (lambda (refs)
2973                 ;; Deal with self-referencing References loops.
2974                 (when (and (car (symbol-value refs))
2975                            (not (zerop
2976                                  (apply
2977                                   '+
2978                                   (mapcar
2979                                    (lambda (thread)
2980                                      (gnus-thread-loop-p
2981                                       (car (symbol-value refs)) thread))
2982                                    (cdr (symbol-value refs)))))))
2983                   (setq threads nil)
2984                   (throw 'infloop t))
2985                 (unless (car (symbol-value refs))
2986                   ;; These threads do not refer back to any other articles,
2987                   ;; so they're roots.
2988                   (setq threads (append (cdr (symbol-value refs)) threads))))
2989               gnus-newsgroup-dependencies)))
2990     threads))
2991
2992 ;; Build the thread tree.
2993 (defsubst gnus-dependencies-add-header (header dependencies force-new)
2994   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2995
2996 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2997 if it was already present.
2998
2999 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3000 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3001 Message-IDs will be renamed be renamed to a unique Message-ID before
3002 being entered.
3003
3004 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3005   (let* ((id (mail-header-id header))
3006          (id-dep (and id (intern id dependencies)))
3007          ref ref-dep ref-header)
3008     ;; Enter this `header' in the `dependencies' table.
3009     (cond
3010      ((not id-dep)
3011       (setq header nil))
3012      ;; The first two cases do the normal part: enter a new `header'
3013      ;; in the `dependencies' table.
3014      ((not (boundp id-dep))
3015       (set id-dep (list header)))
3016      ((null (car (symbol-value id-dep)))
3017       (setcar (symbol-value id-dep) header))
3018
3019      ;; From here the `header' was already present in the
3020      ;; `dependencies' table.
3021      (force-new
3022       ;; Overrides an existing entry;
3023       ;; just set the header part of the entry.
3024       (setcar (symbol-value id-dep) header))
3025
3026      ;; Renames the existing `header' to a unique Message-ID.
3027      ((not gnus-summary-ignore-duplicates)
3028       ;; An article with this Message-ID has already been seen.
3029       ;; We rename the Message-ID.
3030       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3031            (list header))
3032       (mail-header-set-id header id))
3033
3034      ;; The last case ignores an existing entry, except it adds any
3035      ;; additional Xrefs (in case the two articles came from different
3036      ;; servers.
3037      ;; Also sets `header' to `nil' meaning that the `dependencies'
3038      ;; table was *not* modified.
3039      (t
3040       (mail-header-set-xref
3041        (car (symbol-value id-dep))
3042        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3043                    "")
3044                (or (mail-header-xref header) "")))
3045       (setq header nil)))
3046
3047     (when header
3048       ;; First check if that we are not creating a References loop.
3049       (setq ref (gnus-parent-id (mail-header-references header)))
3050       (while (and ref
3051                   (setq ref-dep (intern-soft ref dependencies))
3052                   (boundp ref-dep)
3053                   (setq ref-header (car (symbol-value ref-dep))))
3054         (if (string= id ref)
3055             ;; Yuk!  This is a reference loop.  Make the article be a
3056             ;; root article.
3057             (progn
3058               (mail-header-set-references (car (symbol-value id-dep)) "none")
3059               (setq ref nil))
3060           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3061       (setq ref (gnus-parent-id (mail-header-references header)))
3062       (setq ref-dep (intern (or ref "none") dependencies))
3063       (if (boundp ref-dep)
3064           (setcdr (symbol-value ref-dep)
3065                   (nconc (cdr (symbol-value ref-dep))
3066                          (list (symbol-value id-dep))))
3067         (set ref-dep (list nil (symbol-value id-dep)))))
3068     header))
3069
3070 (defun gnus-build-sparse-threads ()
3071   (let ((headers gnus-newsgroup-headers)
3072         (gnus-summary-ignore-duplicates t)
3073         header references generation relations
3074         subject child end new-child date)
3075     ;; First we create an alist of generations/relations, where
3076     ;; generations is how much we trust the relation, and the relation
3077     ;; is parent/child.
3078     (gnus-message 7 "Making sparse threads...")
3079     (save-excursion
3080       (nnheader-set-temp-buffer " *gnus sparse threads*")
3081       (while (setq header (pop headers))
3082         (when (and (setq references (mail-header-references header))
3083                    (not (string= references "")))
3084           (insert references)
3085           (setq child (mail-header-id header)
3086                 subject (mail-header-subject header)
3087                 date (mail-header-date header)
3088                 generation 0)
3089           (while (search-backward ">" nil t)
3090             (setq end (1+ (point)))
3091             (when (search-backward "<" nil t)
3092               (setq new-child (buffer-substring (point) end))
3093               (push (list (incf generation)
3094                           child (setq child new-child)
3095                           subject date)
3096                     relations)))
3097           (when child
3098             (push (list (1+ generation) child nil subject) relations))
3099           (erase-buffer)))
3100       (kill-buffer (current-buffer)))
3101     ;; Sort over trustworthiness.
3102     (mapcar
3103      (lambda (relation)
3104        (when (gnus-dependencies-add-header
3105               (make-full-mail-header
3106                gnus-reffed-article-number
3107                (nth 3 relation) "" (or (nth 4 relation) "")
3108                (nth 1 relation)
3109                (or (nth 2 relation) "") 0 0 "")
3110               gnus-newsgroup-dependencies nil)
3111          (push gnus-reffed-article-number gnus-newsgroup-limit)
3112          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3113          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3114                gnus-newsgroup-reads)
3115          (decf gnus-reffed-article-number)))
3116      (sort relations 'car-less-than-car))
3117     (gnus-message 7 "Making sparse threads...done")))
3118
3119 (defun gnus-build-old-threads ()
3120   ;; Look at all the articles that refer back to old articles, and
3121   ;; fetch the headers for the articles that aren't there.  This will
3122   ;; build complete threads - if the roots haven't been expired by the
3123   ;; server, that is.
3124   (let (id heads)
3125     (mapatoms
3126      (lambda (refs)
3127        (when (not (car (symbol-value refs)))
3128          (setq heads (cdr (symbol-value refs)))
3129          (while heads
3130            (if (memq (mail-header-number (caar heads))
3131                      gnus-newsgroup-dormant)
3132                (setq heads (cdr heads))
3133              (setq id (symbol-name refs))
3134              (while (and (setq id (gnus-build-get-header id))
3135                          (not (car (gnus-id-to-thread id)))))
3136              (setq heads nil)))))
3137      gnus-newsgroup-dependencies)))
3138
3139 ;; This function has to be called with point after the article number
3140 ;; on the beginning of the line.
3141 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3142   (let ((eol (gnus-point-at-eol))
3143         (buffer (current-buffer))
3144         header)
3145
3146     ;; overview: [num subject from date id refs chars lines misc]
3147     (unless (eobp)
3148       (forward-char))
3149
3150     (setq header
3151           (make-full-mail-header
3152            number                               ; number
3153            (nnheader-nov-field)                 ; subject
3154            (nnheader-nov-field)                 ; from
3155            (nnheader-nov-field)                 ; date
3156            (or (nnheader-nov-field)             ; id
3157                (nnheader-generate-fake-message-id))
3158            (nnheader-nov-field)                 ; refs
3159            (nnheader-nov-read-integer)          ; chars
3160            (nnheader-nov-read-integer)          ; lines
3161            (unless (eq (char-after) ?\n)
3162              (nnheader-nov-field))              ; misc
3163            (nnheader-nov-parse-extra)))         ; extra
3164
3165     (when gnus-alter-header-function
3166       (funcall gnus-alter-header-function header))
3167     (gnus-dependencies-add-header header dependencies force-new)))
3168
3169 (defun gnus-build-get-header (id)
3170   ;; Look through the buffer of NOV lines and find the header to
3171   ;; ID.  Enter this line into the dependencies hash table, and return
3172   ;; the id of the parent article (if any).
3173   (let ((deps gnus-newsgroup-dependencies)
3174         found header)
3175     (prog1
3176         (save-excursion
3177           (set-buffer nntp-server-buffer)
3178           (let ((case-fold-search nil))
3179             (goto-char (point-min))
3180             (while (and (not found)
3181                         (search-forward id nil t))
3182               (beginning-of-line)
3183               (setq found (looking-at
3184                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3185                                    (regexp-quote id))))
3186               (or found (beginning-of-line 2)))
3187             (when found
3188               (beginning-of-line)
3189               (and
3190                (setq header (gnus-nov-parse-line
3191                              (read (current-buffer)) deps))
3192                (gnus-parent-id (mail-header-references header))))))
3193       (when header
3194         (let ((number (mail-header-number header)))
3195           (push number gnus-newsgroup-limit)
3196           (push header gnus-newsgroup-headers)
3197           (if (memq number gnus-newsgroup-unselected)
3198               (progn
3199                 (push number gnus-newsgroup-unreads)
3200                 (setq gnus-newsgroup-unselected
3201                       (delq number gnus-newsgroup-unselected)))
3202             (push number gnus-newsgroup-ancient)))))))
3203
3204 (defun gnus-build-all-threads ()
3205   "Read all the headers."
3206   (let ((gnus-summary-ignore-duplicates t)
3207         (dependencies gnus-newsgroup-dependencies)
3208         header article)
3209     (save-excursion
3210       (set-buffer nntp-server-buffer)
3211       (let ((case-fold-search nil))
3212         (goto-char (point-min))
3213         (while (not (eobp))
3214           (ignore-errors
3215             (setq article (read (current-buffer))
3216                   header (gnus-nov-parse-line
3217                           article dependencies)))
3218           (when header
3219             (save-excursion
3220               (set-buffer gnus-summary-buffer)
3221               (push header gnus-newsgroup-headers)
3222               (if (memq (setq article (mail-header-number header))
3223                         gnus-newsgroup-unselected)
3224                   (progn
3225                     (push article gnus-newsgroup-unreads)
3226                     (setq gnus-newsgroup-unselected
3227                           (delq article gnus-newsgroup-unselected)))
3228                 (push article gnus-newsgroup-ancient)))
3229             (forward-line 1)))))))
3230
3231 (defun gnus-summary-update-article-line (article header)
3232   "Update the line for ARTICLE using HEADERS."
3233   (let* ((id (mail-header-id header))
3234          (thread (gnus-id-to-thread id)))
3235     (unless thread
3236       (error "Article in no thread"))
3237     ;; Update the thread.
3238     (setcar thread header)
3239     (gnus-summary-goto-subject article)
3240     (let* ((datal (gnus-data-find-list article))
3241            (data (car datal))
3242            (length (when (cdr datal)
3243                      (- (gnus-data-pos data)
3244                         (gnus-data-pos (cadr datal)))))
3245            (buffer-read-only nil)
3246            (level (gnus-summary-thread-level)))
3247       (gnus-delete-line)
3248       (gnus-summary-insert-line
3249        header level nil (gnus-article-mark article)
3250        (memq article gnus-newsgroup-replied)
3251        (memq article gnus-newsgroup-expirable)
3252        ;; Only insert the Subject string when it's different
3253        ;; from the previous Subject string.
3254        (if (gnus-subject-equal
3255             (condition-case ()
3256                 (mail-header-subject
3257                  (gnus-data-header
3258                   (cadr
3259                    (gnus-data-find-list
3260                     article
3261                     (gnus-data-list t)))))
3262               ;; Error on the side of excessive subjects.
3263               (error ""))
3264             (mail-header-subject header))
3265            ""
3266          (mail-header-subject header))
3267        nil (cdr (assq article gnus-newsgroup-scored))
3268        (memq article gnus-newsgroup-processable))
3269       (when length
3270         (gnus-data-update-list
3271          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3272
3273 (defun gnus-summary-update-article (article &optional iheader)
3274   "Update ARTICLE in the summary buffer."
3275   (set-buffer gnus-summary-buffer)
3276   (let* ((header (gnus-summary-article-header article))
3277          (id (mail-header-id header))
3278          (data (gnus-data-find article))
3279          (thread (gnus-id-to-thread id))
3280          (references (mail-header-references header))
3281          (parent
3282           (gnus-id-to-thread
3283            (or (gnus-parent-id
3284                 (when (and references
3285                            (not (equal "" references)))
3286                   references))
3287                "none")))
3288          (buffer-read-only nil)
3289          (old (car thread)))
3290     (when thread
3291       (unless iheader
3292         (setcar thread nil)
3293         (when parent
3294           (delq thread parent)))
3295       (if (gnus-summary-insert-subject id header)
3296           ;; Set the (possibly) new article number in the data structure.
3297           (gnus-data-set-number data (gnus-id-to-article id))
3298         (setcar thread old)
3299         nil))))
3300
3301 (defun gnus-rebuild-thread (id &optional line)
3302   "Rebuild the thread containing ID.
3303 If LINE, insert the rebuilt thread starting on line LINE."
3304   (let ((buffer-read-only nil)
3305         old-pos current thread data)
3306     (if (not gnus-show-threads)
3307         (setq thread (list (car (gnus-id-to-thread id))))
3308       ;; Get the thread this article is part of.
3309       (setq thread (gnus-remove-thread id)))
3310     (setq old-pos (gnus-point-at-bol))
3311     (setq current (save-excursion
3312                     (and (zerop (forward-line -1))
3313                          (gnus-summary-article-number))))
3314     ;; If this is a gathered thread, we have to go some re-gathering.
3315     (when (stringp (car thread))
3316       (let ((subject (car thread))
3317             roots thr)
3318         (setq thread (cdr thread))
3319         (while thread
3320           (unless (memq (setq thr (gnus-id-to-thread
3321                                    (gnus-root-id
3322                                     (mail-header-id (caar thread)))))
3323                         roots)
3324             (push thr roots))
3325           (setq thread (cdr thread)))
3326         ;; We now have all (unique) roots.
3327         (if (= (length roots) 1)
3328             ;; All the loose roots are now one solid root.
3329             (setq thread (car roots))
3330           (setq thread (cons subject (gnus-sort-threads roots))))))
3331     (let (threads)
3332       ;; We then insert this thread into the summary buffer.
3333       (when line
3334         (goto-char (point-min))
3335         (forward-line (1- line)))
3336       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3337         (if gnus-show-threads
3338             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3339           (gnus-summary-prepare-unthreaded thread))
3340         (setq data (nreverse gnus-newsgroup-data))
3341         (setq threads gnus-newsgroup-threads))
3342       ;; We splice the new data into the data structure.
3343       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3344       ;;!!! then we want to insert at the beginning of the buffer.
3345       ;;!!! That happens to be true with Gnus now, but that may
3346       ;;!!! change in the future.  Perhaps.
3347       (gnus-data-enter-list
3348        (if line nil current) data (- (point) old-pos))
3349       (setq gnus-newsgroup-threads
3350             (nconc threads gnus-newsgroup-threads))
3351       (gnus-data-compute-positions))))
3352
3353 (defun gnus-number-to-header (number)
3354   "Return the header for article NUMBER."
3355   (let ((headers gnus-newsgroup-headers))
3356     (while (and headers
3357                 (not (= number (mail-header-number (car headers)))))
3358       (pop headers))
3359     (when headers
3360       (car headers))))
3361
3362 (defun gnus-parent-headers (in-headers &optional generation)
3363   "Return the headers of the GENERATIONeth parent of HEADERS."
3364   (unless generation
3365     (setq generation 1))
3366   (let ((parent t)
3367         (headers in-headers)
3368         references)
3369     (while (and parent
3370                 (not (zerop generation))
3371                 (setq references (mail-header-references headers)))
3372       (setq headers (if (and references
3373                              (setq parent (gnus-parent-id references)))
3374                         (car (gnus-id-to-thread parent))
3375                       nil))
3376       (decf generation))
3377     (and (not (eq headers in-headers))
3378          headers)))
3379
3380 (defun gnus-id-to-thread (id)
3381   "Return the (sub-)thread where ID appears."
3382   (gnus-gethash id gnus-newsgroup-dependencies))
3383
3384 (defun gnus-id-to-article (id)
3385   "Return the article number of ID."
3386   (let ((thread (gnus-id-to-thread id)))
3387     (when (and thread
3388                (car thread))
3389       (mail-header-number (car thread)))))
3390
3391 (defun gnus-id-to-header (id)
3392   "Return the article headers of ID."
3393   (car (gnus-id-to-thread id)))
3394
3395 (defun gnus-article-displayed-root-p (article)
3396   "Say whether ARTICLE is a root(ish) article."
3397   (let ((level (gnus-summary-thread-level article))
3398         (refs (mail-header-references  (gnus-summary-article-header article)))
3399         particle)
3400     (cond
3401      ((null level) nil)
3402      ((zerop level) t)
3403      ((null refs) t)
3404      ((null (gnus-parent-id refs)) t)
3405      ((and (= 1 level)
3406            (null (setq particle (gnus-id-to-article
3407                                  (gnus-parent-id refs))))
3408            (null (gnus-summary-thread-level particle)))))))
3409
3410 (defun gnus-root-id (id)
3411   "Return the id of the root of the thread where ID appears."
3412   (let (last-id prev)
3413     (while (and id (setq prev (car (gnus-id-to-thread id))))
3414       (setq last-id id
3415             id (gnus-parent-id (mail-header-references prev))))
3416     last-id))
3417
3418 (defun gnus-articles-in-thread (thread)
3419   "Return the list of articles in THREAD."
3420   (cons (mail-header-number (car thread))
3421         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3422
3423 (defun gnus-remove-thread (id &optional dont-remove)
3424   "Remove the thread that has ID in it."
3425   (let (headers thread last-id)
3426     ;; First go up in this thread until we find the root.
3427     (setq last-id (gnus-root-id id)
3428           headers (message-flatten-list (gnus-id-to-thread last-id)))
3429     ;; We have now found the real root of this thread.  It might have
3430     ;; been gathered into some loose thread, so we have to search
3431     ;; through the threads to find the thread we wanted.
3432     (let ((threads gnus-newsgroup-threads)
3433           sub)
3434       (while threads
3435         (setq sub (car threads))
3436         (if (stringp (car sub))
3437             ;; This is a gathered thread, so we look at the roots
3438             ;; below it to find whether this article is in this
3439             ;; gathered root.
3440             (progn
3441               (setq sub (cdr sub))
3442               (while sub
3443                 (when (member (caar sub) headers)
3444                   (setq thread (car threads)
3445                         threads nil
3446                         sub nil))
3447                 (setq sub (cdr sub))))
3448           ;; It's an ordinary thread, so we check it.
3449           (when (eq (car sub) (car headers))
3450             (setq thread sub
3451                   threads nil)))
3452         (setq threads (cdr threads)))
3453       ;; If this article is in no thread, then it's a root.
3454       (if thread
3455           (unless dont-remove
3456             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3457         (setq thread (gnus-id-to-thread last-id)))
3458       (when thread
3459         (prog1
3460             thread                      ; We return this thread.
3461           (unless dont-remove
3462             (if (stringp (car thread))
3463                 (progn
3464                   ;; If we use dummy roots, then we have to remove the
3465                   ;; dummy root as well.
3466                   (when (eq gnus-summary-make-false-root 'dummy)
3467                     ;; We go to the dummy root by going to
3468                     ;; the first sub-"thread", and then one line up.
3469                     (gnus-summary-goto-article
3470                      (mail-header-number (caadr thread)))
3471                     (forward-line -1)
3472                     (gnus-delete-line)
3473                     (gnus-data-compute-positions))
3474                   (setq thread (cdr thread))
3475                   (while thread
3476                     (gnus-remove-thread-1 (car thread))
3477                     (setq thread (cdr thread))))
3478               (gnus-summary-show-all-threads)
3479               (gnus-remove-thread-1 thread))))))))
3480
3481 (defun gnus-remove-thread-1 (thread)
3482   "Remove the thread THREAD recursively."
3483   (let ((number (mail-header-number (pop thread)))
3484         d)
3485     (setq thread (reverse thread))
3486     (while thread
3487       (gnus-remove-thread-1 (pop thread)))
3488     (when (setq d (gnus-data-find number))
3489       (goto-char (gnus-data-pos d))
3490       (gnus-data-remove
3491        number
3492        (- (gnus-point-at-bol)
3493           (prog1
3494               (1+ (gnus-point-at-eol))
3495             (gnus-delete-line)))))))
3496
3497 (defun gnus-sort-threads (threads)
3498   "Sort THREADS."
3499   (if (not gnus-thread-sort-functions)
3500       threads
3501     (gnus-message 8 "Sorting threads...")
3502     (prog1
3503         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3504       (gnus-message 8 "Sorting threads...done"))))
3505
3506 (defun gnus-sort-articles (articles)
3507   "Sort ARTICLES."
3508   (when gnus-article-sort-functions
3509     (gnus-message 7 "Sorting articles...")
3510     (prog1
3511         (setq gnus-newsgroup-headers
3512               (sort articles (gnus-make-sort-function
3513                               gnus-article-sort-functions)))
3514       (gnus-message 7 "Sorting articles...done"))))
3515
3516 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3517 (defmacro gnus-thread-header (thread)
3518   ;; Return header of first article in THREAD.
3519   ;; Note that THREAD must never, ever be anything else than a variable -
3520   ;; using some other form will lead to serious barfage.
3521   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3522   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3523   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3524         (vector thread) 2))
3525
3526 (defsubst gnus-article-sort-by-number (h1 h2)
3527   "Sort articles by article number."
3528   (< (mail-header-number h1)
3529      (mail-header-number h2)))
3530
3531 (defun gnus-thread-sort-by-number (h1 h2)
3532   "Sort threads by root article number."
3533   (gnus-article-sort-by-number
3534    (gnus-thread-header h1) (gnus-thread-header h2)))
3535
3536 (defsubst gnus-article-sort-by-lines (h1 h2)
3537   "Sort articles by article Lines header."
3538   (< (mail-header-lines h1)
3539      (mail-header-lines h2)))
3540
3541 (defun gnus-thread-sort-by-lines (h1 h2)
3542   "Sort threads by root article Lines header."
3543   (gnus-article-sort-by-lines
3544    (gnus-thread-header h1) (gnus-thread-header h2)))
3545
3546 (defsubst gnus-article-sort-by-author (h1 h2)
3547   "Sort articles by root author."
3548   (string-lessp
3549    (let ((addr (mime-read-field 'From h1)))
3550      (or (std11-full-name-string addr)
3551          (std11-address-string addr)
3552          ""))
3553    (let ((addr (mime-read-field 'From h2)))
3554      (or (std11-full-name-string addr)
3555          (std11-address-string addr)
3556          ""))
3557    ))
3558
3559 (defun gnus-thread-sort-by-author (h1 h2)
3560   "Sort threads by root author."
3561   (gnus-article-sort-by-author
3562    (gnus-thread-header h1)  (gnus-thread-header h2)))
3563
3564 (defsubst gnus-article-sort-by-subject (h1 h2)
3565   "Sort articles by root subject."
3566   (string-lessp
3567    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3568    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3569
3570 (defun gnus-thread-sort-by-subject (h1 h2)
3571   "Sort threads by root subject."
3572   (gnus-article-sort-by-subject
3573    (gnus-thread-header h1) (gnus-thread-header h2)))
3574
3575 (defsubst gnus-article-sort-by-date (h1 h2)
3576   "Sort articles by root article date."
3577   (time-less-p
3578    (gnus-date-get-time (mail-header-date h1))
3579    (gnus-date-get-time (mail-header-date h2))))
3580
3581 (defun gnus-thread-sort-by-date (h1 h2)
3582   "Sort threads by root article date."
3583   (gnus-article-sort-by-date
3584    (gnus-thread-header h1) (gnus-thread-header h2)))
3585
3586 (defsubst gnus-article-sort-by-score (h1 h2)
3587   "Sort articles by root article score.
3588 Unscored articles will be counted as having a score of zero."
3589   (> (or (cdr (assq (mail-header-number h1)
3590                     gnus-newsgroup-scored))
3591          gnus-summary-default-score 0)
3592      (or (cdr (assq (mail-header-number h2)
3593                     gnus-newsgroup-scored))
3594          gnus-summary-default-score 0)))
3595
3596 (defun gnus-thread-sort-by-score (h1 h2)
3597   "Sort threads by root article score."
3598   (gnus-article-sort-by-score
3599    (gnus-thread-header h1) (gnus-thread-header h2)))
3600
3601 (defun gnus-thread-sort-by-total-score (h1 h2)
3602   "Sort threads by the sum of all scores in the thread.
3603 Unscored articles will be counted as having a score of zero."
3604   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3605
3606 (defun gnus-thread-total-score (thread)
3607   ;; This function find the total score of THREAD.
3608   (cond ((null thread)
3609          0)
3610         ((consp thread)
3611          (if (stringp (car thread))
3612              (apply gnus-thread-score-function 0
3613                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3614            (gnus-thread-total-score-1 thread)))
3615         (t
3616          (gnus-thread-total-score-1 (list thread)))))
3617
3618 (defun gnus-thread-total-score-1 (root)
3619   ;; This function find the total score of the thread below ROOT.
3620   (setq root (car root))
3621   (apply gnus-thread-score-function
3622          (or (append
3623               (mapcar 'gnus-thread-total-score
3624                       (cdr (gnus-id-to-thread (mail-header-id root))))
3625               (when (> (mail-header-number root) 0)
3626                 (list (or (cdr (assq (mail-header-number root)
3627                                      gnus-newsgroup-scored))
3628                           gnus-summary-default-score 0))))
3629              (list gnus-summary-default-score)
3630              '(0))))
3631
3632 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3633 (defvar gnus-tmp-prev-subject nil)
3634 (defvar gnus-tmp-false-parent nil)
3635 (defvar gnus-tmp-root-expunged nil)
3636 (defvar gnus-tmp-dummy-line nil)
3637
3638 (defvar gnus-tmp-header)
3639 (defun gnus-extra-header (type &optional header)
3640   "Return the extra header of TYPE."
3641   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3642       ""))
3643
3644 (defun gnus-summary-prepare-threads (threads)
3645   "Prepare summary buffer from THREADS and indentation LEVEL.
3646 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3647 or a straight list of headers."
3648   (gnus-message 7 "Generating summary...")
3649
3650   (setq gnus-newsgroup-threads threads)
3651   (beginning-of-line)
3652
3653   (let ((gnus-tmp-level 0)
3654         (default-score (or gnus-summary-default-score 0))
3655         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3656         thread number subject stack state gnus-tmp-gathered beg-match
3657         new-roots gnus-tmp-new-adopts thread-end
3658         gnus-tmp-header gnus-tmp-unread
3659         gnus-tmp-replied gnus-tmp-subject-or-nil
3660         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3661         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3662         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3663
3664     (setq gnus-tmp-prev-subject nil)
3665
3666     (if (vectorp (car threads))
3667         ;; If this is a straight (sic) list of headers, then a
3668         ;; threaded summary display isn't required, so we just create
3669         ;; an unthreaded one.
3670         (gnus-summary-prepare-unthreaded threads)
3671
3672       ;; Do the threaded display.
3673
3674       (while (or threads stack gnus-tmp-new-adopts new-roots)
3675
3676         (if (and (= gnus-tmp-level 0)
3677                  (or (not stack)
3678                      (= (caar stack) 0))
3679                  (not gnus-tmp-false-parent)
3680                  (or gnus-tmp-new-adopts new-roots))
3681             (if gnus-tmp-new-adopts
3682                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3683                       thread (list (car gnus-tmp-new-adopts))
3684                       gnus-tmp-header (caar thread)
3685                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3686               (when new-roots
3687                 (setq thread (list (car new-roots))
3688                       gnus-tmp-header (caar thread)
3689                       new-roots (cdr new-roots))))
3690
3691           (if threads
3692               ;; If there are some threads, we do them before the
3693               ;; threads on the stack.
3694               (setq thread threads
3695                     gnus-tmp-header (caar thread))
3696             ;; There were no current threads, so we pop something off
3697             ;; the stack.
3698             (setq state (car stack)
3699                   gnus-tmp-level (car state)
3700                   thread (cdr state)
3701                   stack (cdr stack)
3702                   gnus-tmp-header (caar thread))))
3703
3704         (setq gnus-tmp-false-parent nil)
3705         (setq gnus-tmp-root-expunged nil)
3706         (setq thread-end nil)
3707
3708         (if (stringp gnus-tmp-header)
3709             ;; The header is a dummy root.
3710             (cond
3711              ((eq gnus-summary-make-false-root 'adopt)
3712               ;; We let the first article adopt the rest.
3713               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3714                                                (cddar thread)))
3715               (setq gnus-tmp-gathered
3716                     (nconc (mapcar
3717                             (lambda (h) (mail-header-number (car h)))
3718                             (cddar thread))
3719                            gnus-tmp-gathered))
3720               (setq thread (cons (list (caar thread)
3721                                        (cadar thread))
3722                                  (cdr thread)))
3723               (setq gnus-tmp-level -1
3724                     gnus-tmp-false-parent t))
3725              ((eq gnus-summary-make-false-root 'empty)
3726               ;; We print adopted articles with empty subject fields.
3727               (setq gnus-tmp-gathered
3728                     (nconc (mapcar
3729                             (lambda (h) (mail-header-number (car h)))
3730                             (cddar thread))
3731                            gnus-tmp-gathered))
3732               (setq gnus-tmp-level -1))
3733              ((eq gnus-summary-make-false-root 'dummy)
3734               ;; We remember that we probably want to output a dummy
3735               ;; root.
3736               (setq gnus-tmp-dummy-line gnus-tmp-header)
3737               (setq gnus-tmp-prev-subject gnus-tmp-header))
3738              (t
3739               ;; We do not make a root for the gathered
3740               ;; sub-threads at all.
3741               (setq gnus-tmp-level -1)))
3742
3743           (setq number (mail-header-number gnus-tmp-header)
3744                 subject (mail-header-subject gnus-tmp-header))
3745
3746           (cond
3747            ;; If the thread has changed subject, we might want to make
3748            ;; this subthread into a root.
3749            ((and (null gnus-thread-ignore-subject)
3750                  (not (zerop gnus-tmp-level))
3751                  gnus-tmp-prev-subject
3752                  (not (inline
3753                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3754             (setq new-roots (nconc new-roots (list (car thread)))
3755                   thread-end t
3756                   gnus-tmp-header nil))
3757            ;; If the article lies outside the current limit,
3758            ;; then we do not display it.
3759            ((not (memq number gnus-newsgroup-limit))
3760             (setq gnus-tmp-gathered
3761                   (nconc (mapcar
3762                           (lambda (h) (mail-header-number (car h)))
3763                           (cdar thread))
3764                          gnus-tmp-gathered))
3765             (setq gnus-tmp-new-adopts (if (cdar thread)
3766                                           (append gnus-tmp-new-adopts
3767                                                   (cdar thread))
3768                                         gnus-tmp-new-adopts)
3769                   thread-end t
3770                   gnus-tmp-header nil)
3771             (when (zerop gnus-tmp-level)
3772               (setq gnus-tmp-root-expunged t)))
3773            ;; Perhaps this article is to be marked as read?
3774            ((and gnus-summary-mark-below
3775                  (< (or (cdr (assq number gnus-newsgroup-scored))
3776                         default-score)
3777                     gnus-summary-mark-below)
3778                  ;; Don't touch sparse articles.
3779                  (not (gnus-summary-article-sparse-p number))
3780                  (not (gnus-summary-article-ancient-p number)))
3781             (setq gnus-newsgroup-unreads
3782                   (delq number gnus-newsgroup-unreads))
3783             (if gnus-newsgroup-auto-expire
3784                 (push number gnus-newsgroup-expirable)
3785               (push (cons number gnus-low-score-mark)
3786                     gnus-newsgroup-reads))))
3787
3788           (when gnus-tmp-header
3789             ;; We may have an old dummy line to output before this
3790             ;; article.
3791             (when (and gnus-tmp-dummy-line
3792                        (gnus-subject-equal
3793                         gnus-tmp-dummy-line
3794                         (mail-header-subject gnus-tmp-header)))
3795               (gnus-summary-insert-dummy-line
3796                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3797               (setq gnus-tmp-dummy-line nil))
3798
3799             ;; Compute the mark.
3800             (setq gnus-tmp-unread (gnus-article-mark number))
3801
3802             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3803                                   gnus-tmp-header gnus-tmp-level)
3804                   gnus-newsgroup-data)
3805
3806             ;; Actually insert the line.
3807             (setq
3808              gnus-tmp-subject-or-nil
3809              (cond
3810               ((and gnus-thread-ignore-subject
3811                     gnus-tmp-prev-subject
3812                     (not (inline (gnus-subject-equal
3813                                   gnus-tmp-prev-subject subject))))
3814                subject)
3815               ((zerop gnus-tmp-level)
3816                (if (and (eq gnus-summary-make-false-root 'empty)
3817                         (memq number gnus-tmp-gathered)
3818                         gnus-tmp-prev-subject
3819                         (inline (gnus-subject-equal
3820                                  gnus-tmp-prev-subject subject)))
3821                    gnus-summary-same-subject
3822                  subject))
3823               (t gnus-summary-same-subject)))
3824             (if (and (eq gnus-summary-make-false-root 'adopt)
3825                      (= gnus-tmp-level 1)
3826                      (memq number gnus-tmp-gathered))
3827                 (setq gnus-tmp-opening-bracket ?\<
3828                       gnus-tmp-closing-bracket ?\>)
3829               (setq gnus-tmp-opening-bracket ?\[
3830                     gnus-tmp-closing-bracket ?\]))
3831             (setq
3832              gnus-tmp-indentation
3833              (aref gnus-thread-indent-array gnus-tmp-level)
3834              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3835              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3836                                 gnus-summary-default-score 0)
3837              gnus-tmp-score-char
3838              (if (or (null gnus-summary-default-score)
3839                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3840                          gnus-summary-zcore-fuzz))
3841                  ? ;Whitespace
3842                (if (< gnus-tmp-score gnus-summary-default-score)
3843                    gnus-score-below-mark gnus-score-over-mark))
3844              gnus-tmp-replied
3845              (cond ((memq number gnus-newsgroup-processable)
3846                     gnus-process-mark)
3847                    ((memq number gnus-newsgroup-cached)
3848                     gnus-cached-mark)
3849                    ((memq number gnus-newsgroup-replied)
3850                     gnus-replied-mark)
3851                    ((memq number gnus-newsgroup-saved)
3852                     gnus-saved-mark)
3853                    (t gnus-unread-mark))
3854              gnus-tmp-from (mail-header-from gnus-tmp-header)
3855              gnus-tmp-name
3856              (cond
3857               ((string-match "<[^>]+> *$" gnus-tmp-from)
3858                (setq beg-match (match-beginning 0))
3859                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3860                         (substring gnus-tmp-from (1+ (match-beginning 0))
3861                                    (1- (match-end 0))))
3862                    (substring gnus-tmp-from 0 beg-match)))
3863               ((string-match "(.+)" gnus-tmp-from)
3864                (substring gnus-tmp-from
3865                           (1+ (match-beginning 0)) (1- (match-end 0))))
3866               (t gnus-tmp-from)))
3867             (when (string= gnus-tmp-name "")
3868               (setq gnus-tmp-name gnus-tmp-from))
3869             (unless (numberp gnus-tmp-lines)
3870               (setq gnus-tmp-lines 0))
3871             (gnus-put-text-property
3872              (point)
3873              (progn (eval gnus-summary-line-format-spec) (point))
3874              'gnus-number number)
3875             (when gnus-visual-p
3876               (forward-line -1)
3877               (gnus-run-hooks 'gnus-summary-update-hook)
3878               (forward-line 1))
3879
3880             (setq gnus-tmp-prev-subject subject)))
3881
3882         (when (nth 1 thread)
3883           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3884         (incf gnus-tmp-level)
3885         (setq threads (if thread-end nil (cdar thread)))
3886         (unless threads
3887           (setq gnus-tmp-level 0)))))
3888   (gnus-message 7 "Generating summary...done"))
3889
3890 (defun gnus-summary-prepare-unthreaded (headers)
3891   "Generate an unthreaded summary buffer based on HEADERS."
3892   (let (header number mark)
3893
3894     (beginning-of-line)
3895
3896     (while headers
3897       ;; We may have to root out some bad articles...
3898       (when (memq (setq number (mail-header-number
3899                                 (setq header (pop headers))))
3900                   gnus-newsgroup-limit)
3901         ;; Mark article as read when it has a low score.
3902         (when (and gnus-summary-mark-below
3903                    (< (or (cdr (assq number gnus-newsgroup-scored))
3904                           gnus-summary-default-score 0)
3905                       gnus-summary-mark-below)
3906                    (not (gnus-summary-article-ancient-p number)))
3907           (setq gnus-newsgroup-unreads
3908                 (delq number gnus-newsgroup-unreads))
3909           (if gnus-newsgroup-auto-expire
3910               (push number gnus-newsgroup-expirable)
3911             (push (cons number gnus-low-score-mark)
3912                   gnus-newsgroup-reads)))
3913
3914         (setq mark (gnus-article-mark number))
3915         (push (gnus-data-make number mark (1+ (point)) header 0)
3916               gnus-newsgroup-data)
3917         (gnus-summary-insert-line
3918          header 0 number
3919          mark (memq number gnus-newsgroup-replied)
3920          (memq number gnus-newsgroup-expirable)
3921          (mail-header-subject header) nil
3922          (cdr (assq number gnus-newsgroup-scored))
3923          (memq number gnus-newsgroup-processable))))))
3924
3925 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3926   "Select newsgroup GROUP.
3927 If READ-ALL is non-nil, all articles in the group are selected.
3928 If SELECT-ARTICLES, only select those articles from GROUP."
3929   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3930          ;;!!! Dirty hack; should be removed.
3931          (gnus-summary-ignore-duplicates
3932           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3933               t
3934             gnus-summary-ignore-duplicates))
3935          (info (nth 2 entry))
3936          articles fetched-articles cached)
3937
3938     (unless (gnus-check-server
3939              (setq gnus-current-select-method
3940                    (gnus-find-method-for-group group)))
3941       (error "Couldn't open server"))
3942
3943     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3944         (gnus-activate-group group)     ; Or we can activate it...
3945         (progn                          ; Or we bug out.
3946           (when (equal major-mode 'gnus-summary-mode)
3947             (kill-buffer (current-buffer)))
3948           (error "Couldn't request group %s: %s"
3949                  group (gnus-status-message group))))
3950
3951     (unless (gnus-request-group group t)
3952       (when (equal major-mode 'gnus-summary-mode)
3953         (kill-buffer (current-buffer)))
3954       (error "Couldn't request group %s: %s"
3955              group (gnus-status-message group)))
3956
3957     (setq gnus-newsgroup-name group)
3958     (setq gnus-newsgroup-unselected nil)
3959     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3960     (gnus-summary-setup-default-charset)
3961
3962     ;; Adjust and set lists of article marks.
3963     (when info
3964       (gnus-adjust-marked-articles info))
3965
3966     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3967     (when (gnus-virtual-group-p group)
3968       (setq cached gnus-newsgroup-cached))
3969
3970     (setq gnus-newsgroup-unreads
3971           (gnus-set-difference
3972            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3973            gnus-newsgroup-dormant))
3974
3975     (setq gnus-newsgroup-processable nil)
3976
3977     (gnus-update-read-articles group gnus-newsgroup-unreads)
3978
3979     (if (setq articles select-articles)
3980         (setq gnus-newsgroup-unselected
3981               (gnus-sorted-intersection
3982                gnus-newsgroup-unreads
3983                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3984       (setq articles (gnus-articles-to-read group read-all)))
3985
3986     (cond
3987      ((null articles)
3988       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3989       'quit)
3990      ((eq articles 0) nil)
3991      (t
3992       ;; Init the dependencies hash table.
3993       (setq gnus-newsgroup-dependencies
3994             (gnus-make-hashtable (length articles)))
3995       (gnus-set-global-variables)
3996       ;; Retrieve the headers and read them in.
3997       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3998       (setq gnus-newsgroup-headers
3999             (gnus-retrieve-parsed-headers
4000              articles gnus-newsgroup-name
4001              ;; We might want to fetch old headers, but
4002              ;; not if there is only 1 article.
4003              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4004                            (not (numberp gnus-fetch-old-headers)))
4005                       (> (length articles) 1))
4006                   gnus-fetch-old-headers)))
4007       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4008
4009       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4010       (when cached
4011         (setq gnus-newsgroup-cached cached))
4012
4013       ;; Suppress duplicates?
4014       (when gnus-suppress-duplicates
4015         (gnus-dup-suppress-articles))
4016
4017       ;; Set the initial limit.
4018       (setq gnus-newsgroup-limit (copy-sequence articles))
4019       ;; Remove canceled articles from the list of unread articles.
4020       (setq gnus-newsgroup-unreads
4021             (gnus-set-sorted-intersection
4022              gnus-newsgroup-unreads
4023              (setq fetched-articles
4024                    (mapcar (lambda (headers) (mail-header-number headers))
4025                            gnus-newsgroup-headers))))
4026       ;; Removed marked articles that do not exist.
4027       (gnus-update-missing-marks
4028        (gnus-sorted-complement fetched-articles articles))
4029       ;; We might want to build some more threads first.
4030       (when (and gnus-fetch-old-headers
4031                  (eq gnus-headers-retrieved-by 'nov))
4032         (if (eq gnus-fetch-old-headers 'invisible)
4033             (gnus-build-all-threads)
4034           (gnus-build-old-threads)))
4035       ;; Let the Gnus agent mark articles as read.
4036       (when gnus-agent
4037         (gnus-agent-get-undownloaded-list))
4038       ;; Check whether auto-expire is to be done in this group.
4039       (setq gnus-newsgroup-auto-expire
4040             (gnus-group-auto-expirable-p group))
4041       ;; Set up the article buffer now, if necessary.
4042       (unless gnus-single-article-buffer
4043         (gnus-article-setup-buffer))
4044       ;; First and last article in this newsgroup.
4045       (when gnus-newsgroup-headers
4046         (setq gnus-newsgroup-begin
4047               (mail-header-number (car gnus-newsgroup-headers))
4048               gnus-newsgroup-end
4049               (mail-header-number
4050                (gnus-last-element gnus-newsgroup-headers))))
4051       ;; GROUP is successfully selected.
4052       (or gnus-newsgroup-headers t)))))
4053
4054 (defun gnus-articles-to-read (group &optional read-all)
4055   ;; Find out what articles the user wants to read.
4056   (let* ((articles
4057           ;; Select all articles if `read-all' is non-nil, or if there
4058           ;; are no unread articles.
4059           (if (or read-all
4060                   (and (zerop (length gnus-newsgroup-marked))
4061                        (zerop (length gnus-newsgroup-unreads)))
4062                   (eq (gnus-group-find-parameter group 'display)
4063                       'all))
4064               (gnus-uncompress-range (gnus-active group))
4065             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4066                           (copy-sequence gnus-newsgroup-unreads))
4067                   '<)))
4068          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4069          (scored (length scored-list))
4070          (number (length articles))
4071          (marked (+ (length gnus-newsgroup-marked)
4072                     (length gnus-newsgroup-dormant)))
4073          (select
4074           (cond
4075            ((numberp read-all)
4076             read-all)
4077            (t
4078             (condition-case ()
4079                 (cond
4080                  ((and (or (<= scored marked) (= scored number))
4081                        (natnump gnus-large-newsgroup)
4082                        (> number gnus-large-newsgroup))
4083                   (let* ((minibuffer-setup-hook (append
4084                                                  minibuffer-setup-hook
4085                                                  '(beginning-of-line)))
4086                          (input (read-string
4087                                  (format
4088                                   "How many articles from %s (max %d): "
4089                                   (gnus-limit-string gnus-newsgroup-name 35)
4090                                   number)
4091                                  (number-to-string gnus-large-newsgroup))))
4092                     (if (string-match "^[ \t]*$" input)
4093                         number
4094                       input)))
4095                  ((and (> scored marked) (< scored number)
4096                        (> (- scored number) 20))
4097                   (let ((input
4098                          (read-string
4099                           (format "%s %s (%d scored, %d total): "
4100                                   "How many articles from"
4101                                   group scored number))))
4102                     (if (string-match "^[ \t]*$" input)
4103                         number input)))
4104                  (t number))
4105               (quit nil))))))
4106     (setq select (if (stringp select) (string-to-number select) select))
4107     (if (or (null select) (zerop select))
4108         select
4109       (if (and (not (zerop scored)) (<= (abs select) scored))
4110           (progn
4111             (setq articles (sort scored-list '<))
4112             (setq number (length articles)))
4113         (setq articles (copy-sequence articles)))
4114
4115       (when (< (abs select) number)
4116         (if (< select 0)
4117             ;; Select the N oldest articles.
4118             (setcdr (nthcdr (1- (abs select)) articles) nil)
4119           ;; Select the N most recent articles.
4120           (setq articles (nthcdr (- number select) articles))))
4121       (setq gnus-newsgroup-unselected
4122             (gnus-sorted-intersection
4123              gnus-newsgroup-unreads
4124              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4125       articles)))
4126
4127 (defun gnus-killed-articles (killed articles)
4128   (let (out)
4129     (while articles
4130       (when (inline (gnus-member-of-range (car articles) killed))
4131         (push (car articles) out))
4132       (setq articles (cdr articles)))
4133     out))
4134
4135 (defun gnus-uncompress-marks (marks)
4136   "Uncompress the mark ranges in MARKS."
4137   (let ((uncompressed '(score bookmark))
4138         out)
4139     (while marks
4140       (if (memq (caar marks) uncompressed)
4141           (push (car marks) out)
4142         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4143       (setq marks (cdr marks)))
4144     out))
4145
4146 (defun gnus-adjust-marked-articles (info)
4147   "Set all article lists and remove all marks that are no longer valid."
4148   (let* ((marked-lists (gnus-info-marks info))
4149          (active (gnus-active (gnus-info-group info)))
4150          (min (car active))
4151          (max (cdr active))
4152          (types gnus-article-mark-lists)
4153          (uncompressed '(score bookmark killed))
4154          marks var articles article mark)
4155
4156     (while marked-lists
4157       (setq marks (pop marked-lists))
4158       (set (setq var (intern (format "gnus-newsgroup-%s"
4159                                      (car (rassq (setq mark (car marks))
4160                                                  types)))))
4161            (if (memq (car marks) uncompressed) (cdr marks)
4162              (gnus-uncompress-range (cdr marks))))
4163
4164       (setq articles (symbol-value var))
4165
4166       ;; All articles have to be subsets of the active articles.
4167       (cond
4168        ;; Adjust "simple" lists.
4169        ((memq mark '(tick dormant expire reply save))
4170         (while articles
4171           (when (or (< (setq article (pop articles)) min) (> article max))
4172             (set var (delq article (symbol-value var))))))
4173        ;; Adjust assocs.
4174        ((memq mark uncompressed)
4175         (when (not (listp (cdr (symbol-value var))))
4176           (set var (list (symbol-value var))))
4177         (when (not (listp (cdr articles)))
4178           (setq articles (list articles)))
4179         (while articles
4180           (when (or (not (consp (setq article (pop articles))))
4181                     (< (car article) min)
4182                     (> (car article) max))
4183             (set var (delq article (symbol-value var))))))))))
4184
4185 (defun gnus-update-missing-marks (missing)
4186   "Go through the list of MISSING articles and remove them from the mark lists."
4187   (when missing
4188     (let ((types gnus-article-mark-lists)
4189           var m)
4190       ;; Go through all types.
4191       (while types
4192         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4193         (when (symbol-value var)
4194           ;; This list has articles.  So we delete all missing articles
4195           ;; from it.
4196           (setq m missing)
4197           (while m
4198             (set var (delq (pop m) (symbol-value var)))))))))
4199
4200 (defun gnus-update-marks ()
4201   "Enter the various lists of marked articles into the newsgroup info list."
4202   (let ((types gnus-article-mark-lists)
4203         (info (gnus-get-info gnus-newsgroup-name))
4204         (uncompressed '(score bookmark killed))
4205         type list newmarked symbol delta-marks)
4206     (when info
4207       ;; Add all marks lists that are non-nil to the list of marks lists.
4208       (while (setq type (pop types))
4209         (when (setq list (symbol-value
4210                           (setq symbol
4211                                 (intern (format "gnus-newsgroup-%s"
4212                                                 (car type))))))
4213
4214           ;; Get rid of the entries of the articles that have the
4215           ;; default score.
4216           (when (and (eq (cdr type) 'score)
4217                      gnus-save-score
4218                      list)
4219             (let* ((arts list)
4220                    (prev (cons nil list))
4221                    (all prev))
4222               (while arts
4223                 (if (or (not (consp (car arts)))
4224                         (= (cdar arts) gnus-summary-default-score))
4225                     (setcdr prev (cdr arts))
4226                   (setq prev arts))
4227                 (setq arts (cdr arts)))
4228               (setq list (cdr all))))
4229
4230          (when (gnus-check-backend-function 'request-set-mark
4231                                             gnus-newsgroup-name)
4232            ;; score & bookmark are not proper flags (they are cons cells)
4233            ;; cache is a internal gnus flag
4234            (unless (memq (cdr type) '(cache score bookmark))
4235              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4236                     (del (gnus-remove-from-range old list))
4237                     (add (gnus-remove-from-range list old)))
4238                (if add
4239                    (push (list add 'add (list (cdr type))) delta-marks))
4240                (if del
4241                    (push (list del 'del (list (cdr type))) delta-marks)))))
4242
4243           (push (cons (cdr type)
4244                       (if (memq (cdr type) uncompressed) list
4245                         (gnus-compress-sequence
4246                          (set symbol (sort list '<)) t)))
4247                 newmarked)))
4248
4249       (if delta-marks
4250          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4251
4252       ;; Enter these new marks into the info of the group.
4253       (if (nthcdr 3 info)
4254           (setcar (nthcdr 3 info) newmarked)
4255         ;; Add the marks lists to the end of the info.
4256         (when newmarked
4257           (setcdr (nthcdr 2 info) (list newmarked))))
4258
4259       ;; Cut off the end of the info if there's nothing else there.
4260       (let ((i 5))
4261         (while (and (> i 2)
4262                     (not (nth i info)))
4263           (when (nthcdr (decf i) info)
4264             (setcdr (nthcdr i info) nil)))))))
4265
4266 (defun gnus-set-mode-line (where)
4267   "This function sets the mode line of the article or summary buffers.
4268 If WHERE is `summary', the summary mode line format will be used."
4269   ;; Is this mode line one we keep updated?
4270   (when (and (memq where gnus-updated-mode-lines)
4271              (symbol-value
4272               (intern (format "gnus-%s-mode-line-format-spec" where))))
4273     (let (mode-string)
4274       (save-excursion
4275         ;; We evaluate this in the summary buffer since these
4276         ;; variables are buffer-local to that buffer.
4277         (set-buffer gnus-summary-buffer)
4278         ;; We bind all these variables that are used in the `eval' form
4279         ;; below.
4280         (let* ((mformat (symbol-value
4281                          (intern
4282                           (format "gnus-%s-mode-line-format-spec" where))))
4283                (gnus-tmp-group-name gnus-newsgroup-name)
4284                (gnus-tmp-article-number (or gnus-current-article 0))
4285                (gnus-tmp-unread gnus-newsgroup-unreads)
4286                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4287                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4288                (gnus-tmp-unread-and-unselected
4289                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4290                             (zerop gnus-tmp-unselected))
4291                        "")
4292                       ((zerop gnus-tmp-unselected)
4293                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4294                       (t (format "{%d(+%d) more}"
4295                                  gnus-tmp-unread-and-unticked
4296                                  gnus-tmp-unselected))))
4297                (gnus-tmp-subject
4298                 (if (and gnus-current-headers
4299                          (vectorp gnus-current-headers))
4300                     (gnus-mode-string-quote
4301                      (mail-header-subject gnus-current-headers))
4302                   ""))
4303                bufname-length max-len
4304                gnus-tmp-header);; passed as argument to any user-format-funcs
4305           (setq mode-string (eval mformat))
4306           (setq bufname-length (if (string-match "%b" mode-string)
4307                                    (- (length
4308                                        (buffer-name
4309                                         (if (eq where 'summary)
4310                                             nil
4311                                           (get-buffer gnus-article-buffer))))
4312                                       2)
4313                                  0))
4314           (setq max-len (max 4 (if gnus-mode-non-string-length
4315                                    (- (window-width)
4316                                       gnus-mode-non-string-length
4317                                       bufname-length)
4318                                  (length mode-string))))
4319           ;; We might have to chop a bit of the string off...
4320           (when (> (length mode-string) max-len)
4321             (setq mode-string
4322                   (concat (gnus-truncate-string mode-string (- max-len 3))
4323                           "...")))
4324           ;; Pad the mode string a bit.
4325           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4326       ;; Update the mode line.
4327       (setq mode-line-buffer-identification
4328             (gnus-mode-line-buffer-identification (list mode-string)))
4329       (set-buffer-modified-p t))))
4330
4331 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4332   "Go through the HEADERS list and add all Xrefs to a hash table.
4333 The resulting hash table is returned, or nil if no Xrefs were found."
4334   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4335          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4336          (xref-hashtb (gnus-make-hashtable))
4337          start group entry number xrefs header)
4338     (while headers
4339       (setq header (pop headers))
4340       (when (and (setq xrefs (mail-header-xref header))
4341                  (not (memq (setq number (mail-header-number header))
4342                             unreads)))
4343         (setq start 0)
4344         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4345           (setq start (match-end 0))
4346           (setq group (if prefix
4347                           (concat prefix (substring xrefs (match-beginning 1)
4348                                                     (match-end 1)))
4349                         (substring xrefs (match-beginning 1) (match-end 1))))
4350           (setq number
4351                 (string-to-int (substring xrefs (match-beginning 2)
4352                                           (match-end 2))))
4353           (if (setq entry (gnus-gethash group xref-hashtb))
4354               (setcdr entry (cons number (cdr entry)))
4355             (gnus-sethash group (cons number nil) xref-hashtb)))))
4356     (and start xref-hashtb)))
4357
4358 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4359   "Look through all the headers and mark the Xrefs as read."
4360   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4361         name entry info xref-hashtb idlist method nth4)
4362     (save-excursion
4363       (set-buffer gnus-group-buffer)
4364       (when (setq xref-hashtb
4365                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4366         (mapatoms
4367          (lambda (group)
4368            (unless (string= from-newsgroup (setq name (symbol-name group)))
4369              (setq idlist (symbol-value group))
4370              ;; Dead groups are not updated.
4371              (and (prog1
4372                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4373                             info (nth 2 entry))
4374                     (when (stringp (setq nth4 (gnus-info-method info)))
4375                       (setq nth4 (gnus-server-to-method nth4))))
4376                   ;; Only do the xrefs if the group has the same
4377                   ;; select method as the group we have just read.
4378                   (or (gnus-methods-equal-p
4379                        nth4 (gnus-find-method-for-group from-newsgroup))
4380                       virtual
4381                       (equal nth4 (setq method (gnus-find-method-for-group
4382                                                 from-newsgroup)))
4383                       (and (equal (car nth4) (car method))
4384                            (equal (nth 1 nth4) (nth 1 method))))
4385                   gnus-use-cross-reference
4386                   (or (not (eq gnus-use-cross-reference t))
4387                       virtual
4388                       ;; Only do cross-references on subscribed
4389                       ;; groups, if that is what is wanted.
4390                       (<= (gnus-info-level info) gnus-level-subscribed))
4391                   (gnus-group-make-articles-read name idlist))))
4392          xref-hashtb)))))
4393
4394 (defun gnus-compute-read-articles (group articles)
4395   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4396          (info (nth 2 entry))
4397          (active (gnus-active group))
4398          ninfo)
4399     (when entry
4400       ;; First peel off all invalid article numbers.
4401       (when active
4402         (let ((ids articles)
4403               id first)
4404           (while (setq id (pop ids))
4405             (when (and first (> id (cdr active)))
4406               ;; We'll end up in this situation in one particular
4407               ;; obscure situation.  If you re-scan a group and get
4408               ;; a new article that is cross-posted to a different
4409               ;; group that has not been re-scanned, you might get
4410               ;; crossposted article that has a higher number than
4411               ;; Gnus believes possible.  So we re-activate this
4412               ;; group as well.  This might mean doing the
4413               ;; crossposting thingy will *increase* the number
4414               ;; of articles in some groups.  Tsk, tsk.
4415               (setq active (or (gnus-activate-group group) active)))
4416             (when (or (> id (cdr active))
4417                       (< id (car active)))
4418               (setq articles (delq id articles))))))
4419       ;; If the read list is nil, we init it.
4420       (if (and active
4421                (null (gnus-info-read info))
4422                (> (car active) 1))
4423           (setq ninfo (cons 1 (1- (car active))))
4424         (setq ninfo (gnus-info-read info)))
4425       ;; Then we add the read articles to the range.
4426       (gnus-add-to-range
4427        ninfo (setq articles (sort articles '<))))))
4428
4429 (defun gnus-group-make-articles-read (group articles)
4430   "Update the info of GROUP to say that ARTICLES are read."
4431   (let* ((num 0)
4432          (entry (gnus-gethash group gnus-newsrc-hashtb))
4433          (info (nth 2 entry))
4434          (active (gnus-active group))
4435          range)
4436     (when entry
4437       (setq range (gnus-compute-read-articles group articles))
4438       (save-excursion
4439         (set-buffer gnus-group-buffer)
4440         (gnus-undo-register
4441           `(progn
4442              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4443              (gnus-info-set-read ',info ',(gnus-info-read info))
4444              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4445              (gnus-group-update-group ,group t))))
4446       ;; Add the read articles to the range.
4447       (gnus-info-set-read info range)
4448       ;; Then we have to re-compute how many unread
4449       ;; articles there are in this group.
4450       (when active
4451         (cond
4452          ((not range)
4453           (setq num (- (1+ (cdr active)) (car active))))
4454          ((not (listp (cdr range)))
4455           (setq num (- (cdr active) (- (1+ (cdr range))
4456                                        (car range)))))
4457          (t
4458           (while range
4459             (if (numberp (car range))
4460                 (setq num (1+ num))
4461               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4462             (setq range (cdr range)))
4463           (setq num (- (cdr active) num))))
4464         ;; Update the number of unread articles.
4465         (setcar entry num)
4466         ;; Update the group buffer.
4467         (gnus-group-update-group group t)))))
4468
4469 (defun gnus-methods-equal-p (m1 m2)
4470   (let ((m1 (or m1 gnus-select-method))
4471         (m2 (or m2 gnus-select-method)))
4472     (or (equal m1 m2)
4473         (and (eq (car m1) (car m2))
4474              (or (not (memq 'address (assoc (symbol-name (car m1))
4475                                             gnus-valid-select-methods)))
4476                  (equal (nth 1 m1) (nth 1 m2)))))))
4477
4478 (defvar gnus-newsgroup-none-id 0)
4479
4480 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4481   (let ((cur nntp-server-buffer)
4482         (dependencies
4483          (or dependencies
4484              (save-excursion (set-buffer gnus-summary-buffer)
4485                              gnus-newsgroup-dependencies)))
4486         headers id end ref
4487         (mail-parse-charset gnus-newsgroup-charset))
4488     (save-excursion
4489       (set-buffer nntp-server-buffer)
4490       ;; Translate all TAB characters into SPACE characters.
4491       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4492       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4493       (gnus-run-hooks 'gnus-parse-headers-hook)
4494       (let ((case-fold-search t)
4495             in-reply-to header p lines chars ctype)
4496         (goto-char (point-min))
4497         ;; Search to the beginning of the next header.  Error messages
4498         ;; do not begin with 2 or 3.
4499         (while (re-search-forward "^[23][0-9]+ " nil t)
4500           (setq id nil
4501                 ref nil)
4502           ;; This implementation of this function, with nine
4503           ;; search-forwards instead of the one re-search-forward and
4504           ;; a case (which basically was the old function) is actually
4505           ;; about twice as fast, even though it looks messier.  You
4506           ;; can't have everything, I guess.  Speed and elegance
4507           ;; doesn't always go hand in hand.
4508           (setq
4509            header
4510            (make-full-mail-header
4511             ;; Number.
4512             (prog1
4513                 (read cur)
4514               (end-of-line)
4515               (setq p (point))
4516               (narrow-to-region (point)
4517                                 (or (and (search-forward "\n.\n" nil t)
4518                                          (- (point) 2))
4519                                     (point))))
4520             ;; Subject.
4521             (progn
4522               (goto-char p)
4523               (if (search-forward "\nsubject: " nil t)
4524                   (buffer-substring (match-end 0) (std11-field-end))
4525                 "(none)"))
4526             ;; From.
4527             (progn
4528               (goto-char p)
4529               (if (search-forward "\nfrom: " nil t)
4530                   (buffer-substring (match-end 0) (std11-field-end))
4531                 "(nobody)"))
4532             ;; Date.
4533             (progn
4534               (goto-char p)
4535               (if (search-forward "\ndate: " nil t)
4536                   (buffer-substring (match-end 0) (std11-field-end))
4537                 ""))
4538             ;; Message-ID.
4539             (progn
4540               (goto-char p)
4541               (setq id (if (re-search-forward
4542                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4543                            ;; We do it this way to make sure the Message-ID
4544                            ;; is (somewhat) syntactically valid.
4545                            (buffer-substring (match-beginning 1)
4546                                              (match-end 1))
4547                          ;; If there was no message-id, we just fake one
4548                          ;; to make subsequent routines simpler.
4549                          (nnheader-generate-fake-message-id))))
4550             ;; References.
4551             (progn
4552               (goto-char p)
4553               (if (search-forward "\nreferences: " nil t)
4554                   (progn
4555                     (setq end (point))
4556                     (prog1
4557                         (buffer-substring (match-end 0) (std11-field-end))
4558                       (setq ref
4559                             (buffer-substring
4560                              (progn
4561                                ;; (end-of-line)
4562                                (search-backward ">" end t)
4563                                (1+ (point)))
4564                              (progn
4565                                (search-backward "<" end t)
4566                                (point))))))
4567                 ;; Get the references from the in-reply-to header if there
4568                 ;; were no references and the in-reply-to header looks
4569                 ;; promising.
4570                 (if (and (search-forward "\nin-reply-to: " nil t)
4571                          (setq in-reply-to
4572                                (buffer-substring (match-end 0)
4573                                                  (std11-field-end)))
4574                          (string-match "<[^>]+>" in-reply-to))
4575                     (let (ref2)
4576                       (setq ref (substring in-reply-to (match-beginning 0)
4577                                            (match-end 0)))
4578                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4579                         (setq ref2 (substring in-reply-to (match-beginning 0)
4580                                               (match-end 0)))
4581                         (when (> (length ref2) (length ref))
4582                           (setq ref ref2)))
4583                       ref)
4584                   (setq ref nil))))
4585             ;; Chars.
4586             (progn
4587               (goto-char p)
4588               (if (search-forward "\nchars: " nil t)
4589                   (if (numberp (setq chars (ignore-errors (read cur))))
4590                       chars 0)
4591                 0))
4592             ;; Lines.
4593             (progn
4594               (goto-char p)
4595               (if (search-forward "\nlines: " nil t)
4596                   (if (numberp (setq lines (ignore-errors (read cur))))
4597                       lines 0)
4598                 0))
4599             ;; Xref.
4600             (progn
4601               (goto-char p)
4602               (and (search-forward "\nxref: " nil t)
4603                    (buffer-substring (match-end 0) (std11-field-end))))
4604             ;; Extra.
4605             (when gnus-extra-headers
4606               (let ((extra gnus-extra-headers)
4607                     out)
4608                 (while extra
4609                   (goto-char p)
4610                   (when (search-forward
4611                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4612                     (push (cons (car extra)
4613                                 (buffer-substring (match-end 0)
4614                                                   (std11-field-end)))
4615                           out))
4616                   (pop extra))
4617                 out))))
4618           (goto-char p)
4619           (if (and (search-forward "\ncontent-type: " nil t)
4620                    (setq ctype
4621                          (buffer-substring (match-end 0) (std11-field-end))))
4622               (mime-entity-set-content-type-internal
4623                header (mime-parse-Content-Type ctype)))
4624           (when (equal id ref)
4625             (setq ref nil))
4626
4627           (when gnus-alter-header-function
4628             (funcall gnus-alter-header-function header)
4629             (setq id (mail-header-id header)
4630                   ref (gnus-parent-id (mail-header-references header))))
4631
4632           (when (setq header
4633                       (gnus-dependencies-add-header
4634                        header dependencies force-new))
4635             (push header headers))
4636           (goto-char (point-max))
4637           (widen))
4638         (nreverse headers)))))
4639
4640 ;; Goes through the xover lines and returns a list of vectors
4641 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4642                                                   force-new dependencies
4643                                                   group also-fetch-heads)
4644   "Parse the news overview data in the server buffer, and return a
4645 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4646   ;; Get the Xref when the users reads the articles since most/some
4647   ;; NNTP servers do not include Xrefs when using XOVER.
4648   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4649   (let ((mail-parse-charset gnus-newsgroup-charset)
4650         (cur nntp-server-buffer)
4651         (dependencies (or dependencies gnus-newsgroup-dependencies))
4652         number headers header)
4653     (save-excursion
4654       (set-buffer nntp-server-buffer)
4655       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4656       ;; Allow the user to mangle the headers before parsing them.
4657       (gnus-run-hooks 'gnus-parse-headers-hook)
4658       (goto-char (point-min))
4659       (while (not (eobp))
4660         (condition-case ()
4661             (while (and sequence (not (eobp)))
4662               (setq number (read cur))
4663               (while (and sequence
4664                           (< (car sequence) number))
4665                 (setq sequence (cdr sequence)))
4666               (and sequence
4667                    (eq number (car sequence))
4668                    (progn
4669                      (setq sequence (cdr sequence))
4670                      (setq header (inline
4671                                     (gnus-nov-parse-line
4672                                      number dependencies force-new))))
4673                    (push header headers))
4674               (forward-line 1))
4675           (error
4676            (gnus-error 4 "Strange nov line (%d)"
4677                        (count-lines (point-min) (point)))))
4678         (forward-line 1))
4679       ;; A common bug in inn is that if you have posted an article and
4680       ;; then retrieves the active file, it will answer correctly --
4681       ;; the new article is included.  However, a NOV entry for the
4682       ;; article may not have been generated yet, so this may fail.
4683       ;; We work around this problem by retrieving the last few
4684       ;; headers using HEAD.
4685       (if (or (not also-fetch-heads)
4686               (not sequence))
4687           ;; We (probably) got all the headers.
4688           (nreverse headers)
4689         (let ((gnus-nov-is-evil t))
4690           (nconc
4691            (nreverse headers)
4692            (gnus-retrieve-parsed-headers sequence group)
4693            ))))))
4694
4695 (defun gnus-article-get-xrefs ()
4696   "Fill in the Xref value in `gnus-current-headers', if necessary.
4697 This is meant to be called in `gnus-article-internal-prepare-hook'."
4698   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4699                                  gnus-current-headers)))
4700     (or (not gnus-use-cross-reference)
4701         (not headers)
4702         (and (mail-header-xref headers)
4703              (not (string= (mail-header-xref headers) "")))
4704         (let ((case-fold-search t)
4705               xref)
4706           (save-restriction
4707             (nnheader-narrow-to-headers)
4708             (goto-char (point-min))
4709             (when (or (and (eq (downcase (char-after)) ?x)
4710                            (looking-at "Xref:"))
4711                       (search-forward "\nXref:" nil t))
4712               (goto-char (1+ (match-end 0)))
4713               (setq xref (buffer-substring (point)
4714                                            (progn (end-of-line) (point))))
4715               (mail-header-set-xref headers xref)))))))
4716
4717 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4718   "Find article ID and insert the summary line for that article.
4719 OLD-HEADER can either be a header or a line number to insert
4720 the subject line on."
4721   (let* ((line (and (numberp old-header) old-header))
4722          (old-header (and (vectorp old-header) old-header))
4723          (header (cond ((and old-header use-old-header)
4724                         old-header)
4725                        ((and (numberp id)
4726                              (gnus-number-to-header id))
4727                         (gnus-number-to-header id))
4728                        (t
4729                         (gnus-read-header id))))
4730          (number (and (numberp id) id))
4731          d)
4732     (when header
4733       ;; Rebuild the thread that this article is part of and go to the
4734       ;; article we have fetched.
4735       (when (and (not gnus-show-threads)
4736                  old-header)
4737         (when (and number
4738                    (setq d (gnus-data-find (mail-header-number old-header))))
4739           (goto-char (gnus-data-pos d))
4740           (gnus-data-remove
4741            number
4742            (- (gnus-point-at-bol)
4743               (prog1
4744                   (1+ (gnus-point-at-eol))
4745                 (gnus-delete-line))))))
4746       (when old-header
4747         (mail-header-set-number header (mail-header-number old-header)))
4748       (setq gnus-newsgroup-sparse
4749             (delq (setq number (mail-header-number header))
4750                   gnus-newsgroup-sparse))
4751       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4752       (push number gnus-newsgroup-limit)
4753       (gnus-rebuild-thread (mail-header-id header) line)
4754       (gnus-summary-goto-subject number nil t))
4755     (when (and (numberp number)
4756                (> number 0))
4757       ;; We have to update the boundaries even if we can't fetch the
4758       ;; article if ID is a number -- so that the next `P' or `N'
4759       ;; command will fetch the previous (or next) article even
4760       ;; if the one we tried to fetch this time has been canceled.
4761       (when (> number gnus-newsgroup-end)
4762         (setq gnus-newsgroup-end number))
4763       (when (< number gnus-newsgroup-begin)
4764         (setq gnus-newsgroup-begin number))
4765       (setq gnus-newsgroup-unselected
4766             (delq number gnus-newsgroup-unselected)))
4767     ;; Report back a success?
4768     (and header (mail-header-number header))))
4769
4770 ;;; Process/prefix in the summary buffer
4771
4772 (defun gnus-summary-work-articles (n)
4773   "Return a list of articles to be worked upon.
4774 The prefix argument, the list of process marked articles, and the
4775 current article will be taken into consideration."
4776   (save-excursion
4777     (set-buffer gnus-summary-buffer)
4778     (cond
4779      (n
4780       ;; A numerical prefix has been given.
4781       (setq n (prefix-numeric-value n))
4782       (let ((backward (< n 0))
4783             (n (abs (prefix-numeric-value n)))
4784             articles article)
4785         (save-excursion
4786           (while
4787               (and (> n 0)
4788                    (push (setq article (gnus-summary-article-number))
4789                          articles)
4790                    (if backward
4791                        (gnus-summary-find-prev nil article)
4792                      (gnus-summary-find-next nil article)))
4793             (decf n)))
4794         (nreverse articles)))
4795      ((and (gnus-region-active-p) (mark))
4796       (message "region active")
4797       ;; Work on the region between point and mark.
4798       (let ((max (max (point) (mark)))
4799             articles article)
4800         (save-excursion
4801           (goto-char (min (point) (mark)))
4802           (while
4803               (and
4804                (push (setq article (gnus-summary-article-number)) articles)
4805                (gnus-summary-find-next nil article)
4806                (< (point) max)))
4807           (nreverse articles))))
4808      (gnus-newsgroup-processable
4809       ;; There are process-marked articles present.
4810       ;; Save current state.
4811       (gnus-summary-save-process-mark)
4812       ;; Return the list.
4813       (reverse gnus-newsgroup-processable))
4814      (t
4815       ;; Just return the current article.
4816       (list (gnus-summary-article-number))))))
4817
4818 (defmacro gnus-summary-iterate (arg &rest forms)
4819   "Iterate over the process/prefixed articles and do FORMS.
4820 ARG is the interactive prefix given to the command.  FORMS will be
4821 executed with point over the summary line of the articles."
4822   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4823     `(let ((,articles (gnus-summary-work-articles ,arg)))
4824        (while ,articles
4825          (gnus-summary-goto-subject (car ,articles))
4826          ,@forms))))
4827
4828 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4829 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4830
4831 (defun gnus-summary-save-process-mark ()
4832   "Push the current set of process marked articles on the stack."
4833   (interactive)
4834   (push (copy-sequence gnus-newsgroup-processable)
4835         gnus-newsgroup-process-stack))
4836
4837 (defun gnus-summary-kill-process-mark ()
4838   "Push the current set of process marked articles on the stack and unmark."
4839   (interactive)
4840   (gnus-summary-save-process-mark)
4841   (gnus-summary-unmark-all-processable))
4842
4843 (defun gnus-summary-yank-process-mark ()
4844   "Pop the last process mark state off the stack and restore it."
4845   (interactive)
4846   (unless gnus-newsgroup-process-stack
4847     (error "Empty mark stack"))
4848   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4849
4850 (defun gnus-summary-process-mark-set (set)
4851   "Make SET into the current process marked articles."
4852   (gnus-summary-unmark-all-processable)
4853   (while set
4854     (gnus-summary-set-process-mark (pop set))))
4855
4856 ;;; Searching and stuff
4857
4858 (defun gnus-summary-search-group (&optional backward use-level)
4859   "Search for next unread newsgroup.
4860 If optional argument BACKWARD is non-nil, search backward instead."
4861   (save-excursion
4862     (set-buffer gnus-group-buffer)
4863     (when (gnus-group-search-forward
4864            backward nil (if use-level (gnus-group-group-level) nil))
4865       (gnus-group-group-name))))
4866
4867 (defun gnus-summary-best-group (&optional exclude-group)
4868   "Find the name of the best unread group.
4869 If EXCLUDE-GROUP, do not go to this group."
4870   (save-excursion
4871     (set-buffer gnus-group-buffer)
4872     (save-excursion
4873       (gnus-group-best-unread-group exclude-group))))
4874
4875 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4876   (if backward (gnus-summary-find-prev)
4877     (let* ((dummy (gnus-summary-article-intangible-p))
4878            (article (or article (gnus-summary-article-number)))
4879            (arts (gnus-data-find-list article))
4880            result)
4881       (when (and (not dummy)
4882                  (or (not gnus-summary-check-current)
4883                      (not unread)
4884                      (not (gnus-data-unread-p (car arts)))))
4885         (setq arts (cdr arts)))
4886       (when (setq result
4887                   (if unread
4888                       (progn
4889                         (while arts
4890                           (when (or (and undownloaded
4891                                          (eq gnus-undownloaded-mark
4892                                              (gnus-data-mark (car arts))))
4893                                     (gnus-data-unread-p (car arts)))
4894                             (setq result (car arts)
4895                                   arts nil))
4896                           (setq arts (cdr arts)))
4897                         result)
4898                     (car arts)))
4899         (goto-char (gnus-data-pos result))
4900         (gnus-data-number result)))))
4901
4902 (defun gnus-summary-find-prev (&optional unread article)
4903   (let* ((eobp (eobp))
4904          (article (or article (gnus-summary-article-number)))
4905          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4906          result)
4907     (when (and (not eobp)
4908                (or (not gnus-summary-check-current)
4909                    (not unread)
4910                    (not (gnus-data-unread-p (car arts)))))
4911       (setq arts (cdr arts)))
4912     (when (setq result
4913                 (if unread
4914                     (progn
4915                       (while arts
4916                         (when (gnus-data-unread-p (car arts))
4917                           (setq result (car arts)
4918                                 arts nil))
4919                         (setq arts (cdr arts)))
4920                       result)
4921                   (car arts)))
4922       (goto-char (gnus-data-pos result))
4923       (gnus-data-number result))))
4924
4925 (defun gnus-summary-find-subject (subject &optional unread backward article)
4926   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4927          (article (or article (gnus-summary-article-number)))
4928          (articles (gnus-data-list backward))
4929          (arts (gnus-data-find-list article articles))
4930          result)
4931     (when (or (not gnus-summary-check-current)
4932               (not unread)
4933               (not (gnus-data-unread-p (car arts))))
4934       (setq arts (cdr arts)))
4935     (while arts
4936       (and (or (not unread)
4937                (gnus-data-unread-p (car arts)))
4938            (vectorp (gnus-data-header (car arts)))
4939            (gnus-subject-equal
4940             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4941            (setq result (car arts)
4942                  arts nil))
4943       (setq arts (cdr arts)))
4944     (and result
4945          (goto-char (gnus-data-pos result))
4946          (gnus-data-number result))))
4947
4948 (defun gnus-summary-search-forward (&optional unread subject backward)
4949   "Search forward for an article.
4950 If UNREAD, look for unread articles.  If SUBJECT, look for
4951 articles with that subject.  If BACKWARD, search backward instead."
4952   (cond (subject (gnus-summary-find-subject subject unread backward))
4953         (backward (gnus-summary-find-prev unread))
4954         (t (gnus-summary-find-next unread))))
4955
4956 (defun gnus-recenter (&optional n)
4957   "Center point in window and redisplay frame.
4958 Also do horizontal recentering."
4959   (interactive "P")
4960   (when (and gnus-auto-center-summary
4961              (not (eq gnus-auto-center-summary 'vertical)))
4962     (gnus-horizontal-recenter))
4963   (recenter n))
4964
4965 (defun gnus-summary-recenter ()
4966   "Center point in the summary window.
4967 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4968 displayed, no centering will be performed."
4969   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4970   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4971   (let* ((top (cond ((< (window-height) 4) 0)
4972                     ((< (window-height) 7) 1)
4973                     (t (if (numberp gnus-auto-center-summary)
4974                            gnus-auto-center-summary
4975                          2))))
4976          (height (1- (window-height)))
4977          (bottom (save-excursion (goto-char (point-max))
4978                                  (forward-line (- height))
4979                                  (point)))
4980          (window (get-buffer-window (current-buffer))))
4981     ;; The user has to want it.
4982     (when gnus-auto-center-summary
4983       (when (get-buffer-window gnus-article-buffer)
4984         ;; Only do recentering when the article buffer is displayed,
4985         ;; Set the window start to either `bottom', which is the biggest
4986         ;; possible valid number, or the second line from the top,
4987         ;; whichever is the least.
4988         (set-window-start
4989          window (min bottom (save-excursion
4990                               (forward-line (- top)) (point)))))
4991       ;; Do horizontal recentering while we're at it.
4992       (when (and (get-buffer-window (current-buffer) t)
4993                  (not (eq gnus-auto-center-summary 'vertical)))
4994         (let ((selected (selected-window)))
4995           (select-window (get-buffer-window (current-buffer) t))
4996           (gnus-summary-position-point)
4997           (gnus-horizontal-recenter)
4998           (select-window selected))))))
4999
5000 (defun gnus-summary-jump-to-group (newsgroup)
5001   "Move point to NEWSGROUP in group mode buffer."
5002   ;; Keep update point of group mode buffer if visible.
5003   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5004       (save-window-excursion
5005         ;; Take care of tree window mode.
5006         (when (get-buffer-window gnus-group-buffer)
5007           (pop-to-buffer gnus-group-buffer))
5008         (gnus-group-jump-to-group newsgroup))
5009     (save-excursion
5010       ;; Take care of tree window mode.
5011       (if (get-buffer-window gnus-group-buffer)
5012           (pop-to-buffer gnus-group-buffer)
5013         (set-buffer gnus-group-buffer))
5014       (gnus-group-jump-to-group newsgroup))))
5015
5016 ;; This function returns a list of article numbers based on the
5017 ;; difference between the ranges of read articles in this group and
5018 ;; the range of active articles.
5019 (defun gnus-list-of-unread-articles (group)
5020   (let* ((read (gnus-info-read (gnus-get-info group)))
5021          (active (or (gnus-active group) (gnus-activate-group group)))
5022          (last (cdr active))
5023          first nlast unread)
5024     ;; If none are read, then all are unread.
5025     (if (not read)
5026         (setq first (car active))
5027       ;; If the range of read articles is a single range, then the
5028       ;; first unread article is the article after the last read
5029       ;; article.  Sounds logical, doesn't it?
5030       (if (not (listp (cdr read)))
5031           (setq first (max (car active) (1+ (cdr read))))
5032         ;; `read' is a list of ranges.
5033         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5034                                   (caar read)))
5035                   1)
5036           (setq first (car active)))
5037         (while read
5038           (when first
5039             (while (< first nlast)
5040               (push first unread)
5041               (setq first (1+ first))))
5042           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5043           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5044           (setq read (cdr read)))))
5045     ;; And add the last unread articles.
5046     (while (<= first last)
5047       (push first unread)
5048       (setq first (1+ first)))
5049     ;; Return the list of unread articles.
5050     (delq 0 (nreverse unread))))
5051
5052 (defun gnus-list-of-read-articles (group)
5053   "Return a list of unread, unticked and non-dormant articles."
5054   (let* ((info (gnus-get-info group))
5055          (marked (gnus-info-marks info))
5056          (active (gnus-active group)))
5057     (and info active
5058          (gnus-set-difference
5059           (gnus-sorted-complement
5060            (gnus-uncompress-range active)
5061            (gnus-list-of-unread-articles group))
5062           (append
5063            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5064            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5065
5066 ;; Various summary commands
5067
5068 (defun gnus-summary-select-article-buffer ()
5069   "Reconfigure windows to show article buffer."
5070   (interactive)
5071   (if (not (gnus-buffer-live-p gnus-article-buffer))
5072       (error "There is no article buffer for this summary buffer")
5073     (gnus-configure-windows 'article)
5074     (select-window (get-buffer-window gnus-article-buffer))))
5075
5076 (defun gnus-summary-universal-argument (arg)
5077   "Perform any operation on all articles that are process/prefixed."
5078   (interactive "P")
5079   (let ((articles (gnus-summary-work-articles arg))
5080         func article)
5081     (if (eq
5082          (setq
5083           func
5084           (key-binding
5085            (read-key-sequence
5086             (substitute-command-keys
5087              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5088              ))))
5089          'undefined)
5090         (gnus-error 1 "Undefined key")
5091       (save-excursion
5092         (while articles
5093           (gnus-summary-goto-subject (setq article (pop articles)))
5094           (let (gnus-newsgroup-processable)
5095             (command-execute func))
5096           (gnus-summary-remove-process-mark article)))))
5097   (gnus-summary-position-point))
5098
5099 (defun gnus-summary-toggle-truncation (&optional arg)
5100   "Toggle truncation of summary lines.
5101 With arg, turn line truncation on iff arg is positive."
5102   (interactive "P")
5103   (setq truncate-lines
5104         (if (null arg) (not truncate-lines)
5105           (> (prefix-numeric-value arg) 0)))
5106   (redraw-display))
5107
5108 (defun gnus-summary-reselect-current-group (&optional all rescan)
5109   "Exit and then reselect the current newsgroup.
5110 The prefix argument ALL means to select all articles."
5111   (interactive "P")
5112   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5113     (error "Ephemeral groups can't be reselected"))
5114   (let ((current-subject (gnus-summary-article-number))
5115         (group gnus-newsgroup-name))
5116     (setq gnus-newsgroup-begin nil)
5117     (gnus-summary-exit)
5118     ;; We have to adjust the point of group mode buffer because
5119     ;; point was moved to the next unread newsgroup by exiting.
5120     (gnus-summary-jump-to-group group)
5121     (when rescan
5122       (save-excursion
5123         (gnus-group-get-new-news-this-group 1)))
5124     (gnus-group-read-group all t)
5125     (gnus-summary-goto-subject current-subject nil t)))
5126
5127 (defun gnus-summary-rescan-group (&optional all)
5128   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5129   (interactive "P")
5130   (gnus-summary-reselect-current-group all t))
5131
5132 (defun gnus-summary-update-info (&optional non-destructive)
5133   (save-excursion
5134     (let ((group gnus-newsgroup-name))
5135       (when group
5136         (when gnus-newsgroup-kill-headers
5137           (setq gnus-newsgroup-killed
5138                 (gnus-compress-sequence
5139                  (nconc
5140                   (gnus-set-sorted-intersection
5141                    (gnus-uncompress-range gnus-newsgroup-killed)
5142                    (setq gnus-newsgroup-unselected
5143                          (sort gnus-newsgroup-unselected '<)))
5144                   (setq gnus-newsgroup-unreads
5145                         (sort gnus-newsgroup-unreads '<)))
5146                  t)))
5147         (unless (listp (cdr gnus-newsgroup-killed))
5148           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5149         (let ((headers gnus-newsgroup-headers))
5150           ;; Set the new ranges of read articles.
5151           (save-excursion
5152             (set-buffer gnus-group-buffer)
5153             (gnus-undo-force-boundary))
5154           (gnus-update-read-articles
5155            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5156           ;; Set the current article marks.
5157           (let ((gnus-newsgroup-scored
5158                  (if (and (not gnus-save-score)
5159                           (not non-destructive))
5160                      nil
5161                    gnus-newsgroup-scored)))
5162             (save-excursion
5163               (gnus-update-marks)))
5164           ;; Do the cross-ref thing.
5165           (when gnus-use-cross-reference
5166             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5167           ;; Do not switch windows but change the buffer to work.
5168           (set-buffer gnus-group-buffer)
5169           (unless (gnus-ephemeral-group-p group)
5170             (gnus-group-update-group group)))))))
5171
5172 (defun gnus-summary-save-newsrc (&optional force)
5173   "Save the current number of read/marked articles in the dribble buffer.
5174 The dribble buffer will then be saved.
5175 If FORCE (the prefix), also save the .newsrc file(s)."
5176   (interactive "P")
5177   (gnus-summary-update-info t)
5178   (if force
5179       (gnus-save-newsrc-file)
5180     (gnus-dribble-save)))
5181
5182 (defun gnus-summary-exit (&optional temporary)
5183   "Exit reading current newsgroup, and then return to group selection mode.
5184 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5185   (interactive)
5186   (gnus-set-global-variables)
5187   (gnus-kill-save-kill-buffer)
5188   (gnus-async-halt-prefetch)
5189   (let* ((group gnus-newsgroup-name)
5190          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5191          (mode major-mode)
5192          (group-point nil)
5193          (buf (current-buffer)))
5194     (unless quit-config
5195       ;; Do adaptive scoring, and possibly save score files.
5196       (when gnus-newsgroup-adaptive
5197         (gnus-score-adaptive))
5198       (when gnus-use-scoring
5199         (gnus-score-save)))
5200     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5201     ;; If we have several article buffers, we kill them at exit.
5202     (unless gnus-single-article-buffer
5203       (gnus-kill-buffer gnus-original-article-buffer)
5204       (setq gnus-article-current nil))
5205     (when gnus-use-cache
5206       (gnus-cache-possibly-remove-articles)
5207       (gnus-cache-save-buffers))
5208     (gnus-async-prefetch-remove-group group)
5209     (when gnus-suppress-duplicates
5210       (gnus-dup-enter-articles))
5211     (when gnus-use-trees
5212       (gnus-tree-close group))
5213     ;; Remove entries for this group.
5214     (nnmail-purge-split-history (gnus-group-real-name group))
5215     ;; Make all changes in this group permanent.
5216     (unless quit-config
5217       (gnus-run-hooks 'gnus-exit-group-hook)
5218       (gnus-summary-update-info))
5219     (gnus-close-group group)
5220     ;; Make sure where we were, and go to next newsgroup.
5221     (set-buffer gnus-group-buffer)
5222     (unless quit-config
5223       (gnus-group-jump-to-group group))
5224     (gnus-run-hooks 'gnus-summary-exit-hook)
5225     (unless (or quit-config
5226                 ;; If this group has disappeared from the summary
5227                 ;; buffer, don't skip forwards.
5228                 (not (string= group (gnus-group-group-name))))
5229       (gnus-group-next-unread-group 1))
5230     (setq group-point (point))
5231     (if temporary
5232         nil                             ;Nothing to do.
5233       ;; If we have several article buffers, we kill them at exit.
5234       (unless gnus-single-article-buffer
5235         (gnus-kill-buffer gnus-article-buffer)
5236         (gnus-kill-buffer gnus-original-article-buffer)
5237         (setq gnus-article-current nil))
5238       (set-buffer buf)
5239       (if (not gnus-kill-summary-on-exit)
5240           (gnus-deaden-summary)
5241         ;; We set all buffer-local variables to nil.  It is unclear why
5242         ;; this is needed, but if we don't, buffer-local variables are
5243         ;; not garbage-collected, it seems.  This would the lead to en
5244         ;; ever-growing Emacs.
5245         (gnus-summary-clear-local-variables)
5246         (when (get-buffer gnus-article-buffer)
5247           (bury-buffer gnus-article-buffer))
5248         ;; We clear the global counterparts of the buffer-local
5249         ;; variables as well, just to be on the safe side.
5250         (set-buffer gnus-group-buffer)
5251         (gnus-summary-clear-local-variables)
5252         ;; Return to group mode buffer.
5253         (when (eq mode 'gnus-summary-mode)
5254           (gnus-kill-buffer buf)))
5255       (setq gnus-current-select-method gnus-select-method)
5256       (pop-to-buffer gnus-group-buffer)
5257       (if (not quit-config)
5258           (progn
5259             (goto-char group-point)
5260             (gnus-configure-windows 'group 'force))
5261         (gnus-handle-ephemeral-exit quit-config))
5262       ;; Clear the current group name.
5263       (unless quit-config
5264         (setq gnus-newsgroup-name nil)))))
5265
5266 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5267 (defun gnus-summary-exit-no-update (&optional no-questions)
5268   "Quit reading current newsgroup without updating read article info."
5269   (interactive)
5270   (let* ((group gnus-newsgroup-name)
5271          (quit-config (gnus-group-quit-config group)))
5272     (when (or no-questions
5273               gnus-expert-user
5274               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5275       (gnus-async-halt-prefetch)
5276       (mapcar 'funcall
5277               (delq 'gnus-summary-expire-articles
5278                     (copy-list gnus-summary-prepare-exit-hook)))
5279       ;; If we have several article buffers, we kill them at exit.
5280       (unless gnus-single-article-buffer
5281         (gnus-kill-buffer gnus-article-buffer)
5282         (gnus-kill-buffer gnus-original-article-buffer)
5283         (setq gnus-article-current nil))
5284       (if (not gnus-kill-summary-on-exit)
5285           (gnus-deaden-summary)
5286         (gnus-close-group group)
5287         (gnus-summary-clear-local-variables)
5288         (set-buffer gnus-group-buffer)
5289         (gnus-summary-clear-local-variables)
5290         (when (get-buffer gnus-summary-buffer)
5291           (kill-buffer gnus-summary-buffer)))
5292       (unless gnus-single-article-buffer
5293         (setq gnus-article-current nil))
5294       (when gnus-use-trees
5295         (gnus-tree-close group))
5296       (gnus-async-prefetch-remove-group group)
5297       (when (get-buffer gnus-article-buffer)
5298         (bury-buffer gnus-article-buffer))
5299       ;; Return to the group buffer.
5300       (gnus-configure-windows 'group 'force)
5301       ;; Clear the current group name.
5302       (setq gnus-newsgroup-name nil)
5303       (when (equal (gnus-group-group-name) group)
5304         (gnus-group-next-unread-group 1))
5305       (when quit-config
5306         (gnus-handle-ephemeral-exit quit-config)))))
5307
5308 (defun gnus-handle-ephemeral-exit (quit-config)
5309   "Handle movement when leaving an ephemeral group.
5310 The state which existed when entering the ephemeral is reset."
5311   (if (not (buffer-name (car quit-config)))
5312       (gnus-configure-windows 'group 'force)
5313     (set-buffer (car quit-config))
5314     (cond ((eq major-mode 'gnus-summary-mode)
5315            (gnus-set-global-variables))
5316           ((eq major-mode 'gnus-article-mode)
5317            (save-excursion
5318              ;; The `gnus-summary-buffer' variable may point
5319              ;; to the old summary buffer when using a single
5320              ;; article buffer.
5321              (unless (gnus-buffer-live-p gnus-summary-buffer)
5322                (set-buffer gnus-group-buffer))
5323              (set-buffer gnus-summary-buffer)
5324              (gnus-set-global-variables))))
5325     (if (or (eq (cdr quit-config) 'article)
5326             (eq (cdr quit-config) 'pick))
5327         (progn
5328           ;; The current article may be from the ephemeral group
5329           ;; thus it is best that we reload this article
5330           (gnus-summary-show-article)
5331           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5332               (gnus-configure-windows 'pick 'force)
5333             (gnus-configure-windows (cdr quit-config) 'force)))
5334       (gnus-configure-windows (cdr quit-config) 'force))
5335     (when (eq major-mode 'gnus-summary-mode)
5336       (gnus-summary-next-subject 1 nil t)
5337       (gnus-summary-recenter)
5338       (gnus-summary-position-point))))
5339
5340 (defun gnus-summary-preview-mime-message ()
5341   "MIME decode and play this message."
5342   (interactive)
5343   (let ((gnus-break-pages nil)
5344         (gnus-show-mime t))
5345     (gnus-summary-select-article gnus-show-all-headers t))
5346   (select-window (get-buffer-window gnus-article-buffer)))
5347
5348 ;;; Dead summaries.
5349
5350 (defvar gnus-dead-summary-mode-map nil)
5351
5352 (unless gnus-dead-summary-mode-map
5353   (setq gnus-dead-summary-mode-map (make-keymap))
5354   (suppress-keymap gnus-dead-summary-mode-map)
5355   (substitute-key-definition
5356    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5357   (let ((keys '("\C-d" "\r" "\177" [delete])))
5358     (while keys
5359       (define-key gnus-dead-summary-mode-map
5360         (pop keys) 'gnus-summary-wake-up-the-dead))))
5361
5362 (defvar gnus-dead-summary-mode nil
5363   "Minor mode for Gnus summary buffers.")
5364
5365 (defun gnus-dead-summary-mode (&optional arg)
5366   "Minor mode for Gnus summary buffers."
5367   (interactive "P")
5368   (when (eq major-mode 'gnus-summary-mode)
5369     (make-local-variable 'gnus-dead-summary-mode)
5370     (setq gnus-dead-summary-mode
5371           (if (null arg) (not gnus-dead-summary-mode)
5372             (> (prefix-numeric-value arg) 0)))
5373     (when gnus-dead-summary-mode
5374       (gnus-add-minor-mode
5375        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5376
5377 (defun gnus-deaden-summary ()
5378   "Make the current summary buffer into a dead summary buffer."
5379   ;; Kill any previous dead summary buffer.
5380   (when (and gnus-dead-summary
5381              (buffer-name gnus-dead-summary))
5382     (save-excursion
5383       (set-buffer gnus-dead-summary)
5384       (when gnus-dead-summary-mode
5385         (kill-buffer (current-buffer)))))
5386   ;; Make this the current dead summary.
5387   (setq gnus-dead-summary (current-buffer))
5388   (gnus-dead-summary-mode 1)
5389   (let ((name (buffer-name)))
5390     (when (string-match "Summary" name)
5391       (rename-buffer
5392        (concat (substring name 0 (match-beginning 0)) "Dead "
5393                (substring name (match-beginning 0)))
5394        t))))
5395
5396 (defun gnus-kill-or-deaden-summary (buffer)
5397   "Kill or deaden the summary BUFFER."
5398   (save-excursion
5399     (when (and (buffer-name buffer)
5400                (not gnus-single-article-buffer))
5401       (save-excursion
5402         (set-buffer buffer)
5403         (gnus-kill-buffer gnus-article-buffer)
5404         (gnus-kill-buffer gnus-original-article-buffer)))
5405     (cond (gnus-kill-summary-on-exit
5406            (when (and gnus-use-trees
5407                       (gnus-buffer-exists-p buffer))
5408              (save-excursion
5409                (set-buffer buffer)
5410                (gnus-tree-close gnus-newsgroup-name)))
5411            (gnus-kill-buffer buffer))
5412           ((gnus-buffer-exists-p buffer)
5413            (save-excursion
5414              (set-buffer buffer)
5415              (gnus-deaden-summary))))))
5416
5417 (defun gnus-summary-wake-up-the-dead (&rest args)
5418   "Wake up the dead summary buffer."
5419   (interactive)
5420   (gnus-dead-summary-mode -1)
5421   (let ((name (buffer-name)))
5422     (when (string-match "Dead " name)
5423       (rename-buffer
5424        (concat (substring name 0 (match-beginning 0))
5425                (substring name (match-end 0)))
5426        t)))
5427   (gnus-message 3 "This dead summary is now alive again"))
5428
5429 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5430 (defun gnus-summary-fetch-faq (&optional faq-dir)
5431   "Fetch the FAQ for the current group.
5432 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5433 in."
5434   (interactive
5435    (list
5436     (when current-prefix-arg
5437       (completing-read
5438        "Faq dir: " (and (listp gnus-group-faq-directory)
5439                         (mapcar (lambda (file) (list file))
5440                                 gnus-group-faq-directory))))))
5441   (let (gnus-faq-buffer)
5442     (when (setq gnus-faq-buffer
5443                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5444       (gnus-configure-windows 'summary-faq))))
5445
5446 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5447 (defun gnus-summary-describe-group (&optional force)
5448   "Describe the current newsgroup."
5449   (interactive "P")
5450   (gnus-group-describe-group force gnus-newsgroup-name))
5451
5452 (defun gnus-summary-describe-briefly ()
5453   "Describe summary mode commands briefly."
5454   (interactive)
5455   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5456
5457 ;; Walking around group mode buffer from summary mode.
5458
5459 (defun gnus-summary-next-group (&optional no-article target-group backward)
5460   "Exit current newsgroup and then select next unread newsgroup.
5461 If prefix argument NO-ARTICLE is non-nil, no article is selected
5462 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5463 previous group instead."
5464   (interactive "P")
5465   ;; Stop pre-fetching.
5466   (gnus-async-halt-prefetch)
5467   (let ((current-group gnus-newsgroup-name)
5468         (current-buffer (current-buffer))
5469         entered)
5470     ;; First we semi-exit this group to update Xrefs and all variables.
5471     ;; We can't do a real exit, because the window conf must remain
5472     ;; the same in case the user is prompted for info, and we don't
5473     ;; want the window conf to change before that...
5474     (gnus-summary-exit t)
5475     (while (not entered)
5476       ;; Then we find what group we are supposed to enter.
5477       (set-buffer gnus-group-buffer)
5478       (gnus-group-jump-to-group current-group)
5479       (setq target-group
5480             (or target-group
5481                 (if (eq gnus-keep-same-level 'best)
5482                     (gnus-summary-best-group gnus-newsgroup-name)
5483                   (gnus-summary-search-group backward gnus-keep-same-level))))
5484       (if (not target-group)
5485           ;; There are no further groups, so we return to the group
5486           ;; buffer.
5487           (progn
5488             (gnus-message 5 "Returning to the group buffer")
5489             (setq entered t)
5490             (when (gnus-buffer-live-p current-buffer)
5491               (set-buffer current-buffer)
5492               (gnus-summary-exit))
5493             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5494         ;; We try to enter the target group.
5495         (gnus-group-jump-to-group target-group)
5496         (let ((unreads (gnus-group-group-unread)))
5497           (if (and (or (eq t unreads)
5498                        (and unreads (not (zerop unreads))))
5499                    (gnus-summary-read-group
5500                     target-group nil no-article
5501                     (and (buffer-name current-buffer) current-buffer)
5502                     nil backward))
5503               (setq entered t)
5504             (setq current-group target-group
5505                   target-group nil)))))))
5506
5507 (defun gnus-summary-prev-group (&optional no-article)
5508   "Exit current newsgroup and then select previous unread newsgroup.
5509 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5510   (interactive "P")
5511   (gnus-summary-next-group no-article nil t))
5512
5513 ;; Walking around summary lines.
5514
5515 (defun gnus-summary-first-subject (&optional unread undownloaded)
5516   "Go to the first unread subject.
5517 If UNREAD is non-nil, go to the first unread article.
5518 Returns the article selected or nil if there are no unread articles."
5519   (interactive "P")
5520   (prog1
5521       (cond
5522        ;; Empty summary.
5523        ((null gnus-newsgroup-data)
5524         (gnus-message 3 "No articles in the group")
5525         nil)
5526        ;; Pick the first article.
5527        ((not unread)
5528         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5529         (gnus-data-number (car gnus-newsgroup-data)))
5530        ;; No unread articles.
5531        ((null gnus-newsgroup-unreads)
5532         (gnus-message 3 "No more unread articles")
5533         nil)
5534        ;; Find the first unread article.
5535        (t
5536         (let ((data gnus-newsgroup-data))
5537           (while (and data
5538                       (and (not (and undownloaded
5539                                      (eq gnus-undownloaded-mark
5540                                          (gnus-data-mark (car data)))))
5541                            (not (gnus-data-unread-p (car data)))))
5542             (setq data (cdr data)))
5543           (when data
5544             (goto-char (gnus-data-pos (car data)))
5545             (gnus-data-number (car data))))))
5546     (gnus-summary-position-point)))
5547
5548 (defun gnus-summary-next-subject (n &optional unread dont-display)
5549   "Go to next N'th summary line.
5550 If N is negative, go to the previous N'th subject line.
5551 If UNREAD is non-nil, only unread articles are selected.
5552 The difference between N and the actual number of steps taken is
5553 returned."
5554   (interactive "p")
5555   (let ((backward (< n 0))
5556         (n (abs n)))
5557     (while (and (> n 0)
5558                 (if backward
5559                     (gnus-summary-find-prev unread)
5560                   (gnus-summary-find-next unread)))
5561       (gnus-summary-show-thread)
5562       (setq n (1- n)))
5563     (when (/= 0 n)
5564       (gnus-message 7 "No more%s articles"
5565                     (if unread " unread" "")))
5566     (unless dont-display
5567       (gnus-summary-recenter)
5568       (gnus-summary-position-point))
5569     n))
5570
5571 (defun gnus-summary-next-unread-subject (n)
5572   "Go to next N'th unread summary line."
5573   (interactive "p")
5574   (gnus-summary-next-subject n t))
5575
5576 (defun gnus-summary-prev-subject (n &optional unread)
5577   "Go to previous N'th summary line.
5578 If optional argument UNREAD is non-nil, only unread article is selected."
5579   (interactive "p")
5580   (gnus-summary-next-subject (- n) unread))
5581
5582 (defun gnus-summary-prev-unread-subject (n)
5583   "Go to previous N'th unread summary line."
5584   (interactive "p")
5585   (gnus-summary-next-subject (- n) t))
5586
5587 (defun gnus-summary-goto-subject (article &optional force silent)
5588   "Go the subject line of ARTICLE.
5589 If FORCE, also allow jumping to articles not currently shown."
5590   (interactive "nArticle number: ")
5591   (let ((b (point))
5592         (data (gnus-data-find article)))
5593     ;; We read in the article if we have to.
5594     (and (not data)
5595          force
5596          (gnus-summary-insert-subject
5597           article
5598           (if (or (numberp force) (vectorp force)) force)
5599           t)
5600          (setq data (gnus-data-find article)))
5601     (goto-char b)
5602     (if (not data)
5603         (progn
5604           (unless silent
5605             (gnus-message 3 "Can't find article %d" article))
5606           nil)
5607       (goto-char (gnus-data-pos data))
5608       (gnus-summary-position-point)
5609       article)))
5610
5611 ;; Walking around summary lines with displaying articles.
5612
5613 (defun gnus-summary-expand-window (&optional arg)
5614   "Make the summary buffer take up the entire Emacs frame.
5615 Given a prefix, will force an `article' buffer configuration."
5616   (interactive "P")
5617   (if arg
5618       (gnus-configure-windows 'article 'force)
5619     (gnus-configure-windows 'summary 'force)))
5620
5621 (defun gnus-summary-display-article (article &optional all-header)
5622   "Display ARTICLE in article buffer."
5623   (gnus-set-global-variables)
5624   (if (null article)
5625       nil
5626     (prog1
5627         (if gnus-summary-display-article-function
5628             (funcall gnus-summary-display-article-function article all-header)
5629           (gnus-article-prepare article all-header))
5630       (gnus-run-hooks 'gnus-select-article-hook)
5631       (when (and gnus-current-article
5632                  (not (zerop gnus-current-article)))
5633         (gnus-summary-goto-subject gnus-current-article))
5634       (gnus-summary-recenter)
5635       (when (and gnus-use-trees gnus-show-threads)
5636         (gnus-possibly-generate-tree article)
5637         (gnus-highlight-selected-tree article))
5638       ;; Successfully display article.
5639       (gnus-article-set-window-start
5640        (cdr (assq article gnus-newsgroup-bookmarks))))))
5641
5642 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5643   "Select the current article.
5644 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5645 non-nil, the article will be re-fetched even if it already present in
5646 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5647 be displayed."
5648   ;; Make sure we are in the summary buffer to work around bbdb bug.
5649   (unless (eq major-mode 'gnus-summary-mode)
5650     (set-buffer gnus-summary-buffer))
5651   (let ((article (or article (gnus-summary-article-number)))
5652         (all-headers (not (not all-headers))) ;Must be T or NIL.
5653         gnus-summary-display-article-function
5654         did)
5655     (and (not pseudo)
5656          (gnus-summary-article-pseudo-p article)
5657          (error "This is a pseudo-article"))
5658     (prog1
5659         (save-excursion
5660           (set-buffer gnus-summary-buffer)
5661           (if (or (and gnus-single-article-buffer
5662                        (or (null gnus-current-article)
5663                            (null gnus-article-current)
5664                            (null (get-buffer gnus-article-buffer))
5665                            (not (eq article (cdr gnus-article-current)))
5666                            (not (equal (car gnus-article-current)
5667                                        gnus-newsgroup-name))))
5668                   (and (not gnus-single-article-buffer)
5669                        (or (null gnus-current-article)
5670                            (not (eq gnus-current-article article))))
5671                   force)
5672               ;; The requested article is different from the current article.
5673               (prog1
5674                   (gnus-summary-display-article article all-headers)
5675                 (setq did article)
5676                 (when (or all-headers gnus-show-all-headers)
5677                   (gnus-article-show-all-headers)))
5678             (when (or all-headers gnus-show-all-headers)
5679               (gnus-article-show-all-headers))
5680             'old))
5681       (when did
5682         (gnus-article-set-window-start
5683          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5684
5685 (defun gnus-summary-set-current-mark (&optional current-mark)
5686   "Obsolete function."
5687   nil)
5688
5689 (defun gnus-summary-next-article (&optional unread subject backward push)
5690   "Select the next article.
5691 If UNREAD, only unread articles are selected.
5692 If SUBJECT, only articles with SUBJECT are selected.
5693 If BACKWARD, the previous article is selected instead of the next."
5694   (interactive "P")
5695   (cond
5696    ;; Is there such an article?
5697    ((and (gnus-summary-search-forward unread subject backward)
5698          (or (gnus-summary-display-article (gnus-summary-article-number))
5699              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5700     (gnus-summary-position-point))
5701    ;; If not, we try the first unread, if that is wanted.
5702    ((and subject
5703          gnus-auto-select-same
5704          (gnus-summary-first-unread-article))
5705     (gnus-summary-position-point)
5706     (gnus-message 6 "Wrapped"))
5707    ;; Try to get next/previous article not displayed in this group.
5708    ((and gnus-auto-extend-newsgroup
5709          (not unread) (not subject))
5710     (gnus-summary-goto-article
5711      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5712      nil (count-lines (point-min) (point))))
5713    ;; Go to next/previous group.
5714    (t
5715     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5716       (gnus-summary-jump-to-group gnus-newsgroup-name))
5717     (let ((cmd last-command-char)
5718           (point
5719            (save-excursion
5720              (set-buffer gnus-group-buffer)
5721              (point)))
5722           (group
5723            (if (eq gnus-keep-same-level 'best)
5724                (gnus-summary-best-group gnus-newsgroup-name)
5725              (gnus-summary-search-group backward gnus-keep-same-level))))
5726       ;; For some reason, the group window gets selected.  We change
5727       ;; it back.
5728       (select-window (get-buffer-window (current-buffer)))
5729       ;; Select next unread newsgroup automagically.
5730       (cond
5731        ((or (not gnus-auto-select-next)
5732             (not cmd))
5733         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5734        ((or (eq gnus-auto-select-next 'quietly)
5735             (and (eq gnus-auto-select-next 'slightly-quietly)
5736                  push)
5737             (and (eq gnus-auto-select-next 'almost-quietly)
5738                  (gnus-summary-last-article-p)))
5739         ;; Select quietly.
5740         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5741             (gnus-summary-exit)
5742           (gnus-message 7 "No more%s articles (%s)..."
5743                         (if unread " unread" "")
5744                         (if group (concat "selecting " group)
5745                           "exiting"))
5746           (gnus-summary-next-group nil group backward)))
5747        (t
5748         (when (gnus-key-press-event-p last-input-event)
5749           (gnus-summary-walk-group-buffer
5750            gnus-newsgroup-name cmd unread backward point))))))))
5751
5752 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5753   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5754                       (?\C-p (gnus-group-prev-unread-group 1))))
5755         (cursor-in-echo-area t)
5756         keve key group ended)
5757     (save-excursion
5758       (set-buffer gnus-group-buffer)
5759       (goto-char start)
5760       (setq group
5761             (if (eq gnus-keep-same-level 'best)
5762                 (gnus-summary-best-group gnus-newsgroup-name)
5763               (gnus-summary-search-group backward gnus-keep-same-level))))
5764     (while (not ended)
5765       (gnus-message
5766        5 "No more%s articles%s" (if unread " unread" "")
5767        (if (and group
5768                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5769            (format " (Type %s for %s [%s])"
5770                    (single-key-description cmd) group
5771                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5772          (format " (Type %s to exit %s)"
5773                  (single-key-description cmd)
5774                  gnus-newsgroup-name)))
5775       ;; Confirm auto selection.
5776       (setq key (car (setq keve (gnus-read-event-char))))
5777       (setq ended t)
5778       (cond
5779        ((assq key keystrokes)
5780         (let ((obuf (current-buffer)))
5781           (switch-to-buffer gnus-group-buffer)
5782           (when group
5783             (gnus-group-jump-to-group group))
5784           (eval (cadr (assq key keystrokes)))
5785           (setq group (gnus-group-group-name))
5786           (switch-to-buffer obuf))
5787         (setq ended nil))
5788        ((equal key cmd)
5789         (if (or (not group)
5790                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5791             (gnus-summary-exit)
5792           (gnus-summary-next-group nil group backward)))
5793        (t
5794         (push (cdr keve) unread-command-events))))))
5795
5796 (defun gnus-summary-next-unread-article ()
5797   "Select unread article after current one."
5798   (interactive)
5799   (gnus-summary-next-article
5800    (or (not (eq gnus-summary-goto-unread 'never))
5801        (gnus-summary-last-article-p (gnus-summary-article-number)))
5802    (and gnus-auto-select-same
5803         (gnus-summary-article-subject))))
5804
5805 (defun gnus-summary-prev-article (&optional unread subject)
5806   "Select the article after the current one.
5807 If UNREAD is non-nil, only unread articles are selected."
5808   (interactive "P")
5809   (gnus-summary-next-article unread subject t))
5810
5811 (defun gnus-summary-prev-unread-article ()
5812   "Select unread article before current one."
5813   (interactive)
5814   (gnus-summary-prev-article
5815    (or (not (eq gnus-summary-goto-unread 'never))
5816        (gnus-summary-first-article-p (gnus-summary-article-number)))
5817    (and gnus-auto-select-same
5818         (gnus-summary-article-subject))))
5819
5820 (defun gnus-summary-next-page (&optional lines circular)
5821   "Show next page of the selected article.
5822 If at the end of the current article, select the next article.
5823 LINES says how many lines should be scrolled up.
5824
5825 If CIRCULAR is non-nil, go to the start of the article instead of
5826 selecting the next article when reaching the end of the current
5827 article."
5828   (interactive "P")
5829   (setq gnus-summary-buffer (current-buffer))
5830   (gnus-set-global-variables)
5831   (let ((article (gnus-summary-article-number))
5832         (article-window (get-buffer-window gnus-article-buffer t))
5833         endp)
5834     ;; If the buffer is empty, we have no article.
5835     (unless article
5836       (error "No article to select"))
5837     (gnus-configure-windows 'article)
5838     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5839         (if (and (eq gnus-summary-goto-unread 'never)
5840                  (not (gnus-summary-last-article-p article)))
5841             (gnus-summary-next-article)
5842           (gnus-summary-next-unread-article))
5843       (if (or (null gnus-current-article)
5844               (null gnus-article-current)
5845               (/= article (cdr gnus-article-current))
5846               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5847           ;; Selected subject is different from current article's.
5848           (gnus-summary-display-article article)
5849         (when article-window
5850           (gnus-eval-in-buffer-window gnus-article-buffer
5851             (setq endp (gnus-article-next-page lines)))
5852           (when endp
5853             (cond (circular
5854                    (gnus-summary-beginning-of-article))
5855                   (lines
5856                    (gnus-message 3 "End of message"))
5857                   ((null lines)
5858                    (if (and (eq gnus-summary-goto-unread 'never)
5859                             (not (gnus-summary-last-article-p article)))
5860                        (gnus-summary-next-article)
5861                      (gnus-summary-next-unread-article))))))))
5862     (gnus-summary-recenter)
5863     (gnus-summary-position-point)))
5864
5865 (defun gnus-summary-prev-page (&optional lines move)
5866   "Show previous page of selected article.
5867 Argument LINES specifies lines to be scrolled down.
5868 If MOVE, move to the previous unread article if point is at
5869 the beginning of the buffer."
5870   (interactive "P")
5871   (let ((article (gnus-summary-article-number))
5872         (article-window (get-buffer-window gnus-article-buffer t))
5873         endp)
5874     (gnus-configure-windows 'article)
5875     (if (or (null gnus-current-article)
5876             (null gnus-article-current)
5877             (/= article (cdr gnus-article-current))
5878             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5879         ;; Selected subject is different from current article's.
5880         (gnus-summary-display-article article)
5881       (gnus-summary-recenter)
5882       (when article-window
5883         (gnus-eval-in-buffer-window gnus-article-buffer
5884           (setq endp (gnus-article-prev-page lines)))
5885         (when (and move endp)
5886           (cond (lines
5887                  (gnus-message 3 "Beginning of message"))
5888                 ((null lines)
5889                  (if (and (eq gnus-summary-goto-unread 'never)
5890                           (not (gnus-summary-first-article-p article)))
5891                      (gnus-summary-prev-article)
5892                    (gnus-summary-prev-unread-article))))))))
5893   (gnus-summary-position-point))
5894
5895 (defun gnus-summary-prev-page-or-article (&optional lines)
5896   "Show previous page of selected article.
5897 Argument LINES specifies lines to be scrolled down.
5898 If at the beginning of the article, go to the next article."
5899   (interactive "P")
5900   (gnus-summary-prev-page lines t))
5901
5902 (defun gnus-summary-scroll-up (lines)
5903   "Scroll up (or down) one line current article.
5904 Argument LINES specifies lines to be scrolled up (or down if negative)."
5905   (interactive "p")
5906   (gnus-configure-windows 'article)
5907   (gnus-summary-show-thread)
5908   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5909     (gnus-eval-in-buffer-window gnus-article-buffer
5910       (cond ((> lines 0)
5911              (when (gnus-article-next-page lines)
5912                (gnus-message 3 "End of message")))
5913             ((< lines 0)
5914              (gnus-article-prev-page (- lines))))))
5915   (gnus-summary-recenter)
5916   (gnus-summary-position-point))
5917
5918 (defun gnus-summary-scroll-down (lines)
5919   "Scroll down (or up) one line current article.
5920 Argument LINES specifies lines to be scrolled down (or up if negative)."
5921   (interactive "p")
5922   (gnus-summary-scroll-up (- lines)))
5923
5924 (defun gnus-summary-next-same-subject ()
5925   "Select next article which has the same subject as current one."
5926   (interactive)
5927   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5928
5929 (defun gnus-summary-prev-same-subject ()
5930   "Select previous article which has the same subject as current one."
5931   (interactive)
5932   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5933
5934 (defun gnus-summary-next-unread-same-subject ()
5935   "Select next unread article which has the same subject as current one."
5936   (interactive)
5937   (gnus-summary-next-article t (gnus-summary-article-subject)))
5938
5939 (defun gnus-summary-prev-unread-same-subject ()
5940   "Select previous unread article which has the same subject as current one."
5941   (interactive)
5942   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5943
5944 (defun gnus-summary-first-unread-article ()
5945   "Select the first unread article.
5946 Return nil if there are no unread articles."
5947   (interactive)
5948   (prog1
5949       (when (gnus-summary-first-subject t)
5950         (gnus-summary-show-thread)
5951         (gnus-summary-first-subject t)
5952         (gnus-summary-display-article (gnus-summary-article-number)))
5953     (gnus-summary-position-point)))
5954
5955 (defun gnus-summary-first-unread-subject ()
5956   "Place the point on the subject line of 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-position-point)))
5964
5965 (defun gnus-summary-first-article ()
5966   "Select the first article.
5967 Return nil if there are no articles."
5968   (interactive)
5969   (prog1
5970       (when (gnus-summary-first-subject)
5971         (gnus-summary-show-thread)
5972         (gnus-summary-first-subject)
5973         (gnus-summary-display-article (gnus-summary-article-number)))
5974     (gnus-summary-position-point)))
5975
5976 (defun gnus-summary-best-unread-article ()
5977   "Select the unread article with the highest score."
5978   (interactive)
5979   (let ((best -1000000)
5980         (data gnus-newsgroup-data)
5981         article score)
5982     (while data
5983       (and (gnus-data-unread-p (car data))
5984            (> (setq score
5985                     (gnus-summary-article-score (gnus-data-number (car data))))
5986               best)
5987            (setq best score
5988                  article (gnus-data-number (car data))))
5989       (setq data (cdr data)))
5990     (prog1
5991         (if article
5992             (gnus-summary-goto-article article)
5993           (error "No unread articles"))
5994       (gnus-summary-position-point))))
5995
5996 (defun gnus-summary-last-subject ()
5997   "Go to the last displayed subject line in the group."
5998   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5999     (when article
6000       (gnus-summary-goto-subject article))))
6001
6002 (defun gnus-summary-goto-article (article &optional all-headers force)
6003   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6004 If ALL-HEADERS is non-nil, no header lines are hidden.
6005 If FORCE, go to the article even if it isn't displayed.  If FORCE
6006 is a number, it is the line the article is to be displayed on."
6007   (interactive
6008    (list
6009     (completing-read
6010      "Article number or Message-ID: "
6011      (mapcar (lambda (number) (list (int-to-string number)))
6012              gnus-newsgroup-limit))
6013     current-prefix-arg
6014     t))
6015   (prog1
6016       (if (and (stringp article)
6017                (string-match "@" article))
6018           (gnus-summary-refer-article article)
6019         (when (stringp article)
6020           (setq article (string-to-number article)))
6021         (if (gnus-summary-goto-subject article force)
6022             (gnus-summary-display-article article all-headers)
6023           (gnus-message 4 "Couldn't go to article %s" article) nil))
6024     (gnus-summary-position-point)))
6025
6026 (defun gnus-summary-goto-last-article ()
6027   "Go to the previously read article."
6028   (interactive)
6029   (prog1
6030       (when gnus-last-article
6031         (gnus-summary-goto-article gnus-last-article nil t))
6032     (gnus-summary-position-point)))
6033
6034 (defun gnus-summary-pop-article (number)
6035   "Pop one article off the history and go to the previous.
6036 NUMBER articles will be popped off."
6037   (interactive "p")
6038   (let (to)
6039     (setq gnus-newsgroup-history
6040           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6041     (if to
6042         (gnus-summary-goto-article (car to) nil t)
6043       (error "Article history empty")))
6044   (gnus-summary-position-point))
6045
6046 ;; Summary commands and functions for limiting the summary buffer.
6047
6048 (defun gnus-summary-limit-to-articles (n)
6049   "Limit the summary buffer to the next N articles.
6050 If not given a prefix, use the process marked articles instead."
6051   (interactive "P")
6052   (prog1
6053       (let ((articles (gnus-summary-work-articles n)))
6054         (setq gnus-newsgroup-processable nil)
6055         (gnus-summary-limit articles))
6056     (gnus-summary-position-point)))
6057
6058 (defun gnus-summary-pop-limit (&optional total)
6059   "Restore the previous limit.
6060 If given a prefix, remove all limits."
6061   (interactive "P")
6062   (when total
6063     (setq gnus-newsgroup-limits
6064           (list (mapcar (lambda (h) (mail-header-number h))
6065                         gnus-newsgroup-headers))))
6066   (unless gnus-newsgroup-limits
6067     (error "No limit to pop"))
6068   (prog1
6069       (gnus-summary-limit nil 'pop)
6070     (gnus-summary-position-point)))
6071
6072 (defun gnus-summary-limit-to-subject (subject &optional header)
6073   "Limit the summary buffer to articles that have subjects that match a regexp."
6074   (interactive "sLimit to subject (regexp): ")
6075   (unless header
6076     (setq header "subject"))
6077   (when (not (equal "" subject))
6078     (prog1
6079         (let ((articles (gnus-summary-find-matching
6080                          (or header "subject") subject 'all)))
6081           (unless articles
6082             (error "Found no matches for \"%s\"" subject))
6083           (gnus-summary-limit articles))
6084       (gnus-summary-position-point))))
6085
6086 (defun gnus-summary-limit-to-author (from)
6087   "Limit the summary buffer to articles that have authors that match a regexp."
6088   (interactive "sLimit to author (regexp): ")
6089   (gnus-summary-limit-to-subject from "from"))
6090
6091 (defun gnus-summary-limit-to-age (age &optional younger-p)
6092   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6093 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6094 articles that are younger than AGE days."
6095   (interactive "nTime in days: \nP")
6096   (prog1
6097       (let ((data gnus-newsgroup-data)
6098             (cutoff (days-to-time age))
6099             articles d date is-younger)
6100         (while (setq d (pop data))
6101           (when (and (vectorp (gnus-data-header d))
6102                      (setq date (mail-header-date (gnus-data-header d))))
6103             (setq is-younger (time-less-p
6104                               (time-since (date-to-time date))
6105                               cutoff))
6106             (when (if younger-p
6107                       is-younger
6108                     (not is-younger))
6109               (push (gnus-data-number d) articles))))
6110         (gnus-summary-limit (nreverse articles)))
6111     (gnus-summary-position-point)))
6112
6113 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6114 (make-obsolete
6115  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6116
6117 (defun gnus-summary-limit-to-unread (&optional all)
6118   "Limit the summary buffer to articles that are not marked as read.
6119 If ALL is non-nil, limit strictly to unread articles."
6120   (interactive "P")
6121   (if all
6122       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6123     (gnus-summary-limit-to-marks
6124      ;; Concat all the marks that say that an article is read and have
6125      ;; those removed.
6126      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6127            gnus-killed-mark gnus-kill-file-mark
6128            gnus-low-score-mark gnus-expirable-mark
6129            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6130            gnus-duplicate-mark gnus-souped-mark)
6131      'reverse)))
6132
6133 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6134 (make-obsolete 'gnus-summary-delete-marked-with
6135                'gnus-summary-limit-exlude-marks)
6136
6137 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6138   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6139 If REVERSE, limit the summary buffer to articles that are marked
6140 with MARKS.  MARKS can either be a string of marks or a list of marks.
6141 Returns how many articles were removed."
6142   (interactive "sMarks: ")
6143   (gnus-summary-limit-to-marks marks t))
6144
6145 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6146   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6147 If REVERSE (the prefix), limit the summary buffer to articles that are
6148 not marked with MARKS.  MARKS can either be a string of marks or a
6149 list of marks.
6150 Returns how many articles were removed."
6151   (interactive "sMarks: \nP")
6152   (prog1
6153       (let ((data gnus-newsgroup-data)
6154             (marks (if (listp marks) marks
6155                      (append marks nil))) ; Transform to list.
6156             articles)
6157         (while data
6158           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6159                   (memq (gnus-data-mark (car data)) marks))
6160             (push (gnus-data-number (car data)) articles))
6161           (setq data (cdr data)))
6162         (gnus-summary-limit articles))
6163     (gnus-summary-position-point)))
6164
6165 (defun gnus-summary-limit-to-score (&optional score)
6166   "Limit to articles with score at or above SCORE."
6167   (interactive "P")
6168   (setq score (if score
6169                   (prefix-numeric-value score)
6170                 (or gnus-summary-default-score 0)))
6171   (let ((data gnus-newsgroup-data)
6172         articles)
6173     (while data
6174       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6175                 score)
6176         (push (gnus-data-number (car data)) articles))
6177       (setq data (cdr data)))
6178     (prog1
6179         (gnus-summary-limit articles)
6180       (gnus-summary-position-point))))
6181
6182 (defun gnus-summary-limit-include-thread (id)
6183   "Display all the hidden articles that in the current thread."
6184   (interactive (list (mail-header-id (gnus-summary-article-header))))
6185   (let ((articles (gnus-articles-in-thread
6186                    (gnus-id-to-thread (gnus-root-id id)))))
6187     (prog1
6188         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6189       (gnus-summary-position-point))))
6190
6191 (defun gnus-summary-limit-include-dormant ()
6192   "Display all the hidden articles that are marked as dormant.
6193 Note that this command only works on a subset of the articles currently
6194 fetched for this group."
6195   (interactive)
6196   (unless gnus-newsgroup-dormant
6197     (error "There are no dormant articles in this group"))
6198   (prog1
6199       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6200     (gnus-summary-position-point)))
6201
6202 (defun gnus-summary-limit-exclude-dormant ()
6203   "Hide all dormant articles."
6204   (interactive)
6205   (prog1
6206       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6207     (gnus-summary-position-point)))
6208
6209 (defun gnus-summary-limit-exclude-childless-dormant ()
6210   "Hide all dormant articles that have no children."
6211   (interactive)
6212   (let ((data (gnus-data-list t))
6213         articles d children)
6214     ;; Find all articles that are either not dormant or have
6215     ;; children.
6216     (while (setq d (pop data))
6217       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6218                 (and (setq children
6219                            (gnus-article-children (gnus-data-number d)))
6220                      (let (found)
6221                        (while children
6222                          (when (memq (car children) articles)
6223                            (setq children nil
6224                                  found t))
6225                          (pop children))
6226                        found)))
6227         (push (gnus-data-number d) articles)))
6228     ;; Do the limiting.
6229     (prog1
6230         (gnus-summary-limit articles)
6231       (gnus-summary-position-point))))
6232
6233 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6234   "Mark all unread excluded articles as read.
6235 If ALL, mark even excluded ticked and dormants as read."
6236   (interactive "P")
6237   (let ((articles (gnus-sorted-complement
6238                    (sort
6239                     (mapcar (lambda (h) (mail-header-number h))
6240                             gnus-newsgroup-headers)
6241                     '<)
6242                    (sort gnus-newsgroup-limit '<)))
6243         article)
6244     (setq gnus-newsgroup-unreads
6245           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6246     (if all
6247         (setq gnus-newsgroup-dormant nil
6248               gnus-newsgroup-marked nil
6249               gnus-newsgroup-reads
6250               (nconc
6251                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6252                gnus-newsgroup-reads))
6253       (while (setq article (pop articles))
6254         (unless (or (memq article gnus-newsgroup-dormant)
6255                     (memq article gnus-newsgroup-marked))
6256           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6257
6258 (defun gnus-summary-limit (articles &optional pop)
6259   (if pop
6260       ;; We pop the previous limit off the stack and use that.
6261       (setq articles (car gnus-newsgroup-limits)
6262             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6263     ;; We use the new limit, so we push the old limit on the stack.
6264     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6265   ;; Set the limit.
6266   (setq gnus-newsgroup-limit articles)
6267   (let ((total (length gnus-newsgroup-data))
6268         (data (gnus-data-find-list (gnus-summary-article-number)))
6269         (gnus-summary-mark-below nil)   ; Inhibit this.
6270         found)
6271     ;; This will do all the work of generating the new summary buffer
6272     ;; according to the new limit.
6273     (gnus-summary-prepare)
6274     ;; Hide any threads, possibly.
6275     (and gnus-show-threads
6276          gnus-thread-hide-subtree
6277          (gnus-summary-hide-all-threads))
6278     ;; Try to return to the article you were at, or one in the
6279     ;; neighborhood.
6280     (when data
6281       ;; We try to find some article after the current one.
6282       (while data
6283         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6284           (setq data nil
6285                 found t))
6286         (setq data (cdr data))))
6287     (unless found
6288       ;; If there is no data, that means that we were after the last
6289       ;; article.  The same goes when we can't find any articles
6290       ;; after the current one.
6291       (goto-char (point-max))
6292       (gnus-summary-find-prev))
6293     (gnus-set-mode-line 'summary)
6294     ;; We return how many articles were removed from the summary
6295     ;; buffer as a result of the new limit.
6296     (- total (length gnus-newsgroup-data))))
6297
6298 (defsubst gnus-invisible-cut-children (threads)
6299   (let ((num 0))
6300     (while threads
6301       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6302         (incf num))
6303       (pop threads))
6304     (< num 2)))
6305
6306 (defsubst gnus-cut-thread (thread)
6307   "Go forwards in the thread until we find an article that we want to display."
6308   (when (or (eq gnus-fetch-old-headers 'some)
6309             (eq gnus-fetch-old-headers 'invisible)
6310             (eq gnus-build-sparse-threads 'some)
6311             (eq gnus-build-sparse-threads 'more))
6312     ;; Deal with old-fetched headers and sparse threads.
6313     (while (and
6314             thread
6315             (or
6316              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6317              (gnus-summary-article-ancient-p
6318               (mail-header-number (car thread))))
6319             (if (or (<= (length (cdr thread)) 1)
6320                     (eq gnus-fetch-old-headers 'invisible))
6321                 (setq gnus-newsgroup-limit
6322                       (delq (mail-header-number (car thread))
6323                             gnus-newsgroup-limit)
6324                       thread (cadr thread))
6325               (when (gnus-invisible-cut-children (cdr thread))
6326                 (let ((th (cdr thread)))
6327                   (while th
6328                     (if (memq (mail-header-number (caar th))
6329                               gnus-newsgroup-limit)
6330                         (setq thread (car th)
6331                               th nil)
6332                       (setq th (cdr th))))))))))
6333   thread)
6334
6335 (defun gnus-cut-threads (threads)
6336   "Cut off all uninteresting articles from the beginning of threads."
6337   (when (or (eq gnus-fetch-old-headers 'some)
6338             (eq gnus-fetch-old-headers 'invisible)
6339             (eq gnus-build-sparse-threads 'some)
6340             (eq gnus-build-sparse-threads 'more))
6341     (let ((th threads))
6342       (while th
6343         (setcar th (gnus-cut-thread (car th)))
6344         (setq th (cdr th)))))
6345   ;; Remove nixed out threads.
6346   (delq nil threads))
6347
6348 (defun gnus-summary-initial-limit (&optional show-if-empty)
6349   "Figure out what the initial limit is supposed to be on group entry.
6350 This entails weeding out unwanted dormants, low-scored articles,
6351 fetch-old-headers verbiage, and so on."
6352   ;; Most groups have nothing to remove.
6353   (if (or gnus-inhibit-limiting
6354           (and (null gnus-newsgroup-dormant)
6355                (not (eq gnus-fetch-old-headers 'some))
6356                (not (eq gnus-fetch-old-headers 'invisible))
6357                (null gnus-summary-expunge-below)
6358                (not (eq gnus-build-sparse-threads 'some))
6359                (not (eq gnus-build-sparse-threads 'more))
6360                (null gnus-thread-expunge-below)
6361                (not gnus-use-nocem)))
6362       ()                                ; Do nothing.
6363     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6364     (setq gnus-newsgroup-limit nil)
6365     (mapatoms
6366      (lambda (node)
6367        (unless (car (symbol-value node))
6368          ;; These threads have no parents -- they are roots.
6369          (let ((nodes (cdr (symbol-value node)))
6370                thread)
6371            (while nodes
6372              (if (and gnus-thread-expunge-below
6373                       (< (gnus-thread-total-score (car nodes))
6374                          gnus-thread-expunge-below))
6375                  (gnus-expunge-thread (pop nodes))
6376                (setq thread (pop nodes))
6377                (gnus-summary-limit-children thread))))))
6378      gnus-newsgroup-dependencies)
6379     ;; If this limitation resulted in an empty group, we might
6380     ;; pop the previous limit and use it instead.
6381     (when (and (not gnus-newsgroup-limit)
6382                show-if-empty)
6383       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6384     gnus-newsgroup-limit))
6385
6386 (defun gnus-summary-limit-children (thread)
6387   "Return 1 if this subthread is visible and 0 if it is not."
6388   ;; First we get the number of visible children to this thread.  This
6389   ;; is done by recursing down the thread using this function, so this
6390   ;; will really go down to a leaf article first, before slowly
6391   ;; working its way up towards the root.
6392   (when thread
6393     (let ((children
6394            (if (cdr thread)
6395                (apply '+ (mapcar 'gnus-summary-limit-children
6396                                  (cdr thread)))
6397              0))
6398           (number (mail-header-number (car thread)))
6399           score)
6400       (if (and
6401            (not (memq number gnus-newsgroup-marked))
6402            (or
6403             ;; If this article is dormant and has absolutely no visible
6404             ;; children, then this article isn't visible.
6405             (and (memq number gnus-newsgroup-dormant)
6406                  (zerop children))
6407             ;; If this is "fetch-old-headered" and there is no
6408             ;; visible children, then we don't want this article.
6409             (and (eq gnus-fetch-old-headers 'some)
6410                  (gnus-summary-article-ancient-p number)
6411                  (zerop children))
6412             ;; If this is "fetch-old-headered" and `invisible', then
6413             ;; we don't want this article.
6414             (and (eq gnus-fetch-old-headers 'invisible)
6415                  (gnus-summary-article-ancient-p number))
6416             ;; If this is a sparsely inserted article with no children,
6417             ;; we don't want it.
6418             (and (eq gnus-build-sparse-threads 'some)
6419                  (gnus-summary-article-sparse-p number)
6420                  (zerop children))
6421             ;; If we use expunging, and this article is really
6422             ;; low-scored, then we don't want this article.
6423             (when (and gnus-summary-expunge-below
6424                        (< (setq score
6425                                 (or (cdr (assq number gnus-newsgroup-scored))
6426                                     gnus-summary-default-score))
6427                           gnus-summary-expunge-below))
6428               ;; We increase the expunge-tally here, but that has
6429               ;; nothing to do with the limits, really.
6430               (incf gnus-newsgroup-expunged-tally)
6431               ;; We also mark as read here, if that's wanted.
6432               (when (and gnus-summary-mark-below
6433                          (< score gnus-summary-mark-below))
6434                 (setq gnus-newsgroup-unreads
6435                       (delq number gnus-newsgroup-unreads))
6436                 (if gnus-newsgroup-auto-expire
6437                     (push number gnus-newsgroup-expirable)
6438                   (push (cons number gnus-low-score-mark)
6439                         gnus-newsgroup-reads)))
6440               t)
6441             ;; Check NoCeM things.
6442             (if (and gnus-use-nocem
6443                      (gnus-nocem-unwanted-article-p
6444                       (mail-header-id (car thread))))
6445                 (progn
6446                   (setq gnus-newsgroup-unreads
6447                         (delq number gnus-newsgroup-unreads))
6448                   t))))
6449           ;; Nope, invisible article.
6450           0
6451         ;; Ok, this article is to be visible, so we add it to the limit
6452         ;; and return 1.
6453         (push number gnus-newsgroup-limit)
6454         1))))
6455
6456 (defun gnus-expunge-thread (thread)
6457   "Mark all articles in THREAD as read."
6458   (let* ((number (mail-header-number (car thread))))
6459     (incf gnus-newsgroup-expunged-tally)
6460     ;; We also mark as read here, if that's wanted.
6461     (setq gnus-newsgroup-unreads
6462           (delq number gnus-newsgroup-unreads))
6463     (if gnus-newsgroup-auto-expire
6464         (push number gnus-newsgroup-expirable)
6465       (push (cons number gnus-low-score-mark)
6466             gnus-newsgroup-reads)))
6467   ;; Go recursively through all subthreads.
6468   (mapcar 'gnus-expunge-thread (cdr thread)))
6469
6470 ;; Summary article oriented commands
6471
6472 (defun gnus-summary-refer-parent-article (n)
6473   "Refer parent article N times.
6474 If N is negative, go to ancestor -N instead.
6475 The difference between N and the number of articles fetched is returned."
6476   (interactive "p")
6477   (let ((skip 1)
6478         error header ref)
6479     (when (not (natnump n))
6480       (setq skip (abs n)
6481             n 1))
6482     (while (and (> n 0)
6483                 (not error))
6484       (setq header (gnus-summary-article-header))
6485       (if (and (eq (mail-header-number header)
6486                    (cdr gnus-article-current))
6487                (equal gnus-newsgroup-name
6488                       (car gnus-article-current)))
6489           ;; If we try to find the parent of the currently
6490           ;; displayed article, then we take a look at the actual
6491           ;; References header, since this is slightly more
6492           ;; reliable than the References field we got from the
6493           ;; server.
6494           (save-excursion
6495             (set-buffer gnus-original-article-buffer)
6496             (nnheader-narrow-to-headers)
6497             (unless (setq ref (message-fetch-field "references"))
6498               (setq ref (message-fetch-field "in-reply-to")))
6499             (widen))
6500         (setq ref
6501               ;; It's not the current article, so we take a bet on
6502               ;; the value we got from the server.
6503               (mail-header-references header)))
6504       (if (and ref
6505                (not (equal ref "")))
6506           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6507             (gnus-message 1 "Couldn't find parent"))
6508         (gnus-message 1 "No references in article %d"
6509                       (gnus-summary-article-number))
6510         (setq error t))
6511       (decf n))
6512     (gnus-summary-position-point)
6513     n))
6514
6515 (defun gnus-summary-refer-references ()
6516   "Fetch all articles mentioned in the References header.
6517 Return the number of articles fetched."
6518   (interactive)
6519   (let ((ref (mail-header-references (gnus-summary-article-header)))
6520         (current (gnus-summary-article-number))
6521         (n 0))
6522     (if (or (not ref)
6523             (equal ref ""))
6524         (error "No References in the current article")
6525       ;; For each Message-ID in the References header...
6526       (while (string-match "<[^>]*>" ref)
6527         (incf n)
6528         ;; ... fetch that article.
6529         (gnus-summary-refer-article
6530          (prog1 (match-string 0 ref)
6531            (setq ref (substring ref (match-end 0))))))
6532       (gnus-summary-goto-subject current)
6533       (gnus-summary-position-point)
6534       n)))
6535
6536 (defun gnus-summary-refer-thread (&optional limit)
6537   "Fetch all articles in the current thread.
6538 If LIMIT (the numerical prefix), fetch that many old headers instead
6539 of what's specified by the `gnus-refer-thread-limit' variable."
6540   (interactive "P")
6541   (let ((id (mail-header-id (gnus-summary-article-header)))
6542         (limit (if limit (prefix-numeric-value limit)
6543                  gnus-refer-thread-limit)))
6544     ;; We want to fetch LIMIT *old* headers, but we also have to
6545     ;; re-fetch all the headers in the current buffer, because many of
6546     ;; them may be undisplayed.  So we adjust LIMIT.
6547     (when (numberp limit)
6548       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6549     (unless (eq gnus-fetch-old-headers 'invisible)
6550       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6551       ;; Retrieve the headers and read them in.
6552       (if (eq (gnus-retrieve-headers
6553                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6554               'nov)
6555           (gnus-build-all-threads)
6556         (error "Can't fetch thread from backends that don't support NOV"))
6557       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6558     (gnus-summary-limit-include-thread id)))
6559
6560 (defun gnus-summary-refer-article (message-id &optional arg)
6561   "Fetch an article specified by MESSAGE-ID.
6562 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6563 or `gnus-select-method', no matter what backend the article comes from."
6564   (interactive "sMessage-ID: \nP")
6565   (when (and (stringp message-id)
6566              (not (zerop (length message-id))))
6567     ;; Construct the correct Message-ID if necessary.
6568     ;; Suggested by tale@pawl.rpi.edu.
6569     (unless (string-match "^<" message-id)
6570       (setq message-id (concat "<" message-id)))
6571     (unless (string-match ">$" message-id)
6572       (setq message-id (concat message-id ">")))
6573     (let* ((header (gnus-id-to-header message-id))
6574            (sparse (and header
6575                         (gnus-summary-article-sparse-p
6576                          (mail-header-number header))
6577                         (memq (mail-header-number header)
6578                               gnus-newsgroup-limit))))
6579       (cond
6580        ;; If the article is present in the buffer we just go to it.
6581        ((and header
6582              (or (not (gnus-summary-article-sparse-p
6583                        (mail-header-number header)))
6584                  sparse))
6585         (prog1
6586             (gnus-summary-goto-article
6587              (mail-header-number header) nil t)
6588           (when sparse
6589             (gnus-summary-update-article (mail-header-number header)))))
6590        (t
6591         ;; We fetch the article
6592         (let ((gnus-override-method
6593                (cond ((gnus-news-group-p gnus-newsgroup-name)
6594                       gnus-refer-article-method)
6595                      (arg
6596                       (or gnus-refer-article-method gnus-select-method))
6597                      (t nil)))
6598               number)
6599           ;; Start the special refer-article method, if necessary.
6600           (when (and gnus-refer-article-method
6601                      (gnus-news-group-p gnus-newsgroup-name))
6602             (gnus-check-server gnus-refer-article-method))
6603           ;; Fetch the header, and display the article.
6604           (if (setq number (gnus-summary-insert-subject message-id))
6605               (gnus-summary-select-article nil nil nil number)
6606             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6607
6608 (defun gnus-summary-edit-parameters ()
6609   "Edit the group parameters of the current group."
6610   (interactive)
6611   (gnus-group-edit-group gnus-newsgroup-name 'params))
6612
6613 (defun gnus-summary-customize-parameters ()
6614   "Customize the group parameters of the current group."
6615   (interactive)
6616   (gnus-group-customize gnus-newsgroup-name))
6617
6618 (defun gnus-summary-enter-digest-group (&optional force)
6619   "Enter an nndoc group based on the current article.
6620 If FORCE, force a digest interpretation.  If not, try
6621 to guess what the document format is."
6622   (interactive "P")
6623   (let ((conf gnus-current-window-configuration))
6624     (save-excursion
6625       (gnus-summary-select-article))
6626     (setq gnus-current-window-configuration conf)
6627     (let* ((name (format "%s-%d"
6628                          (gnus-group-prefixed-name
6629                           gnus-newsgroup-name (list 'nndoc ""))
6630                          (save-excursion
6631                            (set-buffer gnus-summary-buffer)
6632                            gnus-current-article)))
6633            (ogroup gnus-newsgroup-name)
6634            (params (append (gnus-info-params (gnus-get-info ogroup))
6635                            (list (cons 'to-group ogroup))
6636                            (list (cons 'save-article-group ogroup))))
6637            (case-fold-search t)
6638            (buf (current-buffer))
6639            dig)
6640       (save-excursion
6641         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6642         (insert-buffer-substring gnus-original-article-buffer)
6643         ;; Remove lines that may lead nndoc to misinterpret the
6644         ;; document type.
6645         (narrow-to-region
6646          (goto-char (point-min))
6647          (or (search-forward "\n\n" nil t) (point)))
6648         (goto-char (point-min))
6649         (delete-matching-lines "^\\(Path\\):\\|^From ")
6650         (widen))
6651       (unwind-protect
6652           (if (gnus-group-read-ephemeral-group
6653                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6654                             (nndoc-article-type
6655                              ,(if force 'digest 'guess))) t)
6656               ;; Make all postings to this group go to the parent group.
6657               (nconc (gnus-info-params (gnus-get-info name))
6658                      params)
6659             ;; Couldn't select this doc group.
6660             (switch-to-buffer buf)
6661             (gnus-set-global-variables)
6662             (gnus-configure-windows 'summary)
6663             (gnus-message 3 "Article couldn't be entered?"))
6664         (kill-buffer dig)))))
6665
6666 (defun gnus-summary-read-document (n)
6667   "Open a new group based on the current article(s).
6668 This will allow you to read digests and other similar
6669 documents as newsgroups.
6670 Obeys the standard process/prefix convention."
6671   (interactive "P")
6672   (let* ((articles (gnus-summary-work-articles n))
6673          (ogroup gnus-newsgroup-name)
6674          (params (append (gnus-info-params (gnus-get-info ogroup))
6675                          (list (cons 'to-group ogroup))))
6676          article group egroup groups vgroup)
6677     (while (setq article (pop articles))
6678       (setq group (format "%s-%d" gnus-newsgroup-name article))
6679       (gnus-summary-remove-process-mark article)
6680       (when (gnus-summary-display-article article)
6681         (save-excursion
6682           (with-temp-buffer
6683             (insert-buffer-substring gnus-original-article-buffer)
6684             ;; Remove some headers that may lead nndoc to make
6685             ;; the wrong guess.
6686             (message-narrow-to-head)
6687             (goto-char (point-min))
6688             (delete-matching-lines "^\\(Path\\):\\|^From ")
6689             (widen)
6690             (if (setq egroup
6691                       (gnus-group-read-ephemeral-group
6692                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6693                                      (nndoc-article-type guess))
6694                        t nil t))
6695                 (progn
6696                   ;; Make all postings to this group go to the parent group.
6697                   (nconc (gnus-info-params (gnus-get-info egroup))
6698                          params)
6699                   (push egroup groups))
6700               ;; Couldn't select this doc group.
6701               (gnus-error 3 "Article couldn't be entered"))))))
6702     ;; Now we have selected all the documents.
6703     (cond
6704      ((not groups)
6705       (error "None of the articles could be interpreted as documents"))
6706      ((gnus-group-read-ephemeral-group
6707        (setq vgroup (format
6708                      "nnvirtual:%s-%s" gnus-newsgroup-name
6709                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6710        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6711        t
6712        (cons (current-buffer) 'summary)))
6713      (t
6714       (error "Couldn't select virtual nndoc group")))))
6715
6716 (defun gnus-summary-isearch-article (&optional regexp-p)
6717   "Do incremental search forward on the current article.
6718 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6719   (interactive "P")
6720   (gnus-summary-select-article)
6721   (gnus-configure-windows 'article)
6722   (gnus-eval-in-buffer-window gnus-article-buffer
6723     (save-restriction
6724       (widen)
6725       (isearch-forward regexp-p))))
6726
6727 (defun gnus-summary-search-article-forward (regexp &optional backward)
6728   "Search for an article containing REGEXP forward.
6729 If BACKWARD, search backward instead."
6730   (interactive
6731    (list (read-string
6732           (format "Search article %s (regexp%s): "
6733                   (if current-prefix-arg "backward" "forward")
6734                   (if gnus-last-search-regexp
6735                       (concat ", default " gnus-last-search-regexp)
6736                     "")))
6737          current-prefix-arg))
6738   (if (string-equal regexp "")
6739       (setq regexp (or gnus-last-search-regexp ""))
6740     (setq gnus-last-search-regexp regexp))
6741   (if (gnus-summary-search-article regexp backward)
6742       (gnus-summary-show-thread)
6743     (error "Search failed: \"%s\"" regexp)))
6744
6745 (defun gnus-summary-search-article-backward (regexp)
6746   "Search for an article containing REGEXP backward."
6747   (interactive
6748    (list (read-string
6749           (format "Search article backward (regexp%s): "
6750                   (if gnus-last-search-regexp
6751                       (concat ", default " gnus-last-search-regexp)
6752                     "")))))
6753   (gnus-summary-search-article-forward regexp 'backward))
6754
6755 (defun gnus-summary-search-article (regexp &optional backward)
6756   "Search for an article containing REGEXP.
6757 Optional argument BACKWARD means do search for backward.
6758 `gnus-select-article-hook' is not called during the search."
6759   ;; We have to require this here to make sure that the following
6760   ;; dynamic binding isn't shadowed by autoloading.
6761   (require 'gnus-async)
6762   (require 'gnus-art)
6763   (let ((gnus-select-article-hook nil)  ;Disable hook.
6764         (gnus-article-display-hook nil)
6765         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6766         (gnus-use-article-prefetch nil)
6767         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6768         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6769         (sum (current-buffer))
6770         (found nil)
6771         point)
6772     (gnus-save-hidden-threads
6773       (gnus-summary-select-article)
6774       (set-buffer gnus-article-buffer)
6775       (when backward
6776         (forward-line -1))
6777       (while (not found)
6778         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6779         (if (if backward
6780                 (re-search-backward regexp nil t)
6781               (re-search-forward regexp nil t))
6782             ;; We found the regexp.
6783             (progn
6784               (setq found 'found)
6785               (beginning-of-line)
6786               (set-window-start
6787                (get-buffer-window (current-buffer))
6788                (point))
6789               (forward-line 1)
6790               (set-buffer sum)
6791               (setq point (point)))
6792           ;; We didn't find it, so we go to the next article.
6793           (set-buffer sum)
6794           (setq found 'not)
6795           (while (eq found 'not)
6796             (if (not (if backward (gnus-summary-find-prev)
6797                        (gnus-summary-find-next)))
6798                 ;; No more articles.
6799                 (setq found t)
6800               ;; Select the next article and adjust point.
6801               (unless (gnus-summary-article-sparse-p
6802                        (gnus-summary-article-number))
6803                 (setq found nil)
6804                 (gnus-summary-select-article)
6805                 (set-buffer gnus-article-buffer)
6806                 (widen)
6807                 (goto-char (if backward (point-max) (point-min))))))))
6808       (gnus-message 7 ""))
6809     ;; Return whether we found the regexp.
6810     (when (eq found 'found)
6811       (goto-char point)
6812       (gnus-summary-show-thread)
6813       (gnus-summary-goto-subject gnus-current-article)
6814       (gnus-summary-position-point)
6815       t)))
6816
6817 (defun gnus-summary-find-matching (header regexp &optional backward unread
6818                                           not-case-fold)
6819   "Return a list of all articles that match REGEXP on HEADER.
6820 The search stars on the current article and goes forwards unless
6821 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6822 If UNREAD is non-nil, only unread articles will
6823 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6824 in the comparisons."
6825   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6826                 (gnus-data-find-list
6827                  (gnus-summary-article-number) (gnus-data-list backward))))
6828         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6829         (case-fold-search (not not-case-fold))
6830         articles d)
6831     (unless (fboundp (intern (concat "mail-header-" header)))
6832       (error "%s is not a valid header" header))
6833     (while data
6834       (setq d (car data))
6835       (and (or (not unread)             ; We want all articles...
6836                (gnus-data-unread-p d))  ; Or just unreads.
6837            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6838            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6839            (push (gnus-data-number d) articles)) ; Success!
6840       (setq data (cdr data)))
6841     (nreverse articles)))
6842
6843 (defun gnus-summary-execute-command (header regexp command &optional backward)
6844   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6845 If HEADER is an empty string (or nil), the match is done on the entire
6846 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6847   (interactive
6848    (list (let ((completion-ignore-case t))
6849            (completing-read
6850             "Header name: "
6851             (mapcar (lambda (string) (list string))
6852                     '("Number" "Subject" "From" "Lines" "Date"
6853                       "Message-ID" "Xref" "References" "Body"))
6854             nil 'require-match))
6855          (read-string "Regexp: ")
6856          (read-key-sequence "Command: ")
6857          current-prefix-arg))
6858   (when (equal header "Body")
6859     (setq header ""))
6860   ;; Hidden thread subtrees must be searched as well.
6861   (gnus-summary-show-all-threads)
6862   ;; We don't want to change current point nor window configuration.
6863   (save-excursion
6864     (save-window-excursion
6865       (gnus-message 6 "Executing %s..." (key-description command))
6866       ;; We'd like to execute COMMAND interactively so as to give arguments.
6867       (gnus-execute header regexp
6868                     `(call-interactively ',(key-binding command))
6869                     backward)
6870       (gnus-message 6 "Executing %s...done" (key-description command)))))
6871
6872 (defun gnus-summary-beginning-of-article ()
6873   "Scroll the article back to the beginning."
6874   (interactive)
6875   (gnus-summary-select-article)
6876   (gnus-configure-windows 'article)
6877   (gnus-eval-in-buffer-window gnus-article-buffer
6878     (widen)
6879     (goto-char (point-min))
6880     (when gnus-page-broken
6881       (gnus-narrow-to-page))))
6882
6883 (defun gnus-summary-end-of-article ()
6884   "Scroll to the end of the article."
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-max))
6891     (recenter -3)
6892     (when gnus-page-broken
6893       (gnus-narrow-to-page))))
6894
6895 (defun gnus-summary-print-article (&optional filename n)
6896   "Generate and print a PostScript image of the N next (mail) articles.
6897
6898 If N is negative, print the N previous articles.  If N is nil and articles
6899 have been marked with the process mark, print these instead.
6900
6901 If the optional first argument FILENAME is nil, send the image to the
6902 printer.  If FILENAME is a string, save the PostScript image in a file with
6903 that name.  If FILENAME is a number, prompt the user for the name of the file
6904 to save in."
6905   (interactive (list (ps-print-preprint current-prefix-arg)
6906                      current-prefix-arg))
6907   (dolist (article (gnus-summary-work-articles n))
6908     (gnus-summary-select-article nil nil 'pseudo article)
6909     (gnus-eval-in-buffer-window gnus-article-buffer
6910       (let ((buffer (generate-new-buffer " *print*")))
6911         (unwind-protect
6912             (progn
6913               (copy-to-buffer buffer (point-min) (point-max))
6914               (set-buffer buffer)
6915               (gnus-article-delete-invisible-text)
6916               (let ((ps-left-header
6917                      (list
6918                       (concat "("
6919                               (mail-header-subject gnus-current-headers) ")")
6920                       (concat "("
6921                               (mail-header-from gnus-current-headers) ")")))
6922                     (ps-right-header
6923                      (list
6924                       "/pagenumberstring load"
6925                       (concat "("
6926                               (mail-header-date gnus-current-headers) ")"))))
6927                 (gnus-run-hooks 'gnus-ps-print-hook)
6928                 (save-excursion
6929                   (ps-print-buffer-with-faces filename))))
6930           (kill-buffer buffer))))))
6931
6932 (defun gnus-summary-show-article (&optional arg)
6933   "Force re-fetching of the current article.
6934 If ARG (the prefix) is non-nil, show the raw article without any
6935 article massaging functions being run."
6936   (interactive "P")
6937   (if (not arg)
6938       ;; Select the article the normal way.
6939       (gnus-summary-select-article nil 'force)
6940     ;; We have to require this here to make sure that the following
6941     ;; dynamic binding isn't shadowed by autoloading.
6942     (require 'gnus-async)
6943     (require 'gnus-art)
6944     ;; Bind the article treatment functions to nil.
6945     (let ((gnus-have-all-headers t)
6946           gnus-article-display-hook
6947           gnus-article-prepare-hook
6948           gnus-article-decode-hook
6949           gnus-break-pages
6950           gnus-show-mime
6951           gnus-visual)
6952       (gnus-summary-select-article nil 'force)))
6953   (gnus-summary-goto-subject gnus-current-article)
6954   (gnus-summary-position-point))
6955
6956 (defun gnus-summary-verbose-headers (&optional arg)
6957   "Toggle permanent full header display.
6958 If ARG is a positive number, turn header display on.
6959 If ARG is a negative number, turn header display off."
6960   (interactive "P")
6961   (setq gnus-show-all-headers
6962         (cond ((or (not (numberp arg))
6963                    (zerop arg))
6964                (not gnus-show-all-headers))
6965               ((natnump arg)
6966                t)))
6967   (gnus-summary-show-article))
6968
6969 (defun gnus-summary-toggle-header (&optional arg)
6970   "Show the headers if they are hidden, or hide them if they are shown.
6971 If ARG is a positive number, show the entire header.
6972 If ARG is a negative number, hide the unwanted header lines."
6973   (interactive "P")
6974   (save-excursion
6975     (set-buffer gnus-article-buffer)
6976     (let* ((buffer-read-only nil)
6977            (inhibit-point-motion-hooks t)
6978            (hidden (gnus-article-hidden-text-p 'headers))
6979            e)
6980       (goto-char (point-min))
6981       (when (search-forward "\n\n" nil t)
6982         (delete-region (point-min) (1- (point))))
6983       (goto-char (point-min))
6984       (save-excursion
6985         (set-buffer gnus-original-article-buffer)
6986         (goto-char (point-min))
6987         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6988       (insert-buffer-substring gnus-original-article-buffer 1 e)
6989       (let ((article-inhibit-hiding t))
6990         (gnus-run-hooks 'gnus-article-display-hook))
6991       (if (or hidden
6992               (and (numberp arg) (< arg 0)))
6993           (let ((gnus-treat-hide-headers nil)
6994                 (gnus-treat-hide-boring-headers nil))
6995             (gnus-treat-article 'head))
6996         (gnus-treat-article 'head)))))
6997
6998 (defun gnus-summary-show-all-headers ()
6999   "Make all header lines visible."
7000   (interactive)
7001   (gnus-article-show-all-headers))
7002
7003 (defun gnus-summary-toggle-mime (&optional arg)
7004   "Toggle MIME processing.
7005 If ARG is a positive number, turn MIME processing on."
7006   (interactive "P")
7007   (setq gnus-show-mime
7008         (if (null arg)
7009             (not gnus-show-mime)
7010           (> (prefix-numeric-value arg) 0)))
7011   (gnus-summary-select-article t 'force))
7012
7013 (defun gnus-summary-caesar-message (&optional arg)
7014   "Caesar rotate the current article by 13.
7015 The numerical prefix specifies how many places to rotate each letter
7016 forward."
7017   (interactive "P")
7018   (gnus-summary-select-article)
7019   (let ((mail-header-separator ""))
7020     (gnus-eval-in-buffer-window gnus-article-buffer
7021       (save-restriction
7022         (widen)
7023         (let ((start (window-start))
7024               buffer-read-only)
7025           (message-caesar-buffer-body arg)
7026           (set-window-start (get-buffer-window (current-buffer)) start))))))
7027
7028 (defun gnus-summary-stop-page-breaking ()
7029   "Stop page breaking in the current article."
7030   (interactive)
7031   (gnus-summary-select-article)
7032   (gnus-eval-in-buffer-window gnus-article-buffer
7033     (widen)
7034     (when (gnus-visual-p 'page-marker)
7035       (let ((buffer-read-only nil))
7036         (gnus-remove-text-with-property 'gnus-prev)
7037         (gnus-remove-text-with-property 'gnus-next))
7038       (setq gnus-page-broken nil))))
7039
7040 (defun gnus-summary-move-article (&optional n to-newsgroup
7041                                             select-method action)
7042   "Move the current article to a different newsgroup.
7043 If N is a positive number, move the N next articles.
7044 If N is a negative number, move the N previous articles.
7045 If N is nil and any articles have been marked with the process mark,
7046 move those articles instead.
7047 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7048 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7049 re-spool using this method.
7050
7051 For this function to work, both the current newsgroup and the
7052 newsgroup that you want to move to have to support the `request-move'
7053 and `request-accept' functions."
7054   (interactive "P")
7055   (unless action
7056     (setq action 'move))
7057   ;; Disable marking as read.
7058   (let (gnus-mark-article-hook)
7059     (save-window-excursion
7060       (gnus-summary-select-article)))
7061   ;; Check whether the source group supports the required functions.
7062   (cond ((and (eq action 'move)
7063               (not (gnus-check-backend-function
7064                     'request-move-article gnus-newsgroup-name)))
7065          (error "The current group does not support article moving"))
7066         ((and (eq action 'crosspost)
7067               (not (gnus-check-backend-function
7068                     'request-replace-article gnus-newsgroup-name)))
7069          (error "The current group does not support article editing")))
7070   (let ((articles (gnus-summary-work-articles n))
7071         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7072         (names '((move "Move" "Moving")
7073                  (copy "Copy" "Copying")
7074                  (crosspost "Crosspost" "Crossposting")))
7075         (copy-buf (save-excursion
7076                     (nnheader-set-temp-buffer " *copy article*")))
7077         art-group to-method new-xref article to-groups)
7078     (unless (assq action names)
7079       (error "Unknown action %s" action))
7080     ;; Read the newsgroup name.
7081     (when (and (not to-newsgroup)
7082                (not select-method))
7083       (setq to-newsgroup
7084             (gnus-read-move-group-name
7085              (cadr (assq action names))
7086              (symbol-value (intern (format "gnus-current-%s-group" action)))
7087              articles prefix))
7088       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7089     (setq to-method (or select-method
7090                         (gnus-group-name-to-method to-newsgroup)))
7091     ;; Check the method we are to move this article to...
7092     (unless (gnus-check-backend-function
7093              'request-accept-article (car to-method))
7094       (error "%s does not support article copying" (car to-method)))
7095     (unless (gnus-check-server to-method)
7096       (error "Can't open server %s" (car to-method)))
7097     (gnus-message 6 "%s to %s: %s..."
7098                   (caddr (assq action names))
7099                   (or (car select-method) to-newsgroup) articles)
7100     (while articles
7101       (setq article (pop articles))
7102       (setq
7103        art-group
7104        (cond
7105         ;; Move the article.
7106         ((eq action 'move)
7107          ;; Remove this article from future suppression.
7108          (gnus-dup-unsuppress-article article)
7109          (gnus-request-move-article
7110           article                       ; Article to move
7111           gnus-newsgroup-name           ; From newsgroup
7112           (nth 1 (gnus-find-method-for-group
7113                   gnus-newsgroup-name)) ; Server
7114           (list 'gnus-request-accept-article
7115                 to-newsgroup (list 'quote select-method)
7116                 (not articles) t)               ; Accept form
7117           (not articles)))              ; Only save nov last time
7118         ;; Copy the article.
7119         ((eq action 'copy)
7120          (save-excursion
7121            (set-buffer copy-buf)
7122            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7123              (gnus-request-accept-article
7124               to-newsgroup select-method (not articles) t))))
7125         ;; Crosspost the article.
7126         ((eq action 'crosspost)
7127          (let ((xref (message-tokenize-header
7128                       (mail-header-xref (gnus-summary-article-header article))
7129                       " ")))
7130            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7131                                   ":" article))
7132            (unless xref
7133              (setq xref (list (system-name))))
7134            (setq new-xref
7135                  (concat
7136                   (mapconcat 'identity
7137                              (delete "Xref:" (delete new-xref xref))
7138                              " ")
7139                   " " new-xref))
7140            (save-excursion
7141              (set-buffer copy-buf)
7142              ;; First put the article in the destination group.
7143              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7144              (when (consp (setq art-group
7145                                 (gnus-request-accept-article
7146                                  to-newsgroup select-method (not articles))))
7147                (setq new-xref (concat new-xref " " (car art-group)
7148                                       ":" (cdr art-group)))
7149                ;; Now we have the new Xrefs header, so we insert
7150                ;; it and replace the new article.
7151                (nnheader-replace-header "Xref" new-xref)
7152                (gnus-request-replace-article
7153                 (cdr art-group) to-newsgroup (current-buffer))
7154                art-group))))))
7155       (cond
7156        ((not art-group)
7157         (gnus-message 1 "Couldn't %s article %s"
7158                       (cadr (assq action names)) article))
7159        ((and (eq art-group 'junk)
7160              (eq action 'move))
7161         (gnus-summary-mark-article article gnus-canceled-mark)
7162         (gnus-message 4 "Deleted article %s" article))
7163        (t
7164         (let* ((pto-group (gnus-group-prefixed-name
7165                            (car art-group) to-method))
7166                (entry
7167                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7168                (info (nth 2 entry))
7169                (to-group (gnus-info-group info)))
7170           ;; Update the group that has been moved to.
7171           (when (and info
7172                      (memq action '(move copy)))
7173             (unless (member to-group to-groups)
7174               (push to-group to-groups))
7175
7176             (unless (memq article gnus-newsgroup-unreads)
7177               (gnus-info-set-read
7178                info (gnus-add-to-range (gnus-info-read info)
7179                                        (list (cdr art-group)))))
7180
7181             ;; Copy any marks over to the new group.
7182             (let ((marks gnus-article-mark-lists)
7183                   (to-article (cdr art-group)))
7184               (unless (gnus-group-auto-expirable-p to-group)
7185                 (setq marks (delete '(expirable . expire) marks)))
7186
7187               ;; See whether the article is to be put in the cache.
7188               (when gnus-use-cache
7189                 (gnus-cache-possibly-enter-article
7190                  to-group to-article
7191                  (let ((header (copy-sequence
7192                                 (gnus-summary-article-header article))))
7193                    (mail-header-set-number header to-article)
7194                    header)
7195                  (memq article gnus-newsgroup-marked)
7196                  (memq article gnus-newsgroup-dormant)
7197                  (memq article gnus-newsgroup-unreads)))
7198
7199               (when (and (equal to-group gnus-newsgroup-name)
7200                          (not (memq article gnus-newsgroup-unreads)))
7201                 ;; Mark this article as read in this group.
7202                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7203                 (setcdr (gnus-active to-group) to-article)
7204                 (setcdr gnus-newsgroup-active to-article))
7205
7206               (while marks
7207                 (when (memq article (symbol-value
7208                                      (intern (format "gnus-newsgroup-%s"
7209                                                      (caar marks)))))
7210                   ;; If the other group is the same as this group,
7211                   ;; then we have to add the mark to the list.
7212                   (when (equal to-group gnus-newsgroup-name)
7213                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7214                          (cons to-article
7215                                (symbol-value
7216                                 (intern (format "gnus-newsgroup-%s"
7217                                                 (caar marks)))))))
7218                   ;; Copy the marks to other group.
7219                   (gnus-add-marked-articles
7220                    to-group (cdar marks) (list to-article) info))
7221                 (setq marks (cdr marks)))
7222
7223               (gnus-dribble-enter
7224                (concat "(gnus-group-set-info '"
7225                        (gnus-prin1-to-string (gnus-get-info to-group))
7226                        ")"))))
7227
7228           ;; Update the Xref header in this article to point to
7229           ;; the new crossposted article we have just created.
7230           (when (eq action 'crosspost)
7231             (save-excursion
7232               (set-buffer copy-buf)
7233               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7234               (nnheader-replace-header "Xref" new-xref)
7235               (gnus-request-replace-article
7236                article gnus-newsgroup-name (current-buffer)))))
7237
7238         ;;;!!!Why is this necessary?
7239         (set-buffer gnus-summary-buffer)
7240
7241         (gnus-summary-goto-subject article)
7242         (when (eq action 'move)
7243           (gnus-summary-mark-article article gnus-canceled-mark))))
7244       (gnus-summary-remove-process-mark article))
7245     ;; Re-activate all groups that have been moved to.
7246     (while to-groups
7247       (save-excursion
7248         (set-buffer gnus-group-buffer)
7249         (when (gnus-group-goto-group (car to-groups) t)
7250           (gnus-group-get-new-news-this-group 1 t))
7251         (pop to-groups)))
7252
7253     (gnus-kill-buffer copy-buf)
7254     (gnus-summary-position-point)
7255     (gnus-set-mode-line 'summary)))
7256
7257 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7258   "Move the current article to a different newsgroup.
7259 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7260 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7261 re-spool using this method."
7262   (interactive "P")
7263   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7264
7265 (defun gnus-summary-crosspost-article (&optional n)
7266   "Crosspost the current article to some other group."
7267   (interactive "P")
7268   (gnus-summary-move-article n nil nil 'crosspost))
7269
7270 (defcustom gnus-summary-respool-default-method nil
7271   "Default method for respooling an article.
7272 If nil, use to the current newsgroup method."
7273   :type '(choice (gnus-select-method :value (nnml ""))
7274                  (const nil))
7275   :group 'gnus-summary-mail)
7276
7277 (defun gnus-summary-respool-article (&optional n method)
7278   "Respool the current article.
7279 The article will be squeezed through the mail spooling process again,
7280 which means that it will be put in some mail newsgroup or other
7281 depending on `nnmail-split-methods'.
7282 If N is a positive number, respool the N next articles.
7283 If N is a negative number, respool the N previous articles.
7284 If N is nil and any articles have been marked with the process mark,
7285 respool those articles instead.
7286
7287 Respooling can be done both from mail groups and \"real\" newsgroups.
7288 In the former case, the articles in question will be moved from the
7289 current group into whatever groups they are destined to.  In the
7290 latter case, they will be copied into the relevant groups."
7291   (interactive
7292    (list current-prefix-arg
7293          (let* ((methods (gnus-methods-using 'respool))
7294                 (methname
7295                  (symbol-name (or gnus-summary-respool-default-method
7296                                   (car (gnus-find-method-for-group
7297                                         gnus-newsgroup-name)))))
7298                 (method
7299                  (gnus-completing-read
7300                   methname "What backend do you want to use when respooling?"
7301                   methods nil t nil 'gnus-mail-method-history))
7302                 ms)
7303            (cond
7304             ((zerop (length (setq ms (gnus-servers-using-backend
7305                                       (intern method)))))
7306              (list (intern method) ""))
7307             ((= 1 (length ms))
7308              (car ms))
7309             (t
7310              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7311                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7312                            ms-alist))))))))
7313   (unless method
7314     (error "No method given for respooling"))
7315   (if (assoc (symbol-name
7316               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7317              (gnus-methods-using 'respool))
7318       (gnus-summary-move-article n nil method)
7319     (gnus-summary-copy-article n nil method)))
7320
7321 (defun gnus-summary-import-article (file)
7322   "Import an arbitrary file into a mail newsgroup."
7323   (interactive "fImport file: ")
7324   (let ((group gnus-newsgroup-name)
7325         (now (current-time))
7326         atts lines)
7327     (unless (gnus-check-backend-function 'request-accept-article group)
7328       (error "%s does not support article importing" group))
7329     (or (file-readable-p file)
7330         (not (file-regular-p file))
7331         (error "Can't read %s" file))
7332     (save-excursion
7333       (set-buffer (gnus-get-buffer-create " *import file*"))
7334       (erase-buffer)
7335       (nnheader-insert-file-contents file)
7336       (goto-char (point-min))
7337       (unless (nnheader-article-p)
7338         ;; This doesn't look like an article, so we fudge some headers.
7339         (setq atts (file-attributes file)
7340               lines (count-lines (point-min) (point-max)))
7341         (insert "From: " (read-string "From: ") "\n"
7342                 "Subject: " (read-string "Subject: ") "\n"
7343                 "Date: " (message-make-date (nth 5 atts))
7344                 "\n"
7345                 "Message-ID: " (message-make-message-id) "\n"
7346                 "Lines: " (int-to-string lines) "\n"
7347                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7348       (gnus-request-accept-article group nil t)
7349       (kill-buffer (current-buffer)))))
7350
7351 (defun gnus-summary-article-posted-p ()
7352   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7353 This will be the case if the article has both been mailed and posted."
7354   (interactive)
7355   (let ((id (mail-header-references (gnus-summary-article-header)))
7356         (gnus-override-method
7357          (or gnus-refer-article-method gnus-select-method)))
7358     (if (gnus-request-head id "")
7359         (gnus-message 2 "The current message was found on %s"
7360                       gnus-override-method)
7361       (gnus-message 2 "The current message couldn't be found on %s"
7362                     gnus-override-method)
7363       nil)))
7364
7365 (defun gnus-summary-expire-articles (&optional now)
7366   "Expire all articles that are marked as expirable in the current group."
7367   (interactive)
7368   (when (gnus-check-backend-function
7369          'request-expire-articles gnus-newsgroup-name)
7370     ;; This backend supports expiry.
7371     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7372            (expirable (if total
7373                           (progn
7374                             ;; We need to update the info for
7375                             ;; this group for `gnus-list-of-read-articles'
7376                             ;; to give us the right answer.
7377                             (gnus-run-hooks 'gnus-exit-group-hook)
7378                             (gnus-summary-update-info)
7379                             (gnus-list-of-read-articles gnus-newsgroup-name))
7380                         (setq gnus-newsgroup-expirable
7381                               (sort gnus-newsgroup-expirable '<))))
7382            (expiry-wait (if now 'immediate
7383                           (gnus-group-find-parameter
7384                            gnus-newsgroup-name 'expiry-wait)))
7385            es)
7386       (when expirable
7387         ;; There are expirable articles in this group, so we run them
7388         ;; through the expiry process.
7389         (gnus-message 6 "Expiring articles...")
7390         ;; The list of articles that weren't expired is returned.
7391         (save-excursion
7392           (if expiry-wait
7393               (let ((nnmail-expiry-wait-function nil)
7394                     (nnmail-expiry-wait expiry-wait))
7395                 (setq es (gnus-request-expire-articles
7396                           expirable gnus-newsgroup-name)))
7397             (setq es (gnus-request-expire-articles
7398                       expirable gnus-newsgroup-name))))
7399         (unless total
7400           (setq gnus-newsgroup-expirable es))
7401         ;; We go through the old list of expirable, and mark all
7402         ;; really expired articles as nonexistent.
7403         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7404           (let ((gnus-use-cache nil))
7405             (while expirable
7406               (unless (memq (car expirable) es)
7407                 (when (gnus-data-find (car expirable))
7408                   (gnus-summary-mark-article
7409                    (car expirable) gnus-canceled-mark)))
7410               (setq expirable (cdr expirable)))))
7411         (gnus-message 6 "Expiring articles...done")))))
7412
7413 (defun gnus-summary-expire-articles-now ()
7414   "Expunge all expirable articles in the current group.
7415 This means that *all* articles that are marked as expirable will be
7416 deleted forever, right now."
7417   (interactive)
7418   (or gnus-expert-user
7419       (gnus-yes-or-no-p
7420        "Are you really, really, really sure you want to delete all these messages? ")
7421       (error "Phew!"))
7422   (gnus-summary-expire-articles t))
7423
7424 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7425 (defun gnus-summary-delete-article (&optional n)
7426   "Delete the N next (mail) articles.
7427 This command actually deletes articles.  This is not a marking
7428 command.  The article will disappear forever from your life, never to
7429 return.
7430 If N is negative, delete backwards.
7431 If N is nil and articles have been marked with the process mark,
7432 delete these instead."
7433   (interactive "P")
7434   (unless (gnus-check-backend-function 'request-expire-articles
7435                                        gnus-newsgroup-name)
7436     (error "The current newsgroup does not support article deletion"))
7437   ;; Compute the list of articles to delete.
7438   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7439         not-deleted)
7440     (if (and gnus-novice-user
7441              (not (gnus-yes-or-no-p
7442                    (format "Do you really want to delete %s forever? "
7443                            (if (> (length articles) 1)
7444                                (format "these %s articles" (length articles))
7445                              "this article")))))
7446         ()
7447       ;; Delete the articles.
7448       (setq not-deleted (gnus-request-expire-articles
7449                          articles gnus-newsgroup-name 'force))
7450       (while articles
7451         (gnus-summary-remove-process-mark (car articles))
7452         ;; The backend might not have been able to delete the article
7453         ;; after all.
7454         (unless (memq (car articles) not-deleted)
7455           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7456         (setq articles (cdr articles)))
7457       (when not-deleted
7458         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7459     (gnus-summary-position-point)
7460     (gnus-set-mode-line 'summary)
7461     not-deleted))
7462
7463 (defun gnus-summary-edit-article (&optional force)
7464   "Edit the current article.
7465 This will have permanent effect only in mail groups.
7466 If FORCE is non-nil, allow editing of articles even in read-only
7467 groups."
7468   (interactive "P")
7469   (let ((mail-parse-charset gnus-newsgroup-charset))
7470     (save-excursion
7471       (set-buffer gnus-summary-buffer)
7472       (gnus-set-global-variables)
7473       (when (and (not force)
7474                  (gnus-group-read-only-p))
7475         (error "The current newsgroup does not support article editing"))
7476       (gnus-summary-show-article t)
7477       (gnus-article-edit-article
7478        'mime-to-mml
7479        `(lambda (no-highlight)
7480           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7481             (mml-to-mime)
7482             (gnus-summary-edit-article-done
7483              ,(or (mail-header-references gnus-current-headers) "")
7484              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7485
7486 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7487
7488 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7489                                                  no-highlight)
7490   "Make edits to the current article permanent."
7491   (interactive)
7492   ;; Replace the article.
7493   (let ((buf (current-buffer)))
7494     (with-temp-buffer
7495       (insert-buffer-substring buf)
7496       (if (and (not read-only)
7497                (not (gnus-request-replace-article
7498                      (cdr gnus-article-current) (car gnus-article-current)
7499                      (current-buffer) t)))
7500           (error "Couldn't replace article")
7501         ;; Update the summary buffer.
7502         (if (and references
7503                  (equal (message-tokenize-header references " ")
7504                         (message-tokenize-header
7505                          (or (message-fetch-field "references") "") " ")))
7506             ;; We only have to update this line.
7507             (save-excursion
7508               (save-restriction
7509                 (message-narrow-to-head)
7510                 (let ((head (buffer-string))
7511                       header)
7512                   (with-temp-buffer
7513                     (insert (format "211 %d Article retrieved.\n"
7514                                     (cdr gnus-article-current)))
7515                     (insert head)
7516                     (insert ".\n")
7517                     (let ((nntp-server-buffer (current-buffer)))
7518                       (setq header (car (gnus-get-newsgroup-headers
7519                                          (save-excursion
7520                                            (set-buffer gnus-summary-buffer)
7521                                            gnus-newsgroup-dependencies)
7522                                          t))))
7523                     (save-excursion
7524                       (set-buffer gnus-summary-buffer)
7525                       (gnus-data-set-header
7526                        (gnus-data-find (cdr gnus-article-current))
7527                        header)
7528                       (gnus-summary-update-article-line
7529                        (cdr gnus-article-current) header))))))
7530           ;; Update threads.
7531           (set-buffer (or buffer gnus-summary-buffer))
7532           (gnus-summary-update-article (cdr gnus-article-current)))
7533         ;; Prettify the article buffer again.
7534         (unless no-highlight
7535           (save-excursion
7536             (set-buffer gnus-article-buffer)
7537             ;;;!!! Fix this -- article should be rehighlighted.
7538             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7539             (set-buffer gnus-original-article-buffer)
7540             (gnus-request-article
7541              (cdr gnus-article-current)
7542              (car gnus-article-current) (current-buffer))))
7543         ;; Prettify the summary buffer line.
7544         (when (gnus-visual-p 'summary-highlight 'highlight)
7545           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7546
7547 (defun gnus-summary-edit-wash (key)
7548   "Perform editing command KEY in the article buffer."
7549   (interactive
7550    (list
7551     (progn
7552       (message "%s" (concat (this-command-keys) "- "))
7553       (read-char))))
7554   (message "")
7555   (gnus-summary-edit-article)
7556   (execute-kbd-macro (concat (this-command-keys) key))
7557   (gnus-article-edit-done))
7558
7559 ;;; Respooling
7560
7561 (defun gnus-summary-respool-query (&optional silent trace)
7562   "Query where the respool algorithm would put this article."
7563   (interactive)
7564   (let (gnus-mark-article-hook)
7565     (gnus-summary-select-article)
7566     (save-excursion
7567       (set-buffer gnus-original-article-buffer)
7568       (save-restriction
7569         (message-narrow-to-head)
7570         (let ((groups (nnmail-article-group 'identity trace)))
7571           (unless silent
7572             (if groups
7573                 (message "This message would go to %s"
7574                          (mapconcat 'car groups ", "))
7575               (message "This message would go to no groups"))
7576             groups))))))
7577
7578 (defun gnus-summary-respool-trace ()
7579   "Trace where the respool algorithm would put this article.
7580 Display a buffer showing all fancy splitting patterns which matched."
7581   (interactive)
7582   (gnus-summary-respool-query nil t))
7583
7584 ;; Summary marking commands.
7585
7586 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7587   "Mark articles which has the same subject as read, and then select the next.
7588 If UNMARK is positive, remove any kind of mark.
7589 If UNMARK is negative, tick articles."
7590   (interactive "P")
7591   (when unmark
7592     (setq unmark (prefix-numeric-value unmark)))
7593   (let ((count
7594          (gnus-summary-mark-same-subject
7595           (gnus-summary-article-subject) unmark)))
7596     ;; Select next unread article.  If auto-select-same mode, should
7597     ;; select the first unread article.
7598     (gnus-summary-next-article t (and gnus-auto-select-same
7599                                       (gnus-summary-article-subject)))
7600     (gnus-message 7 "%d article%s marked as %s"
7601                   count (if (= count 1) " is" "s are")
7602                   (if unmark "unread" "read"))))
7603
7604 (defun gnus-summary-kill-same-subject (&optional unmark)
7605   "Mark articles which has the same subject as read.
7606 If UNMARK is positive, remove any kind of mark.
7607 If UNMARK is negative, tick articles."
7608   (interactive "P")
7609   (when unmark
7610     (setq unmark (prefix-numeric-value unmark)))
7611   (let ((count
7612          (gnus-summary-mark-same-subject
7613           (gnus-summary-article-subject) unmark)))
7614     ;; If marked as read, go to next unread subject.
7615     (when (null unmark)
7616       ;; Go to next unread subject.
7617       (gnus-summary-next-subject 1 t))
7618     (gnus-message 7 "%d articles are marked as %s"
7619                   count (if unmark "unread" "read"))))
7620
7621 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7622   "Mark articles with same SUBJECT as read, and return marked number.
7623 If optional argument UNMARK is positive, remove any kinds of marks.
7624 If optional argument UNMARK is negative, mark articles as unread instead."
7625   (let ((count 1))
7626     (save-excursion
7627       (cond
7628        ((null unmark)                   ; Mark as read.
7629         (while (and
7630                 (progn
7631                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7632                   (gnus-summary-show-thread) t)
7633                 (gnus-summary-find-subject subject))
7634           (setq count (1+ count))))
7635        ((> unmark 0)                    ; Tick.
7636         (while (and
7637                 (progn
7638                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7639                   (gnus-summary-show-thread) t)
7640                 (gnus-summary-find-subject subject))
7641           (setq count (1+ count))))
7642        (t                               ; Mark as unread.
7643         (while (and
7644                 (progn
7645                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7646                   (gnus-summary-show-thread) t)
7647                 (gnus-summary-find-subject subject))
7648           (setq count (1+ count)))))
7649       (gnus-set-mode-line 'summary)
7650       ;; Return the number of marked articles.
7651       count)))
7652
7653 (defun gnus-summary-mark-as-processable (n &optional unmark)
7654   "Set the process mark on the next N articles.
7655 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7656 the process mark instead.  The difference between N and the actual
7657 number of articles marked is returned."
7658   (interactive "p")
7659   (let ((backward (< n 0))
7660         (n (abs n)))
7661     (while (and
7662             (> n 0)
7663             (if unmark
7664                 (gnus-summary-remove-process-mark
7665                  (gnus-summary-article-number))
7666               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7667             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7668       (setq n (1- n)))
7669     (when (/= 0 n)
7670       (gnus-message 7 "No more articles"))
7671     (gnus-summary-recenter)
7672     (gnus-summary-position-point)
7673     n))
7674
7675 (defun gnus-summary-unmark-as-processable (n)
7676   "Remove the process mark from the next N articles.
7677 If N is negative, unmark backward instead.  The difference between N and
7678 the actual number of articles unmarked is returned."
7679   (interactive "p")
7680   (gnus-summary-mark-as-processable n t))
7681
7682 (defun gnus-summary-unmark-all-processable ()
7683   "Remove the process mark from all articles."
7684   (interactive)
7685   (save-excursion
7686     (while gnus-newsgroup-processable
7687       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7688   (gnus-summary-position-point))
7689
7690 (defun gnus-summary-mark-as-expirable (n)
7691   "Mark N articles forward as expirable.
7692 If N is negative, mark backward instead.  The difference between N and
7693 the actual number of articles marked is returned."
7694   (interactive "p")
7695   (gnus-summary-mark-forward n gnus-expirable-mark))
7696
7697 (defun gnus-summary-mark-article-as-replied (article)
7698   "Mark ARTICLE replied and update the summary line."
7699   (push article gnus-newsgroup-replied)
7700   (let ((buffer-read-only nil))
7701     (when (gnus-summary-goto-subject article nil t)
7702       (gnus-summary-update-secondary-mark article))))
7703
7704 (defun gnus-summary-set-bookmark (article)
7705   "Set a bookmark in current article."
7706   (interactive (list (gnus-summary-article-number)))
7707   (when (or (not (get-buffer gnus-article-buffer))
7708             (not gnus-current-article)
7709             (not gnus-article-current)
7710             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7711     (error "No current article selected"))
7712   ;; Remove old bookmark, if one exists.
7713   (let ((old (assq article gnus-newsgroup-bookmarks)))
7714     (when old
7715       (setq gnus-newsgroup-bookmarks
7716             (delq old gnus-newsgroup-bookmarks))))
7717   ;; Set the new bookmark, which is on the form
7718   ;; (article-number . line-number-in-body).
7719   (push
7720    (cons article
7721          (save-excursion
7722            (set-buffer gnus-article-buffer)
7723            (count-lines
7724             (min (point)
7725                  (save-excursion
7726                    (goto-char (point-min))
7727                    (search-forward "\n\n" nil t)
7728                    (point)))
7729             (point))))
7730    gnus-newsgroup-bookmarks)
7731   (gnus-message 6 "A bookmark has been added to the current article."))
7732
7733 (defun gnus-summary-remove-bookmark (article)
7734   "Remove the bookmark from the current article."
7735   (interactive (list (gnus-summary-article-number)))
7736   ;; Remove old bookmark, if one exists.
7737   (let ((old (assq article gnus-newsgroup-bookmarks)))
7738     (if old
7739         (progn
7740           (setq gnus-newsgroup-bookmarks
7741                 (delq old gnus-newsgroup-bookmarks))
7742           (gnus-message 6 "Removed bookmark."))
7743       (gnus-message 6 "No bookmark in current article."))))
7744
7745 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7746 (defun gnus-summary-mark-as-dormant (n)
7747   "Mark N articles forward as dormant.
7748 If N is negative, mark backward instead.  The difference between N and
7749 the actual number of articles marked is returned."
7750   (interactive "p")
7751   (gnus-summary-mark-forward n gnus-dormant-mark))
7752
7753 (defun gnus-summary-set-process-mark (article)
7754   "Set the process mark on ARTICLE and update the summary line."
7755   (setq gnus-newsgroup-processable
7756         (cons article
7757               (delq article gnus-newsgroup-processable)))
7758   (when (gnus-summary-goto-subject article)
7759     (gnus-summary-show-thread)
7760     (gnus-summary-goto-subject article)
7761     (gnus-summary-update-secondary-mark article)))
7762
7763 (defun gnus-summary-remove-process-mark (article)
7764   "Remove the process mark from ARTICLE and update the summary line."
7765   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7766   (when (gnus-summary-goto-subject article)
7767     (gnus-summary-show-thread)
7768     (gnus-summary-goto-subject article)
7769     (gnus-summary-update-secondary-mark article)))
7770
7771 (defun gnus-summary-set-saved-mark (article)
7772   "Set the process mark on ARTICLE and update the summary line."
7773   (push article gnus-newsgroup-saved)
7774   (when (gnus-summary-goto-subject article)
7775     (gnus-summary-update-secondary-mark article)))
7776
7777 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7778   "Mark N articles as read forwards.
7779 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7780 The difference between N and the actual number of articles marked is
7781 returned."
7782   (interactive "p")
7783   (gnus-summary-show-thread)
7784   (let ((backward (< n 0))
7785         (gnus-summary-goto-unread
7786          (and gnus-summary-goto-unread
7787               (not (eq gnus-summary-goto-unread 'never))
7788               (not (memq mark (list gnus-unread-mark
7789                                     gnus-ticked-mark gnus-dormant-mark)))))
7790         (n (abs n))
7791         (mark (or mark gnus-del-mark)))
7792     (while (and (> n 0)
7793                 (gnus-summary-mark-article nil mark no-expire)
7794                 (zerop (gnus-summary-next-subject
7795                         (if backward -1 1)
7796                         (and gnus-summary-goto-unread
7797                              (not (eq gnus-summary-goto-unread 'never)))
7798                         t)))
7799       (setq n (1- n)))
7800     (when (/= 0 n)
7801       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7802     (gnus-summary-recenter)
7803     (gnus-summary-position-point)
7804     (gnus-set-mode-line 'summary)
7805     n))
7806
7807 (defun gnus-summary-mark-article-as-read (mark)
7808   "Mark the current article quickly as read with MARK."
7809   (let ((article (gnus-summary-article-number)))
7810     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7811     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7812     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7813     (push (cons article mark) gnus-newsgroup-reads)
7814     ;; Possibly remove from cache, if that is used.
7815     (when gnus-use-cache
7816       (gnus-cache-enter-remove-article article))
7817     ;; Allow the backend to change the mark.
7818     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7819     ;; Check for auto-expiry.
7820     (when (and gnus-newsgroup-auto-expire
7821                (memq mark gnus-auto-expirable-marks))
7822       (setq mark gnus-expirable-mark)
7823       ;; Let the backend know about the mark change.
7824       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7825       (push article gnus-newsgroup-expirable))
7826     ;; Set the mark in the buffer.
7827     (gnus-summary-update-mark mark 'unread)
7828     t))
7829
7830 (defun gnus-summary-mark-article-as-unread (mark)
7831   "Mark the current article quickly as unread with MARK."
7832   (let* ((article (gnus-summary-article-number))
7833          (old-mark (gnus-summary-article-mark article)))
7834     ;; Allow the backend to change the mark.
7835     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7836     (if (eq mark old-mark)
7837         t
7838       (if (<= article 0)
7839           (progn
7840             (gnus-error 1 "Can't mark negative article numbers")
7841             nil)
7842         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7843         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7844         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7845         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7846         (cond ((= mark gnus-ticked-mark)
7847                (push article gnus-newsgroup-marked))
7848               ((= mark gnus-dormant-mark)
7849                (push article gnus-newsgroup-dormant))
7850               (t
7851                (push article gnus-newsgroup-unreads)))
7852         (gnus-pull article gnus-newsgroup-reads)
7853
7854         ;; See whether the article is to be put in the cache.
7855         (and gnus-use-cache
7856              (vectorp (gnus-summary-article-header article))
7857              (save-excursion
7858                (gnus-cache-possibly-enter-article
7859                 gnus-newsgroup-name article
7860                 (gnus-summary-article-header article)
7861                 (= mark gnus-ticked-mark)
7862                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7863
7864         ;; Fix the mark.
7865         (gnus-summary-update-mark mark 'unread)
7866         t))))
7867
7868 (defun gnus-summary-mark-article (&optional article mark no-expire)
7869   "Mark ARTICLE with MARK.  MARK can be any character.
7870 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7871 `??' (dormant) and `?E' (expirable).
7872 If MARK is nil, then the default character `?r' is used.
7873 If ARTICLE is nil, then the article on the current line will be
7874 marked."
7875   ;; The mark might be a string.
7876   (when (stringp mark)
7877     (setq mark (aref mark 0)))
7878   ;; If no mark is given, then we check auto-expiring.
7879   (when (null mark)
7880     (setq mark gnus-del-mark))
7881   (when (and (not no-expire)
7882              gnus-newsgroup-auto-expire
7883              (memq mark gnus-auto-expirable-marks))
7884     (setq mark gnus-expirable-mark))
7885   (let ((article (or article (gnus-summary-article-number)))
7886         (old-mark (gnus-summary-article-mark article)))
7887     ;; Allow the backend to change the mark.
7888     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7889     (if (eq mark old-mark)
7890         t
7891       (unless article
7892         (error "No article on current line"))
7893       (if (not (if (or (= mark gnus-unread-mark)
7894                        (= mark gnus-ticked-mark)
7895                        (= mark gnus-dormant-mark))
7896                    (gnus-mark-article-as-unread article mark)
7897                  (gnus-mark-article-as-read article mark)))
7898           t
7899         ;; See whether the article is to be put in the cache.
7900         (and gnus-use-cache
7901              (not (= mark gnus-canceled-mark))
7902              (vectorp (gnus-summary-article-header article))
7903              (save-excursion
7904                (gnus-cache-possibly-enter-article
7905                 gnus-newsgroup-name article
7906                 (gnus-summary-article-header article)
7907                 (= mark gnus-ticked-mark)
7908                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7909
7910         (when (gnus-summary-goto-subject article nil t)
7911           (let ((buffer-read-only nil))
7912             (gnus-summary-show-thread)
7913             ;; Fix the mark.
7914             (gnus-summary-update-mark mark 'unread)
7915             t))))))
7916
7917 (defun gnus-summary-update-secondary-mark (article)
7918   "Update the secondary (read, process, cache) mark."
7919   (gnus-summary-update-mark
7920    (cond ((memq article gnus-newsgroup-processable)
7921           gnus-process-mark)
7922          ((memq article gnus-newsgroup-cached)
7923           gnus-cached-mark)
7924          ((memq article gnus-newsgroup-replied)
7925           gnus-replied-mark)
7926          ((memq article gnus-newsgroup-saved)
7927           gnus-saved-mark)
7928          (t gnus-unread-mark))
7929    'replied)
7930   (when (gnus-visual-p 'summary-highlight 'highlight)
7931     (gnus-run-hooks 'gnus-summary-update-hook))
7932   t)
7933
7934 (defun gnus-summary-update-mark (mark type)
7935   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7936         (buffer-read-only nil))
7937     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7938     (when forward
7939       (when (looking-at "\r")
7940         (incf forward))
7941       (when (<= (+ forward (point)) (point-max))
7942         ;; Go to the right position on the line.
7943         (goto-char (+ forward (point)))
7944         ;; Replace the old mark with the new mark.
7945         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7946         ;; Optionally update the marks by some user rule.
7947         (when (eq type 'unread)
7948           (gnus-data-set-mark
7949            (gnus-data-find (gnus-summary-article-number)) mark)
7950           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7951
7952 (defun gnus-mark-article-as-read (article &optional mark)
7953   "Enter ARTICLE in the pertinent lists and remove it from others."
7954   ;; Make the article expirable.
7955   (let ((mark (or mark gnus-del-mark)))
7956     (if (= mark gnus-expirable-mark)
7957         (push article gnus-newsgroup-expirable)
7958       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7959     ;; Remove from unread and marked lists.
7960     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7961     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7962     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7963     (push (cons article mark) gnus-newsgroup-reads)
7964     ;; Possibly remove from cache, if that is used.
7965     (when gnus-use-cache
7966       (gnus-cache-enter-remove-article article))
7967     t))
7968
7969 (defun gnus-mark-article-as-unread (article &optional mark)
7970   "Enter ARTICLE in the pertinent lists and remove it from others."
7971   (let ((mark (or mark gnus-ticked-mark)))
7972     (if (<= article 0)
7973         (progn
7974           (gnus-error 1 "Can't mark negative article numbers")
7975           nil)
7976       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7977             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7978             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7979             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7980
7981       ;; Unsuppress duplicates?
7982       (when gnus-suppress-duplicates
7983         (gnus-dup-unsuppress-article article))
7984
7985       (cond ((= mark gnus-ticked-mark)
7986              (push article gnus-newsgroup-marked))
7987             ((= mark gnus-dormant-mark)
7988              (push article gnus-newsgroup-dormant))
7989             (t
7990              (push article gnus-newsgroup-unreads)))
7991       (gnus-pull article gnus-newsgroup-reads)
7992       t)))
7993
7994 (defalias 'gnus-summary-mark-as-unread-forward
7995   'gnus-summary-tick-article-forward)
7996 (make-obsolete 'gnus-summary-mark-as-unread-forward
7997                'gnus-summary-tick-article-forward)
7998 (defun gnus-summary-tick-article-forward (n)
7999   "Tick N articles forwards.
8000 If N is negative, tick backwards instead.
8001 The difference between N and the number of articles ticked is returned."
8002   (interactive "p")
8003   (gnus-summary-mark-forward n gnus-ticked-mark))
8004
8005 (defalias 'gnus-summary-mark-as-unread-backward
8006   'gnus-summary-tick-article-backward)
8007 (make-obsolete 'gnus-summary-mark-as-unread-backward
8008                'gnus-summary-tick-article-backward)
8009 (defun gnus-summary-tick-article-backward (n)
8010   "Tick N articles backwards.
8011 The difference between N and the number of articles ticked is returned."
8012   (interactive "p")
8013   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8014
8015 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8016 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8017 (defun gnus-summary-tick-article (&optional article clear-mark)
8018   "Mark current article as unread.
8019 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8020 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8021   (interactive)
8022   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8023                                        gnus-ticked-mark)))
8024
8025 (defun gnus-summary-mark-as-read-forward (n)
8026   "Mark N articles as read forwards.
8027 If N is negative, mark backwards instead.
8028 The difference between N and the actual number of articles marked is
8029 returned."
8030   (interactive "p")
8031   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8032
8033 (defun gnus-summary-mark-as-read-backward (n)
8034   "Mark the N articles as read backwards.
8035 The difference between N and the actual number of articles marked is
8036 returned."
8037   (interactive "p")
8038   (gnus-summary-mark-forward
8039    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8040
8041 (defun gnus-summary-mark-as-read (&optional article mark)
8042   "Mark current article as read.
8043 ARTICLE specifies the article to be marked as read.
8044 MARK specifies a string to be inserted at the beginning of the line."
8045   (gnus-summary-mark-article article mark))
8046
8047 (defun gnus-summary-clear-mark-forward (n)
8048   "Clear marks from N articles forward.
8049 If N is negative, clear backward instead.
8050 The difference between N and the number of marks cleared is returned."
8051   (interactive "p")
8052   (gnus-summary-mark-forward n gnus-unread-mark))
8053
8054 (defun gnus-summary-clear-mark-backward (n)
8055   "Clear marks from N articles backward.
8056 The difference between N and the number of marks cleared is returned."
8057   (interactive "p")
8058   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8059
8060 (defun gnus-summary-mark-unread-as-read ()
8061   "Intended to be used by `gnus-summary-mark-article-hook'."
8062   (when (memq gnus-current-article gnus-newsgroup-unreads)
8063     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8064
8065 (defun gnus-summary-mark-read-and-unread-as-read ()
8066   "Intended to be used by `gnus-summary-mark-article-hook'."
8067   (let ((mark (gnus-summary-article-mark)))
8068     (when (or (gnus-unread-mark-p mark)
8069               (gnus-read-mark-p mark))
8070       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8071
8072 (defun gnus-summary-mark-region-as-read (point mark all)
8073   "Mark all unread articles between point and mark as read.
8074 If given a prefix, mark all articles between point and mark as read,
8075 even ticked and dormant ones."
8076   (interactive "r\nP")
8077   (save-excursion
8078     (let (article)
8079       (goto-char point)
8080       (beginning-of-line)
8081       (while (and
8082               (< (point) mark)
8083               (progn
8084                 (when (or all
8085                           (memq (setq article (gnus-summary-article-number))
8086                                 gnus-newsgroup-unreads))
8087                   (gnus-summary-mark-article article gnus-del-mark))
8088                 t)
8089               (gnus-summary-find-next))))))
8090
8091 (defun gnus-summary-mark-below (score mark)
8092   "Mark articles with score less than SCORE with MARK."
8093   (interactive "P\ncMark: ")
8094   (setq score (if score
8095                   (prefix-numeric-value score)
8096                 (or gnus-summary-default-score 0)))
8097   (save-excursion
8098     (set-buffer gnus-summary-buffer)
8099     (goto-char (point-min))
8100     (while
8101         (progn
8102           (and (< (gnus-summary-article-score) score)
8103                (gnus-summary-mark-article nil mark))
8104           (gnus-summary-find-next)))))
8105
8106 (defun gnus-summary-kill-below (&optional score)
8107   "Mark articles with score below SCORE as read."
8108   (interactive "P")
8109   (gnus-summary-mark-below score gnus-killed-mark))
8110
8111 (defun gnus-summary-clear-above (&optional score)
8112   "Clear all marks from articles with score above SCORE."
8113   (interactive "P")
8114   (gnus-summary-mark-above score gnus-unread-mark))
8115
8116 (defun gnus-summary-tick-above (&optional score)
8117   "Tick all articles with score above SCORE."
8118   (interactive "P")
8119   (gnus-summary-mark-above score gnus-ticked-mark))
8120
8121 (defun gnus-summary-mark-above (score mark)
8122   "Mark articles with score over SCORE with MARK."
8123   (interactive "P\ncMark: ")
8124   (setq score (if score
8125                   (prefix-numeric-value score)
8126                 (or gnus-summary-default-score 0)))
8127   (save-excursion
8128     (set-buffer gnus-summary-buffer)
8129     (goto-char (point-min))
8130     (while (and (progn
8131                   (when (> (gnus-summary-article-score) score)
8132                     (gnus-summary-mark-article nil mark))
8133                   t)
8134                 (gnus-summary-find-next)))))
8135
8136 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8137 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8138 (defun gnus-summary-limit-include-expunged (&optional no-error)
8139   "Display all the hidden articles that were expunged for low scores."
8140   (interactive)
8141   (let ((buffer-read-only nil))
8142     (let ((scored gnus-newsgroup-scored)
8143           headers h)
8144       (while scored
8145         (unless (gnus-summary-goto-subject (caar scored))
8146           (and (setq h (gnus-summary-article-header (caar scored)))
8147                (< (cdar scored) gnus-summary-expunge-below)
8148                (push h headers)))
8149         (setq scored (cdr scored)))
8150       (if (not headers)
8151           (when (not no-error)
8152             (error "No expunged articles hidden"))
8153         (goto-char (point-min))
8154         (gnus-summary-prepare-unthreaded (nreverse headers))
8155         (goto-char (point-min))
8156         (gnus-summary-position-point)
8157         t))))
8158
8159 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8160   "Mark all unread articles in this newsgroup as read.
8161 If prefix argument ALL is non-nil, ticked and dormant articles will
8162 also be marked as read.
8163 If QUIETLY is non-nil, no questions will be asked.
8164 If TO-HERE is non-nil, it should be a point in the buffer.  All
8165 articles before this point will be marked as read.
8166 Note that this function will only catch up the unread article
8167 in the current summary buffer limitation.
8168 The number of articles marked as read is returned."
8169   (interactive "P")
8170   (prog1
8171       (save-excursion
8172         (when (or quietly
8173                   (not gnus-interactive-catchup) ;Without confirmation?
8174                   gnus-expert-user
8175                   (gnus-y-or-n-p
8176                    (if all
8177                        "Mark absolutely all articles as read? "
8178                      "Mark all unread articles as read? ")))
8179           (if (and not-mark
8180                    (not gnus-newsgroup-adaptive)
8181                    (not gnus-newsgroup-auto-expire)
8182                    (not gnus-suppress-duplicates)
8183                    (or (not gnus-use-cache)
8184                        (eq gnus-use-cache 'passive)))
8185               (progn
8186                 (when all
8187                   (setq gnus-newsgroup-marked nil
8188                         gnus-newsgroup-dormant nil))
8189                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8190             ;; We actually mark all articles as canceled, which we
8191             ;; have to do when using auto-expiry or adaptive scoring.
8192             (gnus-summary-show-all-threads)
8193             (when (gnus-summary-first-subject (not all) t)
8194               (while (and
8195                       (if to-here (< (point) to-here) t)
8196                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8197                       (gnus-summary-find-next (not all) nil nil t))))
8198             (gnus-set-mode-line 'summary))
8199           t))
8200     (gnus-summary-position-point)))
8201
8202 (defun gnus-summary-catchup-to-here (&optional all)
8203   "Mark all unticked articles before the current one as read.
8204 If ALL is non-nil, also mark ticked and dormant articles as read."
8205   (interactive "P")
8206   (save-excursion
8207     (gnus-save-hidden-threads
8208       (let ((beg (point)))
8209         ;; We check that there are unread articles.
8210         (when (or all (gnus-summary-find-prev))
8211           (gnus-summary-catchup all t beg)))))
8212   (gnus-summary-position-point))
8213
8214 (defun gnus-summary-catchup-all (&optional quietly)
8215   "Mark all articles in this newsgroup as read."
8216   (interactive "P")
8217   (gnus-summary-catchup t quietly))
8218
8219 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8220   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8221 If prefix argument ALL is non-nil, all articles are marked as read."
8222   (interactive "P")
8223   (when (gnus-summary-catchup all quietly nil 'fast)
8224     ;; Select next newsgroup or exit.
8225     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8226              (eq gnus-auto-select-next 'quietly))
8227         (gnus-summary-next-group nil)
8228       (gnus-summary-exit))))
8229
8230 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8231   "Mark all articles in this newsgroup as read, and then exit."
8232   (interactive "P")
8233   (gnus-summary-catchup-and-exit t quietly))
8234
8235 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8236 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8237   "Mark all articles in this group as read and select the next group.
8238 If given a prefix, mark all articles, unread as well as ticked, as
8239 read."
8240   (interactive "P")
8241   (save-excursion
8242     (gnus-summary-catchup all))
8243   (gnus-summary-next-article t nil nil t))
8244
8245 ;; Thread-based commands.
8246
8247 (defun gnus-summary-articles-in-thread (&optional article)
8248   "Return a list of all articles in the current thread.
8249 If ARTICLE is non-nil, return all articles in the thread that starts
8250 with that article."
8251   (let* ((article (or article (gnus-summary-article-number)))
8252          (data (gnus-data-find-list article))
8253          (top-level (gnus-data-level (car data)))
8254          (top-subject
8255           (cond ((null gnus-thread-operation-ignore-subject)
8256                  (gnus-simplify-subject-re
8257                   (mail-header-subject (gnus-data-header (car data)))))
8258                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8259                  (gnus-simplify-subject-fuzzy
8260                   (mail-header-subject (gnus-data-header (car data)))))
8261                 (t nil)))
8262          (end-point (save-excursion
8263                       (if (gnus-summary-go-to-next-thread)
8264                           (point) (point-max))))
8265          articles)
8266     (while (and data
8267                 (< (gnus-data-pos (car data)) end-point))
8268       (when (or (not top-subject)
8269                 (string= top-subject
8270                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8271                              (gnus-simplify-subject-fuzzy
8272                               (mail-header-subject
8273                                (gnus-data-header (car data))))
8274                            (gnus-simplify-subject-re
8275                             (mail-header-subject
8276                              (gnus-data-header (car data)))))))
8277         (push (gnus-data-number (car data)) articles))
8278       (unless (and (setq data (cdr data))
8279                    (> (gnus-data-level (car data)) top-level))
8280         (setq data nil)))
8281     ;; Return the list of articles.
8282     (nreverse articles)))
8283
8284 (defun gnus-summary-rethread-current ()
8285   "Rethread the thread the current article is part of."
8286   (interactive)
8287   (let* ((gnus-show-threads t)
8288          (article (gnus-summary-article-number))
8289          (id (mail-header-id (gnus-summary-article-header)))
8290          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8291     (unless id
8292       (error "No article on the current line"))
8293     (gnus-rebuild-thread id)
8294     (gnus-summary-goto-subject article)))
8295
8296 (defun gnus-summary-reparent-thread ()
8297   "Make the current article child of the marked (or previous) article.
8298
8299 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8300 is non-nil or the Subject: of both articles are the same."
8301   (interactive)
8302   (unless (not (gnus-group-read-only-p))
8303     (error "The current newsgroup does not support article editing"))
8304   (unless (<= (length gnus-newsgroup-processable) 1)
8305     (error "No more than one article may be marked"))
8306   (save-window-excursion
8307     (let ((gnus-article-buffer " *reparent*")
8308           (current-article (gnus-summary-article-number))
8309           ;; First grab the marked article, otherwise one line up.
8310           (parent-article (if (not (null gnus-newsgroup-processable))
8311                               (car gnus-newsgroup-processable)
8312                             (save-excursion
8313                               (if (eq (forward-line -1) 0)
8314                                   (gnus-summary-article-number)
8315                                 (error "Beginning of summary buffer"))))))
8316       (unless (not (eq current-article parent-article))
8317         (error "An article may not be self-referential"))
8318       (let ((message-id (mail-header-id
8319                          (gnus-summary-article-header parent-article))))
8320         (unless (and message-id (not (equal message-id "")))
8321           (error "No message-id in desired parent"))
8322         (gnus-with-article current-article
8323           (goto-char (point-min))
8324           (if (re-search-forward "^References: " nil t)
8325               (progn
8326                 (re-search-forward "^[^ \t]" nil t)
8327                 (forward-line -1)
8328                 (end-of-line)
8329                 (insert " " message-id))
8330             (insert "References: " message-id "\n")))
8331         (set-buffer gnus-summary-buffer)
8332         (gnus-summary-unmark-all-processable)
8333         (gnus-summary-update-article current-article)
8334         (gnus-summary-rethread-current)
8335         (gnus-message 3 "Article %d is now the child of article %d"
8336                       current-article parent-article)))))
8337
8338 (defun gnus-summary-toggle-threads (&optional arg)
8339   "Toggle showing conversation threads.
8340 If ARG is positive number, turn showing conversation threads on."
8341   (interactive "P")
8342   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8343     (setq gnus-show-threads
8344           (if (null arg) (not gnus-show-threads)
8345             (> (prefix-numeric-value arg) 0)))
8346     (gnus-summary-prepare)
8347     (gnus-summary-goto-subject current)
8348     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8349     (gnus-summary-position-point)))
8350
8351 (defun gnus-summary-show-all-threads ()
8352   "Show all threads."
8353   (interactive)
8354   (save-excursion
8355     (let ((buffer-read-only nil))
8356       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8357   (gnus-summary-position-point))
8358
8359 (defun gnus-summary-show-thread ()
8360   "Show thread subtrees.
8361 Returns nil if no thread was there to be shown."
8362   (interactive)
8363   (let ((buffer-read-only nil)
8364         (orig (point))
8365         ;; first goto end then to beg, to have point at beg after let
8366         (end (progn (end-of-line) (point)))
8367         (beg (progn (beginning-of-line) (point))))
8368     (prog1
8369         ;; Any hidden lines here?
8370         (search-forward "\r" end t)
8371       (subst-char-in-region beg end ?\^M ?\n t)
8372       (goto-char orig)
8373       (gnus-summary-position-point))))
8374
8375 (defun gnus-summary-hide-all-threads ()
8376   "Hide all thread subtrees."
8377   (interactive)
8378   (save-excursion
8379     (goto-char (point-min))
8380     (gnus-summary-hide-thread)
8381     (while (zerop (gnus-summary-next-thread 1 t))
8382       (gnus-summary-hide-thread)))
8383   (gnus-summary-position-point))
8384
8385 (defun gnus-summary-hide-thread ()
8386   "Hide thread subtrees.
8387 Returns nil if no threads were there to be hidden."
8388   (interactive)
8389   (let ((buffer-read-only nil)
8390         (start (point))
8391         (article (gnus-summary-article-number)))
8392     (goto-char start)
8393     ;; Go forward until either the buffer ends or the subthread
8394     ;; ends.
8395     (when (and (not (eobp))
8396                (or (zerop (gnus-summary-next-thread 1 t))
8397                    (goto-char (point-max))))
8398       (prog1
8399           (if (and (> (point) start)
8400                    (search-backward "\n" start t))
8401               (progn
8402                 (subst-char-in-region start (point) ?\n ?\^M)
8403                 (gnus-summary-goto-subject article))
8404             (goto-char start)
8405             nil)
8406         ;;(gnus-summary-position-point)
8407         ))))
8408
8409 (defun gnus-summary-go-to-next-thread (&optional previous)
8410   "Go to the same level (or less) next thread.
8411 If PREVIOUS is non-nil, go to previous thread instead.
8412 Return the article number moved to, or nil if moving was impossible."
8413   (let ((level (gnus-summary-thread-level))
8414         (way (if previous -1 1))
8415         (beg (point)))
8416     (forward-line way)
8417     (while (and (not (eobp))
8418                 (< level (gnus-summary-thread-level)))
8419       (forward-line way))
8420     (if (eobp)
8421         (progn
8422           (goto-char beg)
8423           nil)
8424       (setq beg (point))
8425       (prog1
8426           (gnus-summary-article-number)
8427         (goto-char beg)))))
8428
8429 (defun gnus-summary-next-thread (n &optional silent)
8430   "Go to the same level next N'th thread.
8431 If N is negative, search backward instead.
8432 Returns the difference between N and the number of skips actually
8433 done.
8434
8435 If SILENT, don't output messages."
8436   (interactive "p")
8437   (let ((backward (< n 0))
8438         (n (abs n)))
8439     (while (and (> n 0)
8440                 (gnus-summary-go-to-next-thread backward))
8441       (decf n))
8442     (unless silent
8443       (gnus-summary-position-point))
8444     (when (and (not silent) (/= 0 n))
8445       (gnus-message 7 "No more threads"))
8446     n))
8447
8448 (defun gnus-summary-prev-thread (n)
8449   "Go to the same level previous N'th thread.
8450 Returns the difference between N and the number of skips actually
8451 done."
8452   (interactive "p")
8453   (gnus-summary-next-thread (- n)))
8454
8455 (defun gnus-summary-go-down-thread ()
8456   "Go down one level in the current thread."
8457   (let ((children (gnus-summary-article-children)))
8458     (when children
8459       (gnus-summary-goto-subject (car children)))))
8460
8461 (defun gnus-summary-go-up-thread ()
8462   "Go up one level in the current thread."
8463   (let ((parent (gnus-summary-article-parent)))
8464     (when parent
8465       (gnus-summary-goto-subject parent))))
8466
8467 (defun gnus-summary-down-thread (n)
8468   "Go down thread N steps.
8469 If N is negative, go up instead.
8470 Returns the difference between N and how many steps down that were
8471 taken."
8472   (interactive "p")
8473   (let ((up (< n 0))
8474         (n (abs n)))
8475     (while (and (> n 0)
8476                 (if up (gnus-summary-go-up-thread)
8477                   (gnus-summary-go-down-thread)))
8478       (setq n (1- n)))
8479     (gnus-summary-position-point)
8480     (when (/= 0 n)
8481       (gnus-message 7 "Can't go further"))
8482     n))
8483
8484 (defun gnus-summary-up-thread (n)
8485   "Go up thread N steps.
8486 If N is negative, go up instead.
8487 Returns the difference between N and how many steps down that were
8488 taken."
8489   (interactive "p")
8490   (gnus-summary-down-thread (- n)))
8491
8492 (defun gnus-summary-top-thread ()
8493   "Go to the top of the thread."
8494   (interactive)
8495   (while (gnus-summary-go-up-thread))
8496   (gnus-summary-article-number))
8497
8498 (defun gnus-summary-kill-thread (&optional unmark)
8499   "Mark articles under current thread as read.
8500 If the prefix argument is positive, remove any kinds of marks.
8501 If the prefix argument is negative, tick articles instead."
8502   (interactive "P")
8503   (when unmark
8504     (setq unmark (prefix-numeric-value unmark)))
8505   (let ((articles (gnus-summary-articles-in-thread)))
8506     (save-excursion
8507       ;; Expand the thread.
8508       (gnus-summary-show-thread)
8509       ;; Mark all the articles.
8510       (while articles
8511         (gnus-summary-goto-subject (car articles))
8512         (cond ((null unmark)
8513                (gnus-summary-mark-article-as-read gnus-killed-mark))
8514               ((> unmark 0)
8515                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8516               (t
8517                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8518         (setq articles (cdr articles))))
8519     ;; Hide killed subtrees.
8520     (and (null unmark)
8521          gnus-thread-hide-killed
8522          (gnus-summary-hide-thread))
8523     ;; If marked as read, go to next unread subject.
8524     (when (null unmark)
8525       ;; Go to next unread subject.
8526       (gnus-summary-next-subject 1 t)))
8527   (gnus-set-mode-line 'summary))
8528
8529 ;; Summary sorting commands
8530
8531 (defun gnus-summary-sort-by-number (&optional reverse)
8532   "Sort the summary buffer by article number.
8533 Argument REVERSE means reverse order."
8534   (interactive "P")
8535   (gnus-summary-sort 'number reverse))
8536
8537 (defun gnus-summary-sort-by-author (&optional reverse)
8538   "Sort the summary buffer by author name alphabetically.
8539 If case-fold-search is non-nil, case of letters is ignored.
8540 Argument REVERSE means reverse order."
8541   (interactive "P")
8542   (gnus-summary-sort 'author reverse))
8543
8544 (defun gnus-summary-sort-by-subject (&optional reverse)
8545   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8546 If case-fold-search is non-nil, case of letters is ignored.
8547 Argument REVERSE means reverse order."
8548   (interactive "P")
8549   (gnus-summary-sort 'subject reverse))
8550
8551 (defun gnus-summary-sort-by-date (&optional reverse)
8552   "Sort the summary buffer by date.
8553 Argument REVERSE means reverse order."
8554   (interactive "P")
8555   (gnus-summary-sort 'date reverse))
8556
8557 (defun gnus-summary-sort-by-score (&optional reverse)
8558   "Sort the summary buffer by score.
8559 Argument REVERSE means reverse order."
8560   (interactive "P")
8561   (gnus-summary-sort 'score reverse))
8562
8563 (defun gnus-summary-sort-by-lines (&optional reverse)
8564   "Sort the summary buffer by article length.
8565 Argument REVERSE means reverse order."
8566   (interactive "P")
8567   (gnus-summary-sort 'lines reverse))
8568
8569 (defun gnus-summary-sort (predicate reverse)
8570   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8571   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8572          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8573          (gnus-thread-sort-functions
8574           (if (not reverse)
8575               thread
8576             `(lambda (t1 t2)
8577                (,thread t2 t1))))
8578          (gnus-article-sort-functions
8579           (if (not reverse)
8580               article
8581             `(lambda (t1 t2)
8582                (,article t2 t1))))
8583          (buffer-read-only)
8584          (gnus-summary-prepare-hook nil))
8585     ;; We do the sorting by regenerating the threads.
8586     (gnus-summary-prepare)
8587     ;; Hide subthreads if needed.
8588     (when (and gnus-show-threads gnus-thread-hide-subtree)
8589       (gnus-summary-hide-all-threads))))
8590
8591 ;; Summary saving commands.
8592
8593 (defun gnus-summary-save-article (&optional n not-saved)
8594   "Save the current article using the default saver function.
8595 If N is a positive number, save the N next articles.
8596 If N is a negative number, save the N previous articles.
8597 If N is nil and any articles have been marked with the process mark,
8598 save those articles instead.
8599 The variable `gnus-default-article-saver' specifies the saver function."
8600   (interactive "P")
8601   (let* ((articles (gnus-summary-work-articles n))
8602          (save-buffer (save-excursion
8603                         (nnheader-set-temp-buffer " *Gnus Save*")))
8604          (num (length articles))
8605          header file)
8606     (dolist (article articles)
8607       (setq header (gnus-summary-article-header article))
8608       (if (not (vectorp header))
8609           ;; This is a pseudo-article.
8610           (if (assq 'name header)
8611               (gnus-copy-file (cdr (assq 'name header)))
8612             (gnus-message 1 "Article %d is unsaveable" article))
8613         ;; This is a real article.
8614         (save-window-excursion
8615           (gnus-summary-select-article t nil nil article))
8616         (save-excursion
8617           (set-buffer save-buffer)
8618           (erase-buffer)
8619           (insert-buffer-substring gnus-original-article-buffer))
8620         (setq file (gnus-article-save save-buffer file num))
8621         (gnus-summary-remove-process-mark article)
8622         (unless not-saved
8623           (gnus-summary-set-saved-mark article))))
8624     (gnus-kill-buffer save-buffer)
8625     (gnus-summary-position-point)
8626     (gnus-set-mode-line 'summary)
8627     n))
8628
8629 (defun gnus-summary-pipe-output (&optional arg)
8630   "Pipe the current article to a subprocess.
8631 If N is a positive number, pipe the N next articles.
8632 If N is a negative number, pipe the N previous articles.
8633 If N is nil and any articles have been marked with the process mark,
8634 pipe those articles instead."
8635   (interactive "P")
8636   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8637     (gnus-summary-save-article arg t))
8638   (gnus-configure-windows 'pipe))
8639
8640 (defun gnus-summary-save-article-mail (&optional arg)
8641   "Append the current article to an mail file.
8642 If N is a positive number, save the N next articles.
8643 If N is a negative number, save the N previous articles.
8644 If N is nil and any articles have been marked with the process mark,
8645 save those articles instead."
8646   (interactive "P")
8647   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8648     (gnus-summary-save-article arg)))
8649
8650 (defun gnus-summary-save-article-rmail (&optional arg)
8651   "Append the current article to an rmail file.
8652 If N is a positive number, save the N next articles.
8653 If N is a negative number, save the N previous articles.
8654 If N is nil and any articles have been marked with the process mark,
8655 save those articles instead."
8656   (interactive "P")
8657   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8658     (gnus-summary-save-article arg)))
8659
8660 (defun gnus-summary-save-article-file (&optional arg)
8661   "Append the current article to a file.
8662 If N is a positive number, save the N next articles.
8663 If N is a negative number, save the N previous articles.
8664 If N is nil and any articles have been marked with the process mark,
8665 save those articles instead."
8666   (interactive "P")
8667   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8668     (gnus-summary-save-article arg)))
8669
8670 (defun gnus-summary-write-article-file (&optional arg)
8671   "Write the current article to a file, deleting the previous file.
8672 If N is a positive number, save the N next articles.
8673 If N is a negative number, save the N previous articles.
8674 If N is nil and any articles have been marked with the process mark,
8675 save those articles instead."
8676   (interactive "P")
8677   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8678     (gnus-summary-save-article arg)))
8679
8680 (defun gnus-summary-save-article-body-file (&optional arg)
8681   "Append the current article body to a file.
8682 If N is a positive number, save the N next articles.
8683 If N is a negative number, save the N previous articles.
8684 If N is nil and any articles have been marked with the process mark,
8685 save those articles instead."
8686   (interactive "P")
8687   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8688     (gnus-summary-save-article arg)))
8689
8690 (defun gnus-summary-pipe-message (program)
8691   "Pipe the current article through PROGRAM."
8692   (interactive "sProgram: ")
8693   (gnus-summary-select-article)
8694   (let ((mail-header-separator ""))
8695     (gnus-eval-in-buffer-window gnus-article-buffer
8696       (save-restriction
8697         (widen)
8698         (let ((start (window-start))
8699               buffer-read-only)
8700           (message-pipe-buffer-body program)
8701           (set-window-start (get-buffer-window (current-buffer)) start))))))
8702
8703 (defun gnus-get-split-value (methods)
8704   "Return a value based on the split METHODS."
8705   (let (split-name method result match)
8706     (when methods
8707       (save-excursion
8708         (set-buffer gnus-original-article-buffer)
8709         (save-restriction
8710           (nnheader-narrow-to-headers)
8711           (while methods
8712             (goto-char (point-min))
8713             (setq method (pop methods))
8714             (setq match (car method))
8715             (when (cond
8716                    ((stringp match)
8717                     ;; Regular expression.
8718                     (ignore-errors
8719                       (re-search-forward match nil t)))
8720                    ((gnus-functionp match)
8721                     ;; Function.
8722                     (save-restriction
8723                       (widen)
8724                       (setq result (funcall match gnus-newsgroup-name))))
8725                    ((consp match)
8726                     ;; Form.
8727                     (save-restriction
8728                       (widen)
8729                       (setq result (eval match)))))
8730               (setq split-name (append (cdr method) split-name))
8731               (cond ((stringp result)
8732                      (push (expand-file-name
8733                             result gnus-article-save-directory)
8734                            split-name))
8735                     ((consp result)
8736                      (setq split-name (append result split-name)))))))))
8737     split-name))
8738
8739 (defun gnus-valid-move-group-p (group)
8740   (and (boundp group)
8741        (symbol-name group)
8742        (symbol-value group)
8743        (memq 'respool
8744              (assoc (symbol-name
8745                      (car (gnus-find-method-for-group
8746                            (symbol-name group))))
8747                     gnus-valid-select-methods))))
8748
8749 (defun gnus-read-move-group-name (prompt default articles prefix)
8750   "Read a group name."
8751   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8752          (minibuffer-confirm-incomplete nil) ; XEmacs
8753          (prom
8754           (format "%s %s to:"
8755                   prompt
8756                   (if (> (length articles) 1)
8757                       (format "these %d articles" (length articles))
8758                     "this article")))
8759          (to-newsgroup
8760           (cond
8761            ((null split-name)
8762             (gnus-completing-read default prom
8763                                   gnus-active-hashtb
8764                                   'gnus-valid-move-group-p
8765                                   nil prefix
8766                                   'gnus-group-history))
8767            ((= 1 (length split-name))
8768             (gnus-completing-read (car split-name) prom
8769                                   gnus-active-hashtb
8770                                   'gnus-valid-move-group-p
8771                                   nil nil
8772                                   'gnus-group-history))
8773            (t
8774             (gnus-completing-read nil prom
8775                                   (mapcar (lambda (el) (list el))
8776                                           (nreverse split-name))
8777                                   nil nil nil
8778                                   'gnus-group-history)))))
8779     (when to-newsgroup
8780       (if (or (string= to-newsgroup "")
8781               (string= to-newsgroup prefix))
8782           (setq to-newsgroup default))
8783       (unless to-newsgroup
8784         (error "No group name entered"))
8785       (or (gnus-active to-newsgroup)
8786           (gnus-activate-group to-newsgroup)
8787           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8788                                      to-newsgroup))
8789               (or (and (gnus-request-create-group
8790                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8791                        (gnus-activate-group to-newsgroup nil nil
8792                                             (gnus-group-name-to-method
8793                                              to-newsgroup)))
8794                   (error "Couldn't create group %s" to-newsgroup)))
8795           (error "No such group: %s" to-newsgroup)))
8796     to-newsgroup))
8797
8798 ;; Summary extract commands
8799
8800 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8801   (let ((buffer-read-only nil)
8802         (article (gnus-summary-article-number))
8803         after-article b e)
8804     (unless (gnus-summary-goto-subject article)
8805       (error "No such article: %d" article))
8806     (gnus-summary-position-point)
8807     ;; If all commands are to be bunched up on one line, we collect
8808     ;; them here.
8809     (unless gnus-view-pseudos-separately
8810       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8811             files action)
8812         (while ps
8813           (setq action (cdr (assq 'action (car ps))))
8814           (setq files (list (cdr (assq 'name (car ps)))))
8815           (while (and ps (cdr ps)
8816                       (string= (or action "1")
8817                                (or (cdr (assq 'action (cadr ps))) "2")))
8818             (push (cdr (assq 'name (cadr ps))) files)
8819             (setcdr ps (cddr ps)))
8820           (when files
8821             (when (not (string-match "%s" action))
8822               (push " " files))
8823             (push " " files)
8824             (when (assq 'execute (car ps))
8825               (setcdr (assq 'execute (car ps))
8826                       (funcall (if (string-match "%s" action)
8827                                    'format 'concat)
8828                                action
8829                                (mapconcat
8830                                 (lambda (f)
8831                                   (if (equal f " ")
8832                                       f
8833                                     (gnus-quote-arg-for-sh-or-csh f)))
8834                                 files " ")))))
8835           (setq ps (cdr ps)))))
8836     (if (and gnus-view-pseudos (not not-view))
8837         (while pslist
8838           (when (assq 'execute (car pslist))
8839             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8840                                   (eq gnus-view-pseudos 'not-confirm)))
8841           (setq pslist (cdr pslist)))
8842       (save-excursion
8843         (while pslist
8844           (setq after-article (or (cdr (assq 'article (car pslist)))
8845                                   (gnus-summary-article-number)))
8846           (gnus-summary-goto-subject after-article)
8847           (forward-line 1)
8848           (setq b (point))
8849           (insert "    " (file-name-nondirectory
8850                           (cdr (assq 'name (car pslist))))
8851                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8852           (setq e (point))
8853           (forward-line -1)             ; back to `b'
8854           (gnus-add-text-properties
8855            b (1- e) (list 'gnus-number gnus-reffed-article-number
8856                           gnus-mouse-face-prop gnus-mouse-face))
8857           (gnus-data-enter
8858            after-article gnus-reffed-article-number
8859            gnus-unread-mark b (car pslist) 0 (- e b))
8860           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8861           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8862           (setq pslist (cdr pslist)))))))
8863
8864 (defun gnus-pseudos< (p1 p2)
8865   (let ((c1 (cdr (assq 'action p1)))
8866         (c2 (cdr (assq 'action p2))))
8867     (and c1 c2 (string< c1 c2))))
8868
8869 (defun gnus-request-pseudo-article (props)
8870   (cond ((assq 'execute props)
8871          (gnus-execute-command (cdr (assq 'execute props)))))
8872   (let ((gnus-current-article (gnus-summary-article-number)))
8873     (gnus-run-hooks 'gnus-mark-article-hook)))
8874
8875 (defun gnus-execute-command (command &optional automatic)
8876   (save-excursion
8877     (gnus-article-setup-buffer)
8878     (set-buffer gnus-article-buffer)
8879     (setq buffer-read-only nil)
8880     (let ((command (if automatic command
8881                      (read-string "Command: " (cons command 0)))))
8882       (erase-buffer)
8883       (insert "$ " command "\n\n")
8884       (if gnus-view-pseudo-asynchronously
8885           (start-process "gnus-execute" (current-buffer) shell-file-name
8886                          shell-command-switch command)
8887         (call-process shell-file-name nil t nil
8888                       shell-command-switch command)))))
8889
8890 ;; Summary kill commands.
8891
8892 (defun gnus-summary-edit-global-kill (article)
8893   "Edit the \"global\" kill file."
8894   (interactive (list (gnus-summary-article-number)))
8895   (gnus-group-edit-global-kill article))
8896
8897 (defun gnus-summary-edit-local-kill ()
8898   "Edit a local kill file applied to the current newsgroup."
8899   (interactive)
8900   (setq gnus-current-headers (gnus-summary-article-header))
8901   (gnus-group-edit-local-kill
8902    (gnus-summary-article-number) gnus-newsgroup-name))
8903
8904 ;;; Header reading.
8905
8906 (defun gnus-read-header (id &optional header)
8907   "Read the headers of article ID and enter them into the Gnus system."
8908   (let ((group gnus-newsgroup-name)
8909         (gnus-override-method
8910          (and (gnus-news-group-p gnus-newsgroup-name)
8911               gnus-refer-article-method))
8912         where)
8913     ;; First we check to see whether the header in question is already
8914     ;; fetched.
8915     (if (stringp id)
8916         ;; This is a Message-ID.
8917         (setq header (or header (gnus-id-to-header id)))
8918       ;; This is an article number.
8919       (setq header (or header (gnus-summary-article-header id))))
8920     (if (and header
8921              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8922         ;; We have found the header.
8923         header
8924       ;; If this is a sparse article, we have to nix out its
8925       ;; previous entry in the thread hashtb.
8926       (when (and header
8927                  (gnus-summary-article-sparse-p (mail-header-number header)))
8928         (let* ((parent (gnus-parent-id (mail-header-references header)))
8929                (thread (and parent (gnus-id-to-thread parent))))
8930           (when thread
8931             (delq (assq header thread) thread))))
8932       ;; We have to really fetch the header to this article.
8933       (save-excursion
8934         (set-buffer nntp-server-buffer)
8935         (when (setq where (gnus-request-head id group))
8936           (nnheader-fold-continuation-lines)
8937           (goto-char (point-max))
8938           (insert ".\n")
8939           (goto-char (point-min))
8940           (insert "211 ")
8941           (princ (cond
8942                   ((numberp id) id)
8943                   ((cdr where) (cdr where))
8944                   (header (mail-header-number header))
8945                   (t gnus-reffed-article-number))
8946                  (current-buffer))
8947           (insert " Article retrieved.\n"))
8948         (if (or (not where)
8949                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8950             ()                          ; Malformed head.
8951           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8952             (when (and (stringp id)
8953                        (not (string= (gnus-group-real-name group)
8954                                      (car where))))
8955               ;; If we fetched by Message-ID and the article came
8956               ;; from a different group, we fudge some bogus article
8957               ;; numbers for this article.
8958               (mail-header-set-number header gnus-reffed-article-number))
8959             (save-excursion
8960               (set-buffer gnus-summary-buffer)
8961               (decf gnus-reffed-article-number)
8962               (gnus-remove-header (mail-header-number header))
8963               (push header gnus-newsgroup-headers)
8964               (setq gnus-current-headers header)
8965               (push (mail-header-number header) gnus-newsgroup-limit)))
8966           header)))))
8967
8968 (defun gnus-remove-header (number)
8969   "Remove header NUMBER from `gnus-newsgroup-headers'."
8970   (if (and gnus-newsgroup-headers
8971            (= number (mail-header-number (car gnus-newsgroup-headers))))
8972       (pop gnus-newsgroup-headers)
8973     (let ((headers gnus-newsgroup-headers))
8974       (while (and (cdr headers)
8975                   (not (= number (mail-header-number (cadr headers)))))
8976         (pop headers))
8977       (when (cdr headers)
8978         (setcdr headers (cddr headers))))))
8979
8980 ;;;
8981 ;;; summary highlights
8982 ;;;
8983
8984 (defun gnus-highlight-selected-summary ()
8985   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8986   ;; Highlight selected article in summary buffer
8987   (when gnus-summary-selected-face
8988     (save-excursion
8989       (let* ((beg (progn (beginning-of-line) (point)))
8990              (end (progn (end-of-line) (point)))
8991              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8992              (from (if (get-text-property beg gnus-mouse-face-prop)
8993                        beg
8994                      (or (next-single-property-change
8995                           beg gnus-mouse-face-prop nil end)
8996                          beg)))
8997              (to
8998               (if (= from end)
8999                   (- from 2)
9000                 (or (next-single-property-change
9001                      from gnus-mouse-face-prop nil end)
9002                     end))))
9003         ;; If no mouse-face prop on line we will have to = from = end,
9004         ;; so we highlight the entire line instead.
9005         (when (= (+ to 2) from)
9006           (setq from beg)
9007           (setq to end))
9008         (if gnus-newsgroup-selected-overlay
9009             ;; Move old overlay.
9010             (gnus-move-overlay
9011              gnus-newsgroup-selected-overlay from to (current-buffer))
9012           ;; Create new overlay.
9013           (gnus-overlay-put
9014            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9015            'face gnus-summary-selected-face))))))
9016
9017 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9018 (defun gnus-summary-highlight-line ()
9019   "Highlight current line according to `gnus-summary-highlight'."
9020   (let* ((list gnus-summary-highlight)
9021          (p (point))
9022          (end (progn (end-of-line) (point)))
9023          ;; now find out where the line starts and leave point there.
9024          (beg (progn (beginning-of-line) (point)))
9025          (article (gnus-summary-article-number))
9026          (score (or (cdr (assq (or article gnus-current-article)
9027                                gnus-newsgroup-scored))
9028                     gnus-summary-default-score 0))
9029          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9030          (inhibit-read-only t))
9031     ;; Eval the cars of the lists until we find a match.
9032     (let ((default gnus-summary-default-score))
9033       (while (and list
9034                   (not (eval (caar list))))
9035         (setq list (cdr list))))
9036     (let ((face (cdar list)))
9037       (unless (eq face (get-text-property beg 'face))
9038         (gnus-put-text-property-excluding-characters-with-faces
9039          beg end 'face
9040          (setq face (if (boundp face) (symbol-value face) face)))
9041         (when gnus-summary-highlight-line-function
9042           (funcall gnus-summary-highlight-line-function article face))))
9043     (goto-char p)))
9044
9045 (defun gnus-update-read-articles (group unread &optional compute)
9046   "Update the list of read articles in GROUP."
9047   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9048          (entry (gnus-gethash group gnus-newsrc-hashtb))
9049          (info (nth 2 entry))
9050          (prev 1)
9051          (unread (sort (copy-sequence unread) '<))
9052          read)
9053     (if (or (not info) (not active))
9054         ;; There is no info on this group if it was, in fact,
9055         ;; killed.  Gnus stores no information on killed groups, so
9056         ;; there's nothing to be done.
9057         ;; One could store the information somewhere temporarily,
9058         ;; perhaps...  Hmmm...
9059         ()
9060       ;; Remove any negative articles numbers.
9061       (while (and unread (< (car unread) 0))
9062         (setq unread (cdr unread)))
9063       ;; Remove any expired article numbers
9064       (while (and unread (< (car unread) (car active)))
9065         (setq unread (cdr unread)))
9066       ;; Compute the ranges of read articles by looking at the list of
9067       ;; unread articles.
9068       (while unread
9069         (when (/= (car unread) prev)
9070           (push (if (= prev (1- (car unread))) prev
9071                   (cons prev (1- (car unread))))
9072                 read))
9073         (setq prev (1+ (car unread)))
9074         (setq unread (cdr unread)))
9075       (when (<= prev (cdr active))
9076         (push (cons prev (cdr active)) read))
9077       (setq read (if (> (length read) 1) (nreverse read) read))
9078       (if compute
9079           read
9080         (save-excursion
9081           (set-buffer gnus-group-buffer)
9082           (gnus-undo-register
9083             `(progn
9084                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9085                (gnus-info-set-read ',info ',(gnus-info-read info))
9086                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9087                (gnus-group-update-group ,group t))))
9088        ;; Propagate the read marks to the backend.
9089        (if (gnus-check-backend-function 'request-set-mark group)
9090            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9091                  (add (gnus-remove-from-range read (gnus-info-read info))))
9092              (when (or add del)
9093                (gnus-request-set-mark
9094                 group (delq nil (list (if add (list add 'add '(read)))
9095                                       (if del (list del 'del '(read)))))))))
9096         ;; Enter this list into the group info.
9097         (gnus-info-set-read info read)
9098         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9099         (gnus-get-unread-articles-in-group info (gnus-active group))
9100         t))))
9101
9102 (defun gnus-offer-save-summaries ()
9103   "Offer to save all active summary buffers."
9104   (save-excursion
9105     (let ((buflist (buffer-list))
9106           buffers bufname)
9107       ;; Go through all buffers and find all summaries.
9108       (while buflist
9109         (and (setq bufname (buffer-name (car buflist)))
9110              (string-match "Summary" bufname)
9111              (save-excursion
9112                (set-buffer bufname)
9113                ;; We check that this is, indeed, a summary buffer.
9114                (and (eq major-mode 'gnus-summary-mode)
9115                     ;; Also make sure this isn't bogus.
9116                     gnus-newsgroup-prepared
9117                     ;; Also make sure that this isn't a dead summary buffer.
9118                     (not gnus-dead-summary-mode)))
9119              (push bufname buffers))
9120         (setq buflist (cdr buflist)))
9121       ;; Go through all these summary buffers and offer to save them.
9122       (when buffers
9123         (map-y-or-n-p
9124          "Update summary buffer %s? "
9125          (lambda (buf)
9126            (switch-to-buffer buf)
9127            (gnus-summary-exit))
9128          buffers)))))
9129
9130
9131 ;;; @ for mime-partial
9132 ;;;
9133
9134 (defun gnus-request-partial-message ()
9135   (save-excursion
9136     (let ((number (gnus-summary-article-number))
9137           (group gnus-newsgroup-name)
9138           (mother gnus-article-buffer))
9139       (set-buffer (get-buffer-create " *Partial Article*"))
9140       (erase-buffer)
9141       (setq mime-preview-buffer mother)
9142       (gnus-request-article-this-buffer number group)
9143       (mime-parse-buffer)
9144       )))
9145
9146 (autoload 'mime-combine-message/partial-pieces-automatically
9147   "mime-partial"
9148   "Internal method to combine message/partial messages automatically.")
9149
9150 (mime-add-condition
9151  'action '((type . message)(subtype . partial)
9152            (major-mode . gnus-original-article-mode)
9153            (method . mime-combine-message/partial-pieces-automatically)
9154            (summary-buffer-exp . gnus-summary-buffer)
9155            (request-partial-message-method . gnus-request-partial-message)
9156            ))
9157
9158
9159 ;;; @ for message/rfc822
9160 ;;;
9161
9162 (defun gnus-mime-extract-message/rfc822 (entity situation)
9163   (let (group article num cwin swin cur)
9164     (with-current-buffer (mime-entity-buffer entity)
9165       (save-restriction
9166         (narrow-to-region (mime-entity-body-start entity)
9167                           (mime-entity-body-end entity))
9168         (setq group (or (cdr (assq 'group situation))
9169                         (completing-read "Group: "
9170                                          gnus-active-hashtb
9171                                          nil
9172                                          (gnus-read-active-file-p)
9173                                          gnus-newsgroup-name))
9174               article (gnus-request-accept-article group)
9175               )
9176         ))
9177     (when (and (consp article)
9178                (numberp (setq article (cdr article))))
9179       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9180             cwin (get-buffer-window (current-buffer) t)
9181             )
9182       (save-window-excursion
9183         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9184             (select-window swin)
9185           (set-buffer gnus-summary-buffer)
9186           )
9187         (setq cur gnus-current-article)
9188         (forward-line num)
9189         (let (gnus-show-threads)
9190           (gnus-summary-goto-subject article t)
9191           )
9192         (gnus-summary-clear-mark-forward 1)
9193         (gnus-summary-goto-subject cur)
9194         )
9195       (when (and cwin (window-frame cwin))
9196         (select-frame (window-frame cwin))
9197         )
9198       (when (boundp 'mime-acting-situation-to-override)
9199         (set-alist 'mime-acting-situation-to-override
9200                    'group
9201                    group)
9202         (set-alist 'mime-acting-situation-to-override
9203                    'after-method
9204                    `(progn
9205                       (save-current-buffer
9206                         (set-buffer gnus-group-buffer)
9207                         (gnus-activate-group ,group)
9208                         )
9209                       (gnus-summary-goto-article ,cur
9210                                                  gnus-show-all-headers)
9211                       ))
9212         (set-alist 'mime-acting-situation-to-override
9213                    'number num)
9214         )
9215       )))
9216
9217 (mime-add-condition
9218  'action '((type . message)(subtype . rfc822)
9219            (major-mode . gnus-original-article-mode)
9220            (method . gnus-mime-extract-message/rfc822)
9221            (mode . "extract")
9222            ))
9223
9224 (mime-add-condition
9225  'action '((type . message)(subtype . news)
9226            (major-mode . gnus-original-article-mode)
9227            (method . gnus-mime-extract-message/rfc822)
9228            (mode . "extract")
9229            ))
9230
9231 (defun gnus-mime-extract-multipart (entity situation)
9232   (let ((children (mime-entity-children entity))
9233         mime-acting-situation-to-override
9234         f)
9235     (while children
9236       (mime-play-entity (car children)
9237                         (cons (assq 'mode situation)
9238                               mime-acting-situation-to-override))
9239       (setq children (cdr children)))
9240     (if (setq f (cdr (assq 'after-method
9241                            mime-acting-situation-to-override)))
9242         (eval f)
9243       )))  
9244
9245 (mime-add-condition
9246  'action '((type . multipart)
9247            (method . gnus-mime-extract-multipart)
9248            (mode . "extract")
9249            )
9250  'with-default)
9251
9252
9253 ;;; @ end
9254 ;;;
9255
9256 (defun gnus-summary-setup-default-charset ()
9257   "Setup newsgroup default charset."
9258   (let ((name (and gnus-newsgroup-name
9259                    (gnus-group-real-name gnus-newsgroup-name))))
9260     (setq gnus-newsgroup-charset
9261           (or (and gnus-newsgroup-name
9262                    (or (gnus-group-find-parameter gnus-newsgroup-name
9263                                                   'charset)
9264                        (let ((alist gnus-group-charset-alist)
9265                              elem (charset nil))
9266                          (while (setq elem (pop alist))
9267                            (when (and name
9268                                       (string-match (car elem) name))
9269                              (setq alist nil
9270                                    charset (cadr elem))))
9271                          charset)))
9272               gnus-default-charset))))
9273
9274 ;;;
9275 ;;; Mime Commands
9276 ;;;
9277
9278 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9279   "Display the current article buffer fully MIME-buttonized.
9280 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9281 treated as multipart/mixed."
9282   (interactive "P")
9283   (require 'gnus-art)
9284   (let ((gnus-unbuttonized-mime-types nil)
9285         (gnus-mime-display-multipart-as-mixed show-all-parts))
9286     (gnus-summary-show-article)))
9287
9288 (defun gnus-summary-repair-multipart (article)
9289   "Add a Content-Type header to a multipart article without one."
9290   (interactive (list (gnus-summary-article-number)))
9291   (gnus-with-article article
9292     (message-narrow-to-head)
9293     (goto-char (point-max))
9294     (widen)
9295     (when (search-forward "\n--" nil t)
9296       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9297         (message-narrow-to-head)
9298         (message-remove-header "Mime-Version")
9299         (message-remove-header "Content-Type")
9300         (goto-char (point-max))
9301         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9302                         separator))
9303         (insert "Mime-Version: 1.0\n")
9304         (widen))))
9305   (let (gnus-mark-article-hook)
9306     (gnus-summary-select-article t t nil article)))
9307
9308 (defun gnus-summary-toggle-display-buttonized ()
9309   "Toggle the buttonizing of the article buffer."
9310   (interactive)
9311   (require 'gnus-art)
9312   (if (setq gnus-inhibit-mime-unbuttonizing
9313             (not gnus-inhibit-mime-unbuttonizing))
9314       (let ((gnus-unbuttonized-mime-types nil))
9315         (gnus-summary-show-article))
9316     (gnus-summary-show-article)))
9317
9318 (gnus-ems-redefine)
9319
9320 (provide 'gnus-sum)
9321
9322 (run-hooks 'gnus-sum-load-hook)
9323
9324 ;;; gnus-sum.el ends here