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