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