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