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