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