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