*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject', 
260 `gnus-summary-first-unread-article' and 
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? 
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? 
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-treading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-newsgroup-default-charset-alist 
801   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" . cn-big5)
802     ("^cn\\>\\|\\<chinese\\>" . cn-gb-2312)
803     ("^fj\\>\\|^japan\\>" . iso-2022-jp-2)
804     ("^relcom\\>" . koi8-r))
805   "Alist of Regexps (to match group names) and default charsets to be applied."
806   :type '(repeat (cons (regexp :tag "Group")
807                        (symbol :tag "Charset")))
808   :group 'gnus)
809
810 (defcustom gnus-newsgroup-iso-8859-1-forced-regexp 
811   "^tw\\>\\|^hk\\>\\|^cn\\>\\|\\<chinese\\>"
812   "Regexp of newsgroup in which ISO-8859-1 is forced to other charset."
813   :type 'regexp
814   :group 'gnus)
815
816 ;;; Internal variables
817
818 (defvar gnus-article-mime-handles nil)
819 (defvar gnus-article-decoded-p nil)
820 (defvar gnus-scores-exclude-files nil)
821 (defvar gnus-page-broken nil)
822 (defvar gnus-inhibit-mime-unbuttonizing nil)
823
824 (defvar gnus-original-article nil)
825 (defvar gnus-article-internal-prepare-hook nil)
826 (defvar gnus-newsgroup-process-stack nil)
827
828 (defvar gnus-thread-indent-array nil)
829 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
830
831 ;; Avoid highlighting in kill files.
832 (defvar gnus-summary-inhibit-highlight nil)
833 (defvar gnus-newsgroup-selected-overlay nil)
834 (defvar gnus-inhibit-limiting nil)
835 (defvar gnus-newsgroup-adaptive-score-file nil)
836 (defvar gnus-current-score-file nil)
837 (defvar gnus-current-move-group nil)
838 (defvar gnus-current-copy-group nil)
839 (defvar gnus-current-crosspost-group nil)
840
841 (defvar gnus-newsgroup-dependencies nil)
842 (defvar gnus-newsgroup-adaptive nil)
843 (defvar gnus-summary-display-article-function nil)
844 (defvar gnus-summary-highlight-line-function nil
845   "Function called after highlighting a summary line.")
846
847 (defvar gnus-summary-line-format-alist
848   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
849     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
850     (?s gnus-tmp-subject-or-nil ?s)
851     (?n gnus-tmp-name ?s)
852     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
853         ?s)
854     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
855             gnus-tmp-from) ?s)
856     (?F gnus-tmp-from ?s)
857     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
858     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
859     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
860     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
861     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
862     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
863     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
864     (?L gnus-tmp-lines ?d)
865     (?I gnus-tmp-indentation ?s)
866     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
867     (?R gnus-tmp-replied ?c)
868     (?\[ gnus-tmp-opening-bracket ?c)
869     (?\] gnus-tmp-closing-bracket ?c)
870     (?\> (make-string gnus-tmp-level ? ) ?s)
871     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
872     (?i gnus-tmp-score ?d)
873     (?z gnus-tmp-score-char ?c)
874     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
875     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
876     (?U gnus-tmp-unread ?c)
877     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
878     (?t (gnus-summary-number-of-articles-in-thread
879          (and (boundp 'thread) (car thread)) gnus-tmp-level)
880         ?d)
881     (?e (gnus-summary-number-of-articles-in-thread
882          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
883         ?c)
884     (?u gnus-tmp-user-defined ?s)
885     (?P (gnus-pick-line-number) ?d))
886   "An alist of format specifications that can appear in summary lines,
887 and what variables they correspond with, along with the type of the
888 variable (string, integer, character, etc).")
889
890 (defvar gnus-summary-dummy-line-format-alist
891   `((?S gnus-tmp-subject ?s)
892     (?N gnus-tmp-number ?d)
893     (?u gnus-tmp-user-defined ?s)))
894
895 (defvar gnus-summary-mode-line-format-alist
896   `((?G gnus-tmp-group-name ?s)
897     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
898     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
899     (?A gnus-tmp-article-number ?d)
900     (?Z gnus-tmp-unread-and-unselected ?s)
901     (?V gnus-version ?s)
902     (?U gnus-tmp-unread-and-unticked ?d)
903     (?S gnus-tmp-subject ?s)
904     (?e gnus-tmp-unselected ?d)
905     (?u gnus-tmp-user-defined ?s)
906     (?d (length gnus-newsgroup-dormant) ?d)
907     (?t (length gnus-newsgroup-marked) ?d)
908     (?r (length gnus-newsgroup-reads) ?d)
909     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
910     (?E gnus-newsgroup-expunged-tally ?d)
911     (?s (gnus-current-score-file-nondirectory) ?s)))
912
913 (defvar gnus-last-search-regexp nil
914   "Default regexp for article search command.")
915
916 (defvar gnus-last-shell-command nil
917   "Default shell command on article.")
918
919 (defvar gnus-newsgroup-begin nil)
920 (defvar gnus-newsgroup-end nil)
921 (defvar gnus-newsgroup-last-rmail nil)
922 (defvar gnus-newsgroup-last-mail nil)
923 (defvar gnus-newsgroup-last-folder nil)
924 (defvar gnus-newsgroup-last-file nil)
925 (defvar gnus-newsgroup-auto-expire nil)
926 (defvar gnus-newsgroup-active nil)
927
928 (defvar gnus-newsgroup-data nil)
929 (defvar gnus-newsgroup-data-reverse nil)
930 (defvar gnus-newsgroup-limit nil)
931 (defvar gnus-newsgroup-limits nil)
932
933 (defvar gnus-newsgroup-unreads nil
934   "List of unread articles in the current newsgroup.")
935
936 (defvar gnus-newsgroup-unselected nil
937   "List of unselected unread articles in the current newsgroup.")
938
939 (defvar gnus-newsgroup-reads nil
940   "Alist of read articles and article marks in the current newsgroup.")
941
942 (defvar gnus-newsgroup-expunged-tally nil)
943
944 (defvar gnus-newsgroup-marked nil
945   "List of ticked articles in the current newsgroup (a subset of unread art).")
946
947 (defvar gnus-newsgroup-killed nil
948   "List of ranges of articles that have been through the scoring process.")
949
950 (defvar gnus-newsgroup-cached nil
951   "List of articles that come from the article cache.")
952
953 (defvar gnus-newsgroup-saved nil
954   "List of articles that have been saved.")
955
956 (defvar gnus-newsgroup-kill-headers nil)
957
958 (defvar gnus-newsgroup-replied nil
959   "List of articles that have been replied to in the current newsgroup.")
960
961 (defvar gnus-newsgroup-expirable nil
962   "List of articles in the current newsgroup that can be expired.")
963
964 (defvar gnus-newsgroup-processable nil
965   "List of articles in the current newsgroup that can be processed.")
966
967 (defvar gnus-newsgroup-downloadable nil
968   "List of articles in the current newsgroup that can be processed.")
969
970 (defvar gnus-newsgroup-undownloaded nil
971   "List of articles in the current newsgroup that haven't been downloaded..")
972
973 (defvar gnus-newsgroup-unsendable nil
974   "List of articles in the current newsgroup that won't be sent.")
975
976 (defvar gnus-newsgroup-bookmarks nil
977   "List of articles in the current newsgroup that have bookmarks.")
978
979 (defvar gnus-newsgroup-dormant nil
980   "List of dormant articles in the current newsgroup.")
981
982 (defvar gnus-newsgroup-scored nil
983   "List of scored articles in the current newsgroup.")
984
985 (defvar gnus-newsgroup-headers nil
986   "List of article headers in the current newsgroup.")
987
988 (defvar gnus-newsgroup-threads nil)
989
990 (defvar gnus-newsgroup-prepared nil
991   "Whether the current group has been prepared properly.")
992
993 (defvar gnus-newsgroup-ancient nil
994   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
995
996 (defvar gnus-newsgroup-sparse nil)
997
998 (defvar gnus-current-article nil)
999 (defvar gnus-article-current nil)
1000 (defvar gnus-current-headers nil)
1001 (defvar gnus-have-all-headers nil)
1002 (defvar gnus-last-article nil)
1003 (defvar gnus-newsgroup-history nil)
1004
1005 (defvar gnus-newsgroup-default-charset nil)
1006 (defvar gnus-newsgroup-iso-8859-1-forced nil)
1007
1008 (defconst gnus-summary-local-variables
1009   '(gnus-newsgroup-name
1010     gnus-newsgroup-begin gnus-newsgroup-end
1011     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1012     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1013     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1014     gnus-newsgroup-unselected gnus-newsgroup-marked
1015     gnus-newsgroup-reads gnus-newsgroup-saved
1016     gnus-newsgroup-replied gnus-newsgroup-expirable
1017     gnus-newsgroup-processable gnus-newsgroup-killed
1018     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1019     gnus-newsgroup-unsendable
1020     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1021     gnus-newsgroup-headers gnus-newsgroup-threads
1022     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1023     gnus-current-article gnus-current-headers gnus-have-all-headers
1024     gnus-last-article gnus-article-internal-prepare-hook
1025     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1026     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1027     gnus-thread-expunge-below
1028     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1029     (gnus-summary-mark-below . global)
1030     gnus-newsgroup-active gnus-scores-exclude-files
1031     gnus-newsgroup-history gnus-newsgroup-ancient
1032     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1033     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1034     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1035     (gnus-newsgroup-expunged-tally . 0)
1036     gnus-cache-removable-articles gnus-newsgroup-cached
1037     gnus-newsgroup-data gnus-newsgroup-data-reverse
1038     gnus-newsgroup-limit gnus-newsgroup-limits
1039     gnus-newsgroup-default-charset gnus-newsgroup-iso-8859-1-forced)
1040   "Variables that are buffer-local to the summary buffers.")
1041
1042 ;; Byte-compiler warning.
1043 (defvar gnus-article-mode-map)
1044
1045 ;; MIME stuff.
1046
1047 (defvar gnus-decode-encoded-word-methods
1048   '(mail-decode-encoded-word-string)
1049   "List of methods used to decode encoded words.
1050
1051 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1052 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1053 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1054 whose names match REGEXP.
1055
1056 For example: 
1057 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1058  mail-decode-encoded-word-string 
1059  (\"chinese\" . rfc1843-decode-string))
1060 ")
1061
1062 (defvar gnus-decode-encoded-word-methods-cache nil)
1063
1064 (defun gnus-multi-decode-encoded-word-string (string)
1065   "Apply the functions from `gnus-encoded-word-methods' that match."
1066   (unless (and gnus-decode-encoded-word-methods-cache
1067                (eq gnus-newsgroup-name 
1068                    (car gnus-decode-encoded-word-methods-cache)))
1069     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1070     (mapc '(lambda (x) 
1071              (if (symbolp x)
1072                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1073                (if (and gnus-newsgroup-name 
1074                         (string-match (car x) gnus-newsgroup-name))
1075                    (nconc gnus-decode-encoded-word-methods-cache 
1076                           (list (cdr x))))))
1077           gnus-decode-encoded-word-methods))
1078   (let ((xlist gnus-decode-encoded-word-methods-cache))
1079     (pop xlist)
1080     (while xlist
1081       (setq string (funcall (pop xlist) string))))
1082   string)
1083
1084 ;; Subject simplification.
1085
1086 (defun gnus-simplify-whitespace (str)
1087   "Remove excessive whitespace."
1088   (let ((mystr str))
1089     ;; Multiple spaces.
1090     (while (string-match "[ \t][ \t]+" mystr)
1091       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1092                           " "
1093                           (substring mystr (match-end 0)))))
1094     ;; Leading spaces.
1095     (when (string-match "^[ \t]+" mystr)
1096       (setq mystr (substring mystr (match-end 0))))
1097     ;; Trailing spaces.
1098     (when (string-match "[ \t]+$" mystr)
1099       (setq mystr (substring mystr 0 (match-beginning 0))))
1100     mystr))
1101
1102 (defsubst gnus-simplify-subject-re (subject)
1103   "Remove \"Re:\" from subject lines."
1104   (if (string-match "^[Rr][Ee]: *" subject)
1105       (substring subject (match-end 0))
1106     subject))
1107
1108 (defun gnus-simplify-subject (subject &optional re-only)
1109   "Remove `Re:' and words in parentheses.
1110 If RE-ONLY is non-nil, strip leading `Re:'s only."
1111   (let ((case-fold-search t))           ;Ignore case.
1112     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1113     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1114       (setq subject (substring subject (match-end 0))))
1115     ;; Remove uninteresting prefixes.
1116     (when (and (not re-only)
1117                gnus-simplify-ignored-prefixes
1118                (string-match gnus-simplify-ignored-prefixes subject))
1119       (setq subject (substring subject (match-end 0))))
1120     ;; Remove words in parentheses from end.
1121     (unless re-only
1122       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1123         (setq subject (substring subject 0 (match-beginning 0)))))
1124     ;; Return subject string.
1125     subject))
1126
1127 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1128 ;; all whitespace.
1129 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1130   (goto-char (point-min))
1131   (while (re-search-forward regexp nil t)
1132       (replace-match (or newtext ""))))
1133
1134 (defun gnus-simplify-buffer-fuzzy ()
1135   "Simplify string in the buffer fuzzily.
1136 The string in the accessible portion of the current buffer is simplified.
1137 It is assumed to be a single-line subject.
1138 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1139 matter is removed.  Additional things can be deleted by setting
1140 gnus-simplify-subject-fuzzy-regexp."
1141   (let ((case-fold-search t)
1142         (modified-tick))
1143     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1144
1145     (while (not (eq modified-tick (buffer-modified-tick)))
1146       (setq modified-tick (buffer-modified-tick))
1147       (cond
1148        ((listp gnus-simplify-subject-fuzzy-regexp)
1149         (mapcar 'gnus-simplify-buffer-fuzzy-step
1150                 gnus-simplify-subject-fuzzy-regexp))
1151        (gnus-simplify-subject-fuzzy-regexp
1152         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1153       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1154       (gnus-simplify-buffer-fuzzy-step
1155        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1156       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1157
1158     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1159     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1160     (gnus-simplify-buffer-fuzzy-step " $")
1161     (gnus-simplify-buffer-fuzzy-step "^ +")))
1162
1163 (defun gnus-simplify-subject-fuzzy (subject)
1164   "Simplify a subject string fuzzily.
1165 See `gnus-simplify-buffer-fuzzy' for details."
1166   (save-excursion
1167     (gnus-set-work-buffer)
1168     (let ((case-fold-search t))
1169       ;; Remove uninteresting prefixes.
1170       (when (and gnus-simplify-ignored-prefixes
1171                  (string-match gnus-simplify-ignored-prefixes subject))
1172         (setq subject (substring subject (match-end 0))))
1173       (insert subject)
1174       (inline (gnus-simplify-buffer-fuzzy))
1175       (buffer-string))))
1176
1177 (defsubst gnus-simplify-subject-fully (subject)
1178   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1179   (cond
1180    (gnus-simplify-subject-functions
1181     (gnus-map-function gnus-simplify-subject-functions subject))
1182    ((null gnus-summary-gather-subject-limit)
1183     (gnus-simplify-subject-re subject))
1184    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1185     (gnus-simplify-subject-fuzzy subject))
1186    ((numberp gnus-summary-gather-subject-limit)
1187     (gnus-limit-string (gnus-simplify-subject-re subject)
1188                        gnus-summary-gather-subject-limit))
1189    (t
1190     subject)))
1191
1192 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1193   "Check whether two subjects are equal.
1194 If optional argument simple-first is t, first argument is already
1195 simplified."
1196   (cond
1197    ((null simple-first)
1198     (equal (gnus-simplify-subject-fully s1)
1199            (gnus-simplify-subject-fully s2)))
1200    (t
1201     (equal s1
1202            (gnus-simplify-subject-fully s2)))))
1203
1204 (defun gnus-summary-bubble-group ()
1205   "Increase the score of the current group.
1206 This is a handy function to add to `gnus-summary-exit-hook' to
1207 increase the score of each group you read."
1208   (gnus-group-add-score gnus-newsgroup-name))
1209
1210 \f
1211 ;;;
1212 ;;; Gnus summary mode
1213 ;;;
1214
1215 (put 'gnus-summary-mode 'mode-class 'special)
1216
1217 (when t
1218   ;; Non-orthogonal keys
1219
1220   (gnus-define-keys gnus-summary-mode-map
1221     " " gnus-summary-next-page
1222     "\177" gnus-summary-prev-page
1223     [delete] gnus-summary-prev-page
1224     [backspace] gnus-summary-prev-page
1225     "\r" gnus-summary-scroll-up
1226     "\M-\r" gnus-summary-scroll-down
1227     "n" gnus-summary-next-unread-article
1228     "p" gnus-summary-prev-unread-article
1229     "N" gnus-summary-next-article
1230     "P" gnus-summary-prev-article
1231     "\M-\C-n" gnus-summary-next-same-subject
1232     "\M-\C-p" gnus-summary-prev-same-subject
1233     "\M-n" gnus-summary-next-unread-subject
1234     "\M-p" gnus-summary-prev-unread-subject
1235     "." gnus-summary-first-unread-article
1236     "," gnus-summary-best-unread-article
1237     "\M-s" gnus-summary-search-article-forward
1238     "\M-r" gnus-summary-search-article-backward
1239     "<" gnus-summary-beginning-of-article
1240     ">" gnus-summary-end-of-article
1241     "j" gnus-summary-goto-article
1242     "^" gnus-summary-refer-parent-article
1243     "\M-^" gnus-summary-refer-article
1244     "u" gnus-summary-tick-article-forward
1245     "!" gnus-summary-tick-article-forward
1246     "U" gnus-summary-tick-article-backward
1247     "d" gnus-summary-mark-as-read-forward
1248     "D" gnus-summary-mark-as-read-backward
1249     "E" gnus-summary-mark-as-expirable
1250     "\M-u" gnus-summary-clear-mark-forward
1251     "\M-U" gnus-summary-clear-mark-backward
1252     "k" gnus-summary-kill-same-subject-and-select
1253     "\C-k" gnus-summary-kill-same-subject
1254     "\M-\C-k" gnus-summary-kill-thread
1255     "\M-\C-l" gnus-summary-lower-thread
1256     "e" gnus-summary-edit-article
1257     "#" gnus-summary-mark-as-processable
1258     "\M-#" gnus-summary-unmark-as-processable
1259     "\M-\C-t" gnus-summary-toggle-threads
1260     "\M-\C-s" gnus-summary-show-thread
1261     "\M-\C-h" gnus-summary-hide-thread
1262     "\M-\C-f" gnus-summary-next-thread
1263     "\M-\C-b" gnus-summary-prev-thread
1264     "\M-\C-u" gnus-summary-up-thread
1265     "\M-\C-d" gnus-summary-down-thread
1266     "&" gnus-summary-execute-command
1267     "c" gnus-summary-catchup-and-exit
1268     "\C-w" gnus-summary-mark-region-as-read
1269     "\C-t" gnus-summary-toggle-truncation
1270     "?" gnus-summary-mark-as-dormant
1271     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1272     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1273     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1274     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1275     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1276     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1277     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1278     "=" gnus-summary-expand-window
1279     "\C-x\C-s" gnus-summary-reselect-current-group
1280     "\M-g" gnus-summary-rescan-group
1281     "w" gnus-summary-stop-page-breaking
1282     "\C-c\C-r" gnus-summary-caesar-message
1283     "f" gnus-summary-followup
1284     "F" gnus-summary-followup-with-original
1285     "C" gnus-summary-cancel-article
1286     "r" gnus-summary-reply
1287     "R" gnus-summary-reply-with-original
1288     "\C-c\C-f" gnus-summary-mail-forward
1289     "o" gnus-summary-save-article
1290     "\C-o" gnus-summary-save-article-mail
1291     "|" gnus-summary-pipe-output
1292     "\M-k" gnus-summary-edit-local-kill
1293     "\M-K" gnus-summary-edit-global-kill
1294     ;; "V" gnus-version
1295     "\C-c\C-d" gnus-summary-describe-group
1296     "q" gnus-summary-exit
1297     "Q" gnus-summary-exit-no-update
1298     "\C-c\C-i" gnus-info-find-node
1299     gnus-mouse-2 gnus-mouse-pick-article
1300     "m" gnus-summary-mail-other-window
1301     "a" gnus-summary-post-news
1302     "x" gnus-summary-limit-to-unread
1303     "s" gnus-summary-isearch-article
1304     "t" gnus-article-hide-headers
1305     "g" gnus-summary-show-article
1306     "l" gnus-summary-goto-last-article
1307     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1308     "\C-d" gnus-summary-enter-digest-group
1309     "\M-\C-d" gnus-summary-read-document
1310     "\M-\C-e" gnus-summary-edit-parameters
1311     "\M-\C-g" gnus-summary-customize-parameters
1312     "\C-c\C-b" gnus-bug
1313     "*" gnus-cache-enter-article
1314     "\M-*" gnus-cache-remove-article
1315     "\M-&" gnus-summary-universal-argument
1316     "\C-l" gnus-recenter
1317     "I" gnus-summary-increase-score
1318     "L" gnus-summary-lower-score
1319     "\M-i" gnus-symbolic-argument
1320     "h" gnus-summary-select-article-buffer
1321
1322     "b" gnus-article-view-part
1323     "\M-t" gnus-summary-toggle-display-buttonized
1324     
1325     "V" gnus-summary-score-map
1326     "X" gnus-uu-extract-map
1327     "S" gnus-summary-send-map)
1328
1329   ;; Sort of orthogonal keymap
1330   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1331     "t" gnus-summary-tick-article-forward
1332     "!" gnus-summary-tick-article-forward
1333     "d" gnus-summary-mark-as-read-forward
1334     "r" gnus-summary-mark-as-read-forward
1335     "c" gnus-summary-clear-mark-forward
1336     " " gnus-summary-clear-mark-forward
1337     "e" gnus-summary-mark-as-expirable
1338     "x" gnus-summary-mark-as-expirable
1339     "?" gnus-summary-mark-as-dormant
1340     "b" gnus-summary-set-bookmark
1341     "B" gnus-summary-remove-bookmark
1342     "#" gnus-summary-mark-as-processable
1343     "\M-#" gnus-summary-unmark-as-processable
1344     "S" gnus-summary-limit-include-expunged
1345     "C" gnus-summary-catchup
1346     "H" gnus-summary-catchup-to-here
1347     "\C-c" gnus-summary-catchup-all
1348     "k" gnus-summary-kill-same-subject-and-select
1349     "K" gnus-summary-kill-same-subject
1350     "P" gnus-uu-mark-map)
1351
1352   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1353     "c" gnus-summary-clear-above
1354     "u" gnus-summary-tick-above
1355     "m" gnus-summary-mark-above
1356     "k" gnus-summary-kill-below)
1357
1358   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1359     "/" gnus-summary-limit-to-subject
1360     "n" gnus-summary-limit-to-articles
1361     "w" gnus-summary-pop-limit
1362     "s" gnus-summary-limit-to-subject
1363     "a" gnus-summary-limit-to-author
1364     "u" gnus-summary-limit-to-unread
1365     "m" gnus-summary-limit-to-marks
1366     "v" gnus-summary-limit-to-score
1367     "*" gnus-summary-limit-include-cached
1368     "D" gnus-summary-limit-include-dormant
1369     "T" gnus-summary-limit-include-thread
1370     "d" gnus-summary-limit-exclude-dormant
1371     "t" gnus-summary-limit-to-age
1372     "E" gnus-summary-limit-include-expunged
1373     "c" gnus-summary-limit-exclude-childless-dormant
1374     "C" gnus-summary-limit-mark-excluded-as-read)
1375
1376   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1377     "n" gnus-summary-next-unread-article
1378     "p" gnus-summary-prev-unread-article
1379     "N" gnus-summary-next-article
1380     "P" gnus-summary-prev-article
1381     "\C-n" gnus-summary-next-same-subject
1382     "\C-p" gnus-summary-prev-same-subject
1383     "\M-n" gnus-summary-next-unread-subject
1384     "\M-p" gnus-summary-prev-unread-subject
1385     "f" gnus-summary-first-unread-article
1386     "b" gnus-summary-best-unread-article
1387     "j" gnus-summary-goto-article
1388     "g" gnus-summary-goto-subject
1389     "l" gnus-summary-goto-last-article
1390     "o" gnus-summary-pop-article)
1391
1392   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1393     "k" gnus-summary-kill-thread
1394     "l" gnus-summary-lower-thread
1395     "i" gnus-summary-raise-thread
1396     "T" gnus-summary-toggle-threads
1397     "t" gnus-summary-rethread-current
1398     "^" gnus-summary-reparent-thread
1399     "s" gnus-summary-show-thread
1400     "S" gnus-summary-show-all-threads
1401     "h" gnus-summary-hide-thread
1402     "H" gnus-summary-hide-all-threads
1403     "n" gnus-summary-next-thread
1404     "p" gnus-summary-prev-thread
1405     "u" gnus-summary-up-thread
1406     "o" gnus-summary-top-thread
1407     "d" gnus-summary-down-thread
1408     "#" gnus-uu-mark-thread
1409     "\M-#" gnus-uu-unmark-thread)
1410
1411   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1412     "g" gnus-summary-prepare
1413     "c" gnus-summary-insert-cached-articles)
1414
1415   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1416     "c" gnus-summary-catchup-and-exit
1417     "C" gnus-summary-catchup-all-and-exit
1418     "E" gnus-summary-exit-no-update
1419     "Q" gnus-summary-exit
1420     "Z" gnus-summary-exit
1421     "n" gnus-summary-catchup-and-goto-next-group
1422     "R" gnus-summary-reselect-current-group
1423     "G" gnus-summary-rescan-group
1424     "N" gnus-summary-next-group
1425     "s" gnus-summary-save-newsrc
1426     "P" gnus-summary-prev-group)
1427
1428   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1429     " " gnus-summary-next-page
1430     "n" gnus-summary-next-page
1431     "\177" gnus-summary-prev-page
1432     [delete] gnus-summary-prev-page
1433     "p" gnus-summary-prev-page
1434     "\r" gnus-summary-scroll-up
1435     "\M-\r" gnus-summary-scroll-down
1436     "<" gnus-summary-beginning-of-article
1437     ">" gnus-summary-end-of-article
1438     "b" gnus-summary-beginning-of-article
1439     "e" gnus-summary-end-of-article
1440     "^" gnus-summary-refer-parent-article
1441     "r" gnus-summary-refer-parent-article
1442     "R" gnus-summary-refer-references
1443     "T" gnus-summary-refer-thread
1444     "g" gnus-summary-show-article
1445     "s" gnus-summary-isearch-article
1446     "P" gnus-summary-print-article)
1447
1448   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1449     "b" gnus-article-add-buttons
1450     "B" gnus-article-add-buttons-to-head
1451     "o" gnus-article-treat-overstrike
1452     "e" gnus-article-emphasize
1453     "w" gnus-article-fill-cited-article
1454     "c" gnus-article-remove-cr
1455     "q" gnus-article-de-quoted-unreadable
1456     "f" gnus-article-display-x-face
1457     "l" gnus-summary-stop-page-breaking
1458     "r" gnus-summary-caesar-message
1459     "t" gnus-article-hide-headers
1460     "v" gnus-summary-verbose-headers
1461     "h" gnus-article-treat-html
1462     "d" gnus-article-treat-dumbquotes)
1463
1464   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1465     "a" gnus-article-hide
1466     "h" gnus-article-hide-headers
1467     "b" gnus-article-hide-boring-headers
1468     "s" gnus-article-hide-signature
1469     "c" gnus-article-hide-citation
1470     "C" gnus-article-hide-citation-in-followups
1471     "p" gnus-article-hide-pgp
1472     "P" gnus-article-hide-pem
1473     "\C-c" gnus-article-hide-citation-maybe)
1474
1475   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1476     "a" gnus-article-highlight
1477     "h" gnus-article-highlight-headers
1478     "c" gnus-article-highlight-citation
1479     "s" gnus-article-highlight-signature)
1480
1481   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1482     "w" gnus-article-decode-mime-words
1483     "c" gnus-article-decode-charset
1484     "v" gnus-mime-view-all-parts
1485     "b" gnus-article-view-part)
1486
1487   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1488     "z" gnus-article-date-ut
1489     "u" gnus-article-date-ut
1490     "l" gnus-article-date-local
1491     "e" gnus-article-date-lapsed
1492     "o" gnus-article-date-original
1493     "i" gnus-article-date-iso8601
1494     "s" gnus-article-date-user)
1495
1496   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1497     "t" gnus-article-remove-trailing-blank-lines
1498     "l" gnus-article-strip-leading-blank-lines
1499     "m" gnus-article-strip-multiple-blank-lines
1500     "a" gnus-article-strip-blank-lines
1501     "A" gnus-article-strip-all-blank-lines
1502     "s" gnus-article-strip-leading-space
1503     "e" gnus-article-strip-trailing-space)
1504
1505   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1506     "v" gnus-version
1507     "f" gnus-summary-fetch-faq
1508     "d" gnus-summary-describe-group
1509     "h" gnus-summary-describe-briefly
1510     "i" gnus-info-find-node)
1511
1512   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1513     "e" gnus-summary-expire-articles
1514     "\M-\C-e" gnus-summary-expire-articles-now
1515     "\177" gnus-summary-delete-article
1516     [delete] gnus-summary-delete-article
1517     "m" gnus-summary-move-article
1518     "r" gnus-summary-respool-article
1519     "w" gnus-summary-edit-article
1520     "c" gnus-summary-copy-article
1521     "B" gnus-summary-crosspost-article
1522     "q" gnus-summary-respool-query
1523     "t" gnus-summary-respool-trace
1524     "i" gnus-summary-import-article
1525     "p" gnus-summary-article-posted-p)
1526
1527   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1528     "o" gnus-summary-save-article
1529     "m" gnus-summary-save-article-mail
1530     "F" gnus-summary-write-article-file
1531     "r" gnus-summary-save-article-rmail
1532     "f" gnus-summary-save-article-file
1533     "b" gnus-summary-save-article-body-file
1534     "h" gnus-summary-save-article-folder
1535     "v" gnus-summary-save-article-vm
1536     "p" gnus-summary-pipe-output
1537     "s" gnus-soup-add-article)
1538
1539   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1540     "b" gnus-summary-display-buttonized
1541     "v" gnus-article-view-part
1542     "o" gnus-article-save-part
1543     "c" gnus-article-copy-part
1544     "e" gnus-article-externalize-part
1545     "|" gnus-article-pipe-part)
1546   )
1547
1548 (defun gnus-summary-make-menu-bar ()
1549   (gnus-turn-off-edit-menu 'summary)
1550
1551   (unless (boundp 'gnus-summary-misc-menu)
1552
1553     (easy-menu-define
1554      gnus-summary-kill-menu gnus-summary-mode-map ""
1555      (cons
1556       "Score"
1557       (nconc
1558        (list
1559         ["Enter score..." gnus-summary-score-entry t]
1560         ["Customize" gnus-score-customize t])
1561        (gnus-make-score-map 'increase)
1562        (gnus-make-score-map 'lower)
1563        '(("Mark"
1564           ["Kill below" gnus-summary-kill-below t]
1565           ["Mark above" gnus-summary-mark-above t]
1566           ["Tick above" gnus-summary-tick-above t]
1567           ["Clear above" gnus-summary-clear-above t])
1568          ["Current score" gnus-summary-current-score t]
1569          ["Set score" gnus-summary-set-score t]
1570          ["Switch current score file..." gnus-score-change-score-file t]
1571          ["Set mark below..." gnus-score-set-mark-below t]
1572          ["Set expunge below..." gnus-score-set-expunge-below t]
1573          ["Edit current score file" gnus-score-edit-current-scores t]
1574          ["Edit score file" gnus-score-edit-file t]
1575          ["Trace score" gnus-score-find-trace t]
1576          ["Find words" gnus-score-find-favourite-words t]
1577          ["Rescore buffer" gnus-summary-rescore t]
1578          ["Increase score..." gnus-summary-increase-score t]
1579          ["Lower score..." gnus-summary-lower-score t]))))
1580
1581     ;; Define both the Article menu in the summary buffer and the equivalent
1582     ;; Commands menu in the article buffer here for consistency.
1583     (let ((innards
1584            '(("Hide"
1585               ["All" gnus-article-hide t]
1586               ["Headers" gnus-article-hide-headers t]
1587               ["Signature" gnus-article-hide-signature t]
1588               ["Citation" gnus-article-hide-citation t]
1589               ["PGP" gnus-article-hide-pgp t]
1590               ["Boring headers" gnus-article-hide-boring-headers t])
1591              ("Highlight"
1592               ["All" gnus-article-highlight t]
1593               ["Headers" gnus-article-highlight-headers t]
1594               ["Signature" gnus-article-highlight-signature t]
1595               ["Citation" gnus-article-highlight-citation t])
1596              ("MIME"
1597               ["Words" gnus-article-decode-mime-words t]
1598               ["Charset" gnus-article-decode-charset t]
1599               ["QP" gnus-article-de-quoted-unreadable t]
1600               ["View all" gnus-mime-view-all-parts t])
1601              ("Date"
1602               ["Local" gnus-article-date-local t]
1603               ["ISO8601" gnus-article-date-iso8601 t]
1604               ["UT" gnus-article-date-ut t]
1605               ["Original" gnus-article-date-original t]
1606               ["Lapsed" gnus-article-date-lapsed t]
1607               ["User-defined" gnus-article-date-user t])
1608              ("Washing"
1609               ("Remove Blanks"
1610                ["Leading" gnus-article-strip-leading-blank-lines t]
1611                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1612                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1613                ["All of the above" gnus-article-strip-blank-lines t]
1614                ["All" gnus-article-strip-all-blank-lines t]
1615                ["Leading space" gnus-article-strip-leading-space t]
1616                ["Trailing space" gnus-article-strip-trailing-space t])
1617               ["Overstrike" gnus-article-treat-overstrike t]
1618               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1619               ["Emphasis" gnus-article-emphasize t]
1620               ["Word wrap" gnus-article-fill-cited-article t]
1621               ["CR" gnus-article-remove-cr t]
1622               ["Show X-Face" gnus-article-display-x-face t]
1623               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1624               ["UnHTMLize" gnus-article-treat-html t]
1625               ["Rot 13" gnus-summary-caesar-message t]
1626               ["Unix pipe" gnus-summary-pipe-message t]
1627               ["Add buttons" gnus-article-add-buttons t]
1628               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1629               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1630               ["Verbose header" gnus-summary-verbose-headers t]
1631               ["Toggle header" gnus-summary-toggle-header t])
1632              ("Output"
1633               ["Save in default format" gnus-summary-save-article t]
1634               ["Save in file" gnus-summary-save-article-file t]
1635               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1636               ["Save in MH folder" gnus-summary-save-article-folder t]
1637               ["Save in VM folder" gnus-summary-save-article-vm t]
1638               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1639               ["Save body in file" gnus-summary-save-article-body-file t]
1640               ["Pipe through a filter" gnus-summary-pipe-output t]
1641               ["Add to SOUP packet" gnus-soup-add-article t]
1642               ["Print" gnus-summary-print-article t])
1643              ("Backend"
1644               ["Respool article..." gnus-summary-respool-article t]
1645               ["Move article..." gnus-summary-move-article
1646                (gnus-check-backend-function
1647                 'request-move-article gnus-newsgroup-name)]
1648               ["Copy article..." gnus-summary-copy-article t]
1649               ["Crosspost article..." gnus-summary-crosspost-article
1650                (gnus-check-backend-function
1651                 'request-replace-article gnus-newsgroup-name)]
1652               ["Import file..." gnus-summary-import-article t]
1653               ["Check if posted" gnus-summary-article-posted-p t]
1654               ["Edit article" gnus-summary-edit-article
1655                (not (gnus-group-read-only-p))]
1656               ["Delete article" gnus-summary-delete-article
1657                (gnus-check-backend-function
1658                 'request-expire-articles gnus-newsgroup-name)]
1659               ["Query respool" gnus-summary-respool-query t]
1660               ["Trace respool" gnus-summary-respool-trace t]
1661               ["Delete expirable articles" gnus-summary-expire-articles-now
1662                (gnus-check-backend-function
1663                 'request-expire-articles gnus-newsgroup-name)])
1664              ("Extract"
1665               ["Uudecode" gnus-uu-decode-uu t]
1666               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1667               ["Unshar" gnus-uu-decode-unshar t]
1668               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1669               ["Save" gnus-uu-decode-save t]
1670               ["Binhex" gnus-uu-decode-binhex t]
1671               ["Postscript" gnus-uu-decode-postscript t])
1672              ("Cache"
1673               ["Enter article" gnus-cache-enter-article t]
1674               ["Remove article" gnus-cache-remove-article t])
1675              ["Select article buffer" gnus-summary-select-article-buffer t]
1676              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1677              ["Isearch article..." gnus-summary-isearch-article t]
1678              ["Beginning of the article" gnus-summary-beginning-of-article t]
1679              ["End of the article" gnus-summary-end-of-article t]
1680              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1681              ["Fetch referenced articles" gnus-summary-refer-references t]
1682              ["Fetch current thread" gnus-summary-refer-thread t]
1683              ["Fetch article with id..." gnus-summary-refer-article t]
1684              ["Redisplay" gnus-summary-show-article t])))
1685       (easy-menu-define
1686        gnus-summary-article-menu gnus-summary-mode-map ""
1687        (cons "Article" innards))
1688
1689       (easy-menu-define
1690        gnus-article-commands-menu gnus-article-mode-map ""
1691        (cons "Commands" innards)))
1692
1693     (easy-menu-define
1694      gnus-summary-thread-menu gnus-summary-mode-map ""
1695      '("Threads"
1696        ["Toggle threading" gnus-summary-toggle-threads t]
1697        ["Hide threads" gnus-summary-hide-all-threads t]
1698        ["Show threads" gnus-summary-show-all-threads t]
1699        ["Hide thread" gnus-summary-hide-thread t]
1700        ["Show thread" gnus-summary-show-thread t]
1701        ["Go to next thread" gnus-summary-next-thread t]
1702        ["Go to previous thread" gnus-summary-prev-thread t]
1703        ["Go down thread" gnus-summary-down-thread t]
1704        ["Go up thread" gnus-summary-up-thread t]
1705        ["Top of thread" gnus-summary-top-thread t]
1706        ["Mark thread as read" gnus-summary-kill-thread t]
1707        ["Lower thread score" gnus-summary-lower-thread t]
1708        ["Raise thread score" gnus-summary-raise-thread t]
1709        ["Rethread current" gnus-summary-rethread-current t]
1710        ))
1711
1712     (easy-menu-define
1713      gnus-summary-post-menu gnus-summary-mode-map ""
1714      '("Post"
1715        ["Post an article" gnus-summary-post-news t]
1716        ["Followup" gnus-summary-followup t]
1717        ["Followup and yank" gnus-summary-followup-with-original t]
1718        ["Supersede article" gnus-summary-supersede-article t]
1719        ["Cancel article" gnus-summary-cancel-article t]
1720        ["Reply" gnus-summary-reply t]
1721        ["Reply and yank" gnus-summary-reply-with-original t]
1722        ["Wide reply" gnus-summary-wide-reply t]
1723        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1724        ["Mail forward" gnus-summary-mail-forward t]
1725        ["Post forward" gnus-summary-post-forward t]
1726        ["Digest and mail" gnus-uu-digest-mail-forward t]
1727        ["Digest and post" gnus-uu-digest-post-forward t]
1728        ["Resend message" gnus-summary-resend-message t]
1729        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1730        ["Send a mail" gnus-summary-mail-other-window t]
1731        ["Uuencode and post" gnus-uu-post-news t]
1732        ["Followup via news" gnus-summary-followup-to-mail t]
1733        ["Followup via news and yank"
1734         gnus-summary-followup-to-mail-with-original t]
1735        ;;("Draft"
1736        ;;["Send" gnus-summary-send-draft t]
1737        ;;["Send bounced" gnus-resend-bounced-mail t])
1738        ))
1739
1740     (easy-menu-define
1741      gnus-summary-misc-menu gnus-summary-mode-map ""
1742      '("Misc"
1743        ("Mark Read"
1744         ["Mark as read" gnus-summary-mark-as-read-forward t]
1745         ["Mark same subject and select"
1746          gnus-summary-kill-same-subject-and-select t]
1747         ["Mark same subject" gnus-summary-kill-same-subject t]
1748         ["Catchup" gnus-summary-catchup t]
1749         ["Catchup all" gnus-summary-catchup-all t]
1750         ["Catchup to here" gnus-summary-catchup-to-here t]
1751         ["Catchup region" gnus-summary-mark-region-as-read t]
1752         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1753        ("Mark Various"
1754         ["Tick" gnus-summary-tick-article-forward t]
1755         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1756         ["Remove marks" gnus-summary-clear-mark-forward t]
1757         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1758         ["Set bookmark" gnus-summary-set-bookmark t]
1759         ["Remove bookmark" gnus-summary-remove-bookmark t])
1760        ("Mark Limit"
1761         ["Marks..." gnus-summary-limit-to-marks t]
1762         ["Subject..." gnus-summary-limit-to-subject t]
1763         ["Author..." gnus-summary-limit-to-author t]
1764         ["Age..." gnus-summary-limit-to-age t]
1765         ["Score" gnus-summary-limit-to-score t]
1766         ["Unread" gnus-summary-limit-to-unread t]
1767         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1768         ["Articles" gnus-summary-limit-to-articles t]
1769         ["Pop limit" gnus-summary-pop-limit t]
1770         ["Show dormant" gnus-summary-limit-include-dormant t]
1771         ["Hide childless dormant"
1772          gnus-summary-limit-exclude-childless-dormant t]
1773         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1774         ["Show expunged" gnus-summary-show-all-expunged t])
1775        ("Process Mark"
1776         ["Set mark" gnus-summary-mark-as-processable t]
1777         ["Remove mark" gnus-summary-unmark-as-processable t]
1778         ["Remove all marks" gnus-summary-unmark-all-processable t]
1779         ["Mark above" gnus-uu-mark-over t]
1780         ["Mark series" gnus-uu-mark-series t]
1781         ["Mark region" gnus-uu-mark-region t]
1782         ["Unmark region" gnus-uu-unmark-region t]
1783         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1784         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1785         ["Mark all" gnus-uu-mark-all t]
1786         ["Mark buffer" gnus-uu-mark-buffer t]
1787         ["Mark sparse" gnus-uu-mark-sparse t]
1788         ["Mark thread" gnus-uu-mark-thread t]
1789         ["Unmark thread" gnus-uu-unmark-thread t]
1790         ("Process Mark Sets"
1791          ["Kill" gnus-summary-kill-process-mark t]
1792          ["Yank" gnus-summary-yank-process-mark
1793           gnus-newsgroup-process-stack]
1794          ["Save" gnus-summary-save-process-mark t]))
1795        ("Scroll article"
1796         ["Page forward" gnus-summary-next-page t]
1797         ["Page backward" gnus-summary-prev-page t]
1798         ["Line forward" gnus-summary-scroll-up t])
1799        ("Move"
1800         ["Next unread article" gnus-summary-next-unread-article t]
1801         ["Previous unread article" gnus-summary-prev-unread-article t]
1802         ["Next article" gnus-summary-next-article t]
1803         ["Previous article" gnus-summary-prev-article t]
1804         ["Next unread subject" gnus-summary-next-unread-subject t]
1805         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1806         ["Next article same subject" gnus-summary-next-same-subject t]
1807         ["Previous article same subject" gnus-summary-prev-same-subject t]
1808         ["First unread article" gnus-summary-first-unread-article t]
1809         ["Best unread article" gnus-summary-best-unread-article t]
1810         ["Go to subject number..." gnus-summary-goto-subject t]
1811         ["Go to article number..." gnus-summary-goto-article t]
1812         ["Go to the last article" gnus-summary-goto-last-article t]
1813         ["Pop article off history" gnus-summary-pop-article t])
1814        ("Sort"
1815         ["Sort by number" gnus-summary-sort-by-number t]
1816         ["Sort by author" gnus-summary-sort-by-author t]
1817         ["Sort by subject" gnus-summary-sort-by-subject t]
1818         ["Sort by date" gnus-summary-sort-by-date t]
1819         ["Sort by score" gnus-summary-sort-by-score t]
1820         ["Sort by lines" gnus-summary-sort-by-lines t])
1821        ("Help"
1822         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1823         ["Describe group" gnus-summary-describe-group t]
1824         ["Read manual" gnus-info-find-node t])
1825        ("Modes"
1826         ["Pick and read" gnus-pick-mode t]
1827         ["Binary" gnus-binary-mode t])
1828        ("Regeneration"
1829         ["Regenerate" gnus-summary-prepare t]
1830         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1831         ["Toggle threading" gnus-summary-toggle-threads t])
1832        ["Filter articles..." gnus-summary-execute-command t]
1833        ["Run command on subjects..." gnus-summary-universal-argument t]
1834        ["Search articles forward..." gnus-summary-search-article-forward t]
1835        ["Search articles backward..." gnus-summary-search-article-backward t]
1836        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1837        ["Expand window" gnus-summary-expand-window t]
1838        ["Expire expirable articles" gnus-summary-expire-articles
1839         (gnus-check-backend-function
1840          'request-expire-articles gnus-newsgroup-name)]
1841        ["Edit local kill file" gnus-summary-edit-local-kill t]
1842        ["Edit main kill file" gnus-summary-edit-global-kill t]
1843        ["Edit group parameters" gnus-summary-edit-parameters t]
1844        ["Customize group parameters" gnus-summary-customize-parameters t]
1845        ["Send a bug report" gnus-bug t]
1846        ("Exit"
1847         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1848         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1849         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1850         ["Exit group" gnus-summary-exit t]
1851         ["Exit group without updating" gnus-summary-exit-no-update t]
1852         ["Exit and goto next group" gnus-summary-next-group t]
1853         ["Exit and goto prev group" gnus-summary-prev-group t]
1854         ["Reselect group" gnus-summary-reselect-current-group t]
1855         ["Rescan group" gnus-summary-rescan-group t]
1856         ["Update dribble" gnus-summary-save-newsrc t])))
1857
1858     (gnus-run-hooks 'gnus-summary-menu-hook)))
1859
1860 (defun gnus-score-set-default (var value)
1861   "A version of set that updates the GNU Emacs menu-bar."
1862   (set var value)
1863   ;; It is the message that forces the active status to be updated.
1864   (message ""))
1865
1866 (defun gnus-make-score-map (type)
1867   "Make a summary score map of type TYPE."
1868   (if t
1869       nil
1870     (let ((headers '(("author" "from" string)
1871                      ("subject" "subject" string)
1872                      ("article body" "body" string)
1873                      ("article head" "head" string)
1874                      ("xref" "xref" string)
1875                      ("extra header" "extra" string)
1876                      ("lines" "lines" number)
1877                      ("followups to author" "followup" string)))
1878           (types '((number ("less than" <)
1879                            ("greater than" >)
1880                            ("equal" =))
1881                    (string ("substring" s)
1882                            ("exact string" e)
1883                            ("fuzzy string" f)
1884                            ("regexp" r))))
1885           (perms '(("temporary" (current-time-string))
1886                    ("permanent" nil)
1887                    ("immediate" now)))
1888           header)
1889       (list
1890        (apply
1891         'nconc
1892         (list
1893          (if (eq type 'lower)
1894              "Lower score"
1895            "Increase score"))
1896         (let (outh)
1897           (while headers
1898             (setq header (car headers))
1899             (setq outh
1900                   (cons
1901                    (apply
1902                     'nconc
1903                     (list (car header))
1904                     (let ((ts (cdr (assoc (nth 2 header) types)))
1905                           outt)
1906                       (while ts
1907                         (setq outt
1908                               (cons
1909                                (apply
1910                                 'nconc
1911                                 (list (caar ts))
1912                                 (let ((ps perms)
1913                                       outp)
1914                                   (while ps
1915                                     (setq outp
1916                                           (cons
1917                                            (vector
1918                                             (caar ps)
1919                                             (list
1920                                              'gnus-summary-score-entry
1921                                              (nth 1 header)
1922                                              (if (or (string= (nth 1 header)
1923                                                               "head")
1924                                                      (string= (nth 1 header)
1925                                                               "body"))
1926                                                  ""
1927                                                (list 'gnus-summary-header
1928                                                      (nth 1 header)))
1929                                              (list 'quote (nth 1 (car ts)))
1930                                              (list 'gnus-score-default nil)
1931                                              (nth 1 (car ps))
1932                                              t)
1933                                             t)
1934                                            outp))
1935                                     (setq ps (cdr ps)))
1936                                   (list (nreverse outp))))
1937                                outt))
1938                         (setq ts (cdr ts)))
1939                       (list (nreverse outt))))
1940                    outh))
1941             (setq headers (cdr headers)))
1942           (list (nreverse outh))))))))
1943
1944 \f
1945
1946 (defun gnus-summary-mode (&optional group)
1947   "Major mode for reading articles.
1948
1949 All normal editing commands are switched off.
1950 \\<gnus-summary-mode-map>
1951 Each line in this buffer represents one article.  To read an
1952 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1953 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1954 respectively.
1955
1956 You can also post articles and send mail from this buffer.  To
1957 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1958 of an article, type `\\[gnus-summary-reply]'.
1959
1960 There are approx. one gazillion commands you can execute in this
1961 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1962
1963 The following commands are available:
1964
1965 \\{gnus-summary-mode-map}"
1966   (interactive)
1967   (when (gnus-visual-p 'summary-menu 'menu)
1968     (gnus-summary-make-menu-bar))
1969   (kill-all-local-variables)
1970   (gnus-summary-make-local-variables)
1971   (gnus-make-thread-indent-array)
1972   (gnus-simplify-mode-line)
1973   (setq major-mode 'gnus-summary-mode)
1974   (setq mode-name "Summary")
1975   (make-local-variable 'minor-mode-alist)
1976   (use-local-map gnus-summary-mode-map)
1977   (buffer-disable-undo)
1978   (setq buffer-read-only t)             ;Disable modification
1979   (setq truncate-lines t)
1980   (setq selective-display t)
1981   (setq selective-display-ellipses t)   ;Display `...'
1982   (gnus-summary-set-display-table)
1983   (gnus-set-default-directory)
1984   (setq gnus-newsgroup-name group)
1985   (make-local-variable 'gnus-summary-line-format)
1986   (make-local-variable 'gnus-summary-line-format-spec)
1987   (make-local-variable 'gnus-summary-dummy-line-format)
1988   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1989   (make-local-variable 'gnus-summary-mark-positions)
1990   (make-local-hook 'post-command-hook)
1991   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1992   (make-local-hook 'pre-command-hook)
1993   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1994   (gnus-run-hooks 'gnus-summary-mode-hook)
1995   (mm-enable-multibyte)
1996   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1997   (gnus-update-summary-mark-positions))
1998
1999 (defun gnus-summary-make-local-variables ()
2000   "Make all the local summary buffer variables."
2001   (let ((locals gnus-summary-local-variables)
2002         global local)
2003     (while (setq local (pop locals))
2004       (if (consp local)
2005           (progn
2006             (if (eq (cdr local) 'global)
2007                 ;; Copy the global value of the variable.
2008                 (setq global (symbol-value (car local)))
2009               ;; Use the value from the list.
2010               (setq global (eval (cdr local))))
2011             (make-local-variable (car local))
2012             (set (car local) global))
2013         ;; Simple nil-valued local variable.
2014         (make-local-variable local)
2015         (set local nil)))))
2016
2017 (defun gnus-summary-clear-local-variables ()
2018   (let ((locals gnus-summary-local-variables))
2019     (while locals
2020       (if (consp (car locals))
2021           (and (vectorp (caar locals))
2022                (set (caar locals) nil))
2023         (and (vectorp (car locals))
2024              (set (car locals) nil)))
2025       (setq locals (cdr locals)))))
2026
2027 ;; Summary data functions.
2028
2029 (defmacro gnus-data-number (data)
2030   `(car ,data))
2031
2032 (defmacro gnus-data-set-number (data number)
2033   `(setcar ,data ,number))
2034
2035 (defmacro gnus-data-mark (data)
2036   `(nth 1 ,data))
2037
2038 (defmacro gnus-data-set-mark (data mark)
2039   `(setcar (nthcdr 1 ,data) ,mark))
2040
2041 (defmacro gnus-data-pos (data)
2042   `(nth 2 ,data))
2043
2044 (defmacro gnus-data-set-pos (data pos)
2045   `(setcar (nthcdr 2 ,data) ,pos))
2046
2047 (defmacro gnus-data-header (data)
2048   `(nth 3 ,data))
2049
2050 (defmacro gnus-data-set-header (data header)
2051   `(setf (nth 3 ,data) ,header))
2052
2053 (defmacro gnus-data-level (data)
2054   `(nth 4 ,data))
2055
2056 (defmacro gnus-data-unread-p (data)
2057   `(= (nth 1 ,data) gnus-unread-mark))
2058
2059 (defmacro gnus-data-read-p (data)
2060   `(/= (nth 1 ,data) gnus-unread-mark))
2061
2062 (defmacro gnus-data-pseudo-p (data)
2063   `(consp (nth 3 ,data)))
2064
2065 (defmacro gnus-data-find (number)
2066   `(assq ,number gnus-newsgroup-data))
2067
2068 (defmacro gnus-data-find-list (number &optional data)
2069   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2070      (memq (assq ,number bdata)
2071            bdata)))
2072
2073 (defmacro gnus-data-make (number mark pos header level)
2074   `(list ,number ,mark ,pos ,header ,level))
2075
2076 (defun gnus-data-enter (after-article number mark pos header level offset)
2077   (let ((data (gnus-data-find-list after-article)))
2078     (unless data
2079       (error "No such article: %d" after-article))
2080     (setcdr data (cons (gnus-data-make number mark pos header level)
2081                        (cdr data)))
2082     (setq gnus-newsgroup-data-reverse nil)
2083     (gnus-data-update-list (cddr data) offset)))
2084
2085 (defun gnus-data-enter-list (after-article list &optional offset)
2086   (when list
2087     (let ((data (and after-article (gnus-data-find-list after-article)))
2088           (ilist list))
2089       (if (not (or data
2090                    after-article))
2091           (let ((odata gnus-newsgroup-data))
2092             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2093             (when offset
2094               (gnus-data-update-list odata offset)))
2095         ;; Find the last element in the list to be spliced into the main
2096         ;; list.
2097         (while (cdr list)
2098           (setq list (cdr list)))
2099         (if (not data)
2100             (progn
2101               (setcdr list gnus-newsgroup-data)
2102               (setq gnus-newsgroup-data ilist)
2103               (when offset
2104                 (gnus-data-update-list (cdr list) offset)))
2105           (setcdr list (cdr data))
2106           (setcdr data ilist)
2107           (when offset
2108             (gnus-data-update-list (cdr list) offset))))
2109       (setq gnus-newsgroup-data-reverse nil))))
2110
2111 (defun gnus-data-remove (article &optional offset)
2112   (let ((data gnus-newsgroup-data))
2113     (if (= (gnus-data-number (car data)) article)
2114         (progn
2115           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2116                 gnus-newsgroup-data-reverse nil)
2117           (when offset
2118             (gnus-data-update-list gnus-newsgroup-data offset)))
2119       (while (cdr data)
2120         (when (= (gnus-data-number (cadr data)) article)
2121           (setcdr data (cddr data))
2122           (when offset
2123             (gnus-data-update-list (cdr data) offset))
2124           (setq data nil
2125                 gnus-newsgroup-data-reverse nil))
2126         (setq data (cdr data))))))
2127
2128 (defmacro gnus-data-list (backward)
2129   `(if ,backward
2130        (or gnus-newsgroup-data-reverse
2131            (setq gnus-newsgroup-data-reverse
2132                  (reverse gnus-newsgroup-data)))
2133      gnus-newsgroup-data))
2134
2135 (defun gnus-data-update-list (data offset)
2136   "Add OFFSET to the POS of all data entries in DATA."
2137   (setq gnus-newsgroup-data-reverse nil)
2138   (while data
2139     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2140     (setq data (cdr data))))
2141
2142 (defun gnus-summary-article-pseudo-p (article)
2143   "Say whether this article is a pseudo article or not."
2144   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2145
2146 (defmacro gnus-summary-article-sparse-p (article)
2147   "Say whether this article is a sparse article or not."
2148   `(memq ,article gnus-newsgroup-sparse))
2149
2150 (defmacro gnus-summary-article-ancient-p (article)
2151   "Say whether this article is a sparse article or not."
2152   `(memq ,article gnus-newsgroup-ancient))
2153
2154 (defun gnus-article-parent-p (number)
2155   "Say whether this article is a parent or not."
2156   (let ((data (gnus-data-find-list number)))
2157     (and (cdr data)                     ; There has to be an article after...
2158          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2159             (gnus-data-level (nth 1 data))))))
2160
2161 (defun gnus-article-children (number)
2162   "Return a list of all children to NUMBER."
2163   (let* ((data (gnus-data-find-list number))
2164          (level (gnus-data-level (car data)))
2165          children)
2166     (setq data (cdr data))
2167     (while (and data
2168                 (= (gnus-data-level (car data)) (1+ level)))
2169       (push (gnus-data-number (car data)) children)
2170       (setq data (cdr data)))
2171     children))
2172
2173 (defmacro gnus-summary-skip-intangible ()
2174   "If the current article is intangible, then jump to a different article."
2175   '(let ((to (get-text-property (point) 'gnus-intangible)))
2176      (and to (gnus-summary-goto-subject to))))
2177
2178 (defmacro gnus-summary-article-intangible-p ()
2179   "Say whether this article is intangible or not."
2180   '(get-text-property (point) 'gnus-intangible))
2181
2182 (defun gnus-article-read-p (article)
2183   "Say whether ARTICLE is read or not."
2184   (not (or (memq article gnus-newsgroup-marked)
2185            (memq article gnus-newsgroup-unreads)
2186            (memq article gnus-newsgroup-unselected)
2187            (memq article gnus-newsgroup-dormant))))
2188
2189 ;; Some summary mode macros.
2190
2191 (defmacro gnus-summary-article-number ()
2192   "The article number of the article on the current line.
2193 If there isn's an article number here, then we return the current
2194 article number."
2195   '(progn
2196      (gnus-summary-skip-intangible)
2197      (or (get-text-property (point) 'gnus-number)
2198          (gnus-summary-last-subject))))
2199
2200 (defmacro gnus-summary-article-header (&optional number)
2201   "Return the header of article NUMBER."
2202   `(gnus-data-header (gnus-data-find
2203                       ,(or number '(gnus-summary-article-number)))))
2204
2205 (defmacro gnus-summary-thread-level (&optional number)
2206   "Return the level of thread that starts with article NUMBER."
2207   `(if (and (eq gnus-summary-make-false-root 'dummy)
2208             (get-text-property (point) 'gnus-intangible))
2209        0
2210      (gnus-data-level (gnus-data-find
2211                        ,(or number '(gnus-summary-article-number))))))
2212
2213 (defmacro gnus-summary-article-mark (&optional number)
2214   "Return the mark of article NUMBER."
2215   `(gnus-data-mark (gnus-data-find
2216                     ,(or number '(gnus-summary-article-number)))))
2217
2218 (defmacro gnus-summary-article-pos (&optional number)
2219   "Return the position of the line of article NUMBER."
2220   `(gnus-data-pos (gnus-data-find
2221                    ,(or number '(gnus-summary-article-number)))))
2222
2223 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2224 (defmacro gnus-summary-article-subject (&optional number)
2225   "Return current subject string or nil if nothing."
2226   `(let ((headers
2227           ,(if number
2228                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2229              '(gnus-data-header (assq (gnus-summary-article-number)
2230                                       gnus-newsgroup-data)))))
2231      (and headers
2232           (vectorp headers)
2233           (mail-header-subject headers))))
2234
2235 (defmacro gnus-summary-article-score (&optional number)
2236   "Return current article score."
2237   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2238                   gnus-newsgroup-scored))
2239        gnus-summary-default-score 0))
2240
2241 (defun gnus-summary-article-children (&optional number)
2242   "Return a list of article numbers that are children of article NUMBER."
2243   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2244          (level (gnus-data-level (car data)))
2245          l children)
2246     (while (and (setq data (cdr data))
2247                 (> (setq l (gnus-data-level (car data))) level))
2248       (and (= (1+ level) l)
2249            (push (gnus-data-number (car data))
2250                  children)))
2251     (nreverse children)))
2252
2253 (defun gnus-summary-article-parent (&optional number)
2254   "Return the article number of the parent of article NUMBER."
2255   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2256                                     (gnus-data-list t)))
2257          (level (gnus-data-level (car data))))
2258     (if (zerop level)
2259         ()                              ; This is a root.
2260       ;; We search until we find an article with a level less than
2261       ;; this one.  That function has to be the parent.
2262       (while (and (setq data (cdr data))
2263                   (not (< (gnus-data-level (car data)) level))))
2264       (and data (gnus-data-number (car data))))))
2265
2266 (defun gnus-unread-mark-p (mark)
2267   "Say whether MARK is the unread mark."
2268   (= mark gnus-unread-mark))
2269
2270 (defun gnus-read-mark-p (mark)
2271   "Say whether MARK is one of the marks that mark as read.
2272 This is all marks except unread, ticked, dormant, and expirable."
2273   (not (or (= mark gnus-unread-mark)
2274            (= mark gnus-ticked-mark)
2275            (= mark gnus-dormant-mark)
2276            (= mark gnus-expirable-mark))))
2277
2278 (defmacro gnus-article-mark (number)
2279   "Return the MARK of article NUMBER.
2280 This macro should only be used when computing the mark the \"first\"
2281 time; i.e., when generating the summary lines.  After that,
2282 `gnus-summary-article-mark' should be used to examine the
2283 marks of articles."
2284   `(cond
2285     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2286     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2287     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2288     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2289     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2290     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2291     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2292     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2293            gnus-ancient-mark))))
2294
2295 ;; Saving hidden threads.
2296
2297 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2298 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2299
2300 (defmacro gnus-save-hidden-threads (&rest forms)
2301   "Save hidden threads, eval FORMS, and restore the hidden threads."
2302   (let ((config (make-symbol "config")))
2303     `(let ((,config (gnus-hidden-threads-configuration)))
2304        (unwind-protect
2305            (save-excursion
2306              ,@forms)
2307          (gnus-restore-hidden-threads-configuration ,config)))))
2308
2309 (defun gnus-data-compute-positions ()
2310   "Compute the positions of all articles."
2311   (setq gnus-newsgroup-data-reverse nil)
2312   (let ((data gnus-newsgroup-data))
2313     (save-excursion
2314       (gnus-save-hidden-threads
2315         (gnus-summary-show-all-threads)
2316         (goto-char (point-min))
2317         (while data
2318           (while (get-text-property (point) 'gnus-intangible)
2319             (forward-line 1))
2320           (gnus-data-set-pos (car data) (+ (point) 3))
2321           (setq data (cdr data))
2322           (forward-line 1))))))
2323
2324 (defun gnus-hidden-threads-configuration ()
2325   "Return the current hidden threads configuration."
2326   (save-excursion
2327     (let (config)
2328       (goto-char (point-min))
2329       (while (search-forward "\r" nil t)
2330         (push (1- (point)) config))
2331       config)))
2332
2333 (defun gnus-restore-hidden-threads-configuration (config)
2334   "Restore hidden threads configuration from CONFIG."
2335   (let (point buffer-read-only)
2336     (while (setq point (pop config))
2337       (when (and (< point (point-max))
2338                  (goto-char point)
2339                  (eq (char-after) ?\n))
2340         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2341
2342 ;; Various summary mode internalish functions.
2343
2344 (defun gnus-mouse-pick-article (e)
2345   (interactive "e")
2346   (mouse-set-point e)
2347   (gnus-summary-next-page nil t))
2348
2349 (defun gnus-summary-set-display-table ()
2350   ;; Change the display table.  Odd characters have a tendency to mess
2351   ;; up nicely formatted displays - we make all possible glyphs
2352   ;; display only a single character.
2353
2354   ;; We start from the standard display table, if any.
2355   (let ((table (or (copy-sequence standard-display-table)
2356                    (make-display-table)))
2357         (i 32))
2358     ;; Nix out all the control chars...
2359     (while (>= (setq i (1- i)) 0)
2360       (aset table i [??]))
2361     ;; ... but not newline and cr, of course.  (cr is necessary for the
2362     ;; selective display).
2363     (aset table ?\n nil)
2364     (aset table ?\r nil)
2365     ;; We keep TAB as well.
2366     (aset table ?\t nil)
2367     ;; We nix out any glyphs over 126 that are not set already.
2368     (let ((i 256))
2369       (while (>= (setq i (1- i)) 127)
2370         ;; Only modify if the entry is nil.
2371         (unless (aref table i)
2372           (aset table i [??]))))
2373     (setq buffer-display-table table)))
2374
2375 (defun gnus-summary-setup-buffer (group)
2376   "Initialize summary buffer."
2377   (let ((buffer (concat "*Summary " group "*")))
2378     (if (get-buffer buffer)
2379         (progn
2380           (set-buffer buffer)
2381           (setq gnus-summary-buffer (current-buffer))
2382           (not gnus-newsgroup-prepared))
2383       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2384       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2385       (gnus-summary-mode group)
2386       (when gnus-carpal
2387         (gnus-carpal-setup-buffer 'summary))
2388       (unless gnus-single-article-buffer
2389         (make-local-variable 'gnus-article-buffer)
2390         (make-local-variable 'gnus-article-current)
2391         (make-local-variable 'gnus-original-article-buffer))
2392       (setq gnus-newsgroup-name group)
2393       t)))
2394
2395 (defun gnus-set-global-variables ()
2396   ;; Set the global equivalents of the summary buffer-local variables
2397   ;; to the latest values they had.  These reflect the summary buffer
2398   ;; that was in action when the last article was fetched.
2399   (when (eq major-mode 'gnus-summary-mode)
2400     (setq gnus-summary-buffer (current-buffer))
2401     (let ((name gnus-newsgroup-name)
2402           (marked gnus-newsgroup-marked)
2403           (unread gnus-newsgroup-unreads)
2404           (headers gnus-current-headers)
2405           (data gnus-newsgroup-data)
2406           (summary gnus-summary-buffer)
2407           (article-buffer gnus-article-buffer)
2408           (original gnus-original-article-buffer)
2409           (gac gnus-article-current)
2410           (reffed gnus-reffed-article-number)
2411           (score-file gnus-current-score-file)
2412           (default-charset gnus-newsgroup-default-charset)
2413           (iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2414       (save-excursion
2415         (set-buffer gnus-group-buffer)
2416         (setq gnus-newsgroup-name name
2417               gnus-newsgroup-marked marked
2418               gnus-newsgroup-unreads unread
2419               gnus-current-headers headers
2420               gnus-newsgroup-data data
2421               gnus-article-current gac
2422               gnus-summary-buffer summary
2423               gnus-article-buffer article-buffer
2424               gnus-original-article-buffer original
2425               gnus-reffed-article-number reffed
2426               gnus-current-score-file score-file
2427               gnus-newsgroup-default-charset default-charset
2428               gnus-newsgroup-iso-8859-1-forced iso-8859-1-forced)
2429         ;; The article buffer also has local variables.
2430         (when (gnus-buffer-live-p gnus-article-buffer)
2431           (set-buffer gnus-article-buffer)
2432           (setq gnus-summary-buffer summary))))))
2433
2434 (defun gnus-summary-article-unread-p (article)
2435   "Say whether ARTICLE is unread or not."
2436   (memq article gnus-newsgroup-unreads))
2437
2438 (defun gnus-summary-first-article-p (&optional article)
2439   "Return whether ARTICLE is the first article in the buffer."
2440   (if (not (setq article (or article (gnus-summary-article-number))))
2441       nil
2442     (eq article (caar gnus-newsgroup-data))))
2443
2444 (defun gnus-summary-last-article-p (&optional article)
2445   "Return whether ARTICLE is the last article in the buffer."
2446   (if (not (setq article (or article (gnus-summary-article-number))))
2447       t                                 ; All non-existent numbers are the last article.  :-)
2448     (not (cdr (gnus-data-find-list article)))))
2449
2450 (defun gnus-make-thread-indent-array ()
2451   (let ((n 200))
2452     (unless (and gnus-thread-indent-array
2453                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2454       (setq gnus-thread-indent-array (make-vector 201 "")
2455             gnus-thread-indent-array-level gnus-thread-indent-level)
2456       (while (>= n 0)
2457         (aset gnus-thread-indent-array n
2458               (make-string (* n gnus-thread-indent-level) ? ))
2459         (setq n (1- n))))))
2460
2461 (defun gnus-update-summary-mark-positions ()
2462   "Compute where the summary marks are to go."
2463   (save-excursion
2464     (when (gnus-buffer-exists-p gnus-summary-buffer)
2465       (set-buffer gnus-summary-buffer))
2466     (let ((gnus-replied-mark 129)
2467           (gnus-score-below-mark 130)
2468           (gnus-score-over-mark 130)
2469           (gnus-download-mark 131)
2470           (spec gnus-summary-line-format-spec)
2471           gnus-visual pos)
2472       (save-excursion
2473         (gnus-set-work-buffer)
2474         (let ((gnus-summary-line-format-spec spec)
2475               (gnus-newsgroup-downloadable '((0 . t))))
2476           (gnus-summary-insert-line
2477            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2478           (goto-char (point-min))
2479           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2480                                              (- (point) 2)))))
2481           (goto-char (point-min))
2482           (push (cons 'replied (and (search-forward "\201" nil t)
2483                                     (- (point) 2)))
2484                 pos)
2485           (goto-char (point-min))
2486           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2487                 pos)
2488           (goto-char (point-min))
2489           (push (cons 'download
2490                       (and (search-forward "\203" nil t) (- (point) 2)))
2491                 pos)))
2492       (setq gnus-summary-mark-positions pos))))
2493
2494 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2495   "Insert a dummy root in the summary buffer."
2496   (beginning-of-line)
2497   (gnus-add-text-properties
2498    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2499    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2500
2501 (defun gnus-summary-from-or-to-or-newsgroups (header)
2502   (let ((to (cdr (assq 'To (mail-header-extra header))))
2503         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2504         (rfc2047-default-charset gnus-newsgroup-default-charset)
2505         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2506     (cond
2507      ((and to
2508            gnus-ignored-from-addresses
2509            (string-match gnus-ignored-from-addresses
2510                          (mail-header-from header)))
2511       (concat "-> "
2512               (or (car (funcall gnus-extract-address-components
2513                                 (funcall
2514                                  gnus-decode-encoded-word-function to)))
2515                   (funcall gnus-decode-encoded-word-function to))))
2516      ((and newsgroups
2517            gnus-ignored-from-addresses
2518            (string-match gnus-ignored-from-addresses
2519                          (mail-header-from header)))
2520       (concat "=> " newsgroups))
2521      (t
2522       (or (car (funcall gnus-extract-address-components
2523                         (mail-header-from header)))
2524           (mail-header-from header))))))
2525
2526 (defun gnus-summary-insert-line (gnus-tmp-header
2527                                  gnus-tmp-level gnus-tmp-current
2528                                  gnus-tmp-unread gnus-tmp-replied
2529                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2530                                  &optional gnus-tmp-dummy gnus-tmp-score
2531                                  gnus-tmp-process)
2532   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2533          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2534          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2535          (gnus-tmp-score-char
2536           (if (or (null gnus-summary-default-score)
2537                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2538                       gnus-summary-zcore-fuzz))
2539               ? 
2540             (if (< gnus-tmp-score gnus-summary-default-score)
2541                 gnus-score-below-mark gnus-score-over-mark)))
2542          (gnus-tmp-replied
2543           (cond (gnus-tmp-process gnus-process-mark)
2544                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2545                  gnus-cached-mark)
2546                 (gnus-tmp-replied gnus-replied-mark)
2547                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2548                  gnus-saved-mark)
2549                 (t gnus-unread-mark)))
2550          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2551          (gnus-tmp-name
2552           (cond
2553            ((string-match "<[^>]+> *$" gnus-tmp-from)
2554             (let ((beg (match-beginning 0)))
2555               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2556                        (substring gnus-tmp-from (1+ (match-beginning 0))
2557                                   (1- (match-end 0))))
2558                   (substring gnus-tmp-from 0 beg))))
2559            ((string-match "(.+)" gnus-tmp-from)
2560             (substring gnus-tmp-from
2561                        (1+ (match-beginning 0)) (1- (match-end 0))))
2562            (t gnus-tmp-from)))
2563          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2564          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2565          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2566          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2567          (buffer-read-only nil))
2568     (when (string= gnus-tmp-name "")
2569       (setq gnus-tmp-name gnus-tmp-from))
2570     (unless (numberp gnus-tmp-lines)
2571       (setq gnus-tmp-lines 0))
2572     (gnus-put-text-property
2573      (point)
2574      (progn (eval gnus-summary-line-format-spec) (point))
2575      'gnus-number gnus-tmp-number)
2576     (when (gnus-visual-p 'summary-highlight 'highlight)
2577       (forward-line -1)
2578       (gnus-run-hooks 'gnus-summary-update-hook)
2579       (forward-line 1))))
2580
2581 (defun gnus-summary-update-line (&optional dont-update)
2582   ;; Update summary line after change.
2583   (when (and gnus-summary-default-score
2584              (not gnus-summary-inhibit-highlight))
2585     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2586            (article (gnus-summary-article-number))
2587            (score (gnus-summary-article-score article)))
2588       (unless dont-update
2589         (if (and gnus-summary-mark-below
2590                  (< (gnus-summary-article-score)
2591                     gnus-summary-mark-below))
2592             ;; This article has a low score, so we mark it as read.
2593             (when (memq article gnus-newsgroup-unreads)
2594               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2595           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2596             ;; This article was previously marked as read on account
2597             ;; of a low score, but now it has risen, so we mark it as
2598             ;; unread.
2599             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2600         (gnus-summary-update-mark
2601          (if (or (null gnus-summary-default-score)
2602                  (<= (abs (- score gnus-summary-default-score))
2603                      gnus-summary-zcore-fuzz))
2604              ? 
2605            (if (< score gnus-summary-default-score)
2606                gnus-score-below-mark gnus-score-over-mark))
2607          'score))
2608       ;; Do visual highlighting.
2609       (when (gnus-visual-p 'summary-highlight 'highlight)
2610         (gnus-run-hooks 'gnus-summary-update-hook)))))
2611
2612 (defvar gnus-tmp-new-adopts nil)
2613
2614 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2615   "Return the number of articles in THREAD.
2616 This may be 0 in some cases -- if none of the articles in
2617 the thread are to be displayed."
2618   (let* ((number
2619           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2620           (cond
2621            ((not (listp thread))
2622             1)
2623            ((and (consp thread) (cdr thread))
2624             (apply
2625              '+ 1 (mapcar
2626                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2627            ((null thread)
2628             1)
2629            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2630             1)
2631            (t 0))))
2632     (when (and level (zerop level) gnus-tmp-new-adopts)
2633       (incf number
2634             (apply '+ (mapcar
2635                        'gnus-summary-number-of-articles-in-thread
2636                        gnus-tmp-new-adopts))))
2637     (if char
2638         (if (> number 1) gnus-not-empty-thread-mark
2639           gnus-empty-thread-mark)
2640       number)))
2641
2642 (defun gnus-summary-set-local-parameters (group)
2643   "Go through the local params of GROUP and set all variable specs in that list."
2644   (let ((params (gnus-group-find-parameter group))
2645         elem)
2646     (while params
2647       (setq elem (car params)
2648             params (cdr params))
2649       (and (consp elem)                 ; Has to be a cons.
2650            (consp (cdr elem))           ; The cdr has to be a list.
2651            (symbolp (car elem))         ; Has to be a symbol in there.
2652            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2653            (ignore-errors               ; So we set it.
2654              (make-local-variable (car elem))
2655              (set (car elem) (eval (nth 1 elem))))))))
2656
2657 (defun gnus-summary-read-group (group &optional show-all no-article
2658                                       kill-buffer no-display backward
2659                                       select-articles)
2660   "Start reading news in newsgroup GROUP.
2661 If SHOW-ALL is non-nil, already read articles are also listed.
2662 If NO-ARTICLE is non-nil, no article is selected initially.
2663 If NO-DISPLAY, don't generate a summary buffer."
2664   (let (result)
2665     (while (and group
2666                 (null (setq result
2667                             (let ((gnus-auto-select-next nil))
2668                               (or (gnus-summary-read-group-1
2669                                    group show-all no-article
2670                                    kill-buffer no-display
2671                                    select-articles)
2672                                   (setq show-all nil
2673                                         select-articles nil)))))
2674                 (eq gnus-auto-select-next 'quietly))
2675       (set-buffer gnus-group-buffer)
2676       ;; The entry function called above goes to the next
2677       ;; group automatically, so we go two groups back
2678       ;; if we are searching for the previous group.
2679       (when backward
2680         (gnus-group-prev-unread-group 2))
2681       (if (not (equal group (gnus-group-group-name)))
2682           (setq group (gnus-group-group-name))
2683         (setq group nil)))
2684     result))
2685
2686 (defun gnus-summary-read-group-1 (group show-all no-article
2687                                         kill-buffer no-display
2688                                         &optional select-articles)
2689   ;; Killed foreign groups can't be entered.
2690   (when (and (not (gnus-group-native-p group))
2691              (not (gnus-gethash group gnus-newsrc-hashtb)))
2692     (error "Dead non-native groups can't be entered"))
2693   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2694   (let* ((new-group (gnus-summary-setup-buffer group))
2695          (quit-config (gnus-group-quit-config group))
2696          (did-select (and new-group (gnus-select-newsgroup
2697                                      group show-all select-articles))))
2698     (cond
2699      ;; This summary buffer exists already, so we just select it.
2700      ((not new-group)
2701       (gnus-set-global-variables)
2702       (when kill-buffer
2703         (gnus-kill-or-deaden-summary kill-buffer))
2704       (gnus-configure-windows 'summary 'force)
2705       (gnus-set-mode-line 'summary)
2706       (gnus-summary-position-point)
2707       (message "")
2708       t)
2709      ;; We couldn't select this group.
2710      ((null did-select)
2711       (when (and (eq major-mode 'gnus-summary-mode)
2712                  (not (equal (current-buffer) kill-buffer)))
2713         (kill-buffer (current-buffer))
2714         (if (not quit-config)
2715             (progn
2716               ;; Update the info -- marks might need to be removed,
2717               ;; for instance.
2718               (gnus-summary-update-info)
2719               (set-buffer gnus-group-buffer)
2720               (gnus-group-jump-to-group group)
2721               (gnus-group-next-unread-group 1))
2722           (gnus-handle-ephemeral-exit quit-config)))
2723       (gnus-message 3 "Can't select group")
2724       nil)
2725      ;; The user did a `C-g' while prompting for number of articles,
2726      ;; so we exit this group.
2727      ((eq did-select 'quit)
2728       (and (eq major-mode 'gnus-summary-mode)
2729            (not (equal (current-buffer) kill-buffer))
2730            (kill-buffer (current-buffer)))
2731       (when kill-buffer
2732         (gnus-kill-or-deaden-summary kill-buffer))
2733       (if (not quit-config)
2734           (progn
2735             (set-buffer gnus-group-buffer)
2736             (gnus-group-jump-to-group group)
2737             (gnus-group-next-unread-group 1)
2738             (gnus-configure-windows 'group 'force))
2739         (gnus-handle-ephemeral-exit quit-config))
2740       ;; Finally signal the quit.
2741       (signal 'quit nil))
2742      ;; The group was successfully selected.
2743      (t
2744       (gnus-set-global-variables)
2745       ;; Save the active value in effect when the group was entered.
2746       (setq gnus-newsgroup-active
2747             (gnus-copy-sequence
2748              (gnus-active gnus-newsgroup-name)))
2749       ;; You can change the summary buffer in some way with this hook.
2750       (gnus-run-hooks 'gnus-select-group-hook)
2751       ;; Set any local variables in the group parameters.
2752       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2753       (gnus-update-format-specifications
2754        nil 'summary 'summary-mode 'summary-dummy)
2755       ;; Do score processing.
2756       (when gnus-use-scoring
2757         (gnus-possibly-score-headers))
2758       ;; Check whether to fill in the gaps in the threads.
2759       (when gnus-build-sparse-threads
2760         (gnus-build-sparse-threads))
2761       ;; Find the initial limit.
2762       (if gnus-show-threads
2763           (if show-all
2764               (let ((gnus-newsgroup-dormant nil))
2765                 (gnus-summary-initial-limit show-all))
2766             (gnus-summary-initial-limit show-all))
2767         (setq gnus-newsgroup-limit
2768               (mapcar
2769                (lambda (header) (mail-header-number header))
2770                gnus-newsgroup-headers)))
2771       ;; Generate the summary buffer.
2772       (unless no-display
2773         (gnus-summary-prepare))
2774       (when gnus-use-trees
2775         (gnus-tree-open group)
2776         (setq gnus-summary-highlight-line-function
2777               'gnus-tree-highlight-article))
2778       ;; If the summary buffer is empty, but there are some low-scored
2779       ;; articles or some excluded dormants, we include these in the
2780       ;; buffer.
2781       (when (and (zerop (buffer-size))
2782                  (not no-display))
2783         (cond (gnus-newsgroup-dormant
2784                (gnus-summary-limit-include-dormant))
2785               ((and gnus-newsgroup-scored show-all)
2786                (gnus-summary-limit-include-expunged t))))
2787       ;; Function `gnus-apply-kill-file' must be called in this hook.
2788       (gnus-run-hooks 'gnus-apply-kill-hook)
2789       (if (and (zerop (buffer-size))
2790                (not no-display))
2791           (progn
2792             ;; This newsgroup is empty.
2793             (gnus-summary-catchup-and-exit nil t)
2794             (gnus-message 6 "No unread news")
2795             (when kill-buffer
2796               (gnus-kill-or-deaden-summary kill-buffer))
2797             ;; Return nil from this function.
2798             nil)
2799         ;; Hide conversation thread subtrees.  We cannot do this in
2800         ;; gnus-summary-prepare-hook since kill processing may not
2801         ;; work with hidden articles.
2802         (and gnus-show-threads
2803              gnus-thread-hide-subtree
2804              (gnus-summary-hide-all-threads))
2805         (when kill-buffer
2806           (gnus-kill-or-deaden-summary kill-buffer))
2807         ;; Show first unread article if requested.
2808         (if (and (not no-article)
2809                  (not no-display)
2810                  gnus-newsgroup-unreads
2811                  gnus-auto-select-first)
2812             (progn
2813               (gnus-configure-windows 'summary)
2814               (cond
2815                ((eq gnus-auto-select-first 'best)
2816                 (gnus-summary-best-unread-article))
2817                ((eq gnus-auto-select-first t)
2818                 (gnus-summary-first-unread-article))
2819                ((gnus-functionp gnus-auto-select-first)
2820                 (funcall gnus-auto-select-first))))
2821           ;; Don't select any articles, just move point to the first
2822           ;; article in the group.
2823           (goto-char (point-min))
2824           (gnus-summary-position-point)
2825           (gnus-configure-windows 'summary 'force)
2826           (gnus-set-mode-line 'summary))
2827         (when (get-buffer-window gnus-group-buffer t)
2828           ;; Gotta use windows, because recenter does weird stuff if
2829           ;; the current buffer ain't the displayed window.
2830           (let ((owin (selected-window)))
2831             (select-window (get-buffer-window gnus-group-buffer t))
2832             (when (gnus-group-goto-group group)
2833               (recenter))
2834             (select-window owin)))
2835         ;; Mark this buffer as "prepared".
2836         (setq gnus-newsgroup-prepared t)
2837         (gnus-run-hooks 'gnus-summary-prepared-hook)
2838         t)))))
2839
2840 (defun gnus-summary-prepare ()
2841   "Generate the summary buffer."
2842   (interactive)
2843   (let ((buffer-read-only nil))
2844     (erase-buffer)
2845     (setq gnus-newsgroup-data nil
2846           gnus-newsgroup-data-reverse nil)
2847     (gnus-run-hooks 'gnus-summary-generate-hook)
2848     ;; Generate the buffer, either with threads or without.
2849     (when gnus-newsgroup-headers
2850       (gnus-summary-prepare-threads
2851        (if gnus-show-threads
2852            (gnus-sort-gathered-threads
2853             (funcall gnus-summary-thread-gathering-function
2854                      (gnus-sort-threads
2855                       (gnus-cut-threads (gnus-make-threads)))))
2856          ;; Unthreaded display.
2857          (gnus-sort-articles gnus-newsgroup-headers))))
2858     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2859     ;; Call hooks for modifying summary buffer.
2860     (goto-char (point-min))
2861     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2862
2863 (defsubst gnus-general-simplify-subject (subject)
2864   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2865   (setq subject
2866         (cond
2867          ;; Truncate the subject.
2868          (gnus-simplify-subject-functions
2869           (gnus-map-function gnus-simplify-subject-functions subject))
2870          ((numberp gnus-summary-gather-subject-limit)
2871           (setq subject (gnus-simplify-subject-re subject))
2872           (if (> (length subject) gnus-summary-gather-subject-limit)
2873               (substring subject 0 gnus-summary-gather-subject-limit)
2874             subject))
2875          ;; Fuzzily simplify it.
2876          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2877           (gnus-simplify-subject-fuzzy subject))
2878          ;; Just remove the leading "Re:".
2879          (t
2880           (gnus-simplify-subject-re subject))))
2881
2882   (if (and gnus-summary-gather-exclude-subject
2883            (string-match gnus-summary-gather-exclude-subject subject))
2884       nil                               ; This article shouldn't be gathered
2885     subject))
2886
2887 (defun gnus-summary-simplify-subject-query ()
2888   "Query where the respool algorithm would put this article."
2889   (interactive)
2890   (gnus-summary-select-article)
2891   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2892
2893 (defun gnus-gather-threads-by-subject (threads)
2894   "Gather threads by looking at Subject headers."
2895   (if (not gnus-summary-make-false-root)
2896       threads
2897     (let ((hashtb (gnus-make-hashtable 1024))
2898           (prev threads)
2899           (result threads)
2900           subject hthread whole-subject)
2901       (while threads
2902         (setq subject (gnus-general-simplify-subject
2903                        (setq whole-subject (mail-header-subject
2904                                             (caar threads)))))
2905         (when subject
2906           (if (setq hthread (gnus-gethash subject hashtb))
2907               (progn
2908                 ;; We enter a dummy root into the thread, if we
2909                 ;; haven't done that already.
2910                 (unless (stringp (caar hthread))
2911                   (setcar hthread (list whole-subject (car hthread))))
2912                 ;; We add this new gathered thread to this gathered
2913                 ;; thread.
2914                 (setcdr (car hthread)
2915                         (nconc (cdar hthread) (list (car threads))))
2916                 ;; Remove it from the list of threads.
2917                 (setcdr prev (cdr threads))
2918                 (setq threads prev))
2919             ;; Enter this thread into the hash table.
2920             (gnus-sethash subject threads hashtb)))
2921         (setq prev threads)
2922         (setq threads (cdr threads)))
2923       result)))
2924
2925 (defun gnus-gather-threads-by-references (threads)
2926   "Gather threads by looking at References headers."
2927   (let ((idhashtb (gnus-make-hashtable 1024))
2928         (thhashtb (gnus-make-hashtable 1024))
2929         (prev threads)
2930         (result threads)
2931         ids references id gthread gid entered ref)
2932     (while threads
2933       (when (setq references (mail-header-references (caar threads)))
2934         (setq id (mail-header-id (caar threads))
2935               ids (gnus-split-references references)
2936               entered nil)
2937         (while (setq ref (pop ids))
2938           (setq ids (delete ref ids))
2939           (if (not (setq gid (gnus-gethash ref idhashtb)))
2940               (progn
2941                 (gnus-sethash ref id idhashtb)
2942                 (gnus-sethash id threads thhashtb))
2943             (setq gthread (gnus-gethash gid thhashtb))
2944             (unless entered
2945               ;; We enter a dummy root into the thread, if we
2946               ;; haven't done that already.
2947               (unless (stringp (caar gthread))
2948                 (setcar gthread (list (mail-header-subject (caar gthread))
2949                                       (car gthread))))
2950               ;; We add this new gathered thread to this gathered
2951               ;; thread.
2952               (setcdr (car gthread)
2953                       (nconc (cdar gthread) (list (car threads)))))
2954             ;; Add it into the thread hash table.
2955             (gnus-sethash id gthread thhashtb)
2956             (setq entered t)
2957             ;; Remove it from the list of threads.
2958             (setcdr prev (cdr threads))
2959             (setq threads prev))))
2960       (setq prev threads)
2961       (setq threads (cdr threads)))
2962     result))
2963
2964 (defun gnus-sort-gathered-threads (threads)
2965   "Sort subtreads inside each gathered thread by article number."
2966   (let ((result threads))
2967     (while threads
2968       (when (stringp (caar threads))
2969         (setcdr (car threads)
2970                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2971       (setq threads (cdr threads)))
2972     result))
2973
2974 (defun gnus-thread-loop-p (root thread)
2975   "Say whether ROOT is in THREAD."
2976   (let ((stack (list thread))
2977         (infloop 0)
2978         th)
2979     (while (setq thread (pop stack))
2980       (setq th (cdr thread))
2981       (while (and th
2982                   (not (eq (caar th) root)))
2983         (pop th))
2984       (if th
2985           ;; We have found a loop.
2986           (let (ref-dep)
2987             (setcdr thread (delq (car th) (cdr thread)))
2988             (if (boundp (setq ref-dep (intern "none"
2989                                               gnus-newsgroup-dependencies)))
2990                 (setcdr (symbol-value ref-dep)
2991                         (nconc (cdr (symbol-value ref-dep))
2992                                (list (car th))))
2993               (set ref-dep (list nil (car th))))
2994             (setq infloop 1
2995                   stack nil))
2996         ;; Push all the subthreads onto the stack.
2997         (push (cdr thread) stack)))
2998     infloop))
2999
3000 (defun gnus-make-threads ()
3001   "Go through the dependency hashtb and find the roots.  Return all threads."
3002   (let (threads)
3003     (while (catch 'infloop
3004              (mapatoms
3005               (lambda (refs)
3006                 ;; Deal with self-referencing References loops.
3007                 (when (and (car (symbol-value refs))
3008                            (not (zerop
3009                                  (apply
3010                                   '+
3011                                   (mapcar
3012                                    (lambda (thread)
3013                                      (gnus-thread-loop-p
3014                                       (car (symbol-value refs)) thread))
3015                                    (cdr (symbol-value refs)))))))
3016                   (setq threads nil)
3017                   (throw 'infloop t))
3018                 (unless (car (symbol-value refs))
3019                   ;; These threads do not refer back to any other articles,
3020                   ;; so they're roots.
3021                   (setq threads (append (cdr (symbol-value refs)) threads))))
3022               gnus-newsgroup-dependencies)))
3023     threads))
3024
3025 ;; Build the thread tree.
3026 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3027   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3028
3029 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3030 if it was already present.
3031
3032 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3033 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3034 Message-IDs will be renamed be renamed to a unique Message-ID before
3035 being entered.
3036
3037 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3038   (let* ((id (mail-header-id header))
3039          (id-dep (and id (intern id dependencies)))
3040          ref ref-dep ref-header)
3041     ;; Enter this `header' in the `dependencies' table.
3042     (cond
3043      ((not id-dep)
3044       (setq header nil))
3045      ;; The first two cases do the normal part: enter a new `header'
3046      ;; in the `dependencies' table.
3047      ((not (boundp id-dep))
3048       (set id-dep (list header)))
3049      ((null (car (symbol-value id-dep)))
3050       (setcar (symbol-value id-dep) header))
3051
3052      ;; From here the `header' was already present in the
3053      ;; `dependencies' table.
3054      (force-new
3055       ;; Overrides an existing entry;
3056       ;; just set the header part of the entry.
3057       (setcar (symbol-value id-dep) header))
3058
3059      ;; Renames the existing `header' to a unique Message-ID.
3060      ((not gnus-summary-ignore-duplicates)
3061       ;; An article with this Message-ID has already been seen.
3062       ;; We rename the Message-ID.
3063       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3064            (list header))
3065       (mail-header-set-id header id))
3066
3067      ;; The last case ignores an existing entry, except it adds any
3068      ;; additional Xrefs (in case the two articles came from different
3069      ;; servers.
3070      ;; Also sets `header' to `nil' meaning that the `dependencies'
3071      ;; table was *not* modified.
3072      (t
3073       (mail-header-set-xref
3074        (car (symbol-value id-dep))
3075        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3076                    "")
3077                (or (mail-header-xref header) "")))
3078       (setq header nil)))
3079
3080     (when header
3081       ;; First check if that we are not creating a References loop.
3082       (setq ref (gnus-parent-id (mail-header-references header)))
3083       (while (and ref
3084                   (setq ref-dep (intern-soft ref dependencies))
3085                   (boundp ref-dep)
3086                   (setq ref-header (car (symbol-value ref-dep))))
3087         (if (string= id ref)
3088             ;; Yuk!  This is a reference loop.  Make the article be a
3089             ;; root article.
3090             (progn
3091               (mail-header-set-references (car (symbol-value id-dep)) "none")
3092               (setq ref nil))
3093           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3094       (setq ref (gnus-parent-id (mail-header-references header)))
3095       (setq ref-dep (intern (or ref "none") dependencies))
3096       (if (boundp ref-dep)
3097           (setcdr (symbol-value ref-dep)
3098                   (nconc (cdr (symbol-value ref-dep))
3099                          (list (symbol-value id-dep))))
3100         (set ref-dep (list nil (symbol-value id-dep)))))
3101     header))
3102
3103 (defun gnus-build-sparse-threads ()
3104   (let ((headers gnus-newsgroup-headers)
3105         (gnus-summary-ignore-duplicates t)
3106         header references generation relations
3107         subject child end new-child date)
3108     ;; First we create an alist of generations/relations, where
3109     ;; generations is how much we trust the relation, and the relation
3110     ;; is parent/child.
3111     (gnus-message 7 "Making sparse threads...")
3112     (save-excursion
3113       (nnheader-set-temp-buffer " *gnus sparse threads*")
3114       (while (setq header (pop headers))
3115         (when (and (setq references (mail-header-references header))
3116                    (not (string= references "")))
3117           (insert references)
3118           (setq child (mail-header-id header)
3119                 subject (mail-header-subject header)
3120                 date (mail-header-date header)
3121                 generation 0)
3122           (while (search-backward ">" nil t)
3123             (setq end (1+ (point)))
3124             (when (search-backward "<" nil t)
3125               (setq new-child (buffer-substring (point) end))
3126               (push (list (incf generation)
3127                           child (setq child new-child)
3128                           subject date)
3129                     relations)))
3130           (when child
3131             (push (list (1+ generation) child nil subject) relations))
3132           (erase-buffer)))
3133       (kill-buffer (current-buffer)))
3134     ;; Sort over trustworthiness.
3135     (mapcar
3136      (lambda (relation)
3137        (when (gnus-dependencies-add-header
3138               (make-full-mail-header
3139                gnus-reffed-article-number
3140                (nth 3 relation) "" (or (nth 4 relation) "")
3141                (nth 1 relation)
3142                (or (nth 2 relation) "") 0 0 "")
3143               gnus-newsgroup-dependencies nil)
3144          (push gnus-reffed-article-number gnus-newsgroup-limit)
3145          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3146          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3147                gnus-newsgroup-reads)
3148          (decf gnus-reffed-article-number)))
3149      (sort relations 'car-less-than-car))
3150     (gnus-message 7 "Making sparse threads...done")))
3151
3152 (defun gnus-build-old-threads ()
3153   ;; Look at all the articles that refer back to old articles, and
3154   ;; fetch the headers for the articles that aren't there.  This will
3155   ;; build complete threads - if the roots haven't been expired by the
3156   ;; server, that is.
3157   (let (id heads)
3158     (mapatoms
3159      (lambda (refs)
3160        (when (not (car (symbol-value refs)))
3161          (setq heads (cdr (symbol-value refs)))
3162          (while heads
3163            (if (memq (mail-header-number (caar heads))
3164                      gnus-newsgroup-dormant)
3165                (setq heads (cdr heads))
3166              (setq id (symbol-name refs))
3167              (while (and (setq id (gnus-build-get-header id))
3168                          (not (car (gnus-id-to-thread id)))))
3169              (setq heads nil)))))
3170      gnus-newsgroup-dependencies)))
3171
3172 (defmacro gnus-nov-read-integer ()
3173   '(prog1
3174        (if (eq (char-after) ?\t)
3175            0
3176          (let ((num (ignore-errors (read buffer))))
3177            (if (numberp num) num 0)))
3178      (unless (eobp)
3179        (search-forward "\t" eol 'move))))
3180
3181 (defmacro gnus-nov-skip-field ()
3182   '(search-forward "\t" eol 'move))
3183
3184 (defmacro gnus-nov-field ()
3185   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3186
3187 (defmacro gnus-nov-parse-extra ()
3188   '(let (out string)
3189      (while (not (memq (char-after) '(?\n nil)))
3190        (setq string (gnus-nov-field))
3191        (when (string-match "^\\([^ :]+\\): " string)
3192          (push (cons (intern (match-string 1 string))
3193                      (substring string (match-end 0)))
3194                out)))
3195      out))
3196
3197 ;; This function has to be called with point after the article number
3198 ;; on the beginning of the line.
3199 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3200   (let ((eol (gnus-point-at-eol))
3201         (buffer (current-buffer))
3202         header)
3203
3204     ;; overview: [num subject from date id refs chars lines misc]
3205     (unwind-protect
3206         (progn
3207           (narrow-to-region (point) eol)
3208           (unless (eobp)
3209             (forward-char))
3210
3211           (setq header
3212                 (make-full-mail-header
3213                  number                 ; number
3214                  (funcall gnus-decode-encoded-word-function
3215                           (gnus-nov-field)) ; subject
3216                  (funcall gnus-decode-encoded-word-function
3217                           (gnus-nov-field)) ; from
3218                  (gnus-nov-field)       ; date
3219                  (or (gnus-nov-field)
3220                      (nnheader-generate-fake-message-id)) ; id
3221                  (gnus-nov-field)       ; refs
3222                  (gnus-nov-read-integer) ; chars
3223                  (gnus-nov-read-integer) ; lines
3224                  (unless (eq (char-after) ?\n)
3225                    (gnus-nov-field))    ; misc
3226                  (gnus-nov-parse-extra)))) ; extra
3227
3228       (widen))
3229
3230     (when gnus-alter-header-function
3231       (funcall gnus-alter-header-function header))
3232     (gnus-dependencies-add-header header dependencies force-new)))
3233
3234 (defun gnus-build-get-header (id)
3235   ;; Look through the buffer of NOV lines and find the header to
3236   ;; ID.  Enter this line into the dependencies hash table, and return
3237   ;; the id of the parent article (if any).
3238   (let ((deps gnus-newsgroup-dependencies)
3239         found header)
3240     (prog1
3241         (save-excursion
3242           (set-buffer nntp-server-buffer)
3243           (let ((case-fold-search nil))
3244             (goto-char (point-min))
3245             (while (and (not found)
3246                         (search-forward id nil t))
3247               (beginning-of-line)
3248               (setq found (looking-at
3249                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3250                                    (regexp-quote id))))
3251               (or found (beginning-of-line 2)))
3252             (when found
3253               (beginning-of-line)
3254               (and
3255                (setq header (gnus-nov-parse-line
3256                              (read (current-buffer)) deps))
3257                (gnus-parent-id (mail-header-references header))))))
3258       (when header
3259         (let ((number (mail-header-number header)))
3260           (push number gnus-newsgroup-limit)
3261           (push header gnus-newsgroup-headers)
3262           (if (memq number gnus-newsgroup-unselected)
3263               (progn
3264                 (push number gnus-newsgroup-unreads)
3265                 (setq gnus-newsgroup-unselected
3266                       (delq number gnus-newsgroup-unselected)))
3267             (push number gnus-newsgroup-ancient)))))))
3268
3269 (defun gnus-build-all-threads ()
3270   "Read all the headers."
3271   (let ((gnus-summary-ignore-duplicates t)
3272         (dependencies gnus-newsgroup-dependencies)
3273         header article)
3274     (save-excursion
3275       (set-buffer nntp-server-buffer)
3276       (let ((case-fold-search nil))
3277         (goto-char (point-min))
3278         (while (not (eobp))
3279           (ignore-errors
3280             (setq article (read (current-buffer))
3281                   header (gnus-nov-parse-line
3282                           article dependencies)))
3283           (when header
3284             (save-excursion
3285               (set-buffer gnus-summary-buffer)
3286               (push header gnus-newsgroup-headers)
3287               (if (memq (setq article (mail-header-number header))
3288                         gnus-newsgroup-unselected)
3289                   (progn
3290                     (push article gnus-newsgroup-unreads)
3291                     (setq gnus-newsgroup-unselected
3292                           (delq article gnus-newsgroup-unselected)))
3293                 (push article gnus-newsgroup-ancient)))
3294             (forward-line 1)))))))
3295
3296 (defun gnus-summary-update-article-line (article header)
3297   "Update the line for ARTICLE using HEADERS."
3298   (let* ((id (mail-header-id header))
3299          (thread (gnus-id-to-thread id)))
3300     (unless thread
3301       (error "Article in no thread"))
3302     ;; Update the thread.
3303     (setcar thread header)
3304     (gnus-summary-goto-subject article)
3305     (let* ((datal (gnus-data-find-list article))
3306            (data (car datal))
3307            (length (when (cdr datal)
3308                      (- (gnus-data-pos data)
3309                         (gnus-data-pos (cadr datal)))))
3310            (buffer-read-only nil)
3311            (level (gnus-summary-thread-level)))
3312       (gnus-delete-line)
3313       (gnus-summary-insert-line
3314        header level nil (gnus-article-mark article)
3315        (memq article gnus-newsgroup-replied)
3316        (memq article gnus-newsgroup-expirable)
3317        ;; Only insert the Subject string when it's different
3318        ;; from the previous Subject string.
3319        (if (gnus-subject-equal
3320             (condition-case ()
3321                 (mail-header-subject
3322                  (gnus-data-header
3323                   (cadr
3324                    (gnus-data-find-list
3325                     article
3326                     (gnus-data-list t)))))
3327               ;; Error on the side of excessive subjects.
3328               (error ""))
3329             (mail-header-subject header))
3330            ""
3331          (mail-header-subject header))
3332        nil (cdr (assq article gnus-newsgroup-scored))
3333        (memq article gnus-newsgroup-processable))
3334       (when length
3335         (gnus-data-update-list
3336          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3337
3338 (defun gnus-summary-update-article (article &optional iheader)
3339   "Update ARTICLE in the summary buffer."
3340   (set-buffer gnus-summary-buffer)
3341   (let* ((header (gnus-summary-article-header article))
3342          (id (mail-header-id header))
3343          (data (gnus-data-find article))
3344          (thread (gnus-id-to-thread id))
3345          (references (mail-header-references header))
3346          (parent
3347           (gnus-id-to-thread
3348            (or (gnus-parent-id
3349                 (when (and references
3350                            (not (equal "" references)))
3351                   references))
3352                "none")))
3353          (buffer-read-only nil)
3354          (old (car thread)))
3355     (when thread
3356       (unless iheader
3357         (setcar thread nil)
3358         (when parent
3359           (delq thread parent)))
3360       (if (gnus-summary-insert-subject id header)
3361           ;; Set the (possibly) new article number in the data structure.
3362           (gnus-data-set-number data (gnus-id-to-article id))
3363         (setcar thread old)
3364         nil))))
3365
3366 (defun gnus-rebuild-thread (id &optional line)
3367   "Rebuild the thread containing ID.
3368 If LINE, insert the rebuilt thread starting on line LINE."
3369   (let ((buffer-read-only nil)
3370         old-pos current thread data)
3371     (if (not gnus-show-threads)
3372         (setq thread (list (car (gnus-id-to-thread id))))
3373       ;; Get the thread this article is part of.
3374       (setq thread (gnus-remove-thread id)))
3375     (setq old-pos (gnus-point-at-bol))
3376     (setq current (save-excursion
3377                     (and (zerop (forward-line -1))
3378                          (gnus-summary-article-number))))
3379     ;; If this is a gathered thread, we have to go some re-gathering.
3380     (when (stringp (car thread))
3381       (let ((subject (car thread))
3382             roots thr)
3383         (setq thread (cdr thread))
3384         (while thread
3385           (unless (memq (setq thr (gnus-id-to-thread
3386                                    (gnus-root-id
3387                                     (mail-header-id (caar thread)))))
3388                         roots)
3389             (push thr roots))
3390           (setq thread (cdr thread)))
3391         ;; We now have all (unique) roots.
3392         (if (= (length roots) 1)
3393             ;; All the loose roots are now one solid root.
3394             (setq thread (car roots))
3395           (setq thread (cons subject (gnus-sort-threads roots))))))
3396     (let (threads)
3397       ;; We then insert this thread into the summary buffer.
3398       (when line
3399         (goto-char (point-min))
3400         (forward-line (1- line)))
3401       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3402         (if gnus-show-threads
3403             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3404           (gnus-summary-prepare-unthreaded thread))
3405         (setq data (nreverse gnus-newsgroup-data))
3406         (setq threads gnus-newsgroup-threads))
3407       ;; We splice the new data into the data structure.
3408       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3409       ;;!!! then we want to insert at the beginning of the buffer.
3410       ;;!!! That happens to be true with Gnus now, but that may
3411       ;;!!! change in the future.  Perhaps.
3412       (gnus-data-enter-list
3413        (if line nil current) data (- (point) old-pos))
3414       (setq gnus-newsgroup-threads
3415             (nconc threads gnus-newsgroup-threads))
3416       (gnus-data-compute-positions))))
3417
3418 (defun gnus-number-to-header (number)
3419   "Return the header for article NUMBER."
3420   (let ((headers gnus-newsgroup-headers))
3421     (while (and headers
3422                 (not (= number (mail-header-number (car headers)))))
3423       (pop headers))
3424     (when headers
3425       (car headers))))
3426
3427 (defun gnus-parent-headers (in-headers &optional generation)
3428   "Return the headers of the GENERATIONeth parent of HEADERS."
3429   (unless generation
3430     (setq generation 1))
3431   (let ((parent t)
3432         (headers in-headers)
3433         references)
3434     (while (and parent
3435                 (not (zerop generation))
3436                 (setq references (mail-header-references headers)))
3437       (setq headers (if (and references
3438                              (setq parent (gnus-parent-id references)))
3439                         (car (gnus-id-to-thread parent))
3440                       nil))
3441       (decf generation))
3442     (and (not (eq headers in-headers))
3443          headers)))
3444
3445 (defun gnus-id-to-thread (id)
3446   "Return the (sub-)thread where ID appears."
3447   (gnus-gethash id gnus-newsgroup-dependencies))
3448
3449 (defun gnus-id-to-article (id)
3450   "Return the article number of ID."
3451   (let ((thread (gnus-id-to-thread id)))
3452     (when (and thread
3453                (car thread))
3454       (mail-header-number (car thread)))))
3455
3456 (defun gnus-id-to-header (id)
3457   "Return the article headers of ID."
3458   (car (gnus-id-to-thread id)))
3459
3460 (defun gnus-article-displayed-root-p (article)
3461   "Say whether ARTICLE is a root(ish) article."
3462   (let ((level (gnus-summary-thread-level article))
3463         (refs (mail-header-references  (gnus-summary-article-header article)))
3464         particle)
3465     (cond
3466      ((null level) nil)
3467      ((zerop level) t)
3468      ((null refs) t)
3469      ((null (gnus-parent-id refs)) t)
3470      ((and (= 1 level)
3471            (null (setq particle (gnus-id-to-article
3472                                  (gnus-parent-id refs))))
3473            (null (gnus-summary-thread-level particle)))))))
3474
3475 (defun gnus-root-id (id)
3476   "Return the id of the root of the thread where ID appears."
3477   (let (last-id prev)
3478     (while (and id (setq prev (car (gnus-id-to-thread id))))
3479       (setq last-id id
3480             id (gnus-parent-id (mail-header-references prev))))
3481     last-id))
3482
3483 (defun gnus-articles-in-thread (thread)
3484   "Return the list of articles in THREAD."
3485   (cons (mail-header-number (car thread))
3486         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3487
3488 (defun gnus-remove-thread (id &optional dont-remove)
3489   "Remove the thread that has ID in it."
3490   (let (headers thread last-id)
3491     ;; First go up in this thread until we find the root.
3492     (setq last-id (gnus-root-id id)
3493           headers (message-flatten-list (gnus-id-to-thread last-id)))
3494     ;; We have now found the real root of this thread.  It might have
3495     ;; been gathered into some loose thread, so we have to search
3496     ;; through the threads to find the thread we wanted.
3497     (let ((threads gnus-newsgroup-threads)
3498           sub)
3499       (while threads
3500         (setq sub (car threads))
3501         (if (stringp (car sub))
3502             ;; This is a gathered thread, so we look at the roots
3503             ;; below it to find whether this article is in this
3504             ;; gathered root.
3505             (progn
3506               (setq sub (cdr sub))
3507               (while sub
3508                 (when (member (caar sub) headers)
3509                   (setq thread (car threads)
3510                         threads nil
3511                         sub nil))
3512                 (setq sub (cdr sub))))
3513           ;; It's an ordinary thread, so we check it.
3514           (when (eq (car sub) (car headers))
3515             (setq thread sub
3516                   threads nil)))
3517         (setq threads (cdr threads)))
3518       ;; If this article is in no thread, then it's a root.
3519       (if thread
3520           (unless dont-remove
3521             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3522         (setq thread (gnus-id-to-thread last-id)))
3523       (when thread
3524         (prog1
3525             thread                      ; We return this thread.
3526           (unless dont-remove
3527             (if (stringp (car thread))
3528                 (progn
3529                   ;; If we use dummy roots, then we have to remove the
3530                   ;; dummy root as well.
3531                   (when (eq gnus-summary-make-false-root 'dummy)
3532                     ;; We go to the dummy root by going to
3533                     ;; the first sub-"thread", and then one line up.
3534                     (gnus-summary-goto-article
3535                      (mail-header-number (caadr thread)))
3536                     (forward-line -1)
3537                     (gnus-delete-line)
3538                     (gnus-data-compute-positions))
3539                   (setq thread (cdr thread))
3540                   (while thread
3541                     (gnus-remove-thread-1 (car thread))
3542                     (setq thread (cdr thread))))
3543               (gnus-summary-show-all-threads)
3544               (gnus-remove-thread-1 thread))))))))
3545
3546 (defun gnus-remove-thread-1 (thread)
3547   "Remove the thread THREAD recursively."
3548   (let ((number (mail-header-number (pop thread)))
3549         d)
3550     (setq thread (reverse thread))
3551     (while thread
3552       (gnus-remove-thread-1 (pop thread)))
3553     (when (setq d (gnus-data-find number))
3554       (goto-char (gnus-data-pos d))
3555       (gnus-data-remove
3556        number
3557        (- (gnus-point-at-bol)
3558           (prog1
3559               (1+ (gnus-point-at-eol))
3560             (gnus-delete-line)))))))
3561
3562 (defun gnus-sort-threads (threads)
3563   "Sort THREADS."
3564   (if (not gnus-thread-sort-functions)
3565       threads
3566     (gnus-message 8 "Sorting threads...")
3567     (prog1
3568         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3569       (gnus-message 8 "Sorting threads...done"))))
3570
3571 (defun gnus-sort-articles (articles)
3572   "Sort ARTICLES."
3573   (when gnus-article-sort-functions
3574     (gnus-message 7 "Sorting articles...")
3575     (prog1
3576         (setq gnus-newsgroup-headers
3577               (sort articles (gnus-make-sort-function
3578                               gnus-article-sort-functions)))
3579       (gnus-message 7 "Sorting articles...done"))))
3580
3581 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3582 (defmacro gnus-thread-header (thread)
3583   ;; Return header of first article in THREAD.
3584   ;; Note that THREAD must never, ever be anything else than a variable -
3585   ;; using some other form will lead to serious barfage.
3586   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3587   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3588   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3589         (vector thread) 2))
3590
3591 (defsubst gnus-article-sort-by-number (h1 h2)
3592   "Sort articles by article number."
3593   (< (mail-header-number h1)
3594      (mail-header-number h2)))
3595
3596 (defun gnus-thread-sort-by-number (h1 h2)
3597   "Sort threads by root article number."
3598   (gnus-article-sort-by-number
3599    (gnus-thread-header h1) (gnus-thread-header h2)))
3600
3601 (defsubst gnus-article-sort-by-lines (h1 h2)
3602   "Sort articles by article Lines header."
3603   (< (mail-header-lines h1)
3604      (mail-header-lines h2)))
3605
3606 (defun gnus-thread-sort-by-lines (h1 h2)
3607   "Sort threads by root article Lines header."
3608   (gnus-article-sort-by-lines
3609    (gnus-thread-header h1) (gnus-thread-header h2)))
3610
3611 (defsubst gnus-article-sort-by-author (h1 h2)
3612   "Sort articles by root author."
3613   (string-lessp
3614    (let ((extract (funcall
3615                    gnus-extract-address-components
3616                    (mail-header-from h1))))
3617      (or (car extract) (cadr extract) ""))
3618    (let ((extract (funcall
3619                    gnus-extract-address-components
3620                    (mail-header-from h2))))
3621      (or (car extract) (cadr extract) ""))))
3622
3623 (defun gnus-thread-sort-by-author (h1 h2)
3624   "Sort threads by root author."
3625   (gnus-article-sort-by-author
3626    (gnus-thread-header h1)  (gnus-thread-header h2)))
3627
3628 (defsubst gnus-article-sort-by-subject (h1 h2)
3629   "Sort articles by root subject."
3630   (string-lessp
3631    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3632    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3633
3634 (defun gnus-thread-sort-by-subject (h1 h2)
3635   "Sort threads by root subject."
3636   (gnus-article-sort-by-subject
3637    (gnus-thread-header h1) (gnus-thread-header h2)))
3638
3639 (defsubst gnus-article-sort-by-date (h1 h2)
3640   "Sort articles by root article date."
3641   (time-less-p
3642    (gnus-date-get-time (mail-header-date h1))
3643    (gnus-date-get-time (mail-header-date h2))))
3644
3645 (defun gnus-thread-sort-by-date (h1 h2)
3646   "Sort threads by root article date."
3647   (gnus-article-sort-by-date
3648    (gnus-thread-header h1) (gnus-thread-header h2)))
3649
3650 (defsubst gnus-article-sort-by-score (h1 h2)
3651   "Sort articles by root article score.
3652 Unscored articles will be counted as having a score of zero."
3653   (> (or (cdr (assq (mail-header-number h1)
3654                     gnus-newsgroup-scored))
3655          gnus-summary-default-score 0)
3656      (or (cdr (assq (mail-header-number h2)
3657                     gnus-newsgroup-scored))
3658          gnus-summary-default-score 0)))
3659
3660 (defun gnus-thread-sort-by-score (h1 h2)
3661   "Sort threads by root article score."
3662   (gnus-article-sort-by-score
3663    (gnus-thread-header h1) (gnus-thread-header h2)))
3664
3665 (defun gnus-thread-sort-by-total-score (h1 h2)
3666   "Sort threads by the sum of all scores in the thread.
3667 Unscored articles will be counted as having a score of zero."
3668   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3669
3670 (defun gnus-thread-total-score (thread)
3671   ;;  This function find the total score of THREAD.
3672   (cond ((null thread)
3673          0)
3674         ((consp thread)
3675          (if (stringp (car thread))
3676              (apply gnus-thread-score-function 0
3677                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3678            (gnus-thread-total-score-1 thread)))
3679         (t
3680          (gnus-thread-total-score-1 (list thread)))))
3681
3682 (defun gnus-thread-total-score-1 (root)
3683   ;; This function find the total score of the thread below ROOT.
3684   (setq root (car root))
3685   (apply gnus-thread-score-function
3686          (or (append
3687               (mapcar 'gnus-thread-total-score
3688                       (cdr (gnus-id-to-thread (mail-header-id root))))
3689               (when (> (mail-header-number root) 0)
3690                 (list (or (cdr (assq (mail-header-number root)
3691                                      gnus-newsgroup-scored))
3692                           gnus-summary-default-score 0))))
3693              (list gnus-summary-default-score)
3694              '(0))))
3695
3696 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3697 (defvar gnus-tmp-prev-subject nil)
3698 (defvar gnus-tmp-false-parent nil)
3699 (defvar gnus-tmp-root-expunged nil)
3700 (defvar gnus-tmp-dummy-line nil)
3701
3702 (defvar gnus-tmp-header)
3703 (defun gnus-extra-header (type &optional header)
3704   "Return the extra header of TYPE."
3705   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3706       ""))
3707
3708 (defun gnus-summary-prepare-threads (threads)
3709   "Prepare summary buffer from THREADS and indentation LEVEL.
3710 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3711 or a straight list of headers."
3712   (gnus-message 7 "Generating summary...")
3713
3714   (setq gnus-newsgroup-threads threads)
3715   (beginning-of-line)
3716
3717   (let ((gnus-tmp-level 0)
3718         (default-score (or gnus-summary-default-score 0))
3719         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3720         thread number subject stack state gnus-tmp-gathered beg-match
3721         new-roots gnus-tmp-new-adopts thread-end
3722         gnus-tmp-header gnus-tmp-unread
3723         gnus-tmp-replied gnus-tmp-subject-or-nil
3724         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3725         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3726         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3727
3728     (setq gnus-tmp-prev-subject nil)
3729
3730     (if (vectorp (car threads))
3731         ;; If this is a straight (sic) list of headers, then a
3732         ;; threaded summary display isn't required, so we just create
3733         ;; an unthreaded one.
3734         (gnus-summary-prepare-unthreaded threads)
3735
3736       ;; Do the threaded display.
3737
3738       (while (or threads stack gnus-tmp-new-adopts new-roots)
3739
3740         (if (and (= gnus-tmp-level 0)
3741                  (or (not stack)
3742                      (= (caar stack) 0))
3743                  (not gnus-tmp-false-parent)
3744                  (or gnus-tmp-new-adopts new-roots))
3745             (if gnus-tmp-new-adopts
3746                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3747                       thread (list (car gnus-tmp-new-adopts))
3748                       gnus-tmp-header (caar thread)
3749                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3750               (when new-roots
3751                 (setq thread (list (car new-roots))
3752                       gnus-tmp-header (caar thread)
3753                       new-roots (cdr new-roots))))
3754
3755           (if threads
3756               ;; If there are some threads, we do them before the
3757               ;; threads on the stack.
3758               (setq thread threads
3759                     gnus-tmp-header (caar thread))
3760             ;; There were no current threads, so we pop something off
3761             ;; the stack.
3762             (setq state (car stack)
3763                   gnus-tmp-level (car state)
3764                   thread (cdr state)
3765                   stack (cdr stack)
3766                   gnus-tmp-header (caar thread))))
3767
3768         (setq gnus-tmp-false-parent nil)
3769         (setq gnus-tmp-root-expunged nil)
3770         (setq thread-end nil)
3771
3772         (if (stringp gnus-tmp-header)
3773             ;; The header is a dummy root.
3774             (cond
3775              ((eq gnus-summary-make-false-root 'adopt)
3776               ;; We let the first article adopt the rest.
3777               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3778                                                (cddar thread)))
3779               (setq gnus-tmp-gathered
3780                     (nconc (mapcar
3781                             (lambda (h) (mail-header-number (car h)))
3782                             (cddar thread))
3783                            gnus-tmp-gathered))
3784               (setq thread (cons (list (caar thread)
3785                                        (cadar thread))
3786                                  (cdr thread)))
3787               (setq gnus-tmp-level -1
3788                     gnus-tmp-false-parent t))
3789              ((eq gnus-summary-make-false-root 'empty)
3790               ;; We print adopted articles with empty subject fields.
3791               (setq gnus-tmp-gathered
3792                     (nconc (mapcar
3793                             (lambda (h) (mail-header-number (car h)))
3794                             (cddar thread))
3795                            gnus-tmp-gathered))
3796               (setq gnus-tmp-level -1))
3797              ((eq gnus-summary-make-false-root 'dummy)
3798               ;; We remember that we probably want to output a dummy
3799               ;; root.
3800               (setq gnus-tmp-dummy-line gnus-tmp-header)
3801               (setq gnus-tmp-prev-subject gnus-tmp-header))
3802              (t
3803               ;; We do not make a root for the gathered
3804               ;; sub-threads at all.
3805               (setq gnus-tmp-level -1)))
3806
3807           (setq number (mail-header-number gnus-tmp-header)
3808                 subject (mail-header-subject gnus-tmp-header))
3809
3810           (cond
3811            ;; If the thread has changed subject, we might want to make
3812            ;; this subthread into a root.
3813            ((and (null gnus-thread-ignore-subject)
3814                  (not (zerop gnus-tmp-level))
3815                  gnus-tmp-prev-subject
3816                  (not (inline
3817                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3818             (setq new-roots (nconc new-roots (list (car thread)))
3819                   thread-end t
3820                   gnus-tmp-header nil))
3821            ;; If the article lies outside the current limit,
3822            ;; then we do not display it.
3823            ((not (memq number gnus-newsgroup-limit))
3824             (setq gnus-tmp-gathered
3825                   (nconc (mapcar
3826                           (lambda (h) (mail-header-number (car h)))
3827                           (cdar thread))
3828                          gnus-tmp-gathered))
3829             (setq gnus-tmp-new-adopts (if (cdar thread)
3830                                           (append gnus-tmp-new-adopts
3831                                                   (cdar thread))
3832                                         gnus-tmp-new-adopts)
3833                   thread-end t
3834                   gnus-tmp-header nil)
3835             (when (zerop gnus-tmp-level)
3836               (setq gnus-tmp-root-expunged t)))
3837            ;; Perhaps this article is to be marked as read?
3838            ((and gnus-summary-mark-below
3839                  (< (or (cdr (assq number gnus-newsgroup-scored))
3840                         default-score)
3841                     gnus-summary-mark-below)
3842                  ;; Don't touch sparse articles.
3843                  (not (gnus-summary-article-sparse-p number))
3844                  (not (gnus-summary-article-ancient-p number)))
3845             (setq gnus-newsgroup-unreads
3846                   (delq number gnus-newsgroup-unreads))
3847             (if gnus-newsgroup-auto-expire
3848                 (push number gnus-newsgroup-expirable)
3849               (push (cons number gnus-low-score-mark)
3850                     gnus-newsgroup-reads))))
3851
3852           (when gnus-tmp-header
3853             ;; We may have an old dummy line to output before this
3854             ;; article.
3855             (when (and gnus-tmp-dummy-line
3856                        (gnus-subject-equal
3857                         gnus-tmp-dummy-line
3858                         (mail-header-subject gnus-tmp-header)))
3859               (gnus-summary-insert-dummy-line
3860                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3861               (setq gnus-tmp-dummy-line nil))
3862
3863             ;; Compute the mark.
3864             (setq gnus-tmp-unread (gnus-article-mark number))
3865
3866             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3867                                   gnus-tmp-header gnus-tmp-level)
3868                   gnus-newsgroup-data)
3869
3870             ;; Actually insert the line.
3871             (setq
3872              gnus-tmp-subject-or-nil
3873              (cond
3874               ((and gnus-thread-ignore-subject
3875                     gnus-tmp-prev-subject
3876                     (not (inline (gnus-subject-equal
3877                                   gnus-tmp-prev-subject subject))))
3878                subject)
3879               ((zerop gnus-tmp-level)
3880                (if (and (eq gnus-summary-make-false-root 'empty)
3881                         (memq number gnus-tmp-gathered)
3882                         gnus-tmp-prev-subject
3883                         (inline (gnus-subject-equal
3884                                  gnus-tmp-prev-subject subject)))
3885                    gnus-summary-same-subject
3886                  subject))
3887               (t gnus-summary-same-subject)))
3888             (if (and (eq gnus-summary-make-false-root 'adopt)
3889                      (= gnus-tmp-level 1)
3890                      (memq number gnus-tmp-gathered))
3891                 (setq gnus-tmp-opening-bracket ?\<
3892                       gnus-tmp-closing-bracket ?\>)
3893               (setq gnus-tmp-opening-bracket ?\[
3894                     gnus-tmp-closing-bracket ?\]))
3895             (setq
3896              gnus-tmp-indentation
3897              (aref gnus-thread-indent-array gnus-tmp-level)
3898              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3899              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3900                                 gnus-summary-default-score 0)
3901              gnus-tmp-score-char
3902              (if (or (null gnus-summary-default-score)
3903                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3904                          gnus-summary-zcore-fuzz))
3905                  ? 
3906                (if (< gnus-tmp-score gnus-summary-default-score)
3907                    gnus-score-below-mark gnus-score-over-mark))
3908              gnus-tmp-replied
3909              (cond ((memq number gnus-newsgroup-processable)
3910                     gnus-process-mark)
3911                    ((memq number gnus-newsgroup-cached)
3912                     gnus-cached-mark)
3913                    ((memq number gnus-newsgroup-replied)
3914                     gnus-replied-mark)
3915                    ((memq number gnus-newsgroup-saved)
3916                     gnus-saved-mark)
3917                    (t gnus-unread-mark))
3918              gnus-tmp-from (mail-header-from gnus-tmp-header)
3919              gnus-tmp-name
3920              (cond
3921               ((string-match "<[^>]+> *$" gnus-tmp-from)
3922                (setq beg-match (match-beginning 0))
3923                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3924                         (substring gnus-tmp-from (1+ (match-beginning 0))
3925                                    (1- (match-end 0))))
3926                    (substring gnus-tmp-from 0 beg-match)))
3927               ((string-match "(.+)" gnus-tmp-from)
3928                (substring gnus-tmp-from
3929                           (1+ (match-beginning 0)) (1- (match-end 0))))
3930               (t gnus-tmp-from)))
3931             (when (string= gnus-tmp-name "")
3932               (setq gnus-tmp-name gnus-tmp-from))
3933             (unless (numberp gnus-tmp-lines)
3934               (setq gnus-tmp-lines 0))
3935             (gnus-put-text-property
3936              (point)
3937              (progn (eval gnus-summary-line-format-spec) (point))
3938              'gnus-number number)
3939             (when gnus-visual-p
3940               (forward-line -1)
3941               (gnus-run-hooks 'gnus-summary-update-hook)
3942               (forward-line 1))
3943
3944             (setq gnus-tmp-prev-subject subject)))
3945
3946         (when (nth 1 thread)
3947           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3948         (incf gnus-tmp-level)
3949         (setq threads (if thread-end nil (cdar thread)))
3950         (unless threads
3951           (setq gnus-tmp-level 0)))))
3952   (gnus-message 7 "Generating summary...done"))
3953
3954 (defun gnus-summary-prepare-unthreaded (headers)
3955   "Generate an unthreaded summary buffer based on HEADERS."
3956   (let (header number mark)
3957
3958     (beginning-of-line)
3959
3960     (while headers
3961       ;; We may have to root out some bad articles...
3962       (when (memq (setq number (mail-header-number
3963                                 (setq header (pop headers))))
3964                   gnus-newsgroup-limit)
3965         ;; Mark article as read when it has a low score.
3966         (when (and gnus-summary-mark-below
3967                    (< (or (cdr (assq number gnus-newsgroup-scored))
3968                           gnus-summary-default-score 0)
3969                       gnus-summary-mark-below)
3970                    (not (gnus-summary-article-ancient-p number)))
3971           (setq gnus-newsgroup-unreads
3972                 (delq number gnus-newsgroup-unreads))
3973           (if gnus-newsgroup-auto-expire
3974               (push number gnus-newsgroup-expirable)
3975             (push (cons number gnus-low-score-mark)
3976                   gnus-newsgroup-reads)))
3977
3978         (setq mark (gnus-article-mark number))
3979         (push (gnus-data-make number mark (1+ (point)) header 0)
3980               gnus-newsgroup-data)
3981         (gnus-summary-insert-line
3982          header 0 number
3983          mark (memq number gnus-newsgroup-replied)
3984          (memq number gnus-newsgroup-expirable)
3985          (mail-header-subject header) nil
3986          (cdr (assq number gnus-newsgroup-scored))
3987          (memq number gnus-newsgroup-processable))))))
3988
3989 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3990   "Select newsgroup GROUP.
3991 If READ-ALL is non-nil, all articles in the group are selected.
3992 If SELECT-ARTICLES, only select those articles from GROUP."
3993   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3994          ;;!!! Dirty hack; should be removed.
3995          (gnus-summary-ignore-duplicates
3996           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3997               t
3998             gnus-summary-ignore-duplicates))
3999          (info (nth 2 entry))
4000          articles fetched-articles cached)
4001
4002     (unless (gnus-check-server
4003              (setq gnus-current-select-method
4004                    (gnus-find-method-for-group group)))
4005       (error "Couldn't open server"))
4006
4007     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4008         (gnus-activate-group group)     ; Or we can activate it...
4009         (progn                          ; Or we bug out.
4010           (when (equal major-mode 'gnus-summary-mode)
4011             (kill-buffer (current-buffer)))
4012           (error "Couldn't request group %s: %s"
4013                  group (gnus-status-message group))))
4014
4015     (unless (gnus-request-group group t)
4016       (when (equal major-mode 'gnus-summary-mode)
4017         (kill-buffer (current-buffer)))
4018       (error "Couldn't request group %s: %s"
4019              group (gnus-status-message group)))
4020
4021     (setq gnus-newsgroup-name group)
4022     (setq gnus-newsgroup-unselected nil)
4023     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4024     (gnus-newsgroup-setup-default-charset)
4025
4026     ;; Adjust and set lists of article marks.
4027     (when info
4028       (gnus-adjust-marked-articles info))
4029
4030     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4031     (when (gnus-virtual-group-p group)
4032       (setq cached gnus-newsgroup-cached))
4033
4034     (setq gnus-newsgroup-unreads
4035           (gnus-set-difference
4036            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4037            gnus-newsgroup-dormant))
4038
4039     (setq gnus-newsgroup-processable nil)
4040
4041     (gnus-update-read-articles group gnus-newsgroup-unreads)
4042
4043     (if (setq articles select-articles)
4044         (setq gnus-newsgroup-unselected
4045               (gnus-sorted-intersection
4046                gnus-newsgroup-unreads
4047                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4048       (setq articles (gnus-articles-to-read group read-all)))
4049
4050     (cond
4051      ((null articles)
4052       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4053       'quit)
4054      ((eq articles 0) nil)
4055      (t
4056       ;; Init the dependencies hash table.
4057       (setq gnus-newsgroup-dependencies
4058             (gnus-make-hashtable (length articles)))
4059       (gnus-set-global-variables)
4060       ;; Retrieve the headers and read them in.
4061       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4062       (setq gnus-newsgroup-headers
4063             (if (eq 'nov
4064                     (setq gnus-headers-retrieved-by
4065                           (gnus-retrieve-headers
4066                            articles gnus-newsgroup-name
4067                            ;; We might want to fetch old headers, but
4068                            ;; not if there is only 1 article.
4069                            (and (or (and
4070                                      (not (eq gnus-fetch-old-headers 'some))
4071                                      (not (numberp gnus-fetch-old-headers)))
4072                                     (> (length articles) 1))
4073                                 gnus-fetch-old-headers))))
4074                 (gnus-get-newsgroup-headers-xover
4075                  articles nil nil gnus-newsgroup-name t)
4076               (gnus-get-newsgroup-headers)))
4077       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4078
4079       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4080       (when cached
4081         (setq gnus-newsgroup-cached cached))
4082
4083       ;; Suppress duplicates?
4084       (when gnus-suppress-duplicates
4085         (gnus-dup-suppress-articles))
4086
4087       ;; Set the initial limit.
4088       (setq gnus-newsgroup-limit (copy-sequence articles))
4089       ;; Remove canceled articles from the list of unread articles.
4090       (setq gnus-newsgroup-unreads
4091             (gnus-set-sorted-intersection
4092              gnus-newsgroup-unreads
4093              (setq fetched-articles
4094                    (mapcar (lambda (headers) (mail-header-number headers))
4095                            gnus-newsgroup-headers))))
4096       ;; Removed marked articles that do not exist.
4097       (gnus-update-missing-marks
4098        (gnus-sorted-complement fetched-articles articles))
4099       ;; We might want to build some more threads first.
4100       (when (and gnus-fetch-old-headers
4101                  (eq gnus-headers-retrieved-by 'nov))
4102         (if (eq gnus-fetch-old-headers 'invisible)
4103             (gnus-build-all-threads)
4104           (gnus-build-old-threads)))
4105       ;; Let the Gnus agent mark articles as read.
4106       (when gnus-agent
4107         (gnus-agent-get-undownloaded-list))
4108       ;; Check whether auto-expire is to be done in this group.
4109       (setq gnus-newsgroup-auto-expire
4110             (gnus-group-auto-expirable-p group))
4111       ;; Set up the article buffer now, if necessary.
4112       (unless gnus-single-article-buffer
4113         (gnus-article-setup-buffer))
4114       ;; First and last article in this newsgroup.
4115       (when gnus-newsgroup-headers
4116         (setq gnus-newsgroup-begin
4117               (mail-header-number (car gnus-newsgroup-headers))
4118               gnus-newsgroup-end
4119               (mail-header-number
4120                (gnus-last-element gnus-newsgroup-headers))))
4121       ;; GROUP is successfully selected.
4122       (or gnus-newsgroup-headers t)))))
4123
4124 (defun gnus-articles-to-read (group &optional read-all)
4125   ;; Find out what articles the user wants to read.
4126   (let* ((articles
4127           ;; Select all articles if `read-all' is non-nil, or if there
4128           ;; are no unread articles.
4129           (if (or read-all
4130                   (and (zerop (length gnus-newsgroup-marked))
4131                        (zerop (length gnus-newsgroup-unreads)))
4132                   (eq (gnus-group-find-parameter group 'display)
4133                       'all))
4134               (gnus-uncompress-range (gnus-active group))
4135             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4136                           (copy-sequence gnus-newsgroup-unreads))
4137                   '<)))
4138          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4139          (scored (length scored-list))
4140          (number (length articles))
4141          (marked (+ (length gnus-newsgroup-marked)
4142                     (length gnus-newsgroup-dormant)))
4143          (select
4144           (cond
4145            ((numberp read-all)
4146             read-all)
4147            (t
4148             (condition-case ()
4149                 (cond
4150                  ((and (or (<= scored marked) (= scored number))
4151                        (numberp gnus-large-newsgroup)
4152                        (> number gnus-large-newsgroup))
4153                   (let ((input
4154                          (read-string
4155                           (format
4156                            "How many articles from %s (default %d): "
4157                            (gnus-limit-string gnus-newsgroup-name 35)
4158                            number))))
4159                     (if (string-match "^[ \t]*$" input) number input)))
4160                  ((and (> scored marked) (< scored number)
4161                        (> (- scored number) 20))
4162                   (let ((input
4163                          (read-string
4164                           (format "%s %s (%d scored, %d total): "
4165                                   "How many articles from"
4166                                   group scored number))))
4167                     (if (string-match "^[ \t]*$" input)
4168                         number input)))
4169                  (t number))
4170               (quit nil))))))
4171     (setq select (if (stringp select) (string-to-number select) select))
4172     (if (or (null select) (zerop select))
4173         select
4174       (if (and (not (zerop scored)) (<= (abs select) scored))
4175           (progn
4176             (setq articles (sort scored-list '<))
4177             (setq number (length articles)))
4178         (setq articles (copy-sequence articles)))
4179
4180       (when (< (abs select) number)
4181         (if (< select 0)
4182             ;; Select the N oldest articles.
4183             (setcdr (nthcdr (1- (abs select)) articles) nil)
4184           ;; Select the N most recent articles.
4185           (setq articles (nthcdr (- number select) articles))))
4186       (setq gnus-newsgroup-unselected
4187             (gnus-sorted-intersection
4188              gnus-newsgroup-unreads
4189              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4190       articles)))
4191
4192 (defun gnus-killed-articles (killed articles)
4193   (let (out)
4194     (while articles
4195       (when (inline (gnus-member-of-range (car articles) killed))
4196         (push (car articles) out))
4197       (setq articles (cdr articles)))
4198     out))
4199
4200 (defun gnus-uncompress-marks (marks)
4201   "Uncompress the mark ranges in MARKS."
4202   (let ((uncompressed '(score bookmark))
4203         out)
4204     (while marks
4205       (if (memq (caar marks) uncompressed)
4206           (push (car marks) out)
4207         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4208       (setq marks (cdr marks)))
4209     out))
4210
4211 (defun gnus-adjust-marked-articles (info)
4212   "Set all article lists and remove all marks that are no longer legal."
4213   (let* ((marked-lists (gnus-info-marks info))
4214          (active (gnus-active (gnus-info-group info)))
4215          (min (car active))
4216          (max (cdr active))
4217          (types gnus-article-mark-lists)
4218          (uncompressed '(score bookmark killed))
4219          marks var articles article mark)
4220
4221     (while marked-lists
4222       (setq marks (pop marked-lists))
4223       (set (setq var (intern (format "gnus-newsgroup-%s"
4224                                      (car (rassq (setq mark (car marks))
4225                                                  types)))))
4226            (if (memq (car marks) uncompressed) (cdr marks)
4227              (gnus-uncompress-range (cdr marks))))
4228
4229       (setq articles (symbol-value var))
4230
4231       ;; All articles have to be subsets of the active articles.
4232       (cond
4233        ;; Adjust "simple" lists.
4234        ((memq mark '(tick dormant expire reply save))
4235         (while articles
4236           (when (or (< (setq article (pop articles)) min) (> article max))
4237             (set var (delq article (symbol-value var))))))
4238        ;; Adjust assocs.
4239        ((memq mark uncompressed)
4240         (when (not (listp (cdr (symbol-value var))))
4241           (set var (list (symbol-value var))))
4242         (when (not (listp (cdr articles)))
4243           (setq articles (list articles)))
4244         (while articles
4245           (when (or (not (consp (setq article (pop articles))))
4246                     (< (car article) min)
4247                     (> (car article) max))
4248             (set var (delq article (symbol-value var))))))))))
4249
4250 (defun gnus-update-missing-marks (missing)
4251   "Go through the list of MISSING articles and remove them from the mark lists."
4252   (when missing
4253     (let ((types gnus-article-mark-lists)
4254           var m)
4255       ;; Go through all types.
4256       (while types
4257         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4258         (when (symbol-value var)
4259           ;; This list has articles.  So we delete all missing articles
4260           ;; from it.
4261           (setq m missing)
4262           (while m
4263             (set var (delq (pop m) (symbol-value var)))))))))
4264
4265 (defun gnus-update-marks ()
4266   "Enter the various lists of marked articles into the newsgroup info list."
4267   (let ((types gnus-article-mark-lists)
4268         (info (gnus-get-info gnus-newsgroup-name))
4269         (uncompressed '(score bookmark killed))
4270         type list newmarked symbol delta-marks)
4271     (when info
4272       ;; Add all marks lists that are non-nil to the list of marks lists.
4273       (while (setq type (pop types))
4274         (when (setq list (symbol-value
4275                           (setq symbol
4276                                 (intern (format "gnus-newsgroup-%s"
4277                                                 (car type))))))
4278
4279           ;; Get rid of the entries of the articles that have the
4280           ;; default score.
4281           (when (and (eq (cdr type) 'score)
4282                      gnus-save-score
4283                      list)
4284             (let* ((arts list)
4285                    (prev (cons nil list))
4286                    (all prev))
4287               (while arts
4288                 (if (or (not (consp (car arts)))
4289                         (= (cdar arts) gnus-summary-default-score))
4290                     (setcdr prev (cdr arts))
4291                   (setq prev arts))
4292                 (setq arts (cdr arts)))
4293               (setq list (cdr all))))
4294
4295           (push (cons (cdr type)
4296                       (if (memq (cdr type) uncompressed) list
4297                         (gnus-compress-sequence
4298                          (set symbol (sort list '<)) t)))
4299                 newmarked)))
4300
4301       ;; Enter these new marks into the info of the group.
4302       (if (nthcdr 3 info)
4303           (setcar (nthcdr 3 info) newmarked)
4304         ;; Add the marks lists to the end of the info.
4305         (when newmarked
4306           (setcdr (nthcdr 2 info) (list newmarked))))
4307
4308       ;; Cut off the end of the info if there's nothing else there.
4309       (let ((i 5))
4310         (while (and (> i 2)
4311                     (not (nth i info)))
4312           (when (nthcdr (decf i) info)
4313             (setcdr (nthcdr i info) nil)))))))
4314
4315 (defun gnus-set-mode-line (where)
4316   "This function sets the mode line of the article or summary buffers.
4317 If WHERE is `summary', the summary mode line format will be used."
4318   ;; Is this mode line one we keep updated?
4319   (when (and (memq where gnus-updated-mode-lines)
4320              (symbol-value
4321               (intern (format "gnus-%s-mode-line-format-spec" where))))
4322     (let (mode-string)
4323       (save-excursion
4324         ;; We evaluate this in the summary buffer since these
4325         ;; variables are buffer-local to that buffer.
4326         (set-buffer gnus-summary-buffer)
4327         ;; We bind all these variables that are used in the `eval' form
4328         ;; below.
4329         (let* ((mformat (symbol-value
4330                          (intern
4331                           (format "gnus-%s-mode-line-format-spec" where))))
4332                (gnus-tmp-group-name gnus-newsgroup-name)
4333                (gnus-tmp-article-number (or gnus-current-article 0))
4334                (gnus-tmp-unread gnus-newsgroup-unreads)
4335                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4336                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4337                (gnus-tmp-unread-and-unselected
4338                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4339                             (zerop gnus-tmp-unselected))
4340                        "")
4341                       ((zerop gnus-tmp-unselected)
4342                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4343                       (t (format "{%d(+%d) more}"
4344                                  gnus-tmp-unread-and-unticked
4345                                  gnus-tmp-unselected))))
4346                (gnus-tmp-subject
4347                 (if (and gnus-current-headers
4348                          (vectorp gnus-current-headers))
4349                     (gnus-mode-string-quote
4350                      (mail-header-subject gnus-current-headers))
4351                   ""))
4352                bufname-length max-len
4353                gnus-tmp-header);; passed as argument to any user-format-funcs
4354           (setq mode-string (eval mformat))
4355           (setq bufname-length (if (string-match "%b" mode-string)
4356                                    (- (length
4357                                        (buffer-name
4358                                         (if (eq where 'summary)
4359                                             nil
4360                                           (get-buffer gnus-article-buffer))))
4361                                       2)
4362                                  0))
4363           (setq max-len (max 4 (if gnus-mode-non-string-length
4364                                    (- (window-width)
4365                                       gnus-mode-non-string-length
4366                                       bufname-length)
4367                                  (length mode-string))))
4368           ;; We might have to chop a bit of the string off...
4369           (when (> (length mode-string) max-len)
4370             (setq mode-string
4371                   (concat (truncate-string-to-width mode-string (- max-len 3))
4372                           "...")))
4373           ;; Pad the mode string a bit.
4374           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4375       ;; Update the mode line.
4376       (setq mode-line-buffer-identification
4377             (gnus-mode-line-buffer-identification (list mode-string)))
4378       (set-buffer-modified-p t))))
4379
4380 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4381   "Go through the HEADERS list and add all Xrefs to a hash table.
4382 The resulting hash table is returned, or nil if no Xrefs were found."
4383   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4384          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4385          (xref-hashtb (gnus-make-hashtable))
4386          start group entry number xrefs header)
4387     (while headers
4388       (setq header (pop headers))
4389       (when (and (setq xrefs (mail-header-xref header))
4390                  (not (memq (setq number (mail-header-number header))
4391                             unreads)))
4392         (setq start 0)
4393         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4394           (setq start (match-end 0))
4395           (setq group (if prefix
4396                           (concat prefix (substring xrefs (match-beginning 1)
4397                                                     (match-end 1)))
4398                         (substring xrefs (match-beginning 1) (match-end 1))))
4399           (setq number
4400                 (string-to-int (substring xrefs (match-beginning 2)
4401                                           (match-end 2))))
4402           (if (setq entry (gnus-gethash group xref-hashtb))
4403               (setcdr entry (cons number (cdr entry)))
4404             (gnus-sethash group (cons number nil) xref-hashtb)))))
4405     (and start xref-hashtb)))
4406
4407 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4408   "Look through all the headers and mark the Xrefs as read."
4409   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4410         name entry info xref-hashtb idlist method nth4)
4411     (save-excursion
4412       (set-buffer gnus-group-buffer)
4413       (when (setq xref-hashtb
4414                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4415         (mapatoms
4416          (lambda (group)
4417            (unless (string= from-newsgroup (setq name (symbol-name group)))
4418              (setq idlist (symbol-value group))
4419              ;; Dead groups are not updated.
4420              (and (prog1
4421                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4422                             info (nth 2 entry))
4423                     (when (stringp (setq nth4 (gnus-info-method info)))
4424                       (setq nth4 (gnus-server-to-method nth4))))
4425                   ;; Only do the xrefs if the group has the same
4426                   ;; select method as the group we have just read.
4427                   (or (gnus-methods-equal-p
4428                        nth4 (gnus-find-method-for-group from-newsgroup))
4429                       virtual
4430                       (equal nth4 (setq method (gnus-find-method-for-group
4431                                                 from-newsgroup)))
4432                       (and (equal (car nth4) (car method))
4433                            (equal (nth 1 nth4) (nth 1 method))))
4434                   gnus-use-cross-reference
4435                   (or (not (eq gnus-use-cross-reference t))
4436                       virtual
4437                       ;; Only do cross-references on subscribed
4438                       ;; groups, if that is what is wanted.
4439                       (<= (gnus-info-level info) gnus-level-subscribed))
4440                   (gnus-group-make-articles-read name idlist))))
4441          xref-hashtb)))))
4442
4443 (defun gnus-compute-read-articles (group articles)
4444   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4445          (info (nth 2 entry))
4446          (active (gnus-active group))
4447          ninfo)
4448     (when entry
4449       ;; First peel off all illegal article numbers.
4450       (when active
4451         (let ((ids articles)
4452               id first)
4453           (while (setq id (pop ids))
4454             (when (and first (> id (cdr active)))
4455               ;; We'll end up in this situation in one particular
4456               ;; obscure situation.  If you re-scan a group and get
4457               ;; a new article that is cross-posted to a different
4458               ;; group that has not been re-scanned, you might get
4459               ;; crossposted article that has a higher number than
4460               ;; Gnus believes possible.  So we re-activate this
4461               ;; group as well.  This might mean doing the
4462               ;; crossposting thingy will *increase* the number
4463               ;; of articles in some groups.  Tsk, tsk.
4464               (setq active (or (gnus-activate-group group) active)))
4465             (when (or (> id (cdr active))
4466                       (< id (car active)))
4467               (setq articles (delq id articles))))))
4468       ;; If the read list is nil, we init it.
4469       (if (and active
4470                (null (gnus-info-read info))
4471                (> (car active) 1))
4472           (setq ninfo (cons 1 (1- (car active))))
4473         (setq ninfo (gnus-info-read info)))
4474       ;; Then we add the read articles to the range.
4475       (gnus-add-to-range
4476        ninfo (setq articles (sort articles '<))))))
4477
4478 (defun gnus-group-make-articles-read (group articles)
4479   "Update the info of GROUP to say that ARTICLES are read."
4480   (let* ((num 0)
4481          (entry (gnus-gethash group gnus-newsrc-hashtb))
4482          (info (nth 2 entry))
4483          (active (gnus-active group))
4484          range)
4485     (when entry
4486       (setq range (gnus-compute-read-articles group articles))
4487       (save-excursion
4488         (set-buffer gnus-group-buffer)
4489         (gnus-undo-register
4490           `(progn
4491              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4492              (gnus-info-set-read ',info ',(gnus-info-read info))
4493              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4494              (gnus-group-update-group ,group t))))
4495       ;; Add the read articles to the range.
4496       (gnus-info-set-read info range)
4497       ;; Then we have to re-compute how many unread
4498       ;; articles there are in this group.
4499       (when active
4500         (cond
4501          ((not range)
4502           (setq num (- (1+ (cdr active)) (car active))))
4503          ((not (listp (cdr range)))
4504           (setq num (- (cdr active) (- (1+ (cdr range))
4505                                        (car range)))))
4506          (t
4507           (while range
4508             (if (numberp (car range))
4509                 (setq num (1+ num))
4510               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4511             (setq range (cdr range)))
4512           (setq num (- (cdr active) num))))
4513         ;; Update the number of unread articles.
4514         (setcar entry num)
4515         ;; Update the group buffer.
4516         (gnus-group-update-group group t)))))
4517
4518 (defun gnus-methods-equal-p (m1 m2)
4519   (let ((m1 (or m1 gnus-select-method))
4520         (m2 (or m2 gnus-select-method)))
4521     (or (equal m1 m2)
4522         (and (eq (car m1) (car m2))
4523              (or (not (memq 'address (assoc (symbol-name (car m1))
4524                                             gnus-valid-select-methods)))
4525                  (equal (nth 1 m1) (nth 1 m2)))))))
4526
4527 (defvar gnus-newsgroup-none-id 0)
4528
4529 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4530   (let ((cur nntp-server-buffer)
4531         (dependencies
4532          (or dependencies
4533              (save-excursion (set-buffer gnus-summary-buffer)
4534                              gnus-newsgroup-dependencies)))
4535         headers id end ref
4536         (rfc2047-default-charset gnus-newsgroup-default-charset)
4537         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
4538     (save-excursion
4539       (set-buffer nntp-server-buffer)
4540       ;; Translate all TAB characters into SPACE characters.
4541       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4542       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4543       (gnus-run-hooks 'gnus-parse-headers-hook)
4544       (let ((case-fold-search t)
4545             in-reply-to header p lines chars)
4546         (goto-char (point-min))
4547         ;; Search to the beginning of the next header.  Error messages
4548         ;; do not begin with 2 or 3.
4549         (while (re-search-forward "^[23][0-9]+ " nil t)
4550           (setq id nil
4551                 ref nil)
4552           ;; This implementation of this function, with nine
4553           ;; search-forwards instead of the one re-search-forward and
4554           ;; a case (which basically was the old function) is actually
4555           ;; about twice as fast, even though it looks messier.  You
4556           ;; can't have everything, I guess.  Speed and elegance
4557           ;; doesn't always go hand in hand.
4558           (setq
4559            header
4560            (vector
4561             ;; Number.
4562             (prog1
4563                 (read cur)
4564               (end-of-line)
4565               (setq p (point))
4566               (narrow-to-region (point)
4567                                 (or (and (search-forward "\n.\n" nil t)
4568                                          (- (point) 2))
4569                                     (point))))
4570             ;; Subject.
4571             (progn
4572               (goto-char p)
4573               (if (search-forward "\nsubject: " nil t)
4574                   (funcall gnus-decode-encoded-word-function
4575                            (nnheader-header-value))
4576                 "(none)"))
4577             ;; From.
4578             (progn
4579               (goto-char p)
4580               (if (search-forward "\nfrom: " nil t)
4581                   (funcall gnus-decode-encoded-word-function
4582                            (nnheader-header-value))
4583                 "(nobody)"))
4584             ;; Date.
4585             (progn
4586               (goto-char p)
4587               (if (search-forward "\ndate: " nil t)
4588                   (nnheader-header-value) ""))
4589             ;; Message-ID.
4590             (progn
4591               (goto-char p)
4592               (setq id (if (re-search-forward
4593                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4594                            ;; We do it this way to make sure the Message-ID
4595                            ;; is (somewhat) syntactically valid.
4596                            (buffer-substring (match-beginning 1)
4597                                              (match-end 1))
4598                          ;; If there was no message-id, we just fake one
4599                          ;; to make subsequent routines simpler.
4600                          (nnheader-generate-fake-message-id))))
4601             ;; References.
4602             (progn
4603               (goto-char p)
4604               (if (search-forward "\nreferences: " nil t)
4605                   (progn
4606                     (setq end (point))
4607                     (prog1
4608                         (nnheader-header-value)
4609                       (setq ref
4610                             (buffer-substring
4611                              (progn
4612                                (end-of-line)
4613                                (search-backward ">" end t)
4614                                (1+ (point)))
4615                              (progn
4616                                (search-backward "<" end t)
4617                                (point))))))
4618                 ;; Get the references from the in-reply-to header if there
4619                 ;; were no references and the in-reply-to header looks
4620                 ;; promising.
4621                 (if (and (search-forward "\nin-reply-to: " nil t)
4622                          (setq in-reply-to (nnheader-header-value))
4623                          (string-match "<[^>]+>" in-reply-to))
4624                     (let (ref2)
4625                       (setq ref (substring in-reply-to (match-beginning 0)
4626                                            (match-end 0)))
4627                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4628                         (setq ref2 (substring in-reply-to (match-beginning 0)
4629                                               (match-end 0)))
4630                         (when (> (length ref2) (length ref))
4631                           (setq ref ref2)))
4632                       ref)
4633                   (setq ref nil))))
4634             ;; Chars.
4635             (progn
4636               (goto-char p)
4637               (if (search-forward "\nchars: " nil t)
4638                   (if (numberp (setq chars (ignore-errors (read cur))))
4639                       chars 0)
4640                 0))
4641             ;; Lines.
4642             (progn
4643               (goto-char p)
4644               (if (search-forward "\nlines: " nil t)
4645                   (if (numberp (setq lines (ignore-errors (read cur))))
4646                       lines 0)
4647                 0))
4648             ;; Xref.
4649             (progn
4650               (goto-char p)
4651               (and (search-forward "\nxref: " nil t)
4652                    (nnheader-header-value)))
4653             ;; Extra.
4654             (when gnus-extra-headers
4655               (let ((extra gnus-extra-headers)
4656                     out)
4657                 (while extra
4658                   (goto-char p)
4659                   (when (search-forward
4660                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4661                     (push (cons (car extra) (nnheader-header-value))
4662                           out))
4663                   (pop extra))
4664                 out))))
4665           (when (equal id ref)
4666             (setq ref nil))
4667
4668           (when gnus-alter-header-function
4669             (funcall gnus-alter-header-function header)
4670             (setq id (mail-header-id header)
4671                   ref (gnus-parent-id (mail-header-references header))))
4672
4673           (when (setq header
4674                       (gnus-dependencies-add-header
4675                        header dependencies force-new))
4676             (push header headers))
4677           (goto-char (point-max))
4678           (widen))
4679         (nreverse headers)))))
4680
4681 ;; Goes through the xover lines and returns a list of vectors
4682 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4683                                                   force-new dependencies
4684                                                   group also-fetch-heads)
4685   "Parse the news overview data in the server buffer, and return a
4686 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4687   ;; Get the Xref when the users reads the articles since most/some
4688   ;; NNTP servers do not include Xrefs when using XOVER.
4689   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4690   (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
4691         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced)
4692         (cur nntp-server-buffer)
4693         (dependencies (or dependencies gnus-newsgroup-dependencies))
4694         number headers header)
4695     (save-excursion
4696       (set-buffer nntp-server-buffer)
4697       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4698       ;; Allow the user to mangle the headers before parsing them.
4699       (gnus-run-hooks 'gnus-parse-headers-hook)
4700       (goto-char (point-min))
4701       (while (not (eobp))
4702         (condition-case ()
4703             (while (and sequence (not (eobp)))
4704               (setq number (read cur))
4705               (while (and sequence
4706                           (< (car sequence) number))
4707                 (setq sequence (cdr sequence)))
4708               (and sequence
4709                    (eq number (car sequence))
4710                    (progn
4711                      (setq sequence (cdr sequence))
4712                      (setq header (inline
4713                                     (gnus-nov-parse-line
4714                                      number dependencies force-new))))
4715                    (push header headers))
4716               (forward-line 1))
4717           (error
4718            (gnus-error 4 "Strange nov line (%d)"
4719                        (count-lines (point-min) (point)))))
4720         (forward-line 1))
4721       ;; A common bug in inn is that if you have posted an article and
4722       ;; then retrieves the active file, it will answer correctly --
4723       ;; the new article is included.  However, a NOV entry for the
4724       ;; article may not have been generated yet, so this may fail.
4725       ;; We work around this problem by retrieving the last few
4726       ;; headers using HEAD.
4727       (if (or (not also-fetch-heads)
4728               (not sequence))
4729           ;; We (probably) got all the headers.
4730           (nreverse headers)
4731         (let ((gnus-nov-is-evil t))
4732           (nconc
4733            (nreverse headers)
4734            (when (gnus-retrieve-headers sequence group)
4735              (gnus-get-newsgroup-headers))))))))
4736
4737 (defun gnus-article-get-xrefs ()
4738   "Fill in the Xref value in `gnus-current-headers', if necessary.
4739 This is meant to be called in `gnus-article-internal-prepare-hook'."
4740   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4741                                  gnus-current-headers)))
4742     (or (not gnus-use-cross-reference)
4743         (not headers)
4744         (and (mail-header-xref headers)
4745              (not (string= (mail-header-xref headers) "")))
4746         (let ((case-fold-search t)
4747               xref)
4748           (save-restriction
4749             (nnheader-narrow-to-headers)
4750             (goto-char (point-min))
4751             (when (or (and (eq (downcase (char-after)) ?x)
4752                            (looking-at "Xref:"))
4753                       (search-forward "\nXref:" nil t))
4754               (goto-char (1+ (match-end 0)))
4755               (setq xref (buffer-substring (point)
4756                                            (progn (end-of-line) (point))))
4757               (mail-header-set-xref headers xref)))))))
4758
4759 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4760   "Find article ID and insert the summary line for that article.
4761 OLD-HEADER can either be a header or a line number to insert
4762 the subject line on."
4763   (let* ((line (and (numberp old-header) old-header))
4764          (old-header (and (vectorp old-header) old-header))
4765          (header (cond ((and old-header use-old-header)
4766                         old-header)
4767                        ((and (numberp id)
4768                              (gnus-number-to-header id))
4769                         (gnus-number-to-header id))
4770                        (t
4771                         (gnus-read-header id))))
4772          (number (and (numberp id) id))
4773          d)
4774     (when header
4775       ;; Rebuild the thread that this article is part of and go to the
4776       ;; article we have fetched.
4777       (when (and (not gnus-show-threads)
4778                  old-header)
4779         (when (and number
4780                    (setq d (gnus-data-find (mail-header-number old-header))))
4781           (goto-char (gnus-data-pos d))
4782           (gnus-data-remove
4783            number
4784            (- (gnus-point-at-bol)
4785               (prog1
4786                   (1+ (gnus-point-at-eol))
4787                 (gnus-delete-line))))))
4788       (when old-header
4789         (mail-header-set-number header (mail-header-number old-header)))
4790       (setq gnus-newsgroup-sparse
4791             (delq (setq number (mail-header-number header))
4792                   gnus-newsgroup-sparse))
4793       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4794       (push number gnus-newsgroup-limit)
4795       (gnus-rebuild-thread (mail-header-id header) line)
4796       (gnus-summary-goto-subject number nil t))
4797     (when (and (numberp number)
4798                (> number 0))
4799       ;; We have to update the boundaries even if we can't fetch the
4800       ;; article if ID is a number -- so that the next `P' or `N'
4801       ;; command will fetch the previous (or next) article even
4802       ;; if the one we tried to fetch this time has been canceled.
4803       (when (> number gnus-newsgroup-end)
4804         (setq gnus-newsgroup-end number))
4805       (when (< number gnus-newsgroup-begin)
4806         (setq gnus-newsgroup-begin number))
4807       (setq gnus-newsgroup-unselected
4808             (delq number gnus-newsgroup-unselected)))
4809     ;; Report back a success?
4810     (and header (mail-header-number header))))
4811
4812 ;;; Process/prefix in the summary buffer
4813
4814 (defun gnus-summary-work-articles (n)
4815   "Return a list of articles to be worked upon.
4816 The prefix argument, the list of process marked articles, and the
4817 current article will be taken into consideration."
4818   (save-excursion
4819     (set-buffer gnus-summary-buffer)
4820     (cond
4821      (n
4822       ;; A numerical prefix has been given.
4823       (setq n (prefix-numeric-value n))
4824       (let ((backward (< n 0))
4825             (n (abs (prefix-numeric-value n)))
4826             articles article)
4827         (save-excursion
4828           (while
4829               (and (> n 0)
4830                    (push (setq article (gnus-summary-article-number))
4831                          articles)
4832                    (if backward
4833                        (gnus-summary-find-prev nil article)
4834                      (gnus-summary-find-next nil article)))
4835             (decf n)))
4836         (nreverse articles)))
4837      ((and (gnus-region-active-p) (mark))
4838       (message "region active")
4839       ;; Work on the region between point and mark.
4840       (let ((max (max (point) (mark)))
4841             articles article)
4842         (save-excursion
4843           (goto-char (min (min (point) (mark))))
4844           (while
4845               (and
4846                (push (setq article (gnus-summary-article-number)) articles)
4847                (gnus-summary-find-next nil article)
4848                (< (point) max)))
4849           (nreverse articles))))
4850      (gnus-newsgroup-processable
4851       ;; There are process-marked articles present.
4852       ;; Save current state.
4853       (gnus-summary-save-process-mark)
4854       ;; Return the list.
4855       (reverse gnus-newsgroup-processable))
4856      (t
4857       ;; Just return the current article.
4858       (list (gnus-summary-article-number))))))
4859
4860 (defmacro gnus-summary-iterate (arg &rest forms)
4861   "Iterate over the process/prefixed articles and do FORMS.
4862 ARG is the interactive prefix given to the command.  FORMS will be
4863 executed with point over the summary line of the articles."
4864   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4865     `(let ((,articles (gnus-summary-work-articles ,arg)))
4866        (while ,articles
4867          (gnus-summary-goto-subject (car ,articles))
4868          ,@forms))))
4869
4870 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4871 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4872
4873 (defun gnus-summary-save-process-mark ()
4874   "Push the current set of process marked articles on the stack."
4875   (interactive)
4876   (push (copy-sequence gnus-newsgroup-processable)
4877         gnus-newsgroup-process-stack))
4878
4879 (defun gnus-summary-kill-process-mark ()
4880   "Push the current set of process marked articles on the stack and unmark."
4881   (interactive)
4882   (gnus-summary-save-process-mark)
4883   (gnus-summary-unmark-all-processable))
4884
4885 (defun gnus-summary-yank-process-mark ()
4886   "Pop the last process mark state off the stack and restore it."
4887   (interactive)
4888   (unless gnus-newsgroup-process-stack
4889     (error "Empty mark stack"))
4890   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4891
4892 (defun gnus-summary-process-mark-set (set)
4893   "Make SET into the current process marked articles."
4894   (gnus-summary-unmark-all-processable)
4895   (while set
4896     (gnus-summary-set-process-mark (pop set))))
4897
4898 ;;; Searching and stuff
4899
4900 (defun gnus-summary-search-group (&optional backward use-level)
4901   "Search for next unread newsgroup.
4902 If optional argument BACKWARD is non-nil, search backward instead."
4903   (save-excursion
4904     (set-buffer gnus-group-buffer)
4905     (when (gnus-group-search-forward
4906            backward nil (if use-level (gnus-group-group-level) nil))
4907       (gnus-group-group-name))))
4908
4909 (defun gnus-summary-best-group (&optional exclude-group)
4910   "Find the name of the best unread group.
4911 If EXCLUDE-GROUP, do not go to this group."
4912   (save-excursion
4913     (set-buffer gnus-group-buffer)
4914     (save-excursion
4915       (gnus-group-best-unread-group exclude-group))))
4916
4917 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4918   (if backward (gnus-summary-find-prev)
4919     (let* ((dummy (gnus-summary-article-intangible-p))
4920            (article (or article (gnus-summary-article-number)))
4921            (arts (gnus-data-find-list article))
4922            result)
4923       (when (and (not dummy)
4924                  (or (not gnus-summary-check-current)
4925                      (not unread)
4926                      (not (gnus-data-unread-p (car arts)))))
4927         (setq arts (cdr arts)))
4928       (when (setq result
4929                   (if unread
4930                       (progn
4931                         (while arts
4932                           (when (or (and undownloaded
4933                                          (eq gnus-undownloaded-mark
4934                                              (gnus-data-mark (car arts))))
4935                                     (gnus-data-unread-p (car arts)))
4936                             (setq result (car arts)
4937                                   arts nil))
4938                           (setq arts (cdr arts)))
4939                         result)
4940                     (car arts)))
4941         (goto-char (gnus-data-pos result))
4942         (gnus-data-number result)))))
4943
4944 (defun gnus-summary-find-prev (&optional unread article)
4945   (let* ((eobp (eobp))
4946          (article (or article (gnus-summary-article-number)))
4947          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4948          result)
4949     (when (and (not eobp)
4950                (or (not gnus-summary-check-current)
4951                    (not unread)
4952                    (not (gnus-data-unread-p (car arts)))))
4953       (setq arts (cdr arts)))
4954     (when (setq result
4955                 (if unread
4956                     (progn
4957                       (while arts
4958                         (when (gnus-data-unread-p (car arts))
4959                           (setq result (car arts)
4960                                 arts nil))
4961                         (setq arts (cdr arts)))
4962                       result)
4963                   (car arts)))
4964       (goto-char (gnus-data-pos result))
4965       (gnus-data-number result))))
4966
4967 (defun gnus-summary-find-subject (subject &optional unread backward article)
4968   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4969          (article (or article (gnus-summary-article-number)))
4970          (articles (gnus-data-list backward))
4971          (arts (gnus-data-find-list article articles))
4972          result)
4973     (when (or (not gnus-summary-check-current)
4974               (not unread)
4975               (not (gnus-data-unread-p (car arts))))
4976       (setq arts (cdr arts)))
4977     (while arts
4978       (and (or (not unread)
4979                (gnus-data-unread-p (car arts)))
4980            (vectorp (gnus-data-header (car arts)))
4981            (gnus-subject-equal
4982             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4983            (setq result (car arts)
4984                  arts nil))
4985       (setq arts (cdr arts)))
4986     (and result
4987          (goto-char (gnus-data-pos result))
4988          (gnus-data-number result))))
4989
4990 (defun gnus-summary-search-forward (&optional unread subject backward)
4991   "Search forward for an article.
4992 If UNREAD, look for unread articles.  If SUBJECT, look for
4993 articles with that subject.  If BACKWARD, search backward instead."
4994   (cond (subject (gnus-summary-find-subject subject unread backward))
4995         (backward (gnus-summary-find-prev unread))
4996         (t (gnus-summary-find-next unread))))
4997
4998 (defun gnus-recenter (&optional n)
4999   "Center point in window and redisplay frame.
5000 Also do horizontal recentering."
5001   (interactive "P")
5002   (when (and gnus-auto-center-summary
5003              (not (eq gnus-auto-center-summary 'vertical)))
5004     (gnus-horizontal-recenter))
5005   (recenter n))
5006
5007 (defun gnus-summary-recenter ()
5008   "Center point in the summary window.
5009 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5010 displayed, no centering will be performed."
5011   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5012   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5013   (let* ((top (cond ((< (window-height) 4) 0)
5014                     ((< (window-height) 7) 1)
5015                     (t (if (numberp gnus-auto-center-summary)
5016                            gnus-auto-center-summary
5017                          2))))
5018          (height (1- (window-height)))
5019          (bottom (save-excursion (goto-char (point-max))
5020                                  (forward-line (- height))
5021                                  (point)))
5022          (window (get-buffer-window (current-buffer))))
5023     ;; The user has to want it.
5024     (when gnus-auto-center-summary
5025       (when (get-buffer-window gnus-article-buffer)
5026         ;; Only do recentering when the article buffer is displayed,
5027         ;; Set the window start to either `bottom', which is the biggest
5028         ;; possible valid number, or the second line from the top,
5029         ;; whichever is the least.
5030         (set-window-start
5031          window (min bottom (save-excursion
5032                               (forward-line (- top)) (point)))))
5033       ;; Do horizontal recentering while we're at it.
5034       (when (and (get-buffer-window (current-buffer) t)
5035                  (not (eq gnus-auto-center-summary 'vertical)))
5036         (let ((selected (selected-window)))
5037           (select-window (get-buffer-window (current-buffer) t))
5038           (gnus-summary-position-point)
5039           (gnus-horizontal-recenter)
5040           (select-window selected))))))
5041
5042 (defun gnus-summary-jump-to-group (newsgroup)
5043   "Move point to NEWSGROUP in group mode buffer."
5044   ;; Keep update point of group mode buffer if visible.
5045   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5046       (save-window-excursion
5047         ;; Take care of tree window mode.
5048         (when (get-buffer-window gnus-group-buffer)
5049           (pop-to-buffer gnus-group-buffer))
5050         (gnus-group-jump-to-group newsgroup))
5051     (save-excursion
5052       ;; Take care of tree window mode.
5053       (if (get-buffer-window gnus-group-buffer)
5054           (pop-to-buffer gnus-group-buffer)
5055         (set-buffer gnus-group-buffer))
5056       (gnus-group-jump-to-group newsgroup))))
5057
5058 ;; This function returns a list of article numbers based on the
5059 ;; difference between the ranges of read articles in this group and
5060 ;; the range of active articles.
5061 (defun gnus-list-of-unread-articles (group)
5062   (let* ((read (gnus-info-read (gnus-get-info group)))
5063          (active (or (gnus-active group) (gnus-activate-group group)))
5064          (last (cdr active))
5065          first nlast unread)
5066     ;; If none are read, then all are unread.
5067     (if (not read)
5068         (setq first (car active))
5069       ;; If the range of read articles is a single range, then the
5070       ;; first unread article is the article after the last read
5071       ;; article.  Sounds logical, doesn't it?
5072       (if (not (listp (cdr read)))
5073           (setq first (max (car active) (1+ (cdr read))))
5074         ;; `read' is a list of ranges.
5075         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5076                                   (caar read)))
5077                   1)
5078           (setq first (car active)))
5079         (while read
5080           (when first
5081             (while (< first nlast)
5082               (push first unread)
5083               (setq first (1+ first))))
5084           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5085           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5086           (setq read (cdr read)))))
5087     ;; And add the last unread articles.
5088     (while (<= first last)
5089       (push first unread)
5090       (setq first (1+ first)))
5091     ;; Return the list of unread articles.
5092     (delq 0 (nreverse unread))))
5093
5094 (defun gnus-list-of-read-articles (group)
5095   "Return a list of unread, unticked and non-dormant articles."
5096   (let* ((info (gnus-get-info group))
5097          (marked (gnus-info-marks info))
5098          (active (gnus-active group)))
5099     (and info active
5100          (gnus-set-difference
5101           (gnus-sorted-complement
5102            (gnus-uncompress-range active)
5103            (gnus-list-of-unread-articles group))
5104           (append
5105            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5106            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5107
5108 ;; Various summary commands
5109
5110 (defun gnus-summary-select-article-buffer ()
5111   "Reconfigure windows to show article buffer."
5112   (interactive)
5113   (if (not (gnus-buffer-live-p gnus-article-buffer))
5114       (error "There is no article buffer for this summary buffer")
5115     (gnus-configure-windows 'article)
5116     (select-window (get-buffer-window gnus-article-buffer))))
5117
5118 (defun gnus-summary-universal-argument (arg)
5119   "Perform any operation on all articles that are process/prefixed."
5120   (interactive "P")
5121   (let ((articles (gnus-summary-work-articles arg))
5122         func article)
5123     (if (eq
5124          (setq
5125           func
5126           (key-binding
5127            (read-key-sequence
5128             (substitute-command-keys
5129              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5130              ))))
5131          'undefined)
5132         (gnus-error 1 "Undefined key")
5133       (save-excursion
5134         (while articles
5135           (gnus-summary-goto-subject (setq article (pop articles)))
5136           (let (gnus-newsgroup-processable)
5137             (command-execute func))
5138           (gnus-summary-remove-process-mark article)))))
5139   (gnus-summary-position-point))
5140
5141 (defun gnus-summary-toggle-truncation (&optional arg)
5142   "Toggle truncation of summary lines.
5143 With arg, turn line truncation on iff arg is positive."
5144   (interactive "P")
5145   (setq truncate-lines
5146         (if (null arg) (not truncate-lines)
5147           (> (prefix-numeric-value arg) 0)))
5148   (redraw-display))
5149
5150 (defun gnus-summary-reselect-current-group (&optional all rescan)
5151   "Exit and then reselect the current newsgroup.
5152 The prefix argument ALL means to select all articles."
5153   (interactive "P")
5154   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5155     (error "Ephemeral groups can't be reselected"))
5156   (let ((current-subject (gnus-summary-article-number))
5157         (group gnus-newsgroup-name))
5158     (setq gnus-newsgroup-begin nil)
5159     (gnus-summary-exit)
5160     ;; We have to adjust the point of group mode buffer because
5161     ;; point was moved to the next unread newsgroup by exiting.
5162     (gnus-summary-jump-to-group group)
5163     (when rescan
5164       (save-excursion
5165         (gnus-group-get-new-news-this-group 1)))
5166     (gnus-group-read-group all t)
5167     (gnus-summary-goto-subject current-subject nil t)))
5168
5169 (defun gnus-summary-rescan-group (&optional all)
5170   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5171   (interactive "P")
5172   (gnus-summary-reselect-current-group all t))
5173
5174 (defun gnus-summary-update-info (&optional non-destructive)
5175   (save-excursion
5176     (let ((group gnus-newsgroup-name))
5177       (when group
5178         (when gnus-newsgroup-kill-headers
5179           (setq gnus-newsgroup-killed
5180                 (gnus-compress-sequence
5181                  (nconc
5182                   (gnus-set-sorted-intersection
5183                    (gnus-uncompress-range gnus-newsgroup-killed)
5184                    (setq gnus-newsgroup-unselected
5185                          (sort gnus-newsgroup-unselected '<)))
5186                   (setq gnus-newsgroup-unreads
5187                         (sort gnus-newsgroup-unreads '<)))
5188                  t)))
5189         (unless (listp (cdr gnus-newsgroup-killed))
5190           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5191         (let ((headers gnus-newsgroup-headers))
5192           ;; Set the new ranges of read articles.
5193           (save-excursion
5194             (set-buffer gnus-group-buffer)
5195             (gnus-undo-force-boundary))
5196           (gnus-update-read-articles
5197            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5198           ;; Set the current article marks.
5199           (let ((gnus-newsgroup-scored
5200                  (if (and (not gnus-save-score)
5201                           (not non-destructive))
5202                      nil
5203                    gnus-newsgroup-scored)))
5204             (save-excursion
5205               (gnus-update-marks)))
5206           ;; Do the cross-ref thing.
5207           (when gnus-use-cross-reference
5208             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5209           ;; Do not switch windows but change the buffer to work.
5210           (set-buffer gnus-group-buffer)
5211           (unless (gnus-ephemeral-group-p group)
5212             (gnus-group-update-group group)))))))
5213
5214 (defun gnus-summary-save-newsrc (&optional force)
5215   "Save the current number of read/marked articles in the dribble buffer.
5216 The dribble buffer will then be saved.
5217 If FORCE (the prefix), also save the .newsrc file(s)."
5218   (interactive "P")
5219   (gnus-summary-update-info t)
5220   (if force
5221       (gnus-save-newsrc-file)
5222     (gnus-dribble-save)))
5223
5224 (defun gnus-summary-exit (&optional temporary)
5225   "Exit reading current newsgroup, and then return to group selection mode.
5226 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5227   (interactive)
5228   (gnus-set-global-variables)
5229   (gnus-kill-save-kill-buffer)
5230   (gnus-async-halt-prefetch)
5231   (let* ((group gnus-newsgroup-name)
5232          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5233          (mode major-mode)
5234          (group-point nil)
5235          (buf (current-buffer)))
5236     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5237     ;; If we have several article buffers, we kill them at exit.
5238     (unless gnus-single-article-buffer
5239       (gnus-kill-buffer gnus-original-article-buffer)
5240       (setq gnus-article-current nil))
5241     (when gnus-use-cache
5242       (gnus-cache-possibly-remove-articles)
5243       (gnus-cache-save-buffers))
5244     (gnus-async-prefetch-remove-group group)
5245     (when gnus-suppress-duplicates
5246       (gnus-dup-enter-articles))
5247     (when gnus-use-trees
5248       (gnus-tree-close group))
5249     ;; Remove entries for this group.
5250     (nnmail-purge-split-history (gnus-group-real-name group))
5251     ;; Make all changes in this group permanent.
5252     (unless quit-config
5253       (gnus-run-hooks 'gnus-exit-group-hook)
5254       (gnus-summary-update-info)
5255       ;; Do adaptive scoring, and possibly save score files.
5256       (when gnus-newsgroup-adaptive
5257         (gnus-score-adaptive))
5258       (when gnus-use-scoring
5259         (gnus-score-save)))
5260     (gnus-close-group group)
5261     ;; Make sure where we were, and go to next newsgroup.
5262     (set-buffer gnus-group-buffer)
5263     (unless quit-config
5264       (gnus-group-jump-to-group group))
5265     (gnus-run-hooks 'gnus-summary-exit-hook)
5266     (unless (or quit-config
5267                 ;; If this group has disappeared from the summary
5268                 ;; buffer, don't skip forwards.
5269                 (not (string= group (gnus-group-group-name))))
5270       (gnus-group-next-unread-group 1))
5271     (setq group-point (point))
5272     (if temporary
5273         nil                             ;Nothing to do.
5274       (when (gnus-buffer-live-p gnus-article-buffer)
5275         (save-excursion
5276           (set-buffer gnus-article-buffer)
5277           (mm-destroy-parts gnus-article-mime-handles)))
5278       ;; If we have several article buffers, we kill them at exit.
5279       (unless gnus-single-article-buffer
5280         (gnus-kill-buffer gnus-article-buffer)
5281         (gnus-kill-buffer gnus-original-article-buffer)
5282         (setq gnus-article-current nil))
5283       (set-buffer buf)
5284       (if (not gnus-kill-summary-on-exit)
5285           (gnus-deaden-summary)
5286         ;; We set all buffer-local variables to nil.  It is unclear why
5287         ;; this is needed, but if we don't, buffer-local variables are
5288         ;; not garbage-collected, it seems.  This would the lead to en
5289         ;; ever-growing Emacs.
5290         (gnus-summary-clear-local-variables)
5291         (when (get-buffer gnus-article-buffer)
5292           (bury-buffer gnus-article-buffer))
5293         ;; We clear the global counterparts of the buffer-local
5294         ;; variables as well, just to be on the safe side.
5295         (set-buffer gnus-group-buffer)
5296         (gnus-summary-clear-local-variables)
5297         ;; Return to group mode buffer.
5298         (when (eq mode 'gnus-summary-mode)
5299           (gnus-kill-buffer buf)))
5300       (setq gnus-current-select-method gnus-select-method)
5301       (pop-to-buffer gnus-group-buffer)
5302       (if (not quit-config)
5303           (progn
5304             (goto-char group-point)
5305             (gnus-configure-windows 'group 'force))
5306         (gnus-handle-ephemeral-exit quit-config))
5307       ;; Clear the current group name.
5308       (unless quit-config
5309         (setq gnus-newsgroup-name nil)))))
5310
5311 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5312 (defun gnus-summary-exit-no-update (&optional no-questions)
5313   "Quit reading current newsgroup without updating read article info."
5314   (interactive)
5315   (let* ((group gnus-newsgroup-name)
5316          (quit-config (gnus-group-quit-config group)))
5317     (when (or no-questions
5318               gnus-expert-user
5319               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5320       (gnus-async-halt-prefetch)
5321       (gnus-run-hooks (delq 'gnus-summary-expire-articles 
5322                             (copy-list gnus-summary-prepare-exit-hook)))
5323       (when (gnus-buffer-live-p gnus-article-buffer)
5324         (save-excursion
5325           (set-buffer gnus-article-buffer)
5326           (mm-destroy-parts gnus-article-mime-handles)))
5327       ;; If we have several article buffers, we kill them at exit.
5328       (unless gnus-single-article-buffer
5329         (gnus-kill-buffer gnus-article-buffer)
5330         (gnus-kill-buffer gnus-original-article-buffer)
5331         (setq gnus-article-current nil))
5332       (if (not gnus-kill-summary-on-exit)
5333           (gnus-deaden-summary)
5334         (gnus-close-group group)
5335         (gnus-summary-clear-local-variables)
5336         (set-buffer gnus-group-buffer)
5337         (gnus-summary-clear-local-variables)
5338         (when (get-buffer gnus-summary-buffer)
5339           (kill-buffer gnus-summary-buffer)))
5340       (unless gnus-single-article-buffer
5341         (setq gnus-article-current nil))
5342       (when gnus-use-trees
5343         (gnus-tree-close group))
5344       (gnus-async-prefetch-remove-group group)
5345       (when (get-buffer gnus-article-buffer)
5346         (bury-buffer gnus-article-buffer))
5347       ;; Return to the group buffer.
5348       (gnus-configure-windows 'group 'force)
5349       ;; Clear the current group name.
5350       (setq gnus-newsgroup-name nil)
5351       (when (equal (gnus-group-group-name) group)
5352         (gnus-group-next-unread-group 1))
5353       (when quit-config
5354         (gnus-handle-ephemeral-exit quit-config)))))
5355
5356 (defun gnus-handle-ephemeral-exit (quit-config)
5357   "Handle movement when leaving an ephemeral group.
5358 The state which existed when entering the ephemeral is reset."
5359   (if (not (buffer-name (car quit-config)))
5360       (gnus-configure-windows 'group 'force)
5361     (set-buffer (car quit-config))
5362     (cond ((eq major-mode 'gnus-summary-mode)
5363            (gnus-set-global-variables))
5364           ((eq major-mode 'gnus-article-mode)
5365            (save-excursion
5366              ;; The `gnus-summary-buffer' variable may point
5367              ;; to the old summary buffer when using a single
5368              ;; article buffer.
5369              (unless (gnus-buffer-live-p gnus-summary-buffer)
5370                (set-buffer gnus-group-buffer))
5371              (set-buffer gnus-summary-buffer)
5372              (gnus-set-global-variables))))
5373     (if (or (eq (cdr quit-config) 'article)
5374             (eq (cdr quit-config) 'pick))
5375         (progn
5376           ;; The current article may be from the ephemeral group
5377           ;; thus it is best that we reload this article
5378           (gnus-summary-show-article)
5379           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5380               (gnus-configure-windows 'pick 'force)
5381             (gnus-configure-windows (cdr quit-config) 'force)))
5382       (gnus-configure-windows (cdr quit-config) 'force))
5383     (when (eq major-mode 'gnus-summary-mode)
5384       (gnus-summary-next-subject 1 nil t)
5385       (gnus-summary-recenter)
5386       (gnus-summary-position-point))))
5387
5388 ;;; Dead summaries.
5389
5390 (defvar gnus-dead-summary-mode-map nil)
5391
5392 (unless gnus-dead-summary-mode-map
5393   (setq gnus-dead-summary-mode-map (make-keymap))
5394   (suppress-keymap gnus-dead-summary-mode-map)
5395   (substitute-key-definition
5396    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5397   (let ((keys '("\C-d" "\r" "\177" [delete])))
5398     (while keys
5399       (define-key gnus-dead-summary-mode-map
5400         (pop keys) 'gnus-summary-wake-up-the-dead))))
5401
5402 (defvar gnus-dead-summary-mode nil
5403   "Minor mode for Gnus summary buffers.")
5404
5405 (defun gnus-dead-summary-mode (&optional arg)
5406   "Minor mode for Gnus summary buffers."
5407   (interactive "P")
5408   (when (eq major-mode 'gnus-summary-mode)
5409     (make-local-variable 'gnus-dead-summary-mode)
5410     (setq gnus-dead-summary-mode
5411           (if (null arg) (not gnus-dead-summary-mode)
5412             (> (prefix-numeric-value arg) 0)))
5413     (when gnus-dead-summary-mode
5414       (gnus-add-minor-mode
5415        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5416
5417 (defun gnus-deaden-summary ()
5418   "Make the current summary buffer into a dead summary buffer."
5419   ;; Kill any previous dead summary buffer.
5420   (when (and gnus-dead-summary
5421              (buffer-name gnus-dead-summary))
5422     (save-excursion
5423       (set-buffer gnus-dead-summary)
5424       (when gnus-dead-summary-mode
5425         (kill-buffer (current-buffer)))))
5426   ;; Make this the current dead summary.
5427   (setq gnus-dead-summary (current-buffer))
5428   (gnus-dead-summary-mode 1)
5429   (let ((name (buffer-name)))
5430     (when (string-match "Summary" name)
5431       (rename-buffer
5432        (concat (substring name 0 (match-beginning 0)) "Dead "
5433                (substring name (match-beginning 0)))
5434        t))))
5435
5436 (defun gnus-kill-or-deaden-summary (buffer)
5437   "Kill or deaden the summary BUFFER."
5438   (save-excursion
5439     (when (and (buffer-name buffer)
5440                (not gnus-single-article-buffer))
5441       (save-excursion
5442         (set-buffer buffer)
5443         (gnus-kill-buffer gnus-article-buffer)
5444         (gnus-kill-buffer gnus-original-article-buffer)))
5445     (cond (gnus-kill-summary-on-exit
5446            (when (and gnus-use-trees
5447                       (gnus-buffer-exists-p buffer))
5448              (save-excursion
5449                (set-buffer buffer)
5450                (gnus-tree-close gnus-newsgroup-name)))
5451            (gnus-kill-buffer buffer))
5452           ((gnus-buffer-exists-p buffer)
5453            (save-excursion
5454              (set-buffer buffer)
5455              (gnus-deaden-summary))))))
5456
5457 (defun gnus-summary-wake-up-the-dead (&rest args)
5458   "Wake up the dead summary buffer."
5459   (interactive)
5460   (gnus-dead-summary-mode -1)
5461   (let ((name (buffer-name)))
5462     (when (string-match "Dead " name)
5463       (rename-buffer
5464        (concat (substring name 0 (match-beginning 0))
5465                (substring name (match-end 0)))
5466        t)))
5467   (gnus-message 3 "This dead summary is now alive again"))
5468
5469 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5470 (defun gnus-summary-fetch-faq (&optional faq-dir)
5471   "Fetch the FAQ for the current group.
5472 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5473 in."
5474   (interactive
5475    (list
5476     (when current-prefix-arg
5477       (completing-read
5478        "Faq dir: " (and (listp gnus-group-faq-directory)
5479                         (mapcar (lambda (file) (list file))
5480                                 gnus-group-faq-directory))))))
5481   (let (gnus-faq-buffer)
5482     (when (setq gnus-faq-buffer
5483                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5484       (gnus-configure-windows 'summary-faq))))
5485
5486 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5487 (defun gnus-summary-describe-group (&optional force)
5488   "Describe the current newsgroup."
5489   (interactive "P")
5490   (gnus-group-describe-group force gnus-newsgroup-name))
5491
5492 (defun gnus-summary-describe-briefly ()
5493   "Describe summary mode commands briefly."
5494   (interactive)
5495   (gnus-message 6
5496                 (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")))
5497
5498 ;; Walking around group mode buffer from summary mode.
5499
5500 (defun gnus-summary-next-group (&optional no-article target-group backward)
5501   "Exit current newsgroup and then select next unread newsgroup.
5502 If prefix argument NO-ARTICLE is non-nil, no article is selected
5503 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5504 previous group instead."
5505   (interactive "P")
5506   ;; Stop pre-fetching.
5507   (gnus-async-halt-prefetch)
5508   (let ((current-group gnus-newsgroup-name)
5509         (current-buffer (current-buffer))
5510         entered)
5511     ;; First we semi-exit this group to update Xrefs and all variables.
5512     ;; We can't do a real exit, because the window conf must remain
5513     ;; the same in case the user is prompted for info, and we don't
5514     ;; want the window conf to change before that...
5515     (gnus-summary-exit t)
5516     (while (not entered)
5517       ;; Then we find what group we are supposed to enter.
5518       (set-buffer gnus-group-buffer)
5519       (gnus-group-jump-to-group current-group)
5520       (setq target-group
5521             (or target-group
5522                 (if (eq gnus-keep-same-level 'best)
5523                     (gnus-summary-best-group gnus-newsgroup-name)
5524                   (gnus-summary-search-group backward gnus-keep-same-level))))
5525       (if (not target-group)
5526           ;; There are no further groups, so we return to the group
5527           ;; buffer.
5528           (progn
5529             (gnus-message 5 "Returning to the group buffer")
5530             (setq entered t)
5531             (when (gnus-buffer-live-p current-buffer)
5532               (set-buffer current-buffer)
5533               (gnus-summary-exit))
5534             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5535         ;; We try to enter the target group.
5536         (gnus-group-jump-to-group target-group)
5537         (let ((unreads (gnus-group-group-unread)))
5538           (if (and (or (eq t unreads)
5539                        (and unreads (not (zerop unreads))))
5540                    (gnus-summary-read-group
5541                     target-group nil no-article
5542                     (and (buffer-name current-buffer) current-buffer)
5543                     nil backward))
5544               (setq entered t)
5545             (setq current-group target-group
5546                   target-group nil)))))))
5547
5548 (defun gnus-summary-prev-group (&optional no-article)
5549   "Exit current newsgroup and then select previous unread newsgroup.
5550 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5551   (interactive "P")
5552   (gnus-summary-next-group no-article nil t))
5553
5554 ;; Walking around summary lines.
5555
5556 (defun gnus-summary-first-subject (&optional unread undownloaded)
5557   "Go to the first unread subject.
5558 If UNREAD is non-nil, go to the first unread article.
5559 Returns the article selected or nil if there are no unread articles."
5560   (interactive "P")
5561   (prog1
5562       (cond
5563        ;; Empty summary.
5564        ((null gnus-newsgroup-data)
5565         (gnus-message 3 "No articles in the group")
5566         nil)
5567        ;; Pick the first article.
5568        ((not unread)
5569         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5570         (gnus-data-number (car gnus-newsgroup-data)))
5571        ;; No unread articles.
5572        ((null gnus-newsgroup-unreads)
5573         (gnus-message 3 "No more unread articles")
5574         nil)
5575        ;; Find the first unread article.
5576        (t
5577         (let ((data gnus-newsgroup-data))
5578           (while (and data
5579                       (and (not (and undownloaded
5580                                      (eq gnus-undownloaded-mark
5581                                          (gnus-data-mark (car data)))))
5582                            (not (gnus-data-unread-p (car data)))))
5583             (setq data (cdr data)))
5584           (when data
5585             (goto-char (gnus-data-pos (car data)))
5586             (gnus-data-number (car data))))))
5587     (gnus-summary-position-point)))
5588
5589 (defun gnus-summary-next-subject (n &optional unread dont-display)
5590   "Go to next N'th summary line.
5591 If N is negative, go to the previous N'th subject line.
5592 If UNREAD is non-nil, only unread articles are selected.
5593 The difference between N and the actual number of steps taken is
5594 returned."
5595   (interactive "p")
5596   (let ((backward (< n 0))
5597         (n (abs n)))
5598     (while (and (> n 0)
5599                 (if backward
5600                     (gnus-summary-find-prev unread)
5601                   (gnus-summary-find-next unread)))
5602       (gnus-summary-show-thread)
5603       (setq n (1- n)))
5604     (when (/= 0 n)
5605       (gnus-message 7 "No more%s articles"
5606                     (if unread " unread" "")))
5607     (unless dont-display
5608       (gnus-summary-recenter)
5609       (gnus-summary-position-point))
5610     n))
5611
5612 (defun gnus-summary-next-unread-subject (n)
5613   "Go to next N'th unread summary line."
5614   (interactive "p")
5615   (gnus-summary-next-subject n t))
5616
5617 (defun gnus-summary-prev-subject (n &optional unread)
5618   "Go to previous N'th summary line.
5619 If optional argument UNREAD is non-nil, only unread article is selected."
5620   (interactive "p")
5621   (gnus-summary-next-subject (- n) unread))
5622
5623 (defun gnus-summary-prev-unread-subject (n)
5624   "Go to previous N'th unread summary line."
5625   (interactive "p")
5626   (gnus-summary-next-subject (- n) t))
5627
5628 (defun gnus-summary-goto-subject (article &optional force silent)
5629   "Go the subject line of ARTICLE.
5630 If FORCE, also allow jumping to articles not currently shown."
5631   (interactive "nArticle number: ")
5632   (let ((b (point))
5633         (data (gnus-data-find article)))
5634     ;; We read in the article if we have to.
5635     (and (not data)
5636          force
5637          (gnus-summary-insert-subject
5638           article
5639           (if (or (numberp force) (vectorp force)) force)
5640           t)
5641          (setq data (gnus-data-find article)))
5642     (goto-char b)
5643     (if (not data)
5644         (progn
5645           (unless silent
5646             (gnus-message 3 "Can't find article %d" article))
5647           nil)
5648       (goto-char (gnus-data-pos data))
5649       (gnus-summary-position-point)
5650       article)))
5651
5652 ;; Walking around summary lines with displaying articles.
5653
5654 (defun gnus-summary-expand-window (&optional arg)
5655   "Make the summary buffer take up the entire Emacs frame.
5656 Given a prefix, will force an `article' buffer configuration."
5657   (interactive "P")
5658   (if arg
5659       (gnus-configure-windows 'article 'force)
5660     (gnus-configure-windows 'summary 'force)))
5661
5662 (defun gnus-summary-display-article (article &optional all-header)
5663   "Display ARTICLE in article buffer."
5664   (gnus-set-global-variables)
5665   (if (null article)
5666       nil
5667     (prog1
5668         (if gnus-summary-display-article-function
5669             (funcall gnus-summary-display-article-function article all-header)
5670           (gnus-article-prepare article all-header))
5671       (gnus-run-hooks 'gnus-select-article-hook)
5672       (when (and gnus-current-article
5673                  (not (zerop gnus-current-article)))
5674         (gnus-summary-goto-subject gnus-current-article))
5675       (gnus-summary-recenter)
5676       (when (and gnus-use-trees gnus-show-threads)
5677         (gnus-possibly-generate-tree article)
5678         (gnus-highlight-selected-tree article))
5679       ;; Successfully display article.
5680       (gnus-article-set-window-start
5681        (cdr (assq article gnus-newsgroup-bookmarks))))))
5682
5683 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5684   "Select the current article.
5685 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5686 non-nil, the article will be re-fetched even if it already present in
5687 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5688 be displayed."
5689   ;; Make sure we are in the summary buffer to work around bbdb bug.
5690   (unless (eq major-mode 'gnus-summary-mode)
5691     (set-buffer gnus-summary-buffer))
5692   (let ((article (or article (gnus-summary-article-number)))
5693         (all-headers (not (not all-headers))) ;Must be T or NIL.
5694         gnus-summary-display-article-function
5695         did)
5696     (and (not pseudo)
5697          (gnus-summary-article-pseudo-p article)
5698          (error "This is a pseudo-article"))
5699     (prog1
5700         (save-excursion
5701           (set-buffer gnus-summary-buffer)
5702           (if (or (and gnus-single-article-buffer
5703                        (or (null gnus-current-article)
5704                            (null gnus-article-current)
5705                            (null (get-buffer gnus-article-buffer))
5706                            (not (eq article (cdr gnus-article-current)))
5707                            (not (equal (car gnus-article-current)
5708                                        gnus-newsgroup-name))))
5709                   (and (not gnus-single-article-buffer)
5710                        (or (null gnus-current-article)
5711                            (not (eq gnus-current-article article))))
5712                   force)
5713               ;; The requested article is different from the current article.
5714               (prog1
5715                   (gnus-summary-display-article article all-headers)
5716                 (setq did article))
5717             (when (or all-headers gnus-show-all-headers)
5718               (gnus-article-show-all-headers))
5719             'old))
5720       (when did
5721         (gnus-article-set-window-start
5722          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5723
5724 (defun gnus-summary-set-current-mark (&optional current-mark)
5725   "Obsolete function."
5726   nil)
5727
5728 (defun gnus-summary-next-article (&optional unread subject backward push)
5729   "Select the next article.
5730 If UNREAD, only unread articles are selected.
5731 If SUBJECT, only articles with SUBJECT are selected.
5732 If BACKWARD, the previous article is selected instead of the next."
5733   (interactive "P")
5734   (cond
5735    ;; Is there such an article?
5736    ((and (gnus-summary-search-forward unread subject backward)
5737          (or (gnus-summary-display-article (gnus-summary-article-number))
5738              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5739     (gnus-summary-position-point))
5740    ;; If not, we try the first unread, if that is wanted.
5741    ((and subject
5742          gnus-auto-select-same
5743          (gnus-summary-first-unread-article))
5744     (gnus-summary-position-point)
5745     (gnus-message 6 "Wrapped"))
5746    ;; Try to get next/previous article not displayed in this group.
5747    ((and gnus-auto-extend-newsgroup
5748          (not unread) (not subject))
5749     (gnus-summary-goto-article
5750      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5751      nil (count-lines (point-min) (point))))
5752    ;; Go to next/previous group.
5753    (t
5754     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5755       (gnus-summary-jump-to-group gnus-newsgroup-name))
5756     (let ((cmd last-command-char)
5757           (point
5758            (save-excursion
5759              (set-buffer gnus-group-buffer)
5760              (point)))
5761           (group
5762            (if (eq gnus-keep-same-level 'best)
5763                (gnus-summary-best-group gnus-newsgroup-name)
5764              (gnus-summary-search-group backward gnus-keep-same-level))))
5765       ;; For some reason, the group window gets selected.  We change
5766       ;; it back.
5767       (select-window (get-buffer-window (current-buffer)))
5768       ;; Select next unread newsgroup automagically.
5769       (cond
5770        ((or (not gnus-auto-select-next)
5771             (not cmd))
5772         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5773        ((or (eq gnus-auto-select-next 'quietly)
5774             (and (eq gnus-auto-select-next 'slightly-quietly)
5775                  push)
5776             (and (eq gnus-auto-select-next 'almost-quietly)
5777                  (gnus-summary-last-article-p)))
5778         ;; Select quietly.
5779         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5780             (gnus-summary-exit)
5781           (gnus-message 7 "No more%s articles (%s)..."
5782                         (if unread " unread" "")
5783                         (if group (concat "selecting " group)
5784                           "exiting"))
5785           (gnus-summary-next-group nil group backward)))
5786        (t
5787         (when (gnus-key-press-event-p last-input-event)
5788           (gnus-summary-walk-group-buffer
5789            gnus-newsgroup-name cmd unread backward point))))))))
5790
5791 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5792   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5793                       (?\C-p (gnus-group-prev-unread-group 1))))
5794         (cursor-in-echo-area t)
5795         keve key group ended)
5796     (save-excursion
5797       (set-buffer gnus-group-buffer)
5798       (goto-char start)
5799       (setq group
5800             (if (eq gnus-keep-same-level 'best)
5801                 (gnus-summary-best-group gnus-newsgroup-name)
5802               (gnus-summary-search-group backward gnus-keep-same-level))))
5803     (while (not ended)
5804       (gnus-message
5805        5 "No more%s articles%s" (if unread " unread" "")
5806        (if (and group
5807                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5808            (format " (Type %s for %s [%s])"
5809                    (single-key-description cmd) group
5810                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5811          (format " (Type %s to exit %s)"
5812                  (single-key-description cmd)
5813                  gnus-newsgroup-name)))
5814       ;; Confirm auto selection.
5815       (setq key (car (setq keve (gnus-read-event-char))))
5816       (setq ended t)
5817       (cond
5818        ((assq key keystrokes)
5819         (let ((obuf (current-buffer)))
5820           (switch-to-buffer gnus-group-buffer)
5821           (when group
5822             (gnus-group-jump-to-group group))
5823           (eval (cadr (assq key keystrokes)))
5824           (setq group (gnus-group-group-name))
5825           (switch-to-buffer obuf))
5826         (setq ended nil))
5827        ((equal key cmd)
5828         (if (or (not group)
5829                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5830             (gnus-summary-exit)
5831           (gnus-summary-next-group nil group backward)))
5832        (t
5833         (push (cdr keve) unread-command-events))))))
5834
5835 (defun gnus-summary-next-unread-article ()
5836   "Select unread article after current one."
5837   (interactive)
5838   (gnus-summary-next-article
5839    (or (not (eq gnus-summary-goto-unread 'never))
5840        (gnus-summary-last-article-p (gnus-summary-article-number)))
5841    (and gnus-auto-select-same
5842         (gnus-summary-article-subject))))
5843
5844 (defun gnus-summary-prev-article (&optional unread subject)
5845   "Select the article after the current one.
5846 If UNREAD is non-nil, only unread articles are selected."
5847   (interactive "P")
5848   (gnus-summary-next-article unread subject t))
5849
5850 (defun gnus-summary-prev-unread-article ()
5851   "Select unread article before current one."
5852   (interactive)
5853   (gnus-summary-prev-article
5854    (or (not (eq gnus-summary-goto-unread 'never))
5855        (gnus-summary-first-article-p (gnus-summary-article-number)))
5856    (and gnus-auto-select-same
5857         (gnus-summary-article-subject))))
5858
5859 (defun gnus-summary-next-page (&optional lines circular)
5860   "Show next page of the selected article.
5861 If at the end of the current article, select the next article.
5862 LINES says how many lines should be scrolled up.
5863
5864 If CIRCULAR is non-nil, go to the start of the article instead of
5865 selecting the next article when reaching the end of the current
5866 article."
5867   (interactive "P")
5868   (setq gnus-summary-buffer (current-buffer))
5869   (gnus-set-global-variables)
5870   (let ((article (gnus-summary-article-number))
5871         (article-window (get-buffer-window gnus-article-buffer t))
5872         endp)
5873     ;; If the buffer is empty, we have no article.
5874     (unless article
5875       (error "No article to select"))
5876     (gnus-configure-windows 'article)
5877     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5878         (if (and (eq gnus-summary-goto-unread 'never)
5879                  (not (gnus-summary-last-article-p article)))
5880             (gnus-summary-next-article)
5881           (gnus-summary-next-unread-article))
5882       (if (or (null gnus-current-article)
5883               (null gnus-article-current)
5884               (/= article (cdr gnus-article-current))
5885               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5886           ;; Selected subject is different from current article's.
5887           (gnus-summary-display-article article)
5888         (when article-window
5889           (gnus-eval-in-buffer-window gnus-article-buffer
5890             (setq endp (gnus-article-next-page lines)))
5891           (when endp
5892             (cond (circular
5893                    (gnus-summary-beginning-of-article))
5894                   (lines
5895                    (gnus-message 3 "End of message"))
5896                   ((null lines)
5897                    (if (and (eq gnus-summary-goto-unread 'never)
5898                             (not (gnus-summary-last-article-p article)))
5899                        (gnus-summary-next-article)
5900                      (gnus-summary-next-unread-article))))))))
5901     (gnus-summary-recenter)
5902     (gnus-summary-position-point)))
5903
5904 (defun gnus-summary-prev-page (&optional lines move)
5905   "Show previous page of selected article.
5906 Argument LINES specifies lines to be scrolled down.
5907 If MOVE, move to the previous unread article if point is at
5908 the beginning of the buffer."
5909   (interactive "P")
5910   (let ((article (gnus-summary-article-number))
5911         (article-window (get-buffer-window gnus-article-buffer t))
5912         endp)
5913     (gnus-configure-windows 'article)
5914     (if (or (null gnus-current-article)
5915             (null gnus-article-current)
5916             (/= article (cdr gnus-article-current))
5917             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5918         ;; Selected subject is different from current article's.
5919         (gnus-summary-display-article article)
5920       (gnus-summary-recenter)
5921       (when article-window
5922         (gnus-eval-in-buffer-window gnus-article-buffer
5923           (setq endp (gnus-article-prev-page lines)))
5924         (when (and move endp)
5925           (cond (lines
5926                  (gnus-message 3 "Beginning of message"))
5927                 ((null lines)
5928                  (if (and (eq gnus-summary-goto-unread 'never)
5929                           (not (gnus-summary-first-article-p article)))
5930                      (gnus-summary-prev-article)
5931                    (gnus-summary-prev-unread-article))))))))
5932   (gnus-summary-position-point))
5933
5934 (defun gnus-summary-prev-page-or-article (&optional lines)
5935   "Show previous page of selected article.
5936 Argument LINES specifies lines to be scrolled down.
5937 If at the beginning of the article, go to the next article."
5938   (interactive "P")
5939   (gnus-summary-prev-page lines t))
5940
5941 (defun gnus-summary-scroll-up (lines)
5942   "Scroll up (or down) one line current article.
5943 Argument LINES specifies lines to be scrolled up (or down if negative)."
5944   (interactive "p")
5945   (gnus-configure-windows 'article)
5946   (gnus-summary-show-thread)
5947   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5948     (gnus-eval-in-buffer-window gnus-article-buffer
5949       (cond ((> lines 0)
5950              (when (gnus-article-next-page lines)
5951                (gnus-message 3 "End of message")))
5952             ((< lines 0)
5953              (gnus-article-prev-page (- lines))))))
5954   (gnus-summary-recenter)
5955   (gnus-summary-position-point))
5956
5957 (defun gnus-summary-scroll-down (lines)
5958   "Scroll down (or up) one line current article.
5959 Argument LINES specifies lines to be scrolled down (or up if negative)."
5960   (interactive "p")
5961   (gnus-summary-scroll-up (- lines)))
5962
5963 (defun gnus-summary-next-same-subject ()
5964   "Select next article which has the same subject as current one."
5965   (interactive)
5966   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5967
5968 (defun gnus-summary-prev-same-subject ()
5969   "Select previous article which has the same subject as current one."
5970   (interactive)
5971   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5972
5973 (defun gnus-summary-next-unread-same-subject ()
5974   "Select next unread article which has the same subject as current one."
5975   (interactive)
5976   (gnus-summary-next-article t (gnus-summary-article-subject)))
5977
5978 (defun gnus-summary-prev-unread-same-subject ()
5979   "Select previous unread article which has the same subject as current one."
5980   (interactive)
5981   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5982
5983 (defun gnus-summary-first-unread-article ()
5984   "Select the first unread article.
5985 Return nil if there are no unread articles."
5986   (interactive)
5987   (prog1
5988       (when (gnus-summary-first-subject t)
5989         (gnus-summary-show-thread)
5990         (gnus-summary-first-subject t)
5991         (gnus-summary-display-article (gnus-summary-article-number)))
5992     (gnus-summary-position-point)))
5993
5994 (defun gnus-summary-first-unread-subject ()
5995   "Place the point on the subject line of the first unread article.
5996 Return nil if there are no unread articles."
5997   (interactive)
5998   (prog1
5999       (when (gnus-summary-first-subject t)
6000         (gnus-summary-show-thread)
6001         (gnus-summary-first-subject t))
6002     (gnus-summary-position-point)))
6003
6004 (defun gnus-summary-first-article ()
6005   "Select the first article.
6006 Return nil if there are no articles."
6007   (interactive)
6008   (prog1
6009       (when (gnus-summary-first-subject)
6010         (gnus-summary-show-thread)
6011         (gnus-summary-first-subject)
6012         (gnus-summary-display-article (gnus-summary-article-number)))
6013     (gnus-summary-position-point)))
6014
6015 (defun gnus-summary-best-unread-article ()
6016   "Select the unread article with the highest score."
6017   (interactive)
6018   (let ((best -1000000)
6019         (data gnus-newsgroup-data)
6020         article score)
6021     (while data
6022       (and (gnus-data-unread-p (car data))
6023            (> (setq score
6024                     (gnus-summary-article-score (gnus-data-number (car data))))
6025               best)
6026            (setq best score
6027                  article (gnus-data-number (car data))))
6028       (setq data (cdr data)))
6029     (prog1
6030         (if article
6031             (gnus-summary-goto-article article)
6032           (error "No unread articles"))
6033       (gnus-summary-position-point))))
6034
6035 (defun gnus-summary-last-subject ()
6036   "Go to the last displayed subject line in the group."
6037   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6038     (when article
6039       (gnus-summary-goto-subject article))))
6040
6041 (defun gnus-summary-goto-article (article &optional all-headers force)
6042   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6043 If ALL-HEADERS is non-nil, no header lines are hidden.
6044 If FORCE, go to the article even if it isn't displayed.  If FORCE
6045 is a number, it is the line the article is to be displayed on."
6046   (interactive
6047    (list
6048     (completing-read
6049      "Article number or Message-ID: "
6050      (mapcar (lambda (number) (list (int-to-string number)))
6051              gnus-newsgroup-limit))
6052     current-prefix-arg
6053     t))
6054   (prog1
6055       (if (and (stringp article)
6056                (string-match "@" article))
6057           (gnus-summary-refer-article article)
6058         (when (stringp article)
6059           (setq article (string-to-number article)))
6060         (if (gnus-summary-goto-subject article force)
6061             (gnus-summary-display-article article all-headers)
6062           (gnus-message 4 "Couldn't go to article %s" article) nil))
6063     (gnus-summary-position-point)))
6064
6065 (defun gnus-summary-goto-last-article ()
6066   "Go to the previously read article."
6067   (interactive)
6068   (prog1
6069       (when gnus-last-article
6070         (gnus-summary-goto-article gnus-last-article nil t))
6071     (gnus-summary-position-point)))
6072
6073 (defun gnus-summary-pop-article (number)
6074   "Pop one article off the history and go to the previous.
6075 NUMBER articles will be popped off."
6076   (interactive "p")
6077   (let (to)
6078     (setq gnus-newsgroup-history
6079           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6080     (if to
6081         (gnus-summary-goto-article (car to) nil t)
6082       (error "Article history empty")))
6083   (gnus-summary-position-point))
6084
6085 ;; Summary commands and functions for limiting the summary buffer.
6086
6087 (defun gnus-summary-limit-to-articles (n)
6088   "Limit the summary buffer to the next N articles.
6089 If not given a prefix, use the process marked articles instead."
6090   (interactive "P")
6091   (prog1
6092       (let ((articles (gnus-summary-work-articles n)))
6093         (setq gnus-newsgroup-processable nil)
6094         (gnus-summary-limit articles))
6095     (gnus-summary-position-point)))
6096
6097 (defun gnus-summary-pop-limit (&optional total)
6098   "Restore the previous limit.
6099 If given a prefix, remove all limits."
6100   (interactive "P")
6101   (when total
6102     (setq gnus-newsgroup-limits
6103           (list (mapcar (lambda (h) (mail-header-number h))
6104                         gnus-newsgroup-headers))))
6105   (unless gnus-newsgroup-limits
6106     (error "No limit to pop"))
6107   (prog1
6108       (gnus-summary-limit nil 'pop)
6109     (gnus-summary-position-point)))
6110
6111 (defun gnus-summary-limit-to-subject (subject &optional header)
6112   "Limit the summary buffer to articles that have subjects that match a regexp."
6113   (interactive "sLimit to subject (regexp): ")
6114   (unless header
6115     (setq header "subject"))
6116   (when (not (equal "" subject))
6117     (prog1
6118         (let ((articles (gnus-summary-find-matching
6119                          (or header "subject") subject 'all)))
6120           (unless articles
6121             (error "Found no matches for \"%s\"" subject))
6122           (gnus-summary-limit articles))
6123       (gnus-summary-position-point))))
6124
6125 (defun gnus-summary-limit-to-author (from)
6126   "Limit the summary buffer to articles that have authors that match a regexp."
6127   (interactive "sLimit to author (regexp): ")
6128   (gnus-summary-limit-to-subject from "from"))
6129
6130 (defun gnus-summary-limit-to-age (age &optional younger-p)
6131   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6132 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6133 articles that are younger than AGE days."
6134   (interactive "nTime in days: \nP")
6135   (prog1
6136       (let ((data gnus-newsgroup-data)
6137             (cutoff (days-to-time age))
6138             articles d date is-younger)
6139         (while (setq d (pop data))
6140           (when (and (vectorp (gnus-data-header d))
6141                      (setq date (mail-header-date (gnus-data-header d))))
6142             (setq is-younger (time-less-p
6143                               (time-since (date-to-time date))
6144                               cutoff))
6145             (when (if younger-p
6146                       is-younger
6147                     (not is-younger))
6148               (push (gnus-data-number d) articles))))
6149         (gnus-summary-limit (nreverse articles)))
6150     (gnus-summary-position-point)))
6151
6152 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6153 (make-obsolete
6154  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6155
6156 (defun gnus-summary-limit-to-unread (&optional all)
6157   "Limit the summary buffer to articles that are not marked as read.
6158 If ALL is non-nil, limit strictly to unread articles."
6159   (interactive "P")
6160   (if all
6161       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6162     (gnus-summary-limit-to-marks
6163      ;; Concat all the marks that say that an article is read and have
6164      ;; those removed.
6165      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6166            gnus-killed-mark gnus-kill-file-mark
6167            gnus-low-score-mark gnus-expirable-mark
6168            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6169            gnus-duplicate-mark gnus-souped-mark)
6170      'reverse)))
6171
6172 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6173 (make-obsolete 'gnus-summary-delete-marked-with
6174                'gnus-summary-limit-exlude-marks)
6175
6176 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6177   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6178 If REVERSE, limit the summary buffer to articles that are marked
6179 with MARKS.  MARKS can either be a string of marks or a list of marks.
6180 Returns how many articles were removed."
6181   (interactive "sMarks: ")
6182   (gnus-summary-limit-to-marks marks t))
6183
6184 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6185   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6186 If REVERSE (the prefix), limit the summary buffer to articles that are
6187 not marked with MARKS.  MARKS can either be a string of marks or a
6188 list of marks.
6189 Returns how many articles were removed."
6190   (interactive "sMarks: \nP")
6191   (prog1
6192       (let ((data gnus-newsgroup-data)
6193             (marks (if (listp marks) marks
6194                      (append marks nil))) ; Transform to list.
6195             articles)
6196         (while data
6197           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6198                   (memq (gnus-data-mark (car data)) marks))
6199             (push (gnus-data-number (car data)) articles))
6200           (setq data (cdr data)))
6201         (gnus-summary-limit articles))
6202     (gnus-summary-position-point)))
6203
6204 (defun gnus-summary-limit-to-score (&optional score)
6205   "Limit to articles with score at or above SCORE."
6206   (interactive "P")
6207   (setq score (if score
6208                   (prefix-numeric-value score)
6209                 (or gnus-summary-default-score 0)))
6210   (let ((data gnus-newsgroup-data)
6211         articles)
6212     (while data
6213       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6214                 score)
6215         (push (gnus-data-number (car data)) articles))
6216       (setq data (cdr data)))
6217     (prog1
6218         (gnus-summary-limit articles)
6219       (gnus-summary-position-point))))
6220
6221 (defun gnus-summary-limit-include-thread (id)
6222   "Display all the hidden articles that in the current thread."
6223   (interactive (list (mail-header-id (gnus-summary-article-header))))
6224   (let ((articles (gnus-articles-in-thread
6225                    (gnus-id-to-thread (gnus-root-id id)))))
6226     (prog1
6227         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6228       (gnus-summary-position-point))))
6229
6230 (defun gnus-summary-limit-include-dormant ()
6231   "Display all the hidden articles that are marked as dormant.
6232 Note that this command only works on a subset of the articles currently
6233 fetched for this group."
6234   (interactive)
6235   (unless gnus-newsgroup-dormant
6236     (error "There are no dormant articles in this group"))
6237   (prog1
6238       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6239     (gnus-summary-position-point)))
6240
6241 (defun gnus-summary-limit-exclude-dormant ()
6242   "Hide all dormant articles."
6243   (interactive)
6244   (prog1
6245       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6246     (gnus-summary-position-point)))
6247
6248 (defun gnus-summary-limit-exclude-childless-dormant ()
6249   "Hide all dormant articles that have no children."
6250   (interactive)
6251   (let ((data (gnus-data-list t))
6252         articles d children)
6253     ;; Find all articles that are either not dormant or have
6254     ;; children.
6255     (while (setq d (pop data))
6256       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6257                 (and (setq children
6258                            (gnus-article-children (gnus-data-number d)))
6259                      (let (found)
6260                        (while children
6261                          (when (memq (car children) articles)
6262                            (setq children nil
6263                                  found t))
6264                          (pop children))
6265                        found)))
6266         (push (gnus-data-number d) articles)))
6267     ;; Do the limiting.
6268     (prog1
6269         (gnus-summary-limit articles)
6270       (gnus-summary-position-point))))
6271
6272 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6273   "Mark all unread excluded articles as read.
6274 If ALL, mark even excluded ticked and dormants as read."
6275   (interactive "P")
6276   (let ((articles (gnus-sorted-complement
6277                    (sort
6278                     (mapcar (lambda (h) (mail-header-number h))
6279                             gnus-newsgroup-headers)
6280                     '<)
6281                    (sort gnus-newsgroup-limit '<)))
6282         article)
6283     (setq gnus-newsgroup-unreads
6284           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6285     (if all
6286         (setq gnus-newsgroup-dormant nil
6287               gnus-newsgroup-marked nil
6288               gnus-newsgroup-reads
6289               (nconc
6290                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6291                gnus-newsgroup-reads))
6292       (while (setq article (pop articles))
6293         (unless (or (memq article gnus-newsgroup-dormant)
6294                     (memq article gnus-newsgroup-marked))
6295           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6296
6297 (defun gnus-summary-limit (articles &optional pop)
6298   (if pop
6299       ;; We pop the previous limit off the stack and use that.
6300       (setq articles (car gnus-newsgroup-limits)
6301             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6302     ;; We use the new limit, so we push the old limit on the stack.
6303     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6304   ;; Set the limit.
6305   (setq gnus-newsgroup-limit articles)
6306   (let ((total (length gnus-newsgroup-data))
6307         (data (gnus-data-find-list (gnus-summary-article-number)))
6308         (gnus-summary-mark-below nil)   ; Inhibit this.
6309         found)
6310     ;; This will do all the work of generating the new summary buffer
6311     ;; according to the new limit.
6312     (gnus-summary-prepare)
6313     ;; Hide any threads, possibly.
6314     (and gnus-show-threads
6315          gnus-thread-hide-subtree
6316          (gnus-summary-hide-all-threads))
6317     ;; Try to return to the article you were at, or one in the
6318     ;; neighborhood.
6319     (when data
6320       ;; We try to find some article after the current one.
6321       (while data
6322         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6323           (setq data nil
6324                 found t))
6325         (setq data (cdr data))))
6326     (unless found
6327       ;; If there is no data, that means that we were after the last
6328       ;; article.  The same goes when we can't find any articles
6329       ;; after the current one.
6330       (goto-char (point-max))
6331       (gnus-summary-find-prev))
6332     (gnus-set-mode-line 'summary)
6333     ;; We return how many articles were removed from the summary
6334     ;; buffer as a result of the new limit.
6335     (- total (length gnus-newsgroup-data))))
6336
6337 (defsubst gnus-invisible-cut-children (threads)
6338   (let ((num 0))
6339     (while threads
6340       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6341         (incf num))
6342       (pop threads))
6343     (< num 2)))
6344
6345 (defsubst gnus-cut-thread (thread)
6346   "Go forwards in the thread until we find an article that we want to display."
6347   (when (or (eq gnus-fetch-old-headers 'some)
6348             (eq gnus-fetch-old-headers 'invisible)
6349             (eq gnus-build-sparse-threads 'some)
6350             (eq gnus-build-sparse-threads 'more))
6351     ;; Deal with old-fetched headers and sparse threads.
6352     (while (and
6353             thread
6354             (or
6355              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6356              (gnus-summary-article-ancient-p
6357               (mail-header-number (car thread))))
6358             (if (or (<= (length (cdr thread)) 1)
6359                     (eq gnus-fetch-old-headers 'invisible))
6360                 (setq gnus-newsgroup-limit
6361                       (delq (mail-header-number (car thread))
6362                             gnus-newsgroup-limit)
6363                       thread (cadr thread))
6364               (when (gnus-invisible-cut-children (cdr thread))
6365                 (let ((th (cdr thread)))
6366                   (while th
6367                     (if (memq (mail-header-number (caar th))
6368                               gnus-newsgroup-limit)
6369                         (setq thread (car th)
6370                               th nil)
6371                       (setq th (cdr th))))))))))
6372   thread)
6373
6374 (defun gnus-cut-threads (threads)
6375   "Cut off all uninteresting articles from the beginning of threads."
6376   (when (or (eq gnus-fetch-old-headers 'some)
6377             (eq gnus-fetch-old-headers 'invisible)
6378             (eq gnus-build-sparse-threads 'some)
6379             (eq gnus-build-sparse-threads 'more))
6380     (let ((th threads))
6381       (while th
6382         (setcar th (gnus-cut-thread (car th)))
6383         (setq th (cdr th)))))
6384   ;; Remove nixed out threads.
6385   (delq nil threads))
6386
6387 (defun gnus-summary-initial-limit (&optional show-if-empty)
6388   "Figure out what the initial limit is supposed to be on group entry.
6389 This entails weeding out unwanted dormants, low-scored articles,
6390 fetch-old-headers verbiage, and so on."
6391   ;; Most groups have nothing to remove.
6392   (if (or gnus-inhibit-limiting
6393           (and (null gnus-newsgroup-dormant)
6394                (not (eq gnus-fetch-old-headers 'some))
6395                (not (eq gnus-fetch-old-headers 'invisible))
6396                (null gnus-summary-expunge-below)
6397                (not (eq gnus-build-sparse-threads 'some))
6398                (not (eq gnus-build-sparse-threads 'more))
6399                (null gnus-thread-expunge-below)
6400                (not gnus-use-nocem)))
6401       ()                                ; Do nothing.
6402     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6403     (setq gnus-newsgroup-limit nil)
6404     (mapatoms
6405      (lambda (node)
6406        (unless (car (symbol-value node))
6407          ;; These threads have no parents -- they are roots.
6408          (let ((nodes (cdr (symbol-value node)))
6409                thread)
6410            (while nodes
6411              (if (and gnus-thread-expunge-below
6412                       (< (gnus-thread-total-score (car nodes))
6413                          gnus-thread-expunge-below))
6414                  (gnus-expunge-thread (pop nodes))
6415                (setq thread (pop nodes))
6416                (gnus-summary-limit-children thread))))))
6417      gnus-newsgroup-dependencies)
6418     ;; If this limitation resulted in an empty group, we might
6419     ;; pop the previous limit and use it instead.
6420     (when (and (not gnus-newsgroup-limit)
6421                show-if-empty)
6422       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6423     gnus-newsgroup-limit))
6424
6425 (defun gnus-summary-limit-children (thread)
6426   "Return 1 if this subthread is visible and 0 if it is not."
6427   ;; First we get the number of visible children to this thread.  This
6428   ;; is done by recursing down the thread using this function, so this
6429   ;; will really go down to a leaf article first, before slowly
6430   ;; working its way up towards the root.
6431   (when thread
6432     (let ((children
6433            (if (cdr thread)
6434                (apply '+ (mapcar 'gnus-summary-limit-children
6435                                  (cdr thread)))
6436              0))
6437           (number (mail-header-number (car thread)))
6438           score)
6439       (if (and
6440            (not (memq number gnus-newsgroup-marked))
6441            (or
6442             ;; If this article is dormant and has absolutely no visible
6443             ;; children, then this article isn't visible.
6444             (and (memq number gnus-newsgroup-dormant)
6445                  (zerop children))
6446             ;; If this is "fetch-old-headered" and there is no
6447             ;; visible children, then we don't want this article.
6448             (and (eq gnus-fetch-old-headers 'some)
6449                  (gnus-summary-article-ancient-p number)
6450                  (zerop children))
6451             ;; If this is "fetch-old-headered" and `invisible', then
6452             ;; we don't want this article.
6453             (and (eq gnus-fetch-old-headers 'invisible)
6454                  (gnus-summary-article-ancient-p number))
6455             ;; If this is a sparsely inserted article with no children,
6456             ;; we don't want it.
6457             (and (eq gnus-build-sparse-threads 'some)
6458                  (gnus-summary-article-sparse-p number)
6459                  (zerop children))
6460             ;; If we use expunging, and this article is really
6461             ;; low-scored, then we don't want this article.
6462             (when (and gnus-summary-expunge-below
6463                        (< (setq score
6464                                 (or (cdr (assq number gnus-newsgroup-scored))
6465                                     gnus-summary-default-score))
6466                           gnus-summary-expunge-below))
6467               ;; We increase the expunge-tally here, but that has
6468               ;; nothing to do with the limits, really.
6469               (incf gnus-newsgroup-expunged-tally)
6470               ;; We also mark as read here, if that's wanted.
6471               (when (and gnus-summary-mark-below
6472                          (< score gnus-summary-mark-below))
6473                 (setq gnus-newsgroup-unreads
6474                       (delq number gnus-newsgroup-unreads))
6475                 (if gnus-newsgroup-auto-expire
6476                     (push number gnus-newsgroup-expirable)
6477                   (push (cons number gnus-low-score-mark)
6478                         gnus-newsgroup-reads)))
6479               t)
6480             ;; Check NoCeM things.
6481             (if (and gnus-use-nocem
6482                      (gnus-nocem-unwanted-article-p
6483                       (mail-header-id (car thread))))
6484                 (progn
6485                   (setq gnus-newsgroup-unreads
6486                         (delq number gnus-newsgroup-unreads))
6487                   t))))
6488           ;; Nope, invisible article.
6489           0
6490         ;; Ok, this article is to be visible, so we add it to the limit
6491         ;; and return 1.
6492         (push number gnus-newsgroup-limit)
6493         1))))
6494
6495 (defun gnus-expunge-thread (thread)
6496   "Mark all articles in THREAD as read."
6497   (let* ((number (mail-header-number (car thread))))
6498     (incf gnus-newsgroup-expunged-tally)
6499     ;; We also mark as read here, if that's wanted.
6500     (setq gnus-newsgroup-unreads
6501           (delq number gnus-newsgroup-unreads))
6502     (if gnus-newsgroup-auto-expire
6503         (push number gnus-newsgroup-expirable)
6504       (push (cons number gnus-low-score-mark)
6505             gnus-newsgroup-reads)))
6506   ;; Go recursively through all subthreads.
6507   (mapcar 'gnus-expunge-thread (cdr thread)))
6508
6509 ;; Summary article oriented commands
6510
6511 (defun gnus-summary-refer-parent-article (n)
6512   "Refer parent article N times.
6513 If N is negative, go to ancestor -N instead.
6514 The difference between N and the number of articles fetched is returned."
6515   (interactive "p")
6516   (let ((skip 1)
6517         error header ref)
6518     (when (not (natnump n))
6519       (setq skip (abs n)
6520             n 1))
6521     (while (and (> n 0)
6522                 (not error))
6523       (setq header (gnus-summary-article-header))
6524       (if (and (eq (mail-header-number header)
6525                    (cdr gnus-article-current))
6526                (equal gnus-newsgroup-name
6527                       (car gnus-article-current)))
6528           ;; If we try to find the parent of the currently
6529           ;; displayed article, then we take a look at the actual
6530           ;; References header, since this is slightly more
6531           ;; reliable than the References field we got from the
6532           ;; server.
6533           (save-excursion
6534             (set-buffer gnus-original-article-buffer)
6535             (nnheader-narrow-to-headers)
6536             (unless (setq ref (message-fetch-field "references"))
6537               (setq ref (message-fetch-field "in-reply-to")))
6538             (widen))
6539         (setq ref
6540               ;; It's not the current article, so we take a bet on
6541               ;; the value we got from the server.
6542               (mail-header-references header)))
6543       (if (and ref
6544                (not (equal ref "")))
6545           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6546             (gnus-message 1 "Couldn't find parent"))
6547         (gnus-message 1 "No references in article %d"
6548                       (gnus-summary-article-number))
6549         (setq error t))
6550       (decf n))
6551     (gnus-summary-position-point)
6552     n))
6553
6554 (defun gnus-summary-refer-references ()
6555   "Fetch all articles mentioned in the References header.
6556 Return the number of articles fetched."
6557   (interactive)
6558   (let ((ref (mail-header-references (gnus-summary-article-header)))
6559         (current (gnus-summary-article-number))
6560         (n 0))
6561     (if (or (not ref)
6562             (equal ref ""))
6563         (error "No References in the current article")
6564       ;; For each Message-ID in the References header...
6565       (while (string-match "<[^>]*>" ref)
6566         (incf n)
6567         ;; ... fetch that article.
6568         (gnus-summary-refer-article
6569          (prog1 (match-string 0 ref)
6570            (setq ref (substring ref (match-end 0))))))
6571       (gnus-summary-goto-subject current)
6572       (gnus-summary-position-point)
6573       n)))
6574
6575 (defun gnus-summary-refer-thread (&optional limit)
6576   "Fetch all articles in the current thread.
6577 If LIMIT (the numerical prefix), fetch that many old headers instead
6578 of what's specified by the `gnus-refer-thread-limit' variable."
6579   (interactive "P")
6580   (let ((id (mail-header-id (gnus-summary-article-header)))
6581         (limit (if limit (prefix-numeric-value limit)
6582                  gnus-refer-thread-limit)))
6583     ;; We want to fetch LIMIT *old* headers, but we also have to
6584     ;; re-fetch all the headers in the current buffer, because many of
6585     ;; them may be undisplayed.  So we adjust LIMIT.
6586     (when (numberp limit)
6587       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6588     (unless (eq gnus-fetch-old-headers 'invisible)
6589       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6590       ;; Retrieve the headers and read them in.
6591       (if (eq (gnus-retrieve-headers
6592                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6593               'nov)
6594           (gnus-build-all-threads)
6595         (error "Can't fetch thread from backends that don't support NOV"))
6596       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6597     (gnus-summary-limit-include-thread id)))
6598
6599 (defun gnus-summary-refer-article (message-id &optional arg)
6600   "Fetch an article specified by MESSAGE-ID.
6601 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6602 or `gnus-select-method', no matter what backend the article comes from."
6603   (interactive "sMessage-ID: \nP")
6604   (when (and (stringp message-id)
6605              (not (zerop (length message-id))))
6606     ;; Construct the correct Message-ID if necessary.
6607     ;; Suggested by tale@pawl.rpi.edu.
6608     (unless (string-match "^<" message-id)
6609       (setq message-id (concat "<" message-id)))
6610     (unless (string-match ">$" message-id)
6611       (setq message-id (concat message-id ">")))
6612     (let* ((header (gnus-id-to-header message-id))
6613            (sparse (and header
6614                         (gnus-summary-article-sparse-p
6615                          (mail-header-number header))
6616                         (memq (mail-header-number header)
6617                               gnus-newsgroup-limit))))
6618       (cond
6619        ;; If the article is present in the buffer we just go to it.
6620        ((and header
6621              (or (not (gnus-summary-article-sparse-p
6622                        (mail-header-number header)))
6623                  sparse))
6624         (prog1
6625             (gnus-summary-goto-article
6626              (mail-header-number header) nil t)
6627           (when sparse
6628             (gnus-summary-update-article (mail-header-number header)))))
6629        (t
6630         ;; We fetch the article
6631         (let ((gnus-override-method
6632                (cond ((gnus-news-group-p gnus-newsgroup-name)
6633                       gnus-refer-article-method)
6634                      (arg
6635                       (or gnus-refer-article-method gnus-select-method))
6636                      (t nil)))
6637               number)
6638           ;; Start the special refer-article method, if necessary.
6639           (when (and gnus-refer-article-method
6640                      (gnus-news-group-p gnus-newsgroup-name))
6641             (gnus-check-server gnus-refer-article-method))
6642           ;; Fetch the header, and display the article.
6643           (if (setq number (gnus-summary-insert-subject message-id))
6644               (gnus-summary-select-article nil nil nil number)
6645             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6646
6647 (defun gnus-summary-edit-parameters ()
6648   "Edit the group parameters of the current group."
6649   (interactive)
6650   (gnus-group-edit-group gnus-newsgroup-name 'params))
6651
6652 (defun gnus-summary-customize-parameters ()
6653   "Customize the group parameters of the current group."
6654   (interactive)
6655   (gnus-group-customize gnus-newsgroup-name))
6656
6657 (defun gnus-summary-enter-digest-group (&optional force)
6658   "Enter an nndoc group based on the current article.
6659 If FORCE, force a digest interpretation.  If not, try
6660 to guess what the document format is."
6661   (interactive "P")
6662   (let ((conf gnus-current-window-configuration))
6663     (save-excursion
6664       (gnus-summary-select-article))
6665     (setq gnus-current-window-configuration conf)
6666     (let* ((name (format "%s-%d"
6667                          (gnus-group-prefixed-name
6668                           gnus-newsgroup-name (list 'nndoc ""))
6669                          (save-excursion
6670                            (set-buffer gnus-summary-buffer)
6671                            gnus-current-article)))
6672            (ogroup gnus-newsgroup-name)
6673            (params (append (gnus-info-params (gnus-get-info ogroup))
6674                            (list (cons 'to-group ogroup))
6675                            (list (cons 'save-article-group ogroup))))
6676            (case-fold-search t)
6677            (buf (current-buffer))
6678            dig)
6679       (save-excursion
6680         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6681         (insert-buffer-substring gnus-original-article-buffer)
6682         ;; Remove lines that may lead nndoc to misinterpret the
6683         ;; document type.
6684         (narrow-to-region
6685          (goto-char (point-min))
6686          (or (search-forward "\n\n" nil t) (point)))
6687         (goto-char (point-min))
6688         (delete-matching-lines "^\\(Path\\):\\|^From ")
6689         (widen))
6690       (unwind-protect
6691           (if (gnus-group-read-ephemeral-group
6692                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6693                             (nndoc-article-type
6694                              ,(if force 'digest 'guess))) t)
6695               ;; Make all postings to this group go to the parent group.
6696               (nconc (gnus-info-params (gnus-get-info name))
6697                      params)
6698             ;; Couldn't select this doc group.
6699             (switch-to-buffer buf)
6700             (gnus-set-global-variables)
6701             (gnus-configure-windows 'summary)
6702             (gnus-message 3 "Article couldn't be entered?"))
6703         (kill-buffer dig)))))
6704
6705 (defun gnus-summary-read-document (n)
6706   "Open a new group based on the current article(s).
6707 This will allow you to read digests and other similar
6708 documents as newsgroups.
6709 Obeys the standard process/prefix convention."
6710   (interactive "P")
6711   (let* ((articles (gnus-summary-work-articles n))
6712          (ogroup gnus-newsgroup-name)
6713          (params (append (gnus-info-params (gnus-get-info ogroup))
6714                          (list (cons 'to-group ogroup))))
6715          article group egroup groups vgroup)
6716     (while (setq article (pop articles))
6717       (setq group (format "%s-%d" gnus-newsgroup-name article))
6718       (gnus-summary-remove-process-mark article)
6719       (when (gnus-summary-display-article article)
6720         (save-excursion
6721           (with-temp-buffer
6722             (insert-buffer-substring gnus-original-article-buffer)
6723             ;; Remove some headers that may lead nndoc to make
6724             ;; the wrong guess.
6725             (message-narrow-to-head)
6726             (goto-char (point-min))
6727             (delete-matching-lines "^\\(Path\\):\\|^From ")
6728             (widen)
6729             (if (setq egroup
6730                       (gnus-group-read-ephemeral-group
6731                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6732                                      (nndoc-article-type guess))
6733                        t nil t))
6734                 (progn
6735                   ;; Make all postings to this group go to the parent group.
6736                   (nconc (gnus-info-params (gnus-get-info egroup))
6737                          params)
6738                   (push egroup groups))
6739               ;; Couldn't select this doc group.
6740               (gnus-error 3 "Article couldn't be entered"))))))
6741     ;; Now we have selected all the documents.
6742     (cond
6743      ((not groups)
6744       (error "None of the articles could be interpreted as documents"))
6745      ((gnus-group-read-ephemeral-group
6746        (setq vgroup (format
6747                      "nnvirtual:%s-%s" gnus-newsgroup-name
6748                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6749        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6750        t
6751        (cons (current-buffer) 'summary)))
6752      (t
6753       (error "Couldn't select virtual nndoc group")))))
6754
6755 (defun gnus-summary-isearch-article (&optional regexp-p)
6756   "Do incremental search forward on the current article.
6757 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6758   (interactive "P")
6759   (gnus-summary-select-article)
6760   (gnus-configure-windows 'article)
6761   (gnus-eval-in-buffer-window gnus-article-buffer
6762     (save-restriction
6763       (widen)
6764       (isearch-forward regexp-p))))
6765
6766 (defun gnus-summary-search-article-forward (regexp &optional backward)
6767   "Search for an article containing REGEXP forward.
6768 If BACKWARD, search backward instead."
6769   (interactive
6770    (list (read-string
6771           (format "Search article %s (regexp%s): "
6772                   (if current-prefix-arg "backward" "forward")
6773                   (if gnus-last-search-regexp
6774                       (concat ", default " gnus-last-search-regexp)
6775                     "")))
6776          current-prefix-arg))
6777   (if (string-equal regexp "")
6778       (setq regexp (or gnus-last-search-regexp ""))
6779     (setq gnus-last-search-regexp regexp))
6780   (if (gnus-summary-search-article regexp backward)
6781       (gnus-summary-show-thread)
6782     (error "Search failed: \"%s\"" regexp)))
6783
6784 (defun gnus-summary-search-article-backward (regexp)
6785   "Search for an article containing REGEXP backward."
6786   (interactive
6787    (list (read-string
6788           (format "Search article backward (regexp%s): "
6789                   (if gnus-last-search-regexp
6790                       (concat ", default " gnus-last-search-regexp)
6791                     "")))))
6792   (gnus-summary-search-article-forward regexp 'backward))
6793
6794 (defun gnus-summary-search-article (regexp &optional backward)
6795   "Search for an article containing REGEXP.
6796 Optional argument BACKWARD means do search for backward.
6797 `gnus-select-article-hook' is not called during the search."
6798   ;; We have to require this here to make sure that the following
6799   ;; dynamic binding isn't shadowed by autoloading.
6800   (require 'gnus-async)
6801   (require 'gnus-art)
6802   (let ((gnus-select-article-hook nil)  ;Disable hook.
6803         (gnus-article-display-hook nil)
6804         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6805         (gnus-use-article-prefetch nil)
6806         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6807         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6808         (sum (current-buffer))
6809         (gnus-display-mime-function nil)
6810         (found nil)
6811         point)
6812     (gnus-save-hidden-threads
6813       (gnus-summary-select-article)
6814       (set-buffer gnus-article-buffer)
6815       (when backward
6816         (forward-line -1))
6817       (while (not found)
6818         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6819         (if (if backward
6820                 (re-search-backward regexp nil t)
6821               (re-search-forward regexp nil t))
6822             ;; We found the regexp.
6823             (progn
6824               (setq found 'found)
6825               (beginning-of-line)
6826               (set-window-start
6827                (get-buffer-window (current-buffer))
6828                (point))
6829               (forward-line 1)
6830               (set-buffer sum)
6831               (setq point (point)))
6832           ;; We didn't find it, so we go to the next article.
6833           (set-buffer sum)
6834           (setq found 'not)
6835           (while (eq found 'not)
6836             (if (not (if backward (gnus-summary-find-prev)
6837                        (gnus-summary-find-next)))
6838                 ;; No more articles.
6839                 (setq found t)
6840               ;; Select the next article and adjust point.
6841               (unless (gnus-summary-article-sparse-p
6842                        (gnus-summary-article-number))
6843                 (setq found nil)
6844                 (gnus-summary-select-article)
6845                 (set-buffer gnus-article-buffer)
6846                 (widen)
6847                 (goto-char (if backward (point-max) (point-min))))))))
6848       (gnus-message 7 ""))
6849     ;; Return whether we found the regexp.
6850     (when (eq found 'found)
6851       (goto-char point)
6852       (gnus-summary-show-thread)
6853       (gnus-summary-goto-subject gnus-current-article)
6854       (gnus-summary-position-point)
6855       t)))
6856
6857 (defun gnus-summary-find-matching (header regexp &optional backward unread
6858                                           not-case-fold)
6859   "Return a list of all articles that match REGEXP on HEADER.
6860 The search stars on the current article and goes forwards unless
6861 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6862 If UNREAD is non-nil, only unread articles will
6863 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6864 in the comparisons."
6865   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6866                 (gnus-data-find-list
6867                  (gnus-summary-article-number) (gnus-data-list backward))))
6868         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6869         (case-fold-search (not not-case-fold))
6870         articles d)
6871     (unless (fboundp (intern (concat "mail-header-" header)))
6872       (error "%s is not a valid header" header))
6873     (while data
6874       (setq d (car data))
6875       (and (or (not unread)             ; We want all articles...
6876                (gnus-data-unread-p d))  ; Or just unreads.
6877            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6878            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6879            (push (gnus-data-number d) articles)) ; Success!
6880       (setq data (cdr data)))
6881     (nreverse articles)))
6882
6883 (defun gnus-summary-execute-command (header regexp command &optional backward)
6884   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6885 If HEADER is an empty string (or nil), the match is done on the entire
6886 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6887   (interactive
6888    (list (let ((completion-ignore-case t))
6889            (completing-read
6890             "Header name: "
6891             (mapcar (lambda (string) (list string))
6892                     '("Number" "Subject" "From" "Lines" "Date"
6893                       "Message-ID" "Xref" "References" "Body"))
6894             nil 'require-match))
6895          (read-string "Regexp: ")
6896          (read-key-sequence "Command: ")
6897          current-prefix-arg))
6898   (when (equal header "Body")
6899     (setq header ""))
6900   ;; Hidden thread subtrees must be searched as well.
6901   (gnus-summary-show-all-threads)
6902   ;; We don't want to change current point nor window configuration.
6903   (save-excursion
6904     (save-window-excursion
6905       (gnus-message 6 "Executing %s..." (key-description command))
6906       ;; We'd like to execute COMMAND interactively so as to give arguments.
6907       (gnus-execute header regexp
6908                     `(call-interactively ',(key-binding command))
6909                     backward)
6910       (gnus-message 6 "Executing %s...done" (key-description command)))))
6911
6912 (defun gnus-summary-beginning-of-article ()
6913   "Scroll the article back to the beginning."
6914   (interactive)
6915   (gnus-summary-select-article)
6916   (gnus-configure-windows 'article)
6917   (gnus-eval-in-buffer-window gnus-article-buffer
6918     (widen)
6919     (goto-char (point-min))
6920     (when gnus-page-broken
6921       (gnus-narrow-to-page))))
6922
6923 (defun gnus-summary-end-of-article ()
6924   "Scroll to the end of the article."
6925   (interactive)
6926   (gnus-summary-select-article)
6927   (gnus-configure-windows 'article)
6928   (gnus-eval-in-buffer-window gnus-article-buffer
6929     (widen)
6930     (goto-char (point-max))
6931     (recenter -3)
6932     (when gnus-page-broken
6933       (gnus-narrow-to-page))))
6934
6935 (defun gnus-summary-print-article (&optional filename n)
6936   "Generate and print a PostScript image of the N next (mail) articles.
6937
6938 If N is negative, print the N previous articles.  If N is nil and articles
6939 have been marked with the process mark, print these instead.
6940
6941 If the optional second argument FILENAME is nil, send the image to the
6942 printer.  If FILENAME is a string, save the PostScript image in a file with
6943 that name.  If FILENAME is a number, prompt the user for the name of the file
6944 to save in."
6945   (interactive (list (ps-print-preprint current-prefix-arg)
6946                      current-prefix-arg))
6947   (dolist (article (gnus-summary-work-articles n))
6948     (gnus-summary-select-article nil nil 'pseudo article)
6949     (gnus-eval-in-buffer-window gnus-article-buffer
6950       (let ((buffer (generate-new-buffer " *print*")))
6951         (unwind-protect
6952             (progn
6953               (copy-to-buffer buffer (point-min) (point-max))
6954               (set-buffer buffer)
6955               (gnus-article-delete-invisible-text)
6956               (let ((ps-left-header
6957                      (list
6958                       (concat "("
6959                               (mail-header-subject gnus-current-headers) ")")
6960                       (concat "("
6961                               (mail-header-from gnus-current-headers) ")")))
6962                     (ps-right-header
6963                      (list
6964                       "/pagenumberstring load"
6965                       (concat "("
6966                               (mail-header-date gnus-current-headers) ")"))))
6967                 (gnus-run-hooks 'gnus-ps-print-hook)
6968                 (save-excursion
6969                   (ps-print-buffer-with-faces filename))))
6970           (kill-buffer buffer))))))
6971
6972 (defun gnus-summary-show-article (&optional arg)
6973   "Force re-fetching of the current article.
6974 If ARG (the prefix) is non-nil, show the raw article without any
6975 article massaging functions being run."
6976   (interactive "P")
6977   (if (not arg)
6978       ;; Select the article the normal way.
6979       (gnus-summary-select-article nil 'force)
6980     ;; We have to require this here to make sure that the following
6981     ;; dynamic binding isn't shadowed by autoloading.
6982     (require 'gnus-async)
6983     (require 'gnus-art)
6984     ;; Bind the article treatment functions to nil.
6985     (let ((gnus-have-all-headers t)
6986           gnus-article-display-hook
6987           gnus-article-prepare-hook
6988           gnus-article-decode-hook
6989           gnus-display-mime-function
6990           gnus-break-pages
6991           gnus-visual)
6992       (gnus-summary-select-article nil 'force)))
6993   (gnus-summary-goto-subject gnus-current-article)
6994   (gnus-summary-position-point))
6995
6996 (defun gnus-summary-verbose-headers (&optional arg)
6997   "Toggle permanent full header display.
6998 If ARG is a positive number, turn header display on.
6999 If ARG is a negative number, turn header display off."
7000   (interactive "P")
7001   (setq gnus-show-all-headers
7002         (cond ((or (not (numberp arg))
7003                    (zerop arg))
7004                (not gnus-show-all-headers))
7005               ((natnump arg)
7006                t)))
7007   (gnus-summary-show-article))
7008
7009 (defun gnus-summary-toggle-header (&optional arg)
7010   "Show the headers if they are hidden, or hide them if they are shown.
7011 If ARG is a positive number, show the entire header.
7012 If ARG is a negative number, hide the unwanted header lines."
7013   (interactive "P")
7014   (save-excursion
7015     (set-buffer gnus-article-buffer)
7016     (let* ((buffer-read-only nil)
7017            (inhibit-point-motion-hooks t)
7018            (hidden (text-property-any
7019                     (goto-char (point-min)) (search-forward "\n\n")
7020                     'invisible t))
7021            e)
7022       (goto-char (point-min))
7023       (when (search-forward "\n\n" nil t)
7024         (delete-region (point-min) (1- (point))))
7025       (goto-char (point-min))
7026       (save-excursion
7027         (set-buffer gnus-original-article-buffer)
7028         (goto-char (point-min))
7029         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7030       (insert-buffer-substring gnus-original-article-buffer 1 e)
7031       (let ((article-inhibit-hiding t))
7032         (gnus-run-hooks 'gnus-article-display-hook))
7033       (when (or (not hidden) (and (numberp arg) (< arg 0)))
7034         (gnus-article-hide-headers)))))
7035
7036 (defun gnus-summary-show-all-headers ()
7037   "Make all header lines visible."
7038   (interactive)
7039   (gnus-article-show-all-headers))
7040
7041 (defun gnus-summary-caesar-message (&optional arg)
7042   "Caesar rotate the current article by 13.
7043 The numerical prefix specifies how many places to rotate each letter
7044 forward."
7045   (interactive "P")
7046   (gnus-summary-select-article)
7047   (let ((mail-header-separator ""))
7048     (gnus-eval-in-buffer-window gnus-article-buffer
7049       (save-restriction
7050         (widen)
7051         (let ((start (window-start))
7052               buffer-read-only)
7053           (message-caesar-buffer-body arg)
7054           (set-window-start (get-buffer-window (current-buffer)) start))))))
7055
7056 (defun gnus-summary-stop-page-breaking ()
7057   "Stop page breaking in the current article."
7058   (interactive)
7059   (gnus-summary-select-article)
7060   (gnus-eval-in-buffer-window gnus-article-buffer
7061     (widen)
7062     (when (gnus-visual-p 'page-marker)
7063       (let ((buffer-read-only nil))
7064         (gnus-remove-text-with-property 'gnus-prev)
7065         (gnus-remove-text-with-property 'gnus-next))
7066       (setq gnus-page-broken nil))))
7067
7068 (defun gnus-summary-move-article (&optional n to-newsgroup
7069                                             select-method action)
7070   "Move the current article to a different newsgroup.
7071 If N is a positive number, move the N next articles.
7072 If N is a negative number, move the N previous articles.
7073 If N is nil and any articles have been marked with the process mark,
7074 move those articles instead.
7075 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7076 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7077 re-spool using this method.
7078
7079 For this function to work, both the current newsgroup and the
7080 newsgroup that you want to move to have to support the `request-move'
7081 and `request-accept' functions."
7082   (interactive "P")
7083   (unless action
7084     (setq action 'move))
7085   ;; Disable marking as read.
7086   (let (gnus-mark-article-hook)
7087     (save-window-excursion
7088       (gnus-summary-select-article)))
7089   ;; Check whether the source group supports the required functions.
7090   (cond ((and (eq action 'move)
7091               (not (gnus-check-backend-function
7092                     'request-move-article gnus-newsgroup-name)))
7093          (error "The current group does not support article moving"))
7094         ((and (eq action 'crosspost)
7095               (not (gnus-check-backend-function
7096                     'request-replace-article gnus-newsgroup-name)))
7097          (error "The current group does not support article editing")))
7098   (let ((articles (gnus-summary-work-articles n))
7099         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7100         (names '((move "Move" "Moving")
7101                  (copy "Copy" "Copying")
7102                  (crosspost "Crosspost" "Crossposting")))
7103         (copy-buf (save-excursion
7104                     (nnheader-set-temp-buffer " *copy article*")))
7105         art-group to-method new-xref article to-groups)
7106     (unless (assq action names)
7107       (error "Unknown action %s" action))
7108     ;; Read the newsgroup name.
7109     (when (and (not to-newsgroup)
7110                (not select-method))
7111       (setq to-newsgroup
7112             (gnus-read-move-group-name
7113              (cadr (assq action names))
7114              (symbol-value (intern (format "gnus-current-%s-group" action)))
7115              articles prefix))
7116       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7117     (setq to-method (or select-method
7118                         (gnus-group-name-to-method to-newsgroup)))
7119     ;; Check the method we are to move this article to...
7120     (unless (gnus-check-backend-function
7121              'request-accept-article (car to-method))
7122       (error "%s does not support article copying" (car to-method)))
7123     (unless (gnus-check-server to-method)
7124       (error "Can't open server %s" (car to-method)))
7125     (gnus-message 6 "%s to %s: %s..."
7126                   (caddr (assq action names))
7127                   (or (car select-method) to-newsgroup) articles)
7128     (while articles
7129       (setq article (pop articles))
7130       (setq
7131        art-group
7132        (cond
7133         ;; Move the article.
7134         ((eq action 'move)
7135          ;; Remove this article from future suppression.
7136          (gnus-dup-unsuppress-article article)
7137          (gnus-request-move-article
7138           article                       ; Article to move
7139           gnus-newsgroup-name           ; From newsgroup
7140           (nth 1 (gnus-find-method-for-group
7141                   gnus-newsgroup-name)) ; Server
7142           (list 'gnus-request-accept-article
7143                 to-newsgroup (list 'quote select-method)
7144                 (not articles) t)               ; Accept form
7145           (not articles)))              ; Only save nov last time
7146         ;; Copy the article.
7147         ((eq action 'copy)
7148          (save-excursion
7149            (set-buffer copy-buf)
7150            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7151              (gnus-request-accept-article
7152               to-newsgroup select-method (not articles) t))))
7153         ;; Crosspost the article.
7154         ((eq action 'crosspost)
7155          (let ((xref (message-tokenize-header
7156                       (mail-header-xref (gnus-summary-article-header article))
7157                       " ")))
7158            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7159                                   ":" article))
7160            (unless xref
7161              (setq xref (list (system-name))))
7162            (setq new-xref
7163                  (concat
7164                   (mapconcat 'identity
7165                              (delete "Xref:" (delete new-xref xref))
7166                              " ")
7167                   " " new-xref))
7168            (save-excursion
7169              (set-buffer copy-buf)
7170              ;; First put the article in the destination group.
7171              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7172              (when (consp (setq art-group
7173                                 (gnus-request-accept-article
7174                                  to-newsgroup select-method (not articles))))
7175                (setq new-xref (concat new-xref " " (car art-group)
7176                                       ":" (cdr art-group)))
7177                ;; Now we have the new Xrefs header, so we insert
7178                ;; it and replace the new article.
7179                (nnheader-replace-header "Xref" new-xref)
7180                (gnus-request-replace-article
7181                 (cdr art-group) to-newsgroup (current-buffer))
7182                art-group))))))
7183       (cond
7184        ((not art-group)
7185         (gnus-message 1 "Couldn't %s article %s"
7186                       (cadr (assq action names)) article))
7187        ((and (eq art-group 'junk)
7188              (eq action 'move))
7189         (gnus-summary-mark-article article gnus-canceled-mark)
7190         (gnus-message 4 "Deleted article %s" article))
7191        (t
7192         (let* ((pto-group (gnus-group-prefixed-name
7193                            (car art-group) to-method))
7194                (entry
7195                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7196                (info (nth 2 entry))
7197                (to-group (gnus-info-group info)))
7198           ;; Update the group that has been moved to.
7199           (when (and info
7200                      (memq action '(move copy)))
7201             (unless (member to-group to-groups)
7202               (push to-group to-groups))
7203
7204             (unless (memq article gnus-newsgroup-unreads)
7205               (gnus-info-set-read
7206                info (gnus-add-to-range (gnus-info-read info)
7207                                        (list (cdr art-group)))))
7208
7209             ;; Copy any marks over to the new group.
7210             (let ((marks gnus-article-mark-lists)
7211                   (to-article (cdr art-group)))
7212
7213               ;; See whether the article is to be put in the cache.
7214               (when gnus-use-cache
7215                 (gnus-cache-possibly-enter-article
7216                  to-group to-article
7217                  (let ((header (copy-sequence
7218                                 (gnus-summary-article-header article))))
7219                    (mail-header-set-number header to-article)
7220                    header)
7221                  (memq article gnus-newsgroup-marked)
7222                  (memq article gnus-newsgroup-dormant)
7223                  (memq article gnus-newsgroup-unreads)))
7224
7225               (when (and (equal to-group gnus-newsgroup-name)
7226                          (not (memq article gnus-newsgroup-unreads)))
7227                 ;; Mark this article as read in this group.
7228                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7229                 (setcdr (gnus-active to-group) to-article)
7230                 (setcdr gnus-newsgroup-active to-article))
7231
7232               (while marks
7233                 (when (memq article (symbol-value
7234                                      (intern (format "gnus-newsgroup-%s"
7235                                                      (caar marks)))))
7236                   ;; If the other group is the same as this group,
7237                   ;; then we have to add the mark to the list.
7238                   (when (equal to-group gnus-newsgroup-name)
7239                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7240                          (cons to-article
7241                                (symbol-value
7242                                 (intern (format "gnus-newsgroup-%s"
7243                                                 (caar marks)))))))
7244                   ;; Copy the marks to other group.
7245                   (gnus-add-marked-articles
7246                    to-group (cdar marks) (list to-article) info))
7247                 (setq marks (cdr marks)))
7248
7249               (gnus-dribble-enter
7250                (concat "(gnus-group-set-info '"
7251                        (gnus-prin1-to-string (gnus-get-info to-group))
7252                        ")"))))
7253
7254           ;; Update the Xref header in this article to point to
7255           ;; the new crossposted article we have just created.
7256           (when (eq action 'crosspost)
7257             (save-excursion
7258               (set-buffer copy-buf)
7259               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7260               (nnheader-replace-header "Xref" new-xref)
7261               (gnus-request-replace-article
7262                article gnus-newsgroup-name (current-buffer)))))
7263
7264         ;;;!!!Why is this necessary?
7265         (set-buffer gnus-summary-buffer)
7266
7267         (gnus-summary-goto-subject article)
7268         (when (eq action 'move)
7269           (gnus-summary-mark-article article gnus-canceled-mark))))
7270       (gnus-summary-remove-process-mark article))
7271     ;; Re-activate all groups that have been moved to.
7272     (while to-groups
7273       (save-excursion
7274         (set-buffer gnus-group-buffer)
7275         (when (gnus-group-goto-group (car to-groups) t)
7276           (gnus-group-get-new-news-this-group 1 t))
7277         (pop to-groups)))
7278
7279     (gnus-kill-buffer copy-buf)
7280     (gnus-summary-position-point)
7281     (gnus-set-mode-line 'summary)))
7282
7283 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7284   "Move the current article to a different newsgroup.
7285 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7286 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7287 re-spool using this method."
7288   (interactive "P")
7289   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7290
7291 (defun gnus-summary-crosspost-article (&optional n)
7292   "Crosspost the current article to some other group."
7293   (interactive "P")
7294   (gnus-summary-move-article n nil nil 'crosspost))
7295
7296 (defcustom gnus-summary-respool-default-method nil
7297   "Default method for respooling an article.
7298 If nil, use to the current newsgroup method."
7299   :type '(choice (gnus-select-method :value (nnml ""))
7300                  (const nil))
7301   :group 'gnus-summary-mail)
7302
7303 (defun gnus-summary-respool-article (&optional n method)
7304   "Respool the current article.
7305 The article will be squeezed through the mail spooling process again,
7306 which means that it will be put in some mail newsgroup or other
7307 depending on `nnmail-split-methods'.
7308 If N is a positive number, respool the N next articles.
7309 If N is a negative number, respool the N previous articles.
7310 If N is nil and any articles have been marked with the process mark,
7311 respool those articles instead.
7312
7313 Respooling can be done both from mail groups and \"real\" newsgroups.
7314 In the former case, the articles in question will be moved from the
7315 current group into whatever groups they are destined to.  In the
7316 latter case, they will be copied into the relevant groups."
7317   (interactive
7318    (list current-prefix-arg
7319          (let* ((methods (gnus-methods-using 'respool))
7320                 (methname
7321                  (symbol-name (or gnus-summary-respool-default-method
7322                                   (car (gnus-find-method-for-group
7323                                         gnus-newsgroup-name)))))
7324                 (method
7325                  (gnus-completing-read
7326                   methname "What backend do you want to use when respooling?"
7327                   methods nil t nil 'gnus-mail-method-history))
7328                 ms)
7329            (cond
7330             ((zerop (length (setq ms (gnus-servers-using-backend
7331                                       (intern method)))))
7332              (list (intern method) ""))
7333             ((= 1 (length ms))
7334              (car ms))
7335             (t
7336              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7337                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7338                            ms-alist))))))))
7339   (unless method
7340     (error "No method given for respooling"))
7341   (if (assoc (symbol-name
7342               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7343              (gnus-methods-using 'respool))
7344       (gnus-summary-move-article n nil method)
7345     (gnus-summary-copy-article n nil method)))
7346
7347 (defun gnus-summary-import-article (file)
7348   "Import an arbitrary file into a mail newsgroup."
7349   (interactive "fImport file: ")
7350   (let ((group gnus-newsgroup-name)
7351         (now (current-time))
7352         atts lines)
7353     (unless (gnus-check-backend-function 'request-accept-article group)
7354       (error "%s does not support article importing" group))
7355     (or (file-readable-p file)
7356         (not (file-regular-p file))
7357         (error "Can't read %s" file))
7358     (save-excursion
7359       (set-buffer (gnus-get-buffer-create " *import file*"))
7360       (erase-buffer)
7361       (insert-file-contents file)
7362       (goto-char (point-min))
7363       (unless (nnheader-article-p)
7364         ;; This doesn't look like an article, so we fudge some headers.
7365         (setq atts (file-attributes file)
7366               lines (count-lines (point-min) (point-max)))
7367         (insert "From: " (read-string "From: ") "\n"
7368                 "Subject: " (read-string "Subject: ") "\n"
7369                 "Date: " (message-make-date (nth 5 atts))
7370                 "\n"
7371                 "Message-ID: " (message-make-message-id) "\n"
7372                 "Lines: " (int-to-string lines) "\n"
7373                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7374       (gnus-request-accept-article group nil t)
7375       (kill-buffer (current-buffer)))))
7376
7377 (defun gnus-summary-article-posted-p ()
7378   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7379 This will be the case if the article has both been mailed and posted."
7380   (interactive)
7381   (let ((id (mail-header-references (gnus-summary-article-header)))
7382         (gnus-override-method
7383          (or gnus-refer-article-method gnus-select-method)))
7384     (if (gnus-request-head id "")
7385         (gnus-message 2 "The current message was found on %s"
7386                       gnus-override-method)
7387       (gnus-message 2 "The current message couldn't be found on %s"
7388                     gnus-override-method)
7389       nil)))
7390
7391 (defun gnus-summary-expire-articles (&optional now)
7392   "Expire all articles that are marked as expirable in the current group."
7393   (interactive)
7394   (when (gnus-check-backend-function
7395          'request-expire-articles gnus-newsgroup-name)
7396     ;; This backend supports expiry.
7397     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7398            (expirable (if total
7399                           (progn
7400                             ;; We need to update the info for
7401                             ;; this group for `gnus-list-of-read-articles'
7402                             ;; to give us the right answer.
7403                             (gnus-run-hooks 'gnus-exit-group-hook)
7404                             (gnus-summary-update-info)
7405                             (gnus-list-of-read-articles gnus-newsgroup-name))
7406                         (setq gnus-newsgroup-expirable
7407                               (sort gnus-newsgroup-expirable '<))))
7408            (expiry-wait (if now 'immediate
7409                           (gnus-group-find-parameter
7410                            gnus-newsgroup-name 'expiry-wait)))
7411            es)
7412       (when expirable
7413         ;; There are expirable articles in this group, so we run them
7414         ;; through the expiry process.
7415         (gnus-message 6 "Expiring articles...")
7416         ;; The list of articles that weren't expired is returned.
7417         (save-excursion
7418           (if expiry-wait
7419               (let ((nnmail-expiry-wait-function nil)
7420                     (nnmail-expiry-wait expiry-wait))
7421                 (setq es (gnus-request-expire-articles
7422                           expirable gnus-newsgroup-name)))
7423             (setq es (gnus-request-expire-articles
7424                       expirable gnus-newsgroup-name))))
7425         (unless total
7426           (setq gnus-newsgroup-expirable es))
7427         ;; We go through the old list of expirable, and mark all
7428         ;; really expired articles as nonexistent.
7429         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7430           (let ((gnus-use-cache nil))
7431             (while expirable
7432               (unless (memq (car expirable) es)
7433                 (when (gnus-data-find (car expirable))
7434                   (gnus-summary-mark-article
7435                    (car expirable) gnus-canceled-mark)))
7436               (setq expirable (cdr expirable)))))
7437         (gnus-message 6 "Expiring articles...done")))))
7438
7439 (defun gnus-summary-expire-articles-now ()
7440   "Expunge all expirable articles in the current group.
7441 This means that *all* articles that are marked as expirable will be
7442 deleted forever, right now."
7443   (interactive)
7444   (or gnus-expert-user
7445       (gnus-yes-or-no-p
7446        "Are you really, really, really sure you want to delete all these messages? ")
7447       (error "Phew!"))
7448   (gnus-summary-expire-articles t))
7449
7450 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7451 (defun gnus-summary-delete-article (&optional n)
7452   "Delete the N next (mail) articles.
7453 This command actually deletes articles.  This is not a marking
7454 command.  The article will disappear forever from your life, never to
7455 return.
7456 If N is negative, delete backwards.
7457 If N is nil and articles have been marked with the process mark,
7458 delete these instead."
7459   (interactive "P")
7460   (unless (gnus-check-backend-function 'request-expire-articles
7461                                        gnus-newsgroup-name)
7462     (error "The current newsgroup does not support article deletion"))
7463   ;; Compute the list of articles to delete.
7464   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7465         not-deleted)
7466     (if (and gnus-novice-user
7467              (not (gnus-yes-or-no-p
7468                    (format "Do you really want to delete %s forever? "
7469                            (if (> (length articles) 1)
7470                                (format "these %s articles" (length articles))
7471                              "this article")))))
7472         ()
7473       ;; Delete the articles.
7474       (setq not-deleted (gnus-request-expire-articles
7475                          articles gnus-newsgroup-name 'force))
7476       (while articles
7477         (gnus-summary-remove-process-mark (car articles))
7478         ;; The backend might not have been able to delete the article
7479         ;; after all.
7480         (unless (memq (car articles) not-deleted)
7481           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7482         (setq articles (cdr articles)))
7483       (when not-deleted
7484         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7485     (gnus-summary-position-point)
7486     (gnus-set-mode-line 'summary)
7487     not-deleted))
7488
7489 (defun gnus-summary-edit-article (&optional force)
7490   "Edit the current article.
7491 This will have permanent effect only in mail groups.
7492 If FORCE is non-nil, allow editing of articles even in read-only
7493 groups."
7494   (interactive "P")
7495   (save-excursion
7496     (set-buffer gnus-summary-buffer)
7497     (gnus-set-global-variables)
7498     (when (and (not force)
7499                (gnus-group-read-only-p))
7500       (error "The current newsgroup does not support article editing"))
7501     ;; Select article if needed.
7502     (unless (eq (gnus-summary-article-number)
7503                 gnus-current-article)
7504       (gnus-summary-select-article t))
7505     (gnus-article-date-original)
7506     (gnus-article-edit-article
7507      `(lambda (no-highlight)
7508         (gnus-summary-edit-article-done
7509          ,(or (mail-header-references gnus-current-headers) "")
7510          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7511
7512 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7513
7514 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7515                                                  no-highlight)
7516   "Make edits to the current article permanent."
7517   (interactive)
7518   ;; Replace the article.
7519   (let ((buf (current-buffer)))
7520     (with-temp-buffer
7521       (insert-buffer buf)
7522       (if (and (not read-only)
7523                (not (gnus-request-replace-article
7524                      (cdr gnus-article-current) (car gnus-article-current)
7525                      (current-buffer)
7526                      (not gnus-article-decoded-p))))
7527           (error "Couldn't replace article")
7528         ;; Update the summary buffer.
7529         (if (and references
7530                  (equal (message-tokenize-header references " ")
7531                         (message-tokenize-header
7532                          (or (message-fetch-field "references") "") " ")))
7533             ;; We only have to update this line.
7534             (save-excursion
7535               (save-restriction
7536                 (message-narrow-to-head)
7537                 (let ((head (buffer-string))
7538                       header)
7539                   (with-temp-buffer
7540                     (insert (format "211 %d Article retrieved.\n"
7541                                     (cdr gnus-article-current)))
7542                     (insert head)
7543                     (insert ".\n")
7544                     (let ((nntp-server-buffer (current-buffer)))
7545                       (setq header (car (gnus-get-newsgroup-headers
7546                                          (save-excursion
7547                                            (set-buffer gnus-summary-buffer)
7548                                            gnus-newsgroup-dependencies)
7549                                          t))))
7550                     (save-excursion
7551                       (set-buffer gnus-summary-buffer)
7552                       (gnus-data-set-header
7553                        (gnus-data-find (cdr gnus-article-current))
7554                        header)
7555                       (gnus-summary-update-article-line
7556                        (cdr gnus-article-current) header))))))
7557           ;; Update threads.
7558           (set-buffer (or buffer gnus-summary-buffer))
7559           (gnus-summary-update-article (cdr gnus-article-current)))
7560         ;; Prettify the article buffer again.
7561         (unless no-highlight
7562           (save-excursion
7563             (set-buffer gnus-article-buffer)
7564             (gnus-run-hooks 'gnus-article-display-hook)
7565             (set-buffer gnus-original-article-buffer)
7566             (gnus-request-article
7567              (cdr gnus-article-current)
7568              (car gnus-article-current) (current-buffer))))
7569         ;; Prettify the summary buffer line.
7570         (when (gnus-visual-p 'summary-highlight 'highlight)
7571           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7572
7573 (defun gnus-summary-edit-wash (key)
7574   "Perform editing command KEY in the article buffer."
7575   (interactive
7576    (list
7577     (progn
7578       (message "%s" (concat (this-command-keys) "- "))
7579       (read-char))))
7580   (message "")
7581   (gnus-summary-edit-article)
7582   (execute-kbd-macro (concat (this-command-keys) key))
7583   (gnus-article-edit-done))
7584
7585 ;;; Respooling
7586
7587 (defun gnus-summary-respool-query (&optional silent trace)
7588   "Query where the respool algorithm would put this article."
7589   (interactive)
7590   (let (gnus-mark-article-hook)
7591     (gnus-summary-select-article)
7592     (save-excursion
7593       (set-buffer gnus-original-article-buffer)
7594       (save-restriction
7595         (message-narrow-to-head)
7596         (let ((groups (nnmail-article-group 'identity trace)))
7597           (unless silent
7598             (if groups
7599                 (message "This message would go to %s"
7600                          (mapconcat 'car groups ", "))
7601               (message "This message would go to no groups"))
7602             groups))))))
7603
7604 (defun gnus-summary-respool-trace ()
7605   "Trace where the respool algorithm would put this article.
7606 Display a buffer showing all fancy splitting patterns which matched."
7607   (interactive)
7608   (gnus-summary-respool-query nil t))
7609
7610 ;; Summary marking commands.
7611
7612 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7613   "Mark articles which has the same subject as read, and then select the next.
7614 If UNMARK is positive, remove any kind of mark.
7615 If UNMARK is negative, tick articles."
7616   (interactive "P")
7617   (when unmark
7618     (setq unmark (prefix-numeric-value unmark)))
7619   (let ((count
7620          (gnus-summary-mark-same-subject
7621           (gnus-summary-article-subject) unmark)))
7622     ;; Select next unread article.  If auto-select-same mode, should
7623     ;; select the first unread article.
7624     (gnus-summary-next-article t (and gnus-auto-select-same
7625                                       (gnus-summary-article-subject)))
7626     (gnus-message 7 "%d article%s marked as %s"
7627                   count (if (= count 1) " is" "s are")
7628                   (if unmark "unread" "read"))))
7629
7630 (defun gnus-summary-kill-same-subject (&optional unmark)
7631   "Mark articles which has the same subject as read.
7632 If UNMARK is positive, remove any kind of mark.
7633 If UNMARK is negative, tick articles."
7634   (interactive "P")
7635   (when unmark
7636     (setq unmark (prefix-numeric-value unmark)))
7637   (let ((count
7638          (gnus-summary-mark-same-subject
7639           (gnus-summary-article-subject) unmark)))
7640     ;; If marked as read, go to next unread subject.
7641     (when (null unmark)
7642       ;; Go to next unread subject.
7643       (gnus-summary-next-subject 1 t))
7644     (gnus-message 7 "%d articles are marked as %s"
7645                   count (if unmark "unread" "read"))))
7646
7647 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7648   "Mark articles with same SUBJECT as read, and return marked number.
7649 If optional argument UNMARK is positive, remove any kinds of marks.
7650 If optional argument UNMARK is negative, mark articles as unread instead."
7651   (let ((count 1))
7652     (save-excursion
7653       (cond
7654        ((null unmark)                   ; Mark as read.
7655         (while (and
7656                 (progn
7657                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7658                   (gnus-summary-show-thread) t)
7659                 (gnus-summary-find-subject subject))
7660           (setq count (1+ count))))
7661        ((> unmark 0)                    ; Tick.
7662         (while (and
7663                 (progn
7664                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7665                   (gnus-summary-show-thread) t)
7666                 (gnus-summary-find-subject subject))
7667           (setq count (1+ count))))
7668        (t                               ; Mark as unread.
7669         (while (and
7670                 (progn
7671                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7672                   (gnus-summary-show-thread) t)
7673                 (gnus-summary-find-subject subject))
7674           (setq count (1+ count)))))
7675       (gnus-set-mode-line 'summary)
7676       ;; Return the number of marked articles.
7677       count)))
7678
7679 (defun gnus-summary-mark-as-processable (n &optional unmark)
7680   "Set the process mark on the next N articles.
7681 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7682 the process mark instead.  The difference between N and the actual
7683 number of articles marked is returned."
7684   (interactive "p")
7685   (let ((backward (< n 0))
7686         (n (abs n)))
7687     (while (and
7688             (> n 0)
7689             (if unmark
7690                 (gnus-summary-remove-process-mark
7691                  (gnus-summary-article-number))
7692               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7693             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7694       (setq n (1- n)))
7695     (when (/= 0 n)
7696       (gnus-message 7 "No more articles"))
7697     (gnus-summary-recenter)
7698     (gnus-summary-position-point)
7699     n))
7700
7701 (defun gnus-summary-unmark-as-processable (n)
7702   "Remove the process mark from the next N articles.
7703 If N is negative, unmark backward instead.  The difference between N and
7704 the actual number of articles unmarked is returned."
7705   (interactive "p")
7706   (gnus-summary-mark-as-processable n t))
7707
7708 (defun gnus-summary-unmark-all-processable ()
7709   "Remove the process mark from all articles."
7710   (interactive)
7711   (save-excursion
7712     (while gnus-newsgroup-processable
7713       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7714   (gnus-summary-position-point))
7715
7716 (defun gnus-summary-mark-as-expirable (n)
7717   "Mark N articles forward as expirable.
7718 If N is negative, mark backward instead.  The difference between N and
7719 the actual number of articles marked is returned."
7720   (interactive "p")
7721   (gnus-summary-mark-forward n gnus-expirable-mark))
7722
7723 (defun gnus-summary-mark-article-as-replied (article)
7724   "Mark ARTICLE replied and update the summary line."
7725   (push article gnus-newsgroup-replied)
7726   (let ((buffer-read-only nil))
7727     (when (gnus-summary-goto-subject article nil t)
7728       (gnus-summary-update-secondary-mark article))))
7729
7730 (defun gnus-summary-set-bookmark (article)
7731   "Set a bookmark in current article."
7732   (interactive (list (gnus-summary-article-number)))
7733   (when (or (not (get-buffer gnus-article-buffer))
7734             (not gnus-current-article)
7735             (not gnus-article-current)
7736             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7737     (error "No current article selected"))
7738   ;; Remove old bookmark, if one exists.
7739   (let ((old (assq article gnus-newsgroup-bookmarks)))
7740     (when old
7741       (setq gnus-newsgroup-bookmarks
7742             (delq old gnus-newsgroup-bookmarks))))
7743   ;; Set the new bookmark, which is on the form
7744   ;; (article-number . line-number-in-body).
7745   (push
7746    (cons article
7747          (save-excursion
7748            (set-buffer gnus-article-buffer)
7749            (count-lines
7750             (min (point)
7751                  (save-excursion
7752                    (goto-char (point-min))
7753                    (search-forward "\n\n" nil t)
7754                    (point)))
7755             (point))))
7756    gnus-newsgroup-bookmarks)
7757   (gnus-message 6 "A bookmark has been added to the current article."))
7758
7759 (defun gnus-summary-remove-bookmark (article)
7760   "Remove the bookmark from the current article."
7761   (interactive (list (gnus-summary-article-number)))
7762   ;; Remove old bookmark, if one exists.
7763   (let ((old (assq article gnus-newsgroup-bookmarks)))
7764     (if old
7765         (progn
7766           (setq gnus-newsgroup-bookmarks
7767                 (delq old gnus-newsgroup-bookmarks))
7768           (gnus-message 6 "Removed bookmark."))
7769       (gnus-message 6 "No bookmark in current article."))))
7770
7771 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7772 (defun gnus-summary-mark-as-dormant (n)
7773   "Mark N articles forward as dormant.
7774 If N is negative, mark backward instead.  The difference between N and
7775 the actual number of articles marked is returned."
7776   (interactive "p")
7777   (gnus-summary-mark-forward n gnus-dormant-mark))
7778
7779 (defun gnus-summary-set-process-mark (article)
7780   "Set the process mark on ARTICLE and update the summary line."
7781   (setq gnus-newsgroup-processable
7782         (cons article
7783               (delq article gnus-newsgroup-processable)))
7784   (when (gnus-summary-goto-subject article)
7785     (gnus-summary-show-thread)
7786     (gnus-summary-goto-subject article)
7787     (gnus-summary-update-secondary-mark article)))
7788
7789 (defun gnus-summary-remove-process-mark (article)
7790   "Remove the process mark from ARTICLE and update the summary line."
7791   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7792   (when (gnus-summary-goto-subject article)
7793     (gnus-summary-show-thread)
7794     (gnus-summary-goto-subject article)
7795     (gnus-summary-update-secondary-mark article)))
7796
7797 (defun gnus-summary-set-saved-mark (article)
7798   "Set the process mark on ARTICLE and update the summary line."
7799   (push article gnus-newsgroup-saved)
7800   (when (gnus-summary-goto-subject article)
7801     (gnus-summary-update-secondary-mark article)))
7802
7803 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7804   "Mark N articles as read forwards.
7805 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7806 The difference between N and the actual number of articles marked is
7807 returned."
7808   (interactive "p")
7809   (gnus-summary-show-thread)
7810   (let ((backward (< n 0))
7811         (gnus-summary-goto-unread
7812          (and gnus-summary-goto-unread
7813               (not (eq gnus-summary-goto-unread 'never))
7814               (not (memq mark (list gnus-unread-mark
7815                                     gnus-ticked-mark gnus-dormant-mark)))))
7816         (n (abs n))
7817         (mark (or mark gnus-del-mark)))
7818     (while (and (> n 0)
7819                 (gnus-summary-mark-article nil mark no-expire)
7820                 (zerop (gnus-summary-next-subject
7821                         (if backward -1 1)
7822                         (and gnus-summary-goto-unread
7823                              (not (eq gnus-summary-goto-unread 'never)))
7824                         t)))
7825       (setq n (1- n)))
7826     (when (/= 0 n)
7827       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7828     (gnus-summary-recenter)
7829     (gnus-summary-position-point)
7830     (gnus-set-mode-line 'summary)
7831     n))
7832
7833 (defun gnus-summary-mark-article-as-read (mark)
7834   "Mark the current article quickly as read with MARK."
7835   (let ((article (gnus-summary-article-number)))
7836     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7837     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7838     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7839     (push (cons article mark) gnus-newsgroup-reads)
7840     ;; Possibly remove from cache, if that is used.
7841     (when gnus-use-cache
7842       (gnus-cache-enter-remove-article article))
7843     ;; Allow the backend to change the mark.
7844     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7845     ;; Check for auto-expiry.
7846     (when (and gnus-newsgroup-auto-expire
7847                (memq mark gnus-auto-expirable-marks))
7848       (setq mark gnus-expirable-mark)
7849       ;; Let the backend know about the mark change.
7850       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7851       (push article gnus-newsgroup-expirable))
7852     ;; Set the mark in the buffer.
7853     (gnus-summary-update-mark mark 'unread)
7854     t))
7855
7856 (defun gnus-summary-mark-article-as-unread (mark)
7857   "Mark the current article quickly as unread with MARK."
7858   (let* ((article (gnus-summary-article-number))
7859          (old-mark (gnus-summary-article-mark article)))
7860     ;; Allow the backend to change the mark.
7861     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7862     (if (eq mark old-mark)
7863         t
7864       (if (<= article 0)
7865           (progn
7866             (gnus-error 1 "Can't mark negative article numbers")
7867             nil)
7868         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7869         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7870         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7871         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7872         (cond ((= mark gnus-ticked-mark)
7873                (push article gnus-newsgroup-marked))
7874               ((= mark gnus-dormant-mark)
7875                (push article gnus-newsgroup-dormant))
7876               (t
7877                (push article gnus-newsgroup-unreads)))
7878         (gnus-pull article gnus-newsgroup-reads)
7879
7880         ;; See whether the article is to be put in the cache.
7881         (and gnus-use-cache
7882              (vectorp (gnus-summary-article-header article))
7883              (save-excursion
7884                (gnus-cache-possibly-enter-article
7885                 gnus-newsgroup-name article
7886                 (gnus-summary-article-header article)
7887                 (= mark gnus-ticked-mark)
7888                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7889
7890         ;; Fix the mark.
7891         (gnus-summary-update-mark mark 'unread)
7892         t))))
7893
7894 (defun gnus-summary-mark-article (&optional article mark no-expire)
7895   "Mark ARTICLE with MARK.  MARK can be any character.
7896 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7897 `??' (dormant) and `?E' (expirable).
7898 If MARK is nil, then the default character `?r' is used.
7899 If ARTICLE is nil, then the article on the current line will be
7900 marked."
7901   ;; The mark might be a string.
7902   (when (stringp mark)
7903     (setq mark (aref mark 0)))
7904   ;; If no mark is given, then we check auto-expiring.
7905   (when (null mark)
7906     (setq mark gnus-del-mark))
7907   (when (and (not no-expire)
7908              gnus-newsgroup-auto-expire
7909              (memq mark gnus-auto-expirable-marks))
7910     (setq mark gnus-expirable-mark))
7911   (let ((article (or article (gnus-summary-article-number)))
7912         (old-mark (gnus-summary-article-mark article)))
7913     ;; Allow the backend to change the mark.
7914     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7915     (if (eq mark old-mark)
7916         t
7917       (unless article
7918         (error "No article on current line"))
7919       (if (not (if (or (= mark gnus-unread-mark)
7920                        (= mark gnus-ticked-mark)
7921                        (= mark gnus-dormant-mark))
7922                    (gnus-mark-article-as-unread article mark)
7923                  (gnus-mark-article-as-read article mark)))
7924           t
7925         ;; See whether the article is to be put in the cache.
7926         (and gnus-use-cache
7927              (not (= mark gnus-canceled-mark))
7928              (vectorp (gnus-summary-article-header article))
7929              (save-excursion
7930                (gnus-cache-possibly-enter-article
7931                 gnus-newsgroup-name article
7932                 (gnus-summary-article-header article)
7933                 (= mark gnus-ticked-mark)
7934                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7935
7936         (when (gnus-summary-goto-subject article nil t)
7937           (let ((buffer-read-only nil))
7938             (gnus-summary-show-thread)
7939             ;; Fix the mark.
7940             (gnus-summary-update-mark mark 'unread)
7941             t))))))
7942
7943 (defun gnus-summary-update-secondary-mark (article)
7944   "Update the secondary (read, process, cache) mark."
7945   (gnus-summary-update-mark
7946    (cond ((memq article gnus-newsgroup-processable)
7947           gnus-process-mark)
7948          ((memq article gnus-newsgroup-cached)
7949           gnus-cached-mark)
7950          ((memq article gnus-newsgroup-replied)
7951           gnus-replied-mark)
7952          ((memq article gnus-newsgroup-saved)
7953           gnus-saved-mark)
7954          (t gnus-unread-mark))
7955    'replied)
7956   (when (gnus-visual-p 'summary-highlight 'highlight)
7957     (gnus-run-hooks 'gnus-summary-update-hook))
7958   t)
7959
7960 (defun gnus-summary-update-mark (mark type)
7961   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7962         (buffer-read-only nil))
7963     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7964     (when forward
7965       (when (looking-at "\r")
7966         (incf forward))
7967       (when (<= (+ forward (point)) (point-max))
7968         ;; Go to the right position on the line.
7969         (goto-char (+ forward (point)))
7970         ;; Replace the old mark with the new mark.
7971         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7972         ;; Optionally update the marks by some user rule.
7973         (when (eq type 'unread)
7974           (gnus-data-set-mark
7975            (gnus-data-find (gnus-summary-article-number)) mark)
7976           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7977
7978 (defun gnus-mark-article-as-read (article &optional mark)
7979   "Enter ARTICLE in the pertinent lists and remove it from others."
7980   ;; Make the article expirable.
7981   (let ((mark (or mark gnus-del-mark)))
7982     (if (= mark gnus-expirable-mark)
7983         (push article gnus-newsgroup-expirable)
7984       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7985     ;; Remove from unread and marked lists.
7986     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7987     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7988     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7989     (push (cons article mark) gnus-newsgroup-reads)
7990     ;; Possibly remove from cache, if that is used.
7991     (when gnus-use-cache
7992       (gnus-cache-enter-remove-article article))
7993     t))
7994
7995 (defun gnus-mark-article-as-unread (article &optional mark)
7996   "Enter ARTICLE in the pertinent lists and remove it from others."
7997   (let ((mark (or mark gnus-ticked-mark)))
7998     (if (<= article 0)
7999         (progn
8000           (gnus-error 1 "Can't mark negative article numbers")
8001           nil)
8002       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8003             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8004             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8005             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8006
8007       ;; Unsuppress duplicates?
8008       (when gnus-suppress-duplicates
8009         (gnus-dup-unsuppress-article article))
8010
8011       (cond ((= mark gnus-ticked-mark)
8012              (push article gnus-newsgroup-marked))
8013             ((= mark gnus-dormant-mark)
8014              (push article gnus-newsgroup-dormant))
8015             (t
8016              (push article gnus-newsgroup-unreads)))
8017       (gnus-pull article gnus-newsgroup-reads)
8018       t)))
8019
8020 (defalias 'gnus-summary-mark-as-unread-forward
8021   'gnus-summary-tick-article-forward)
8022 (make-obsolete 'gnus-summary-mark-as-unread-forward
8023                'gnus-summary-tick-article-forward)
8024 (defun gnus-summary-tick-article-forward (n)
8025   "Tick N articles forwards.
8026 If N is negative, tick backwards instead.
8027 The difference between N and the number of articles ticked is returned."
8028   (interactive "p")
8029   (gnus-summary-mark-forward n gnus-ticked-mark))
8030
8031 (defalias 'gnus-summary-mark-as-unread-backward
8032   'gnus-summary-tick-article-backward)
8033 (make-obsolete 'gnus-summary-mark-as-unread-backward
8034                'gnus-summary-tick-article-backward)
8035 (defun gnus-summary-tick-article-backward (n)
8036   "Tick N articles backwards.
8037 The difference between N and the number of articles ticked is returned."
8038   (interactive "p")
8039   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8040
8041 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8042 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8043 (defun gnus-summary-tick-article (&optional article clear-mark)
8044   "Mark current article as unread.
8045 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8046 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8047   (interactive)
8048   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8049                                        gnus-ticked-mark)))
8050
8051 (defun gnus-summary-mark-as-read-forward (n)
8052   "Mark N articles as read forwards.
8053 If N is negative, mark backwards instead.
8054 The difference between N and the actual number of articles marked is
8055 returned."
8056   (interactive "p")
8057   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8058
8059 (defun gnus-summary-mark-as-read-backward (n)
8060   "Mark the N articles as read backwards.
8061 The difference between N and the actual number of articles marked is
8062 returned."
8063   (interactive "p")
8064   (gnus-summary-mark-forward (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8065
8066 (defun gnus-summary-mark-as-read (&optional article mark)
8067   "Mark current article as read.
8068 ARTICLE specifies the article to be marked as read.
8069 MARK specifies a string to be inserted at the beginning of the line."
8070   (gnus-summary-mark-article article mark))
8071
8072 (defun gnus-summary-clear-mark-forward (n)
8073   "Clear marks from N articles forward.
8074 If N is negative, clear backward instead.
8075 The difference between N and the number of marks cleared is returned."
8076   (interactive "p")
8077   (gnus-summary-mark-forward n gnus-unread-mark))
8078
8079 (defun gnus-summary-clear-mark-backward (n)
8080   "Clear marks from N articles backward.
8081 The difference between N and the number of marks cleared is returned."
8082   (interactive "p")
8083   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8084
8085 (defun gnus-summary-mark-unread-as-read ()
8086   "Intended to be used by `gnus-summary-mark-article-hook'."
8087   (when (memq gnus-current-article gnus-newsgroup-unreads)
8088     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8089
8090 (defun gnus-summary-mark-read-and-unread-as-read ()
8091   "Intended to be used by `gnus-summary-mark-article-hook'."
8092   (let ((mark (gnus-summary-article-mark)))
8093     (when (or (gnus-unread-mark-p mark)
8094               (gnus-read-mark-p mark))
8095       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8096
8097 (defun gnus-summary-mark-region-as-read (point mark all)
8098   "Mark all unread articles between point and mark as read.
8099 If given a prefix, mark all articles between point and mark as read,
8100 even ticked and dormant ones."
8101   (interactive "r\nP")
8102   (save-excursion
8103     (let (article)
8104       (goto-char point)
8105       (beginning-of-line)
8106       (while (and
8107               (< (point) mark)
8108               (progn
8109                 (when (or all
8110                           (memq (setq article (gnus-summary-article-number))
8111                                 gnus-newsgroup-unreads))
8112                   (gnus-summary-mark-article article gnus-del-mark))
8113                 t)
8114               (gnus-summary-find-next))))))
8115
8116 (defun gnus-summary-mark-below (score mark)
8117   "Mark articles with score less than SCORE with MARK."
8118   (interactive "P\ncMark: ")
8119   (setq score (if score
8120                   (prefix-numeric-value score)
8121                 (or gnus-summary-default-score 0)))
8122   (save-excursion
8123     (set-buffer gnus-summary-buffer)
8124     (goto-char (point-min))
8125     (while
8126         (progn
8127           (and (< (gnus-summary-article-score) score)
8128                (gnus-summary-mark-article nil mark))
8129           (gnus-summary-find-next)))))
8130
8131 (defun gnus-summary-kill-below (&optional score)
8132   "Mark articles with score below SCORE as read."
8133   (interactive "P")
8134   (gnus-summary-mark-below score gnus-killed-mark))
8135
8136 (defun gnus-summary-clear-above (&optional score)
8137   "Clear all marks from articles with score above SCORE."
8138   (interactive "P")
8139   (gnus-summary-mark-above score gnus-unread-mark))
8140
8141 (defun gnus-summary-tick-above (&optional score)
8142   "Tick all articles with score above SCORE."
8143   (interactive "P")
8144   (gnus-summary-mark-above score gnus-ticked-mark))
8145
8146 (defun gnus-summary-mark-above (score mark)
8147   "Mark articles with score over SCORE with MARK."
8148   (interactive "P\ncMark: ")
8149   (setq score (if score
8150                   (prefix-numeric-value score)
8151                 (or gnus-summary-default-score 0)))
8152   (save-excursion
8153     (set-buffer gnus-summary-buffer)
8154     (goto-char (point-min))
8155     (while (and (progn
8156                   (when (> (gnus-summary-article-score) score)
8157                     (gnus-summary-mark-article nil mark))
8158                   t)
8159                 (gnus-summary-find-next)))))
8160
8161 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8162 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8163 (defun gnus-summary-limit-include-expunged (&optional no-error)
8164   "Display all the hidden articles that were expunged for low scores."
8165   (interactive)
8166   (let ((buffer-read-only nil))
8167     (let ((scored gnus-newsgroup-scored)
8168           headers h)
8169       (while scored
8170         (unless (gnus-summary-goto-subject (caar scored))
8171           (and (setq h (gnus-summary-article-header (caar scored)))
8172                (< (cdar scored) gnus-summary-expunge-below)
8173                (push h headers)))
8174         (setq scored (cdr scored)))
8175       (if (not headers)
8176           (when (not no-error)
8177             (error "No expunged articles hidden"))
8178         (goto-char (point-min))
8179         (gnus-summary-prepare-unthreaded (nreverse headers))
8180         (goto-char (point-min))
8181         (gnus-summary-position-point)
8182         t))))
8183
8184 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8185   "Mark all unread articles in this newsgroup as read.
8186 If prefix argument ALL is non-nil, ticked and dormant articles will
8187 also be marked as read.
8188 If QUIETLY is non-nil, no questions will be asked.
8189 If TO-HERE is non-nil, it should be a point in the buffer.  All
8190 articles before this point will be marked as read.
8191 Note that this function will only catch up the unread article
8192 in the current summary buffer limitation.
8193 The number of articles marked as read is returned."
8194   (interactive "P")
8195   (prog1
8196       (save-excursion
8197         (when (or quietly
8198                   (not gnus-interactive-catchup) ;Without confirmation?
8199                   gnus-expert-user
8200                   (gnus-y-or-n-p
8201                    (if all
8202                        "Mark absolutely all articles as read? "
8203                      "Mark all unread articles as read? ")))
8204           (if (and not-mark
8205                    (not gnus-newsgroup-adaptive)
8206                    (not gnus-newsgroup-auto-expire)
8207                    (not gnus-suppress-duplicates)
8208                    (or (not gnus-use-cache)
8209                        (eq gnus-use-cache 'passive)))
8210               (progn
8211                 (when all
8212                   (setq gnus-newsgroup-marked nil
8213                         gnus-newsgroup-dormant nil))
8214                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8215             ;; We actually mark all articles as canceled, which we
8216             ;; have to do when using auto-expiry or adaptive scoring.
8217             (gnus-summary-show-all-threads)
8218             (when (gnus-summary-first-subject (not all) t)
8219               (while (and
8220                       (if to-here (< (point) to-here) t)
8221                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8222                       (gnus-summary-find-next (not all) nil nil t))))
8223             (gnus-set-mode-line 'summary))
8224           t))
8225     (gnus-summary-position-point)))
8226
8227 (defun gnus-summary-catchup-to-here (&optional all)
8228   "Mark all unticked articles before the current one as read.
8229 If ALL is non-nil, also mark ticked and dormant articles as read."
8230   (interactive "P")
8231   (save-excursion
8232     (gnus-save-hidden-threads
8233       (let ((beg (point)))
8234         ;; We check that there are unread articles.
8235         (when (or all (gnus-summary-find-prev))
8236           (gnus-summary-catchup all t beg)))))
8237   (gnus-summary-position-point))
8238
8239 (defun gnus-summary-catchup-all (&optional quietly)
8240   "Mark all articles in this newsgroup as read."
8241   (interactive "P")
8242   (gnus-summary-catchup t quietly))
8243
8244 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8245   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8246 If prefix argument ALL is non-nil, all articles are marked as read."
8247   (interactive "P")
8248   (when (gnus-summary-catchup all quietly nil 'fast)
8249     ;; Select next newsgroup or exit.
8250     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8251              (eq gnus-auto-select-next 'quietly))
8252         (gnus-summary-next-group nil)
8253       (gnus-summary-exit))))
8254
8255 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8256   "Mark all articles in this newsgroup as read, and then exit."
8257   (interactive "P")
8258   (gnus-summary-catchup-and-exit t quietly))
8259
8260 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8261 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8262   "Mark all articles in this group as read and select the next group.
8263 If given a prefix, mark all articles, unread as well as ticked, as
8264 read."
8265   (interactive "P")
8266   (save-excursion
8267     (gnus-summary-catchup all))
8268   (gnus-summary-next-article t nil nil t))
8269
8270 ;; Thread-based commands.
8271
8272 (defun gnus-summary-articles-in-thread (&optional article)
8273   "Return a list of all articles in the current thread.
8274 If ARTICLE is non-nil, return all articles in the thread that starts
8275 with that article."
8276   (let* ((article (or article (gnus-summary-article-number)))
8277          (data (gnus-data-find-list article))
8278          (top-level (gnus-data-level (car data)))
8279          (top-subject
8280           (cond ((null gnus-thread-operation-ignore-subject)
8281                  (gnus-simplify-subject-re
8282                   (mail-header-subject (gnus-data-header (car data)))))
8283                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8284                  (gnus-simplify-subject-fuzzy
8285                   (mail-header-subject (gnus-data-header (car data)))))
8286                 (t nil)))
8287          (end-point (save-excursion
8288                       (if (gnus-summary-go-to-next-thread)
8289                           (point) (point-max))))
8290          articles)
8291     (while (and data
8292                 (< (gnus-data-pos (car data)) end-point))
8293       (when (or (not top-subject)
8294                 (string= top-subject
8295                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8296                              (gnus-simplify-subject-fuzzy
8297                               (mail-header-subject
8298                                (gnus-data-header (car data))))
8299                            (gnus-simplify-subject-re
8300                             (mail-header-subject
8301                              (gnus-data-header (car data)))))))
8302         (push (gnus-data-number (car data)) articles))
8303       (unless (and (setq data (cdr data))
8304                    (> (gnus-data-level (car data)) top-level))
8305         (setq data nil)))
8306     ;; Return the list of articles.
8307     (nreverse articles)))
8308
8309 (defun gnus-summary-rethread-current ()
8310   "Rethread the thread the current article is part of."
8311   (interactive)
8312   (let* ((gnus-show-threads t)
8313          (article (gnus-summary-article-number))
8314          (id (mail-header-id (gnus-summary-article-header)))
8315          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8316     (unless id
8317       (error "No article on the current line"))
8318     (gnus-rebuild-thread id)
8319     (gnus-summary-goto-subject article)))
8320
8321 (defun gnus-summary-reparent-thread ()
8322   "Make the current article child of the marked (or previous) article.
8323
8324 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8325 is non-nil or the Subject: of both articles are the same."
8326   (interactive)
8327   (unless (not (gnus-group-read-only-p))
8328     (error "The current newsgroup does not support article editing"))
8329   (unless (<= (length gnus-newsgroup-processable) 1)
8330     (error "No more than one article may be marked"))
8331   (save-window-excursion
8332     (let ((gnus-article-buffer " *reparent*")
8333           (current-article (gnus-summary-article-number))
8334           ;; First grab the marked article, otherwise one line up.
8335           (parent-article (if (not (null gnus-newsgroup-processable))
8336                               (car gnus-newsgroup-processable)
8337                             (save-excursion
8338                               (if (eq (forward-line -1) 0)
8339                                   (gnus-summary-article-number)
8340                                 (error "Beginning of summary buffer"))))))
8341       (unless (not (eq current-article parent-article))
8342         (error "An article may not be self-referential"))
8343       (let ((message-id (mail-header-id
8344                          (gnus-summary-article-header parent-article))))
8345         (unless (and message-id (not (equal message-id "")))
8346           (error "No message-id in desired parent"))
8347         ;; We don't want the article to be marked as read.
8348         (let (gnus-mark-article-hook)
8349           (gnus-summary-select-article t t nil current-article))
8350         (set-buffer gnus-original-article-buffer)
8351         (let ((buf (format "%s" (buffer-string))))
8352           (with-temp-buffer
8353             (insert buf)
8354             (goto-char (point-min))
8355             (if (re-search-forward "^References: " nil t)
8356                 (progn
8357                   (re-search-forward "^[^ \t]" nil t)
8358                   (forward-line -1)
8359                   (end-of-line)
8360                   (insert " " message-id))
8361               (insert "References: " message-id "\n"))
8362             (unless (gnus-request-replace-article
8363                      current-article (car gnus-article-current)
8364                      (current-buffer))
8365               (error "Couldn't replace article"))))
8366         (set-buffer gnus-summary-buffer)
8367         (gnus-summary-unmark-all-processable)
8368         (gnus-summary-update-article current-article)
8369         (gnus-summary-rethread-current)
8370         (gnus-message 3 "Article %d is now the child of article %d"
8371                       current-article parent-article)))))
8372
8373 (defun gnus-summary-toggle-threads (&optional arg)
8374   "Toggle showing conversation threads.
8375 If ARG is positive number, turn showing conversation threads on."
8376   (interactive "P")
8377   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8378     (setq gnus-show-threads
8379           (if (null arg) (not gnus-show-threads)
8380             (> (prefix-numeric-value arg) 0)))
8381     (gnus-summary-prepare)
8382     (gnus-summary-goto-subject current)
8383     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8384     (gnus-summary-position-point)))
8385
8386 (defun gnus-summary-show-all-threads ()
8387   "Show all threads."
8388   (interactive)
8389   (save-excursion
8390     (let ((buffer-read-only nil))
8391       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8392   (gnus-summary-position-point))
8393
8394 (defun gnus-summary-show-thread ()
8395   "Show thread subtrees.
8396 Returns nil if no thread was there to be shown."
8397   (interactive)
8398   (let ((buffer-read-only nil)
8399         (orig (point))
8400         ;; first goto end then to beg, to have point at beg after let
8401         (end (progn (end-of-line) (point)))
8402         (beg (progn (beginning-of-line) (point))))
8403     (prog1
8404         ;; Any hidden lines here?
8405         (search-forward "\r" end t)
8406       (subst-char-in-region beg end ?\^M ?\n t)
8407       (goto-char orig)
8408       (gnus-summary-position-point))))
8409
8410 (defun gnus-summary-hide-all-threads ()
8411   "Hide all thread subtrees."
8412   (interactive)
8413   (save-excursion
8414     (goto-char (point-min))
8415     (gnus-summary-hide-thread)
8416     (while (zerop (gnus-summary-next-thread 1 t))
8417       (gnus-summary-hide-thread)))
8418   (gnus-summary-position-point))
8419
8420 (defun gnus-summary-hide-thread ()
8421   "Hide thread subtrees.
8422 Returns nil if no threads were there to be hidden."
8423   (interactive)
8424   (let ((buffer-read-only nil)
8425         (start (point))
8426         (article (gnus-summary-article-number)))
8427     (goto-char start)
8428     ;; Go forward until either the buffer ends or the subthread
8429     ;; ends.
8430     (when (and (not (eobp))
8431                (or (zerop (gnus-summary-next-thread 1 t))
8432                    (goto-char (point-max))))
8433       (prog1
8434           (if (and (> (point) start)
8435                    (search-backward "\n" start t))
8436               (progn
8437                 (subst-char-in-region start (point) ?\n ?\^M)
8438                 (gnus-summary-goto-subject article))
8439             (goto-char start)
8440             nil)
8441         ;;(gnus-summary-position-point)
8442         ))))
8443
8444 (defun gnus-summary-go-to-next-thread (&optional previous)
8445   "Go to the same level (or less) next thread.
8446 If PREVIOUS is non-nil, go to previous thread instead.
8447 Return the article number moved to, or nil if moving was impossible."
8448   (let ((level (gnus-summary-thread-level))
8449         (way (if previous -1 1))
8450         (beg (point)))
8451     (forward-line way)
8452     (while (and (not (eobp))
8453                 (< level (gnus-summary-thread-level)))
8454       (forward-line way))
8455     (if (eobp)
8456         (progn
8457           (goto-char beg)
8458           nil)
8459       (setq beg (point))
8460       (prog1
8461           (gnus-summary-article-number)
8462         (goto-char beg)))))
8463
8464 (defun gnus-summary-next-thread (n &optional silent)
8465   "Go to the same level next N'th thread.
8466 If N is negative, search backward instead.
8467 Returns the difference between N and the number of skips actually
8468 done.
8469
8470 If SILENT, don't output messages."
8471   (interactive "p")
8472   (let ((backward (< n 0))
8473         (n (abs n)))
8474     (while (and (> n 0)
8475                 (gnus-summary-go-to-next-thread backward))
8476       (decf n))
8477     (unless silent
8478       (gnus-summary-position-point))
8479     (when (and (not silent) (/= 0 n))
8480       (gnus-message 7 "No more threads"))
8481     n))
8482
8483 (defun gnus-summary-prev-thread (n)
8484   "Go to the same level previous N'th thread.
8485 Returns the difference between N and the number of skips actually
8486 done."
8487   (interactive "p")
8488   (gnus-summary-next-thread (- n)))
8489
8490 (defun gnus-summary-go-down-thread ()
8491   "Go down one level in the current thread."
8492   (let ((children (gnus-summary-article-children)))
8493     (when children
8494       (gnus-summary-goto-subject (car children)))))
8495
8496 (defun gnus-summary-go-up-thread ()
8497   "Go up one level in the current thread."
8498   (let ((parent (gnus-summary-article-parent)))
8499     (when parent
8500       (gnus-summary-goto-subject parent))))
8501
8502 (defun gnus-summary-down-thread (n)
8503   "Go down thread N steps.
8504 If N is negative, go up instead.
8505 Returns the difference between N and how many steps down that were
8506 taken."
8507   (interactive "p")
8508   (let ((up (< n 0))
8509         (n (abs n)))
8510     (while (and (> n 0)
8511                 (if up (gnus-summary-go-up-thread)
8512                   (gnus-summary-go-down-thread)))
8513       (setq n (1- n)))
8514     (gnus-summary-position-point)
8515     (when (/= 0 n)
8516       (gnus-message 7 "Can't go further"))
8517     n))
8518
8519 (defun gnus-summary-up-thread (n)
8520   "Go up thread N steps.
8521 If N is negative, go up instead.
8522 Returns the difference between N and how many steps down that were
8523 taken."
8524   (interactive "p")
8525   (gnus-summary-down-thread (- n)))
8526
8527 (defun gnus-summary-top-thread ()
8528   "Go to the top of the thread."
8529   (interactive)
8530   (while (gnus-summary-go-up-thread))
8531   (gnus-summary-article-number))
8532
8533 (defun gnus-summary-kill-thread (&optional unmark)
8534   "Mark articles under current thread as read.
8535 If the prefix argument is positive, remove any kinds of marks.
8536 If the prefix argument is negative, tick articles instead."
8537   (interactive "P")
8538   (when unmark
8539     (setq unmark (prefix-numeric-value unmark)))
8540   (let ((articles (gnus-summary-articles-in-thread)))
8541     (save-excursion
8542       ;; Expand the thread.
8543       (gnus-summary-show-thread)
8544       ;; Mark all the articles.
8545       (while articles
8546         (gnus-summary-goto-subject (car articles))
8547         (cond ((null unmark)
8548                (gnus-summary-mark-article-as-read gnus-killed-mark))
8549               ((> unmark 0)
8550                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8551               (t
8552                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8553         (setq articles (cdr articles))))
8554     ;; Hide killed subtrees.
8555     (and (null unmark)
8556          gnus-thread-hide-killed
8557          (gnus-summary-hide-thread))
8558     ;; If marked as read, go to next unread subject.
8559     (when (null unmark)
8560       ;; Go to next unread subject.
8561       (gnus-summary-next-subject 1 t)))
8562   (gnus-set-mode-line 'summary))
8563
8564 ;; Summary sorting commands
8565
8566 (defun gnus-summary-sort-by-number (&optional reverse)
8567   "Sort the summary buffer by article number.
8568 Argument REVERSE means reverse order."
8569   (interactive "P")
8570   (gnus-summary-sort 'number reverse))
8571
8572 (defun gnus-summary-sort-by-author (&optional reverse)
8573   "Sort the summary buffer by author name alphabetically.
8574 If case-fold-search is non-nil, case of letters is ignored.
8575 Argument REVERSE means reverse order."
8576   (interactive "P")
8577   (gnus-summary-sort 'author reverse))
8578
8579 (defun gnus-summary-sort-by-subject (&optional reverse)
8580   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8581 If case-fold-search is non-nil, case of letters is ignored.
8582 Argument REVERSE means reverse order."
8583   (interactive "P")
8584   (gnus-summary-sort 'subject reverse))
8585
8586 (defun gnus-summary-sort-by-date (&optional reverse)
8587   "Sort the summary buffer by date.
8588 Argument REVERSE means reverse order."
8589   (interactive "P")
8590   (gnus-summary-sort 'date reverse))
8591
8592 (defun gnus-summary-sort-by-score (&optional reverse)
8593   "Sort the summary buffer by score.
8594 Argument REVERSE means reverse order."
8595   (interactive "P")
8596   (gnus-summary-sort 'score reverse))
8597
8598 (defun gnus-summary-sort-by-lines (&optional reverse)
8599   "Sort the summary buffer by article length.
8600 Argument REVERSE means reverse order."
8601   (interactive "P")
8602   (gnus-summary-sort 'lines reverse))
8603
8604 (defun gnus-summary-sort (predicate reverse)
8605   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8606   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8607          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8608          (gnus-thread-sort-functions
8609           (if (not reverse)
8610               thread
8611             `(lambda (t1 t2)
8612                (,thread t2 t1))))
8613          (gnus-article-sort-functions
8614           (if (not reverse)
8615               article
8616             `(lambda (t1 t2)
8617                (,article t2 t1))))
8618          (buffer-read-only)
8619          (gnus-summary-prepare-hook nil))
8620     ;; We do the sorting by regenerating the threads.
8621     (gnus-summary-prepare)
8622     ;; Hide subthreads if needed.
8623     (when (and gnus-show-threads gnus-thread-hide-subtree)
8624       (gnus-summary-hide-all-threads))))
8625
8626 ;; Summary saving commands.
8627
8628 (defun gnus-summary-save-article (&optional n not-saved)
8629   "Save the current article using the default saver function.
8630 If N is a positive number, save the N next articles.
8631 If N is a negative number, save the N previous articles.
8632 If N is nil and any articles have been marked with the process mark,
8633 save those articles instead.
8634 The variable `gnus-default-article-saver' specifies the saver function."
8635   (interactive "P")
8636   (let* ((articles (gnus-summary-work-articles n))
8637          (save-buffer (save-excursion
8638                         (nnheader-set-temp-buffer " *Gnus Save*")))
8639          (num (length articles))
8640          header article file)
8641     (while articles
8642       (setq header (gnus-summary-article-header
8643                     (setq article (pop articles))))
8644       (if (not (vectorp header))
8645           ;; This is a pseudo-article.
8646           (if (assq 'name header)
8647               (gnus-copy-file (cdr (assq 'name header)))
8648             (gnus-message 1 "Article %d is unsaveable" article))
8649         ;; This is a real article.
8650         (save-window-excursion
8651           (gnus-summary-select-article t nil nil article))
8652         (save-excursion
8653           (set-buffer save-buffer)
8654           (erase-buffer)
8655           (insert-buffer-substring gnus-original-article-buffer))
8656         (setq file (gnus-article-save save-buffer file num))
8657         (gnus-summary-remove-process-mark article)
8658         (unless not-saved
8659           (gnus-summary-set-saved-mark article))))
8660     (gnus-kill-buffer save-buffer)
8661     (gnus-summary-position-point)
8662     (gnus-set-mode-line 'summary)
8663     n))
8664
8665 (defun gnus-summary-pipe-output (&optional arg)
8666   "Pipe the current article to a subprocess.
8667 If N is a positive number, pipe the N next articles.
8668 If N is a negative number, pipe the N previous articles.
8669 If N is nil and any articles have been marked with the process mark,
8670 pipe those articles instead."
8671   (interactive "P")
8672   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8673     (gnus-summary-save-article arg t))
8674   (gnus-configure-windows 'pipe))
8675
8676 (defun gnus-summary-save-article-mail (&optional arg)
8677   "Append the current article to an mail file.
8678 If N is a positive number, save the N next articles.
8679 If N is a negative number, save the N previous articles.
8680 If N is nil and any articles have been marked with the process mark,
8681 save those articles instead."
8682   (interactive "P")
8683   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8684     (gnus-summary-save-article arg)))
8685
8686 (defun gnus-summary-save-article-rmail (&optional arg)
8687   "Append the current article to an rmail file.
8688 If N is a positive number, save the N next articles.
8689 If N is a negative number, save the N previous articles.
8690 If N is nil and any articles have been marked with the process mark,
8691 save those articles instead."
8692   (interactive "P")
8693   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8694     (gnus-summary-save-article arg)))
8695
8696 (defun gnus-summary-save-article-file (&optional arg)
8697   "Append the current article to a file.
8698 If N is a positive number, save the N next articles.
8699 If N is a negative number, save the N previous articles.
8700 If N is nil and any articles have been marked with the process mark,
8701 save those articles instead."
8702   (interactive "P")
8703   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8704     (gnus-summary-save-article arg)))
8705
8706 (defun gnus-summary-write-article-file (&optional arg)
8707   "Write the current article to a file, deleting the previous file.
8708 If N is a positive number, save the N next articles.
8709 If N is a negative number, save the N previous articles.
8710 If N is nil and any articles have been marked with the process mark,
8711 save those articles instead."
8712   (interactive "P")
8713   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8714     (gnus-summary-save-article arg)))
8715
8716 (defun gnus-summary-save-article-body-file (&optional arg)
8717   "Append the current article body to a file.
8718 If N is a positive number, save the N next articles.
8719 If N is a negative number, save the N previous articles.
8720 If N is nil and any articles have been marked with the process mark,
8721 save those articles instead."
8722   (interactive "P")
8723   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8724     (gnus-summary-save-article arg)))
8725
8726 (defun gnus-summary-pipe-message (program)
8727   "Pipe the current article through PROGRAM."
8728   (interactive "sProgram: ")
8729   (gnus-summary-select-article)
8730   (let ((mail-header-separator ""))
8731     (gnus-eval-in-buffer-window gnus-article-buffer
8732       (save-restriction
8733         (widen)
8734         (let ((start (window-start))
8735               buffer-read-only)
8736           (message-pipe-buffer-body program)
8737           (set-window-start (get-buffer-window (current-buffer)) start))))))
8738
8739 (defun gnus-get-split-value (methods)
8740   "Return a value based on the split METHODS."
8741   (let (split-name method result match)
8742     (when methods
8743       (save-excursion
8744         (set-buffer gnus-original-article-buffer)
8745         (save-restriction
8746           (nnheader-narrow-to-headers)
8747           (while methods
8748             (goto-char (point-min))
8749             (setq method (pop methods))
8750             (setq match (car method))
8751             (when (cond
8752                    ((stringp match)
8753                     ;; Regular expression.
8754                     (ignore-errors
8755                       (re-search-forward match nil t)))
8756                    ((gnus-functionp match)
8757                     ;; Function.
8758                     (save-restriction
8759                       (widen)
8760                       (setq result (funcall match gnus-newsgroup-name))))
8761                    ((consp match)
8762                     ;; Form.
8763                     (save-restriction
8764                       (widen)
8765                       (setq result (eval match)))))
8766               (setq split-name (append (cdr method) split-name))
8767               (cond ((stringp result)
8768                      (push (expand-file-name
8769                             result gnus-article-save-directory)
8770                            split-name))
8771                     ((consp result)
8772                      (setq split-name (append result split-name)))))))))
8773     split-name))
8774
8775 (defun gnus-valid-move-group-p (group)
8776   (and (boundp group)
8777        (symbol-name group)
8778        (symbol-value group)
8779        (memq 'respool
8780              (assoc (symbol-name
8781                      (car (gnus-find-method-for-group
8782                            (symbol-name group))))
8783                     gnus-valid-select-methods))))
8784
8785 (defun gnus-read-move-group-name (prompt default articles prefix)
8786   "Read a group name."
8787   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8788          (minibuffer-confirm-incomplete nil) ; XEmacs
8789          (prom
8790           (format "%s %s to:"
8791                   prompt
8792                   (if (> (length articles) 1)
8793                       (format "these %d articles" (length articles))
8794                     "this article")))
8795          (to-newsgroup
8796           (cond
8797            ((null split-name)
8798             (gnus-completing-read default prom
8799                                   gnus-active-hashtb
8800                                   'gnus-valid-move-group-p
8801                                   nil prefix
8802                                   'gnus-group-history))
8803            ((= 1 (length split-name))
8804             (gnus-completing-read (car split-name) prom
8805                                   gnus-active-hashtb
8806                                   'gnus-valid-move-group-p
8807                                   nil nil
8808                                   'gnus-group-history))
8809            (t
8810             (gnus-completing-read nil prom
8811                                   (mapcar (lambda (el) (list el))
8812                                           (nreverse split-name))
8813                                   nil nil nil
8814                                   'gnus-group-history)))))
8815     (when to-newsgroup
8816       (if (or (string= to-newsgroup "")
8817               (string= to-newsgroup prefix))
8818           (setq to-newsgroup default))
8819       (unless to-newsgroup
8820         (error "No group name entered"))
8821       (or (gnus-active to-newsgroup)
8822           (gnus-activate-group to-newsgroup)
8823           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8824                                      to-newsgroup))
8825               (or (and (gnus-request-create-group
8826                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8827                        (gnus-activate-group to-newsgroup nil nil
8828                                             (gnus-group-name-to-method
8829                                              to-newsgroup)))
8830                   (error "Couldn't create group %s" to-newsgroup)))
8831           (error "No such group: %s" to-newsgroup)))
8832     to-newsgroup))
8833
8834 ;; Summary extract commands
8835
8836 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8837   (let ((buffer-read-only nil)
8838         (article (gnus-summary-article-number))
8839         after-article b e)
8840     (unless (gnus-summary-goto-subject article)
8841       (error "No such article: %d" article))
8842     (gnus-summary-position-point)
8843     ;; If all commands are to be bunched up on one line, we collect
8844     ;; them here.
8845     (unless gnus-view-pseudos-separately
8846       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8847             files action)
8848         (while ps
8849           (setq action (cdr (assq 'action (car ps))))
8850           (setq files (list (cdr (assq 'name (car ps)))))
8851           (while (and ps (cdr ps)
8852                       (string= (or action "1")
8853                                (or (cdr (assq 'action (cadr ps))) "2")))
8854             (push (cdr (assq 'name (cadr ps))) files)
8855             (setcdr ps (cddr ps)))
8856           (when files
8857             (when (not (string-match "%s" action))
8858               (push " " files))
8859             (push " " files)
8860             (when (assq 'execute (car ps))
8861               (setcdr (assq 'execute (car ps))
8862                       (funcall (if (string-match "%s" action)
8863                                    'format 'concat)
8864                                action
8865                                (mapconcat
8866                                 (lambda (f)
8867                                   (if (equal f " ")
8868                                       f
8869                                     (mm-quote-arg f)))
8870                                 files " ")))))
8871           (setq ps (cdr ps)))))
8872     (if (and gnus-view-pseudos (not not-view))
8873         (while pslist
8874           (when (assq 'execute (car pslist))
8875             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8876                                   (eq gnus-view-pseudos 'not-confirm)))
8877           (setq pslist (cdr pslist)))
8878       (save-excursion
8879         (while pslist
8880           (setq after-article (or (cdr (assq 'article (car pslist)))
8881                                   (gnus-summary-article-number)))
8882           (gnus-summary-goto-subject after-article)
8883           (forward-line 1)
8884           (setq b (point))
8885           (insert "    " (file-name-nondirectory
8886                           (cdr (assq 'name (car pslist))))
8887                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8888           (setq e (point))
8889           (forward-line -1)             ; back to `b'
8890           (gnus-add-text-properties
8891            b (1- e) (list 'gnus-number gnus-reffed-article-number
8892                           gnus-mouse-face-prop gnus-mouse-face))
8893           (gnus-data-enter
8894            after-article gnus-reffed-article-number
8895            gnus-unread-mark b (car pslist) 0 (- e b))
8896           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8897           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8898           (setq pslist (cdr pslist)))))))
8899
8900 (defun gnus-pseudos< (p1 p2)
8901   (let ((c1 (cdr (assq 'action p1)))
8902         (c2 (cdr (assq 'action p2))))
8903     (and c1 c2 (string< c1 c2))))
8904
8905 (defun gnus-request-pseudo-article (props)
8906   (cond ((assq 'execute props)
8907          (gnus-execute-command (cdr (assq 'execute props)))))
8908   (let ((gnus-current-article (gnus-summary-article-number)))
8909     (gnus-run-hooks 'gnus-mark-article-hook)))
8910
8911 (defun gnus-execute-command (command &optional automatic)
8912   (save-excursion
8913     (gnus-article-setup-buffer)
8914     (set-buffer gnus-article-buffer)
8915     (setq buffer-read-only nil)
8916     (let ((command (if automatic command
8917                      (read-string "Command: " (cons command 0)))))
8918       (erase-buffer)
8919       (insert "$ " command "\n\n")
8920       (if gnus-view-pseudo-asynchronously
8921           (start-process "gnus-execute" (current-buffer) shell-file-name
8922                          shell-command-switch command)
8923         (call-process shell-file-name nil t nil
8924                       shell-command-switch command)))))
8925
8926 ;; Summary kill commands.
8927
8928 (defun gnus-summary-edit-global-kill (article)
8929   "Edit the \"global\" kill file."
8930   (interactive (list (gnus-summary-article-number)))
8931   (gnus-group-edit-global-kill article))
8932
8933 (defun gnus-summary-edit-local-kill ()
8934   "Edit a local kill file applied to the current newsgroup."
8935   (interactive)
8936   (setq gnus-current-headers (gnus-summary-article-header))
8937   (gnus-group-edit-local-kill
8938    (gnus-summary-article-number) gnus-newsgroup-name))
8939
8940 ;;; Header reading.
8941
8942 (defun gnus-read-header (id &optional header)
8943   "Read the headers of article ID and enter them into the Gnus system."
8944   (let ((group gnus-newsgroup-name)
8945         (gnus-override-method
8946          (and (gnus-news-group-p gnus-newsgroup-name)
8947               gnus-refer-article-method))
8948         where)
8949     ;; First we check to see whether the header in question is already
8950     ;; fetched.
8951     (if (stringp id)
8952         ;; This is a Message-ID.
8953         (setq header (or header (gnus-id-to-header id)))
8954       ;; This is an article number.
8955       (setq header (or header (gnus-summary-article-header id))))
8956     (if (and header
8957              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8958         ;; We have found the header.
8959         header
8960       ;; If this is a sparse article, we have to nix out its
8961       ;; previous entry in the thread hashtb.
8962       (when (and header
8963                  (gnus-summary-article-sparse-p (mail-header-number header)))
8964         (let* ((parent (gnus-parent-id (mail-header-references header)))
8965                (thread (and parent (gnus-id-to-thread parent))))
8966           (when thread
8967             (delq (assq header thread) thread))))
8968       ;; We have to really fetch the header to this article.
8969       (save-excursion
8970         (set-buffer nntp-server-buffer)
8971         (when (setq where (gnus-request-head id group))
8972           (nnheader-fold-continuation-lines)
8973           (goto-char (point-max))
8974           (insert ".\n")
8975           (goto-char (point-min))
8976           (insert "211 ")
8977           (princ (cond
8978                   ((numberp id) id)
8979                   ((cdr where) (cdr where))
8980                   (header (mail-header-number header))
8981                   (t gnus-reffed-article-number))
8982                  (current-buffer))
8983           (insert " Article retrieved.\n"))
8984         (if (or (not where)
8985                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8986             ()                          ; Malformed head.
8987           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8988             (when (and (stringp id)
8989                        (not (string= (gnus-group-real-name group)
8990                                      (car where))))
8991               ;; If we fetched by Message-ID and the article came
8992               ;; from a different group, we fudge some bogus article
8993               ;; numbers for this article.
8994               (mail-header-set-number header gnus-reffed-article-number))
8995             (save-excursion
8996               (set-buffer gnus-summary-buffer)
8997               (decf gnus-reffed-article-number)
8998               (gnus-remove-header (mail-header-number header))
8999               (push header gnus-newsgroup-headers)
9000               (setq gnus-current-headers header)
9001               (push (mail-header-number header) gnus-newsgroup-limit)))
9002           header)))))
9003
9004 (defun gnus-remove-header (number)
9005   "Remove header NUMBER from `gnus-newsgroup-headers'."
9006   (if (and gnus-newsgroup-headers
9007            (= number (mail-header-number (car gnus-newsgroup-headers))))
9008       (pop gnus-newsgroup-headers)
9009     (let ((headers gnus-newsgroup-headers))
9010       (while (and (cdr headers)
9011                   (not (= number (mail-header-number (cadr headers)))))
9012         (pop headers))
9013       (when (cdr headers)
9014         (setcdr headers (cddr headers))))))
9015
9016 ;;;
9017 ;;; summary highlights
9018 ;;;
9019
9020 (defun gnus-highlight-selected-summary ()
9021   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9022   ;; Highlight selected article in summary buffer
9023   (when gnus-summary-selected-face
9024     (save-excursion
9025       (let* ((beg (progn (beginning-of-line) (point)))
9026              (end (progn (end-of-line) (point)))
9027              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9028              (from (if (get-text-property beg gnus-mouse-face-prop)
9029                        beg
9030                      (or (next-single-property-change
9031                           beg gnus-mouse-face-prop nil end)
9032                          beg)))
9033              (to
9034               (if (= from end)
9035                   (- from 2)
9036                 (or (next-single-property-change
9037                      from gnus-mouse-face-prop nil end)
9038                     end))))
9039         ;; If no mouse-face prop on line we will have to = from = end,
9040         ;; so we highlight the entire line instead.
9041         (when (= (+ to 2) from)
9042           (setq from beg)
9043           (setq to end))
9044         (if gnus-newsgroup-selected-overlay
9045             ;; Move old overlay.
9046             (gnus-move-overlay
9047              gnus-newsgroup-selected-overlay from to (current-buffer))
9048           ;; Create new overlay.
9049           (gnus-overlay-put
9050            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9051            'face gnus-summary-selected-face))))))
9052
9053 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9054 (defun gnus-summary-highlight-line ()
9055   "Highlight current line according to `gnus-summary-highlight'."
9056   (let* ((list gnus-summary-highlight)
9057          (p (point))
9058          (end (progn (end-of-line) (point)))
9059          ;; now find out where the line starts and leave point there.
9060          (beg (progn (beginning-of-line) (point)))
9061          (article (gnus-summary-article-number))
9062          (score (or (cdr (assq (or article gnus-current-article)
9063                                gnus-newsgroup-scored))
9064                     gnus-summary-default-score 0))
9065          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9066          (inhibit-read-only t))
9067     ;; Eval the cars of the lists until we find a match.
9068     (let ((default gnus-summary-default-score))
9069       (while (and list
9070                   (not (eval (caar list))))
9071         (setq list (cdr list))))
9072     (let ((face (cdar list)))
9073       (unless (eq face (get-text-property beg 'face))
9074         (gnus-put-text-property-excluding-characters-with-faces
9075          beg end 'face
9076          (setq face (if (boundp face) (symbol-value face) face)))
9077         (when gnus-summary-highlight-line-function
9078           (funcall gnus-summary-highlight-line-function article face))))
9079     (goto-char p)))
9080
9081 (defun gnus-update-read-articles (group unread &optional compute)
9082   "Update the list of read articles in GROUP."
9083   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9084          (entry (gnus-gethash group gnus-newsrc-hashtb))
9085          (info (nth 2 entry))
9086          (prev 1)
9087          (unread (sort (copy-sequence unread) '<))
9088          read)
9089     (if (or (not info) (not active))
9090         ;; There is no info on this group if it was, in fact,
9091         ;; killed.  Gnus stores no information on killed groups, so
9092         ;; there's nothing to be done.
9093         ;; One could store the information somewhere temporarily,
9094         ;; perhaps...  Hmmm...
9095         ()
9096       ;; Remove any negative articles numbers.
9097       (while (and unread (< (car unread) 0))
9098         (setq unread (cdr unread)))
9099       ;; Remove any expired article numbers
9100       (while (and unread (< (car unread) (car active)))
9101         (setq unread (cdr unread)))
9102       ;; Compute the ranges of read articles by looking at the list of
9103       ;; unread articles.
9104       (while unread
9105         (when (/= (car unread) prev)
9106           (push (if (= prev (1- (car unread))) prev
9107                   (cons prev (1- (car unread))))
9108                 read))
9109         (setq prev (1+ (car unread)))
9110         (setq unread (cdr unread)))
9111       (when (<= prev (cdr active))
9112         (push (cons prev (cdr active)) read))
9113       (setq read (if (> (length read) 1) (nreverse read) read))
9114       (if compute
9115           read
9116         (save-excursion
9117           (set-buffer gnus-group-buffer)
9118           (gnus-undo-register
9119             `(progn
9120                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9121                (gnus-info-set-read ',info ',(gnus-info-read info))
9122                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9123                (gnus-group-update-group ,group t))))
9124         ;; Enter this list into the group info.
9125         (gnus-info-set-read info read)
9126         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9127         (gnus-get-unread-articles-in-group info (gnus-active group))
9128         t))))
9129
9130 (defun gnus-offer-save-summaries ()
9131   "Offer to save all active summary buffers."
9132   (save-excursion
9133     (let ((buflist (buffer-list))
9134           buffers bufname)
9135       ;; Go through all buffers and find all summaries.
9136       (while buflist
9137         (and (setq bufname (buffer-name (car buflist)))
9138              (string-match "Summary" bufname)
9139              (save-excursion
9140                (set-buffer bufname)
9141                ;; We check that this is, indeed, a summary buffer.
9142                (and (eq major-mode 'gnus-summary-mode)
9143                     ;; Also make sure this isn't bogus.
9144                     gnus-newsgroup-prepared
9145                     ;; Also make sure that this isn't a dead summary buffer.
9146                     (not gnus-dead-summary-mode)))
9147              (push bufname buffers))
9148         (setq buflist (cdr buflist)))
9149       ;; Go through all these summary buffers and offer to save them.
9150       (when buffers
9151         (map-y-or-n-p
9152          "Update summary buffer %s? "
9153          (lambda (buf)
9154            (switch-to-buffer buf)
9155            (gnus-summary-exit))
9156          buffers)))))
9157
9158 (defun gnus-newsgroup-setup-default-charset ()
9159   "Setup newsgroup default charset."
9160   (let ((name (and gnus-newsgroup-name 
9161                    (string-match "[^:]+$" gnus-newsgroup-name)
9162                    (match-string 0 gnus-newsgroup-name))))
9163     (setq gnus-newsgroup-default-charset
9164           (or (and gnus-newsgroup-name
9165                    (or (gnus-group-find-parameter 
9166                         gnus-newsgroup-name 'charset)
9167                        (let ((alist gnus-newsgroup-default-charset-alist) 
9168                              elem (charset nil))
9169                          (while alist
9170                            (if (and name
9171                                     (string-match 
9172                                      (car (setq elem (pop alist)))
9173                                      name))
9174                                (setq alist nil
9175                                      charset (cdr elem))))
9176                          charset)))
9177               rfc2047-default-charset))
9178     (setq gnus-newsgroup-iso-8859-1-forced 
9179           (and gnus-newsgroup-name
9180                (or (gnus-group-find-parameter
9181                     gnus-newsgroup-name 'iso-8859-1-forced)
9182                    (and name
9183                         (string-match  gnus-newsgroup-iso-8859-1-forced-regexp 
9184                                        name))))))
9185   (if (stringp gnus-newsgroup-default-charset)
9186       (setq gnus-newsgroup-default-charset
9187             (intern (downcase gnus-newsgroup-default-charset))))
9188   (setq gnus-newsgroup-iso-8859-1-forced
9189         (if (stringp gnus-newsgroup-iso-8859-1-forced)
9190             (intern (downcase gnus-newsgroup-iso-8859-1-forced))
9191           (and gnus-newsgroup-iso-8859-1-forced
9192                gnus-newsgroup-default-charset))))
9193   
9194 ;;;
9195 ;;; MIME Commands
9196 ;;;
9197
9198 (defun gnus-summary-display-buttonized ()
9199   "Display the current article buffer fully MIME-buttonized."
9200   (interactive)
9201   (require 'gnus-art)
9202   (let ((gnus-unbuttonized-mime-types nil))
9203     (gnus-summary-show-article)))
9204     
9205 (defun gnus-summary-toggle-display-buttonized ()
9206   "Toggle the buttonizing of the article buffer."
9207   (interactive)
9208   (require 'gnus-art)
9209   (if (setq gnus-inhibit-mime-unbuttonizing
9210             (not gnus-inhibit-mime-unbuttonizing))
9211       (let ((gnus-unbuttonized-mime-types nil))
9212         (gnus-summary-show-article))
9213     (gnus-summary-show-article)))
9214     
9215 (gnus-ems-redefine)
9216
9217 (provide 'gnus-sum)
9218
9219 (run-hooks 'gnus-sum-load-hook)
9220
9221 ;;; gnus-sum.el ends here