9a8a28c86b76995b0c6fe45edb786adf433d99c8
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 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 ? ;Whitespace
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 ? ;Whitespace
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-threading
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-group-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     ("^\\(cz\\|hun\\|pl\\|sk\\)\\>" iso-8859-2)
806     ("^israel\\>" iso-8859-1)
807     ("^han\\>" euc-kr)
808     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
809     (".*" iso-8859-1))
810   "Alist of regexps (to match group names) and default charsets to be used when reading."
811   :type '(repeat (list (regexp :tag "Group")
812                        (symbol :tag "Charset")))
813   :group 'gnus-charset)
814
815 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
816   "List of charsets that should be ignored.
817 When these charsets are used in the \"charset\" parameter, the
818 default charset will be used instead."
819   :type '(repeat symbol)
820   :group 'gnus-charset)
821
822 ;;; Internal variables
823
824 (defvar gnus-article-mime-handles nil)
825 (defvar gnus-article-decoded-p nil)
826 (defvar gnus-scores-exclude-files nil)
827 (defvar gnus-page-broken nil)
828 (defvar gnus-inhibit-mime-unbuttonizing nil)
829
830 (defvar gnus-original-article nil)
831 (defvar gnus-article-internal-prepare-hook nil)
832 (defvar gnus-newsgroup-process-stack nil)
833
834 (defvar gnus-thread-indent-array nil)
835 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
836
837 ;; Avoid highlighting in kill files.
838 (defvar gnus-summary-inhibit-highlight nil)
839 (defvar gnus-newsgroup-selected-overlay nil)
840 (defvar gnus-inhibit-limiting nil)
841 (defvar gnus-newsgroup-adaptive-score-file nil)
842 (defvar gnus-current-score-file nil)
843 (defvar gnus-current-move-group nil)
844 (defvar gnus-current-copy-group nil)
845 (defvar gnus-current-crosspost-group nil)
846
847 (defvar gnus-newsgroup-dependencies nil)
848 (defvar gnus-newsgroup-adaptive nil)
849 (defvar gnus-summary-display-article-function nil)
850 (defvar gnus-summary-highlight-line-function nil
851   "Function called after highlighting a summary line.")
852
853 (defvar gnus-summary-line-format-alist
854   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
855     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
856     (?s gnus-tmp-subject-or-nil ?s)
857     (?n gnus-tmp-name ?s)
858     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
859         ?s)
860     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
861             gnus-tmp-from) ?s)
862     (?F gnus-tmp-from ?s)
863     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
864     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
865     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
866     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
867     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
868     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
869     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
870     (?L gnus-tmp-lines ?d)
871     (?I gnus-tmp-indentation ?s)
872     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
873     (?R gnus-tmp-replied ?c)
874     (?\[ gnus-tmp-opening-bracket ?c)
875     (?\] gnus-tmp-closing-bracket ?c)
876     (?\> (make-string gnus-tmp-level ? ) ?s)
877     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
878     (?i gnus-tmp-score ?d)
879     (?z gnus-tmp-score-char ?c)
880     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
881     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
882     (?U gnus-tmp-unread ?c)
883     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
884     (?t (gnus-summary-number-of-articles-in-thread
885          (and (boundp 'thread) (car thread)) gnus-tmp-level)
886         ?d)
887     (?e (gnus-summary-number-of-articles-in-thread
888          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
889         ?c)
890     (?u gnus-tmp-user-defined ?s)
891     (?P (gnus-pick-line-number) ?d))
892   "An alist of format specifications that can appear in summary lines,
893 and what variables they correspond with, along with the type of the
894 variable (string, integer, character, etc).")
895
896 (defvar gnus-summary-dummy-line-format-alist
897   `((?S gnus-tmp-subject ?s)
898     (?N gnus-tmp-number ?d)
899     (?u gnus-tmp-user-defined ?s)))
900
901 (defvar gnus-summary-mode-line-format-alist
902   `((?G gnus-tmp-group-name ?s)
903     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
904     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
905     (?A gnus-tmp-article-number ?d)
906     (?Z gnus-tmp-unread-and-unselected ?s)
907     (?V gnus-version ?s)
908     (?U gnus-tmp-unread-and-unticked ?d)
909     (?S gnus-tmp-subject ?s)
910     (?e gnus-tmp-unselected ?d)
911     (?u gnus-tmp-user-defined ?s)
912     (?d (length gnus-newsgroup-dormant) ?d)
913     (?t (length gnus-newsgroup-marked) ?d)
914     (?r (length gnus-newsgroup-reads) ?d)
915     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
916     (?E gnus-newsgroup-expunged-tally ?d)
917     (?s (gnus-current-score-file-nondirectory) ?s)))
918
919 (defvar gnus-last-search-regexp nil
920   "Default regexp for article search command.")
921
922 (defvar gnus-last-shell-command nil
923   "Default shell command on article.")
924
925 (defvar gnus-newsgroup-begin nil)
926 (defvar gnus-newsgroup-end nil)
927 (defvar gnus-newsgroup-last-rmail nil)
928 (defvar gnus-newsgroup-last-mail nil)
929 (defvar gnus-newsgroup-last-folder nil)
930 (defvar gnus-newsgroup-last-file nil)
931 (defvar gnus-newsgroup-auto-expire nil)
932 (defvar gnus-newsgroup-active nil)
933
934 (defvar gnus-newsgroup-data nil)
935 (defvar gnus-newsgroup-data-reverse nil)
936 (defvar gnus-newsgroup-limit nil)
937 (defvar gnus-newsgroup-limits nil)
938
939 (defvar gnus-newsgroup-unreads nil
940   "List of unread articles in the current newsgroup.")
941
942 (defvar gnus-newsgroup-unselected nil
943   "List of unselected unread articles in the current newsgroup.")
944
945 (defvar gnus-newsgroup-reads nil
946   "Alist of read articles and article marks in the current newsgroup.")
947
948 (defvar gnus-newsgroup-expunged-tally nil)
949
950 (defvar gnus-newsgroup-marked nil
951   "List of ticked articles in the current newsgroup (a subset of unread art).")
952
953 (defvar gnus-newsgroup-killed nil
954   "List of ranges of articles that have been through the scoring process.")
955
956 (defvar gnus-newsgroup-cached nil
957   "List of articles that come from the article cache.")
958
959 (defvar gnus-newsgroup-saved nil
960   "List of articles that have been saved.")
961
962 (defvar gnus-newsgroup-kill-headers nil)
963
964 (defvar gnus-newsgroup-replied nil
965   "List of articles that have been replied to in the current newsgroup.")
966
967 (defvar gnus-newsgroup-expirable nil
968   "List of articles in the current newsgroup that can be expired.")
969
970 (defvar gnus-newsgroup-processable nil
971   "List of articles in the current newsgroup that can be processed.")
972
973 (defvar gnus-newsgroup-downloadable nil
974   "List of articles in the current newsgroup that can be processed.")
975
976 (defvar gnus-newsgroup-undownloaded nil
977   "List of articles in the current newsgroup that haven't been downloaded..")
978
979 (defvar gnus-newsgroup-unsendable nil
980   "List of articles in the current newsgroup that won't be sent.")
981
982 (defvar gnus-newsgroup-bookmarks nil
983   "List of articles in the current newsgroup that have bookmarks.")
984
985 (defvar gnus-newsgroup-dormant nil
986   "List of dormant articles in the current newsgroup.")
987
988 (defvar gnus-newsgroup-scored nil
989   "List of scored articles in the current newsgroup.")
990
991 (defvar gnus-newsgroup-headers nil
992   "List of article headers in the current newsgroup.")
993
994 (defvar gnus-newsgroup-threads nil)
995
996 (defvar gnus-newsgroup-prepared nil
997   "Whether the current group has been prepared properly.")
998
999 (defvar gnus-newsgroup-ancient nil
1000   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-sparse nil)
1003
1004 (defvar gnus-current-article nil)
1005 (defvar gnus-article-current nil)
1006 (defvar gnus-current-headers nil)
1007 (defvar gnus-have-all-headers nil)
1008 (defvar gnus-last-article nil)
1009 (defvar gnus-newsgroup-history nil)
1010 (defvar gnus-newsgroup-charset nil)
1011
1012 (defconst gnus-summary-local-variables
1013   '(gnus-newsgroup-name
1014     gnus-newsgroup-begin gnus-newsgroup-end
1015     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1016     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1017     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1018     gnus-newsgroup-unselected gnus-newsgroup-marked
1019     gnus-newsgroup-reads gnus-newsgroup-saved
1020     gnus-newsgroup-replied gnus-newsgroup-expirable
1021     gnus-newsgroup-processable gnus-newsgroup-killed
1022     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1023     gnus-newsgroup-unsendable
1024     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1025     gnus-newsgroup-headers gnus-newsgroup-threads
1026     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1027     gnus-current-article gnus-current-headers gnus-have-all-headers
1028     gnus-last-article gnus-article-internal-prepare-hook
1029     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1030     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1031     gnus-thread-expunge-below
1032     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1033     (gnus-summary-mark-below . global)
1034     gnus-newsgroup-active gnus-scores-exclude-files
1035     gnus-newsgroup-history gnus-newsgroup-ancient
1036     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1037     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1038     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1039     (gnus-newsgroup-expunged-tally . 0)
1040     gnus-cache-removable-articles gnus-newsgroup-cached
1041     gnus-newsgroup-data gnus-newsgroup-data-reverse
1042     gnus-newsgroup-limit gnus-newsgroup-limits
1043     gnus-newsgroup-charset)
1044   "Variables that are buffer-local to the summary buffers.")
1045
1046 ;; Byte-compiler warning.
1047 (defvar gnus-article-mode-map)
1048
1049 ;; MIME stuff.
1050
1051 (defvar gnus-decode-encoded-word-methods
1052   '(mail-decode-encoded-word-string)
1053   "List of methods used to decode encoded words.
1054
1055 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1056 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1057 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1058 whose names match REGEXP.
1059
1060 For example:
1061 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1062  mail-decode-encoded-word-string
1063  (\"chinese\" . rfc1843-decode-string))
1064 ")
1065
1066 (defvar gnus-decode-encoded-word-methods-cache nil)
1067
1068 (defun gnus-multi-decode-encoded-word-string (string)
1069   "Apply the functions from `gnus-encoded-word-methods' that match."
1070   (unless (and gnus-decode-encoded-word-methods-cache
1071                (eq gnus-newsgroup-name
1072                    (car gnus-decode-encoded-word-methods-cache)))
1073     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1074     (mapc '(lambda (x)
1075              (if (symbolp x)
1076                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1077                (if (and gnus-newsgroup-name
1078                         (string-match (car x) gnus-newsgroup-name))
1079                    (nconc gnus-decode-encoded-word-methods-cache
1080                           (list (cdr x))))))
1081           gnus-decode-encoded-word-methods))
1082   (let ((xlist gnus-decode-encoded-word-methods-cache))
1083     (pop xlist)
1084     (while xlist
1085       (setq string (funcall (pop xlist) string))))
1086   string)
1087
1088 ;; Subject simplification.
1089
1090 (defun gnus-simplify-whitespace (str)
1091   "Remove excessive whitespace."
1092   (let ((mystr str))
1093     ;; Multiple spaces.
1094     (while (string-match "[ \t][ \t]+" mystr)
1095       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1096                           " "
1097                           (substring mystr (match-end 0)))))
1098     ;; Leading spaces.
1099     (when (string-match "^[ \t]+" mystr)
1100       (setq mystr (substring mystr (match-end 0))))
1101     ;; Trailing spaces.
1102     (when (string-match "[ \t]+$" mystr)
1103       (setq mystr (substring mystr 0 (match-beginning 0))))
1104     mystr))
1105
1106 (defsubst gnus-simplify-subject-re (subject)
1107   "Remove \"Re:\" from subject lines."
1108   (if (string-match "^[Rr][Ee]: *" subject)
1109       (substring subject (match-end 0))
1110     subject))
1111
1112 (defun gnus-simplify-subject (subject &optional re-only)
1113   "Remove `Re:' and words in parentheses.
1114 If RE-ONLY is non-nil, strip leading `Re:'s only."
1115   (let ((case-fold-search t))           ;Ignore case.
1116     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1117     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1118       (setq subject (substring subject (match-end 0))))
1119     ;; Remove uninteresting prefixes.
1120     (when (and (not re-only)
1121                gnus-simplify-ignored-prefixes
1122                (string-match gnus-simplify-ignored-prefixes subject))
1123       (setq subject (substring subject (match-end 0))))
1124     ;; Remove words in parentheses from end.
1125     (unless re-only
1126       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1127         (setq subject (substring subject 0 (match-beginning 0)))))
1128     ;; Return subject string.
1129     subject))
1130
1131 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1132 ;; all whitespace.
1133 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1134   (goto-char (point-min))
1135   (while (re-search-forward regexp nil t)
1136       (replace-match (or newtext ""))))
1137
1138 (defun gnus-simplify-buffer-fuzzy ()
1139   "Simplify string in the buffer fuzzily.
1140 The string in the accessible portion of the current buffer is simplified.
1141 It is assumed to be a single-line subject.
1142 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1143 matter is removed.  Additional things can be deleted by setting
1144 gnus-simplify-subject-fuzzy-regexp."
1145   (let ((case-fold-search t)
1146         (modified-tick))
1147     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1148
1149     (while (not (eq modified-tick (buffer-modified-tick)))
1150       (setq modified-tick (buffer-modified-tick))
1151       (cond
1152        ((listp gnus-simplify-subject-fuzzy-regexp)
1153         (mapcar 'gnus-simplify-buffer-fuzzy-step
1154                 gnus-simplify-subject-fuzzy-regexp))
1155        (gnus-simplify-subject-fuzzy-regexp
1156         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1157       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1158       (gnus-simplify-buffer-fuzzy-step
1159        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1160       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1161
1162     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1163     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1164     (gnus-simplify-buffer-fuzzy-step " $")
1165     (gnus-simplify-buffer-fuzzy-step "^ +")))
1166
1167 (defun gnus-simplify-subject-fuzzy (subject)
1168   "Simplify a subject string fuzzily.
1169 See `gnus-simplify-buffer-fuzzy' for details."
1170   (save-excursion
1171     (gnus-set-work-buffer)
1172     (let ((case-fold-search t))
1173       ;; Remove uninteresting prefixes.
1174       (when (and gnus-simplify-ignored-prefixes
1175                  (string-match gnus-simplify-ignored-prefixes subject))
1176         (setq subject (substring subject (match-end 0))))
1177       (insert subject)
1178       (inline (gnus-simplify-buffer-fuzzy))
1179       (buffer-string))))
1180
1181 (defsubst gnus-simplify-subject-fully (subject)
1182   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1183   (cond
1184    (gnus-simplify-subject-functions
1185     (gnus-map-function gnus-simplify-subject-functions subject))
1186    ((null gnus-summary-gather-subject-limit)
1187     (gnus-simplify-subject-re subject))
1188    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1189     (gnus-simplify-subject-fuzzy subject))
1190    ((numberp gnus-summary-gather-subject-limit)
1191     (gnus-limit-string (gnus-simplify-subject-re subject)
1192                        gnus-summary-gather-subject-limit))
1193    (t
1194     subject)))
1195
1196 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1197   "Check whether two subjects are equal.
1198 If optional argument simple-first is t, first argument is already
1199 simplified."
1200   (cond
1201    ((null simple-first)
1202     (equal (gnus-simplify-subject-fully s1)
1203            (gnus-simplify-subject-fully s2)))
1204    (t
1205     (equal s1
1206            (gnus-simplify-subject-fully s2)))))
1207
1208 (defun gnus-summary-bubble-group ()
1209   "Increase the score of the current group.
1210 This is a handy function to add to `gnus-summary-exit-hook' to
1211 increase the score of each group you read."
1212   (gnus-group-add-score gnus-newsgroup-name))
1213
1214 \f
1215 ;;;
1216 ;;; Gnus summary mode
1217 ;;;
1218
1219 (put 'gnus-summary-mode 'mode-class 'special)
1220
1221 (when t
1222   ;; Non-orthogonal keys
1223
1224   (gnus-define-keys gnus-summary-mode-map
1225     " " gnus-summary-next-page
1226     "\177" gnus-summary-prev-page
1227     [delete] gnus-summary-prev-page
1228     [backspace] gnus-summary-prev-page
1229     "\r" gnus-summary-scroll-up
1230     "\M-\r" gnus-summary-scroll-down
1231     "n" gnus-summary-next-unread-article
1232     "p" gnus-summary-prev-unread-article
1233     "N" gnus-summary-next-article
1234     "P" gnus-summary-prev-article
1235     "\M-\C-n" gnus-summary-next-same-subject
1236     "\M-\C-p" gnus-summary-prev-same-subject
1237     "\M-n" gnus-summary-next-unread-subject
1238     "\M-p" gnus-summary-prev-unread-subject
1239     "." gnus-summary-first-unread-article
1240     "," gnus-summary-best-unread-article
1241     "\M-s" gnus-summary-search-article-forward
1242     "\M-r" gnus-summary-search-article-backward
1243     "<" gnus-summary-beginning-of-article
1244     ">" gnus-summary-end-of-article
1245     "j" gnus-summary-goto-article
1246     "^" gnus-summary-refer-parent-article
1247     "\M-^" gnus-summary-refer-article
1248     "u" gnus-summary-tick-article-forward
1249     "!" gnus-summary-tick-article-forward
1250     "U" gnus-summary-tick-article-backward
1251     "d" gnus-summary-mark-as-read-forward
1252     "D" gnus-summary-mark-as-read-backward
1253     "E" gnus-summary-mark-as-expirable
1254     "\M-u" gnus-summary-clear-mark-forward
1255     "\M-U" gnus-summary-clear-mark-backward
1256     "k" gnus-summary-kill-same-subject-and-select
1257     "\C-k" gnus-summary-kill-same-subject
1258     "\M-\C-k" gnus-summary-kill-thread
1259     "\M-\C-l" gnus-summary-lower-thread
1260     "e" gnus-summary-edit-article
1261     "#" gnus-summary-mark-as-processable
1262     "\M-#" gnus-summary-unmark-as-processable
1263     "\M-\C-t" gnus-summary-toggle-threads
1264     "\M-\C-s" gnus-summary-show-thread
1265     "\M-\C-h" gnus-summary-hide-thread
1266     "\M-\C-f" gnus-summary-next-thread
1267     "\M-\C-b" gnus-summary-prev-thread
1268     "\M-\C-u" gnus-summary-up-thread
1269     "\M-\C-d" gnus-summary-down-thread
1270     "&" gnus-summary-execute-command
1271     "c" gnus-summary-catchup-and-exit
1272     "\C-w" gnus-summary-mark-region-as-read
1273     "\C-t" gnus-summary-toggle-truncation
1274     "?" gnus-summary-mark-as-dormant
1275     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1276     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1277     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1278     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1279     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1280     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1281     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1282     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1283     "=" gnus-summary-expand-window
1284     "\C-x\C-s" gnus-summary-reselect-current-group
1285     "\M-g" gnus-summary-rescan-group
1286     "w" gnus-summary-stop-page-breaking
1287     "\C-c\C-r" gnus-summary-caesar-message
1288     "f" gnus-summary-followup
1289     "F" gnus-summary-followup-with-original
1290     "C" gnus-summary-cancel-article
1291     "r" gnus-summary-reply
1292     "R" gnus-summary-reply-with-original
1293     "\C-c\C-f" gnus-summary-mail-forward
1294     "o" gnus-summary-save-article
1295     "\C-o" gnus-summary-save-article-mail
1296     "|" gnus-summary-pipe-output
1297     "\M-k" gnus-summary-edit-local-kill
1298     "\M-K" gnus-summary-edit-global-kill
1299     ;; "V" gnus-version
1300     "\C-c\C-d" gnus-summary-describe-group
1301     "q" gnus-summary-exit
1302     "Q" gnus-summary-exit-no-update
1303     "\C-c\C-i" gnus-info-find-node
1304     gnus-mouse-2 gnus-mouse-pick-article
1305     "m" gnus-summary-mail-other-window
1306     "a" gnus-summary-post-news
1307     "x" gnus-summary-limit-to-unread
1308     "s" gnus-summary-isearch-article
1309     "t" gnus-summary-toggle-header
1310     "g" gnus-summary-show-article
1311     "l" gnus-summary-goto-last-article
1312     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1313     "\C-d" gnus-summary-enter-digest-group
1314     "\M-\C-d" gnus-summary-read-document
1315     "\M-\C-e" gnus-summary-edit-parameters
1316     "\M-\C-g" gnus-summary-customize-parameters
1317     "\C-c\C-b" gnus-bug
1318     "*" gnus-cache-enter-article
1319     "\M-*" gnus-cache-remove-article
1320     "\M-&" gnus-summary-universal-argument
1321     "\C-l" gnus-recenter
1322     "I" gnus-summary-increase-score
1323     "L" gnus-summary-lower-score
1324     "\M-i" gnus-symbolic-argument
1325     "h" gnus-summary-select-article-buffer
1326
1327     "b" gnus-article-view-part
1328     "\M-t" gnus-summary-toggle-display-buttonized
1329
1330     "V" gnus-summary-score-map
1331     "X" gnus-uu-extract-map
1332     "S" gnus-summary-send-map)
1333
1334   ;; Sort of orthogonal keymap
1335   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1336     "t" gnus-summary-tick-article-forward
1337     "!" gnus-summary-tick-article-forward
1338     "d" gnus-summary-mark-as-read-forward
1339     "r" gnus-summary-mark-as-read-forward
1340     "c" gnus-summary-clear-mark-forward
1341     " " gnus-summary-clear-mark-forward
1342     "e" gnus-summary-mark-as-expirable
1343     "x" gnus-summary-mark-as-expirable
1344     "?" gnus-summary-mark-as-dormant
1345     "b" gnus-summary-set-bookmark
1346     "B" gnus-summary-remove-bookmark
1347     "#" gnus-summary-mark-as-processable
1348     "\M-#" gnus-summary-unmark-as-processable
1349     "S" gnus-summary-limit-include-expunged
1350     "C" gnus-summary-catchup
1351     "H" gnus-summary-catchup-to-here
1352     "\C-c" gnus-summary-catchup-all
1353     "k" gnus-summary-kill-same-subject-and-select
1354     "K" gnus-summary-kill-same-subject
1355     "P" gnus-uu-mark-map)
1356
1357   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1358     "c" gnus-summary-clear-above
1359     "u" gnus-summary-tick-above
1360     "m" gnus-summary-mark-above
1361     "k" gnus-summary-kill-below)
1362
1363   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1364     "/" gnus-summary-limit-to-subject
1365     "n" gnus-summary-limit-to-articles
1366     "w" gnus-summary-pop-limit
1367     "s" gnus-summary-limit-to-subject
1368     "a" gnus-summary-limit-to-author
1369     "u" gnus-summary-limit-to-unread
1370     "m" gnus-summary-limit-to-marks
1371     "M" gnus-summary-limit-exclude-marks
1372     "v" gnus-summary-limit-to-score
1373     "*" gnus-summary-limit-include-cached
1374     "D" gnus-summary-limit-include-dormant
1375     "T" gnus-summary-limit-include-thread
1376     "d" gnus-summary-limit-exclude-dormant
1377     "t" gnus-summary-limit-to-age
1378     "E" gnus-summary-limit-include-expunged
1379     "c" gnus-summary-limit-exclude-childless-dormant
1380     "C" gnus-summary-limit-mark-excluded-as-read)
1381
1382   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1383     "n" gnus-summary-next-unread-article
1384     "p" gnus-summary-prev-unread-article
1385     "N" gnus-summary-next-article
1386     "P" gnus-summary-prev-article
1387     "\C-n" gnus-summary-next-same-subject
1388     "\C-p" gnus-summary-prev-same-subject
1389     "\M-n" gnus-summary-next-unread-subject
1390     "\M-p" gnus-summary-prev-unread-subject
1391     "f" gnus-summary-first-unread-article
1392     "b" gnus-summary-best-unread-article
1393     "j" gnus-summary-goto-article
1394     "g" gnus-summary-goto-subject
1395     "l" gnus-summary-goto-last-article
1396     "o" gnus-summary-pop-article)
1397
1398   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1399     "k" gnus-summary-kill-thread
1400     "l" gnus-summary-lower-thread
1401     "i" gnus-summary-raise-thread
1402     "T" gnus-summary-toggle-threads
1403     "t" gnus-summary-rethread-current
1404     "^" gnus-summary-reparent-thread
1405     "s" gnus-summary-show-thread
1406     "S" gnus-summary-show-all-threads
1407     "h" gnus-summary-hide-thread
1408     "H" gnus-summary-hide-all-threads
1409     "n" gnus-summary-next-thread
1410     "p" gnus-summary-prev-thread
1411     "u" gnus-summary-up-thread
1412     "o" gnus-summary-top-thread
1413     "d" gnus-summary-down-thread
1414     "#" gnus-uu-mark-thread
1415     "\M-#" gnus-uu-unmark-thread)
1416
1417   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1418     "g" gnus-summary-prepare
1419     "c" gnus-summary-insert-cached-articles)
1420
1421   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1422     "c" gnus-summary-catchup-and-exit
1423     "C" gnus-summary-catchup-all-and-exit
1424     "E" gnus-summary-exit-no-update
1425     "Q" gnus-summary-exit
1426     "Z" gnus-summary-exit
1427     "n" gnus-summary-catchup-and-goto-next-group
1428     "R" gnus-summary-reselect-current-group
1429     "G" gnus-summary-rescan-group
1430     "N" gnus-summary-next-group
1431     "s" gnus-summary-save-newsrc
1432     "P" gnus-summary-prev-group)
1433
1434   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1435     " " gnus-summary-next-page
1436     "n" gnus-summary-next-page
1437     "\177" gnus-summary-prev-page
1438     [delete] gnus-summary-prev-page
1439     "p" gnus-summary-prev-page
1440     "\r" gnus-summary-scroll-up
1441     "\M-\r" gnus-summary-scroll-down
1442     "<" gnus-summary-beginning-of-article
1443     ">" gnus-summary-end-of-article
1444     "b" gnus-summary-beginning-of-article
1445     "e" gnus-summary-end-of-article
1446     "^" gnus-summary-refer-parent-article
1447     "r" gnus-summary-refer-parent-article
1448     "R" gnus-summary-refer-references
1449     "T" gnus-summary-refer-thread
1450     "g" gnus-summary-show-article
1451     "s" gnus-summary-isearch-article
1452     "P" gnus-summary-print-article)
1453
1454   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1455     "b" gnus-article-add-buttons
1456     "B" gnus-article-add-buttons-to-head
1457     "o" gnus-article-treat-overstrike
1458     "e" gnus-article-emphasize
1459     "w" gnus-article-fill-cited-article
1460     "Q" gnus-article-fill-long-lines
1461     "C" gnus-article-capitalize-sentences
1462     "c" gnus-article-remove-cr
1463     "q" gnus-article-de-quoted-unreadable
1464     "f" gnus-article-display-x-face
1465     "l" gnus-summary-stop-page-breaking
1466     "r" gnus-summary-caesar-message
1467     "t" gnus-article-hide-headers
1468     "v" gnus-summary-verbose-headers
1469     "h" gnus-article-treat-html
1470     "d" gnus-article-treat-dumbquotes)
1471
1472   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1473     "a" gnus-article-hide
1474     "h" gnus-article-hide-headers
1475     "b" gnus-article-hide-boring-headers
1476     "s" gnus-article-hide-signature
1477     "c" gnus-article-hide-citation
1478     "C" gnus-article-hide-citation-in-followups
1479     "p" gnus-article-hide-pgp
1480     "B" gnus-article-strip-banner
1481     "P" gnus-article-hide-pem
1482     "\C-c" gnus-article-hide-citation-maybe)
1483
1484   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1485     "a" gnus-article-highlight
1486     "h" gnus-article-highlight-headers
1487     "c" gnus-article-highlight-citation
1488     "s" gnus-article-highlight-signature)
1489
1490   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1491     "w" gnus-article-decode-mime-words
1492     "c" gnus-article-decode-charset
1493     "v" gnus-mime-view-all-parts
1494     "b" gnus-article-view-part)
1495
1496   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1497     "z" gnus-article-date-ut
1498     "u" gnus-article-date-ut
1499     "l" gnus-article-date-local
1500     "e" gnus-article-date-lapsed
1501     "o" gnus-article-date-original
1502     "i" gnus-article-date-iso8601
1503     "s" gnus-article-date-user)
1504
1505   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1506     "t" gnus-article-remove-trailing-blank-lines
1507     "l" gnus-article-strip-leading-blank-lines
1508     "m" gnus-article-strip-multiple-blank-lines
1509     "a" gnus-article-strip-blank-lines
1510     "A" gnus-article-strip-all-blank-lines
1511     "s" gnus-article-strip-leading-space
1512     "e" gnus-article-strip-trailing-space)
1513
1514   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1515     "v" gnus-version
1516     "f" gnus-summary-fetch-faq
1517     "d" gnus-summary-describe-group
1518     "h" gnus-summary-describe-briefly
1519     "i" gnus-info-find-node)
1520
1521   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1522     "e" gnus-summary-expire-articles
1523     "\M-\C-e" gnus-summary-expire-articles-now
1524     "\177" gnus-summary-delete-article
1525     [delete] gnus-summary-delete-article
1526     "m" gnus-summary-move-article
1527     "r" gnus-summary-respool-article
1528     "w" gnus-summary-edit-article
1529     "c" gnus-summary-copy-article
1530     "B" gnus-summary-crosspost-article
1531     "q" gnus-summary-respool-query
1532     "t" gnus-summary-respool-trace
1533     "i" gnus-summary-import-article
1534     "p" gnus-summary-article-posted-p)
1535
1536   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1537     "o" gnus-summary-save-article
1538     "m" gnus-summary-save-article-mail
1539     "F" gnus-summary-write-article-file
1540     "r" gnus-summary-save-article-rmail
1541     "f" gnus-summary-save-article-file
1542     "b" gnus-summary-save-article-body-file
1543     "h" gnus-summary-save-article-folder
1544     "v" gnus-summary-save-article-vm
1545     "p" gnus-summary-pipe-output
1546     "s" gnus-soup-add-article)
1547
1548   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1549     "b" gnus-summary-display-buttonized
1550     "m" gnus-summary-repair-multipart
1551     "v" gnus-article-view-part
1552     "o" gnus-article-save-part
1553     "c" gnus-article-copy-part
1554     "e" gnus-article-externalize-part
1555     "i" gnus-article-inline-part
1556     "|" gnus-article-pipe-part)
1557   )
1558
1559 (defun gnus-summary-make-menu-bar ()
1560   (gnus-turn-off-edit-menu 'summary)
1561
1562   (unless (boundp 'gnus-summary-misc-menu)
1563
1564     (easy-menu-define
1565      gnus-summary-kill-menu gnus-summary-mode-map ""
1566      (cons
1567       "Score"
1568       (nconc
1569        (list
1570         ["Enter score..." gnus-summary-score-entry t]
1571         ["Customize" gnus-score-customize t])
1572        (gnus-make-score-map 'increase)
1573        (gnus-make-score-map 'lower)
1574        '(("Mark"
1575           ["Kill below" gnus-summary-kill-below t]
1576           ["Mark above" gnus-summary-mark-above t]
1577           ["Tick above" gnus-summary-tick-above t]
1578           ["Clear above" gnus-summary-clear-above t])
1579          ["Current score" gnus-summary-current-score t]
1580          ["Set score" gnus-summary-set-score t]
1581          ["Switch current score file..." gnus-score-change-score-file t]
1582          ["Set mark below..." gnus-score-set-mark-below t]
1583          ["Set expunge below..." gnus-score-set-expunge-below t]
1584          ["Edit current score file" gnus-score-edit-current-scores t]
1585          ["Edit score file" gnus-score-edit-file t]
1586          ["Trace score" gnus-score-find-trace t]
1587          ["Find words" gnus-score-find-favourite-words t]
1588          ["Rescore buffer" gnus-summary-rescore t]
1589          ["Increase score..." gnus-summary-increase-score t]
1590          ["Lower score..." gnus-summary-lower-score t]))))
1591
1592     ;; Define both the Article menu in the summary buffer and the equivalent
1593     ;; Commands menu in the article buffer here for consistency.
1594     (let ((innards
1595            '(("Hide"
1596               ["All" gnus-article-hide t]
1597               ["Headers" gnus-article-hide-headers t]
1598               ["Signature" gnus-article-hide-signature t]
1599               ["Citation" gnus-article-hide-citation t]
1600               ["PGP" gnus-article-hide-pgp t]
1601               ["Banner" gnus-article-strip-banner t]
1602               ["Boring headers" gnus-article-hide-boring-headers t])
1603              ("Highlight"
1604               ["All" gnus-article-highlight t]
1605               ["Headers" gnus-article-highlight-headers t]
1606               ["Signature" gnus-article-highlight-signature t]
1607               ["Citation" gnus-article-highlight-citation t])
1608              ("MIME"
1609               ["Words" gnus-article-decode-mime-words t]
1610               ["Charset" gnus-article-decode-charset t]
1611               ["QP" gnus-article-de-quoted-unreadable t]
1612               ["View all" gnus-mime-view-all-parts t])
1613              ("Date"
1614               ["Local" gnus-article-date-local t]
1615               ["ISO8601" gnus-article-date-iso8601 t]
1616               ["UT" gnus-article-date-ut t]
1617               ["Original" gnus-article-date-original t]
1618               ["Lapsed" gnus-article-date-lapsed t]
1619               ["User-defined" gnus-article-date-user t])
1620              ("Washing"
1621               ("Remove Blanks"
1622                ["Leading" gnus-article-strip-leading-blank-lines t]
1623                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1624                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1625                ["All of the above" gnus-article-strip-blank-lines t]
1626                ["All" gnus-article-strip-all-blank-lines t]
1627                ["Leading space" gnus-article-strip-leading-space t]
1628                ["Trailing space" gnus-article-strip-trailing-space t])
1629               ["Overstrike" gnus-article-treat-overstrike t]
1630               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1631               ["Emphasis" gnus-article-emphasize t]
1632               ["Word wrap" gnus-article-fill-cited-article t]
1633               ["Fill long lines" gnus-article-fill-long-lines t]
1634               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1635               ["CR" gnus-article-remove-cr t]
1636               ["Show X-Face" gnus-article-display-x-face t]
1637               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1638               ["UnHTMLize" gnus-article-treat-html t]
1639               ["Rot 13" gnus-summary-caesar-message t]
1640               ["Unix pipe" gnus-summary-pipe-message t]
1641               ["Add buttons" gnus-article-add-buttons t]
1642               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1643               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1644               ["Verbose header" gnus-summary-verbose-headers t]
1645               ["Toggle header" gnus-summary-toggle-header t])
1646              ("Output"
1647               ["Save in default format" gnus-summary-save-article t]
1648               ["Save in file" gnus-summary-save-article-file t]
1649               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1650               ["Save in MH folder" gnus-summary-save-article-folder t]
1651               ["Save in VM folder" gnus-summary-save-article-vm t]
1652               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1653               ["Save body in file" gnus-summary-save-article-body-file t]
1654               ["Pipe through a filter" gnus-summary-pipe-output t]
1655               ["Add to SOUP packet" gnus-soup-add-article t]
1656               ["Print" gnus-summary-print-article t])
1657              ("Backend"
1658               ["Respool article..." gnus-summary-respool-article t]
1659               ["Move article..." gnus-summary-move-article
1660                (gnus-check-backend-function
1661                 'request-move-article gnus-newsgroup-name)]
1662               ["Copy article..." gnus-summary-copy-article t]
1663               ["Crosspost article..." gnus-summary-crosspost-article
1664                (gnus-check-backend-function
1665                 'request-replace-article gnus-newsgroup-name)]
1666               ["Import file..." gnus-summary-import-article t]
1667               ["Check if posted" gnus-summary-article-posted-p t]
1668               ["Edit article" gnus-summary-edit-article
1669                (not (gnus-group-read-only-p))]
1670               ["Delete article" gnus-summary-delete-article
1671                (gnus-check-backend-function
1672                 'request-expire-articles gnus-newsgroup-name)]
1673               ["Query respool" gnus-summary-respool-query t]
1674               ["Trace respool" gnus-summary-respool-trace t]
1675               ["Delete expirable articles" gnus-summary-expire-articles-now
1676                (gnus-check-backend-function
1677                 'request-expire-articles gnus-newsgroup-name)])
1678              ("Extract"
1679               ["Uudecode" gnus-uu-decode-uu t]
1680               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1681               ["Unshar" gnus-uu-decode-unshar t]
1682               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1683               ["Save" gnus-uu-decode-save t]
1684               ["Binhex" gnus-uu-decode-binhex t]
1685               ["Postscript" gnus-uu-decode-postscript t])
1686              ("Cache"
1687               ["Enter article" gnus-cache-enter-article t]
1688               ["Remove article" gnus-cache-remove-article t])
1689              ["Select article buffer" gnus-summary-select-article-buffer t]
1690              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1691              ["Isearch article..." gnus-summary-isearch-article t]
1692              ["Beginning of the article" gnus-summary-beginning-of-article t]
1693              ["End of the article" gnus-summary-end-of-article t]
1694              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1695              ["Fetch referenced articles" gnus-summary-refer-references t]
1696              ["Fetch current thread" gnus-summary-refer-thread t]
1697              ["Fetch article with id..." gnus-summary-refer-article t]
1698              ["Redisplay" gnus-summary-show-article t])))
1699       (easy-menu-define
1700        gnus-summary-article-menu gnus-summary-mode-map ""
1701        (cons "Article" innards))
1702
1703       (easy-menu-define
1704        gnus-article-commands-menu gnus-article-mode-map ""
1705        (cons "Commands" innards)))
1706
1707     (easy-menu-define
1708      gnus-summary-thread-menu gnus-summary-mode-map ""
1709      '("Threads"
1710        ["Toggle threading" gnus-summary-toggle-threads t]
1711        ["Hide threads" gnus-summary-hide-all-threads t]
1712        ["Show threads" gnus-summary-show-all-threads t]
1713        ["Hide thread" gnus-summary-hide-thread t]
1714        ["Show thread" gnus-summary-show-thread t]
1715        ["Go to next thread" gnus-summary-next-thread t]
1716        ["Go to previous thread" gnus-summary-prev-thread t]
1717        ["Go down thread" gnus-summary-down-thread t]
1718        ["Go up thread" gnus-summary-up-thread t]
1719        ["Top of thread" gnus-summary-top-thread t]
1720        ["Mark thread as read" gnus-summary-kill-thread t]
1721        ["Lower thread score" gnus-summary-lower-thread t]
1722        ["Raise thread score" gnus-summary-raise-thread t]
1723        ["Rethread current" gnus-summary-rethread-current t]
1724        ))
1725
1726     (easy-menu-define
1727      gnus-summary-post-menu gnus-summary-mode-map ""
1728      '("Post"
1729        ["Post an article" gnus-summary-post-news t]
1730        ["Followup" gnus-summary-followup t]
1731        ["Followup and yank" gnus-summary-followup-with-original t]
1732        ["Supersede article" gnus-summary-supersede-article t]
1733        ["Cancel article" gnus-summary-cancel-article t]
1734        ["Reply" gnus-summary-reply t]
1735        ["Reply and yank" gnus-summary-reply-with-original t]
1736        ["Wide reply" gnus-summary-wide-reply t]
1737        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1738        ["Mail forward" gnus-summary-mail-forward t]
1739        ["Post forward" gnus-summary-post-forward t]
1740        ["Digest and mail" gnus-uu-digest-mail-forward t]
1741        ["Digest and post" gnus-uu-digest-post-forward t]
1742        ["Resend message" gnus-summary-resend-message t]
1743        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1744        ["Send a mail" gnus-summary-mail-other-window t]
1745        ["Uuencode and post" gnus-uu-post-news t]
1746        ["Followup via news" gnus-summary-followup-to-mail t]
1747        ["Followup via news and yank"
1748         gnus-summary-followup-to-mail-with-original t]
1749        ;;("Draft"
1750        ;;["Send" gnus-summary-send-draft t]
1751        ;;["Send bounced" gnus-resend-bounced-mail t])
1752        ))
1753
1754     (easy-menu-define
1755      gnus-summary-misc-menu gnus-summary-mode-map ""
1756      '("Misc"
1757        ("Mark Read"
1758         ["Mark as read" gnus-summary-mark-as-read-forward t]
1759         ["Mark same subject and select"
1760          gnus-summary-kill-same-subject-and-select t]
1761         ["Mark same subject" gnus-summary-kill-same-subject t]
1762         ["Catchup" gnus-summary-catchup t]
1763         ["Catchup all" gnus-summary-catchup-all t]
1764         ["Catchup to here" gnus-summary-catchup-to-here t]
1765         ["Catchup region" gnus-summary-mark-region-as-read t]
1766         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1767        ("Mark Various"
1768         ["Tick" gnus-summary-tick-article-forward t]
1769         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1770         ["Remove marks" gnus-summary-clear-mark-forward t]
1771         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1772         ["Set bookmark" gnus-summary-set-bookmark t]
1773         ["Remove bookmark" gnus-summary-remove-bookmark t])
1774        ("Mark Limit"
1775         ["Marks..." gnus-summary-limit-to-marks t]
1776         ["Subject..." gnus-summary-limit-to-subject t]
1777         ["Author..." gnus-summary-limit-to-author t]
1778         ["Age..." gnus-summary-limit-to-age t]
1779         ["Score" gnus-summary-limit-to-score t]
1780         ["Unread" gnus-summary-limit-to-unread t]
1781         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1782         ["Articles" gnus-summary-limit-to-articles t]
1783         ["Pop limit" gnus-summary-pop-limit t]
1784         ["Show dormant" gnus-summary-limit-include-dormant t]
1785         ["Hide childless dormant"
1786          gnus-summary-limit-exclude-childless-dormant t]
1787         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1788         ["Hide marked" gnus-summary-limit-exclude-marks t]
1789         ["Show expunged" gnus-summary-show-all-expunged t])
1790        ("Process Mark"
1791         ["Set mark" gnus-summary-mark-as-processable t]
1792         ["Remove mark" gnus-summary-unmark-as-processable t]
1793         ["Remove all marks" gnus-summary-unmark-all-processable t]
1794         ["Mark above" gnus-uu-mark-over t]
1795         ["Mark series" gnus-uu-mark-series t]
1796         ["Mark region" gnus-uu-mark-region t]
1797         ["Unmark region" gnus-uu-unmark-region t]
1798         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1799         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1800         ["Mark all" gnus-uu-mark-all t]
1801         ["Mark buffer" gnus-uu-mark-buffer t]
1802         ["Mark sparse" gnus-uu-mark-sparse t]
1803         ["Mark thread" gnus-uu-mark-thread t]
1804         ["Unmark thread" gnus-uu-unmark-thread t]
1805         ("Process Mark Sets"
1806          ["Kill" gnus-summary-kill-process-mark t]
1807          ["Yank" gnus-summary-yank-process-mark
1808           gnus-newsgroup-process-stack]
1809          ["Save" gnus-summary-save-process-mark t]))
1810        ("Scroll article"
1811         ["Page forward" gnus-summary-next-page t]
1812         ["Page backward" gnus-summary-prev-page t]
1813         ["Line forward" gnus-summary-scroll-up t])
1814        ("Move"
1815         ["Next unread article" gnus-summary-next-unread-article t]
1816         ["Previous unread article" gnus-summary-prev-unread-article t]
1817         ["Next article" gnus-summary-next-article t]
1818         ["Previous article" gnus-summary-prev-article t]
1819         ["Next unread subject" gnus-summary-next-unread-subject t]
1820         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1821         ["Next article same subject" gnus-summary-next-same-subject t]
1822         ["Previous article same subject" gnus-summary-prev-same-subject t]
1823         ["First unread article" gnus-summary-first-unread-article t]
1824         ["Best unread article" gnus-summary-best-unread-article t]
1825         ["Go to subject number..." gnus-summary-goto-subject t]
1826         ["Go to article number..." gnus-summary-goto-article t]
1827         ["Go to the last article" gnus-summary-goto-last-article t]
1828         ["Pop article off history" gnus-summary-pop-article t])
1829        ("Sort"
1830         ["Sort by number" gnus-summary-sort-by-number t]
1831         ["Sort by author" gnus-summary-sort-by-author t]
1832         ["Sort by subject" gnus-summary-sort-by-subject t]
1833         ["Sort by date" gnus-summary-sort-by-date t]
1834         ["Sort by score" gnus-summary-sort-by-score t]
1835         ["Sort by lines" gnus-summary-sort-by-lines t]
1836         ["Sort by characters" gnus-summary-sort-by-chars t])
1837        ("Help"
1838         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1839         ["Describe group" gnus-summary-describe-group t]
1840         ["Read manual" gnus-info-find-node t])
1841        ("Modes"
1842         ["Pick and read" gnus-pick-mode t]
1843         ["Binary" gnus-binary-mode t])
1844        ("Regeneration"
1845         ["Regenerate" gnus-summary-prepare t]
1846         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1847         ["Toggle threading" gnus-summary-toggle-threads t])
1848        ["Filter articles..." gnus-summary-execute-command t]
1849        ["Run command on subjects..." gnus-summary-universal-argument t]
1850        ["Search articles forward..." gnus-summary-search-article-forward t]
1851        ["Search articles backward..." gnus-summary-search-article-backward t]
1852        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1853        ["Expand window" gnus-summary-expand-window t]
1854        ["Expire expirable articles" gnus-summary-expire-articles
1855         (gnus-check-backend-function
1856          'request-expire-articles gnus-newsgroup-name)]
1857        ["Edit local kill file" gnus-summary-edit-local-kill t]
1858        ["Edit main kill file" gnus-summary-edit-global-kill t]
1859        ["Edit group parameters" gnus-summary-edit-parameters t]
1860        ["Customize group parameters" gnus-summary-customize-parameters t]
1861        ["Send a bug report" gnus-bug t]
1862        ("Exit"
1863         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1864         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1865         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1866         ["Exit group" gnus-summary-exit t]
1867         ["Exit group without updating" gnus-summary-exit-no-update t]
1868         ["Exit and goto next group" gnus-summary-next-group t]
1869         ["Exit and goto prev group" gnus-summary-prev-group t]
1870         ["Reselect group" gnus-summary-reselect-current-group t]
1871         ["Rescan group" gnus-summary-rescan-group t]
1872         ["Update dribble" gnus-summary-save-newsrc t])))
1873
1874     (gnus-run-hooks 'gnus-summary-menu-hook)))
1875
1876 (defun gnus-score-set-default (var value)
1877   "A version of set that updates the GNU Emacs menu-bar."
1878   (set var value)
1879   ;; It is the message that forces the active status to be updated.
1880   (message ""))
1881
1882 (defun gnus-make-score-map (type)
1883   "Make a summary score map of type TYPE."
1884   (if t
1885       nil
1886     (let ((headers '(("author" "from" string)
1887                      ("subject" "subject" string)
1888                      ("article body" "body" string)
1889                      ("article head" "head" string)
1890                      ("xref" "xref" string)
1891                      ("extra header" "extra" string)
1892                      ("lines" "lines" number)
1893                      ("followups to author" "followup" string)))
1894           (types '((number ("less than" <)
1895                            ("greater than" >)
1896                            ("equal" =))
1897                    (string ("substring" s)
1898                            ("exact string" e)
1899                            ("fuzzy string" f)
1900                            ("regexp" r))))
1901           (perms '(("temporary" (current-time-string))
1902                    ("permanent" nil)
1903                    ("immediate" now)))
1904           header)
1905       (list
1906        (apply
1907         'nconc
1908         (list
1909          (if (eq type 'lower)
1910              "Lower score"
1911            "Increase score"))
1912         (let (outh)
1913           (while headers
1914             (setq header (car headers))
1915             (setq outh
1916                   (cons
1917                    (apply
1918                     'nconc
1919                     (list (car header))
1920                     (let ((ts (cdr (assoc (nth 2 header) types)))
1921                           outt)
1922                       (while ts
1923                         (setq outt
1924                               (cons
1925                                (apply
1926                                 'nconc
1927                                 (list (caar ts))
1928                                 (let ((ps perms)
1929                                       outp)
1930                                   (while ps
1931                                     (setq outp
1932                                           (cons
1933                                            (vector
1934                                             (caar ps)
1935                                             (list
1936                                              'gnus-summary-score-entry
1937                                              (nth 1 header)
1938                                              (if (or (string= (nth 1 header)
1939                                                               "head")
1940                                                      (string= (nth 1 header)
1941                                                               "body"))
1942                                                  ""
1943                                                (list 'gnus-summary-header
1944                                                      (nth 1 header)))
1945                                              (list 'quote (nth 1 (car ts)))
1946                                              (list 'gnus-score-default nil)
1947                                              (nth 1 (car ps))
1948                                              t)
1949                                             t)
1950                                            outp))
1951                                     (setq ps (cdr ps)))
1952                                   (list (nreverse outp))))
1953                                outt))
1954                         (setq ts (cdr ts)))
1955                       (list (nreverse outt))))
1956                    outh))
1957             (setq headers (cdr headers)))
1958           (list (nreverse outh))))))))
1959
1960 \f
1961
1962 (defun gnus-summary-mode (&optional group)
1963   "Major mode for reading articles.
1964
1965 All normal editing commands are switched off.
1966 \\<gnus-summary-mode-map>
1967 Each line in this buffer represents one article.  To read an
1968 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1969 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1970 respectively.
1971
1972 You can also post articles and send mail from this buffer.  To
1973 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1974 of an article, type `\\[gnus-summary-reply]'.
1975
1976 There are approx. one gazillion commands you can execute in this
1977 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1978
1979 The following commands are available:
1980
1981 \\{gnus-summary-mode-map}"
1982   (interactive)
1983   (when (gnus-visual-p 'summary-menu 'menu)
1984     (gnus-summary-make-menu-bar))
1985   (kill-all-local-variables)
1986   (gnus-summary-make-local-variables)
1987   (gnus-make-thread-indent-array)
1988   (gnus-simplify-mode-line)
1989   (setq major-mode 'gnus-summary-mode)
1990   (setq mode-name "Summary")
1991   (make-local-variable 'minor-mode-alist)
1992   (use-local-map gnus-summary-mode-map)
1993   (buffer-disable-undo)
1994   (setq buffer-read-only t)             ;Disable modification
1995   (setq truncate-lines t)
1996   (setq selective-display t)
1997   (setq selective-display-ellipses t)   ;Display `...'
1998   (gnus-summary-set-display-table)
1999   (gnus-set-default-directory)
2000   (setq gnus-newsgroup-name group)
2001   (make-local-variable 'gnus-summary-line-format)
2002   (make-local-variable 'gnus-summary-line-format-spec)
2003   (make-local-variable 'gnus-summary-dummy-line-format)
2004   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2005   (make-local-variable 'gnus-summary-mark-positions)
2006   (make-local-hook 'pre-command-hook)
2007   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2008   (gnus-run-hooks 'gnus-summary-mode-hook)
2009   (mm-enable-multibyte)
2010   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2011   (gnus-update-summary-mark-positions))
2012
2013 (defun gnus-summary-make-local-variables ()
2014   "Make all the local summary buffer variables."
2015   (let (global)
2016     (dolist (local gnus-summary-local-variables)
2017       (if (consp local)
2018           (progn
2019             (if (eq (cdr local) 'global)
2020                 ;; Copy the global value of the variable.
2021                 (setq global (symbol-value (car local)))
2022               ;; Use the value from the list.
2023               (setq global (eval (cdr local))))
2024             (set (make-local-variable (car local)) global))
2025         ;; Simple nil-valued local variable.
2026         (set (make-local-variable local) nil)))))
2027
2028 (defun gnus-summary-clear-local-variables ()
2029   (let ((locals gnus-summary-local-variables))
2030     (while locals
2031       (if (consp (car locals))
2032           (and (vectorp (caar locals))
2033                (set (caar locals) nil))
2034         (and (vectorp (car locals))
2035              (set (car locals) nil)))
2036       (setq locals (cdr locals)))))
2037
2038 ;; Summary data functions.
2039
2040 (defmacro gnus-data-number (data)
2041   `(car ,data))
2042
2043 (defmacro gnus-data-set-number (data number)
2044   `(setcar ,data ,number))
2045
2046 (defmacro gnus-data-mark (data)
2047   `(nth 1 ,data))
2048
2049 (defmacro gnus-data-set-mark (data mark)
2050   `(setcar (nthcdr 1 ,data) ,mark))
2051
2052 (defmacro gnus-data-pos (data)
2053   `(nth 2 ,data))
2054
2055 (defmacro gnus-data-set-pos (data pos)
2056   `(setcar (nthcdr 2 ,data) ,pos))
2057
2058 (defmacro gnus-data-header (data)
2059   `(nth 3 ,data))
2060
2061 (defmacro gnus-data-set-header (data header)
2062   `(setf (nth 3 ,data) ,header))
2063
2064 (defmacro gnus-data-level (data)
2065   `(nth 4 ,data))
2066
2067 (defmacro gnus-data-unread-p (data)
2068   `(= (nth 1 ,data) gnus-unread-mark))
2069
2070 (defmacro gnus-data-read-p (data)
2071   `(/= (nth 1 ,data) gnus-unread-mark))
2072
2073 (defmacro gnus-data-pseudo-p (data)
2074   `(consp (nth 3 ,data)))
2075
2076 (defmacro gnus-data-find (number)
2077   `(assq ,number gnus-newsgroup-data))
2078
2079 (defmacro gnus-data-find-list (number &optional data)
2080   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2081      (memq (assq ,number bdata)
2082            bdata)))
2083
2084 (defmacro gnus-data-make (number mark pos header level)
2085   `(list ,number ,mark ,pos ,header ,level))
2086
2087 (defun gnus-data-enter (after-article number mark pos header level offset)
2088   (let ((data (gnus-data-find-list after-article)))
2089     (unless data
2090       (error "No such article: %d" after-article))
2091     (setcdr data (cons (gnus-data-make number mark pos header level)
2092                        (cdr data)))
2093     (setq gnus-newsgroup-data-reverse nil)
2094     (gnus-data-update-list (cddr data) offset)))
2095
2096 (defun gnus-data-enter-list (after-article list &optional offset)
2097   (when list
2098     (let ((data (and after-article (gnus-data-find-list after-article)))
2099           (ilist list))
2100       (if (not (or data
2101                    after-article))
2102           (let ((odata gnus-newsgroup-data))
2103             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2104             (when offset
2105               (gnus-data-update-list odata offset)))
2106         ;; Find the last element in the list to be spliced into the main
2107         ;; list.
2108         (while (cdr list)
2109           (setq list (cdr list)))
2110         (if (not data)
2111             (progn
2112               (setcdr list gnus-newsgroup-data)
2113               (setq gnus-newsgroup-data ilist)
2114               (when offset
2115                 (gnus-data-update-list (cdr list) offset)))
2116           (setcdr list (cdr data))
2117           (setcdr data ilist)
2118           (when offset
2119             (gnus-data-update-list (cdr list) offset))))
2120       (setq gnus-newsgroup-data-reverse nil))))
2121
2122 (defun gnus-data-remove (article &optional offset)
2123   (let ((data gnus-newsgroup-data))
2124     (if (= (gnus-data-number (car data)) article)
2125         (progn
2126           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2127                 gnus-newsgroup-data-reverse nil)
2128           (when offset
2129             (gnus-data-update-list gnus-newsgroup-data offset)))
2130       (while (cdr data)
2131         (when (= (gnus-data-number (cadr data)) article)
2132           (setcdr data (cddr data))
2133           (when offset
2134             (gnus-data-update-list (cdr data) offset))
2135           (setq data nil
2136                 gnus-newsgroup-data-reverse nil))
2137         (setq data (cdr data))))))
2138
2139 (defmacro gnus-data-list (backward)
2140   `(if ,backward
2141        (or gnus-newsgroup-data-reverse
2142            (setq gnus-newsgroup-data-reverse
2143                  (reverse gnus-newsgroup-data)))
2144      gnus-newsgroup-data))
2145
2146 (defun gnus-data-update-list (data offset)
2147   "Add OFFSET to the POS of all data entries in DATA."
2148   (setq gnus-newsgroup-data-reverse nil)
2149   (while data
2150     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2151     (setq data (cdr data))))
2152
2153 (defun gnus-summary-article-pseudo-p (article)
2154   "Say whether this article is a pseudo article or not."
2155   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2156
2157 (defmacro gnus-summary-article-sparse-p (article)
2158   "Say whether this article is a sparse article or not."
2159   `(memq ,article gnus-newsgroup-sparse))
2160
2161 (defmacro gnus-summary-article-ancient-p (article)
2162   "Say whether this article is a sparse article or not."
2163   `(memq ,article gnus-newsgroup-ancient))
2164
2165 (defun gnus-article-parent-p (number)
2166   "Say whether this article is a parent or not."
2167   (let ((data (gnus-data-find-list number)))
2168     (and (cdr data)                     ; There has to be an article after...
2169          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2170             (gnus-data-level (nth 1 data))))))
2171
2172 (defun gnus-article-children (number)
2173   "Return a list of all children to NUMBER."
2174   (let* ((data (gnus-data-find-list number))
2175          (level (gnus-data-level (car data)))
2176          children)
2177     (setq data (cdr data))
2178     (while (and data
2179                 (= (gnus-data-level (car data)) (1+ level)))
2180       (push (gnus-data-number (car data)) children)
2181       (setq data (cdr data)))
2182     children))
2183
2184 (defmacro gnus-summary-skip-intangible ()
2185   "If the current article is intangible, then jump to a different article."
2186   '(let ((to (get-text-property (point) 'gnus-intangible)))
2187      (and to (gnus-summary-goto-subject to))))
2188
2189 (defmacro gnus-summary-article-intangible-p ()
2190   "Say whether this article is intangible or not."
2191   '(get-text-property (point) 'gnus-intangible))
2192
2193 (defun gnus-article-read-p (article)
2194   "Say whether ARTICLE is read or not."
2195   (not (or (memq article gnus-newsgroup-marked)
2196            (memq article gnus-newsgroup-unreads)
2197            (memq article gnus-newsgroup-unselected)
2198            (memq article gnus-newsgroup-dormant))))
2199
2200 ;; Some summary mode macros.
2201
2202 (defmacro gnus-summary-article-number ()
2203   "The article number of the article on the current line.
2204 If there isn's an article number here, then we return the current
2205 article number."
2206   '(progn
2207      (gnus-summary-skip-intangible)
2208      (or (get-text-property (point) 'gnus-number)
2209          (gnus-summary-last-subject))))
2210
2211 (defmacro gnus-summary-article-header (&optional number)
2212   "Return the header of article NUMBER."
2213   `(gnus-data-header (gnus-data-find
2214                       ,(or number '(gnus-summary-article-number)))))
2215
2216 (defmacro gnus-summary-thread-level (&optional number)
2217   "Return the level of thread that starts with article NUMBER."
2218   `(if (and (eq gnus-summary-make-false-root 'dummy)
2219             (get-text-property (point) 'gnus-intangible))
2220        0
2221      (gnus-data-level (gnus-data-find
2222                        ,(or number '(gnus-summary-article-number))))))
2223
2224 (defmacro gnus-summary-article-mark (&optional number)
2225   "Return the mark of article NUMBER."
2226   `(gnus-data-mark (gnus-data-find
2227                     ,(or number '(gnus-summary-article-number)))))
2228
2229 (defmacro gnus-summary-article-pos (&optional number)
2230   "Return the position of the line of article NUMBER."
2231   `(gnus-data-pos (gnus-data-find
2232                    ,(or number '(gnus-summary-article-number)))))
2233
2234 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2235 (defmacro gnus-summary-article-subject (&optional number)
2236   "Return current subject string or nil if nothing."
2237   `(let ((headers
2238           ,(if number
2239                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2240              '(gnus-data-header (assq (gnus-summary-article-number)
2241                                       gnus-newsgroup-data)))))
2242      (and headers
2243           (vectorp headers)
2244           (mail-header-subject headers))))
2245
2246 (defmacro gnus-summary-article-score (&optional number)
2247   "Return current article score."
2248   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2249                   gnus-newsgroup-scored))
2250        gnus-summary-default-score 0))
2251
2252 (defun gnus-summary-article-children (&optional number)
2253   "Return a list of article numbers that are children of article NUMBER."
2254   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2255          (level (gnus-data-level (car data)))
2256          l children)
2257     (while (and (setq data (cdr data))
2258                 (> (setq l (gnus-data-level (car data))) level))
2259       (and (= (1+ level) l)
2260            (push (gnus-data-number (car data))
2261                  children)))
2262     (nreverse children)))
2263
2264 (defun gnus-summary-article-parent (&optional number)
2265   "Return the article number of the parent of article NUMBER."
2266   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2267                                     (gnus-data-list t)))
2268          (level (gnus-data-level (car data))))
2269     (if (zerop level)
2270         ()                              ; This is a root.
2271       ;; We search until we find an article with a level less than
2272       ;; this one.  That function has to be the parent.
2273       (while (and (setq data (cdr data))
2274                   (not (< (gnus-data-level (car data)) level))))
2275       (and data (gnus-data-number (car data))))))
2276
2277 (defun gnus-unread-mark-p (mark)
2278   "Say whether MARK is the unread mark."
2279   (= mark gnus-unread-mark))
2280
2281 (defun gnus-read-mark-p (mark)
2282   "Say whether MARK is one of the marks that mark as read.
2283 This is all marks except unread, ticked, dormant, and expirable."
2284   (not (or (= mark gnus-unread-mark)
2285            (= mark gnus-ticked-mark)
2286            (= mark gnus-dormant-mark)
2287            (= mark gnus-expirable-mark))))
2288
2289 (defmacro gnus-article-mark (number)
2290   "Return the MARK of article NUMBER.
2291 This macro should only be used when computing the mark the \"first\"
2292 time; i.e., when generating the summary lines.  After that,
2293 `gnus-summary-article-mark' should be used to examine the
2294 marks of articles."
2295   `(cond
2296     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2297     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2298     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2299     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2300     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2301     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2302     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2303     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2304            gnus-ancient-mark))))
2305
2306 ;; Saving hidden threads.
2307
2308 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2309 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2310
2311 (defmacro gnus-save-hidden-threads (&rest forms)
2312   "Save hidden threads, eval FORMS, and restore the hidden threads."
2313   (let ((config (make-symbol "config")))
2314     `(let ((,config (gnus-hidden-threads-configuration)))
2315        (unwind-protect
2316            (save-excursion
2317              ,@forms)
2318          (gnus-restore-hidden-threads-configuration ,config)))))
2319
2320 (defun gnus-data-compute-positions ()
2321   "Compute the positions of all articles."
2322   (setq gnus-newsgroup-data-reverse nil)
2323   (let ((data gnus-newsgroup-data))
2324     (save-excursion
2325       (gnus-save-hidden-threads
2326         (gnus-summary-show-all-threads)
2327         (goto-char (point-min))
2328         (while data
2329           (while (get-text-property (point) 'gnus-intangible)
2330             (forward-line 1))
2331           (gnus-data-set-pos (car data) (+ (point) 3))
2332           (setq data (cdr data))
2333           (forward-line 1))))))
2334
2335 (defun gnus-hidden-threads-configuration ()
2336   "Return the current hidden threads configuration."
2337   (save-excursion
2338     (let (config)
2339       (goto-char (point-min))
2340       (while (search-forward "\r" nil t)
2341         (push (1- (point)) config))
2342       config)))
2343
2344 (defun gnus-restore-hidden-threads-configuration (config)
2345   "Restore hidden threads configuration from CONFIG."
2346   (let (point buffer-read-only)
2347     (while (setq point (pop config))
2348       (when (and (< point (point-max))
2349                  (goto-char point)
2350                  (eq (char-after) ?\n))
2351         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2352
2353 ;; Various summary mode internalish functions.
2354
2355 (defun gnus-mouse-pick-article (e)
2356   (interactive "e")
2357   (mouse-set-point e)
2358   (gnus-summary-next-page nil t))
2359
2360 (defun gnus-summary-set-display-table ()
2361   ;; Change the display table.  Odd characters have a tendency to mess
2362   ;; up nicely formatted displays - we make all possible glyphs
2363   ;; display only a single character.
2364
2365   ;; We start from the standard display table, if any.
2366   (let ((table (or (copy-sequence standard-display-table)
2367                    (make-display-table)))
2368         (i 32))
2369     ;; Nix out all the control chars...
2370     (while (>= (setq i (1- i)) 0)
2371       (aset table i [??]))
2372     ;; ... but not newline and cr, of course.  (cr is necessary for the
2373     ;; selective display).
2374     (aset table ?\n nil)
2375     (aset table ?\r nil)
2376     ;; We keep TAB as well.
2377     (aset table ?\t nil)
2378     ;; We nix out any glyphs over 126 that are not set already.
2379     (let ((i 256))
2380       (while (>= (setq i (1- i)) 127)
2381         ;; Only modify if the entry is nil.
2382         (unless (aref table i)
2383           (aset table i [??]))))
2384     (setq buffer-display-table table)))
2385
2386 (defun gnus-summary-setup-buffer (group)
2387   "Initialize summary buffer."
2388   (let ((buffer (concat "*Summary " group "*")))
2389     (if (get-buffer buffer)
2390         (progn
2391           (set-buffer buffer)
2392           (setq gnus-summary-buffer (current-buffer))
2393           (not gnus-newsgroup-prepared))
2394       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2395       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2396       (gnus-summary-mode group)
2397       (when gnus-carpal
2398         (gnus-carpal-setup-buffer 'summary))
2399       (unless gnus-single-article-buffer
2400         (make-local-variable 'gnus-article-buffer)
2401         (make-local-variable 'gnus-article-current)
2402         (make-local-variable 'gnus-original-article-buffer))
2403       (setq gnus-newsgroup-name group)
2404       t)))
2405
2406 (defun gnus-set-global-variables ()
2407   ;; Set the global equivalents of the summary buffer-local variables
2408   ;; to the latest values they had.  These reflect the summary buffer
2409   ;; that was in action when the last article was fetched.
2410   (when (eq major-mode 'gnus-summary-mode)
2411     (setq gnus-summary-buffer (current-buffer))
2412     (let ((name gnus-newsgroup-name)
2413           (marked gnus-newsgroup-marked)
2414           (unread gnus-newsgroup-unreads)
2415           (headers gnus-current-headers)
2416           (data gnus-newsgroup-data)
2417           (summary gnus-summary-buffer)
2418           (article-buffer gnus-article-buffer)
2419           (original gnus-original-article-buffer)
2420           (gac gnus-article-current)
2421           (reffed gnus-reffed-article-number)
2422           (score-file gnus-current-score-file)
2423           (default-charset gnus-newsgroup-charset))
2424       (save-excursion
2425         (set-buffer gnus-group-buffer)
2426         (setq gnus-newsgroup-name name
2427               gnus-newsgroup-marked marked
2428               gnus-newsgroup-unreads unread
2429               gnus-current-headers headers
2430               gnus-newsgroup-data data
2431               gnus-article-current gac
2432               gnus-summary-buffer summary
2433               gnus-article-buffer article-buffer
2434               gnus-original-article-buffer original
2435               gnus-reffed-article-number reffed
2436               gnus-current-score-file score-file
2437               gnus-newsgroup-charset default-charset)
2438         ;; The article buffer also has local variables.
2439         (when (gnus-buffer-live-p gnus-article-buffer)
2440           (set-buffer gnus-article-buffer)
2441           (setq gnus-summary-buffer summary))))))
2442
2443 (defun gnus-summary-article-unread-p (article)
2444   "Say whether ARTICLE is unread or not."
2445   (memq article gnus-newsgroup-unreads))
2446
2447 (defun gnus-summary-first-article-p (&optional article)
2448   "Return whether ARTICLE is the first article in the buffer."
2449   (if (not (setq article (or article (gnus-summary-article-number))))
2450       nil
2451     (eq article (caar gnus-newsgroup-data))))
2452
2453 (defun gnus-summary-last-article-p (&optional article)
2454   "Return whether ARTICLE is the last article in the buffer."
2455   (if (not (setq article (or article (gnus-summary-article-number))))
2456       ;; All non-existent numbers are the last article.  :-)
2457       t
2458     (not (cdr (gnus-data-find-list article)))))
2459
2460 (defun gnus-make-thread-indent-array ()
2461   (let ((n 200))
2462     (unless (and gnus-thread-indent-array
2463                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2464       (setq gnus-thread-indent-array (make-vector 201 "")
2465             gnus-thread-indent-array-level gnus-thread-indent-level)
2466       (while (>= n 0)
2467         (aset gnus-thread-indent-array n
2468               (make-string (* n gnus-thread-indent-level) ? ))
2469         (setq n (1- n))))))
2470
2471 (defun gnus-update-summary-mark-positions ()
2472   "Compute where the summary marks are to go."
2473   (save-excursion
2474     (when (gnus-buffer-exists-p gnus-summary-buffer)
2475       (set-buffer gnus-summary-buffer))
2476     (let ((gnus-replied-mark 129)
2477           (gnus-score-below-mark 130)
2478           (gnus-score-over-mark 130)
2479           (gnus-download-mark 131)
2480           (spec gnus-summary-line-format-spec)
2481           gnus-visual pos)
2482       (save-excursion
2483         (gnus-set-work-buffer)
2484         (let ((gnus-summary-line-format-spec spec)
2485               (gnus-newsgroup-downloadable '((0 . t))))
2486           (gnus-summary-insert-line
2487            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2488           (goto-char (point-min))
2489           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2490                                              (- (point) 2)))))
2491           (goto-char (point-min))
2492           (push (cons 'replied (and (search-forward "\201" nil t)
2493                                     (- (point) 2)))
2494                 pos)
2495           (goto-char (point-min))
2496           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2497                 pos)
2498           (goto-char (point-min))
2499           (push (cons 'download
2500                       (and (search-forward "\203" nil t) (- (point) 2)))
2501                 pos)))
2502       (setq gnus-summary-mark-positions pos))))
2503
2504 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2505   "Insert a dummy root in the summary buffer."
2506   (beginning-of-line)
2507   (gnus-add-text-properties
2508    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2509    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2510
2511 (defun gnus-summary-from-or-to-or-newsgroups (header)
2512   (let ((to (cdr (assq 'To (mail-header-extra header))))
2513         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2514         (mail-parse-charset gnus-newsgroup-charset))
2515     (cond
2516      ((and to
2517            gnus-ignored-from-addresses
2518            (string-match gnus-ignored-from-addresses
2519                          (mail-header-from header)))
2520       (concat "-> "
2521               (or (car (funcall gnus-extract-address-components
2522                                 (funcall
2523                                  gnus-decode-encoded-word-function to)))
2524                   (funcall gnus-decode-encoded-word-function to))))
2525      ((and newsgroups
2526            gnus-ignored-from-addresses
2527            (string-match gnus-ignored-from-addresses
2528                          (mail-header-from header)))
2529       (concat "=> " newsgroups))
2530      (t
2531       (or (car (funcall gnus-extract-address-components
2532                         (mail-header-from header)))
2533           (mail-header-from header))))))
2534
2535 (defun gnus-summary-insert-line (gnus-tmp-header
2536                                  gnus-tmp-level gnus-tmp-current
2537                                  gnus-tmp-unread gnus-tmp-replied
2538                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2539                                  &optional gnus-tmp-dummy gnus-tmp-score
2540                                  gnus-tmp-process)
2541   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2542          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2543          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2544          (gnus-tmp-score-char
2545           (if (or (null gnus-summary-default-score)
2546                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2547                       gnus-summary-zcore-fuzz))
2548               ? ;Whitespace
2549             (if (< gnus-tmp-score gnus-summary-default-score)
2550                 gnus-score-below-mark gnus-score-over-mark)))
2551          (gnus-tmp-replied
2552           (cond (gnus-tmp-process gnus-process-mark)
2553                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2554                  gnus-cached-mark)
2555                 (gnus-tmp-replied gnus-replied-mark)
2556                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2557                  gnus-saved-mark)
2558                 (t gnus-unread-mark)))
2559          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2560          (gnus-tmp-name
2561           (cond
2562            ((string-match "<[^>]+> *$" gnus-tmp-from)
2563             (let ((beg (match-beginning 0)))
2564               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2565                        (substring gnus-tmp-from (1+ (match-beginning 0))
2566                                   (1- (match-end 0))))
2567                   (substring gnus-tmp-from 0 beg))))
2568            ((string-match "(.+)" gnus-tmp-from)
2569             (substring gnus-tmp-from
2570                        (1+ (match-beginning 0)) (1- (match-end 0))))
2571            (t gnus-tmp-from)))
2572          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2573          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2574          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2575          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2576          (buffer-read-only nil))
2577     (when (string= gnus-tmp-name "")
2578       (setq gnus-tmp-name gnus-tmp-from))
2579     (unless (numberp gnus-tmp-lines)
2580       (setq gnus-tmp-lines 0))
2581     (gnus-put-text-property
2582      (point)
2583      (progn (eval gnus-summary-line-format-spec) (point))
2584      'gnus-number gnus-tmp-number)
2585     (when (gnus-visual-p 'summary-highlight 'highlight)
2586       (forward-line -1)
2587       (gnus-run-hooks 'gnus-summary-update-hook)
2588       (forward-line 1))))
2589
2590 (defun gnus-summary-update-line (&optional dont-update)
2591   ;; Update summary line after change.
2592   (when (and gnus-summary-default-score
2593              (not gnus-summary-inhibit-highlight))
2594     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2595            (article (gnus-summary-article-number))
2596            (score (gnus-summary-article-score article)))
2597       (unless dont-update
2598         (if (and gnus-summary-mark-below
2599                  (< (gnus-summary-article-score)
2600                     gnus-summary-mark-below))
2601             ;; This article has a low score, so we mark it as read.
2602             (when (memq article gnus-newsgroup-unreads)
2603               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2604           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2605             ;; This article was previously marked as read on account
2606             ;; of a low score, but now it has risen, so we mark it as
2607             ;; unread.
2608             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2609         (gnus-summary-update-mark
2610          (if (or (null gnus-summary-default-score)
2611                  (<= (abs (- score gnus-summary-default-score))
2612                      gnus-summary-zcore-fuzz))
2613              ? ;Whitespace
2614            (if (< score gnus-summary-default-score)
2615                gnus-score-below-mark gnus-score-over-mark))
2616          'score))
2617       ;; Do visual highlighting.
2618       (when (gnus-visual-p 'summary-highlight 'highlight)
2619         (gnus-run-hooks 'gnus-summary-update-hook)))))
2620
2621 (defvar gnus-tmp-new-adopts nil)
2622
2623 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2624   "Return the number of articles in THREAD.
2625 This may be 0 in some cases -- if none of the articles in
2626 the thread are to be displayed."
2627   (let* ((number
2628           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2629           (cond
2630            ((not (listp thread))
2631             1)
2632            ((and (consp thread) (cdr thread))
2633             (apply
2634              '+ 1 (mapcar
2635                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2636            ((null thread)
2637             1)
2638            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2639             1)
2640            (t 0))))
2641     (when (and level (zerop level) gnus-tmp-new-adopts)
2642       (incf number
2643             (apply '+ (mapcar
2644                        'gnus-summary-number-of-articles-in-thread
2645                        gnus-tmp-new-adopts))))
2646     (if char
2647         (if (> number 1) gnus-not-empty-thread-mark
2648           gnus-empty-thread-mark)
2649       number)))
2650
2651 (defun gnus-summary-set-local-parameters (group)
2652   "Go through the local params of GROUP and set all variable specs in that list."
2653   (let ((params (gnus-group-find-parameter group))
2654         elem)
2655     (while params
2656       (setq elem (car params)
2657             params (cdr params))
2658       (and (consp elem)                 ; Has to be a cons.
2659            (consp (cdr elem))           ; The cdr has to be a list.
2660            (symbolp (car elem))         ; Has to be a symbol in there.
2661            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2662            (ignore-errors               ; So we set it.
2663              (make-local-variable (car elem))
2664              (set (car elem) (eval (nth 1 elem))))))))
2665
2666 (defun gnus-summary-read-group (group &optional show-all no-article
2667                                       kill-buffer no-display backward
2668                                       select-articles)
2669   "Start reading news in newsgroup GROUP.
2670 If SHOW-ALL is non-nil, already read articles are also listed.
2671 If NO-ARTICLE is non-nil, no article is selected initially.
2672 If NO-DISPLAY, don't generate a summary buffer."
2673   (let (result)
2674     (while (and group
2675                 (null (setq result
2676                             (let ((gnus-auto-select-next nil))
2677                               (or (gnus-summary-read-group-1
2678                                    group show-all no-article
2679                                    kill-buffer no-display
2680                                    select-articles)
2681                                   (setq show-all nil
2682                                         select-articles nil)))))
2683                 (eq gnus-auto-select-next 'quietly))
2684       (set-buffer gnus-group-buffer)
2685       ;; The entry function called above goes to the next
2686       ;; group automatically, so we go two groups back
2687       ;; if we are searching for the previous group.
2688       (when backward
2689         (gnus-group-prev-unread-group 2))
2690       (if (not (equal group (gnus-group-group-name)))
2691           (setq group (gnus-group-group-name))
2692         (setq group nil)))
2693     result))
2694
2695 (defun gnus-summary-read-group-1 (group show-all no-article
2696                                         kill-buffer no-display
2697                                         &optional select-articles)
2698   ;; Killed foreign groups can't be entered.
2699   (when (and (not (gnus-group-native-p group))
2700              (not (gnus-gethash group gnus-newsrc-hashtb)))
2701     (error "Dead non-native groups can't be entered"))
2702   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2703   (let* ((new-group (gnus-summary-setup-buffer group))
2704          (quit-config (gnus-group-quit-config group))
2705          (did-select (and new-group (gnus-select-newsgroup
2706                                      group show-all select-articles))))
2707     (cond
2708      ;; This summary buffer exists already, so we just select it.
2709      ((not new-group)
2710       (gnus-set-global-variables)
2711       (when kill-buffer
2712         (gnus-kill-or-deaden-summary kill-buffer))
2713       (gnus-configure-windows 'summary 'force)
2714       (gnus-set-mode-line 'summary)
2715       (gnus-summary-position-point)
2716       (message "")
2717       t)
2718      ;; We couldn't select this group.
2719      ((null did-select)
2720       (when (and (eq major-mode 'gnus-summary-mode)
2721                  (not (equal (current-buffer) kill-buffer)))
2722         (kill-buffer (current-buffer))
2723         (if (not quit-config)
2724             (progn
2725               ;; Update the info -- marks might need to be removed,
2726               ;; for instance.
2727               (gnus-summary-update-info)
2728               (set-buffer gnus-group-buffer)
2729               (gnus-group-jump-to-group group)
2730               (gnus-group-next-unread-group 1))
2731           (gnus-handle-ephemeral-exit quit-config)))
2732       (gnus-message 3 "Can't select group")
2733       nil)
2734      ;; The user did a `C-g' while prompting for number of articles,
2735      ;; so we exit this group.
2736      ((eq did-select 'quit)
2737       (and (eq major-mode 'gnus-summary-mode)
2738            (not (equal (current-buffer) kill-buffer))
2739            (kill-buffer (current-buffer)))
2740       (when kill-buffer
2741         (gnus-kill-or-deaden-summary kill-buffer))
2742       (if (not quit-config)
2743           (progn
2744             (set-buffer gnus-group-buffer)
2745             (gnus-group-jump-to-group group)
2746             (gnus-group-next-unread-group 1)
2747             (gnus-configure-windows 'group 'force))
2748         (gnus-handle-ephemeral-exit quit-config))
2749       ;; Finally signal the quit.
2750       (signal 'quit nil))
2751      ;; The group was successfully selected.
2752      (t
2753       (gnus-set-global-variables)
2754       ;; Save the active value in effect when the group was entered.
2755       (setq gnus-newsgroup-active
2756             (gnus-copy-sequence
2757              (gnus-active gnus-newsgroup-name)))
2758       ;; You can change the summary buffer in some way with this hook.
2759       (gnus-run-hooks 'gnus-select-group-hook)
2760       ;; Set any local variables in the group parameters.
2761       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2762       (gnus-update-format-specifications
2763        nil 'summary 'summary-mode 'summary-dummy)
2764       ;; Do score processing.
2765       (when gnus-use-scoring
2766         (gnus-possibly-score-headers))
2767       ;; Check whether to fill in the gaps in the threads.
2768       (when gnus-build-sparse-threads
2769         (gnus-build-sparse-threads))
2770       ;; Find the initial limit.
2771       (if gnus-show-threads
2772           (if show-all
2773               (let ((gnus-newsgroup-dormant nil))
2774                 (gnus-summary-initial-limit show-all))
2775             (gnus-summary-initial-limit show-all))
2776         (setq gnus-newsgroup-limit
2777               (mapcar
2778                (lambda (header) (mail-header-number header))
2779                gnus-newsgroup-headers)))
2780       ;; Generate the summary buffer.
2781       (unless no-display
2782         (gnus-summary-prepare))
2783       (when gnus-use-trees
2784         (gnus-tree-open group)
2785         (setq gnus-summary-highlight-line-function
2786               'gnus-tree-highlight-article))
2787       ;; If the summary buffer is empty, but there are some low-scored
2788       ;; articles or some excluded dormants, we include these in the
2789       ;; buffer.
2790       (when (and (zerop (buffer-size))
2791                  (not no-display))
2792         (cond (gnus-newsgroup-dormant
2793                (gnus-summary-limit-include-dormant))
2794               ((and gnus-newsgroup-scored show-all)
2795                (gnus-summary-limit-include-expunged t))))
2796       ;; Function `gnus-apply-kill-file' must be called in this hook.
2797       (gnus-run-hooks 'gnus-apply-kill-hook)
2798       (if (and (zerop (buffer-size))
2799                (not no-display))
2800           (progn
2801             ;; This newsgroup is empty.
2802             (gnus-summary-catchup-and-exit nil t)
2803             (gnus-message 6 "No unread news")
2804             (when kill-buffer
2805               (gnus-kill-or-deaden-summary kill-buffer))
2806             ;; Return nil from this function.
2807             nil)
2808         ;; Hide conversation thread subtrees.  We cannot do this in
2809         ;; gnus-summary-prepare-hook since kill processing may not
2810         ;; work with hidden articles.
2811         (and gnus-show-threads
2812              gnus-thread-hide-subtree
2813              (gnus-summary-hide-all-threads))
2814         (when kill-buffer
2815           (gnus-kill-or-deaden-summary kill-buffer))
2816         ;; Show first unread article if requested.
2817         (if (and (not no-article)
2818                  (not no-display)
2819                  gnus-newsgroup-unreads
2820                  gnus-auto-select-first)
2821             (progn
2822               (gnus-configure-windows 'summary)
2823               (cond
2824                ((eq gnus-auto-select-first 'best)
2825                 (gnus-summary-best-unread-article))
2826                ((eq gnus-auto-select-first t)
2827                 (gnus-summary-first-unread-article))
2828                ((gnus-functionp gnus-auto-select-first)
2829                 (funcall gnus-auto-select-first))))
2830           ;; Don't select any articles, just move point to the first
2831           ;; article in the group.
2832           (goto-char (point-min))
2833           (gnus-summary-position-point)
2834           (gnus-configure-windows 'summary 'force)
2835           (gnus-set-mode-line 'summary))
2836         (when (get-buffer-window gnus-group-buffer t)
2837           ;; Gotta use windows, because recenter does weird stuff if
2838           ;; the current buffer ain't the displayed window.
2839           (let ((owin (selected-window)))
2840             (select-window (get-buffer-window gnus-group-buffer t))
2841             (when (gnus-group-goto-group group)
2842               (recenter))
2843             (select-window owin)))
2844         ;; Mark this buffer as "prepared".
2845         (setq gnus-newsgroup-prepared t)
2846         (gnus-run-hooks 'gnus-summary-prepared-hook)
2847         t)))))
2848
2849 (defun gnus-summary-prepare ()
2850   "Generate the summary buffer."
2851   (interactive)
2852   (let ((buffer-read-only nil))
2853     (erase-buffer)
2854     (setq gnus-newsgroup-data nil
2855           gnus-newsgroup-data-reverse nil)
2856     (gnus-run-hooks 'gnus-summary-generate-hook)
2857     ;; Generate the buffer, either with threads or without.
2858     (when gnus-newsgroup-headers
2859       (gnus-summary-prepare-threads
2860        (if gnus-show-threads
2861            (gnus-sort-gathered-threads
2862             (funcall gnus-summary-thread-gathering-function
2863                      (gnus-sort-threads
2864                       (gnus-cut-threads (gnus-make-threads)))))
2865          ;; Unthreaded display.
2866          (gnus-sort-articles gnus-newsgroup-headers))))
2867     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2868     ;; Call hooks for modifying summary buffer.
2869     (goto-char (point-min))
2870     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2871
2872 (defsubst gnus-general-simplify-subject (subject)
2873   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2874   (setq subject
2875         (cond
2876          ;; Truncate the subject.
2877          (gnus-simplify-subject-functions
2878           (gnus-map-function gnus-simplify-subject-functions subject))
2879          ((numberp gnus-summary-gather-subject-limit)
2880           (setq subject (gnus-simplify-subject-re subject))
2881           (if (> (length subject) gnus-summary-gather-subject-limit)
2882               (substring subject 0 gnus-summary-gather-subject-limit)
2883             subject))
2884          ;; Fuzzily simplify it.
2885          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2886           (gnus-simplify-subject-fuzzy subject))
2887          ;; Just remove the leading "Re:".
2888          (t
2889           (gnus-simplify-subject-re subject))))
2890
2891   (if (and gnus-summary-gather-exclude-subject
2892            (string-match gnus-summary-gather-exclude-subject subject))
2893       nil                               ; This article shouldn't be gathered
2894     subject))
2895
2896 (defun gnus-summary-simplify-subject-query ()
2897   "Query where the respool algorithm would put this article."
2898   (interactive)
2899   (gnus-summary-select-article)
2900   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2901
2902 (defun gnus-gather-threads-by-subject (threads)
2903   "Gather threads by looking at Subject headers."
2904   (if (not gnus-summary-make-false-root)
2905       threads
2906     (let ((hashtb (gnus-make-hashtable 1024))
2907           (prev threads)
2908           (result threads)
2909           subject hthread whole-subject)
2910       (while threads
2911         (setq subject (gnus-general-simplify-subject
2912                        (setq whole-subject (mail-header-subject
2913                                             (caar threads)))))
2914         (when subject
2915           (if (setq hthread (gnus-gethash subject hashtb))
2916               (progn
2917                 ;; We enter a dummy root into the thread, if we
2918                 ;; haven't done that already.
2919                 (unless (stringp (caar hthread))
2920                   (setcar hthread (list whole-subject (car hthread))))
2921                 ;; We add this new gathered thread to this gathered
2922                 ;; thread.
2923                 (setcdr (car hthread)
2924                         (nconc (cdar hthread) (list (car threads))))
2925                 ;; Remove it from the list of threads.
2926                 (setcdr prev (cdr threads))
2927                 (setq threads prev))
2928             ;; Enter this thread into the hash table.
2929             (gnus-sethash subject threads hashtb)))
2930         (setq prev threads)
2931         (setq threads (cdr threads)))
2932       result)))
2933
2934 (defun gnus-gather-threads-by-references (threads)
2935   "Gather threads by looking at References headers."
2936   (let ((idhashtb (gnus-make-hashtable 1024))
2937         (thhashtb (gnus-make-hashtable 1024))
2938         (prev threads)
2939         (result threads)
2940         ids references id gthread gid entered ref)
2941     (while threads
2942       (when (setq references (mail-header-references (caar threads)))
2943         (setq id (mail-header-id (caar threads))
2944               ids (gnus-split-references references)
2945               entered nil)
2946         (while (setq ref (pop ids))
2947           (setq ids (delete ref ids))
2948           (if (not (setq gid (gnus-gethash ref idhashtb)))
2949               (progn
2950                 (gnus-sethash ref id idhashtb)
2951                 (gnus-sethash id threads thhashtb))
2952             (setq gthread (gnus-gethash gid thhashtb))
2953             (unless entered
2954               ;; We enter a dummy root into the thread, if we
2955               ;; haven't done that already.
2956               (unless (stringp (caar gthread))
2957                 (setcar gthread (list (mail-header-subject (caar gthread))
2958                                       (car gthread))))
2959               ;; We add this new gathered thread to this gathered
2960               ;; thread.
2961               (setcdr (car gthread)
2962                       (nconc (cdar gthread) (list (car threads)))))
2963             ;; Add it into the thread hash table.
2964             (gnus-sethash id gthread thhashtb)
2965             (setq entered t)
2966             ;; Remove it from the list of threads.
2967             (setcdr prev (cdr threads))
2968             (setq threads prev))))
2969       (setq prev threads)
2970       (setq threads (cdr threads)))
2971     result))
2972
2973 (defun gnus-sort-gathered-threads (threads)
2974   "Sort subtreads inside each gathered thread by article number."
2975   (let ((result threads))
2976     (while threads
2977       (when (stringp (caar threads))
2978         (setcdr (car threads)
2979                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2980       (setq threads (cdr threads)))
2981     result))
2982
2983 (defun gnus-thread-loop-p (root thread)
2984   "Say whether ROOT is in THREAD."
2985   (let ((stack (list thread))
2986         (infloop 0)
2987         th)
2988     (while (setq thread (pop stack))
2989       (setq th (cdr thread))
2990       (while (and th
2991                   (not (eq (caar th) root)))
2992         (pop th))
2993       (if th
2994           ;; We have found a loop.
2995           (let (ref-dep)
2996             (setcdr thread (delq (car th) (cdr thread)))
2997             (if (boundp (setq ref-dep (intern "none"
2998                                               gnus-newsgroup-dependencies)))
2999                 (setcdr (symbol-value ref-dep)
3000                         (nconc (cdr (symbol-value ref-dep))
3001                                (list (car th))))
3002               (set ref-dep (list nil (car th))))
3003             (setq infloop 1
3004                   stack nil))
3005         ;; Push all the subthreads onto the stack.
3006         (push (cdr thread) stack)))
3007     infloop))
3008
3009 (defun gnus-make-threads ()
3010   "Go through the dependency hashtb and find the roots.  Return all threads."
3011   (let (threads)
3012     (while (catch 'infloop
3013              (mapatoms
3014               (lambda (refs)
3015                 ;; Deal with self-referencing References loops.
3016                 (when (and (car (symbol-value refs))
3017                            (not (zerop
3018                                  (apply
3019                                   '+
3020                                   (mapcar
3021                                    (lambda (thread)
3022                                      (gnus-thread-loop-p
3023                                       (car (symbol-value refs)) thread))
3024                                    (cdr (symbol-value refs)))))))
3025                   (setq threads nil)
3026                   (throw 'infloop t))
3027                 (unless (car (symbol-value refs))
3028                   ;; These threads do not refer back to any other articles,
3029                   ;; so they're roots.
3030                   (setq threads (append (cdr (symbol-value refs)) threads))))
3031               gnus-newsgroup-dependencies)))
3032     threads))
3033
3034 ;; Build the thread tree.
3035 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3036   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3037
3038 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3039 if it was already present.
3040
3041 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3042 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3043 Message-IDs will be renamed be renamed to a unique Message-ID before
3044 being entered.
3045
3046 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3047   (let* ((id (mail-header-id header))
3048          (id-dep (and id (intern id dependencies)))
3049          ref ref-dep ref-header)
3050     ;; Enter this `header' in the `dependencies' table.
3051     (cond
3052      ((not id-dep)
3053       (setq header nil))
3054      ;; The first two cases do the normal part: enter a new `header'
3055      ;; in the `dependencies' table.
3056      ((not (boundp id-dep))
3057       (set id-dep (list header)))
3058      ((null (car (symbol-value id-dep)))
3059       (setcar (symbol-value id-dep) header))
3060
3061      ;; From here the `header' was already present in the
3062      ;; `dependencies' table.
3063      (force-new
3064       ;; Overrides an existing entry;
3065       ;; just set the header part of the entry.
3066       (setcar (symbol-value id-dep) header))
3067
3068      ;; Renames the existing `header' to a unique Message-ID.
3069      ((not gnus-summary-ignore-duplicates)
3070       ;; An article with this Message-ID has already been seen.
3071       ;; We rename the Message-ID.
3072       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3073            (list header))
3074       (mail-header-set-id header id))
3075
3076      ;; The last case ignores an existing entry, except it adds any
3077      ;; additional Xrefs (in case the two articles came from different
3078      ;; servers.
3079      ;; Also sets `header' to `nil' meaning that the `dependencies'
3080      ;; table was *not* modified.
3081      (t
3082       (mail-header-set-xref
3083        (car (symbol-value id-dep))
3084        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3085                    "")
3086                (or (mail-header-xref header) "")))
3087       (setq header nil)))
3088
3089     (when header
3090       ;; First check if that we are not creating a References loop.
3091       (setq ref (gnus-parent-id (mail-header-references header)))
3092       (while (and ref
3093                   (setq ref-dep (intern-soft ref dependencies))
3094                   (boundp ref-dep)
3095                   (setq ref-header (car (symbol-value ref-dep))))
3096         (if (string= id ref)
3097             ;; Yuk!  This is a reference loop.  Make the article be a
3098             ;; root article.
3099             (progn
3100               (mail-header-set-references (car (symbol-value id-dep)) "none")
3101               (setq ref nil))
3102           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3103       (setq ref (gnus-parent-id (mail-header-references header)))
3104       (setq ref-dep (intern (or ref "none") dependencies))
3105       (if (boundp ref-dep)
3106           (setcdr (symbol-value ref-dep)
3107                   (nconc (cdr (symbol-value ref-dep))
3108                          (list (symbol-value id-dep))))
3109         (set ref-dep (list nil (symbol-value id-dep)))))
3110     header))
3111
3112 (defun gnus-build-sparse-threads ()
3113   (let ((headers gnus-newsgroup-headers)
3114         (gnus-summary-ignore-duplicates t)
3115         header references generation relations
3116         subject child end new-child date)
3117     ;; First we create an alist of generations/relations, where
3118     ;; generations is how much we trust the relation, and the relation
3119     ;; is parent/child.
3120     (gnus-message 7 "Making sparse threads...")
3121     (save-excursion
3122       (nnheader-set-temp-buffer " *gnus sparse threads*")
3123       (while (setq header (pop headers))
3124         (when (and (setq references (mail-header-references header))
3125                    (not (string= references "")))
3126           (insert references)
3127           (setq child (mail-header-id header)
3128                 subject (mail-header-subject header)
3129                 date (mail-header-date header)
3130                 generation 0)
3131           (while (search-backward ">" nil t)
3132             (setq end (1+ (point)))
3133             (when (search-backward "<" nil t)
3134               (setq new-child (buffer-substring (point) end))
3135               (push (list (incf generation)
3136                           child (setq child new-child)
3137                           subject date)
3138                     relations)))
3139           (when child
3140             (push (list (1+ generation) child nil subject) relations))
3141           (erase-buffer)))
3142       (kill-buffer (current-buffer)))
3143     ;; Sort over trustworthiness.
3144     (mapcar
3145      (lambda (relation)
3146        (when (gnus-dependencies-add-header
3147               (make-full-mail-header
3148                gnus-reffed-article-number
3149                (nth 3 relation) "" (or (nth 4 relation) "")
3150                (nth 1 relation)
3151                (or (nth 2 relation) "") 0 0 "")
3152               gnus-newsgroup-dependencies nil)
3153          (push gnus-reffed-article-number gnus-newsgroup-limit)
3154          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3155          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3156                gnus-newsgroup-reads)
3157          (decf gnus-reffed-article-number)))
3158      (sort relations 'car-less-than-car))
3159     (gnus-message 7 "Making sparse threads...done")))
3160
3161 (defun gnus-build-old-threads ()
3162   ;; Look at all the articles that refer back to old articles, and
3163   ;; fetch the headers for the articles that aren't there.  This will
3164   ;; build complete threads - if the roots haven't been expired by the
3165   ;; server, that is.
3166   (let (id heads)
3167     (mapatoms
3168      (lambda (refs)
3169        (when (not (car (symbol-value refs)))
3170          (setq heads (cdr (symbol-value refs)))
3171          (while heads
3172            (if (memq (mail-header-number (caar heads))
3173                      gnus-newsgroup-dormant)
3174                (setq heads (cdr heads))
3175              (setq id (symbol-name refs))
3176              (while (and (setq id (gnus-build-get-header id))
3177                          (not (car (gnus-id-to-thread id)))))
3178              (setq heads nil)))))
3179      gnus-newsgroup-dependencies)))
3180
3181 ;; This function has to be called with point after the article number
3182 ;; on the beginning of the line.
3183 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3184   (let ((eol (gnus-point-at-eol))
3185         (buffer (current-buffer))
3186         header)
3187
3188     ;; overview: [num subject from date id refs chars lines misc]
3189     (unwind-protect
3190         (progn
3191           (narrow-to-region (point) eol)
3192           (unless (eobp)
3193             (forward-char))
3194
3195           (setq header
3196                 (make-full-mail-header
3197                  number                         ; number
3198                  (funcall gnus-decode-encoded-word-function
3199                           (nnheader-nov-field)) ; subject
3200                  (funcall gnus-decode-encoded-word-function
3201                           (nnheader-nov-field)) ; from
3202                  (nnheader-nov-field)           ; date
3203                  (nnheader-nov-read-message-id) ; id
3204                  (nnheader-nov-field)           ; refs
3205                  (nnheader-nov-read-integer)    ; chars
3206                  (nnheader-nov-read-integer)    ; lines
3207                  (unless (eobp)
3208                    (nnheader-nov-field))                ; misc
3209                  (nnheader-nov-parse-extra))))  ; extra
3210
3211       (widen))
3212
3213     (when gnus-alter-header-function
3214       (funcall gnus-alter-header-function header))
3215     (gnus-dependencies-add-header header dependencies force-new)))
3216
3217 (defun gnus-build-get-header (id)
3218   ;; Look through the buffer of NOV lines and find the header to
3219   ;; ID.  Enter this line into the dependencies hash table, and return
3220   ;; the id of the parent article (if any).
3221   (let ((deps gnus-newsgroup-dependencies)
3222         found header)
3223     (prog1
3224         (save-excursion
3225           (set-buffer nntp-server-buffer)
3226           (let ((case-fold-search nil))
3227             (goto-char (point-min))
3228             (while (and (not found)
3229                         (search-forward id nil t))
3230               (beginning-of-line)
3231               (setq found (looking-at
3232                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3233                                    (regexp-quote id))))
3234               (or found (beginning-of-line 2)))
3235             (when found
3236               (beginning-of-line)
3237               (and
3238                (setq header (gnus-nov-parse-line
3239                              (read (current-buffer)) deps))
3240                (gnus-parent-id (mail-header-references header))))))
3241       (when header
3242         (let ((number (mail-header-number header)))
3243           (push number gnus-newsgroup-limit)
3244           (push header gnus-newsgroup-headers)
3245           (if (memq number gnus-newsgroup-unselected)
3246               (progn
3247                 (push number gnus-newsgroup-unreads)
3248                 (setq gnus-newsgroup-unselected
3249                       (delq number gnus-newsgroup-unselected)))
3250             (push number gnus-newsgroup-ancient)))))))
3251
3252 (defun gnus-build-all-threads ()
3253   "Read all the headers."
3254   (let ((gnus-summary-ignore-duplicates t)
3255         (dependencies gnus-newsgroup-dependencies)
3256         header article)
3257     (save-excursion
3258       (set-buffer nntp-server-buffer)
3259       (let ((case-fold-search nil))
3260         (goto-char (point-min))
3261         (while (not (eobp))
3262           (ignore-errors
3263             (setq article (read (current-buffer))
3264                   header (gnus-nov-parse-line
3265                           article dependencies)))
3266           (when header
3267             (save-excursion
3268               (set-buffer gnus-summary-buffer)
3269               (push header gnus-newsgroup-headers)
3270               (if (memq (setq article (mail-header-number header))
3271                         gnus-newsgroup-unselected)
3272                   (progn
3273                     (push article gnus-newsgroup-unreads)
3274                     (setq gnus-newsgroup-unselected
3275                           (delq article gnus-newsgroup-unselected)))
3276                 (push article gnus-newsgroup-ancient)))
3277             (forward-line 1)))))))
3278
3279 (defun gnus-summary-update-article-line (article header)
3280   "Update the line for ARTICLE using HEADERS."
3281   (let* ((id (mail-header-id header))
3282          (thread (gnus-id-to-thread id)))
3283     (unless thread
3284       (error "Article in no thread"))
3285     ;; Update the thread.
3286     (setcar thread header)
3287     (gnus-summary-goto-subject article)
3288     (let* ((datal (gnus-data-find-list article))
3289            (data (car datal))
3290            (length (when (cdr datal)
3291                      (- (gnus-data-pos data)
3292                         (gnus-data-pos (cadr datal)))))
3293            (buffer-read-only nil)
3294            (level (gnus-summary-thread-level)))
3295       (gnus-delete-line)
3296       (gnus-summary-insert-line
3297        header level nil (gnus-article-mark article)
3298        (memq article gnus-newsgroup-replied)
3299        (memq article gnus-newsgroup-expirable)
3300        ;; Only insert the Subject string when it's different
3301        ;; from the previous Subject string.
3302        (if (gnus-subject-equal
3303             (condition-case ()
3304                 (mail-header-subject
3305                  (gnus-data-header
3306                   (cadr
3307                    (gnus-data-find-list
3308                     article
3309                     (gnus-data-list t)))))
3310               ;; Error on the side of excessive subjects.
3311               (error ""))
3312             (mail-header-subject header))
3313            ""
3314          (mail-header-subject header))
3315        nil (cdr (assq article gnus-newsgroup-scored))
3316        (memq article gnus-newsgroup-processable))
3317       (when length
3318         (gnus-data-update-list
3319          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3320
3321 (defun gnus-summary-update-article (article &optional iheader)
3322   "Update ARTICLE in the summary buffer."
3323   (set-buffer gnus-summary-buffer)
3324   (let* ((header (gnus-summary-article-header article))
3325          (id (mail-header-id header))
3326          (data (gnus-data-find article))
3327          (thread (gnus-id-to-thread id))
3328          (references (mail-header-references header))
3329          (parent
3330           (gnus-id-to-thread
3331            (or (gnus-parent-id
3332                 (when (and references
3333                            (not (equal "" references)))
3334                   references))
3335                "none")))
3336          (buffer-read-only nil)
3337          (old (car thread)))
3338     (when thread
3339       (unless iheader
3340         (setcar thread nil)
3341         (when parent
3342           (delq thread parent)))
3343       (if (gnus-summary-insert-subject id header)
3344           ;; Set the (possibly) new article number in the data structure.
3345           (gnus-data-set-number data (gnus-id-to-article id))
3346         (setcar thread old)
3347         nil))))
3348
3349 (defun gnus-rebuild-thread (id &optional line)
3350   "Rebuild the thread containing ID.
3351 If LINE, insert the rebuilt thread starting on line LINE."
3352   (let ((buffer-read-only nil)
3353         old-pos current thread data)
3354     (if (not gnus-show-threads)
3355         (setq thread (list (car (gnus-id-to-thread id))))
3356       ;; Get the thread this article is part of.
3357       (setq thread (gnus-remove-thread id)))
3358     (setq old-pos (gnus-point-at-bol))
3359     (setq current (save-excursion
3360                     (and (zerop (forward-line -1))
3361                          (gnus-summary-article-number))))
3362     ;; If this is a gathered thread, we have to go some re-gathering.
3363     (when (stringp (car thread))
3364       (let ((subject (car thread))
3365             roots thr)
3366         (setq thread (cdr thread))
3367         (while thread
3368           (unless (memq (setq thr (gnus-id-to-thread
3369                                    (gnus-root-id
3370                                     (mail-header-id (caar thread)))))
3371                         roots)
3372             (push thr roots))
3373           (setq thread (cdr thread)))
3374         ;; We now have all (unique) roots.
3375         (if (= (length roots) 1)
3376             ;; All the loose roots are now one solid root.
3377             (setq thread (car roots))
3378           (setq thread (cons subject (gnus-sort-threads roots))))))
3379     (let (threads)
3380       ;; We then insert this thread into the summary buffer.
3381       (when line
3382         (goto-char (point-min))
3383         (forward-line (1- line)))
3384       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3385         (if gnus-show-threads
3386             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3387           (gnus-summary-prepare-unthreaded thread))
3388         (setq data (nreverse gnus-newsgroup-data))
3389         (setq threads gnus-newsgroup-threads))
3390       ;; We splice the new data into the data structure.
3391       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3392       ;;!!! then we want to insert at the beginning of the buffer.
3393       ;;!!! That happens to be true with Gnus now, but that may
3394       ;;!!! change in the future.  Perhaps.
3395       (gnus-data-enter-list
3396        (if line nil current) data (- (point) old-pos))
3397       (setq gnus-newsgroup-threads
3398             (nconc threads gnus-newsgroup-threads))
3399       (gnus-data-compute-positions))))
3400
3401 (defun gnus-number-to-header (number)
3402   "Return the header for article NUMBER."
3403   (let ((headers gnus-newsgroup-headers))
3404     (while (and headers
3405                 (not (= number (mail-header-number (car headers)))))
3406       (pop headers))
3407     (when headers
3408       (car headers))))
3409
3410 (defun gnus-parent-headers (in-headers &optional generation)
3411   "Return the headers of the GENERATIONeth parent of HEADERS."
3412   (unless generation
3413     (setq generation 1))
3414   (let ((parent t)
3415         (headers in-headers)
3416         references)
3417     (while (and parent
3418                 (not (zerop generation))
3419                 (setq references (mail-header-references headers)))
3420       (setq headers (if (and references
3421                              (setq parent (gnus-parent-id references)))
3422                         (car (gnus-id-to-thread parent))
3423                       nil))
3424       (decf generation))
3425     (and (not (eq headers in-headers))
3426          headers)))
3427
3428 (defun gnus-id-to-thread (id)
3429   "Return the (sub-)thread where ID appears."
3430   (gnus-gethash id gnus-newsgroup-dependencies))
3431
3432 (defun gnus-id-to-article (id)
3433   "Return the article number of ID."
3434   (let ((thread (gnus-id-to-thread id)))
3435     (when (and thread
3436                (car thread))
3437       (mail-header-number (car thread)))))
3438
3439 (defun gnus-id-to-header (id)
3440   "Return the article headers of ID."
3441   (car (gnus-id-to-thread id)))
3442
3443 (defun gnus-article-displayed-root-p (article)
3444   "Say whether ARTICLE is a root(ish) article."
3445   (let ((level (gnus-summary-thread-level article))
3446         (refs (mail-header-references  (gnus-summary-article-header article)))
3447         particle)
3448     (cond
3449      ((null level) nil)
3450      ((zerop level) t)
3451      ((null refs) t)
3452      ((null (gnus-parent-id refs)) t)
3453      ((and (= 1 level)
3454            (null (setq particle (gnus-id-to-article
3455                                  (gnus-parent-id refs))))
3456            (null (gnus-summary-thread-level particle)))))))
3457
3458 (defun gnus-root-id (id)
3459   "Return the id of the root of the thread where ID appears."
3460   (let (last-id prev)
3461     (while (and id (setq prev (car (gnus-id-to-thread id))))
3462       (setq last-id id
3463             id (gnus-parent-id (mail-header-references prev))))
3464     last-id))
3465
3466 (defun gnus-articles-in-thread (thread)
3467   "Return the list of articles in THREAD."
3468   (cons (mail-header-number (car thread))
3469         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3470
3471 (defun gnus-remove-thread (id &optional dont-remove)
3472   "Remove the thread that has ID in it."
3473   (let (headers thread last-id)
3474     ;; First go up in this thread until we find the root.
3475     (setq last-id (gnus-root-id id)
3476           headers (message-flatten-list (gnus-id-to-thread last-id)))
3477     ;; We have now found the real root of this thread.  It might have
3478     ;; been gathered into some loose thread, so we have to search
3479     ;; through the threads to find the thread we wanted.
3480     (let ((threads gnus-newsgroup-threads)
3481           sub)
3482       (while threads
3483         (setq sub (car threads))
3484         (if (stringp (car sub))
3485             ;; This is a gathered thread, so we look at the roots
3486             ;; below it to find whether this article is in this
3487             ;; gathered root.
3488             (progn
3489               (setq sub (cdr sub))
3490               (while sub
3491                 (when (member (caar sub) headers)
3492                   (setq thread (car threads)
3493                         threads nil
3494                         sub nil))
3495                 (setq sub (cdr sub))))
3496           ;; It's an ordinary thread, so we check it.
3497           (when (eq (car sub) (car headers))
3498             (setq thread sub
3499                   threads nil)))
3500         (setq threads (cdr threads)))
3501       ;; If this article is in no thread, then it's a root.
3502       (if thread
3503           (unless dont-remove
3504             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3505         (setq thread (gnus-id-to-thread last-id)))
3506       (when thread
3507         (prog1
3508             thread                      ; We return this thread.
3509           (unless dont-remove
3510             (if (stringp (car thread))
3511                 (progn
3512                   ;; If we use dummy roots, then we have to remove the
3513                   ;; dummy root as well.
3514                   (when (eq gnus-summary-make-false-root 'dummy)
3515                     ;; We go to the dummy root by going to
3516                     ;; the first sub-"thread", and then one line up.
3517                     (gnus-summary-goto-article
3518                      (mail-header-number (caadr thread)))
3519                     (forward-line -1)
3520                     (gnus-delete-line)
3521                     (gnus-data-compute-positions))
3522                   (setq thread (cdr thread))
3523                   (while thread
3524                     (gnus-remove-thread-1 (car thread))
3525                     (setq thread (cdr thread))))
3526               (gnus-summary-show-all-threads)
3527               (gnus-remove-thread-1 thread))))))))
3528
3529 (defun gnus-remove-thread-1 (thread)
3530   "Remove the thread THREAD recursively."
3531   (let ((number (mail-header-number (pop thread)))
3532         d)
3533     (setq thread (reverse thread))
3534     (while thread
3535       (gnus-remove-thread-1 (pop thread)))
3536     (when (setq d (gnus-data-find number))
3537       (goto-char (gnus-data-pos d))
3538       (gnus-data-remove
3539        number
3540        (- (gnus-point-at-bol)
3541           (prog1
3542               (1+ (gnus-point-at-eol))
3543             (gnus-delete-line)))))))
3544
3545 (defun gnus-sort-threads (threads)
3546   "Sort THREADS."
3547   (if (not gnus-thread-sort-functions)
3548       threads
3549     (gnus-message 8 "Sorting threads...")
3550     (prog1
3551         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3552       (gnus-message 8 "Sorting threads...done"))))
3553
3554 (defun gnus-sort-articles (articles)
3555   "Sort ARTICLES."
3556   (when gnus-article-sort-functions
3557     (gnus-message 7 "Sorting articles...")
3558     (prog1
3559         (setq gnus-newsgroup-headers
3560               (sort articles (gnus-make-sort-function
3561                               gnus-article-sort-functions)))
3562       (gnus-message 7 "Sorting articles...done"))))
3563
3564 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3565 (defmacro gnus-thread-header (thread)
3566   ;; Return header of first article in THREAD.
3567   ;; Note that THREAD must never, ever be anything else than a variable -
3568   ;; using some other form will lead to serious barfage.
3569   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3570   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3571   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3572         (vector thread) 2))
3573
3574 (defsubst gnus-article-sort-by-number (h1 h2)
3575   "Sort articles by article number."
3576   (< (mail-header-number h1)
3577      (mail-header-number h2)))
3578
3579 (defun gnus-thread-sort-by-number (h1 h2)
3580   "Sort threads by root article number."
3581   (gnus-article-sort-by-number
3582    (gnus-thread-header h1) (gnus-thread-header h2)))
3583
3584 (defsubst gnus-article-sort-by-lines (h1 h2)
3585   "Sort articles by article Lines header."
3586   (< (mail-header-lines h1)
3587      (mail-header-lines h2)))
3588
3589 (defun gnus-thread-sort-by-lines (h1 h2)
3590   "Sort threads by root article Lines header."
3591   (gnus-article-sort-by-lines
3592    (gnus-thread-header h1) (gnus-thread-header h2)))
3593
3594 (defsubst gnus-article-sort-by-chars (h1 h2)
3595   "Sort articles by octet length."
3596   (< (mail-header-chars h1)
3597      (mail-header-chars h2)))
3598
3599 (defun gnus-thread-sort-by-chars (h1 h2)
3600   "Sort threads by root article octet length."
3601   (gnus-article-sort-by-chars
3602    (gnus-thread-header h1) (gnus-thread-header h2)))
3603
3604 (defsubst gnus-article-sort-by-author (h1 h2)
3605   "Sort articles by root author."
3606   (string-lessp
3607    (let ((extract (funcall
3608                    gnus-extract-address-components
3609                    (mail-header-from h1))))
3610      (or (car extract) (cadr extract) ""))
3611    (let ((extract (funcall
3612                    gnus-extract-address-components
3613                    (mail-header-from h2))))
3614      (or (car extract) (cadr extract) ""))))
3615
3616 (defun gnus-thread-sort-by-author (h1 h2)
3617   "Sort threads by root author."
3618   (gnus-article-sort-by-author
3619    (gnus-thread-header h1)  (gnus-thread-header h2)))
3620
3621 (defsubst gnus-article-sort-by-subject (h1 h2)
3622   "Sort articles by root subject."
3623   (string-lessp
3624    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3625    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3626
3627 (defun gnus-thread-sort-by-subject (h1 h2)
3628   "Sort threads by root subject."
3629   (gnus-article-sort-by-subject
3630    (gnus-thread-header h1) (gnus-thread-header h2)))
3631
3632 (defsubst gnus-article-sort-by-date (h1 h2)
3633   "Sort articles by root article date."
3634   (time-less-p
3635    (gnus-date-get-time (mail-header-date h1))
3636    (gnus-date-get-time (mail-header-date h2))))
3637
3638 (defun gnus-thread-sort-by-date (h1 h2)
3639   "Sort threads by root article date."
3640   (gnus-article-sort-by-date
3641    (gnus-thread-header h1) (gnus-thread-header h2)))
3642
3643 (defsubst gnus-article-sort-by-score (h1 h2)
3644   "Sort articles by root article score.
3645 Unscored articles will be counted as having a score of zero."
3646   (> (or (cdr (assq (mail-header-number h1)
3647                     gnus-newsgroup-scored))
3648          gnus-summary-default-score 0)
3649      (or (cdr (assq (mail-header-number h2)
3650                     gnus-newsgroup-scored))
3651          gnus-summary-default-score 0)))
3652
3653 (defun gnus-thread-sort-by-score (h1 h2)
3654   "Sort threads by root article score."
3655   (gnus-article-sort-by-score
3656    (gnus-thread-header h1) (gnus-thread-header h2)))
3657
3658 (defun gnus-thread-sort-by-total-score (h1 h2)
3659   "Sort threads by the sum of all scores in the thread.
3660 Unscored articles will be counted as having a score of zero."
3661   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3662
3663 (defun gnus-thread-total-score (thread)
3664   ;; This function find the total score of THREAD.
3665   (cond ((null thread)
3666          0)
3667         ((consp thread)
3668          (if (stringp (car thread))
3669              (apply gnus-thread-score-function 0
3670                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3671            (gnus-thread-total-score-1 thread)))
3672         (t
3673          (gnus-thread-total-score-1 (list thread)))))
3674
3675 (defun gnus-thread-total-score-1 (root)
3676   ;; This function find the total score of the thread below ROOT.
3677   (setq root (car root))
3678   (apply gnus-thread-score-function
3679          (or (append
3680               (mapcar 'gnus-thread-total-score
3681                       (cdr (gnus-id-to-thread (mail-header-id root))))
3682               (when (> (mail-header-number root) 0)
3683                 (list (or (cdr (assq (mail-header-number root)
3684                                      gnus-newsgroup-scored))
3685                           gnus-summary-default-score 0))))
3686              (list gnus-summary-default-score)
3687              '(0))))
3688
3689 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3690 (defvar gnus-tmp-prev-subject nil)
3691 (defvar gnus-tmp-false-parent nil)
3692 (defvar gnus-tmp-root-expunged nil)
3693 (defvar gnus-tmp-dummy-line nil)
3694
3695 (defvar gnus-tmp-header)
3696 (defun gnus-extra-header (type &optional header)
3697   "Return the extra header of TYPE."
3698   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3699       ""))
3700
3701 (defun gnus-summary-prepare-threads (threads)
3702   "Prepare summary buffer from THREADS and indentation LEVEL.
3703 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3704 or a straight list of headers."
3705   (gnus-message 7 "Generating summary...")
3706
3707   (setq gnus-newsgroup-threads threads)
3708   (beginning-of-line)
3709
3710   (let ((gnus-tmp-level 0)
3711         (default-score (or gnus-summary-default-score 0))
3712         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3713         thread number subject stack state gnus-tmp-gathered beg-match
3714         new-roots gnus-tmp-new-adopts thread-end
3715         gnus-tmp-header gnus-tmp-unread
3716         gnus-tmp-replied gnus-tmp-subject-or-nil
3717         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3718         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3719         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3720
3721     (setq gnus-tmp-prev-subject nil)
3722
3723     (if (vectorp (car threads))
3724         ;; If this is a straight (sic) list of headers, then a
3725         ;; threaded summary display isn't required, so we just create
3726         ;; an unthreaded one.
3727         (gnus-summary-prepare-unthreaded threads)
3728
3729       ;; Do the threaded display.
3730
3731       (while (or threads stack gnus-tmp-new-adopts new-roots)
3732
3733         (if (and (= gnus-tmp-level 0)
3734                  (or (not stack)
3735                      (= (caar stack) 0))
3736                  (not gnus-tmp-false-parent)
3737                  (or gnus-tmp-new-adopts new-roots))
3738             (if gnus-tmp-new-adopts
3739                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3740                       thread (list (car gnus-tmp-new-adopts))
3741                       gnus-tmp-header (caar thread)
3742                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3743               (when new-roots
3744                 (setq thread (list (car new-roots))
3745                       gnus-tmp-header (caar thread)
3746                       new-roots (cdr new-roots))))
3747
3748           (if threads
3749               ;; If there are some threads, we do them before the
3750               ;; threads on the stack.
3751               (setq thread threads
3752                     gnus-tmp-header (caar thread))
3753             ;; There were no current threads, so we pop something off
3754             ;; the stack.
3755             (setq state (car stack)
3756                   gnus-tmp-level (car state)
3757                   thread (cdr state)
3758                   stack (cdr stack)
3759                   gnus-tmp-header (caar thread))))
3760
3761         (setq gnus-tmp-false-parent nil)
3762         (setq gnus-tmp-root-expunged nil)
3763         (setq thread-end nil)
3764
3765         (if (stringp gnus-tmp-header)
3766             ;; The header is a dummy root.
3767             (cond
3768              ((eq gnus-summary-make-false-root 'adopt)
3769               ;; We let the first article adopt the rest.
3770               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3771                                                (cddar thread)))
3772               (setq gnus-tmp-gathered
3773                     (nconc (mapcar
3774                             (lambda (h) (mail-header-number (car h)))
3775                             (cddar thread))
3776                            gnus-tmp-gathered))
3777               (setq thread (cons (list (caar thread)
3778                                        (cadar thread))
3779                                  (cdr thread)))
3780               (setq gnus-tmp-level -1
3781                     gnus-tmp-false-parent t))
3782              ((eq gnus-summary-make-false-root 'empty)
3783               ;; We print adopted articles with empty subject fields.
3784               (setq gnus-tmp-gathered
3785                     (nconc (mapcar
3786                             (lambda (h) (mail-header-number (car h)))
3787                             (cddar thread))
3788                            gnus-tmp-gathered))
3789               (setq gnus-tmp-level -1))
3790              ((eq gnus-summary-make-false-root 'dummy)
3791               ;; We remember that we probably want to output a dummy
3792               ;; root.
3793               (setq gnus-tmp-dummy-line gnus-tmp-header)
3794               (setq gnus-tmp-prev-subject gnus-tmp-header))
3795              (t
3796               ;; We do not make a root for the gathered
3797               ;; sub-threads at all.
3798               (setq gnus-tmp-level -1)))
3799
3800           (setq number (mail-header-number gnus-tmp-header)
3801                 subject (mail-header-subject gnus-tmp-header))
3802
3803           (cond
3804            ;; If the thread has changed subject, we might want to make
3805            ;; this subthread into a root.
3806            ((and (null gnus-thread-ignore-subject)
3807                  (not (zerop gnus-tmp-level))
3808                  gnus-tmp-prev-subject
3809                  (not (inline
3810                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3811             (setq new-roots (nconc new-roots (list (car thread)))
3812                   thread-end t
3813                   gnus-tmp-header nil))
3814            ;; If the article lies outside the current limit,
3815            ;; then we do not display it.
3816            ((not (memq number gnus-newsgroup-limit))
3817             (setq gnus-tmp-gathered
3818                   (nconc (mapcar
3819                           (lambda (h) (mail-header-number (car h)))
3820                           (cdar thread))
3821                          gnus-tmp-gathered))
3822             (setq gnus-tmp-new-adopts (if (cdar thread)
3823                                           (append gnus-tmp-new-adopts
3824                                                   (cdar thread))
3825                                         gnus-tmp-new-adopts)
3826                   thread-end t
3827                   gnus-tmp-header nil)
3828             (when (zerop gnus-tmp-level)
3829               (setq gnus-tmp-root-expunged t)))
3830            ;; Perhaps this article is to be marked as read?
3831            ((and gnus-summary-mark-below
3832                  (< (or (cdr (assq number gnus-newsgroup-scored))
3833                         default-score)
3834                     gnus-summary-mark-below)
3835                  ;; Don't touch sparse articles.
3836                  (not (gnus-summary-article-sparse-p number))
3837                  (not (gnus-summary-article-ancient-p number)))
3838             (setq gnus-newsgroup-unreads
3839                   (delq number gnus-newsgroup-unreads))
3840             (if gnus-newsgroup-auto-expire
3841                 (push number gnus-newsgroup-expirable)
3842               (push (cons number gnus-low-score-mark)
3843                     gnus-newsgroup-reads))))
3844
3845           (when gnus-tmp-header
3846             ;; We may have an old dummy line to output before this
3847             ;; article.
3848             (when (and gnus-tmp-dummy-line
3849                        (gnus-subject-equal
3850                         gnus-tmp-dummy-line
3851                         (mail-header-subject gnus-tmp-header)))
3852               (gnus-summary-insert-dummy-line
3853                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3854               (setq gnus-tmp-dummy-line nil))
3855
3856             ;; Compute the mark.
3857             (setq gnus-tmp-unread (gnus-article-mark number))
3858
3859             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3860                                   gnus-tmp-header gnus-tmp-level)
3861                   gnus-newsgroup-data)
3862
3863             ;; Actually insert the line.
3864             (setq
3865              gnus-tmp-subject-or-nil
3866              (cond
3867               ((and gnus-thread-ignore-subject
3868                     gnus-tmp-prev-subject
3869                     (not (inline (gnus-subject-equal
3870                                   gnus-tmp-prev-subject subject))))
3871                subject)
3872               ((zerop gnus-tmp-level)
3873                (if (and (eq gnus-summary-make-false-root 'empty)
3874                         (memq number gnus-tmp-gathered)
3875                         gnus-tmp-prev-subject
3876                         (inline (gnus-subject-equal
3877                                  gnus-tmp-prev-subject subject)))
3878                    gnus-summary-same-subject
3879                  subject))
3880               (t gnus-summary-same-subject)))
3881             (if (and (eq gnus-summary-make-false-root 'adopt)
3882                      (= gnus-tmp-level 1)
3883                      (memq number gnus-tmp-gathered))
3884                 (setq gnus-tmp-opening-bracket ?\<
3885                       gnus-tmp-closing-bracket ?\>)
3886               (setq gnus-tmp-opening-bracket ?\[
3887                     gnus-tmp-closing-bracket ?\]))
3888             (setq
3889              gnus-tmp-indentation
3890              (aref gnus-thread-indent-array gnus-tmp-level)
3891              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3892              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3893                                 gnus-summary-default-score 0)
3894              gnus-tmp-score-char
3895              (if (or (null gnus-summary-default-score)
3896                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3897                          gnus-summary-zcore-fuzz))
3898                  ? ;Whitespace
3899                (if (< gnus-tmp-score gnus-summary-default-score)
3900                    gnus-score-below-mark gnus-score-over-mark))
3901              gnus-tmp-replied
3902              (cond ((memq number gnus-newsgroup-processable)
3903                     gnus-process-mark)
3904                    ((memq number gnus-newsgroup-cached)
3905                     gnus-cached-mark)
3906                    ((memq number gnus-newsgroup-replied)
3907                     gnus-replied-mark)
3908                    ((memq number gnus-newsgroup-saved)
3909                     gnus-saved-mark)
3910                    (t gnus-unread-mark))
3911              gnus-tmp-from (mail-header-from gnus-tmp-header)
3912              gnus-tmp-name
3913              (cond
3914               ((string-match "<[^>]+> *$" gnus-tmp-from)
3915                (setq beg-match (match-beginning 0))
3916                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3917                         (substring gnus-tmp-from (1+ (match-beginning 0))
3918                                    (1- (match-end 0))))
3919                    (substring gnus-tmp-from 0 beg-match)))
3920               ((string-match "(.+)" gnus-tmp-from)
3921                (substring gnus-tmp-from
3922                           (1+ (match-beginning 0)) (1- (match-end 0))))
3923               (t gnus-tmp-from)))
3924             (when (string= gnus-tmp-name "")
3925               (setq gnus-tmp-name gnus-tmp-from))
3926             (unless (numberp gnus-tmp-lines)
3927               (setq gnus-tmp-lines 0))
3928             (gnus-put-text-property
3929              (point)
3930              (progn (eval gnus-summary-line-format-spec) (point))
3931              'gnus-number number)
3932             (when gnus-visual-p
3933               (forward-line -1)
3934               (gnus-run-hooks 'gnus-summary-update-hook)
3935               (forward-line 1))
3936
3937             (setq gnus-tmp-prev-subject subject)))
3938
3939         (when (nth 1 thread)
3940           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3941         (incf gnus-tmp-level)
3942         (setq threads (if thread-end nil (cdar thread)))
3943         (unless threads
3944           (setq gnus-tmp-level 0)))))
3945   (gnus-message 7 "Generating summary...done"))
3946
3947 (defun gnus-summary-prepare-unthreaded (headers)
3948   "Generate an unthreaded summary buffer based on HEADERS."
3949   (let (header number mark)
3950
3951     (beginning-of-line)
3952
3953     (while headers
3954       ;; We may have to root out some bad articles...
3955       (when (memq (setq number (mail-header-number
3956                                 (setq header (pop headers))))
3957                   gnus-newsgroup-limit)
3958         ;; Mark article as read when it has a low score.
3959         (when (and gnus-summary-mark-below
3960                    (< (or (cdr (assq number gnus-newsgroup-scored))
3961                           gnus-summary-default-score 0)
3962                       gnus-summary-mark-below)
3963                    (not (gnus-summary-article-ancient-p number)))
3964           (setq gnus-newsgroup-unreads
3965                 (delq number gnus-newsgroup-unreads))
3966           (if gnus-newsgroup-auto-expire
3967               (push number gnus-newsgroup-expirable)
3968             (push (cons number gnus-low-score-mark)
3969                   gnus-newsgroup-reads)))
3970
3971         (setq mark (gnus-article-mark number))
3972         (push (gnus-data-make number mark (1+ (point)) header 0)
3973               gnus-newsgroup-data)
3974         (gnus-summary-insert-line
3975          header 0 number
3976          mark (memq number gnus-newsgroup-replied)
3977          (memq number gnus-newsgroup-expirable)
3978          (mail-header-subject header) nil
3979          (cdr (assq number gnus-newsgroup-scored))
3980          (memq number gnus-newsgroup-processable))))))
3981
3982 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3983   "Select newsgroup GROUP.
3984 If READ-ALL is non-nil, all articles in the group are selected.
3985 If SELECT-ARTICLES, only select those articles from GROUP."
3986   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3987          ;;!!! Dirty hack; should be removed.
3988          (gnus-summary-ignore-duplicates
3989           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3990               t
3991             gnus-summary-ignore-duplicates))
3992          (info (nth 2 entry))
3993          articles fetched-articles cached)
3994
3995     (unless (gnus-check-server
3996              (setq gnus-current-select-method
3997                    (gnus-find-method-for-group group)))
3998       (error "Couldn't open server"))
3999
4000     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4001         (gnus-activate-group group)     ; Or we can activate it...
4002         (progn                          ; Or we bug out.
4003           (when (equal major-mode 'gnus-summary-mode)
4004             (kill-buffer (current-buffer)))
4005           (error "Couldn't request group %s: %s"
4006                  group (gnus-status-message group))))
4007
4008     (unless (gnus-request-group group t)
4009       (when (equal major-mode 'gnus-summary-mode)
4010         (kill-buffer (current-buffer)))
4011       (error "Couldn't request group %s: %s"
4012              group (gnus-status-message group)))
4013
4014     (setq gnus-newsgroup-name group)
4015     (setq gnus-newsgroup-unselected nil)
4016     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4017     (gnus-summary-setup-default-charset)
4018
4019     ;; Adjust and set lists of article marks.
4020     (when info
4021       (gnus-adjust-marked-articles info))
4022
4023     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4024     (when (gnus-virtual-group-p group)
4025       (setq cached gnus-newsgroup-cached))
4026
4027     (setq gnus-newsgroup-unreads
4028           (gnus-set-difference
4029            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4030            gnus-newsgroup-dormant))
4031
4032     (setq gnus-newsgroup-processable nil)
4033
4034     (gnus-update-read-articles group gnus-newsgroup-unreads)
4035
4036     (if (setq articles select-articles)
4037         (setq gnus-newsgroup-unselected
4038               (gnus-sorted-intersection
4039                gnus-newsgroup-unreads
4040                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4041       (setq articles (gnus-articles-to-read group read-all)))
4042
4043     (cond
4044      ((null articles)
4045       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4046       'quit)
4047      ((eq articles 0) nil)
4048      (t
4049       ;; Init the dependencies hash table.
4050       (setq gnus-newsgroup-dependencies
4051             (gnus-make-hashtable (length articles)))
4052       (gnus-set-global-variables)
4053       ;; Retrieve the headers and read them in.
4054       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4055       (setq gnus-newsgroup-headers
4056             (if (eq 'nov
4057                     (setq gnus-headers-retrieved-by
4058                           (gnus-retrieve-headers
4059                            articles gnus-newsgroup-name
4060                            ;; We might want to fetch old headers, but
4061                            ;; not if there is only 1 article.
4062                            (and (or (and
4063                                      (not (eq gnus-fetch-old-headers 'some))
4064                                      (not (numberp gnus-fetch-old-headers)))
4065                                     (> (length articles) 1))
4066                                 gnus-fetch-old-headers))))
4067                 (gnus-get-newsgroup-headers-xover
4068                  articles nil nil gnus-newsgroup-name t)
4069               (gnus-get-newsgroup-headers)))
4070       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4071
4072       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4073       (when cached
4074         (setq gnus-newsgroup-cached cached))
4075
4076       ;; Suppress duplicates?
4077       (when gnus-suppress-duplicates
4078         (gnus-dup-suppress-articles))
4079
4080       ;; Set the initial limit.
4081       (setq gnus-newsgroup-limit (copy-sequence articles))
4082       ;; Remove canceled articles from the list of unread articles.
4083       (setq gnus-newsgroup-unreads
4084             (gnus-set-sorted-intersection
4085              gnus-newsgroup-unreads
4086              (setq fetched-articles
4087                    (mapcar (lambda (headers) (mail-header-number headers))
4088                            gnus-newsgroup-headers))))
4089       ;; Removed marked articles that do not exist.
4090       (gnus-update-missing-marks
4091        (gnus-sorted-complement fetched-articles articles))
4092       ;; We might want to build some more threads first.
4093       (when (and gnus-fetch-old-headers
4094                  (eq gnus-headers-retrieved-by 'nov))
4095         (if (eq gnus-fetch-old-headers 'invisible)
4096             (gnus-build-all-threads)
4097           (gnus-build-old-threads)))
4098       ;; Let the Gnus agent mark articles as read.
4099       (when gnus-agent
4100         (gnus-agent-get-undownloaded-list))
4101       ;; Check whether auto-expire is to be done in this group.
4102       (setq gnus-newsgroup-auto-expire
4103             (gnus-group-auto-expirable-p group))
4104       ;; Set up the article buffer now, if necessary.
4105       (unless gnus-single-article-buffer
4106         (gnus-article-setup-buffer))
4107       ;; First and last article in this newsgroup.
4108       (when gnus-newsgroup-headers
4109         (setq gnus-newsgroup-begin
4110               (mail-header-number (car gnus-newsgroup-headers))
4111               gnus-newsgroup-end
4112               (mail-header-number
4113                (gnus-last-element gnus-newsgroup-headers))))
4114       ;; GROUP is successfully selected.
4115       (or gnus-newsgroup-headers t)))))
4116
4117 (defun gnus-articles-to-read (group &optional read-all)
4118   ;; Find out what articles the user wants to read.
4119   (let* ((articles
4120           ;; Select all articles if `read-all' is non-nil, or if there
4121           ;; are no unread articles.
4122           (if (or read-all
4123                   (and (zerop (length gnus-newsgroup-marked))
4124                        (zerop (length gnus-newsgroup-unreads)))
4125                   (eq (gnus-group-find-parameter group 'display)
4126                       'all))
4127               (gnus-uncompress-range (gnus-active group))
4128             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4129                           (copy-sequence gnus-newsgroup-unreads))
4130                   '<)))
4131          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4132          (scored (length scored-list))
4133          (number (length articles))
4134          (marked (+ (length gnus-newsgroup-marked)
4135                     (length gnus-newsgroup-dormant)))
4136          (select
4137           (cond
4138            ((numberp read-all)
4139             read-all)
4140            (t
4141             (condition-case ()
4142                 (cond
4143                  ((and (or (<= scored marked) (= scored number))
4144                        (numberp gnus-large-newsgroup)
4145                        (> number gnus-large-newsgroup))
4146                   (let ((input
4147                          (read-string
4148                           (format
4149                            "How many articles from %s (default %d): "
4150                            (gnus-limit-string gnus-newsgroup-name 35)
4151                            number))))
4152                     (if (string-match "^[ \t]*$" input) number input)))
4153                  ((and (> scored marked) (< scored number)
4154                        (> (- scored number) 20))
4155                   (let ((input
4156                          (read-string
4157                           (format "%s %s (%d scored, %d total): "
4158                                   "How many articles from"
4159                                   group scored number))))
4160                     (if (string-match "^[ \t]*$" input)
4161                         number input)))
4162                  (t number))
4163               (quit nil))))))
4164     (setq select (if (stringp select) (string-to-number select) select))
4165     (if (or (null select) (zerop select))
4166         select
4167       (if (and (not (zerop scored)) (<= (abs select) scored))
4168           (progn
4169             (setq articles (sort scored-list '<))
4170             (setq number (length articles)))
4171         (setq articles (copy-sequence articles)))
4172
4173       (when (< (abs select) number)
4174         (if (< select 0)
4175             ;; Select the N oldest articles.
4176             (setcdr (nthcdr (1- (abs select)) articles) nil)
4177           ;; Select the N most recent articles.
4178           (setq articles (nthcdr (- number select) articles))))
4179       (setq gnus-newsgroup-unselected
4180             (gnus-sorted-intersection
4181              gnus-newsgroup-unreads
4182              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4183       articles)))
4184
4185 (defun gnus-killed-articles (killed articles)
4186   (let (out)
4187     (while articles
4188       (when (inline (gnus-member-of-range (car articles) killed))
4189         (push (car articles) out))
4190       (setq articles (cdr articles)))
4191     out))
4192
4193 (defun gnus-uncompress-marks (marks)
4194   "Uncompress the mark ranges in MARKS."
4195   (let ((uncompressed '(score bookmark))
4196         out)
4197     (while marks
4198       (if (memq (caar marks) uncompressed)
4199           (push (car marks) out)
4200         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4201       (setq marks (cdr marks)))
4202     out))
4203
4204 (defun gnus-adjust-marked-articles (info)
4205   "Set all article lists and remove all marks that are no longer valid."
4206   (let* ((marked-lists (gnus-info-marks info))
4207          (active (gnus-active (gnus-info-group info)))
4208          (min (car active))
4209          (max (cdr active))
4210          (types gnus-article-mark-lists)
4211          (uncompressed '(score bookmark killed))
4212          marks var articles article mark)
4213
4214     (while marked-lists
4215       (setq marks (pop marked-lists))
4216       (set (setq var (intern (format "gnus-newsgroup-%s"
4217                                      (car (rassq (setq mark (car marks))
4218                                                  types)))))
4219            (if (memq (car marks) uncompressed) (cdr marks)
4220              (gnus-uncompress-range (cdr marks))))
4221
4222       (setq articles (symbol-value var))
4223
4224       ;; All articles have to be subsets of the active articles.
4225       (cond
4226        ;; Adjust "simple" lists.
4227        ((memq mark '(tick dormant expire reply save))
4228         (while articles
4229           (when (or (< (setq article (pop articles)) min) (> article max))
4230             (set var (delq article (symbol-value var))))))
4231        ;; Adjust assocs.
4232        ((memq mark uncompressed)
4233         (when (not (listp (cdr (symbol-value var))))
4234           (set var (list (symbol-value var))))
4235         (when (not (listp (cdr articles)))
4236           (setq articles (list articles)))
4237         (while articles
4238           (when (or (not (consp (setq article (pop articles))))
4239                     (< (car article) min)
4240                     (> (car article) max))
4241             (set var (delq article (symbol-value var))))))))))
4242
4243 (defun gnus-update-missing-marks (missing)
4244   "Go through the list of MISSING articles and remove them from the mark lists."
4245   (when missing
4246     (let ((types gnus-article-mark-lists)
4247           var m)
4248       ;; Go through all types.
4249       (while types
4250         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4251         (when (symbol-value var)
4252           ;; This list has articles.  So we delete all missing articles
4253           ;; from it.
4254           (setq m missing)
4255           (while m
4256             (set var (delq (pop m) (symbol-value var)))))))))
4257
4258 (defun gnus-update-marks ()
4259   "Enter the various lists of marked articles into the newsgroup info list."
4260   (let ((types gnus-article-mark-lists)
4261         (info (gnus-get-info gnus-newsgroup-name))
4262         (uncompressed '(score bookmark killed))
4263         type list newmarked symbol delta-marks)
4264     (when info
4265       ;; Add all marks lists that are non-nil to the list of marks lists.
4266       (while (setq type (pop types))
4267         (when (setq list (symbol-value
4268                           (setq symbol
4269                                 (intern (format "gnus-newsgroup-%s"
4270                                                 (car type))))))
4271
4272           ;; Get rid of the entries of the articles that have the
4273           ;; default score.
4274           (when (and (eq (cdr type) 'score)
4275                      gnus-save-score
4276                      list)
4277             (let* ((arts list)
4278                    (prev (cons nil list))
4279                    (all prev))
4280               (while arts
4281                 (if (or (not (consp (car arts)))
4282                         (= (cdar arts) gnus-summary-default-score))
4283                     (setcdr prev (cdr arts))
4284                   (setq prev arts))
4285                 (setq arts (cdr arts)))
4286               (setq list (cdr all))))
4287
4288          (when (gnus-check-backend-function 'request-set-mark
4289                                             gnus-newsgroup-name)
4290            ;; score & bookmark are not proper flags (they are cons cells)
4291            ;; cache is a internal gnus flag
4292            (unless (memq (cdr type) '(cache score bookmark))
4293              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4294                     (del (gnus-remove-from-range old list))
4295                     (add (gnus-remove-from-range list old)))
4296                (if add
4297                    (push (list add 'add (list (cdr type))) delta-marks))
4298                (if del
4299                    (push (list del 'del (list (cdr type))) delta-marks)))))
4300
4301           (push (cons (cdr type)
4302                       (if (memq (cdr type) uncompressed) list
4303                         (gnus-compress-sequence
4304                          (set symbol (sort list '<)) t)))
4305                 newmarked)))
4306
4307       (if delta-marks
4308          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4309
4310       ;; Enter these new marks into the info of the group.
4311       (if (nthcdr 3 info)
4312           (setcar (nthcdr 3 info) newmarked)
4313         ;; Add the marks lists to the end of the info.
4314         (when newmarked
4315           (setcdr (nthcdr 2 info) (list newmarked))))
4316
4317       ;; Cut off the end of the info if there's nothing else there.
4318       (let ((i 5))
4319         (while (and (> i 2)
4320                     (not (nth i info)))
4321           (when (nthcdr (decf i) info)
4322             (setcdr (nthcdr i info) nil)))))))
4323
4324 (defun gnus-set-mode-line (where)
4325   "This function sets the mode line of the article or summary buffers.
4326 If WHERE is `summary', the summary mode line format will be used."
4327   ;; Is this mode line one we keep updated?
4328   (when (and (memq where gnus-updated-mode-lines)
4329              (symbol-value
4330               (intern (format "gnus-%s-mode-line-format-spec" where))))
4331     (let (mode-string)
4332       (save-excursion
4333         ;; We evaluate this in the summary buffer since these
4334         ;; variables are buffer-local to that buffer.
4335         (set-buffer gnus-summary-buffer)
4336         ;; We bind all these variables that are used in the `eval' form
4337         ;; below.
4338         (let* ((mformat (symbol-value
4339                          (intern
4340                           (format "gnus-%s-mode-line-format-spec" where))))
4341                (gnus-tmp-group-name gnus-newsgroup-name)
4342                (gnus-tmp-article-number (or gnus-current-article 0))
4343                (gnus-tmp-unread gnus-newsgroup-unreads)
4344                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4345                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4346                (gnus-tmp-unread-and-unselected
4347                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4348                             (zerop gnus-tmp-unselected))
4349                        "")
4350                       ((zerop gnus-tmp-unselected)
4351                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4352                       (t (format "{%d(+%d) more}"
4353                                  gnus-tmp-unread-and-unticked
4354                                  gnus-tmp-unselected))))
4355                (gnus-tmp-subject
4356                 (if (and gnus-current-headers
4357                          (vectorp gnus-current-headers))
4358                     (gnus-mode-string-quote
4359                      (mail-header-subject gnus-current-headers))
4360                   ""))
4361                bufname-length max-len
4362                gnus-tmp-header);; passed as argument to any user-format-funcs
4363           (setq mode-string (eval mformat))
4364           (setq bufname-length (if (string-match "%b" mode-string)
4365                                    (- (length
4366                                        (buffer-name
4367                                         (if (eq where 'summary)
4368                                             nil
4369                                           (get-buffer gnus-article-buffer))))
4370                                       2)
4371                                  0))
4372           (setq max-len (max 4 (if gnus-mode-non-string-length
4373                                    (- (window-width)
4374                                       gnus-mode-non-string-length
4375                                       bufname-length)
4376                                  (length mode-string))))
4377           ;; We might have to chop a bit of the string off...
4378           (when (> (length mode-string) max-len)
4379             (setq mode-string
4380                   (concat (truncate-string-to-width mode-string (- max-len 3))
4381                           "...")))
4382           ;; Pad the mode string a bit.
4383           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4384       ;; Update the mode line.
4385       (setq mode-line-buffer-identification
4386             (gnus-mode-line-buffer-identification (list mode-string)))
4387       (set-buffer-modified-p t))))
4388
4389 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4390   "Go through the HEADERS list and add all Xrefs to a hash table.
4391 The resulting hash table is returned, or nil if no Xrefs were found."
4392   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4393          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4394          (xref-hashtb (gnus-make-hashtable))
4395          start group entry number xrefs header)
4396     (while headers
4397       (setq header (pop headers))
4398       (when (and (setq xrefs (mail-header-xref header))
4399                  (not (memq (setq number (mail-header-number header))
4400                             unreads)))
4401         (setq start 0)
4402         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4403           (setq start (match-end 0))
4404           (setq group (if prefix
4405                           (concat prefix (substring xrefs (match-beginning 1)
4406                                                     (match-end 1)))
4407                         (substring xrefs (match-beginning 1) (match-end 1))))
4408           (setq number
4409                 (string-to-int (substring xrefs (match-beginning 2)
4410                                           (match-end 2))))
4411           (if (setq entry (gnus-gethash group xref-hashtb))
4412               (setcdr entry (cons number (cdr entry)))
4413             (gnus-sethash group (cons number nil) xref-hashtb)))))
4414     (and start xref-hashtb)))
4415
4416 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4417   "Look through all the headers and mark the Xrefs as read."
4418   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4419         name entry info xref-hashtb idlist method nth4)
4420     (save-excursion
4421       (set-buffer gnus-group-buffer)
4422       (when (setq xref-hashtb
4423                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4424         (mapatoms
4425          (lambda (group)
4426            (unless (string= from-newsgroup (setq name (symbol-name group)))
4427              (setq idlist (symbol-value group))
4428              ;; Dead groups are not updated.
4429              (and (prog1
4430                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4431                             info (nth 2 entry))
4432                     (when (stringp (setq nth4 (gnus-info-method info)))
4433                       (setq nth4 (gnus-server-to-method nth4))))
4434                   ;; Only do the xrefs if the group has the same
4435                   ;; select method as the group we have just read.
4436                   (or (gnus-methods-equal-p
4437                        nth4 (gnus-find-method-for-group from-newsgroup))
4438                       virtual
4439                       (equal nth4 (setq method (gnus-find-method-for-group
4440                                                 from-newsgroup)))
4441                       (and (equal (car nth4) (car method))
4442                            (equal (nth 1 nth4) (nth 1 method))))
4443                   gnus-use-cross-reference
4444                   (or (not (eq gnus-use-cross-reference t))
4445                       virtual
4446                       ;; Only do cross-references on subscribed
4447                       ;; groups, if that is what is wanted.
4448                       (<= (gnus-info-level info) gnus-level-subscribed))
4449                   (gnus-group-make-articles-read name idlist))))
4450          xref-hashtb)))))
4451
4452 (defun gnus-compute-read-articles (group articles)
4453   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4454          (info (nth 2 entry))
4455          (active (gnus-active group))
4456          ninfo)
4457     (when entry
4458       ;; First peel off all invalid article numbers.
4459       (when active
4460         (let ((ids articles)
4461               id first)
4462           (while (setq id (pop ids))
4463             (when (and first (> id (cdr active)))
4464               ;; We'll end up in this situation in one particular
4465               ;; obscure situation.  If you re-scan a group and get
4466               ;; a new article that is cross-posted to a different
4467               ;; group that has not been re-scanned, you might get
4468               ;; crossposted article that has a higher number than
4469               ;; Gnus believes possible.  So we re-activate this
4470               ;; group as well.  This might mean doing the
4471               ;; crossposting thingy will *increase* the number
4472               ;; of articles in some groups.  Tsk, tsk.
4473               (setq active (or (gnus-activate-group group) active)))
4474             (when (or (> id (cdr active))
4475                       (< id (car active)))
4476               (setq articles (delq id articles))))))
4477       ;; If the read list is nil, we init it.
4478       (if (and active
4479                (null (gnus-info-read info))
4480                (> (car active) 1))
4481           (setq ninfo (cons 1 (1- (car active))))
4482         (setq ninfo (gnus-info-read info)))
4483       ;; Then we add the read articles to the range.
4484       (gnus-add-to-range
4485        ninfo (setq articles (sort articles '<))))))
4486
4487 (defun gnus-group-make-articles-read (group articles)
4488   "Update the info of GROUP to say that ARTICLES are read."
4489   (let* ((num 0)
4490          (entry (gnus-gethash group gnus-newsrc-hashtb))
4491          (info (nth 2 entry))
4492          (active (gnus-active group))
4493          range)
4494     (when entry
4495       (setq range (gnus-compute-read-articles group articles))
4496       (save-excursion
4497         (set-buffer gnus-group-buffer)
4498         (gnus-undo-register
4499           `(progn
4500              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4501              (gnus-info-set-read ',info ',(gnus-info-read info))
4502              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4503              (gnus-group-update-group ,group t))))
4504       ;; Add the read articles to the range.
4505       (gnus-info-set-read info range)
4506       ;; Then we have to re-compute how many unread
4507       ;; articles there are in this group.
4508       (when active
4509         (cond
4510          ((not range)
4511           (setq num (- (1+ (cdr active)) (car active))))
4512          ((not (listp (cdr range)))
4513           (setq num (- (cdr active) (- (1+ (cdr range))
4514                                        (car range)))))
4515          (t
4516           (while range
4517             (if (numberp (car range))
4518                 (setq num (1+ num))
4519               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4520             (setq range (cdr range)))
4521           (setq num (- (cdr active) num))))
4522         ;; Update the number of unread articles.
4523         (setcar entry num)
4524         ;; Update the group buffer.
4525         (gnus-group-update-group group t)))))
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         (mail-parse-charset gnus-newsgroup-charset))
4537     (save-excursion
4538       (set-buffer nntp-server-buffer)
4539       ;; Translate all TAB characters into SPACE characters.
4540       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4541       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4542       (gnus-run-hooks 'gnus-parse-headers-hook)
4543       (let ((case-fold-search t)
4544             in-reply-to header p lines chars)
4545         (goto-char (point-min))
4546         ;; Search to the beginning of the next header.  Error messages
4547         ;; do not begin with 2 or 3.
4548         (while (re-search-forward "^[23][0-9]+ " nil t)
4549           (setq id nil
4550                 ref nil)
4551           ;; This implementation of this function, with nine
4552           ;; search-forwards instead of the one re-search-forward and
4553           ;; a case (which basically was the old function) is actually
4554           ;; about twice as fast, even though it looks messier.  You
4555           ;; can't have everything, I guess.  Speed and elegance
4556           ;; doesn't always go hand in hand.
4557           (setq
4558            header
4559            (vector
4560             ;; Number.
4561             (prog1
4562                 (read cur)
4563               (end-of-line)
4564               (setq p (point))
4565               (narrow-to-region (point)
4566                                 (or (and (search-forward "\n.\n" nil t)
4567                                          (- (point) 2))
4568                                     (point))))
4569             ;; Subject.
4570             (progn
4571               (goto-char p)
4572               (if (search-forward "\nsubject: " nil t)
4573                   (funcall gnus-decode-encoded-word-function
4574                            (nnheader-header-value))
4575                 "(none)"))
4576             ;; From.
4577             (progn
4578               (goto-char p)
4579               (if (search-forward "\nfrom: " nil t)
4580                   (funcall gnus-decode-encoded-word-function
4581                            (nnheader-header-value))
4582                 "(nobody)"))
4583             ;; Date.
4584             (progn
4585               (goto-char p)
4586               (if (search-forward "\ndate: " nil t)
4587                   (nnheader-header-value) ""))
4588             ;; Message-ID.
4589             (progn
4590               (goto-char p)
4591               (setq id (if (re-search-forward
4592                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4593                            ;; We do it this way to make sure the Message-ID
4594                            ;; is (somewhat) syntactically valid.
4595                            (buffer-substring (match-beginning 1)
4596                                              (match-end 1))
4597                          ;; If there was no message-id, we just fake one
4598                          ;; to make subsequent routines simpler.
4599                          (nnheader-generate-fake-message-id))))
4600             ;; References.
4601             (progn
4602               (goto-char p)
4603               (if (search-forward "\nreferences: " nil t)
4604                   (progn
4605                     (setq end (point))
4606                     (prog1
4607                         (nnheader-header-value)
4608                       (setq ref
4609                             (buffer-substring
4610                              (progn
4611                                (end-of-line)
4612                                (search-backward ">" end t)
4613                                (1+ (point)))
4614                              (progn
4615                                (search-backward "<" end t)
4616                                (point))))))
4617                 ;; Get the references from the in-reply-to header if there
4618                 ;; were no references and the in-reply-to header looks
4619                 ;; promising.
4620                 (if (and (search-forward "\nin-reply-to: " nil t)
4621                          (setq in-reply-to (nnheader-header-value))
4622                          (string-match "<[^>]+>" in-reply-to))
4623                     (let (ref2)
4624                       (setq ref (substring in-reply-to (match-beginning 0)
4625                                            (match-end 0)))
4626                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4627                         (setq ref2 (substring in-reply-to (match-beginning 0)
4628                                               (match-end 0)))
4629                         (when (> (length ref2) (length ref))
4630                           (setq ref ref2)))
4631                       ref)
4632                   (setq ref nil))))
4633             ;; Chars.
4634             (progn
4635               (goto-char p)
4636               (if (search-forward "\nchars: " nil t)
4637                   (if (numberp (setq chars (ignore-errors (read cur))))
4638                       chars 0)
4639                 0))
4640             ;; Lines.
4641             (progn
4642               (goto-char p)
4643               (if (search-forward "\nlines: " nil t)
4644                   (if (numberp (setq lines (ignore-errors (read cur))))
4645                       lines 0)
4646                 0))
4647             ;; Xref.
4648             (progn
4649               (goto-char p)
4650               (and (search-forward "\nxref: " nil t)
4651                    (nnheader-header-value)))
4652             ;; Extra.
4653             (when gnus-extra-headers
4654               (let ((extra gnus-extra-headers)
4655                     out)
4656                 (while extra
4657                   (goto-char p)
4658                   (when (search-forward
4659                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4660                     (push (cons (car extra) (nnheader-header-value))
4661                           out))
4662                   (pop extra))
4663                 out))))
4664           (when (equal id ref)
4665             (setq ref nil))
4666
4667           (when gnus-alter-header-function
4668             (funcall gnus-alter-header-function header)
4669             (setq id (mail-header-id header)
4670                   ref (gnus-parent-id (mail-header-references header))))
4671
4672           (when (setq header
4673                       (gnus-dependencies-add-header
4674                        header dependencies force-new))
4675             (push header headers))
4676           (goto-char (point-max))
4677           (widen))
4678         (nreverse headers)))))
4679
4680 ;; Goes through the xover lines and returns a list of vectors
4681 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4682                                                   force-new dependencies
4683                                                   group also-fetch-heads)
4684   "Parse the news overview data in the server buffer, and return a
4685 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4686   ;; Get the Xref when the users reads the articles since most/some
4687   ;; NNTP servers do not include Xrefs when using XOVER.
4688   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4689   (let ((mail-parse-charset gnus-newsgroup-charset)
4690         (cur nntp-server-buffer)
4691         (dependencies (or dependencies gnus-newsgroup-dependencies))
4692         number headers header)
4693     (save-excursion
4694       (set-buffer nntp-server-buffer)
4695       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4696       ;; Allow the user to mangle the headers before parsing them.
4697       (gnus-run-hooks 'gnus-parse-headers-hook)
4698       (goto-char (point-min))
4699       (while (not (eobp))
4700         (condition-case ()
4701             (while (and sequence (not (eobp)))
4702               (setq number (read cur))
4703               (while (and sequence
4704                           (< (car sequence) number))
4705                 (setq sequence (cdr sequence)))
4706               (and sequence
4707                    (eq number (car sequence))
4708                    (progn
4709                      (setq sequence (cdr sequence))
4710                      (setq header (inline
4711                                     (gnus-nov-parse-line
4712                                      number dependencies force-new))))
4713                    (push header headers))
4714               (forward-line 1))
4715           (error
4716            (gnus-error 4 "Strange nov line (%d)"
4717                        (count-lines (point-min) (point)))))
4718         (forward-line 1))
4719       ;; A common bug in inn is that if you have posted an article and
4720       ;; then retrieves the active file, it will answer correctly --
4721       ;; the new article is included.  However, a NOV entry for the
4722       ;; article may not have been generated yet, so this may fail.
4723       ;; We work around this problem by retrieving the last few
4724       ;; headers using HEAD.
4725       (if (or (not also-fetch-heads)
4726               (not sequence))
4727           ;; We (probably) got all the headers.
4728           (nreverse headers)
4729         (let ((gnus-nov-is-evil t))
4730           (nconc
4731            (nreverse headers)
4732            (when (gnus-retrieve-headers sequence group)
4733              (gnus-get-newsgroup-headers))))))))
4734
4735 (defun gnus-article-get-xrefs ()
4736   "Fill in the Xref value in `gnus-current-headers', if necessary.
4737 This is meant to be called in `gnus-article-internal-prepare-hook'."
4738   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4739                                  gnus-current-headers)))
4740     (or (not gnus-use-cross-reference)
4741         (not headers)
4742         (and (mail-header-xref headers)
4743              (not (string= (mail-header-xref headers) "")))
4744         (let ((case-fold-search t)
4745               xref)
4746           (save-restriction
4747             (nnheader-narrow-to-headers)
4748             (goto-char (point-min))
4749             (when (or (and (eq (downcase (char-after)) ?x)
4750                            (looking-at "Xref:"))
4751                       (search-forward "\nXref:" nil t))
4752               (goto-char (1+ (match-end 0)))
4753               (setq xref (buffer-substring (point)
4754                                            (progn (end-of-line) (point))))
4755               (mail-header-set-xref headers xref)))))))
4756
4757 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4758   "Find article ID and insert the summary line for that article.
4759 OLD-HEADER can either be a header or a line number to insert
4760 the subject line on."
4761   (let* ((line (and (numberp old-header) old-header))
4762          (old-header (and (vectorp old-header) old-header))
4763          (header (cond ((and old-header use-old-header)
4764                         old-header)
4765                        ((and (numberp id)
4766                              (gnus-number-to-header id))
4767                         (gnus-number-to-header id))
4768                        (t
4769                         (gnus-read-header id))))
4770          (number (and (numberp id) id))
4771          d)
4772     (when header
4773       ;; Rebuild the thread that this article is part of and go to the
4774       ;; article we have fetched.
4775       (when (and (not gnus-show-threads)
4776                  old-header)
4777         (when (and number
4778                    (setq d (gnus-data-find (mail-header-number old-header))))
4779           (goto-char (gnus-data-pos d))
4780           (gnus-data-remove
4781            number
4782            (- (gnus-point-at-bol)
4783               (prog1
4784                   (1+ (gnus-point-at-eol))
4785                 (gnus-delete-line))))))
4786       (when old-header
4787         (mail-header-set-number header (mail-header-number old-header)))
4788       (setq gnus-newsgroup-sparse
4789             (delq (setq number (mail-header-number header))
4790                   gnus-newsgroup-sparse))
4791       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4792       (push number gnus-newsgroup-limit)
4793       (gnus-rebuild-thread (mail-header-id header) line)
4794       (gnus-summary-goto-subject number nil t))
4795     (when (and (numberp number)
4796                (> number 0))
4797       ;; We have to update the boundaries even if we can't fetch the
4798       ;; article if ID is a number -- so that the next `P' or `N'
4799       ;; command will fetch the previous (or next) article even
4800       ;; if the one we tried to fetch this time has been canceled.
4801       (when (> number gnus-newsgroup-end)
4802         (setq gnus-newsgroup-end number))
4803       (when (< number gnus-newsgroup-begin)
4804         (setq gnus-newsgroup-begin number))
4805       (setq gnus-newsgroup-unselected
4806             (delq number gnus-newsgroup-unselected)))
4807     ;; Report back a success?
4808     (and header (mail-header-number header))))
4809
4810 ;;; Process/prefix in the summary buffer
4811
4812 (defun gnus-summary-work-articles (n)
4813   "Return a list of articles to be worked upon.
4814 The prefix argument, the list of process marked articles, and the
4815 current article will be taken into consideration."
4816   (save-excursion
4817     (set-buffer gnus-summary-buffer)
4818     (cond
4819      (n
4820       ;; A numerical prefix has been given.
4821       (setq n (prefix-numeric-value n))
4822       (let ((backward (< n 0))
4823             (n (abs (prefix-numeric-value n)))
4824             articles article)
4825         (save-excursion
4826           (while
4827               (and (> n 0)
4828                    (push (setq article (gnus-summary-article-number))
4829                          articles)
4830                    (if backward
4831                        (gnus-summary-find-prev nil article)
4832                      (gnus-summary-find-next nil article)))
4833             (decf n)))
4834         (nreverse articles)))
4835      ((and (gnus-region-active-p) (mark))
4836       (message "region active")
4837       ;; Work on the region between point and mark.
4838       (let ((max (max (point) (mark)))
4839             articles article)
4840         (save-excursion
4841           (goto-char (min (min (point) (mark))))
4842           (while
4843               (and
4844                (push (setq article (gnus-summary-article-number)) articles)
4845                (gnus-summary-find-next nil article)
4846                (< (point) max)))
4847           (nreverse articles))))
4848      (gnus-newsgroup-processable
4849       ;; There are process-marked articles present.
4850       ;; Save current state.
4851       (gnus-summary-save-process-mark)
4852       ;; Return the list.
4853       (reverse gnus-newsgroup-processable))
4854      (t
4855       ;; Just return the current article.
4856       (list (gnus-summary-article-number))))))
4857
4858 (defmacro gnus-summary-iterate (arg &rest forms)
4859   "Iterate over the process/prefixed articles and do FORMS.
4860 ARG is the interactive prefix given to the command.  FORMS will be
4861 executed with point over the summary line of the articles."
4862   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4863     `(let ((,articles (gnus-summary-work-articles ,arg)))
4864        (while ,articles
4865          (gnus-summary-goto-subject (car ,articles))
4866          ,@forms))))
4867
4868 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4869 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4870
4871 (defun gnus-summary-save-process-mark ()
4872   "Push the current set of process marked articles on the stack."
4873   (interactive)
4874   (push (copy-sequence gnus-newsgroup-processable)
4875         gnus-newsgroup-process-stack))
4876
4877 (defun gnus-summary-kill-process-mark ()
4878   "Push the current set of process marked articles on the stack and unmark."
4879   (interactive)
4880   (gnus-summary-save-process-mark)
4881   (gnus-summary-unmark-all-processable))
4882
4883 (defun gnus-summary-yank-process-mark ()
4884   "Pop the last process mark state off the stack and restore it."
4885   (interactive)
4886   (unless gnus-newsgroup-process-stack
4887     (error "Empty mark stack"))
4888   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4889
4890 (defun gnus-summary-process-mark-set (set)
4891   "Make SET into the current process marked articles."
4892   (gnus-summary-unmark-all-processable)
4893   (while set
4894     (gnus-summary-set-process-mark (pop set))))
4895
4896 ;;; Searching and stuff
4897
4898 (defun gnus-summary-search-group (&optional backward use-level)
4899   "Search for next unread newsgroup.
4900 If optional argument BACKWARD is non-nil, search backward instead."
4901   (save-excursion
4902     (set-buffer gnus-group-buffer)
4903     (when (gnus-group-search-forward
4904            backward nil (if use-level (gnus-group-group-level) nil))
4905       (gnus-group-group-name))))
4906
4907 (defun gnus-summary-best-group (&optional exclude-group)
4908   "Find the name of the best unread group.
4909 If EXCLUDE-GROUP, do not go to this group."
4910   (save-excursion
4911     (set-buffer gnus-group-buffer)
4912     (save-excursion
4913       (gnus-group-best-unread-group exclude-group))))
4914
4915 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4916   (if backward (gnus-summary-find-prev)
4917     (let* ((dummy (gnus-summary-article-intangible-p))
4918            (article (or article (gnus-summary-article-number)))
4919            (arts (gnus-data-find-list article))
4920            result)
4921       (when (and (not dummy)
4922                  (or (not gnus-summary-check-current)
4923                      (not unread)
4924                      (not (gnus-data-unread-p (car arts)))))
4925         (setq arts (cdr arts)))
4926       (when (setq result
4927                   (if unread
4928                       (progn
4929                         (while arts
4930                           (when (or (and undownloaded
4931                                          (eq gnus-undownloaded-mark
4932                                              (gnus-data-mark (car arts))))
4933                                     (gnus-data-unread-p (car arts)))
4934                             (setq result (car arts)
4935                                   arts nil))
4936                           (setq arts (cdr arts)))
4937                         result)
4938                     (car arts)))
4939         (goto-char (gnus-data-pos result))
4940         (gnus-data-number result)))))
4941
4942 (defun gnus-summary-find-prev (&optional unread article)
4943   (let* ((eobp (eobp))
4944          (article (or article (gnus-summary-article-number)))
4945          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4946          result)
4947     (when (and (not eobp)
4948                (or (not gnus-summary-check-current)
4949                    (not unread)
4950                    (not (gnus-data-unread-p (car arts)))))
4951       (setq arts (cdr arts)))
4952     (when (setq result
4953                 (if unread
4954                     (progn
4955                       (while arts
4956                         (when (gnus-data-unread-p (car arts))
4957                           (setq result (car arts)
4958                                 arts nil))
4959                         (setq arts (cdr arts)))
4960                       result)
4961                   (car arts)))
4962       (goto-char (gnus-data-pos result))
4963       (gnus-data-number result))))
4964
4965 (defun gnus-summary-find-subject (subject &optional unread backward article)
4966   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4967          (article (or article (gnus-summary-article-number)))
4968          (articles (gnus-data-list backward))
4969          (arts (gnus-data-find-list article articles))
4970          result)
4971     (when (or (not gnus-summary-check-current)
4972               (not unread)
4973               (not (gnus-data-unread-p (car arts))))
4974       (setq arts (cdr arts)))
4975     (while arts
4976       (and (or (not unread)
4977                (gnus-data-unread-p (car arts)))
4978            (vectorp (gnus-data-header (car arts)))
4979            (gnus-subject-equal
4980             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4981            (setq result (car arts)
4982                  arts nil))
4983       (setq arts (cdr arts)))
4984     (and result
4985          (goto-char (gnus-data-pos result))
4986          (gnus-data-number result))))
4987
4988 (defun gnus-summary-search-forward (&optional unread subject backward)
4989   "Search forward for an article.
4990 If UNREAD, look for unread articles.  If SUBJECT, look for
4991 articles with that subject.  If BACKWARD, search backward instead."
4992   (cond (subject (gnus-summary-find-subject subject unread backward))
4993         (backward (gnus-summary-find-prev unread))
4994         (t (gnus-summary-find-next unread))))
4995
4996 (defun gnus-recenter (&optional n)
4997   "Center point in window and redisplay frame.
4998 Also do horizontal recentering."
4999   (interactive "P")
5000   (when (and gnus-auto-center-summary
5001              (not (eq gnus-auto-center-summary 'vertical)))
5002     (gnus-horizontal-recenter))
5003   (recenter n))
5004
5005 (defun gnus-summary-recenter ()
5006   "Center point in the summary window.
5007 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5008 displayed, no centering will be performed."
5009   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5010   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5011   (let* ((top (cond ((< (window-height) 4) 0)
5012                     ((< (window-height) 7) 1)
5013                     (t (if (numberp gnus-auto-center-summary)
5014                            gnus-auto-center-summary
5015                          2))))
5016          (height (1- (window-height)))
5017          (bottom (save-excursion (goto-char (point-max))
5018                                  (forward-line (- height))
5019                                  (point)))
5020          (window (get-buffer-window (current-buffer))))
5021     ;; The user has to want it.
5022     (when gnus-auto-center-summary
5023       (when (get-buffer-window gnus-article-buffer)
5024         ;; Only do recentering when the article buffer is displayed,
5025         ;; Set the window start to either `bottom', which is the biggest
5026         ;; possible valid number, or the second line from the top,
5027         ;; whichever is the least.
5028         (set-window-start
5029          window (min bottom (save-excursion
5030                               (forward-line (- top)) (point)))))
5031       ;; Do horizontal recentering while we're at it.
5032       (when (and (get-buffer-window (current-buffer) t)
5033                  (not (eq gnus-auto-center-summary 'vertical)))
5034         (let ((selected (selected-window)))
5035           (select-window (get-buffer-window (current-buffer) t))
5036           (gnus-summary-position-point)
5037           (gnus-horizontal-recenter)
5038           (select-window selected))))))
5039
5040 (defun gnus-summary-jump-to-group (newsgroup)
5041   "Move point to NEWSGROUP in group mode buffer."
5042   ;; Keep update point of group mode buffer if visible.
5043   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5044       (save-window-excursion
5045         ;; Take care of tree window mode.
5046         (when (get-buffer-window gnus-group-buffer)
5047           (pop-to-buffer gnus-group-buffer))
5048         (gnus-group-jump-to-group newsgroup))
5049     (save-excursion
5050       ;; Take care of tree window mode.
5051       (if (get-buffer-window gnus-group-buffer)
5052           (pop-to-buffer gnus-group-buffer)
5053         (set-buffer gnus-group-buffer))
5054       (gnus-group-jump-to-group newsgroup))))
5055
5056 ;; This function returns a list of article numbers based on the
5057 ;; difference between the ranges of read articles in this group and
5058 ;; the range of active articles.
5059 (defun gnus-list-of-unread-articles (group)
5060   (let* ((read (gnus-info-read (gnus-get-info group)))
5061          (active (or (gnus-active group) (gnus-activate-group group)))
5062          (last (cdr active))
5063          first nlast unread)
5064     ;; If none are read, then all are unread.
5065     (if (not read)
5066         (setq first (car active))
5067       ;; If the range of read articles is a single range, then the
5068       ;; first unread article is the article after the last read
5069       ;; article.  Sounds logical, doesn't it?
5070       (if (not (listp (cdr read)))
5071           (setq first (max (car active) (1+ (cdr read))))
5072         ;; `read' is a list of ranges.
5073         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5074                                   (caar read)))
5075                   1)
5076           (setq first (car active)))
5077         (while read
5078           (when first
5079             (while (< first nlast)
5080               (push first unread)
5081               (setq first (1+ first))))
5082           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5083           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5084           (setq read (cdr read)))))
5085     ;; And add the last unread articles.
5086     (while (<= first last)
5087       (push first unread)
5088       (setq first (1+ first)))
5089     ;; Return the list of unread articles.
5090     (delq 0 (nreverse unread))))
5091
5092 (defun gnus-list-of-read-articles (group)
5093   "Return a list of unread, unticked and non-dormant articles."
5094   (let* ((info (gnus-get-info group))
5095          (marked (gnus-info-marks info))
5096          (active (gnus-active group)))
5097     (and info active
5098          (gnus-set-difference
5099           (gnus-sorted-complement
5100            (gnus-uncompress-range active)
5101            (gnus-list-of-unread-articles group))
5102           (append
5103            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5104            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5105
5106 ;; Various summary commands
5107
5108 (defun gnus-summary-select-article-buffer ()
5109   "Reconfigure windows to show article buffer."
5110   (interactive)
5111   (if (not (gnus-buffer-live-p gnus-article-buffer))
5112       (error "There is no article buffer for this summary buffer")
5113     (gnus-configure-windows 'article)
5114     (select-window (get-buffer-window gnus-article-buffer))))
5115
5116 (defun gnus-summary-universal-argument (arg)
5117   "Perform any operation on all articles that are process/prefixed."
5118   (interactive "P")
5119   (let ((articles (gnus-summary-work-articles arg))
5120         func article)
5121     (if (eq
5122          (setq
5123           func
5124           (key-binding
5125            (read-key-sequence
5126             (substitute-command-keys
5127              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5128              ))))
5129          'undefined)
5130         (gnus-error 1 "Undefined key")
5131       (save-excursion
5132         (while articles
5133           (gnus-summary-goto-subject (setq article (pop articles)))
5134           (let (gnus-newsgroup-processable)
5135             (command-execute func))
5136           (gnus-summary-remove-process-mark article)))))
5137   (gnus-summary-position-point))
5138
5139 (defun gnus-summary-toggle-truncation (&optional arg)
5140   "Toggle truncation of summary lines.
5141 With arg, turn line truncation on iff arg is positive."
5142   (interactive "P")
5143   (setq truncate-lines
5144         (if (null arg) (not truncate-lines)
5145           (> (prefix-numeric-value arg) 0)))
5146   (redraw-display))
5147
5148 (defun gnus-summary-reselect-current-group (&optional all rescan)
5149   "Exit and then reselect the current newsgroup.
5150 The prefix argument ALL means to select all articles."
5151   (interactive "P")
5152   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5153     (error "Ephemeral groups can't be reselected"))
5154   (let ((current-subject (gnus-summary-article-number))
5155         (group gnus-newsgroup-name))
5156     (setq gnus-newsgroup-begin nil)
5157     (gnus-summary-exit)
5158     ;; We have to adjust the point of group mode buffer because
5159     ;; point was moved to the next unread newsgroup by exiting.
5160     (gnus-summary-jump-to-group group)
5161     (when rescan
5162       (save-excursion
5163         (gnus-group-get-new-news-this-group 1)))
5164     (gnus-group-read-group all t)
5165     (gnus-summary-goto-subject current-subject nil t)))
5166
5167 (defun gnus-summary-rescan-group (&optional all)
5168   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5169   (interactive "P")
5170   (gnus-summary-reselect-current-group all t))
5171
5172 (defun gnus-summary-update-info (&optional non-destructive)
5173   (save-excursion
5174     (let ((group gnus-newsgroup-name))
5175       (when group
5176         (when gnus-newsgroup-kill-headers
5177           (setq gnus-newsgroup-killed
5178                 (gnus-compress-sequence
5179                  (nconc
5180                   (gnus-set-sorted-intersection
5181                    (gnus-uncompress-range gnus-newsgroup-killed)
5182                    (setq gnus-newsgroup-unselected
5183                          (sort gnus-newsgroup-unselected '<)))
5184                   (setq gnus-newsgroup-unreads
5185                         (sort gnus-newsgroup-unreads '<)))
5186                  t)))
5187         (unless (listp (cdr gnus-newsgroup-killed))
5188           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5189         (let ((headers gnus-newsgroup-headers))
5190           ;; Set the new ranges of read articles.
5191           (save-excursion
5192             (set-buffer gnus-group-buffer)
5193             (gnus-undo-force-boundary))
5194           (gnus-update-read-articles
5195            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5196           ;; Set the current article marks.
5197           (let ((gnus-newsgroup-scored
5198                  (if (and (not gnus-save-score)
5199                           (not non-destructive))
5200                      nil
5201                    gnus-newsgroup-scored)))
5202             (save-excursion
5203               (gnus-update-marks)))
5204           ;; Do the cross-ref thing.
5205           (when gnus-use-cross-reference
5206             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5207           ;; Do not switch windows but change the buffer to work.
5208           (set-buffer gnus-group-buffer)
5209           (unless (gnus-ephemeral-group-p group)
5210             (gnus-group-update-group group)))))))
5211
5212 (defun gnus-summary-save-newsrc (&optional force)
5213   "Save the current number of read/marked articles in the dribble buffer.
5214 The dribble buffer will then be saved.
5215 If FORCE (the prefix), also save the .newsrc file(s)."
5216   (interactive "P")
5217   (gnus-summary-update-info t)
5218   (if force
5219       (gnus-save-newsrc-file)
5220     (gnus-dribble-save)))
5221
5222 (defun gnus-summary-exit (&optional temporary)
5223   "Exit reading current newsgroup, and then return to group selection mode.
5224 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5225   (interactive)
5226   (gnus-set-global-variables)
5227   (when (gnus-buffer-live-p gnus-article-buffer)
5228     (save-excursion
5229       (set-buffer gnus-article-buffer)
5230       (mm-destroy-parts gnus-article-mime-handles)))
5231   (gnus-kill-save-kill-buffer)
5232   (gnus-async-halt-prefetch)
5233   (let* ((group gnus-newsgroup-name)
5234          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5235          (mode major-mode)
5236          (group-point nil)
5237          (buf (current-buffer)))
5238     (unless quit-config
5239       ;; Do adaptive scoring, and possibly save score files.
5240       (when gnus-newsgroup-adaptive
5241         (gnus-score-adaptive))
5242       (when gnus-use-scoring
5243         (gnus-score-save)))
5244     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5245     ;; If we have several article buffers, we kill them at exit.
5246     (unless gnus-single-article-buffer
5247       (gnus-kill-buffer gnus-original-article-buffer)
5248       (setq gnus-article-current nil))
5249     (when gnus-use-cache
5250       (gnus-cache-possibly-remove-articles)
5251       (gnus-cache-save-buffers))
5252     (gnus-async-prefetch-remove-group group)
5253     (when gnus-suppress-duplicates
5254       (gnus-dup-enter-articles))
5255     (when gnus-use-trees
5256       (gnus-tree-close group))
5257     ;; Remove entries for this group.
5258     (nnmail-purge-split-history (gnus-group-real-name group))
5259     ;; Make all changes in this group permanent.
5260     (unless quit-config
5261       (gnus-run-hooks 'gnus-exit-group-hook)
5262       (gnus-summary-update-info))
5263     (gnus-close-group group)
5264     ;; Make sure where we were, and go to next newsgroup.
5265     (set-buffer gnus-group-buffer)
5266     (unless quit-config
5267       (gnus-group-jump-to-group group))
5268     (gnus-run-hooks 'gnus-summary-exit-hook)
5269     (unless (or quit-config
5270                 ;; If this group has disappeared from the summary
5271                 ;; buffer, don't skip forwards.
5272                 (not (string= group (gnus-group-group-name))))
5273       (gnus-group-next-unread-group 1))
5274     (setq group-point (point))
5275     (if temporary
5276         nil                             ;Nothing to do.
5277       ;; If we have several article buffers, we kill them at exit.
5278       (unless gnus-single-article-buffer
5279         (gnus-kill-buffer gnus-article-buffer)
5280         (gnus-kill-buffer gnus-original-article-buffer)
5281         (setq gnus-article-current nil))
5282       (set-buffer buf)
5283       (if (not gnus-kill-summary-on-exit)
5284           (gnus-deaden-summary)
5285         ;; We set all buffer-local variables to nil.  It is unclear why
5286         ;; this is needed, but if we don't, buffer-local variables are
5287         ;; not garbage-collected, it seems.  This would the lead to en
5288         ;; ever-growing Emacs.
5289         (gnus-summary-clear-local-variables)
5290         (when (get-buffer gnus-article-buffer)
5291           (bury-buffer gnus-article-buffer))
5292         ;; We clear the global counterparts of the buffer-local
5293         ;; variables as well, just to be on the safe side.
5294         (set-buffer gnus-group-buffer)
5295         (gnus-summary-clear-local-variables)
5296         ;; Return to group mode buffer.
5297         (when (eq mode 'gnus-summary-mode)
5298           (gnus-kill-buffer buf)))
5299       (setq gnus-current-select-method gnus-select-method)
5300       (pop-to-buffer gnus-group-buffer)
5301       (if (not quit-config)
5302           (progn
5303             (goto-char group-point)
5304             (gnus-configure-windows 'group 'force))
5305         (gnus-handle-ephemeral-exit quit-config))
5306       ;; Clear the current group name.
5307       (unless quit-config
5308         (setq gnus-newsgroup-name nil)))))
5309
5310 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5311 (defun gnus-summary-exit-no-update (&optional no-questions)
5312   "Quit reading current newsgroup without updating read article info."
5313   (interactive)
5314   (let* ((group gnus-newsgroup-name)
5315          (quit-config (gnus-group-quit-config group)))
5316     (when (or no-questions
5317               gnus-expert-user
5318               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5319       (gnus-async-halt-prefetch)
5320       (mapcar 'funcall
5321               (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 (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")))
5496
5497 ;; Walking around group mode buffer from summary mode.
5498
5499 (defun gnus-summary-next-group (&optional no-article target-group backward)
5500   "Exit current newsgroup and then select next unread newsgroup.
5501 If prefix argument NO-ARTICLE is non-nil, no article is selected
5502 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5503 previous group instead."
5504   (interactive "P")
5505   ;; Stop pre-fetching.
5506   (gnus-async-halt-prefetch)
5507   (let ((current-group gnus-newsgroup-name)
5508         (current-buffer (current-buffer))
5509         entered)
5510     ;; First we semi-exit this group to update Xrefs and all variables.
5511     ;; We can't do a real exit, because the window conf must remain
5512     ;; the same in case the user is prompted for info, and we don't
5513     ;; want the window conf to change before that...
5514     (gnus-summary-exit t)
5515     (while (not entered)
5516       ;; Then we find what group we are supposed to enter.
5517       (set-buffer gnus-group-buffer)
5518       (gnus-group-jump-to-group current-group)
5519       (setq target-group
5520             (or target-group
5521                 (if (eq gnus-keep-same-level 'best)
5522                     (gnus-summary-best-group gnus-newsgroup-name)
5523                   (gnus-summary-search-group backward gnus-keep-same-level))))
5524       (if (not target-group)
5525           ;; There are no further groups, so we return to the group
5526           ;; buffer.
5527           (progn
5528             (gnus-message 5 "Returning to the group buffer")
5529             (setq entered t)
5530             (when (gnus-buffer-live-p current-buffer)
5531               (set-buffer current-buffer)
5532               (gnus-summary-exit))
5533             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5534         ;; We try to enter the target group.
5535         (gnus-group-jump-to-group target-group)
5536         (let ((unreads (gnus-group-group-unread)))
5537           (if (and (or (eq t unreads)
5538                        (and unreads (not (zerop unreads))))
5539                    (gnus-summary-read-group
5540                     target-group nil no-article
5541                     (and (buffer-name current-buffer) current-buffer)
5542                     nil backward))
5543               (setq entered t)
5544             (setq current-group target-group
5545                   target-group nil)))))))
5546
5547 (defun gnus-summary-prev-group (&optional no-article)
5548   "Exit current newsgroup and then select previous unread newsgroup.
5549 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5550   (interactive "P")
5551   (gnus-summary-next-group no-article nil t))
5552
5553 ;; Walking around summary lines.
5554
5555 (defun gnus-summary-first-subject (&optional unread undownloaded)
5556   "Go to the first unread subject.
5557 If UNREAD is non-nil, go to the first unread article.
5558 Returns the article selected or nil if there are no unread articles."
5559   (interactive "P")
5560   (prog1
5561       (cond
5562        ;; Empty summary.
5563        ((null gnus-newsgroup-data)
5564         (gnus-message 3 "No articles in the group")
5565         nil)
5566        ;; Pick the first article.
5567        ((not unread)
5568         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5569         (gnus-data-number (car gnus-newsgroup-data)))
5570        ;; No unread articles.
5571        ((null gnus-newsgroup-unreads)
5572         (gnus-message 3 "No more unread articles")
5573         nil)
5574        ;; Find the first unread article.
5575        (t
5576         (let ((data gnus-newsgroup-data))
5577           (while (and data
5578                       (and (not (and undownloaded
5579                                      (eq gnus-undownloaded-mark
5580                                          (gnus-data-mark (car data)))))
5581                            (not (gnus-data-unread-p (car data)))))
5582             (setq data (cdr data)))
5583           (when data
5584             (goto-char (gnus-data-pos (car data)))
5585             (gnus-data-number (car data))))))
5586     (gnus-summary-position-point)))
5587
5588 (defun gnus-summary-next-subject (n &optional unread dont-display)
5589   "Go to next N'th summary line.
5590 If N is negative, go to the previous N'th subject line.
5591 If UNREAD is non-nil, only unread articles are selected.
5592 The difference between N and the actual number of steps taken is
5593 returned."
5594   (interactive "p")
5595   (let ((backward (< n 0))
5596         (n (abs n)))
5597     (while (and (> n 0)
5598                 (if backward
5599                     (gnus-summary-find-prev unread)
5600                   (gnus-summary-find-next unread)))
5601       (gnus-summary-show-thread)
5602       (setq n (1- n)))
5603     (when (/= 0 n)
5604       (gnus-message 7 "No more%s articles"
5605                     (if unread " unread" "")))
5606     (unless dont-display
5607       (gnus-summary-recenter)
5608       (gnus-summary-position-point))
5609     n))
5610
5611 (defun gnus-summary-next-unread-subject (n)
5612   "Go to next N'th unread summary line."
5613   (interactive "p")
5614   (gnus-summary-next-subject n t))
5615
5616 (defun gnus-summary-prev-subject (n &optional unread)
5617   "Go to previous N'th summary line.
5618 If optional argument UNREAD is non-nil, only unread article is selected."
5619   (interactive "p")
5620   (gnus-summary-next-subject (- n) unread))
5621
5622 (defun gnus-summary-prev-unread-subject (n)
5623   "Go to previous N'th unread summary line."
5624   (interactive "p")
5625   (gnus-summary-next-subject (- n) t))
5626
5627 (defun gnus-summary-goto-subject (article &optional force silent)
5628   "Go the subject line of ARTICLE.
5629 If FORCE, also allow jumping to articles not currently shown."
5630   (interactive "nArticle number: ")
5631   (let ((b (point))
5632         (data (gnus-data-find article)))
5633     ;; We read in the article if we have to.
5634     (and (not data)
5635          force
5636          (gnus-summary-insert-subject
5637           article
5638           (if (or (numberp force) (vectorp force)) force)
5639           t)
5640          (setq data (gnus-data-find article)))
5641     (goto-char b)
5642     (if (not data)
5643         (progn
5644           (unless silent
5645             (gnus-message 3 "Can't find article %d" article))
5646           nil)
5647       (goto-char (gnus-data-pos data))
5648       (gnus-summary-position-point)
5649       article)))
5650
5651 ;; Walking around summary lines with displaying articles.
5652
5653 (defun gnus-summary-expand-window (&optional arg)
5654   "Make the summary buffer take up the entire Emacs frame.
5655 Given a prefix, will force an `article' buffer configuration."
5656   (interactive "P")
5657   (if arg
5658       (gnus-configure-windows 'article 'force)
5659     (gnus-configure-windows 'summary 'force)))
5660
5661 (defun gnus-summary-display-article (article &optional all-header)
5662   "Display ARTICLE in article buffer."
5663   (gnus-set-global-variables)
5664   (if (null article)
5665       nil
5666     (prog1
5667         (if gnus-summary-display-article-function
5668             (funcall gnus-summary-display-article-function article all-header)
5669           (gnus-article-prepare article all-header))
5670       (gnus-run-hooks 'gnus-select-article-hook)
5671       (when (and gnus-current-article
5672                  (not (zerop gnus-current-article)))
5673         (gnus-summary-goto-subject gnus-current-article))
5674       (gnus-summary-recenter)
5675       (when (and gnus-use-trees gnus-show-threads)
5676         (gnus-possibly-generate-tree article)
5677         (gnus-highlight-selected-tree article))
5678       ;; Successfully display article.
5679       (gnus-article-set-window-start
5680        (cdr (assq article gnus-newsgroup-bookmarks))))))
5681
5682 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5683   "Select the current article.
5684 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5685 non-nil, the article will be re-fetched even if it already present in
5686 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5687 be displayed."
5688   ;; Make sure we are in the summary buffer to work around bbdb bug.
5689   (unless (eq major-mode 'gnus-summary-mode)
5690     (set-buffer gnus-summary-buffer))
5691   (let ((article (or article (gnus-summary-article-number)))
5692         (all-headers (not (not all-headers))) ;Must be T or NIL.
5693         gnus-summary-display-article-function
5694         did)
5695     (and (not pseudo)
5696          (gnus-summary-article-pseudo-p article)
5697          (error "This is a pseudo-article"))
5698     (prog1
5699         (save-excursion
5700           (set-buffer gnus-summary-buffer)
5701           (if (or (and gnus-single-article-buffer
5702                        (or (null gnus-current-article)
5703                            (null gnus-article-current)
5704                            (null (get-buffer gnus-article-buffer))
5705                            (not (eq article (cdr gnus-article-current)))
5706                            (not (equal (car gnus-article-current)
5707                                        gnus-newsgroup-name))))
5708                   (and (not gnus-single-article-buffer)
5709                        (or (null gnus-current-article)
5710                            (not (eq gnus-current-article article))))
5711                   force)
5712               ;; The requested article is different from the current article.
5713               (prog1
5714                   (gnus-summary-display-article article all-headers)
5715                 (setq did article)
5716                 (when (or all-headers gnus-show-all-headers)
5717                   (gnus-article-show-all-headers)))
5718             (when (or all-headers gnus-show-all-headers)
5719               (gnus-article-show-all-headers))
5720             'old))
5721       (when did
5722         (gnus-article-set-window-start
5723          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5724
5725 (defun gnus-summary-set-current-mark (&optional current-mark)
5726   "Obsolete function."
5727   nil)
5728
5729 (defun gnus-summary-next-article (&optional unread subject backward push)
5730   "Select the next article.
5731 If UNREAD, only unread articles are selected.
5732 If SUBJECT, only articles with SUBJECT are selected.
5733 If BACKWARD, the previous article is selected instead of the next."
5734   (interactive "P")
5735   (cond
5736    ;; Is there such an article?
5737    ((and (gnus-summary-search-forward unread subject backward)
5738          (or (gnus-summary-display-article (gnus-summary-article-number))
5739              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5740     (gnus-summary-position-point))
5741    ;; If not, we try the first unread, if that is wanted.
5742    ((and subject
5743          gnus-auto-select-same
5744          (gnus-summary-first-unread-article))
5745     (gnus-summary-position-point)
5746     (gnus-message 6 "Wrapped"))
5747    ;; Try to get next/previous article not displayed in this group.
5748    ((and gnus-auto-extend-newsgroup
5749          (not unread) (not subject))
5750     (gnus-summary-goto-article
5751      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5752      nil (count-lines (point-min) (point))))
5753    ;; Go to next/previous group.
5754    (t
5755     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5756       (gnus-summary-jump-to-group gnus-newsgroup-name))
5757     (let ((cmd last-command-char)
5758           (point
5759            (save-excursion
5760              (set-buffer gnus-group-buffer)
5761              (point)))
5762           (group
5763            (if (eq gnus-keep-same-level 'best)
5764                (gnus-summary-best-group gnus-newsgroup-name)
5765              (gnus-summary-search-group backward gnus-keep-same-level))))
5766       ;; For some reason, the group window gets selected.  We change
5767       ;; it back.
5768       (select-window (get-buffer-window (current-buffer)))
5769       ;; Select next unread newsgroup automagically.
5770       (cond
5771        ((or (not gnus-auto-select-next)
5772             (not cmd))
5773         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5774        ((or (eq gnus-auto-select-next 'quietly)
5775             (and (eq gnus-auto-select-next 'slightly-quietly)
5776                  push)
5777             (and (eq gnus-auto-select-next 'almost-quietly)
5778                  (gnus-summary-last-article-p)))
5779         ;; Select quietly.
5780         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5781             (gnus-summary-exit)
5782           (gnus-message 7 "No more%s articles (%s)..."
5783                         (if unread " unread" "")
5784                         (if group (concat "selecting " group)
5785                           "exiting"))
5786           (gnus-summary-next-group nil group backward)))
5787        (t
5788         (when (gnus-key-press-event-p last-input-event)
5789           (gnus-summary-walk-group-buffer
5790            gnus-newsgroup-name cmd unread backward point))))))))
5791
5792 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5793   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5794                       (?\C-p (gnus-group-prev-unread-group 1))))
5795         (cursor-in-echo-area t)
5796         keve key group ended)
5797     (save-excursion
5798       (set-buffer gnus-group-buffer)
5799       (goto-char start)
5800       (setq group
5801             (if (eq gnus-keep-same-level 'best)
5802                 (gnus-summary-best-group gnus-newsgroup-name)
5803               (gnus-summary-search-group backward gnus-keep-same-level))))
5804     (while (not ended)
5805       (gnus-message
5806        5 "No more%s articles%s" (if unread " unread" "")
5807        (if (and group
5808                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5809            (format " (Type %s for %s [%s])"
5810                    (single-key-description cmd) group
5811                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5812          (format " (Type %s to exit %s)"
5813                  (single-key-description cmd)
5814                  gnus-newsgroup-name)))
5815       ;; Confirm auto selection.
5816       (setq key (car (setq keve (gnus-read-event-char))))
5817       (setq ended t)
5818       (cond
5819        ((assq key keystrokes)
5820         (let ((obuf (current-buffer)))
5821           (switch-to-buffer gnus-group-buffer)
5822           (when group
5823             (gnus-group-jump-to-group group))
5824           (eval (cadr (assq key keystrokes)))
5825           (setq group (gnus-group-group-name))
5826           (switch-to-buffer obuf))
5827         (setq ended nil))
5828        ((equal key cmd)
5829         (if (or (not group)
5830                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5831             (gnus-summary-exit)
5832           (gnus-summary-next-group nil group backward)))
5833        (t
5834         (push (cdr keve) unread-command-events))))))
5835
5836 (defun gnus-summary-next-unread-article ()
5837   "Select unread article after current one."
5838   (interactive)
5839   (gnus-summary-next-article
5840    (or (not (eq gnus-summary-goto-unread 'never))
5841        (gnus-summary-last-article-p (gnus-summary-article-number)))
5842    (and gnus-auto-select-same
5843         (gnus-summary-article-subject))))
5844
5845 (defun gnus-summary-prev-article (&optional unread subject)
5846   "Select the article after the current one.
5847 If UNREAD is non-nil, only unread articles are selected."
5848   (interactive "P")
5849   (gnus-summary-next-article unread subject t))
5850
5851 (defun gnus-summary-prev-unread-article ()
5852   "Select unread article before current one."
5853   (interactive)
5854   (gnus-summary-prev-article
5855    (or (not (eq gnus-summary-goto-unread 'never))
5856        (gnus-summary-first-article-p (gnus-summary-article-number)))
5857    (and gnus-auto-select-same
5858         (gnus-summary-article-subject))))
5859
5860 (defun gnus-summary-next-page (&optional lines circular)
5861   "Show next page of the selected article.
5862 If at the end of the current article, select the next article.
5863 LINES says how many lines should be scrolled up.
5864
5865 If CIRCULAR is non-nil, go to the start of the article instead of
5866 selecting the next article when reaching the end of the current
5867 article."
5868   (interactive "P")
5869   (setq gnus-summary-buffer (current-buffer))
5870   (gnus-set-global-variables)
5871   (let ((article (gnus-summary-article-number))
5872         (article-window (get-buffer-window gnus-article-buffer t))
5873         endp)
5874     ;; If the buffer is empty, we have no article.
5875     (unless article
5876       (error "No article to select"))
5877     (gnus-configure-windows 'article)
5878     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5879         (if (and (eq gnus-summary-goto-unread 'never)
5880                  (not (gnus-summary-last-article-p article)))
5881             (gnus-summary-next-article)
5882           (gnus-summary-next-unread-article))
5883       (if (or (null gnus-current-article)
5884               (null gnus-article-current)
5885               (/= article (cdr gnus-article-current))
5886               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5887           ;; Selected subject is different from current article's.
5888           (gnus-summary-display-article article)
5889         (when article-window
5890           (gnus-eval-in-buffer-window gnus-article-buffer
5891             (setq endp (gnus-article-next-page lines)))
5892           (when endp
5893             (cond (circular
5894                    (gnus-summary-beginning-of-article))
5895                   (lines
5896                    (gnus-message 3 "End of message"))
5897                   ((null lines)
5898                    (if (and (eq gnus-summary-goto-unread 'never)
5899                             (not (gnus-summary-last-article-p article)))
5900                        (gnus-summary-next-article)
5901                      (gnus-summary-next-unread-article))))))))
5902     (gnus-summary-recenter)
5903     (gnus-summary-position-point)))
5904
5905 (defun gnus-summary-prev-page (&optional lines move)
5906   "Show previous page of selected article.
5907 Argument LINES specifies lines to be scrolled down.
5908 If MOVE, move to the previous unread article if point is at
5909 the beginning of the buffer."
5910   (interactive "P")
5911   (let ((article (gnus-summary-article-number))
5912         (article-window (get-buffer-window gnus-article-buffer t))
5913         endp)
5914     (gnus-configure-windows 'article)
5915     (if (or (null gnus-current-article)
5916             (null gnus-article-current)
5917             (/= article (cdr gnus-article-current))
5918             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5919         ;; Selected subject is different from current article's.
5920         (gnus-summary-display-article article)
5921       (gnus-summary-recenter)
5922       (when article-window
5923         (gnus-eval-in-buffer-window gnus-article-buffer
5924           (setq endp (gnus-article-prev-page lines)))
5925         (when (and move endp)
5926           (cond (lines
5927                  (gnus-message 3 "Beginning of message"))
5928                 ((null lines)
5929                  (if (and (eq gnus-summary-goto-unread 'never)
5930                           (not (gnus-summary-first-article-p article)))
5931                      (gnus-summary-prev-article)
5932                    (gnus-summary-prev-unread-article))))))))
5933   (gnus-summary-position-point))
5934
5935 (defun gnus-summary-prev-page-or-article (&optional lines)
5936   "Show previous page of selected article.
5937 Argument LINES specifies lines to be scrolled down.
5938 If at the beginning of the article, go to the next article."
5939   (interactive "P")
5940   (gnus-summary-prev-page lines t))
5941
5942 (defun gnus-summary-scroll-up (lines)
5943   "Scroll up (or down) one line current article.
5944 Argument LINES specifies lines to be scrolled up (or down if negative)."
5945   (interactive "p")
5946   (gnus-configure-windows 'article)
5947   (gnus-summary-show-thread)
5948   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5949     (gnus-eval-in-buffer-window gnus-article-buffer
5950       (cond ((> lines 0)
5951              (when (gnus-article-next-page lines)
5952                (gnus-message 3 "End of message")))
5953             ((< lines 0)
5954              (gnus-article-prev-page (- lines))))))
5955   (gnus-summary-recenter)
5956   (gnus-summary-position-point))
5957
5958 (defun gnus-summary-scroll-down (lines)
5959   "Scroll down (or up) one line current article.
5960 Argument LINES specifies lines to be scrolled down (or up if negative)."
5961   (interactive "p")
5962   (gnus-summary-scroll-up (- lines)))
5963
5964 (defun gnus-summary-next-same-subject ()
5965   "Select next article which has the same subject as current one."
5966   (interactive)
5967   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5968
5969 (defun gnus-summary-prev-same-subject ()
5970   "Select previous article which has the same subject as current one."
5971   (interactive)
5972   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5973
5974 (defun gnus-summary-next-unread-same-subject ()
5975   "Select next unread article which has the same subject as current one."
5976   (interactive)
5977   (gnus-summary-next-article t (gnus-summary-article-subject)))
5978
5979 (defun gnus-summary-prev-unread-same-subject ()
5980   "Select previous unread article which has the same subject as current one."
5981   (interactive)
5982   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5983
5984 (defun gnus-summary-first-unread-article ()
5985   "Select the first unread article.
5986 Return nil if there are no unread articles."
5987   (interactive)
5988   (prog1
5989       (when (gnus-summary-first-subject t)
5990         (gnus-summary-show-thread)
5991         (gnus-summary-first-subject t)
5992         (gnus-summary-display-article (gnus-summary-article-number)))
5993     (gnus-summary-position-point)))
5994
5995 (defun gnus-summary-first-unread-subject ()
5996   "Place the point on the subject line of the first unread article.
5997 Return nil if there are no unread articles."
5998   (interactive)
5999   (prog1
6000       (when (gnus-summary-first-subject t)
6001         (gnus-summary-show-thread)
6002         (gnus-summary-first-subject t))
6003     (gnus-summary-position-point)))
6004
6005 (defun gnus-summary-first-article ()
6006   "Select the first article.
6007 Return nil if there are no articles."
6008   (interactive)
6009   (prog1
6010       (when (gnus-summary-first-subject)
6011         (gnus-summary-show-thread)
6012         (gnus-summary-first-subject)
6013         (gnus-summary-display-article (gnus-summary-article-number)))
6014     (gnus-summary-position-point)))
6015
6016 (defun gnus-summary-best-unread-article ()
6017   "Select the unread article with the highest score."
6018   (interactive)
6019   (let ((best -1000000)
6020         (data gnus-newsgroup-data)
6021         article score)
6022     (while data
6023       (and (gnus-data-unread-p (car data))
6024            (> (setq score
6025                     (gnus-summary-article-score (gnus-data-number (car data))))
6026               best)
6027            (setq best score
6028                  article (gnus-data-number (car data))))
6029       (setq data (cdr data)))
6030     (prog1
6031         (if article
6032             (gnus-summary-goto-article article)
6033           (error "No unread articles"))
6034       (gnus-summary-position-point))))
6035
6036 (defun gnus-summary-last-subject ()
6037   "Go to the last displayed subject line in the group."
6038   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6039     (when article
6040       (gnus-summary-goto-subject article))))
6041
6042 (defun gnus-summary-goto-article (article &optional all-headers force)
6043   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6044 If ALL-HEADERS is non-nil, no header lines are hidden.
6045 If FORCE, go to the article even if it isn't displayed.  If FORCE
6046 is a number, it is the line the article is to be displayed on."
6047   (interactive
6048    (list
6049     (completing-read
6050      "Article number or Message-ID: "
6051      (mapcar (lambda (number) (list (int-to-string number)))
6052              gnus-newsgroup-limit))
6053     current-prefix-arg
6054     t))
6055   (prog1
6056       (if (and (stringp article)
6057                (string-match "@" article))
6058           (gnus-summary-refer-article article)
6059         (when (stringp article)
6060           (setq article (string-to-number article)))
6061         (if (gnus-summary-goto-subject article force)
6062             (gnus-summary-display-article article all-headers)
6063           (gnus-message 4 "Couldn't go to article %s" article) nil))
6064     (gnus-summary-position-point)))
6065
6066 (defun gnus-summary-goto-last-article ()
6067   "Go to the previously read article."
6068   (interactive)
6069   (prog1
6070       (when gnus-last-article
6071         (gnus-summary-goto-article gnus-last-article nil t))
6072     (gnus-summary-position-point)))
6073
6074 (defun gnus-summary-pop-article (number)
6075   "Pop one article off the history and go to the previous.
6076 NUMBER articles will be popped off."
6077   (interactive "p")
6078   (let (to)
6079     (setq gnus-newsgroup-history
6080           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6081     (if to
6082         (gnus-summary-goto-article (car to) nil t)
6083       (error "Article history empty")))
6084   (gnus-summary-position-point))
6085
6086 ;; Summary commands and functions for limiting the summary buffer.
6087
6088 (defun gnus-summary-limit-to-articles (n)
6089   "Limit the summary buffer to the next N articles.
6090 If not given a prefix, use the process marked articles instead."
6091   (interactive "P")
6092   (prog1
6093       (let ((articles (gnus-summary-work-articles n)))
6094         (setq gnus-newsgroup-processable nil)
6095         (gnus-summary-limit articles))
6096     (gnus-summary-position-point)))
6097
6098 (defun gnus-summary-pop-limit (&optional total)
6099   "Restore the previous limit.
6100 If given a prefix, remove all limits."
6101   (interactive "P")
6102   (when total
6103     (setq gnus-newsgroup-limits
6104           (list (mapcar (lambda (h) (mail-header-number h))
6105                         gnus-newsgroup-headers))))
6106   (unless gnus-newsgroup-limits
6107     (error "No limit to pop"))
6108   (prog1
6109       (gnus-summary-limit nil 'pop)
6110     (gnus-summary-position-point)))
6111
6112 (defun gnus-summary-limit-to-subject (subject &optional header)
6113   "Limit the summary buffer to articles that have subjects that match a regexp."
6114   (interactive "sLimit to subject (regexp): ")
6115   (unless header
6116     (setq header "subject"))
6117   (when (not (equal "" subject))
6118     (prog1
6119         (let ((articles (gnus-summary-find-matching
6120                          (or header "subject") subject 'all)))
6121           (unless articles
6122             (error "Found no matches for \"%s\"" subject))
6123           (gnus-summary-limit articles))
6124       (gnus-summary-position-point))))
6125
6126 (defun gnus-summary-limit-to-author (from)
6127   "Limit the summary buffer to articles that have authors that match a regexp."
6128   (interactive "sLimit to author (regexp): ")
6129   (gnus-summary-limit-to-subject from "from"))
6130
6131 (defun gnus-summary-limit-to-age (age &optional younger-p)
6132   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6133 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6134 articles that are younger than AGE days."
6135   (interactive "nTime in days: \nP")
6136   (prog1
6137       (let ((data gnus-newsgroup-data)
6138             (cutoff (days-to-time age))
6139             articles d date is-younger)
6140         (while (setq d (pop data))
6141           (when (and (vectorp (gnus-data-header d))
6142                      (setq date (mail-header-date (gnus-data-header d))))
6143             (setq is-younger (time-less-p
6144                               (time-since (date-to-time date))
6145                               cutoff))
6146             (when (if younger-p
6147                       is-younger
6148                     (not is-younger))
6149               (push (gnus-data-number d) articles))))
6150         (gnus-summary-limit (nreverse articles)))
6151     (gnus-summary-position-point)))
6152
6153 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6154 (make-obsolete
6155  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6156
6157 (defun gnus-summary-limit-to-unread (&optional all)
6158   "Limit the summary buffer to articles that are not marked as read.
6159 If ALL is non-nil, limit strictly to unread articles."
6160   (interactive "P")
6161   (if all
6162       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6163     (gnus-summary-limit-to-marks
6164      ;; Concat all the marks that say that an article is read and have
6165      ;; those removed.
6166      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6167            gnus-killed-mark gnus-kill-file-mark
6168            gnus-low-score-mark gnus-expirable-mark
6169            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6170            gnus-duplicate-mark gnus-souped-mark)
6171      'reverse)))
6172
6173 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6174 (make-obsolete 'gnus-summary-delete-marked-with
6175                'gnus-summary-limit-exlude-marks)
6176
6177 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6178   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6179 If REVERSE, limit the summary buffer to articles that are marked
6180 with MARKS.  MARKS can either be a string of marks or a list of marks.
6181 Returns how many articles were removed."
6182   (interactive "sMarks: ")
6183   (gnus-summary-limit-to-marks marks t))
6184
6185 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6186   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6187 If REVERSE (the prefix), limit the summary buffer to articles that are
6188 not marked with MARKS.  MARKS can either be a string of marks or a
6189 list of marks.
6190 Returns how many articles were removed."
6191   (interactive "sMarks: \nP")
6192   (prog1
6193       (let ((data gnus-newsgroup-data)
6194             (marks (if (listp marks) marks
6195                      (append marks nil))) ; Transform to list.
6196             articles)
6197         (while data
6198           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6199                   (memq (gnus-data-mark (car data)) marks))
6200             (push (gnus-data-number (car data)) articles))
6201           (setq data (cdr data)))
6202         (gnus-summary-limit articles))
6203     (gnus-summary-position-point)))
6204
6205 (defun gnus-summary-limit-to-score (&optional score)
6206   "Limit to articles with score at or above SCORE."
6207   (interactive "P")
6208   (setq score (if score
6209                   (prefix-numeric-value score)
6210                 (or gnus-summary-default-score 0)))
6211   (let ((data gnus-newsgroup-data)
6212         articles)
6213     (while data
6214       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6215                 score)
6216         (push (gnus-data-number (car data)) articles))
6217       (setq data (cdr data)))
6218     (prog1
6219         (gnus-summary-limit articles)
6220       (gnus-summary-position-point))))
6221
6222 (defun gnus-summary-limit-include-thread (id)
6223   "Display all the hidden articles that in the current thread."
6224   (interactive (list (mail-header-id (gnus-summary-article-header))))
6225   (let ((articles (gnus-articles-in-thread
6226                    (gnus-id-to-thread (gnus-root-id id)))))
6227     (prog1
6228         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6229       (gnus-summary-position-point))))
6230
6231 (defun gnus-summary-limit-include-dormant ()
6232   "Display all the hidden articles that are marked as dormant.
6233 Note that this command only works on a subset of the articles currently
6234 fetched for this group."
6235   (interactive)
6236   (unless gnus-newsgroup-dormant
6237     (error "There are no dormant articles in this group"))
6238   (prog1
6239       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6240     (gnus-summary-position-point)))
6241
6242 (defun gnus-summary-limit-exclude-dormant ()
6243   "Hide all dormant articles."
6244   (interactive)
6245   (prog1
6246       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6247     (gnus-summary-position-point)))
6248
6249 (defun gnus-summary-limit-exclude-childless-dormant ()
6250   "Hide all dormant articles that have no children."
6251   (interactive)
6252   (let ((data (gnus-data-list t))
6253         articles d children)
6254     ;; Find all articles that are either not dormant or have
6255     ;; children.
6256     (while (setq d (pop data))
6257       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6258                 (and (setq children
6259                            (gnus-article-children (gnus-data-number d)))
6260                      (let (found)
6261                        (while children
6262                          (when (memq (car children) articles)
6263                            (setq children nil
6264                                  found t))
6265                          (pop children))
6266                        found)))
6267         (push (gnus-data-number d) articles)))
6268     ;; Do the limiting.
6269     (prog1
6270         (gnus-summary-limit articles)
6271       (gnus-summary-position-point))))
6272
6273 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6274   "Mark all unread excluded articles as read.
6275 If ALL, mark even excluded ticked and dormants as read."
6276   (interactive "P")
6277   (let ((articles (gnus-sorted-complement
6278                    (sort
6279                     (mapcar (lambda (h) (mail-header-number h))
6280                             gnus-newsgroup-headers)
6281                     '<)
6282                    (sort gnus-newsgroup-limit '<)))
6283         article)
6284     (setq gnus-newsgroup-unreads
6285           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6286     (if all
6287         (setq gnus-newsgroup-dormant nil
6288               gnus-newsgroup-marked nil
6289               gnus-newsgroup-reads
6290               (nconc
6291                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6292                gnus-newsgroup-reads))
6293       (while (setq article (pop articles))
6294         (unless (or (memq article gnus-newsgroup-dormant)
6295                     (memq article gnus-newsgroup-marked))
6296           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6297
6298 (defun gnus-summary-limit (articles &optional pop)
6299   (if pop
6300       ;; We pop the previous limit off the stack and use that.
6301       (setq articles (car gnus-newsgroup-limits)
6302             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6303     ;; We use the new limit, so we push the old limit on the stack.
6304     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6305   ;; Set the limit.
6306   (setq gnus-newsgroup-limit articles)
6307   (let ((total (length gnus-newsgroup-data))
6308         (data (gnus-data-find-list (gnus-summary-article-number)))
6309         (gnus-summary-mark-below nil)   ; Inhibit this.
6310         found)
6311     ;; This will do all the work of generating the new summary buffer
6312     ;; according to the new limit.
6313     (gnus-summary-prepare)
6314     ;; Hide any threads, possibly.
6315     (and gnus-show-threads
6316          gnus-thread-hide-subtree
6317          (gnus-summary-hide-all-threads))
6318     ;; Try to return to the article you were at, or one in the
6319     ;; neighborhood.
6320     (when data
6321       ;; We try to find some article after the current one.
6322       (while data
6323         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6324           (setq data nil
6325                 found t))
6326         (setq data (cdr data))))
6327     (unless found
6328       ;; If there is no data, that means that we were after the last
6329       ;; article.  The same goes when we can't find any articles
6330       ;; after the current one.
6331       (goto-char (point-max))
6332       (gnus-summary-find-prev))
6333     (gnus-set-mode-line 'summary)
6334     ;; We return how many articles were removed from the summary
6335     ;; buffer as a result of the new limit.
6336     (- total (length gnus-newsgroup-data))))
6337
6338 (defsubst gnus-invisible-cut-children (threads)
6339   (let ((num 0))
6340     (while threads
6341       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6342         (incf num))
6343       (pop threads))
6344     (< num 2)))
6345
6346 (defsubst gnus-cut-thread (thread)
6347   "Go forwards in the thread until we find an article that we want to display."
6348   (when (or (eq gnus-fetch-old-headers 'some)
6349             (eq gnus-fetch-old-headers 'invisible)
6350             (eq gnus-build-sparse-threads 'some)
6351             (eq gnus-build-sparse-threads 'more))
6352     ;; Deal with old-fetched headers and sparse threads.
6353     (while (and
6354             thread
6355             (or
6356              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6357              (gnus-summary-article-ancient-p
6358               (mail-header-number (car thread))))
6359             (if (or (<= (length (cdr thread)) 1)
6360                     (eq gnus-fetch-old-headers 'invisible))
6361                 (setq gnus-newsgroup-limit
6362                       (delq (mail-header-number (car thread))
6363                             gnus-newsgroup-limit)
6364                       thread (cadr thread))
6365               (when (gnus-invisible-cut-children (cdr thread))
6366                 (let ((th (cdr thread)))
6367                   (while th
6368                     (if (memq (mail-header-number (caar th))
6369                               gnus-newsgroup-limit)
6370                         (setq thread (car th)
6371                               th nil)
6372                       (setq th (cdr th))))))))))
6373   thread)
6374
6375 (defun gnus-cut-threads (threads)
6376   "Cut off all uninteresting articles from the beginning of threads."
6377   (when (or (eq gnus-fetch-old-headers 'some)
6378             (eq gnus-fetch-old-headers 'invisible)
6379             (eq gnus-build-sparse-threads 'some)
6380             (eq gnus-build-sparse-threads 'more))
6381     (let ((th threads))
6382       (while th
6383         (setcar th (gnus-cut-thread (car th)))
6384         (setq th (cdr th)))))
6385   ;; Remove nixed out threads.
6386   (delq nil threads))
6387
6388 (defun gnus-summary-initial-limit (&optional show-if-empty)
6389   "Figure out what the initial limit is supposed to be on group entry.
6390 This entails weeding out unwanted dormants, low-scored articles,
6391 fetch-old-headers verbiage, and so on."
6392   ;; Most groups have nothing to remove.
6393   (if (or gnus-inhibit-limiting
6394           (and (null gnus-newsgroup-dormant)
6395                (not (eq gnus-fetch-old-headers 'some))
6396                (not (eq gnus-fetch-old-headers 'invisible))
6397                (null gnus-summary-expunge-below)
6398                (not (eq gnus-build-sparse-threads 'some))
6399                (not (eq gnus-build-sparse-threads 'more))
6400                (null gnus-thread-expunge-below)
6401                (not gnus-use-nocem)))
6402       ()                                ; Do nothing.
6403     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6404     (setq gnus-newsgroup-limit nil)
6405     (mapatoms
6406      (lambda (node)
6407        (unless (car (symbol-value node))
6408          ;; These threads have no parents -- they are roots.
6409          (let ((nodes (cdr (symbol-value node)))
6410                thread)
6411            (while nodes
6412              (if (and gnus-thread-expunge-below
6413                       (< (gnus-thread-total-score (car nodes))
6414                          gnus-thread-expunge-below))
6415                  (gnus-expunge-thread (pop nodes))
6416                (setq thread (pop nodes))
6417                (gnus-summary-limit-children thread))))))
6418      gnus-newsgroup-dependencies)
6419     ;; If this limitation resulted in an empty group, we might
6420     ;; pop the previous limit and use it instead.
6421     (when (and (not gnus-newsgroup-limit)
6422                show-if-empty)
6423       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6424     gnus-newsgroup-limit))
6425
6426 (defun gnus-summary-limit-children (thread)
6427   "Return 1 if this subthread is visible and 0 if it is not."
6428   ;; First we get the number of visible children to this thread.  This
6429   ;; is done by recursing down the thread using this function, so this
6430   ;; will really go down to a leaf article first, before slowly
6431   ;; working its way up towards the root.
6432   (when thread
6433     (let ((children
6434            (if (cdr thread)
6435                (apply '+ (mapcar 'gnus-summary-limit-children
6436                                  (cdr thread)))
6437              0))
6438           (number (mail-header-number (car thread)))
6439           score)
6440       (if (and
6441            (not (memq number gnus-newsgroup-marked))
6442            (or
6443             ;; If this article is dormant and has absolutely no visible
6444             ;; children, then this article isn't visible.
6445             (and (memq number gnus-newsgroup-dormant)
6446                  (zerop children))
6447             ;; If this is "fetch-old-headered" and there is no
6448             ;; visible children, then we don't want this article.
6449             (and (eq gnus-fetch-old-headers 'some)
6450                  (gnus-summary-article-ancient-p number)
6451                  (zerop children))
6452             ;; If this is "fetch-old-headered" and `invisible', then
6453             ;; we don't want this article.
6454             (and (eq gnus-fetch-old-headers 'invisible)
6455                  (gnus-summary-article-ancient-p number))
6456             ;; If this is a sparsely inserted article with no children,
6457             ;; we don't want it.
6458             (and (eq gnus-build-sparse-threads 'some)
6459                  (gnus-summary-article-sparse-p number)
6460                  (zerop children))
6461             ;; If we use expunging, and this article is really
6462             ;; low-scored, then we don't want this article.
6463             (when (and gnus-summary-expunge-below
6464                        (< (setq score
6465                                 (or (cdr (assq number gnus-newsgroup-scored))
6466                                     gnus-summary-default-score))
6467                           gnus-summary-expunge-below))
6468               ;; We increase the expunge-tally here, but that has
6469               ;; nothing to do with the limits, really.
6470               (incf gnus-newsgroup-expunged-tally)
6471               ;; We also mark as read here, if that's wanted.
6472               (when (and gnus-summary-mark-below
6473                          (< score gnus-summary-mark-below))
6474                 (setq gnus-newsgroup-unreads
6475                       (delq number gnus-newsgroup-unreads))
6476                 (if gnus-newsgroup-auto-expire
6477                     (push number gnus-newsgroup-expirable)
6478                   (push (cons number gnus-low-score-mark)
6479                         gnus-newsgroup-reads)))
6480               t)
6481             ;; Check NoCeM things.
6482             (if (and gnus-use-nocem
6483                      (gnus-nocem-unwanted-article-p
6484                       (mail-header-id (car thread))))
6485                 (progn
6486                   (setq gnus-newsgroup-unreads
6487                         (delq number gnus-newsgroup-unreads))
6488                   t))))
6489           ;; Nope, invisible article.
6490           0
6491         ;; Ok, this article is to be visible, so we add it to the limit
6492         ;; and return 1.
6493         (push number gnus-newsgroup-limit)
6494         1))))
6495
6496 (defun gnus-expunge-thread (thread)
6497   "Mark all articles in THREAD as read."
6498   (let* ((number (mail-header-number (car thread))))
6499     (incf gnus-newsgroup-expunged-tally)
6500     ;; We also mark as read here, if that's wanted.
6501     (setq gnus-newsgroup-unreads
6502           (delq number gnus-newsgroup-unreads))
6503     (if gnus-newsgroup-auto-expire
6504         (push number gnus-newsgroup-expirable)
6505       (push (cons number gnus-low-score-mark)
6506             gnus-newsgroup-reads)))
6507   ;; Go recursively through all subthreads.
6508   (mapcar 'gnus-expunge-thread (cdr thread)))
6509
6510 ;; Summary article oriented commands
6511
6512 (defun gnus-summary-refer-parent-article (n)
6513   "Refer parent article N times.
6514 If N is negative, go to ancestor -N instead.
6515 The difference between N and the number of articles fetched is returned."
6516   (interactive "p")
6517   (let ((skip 1)
6518         error header ref)
6519     (when (not (natnump n))
6520       (setq skip (abs n)
6521             n 1))
6522     (while (and (> n 0)
6523                 (not error))
6524       (setq header (gnus-summary-article-header))
6525       (if (and (eq (mail-header-number header)
6526                    (cdr gnus-article-current))
6527                (equal gnus-newsgroup-name
6528                       (car gnus-article-current)))
6529           ;; If we try to find the parent of the currently
6530           ;; displayed article, then we take a look at the actual
6531           ;; References header, since this is slightly more
6532           ;; reliable than the References field we got from the
6533           ;; server.
6534           (save-excursion
6535             (set-buffer gnus-original-article-buffer)
6536             (nnheader-narrow-to-headers)
6537             (unless (setq ref (message-fetch-field "references"))
6538               (setq ref (message-fetch-field "in-reply-to")))
6539             (widen))
6540         (setq ref
6541               ;; It's not the current article, so we take a bet on
6542               ;; the value we got from the server.
6543               (mail-header-references header)))
6544       (if (and ref
6545                (not (equal ref "")))
6546           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6547             (gnus-message 1 "Couldn't find parent"))
6548         (gnus-message 1 "No references in article %d"
6549                       (gnus-summary-article-number))
6550         (setq error t))
6551       (decf n))
6552     (gnus-summary-position-point)
6553     n))
6554
6555 (defun gnus-summary-refer-references ()
6556   "Fetch all articles mentioned in the References header.
6557 Return the number of articles fetched."
6558   (interactive)
6559   (let ((ref (mail-header-references (gnus-summary-article-header)))
6560         (current (gnus-summary-article-number))
6561         (n 0))
6562     (if (or (not ref)
6563             (equal ref ""))
6564         (error "No References in the current article")
6565       ;; For each Message-ID in the References header...
6566       (while (string-match "<[^>]*>" ref)
6567         (incf n)
6568         ;; ... fetch that article.
6569         (gnus-summary-refer-article
6570          (prog1 (match-string 0 ref)
6571            (setq ref (substring ref (match-end 0))))))
6572       (gnus-summary-goto-subject current)
6573       (gnus-summary-position-point)
6574       n)))
6575
6576 (defun gnus-summary-refer-thread (&optional limit)
6577   "Fetch all articles in the current thread.
6578 If LIMIT (the numerical prefix), fetch that many old headers instead
6579 of what's specified by the `gnus-refer-thread-limit' variable."
6580   (interactive "P")
6581   (let ((id (mail-header-id (gnus-summary-article-header)))
6582         (limit (if limit (prefix-numeric-value limit)
6583                  gnus-refer-thread-limit)))
6584     ;; We want to fetch LIMIT *old* headers, but we also have to
6585     ;; re-fetch all the headers in the current buffer, because many of
6586     ;; them may be undisplayed.  So we adjust LIMIT.
6587     (when (numberp limit)
6588       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6589     (unless (eq gnus-fetch-old-headers 'invisible)
6590       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6591       ;; Retrieve the headers and read them in.
6592       (if (eq (gnus-retrieve-headers
6593                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6594               'nov)
6595           (gnus-build-all-threads)
6596         (error "Can't fetch thread from backends that don't support NOV"))
6597       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6598     (gnus-summary-limit-include-thread id)))
6599
6600 (defun gnus-summary-refer-article (message-id &optional arg)
6601   "Fetch an article specified by MESSAGE-ID.
6602 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6603 or `gnus-select-method', no matter what backend the article comes from."
6604   (interactive "sMessage-ID: \nP")
6605   (when (and (stringp message-id)
6606              (not (zerop (length message-id))))
6607     ;; Construct the correct Message-ID if necessary.
6608     ;; Suggested by tale@pawl.rpi.edu.
6609     (unless (string-match "^<" message-id)
6610       (setq message-id (concat "<" message-id)))
6611     (unless (string-match ">$" message-id)
6612       (setq message-id (concat message-id ">")))
6613     (let* ((header (gnus-id-to-header message-id))
6614            (sparse (and header
6615                         (gnus-summary-article-sparse-p
6616                          (mail-header-number header))
6617                         (memq (mail-header-number header)
6618                               gnus-newsgroup-limit))))
6619       (cond
6620        ;; If the article is present in the buffer we just go to it.
6621        ((and header
6622              (or (not (gnus-summary-article-sparse-p
6623                        (mail-header-number header)))
6624                  sparse))
6625         (prog1
6626             (gnus-summary-goto-article
6627              (mail-header-number header) nil t)
6628           (when sparse
6629             (gnus-summary-update-article (mail-header-number header)))))
6630        (t
6631         ;; We fetch the article
6632         (let ((gnus-override-method
6633                (cond ((gnus-news-group-p gnus-newsgroup-name)
6634                       gnus-refer-article-method)
6635                      (arg
6636                       (or gnus-refer-article-method gnus-select-method))
6637                      (t nil)))
6638               number)
6639           ;; Start the special refer-article method, if necessary.
6640           (when (and gnus-refer-article-method
6641                      (gnus-news-group-p gnus-newsgroup-name))
6642             (gnus-check-server gnus-refer-article-method))
6643           ;; Fetch the header, and display the article.
6644           (if (setq number (gnus-summary-insert-subject message-id))
6645               (gnus-summary-select-article nil nil nil number)
6646             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6647
6648 (defun gnus-summary-edit-parameters ()
6649   "Edit the group parameters of the current group."
6650   (interactive)
6651   (gnus-group-edit-group gnus-newsgroup-name 'params))
6652
6653 (defun gnus-summary-customize-parameters ()
6654   "Customize the group parameters of the current group."
6655   (interactive)
6656   (gnus-group-customize gnus-newsgroup-name))
6657
6658 (defun gnus-summary-enter-digest-group (&optional force)
6659   "Enter an nndoc group based on the current article.
6660 If FORCE, force a digest interpretation.  If not, try
6661 to guess what the document format is."
6662   (interactive "P")
6663   (let ((conf gnus-current-window-configuration))
6664     (save-excursion
6665       (gnus-summary-select-article))
6666     (setq gnus-current-window-configuration conf)
6667     (let* ((name (format "%s-%d"
6668                          (gnus-group-prefixed-name
6669                           gnus-newsgroup-name (list 'nndoc ""))
6670                          (save-excursion
6671                            (set-buffer gnus-summary-buffer)
6672                            gnus-current-article)))
6673            (ogroup gnus-newsgroup-name)
6674            (params (append (gnus-info-params (gnus-get-info ogroup))
6675                            (list (cons 'to-group ogroup))
6676                            (list (cons 'save-article-group ogroup))))
6677            (case-fold-search t)
6678            (buf (current-buffer))
6679            dig)
6680       (save-excursion
6681         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6682         (insert-buffer-substring gnus-original-article-buffer)
6683         ;; Remove lines that may lead nndoc to misinterpret the
6684         ;; document type.
6685         (narrow-to-region
6686          (goto-char (point-min))
6687          (or (search-forward "\n\n" nil t) (point)))
6688         (goto-char (point-min))
6689         (delete-matching-lines "^\\(Path\\):\\|^From ")
6690         (widen))
6691       (unwind-protect
6692           (if (gnus-group-read-ephemeral-group
6693                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6694                             (nndoc-article-type
6695                              ,(if force 'digest 'guess))) t)
6696               ;; Make all postings to this group go to the parent group.
6697               (nconc (gnus-info-params (gnus-get-info name))
6698                      params)
6699             ;; Couldn't select this doc group.
6700             (switch-to-buffer buf)
6701             (gnus-set-global-variables)
6702             (gnus-configure-windows 'summary)
6703             (gnus-message 3 "Article couldn't be entered?"))
6704         (kill-buffer dig)))))
6705
6706 (defun gnus-summary-read-document (n)
6707   "Open a new group based on the current article(s).
6708 This will allow you to read digests and other similar
6709 documents as newsgroups.
6710 Obeys the standard process/prefix convention."
6711   (interactive "P")
6712   (let* ((articles (gnus-summary-work-articles n))
6713          (ogroup gnus-newsgroup-name)
6714          (params (append (gnus-info-params (gnus-get-info ogroup))
6715                          (list (cons 'to-group ogroup))))
6716          article group egroup groups vgroup)
6717     (while (setq article (pop articles))
6718       (setq group (format "%s-%d" gnus-newsgroup-name article))
6719       (gnus-summary-remove-process-mark article)
6720       (when (gnus-summary-display-article article)
6721         (save-excursion
6722           (with-temp-buffer
6723             (insert-buffer-substring gnus-original-article-buffer)
6724             ;; Remove some headers that may lead nndoc to make
6725             ;; the wrong guess.
6726             (message-narrow-to-head)
6727             (goto-char (point-min))
6728             (delete-matching-lines "^\\(Path\\):\\|^From ")
6729             (widen)
6730             (if (setq egroup
6731                       (gnus-group-read-ephemeral-group
6732                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6733                                      (nndoc-article-type guess))
6734                        t nil t))
6735                 (progn
6736                   ;; Make all postings to this group go to the parent group.
6737                   (nconc (gnus-info-params (gnus-get-info egroup))
6738                          params)
6739                   (push egroup groups))
6740               ;; Couldn't select this doc group.
6741               (gnus-error 3 "Article couldn't be entered"))))))
6742     ;; Now we have selected all the documents.
6743     (cond
6744      ((not groups)
6745       (error "None of the articles could be interpreted as documents"))
6746      ((gnus-group-read-ephemeral-group
6747        (setq vgroup (format
6748                      "nnvirtual:%s-%s" gnus-newsgroup-name
6749                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6750        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6751        t
6752        (cons (current-buffer) 'summary)))
6753      (t
6754       (error "Couldn't select virtual nndoc group")))))
6755
6756 (defun gnus-summary-isearch-article (&optional regexp-p)
6757   "Do incremental search forward on the current article.
6758 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6759   (interactive "P")
6760   (gnus-summary-select-article)
6761   (gnus-configure-windows 'article)
6762   (gnus-eval-in-buffer-window gnus-article-buffer
6763     (save-restriction
6764       (widen)
6765       (isearch-forward regexp-p))))
6766
6767 (defun gnus-summary-search-article-forward (regexp &optional backward)
6768   "Search for an article containing REGEXP forward.
6769 If BACKWARD, search backward instead."
6770   (interactive
6771    (list (read-string
6772           (format "Search article %s (regexp%s): "
6773                   (if current-prefix-arg "backward" "forward")
6774                   (if gnus-last-search-regexp
6775                       (concat ", default " gnus-last-search-regexp)
6776                     "")))
6777          current-prefix-arg))
6778   (if (string-equal regexp "")
6779       (setq regexp (or gnus-last-search-regexp ""))
6780     (setq gnus-last-search-regexp regexp))
6781   (if (gnus-summary-search-article regexp backward)
6782       (gnus-summary-show-thread)
6783     (error "Search failed: \"%s\"" regexp)))
6784
6785 (defun gnus-summary-search-article-backward (regexp)
6786   "Search for an article containing REGEXP backward."
6787   (interactive
6788    (list (read-string
6789           (format "Search article backward (regexp%s): "
6790                   (if gnus-last-search-regexp
6791                       (concat ", default " gnus-last-search-regexp)
6792                     "")))))
6793   (gnus-summary-search-article-forward regexp 'backward))
6794
6795 (defun gnus-summary-search-article (regexp &optional backward)
6796   "Search for an article containing REGEXP.
6797 Optional argument BACKWARD means do search for backward.
6798 `gnus-select-article-hook' is not called during the search."
6799   ;; We have to require this here to make sure that the following
6800   ;; dynamic binding isn't shadowed by autoloading.
6801   (require 'gnus-async)
6802   (require 'gnus-art)
6803   (let ((gnus-select-article-hook nil)  ;Disable hook.
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 first 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-prepare-hook
6987           gnus-article-decode-hook
6988           gnus-display-mime-function
6989           gnus-break-pages
6990           gnus-visual)
6991       ;; Destroy any MIME parts.
6992       (when (gnus-buffer-live-p gnus-article-buffer)
6993         (save-excursion
6994           (set-buffer gnus-article-buffer)
6995           (mm-destroy-parts gnus-article-mime-handles)))
6996       (gnus-summary-select-article nil 'force)))
6997   (gnus-summary-goto-subject gnus-current-article)
6998   (gnus-summary-position-point))
6999
7000 (defun gnus-summary-verbose-headers (&optional arg)
7001   "Toggle permanent full header display.
7002 If ARG is a positive number, turn header display on.
7003 If ARG is a negative number, turn header display off."
7004   (interactive "P")
7005   (setq gnus-show-all-headers
7006         (cond ((or (not (numberp arg))
7007                    (zerop arg))
7008                (not gnus-show-all-headers))
7009               ((natnump arg)
7010                t)))
7011   (gnus-summary-show-article))
7012
7013 (defun gnus-summary-toggle-header (&optional arg)
7014   "Show the headers if they are hidden, or hide them if they are shown.
7015 If ARG is a positive number, show the entire header.
7016 If ARG is a negative number, hide the unwanted header lines."
7017   (interactive "P")
7018   (save-excursion
7019     (set-buffer gnus-article-buffer)
7020     (save-restriction
7021       (let* ((buffer-read-only nil)
7022              (inhibit-point-motion-hooks t)
7023              hidden e)
7024         (save-restriction 
7025           (message-narrow-to-head)
7026           (setq hidden (gnus-article-hidden-text-p 'headers)))
7027         (goto-char (point-min))
7028         (when (search-forward "\n\n" nil t)
7029           (delete-region (point-min) (1- (point))))
7030         (goto-char (point-min))
7031         (save-excursion
7032           (set-buffer gnus-original-article-buffer)
7033           (goto-char (point-min))
7034           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7035         (insert-buffer-substring gnus-original-article-buffer 1 e)
7036         (save-restriction
7037           (narrow-to-region (point-min) (point))
7038           (if (or hidden
7039                   (and (numberp arg) (< arg 0)))
7040               (let ((gnus-treat-hide-headers nil)
7041                     (gnus-treat-hide-boring-headers nil))
7042                 (gnus-treat-article 'head))
7043             (gnus-treat-article 'head)))))))
7044
7045 (defun gnus-summary-show-all-headers ()
7046   "Make all header lines visible."
7047   (interactive)
7048   (gnus-article-show-all-headers))
7049
7050 (defun gnus-summary-caesar-message (&optional arg)
7051   "Caesar rotate the current article by 13.
7052 The numerical prefix specifies how many places to rotate each letter
7053 forward."
7054   (interactive "P")
7055   (gnus-summary-select-article)
7056   (let ((mail-header-separator ""))
7057     (gnus-eval-in-buffer-window gnus-article-buffer
7058       (save-restriction
7059         (widen)
7060         (let ((start (window-start))
7061               buffer-read-only)
7062           (message-caesar-buffer-body arg)
7063           (set-window-start (get-buffer-window (current-buffer)) start))))))
7064
7065 (defun gnus-summary-stop-page-breaking ()
7066   "Stop page breaking in the current article."
7067   (interactive)
7068   (gnus-summary-select-article)
7069   (gnus-eval-in-buffer-window gnus-article-buffer
7070     (widen)
7071     (when (gnus-visual-p 'page-marker)
7072       (let ((buffer-read-only nil))
7073         (gnus-remove-text-with-property 'gnus-prev)
7074         (gnus-remove-text-with-property 'gnus-next))
7075       (setq gnus-page-broken nil))))
7076
7077 (defun gnus-summary-move-article (&optional n to-newsgroup
7078                                             select-method action)
7079   "Move the current article to a different newsgroup.
7080 If N is a positive number, move the N next articles.
7081 If N is a negative number, move the N previous articles.
7082 If N is nil and any articles have been marked with the process mark,
7083 move those articles instead.
7084 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7085 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7086 re-spool using this method.
7087
7088 For this function to work, both the current newsgroup and the
7089 newsgroup that you want to move to have to support the `request-move'
7090 and `request-accept' functions."
7091   (interactive "P")
7092   (unless action
7093     (setq action 'move))
7094   ;; Disable marking as read.
7095   (let (gnus-mark-article-hook)
7096     (save-window-excursion
7097       (gnus-summary-select-article)))
7098   ;; Check whether the source group supports the required functions.
7099   (cond ((and (eq action 'move)
7100               (not (gnus-check-backend-function
7101                     'request-move-article gnus-newsgroup-name)))
7102          (error "The current group does not support article moving"))
7103         ((and (eq action 'crosspost)
7104               (not (gnus-check-backend-function
7105                     'request-replace-article gnus-newsgroup-name)))
7106          (error "The current group does not support article editing")))
7107   (let ((articles (gnus-summary-work-articles n))
7108         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7109         (names '((move "Move" "Moving")
7110                  (copy "Copy" "Copying")
7111                  (crosspost "Crosspost" "Crossposting")))
7112         (copy-buf (save-excursion
7113                     (nnheader-set-temp-buffer " *copy article*")))
7114         art-group to-method new-xref article to-groups)
7115     (unless (assq action names)
7116       (error "Unknown action %s" action))
7117     ;; Read the newsgroup name.
7118     (when (and (not to-newsgroup)
7119                (not select-method))
7120       (setq to-newsgroup
7121             (gnus-read-move-group-name
7122              (cadr (assq action names))
7123              (symbol-value (intern (format "gnus-current-%s-group" action)))
7124              articles prefix))
7125       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7126     (setq to-method (or select-method
7127                         (gnus-group-name-to-method to-newsgroup)))
7128     ;; Check the method we are to move this article to...
7129     (unless (gnus-check-backend-function
7130              'request-accept-article (car to-method))
7131       (error "%s does not support article copying" (car to-method)))
7132     (unless (gnus-check-server to-method)
7133       (error "Can't open server %s" (car to-method)))
7134     (gnus-message 6 "%s to %s: %s..."
7135                   (caddr (assq action names))
7136                   (or (car select-method) to-newsgroup) articles)
7137     (while articles
7138       (setq article (pop articles))
7139       (setq
7140        art-group
7141        (cond
7142         ;; Move the article.
7143         ((eq action 'move)
7144          ;; Remove this article from future suppression.
7145          (gnus-dup-unsuppress-article article)
7146          (gnus-request-move-article
7147           article                       ; Article to move
7148           gnus-newsgroup-name           ; From newsgroup
7149           (nth 1 (gnus-find-method-for-group
7150                   gnus-newsgroup-name)) ; Server
7151           (list 'gnus-request-accept-article
7152                 to-newsgroup (list 'quote select-method)
7153                 (not articles) t)               ; Accept form
7154           (not articles)))              ; Only save nov last time
7155         ;; Copy the article.
7156         ((eq action 'copy)
7157          (save-excursion
7158            (set-buffer copy-buf)
7159            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7160              (gnus-request-accept-article
7161               to-newsgroup select-method (not articles) t))))
7162         ;; Crosspost the article.
7163         ((eq action 'crosspost)
7164          (let ((xref (message-tokenize-header
7165                       (mail-header-xref (gnus-summary-article-header article))
7166                       " ")))
7167            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7168                                   ":" article))
7169            (unless xref
7170              (setq xref (list (system-name))))
7171            (setq new-xref
7172                  (concat
7173                   (mapconcat 'identity
7174                              (delete "Xref:" (delete new-xref xref))
7175                              " ")
7176                   " " new-xref))
7177            (save-excursion
7178              (set-buffer copy-buf)
7179              ;; First put the article in the destination group.
7180              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7181              (when (consp (setq art-group
7182                                 (gnus-request-accept-article
7183                                  to-newsgroup select-method (not articles))))
7184                (setq new-xref (concat new-xref " " (car art-group)
7185                                       ":" (cdr art-group)))
7186                ;; Now we have the new Xrefs header, so we insert
7187                ;; it and replace the new article.
7188                (nnheader-replace-header "Xref" new-xref)
7189                (gnus-request-replace-article
7190                 (cdr art-group) to-newsgroup (current-buffer))
7191                art-group))))))
7192       (cond
7193        ((not art-group)
7194         (gnus-message 1 "Couldn't %s article %s"
7195                       (cadr (assq action names)) article))
7196        ((and (eq art-group 'junk)
7197              (eq action 'move))
7198         (gnus-summary-mark-article article gnus-canceled-mark)
7199         (gnus-message 4 "Deleted article %s" article))
7200        (t
7201         (let* ((pto-group (gnus-group-prefixed-name
7202                            (car art-group) to-method))
7203                (entry
7204                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7205                (info (nth 2 entry))
7206                (to-group (gnus-info-group info)))
7207           ;; Update the group that has been moved to.
7208           (when (and info
7209                      (memq action '(move copy)))
7210             (unless (member to-group to-groups)
7211               (push to-group to-groups))
7212
7213             (unless (memq article gnus-newsgroup-unreads)
7214               (gnus-info-set-read
7215                info (gnus-add-to-range (gnus-info-read info)
7216                                        (list (cdr art-group)))))
7217
7218             ;; Copy any marks over to the new group.
7219             (let ((marks gnus-article-mark-lists)
7220                   (to-article (cdr art-group)))
7221
7222               ;; See whether the article is to be put in the cache.
7223               (when gnus-use-cache
7224                 (gnus-cache-possibly-enter-article
7225                  to-group to-article
7226                  (let ((header (copy-sequence
7227                                 (gnus-summary-article-header article))))
7228                    (mail-header-set-number header to-article)
7229                    header)
7230                  (memq article gnus-newsgroup-marked)
7231                  (memq article gnus-newsgroup-dormant)
7232                  (memq article gnus-newsgroup-unreads)))
7233
7234               (when (and (equal to-group gnus-newsgroup-name)
7235                          (not (memq article gnus-newsgroup-unreads)))
7236                 ;; Mark this article as read in this group.
7237                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7238                 (setcdr (gnus-active to-group) to-article)
7239                 (setcdr gnus-newsgroup-active to-article))
7240
7241               (while marks
7242                 (when (memq article (symbol-value
7243                                      (intern (format "gnus-newsgroup-%s"
7244                                                      (caar marks)))))
7245                   ;; If the other group is the same as this group,
7246                   ;; then we have to add the mark to the list.
7247                   (when (equal to-group gnus-newsgroup-name)
7248                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7249                          (cons to-article
7250                                (symbol-value
7251                                 (intern (format "gnus-newsgroup-%s"
7252                                                 (caar marks)))))))
7253                   ;; Copy the marks to other group.
7254                   (gnus-add-marked-articles
7255                    to-group (cdar marks) (list to-article) info))
7256                 (setq marks (cdr marks)))
7257
7258               (gnus-dribble-enter
7259                (concat "(gnus-group-set-info '"
7260                        (gnus-prin1-to-string (gnus-get-info to-group))
7261                        ")"))))
7262
7263           ;; Update the Xref header in this article to point to
7264           ;; the new crossposted article we have just created.
7265           (when (eq action 'crosspost)
7266             (save-excursion
7267               (set-buffer copy-buf)
7268               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7269               (nnheader-replace-header "Xref" new-xref)
7270               (gnus-request-replace-article
7271                article gnus-newsgroup-name (current-buffer)))))
7272
7273         ;;;!!!Why is this necessary?
7274         (set-buffer gnus-summary-buffer)
7275
7276         (gnus-summary-goto-subject article)
7277         (when (eq action 'move)
7278           (gnus-summary-mark-article article gnus-canceled-mark))))
7279       (gnus-summary-remove-process-mark article))
7280     ;; Re-activate all groups that have been moved to.
7281     (while to-groups
7282       (save-excursion
7283         (set-buffer gnus-group-buffer)
7284         (when (gnus-group-goto-group (car to-groups) t)
7285           (gnus-group-get-new-news-this-group 1 t))
7286         (pop to-groups)))
7287
7288     (gnus-kill-buffer copy-buf)
7289     (gnus-summary-position-point)
7290     (gnus-set-mode-line 'summary)))
7291
7292 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7293   "Move the current article to a different newsgroup.
7294 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7295 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7296 re-spool using this method."
7297   (interactive "P")
7298   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7299
7300 (defun gnus-summary-crosspost-article (&optional n)
7301   "Crosspost the current article to some other group."
7302   (interactive "P")
7303   (gnus-summary-move-article n nil nil 'crosspost))
7304
7305 (defcustom gnus-summary-respool-default-method nil
7306   "Default method for respooling an article.
7307 If nil, use to the current newsgroup method."
7308   :type '(choice (gnus-select-method :value (nnml ""))
7309                  (const nil))
7310   :group 'gnus-summary-mail)
7311
7312 (defun gnus-summary-respool-article (&optional n method)
7313   "Respool the current article.
7314 The article will be squeezed through the mail spooling process again,
7315 which means that it will be put in some mail newsgroup or other
7316 depending on `nnmail-split-methods'.
7317 If N is a positive number, respool the N next articles.
7318 If N is a negative number, respool the N previous articles.
7319 If N is nil and any articles have been marked with the process mark,
7320 respool those articles instead.
7321
7322 Respooling can be done both from mail groups and \"real\" newsgroups.
7323 In the former case, the articles in question will be moved from the
7324 current group into whatever groups they are destined to.  In the
7325 latter case, they will be copied into the relevant groups."
7326   (interactive
7327    (list current-prefix-arg
7328          (let* ((methods (gnus-methods-using 'respool))
7329                 (methname
7330                  (symbol-name (or gnus-summary-respool-default-method
7331                                   (car (gnus-find-method-for-group
7332                                         gnus-newsgroup-name)))))
7333                 (method
7334                  (gnus-completing-read
7335                   methname "What backend do you want to use when respooling?"
7336                   methods nil t nil 'gnus-mail-method-history))
7337                 ms)
7338            (cond
7339             ((zerop (length (setq ms (gnus-servers-using-backend
7340                                       (intern method)))))
7341              (list (intern method) ""))
7342             ((= 1 (length ms))
7343              (car ms))
7344             (t
7345              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7346                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7347                            ms-alist))))))))
7348   (unless method
7349     (error "No method given for respooling"))
7350   (if (assoc (symbol-name
7351               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7352              (gnus-methods-using 'respool))
7353       (gnus-summary-move-article n nil method)
7354     (gnus-summary-copy-article n nil method)))
7355
7356 (defun gnus-summary-import-article (file)
7357   "Import an arbitrary file into a mail newsgroup."
7358   (interactive "fImport file: ")
7359   (let ((group gnus-newsgroup-name)
7360         (now (current-time))
7361         atts lines)
7362     (unless (gnus-check-backend-function 'request-accept-article group)
7363       (error "%s does not support article importing" group))
7364     (or (file-readable-p file)
7365         (not (file-regular-p file))
7366         (error "Can't read %s" file))
7367     (save-excursion
7368       (set-buffer (gnus-get-buffer-create " *import file*"))
7369       (erase-buffer)
7370       (insert-file-contents file)
7371       (goto-char (point-min))
7372       (unless (nnheader-article-p)
7373         ;; This doesn't look like an article, so we fudge some headers.
7374         (setq atts (file-attributes file)
7375               lines (count-lines (point-min) (point-max)))
7376         (insert "From: " (read-string "From: ") "\n"
7377                 "Subject: " (read-string "Subject: ") "\n"
7378                 "Date: " (message-make-date (nth 5 atts))
7379                 "\n"
7380                 "Message-ID: " (message-make-message-id) "\n"
7381                 "Lines: " (int-to-string lines) "\n"
7382                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7383       (gnus-request-accept-article group nil t)
7384       (kill-buffer (current-buffer)))))
7385
7386 (defun gnus-summary-article-posted-p ()
7387   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7388 This will be the case if the article has both been mailed and posted."
7389   (interactive)
7390   (let ((id (mail-header-references (gnus-summary-article-header)))
7391         (gnus-override-method
7392          (or gnus-refer-article-method gnus-select-method)))
7393     (if (gnus-request-head id "")
7394         (gnus-message 2 "The current message was found on %s"
7395                       gnus-override-method)
7396       (gnus-message 2 "The current message couldn't be found on %s"
7397                     gnus-override-method)
7398       nil)))
7399
7400 (defun gnus-summary-expire-articles (&optional now)
7401   "Expire all articles that are marked as expirable in the current group."
7402   (interactive)
7403   (when (gnus-check-backend-function
7404          'request-expire-articles gnus-newsgroup-name)
7405     ;; This backend supports expiry.
7406     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7407            (expirable (if total
7408                           (progn
7409                             ;; We need to update the info for
7410                             ;; this group for `gnus-list-of-read-articles'
7411                             ;; to give us the right answer.
7412                             (gnus-run-hooks 'gnus-exit-group-hook)
7413                             (gnus-summary-update-info)
7414                             (gnus-list-of-read-articles gnus-newsgroup-name))
7415                         (setq gnus-newsgroup-expirable
7416                               (sort gnus-newsgroup-expirable '<))))
7417            (expiry-wait (if now 'immediate
7418                           (gnus-group-find-parameter
7419                            gnus-newsgroup-name 'expiry-wait)))
7420            es)
7421       (when expirable
7422         ;; There are expirable articles in this group, so we run them
7423         ;; through the expiry process.
7424         (gnus-message 6 "Expiring articles...")
7425         ;; The list of articles that weren't expired is returned.
7426         (save-excursion
7427           (if expiry-wait
7428               (let ((nnmail-expiry-wait-function nil)
7429                     (nnmail-expiry-wait expiry-wait))
7430                 (setq es (gnus-request-expire-articles
7431                           expirable gnus-newsgroup-name)))
7432             (setq es (gnus-request-expire-articles
7433                       expirable gnus-newsgroup-name))))
7434         (unless total
7435           (setq gnus-newsgroup-expirable es))
7436         ;; We go through the old list of expirable, and mark all
7437         ;; really expired articles as nonexistent.
7438         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7439           (let ((gnus-use-cache nil))
7440             (while expirable
7441               (unless (memq (car expirable) es)
7442                 (when (gnus-data-find (car expirable))
7443                   (gnus-summary-mark-article
7444                    (car expirable) gnus-canceled-mark)))
7445               (setq expirable (cdr expirable)))))
7446         (gnus-message 6 "Expiring articles...done")))))
7447
7448 (defun gnus-summary-expire-articles-now ()
7449   "Expunge all expirable articles in the current group.
7450 This means that *all* articles that are marked as expirable will be
7451 deleted forever, right now."
7452   (interactive)
7453   (or gnus-expert-user
7454       (gnus-yes-or-no-p
7455        "Are you really, really, really sure you want to delete all these messages? ")
7456       (error "Phew!"))
7457   (gnus-summary-expire-articles t))
7458
7459 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7460 (defun gnus-summary-delete-article (&optional n)
7461   "Delete the N next (mail) articles.
7462 This command actually deletes articles.  This is not a marking
7463 command.  The article will disappear forever from your life, never to
7464 return.
7465 If N is negative, delete backwards.
7466 If N is nil and articles have been marked with the process mark,
7467 delete these instead."
7468   (interactive "P")
7469   (unless (gnus-check-backend-function 'request-expire-articles
7470                                        gnus-newsgroup-name)
7471     (error "The current newsgroup does not support article deletion"))
7472   ;; Compute the list of articles to delete.
7473   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7474         not-deleted)
7475     (if (and gnus-novice-user
7476              (not (gnus-yes-or-no-p
7477                    (format "Do you really want to delete %s forever? "
7478                            (if (> (length articles) 1)
7479                                (format "these %s articles" (length articles))
7480                              "this article")))))
7481         ()
7482       ;; Delete the articles.
7483       (setq not-deleted (gnus-request-expire-articles
7484                          articles gnus-newsgroup-name 'force))
7485       (while articles
7486         (gnus-summary-remove-process-mark (car articles))
7487         ;; The backend might not have been able to delete the article
7488         ;; after all.
7489         (unless (memq (car articles) not-deleted)
7490           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7491         (setq articles (cdr articles)))
7492       (when not-deleted
7493         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7494     (gnus-summary-position-point)
7495     (gnus-set-mode-line 'summary)
7496     not-deleted))
7497
7498 (defun gnus-summary-edit-article (&optional force)
7499   "Edit the current article.
7500 This will have permanent effect only in mail groups.
7501 If FORCE is non-nil, allow editing of articles even in read-only
7502 groups."
7503   (interactive "P")
7504   (let ((mail-parse-charset gnus-newsgroup-charset))
7505     (save-excursion
7506       (set-buffer gnus-summary-buffer)
7507       (gnus-set-global-variables)
7508       (when (and (not force)
7509                  (gnus-group-read-only-p))
7510         (error "The current newsgroup does not support article editing"))
7511       (gnus-summary-show-article t)
7512       (gnus-article-edit-article
7513        'mime-to-mml
7514        `(lambda (no-highlight)
7515           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7516             (mml-to-mime)
7517             (gnus-summary-edit-article-done
7518              ,(or (mail-header-references gnus-current-headers) "")
7519              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7520
7521 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7522
7523 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7524                                                  no-highlight)
7525   "Make edits to the current article permanent."
7526   (interactive)
7527   ;; Replace the article.
7528   (let ((buf (current-buffer)))
7529     (with-temp-buffer
7530       (insert-buffer-substring buf)
7531       (if (and (not read-only)
7532                (not (gnus-request-replace-article
7533                      (cdr gnus-article-current) (car gnus-article-current)
7534                      (current-buffer) t)))
7535           (error "Couldn't replace article")
7536         ;; Update the summary buffer.
7537         (if (and references
7538                  (equal (message-tokenize-header references " ")
7539                         (message-tokenize-header
7540                          (or (message-fetch-field "references") "") " ")))
7541             ;; We only have to update this line.
7542             (save-excursion
7543               (save-restriction
7544                 (message-narrow-to-head)
7545                 (let ((head (buffer-string))
7546                       header)
7547                   (with-temp-buffer
7548                     (insert (format "211 %d Article retrieved.\n"
7549                                     (cdr gnus-article-current)))
7550                     (insert head)
7551                     (insert ".\n")
7552                     (let ((nntp-server-buffer (current-buffer)))
7553                       (setq header (car (gnus-get-newsgroup-headers
7554                                          (save-excursion
7555                                            (set-buffer gnus-summary-buffer)
7556                                            gnus-newsgroup-dependencies)
7557                                          t))))
7558                     (save-excursion
7559                       (set-buffer gnus-summary-buffer)
7560                       (gnus-data-set-header
7561                        (gnus-data-find (cdr gnus-article-current))
7562                        header)
7563                       (gnus-summary-update-article-line
7564                        (cdr gnus-article-current) header))))))
7565           ;; Update threads.
7566           (set-buffer (or buffer gnus-summary-buffer))
7567           (gnus-summary-update-article (cdr gnus-article-current)))
7568         ;; Prettify the article buffer again.
7569         (unless no-highlight
7570           (save-excursion
7571             (set-buffer gnus-article-buffer)
7572             ;;;!!! Fix this -- article should be rehighlighted.
7573             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7574             (set-buffer gnus-original-article-buffer)
7575             (gnus-request-article
7576              (cdr gnus-article-current)
7577              (car gnus-article-current) (current-buffer))))
7578         ;; Prettify the summary buffer line.
7579         (when (gnus-visual-p 'summary-highlight 'highlight)
7580           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7581
7582 (defun gnus-summary-edit-wash (key)
7583   "Perform editing command KEY in the article buffer."
7584   (interactive
7585    (list
7586     (progn
7587       (message "%s" (concat (this-command-keys) "- "))
7588       (read-char))))
7589   (message "")
7590   (gnus-summary-edit-article)
7591   (execute-kbd-macro (concat (this-command-keys) key))
7592   (gnus-article-edit-done))
7593
7594 ;;; Respooling
7595
7596 (defun gnus-summary-respool-query (&optional silent trace)
7597   "Query where the respool algorithm would put this article."
7598   (interactive)
7599   (let (gnus-mark-article-hook)
7600     (gnus-summary-select-article)
7601     (save-excursion
7602       (set-buffer gnus-original-article-buffer)
7603       (save-restriction
7604         (message-narrow-to-head)
7605         (let ((groups (nnmail-article-group 'identity trace)))
7606           (unless silent
7607             (if groups
7608                 (message "This message would go to %s"
7609                          (mapconcat 'car groups ", "))
7610               (message "This message would go to no groups"))
7611             groups))))))
7612
7613 (defun gnus-summary-respool-trace ()
7614   "Trace where the respool algorithm would put this article.
7615 Display a buffer showing all fancy splitting patterns which matched."
7616   (interactive)
7617   (gnus-summary-respool-query nil t))
7618
7619 ;; Summary marking commands.
7620
7621 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7622   "Mark articles which has the same subject as read, and then select the next.
7623 If UNMARK is positive, remove any kind of mark.
7624 If UNMARK is negative, tick articles."
7625   (interactive "P")
7626   (when unmark
7627     (setq unmark (prefix-numeric-value unmark)))
7628   (let ((count
7629          (gnus-summary-mark-same-subject
7630           (gnus-summary-article-subject) unmark)))
7631     ;; Select next unread article.  If auto-select-same mode, should
7632     ;; select the first unread article.
7633     (gnus-summary-next-article t (and gnus-auto-select-same
7634                                       (gnus-summary-article-subject)))
7635     (gnus-message 7 "%d article%s marked as %s"
7636                   count (if (= count 1) " is" "s are")
7637                   (if unmark "unread" "read"))))
7638
7639 (defun gnus-summary-kill-same-subject (&optional unmark)
7640   "Mark articles which has the same subject as read.
7641 If UNMARK is positive, remove any kind of mark.
7642 If UNMARK is negative, tick articles."
7643   (interactive "P")
7644   (when unmark
7645     (setq unmark (prefix-numeric-value unmark)))
7646   (let ((count
7647          (gnus-summary-mark-same-subject
7648           (gnus-summary-article-subject) unmark)))
7649     ;; If marked as read, go to next unread subject.
7650     (when (null unmark)
7651       ;; Go to next unread subject.
7652       (gnus-summary-next-subject 1 t))
7653     (gnus-message 7 "%d articles are marked as %s"
7654                   count (if unmark "unread" "read"))))
7655
7656 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7657   "Mark articles with same SUBJECT as read, and return marked number.
7658 If optional argument UNMARK is positive, remove any kinds of marks.
7659 If optional argument UNMARK is negative, mark articles as unread instead."
7660   (let ((count 1))
7661     (save-excursion
7662       (cond
7663        ((null unmark)                   ; Mark as read.
7664         (while (and
7665                 (progn
7666                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7667                   (gnus-summary-show-thread) t)
7668                 (gnus-summary-find-subject subject))
7669           (setq count (1+ count))))
7670        ((> unmark 0)                    ; Tick.
7671         (while (and
7672                 (progn
7673                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7674                   (gnus-summary-show-thread) t)
7675                 (gnus-summary-find-subject subject))
7676           (setq count (1+ count))))
7677        (t                               ; Mark as unread.
7678         (while (and
7679                 (progn
7680                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7681                   (gnus-summary-show-thread) t)
7682                 (gnus-summary-find-subject subject))
7683           (setq count (1+ count)))))
7684       (gnus-set-mode-line 'summary)
7685       ;; Return the number of marked articles.
7686       count)))
7687
7688 (defun gnus-summary-mark-as-processable (n &optional unmark)
7689   "Set the process mark on the next N articles.
7690 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7691 the process mark instead.  The difference between N and the actual
7692 number of articles marked is returned."
7693   (interactive "p")
7694   (let ((backward (< n 0))
7695         (n (abs n)))
7696     (while (and
7697             (> n 0)
7698             (if unmark
7699                 (gnus-summary-remove-process-mark
7700                  (gnus-summary-article-number))
7701               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7702             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7703       (setq n (1- n)))
7704     (when (/= 0 n)
7705       (gnus-message 7 "No more articles"))
7706     (gnus-summary-recenter)
7707     (gnus-summary-position-point)
7708     n))
7709
7710 (defun gnus-summary-unmark-as-processable (n)
7711   "Remove the process mark from the next N articles.
7712 If N is negative, unmark backward instead.  The difference between N and
7713 the actual number of articles unmarked is returned."
7714   (interactive "p")
7715   (gnus-summary-mark-as-processable n t))
7716
7717 (defun gnus-summary-unmark-all-processable ()
7718   "Remove the process mark from all articles."
7719   (interactive)
7720   (save-excursion
7721     (while gnus-newsgroup-processable
7722       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7723   (gnus-summary-position-point))
7724
7725 (defun gnus-summary-mark-as-expirable (n)
7726   "Mark N articles forward as expirable.
7727 If N is negative, mark backward instead.  The difference between N and
7728 the actual number of articles marked is returned."
7729   (interactive "p")
7730   (gnus-summary-mark-forward n gnus-expirable-mark))
7731
7732 (defun gnus-summary-mark-article-as-replied (article)
7733   "Mark ARTICLE replied and update the summary line."
7734   (push article gnus-newsgroup-replied)
7735   (let ((buffer-read-only nil))
7736     (when (gnus-summary-goto-subject article nil t)
7737       (gnus-summary-update-secondary-mark article))))
7738
7739 (defun gnus-summary-set-bookmark (article)
7740   "Set a bookmark in current article."
7741   (interactive (list (gnus-summary-article-number)))
7742   (when (or (not (get-buffer gnus-article-buffer))
7743             (not gnus-current-article)
7744             (not gnus-article-current)
7745             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7746     (error "No current article selected"))
7747   ;; Remove old bookmark, if one exists.
7748   (let ((old (assq article gnus-newsgroup-bookmarks)))
7749     (when old
7750       (setq gnus-newsgroup-bookmarks
7751             (delq old gnus-newsgroup-bookmarks))))
7752   ;; Set the new bookmark, which is on the form
7753   ;; (article-number . line-number-in-body).
7754   (push
7755    (cons article
7756          (save-excursion
7757            (set-buffer gnus-article-buffer)
7758            (count-lines
7759             (min (point)
7760                  (save-excursion
7761                    (goto-char (point-min))
7762                    (search-forward "\n\n" nil t)
7763                    (point)))
7764             (point))))
7765    gnus-newsgroup-bookmarks)
7766   (gnus-message 6 "A bookmark has been added to the current article."))
7767
7768 (defun gnus-summary-remove-bookmark (article)
7769   "Remove the bookmark from the current article."
7770   (interactive (list (gnus-summary-article-number)))
7771   ;; Remove old bookmark, if one exists.
7772   (let ((old (assq article gnus-newsgroup-bookmarks)))
7773     (if old
7774         (progn
7775           (setq gnus-newsgroup-bookmarks
7776                 (delq old gnus-newsgroup-bookmarks))
7777           (gnus-message 6 "Removed bookmark."))
7778       (gnus-message 6 "No bookmark in current article."))))
7779
7780 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7781 (defun gnus-summary-mark-as-dormant (n)
7782   "Mark N articles forward as dormant.
7783 If N is negative, mark backward instead.  The difference between N and
7784 the actual number of articles marked is returned."
7785   (interactive "p")
7786   (gnus-summary-mark-forward n gnus-dormant-mark))
7787
7788 (defun gnus-summary-set-process-mark (article)
7789   "Set the process mark on ARTICLE and update the summary line."
7790   (setq gnus-newsgroup-processable
7791         (cons article
7792               (delq article gnus-newsgroup-processable)))
7793   (when (gnus-summary-goto-subject article)
7794     (gnus-summary-show-thread)
7795     (gnus-summary-goto-subject article)
7796     (gnus-summary-update-secondary-mark article)))
7797
7798 (defun gnus-summary-remove-process-mark (article)
7799   "Remove the process mark from ARTICLE and update the summary line."
7800   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7801   (when (gnus-summary-goto-subject article)
7802     (gnus-summary-show-thread)
7803     (gnus-summary-goto-subject article)
7804     (gnus-summary-update-secondary-mark article)))
7805
7806 (defun gnus-summary-set-saved-mark (article)
7807   "Set the process mark on ARTICLE and update the summary line."
7808   (push article gnus-newsgroup-saved)
7809   (when (gnus-summary-goto-subject article)
7810     (gnus-summary-update-secondary-mark article)))
7811
7812 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7813   "Mark N articles as read forwards.
7814 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7815 The difference between N and the actual number of articles marked is
7816 returned."
7817   (interactive "p")
7818   (gnus-summary-show-thread)
7819   (let ((backward (< n 0))
7820         (gnus-summary-goto-unread
7821          (and gnus-summary-goto-unread
7822               (not (eq gnus-summary-goto-unread 'never))
7823               (not (memq mark (list gnus-unread-mark
7824                                     gnus-ticked-mark gnus-dormant-mark)))))
7825         (n (abs n))
7826         (mark (or mark gnus-del-mark)))
7827     (while (and (> n 0)
7828                 (gnus-summary-mark-article nil mark no-expire)
7829                 (zerop (gnus-summary-next-subject
7830                         (if backward -1 1)
7831                         (and gnus-summary-goto-unread
7832                              (not (eq gnus-summary-goto-unread 'never)))
7833                         t)))
7834       (setq n (1- n)))
7835     (when (/= 0 n)
7836       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7837     (gnus-summary-recenter)
7838     (gnus-summary-position-point)
7839     (gnus-set-mode-line 'summary)
7840     n))
7841
7842 (defun gnus-summary-mark-article-as-read (mark)
7843   "Mark the current article quickly as read with MARK."
7844   (let ((article (gnus-summary-article-number)))
7845     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7846     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7847     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7848     (push (cons article mark) gnus-newsgroup-reads)
7849     ;; Possibly remove from cache, if that is used.
7850     (when gnus-use-cache
7851       (gnus-cache-enter-remove-article article))
7852     ;; Allow the backend to change the mark.
7853     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7854     ;; Check for auto-expiry.
7855     (when (and gnus-newsgroup-auto-expire
7856                (memq mark gnus-auto-expirable-marks))
7857       (setq mark gnus-expirable-mark)
7858       ;; Let the backend know about the mark change.
7859       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7860       (push article gnus-newsgroup-expirable))
7861     ;; Set the mark in the buffer.
7862     (gnus-summary-update-mark mark 'unread)
7863     t))
7864
7865 (defun gnus-summary-mark-article-as-unread (mark)
7866   "Mark the current article quickly as unread with MARK."
7867   (let* ((article (gnus-summary-article-number))
7868          (old-mark (gnus-summary-article-mark article)))
7869     ;; Allow the backend to change the mark.
7870     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7871     (if (eq mark old-mark)
7872         t
7873       (if (<= article 0)
7874           (progn
7875             (gnus-error 1 "Can't mark negative article numbers")
7876             nil)
7877         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7878         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7879         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7880         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7881         (cond ((= mark gnus-ticked-mark)
7882                (push article gnus-newsgroup-marked))
7883               ((= mark gnus-dormant-mark)
7884                (push article gnus-newsgroup-dormant))
7885               (t
7886                (push article gnus-newsgroup-unreads)))
7887         (gnus-pull article gnus-newsgroup-reads)
7888
7889         ;; See whether the article is to be put in the cache.
7890         (and gnus-use-cache
7891              (vectorp (gnus-summary-article-header article))
7892              (save-excursion
7893                (gnus-cache-possibly-enter-article
7894                 gnus-newsgroup-name article
7895                 (gnus-summary-article-header article)
7896                 (= mark gnus-ticked-mark)
7897                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7898
7899         ;; Fix the mark.
7900         (gnus-summary-update-mark mark 'unread)
7901         t))))
7902
7903 (defun gnus-summary-mark-article (&optional article mark no-expire)
7904   "Mark ARTICLE with MARK.  MARK can be any character.
7905 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7906 `??' (dormant) and `?E' (expirable).
7907 If MARK is nil, then the default character `?r' is used.
7908 If ARTICLE is nil, then the article on the current line will be
7909 marked."
7910   ;; The mark might be a string.
7911   (when (stringp mark)
7912     (setq mark (aref mark 0)))
7913   ;; If no mark is given, then we check auto-expiring.
7914   (when (null mark)
7915     (setq mark gnus-del-mark))
7916   (when (and (not no-expire)
7917              gnus-newsgroup-auto-expire
7918              (memq mark gnus-auto-expirable-marks))
7919     (setq mark gnus-expirable-mark))
7920   (let ((article (or article (gnus-summary-article-number)))
7921         (old-mark (gnus-summary-article-mark article)))
7922     ;; Allow the backend to change the mark.
7923     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7924     (if (eq mark old-mark)
7925         t
7926       (unless article
7927         (error "No article on current line"))
7928       (if (not (if (or (= mark gnus-unread-mark)
7929                        (= mark gnus-ticked-mark)
7930                        (= mark gnus-dormant-mark))
7931                    (gnus-mark-article-as-unread article mark)
7932                  (gnus-mark-article-as-read article mark)))
7933           t
7934         ;; See whether the article is to be put in the cache.
7935         (and gnus-use-cache
7936              (not (= mark gnus-canceled-mark))
7937              (vectorp (gnus-summary-article-header article))
7938              (save-excursion
7939                (gnus-cache-possibly-enter-article
7940                 gnus-newsgroup-name article
7941                 (gnus-summary-article-header article)
7942                 (= mark gnus-ticked-mark)
7943                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7944
7945         (when (gnus-summary-goto-subject article nil t)
7946           (let ((buffer-read-only nil))
7947             (gnus-summary-show-thread)
7948             ;; Fix the mark.
7949             (gnus-summary-update-mark mark 'unread)
7950             t))))))
7951
7952 (defun gnus-summary-update-secondary-mark (article)
7953   "Update the secondary (read, process, cache) mark."
7954   (gnus-summary-update-mark
7955    (cond ((memq article gnus-newsgroup-processable)
7956           gnus-process-mark)
7957          ((memq article gnus-newsgroup-cached)
7958           gnus-cached-mark)
7959          ((memq article gnus-newsgroup-replied)
7960           gnus-replied-mark)
7961          ((memq article gnus-newsgroup-saved)
7962           gnus-saved-mark)
7963          (t gnus-unread-mark))
7964    'replied)
7965   (when (gnus-visual-p 'summary-highlight 'highlight)
7966     (gnus-run-hooks 'gnus-summary-update-hook))
7967   t)
7968
7969 (defun gnus-summary-update-mark (mark type)
7970   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7971         (buffer-read-only nil))
7972     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7973     (when forward
7974       (when (looking-at "\r")
7975         (incf forward))
7976       (when (<= (+ forward (point)) (point-max))
7977         ;; Go to the right position on the line.
7978         (goto-char (+ forward (point)))
7979         ;; Replace the old mark with the new mark.
7980         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7981         ;; Optionally update the marks by some user rule.
7982         (when (eq type 'unread)
7983           (gnus-data-set-mark
7984            (gnus-data-find (gnus-summary-article-number)) mark)
7985           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7986
7987 (defun gnus-mark-article-as-read (article &optional mark)
7988   "Enter ARTICLE in the pertinent lists and remove it from others."
7989   ;; Make the article expirable.
7990   (let ((mark (or mark gnus-del-mark)))
7991     (if (= mark gnus-expirable-mark)
7992         (push article gnus-newsgroup-expirable)
7993       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7994     ;; Remove from unread and marked lists.
7995     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7996     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7997     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7998     (push (cons article mark) gnus-newsgroup-reads)
7999     ;; Possibly remove from cache, if that is used.
8000     (when gnus-use-cache
8001       (gnus-cache-enter-remove-article article))
8002     t))
8003
8004 (defun gnus-mark-article-as-unread (article &optional mark)
8005   "Enter ARTICLE in the pertinent lists and remove it from others."
8006   (let ((mark (or mark gnus-ticked-mark)))
8007     (if (<= article 0)
8008         (progn
8009           (gnus-error 1 "Can't mark negative article numbers")
8010           nil)
8011       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8012             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8013             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8014             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8015
8016       ;; Unsuppress duplicates?
8017       (when gnus-suppress-duplicates
8018         (gnus-dup-unsuppress-article article))
8019
8020       (cond ((= mark gnus-ticked-mark)
8021              (push article gnus-newsgroup-marked))
8022             ((= mark gnus-dormant-mark)
8023              (push article gnus-newsgroup-dormant))
8024             (t
8025              (push article gnus-newsgroup-unreads)))
8026       (gnus-pull article gnus-newsgroup-reads)
8027       t)))
8028
8029 (defalias 'gnus-summary-mark-as-unread-forward
8030   'gnus-summary-tick-article-forward)
8031 (make-obsolete 'gnus-summary-mark-as-unread-forward
8032                'gnus-summary-tick-article-forward)
8033 (defun gnus-summary-tick-article-forward (n)
8034   "Tick N articles forwards.
8035 If N is negative, tick backwards instead.
8036 The difference between N and the number of articles ticked is returned."
8037   (interactive "p")
8038   (gnus-summary-mark-forward n gnus-ticked-mark))
8039
8040 (defalias 'gnus-summary-mark-as-unread-backward
8041   'gnus-summary-tick-article-backward)
8042 (make-obsolete 'gnus-summary-mark-as-unread-backward
8043                'gnus-summary-tick-article-backward)
8044 (defun gnus-summary-tick-article-backward (n)
8045   "Tick N articles backwards.
8046 The difference between N and the number of articles ticked is returned."
8047   (interactive "p")
8048   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8049
8050 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8051 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8052 (defun gnus-summary-tick-article (&optional article clear-mark)
8053   "Mark current article as unread.
8054 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8055 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8056   (interactive)
8057   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8058                                        gnus-ticked-mark)))
8059
8060 (defun gnus-summary-mark-as-read-forward (n)
8061   "Mark N articles as read forwards.
8062 If N is negative, mark backwards instead.
8063 The difference between N and the actual number of articles marked is
8064 returned."
8065   (interactive "p")
8066   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8067
8068 (defun gnus-summary-mark-as-read-backward (n)
8069   "Mark the N articles as read backwards.
8070 The difference between N and the actual number of articles marked is
8071 returned."
8072   (interactive "p")
8073   (gnus-summary-mark-forward
8074    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8075
8076 (defun gnus-summary-mark-as-read (&optional article mark)
8077   "Mark current article as read.
8078 ARTICLE specifies the article to be marked as read.
8079 MARK specifies a string to be inserted at the beginning of the line."
8080   (gnus-summary-mark-article article mark))
8081
8082 (defun gnus-summary-clear-mark-forward (n)
8083   "Clear marks from N articles forward.
8084 If N is negative, clear backward instead.
8085 The difference between N and the number of marks cleared is returned."
8086   (interactive "p")
8087   (gnus-summary-mark-forward n gnus-unread-mark))
8088
8089 (defun gnus-summary-clear-mark-backward (n)
8090   "Clear marks from N articles backward.
8091 The difference between N and the number of marks cleared is returned."
8092   (interactive "p")
8093   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8094
8095 (defun gnus-summary-mark-unread-as-read ()
8096   "Intended to be used by `gnus-summary-mark-article-hook'."
8097   (when (memq gnus-current-article gnus-newsgroup-unreads)
8098     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8099
8100 (defun gnus-summary-mark-read-and-unread-as-read ()
8101   "Intended to be used by `gnus-summary-mark-article-hook'."
8102   (let ((mark (gnus-summary-article-mark)))
8103     (when (or (gnus-unread-mark-p mark)
8104               (gnus-read-mark-p mark))
8105       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8106
8107 (defun gnus-summary-mark-region-as-read (point mark all)
8108   "Mark all unread articles between point and mark as read.
8109 If given a prefix, mark all articles between point and mark as read,
8110 even ticked and dormant ones."
8111   (interactive "r\nP")
8112   (save-excursion
8113     (let (article)
8114       (goto-char point)
8115       (beginning-of-line)
8116       (while (and
8117               (< (point) mark)
8118               (progn
8119                 (when (or all
8120                           (memq (setq article (gnus-summary-article-number))
8121                                 gnus-newsgroup-unreads))
8122                   (gnus-summary-mark-article article gnus-del-mark))
8123                 t)
8124               (gnus-summary-find-next))))))
8125
8126 (defun gnus-summary-mark-below (score mark)
8127   "Mark articles with score less than SCORE with MARK."
8128   (interactive "P\ncMark: ")
8129   (setq score (if score
8130                   (prefix-numeric-value score)
8131                 (or gnus-summary-default-score 0)))
8132   (save-excursion
8133     (set-buffer gnus-summary-buffer)
8134     (goto-char (point-min))
8135     (while
8136         (progn
8137           (and (< (gnus-summary-article-score) score)
8138                (gnus-summary-mark-article nil mark))
8139           (gnus-summary-find-next)))))
8140
8141 (defun gnus-summary-kill-below (&optional score)
8142   "Mark articles with score below SCORE as read."
8143   (interactive "P")
8144   (gnus-summary-mark-below score gnus-killed-mark))
8145
8146 (defun gnus-summary-clear-above (&optional score)
8147   "Clear all marks from articles with score above SCORE."
8148   (interactive "P")
8149   (gnus-summary-mark-above score gnus-unread-mark))
8150
8151 (defun gnus-summary-tick-above (&optional score)
8152   "Tick all articles with score above SCORE."
8153   (interactive "P")
8154   (gnus-summary-mark-above score gnus-ticked-mark))
8155
8156 (defun gnus-summary-mark-above (score mark)
8157   "Mark articles with score over SCORE with MARK."
8158   (interactive "P\ncMark: ")
8159   (setq score (if score
8160                   (prefix-numeric-value score)
8161                 (or gnus-summary-default-score 0)))
8162   (save-excursion
8163     (set-buffer gnus-summary-buffer)
8164     (goto-char (point-min))
8165     (while (and (progn
8166                   (when (> (gnus-summary-article-score) score)
8167                     (gnus-summary-mark-article nil mark))
8168                   t)
8169                 (gnus-summary-find-next)))))
8170
8171 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8172 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8173 (defun gnus-summary-limit-include-expunged (&optional no-error)
8174   "Display all the hidden articles that were expunged for low scores."
8175   (interactive)
8176   (let ((buffer-read-only nil))
8177     (let ((scored gnus-newsgroup-scored)
8178           headers h)
8179       (while scored
8180         (unless (gnus-summary-goto-subject (caar scored))
8181           (and (setq h (gnus-summary-article-header (caar scored)))
8182                (< (cdar scored) gnus-summary-expunge-below)
8183                (push h headers)))
8184         (setq scored (cdr scored)))
8185       (if (not headers)
8186           (when (not no-error)
8187             (error "No expunged articles hidden"))
8188         (goto-char (point-min))
8189         (gnus-summary-prepare-unthreaded (nreverse headers))
8190         (goto-char (point-min))
8191         (gnus-summary-position-point)
8192         t))))
8193
8194 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8195   "Mark all unread articles in this newsgroup as read.
8196 If prefix argument ALL is non-nil, ticked and dormant articles will
8197 also be marked as read.
8198 If QUIETLY is non-nil, no questions will be asked.
8199 If TO-HERE is non-nil, it should be a point in the buffer.  All
8200 articles before this point will be marked as read.
8201 Note that this function will only catch up the unread article
8202 in the current summary buffer limitation.
8203 The number of articles marked as read is returned."
8204   (interactive "P")
8205   (prog1
8206       (save-excursion
8207         (when (or quietly
8208                   (not gnus-interactive-catchup) ;Without confirmation?
8209                   gnus-expert-user
8210                   (gnus-y-or-n-p
8211                    (if all
8212                        "Mark absolutely all articles as read? "
8213                      "Mark all unread articles as read? ")))
8214           (if (and not-mark
8215                    (not gnus-newsgroup-adaptive)
8216                    (not gnus-newsgroup-auto-expire)
8217                    (not gnus-suppress-duplicates)
8218                    (or (not gnus-use-cache)
8219                        (eq gnus-use-cache 'passive)))
8220               (progn
8221                 (when all
8222                   (setq gnus-newsgroup-marked nil
8223                         gnus-newsgroup-dormant nil))
8224                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8225             ;; We actually mark all articles as canceled, which we
8226             ;; have to do when using auto-expiry or adaptive scoring.
8227             (gnus-summary-show-all-threads)
8228             (when (gnus-summary-first-subject (not all) t)
8229               (while (and
8230                       (if to-here (< (point) to-here) t)
8231                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8232                       (gnus-summary-find-next (not all) nil nil t))))
8233             (gnus-set-mode-line 'summary))
8234           t))
8235     (gnus-summary-position-point)))
8236
8237 (defun gnus-summary-catchup-to-here (&optional all)
8238   "Mark all unticked articles before the current one as read.
8239 If ALL is non-nil, also mark ticked and dormant articles as read."
8240   (interactive "P")
8241   (save-excursion
8242     (gnus-save-hidden-threads
8243       (let ((beg (point)))
8244         ;; We check that there are unread articles.
8245         (when (or all (gnus-summary-find-prev))
8246           (gnus-summary-catchup all t beg)))))
8247   (gnus-summary-position-point))
8248
8249 (defun gnus-summary-catchup-all (&optional quietly)
8250   "Mark all articles in this newsgroup as read."
8251   (interactive "P")
8252   (gnus-summary-catchup t quietly))
8253
8254 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8255   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8256 If prefix argument ALL is non-nil, all articles are marked as read."
8257   (interactive "P")
8258   (when (gnus-summary-catchup all quietly nil 'fast)
8259     ;; Select next newsgroup or exit.
8260     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8261              (eq gnus-auto-select-next 'quietly))
8262         (gnus-summary-next-group nil)
8263       (gnus-summary-exit))))
8264
8265 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8266   "Mark all articles in this newsgroup as read, and then exit."
8267   (interactive "P")
8268   (gnus-summary-catchup-and-exit t quietly))
8269
8270 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8271 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8272   "Mark all articles in this group as read and select the next group.
8273 If given a prefix, mark all articles, unread as well as ticked, as
8274 read."
8275   (interactive "P")
8276   (save-excursion
8277     (gnus-summary-catchup all))
8278   (gnus-summary-next-article t nil nil t))
8279
8280 ;; Thread-based commands.
8281
8282 (defun gnus-summary-articles-in-thread (&optional article)
8283   "Return a list of all articles in the current thread.
8284 If ARTICLE is non-nil, return all articles in the thread that starts
8285 with that article."
8286   (let* ((article (or article (gnus-summary-article-number)))
8287          (data (gnus-data-find-list article))
8288          (top-level (gnus-data-level (car data)))
8289          (top-subject
8290           (cond ((null gnus-thread-operation-ignore-subject)
8291                  (gnus-simplify-subject-re
8292                   (mail-header-subject (gnus-data-header (car data)))))
8293                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8294                  (gnus-simplify-subject-fuzzy
8295                   (mail-header-subject (gnus-data-header (car data)))))
8296                 (t nil)))
8297          (end-point (save-excursion
8298                       (if (gnus-summary-go-to-next-thread)
8299                           (point) (point-max))))
8300          articles)
8301     (while (and data
8302                 (< (gnus-data-pos (car data)) end-point))
8303       (when (or (not top-subject)
8304                 (string= top-subject
8305                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8306                              (gnus-simplify-subject-fuzzy
8307                               (mail-header-subject
8308                                (gnus-data-header (car data))))
8309                            (gnus-simplify-subject-re
8310                             (mail-header-subject
8311                              (gnus-data-header (car data)))))))
8312         (push (gnus-data-number (car data)) articles))
8313       (unless (and (setq data (cdr data))
8314                    (> (gnus-data-level (car data)) top-level))
8315         (setq data nil)))
8316     ;; Return the list of articles.
8317     (nreverse articles)))
8318
8319 (defun gnus-summary-rethread-current ()
8320   "Rethread the thread the current article is part of."
8321   (interactive)
8322   (let* ((gnus-show-threads t)
8323          (article (gnus-summary-article-number))
8324          (id (mail-header-id (gnus-summary-article-header)))
8325          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8326     (unless id
8327       (error "No article on the current line"))
8328     (gnus-rebuild-thread id)
8329     (gnus-summary-goto-subject article)))
8330
8331 (defun gnus-summary-reparent-thread ()
8332   "Make the current article child of the marked (or previous) article.
8333
8334 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8335 is non-nil or the Subject: of both articles are the same."
8336   (interactive)
8337   (unless (not (gnus-group-read-only-p))
8338     (error "The current newsgroup does not support article editing"))
8339   (unless (<= (length gnus-newsgroup-processable) 1)
8340     (error "No more than one article may be marked"))
8341   (save-window-excursion
8342     (let ((gnus-article-buffer " *reparent*")
8343           (current-article (gnus-summary-article-number))
8344           ;; First grab the marked article, otherwise one line up.
8345           (parent-article (if (not (null gnus-newsgroup-processable))
8346                               (car gnus-newsgroup-processable)
8347                             (save-excursion
8348                               (if (eq (forward-line -1) 0)
8349                                   (gnus-summary-article-number)
8350                                 (error "Beginning of summary buffer"))))))
8351       (unless (not (eq current-article parent-article))
8352         (error "An article may not be self-referential"))
8353       (let ((message-id (mail-header-id
8354                          (gnus-summary-article-header parent-article))))
8355         (unless (and message-id (not (equal message-id "")))
8356           (error "No message-id in desired parent"))
8357         (gnus-with-article current-article
8358           (goto-char (point-min))
8359           (if (re-search-forward "^References: " nil t)
8360               (progn
8361                 (re-search-forward "^[^ \t]" nil t)
8362                 (forward-line -1)
8363                 (end-of-line)
8364                 (insert " " message-id))
8365             (insert "References: " message-id "\n")))
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 the number of lines.
8600 Argument REVERSE means reverse order."
8601   (interactive "P")
8602   (gnus-summary-sort 'lines reverse))
8603
8604 (defun gnus-summary-sort-by-chars (&optional reverse)
8605   "Sort the summary buffer by article length.
8606 Argument REVERSE means reverse order."
8607   (interactive "P")
8608   (gnus-summary-sort 'chars reverse))   
8609
8610 (defun gnus-summary-sort (predicate reverse)
8611   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8612   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8613          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8614          (gnus-thread-sort-functions
8615           (if (not reverse)
8616               thread
8617             `(lambda (t1 t2)
8618                (,thread t2 t1))))
8619          (gnus-article-sort-functions
8620           (if (not reverse)
8621               article
8622             `(lambda (t1 t2)
8623                (,article t2 t1))))
8624          (buffer-read-only)
8625          (gnus-summary-prepare-hook nil))
8626     ;; We do the sorting by regenerating the threads.
8627     (gnus-summary-prepare)
8628     ;; Hide subthreads if needed.
8629     (when (and gnus-show-threads gnus-thread-hide-subtree)
8630       (gnus-summary-hide-all-threads))))
8631
8632 ;; Summary saving commands.
8633
8634 (defun gnus-summary-save-article (&optional n not-saved)
8635   "Save the current article using the default saver function.
8636 If N is a positive number, save the N next articles.
8637 If N is a negative number, save the N previous articles.
8638 If N is nil and any articles have been marked with the process mark,
8639 save those articles instead.
8640 The variable `gnus-default-article-saver' specifies the saver function."
8641   (interactive "P")
8642   (let* ((articles (gnus-summary-work-articles n))
8643          (save-buffer (save-excursion
8644                         (nnheader-set-temp-buffer " *Gnus Save*")))
8645          (num (length articles))
8646          header file)
8647     (dolist (article articles)
8648       (setq header (gnus-summary-article-header article))
8649       (if (not (vectorp header))
8650           ;; This is a pseudo-article.
8651           (if (assq 'name header)
8652               (gnus-copy-file (cdr (assq 'name header)))
8653             (gnus-message 1 "Article %d is unsaveable" article))
8654         ;; This is a real article.
8655         (save-window-excursion
8656           (gnus-summary-select-article t nil nil article))
8657         (save-excursion
8658           (set-buffer save-buffer)
8659           (erase-buffer)
8660           (insert-buffer-substring gnus-original-article-buffer))
8661         (setq file (gnus-article-save save-buffer file num))
8662         (gnus-summary-remove-process-mark article)
8663         (unless not-saved
8664           (gnus-summary-set-saved-mark article))))
8665     (gnus-kill-buffer save-buffer)
8666     (gnus-summary-position-point)
8667     (gnus-set-mode-line 'summary)
8668     n))
8669
8670 (defun gnus-summary-pipe-output (&optional arg)
8671   "Pipe the current article to a subprocess.
8672 If N is a positive number, pipe the N next articles.
8673 If N is a negative number, pipe the N previous articles.
8674 If N is nil and any articles have been marked with the process mark,
8675 pipe those articles instead."
8676   (interactive "P")
8677   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8678     (gnus-summary-save-article arg t))
8679   (gnus-configure-windows 'pipe))
8680
8681 (defun gnus-summary-save-article-mail (&optional arg)
8682   "Append the current article to an mail file.
8683 If N is a positive number, save the N next articles.
8684 If N is a negative number, save the N previous articles.
8685 If N is nil and any articles have been marked with the process mark,
8686 save those articles instead."
8687   (interactive "P")
8688   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8689     (gnus-summary-save-article arg)))
8690
8691 (defun gnus-summary-save-article-rmail (&optional arg)
8692   "Append the current article to an rmail file.
8693 If N is a positive number, save the N next articles.
8694 If N is a negative number, save the N previous articles.
8695 If N is nil and any articles have been marked with the process mark,
8696 save those articles instead."
8697   (interactive "P")
8698   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8699     (gnus-summary-save-article arg)))
8700
8701 (defun gnus-summary-save-article-file (&optional arg)
8702   "Append the current article to a file.
8703 If N is a positive number, save the N next articles.
8704 If N is a negative number, save the N previous articles.
8705 If N is nil and any articles have been marked with the process mark,
8706 save those articles instead."
8707   (interactive "P")
8708   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8709     (gnus-summary-save-article arg)))
8710
8711 (defun gnus-summary-write-article-file (&optional arg)
8712   "Write the current article to a file, deleting the previous file.
8713 If N is a positive number, save the N next articles.
8714 If N is a negative number, save the N previous articles.
8715 If N is nil and any articles have been marked with the process mark,
8716 save those articles instead."
8717   (interactive "P")
8718   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8719     (gnus-summary-save-article arg)))
8720
8721 (defun gnus-summary-save-article-body-file (&optional arg)
8722   "Append the current article body to a file.
8723 If N is a positive number, save the N next articles.
8724 If N is a negative number, save the N previous articles.
8725 If N is nil and any articles have been marked with the process mark,
8726 save those articles instead."
8727   (interactive "P")
8728   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8729     (gnus-summary-save-article arg)))
8730
8731 (defun gnus-summary-pipe-message (program)
8732   "Pipe the current article through PROGRAM."
8733   (interactive "sProgram: ")
8734   (gnus-summary-select-article)
8735   (let ((mail-header-separator ""))
8736     (gnus-eval-in-buffer-window gnus-article-buffer
8737       (save-restriction
8738         (widen)
8739         (let ((start (window-start))
8740               buffer-read-only)
8741           (message-pipe-buffer-body program)
8742           (set-window-start (get-buffer-window (current-buffer)) start))))))
8743
8744 (defun gnus-get-split-value (methods)
8745   "Return a value based on the split METHODS."
8746   (let (split-name method result match)
8747     (when methods
8748       (save-excursion
8749         (set-buffer gnus-original-article-buffer)
8750         (save-restriction
8751           (nnheader-narrow-to-headers)
8752           (while methods
8753             (goto-char (point-min))
8754             (setq method (pop methods))
8755             (setq match (car method))
8756             (when (cond
8757                    ((stringp match)
8758                     ;; Regular expression.
8759                     (ignore-errors
8760                       (re-search-forward match nil t)))
8761                    ((gnus-functionp match)
8762                     ;; Function.
8763                     (save-restriction
8764                       (widen)
8765                       (setq result (funcall match gnus-newsgroup-name))))
8766                    ((consp match)
8767                     ;; Form.
8768                     (save-restriction
8769                       (widen)
8770                       (setq result (eval match)))))
8771               (setq split-name (append (cdr method) split-name))
8772               (cond ((stringp result)
8773                      (push (expand-file-name
8774                             result gnus-article-save-directory)
8775                            split-name))
8776                     ((consp result)
8777                      (setq split-name (append result split-name)))))))))
8778     split-name))
8779
8780 (defun gnus-valid-move-group-p (group)
8781   (and (boundp group)
8782        (symbol-name group)
8783        (symbol-value group)
8784        (memq 'respool
8785              (assoc (symbol-name
8786                      (car (gnus-find-method-for-group
8787                            (symbol-name group))))
8788                     gnus-valid-select-methods))))
8789
8790 (defun gnus-read-move-group-name (prompt default articles prefix)
8791   "Read a group name."
8792   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8793          (minibuffer-confirm-incomplete nil) ; XEmacs
8794          (prom
8795           (format "%s %s to:"
8796                   prompt
8797                   (if (> (length articles) 1)
8798                       (format "these %d articles" (length articles))
8799                     "this article")))
8800          (to-newsgroup
8801           (cond
8802            ((null split-name)
8803             (gnus-completing-read default prom
8804                                   gnus-active-hashtb
8805                                   'gnus-valid-move-group-p
8806                                   nil prefix
8807                                   'gnus-group-history))
8808            ((= 1 (length split-name))
8809             (gnus-completing-read (car split-name) prom
8810                                   gnus-active-hashtb
8811                                   'gnus-valid-move-group-p
8812                                   nil nil
8813                                   'gnus-group-history))
8814            (t
8815             (gnus-completing-read nil prom
8816                                   (mapcar (lambda (el) (list el))
8817                                           (nreverse split-name))
8818                                   nil nil nil
8819                                   'gnus-group-history)))))
8820     (when to-newsgroup
8821       (if (or (string= to-newsgroup "")
8822               (string= to-newsgroup prefix))
8823           (setq to-newsgroup default))
8824       (unless to-newsgroup
8825         (error "No group name entered"))
8826       (or (gnus-active to-newsgroup)
8827           (gnus-activate-group to-newsgroup)
8828           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8829                                      to-newsgroup))
8830               (or (and (gnus-request-create-group
8831                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8832                        (gnus-activate-group to-newsgroup nil nil
8833                                             (gnus-group-name-to-method
8834                                              to-newsgroup)))
8835                   (error "Couldn't create group %s" to-newsgroup)))
8836           (error "No such group: %s" to-newsgroup)))
8837     to-newsgroup))
8838
8839 ;; Summary extract commands
8840
8841 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8842   (let ((buffer-read-only nil)
8843         (article (gnus-summary-article-number))
8844         after-article b e)
8845     (unless (gnus-summary-goto-subject article)
8846       (error "No such article: %d" article))
8847     (gnus-summary-position-point)
8848     ;; If all commands are to be bunched up on one line, we collect
8849     ;; them here.
8850     (unless gnus-view-pseudos-separately
8851       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8852             files action)
8853         (while ps
8854           (setq action (cdr (assq 'action (car ps))))
8855           (setq files (list (cdr (assq 'name (car ps)))))
8856           (while (and ps (cdr ps)
8857                       (string= (or action "1")
8858                                (or (cdr (assq 'action (cadr ps))) "2")))
8859             (push (cdr (assq 'name (cadr ps))) files)
8860             (setcdr ps (cddr ps)))
8861           (when files
8862             (when (not (string-match "%s" action))
8863               (push " " files))
8864             (push " " files)
8865             (when (assq 'execute (car ps))
8866               (setcdr (assq 'execute (car ps))
8867                       (funcall (if (string-match "%s" action)
8868                                    'format 'concat)
8869                                action
8870                                (mapconcat
8871                                 (lambda (f)
8872                                   (if (equal f " ")
8873                                       f
8874                                     (mm-quote-arg f)))
8875                                 files " ")))))
8876           (setq ps (cdr ps)))))
8877     (if (and gnus-view-pseudos (not not-view))
8878         (while pslist
8879           (when (assq 'execute (car pslist))
8880             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8881                                   (eq gnus-view-pseudos 'not-confirm)))
8882           (setq pslist (cdr pslist)))
8883       (save-excursion
8884         (while pslist
8885           (setq after-article (or (cdr (assq 'article (car pslist)))
8886                                   (gnus-summary-article-number)))
8887           (gnus-summary-goto-subject after-article)
8888           (forward-line 1)
8889           (setq b (point))
8890           (insert "    " (file-name-nondirectory
8891                           (cdr (assq 'name (car pslist))))
8892                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8893           (setq e (point))
8894           (forward-line -1)             ; back to `b'
8895           (gnus-add-text-properties
8896            b (1- e) (list 'gnus-number gnus-reffed-article-number
8897                           gnus-mouse-face-prop gnus-mouse-face))
8898           (gnus-data-enter
8899            after-article gnus-reffed-article-number
8900            gnus-unread-mark b (car pslist) 0 (- e b))
8901           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8902           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8903           (setq pslist (cdr pslist)))))))
8904
8905 (defun gnus-pseudos< (p1 p2)
8906   (let ((c1 (cdr (assq 'action p1)))
8907         (c2 (cdr (assq 'action p2))))
8908     (and c1 c2 (string< c1 c2))))
8909
8910 (defun gnus-request-pseudo-article (props)
8911   (cond ((assq 'execute props)
8912          (gnus-execute-command (cdr (assq 'execute props)))))
8913   (let ((gnus-current-article (gnus-summary-article-number)))
8914     (gnus-run-hooks 'gnus-mark-article-hook)))
8915
8916 (defun gnus-execute-command (command &optional automatic)
8917   (save-excursion
8918     (gnus-article-setup-buffer)
8919     (set-buffer gnus-article-buffer)
8920     (setq buffer-read-only nil)
8921     (let ((command (if automatic command
8922                      (read-string "Command: " (cons command 0)))))
8923       (erase-buffer)
8924       (insert "$ " command "\n\n")
8925       (if gnus-view-pseudo-asynchronously
8926           (start-process "gnus-execute" (current-buffer) shell-file-name
8927                          shell-command-switch command)
8928         (call-process shell-file-name nil t nil
8929                       shell-command-switch command)))))
8930
8931 ;; Summary kill commands.
8932
8933 (defun gnus-summary-edit-global-kill (article)
8934   "Edit the \"global\" kill file."
8935   (interactive (list (gnus-summary-article-number)))
8936   (gnus-group-edit-global-kill article))
8937
8938 (defun gnus-summary-edit-local-kill ()
8939   "Edit a local kill file applied to the current newsgroup."
8940   (interactive)
8941   (setq gnus-current-headers (gnus-summary-article-header))
8942   (gnus-group-edit-local-kill
8943    (gnus-summary-article-number) gnus-newsgroup-name))
8944
8945 ;;; Header reading.
8946
8947 (defun gnus-read-header (id &optional header)
8948   "Read the headers of article ID and enter them into the Gnus system."
8949   (let ((group gnus-newsgroup-name)
8950         (gnus-override-method
8951          (and (gnus-news-group-p gnus-newsgroup-name)
8952               gnus-refer-article-method))
8953         where)
8954     ;; First we check to see whether the header in question is already
8955     ;; fetched.
8956     (if (stringp id)
8957         ;; This is a Message-ID.
8958         (setq header (or header (gnus-id-to-header id)))
8959       ;; This is an article number.
8960       (setq header (or header (gnus-summary-article-header id))))
8961     (if (and header
8962              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8963         ;; We have found the header.
8964         header
8965       ;; If this is a sparse article, we have to nix out its
8966       ;; previous entry in the thread hashtb.
8967       (when (and header
8968                  (gnus-summary-article-sparse-p (mail-header-number header)))
8969         (let* ((parent (gnus-parent-id (mail-header-references header)))
8970                (thread (and parent (gnus-id-to-thread parent))))
8971           (when thread
8972             (delq (assq header thread) thread))))
8973       ;; We have to really fetch the header to this article.
8974       (save-excursion
8975         (set-buffer nntp-server-buffer)
8976         (when (setq where (gnus-request-head id group))
8977           (nnheader-fold-continuation-lines)
8978           (goto-char (point-max))
8979           (insert ".\n")
8980           (goto-char (point-min))
8981           (insert "211 ")
8982           (princ (cond
8983                   ((numberp id) id)
8984                   ((cdr where) (cdr where))
8985                   (header (mail-header-number header))
8986                   (t gnus-reffed-article-number))
8987                  (current-buffer))
8988           (insert " Article retrieved.\n"))
8989         (if (or (not where)
8990                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8991             ()                          ; Malformed head.
8992           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8993             (when (and (stringp id)
8994                        (not (string= (gnus-group-real-name group)
8995                                      (car where))))
8996               ;; If we fetched by Message-ID and the article came
8997               ;; from a different group, we fudge some bogus article
8998               ;; numbers for this article.
8999               (mail-header-set-number header gnus-reffed-article-number))
9000             (save-excursion
9001               (set-buffer gnus-summary-buffer)
9002               (decf gnus-reffed-article-number)
9003               (gnus-remove-header (mail-header-number header))
9004               (push header gnus-newsgroup-headers)
9005               (setq gnus-current-headers header)
9006               (push (mail-header-number header) gnus-newsgroup-limit)))
9007           header)))))
9008
9009 (defun gnus-remove-header (number)
9010   "Remove header NUMBER from `gnus-newsgroup-headers'."
9011   (if (and gnus-newsgroup-headers
9012            (= number (mail-header-number (car gnus-newsgroup-headers))))
9013       (pop gnus-newsgroup-headers)
9014     (let ((headers gnus-newsgroup-headers))
9015       (while (and (cdr headers)
9016                   (not (= number (mail-header-number (cadr headers)))))
9017         (pop headers))
9018       (when (cdr headers)
9019         (setcdr headers (cddr headers))))))
9020
9021 ;;;
9022 ;;; summary highlights
9023 ;;;
9024
9025 (defun gnus-highlight-selected-summary ()
9026   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9027   ;; Highlight selected article in summary buffer
9028   (when gnus-summary-selected-face
9029     (save-excursion
9030       (let* ((beg (progn (beginning-of-line) (point)))
9031              (end (progn (end-of-line) (point)))
9032              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9033              (from (if (get-text-property beg gnus-mouse-face-prop)
9034                        beg
9035                      (or (next-single-property-change
9036                           beg gnus-mouse-face-prop nil end)
9037                          beg)))
9038              (to
9039               (if (= from end)
9040                   (- from 2)
9041                 (or (next-single-property-change
9042                      from gnus-mouse-face-prop nil end)
9043                     end))))
9044         ;; If no mouse-face prop on line we will have to = from = end,
9045         ;; so we highlight the entire line instead.
9046         (when (= (+ to 2) from)
9047           (setq from beg)
9048           (setq to end))
9049         (if gnus-newsgroup-selected-overlay
9050             ;; Move old overlay.
9051             (gnus-move-overlay
9052              gnus-newsgroup-selected-overlay from to (current-buffer))
9053           ;; Create new overlay.
9054           (gnus-overlay-put
9055            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9056            'face gnus-summary-selected-face))))))
9057
9058 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9059 (defun gnus-summary-highlight-line ()
9060   "Highlight current line according to `gnus-summary-highlight'."
9061   (let* ((list gnus-summary-highlight)
9062          (p (point))
9063          (end (progn (end-of-line) (point)))
9064          ;; now find out where the line starts and leave point there.
9065          (beg (progn (beginning-of-line) (point)))
9066          (article (gnus-summary-article-number))
9067          (score (or (cdr (assq (or article gnus-current-article)
9068                                gnus-newsgroup-scored))
9069                     gnus-summary-default-score 0))
9070          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9071          (inhibit-read-only t))
9072     ;; Eval the cars of the lists until we find a match.
9073     (let ((default gnus-summary-default-score))
9074       (while (and list
9075                   (not (eval (caar list))))
9076         (setq list (cdr list))))
9077     (let ((face (cdar list)))
9078       (unless (eq face (get-text-property beg 'face))
9079         (gnus-put-text-property-excluding-characters-with-faces
9080          beg end 'face
9081          (setq face (if (boundp face) (symbol-value face) face)))
9082         (when gnus-summary-highlight-line-function
9083           (funcall gnus-summary-highlight-line-function article face))))
9084     (goto-char p)))
9085
9086 (defun gnus-update-read-articles (group unread &optional compute)
9087   "Update the list of read articles in GROUP."
9088   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9089          (entry (gnus-gethash group gnus-newsrc-hashtb))
9090          (info (nth 2 entry))
9091          (prev 1)
9092          (unread (sort (copy-sequence unread) '<))
9093          read)
9094     (if (or (not info) (not active))
9095         ;; There is no info on this group if it was, in fact,
9096         ;; killed.  Gnus stores no information on killed groups, so
9097         ;; there's nothing to be done.
9098         ;; One could store the information somewhere temporarily,
9099         ;; perhaps...  Hmmm...
9100         ()
9101       ;; Remove any negative articles numbers.
9102       (while (and unread (< (car unread) 0))
9103         (setq unread (cdr unread)))
9104       ;; Remove any expired article numbers
9105       (while (and unread (< (car unread) (car active)))
9106         (setq unread (cdr unread)))
9107       ;; Compute the ranges of read articles by looking at the list of
9108       ;; unread articles.
9109       (while unread
9110         (when (/= (car unread) prev)
9111           (push (if (= prev (1- (car unread))) prev
9112                   (cons prev (1- (car unread))))
9113                 read))
9114         (setq prev (1+ (car unread)))
9115         (setq unread (cdr unread)))
9116       (when (<= prev (cdr active))
9117         (push (cons prev (cdr active)) read))
9118       (setq read (if (> (length read) 1) (nreverse read) read))
9119       (if compute
9120           read
9121         (save-excursion
9122           (set-buffer gnus-group-buffer)
9123           (gnus-undo-register
9124             `(progn
9125                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9126                (gnus-info-set-read ',info ',(gnus-info-read info))
9127                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9128                (gnus-group-update-group ,group t))))
9129        ;; Propagate the read marks to the backend.
9130        (if (gnus-check-backend-function 'request-set-mark group)
9131            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9132                  (add (gnus-remove-from-range read (gnus-info-read info))))
9133              (when (or add del)
9134                (gnus-request-set-mark
9135                 group (delq nil (list (if add (list add 'add '(read)))
9136                                       (if del (list del 'del '(read)))))))))
9137         ;; Enter this list into the group info.
9138         (gnus-info-set-read info read)
9139         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9140         (gnus-get-unread-articles-in-group info (gnus-active group))
9141         t))))
9142
9143 (defun gnus-offer-save-summaries ()
9144   "Offer to save all active summary buffers."
9145   (save-excursion
9146     (let ((buflist (buffer-list))
9147           buffers bufname)
9148       ;; Go through all buffers and find all summaries.
9149       (while buflist
9150         (and (setq bufname (buffer-name (car buflist)))
9151              (string-match "Summary" bufname)
9152              (save-excursion
9153                (set-buffer bufname)
9154                ;; We check that this is, indeed, a summary buffer.
9155                (and (eq major-mode 'gnus-summary-mode)
9156                     ;; Also make sure this isn't bogus.
9157                     gnus-newsgroup-prepared
9158                     ;; Also make sure that this isn't a dead summary buffer.
9159                     (not gnus-dead-summary-mode)))
9160              (push bufname buffers))
9161         (setq buflist (cdr buflist)))
9162       ;; Go through all these summary buffers and offer to save them.
9163       (when buffers
9164         (map-y-or-n-p
9165          "Update summary buffer %s? "
9166          (lambda (buf)
9167            (switch-to-buffer buf)
9168            (gnus-summary-exit))
9169          buffers)))))
9170
9171 (defun gnus-summary-setup-default-charset ()
9172   "Setup newsgroup default charset."
9173   (let ((name (and gnus-newsgroup-name
9174                    (gnus-group-real-name gnus-newsgroup-name))))
9175     (setq gnus-newsgroup-charset
9176           (or (and gnus-newsgroup-name
9177                    (or (gnus-group-find-parameter gnus-newsgroup-name
9178                                                   'charset)
9179                        (let ((alist gnus-group-charset-alist)
9180                              elem (charset nil))
9181                          (while (setq elem (pop alist))
9182                            (when (and name
9183                                       (string-match (car elem) name))
9184                              (setq alist nil
9185                                    charset (cadr elem))))
9186                          charset)))
9187               gnus-default-charset))))
9188
9189 ;;;
9190 ;;; Mime Commands
9191 ;;;
9192
9193 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9194   "Display the current article buffer fully MIME-buttonized.
9195 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9196 treated as multipart/mixed."
9197   (interactive "P")
9198   (require 'gnus-art)
9199   (let ((gnus-unbuttonized-mime-types nil)
9200         (gnus-mime-display-multipart-as-mixed show-all-parts))
9201     (gnus-summary-show-article)))
9202
9203 (defun gnus-summary-repair-multipart (article)
9204   "Add a Content-Type header to a multipart article without one."
9205   (interactive (list (gnus-summary-article-number)))
9206   (gnus-with-article article
9207     (message-narrow-to-head)
9208     (goto-char (point-max))
9209     (widen)
9210     (when (search-forward "\n--" nil t)
9211       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9212         (message-narrow-to-head)
9213         (message-remove-header "Mime-Version")
9214         (message-remove-header "Content-Type")
9215         (goto-char (point-max))
9216         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9217                         separator))
9218         (insert "Mime-Version: 1.0\n")
9219         (widen))))
9220   (let (gnus-mark-article-hook)
9221     (gnus-summary-select-article t t nil article)))
9222
9223 (defun gnus-summary-toggle-display-buttonized ()
9224   "Toggle the buttonizing of the article buffer."
9225   (interactive)
9226   (require 'gnus-art)
9227   (if (setq gnus-inhibit-mime-unbuttonizing
9228             (not gnus-inhibit-mime-unbuttonizing))
9229       (let ((gnus-unbuttonized-mime-types nil))
9230         (gnus-summary-show-article))
9231     (gnus-summary-show-article)))
9232
9233 ;;;
9234 ;;; with article
9235 ;;;
9236
9237 (defmacro gnus-with-article (article &rest forms)
9238   "Select ARTICLE and perform FORMS in the original article buffer.
9239 Then replace the article with the result."
9240   `(progn
9241      ;; We don't want the article to be marked as read.
9242      (let (gnus-mark-article-hook)
9243        (gnus-summary-select-article t t nil ,article))
9244      (set-buffer gnus-original-article-buffer)
9245      ,@forms
9246      (if (not (gnus-check-backend-function
9247                'request-replace-article (car gnus-article-current)))
9248          (gnus-message 5 "Read-only group; not replacing")
9249        (unless (gnus-request-replace-article
9250                 ,article (car gnus-article-current)
9251                 (current-buffer) t)
9252          (error "Couldn't replace article")))
9253      ;; The cache and backlog have to be flushed somewhat.
9254      (when gnus-keep-backlog
9255        (gnus-backlog-remove-article
9256         (car gnus-article-current) (cdr gnus-article-current)))
9257      (when gnus-use-cache
9258        (gnus-cache-update-article
9259         (car gnus-article-current) (cdr gnus-article-current)))))
9260
9261 (put 'gnus-with-article 'lisp-indent-function 1)
9262 (put 'gnus-with-article 'edebug-form-spec '(form body))
9263
9264
9265 (gnus-ems-redefine)
9266
9267 (provide 'gnus-sum)
9268
9269 (run-hooks 'gnus-sum-load-hook)
9270
9271 ;;; gnus-sum.el ends here