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