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