*** 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-to-marks)
5631 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
5632
5633 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5634   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5635 If REVERSE, limit the summary buffer to articles that are not marked
5636 with MARKS.  MARKS can either be a string of marks or a list of marks.
5637 Returns how many articles were removed."
5638   (interactive "sMarks: ")
5639   (gnus-set-global-variables)
5640   (prog1
5641       (let ((data gnus-newsgroup-data)
5642             (marks (if (listp marks) marks
5643                      (append marks nil))) ; Transform to list.
5644             articles)
5645         (while data
5646           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5647                   (memq (gnus-data-mark (car data)) marks))
5648             (push (gnus-data-number (car data)) articles))
5649           (setq data (cdr data)))
5650         (gnus-summary-limit articles))
5651     (gnus-summary-position-point)))
5652
5653 (defun gnus-summary-limit-to-score (&optional score)
5654   "Limit to articles with score at or above SCORE."
5655   (interactive "P")
5656   (gnus-set-global-variables)
5657   (setq score (if score
5658                   (prefix-numeric-value score)
5659                 (or gnus-summary-default-score 0)))
5660   (let ((data gnus-newsgroup-data)
5661         articles)
5662     (while data
5663       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5664                 score)
5665         (push (gnus-data-number (car data)) articles))
5666       (setq data (cdr data)))
5667     (prog1
5668         (gnus-summary-limit articles)
5669       (gnus-summary-position-point))))
5670
5671 (defun gnus-summary-limit-include-dormant ()
5672   "Display all the hidden articles that are marked as dormant."
5673   (interactive)
5674   (gnus-set-global-variables)
5675   (unless gnus-newsgroup-dormant
5676     (error "There are no dormant articles in this group"))
5677   (prog1
5678       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
5679     (gnus-summary-position-point)))
5680
5681 (defun gnus-summary-limit-exclude-dormant ()
5682   "Hide all dormant articles."
5683   (interactive)
5684   (gnus-set-global-variables)
5685   (prog1
5686       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
5687     (gnus-summary-position-point)))
5688
5689 (defun gnus-summary-limit-exclude-childless-dormant ()
5690   "Hide all dormant articles that have no children."
5691   (interactive)
5692   (gnus-set-global-variables)
5693   (let ((data (gnus-data-list t))
5694         articles d children)
5695     ;; Find all articles that are either not dormant or have
5696     ;; children.
5697     (while (setq d (pop data))
5698       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
5699                 (and (setq children 
5700                            (gnus-article-children (gnus-data-number d)))
5701                      (let (found)
5702                        (while children
5703                          (when (memq (car children) articles)
5704                            (setq children nil
5705                                  found t))
5706                          (pop children))
5707                        found)))
5708         (push (gnus-data-number d) articles)))
5709     ;; Do the limiting.
5710     (prog1
5711         (gnus-summary-limit articles)
5712       (gnus-summary-position-point))))
5713
5714 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
5715   "Mark all unread excluded articles as read.
5716 If ALL, mark even excluded ticked and dormants as read."
5717   (interactive "P")
5718   (let ((articles (gnus-sorted-complement
5719                    (sort
5720                     (mapcar (lambda (h) (mail-header-number h))
5721                             gnus-newsgroup-headers)
5722                     '<)
5723                    (sort gnus-newsgroup-limit '<)))
5724         article)
5725     (setq gnus-newsgroup-unreads nil)
5726     (if all
5727         (setq gnus-newsgroup-dormant nil
5728               gnus-newsgroup-marked nil
5729               gnus-newsgroup-reads
5730               (nconc
5731                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
5732                gnus-newsgroup-reads))
5733       (while (setq article (pop articles))
5734         (unless (or (memq article gnus-newsgroup-dormant)
5735                     (memq article gnus-newsgroup-marked))
5736           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
5737
5738 (defun gnus-summary-limit (articles &optional pop)
5739   (if pop
5740       ;; We pop the previous limit off the stack and use that.
5741       (setq articles (car gnus-newsgroup-limits)
5742             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
5743     ;; We use the new limit, so we push the old limit on the stack.
5744     (push gnus-newsgroup-limit gnus-newsgroup-limits))
5745   ;; Set the limit.
5746   (setq gnus-newsgroup-limit articles)
5747   (let ((total (length gnus-newsgroup-data))
5748         (data (gnus-data-find-list (gnus-summary-article-number)))
5749         (gnus-summary-mark-below nil)   ; Inhibit this.
5750         found)
5751     ;; This will do all the work of generating the new summary buffer
5752     ;; according to the new limit.
5753     (gnus-summary-prepare)
5754     ;; Hide any threads, possibly.
5755     (and gnus-show-threads
5756          gnus-thread-hide-subtree
5757          (gnus-summary-hide-all-threads))
5758     ;; Try to return to the article you were at, or one in the
5759     ;; neighborhood.
5760     (when data
5761       ;; We try to find some article after the current one.
5762       (while data
5763         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
5764           (setq data nil
5765                 found t))
5766         (setq data (cdr data))))
5767     (unless found
5768       ;; If there is no data, that means that we were after the last
5769       ;; article.  The same goes when we can't find any articles
5770       ;; after the current one.
5771       (goto-char (point-max))
5772       (gnus-summary-find-prev))
5773     ;; We return how many articles were removed from the summary
5774     ;; buffer as a result of the new limit.
5775     (- total (length gnus-newsgroup-data))))
5776
5777 (defsubst gnus-invisible-cut-children (threads)
5778   (let ((num 0))
5779     (while threads
5780       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
5781         (incf num))
5782       (pop threads))
5783     (< num 2)))
5784
5785 (defsubst gnus-cut-thread (thread)
5786   "Go forwards in the thread until we find an article that we want to display."
5787   (when (or (eq gnus-fetch-old-headers 'some)
5788             (eq gnus-build-sparse-threads 'some)
5789             (eq gnus-build-sparse-threads 'more))
5790     ;; Deal with old-fetched headers and sparse threads.
5791     (while (and
5792             thread
5793             (or
5794              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
5795              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
5796             (or (<= (length (cdr thread)) 1)
5797                 (gnus-invisible-cut-children (cdr thread))))
5798       (setq thread (cadr thread))))
5799   thread)
5800
5801 (defun gnus-cut-threads (threads)
5802   "Cut off all uninteresting articles from the beginning of threads."
5803   (when (or (eq gnus-fetch-old-headers 'some)
5804             (eq gnus-build-sparse-threads 'some)
5805             (eq gnus-build-sparse-threads 'more))
5806     (let ((th threads))
5807       (while th
5808         (setcar th (gnus-cut-thread (car th)))
5809         (setq th (cdr th)))))
5810   ;; Remove nixed out threads.
5811   (delq nil threads))
5812
5813 (defun gnus-summary-initial-limit (&optional show-if-empty)
5814   "Figure out what the initial limit is supposed to be on group entry.
5815 This entails weeding out unwanted dormants, low-scored articles,
5816 fetch-old-headers verbiage, and so on."
5817   ;; Most groups have nothing to remove.
5818   (if (or gnus-inhibit-limiting
5819           (and (null gnus-newsgroup-dormant)
5820                (not (eq gnus-fetch-old-headers 'some))
5821                (null gnus-summary-expunge-below)
5822                (not (eq gnus-build-sparse-threads 'some))
5823                (not (eq gnus-build-sparse-threads 'more))
5824                (null gnus-thread-expunge-below)
5825                (not gnus-use-nocem)))
5826       ()                                ; Do nothing.
5827     (push gnus-newsgroup-limit gnus-newsgroup-limits)
5828     (setq gnus-newsgroup-limit nil)
5829     (mapatoms
5830      (lambda (node)
5831        (unless (car (symbol-value node))
5832          ;; These threads have no parents -- they are roots.
5833          (let ((nodes (cdr (symbol-value node)))
5834                thread)
5835            (while nodes
5836              (if (and gnus-thread-expunge-below
5837                       (< (gnus-thread-total-score (car nodes))
5838                          gnus-thread-expunge-below))
5839                  (gnus-expunge-thread (pop nodes))
5840                (setq thread (pop nodes))
5841                (gnus-summary-limit-children thread))))))
5842      gnus-newsgroup-dependencies)
5843     ;; If this limitation resulted in an empty group, we might
5844     ;; pop the previous limit and use it instead.
5845     (when (and (not gnus-newsgroup-limit)
5846                show-if-empty)
5847       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
5848     gnus-newsgroup-limit))
5849
5850 (defun gnus-summary-limit-children (thread)
5851   "Return 1 if this subthread is visible and 0 if it is not."
5852   ;; First we get the number of visible children to this thread.  This
5853   ;; is done by recursing down the thread using this function, so this
5854   ;; will really go down to a leaf article first, before slowly
5855   ;; working its way up towards the root.
5856   (when thread
5857     (let ((children
5858            (if (cdr thread)
5859                (apply '+ (mapcar 'gnus-summary-limit-children
5860                                  (cdr thread)))
5861              0))
5862           (number (mail-header-number (car thread)))
5863           score)
5864       (if (and
5865            (not (memq number gnus-newsgroup-marked))
5866            (or
5867             ;; If this article is dormant and has absolutely no visible
5868             ;; children, then this article isn't visible.
5869             (and (memq number gnus-newsgroup-dormant)
5870                  (= children 0))
5871             ;; If this is "fetch-old-headered" and there is only one
5872             ;; visible child (or less), then we don't want this article.
5873             (and (eq gnus-fetch-old-headers 'some)
5874                  (memq number gnus-newsgroup-ancient)
5875                  (zerop children))
5876             ;; If this is a sparsely inserted article with no children,
5877             ;; we don't want it.
5878             (and (eq gnus-build-sparse-threads 'some)
5879                  (memq number gnus-newsgroup-sparse)
5880                  (zerop children))
5881             ;; If we use expunging, and this article is really
5882             ;; low-scored, then we don't want this article.
5883             (when (and gnus-summary-expunge-below
5884                        (< (setq score
5885                                 (or (cdr (assq number gnus-newsgroup-scored))
5886                                     gnus-summary-default-score))
5887                           gnus-summary-expunge-below))
5888               ;; We increase the expunge-tally here, but that has
5889               ;; nothing to do with the limits, really.
5890               (incf gnus-newsgroup-expunged-tally)
5891               ;; We also mark as read here, if that's wanted.
5892               (when (and gnus-summary-mark-below
5893                          (< score gnus-summary-mark-below))
5894                 (setq gnus-newsgroup-unreads
5895                       (delq number gnus-newsgroup-unreads))
5896                 (if gnus-newsgroup-auto-expire
5897                     (push number gnus-newsgroup-expirable)
5898                   (push (cons number gnus-low-score-mark)
5899                         gnus-newsgroup-reads)))
5900               t)
5901             ;; Check NoCeM things.
5902             (and gnus-use-nocem
5903                  (gnus-nocem-unwanted-article-p
5904                   (mail-header-id (car thread))))))
5905           ;; Nope, invisible article.
5906           0
5907         ;; Ok, this article is to be visible, so we add it to the limit
5908         ;; and return 1.
5909         (push number gnus-newsgroup-limit)
5910         1))))
5911
5912 (defun gnus-expunge-thread (thread)
5913   "Mark all articles in THREAD as read."
5914   (let* ((number (mail-header-number (car thread))))
5915     (incf gnus-newsgroup-expunged-tally)
5916     ;; We also mark as read here, if that's wanted.
5917     (setq gnus-newsgroup-unreads
5918           (delq number gnus-newsgroup-unreads))
5919     (if gnus-newsgroup-auto-expire
5920         (push number gnus-newsgroup-expirable)
5921       (push (cons number gnus-low-score-mark)
5922             gnus-newsgroup-reads)))
5923   ;; Go recursively through all subthreads.
5924   (mapcar 'gnus-expunge-thread (cdr thread)))
5925
5926 ;; Summary article oriented commands
5927
5928 (defun gnus-summary-refer-parent-article (n)
5929   "Refer parent article N times.
5930 If N is negative, go to ancestor -N instead.
5931 The difference between N and the number of articles fetched is returned."
5932   (interactive "p")
5933   (gnus-set-global-variables)
5934   (let ((skip 1)
5935         error header ref)
5936     (when (not (natnump n))
5937       (setq skip (abs n)
5938             n 1))
5939     (while (and (> n 0)
5940                 (not error))
5941       (setq header (gnus-summary-article-header))
5942       (setq ref
5943             ;; If we try to find the parent of the currently
5944             ;; displayed article, then we take a look at the actual
5945             ;; References header, since this is slightly more
5946             ;; reliable than the References field we got from the
5947             ;; server.
5948             (if (and (eq (mail-header-number header)
5949                          (cdr gnus-article-current))
5950                      (equal gnus-newsgroup-name
5951                             (car gnus-article-current)))
5952                 (save-excursion
5953                   (set-buffer gnus-original-article-buffer)
5954                   (nnheader-narrow-to-headers)
5955                   (prog1
5956                       (message-fetch-field "references")
5957                     (widen)))
5958               ;; It's not the current article, so we take a bet on
5959               ;; the value we got from the server.
5960               (mail-header-references header)))
5961       (if ref 
5962           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
5963             (gnus-message 1 "Couldn't find parent"))
5964         (gnus-message 1 "No references in article %d"
5965                       (gnus-summary-article-number))
5966         (setq error t))
5967       (decf n))
5968     (gnus-summary-position-point)
5969     n))
5970
5971 (defun gnus-summary-refer-references ()
5972   "Fetch all articles mentioned in the References header.
5973 Return how many articles were fetched."
5974   (interactive)
5975   (gnus-set-global-variables)
5976   (let ((ref (mail-header-references (gnus-summary-article-header)))
5977         (current (gnus-summary-article-number))
5978         (n 0))
5979     ;; For each Message-ID in the References header...
5980     (while (string-match "<[^>]*>" ref)
5981       (incf n)
5982       ;; ... fetch that article.
5983       (gnus-summary-refer-article
5984        (prog1 (match-string 0 ref)
5985          (setq ref (substring ref (match-end 0))))))
5986     (gnus-summary-goto-subject current)
5987     (gnus-summary-position-point)
5988     n))
5989
5990 (defun gnus-summary-refer-article (message-id)
5991   "Fetch an article specified by MESSAGE-ID."
5992   (interactive "sMessage-ID: ")
5993   (when (and (stringp message-id)
5994              (not (zerop (length message-id))))
5995     ;; Construct the correct Message-ID if necessary.
5996     ;; Suggested by tale@pawl.rpi.edu.
5997     (unless (string-match "^<" message-id)
5998       (setq message-id (concat "<" message-id)))
5999     (unless (string-match ">$" message-id)
6000       (setq message-id (concat message-id ">")))
6001     (let* ((header (gnus-id-to-header message-id))
6002            (sparse (and header
6003                         (memq (mail-header-number header)
6004                               gnus-newsgroup-sparse))))
6005       (if header
6006           (prog1
6007               ;; The article is present in the buffer, to we just go to it.
6008               (gnus-summary-goto-article 
6009                (mail-header-number header) nil header)
6010             (when sparse
6011               (gnus-summary-update-article (mail-header-number header))))
6012         ;; We fetch the article
6013         (let ((gnus-override-method 
6014                (and (gnus-news-group-p gnus-newsgroup-name)
6015                     gnus-refer-article-method))
6016               number)
6017           ;; Start the special refer-article method, if necessary.
6018           (when (and gnus-refer-article-method
6019                      (gnus-news-group-p gnus-newsgroup-name))
6020             (gnus-check-server gnus-refer-article-method))
6021           ;; Fetch the header, and display the article.
6022           (if (setq number (gnus-summary-insert-subject message-id))
6023               (gnus-summary-select-article nil nil nil number)
6024             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6025
6026 (defun gnus-summary-enter-digest-group (&optional force)
6027   "Enter an nndoc group based on the current article.
6028 If FORCE, force a digest interpretation.  If not, try
6029 to guess what the document format is."
6030   (interactive "P")
6031   (gnus-set-global-variables)
6032   (let ((conf gnus-current-window-configuration))
6033     (save-excursion
6034       (gnus-summary-select-article))
6035     (setq gnus-current-window-configuration conf)
6036     (let* ((name (format "%s-%d"
6037                          (gnus-group-prefixed-name
6038                           gnus-newsgroup-name (list 'nndoc ""))
6039                          (save-excursion
6040                            (set-buffer gnus-summary-buffer)
6041                            gnus-current-article)))
6042            (ogroup gnus-newsgroup-name)
6043            (params (append (gnus-info-params (gnus-get-info ogroup))
6044                            (list (cons 'to-group ogroup))))
6045            (case-fold-search t)
6046            (buf (current-buffer))
6047            dig)
6048       (save-excursion
6049         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6050         (insert-buffer-substring gnus-original-article-buffer)
6051         ;; Remove lines that may lead nndoc to misinterpret the
6052         ;; document type.
6053         (narrow-to-region
6054          (goto-char (point-min))
6055          (or (search-forward "\n\n" nil t) (point)))
6056         (goto-char (point-min))
6057         (delete-matching-lines "^\\(Path\\):\\|^From ")
6058         (widen))
6059       (unwind-protect
6060           (if (gnus-group-read-ephemeral-group
6061                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6062                             (nndoc-article-type 
6063                              ,(if force 'digest 'guess))) t)
6064               ;; Make all postings to this group go to the parent group.
6065               (nconc (gnus-info-params (gnus-get-info name))
6066                      params)
6067             ;; Couldn't select this doc group.
6068             (switch-to-buffer buf)
6069             (gnus-set-global-variables)
6070             (gnus-configure-windows 'summary)
6071             (gnus-message 3 "Article couldn't be entered?"))
6072         (kill-buffer dig)))))
6073
6074 (defun gnus-summary-read-document (n)
6075   "Open a new group based on the current article(s).
6076 Obeys the standard process/prefix convention."
6077   (interactive "P")
6078   (let* ((articles (gnus-summary-work-articles n))
6079          (ogroup gnus-newsgroup-name)
6080          (params (append (gnus-info-params (gnus-get-info ogroup))
6081                          (list (cons 'to-group ogroup))))
6082          article group egroup groups vgroup)
6083     (while (setq article (pop articles))
6084       (setq group (format "%s-%d" gnus-newsgroup-name article))
6085       (gnus-summary-remove-process-mark article)
6086       (when (gnus-summary-display-article article)
6087         (save-excursion
6088           (nnheader-temp-write nil
6089             (insert-buffer-substring gnus-original-article-buffer)
6090             ;; Remove some headers that may lead nndoc to make
6091             ;; the wrong guess.
6092             (message-narrow-to-head)
6093             (goto-char (point-min))
6094             (delete-matching-lines "^\\(Path\\):\\|^From ")
6095             (widen)
6096             (if (setq egroup
6097                       (gnus-group-read-ephemeral-group
6098                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6099                                      (nndoc-article-type guess))
6100                        t nil t))
6101                 (progn
6102                   ;; Make all postings to this group go to the parent group.
6103                   (nconc (gnus-info-params (gnus-get-info egroup))
6104                          params)
6105                   (push egroup groups))
6106               ;; Couldn't select this doc group.
6107               (gnus-error 3 "Article couldn't be entered"))))))
6108     ;; Now we have selected all the documents.
6109     (cond
6110      ((not groups)
6111       (error "None of the articles could be interpreted as documents"))
6112      ((gnus-group-read-ephemeral-group
6113        (setq vgroup (format
6114                      "nnvirtual:%s-%s" gnus-newsgroup-name
6115                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6116        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6117        t
6118        (cons (current-buffer) 'summary)))
6119      (t
6120       (error "Couldn't select virtual nndoc group")))))
6121       
6122 (defun gnus-summary-isearch-article (&optional regexp-p)
6123   "Do incremental search forward on the current article.
6124 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6125   (interactive "P")
6126   (gnus-set-global-variables)
6127   (gnus-summary-select-article)
6128   (gnus-configure-windows 'article)
6129   (gnus-eval-in-buffer-window gnus-article-buffer
6130     ;;(goto-char (point-min))
6131     (isearch-forward regexp-p)))
6132
6133 (defun gnus-summary-search-article-forward (regexp &optional backward)
6134   "Search for an article containing REGEXP forward.
6135 If BACKWARD, search backward instead."
6136   (interactive
6137    (list (read-string
6138           (format "Search article %s (regexp%s): "
6139                   (if current-prefix-arg "backward" "forward")
6140                   (if gnus-last-search-regexp
6141                       (concat ", default " gnus-last-search-regexp)
6142                     "")))
6143          current-prefix-arg))
6144   (gnus-set-global-variables)
6145   (if (string-equal regexp "")
6146       (setq regexp (or gnus-last-search-regexp ""))
6147     (setq gnus-last-search-regexp regexp))
6148   (if (gnus-summary-search-article regexp backward)
6149       (gnus-summary-show-thread)
6150     (error "Search failed: \"%s\"" regexp)))
6151
6152 (defun gnus-summary-search-article-backward (regexp)
6153   "Search for an article containing REGEXP backward."
6154   (interactive
6155    (list (read-string
6156           (format "Search article backward (regexp%s): "
6157                   (if gnus-last-search-regexp
6158                       (concat ", default " gnus-last-search-regexp)
6159                     "")))))
6160   (gnus-summary-search-article-forward regexp 'backward))
6161
6162 (defun gnus-summary-search-article (regexp &optional backward)
6163   "Search for an article containing REGEXP.
6164 Optional argument BACKWARD means do search for backward.
6165 `gnus-select-article-hook' is not called during the search."
6166   (let ((gnus-select-article-hook nil)  ;Disable hook.
6167         (gnus-article-display-hook nil)
6168         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6169         (gnus-use-article-prefetch nil)
6170         (sum (current-buffer))
6171         (found nil)
6172         point)
6173     (gnus-save-hidden-threads
6174       (gnus-summary-select-article)
6175       (set-buffer gnus-article-buffer)
6176       (when backward
6177         (forward-line -1))
6178       (while (not found)
6179         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6180         (if (if backward
6181                 (re-search-backward regexp nil t)
6182               (re-search-forward regexp nil t))
6183             ;; We found the regexp.
6184             (progn
6185               (setq found 'found)
6186               (beginning-of-line)
6187               (set-window-start
6188                (get-buffer-window (current-buffer))
6189                (point))
6190               (forward-line 1)
6191               (set-buffer sum)
6192               (setq point (point)))
6193           ;; We didn't find it, so we go to the next article.
6194           (set-buffer sum)
6195           (if (not (if backward (gnus-summary-find-prev)
6196                      (gnus-summary-find-next)))
6197               ;; No more articles.
6198               (setq found t)
6199             ;; Select the next article and adjust point.
6200             (gnus-summary-select-article)
6201             (set-buffer gnus-article-buffer)
6202             (widen)
6203             (goto-char (if backward (point-max) (point-min))))))
6204       (gnus-message 7 ""))
6205     ;; Return whether we found the regexp.
6206     (when (eq found 'found)
6207       (goto-char point)
6208       (gnus-summary-show-thread)
6209       (gnus-summary-goto-subject gnus-current-article)
6210       (gnus-summary-position-point)
6211       t)))
6212
6213 (defun gnus-summary-find-matching (header regexp &optional backward unread
6214                                           not-case-fold)
6215   "Return a list of all articles that match REGEXP on HEADER.
6216 The search stars on the current article and goes forwards unless
6217 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6218 If UNREAD is non-nil, only unread articles will
6219 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6220 in the comparisons."
6221   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6222                 (gnus-data-find-list
6223                  (gnus-summary-article-number) (gnus-data-list backward))))
6224         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6225         (case-fold-search (not not-case-fold))
6226         articles d)
6227     (unless (fboundp (intern (concat "mail-header-" header)))
6228       (error "%s is not a valid header" header))
6229     (while data
6230       (setq d (car data))
6231       (and (or (not unread)             ; We want all articles...
6232                (gnus-data-unread-p d))  ; Or just unreads.
6233            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6234            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6235            (push (gnus-data-number d) articles)) ; Success!
6236       (setq data (cdr data)))
6237     (nreverse articles)))
6238
6239 (defun gnus-summary-execute-command (header regexp command &optional backward)
6240   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6241 If HEADER is an empty string (or nil), the match is done on the entire
6242 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6243   (interactive
6244    (list (let ((completion-ignore-case t))
6245            (completing-read
6246             "Header name: "
6247             (mapcar (lambda (string) (list string))
6248                     '("Number" "Subject" "From" "Lines" "Date"
6249                       "Message-ID" "Xref" "References" "Body"))
6250             nil 'require-match))
6251          (read-string "Regexp: ")
6252          (read-key-sequence "Command: ")
6253          current-prefix-arg))
6254   (when (equal header "Body")
6255     (setq header ""))
6256   (gnus-set-global-variables)
6257   ;; Hidden thread subtrees must be searched as well.
6258   (gnus-summary-show-all-threads)
6259   ;; We don't want to change current point nor window configuration.
6260   (save-excursion
6261     (save-window-excursion
6262       (gnus-message 6 "Executing %s..." (key-description command))
6263       ;; We'd like to execute COMMAND interactively so as to give arguments.
6264       (gnus-execute header regexp
6265                     `(call-interactively ',(key-binding command))
6266                     backward)
6267       (gnus-message 6 "Executing %s...done" (key-description command)))))
6268
6269 (defun gnus-summary-beginning-of-article ()
6270   "Scroll the article back to the beginning."
6271   (interactive)
6272   (gnus-set-global-variables)
6273   (gnus-summary-select-article)
6274   (gnus-configure-windows 'article)
6275   (gnus-eval-in-buffer-window gnus-article-buffer
6276     (widen)
6277     (goto-char (point-min))
6278     (when gnus-break-pages
6279       (gnus-narrow-to-page))))
6280
6281 (defun gnus-summary-end-of-article ()
6282   "Scroll to the end of the article."
6283   (interactive)
6284   (gnus-set-global-variables)
6285   (gnus-summary-select-article)
6286   (gnus-configure-windows 'article)
6287   (gnus-eval-in-buffer-window gnus-article-buffer
6288     (widen)
6289     (goto-char (point-max))
6290     (recenter -3)
6291     (when gnus-break-pages
6292       (gnus-narrow-to-page))))
6293
6294 (defun gnus-summary-show-article (&optional arg)
6295   "Force re-fetching of the current article.
6296 If ARG (the prefix) is non-nil, show the raw article without any
6297 article massaging functions being run."
6298   (interactive "P")
6299   (gnus-set-global-variables)
6300   (if (not arg)
6301       ;; Select the article the normal way.
6302       (gnus-summary-select-article nil 'force)
6303     ;; Bind the article treatment functions to nil.
6304     (let ((gnus-have-all-headers t)
6305           gnus-article-display-hook
6306           gnus-article-prepare-hook
6307           gnus-break-pages
6308           gnus-visual)
6309       (gnus-summary-select-article nil 'force)))
6310   (gnus-summary-goto-subject gnus-current-article)
6311                                         ;  (gnus-configure-windows 'article)
6312   (gnus-summary-position-point))
6313
6314 (defun gnus-summary-verbose-headers (&optional arg)
6315   "Toggle permanent full header display.
6316 If ARG is a positive number, turn header display on.
6317 If ARG is a negative number, turn header display off."
6318   (interactive "P")
6319   (gnus-set-global-variables)
6320   (setq gnus-show-all-headers
6321         (cond ((or (not (numberp arg))
6322                    (zerop arg))
6323                (not gnus-show-all-headers))
6324               ((natnump arg)
6325                t)))
6326   (gnus-summary-show-article))
6327
6328 (defun gnus-summary-toggle-header (&optional arg)
6329   "Show the headers if they are hidden, or hide them if they are shown.
6330 If ARG is a positive number, show the entire header.
6331 If ARG is a negative number, hide the unwanted header lines."
6332   (interactive "P")
6333   (gnus-set-global-variables)
6334   (save-excursion
6335     (set-buffer gnus-article-buffer)
6336     (let* ((buffer-read-only nil)
6337            (inhibit-point-motion-hooks t)
6338            (hidden (text-property-any
6339                     (goto-char (point-min)) (search-forward "\n\n")
6340                     'invisible t))
6341            e)
6342       (goto-char (point-min))
6343       (when (search-forward "\n\n" nil t)
6344         (delete-region (point-min) (1- (point))))
6345       (goto-char (point-min))
6346       (save-excursion
6347         (set-buffer gnus-original-article-buffer)
6348         (goto-char (point-min))
6349         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6350       (insert-buffer-substring gnus-original-article-buffer 1 e)
6351       (let ((article-inhibit-hiding t))
6352         (run-hooks 'gnus-article-display-hook))
6353       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6354         (gnus-article-hide-headers)))))
6355
6356 (defun gnus-summary-show-all-headers ()
6357   "Make all header lines visible."
6358   (interactive)
6359   (gnus-set-global-variables)
6360   (gnus-article-show-all-headers))
6361
6362 (defun gnus-summary-toggle-mime (&optional arg)
6363   "Toggle MIME processing.
6364 If ARG is a positive number, turn MIME processing on."
6365   (interactive "P")
6366   (gnus-set-global-variables)
6367   (setq gnus-show-mime
6368         (if (null arg) (not gnus-show-mime)
6369           (> (prefix-numeric-value arg) 0)))
6370   (gnus-summary-select-article t 'force))
6371
6372 (defun gnus-summary-caesar-message (&optional arg)
6373   "Caesar rotate the current article by 13.
6374 The numerical prefix specifies how manu places to rotate each letter
6375 forward."
6376   (interactive "P")
6377   (gnus-set-global-variables)
6378   (gnus-summary-select-article)
6379   (let ((mail-header-separator ""))
6380     (gnus-eval-in-buffer-window gnus-article-buffer
6381       (save-restriction
6382         (widen)
6383         (let ((start (window-start))
6384               buffer-read-only)
6385           (message-caesar-buffer-body arg)
6386           (set-window-start (get-buffer-window (current-buffer)) start))))))
6387
6388 (defun gnus-summary-stop-page-breaking ()
6389   "Stop page breaking in the current article."
6390   (interactive)
6391   (gnus-set-global-variables)
6392   (gnus-summary-select-article)
6393   (gnus-eval-in-buffer-window gnus-article-buffer
6394     (widen)))
6395
6396 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
6397   "Move the current article to a different newsgroup.
6398 If N is a positive number, move the N next articles.
6399 If N is a negative number, move the N previous articles.
6400 If N is nil and any articles have been marked with the process mark,
6401 move those articles instead.
6402 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6403 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6404 re-spool using this method.
6405
6406 For this function to work, both the current newsgroup and the
6407 newsgroup that you want to move to have to support the `request-move'
6408 and `request-accept' functions."
6409   (interactive "P")
6410   (unless action
6411     (setq action 'move))
6412   (gnus-set-global-variables)
6413   ;; Check whether the source group supports the required functions.
6414   (cond ((and (eq action 'move)
6415               (not (gnus-check-backend-function
6416                     'request-move-article gnus-newsgroup-name)))
6417          (error "The current group does not support article moving"))
6418         ((and (eq action 'crosspost)
6419               (not (gnus-check-backend-function
6420                     'request-replace-article gnus-newsgroup-name)))
6421          (error "The current group does not support article editing")))
6422   (let ((articles (gnus-summary-work-articles n))
6423         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6424         (names '((move "Move" "Moving")
6425                  (copy "Copy" "Copying")
6426                  (crosspost "Crosspost" "Crossposting")))
6427         (copy-buf (save-excursion
6428                     (nnheader-set-temp-buffer " *copy article*")))
6429         art-group to-method new-xref article to-groups)
6430     (unless (assq action names)
6431       (error "Unknown action %s" action))
6432     ;; Read the newsgroup name.
6433     (when (and (not to-newsgroup)
6434                (not select-method))
6435       (setq to-newsgroup
6436             (gnus-read-move-group-name
6437              (cadr (assq action names))
6438              (symbol-value (intern (format "gnus-current-%s-group" action)))
6439              articles prefix))
6440       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6441     (setq to-method (or select-method 
6442                         (gnus-group-name-to-method to-newsgroup)))
6443     ;; Check the method we are to move this article to...
6444     (unless (gnus-check-backend-function 'request-accept-article (car to-method))
6445       (error "%s does not support article copying" (car to-method)))
6446     (unless (gnus-check-server to-method)
6447       (error "Can't open server %s" (car to-method)))
6448     (gnus-message 6 "%s to %s: %s..."
6449                   (caddr (assq action names))
6450                   (or (car select-method) to-newsgroup) articles)
6451     (while articles
6452       (setq article (pop articles))
6453       (setq
6454        art-group
6455        (cond
6456         ;; Move the article.
6457         ((eq action 'move)
6458          (gnus-request-move-article
6459           article                       ; Article to move
6460           gnus-newsgroup-name           ; From newsgroup
6461           (nth 1 (gnus-find-method-for-group
6462                   gnus-newsgroup-name)) ; Server
6463           (list 'gnus-request-accept-article
6464                 to-newsgroup (list 'quote select-method)
6465                 (not articles))         ; Accept form
6466           (not articles)))              ; Only save nov last time
6467         ;; Copy the article.
6468         ((eq action 'copy)
6469          (save-excursion
6470            (set-buffer copy-buf)
6471            (gnus-request-article-this-buffer article gnus-newsgroup-name)
6472            (gnus-request-accept-article
6473             to-newsgroup select-method (not articles))))
6474         ;; Crosspost the article.
6475         ((eq action 'crosspost)
6476          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
6477            (setq new-xref (concat gnus-newsgroup-name ":" article))
6478            (if (and xref (not (string= xref "")))
6479                (progn
6480                  (when (string-match "^Xref: " xref)
6481                    (setq xref (substring xref (match-end 0))))
6482                  (setq new-xref (concat xref " " new-xref)))
6483              (setq new-xref (concat (system-name) " " new-xref)))
6484            (save-excursion
6485              (set-buffer copy-buf)
6486              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6487              (nnheader-replace-header "xref" new-xref)
6488              (gnus-request-accept-article
6489               to-newsgroup select-method (not articles)))))))
6490       (if (not art-group)
6491           (gnus-message 1 "Couldn't %s article %s"
6492                         (cadr (assq action names)) article)
6493         (let* ((entry
6494                 (or
6495                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6496                  (gnus-gethash
6497                   (gnus-group-prefixed-name
6498                    (car art-group)
6499                    (or select-method 
6500                        (gnus-find-method-for-group to-newsgroup)))
6501                   gnus-newsrc-hashtb)))
6502                (info (nth 2 entry))
6503                (to-group (gnus-info-group info)))
6504           ;; Update the group that has been moved to.
6505           (when (and info
6506                      (memq action '(move copy)))
6507             (unless (member to-group to-groups)
6508               (push to-group to-groups))
6509
6510             (unless (memq article gnus-newsgroup-unreads)
6511               (gnus-info-set-read
6512                info (gnus-add-to-range (gnus-info-read info)
6513                                        (list (cdr art-group)))))
6514
6515             ;; Copy any marks over to the new group.
6516             (let ((marks gnus-article-mark-lists)
6517                   (to-article (cdr art-group)))
6518
6519               ;; See whether the article is to be put in the cache.
6520               (when gnus-use-cache
6521                 (gnus-cache-possibly-enter-article
6522                  to-group to-article
6523                  (let ((header (copy-sequence
6524                                 (gnus-summary-article-header article))))
6525                    (mail-header-set-number header to-article)
6526                    header)
6527                  (memq article gnus-newsgroup-marked)
6528                  (memq article gnus-newsgroup-dormant)
6529                  (memq article gnus-newsgroup-unreads)))
6530
6531               (while marks
6532                 (when (memq article (symbol-value
6533                                      (intern (format "gnus-newsgroup-%s"
6534                                                      (caar marks)))))
6535                   ;; If the other group is the same as this group,
6536                   ;; then we have to add the mark to the list.
6537                   (when (equal to-group gnus-newsgroup-name)
6538                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
6539                          (cons to-article
6540                                (symbol-value
6541                                 (intern (format "gnus-newsgroup-%s"
6542                                                 (caar marks)))))))
6543                   ;; Copy mark to other group.
6544                   (gnus-add-marked-articles
6545                    to-group (cdar marks) (list to-article) info))
6546                 (setq marks (cdr marks)))))
6547
6548           ;; Update the Xref header in this article to point to
6549           ;; the new crossposted article we have just created.
6550           (when (eq action 'crosspost)
6551             (save-excursion
6552               (set-buffer copy-buf)
6553               (gnus-request-article-this-buffer article gnus-newsgroup-name)
6554               (nnheader-replace-header
6555                "xref" (concat new-xref " " (gnus-group-prefixed-name
6556                                             (car art-group) to-method)
6557                               ":" (cdr art-group)))
6558               (gnus-request-replace-article
6559                article gnus-newsgroup-name (current-buffer)))))
6560
6561         (gnus-summary-goto-subject article)
6562         (when (eq action 'move)
6563           (gnus-summary-mark-article article gnus-canceled-mark)))
6564       (gnus-summary-remove-process-mark article))
6565     ;; Re-activate all groups that have been moved to.
6566     (while to-groups
6567       (gnus-activate-group (pop to-groups)))
6568     
6569     (gnus-kill-buffer copy-buf)
6570     (gnus-summary-position-point)
6571     (gnus-set-mode-line 'summary)))
6572
6573 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
6574   "Move the current article to a different newsgroup.
6575 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6576 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6577 re-spool using this method."
6578   (interactive "P")
6579   (gnus-summary-move-article n nil select-method 'copy))
6580
6581 (defun gnus-summary-crosspost-article (&optional n)
6582   "Crosspost the current article to some other group."
6583   (interactive "P")
6584   (gnus-summary-move-article n nil nil 'crosspost))
6585
6586 (defvar gnus-summary-respool-default-method nil
6587   "Default method for respooling an article.  
6588 If nil, use to the current newsgroup method.")
6589
6590 (defun gnus-summary-respool-article (&optional n method)
6591   "Respool the current article.
6592 The article will be squeezed through the mail spooling process again,
6593 which means that it will be put in some mail newsgroup or other
6594 depending on `nnmail-split-methods'.
6595 If N is a positive number, respool the N next articles.
6596 If N is a negative number, respool the N previous articles.
6597 If N is nil and any articles have been marked with the process mark,
6598 respool those articles instead.
6599
6600 Respooling can be done both from mail groups and \"real\" newsgroups.
6601 In the former case, the articles in question will be moved from the
6602 current group into whatever groups they are destined to.  In the
6603 latter case, they will be copied into the relevant groups."
6604   (interactive 
6605    (list current-prefix-arg
6606          (let* ((methods (gnus-methods-using 'respool))
6607                 (methname
6608                  (symbol-name (or gnus-summary-respool-default-method
6609                                   (car (gnus-find-method-for-group
6610                                         gnus-newsgroup-name)))))
6611                 (method
6612                  (gnus-completing-read 
6613                   methname "What backend do you want to use when respooling?"
6614                   methods nil t nil 'gnus-mail-method-history))
6615                 ms)
6616            (cond
6617             ((zerop (length (setq ms (gnus-servers-using-backend method))))
6618              (list (intern method) ""))
6619             ((= 1 (length ms))
6620              (car ms))
6621             (t
6622              (cdr (completing-read 
6623                    "Server name: "
6624                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
6625   (gnus-set-global-variables)
6626   (unless method
6627     (error "No method given for respooling"))
6628   (if (assoc (symbol-name
6629               (car (gnus-find-method-for-group gnus-newsgroup-name)))
6630              (gnus-methods-using 'respool))
6631       (gnus-summary-move-article n nil method)
6632     (gnus-summary-copy-article n nil method)))
6633
6634 (defun gnus-summary-import-article (file)
6635   "Import a random file into a mail newsgroup."
6636   (interactive "fImport file: ")
6637   (gnus-set-global-variables)
6638   (let ((group gnus-newsgroup-name)
6639         (now (current-time))
6640         atts lines)
6641     (unless (gnus-check-backend-function 'request-accept-article group)
6642       (error "%s does not support article importing" group))
6643     (or (file-readable-p file)
6644         (not (file-regular-p file))
6645         (error "Can't read %s" file))
6646     (save-excursion
6647       (set-buffer (get-buffer-create " *import file*"))
6648       (buffer-disable-undo (current-buffer))
6649       (erase-buffer)
6650       (insert-file-contents file)
6651       (goto-char (point-min))
6652       (unless (nnheader-article-p)
6653         ;; This doesn't look like an article, so we fudge some headers.
6654         (setq atts (file-attributes file)
6655               lines (count-lines (point-min) (point-max)))
6656         (insert "From: " (read-string "From: ") "\n"
6657                 "Subject: " (read-string "Subject: ") "\n"
6658                 "Date: " (timezone-make-date-arpa-standard
6659                           (current-time-string (nth 5 atts))
6660                           (current-time-zone now)
6661                           (current-time-zone now))
6662                 "\n"
6663                 "Message-ID: " (message-make-message-id) "\n"
6664                 "Lines: " (int-to-string lines) "\n"
6665                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
6666       (gnus-request-accept-article group nil t)
6667       (kill-buffer (current-buffer)))))
6668
6669 (defun gnus-summary-article-posted-p ()
6670   "Say whether the current (mail) article is available from `gnus-select-method' as well.
6671 This will be the case if the article has both been mailed and posted."
6672   (interactive)
6673   (let ((id (mail-header-references (gnus-summary-article-header)))
6674         (gnus-override-method
6675          (or gnus-refer-article-method gnus-select-method)))
6676     (if (gnus-request-head id "")
6677         (gnus-message 2 "The current message was found on %s"
6678                       gnus-override-method)
6679       (gnus-message 2 "The current message couldn't be found on %s"
6680                     gnus-override-method)
6681       nil)))
6682
6683 (defun gnus-summary-expire-articles (&optional now)
6684   "Expire all articles that are marked as expirable in the current group."
6685   (interactive)
6686   (gnus-set-global-variables)
6687   (when (gnus-check-backend-function
6688          'request-expire-articles gnus-newsgroup-name)
6689     ;; This backend supports expiry.
6690     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
6691            (expirable (if total
6692                           (gnus-list-of-read-articles gnus-newsgroup-name)
6693                         (setq gnus-newsgroup-expirable
6694                               (sort gnus-newsgroup-expirable '<))))
6695            (expiry-wait (if now 'immediate
6696                           (gnus-group-find-parameter
6697                            gnus-newsgroup-name 'expiry-wait)))
6698            es)
6699       (when expirable
6700         ;; There are expirable articles in this group, so we run them
6701         ;; through the expiry process.
6702         (gnus-message 6 "Expiring articles...")
6703         ;; The list of articles that weren't expired is returned.
6704         (if expiry-wait
6705             (let ((nnmail-expiry-wait-function nil)
6706                   (nnmail-expiry-wait expiry-wait))
6707               (setq es (gnus-request-expire-articles
6708                         expirable gnus-newsgroup-name)))
6709           (setq es (gnus-request-expire-articles
6710                     expirable gnus-newsgroup-name)))
6711         (unless total
6712           (setq gnus-newsgroup-expirable es))
6713         ;; We go through the old list of expirable, and mark all
6714         ;; really expired articles as nonexistent.
6715         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
6716           (let ((gnus-use-cache nil))
6717             (while expirable
6718               (unless (memq (car expirable) es)
6719                 (when (gnus-data-find (car expirable))
6720                   (gnus-summary-mark-article
6721                    (car expirable) gnus-canceled-mark)))
6722               (setq expirable (cdr expirable)))))
6723         (gnus-message 6 "Expiring articles...done")))))
6724
6725 (defun gnus-summary-expire-articles-now ()
6726   "Expunge all expirable articles in the current group.
6727 This means that *all* articles that are marked as expirable will be
6728 deleted forever, right now."
6729   (interactive)
6730   (gnus-set-global-variables)
6731   (or gnus-expert-user
6732       (gnus-y-or-n-p
6733        "Are you really, really, really sure you want to delete all these messages? ")
6734       (error "Phew!"))
6735   (gnus-summary-expire-articles t))
6736
6737 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6738 (defun gnus-summary-delete-article (&optional n)
6739   "Delete the N next (mail) articles.
6740 This command actually deletes articles.  This is not a marking
6741 command.  The article will disappear forever from your life, never to
6742 return.
6743 If N is negative, delete backwards.
6744 If N is nil and articles have been marked with the process mark,
6745 delete these instead."
6746   (interactive "P")
6747   (gnus-set-global-variables)
6748   (unless (gnus-check-backend-function 'request-expire-articles
6749                                        gnus-newsgroup-name)
6750     (error "The current newsgroup does not support article deletion."))
6751   ;; Compute the list of articles to delete.
6752   (let ((articles (gnus-summary-work-articles n))
6753         not-deleted)
6754     (if (and gnus-novice-user
6755              (not (gnus-y-or-n-p
6756                    (format "Do you really want to delete %s forever? "
6757                            (if (> (length articles) 1)
6758                                (format "these %s articles" (length articles))
6759                              "this article")))))
6760         ()
6761       ;; Delete the articles.
6762       (setq not-deleted (gnus-request-expire-articles
6763                          articles gnus-newsgroup-name 'force))
6764       (while articles
6765         (gnus-summary-remove-process-mark (car articles))
6766         ;; The backend might not have been able to delete the article
6767         ;; after all.
6768         (unless (memq (car articles) not-deleted)
6769           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
6770         (setq articles (cdr articles))))
6771     (gnus-summary-position-point)
6772     (gnus-set-mode-line 'summary)
6773     not-deleted))
6774
6775 (defun gnus-summary-edit-article (&optional force)
6776   "Edit the current article.
6777 This will have permanent effect only in mail groups.
6778 If FORCE is non-nil, allow editing of articles even in read-only
6779 groups." 
6780   (interactive "P")
6781   (save-excursion
6782     (set-buffer gnus-summary-buffer)
6783     (gnus-set-global-variables)
6784     (when (and (not force)
6785                (gnus-group-read-only-p))
6786       (error "The current newsgroup does not support article editing."))
6787     ;; Select article if needed.
6788     (unless (eq (gnus-summary-article-number)
6789                 gnus-current-article)
6790       (gnus-summary-select-article t))
6791     (gnus-article-edit-article
6792      `(lambda ()
6793         (gnus-summary-edit-article-done
6794          ,(or (mail-header-references gnus-current-headers) "")
6795          ,(gnus-group-read-only-p) ,gnus-summary-buffer)))))
6796
6797 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
6798
6799 (defun gnus-summary-edit-article-done (&optional references read-only buffer)
6800   "Make edits to the current article permanent."
6801   (interactive)
6802   ;; Replace the article.
6803   (if (and (not read-only)
6804            (not (gnus-request-replace-article
6805                  (cdr gnus-article-current) (car gnus-article-current)
6806                  (current-buffer))))
6807       (error "Couldn't replace article.")
6808     ;; Update the summary buffer.
6809     (if (and references
6810              (equal (message-tokenize-header references " ")
6811                     (message-tokenize-header
6812                      (or (message-fetch-field "references") "") " ")))
6813         ;; We only have to update this line.
6814         (save-excursion
6815           (save-restriction
6816             (message-narrow-to-head)
6817             (let ((header (nnheader-parse-head t)))
6818               (set-buffer buffer)
6819               (mail-header-set-number header (cdr gnus-article-current))
6820               (gnus-summary-update-article-line
6821                (cdr gnus-article-current) header))))
6822       ;; Update threads.
6823       (set-buffer (or buffer gnus-summary-buffer))
6824       (gnus-summary-update-article (cdr gnus-article-current)))
6825     ;; Prettify the article buffer again.
6826     (save-excursion
6827       (set-buffer gnus-article-buffer)
6828       (run-hooks 'gnus-article-display-hook))
6829     ;; Prettify the summary buffer line.
6830     (when (gnus-visual-p 'summary-highlight 'highlight)
6831       (run-hooks 'gnus-visual-mark-article-hook))))
6832
6833 (defun gnus-summary-edit-wash (key)
6834   "Perform editing command in the article buffer."
6835   (interactive 
6836    (list
6837     (progn
6838       (message "%s" (concat (this-command-keys) "- "))
6839       (read-char))))
6840   (message "")
6841   (gnus-summary-edit-article)
6842   (execute-kbd-macro (concat (this-command-keys) key))
6843   (gnus-article-edit-done))
6844
6845 ;;; Respooling
6846
6847 (defun gnus-summary-respool-query ()
6848   "Query where the respool algorithm would put this article."
6849   (interactive)
6850   (gnus-set-global-variables)
6851   (gnus-summary-select-article)
6852   (save-excursion
6853     (set-buffer gnus-article-buffer)
6854     (save-restriction
6855       (goto-char (point-min))
6856       (search-forward "\n\n")
6857       (narrow-to-region (point-min) (point))
6858       (message "This message would go to %s"
6859                (mapconcat 'car (nnmail-article-group 'identity) ", ")))))
6860
6861 ;; Summary marking commands.
6862
6863 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
6864   "Mark articles which has the same subject as read, and then select the next.
6865 If UNMARK is positive, remove any kind of mark.
6866 If UNMARK is negative, tick articles."
6867   (interactive "P")
6868   (gnus-set-global-variables)
6869   (when unmark
6870     (setq unmark (prefix-numeric-value unmark)))
6871   (let ((count
6872          (gnus-summary-mark-same-subject
6873           (gnus-summary-article-subject) unmark)))
6874     ;; Select next unread article.  If auto-select-same mode, should
6875     ;; select the first unread article.
6876     (gnus-summary-next-article t (and gnus-auto-select-same
6877                                       (gnus-summary-article-subject)))
6878     (gnus-message 7 "%d article%s marked as %s"
6879                   count (if (= count 1) " is" "s are")
6880                   (if unmark "unread" "read"))))
6881
6882 (defun gnus-summary-kill-same-subject (&optional unmark)
6883   "Mark articles which has the same subject as read.
6884 If UNMARK is positive, remove any kind of mark.
6885 If UNMARK is negative, tick articles."
6886   (interactive "P")
6887   (gnus-set-global-variables)
6888   (when unmark
6889     (setq unmark (prefix-numeric-value unmark)))
6890   (let ((count
6891          (gnus-summary-mark-same-subject
6892           (gnus-summary-article-subject) unmark)))
6893     ;; If marked as read, go to next unread subject.
6894     (when (null unmark)
6895       ;; Go to next unread subject.
6896       (gnus-summary-next-subject 1 t))
6897     (gnus-message 7 "%d articles are marked as %s"
6898                   count (if unmark "unread" "read"))))
6899
6900 (defun gnus-summary-mark-same-subject (subject &optional unmark)
6901   "Mark articles with same SUBJECT as read, and return marked number.
6902 If optional argument UNMARK is positive, remove any kinds of marks.
6903 If optional argument UNMARK is negative, mark articles as unread instead."
6904   (let ((count 1))
6905     (save-excursion
6906       (cond
6907        ((null unmark)                   ; Mark as read.
6908         (while (and
6909                 (progn
6910                   (gnus-summary-mark-article-as-read gnus-killed-mark)
6911                   (gnus-summary-show-thread) t)
6912                 (gnus-summary-find-subject subject))
6913           (setq count (1+ count))))
6914        ((> unmark 0)                    ; Tick.
6915         (while (and
6916                 (progn
6917                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
6918                   (gnus-summary-show-thread) t)
6919                 (gnus-summary-find-subject subject))
6920           (setq count (1+ count))))
6921        (t                               ; Mark as unread.
6922         (while (and
6923                 (progn
6924                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
6925                   (gnus-summary-show-thread) t)
6926                 (gnus-summary-find-subject subject))
6927           (setq count (1+ count)))))
6928       (gnus-set-mode-line 'summary)
6929       ;; Return the number of marked articles.
6930       count)))
6931
6932 (defun gnus-summary-mark-as-processable (n &optional unmark)
6933   "Set the process mark on the next N articles.
6934 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
6935 the process mark instead.  The difference between N and the actual
6936 number of articles marked is returned."
6937   (interactive "p")
6938   (gnus-set-global-variables)
6939   (let ((backward (< n 0))
6940         (n (abs n)))
6941     (while (and
6942             (> n 0)
6943             (if unmark
6944                 (gnus-summary-remove-process-mark
6945                  (gnus-summary-article-number))
6946               (gnus-summary-set-process-mark (gnus-summary-article-number)))
6947             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
6948       (setq n (1- n)))
6949     (when (/= 0 n)
6950       (gnus-message 7 "No more articles"))
6951     (gnus-summary-recenter)
6952     (gnus-summary-position-point)
6953     n))
6954
6955 (defun gnus-summary-unmark-as-processable (n)
6956   "Remove the process mark from the next N articles.
6957 If N is negative, mark backward instead.  The difference between N and
6958 the actual number of articles marked is returned."
6959   (interactive "p")
6960   (gnus-set-global-variables)
6961   (gnus-summary-mark-as-processable n t))
6962
6963 (defun gnus-summary-unmark-all-processable ()
6964   "Remove the process mark from all articles."
6965   (interactive)
6966   (gnus-set-global-variables)
6967   (save-excursion
6968     (while gnus-newsgroup-processable
6969       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
6970   (gnus-summary-position-point))
6971
6972 (defun gnus-summary-mark-as-expirable (n)
6973   "Mark N articles forward as expirable.
6974 If N is negative, mark backward instead.  The difference between N and
6975 the actual number of articles marked is returned."
6976   (interactive "p")
6977   (gnus-set-global-variables)
6978   (gnus-summary-mark-forward n gnus-expirable-mark))
6979
6980 (defun gnus-summary-mark-article-as-replied (article)
6981   "Mark ARTICLE replied and update the summary line."
6982   (push article gnus-newsgroup-replied)
6983   (let ((buffer-read-only nil))
6984     (when (gnus-summary-goto-subject article)
6985       (gnus-summary-update-secondary-mark article))))
6986
6987 (defun gnus-summary-set-bookmark (article)
6988   "Set a bookmark in current article."
6989   (interactive (list (gnus-summary-article-number)))
6990   (gnus-set-global-variables)
6991   (when (or (not (get-buffer gnus-article-buffer))
6992             (not gnus-current-article)
6993             (not gnus-article-current)
6994             (not (equal gnus-newsgroup-name (car gnus-article-current))))
6995     (error "No current article selected"))
6996   ;; Remove old bookmark, if one exists.
6997   (let ((old (assq article gnus-newsgroup-bookmarks)))
6998     (when old
6999       (setq gnus-newsgroup-bookmarks
7000             (delq old gnus-newsgroup-bookmarks))))
7001   ;; Set the new bookmark, which is on the form
7002   ;; (article-number . line-number-in-body).
7003   (push
7004    (cons article
7005          (save-excursion
7006            (set-buffer gnus-article-buffer)
7007            (count-lines
7008             (min (point)
7009                  (save-excursion
7010                    (goto-char (point-min))
7011                    (search-forward "\n\n" nil t)
7012                    (point)))
7013             (point))))
7014    gnus-newsgroup-bookmarks)
7015   (gnus-message 6 "A bookmark has been added to the current article."))
7016
7017 (defun gnus-summary-remove-bookmark (article)
7018   "Remove the bookmark from the current article."
7019   (interactive (list (gnus-summary-article-number)))
7020   (gnus-set-global-variables)
7021   ;; Remove old bookmark, if one exists.
7022   (let ((old (assq article gnus-newsgroup-bookmarks)))
7023     (if old
7024         (progn
7025           (setq gnus-newsgroup-bookmarks
7026                 (delq old gnus-newsgroup-bookmarks))
7027           (gnus-message 6 "Removed bookmark."))
7028       (gnus-message 6 "No bookmark in current article."))))
7029
7030 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7031 (defun gnus-summary-mark-as-dormant (n)
7032   "Mark N articles forward as dormant.
7033 If N is negative, mark backward instead.  The difference between N and
7034 the actual number of articles marked is returned."
7035   (interactive "p")
7036   (gnus-set-global-variables)
7037   (gnus-summary-mark-forward n gnus-dormant-mark))
7038
7039 (defun gnus-summary-set-process-mark (article)
7040   "Set the process mark on ARTICLE and update the summary line."
7041   (setq gnus-newsgroup-processable
7042         (cons article
7043               (delq article gnus-newsgroup-processable)))
7044   (when (gnus-summary-goto-subject article)
7045     (gnus-summary-show-thread)
7046     (gnus-summary-update-secondary-mark article)))
7047
7048 (defun gnus-summary-remove-process-mark (article)
7049   "Remove the process mark from ARTICLE and update the summary line."
7050   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7051   (when (gnus-summary-goto-subject article)
7052     (gnus-summary-show-thread)
7053     (gnus-summary-update-secondary-mark article)))
7054
7055 (defun gnus-summary-set-saved-mark (article)
7056   "Set the process mark on ARTICLE and update the summary line."
7057   (push article gnus-newsgroup-saved)
7058   (when (gnus-summary-goto-subject article)
7059     (gnus-summary-update-secondary-mark article)))
7060
7061 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7062   "Mark N articles as read forwards.
7063 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7064 The difference between N and the actual number of articles marked is
7065 returned."
7066   (interactive "p")
7067   (gnus-set-global-variables)
7068   (let ((backward (< n 0))
7069         (gnus-summary-goto-unread
7070          (and gnus-summary-goto-unread
7071               (not (eq gnus-summary-goto-unread 'never))
7072               (not (memq mark (list gnus-unread-mark
7073                                     gnus-ticked-mark gnus-dormant-mark)))))
7074         (n (abs n))
7075         (mark (or mark gnus-del-mark)))
7076     (while (and (> n 0)
7077                 (gnus-summary-mark-article nil mark no-expire)
7078                 (zerop (gnus-summary-next-subject
7079                         (if backward -1 1)
7080                         (and gnus-summary-goto-unread
7081                              (not (eq gnus-summary-goto-unread 'never)))
7082                         t)))
7083       (setq n (1- n)))
7084     (when (/= 0 n)
7085       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7086     (gnus-summary-recenter)
7087     (gnus-summary-position-point)
7088     (gnus-set-mode-line 'summary)
7089     n))
7090
7091 (defun gnus-summary-mark-article-as-read (mark)
7092   "Mark the current article quickly as read with MARK."
7093   (let ((article (gnus-summary-article-number)))
7094     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7095     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7096     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7097     (push (cons article mark) gnus-newsgroup-reads)
7098     ;; Possibly remove from cache, if that is used.
7099     (when gnus-use-cache
7100       (gnus-cache-enter-remove-article article))
7101     ;; Allow the backend to change the mark.
7102     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7103     ;; Check for auto-expiry.
7104     (when (and gnus-newsgroup-auto-expire
7105                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7106                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7107                    (= mark gnus-ancient-mark)
7108                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7109                    (= mark gnus-duplicate-mark)))
7110       (setq mark gnus-expirable-mark)
7111       (push article gnus-newsgroup-expirable))
7112     ;; Set the mark in the buffer.
7113     (gnus-summary-update-mark mark 'unread)
7114     t))
7115
7116 (defun gnus-summary-mark-article-as-unread (mark)
7117   "Mark the current article quickly as unread with MARK."
7118   (let ((article (gnus-summary-article-number)))
7119     (if (< article 0)
7120         (gnus-error 1 "Unmarkable article")
7121       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7122       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7123       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7124       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7125       (cond ((= mark gnus-ticked-mark)
7126              (push article gnus-newsgroup-marked))
7127             ((= mark gnus-dormant-mark)
7128              (push article gnus-newsgroup-dormant))
7129             (t
7130              (push article gnus-newsgroup-unreads)))
7131       (setq gnus-newsgroup-reads
7132             (delq (assq article gnus-newsgroup-reads)
7133                   gnus-newsgroup-reads))
7134
7135       ;; See whether the article is to be put in the cache.
7136       (and gnus-use-cache
7137            (vectorp (gnus-summary-article-header article))
7138            (save-excursion
7139              (gnus-cache-possibly-enter-article
7140               gnus-newsgroup-name article
7141               (gnus-summary-article-header article)
7142               (= mark gnus-ticked-mark)
7143               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7144
7145       ;; Fix the mark.
7146       (gnus-summary-update-mark mark 'unread))
7147     t))
7148
7149 (defun gnus-summary-mark-article (&optional article mark no-expire)
7150   "Mark ARTICLE with MARK.  MARK can be any character.
7151 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7152 `??' (dormant) and `?E' (expirable).
7153 If MARK is nil, then the default character `?D' is used.
7154 If ARTICLE is nil, then the article on the current line will be
7155 marked."
7156   ;; The mark might be a string.
7157   (when (stringp mark)
7158     (setq mark (aref mark 0)))
7159   ;; If no mark is given, then we check auto-expiring.
7160   (and (not no-expire)
7161        gnus-newsgroup-auto-expire
7162        (or (not mark)
7163            (and (numberp mark)
7164                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7165                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7166                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7167                     (= mark gnus-duplicate-mark))))
7168        (setq mark gnus-expirable-mark))
7169   (let* ((mark (or mark gnus-del-mark))
7170          (article (or article (gnus-summary-article-number))))
7171     (unless article
7172       (error "No article on current line"))
7173     (if (or (= mark gnus-unread-mark)
7174             (= mark gnus-ticked-mark)
7175             (= mark gnus-dormant-mark))
7176         (gnus-mark-article-as-unread article mark)
7177       (gnus-mark-article-as-read article mark))
7178
7179     ;; See whether the article is to be put in the cache.
7180     (and gnus-use-cache
7181          (not (= mark gnus-canceled-mark))
7182          (vectorp (gnus-summary-article-header article))
7183          (save-excursion
7184            (gnus-cache-possibly-enter-article
7185             gnus-newsgroup-name article
7186             (gnus-summary-article-header article)
7187             (= mark gnus-ticked-mark)
7188             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7189
7190     (when (gnus-summary-goto-subject article nil t)
7191       (let ((buffer-read-only nil))
7192         (gnus-summary-show-thread)
7193         ;; Fix the mark.
7194         (gnus-summary-update-mark mark 'unread)
7195         t))))
7196
7197 (defun gnus-summary-update-secondary-mark (article)
7198   "Update the secondary (read, process, cache) mark."
7199   (gnus-summary-update-mark
7200    (cond ((memq article gnus-newsgroup-processable)
7201           gnus-process-mark)
7202          ((memq article gnus-newsgroup-cached)
7203           gnus-cached-mark)
7204          ((memq article gnus-newsgroup-replied)
7205           gnus-replied-mark)
7206          ((memq article gnus-newsgroup-saved)
7207           gnus-saved-mark)
7208          (t gnus-unread-mark))
7209    'replied)
7210   (when (gnus-visual-p 'summary-highlight 'highlight)
7211     (run-hooks 'gnus-summary-update-hook))
7212   t)
7213
7214 (defun gnus-summary-update-mark (mark type)
7215   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7216         (buffer-read-only nil))
7217     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7218     (when (looking-at "\r")
7219       (incf forward))
7220     (when (and forward
7221                (<= (+ forward (point)) (point-max)))
7222       ;; Go to the right position on the line.
7223       (goto-char (+ forward (point)))
7224       ;; Replace the old mark with the new mark.
7225       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7226       ;; Optionally update the marks by some user rule.
7227       (when (eq type 'unread)
7228         (gnus-data-set-mark
7229          (gnus-data-find (gnus-summary-article-number)) mark)
7230         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7231
7232 (defun gnus-mark-article-as-read (article &optional mark)
7233   "Enter ARTICLE in the pertinent lists and remove it from others."
7234   ;; Make the article expirable.
7235   (let ((mark (or mark gnus-del-mark)))
7236     (if (= mark gnus-expirable-mark)
7237         (push article gnus-newsgroup-expirable)
7238       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7239     ;; Remove from unread and marked lists.
7240     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7241     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7242     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7243     (push (cons article mark) gnus-newsgroup-reads)
7244     ;; Possibly remove from cache, if that is used.
7245     (when gnus-use-cache
7246       (gnus-cache-enter-remove-article article))))
7247
7248 (defun gnus-mark-article-as-unread (article &optional mark)
7249   "Enter ARTICLE in the pertinent lists and remove it from others."
7250   (let ((mark (or mark gnus-ticked-mark)))
7251     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7252           gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7253           gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7254           gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7255
7256     ;; Unsuppress duplicates?
7257     (when gnus-suppress-duplicates
7258       (gnus-dup-unsuppress-article article))
7259
7260     (cond ((= mark gnus-ticked-mark)
7261            (push article gnus-newsgroup-marked))
7262           ((= mark gnus-dormant-mark)
7263            (push article gnus-newsgroup-dormant))
7264           (t
7265            (push article gnus-newsgroup-unreads)))
7266     (setq gnus-newsgroup-reads
7267           (delq (assq article gnus-newsgroup-reads)
7268                 gnus-newsgroup-reads))))
7269
7270 (defalias 'gnus-summary-mark-as-unread-forward
7271   'gnus-summary-tick-article-forward)
7272 (make-obsolete 'gnus-summary-mark-as-unread-forward
7273                'gnus-summary-tick-article-forward)
7274 (defun gnus-summary-tick-article-forward (n)
7275   "Tick N articles forwards.
7276 If N is negative, tick backwards instead.
7277 The difference between N and the number of articles ticked is returned."
7278   (interactive "p")
7279   (gnus-summary-mark-forward n gnus-ticked-mark))
7280
7281 (defalias 'gnus-summary-mark-as-unread-backward
7282   'gnus-summary-tick-article-backward)
7283 (make-obsolete 'gnus-summary-mark-as-unread-backward
7284                'gnus-summary-tick-article-backward)
7285 (defun gnus-summary-tick-article-backward (n)
7286   "Tick N articles backwards.
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 'gnus-summary-tick-article)
7292 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7293 (defun gnus-summary-tick-article (&optional article clear-mark)
7294   "Mark current article as unread.
7295 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7296 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7297   (interactive)
7298   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7299                                        gnus-ticked-mark)))
7300
7301 (defun gnus-summary-mark-as-read-forward (n)
7302   "Mark N articles as read forwards.
7303 If N is negative, mark backwards instead.
7304 The difference between N and the actual number of articles marked is
7305 returned."
7306   (interactive "p")
7307   (gnus-summary-mark-forward n gnus-del-mark t))
7308
7309 (defun gnus-summary-mark-as-read-backward (n)
7310   "Mark the N articles as read backwards.
7311 The difference between N and the actual number of articles marked is
7312 returned."
7313   (interactive "p")
7314   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7315
7316 (defun gnus-summary-mark-as-read (&optional article mark)
7317   "Mark current article as read.
7318 ARTICLE specifies the article to be marked as read.
7319 MARK specifies a string to be inserted at the beginning of the line."
7320   (gnus-summary-mark-article article mark))
7321
7322 (defun gnus-summary-clear-mark-forward (n)
7323   "Clear marks from N articles forward.
7324 If N is negative, clear backward instead.
7325 The difference between N and the number of marks cleared is returned."
7326   (interactive "p")
7327   (gnus-summary-mark-forward n gnus-unread-mark))
7328
7329 (defun gnus-summary-clear-mark-backward (n)
7330   "Clear marks from N articles backward.
7331 The difference between N and the number of marks cleared is returned."
7332   (interactive "p")
7333   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7334
7335 (defun gnus-summary-mark-unread-as-read ()
7336   "Intended to be used by `gnus-summary-mark-article-hook'."
7337   (when (memq gnus-current-article gnus-newsgroup-unreads)
7338     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7339
7340 (defun gnus-summary-mark-read-and-unread-as-read ()
7341   "Intended to be used by `gnus-summary-mark-article-hook'."
7342   (let ((mark (gnus-summary-article-mark)))
7343     (when (or (gnus-unread-mark-p mark)
7344               (gnus-read-mark-p mark))
7345       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7346
7347 (defun gnus-summary-mark-region-as-read (point mark all)
7348   "Mark all unread articles between point and mark as read.
7349 If given a prefix, mark all articles between point and mark as read,
7350 even ticked and dormant ones."
7351   (interactive "r\nP")
7352   (save-excursion
7353     (let (article)
7354       (goto-char point)
7355       (beginning-of-line)
7356       (while (and
7357               (< (point) mark)
7358               (progn
7359                 (when (or all
7360                           (memq (setq article (gnus-summary-article-number))
7361                                 gnus-newsgroup-unreads))
7362                   (gnus-summary-mark-article article gnus-del-mark))
7363                 t)
7364               (gnus-summary-find-next))))))
7365
7366 (defun gnus-summary-mark-below (score mark)
7367   "Mark articles with score less than SCORE with MARK."
7368   (interactive "P\ncMark: ")
7369   (gnus-set-global-variables)
7370   (setq score (if score
7371                   (prefix-numeric-value score)
7372                 (or gnus-summary-default-score 0)))
7373   (save-excursion
7374     (set-buffer gnus-summary-buffer)
7375     (goto-char (point-min))
7376     (while 
7377         (progn
7378           (and (< (gnus-summary-article-score) score)
7379                (gnus-summary-mark-article nil mark))
7380           (gnus-summary-find-next)))))
7381
7382 (defun gnus-summary-kill-below (&optional score)
7383   "Mark articles with score below SCORE as read."
7384   (interactive "P")
7385   (gnus-set-global-variables)
7386   (gnus-summary-mark-below score gnus-killed-mark))
7387
7388 (defun gnus-summary-clear-above (&optional score)
7389   "Clear all marks from articles with score above SCORE."
7390   (interactive "P")
7391   (gnus-set-global-variables)
7392   (gnus-summary-mark-above score gnus-unread-mark))
7393
7394 (defun gnus-summary-tick-above (&optional score)
7395   "Tick all articles with score above SCORE."
7396   (interactive "P")
7397   (gnus-set-global-variables)
7398   (gnus-summary-mark-above score gnus-ticked-mark))
7399
7400 (defun gnus-summary-mark-above (score mark)
7401   "Mark articles with score over SCORE with MARK."
7402   (interactive "P\ncMark: ")
7403   (gnus-set-global-variables)
7404   (setq score (if score
7405                   (prefix-numeric-value score)
7406                 (or gnus-summary-default-score 0)))
7407   (save-excursion
7408     (set-buffer gnus-summary-buffer)
7409     (goto-char (point-min))
7410     (while (and (progn
7411                   (when (> (gnus-summary-article-score) score)
7412                     (gnus-summary-mark-article nil mark))
7413                   t)
7414                 (gnus-summary-find-next)))))
7415
7416 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7417 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7418 (defun gnus-summary-limit-include-expunged (&optional no-error)
7419   "Display all the hidden articles that were expunged for low scores."
7420   (interactive)
7421   (gnus-set-global-variables)
7422   (let ((buffer-read-only nil))
7423     (let ((scored gnus-newsgroup-scored)
7424           headers h)
7425       (while scored
7426         (unless (gnus-summary-goto-subject (caar scored))
7427           (and (setq h (gnus-summary-article-header (caar scored)))
7428                (< (cdar scored) gnus-summary-expunge-below)
7429                (push h headers)))
7430         (setq scored (cdr scored)))
7431       (if (not headers)
7432           (when (not no-error)
7433             (error "No expunged articles hidden."))
7434         (goto-char (point-min))
7435         (gnus-summary-prepare-unthreaded (nreverse headers))
7436         (goto-char (point-min))
7437         (gnus-summary-position-point)
7438         t))))
7439
7440 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7441   "Mark all articles not marked as unread in this newsgroup as read.
7442 If prefix argument ALL is non-nil, all articles are marked as read.
7443 If QUIETLY is non-nil, no questions will be asked.
7444 If TO-HERE is non-nil, it should be a point in the buffer.  All
7445 articles before this point will be marked as read.
7446 The number of articles marked as read is returned."
7447   (interactive "P")
7448   (gnus-set-global-variables)
7449   (prog1
7450       (when (or quietly
7451                 (not gnus-interactive-catchup) ;Without confirmation?
7452                 gnus-expert-user
7453                 (gnus-y-or-n-p
7454                  (if all
7455                      "Mark absolutely all articles as read? "
7456                    "Mark all unread articles as read? ")))
7457         (if (and not-mark
7458                  (not gnus-newsgroup-adaptive)
7459                  (not gnus-newsgroup-auto-expire)
7460                  (not gnus-suppress-duplicates))
7461             (progn
7462               (when all
7463                 (setq gnus-newsgroup-marked nil
7464                       gnus-newsgroup-dormant nil))
7465               (setq gnus-newsgroup-unreads nil))
7466           ;; We actually mark all articles as canceled, which we
7467           ;; have to do when using auto-expiry or adaptive scoring.
7468           (gnus-summary-show-all-threads)
7469           (when (gnus-summary-first-subject (not all))
7470             (while (and
7471                     (if to-here (< (point) to-here) t)
7472                     (gnus-summary-mark-article-as-read gnus-catchup-mark)
7473                     (gnus-summary-find-next (not all)))))
7474           (unless to-here
7475             (setq gnus-newsgroup-unreads nil))
7476           (gnus-set-mode-line 'summary)))
7477     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
7478       (when (and (not to-here) (eq 'nnvirtual (car method)))
7479         (nnvirtual-catchup-group
7480          (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
7481     (gnus-summary-position-point)))
7482
7483 (defun gnus-summary-catchup-to-here (&optional all)
7484   "Mark all unticked articles before the current one as read.
7485 If ALL is non-nil, also mark ticked and dormant articles as read."
7486   (interactive "P")
7487   (gnus-set-global-variables)
7488   (save-excursion
7489     (gnus-save-hidden-threads
7490       (let ((beg (point)))
7491         ;; We check that there are unread articles.
7492         (when (or all (gnus-summary-find-prev))
7493           (gnus-summary-catchup all t beg)))))
7494   (gnus-summary-position-point))
7495
7496 (defun gnus-summary-catchup-all (&optional quietly)
7497   "Mark all articles in this newsgroup as read."
7498   (interactive "P")
7499   (gnus-set-global-variables)
7500   (gnus-summary-catchup t quietly))
7501
7502 (defun gnus-summary-catchup-and-exit (&optional all quietly)
7503   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7504 If prefix argument ALL is non-nil, all articles are marked as read."
7505   (interactive "P")
7506   (gnus-set-global-variables)
7507   (gnus-summary-catchup all quietly nil 'fast)
7508   ;; Select next newsgroup or exit.
7509   (if (eq gnus-auto-select-next 'quietly)
7510       (gnus-summary-next-group nil)
7511     (gnus-summary-exit)))
7512
7513 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
7514   "Mark all articles in this newsgroup as read, and then exit."
7515   (interactive "P")
7516   (gnus-set-global-variables)
7517   (gnus-summary-catchup-and-exit t quietly))
7518
7519 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
7520 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
7521   "Mark all articles in this group as read and select the next group.
7522 If given a prefix, mark all articles, unread as well as ticked, as
7523 read."
7524   (interactive "P")
7525   (gnus-set-global-variables)
7526   (save-excursion
7527     (gnus-summary-catchup all))
7528   (gnus-summary-next-article t nil nil t))
7529
7530 ;; Thread-based commands.
7531
7532 (defun gnus-summary-articles-in-thread (&optional article)
7533   "Return a list of all articles in the current thread.
7534 If ARTICLE is non-nil, return all articles in the thread that starts
7535 with that article."
7536   (let* ((article (or article (gnus-summary-article-number)))
7537          (data (gnus-data-find-list article))
7538          (top-level (gnus-data-level (car data)))
7539          (top-subject
7540           (cond ((null gnus-thread-operation-ignore-subject)
7541                  (gnus-simplify-subject-re
7542                   (mail-header-subject (gnus-data-header (car data)))))
7543                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
7544                  (gnus-simplify-subject-fuzzy
7545                   (mail-header-subject (gnus-data-header (car data)))))
7546                 (t nil)))
7547          (end-point (save-excursion
7548                       (if (gnus-summary-go-to-next-thread)
7549                           (point) (point-max))))
7550          articles)
7551     (while (and data
7552                 (< (gnus-data-pos (car data)) end-point))
7553       (when (or (not top-subject)
7554                 (string= top-subject
7555                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
7556                              (gnus-simplify-subject-fuzzy
7557                               (mail-header-subject
7558                                (gnus-data-header (car data))))
7559                            (gnus-simplify-subject-re
7560                             (mail-header-subject
7561                              (gnus-data-header (car data)))))))
7562         (push (gnus-data-number (car data)) articles))
7563       (unless (and (setq data (cdr data))
7564                    (> (gnus-data-level (car data)) top-level))
7565         (setq data nil)))
7566     ;; Return the list of articles.
7567     (nreverse articles)))
7568
7569 (defun gnus-summary-rethread-current ()
7570   "Rethread the thread the current article is part of."
7571   (interactive)
7572   (gnus-set-global-variables)
7573   (let* ((gnus-show-threads t)
7574          (article (gnus-summary-article-number))
7575          (id (mail-header-id (gnus-summary-article-header)))
7576          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
7577     (unless id
7578       (error "No article on the current line"))
7579     (gnus-rebuild-thread id)
7580     (gnus-summary-goto-subject article)))
7581
7582 (defun gnus-summary-reparent-thread ()
7583   "Make current article child of the marked (or previous) article.
7584
7585 Note that the re-threading will only work if `gnus-thread-ignore-subject'
7586 is non-nil or the Subject: of both articles are the same."
7587   (interactive)
7588   (unless (not (gnus-group-read-only-p))
7589     (error "The current newsgroup does not support article editing."))
7590   (unless (<= (length gnus-newsgroup-processable) 1)
7591     (error "No more than one article may be marked."))
7592   (save-window-excursion
7593     (let ((gnus-article-buffer " *reparent*")
7594           (current-article (gnus-summary-article-number))
7595                                         ; first grab the marked article, otherwise one line up.
7596           (parent-article (if (not (null gnus-newsgroup-processable))
7597                               (car gnus-newsgroup-processable)
7598                             (save-excursion
7599                               (if (eq (forward-line -1) 0)
7600                                   (gnus-summary-article-number)
7601                                 (error "Beginning of summary buffer."))))))
7602       (unless (not (eq current-article parent-article))
7603         (error "An article may not be self-referential."))
7604       (let ((message-id (mail-header-id 
7605                          (gnus-summary-article-header parent-article))))
7606         (unless (and message-id (not (equal message-id "")))
7607           (error "No message-id in desired parent."))
7608         (gnus-summary-select-article t t nil current-article)
7609         (set-buffer gnus-article-buffer)
7610         (setq buffer-read-only nil)
7611         (let ((buf (format "%s" (buffer-string))))
7612           (erase-buffer)
7613           (insert buf))
7614         (goto-char (point-min))
7615         (if (search-forward-regexp "^References: " nil t)
7616             (insert message-id " " )
7617           (insert "References: " message-id "\n"))
7618         (unless (gnus-request-replace-article current-article
7619                                               (car gnus-article-current)
7620                                               gnus-article-buffer)
7621           (error "Couldn't replace article."))
7622         (set-buffer gnus-summary-buffer)
7623         (gnus-summary-unmark-all-processable)
7624         (gnus-summary-rethread-current)
7625         (gnus-message 3 "Article %d is now the child of article %d."
7626                       current-article parent-article)))))
7627
7628 (defun gnus-summary-toggle-threads (&optional arg)
7629   "Toggle showing conversation threads.
7630 If ARG is positive number, turn showing conversation threads on."
7631   (interactive "P")
7632   (gnus-set-global-variables)
7633   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
7634     (setq gnus-show-threads
7635           (if (null arg) (not gnus-show-threads)
7636             (> (prefix-numeric-value arg) 0)))
7637     (gnus-summary-prepare)
7638     (gnus-summary-goto-subject current)
7639     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
7640     (gnus-summary-position-point)))
7641
7642 (defun gnus-summary-show-all-threads ()
7643   "Show all threads."
7644   (interactive)
7645   (gnus-set-global-variables)
7646   (save-excursion
7647     (let ((buffer-read-only nil))
7648       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
7649   (gnus-summary-position-point))
7650
7651 (defun gnus-summary-show-thread ()
7652   "Show thread subtrees.
7653 Returns nil if no thread was there to be shown."
7654   (interactive)
7655   (gnus-set-global-variables)
7656   (let ((buffer-read-only nil)
7657         (orig (point))
7658         ;; first goto end then to beg, to have point at beg after let
7659         (end (progn (end-of-line) (point)))
7660         (beg (progn (beginning-of-line) (point))))
7661     (prog1
7662         ;; Any hidden lines here?
7663         (search-forward "\r" end t)
7664       (subst-char-in-region beg end ?\^M ?\n t)
7665       (goto-char orig)
7666       (gnus-summary-position-point))))
7667
7668 (defun gnus-summary-hide-all-threads ()
7669   "Hide all thread subtrees."
7670   (interactive)
7671   (gnus-set-global-variables)
7672   (save-excursion
7673     (goto-char (point-min))
7674     (gnus-summary-hide-thread)
7675     (while (zerop (gnus-summary-next-thread 1 t))
7676       (gnus-summary-hide-thread)))
7677   (gnus-summary-position-point))
7678
7679 (defun gnus-summary-hide-thread ()
7680   "Hide thread subtrees.
7681 Returns nil if no threads were there to be hidden."
7682   (interactive)
7683   (gnus-set-global-variables)
7684   (let ((buffer-read-only nil)
7685         (start (point))
7686         (article (gnus-summary-article-number)))
7687     (goto-char start)
7688     ;; Go forward until either the buffer ends or the subthread
7689     ;; ends.
7690     (when (and (not (eobp))
7691                (or (zerop (gnus-summary-next-thread 1 t))
7692                    (goto-char (point-max))))
7693       (prog1
7694           (if (and (> (point) start)
7695                    (search-backward "\n" start t))
7696               (progn
7697                 (subst-char-in-region start (point) ?\n ?\^M)
7698                 (gnus-summary-goto-subject article))
7699             (goto-char start)
7700             nil)
7701         ;;(gnus-summary-position-point)
7702         ))))
7703
7704 (defun gnus-summary-go-to-next-thread (&optional previous)
7705   "Go to the same level (or less) next thread.
7706 If PREVIOUS is non-nil, go to previous thread instead.
7707 Return the article number moved to, or nil if moving was impossible."
7708   (let ((level (gnus-summary-thread-level))
7709         (way (if previous -1 1))
7710         (beg (point)))
7711     (forward-line way)
7712     (while (and (not (eobp))
7713                 (< level (gnus-summary-thread-level)))
7714       (forward-line way))
7715     (if (eobp)
7716         (progn
7717           (goto-char beg)
7718           nil)
7719       (setq beg (point))
7720       (prog1
7721           (gnus-summary-article-number)
7722         (goto-char beg)))))
7723
7724 (defun gnus-summary-next-thread (n &optional silent)
7725   "Go to the same level next N'th thread.
7726 If N is negative, search backward instead.
7727 Returns the difference between N and the number of skips actually
7728 done.
7729
7730 If SILENT, don't output messages."
7731   (interactive "p")
7732   (gnus-set-global-variables)
7733   (let ((backward (< n 0))
7734         (n (abs n)))
7735     (while (and (> n 0)
7736                 (gnus-summary-go-to-next-thread backward))
7737       (decf n))
7738     (unless silent 
7739       (gnus-summary-position-point))
7740     (when (and (not silent) (/= 0 n))
7741       (gnus-message 7 "No more threads"))
7742     n))
7743
7744 (defun gnus-summary-prev-thread (n)
7745   "Go to the same level previous N'th thread.
7746 Returns the difference between N and the number of skips actually
7747 done."
7748   (interactive "p")
7749   (gnus-set-global-variables)
7750   (gnus-summary-next-thread (- n)))
7751
7752 (defun gnus-summary-go-down-thread ()
7753   "Go down one level in the current thread."
7754   (let ((children (gnus-summary-article-children)))
7755     (when children
7756       (gnus-summary-goto-subject (car children)))))
7757
7758 (defun gnus-summary-go-up-thread ()
7759   "Go up one level in the current thread."
7760   (let ((parent (gnus-summary-article-parent)))
7761     (when parent
7762       (gnus-summary-goto-subject parent))))
7763
7764 (defun gnus-summary-down-thread (n)
7765   "Go down thread N steps.
7766 If N is negative, go up instead.
7767 Returns the difference between N and how many steps down that were
7768 taken."
7769   (interactive "p")
7770   (gnus-set-global-variables)
7771   (let ((up (< n 0))
7772         (n (abs n)))
7773     (while (and (> n 0)
7774                 (if up (gnus-summary-go-up-thread)
7775                   (gnus-summary-go-down-thread)))
7776       (setq n (1- n)))
7777     (gnus-summary-position-point)
7778     (when (/= 0 n)
7779       (gnus-message 7 "Can't go further"))
7780     n))
7781
7782 (defun gnus-summary-up-thread (n)
7783   "Go up thread N steps.
7784 If N is negative, go up instead.
7785 Returns the difference between N and how many steps down that were
7786 taken."
7787   (interactive "p")
7788   (gnus-set-global-variables)
7789   (gnus-summary-down-thread (- n)))
7790
7791 (defun gnus-summary-top-thread ()
7792   "Go to the top of the thread."
7793   (interactive)
7794   (gnus-set-global-variables)
7795   (while (gnus-summary-go-up-thread))
7796   (gnus-summary-article-number))
7797
7798 (defun gnus-summary-kill-thread (&optional unmark)
7799   "Mark articles under current thread as read.
7800 If the prefix argument is positive, remove any kinds of marks.
7801 If the prefix argument is negative, tick articles instead."
7802   (interactive "P")
7803   (gnus-set-global-variables)
7804   (when unmark
7805     (setq unmark (prefix-numeric-value unmark)))
7806   (let ((articles (gnus-summary-articles-in-thread)))
7807     (save-excursion
7808       ;; Expand the thread.
7809       (gnus-summary-show-thread)
7810       ;; Mark all the articles.
7811       (while articles
7812         (gnus-summary-goto-subject (car articles))
7813         (cond ((null unmark)
7814                (gnus-summary-mark-article-as-read gnus-killed-mark))
7815               ((> unmark 0)
7816                (gnus-summary-mark-article-as-unread gnus-unread-mark))
7817               (t
7818                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
7819         (setq articles (cdr articles))))
7820     ;; Hide killed subtrees.
7821     (and (null unmark)
7822          gnus-thread-hide-killed
7823          (gnus-summary-hide-thread))
7824     ;; If marked as read, go to next unread subject.
7825     (when (null unmark)
7826       ;; Go to next unread subject.
7827       (gnus-summary-next-subject 1 t)))
7828   (gnus-set-mode-line 'summary))
7829
7830 ;; Summary sorting commands
7831
7832 (defun gnus-summary-sort-by-number (&optional reverse)
7833   "Sort summary buffer by article number.
7834 Argument REVERSE means reverse order."
7835   (interactive "P")
7836   (gnus-summary-sort 'number reverse))
7837
7838 (defun gnus-summary-sort-by-author (&optional reverse)
7839   "Sort summary buffer by author name alphabetically.
7840 If case-fold-search is non-nil, case of letters is ignored.
7841 Argument REVERSE means reverse order."
7842   (interactive "P")
7843   (gnus-summary-sort 'author reverse))
7844
7845 (defun gnus-summary-sort-by-subject (&optional reverse)
7846   "Sort summary buffer by subject alphabetically.  `Re:'s are ignored.
7847 If case-fold-search is non-nil, case of letters is ignored.
7848 Argument REVERSE means reverse order."
7849   (interactive "P")
7850   (gnus-summary-sort 'subject reverse))
7851
7852 (defun gnus-summary-sort-by-date (&optional reverse)
7853   "Sort summary buffer by date.
7854 Argument REVERSE means reverse order."
7855   (interactive "P")
7856   (gnus-summary-sort 'date reverse))
7857
7858 (defun gnus-summary-sort-by-score (&optional reverse)
7859   "Sort summary buffer by score.
7860 Argument REVERSE means reverse order."
7861   (interactive "P")
7862   (gnus-summary-sort 'score reverse))
7863
7864 (defun gnus-summary-sort (predicate reverse)
7865   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
7866   (gnus-set-global-variables)
7867   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
7868          (article (intern (format "gnus-article-sort-by-%s" predicate)))
7869          (gnus-thread-sort-functions
7870           (list
7871            (if (not reverse)
7872                thread
7873              `(lambda (t1 t2)
7874                 (,thread t2 t1)))))
7875          (gnus-article-sort-functions
7876           (list
7877            (if (not reverse)
7878                article
7879              `(lambda (t1 t2)
7880                 (,article t2 t1)))))
7881          (buffer-read-only)
7882          (gnus-summary-prepare-hook nil))
7883     ;; We do the sorting by regenerating the threads.
7884     (gnus-summary-prepare)
7885     ;; Hide subthreads if needed.
7886     (when (and gnus-show-threads gnus-thread-hide-subtree)
7887       (gnus-summary-hide-all-threads))))
7888
7889 ;; Summary saving commands.
7890
7891 (defun gnus-summary-save-article (&optional n not-saved)
7892   "Save the current article using the default saver function.
7893 If N is a positive number, save the N next articles.
7894 If N is a negative number, save the N previous articles.
7895 If N is nil and any articles have been marked with the process mark,
7896 save those articles instead.
7897 The variable `gnus-default-article-saver' specifies the saver function."
7898   (interactive "P")
7899   (gnus-set-global-variables)
7900   (let* ((articles (gnus-summary-work-articles n))
7901          (save-buffer (save-excursion 
7902                         (nnheader-set-temp-buffer " *Gnus Save*")))
7903          (num (length articles))
7904          header article file)
7905     (while articles
7906       (setq header (gnus-summary-article-header
7907                     (setq article (pop articles))))
7908       (if (not (vectorp header))
7909           ;; This is a pseudo-article.
7910           (if (assq 'name header)
7911               (gnus-copy-file (cdr (assq 'name header)))
7912             (gnus-message 1 "Article %d is unsaveable" article))
7913         ;; This is a real article.
7914         (save-window-excursion
7915           (gnus-summary-select-article t nil nil article))
7916         (save-excursion
7917           (set-buffer save-buffer)
7918           (erase-buffer)
7919           (insert-buffer-substring gnus-original-article-buffer))
7920         (setq file (gnus-article-save save-buffer file num))
7921         (gnus-summary-remove-process-mark article)
7922         (unless not-saved
7923           (gnus-summary-set-saved-mark article))))
7924     (gnus-kill-buffer save-buffer)
7925     (gnus-summary-position-point)
7926     n))
7927
7928 (defun gnus-summary-pipe-output (&optional arg)
7929   "Pipe the current article to a subprocess.
7930 If N is a positive number, pipe the N next articles.
7931 If N is a negative number, pipe the N previous articles.
7932 If N is nil and any articles have been marked with the process mark,
7933 pipe those articles instead."
7934   (interactive "P")
7935   (gnus-set-global-variables)
7936   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
7937     (gnus-summary-save-article arg t))
7938   (gnus-configure-windows 'pipe))
7939
7940 (defun gnus-summary-save-article-mail (&optional arg)
7941   "Append the current article to an mail file.
7942 If N is a positive number, save the N next articles.
7943 If N is a negative number, save the N previous articles.
7944 If N is nil and any articles have been marked with the process mark,
7945 save those articles instead."
7946   (interactive "P")
7947   (gnus-set-global-variables)
7948   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
7949     (gnus-summary-save-article arg)))
7950
7951 (defun gnus-summary-save-article-rmail (&optional arg)
7952   "Append the current article to an rmail file.
7953 If N is a positive number, save the N next articles.
7954 If N is a negative number, save the N previous articles.
7955 If N is nil and any articles have been marked with the process mark,
7956 save those articles instead."
7957   (interactive "P")
7958   (gnus-set-global-variables)
7959   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
7960     (gnus-summary-save-article arg)))
7961
7962 (defun gnus-summary-save-article-file (&optional arg)
7963   "Append the current article to a file.
7964 If N is a positive number, save the N next articles.
7965 If N is a negative number, save the N previous articles.
7966 If N is nil and any articles have been marked with the process mark,
7967 save those articles instead."
7968   (interactive "P")
7969   (gnus-set-global-variables)
7970   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
7971     (gnus-summary-save-article arg)))
7972
7973 (defun gnus-summary-save-article-body-file (&optional arg)
7974   "Append the current article body to a file.
7975 If N is a positive number, save the N next articles.
7976 If N is a negative number, save the N previous articles.
7977 If N is nil and any articles have been marked with the process mark,
7978 save those articles instead."
7979   (interactive "P")
7980   (gnus-set-global-variables)
7981   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
7982     (gnus-summary-save-article arg)))
7983
7984 (defun gnus-get-split-value (methods)
7985   "Return a value based on the split METHODS."
7986   (let (split-name method result match)
7987     (when methods
7988       (save-excursion
7989         (set-buffer gnus-original-article-buffer)
7990         (save-restriction
7991           (nnheader-narrow-to-headers)
7992           (while methods
7993             (goto-char (point-min))
7994             (setq method (pop methods))
7995             (setq match (car method))
7996             (when (cond
7997                    ((stringp match)
7998                     ;; Regular expression.
7999                     (condition-case ()
8000                         (re-search-forward match nil t)
8001                       (error nil)))
8002                    ((gnus-functionp match)
8003                     ;; Function.
8004                     (save-restriction
8005                       (widen)
8006                       (setq result (funcall match gnus-newsgroup-name))))
8007                    ((consp match)
8008                     ;; Form.
8009                     (save-restriction
8010                       (widen)
8011                       (setq result (eval match)))))
8012               (setq split-name (append (cdr method) split-name))
8013               (cond ((stringp result)
8014                      (push result split-name))
8015                     ((consp result)
8016                      (setq split-name (append result split-name)))))))))
8017     split-name))
8018
8019 (defun gnus-read-move-group-name (prompt default articles prefix)
8020   "Read a group name."
8021   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8022          (minibuffer-confirm-incomplete nil) ; XEmacs
8023          group-map
8024          (dum (mapatoms
8025                (lambda (g)
8026                  (and (boundp g)
8027                       (symbol-name g)
8028                       (memq 'respool
8029                             (assoc (symbol-name
8030                                     (car (gnus-find-method-for-group
8031                                           (symbol-name g))))
8032                                    gnus-valid-select-methods))
8033                       (push (list (symbol-name g)) group-map)))
8034                gnus-active-hashtb))
8035          (prom
8036           (format "%s %s to:"
8037                   prompt
8038                   (if (> (length articles) 1)
8039                       (format "these %d articles" (length articles))
8040                     "this article")))
8041          (to-newsgroup
8042           (cond
8043            ((null split-name)
8044             (gnus-completing-read default prom
8045                                   group-map nil nil prefix
8046                                   'gnus-group-history))
8047            ((= 1 (length split-name))
8048             (gnus-completing-read (car split-name) prom group-map
8049                                   nil nil nil
8050                                   'gnus-group-history))
8051            (t
8052             (gnus-completing-read nil prom 
8053                                   (mapcar (lambda (el) (list el))
8054                                           (nreverse split-name))
8055                                   nil nil nil
8056                                   'gnus-group-history)))))
8057     (when to-newsgroup
8058       (when (or (string= to-newsgroup "")
8059                 (string= to-newsgroup prefix))
8060         (setq to-newsgroup (or default "")))
8061       (or (gnus-active to-newsgroup)
8062           (gnus-activate-group to-newsgroup)
8063           (when (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8064                                        to-newsgroup))
8065             (or (and (gnus-request-create-group 
8066                       to-newsgroup (gnus-group-name-to-method to-newsgroup))
8067                      (gnus-activate-group to-newsgroup nil nil
8068                                           (gnus-group-name-to-method
8069                                            to-newsgroup)))
8070                 (error "Couldn't create group %s" to-newsgroup)))
8071           (error "No such group: %s" to-newsgroup)))
8072     to-newsgroup))
8073
8074 ;; Summary extract commands
8075
8076 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8077   (let ((buffer-read-only nil)
8078         (article (gnus-summary-article-number))
8079         after-article b e)
8080     (unless (gnus-summary-goto-subject article)
8081       (error "No such article: %d" article))
8082     (gnus-summary-position-point)
8083     ;; If all commands are to be bunched up on one line, we collect
8084     ;; them here.
8085     (if gnus-view-pseudos-separately
8086         ()
8087       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8088             files action)
8089         (while ps
8090           (setq action (cdr (assq 'action (car ps))))
8091           (setq files (list (cdr (assq 'name (car ps)))))
8092           (while (and ps (cdr ps)
8093                       (string= (or action "1")
8094                                (or (cdr (assq 'action (cadr ps))) "2")))
8095             (push (cdr (assq 'name (cadr ps))) files)
8096             (setcdr ps (cddr ps)))
8097           (if (not files)
8098               ()
8099             (when (not (string-match "%s" action))
8100               (push " " files))
8101             (push " " files)
8102             (and (assq 'execute (car ps))
8103                  (setcdr (assq 'execute (car ps))
8104                          (funcall (if (string-match "%s" action)
8105                                       'format 'concat)
8106                                   action
8107                                   (mapconcat (lambda (f) f) files " ")))))
8108           (setq ps (cdr ps)))))
8109     (if (and gnus-view-pseudos (not not-view))
8110         (while pslist
8111           (when (assq 'execute (car pslist))
8112             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8113                                   (eq gnus-view-pseudos 'not-confirm)))
8114           (setq pslist (cdr pslist)))
8115       (save-excursion
8116         (while pslist
8117           (setq after-article (or (cdr (assq 'article (car pslist)))
8118                                   (gnus-summary-article-number)))
8119           (gnus-summary-goto-subject after-article)
8120           (forward-line 1)
8121           (setq b (point))
8122           (insert "    " (file-name-nondirectory
8123                           (cdr (assq 'name (car pslist))))
8124                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8125           (setq e (point))
8126           (forward-line -1)             ; back to `b'
8127           (gnus-add-text-properties
8128            b (1- e) (list 'gnus-number gnus-reffed-article-number
8129                           gnus-mouse-face-prop gnus-mouse-face))
8130           (gnus-data-enter
8131            after-article gnus-reffed-article-number
8132            gnus-unread-mark b (car pslist) 0 (- e b))
8133           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8134           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8135           (setq pslist (cdr pslist)))))))
8136
8137 (defun gnus-pseudos< (p1 p2)
8138   (let ((c1 (cdr (assq 'action p1)))
8139         (c2 (cdr (assq 'action p2))))
8140     (and c1 c2 (string< c1 c2))))
8141
8142 (defun gnus-request-pseudo-article (props)
8143   (cond ((assq 'execute props)
8144          (gnus-execute-command (cdr (assq 'execute props)))))
8145   (let ((gnus-current-article (gnus-summary-article-number)))
8146     (run-hooks 'gnus-mark-article-hook)))
8147
8148 (defun gnus-execute-command (command &optional automatic)
8149   (save-excursion
8150     (gnus-article-setup-buffer)
8151     (set-buffer gnus-article-buffer)
8152     (setq buffer-read-only nil)
8153     (let ((command (if automatic command (read-string "Command: " command)))
8154           ;; Just binding this here doesn't help, because there might
8155           ;; be output from the process after exiting the scope of 
8156           ;; this `let'.
8157           ;; (buffer-read-only nil)
8158           )
8159       (erase-buffer)
8160       (insert "$ " command "\n\n")
8161       (if gnus-view-pseudo-asynchronously
8162           (start-process "gnus-execute" nil shell-file-name
8163                          shell-command-switch command)
8164         (call-process shell-file-name nil t nil
8165                       shell-command-switch command)))))
8166
8167 ;; Summary kill commands.
8168
8169 (defun gnus-summary-edit-global-kill (article)
8170   "Edit the \"global\" kill file."
8171   (interactive (list (gnus-summary-article-number)))
8172   (gnus-set-global-variables)
8173   (gnus-group-edit-global-kill article))
8174
8175 (defun gnus-summary-edit-local-kill ()
8176   "Edit a local kill file applied to the current newsgroup."
8177   (interactive)
8178   (gnus-set-global-variables)
8179   (setq gnus-current-headers (gnus-summary-article-header))
8180   (gnus-set-global-variables)
8181   (gnus-group-edit-local-kill
8182    (gnus-summary-article-number) gnus-newsgroup-name))
8183
8184 ;;; Header reading.
8185
8186 (defun gnus-read-header (id &optional header)
8187   "Read the headers of article ID and enter them into the Gnus system."
8188   (let ((group gnus-newsgroup-name)
8189         (gnus-override-method 
8190          (and (gnus-news-group-p gnus-newsgroup-name)
8191               gnus-refer-article-method))
8192         where)
8193     ;; First we check to see whether the header in question is already
8194     ;; fetched.
8195     (if (stringp id)
8196         ;; This is a Message-ID.
8197         (setq header (or header (gnus-id-to-header id)))
8198       ;; This is an article number.
8199       (setq header (or header (gnus-summary-article-header id))))
8200     (if (and header
8201              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
8202         ;; We have found the header.
8203         header
8204       ;; We have to really fetch the header to this article.
8205       (save-excursion
8206         (set-buffer nntp-server-buffer)
8207         (when (setq where (gnus-request-head id group))
8208           (goto-char (point-max))
8209           (insert ".\n")
8210           (goto-char (point-min))
8211           (insert "211 ")
8212           (princ (cond
8213                   ((numberp id) id)
8214                   ((cdr where) (cdr where))
8215                   (header (mail-header-number header))
8216                   (t gnus-reffed-article-number))
8217                  (current-buffer))
8218           (insert " Article retrieved.\n"))
8219         (if (not (setq header (car (gnus-get-newsgroup-headers nil t))))
8220             ()                          ; Malformed head.
8221           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
8222             (when (and (stringp id)
8223                        (not (string= (gnus-group-real-name group)
8224                                      (car where))))
8225               ;; If we fetched by Message-ID and the article came
8226               ;; from a different group, we fudge some bogus article
8227               ;; numbers for this article.
8228               (mail-header-set-number header gnus-reffed-article-number))
8229             (save-excursion
8230               (set-buffer gnus-summary-buffer)
8231               (decf gnus-reffed-article-number)
8232               (gnus-remove-header (mail-header-number header))
8233               (push header gnus-newsgroup-headers)
8234               (setq gnus-current-headers header)
8235               (push (mail-header-number header) gnus-newsgroup-limit)))
8236           header)))))
8237
8238 (defun gnus-remove-header (number)
8239   "Remove header NUMBER from `gnus-newsgroup-headers'."
8240   (if (and gnus-newsgroup-headers
8241            (= number (mail-header-number (car gnus-newsgroup-headers))))
8242       (pop gnus-newsgroup-headers)
8243     (let ((headers gnus-newsgroup-headers))
8244       (while (and (cdr headers)
8245                   (not (= number (mail-header-number (cadr headers)))))
8246         (pop headers))
8247       (when (cdr headers)
8248         (setcdr headers (cddr headers))))))
8249
8250 ;;;
8251 ;;; summary highlights
8252 ;;;
8253
8254 (defun gnus-highlight-selected-summary ()
8255   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8256   ;; Highlight selected article in summary buffer
8257   (when gnus-summary-selected-face
8258     (save-excursion
8259       (let* ((beg (progn (beginning-of-line) (point)))
8260              (end (progn (end-of-line) (point)))
8261              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8262              (from (if (get-text-property beg gnus-mouse-face-prop)
8263                        beg
8264                      (or (next-single-property-change 
8265                           beg gnus-mouse-face-prop nil end)
8266                          beg)))
8267              (to
8268               (if (= from end)
8269                   (- from 2)
8270                 (or (next-single-property-change
8271                      from gnus-mouse-face-prop nil end)
8272                     end))))
8273         ;; If no mouse-face prop on line we will have to = from = end,
8274         ;; so we highlight the entire line instead.
8275         (when (= (+ to 2) from)
8276           (setq from beg)
8277           (setq to end))
8278         (if gnus-newsgroup-selected-overlay
8279             ;; Move old overlay.
8280             (gnus-move-overlay
8281              gnus-newsgroup-selected-overlay from to (current-buffer))
8282           ;; Create new overlay.
8283           (gnus-overlay-put
8284            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8285            'face gnus-summary-selected-face))))))
8286
8287 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8288 (defun gnus-summary-highlight-line ()
8289   "Highlight current line according to `gnus-summary-highlight'."
8290   (let* ((list gnus-summary-highlight)
8291          (p (point))
8292          (end (progn (end-of-line) (point)))
8293          ;; now find out where the line starts and leave point there.
8294          (beg (progn (beginning-of-line) (point)))
8295          (article (gnus-summary-article-number))
8296          (score (or (cdr (assq (or article gnus-current-article)
8297                                gnus-newsgroup-scored))
8298                     gnus-summary-default-score 0))
8299          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8300          (inhibit-read-only t))
8301     ;; Eval the cars of the lists until we find a match.
8302     (let ((default gnus-summary-default-score))
8303       (while (and list
8304                   (not (eval (caar list))))
8305         (setq list (cdr list))))
8306     (let ((face (cdar list)))
8307       (unless (eq face (get-text-property beg 'face))
8308         (gnus-put-text-property 
8309          beg end 'face 
8310          (setq face (if (boundp face) (symbol-value face) face)))
8311         (when gnus-summary-highlight-line-function
8312           (funcall gnus-summary-highlight-line-function article face))))
8313     (goto-char p)))
8314
8315 (defun gnus-update-read-articles (group unread)
8316   "Update the list of read articles in GROUP."
8317   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8318          (entry (gnus-gethash group gnus-newsrc-hashtb))
8319          (info (nth 2 entry))
8320          (prev 1)
8321          (unread (sort (copy-sequence unread) '<))
8322          read)
8323     (if (or (not info) (not active))
8324         ;; There is no info on this group if it was, in fact,
8325         ;; killed.  Gnus stores no information on killed groups, so
8326         ;; there's nothing to be done.
8327         ;; One could store the information somewhere temporarily,
8328         ;; perhaps...  Hmmm...
8329         ()
8330       ;; Remove any negative articles numbers.
8331       (while (and unread (< (car unread) 0))
8332         (setq unread (cdr unread)))
8333       ;; Remove any expired article numbers
8334       (while (and unread (< (car unread) (car active)))
8335         (setq unread (cdr unread)))
8336       ;; Compute the ranges of read articles by looking at the list of
8337       ;; unread articles.
8338       (while unread
8339         (when (/= (car unread) prev)
8340           (push (if (= prev (1- (car unread))) prev
8341                   (cons prev (1- (car unread))))
8342                 read))
8343         (setq prev (1+ (car unread)))
8344         (setq unread (cdr unread)))
8345       (when (<= prev (cdr active))
8346         (push (cons prev (cdr active)) read))
8347       (gnus-undo-register
8348         `(progn
8349            (gnus-info-set-marks ,info ,(gnus-info-marks info))
8350            (gnus-info-set-read ,info ,(gnus-info-read info))
8351            (gnus-get-unread-articles-in-group ,info (gnus-active ,group))))
8352       ;; Enter this list into the group info.
8353       (gnus-info-set-read
8354        info (if (> (length read) 1) (nreverse read) read))
8355       ;; Set the number of unread articles in gnus-newsrc-hashtb.
8356       (gnus-get-unread-articles-in-group info (gnus-active group))
8357       t)))
8358
8359 (provide 'gnus-sum)
8360
8361 ;;; gnus-sum.el ends here