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