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