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