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