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