ee421d3a7c568ab6d26c103b29703eb0df80d4d9
[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 (eval-and-compile
49   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, marking commands will go to the next unread article.
186 If `never', commands that usually go to the next unread article, will
187 go to the next article, whether it is read or not.
188 If nil, only the marking commands will go to the next (un)read article."
189   :group 'gnus-summary-marks
190   :link '(custom-manual "(gnus)Setting Marks")
191   :type '(choice (const :tag "off" nil)
192                  (const never)
193                  (sexp :menu-tag "on" t)))
194
195 (defcustom gnus-summary-default-score 0
196   "*Default article score level.
197 All scores generated by the score files will be added to this score.
198 If this variable is nil, scoring will be disabled."
199   :group 'gnus-score-default
200   :type '(choice (const :tag "disable")
201                  integer))
202
203 (defcustom gnus-summary-zcore-fuzz 0
204   "*Fuzziness factor for the zcore in the summary buffer.
205 Articles with scores closer than this to `gnus-summary-default-score'
206 will not be marked."
207   :group 'gnus-summary-format
208   :type 'integer)
209
210 (defcustom gnus-simplify-subject-fuzzy-regexp nil
211   "*Strings to be removed when doing fuzzy matches.
212 This can either be a regular expression or list of regular expressions
213 that will be removed from subject strings if fuzzy subject
214 simplification is selected."
215   :group 'gnus-thread
216   :type '(repeat regexp))
217
218 (defcustom gnus-show-threads t
219   "*If non-nil, display threads in summary mode."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-subtree nil
224   "*If non-nil, hide all threads initially.
225 If threads are hidden, you have to run the command
226 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
227 to expose hidden threads."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-hide-killed t
232   "*If non-nil, hide killed threads automatically."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-ignore-subject t
237   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
238 If nil, articles that have different subjects from their parents will
239 start separate threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-operation-ignore-subject t
244   "*If non-nil, subjects will be ignored when doing thread commands.
245 This affects commands like `gnus-summary-kill-thread' and
246 `gnus-summary-lower-thread'.
247
248 If this variable is nil, articles in the same thread with different
249 subjects will not be included in the operation in question.  If this
250 variable is `fuzzy', only articles that have subjects that are fuzzily
251 equal will be included."
252   :group 'gnus-thread
253   :type '(choice (const :tag "off" nil)
254                  (const fuzzy)
255                  (sexp :tag "on" t)))
256
257 (defcustom gnus-thread-indent-level 4
258   "*Number that says how much each sub-thread should be indented."
259   :group 'gnus-thread
260   :type 'integer)
261
262 (defcustom gnus-auto-extend-newsgroup t
263   "*If non-nil, extend newsgroup forward and backward when requested."
264   :group 'gnus-summary-choose
265   :type 'boolean)
266
267 (defcustom gnus-auto-select-first t
268   "*If nil, don't select the first unread article when entering a group.
269 If this variable is `best', select the highest-scored unread article
270 in the group.  If t, select the first unread article.
271
272 This variable can also be a function to place point on a likely
273 subject line.  Useful values include `gnus-summary-first-unread-subject',
274 `gnus-summary-first-unread-article' and
275 `gnus-summary-best-unread-article'.
276
277 If you want to prevent automatic selection of the first unread article
278 in some newsgroups, set the variable to nil in
279 `gnus-select-group-hook'."
280   :group 'gnus-group-select
281   :type '(choice (const :tag "none" nil)
282                  (const best)
283                  (sexp :menu-tag "first" t)
284                  (function-item gnus-summary-first-unread-subject)
285                  (function-item gnus-summary-first-unread-article)
286                  (function-item gnus-summary-best-unread-article)))
287
288 (defcustom gnus-auto-select-next t
289   "*If non-nil, offer to go to the next group from the end of the previous.
290 If the value is t and the next newsgroup is empty, Gnus will exit
291 summary mode and go back to group mode.  If the value is neither nil
292 nor t, Gnus will select the following unread newsgroup.  In
293 particular, if the value is the symbol `quietly', the next unread
294 newsgroup will be selected without any confirmation, and if it is
295 `almost-quietly', the next group will be selected without any
296 confirmation if you are located on the last article in the group.
297 Finally, if this variable is `slightly-quietly', the `Z n' command
298 will go to the next group without confirmation."
299   :group 'gnus-summary-maneuvering
300   :type '(choice (const :tag "off" nil)
301                  (const quietly)
302                  (const almost-quietly)
303                  (const slightly-quietly)
304                  (sexp :menu-tag "on" t)))
305
306 (defcustom gnus-auto-select-same nil
307   "*If non-nil, select the next article with the same subject.
308 If there are no more articles with the same subject, go to
309 the first unread article."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-summary-check-current nil
314   "*If non-nil, consider the current article when moving.
315 The \"unread\" movement commands will stay on the same line if the
316 current article is unread."
317   :group 'gnus-summary-maneuvering
318   :type 'boolean)
319
320 (defcustom gnus-auto-center-summary t
321   "*If non-nil, always center the current summary buffer.
322 In particular, if `vertical' do only vertical recentering.  If non-nil
323 and non-`vertical', do both horizontal and vertical recentering."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "none" nil)
326                  (const vertical)
327                  (integer :tag "height")
328                  (sexp :menu-tag "both" t)))
329
330 (defcustom gnus-show-all-headers nil
331   "*If non-nil, don't hide any headers."
332   :group 'gnus-article-hiding
333   :group 'gnus-article-headers
334   :type 'boolean)
335
336 (defcustom gnus-summary-ignore-duplicates nil
337   "*If non-nil, ignore articles with identical Message-ID headers."
338   :group 'gnus-summary
339   :type 'boolean)
340
341 (defcustom gnus-single-article-buffer t
342   "*If non-nil, display all articles in the same buffer.
343 If nil, each group will get its own article buffer."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-break-pages t
348   "*If non-nil, do page breaking on articles.
349 The page delimiter is specified by the `gnus-page-delimiter'
350 variable."
351   :group 'gnus-article-various
352   :type 'boolean)
353
354 (defcustom gnus-show-mime t
355   "*If non-nil, do mime processing of articles.
356 The articles will simply be fed to the function given by
357 `gnus-article-display-method-for-mime'."
358   :group 'gnus-article-mime
359   :type 'boolean)
360
361 (defcustom gnus-move-split-methods nil
362   "*Variable used to suggest where articles are to be moved to.
363 It uses the same syntax as the `gnus-split-methods' variable."
364   :group 'gnus-summary-mail
365   :type '(repeat (choice (list :value (fun) function)
366                          (cons :value ("" "") regexp (repeat string))
367                          (sexp :value nil))))
368
369 (defcustom gnus-unread-mark ? ;Whitespace
370   "*Mark used for unread articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-ticked-mark ?!
375   "*Mark used for ticked articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-dormant-mark ??
380   "*Mark used for dormant articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-del-mark ?r
385   "*Mark used for del'd articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-read-mark ?R
390   "*Mark used for read articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-expirable-mark ?E
395   "*Mark used for expirable articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-killed-mark ?K
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-souped-mark ?F
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-kill-file-mark ?X
410   "*Mark used for articles killed by kill files."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-low-score-mark ?Y
415   "*Mark used for articles with a low score."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-catchup-mark ?C
420   "*Mark used for articles that are caught up."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-replied-mark ?A
425   "*Mark used for articles that have been replied to."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-cached-mark ?*
430   "*Mark used for articles that are in the cache."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-saved-mark ?S
435   "*Mark used for articles that have been saved to."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-ancient-mark ?O
440   "*Mark used for ancient articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-sparse-mark ?Q
445   "*Mark used for sparsely reffed articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-canceled-mark ?G
450   "*Mark used for canceled articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-duplicate-mark ?M
455   "*Mark used for duplicate articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-undownloaded-mark ?@
460   "*Mark used for articles that weren't downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-downloadable-mark ?%
465   "*Mark used for articles that are to be downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-unsendable-mark ?=
470   "*Mark used for articles that won't be sent."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-over-mark ?+
475   "*Score mark used for articles with high scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-below-mark ?-
480   "*Score mark used for articles with low scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-empty-thread-mark ? ;Whitespace
485   "*There is no thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-not-empty-thread-mark ?=
490   "*There is a thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-view-pseudo-asynchronously nil
495   "*If non-nil, Gnus will view pseudo-articles asynchronously."
496   :group 'gnus-extract-view
497   :type 'boolean)
498
499 (defcustom gnus-auto-expirable-marks
500   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
501         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
502         gnus-souped-mark gnus-duplicate-mark)
503   "*The list of marks converted into expiration if a group is auto-expirable."
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :group 'gnus-summary
510   :type 'boolean)
511
512 (defcustom gnus-view-pseudos nil
513   "*If `automatic', pseudo-articles will be viewed automatically.
514 If `not-confirm', pseudos will be viewed automatically, and the user
515 will not be asked to confirm the command."
516   :group 'gnus-extract-view
517   :type '(choice (const :tag "off" nil)
518                  (const automatic)
519                  (const not-confirm)))
520
521 (defcustom gnus-view-pseudos-separately t
522   "*If non-nil, one pseudo-article will be created for each file to be viewed.
523 If nil, all files that use the same viewing command will be given as a
524 list of parameters to that command."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-insert-pseudo-articles t
529   "*If non-nil, insert pseudo-articles when decoding articles."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-summary-dummy-line-format
534   "  %(:                          :%) %S\n"
535   "*The format specification for the dummy roots in the summary buffer.
536 It works along the same lines as a normal formatting string,
537 with some simple extensions.
538
539 %S  The subject"
540   :group 'gnus-threading
541   :type 'string)
542
543 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
544   "*The format specification for the summary mode line.
545 It works along the same lines as a normal formatting string,
546 with some simple extensions:
547
548 %G  Group name
549 %p  Unprefixed group name
550 %A  Current article number
551 %z  Current article score
552 %V  Gnus version
553 %U  Number of unread articles in the group
554 %e  Number of unselected articles in the group
555 %Z  A string with unread/unselected article counts
556 %g  Shortish group name
557 %S  Subject of the current article
558 %u  User-defined spec
559 %s  Current score file name
560 %d  Number of dormant articles
561 %r  Number of articles that have been marked as read in this session
562 %E  Number of articles expunged by the score files"
563   :group 'gnus-summary-format
564   :type 'string)
565
566 (defcustom gnus-summary-mark-below 0
567   "*Mark all articles with a score below this variable as read.
568 This variable is local to each summary buffer and usually set by the
569 score file."
570   :group 'gnus-score-default
571   :type 'integer)
572
573 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
574   "*List of functions used for sorting articles in the summary buffer.
575 This variable is only used when not using a threaded display."
576   :group 'gnus-summary-sort
577   :type '(repeat (choice (function-item gnus-article-sort-by-number)
578                          (function-item gnus-article-sort-by-author)
579                          (function-item gnus-article-sort-by-subject)
580                          (function-item gnus-article-sort-by-date)
581                          (function-item gnus-article-sort-by-score)
582                          (function :tag "other"))))
583
584 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
585   "*List of functions used for sorting threads in the summary buffer.
586 By default, threads are sorted by article number.
587
588 Each function takes two threads and return non-nil if the first thread
589 should be sorted before the other.  If you use more than one function,
590 the primary sort function should be the last.  You should probably
591 always include `gnus-thread-sort-by-number' in the list of sorting
592 functions -- preferably first.
593
594 Ready-made functions include `gnus-thread-sort-by-number',
595 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
596 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
597 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
600                          (function-item gnus-thread-sort-by-author)
601                          (function-item gnus-thread-sort-by-subject)
602                          (function-item gnus-thread-sort-by-date)
603                          (function-item gnus-thread-sort-by-score)
604                          (function-item gnus-thread-sort-by-total-score)
605                          (function :tag "other"))))
606
607 (defcustom gnus-thread-score-function '+
608   "*Function used for calculating the total score of a thread.
609
610 The function is called with the scores of the article and each
611 subthread and should then return the score of the thread.
612
613 Some functions you can use are `+', `max', or `min'."
614   :group 'gnus-summary-sort
615   :type 'function)
616
617 (defcustom gnus-summary-expunge-below nil
618   "All articles that have a score less than this variable will be expunged.
619 This variable is local to the summary buffers."
620   :group 'gnus-score-default
621   :type '(choice (const :tag "off" nil)
622                  integer))
623
624 (defcustom gnus-thread-expunge-below nil
625   "All threads that have a total score less than this variable will be expunged.
626 See `gnus-thread-score-function' for en explanation of what a
627 \"thread score\" is.
628
629 This variable is local to the summary buffers."
630   :group 'gnus-threading
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-summary-mode-hook nil
636   "*A hook for Gnus summary mode.
637 This hook is run before any variables are set in the summary buffer."
638   :group 'gnus-summary-various
639   :type 'hook)
640
641 (defcustom gnus-summary-menu-hook nil
642   "*Hook run after the creation of the summary mode menu."
643   :group 'gnus-summary-visual
644   :type 'hook)
645
646 (defcustom gnus-summary-exit-hook nil
647   "*A hook called on exit from the summary buffer.
648 It will be called with point in the group buffer."
649   :group 'gnus-summary-exit
650   :type 'hook)
651
652 (defcustom gnus-summary-prepare-hook nil
653   "*A hook called after the summary buffer has been generated.
654 If you want to modify the summary buffer, you can use this hook."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-prepared-hook nil
659   "*A hook called as the last thing after the summary buffer has been generated."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-generate-hook nil
664   "*A hook run just before generating the summary buffer.
665 This hook is commonly used to customize threading variables and the
666 like."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-select-group-hook nil
671   "*A hook called when a newsgroup is selected.
672
673 If you'd like to simplify subjects like the
674 `gnus-summary-next-same-subject' command does, you can use the
675 following hook:
676
677  (setq gnus-select-group-hook
678       (list
679         (lambda ()
680           (mapcar (lambda (header)
681                      (mail-header-set-subject
682                       header
683                       (gnus-simplify-subject
684                        (mail-header-subject header) 're-only)))
685                   gnus-newsgroup-headers))))"
686   :group 'gnus-group-select
687   :type 'hook)
688
689 (defcustom gnus-select-article-hook nil
690   "*A hook called when an article is selected."
691   :group 'gnus-summary-choose
692   :type 'hook)
693
694 (defcustom gnus-visual-mark-article-hook
695   (list 'gnus-highlight-selected-summary)
696   "*Hook run after selecting an article in the summary buffer.
697 It is meant to be used for highlighting the article in some way.  It
698 is not run if `gnus-visual' is nil."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
703   "*A hook called before parsing the headers."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-exit-group-hook nil
708   "*A hook called when exiting (not quitting) summary mode."
709   :group 'gnus-various
710   :type 'hook)
711
712 (defcustom gnus-summary-update-hook
713   (list 'gnus-summary-highlight-line)
714   "*A hook called when a summary line is changed.
715 The hook will not be called if `gnus-visual' is nil.
716
717 The default function `gnus-summary-highlight-line' will
718 highlight the line according to the `gnus-summary-highlight'
719 variable."
720   :group 'gnus-summary-visual
721   :type 'hook)
722
723 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
724   "*A hook called when an article is selected for the first time.
725 The hook is intended to mark an article as read (or unread)
726 automatically when it is selected."
727   :group 'gnus-summary-choose
728   :type 'hook)
729
730 (defcustom gnus-group-no-more-groups-hook nil
731   "*A hook run when returning to group mode having no more (unread) groups."
732   :group 'gnus-group-select
733   :type 'hook)
734
735 (defcustom gnus-ps-print-hook nil
736   "*A hook run before ps-printing something from Gnus."
737   :group 'gnus-summary
738   :type 'hook)
739
740 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
741   "Face used for highlighting the current article in the summary buffer."
742   :group 'gnus-summary-visual
743   :type 'face)
744
745 (defcustom gnus-summary-highlight
746   '(((= mark gnus-canceled-mark)
747      . gnus-summary-cancelled-face)
748     ((and (> score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-high-ticked-face)
752     ((and (< score default)
753           (or (= mark gnus-dormant-mark)
754               (= mark gnus-ticked-mark)))
755      . gnus-summary-low-ticked-face)
756     ((or (= mark gnus-dormant-mark)
757          (= mark gnus-ticked-mark))
758      . gnus-summary-normal-ticked-face)
759     ((and (> score default) (= mark gnus-ancient-mark))
760      . gnus-summary-high-ancient-face)
761     ((and (< score default) (= mark gnus-ancient-mark))
762      . gnus-summary-low-ancient-face)
763     ((= mark gnus-ancient-mark)
764      . gnus-summary-normal-ancient-face)
765     ((and (> score default) (= mark gnus-unread-mark))
766      . gnus-summary-high-unread-face)
767     ((and (< score default) (= mark gnus-unread-mark))
768      . gnus-summary-low-unread-face)
769     ((= mark gnus-unread-mark)
770      . gnus-summary-normal-unread-face)
771     ((and (> score default) (memq mark (list gnus-downloadable-mark
772                                              gnus-undownloaded-mark)))
773      . gnus-summary-high-unread-face)
774     ((and (< score default) (memq mark (list gnus-downloadable-mark
775                                              gnus-undownloaded-mark)))
776      . gnus-summary-low-unread-face)
777     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
778      . gnus-summary-normal-unread-face)
779     ((> score default)
780      . gnus-summary-high-read-face)
781     ((< score default)
782      . gnus-summary-low-read-face)
783     (t
784      . gnus-summary-normal-read-face))
785   "*Controls the highlighting of summary buffer lines.
786
787 A list of (FORM . FACE) pairs.  When deciding how a a particular
788 summary line should be displayed, each form is evaluated.  The content
789 of the face field after the first true form is used.  You can change
790 how those summary lines are displayed, by editing the face field.
791
792 You can use the following variables in the FORM field.
793
794 score:   The articles score
795 default: The default article score.
796 below:   The score below which articles are automatically marked as read.
797 mark:    The articles mark."
798   :group 'gnus-summary-visual
799   :type '(repeat (cons (sexp :tag "Form" nil)
800                        face)))
801
802 (defcustom gnus-alter-header-function nil
803   "Function called to allow alteration of article header structures.
804 The function is called with one parameter, the article header vector,
805 which it may alter in any way.")
806
807 (defcustom gnus-extra-headers nil
808   "*Extra headers to parse."
809   :group 'gnus-summary
810   :type '(repeat symbol))
811
812 (defcustom gnus-ignored-from-addresses
813   (and user-mail-address (regexp-quote user-mail-address))
814   "*Regexp of From headers that may be suppressed in favor of To headers."
815   :group 'gnus-summary
816   :type 'regexp)
817
818 (defcustom gnus-group-charset-alist
819   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
820     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
821     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
822     ("^relcom\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
827     (".*" iso-8859-1))
828   "Alist of regexps (to match group names) and default charsets to be used when reading."
829   :type '(repeat (list (regexp :tag "Group")
830                        (symbol :tag "Charset")))
831   :group 'gnus-charset)
832
833 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
834   "List of charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat symbol)
838   :group 'gnus-charset)
839
840 (defcustom gnus-group-ignored-charsets-alist 
841   '(("alt\\.chinese\\.text" iso-8859-1))
842   "Alist of regexps (to match group names) and charsets that should be ignored.
843 When these charsets are used in the \"charset\" parameter, the
844 default charset will be used instead."
845   :type '(repeat (cons (regexp :tag "Group")
846                        (repeat symbol)))
847   :group 'gnus-charset)
848
849 (defcustom gnus-group-highlight-words-alist nil
850   "Alist of group regexps and highlight regexps.
851 This variable uses the same syntax as `gnus-emphasis-alist'."
852   :type '(repeat (cons (regexp :tag "Group")
853                        (repeat (list (regexp :tag "Highlight regexp")
854                                      (number :tag "Group for entire word" 0)
855                                      (number :tag "Group for displayed part" 0)
856                                      (symbol :tag "Face" 
857                                              gnus-emphasis-highlight-words)))))
858   :group 'gnus-summary-visual)
859
860 ;;; Internal variables
861
862 (defvar gnus-scores-exclude-files nil)
863 (defvar gnus-page-broken nil)
864 (defvar gnus-inhibit-mime-unbuttonizing nil)
865
866 (defvar gnus-original-article nil)
867 (defvar gnus-article-internal-prepare-hook nil)
868 (defvar gnus-newsgroup-process-stack nil)
869
870 (defvar gnus-thread-indent-array nil)
871 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
872
873 ;; Avoid highlighting in kill files.
874 (defvar gnus-summary-inhibit-highlight nil)
875 (defvar gnus-newsgroup-selected-overlay nil)
876 (defvar gnus-inhibit-limiting nil)
877 (defvar gnus-newsgroup-adaptive-score-file nil)
878 (defvar gnus-current-score-file nil)
879 (defvar gnus-current-move-group nil)
880 (defvar gnus-current-copy-group nil)
881 (defvar gnus-current-crosspost-group nil)
882
883 (defvar gnus-newsgroup-dependencies nil)
884 (defvar gnus-newsgroup-adaptive nil)
885 (defvar gnus-summary-display-article-function nil)
886 (defvar gnus-summary-highlight-line-function nil
887   "Function called after highlighting a summary line.")
888
889 (defvar gnus-summary-line-format-alist
890   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
891     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
892     (?s gnus-tmp-subject-or-nil ?s)
893     (?n gnus-tmp-name ?s)
894     (?A (std11-address-string
895          (car (mime-read-field 'From gnus-tmp-header))) ?s)
896     (?a (or (std11-full-name-string
897              (car (mime-read-field 'From gnus-tmp-header)))
898             gnus-tmp-from) ?s)
899     (?F gnus-tmp-from ?s)
900     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
901     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
902     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
903     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
904     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
905     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
906     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
907     (?L gnus-tmp-lines ?d)
908     (?I gnus-tmp-indentation ?s)
909     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
910     (?R gnus-tmp-replied ?c)
911     (?\[ gnus-tmp-opening-bracket ?c)
912     (?\] gnus-tmp-closing-bracket ?c)
913     (?\> (make-string gnus-tmp-level ? ) ?s)
914     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
915     (?i gnus-tmp-score ?d)
916     (?z gnus-tmp-score-char ?c)
917     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
918     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
919     (?U gnus-tmp-unread ?c)
920     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
921     (?t (gnus-summary-number-of-articles-in-thread
922          (and (boundp 'thread) (car thread)) gnus-tmp-level)
923         ?d)
924     (?e (gnus-summary-number-of-articles-in-thread
925          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
926         ?c)
927     (?u gnus-tmp-user-defined ?s)
928     (?P (gnus-pick-line-number) ?d))
929   "An alist of format specifications that can appear in summary lines,
930 and what variables they correspond with, along with the type of the
931 variable (string, integer, character, etc).")
932
933 (defvar gnus-summary-dummy-line-format-alist
934   `((?S gnus-tmp-subject ?s)
935     (?N gnus-tmp-number ?d)
936     (?u gnus-tmp-user-defined ?s)))
937
938 (defvar gnus-summary-mode-line-format-alist
939   `((?G gnus-tmp-group-name ?s)
940     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
941     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
942     (?A gnus-tmp-article-number ?d)
943     (?Z gnus-tmp-unread-and-unselected ?s)
944     (?V gnus-version ?s)
945     (?U gnus-tmp-unread-and-unticked ?d)
946     (?S gnus-tmp-subject ?s)
947     (?e gnus-tmp-unselected ?d)
948     (?u gnus-tmp-user-defined ?s)
949     (?d (length gnus-newsgroup-dormant) ?d)
950     (?t (length gnus-newsgroup-marked) ?d)
951     (?r (length gnus-newsgroup-reads) ?d)
952     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
953     (?E gnus-newsgroup-expunged-tally ?d)
954     (?s (gnus-current-score-file-nondirectory) ?s)))
955
956 (defvar gnus-last-search-regexp nil
957   "Default regexp for article search command.")
958
959 (defvar gnus-last-shell-command nil
960   "Default shell command on article.")
961
962 (defvar gnus-newsgroup-begin nil)
963 (defvar gnus-newsgroup-end nil)
964 (defvar gnus-newsgroup-last-rmail nil)
965 (defvar gnus-newsgroup-last-mail nil)
966 (defvar gnus-newsgroup-last-folder nil)
967 (defvar gnus-newsgroup-last-file nil)
968 (defvar gnus-newsgroup-auto-expire nil)
969 (defvar gnus-newsgroup-active nil)
970
971 (defvar gnus-newsgroup-data nil)
972 (defvar gnus-newsgroup-data-reverse nil)
973 (defvar gnus-newsgroup-limit nil)
974 (defvar gnus-newsgroup-limits nil)
975
976 (defvar gnus-newsgroup-unreads nil
977   "List of unread articles in the current newsgroup.")
978
979 (defvar gnus-newsgroup-unselected nil
980   "List of unselected unread articles in the current newsgroup.")
981
982 (defvar gnus-newsgroup-reads nil
983   "Alist of read articles and article marks in the current newsgroup.")
984
985 (defvar gnus-newsgroup-expunged-tally nil)
986
987 (defvar gnus-newsgroup-marked nil
988   "List of ticked articles in the current newsgroup (a subset of unread art).")
989
990 (defvar gnus-newsgroup-killed nil
991   "List of ranges of articles that have been through the scoring process.")
992
993 (defvar gnus-newsgroup-cached nil
994   "List of articles that come from the article cache.")
995
996 (defvar gnus-newsgroup-saved nil
997   "List of articles that have been saved.")
998
999 (defvar gnus-newsgroup-kill-headers nil)
1000
1001 (defvar gnus-newsgroup-replied nil
1002   "List of articles that have been replied to in the current newsgroup.")
1003
1004 (defvar gnus-newsgroup-expirable nil
1005   "List of articles in the current newsgroup that can be expired.")
1006
1007 (defvar gnus-newsgroup-processable nil
1008   "List of articles in the current newsgroup that can be processed.")
1009
1010 (defvar gnus-newsgroup-downloadable nil
1011   "List of articles in the current newsgroup that can be processed.")
1012
1013 (defvar gnus-newsgroup-undownloaded nil
1014   "List of articles in the current newsgroup that haven't been downloaded..")
1015
1016 (defvar gnus-newsgroup-unsendable nil
1017   "List of articles in the current newsgroup that won't be sent.")
1018
1019 (defvar gnus-newsgroup-bookmarks nil
1020   "List of articles in the current newsgroup that have bookmarks.")
1021
1022 (defvar gnus-newsgroup-dormant nil
1023   "List of dormant articles in the current newsgroup.")
1024
1025 (defvar gnus-newsgroup-scored nil
1026   "List of scored articles in the current newsgroup.")
1027
1028 (defvar gnus-newsgroup-headers nil
1029   "List of article headers in the current newsgroup.")
1030
1031 (defvar gnus-newsgroup-threads nil)
1032
1033 (defvar gnus-newsgroup-prepared nil
1034   "Whether the current group has been prepared properly.")
1035
1036 (defvar gnus-newsgroup-ancient nil
1037   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1038
1039 (defvar gnus-newsgroup-sparse nil)
1040
1041 (defvar gnus-current-article nil)
1042 (defvar gnus-article-current nil)
1043 (defvar gnus-current-headers nil)
1044 (defvar gnus-have-all-headers nil)
1045 (defvar gnus-last-article nil)
1046 (defvar gnus-newsgroup-history nil)
1047 (defvar gnus-newsgroup-charset nil)
1048
1049 (defconst gnus-summary-local-variables
1050   '(gnus-newsgroup-name
1051     gnus-newsgroup-begin gnus-newsgroup-end
1052     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1053     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1054     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1055     gnus-newsgroup-unselected gnus-newsgroup-marked
1056     gnus-newsgroup-reads gnus-newsgroup-saved
1057     gnus-newsgroup-replied gnus-newsgroup-expirable
1058     gnus-newsgroup-processable gnus-newsgroup-killed
1059     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1060     gnus-newsgroup-unsendable
1061     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1062     gnus-newsgroup-headers gnus-newsgroup-threads
1063     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1064     gnus-current-article gnus-current-headers gnus-have-all-headers
1065     gnus-last-article gnus-article-internal-prepare-hook
1066     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1067     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1068     gnus-thread-expunge-below
1069     gnus-score-alist gnus-current-score-file
1070     (gnus-summary-expunge-below . global)
1071     (gnus-summary-mark-below . global)
1072     gnus-newsgroup-active gnus-scores-exclude-files
1073     gnus-newsgroup-history gnus-newsgroup-ancient
1074     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1075     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1076     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1077     (gnus-newsgroup-expunged-tally . 0)
1078     gnus-cache-removable-articles gnus-newsgroup-cached
1079     gnus-newsgroup-data gnus-newsgroup-data-reverse
1080     gnus-newsgroup-limit gnus-newsgroup-limits
1081     gnus-newsgroup-charset)
1082   "Variables that are buffer-local to the summary buffers.")
1083
1084 ;; Byte-compiler warning.
1085 (defvar gnus-article-mode-map)
1086
1087 ;; Subject simplification.
1088
1089 (defun gnus-simplify-whitespace (str)
1090   "Remove excessive whitespace."
1091   (let ((mystr str))
1092     ;; Multiple spaces.
1093     (while (string-match "[ \t][ \t]+" mystr)
1094       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1095                           " "
1096                           (substring mystr (match-end 0)))))
1097     ;; Leading spaces.
1098     (when (string-match "^[ \t]+" mystr)
1099       (setq mystr (substring mystr (match-end 0))))
1100     ;; Trailing spaces.
1101     (when (string-match "[ \t]+$" mystr)
1102       (setq mystr (substring mystr 0 (match-beginning 0))))
1103     mystr))
1104
1105 (defsubst gnus-simplify-subject-re (subject)
1106   "Remove \"Re:\" from subject lines."
1107   (if (string-match "^[Rr][Ee]: *" subject)
1108       (substring subject (match-end 0))
1109     subject))
1110
1111 (defun gnus-simplify-subject (subject &optional re-only)
1112   "Remove `Re:' and words in parentheses.
1113 If RE-ONLY is non-nil, strip leading `Re:'s only."
1114   (let ((case-fold-search t))           ;Ignore case.
1115     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1116     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1117       (setq subject (substring subject (match-end 0))))
1118     ;; Remove uninteresting prefixes.
1119     (when (and (not re-only)
1120                gnus-simplify-ignored-prefixes
1121                (string-match gnus-simplify-ignored-prefixes subject))
1122       (setq subject (substring subject (match-end 0))))
1123     ;; Remove words in parentheses from end.
1124     (unless re-only
1125       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1126         (setq subject (substring subject 0 (match-beginning 0)))))
1127     ;; Return subject string.
1128     subject))
1129
1130 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1131 ;; all whitespace.
1132 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1133   (goto-char (point-min))
1134   (while (re-search-forward regexp nil t)
1135       (replace-match (or newtext ""))))
1136
1137 (defun gnus-simplify-buffer-fuzzy ()
1138   "Simplify string in the buffer fuzzily.
1139 The string in the accessible portion of the current buffer is simplified.
1140 It is assumed to be a single-line subject.
1141 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1142 matter is removed.  Additional things can be deleted by setting
1143 gnus-simplify-subject-fuzzy-regexp."
1144   (let ((case-fold-search t)
1145         (modified-tick))
1146     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1147
1148     (while (not (eq modified-tick (buffer-modified-tick)))
1149       (setq modified-tick (buffer-modified-tick))
1150       (cond
1151        ((listp gnus-simplify-subject-fuzzy-regexp)
1152         (mapcar 'gnus-simplify-buffer-fuzzy-step
1153                 gnus-simplify-subject-fuzzy-regexp))
1154        (gnus-simplify-subject-fuzzy-regexp
1155         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1156       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1157       (gnus-simplify-buffer-fuzzy-step
1158        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1159       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1160
1161     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1162     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1163     (gnus-simplify-buffer-fuzzy-step " $")
1164     (gnus-simplify-buffer-fuzzy-step "^ +")))
1165
1166 (defun gnus-simplify-subject-fuzzy (subject)
1167   "Simplify a subject string fuzzily.
1168 See `gnus-simplify-buffer-fuzzy' for details."
1169   (save-excursion
1170     (gnus-set-work-buffer)
1171     (let ((case-fold-search t))
1172       ;; Remove uninteresting prefixes.
1173       (when (and gnus-simplify-ignored-prefixes
1174                  (string-match gnus-simplify-ignored-prefixes subject))
1175         (setq subject (substring subject (match-end 0))))
1176       (insert subject)
1177       (inline (gnus-simplify-buffer-fuzzy))
1178       (buffer-string))))
1179
1180 (defsubst gnus-simplify-subject-fully (subject)
1181   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1182   (cond
1183    (gnus-simplify-subject-functions
1184     (gnus-map-function gnus-simplify-subject-functions subject))
1185    ((null gnus-summary-gather-subject-limit)
1186     (gnus-simplify-subject-re subject))
1187    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1188     (gnus-simplify-subject-fuzzy subject))
1189    ((numberp gnus-summary-gather-subject-limit)
1190     (gnus-limit-string (gnus-simplify-subject-re subject)
1191                        gnus-summary-gather-subject-limit))
1192    (t
1193     subject)))
1194
1195 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1196   "Check whether two subjects are equal.
1197 If optional argument simple-first is t, first argument is already
1198 simplified."
1199   (cond
1200    ((null simple-first)
1201     (equal (gnus-simplify-subject-fully s1)
1202            (gnus-simplify-subject-fully s2)))
1203    (t
1204     (equal s1
1205            (gnus-simplify-subject-fully s2)))))
1206
1207 (defun gnus-summary-bubble-group ()
1208   "Increase the score of the current group.
1209 This is a handy function to add to `gnus-summary-exit-hook' to
1210 increase the score of each group you read."
1211   (gnus-group-add-score gnus-newsgroup-name))
1212
1213 \f
1214 ;;;
1215 ;;; Gnus summary mode
1216 ;;;
1217
1218 (put 'gnus-summary-mode 'mode-class 'special)
1219
1220 (when t
1221   ;; Non-orthogonal keys
1222
1223   (gnus-define-keys gnus-summary-mode-map
1224     " " gnus-summary-next-page
1225     "\177" gnus-summary-prev-page
1226     [delete] gnus-summary-prev-page
1227     [backspace] gnus-summary-prev-page
1228     "\r" gnus-summary-scroll-up
1229     "\M-\r" gnus-summary-scroll-down
1230     "n" gnus-summary-next-unread-article
1231     "p" gnus-summary-prev-unread-article
1232     "N" gnus-summary-next-article
1233     "P" gnus-summary-prev-article
1234     "\M-\C-n" gnus-summary-next-same-subject
1235     "\M-\C-p" gnus-summary-prev-same-subject
1236     "\M-n" gnus-summary-next-unread-subject
1237     "\M-p" gnus-summary-prev-unread-subject
1238     "." gnus-summary-first-unread-article
1239     "," gnus-summary-best-unread-article
1240     "\M-s" gnus-summary-search-article-forward
1241     "\M-r" gnus-summary-search-article-backward
1242     "<" gnus-summary-beginning-of-article
1243     ">" gnus-summary-end-of-article
1244     "j" gnus-summary-goto-article
1245     "^" gnus-summary-refer-parent-article
1246     "\M-^" gnus-summary-refer-article
1247     "u" gnus-summary-tick-article-forward
1248     "!" gnus-summary-tick-article-forward
1249     "U" gnus-summary-tick-article-backward
1250     "d" gnus-summary-mark-as-read-forward
1251     "D" gnus-summary-mark-as-read-backward
1252     "E" gnus-summary-mark-as-expirable
1253     "\M-u" gnus-summary-clear-mark-forward
1254     "\M-U" gnus-summary-clear-mark-backward
1255     "k" gnus-summary-kill-same-subject-and-select
1256     "\C-k" gnus-summary-kill-same-subject
1257     "\M-\C-k" gnus-summary-kill-thread
1258     "\M-\C-l" gnus-summary-lower-thread
1259     "e" gnus-summary-edit-article
1260     "#" gnus-summary-mark-as-processable
1261     "\M-#" gnus-summary-unmark-as-processable
1262     "\M-\C-t" gnus-summary-toggle-threads
1263     "\M-\C-s" gnus-summary-show-thread
1264     "\M-\C-h" gnus-summary-hide-thread
1265     "\M-\C-f" gnus-summary-next-thread
1266     "\M-\C-b" gnus-summary-prev-thread
1267     "\M-\C-u" gnus-summary-up-thread
1268     "\M-\C-d" gnus-summary-down-thread
1269     "&" gnus-summary-execute-command
1270     "c" gnus-summary-catchup-and-exit
1271     "\C-w" gnus-summary-mark-region-as-read
1272     "\C-t" gnus-summary-toggle-truncation
1273     "?" gnus-summary-mark-as-dormant
1274     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1275     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1276     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1277     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1278     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1279     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1280     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1281     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1282     "=" gnus-summary-expand-window
1283     "\C-x\C-s" gnus-summary-reselect-current-group
1284     "\M-g" gnus-summary-rescan-group
1285     "w" gnus-summary-stop-page-breaking
1286     "\C-c\C-r" gnus-summary-caesar-message
1287     "\M-t" gnus-summary-toggle-mime
1288     "f" gnus-summary-followup
1289     "F" gnus-summary-followup-with-original
1290     "C" gnus-summary-cancel-article
1291     "r" gnus-summary-reply
1292     "R" gnus-summary-reply-with-original
1293     "\C-c\C-f" gnus-summary-mail-forward
1294     "o" gnus-summary-save-article
1295     "\C-o" gnus-summary-save-article-mail
1296     "|" gnus-summary-pipe-output
1297     "\M-k" gnus-summary-edit-local-kill
1298     "\M-K" gnus-summary-edit-global-kill
1299     ;; "V" gnus-version
1300     "\C-c\C-d" gnus-summary-describe-group
1301     "q" gnus-summary-exit
1302     "Q" gnus-summary-exit-no-update
1303     "\C-c\C-i" gnus-info-find-node
1304     gnus-mouse-2 gnus-mouse-pick-article
1305     "m" gnus-summary-mail-other-window
1306     "a" gnus-summary-post-news
1307     "x" gnus-summary-limit-to-unread
1308     "s" gnus-summary-isearch-article
1309     "t" gnus-article-toggle-headers
1310     "g" gnus-summary-show-article
1311     "l" gnus-summary-goto-last-article
1312     "v" gnus-summary-preview-mime-message
1313     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1314     "\C-d" gnus-summary-enter-digest-group
1315     "\M-\C-d" gnus-summary-read-document
1316     "\M-\C-e" gnus-summary-edit-parameters
1317     "\M-\C-g" gnus-summary-customize-parameters
1318     "\C-c\C-b" gnus-bug
1319     "*" gnus-cache-enter-article
1320     "\M-*" gnus-cache-remove-article
1321     "\M-&" gnus-summary-universal-argument
1322     "\C-l" gnus-recenter
1323     "I" gnus-summary-increase-score
1324     "L" gnus-summary-lower-score
1325     "\M-i" gnus-symbolic-argument
1326     "h" gnus-summary-select-article-buffer
1327
1328     "V" gnus-summary-score-map
1329     "X" gnus-uu-extract-map
1330     "S" gnus-summary-send-map)
1331
1332   ;; Sort of orthogonal keymap
1333   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1334     "t" gnus-summary-tick-article-forward
1335     "!" gnus-summary-tick-article-forward
1336     "d" gnus-summary-mark-as-read-forward
1337     "r" gnus-summary-mark-as-read-forward
1338     "c" gnus-summary-clear-mark-forward
1339     " " gnus-summary-clear-mark-forward
1340     "e" gnus-summary-mark-as-expirable
1341     "x" gnus-summary-mark-as-expirable
1342     "?" gnus-summary-mark-as-dormant
1343     "b" gnus-summary-set-bookmark
1344     "B" gnus-summary-remove-bookmark
1345     "#" gnus-summary-mark-as-processable
1346     "\M-#" gnus-summary-unmark-as-processable
1347     "S" gnus-summary-limit-include-expunged
1348     "C" gnus-summary-catchup
1349     "H" gnus-summary-catchup-to-here
1350     "\C-c" gnus-summary-catchup-all
1351     "k" gnus-summary-kill-same-subject-and-select
1352     "K" gnus-summary-kill-same-subject
1353     "P" gnus-uu-mark-map)
1354
1355   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1356     "c" gnus-summary-clear-above
1357     "u" gnus-summary-tick-above
1358     "m" gnus-summary-mark-above
1359     "k" gnus-summary-kill-below)
1360
1361   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1362     "/" gnus-summary-limit-to-subject
1363     "n" gnus-summary-limit-to-articles
1364     "w" gnus-summary-pop-limit
1365     "s" gnus-summary-limit-to-subject
1366     "a" gnus-summary-limit-to-author
1367     "u" gnus-summary-limit-to-unread
1368     "m" gnus-summary-limit-to-marks
1369     "M" gnus-summary-limit-exclude-marks
1370     "v" gnus-summary-limit-to-score
1371     "*" gnus-summary-limit-include-cached
1372     "D" gnus-summary-limit-include-dormant
1373     "T" gnus-summary-limit-include-thread
1374     "d" gnus-summary-limit-exclude-dormant
1375     "t" gnus-summary-limit-to-age
1376     "x" gnus-summary-limit-to-extra 
1377     "E" gnus-summary-limit-include-expunged
1378     "c" gnus-summary-limit-exclude-childless-dormant
1379     "C" gnus-summary-limit-mark-excluded-as-read)
1380
1381   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1382     "n" gnus-summary-next-unread-article
1383     "p" gnus-summary-prev-unread-article
1384     "N" gnus-summary-next-article
1385     "P" gnus-summary-prev-article
1386     "\C-n" gnus-summary-next-same-subject
1387     "\C-p" gnus-summary-prev-same-subject
1388     "\M-n" gnus-summary-next-unread-subject
1389     "\M-p" gnus-summary-prev-unread-subject
1390     "f" gnus-summary-first-unread-article
1391     "b" gnus-summary-best-unread-article
1392     "j" gnus-summary-goto-article
1393     "g" gnus-summary-goto-subject
1394     "l" gnus-summary-goto-last-article
1395     "o" gnus-summary-pop-article)
1396
1397   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1398     "k" gnus-summary-kill-thread
1399     "l" gnus-summary-lower-thread
1400     "i" gnus-summary-raise-thread
1401     "T" gnus-summary-toggle-threads
1402     "t" gnus-summary-rethread-current
1403     "^" gnus-summary-reparent-thread
1404     "s" gnus-summary-show-thread
1405     "S" gnus-summary-show-all-threads
1406     "h" gnus-summary-hide-thread
1407     "H" gnus-summary-hide-all-threads
1408     "n" gnus-summary-next-thread
1409     "p" gnus-summary-prev-thread
1410     "u" gnus-summary-up-thread
1411     "o" gnus-summary-top-thread
1412     "d" gnus-summary-down-thread
1413     "#" gnus-uu-mark-thread
1414     "\M-#" gnus-uu-unmark-thread)
1415
1416   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1417     "g" gnus-summary-prepare
1418     "c" gnus-summary-insert-cached-articles)
1419
1420   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1421     "c" gnus-summary-catchup-and-exit
1422     "C" gnus-summary-catchup-all-and-exit
1423     "E" gnus-summary-exit-no-update
1424     "Q" gnus-summary-exit
1425     "Z" gnus-summary-exit
1426     "n" gnus-summary-catchup-and-goto-next-group
1427     "R" gnus-summary-reselect-current-group
1428     "G" gnus-summary-rescan-group
1429     "N" gnus-summary-next-group
1430     "s" gnus-summary-save-newsrc
1431     "P" gnus-summary-prev-group)
1432
1433   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1434     " " gnus-summary-next-page
1435     "n" gnus-summary-next-page
1436     "\177" gnus-summary-prev-page
1437     [delete] gnus-summary-prev-page
1438     "p" gnus-summary-prev-page
1439     "\r" gnus-summary-scroll-up
1440     "\M-\r" gnus-summary-scroll-down
1441     "<" gnus-summary-beginning-of-article
1442     ">" gnus-summary-end-of-article
1443     "b" gnus-summary-beginning-of-article
1444     "e" gnus-summary-end-of-article
1445     "^" gnus-summary-refer-parent-article
1446     "r" gnus-summary-refer-parent-article
1447     "D" gnus-summary-enter-digest-group
1448     "R" gnus-summary-refer-references
1449     "T" gnus-summary-refer-thread
1450     "g" gnus-summary-show-article
1451     "s" gnus-summary-isearch-article
1452     "P" gnus-summary-print-article
1453     "t" gnus-article-babel)
1454
1455   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1456     "b" gnus-article-add-buttons
1457     "B" gnus-article-add-buttons-to-head
1458     "o" gnus-article-treat-overstrike
1459     "e" gnus-article-emphasize
1460     "w" gnus-article-fill-cited-article
1461     "Q" gnus-article-fill-long-lines
1462     "C" gnus-article-capitalize-sentences
1463     "c" gnus-article-remove-cr
1464     "f" gnus-article-display-x-face
1465     "l" gnus-summary-stop-page-breaking
1466     "r" gnus-summary-caesar-message
1467     "t" gnus-article-toggle-headers
1468     "v" gnus-summary-verbose-headers
1469     "m" gnus-summary-toggle-mime
1470     "h" gnus-article-treat-html
1471     "H" gnus-article-strip-headers-in-body
1472     "d" gnus-article-treat-dumbquotes)
1473
1474   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1475     "a" gnus-article-hide
1476     "h" gnus-article-toggle-headers
1477     "b" gnus-article-hide-boring-headers
1478     "s" gnus-article-hide-signature
1479     "c" gnus-article-hide-citation
1480     "C" gnus-article-hide-citation-in-followups
1481     "p" gnus-article-hide-pgp
1482     "B" gnus-article-strip-banner
1483     "P" gnus-article-hide-pem
1484     "\C-c" gnus-article-hide-citation-maybe)
1485
1486   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1487     "a" gnus-article-highlight
1488     "h" gnus-article-highlight-headers
1489     "c" gnus-article-highlight-citation
1490     "s" gnus-article-highlight-signature)
1491
1492   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1493     "z" gnus-article-date-ut
1494     "u" gnus-article-date-ut
1495     "l" gnus-article-date-local
1496     "e" gnus-article-date-lapsed
1497     "o" gnus-article-date-original
1498     "i" gnus-article-date-iso8601
1499     "s" gnus-article-date-user)
1500
1501   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1502     "t" gnus-article-remove-trailing-blank-lines
1503     "l" gnus-article-strip-leading-blank-lines
1504     "m" gnus-article-strip-multiple-blank-lines
1505     "a" gnus-article-strip-blank-lines
1506     "A" gnus-article-strip-all-blank-lines
1507     "s" gnus-article-strip-leading-space
1508     "e" gnus-article-strip-trailing-space)
1509
1510   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1511     "v" gnus-version
1512     "f" gnus-summary-fetch-faq
1513     "d" gnus-summary-describe-group
1514     "h" gnus-summary-describe-briefly
1515     "i" gnus-info-find-node)
1516
1517   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1518     "e" gnus-summary-expire-articles
1519     "\M-\C-e" gnus-summary-expire-articles-now
1520     "\177" gnus-summary-delete-article
1521     [delete] gnus-summary-delete-article
1522     [backspace] gnus-summary-delete-article
1523     "m" gnus-summary-move-article
1524     "r" gnus-summary-respool-article
1525     "w" gnus-summary-edit-article
1526     "c" gnus-summary-copy-article
1527     "B" gnus-summary-crosspost-article
1528     "q" gnus-summary-respool-query
1529     "t" gnus-summary-respool-trace
1530     "i" gnus-summary-import-article
1531     "p" gnus-summary-article-posted-p)
1532
1533   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1534     "o" gnus-summary-save-article
1535     "m" gnus-summary-save-article-mail
1536     "F" gnus-summary-write-article-file
1537     "r" gnus-summary-save-article-rmail
1538     "f" gnus-summary-save-article-file
1539     "b" gnus-summary-save-article-body-file
1540     "h" gnus-summary-save-article-folder
1541     "v" gnus-summary-save-article-vm
1542     "p" gnus-summary-pipe-output
1543     "s" gnus-soup-add-article)
1544
1545   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1546     "b" gnus-summary-display-buttonized
1547     "m" gnus-summary-repair-multipart
1548     "v" gnus-article-view-part
1549     "o" gnus-article-save-part
1550     "c" gnus-article-copy-part
1551     "e" gnus-article-externalize-part
1552     "i" gnus-article-inline-part
1553     "|" gnus-article-pipe-part)
1554   )
1555
1556 (defun gnus-summary-make-menu-bar ()
1557   (gnus-turn-off-edit-menu 'summary)
1558
1559   (unless (boundp 'gnus-summary-misc-menu)
1560
1561     (easy-menu-define
1562      gnus-summary-kill-menu gnus-summary-mode-map ""
1563      (cons
1564       "Score"
1565       (nconc
1566        (list
1567         ["Enter score..." gnus-summary-score-entry t]
1568         ["Customize" gnus-score-customize t])
1569        (gnus-make-score-map 'increase)
1570        (gnus-make-score-map 'lower)
1571        '(("Mark"
1572           ["Kill below" gnus-summary-kill-below t]
1573           ["Mark above" gnus-summary-mark-above t]
1574           ["Tick above" gnus-summary-tick-above t]
1575           ["Clear above" gnus-summary-clear-above t])
1576          ["Current score" gnus-summary-current-score t]
1577          ["Set score" gnus-summary-set-score t]
1578          ["Switch current score file..." gnus-score-change-score-file t]
1579          ["Set mark below..." gnus-score-set-mark-below t]
1580          ["Set expunge below..." gnus-score-set-expunge-below t]
1581          ["Edit current score file" gnus-score-edit-current-scores t]
1582          ["Edit score file" gnus-score-edit-file t]
1583          ["Trace score" gnus-score-find-trace t]
1584          ["Find words" gnus-score-find-favourite-words t]
1585          ["Rescore buffer" gnus-summary-rescore t]
1586          ["Increase score..." gnus-summary-increase-score t]
1587          ["Lower score..." gnus-summary-lower-score t]))))
1588
1589     ;; Define both the Article menu in the summary buffer and the equivalent
1590     ;; Commands menu in the article buffer here for consistency.
1591     (let ((innards
1592            '(("Hide"
1593               ["All" gnus-article-hide t]
1594               ["Headers" gnus-article-toggle-headers t]
1595               ["Signature" gnus-article-hide-signature t]
1596               ["Citation" gnus-article-hide-citation t]
1597               ["PGP" gnus-article-hide-pgp t]
1598               ["Banner" gnus-article-strip-banner t]
1599               ["Boring headers" gnus-article-hide-boring-headers t])
1600              ("Highlight"
1601               ["All" gnus-article-highlight t]
1602               ["Headers" gnus-article-highlight-headers t]
1603               ["Signature" gnus-article-highlight-signature t]
1604               ["Citation" gnus-article-highlight-citation t])
1605              ("Date"
1606               ["Local" gnus-article-date-local t]
1607               ["ISO8601" gnus-article-date-iso8601 t]
1608               ["UT" gnus-article-date-ut t]
1609               ["Original" gnus-article-date-original t]
1610               ["Lapsed" gnus-article-date-lapsed t]
1611               ["User-defined" gnus-article-date-user t])
1612              ("Washing"
1613               ("Remove Blanks"
1614                ["Leading" gnus-article-strip-leading-blank-lines t]
1615                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1616                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1617                ["All of the above" gnus-article-strip-blank-lines t]
1618                ["All" gnus-article-strip-all-blank-lines t]
1619                ["Leading space" gnus-article-strip-leading-space t]
1620                ["Trailing space" gnus-article-strip-trailing-space t])
1621               ["Overstrike" gnus-article-treat-overstrike t]
1622               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1623               ["Emphasis" gnus-article-emphasize t]
1624               ["Word wrap" gnus-article-fill-cited-article t]
1625               ["Fill long lines" gnus-article-fill-long-lines t]
1626               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1627               ["CR" gnus-article-remove-cr t]
1628               ["Show X-Face" gnus-article-display-x-face t]
1629               ["UnHTMLize" gnus-article-treat-html t]
1630               ["Rot 13" gnus-summary-caesar-message t]
1631               ["Unix pipe" gnus-summary-pipe-message t]
1632               ["Add buttons" gnus-article-add-buttons t]
1633               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1634               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1635               ["Toggle MIME" gnus-summary-toggle-mime t]
1636               ["Verbose header" gnus-summary-verbose-headers t]
1637               ["Toggle header" gnus-summary-toggle-header t])
1638              ("Output"
1639               ["Save in default format" gnus-summary-save-article t]
1640               ["Save in file" gnus-summary-save-article-file t]
1641               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1642               ["Save in MH folder" gnus-summary-save-article-folder t]
1643               ["Save in VM folder" gnus-summary-save-article-vm t]
1644               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1645               ["Save body in file" gnus-summary-save-article-body-file t]
1646               ["Pipe through a filter" gnus-summary-pipe-output t]
1647               ["Add to SOUP packet" gnus-soup-add-article t]
1648               ["Print" gnus-summary-print-article t])
1649              ("Backend"
1650               ["Respool article..." gnus-summary-respool-article t]
1651               ["Move article..." gnus-summary-move-article
1652                (gnus-check-backend-function
1653                 'request-move-article gnus-newsgroup-name)]
1654               ["Copy article..." gnus-summary-copy-article t]
1655               ["Crosspost article..." gnus-summary-crosspost-article
1656                (gnus-check-backend-function
1657                 'request-replace-article gnus-newsgroup-name)]
1658               ["Import file..." gnus-summary-import-article t]
1659               ["Check if posted" gnus-summary-article-posted-p t]
1660               ["Edit article" gnus-summary-edit-article
1661                (not (gnus-group-read-only-p))]
1662               ["Delete article" gnus-summary-delete-article
1663                (gnus-check-backend-function
1664                 'request-expire-articles gnus-newsgroup-name)]
1665               ["Query respool" gnus-summary-respool-query t]
1666               ["Trace respool" gnus-summary-respool-trace t]
1667               ["Delete expirable articles" gnus-summary-expire-articles-now
1668                (gnus-check-backend-function
1669                 'request-expire-articles gnus-newsgroup-name)])
1670              ("Extract"
1671               ["Uudecode" gnus-uu-decode-uu t]
1672               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1673               ["Unshar" gnus-uu-decode-unshar t]
1674               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1675               ["Save" gnus-uu-decode-save t]
1676               ["Binhex" gnus-uu-decode-binhex t]
1677               ["Postscript" gnus-uu-decode-postscript t])
1678              ("Cache"
1679               ["Enter article" gnus-cache-enter-article t]
1680               ["Remove article" gnus-cache-remove-article t])
1681              ["Translate" gnus-article-babel t]
1682              ["Select article buffer" gnus-summary-select-article-buffer t]
1683              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1684              ["Isearch article..." gnus-summary-isearch-article t]
1685              ["Beginning of the article" gnus-summary-beginning-of-article t]
1686              ["End of the article" gnus-summary-end-of-article t]
1687              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1688              ["Fetch referenced articles" gnus-summary-refer-references t]
1689              ["Fetch current thread" gnus-summary-refer-thread t]
1690              ["Fetch article with id..." gnus-summary-refer-article t]
1691              ["Redisplay" gnus-summary-show-article t])))
1692       (easy-menu-define
1693        gnus-summary-article-menu gnus-summary-mode-map ""
1694        (cons "Article" innards))
1695
1696       (easy-menu-define
1697        gnus-article-commands-menu gnus-article-mode-map ""
1698        (cons "Commands" innards)))
1699
1700     (easy-menu-define
1701      gnus-summary-thread-menu gnus-summary-mode-map ""
1702      '("Threads"
1703        ["Toggle threading" gnus-summary-toggle-threads t]
1704        ["Hide threads" gnus-summary-hide-all-threads t]
1705        ["Show threads" gnus-summary-show-all-threads t]
1706        ["Hide thread" gnus-summary-hide-thread t]
1707        ["Show thread" gnus-summary-show-thread t]
1708        ["Go to next thread" gnus-summary-next-thread t]
1709        ["Go to previous thread" gnus-summary-prev-thread t]
1710        ["Go down thread" gnus-summary-down-thread t]
1711        ["Go up thread" gnus-summary-up-thread t]
1712        ["Top of thread" gnus-summary-top-thread t]
1713        ["Mark thread as read" gnus-summary-kill-thread t]
1714        ["Lower thread score" gnus-summary-lower-thread t]
1715        ["Raise thread score" gnus-summary-raise-thread t]
1716        ["Rethread current" gnus-summary-rethread-current t]
1717        ))
1718
1719     (easy-menu-define
1720      gnus-summary-post-menu gnus-summary-mode-map ""
1721      '("Post"
1722        ["Post an article" gnus-summary-post-news t]
1723        ["Followup" gnus-summary-followup t]
1724        ["Followup and yank" gnus-summary-followup-with-original t]
1725        ["Supersede article" gnus-summary-supersede-article t]
1726        ["Cancel article" gnus-summary-cancel-article t]
1727        ["Reply" gnus-summary-reply t]
1728        ["Reply and yank" gnus-summary-reply-with-original t]
1729        ["Wide reply" gnus-summary-wide-reply t]
1730        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1731        ["Mail forward" gnus-summary-mail-forward t]
1732        ["Post forward" gnus-summary-post-forward t]
1733        ["Digest and mail" gnus-summary-mail-digest t]
1734        ["Digest and post" gnus-summary-post-digest t]
1735        ["Resend message" gnus-summary-resend-message t]
1736        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1737        ["Send a mail" gnus-summary-mail-other-window t]
1738        ["Uuencode and post" gnus-uu-post-news t]
1739        ["Followup via news" gnus-summary-followup-to-mail t]
1740        ["Followup via news and yank"
1741         gnus-summary-followup-to-mail-with-original t]
1742        ;;("Draft"
1743        ;;["Send" gnus-summary-send-draft t]
1744        ;;["Send bounced" gnus-resend-bounced-mail t])
1745        ))
1746
1747     (easy-menu-define
1748      gnus-summary-misc-menu gnus-summary-mode-map ""
1749      '("Misc"
1750        ("Mark Read"
1751         ["Mark as read" gnus-summary-mark-as-read-forward t]
1752         ["Mark same subject and select"
1753          gnus-summary-kill-same-subject-and-select t]
1754         ["Mark same subject" gnus-summary-kill-same-subject t]
1755         ["Catchup" gnus-summary-catchup t]
1756         ["Catchup all" gnus-summary-catchup-all t]
1757         ["Catchup to here" gnus-summary-catchup-to-here t]
1758         ["Catchup region" gnus-summary-mark-region-as-read t]
1759         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1760        ("Mark Various"
1761         ["Tick" gnus-summary-tick-article-forward t]
1762         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1763         ["Remove marks" gnus-summary-clear-mark-forward t]
1764         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1765         ["Set bookmark" gnus-summary-set-bookmark t]
1766         ["Remove bookmark" gnus-summary-remove-bookmark t])
1767        ("Mark Limit"
1768         ["Marks..." gnus-summary-limit-to-marks t]
1769         ["Subject..." gnus-summary-limit-to-subject t]
1770         ["Author..." gnus-summary-limit-to-author t]
1771         ["Age..." gnus-summary-limit-to-age t]
1772         ["Extra..." gnus-summary-limit-to-extra t]
1773         ["Score" gnus-summary-limit-to-score t]
1774         ["Unread" gnus-summary-limit-to-unread t]
1775         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1776         ["Articles" gnus-summary-limit-to-articles t]
1777         ["Pop limit" gnus-summary-pop-limit t]
1778         ["Show dormant" gnus-summary-limit-include-dormant t]
1779         ["Hide childless dormant"
1780          gnus-summary-limit-exclude-childless-dormant t]
1781         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1782         ["Hide marked" gnus-summary-limit-exclude-marks t]
1783         ["Show expunged" gnus-summary-show-all-expunged t])
1784        ("Process Mark"
1785         ["Set mark" gnus-summary-mark-as-processable t]
1786         ["Remove mark" gnus-summary-unmark-as-processable t]
1787         ["Remove all marks" gnus-summary-unmark-all-processable t]
1788         ["Mark above" gnus-uu-mark-over t]
1789         ["Mark series" gnus-uu-mark-series t]
1790         ["Mark region" gnus-uu-mark-region t]
1791         ["Unmark region" gnus-uu-unmark-region t]
1792         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1793         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1794         ["Mark all" gnus-uu-mark-all t]
1795         ["Mark buffer" gnus-uu-mark-buffer t]
1796         ["Mark sparse" gnus-uu-mark-sparse t]
1797         ["Mark thread" gnus-uu-mark-thread t]
1798         ["Unmark thread" gnus-uu-unmark-thread t]
1799         ("Process Mark Sets"
1800          ["Kill" gnus-summary-kill-process-mark t]
1801          ["Yank" gnus-summary-yank-process-mark
1802           gnus-newsgroup-process-stack]
1803          ["Save" gnus-summary-save-process-mark t]))
1804        ("Scroll article"
1805         ["Page forward" gnus-summary-next-page t]
1806         ["Page backward" gnus-summary-prev-page t]
1807         ["Line forward" gnus-summary-scroll-up t])
1808        ("Move"
1809         ["Next unread article" gnus-summary-next-unread-article t]
1810         ["Previous unread article" gnus-summary-prev-unread-article t]
1811         ["Next article" gnus-summary-next-article t]
1812         ["Previous article" gnus-summary-prev-article t]
1813         ["Next unread subject" gnus-summary-next-unread-subject t]
1814         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1815         ["Next article same subject" gnus-summary-next-same-subject t]
1816         ["Previous article same subject" gnus-summary-prev-same-subject t]
1817         ["First unread article" gnus-summary-first-unread-article t]
1818         ["Best unread article" gnus-summary-best-unread-article t]
1819         ["Go to subject number..." gnus-summary-goto-subject t]
1820         ["Go to article number..." gnus-summary-goto-article t]
1821         ["Go to the last article" gnus-summary-goto-last-article t]
1822         ["Pop article off history" gnus-summary-pop-article t])
1823        ("Sort"
1824         ["Sort by number" gnus-summary-sort-by-number t]
1825         ["Sort by author" gnus-summary-sort-by-author t]
1826         ["Sort by subject" gnus-summary-sort-by-subject t]
1827         ["Sort by date" gnus-summary-sort-by-date t]
1828         ["Sort by score" gnus-summary-sort-by-score t]
1829         ["Sort by lines" gnus-summary-sort-by-lines t]
1830         ["Sort by characters" gnus-summary-sort-by-chars t])
1831        ("Help"
1832         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1833         ["Describe group" gnus-summary-describe-group t]
1834         ["Read manual" gnus-info-find-node t])
1835        ("Modes"
1836         ["Pick and read" gnus-pick-mode t]
1837         ["Binary" gnus-binary-mode t])
1838        ("Regeneration"
1839         ["Regenerate" gnus-summary-prepare t]
1840         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1841         ["Toggle threading" gnus-summary-toggle-threads t])
1842        ["Filter articles..." gnus-summary-execute-command t]
1843        ["Run command on subjects..." gnus-summary-universal-argument t]
1844        ["Search articles forward..." gnus-summary-search-article-forward t]
1845        ["Search articles backward..." gnus-summary-search-article-backward t]
1846        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1847        ["Expand window" gnus-summary-expand-window t]
1848        ["Expire expirable articles" gnus-summary-expire-articles
1849         (gnus-check-backend-function
1850          'request-expire-articles gnus-newsgroup-name)]
1851        ["Edit local kill file" gnus-summary-edit-local-kill t]
1852        ["Edit main kill file" gnus-summary-edit-global-kill t]
1853        ["Edit group parameters" gnus-summary-edit-parameters t]
1854        ["Customize group parameters" gnus-summary-customize-parameters t]
1855        ["Send a bug report" gnus-bug t]
1856        ("Exit"
1857         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1858         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1859         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1860         ["Exit group" gnus-summary-exit t]
1861         ["Exit group without updating" gnus-summary-exit-no-update t]
1862         ["Exit and goto next group" gnus-summary-next-group t]
1863         ["Exit and goto prev group" gnus-summary-prev-group t]
1864         ["Reselect group" gnus-summary-reselect-current-group t]
1865         ["Rescan group" gnus-summary-rescan-group t]
1866         ["Update dribble" gnus-summary-save-newsrc t])))
1867
1868     (gnus-run-hooks 'gnus-summary-menu-hook)))
1869
1870 (defun gnus-score-set-default (var value)
1871   "A version of set that updates the GNU Emacs menu-bar."
1872   (set var value)
1873   ;; It is the message that forces the active status to be updated.
1874   (message ""))
1875
1876 (defun gnus-make-score-map (type)
1877   "Make a summary score map of type TYPE."
1878   (if t
1879       nil
1880     (let ((headers '(("author" "from" string)
1881                      ("subject" "subject" string)
1882                      ("article body" "body" string)
1883                      ("article head" "head" string)
1884                      ("xref" "xref" string)
1885                      ("extra header" "extra" string)
1886                      ("lines" "lines" number)
1887                      ("followups to author" "followup" string)))
1888           (types '((number ("less than" <)
1889                            ("greater than" >)
1890                            ("equal" =))
1891                    (string ("substring" s)
1892                            ("exact string" e)
1893                            ("fuzzy string" f)
1894                            ("regexp" r))))
1895           (perms '(("temporary" (current-time-string))
1896                    ("permanent" nil)
1897                    ("immediate" now)))
1898           header)
1899       (list
1900        (apply
1901         'nconc
1902         (list
1903          (if (eq type 'lower)
1904              "Lower score"
1905            "Increase score"))
1906         (let (outh)
1907           (while headers
1908             (setq header (car headers))
1909             (setq outh
1910                   (cons
1911                    (apply
1912                     'nconc
1913                     (list (car header))
1914                     (let ((ts (cdr (assoc (nth 2 header) types)))
1915                           outt)
1916                       (while ts
1917                         (setq outt
1918                               (cons
1919                                (apply
1920                                 'nconc
1921                                 (list (caar ts))
1922                                 (let ((ps perms)
1923                                       outp)
1924                                   (while ps
1925                                     (setq outp
1926                                           (cons
1927                                            (vector
1928                                             (caar ps)
1929                                             (list
1930                                              'gnus-summary-score-entry
1931                                              (nth 1 header)
1932                                              (if (or (string= (nth 1 header)
1933                                                               "head")
1934                                                      (string= (nth 1 header)
1935                                                               "body"))
1936                                                  ""
1937                                                (list 'gnus-summary-header
1938                                                      (nth 1 header)))
1939                                              (list 'quote (nth 1 (car ts)))
1940                                              (list 'gnus-score-default nil)
1941                                              (nth 1 (car ps))
1942                                              t)
1943                                             t)
1944                                            outp))
1945                                     (setq ps (cdr ps)))
1946                                   (list (nreverse outp))))
1947                                outt))
1948                         (setq ts (cdr ts)))
1949                       (list (nreverse outt))))
1950                    outh))
1951             (setq headers (cdr headers)))
1952           (list (nreverse outh))))))))
1953
1954 \f
1955
1956 (defun gnus-summary-mode (&optional group)
1957   "Major mode for reading articles.
1958
1959 All normal editing commands are switched off.
1960 \\<gnus-summary-mode-map>
1961 Each line in this buffer represents one article.  To read an
1962 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1963 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1964 respectively.
1965
1966 You can also post articles and send mail from this buffer.  To
1967 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1968 of an article, type `\\[gnus-summary-reply]'.
1969
1970 There are approx. one gazillion commands you can execute in this
1971 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1972
1973 The following commands are available:
1974
1975 \\{gnus-summary-mode-map}"
1976   (interactive)
1977   (when (gnus-visual-p 'summary-menu 'menu)
1978     (gnus-summary-make-menu-bar))
1979   (kill-all-local-variables)
1980   (gnus-summary-make-local-variables)
1981   (gnus-make-thread-indent-array)
1982   (gnus-simplify-mode-line)
1983   (setq major-mode 'gnus-summary-mode)
1984   (setq mode-name "Summary")
1985   (make-local-variable 'minor-mode-alist)
1986   (use-local-map gnus-summary-mode-map)
1987   (buffer-disable-undo)
1988   (setq buffer-read-only t)             ;Disable modification
1989   (setq truncate-lines t)
1990   (setq selective-display t)
1991   (setq selective-display-ellipses t)   ;Display `...'
1992   (gnus-summary-set-display-table)
1993   (gnus-set-default-directory)
1994   (setq gnus-newsgroup-name group)
1995   (make-local-variable 'gnus-summary-line-format)
1996   (make-local-variable 'gnus-summary-line-format-spec)
1997   (make-local-variable 'gnus-summary-dummy-line-format)
1998   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1999   (make-local-variable 'gnus-summary-mark-positions)
2000   (make-local-hook 'pre-command-hook)
2001   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2002   (gnus-run-hooks 'gnus-summary-mode-hook)
2003   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2004   (gnus-update-summary-mark-positions))
2005
2006 (defun gnus-summary-make-local-variables ()
2007   "Make all the local summary buffer variables."
2008   (let (global)
2009     (dolist (local gnus-summary-local-variables)
2010       (if (consp local)
2011           (progn
2012             (if (eq (cdr local) 'global)
2013                 ;; Copy the global value of the variable.
2014                 (setq global (symbol-value (car local)))
2015               ;; Use the value from the list.
2016               (setq global (eval (cdr local))))
2017             (set (make-local-variable (car local)) global))
2018         ;; Simple nil-valued local variable.
2019         (set (make-local-variable local) nil)))))
2020
2021 (defun gnus-summary-clear-local-variables ()
2022   (let ((locals gnus-summary-local-variables))
2023     (while locals
2024       (if (consp (car locals))
2025           (and (vectorp (caar locals))
2026                (set (caar locals) nil))
2027         (and (vectorp (car locals))
2028              (set (car locals) nil)))
2029       (setq locals (cdr locals)))))
2030
2031 ;; Summary data functions.
2032
2033 (defmacro gnus-data-number (data)
2034   `(car ,data))
2035
2036 (defmacro gnus-data-set-number (data number)
2037   `(setcar ,data ,number))
2038
2039 (defmacro gnus-data-mark (data)
2040   `(nth 1 ,data))
2041
2042 (defmacro gnus-data-set-mark (data mark)
2043   `(setcar (nthcdr 1 ,data) ,mark))
2044
2045 (defmacro gnus-data-pos (data)
2046   `(nth 2 ,data))
2047
2048 (defmacro gnus-data-set-pos (data pos)
2049   `(setcar (nthcdr 2 ,data) ,pos))
2050
2051 (defmacro gnus-data-header (data)
2052   `(nth 3 ,data))
2053
2054 (defmacro gnus-data-set-header (data header)
2055   `(setf (nth 3 ,data) ,header))
2056
2057 (defmacro gnus-data-level (data)
2058   `(nth 4 ,data))
2059
2060 (defmacro gnus-data-unread-p (data)
2061   `(= (nth 1 ,data) gnus-unread-mark))
2062
2063 (defmacro gnus-data-read-p (data)
2064   `(/= (nth 1 ,data) gnus-unread-mark))
2065
2066 (defmacro gnus-data-pseudo-p (data)
2067   `(consp (nth 3 ,data)))
2068
2069 (defmacro gnus-data-find (number)
2070   `(assq ,number gnus-newsgroup-data))
2071
2072 (defmacro gnus-data-find-list (number &optional data)
2073   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2074      (memq (assq ,number bdata)
2075            bdata)))
2076
2077 (defmacro gnus-data-make (number mark pos header level)
2078   `(list ,number ,mark ,pos ,header ,level))
2079
2080 (defun gnus-data-enter (after-article number mark pos header level offset)
2081   (let ((data (gnus-data-find-list after-article)))
2082     (unless data
2083       (error "No such article: %d" after-article))
2084     (setcdr data (cons (gnus-data-make number mark pos header level)
2085                        (cdr data)))
2086     (setq gnus-newsgroup-data-reverse nil)
2087     (gnus-data-update-list (cddr data) offset)))
2088
2089 (defun gnus-data-enter-list (after-article list &optional offset)
2090   (when list
2091     (let ((data (and after-article (gnus-data-find-list after-article)))
2092           (ilist list))
2093       (if (not (or data
2094                    after-article))
2095           (let ((odata gnus-newsgroup-data))
2096             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2097             (when offset
2098               (gnus-data-update-list odata offset)))
2099         ;; Find the last element in the list to be spliced into the main
2100         ;; list.
2101         (while (cdr list)
2102           (setq list (cdr list)))
2103         (if (not data)
2104             (progn
2105               (setcdr list gnus-newsgroup-data)
2106               (setq gnus-newsgroup-data ilist)
2107               (when offset
2108                 (gnus-data-update-list (cdr list) offset)))
2109           (setcdr list (cdr data))
2110           (setcdr data ilist)
2111           (when offset
2112             (gnus-data-update-list (cdr list) offset))))
2113       (setq gnus-newsgroup-data-reverse nil))))
2114
2115 (defun gnus-data-remove (article &optional offset)
2116   (let ((data gnus-newsgroup-data))
2117     (if (= (gnus-data-number (car data)) article)
2118         (progn
2119           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2120                 gnus-newsgroup-data-reverse nil)
2121           (when offset
2122             (gnus-data-update-list gnus-newsgroup-data offset)))
2123       (while (cdr data)
2124         (when (= (gnus-data-number (cadr data)) article)
2125           (setcdr data (cddr data))
2126           (when offset
2127             (gnus-data-update-list (cdr data) offset))
2128           (setq data nil
2129                 gnus-newsgroup-data-reverse nil))
2130         (setq data (cdr data))))))
2131
2132 (defmacro gnus-data-list (backward)
2133   `(if ,backward
2134        (or gnus-newsgroup-data-reverse
2135            (setq gnus-newsgroup-data-reverse
2136                  (reverse gnus-newsgroup-data)))
2137      gnus-newsgroup-data))
2138
2139 (defun gnus-data-update-list (data offset)
2140   "Add OFFSET to the POS of all data entries in DATA."
2141   (setq gnus-newsgroup-data-reverse nil)
2142   (while data
2143     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2144     (setq data (cdr data))))
2145
2146 (defun gnus-summary-article-pseudo-p (article)
2147   "Say whether this article is a pseudo article or not."
2148   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2149
2150 (defmacro gnus-summary-article-sparse-p (article)
2151   "Say whether this article is a sparse article or not."
2152   `(memq ,article gnus-newsgroup-sparse))
2153
2154 (defmacro gnus-summary-article-ancient-p (article)
2155   "Say whether this article is a sparse article or not."
2156   `(memq ,article gnus-newsgroup-ancient))
2157
2158 (defun gnus-article-parent-p (number)
2159   "Say whether this article is a parent or not."
2160   (let ((data (gnus-data-find-list number)))
2161     (and (cdr data)                     ; There has to be an article after...
2162          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2163             (gnus-data-level (nth 1 data))))))
2164
2165 (defun gnus-article-children (number)
2166   "Return a list of all children to NUMBER."
2167   (let* ((data (gnus-data-find-list number))
2168          (level (gnus-data-level (car data)))
2169          children)
2170     (setq data (cdr data))
2171     (while (and data
2172                 (= (gnus-data-level (car data)) (1+ level)))
2173       (push (gnus-data-number (car data)) children)
2174       (setq data (cdr data)))
2175     children))
2176
2177 (defmacro gnus-summary-skip-intangible ()
2178   "If the current article is intangible, then jump to a different article."
2179   '(let ((to (get-text-property (point) 'gnus-intangible)))
2180      (and to (gnus-summary-goto-subject to))))
2181
2182 (defmacro gnus-summary-article-intangible-p ()
2183   "Say whether this article is intangible or not."
2184   '(get-text-property (point) 'gnus-intangible))
2185
2186 (defun gnus-article-read-p (article)
2187   "Say whether ARTICLE is read or not."
2188   (not (or (memq article gnus-newsgroup-marked)
2189            (memq article gnus-newsgroup-unreads)
2190            (memq article gnus-newsgroup-unselected)
2191            (memq article gnus-newsgroup-dormant))))
2192
2193 ;; Some summary mode macros.
2194
2195 (defmacro gnus-summary-article-number ()
2196   "The article number of the article on the current line.
2197 If there isn's an article number here, then we return the current
2198 article number."
2199   '(progn
2200      (gnus-summary-skip-intangible)
2201      (or (get-text-property (point) 'gnus-number)
2202          (gnus-summary-last-subject))))
2203
2204 (defmacro gnus-summary-article-header (&optional number)
2205   "Return the header of article NUMBER."
2206   `(gnus-data-header (gnus-data-find
2207                       ,(or number '(gnus-summary-article-number)))))
2208
2209 (defmacro gnus-summary-thread-level (&optional number)
2210   "Return the level of thread that starts with article NUMBER."
2211   `(if (and (eq gnus-summary-make-false-root 'dummy)
2212             (get-text-property (point) 'gnus-intangible))
2213        0
2214      (gnus-data-level (gnus-data-find
2215                        ,(or number '(gnus-summary-article-number))))))
2216
2217 (defmacro gnus-summary-article-mark (&optional number)
2218   "Return the mark of article NUMBER."
2219   `(gnus-data-mark (gnus-data-find
2220                     ,(or number '(gnus-summary-article-number)))))
2221
2222 (defmacro gnus-summary-article-pos (&optional number)
2223   "Return the position of the line of article NUMBER."
2224   `(gnus-data-pos (gnus-data-find
2225                    ,(or number '(gnus-summary-article-number)))))
2226
2227 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2228 (defmacro gnus-summary-article-subject (&optional number)
2229   "Return current subject string or nil if nothing."
2230   `(let ((headers
2231           ,(if number
2232                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2233              '(gnus-data-header (assq (gnus-summary-article-number)
2234                                       gnus-newsgroup-data)))))
2235      (and headers
2236           (vectorp headers)
2237           (mail-header-subject headers))))
2238
2239 (defmacro gnus-summary-article-score (&optional number)
2240   "Return current article score."
2241   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2242                   gnus-newsgroup-scored))
2243        gnus-summary-default-score 0))
2244
2245 (defun gnus-summary-article-children (&optional number)
2246   "Return a list of article numbers that are children of article NUMBER."
2247   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2248          (level (gnus-data-level (car data)))
2249          l children)
2250     (while (and (setq data (cdr data))
2251                 (> (setq l (gnus-data-level (car data))) level))
2252       (and (= (1+ level) l)
2253            (push (gnus-data-number (car data))
2254                  children)))
2255     (nreverse children)))
2256
2257 (defun gnus-summary-article-parent (&optional number)
2258   "Return the article number of the parent of article NUMBER."
2259   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2260                                     (gnus-data-list t)))
2261          (level (gnus-data-level (car data))))
2262     (if (zerop level)
2263         ()                              ; This is a root.
2264       ;; We search until we find an article with a level less than
2265       ;; this one.  That function has to be the parent.
2266       (while (and (setq data (cdr data))
2267                   (not (< (gnus-data-level (car data)) level))))
2268       (and data (gnus-data-number (car data))))))
2269
2270 (defun gnus-unread-mark-p (mark)
2271   "Say whether MARK is the unread mark."
2272   (= mark gnus-unread-mark))
2273
2274 (defun gnus-read-mark-p (mark)
2275   "Say whether MARK is one of the marks that mark as read.
2276 This is all marks except unread, ticked, dormant, and expirable."
2277   (not (or (= mark gnus-unread-mark)
2278            (= mark gnus-ticked-mark)
2279            (= mark gnus-dormant-mark)
2280            (= mark gnus-expirable-mark))))
2281
2282 (defmacro gnus-article-mark (number)
2283   "Return the MARK of article NUMBER.
2284 This macro should only be used when computing the mark the \"first\"
2285 time; i.e., when generating the summary lines.  After that,
2286 `gnus-summary-article-mark' should be used to examine the
2287 marks of articles."
2288   `(cond
2289     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2290     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2291     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2292     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2293     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2294     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2295     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2296     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2297            gnus-ancient-mark))))
2298
2299 ;; Saving hidden threads.
2300
2301 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2302 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2303
2304 (defmacro gnus-save-hidden-threads (&rest forms)
2305   "Save hidden threads, eval FORMS, and restore the hidden threads."
2306   (let ((config (make-symbol "config")))
2307     `(let ((,config (gnus-hidden-threads-configuration)))
2308        (unwind-protect
2309            (save-excursion
2310              ,@forms)
2311          (gnus-restore-hidden-threads-configuration ,config)))))
2312
2313 (defun gnus-data-compute-positions ()
2314   "Compute the positions of all articles."
2315   (setq gnus-newsgroup-data-reverse nil)
2316   (let ((data gnus-newsgroup-data))
2317     (save-excursion
2318       (gnus-save-hidden-threads
2319         (gnus-summary-show-all-threads)
2320         (goto-char (point-min))
2321         (while data
2322           (while (get-text-property (point) 'gnus-intangible)
2323             (forward-line 1))
2324           (gnus-data-set-pos (car data) (+ (point) 3))
2325           (setq data (cdr data))
2326           (forward-line 1))))))
2327
2328 (defun gnus-hidden-threads-configuration ()
2329   "Return the current hidden threads configuration."
2330   (save-excursion
2331     (let (config)
2332       (goto-char (point-min))
2333       (while (search-forward "\r" nil t)
2334         (push (1- (point)) config))
2335       config)))
2336
2337 (defun gnus-restore-hidden-threads-configuration (config)
2338   "Restore hidden threads configuration from CONFIG."
2339   (let (point buffer-read-only)
2340     (while (setq point (pop config))
2341       (when (and (< point (point-max))
2342                  (goto-char point)
2343                  (eq (char-after) ?\n))
2344         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2345
2346 ;; Various summary mode internalish functions.
2347
2348 (defun gnus-mouse-pick-article (e)
2349   (interactive "e")
2350   (mouse-set-point e)
2351   (gnus-summary-next-page nil t))
2352
2353 (defun gnus-summary-set-display-table ()
2354   ;; Change the display table.  Odd characters have a tendency to mess
2355   ;; up nicely formatted displays - we make all possible glyphs
2356   ;; display only a single character.
2357
2358   ;; We start from the standard display table, if any.
2359   (let ((table (or (copy-sequence standard-display-table)
2360                    (make-display-table)))
2361         (i 32))
2362     ;; Nix out all the control chars...
2363     (while (>= (setq i (1- i)) 0)
2364       (aset table i [??]))
2365     ;; ... but not newline and cr, of course.  (cr is necessary for the
2366     ;; selective display).
2367     (aset table ?\n nil)
2368     (aset table ?\r nil)
2369     ;; We keep TAB as well.
2370     (aset table ?\t nil)
2371     ;; We nix out any glyphs over 126 that are not set already.
2372     (let ((i 256))
2373       (while (>= (setq i (1- i)) 127)
2374         ;; Only modify if the entry is nil.
2375         (unless (aref table i)
2376           (aset table i [??]))))
2377     (setq buffer-display-table table)))
2378
2379 (defun gnus-summary-setup-buffer (group)
2380   "Initialize summary buffer."
2381   (let ((buffer (concat "*Summary " group "*")))
2382     (if (get-buffer buffer)
2383         (progn
2384           (set-buffer buffer)
2385           (setq gnus-summary-buffer (current-buffer))
2386           (not gnus-newsgroup-prepared))
2387       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2388       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2389       (gnus-summary-mode group)
2390       (when gnus-carpal
2391         (gnus-carpal-setup-buffer 'summary))
2392       (unless gnus-single-article-buffer
2393         (make-local-variable 'gnus-article-buffer)
2394         (make-local-variable 'gnus-article-current)
2395         (make-local-variable 'gnus-original-article-buffer))
2396       (setq gnus-newsgroup-name group)
2397       t)))
2398
2399 (defun gnus-set-global-variables ()
2400   ;; Set the global equivalents of the summary buffer-local variables
2401   ;; to the latest values they had.  These reflect the summary buffer
2402   ;; that was in action when the last article was fetched.
2403   (when (eq major-mode 'gnus-summary-mode)
2404     (setq gnus-summary-buffer (current-buffer))
2405     (let ((name gnus-newsgroup-name)
2406           (marked gnus-newsgroup-marked)
2407           (unread gnus-newsgroup-unreads)
2408           (headers gnus-current-headers)
2409           (data gnus-newsgroup-data)
2410           (summary gnus-summary-buffer)
2411           (article-buffer gnus-article-buffer)
2412           (original gnus-original-article-buffer)
2413           (gac gnus-article-current)
2414           (reffed gnus-reffed-article-number)
2415           (score-file gnus-current-score-file)
2416           (default-charset gnus-newsgroup-charset))
2417       (save-excursion
2418         (set-buffer gnus-group-buffer)
2419         (setq gnus-newsgroup-name name
2420               gnus-newsgroup-marked marked
2421               gnus-newsgroup-unreads unread
2422               gnus-current-headers headers
2423               gnus-newsgroup-data data
2424               gnus-article-current gac
2425               gnus-summary-buffer summary
2426               gnus-article-buffer article-buffer
2427               gnus-original-article-buffer original
2428               gnus-reffed-article-number reffed
2429               gnus-current-score-file score-file
2430               gnus-newsgroup-charset default-charset)
2431         ;; The article buffer also has local variables.
2432         (when (gnus-buffer-live-p gnus-article-buffer)
2433           (set-buffer gnus-article-buffer)
2434           (setq gnus-summary-buffer summary))))))
2435
2436 (defun gnus-summary-article-unread-p (article)
2437   "Say whether ARTICLE is unread or not."
2438   (memq article gnus-newsgroup-unreads))
2439
2440 (defun gnus-summary-first-article-p (&optional article)
2441   "Return whether ARTICLE is the first article in the buffer."
2442   (if (not (setq article (or article (gnus-summary-article-number))))
2443       nil
2444     (eq article (caar gnus-newsgroup-data))))
2445
2446 (defun gnus-summary-last-article-p (&optional article)
2447   "Return whether ARTICLE is the last article in the buffer."
2448   (if (not (setq article (or article (gnus-summary-article-number))))
2449       ;; All non-existent numbers are the last article.  :-)
2450       t
2451     (not (cdr (gnus-data-find-list article)))))
2452
2453 (defun gnus-make-thread-indent-array ()
2454   (let ((n 200))
2455     (unless (and gnus-thread-indent-array
2456                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2457       (setq gnus-thread-indent-array (make-vector 201 "")
2458             gnus-thread-indent-array-level gnus-thread-indent-level)
2459       (while (>= n 0)
2460         (aset gnus-thread-indent-array n
2461               (make-string (* n gnus-thread-indent-level) ? ))
2462         (setq n (1- n))))))
2463
2464 (defun gnus-update-summary-mark-positions ()
2465   "Compute where the summary marks are to go."
2466   (save-excursion
2467     (when (gnus-buffer-exists-p gnus-summary-buffer)
2468       (set-buffer gnus-summary-buffer))
2469     (let ((gnus-replied-mark 129)
2470           (gnus-score-below-mark 130)
2471           (gnus-score-over-mark 130)
2472           (gnus-download-mark 131)
2473           (spec gnus-summary-line-format-spec)
2474           gnus-visual pos)
2475       (save-excursion
2476         (gnus-set-work-buffer)
2477         (let ((gnus-summary-line-format-spec spec)
2478               (gnus-newsgroup-downloadable '((0 . t))))
2479           (gnus-summary-insert-line
2480            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2481            0 nil 128 t nil "" nil 1)
2482           (goto-char (point-min))
2483           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2484                                              (- (point) 2)))))
2485           (goto-char (point-min))
2486           (push (cons 'replied (and (search-forward "\201" nil t)
2487                                     (- (point) 2)))
2488                 pos)
2489           (goto-char (point-min))
2490           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2491                 pos)
2492           (goto-char (point-min))
2493           (push (cons 'download
2494                       (and (search-forward "\203" nil t) (- (point) 2)))
2495                 pos)))
2496       (setq gnus-summary-mark-positions pos))))
2497
2498 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2499   "Insert a dummy root in the summary buffer."
2500   (beginning-of-line)
2501   (gnus-add-text-properties
2502    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2503    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2504
2505 (defun gnus-summary-from-or-to-or-newsgroups (header)
2506   (let ((to (cdr (assq 'To (mail-header-extra header))))
2507         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2508         (mail-parse-charset gnus-newsgroup-charset)
2509         (mail-parse-ignored-charsets 
2510          (save-excursion (set-buffer gnus-summary-buffer)
2511                          gnus-newsgroup-ignored-charsets)))
2512     (cond
2513      ((and to
2514            gnus-ignored-from-addresses
2515            (string-match gnus-ignored-from-addresses
2516                          (mail-header-from header)))
2517       (concat "-> "
2518               (or (car (funcall gnus-extract-address-components
2519                                 (funcall
2520                                  gnus-decode-encoded-word-function to)))
2521                   (funcall gnus-decode-encoded-word-function to))))
2522      ((and newsgroups
2523            gnus-ignored-from-addresses
2524            (string-match gnus-ignored-from-addresses
2525                          (mail-header-from header)))
2526       (concat "=> " newsgroups))
2527      (t
2528       (or (car (funcall gnus-extract-address-components
2529                         (mail-header-from header)))
2530           (mail-header-from header))))))
2531
2532 (defun gnus-summary-insert-line (gnus-tmp-header
2533                                  gnus-tmp-level gnus-tmp-current
2534                                  gnus-tmp-unread gnus-tmp-replied
2535                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2536                                  &optional gnus-tmp-dummy gnus-tmp-score
2537                                  gnus-tmp-process)
2538   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2539          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2540          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2541          (gnus-tmp-score-char
2542           (if (or (null gnus-summary-default-score)
2543                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2544                       gnus-summary-zcore-fuzz))
2545               ? ;Whitespace
2546             (if (< gnus-tmp-score gnus-summary-default-score)
2547                 gnus-score-below-mark gnus-score-over-mark)))
2548          (gnus-tmp-replied
2549           (cond (gnus-tmp-process gnus-process-mark)
2550                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2551                  gnus-cached-mark)
2552                 (gnus-tmp-replied gnus-replied-mark)
2553                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2554                  gnus-saved-mark)
2555                 (t gnus-unread-mark)))
2556          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2557          (gnus-tmp-name
2558           (cond
2559            ((string-match "<[^>]+> *$" gnus-tmp-from)
2560             (let ((beg (match-beginning 0)))
2561               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2562                        (substring gnus-tmp-from (1+ (match-beginning 0))
2563                                   (1- (match-end 0))))
2564                   (substring gnus-tmp-from 0 beg))))
2565            ((string-match "(.+)" gnus-tmp-from)
2566             (substring gnus-tmp-from
2567                        (1+ (match-beginning 0)) (1- (match-end 0))))
2568            (t gnus-tmp-from)))
2569          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2570          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2571          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2572          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2573          (buffer-read-only nil))
2574     (when (string= gnus-tmp-name "")
2575       (setq gnus-tmp-name gnus-tmp-from))
2576     (unless (numberp gnus-tmp-lines)
2577       (setq gnus-tmp-lines 0))
2578     (gnus-put-text-property-excluding-characters-with-faces
2579      (point)
2580      (progn (eval gnus-summary-line-format-spec) (point))
2581      'gnus-number gnus-tmp-number)
2582     (when (gnus-visual-p 'summary-highlight 'highlight)
2583       (forward-line -1)
2584       (gnus-run-hooks 'gnus-summary-update-hook)
2585       (forward-line 1))))
2586
2587 (defun gnus-summary-update-line (&optional dont-update)
2588   ;; Update summary line after change.
2589   (when (and gnus-summary-default-score
2590              (not gnus-summary-inhibit-highlight))
2591     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2592            (article (gnus-summary-article-number))
2593            (score (gnus-summary-article-score article)))
2594       (unless dont-update
2595         (if (and gnus-summary-mark-below
2596                  (< (gnus-summary-article-score)
2597                     gnus-summary-mark-below))
2598             ;; This article has a low score, so we mark it as read.
2599             (when (memq article gnus-newsgroup-unreads)
2600               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2601           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2602             ;; This article was previously marked as read on account
2603             ;; of a low score, but now it has risen, so we mark it as
2604             ;; unread.
2605             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2606         (gnus-summary-update-mark
2607          (if (or (null gnus-summary-default-score)
2608                  (<= (abs (- score gnus-summary-default-score))
2609                      gnus-summary-zcore-fuzz))
2610              ? ;Whitespace
2611            (if (< score gnus-summary-default-score)
2612                gnus-score-below-mark gnus-score-over-mark))
2613          'score))
2614       ;; Do visual highlighting.
2615       (when (gnus-visual-p 'summary-highlight 'highlight)
2616         (gnus-run-hooks 'gnus-summary-update-hook)))))
2617
2618 (defvar gnus-tmp-new-adopts nil)
2619
2620 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2621   "Return the number of articles in THREAD.
2622 This may be 0 in some cases -- if none of the articles in
2623 the thread are to be displayed."
2624   (let* ((number
2625           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2626           (cond
2627            ((not (listp thread))
2628             1)
2629            ((and (consp thread) (cdr thread))
2630             (apply
2631              '+ 1 (mapcar
2632                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2633            ((null thread)
2634             1)
2635            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2636             1)
2637            (t 0))))
2638     (when (and level (zerop level) gnus-tmp-new-adopts)
2639       (incf number
2640             (apply '+ (mapcar
2641                        'gnus-summary-number-of-articles-in-thread
2642                        gnus-tmp-new-adopts))))
2643     (if char
2644         (if (> number 1) gnus-not-empty-thread-mark
2645           gnus-empty-thread-mark)
2646       number)))
2647
2648 (defun gnus-summary-set-local-parameters (group)
2649   "Go through the local params of GROUP and set all variable specs in that list."
2650   (let ((params (gnus-group-find-parameter group))
2651         elem)
2652     (while params
2653       (setq elem (car params)
2654             params (cdr params))
2655       (and (consp elem)                 ; Has to be a cons.
2656            (consp (cdr elem))           ; The cdr has to be a list.
2657            (symbolp (car elem))         ; Has to be a symbol in there.
2658            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2659            (ignore-errors               ; So we set it.
2660              (make-local-variable (car elem))
2661              (set (car elem) (eval (nth 1 elem))))))))
2662
2663 (defun gnus-summary-read-group (group &optional show-all no-article
2664                                       kill-buffer no-display backward
2665                                       select-articles)
2666   "Start reading news in newsgroup GROUP.
2667 If SHOW-ALL is non-nil, already read articles are also listed.
2668 If NO-ARTICLE is non-nil, no article is selected initially.
2669 If NO-DISPLAY, don't generate a summary buffer."
2670   (let (result)
2671     (while (and group
2672                 (null (setq result
2673                             (let ((gnus-auto-select-next nil))
2674                               (or (gnus-summary-read-group-1
2675                                    group show-all no-article
2676                                    kill-buffer no-display
2677                                    select-articles)
2678                                   (setq show-all nil
2679                                         select-articles nil)))))
2680                 (eq gnus-auto-select-next 'quietly))
2681       (set-buffer gnus-group-buffer)
2682       ;; The entry function called above goes to the next
2683       ;; group automatically, so we go two groups back
2684       ;; if we are searching for the previous group.
2685       (when backward
2686         (gnus-group-prev-unread-group 2))
2687       (if (not (equal group (gnus-group-group-name)))
2688           (setq group (gnus-group-group-name))
2689         (setq group nil)))
2690     result))
2691
2692 (defun gnus-summary-read-group-1 (group show-all no-article
2693                                         kill-buffer no-display
2694                                         &optional select-articles)
2695   ;; Killed foreign groups can't be entered.
2696   (when (and (not (gnus-group-native-p group))
2697              (not (gnus-gethash group gnus-newsrc-hashtb)))
2698     (error "Dead non-native groups can't be entered"))
2699   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2700   (let* ((new-group (gnus-summary-setup-buffer group))
2701          (quit-config (gnus-group-quit-config group))
2702          (did-select (and new-group (gnus-select-newsgroup
2703                                      group show-all select-articles))))
2704     (cond
2705      ;; This summary buffer exists already, so we just select it.
2706      ((not new-group)
2707       (gnus-set-global-variables)
2708       (when kill-buffer
2709         (gnus-kill-or-deaden-summary kill-buffer))
2710       (gnus-configure-windows 'summary 'force)
2711       (gnus-set-mode-line 'summary)
2712       (gnus-summary-position-point)
2713       (message "")
2714       t)
2715      ;; We couldn't select this group.
2716      ((null did-select)
2717       (when (and (eq major-mode 'gnus-summary-mode)
2718                  (not (equal (current-buffer) kill-buffer)))
2719         (kill-buffer (current-buffer))
2720         (if (not quit-config)
2721             (progn
2722               ;; Update the info -- marks might need to be removed,
2723               ;; for instance.
2724               (gnus-summary-update-info)
2725               (set-buffer gnus-group-buffer)
2726               (gnus-group-jump-to-group group)
2727               (gnus-group-next-unread-group 1))
2728           (gnus-handle-ephemeral-exit quit-config)))
2729       (gnus-message 3 "Can't select group")
2730       nil)
2731      ;; The user did a `C-g' while prompting for number of articles,
2732      ;; so we exit this group.
2733      ((eq did-select 'quit)
2734       (and (eq major-mode 'gnus-summary-mode)
2735            (not (equal (current-buffer) kill-buffer))
2736            (kill-buffer (current-buffer)))
2737       (when kill-buffer
2738         (gnus-kill-or-deaden-summary kill-buffer))
2739       (if (not quit-config)
2740           (progn
2741             (set-buffer gnus-group-buffer)
2742             (gnus-group-jump-to-group group)
2743             (gnus-group-next-unread-group 1)
2744             (gnus-configure-windows 'group 'force))
2745         (gnus-handle-ephemeral-exit quit-config))
2746       ;; Finally signal the quit.
2747       (signal 'quit nil))
2748      ;; The group was successfully selected.
2749      (t
2750       (gnus-set-global-variables)
2751       ;; Save the active value in effect when the group was entered.
2752       (setq gnus-newsgroup-active
2753             (gnus-copy-sequence
2754              (gnus-active gnus-newsgroup-name)))
2755       ;; You can change the summary buffer in some way with this hook.
2756       (gnus-run-hooks 'gnus-select-group-hook)
2757       ;; Set any local variables in the group parameters.
2758       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2759       (gnus-update-format-specifications
2760        nil 'summary 'summary-mode 'summary-dummy)
2761       ;; Do score processing.
2762       (when gnus-use-scoring
2763         (gnus-possibly-score-headers))
2764       ;; Check whether to fill in the gaps in the threads.
2765       (when gnus-build-sparse-threads
2766         (gnus-build-sparse-threads))
2767       ;; Find the initial limit.
2768       (if gnus-show-threads
2769           (if show-all
2770               (let ((gnus-newsgroup-dormant nil))
2771                 (gnus-summary-initial-limit show-all))
2772             (gnus-summary-initial-limit show-all))
2773         (setq gnus-newsgroup-limit
2774               (mapcar
2775                (lambda (header) (mail-header-number header))
2776                gnus-newsgroup-headers)))
2777       ;; Generate the summary buffer.
2778       (unless no-display
2779         (gnus-summary-prepare))
2780       (when gnus-use-trees
2781         (gnus-tree-open group)
2782         (setq gnus-summary-highlight-line-function
2783               'gnus-tree-highlight-article))
2784       ;; If the summary buffer is empty, but there are some low-scored
2785       ;; articles or some excluded dormants, we include these in the
2786       ;; buffer.
2787       (when (and (zerop (buffer-size))
2788                  (not no-display))
2789         (cond (gnus-newsgroup-dormant
2790                (gnus-summary-limit-include-dormant))
2791               ((and gnus-newsgroup-scored show-all)
2792                (gnus-summary-limit-include-expunged t))))
2793       ;; Function `gnus-apply-kill-file' must be called in this hook.
2794       (gnus-run-hooks 'gnus-apply-kill-hook)
2795       (if (and (zerop (buffer-size))
2796                (not no-display))
2797           (progn
2798             ;; This newsgroup is empty.
2799             (gnus-summary-catchup-and-exit nil t)
2800             (gnus-message 6 "No unread news")
2801             (when kill-buffer
2802               (gnus-kill-or-deaden-summary kill-buffer))
2803             ;; Return nil from this function.
2804             nil)
2805         ;; Hide conversation thread subtrees.  We cannot do this in
2806         ;; gnus-summary-prepare-hook since kill processing may not
2807         ;; work with hidden articles.
2808         (and gnus-show-threads
2809              gnus-thread-hide-subtree
2810              (gnus-summary-hide-all-threads))
2811         (when kill-buffer
2812           (gnus-kill-or-deaden-summary kill-buffer))
2813         ;; Show first unread article if requested.
2814         (if (and (not no-article)
2815                  (not no-display)
2816                  gnus-newsgroup-unreads
2817                  gnus-auto-select-first)
2818             (progn
2819               (gnus-configure-windows 'summary)
2820               (cond
2821                ((eq gnus-auto-select-first 'best)
2822                 (gnus-summary-best-unread-article))
2823                ((eq gnus-auto-select-first t)
2824                 (gnus-summary-first-unread-article))
2825                ((gnus-functionp gnus-auto-select-first)
2826                 (funcall gnus-auto-select-first))))
2827           ;; Don't select any articles, just move point to the first
2828           ;; article in the group.
2829           (goto-char (point-min))
2830           (gnus-summary-position-point)
2831           (gnus-configure-windows 'summary 'force)
2832           (gnus-set-mode-line 'summary))
2833         (when (get-buffer-window gnus-group-buffer t)
2834           ;; Gotta use windows, because recenter does weird stuff if
2835           ;; the current buffer ain't the displayed window.
2836           (let ((owin (selected-window)))
2837             (select-window (get-buffer-window gnus-group-buffer t))
2838             (when (gnus-group-goto-group group)
2839               (recenter))
2840             (select-window owin)))
2841         ;; Mark this buffer as "prepared".
2842         (setq gnus-newsgroup-prepared t)
2843         (gnus-run-hooks 'gnus-summary-prepared-hook)
2844         t)))))
2845
2846 (defun gnus-summary-prepare ()
2847   "Generate the summary buffer."
2848   (interactive)
2849   (let ((buffer-read-only nil))
2850     (erase-buffer)
2851     (setq gnus-newsgroup-data nil
2852           gnus-newsgroup-data-reverse nil)
2853     (gnus-run-hooks 'gnus-summary-generate-hook)
2854     ;; Generate the buffer, either with threads or without.
2855     (when gnus-newsgroup-headers
2856       (gnus-summary-prepare-threads
2857        (if gnus-show-threads
2858            (gnus-sort-gathered-threads
2859             (funcall gnus-summary-thread-gathering-function
2860                      (gnus-sort-threads
2861                       (gnus-cut-threads (gnus-make-threads)))))
2862          ;; Unthreaded display.
2863          (gnus-sort-articles gnus-newsgroup-headers))))
2864     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2865     ;; Call hooks for modifying summary buffer.
2866     (goto-char (point-min))
2867     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2868
2869 (defsubst gnus-general-simplify-subject (subject)
2870   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2871   (setq subject
2872         (cond
2873          ;; Truncate the subject.
2874          (gnus-simplify-subject-functions
2875           (gnus-map-function gnus-simplify-subject-functions subject))
2876          ((numberp gnus-summary-gather-subject-limit)
2877           (setq subject (gnus-simplify-subject-re subject))
2878           (if (> (length subject) gnus-summary-gather-subject-limit)
2879               (substring subject 0 gnus-summary-gather-subject-limit)
2880             subject))
2881          ;; Fuzzily simplify it.
2882          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2883           (gnus-simplify-subject-fuzzy subject))
2884          ;; Just remove the leading "Re:".
2885          (t
2886           (gnus-simplify-subject-re subject))))
2887
2888   (if (and gnus-summary-gather-exclude-subject
2889            (string-match gnus-summary-gather-exclude-subject subject))
2890       nil                               ; This article shouldn't be gathered
2891     subject))
2892
2893 (defun gnus-summary-simplify-subject-query ()
2894   "Query where the respool algorithm would put this article."
2895   (interactive)
2896   (gnus-summary-select-article)
2897   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2898
2899 (defun gnus-gather-threads-by-subject (threads)
2900   "Gather threads by looking at Subject headers."
2901   (if (not gnus-summary-make-false-root)
2902       threads
2903     (let ((hashtb (gnus-make-hashtable 1024))
2904           (prev threads)
2905           (result threads)
2906           subject hthread whole-subject)
2907       (while threads
2908         (setq subject (gnus-general-simplify-subject
2909                        (setq whole-subject (mail-header-subject
2910                                             (caar threads)))))
2911         (when subject
2912           (if (setq hthread (gnus-gethash subject hashtb))
2913               (progn
2914                 ;; We enter a dummy root into the thread, if we
2915                 ;; haven't done that already.
2916                 (unless (stringp (caar hthread))
2917                   (setcar hthread (list whole-subject (car hthread))))
2918                 ;; We add this new gathered thread to this gathered
2919                 ;; thread.
2920                 (setcdr (car hthread)
2921                         (nconc (cdar hthread) (list (car threads))))
2922                 ;; Remove it from the list of threads.
2923                 (setcdr prev (cdr threads))
2924                 (setq threads prev))
2925             ;; Enter this thread into the hash table.
2926             (gnus-sethash subject threads hashtb)))
2927         (setq prev threads)
2928         (setq threads (cdr threads)))
2929       result)))
2930
2931 (defun gnus-gather-threads-by-references (threads)
2932   "Gather threads by looking at References headers."
2933   (let ((idhashtb (gnus-make-hashtable 1024))
2934         (thhashtb (gnus-make-hashtable 1024))
2935         (prev threads)
2936         (result threads)
2937         ids references id gthread gid entered ref)
2938     (while threads
2939       (when (setq references (mail-header-references (caar threads)))
2940         (setq id (mail-header-id (caar threads))
2941               ids (gnus-split-references references)
2942               entered nil)
2943         (while (setq ref (pop ids))
2944           (setq ids (delete ref ids))
2945           (if (not (setq gid (gnus-gethash ref idhashtb)))
2946               (progn
2947                 (gnus-sethash ref id idhashtb)
2948                 (gnus-sethash id threads thhashtb))
2949             (setq gthread (gnus-gethash gid thhashtb))
2950             (unless entered
2951               ;; We enter a dummy root into the thread, if we
2952               ;; haven't done that already.
2953               (unless (stringp (caar gthread))
2954                 (setcar gthread (list (mail-header-subject (caar gthread))
2955                                       (car gthread))))
2956               ;; We add this new gathered thread to this gathered
2957               ;; thread.
2958               (setcdr (car gthread)
2959                       (nconc (cdar gthread) (list (car threads)))))
2960             ;; Add it into the thread hash table.
2961             (gnus-sethash id gthread thhashtb)
2962             (setq entered t)
2963             ;; Remove it from the list of threads.
2964             (setcdr prev (cdr threads))
2965             (setq threads prev))))
2966       (setq prev threads)
2967       (setq threads (cdr threads)))
2968     result))
2969
2970 (defun gnus-sort-gathered-threads (threads)
2971   "Sort subtreads inside each gathered thread by article number."
2972   (let ((result threads))
2973     (while threads
2974       (when (stringp (caar threads))
2975         (setcdr (car threads)
2976                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2977       (setq threads (cdr threads)))
2978     result))
2979
2980 (defun gnus-thread-loop-p (root thread)
2981   "Say whether ROOT is in THREAD."
2982   (let ((stack (list thread))
2983         (infloop 0)
2984         th)
2985     (while (setq thread (pop stack))
2986       (setq th (cdr thread))
2987       (while (and th
2988                   (not (eq (caar th) root)))
2989         (pop th))
2990       (if th
2991           ;; We have found a loop.
2992           (let (ref-dep)
2993             (setcdr thread (delq (car th) (cdr thread)))
2994             (if (boundp (setq ref-dep (intern "none"
2995                                               gnus-newsgroup-dependencies)))
2996                 (setcdr (symbol-value ref-dep)
2997                         (nconc (cdr (symbol-value ref-dep))
2998                                (list (car th))))
2999               (set ref-dep (list nil (car th))))
3000             (setq infloop 1
3001                   stack nil))
3002         ;; Push all the subthreads onto the stack.
3003         (push (cdr thread) stack)))
3004     infloop))
3005
3006 (defun gnus-make-threads ()
3007   "Go through the dependency hashtb and find the roots.  Return all threads."
3008   (let (threads)
3009     (while (catch 'infloop
3010              (mapatoms
3011               (lambda (refs)
3012                 ;; Deal with self-referencing References loops.
3013                 (when (and (car (symbol-value refs))
3014                            (not (zerop
3015                                  (apply
3016                                   '+
3017                                   (mapcar
3018                                    (lambda (thread)
3019                                      (gnus-thread-loop-p
3020                                       (car (symbol-value refs)) thread))
3021                                    (cdr (symbol-value refs)))))))
3022                   (setq threads nil)
3023                   (throw 'infloop t))
3024                 (unless (car (symbol-value refs))
3025                   ;; These threads do not refer back to any other articles,
3026                   ;; so they're roots.
3027                   (setq threads (append (cdr (symbol-value refs)) threads))))
3028               gnus-newsgroup-dependencies)))
3029     threads))
3030
3031 ;; Build the thread tree.
3032 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3033   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3034
3035 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3036 if it was already present.
3037
3038 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3039 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3040 Message-IDs will be renamed be renamed to a unique Message-ID before
3041 being entered.
3042
3043 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3044   (let* ((id (mail-header-id header))
3045          (id-dep (and id (intern id dependencies)))
3046          ref ref-dep ref-header)
3047     ;; Enter this `header' in the `dependencies' table.
3048     (cond
3049      ((not id-dep)
3050       (setq header nil))
3051      ;; The first two cases do the normal part: enter a new `header'
3052      ;; in the `dependencies' table.
3053      ((not (boundp id-dep))
3054       (set id-dep (list header)))
3055      ((null (car (symbol-value id-dep)))
3056       (setcar (symbol-value id-dep) header))
3057
3058      ;; From here the `header' was already present in the
3059      ;; `dependencies' table.
3060      (force-new
3061       ;; Overrides an existing entry;
3062       ;; just set the header part of the entry.
3063       (setcar (symbol-value id-dep) header))
3064
3065      ;; Renames the existing `header' to a unique Message-ID.
3066      ((not gnus-summary-ignore-duplicates)
3067       ;; An article with this Message-ID has already been seen.
3068       ;; We rename the Message-ID.
3069       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3070            (list header))
3071       (mail-header-set-id header id))
3072
3073      ;; The last case ignores an existing entry, except it adds any
3074      ;; additional Xrefs (in case the two articles came from different
3075      ;; servers.
3076      ;; Also sets `header' to `nil' meaning that the `dependencies'
3077      ;; table was *not* modified.
3078      (t
3079       (mail-header-set-xref
3080        (car (symbol-value id-dep))
3081        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3082                    "")
3083                (or (mail-header-xref header) "")))
3084       (setq header nil)))
3085
3086     (when header
3087       ;; First check if that we are not creating a References loop.
3088       (setq ref (gnus-parent-id (mail-header-references header)))
3089       (while (and ref
3090                   (setq ref-dep (intern-soft ref dependencies))
3091                   (boundp ref-dep)
3092                   (setq ref-header (car (symbol-value ref-dep))))
3093         (if (string= id ref)
3094             ;; Yuk!  This is a reference loop.  Make the article be a
3095             ;; root article.
3096             (progn
3097               (mail-header-set-references (car (symbol-value id-dep)) "none")
3098               (setq ref nil))
3099           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3100       (setq ref (gnus-parent-id (mail-header-references header)))
3101       (setq ref-dep (intern (or ref "none") dependencies))
3102       (if (boundp ref-dep)
3103           (setcdr (symbol-value ref-dep)
3104                   (nconc (cdr (symbol-value ref-dep))
3105                          (list (symbol-value id-dep))))
3106         (set ref-dep (list nil (symbol-value id-dep)))))
3107     header))
3108
3109 (defun gnus-build-sparse-threads ()
3110   (let ((headers gnus-newsgroup-headers)
3111         (gnus-summary-ignore-duplicates t)
3112         header references generation relations
3113         subject child end new-child date)
3114     ;; First we create an alist of generations/relations, where
3115     ;; generations is how much we trust the relation, and the relation
3116     ;; is parent/child.
3117     (gnus-message 7 "Making sparse threads...")
3118     (save-excursion
3119       (nnheader-set-temp-buffer " *gnus sparse threads*")
3120       (while (setq header (pop headers))
3121         (when (and (setq references (mail-header-references header))
3122                    (not (string= references "")))
3123           (insert references)
3124           (setq child (mail-header-id header)
3125                 subject (mail-header-subject header)
3126                 date (mail-header-date header)
3127                 generation 0)
3128           (while (search-backward ">" nil t)
3129             (setq end (1+ (point)))
3130             (when (search-backward "<" nil t)
3131               (setq new-child (buffer-substring (point) end))
3132               (push (list (incf generation)
3133                           child (setq child new-child)
3134                           subject date)
3135                     relations)))
3136           (when child
3137             (push (list (1+ generation) child nil subject) relations))
3138           (erase-buffer)))
3139       (kill-buffer (current-buffer)))
3140     ;; Sort over trustworthiness.
3141     (mapcar
3142      (lambda (relation)
3143        (when (gnus-dependencies-add-header
3144               (make-full-mail-header
3145                gnus-reffed-article-number
3146                (nth 3 relation) "" (or (nth 4 relation) "")
3147                (nth 1 relation)
3148                (or (nth 2 relation) "") 0 0 "")
3149               gnus-newsgroup-dependencies nil)
3150          (push gnus-reffed-article-number gnus-newsgroup-limit)
3151          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3152          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3153                gnus-newsgroup-reads)
3154          (decf gnus-reffed-article-number)))
3155      (sort relations 'car-less-than-car))
3156     (gnus-message 7 "Making sparse threads...done")))
3157
3158 (defun gnus-build-old-threads ()
3159   ;; Look at all the articles that refer back to old articles, and
3160   ;; fetch the headers for the articles that aren't there.  This will
3161   ;; build complete threads - if the roots haven't been expired by the
3162   ;; server, that is.
3163   (let (id heads)
3164     (mapatoms
3165      (lambda (refs)
3166        (when (not (car (symbol-value refs)))
3167          (setq heads (cdr (symbol-value refs)))
3168          (while heads
3169            (if (memq (mail-header-number (caar heads))
3170                      gnus-newsgroup-dormant)
3171                (setq heads (cdr heads))
3172              (setq id (symbol-name refs))
3173              (while (and (setq id (gnus-build-get-header id))
3174                          (not (car (gnus-id-to-thread id)))))
3175              (setq heads nil)))))
3176      gnus-newsgroup-dependencies)))
3177
3178 ;; This function has to be called with point after the article number
3179 ;; on the beginning of the line.
3180 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3181   (let ((eol (gnus-point-at-eol))
3182         (buffer (current-buffer))
3183         header)
3184
3185     ;; overview: [num subject from date id refs chars lines misc]
3186     (unless (eobp)
3187       (forward-char))
3188
3189     (setq header
3190           (make-full-mail-header
3191            number                               ; number
3192            (nnheader-nov-field)                 ; subject
3193            (nnheader-nov-field)                 ; from
3194            (nnheader-nov-field)                 ; date
3195            (nnheader-nov-read-message-id)       ; id
3196            (nnheader-nov-field)                 ; refs
3197            (nnheader-nov-read-integer)          ; chars
3198            (nnheader-nov-read-integer)          ; lines
3199            (unless (eobp)
3200              (nnheader-nov-field))              ; misc
3201            (nnheader-nov-parse-extra)))         ; extra
3202
3203     (when gnus-alter-header-function
3204       (funcall gnus-alter-header-function header))
3205     (gnus-dependencies-add-header header dependencies force-new)))
3206
3207 (defun gnus-build-get-header (id)
3208   ;; Look through the buffer of NOV lines and find the header to
3209   ;; ID.  Enter this line into the dependencies hash table, and return
3210   ;; the id of the parent article (if any).
3211   (let ((deps gnus-newsgroup-dependencies)
3212         found header)
3213     (prog1
3214         (save-excursion
3215           (set-buffer nntp-server-buffer)
3216           (let ((case-fold-search nil))
3217             (goto-char (point-min))
3218             (while (and (not found)
3219                         (search-forward id nil t))
3220               (beginning-of-line)
3221               (setq found (looking-at
3222                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3223                                    (regexp-quote id))))
3224               (or found (beginning-of-line 2)))
3225             (when found
3226               (beginning-of-line)
3227               (and
3228                (setq header (gnus-nov-parse-line
3229                              (read (current-buffer)) deps))
3230                (gnus-parent-id (mail-header-references header))))))
3231       (when header
3232         (let ((number (mail-header-number header)))
3233           (push number gnus-newsgroup-limit)
3234           (push header gnus-newsgroup-headers)
3235           (if (memq number gnus-newsgroup-unselected)
3236               (progn
3237                 (push number gnus-newsgroup-unreads)
3238                 (setq gnus-newsgroup-unselected
3239                       (delq number gnus-newsgroup-unselected)))
3240             (push number gnus-newsgroup-ancient)))))))
3241
3242 (defun gnus-build-all-threads ()
3243   "Read all the headers."
3244   (let ((gnus-summary-ignore-duplicates t)
3245         (dependencies gnus-newsgroup-dependencies)
3246         header article)
3247     (save-excursion
3248       (set-buffer nntp-server-buffer)
3249       (let ((case-fold-search nil))
3250         (goto-char (point-min))
3251         (while (not (eobp))
3252           (ignore-errors
3253             (setq article (read (current-buffer))
3254                   header (gnus-nov-parse-line
3255                           article dependencies)))
3256           (when header
3257             (save-excursion
3258               (set-buffer gnus-summary-buffer)
3259               (push header gnus-newsgroup-headers)
3260               (if (memq (setq article (mail-header-number header))
3261                         gnus-newsgroup-unselected)
3262                   (progn
3263                     (push article gnus-newsgroup-unreads)
3264                     (setq gnus-newsgroup-unselected
3265                           (delq article gnus-newsgroup-unselected)))
3266                 (push article gnus-newsgroup-ancient)))
3267             (forward-line 1)))))))
3268
3269 (defun gnus-summary-update-article-line (article header)
3270   "Update the line for ARTICLE using HEADERS."
3271   (let* ((id (mail-header-id header))
3272          (thread (gnus-id-to-thread id)))
3273     (unless thread
3274       (error "Article in no thread"))
3275     ;; Update the thread.
3276     (setcar thread header)
3277     (gnus-summary-goto-subject article)
3278     (let* ((datal (gnus-data-find-list article))
3279            (data (car datal))
3280            (length (when (cdr datal)
3281                      (- (gnus-data-pos data)
3282                         (gnus-data-pos (cadr datal)))))
3283            (buffer-read-only nil)
3284            (level (gnus-summary-thread-level)))
3285       (gnus-delete-line)
3286       (gnus-summary-insert-line
3287        header level nil (gnus-article-mark article)
3288        (memq article gnus-newsgroup-replied)
3289        (memq article gnus-newsgroup-expirable)
3290        ;; Only insert the Subject string when it's different
3291        ;; from the previous Subject string.
3292        (if (gnus-subject-equal
3293             (condition-case ()
3294                 (mail-header-subject
3295                  (gnus-data-header
3296                   (cadr
3297                    (gnus-data-find-list
3298                     article
3299                     (gnus-data-list t)))))
3300               ;; Error on the side of excessive subjects.
3301               (error ""))
3302             (mail-header-subject header))
3303            ""
3304          (mail-header-subject header))
3305        nil (cdr (assq article gnus-newsgroup-scored))
3306        (memq article gnus-newsgroup-processable))
3307       (when length
3308         (gnus-data-update-list
3309          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3310
3311 (defun gnus-summary-update-article (article &optional iheader)
3312   "Update ARTICLE in the summary buffer."
3313   (set-buffer gnus-summary-buffer)
3314   (let* ((header (gnus-summary-article-header article))
3315          (id (mail-header-id header))
3316          (data (gnus-data-find article))
3317          (thread (gnus-id-to-thread id))
3318          (references (mail-header-references header))
3319          (parent
3320           (gnus-id-to-thread
3321            (or (gnus-parent-id
3322                 (when (and references
3323                            (not (equal "" references)))
3324                   references))
3325                "none")))
3326          (buffer-read-only nil)
3327          (old (car thread)))
3328     (when thread
3329       (unless iheader
3330         (setcar thread nil)
3331         (when parent
3332           (delq thread parent)))
3333       (if (gnus-summary-insert-subject id header)
3334           ;; Set the (possibly) new article number in the data structure.
3335           (gnus-data-set-number data (gnus-id-to-article id))
3336         (setcar thread old)
3337         nil))))
3338
3339 (defun gnus-rebuild-thread (id &optional line)
3340   "Rebuild the thread containing ID.
3341 If LINE, insert the rebuilt thread starting on line LINE."
3342   (let ((buffer-read-only nil)
3343         old-pos current thread data)
3344     (if (not gnus-show-threads)
3345         (setq thread (list (car (gnus-id-to-thread id))))
3346       ;; Get the thread this article is part of.
3347       (setq thread (gnus-remove-thread id)))
3348     (setq old-pos (gnus-point-at-bol))
3349     (setq current (save-excursion
3350                     (and (zerop (forward-line -1))
3351                          (gnus-summary-article-number))))
3352     ;; If this is a gathered thread, we have to go some re-gathering.
3353     (when (stringp (car thread))
3354       (let ((subject (car thread))
3355             roots thr)
3356         (setq thread (cdr thread))
3357         (while thread
3358           (unless (memq (setq thr (gnus-id-to-thread
3359                                    (gnus-root-id
3360                                     (mail-header-id (caar thread)))))
3361                         roots)
3362             (push thr roots))
3363           (setq thread (cdr thread)))
3364         ;; We now have all (unique) roots.
3365         (if (= (length roots) 1)
3366             ;; All the loose roots are now one solid root.
3367             (setq thread (car roots))
3368           (setq thread (cons subject (gnus-sort-threads roots))))))
3369     (let (threads)
3370       ;; We then insert this thread into the summary buffer.
3371       (when line
3372         (goto-char (point-min))
3373         (forward-line (1- line)))
3374       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3375         (if gnus-show-threads
3376             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3377           (gnus-summary-prepare-unthreaded thread))
3378         (setq data (nreverse gnus-newsgroup-data))
3379         (setq threads gnus-newsgroup-threads))
3380       ;; We splice the new data into the data structure.
3381       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3382       ;;!!! then we want to insert at the beginning of the buffer.
3383       ;;!!! That happens to be true with Gnus now, but that may
3384       ;;!!! change in the future.  Perhaps.
3385       (gnus-data-enter-list
3386        (if line nil current) data (- (point) old-pos))
3387       (setq gnus-newsgroup-threads
3388             (nconc threads gnus-newsgroup-threads))
3389       (gnus-data-compute-positions))))
3390
3391 (defun gnus-number-to-header (number)
3392   "Return the header for article NUMBER."
3393   (let ((headers gnus-newsgroup-headers))
3394     (while (and headers
3395                 (not (= number (mail-header-number (car headers)))))
3396       (pop headers))
3397     (when headers
3398       (car headers))))
3399
3400 (defun gnus-parent-headers (in-headers &optional generation)
3401   "Return the headers of the GENERATIONeth parent of HEADERS."
3402   (unless generation
3403     (setq generation 1))
3404   (let ((parent t)
3405         (headers in-headers)
3406         references)
3407     (while (and parent
3408                 (not (zerop generation))
3409                 (setq references (mail-header-references headers)))
3410       (setq headers (if (and references
3411                              (setq parent (gnus-parent-id references)))
3412                         (car (gnus-id-to-thread parent))
3413                       nil))
3414       (decf generation))
3415     (and (not (eq headers in-headers))
3416          headers)))
3417
3418 (defun gnus-id-to-thread (id)
3419   "Return the (sub-)thread where ID appears."
3420   (gnus-gethash id gnus-newsgroup-dependencies))
3421
3422 (defun gnus-id-to-article (id)
3423   "Return the article number of ID."
3424   (let ((thread (gnus-id-to-thread id)))
3425     (when (and thread
3426                (car thread))
3427       (mail-header-number (car thread)))))
3428
3429 (defun gnus-id-to-header (id)
3430   "Return the article headers of ID."
3431   (car (gnus-id-to-thread id)))
3432
3433 (defun gnus-article-displayed-root-p (article)
3434   "Say whether ARTICLE is a root(ish) article."
3435   (let ((level (gnus-summary-thread-level article))
3436         (refs (mail-header-references  (gnus-summary-article-header article)))
3437         particle)
3438     (cond
3439      ((null level) nil)
3440      ((zerop level) t)
3441      ((null refs) t)
3442      ((null (gnus-parent-id refs)) t)
3443      ((and (= 1 level)
3444            (null (setq particle (gnus-id-to-article
3445                                  (gnus-parent-id refs))))
3446            (null (gnus-summary-thread-level particle)))))))
3447
3448 (defun gnus-root-id (id)
3449   "Return the id of the root of the thread where ID appears."
3450   (let (last-id prev)
3451     (while (and id (setq prev (car (gnus-id-to-thread id))))
3452       (setq last-id id
3453             id (gnus-parent-id (mail-header-references prev))))
3454     last-id))
3455
3456 (defun gnus-articles-in-thread (thread)
3457   "Return the list of articles in THREAD."
3458   (cons (mail-header-number (car thread))
3459         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3460
3461 (defun gnus-remove-thread (id &optional dont-remove)
3462   "Remove the thread that has ID in it."
3463   (let (headers thread last-id)
3464     ;; First go up in this thread until we find the root.
3465     (setq last-id (gnus-root-id id)
3466           headers (message-flatten-list (gnus-id-to-thread last-id)))
3467     ;; We have now found the real root of this thread.  It might have
3468     ;; been gathered into some loose thread, so we have to search
3469     ;; through the threads to find the thread we wanted.
3470     (let ((threads gnus-newsgroup-threads)
3471           sub)
3472       (while threads
3473         (setq sub (car threads))
3474         (if (stringp (car sub))
3475             ;; This is a gathered thread, so we look at the roots
3476             ;; below it to find whether this article is in this
3477             ;; gathered root.
3478             (progn
3479               (setq sub (cdr sub))
3480               (while sub
3481                 (when (member (caar sub) headers)
3482                   (setq thread (car threads)
3483                         threads nil
3484                         sub nil))
3485                 (setq sub (cdr sub))))
3486           ;; It's an ordinary thread, so we check it.
3487           (when (eq (car sub) (car headers))
3488             (setq thread sub
3489                   threads nil)))
3490         (setq threads (cdr threads)))
3491       ;; If this article is in no thread, then it's a root.
3492       (if thread
3493           (unless dont-remove
3494             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3495         (setq thread (gnus-id-to-thread last-id)))
3496       (when thread
3497         (prog1
3498             thread                      ; We return this thread.
3499           (unless dont-remove
3500             (if (stringp (car thread))
3501                 (progn
3502                   ;; If we use dummy roots, then we have to remove the
3503                   ;; dummy root as well.
3504                   (when (eq gnus-summary-make-false-root 'dummy)
3505                     ;; We go to the dummy root by going to
3506                     ;; the first sub-"thread", and then one line up.
3507                     (gnus-summary-goto-article
3508                      (mail-header-number (caadr thread)))
3509                     (forward-line -1)
3510                     (gnus-delete-line)
3511                     (gnus-data-compute-positions))
3512                   (setq thread (cdr thread))
3513                   (while thread
3514                     (gnus-remove-thread-1 (car thread))
3515                     (setq thread (cdr thread))))
3516               (gnus-summary-show-all-threads)
3517               (gnus-remove-thread-1 thread))))))))
3518
3519 (defun gnus-remove-thread-1 (thread)
3520   "Remove the thread THREAD recursively."
3521   (let ((number (mail-header-number (pop thread)))
3522         d)
3523     (setq thread (reverse thread))
3524     (while thread
3525       (gnus-remove-thread-1 (pop thread)))
3526     (when (setq d (gnus-data-find number))
3527       (goto-char (gnus-data-pos d))
3528       (gnus-data-remove
3529        number
3530        (- (gnus-point-at-bol)
3531           (prog1
3532               (1+ (gnus-point-at-eol))
3533             (gnus-delete-line)))))))
3534
3535 (defun gnus-sort-threads (threads)
3536   "Sort THREADS."
3537   (if (not gnus-thread-sort-functions)
3538       threads
3539     (gnus-message 8 "Sorting threads...")
3540     (prog1
3541         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3542       (gnus-message 8 "Sorting threads...done"))))
3543
3544 (defun gnus-sort-articles (articles)
3545   "Sort ARTICLES."
3546   (when gnus-article-sort-functions
3547     (gnus-message 7 "Sorting articles...")
3548     (prog1
3549         (setq gnus-newsgroup-headers
3550               (sort articles (gnus-make-sort-function
3551                               gnus-article-sort-functions)))
3552       (gnus-message 7 "Sorting articles...done"))))
3553
3554 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3555 (defmacro gnus-thread-header (thread)
3556   ;; Return header of first article in THREAD.
3557   ;; Note that THREAD must never, ever be anything else than a variable -
3558   ;; using some other form will lead to serious barfage.
3559   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3560   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3561   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3562         (vector thread) 2))
3563
3564 (defsubst gnus-article-sort-by-number (h1 h2)
3565   "Sort articles by article number."
3566   (< (mail-header-number h1)
3567      (mail-header-number h2)))
3568
3569 (defun gnus-thread-sort-by-number (h1 h2)
3570   "Sort threads by root article number."
3571   (gnus-article-sort-by-number
3572    (gnus-thread-header h1) (gnus-thread-header h2)))
3573
3574 (defsubst gnus-article-sort-by-lines (h1 h2)
3575   "Sort articles by article Lines header."
3576   (< (mail-header-lines h1)
3577      (mail-header-lines h2)))
3578
3579 (defun gnus-thread-sort-by-lines (h1 h2)
3580   "Sort threads by root article Lines header."
3581   (gnus-article-sort-by-lines
3582    (gnus-thread-header h1) (gnus-thread-header h2)))
3583
3584 (defsubst gnus-article-sort-by-chars (h1 h2)
3585   "Sort articles by octet length."
3586   (< (mail-header-chars h1)
3587      (mail-header-chars h2)))
3588
3589 (defun gnus-thread-sort-by-chars (h1 h2)
3590   "Sort threads by root article octet length."
3591   (gnus-article-sort-by-chars
3592    (gnus-thread-header h1) (gnus-thread-header h2)))
3593
3594 (defsubst gnus-article-sort-by-author (h1 h2)
3595   "Sort articles by root author."
3596   (string-lessp
3597    (let ((addr (car (mime-read-field 'From h1))))
3598      (or (std11-full-name-string addr)
3599          (std11-address-string addr)
3600          ""))
3601    (let ((addr (car (mime-read-field 'From h2))))
3602      (or (std11-full-name-string addr)
3603          (std11-address-string addr)
3604          ""))
3605    ))
3606
3607 (defun gnus-thread-sort-by-author (h1 h2)
3608   "Sort threads by root author."
3609   (gnus-article-sort-by-author
3610    (gnus-thread-header h1)  (gnus-thread-header h2)))
3611
3612 (defsubst gnus-article-sort-by-subject (h1 h2)
3613   "Sort articles by root subject."
3614   (string-lessp
3615    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3616    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3617
3618 (defun gnus-thread-sort-by-subject (h1 h2)
3619   "Sort threads by root subject."
3620   (gnus-article-sort-by-subject
3621    (gnus-thread-header h1) (gnus-thread-header h2)))
3622
3623 (defsubst gnus-article-sort-by-date (h1 h2)
3624   "Sort articles by root article date."
3625   (time-less-p
3626    (gnus-date-get-time (mail-header-date h1))
3627    (gnus-date-get-time (mail-header-date h2))))
3628
3629 (defun gnus-thread-sort-by-date (h1 h2)
3630   "Sort threads by root article date."
3631   (gnus-article-sort-by-date
3632    (gnus-thread-header h1) (gnus-thread-header h2)))
3633
3634 (defsubst gnus-article-sort-by-score (h1 h2)
3635   "Sort articles by root article score.
3636 Unscored articles will be counted as having a score of zero."
3637   (> (or (cdr (assq (mail-header-number h1)
3638                     gnus-newsgroup-scored))
3639          gnus-summary-default-score 0)
3640      (or (cdr (assq (mail-header-number h2)
3641                     gnus-newsgroup-scored))
3642          gnus-summary-default-score 0)))
3643
3644 (defun gnus-thread-sort-by-score (h1 h2)
3645   "Sort threads by root article score."
3646   (gnus-article-sort-by-score
3647    (gnus-thread-header h1) (gnus-thread-header h2)))
3648
3649 (defun gnus-thread-sort-by-total-score (h1 h2)
3650   "Sort threads by the sum of all scores in the thread.
3651 Unscored articles will be counted as having a score of zero."
3652   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3653
3654 (defun gnus-thread-total-score (thread)
3655   ;; This function find the total score of THREAD.
3656   (cond ((null thread)
3657          0)
3658         ((consp thread)
3659          (if (stringp (car thread))
3660              (apply gnus-thread-score-function 0
3661                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3662            (gnus-thread-total-score-1 thread)))
3663         (t
3664          (gnus-thread-total-score-1 (list thread)))))
3665
3666 (defun gnus-thread-total-score-1 (root)
3667   ;; This function find the total score of the thread below ROOT.
3668   (setq root (car root))
3669   (apply gnus-thread-score-function
3670          (or (append
3671               (mapcar 'gnus-thread-total-score
3672                       (cdr (gnus-id-to-thread (mail-header-id root))))
3673               (when (> (mail-header-number root) 0)
3674                 (list (or (cdr (assq (mail-header-number root)
3675                                      gnus-newsgroup-scored))
3676                           gnus-summary-default-score 0))))
3677              (list gnus-summary-default-score)
3678              '(0))))
3679
3680 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3681 (defvar gnus-tmp-prev-subject nil)
3682 (defvar gnus-tmp-false-parent nil)
3683 (defvar gnus-tmp-root-expunged nil)
3684 (defvar gnus-tmp-dummy-line nil)
3685
3686 (defvar gnus-tmp-header)
3687 (defun gnus-extra-header (type &optional header)
3688   "Return the extra header of TYPE."
3689   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3690       ""))
3691
3692 (defun gnus-summary-prepare-threads (threads)
3693   "Prepare summary buffer from THREADS and indentation LEVEL.
3694 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3695 or a straight list of headers."
3696   (gnus-message 7 "Generating summary...")
3697
3698   (setq gnus-newsgroup-threads threads)
3699   (beginning-of-line)
3700
3701   (let ((gnus-tmp-level 0)
3702         (default-score (or gnus-summary-default-score 0))
3703         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3704         thread number subject stack state gnus-tmp-gathered beg-match
3705         new-roots gnus-tmp-new-adopts thread-end
3706         gnus-tmp-header gnus-tmp-unread
3707         gnus-tmp-replied gnus-tmp-subject-or-nil
3708         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3709         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3710         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3711
3712     (setq gnus-tmp-prev-subject nil)
3713
3714     (if (vectorp (car threads))
3715         ;; If this is a straight (sic) list of headers, then a
3716         ;; threaded summary display isn't required, so we just create
3717         ;; an unthreaded one.
3718         (gnus-summary-prepare-unthreaded threads)
3719
3720       ;; Do the threaded display.
3721
3722       (while (or threads stack gnus-tmp-new-adopts new-roots)
3723
3724         (if (and (= gnus-tmp-level 0)
3725                  (or (not stack)
3726                      (= (caar stack) 0))
3727                  (not gnus-tmp-false-parent)
3728                  (or gnus-tmp-new-adopts new-roots))
3729             (if gnus-tmp-new-adopts
3730                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3731                       thread (list (car gnus-tmp-new-adopts))
3732                       gnus-tmp-header (caar thread)
3733                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3734               (when new-roots
3735                 (setq thread (list (car new-roots))
3736                       gnus-tmp-header (caar thread)
3737                       new-roots (cdr new-roots))))
3738
3739           (if threads
3740               ;; If there are some threads, we do them before the
3741               ;; threads on the stack.
3742               (setq thread threads
3743                     gnus-tmp-header (caar thread))
3744             ;; There were no current threads, so we pop something off
3745             ;; the stack.
3746             (setq state (car stack)
3747                   gnus-tmp-level (car state)
3748                   thread (cdr state)
3749                   stack (cdr stack)
3750                   gnus-tmp-header (caar thread))))
3751
3752         (setq gnus-tmp-false-parent nil)
3753         (setq gnus-tmp-root-expunged nil)
3754         (setq thread-end nil)
3755
3756         (if (stringp gnus-tmp-header)
3757             ;; The header is a dummy root.
3758             (cond
3759              ((eq gnus-summary-make-false-root 'adopt)
3760               ;; We let the first article adopt the rest.
3761               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3762                                                (cddar thread)))
3763               (setq gnus-tmp-gathered
3764                     (nconc (mapcar
3765                             (lambda (h) (mail-header-number (car h)))
3766                             (cddar thread))
3767                            gnus-tmp-gathered))
3768               (setq thread (cons (list (caar thread)
3769                                        (cadar thread))
3770                                  (cdr thread)))
3771               (setq gnus-tmp-level -1
3772                     gnus-tmp-false-parent t))
3773              ((eq gnus-summary-make-false-root 'empty)
3774               ;; We print adopted articles with empty subject fields.
3775               (setq gnus-tmp-gathered
3776                     (nconc (mapcar
3777                             (lambda (h) (mail-header-number (car h)))
3778                             (cddar thread))
3779                            gnus-tmp-gathered))
3780               (setq gnus-tmp-level -1))
3781              ((eq gnus-summary-make-false-root 'dummy)
3782               ;; We remember that we probably want to output a dummy
3783               ;; root.
3784               (setq gnus-tmp-dummy-line gnus-tmp-header)
3785               (setq gnus-tmp-prev-subject gnus-tmp-header))
3786              (t
3787               ;; We do not make a root for the gathered
3788               ;; sub-threads at all.
3789               (setq gnus-tmp-level -1)))
3790
3791           (setq number (mail-header-number gnus-tmp-header)
3792                 subject (mail-header-subject gnus-tmp-header))
3793
3794           (cond
3795            ;; If the thread has changed subject, we might want to make
3796            ;; this subthread into a root.
3797            ((and (null gnus-thread-ignore-subject)
3798                  (not (zerop gnus-tmp-level))
3799                  gnus-tmp-prev-subject
3800                  (not (inline
3801                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3802             (setq new-roots (nconc new-roots (list (car thread)))
3803                   thread-end t
3804                   gnus-tmp-header nil))
3805            ;; If the article lies outside the current limit,
3806            ;; then we do not display it.
3807            ((not (memq number gnus-newsgroup-limit))
3808             (setq gnus-tmp-gathered
3809                   (nconc (mapcar
3810                           (lambda (h) (mail-header-number (car h)))
3811                           (cdar thread))
3812                          gnus-tmp-gathered))
3813             (setq gnus-tmp-new-adopts (if (cdar thread)
3814                                           (append gnus-tmp-new-adopts
3815                                                   (cdar thread))
3816                                         gnus-tmp-new-adopts)
3817                   thread-end t
3818                   gnus-tmp-header nil)
3819             (when (zerop gnus-tmp-level)
3820               (setq gnus-tmp-root-expunged t)))
3821            ;; Perhaps this article is to be marked as read?
3822            ((and gnus-summary-mark-below
3823                  (< (or (cdr (assq number gnus-newsgroup-scored))
3824                         default-score)
3825                     gnus-summary-mark-below)
3826                  ;; Don't touch sparse articles.
3827                  (not (gnus-summary-article-sparse-p number))
3828                  (not (gnus-summary-article-ancient-p number)))
3829             (setq gnus-newsgroup-unreads
3830                   (delq number gnus-newsgroup-unreads))
3831             (if gnus-newsgroup-auto-expire
3832                 (push number gnus-newsgroup-expirable)
3833               (push (cons number gnus-low-score-mark)
3834                     gnus-newsgroup-reads))))
3835
3836           (when gnus-tmp-header
3837             ;; We may have an old dummy line to output before this
3838             ;; article.
3839             (when (and gnus-tmp-dummy-line
3840                        (gnus-subject-equal
3841                         gnus-tmp-dummy-line
3842                         (mail-header-subject gnus-tmp-header)))
3843               (gnus-summary-insert-dummy-line
3844                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3845               (setq gnus-tmp-dummy-line nil))
3846
3847             ;; Compute the mark.
3848             (setq gnus-tmp-unread (gnus-article-mark number))
3849
3850             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3851                                   gnus-tmp-header gnus-tmp-level)
3852                   gnus-newsgroup-data)
3853
3854             ;; Actually insert the line.
3855             (setq
3856              gnus-tmp-subject-or-nil
3857              (cond
3858               ((and gnus-thread-ignore-subject
3859                     gnus-tmp-prev-subject
3860                     (not (inline (gnus-subject-equal
3861                                   gnus-tmp-prev-subject subject))))
3862                subject)
3863               ((zerop gnus-tmp-level)
3864                (if (and (eq gnus-summary-make-false-root 'empty)
3865                         (memq number gnus-tmp-gathered)
3866                         gnus-tmp-prev-subject
3867                         (inline (gnus-subject-equal
3868                                  gnus-tmp-prev-subject subject)))
3869                    gnus-summary-same-subject
3870                  subject))
3871               (t gnus-summary-same-subject)))
3872             (if (and (eq gnus-summary-make-false-root 'adopt)
3873                      (= gnus-tmp-level 1)
3874                      (memq number gnus-tmp-gathered))
3875                 (setq gnus-tmp-opening-bracket ?\<
3876                       gnus-tmp-closing-bracket ?\>)
3877               (setq gnus-tmp-opening-bracket ?\[
3878                     gnus-tmp-closing-bracket ?\]))
3879             (setq
3880              gnus-tmp-indentation
3881              (aref gnus-thread-indent-array gnus-tmp-level)
3882              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3883              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3884                                 gnus-summary-default-score 0)
3885              gnus-tmp-score-char
3886              (if (or (null gnus-summary-default-score)
3887                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3888                          gnus-summary-zcore-fuzz))
3889                  ? ;Whitespace
3890                (if (< gnus-tmp-score gnus-summary-default-score)
3891                    gnus-score-below-mark gnus-score-over-mark))
3892              gnus-tmp-replied
3893              (cond ((memq number gnus-newsgroup-processable)
3894                     gnus-process-mark)
3895                    ((memq number gnus-newsgroup-cached)
3896                     gnus-cached-mark)
3897                    ((memq number gnus-newsgroup-replied)
3898                     gnus-replied-mark)
3899                    ((memq number gnus-newsgroup-saved)
3900                     gnus-saved-mark)
3901                    (t gnus-unread-mark))
3902              gnus-tmp-from (mail-header-from gnus-tmp-header)
3903              gnus-tmp-name
3904              (cond
3905               ((string-match "<[^>]+> *$" gnus-tmp-from)
3906                (setq beg-match (match-beginning 0))
3907                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3908                         (substring gnus-tmp-from (1+ (match-beginning 0))
3909                                    (1- (match-end 0))))
3910                    (substring gnus-tmp-from 0 beg-match)))
3911               ((string-match "(.+)" gnus-tmp-from)
3912                (substring gnus-tmp-from
3913                           (1+ (match-beginning 0)) (1- (match-end 0))))
3914               (t gnus-tmp-from)))
3915             (when (string= gnus-tmp-name "")
3916               (setq gnus-tmp-name gnus-tmp-from))
3917             (unless (numberp gnus-tmp-lines)
3918               (setq gnus-tmp-lines 0))
3919             (gnus-put-text-property
3920              (point)
3921              (progn (eval gnus-summary-line-format-spec) (point))
3922              'gnus-number number)
3923             (when gnus-visual-p
3924               (forward-line -1)
3925               (gnus-run-hooks 'gnus-summary-update-hook)
3926               (forward-line 1))
3927
3928             (setq gnus-tmp-prev-subject subject)))
3929
3930         (when (nth 1 thread)
3931           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3932         (incf gnus-tmp-level)
3933         (setq threads (if thread-end nil (cdar thread)))
3934         (unless threads
3935           (setq gnus-tmp-level 0)))))
3936   (gnus-message 7 "Generating summary...done"))
3937
3938 (defun gnus-summary-prepare-unthreaded (headers)
3939   "Generate an unthreaded summary buffer based on HEADERS."
3940   (let (header number mark)
3941
3942     (beginning-of-line)
3943
3944     (while headers
3945       ;; We may have to root out some bad articles...
3946       (when (memq (setq number (mail-header-number
3947                                 (setq header (pop headers))))
3948                   gnus-newsgroup-limit)
3949         ;; Mark article as read when it has a low score.
3950         (when (and gnus-summary-mark-below
3951                    (< (or (cdr (assq number gnus-newsgroup-scored))
3952                           gnus-summary-default-score 0)
3953                       gnus-summary-mark-below)
3954                    (not (gnus-summary-article-ancient-p number)))
3955           (setq gnus-newsgroup-unreads
3956                 (delq number gnus-newsgroup-unreads))
3957           (if gnus-newsgroup-auto-expire
3958               (push number gnus-newsgroup-expirable)
3959             (push (cons number gnus-low-score-mark)
3960                   gnus-newsgroup-reads)))
3961
3962         (setq mark (gnus-article-mark number))
3963         (push (gnus-data-make number mark (1+ (point)) header 0)
3964               gnus-newsgroup-data)
3965         (gnus-summary-insert-line
3966          header 0 number
3967          mark (memq number gnus-newsgroup-replied)
3968          (memq number gnus-newsgroup-expirable)
3969          (mail-header-subject header) nil
3970          (cdr (assq number gnus-newsgroup-scored))
3971          (memq number gnus-newsgroup-processable))))))
3972
3973 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3974   "Select newsgroup GROUP.
3975 If READ-ALL is non-nil, all articles in the group are selected.
3976 If SELECT-ARTICLES, only select those articles from GROUP."
3977   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3978          ;;!!! Dirty hack; should be removed.
3979          (gnus-summary-ignore-duplicates
3980           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3981               t
3982             gnus-summary-ignore-duplicates))
3983          (info (nth 2 entry))
3984          articles fetched-articles cached)
3985
3986     (unless (gnus-check-server
3987              (setq gnus-current-select-method
3988                    (gnus-find-method-for-group group)))
3989       (error "Couldn't open server"))
3990
3991     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3992         (gnus-activate-group group)     ; Or we can activate it...
3993         (progn                          ; Or we bug out.
3994           (when (equal major-mode 'gnus-summary-mode)
3995             (kill-buffer (current-buffer)))
3996           (error "Couldn't request group %s: %s"
3997                  group (gnus-status-message group))))
3998
3999     (unless (gnus-request-group group t)
4000       (when (equal major-mode 'gnus-summary-mode)
4001         (kill-buffer (current-buffer)))
4002       (error "Couldn't request group %s: %s"
4003              group (gnus-status-message group)))
4004
4005     (setq gnus-newsgroup-name group)
4006     (setq gnus-newsgroup-unselected nil)
4007     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4008     (gnus-summary-setup-default-charset)
4009
4010     ;; Adjust and set lists of article marks.
4011     (when info
4012       (gnus-adjust-marked-articles info))
4013
4014     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4015     (setq cached
4016           (if (gnus-virtual-group-p group)
4017               gnus-newsgroup-cached
4018             (gnus-cache-articles-in-group group)))
4019
4020     (setq gnus-newsgroup-unreads
4021           (gnus-set-difference
4022            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4023            gnus-newsgroup-dormant))
4024
4025     (setq gnus-newsgroup-processable nil)
4026
4027     (gnus-update-read-articles group gnus-newsgroup-unreads)
4028
4029     (if (setq articles select-articles)
4030         (setq gnus-newsgroup-unselected
4031               (gnus-sorted-intersection
4032                gnus-newsgroup-unreads
4033                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4034       (setq articles (gnus-articles-to-read group read-all)))
4035
4036     (cond
4037      ((null articles)
4038       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4039       'quit)
4040      ((eq articles 0) nil)
4041      (t
4042       ;; Init the dependencies hash table.
4043       (setq gnus-newsgroup-dependencies
4044             (gnus-make-hashtable (length articles)))
4045       (gnus-set-global-variables)
4046       ;; Retrieve the headers and read them in.
4047       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4048       (setq gnus-newsgroup-headers
4049             (gnus-retrieve-parsed-headers
4050              articles gnus-newsgroup-name
4051              ;; We might want to fetch old headers, but
4052              ;; not if there is only 1 article.
4053              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4054                            (not (numberp gnus-fetch-old-headers)))
4055                       (> (length articles) 1))
4056                   gnus-fetch-old-headers)))
4057       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4058
4059       ;; Suppress duplicates?
4060       (when gnus-suppress-duplicates
4061         (gnus-dup-suppress-articles))
4062
4063       ;; Set the initial limit.
4064       (setq gnus-newsgroup-limit (copy-sequence articles))
4065       ;; Remove canceled articles from the list of unread articles.
4066       (setq gnus-newsgroup-unreads
4067             (gnus-set-sorted-intersection
4068              gnus-newsgroup-unreads
4069              (setq fetched-articles
4070                    (mapcar (lambda (headers) (mail-header-number headers))
4071                            gnus-newsgroup-headers))))
4072       ;; Removed marked articles that do not exist.
4073       (gnus-update-missing-marks
4074        (gnus-sorted-complement fetched-articles articles))
4075
4076       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4077       (when cached
4078         (setq gnus-newsgroup-cached cached))
4079
4080       ;; We might want to build some more threads first.
4081       (when (and gnus-fetch-old-headers
4082                  (eq gnus-headers-retrieved-by 'nov))
4083         (if (eq gnus-fetch-old-headers 'invisible)
4084             (gnus-build-all-threads)
4085           (gnus-build-old-threads)))
4086       ;; Let the Gnus agent mark articles as read.
4087       (when gnus-agent
4088         (gnus-agent-get-undownloaded-list))
4089       ;; Check whether auto-expire is to be done in this group.
4090       (setq gnus-newsgroup-auto-expire
4091             (gnus-group-auto-expirable-p group))
4092       ;; Set up the article buffer now, if necessary.
4093       (unless gnus-single-article-buffer
4094         (gnus-article-setup-buffer))
4095       ;; First and last article in this newsgroup.
4096       (when gnus-newsgroup-headers
4097         (setq gnus-newsgroup-begin
4098               (mail-header-number (car gnus-newsgroup-headers))
4099               gnus-newsgroup-end
4100               (mail-header-number
4101                (gnus-last-element gnus-newsgroup-headers))))
4102       ;; GROUP is successfully selected.
4103       (or gnus-newsgroup-headers t)))))
4104
4105 (defun gnus-articles-to-read (group &optional read-all)
4106   ;; Find out what articles the user wants to read.
4107   (let* ((articles
4108           ;; Select all articles if `read-all' is non-nil, or if there
4109           ;; are no unread articles.
4110           (if (or read-all
4111                   (and (zerop (length gnus-newsgroup-marked))
4112                        (zerop (length gnus-newsgroup-unreads)))
4113                   (eq (gnus-group-find-parameter group 'display)
4114                       'all))
4115               (gnus-uncompress-range (gnus-active group))
4116             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4117                           (copy-sequence gnus-newsgroup-unreads))
4118                   '<)))
4119          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4120          (scored (length scored-list))
4121          (number (length articles))
4122          (marked (+ (length gnus-newsgroup-marked)
4123                     (length gnus-newsgroup-dormant)))
4124          (select
4125           (cond
4126            ((numberp read-all)
4127             read-all)
4128            (t
4129             (condition-case ()
4130                 (cond
4131                  ((and (or (<= scored marked) (= scored number))
4132                        (natnump gnus-large-newsgroup)
4133                        (> number gnus-large-newsgroup))
4134                   (let* ((minibuffer-setup-hook (append
4135                                                  minibuffer-setup-hook
4136                                                  '(beginning-of-line)))
4137                          (input (read-string
4138                                  (format
4139                                   "How many articles from %s (max %d): "
4140                                   (gnus-limit-string gnus-newsgroup-name 35)
4141                                   number)
4142                                  (number-to-string gnus-large-newsgroup))))
4143                     (if (string-match "^[ \t]*$" input)
4144                         number
4145                       input)))
4146                  ((and (> scored marked) (< scored number)
4147                        (> (- scored number) 20))
4148                   (let ((input
4149                          (read-string
4150                           (format "%s %s (%d scored, %d total): "
4151                                   "How many articles from"
4152                                   group scored number))))
4153                     (if (string-match "^[ \t]*$" input)
4154                         number input)))
4155                  (t number))
4156               (quit nil))))))
4157     (setq select (if (stringp select) (string-to-number select) select))
4158     (if (or (null select) (zerop select))
4159         select
4160       (if (and (not (zerop scored)) (<= (abs select) scored))
4161           (progn
4162             (setq articles (sort scored-list '<))
4163             (setq number (length articles)))
4164         (setq articles (copy-sequence articles)))
4165
4166       (when (< (abs select) number)
4167         (if (< select 0)
4168             ;; Select the N oldest articles.
4169             (setcdr (nthcdr (1- (abs select)) articles) nil)
4170           ;; Select the N most recent articles.
4171           (setq articles (nthcdr (- number select) articles))))
4172       (setq gnus-newsgroup-unselected
4173             (gnus-sorted-intersection
4174              gnus-newsgroup-unreads
4175              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4176       articles)))
4177
4178 (defun gnus-killed-articles (killed articles)
4179   (let (out)
4180     (while articles
4181       (when (inline (gnus-member-of-range (car articles) killed))
4182         (push (car articles) out))
4183       (setq articles (cdr articles)))
4184     out))
4185
4186 (defun gnus-uncompress-marks (marks)
4187   "Uncompress the mark ranges in MARKS."
4188   (let ((uncompressed '(score bookmark))
4189         out)
4190     (while marks
4191       (if (memq (caar marks) uncompressed)
4192           (push (car marks) out)
4193         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4194       (setq marks (cdr marks)))
4195     out))
4196
4197 (defun gnus-adjust-marked-articles (info)
4198   "Set all article lists and remove all marks that are no longer valid."
4199   (let* ((marked-lists (gnus-info-marks info))
4200          (active (gnus-active (gnus-info-group info)))
4201          (min (car active))
4202          (max (cdr active))
4203          (types gnus-article-mark-lists)
4204          (uncompressed '(score bookmark killed))
4205          marks var articles article mark)
4206
4207     (while marked-lists
4208       (setq marks (pop marked-lists))
4209       (set (setq var (intern (format "gnus-newsgroup-%s"
4210                                      (car (rassq (setq mark (car marks))
4211                                                  types)))))
4212            (if (memq (car marks) uncompressed) (cdr marks)
4213              (gnus-uncompress-range (cdr marks))))
4214
4215       (setq articles (symbol-value var))
4216
4217       ;; All articles have to be subsets of the active articles.
4218       (cond
4219        ;; Adjust "simple" lists.
4220        ((memq mark '(tick dormant expire reply save))
4221         (while articles
4222           (when (or (< (setq article (pop articles)) min) (> article max))
4223             (set var (delq article (symbol-value var))))))
4224        ;; Adjust assocs.
4225        ((memq mark uncompressed)
4226         (when (not (listp (cdr (symbol-value var))))
4227           (set var (list (symbol-value var))))
4228         (when (not (listp (cdr articles)))
4229           (setq articles (list articles)))
4230         (while articles
4231           (when (or (not (consp (setq article (pop articles))))
4232                     (< (car article) min)
4233                     (> (car article) max))
4234             (set var (delq article (symbol-value var))))))))))
4235
4236 (defun gnus-update-missing-marks (missing)
4237   "Go through the list of MISSING articles and remove them from the mark lists."
4238   (when missing
4239     (let ((types gnus-article-mark-lists)
4240           var m)
4241       ;; Go through all types.
4242       (while types
4243         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4244         (when (symbol-value var)
4245           ;; This list has articles.  So we delete all missing articles
4246           ;; from it.
4247           (setq m missing)
4248           (while m
4249             (set var (delq (pop m) (symbol-value var)))))))))
4250
4251 (defun gnus-update-marks ()
4252   "Enter the various lists of marked articles into the newsgroup info list."
4253   (let ((types gnus-article-mark-lists)
4254         (info (gnus-get-info gnus-newsgroup-name))
4255         (uncompressed '(score bookmark killed))
4256         type list newmarked symbol delta-marks)
4257     (when info
4258       ;; Add all marks lists to the list of marks lists.
4259       (while (setq type (pop types))
4260         (setq list (symbol-value
4261                           (setq symbol
4262                                 (intern (format "gnus-newsgroup-%s"
4263                                                 (car type))))))
4264
4265         (when list
4266           ;; Get rid of the entries of the articles that have the
4267           ;; default score.
4268           (when (and (eq (cdr type) 'score)
4269                      gnus-save-score
4270                      list)
4271             (let* ((arts list)
4272                    (prev (cons nil list))
4273                    (all prev))
4274               (while arts
4275                 (if (or (not (consp (car arts)))
4276                         (= (cdar arts) gnus-summary-default-score))
4277                     (setcdr prev (cdr arts))
4278                   (setq prev arts))
4279                 (setq arts (cdr arts)))
4280               (setq list (cdr all)))))
4281
4282        (or (memq (cdr type) uncompressed)
4283            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4284        
4285        (when (gnus-check-backend-function 'request-set-mark
4286                                           gnus-newsgroup-name)
4287          ;; uncompressed:s are not proper flags (they are cons cells)
4288          ;; cache is a internal gnus flag
4289          (unless (memq (cdr type) (cons 'cache uncompressed))
4290            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4291                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4292                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4293              (if add
4294                  (push (list add 'add (list (cdr type))) delta-marks))
4295              (if del
4296                  (push (list del 'del (list (cdr type))) delta-marks)))))
4297           
4298         (when list
4299          (push (cons (cdr type) list) newmarked)))
4300
4301       (when delta-marks
4302         (unless (gnus-check-group gnus-newsgroup-name)
4303           (error "Can't open server for %s" gnus-newsgroup-name))
4304         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4305           
4306       ;; Enter these new marks into the info of the group.
4307       (if (nthcdr 3 info)
4308           (setcar (nthcdr 3 info) newmarked)
4309         ;; Add the marks lists to the end of the info.
4310         (when newmarked
4311           (setcdr (nthcdr 2 info) (list newmarked))))
4312
4313       ;; Cut off the end of the info if there's nothing else there.
4314       (let ((i 5))
4315         (while (and (> i 2)
4316                     (not (nth i info)))
4317           (when (nthcdr (decf i) info)
4318             (setcdr (nthcdr i info) nil)))))))
4319
4320 (defun gnus-set-mode-line (where)
4321   "This function sets the mode line of the article or summary buffers.
4322 If WHERE is `summary', the summary mode line format will be used."
4323   ;; Is this mode line one we keep updated?
4324   (when (and (memq where gnus-updated-mode-lines)
4325              (symbol-value
4326               (intern (format "gnus-%s-mode-line-format-spec" where))))
4327     (let (mode-string)
4328       (save-excursion
4329         ;; We evaluate this in the summary buffer since these
4330         ;; variables are buffer-local to that buffer.
4331         (set-buffer gnus-summary-buffer)
4332         ;; We bind all these variables that are used in the `eval' form
4333         ;; below.
4334         (let* ((mformat (symbol-value
4335                          (intern
4336                           (format "gnus-%s-mode-line-format-spec" where))))
4337                (gnus-tmp-group-name gnus-newsgroup-name)
4338                (gnus-tmp-article-number (or gnus-current-article 0))
4339                (gnus-tmp-unread gnus-newsgroup-unreads)
4340                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4341                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4342                (gnus-tmp-unread-and-unselected
4343                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4344                             (zerop gnus-tmp-unselected))
4345                        "")
4346                       ((zerop gnus-tmp-unselected)
4347                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4348                       (t (format "{%d(+%d) more}"
4349                                  gnus-tmp-unread-and-unticked
4350                                  gnus-tmp-unselected))))
4351                (gnus-tmp-subject
4352                 (if (and gnus-current-headers
4353                          (vectorp gnus-current-headers))
4354                     (gnus-mode-string-quote
4355                      (mail-header-subject gnus-current-headers))
4356                   ""))
4357                bufname-length max-len
4358                gnus-tmp-header);; passed as argument to any user-format-funcs
4359           (setq mode-string (eval mformat))
4360           (setq bufname-length (if (string-match "%b" mode-string)
4361                                    (- (length
4362                                        (buffer-name
4363                                         (if (eq where 'summary)
4364                                             nil
4365                                           (get-buffer gnus-article-buffer))))
4366                                       2)
4367                                  0))
4368           (setq max-len (max 4 (if gnus-mode-non-string-length
4369                                    (- (window-width)
4370                                       gnus-mode-non-string-length
4371                                       bufname-length)
4372                                  (length mode-string))))
4373           ;; We might have to chop a bit of the string off...
4374           (when (> (length mode-string) max-len)
4375             (setq mode-string
4376                   (concat (gnus-truncate-string mode-string (- max-len 3))
4377                           "...")))
4378           ;; Pad the mode string a bit.
4379           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4380       ;; Update the mode line.
4381       (setq mode-line-buffer-identification
4382             (gnus-mode-line-buffer-identification (list mode-string)))
4383       (set-buffer-modified-p t))))
4384
4385 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4386   "Go through the HEADERS list and add all Xrefs to a hash table.
4387 The resulting hash table is returned, or nil if no Xrefs were found."
4388   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4389          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4390          (xref-hashtb (gnus-make-hashtable))
4391          start group entry number xrefs header)
4392     (while headers
4393       (setq header (pop headers))
4394       (when (and (setq xrefs (mail-header-xref header))
4395                  (not (memq (setq number (mail-header-number header))
4396                             unreads)))
4397         (setq start 0)
4398         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4399           (setq start (match-end 0))
4400           (setq group (if prefix
4401                           (concat prefix (substring xrefs (match-beginning 1)
4402                                                     (match-end 1)))
4403                         (substring xrefs (match-beginning 1) (match-end 1))))
4404           (setq number
4405                 (string-to-int (substring xrefs (match-beginning 2)
4406                                           (match-end 2))))
4407           (if (setq entry (gnus-gethash group xref-hashtb))
4408               (setcdr entry (cons number (cdr entry)))
4409             (gnus-sethash group (cons number nil) xref-hashtb)))))
4410     (and start xref-hashtb)))
4411
4412 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4413   "Look through all the headers and mark the Xrefs as read."
4414   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4415         name entry info xref-hashtb idlist method nth4)
4416     (save-excursion
4417       (set-buffer gnus-group-buffer)
4418       (when (setq xref-hashtb
4419                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4420         (mapatoms
4421          (lambda (group)
4422            (unless (string= from-newsgroup (setq name (symbol-name group)))
4423              (setq idlist (symbol-value group))
4424              ;; Dead groups are not updated.
4425              (and (prog1
4426                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4427                             info (nth 2 entry))
4428                     (when (stringp (setq nth4 (gnus-info-method info)))
4429                       (setq nth4 (gnus-server-to-method nth4))))
4430                   ;; Only do the xrefs if the group has the same
4431                   ;; select method as the group we have just read.
4432                   (or (gnus-methods-equal-p
4433                        nth4 (gnus-find-method-for-group from-newsgroup))
4434                       virtual
4435                       (equal nth4 (setq method (gnus-find-method-for-group
4436                                                 from-newsgroup)))
4437                       (and (equal (car nth4) (car method))
4438                            (equal (nth 1 nth4) (nth 1 method))))
4439                   gnus-use-cross-reference
4440                   (or (not (eq gnus-use-cross-reference t))
4441                       virtual
4442                       ;; Only do cross-references on subscribed
4443                       ;; groups, if that is what is wanted.
4444                       (<= (gnus-info-level info) gnus-level-subscribed))
4445                   (gnus-group-make-articles-read name idlist))))
4446          xref-hashtb)))))
4447
4448 (defun gnus-compute-read-articles (group articles)
4449   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4450          (info (nth 2 entry))
4451          (active (gnus-active group))
4452          ninfo)
4453     (when entry
4454       ;; First peel off all invalid article numbers.
4455       (when active
4456         (let ((ids articles)
4457               id first)
4458           (while (setq id (pop ids))
4459             (when (and first (> id (cdr active)))
4460               ;; We'll end up in this situation in one particular
4461               ;; obscure situation.  If you re-scan a group and get
4462               ;; a new article that is cross-posted to a different
4463               ;; group that has not been re-scanned, you might get
4464               ;; crossposted article that has a higher number than
4465               ;; Gnus believes possible.  So we re-activate this
4466               ;; group as well.  This might mean doing the
4467               ;; crossposting thingy will *increase* the number
4468               ;; of articles in some groups.  Tsk, tsk.
4469               (setq active (or (gnus-activate-group group) active)))
4470             (when (or (> id (cdr active))
4471                       (< id (car active)))
4472               (setq articles (delq id articles))))))
4473       ;; If the read list is nil, we init it.
4474       (if (and active
4475                (null (gnus-info-read info))
4476                (> (car active) 1))
4477           (setq ninfo (cons 1 (1- (car active))))
4478         (setq ninfo (gnus-info-read info)))
4479       ;; Then we add the read articles to the range.
4480       (gnus-add-to-range
4481        ninfo (setq articles (sort articles '<))))))
4482
4483 (defun gnus-group-make-articles-read (group articles)
4484   "Update the info of GROUP to say that ARTICLES are read."
4485   (let* ((num 0)
4486          (entry (gnus-gethash group gnus-newsrc-hashtb))
4487          (info (nth 2 entry))
4488          (active (gnus-active group))
4489          range)
4490     (when entry
4491       (setq range (gnus-compute-read-articles group articles))
4492       (save-excursion
4493         (set-buffer gnus-group-buffer)
4494         (gnus-undo-register
4495           `(progn
4496              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4497              (gnus-info-set-read ',info ',(gnus-info-read info))
4498              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4499              (gnus-group-update-group ,group t))))
4500       ;; Add the read articles to the range.
4501       (gnus-info-set-read info range)
4502       ;; Then we have to re-compute how many unread
4503       ;; articles there are in this group.
4504       (when active
4505         (cond
4506          ((not range)
4507           (setq num (- (1+ (cdr active)) (car active))))
4508          ((not (listp (cdr range)))
4509           (setq num (- (cdr active) (- (1+ (cdr range))
4510                                        (car range)))))
4511          (t
4512           (while range
4513             (if (numberp (car range))
4514                 (setq num (1+ num))
4515               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4516             (setq range (cdr range)))
4517           (setq num (- (cdr active) num))))
4518         ;; Update the number of unread articles.
4519         (setcar entry num)
4520         ;; Update the group buffer.
4521         (gnus-group-update-group group t)))))
4522
4523 (defvar gnus-newsgroup-none-id 0)
4524
4525 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4526   (let ((cur nntp-server-buffer)
4527         (dependencies
4528          (or dependencies
4529              (save-excursion (set-buffer gnus-summary-buffer)
4530                              gnus-newsgroup-dependencies)))
4531         headers id end ref
4532         (mail-parse-charset gnus-newsgroup-charset)
4533         (mail-parse-ignored-charsets
4534          (or (and (buffer-live-p gnus-summary-buffer)
4535                   (save-excursion (set-buffer gnus-summary-buffer)
4536                                   gnus-newsgroup-ignored-charsets))
4537              gnus-newsgroup-ignored-charsets)))
4538     (save-excursion
4539       (set-buffer nntp-server-buffer)
4540       ;; Translate all TAB characters into SPACE characters.
4541       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4542       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4543       (gnus-run-hooks 'gnus-parse-headers-hook)
4544       (let ((case-fold-search t)
4545             in-reply-to header p lines chars ctype)
4546         (goto-char (point-min))
4547         ;; Search to the beginning of the next header.  Error messages
4548         ;; do not begin with 2 or 3.
4549         (while (re-search-forward "^[23][0-9]+ " nil t)
4550           (setq id nil
4551                 ref nil)
4552           ;; This implementation of this function, with nine
4553           ;; search-forwards instead of the one re-search-forward and
4554           ;; a case (which basically was the old function) is actually
4555           ;; about twice as fast, even though it looks messier.  You
4556           ;; can't have everything, I guess.  Speed and elegance
4557           ;; doesn't always go hand in hand.
4558           (setq
4559            header
4560            (make-full-mail-header
4561             ;; Number.
4562             (prog1
4563                 (read cur)
4564               (end-of-line)
4565               (setq p (point))
4566               (narrow-to-region (point)
4567                                 (or (and (search-forward "\n.\n" nil t)
4568                                          (- (point) 2))
4569                                     (point))))
4570             ;; Subject.
4571             (progn
4572               (goto-char p)
4573               (if (search-forward "\nsubject: " nil t)
4574                   (buffer-substring (match-end 0) (std11-field-end))
4575                 "(none)"))
4576             ;; From.
4577             (progn
4578               (goto-char p)
4579               (if (search-forward "\nfrom: " nil t)
4580                   (buffer-substring (match-end 0) (std11-field-end))
4581                 "(nobody)"))
4582             ;; Date.
4583             (progn
4584               (goto-char p)
4585               (if (search-forward "\ndate: " nil t)
4586                   (buffer-substring (match-end 0) (std11-field-end))
4587                 ""))
4588             ;; Message-ID.
4589             (progn
4590               (goto-char p)
4591               (setq id (if (re-search-forward
4592                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4593                            ;; We do it this way to make sure the Message-ID
4594                            ;; is (somewhat) syntactically valid.
4595                            (buffer-substring (match-beginning 1)
4596                                              (match-end 1))
4597                          ;; If there was no message-id, we just fake one
4598                          ;; to make subsequent routines simpler.
4599                          (nnheader-generate-fake-message-id))))
4600             ;; References.
4601             (progn
4602               (goto-char p)
4603               (if (search-forward "\nreferences: " nil t)
4604                   (progn
4605                     (setq end (point))
4606                     (prog1
4607                         (buffer-substring (match-end 0) (std11-field-end))
4608                       (setq ref
4609                             (buffer-substring
4610                              (progn
4611                                ;; (end-of-line)
4612                                (search-backward ">" end t)
4613                                (1+ (point)))
4614                              (progn
4615                                (search-backward "<" end t)
4616                                (point))))))
4617                 ;; Get the references from the in-reply-to header if there
4618                 ;; were no references and the in-reply-to header looks
4619                 ;; promising.
4620                 (if (and (search-forward "\nin-reply-to: " nil t)
4621                          (setq in-reply-to
4622                                (buffer-substring (match-end 0)
4623                                                  (std11-field-end)))
4624                          (string-match "<[^>]+>" in-reply-to))
4625                     (let (ref2)
4626                       (setq ref (substring in-reply-to (match-beginning 0)
4627                                            (match-end 0)))
4628                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4629                         (setq ref2 (substring in-reply-to (match-beginning 0)
4630                                               (match-end 0)))
4631                         (when (> (length ref2) (length ref))
4632                           (setq ref ref2)))
4633                       ref)
4634                   (setq ref nil))))
4635             ;; Chars.
4636             (progn
4637               (goto-char p)
4638               (if (search-forward "\nchars: " nil t)
4639                   (if (numberp (setq chars (ignore-errors (read cur))))
4640                       chars 0)
4641                 0))
4642             ;; Lines.
4643             (progn
4644               (goto-char p)
4645               (if (search-forward "\nlines: " nil t)
4646                   (if (numberp (setq lines (ignore-errors (read cur))))
4647                       lines 0)
4648                 0))
4649             ;; Xref.
4650             (progn
4651               (goto-char p)
4652               (and (search-forward "\nxref: " nil t)
4653                    (buffer-substring (match-end 0) (std11-field-end))))
4654             ;; Extra.
4655             (when gnus-extra-headers
4656               (let ((extra gnus-extra-headers)
4657                     out)
4658                 (while extra
4659                   (goto-char p)
4660                   (when (search-forward
4661                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4662                     (push (cons (car extra)
4663                                 (buffer-substring (match-end 0)
4664                                                   (std11-field-end)))
4665                           out))
4666                   (pop extra))
4667                 out))))
4668           (goto-char p)
4669           (if (and (search-forward "\ncontent-type: " nil t)
4670                    (setq ctype
4671                          (buffer-substring (match-end 0) (std11-field-end))))
4672               (mime-entity-set-content-type-internal
4673                header (mime-parse-Content-Type ctype)))
4674           (when (equal id ref)
4675             (setq ref nil))
4676
4677           (when gnus-alter-header-function
4678             (funcall gnus-alter-header-function header)
4679             (setq id (mail-header-id header)
4680                   ref (gnus-parent-id (mail-header-references header))))
4681
4682           (when (setq header
4683                       (gnus-dependencies-add-header
4684                        header dependencies force-new))
4685             (push header headers))
4686           (goto-char (point-max))
4687           (widen))
4688         (nreverse headers)))))
4689
4690 ;; Goes through the xover lines and returns a list of vectors
4691 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4692                                                   force-new dependencies
4693                                                   group also-fetch-heads)
4694   "Parse the news overview data in the server buffer, and return a
4695 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4696   ;; Get the Xref when the users reads the articles since most/some
4697   ;; NNTP servers do not include Xrefs when using XOVER.
4698   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4699   (let ((mail-parse-charset gnus-newsgroup-charset)
4700         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4701         (cur nntp-server-buffer)
4702         (dependencies (or dependencies gnus-newsgroup-dependencies))
4703         number headers header)
4704     (save-excursion
4705       (set-buffer nntp-server-buffer)
4706       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4707       ;; Allow the user to mangle the headers before parsing them.
4708       (gnus-run-hooks 'gnus-parse-headers-hook)
4709       (goto-char (point-min))
4710       (while (not (eobp))
4711         (condition-case ()
4712             (while (and sequence (not (eobp)))
4713               (setq number (read cur))
4714               (while (and sequence
4715                           (< (car sequence) number))
4716                 (setq sequence (cdr sequence)))
4717               (and sequence
4718                    (eq number (car sequence))
4719                    (progn
4720                      (setq sequence (cdr sequence))
4721                      (setq header (inline
4722                                     (gnus-nov-parse-line
4723                                      number dependencies force-new))))
4724                    (push header headers))
4725               (forward-line 1))
4726           (error
4727            (gnus-error 4 "Strange nov line (%d)"
4728                        (count-lines (point-min) (point)))))
4729         (forward-line 1))
4730       ;; A common bug in inn is that if you have posted an article and
4731       ;; then retrieves the active file, it will answer correctly --
4732       ;; the new article is included.  However, a NOV entry for the
4733       ;; article may not have been generated yet, so this may fail.
4734       ;; We work around this problem by retrieving the last few
4735       ;; headers using HEAD.
4736       (if (or (not also-fetch-heads)
4737               (not sequence))
4738           ;; We (probably) got all the headers.
4739           (nreverse headers)
4740         (let ((gnus-nov-is-evil t))
4741           (nconc
4742            (nreverse headers)
4743            (gnus-retrieve-parsed-headers sequence group)
4744            ))))))
4745
4746 (defun gnus-article-get-xrefs ()
4747   "Fill in the Xref value in `gnus-current-headers', if necessary.
4748 This is meant to be called in `gnus-article-internal-prepare-hook'."
4749   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4750                                  gnus-current-headers)))
4751     (or (not gnus-use-cross-reference)
4752         (not headers)
4753         (and (mail-header-xref headers)
4754              (not (string= (mail-header-xref headers) "")))
4755         (let ((case-fold-search t)
4756               xref)
4757           (save-restriction
4758             (nnheader-narrow-to-headers)
4759             (goto-char (point-min))
4760             (when (or (and (eq (downcase (char-after)) ?x)
4761                            (looking-at "Xref:"))
4762                       (search-forward "\nXref:" nil t))
4763               (goto-char (1+ (match-end 0)))
4764               (setq xref (buffer-substring (point)
4765                                            (progn (end-of-line) (point))))
4766               (mail-header-set-xref headers xref)))))))
4767
4768 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4769   "Find article ID and insert the summary line for that article.
4770 OLD-HEADER can either be a header or a line number to insert
4771 the subject line on."
4772   (let* ((line (and (numberp old-header) old-header))
4773          (old-header (and (vectorp old-header) old-header))
4774          (header (cond ((and old-header use-old-header)
4775                         old-header)
4776                        ((and (numberp id)
4777                              (gnus-number-to-header id))
4778                         (gnus-number-to-header id))
4779                        (t
4780                         (gnus-read-header id))))
4781          (number (and (numberp id) id))
4782          d)
4783     (when header
4784       ;; Rebuild the thread that this article is part of and go to the
4785       ;; article we have fetched.
4786       (when (and (not gnus-show-threads)
4787                  old-header)
4788         (when (and number
4789                    (setq d (gnus-data-find (mail-header-number old-header))))
4790           (goto-char (gnus-data-pos d))
4791           (gnus-data-remove
4792            number
4793            (- (gnus-point-at-bol)
4794               (prog1
4795                   (1+ (gnus-point-at-eol))
4796                 (gnus-delete-line))))))
4797       (when old-header
4798         (mail-header-set-number header (mail-header-number old-header)))
4799       (setq gnus-newsgroup-sparse
4800             (delq (setq number (mail-header-number header))
4801                   gnus-newsgroup-sparse))
4802       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4803       (push number gnus-newsgroup-limit)
4804       (gnus-rebuild-thread (mail-header-id header) line)
4805       (gnus-summary-goto-subject number nil t))
4806     (when (and (numberp number)
4807                (> number 0))
4808       ;; We have to update the boundaries even if we can't fetch the
4809       ;; article if ID is a number -- so that the next `P' or `N'
4810       ;; command will fetch the previous (or next) article even
4811       ;; if the one we tried to fetch this time has been canceled.
4812       (when (> number gnus-newsgroup-end)
4813         (setq gnus-newsgroup-end number))
4814       (when (< number gnus-newsgroup-begin)
4815         (setq gnus-newsgroup-begin number))
4816       (setq gnus-newsgroup-unselected
4817             (delq number gnus-newsgroup-unselected)))
4818     ;; Report back a success?
4819     (and header (mail-header-number header))))
4820
4821 ;;; Process/prefix in the summary buffer
4822
4823 (defun gnus-summary-work-articles (n)
4824   "Return a list of articles to be worked upon.
4825 The prefix argument, the list of process marked articles, and the
4826 current article will be taken into consideration."
4827   (save-excursion
4828     (set-buffer gnus-summary-buffer)
4829     (cond
4830      (n
4831       ;; A numerical prefix has been given.
4832       (setq n (prefix-numeric-value n))
4833       (let ((backward (< n 0))
4834             (n (abs (prefix-numeric-value n)))
4835             articles article)
4836         (save-excursion
4837           (while
4838               (and (> n 0)
4839                    (push (setq article (gnus-summary-article-number))
4840                          articles)
4841                    (if backward
4842                        (gnus-summary-find-prev nil article)
4843                      (gnus-summary-find-next nil article)))
4844             (decf n)))
4845         (nreverse articles)))
4846      ((and (gnus-region-active-p) (mark))
4847       (message "region active")
4848       ;; Work on the region between point and mark.
4849       (let ((max (max (point) (mark)))
4850             articles article)
4851         (save-excursion
4852           (goto-char (min (point) (mark)))
4853           (while
4854               (and
4855                (push (setq article (gnus-summary-article-number)) articles)
4856                (gnus-summary-find-next nil article)
4857                (< (point) max)))
4858           (nreverse articles))))
4859      (gnus-newsgroup-processable
4860       ;; There are process-marked articles present.
4861       ;; Save current state.
4862       (gnus-summary-save-process-mark)
4863       ;; Return the list.
4864       (reverse gnus-newsgroup-processable))
4865      (t
4866       ;; Just return the current article.
4867       (list (gnus-summary-article-number))))))
4868
4869 (defmacro gnus-summary-iterate (arg &rest forms)
4870   "Iterate over the process/prefixed articles and do FORMS.
4871 ARG is the interactive prefix given to the command.  FORMS will be
4872 executed with point over the summary line of the articles."
4873   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4874     `(let ((,articles (gnus-summary-work-articles ,arg)))
4875        (while ,articles
4876          (gnus-summary-goto-subject (car ,articles))
4877          ,@forms
4878          (pop ,articles)))))
4879
4880 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4881 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4882
4883 (defun gnus-summary-save-process-mark ()
4884   "Push the current set of process marked articles on the stack."
4885   (interactive)
4886   (push (copy-sequence gnus-newsgroup-processable)
4887         gnus-newsgroup-process-stack))
4888
4889 (defun gnus-summary-kill-process-mark ()
4890   "Push the current set of process marked articles on the stack and unmark."
4891   (interactive)
4892   (gnus-summary-save-process-mark)
4893   (gnus-summary-unmark-all-processable))
4894
4895 (defun gnus-summary-yank-process-mark ()
4896   "Pop the last process mark state off the stack and restore it."
4897   (interactive)
4898   (unless gnus-newsgroup-process-stack
4899     (error "Empty mark stack"))
4900   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4901
4902 (defun gnus-summary-process-mark-set (set)
4903   "Make SET into the current process marked articles."
4904   (gnus-summary-unmark-all-processable)
4905   (while set
4906     (gnus-summary-set-process-mark (pop set))))
4907
4908 ;;; Searching and stuff
4909
4910 (defun gnus-summary-search-group (&optional backward use-level)
4911   "Search for next unread newsgroup.
4912 If optional argument BACKWARD is non-nil, search backward instead."
4913   (save-excursion
4914     (set-buffer gnus-group-buffer)
4915     (when (gnus-group-search-forward
4916            backward nil (if use-level (gnus-group-group-level) nil))
4917       (gnus-group-group-name))))
4918
4919 (defun gnus-summary-best-group (&optional exclude-group)
4920   "Find the name of the best unread group.
4921 If EXCLUDE-GROUP, do not go to this group."
4922   (save-excursion
4923     (set-buffer gnus-group-buffer)
4924     (save-excursion
4925       (gnus-group-best-unread-group exclude-group))))
4926
4927 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4928   (if backward (gnus-summary-find-prev)
4929     (let* ((dummy (gnus-summary-article-intangible-p))
4930            (article (or article (gnus-summary-article-number)))
4931            (arts (gnus-data-find-list article))
4932            result)
4933       (when (and (not dummy)
4934                  (or (not gnus-summary-check-current)
4935                      (not unread)
4936                      (not (gnus-data-unread-p (car arts)))))
4937         (setq arts (cdr arts)))
4938       (when (setq result
4939                   (if unread
4940                       (progn
4941                         (while arts
4942                           (when (or (and undownloaded
4943                                          (eq gnus-undownloaded-mark
4944                                              (gnus-data-mark (car arts))))
4945                                     (gnus-data-unread-p (car arts)))
4946                             (setq result (car arts)
4947                                   arts nil))
4948                           (setq arts (cdr arts)))
4949                         result)
4950                     (car arts)))
4951         (goto-char (gnus-data-pos result))
4952         (gnus-data-number result)))))
4953
4954 (defun gnus-summary-find-prev (&optional unread article)
4955   (let* ((eobp (eobp))
4956          (article (or article (gnus-summary-article-number)))
4957          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4958          result)
4959     (when (and (not eobp)
4960                (or (not gnus-summary-check-current)
4961                    (not unread)
4962                    (not (gnus-data-unread-p (car arts)))))
4963       (setq arts (cdr arts)))
4964     (when (setq result
4965                 (if unread
4966                     (progn
4967                       (while arts
4968                         (when (gnus-data-unread-p (car arts))
4969                           (setq result (car arts)
4970                                 arts nil))
4971                         (setq arts (cdr arts)))
4972                       result)
4973                   (car arts)))
4974       (goto-char (gnus-data-pos result))
4975       (gnus-data-number result))))
4976
4977 (defun gnus-summary-find-subject (subject &optional unread backward article)
4978   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4979          (article (or article (gnus-summary-article-number)))
4980          (articles (gnus-data-list backward))
4981          (arts (gnus-data-find-list article articles))
4982          result)
4983     (when (or (not gnus-summary-check-current)
4984               (not unread)
4985               (not (gnus-data-unread-p (car arts))))
4986       (setq arts (cdr arts)))
4987     (while arts
4988       (and (or (not unread)
4989                (gnus-data-unread-p (car arts)))
4990            (vectorp (gnus-data-header (car arts)))
4991            (gnus-subject-equal
4992             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4993            (setq result (car arts)
4994                  arts nil))
4995       (setq arts (cdr arts)))
4996     (and result
4997          (goto-char (gnus-data-pos result))
4998          (gnus-data-number result))))
4999
5000 (defun gnus-summary-search-forward (&optional unread subject backward)
5001   "Search forward for an article.
5002 If UNREAD, look for unread articles.  If SUBJECT, look for
5003 articles with that subject.  If BACKWARD, search backward instead."
5004   (cond (subject (gnus-summary-find-subject subject unread backward))
5005         (backward (gnus-summary-find-prev unread))
5006         (t (gnus-summary-find-next unread))))
5007
5008 (defun gnus-recenter (&optional n)
5009   "Center point in window and redisplay frame.
5010 Also do horizontal recentering."
5011   (interactive "P")
5012   (when (and gnus-auto-center-summary
5013              (not (eq gnus-auto-center-summary 'vertical)))
5014     (gnus-horizontal-recenter))
5015   (recenter n))
5016
5017 (defun gnus-summary-recenter ()
5018   "Center point in the summary window.
5019 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5020 displayed, no centering will be performed."
5021   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5022   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5023   (let* ((top (cond ((< (window-height) 4) 0)
5024                     ((< (window-height) 7) 1)
5025                     (t (if (numberp gnus-auto-center-summary)
5026                            gnus-auto-center-summary
5027                          2))))
5028          (height (1- (window-height)))
5029          (bottom (save-excursion (goto-char (point-max))
5030                                  (forward-line (- height))
5031                                  (point)))
5032          (window (get-buffer-window (current-buffer))))
5033     ;; The user has to want it.
5034     (when gnus-auto-center-summary
5035       (when (get-buffer-window gnus-article-buffer)
5036         ;; Only do recentering when the article buffer is displayed,
5037         ;; Set the window start to either `bottom', which is the biggest
5038         ;; possible valid number, or the second line from the top,
5039         ;; whichever is the least.
5040         (set-window-start
5041          window (min bottom (save-excursion
5042                               (forward-line (- top)) (point)))))
5043       ;; Do horizontal recentering while we're at it.
5044       (when (and (get-buffer-window (current-buffer) t)
5045                  (not (eq gnus-auto-center-summary 'vertical)))
5046         (let ((selected (selected-window)))
5047           (select-window (get-buffer-window (current-buffer) t))
5048           (gnus-summary-position-point)
5049           (gnus-horizontal-recenter)
5050           (select-window selected))))))
5051
5052 (defun gnus-summary-jump-to-group (newsgroup)
5053   "Move point to NEWSGROUP in group mode buffer."
5054   ;; Keep update point of group mode buffer if visible.
5055   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5056       (save-window-excursion
5057         ;; Take care of tree window mode.
5058         (when (get-buffer-window gnus-group-buffer)
5059           (pop-to-buffer gnus-group-buffer))
5060         (gnus-group-jump-to-group newsgroup))
5061     (save-excursion
5062       ;; Take care of tree window mode.
5063       (if (get-buffer-window gnus-group-buffer)
5064           (pop-to-buffer gnus-group-buffer)
5065         (set-buffer gnus-group-buffer))
5066       (gnus-group-jump-to-group newsgroup))))
5067
5068 ;; This function returns a list of article numbers based on the
5069 ;; difference between the ranges of read articles in this group and
5070 ;; the range of active articles.
5071 (defun gnus-list-of-unread-articles (group)
5072   (let* ((read (gnus-info-read (gnus-get-info group)))
5073          (active (or (gnus-active group) (gnus-activate-group group)))
5074          (last (cdr active))
5075          first nlast unread)
5076     ;; If none are read, then all are unread.
5077     (if (not read)
5078         (setq first (car active))
5079       ;; If the range of read articles is a single range, then the
5080       ;; first unread article is the article after the last read
5081       ;; article.  Sounds logical, doesn't it?
5082       (if (not (listp (cdr read)))
5083           (setq first (max (car active) (1+ (cdr read))))
5084         ;; `read' is a list of ranges.
5085         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5086                                   (caar read)))
5087                   1)
5088           (setq first (car active)))
5089         (while read
5090           (when first
5091             (while (< first nlast)
5092               (push first unread)
5093               (setq first (1+ first))))
5094           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5095           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5096           (setq read (cdr read)))))
5097     ;; And add the last unread articles.
5098     (while (<= first last)
5099       (push first unread)
5100       (setq first (1+ first)))
5101     ;; Return the list of unread articles.
5102     (delq 0 (nreverse unread))))
5103
5104 (defun gnus-list-of-read-articles (group)
5105   "Return a list of unread, unticked and non-dormant articles."
5106   (let* ((info (gnus-get-info group))
5107          (marked (gnus-info-marks info))
5108          (active (gnus-active group)))
5109     (and info active
5110          (gnus-set-difference
5111           (gnus-sorted-complement
5112            (gnus-uncompress-range active)
5113            (gnus-list-of-unread-articles group))
5114           (append
5115            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5116            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5117
5118 ;; Various summary commands
5119
5120 (defun gnus-summary-select-article-buffer ()
5121   "Reconfigure windows to show article buffer."
5122   (interactive)
5123   (if (not (gnus-buffer-live-p gnus-article-buffer))
5124       (error "There is no article buffer for this summary buffer")
5125     (gnus-configure-windows 'article)
5126     (select-window (get-buffer-window gnus-article-buffer))))
5127
5128 (defun gnus-summary-universal-argument (arg)
5129   "Perform any operation on all articles that are process/prefixed."
5130   (interactive "P")
5131   (let ((articles (gnus-summary-work-articles arg))
5132         func article)
5133     (if (eq
5134          (setq
5135           func
5136           (key-binding
5137            (read-key-sequence
5138             (substitute-command-keys
5139              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5140              ))))
5141          'undefined)
5142         (gnus-error 1 "Undefined key")
5143       (save-excursion
5144         (while articles
5145           (gnus-summary-goto-subject (setq article (pop articles)))
5146           (let (gnus-newsgroup-processable)
5147             (command-execute func))
5148           (gnus-summary-remove-process-mark article)))))
5149   (gnus-summary-position-point))
5150
5151 (defun gnus-summary-toggle-truncation (&optional arg)
5152   "Toggle truncation of summary lines.
5153 With arg, turn line truncation on iff arg is positive."
5154   (interactive "P")
5155   (setq truncate-lines
5156         (if (null arg) (not truncate-lines)
5157           (> (prefix-numeric-value arg) 0)))
5158   (redraw-display))
5159
5160 (defun gnus-summary-reselect-current-group (&optional all rescan)
5161   "Exit and then reselect the current newsgroup.
5162 The prefix argument ALL means to select all articles."
5163   (interactive "P")
5164   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5165     (error "Ephemeral groups can't be reselected"))
5166   (let ((current-subject (gnus-summary-article-number))
5167         (group gnus-newsgroup-name))
5168     (setq gnus-newsgroup-begin nil)
5169     (gnus-summary-exit)
5170     ;; We have to adjust the point of group mode buffer because
5171     ;; point was moved to the next unread newsgroup by exiting.
5172     (gnus-summary-jump-to-group group)
5173     (when rescan
5174       (save-excursion
5175         (gnus-group-get-new-news-this-group 1)))
5176     (gnus-group-read-group all t)
5177     (gnus-summary-goto-subject current-subject nil t)))
5178
5179 (defun gnus-summary-rescan-group (&optional all)
5180   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5181   (interactive "P")
5182   (gnus-summary-reselect-current-group all t))
5183
5184 (defun gnus-summary-update-info (&optional non-destructive)
5185   (save-excursion
5186     (let ((group gnus-newsgroup-name))
5187       (when group
5188         (when gnus-newsgroup-kill-headers
5189           (setq gnus-newsgroup-killed
5190                 (gnus-compress-sequence
5191                  (nconc
5192                   (gnus-set-sorted-intersection
5193                    (gnus-uncompress-range gnus-newsgroup-killed)
5194                    (setq gnus-newsgroup-unselected
5195                          (sort gnus-newsgroup-unselected '<)))
5196                   (setq gnus-newsgroup-unreads
5197                         (sort gnus-newsgroup-unreads '<)))
5198                  t)))
5199         (unless (listp (cdr gnus-newsgroup-killed))
5200           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5201         (let ((headers gnus-newsgroup-headers))
5202           ;; Set the new ranges of read articles.
5203           (save-excursion
5204             (set-buffer gnus-group-buffer)
5205             (gnus-undo-force-boundary))
5206           (gnus-update-read-articles
5207            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5208           ;; Set the current article marks.
5209           (let ((gnus-newsgroup-scored
5210                  (if (and (not gnus-save-score)
5211                           (not non-destructive))
5212                      nil
5213                    gnus-newsgroup-scored)))
5214             (save-excursion
5215               (gnus-update-marks)))
5216           ;; Do the cross-ref thing.
5217           (when gnus-use-cross-reference
5218             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5219           ;; Do not switch windows but change the buffer to work.
5220           (set-buffer gnus-group-buffer)
5221           (unless (gnus-ephemeral-group-p group)
5222             (gnus-group-update-group group)))))))
5223
5224 (defun gnus-summary-save-newsrc (&optional force)
5225   "Save the current number of read/marked articles in the dribble buffer.
5226 The dribble buffer will then be saved.
5227 If FORCE (the prefix), also save the .newsrc file(s)."
5228   (interactive "P")
5229   (gnus-summary-update-info t)
5230   (if force
5231       (gnus-save-newsrc-file)
5232     (gnus-dribble-save)))
5233
5234 (defun gnus-summary-exit (&optional temporary)
5235   "Exit reading current newsgroup, and then return to group selection mode.
5236 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5237   (interactive)
5238   (gnus-set-global-variables)
5239   (gnus-kill-save-kill-buffer)
5240   (gnus-async-halt-prefetch)
5241   (let* ((group gnus-newsgroup-name)
5242          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5243          (mode major-mode)
5244          (group-point nil)
5245          (buf (current-buffer)))
5246     (unless quit-config
5247       ;; Do adaptive scoring, and possibly save score files.
5248       (when gnus-newsgroup-adaptive
5249         (gnus-score-adaptive))
5250       (when gnus-use-scoring
5251         (gnus-score-save)))
5252     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5253     ;; If we have several article buffers, we kill them at exit.
5254     (unless gnus-single-article-buffer
5255       (gnus-kill-buffer gnus-original-article-buffer)
5256       (setq gnus-article-current nil))
5257     (when gnus-use-cache
5258       (gnus-cache-possibly-remove-articles)
5259       (gnus-cache-save-buffers))
5260     (gnus-async-prefetch-remove-group group)
5261     (when gnus-suppress-duplicates
5262       (gnus-dup-enter-articles))
5263     (when gnus-use-trees
5264       (gnus-tree-close group))
5265     ;; Remove entries for this group.
5266     (nnmail-purge-split-history (gnus-group-real-name group))
5267     ;; Make all changes in this group permanent.
5268     (unless quit-config
5269       (gnus-run-hooks 'gnus-exit-group-hook)
5270       (gnus-summary-update-info))
5271     (gnus-close-group group)
5272     ;; Make sure where we were, and go to next newsgroup.
5273     (set-buffer gnus-group-buffer)
5274     (unless quit-config
5275       (gnus-group-jump-to-group group))
5276     (gnus-run-hooks 'gnus-summary-exit-hook)
5277     (unless (or quit-config
5278                 ;; If this group has disappeared from the summary
5279                 ;; buffer, don't skip forwards.
5280                 (not (string= group (gnus-group-group-name))))
5281       (gnus-group-next-unread-group 1))
5282     (setq group-point (point))
5283     (if temporary
5284         nil                             ;Nothing to do.
5285       ;; If we have several article buffers, we kill them at exit.
5286       (unless gnus-single-article-buffer
5287         (gnus-kill-buffer gnus-article-buffer)
5288         (gnus-kill-buffer gnus-original-article-buffer)
5289         (setq gnus-article-current nil))
5290       (set-buffer buf)
5291       (if (not gnus-kill-summary-on-exit)
5292           (gnus-deaden-summary)
5293         ;; We set all buffer-local variables to nil.  It is unclear why
5294         ;; this is needed, but if we don't, buffer-local variables are
5295         ;; not garbage-collected, it seems.  This would the lead to en
5296         ;; ever-growing Emacs.
5297         (gnus-summary-clear-local-variables)
5298         (when (get-buffer gnus-article-buffer)
5299           (bury-buffer gnus-article-buffer))
5300         ;; We clear the global counterparts of the buffer-local
5301         ;; variables as well, just to be on the safe side.
5302         (set-buffer gnus-group-buffer)
5303         (gnus-summary-clear-local-variables)
5304         ;; Return to group mode buffer.
5305         (when (eq mode 'gnus-summary-mode)
5306           (gnus-kill-buffer buf)))
5307       (setq gnus-current-select-method gnus-select-method)
5308       (pop-to-buffer gnus-group-buffer)
5309       (if (not quit-config)
5310           (progn
5311             (goto-char group-point)
5312             (gnus-configure-windows 'group 'force))
5313         (gnus-handle-ephemeral-exit quit-config))
5314       ;; Clear the current group name.
5315       (unless quit-config
5316         (setq gnus-newsgroup-name nil)))))
5317
5318 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5319 (defun gnus-summary-exit-no-update (&optional no-questions)
5320   "Quit reading current newsgroup without updating read article info."
5321   (interactive)
5322   (let* ((group gnus-newsgroup-name)
5323          (quit-config (gnus-group-quit-config group)))
5324     (when (or no-questions
5325               gnus-expert-user
5326               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5327       (gnus-async-halt-prefetch)
5328       (mapcar 'funcall
5329               (delq 'gnus-summary-expire-articles
5330                     (copy-list gnus-summary-prepare-exit-hook)))
5331       ;; If we have several article buffers, we kill them at exit.
5332       (unless gnus-single-article-buffer
5333         (gnus-kill-buffer gnus-article-buffer)
5334         (gnus-kill-buffer gnus-original-article-buffer)
5335         (setq gnus-article-current nil))
5336       (if (not gnus-kill-summary-on-exit)
5337           (gnus-deaden-summary)
5338         (gnus-close-group group)
5339         (gnus-summary-clear-local-variables)
5340         (set-buffer gnus-group-buffer)
5341         (gnus-summary-clear-local-variables)
5342         (when (get-buffer gnus-summary-buffer)
5343           (kill-buffer gnus-summary-buffer)))
5344       (unless gnus-single-article-buffer
5345         (setq gnus-article-current nil))
5346       (when gnus-use-trees
5347         (gnus-tree-close group))
5348       (gnus-async-prefetch-remove-group group)
5349       (when (get-buffer gnus-article-buffer)
5350         (bury-buffer gnus-article-buffer))
5351       ;; Return to the group buffer.
5352       (gnus-configure-windows 'group 'force)
5353       ;; Clear the current group name.
5354       (setq gnus-newsgroup-name nil)
5355       (when (equal (gnus-group-group-name) group)
5356         (gnus-group-next-unread-group 1))
5357       (when quit-config
5358         (gnus-handle-ephemeral-exit quit-config)))))
5359
5360 (defun gnus-handle-ephemeral-exit (quit-config)
5361   "Handle movement when leaving an ephemeral group.
5362 The state which existed when entering the ephemeral is reset."
5363   (if (not (buffer-name (car quit-config)))
5364       (gnus-configure-windows 'group 'force)
5365     (set-buffer (car quit-config))
5366     (cond ((eq major-mode 'gnus-summary-mode)
5367            (gnus-set-global-variables))
5368           ((eq major-mode 'gnus-article-mode)
5369            (save-excursion
5370              ;; The `gnus-summary-buffer' variable may point
5371              ;; to the old summary buffer when using a single
5372              ;; article buffer.
5373              (unless (gnus-buffer-live-p gnus-summary-buffer)
5374                (set-buffer gnus-group-buffer))
5375              (set-buffer gnus-summary-buffer)
5376              (gnus-set-global-variables))))
5377     (if (or (eq (cdr quit-config) 'article)
5378             (eq (cdr quit-config) 'pick))
5379         (progn
5380           ;; The current article may be from the ephemeral group
5381           ;; thus it is best that we reload this article
5382           (gnus-summary-show-article)
5383           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5384               (gnus-configure-windows 'pick 'force)
5385             (gnus-configure-windows (cdr quit-config) 'force)))
5386       (gnus-configure-windows (cdr quit-config) 'force))
5387     (when (eq major-mode 'gnus-summary-mode)
5388       (gnus-summary-next-subject 1 nil t)
5389       (gnus-summary-recenter)
5390       (gnus-summary-position-point))))
5391
5392 (defun gnus-summary-preview-mime-message ()
5393   "MIME decode and play this message."
5394   (interactive)
5395   (let ((gnus-break-pages nil)
5396         (gnus-show-mime t))
5397     (gnus-summary-select-article gnus-show-all-headers t))
5398   (select-window (get-buffer-window gnus-article-buffer)))
5399
5400 ;;; Dead summaries.
5401
5402 (defvar gnus-dead-summary-mode-map nil)
5403
5404 (unless gnus-dead-summary-mode-map
5405   (setq gnus-dead-summary-mode-map (make-keymap))
5406   (suppress-keymap gnus-dead-summary-mode-map)
5407   (substitute-key-definition
5408    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5409   (let ((keys '("\C-d" "\r" "\177" [delete])))
5410     (while keys
5411       (define-key gnus-dead-summary-mode-map
5412         (pop keys) 'gnus-summary-wake-up-the-dead))))
5413
5414 (defvar gnus-dead-summary-mode nil
5415   "Minor mode for Gnus summary buffers.")
5416
5417 (defun gnus-dead-summary-mode (&optional arg)
5418   "Minor mode for Gnus summary buffers."
5419   (interactive "P")
5420   (when (eq major-mode 'gnus-summary-mode)
5421     (make-local-variable 'gnus-dead-summary-mode)
5422     (setq gnus-dead-summary-mode
5423           (if (null arg) (not gnus-dead-summary-mode)
5424             (> (prefix-numeric-value arg) 0)))
5425     (when gnus-dead-summary-mode
5426       (gnus-add-minor-mode
5427        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5428
5429 (defun gnus-deaden-summary ()
5430   "Make the current summary buffer into a dead summary buffer."
5431   ;; Kill any previous dead summary buffer.
5432   (when (and gnus-dead-summary
5433              (buffer-name gnus-dead-summary))
5434     (save-excursion
5435       (set-buffer gnus-dead-summary)
5436       (when gnus-dead-summary-mode
5437         (kill-buffer (current-buffer)))))
5438   ;; Make this the current dead summary.
5439   (setq gnus-dead-summary (current-buffer))
5440   (gnus-dead-summary-mode 1)
5441   (let ((name (buffer-name)))
5442     (when (string-match "Summary" name)
5443       (rename-buffer
5444        (concat (substring name 0 (match-beginning 0)) "Dead "
5445                (substring name (match-beginning 0)))
5446        t))))
5447
5448 (defun gnus-kill-or-deaden-summary (buffer)
5449   "Kill or deaden the summary BUFFER."
5450   (save-excursion
5451     (when (and (buffer-name buffer)
5452                (not gnus-single-article-buffer))
5453       (save-excursion
5454         (set-buffer buffer)
5455         (gnus-kill-buffer gnus-article-buffer)
5456         (gnus-kill-buffer gnus-original-article-buffer)))
5457     (cond (gnus-kill-summary-on-exit
5458            (when (and gnus-use-trees
5459                       (gnus-buffer-exists-p buffer))
5460              (save-excursion
5461                (set-buffer buffer)
5462                (gnus-tree-close gnus-newsgroup-name)))
5463            (gnus-kill-buffer buffer))
5464           ((gnus-buffer-exists-p buffer)
5465            (save-excursion
5466              (set-buffer buffer)
5467              (gnus-deaden-summary))))))
5468
5469 (defun gnus-summary-wake-up-the-dead (&rest args)
5470   "Wake up the dead summary buffer."
5471   (interactive)
5472   (gnus-dead-summary-mode -1)
5473   (let ((name (buffer-name)))
5474     (when (string-match "Dead " name)
5475       (rename-buffer
5476        (concat (substring name 0 (match-beginning 0))
5477                (substring name (match-end 0)))
5478        t)))
5479   (gnus-message 3 "This dead summary is now alive again"))
5480
5481 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5482 (defun gnus-summary-fetch-faq (&optional faq-dir)
5483   "Fetch the FAQ for the current group.
5484 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5485 in."
5486   (interactive
5487    (list
5488     (when current-prefix-arg
5489       (completing-read
5490        "Faq dir: " (and (listp gnus-group-faq-directory)
5491                         (mapcar (lambda (file) (list file))
5492                                 gnus-group-faq-directory))))))
5493   (let (gnus-faq-buffer)
5494     (when (setq gnus-faq-buffer
5495                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5496       (gnus-configure-windows 'summary-faq))))
5497
5498 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5499 (defun gnus-summary-describe-group (&optional force)
5500   "Describe the current newsgroup."
5501   (interactive "P")
5502   (gnus-group-describe-group force gnus-newsgroup-name))
5503
5504 (defun gnus-summary-describe-briefly ()
5505   "Describe summary mode commands briefly."
5506   (interactive)
5507   (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")))
5508
5509 ;; Walking around group mode buffer from summary mode.
5510
5511 (defun gnus-summary-next-group (&optional no-article target-group backward)
5512   "Exit current newsgroup and then select next unread newsgroup.
5513 If prefix argument NO-ARTICLE is non-nil, no article is selected
5514 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5515 previous group instead."
5516   (interactive "P")
5517   ;; Stop pre-fetching.
5518   (gnus-async-halt-prefetch)
5519   (let ((current-group gnus-newsgroup-name)
5520         (current-buffer (current-buffer))
5521         entered)
5522     ;; First we semi-exit this group to update Xrefs and all variables.
5523     ;; We can't do a real exit, because the window conf must remain
5524     ;; the same in case the user is prompted for info, and we don't
5525     ;; want the window conf to change before that...
5526     (gnus-summary-exit t)
5527     (while (not entered)
5528       ;; Then we find what group we are supposed to enter.
5529       (set-buffer gnus-group-buffer)
5530       (gnus-group-jump-to-group current-group)
5531       (setq target-group
5532             (or target-group
5533                 (if (eq gnus-keep-same-level 'best)
5534                     (gnus-summary-best-group gnus-newsgroup-name)
5535                   (gnus-summary-search-group backward gnus-keep-same-level))))
5536       (if (not target-group)
5537           ;; There are no further groups, so we return to the group
5538           ;; buffer.
5539           (progn
5540             (gnus-message 5 "Returning to the group buffer")
5541             (setq entered t)
5542             (when (gnus-buffer-live-p current-buffer)
5543               (set-buffer current-buffer)
5544               (gnus-summary-exit))
5545             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5546         ;; We try to enter the target group.
5547         (gnus-group-jump-to-group target-group)
5548         (let ((unreads (gnus-group-group-unread)))
5549           (if (and (or (eq t unreads)
5550                        (and unreads (not (zerop unreads))))
5551                    (gnus-summary-read-group
5552                     target-group nil no-article
5553                     (and (buffer-name current-buffer) current-buffer)
5554                     nil backward))
5555               (setq entered t)
5556             (setq current-group target-group
5557                   target-group nil)))))))
5558
5559 (defun gnus-summary-prev-group (&optional no-article)
5560   "Exit current newsgroup and then select previous unread newsgroup.
5561 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5562   (interactive "P")
5563   (gnus-summary-next-group no-article nil t))
5564
5565 ;; Walking around summary lines.
5566
5567 (defun gnus-summary-first-subject (&optional unread undownloaded)
5568   "Go to the first unread subject.
5569 If UNREAD is non-nil, go to the first unread article.
5570 Returns the article selected or nil if there are no unread articles."
5571   (interactive "P")
5572   (prog1
5573       (cond
5574        ;; Empty summary.
5575        ((null gnus-newsgroup-data)
5576         (gnus-message 3 "No articles in the group")
5577         nil)
5578        ;; Pick the first article.
5579        ((not unread)
5580         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5581         (gnus-data-number (car gnus-newsgroup-data)))
5582        ;; No unread articles.
5583        ((null gnus-newsgroup-unreads)
5584         (gnus-message 3 "No more unread articles")
5585         nil)
5586        ;; Find the first unread article.
5587        (t
5588         (let ((data gnus-newsgroup-data))
5589           (while (and data
5590                       (and (not (and undownloaded
5591                                      (eq gnus-undownloaded-mark
5592                                          (gnus-data-mark (car data)))))
5593                            (not (gnus-data-unread-p (car data)))))
5594             (setq data (cdr data)))
5595           (when data
5596             (goto-char (gnus-data-pos (car data)))
5597             (gnus-data-number (car data))))))
5598     (gnus-summary-position-point)))
5599
5600 (defun gnus-summary-next-subject (n &optional unread dont-display)
5601   "Go to next N'th summary line.
5602 If N is negative, go to the previous N'th subject line.
5603 If UNREAD is non-nil, only unread articles are selected.
5604 The difference between N and the actual number of steps taken is
5605 returned."
5606   (interactive "p")
5607   (let ((backward (< n 0))
5608         (n (abs n)))
5609     (while (and (> n 0)
5610                 (if backward
5611                     (gnus-summary-find-prev unread)
5612                   (gnus-summary-find-next unread)))
5613       (gnus-summary-show-thread)
5614       (setq n (1- n)))
5615     (when (/= 0 n)
5616       (gnus-message 7 "No more%s articles"
5617                     (if unread " unread" "")))
5618     (unless dont-display
5619       (gnus-summary-recenter)
5620       (gnus-summary-position-point))
5621     n))
5622
5623 (defun gnus-summary-next-unread-subject (n)
5624   "Go to next N'th unread summary line."
5625   (interactive "p")
5626   (gnus-summary-next-subject n t))
5627
5628 (defun gnus-summary-prev-subject (n &optional unread)
5629   "Go to previous N'th summary line.
5630 If optional argument UNREAD is non-nil, only unread article is selected."
5631   (interactive "p")
5632   (gnus-summary-next-subject (- n) unread))
5633
5634 (defun gnus-summary-prev-unread-subject (n)
5635   "Go to previous N'th unread summary line."
5636   (interactive "p")
5637   (gnus-summary-next-subject (- n) t))
5638
5639 (defun gnus-summary-goto-subject (article &optional force silent)
5640   "Go the subject line of ARTICLE.
5641 If FORCE, also allow jumping to articles not currently shown."
5642   (interactive "nArticle number: ")
5643   (let ((b (point))
5644         (data (gnus-data-find article)))
5645     ;; We read in the article if we have to.
5646     (and (not data)
5647          force
5648          (gnus-summary-insert-subject
5649           article
5650           (if (or (numberp force) (vectorp force)) force)
5651           t)
5652          (setq data (gnus-data-find article)))
5653     (goto-char b)
5654     (if (not data)
5655         (progn
5656           (unless silent
5657             (gnus-message 3 "Can't find article %d" article))
5658           nil)
5659       (goto-char (gnus-data-pos data))
5660       (gnus-summary-position-point)
5661       article)))
5662
5663 ;; Walking around summary lines with displaying articles.
5664
5665 (defun gnus-summary-expand-window (&optional arg)
5666   "Make the summary buffer take up the entire Emacs frame.
5667 Given a prefix, will force an `article' buffer configuration."
5668   (interactive "P")
5669   (if arg
5670       (gnus-configure-windows 'article 'force)
5671     (gnus-configure-windows 'summary 'force)))
5672
5673 (defun gnus-summary-display-article (article &optional all-header)
5674   "Display ARTICLE in article buffer."
5675   (gnus-set-global-variables)
5676   (if (null article)
5677       nil
5678     (prog1
5679         (if gnus-summary-display-article-function
5680             (funcall gnus-summary-display-article-function article all-header)
5681           (gnus-article-prepare article all-header))
5682       (gnus-run-hooks 'gnus-select-article-hook)
5683       (when (and gnus-current-article
5684                  (not (zerop gnus-current-article)))
5685         (gnus-summary-goto-subject gnus-current-article))
5686       (gnus-summary-recenter)
5687       (when (and gnus-use-trees gnus-show-threads)
5688         (gnus-possibly-generate-tree article)
5689         (gnus-highlight-selected-tree article))
5690       ;; Successfully display article.
5691       (gnus-article-set-window-start
5692        (cdr (assq article gnus-newsgroup-bookmarks))))))
5693
5694 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5695   "Select the current article.
5696 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5697 non-nil, the article will be re-fetched even if it already present in
5698 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5699 be displayed."
5700   ;; Make sure we are in the summary buffer to work around bbdb bug.
5701   (unless (eq major-mode 'gnus-summary-mode)
5702     (set-buffer gnus-summary-buffer))
5703   (let ((article (or article (gnus-summary-article-number)))
5704         (all-headers (not (not all-headers))) ;Must be T or NIL.
5705         gnus-summary-display-article-function
5706         did)
5707     (and (not pseudo)
5708          (gnus-summary-article-pseudo-p article)
5709          (error "This is a pseudo-article"))
5710     (prog1
5711         (save-excursion
5712           (set-buffer gnus-summary-buffer)
5713           (if (or (and gnus-single-article-buffer
5714                        (or (null gnus-current-article)
5715                            (null gnus-article-current)
5716                            (null (get-buffer gnus-article-buffer))
5717                            (not (eq article (cdr gnus-article-current)))
5718                            (not (equal (car gnus-article-current)
5719                                        gnus-newsgroup-name))))
5720                   (and (not gnus-single-article-buffer)
5721                        (or (null gnus-current-article)
5722                            (not (eq gnus-current-article article))))
5723                   force)
5724               ;; The requested article is different from the current article.
5725               (prog1
5726                   (gnus-summary-display-article article all-headers)
5727                 (setq did article)
5728                 (when (or all-headers gnus-show-all-headers)
5729                   (gnus-article-show-all-headers)))
5730             (when (or all-headers gnus-show-all-headers)
5731               (gnus-article-show-all-headers))
5732             'old))
5733       (when did
5734         (gnus-article-set-window-start
5735          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5736
5737 (defun gnus-summary-set-current-mark (&optional current-mark)
5738   "Obsolete function."
5739   nil)
5740
5741 (defun gnus-summary-next-article (&optional unread subject backward push)
5742   "Select the next article.
5743 If UNREAD, only unread articles are selected.
5744 If SUBJECT, only articles with SUBJECT are selected.
5745 If BACKWARD, the previous article is selected instead of the next."
5746   (interactive "P")
5747   (cond
5748    ;; Is there such an article?
5749    ((and (gnus-summary-search-forward unread subject backward)
5750          (or (gnus-summary-display-article (gnus-summary-article-number))
5751              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5752     (gnus-summary-position-point))
5753    ;; If not, we try the first unread, if that is wanted.
5754    ((and subject
5755          gnus-auto-select-same
5756          (gnus-summary-first-unread-article))
5757     (gnus-summary-position-point)
5758     (gnus-message 6 "Wrapped"))
5759    ;; Try to get next/previous article not displayed in this group.
5760    ((and gnus-auto-extend-newsgroup
5761          (not unread) (not subject))
5762     (gnus-summary-goto-article
5763      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5764      nil (count-lines (point-min) (point))))
5765    ;; Go to next/previous group.
5766    (t
5767     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5768       (gnus-summary-jump-to-group gnus-newsgroup-name))
5769     (let ((cmd last-command-char)
5770           (point
5771            (save-excursion
5772              (set-buffer gnus-group-buffer)
5773              (point)))
5774           (group
5775            (if (eq gnus-keep-same-level 'best)
5776                (gnus-summary-best-group gnus-newsgroup-name)
5777              (gnus-summary-search-group backward gnus-keep-same-level))))
5778       ;; For some reason, the group window gets selected.  We change
5779       ;; it back.
5780       (select-window (get-buffer-window (current-buffer)))
5781       ;; Select next unread newsgroup automagically.
5782       (cond
5783        ((or (not gnus-auto-select-next)
5784             (not cmd))
5785         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5786        ((or (eq gnus-auto-select-next 'quietly)
5787             (and (eq gnus-auto-select-next 'slightly-quietly)
5788                  push)
5789             (and (eq gnus-auto-select-next 'almost-quietly)
5790                  (gnus-summary-last-article-p)))
5791         ;; Select quietly.
5792         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5793             (gnus-summary-exit)
5794           (gnus-message 7 "No more%s articles (%s)..."
5795                         (if unread " unread" "")
5796                         (if group (concat "selecting " group)
5797                           "exiting"))
5798           (gnus-summary-next-group nil group backward)))
5799        (t
5800         (when (gnus-key-press-event-p last-input-event)
5801           (gnus-summary-walk-group-buffer
5802            gnus-newsgroup-name cmd unread backward point))))))))
5803
5804 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5805   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5806                       (?\C-p (gnus-group-prev-unread-group 1))))
5807         (cursor-in-echo-area t)
5808         keve key group ended)
5809     (save-excursion
5810       (set-buffer gnus-group-buffer)
5811       (goto-char start)
5812       (setq group
5813             (if (eq gnus-keep-same-level 'best)
5814                 (gnus-summary-best-group gnus-newsgroup-name)
5815               (gnus-summary-search-group backward gnus-keep-same-level))))
5816     (while (not ended)
5817       (gnus-message
5818        5 "No more%s articles%s" (if unread " unread" "")
5819        (if (and group
5820                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5821            (format " (Type %s for %s [%s])"
5822                    (single-key-description cmd) group
5823                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5824          (format " (Type %s to exit %s)"
5825                  (single-key-description cmd)
5826                  gnus-newsgroup-name)))
5827       ;; Confirm auto selection.
5828       (setq key (car (setq keve (gnus-read-event-char))))
5829       (setq ended t)
5830       (cond
5831        ((assq key keystrokes)
5832         (let ((obuf (current-buffer)))
5833           (switch-to-buffer gnus-group-buffer)
5834           (when group
5835             (gnus-group-jump-to-group group))
5836           (eval (cadr (assq key keystrokes)))
5837           (setq group (gnus-group-group-name))
5838           (switch-to-buffer obuf))
5839         (setq ended nil))
5840        ((equal key cmd)
5841         (if (or (not group)
5842                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5843             (gnus-summary-exit)
5844           (gnus-summary-next-group nil group backward)))
5845        (t
5846         (push (cdr keve) unread-command-events))))))
5847
5848 (defun gnus-summary-next-unread-article ()
5849   "Select unread article after current one."
5850   (interactive)
5851   (gnus-summary-next-article
5852    (or (not (eq gnus-summary-goto-unread 'never))
5853        (gnus-summary-last-article-p (gnus-summary-article-number)))
5854    (and gnus-auto-select-same
5855         (gnus-summary-article-subject))))
5856
5857 (defun gnus-summary-prev-article (&optional unread subject)
5858   "Select the article after the current one.
5859 If UNREAD is non-nil, only unread articles are selected."
5860   (interactive "P")
5861   (gnus-summary-next-article unread subject t))
5862
5863 (defun gnus-summary-prev-unread-article ()
5864   "Select unread article before current one."
5865   (interactive)
5866   (gnus-summary-prev-article
5867    (or (not (eq gnus-summary-goto-unread 'never))
5868        (gnus-summary-first-article-p (gnus-summary-article-number)))
5869    (and gnus-auto-select-same
5870         (gnus-summary-article-subject))))
5871
5872 (defun gnus-summary-next-page (&optional lines circular)
5873   "Show next page of the selected article.
5874 If at the end of the current article, select the next article.
5875 LINES says how many lines should be scrolled up.
5876
5877 If CIRCULAR is non-nil, go to the start of the article instead of
5878 selecting the next article when reaching the end of the current
5879 article."
5880   (interactive "P")
5881   (setq gnus-summary-buffer (current-buffer))
5882   (gnus-set-global-variables)
5883   (let ((article (gnus-summary-article-number))
5884         (article-window (get-buffer-window gnus-article-buffer t))
5885         endp)
5886     ;; If the buffer is empty, we have no article.
5887     (unless article
5888       (error "No article to select"))
5889     (gnus-configure-windows 'article)
5890     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5891         (if (and (eq gnus-summary-goto-unread 'never)
5892                  (not (gnus-summary-last-article-p article)))
5893             (gnus-summary-next-article)
5894           (gnus-summary-next-unread-article))
5895       (if (or (null gnus-current-article)
5896               (null gnus-article-current)
5897               (/= article (cdr gnus-article-current))
5898               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5899           ;; Selected subject is different from current article's.
5900           (gnus-summary-display-article article)
5901         (when article-window
5902           (gnus-eval-in-buffer-window gnus-article-buffer
5903             (setq endp (gnus-article-next-page lines)))
5904           (when endp
5905             (cond (circular
5906                    (gnus-summary-beginning-of-article))
5907                   (lines
5908                    (gnus-message 3 "End of message"))
5909                   ((null lines)
5910                    (if (and (eq gnus-summary-goto-unread 'never)
5911                             (not (gnus-summary-last-article-p article)))
5912                        (gnus-summary-next-article)
5913                      (gnus-summary-next-unread-article))))))))
5914     (gnus-summary-recenter)
5915     (gnus-summary-position-point)))
5916
5917 (defun gnus-summary-prev-page (&optional lines move)
5918   "Show previous page of selected article.
5919 Argument LINES specifies lines to be scrolled down.
5920 If MOVE, move to the previous unread article if point is at
5921 the beginning of the buffer."
5922   (interactive "P")
5923   (let ((article (gnus-summary-article-number))
5924         (article-window (get-buffer-window gnus-article-buffer t))
5925         endp)
5926     (gnus-configure-windows 'article)
5927     (if (or (null gnus-current-article)
5928             (null gnus-article-current)
5929             (/= article (cdr gnus-article-current))
5930             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5931         ;; Selected subject is different from current article's.
5932         (gnus-summary-display-article article)
5933       (gnus-summary-recenter)
5934       (when article-window
5935         (gnus-eval-in-buffer-window gnus-article-buffer
5936           (setq endp (gnus-article-prev-page lines)))
5937         (when (and move endp)
5938           (cond (lines
5939                  (gnus-message 3 "Beginning of message"))
5940                 ((null lines)
5941                  (if (and (eq gnus-summary-goto-unread 'never)
5942                           (not (gnus-summary-first-article-p article)))
5943                      (gnus-summary-prev-article)
5944                    (gnus-summary-prev-unread-article))))))))
5945   (gnus-summary-position-point))
5946
5947 (defun gnus-summary-prev-page-or-article (&optional lines)
5948   "Show previous page of selected article.
5949 Argument LINES specifies lines to be scrolled down.
5950 If at the beginning of the article, go to the next article."
5951   (interactive "P")
5952   (gnus-summary-prev-page lines t))
5953
5954 (defun gnus-summary-scroll-up (lines)
5955   "Scroll up (or down) one line current article.
5956 Argument LINES specifies lines to be scrolled up (or down if negative)."
5957   (interactive "p")
5958   (gnus-configure-windows 'article)
5959   (gnus-summary-show-thread)
5960   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5961     (gnus-eval-in-buffer-window gnus-article-buffer
5962       (cond ((> lines 0)
5963              (when (gnus-article-next-page lines)
5964                (gnus-message 3 "End of message")))
5965             ((< lines 0)
5966              (gnus-article-prev-page (- lines))))))
5967   (gnus-summary-recenter)
5968   (gnus-summary-position-point))
5969
5970 (defun gnus-summary-scroll-down (lines)
5971   "Scroll down (or up) one line current article.
5972 Argument LINES specifies lines to be scrolled down (or up if negative)."
5973   (interactive "p")
5974   (gnus-summary-scroll-up (- lines)))
5975
5976 (defun gnus-summary-next-same-subject ()
5977   "Select next article which has the same subject as current one."
5978   (interactive)
5979   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5980
5981 (defun gnus-summary-prev-same-subject ()
5982   "Select previous article which has the same subject as current one."
5983   (interactive)
5984   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5985
5986 (defun gnus-summary-next-unread-same-subject ()
5987   "Select next unread article which has the same subject as current one."
5988   (interactive)
5989   (gnus-summary-next-article t (gnus-summary-article-subject)))
5990
5991 (defun gnus-summary-prev-unread-same-subject ()
5992   "Select previous unread article which has the same subject as current one."
5993   (interactive)
5994   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5995
5996 (defun gnus-summary-first-unread-article ()
5997   "Select the first unread article.
5998 Return nil if there are no unread articles."
5999   (interactive)
6000   (prog1
6001       (when (gnus-summary-first-subject t)
6002         (gnus-summary-show-thread)
6003         (gnus-summary-first-subject t)
6004         (gnus-summary-display-article (gnus-summary-article-number)))
6005     (gnus-summary-position-point)))
6006
6007 (defun gnus-summary-first-unread-subject ()
6008   "Place the point on the subject line of the first unread article.
6009 Return nil if there are no unread articles."
6010   (interactive)
6011   (prog1
6012       (when (gnus-summary-first-subject t)
6013         (gnus-summary-show-thread)
6014         (gnus-summary-first-subject t))
6015     (gnus-summary-position-point)))
6016
6017 (defun gnus-summary-first-article ()
6018   "Select the first article.
6019 Return nil if there are no articles."
6020   (interactive)
6021   (prog1
6022       (when (gnus-summary-first-subject)
6023         (gnus-summary-show-thread)
6024         (gnus-summary-first-subject)
6025         (gnus-summary-display-article (gnus-summary-article-number)))
6026     (gnus-summary-position-point)))
6027
6028 (defun gnus-summary-best-unread-article ()
6029   "Select the unread article with the highest score."
6030   (interactive)
6031   (let ((best -1000000)
6032         (data gnus-newsgroup-data)
6033         article score)
6034     (while data
6035       (and (gnus-data-unread-p (car data))
6036            (> (setq score
6037                     (gnus-summary-article-score (gnus-data-number (car data))))
6038               best)
6039            (setq best score
6040                  article (gnus-data-number (car data))))
6041       (setq data (cdr data)))
6042     (prog1
6043         (if article
6044             (gnus-summary-goto-article article)
6045           (error "No unread articles"))
6046       (gnus-summary-position-point))))
6047
6048 (defun gnus-summary-last-subject ()
6049   "Go to the last displayed subject line in the group."
6050   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6051     (when article
6052       (gnus-summary-goto-subject article))))
6053
6054 (defun gnus-summary-goto-article (article &optional all-headers force)
6055   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6056 If ALL-HEADERS is non-nil, no header lines are hidden.
6057 If FORCE, go to the article even if it isn't displayed.  If FORCE
6058 is a number, it is the line the article is to be displayed on."
6059   (interactive
6060    (list
6061     (completing-read
6062      "Article number or Message-ID: "
6063      (mapcar (lambda (number) (list (int-to-string number)))
6064              gnus-newsgroup-limit))
6065     current-prefix-arg
6066     t))
6067   (prog1
6068       (if (and (stringp article)
6069                (string-match "@" article))
6070           (gnus-summary-refer-article article)
6071         (when (stringp article)
6072           (setq article (string-to-number article)))
6073         (if (gnus-summary-goto-subject article force)
6074             (gnus-summary-display-article article all-headers)
6075           (gnus-message 4 "Couldn't go to article %s" article) nil))
6076     (gnus-summary-position-point)))
6077
6078 (defun gnus-summary-goto-last-article ()
6079   "Go to the previously read article."
6080   (interactive)
6081   (prog1
6082       (when gnus-last-article
6083         (gnus-summary-goto-article gnus-last-article nil t))
6084     (gnus-summary-position-point)))
6085
6086 (defun gnus-summary-pop-article (number)
6087   "Pop one article off the history and go to the previous.
6088 NUMBER articles will be popped off."
6089   (interactive "p")
6090   (let (to)
6091     (setq gnus-newsgroup-history
6092           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6093     (if to
6094         (gnus-summary-goto-article (car to) nil t)
6095       (error "Article history empty")))
6096   (gnus-summary-position-point))
6097
6098 ;; Summary commands and functions for limiting the summary buffer.
6099
6100 (defun gnus-summary-limit-to-articles (n)
6101   "Limit the summary buffer to the next N articles.
6102 If not given a prefix, use the process marked articles instead."
6103   (interactive "P")
6104   (prog1
6105       (let ((articles (gnus-summary-work-articles n)))
6106         (setq gnus-newsgroup-processable nil)
6107         (gnus-summary-limit articles))
6108     (gnus-summary-position-point)))
6109
6110 (defun gnus-summary-pop-limit (&optional total)
6111   "Restore the previous limit.
6112 If given a prefix, remove all limits."
6113   (interactive "P")
6114   (when total
6115     (setq gnus-newsgroup-limits
6116           (list (mapcar (lambda (h) (mail-header-number h))
6117                         gnus-newsgroup-headers))))
6118   (unless gnus-newsgroup-limits
6119     (error "No limit to pop"))
6120   (prog1
6121       (gnus-summary-limit nil 'pop)
6122     (gnus-summary-position-point)))
6123
6124 (defun gnus-summary-limit-to-subject (subject &optional header)
6125   "Limit the summary buffer to articles that have subjects that match a regexp."
6126   (interactive "sLimit to subject (regexp): ")
6127   (unless header
6128     (setq header "subject"))
6129   (when (not (equal "" subject))
6130     (prog1
6131         (let ((articles (gnus-summary-find-matching
6132                          (or header "subject") subject 'all)))
6133           (unless articles
6134             (error "Found no matches for \"%s\"" subject))
6135           (gnus-summary-limit articles))
6136       (gnus-summary-position-point))))
6137
6138 (defun gnus-summary-limit-to-author (from)
6139   "Limit the summary buffer to articles that have authors that match a regexp."
6140   (interactive "sLimit to author (regexp): ")
6141   (gnus-summary-limit-to-subject from "from"))
6142
6143 (defun gnus-summary-limit-to-age (age &optional younger-p)
6144   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6145 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6146 articles that are younger than AGE days."
6147   (interactive "nLimit to articles older than (in days): \nP")
6148   (prog1
6149       (let ((data gnus-newsgroup-data)
6150             (cutoff (days-to-time age))
6151             articles d date is-younger)
6152         (while (setq d (pop data))
6153           (when (and (vectorp (gnus-data-header d))
6154                      (setq date (mail-header-date (gnus-data-header d))))
6155             (setq is-younger (time-less-p
6156                               (time-since (date-to-time date))
6157                               cutoff))
6158             (when (if younger-p
6159                       is-younger
6160                     (not is-younger))
6161               (push (gnus-data-number d) articles))))
6162         (gnus-summary-limit (nreverse articles)))
6163     (gnus-summary-position-point)))
6164
6165 (defun gnus-summary-limit-to-extra (header regexp)
6166   "Limit the summary buffer to articles that match an 'extra' header."
6167   (interactive
6168    (let ((header
6169           (intern
6170            (gnus-completing-read
6171             (symbol-name (car gnus-extra-headers))      
6172             "Score extra header:"       
6173             (mapcar (lambda (x) 
6174                       (cons (symbol-name x) x))
6175                     gnus-extra-headers)
6176             nil                 
6177             t))))
6178      (list header
6179            (read-string (format "Limit to header %s (regexp): " header)))))
6180   (when (not (equal "" regexp))
6181     (prog1
6182         (let ((articles (gnus-summary-find-matching
6183                          (cons 'extra header) regexp 'all)))
6184           (unless articles
6185             (error "Found no matches for \"%s\"" regexp))
6186           (gnus-summary-limit articles))
6187       (gnus-summary-position-point))))
6188
6189 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6190 (make-obsolete
6191  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6192
6193 (defun gnus-summary-limit-to-unread (&optional all)
6194   "Limit the summary buffer to articles that are not marked as read.
6195 If ALL is non-nil, limit strictly to unread articles."
6196   (interactive "P")
6197   (if all
6198       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6199     (gnus-summary-limit-to-marks
6200      ;; Concat all the marks that say that an article is read and have
6201      ;; those removed.
6202      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6203            gnus-killed-mark gnus-kill-file-mark
6204            gnus-low-score-mark gnus-expirable-mark
6205            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6206            gnus-duplicate-mark gnus-souped-mark)
6207      'reverse)))
6208
6209 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6210 (make-obsolete 'gnus-summary-delete-marked-with
6211                'gnus-summary-limit-exlude-marks)
6212
6213 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6214   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6215 If REVERSE, limit the summary buffer to articles that are marked
6216 with MARKS.  MARKS can either be a string of marks or a list of marks.
6217 Returns how many articles were removed."
6218   (interactive "sMarks: ")
6219   (gnus-summary-limit-to-marks marks t))
6220
6221 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6222   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6223 If REVERSE (the prefix), limit the summary buffer to articles that are
6224 not marked with MARKS.  MARKS can either be a string of marks or a
6225 list of marks.
6226 Returns how many articles were removed."
6227   (interactive "sMarks: \nP")
6228   (prog1
6229       (let ((data gnus-newsgroup-data)
6230             (marks (if (listp marks) marks
6231                      (append marks nil))) ; Transform to list.
6232             articles)
6233         (while data
6234           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6235                   (memq (gnus-data-mark (car data)) marks))
6236             (push (gnus-data-number (car data)) articles))
6237           (setq data (cdr data)))
6238         (gnus-summary-limit articles))
6239     (gnus-summary-position-point)))
6240
6241 (defun gnus-summary-limit-to-score (&optional score)
6242   "Limit to articles with score at or above SCORE."
6243   (interactive "P")
6244   (setq score (if score
6245                   (prefix-numeric-value score)
6246                 (or gnus-summary-default-score 0)))
6247   (let ((data gnus-newsgroup-data)
6248         articles)
6249     (while data
6250       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6251                 score)
6252         (push (gnus-data-number (car data)) articles))
6253       (setq data (cdr data)))
6254     (prog1
6255         (gnus-summary-limit articles)
6256       (gnus-summary-position-point))))
6257
6258 (defun gnus-summary-limit-include-thread (id)
6259   "Display all the hidden articles that in the current thread."
6260   (interactive (list (mail-header-id (gnus-summary-article-header))))
6261   (let ((articles (gnus-articles-in-thread
6262                    (gnus-id-to-thread (gnus-root-id id)))))
6263     (prog1
6264         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6265       (gnus-summary-position-point))))
6266
6267 (defun gnus-summary-limit-include-dormant ()
6268   "Display all the hidden articles that are marked as dormant.
6269 Note that this command only works on a subset of the articles currently
6270 fetched for this group."
6271   (interactive)
6272   (unless gnus-newsgroup-dormant
6273     (error "There are no dormant articles in this group"))
6274   (prog1
6275       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6276     (gnus-summary-position-point)))
6277
6278 (defun gnus-summary-limit-exclude-dormant ()
6279   "Hide all dormant articles."
6280   (interactive)
6281   (prog1
6282       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6283     (gnus-summary-position-point)))
6284
6285 (defun gnus-summary-limit-exclude-childless-dormant ()
6286   "Hide all dormant articles that have no children."
6287   (interactive)
6288   (let ((data (gnus-data-list t))
6289         articles d children)
6290     ;; Find all articles that are either not dormant or have
6291     ;; children.
6292     (while (setq d (pop data))
6293       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6294                 (and (setq children
6295                            (gnus-article-children (gnus-data-number d)))
6296                      (let (found)
6297                        (while children
6298                          (when (memq (car children) articles)
6299                            (setq children nil
6300                                  found t))
6301                          (pop children))
6302                        found)))
6303         (push (gnus-data-number d) articles)))
6304     ;; Do the limiting.
6305     (prog1
6306         (gnus-summary-limit articles)
6307       (gnus-summary-position-point))))
6308
6309 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6310   "Mark all unread excluded articles as read.
6311 If ALL, mark even excluded ticked and dormants as read."
6312   (interactive "P")
6313   (let ((articles (gnus-sorted-complement
6314                    (sort
6315                     (mapcar (lambda (h) (mail-header-number h))
6316                             gnus-newsgroup-headers)
6317                     '<)
6318                    (sort gnus-newsgroup-limit '<)))
6319         article)
6320     (setq gnus-newsgroup-unreads
6321           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6322     (if all
6323         (setq gnus-newsgroup-dormant nil
6324               gnus-newsgroup-marked nil
6325               gnus-newsgroup-reads
6326               (nconc
6327                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6328                gnus-newsgroup-reads))
6329       (while (setq article (pop articles))
6330         (unless (or (memq article gnus-newsgroup-dormant)
6331                     (memq article gnus-newsgroup-marked))
6332           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6333
6334 (defun gnus-summary-limit (articles &optional pop)
6335   (if pop
6336       ;; We pop the previous limit off the stack and use that.
6337       (setq articles (car gnus-newsgroup-limits)
6338             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6339     ;; We use the new limit, so we push the old limit on the stack.
6340     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6341   ;; Set the limit.
6342   (setq gnus-newsgroup-limit articles)
6343   (let ((total (length gnus-newsgroup-data))
6344         (data (gnus-data-find-list (gnus-summary-article-number)))
6345         (gnus-summary-mark-below nil)   ; Inhibit this.
6346         found)
6347     ;; This will do all the work of generating the new summary buffer
6348     ;; according to the new limit.
6349     (gnus-summary-prepare)
6350     ;; Hide any threads, possibly.
6351     (and gnus-show-threads
6352          gnus-thread-hide-subtree
6353          (gnus-summary-hide-all-threads))
6354     ;; Try to return to the article you were at, or one in the
6355     ;; neighborhood.
6356     (when data
6357       ;; We try to find some article after the current one.
6358       (while data
6359         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6360           (setq data nil
6361                 found t))
6362         (setq data (cdr data))))
6363     (unless found
6364       ;; If there is no data, that means that we were after the last
6365       ;; article.  The same goes when we can't find any articles
6366       ;; after the current one.
6367       (goto-char (point-max))
6368       (gnus-summary-find-prev))
6369     (gnus-set-mode-line 'summary)
6370     ;; We return how many articles were removed from the summary
6371     ;; buffer as a result of the new limit.
6372     (- total (length gnus-newsgroup-data))))
6373
6374 (defsubst gnus-invisible-cut-children (threads)
6375   (let ((num 0))
6376     (while threads
6377       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6378         (incf num))
6379       (pop threads))
6380     (< num 2)))
6381
6382 (defsubst gnus-cut-thread (thread)
6383   "Go forwards in the thread until we find an article that we want to display."
6384   (when (or (eq gnus-fetch-old-headers 'some)
6385             (eq gnus-fetch-old-headers 'invisible)
6386             (eq gnus-build-sparse-threads 'some)
6387             (eq gnus-build-sparse-threads 'more))
6388     ;; Deal with old-fetched headers and sparse threads.
6389     (while (and
6390             thread
6391             (or
6392              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6393              (gnus-summary-article-ancient-p
6394               (mail-header-number (car thread))))
6395             (if (or (<= (length (cdr thread)) 1)
6396                     (eq gnus-fetch-old-headers 'invisible))
6397                 (setq gnus-newsgroup-limit
6398                       (delq (mail-header-number (car thread))
6399                             gnus-newsgroup-limit)
6400                       thread (cadr thread))
6401               (when (gnus-invisible-cut-children (cdr thread))
6402                 (let ((th (cdr thread)))
6403                   (while th
6404                     (if (memq (mail-header-number (caar th))
6405                               gnus-newsgroup-limit)
6406                         (setq thread (car th)
6407                               th nil)
6408                       (setq th (cdr th))))))))))
6409   thread)
6410
6411 (defun gnus-cut-threads (threads)
6412   "Cut off all uninteresting articles from the beginning of threads."
6413   (when (or (eq gnus-fetch-old-headers 'some)
6414             (eq gnus-fetch-old-headers 'invisible)
6415             (eq gnus-build-sparse-threads 'some)
6416             (eq gnus-build-sparse-threads 'more))
6417     (let ((th threads))
6418       (while th
6419         (setcar th (gnus-cut-thread (car th)))
6420         (setq th (cdr th)))))
6421   ;; Remove nixed out threads.
6422   (delq nil threads))
6423
6424 (defun gnus-summary-initial-limit (&optional show-if-empty)
6425   "Figure out what the initial limit is supposed to be on group entry.
6426 This entails weeding out unwanted dormants, low-scored articles,
6427 fetch-old-headers verbiage, and so on."
6428   ;; Most groups have nothing to remove.
6429   (if (or gnus-inhibit-limiting
6430           (and (null gnus-newsgroup-dormant)
6431                (not (eq gnus-fetch-old-headers 'some))
6432                (not (eq gnus-fetch-old-headers 'invisible))
6433                (null gnus-summary-expunge-below)
6434                (not (eq gnus-build-sparse-threads 'some))
6435                (not (eq gnus-build-sparse-threads 'more))
6436                (null gnus-thread-expunge-below)
6437                (not gnus-use-nocem)))
6438       ()                                ; Do nothing.
6439     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6440     (setq gnus-newsgroup-limit nil)
6441     (mapatoms
6442      (lambda (node)
6443        (unless (car (symbol-value node))
6444          ;; These threads have no parents -- they are roots.
6445          (let ((nodes (cdr (symbol-value node)))
6446                thread)
6447            (while nodes
6448              (if (and gnus-thread-expunge-below
6449                       (< (gnus-thread-total-score (car nodes))
6450                          gnus-thread-expunge-below))
6451                  (gnus-expunge-thread (pop nodes))
6452                (setq thread (pop nodes))
6453                (gnus-summary-limit-children thread))))))
6454      gnus-newsgroup-dependencies)
6455     ;; If this limitation resulted in an empty group, we might
6456     ;; pop the previous limit and use it instead.
6457     (when (and (not gnus-newsgroup-limit)
6458                show-if-empty)
6459       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6460     gnus-newsgroup-limit))
6461
6462 (defun gnus-summary-limit-children (thread)
6463   "Return 1 if this subthread is visible and 0 if it is not."
6464   ;; First we get the number of visible children to this thread.  This
6465   ;; is done by recursing down the thread using this function, so this
6466   ;; will really go down to a leaf article first, before slowly
6467   ;; working its way up towards the root.
6468   (when thread
6469     (let ((children
6470            (if (cdr thread)
6471                (apply '+ (mapcar 'gnus-summary-limit-children
6472                                  (cdr thread)))
6473              0))
6474           (number (mail-header-number (car thread)))
6475           score)
6476       (if (and
6477            (not (memq number gnus-newsgroup-marked))
6478            (or
6479             ;; If this article is dormant and has absolutely no visible
6480             ;; children, then this article isn't visible.
6481             (and (memq number gnus-newsgroup-dormant)
6482                  (zerop children))
6483             ;; If this is "fetch-old-headered" and there is no
6484             ;; visible children, then we don't want this article.
6485             (and (eq gnus-fetch-old-headers 'some)
6486                  (gnus-summary-article-ancient-p number)
6487                  (zerop children))
6488             ;; If this is "fetch-old-headered" and `invisible', then
6489             ;; we don't want this article.
6490             (and (eq gnus-fetch-old-headers 'invisible)
6491                  (gnus-summary-article-ancient-p number))
6492             ;; If this is a sparsely inserted article with no children,
6493             ;; we don't want it.
6494             (and (eq gnus-build-sparse-threads 'some)
6495                  (gnus-summary-article-sparse-p number)
6496                  (zerop children))
6497             ;; If we use expunging, and this article is really
6498             ;; low-scored, then we don't want this article.
6499             (when (and gnus-summary-expunge-below
6500                        (< (setq score
6501                                 (or (cdr (assq number gnus-newsgroup-scored))
6502                                     gnus-summary-default-score))
6503                           gnus-summary-expunge-below))
6504               ;; We increase the expunge-tally here, but that has
6505               ;; nothing to do with the limits, really.
6506               (incf gnus-newsgroup-expunged-tally)
6507               ;; We also mark as read here, if that's wanted.
6508               (when (and gnus-summary-mark-below
6509                          (< score gnus-summary-mark-below))
6510                 (setq gnus-newsgroup-unreads
6511                       (delq number gnus-newsgroup-unreads))
6512                 (if gnus-newsgroup-auto-expire
6513                     (push number gnus-newsgroup-expirable)
6514                   (push (cons number gnus-low-score-mark)
6515                         gnus-newsgroup-reads)))
6516               t)
6517             ;; Check NoCeM things.
6518             (if (and gnus-use-nocem
6519                      (gnus-nocem-unwanted-article-p
6520                       (mail-header-id (car thread))))
6521                 (progn
6522                   (setq gnus-newsgroup-unreads
6523                         (delq number gnus-newsgroup-unreads))
6524                   t))))
6525           ;; Nope, invisible article.
6526           0
6527         ;; Ok, this article is to be visible, so we add it to the limit
6528         ;; and return 1.
6529         (push number gnus-newsgroup-limit)
6530         1))))
6531
6532 (defun gnus-expunge-thread (thread)
6533   "Mark all articles in THREAD as read."
6534   (let* ((number (mail-header-number (car thread))))
6535     (incf gnus-newsgroup-expunged-tally)
6536     ;; We also mark as read here, if that's wanted.
6537     (setq gnus-newsgroup-unreads
6538           (delq number gnus-newsgroup-unreads))
6539     (if gnus-newsgroup-auto-expire
6540         (push number gnus-newsgroup-expirable)
6541       (push (cons number gnus-low-score-mark)
6542             gnus-newsgroup-reads)))
6543   ;; Go recursively through all subthreads.
6544   (mapcar 'gnus-expunge-thread (cdr thread)))
6545
6546 ;; Summary article oriented commands
6547
6548 (defun gnus-summary-refer-parent-article (n)
6549   "Refer parent article N times.
6550 If N is negative, go to ancestor -N instead.
6551 The difference between N and the number of articles fetched is returned."
6552   (interactive "p")
6553   (let ((skip 1)
6554         error header ref)
6555     (when (not (natnump n))
6556       (setq skip (abs n)
6557             n 1))
6558     (while (and (> n 0)
6559                 (not error))
6560       (setq header (gnus-summary-article-header))
6561       (if (and (eq (mail-header-number header)
6562                    (cdr gnus-article-current))
6563                (equal gnus-newsgroup-name
6564                       (car gnus-article-current)))
6565           ;; If we try to find the parent of the currently
6566           ;; displayed article, then we take a look at the actual
6567           ;; References header, since this is slightly more
6568           ;; reliable than the References field we got from the
6569           ;; server.
6570           (save-excursion
6571             (set-buffer gnus-original-article-buffer)
6572             (nnheader-narrow-to-headers)
6573             (unless (setq ref (message-fetch-field "references"))
6574               (setq ref (message-fetch-field "in-reply-to")))
6575             (widen))
6576         (setq ref
6577               ;; It's not the current article, so we take a bet on
6578               ;; the value we got from the server.
6579               (mail-header-references header)))
6580       (if (and ref
6581                (not (equal ref "")))
6582           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6583             (gnus-message 1 "Couldn't find parent"))
6584         (gnus-message 1 "No references in article %d"
6585                       (gnus-summary-article-number))
6586         (setq error t))
6587       (decf n))
6588     (gnus-summary-position-point)
6589     n))
6590
6591 (defun gnus-summary-refer-references ()
6592   "Fetch all articles mentioned in the References header.
6593 Return the number of articles fetched."
6594   (interactive)
6595   (let ((ref (mail-header-references (gnus-summary-article-header)))
6596         (current (gnus-summary-article-number))
6597         (n 0))
6598     (if (or (not ref)
6599             (equal ref ""))
6600         (error "No References in the current article")
6601       ;; For each Message-ID in the References header...
6602       (while (string-match "<[^>]*>" ref)
6603         (incf n)
6604         ;; ... fetch that article.
6605         (gnus-summary-refer-article
6606          (prog1 (match-string 0 ref)
6607            (setq ref (substring ref (match-end 0))))))
6608       (gnus-summary-goto-subject current)
6609       (gnus-summary-position-point)
6610       n)))
6611
6612 (defun gnus-summary-refer-thread (&optional limit)
6613   "Fetch all articles in the current thread.
6614 If LIMIT (the numerical prefix), fetch that many old headers instead
6615 of what's specified by the `gnus-refer-thread-limit' variable."
6616   (interactive "P")
6617   (let ((id (mail-header-id (gnus-summary-article-header)))
6618         (limit (if limit (prefix-numeric-value limit)
6619                  gnus-refer-thread-limit)))
6620     ;; We want to fetch LIMIT *old* headers, but we also have to
6621     ;; re-fetch all the headers in the current buffer, because many of
6622     ;; them may be undisplayed.  So we adjust LIMIT.
6623     (when (numberp limit)
6624       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6625     (unless (eq gnus-fetch-old-headers 'invisible)
6626       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6627       ;; Retrieve the headers and read them in.
6628       (if (eq (gnus-retrieve-headers
6629                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6630               'nov)
6631           (gnus-build-all-threads)
6632         (error "Can't fetch thread from backends that don't support NOV"))
6633       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6634     (gnus-summary-limit-include-thread id)))
6635
6636 (defun gnus-summary-refer-article (message-id &optional arg)
6637   "Fetch an article specified by MESSAGE-ID.
6638 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6639 or `gnus-select-method', no matter what backend the article comes from."
6640   (interactive "sMessage-ID: \nP")
6641   (when (and (stringp message-id)
6642              (not (zerop (length message-id))))
6643     ;; Construct the correct Message-ID if necessary.
6644     ;; Suggested by tale@pawl.rpi.edu.
6645     (unless (string-match "^<" message-id)
6646       (setq message-id (concat "<" message-id)))
6647     (unless (string-match ">$" message-id)
6648       (setq message-id (concat message-id ">")))
6649     (let* ((header (gnus-id-to-header message-id))
6650            (sparse (and header
6651                         (gnus-summary-article-sparse-p
6652                          (mail-header-number header))
6653                         (memq (mail-header-number header)
6654                               gnus-newsgroup-limit))))
6655       (cond
6656        ;; If the article is present in the buffer we just go to it.
6657        ((and header
6658              (or (not (gnus-summary-article-sparse-p
6659                        (mail-header-number header)))
6660                  sparse))
6661         (prog1
6662             (gnus-summary-goto-article
6663              (mail-header-number header) nil t)
6664           (when sparse
6665             (gnus-summary-update-article (mail-header-number header)))))
6666        (t
6667         ;; We fetch the article
6668         (let ((gnus-override-method
6669                (cond ((gnus-news-group-p gnus-newsgroup-name)
6670                       gnus-refer-article-method)
6671                      (arg
6672                       (or gnus-refer-article-method gnus-select-method))
6673                      (t nil)))
6674               number)
6675           ;; Start the special refer-article method, if necessary.
6676           (when (and gnus-refer-article-method
6677                      (gnus-news-group-p gnus-newsgroup-name))
6678             (gnus-check-server gnus-refer-article-method))
6679           ;; Fetch the header, and display the article.
6680           (if (setq number (gnus-summary-insert-subject message-id))
6681               (gnus-summary-select-article nil nil nil number)
6682             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6683
6684 (defun gnus-summary-edit-parameters ()
6685   "Edit the group parameters of the current group."
6686   (interactive)
6687   (gnus-group-edit-group gnus-newsgroup-name 'params))
6688
6689 (defun gnus-summary-customize-parameters ()
6690   "Customize the group parameters of the current group."
6691   (interactive)
6692   (gnus-group-customize gnus-newsgroup-name))
6693
6694 (defun gnus-summary-enter-digest-group (&optional force)
6695   "Enter an nndoc group based on the current article.
6696 If FORCE, force a digest interpretation.  If not, try
6697 to guess what the document format is."
6698   (interactive "P")
6699   (let ((conf gnus-current-window-configuration))
6700     (save-excursion
6701       (gnus-summary-select-article))
6702     (setq gnus-current-window-configuration conf)
6703     (let* ((name (format "%s-%d"
6704                          (gnus-group-prefixed-name
6705                           gnus-newsgroup-name (list 'nndoc ""))
6706                          (save-excursion
6707                            (set-buffer gnus-summary-buffer)
6708                            gnus-current-article)))
6709            (ogroup gnus-newsgroup-name)
6710            (params (append (gnus-info-params (gnus-get-info ogroup))
6711                            (list (cons 'to-group ogroup))
6712                            (list (cons 'save-article-group ogroup))))
6713            (case-fold-search t)
6714            (buf (current-buffer))
6715            dig)
6716       (save-excursion
6717         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6718         (insert-buffer-substring gnus-original-article-buffer)
6719         ;; Remove lines that may lead nndoc to misinterpret the
6720         ;; document type.
6721         (narrow-to-region
6722          (goto-char (point-min))
6723          (or (search-forward "\n\n" nil t) (point)))
6724         (goto-char (point-min))
6725         (delete-matching-lines "^\\(Path\\):\\|^From ")
6726         (widen))
6727       (unwind-protect
6728           (if (gnus-group-read-ephemeral-group
6729                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6730                             (nndoc-article-type
6731                              ,(if force 'digest 'guess))) t)
6732               ;; Make all postings to this group go to the parent group.
6733               (nconc (gnus-info-params (gnus-get-info name))
6734                      params)
6735             ;; Couldn't select this doc group.
6736             (switch-to-buffer buf)
6737             (gnus-set-global-variables)
6738             (gnus-configure-windows 'summary)
6739             (gnus-message 3 "Article couldn't be entered?"))
6740         (kill-buffer dig)))))
6741
6742 (defun gnus-summary-read-document (n)
6743   "Open a new group based on the current article(s).
6744 This will allow you to read digests and other similar
6745 documents as newsgroups.
6746 Obeys the standard process/prefix convention."
6747   (interactive "P")
6748   (let* ((articles (gnus-summary-work-articles n))
6749          (ogroup gnus-newsgroup-name)
6750          (params (append (gnus-info-params (gnus-get-info ogroup))
6751                          (list (cons 'to-group ogroup))))
6752          article group egroup groups vgroup)
6753     (while (setq article (pop articles))
6754       (setq group (format "%s-%d" gnus-newsgroup-name article))
6755       (gnus-summary-remove-process-mark article)
6756       (when (gnus-summary-display-article article)
6757         (save-excursion
6758           (with-temp-buffer
6759             (insert-buffer-substring gnus-original-article-buffer)
6760             ;; Remove some headers that may lead nndoc to make
6761             ;; the wrong guess.
6762             (message-narrow-to-head)
6763             (goto-char (point-min))
6764             (delete-matching-lines "^\\(Path\\):\\|^From ")
6765             (widen)
6766             (if (setq egroup
6767                       (gnus-group-read-ephemeral-group
6768                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6769                                      (nndoc-article-type guess))
6770                        t nil t))
6771                 (progn
6772                   ;; Make all postings to this group go to the parent group.
6773                   (nconc (gnus-info-params (gnus-get-info egroup))
6774                          params)
6775                   (push egroup groups))
6776               ;; Couldn't select this doc group.
6777               (gnus-error 3 "Article couldn't be entered"))))))
6778     ;; Now we have selected all the documents.
6779     (cond
6780      ((not groups)
6781       (error "None of the articles could be interpreted as documents"))
6782      ((gnus-group-read-ephemeral-group
6783        (setq vgroup (format
6784                      "nnvirtual:%s-%s" gnus-newsgroup-name
6785                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6786        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6787        t
6788        (cons (current-buffer) 'summary)))
6789      (t
6790       (error "Couldn't select virtual nndoc group")))))
6791
6792 (defun gnus-summary-isearch-article (&optional regexp-p)
6793   "Do incremental search forward on the current article.
6794 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6795   (interactive "P")
6796   (gnus-summary-select-article)
6797   (gnus-configure-windows 'article)
6798   (gnus-eval-in-buffer-window gnus-article-buffer
6799     (save-restriction
6800       (widen)
6801       (isearch-forward regexp-p))))
6802
6803 (defun gnus-summary-search-article-forward (regexp &optional backward)
6804   "Search for an article containing REGEXP forward.
6805 If BACKWARD, search backward instead."
6806   (interactive
6807    (list (read-string
6808           (format "Search article %s (regexp%s): "
6809                   (if current-prefix-arg "backward" "forward")
6810                   (if gnus-last-search-regexp
6811                       (concat ", default " gnus-last-search-regexp)
6812                     "")))
6813          current-prefix-arg))
6814   (if (string-equal regexp "")
6815       (setq regexp (or gnus-last-search-regexp ""))
6816     (setq gnus-last-search-regexp regexp))
6817   (if (gnus-summary-search-article regexp backward)
6818       (gnus-summary-show-thread)
6819     (error "Search failed: \"%s\"" regexp)))
6820
6821 (defun gnus-summary-search-article-backward (regexp)
6822   "Search for an article containing REGEXP backward."
6823   (interactive
6824    (list (read-string
6825           (format "Search article backward (regexp%s): "
6826                   (if gnus-last-search-regexp
6827                       (concat ", default " gnus-last-search-regexp)
6828                     "")))))
6829   (gnus-summary-search-article-forward regexp 'backward))
6830
6831 (defun gnus-summary-search-article (regexp &optional backward)
6832   "Search for an article containing REGEXP.
6833 Optional argument BACKWARD means do search for backward.
6834 `gnus-select-article-hook' is not called during the search."
6835   ;; We have to require this here to make sure that the following
6836   ;; dynamic binding isn't shadowed by autoloading.
6837   (require 'gnus-async)
6838   (require 'gnus-art)
6839   (let ((gnus-select-article-hook nil)  ;Disable hook.
6840         (gnus-article-display-hook nil)
6841         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6842         (gnus-use-article-prefetch nil)
6843         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6844         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6845         (sum (current-buffer))
6846         (found nil)
6847         point)
6848     (gnus-save-hidden-threads
6849       (gnus-summary-select-article)
6850       (set-buffer gnus-article-buffer)
6851       (when backward
6852         (forward-line -1))
6853       (while (not found)
6854         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6855         (if (if backward
6856                 (re-search-backward regexp nil t)
6857               (re-search-forward regexp nil t))
6858             ;; We found the regexp.
6859             (progn
6860               (setq found 'found)
6861               (beginning-of-line)
6862               (set-window-start
6863                (get-buffer-window (current-buffer))
6864                (point))
6865               (forward-line 1)
6866               (set-buffer sum)
6867               (setq point (point)))
6868           ;; We didn't find it, so we go to the next article.
6869           (set-buffer sum)
6870           (setq found 'not)
6871           (while (eq found 'not)
6872             (if (not (if backward (gnus-summary-find-prev)
6873                        (gnus-summary-find-next)))
6874                 ;; No more articles.
6875                 (setq found t)
6876               ;; Select the next article and adjust point.
6877               (unless (gnus-summary-article-sparse-p
6878                        (gnus-summary-article-number))
6879                 (setq found nil)
6880                 (gnus-summary-select-article)
6881                 (set-buffer gnus-article-buffer)
6882                 (widen)
6883                 (goto-char (if backward (point-max) (point-min))))))))
6884       (gnus-message 7 ""))
6885     ;; Return whether we found the regexp.
6886     (when (eq found 'found)
6887       (goto-char point)
6888       (gnus-summary-show-thread)
6889       (gnus-summary-goto-subject gnus-current-article)
6890       (gnus-summary-position-point)
6891       t)))
6892
6893 (defun gnus-summary-find-matching (header regexp &optional backward unread
6894                                           not-case-fold)
6895   "Return a list of all articles that match REGEXP on HEADER.
6896 The search stars on the current article and goes forwards unless
6897 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6898 If UNREAD is non-nil, only unread articles will
6899 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6900 in the comparisons."
6901   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6902                 (gnus-data-find-list
6903                  (gnus-summary-article-number) (gnus-data-list backward))))
6904         (case-fold-search (not not-case-fold))
6905         articles d func)
6906     (if (consp header)
6907         (if (eq (car header) 'extra)
6908             (setq func
6909                   `(lambda (h)
6910                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6911                          "")))
6912           (error "%s is an invalid header" header))
6913       (unless (fboundp (intern (concat "mail-header-" header)))
6914         (error "%s is not a valid header" header))
6915       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6916     (while data
6917       (setq d (car data))
6918       (and (or (not unread)             ; We want all articles...
6919                (gnus-data-unread-p d))  ; Or just unreads.
6920            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6921            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6922            (push (gnus-data-number d) articles)) ; Success!
6923       (setq data (cdr data)))
6924     (nreverse articles)))
6925
6926 (defun gnus-summary-execute-command (header regexp command &optional backward)
6927   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6928 If HEADER is an empty string (or nil), the match is done on the entire
6929 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6930   (interactive
6931    (list (let ((completion-ignore-case t))
6932            (completing-read
6933             "Header name: "
6934             (mapcar (lambda (string) (list string))
6935                     '("Number" "Subject" "From" "Lines" "Date"
6936                       "Message-ID" "Xref" "References" "Body"))
6937             nil 'require-match))
6938          (read-string "Regexp: ")
6939          (read-key-sequence "Command: ")
6940          current-prefix-arg))
6941   (when (equal header "Body")
6942     (setq header ""))
6943   ;; Hidden thread subtrees must be searched as well.
6944   (gnus-summary-show-all-threads)
6945   ;; We don't want to change current point nor window configuration.
6946   (save-excursion
6947     (save-window-excursion
6948       (gnus-message 6 "Executing %s..." (key-description command))
6949       ;; We'd like to execute COMMAND interactively so as to give arguments.
6950       (gnus-execute header regexp
6951                     `(call-interactively ',(key-binding command))
6952                     backward)
6953       (gnus-message 6 "Executing %s...done" (key-description command)))))
6954
6955 (defun gnus-summary-beginning-of-article ()
6956   "Scroll the article back to the beginning."
6957   (interactive)
6958   (gnus-summary-select-article)
6959   (gnus-configure-windows 'article)
6960   (gnus-eval-in-buffer-window gnus-article-buffer
6961     (widen)
6962     (goto-char (point-min))
6963     (when gnus-page-broken
6964       (gnus-narrow-to-page))))
6965
6966 (defun gnus-summary-end-of-article ()
6967   "Scroll to the end of the article."
6968   (interactive)
6969   (gnus-summary-select-article)
6970   (gnus-configure-windows 'article)
6971   (gnus-eval-in-buffer-window gnus-article-buffer
6972     (widen)
6973     (goto-char (point-max))
6974     (recenter -3)
6975     (when gnus-page-broken
6976       (gnus-narrow-to-page))))
6977
6978 (defun gnus-summary-print-article (&optional filename n)
6979   "Generate and print a PostScript image of the N next (mail) articles.
6980
6981 If N is negative, print the N previous articles.  If N is nil and articles
6982 have been marked with the process mark, print these instead.
6983
6984 If the optional first argument FILENAME is nil, send the image to the
6985 printer.  If FILENAME is a string, save the PostScript image in a file with
6986 that name.  If FILENAME is a number, prompt the user for the name of the file
6987 to save in."
6988   (interactive (list (ps-print-preprint current-prefix-arg)
6989                      current-prefix-arg))
6990   (dolist (article (gnus-summary-work-articles n))
6991     (gnus-summary-select-article nil nil 'pseudo article)
6992     (gnus-eval-in-buffer-window gnus-article-buffer
6993       (let ((buffer (generate-new-buffer " *print*")))
6994         (unwind-protect
6995             (progn
6996               (copy-to-buffer buffer (point-min) (point-max))
6997               (set-buffer buffer)
6998               (gnus-article-delete-invisible-text)
6999               (let ((ps-left-header
7000                      (list
7001                       (concat "("
7002                               (mail-header-subject gnus-current-headers) ")")
7003                       (concat "("
7004                               (mail-header-from gnus-current-headers) ")")))
7005                     (ps-right-header
7006                      (list
7007                       "/pagenumberstring load"
7008                       (concat "("
7009                               (mail-header-date gnus-current-headers) ")"))))
7010                 (gnus-run-hooks 'gnus-ps-print-hook)
7011                 (save-excursion
7012                   (ps-print-buffer-with-faces filename))))
7013           (kill-buffer buffer))))))
7014
7015 (defun gnus-summary-show-article (&optional arg)
7016   "Force re-fetching of the current article.
7017 If ARG (the prefix) is non-nil, show the raw article without any
7018 article massaging functions being run."
7019   (interactive "P")
7020   (if (not arg)
7021       ;; Select the article the normal way.
7022       (gnus-summary-select-article nil 'force)
7023     ;; We have to require this here to make sure that the following
7024     ;; dynamic binding isn't shadowed by autoloading.
7025     (require 'gnus-async)
7026     (require 'gnus-art)
7027     ;; Bind the article treatment functions to nil.
7028     (let ((gnus-have-all-headers t)
7029           gnus-article-display-hook
7030           gnus-article-prepare-hook
7031           gnus-article-decode-hook
7032           gnus-break-pages
7033           gnus-show-mime
7034           gnus-visual)
7035       (gnus-summary-select-article nil 'force)))
7036   (gnus-summary-goto-subject gnus-current-article)
7037   (gnus-summary-position-point))
7038
7039 (defun gnus-summary-verbose-headers (&optional arg)
7040   "Toggle permanent full header display.
7041 If ARG is a positive number, turn header display on.
7042 If ARG is a negative number, turn header display off."
7043   (interactive "P")
7044   (setq gnus-show-all-headers
7045         (cond ((or (not (numberp arg))
7046                    (zerop arg))
7047                (not gnus-show-all-headers))
7048               ((natnump arg)
7049                t)))
7050   (gnus-summary-show-article))
7051
7052 (defun gnus-summary-toggle-header (&optional arg)
7053   "Show the headers if they are hidden, or hide them if they are shown.
7054 If ARG is a positive number, show the entire header.
7055 If ARG is a negative number, hide the unwanted header lines."
7056   (interactive "P")
7057   (save-excursion
7058     (set-buffer gnus-article-buffer)
7059     (save-restriction
7060       (let* ((buffer-read-only nil)
7061              (inhibit-point-motion-hooks t)
7062              hidden e)
7063         (save-restriction 
7064           (article-narrow-to-head)
7065           (setq hidden (gnus-article-hidden-text-p 'headers)))
7066         (goto-char (point-min))
7067         (when (search-forward "\n\n" nil t)
7068           (delete-region (point-min) (1- (point))))
7069         (goto-char (point-min))
7070         (save-excursion
7071           (set-buffer gnus-original-article-buffer)
7072           (goto-char (point-min))
7073           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7074         (insert-buffer-substring gnus-original-article-buffer 1 e)
7075         (save-restriction
7076           (narrow-to-region (point-min) (point))
7077           (article-decode-encoded-words)
7078           (if (or hidden
7079                   (and (numberp arg) (< arg 0)))
7080               (let ((gnus-treat-hide-headers nil)
7081                     (gnus-treat-hide-boring-headers nil))
7082                 (gnus-treat-article 'head))
7083             (gnus-treat-article 'head)))))))
7084
7085 (defun gnus-summary-show-all-headers ()
7086   "Make all header lines visible."
7087   (interactive)
7088   (gnus-article-show-all-headers))
7089
7090 (defun gnus-summary-toggle-mime (&optional arg)
7091   "Toggle MIME processing.
7092 If ARG is a positive number, turn MIME processing on."
7093   (interactive "P")
7094   (setq gnus-show-mime
7095         (if (null arg)
7096             (not gnus-show-mime)
7097           (> (prefix-numeric-value arg) 0)))
7098   (gnus-summary-select-article t 'force))
7099
7100 (defun gnus-summary-caesar-message (&optional arg)
7101   "Caesar rotate the current article by 13.
7102 The numerical prefix specifies how many places to rotate each letter
7103 forward."
7104   (interactive "P")
7105   (gnus-summary-select-article)
7106   (let ((mail-header-separator ""))
7107     (gnus-eval-in-buffer-window gnus-article-buffer
7108       (save-restriction
7109         (widen)
7110         (let ((start (window-start))
7111               buffer-read-only)
7112           (message-caesar-buffer-body arg)
7113           (set-window-start (get-buffer-window (current-buffer)) start))))))
7114
7115 (defun gnus-summary-stop-page-breaking ()
7116   "Stop page breaking in the current article."
7117   (interactive)
7118   (gnus-summary-select-article)
7119   (gnus-eval-in-buffer-window gnus-article-buffer
7120     (widen)
7121     (when (gnus-visual-p 'page-marker)
7122       (let ((buffer-read-only nil))
7123         (gnus-remove-text-with-property 'gnus-prev)
7124         (gnus-remove-text-with-property 'gnus-next))
7125       (setq gnus-page-broken nil))))
7126
7127 (defun gnus-summary-move-article (&optional n to-newsgroup
7128                                             select-method action)
7129   "Move the current article to a different newsgroup.
7130 If N is a positive number, move the N next articles.
7131 If N is a negative number, move the N previous articles.
7132 If N is nil and any articles have been marked with the process mark,
7133 move those articles instead.
7134 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7135 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7136 re-spool using this method.
7137
7138 For this function to work, both the current newsgroup and the
7139 newsgroup that you want to move to have to support the `request-move'
7140 and `request-accept' functions."
7141   (interactive "P")
7142   (unless action
7143     (setq action 'move))
7144   ;; Disable marking as read.
7145   (let (gnus-mark-article-hook)
7146     (save-window-excursion
7147       (gnus-summary-select-article)))
7148   ;; Check whether the source group supports the required functions.
7149   (cond ((and (eq action 'move)
7150               (not (gnus-check-backend-function
7151                     'request-move-article gnus-newsgroup-name)))
7152          (error "The current group does not support article moving"))
7153         ((and (eq action 'crosspost)
7154               (not (gnus-check-backend-function
7155                     'request-replace-article gnus-newsgroup-name)))
7156          (error "The current group does not support article editing")))
7157   (let ((articles (gnus-summary-work-articles n))
7158         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7159         (names '((move "Move" "Moving")
7160                  (copy "Copy" "Copying")
7161                  (crosspost "Crosspost" "Crossposting")))
7162         (copy-buf (save-excursion
7163                     (nnheader-set-temp-buffer " *copy article*")))
7164         (default-marks gnus-article-mark-lists)
7165         (no-expire-marks (delete '(expirable . expire)
7166                                  (copy-sequence gnus-article-mark-lists)))
7167         art-group to-method new-xref article to-groups)
7168     (unless (assq action names)
7169       (error "Unknown action %s" action))
7170     ;; Read the newsgroup name.
7171     (when (and (not to-newsgroup)
7172                (not select-method))
7173       (setq to-newsgroup
7174             (gnus-read-move-group-name
7175              (cadr (assq action names))
7176              (symbol-value (intern (format "gnus-current-%s-group" action)))
7177              articles prefix))
7178       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7179     (setq to-method (or select-method
7180                         (gnus-group-name-to-method to-newsgroup)))
7181     ;; Check the method we are to move this article to...
7182     (unless (gnus-check-backend-function
7183              'request-accept-article (car to-method))
7184       (error "%s does not support article copying" (car to-method)))
7185     (unless (gnus-check-server to-method)
7186       (error "Can't open server %s" (car to-method)))
7187     (gnus-message 6 "%s to %s: %s..."
7188                   (caddr (assq action names))
7189                   (or (car select-method) to-newsgroup) articles)
7190     (while articles
7191       (setq article (pop articles))
7192       (setq
7193        art-group
7194        (cond
7195         ;; Move the article.
7196         ((eq action 'move)
7197          ;; Remove this article from future suppression.
7198          (gnus-dup-unsuppress-article article)
7199          (gnus-request-move-article
7200           article                       ; Article to move
7201           gnus-newsgroup-name           ; From newsgroup
7202           (nth 1 (gnus-find-method-for-group
7203                   gnus-newsgroup-name)) ; Server
7204           (list 'gnus-request-accept-article
7205                 to-newsgroup (list 'quote select-method)
7206                 (not articles) t)               ; Accept form
7207           (not articles)))              ; Only save nov last time
7208         ;; Copy the article.
7209         ((eq action 'copy)
7210          (save-excursion
7211            (set-buffer copy-buf)
7212            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7213              (gnus-request-accept-article
7214               to-newsgroup select-method (not articles) t))))
7215         ;; Crosspost the article.
7216         ((eq action 'crosspost)
7217          (let ((xref (message-tokenize-header
7218                       (mail-header-xref (gnus-summary-article-header article))
7219                       " ")))
7220            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7221                                   ":" article))
7222            (unless xref
7223              (setq xref (list (system-name))))
7224            (setq new-xref
7225                  (concat
7226                   (mapconcat 'identity
7227                              (delete "Xref:" (delete new-xref xref))
7228                              " ")
7229                   " " new-xref))
7230            (save-excursion
7231              (set-buffer copy-buf)
7232              ;; First put the article in the destination group.
7233              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7234              (when (consp (setq art-group
7235                                 (gnus-request-accept-article
7236                                  to-newsgroup select-method (not articles))))
7237                (setq new-xref (concat new-xref " " (car art-group)
7238                                       ":" (cdr art-group)))
7239                ;; Now we have the new Xrefs header, so we insert
7240                ;; it and replace the new article.
7241                (nnheader-replace-header "Xref" new-xref)
7242                (gnus-request-replace-article
7243                 (cdr art-group) to-newsgroup (current-buffer))
7244                art-group))))))
7245       (cond
7246        ((not art-group)
7247         (gnus-message 1 "Couldn't %s article %s"
7248                       (cadr (assq action names)) article))
7249        ((and (eq art-group 'junk)
7250              (eq action 'move))
7251         (gnus-summary-mark-article article gnus-canceled-mark)
7252         (gnus-message 4 "Deleted article %s" article))
7253        (t
7254         (let* ((pto-group (gnus-group-prefixed-name
7255                            (car art-group) to-method))
7256                (entry
7257                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7258                (info (nth 2 entry))
7259                (to-group (gnus-info-group info)))
7260           ;; Update the group that has been moved to.
7261           (when (and info
7262                      (memq action '(move copy)))
7263             (unless (member to-group to-groups)
7264               (push to-group to-groups))
7265
7266             (unless (memq article gnus-newsgroup-unreads)
7267               (gnus-info-set-read
7268                info (gnus-add-to-range (gnus-info-read info)
7269                                        (list (cdr art-group)))))
7270
7271             ;; Copy any marks over to the new group.
7272             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7273                              default-marks
7274                            no-expire-marks))
7275                   (to-article (cdr art-group)))
7276
7277               ;; See whether the article is to be put in the cache.
7278               (when gnus-use-cache
7279                 (gnus-cache-possibly-enter-article
7280                  to-group to-article
7281                  (let ((header (copy-sequence
7282                                 (gnus-summary-article-header article))))
7283                    (mail-header-set-number header to-article)
7284                    header)
7285                  (memq article gnus-newsgroup-marked)
7286                  (memq article gnus-newsgroup-dormant)
7287                  (memq article gnus-newsgroup-unreads)))
7288
7289               (when (and (equal to-group gnus-newsgroup-name)
7290                          (not (memq article gnus-newsgroup-unreads)))
7291                 ;; Mark this article as read in this group.
7292                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7293                 (setcdr (gnus-active to-group) to-article)
7294                 (setcdr gnus-newsgroup-active to-article))
7295
7296               (while marks
7297                 (when (memq article (symbol-value
7298                                      (intern (format "gnus-newsgroup-%s"
7299                                                      (caar marks)))))
7300                   ;; If the other group is the same as this group,
7301                   ;; then we have to add the mark to the list.
7302                   (when (equal to-group gnus-newsgroup-name)
7303                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7304                          (cons to-article
7305                                (symbol-value
7306                                 (intern (format "gnus-newsgroup-%s"
7307                                                 (caar marks)))))))
7308                   ;; Copy the marks to other group.
7309                   (gnus-add-marked-articles
7310                    to-group (cdar marks) (list to-article) info))
7311                 (setq marks (cdr marks)))
7312
7313               (gnus-dribble-enter
7314                (concat "(gnus-group-set-info '"
7315                        (gnus-prin1-to-string (gnus-get-info to-group))
7316                        ")"))))
7317
7318           ;; Update the Xref header in this article to point to
7319           ;; the new crossposted article we have just created.
7320           (when (eq action 'crosspost)
7321             (save-excursion
7322               (set-buffer copy-buf)
7323               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7324               (nnheader-replace-header "Xref" new-xref)
7325               (gnus-request-replace-article
7326                article gnus-newsgroup-name (current-buffer)))))
7327
7328         ;;;!!!Why is this necessary?
7329         (set-buffer gnus-summary-buffer)
7330
7331         (gnus-summary-goto-subject article)
7332         (when (eq action 'move)
7333           (gnus-summary-mark-article article gnus-canceled-mark))))
7334       (gnus-summary-remove-process-mark article))
7335     ;; Re-activate all groups that have been moved to.
7336     (while to-groups
7337       (save-excursion
7338         (set-buffer gnus-group-buffer)
7339         (when (gnus-group-goto-group (car to-groups) t)
7340           (gnus-group-get-new-news-this-group 1 t))
7341         (pop to-groups)))
7342
7343     (gnus-kill-buffer copy-buf)
7344     (gnus-summary-position-point)
7345     (gnus-set-mode-line 'summary)))
7346
7347 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7348   "Move the current article to a different newsgroup.
7349 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7350 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7351 re-spool using this method."
7352   (interactive "P")
7353   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7354
7355 (defun gnus-summary-crosspost-article (&optional n)
7356   "Crosspost the current article to some other group."
7357   (interactive "P")
7358   (gnus-summary-move-article n nil nil 'crosspost))
7359
7360 (defcustom gnus-summary-respool-default-method nil
7361   "Default method for respooling an article.
7362 If nil, use to the current newsgroup method."
7363   :type '(choice (gnus-select-method :value (nnml ""))
7364                  (const nil))
7365   :group 'gnus-summary-mail)
7366
7367 (defun gnus-summary-respool-article (&optional n method)
7368   "Respool the current article.
7369 The article will be squeezed through the mail spooling process again,
7370 which means that it will be put in some mail newsgroup or other
7371 depending on `nnmail-split-methods'.
7372 If N is a positive number, respool the N next articles.
7373 If N is a negative number, respool the N previous articles.
7374 If N is nil and any articles have been marked with the process mark,
7375 respool those articles instead.
7376
7377 Respooling can be done both from mail groups and \"real\" newsgroups.
7378 In the former case, the articles in question will be moved from the
7379 current group into whatever groups they are destined to.  In the
7380 latter case, they will be copied into the relevant groups."
7381   (interactive
7382    (list current-prefix-arg
7383          (let* ((methods (gnus-methods-using 'respool))
7384                 (methname
7385                  (symbol-name (or gnus-summary-respool-default-method
7386                                   (car (gnus-find-method-for-group
7387                                         gnus-newsgroup-name)))))
7388                 (method
7389                  (gnus-completing-read
7390                   methname "What backend do you want to use when respooling?"
7391                   methods nil t nil 'gnus-mail-method-history))
7392                 ms)
7393            (cond
7394             ((zerop (length (setq ms (gnus-servers-using-backend
7395                                       (intern method)))))
7396              (list (intern method) ""))
7397             ((= 1 (length ms))
7398              (car ms))
7399             (t
7400              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7401                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7402                            ms-alist))))))))
7403   (unless method
7404     (error "No method given for respooling"))
7405   (if (assoc (symbol-name
7406               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7407              (gnus-methods-using 'respool))
7408       (gnus-summary-move-article n nil method)
7409     (gnus-summary-copy-article n nil method)))
7410
7411 (defun gnus-summary-import-article (file)
7412   "Import an arbitrary file into a mail newsgroup."
7413   (interactive "fImport file: ")
7414   (let ((group gnus-newsgroup-name)
7415         (now (current-time))
7416         atts lines)
7417     (unless (gnus-check-backend-function 'request-accept-article group)
7418       (error "%s does not support article importing" group))
7419     (or (file-readable-p file)
7420         (not (file-regular-p file))
7421         (error "Can't read %s" file))
7422     (save-excursion
7423       (set-buffer (gnus-get-buffer-create " *import file*"))
7424       (erase-buffer)
7425       (nnheader-insert-file-contents file)
7426       (goto-char (point-min))
7427       (unless (nnheader-article-p)
7428         ;; This doesn't look like an article, so we fudge some headers.
7429         (setq atts (file-attributes file)
7430               lines (count-lines (point-min) (point-max)))
7431         (insert "From: " (read-string "From: ") "\n"
7432                 "Subject: " (read-string "Subject: ") "\n"
7433                 "Date: " (message-make-date (nth 5 atts))
7434                 "\n"
7435                 "Message-ID: " (message-make-message-id) "\n"
7436                 "Lines: " (int-to-string lines) "\n"
7437                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7438       (gnus-request-accept-article group nil t)
7439       (kill-buffer (current-buffer)))))
7440
7441 (defun gnus-summary-article-posted-p ()
7442   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7443 This will be the case if the article has both been mailed and posted."
7444   (interactive)
7445   (let ((id (mail-header-references (gnus-summary-article-header)))
7446         (gnus-override-method
7447          (or gnus-refer-article-method gnus-select-method)))
7448     (if (gnus-request-head id "")
7449         (gnus-message 2 "The current message was found on %s"
7450                       gnus-override-method)
7451       (gnus-message 2 "The current message couldn't be found on %s"
7452                     gnus-override-method)
7453       nil)))
7454
7455 (defun gnus-summary-expire-articles (&optional now)
7456   "Expire all articles that are marked as expirable in the current group."
7457   (interactive)
7458   (when (gnus-check-backend-function
7459          'request-expire-articles gnus-newsgroup-name)
7460     ;; This backend supports expiry.
7461     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7462            (expirable (if total
7463                           (progn
7464                             ;; We need to update the info for
7465                             ;; this group for `gnus-list-of-read-articles'
7466                             ;; to give us the right answer.
7467                             (gnus-run-hooks 'gnus-exit-group-hook)
7468                             (gnus-summary-update-info)
7469                             (gnus-list-of-read-articles gnus-newsgroup-name))
7470                         (setq gnus-newsgroup-expirable
7471                               (sort gnus-newsgroup-expirable '<))))
7472            (expiry-wait (if now 'immediate
7473                           (gnus-group-find-parameter
7474                            gnus-newsgroup-name 'expiry-wait)))
7475            es)
7476       (when expirable
7477         ;; There are expirable articles in this group, so we run them
7478         ;; through the expiry process.
7479         (gnus-message 6 "Expiring articles...")
7480         (unless (gnus-check-group gnus-newsgroup-name)
7481           (error "Can't open server for %s" gnus-newsgroup-name))
7482         ;; The list of articles that weren't expired is returned.
7483         (save-excursion
7484           (if expiry-wait
7485               (let ((nnmail-expiry-wait-function nil)
7486                     (nnmail-expiry-wait expiry-wait))
7487                 (setq es (gnus-request-expire-articles
7488                           expirable gnus-newsgroup-name)))
7489             (setq es (gnus-request-expire-articles
7490                       expirable gnus-newsgroup-name))))
7491         (unless total
7492           (setq gnus-newsgroup-expirable es))
7493         ;; We go through the old list of expirable, and mark all
7494         ;; really expired articles as nonexistent.
7495         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7496           (let ((gnus-use-cache nil))
7497             (while expirable
7498               (unless (memq (car expirable) es)
7499                 (when (gnus-data-find (car expirable))
7500                   (gnus-summary-mark-article
7501                    (car expirable) gnus-canceled-mark)))
7502               (setq expirable (cdr expirable)))))
7503         (gnus-message 6 "Expiring articles...done")))))
7504
7505 (defun gnus-summary-expire-articles-now ()
7506   "Expunge all expirable articles in the current group.
7507 This means that *all* articles that are marked as expirable will be
7508 deleted forever, right now."
7509   (interactive)
7510   (or gnus-expert-user
7511       (gnus-yes-or-no-p
7512        "Are you really, really, really sure you want to delete all these messages? ")
7513       (error "Phew!"))
7514   (gnus-summary-expire-articles t))
7515
7516 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7517 (defun gnus-summary-delete-article (&optional n)
7518   "Delete the N next (mail) articles.
7519 This command actually deletes articles.  This is not a marking
7520 command.  The article will disappear forever from your life, never to
7521 return.
7522 If N is negative, delete backwards.
7523 If N is nil and articles have been marked with the process mark,
7524 delete these instead."
7525   (interactive "P")
7526   (unless (gnus-check-backend-function 'request-expire-articles
7527                                        gnus-newsgroup-name)
7528     (error "The current newsgroup does not support article deletion"))
7529   ;; Compute the list of articles to delete.
7530   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7531         not-deleted)
7532     (if (and gnus-novice-user
7533              (not (gnus-yes-or-no-p
7534                    (format "Do you really want to delete %s forever? "
7535                            (if (> (length articles) 1)
7536                                (format "these %s articles" (length articles))
7537                              "this article")))))
7538         ()
7539       ;; Delete the articles.
7540       (setq not-deleted (gnus-request-expire-articles
7541                          articles gnus-newsgroup-name 'force))
7542       (while articles
7543         (gnus-summary-remove-process-mark (car articles))
7544         ;; The backend might not have been able to delete the article
7545         ;; after all.
7546         (unless (memq (car articles) not-deleted)
7547           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7548         (setq articles (cdr articles)))
7549       (when not-deleted
7550         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7551     (gnus-summary-position-point)
7552     (gnus-set-mode-line 'summary)
7553     not-deleted))
7554
7555 (defun gnus-summary-edit-article (&optional force)
7556   "Edit the current article.
7557 This will have permanent effect only in mail groups.
7558 If FORCE is non-nil, allow editing of articles even in read-only
7559 groups."
7560   (interactive "P")
7561   (save-excursion
7562     (set-buffer gnus-summary-buffer)
7563     (let ((mail-parse-charset gnus-newsgroup-charset)
7564           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7565       (gnus-set-global-variables)
7566       (when (and (not force)
7567                  (gnus-group-read-only-p))
7568         (error "The current newsgroup does not support article editing"))
7569       (gnus-summary-show-article t)
7570       (gnus-article-edit-article
7571        'ignore
7572        `(lambda (no-highlight)
7573           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7574                 (mail-parse-ignored-charsets
7575                  ',gnus-newsgroup-ignored-charsets))
7576             (gnus-summary-edit-article-done
7577              ,(or (mail-header-references gnus-current-headers) "")
7578              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7579
7580 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7581
7582 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7583                                                  no-highlight)
7584   "Make edits to the current article permanent."
7585   (interactive)
7586   ;; Replace the article.
7587   (let ((buf (current-buffer)))
7588     (with-temp-buffer
7589       (insert-buffer-substring buf)
7590       (if (and (not read-only)
7591                (not (gnus-request-replace-article
7592                      (cdr gnus-article-current) (car gnus-article-current)
7593                      (current-buffer) t)))
7594           (error "Couldn't replace article")
7595         ;; Update the summary buffer.
7596         (if (and references
7597                  (equal (message-tokenize-header references " ")
7598                         (message-tokenize-header
7599                          (or (message-fetch-field "references") "") " ")))
7600             ;; We only have to update this line.
7601             (save-excursion
7602               (save-restriction
7603                 (message-narrow-to-head)
7604                 (let ((head (buffer-string))
7605                       header)
7606                   (with-temp-buffer
7607                     (insert (format "211 %d Article retrieved.\n"
7608                                     (cdr gnus-article-current)))
7609                     (insert head)
7610                     (insert ".\n")
7611                     (let ((nntp-server-buffer (current-buffer)))
7612                       (setq header (car (gnus-get-newsgroup-headers
7613                                          (save-excursion
7614                                            (set-buffer gnus-summary-buffer)
7615                                            gnus-newsgroup-dependencies)
7616                                          t))))
7617                     (save-excursion
7618                       (set-buffer gnus-summary-buffer)
7619                       (gnus-data-set-header
7620                        (gnus-data-find (cdr gnus-article-current))
7621                        header)
7622                       (gnus-summary-update-article-line
7623                        (cdr gnus-article-current) header))))))
7624           ;; Update threads.
7625           (set-buffer (or buffer gnus-summary-buffer))
7626           (gnus-summary-update-article (cdr gnus-article-current)))
7627         ;; Prettify the article buffer again.
7628         (unless no-highlight
7629           (save-excursion
7630             (set-buffer gnus-article-buffer)
7631             ;;;!!! Fix this -- article should be rehighlighted.
7632             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7633             (set-buffer gnus-original-article-buffer)
7634             (gnus-request-article
7635              (cdr gnus-article-current)
7636              (car gnus-article-current) (current-buffer))))
7637         ;; Prettify the summary buffer line.
7638         (when (gnus-visual-p 'summary-highlight 'highlight)
7639           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7640
7641 (defun gnus-summary-edit-wash (key)
7642   "Perform editing command KEY in the article buffer."
7643   (interactive
7644    (list
7645     (progn
7646       (message "%s" (concat (this-command-keys) "- "))
7647       (read-char))))
7648   (message "")
7649   (gnus-summary-edit-article)
7650   (execute-kbd-macro (concat (this-command-keys) key))
7651   (gnus-article-edit-done))
7652
7653 ;;; Respooling
7654
7655 (defun gnus-summary-respool-query (&optional silent trace)
7656   "Query where the respool algorithm would put this article."
7657   (interactive)
7658   (let (gnus-mark-article-hook)
7659     (gnus-summary-select-article)
7660     (save-excursion
7661       (set-buffer gnus-original-article-buffer)
7662       (save-restriction
7663         (message-narrow-to-head)
7664         (let ((groups (nnmail-article-group 'identity trace)))
7665           (unless silent
7666             (if groups
7667                 (message "This message would go to %s"
7668                          (mapconcat 'car groups ", "))
7669               (message "This message would go to no groups"))
7670             groups))))))
7671
7672 (defun gnus-summary-respool-trace ()
7673   "Trace where the respool algorithm would put this article.
7674 Display a buffer showing all fancy splitting patterns which matched."
7675   (interactive)
7676   (gnus-summary-respool-query nil t))
7677
7678 ;; Summary marking commands.
7679
7680 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7681   "Mark articles which has the same subject as read, and then select the next.
7682 If UNMARK is positive, remove any kind of mark.
7683 If UNMARK is negative, tick articles."
7684   (interactive "P")
7685   (when unmark
7686     (setq unmark (prefix-numeric-value unmark)))
7687   (let ((count
7688          (gnus-summary-mark-same-subject
7689           (gnus-summary-article-subject) unmark)))
7690     ;; Select next unread article.  If auto-select-same mode, should
7691     ;; select the first unread article.
7692     (gnus-summary-next-article t (and gnus-auto-select-same
7693                                       (gnus-summary-article-subject)))
7694     (gnus-message 7 "%d article%s marked as %s"
7695                   count (if (= count 1) " is" "s are")
7696                   (if unmark "unread" "read"))))
7697
7698 (defun gnus-summary-kill-same-subject (&optional unmark)
7699   "Mark articles which has the same subject as read.
7700 If UNMARK is positive, remove any kind of mark.
7701 If UNMARK is negative, tick articles."
7702   (interactive "P")
7703   (when unmark
7704     (setq unmark (prefix-numeric-value unmark)))
7705   (let ((count
7706          (gnus-summary-mark-same-subject
7707           (gnus-summary-article-subject) unmark)))
7708     ;; If marked as read, go to next unread subject.
7709     (when (null unmark)
7710       ;; Go to next unread subject.
7711       (gnus-summary-next-subject 1 t))
7712     (gnus-message 7 "%d articles are marked as %s"
7713                   count (if unmark "unread" "read"))))
7714
7715 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7716   "Mark articles with same SUBJECT as read, and return marked number.
7717 If optional argument UNMARK is positive, remove any kinds of marks.
7718 If optional argument UNMARK is negative, mark articles as unread instead."
7719   (let ((count 1))
7720     (save-excursion
7721       (cond
7722        ((null unmark)                   ; Mark as read.
7723         (while (and
7724                 (progn
7725                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7726                   (gnus-summary-show-thread) t)
7727                 (gnus-summary-find-subject subject))
7728           (setq count (1+ count))))
7729        ((> unmark 0)                    ; Tick.
7730         (while (and
7731                 (progn
7732                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7733                   (gnus-summary-show-thread) t)
7734                 (gnus-summary-find-subject subject))
7735           (setq count (1+ count))))
7736        (t                               ; Mark as unread.
7737         (while (and
7738                 (progn
7739                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7740                   (gnus-summary-show-thread) t)
7741                 (gnus-summary-find-subject subject))
7742           (setq count (1+ count)))))
7743       (gnus-set-mode-line 'summary)
7744       ;; Return the number of marked articles.
7745       count)))
7746
7747 (defun gnus-summary-mark-as-processable (n &optional unmark)
7748   "Set the process mark on the next N articles.
7749 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7750 the process mark instead.  The difference between N and the actual
7751 number of articles marked is returned."
7752   (interactive "p")
7753   (let ((backward (< n 0))
7754         (n (abs n)))
7755     (while (and
7756             (> n 0)
7757             (if unmark
7758                 (gnus-summary-remove-process-mark
7759                  (gnus-summary-article-number))
7760               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7761             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7762       (setq n (1- n)))
7763     (when (/= 0 n)
7764       (gnus-message 7 "No more articles"))
7765     (gnus-summary-recenter)
7766     (gnus-summary-position-point)
7767     n))
7768
7769 (defun gnus-summary-unmark-as-processable (n)
7770   "Remove the process mark from the next N articles.
7771 If N is negative, unmark backward instead.  The difference between N and
7772 the actual number of articles unmarked is returned."
7773   (interactive "p")
7774   (gnus-summary-mark-as-processable n t))
7775
7776 (defun gnus-summary-unmark-all-processable ()
7777   "Remove the process mark from all articles."
7778   (interactive)
7779   (save-excursion
7780     (while gnus-newsgroup-processable
7781       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7782   (gnus-summary-position-point))
7783
7784 (defun gnus-summary-mark-as-expirable (n)
7785   "Mark N articles forward as expirable.
7786 If N is negative, mark backward instead.  The difference between N and
7787 the actual number of articles marked is returned."
7788   (interactive "p")
7789   (gnus-summary-mark-forward n gnus-expirable-mark))
7790
7791 (defun gnus-summary-mark-article-as-replied (article)
7792   "Mark ARTICLE replied and update the summary line."
7793   (push article gnus-newsgroup-replied)
7794   (let ((buffer-read-only nil))
7795     (when (gnus-summary-goto-subject article nil t)
7796       (gnus-summary-update-secondary-mark article))))
7797
7798 (defun gnus-summary-set-bookmark (article)
7799   "Set a bookmark in current article."
7800   (interactive (list (gnus-summary-article-number)))
7801   (when (or (not (get-buffer gnus-article-buffer))
7802             (not gnus-current-article)
7803             (not gnus-article-current)
7804             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7805     (error "No current article selected"))
7806   ;; Remove old bookmark, if one exists.
7807   (let ((old (assq article gnus-newsgroup-bookmarks)))
7808     (when old
7809       (setq gnus-newsgroup-bookmarks
7810             (delq old gnus-newsgroup-bookmarks))))
7811   ;; Set the new bookmark, which is on the form
7812   ;; (article-number . line-number-in-body).
7813   (push
7814    (cons article
7815          (save-excursion
7816            (set-buffer gnus-article-buffer)
7817            (count-lines
7818             (min (point)
7819                  (save-excursion
7820                    (goto-char (point-min))
7821                    (search-forward "\n\n" nil t)
7822                    (point)))
7823             (point))))
7824    gnus-newsgroup-bookmarks)
7825   (gnus-message 6 "A bookmark has been added to the current article."))
7826
7827 (defun gnus-summary-remove-bookmark (article)
7828   "Remove the bookmark from the current article."
7829   (interactive (list (gnus-summary-article-number)))
7830   ;; Remove old bookmark, if one exists.
7831   (let ((old (assq article gnus-newsgroup-bookmarks)))
7832     (if old
7833         (progn
7834           (setq gnus-newsgroup-bookmarks
7835                 (delq old gnus-newsgroup-bookmarks))
7836           (gnus-message 6 "Removed bookmark."))
7837       (gnus-message 6 "No bookmark in current article."))))
7838
7839 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7840 (defun gnus-summary-mark-as-dormant (n)
7841   "Mark N articles forward as dormant.
7842 If N is negative, mark backward instead.  The difference between N and
7843 the actual number of articles marked is returned."
7844   (interactive "p")
7845   (gnus-summary-mark-forward n gnus-dormant-mark))
7846
7847 (defun gnus-summary-set-process-mark (article)
7848   "Set the process mark on ARTICLE and update the summary line."
7849   (setq gnus-newsgroup-processable
7850         (cons article
7851               (delq article gnus-newsgroup-processable)))
7852   (when (gnus-summary-goto-subject article)
7853     (gnus-summary-show-thread)
7854     (gnus-summary-goto-subject article)
7855     (gnus-summary-update-secondary-mark article)))
7856
7857 (defun gnus-summary-remove-process-mark (article)
7858   "Remove the process mark from ARTICLE and update the summary line."
7859   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7860   (when (gnus-summary-goto-subject article)
7861     (gnus-summary-show-thread)
7862     (gnus-summary-goto-subject article)
7863     (gnus-summary-update-secondary-mark article)))
7864
7865 (defun gnus-summary-set-saved-mark (article)
7866   "Set the process mark on ARTICLE and update the summary line."
7867   (push article gnus-newsgroup-saved)
7868   (when (gnus-summary-goto-subject article)
7869     (gnus-summary-update-secondary-mark article)))
7870
7871 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7872   "Mark N articles as read forwards.
7873 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7874 The difference between N and the actual number of articles marked is
7875 returned."
7876   (interactive "p")
7877   (gnus-summary-show-thread)
7878   (let ((backward (< n 0))
7879         (gnus-summary-goto-unread
7880          (and gnus-summary-goto-unread
7881               (not (eq gnus-summary-goto-unread 'never))
7882               (not (memq mark (list gnus-unread-mark
7883                                     gnus-ticked-mark gnus-dormant-mark)))))
7884         (n (abs n))
7885         (mark (or mark gnus-del-mark)))
7886     (while (and (> n 0)
7887                 (gnus-summary-mark-article nil mark no-expire)
7888                 (zerop (gnus-summary-next-subject
7889                         (if backward -1 1)
7890                         (and gnus-summary-goto-unread
7891                              (not (eq gnus-summary-goto-unread 'never)))
7892                         t)))
7893       (setq n (1- n)))
7894     (when (/= 0 n)
7895       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7896     (gnus-summary-recenter)
7897     (gnus-summary-position-point)
7898     (gnus-set-mode-line 'summary)
7899     n))
7900
7901 (defun gnus-summary-mark-article-as-read (mark)
7902   "Mark the current article quickly as read with MARK."
7903   (let ((article (gnus-summary-article-number)))
7904     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7905     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7906     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7907     (push (cons article mark) gnus-newsgroup-reads)
7908     ;; Possibly remove from cache, if that is used.
7909     (when gnus-use-cache
7910       (gnus-cache-enter-remove-article article))
7911     ;; Allow the backend to change the mark.
7912     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7913     ;; Check for auto-expiry.
7914     (when (and gnus-newsgroup-auto-expire
7915                (memq mark gnus-auto-expirable-marks))
7916       (setq mark gnus-expirable-mark)
7917       ;; Let the backend know about the mark change.
7918       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7919       (push article gnus-newsgroup-expirable))
7920     ;; Set the mark in the buffer.
7921     (gnus-summary-update-mark mark 'unread)
7922     t))
7923
7924 (defun gnus-summary-mark-article-as-unread (mark)
7925   "Mark the current article quickly as unread with MARK."
7926   (let* ((article (gnus-summary-article-number))
7927          (old-mark (gnus-summary-article-mark article)))
7928     ;; Allow the backend to change the mark.
7929     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7930     (if (eq mark old-mark)
7931         t
7932       (if (<= article 0)
7933           (progn
7934             (gnus-error 1 "Can't mark negative article numbers")
7935             nil)
7936         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7937         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7938         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7939         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7940         (cond ((= mark gnus-ticked-mark)
7941                (push article gnus-newsgroup-marked))
7942               ((= mark gnus-dormant-mark)
7943                (push article gnus-newsgroup-dormant))
7944               (t
7945                (push article gnus-newsgroup-unreads)))
7946         (gnus-pull article gnus-newsgroup-reads)
7947
7948         ;; See whether the article is to be put in the cache.
7949         (and gnus-use-cache
7950              (vectorp (gnus-summary-article-header article))
7951              (save-excursion
7952                (gnus-cache-possibly-enter-article
7953                 gnus-newsgroup-name article
7954                 (gnus-summary-article-header article)
7955                 (= mark gnus-ticked-mark)
7956                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7957
7958         ;; Fix the mark.
7959         (gnus-summary-update-mark mark 'unread)
7960         t))))
7961
7962 (defun gnus-summary-mark-article (&optional article mark no-expire)
7963   "Mark ARTICLE with MARK.  MARK can be any character.
7964 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7965 `??' (dormant) and `?E' (expirable).
7966 If MARK is nil, then the default character `?r' is used.
7967 If ARTICLE is nil, then the article on the current line will be
7968 marked."
7969   ;; The mark might be a string.
7970   (when (stringp mark)
7971     (setq mark (aref mark 0)))
7972   ;; If no mark is given, then we check auto-expiring.
7973   (when (null mark)
7974     (setq mark gnus-del-mark))
7975   (when (and (not no-expire)
7976              gnus-newsgroup-auto-expire
7977              (memq mark gnus-auto-expirable-marks))
7978     (setq mark gnus-expirable-mark))
7979   (let ((article (or article (gnus-summary-article-number)))
7980         (old-mark (gnus-summary-article-mark article)))
7981     ;; Allow the backend to change the mark.
7982     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7983     (if (eq mark old-mark)
7984         t
7985       (unless article
7986         (error "No article on current line"))
7987       (if (not (if (or (= mark gnus-unread-mark)
7988                        (= mark gnus-ticked-mark)
7989                        (= mark gnus-dormant-mark))
7990                    (gnus-mark-article-as-unread article mark)
7991                  (gnus-mark-article-as-read article mark)))
7992           t
7993         ;; See whether the article is to be put in the cache.
7994         (and gnus-use-cache
7995              (not (= mark gnus-canceled-mark))
7996              (vectorp (gnus-summary-article-header article))
7997              (save-excursion
7998                (gnus-cache-possibly-enter-article
7999                 gnus-newsgroup-name article
8000                 (gnus-summary-article-header article)
8001                 (= mark gnus-ticked-mark)
8002                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8003
8004         (when (gnus-summary-goto-subject article nil t)
8005           (let ((buffer-read-only nil))
8006             (gnus-summary-show-thread)
8007             ;; Fix the mark.
8008             (gnus-summary-update-mark mark 'unread)
8009             t))))))
8010
8011 (defun gnus-summary-update-secondary-mark (article)
8012   "Update the secondary (read, process, cache) mark."
8013   (gnus-summary-update-mark
8014    (cond ((memq article gnus-newsgroup-processable)
8015           gnus-process-mark)
8016          ((memq article gnus-newsgroup-cached)
8017           gnus-cached-mark)
8018          ((memq article gnus-newsgroup-replied)
8019           gnus-replied-mark)
8020          ((memq article gnus-newsgroup-saved)
8021           gnus-saved-mark)
8022          (t gnus-unread-mark))
8023    'replied)
8024   (when (gnus-visual-p 'summary-highlight 'highlight)
8025     (gnus-run-hooks 'gnus-summary-update-hook))
8026   t)
8027
8028 (defun gnus-summary-update-mark (mark type)
8029   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8030         (buffer-read-only nil))
8031     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8032     (when forward
8033       (when (looking-at "\r")
8034         (incf forward))
8035       (when (<= (+ forward (point)) (point-max))
8036         ;; Go to the right position on the line.
8037         (goto-char (+ forward (point)))
8038         ;; Replace the old mark with the new mark.
8039         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8040         ;; Optionally update the marks by some user rule.
8041         (when (eq type 'unread)
8042           (gnus-data-set-mark
8043            (gnus-data-find (gnus-summary-article-number)) mark)
8044           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8045
8046 (defun gnus-mark-article-as-read (article &optional mark)
8047   "Enter ARTICLE in the pertinent lists and remove it from others."
8048   ;; Make the article expirable.
8049   (let ((mark (or mark gnus-del-mark)))
8050     (if (= mark gnus-expirable-mark)
8051         (push article gnus-newsgroup-expirable)
8052       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8053     ;; Remove from unread and marked lists.
8054     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8055     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8056     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8057     (push (cons article mark) gnus-newsgroup-reads)
8058     ;; Possibly remove from cache, if that is used.
8059     (when gnus-use-cache
8060       (gnus-cache-enter-remove-article article))
8061     t))
8062
8063 (defun gnus-mark-article-as-unread (article &optional mark)
8064   "Enter ARTICLE in the pertinent lists and remove it from others."
8065   (let ((mark (or mark gnus-ticked-mark)))
8066     (if (<= article 0)
8067         (progn
8068           (gnus-error 1 "Can't mark negative article numbers")
8069           nil)
8070       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8071             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8072             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8073             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8074
8075       ;; Unsuppress duplicates?
8076       (when gnus-suppress-duplicates
8077         (gnus-dup-unsuppress-article article))
8078
8079       (cond ((= mark gnus-ticked-mark)
8080              (push article gnus-newsgroup-marked))
8081             ((= mark gnus-dormant-mark)
8082              (push article gnus-newsgroup-dormant))
8083             (t
8084              (push article gnus-newsgroup-unreads)))
8085       (gnus-pull article gnus-newsgroup-reads)
8086       t)))
8087
8088 (defalias 'gnus-summary-mark-as-unread-forward
8089   'gnus-summary-tick-article-forward)
8090 (make-obsolete 'gnus-summary-mark-as-unread-forward
8091                'gnus-summary-tick-article-forward)
8092 (defun gnus-summary-tick-article-forward (n)
8093   "Tick N articles forwards.
8094 If N is negative, tick backwards instead.
8095 The difference between N and the number of articles ticked is returned."
8096   (interactive "p")
8097   (gnus-summary-mark-forward n gnus-ticked-mark))
8098
8099 (defalias 'gnus-summary-mark-as-unread-backward
8100   'gnus-summary-tick-article-backward)
8101 (make-obsolete 'gnus-summary-mark-as-unread-backward
8102                'gnus-summary-tick-article-backward)
8103 (defun gnus-summary-tick-article-backward (n)
8104   "Tick N articles backwards.
8105 The difference between N and the number of articles ticked is returned."
8106   (interactive "p")
8107   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8108
8109 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8110 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8111 (defun gnus-summary-tick-article (&optional article clear-mark)
8112   "Mark current article as unread.
8113 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8114 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8115   (interactive)
8116   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8117                                        gnus-ticked-mark)))
8118
8119 (defun gnus-summary-mark-as-read-forward (n)
8120   "Mark N articles as read forwards.
8121 If N is negative, mark backwards instead.
8122 The difference between N and the actual number of articles marked is
8123 returned."
8124   (interactive "p")
8125   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8126
8127 (defun gnus-summary-mark-as-read-backward (n)
8128   "Mark the N articles as read backwards.
8129 The difference between N and the actual number of articles marked is
8130 returned."
8131   (interactive "p")
8132   (gnus-summary-mark-forward
8133    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8134
8135 (defun gnus-summary-mark-as-read (&optional article mark)
8136   "Mark current article as read.
8137 ARTICLE specifies the article to be marked as read.
8138 MARK specifies a string to be inserted at the beginning of the line."
8139   (gnus-summary-mark-article article mark))
8140
8141 (defun gnus-summary-clear-mark-forward (n)
8142   "Clear marks from N articles forward.
8143 If N is negative, clear backward instead.
8144 The difference between N and the number of marks cleared is returned."
8145   (interactive "p")
8146   (gnus-summary-mark-forward n gnus-unread-mark))
8147
8148 (defun gnus-summary-clear-mark-backward (n)
8149   "Clear marks from N articles backward.
8150 The difference between N and the number of marks cleared is returned."
8151   (interactive "p")
8152   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8153
8154 (defun gnus-summary-mark-unread-as-read ()
8155   "Intended to be used by `gnus-summary-mark-article-hook'."
8156   (when (memq gnus-current-article gnus-newsgroup-unreads)
8157     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8158
8159 (defun gnus-summary-mark-read-and-unread-as-read ()
8160   "Intended to be used by `gnus-summary-mark-article-hook'."
8161   (let ((mark (gnus-summary-article-mark)))
8162     (when (or (gnus-unread-mark-p mark)
8163               (gnus-read-mark-p mark))
8164       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8165
8166 (defun gnus-summary-mark-region-as-read (point mark all)
8167   "Mark all unread articles between point and mark as read.
8168 If given a prefix, mark all articles between point and mark as read,
8169 even ticked and dormant ones."
8170   (interactive "r\nP")
8171   (save-excursion
8172     (let (article)
8173       (goto-char point)
8174       (beginning-of-line)
8175       (while (and
8176               (< (point) mark)
8177               (progn
8178                 (when (or all
8179                           (memq (setq article (gnus-summary-article-number))
8180                                 gnus-newsgroup-unreads))
8181                   (gnus-summary-mark-article article gnus-del-mark))
8182                 t)
8183               (gnus-summary-find-next))))))
8184
8185 (defun gnus-summary-mark-below (score mark)
8186   "Mark articles with score less than SCORE with MARK."
8187   (interactive "P\ncMark: ")
8188   (setq score (if score
8189                   (prefix-numeric-value score)
8190                 (or gnus-summary-default-score 0)))
8191   (save-excursion
8192     (set-buffer gnus-summary-buffer)
8193     (goto-char (point-min))
8194     (while
8195         (progn
8196           (and (< (gnus-summary-article-score) score)
8197                (gnus-summary-mark-article nil mark))
8198           (gnus-summary-find-next)))))
8199
8200 (defun gnus-summary-kill-below (&optional score)
8201   "Mark articles with score below SCORE as read."
8202   (interactive "P")
8203   (gnus-summary-mark-below score gnus-killed-mark))
8204
8205 (defun gnus-summary-clear-above (&optional score)
8206   "Clear all marks from articles with score above SCORE."
8207   (interactive "P")
8208   (gnus-summary-mark-above score gnus-unread-mark))
8209
8210 (defun gnus-summary-tick-above (&optional score)
8211   "Tick all articles with score above SCORE."
8212   (interactive "P")
8213   (gnus-summary-mark-above score gnus-ticked-mark))
8214
8215 (defun gnus-summary-mark-above (score mark)
8216   "Mark articles with score over SCORE with MARK."
8217   (interactive "P\ncMark: ")
8218   (setq score (if score
8219                   (prefix-numeric-value score)
8220                 (or gnus-summary-default-score 0)))
8221   (save-excursion
8222     (set-buffer gnus-summary-buffer)
8223     (goto-char (point-min))
8224     (while (and (progn
8225                   (when (> (gnus-summary-article-score) score)
8226                     (gnus-summary-mark-article nil mark))
8227                   t)
8228                 (gnus-summary-find-next)))))
8229
8230 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8231 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8232 (defun gnus-summary-limit-include-expunged (&optional no-error)
8233   "Display all the hidden articles that were expunged for low scores."
8234   (interactive)
8235   (let ((buffer-read-only nil))
8236     (let ((scored gnus-newsgroup-scored)
8237           headers h)
8238       (while scored
8239         (unless (gnus-summary-goto-subject (caar scored))
8240           (and (setq h (gnus-summary-article-header (caar scored)))
8241                (< (cdar scored) gnus-summary-expunge-below)
8242                (push h headers)))
8243         (setq scored (cdr scored)))
8244       (if (not headers)
8245           (when (not no-error)
8246             (error "No expunged articles hidden"))
8247         (goto-char (point-min))
8248         (gnus-summary-prepare-unthreaded (nreverse headers))
8249         (goto-char (point-min))
8250         (gnus-summary-position-point)
8251         t))))
8252
8253 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8254   "Mark all unread articles in this newsgroup as read.
8255 If prefix argument ALL is non-nil, ticked and dormant articles will
8256 also be marked as read.
8257 If QUIETLY is non-nil, no questions will be asked.
8258 If TO-HERE is non-nil, it should be a point in the buffer.  All
8259 articles before this point will be marked as read.
8260 Note that this function will only catch up the unread article
8261 in the current summary buffer limitation.
8262 The number of articles marked as read is returned."
8263   (interactive "P")
8264   (prog1
8265       (save-excursion
8266         (when (or quietly
8267                   (not gnus-interactive-catchup) ;Without confirmation?
8268                   gnus-expert-user
8269                   (gnus-y-or-n-p
8270                    (if all
8271                        "Mark absolutely all articles as read? "
8272                      "Mark all unread articles as read? ")))
8273           (if (and not-mark
8274                    (not gnus-newsgroup-adaptive)
8275                    (not gnus-newsgroup-auto-expire)
8276                    (not gnus-suppress-duplicates)
8277                    (or (not gnus-use-cache)
8278                        (eq gnus-use-cache 'passive)))
8279               (progn
8280                 (when all
8281                   (setq gnus-newsgroup-marked nil
8282                         gnus-newsgroup-dormant nil))
8283                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8284             ;; We actually mark all articles as canceled, which we
8285             ;; have to do when using auto-expiry or adaptive scoring.
8286             (gnus-summary-show-all-threads)
8287             (when (gnus-summary-first-subject (not all) t)
8288               (while (and
8289                       (if to-here (< (point) to-here) t)
8290                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8291                       (gnus-summary-find-next (not all) nil nil t))))
8292             (gnus-set-mode-line 'summary))
8293           t))
8294     (gnus-summary-position-point)))
8295
8296 (defun gnus-summary-catchup-to-here (&optional all)
8297   "Mark all unticked articles before the current one as read.
8298 If ALL is non-nil, also mark ticked and dormant articles as read."
8299   (interactive "P")
8300   (save-excursion
8301     (gnus-save-hidden-threads
8302       (let ((beg (point)))
8303         ;; We check that there are unread articles.
8304         (when (or all (gnus-summary-find-prev))
8305           (gnus-summary-catchup all t beg)))))
8306   (gnus-summary-position-point))
8307
8308 (defun gnus-summary-catchup-all (&optional quietly)
8309   "Mark all articles in this newsgroup as read."
8310   (interactive "P")
8311   (gnus-summary-catchup t quietly))
8312
8313 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8314   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8315 If prefix argument ALL is non-nil, all articles are marked as read."
8316   (interactive "P")
8317   (when (gnus-summary-catchup all quietly nil 'fast)
8318     ;; Select next newsgroup or exit.
8319     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8320              (eq gnus-auto-select-next 'quietly))
8321         (gnus-summary-next-group nil)
8322       (gnus-summary-exit))))
8323
8324 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8325   "Mark all articles in this newsgroup as read, and then exit."
8326   (interactive "P")
8327   (gnus-summary-catchup-and-exit t quietly))
8328
8329 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8330 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8331   "Mark all articles in this group as read and select the next group.
8332 If given a prefix, mark all articles, unread as well as ticked, as
8333 read."
8334   (interactive "P")
8335   (save-excursion
8336     (gnus-summary-catchup all))
8337   (gnus-summary-next-article t nil nil t))
8338
8339 ;; Thread-based commands.
8340
8341 (defun gnus-summary-articles-in-thread (&optional article)
8342   "Return a list of all articles in the current thread.
8343 If ARTICLE is non-nil, return all articles in the thread that starts
8344 with that article."
8345   (let* ((article (or article (gnus-summary-article-number)))
8346          (data (gnus-data-find-list article))
8347          (top-level (gnus-data-level (car data)))
8348          (top-subject
8349           (cond ((null gnus-thread-operation-ignore-subject)
8350                  (gnus-simplify-subject-re
8351                   (mail-header-subject (gnus-data-header (car data)))))
8352                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8353                  (gnus-simplify-subject-fuzzy
8354                   (mail-header-subject (gnus-data-header (car data)))))
8355                 (t nil)))
8356          (end-point (save-excursion
8357                       (if (gnus-summary-go-to-next-thread)
8358                           (point) (point-max))))
8359          articles)
8360     (while (and data
8361                 (< (gnus-data-pos (car data)) end-point))
8362       (when (or (not top-subject)
8363                 (string= top-subject
8364                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8365                              (gnus-simplify-subject-fuzzy
8366                               (mail-header-subject
8367                                (gnus-data-header (car data))))
8368                            (gnus-simplify-subject-re
8369                             (mail-header-subject
8370                              (gnus-data-header (car data)))))))
8371         (push (gnus-data-number (car data)) articles))
8372       (unless (and (setq data (cdr data))
8373                    (> (gnus-data-level (car data)) top-level))
8374         (setq data nil)))
8375     ;; Return the list of articles.
8376     (nreverse articles)))
8377
8378 (defun gnus-summary-rethread-current ()
8379   "Rethread the thread the current article is part of."
8380   (interactive)
8381   (let* ((gnus-show-threads t)
8382          (article (gnus-summary-article-number))
8383          (id (mail-header-id (gnus-summary-article-header)))
8384          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8385     (unless id
8386       (error "No article on the current line"))
8387     (gnus-rebuild-thread id)
8388     (gnus-summary-goto-subject article)))
8389
8390 (defun gnus-summary-reparent-thread ()
8391   "Make the current article child of the marked (or previous) article.
8392
8393 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8394 is non-nil or the Subject: of both articles are the same."
8395   (interactive)
8396   (unless (not (gnus-group-read-only-p))
8397     (error "The current newsgroup does not support article editing"))
8398   (unless (<= (length gnus-newsgroup-processable) 1)
8399     (error "No more than one article may be marked"))
8400   (save-window-excursion
8401     (let ((gnus-article-buffer " *reparent*")
8402           (current-article (gnus-summary-article-number))
8403           ;; First grab the marked article, otherwise one line up.
8404           (parent-article (if (not (null gnus-newsgroup-processable))
8405                               (car gnus-newsgroup-processable)
8406                             (save-excursion
8407                               (if (eq (forward-line -1) 0)
8408                                   (gnus-summary-article-number)
8409                                 (error "Beginning of summary buffer"))))))
8410       (unless (not (eq current-article parent-article))
8411         (error "An article may not be self-referential"))
8412       (let ((message-id (mail-header-id
8413                          (gnus-summary-article-header parent-article))))
8414         (unless (and message-id (not (equal message-id "")))
8415           (error "No message-id in desired parent"))
8416         (gnus-with-article current-article
8417           (goto-char (point-min))
8418           (if (re-search-forward "^References: " nil t)
8419               (progn
8420                 (re-search-forward "^[^ \t]" nil t)
8421                 (forward-line -1)
8422                 (end-of-line)
8423                 (insert " " message-id))
8424             (insert "References: " message-id "\n")))
8425         (set-buffer gnus-summary-buffer)
8426         (gnus-summary-unmark-all-processable)
8427         (gnus-summary-update-article current-article)
8428         (gnus-summary-rethread-current)
8429         (gnus-message 3 "Article %d is now the child of article %d"
8430                       current-article parent-article)))))
8431
8432 (defun gnus-summary-toggle-threads (&optional arg)
8433   "Toggle showing conversation threads.
8434 If ARG is positive number, turn showing conversation threads on."
8435   (interactive "P")
8436   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8437     (setq gnus-show-threads
8438           (if (null arg) (not gnus-show-threads)
8439             (> (prefix-numeric-value arg) 0)))
8440     (gnus-summary-prepare)
8441     (gnus-summary-goto-subject current)
8442     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8443     (gnus-summary-position-point)))
8444
8445 (defun gnus-summary-show-all-threads ()
8446   "Show all threads."
8447   (interactive)
8448   (save-excursion
8449     (let ((buffer-read-only nil))
8450       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8451   (gnus-summary-position-point))
8452
8453 (defun gnus-summary-show-thread ()
8454   "Show thread subtrees.
8455 Returns nil if no thread was there to be shown."
8456   (interactive)
8457   (let ((buffer-read-only nil)
8458         (orig (point))
8459         ;; first goto end then to beg, to have point at beg after let
8460         (end (progn (end-of-line) (point)))
8461         (beg (progn (beginning-of-line) (point))))
8462     (prog1
8463         ;; Any hidden lines here?
8464         (search-forward "\r" end t)
8465       (subst-char-in-region beg end ?\^M ?\n t)
8466       (goto-char orig)
8467       (gnus-summary-position-point))))
8468
8469 (defun gnus-summary-hide-all-threads ()
8470   "Hide all thread subtrees."
8471   (interactive)
8472   (save-excursion
8473     (goto-char (point-min))
8474     (gnus-summary-hide-thread)
8475     (while (zerop (gnus-summary-next-thread 1 t))
8476       (gnus-summary-hide-thread)))
8477   (gnus-summary-position-point))
8478
8479 (defun gnus-summary-hide-thread ()
8480   "Hide thread subtrees.
8481 Returns nil if no threads were there to be hidden."
8482   (interactive)
8483   (let ((buffer-read-only nil)
8484         (start (point))
8485         (article (gnus-summary-article-number)))
8486     (goto-char start)
8487     ;; Go forward until either the buffer ends or the subthread
8488     ;; ends.
8489     (when (and (not (eobp))
8490                (or (zerop (gnus-summary-next-thread 1 t))
8491                    (goto-char (point-max))))
8492       (prog1
8493           (if (and (> (point) start)
8494                    (search-backward "\n" start t))
8495               (progn
8496                 (subst-char-in-region start (point) ?\n ?\^M)
8497                 (gnus-summary-goto-subject article))
8498             (goto-char start)
8499             nil)
8500         ;;(gnus-summary-position-point)
8501         ))))
8502
8503 (defun gnus-summary-go-to-next-thread (&optional previous)
8504   "Go to the same level (or less) next thread.
8505 If PREVIOUS is non-nil, go to previous thread instead.
8506 Return the article number moved to, or nil if moving was impossible."
8507   (let ((level (gnus-summary-thread-level))
8508         (way (if previous -1 1))
8509         (beg (point)))
8510     (forward-line way)
8511     (while (and (not (eobp))
8512                 (< level (gnus-summary-thread-level)))
8513       (forward-line way))
8514     (if (eobp)
8515         (progn
8516           (goto-char beg)
8517           nil)
8518       (setq beg (point))
8519       (prog1
8520           (gnus-summary-article-number)
8521         (goto-char beg)))))
8522
8523 (defun gnus-summary-next-thread (n &optional silent)
8524   "Go to the same level next N'th thread.
8525 If N is negative, search backward instead.
8526 Returns the difference between N and the number of skips actually
8527 done.
8528
8529 If SILENT, don't output messages."
8530   (interactive "p")
8531   (let ((backward (< n 0))
8532         (n (abs n)))
8533     (while (and (> n 0)
8534                 (gnus-summary-go-to-next-thread backward))
8535       (decf n))
8536     (unless silent
8537       (gnus-summary-position-point))
8538     (when (and (not silent) (/= 0 n))
8539       (gnus-message 7 "No more threads"))
8540     n))
8541
8542 (defun gnus-summary-prev-thread (n)
8543   "Go to the same level previous N'th thread.
8544 Returns the difference between N and the number of skips actually
8545 done."
8546   (interactive "p")
8547   (gnus-summary-next-thread (- n)))
8548
8549 (defun gnus-summary-go-down-thread ()
8550   "Go down one level in the current thread."
8551   (let ((children (gnus-summary-article-children)))
8552     (when children
8553       (gnus-summary-goto-subject (car children)))))
8554
8555 (defun gnus-summary-go-up-thread ()
8556   "Go up one level in the current thread."
8557   (let ((parent (gnus-summary-article-parent)))
8558     (when parent
8559       (gnus-summary-goto-subject parent))))
8560
8561 (defun gnus-summary-down-thread (n)
8562   "Go down thread N steps.
8563 If N is negative, go up instead.
8564 Returns the difference between N and how many steps down that were
8565 taken."
8566   (interactive "p")
8567   (let ((up (< n 0))
8568         (n (abs n)))
8569     (while (and (> n 0)
8570                 (if up (gnus-summary-go-up-thread)
8571                   (gnus-summary-go-down-thread)))
8572       (setq n (1- n)))
8573     (gnus-summary-position-point)
8574     (when (/= 0 n)
8575       (gnus-message 7 "Can't go further"))
8576     n))
8577
8578 (defun gnus-summary-up-thread (n)
8579   "Go up thread N steps.
8580 If N is negative, go up instead.
8581 Returns the difference between N and how many steps down that were
8582 taken."
8583   (interactive "p")
8584   (gnus-summary-down-thread (- n)))
8585
8586 (defun gnus-summary-top-thread ()
8587   "Go to the top of the thread."
8588   (interactive)
8589   (while (gnus-summary-go-up-thread))
8590   (gnus-summary-article-number))
8591
8592 (defun gnus-summary-kill-thread (&optional unmark)
8593   "Mark articles under current thread as read.
8594 If the prefix argument is positive, remove any kinds of marks.
8595 If the prefix argument is negative, tick articles instead."
8596   (interactive "P")
8597   (when unmark
8598     (setq unmark (prefix-numeric-value unmark)))
8599   (let ((articles (gnus-summary-articles-in-thread)))
8600     (save-excursion
8601       ;; Expand the thread.
8602       (gnus-summary-show-thread)
8603       ;; Mark all the articles.
8604       (while articles
8605         (gnus-summary-goto-subject (car articles))
8606         (cond ((null unmark)
8607                (gnus-summary-mark-article-as-read gnus-killed-mark))
8608               ((> unmark 0)
8609                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8610               (t
8611                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8612         (setq articles (cdr articles))))
8613     ;; Hide killed subtrees.
8614     (and (null unmark)
8615          gnus-thread-hide-killed
8616          (gnus-summary-hide-thread))
8617     ;; If marked as read, go to next unread subject.
8618     (when (null unmark)
8619       ;; Go to next unread subject.
8620       (gnus-summary-next-subject 1 t)))
8621   (gnus-set-mode-line 'summary))
8622
8623 ;; Summary sorting commands
8624
8625 (defun gnus-summary-sort-by-number (&optional reverse)
8626   "Sort the summary buffer by article number.
8627 Argument REVERSE means reverse order."
8628   (interactive "P")
8629   (gnus-summary-sort 'number reverse))
8630
8631 (defun gnus-summary-sort-by-author (&optional reverse)
8632   "Sort the summary buffer by author name alphabetically.
8633 If case-fold-search is non-nil, case of letters is ignored.
8634 Argument REVERSE means reverse order."
8635   (interactive "P")
8636   (gnus-summary-sort 'author reverse))
8637
8638 (defun gnus-summary-sort-by-subject (&optional reverse)
8639   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8640 If case-fold-search is non-nil, case of letters is ignored.
8641 Argument REVERSE means reverse order."
8642   (interactive "P")
8643   (gnus-summary-sort 'subject reverse))
8644
8645 (defun gnus-summary-sort-by-date (&optional reverse)
8646   "Sort the summary buffer by date.
8647 Argument REVERSE means reverse order."
8648   (interactive "P")
8649   (gnus-summary-sort 'date reverse))
8650
8651 (defun gnus-summary-sort-by-score (&optional reverse)
8652   "Sort the summary buffer by score.
8653 Argument REVERSE means reverse order."
8654   (interactive "P")
8655   (gnus-summary-sort 'score reverse))
8656
8657 (defun gnus-summary-sort-by-lines (&optional reverse)
8658   "Sort the summary buffer by the number of lines.
8659 Argument REVERSE means reverse order."
8660   (interactive "P")
8661   (gnus-summary-sort 'lines reverse))
8662
8663 (defun gnus-summary-sort-by-chars (&optional reverse)
8664   "Sort the summary buffer by article length.
8665 Argument REVERSE means reverse order."
8666   (interactive "P")
8667   (gnus-summary-sort 'chars reverse))   
8668
8669 (defun gnus-summary-sort (predicate reverse)
8670   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8671   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8672          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8673          (gnus-thread-sort-functions
8674           (if (not reverse)
8675               thread
8676             `(lambda (t1 t2)
8677                (,thread t2 t1))))
8678          (gnus-article-sort-functions
8679           (if (not reverse)
8680               article
8681             `(lambda (t1 t2)
8682                (,article t2 t1))))
8683          (buffer-read-only)
8684          (gnus-summary-prepare-hook nil))
8685     ;; We do the sorting by regenerating the threads.
8686     (gnus-summary-prepare)
8687     ;; Hide subthreads if needed.
8688     (when (and gnus-show-threads gnus-thread-hide-subtree)
8689       (gnus-summary-hide-all-threads))))
8690
8691 ;; Summary saving commands.
8692
8693 (defun gnus-summary-save-article (&optional n not-saved)
8694   "Save the current article using the default saver function.
8695 If N is a positive number, save the N next articles.
8696 If N is a negative number, save the N previous articles.
8697 If N is nil and any articles have been marked with the process mark,
8698 save those articles instead.
8699 The variable `gnus-default-article-saver' specifies the saver function."
8700   (interactive "P")
8701   (let* ((articles (gnus-summary-work-articles n))
8702          (save-buffer (save-excursion
8703                         (nnheader-set-temp-buffer " *Gnus Save*")))
8704          (num (length articles))
8705          header file)
8706     (dolist (article articles)
8707       (setq header (gnus-summary-article-header article))
8708       (if (not (vectorp header))
8709           ;; This is a pseudo-article.
8710           (if (assq 'name header)
8711               (gnus-copy-file (cdr (assq 'name header)))
8712             (gnus-message 1 "Article %d is unsaveable" article))
8713         ;; This is a real article.
8714         (save-window-excursion
8715           (gnus-summary-select-article t nil nil article))
8716         (save-excursion
8717           (set-buffer save-buffer)
8718           (erase-buffer)
8719           (insert-buffer-substring gnus-original-article-buffer))
8720         (setq file (gnus-article-save save-buffer file num))
8721         (gnus-summary-remove-process-mark article)
8722         (unless not-saved
8723           (gnus-summary-set-saved-mark article))))
8724     (gnus-kill-buffer save-buffer)
8725     (gnus-summary-position-point)
8726     (gnus-set-mode-line 'summary)
8727     n))
8728
8729 (defun gnus-summary-pipe-output (&optional arg)
8730   "Pipe the current article to a subprocess.
8731 If N is a positive number, pipe the N next articles.
8732 If N is a negative number, pipe the N previous articles.
8733 If N is nil and any articles have been marked with the process mark,
8734 pipe those articles instead."
8735   (interactive "P")
8736   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8737     (gnus-summary-save-article arg t))
8738   (gnus-configure-windows 'pipe))
8739
8740 (defun gnus-summary-save-article-mail (&optional arg)
8741   "Append the current article to an mail file.
8742 If N is a positive number, save the N next articles.
8743 If N is a negative number, save the N previous articles.
8744 If N is nil and any articles have been marked with the process mark,
8745 save those articles instead."
8746   (interactive "P")
8747   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8748     (gnus-summary-save-article arg)))
8749
8750 (defun gnus-summary-save-article-rmail (&optional arg)
8751   "Append the current article to an rmail file.
8752 If N is a positive number, save the N next articles.
8753 If N is a negative number, save the N previous articles.
8754 If N is nil and any articles have been marked with the process mark,
8755 save those articles instead."
8756   (interactive "P")
8757   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8758     (gnus-summary-save-article arg)))
8759
8760 (defun gnus-summary-save-article-file (&optional arg)
8761   "Append the current article to a file.
8762 If N is a positive number, save the N next articles.
8763 If N is a negative number, save the N previous articles.
8764 If N is nil and any articles have been marked with the process mark,
8765 save those articles instead."
8766   (interactive "P")
8767   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8768     (gnus-summary-save-article arg)))
8769
8770 (defun gnus-summary-write-article-file (&optional arg)
8771   "Write the current article to a file, deleting the previous file.
8772 If N is a positive number, save the N next articles.
8773 If N is a negative number, save the N previous articles.
8774 If N is nil and any articles have been marked with the process mark,
8775 save those articles instead."
8776   (interactive "P")
8777   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8778     (gnus-summary-save-article arg)))
8779
8780 (defun gnus-summary-save-article-body-file (&optional arg)
8781   "Append the current article body to a file.
8782 If N is a positive number, save the N next articles.
8783 If N is a negative number, save the N previous articles.
8784 If N is nil and any articles have been marked with the process mark,
8785 save those articles instead."
8786   (interactive "P")
8787   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8788     (gnus-summary-save-article arg)))
8789
8790 (defun gnus-summary-pipe-message (program)
8791   "Pipe the current article through PROGRAM."
8792   (interactive "sProgram: ")
8793   (gnus-summary-select-article)
8794   (let ((mail-header-separator ""))
8795     (gnus-eval-in-buffer-window gnus-article-buffer
8796       (save-restriction
8797         (widen)
8798         (let ((start (window-start))
8799               buffer-read-only)
8800           (message-pipe-buffer-body program)
8801           (set-window-start (get-buffer-window (current-buffer)) start))))))
8802
8803 (defun gnus-get-split-value (methods)
8804   "Return a value based on the split METHODS."
8805   (let (split-name method result match)
8806     (when methods
8807       (save-excursion
8808         (set-buffer gnus-original-article-buffer)
8809         (save-restriction
8810           (nnheader-narrow-to-headers)
8811           (while methods
8812             (goto-char (point-min))
8813             (setq method (pop methods))
8814             (setq match (car method))
8815             (when (cond
8816                    ((stringp match)
8817                     ;; Regular expression.
8818                     (ignore-errors
8819                       (re-search-forward match nil t)))
8820                    ((gnus-functionp match)
8821                     ;; Function.
8822                     (save-restriction
8823                       (widen)
8824                       (setq result (funcall match gnus-newsgroup-name))))
8825                    ((consp match)
8826                     ;; Form.
8827                     (save-restriction
8828                       (widen)
8829                       (setq result (eval match)))))
8830               (setq split-name (append (cdr method) split-name))
8831               (cond ((stringp result)
8832                      (push (expand-file-name
8833                             result gnus-article-save-directory)
8834                            split-name))
8835                     ((consp result)
8836                      (setq split-name (append result split-name)))))))))
8837     (nreverse split-name)))
8838
8839 (defun gnus-valid-move-group-p (group)
8840   (and (boundp group)
8841        (symbol-name group)
8842        (symbol-value group)
8843        (gnus-get-function (gnus-find-method-for-group
8844                            (symbol-name group)) 'request-accept-article t)))
8845
8846 (defun gnus-read-move-group-name (prompt default articles prefix)
8847   "Read a group name."
8848   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8849          (minibuffer-confirm-incomplete nil) ; XEmacs
8850          (prom
8851           (format "%s %s to:"
8852                   prompt
8853                   (if (> (length articles) 1)
8854                       (format "these %d articles" (length articles))
8855                     "this article")))
8856          (to-newsgroup
8857           (cond
8858            ((null split-name)
8859             (gnus-completing-read default prom
8860                                   gnus-active-hashtb
8861                                   'gnus-valid-move-group-p
8862                                   nil prefix
8863                                   'gnus-group-history))
8864            ((= 1 (length split-name))
8865             (gnus-completing-read (car split-name) prom
8866                                   gnus-active-hashtb
8867                                   'gnus-valid-move-group-p
8868                                   nil nil
8869                                   'gnus-group-history))
8870            (t
8871             (gnus-completing-read nil prom
8872                                   (mapcar (lambda (el) (list el))
8873                                           (nreverse split-name))
8874                                   nil nil nil
8875                                   'gnus-group-history)))))
8876     (when to-newsgroup
8877       (if (or (string= to-newsgroup "")
8878               (string= to-newsgroup prefix))
8879           (setq to-newsgroup default))
8880       (unless to-newsgroup
8881         (error "No group name entered"))
8882       (or (gnus-active to-newsgroup)
8883           (gnus-activate-group to-newsgroup)
8884           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8885                                      to-newsgroup))
8886               (or (and (gnus-request-create-group
8887                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8888                        (gnus-activate-group to-newsgroup nil nil
8889                                             (gnus-group-name-to-method
8890                                              to-newsgroup)))
8891                   (error "Couldn't create group %s" to-newsgroup)))
8892           (error "No such group: %s" to-newsgroup)))
8893     to-newsgroup))
8894
8895 (defun gnus-summary-save-parts (type dir n reverse)
8896   "Save parts matching TYPE to DIR.
8897 If REVERSE, save parts that do not match TYPE."
8898   (interactive
8899    (list (read-string "Save parts of type: " "image/.*")
8900          (read-file-name "Save to directory: " t nil t)
8901          current-prefix-arg))
8902   (gnus-summary-iterate n
8903     (let ((gnus-display-mime-function nil)
8904           (gnus-inhibit-treatment t))
8905       (gnus-summary-select-article))
8906     (save-excursion
8907       (set-buffer gnus-article-buffer)
8908       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8909         (when handles
8910           (gnus-summary-save-parts-1 type dir handles reverse)
8911           (mm-destroy-parts handles))))))
8912
8913 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8914   (if (stringp (car handle))
8915       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8916               (cdr handle))
8917     (when (if reverse
8918               (not (string-match type (car (mm-handle-type handle))))
8919             (string-match type (car (mm-handle-type handle))))
8920       (let ((file (expand-file-name
8921                    (file-name-nondirectory
8922                     (or
8923                      (mail-content-type-get
8924                       (mm-handle-disposition handle) 'filename)
8925                      (concat gnus-newsgroup-name "." gnus-current-article)))
8926                    dir)))
8927         (unless (file-exists-p file)
8928           (mm-save-part-to-file handle file))))))
8929
8930 ;; Summary extract commands
8931
8932 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8933   (let ((buffer-read-only nil)
8934         (article (gnus-summary-article-number))
8935         after-article b e)
8936     (unless (gnus-summary-goto-subject article)
8937       (error "No such article: %d" article))
8938     (gnus-summary-position-point)
8939     ;; If all commands are to be bunched up on one line, we collect
8940     ;; them here.
8941     (unless gnus-view-pseudos-separately
8942       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8943             files action)
8944         (while ps
8945           (setq action (cdr (assq 'action (car ps))))
8946           (setq files (list (cdr (assq 'name (car ps)))))
8947           (while (and ps (cdr ps)
8948                       (string= (or action "1")
8949                                (or (cdr (assq 'action (cadr ps))) "2")))
8950             (push (cdr (assq 'name (cadr ps))) files)
8951             (setcdr ps (cddr ps)))
8952           (when files
8953             (when (not (string-match "%s" action))
8954               (push " " files))
8955             (push " " files)
8956             (when (assq 'execute (car ps))
8957               (setcdr (assq 'execute (car ps))
8958                       (funcall (if (string-match "%s" action)
8959                                    'format 'concat)
8960                                action
8961                                (mapconcat
8962                                 (lambda (f)
8963                                   (if (equal f " ")
8964                                       f
8965                                     (gnus-quote-arg-for-sh-or-csh f)))
8966                                 files " ")))))
8967           (setq ps (cdr ps)))))
8968     (if (and gnus-view-pseudos (not not-view))
8969         (while pslist
8970           (when (assq 'execute (car pslist))
8971             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8972                                   (eq gnus-view-pseudos 'not-confirm)))
8973           (setq pslist (cdr pslist)))
8974       (save-excursion
8975         (while pslist
8976           (setq after-article (or (cdr (assq 'article (car pslist)))
8977                                   (gnus-summary-article-number)))
8978           (gnus-summary-goto-subject after-article)
8979           (forward-line 1)
8980           (setq b (point))
8981           (insert "    " (file-name-nondirectory
8982                           (cdr (assq 'name (car pslist))))
8983                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8984           (setq e (point))
8985           (forward-line -1)             ; back to `b'
8986           (gnus-add-text-properties
8987            b (1- e) (list 'gnus-number gnus-reffed-article-number
8988                           gnus-mouse-face-prop gnus-mouse-face))
8989           (gnus-data-enter
8990            after-article gnus-reffed-article-number
8991            gnus-unread-mark b (car pslist) 0 (- e b))
8992           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8993           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8994           (setq pslist (cdr pslist)))))))
8995
8996 (defun gnus-pseudos< (p1 p2)
8997   (let ((c1 (cdr (assq 'action p1)))
8998         (c2 (cdr (assq 'action p2))))
8999     (and c1 c2 (string< c1 c2))))
9000
9001 (defun gnus-request-pseudo-article (props)
9002   (cond ((assq 'execute props)
9003          (gnus-execute-command (cdr (assq 'execute props)))))
9004   (let ((gnus-current-article (gnus-summary-article-number)))
9005     (gnus-run-hooks 'gnus-mark-article-hook)))
9006
9007 (defun gnus-execute-command (command &optional automatic)
9008   (save-excursion
9009     (gnus-article-setup-buffer)
9010     (set-buffer gnus-article-buffer)
9011     (setq buffer-read-only nil)
9012     (let ((command (if automatic command
9013                      (read-string "Command: " (cons command 0)))))
9014       (erase-buffer)
9015       (insert "$ " command "\n\n")
9016       (if gnus-view-pseudo-asynchronously
9017           (start-process "gnus-execute" (current-buffer) shell-file-name
9018                          shell-command-switch command)
9019         (call-process shell-file-name nil t nil
9020                       shell-command-switch command)))))
9021
9022 ;; Summary kill commands.
9023
9024 (defun gnus-summary-edit-global-kill (article)
9025   "Edit the \"global\" kill file."
9026   (interactive (list (gnus-summary-article-number)))
9027   (gnus-group-edit-global-kill article))
9028
9029 (defun gnus-summary-edit-local-kill ()
9030   "Edit a local kill file applied to the current newsgroup."
9031   (interactive)
9032   (setq gnus-current-headers (gnus-summary-article-header))
9033   (gnus-group-edit-local-kill
9034    (gnus-summary-article-number) gnus-newsgroup-name))
9035
9036 ;;; Header reading.
9037
9038 (defun gnus-read-header (id &optional header)
9039   "Read the headers of article ID and enter them into the Gnus system."
9040   (let ((group gnus-newsgroup-name)
9041         (gnus-override-method
9042          (and (gnus-news-group-p gnus-newsgroup-name)
9043               gnus-refer-article-method))
9044         where)
9045     ;; First we check to see whether the header in question is already
9046     ;; fetched.
9047     (if (stringp id)
9048         ;; This is a Message-ID.
9049         (setq header (or header (gnus-id-to-header id)))
9050       ;; This is an article number.
9051       (setq header (or header (gnus-summary-article-header id))))
9052     (if (and header
9053              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9054         ;; We have found the header.
9055         header
9056       ;; If this is a sparse article, we have to nix out its
9057       ;; previous entry in the thread hashtb.
9058       (when (and header
9059                  (gnus-summary-article-sparse-p (mail-header-number header)))
9060         (let* ((parent (gnus-parent-id (mail-header-references header)))
9061                (thread (and parent (gnus-id-to-thread parent))))
9062           (when thread
9063             (delq (assq header thread) thread))))
9064       ;; We have to really fetch the header to this article.
9065       (save-excursion
9066         (set-buffer nntp-server-buffer)
9067         (when (setq where (gnus-request-head id group))
9068           (nnheader-fold-continuation-lines)
9069           (goto-char (point-max))
9070           (insert ".\n")
9071           (goto-char (point-min))
9072           (insert "211 ")
9073           (princ (cond
9074                   ((numberp id) id)
9075                   ((cdr where) (cdr where))
9076                   (header (mail-header-number header))
9077                   (t gnus-reffed-article-number))
9078                  (current-buffer))
9079           (insert " Article retrieved.\n"))
9080         (if (or (not where)
9081                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9082             ()                          ; Malformed head.
9083           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9084             (when (and (stringp id)
9085                        (not (string= (gnus-group-real-name group)
9086                                      (car where))))
9087               ;; If we fetched by Message-ID and the article came
9088               ;; from a different group, we fudge some bogus article
9089               ;; numbers for this article.
9090               (mail-header-set-number header gnus-reffed-article-number))
9091             (save-excursion
9092               (set-buffer gnus-summary-buffer)
9093               (decf gnus-reffed-article-number)
9094               (gnus-remove-header (mail-header-number header))
9095               (push header gnus-newsgroup-headers)
9096               (setq gnus-current-headers header)
9097               (push (mail-header-number header) gnus-newsgroup-limit)))
9098           header)))))
9099
9100 (defun gnus-remove-header (number)
9101   "Remove header NUMBER from `gnus-newsgroup-headers'."
9102   (if (and gnus-newsgroup-headers
9103            (= number (mail-header-number (car gnus-newsgroup-headers))))
9104       (pop gnus-newsgroup-headers)
9105     (let ((headers gnus-newsgroup-headers))
9106       (while (and (cdr headers)
9107                   (not (= number (mail-header-number (cadr headers)))))
9108         (pop headers))
9109       (when (cdr headers)
9110         (setcdr headers (cddr headers))))))
9111
9112 ;;;
9113 ;;; summary highlights
9114 ;;;
9115
9116 (defun gnus-highlight-selected-summary ()
9117   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9118   ;; Highlight selected article in summary buffer
9119   (when gnus-summary-selected-face
9120     (save-excursion
9121       (let* ((beg (progn (beginning-of-line) (point)))
9122              (end (progn (end-of-line) (point)))
9123              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9124              (from (if (get-text-property beg gnus-mouse-face-prop)
9125                        beg
9126                      (or (next-single-property-change
9127                           beg gnus-mouse-face-prop nil end)
9128                          beg)))
9129              (to
9130               (if (= from end)
9131                   (- from 2)
9132                 (or (next-single-property-change
9133                      from gnus-mouse-face-prop nil end)
9134                     end))))
9135         ;; If no mouse-face prop on line we will have to = from = end,
9136         ;; so we highlight the entire line instead.
9137         (when (= (+ to 2) from)
9138           (setq from beg)
9139           (setq to end))
9140         (if gnus-newsgroup-selected-overlay
9141             ;; Move old overlay.
9142             (gnus-move-overlay
9143              gnus-newsgroup-selected-overlay from to (current-buffer))
9144           ;; Create new overlay.
9145           (gnus-overlay-put
9146            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9147            'face gnus-summary-selected-face))))))
9148
9149 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9150 (defun gnus-summary-highlight-line ()
9151   "Highlight current line according to `gnus-summary-highlight'."
9152   (let* ((list gnus-summary-highlight)
9153          (p (point))
9154          (end (progn (end-of-line) (point)))
9155          ;; now find out where the line starts and leave point there.
9156          (beg (progn (beginning-of-line) (point)))
9157          (article (gnus-summary-article-number))
9158          (score (or (cdr (assq (or article gnus-current-article)
9159                                gnus-newsgroup-scored))
9160                     gnus-summary-default-score 0))
9161          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9162          (inhibit-read-only t))
9163     ;; Eval the cars of the lists until we find a match.
9164     (let ((default gnus-summary-default-score))
9165       (while (and list
9166                   (not (eval (caar list))))
9167         (setq list (cdr list))))
9168     (let ((face (cdar list)))
9169       (unless (eq face (get-text-property beg 'face))
9170         (gnus-put-text-property-excluding-characters-with-faces
9171          beg end 'face
9172          (setq face (if (boundp face) (symbol-value face) face)))
9173         (when gnus-summary-highlight-line-function
9174           (funcall gnus-summary-highlight-line-function article face))))
9175     (goto-char p)))
9176
9177 (defun gnus-update-read-articles (group unread &optional compute)
9178   "Update the list of read articles in GROUP."
9179   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9180          (entry (gnus-gethash group gnus-newsrc-hashtb))
9181          (info (nth 2 entry))
9182          (prev 1)
9183          (unread (sort (copy-sequence unread) '<))
9184          read)
9185     (if (or (not info) (not active))
9186         ;; There is no info on this group if it was, in fact,
9187         ;; killed.  Gnus stores no information on killed groups, so
9188         ;; there's nothing to be done.
9189         ;; One could store the information somewhere temporarily,
9190         ;; perhaps...  Hmmm...
9191         ()
9192       ;; Remove any negative articles numbers.
9193       (while (and unread (< (car unread) 0))
9194         (setq unread (cdr unread)))
9195       ;; Remove any expired article numbers
9196       (while (and unread (< (car unread) (car active)))
9197         (setq unread (cdr unread)))
9198       ;; Compute the ranges of read articles by looking at the list of
9199       ;; unread articles.
9200       (while unread
9201         (when (/= (car unread) prev)
9202           (push (if (= prev (1- (car unread))) prev
9203                   (cons prev (1- (car unread))))
9204                 read))
9205         (setq prev (1+ (car unread)))
9206         (setq unread (cdr unread)))
9207       (when (<= prev (cdr active))
9208         (push (cons prev (cdr active)) read))
9209       (setq read (if (> (length read) 1) (nreverse read) read))
9210       (if compute
9211           read
9212         (save-excursion
9213           (set-buffer gnus-group-buffer)
9214           (gnus-undo-register
9215             `(progn
9216                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9217                (gnus-info-set-read ',info ',(gnus-info-read info))
9218                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9219                (gnus-group-update-group ,group t))))
9220        ;; Propagate the read marks to the backend.
9221        (if (gnus-check-backend-function 'request-set-mark group)
9222            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9223                  (add (gnus-remove-from-range read (gnus-info-read info))))
9224              (when (or add del)
9225                (unless (gnus-check-group group)
9226                  (error "Can't open server for %s" group))
9227                (gnus-request-set-mark
9228                 group (delq nil (list (if add (list add 'add '(read)))
9229                                       (if del (list del 'del '(read)))))))))
9230         ;; Enter this list into the group info.
9231         (gnus-info-set-read info read)
9232         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9233         (gnus-get-unread-articles-in-group info (gnus-active group))
9234         t))))
9235
9236 (defun gnus-offer-save-summaries ()
9237   "Offer to save all active summary buffers."
9238   (save-excursion
9239     (let ((buflist (buffer-list))
9240           buffers bufname)
9241       ;; Go through all buffers and find all summaries.
9242       (while buflist
9243         (and (setq bufname (buffer-name (car buflist)))
9244              (string-match "Summary" bufname)
9245              (save-excursion
9246                (set-buffer bufname)
9247                ;; We check that this is, indeed, a summary buffer.
9248                (and (eq major-mode 'gnus-summary-mode)
9249                     ;; Also make sure this isn't bogus.
9250                     gnus-newsgroup-prepared
9251                     ;; Also make sure that this isn't a dead summary buffer.
9252                     (not gnus-dead-summary-mode)))
9253              (push bufname buffers))
9254         (setq buflist (cdr buflist)))
9255       ;; Go through all these summary buffers and offer to save them.
9256       (when buffers
9257         (map-y-or-n-p
9258          "Update summary buffer %s? "
9259          (lambda (buf)
9260            (switch-to-buffer buf)
9261            (gnus-summary-exit))
9262          buffers)))))
9263
9264
9265 ;;; @ for mime-partial
9266 ;;;
9267
9268 (defun gnus-request-partial-message ()
9269   (save-excursion
9270     (let ((number (gnus-summary-article-number))
9271           (group gnus-newsgroup-name)
9272           (mother gnus-article-buffer))
9273       (set-buffer (get-buffer-create " *Partial Article*"))
9274       (erase-buffer)
9275       (setq mime-preview-buffer mother)
9276       (gnus-request-article-this-buffer number group)
9277       (mime-parse-buffer)
9278       )))
9279
9280 (autoload 'mime-combine-message/partial-pieces-automatically
9281   "mime-partial"
9282   "Internal method to combine message/partial messages automatically.")
9283
9284 (mime-add-condition
9285  'action '((type . message)(subtype . partial)
9286            (major-mode . gnus-original-article-mode)
9287            (method . mime-combine-message/partial-pieces-automatically)
9288            (summary-buffer-exp . gnus-summary-buffer)
9289            (request-partial-message-method . gnus-request-partial-message)
9290            ))
9291
9292
9293 ;;; @ for message/rfc822
9294 ;;;
9295
9296 (defun gnus-mime-extract-message/rfc822 (entity situation)
9297   (let (group article num cwin swin cur)
9298     (with-current-buffer (mime-entity-buffer entity)
9299       (save-restriction
9300         (narrow-to-region (mime-entity-body-start entity)
9301                           (mime-entity-body-end entity))
9302         (setq group (or (cdr (assq 'group situation))
9303                         (completing-read "Group: "
9304                                          gnus-active-hashtb
9305                                          nil
9306                                          (gnus-read-active-file-p)
9307                                          gnus-newsgroup-name))
9308               article (gnus-request-accept-article group)
9309               )
9310         ))
9311     (when (and (consp article)
9312                (numberp (setq article (cdr article))))
9313       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9314             cwin (get-buffer-window (current-buffer) t)
9315             )
9316       (save-window-excursion
9317         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9318             (select-window swin)
9319           (set-buffer gnus-summary-buffer)
9320           )
9321         (setq cur gnus-current-article)
9322         (forward-line num)
9323         (let (gnus-show-threads)
9324           (gnus-summary-goto-subject article t)
9325           )
9326         (gnus-summary-clear-mark-forward 1)
9327         (gnus-summary-goto-subject cur)
9328         )
9329       (when (and cwin (window-frame cwin))
9330         (select-frame (window-frame cwin))
9331         )
9332       (when (boundp 'mime-acting-situation-to-override)
9333         (set-alist 'mime-acting-situation-to-override
9334                    'group
9335                    group)
9336         (set-alist 'mime-acting-situation-to-override
9337                    'after-method
9338                    `(progn
9339                       (save-current-buffer
9340                         (set-buffer gnus-group-buffer)
9341                         (gnus-activate-group ,group)
9342                         )
9343                       (gnus-summary-goto-article ,cur
9344                                                  gnus-show-all-headers)
9345                       ))
9346         (set-alist 'mime-acting-situation-to-override
9347                    'number num)
9348         )
9349       )))
9350
9351 (mime-add-condition
9352  'action '((type . message)(subtype . rfc822)
9353            (major-mode . gnus-original-article-mode)
9354            (method . gnus-mime-extract-message/rfc822)
9355            (mode . "extract")
9356            ))
9357
9358 (mime-add-condition
9359  'action '((type . message)(subtype . news)
9360            (major-mode . gnus-original-article-mode)
9361            (method . gnus-mime-extract-message/rfc822)
9362            (mode . "extract")
9363            ))
9364
9365 (defun gnus-mime-extract-multipart (entity situation)
9366   (let ((children (mime-entity-children entity))
9367         mime-acting-situation-to-override
9368         f)
9369     (while children
9370       (mime-play-entity (car children)
9371                         (cons (assq 'mode situation)
9372                               mime-acting-situation-to-override))
9373       (setq children (cdr children)))
9374     (if (setq f (cdr (assq 'after-method
9375                            mime-acting-situation-to-override)))
9376         (eval f)
9377       )))  
9378
9379 (mime-add-condition
9380  'action '((type . multipart)
9381            (method . gnus-mime-extract-multipart)
9382            (mode . "extract")
9383            )
9384  'with-default)
9385
9386
9387 ;;; @ end
9388 ;;;
9389
9390 (defun gnus-summary-setup-default-charset ()
9391   "Setup newsgroup default charset."
9392   (let* ((name (and gnus-newsgroup-name
9393                    (gnus-group-real-name gnus-newsgroup-name)))
9394          (ignored-charsets 
9395           (append
9396            (and gnus-newsgroup-name
9397                 (or (gnus-group-find-parameter gnus-newsgroup-name
9398                                                'ignored-charsets t)
9399                     (let ((alist gnus-group-ignored-charsets-alist)
9400                           elem (charsets nil))
9401                       (while (setq elem (pop alist))
9402                         (when (and name
9403                                    (string-match (car elem) name))
9404                          (setq alist nil
9405                                charsets (cdr elem))))
9406                       charsets)))
9407            gnus-newsgroup-ignored-charsets)))
9408     (setq gnus-newsgroup-charset
9409           (or (and gnus-newsgroup-name
9410                    (or (gnus-group-find-parameter gnus-newsgroup-name
9411                                                   'charset)
9412                        (let ((alist gnus-group-charset-alist)
9413                              elem (charset nil))
9414                          (while (setq elem (pop alist))
9415                            (when (and name
9416                                       (string-match (car elem) name))
9417                              (setq alist nil
9418                                    charset (cadr elem))))
9419                          charset)))
9420               gnus-default-charset))
9421     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9422          ignored-charsets)))
9423
9424 ;;;
9425 ;;; Mime Commands
9426 ;;;
9427
9428 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9429   "Display the current article buffer fully MIME-buttonized.
9430 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9431 treated as multipart/mixed."
9432   (interactive "P")
9433   (require 'gnus-art)
9434   (let ((gnus-unbuttonized-mime-types nil)
9435         (gnus-mime-display-multipart-as-mixed show-all-parts))
9436     (gnus-summary-show-article)))
9437
9438 (defun gnus-summary-repair-multipart (article)
9439   "Add a Content-Type header to a multipart article without one."
9440   (interactive (list (gnus-summary-article-number)))
9441   (gnus-with-article article
9442     (message-narrow-to-head)
9443     (goto-char (point-max))
9444     (widen)
9445     (when (search-forward "\n--" nil t)
9446       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9447         (message-narrow-to-head)
9448         (message-remove-header "Mime-Version")
9449         (message-remove-header "Content-Type")
9450         (goto-char (point-max))
9451         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9452                         separator))
9453         (insert "Mime-Version: 1.0\n")
9454         (widen))))
9455   (let (gnus-mark-article-hook)
9456     (gnus-summary-select-article t t nil article)))
9457
9458 (defun gnus-summary-toggle-display-buttonized ()
9459   "Toggle the buttonizing of the article buffer."
9460   (interactive)
9461   (require 'gnus-art)
9462   (if (setq gnus-inhibit-mime-unbuttonizing
9463             (not gnus-inhibit-mime-unbuttonizing))
9464       (let ((gnus-unbuttonized-mime-types nil))
9465         (gnus-summary-show-article))
9466     (gnus-summary-show-article)))
9467
9468 ;;;
9469 ;;; with article
9470 ;;;
9471
9472 (defmacro gnus-with-article (article &rest forms)
9473   "Select ARTICLE and perform FORMS in the original article buffer.
9474 Then replace the article with the result."
9475   `(progn
9476      ;; We don't want the article to be marked as read.
9477      (let (gnus-mark-article-hook)
9478        (gnus-summary-select-article t t nil ,article))
9479      (set-buffer gnus-original-article-buffer)
9480      ,@forms
9481      (if (not (gnus-check-backend-function
9482                'request-replace-article (car gnus-article-current)))
9483          (gnus-message 5 "Read-only group; not replacing")
9484        (unless (gnus-request-replace-article
9485                 ,article (car gnus-article-current)
9486                 (current-buffer) t)
9487          (error "Couldn't replace article")))
9488      ;; The cache and backlog have to be flushed somewhat.
9489      (when gnus-keep-backlog
9490        (gnus-backlog-remove-article
9491         (car gnus-article-current) (cdr gnus-article-current)))
9492      (when gnus-use-cache
9493        (gnus-cache-update-article
9494         (car gnus-article-current) (cdr gnus-article-current)))))
9495
9496 (put 'gnus-with-article 'lisp-indent-function 1)
9497 (put 'gnus-with-article 'edebug-form-spec '(form body))
9498
9499 ;;;
9500 ;;; Generic summary marking commands
9501 ;;;
9502
9503 (defvar gnus-summary-marking-alist
9504   '((read gnus-del-mark "d")
9505     (unread gnus-unread-mark "u")
9506     (ticked gnus-ticked-mark "!")
9507     (dormant gnus-dormant-mark "?")
9508     (expirable gnus-expirable-mark "e"))
9509   "An alist of names/marks/keystrokes.")
9510
9511 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9512 (defvar gnus-summary-mark-map)
9513
9514 (defun gnus-summary-make-all-marking-commands ()
9515   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9516   (dolist (elem gnus-summary-marking-alist)
9517     (apply 'gnus-summary-make-marking-command elem)))
9518
9519 (defun gnus-summary-make-marking-command (name mark keystroke)
9520   (let ((map (make-sparse-keymap)))
9521     (define-key gnus-summary-generic-mark-map keystroke map)
9522     (dolist (lway `((next "next" next nil "n")
9523                     (next-unread "next unread" next t "N")
9524                     (prev "previous" prev nil "p")
9525                     (prev-unread "previous unread" prev t "P")
9526                     (nomove "" nil nil ,keystroke)))
9527       (let ((func (gnus-summary-make-marking-command-1
9528                    mark (car lway) lway name)))
9529         (setq func (eval func))
9530         (define-key map (nth 4 lway) func)))))
9531       
9532 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9533   `(defun ,(intern
9534             (format "gnus-summary-put-mark-as-%s%s"
9535                     name (if (eq way 'nomove)
9536                              ""
9537                            (concat "-" (symbol-name way)))))
9538      (n)
9539      ,(format
9540        "Mark the current article as %s%s.
9541 If N, the prefix, then repeat N times.
9542 If N is negative, move in reverse order.
9543 The difference between N and the actual number of articles marked is
9544 returned."
9545        name (cadr lway))
9546      (interactive "p")
9547      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9548     
9549 (defun gnus-summary-generic-mark (n mark move unread)
9550   "Mark N articles with MARK."
9551   (unless (eq major-mode 'gnus-summary-mode)
9552     (error "This command can only be used in the summary buffer"))
9553   (gnus-summary-show-thread)
9554   (let ((nummove
9555          (cond
9556           ((eq move 'next) 1)
9557           ((eq move 'prev) -1)
9558           (t 0))))
9559     (if (zerop nummove)
9560         (setq n 1)
9561       (when (< n 0)
9562         (setq n (abs n)
9563               nummove (* -1 nummove))))
9564     (while (and (> n 0)
9565                 (gnus-summary-mark-article nil mark)
9566                 (zerop (gnus-summary-next-subject nummove unread t)))
9567       (setq n (1- n)))
9568     (when (/= 0 n)
9569       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9570     (gnus-summary-recenter)
9571     (gnus-summary-position-point)
9572     (gnus-set-mode-line 'summary)
9573     n))
9574
9575 (gnus-summary-make-all-marking-commands)
9576
9577 (gnus-ems-redefine)
9578
9579 (provide 'gnus-sum)
9580
9581 (run-hooks 'gnus-sum-load-hook)
9582
9583 ;;; gnus-sum.el ends here