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