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