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