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