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