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