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