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