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