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