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