c08299545d548525b02018d19df8b4494aedd1a9
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   ;; Avoid byte-compile warnings.
44   (defvar gnus-article-decoded-p)
45   (defvar gnus-decode-encoded-word-function)
46   )
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
50
51 (defcustom gnus-kill-summary-on-exit t
52   "*If non-nil, kill the summary buffer when you exit from it.
53 If nil, the summary will become a \"*Dead Summary*\" buffer, and
54 it will be killed sometime later."
55   :group 'gnus-summary-exit
56   :type 'boolean)
57
58 (defcustom gnus-fetch-old-headers nil
59   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
60 If an unread article in the group refers to an older, already read (or
61 just marked as read) article, the old article will not normally be
62 displayed in the Summary buffer.  If this variable is non-nil, Gnus
63 will attempt to grab the headers to the old articles, and thereby
64 build complete threads.  If it has the value `some', only enough
65 headers to connect otherwise loose threads will be displayed.  This
66 variable can also be a number.  In that case, no more than that number
67 of old headers will be fetched.  If it has the value `invisible', all
68 old headers will be fetched, but none will be displayed.
69
70 The server has to support NOV for any of this to work."
71   :group 'gnus-thread
72   :type '(choice (const :tag "off" nil)
73                  (const some)
74                  number
75                  (sexp :menu-tag "other" t)))
76
77 (defcustom gnus-refer-thread-limit 200
78   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
79 If t, fetch all the available old headers."
80   :group 'gnus-thread
81   :type '(choice number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-summary-make-false-root 'adopt
85   "*nil means that Gnus won't gather loose threads.
86 If the root of a thread has expired or been read in a previous
87 session, the information necessary to build a complete thread has been
88 lost.  Instead of having many small sub-threads from this original thread
89 scattered all over the summary buffer, Gnus can gather them.
90
91 If non-nil, Gnus will try to gather all loose sub-threads from an
92 original thread into one large thread.
93
94 If this variable is non-nil, it should be one of `none', `adopt',
95 `dummy' or `empty'.
96
97 If this variable is `none', Gnus will not make a false root, but just
98 present the sub-threads after another.
99 If this variable is `dummy', Gnus will create a dummy root that will
100 have all the sub-threads as children.
101 If this variable is `adopt', Gnus will make one of the \"children\"
102 the parent and mark all the step-children as such.
103 If this variable is `empty', the \"children\" are printed with empty
104 subject fields.  (Or rather, they will be printed with a string
105 given by the `gnus-summary-same-subject' variable.)"
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const none)
109                  (const dummy)
110                  (const adopt)
111                  (const empty)))
112
113 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
114   "*A regexp to match subjects to be excluded from loose thread gathering.
115 As loose thread gathering is done on subjects only, that means that
116 there can be many false gatherings performed.  By rooting out certain
117 common subjects, gathering might become saner."
118   :group 'gnus-thread
119   :type 'regexp)
120
121 (defcustom gnus-summary-gather-subject-limit nil
122   "*Maximum length of subject comparisons when gathering loose threads.
123 Use nil to compare full subjects.  Setting this variable to a low
124 number will help gather threads that have been corrupted by
125 newsreaders chopping off subject lines, but it might also mean that
126 unrelated articles that have subject that happen to begin with the
127 same few characters will be incorrectly gathered.
128
129 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
130 comparing subjects."
131   :group 'gnus-thread
132   :type '(choice (const :tag "off" nil)
133                  (const fuzzy)
134                  (sexp :menu-tag "on" t)))
135
136 (defcustom gnus-simplify-subject-functions nil
137   "List of functions taking a string argument that simplify subjects.
138 The functions are applied recursively.
139
140 Useful functions to put in this list include: `gnus-simplify-subject-re',
141 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, marking commands will go to the next unread article.
183 If `never', commands that usually go to the next unread article, will
184 go to the next article, whether it is read or not.
185 If nil, only the marking commands will go to the next (un)read article."
186   :group 'gnus-summary-marks
187   :link '(custom-manual "(gnus)Setting Marks")
188   :type '(choice (const :tag "off" nil)
189                  (const never)
190                  (sexp :menu-tag "on" t)))
191
192 (defcustom gnus-summary-default-score 0
193   "*Default article score level.
194 All scores generated by the score files will be added to this score.
195 If this variable is nil, scoring will be disabled."
196   :group 'gnus-score-default
197   :type '(choice (const :tag "disable")
198                  integer))
199
200 (defcustom gnus-summary-zcore-fuzz 0
201   "*Fuzziness factor for the zcore in the summary buffer.
202 Articles with scores closer than this to `gnus-summary-default-score'
203 will not be marked."
204   :group 'gnus-summary-format
205   :type 'integer)
206
207 (defcustom gnus-simplify-subject-fuzzy-regexp nil
208   "*Strings to be removed when doing fuzzy matches.
209 This can either be a regular expression or list of regular expressions
210 that will be removed from subject strings if fuzzy subject
211 simplification is selected."
212   :group 'gnus-thread
213   :type '(repeat regexp))
214
215 (defcustom gnus-show-threads t
216   "*If non-nil, display threads in summary mode."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-subtree nil
221   "*If non-nil, hide all threads initially.
222 If threads are hidden, you have to run the command
223 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
224 to expose hidden threads."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-killed t
229   "*If non-nil, hide killed threads automatically."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-ignore-subject t
234   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
235 If nil, articles that have different subjects from their parents will
236 start separate threads."
237   :group 'gnus-thread
238   :type 'boolean)
239
240 (defcustom gnus-thread-operation-ignore-subject t
241   "*If non-nil, subjects will be ignored when doing thread commands.
242 This affects commands like `gnus-summary-kill-thread' and
243 `gnus-summary-lower-thread'.
244
245 If this variable is nil, articles in the same thread with different
246 subjects will not be included in the operation in question.  If this
247 variable is `fuzzy', only articles that have subjects that are fuzzily
248 equal will be included."
249   :group 'gnus-thread
250   :type '(choice (const :tag "off" nil)
251                  (const fuzzy)
252                  (sexp :tag "on" t)))
253
254 (defcustom gnus-thread-indent-level 4
255   "*Number that says how much each sub-thread should be indented."
256   :group 'gnus-thread
257   :type 'integer)
258
259 (defcustom gnus-auto-extend-newsgroup t
260   "*If non-nil, extend newsgroup forward and backward when requested."
261   :group 'gnus-summary-choose
262   :type 'boolean)
263
264 (defcustom gnus-auto-select-first t
265   "*If nil, don't select the first unread article when entering a group.
266 If this variable is `best', select the highest-scored unread article
267 in the group.  If t, select the first unread article.
268
269 This variable can also be a function to place point on a likely
270 subject line.  Useful values include `gnus-summary-first-unread-subject',
271 `gnus-summary-first-unread-article' and
272 `gnus-summary-best-unread-article'.
273
274 If you want to prevent automatic selection of the first unread article
275 in some newsgroups, set the variable to nil in
276 `gnus-select-group-hook'."
277   :group 'gnus-group-select
278   :type '(choice (const :tag "none" nil)
279                  (const best)
280                  (sexp :menu-tag "first" t)
281                  (function-item gnus-summary-first-unread-subject)
282                  (function-item gnus-summary-first-unread-article)
283                  (function-item gnus-summary-best-unread-article)))
284
285 (defcustom gnus-auto-select-next t
286   "*If non-nil, offer to go to the next group from the end of the previous.
287 If the value is t and the next newsgroup is empty, Gnus will exit
288 summary mode and go back to group mode.  If the value is neither nil
289 nor t, Gnus will select the following unread newsgroup.  In
290 particular, if the value is the symbol `quietly', the next unread
291 newsgroup will be selected without any confirmation, and if it is
292 `almost-quietly', the next group will be selected without any
293 confirmation if you are located on the last article in the group.
294 Finally, if this variable is `slightly-quietly', the `Z n' command
295 will go to the next group without confirmation."
296   :group 'gnus-summary-maneuvering
297   :type '(choice (const :tag "off" nil)
298                  (const quietly)
299                  (const almost-quietly)
300                  (const slightly-quietly)
301                  (sexp :menu-tag "on" t)))
302
303 (defcustom gnus-auto-select-same nil
304   "*If non-nil, select the next article with the same subject.
305 If there are no more articles with the same subject, go to
306 the first unread article."
307   :group 'gnus-summary-maneuvering
308   :type 'boolean)
309
310 (defcustom gnus-summary-check-current nil
311   "*If non-nil, consider the current article when moving.
312 The \"unread\" movement commands will stay on the same line if the
313 current article is unread."
314   :group 'gnus-summary-maneuvering
315   :type 'boolean)
316
317 (defcustom gnus-auto-center-summary t
318   "*If non-nil, always center the current summary buffer.
319 In particular, if `vertical' do only vertical recentering.  If non-nil
320 and non-`vertical', do both horizontal and vertical recentering."
321   :group 'gnus-summary-maneuvering
322   :type '(choice (const :tag "none" nil)
323                  (const vertical)
324                  (integer :tag "height")
325                  (sexp :menu-tag "both" t)))
326
327 (defcustom gnus-show-all-headers nil
328   "*If non-nil, don't hide any headers."
329   :group 'gnus-article-hiding
330   :group 'gnus-article-headers
331   :type 'boolean)
332
333 (defcustom gnus-summary-ignore-duplicates nil
334   "*If non-nil, ignore articles with identical Message-ID headers."
335   :group 'gnus-summary
336   :type 'boolean)
337
338 (defcustom gnus-single-article-buffer t
339   "*If non-nil, display all articles in the same buffer.
340 If nil, each group will get its own article buffer."
341   :group 'gnus-article-various
342   :type 'boolean)
343
344 (defcustom gnus-break-pages t
345   "*If non-nil, do page breaking on articles.
346 The page delimiter is specified by the `gnus-page-delimiter'
347 variable."
348   :group 'gnus-article-various
349   :type 'boolean)
350
351 (defcustom gnus-show-mime t
352   "*If non-nil, do mime processing of articles.
353 The articles will simply be fed to the function given by
354 `gnus-article-display-method-for-mime'."
355   :group 'gnus-article-mime
356   :type 'boolean)
357
358 (defcustom gnus-move-split-methods nil
359   "*Variable used to suggest where articles are to be moved to.
360 It uses the same syntax as the `gnus-split-methods' variable."
361   :group 'gnus-summary-mail
362   :type '(repeat (choice (list :value (fun) function)
363                          (cons :value ("" "") regexp (repeat string))
364                          (sexp :value nil))))
365
366 (defcustom gnus-unread-mark ? ;Whitespace
367   "*Mark used for unread articles."
368   :group 'gnus-summary-marks
369   :type 'character)
370
371 (defcustom gnus-ticked-mark ?!
372   "*Mark used for ticked articles."
373   :group 'gnus-summary-marks
374   :type 'character)
375
376 (defcustom gnus-dormant-mark ??
377   "*Mark used for dormant articles."
378   :group 'gnus-summary-marks
379   :type 'character)
380
381 (defcustom gnus-del-mark ?r
382   "*Mark used for del'd articles."
383   :group 'gnus-summary-marks
384   :type 'character)
385
386 (defcustom gnus-read-mark ?R
387   "*Mark used for read articles."
388   :group 'gnus-summary-marks
389   :type 'character)
390
391 (defcustom gnus-expirable-mark ?E
392   "*Mark used for expirable articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-killed-mark ?K
397   "*Mark used for killed articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-souped-mark ?F
402   "*Mark used for killed articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-kill-file-mark ?X
407   "*Mark used for articles killed by kill files."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-low-score-mark ?Y
412   "*Mark used for articles with a low score."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-catchup-mark ?C
417   "*Mark used for articles that are caught up."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-replied-mark ?A
422   "*Mark used for articles that have been replied to."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-cached-mark ?*
427   "*Mark used for articles that are in the cache."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-saved-mark ?S
432   "*Mark used for articles that have been saved to."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-ancient-mark ?O
437   "*Mark used for ancient articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-sparse-mark ?Q
442   "*Mark used for sparsely reffed articles."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-canceled-mark ?G
447   "*Mark used for canceled articles."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-duplicate-mark ?M
452   "*Mark used for duplicate articles."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-undownloaded-mark ?@
457   "*Mark used for articles that weren't downloaded."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-downloadable-mark ?%
462   "*Mark used for articles that are to be downloaded."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-unsendable-mark ?=
467   "*Mark used for articles that won't be sent."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-score-over-mark ?+
472   "*Score mark used for articles with high scores."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-score-below-mark ?-
477   "*Score mark used for articles with low scores."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-empty-thread-mark ? ;Whitespace
482   "*There is no thread under the article."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-not-empty-thread-mark ?=
487   "*There is a thread under the article."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-view-pseudo-asynchronously nil
492   "*If non-nil, Gnus will view pseudo-articles asynchronously."
493   :group 'gnus-extract-view
494   :type 'boolean)
495
496 (defcustom gnus-auto-expirable-marks
497   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
498         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
499         gnus-souped-mark gnus-duplicate-mark)
500   "*The list of marks converted into expiration if a group is auto-expirable."
501   :group 'gnus-summary
502   :type '(repeat character))
503
504 (defcustom gnus-inhibit-user-auto-expire t
505   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
506   :group 'gnus-summary
507   :type 'boolean)
508
509 (defcustom gnus-view-pseudos nil
510   "*If `automatic', pseudo-articles will be viewed automatically.
511 If `not-confirm', pseudos will be viewed automatically, and the user
512 will not be asked to confirm the command."
513   :group 'gnus-extract-view
514   :type '(choice (const :tag "off" nil)
515                  (const automatic)
516                  (const not-confirm)))
517
518 (defcustom gnus-view-pseudos-separately t
519   "*If non-nil, one pseudo-article will be created for each file to be viewed.
520 If nil, all files that use the same viewing command will be given as a
521 list of parameters to that command."
522   :group 'gnus-extract-view
523   :type 'boolean)
524
525 (defcustom gnus-insert-pseudo-articles t
526   "*If non-nil, insert pseudo-articles when decoding articles."
527   :group 'gnus-extract-view
528   :type 'boolean)
529
530 (defcustom gnus-summary-dummy-line-format
531   "  %(:                          :%) %S\n"
532   "*The format specification for the dummy roots in the summary buffer.
533 It works along the same lines as a normal formatting string,
534 with some simple extensions.
535
536 %S  The subject"
537   :group 'gnus-threading
538   :type 'string)
539
540 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
541   "*The format specification for the summary mode line.
542 It works along the same lines as a normal formatting string,
543 with some simple extensions:
544
545 %G  Group name
546 %p  Unprefixed group name
547 %A  Current article number
548 %z  Current article score
549 %V  Gnus version
550 %U  Number of unread articles in the group
551 %e  Number of unselected articles in the group
552 %Z  A string with unread/unselected article counts
553 %g  Shortish group name
554 %S  Subject of the current article
555 %u  User-defined spec
556 %s  Current score file name
557 %d  Number of dormant articles
558 %r  Number of articles that have been marked as read in this session
559 %E  Number of articles expunged by the score files"
560   :group 'gnus-summary-format
561   :type 'string)
562
563 (defcustom gnus-summary-mark-below 0
564   "*Mark all articles with a score below this variable as read.
565 This variable is local to each summary buffer and usually set by the
566 score file."
567   :group 'gnus-score-default
568   :type 'integer)
569
570 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
571   "*List of functions used for sorting articles in the summary buffer.
572 This variable is only used when not using a threaded display."
573   :group 'gnus-summary-sort
574   :type '(repeat (choice (function-item gnus-article-sort-by-number)
575                          (function-item gnus-article-sort-by-author)
576                          (function-item gnus-article-sort-by-subject)
577                          (function-item gnus-article-sort-by-date)
578                          (function-item gnus-article-sort-by-score)
579                          (function :tag "other"))))
580
581 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
582   "*List of functions used for sorting threads in the summary buffer.
583 By default, threads are sorted by article number.
584
585 Each function takes two threads and return non-nil if the first thread
586 should be sorted before the other.  If you use more than one function,
587 the primary sort function should be the last.  You should probably
588 always include `gnus-thread-sort-by-number' in the list of sorting
589 functions -- preferably first.
590
591 Ready-made functions include `gnus-thread-sort-by-number',
592 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
593 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
594 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
595   :group 'gnus-summary-sort
596   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
597                          (function-item gnus-thread-sort-by-author)
598                          (function-item gnus-thread-sort-by-subject)
599                          (function-item gnus-thread-sort-by-date)
600                          (function-item gnus-thread-sort-by-score)
601                          (function-item gnus-thread-sort-by-total-score)
602                          (function :tag "other"))))
603
604 (defcustom gnus-thread-score-function '+
605   "*Function used for calculating the total score of a thread.
606
607 The function is called with the scores of the article and each
608 subthread and should then return the score of the thread.
609
610 Some functions you can use are `+', `max', or `min'."
611   :group 'gnus-summary-sort
612   :type 'function)
613
614 (defcustom gnus-summary-expunge-below nil
615   "All articles that have a score less than this variable will be expunged.
616 This variable is local to the summary buffers."
617   :group 'gnus-score-default
618   :type '(choice (const :tag "off" nil)
619                  integer))
620
621 (defcustom gnus-thread-expunge-below nil
622   "All threads that have a total score less than this variable will be expunged.
623 See `gnus-thread-score-function' for en explanation of what a
624 \"thread score\" is.
625
626 This variable is local to the summary buffers."
627   :group 'gnus-threading
628   :group 'gnus-score-default
629   :type '(choice (const :tag "off" nil)
630                  integer))
631
632 (defcustom gnus-summary-mode-hook nil
633   "*A hook for Gnus summary mode.
634 This hook is run before any variables are set in the summary buffer."
635   :group 'gnus-summary-various
636   :type 'hook)
637
638 (defcustom gnus-summary-menu-hook nil
639   "*Hook run after the creation of the summary mode menu."
640   :group 'gnus-summary-visual
641   :type 'hook)
642
643 (defcustom gnus-summary-exit-hook nil
644   "*A hook called on exit from the summary buffer.
645 It will be called with point in the group buffer."
646   :group 'gnus-summary-exit
647   :type 'hook)
648
649 (defcustom gnus-summary-prepare-hook nil
650   "*A hook called after the summary buffer has been generated.
651 If you want to modify the summary buffer, you can use this hook."
652   :group 'gnus-summary-various
653   :type 'hook)
654
655 (defcustom gnus-summary-prepared-hook nil
656   "*A hook called as the last thing after the summary buffer has been generated."
657   :group 'gnus-summary-various
658   :type 'hook)
659
660 (defcustom gnus-summary-generate-hook nil
661   "*A hook run just before generating the summary buffer.
662 This hook is commonly used to customize threading variables and the
663 like."
664   :group 'gnus-summary-various
665   :type 'hook)
666
667 (defcustom gnus-select-group-hook nil
668   "*A hook called when a newsgroup is selected.
669
670 If you'd like to simplify subjects like the
671 `gnus-summary-next-same-subject' command does, you can use the
672 following hook:
673
674  (setq gnus-select-group-hook
675       (list
676         (lambda ()
677           (mapcar (lambda (header)
678                      (mail-header-set-subject
679                       header
680                       (gnus-simplify-subject
681                        (mail-header-subject header) 're-only)))
682                   gnus-newsgroup-headers))))"
683   :group 'gnus-group-select
684   :type 'hook)
685
686 (defcustom gnus-select-article-hook nil
687   "*A hook called when an article is selected."
688   :group 'gnus-summary-choose
689   :type 'hook)
690
691 (defcustom gnus-visual-mark-article-hook
692   (list 'gnus-highlight-selected-summary)
693   "*Hook run after selecting an article in the summary buffer.
694 It is meant to be used for highlighting the article in some way.  It
695 is not run if `gnus-visual' is nil."
696   :group 'gnus-summary-visual
697   :type 'hook)
698
699 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
700   "*A hook called before parsing the headers."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-exit-group-hook nil
705   "*A hook called when exiting (not quitting) summary mode."
706   :group 'gnus-various
707   :type 'hook)
708
709 (defcustom gnus-summary-update-hook
710   (list 'gnus-summary-highlight-line)
711   "*A hook called when a summary line is changed.
712 The hook will not be called if `gnus-visual' is nil.
713
714 The default function `gnus-summary-highlight-line' will
715 highlight the line according to the `gnus-summary-highlight'
716 variable."
717   :group 'gnus-summary-visual
718   :type 'hook)
719
720 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
721   "*A hook called when an article is selected for the first time.
722 The hook is intended to mark an article as read (or unread)
723 automatically when it is selected."
724   :group 'gnus-summary-choose
725   :type 'hook)
726
727 (defcustom gnus-group-no-more-groups-hook nil
728   "*A hook run when returning to group mode having no more (unread) groups."
729   :group 'gnus-group-select
730   :type 'hook)
731
732 (defcustom gnus-ps-print-hook nil
733   "*A hook run before ps-printing something from Gnus."
734   :group 'gnus-summary
735   :type 'hook)
736
737 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
738   "Face used for highlighting the current article in the summary buffer."
739   :group 'gnus-summary-visual
740   :type 'face)
741
742 (defcustom gnus-summary-highlight
743   '(((= mark gnus-canceled-mark)
744      . gnus-summary-cancelled-face)
745     ((and (> score default)
746           (or (= mark gnus-dormant-mark)
747               (= mark gnus-ticked-mark)))
748      . gnus-summary-high-ticked-face)
749     ((and (< score default)
750           (or (= mark gnus-dormant-mark)
751               (= mark gnus-ticked-mark)))
752      . gnus-summary-low-ticked-face)
753     ((or (= mark gnus-dormant-mark)
754          (= mark gnus-ticked-mark))
755      . gnus-summary-normal-ticked-face)
756     ((and (> score default) (= mark gnus-ancient-mark))
757      . gnus-summary-high-ancient-face)
758     ((and (< score default) (= mark gnus-ancient-mark))
759      . gnus-summary-low-ancient-face)
760     ((= mark gnus-ancient-mark)
761      . gnus-summary-normal-ancient-face)
762     ((and (> score default) (= mark gnus-unread-mark))
763      . gnus-summary-high-unread-face)
764     ((and (< score default) (= mark gnus-unread-mark))
765      . gnus-summary-low-unread-face)
766     ((= mark gnus-unread-mark)
767      . gnus-summary-normal-unread-face)
768     ((and (> score default) (memq mark (list gnus-downloadable-mark
769                                              gnus-undownloaded-mark)))
770      . gnus-summary-high-unread-face)
771     ((and (< score default) (memq mark (list gnus-downloadable-mark
772                                              gnus-undownloaded-mark)))
773      . gnus-summary-low-unread-face)
774     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
775      . gnus-summary-normal-unread-face)
776     ((> score default)
777      . gnus-summary-high-read-face)
778     ((< score default)
779      . gnus-summary-low-read-face)
780     (t
781      . gnus-summary-normal-read-face))
782   "*Controls the highlighting of summary buffer lines.
783
784 A list of (FORM . FACE) pairs.  When deciding how a a particular
785 summary line should be displayed, each form is evaluated.  The content
786 of the face field after the first true form is used.  You can change
787 how those summary lines are displayed, by editing the face field.
788
789 You can use the following variables in the FORM field.
790
791 score:   The articles score
792 default: The default article score.
793 below:   The score below which articles are automatically marked as read.
794 mark:    The articles mark."
795   :group 'gnus-summary-visual
796   :type '(repeat (cons (sexp :tag "Form" nil)
797                        face)))
798
799 (defcustom gnus-alter-header-function nil
800   "Function called to allow alteration of article header structures.
801 The function is called with one parameter, the article header vector,
802 which it may alter in any way.")
803
804 (defcustom gnus-extra-headers nil
805   "*Extra headers to parse."
806   :group 'gnus-summary
807   :type '(repeat symbol))
808
809 (defcustom gnus-ignored-from-addresses
810   (and user-mail-address (regexp-quote user-mail-address))
811   "*Regexp of From headers that may be suppressed in favor of To headers."
812   :group 'gnus-summary
813   :type 'regexp)
814
815 (defcustom gnus-group-charset-alist
816   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
817     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
818     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
819     ("^relcom\\>" koi8-r)
820     ("^\\(cz\\|hun\\|pl\\|sk\\)\\>" iso-8859-2)
821     ("^israel\\>" iso-8859-1)
822     ("^han\\>" euc-kr)
823     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
824     (".*" iso-8859-1))
825   "Alist of regexps (to match group names) and default charsets to be used when reading."
826   :type '(repeat (list (regexp :tag "Group")
827                        (symbol :tag "Charset")))
828   :group 'gnus-charset)
829
830 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
831   "List of charsets that should be ignored.
832 When these charsets are used in the \"charset\" parameter, the
833 default charset will be used instead."
834   :type '(repeat symbol)
835   :group 'gnus-charset)
836
837 ;;; Internal variables
838
839 (defvar gnus-scores-exclude-files nil)
840 (defvar gnus-page-broken nil)
841 (defvar gnus-inhibit-mime-unbuttonizing nil)
842
843 (defvar gnus-original-article nil)
844 (defvar gnus-article-internal-prepare-hook nil)
845 (defvar gnus-newsgroup-process-stack nil)
846
847 (defvar gnus-thread-indent-array nil)
848 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
849
850 ;; Avoid highlighting in kill files.
851 (defvar gnus-summary-inhibit-highlight nil)
852 (defvar gnus-newsgroup-selected-overlay nil)
853 (defvar gnus-inhibit-limiting nil)
854 (defvar gnus-newsgroup-adaptive-score-file nil)
855 (defvar gnus-current-score-file nil)
856 (defvar gnus-current-move-group nil)
857 (defvar gnus-current-copy-group nil)
858 (defvar gnus-current-crosspost-group nil)
859
860 (defvar gnus-newsgroup-dependencies nil)
861 (defvar gnus-newsgroup-adaptive nil)
862 (defvar gnus-summary-display-article-function nil)
863 (defvar gnus-summary-highlight-line-function nil
864   "Function called after highlighting a summary line.")
865
866 (defvar gnus-summary-line-format-alist
867   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
868     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
869     (?s gnus-tmp-subject-or-nil ?s)
870     (?n gnus-tmp-name ?s)
871     (?A (std11-address-string
872          (car (mime-read-field 'From gnus-tmp-header))) ?s)
873     (?a (or (std11-full-name-string
874              (car (mime-read-field 'From gnus-tmp-header)))
875             gnus-tmp-from) ?s)
876     (?F gnus-tmp-from ?s)
877     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
878     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
879     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
880     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
881     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
882     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
883     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
884     (?L gnus-tmp-lines ?d)
885     (?I gnus-tmp-indentation ?s)
886     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
887     (?R gnus-tmp-replied ?c)
888     (?\[ gnus-tmp-opening-bracket ?c)
889     (?\] gnus-tmp-closing-bracket ?c)
890     (?\> (make-string gnus-tmp-level ? ) ?s)
891     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
892     (?i gnus-tmp-score ?d)
893     (?z gnus-tmp-score-char ?c)
894     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
895     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
896     (?U gnus-tmp-unread ?c)
897     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
898     (?t (gnus-summary-number-of-articles-in-thread
899          (and (boundp 'thread) (car thread)) gnus-tmp-level)
900         ?d)
901     (?e (gnus-summary-number-of-articles-in-thread
902          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
903         ?c)
904     (?u gnus-tmp-user-defined ?s)
905     (?P (gnus-pick-line-number) ?d))
906   "An alist of format specifications that can appear in summary lines,
907 and what variables they correspond with, along with the type of the
908 variable (string, integer, character, etc).")
909
910 (defvar gnus-summary-dummy-line-format-alist
911   `((?S gnus-tmp-subject ?s)
912     (?N gnus-tmp-number ?d)
913     (?u gnus-tmp-user-defined ?s)))
914
915 (defvar gnus-summary-mode-line-format-alist
916   `((?G gnus-tmp-group-name ?s)
917     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
918     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
919     (?A gnus-tmp-article-number ?d)
920     (?Z gnus-tmp-unread-and-unselected ?s)
921     (?V gnus-version ?s)
922     (?U gnus-tmp-unread-and-unticked ?d)
923     (?S gnus-tmp-subject ?s)
924     (?e gnus-tmp-unselected ?d)
925     (?u gnus-tmp-user-defined ?s)
926     (?d (length gnus-newsgroup-dormant) ?d)
927     (?t (length gnus-newsgroup-marked) ?d)
928     (?r (length gnus-newsgroup-reads) ?d)
929     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
930     (?E gnus-newsgroup-expunged-tally ?d)
931     (?s (gnus-current-score-file-nondirectory) ?s)))
932
933 (defvar gnus-last-search-regexp nil
934   "Default regexp for article search command.")
935
936 (defvar gnus-last-shell-command nil
937   "Default shell command on article.")
938
939 (defvar gnus-newsgroup-begin nil)
940 (defvar gnus-newsgroup-end nil)
941 (defvar gnus-newsgroup-last-rmail nil)
942 (defvar gnus-newsgroup-last-mail nil)
943 (defvar gnus-newsgroup-last-folder nil)
944 (defvar gnus-newsgroup-last-file nil)
945 (defvar gnus-newsgroup-auto-expire nil)
946 (defvar gnus-newsgroup-active nil)
947
948 (defvar gnus-newsgroup-data nil)
949 (defvar gnus-newsgroup-data-reverse nil)
950 (defvar gnus-newsgroup-limit nil)
951 (defvar gnus-newsgroup-limits nil)
952
953 (defvar gnus-newsgroup-unreads nil
954   "List of unread articles in the current newsgroup.")
955
956 (defvar gnus-newsgroup-unselected nil
957   "List of unselected unread articles in the current newsgroup.")
958
959 (defvar gnus-newsgroup-reads nil
960   "Alist of read articles and article marks in the current newsgroup.")
961
962 (defvar gnus-newsgroup-expunged-tally nil)
963
964 (defvar gnus-newsgroup-marked nil
965   "List of ticked articles in the current newsgroup (a subset of unread art).")
966
967 (defvar gnus-newsgroup-killed nil
968   "List of ranges of articles that have been through the scoring process.")
969
970 (defvar gnus-newsgroup-cached nil
971   "List of articles that come from the article cache.")
972
973 (defvar gnus-newsgroup-saved nil
974   "List of articles that have been saved.")
975
976 (defvar gnus-newsgroup-kill-headers nil)
977
978 (defvar gnus-newsgroup-replied nil
979   "List of articles that have been replied to in the current newsgroup.")
980
981 (defvar gnus-newsgroup-expirable nil
982   "List of articles in the current newsgroup that can be expired.")
983
984 (defvar gnus-newsgroup-processable nil
985   "List of articles in the current newsgroup that can be processed.")
986
987 (defvar gnus-newsgroup-downloadable nil
988   "List of articles in the current newsgroup that can be processed.")
989
990 (defvar gnus-newsgroup-undownloaded nil
991   "List of articles in the current newsgroup that haven't been downloaded..")
992
993 (defvar gnus-newsgroup-unsendable nil
994   "List of articles in the current newsgroup that won't be sent.")
995
996 (defvar gnus-newsgroup-bookmarks nil
997   "List of articles in the current newsgroup that have bookmarks.")
998
999 (defvar gnus-newsgroup-dormant nil
1000   "List of dormant articles in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-scored nil
1003   "List of scored articles in the current newsgroup.")
1004
1005 (defvar gnus-newsgroup-headers nil
1006   "List of article headers in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-threads nil)
1009
1010 (defvar gnus-newsgroup-prepared nil
1011   "Whether the current group has been prepared properly.")
1012
1013 (defvar gnus-newsgroup-ancient nil
1014   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1015
1016 (defvar gnus-newsgroup-sparse nil)
1017
1018 (defvar gnus-current-article nil)
1019 (defvar gnus-article-current nil)
1020 (defvar gnus-current-headers nil)
1021 (defvar gnus-have-all-headers nil)
1022 (defvar gnus-last-article nil)
1023 (defvar gnus-newsgroup-history nil)
1024 (defvar gnus-newsgroup-charset nil)
1025
1026 (defconst gnus-summary-local-variables
1027   '(gnus-newsgroup-name
1028     gnus-newsgroup-begin gnus-newsgroup-end
1029     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1030     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1031     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1032     gnus-newsgroup-unselected gnus-newsgroup-marked
1033     gnus-newsgroup-reads gnus-newsgroup-saved
1034     gnus-newsgroup-replied gnus-newsgroup-expirable
1035     gnus-newsgroup-processable gnus-newsgroup-killed
1036     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1037     gnus-newsgroup-unsendable
1038     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1039     gnus-newsgroup-headers gnus-newsgroup-threads
1040     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1041     gnus-current-article gnus-current-headers gnus-have-all-headers
1042     gnus-last-article gnus-article-internal-prepare-hook
1043     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1044     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1045     gnus-thread-expunge-below
1046     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1047     (gnus-summary-mark-below . global)
1048     gnus-newsgroup-active gnus-scores-exclude-files
1049     gnus-newsgroup-history gnus-newsgroup-ancient
1050     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1051     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1052     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1053     (gnus-newsgroup-expunged-tally . 0)
1054     gnus-cache-removable-articles gnus-newsgroup-cached
1055     gnus-newsgroup-data gnus-newsgroup-data-reverse
1056     gnus-newsgroup-limit gnus-newsgroup-limits
1057     gnus-newsgroup-charset)
1058   "Variables that are buffer-local to the summary buffers.")
1059
1060 ;; Byte-compiler warning.
1061 (defvar gnus-article-mode-map)
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-c" gnus-summary-sort-by-chars
1254     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1255     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1256     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1257     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1258     "=" gnus-summary-expand-window
1259     "\C-x\C-s" gnus-summary-reselect-current-group
1260     "\M-g" gnus-summary-rescan-group
1261     "w" gnus-summary-stop-page-breaking
1262     "\C-c\C-r" gnus-summary-caesar-message
1263     "\M-t" gnus-summary-toggle-mime
1264     "f" gnus-summary-followup
1265     "F" gnus-summary-followup-with-original
1266     "C" gnus-summary-cancel-article
1267     "r" gnus-summary-reply
1268     "R" gnus-summary-reply-with-original
1269     "\C-c\C-f" gnus-summary-mail-forward
1270     "o" gnus-summary-save-article
1271     "\C-o" gnus-summary-save-article-mail
1272     "|" gnus-summary-pipe-output
1273     "\M-k" gnus-summary-edit-local-kill
1274     "\M-K" gnus-summary-edit-global-kill
1275     ;; "V" gnus-version
1276     "\C-c\C-d" gnus-summary-describe-group
1277     "q" gnus-summary-exit
1278     "Q" gnus-summary-exit-no-update
1279     "\C-c\C-i" gnus-info-find-node
1280     gnus-mouse-2 gnus-mouse-pick-article
1281     "m" gnus-summary-mail-other-window
1282     "a" gnus-summary-post-news
1283     "x" gnus-summary-limit-to-unread
1284     "s" gnus-summary-isearch-article
1285     "t" gnus-article-hide-headers
1286     "g" gnus-summary-show-article
1287     "l" gnus-summary-goto-last-article
1288     "v" gnus-summary-preview-mime-message
1289     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1290     "\C-d" gnus-summary-enter-digest-group
1291     "\M-\C-d" gnus-summary-read-document
1292     "\M-\C-e" gnus-summary-edit-parameters
1293     "\M-\C-g" gnus-summary-customize-parameters
1294     "\C-c\C-b" gnus-bug
1295     "*" gnus-cache-enter-article
1296     "\M-*" gnus-cache-remove-article
1297     "\M-&" gnus-summary-universal-argument
1298     "\C-l" gnus-recenter
1299     "I" gnus-summary-increase-score
1300     "L" gnus-summary-lower-score
1301     "\M-i" gnus-symbolic-argument
1302     "h" gnus-summary-select-article-buffer
1303
1304     "V" gnus-summary-score-map
1305     "X" gnus-uu-extract-map
1306     "S" gnus-summary-send-map)
1307
1308   ;; Sort of orthogonal keymap
1309   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1310     "t" gnus-summary-tick-article-forward
1311     "!" gnus-summary-tick-article-forward
1312     "d" gnus-summary-mark-as-read-forward
1313     "r" gnus-summary-mark-as-read-forward
1314     "c" gnus-summary-clear-mark-forward
1315     " " gnus-summary-clear-mark-forward
1316     "e" gnus-summary-mark-as-expirable
1317     "x" gnus-summary-mark-as-expirable
1318     "?" gnus-summary-mark-as-dormant
1319     "b" gnus-summary-set-bookmark
1320     "B" gnus-summary-remove-bookmark
1321     "#" gnus-summary-mark-as-processable
1322     "\M-#" gnus-summary-unmark-as-processable
1323     "S" gnus-summary-limit-include-expunged
1324     "C" gnus-summary-catchup
1325     "H" gnus-summary-catchup-to-here
1326     "\C-c" gnus-summary-catchup-all
1327     "k" gnus-summary-kill-same-subject-and-select
1328     "K" gnus-summary-kill-same-subject
1329     "P" gnus-uu-mark-map)
1330
1331   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1332     "c" gnus-summary-clear-above
1333     "u" gnus-summary-tick-above
1334     "m" gnus-summary-mark-above
1335     "k" gnus-summary-kill-below)
1336
1337   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1338     "/" gnus-summary-limit-to-subject
1339     "n" gnus-summary-limit-to-articles
1340     "w" gnus-summary-pop-limit
1341     "s" gnus-summary-limit-to-subject
1342     "a" gnus-summary-limit-to-author
1343     "u" gnus-summary-limit-to-unread
1344     "m" gnus-summary-limit-to-marks
1345     "M" gnus-summary-limit-exclude-marks
1346     "v" gnus-summary-limit-to-score
1347     "*" gnus-summary-limit-include-cached
1348     "D" gnus-summary-limit-include-dormant
1349     "T" gnus-summary-limit-include-thread
1350     "d" gnus-summary-limit-exclude-dormant
1351     "t" gnus-summary-limit-to-age
1352     "E" gnus-summary-limit-include-expunged
1353     "c" gnus-summary-limit-exclude-childless-dormant
1354     "C" gnus-summary-limit-mark-excluded-as-read)
1355
1356   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1357     "n" gnus-summary-next-unread-article
1358     "p" gnus-summary-prev-unread-article
1359     "N" gnus-summary-next-article
1360     "P" gnus-summary-prev-article
1361     "\C-n" gnus-summary-next-same-subject
1362     "\C-p" gnus-summary-prev-same-subject
1363     "\M-n" gnus-summary-next-unread-subject
1364     "\M-p" gnus-summary-prev-unread-subject
1365     "f" gnus-summary-first-unread-article
1366     "b" gnus-summary-best-unread-article
1367     "j" gnus-summary-goto-article
1368     "g" gnus-summary-goto-subject
1369     "l" gnus-summary-goto-last-article
1370     "o" gnus-summary-pop-article)
1371
1372   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1373     "k" gnus-summary-kill-thread
1374     "l" gnus-summary-lower-thread
1375     "i" gnus-summary-raise-thread
1376     "T" gnus-summary-toggle-threads
1377     "t" gnus-summary-rethread-current
1378     "^" gnus-summary-reparent-thread
1379     "s" gnus-summary-show-thread
1380     "S" gnus-summary-show-all-threads
1381     "h" gnus-summary-hide-thread
1382     "H" gnus-summary-hide-all-threads
1383     "n" gnus-summary-next-thread
1384     "p" gnus-summary-prev-thread
1385     "u" gnus-summary-up-thread
1386     "o" gnus-summary-top-thread
1387     "d" gnus-summary-down-thread
1388     "#" gnus-uu-mark-thread
1389     "\M-#" gnus-uu-unmark-thread)
1390
1391   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1392     "g" gnus-summary-prepare
1393     "c" gnus-summary-insert-cached-articles)
1394
1395   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1396     "c" gnus-summary-catchup-and-exit
1397     "C" gnus-summary-catchup-all-and-exit
1398     "E" gnus-summary-exit-no-update
1399     "Q" gnus-summary-exit
1400     "Z" gnus-summary-exit
1401     "n" gnus-summary-catchup-and-goto-next-group
1402     "R" gnus-summary-reselect-current-group
1403     "G" gnus-summary-rescan-group
1404     "N" gnus-summary-next-group
1405     "s" gnus-summary-save-newsrc
1406     "P" gnus-summary-prev-group)
1407
1408   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1409     " " gnus-summary-next-page
1410     "n" gnus-summary-next-page
1411     "\177" gnus-summary-prev-page
1412     [delete] gnus-summary-prev-page
1413     "p" gnus-summary-prev-page
1414     "\r" gnus-summary-scroll-up
1415     "\M-\r" gnus-summary-scroll-down
1416     "<" gnus-summary-beginning-of-article
1417     ">" gnus-summary-end-of-article
1418     "b" gnus-summary-beginning-of-article
1419     "e" gnus-summary-end-of-article
1420     "^" gnus-summary-refer-parent-article
1421     "r" gnus-summary-refer-parent-article
1422     "R" gnus-summary-refer-references
1423     "T" gnus-summary-refer-thread
1424     "g" gnus-summary-show-article
1425     "s" gnus-summary-isearch-article
1426     "P" gnus-summary-print-article)
1427
1428   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1429     "b" gnus-article-add-buttons
1430     "B" gnus-article-add-buttons-to-head
1431     "o" gnus-article-treat-overstrike
1432     "e" gnus-article-emphasize
1433     "w" gnus-article-fill-cited-article
1434     "Q" gnus-article-fill-long-lines
1435     "C" gnus-article-capitalize-sentences
1436     "c" gnus-article-remove-cr
1437     "f" gnus-article-display-x-face
1438     "l" gnus-summary-stop-page-breaking
1439     "r" gnus-summary-caesar-message
1440     "t" gnus-article-hide-headers
1441     "v" gnus-summary-verbose-headers
1442     "m" gnus-summary-toggle-mime
1443     "h" gnus-article-treat-html
1444     "d" gnus-article-treat-dumbquotes)
1445
1446   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1447     "a" gnus-article-hide
1448     "h" gnus-article-hide-headers
1449     "b" gnus-article-hide-boring-headers
1450     "s" gnus-article-hide-signature
1451     "c" gnus-article-hide-citation
1452     "C" gnus-article-hide-citation-in-followups
1453     "p" gnus-article-hide-pgp
1454     "B" gnus-article-strip-banner
1455     "P" gnus-article-hide-pem
1456     "\C-c" gnus-article-hide-citation-maybe)
1457
1458   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1459     "a" gnus-article-highlight
1460     "h" gnus-article-highlight-headers
1461     "c" gnus-article-highlight-citation
1462     "s" gnus-article-highlight-signature)
1463
1464   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1465     "z" gnus-article-date-ut
1466     "u" gnus-article-date-ut
1467     "l" gnus-article-date-local
1468     "e" gnus-article-date-lapsed
1469     "o" gnus-article-date-original
1470     "i" gnus-article-date-iso8601
1471     "s" gnus-article-date-user)
1472
1473   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1474     "t" gnus-article-remove-trailing-blank-lines
1475     "l" gnus-article-strip-leading-blank-lines
1476     "m" gnus-article-strip-multiple-blank-lines
1477     "a" gnus-article-strip-blank-lines
1478     "A" gnus-article-strip-all-blank-lines
1479     "s" gnus-article-strip-leading-space
1480     "e" gnus-article-strip-trailing-space)
1481
1482   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1483     "v" gnus-version
1484     "f" gnus-summary-fetch-faq
1485     "d" gnus-summary-describe-group
1486     "h" gnus-summary-describe-briefly
1487     "i" gnus-info-find-node)
1488
1489   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1490     "e" gnus-summary-expire-articles
1491     "\M-\C-e" gnus-summary-expire-articles-now
1492     "\177" gnus-summary-delete-article
1493     [delete] gnus-summary-delete-article
1494     "m" gnus-summary-move-article
1495     "r" gnus-summary-respool-article
1496     "w" gnus-summary-edit-article
1497     "c" gnus-summary-copy-article
1498     "B" gnus-summary-crosspost-article
1499     "q" gnus-summary-respool-query
1500     "t" gnus-summary-respool-trace
1501     "i" gnus-summary-import-article
1502     "p" gnus-summary-article-posted-p)
1503
1504   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1505     "o" gnus-summary-save-article
1506     "m" gnus-summary-save-article-mail
1507     "F" gnus-summary-write-article-file
1508     "r" gnus-summary-save-article-rmail
1509     "f" gnus-summary-save-article-file
1510     "b" gnus-summary-save-article-body-file
1511     "h" gnus-summary-save-article-folder
1512     "v" gnus-summary-save-article-vm
1513     "p" gnus-summary-pipe-output
1514     "s" gnus-soup-add-article)
1515
1516   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1517     "b" gnus-summary-display-buttonized
1518     "m" gnus-summary-repair-multipart
1519     "v" gnus-article-view-part
1520     "o" gnus-article-save-part
1521     "c" gnus-article-copy-part
1522     "e" gnus-article-externalize-part
1523     "i" gnus-article-inline-part
1524     "|" gnus-article-pipe-part)
1525   )
1526
1527 (defun gnus-summary-make-menu-bar ()
1528   (gnus-turn-off-edit-menu 'summary)
1529
1530   (unless (boundp 'gnus-summary-misc-menu)
1531
1532     (easy-menu-define
1533      gnus-summary-kill-menu gnus-summary-mode-map ""
1534      (cons
1535       "Score"
1536       (nconc
1537        (list
1538         ["Enter score..." gnus-summary-score-entry t]
1539         ["Customize" gnus-score-customize t])
1540        (gnus-make-score-map 'increase)
1541        (gnus-make-score-map 'lower)
1542        '(("Mark"
1543           ["Kill below" gnus-summary-kill-below t]
1544           ["Mark above" gnus-summary-mark-above t]
1545           ["Tick above" gnus-summary-tick-above t]
1546           ["Clear above" gnus-summary-clear-above t])
1547          ["Current score" gnus-summary-current-score t]
1548          ["Set score" gnus-summary-set-score t]
1549          ["Switch current score file..." gnus-score-change-score-file t]
1550          ["Set mark below..." gnus-score-set-mark-below t]
1551          ["Set expunge below..." gnus-score-set-expunge-below t]
1552          ["Edit current score file" gnus-score-edit-current-scores t]
1553          ["Edit score file" gnus-score-edit-file t]
1554          ["Trace score" gnus-score-find-trace t]
1555          ["Find words" gnus-score-find-favourite-words t]
1556          ["Rescore buffer" gnus-summary-rescore t]
1557          ["Increase score..." gnus-summary-increase-score t]
1558          ["Lower score..." gnus-summary-lower-score t]))))
1559
1560     ;; Define both the Article menu in the summary buffer and the equivalent
1561     ;; Commands menu in the article buffer here for consistency.
1562     (let ((innards
1563            '(("Hide"
1564               ["All" gnus-article-hide t]
1565               ["Headers" gnus-article-hide-headers t]
1566               ["Signature" gnus-article-hide-signature t]
1567               ["Citation" gnus-article-hide-citation t]
1568               ["PGP" gnus-article-hide-pgp t]
1569               ["Banner" gnus-article-strip-banner t]
1570               ["Boring headers" gnus-article-hide-boring-headers t])
1571              ("Highlight"
1572               ["All" gnus-article-highlight t]
1573               ["Headers" gnus-article-highlight-headers t]
1574               ["Signature" gnus-article-highlight-signature t]
1575               ["Citation" gnus-article-highlight-citation t])
1576              ("Date"
1577               ["Local" gnus-article-date-local t]
1578               ["ISO8601" gnus-article-date-iso8601 t]
1579               ["UT" gnus-article-date-ut t]
1580               ["Original" gnus-article-date-original t]
1581               ["Lapsed" gnus-article-date-lapsed t]
1582               ["User-defined" gnus-article-date-user t])
1583              ("Washing"
1584               ("Remove Blanks"
1585                ["Leading" gnus-article-strip-leading-blank-lines t]
1586                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1587                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1588                ["All of the above" gnus-article-strip-blank-lines t]
1589                ["All" gnus-article-strip-all-blank-lines t]
1590                ["Leading space" gnus-article-strip-leading-space t]
1591                ["Trailing space" gnus-article-strip-trailing-space t])
1592               ["Overstrike" gnus-article-treat-overstrike t]
1593               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1594               ["Emphasis" gnus-article-emphasize t]
1595               ["Word wrap" gnus-article-fill-cited-article t]
1596               ["Fill long lines" gnus-article-fill-long-lines t]
1597               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1598               ["CR" gnus-article-remove-cr t]
1599               ["Show X-Face" gnus-article-display-x-face t]
1600               ["UnHTMLize" gnus-article-treat-html t]
1601               ["Rot 13" gnus-summary-caesar-message t]
1602               ["Unix pipe" gnus-summary-pipe-message t]
1603               ["Add buttons" gnus-article-add-buttons t]
1604               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1605               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1606               ["Toggle MIME" gnus-summary-toggle-mime t]
1607               ["Verbose header" gnus-summary-verbose-headers t]
1608               ["Toggle header" gnus-summary-toggle-header t])
1609              ("Output"
1610               ["Save in default format" gnus-summary-save-article t]
1611               ["Save in file" gnus-summary-save-article-file t]
1612               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1613               ["Save in MH folder" gnus-summary-save-article-folder t]
1614               ["Save in VM folder" gnus-summary-save-article-vm t]
1615               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1616               ["Save body in file" gnus-summary-save-article-body-file t]
1617               ["Pipe through a filter" gnus-summary-pipe-output t]
1618               ["Add to SOUP packet" gnus-soup-add-article t]
1619               ["Print" gnus-summary-print-article t])
1620              ("Backend"
1621               ["Respool article..." gnus-summary-respool-article t]
1622               ["Move article..." gnus-summary-move-article
1623                (gnus-check-backend-function
1624                 'request-move-article gnus-newsgroup-name)]
1625               ["Copy article..." gnus-summary-copy-article t]
1626               ["Crosspost article..." gnus-summary-crosspost-article
1627                (gnus-check-backend-function
1628                 'request-replace-article gnus-newsgroup-name)]
1629               ["Import file..." gnus-summary-import-article t]
1630               ["Check if posted" gnus-summary-article-posted-p t]
1631               ["Edit article" gnus-summary-edit-article
1632                (not (gnus-group-read-only-p))]
1633               ["Delete article" gnus-summary-delete-article
1634                (gnus-check-backend-function
1635                 'request-expire-articles gnus-newsgroup-name)]
1636               ["Query respool" gnus-summary-respool-query t]
1637               ["Trace respool" gnus-summary-respool-trace t]
1638               ["Delete expirable articles" gnus-summary-expire-articles-now
1639                (gnus-check-backend-function
1640                 'request-expire-articles gnus-newsgroup-name)])
1641              ("Extract"
1642               ["Uudecode" gnus-uu-decode-uu t]
1643               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1644               ["Unshar" gnus-uu-decode-unshar t]
1645               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1646               ["Save" gnus-uu-decode-save t]
1647               ["Binhex" gnus-uu-decode-binhex t]
1648               ["Postscript" gnus-uu-decode-postscript t])
1649              ("Cache"
1650               ["Enter article" gnus-cache-enter-article t]
1651               ["Remove article" gnus-cache-remove-article t])
1652              ["Select article buffer" gnus-summary-select-article-buffer t]
1653              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1654              ["Isearch article..." gnus-summary-isearch-article t]
1655              ["Beginning of the article" gnus-summary-beginning-of-article t]
1656              ["End of the article" gnus-summary-end-of-article t]
1657              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1658              ["Fetch referenced articles" gnus-summary-refer-references t]
1659              ["Fetch current thread" gnus-summary-refer-thread t]
1660              ["Fetch article with id..." gnus-summary-refer-article t]
1661              ["Redisplay" gnus-summary-show-article t])))
1662       (easy-menu-define
1663        gnus-summary-article-menu gnus-summary-mode-map ""
1664        (cons "Article" innards))
1665
1666       (easy-menu-define
1667        gnus-article-commands-menu gnus-article-mode-map ""
1668        (cons "Commands" innards)))
1669
1670     (easy-menu-define
1671      gnus-summary-thread-menu gnus-summary-mode-map ""
1672      '("Threads"
1673        ["Toggle threading" gnus-summary-toggle-threads t]
1674        ["Hide threads" gnus-summary-hide-all-threads t]
1675        ["Show threads" gnus-summary-show-all-threads t]
1676        ["Hide thread" gnus-summary-hide-thread t]
1677        ["Show thread" gnus-summary-show-thread t]
1678        ["Go to next thread" gnus-summary-next-thread t]
1679        ["Go to previous thread" gnus-summary-prev-thread t]
1680        ["Go down thread" gnus-summary-down-thread t]
1681        ["Go up thread" gnus-summary-up-thread t]
1682        ["Top of thread" gnus-summary-top-thread t]
1683        ["Mark thread as read" gnus-summary-kill-thread t]
1684        ["Lower thread score" gnus-summary-lower-thread t]
1685        ["Raise thread score" gnus-summary-raise-thread t]
1686        ["Rethread current" gnus-summary-rethread-current t]
1687        ))
1688
1689     (easy-menu-define
1690      gnus-summary-post-menu gnus-summary-mode-map ""
1691      '("Post"
1692        ["Post an article" gnus-summary-post-news t]
1693        ["Followup" gnus-summary-followup t]
1694        ["Followup and yank" gnus-summary-followup-with-original t]
1695        ["Supersede article" gnus-summary-supersede-article t]
1696        ["Cancel article" gnus-summary-cancel-article t]
1697        ["Reply" gnus-summary-reply t]
1698        ["Reply and yank" gnus-summary-reply-with-original t]
1699        ["Wide reply" gnus-summary-wide-reply t]
1700        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1701        ["Mail forward" gnus-summary-mail-forward t]
1702        ["Post forward" gnus-summary-post-forward t]
1703        ["Digest and mail" gnus-summary-mail-digest t]
1704        ["Digest and post" gnus-summary-post-digest t]
1705        ["Resend message" gnus-summary-resend-message t]
1706        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1707        ["Send a mail" gnus-summary-mail-other-window t]
1708        ["Uuencode and post" gnus-uu-post-news t]
1709        ["Followup via news" gnus-summary-followup-to-mail t]
1710        ["Followup via news and yank"
1711         gnus-summary-followup-to-mail-with-original t]
1712        ;;("Draft"
1713        ;;["Send" gnus-summary-send-draft t]
1714        ;;["Send bounced" gnus-resend-bounced-mail t])
1715        ))
1716
1717     (easy-menu-define
1718      gnus-summary-misc-menu gnus-summary-mode-map ""
1719      '("Misc"
1720        ("Mark Read"
1721         ["Mark as read" gnus-summary-mark-as-read-forward t]
1722         ["Mark same subject and select"
1723          gnus-summary-kill-same-subject-and-select t]
1724         ["Mark same subject" gnus-summary-kill-same-subject t]
1725         ["Catchup" gnus-summary-catchup t]
1726         ["Catchup all" gnus-summary-catchup-all t]
1727         ["Catchup to here" gnus-summary-catchup-to-here t]
1728         ["Catchup region" gnus-summary-mark-region-as-read t]
1729         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1730        ("Mark Various"
1731         ["Tick" gnus-summary-tick-article-forward t]
1732         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1733         ["Remove marks" gnus-summary-clear-mark-forward t]
1734         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1735         ["Set bookmark" gnus-summary-set-bookmark t]
1736         ["Remove bookmark" gnus-summary-remove-bookmark t])
1737        ("Mark Limit"
1738         ["Marks..." gnus-summary-limit-to-marks t]
1739         ["Subject..." gnus-summary-limit-to-subject t]
1740         ["Author..." gnus-summary-limit-to-author t]
1741         ["Age..." gnus-summary-limit-to-age t]
1742         ["Score" gnus-summary-limit-to-score t]
1743         ["Unread" gnus-summary-limit-to-unread t]
1744         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1745         ["Articles" gnus-summary-limit-to-articles t]
1746         ["Pop limit" gnus-summary-pop-limit t]
1747         ["Show dormant" gnus-summary-limit-include-dormant t]
1748         ["Hide childless dormant"
1749          gnus-summary-limit-exclude-childless-dormant t]
1750         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1751         ["Hide marked" gnus-summary-limit-exclude-marks t]
1752         ["Show expunged" gnus-summary-show-all-expunged t])
1753        ("Process Mark"
1754         ["Set mark" gnus-summary-mark-as-processable t]
1755         ["Remove mark" gnus-summary-unmark-as-processable t]
1756         ["Remove all marks" gnus-summary-unmark-all-processable t]
1757         ["Mark above" gnus-uu-mark-over t]
1758         ["Mark series" gnus-uu-mark-series t]
1759         ["Mark region" gnus-uu-mark-region t]
1760         ["Unmark region" gnus-uu-unmark-region t]
1761         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1762         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1763         ["Mark all" gnus-uu-mark-all t]
1764         ["Mark buffer" gnus-uu-mark-buffer t]
1765         ["Mark sparse" gnus-uu-mark-sparse t]
1766         ["Mark thread" gnus-uu-mark-thread t]
1767         ["Unmark thread" gnus-uu-unmark-thread t]
1768         ("Process Mark Sets"
1769          ["Kill" gnus-summary-kill-process-mark t]
1770          ["Yank" gnus-summary-yank-process-mark
1771           gnus-newsgroup-process-stack]
1772          ["Save" gnus-summary-save-process-mark t]))
1773        ("Scroll article"
1774         ["Page forward" gnus-summary-next-page t]
1775         ["Page backward" gnus-summary-prev-page t]
1776         ["Line forward" gnus-summary-scroll-up t])
1777        ("Move"
1778         ["Next unread article" gnus-summary-next-unread-article t]
1779         ["Previous unread article" gnus-summary-prev-unread-article t]
1780         ["Next article" gnus-summary-next-article t]
1781         ["Previous article" gnus-summary-prev-article t]
1782         ["Next unread subject" gnus-summary-next-unread-subject t]
1783         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1784         ["Next article same subject" gnus-summary-next-same-subject t]
1785         ["Previous article same subject" gnus-summary-prev-same-subject t]
1786         ["First unread article" gnus-summary-first-unread-article t]
1787         ["Best unread article" gnus-summary-best-unread-article t]
1788         ["Go to subject number..." gnus-summary-goto-subject t]
1789         ["Go to article number..." gnus-summary-goto-article t]
1790         ["Go to the last article" gnus-summary-goto-last-article t]
1791         ["Pop article off history" gnus-summary-pop-article t])
1792        ("Sort"
1793         ["Sort by number" gnus-summary-sort-by-number t]
1794         ["Sort by author" gnus-summary-sort-by-author t]
1795         ["Sort by subject" gnus-summary-sort-by-subject t]
1796         ["Sort by date" gnus-summary-sort-by-date t]
1797         ["Sort by score" gnus-summary-sort-by-score t]
1798         ["Sort by lines" gnus-summary-sort-by-lines t]
1799         ["Sort by characters" gnus-summary-sort-by-chars t])
1800        ("Help"
1801         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1802         ["Describe group" gnus-summary-describe-group t]
1803         ["Read manual" gnus-info-find-node t])
1804        ("Modes"
1805         ["Pick and read" gnus-pick-mode t]
1806         ["Binary" gnus-binary-mode t])
1807        ("Regeneration"
1808         ["Regenerate" gnus-summary-prepare t]
1809         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1810         ["Toggle threading" gnus-summary-toggle-threads t])
1811        ["Filter articles..." gnus-summary-execute-command t]
1812        ["Run command on subjects..." gnus-summary-universal-argument t]
1813        ["Search articles forward..." gnus-summary-search-article-forward t]
1814        ["Search articles backward..." gnus-summary-search-article-backward t]
1815        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1816        ["Expand window" gnus-summary-expand-window t]
1817        ["Expire expirable articles" gnus-summary-expire-articles
1818         (gnus-check-backend-function
1819          'request-expire-articles gnus-newsgroup-name)]
1820        ["Edit local kill file" gnus-summary-edit-local-kill t]
1821        ["Edit main kill file" gnus-summary-edit-global-kill t]
1822        ["Edit group parameters" gnus-summary-edit-parameters t]
1823        ["Customize group parameters" gnus-summary-customize-parameters t]
1824        ["Send a bug report" gnus-bug t]
1825        ("Exit"
1826         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1827         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1828         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1829         ["Exit group" gnus-summary-exit t]
1830         ["Exit group without updating" gnus-summary-exit-no-update t]
1831         ["Exit and goto next group" gnus-summary-next-group t]
1832         ["Exit and goto prev group" gnus-summary-prev-group t]
1833         ["Reselect group" gnus-summary-reselect-current-group t]
1834         ["Rescan group" gnus-summary-rescan-group t]
1835         ["Update dribble" gnus-summary-save-newsrc t])))
1836
1837     (gnus-run-hooks 'gnus-summary-menu-hook)))
1838
1839 (defun gnus-score-set-default (var value)
1840   "A version of set that updates the GNU Emacs menu-bar."
1841   (set var value)
1842   ;; It is the message that forces the active status to be updated.
1843   (message ""))
1844
1845 (defun gnus-make-score-map (type)
1846   "Make a summary score map of type TYPE."
1847   (if t
1848       nil
1849     (let ((headers '(("author" "from" string)
1850                      ("subject" "subject" string)
1851                      ("article body" "body" string)
1852                      ("article head" "head" string)
1853                      ("xref" "xref" string)
1854                      ("extra header" "extra" string)
1855                      ("lines" "lines" number)
1856                      ("followups to author" "followup" string)))
1857           (types '((number ("less than" <)
1858                            ("greater than" >)
1859                            ("equal" =))
1860                    (string ("substring" s)
1861                            ("exact string" e)
1862                            ("fuzzy string" f)
1863                            ("regexp" r))))
1864           (perms '(("temporary" (current-time-string))
1865                    ("permanent" nil)
1866                    ("immediate" now)))
1867           header)
1868       (list
1869        (apply
1870         'nconc
1871         (list
1872          (if (eq type 'lower)
1873              "Lower score"
1874            "Increase score"))
1875         (let (outh)
1876           (while headers
1877             (setq header (car headers))
1878             (setq outh
1879                   (cons
1880                    (apply
1881                     'nconc
1882                     (list (car header))
1883                     (let ((ts (cdr (assoc (nth 2 header) types)))
1884                           outt)
1885                       (while ts
1886                         (setq outt
1887                               (cons
1888                                (apply
1889                                 'nconc
1890                                 (list (caar ts))
1891                                 (let ((ps perms)
1892                                       outp)
1893                                   (while ps
1894                                     (setq outp
1895                                           (cons
1896                                            (vector
1897                                             (caar ps)
1898                                             (list
1899                                              'gnus-summary-score-entry
1900                                              (nth 1 header)
1901                                              (if (or (string= (nth 1 header)
1902                                                               "head")
1903                                                      (string= (nth 1 header)
1904                                                               "body"))
1905                                                  ""
1906                                                (list 'gnus-summary-header
1907                                                      (nth 1 header)))
1908                                              (list 'quote (nth 1 (car ts)))
1909                                              (list 'gnus-score-default nil)
1910                                              (nth 1 (car ps))
1911                                              t)
1912                                             t)
1913                                            outp))
1914                                     (setq ps (cdr ps)))
1915                                   (list (nreverse outp))))
1916                                outt))
1917                         (setq ts (cdr ts)))
1918                       (list (nreverse outt))))
1919                    outh))
1920             (setq headers (cdr headers)))
1921           (list (nreverse outh))))))))
1922
1923 \f
1924
1925 (defun gnus-summary-mode (&optional group)
1926   "Major mode for reading articles.
1927
1928 All normal editing commands are switched off.
1929 \\<gnus-summary-mode-map>
1930 Each line in this buffer represents one article.  To read an
1931 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1932 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1933 respectively.
1934
1935 You can also post articles and send mail from this buffer.  To
1936 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1937 of an article, type `\\[gnus-summary-reply]'.
1938
1939 There are approx. one gazillion commands you can execute in this
1940 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1941
1942 The following commands are available:
1943
1944 \\{gnus-summary-mode-map}"
1945   (interactive)
1946   (when (gnus-visual-p 'summary-menu 'menu)
1947     (gnus-summary-make-menu-bar))
1948   (kill-all-local-variables)
1949   (gnus-summary-make-local-variables)
1950   (gnus-make-thread-indent-array)
1951   (gnus-simplify-mode-line)
1952   (setq major-mode 'gnus-summary-mode)
1953   (setq mode-name "Summary")
1954   (make-local-variable 'minor-mode-alist)
1955   (use-local-map gnus-summary-mode-map)
1956   (buffer-disable-undo)
1957   (setq buffer-read-only t)             ;Disable modification
1958   (setq truncate-lines t)
1959   (setq selective-display t)
1960   (setq selective-display-ellipses t)   ;Display `...'
1961   (gnus-summary-set-display-table)
1962   (gnus-set-default-directory)
1963   (setq gnus-newsgroup-name group)
1964   (make-local-variable 'gnus-summary-line-format)
1965   (make-local-variable 'gnus-summary-line-format-spec)
1966   (make-local-variable 'gnus-summary-dummy-line-format)
1967   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1968   (make-local-variable 'gnus-summary-mark-positions)
1969   (make-local-hook 'pre-command-hook)
1970   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1971   (gnus-run-hooks 'gnus-summary-mode-hook)
1972   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1973   (gnus-update-summary-mark-positions))
1974
1975 (defun gnus-summary-make-local-variables ()
1976   "Make all the local summary buffer variables."
1977   (let (global)
1978     (dolist (local gnus-summary-local-variables)
1979       (if (consp local)
1980           (progn
1981             (if (eq (cdr local) 'global)
1982                 ;; Copy the global value of the variable.
1983                 (setq global (symbol-value (car local)))
1984               ;; Use the value from the list.
1985               (setq global (eval (cdr local))))
1986             (set (make-local-variable (car local)) global))
1987         ;; Simple nil-valued local variable.
1988         (set (make-local-variable local) nil)))))
1989
1990 (defun gnus-summary-clear-local-variables ()
1991   (let ((locals gnus-summary-local-variables))
1992     (while locals
1993       (if (consp (car locals))
1994           (and (vectorp (caar locals))
1995                (set (caar locals) nil))
1996         (and (vectorp (car locals))
1997              (set (car locals) nil)))
1998       (setq locals (cdr locals)))))
1999
2000 ;; Summary data functions.
2001
2002 (defmacro gnus-data-number (data)
2003   `(car ,data))
2004
2005 (defmacro gnus-data-set-number (data number)
2006   `(setcar ,data ,number))
2007
2008 (defmacro gnus-data-mark (data)
2009   `(nth 1 ,data))
2010
2011 (defmacro gnus-data-set-mark (data mark)
2012   `(setcar (nthcdr 1 ,data) ,mark))
2013
2014 (defmacro gnus-data-pos (data)
2015   `(nth 2 ,data))
2016
2017 (defmacro gnus-data-set-pos (data pos)
2018   `(setcar (nthcdr 2 ,data) ,pos))
2019
2020 (defmacro gnus-data-header (data)
2021   `(nth 3 ,data))
2022
2023 (defmacro gnus-data-set-header (data header)
2024   `(setf (nth 3 ,data) ,header))
2025
2026 (defmacro gnus-data-level (data)
2027   `(nth 4 ,data))
2028
2029 (defmacro gnus-data-unread-p (data)
2030   `(= (nth 1 ,data) gnus-unread-mark))
2031
2032 (defmacro gnus-data-read-p (data)
2033   `(/= (nth 1 ,data) gnus-unread-mark))
2034
2035 (defmacro gnus-data-pseudo-p (data)
2036   `(consp (nth 3 ,data)))
2037
2038 (defmacro gnus-data-find (number)
2039   `(assq ,number gnus-newsgroup-data))
2040
2041 (defmacro gnus-data-find-list (number &optional data)
2042   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2043      (memq (assq ,number bdata)
2044            bdata)))
2045
2046 (defmacro gnus-data-make (number mark pos header level)
2047   `(list ,number ,mark ,pos ,header ,level))
2048
2049 (defun gnus-data-enter (after-article number mark pos header level offset)
2050   (let ((data (gnus-data-find-list after-article)))
2051     (unless data
2052       (error "No such article: %d" after-article))
2053     (setcdr data (cons (gnus-data-make number mark pos header level)
2054                        (cdr data)))
2055     (setq gnus-newsgroup-data-reverse nil)
2056     (gnus-data-update-list (cddr data) offset)))
2057
2058 (defun gnus-data-enter-list (after-article list &optional offset)
2059   (when list
2060     (let ((data (and after-article (gnus-data-find-list after-article)))
2061           (ilist list))
2062       (if (not (or data
2063                    after-article))
2064           (let ((odata gnus-newsgroup-data))
2065             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2066             (when offset
2067               (gnus-data-update-list odata offset)))
2068         ;; Find the last element in the list to be spliced into the main
2069         ;; list.
2070         (while (cdr list)
2071           (setq list (cdr list)))
2072         (if (not data)
2073             (progn
2074               (setcdr list gnus-newsgroup-data)
2075               (setq gnus-newsgroup-data ilist)
2076               (when offset
2077                 (gnus-data-update-list (cdr list) offset)))
2078           (setcdr list (cdr data))
2079           (setcdr data ilist)
2080           (when offset
2081             (gnus-data-update-list (cdr list) offset))))
2082       (setq gnus-newsgroup-data-reverse nil))))
2083
2084 (defun gnus-data-remove (article &optional offset)
2085   (let ((data gnus-newsgroup-data))
2086     (if (= (gnus-data-number (car data)) article)
2087         (progn
2088           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2089                 gnus-newsgroup-data-reverse nil)
2090           (when offset
2091             (gnus-data-update-list gnus-newsgroup-data offset)))
2092       (while (cdr data)
2093         (when (= (gnus-data-number (cadr data)) article)
2094           (setcdr data (cddr data))
2095           (when offset
2096             (gnus-data-update-list (cdr data) offset))
2097           (setq data nil
2098                 gnus-newsgroup-data-reverse nil))
2099         (setq data (cdr data))))))
2100
2101 (defmacro gnus-data-list (backward)
2102   `(if ,backward
2103        (or gnus-newsgroup-data-reverse
2104            (setq gnus-newsgroup-data-reverse
2105                  (reverse gnus-newsgroup-data)))
2106      gnus-newsgroup-data))
2107
2108 (defun gnus-data-update-list (data offset)
2109   "Add OFFSET to the POS of all data entries in DATA."
2110   (setq gnus-newsgroup-data-reverse nil)
2111   (while data
2112     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2113     (setq data (cdr data))))
2114
2115 (defun gnus-summary-article-pseudo-p (article)
2116   "Say whether this article is a pseudo article or not."
2117   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2118
2119 (defmacro gnus-summary-article-sparse-p (article)
2120   "Say whether this article is a sparse article or not."
2121   `(memq ,article gnus-newsgroup-sparse))
2122
2123 (defmacro gnus-summary-article-ancient-p (article)
2124   "Say whether this article is a sparse article or not."
2125   `(memq ,article gnus-newsgroup-ancient))
2126
2127 (defun gnus-article-parent-p (number)
2128   "Say whether this article is a parent or not."
2129   (let ((data (gnus-data-find-list number)))
2130     (and (cdr data)                     ; There has to be an article after...
2131          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2132             (gnus-data-level (nth 1 data))))))
2133
2134 (defun gnus-article-children (number)
2135   "Return a list of all children to NUMBER."
2136   (let* ((data (gnus-data-find-list number))
2137          (level (gnus-data-level (car data)))
2138          children)
2139     (setq data (cdr data))
2140     (while (and data
2141                 (= (gnus-data-level (car data)) (1+ level)))
2142       (push (gnus-data-number (car data)) children)
2143       (setq data (cdr data)))
2144     children))
2145
2146 (defmacro gnus-summary-skip-intangible ()
2147   "If the current article is intangible, then jump to a different article."
2148   '(let ((to (get-text-property (point) 'gnus-intangible)))
2149      (and to (gnus-summary-goto-subject to))))
2150
2151 (defmacro gnus-summary-article-intangible-p ()
2152   "Say whether this article is intangible or not."
2153   '(get-text-property (point) 'gnus-intangible))
2154
2155 (defun gnus-article-read-p (article)
2156   "Say whether ARTICLE is read or not."
2157   (not (or (memq article gnus-newsgroup-marked)
2158            (memq article gnus-newsgroup-unreads)
2159            (memq article gnus-newsgroup-unselected)
2160            (memq article gnus-newsgroup-dormant))))
2161
2162 ;; Some summary mode macros.
2163
2164 (defmacro gnus-summary-article-number ()
2165   "The article number of the article on the current line.
2166 If there isn's an article number here, then we return the current
2167 article number."
2168   '(progn
2169      (gnus-summary-skip-intangible)
2170      (or (get-text-property (point) 'gnus-number)
2171          (gnus-summary-last-subject))))
2172
2173 (defmacro gnus-summary-article-header (&optional number)
2174   "Return the header of article NUMBER."
2175   `(gnus-data-header (gnus-data-find
2176                       ,(or number '(gnus-summary-article-number)))))
2177
2178 (defmacro gnus-summary-thread-level (&optional number)
2179   "Return the level of thread that starts with article NUMBER."
2180   `(if (and (eq gnus-summary-make-false-root 'dummy)
2181             (get-text-property (point) 'gnus-intangible))
2182        0
2183      (gnus-data-level (gnus-data-find
2184                        ,(or number '(gnus-summary-article-number))))))
2185
2186 (defmacro gnus-summary-article-mark (&optional number)
2187   "Return the mark of article NUMBER."
2188   `(gnus-data-mark (gnus-data-find
2189                     ,(or number '(gnus-summary-article-number)))))
2190
2191 (defmacro gnus-summary-article-pos (&optional number)
2192   "Return the position of the line of article NUMBER."
2193   `(gnus-data-pos (gnus-data-find
2194                    ,(or number '(gnus-summary-article-number)))))
2195
2196 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2197 (defmacro gnus-summary-article-subject (&optional number)
2198   "Return current subject string or nil if nothing."
2199   `(let ((headers
2200           ,(if number
2201                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2202              '(gnus-data-header (assq (gnus-summary-article-number)
2203                                       gnus-newsgroup-data)))))
2204      (and headers
2205           (vectorp headers)
2206           (mail-header-subject headers))))
2207
2208 (defmacro gnus-summary-article-score (&optional number)
2209   "Return current article score."
2210   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2211                   gnus-newsgroup-scored))
2212        gnus-summary-default-score 0))
2213
2214 (defun gnus-summary-article-children (&optional number)
2215   "Return a list of article numbers that are children of article NUMBER."
2216   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2217          (level (gnus-data-level (car data)))
2218          l children)
2219     (while (and (setq data (cdr data))
2220                 (> (setq l (gnus-data-level (car data))) level))
2221       (and (= (1+ level) l)
2222            (push (gnus-data-number (car data))
2223                  children)))
2224     (nreverse children)))
2225
2226 (defun gnus-summary-article-parent (&optional number)
2227   "Return the article number of the parent of article NUMBER."
2228   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2229                                     (gnus-data-list t)))
2230          (level (gnus-data-level (car data))))
2231     (if (zerop level)
2232         ()                              ; This is a root.
2233       ;; We search until we find an article with a level less than
2234       ;; this one.  That function has to be the parent.
2235       (while (and (setq data (cdr data))
2236                   (not (< (gnus-data-level (car data)) level))))
2237       (and data (gnus-data-number (car data))))))
2238
2239 (defun gnus-unread-mark-p (mark)
2240   "Say whether MARK is the unread mark."
2241   (= mark gnus-unread-mark))
2242
2243 (defun gnus-read-mark-p (mark)
2244   "Say whether MARK is one of the marks that mark as read.
2245 This is all marks except unread, ticked, dormant, and expirable."
2246   (not (or (= mark gnus-unread-mark)
2247            (= mark gnus-ticked-mark)
2248            (= mark gnus-dormant-mark)
2249            (= mark gnus-expirable-mark))))
2250
2251 (defmacro gnus-article-mark (number)
2252   "Return the MARK of article NUMBER.
2253 This macro should only be used when computing the mark the \"first\"
2254 time; i.e., when generating the summary lines.  After that,
2255 `gnus-summary-article-mark' should be used to examine the
2256 marks of articles."
2257   `(cond
2258     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2259     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2260     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2261     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2262     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2263     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2264     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2265     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2266            gnus-ancient-mark))))
2267
2268 ;; Saving hidden threads.
2269
2270 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2271 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2272
2273 (defmacro gnus-save-hidden-threads (&rest forms)
2274   "Save hidden threads, eval FORMS, and restore the hidden threads."
2275   (let ((config (make-symbol "config")))
2276     `(let ((,config (gnus-hidden-threads-configuration)))
2277        (unwind-protect
2278            (save-excursion
2279              ,@forms)
2280          (gnus-restore-hidden-threads-configuration ,config)))))
2281
2282 (defun gnus-data-compute-positions ()
2283   "Compute the positions of all articles."
2284   (setq gnus-newsgroup-data-reverse nil)
2285   (let ((data gnus-newsgroup-data))
2286     (save-excursion
2287       (gnus-save-hidden-threads
2288         (gnus-summary-show-all-threads)
2289         (goto-char (point-min))
2290         (while data
2291           (while (get-text-property (point) 'gnus-intangible)
2292             (forward-line 1))
2293           (gnus-data-set-pos (car data) (+ (point) 3))
2294           (setq data (cdr data))
2295           (forward-line 1))))))
2296
2297 (defun gnus-hidden-threads-configuration ()
2298   "Return the current hidden threads configuration."
2299   (save-excursion
2300     (let (config)
2301       (goto-char (point-min))
2302       (while (search-forward "\r" nil t)
2303         (push (1- (point)) config))
2304       config)))
2305
2306 (defun gnus-restore-hidden-threads-configuration (config)
2307   "Restore hidden threads configuration from CONFIG."
2308   (let (point buffer-read-only)
2309     (while (setq point (pop config))
2310       (when (and (< point (point-max))
2311                  (goto-char point)
2312                  (eq (char-after) ?\n))
2313         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2314
2315 ;; Various summary mode internalish functions.
2316
2317 (defun gnus-mouse-pick-article (e)
2318   (interactive "e")
2319   (mouse-set-point e)
2320   (gnus-summary-next-page nil t))
2321
2322 (defun gnus-summary-set-display-table ()
2323   ;; Change the display table.  Odd characters have a tendency to mess
2324   ;; up nicely formatted displays - we make all possible glyphs
2325   ;; display only a single character.
2326
2327   ;; We start from the standard display table, if any.
2328   (let ((table (or (copy-sequence standard-display-table)
2329                    (make-display-table)))
2330         (i 32))
2331     ;; Nix out all the control chars...
2332     (while (>= (setq i (1- i)) 0)
2333       (aset table i [??]))
2334     ;; ... but not newline and cr, of course.  (cr is necessary for the
2335     ;; selective display).
2336     (aset table ?\n nil)
2337     (aset table ?\r nil)
2338     ;; We keep TAB as well.
2339     (aset table ?\t nil)
2340     ;; We nix out any glyphs over 126 that are not set already.
2341     (let ((i 256))
2342       (while (>= (setq i (1- i)) 127)
2343         ;; Only modify if the entry is nil.
2344         (unless (aref table i)
2345           (aset table i [??]))))
2346     (setq buffer-display-table table)))
2347
2348 (defun gnus-summary-setup-buffer (group)
2349   "Initialize summary buffer."
2350   (let ((buffer (concat "*Summary " group "*")))
2351     (if (get-buffer buffer)
2352         (progn
2353           (set-buffer buffer)
2354           (setq gnus-summary-buffer (current-buffer))
2355           (not gnus-newsgroup-prepared))
2356       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2357       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2358       (gnus-summary-mode group)
2359       (when gnus-carpal
2360         (gnus-carpal-setup-buffer 'summary))
2361       (unless gnus-single-article-buffer
2362         (make-local-variable 'gnus-article-buffer)
2363         (make-local-variable 'gnus-article-current)
2364         (make-local-variable 'gnus-original-article-buffer))
2365       (setq gnus-newsgroup-name group)
2366       t)))
2367
2368 (defun gnus-set-global-variables ()
2369   ;; Set the global equivalents of the summary buffer-local variables
2370   ;; to the latest values they had.  These reflect the summary buffer
2371   ;; that was in action when the last article was fetched.
2372   (when (eq major-mode 'gnus-summary-mode)
2373     (setq gnus-summary-buffer (current-buffer))
2374     (let ((name gnus-newsgroup-name)
2375           (marked gnus-newsgroup-marked)
2376           (unread gnus-newsgroup-unreads)
2377           (headers gnus-current-headers)
2378           (data gnus-newsgroup-data)
2379           (summary gnus-summary-buffer)
2380           (article-buffer gnus-article-buffer)
2381           (original gnus-original-article-buffer)
2382           (gac gnus-article-current)
2383           (reffed gnus-reffed-article-number)
2384           (score-file gnus-current-score-file)
2385           (default-charset gnus-newsgroup-charset))
2386       (save-excursion
2387         (set-buffer gnus-group-buffer)
2388         (setq gnus-newsgroup-name name
2389               gnus-newsgroup-marked marked
2390               gnus-newsgroup-unreads unread
2391               gnus-current-headers headers
2392               gnus-newsgroup-data data
2393               gnus-article-current gac
2394               gnus-summary-buffer summary
2395               gnus-article-buffer article-buffer
2396               gnus-original-article-buffer original
2397               gnus-reffed-article-number reffed
2398               gnus-current-score-file score-file
2399               gnus-newsgroup-charset default-charset)
2400         ;; The article buffer also has local variables.
2401         (when (gnus-buffer-live-p gnus-article-buffer)
2402           (set-buffer gnus-article-buffer)
2403           (setq gnus-summary-buffer summary))))))
2404
2405 (defun gnus-summary-article-unread-p (article)
2406   "Say whether ARTICLE is unread or not."
2407   (memq article gnus-newsgroup-unreads))
2408
2409 (defun gnus-summary-first-article-p (&optional article)
2410   "Return whether ARTICLE is the first article in the buffer."
2411   (if (not (setq article (or article (gnus-summary-article-number))))
2412       nil
2413     (eq article (caar gnus-newsgroup-data))))
2414
2415 (defun gnus-summary-last-article-p (&optional article)
2416   "Return whether ARTICLE is the last article in the buffer."
2417   (if (not (setq article (or article (gnus-summary-article-number))))
2418       ;; All non-existent numbers are the last article.  :-)
2419       t
2420     (not (cdr (gnus-data-find-list article)))))
2421
2422 (defun gnus-make-thread-indent-array ()
2423   (let ((n 200))
2424     (unless (and gnus-thread-indent-array
2425                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2426       (setq gnus-thread-indent-array (make-vector 201 "")
2427             gnus-thread-indent-array-level gnus-thread-indent-level)
2428       (while (>= n 0)
2429         (aset gnus-thread-indent-array n
2430               (make-string (* n gnus-thread-indent-level) ? ))
2431         (setq n (1- n))))))
2432
2433 (defun gnus-update-summary-mark-positions ()
2434   "Compute where the summary marks are to go."
2435   (save-excursion
2436     (when (gnus-buffer-exists-p gnus-summary-buffer)
2437       (set-buffer gnus-summary-buffer))
2438     (let ((gnus-replied-mark 129)
2439           (gnus-score-below-mark 130)
2440           (gnus-score-over-mark 130)
2441           (gnus-download-mark 131)
2442           (spec gnus-summary-line-format-spec)
2443           gnus-visual pos)
2444       (save-excursion
2445         (gnus-set-work-buffer)
2446         (let ((gnus-summary-line-format-spec spec)
2447               (gnus-newsgroup-downloadable '((0 . t))))
2448           (gnus-summary-insert-line
2449            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2450            0 nil 128 t nil "" nil 1)
2451           (goto-char (point-min))
2452           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2453                                              (- (point) 2)))))
2454           (goto-char (point-min))
2455           (push (cons 'replied (and (search-forward "\201" nil t)
2456                                     (- (point) 2)))
2457                 pos)
2458           (goto-char (point-min))
2459           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2460                 pos)
2461           (goto-char (point-min))
2462           (push (cons 'download
2463                       (and (search-forward "\203" nil t) (- (point) 2)))
2464                 pos)))
2465       (setq gnus-summary-mark-positions pos))))
2466
2467 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2468   "Insert a dummy root in the summary buffer."
2469   (beginning-of-line)
2470   (gnus-add-text-properties
2471    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2472    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2473
2474 (defun gnus-summary-from-or-to-or-newsgroups (header)
2475   (let ((to (cdr (assq 'To (mail-header-extra header))))
2476         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2477         (mail-parse-charset gnus-newsgroup-charset))
2478     (cond
2479      ((and to
2480            gnus-ignored-from-addresses
2481            (string-match gnus-ignored-from-addresses
2482                          (mail-header-from header)))
2483       (concat "-> "
2484               (or (car (funcall gnus-extract-address-components
2485                                 (funcall
2486                                  gnus-decode-encoded-word-function to)))
2487                   (funcall gnus-decode-encoded-word-function to))))
2488      ((and newsgroups
2489            gnus-ignored-from-addresses
2490            (string-match gnus-ignored-from-addresses
2491                          (mail-header-from header)))
2492       (concat "=> " newsgroups))
2493      (t
2494       (or (car (funcall gnus-extract-address-components
2495                         (mail-header-from header)))
2496           (mail-header-from header))))))
2497
2498 (defun gnus-summary-insert-line (gnus-tmp-header
2499                                  gnus-tmp-level gnus-tmp-current
2500                                  gnus-tmp-unread gnus-tmp-replied
2501                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2502                                  &optional gnus-tmp-dummy gnus-tmp-score
2503                                  gnus-tmp-process)
2504   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2505          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2506          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2507          (gnus-tmp-score-char
2508           (if (or (null gnus-summary-default-score)
2509                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2510                       gnus-summary-zcore-fuzz))
2511               ? ;Whitespace
2512             (if (< gnus-tmp-score gnus-summary-default-score)
2513                 gnus-score-below-mark gnus-score-over-mark)))
2514          (gnus-tmp-replied
2515           (cond (gnus-tmp-process gnus-process-mark)
2516                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2517                  gnus-cached-mark)
2518                 (gnus-tmp-replied gnus-replied-mark)
2519                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2520                  gnus-saved-mark)
2521                 (t gnus-unread-mark)))
2522          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2523          (gnus-tmp-name
2524           (cond
2525            ((string-match "<[^>]+> *$" gnus-tmp-from)
2526             (let ((beg (match-beginning 0)))
2527               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2528                        (substring gnus-tmp-from (1+ (match-beginning 0))
2529                                   (1- (match-end 0))))
2530                   (substring gnus-tmp-from 0 beg))))
2531            ((string-match "(.+)" gnus-tmp-from)
2532             (substring gnus-tmp-from
2533                        (1+ (match-beginning 0)) (1- (match-end 0))))
2534            (t gnus-tmp-from)))
2535          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2536          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2537          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2538          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2539          (buffer-read-only nil))
2540     (when (string= gnus-tmp-name "")
2541       (setq gnus-tmp-name gnus-tmp-from))
2542     (unless (numberp gnus-tmp-lines)
2543       (setq gnus-tmp-lines 0))
2544     (gnus-put-text-property-excluding-characters-with-faces
2545      (point)
2546      (progn (eval gnus-summary-line-format-spec) (point))
2547      'gnus-number gnus-tmp-number)
2548     (when (gnus-visual-p 'summary-highlight 'highlight)
2549       (forward-line -1)
2550       (gnus-run-hooks 'gnus-summary-update-hook)
2551       (forward-line 1))))
2552
2553 (defun gnus-summary-update-line (&optional dont-update)
2554   ;; Update summary line after change.
2555   (when (and gnus-summary-default-score
2556              (not gnus-summary-inhibit-highlight))
2557     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2558            (article (gnus-summary-article-number))
2559            (score (gnus-summary-article-score article)))
2560       (unless dont-update
2561         (if (and gnus-summary-mark-below
2562                  (< (gnus-summary-article-score)
2563                     gnus-summary-mark-below))
2564             ;; This article has a low score, so we mark it as read.
2565             (when (memq article gnus-newsgroup-unreads)
2566               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2567           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2568             ;; This article was previously marked as read on account
2569             ;; of a low score, but now it has risen, so we mark it as
2570             ;; unread.
2571             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2572         (gnus-summary-update-mark
2573          (if (or (null gnus-summary-default-score)
2574                  (<= (abs (- score gnus-summary-default-score))
2575                      gnus-summary-zcore-fuzz))
2576              ? ;Whitespace
2577            (if (< score gnus-summary-default-score)
2578                gnus-score-below-mark gnus-score-over-mark))
2579          'score))
2580       ;; Do visual highlighting.
2581       (when (gnus-visual-p 'summary-highlight 'highlight)
2582         (gnus-run-hooks 'gnus-summary-update-hook)))))
2583
2584 (defvar gnus-tmp-new-adopts nil)
2585
2586 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2587   "Return the number of articles in THREAD.
2588 This may be 0 in some cases -- if none of the articles in
2589 the thread are to be displayed."
2590   (let* ((number
2591           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2592           (cond
2593            ((not (listp thread))
2594             1)
2595            ((and (consp thread) (cdr thread))
2596             (apply
2597              '+ 1 (mapcar
2598                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2599            ((null thread)
2600             1)
2601            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2602             1)
2603            (t 0))))
2604     (when (and level (zerop level) gnus-tmp-new-adopts)
2605       (incf number
2606             (apply '+ (mapcar
2607                        'gnus-summary-number-of-articles-in-thread
2608                        gnus-tmp-new-adopts))))
2609     (if char
2610         (if (> number 1) gnus-not-empty-thread-mark
2611           gnus-empty-thread-mark)
2612       number)))
2613
2614 (defun gnus-summary-set-local-parameters (group)
2615   "Go through the local params of GROUP and set all variable specs in that list."
2616   (let ((params (gnus-group-find-parameter group))
2617         elem)
2618     (while params
2619       (setq elem (car params)
2620             params (cdr params))
2621       (and (consp elem)                 ; Has to be a cons.
2622            (consp (cdr elem))           ; The cdr has to be a list.
2623            (symbolp (car elem))         ; Has to be a symbol in there.
2624            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2625            (ignore-errors               ; So we set it.
2626              (make-local-variable (car elem))
2627              (set (car elem) (eval (nth 1 elem))))))))
2628
2629 (defun gnus-summary-read-group (group &optional show-all no-article
2630                                       kill-buffer no-display backward
2631                                       select-articles)
2632   "Start reading news in newsgroup GROUP.
2633 If SHOW-ALL is non-nil, already read articles are also listed.
2634 If NO-ARTICLE is non-nil, no article is selected initially.
2635 If NO-DISPLAY, don't generate a summary buffer."
2636   (let (result)
2637     (while (and group
2638                 (null (setq result
2639                             (let ((gnus-auto-select-next nil))
2640                               (or (gnus-summary-read-group-1
2641                                    group show-all no-article
2642                                    kill-buffer no-display
2643                                    select-articles)
2644                                   (setq show-all nil
2645                                         select-articles nil)))))
2646                 (eq gnus-auto-select-next 'quietly))
2647       (set-buffer gnus-group-buffer)
2648       ;; The entry function called above goes to the next
2649       ;; group automatically, so we go two groups back
2650       ;; if we are searching for the previous group.
2651       (when backward
2652         (gnus-group-prev-unread-group 2))
2653       (if (not (equal group (gnus-group-group-name)))
2654           (setq group (gnus-group-group-name))
2655         (setq group nil)))
2656     result))
2657
2658 (defun gnus-summary-read-group-1 (group show-all no-article
2659                                         kill-buffer no-display
2660                                         &optional select-articles)
2661   ;; Killed foreign groups can't be entered.
2662   (when (and (not (gnus-group-native-p group))
2663              (not (gnus-gethash group gnus-newsrc-hashtb)))
2664     (error "Dead non-native groups can't be entered"))
2665   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2666   (let* ((new-group (gnus-summary-setup-buffer group))
2667          (quit-config (gnus-group-quit-config group))
2668          (did-select (and new-group (gnus-select-newsgroup
2669                                      group show-all select-articles))))
2670     (cond
2671      ;; This summary buffer exists already, so we just select it.
2672      ((not new-group)
2673       (gnus-set-global-variables)
2674       (when kill-buffer
2675         (gnus-kill-or-deaden-summary kill-buffer))
2676       (gnus-configure-windows 'summary 'force)
2677       (gnus-set-mode-line 'summary)
2678       (gnus-summary-position-point)
2679       (message "")
2680       t)
2681      ;; We couldn't select this group.
2682      ((null did-select)
2683       (when (and (eq major-mode 'gnus-summary-mode)
2684                  (not (equal (current-buffer) kill-buffer)))
2685         (kill-buffer (current-buffer))
2686         (if (not quit-config)
2687             (progn
2688               ;; Update the info -- marks might need to be removed,
2689               ;; for instance.
2690               (gnus-summary-update-info)
2691               (set-buffer gnus-group-buffer)
2692               (gnus-group-jump-to-group group)
2693               (gnus-group-next-unread-group 1))
2694           (gnus-handle-ephemeral-exit quit-config)))
2695       (gnus-message 3 "Can't select group")
2696       nil)
2697      ;; The user did a `C-g' while prompting for number of articles,
2698      ;; so we exit this group.
2699      ((eq did-select 'quit)
2700       (and (eq major-mode 'gnus-summary-mode)
2701            (not (equal (current-buffer) kill-buffer))
2702            (kill-buffer (current-buffer)))
2703       (when kill-buffer
2704         (gnus-kill-or-deaden-summary kill-buffer))
2705       (if (not quit-config)
2706           (progn
2707             (set-buffer gnus-group-buffer)
2708             (gnus-group-jump-to-group group)
2709             (gnus-group-next-unread-group 1)
2710             (gnus-configure-windows 'group 'force))
2711         (gnus-handle-ephemeral-exit quit-config))
2712       ;; Finally signal the quit.
2713       (signal 'quit nil))
2714      ;; The group was successfully selected.
2715      (t
2716       (gnus-set-global-variables)
2717       ;; Save the active value in effect when the group was entered.
2718       (setq gnus-newsgroup-active
2719             (gnus-copy-sequence
2720              (gnus-active gnus-newsgroup-name)))
2721       ;; You can change the summary buffer in some way with this hook.
2722       (gnus-run-hooks 'gnus-select-group-hook)
2723       ;; Set any local variables in the group parameters.
2724       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2725       (gnus-update-format-specifications
2726        nil 'summary 'summary-mode 'summary-dummy)
2727       ;; Do score processing.
2728       (when gnus-use-scoring
2729         (gnus-possibly-score-headers))
2730       ;; Check whether to fill in the gaps in the threads.
2731       (when gnus-build-sparse-threads
2732         (gnus-build-sparse-threads))
2733       ;; Find the initial limit.
2734       (if gnus-show-threads
2735           (if show-all
2736               (let ((gnus-newsgroup-dormant nil))
2737                 (gnus-summary-initial-limit show-all))
2738             (gnus-summary-initial-limit show-all))
2739         (setq gnus-newsgroup-limit
2740               (mapcar
2741                (lambda (header) (mail-header-number header))
2742                gnus-newsgroup-headers)))
2743       ;; Generate the summary buffer.
2744       (unless no-display
2745         (gnus-summary-prepare))
2746       (when gnus-use-trees
2747         (gnus-tree-open group)
2748         (setq gnus-summary-highlight-line-function
2749               'gnus-tree-highlight-article))
2750       ;; If the summary buffer is empty, but there are some low-scored
2751       ;; articles or some excluded dormants, we include these in the
2752       ;; buffer.
2753       (when (and (zerop (buffer-size))
2754                  (not no-display))
2755         (cond (gnus-newsgroup-dormant
2756                (gnus-summary-limit-include-dormant))
2757               ((and gnus-newsgroup-scored show-all)
2758                (gnus-summary-limit-include-expunged t))))
2759       ;; Function `gnus-apply-kill-file' must be called in this hook.
2760       (gnus-run-hooks 'gnus-apply-kill-hook)
2761       (if (and (zerop (buffer-size))
2762                (not no-display))
2763           (progn
2764             ;; This newsgroup is empty.
2765             (gnus-summary-catchup-and-exit nil t)
2766             (gnus-message 6 "No unread news")
2767             (when kill-buffer
2768               (gnus-kill-or-deaden-summary kill-buffer))
2769             ;; Return nil from this function.
2770             nil)
2771         ;; Hide conversation thread subtrees.  We cannot do this in
2772         ;; gnus-summary-prepare-hook since kill processing may not
2773         ;; work with hidden articles.
2774         (and gnus-show-threads
2775              gnus-thread-hide-subtree
2776              (gnus-summary-hide-all-threads))
2777         (when kill-buffer
2778           (gnus-kill-or-deaden-summary kill-buffer))
2779         ;; Show first unread article if requested.
2780         (if (and (not no-article)
2781                  (not no-display)
2782                  gnus-newsgroup-unreads
2783                  gnus-auto-select-first)
2784             (progn
2785               (gnus-configure-windows 'summary)
2786               (cond
2787                ((eq gnus-auto-select-first 'best)
2788                 (gnus-summary-best-unread-article))
2789                ((eq gnus-auto-select-first t)
2790                 (gnus-summary-first-unread-article))
2791                ((gnus-functionp gnus-auto-select-first)
2792                 (funcall gnus-auto-select-first))))
2793           ;; Don't select any articles, just move point to the first
2794           ;; article in the group.
2795           (goto-char (point-min))
2796           (gnus-summary-position-point)
2797           (gnus-configure-windows 'summary 'force)
2798           (gnus-set-mode-line 'summary))
2799         (when (get-buffer-window gnus-group-buffer t)
2800           ;; Gotta use windows, because recenter does weird stuff if
2801           ;; the current buffer ain't the displayed window.
2802           (let ((owin (selected-window)))
2803             (select-window (get-buffer-window gnus-group-buffer t))
2804             (when (gnus-group-goto-group group)
2805               (recenter))
2806             (select-window owin)))
2807         ;; Mark this buffer as "prepared".
2808         (setq gnus-newsgroup-prepared t)
2809         (gnus-run-hooks 'gnus-summary-prepared-hook)
2810         t)))))
2811
2812 (defun gnus-summary-prepare ()
2813   "Generate the summary buffer."
2814   (interactive)
2815   (let ((buffer-read-only nil))
2816     (erase-buffer)
2817     (setq gnus-newsgroup-data nil
2818           gnus-newsgroup-data-reverse nil)
2819     (gnus-run-hooks 'gnus-summary-generate-hook)
2820     ;; Generate the buffer, either with threads or without.
2821     (when gnus-newsgroup-headers
2822       (gnus-summary-prepare-threads
2823        (if gnus-show-threads
2824            (gnus-sort-gathered-threads
2825             (funcall gnus-summary-thread-gathering-function
2826                      (gnus-sort-threads
2827                       (gnus-cut-threads (gnus-make-threads)))))
2828          ;; Unthreaded display.
2829          (gnus-sort-articles gnus-newsgroup-headers))))
2830     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2831     ;; Call hooks for modifying summary buffer.
2832     (goto-char (point-min))
2833     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2834
2835 (defsubst gnus-general-simplify-subject (subject)
2836   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2837   (setq subject
2838         (cond
2839          ;; Truncate the subject.
2840          (gnus-simplify-subject-functions
2841           (gnus-map-function gnus-simplify-subject-functions subject))
2842          ((numberp gnus-summary-gather-subject-limit)
2843           (setq subject (gnus-simplify-subject-re subject))
2844           (if (> (length subject) gnus-summary-gather-subject-limit)
2845               (substring subject 0 gnus-summary-gather-subject-limit)
2846             subject))
2847          ;; Fuzzily simplify it.
2848          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2849           (gnus-simplify-subject-fuzzy subject))
2850          ;; Just remove the leading "Re:".
2851          (t
2852           (gnus-simplify-subject-re subject))))
2853
2854   (if (and gnus-summary-gather-exclude-subject
2855            (string-match gnus-summary-gather-exclude-subject subject))
2856       nil                               ; This article shouldn't be gathered
2857     subject))
2858
2859 (defun gnus-summary-simplify-subject-query ()
2860   "Query where the respool algorithm would put this article."
2861   (interactive)
2862   (gnus-summary-select-article)
2863   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2864
2865 (defun gnus-gather-threads-by-subject (threads)
2866   "Gather threads by looking at Subject headers."
2867   (if (not gnus-summary-make-false-root)
2868       threads
2869     (let ((hashtb (gnus-make-hashtable 1024))
2870           (prev threads)
2871           (result threads)
2872           subject hthread whole-subject)
2873       (while threads
2874         (setq subject (gnus-general-simplify-subject
2875                        (setq whole-subject (mail-header-subject
2876                                             (caar threads)))))
2877         (when subject
2878           (if (setq hthread (gnus-gethash subject hashtb))
2879               (progn
2880                 ;; We enter a dummy root into the thread, if we
2881                 ;; haven't done that already.
2882                 (unless (stringp (caar hthread))
2883                   (setcar hthread (list whole-subject (car hthread))))
2884                 ;; We add this new gathered thread to this gathered
2885                 ;; thread.
2886                 (setcdr (car hthread)
2887                         (nconc (cdar hthread) (list (car threads))))
2888                 ;; Remove it from the list of threads.
2889                 (setcdr prev (cdr threads))
2890                 (setq threads prev))
2891             ;; Enter this thread into the hash table.
2892             (gnus-sethash subject threads hashtb)))
2893         (setq prev threads)
2894         (setq threads (cdr threads)))
2895       result)))
2896
2897 (defun gnus-gather-threads-by-references (threads)
2898   "Gather threads by looking at References headers."
2899   (let ((idhashtb (gnus-make-hashtable 1024))
2900         (thhashtb (gnus-make-hashtable 1024))
2901         (prev threads)
2902         (result threads)
2903         ids references id gthread gid entered ref)
2904     (while threads
2905       (when (setq references (mail-header-references (caar threads)))
2906         (setq id (mail-header-id (caar threads))
2907               ids (gnus-split-references references)
2908               entered nil)
2909         (while (setq ref (pop ids))
2910           (setq ids (delete ref ids))
2911           (if (not (setq gid (gnus-gethash ref idhashtb)))
2912               (progn
2913                 (gnus-sethash ref id idhashtb)
2914                 (gnus-sethash id threads thhashtb))
2915             (setq gthread (gnus-gethash gid thhashtb))
2916             (unless entered
2917               ;; We enter a dummy root into the thread, if we
2918               ;; haven't done that already.
2919               (unless (stringp (caar gthread))
2920                 (setcar gthread (list (mail-header-subject (caar gthread))
2921                                       (car gthread))))
2922               ;; We add this new gathered thread to this gathered
2923               ;; thread.
2924               (setcdr (car gthread)
2925                       (nconc (cdar gthread) (list (car threads)))))
2926             ;; Add it into the thread hash table.
2927             (gnus-sethash id gthread thhashtb)
2928             (setq entered t)
2929             ;; Remove it from the list of threads.
2930             (setcdr prev (cdr threads))
2931             (setq threads prev))))
2932       (setq prev threads)
2933       (setq threads (cdr threads)))
2934     result))
2935
2936 (defun gnus-sort-gathered-threads (threads)
2937   "Sort subtreads inside each gathered thread by article number."
2938   (let ((result threads))
2939     (while threads
2940       (when (stringp (caar threads))
2941         (setcdr (car threads)
2942                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2943       (setq threads (cdr threads)))
2944     result))
2945
2946 (defun gnus-thread-loop-p (root thread)
2947   "Say whether ROOT is in THREAD."
2948   (let ((stack (list thread))
2949         (infloop 0)
2950         th)
2951     (while (setq thread (pop stack))
2952       (setq th (cdr thread))
2953       (while (and th
2954                   (not (eq (caar th) root)))
2955         (pop th))
2956       (if th
2957           ;; We have found a loop.
2958           (let (ref-dep)
2959             (setcdr thread (delq (car th) (cdr thread)))
2960             (if (boundp (setq ref-dep (intern "none"
2961                                               gnus-newsgroup-dependencies)))
2962                 (setcdr (symbol-value ref-dep)
2963                         (nconc (cdr (symbol-value ref-dep))
2964                                (list (car th))))
2965               (set ref-dep (list nil (car th))))
2966             (setq infloop 1
2967                   stack nil))
2968         ;; Push all the subthreads onto the stack.
2969         (push (cdr thread) stack)))
2970     infloop))
2971
2972 (defun gnus-make-threads ()
2973   "Go through the dependency hashtb and find the roots.  Return all threads."
2974   (let (threads)
2975     (while (catch 'infloop
2976              (mapatoms
2977               (lambda (refs)
2978                 ;; Deal with self-referencing References loops.
2979                 (when (and (car (symbol-value refs))
2980                            (not (zerop
2981                                  (apply
2982                                   '+
2983                                   (mapcar
2984                                    (lambda (thread)
2985                                      (gnus-thread-loop-p
2986                                       (car (symbol-value refs)) thread))
2987                                    (cdr (symbol-value refs)))))))
2988                   (setq threads nil)
2989                   (throw 'infloop t))
2990                 (unless (car (symbol-value refs))
2991                   ;; These threads do not refer back to any other articles,
2992                   ;; so they're roots.
2993                   (setq threads (append (cdr (symbol-value refs)) threads))))
2994               gnus-newsgroup-dependencies)))
2995     threads))
2996
2997 ;; Build the thread tree.
2998 (defsubst gnus-dependencies-add-header (header dependencies force-new)
2999   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3000
3001 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3002 if it was already present.
3003
3004 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3005 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3006 Message-IDs will be renamed be renamed to a unique Message-ID before
3007 being entered.
3008
3009 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3010   (let* ((id (mail-header-id header))
3011          (id-dep (and id (intern id dependencies)))
3012          ref ref-dep ref-header)
3013     ;; Enter this `header' in the `dependencies' table.
3014     (cond
3015      ((not id-dep)
3016       (setq header nil))
3017      ;; The first two cases do the normal part: enter a new `header'
3018      ;; in the `dependencies' table.
3019      ((not (boundp id-dep))
3020       (set id-dep (list header)))
3021      ((null (car (symbol-value id-dep)))
3022       (setcar (symbol-value id-dep) header))
3023
3024      ;; From here the `header' was already present in the
3025      ;; `dependencies' table.
3026      (force-new
3027       ;; Overrides an existing entry;
3028       ;; just set the header part of the entry.
3029       (setcar (symbol-value id-dep) header))
3030
3031      ;; Renames the existing `header' to a unique Message-ID.
3032      ((not gnus-summary-ignore-duplicates)
3033       ;; An article with this Message-ID has already been seen.
3034       ;; We rename the Message-ID.
3035       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3036            (list header))
3037       (mail-header-set-id header id))
3038
3039      ;; The last case ignores an existing entry, except it adds any
3040      ;; additional Xrefs (in case the two articles came from different
3041      ;; servers.
3042      ;; Also sets `header' to `nil' meaning that the `dependencies'
3043      ;; table was *not* modified.
3044      (t
3045       (mail-header-set-xref
3046        (car (symbol-value id-dep))
3047        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3048                    "")
3049                (or (mail-header-xref header) "")))
3050       (setq header nil)))
3051
3052     (when header
3053       ;; First check if that we are not creating a References loop.
3054       (setq ref (gnus-parent-id (mail-header-references header)))
3055       (while (and ref
3056                   (setq ref-dep (intern-soft ref dependencies))
3057                   (boundp ref-dep)
3058                   (setq ref-header (car (symbol-value ref-dep))))
3059         (if (string= id ref)
3060             ;; Yuk!  This is a reference loop.  Make the article be a
3061             ;; root article.
3062             (progn
3063               (mail-header-set-references (car (symbol-value id-dep)) "none")
3064               (setq ref nil))
3065           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3066       (setq ref (gnus-parent-id (mail-header-references header)))
3067       (setq ref-dep (intern (or ref "none") dependencies))
3068       (if (boundp ref-dep)
3069           (setcdr (symbol-value ref-dep)
3070                   (nconc (cdr (symbol-value ref-dep))
3071                          (list (symbol-value id-dep))))
3072         (set ref-dep (list nil (symbol-value id-dep)))))
3073     header))
3074
3075 (defun gnus-build-sparse-threads ()
3076   (let ((headers gnus-newsgroup-headers)
3077         (gnus-summary-ignore-duplicates t)
3078         header references generation relations
3079         subject child end new-child date)
3080     ;; First we create an alist of generations/relations, where
3081     ;; generations is how much we trust the relation, and the relation
3082     ;; is parent/child.
3083     (gnus-message 7 "Making sparse threads...")
3084     (save-excursion
3085       (nnheader-set-temp-buffer " *gnus sparse threads*")
3086       (while (setq header (pop headers))
3087         (when (and (setq references (mail-header-references header))
3088                    (not (string= references "")))
3089           (insert references)
3090           (setq child (mail-header-id header)
3091                 subject (mail-header-subject header)
3092                 date (mail-header-date header)
3093                 generation 0)
3094           (while (search-backward ">" nil t)
3095             (setq end (1+ (point)))
3096             (when (search-backward "<" nil t)
3097               (setq new-child (buffer-substring (point) end))
3098               (push (list (incf generation)
3099                           child (setq child new-child)
3100                           subject date)
3101                     relations)))
3102           (when child
3103             (push (list (1+ generation) child nil subject) relations))
3104           (erase-buffer)))
3105       (kill-buffer (current-buffer)))
3106     ;; Sort over trustworthiness.
3107     (mapcar
3108      (lambda (relation)
3109        (when (gnus-dependencies-add-header
3110               (make-full-mail-header
3111                gnus-reffed-article-number
3112                (nth 3 relation) "" (or (nth 4 relation) "")
3113                (nth 1 relation)
3114                (or (nth 2 relation) "") 0 0 "")
3115               gnus-newsgroup-dependencies nil)
3116          (push gnus-reffed-article-number gnus-newsgroup-limit)
3117          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3118          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3119                gnus-newsgroup-reads)
3120          (decf gnus-reffed-article-number)))
3121      (sort relations 'car-less-than-car))
3122     (gnus-message 7 "Making sparse threads...done")))
3123
3124 (defun gnus-build-old-threads ()
3125   ;; Look at all the articles that refer back to old articles, and
3126   ;; fetch the headers for the articles that aren't there.  This will
3127   ;; build complete threads - if the roots haven't been expired by the
3128   ;; server, that is.
3129   (let (id heads)
3130     (mapatoms
3131      (lambda (refs)
3132        (when (not (car (symbol-value refs)))
3133          (setq heads (cdr (symbol-value refs)))
3134          (while heads
3135            (if (memq (mail-header-number (caar heads))
3136                      gnus-newsgroup-dormant)
3137                (setq heads (cdr heads))
3138              (setq id (symbol-name refs))
3139              (while (and (setq id (gnus-build-get-header id))
3140                          (not (car (gnus-id-to-thread id)))))
3141              (setq heads nil)))))
3142      gnus-newsgroup-dependencies)))
3143
3144 ;; This function has to be called with point after the article number
3145 ;; on the beginning of the line.
3146 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3147   (let ((eol (gnus-point-at-eol))
3148         (buffer (current-buffer))
3149         header)
3150
3151     ;; overview: [num subject from date id refs chars lines misc]
3152     (unless (eobp)
3153       (forward-char))
3154
3155     (setq header
3156           (make-full-mail-header
3157            number                               ; number
3158            (nnheader-nov-field)                 ; subject
3159            (nnheader-nov-field)                 ; from
3160            (nnheader-nov-field)                 ; date
3161            (nnheader-nov-read-message-id)       ; id
3162            (nnheader-nov-field)                 ; refs
3163            (nnheader-nov-read-integer)          ; chars
3164            (nnheader-nov-read-integer)          ; lines
3165            (unless (eobp)
3166              (nnheader-nov-field))              ; misc
3167            (nnheader-nov-parse-extra)))         ; extra
3168
3169     (when gnus-alter-header-function
3170       (funcall gnus-alter-header-function header))
3171     (gnus-dependencies-add-header header dependencies force-new)))
3172
3173 (defun gnus-build-get-header (id)
3174   ;; Look through the buffer of NOV lines and find the header to
3175   ;; ID.  Enter this line into the dependencies hash table, and return
3176   ;; the id of the parent article (if any).
3177   (let ((deps gnus-newsgroup-dependencies)
3178         found header)
3179     (prog1
3180         (save-excursion
3181           (set-buffer nntp-server-buffer)
3182           (let ((case-fold-search nil))
3183             (goto-char (point-min))
3184             (while (and (not found)
3185                         (search-forward id nil t))
3186               (beginning-of-line)
3187               (setq found (looking-at
3188                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3189                                    (regexp-quote id))))
3190               (or found (beginning-of-line 2)))
3191             (when found
3192               (beginning-of-line)
3193               (and
3194                (setq header (gnus-nov-parse-line
3195                              (read (current-buffer)) deps))
3196                (gnus-parent-id (mail-header-references header))))))
3197       (when header
3198         (let ((number (mail-header-number header)))
3199           (push number gnus-newsgroup-limit)
3200           (push header gnus-newsgroup-headers)
3201           (if (memq number gnus-newsgroup-unselected)
3202               (progn
3203                 (push number gnus-newsgroup-unreads)
3204                 (setq gnus-newsgroup-unselected
3205                       (delq number gnus-newsgroup-unselected)))
3206             (push number gnus-newsgroup-ancient)))))))
3207
3208 (defun gnus-build-all-threads ()
3209   "Read all the headers."
3210   (let ((gnus-summary-ignore-duplicates t)
3211         (dependencies gnus-newsgroup-dependencies)
3212         header article)
3213     (save-excursion
3214       (set-buffer nntp-server-buffer)
3215       (let ((case-fold-search nil))
3216         (goto-char (point-min))
3217         (while (not (eobp))
3218           (ignore-errors
3219             (setq article (read (current-buffer))
3220                   header (gnus-nov-parse-line
3221                           article dependencies)))
3222           (when header
3223             (save-excursion
3224               (set-buffer gnus-summary-buffer)
3225               (push header gnus-newsgroup-headers)
3226               (if (memq (setq article (mail-header-number header))
3227                         gnus-newsgroup-unselected)
3228                   (progn
3229                     (push article gnus-newsgroup-unreads)
3230                     (setq gnus-newsgroup-unselected
3231                           (delq article gnus-newsgroup-unselected)))
3232                 (push article gnus-newsgroup-ancient)))
3233             (forward-line 1)))))))
3234
3235 (defun gnus-summary-update-article-line (article header)
3236   "Update the line for ARTICLE using HEADERS."
3237   (let* ((id (mail-header-id header))
3238          (thread (gnus-id-to-thread id)))
3239     (unless thread
3240       (error "Article in no thread"))
3241     ;; Update the thread.
3242     (setcar thread header)
3243     (gnus-summary-goto-subject article)
3244     (let* ((datal (gnus-data-find-list article))
3245            (data (car datal))
3246            (length (when (cdr datal)
3247                      (- (gnus-data-pos data)
3248                         (gnus-data-pos (cadr datal)))))
3249            (buffer-read-only nil)
3250            (level (gnus-summary-thread-level)))
3251       (gnus-delete-line)
3252       (gnus-summary-insert-line
3253        header level nil (gnus-article-mark article)
3254        (memq article gnus-newsgroup-replied)
3255        (memq article gnus-newsgroup-expirable)
3256        ;; Only insert the Subject string when it's different
3257        ;; from the previous Subject string.
3258        (if (gnus-subject-equal
3259             (condition-case ()
3260                 (mail-header-subject
3261                  (gnus-data-header
3262                   (cadr
3263                    (gnus-data-find-list
3264                     article
3265                     (gnus-data-list t)))))
3266               ;; Error on the side of excessive subjects.
3267               (error ""))
3268             (mail-header-subject header))
3269            ""
3270          (mail-header-subject header))
3271        nil (cdr (assq article gnus-newsgroup-scored))
3272        (memq article gnus-newsgroup-processable))
3273       (when length
3274         (gnus-data-update-list
3275          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3276
3277 (defun gnus-summary-update-article (article &optional iheader)
3278   "Update ARTICLE in the summary buffer."
3279   (set-buffer gnus-summary-buffer)
3280   (let* ((header (gnus-summary-article-header article))
3281          (id (mail-header-id header))
3282          (data (gnus-data-find article))
3283          (thread (gnus-id-to-thread id))
3284          (references (mail-header-references header))
3285          (parent
3286           (gnus-id-to-thread
3287            (or (gnus-parent-id
3288                 (when (and references
3289                            (not (equal "" references)))
3290                   references))
3291                "none")))
3292          (buffer-read-only nil)
3293          (old (car thread)))
3294     (when thread
3295       (unless iheader
3296         (setcar thread nil)
3297         (when parent
3298           (delq thread parent)))
3299       (if (gnus-summary-insert-subject id header)
3300           ;; Set the (possibly) new article number in the data structure.
3301           (gnus-data-set-number data (gnus-id-to-article id))
3302         (setcar thread old)
3303         nil))))
3304
3305 (defun gnus-rebuild-thread (id &optional line)
3306   "Rebuild the thread containing ID.
3307 If LINE, insert the rebuilt thread starting on line LINE."
3308   (let ((buffer-read-only nil)
3309         old-pos current thread data)
3310     (if (not gnus-show-threads)
3311         (setq thread (list (car (gnus-id-to-thread id))))
3312       ;; Get the thread this article is part of.
3313       (setq thread (gnus-remove-thread id)))
3314     (setq old-pos (gnus-point-at-bol))
3315     (setq current (save-excursion
3316                     (and (zerop (forward-line -1))
3317                          (gnus-summary-article-number))))
3318     ;; If this is a gathered thread, we have to go some re-gathering.
3319     (when (stringp (car thread))
3320       (let ((subject (car thread))
3321             roots thr)
3322         (setq thread (cdr thread))
3323         (while thread
3324           (unless (memq (setq thr (gnus-id-to-thread
3325                                    (gnus-root-id
3326                                     (mail-header-id (caar thread)))))
3327                         roots)
3328             (push thr roots))
3329           (setq thread (cdr thread)))
3330         ;; We now have all (unique) roots.
3331         (if (= (length roots) 1)
3332             ;; All the loose roots are now one solid root.
3333             (setq thread (car roots))
3334           (setq thread (cons subject (gnus-sort-threads roots))))))
3335     (let (threads)
3336       ;; We then insert this thread into the summary buffer.
3337       (when line
3338         (goto-char (point-min))
3339         (forward-line (1- line)))
3340       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3341         (if gnus-show-threads
3342             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3343           (gnus-summary-prepare-unthreaded thread))
3344         (setq data (nreverse gnus-newsgroup-data))
3345         (setq threads gnus-newsgroup-threads))
3346       ;; We splice the new data into the data structure.
3347       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3348       ;;!!! then we want to insert at the beginning of the buffer.
3349       ;;!!! That happens to be true with Gnus now, but that may
3350       ;;!!! change in the future.  Perhaps.
3351       (gnus-data-enter-list
3352        (if line nil current) data (- (point) old-pos))
3353       (setq gnus-newsgroup-threads
3354             (nconc threads gnus-newsgroup-threads))
3355       (gnus-data-compute-positions))))
3356
3357 (defun gnus-number-to-header (number)
3358   "Return the header for article NUMBER."
3359   (let ((headers gnus-newsgroup-headers))
3360     (while (and headers
3361                 (not (= number (mail-header-number (car headers)))))
3362       (pop headers))
3363     (when headers
3364       (car headers))))
3365
3366 (defun gnus-parent-headers (in-headers &optional generation)
3367   "Return the headers of the GENERATIONeth parent of HEADERS."
3368   (unless generation
3369     (setq generation 1))
3370   (let ((parent t)
3371         (headers in-headers)
3372         references)
3373     (while (and parent
3374                 (not (zerop generation))
3375                 (setq references (mail-header-references headers)))
3376       (setq headers (if (and references
3377                              (setq parent (gnus-parent-id references)))
3378                         (car (gnus-id-to-thread parent))
3379                       nil))
3380       (decf generation))
3381     (and (not (eq headers in-headers))
3382          headers)))
3383
3384 (defun gnus-id-to-thread (id)
3385   "Return the (sub-)thread where ID appears."
3386   (gnus-gethash id gnus-newsgroup-dependencies))
3387
3388 (defun gnus-id-to-article (id)
3389   "Return the article number of ID."
3390   (let ((thread (gnus-id-to-thread id)))
3391     (when (and thread
3392                (car thread))
3393       (mail-header-number (car thread)))))
3394
3395 (defun gnus-id-to-header (id)
3396   "Return the article headers of ID."
3397   (car (gnus-id-to-thread id)))
3398
3399 (defun gnus-article-displayed-root-p (article)
3400   "Say whether ARTICLE is a root(ish) article."
3401   (let ((level (gnus-summary-thread-level article))
3402         (refs (mail-header-references  (gnus-summary-article-header article)))
3403         particle)
3404     (cond
3405      ((null level) nil)
3406      ((zerop level) t)
3407      ((null refs) t)
3408      ((null (gnus-parent-id refs)) t)
3409      ((and (= 1 level)
3410            (null (setq particle (gnus-id-to-article
3411                                  (gnus-parent-id refs))))
3412            (null (gnus-summary-thread-level particle)))))))
3413
3414 (defun gnus-root-id (id)
3415   "Return the id of the root of the thread where ID appears."
3416   (let (last-id prev)
3417     (while (and id (setq prev (car (gnus-id-to-thread id))))
3418       (setq last-id id
3419             id (gnus-parent-id (mail-header-references prev))))
3420     last-id))
3421
3422 (defun gnus-articles-in-thread (thread)
3423   "Return the list of articles in THREAD."
3424   (cons (mail-header-number (car thread))
3425         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3426
3427 (defun gnus-remove-thread (id &optional dont-remove)
3428   "Remove the thread that has ID in it."
3429   (let (headers thread last-id)
3430     ;; First go up in this thread until we find the root.
3431     (setq last-id (gnus-root-id id)
3432           headers (message-flatten-list (gnus-id-to-thread last-id)))
3433     ;; We have now found the real root of this thread.  It might have
3434     ;; been gathered into some loose thread, so we have to search
3435     ;; through the threads to find the thread we wanted.
3436     (let ((threads gnus-newsgroup-threads)
3437           sub)
3438       (while threads
3439         (setq sub (car threads))
3440         (if (stringp (car sub))
3441             ;; This is a gathered thread, so we look at the roots
3442             ;; below it to find whether this article is in this
3443             ;; gathered root.
3444             (progn
3445               (setq sub (cdr sub))
3446               (while sub
3447                 (when (member (caar sub) headers)
3448                   (setq thread (car threads)
3449                         threads nil
3450                         sub nil))
3451                 (setq sub (cdr sub))))
3452           ;; It's an ordinary thread, so we check it.
3453           (when (eq (car sub) (car headers))
3454             (setq thread sub
3455                   threads nil)))
3456         (setq threads (cdr threads)))
3457       ;; If this article is in no thread, then it's a root.
3458       (if thread
3459           (unless dont-remove
3460             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3461         (setq thread (gnus-id-to-thread last-id)))
3462       (when thread
3463         (prog1
3464             thread                      ; We return this thread.
3465           (unless dont-remove
3466             (if (stringp (car thread))
3467                 (progn
3468                   ;; If we use dummy roots, then we have to remove the
3469                   ;; dummy root as well.
3470                   (when (eq gnus-summary-make-false-root 'dummy)
3471                     ;; We go to the dummy root by going to
3472                     ;; the first sub-"thread", and then one line up.
3473                     (gnus-summary-goto-article
3474                      (mail-header-number (caadr thread)))
3475                     (forward-line -1)
3476                     (gnus-delete-line)
3477                     (gnus-data-compute-positions))
3478                   (setq thread (cdr thread))
3479                   (while thread
3480                     (gnus-remove-thread-1 (car thread))
3481                     (setq thread (cdr thread))))
3482               (gnus-summary-show-all-threads)
3483               (gnus-remove-thread-1 thread))))))))
3484
3485 (defun gnus-remove-thread-1 (thread)
3486   "Remove the thread THREAD recursively."
3487   (let ((number (mail-header-number (pop thread)))
3488         d)
3489     (setq thread (reverse thread))
3490     (while thread
3491       (gnus-remove-thread-1 (pop thread)))
3492     (when (setq d (gnus-data-find number))
3493       (goto-char (gnus-data-pos d))
3494       (gnus-data-remove
3495        number
3496        (- (gnus-point-at-bol)
3497           (prog1
3498               (1+ (gnus-point-at-eol))
3499             (gnus-delete-line)))))))
3500
3501 (defun gnus-sort-threads (threads)
3502   "Sort THREADS."
3503   (if (not gnus-thread-sort-functions)
3504       threads
3505     (gnus-message 8 "Sorting threads...")
3506     (prog1
3507         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3508       (gnus-message 8 "Sorting threads...done"))))
3509
3510 (defun gnus-sort-articles (articles)
3511   "Sort ARTICLES."
3512   (when gnus-article-sort-functions
3513     (gnus-message 7 "Sorting articles...")
3514     (prog1
3515         (setq gnus-newsgroup-headers
3516               (sort articles (gnus-make-sort-function
3517                               gnus-article-sort-functions)))
3518       (gnus-message 7 "Sorting articles...done"))))
3519
3520 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3521 (defmacro gnus-thread-header (thread)
3522   ;; Return header of first article in THREAD.
3523   ;; Note that THREAD must never, ever be anything else than a variable -
3524   ;; using some other form will lead to serious barfage.
3525   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3526   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3527   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3528         (vector thread) 2))
3529
3530 (defsubst gnus-article-sort-by-number (h1 h2)
3531   "Sort articles by article number."
3532   (< (mail-header-number h1)
3533      (mail-header-number h2)))
3534
3535 (defun gnus-thread-sort-by-number (h1 h2)
3536   "Sort threads by root article number."
3537   (gnus-article-sort-by-number
3538    (gnus-thread-header h1) (gnus-thread-header h2)))
3539
3540 (defsubst gnus-article-sort-by-lines (h1 h2)
3541   "Sort articles by article Lines header."
3542   (< (mail-header-lines h1)
3543      (mail-header-lines h2)))
3544
3545 (defun gnus-thread-sort-by-lines (h1 h2)
3546   "Sort threads by root article Lines header."
3547   (gnus-article-sort-by-lines
3548    (gnus-thread-header h1) (gnus-thread-header h2)))
3549
3550 (defsubst gnus-article-sort-by-chars (h1 h2)
3551   "Sort articles by octet length."
3552   (< (mail-header-chars h1)
3553      (mail-header-chars h2)))
3554
3555 (defun gnus-thread-sort-by-chars (h1 h2)
3556   "Sort threads by root article octet length."
3557   (gnus-article-sort-by-chars
3558    (gnus-thread-header h1) (gnus-thread-header h2)))
3559
3560 (defsubst gnus-article-sort-by-author (h1 h2)
3561   "Sort articles by root author."
3562   (string-lessp
3563    (let ((addr (mime-read-field 'From h1)))
3564      (or (std11-full-name-string addr)
3565          (std11-address-string addr)
3566          ""))
3567    (let ((addr (mime-read-field 'From h2)))
3568      (or (std11-full-name-string addr)
3569          (std11-address-string addr)
3570          ""))
3571    ))
3572
3573 (defun gnus-thread-sort-by-author (h1 h2)
3574   "Sort threads by root author."
3575   (gnus-article-sort-by-author
3576    (gnus-thread-header h1)  (gnus-thread-header h2)))
3577
3578 (defsubst gnus-article-sort-by-subject (h1 h2)
3579   "Sort articles by root subject."
3580   (string-lessp
3581    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3582    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3583
3584 (defun gnus-thread-sort-by-subject (h1 h2)
3585   "Sort threads by root subject."
3586   (gnus-article-sort-by-subject
3587    (gnus-thread-header h1) (gnus-thread-header h2)))
3588
3589 (defsubst gnus-article-sort-by-date (h1 h2)
3590   "Sort articles by root article date."
3591   (time-less-p
3592    (gnus-date-get-time (mail-header-date h1))
3593    (gnus-date-get-time (mail-header-date h2))))
3594
3595 (defun gnus-thread-sort-by-date (h1 h2)
3596   "Sort threads by root article date."
3597   (gnus-article-sort-by-date
3598    (gnus-thread-header h1) (gnus-thread-header h2)))
3599
3600 (defsubst gnus-article-sort-by-score (h1 h2)
3601   "Sort articles by root article score.
3602 Unscored articles will be counted as having a score of zero."
3603   (> (or (cdr (assq (mail-header-number h1)
3604                     gnus-newsgroup-scored))
3605          gnus-summary-default-score 0)
3606      (or (cdr (assq (mail-header-number h2)
3607                     gnus-newsgroup-scored))
3608          gnus-summary-default-score 0)))
3609
3610 (defun gnus-thread-sort-by-score (h1 h2)
3611   "Sort threads by root article score."
3612   (gnus-article-sort-by-score
3613    (gnus-thread-header h1) (gnus-thread-header h2)))
3614
3615 (defun gnus-thread-sort-by-total-score (h1 h2)
3616   "Sort threads by the sum of all scores in the thread.
3617 Unscored articles will be counted as having a score of zero."
3618   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3619
3620 (defun gnus-thread-total-score (thread)
3621   ;; This function find the total score of THREAD.
3622   (cond ((null thread)
3623          0)
3624         ((consp thread)
3625          (if (stringp (car thread))
3626              (apply gnus-thread-score-function 0
3627                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3628            (gnus-thread-total-score-1 thread)))
3629         (t
3630          (gnus-thread-total-score-1 (list thread)))))
3631
3632 (defun gnus-thread-total-score-1 (root)
3633   ;; This function find the total score of the thread below ROOT.
3634   (setq root (car root))
3635   (apply gnus-thread-score-function
3636          (or (append
3637               (mapcar 'gnus-thread-total-score
3638                       (cdr (gnus-id-to-thread (mail-header-id root))))
3639               (when (> (mail-header-number root) 0)
3640                 (list (or (cdr (assq (mail-header-number root)
3641                                      gnus-newsgroup-scored))
3642                           gnus-summary-default-score 0))))
3643              (list gnus-summary-default-score)
3644              '(0))))
3645
3646 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3647 (defvar gnus-tmp-prev-subject nil)
3648 (defvar gnus-tmp-false-parent nil)
3649 (defvar gnus-tmp-root-expunged nil)
3650 (defvar gnus-tmp-dummy-line nil)
3651
3652 (defvar gnus-tmp-header)
3653 (defun gnus-extra-header (type &optional header)
3654   "Return the extra header of TYPE."
3655   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3656       ""))
3657
3658 (defun gnus-summary-prepare-threads (threads)
3659   "Prepare summary buffer from THREADS and indentation LEVEL.
3660 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3661 or a straight list of headers."
3662   (gnus-message 7 "Generating summary...")
3663
3664   (setq gnus-newsgroup-threads threads)
3665   (beginning-of-line)
3666
3667   (let ((gnus-tmp-level 0)
3668         (default-score (or gnus-summary-default-score 0))
3669         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3670         thread number subject stack state gnus-tmp-gathered beg-match
3671         new-roots gnus-tmp-new-adopts thread-end
3672         gnus-tmp-header gnus-tmp-unread
3673         gnus-tmp-replied gnus-tmp-subject-or-nil
3674         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3675         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3676         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3677
3678     (setq gnus-tmp-prev-subject nil)
3679
3680     (if (vectorp (car threads))
3681         ;; If this is a straight (sic) list of headers, then a
3682         ;; threaded summary display isn't required, so we just create
3683         ;; an unthreaded one.
3684         (gnus-summary-prepare-unthreaded threads)
3685
3686       ;; Do the threaded display.
3687
3688       (while (or threads stack gnus-tmp-new-adopts new-roots)
3689
3690         (if (and (= gnus-tmp-level 0)
3691                  (or (not stack)
3692                      (= (caar stack) 0))
3693                  (not gnus-tmp-false-parent)
3694                  (or gnus-tmp-new-adopts new-roots))
3695             (if gnus-tmp-new-adopts
3696                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3697                       thread (list (car gnus-tmp-new-adopts))
3698                       gnus-tmp-header (caar thread)
3699                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3700               (when new-roots
3701                 (setq thread (list (car new-roots))
3702                       gnus-tmp-header (caar thread)
3703                       new-roots (cdr new-roots))))
3704
3705           (if threads
3706               ;; If there are some threads, we do them before the
3707               ;; threads on the stack.
3708               (setq thread threads
3709                     gnus-tmp-header (caar thread))
3710             ;; There were no current threads, so we pop something off
3711             ;; the stack.
3712             (setq state (car stack)
3713                   gnus-tmp-level (car state)
3714                   thread (cdr state)
3715                   stack (cdr stack)
3716                   gnus-tmp-header (caar thread))))
3717
3718         (setq gnus-tmp-false-parent nil)
3719         (setq gnus-tmp-root-expunged nil)
3720         (setq thread-end nil)
3721
3722         (if (stringp gnus-tmp-header)
3723             ;; The header is a dummy root.
3724             (cond
3725              ((eq gnus-summary-make-false-root 'adopt)
3726               ;; We let the first article adopt the rest.
3727               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3728                                                (cddar thread)))
3729               (setq gnus-tmp-gathered
3730                     (nconc (mapcar
3731                             (lambda (h) (mail-header-number (car h)))
3732                             (cddar thread))
3733                            gnus-tmp-gathered))
3734               (setq thread (cons (list (caar thread)
3735                                        (cadar thread))
3736                                  (cdr thread)))
3737               (setq gnus-tmp-level -1
3738                     gnus-tmp-false-parent t))
3739              ((eq gnus-summary-make-false-root 'empty)
3740               ;; We print adopted articles with empty subject fields.
3741               (setq gnus-tmp-gathered
3742                     (nconc (mapcar
3743                             (lambda (h) (mail-header-number (car h)))
3744                             (cddar thread))
3745                            gnus-tmp-gathered))
3746               (setq gnus-tmp-level -1))
3747              ((eq gnus-summary-make-false-root 'dummy)
3748               ;; We remember that we probably want to output a dummy
3749               ;; root.
3750               (setq gnus-tmp-dummy-line gnus-tmp-header)
3751               (setq gnus-tmp-prev-subject gnus-tmp-header))
3752              (t
3753               ;; We do not make a root for the gathered
3754               ;; sub-threads at all.
3755               (setq gnus-tmp-level -1)))
3756
3757           (setq number (mail-header-number gnus-tmp-header)
3758                 subject (mail-header-subject gnus-tmp-header))
3759
3760           (cond
3761            ;; If the thread has changed subject, we might want to make
3762            ;; this subthread into a root.
3763            ((and (null gnus-thread-ignore-subject)
3764                  (not (zerop gnus-tmp-level))
3765                  gnus-tmp-prev-subject
3766                  (not (inline
3767                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3768             (setq new-roots (nconc new-roots (list (car thread)))
3769                   thread-end t
3770                   gnus-tmp-header nil))
3771            ;; If the article lies outside the current limit,
3772            ;; then we do not display it.
3773            ((not (memq number gnus-newsgroup-limit))
3774             (setq gnus-tmp-gathered
3775                   (nconc (mapcar
3776                           (lambda (h) (mail-header-number (car h)))
3777                           (cdar thread))
3778                          gnus-tmp-gathered))
3779             (setq gnus-tmp-new-adopts (if (cdar thread)
3780                                           (append gnus-tmp-new-adopts
3781                                                   (cdar thread))
3782                                         gnus-tmp-new-adopts)
3783                   thread-end t
3784                   gnus-tmp-header nil)
3785             (when (zerop gnus-tmp-level)
3786               (setq gnus-tmp-root-expunged t)))
3787            ;; Perhaps this article is to be marked as read?
3788            ((and gnus-summary-mark-below
3789                  (< (or (cdr (assq number gnus-newsgroup-scored))
3790                         default-score)
3791                     gnus-summary-mark-below)
3792                  ;; Don't touch sparse articles.
3793                  (not (gnus-summary-article-sparse-p number))
3794                  (not (gnus-summary-article-ancient-p number)))
3795             (setq gnus-newsgroup-unreads
3796                   (delq number gnus-newsgroup-unreads))
3797             (if gnus-newsgroup-auto-expire
3798                 (push number gnus-newsgroup-expirable)
3799               (push (cons number gnus-low-score-mark)
3800                     gnus-newsgroup-reads))))
3801
3802           (when gnus-tmp-header
3803             ;; We may have an old dummy line to output before this
3804             ;; article.
3805             (when (and gnus-tmp-dummy-line
3806                        (gnus-subject-equal
3807                         gnus-tmp-dummy-line
3808                         (mail-header-subject gnus-tmp-header)))
3809               (gnus-summary-insert-dummy-line
3810                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3811               (setq gnus-tmp-dummy-line nil))
3812
3813             ;; Compute the mark.
3814             (setq gnus-tmp-unread (gnus-article-mark number))
3815
3816             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3817                                   gnus-tmp-header gnus-tmp-level)
3818                   gnus-newsgroup-data)
3819
3820             ;; Actually insert the line.
3821             (setq
3822              gnus-tmp-subject-or-nil
3823              (cond
3824               ((and gnus-thread-ignore-subject
3825                     gnus-tmp-prev-subject
3826                     (not (inline (gnus-subject-equal
3827                                   gnus-tmp-prev-subject subject))))
3828                subject)
3829               ((zerop gnus-tmp-level)
3830                (if (and (eq gnus-summary-make-false-root 'empty)
3831                         (memq number gnus-tmp-gathered)
3832                         gnus-tmp-prev-subject
3833                         (inline (gnus-subject-equal
3834                                  gnus-tmp-prev-subject subject)))
3835                    gnus-summary-same-subject
3836                  subject))
3837               (t gnus-summary-same-subject)))
3838             (if (and (eq gnus-summary-make-false-root 'adopt)
3839                      (= gnus-tmp-level 1)
3840                      (memq number gnus-tmp-gathered))
3841                 (setq gnus-tmp-opening-bracket ?\<
3842                       gnus-tmp-closing-bracket ?\>)
3843               (setq gnus-tmp-opening-bracket ?\[
3844                     gnus-tmp-closing-bracket ?\]))
3845             (setq
3846              gnus-tmp-indentation
3847              (aref gnus-thread-indent-array gnus-tmp-level)
3848              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3849              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3850                                 gnus-summary-default-score 0)
3851              gnus-tmp-score-char
3852              (if (or (null gnus-summary-default-score)
3853                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3854                          gnus-summary-zcore-fuzz))
3855                  ? ;Whitespace
3856                (if (< gnus-tmp-score gnus-summary-default-score)
3857                    gnus-score-below-mark gnus-score-over-mark))
3858              gnus-tmp-replied
3859              (cond ((memq number gnus-newsgroup-processable)
3860                     gnus-process-mark)
3861                    ((memq number gnus-newsgroup-cached)
3862                     gnus-cached-mark)
3863                    ((memq number gnus-newsgroup-replied)
3864                     gnus-replied-mark)
3865                    ((memq number gnus-newsgroup-saved)
3866                     gnus-saved-mark)
3867                    (t gnus-unread-mark))
3868              gnus-tmp-from (mail-header-from gnus-tmp-header)
3869              gnus-tmp-name
3870              (cond
3871               ((string-match "<[^>]+> *$" gnus-tmp-from)
3872                (setq beg-match (match-beginning 0))
3873                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3874                         (substring gnus-tmp-from (1+ (match-beginning 0))
3875                                    (1- (match-end 0))))
3876                    (substring gnus-tmp-from 0 beg-match)))
3877               ((string-match "(.+)" gnus-tmp-from)
3878                (substring gnus-tmp-from
3879                           (1+ (match-beginning 0)) (1- (match-end 0))))
3880               (t gnus-tmp-from)))
3881             (when (string= gnus-tmp-name "")
3882               (setq gnus-tmp-name gnus-tmp-from))
3883             (unless (numberp gnus-tmp-lines)
3884               (setq gnus-tmp-lines 0))
3885             (gnus-put-text-property
3886              (point)
3887              (progn (eval gnus-summary-line-format-spec) (point))
3888              'gnus-number number)
3889             (when gnus-visual-p
3890               (forward-line -1)
3891               (gnus-run-hooks 'gnus-summary-update-hook)
3892               (forward-line 1))
3893
3894             (setq gnus-tmp-prev-subject subject)))
3895
3896         (when (nth 1 thread)
3897           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3898         (incf gnus-tmp-level)
3899         (setq threads (if thread-end nil (cdar thread)))
3900         (unless threads
3901           (setq gnus-tmp-level 0)))))
3902   (gnus-message 7 "Generating summary...done"))
3903
3904 (defun gnus-summary-prepare-unthreaded (headers)
3905   "Generate an unthreaded summary buffer based on HEADERS."
3906   (let (header number mark)
3907
3908     (beginning-of-line)
3909
3910     (while headers
3911       ;; We may have to root out some bad articles...
3912       (when (memq (setq number (mail-header-number
3913                                 (setq header (pop headers))))
3914                   gnus-newsgroup-limit)
3915         ;; Mark article as read when it has a low score.
3916         (when (and gnus-summary-mark-below
3917                    (< (or (cdr (assq number gnus-newsgroup-scored))
3918                           gnus-summary-default-score 0)
3919                       gnus-summary-mark-below)
3920                    (not (gnus-summary-article-ancient-p number)))
3921           (setq gnus-newsgroup-unreads
3922                 (delq number gnus-newsgroup-unreads))
3923           (if gnus-newsgroup-auto-expire
3924               (push number gnus-newsgroup-expirable)
3925             (push (cons number gnus-low-score-mark)
3926                   gnus-newsgroup-reads)))
3927
3928         (setq mark (gnus-article-mark number))
3929         (push (gnus-data-make number mark (1+ (point)) header 0)
3930               gnus-newsgroup-data)
3931         (gnus-summary-insert-line
3932          header 0 number
3933          mark (memq number gnus-newsgroup-replied)
3934          (memq number gnus-newsgroup-expirable)
3935          (mail-header-subject header) nil
3936          (cdr (assq number gnus-newsgroup-scored))
3937          (memq number gnus-newsgroup-processable))))))
3938
3939 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3940   "Select newsgroup GROUP.
3941 If READ-ALL is non-nil, all articles in the group are selected.
3942 If SELECT-ARTICLES, only select those articles from GROUP."
3943   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3944          ;;!!! Dirty hack; should be removed.
3945          (gnus-summary-ignore-duplicates
3946           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3947               t
3948             gnus-summary-ignore-duplicates))
3949          (info (nth 2 entry))
3950          articles fetched-articles cached)
3951
3952     (unless (gnus-check-server
3953              (setq gnus-current-select-method
3954                    (gnus-find-method-for-group group)))
3955       (error "Couldn't open server"))
3956
3957     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3958         (gnus-activate-group group)     ; Or we can activate it...
3959         (progn                          ; Or we bug out.
3960           (when (equal major-mode 'gnus-summary-mode)
3961             (kill-buffer (current-buffer)))
3962           (error "Couldn't request group %s: %s"
3963                  group (gnus-status-message group))))
3964
3965     (unless (gnus-request-group group t)
3966       (when (equal major-mode 'gnus-summary-mode)
3967         (kill-buffer (current-buffer)))
3968       (error "Couldn't request group %s: %s"
3969              group (gnus-status-message group)))
3970
3971     (setq gnus-newsgroup-name group)
3972     (setq gnus-newsgroup-unselected nil)
3973     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3974     (gnus-summary-setup-default-charset)
3975
3976     ;; Adjust and set lists of article marks.
3977     (when info
3978       (gnus-adjust-marked-articles info))
3979
3980     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3981     (when (gnus-virtual-group-p group)
3982       (setq cached gnus-newsgroup-cached))
3983
3984     (setq gnus-newsgroup-unreads
3985           (gnus-set-difference
3986            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3987            gnus-newsgroup-dormant))
3988
3989     (setq gnus-newsgroup-processable nil)
3990
3991     (gnus-update-read-articles group gnus-newsgroup-unreads)
3992
3993     (if (setq articles select-articles)
3994         (setq gnus-newsgroup-unselected
3995               (gnus-sorted-intersection
3996                gnus-newsgroup-unreads
3997                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3998       (setq articles (gnus-articles-to-read group read-all)))
3999
4000     (cond
4001      ((null articles)
4002       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4003       'quit)
4004      ((eq articles 0) nil)
4005      (t
4006       ;; Init the dependencies hash table.
4007       (setq gnus-newsgroup-dependencies
4008             (gnus-make-hashtable (length articles)))
4009       (gnus-set-global-variables)
4010       ;; Retrieve the headers and read them in.
4011       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4012       (setq gnus-newsgroup-headers
4013             (gnus-retrieve-parsed-headers
4014              articles gnus-newsgroup-name
4015              ;; We might want to fetch old headers, but
4016              ;; not if there is only 1 article.
4017              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4018                            (not (numberp gnus-fetch-old-headers)))
4019                       (> (length articles) 1))
4020                   gnus-fetch-old-headers)))
4021       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4022
4023       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4024       (when cached
4025         (setq gnus-newsgroup-cached cached))
4026
4027       ;; Suppress duplicates?
4028       (when gnus-suppress-duplicates
4029         (gnus-dup-suppress-articles))
4030
4031       ;; Set the initial limit.
4032       (setq gnus-newsgroup-limit (copy-sequence articles))
4033       ;; Remove canceled articles from the list of unread articles.
4034       (setq gnus-newsgroup-unreads
4035             (gnus-set-sorted-intersection
4036              gnus-newsgroup-unreads
4037              (setq fetched-articles
4038                    (mapcar (lambda (headers) (mail-header-number headers))
4039                            gnus-newsgroup-headers))))
4040       ;; Removed marked articles that do not exist.
4041       (gnus-update-missing-marks
4042        (gnus-sorted-complement fetched-articles articles))
4043       ;; We might want to build some more threads first.
4044       (when (and gnus-fetch-old-headers
4045                  (eq gnus-headers-retrieved-by 'nov))
4046         (if (eq gnus-fetch-old-headers 'invisible)
4047             (gnus-build-all-threads)
4048           (gnus-build-old-threads)))
4049       ;; Let the Gnus agent mark articles as read.
4050       (when gnus-agent
4051         (gnus-agent-get-undownloaded-list))
4052       ;; Check whether auto-expire is to be done in this group.
4053       (setq gnus-newsgroup-auto-expire
4054             (gnus-group-auto-expirable-p group))
4055       ;; Set up the article buffer now, if necessary.
4056       (unless gnus-single-article-buffer
4057         (gnus-article-setup-buffer))
4058       ;; First and last article in this newsgroup.
4059       (when gnus-newsgroup-headers
4060         (setq gnus-newsgroup-begin
4061               (mail-header-number (car gnus-newsgroup-headers))
4062               gnus-newsgroup-end
4063               (mail-header-number
4064                (gnus-last-element gnus-newsgroup-headers))))
4065       ;; GROUP is successfully selected.
4066       (or gnus-newsgroup-headers t)))))
4067
4068 (defun gnus-articles-to-read (group &optional read-all)
4069   ;; Find out what articles the user wants to read.
4070   (let* ((articles
4071           ;; Select all articles if `read-all' is non-nil, or if there
4072           ;; are no unread articles.
4073           (if (or read-all
4074                   (and (zerop (length gnus-newsgroup-marked))
4075                        (zerop (length gnus-newsgroup-unreads)))
4076                   (eq (gnus-group-find-parameter group 'display)
4077                       'all))
4078               (gnus-uncompress-range (gnus-active group))
4079             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4080                           (copy-sequence gnus-newsgroup-unreads))
4081                   '<)))
4082          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4083          (scored (length scored-list))
4084          (number (length articles))
4085          (marked (+ (length gnus-newsgroup-marked)
4086                     (length gnus-newsgroup-dormant)))
4087          (select
4088           (cond
4089            ((numberp read-all)
4090             read-all)
4091            (t
4092             (condition-case ()
4093                 (cond
4094                  ((and (or (<= scored marked) (= scored number))
4095                        (natnump gnus-large-newsgroup)
4096                        (> number gnus-large-newsgroup))
4097                   (let* ((minibuffer-setup-hook (append
4098                                                  minibuffer-setup-hook
4099                                                  '(beginning-of-line)))
4100                          (input (read-string
4101                                  (format
4102                                   "How many articles from %s (max %d): "
4103                                   (gnus-limit-string gnus-newsgroup-name 35)
4104                                   number)
4105                                  (number-to-string gnus-large-newsgroup))))
4106                     (if (string-match "^[ \t]*$" input)
4107                         number
4108                       input)))
4109                  ((and (> scored marked) (< scored number)
4110                        (> (- scored number) 20))
4111                   (let ((input
4112                          (read-string
4113                           (format "%s %s (%d scored, %d total): "
4114                                   "How many articles from"
4115                                   group scored number))))
4116                     (if (string-match "^[ \t]*$" input)
4117                         number input)))
4118                  (t number))
4119               (quit nil))))))
4120     (setq select (if (stringp select) (string-to-number select) select))
4121     (if (or (null select) (zerop select))
4122         select
4123       (if (and (not (zerop scored)) (<= (abs select) scored))
4124           (progn
4125             (setq articles (sort scored-list '<))
4126             (setq number (length articles)))
4127         (setq articles (copy-sequence articles)))
4128
4129       (when (< (abs select) number)
4130         (if (< select 0)
4131             ;; Select the N oldest articles.
4132             (setcdr (nthcdr (1- (abs select)) articles) nil)
4133           ;; Select the N most recent articles.
4134           (setq articles (nthcdr (- number select) articles))))
4135       (setq gnus-newsgroup-unselected
4136             (gnus-sorted-intersection
4137              gnus-newsgroup-unreads
4138              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4139       articles)))
4140
4141 (defun gnus-killed-articles (killed articles)
4142   (let (out)
4143     (while articles
4144       (when (inline (gnus-member-of-range (car articles) killed))
4145         (push (car articles) out))
4146       (setq articles (cdr articles)))
4147     out))
4148
4149 (defun gnus-uncompress-marks (marks)
4150   "Uncompress the mark ranges in MARKS."
4151   (let ((uncompressed '(score bookmark))
4152         out)
4153     (while marks
4154       (if (memq (caar marks) uncompressed)
4155           (push (car marks) out)
4156         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4157       (setq marks (cdr marks)))
4158     out))
4159
4160 (defun gnus-adjust-marked-articles (info)
4161   "Set all article lists and remove all marks that are no longer valid."
4162   (let* ((marked-lists (gnus-info-marks info))
4163          (active (gnus-active (gnus-info-group info)))
4164          (min (car active))
4165          (max (cdr active))
4166          (types gnus-article-mark-lists)
4167          (uncompressed '(score bookmark killed))
4168          marks var articles article mark)
4169
4170     (while marked-lists
4171       (setq marks (pop marked-lists))
4172       (set (setq var (intern (format "gnus-newsgroup-%s"
4173                                      (car (rassq (setq mark (car marks))
4174                                                  types)))))
4175            (if (memq (car marks) uncompressed) (cdr marks)
4176              (gnus-uncompress-range (cdr marks))))
4177
4178       (setq articles (symbol-value var))
4179
4180       ;; All articles have to be subsets of the active articles.
4181       (cond
4182        ;; Adjust "simple" lists.
4183        ((memq mark '(tick dormant expire reply save))
4184         (while articles
4185           (when (or (< (setq article (pop articles)) min) (> article max))
4186             (set var (delq article (symbol-value var))))))
4187        ;; Adjust assocs.
4188        ((memq mark uncompressed)
4189         (when (not (listp (cdr (symbol-value var))))
4190           (set var (list (symbol-value var))))
4191         (when (not (listp (cdr articles)))
4192           (setq articles (list articles)))
4193         (while articles
4194           (when (or (not (consp (setq article (pop articles))))
4195                     (< (car article) min)
4196                     (> (car article) max))
4197             (set var (delq article (symbol-value var))))))))))
4198
4199 (defun gnus-update-missing-marks (missing)
4200   "Go through the list of MISSING articles and remove them from the mark lists."
4201   (when missing
4202     (let ((types gnus-article-mark-lists)
4203           var m)
4204       ;; Go through all types.
4205       (while types
4206         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4207         (when (symbol-value var)
4208           ;; This list has articles.  So we delete all missing articles
4209           ;; from it.
4210           (setq m missing)
4211           (while m
4212             (set var (delq (pop m) (symbol-value var)))))))))
4213
4214 (defun gnus-update-marks ()
4215   "Enter the various lists of marked articles into the newsgroup info list."
4216   (let ((types gnus-article-mark-lists)
4217         (info (gnus-get-info gnus-newsgroup-name))
4218         (uncompressed '(score bookmark killed))
4219         type list newmarked symbol delta-marks)
4220     (when info
4221       ;; Add all marks lists that are non-nil to the list of marks lists.
4222       (while (setq type (pop types))
4223         (when (setq list (symbol-value
4224                           (setq symbol
4225                                 (intern (format "gnus-newsgroup-%s"
4226                                                 (car type))))))
4227
4228           ;; Get rid of the entries of the articles that have the
4229           ;; default score.
4230           (when (and (eq (cdr type) 'score)
4231                      gnus-save-score
4232                      list)
4233             (let* ((arts list)
4234                    (prev (cons nil list))
4235                    (all prev))
4236               (while arts
4237                 (if (or (not (consp (car arts)))
4238                         (= (cdar arts) gnus-summary-default-score))
4239                     (setcdr prev (cdr arts))
4240                   (setq prev arts))
4241                 (setq arts (cdr arts)))
4242               (setq list (cdr all))))
4243
4244          (when (gnus-check-backend-function 'request-set-mark
4245                                             gnus-newsgroup-name)
4246            ;; score & bookmark are not proper flags (they are cons cells)
4247            ;; cache is a internal gnus flag
4248            (unless (memq (cdr type) '(cache score bookmark))
4249              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4250                     (del (gnus-remove-from-range old list))
4251                     (add (gnus-remove-from-range list old)))
4252                (if add
4253                    (push (list add 'add (list (cdr type))) delta-marks))
4254                (if del
4255                    (push (list del 'del (list (cdr type))) delta-marks)))))
4256
4257           (push (cons (cdr type)
4258                       (if (memq (cdr type) uncompressed) list
4259                         (gnus-compress-sequence
4260                          (set symbol (sort list '<)) t)))
4261                 newmarked)))
4262
4263       (if delta-marks
4264          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4265
4266       ;; Enter these new marks into the info of the group.
4267       (if (nthcdr 3 info)
4268           (setcar (nthcdr 3 info) newmarked)
4269         ;; Add the marks lists to the end of the info.
4270         (when newmarked
4271           (setcdr (nthcdr 2 info) (list newmarked))))
4272
4273       ;; Cut off the end of the info if there's nothing else there.
4274       (let ((i 5))
4275         (while (and (> i 2)
4276                     (not (nth i info)))
4277           (when (nthcdr (decf i) info)
4278             (setcdr (nthcdr i info) nil)))))))
4279
4280 (defun gnus-set-mode-line (where)
4281   "This function sets the mode line of the article or summary buffers.
4282 If WHERE is `summary', the summary mode line format will be used."
4283   ;; Is this mode line one we keep updated?
4284   (when (and (memq where gnus-updated-mode-lines)
4285              (symbol-value
4286               (intern (format "gnus-%s-mode-line-format-spec" where))))
4287     (let (mode-string)
4288       (save-excursion
4289         ;; We evaluate this in the summary buffer since these
4290         ;; variables are buffer-local to that buffer.
4291         (set-buffer gnus-summary-buffer)
4292         ;; We bind all these variables that are used in the `eval' form
4293         ;; below.
4294         (let* ((mformat (symbol-value
4295                          (intern
4296                           (format "gnus-%s-mode-line-format-spec" where))))
4297                (gnus-tmp-group-name gnus-newsgroup-name)
4298                (gnus-tmp-article-number (or gnus-current-article 0))
4299                (gnus-tmp-unread gnus-newsgroup-unreads)
4300                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4301                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4302                (gnus-tmp-unread-and-unselected
4303                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4304                             (zerop gnus-tmp-unselected))
4305                        "")
4306                       ((zerop gnus-tmp-unselected)
4307                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4308                       (t (format "{%d(+%d) more}"
4309                                  gnus-tmp-unread-and-unticked
4310                                  gnus-tmp-unselected))))
4311                (gnus-tmp-subject
4312                 (if (and gnus-current-headers
4313                          (vectorp gnus-current-headers))
4314                     (gnus-mode-string-quote
4315                      (mail-header-subject gnus-current-headers))
4316                   ""))
4317                bufname-length max-len
4318                gnus-tmp-header);; passed as argument to any user-format-funcs
4319           (setq mode-string (eval mformat))
4320           (setq bufname-length (if (string-match "%b" mode-string)
4321                                    (- (length
4322                                        (buffer-name
4323                                         (if (eq where 'summary)
4324                                             nil
4325                                           (get-buffer gnus-article-buffer))))
4326                                       2)
4327                                  0))
4328           (setq max-len (max 4 (if gnus-mode-non-string-length
4329                                    (- (window-width)
4330                                       gnus-mode-non-string-length
4331                                       bufname-length)
4332                                  (length mode-string))))
4333           ;; We might have to chop a bit of the string off...
4334           (when (> (length mode-string) max-len)
4335             (setq mode-string
4336                   (concat (gnus-truncate-string mode-string (- max-len 3))
4337                           "...")))
4338           ;; Pad the mode string a bit.
4339           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4340       ;; Update the mode line.
4341       (setq mode-line-buffer-identification
4342             (gnus-mode-line-buffer-identification (list mode-string)))
4343       (set-buffer-modified-p t))))
4344
4345 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4346   "Go through the HEADERS list and add all Xrefs to a hash table.
4347 The resulting hash table is returned, or nil if no Xrefs were found."
4348   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4349          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4350          (xref-hashtb (gnus-make-hashtable))
4351          start group entry number xrefs header)
4352     (while headers
4353       (setq header (pop headers))
4354       (when (and (setq xrefs (mail-header-xref header))
4355                  (not (memq (setq number (mail-header-number header))
4356                             unreads)))
4357         (setq start 0)
4358         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4359           (setq start (match-end 0))
4360           (setq group (if prefix
4361                           (concat prefix (substring xrefs (match-beginning 1)
4362                                                     (match-end 1)))
4363                         (substring xrefs (match-beginning 1) (match-end 1))))
4364           (setq number
4365                 (string-to-int (substring xrefs (match-beginning 2)
4366                                           (match-end 2))))
4367           (if (setq entry (gnus-gethash group xref-hashtb))
4368               (setcdr entry (cons number (cdr entry)))
4369             (gnus-sethash group (cons number nil) xref-hashtb)))))
4370     (and start xref-hashtb)))
4371
4372 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4373   "Look through all the headers and mark the Xrefs as read."
4374   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4375         name entry info xref-hashtb idlist method nth4)
4376     (save-excursion
4377       (set-buffer gnus-group-buffer)
4378       (when (setq xref-hashtb
4379                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4380         (mapatoms
4381          (lambda (group)
4382            (unless (string= from-newsgroup (setq name (symbol-name group)))
4383              (setq idlist (symbol-value group))
4384              ;; Dead groups are not updated.
4385              (and (prog1
4386                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4387                             info (nth 2 entry))
4388                     (when (stringp (setq nth4 (gnus-info-method info)))
4389                       (setq nth4 (gnus-server-to-method nth4))))
4390                   ;; Only do the xrefs if the group has the same
4391                   ;; select method as the group we have just read.
4392                   (or (gnus-methods-equal-p
4393                        nth4 (gnus-find-method-for-group from-newsgroup))
4394                       virtual
4395                       (equal nth4 (setq method (gnus-find-method-for-group
4396                                                 from-newsgroup)))
4397                       (and (equal (car nth4) (car method))
4398                            (equal (nth 1 nth4) (nth 1 method))))
4399                   gnus-use-cross-reference
4400                   (or (not (eq gnus-use-cross-reference t))
4401                       virtual
4402                       ;; Only do cross-references on subscribed
4403                       ;; groups, if that is what is wanted.
4404                       (<= (gnus-info-level info) gnus-level-subscribed))
4405                   (gnus-group-make-articles-read name idlist))))
4406          xref-hashtb)))))
4407
4408 (defun gnus-compute-read-articles (group articles)
4409   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4410          (info (nth 2 entry))
4411          (active (gnus-active group))
4412          ninfo)
4413     (when entry
4414       ;; First peel off all invalid article numbers.
4415       (when active
4416         (let ((ids articles)
4417               id first)
4418           (while (setq id (pop ids))
4419             (when (and first (> id (cdr active)))
4420               ;; We'll end up in this situation in one particular
4421               ;; obscure situation.  If you re-scan a group and get
4422               ;; a new article that is cross-posted to a different
4423               ;; group that has not been re-scanned, you might get
4424               ;; crossposted article that has a higher number than
4425               ;; Gnus believes possible.  So we re-activate this
4426               ;; group as well.  This might mean doing the
4427               ;; crossposting thingy will *increase* the number
4428               ;; of articles in some groups.  Tsk, tsk.
4429               (setq active (or (gnus-activate-group group) active)))
4430             (when (or (> id (cdr active))
4431                       (< id (car active)))
4432               (setq articles (delq id articles))))))
4433       ;; If the read list is nil, we init it.
4434       (if (and active
4435                (null (gnus-info-read info))
4436                (> (car active) 1))
4437           (setq ninfo (cons 1 (1- (car active))))
4438         (setq ninfo (gnus-info-read info)))
4439       ;; Then we add the read articles to the range.
4440       (gnus-add-to-range
4441        ninfo (setq articles (sort articles '<))))))
4442
4443 (defun gnus-group-make-articles-read (group articles)
4444   "Update the info of GROUP to say that ARTICLES are read."
4445   (let* ((num 0)
4446          (entry (gnus-gethash group gnus-newsrc-hashtb))
4447          (info (nth 2 entry))
4448          (active (gnus-active group))
4449          range)
4450     (when entry
4451       (setq range (gnus-compute-read-articles group articles))
4452       (save-excursion
4453         (set-buffer gnus-group-buffer)
4454         (gnus-undo-register
4455           `(progn
4456              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4457              (gnus-info-set-read ',info ',(gnus-info-read info))
4458              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4459              (gnus-group-update-group ,group t))))
4460       ;; Add the read articles to the range.
4461       (gnus-info-set-read info range)
4462       ;; Then we have to re-compute how many unread
4463       ;; articles there are in this group.
4464       (when active
4465         (cond
4466          ((not range)
4467           (setq num (- (1+ (cdr active)) (car active))))
4468          ((not (listp (cdr range)))
4469           (setq num (- (cdr active) (- (1+ (cdr range))
4470                                        (car range)))))
4471          (t
4472           (while range
4473             (if (numberp (car range))
4474                 (setq num (1+ num))
4475               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4476             (setq range (cdr range)))
4477           (setq num (- (cdr active) num))))
4478         ;; Update the number of unread articles.
4479         (setcar entry num)
4480         ;; Update the group buffer.
4481         (gnus-group-update-group group t)))))
4482
4483 (defun gnus-methods-equal-p (m1 m2)
4484   (let ((m1 (or m1 gnus-select-method))
4485         (m2 (or m2 gnus-select-method)))
4486     (or (equal m1 m2)
4487         (and (eq (car m1) (car m2))
4488              (or (not (memq 'address (assoc (symbol-name (car m1))
4489                                             gnus-valid-select-methods)))
4490                  (equal (nth 1 m1) (nth 1 m2)))))))
4491
4492 (defvar gnus-newsgroup-none-id 0)
4493
4494 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4495   (let ((cur nntp-server-buffer)
4496         (dependencies
4497          (or dependencies
4498              (save-excursion (set-buffer gnus-summary-buffer)
4499                              gnus-newsgroup-dependencies)))
4500         headers id end ref
4501         (mail-parse-charset gnus-newsgroup-charset))
4502     (save-excursion
4503       (set-buffer nntp-server-buffer)
4504       ;; Translate all TAB characters into SPACE characters.
4505       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4506       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4507       (gnus-run-hooks 'gnus-parse-headers-hook)
4508       (let ((case-fold-search t)
4509             in-reply-to header p lines chars ctype)
4510         (goto-char (point-min))
4511         ;; Search to the beginning of the next header.  Error messages
4512         ;; do not begin with 2 or 3.
4513         (while (re-search-forward "^[23][0-9]+ " nil t)
4514           (setq id nil
4515                 ref nil)
4516           ;; This implementation of this function, with nine
4517           ;; search-forwards instead of the one re-search-forward and
4518           ;; a case (which basically was the old function) is actually
4519           ;; about twice as fast, even though it looks messier.  You
4520           ;; can't have everything, I guess.  Speed and elegance
4521           ;; doesn't always go hand in hand.
4522           (setq
4523            header
4524            (make-full-mail-header
4525             ;; Number.
4526             (prog1
4527                 (read cur)
4528               (end-of-line)
4529               (setq p (point))
4530               (narrow-to-region (point)
4531                                 (or (and (search-forward "\n.\n" nil t)
4532                                          (- (point) 2))
4533                                     (point))))
4534             ;; Subject.
4535             (progn
4536               (goto-char p)
4537               (if (search-forward "\nsubject: " nil t)
4538                   (buffer-substring (match-end 0) (std11-field-end))
4539                 "(none)"))
4540             ;; From.
4541             (progn
4542               (goto-char p)
4543               (if (search-forward "\nfrom: " nil t)
4544                   (buffer-substring (match-end 0) (std11-field-end))
4545                 "(nobody)"))
4546             ;; Date.
4547             (progn
4548               (goto-char p)
4549               (if (search-forward "\ndate: " nil t)
4550                   (buffer-substring (match-end 0) (std11-field-end))
4551                 ""))
4552             ;; Message-ID.
4553             (progn
4554               (goto-char p)
4555               (setq id (if (re-search-forward
4556                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4557                            ;; We do it this way to make sure the Message-ID
4558                            ;; is (somewhat) syntactically valid.
4559                            (buffer-substring (match-beginning 1)
4560                                              (match-end 1))
4561                          ;; If there was no message-id, we just fake one
4562                          ;; to make subsequent routines simpler.
4563                          (nnheader-generate-fake-message-id))))
4564             ;; References.
4565             (progn
4566               (goto-char p)
4567               (if (search-forward "\nreferences: " nil t)
4568                   (progn
4569                     (setq end (point))
4570                     (prog1
4571                         (buffer-substring (match-end 0) (std11-field-end))
4572                       (setq ref
4573                             (buffer-substring
4574                              (progn
4575                                ;; (end-of-line)
4576                                (search-backward ">" end t)
4577                                (1+ (point)))
4578                              (progn
4579                                (search-backward "<" end t)
4580                                (point))))))
4581                 ;; Get the references from the in-reply-to header if there
4582                 ;; were no references and the in-reply-to header looks
4583                 ;; promising.
4584                 (if (and (search-forward "\nin-reply-to: " nil t)
4585                          (setq in-reply-to
4586                                (buffer-substring (match-end 0)
4587                                                  (std11-field-end)))
4588                          (string-match "<[^>]+>" in-reply-to))
4589                     (let (ref2)
4590                       (setq ref (substring in-reply-to (match-beginning 0)
4591                                            (match-end 0)))
4592                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4593                         (setq ref2 (substring in-reply-to (match-beginning 0)
4594                                               (match-end 0)))
4595                         (when (> (length ref2) (length ref))
4596                           (setq ref ref2)))
4597                       ref)
4598                   (setq ref nil))))
4599             ;; Chars.
4600             (progn
4601               (goto-char p)
4602               (if (search-forward "\nchars: " nil t)
4603                   (if (numberp (setq chars (ignore-errors (read cur))))
4604                       chars 0)
4605                 0))
4606             ;; Lines.
4607             (progn
4608               (goto-char p)
4609               (if (search-forward "\nlines: " nil t)
4610                   (if (numberp (setq lines (ignore-errors (read cur))))
4611                       lines 0)
4612                 0))
4613             ;; Xref.
4614             (progn
4615               (goto-char p)
4616               (and (search-forward "\nxref: " nil t)
4617                    (buffer-substring (match-end 0) (std11-field-end))))
4618             ;; Extra.
4619             (when gnus-extra-headers
4620               (let ((extra gnus-extra-headers)
4621                     out)
4622                 (while extra
4623                   (goto-char p)
4624                   (when (search-forward
4625                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4626                     (push (cons (car extra)
4627                                 (buffer-substring (match-end 0)
4628                                                   (std11-field-end)))
4629                           out))
4630                   (pop extra))
4631                 out))))
4632           (goto-char p)
4633           (if (and (search-forward "\ncontent-type: " nil t)
4634                    (setq ctype
4635                          (buffer-substring (match-end 0) (std11-field-end))))
4636               (mime-entity-set-content-type-internal
4637                header (mime-parse-Content-Type ctype)))
4638           (when (equal id ref)
4639             (setq ref nil))
4640
4641           (when gnus-alter-header-function
4642             (funcall gnus-alter-header-function header)
4643             (setq id (mail-header-id header)
4644                   ref (gnus-parent-id (mail-header-references header))))
4645
4646           (when (setq header
4647                       (gnus-dependencies-add-header
4648                        header dependencies force-new))
4649             (push header headers))
4650           (goto-char (point-max))
4651           (widen))
4652         (nreverse headers)))))
4653
4654 ;; Goes through the xover lines and returns a list of vectors
4655 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4656                                                   force-new dependencies
4657                                                   group also-fetch-heads)
4658   "Parse the news overview data in the server buffer, and return a
4659 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4660   ;; Get the Xref when the users reads the articles since most/some
4661   ;; NNTP servers do not include Xrefs when using XOVER.
4662   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4663   (let ((mail-parse-charset gnus-newsgroup-charset)
4664         (cur nntp-server-buffer)
4665         (dependencies (or dependencies gnus-newsgroup-dependencies))
4666         number headers header)
4667     (save-excursion
4668       (set-buffer nntp-server-buffer)
4669       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4670       ;; Allow the user to mangle the headers before parsing them.
4671       (gnus-run-hooks 'gnus-parse-headers-hook)
4672       (goto-char (point-min))
4673       (while (not (eobp))
4674         (condition-case ()
4675             (while (and sequence (not (eobp)))
4676               (setq number (read cur))
4677               (while (and sequence
4678                           (< (car sequence) number))
4679                 (setq sequence (cdr sequence)))
4680               (and sequence
4681                    (eq number (car sequence))
4682                    (progn
4683                      (setq sequence (cdr sequence))
4684                      (setq header (inline
4685                                     (gnus-nov-parse-line
4686                                      number dependencies force-new))))
4687                    (push header headers))
4688               (forward-line 1))
4689           (error
4690            (gnus-error 4 "Strange nov line (%d)"
4691                        (count-lines (point-min) (point)))))
4692         (forward-line 1))
4693       ;; A common bug in inn is that if you have posted an article and
4694       ;; then retrieves the active file, it will answer correctly --
4695       ;; the new article is included.  However, a NOV entry for the
4696       ;; article may not have been generated yet, so this may fail.
4697       ;; We work around this problem by retrieving the last few
4698       ;; headers using HEAD.
4699       (if (or (not also-fetch-heads)
4700               (not sequence))
4701           ;; We (probably) got all the headers.
4702           (nreverse headers)
4703         (let ((gnus-nov-is-evil t))
4704           (nconc
4705            (nreverse headers)
4706            (gnus-retrieve-parsed-headers sequence group)
4707            ))))))
4708
4709 (defun gnus-article-get-xrefs ()
4710   "Fill in the Xref value in `gnus-current-headers', if necessary.
4711 This is meant to be called in `gnus-article-internal-prepare-hook'."
4712   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4713                                  gnus-current-headers)))
4714     (or (not gnus-use-cross-reference)
4715         (not headers)
4716         (and (mail-header-xref headers)
4717              (not (string= (mail-header-xref headers) "")))
4718         (let ((case-fold-search t)
4719               xref)
4720           (save-restriction
4721             (nnheader-narrow-to-headers)
4722             (goto-char (point-min))
4723             (when (or (and (eq (downcase (char-after)) ?x)
4724                            (looking-at "Xref:"))
4725                       (search-forward "\nXref:" nil t))
4726               (goto-char (1+ (match-end 0)))
4727               (setq xref (buffer-substring (point)
4728                                            (progn (end-of-line) (point))))
4729               (mail-header-set-xref headers xref)))))))
4730
4731 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4732   "Find article ID and insert the summary line for that article.
4733 OLD-HEADER can either be a header or a line number to insert
4734 the subject line on."
4735   (let* ((line (and (numberp old-header) old-header))
4736          (old-header (and (vectorp old-header) old-header))
4737          (header (cond ((and old-header use-old-header)
4738                         old-header)
4739                        ((and (numberp id)
4740                              (gnus-number-to-header id))
4741                         (gnus-number-to-header id))
4742                        (t
4743                         (gnus-read-header id))))
4744          (number (and (numberp id) id))
4745          d)
4746     (when header
4747       ;; Rebuild the thread that this article is part of and go to the
4748       ;; article we have fetched.
4749       (when (and (not gnus-show-threads)
4750                  old-header)
4751         (when (and number
4752                    (setq d (gnus-data-find (mail-header-number old-header))))
4753           (goto-char (gnus-data-pos d))
4754           (gnus-data-remove
4755            number
4756            (- (gnus-point-at-bol)
4757               (prog1
4758                   (1+ (gnus-point-at-eol))
4759                 (gnus-delete-line))))))
4760       (when old-header
4761         (mail-header-set-number header (mail-header-number old-header)))
4762       (setq gnus-newsgroup-sparse
4763             (delq (setq number (mail-header-number header))
4764                   gnus-newsgroup-sparse))
4765       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4766       (push number gnus-newsgroup-limit)
4767       (gnus-rebuild-thread (mail-header-id header) line)
4768       (gnus-summary-goto-subject number nil t))
4769     (when (and (numberp number)
4770                (> number 0))
4771       ;; We have to update the boundaries even if we can't fetch the
4772       ;; article if ID is a number -- so that the next `P' or `N'
4773       ;; command will fetch the previous (or next) article even
4774       ;; if the one we tried to fetch this time has been canceled.
4775       (when (> number gnus-newsgroup-end)
4776         (setq gnus-newsgroup-end number))
4777       (when (< number gnus-newsgroup-begin)
4778         (setq gnus-newsgroup-begin number))
4779       (setq gnus-newsgroup-unselected
4780             (delq number gnus-newsgroup-unselected)))
4781     ;; Report back a success?
4782     (and header (mail-header-number header))))
4783
4784 ;;; Process/prefix in the summary buffer
4785
4786 (defun gnus-summary-work-articles (n)
4787   "Return a list of articles to be worked upon.
4788 The prefix argument, the list of process marked articles, and the
4789 current article will be taken into consideration."
4790   (save-excursion
4791     (set-buffer gnus-summary-buffer)
4792     (cond
4793      (n
4794       ;; A numerical prefix has been given.
4795       (setq n (prefix-numeric-value n))
4796       (let ((backward (< n 0))
4797             (n (abs (prefix-numeric-value n)))
4798             articles article)
4799         (save-excursion
4800           (while
4801               (and (> n 0)
4802                    (push (setq article (gnus-summary-article-number))
4803                          articles)
4804                    (if backward
4805                        (gnus-summary-find-prev nil article)
4806                      (gnus-summary-find-next nil article)))
4807             (decf n)))
4808         (nreverse articles)))
4809      ((and (gnus-region-active-p) (mark))
4810       (message "region active")
4811       ;; Work on the region between point and mark.
4812       (let ((max (max (point) (mark)))
4813             articles article)
4814         (save-excursion
4815           (goto-char (min (point) (mark)))
4816           (while
4817               (and
4818                (push (setq article (gnus-summary-article-number)) articles)
4819                (gnus-summary-find-next nil article)
4820                (< (point) max)))
4821           (nreverse articles))))
4822      (gnus-newsgroup-processable
4823       ;; There are process-marked articles present.
4824       ;; Save current state.
4825       (gnus-summary-save-process-mark)
4826       ;; Return the list.
4827       (reverse gnus-newsgroup-processable))
4828      (t
4829       ;; Just return the current article.
4830       (list (gnus-summary-article-number))))))
4831
4832 (defmacro gnus-summary-iterate (arg &rest forms)
4833   "Iterate over the process/prefixed articles and do FORMS.
4834 ARG is the interactive prefix given to the command.  FORMS will be
4835 executed with point over the summary line of the articles."
4836   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4837     `(let ((,articles (gnus-summary-work-articles ,arg)))
4838        (while ,articles
4839          (gnus-summary-goto-subject (car ,articles))
4840          ,@forms))))
4841
4842 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4843 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4844
4845 (defun gnus-summary-save-process-mark ()
4846   "Push the current set of process marked articles on the stack."
4847   (interactive)
4848   (push (copy-sequence gnus-newsgroup-processable)
4849         gnus-newsgroup-process-stack))
4850
4851 (defun gnus-summary-kill-process-mark ()
4852   "Push the current set of process marked articles on the stack and unmark."
4853   (interactive)
4854   (gnus-summary-save-process-mark)
4855   (gnus-summary-unmark-all-processable))
4856
4857 (defun gnus-summary-yank-process-mark ()
4858   "Pop the last process mark state off the stack and restore it."
4859   (interactive)
4860   (unless gnus-newsgroup-process-stack
4861     (error "Empty mark stack"))
4862   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4863
4864 (defun gnus-summary-process-mark-set (set)
4865   "Make SET into the current process marked articles."
4866   (gnus-summary-unmark-all-processable)
4867   (while set
4868     (gnus-summary-set-process-mark (pop set))))
4869
4870 ;;; Searching and stuff
4871
4872 (defun gnus-summary-search-group (&optional backward use-level)
4873   "Search for next unread newsgroup.
4874 If optional argument BACKWARD is non-nil, search backward instead."
4875   (save-excursion
4876     (set-buffer gnus-group-buffer)
4877     (when (gnus-group-search-forward
4878            backward nil (if use-level (gnus-group-group-level) nil))
4879       (gnus-group-group-name))))
4880
4881 (defun gnus-summary-best-group (&optional exclude-group)
4882   "Find the name of the best unread group.
4883 If EXCLUDE-GROUP, do not go to this group."
4884   (save-excursion
4885     (set-buffer gnus-group-buffer)
4886     (save-excursion
4887       (gnus-group-best-unread-group exclude-group))))
4888
4889 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4890   (if backward (gnus-summary-find-prev)
4891     (let* ((dummy (gnus-summary-article-intangible-p))
4892            (article (or article (gnus-summary-article-number)))
4893            (arts (gnus-data-find-list article))
4894            result)
4895       (when (and (not dummy)
4896                  (or (not gnus-summary-check-current)
4897                      (not unread)
4898                      (not (gnus-data-unread-p (car arts)))))
4899         (setq arts (cdr arts)))
4900       (when (setq result
4901                   (if unread
4902                       (progn
4903                         (while arts
4904                           (when (or (and undownloaded
4905                                          (eq gnus-undownloaded-mark
4906                                              (gnus-data-mark (car arts))))
4907                                     (gnus-data-unread-p (car arts)))
4908                             (setq result (car arts)
4909                                   arts nil))
4910                           (setq arts (cdr arts)))
4911                         result)
4912                     (car arts)))
4913         (goto-char (gnus-data-pos result))
4914         (gnus-data-number result)))))
4915
4916 (defun gnus-summary-find-prev (&optional unread article)
4917   (let* ((eobp (eobp))
4918          (article (or article (gnus-summary-article-number)))
4919          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4920          result)
4921     (when (and (not eobp)
4922                (or (not gnus-summary-check-current)
4923                    (not unread)
4924                    (not (gnus-data-unread-p (car arts)))))
4925       (setq arts (cdr arts)))
4926     (when (setq result
4927                 (if unread
4928                     (progn
4929                       (while arts
4930                         (when (gnus-data-unread-p (car arts))
4931                           (setq result (car arts)
4932                                 arts nil))
4933                         (setq arts (cdr arts)))
4934                       result)
4935                   (car arts)))
4936       (goto-char (gnus-data-pos result))
4937       (gnus-data-number result))))
4938
4939 (defun gnus-summary-find-subject (subject &optional unread backward article)
4940   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4941          (article (or article (gnus-summary-article-number)))
4942          (articles (gnus-data-list backward))
4943          (arts (gnus-data-find-list article articles))
4944          result)
4945     (when (or (not gnus-summary-check-current)
4946               (not unread)
4947               (not (gnus-data-unread-p (car arts))))
4948       (setq arts (cdr arts)))
4949     (while arts
4950       (and (or (not unread)
4951                (gnus-data-unread-p (car arts)))
4952            (vectorp (gnus-data-header (car arts)))
4953            (gnus-subject-equal
4954             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4955            (setq result (car arts)
4956                  arts nil))
4957       (setq arts (cdr arts)))
4958     (and result
4959          (goto-char (gnus-data-pos result))
4960          (gnus-data-number result))))
4961
4962 (defun gnus-summary-search-forward (&optional unread subject backward)
4963   "Search forward for an article.
4964 If UNREAD, look for unread articles.  If SUBJECT, look for
4965 articles with that subject.  If BACKWARD, search backward instead."
4966   (cond (subject (gnus-summary-find-subject subject unread backward))
4967         (backward (gnus-summary-find-prev unread))
4968         (t (gnus-summary-find-next unread))))
4969
4970 (defun gnus-recenter (&optional n)
4971   "Center point in window and redisplay frame.
4972 Also do horizontal recentering."
4973   (interactive "P")
4974   (when (and gnus-auto-center-summary
4975              (not (eq gnus-auto-center-summary 'vertical)))
4976     (gnus-horizontal-recenter))
4977   (recenter n))
4978
4979 (defun gnus-summary-recenter ()
4980   "Center point in the summary window.
4981 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4982 displayed, no centering will be performed."
4983   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4984   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4985   (let* ((top (cond ((< (window-height) 4) 0)
4986                     ((< (window-height) 7) 1)
4987                     (t (if (numberp gnus-auto-center-summary)
4988                            gnus-auto-center-summary
4989                          2))))
4990          (height (1- (window-height)))
4991          (bottom (save-excursion (goto-char (point-max))
4992                                  (forward-line (- height))
4993                                  (point)))
4994          (window (get-buffer-window (current-buffer))))
4995     ;; The user has to want it.
4996     (when gnus-auto-center-summary
4997       (when (get-buffer-window gnus-article-buffer)
4998         ;; Only do recentering when the article buffer is displayed,
4999         ;; Set the window start to either `bottom', which is the biggest
5000         ;; possible valid number, or the second line from the top,
5001         ;; whichever is the least.
5002         (set-window-start
5003          window (min bottom (save-excursion
5004                               (forward-line (- top)) (point)))))
5005       ;; Do horizontal recentering while we're at it.
5006       (when (and (get-buffer-window (current-buffer) t)
5007                  (not (eq gnus-auto-center-summary 'vertical)))
5008         (let ((selected (selected-window)))
5009           (select-window (get-buffer-window (current-buffer) t))
5010           (gnus-summary-position-point)
5011           (gnus-horizontal-recenter)
5012           (select-window selected))))))
5013
5014 (defun gnus-summary-jump-to-group (newsgroup)
5015   "Move point to NEWSGROUP in group mode buffer."
5016   ;; Keep update point of group mode buffer if visible.
5017   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5018       (save-window-excursion
5019         ;; Take care of tree window mode.
5020         (when (get-buffer-window gnus-group-buffer)
5021           (pop-to-buffer gnus-group-buffer))
5022         (gnus-group-jump-to-group newsgroup))
5023     (save-excursion
5024       ;; Take care of tree window mode.
5025       (if (get-buffer-window gnus-group-buffer)
5026           (pop-to-buffer gnus-group-buffer)
5027         (set-buffer gnus-group-buffer))
5028       (gnus-group-jump-to-group newsgroup))))
5029
5030 ;; This function returns a list of article numbers based on the
5031 ;; difference between the ranges of read articles in this group and
5032 ;; the range of active articles.
5033 (defun gnus-list-of-unread-articles (group)
5034   (let* ((read (gnus-info-read (gnus-get-info group)))
5035          (active (or (gnus-active group) (gnus-activate-group group)))
5036          (last (cdr active))
5037          first nlast unread)
5038     ;; If none are read, then all are unread.
5039     (if (not read)
5040         (setq first (car active))
5041       ;; If the range of read articles is a single range, then the
5042       ;; first unread article is the article after the last read
5043       ;; article.  Sounds logical, doesn't it?
5044       (if (not (listp (cdr read)))
5045           (setq first (max (car active) (1+ (cdr read))))
5046         ;; `read' is a list of ranges.
5047         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5048                                   (caar read)))
5049                   1)
5050           (setq first (car active)))
5051         (while read
5052           (when first
5053             (while (< first nlast)
5054               (push first unread)
5055               (setq first (1+ first))))
5056           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5057           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5058           (setq read (cdr read)))))
5059     ;; And add the last unread articles.
5060     (while (<= first last)
5061       (push first unread)
5062       (setq first (1+ first)))
5063     ;; Return the list of unread articles.
5064     (delq 0 (nreverse unread))))
5065
5066 (defun gnus-list-of-read-articles (group)
5067   "Return a list of unread, unticked and non-dormant articles."
5068   (let* ((info (gnus-get-info group))
5069          (marked (gnus-info-marks info))
5070          (active (gnus-active group)))
5071     (and info active
5072          (gnus-set-difference
5073           (gnus-sorted-complement
5074            (gnus-uncompress-range active)
5075            (gnus-list-of-unread-articles group))
5076           (append
5077            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5078            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5079
5080 ;; Various summary commands
5081
5082 (defun gnus-summary-select-article-buffer ()
5083   "Reconfigure windows to show article buffer."
5084   (interactive)
5085   (if (not (gnus-buffer-live-p gnus-article-buffer))
5086       (error "There is no article buffer for this summary buffer")
5087     (gnus-configure-windows 'article)
5088     (select-window (get-buffer-window gnus-article-buffer))))
5089
5090 (defun gnus-summary-universal-argument (arg)
5091   "Perform any operation on all articles that are process/prefixed."
5092   (interactive "P")
5093   (let ((articles (gnus-summary-work-articles arg))
5094         func article)
5095     (if (eq
5096          (setq
5097           func
5098           (key-binding
5099            (read-key-sequence
5100             (substitute-command-keys
5101              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5102              ))))
5103          'undefined)
5104         (gnus-error 1 "Undefined key")
5105       (save-excursion
5106         (while articles
5107           (gnus-summary-goto-subject (setq article (pop articles)))
5108           (let (gnus-newsgroup-processable)
5109             (command-execute func))
5110           (gnus-summary-remove-process-mark article)))))
5111   (gnus-summary-position-point))
5112
5113 (defun gnus-summary-toggle-truncation (&optional arg)
5114   "Toggle truncation of summary lines.
5115 With arg, turn line truncation on iff arg is positive."
5116   (interactive "P")
5117   (setq truncate-lines
5118         (if (null arg) (not truncate-lines)
5119           (> (prefix-numeric-value arg) 0)))
5120   (redraw-display))
5121
5122 (defun gnus-summary-reselect-current-group (&optional all rescan)
5123   "Exit and then reselect the current newsgroup.
5124 The prefix argument ALL means to select all articles."
5125   (interactive "P")
5126   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5127     (error "Ephemeral groups can't be reselected"))
5128   (let ((current-subject (gnus-summary-article-number))
5129         (group gnus-newsgroup-name))
5130     (setq gnus-newsgroup-begin nil)
5131     (gnus-summary-exit)
5132     ;; We have to adjust the point of group mode buffer because
5133     ;; point was moved to the next unread newsgroup by exiting.
5134     (gnus-summary-jump-to-group group)
5135     (when rescan
5136       (save-excursion
5137         (gnus-group-get-new-news-this-group 1)))
5138     (gnus-group-read-group all t)
5139     (gnus-summary-goto-subject current-subject nil t)))
5140
5141 (defun gnus-summary-rescan-group (&optional all)
5142   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5143   (interactive "P")
5144   (gnus-summary-reselect-current-group all t))
5145
5146 (defun gnus-summary-update-info (&optional non-destructive)
5147   (save-excursion
5148     (let ((group gnus-newsgroup-name))
5149       (when group
5150         (when gnus-newsgroup-kill-headers
5151           (setq gnus-newsgroup-killed
5152                 (gnus-compress-sequence
5153                  (nconc
5154                   (gnus-set-sorted-intersection
5155                    (gnus-uncompress-range gnus-newsgroup-killed)
5156                    (setq gnus-newsgroup-unselected
5157                          (sort gnus-newsgroup-unselected '<)))
5158                   (setq gnus-newsgroup-unreads
5159                         (sort gnus-newsgroup-unreads '<)))
5160                  t)))
5161         (unless (listp (cdr gnus-newsgroup-killed))
5162           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5163         (let ((headers gnus-newsgroup-headers))
5164           ;; Set the new ranges of read articles.
5165           (save-excursion
5166             (set-buffer gnus-group-buffer)
5167             (gnus-undo-force-boundary))
5168           (gnus-update-read-articles
5169            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5170           ;; Set the current article marks.
5171           (let ((gnus-newsgroup-scored
5172                  (if (and (not gnus-save-score)
5173                           (not non-destructive))
5174                      nil
5175                    gnus-newsgroup-scored)))
5176             (save-excursion
5177               (gnus-update-marks)))
5178           ;; Do the cross-ref thing.
5179           (when gnus-use-cross-reference
5180             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5181           ;; Do not switch windows but change the buffer to work.
5182           (set-buffer gnus-group-buffer)
5183           (unless (gnus-ephemeral-group-p group)
5184             (gnus-group-update-group group)))))))
5185
5186 (defun gnus-summary-save-newsrc (&optional force)
5187   "Save the current number of read/marked articles in the dribble buffer.
5188 The dribble buffer will then be saved.
5189 If FORCE (the prefix), also save the .newsrc file(s)."
5190   (interactive "P")
5191   (gnus-summary-update-info t)
5192   (if force
5193       (gnus-save-newsrc-file)
5194     (gnus-dribble-save)))
5195
5196 (defun gnus-summary-exit (&optional temporary)
5197   "Exit reading current newsgroup, and then return to group selection mode.
5198 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5199   (interactive)
5200   (gnus-set-global-variables)
5201   (gnus-kill-save-kill-buffer)
5202   (gnus-async-halt-prefetch)
5203   (let* ((group gnus-newsgroup-name)
5204          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5205          (mode major-mode)
5206          (group-point nil)
5207          (buf (current-buffer)))
5208     (unless quit-config
5209       ;; Do adaptive scoring, and possibly save score files.
5210       (when gnus-newsgroup-adaptive
5211         (gnus-score-adaptive))
5212       (when gnus-use-scoring
5213         (gnus-score-save)))
5214     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5215     ;; If we have several article buffers, we kill them at exit.
5216     (unless gnus-single-article-buffer
5217       (gnus-kill-buffer gnus-original-article-buffer)
5218       (setq gnus-article-current nil))
5219     (when gnus-use-cache
5220       (gnus-cache-possibly-remove-articles)
5221       (gnus-cache-save-buffers))
5222     (gnus-async-prefetch-remove-group group)
5223     (when gnus-suppress-duplicates
5224       (gnus-dup-enter-articles))
5225     (when gnus-use-trees
5226       (gnus-tree-close group))
5227     ;; Remove entries for this group.
5228     (nnmail-purge-split-history (gnus-group-real-name group))
5229     ;; Make all changes in this group permanent.
5230     (unless quit-config
5231       (gnus-run-hooks 'gnus-exit-group-hook)
5232       (gnus-summary-update-info))
5233     (gnus-close-group group)
5234     ;; Make sure where we were, and go to next newsgroup.
5235     (set-buffer gnus-group-buffer)
5236     (unless quit-config
5237       (gnus-group-jump-to-group group))
5238     (gnus-run-hooks 'gnus-summary-exit-hook)
5239     (unless (or quit-config
5240                 ;; If this group has disappeared from the summary
5241                 ;; buffer, don't skip forwards.
5242                 (not (string= group (gnus-group-group-name))))
5243       (gnus-group-next-unread-group 1))
5244     (setq group-point (point))
5245     (if temporary
5246         nil                             ;Nothing to do.
5247       ;; If we have several article buffers, we kill them at exit.
5248       (unless gnus-single-article-buffer
5249         (gnus-kill-buffer gnus-article-buffer)
5250         (gnus-kill-buffer gnus-original-article-buffer)
5251         (setq gnus-article-current nil))
5252       (set-buffer buf)
5253       (if (not gnus-kill-summary-on-exit)
5254           (gnus-deaden-summary)
5255         ;; We set all buffer-local variables to nil.  It is unclear why
5256         ;; this is needed, but if we don't, buffer-local variables are
5257         ;; not garbage-collected, it seems.  This would the lead to en
5258         ;; ever-growing Emacs.
5259         (gnus-summary-clear-local-variables)
5260         (when (get-buffer gnus-article-buffer)
5261           (bury-buffer gnus-article-buffer))
5262         ;; We clear the global counterparts of the buffer-local
5263         ;; variables as well, just to be on the safe side.
5264         (set-buffer gnus-group-buffer)
5265         (gnus-summary-clear-local-variables)
5266         ;; Return to group mode buffer.
5267         (when (eq mode 'gnus-summary-mode)
5268           (gnus-kill-buffer buf)))
5269       (setq gnus-current-select-method gnus-select-method)
5270       (pop-to-buffer gnus-group-buffer)
5271       (if (not quit-config)
5272           (progn
5273             (goto-char group-point)
5274             (gnus-configure-windows 'group 'force))
5275         (gnus-handle-ephemeral-exit quit-config))
5276       ;; Clear the current group name.
5277       (unless quit-config
5278         (setq gnus-newsgroup-name nil)))))
5279
5280 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5281 (defun gnus-summary-exit-no-update (&optional no-questions)
5282   "Quit reading current newsgroup without updating read article info."
5283   (interactive)
5284   (let* ((group gnus-newsgroup-name)
5285          (quit-config (gnus-group-quit-config group)))
5286     (when (or no-questions
5287               gnus-expert-user
5288               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5289       (gnus-async-halt-prefetch)
5290       (mapcar 'funcall
5291               (delq 'gnus-summary-expire-articles
5292                     (copy-list gnus-summary-prepare-exit-hook)))
5293       ;; If we have several article buffers, we kill them at exit.
5294       (unless gnus-single-article-buffer
5295         (gnus-kill-buffer gnus-article-buffer)
5296         (gnus-kill-buffer gnus-original-article-buffer)
5297         (setq gnus-article-current nil))
5298       (if (not gnus-kill-summary-on-exit)
5299           (gnus-deaden-summary)
5300         (gnus-close-group group)
5301         (gnus-summary-clear-local-variables)
5302         (set-buffer gnus-group-buffer)
5303         (gnus-summary-clear-local-variables)
5304         (when (get-buffer gnus-summary-buffer)
5305           (kill-buffer gnus-summary-buffer)))
5306       (unless gnus-single-article-buffer
5307         (setq gnus-article-current nil))
5308       (when gnus-use-trees
5309         (gnus-tree-close group))
5310       (gnus-async-prefetch-remove-group group)
5311       (when (get-buffer gnus-article-buffer)
5312         (bury-buffer gnus-article-buffer))
5313       ;; Return to the group buffer.
5314       (gnus-configure-windows 'group 'force)
5315       ;; Clear the current group name.
5316       (setq gnus-newsgroup-name nil)
5317       (when (equal (gnus-group-group-name) group)
5318         (gnus-group-next-unread-group 1))
5319       (when quit-config
5320         (gnus-handle-ephemeral-exit quit-config)))))
5321
5322 (defun gnus-handle-ephemeral-exit (quit-config)
5323   "Handle movement when leaving an ephemeral group.
5324 The state which existed when entering the ephemeral is reset."
5325   (if (not (buffer-name (car quit-config)))
5326       (gnus-configure-windows 'group 'force)
5327     (set-buffer (car quit-config))
5328     (cond ((eq major-mode 'gnus-summary-mode)
5329            (gnus-set-global-variables))
5330           ((eq major-mode 'gnus-article-mode)
5331            (save-excursion
5332              ;; The `gnus-summary-buffer' variable may point
5333              ;; to the old summary buffer when using a single
5334              ;; article buffer.
5335              (unless (gnus-buffer-live-p gnus-summary-buffer)
5336                (set-buffer gnus-group-buffer))
5337              (set-buffer gnus-summary-buffer)
5338              (gnus-set-global-variables))))
5339     (if (or (eq (cdr quit-config) 'article)
5340             (eq (cdr quit-config) 'pick))
5341         (progn
5342           ;; The current article may be from the ephemeral group
5343           ;; thus it is best that we reload this article
5344           (gnus-summary-show-article)
5345           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5346               (gnus-configure-windows 'pick 'force)
5347             (gnus-configure-windows (cdr quit-config) 'force)))
5348       (gnus-configure-windows (cdr quit-config) 'force))
5349     (when (eq major-mode 'gnus-summary-mode)
5350       (gnus-summary-next-subject 1 nil t)
5351       (gnus-summary-recenter)
5352       (gnus-summary-position-point))))
5353
5354 (defun gnus-summary-preview-mime-message ()
5355   "MIME decode and play this message."
5356   (interactive)
5357   (let ((gnus-break-pages nil)
5358         (gnus-show-mime t))
5359     (gnus-summary-select-article gnus-show-all-headers t))
5360   (select-window (get-buffer-window gnus-article-buffer)))
5361
5362 ;;; Dead summaries.
5363
5364 (defvar gnus-dead-summary-mode-map nil)
5365
5366 (unless gnus-dead-summary-mode-map
5367   (setq gnus-dead-summary-mode-map (make-keymap))
5368   (suppress-keymap gnus-dead-summary-mode-map)
5369   (substitute-key-definition
5370    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5371   (let ((keys '("\C-d" "\r" "\177" [delete])))
5372     (while keys
5373       (define-key gnus-dead-summary-mode-map
5374         (pop keys) 'gnus-summary-wake-up-the-dead))))
5375
5376 (defvar gnus-dead-summary-mode nil
5377   "Minor mode for Gnus summary buffers.")
5378
5379 (defun gnus-dead-summary-mode (&optional arg)
5380   "Minor mode for Gnus summary buffers."
5381   (interactive "P")
5382   (when (eq major-mode 'gnus-summary-mode)
5383     (make-local-variable 'gnus-dead-summary-mode)
5384     (setq gnus-dead-summary-mode
5385           (if (null arg) (not gnus-dead-summary-mode)
5386             (> (prefix-numeric-value arg) 0)))
5387     (when gnus-dead-summary-mode
5388       (gnus-add-minor-mode
5389        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5390
5391 (defun gnus-deaden-summary ()
5392   "Make the current summary buffer into a dead summary buffer."
5393   ;; Kill any previous dead summary buffer.
5394   (when (and gnus-dead-summary
5395              (buffer-name gnus-dead-summary))
5396     (save-excursion
5397       (set-buffer gnus-dead-summary)
5398       (when gnus-dead-summary-mode
5399         (kill-buffer (current-buffer)))))
5400   ;; Make this the current dead summary.
5401   (setq gnus-dead-summary (current-buffer))
5402   (gnus-dead-summary-mode 1)
5403   (let ((name (buffer-name)))
5404     (when (string-match "Summary" name)
5405       (rename-buffer
5406        (concat (substring name 0 (match-beginning 0)) "Dead "
5407                (substring name (match-beginning 0)))
5408        t))))
5409
5410 (defun gnus-kill-or-deaden-summary (buffer)
5411   "Kill or deaden the summary BUFFER."
5412   (save-excursion
5413     (when (and (buffer-name buffer)
5414                (not gnus-single-article-buffer))
5415       (save-excursion
5416         (set-buffer buffer)
5417         (gnus-kill-buffer gnus-article-buffer)
5418         (gnus-kill-buffer gnus-original-article-buffer)))
5419     (cond (gnus-kill-summary-on-exit
5420            (when (and gnus-use-trees
5421                       (gnus-buffer-exists-p buffer))
5422              (save-excursion
5423                (set-buffer buffer)
5424                (gnus-tree-close gnus-newsgroup-name)))
5425            (gnus-kill-buffer buffer))
5426           ((gnus-buffer-exists-p buffer)
5427            (save-excursion
5428              (set-buffer buffer)
5429              (gnus-deaden-summary))))))
5430
5431 (defun gnus-summary-wake-up-the-dead (&rest args)
5432   "Wake up the dead summary buffer."
5433   (interactive)
5434   (gnus-dead-summary-mode -1)
5435   (let ((name (buffer-name)))
5436     (when (string-match "Dead " name)
5437       (rename-buffer
5438        (concat (substring name 0 (match-beginning 0))
5439                (substring name (match-end 0)))
5440        t)))
5441   (gnus-message 3 "This dead summary is now alive again"))
5442
5443 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5444 (defun gnus-summary-fetch-faq (&optional faq-dir)
5445   "Fetch the FAQ for the current group.
5446 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5447 in."
5448   (interactive
5449    (list
5450     (when current-prefix-arg
5451       (completing-read
5452        "Faq dir: " (and (listp gnus-group-faq-directory)
5453                         (mapcar (lambda (file) (list file))
5454                                 gnus-group-faq-directory))))))
5455   (let (gnus-faq-buffer)
5456     (when (setq gnus-faq-buffer
5457                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5458       (gnus-configure-windows 'summary-faq))))
5459
5460 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5461 (defun gnus-summary-describe-group (&optional force)
5462   "Describe the current newsgroup."
5463   (interactive "P")
5464   (gnus-group-describe-group force gnus-newsgroup-name))
5465
5466 (defun gnus-summary-describe-briefly ()
5467   "Describe summary mode commands briefly."
5468   (interactive)
5469   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5470
5471 ;; Walking around group mode buffer from summary mode.
5472
5473 (defun gnus-summary-next-group (&optional no-article target-group backward)
5474   "Exit current newsgroup and then select next unread newsgroup.
5475 If prefix argument NO-ARTICLE is non-nil, no article is selected
5476 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5477 previous group instead."
5478   (interactive "P")
5479   ;; Stop pre-fetching.
5480   (gnus-async-halt-prefetch)
5481   (let ((current-group gnus-newsgroup-name)
5482         (current-buffer (current-buffer))
5483         entered)
5484     ;; First we semi-exit this group to update Xrefs and all variables.
5485     ;; We can't do a real exit, because the window conf must remain
5486     ;; the same in case the user is prompted for info, and we don't
5487     ;; want the window conf to change before that...
5488     (gnus-summary-exit t)
5489     (while (not entered)
5490       ;; Then we find what group we are supposed to enter.
5491       (set-buffer gnus-group-buffer)
5492       (gnus-group-jump-to-group current-group)
5493       (setq target-group
5494             (or target-group
5495                 (if (eq gnus-keep-same-level 'best)
5496                     (gnus-summary-best-group gnus-newsgroup-name)
5497                   (gnus-summary-search-group backward gnus-keep-same-level))))
5498       (if (not target-group)
5499           ;; There are no further groups, so we return to the group
5500           ;; buffer.
5501           (progn
5502             (gnus-message 5 "Returning to the group buffer")
5503             (setq entered t)
5504             (when (gnus-buffer-live-p current-buffer)
5505               (set-buffer current-buffer)
5506               (gnus-summary-exit))
5507             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5508         ;; We try to enter the target group.
5509         (gnus-group-jump-to-group target-group)
5510         (let ((unreads (gnus-group-group-unread)))
5511           (if (and (or (eq t unreads)
5512                        (and unreads (not (zerop unreads))))
5513                    (gnus-summary-read-group
5514                     target-group nil no-article
5515                     (and (buffer-name current-buffer) current-buffer)
5516                     nil backward))
5517               (setq entered t)
5518             (setq current-group target-group
5519                   target-group nil)))))))
5520
5521 (defun gnus-summary-prev-group (&optional no-article)
5522   "Exit current newsgroup and then select previous unread newsgroup.
5523 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5524   (interactive "P")
5525   (gnus-summary-next-group no-article nil t))
5526
5527 ;; Walking around summary lines.
5528
5529 (defun gnus-summary-first-subject (&optional unread undownloaded)
5530   "Go to the first unread subject.
5531 If UNREAD is non-nil, go to the first unread article.
5532 Returns the article selected or nil if there are no unread articles."
5533   (interactive "P")
5534   (prog1
5535       (cond
5536        ;; Empty summary.
5537        ((null gnus-newsgroup-data)
5538         (gnus-message 3 "No articles in the group")
5539         nil)
5540        ;; Pick the first article.
5541        ((not unread)
5542         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5543         (gnus-data-number (car gnus-newsgroup-data)))
5544        ;; No unread articles.
5545        ((null gnus-newsgroup-unreads)
5546         (gnus-message 3 "No more unread articles")
5547         nil)
5548        ;; Find the first unread article.
5549        (t
5550         (let ((data gnus-newsgroup-data))
5551           (while (and data
5552                       (and (not (and undownloaded
5553                                      (eq gnus-undownloaded-mark
5554                                          (gnus-data-mark (car data)))))
5555                            (not (gnus-data-unread-p (car data)))))
5556             (setq data (cdr data)))
5557           (when data
5558             (goto-char (gnus-data-pos (car data)))
5559             (gnus-data-number (car data))))))
5560     (gnus-summary-position-point)))
5561
5562 (defun gnus-summary-next-subject (n &optional unread dont-display)
5563   "Go to next N'th summary line.
5564 If N is negative, go to the previous N'th subject line.
5565 If UNREAD is non-nil, only unread articles are selected.
5566 The difference between N and the actual number of steps taken is
5567 returned."
5568   (interactive "p")
5569   (let ((backward (< n 0))
5570         (n (abs n)))
5571     (while (and (> n 0)
5572                 (if backward
5573                     (gnus-summary-find-prev unread)
5574                   (gnus-summary-find-next unread)))
5575       (gnus-summary-show-thread)
5576       (setq n (1- n)))
5577     (when (/= 0 n)
5578       (gnus-message 7 "No more%s articles"
5579                     (if unread " unread" "")))
5580     (unless dont-display
5581       (gnus-summary-recenter)
5582       (gnus-summary-position-point))
5583     n))
5584
5585 (defun gnus-summary-next-unread-subject (n)
5586   "Go to next N'th unread summary line."
5587   (interactive "p")
5588   (gnus-summary-next-subject n t))
5589
5590 (defun gnus-summary-prev-subject (n &optional unread)
5591   "Go to previous N'th summary line.
5592 If optional argument UNREAD is non-nil, only unread article is selected."
5593   (interactive "p")
5594   (gnus-summary-next-subject (- n) unread))
5595
5596 (defun gnus-summary-prev-unread-subject (n)
5597   "Go to previous N'th unread summary line."
5598   (interactive "p")
5599   (gnus-summary-next-subject (- n) t))
5600
5601 (defun gnus-summary-goto-subject (article &optional force silent)
5602   "Go the subject line of ARTICLE.
5603 If FORCE, also allow jumping to articles not currently shown."
5604   (interactive "nArticle number: ")
5605   (let ((b (point))
5606         (data (gnus-data-find article)))
5607     ;; We read in the article if we have to.
5608     (and (not data)
5609          force
5610          (gnus-summary-insert-subject
5611           article
5612           (if (or (numberp force) (vectorp force)) force)
5613           t)
5614          (setq data (gnus-data-find article)))
5615     (goto-char b)
5616     (if (not data)
5617         (progn
5618           (unless silent
5619             (gnus-message 3 "Can't find article %d" article))
5620           nil)
5621       (goto-char (gnus-data-pos data))
5622       (gnus-summary-position-point)
5623       article)))
5624
5625 ;; Walking around summary lines with displaying articles.
5626
5627 (defun gnus-summary-expand-window (&optional arg)
5628   "Make the summary buffer take up the entire Emacs frame.
5629 Given a prefix, will force an `article' buffer configuration."
5630   (interactive "P")
5631   (if arg
5632       (gnus-configure-windows 'article 'force)
5633     (gnus-configure-windows 'summary 'force)))
5634
5635 (defun gnus-summary-display-article (article &optional all-header)
5636   "Display ARTICLE in article buffer."
5637   (gnus-set-global-variables)
5638   (if (null article)
5639       nil
5640     (prog1
5641         (if gnus-summary-display-article-function
5642             (funcall gnus-summary-display-article-function article all-header)
5643           (gnus-article-prepare article all-header))
5644       (gnus-run-hooks 'gnus-select-article-hook)
5645       (when (and gnus-current-article
5646                  (not (zerop gnus-current-article)))
5647         (gnus-summary-goto-subject gnus-current-article))
5648       (gnus-summary-recenter)
5649       (when (and gnus-use-trees gnus-show-threads)
5650         (gnus-possibly-generate-tree article)
5651         (gnus-highlight-selected-tree article))
5652       ;; Successfully display article.
5653       (gnus-article-set-window-start
5654        (cdr (assq article gnus-newsgroup-bookmarks))))))
5655
5656 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5657   "Select the current article.
5658 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5659 non-nil, the article will be re-fetched even if it already present in
5660 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5661 be displayed."
5662   ;; Make sure we are in the summary buffer to work around bbdb bug.
5663   (unless (eq major-mode 'gnus-summary-mode)
5664     (set-buffer gnus-summary-buffer))
5665   (let ((article (or article (gnus-summary-article-number)))
5666         (all-headers (not (not all-headers))) ;Must be T or NIL.
5667         gnus-summary-display-article-function
5668         did)
5669     (and (not pseudo)
5670          (gnus-summary-article-pseudo-p article)
5671          (error "This is a pseudo-article"))
5672     (prog1
5673         (save-excursion
5674           (set-buffer gnus-summary-buffer)
5675           (if (or (and gnus-single-article-buffer
5676                        (or (null gnus-current-article)
5677                            (null gnus-article-current)
5678                            (null (get-buffer gnus-article-buffer))
5679                            (not (eq article (cdr gnus-article-current)))
5680                            (not (equal (car gnus-article-current)
5681                                        gnus-newsgroup-name))))
5682                   (and (not gnus-single-article-buffer)
5683                        (or (null gnus-current-article)
5684                            (not (eq gnus-current-article article))))
5685                   force)
5686               ;; The requested article is different from the current article.
5687               (prog1
5688                   (gnus-summary-display-article article all-headers)
5689                 (setq did article)
5690                 (when (or all-headers gnus-show-all-headers)
5691                   (gnus-article-show-all-headers)))
5692             (when (or all-headers gnus-show-all-headers)
5693               (gnus-article-show-all-headers))
5694             'old))
5695       (when did
5696         (gnus-article-set-window-start
5697          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5698
5699 (defun gnus-summary-set-current-mark (&optional current-mark)
5700   "Obsolete function."
5701   nil)
5702
5703 (defun gnus-summary-next-article (&optional unread subject backward push)
5704   "Select the next article.
5705 If UNREAD, only unread articles are selected.
5706 If SUBJECT, only articles with SUBJECT are selected.
5707 If BACKWARD, the previous article is selected instead of the next."
5708   (interactive "P")
5709   (cond
5710    ;; Is there such an article?
5711    ((and (gnus-summary-search-forward unread subject backward)
5712          (or (gnus-summary-display-article (gnus-summary-article-number))
5713              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5714     (gnus-summary-position-point))
5715    ;; If not, we try the first unread, if that is wanted.
5716    ((and subject
5717          gnus-auto-select-same
5718          (gnus-summary-first-unread-article))
5719     (gnus-summary-position-point)
5720     (gnus-message 6 "Wrapped"))
5721    ;; Try to get next/previous article not displayed in this group.
5722    ((and gnus-auto-extend-newsgroup
5723          (not unread) (not subject))
5724     (gnus-summary-goto-article
5725      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5726      nil (count-lines (point-min) (point))))
5727    ;; Go to next/previous group.
5728    (t
5729     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5730       (gnus-summary-jump-to-group gnus-newsgroup-name))
5731     (let ((cmd last-command-char)
5732           (point
5733            (save-excursion
5734              (set-buffer gnus-group-buffer)
5735              (point)))
5736           (group
5737            (if (eq gnus-keep-same-level 'best)
5738                (gnus-summary-best-group gnus-newsgroup-name)
5739              (gnus-summary-search-group backward gnus-keep-same-level))))
5740       ;; For some reason, the group window gets selected.  We change
5741       ;; it back.
5742       (select-window (get-buffer-window (current-buffer)))
5743       ;; Select next unread newsgroup automagically.
5744       (cond
5745        ((or (not gnus-auto-select-next)
5746             (not cmd))
5747         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5748        ((or (eq gnus-auto-select-next 'quietly)
5749             (and (eq gnus-auto-select-next 'slightly-quietly)
5750                  push)
5751             (and (eq gnus-auto-select-next 'almost-quietly)
5752                  (gnus-summary-last-article-p)))
5753         ;; Select quietly.
5754         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5755             (gnus-summary-exit)
5756           (gnus-message 7 "No more%s articles (%s)..."
5757                         (if unread " unread" "")
5758                         (if group (concat "selecting " group)
5759                           "exiting"))
5760           (gnus-summary-next-group nil group backward)))
5761        (t
5762         (when (gnus-key-press-event-p last-input-event)
5763           (gnus-summary-walk-group-buffer
5764            gnus-newsgroup-name cmd unread backward point))))))))
5765
5766 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5767   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5768                       (?\C-p (gnus-group-prev-unread-group 1))))
5769         (cursor-in-echo-area t)
5770         keve key group ended)
5771     (save-excursion
5772       (set-buffer gnus-group-buffer)
5773       (goto-char start)
5774       (setq group
5775             (if (eq gnus-keep-same-level 'best)
5776                 (gnus-summary-best-group gnus-newsgroup-name)
5777               (gnus-summary-search-group backward gnus-keep-same-level))))
5778     (while (not ended)
5779       (gnus-message
5780        5 "No more%s articles%s" (if unread " unread" "")
5781        (if (and group
5782                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5783            (format " (Type %s for %s [%s])"
5784                    (single-key-description cmd) group
5785                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5786          (format " (Type %s to exit %s)"
5787                  (single-key-description cmd)
5788                  gnus-newsgroup-name)))
5789       ;; Confirm auto selection.
5790       (setq key (car (setq keve (gnus-read-event-char))))
5791       (setq ended t)
5792       (cond
5793        ((assq key keystrokes)
5794         (let ((obuf (current-buffer)))
5795           (switch-to-buffer gnus-group-buffer)
5796           (when group
5797             (gnus-group-jump-to-group group))
5798           (eval (cadr (assq key keystrokes)))
5799           (setq group (gnus-group-group-name))
5800           (switch-to-buffer obuf))
5801         (setq ended nil))
5802        ((equal key cmd)
5803         (if (or (not group)
5804                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5805             (gnus-summary-exit)
5806           (gnus-summary-next-group nil group backward)))
5807        (t
5808         (push (cdr keve) unread-command-events))))))
5809
5810 (defun gnus-summary-next-unread-article ()
5811   "Select unread article after current one."
5812   (interactive)
5813   (gnus-summary-next-article
5814    (or (not (eq gnus-summary-goto-unread 'never))
5815        (gnus-summary-last-article-p (gnus-summary-article-number)))
5816    (and gnus-auto-select-same
5817         (gnus-summary-article-subject))))
5818
5819 (defun gnus-summary-prev-article (&optional unread subject)
5820   "Select the article after the current one.
5821 If UNREAD is non-nil, only unread articles are selected."
5822   (interactive "P")
5823   (gnus-summary-next-article unread subject t))
5824
5825 (defun gnus-summary-prev-unread-article ()
5826   "Select unread article before current one."
5827   (interactive)
5828   (gnus-summary-prev-article
5829    (or (not (eq gnus-summary-goto-unread 'never))
5830        (gnus-summary-first-article-p (gnus-summary-article-number)))
5831    (and gnus-auto-select-same
5832         (gnus-summary-article-subject))))
5833
5834 (defun gnus-summary-next-page (&optional lines circular)
5835   "Show next page of the selected article.
5836 If at the end of the current article, select the next article.
5837 LINES says how many lines should be scrolled up.
5838
5839 If CIRCULAR is non-nil, go to the start of the article instead of
5840 selecting the next article when reaching the end of the current
5841 article."
5842   (interactive "P")
5843   (setq gnus-summary-buffer (current-buffer))
5844   (gnus-set-global-variables)
5845   (let ((article (gnus-summary-article-number))
5846         (article-window (get-buffer-window gnus-article-buffer t))
5847         endp)
5848     ;; If the buffer is empty, we have no article.
5849     (unless article
5850       (error "No article to select"))
5851     (gnus-configure-windows 'article)
5852     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5853         (if (and (eq gnus-summary-goto-unread 'never)
5854                  (not (gnus-summary-last-article-p article)))
5855             (gnus-summary-next-article)
5856           (gnus-summary-next-unread-article))
5857       (if (or (null gnus-current-article)
5858               (null gnus-article-current)
5859               (/= article (cdr gnus-article-current))
5860               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5861           ;; Selected subject is different from current article's.
5862           (gnus-summary-display-article article)
5863         (when article-window
5864           (gnus-eval-in-buffer-window gnus-article-buffer
5865             (setq endp (gnus-article-next-page lines)))
5866           (when endp
5867             (cond (circular
5868                    (gnus-summary-beginning-of-article))
5869                   (lines
5870                    (gnus-message 3 "End of message"))
5871                   ((null lines)
5872                    (if (and (eq gnus-summary-goto-unread 'never)
5873                             (not (gnus-summary-last-article-p article)))
5874                        (gnus-summary-next-article)
5875                      (gnus-summary-next-unread-article))))))))
5876     (gnus-summary-recenter)
5877     (gnus-summary-position-point)))
5878
5879 (defun gnus-summary-prev-page (&optional lines move)
5880   "Show previous page of selected article.
5881 Argument LINES specifies lines to be scrolled down.
5882 If MOVE, move to the previous unread article if point is at
5883 the beginning of the buffer."
5884   (interactive "P")
5885   (let ((article (gnus-summary-article-number))
5886         (article-window (get-buffer-window gnus-article-buffer t))
5887         endp)
5888     (gnus-configure-windows 'article)
5889     (if (or (null gnus-current-article)
5890             (null gnus-article-current)
5891             (/= article (cdr gnus-article-current))
5892             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5893         ;; Selected subject is different from current article's.
5894         (gnus-summary-display-article article)
5895       (gnus-summary-recenter)
5896       (when article-window
5897         (gnus-eval-in-buffer-window gnus-article-buffer
5898           (setq endp (gnus-article-prev-page lines)))
5899         (when (and move endp)
5900           (cond (lines
5901                  (gnus-message 3 "Beginning of message"))
5902                 ((null lines)
5903                  (if (and (eq gnus-summary-goto-unread 'never)
5904                           (not (gnus-summary-first-article-p article)))
5905                      (gnus-summary-prev-article)
5906                    (gnus-summary-prev-unread-article))))))))
5907   (gnus-summary-position-point))
5908
5909 (defun gnus-summary-prev-page-or-article (&optional lines)
5910   "Show previous page of selected article.
5911 Argument LINES specifies lines to be scrolled down.
5912 If at the beginning of the article, go to the next article."
5913   (interactive "P")
5914   (gnus-summary-prev-page lines t))
5915
5916 (defun gnus-summary-scroll-up (lines)
5917   "Scroll up (or down) one line current article.
5918 Argument LINES specifies lines to be scrolled up (or down if negative)."
5919   (interactive "p")
5920   (gnus-configure-windows 'article)
5921   (gnus-summary-show-thread)
5922   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5923     (gnus-eval-in-buffer-window gnus-article-buffer
5924       (cond ((> lines 0)
5925              (when (gnus-article-next-page lines)
5926                (gnus-message 3 "End of message")))
5927             ((< lines 0)
5928              (gnus-article-prev-page (- lines))))))
5929   (gnus-summary-recenter)
5930   (gnus-summary-position-point))
5931
5932 (defun gnus-summary-scroll-down (lines)
5933   "Scroll down (or up) one line current article.
5934 Argument LINES specifies lines to be scrolled down (or up if negative)."
5935   (interactive "p")
5936   (gnus-summary-scroll-up (- lines)))
5937
5938 (defun gnus-summary-next-same-subject ()
5939   "Select next article which has the same subject as current one."
5940   (interactive)
5941   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5942
5943 (defun gnus-summary-prev-same-subject ()
5944   "Select previous article which has the same subject as current one."
5945   (interactive)
5946   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5947
5948 (defun gnus-summary-next-unread-same-subject ()
5949   "Select next unread article which has the same subject as current one."
5950   (interactive)
5951   (gnus-summary-next-article t (gnus-summary-article-subject)))
5952
5953 (defun gnus-summary-prev-unread-same-subject ()
5954   "Select previous unread article which has the same subject as current one."
5955   (interactive)
5956   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5957
5958 (defun gnus-summary-first-unread-article ()
5959   "Select the first unread article.
5960 Return nil if there are no unread articles."
5961   (interactive)
5962   (prog1
5963       (when (gnus-summary-first-subject t)
5964         (gnus-summary-show-thread)
5965         (gnus-summary-first-subject t)
5966         (gnus-summary-display-article (gnus-summary-article-number)))
5967     (gnus-summary-position-point)))
5968
5969 (defun gnus-summary-first-unread-subject ()
5970   "Place the point on the subject line of the first unread article.
5971 Return nil if there are no unread articles."
5972   (interactive)
5973   (prog1
5974       (when (gnus-summary-first-subject t)
5975         (gnus-summary-show-thread)
5976         (gnus-summary-first-subject t))
5977     (gnus-summary-position-point)))
5978
5979 (defun gnus-summary-first-article ()
5980   "Select the first article.
5981 Return nil if there are no articles."
5982   (interactive)
5983   (prog1
5984       (when (gnus-summary-first-subject)
5985         (gnus-summary-show-thread)
5986         (gnus-summary-first-subject)
5987         (gnus-summary-display-article (gnus-summary-article-number)))
5988     (gnus-summary-position-point)))
5989
5990 (defun gnus-summary-best-unread-article ()
5991   "Select the unread article with the highest score."
5992   (interactive)
5993   (let ((best -1000000)
5994         (data gnus-newsgroup-data)
5995         article score)
5996     (while data
5997       (and (gnus-data-unread-p (car data))
5998            (> (setq score
5999                     (gnus-summary-article-score (gnus-data-number (car data))))
6000               best)
6001            (setq best score
6002                  article (gnus-data-number (car data))))
6003       (setq data (cdr data)))
6004     (prog1
6005         (if article
6006             (gnus-summary-goto-article article)
6007           (error "No unread articles"))
6008       (gnus-summary-position-point))))
6009
6010 (defun gnus-summary-last-subject ()
6011   "Go to the last displayed subject line in the group."
6012   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6013     (when article
6014       (gnus-summary-goto-subject article))))
6015
6016 (defun gnus-summary-goto-article (article &optional all-headers force)
6017   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6018 If ALL-HEADERS is non-nil, no header lines are hidden.
6019 If FORCE, go to the article even if it isn't displayed.  If FORCE
6020 is a number, it is the line the article is to be displayed on."
6021   (interactive
6022    (list
6023     (completing-read
6024      "Article number or Message-ID: "
6025      (mapcar (lambda (number) (list (int-to-string number)))
6026              gnus-newsgroup-limit))
6027     current-prefix-arg
6028     t))
6029   (prog1
6030       (if (and (stringp article)
6031                (string-match "@" article))
6032           (gnus-summary-refer-article article)
6033         (when (stringp article)
6034           (setq article (string-to-number article)))
6035         (if (gnus-summary-goto-subject article force)
6036             (gnus-summary-display-article article all-headers)
6037           (gnus-message 4 "Couldn't go to article %s" article) nil))
6038     (gnus-summary-position-point)))
6039
6040 (defun gnus-summary-goto-last-article ()
6041   "Go to the previously read article."
6042   (interactive)
6043   (prog1
6044       (when gnus-last-article
6045         (gnus-summary-goto-article gnus-last-article nil t))
6046     (gnus-summary-position-point)))
6047
6048 (defun gnus-summary-pop-article (number)
6049   "Pop one article off the history and go to the previous.
6050 NUMBER articles will be popped off."
6051   (interactive "p")
6052   (let (to)
6053     (setq gnus-newsgroup-history
6054           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6055     (if to
6056         (gnus-summary-goto-article (car to) nil t)
6057       (error "Article history empty")))
6058   (gnus-summary-position-point))
6059
6060 ;; Summary commands and functions for limiting the summary buffer.
6061
6062 (defun gnus-summary-limit-to-articles (n)
6063   "Limit the summary buffer to the next N articles.
6064 If not given a prefix, use the process marked articles instead."
6065   (interactive "P")
6066   (prog1
6067       (let ((articles (gnus-summary-work-articles n)))
6068         (setq gnus-newsgroup-processable nil)
6069         (gnus-summary-limit articles))
6070     (gnus-summary-position-point)))
6071
6072 (defun gnus-summary-pop-limit (&optional total)
6073   "Restore the previous limit.
6074 If given a prefix, remove all limits."
6075   (interactive "P")
6076   (when total
6077     (setq gnus-newsgroup-limits
6078           (list (mapcar (lambda (h) (mail-header-number h))
6079                         gnus-newsgroup-headers))))
6080   (unless gnus-newsgroup-limits
6081     (error "No limit to pop"))
6082   (prog1
6083       (gnus-summary-limit nil 'pop)
6084     (gnus-summary-position-point)))
6085
6086 (defun gnus-summary-limit-to-subject (subject &optional header)
6087   "Limit the summary buffer to articles that have subjects that match a regexp."
6088   (interactive "sLimit to subject (regexp): ")
6089   (unless header
6090     (setq header "subject"))
6091   (when (not (equal "" subject))
6092     (prog1
6093         (let ((articles (gnus-summary-find-matching
6094                          (or header "subject") subject 'all)))
6095           (unless articles
6096             (error "Found no matches for \"%s\"" subject))
6097           (gnus-summary-limit articles))
6098       (gnus-summary-position-point))))
6099
6100 (defun gnus-summary-limit-to-author (from)
6101   "Limit the summary buffer to articles that have authors that match a regexp."
6102   (interactive "sLimit to author (regexp): ")
6103   (gnus-summary-limit-to-subject from "from"))
6104
6105 (defun gnus-summary-limit-to-age (age &optional younger-p)
6106   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6107 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6108 articles that are younger than AGE days."
6109   (interactive "nTime in days: \nP")
6110   (prog1
6111       (let ((data gnus-newsgroup-data)
6112             (cutoff (days-to-time age))
6113             articles d date is-younger)
6114         (while (setq d (pop data))
6115           (when (and (vectorp (gnus-data-header d))
6116                      (setq date (mail-header-date (gnus-data-header d))))
6117             (setq is-younger (time-less-p
6118                               (time-since (date-to-time date))
6119                               cutoff))
6120             (when (if younger-p
6121                       is-younger
6122                     (not is-younger))
6123               (push (gnus-data-number d) articles))))
6124         (gnus-summary-limit (nreverse articles)))
6125     (gnus-summary-position-point)))
6126
6127 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6128 (make-obsolete
6129  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6130
6131 (defun gnus-summary-limit-to-unread (&optional all)
6132   "Limit the summary buffer to articles that are not marked as read.
6133 If ALL is non-nil, limit strictly to unread articles."
6134   (interactive "P")
6135   (if all
6136       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6137     (gnus-summary-limit-to-marks
6138      ;; Concat all the marks that say that an article is read and have
6139      ;; those removed.
6140      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6141            gnus-killed-mark gnus-kill-file-mark
6142            gnus-low-score-mark gnus-expirable-mark
6143            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6144            gnus-duplicate-mark gnus-souped-mark)
6145      'reverse)))
6146
6147 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6148 (make-obsolete 'gnus-summary-delete-marked-with
6149                'gnus-summary-limit-exlude-marks)
6150
6151 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6152   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6153 If REVERSE, limit the summary buffer to articles that are marked
6154 with MARKS.  MARKS can either be a string of marks or a list of marks.
6155 Returns how many articles were removed."
6156   (interactive "sMarks: ")
6157   (gnus-summary-limit-to-marks marks t))
6158
6159 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6160   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6161 If REVERSE (the prefix), limit the summary buffer to articles that are
6162 not marked with MARKS.  MARKS can either be a string of marks or a
6163 list of marks.
6164 Returns how many articles were removed."
6165   (interactive "sMarks: \nP")
6166   (prog1
6167       (let ((data gnus-newsgroup-data)
6168             (marks (if (listp marks) marks
6169                      (append marks nil))) ; Transform to list.
6170             articles)
6171         (while data
6172           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6173                   (memq (gnus-data-mark (car data)) marks))
6174             (push (gnus-data-number (car data)) articles))
6175           (setq data (cdr data)))
6176         (gnus-summary-limit articles))
6177     (gnus-summary-position-point)))
6178
6179 (defun gnus-summary-limit-to-score (&optional score)
6180   "Limit to articles with score at or above SCORE."
6181   (interactive "P")
6182   (setq score (if score
6183                   (prefix-numeric-value score)
6184                 (or gnus-summary-default-score 0)))
6185   (let ((data gnus-newsgroup-data)
6186         articles)
6187     (while data
6188       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6189                 score)
6190         (push (gnus-data-number (car data)) articles))
6191       (setq data (cdr data)))
6192     (prog1
6193         (gnus-summary-limit articles)
6194       (gnus-summary-position-point))))
6195
6196 (defun gnus-summary-limit-include-thread (id)
6197   "Display all the hidden articles that in the current thread."
6198   (interactive (list (mail-header-id (gnus-summary-article-header))))
6199   (let ((articles (gnus-articles-in-thread
6200                    (gnus-id-to-thread (gnus-root-id id)))))
6201     (prog1
6202         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6203       (gnus-summary-position-point))))
6204
6205 (defun gnus-summary-limit-include-dormant ()
6206   "Display all the hidden articles that are marked as dormant.
6207 Note that this command only works on a subset of the articles currently
6208 fetched for this group."
6209   (interactive)
6210   (unless gnus-newsgroup-dormant
6211     (error "There are no dormant articles in this group"))
6212   (prog1
6213       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-limit-exclude-dormant ()
6217   "Hide all dormant articles."
6218   (interactive)
6219   (prog1
6220       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6221     (gnus-summary-position-point)))
6222
6223 (defun gnus-summary-limit-exclude-childless-dormant ()
6224   "Hide all dormant articles that have no children."
6225   (interactive)
6226   (let ((data (gnus-data-list t))
6227         articles d children)
6228     ;; Find all articles that are either not dormant or have
6229     ;; children.
6230     (while (setq d (pop data))
6231       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6232                 (and (setq children
6233                            (gnus-article-children (gnus-data-number d)))
6234                      (let (found)
6235                        (while children
6236                          (when (memq (car children) articles)
6237                            (setq children nil
6238                                  found t))
6239                          (pop children))
6240                        found)))
6241         (push (gnus-data-number d) articles)))
6242     ;; Do the limiting.
6243     (prog1
6244         (gnus-summary-limit articles)
6245       (gnus-summary-position-point))))
6246
6247 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6248   "Mark all unread excluded articles as read.
6249 If ALL, mark even excluded ticked and dormants as read."
6250   (interactive "P")
6251   (let ((articles (gnus-sorted-complement
6252                    (sort
6253                     (mapcar (lambda (h) (mail-header-number h))
6254                             gnus-newsgroup-headers)
6255                     '<)
6256                    (sort gnus-newsgroup-limit '<)))
6257         article)
6258     (setq gnus-newsgroup-unreads
6259           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6260     (if all
6261         (setq gnus-newsgroup-dormant nil
6262               gnus-newsgroup-marked nil
6263               gnus-newsgroup-reads
6264               (nconc
6265                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6266                gnus-newsgroup-reads))
6267       (while (setq article (pop articles))
6268         (unless (or (memq article gnus-newsgroup-dormant)
6269                     (memq article gnus-newsgroup-marked))
6270           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6271
6272 (defun gnus-summary-limit (articles &optional pop)
6273   (if pop
6274       ;; We pop the previous limit off the stack and use that.
6275       (setq articles (car gnus-newsgroup-limits)
6276             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6277     ;; We use the new limit, so we push the old limit on the stack.
6278     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6279   ;; Set the limit.
6280   (setq gnus-newsgroup-limit articles)
6281   (let ((total (length gnus-newsgroup-data))
6282         (data (gnus-data-find-list (gnus-summary-article-number)))
6283         (gnus-summary-mark-below nil)   ; Inhibit this.
6284         found)
6285     ;; This will do all the work of generating the new summary buffer
6286     ;; according to the new limit.
6287     (gnus-summary-prepare)
6288     ;; Hide any threads, possibly.
6289     (and gnus-show-threads
6290          gnus-thread-hide-subtree
6291          (gnus-summary-hide-all-threads))
6292     ;; Try to return to the article you were at, or one in the
6293     ;; neighborhood.
6294     (when data
6295       ;; We try to find some article after the current one.
6296       (while data
6297         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6298           (setq data nil
6299                 found t))
6300         (setq data (cdr data))))
6301     (unless found
6302       ;; If there is no data, that means that we were after the last
6303       ;; article.  The same goes when we can't find any articles
6304       ;; after the current one.
6305       (goto-char (point-max))
6306       (gnus-summary-find-prev))
6307     (gnus-set-mode-line 'summary)
6308     ;; We return how many articles were removed from the summary
6309     ;; buffer as a result of the new limit.
6310     (- total (length gnus-newsgroup-data))))
6311
6312 (defsubst gnus-invisible-cut-children (threads)
6313   (let ((num 0))
6314     (while threads
6315       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6316         (incf num))
6317       (pop threads))
6318     (< num 2)))
6319
6320 (defsubst gnus-cut-thread (thread)
6321   "Go forwards in the thread until we find an article that we want to display."
6322   (when (or (eq gnus-fetch-old-headers 'some)
6323             (eq gnus-fetch-old-headers 'invisible)
6324             (eq gnus-build-sparse-threads 'some)
6325             (eq gnus-build-sparse-threads 'more))
6326     ;; Deal with old-fetched headers and sparse threads.
6327     (while (and
6328             thread
6329             (or
6330              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6331              (gnus-summary-article-ancient-p
6332               (mail-header-number (car thread))))
6333             (if (or (<= (length (cdr thread)) 1)
6334                     (eq gnus-fetch-old-headers 'invisible))
6335                 (setq gnus-newsgroup-limit
6336                       (delq (mail-header-number (car thread))
6337                             gnus-newsgroup-limit)
6338                       thread (cadr thread))
6339               (when (gnus-invisible-cut-children (cdr thread))
6340                 (let ((th (cdr thread)))
6341                   (while th
6342                     (if (memq (mail-header-number (caar th))
6343                               gnus-newsgroup-limit)
6344                         (setq thread (car th)
6345                               th nil)
6346                       (setq th (cdr th))))))))))
6347   thread)
6348
6349 (defun gnus-cut-threads (threads)
6350   "Cut off all uninteresting articles from the beginning of threads."
6351   (when (or (eq gnus-fetch-old-headers 'some)
6352             (eq gnus-fetch-old-headers 'invisible)
6353             (eq gnus-build-sparse-threads 'some)
6354             (eq gnus-build-sparse-threads 'more))
6355     (let ((th threads))
6356       (while th
6357         (setcar th (gnus-cut-thread (car th)))
6358         (setq th (cdr th)))))
6359   ;; Remove nixed out threads.
6360   (delq nil threads))
6361
6362 (defun gnus-summary-initial-limit (&optional show-if-empty)
6363   "Figure out what the initial limit is supposed to be on group entry.
6364 This entails weeding out unwanted dormants, low-scored articles,
6365 fetch-old-headers verbiage, and so on."
6366   ;; Most groups have nothing to remove.
6367   (if (or gnus-inhibit-limiting
6368           (and (null gnus-newsgroup-dormant)
6369                (not (eq gnus-fetch-old-headers 'some))
6370                (not (eq gnus-fetch-old-headers 'invisible))
6371                (null gnus-summary-expunge-below)
6372                (not (eq gnus-build-sparse-threads 'some))
6373                (not (eq gnus-build-sparse-threads 'more))
6374                (null gnus-thread-expunge-below)
6375                (not gnus-use-nocem)))
6376       ()                                ; Do nothing.
6377     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6378     (setq gnus-newsgroup-limit nil)
6379     (mapatoms
6380      (lambda (node)
6381        (unless (car (symbol-value node))
6382          ;; These threads have no parents -- they are roots.
6383          (let ((nodes (cdr (symbol-value node)))
6384                thread)
6385            (while nodes
6386              (if (and gnus-thread-expunge-below
6387                       (< (gnus-thread-total-score (car nodes))
6388                          gnus-thread-expunge-below))
6389                  (gnus-expunge-thread (pop nodes))
6390                (setq thread (pop nodes))
6391                (gnus-summary-limit-children thread))))))
6392      gnus-newsgroup-dependencies)
6393     ;; If this limitation resulted in an empty group, we might
6394     ;; pop the previous limit and use it instead.
6395     (when (and (not gnus-newsgroup-limit)
6396                show-if-empty)
6397       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6398     gnus-newsgroup-limit))
6399
6400 (defun gnus-summary-limit-children (thread)
6401   "Return 1 if this subthread is visible and 0 if it is not."
6402   ;; First we get the number of visible children to this thread.  This
6403   ;; is done by recursing down the thread using this function, so this
6404   ;; will really go down to a leaf article first, before slowly
6405   ;; working its way up towards the root.
6406   (when thread
6407     (let ((children
6408            (if (cdr thread)
6409                (apply '+ (mapcar 'gnus-summary-limit-children
6410                                  (cdr thread)))
6411              0))
6412           (number (mail-header-number (car thread)))
6413           score)
6414       (if (and
6415            (not (memq number gnus-newsgroup-marked))
6416            (or
6417             ;; If this article is dormant and has absolutely no visible
6418             ;; children, then this article isn't visible.
6419             (and (memq number gnus-newsgroup-dormant)
6420                  (zerop children))
6421             ;; If this is "fetch-old-headered" and there is no
6422             ;; visible children, then we don't want this article.
6423             (and (eq gnus-fetch-old-headers 'some)
6424                  (gnus-summary-article-ancient-p number)
6425                  (zerop children))
6426             ;; If this is "fetch-old-headered" and `invisible', then
6427             ;; we don't want this article.
6428             (and (eq gnus-fetch-old-headers 'invisible)
6429                  (gnus-summary-article-ancient-p number))
6430             ;; If this is a sparsely inserted article with no children,
6431             ;; we don't want it.
6432             (and (eq gnus-build-sparse-threads 'some)
6433                  (gnus-summary-article-sparse-p number)
6434                  (zerop children))
6435             ;; If we use expunging, and this article is really
6436             ;; low-scored, then we don't want this article.
6437             (when (and gnus-summary-expunge-below
6438                        (< (setq score
6439                                 (or (cdr (assq number gnus-newsgroup-scored))
6440                                     gnus-summary-default-score))
6441                           gnus-summary-expunge-below))
6442               ;; We increase the expunge-tally here, but that has
6443               ;; nothing to do with the limits, really.
6444               (incf gnus-newsgroup-expunged-tally)
6445               ;; We also mark as read here, if that's wanted.
6446               (when (and gnus-summary-mark-below
6447                          (< score gnus-summary-mark-below))
6448                 (setq gnus-newsgroup-unreads
6449                       (delq number gnus-newsgroup-unreads))
6450                 (if gnus-newsgroup-auto-expire
6451                     (push number gnus-newsgroup-expirable)
6452                   (push (cons number gnus-low-score-mark)
6453                         gnus-newsgroup-reads)))
6454               t)
6455             ;; Check NoCeM things.
6456             (if (and gnus-use-nocem
6457                      (gnus-nocem-unwanted-article-p
6458                       (mail-header-id (car thread))))
6459                 (progn
6460                   (setq gnus-newsgroup-unreads
6461                         (delq number gnus-newsgroup-unreads))
6462                   t))))
6463           ;; Nope, invisible article.
6464           0
6465         ;; Ok, this article is to be visible, so we add it to the limit
6466         ;; and return 1.
6467         (push number gnus-newsgroup-limit)
6468         1))))
6469
6470 (defun gnus-expunge-thread (thread)
6471   "Mark all articles in THREAD as read."
6472   (let* ((number (mail-header-number (car thread))))
6473     (incf gnus-newsgroup-expunged-tally)
6474     ;; We also mark as read here, if that's wanted.
6475     (setq gnus-newsgroup-unreads
6476           (delq number gnus-newsgroup-unreads))
6477     (if gnus-newsgroup-auto-expire
6478         (push number gnus-newsgroup-expirable)
6479       (push (cons number gnus-low-score-mark)
6480             gnus-newsgroup-reads)))
6481   ;; Go recursively through all subthreads.
6482   (mapcar 'gnus-expunge-thread (cdr thread)))
6483
6484 ;; Summary article oriented commands
6485
6486 (defun gnus-summary-refer-parent-article (n)
6487   "Refer parent article N times.
6488 If N is negative, go to ancestor -N instead.
6489 The difference between N and the number of articles fetched is returned."
6490   (interactive "p")
6491   (let ((skip 1)
6492         error header ref)
6493     (when (not (natnump n))
6494       (setq skip (abs n)
6495             n 1))
6496     (while (and (> n 0)
6497                 (not error))
6498       (setq header (gnus-summary-article-header))
6499       (if (and (eq (mail-header-number header)
6500                    (cdr gnus-article-current))
6501                (equal gnus-newsgroup-name
6502                       (car gnus-article-current)))
6503           ;; If we try to find the parent of the currently
6504           ;; displayed article, then we take a look at the actual
6505           ;; References header, since this is slightly more
6506           ;; reliable than the References field we got from the
6507           ;; server.
6508           (save-excursion
6509             (set-buffer gnus-original-article-buffer)
6510             (nnheader-narrow-to-headers)
6511             (unless (setq ref (message-fetch-field "references"))
6512               (setq ref (message-fetch-field "in-reply-to")))
6513             (widen))
6514         (setq ref
6515               ;; It's not the current article, so we take a bet on
6516               ;; the value we got from the server.
6517               (mail-header-references header)))
6518       (if (and ref
6519                (not (equal ref "")))
6520           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6521             (gnus-message 1 "Couldn't find parent"))
6522         (gnus-message 1 "No references in article %d"
6523                       (gnus-summary-article-number))
6524         (setq error t))
6525       (decf n))
6526     (gnus-summary-position-point)
6527     n))
6528
6529 (defun gnus-summary-refer-references ()
6530   "Fetch all articles mentioned in the References header.
6531 Return the number of articles fetched."
6532   (interactive)
6533   (let ((ref (mail-header-references (gnus-summary-article-header)))
6534         (current (gnus-summary-article-number))
6535         (n 0))
6536     (if (or (not ref)
6537             (equal ref ""))
6538         (error "No References in the current article")
6539       ;; For each Message-ID in the References header...
6540       (while (string-match "<[^>]*>" ref)
6541         (incf n)
6542         ;; ... fetch that article.
6543         (gnus-summary-refer-article
6544          (prog1 (match-string 0 ref)
6545            (setq ref (substring ref (match-end 0))))))
6546       (gnus-summary-goto-subject current)
6547       (gnus-summary-position-point)
6548       n)))
6549
6550 (defun gnus-summary-refer-thread (&optional limit)
6551   "Fetch all articles in the current thread.
6552 If LIMIT (the numerical prefix), fetch that many old headers instead
6553 of what's specified by the `gnus-refer-thread-limit' variable."
6554   (interactive "P")
6555   (let ((id (mail-header-id (gnus-summary-article-header)))
6556         (limit (if limit (prefix-numeric-value limit)
6557                  gnus-refer-thread-limit)))
6558     ;; We want to fetch LIMIT *old* headers, but we also have to
6559     ;; re-fetch all the headers in the current buffer, because many of
6560     ;; them may be undisplayed.  So we adjust LIMIT.
6561     (when (numberp limit)
6562       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6563     (unless (eq gnus-fetch-old-headers 'invisible)
6564       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6565       ;; Retrieve the headers and read them in.
6566       (if (eq (gnus-retrieve-headers
6567                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6568               'nov)
6569           (gnus-build-all-threads)
6570         (error "Can't fetch thread from backends that don't support NOV"))
6571       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6572     (gnus-summary-limit-include-thread id)))
6573
6574 (defun gnus-summary-refer-article (message-id &optional arg)
6575   "Fetch an article specified by MESSAGE-ID.
6576 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6577 or `gnus-select-method', no matter what backend the article comes from."
6578   (interactive "sMessage-ID: \nP")
6579   (when (and (stringp message-id)
6580              (not (zerop (length message-id))))
6581     ;; Construct the correct Message-ID if necessary.
6582     ;; Suggested by tale@pawl.rpi.edu.
6583     (unless (string-match "^<" message-id)
6584       (setq message-id (concat "<" message-id)))
6585     (unless (string-match ">$" message-id)
6586       (setq message-id (concat message-id ">")))
6587     (let* ((header (gnus-id-to-header message-id))
6588            (sparse (and header
6589                         (gnus-summary-article-sparse-p
6590                          (mail-header-number header))
6591                         (memq (mail-header-number header)
6592                               gnus-newsgroup-limit))))
6593       (cond
6594        ;; If the article is present in the buffer we just go to it.
6595        ((and header
6596              (or (not (gnus-summary-article-sparse-p
6597                        (mail-header-number header)))
6598                  sparse))
6599         (prog1
6600             (gnus-summary-goto-article
6601              (mail-header-number header) nil t)
6602           (when sparse
6603             (gnus-summary-update-article (mail-header-number header)))))
6604        (t
6605         ;; We fetch the article
6606         (let ((gnus-override-method
6607                (cond ((gnus-news-group-p gnus-newsgroup-name)
6608                       gnus-refer-article-method)
6609                      (arg
6610                       (or gnus-refer-article-method gnus-select-method))
6611                      (t nil)))
6612               number)
6613           ;; Start the special refer-article method, if necessary.
6614           (when (and gnus-refer-article-method
6615                      (gnus-news-group-p gnus-newsgroup-name))
6616             (gnus-check-server gnus-refer-article-method))
6617           ;; Fetch the header, and display the article.
6618           (if (setq number (gnus-summary-insert-subject message-id))
6619               (gnus-summary-select-article nil nil nil number)
6620             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6621
6622 (defun gnus-summary-edit-parameters ()
6623   "Edit the group parameters of the current group."
6624   (interactive)
6625   (gnus-group-edit-group gnus-newsgroup-name 'params))
6626
6627 (defun gnus-summary-customize-parameters ()
6628   "Customize the group parameters of the current group."
6629   (interactive)
6630   (gnus-group-customize gnus-newsgroup-name))
6631
6632 (defun gnus-summary-enter-digest-group (&optional force)
6633   "Enter an nndoc group based on the current article.
6634 If FORCE, force a digest interpretation.  If not, try
6635 to guess what the document format is."
6636   (interactive "P")
6637   (let ((conf gnus-current-window-configuration))
6638     (save-excursion
6639       (gnus-summary-select-article))
6640     (setq gnus-current-window-configuration conf)
6641     (let* ((name (format "%s-%d"
6642                          (gnus-group-prefixed-name
6643                           gnus-newsgroup-name (list 'nndoc ""))
6644                          (save-excursion
6645                            (set-buffer gnus-summary-buffer)
6646                            gnus-current-article)))
6647            (ogroup gnus-newsgroup-name)
6648            (params (append (gnus-info-params (gnus-get-info ogroup))
6649                            (list (cons 'to-group ogroup))
6650                            (list (cons 'save-article-group ogroup))))
6651            (case-fold-search t)
6652            (buf (current-buffer))
6653            dig)
6654       (save-excursion
6655         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6656         (insert-buffer-substring gnus-original-article-buffer)
6657         ;; Remove lines that may lead nndoc to misinterpret the
6658         ;; document type.
6659         (narrow-to-region
6660          (goto-char (point-min))
6661          (or (search-forward "\n\n" nil t) (point)))
6662         (goto-char (point-min))
6663         (delete-matching-lines "^\\(Path\\):\\|^From ")
6664         (widen))
6665       (unwind-protect
6666           (if (gnus-group-read-ephemeral-group
6667                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6668                             (nndoc-article-type
6669                              ,(if force 'digest 'guess))) t)
6670               ;; Make all postings to this group go to the parent group.
6671               (nconc (gnus-info-params (gnus-get-info name))
6672                      params)
6673             ;; Couldn't select this doc group.
6674             (switch-to-buffer buf)
6675             (gnus-set-global-variables)
6676             (gnus-configure-windows 'summary)
6677             (gnus-message 3 "Article couldn't be entered?"))
6678         (kill-buffer dig)))))
6679
6680 (defun gnus-summary-read-document (n)
6681   "Open a new group based on the current article(s).
6682 This will allow you to read digests and other similar
6683 documents as newsgroups.
6684 Obeys the standard process/prefix convention."
6685   (interactive "P")
6686   (let* ((articles (gnus-summary-work-articles n))
6687          (ogroup gnus-newsgroup-name)
6688          (params (append (gnus-info-params (gnus-get-info ogroup))
6689                          (list (cons 'to-group ogroup))))
6690          article group egroup groups vgroup)
6691     (while (setq article (pop articles))
6692       (setq group (format "%s-%d" gnus-newsgroup-name article))
6693       (gnus-summary-remove-process-mark article)
6694       (when (gnus-summary-display-article article)
6695         (save-excursion
6696           (with-temp-buffer
6697             (insert-buffer-substring gnus-original-article-buffer)
6698             ;; Remove some headers that may lead nndoc to make
6699             ;; the wrong guess.
6700             (message-narrow-to-head)
6701             (goto-char (point-min))
6702             (delete-matching-lines "^\\(Path\\):\\|^From ")
6703             (widen)
6704             (if (setq egroup
6705                       (gnus-group-read-ephemeral-group
6706                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6707                                      (nndoc-article-type guess))
6708                        t nil t))
6709                 (progn
6710                   ;; Make all postings to this group go to the parent group.
6711                   (nconc (gnus-info-params (gnus-get-info egroup))
6712                          params)
6713                   (push egroup groups))
6714               ;; Couldn't select this doc group.
6715               (gnus-error 3 "Article couldn't be entered"))))))
6716     ;; Now we have selected all the documents.
6717     (cond
6718      ((not groups)
6719       (error "None of the articles could be interpreted as documents"))
6720      ((gnus-group-read-ephemeral-group
6721        (setq vgroup (format
6722                      "nnvirtual:%s-%s" gnus-newsgroup-name
6723                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6724        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6725        t
6726        (cons (current-buffer) 'summary)))
6727      (t
6728       (error "Couldn't select virtual nndoc group")))))
6729
6730 (defun gnus-summary-isearch-article (&optional regexp-p)
6731   "Do incremental search forward on the current article.
6732 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6733   (interactive "P")
6734   (gnus-summary-select-article)
6735   (gnus-configure-windows 'article)
6736   (gnus-eval-in-buffer-window gnus-article-buffer
6737     (save-restriction
6738       (widen)
6739       (isearch-forward regexp-p))))
6740
6741 (defun gnus-summary-search-article-forward (regexp &optional backward)
6742   "Search for an article containing REGEXP forward.
6743 If BACKWARD, search backward instead."
6744   (interactive
6745    (list (read-string
6746           (format "Search article %s (regexp%s): "
6747                   (if current-prefix-arg "backward" "forward")
6748                   (if gnus-last-search-regexp
6749                       (concat ", default " gnus-last-search-regexp)
6750                     "")))
6751          current-prefix-arg))
6752   (if (string-equal regexp "")
6753       (setq regexp (or gnus-last-search-regexp ""))
6754     (setq gnus-last-search-regexp regexp))
6755   (if (gnus-summary-search-article regexp backward)
6756       (gnus-summary-show-thread)
6757     (error "Search failed: \"%s\"" regexp)))
6758
6759 (defun gnus-summary-search-article-backward (regexp)
6760   "Search for an article containing REGEXP backward."
6761   (interactive
6762    (list (read-string
6763           (format "Search article backward (regexp%s): "
6764                   (if gnus-last-search-regexp
6765                       (concat ", default " gnus-last-search-regexp)
6766                     "")))))
6767   (gnus-summary-search-article-forward regexp 'backward))
6768
6769 (defun gnus-summary-search-article (regexp &optional backward)
6770   "Search for an article containing REGEXP.
6771 Optional argument BACKWARD means do search for backward.
6772 `gnus-select-article-hook' is not called during the search."
6773   ;; We have to require this here to make sure that the following
6774   ;; dynamic binding isn't shadowed by autoloading.
6775   (require 'gnus-async)
6776   (require 'gnus-art)
6777   (let ((gnus-select-article-hook nil)  ;Disable hook.
6778         (gnus-article-display-hook nil)
6779         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6780         (gnus-use-article-prefetch nil)
6781         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6782         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6783         (sum (current-buffer))
6784         (found nil)
6785         point)
6786     (gnus-save-hidden-threads
6787       (gnus-summary-select-article)
6788       (set-buffer gnus-article-buffer)
6789       (when backward
6790         (forward-line -1))
6791       (while (not found)
6792         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6793         (if (if backward
6794                 (re-search-backward regexp nil t)
6795               (re-search-forward regexp nil t))
6796             ;; We found the regexp.
6797             (progn
6798               (setq found 'found)
6799               (beginning-of-line)
6800               (set-window-start
6801                (get-buffer-window (current-buffer))
6802                (point))
6803               (forward-line 1)
6804               (set-buffer sum)
6805               (setq point (point)))
6806           ;; We didn't find it, so we go to the next article.
6807           (set-buffer sum)
6808           (setq found 'not)
6809           (while (eq found 'not)
6810             (if (not (if backward (gnus-summary-find-prev)
6811                        (gnus-summary-find-next)))
6812                 ;; No more articles.
6813                 (setq found t)
6814               ;; Select the next article and adjust point.
6815               (unless (gnus-summary-article-sparse-p
6816                        (gnus-summary-article-number))
6817                 (setq found nil)
6818                 (gnus-summary-select-article)
6819                 (set-buffer gnus-article-buffer)
6820                 (widen)
6821                 (goto-char (if backward (point-max) (point-min))))))))
6822       (gnus-message 7 ""))
6823     ;; Return whether we found the regexp.
6824     (when (eq found 'found)
6825       (goto-char point)
6826       (gnus-summary-show-thread)
6827       (gnus-summary-goto-subject gnus-current-article)
6828       (gnus-summary-position-point)
6829       t)))
6830
6831 (defun gnus-summary-find-matching (header regexp &optional backward unread
6832                                           not-case-fold)
6833   "Return a list of all articles that match REGEXP on HEADER.
6834 The search stars on the current article and goes forwards unless
6835 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6836 If UNREAD is non-nil, only unread articles will
6837 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6838 in the comparisons."
6839   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6840                 (gnus-data-find-list
6841                  (gnus-summary-article-number) (gnus-data-list backward))))
6842         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6843         (case-fold-search (not not-case-fold))
6844         articles d)
6845     (unless (fboundp (intern (concat "mail-header-" header)))
6846       (error "%s is not a valid header" header))
6847     (while data
6848       (setq d (car data))
6849       (and (or (not unread)             ; We want all articles...
6850                (gnus-data-unread-p d))  ; Or just unreads.
6851            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6852            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6853            (push (gnus-data-number d) articles)) ; Success!
6854       (setq data (cdr data)))
6855     (nreverse articles)))
6856
6857 (defun gnus-summary-execute-command (header regexp command &optional backward)
6858   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6859 If HEADER is an empty string (or nil), the match is done on the entire
6860 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6861   (interactive
6862    (list (let ((completion-ignore-case t))
6863            (completing-read
6864             "Header name: "
6865             (mapcar (lambda (string) (list string))
6866                     '("Number" "Subject" "From" "Lines" "Date"
6867                       "Message-ID" "Xref" "References" "Body"))
6868             nil 'require-match))
6869          (read-string "Regexp: ")
6870          (read-key-sequence "Command: ")
6871          current-prefix-arg))
6872   (when (equal header "Body")
6873     (setq header ""))
6874   ;; Hidden thread subtrees must be searched as well.
6875   (gnus-summary-show-all-threads)
6876   ;; We don't want to change current point nor window configuration.
6877   (save-excursion
6878     (save-window-excursion
6879       (gnus-message 6 "Executing %s..." (key-description command))
6880       ;; We'd like to execute COMMAND interactively so as to give arguments.
6881       (gnus-execute header regexp
6882                     `(call-interactively ',(key-binding command))
6883                     backward)
6884       (gnus-message 6 "Executing %s...done" (key-description command)))))
6885
6886 (defun gnus-summary-beginning-of-article ()
6887   "Scroll the article back to the beginning."
6888   (interactive)
6889   (gnus-summary-select-article)
6890   (gnus-configure-windows 'article)
6891   (gnus-eval-in-buffer-window gnus-article-buffer
6892     (widen)
6893     (goto-char (point-min))
6894     (when gnus-page-broken
6895       (gnus-narrow-to-page))))
6896
6897 (defun gnus-summary-end-of-article ()
6898   "Scroll to the end of the article."
6899   (interactive)
6900   (gnus-summary-select-article)
6901   (gnus-configure-windows 'article)
6902   (gnus-eval-in-buffer-window gnus-article-buffer
6903     (widen)
6904     (goto-char (point-max))
6905     (recenter -3)
6906     (when gnus-page-broken
6907       (gnus-narrow-to-page))))
6908
6909 (defun gnus-summary-print-article (&optional filename n)
6910   "Generate and print a PostScript image of the N next (mail) articles.
6911
6912 If N is negative, print the N previous articles.  If N is nil and articles
6913 have been marked with the process mark, print these instead.
6914
6915 If the optional first argument FILENAME is nil, send the image to the
6916 printer.  If FILENAME is a string, save the PostScript image in a file with
6917 that name.  If FILENAME is a number, prompt the user for the name of the file
6918 to save in."
6919   (interactive (list (ps-print-preprint current-prefix-arg)
6920                      current-prefix-arg))
6921   (dolist (article (gnus-summary-work-articles n))
6922     (gnus-summary-select-article nil nil 'pseudo article)
6923     (gnus-eval-in-buffer-window gnus-article-buffer
6924       (let ((buffer (generate-new-buffer " *print*")))
6925         (unwind-protect
6926             (progn
6927               (copy-to-buffer buffer (point-min) (point-max))
6928               (set-buffer buffer)
6929               (gnus-article-delete-invisible-text)
6930               (let ((ps-left-header
6931                      (list
6932                       (concat "("
6933                               (mail-header-subject gnus-current-headers) ")")
6934                       (concat "("
6935                               (mail-header-from gnus-current-headers) ")")))
6936                     (ps-right-header
6937                      (list
6938                       "/pagenumberstring load"
6939                       (concat "("
6940                               (mail-header-date gnus-current-headers) ")"))))
6941                 (gnus-run-hooks 'gnus-ps-print-hook)
6942                 (save-excursion
6943                   (ps-print-buffer-with-faces filename))))
6944           (kill-buffer buffer))))))
6945
6946 (defun gnus-summary-show-article (&optional arg)
6947   "Force re-fetching of the current article.
6948 If ARG (the prefix) is non-nil, show the raw article without any
6949 article massaging functions being run."
6950   (interactive "P")
6951   (if (not arg)
6952       ;; Select the article the normal way.
6953       (gnus-summary-select-article nil 'force)
6954     ;; We have to require this here to make sure that the following
6955     ;; dynamic binding isn't shadowed by autoloading.
6956     (require 'gnus-async)
6957     (require 'gnus-art)
6958     ;; Bind the article treatment functions to nil.
6959     (let ((gnus-have-all-headers t)
6960           gnus-article-display-hook
6961           gnus-article-prepare-hook
6962           gnus-article-decode-hook
6963           gnus-break-pages
6964           gnus-show-mime
6965           gnus-visual)
6966       (gnus-summary-select-article nil 'force)))
6967   (gnus-summary-goto-subject gnus-current-article)
6968   (gnus-summary-position-point))
6969
6970 (defun gnus-summary-verbose-headers (&optional arg)
6971   "Toggle permanent full header display.
6972 If ARG is a positive number, turn header display on.
6973 If ARG is a negative number, turn header display off."
6974   (interactive "P")
6975   (setq gnus-show-all-headers
6976         (cond ((or (not (numberp arg))
6977                    (zerop arg))
6978                (not gnus-show-all-headers))
6979               ((natnump arg)
6980                t)))
6981   (gnus-summary-show-article))
6982
6983 (defun gnus-summary-toggle-header (&optional arg)
6984   "Show the headers if they are hidden, or hide them if they are shown.
6985 If ARG is a positive number, show the entire header.
6986 If ARG is a negative number, hide the unwanted header lines."
6987   (interactive "P")
6988   (save-excursion
6989     (set-buffer gnus-article-buffer)
6990     (save-restriction
6991       (let* ((buffer-read-only nil)
6992              (inhibit-point-motion-hooks t)
6993              (hidden (gnus-article-hidden-text-p 'headers))
6994              e)
6995         (goto-char (point-min))
6996         (when (search-forward "\n\n" nil t)
6997           (delete-region (point-min) (1- (point))))
6998         (goto-char (point-min))
6999         (save-excursion
7000           (set-buffer gnus-original-article-buffer)
7001           (goto-char (point-min))
7002           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7003         (insert-buffer-substring gnus-original-article-buffer 1 e)
7004         (save-restriction
7005           (narrow-to-region (point-min) (point))
7006           (if (or hidden
7007                   (and (numberp arg) (< arg 0)))
7008               (let ((gnus-treat-hide-headers nil)
7009                     (gnus-treat-hide-boring-headers nil))
7010                 (gnus-treat-article 'head))
7011             (gnus-treat-article 'head)))))))
7012
7013 (defun gnus-summary-show-all-headers ()
7014   "Make all header lines visible."
7015   (interactive)
7016   (gnus-article-show-all-headers))
7017
7018 (defun gnus-summary-toggle-mime (&optional arg)
7019   "Toggle MIME processing.
7020 If ARG is a positive number, turn MIME processing on."
7021   (interactive "P")
7022   (setq gnus-show-mime
7023         (if (null arg)
7024             (not gnus-show-mime)
7025           (> (prefix-numeric-value arg) 0)))
7026   (gnus-summary-select-article t 'force))
7027
7028 (defun gnus-summary-caesar-message (&optional arg)
7029   "Caesar rotate the current article by 13.
7030 The numerical prefix specifies how many places to rotate each letter
7031 forward."
7032   (interactive "P")
7033   (gnus-summary-select-article)
7034   (let ((mail-header-separator ""))
7035     (gnus-eval-in-buffer-window gnus-article-buffer
7036       (save-restriction
7037         (widen)
7038         (let ((start (window-start))
7039               buffer-read-only)
7040           (message-caesar-buffer-body arg)
7041           (set-window-start (get-buffer-window (current-buffer)) start))))))
7042
7043 (defun gnus-summary-stop-page-breaking ()
7044   "Stop page breaking in the current article."
7045   (interactive)
7046   (gnus-summary-select-article)
7047   (gnus-eval-in-buffer-window gnus-article-buffer
7048     (widen)
7049     (when (gnus-visual-p 'page-marker)
7050       (let ((buffer-read-only nil))
7051         (gnus-remove-text-with-property 'gnus-prev)
7052         (gnus-remove-text-with-property 'gnus-next))
7053       (setq gnus-page-broken nil))))
7054
7055 (defun gnus-summary-move-article (&optional n to-newsgroup
7056                                             select-method action)
7057   "Move the current article to a different newsgroup.
7058 If N is a positive number, move the N next articles.
7059 If N is a negative number, move the N previous articles.
7060 If N is nil and any articles have been marked with the process mark,
7061 move those articles instead.
7062 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7063 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7064 re-spool using this method.
7065
7066 For this function to work, both the current newsgroup and the
7067 newsgroup that you want to move to have to support the `request-move'
7068 and `request-accept' functions."
7069   (interactive "P")
7070   (unless action
7071     (setq action 'move))
7072   ;; Disable marking as read.
7073   (let (gnus-mark-article-hook)
7074     (save-window-excursion
7075       (gnus-summary-select-article)))
7076   ;; Check whether the source group supports the required functions.
7077   (cond ((and (eq action 'move)
7078               (not (gnus-check-backend-function
7079                     'request-move-article gnus-newsgroup-name)))
7080          (error "The current group does not support article moving"))
7081         ((and (eq action 'crosspost)
7082               (not (gnus-check-backend-function
7083                     'request-replace-article gnus-newsgroup-name)))
7084          (error "The current group does not support article editing")))
7085   (let ((articles (gnus-summary-work-articles n))
7086         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7087         (names '((move "Move" "Moving")
7088                  (copy "Copy" "Copying")
7089                  (crosspost "Crosspost" "Crossposting")))
7090         (copy-buf (save-excursion
7091                     (nnheader-set-temp-buffer " *copy article*")))
7092         art-group to-method new-xref article to-groups)
7093     (unless (assq action names)
7094       (error "Unknown action %s" action))
7095     ;; Read the newsgroup name.
7096     (when (and (not to-newsgroup)
7097                (not select-method))
7098       (setq to-newsgroup
7099             (gnus-read-move-group-name
7100              (cadr (assq action names))
7101              (symbol-value (intern (format "gnus-current-%s-group" action)))
7102              articles prefix))
7103       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7104     (setq to-method (or select-method
7105                         (gnus-group-name-to-method to-newsgroup)))
7106     ;; Check the method we are to move this article to...
7107     (unless (gnus-check-backend-function
7108              'request-accept-article (car to-method))
7109       (error "%s does not support article copying" (car to-method)))
7110     (unless (gnus-check-server to-method)
7111       (error "Can't open server %s" (car to-method)))
7112     (gnus-message 6 "%s to %s: %s..."
7113                   (caddr (assq action names))
7114                   (or (car select-method) to-newsgroup) articles)
7115     (while articles
7116       (setq article (pop articles))
7117       (setq
7118        art-group
7119        (cond
7120         ;; Move the article.
7121         ((eq action 'move)
7122          ;; Remove this article from future suppression.
7123          (gnus-dup-unsuppress-article article)
7124          (gnus-request-move-article
7125           article                       ; Article to move
7126           gnus-newsgroup-name           ; From newsgroup
7127           (nth 1 (gnus-find-method-for-group
7128                   gnus-newsgroup-name)) ; Server
7129           (list 'gnus-request-accept-article
7130                 to-newsgroup (list 'quote select-method)
7131                 (not articles) t)               ; Accept form
7132           (not articles)))              ; Only save nov last time
7133         ;; Copy the article.
7134         ((eq action 'copy)
7135          (save-excursion
7136            (set-buffer copy-buf)
7137            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7138              (gnus-request-accept-article
7139               to-newsgroup select-method (not articles) t))))
7140         ;; Crosspost the article.
7141         ((eq action 'crosspost)
7142          (let ((xref (message-tokenize-header
7143                       (mail-header-xref (gnus-summary-article-header article))
7144                       " ")))
7145            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7146                                   ":" article))
7147            (unless xref
7148              (setq xref (list (system-name))))
7149            (setq new-xref
7150                  (concat
7151                   (mapconcat 'identity
7152                              (delete "Xref:" (delete new-xref xref))
7153                              " ")
7154                   " " new-xref))
7155            (save-excursion
7156              (set-buffer copy-buf)
7157              ;; First put the article in the destination group.
7158              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7159              (when (consp (setq art-group
7160                                 (gnus-request-accept-article
7161                                  to-newsgroup select-method (not articles))))
7162                (setq new-xref (concat new-xref " " (car art-group)
7163                                       ":" (cdr art-group)))
7164                ;; Now we have the new Xrefs header, so we insert
7165                ;; it and replace the new article.
7166                (nnheader-replace-header "Xref" new-xref)
7167                (gnus-request-replace-article
7168                 (cdr art-group) to-newsgroup (current-buffer))
7169                art-group))))))
7170       (cond
7171        ((not art-group)
7172         (gnus-message 1 "Couldn't %s article %s"
7173                       (cadr (assq action names)) article))
7174        ((and (eq art-group 'junk)
7175              (eq action 'move))
7176         (gnus-summary-mark-article article gnus-canceled-mark)
7177         (gnus-message 4 "Deleted article %s" article))
7178        (t
7179         (let* ((pto-group (gnus-group-prefixed-name
7180                            (car art-group) to-method))
7181                (entry
7182                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7183                (info (nth 2 entry))
7184                (to-group (gnus-info-group info)))
7185           ;; Update the group that has been moved to.
7186           (when (and info
7187                      (memq action '(move copy)))
7188             (unless (member to-group to-groups)
7189               (push to-group to-groups))
7190
7191             (unless (memq article gnus-newsgroup-unreads)
7192               (gnus-info-set-read
7193                info (gnus-add-to-range (gnus-info-read info)
7194                                        (list (cdr art-group)))))
7195
7196             ;; Copy any marks over to the new group.
7197             (let ((marks gnus-article-mark-lists)
7198                   (to-article (cdr art-group)))
7199               (unless (gnus-group-auto-expirable-p to-group)
7200                 (setq marks (delete '(expirable . expire) marks)))
7201
7202               ;; See whether the article is to be put in the cache.
7203               (when gnus-use-cache
7204                 (gnus-cache-possibly-enter-article
7205                  to-group to-article
7206                  (let ((header (copy-sequence
7207                                 (gnus-summary-article-header article))))
7208                    (mail-header-set-number header to-article)
7209                    header)
7210                  (memq article gnus-newsgroup-marked)
7211                  (memq article gnus-newsgroup-dormant)
7212                  (memq article gnus-newsgroup-unreads)))
7213
7214               (when (and (equal to-group gnus-newsgroup-name)
7215                          (not (memq article gnus-newsgroup-unreads)))
7216                 ;; Mark this article as read in this group.
7217                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7218                 (setcdr (gnus-active to-group) to-article)
7219                 (setcdr gnus-newsgroup-active to-article))
7220
7221               (while marks
7222                 (when (memq article (symbol-value
7223                                      (intern (format "gnus-newsgroup-%s"
7224                                                      (caar marks)))))
7225                   ;; If the other group is the same as this group,
7226                   ;; then we have to add the mark to the list.
7227                   (when (equal to-group gnus-newsgroup-name)
7228                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7229                          (cons to-article
7230                                (symbol-value
7231                                 (intern (format "gnus-newsgroup-%s"
7232                                                 (caar marks)))))))
7233                   ;; Copy the marks to other group.
7234                   (gnus-add-marked-articles
7235                    to-group (cdar marks) (list to-article) info))
7236                 (setq marks (cdr marks)))
7237
7238               (gnus-dribble-enter
7239                (concat "(gnus-group-set-info '"
7240                        (gnus-prin1-to-string (gnus-get-info to-group))
7241                        ")"))))
7242
7243           ;; Update the Xref header in this article to point to
7244           ;; the new crossposted article we have just created.
7245           (when (eq action 'crosspost)
7246             (save-excursion
7247               (set-buffer copy-buf)
7248               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7249               (nnheader-replace-header "Xref" new-xref)
7250               (gnus-request-replace-article
7251                article gnus-newsgroup-name (current-buffer)))))
7252
7253         ;;;!!!Why is this necessary?
7254         (set-buffer gnus-summary-buffer)
7255
7256         (gnus-summary-goto-subject article)
7257         (when (eq action 'move)
7258           (gnus-summary-mark-article article gnus-canceled-mark))))
7259       (gnus-summary-remove-process-mark article))
7260     ;; Re-activate all groups that have been moved to.
7261     (while to-groups
7262       (save-excursion
7263         (set-buffer gnus-group-buffer)
7264         (when (gnus-group-goto-group (car to-groups) t)
7265           (gnus-group-get-new-news-this-group 1 t))
7266         (pop to-groups)))
7267
7268     (gnus-kill-buffer copy-buf)
7269     (gnus-summary-position-point)
7270     (gnus-set-mode-line 'summary)))
7271
7272 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7273   "Move the current article to a different newsgroup.
7274 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7275 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7276 re-spool using this method."
7277   (interactive "P")
7278   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7279
7280 (defun gnus-summary-crosspost-article (&optional n)
7281   "Crosspost the current article to some other group."
7282   (interactive "P")
7283   (gnus-summary-move-article n nil nil 'crosspost))
7284
7285 (defcustom gnus-summary-respool-default-method nil
7286   "Default method for respooling an article.
7287 If nil, use to the current newsgroup method."
7288   :type '(choice (gnus-select-method :value (nnml ""))
7289                  (const nil))
7290   :group 'gnus-summary-mail)
7291
7292 (defun gnus-summary-respool-article (&optional n method)
7293   "Respool the current article.
7294 The article will be squeezed through the mail spooling process again,
7295 which means that it will be put in some mail newsgroup or other
7296 depending on `nnmail-split-methods'.
7297 If N is a positive number, respool the N next articles.
7298 If N is a negative number, respool the N previous articles.
7299 If N is nil and any articles have been marked with the process mark,
7300 respool those articles instead.
7301
7302 Respooling can be done both from mail groups and \"real\" newsgroups.
7303 In the former case, the articles in question will be moved from the
7304 current group into whatever groups they are destined to.  In the
7305 latter case, they will be copied into the relevant groups."
7306   (interactive
7307    (list current-prefix-arg
7308          (let* ((methods (gnus-methods-using 'respool))
7309                 (methname
7310                  (symbol-name (or gnus-summary-respool-default-method
7311                                   (car (gnus-find-method-for-group
7312                                         gnus-newsgroup-name)))))
7313                 (method
7314                  (gnus-completing-read
7315                   methname "What backend do you want to use when respooling?"
7316                   methods nil t nil 'gnus-mail-method-history))
7317                 ms)
7318            (cond
7319             ((zerop (length (setq ms (gnus-servers-using-backend
7320                                       (intern method)))))
7321              (list (intern method) ""))
7322             ((= 1 (length ms))
7323              (car ms))
7324             (t
7325              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7326                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7327                            ms-alist))))))))
7328   (unless method
7329     (error "No method given for respooling"))
7330   (if (assoc (symbol-name
7331               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7332              (gnus-methods-using 'respool))
7333       (gnus-summary-move-article n nil method)
7334     (gnus-summary-copy-article n nil method)))
7335
7336 (defun gnus-summary-import-article (file)
7337   "Import an arbitrary file into a mail newsgroup."
7338   (interactive "fImport file: ")
7339   (let ((group gnus-newsgroup-name)
7340         (now (current-time))
7341         atts lines)
7342     (unless (gnus-check-backend-function 'request-accept-article group)
7343       (error "%s does not support article importing" group))
7344     (or (file-readable-p file)
7345         (not (file-regular-p file))
7346         (error "Can't read %s" file))
7347     (save-excursion
7348       (set-buffer (gnus-get-buffer-create " *import file*"))
7349       (erase-buffer)
7350       (nnheader-insert-file-contents file)
7351       (goto-char (point-min))
7352       (unless (nnheader-article-p)
7353         ;; This doesn't look like an article, so we fudge some headers.
7354         (setq atts (file-attributes file)
7355               lines (count-lines (point-min) (point-max)))
7356         (insert "From: " (read-string "From: ") "\n"
7357                 "Subject: " (read-string "Subject: ") "\n"
7358                 "Date: " (message-make-date (nth 5 atts))
7359                 "\n"
7360                 "Message-ID: " (message-make-message-id) "\n"
7361                 "Lines: " (int-to-string lines) "\n"
7362                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7363       (gnus-request-accept-article group nil t)
7364       (kill-buffer (current-buffer)))))
7365
7366 (defun gnus-summary-article-posted-p ()
7367   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7368 This will be the case if the article has both been mailed and posted."
7369   (interactive)
7370   (let ((id (mail-header-references (gnus-summary-article-header)))
7371         (gnus-override-method
7372          (or gnus-refer-article-method gnus-select-method)))
7373     (if (gnus-request-head id "")
7374         (gnus-message 2 "The current message was found on %s"
7375                       gnus-override-method)
7376       (gnus-message 2 "The current message couldn't be found on %s"
7377                     gnus-override-method)
7378       nil)))
7379
7380 (defun gnus-summary-expire-articles (&optional now)
7381   "Expire all articles that are marked as expirable in the current group."
7382   (interactive)
7383   (when (gnus-check-backend-function
7384          'request-expire-articles gnus-newsgroup-name)
7385     ;; This backend supports expiry.
7386     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7387            (expirable (if total
7388                           (progn
7389                             ;; We need to update the info for
7390                             ;; this group for `gnus-list-of-read-articles'
7391                             ;; to give us the right answer.
7392                             (gnus-run-hooks 'gnus-exit-group-hook)
7393                             (gnus-summary-update-info)
7394                             (gnus-list-of-read-articles gnus-newsgroup-name))
7395                         (setq gnus-newsgroup-expirable
7396                               (sort gnus-newsgroup-expirable '<))))
7397            (expiry-wait (if now 'immediate
7398                           (gnus-group-find-parameter
7399                            gnus-newsgroup-name 'expiry-wait)))
7400            es)
7401       (when expirable
7402         ;; There are expirable articles in this group, so we run them
7403         ;; through the expiry process.
7404         (gnus-message 6 "Expiring articles...")
7405         ;; The list of articles that weren't expired is returned.
7406         (save-excursion
7407           (if expiry-wait
7408               (let ((nnmail-expiry-wait-function nil)
7409                     (nnmail-expiry-wait expiry-wait))
7410                 (setq es (gnus-request-expire-articles
7411                           expirable gnus-newsgroup-name)))
7412             (setq es (gnus-request-expire-articles
7413                       expirable gnus-newsgroup-name))))
7414         (unless total
7415           (setq gnus-newsgroup-expirable es))
7416         ;; We go through the old list of expirable, and mark all
7417         ;; really expired articles as nonexistent.
7418         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7419           (let ((gnus-use-cache nil))
7420             (while expirable
7421               (unless (memq (car expirable) es)
7422                 (when (gnus-data-find (car expirable))
7423                   (gnus-summary-mark-article
7424                    (car expirable) gnus-canceled-mark)))
7425               (setq expirable (cdr expirable)))))
7426         (gnus-message 6 "Expiring articles...done")))))
7427
7428 (defun gnus-summary-expire-articles-now ()
7429   "Expunge all expirable articles in the current group.
7430 This means that *all* articles that are marked as expirable will be
7431 deleted forever, right now."
7432   (interactive)
7433   (or gnus-expert-user
7434       (gnus-yes-or-no-p
7435        "Are you really, really, really sure you want to delete all these messages? ")
7436       (error "Phew!"))
7437   (gnus-summary-expire-articles t))
7438
7439 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7440 (defun gnus-summary-delete-article (&optional n)
7441   "Delete the N next (mail) articles.
7442 This command actually deletes articles.  This is not a marking
7443 command.  The article will disappear forever from your life, never to
7444 return.
7445 If N is negative, delete backwards.
7446 If N is nil and articles have been marked with the process mark,
7447 delete these instead."
7448   (interactive "P")
7449   (unless (gnus-check-backend-function 'request-expire-articles
7450                                        gnus-newsgroup-name)
7451     (error "The current newsgroup does not support article deletion"))
7452   ;; Compute the list of articles to delete.
7453   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7454         not-deleted)
7455     (if (and gnus-novice-user
7456              (not (gnus-yes-or-no-p
7457                    (format "Do you really want to delete %s forever? "
7458                            (if (> (length articles) 1)
7459                                (format "these %s articles" (length articles))
7460                              "this article")))))
7461         ()
7462       ;; Delete the articles.
7463       (setq not-deleted (gnus-request-expire-articles
7464                          articles gnus-newsgroup-name 'force))
7465       (while articles
7466         (gnus-summary-remove-process-mark (car articles))
7467         ;; The backend might not have been able to delete the article
7468         ;; after all.
7469         (unless (memq (car articles) not-deleted)
7470           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7471         (setq articles (cdr articles)))
7472       (when not-deleted
7473         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7474     (gnus-summary-position-point)
7475     (gnus-set-mode-line 'summary)
7476     not-deleted))
7477
7478 (defun gnus-summary-edit-article (&optional force)
7479   "Edit the current article.
7480 This will have permanent effect only in mail groups.
7481 If FORCE is non-nil, allow editing of articles even in read-only
7482 groups."
7483   (interactive "P")
7484   (let ((mail-parse-charset gnus-newsgroup-charset))
7485     (save-excursion
7486       (set-buffer gnus-summary-buffer)
7487       (gnus-set-global-variables)
7488       (when (and (not force)
7489                  (gnus-group-read-only-p))
7490         (error "The current newsgroup does not support article editing"))
7491       (gnus-summary-show-article t)
7492       (gnus-article-edit-article
7493        'mime-to-mml
7494        `(lambda (no-highlight)
7495           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7496             (mml-to-mime)
7497             (gnus-summary-edit-article-done
7498              ,(or (mail-header-references gnus-current-headers) "")
7499              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7500
7501 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7502
7503 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7504                                                  no-highlight)
7505   "Make edits to the current article permanent."
7506   (interactive)
7507   ;; Replace the article.
7508   (let ((buf (current-buffer)))
7509     (with-temp-buffer
7510       (insert-buffer-substring buf)
7511       (if (and (not read-only)
7512                (not (gnus-request-replace-article
7513                      (cdr gnus-article-current) (car gnus-article-current)
7514                      (current-buffer) t)))
7515           (error "Couldn't replace article")
7516         ;; Update the summary buffer.
7517         (if (and references
7518                  (equal (message-tokenize-header references " ")
7519                         (message-tokenize-header
7520                          (or (message-fetch-field "references") "") " ")))
7521             ;; We only have to update this line.
7522             (save-excursion
7523               (save-restriction
7524                 (message-narrow-to-head)
7525                 (let ((head (buffer-string))
7526                       header)
7527                   (with-temp-buffer
7528                     (insert (format "211 %d Article retrieved.\n"
7529                                     (cdr gnus-article-current)))
7530                     (insert head)
7531                     (insert ".\n")
7532                     (let ((nntp-server-buffer (current-buffer)))
7533                       (setq header (car (gnus-get-newsgroup-headers
7534                                          (save-excursion
7535                                            (set-buffer gnus-summary-buffer)
7536                                            gnus-newsgroup-dependencies)
7537                                          t))))
7538                     (save-excursion
7539                       (set-buffer gnus-summary-buffer)
7540                       (gnus-data-set-header
7541                        (gnus-data-find (cdr gnus-article-current))
7542                        header)
7543                       (gnus-summary-update-article-line
7544                        (cdr gnus-article-current) header))))))
7545           ;; Update threads.
7546           (set-buffer (or buffer gnus-summary-buffer))
7547           (gnus-summary-update-article (cdr gnus-article-current)))
7548         ;; Prettify the article buffer again.
7549         (unless no-highlight
7550           (save-excursion
7551             (set-buffer gnus-article-buffer)
7552             ;;;!!! Fix this -- article should be rehighlighted.
7553             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7554             (set-buffer gnus-original-article-buffer)
7555             (gnus-request-article
7556              (cdr gnus-article-current)
7557              (car gnus-article-current) (current-buffer))))
7558         ;; Prettify the summary buffer line.
7559         (when (gnus-visual-p 'summary-highlight 'highlight)
7560           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7561
7562 (defun gnus-summary-edit-wash (key)
7563   "Perform editing command KEY in the article buffer."
7564   (interactive
7565    (list
7566     (progn
7567       (message "%s" (concat (this-command-keys) "- "))
7568       (read-char))))
7569   (message "")
7570   (gnus-summary-edit-article)
7571   (execute-kbd-macro (concat (this-command-keys) key))
7572   (gnus-article-edit-done))
7573
7574 ;;; Respooling
7575
7576 (defun gnus-summary-respool-query (&optional silent trace)
7577   "Query where the respool algorithm would put this article."
7578   (interactive)
7579   (let (gnus-mark-article-hook)
7580     (gnus-summary-select-article)
7581     (save-excursion
7582       (set-buffer gnus-original-article-buffer)
7583       (save-restriction
7584         (message-narrow-to-head)
7585         (let ((groups (nnmail-article-group 'identity trace)))
7586           (unless silent
7587             (if groups
7588                 (message "This message would go to %s"
7589                          (mapconcat 'car groups ", "))
7590               (message "This message would go to no groups"))
7591             groups))))))
7592
7593 (defun gnus-summary-respool-trace ()
7594   "Trace where the respool algorithm would put this article.
7595 Display a buffer showing all fancy splitting patterns which matched."
7596   (interactive)
7597   (gnus-summary-respool-query nil t))
7598
7599 ;; Summary marking commands.
7600
7601 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7602   "Mark articles which has the same subject as read, and then select the next.
7603 If UNMARK is positive, remove any kind of mark.
7604 If UNMARK is negative, tick articles."
7605   (interactive "P")
7606   (when unmark
7607     (setq unmark (prefix-numeric-value unmark)))
7608   (let ((count
7609          (gnus-summary-mark-same-subject
7610           (gnus-summary-article-subject) unmark)))
7611     ;; Select next unread article.  If auto-select-same mode, should
7612     ;; select the first unread article.
7613     (gnus-summary-next-article t (and gnus-auto-select-same
7614                                       (gnus-summary-article-subject)))
7615     (gnus-message 7 "%d article%s marked as %s"
7616                   count (if (= count 1) " is" "s are")
7617                   (if unmark "unread" "read"))))
7618
7619 (defun gnus-summary-kill-same-subject (&optional unmark)
7620   "Mark articles which has the same subject as read.
7621 If UNMARK is positive, remove any kind of mark.
7622 If UNMARK is negative, tick articles."
7623   (interactive "P")
7624   (when unmark
7625     (setq unmark (prefix-numeric-value unmark)))
7626   (let ((count
7627          (gnus-summary-mark-same-subject
7628           (gnus-summary-article-subject) unmark)))
7629     ;; If marked as read, go to next unread subject.
7630     (when (null unmark)
7631       ;; Go to next unread subject.
7632       (gnus-summary-next-subject 1 t))
7633     (gnus-message 7 "%d articles are marked as %s"
7634                   count (if unmark "unread" "read"))))
7635
7636 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7637   "Mark articles with same SUBJECT as read, and return marked number.
7638 If optional argument UNMARK is positive, remove any kinds of marks.
7639 If optional argument UNMARK is negative, mark articles as unread instead."
7640   (let ((count 1))
7641     (save-excursion
7642       (cond
7643        ((null unmark)                   ; Mark as read.
7644         (while (and
7645                 (progn
7646                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7647                   (gnus-summary-show-thread) t)
7648                 (gnus-summary-find-subject subject))
7649           (setq count (1+ count))))
7650        ((> unmark 0)                    ; Tick.
7651         (while (and
7652                 (progn
7653                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7654                   (gnus-summary-show-thread) t)
7655                 (gnus-summary-find-subject subject))
7656           (setq count (1+ count))))
7657        (t                               ; Mark as unread.
7658         (while (and
7659                 (progn
7660                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7661                   (gnus-summary-show-thread) t)
7662                 (gnus-summary-find-subject subject))
7663           (setq count (1+ count)))))
7664       (gnus-set-mode-line 'summary)
7665       ;; Return the number of marked articles.
7666       count)))
7667
7668 (defun gnus-summary-mark-as-processable (n &optional unmark)
7669   "Set the process mark on the next N articles.
7670 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7671 the process mark instead.  The difference between N and the actual
7672 number of articles marked is returned."
7673   (interactive "p")
7674   (let ((backward (< n 0))
7675         (n (abs n)))
7676     (while (and
7677             (> n 0)
7678             (if unmark
7679                 (gnus-summary-remove-process-mark
7680                  (gnus-summary-article-number))
7681               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7682             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7683       (setq n (1- n)))
7684     (when (/= 0 n)
7685       (gnus-message 7 "No more articles"))
7686     (gnus-summary-recenter)
7687     (gnus-summary-position-point)
7688     n))
7689
7690 (defun gnus-summary-unmark-as-processable (n)
7691   "Remove the process mark from the next N articles.
7692 If N is negative, unmark backward instead.  The difference between N and
7693 the actual number of articles unmarked is returned."
7694   (interactive "p")
7695   (gnus-summary-mark-as-processable n t))
7696
7697 (defun gnus-summary-unmark-all-processable ()
7698   "Remove the process mark from all articles."
7699   (interactive)
7700   (save-excursion
7701     (while gnus-newsgroup-processable
7702       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7703   (gnus-summary-position-point))
7704
7705 (defun gnus-summary-mark-as-expirable (n)
7706   "Mark N articles forward as expirable.
7707 If N is negative, mark backward instead.  The difference between N and
7708 the actual number of articles marked is returned."
7709   (interactive "p")
7710   (gnus-summary-mark-forward n gnus-expirable-mark))
7711
7712 (defun gnus-summary-mark-article-as-replied (article)
7713   "Mark ARTICLE replied and update the summary line."
7714   (push article gnus-newsgroup-replied)
7715   (let ((buffer-read-only nil))
7716     (when (gnus-summary-goto-subject article nil t)
7717       (gnus-summary-update-secondary-mark article))))
7718
7719 (defun gnus-summary-set-bookmark (article)
7720   "Set a bookmark in current article."
7721   (interactive (list (gnus-summary-article-number)))
7722   (when (or (not (get-buffer gnus-article-buffer))
7723             (not gnus-current-article)
7724             (not gnus-article-current)
7725             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7726     (error "No current article selected"))
7727   ;; Remove old bookmark, if one exists.
7728   (let ((old (assq article gnus-newsgroup-bookmarks)))
7729     (when old
7730       (setq gnus-newsgroup-bookmarks
7731             (delq old gnus-newsgroup-bookmarks))))
7732   ;; Set the new bookmark, which is on the form
7733   ;; (article-number . line-number-in-body).
7734   (push
7735    (cons article
7736          (save-excursion
7737            (set-buffer gnus-article-buffer)
7738            (count-lines
7739             (min (point)
7740                  (save-excursion
7741                    (goto-char (point-min))
7742                    (search-forward "\n\n" nil t)
7743                    (point)))
7744             (point))))
7745    gnus-newsgroup-bookmarks)
7746   (gnus-message 6 "A bookmark has been added to the current article."))
7747
7748 (defun gnus-summary-remove-bookmark (article)
7749   "Remove the bookmark from the current article."
7750   (interactive (list (gnus-summary-article-number)))
7751   ;; Remove old bookmark, if one exists.
7752   (let ((old (assq article gnus-newsgroup-bookmarks)))
7753     (if old
7754         (progn
7755           (setq gnus-newsgroup-bookmarks
7756                 (delq old gnus-newsgroup-bookmarks))
7757           (gnus-message 6 "Removed bookmark."))
7758       (gnus-message 6 "No bookmark in current article."))))
7759
7760 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7761 (defun gnus-summary-mark-as-dormant (n)
7762   "Mark N articles forward as dormant.
7763 If N is negative, mark backward instead.  The difference between N and
7764 the actual number of articles marked is returned."
7765   (interactive "p")
7766   (gnus-summary-mark-forward n gnus-dormant-mark))
7767
7768 (defun gnus-summary-set-process-mark (article)
7769   "Set the process mark on ARTICLE and update the summary line."
7770   (setq gnus-newsgroup-processable
7771         (cons article
7772               (delq article gnus-newsgroup-processable)))
7773   (when (gnus-summary-goto-subject article)
7774     (gnus-summary-show-thread)
7775     (gnus-summary-goto-subject article)
7776     (gnus-summary-update-secondary-mark article)))
7777
7778 (defun gnus-summary-remove-process-mark (article)
7779   "Remove the process mark from ARTICLE and update the summary line."
7780   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7781   (when (gnus-summary-goto-subject article)
7782     (gnus-summary-show-thread)
7783     (gnus-summary-goto-subject article)
7784     (gnus-summary-update-secondary-mark article)))
7785
7786 (defun gnus-summary-set-saved-mark (article)
7787   "Set the process mark on ARTICLE and update the summary line."
7788   (push article gnus-newsgroup-saved)
7789   (when (gnus-summary-goto-subject article)
7790     (gnus-summary-update-secondary-mark article)))
7791
7792 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7793   "Mark N articles as read forwards.
7794 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7795 The difference between N and the actual number of articles marked is
7796 returned."
7797   (interactive "p")
7798   (gnus-summary-show-thread)
7799   (let ((backward (< n 0))
7800         (gnus-summary-goto-unread
7801          (and gnus-summary-goto-unread
7802               (not (eq gnus-summary-goto-unread 'never))
7803               (not (memq mark (list gnus-unread-mark
7804                                     gnus-ticked-mark gnus-dormant-mark)))))
7805         (n (abs n))
7806         (mark (or mark gnus-del-mark)))
7807     (while (and (> n 0)
7808                 (gnus-summary-mark-article nil mark no-expire)
7809                 (zerop (gnus-summary-next-subject
7810                         (if backward -1 1)
7811                         (and gnus-summary-goto-unread
7812                              (not (eq gnus-summary-goto-unread 'never)))
7813                         t)))
7814       (setq n (1- n)))
7815     (when (/= 0 n)
7816       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7817     (gnus-summary-recenter)
7818     (gnus-summary-position-point)
7819     (gnus-set-mode-line 'summary)
7820     n))
7821
7822 (defun gnus-summary-mark-article-as-read (mark)
7823   "Mark the current article quickly as read with MARK."
7824   (let ((article (gnus-summary-article-number)))
7825     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7826     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7827     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7828     (push (cons article mark) gnus-newsgroup-reads)
7829     ;; Possibly remove from cache, if that is used.
7830     (when gnus-use-cache
7831       (gnus-cache-enter-remove-article article))
7832     ;; Allow the backend to change the mark.
7833     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7834     ;; Check for auto-expiry.
7835     (when (and gnus-newsgroup-auto-expire
7836                (memq mark gnus-auto-expirable-marks))
7837       (setq mark gnus-expirable-mark)
7838       ;; Let the backend know about the mark change.
7839       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7840       (push article gnus-newsgroup-expirable))
7841     ;; Set the mark in the buffer.
7842     (gnus-summary-update-mark mark 'unread)
7843     t))
7844
7845 (defun gnus-summary-mark-article-as-unread (mark)
7846   "Mark the current article quickly as unread with MARK."
7847   (let* ((article (gnus-summary-article-number))
7848          (old-mark (gnus-summary-article-mark article)))
7849     ;; Allow the backend to change the mark.
7850     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7851     (if (eq mark old-mark)
7852         t
7853       (if (<= article 0)
7854           (progn
7855             (gnus-error 1 "Can't mark negative article numbers")
7856             nil)
7857         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7858         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7859         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7860         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7861         (cond ((= mark gnus-ticked-mark)
7862                (push article gnus-newsgroup-marked))
7863               ((= mark gnus-dormant-mark)
7864                (push article gnus-newsgroup-dormant))
7865               (t
7866                (push article gnus-newsgroup-unreads)))
7867         (gnus-pull article gnus-newsgroup-reads)
7868
7869         ;; See whether the article is to be put in the cache.
7870         (and gnus-use-cache
7871              (vectorp (gnus-summary-article-header article))
7872              (save-excursion
7873                (gnus-cache-possibly-enter-article
7874                 gnus-newsgroup-name article
7875                 (gnus-summary-article-header article)
7876                 (= mark gnus-ticked-mark)
7877                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7878
7879         ;; Fix the mark.
7880         (gnus-summary-update-mark mark 'unread)
7881         t))))
7882
7883 (defun gnus-summary-mark-article (&optional article mark no-expire)
7884   "Mark ARTICLE with MARK.  MARK can be any character.
7885 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7886 `??' (dormant) and `?E' (expirable).
7887 If MARK is nil, then the default character `?r' is used.
7888 If ARTICLE is nil, then the article on the current line will be
7889 marked."
7890   ;; The mark might be a string.
7891   (when (stringp mark)
7892     (setq mark (aref mark 0)))
7893   ;; If no mark is given, then we check auto-expiring.
7894   (when (null mark)
7895     (setq mark gnus-del-mark))
7896   (when (and (not no-expire)
7897              gnus-newsgroup-auto-expire
7898              (memq mark gnus-auto-expirable-marks))
7899     (setq mark gnus-expirable-mark))
7900   (let ((article (or article (gnus-summary-article-number)))
7901         (old-mark (gnus-summary-article-mark article)))
7902     ;; Allow the backend to change the mark.
7903     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7904     (if (eq mark old-mark)
7905         t
7906       (unless article
7907         (error "No article on current line"))
7908       (if (not (if (or (= mark gnus-unread-mark)
7909                        (= mark gnus-ticked-mark)
7910                        (= mark gnus-dormant-mark))
7911                    (gnus-mark-article-as-unread article mark)
7912                  (gnus-mark-article-as-read article mark)))
7913           t
7914         ;; See whether the article is to be put in the cache.
7915         (and gnus-use-cache
7916              (not (= mark gnus-canceled-mark))
7917              (vectorp (gnus-summary-article-header article))
7918              (save-excursion
7919                (gnus-cache-possibly-enter-article
7920                 gnus-newsgroup-name article
7921                 (gnus-summary-article-header article)
7922                 (= mark gnus-ticked-mark)
7923                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7924
7925         (when (gnus-summary-goto-subject article nil t)
7926           (let ((buffer-read-only nil))
7927             (gnus-summary-show-thread)
7928             ;; Fix the mark.
7929             (gnus-summary-update-mark mark 'unread)
7930             t))))))
7931
7932 (defun gnus-summary-update-secondary-mark (article)
7933   "Update the secondary (read, process, cache) mark."
7934   (gnus-summary-update-mark
7935    (cond ((memq article gnus-newsgroup-processable)
7936           gnus-process-mark)
7937          ((memq article gnus-newsgroup-cached)
7938           gnus-cached-mark)
7939          ((memq article gnus-newsgroup-replied)
7940           gnus-replied-mark)
7941          ((memq article gnus-newsgroup-saved)
7942           gnus-saved-mark)
7943          (t gnus-unread-mark))
7944    'replied)
7945   (when (gnus-visual-p 'summary-highlight 'highlight)
7946     (gnus-run-hooks 'gnus-summary-update-hook))
7947   t)
7948
7949 (defun gnus-summary-update-mark (mark type)
7950   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7951         (buffer-read-only nil))
7952     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7953     (when forward
7954       (when (looking-at "\r")
7955         (incf forward))
7956       (when (<= (+ forward (point)) (point-max))
7957         ;; Go to the right position on the line.
7958         (goto-char (+ forward (point)))
7959         ;; Replace the old mark with the new mark.
7960         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7961         ;; Optionally update the marks by some user rule.
7962         (when (eq type 'unread)
7963           (gnus-data-set-mark
7964            (gnus-data-find (gnus-summary-article-number)) mark)
7965           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7966
7967 (defun gnus-mark-article-as-read (article &optional mark)
7968   "Enter ARTICLE in the pertinent lists and remove it from others."
7969   ;; Make the article expirable.
7970   (let ((mark (or mark gnus-del-mark)))
7971     (if (= mark gnus-expirable-mark)
7972         (push article gnus-newsgroup-expirable)
7973       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7974     ;; Remove from unread and marked lists.
7975     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7976     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7977     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7978     (push (cons article mark) gnus-newsgroup-reads)
7979     ;; Possibly remove from cache, if that is used.
7980     (when gnus-use-cache
7981       (gnus-cache-enter-remove-article article))
7982     t))
7983
7984 (defun gnus-mark-article-as-unread (article &optional mark)
7985   "Enter ARTICLE in the pertinent lists and remove it from others."
7986   (let ((mark (or mark gnus-ticked-mark)))
7987     (if (<= article 0)
7988         (progn
7989           (gnus-error 1 "Can't mark negative article numbers")
7990           nil)
7991       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7992             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7993             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7994             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7995
7996       ;; Unsuppress duplicates?
7997       (when gnus-suppress-duplicates
7998         (gnus-dup-unsuppress-article article))
7999
8000       (cond ((= mark gnus-ticked-mark)
8001              (push article gnus-newsgroup-marked))
8002             ((= mark gnus-dormant-mark)
8003              (push article gnus-newsgroup-dormant))
8004             (t
8005              (push article gnus-newsgroup-unreads)))
8006       (gnus-pull article gnus-newsgroup-reads)
8007       t)))
8008
8009 (defalias 'gnus-summary-mark-as-unread-forward
8010   'gnus-summary-tick-article-forward)
8011 (make-obsolete 'gnus-summary-mark-as-unread-forward
8012                'gnus-summary-tick-article-forward)
8013 (defun gnus-summary-tick-article-forward (n)
8014   "Tick N articles forwards.
8015 If N is negative, tick backwards instead.
8016 The difference between N and the number of articles ticked is returned."
8017   (interactive "p")
8018   (gnus-summary-mark-forward n gnus-ticked-mark))
8019
8020 (defalias 'gnus-summary-mark-as-unread-backward
8021   'gnus-summary-tick-article-backward)
8022 (make-obsolete 'gnus-summary-mark-as-unread-backward
8023                'gnus-summary-tick-article-backward)
8024 (defun gnus-summary-tick-article-backward (n)
8025   "Tick N articles backwards.
8026 The difference between N and the number of articles ticked is returned."
8027   (interactive "p")
8028   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8029
8030 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8031 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8032 (defun gnus-summary-tick-article (&optional article clear-mark)
8033   "Mark current article as unread.
8034 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8035 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8036   (interactive)
8037   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8038                                        gnus-ticked-mark)))
8039
8040 (defun gnus-summary-mark-as-read-forward (n)
8041   "Mark N articles as read forwards.
8042 If N is negative, mark backwards instead.
8043 The difference between N and the actual number of articles marked is
8044 returned."
8045   (interactive "p")
8046   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8047
8048 (defun gnus-summary-mark-as-read-backward (n)
8049   "Mark the N articles as read backwards.
8050 The difference between N and the actual number of articles marked is
8051 returned."
8052   (interactive "p")
8053   (gnus-summary-mark-forward
8054    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8055
8056 (defun gnus-summary-mark-as-read (&optional article mark)
8057   "Mark current article as read.
8058 ARTICLE specifies the article to be marked as read.
8059 MARK specifies a string to be inserted at the beginning of the line."
8060   (gnus-summary-mark-article article mark))
8061
8062 (defun gnus-summary-clear-mark-forward (n)
8063   "Clear marks from N articles forward.
8064 If N is negative, clear backward instead.
8065 The difference between N and the number of marks cleared is returned."
8066   (interactive "p")
8067   (gnus-summary-mark-forward n gnus-unread-mark))
8068
8069 (defun gnus-summary-clear-mark-backward (n)
8070   "Clear marks from N articles backward.
8071 The difference between N and the number of marks cleared is returned."
8072   (interactive "p")
8073   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8074
8075 (defun gnus-summary-mark-unread-as-read ()
8076   "Intended to be used by `gnus-summary-mark-article-hook'."
8077   (when (memq gnus-current-article gnus-newsgroup-unreads)
8078     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8079
8080 (defun gnus-summary-mark-read-and-unread-as-read ()
8081   "Intended to be used by `gnus-summary-mark-article-hook'."
8082   (let ((mark (gnus-summary-article-mark)))
8083     (when (or (gnus-unread-mark-p mark)
8084               (gnus-read-mark-p mark))
8085       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8086
8087 (defun gnus-summary-mark-region-as-read (point mark all)
8088   "Mark all unread articles between point and mark as read.
8089 If given a prefix, mark all articles between point and mark as read,
8090 even ticked and dormant ones."
8091   (interactive "r\nP")
8092   (save-excursion
8093     (let (article)
8094       (goto-char point)
8095       (beginning-of-line)
8096       (while (and
8097               (< (point) mark)
8098               (progn
8099                 (when (or all
8100                           (memq (setq article (gnus-summary-article-number))
8101                                 gnus-newsgroup-unreads))
8102                   (gnus-summary-mark-article article gnus-del-mark))
8103                 t)
8104               (gnus-summary-find-next))))))
8105
8106 (defun gnus-summary-mark-below (score mark)
8107   "Mark articles with score less than SCORE with MARK."
8108   (interactive "P\ncMark: ")
8109   (setq score (if score
8110                   (prefix-numeric-value score)
8111                 (or gnus-summary-default-score 0)))
8112   (save-excursion
8113     (set-buffer gnus-summary-buffer)
8114     (goto-char (point-min))
8115     (while
8116         (progn
8117           (and (< (gnus-summary-article-score) score)
8118                (gnus-summary-mark-article nil mark))
8119           (gnus-summary-find-next)))))
8120
8121 (defun gnus-summary-kill-below (&optional score)
8122   "Mark articles with score below SCORE as read."
8123   (interactive "P")
8124   (gnus-summary-mark-below score gnus-killed-mark))
8125
8126 (defun gnus-summary-clear-above (&optional score)
8127   "Clear all marks from articles with score above SCORE."
8128   (interactive "P")
8129   (gnus-summary-mark-above score gnus-unread-mark))
8130
8131 (defun gnus-summary-tick-above (&optional score)
8132   "Tick all articles with score above SCORE."
8133   (interactive "P")
8134   (gnus-summary-mark-above score gnus-ticked-mark))
8135
8136 (defun gnus-summary-mark-above (score mark)
8137   "Mark articles with score over SCORE with MARK."
8138   (interactive "P\ncMark: ")
8139   (setq score (if score
8140                   (prefix-numeric-value score)
8141                 (or gnus-summary-default-score 0)))
8142   (save-excursion
8143     (set-buffer gnus-summary-buffer)
8144     (goto-char (point-min))
8145     (while (and (progn
8146                   (when (> (gnus-summary-article-score) score)
8147                     (gnus-summary-mark-article nil mark))
8148                   t)
8149                 (gnus-summary-find-next)))))
8150
8151 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8152 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8153 (defun gnus-summary-limit-include-expunged (&optional no-error)
8154   "Display all the hidden articles that were expunged for low scores."
8155   (interactive)
8156   (let ((buffer-read-only nil))
8157     (let ((scored gnus-newsgroup-scored)
8158           headers h)
8159       (while scored
8160         (unless (gnus-summary-goto-subject (caar scored))
8161           (and (setq h (gnus-summary-article-header (caar scored)))
8162                (< (cdar scored) gnus-summary-expunge-below)
8163                (push h headers)))
8164         (setq scored (cdr scored)))
8165       (if (not headers)
8166           (when (not no-error)
8167             (error "No expunged articles hidden"))
8168         (goto-char (point-min))
8169         (gnus-summary-prepare-unthreaded (nreverse headers))
8170         (goto-char (point-min))
8171         (gnus-summary-position-point)
8172         t))))
8173
8174 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8175   "Mark all unread articles in this newsgroup as read.
8176 If prefix argument ALL is non-nil, ticked and dormant articles will
8177 also be marked as read.
8178 If QUIETLY is non-nil, no questions will be asked.
8179 If TO-HERE is non-nil, it should be a point in the buffer.  All
8180 articles before this point will be marked as read.
8181 Note that this function will only catch up the unread article
8182 in the current summary buffer limitation.
8183 The number of articles marked as read is returned."
8184   (interactive "P")
8185   (prog1
8186       (save-excursion
8187         (when (or quietly
8188                   (not gnus-interactive-catchup) ;Without confirmation?
8189                   gnus-expert-user
8190                   (gnus-y-or-n-p
8191                    (if all
8192                        "Mark absolutely all articles as read? "
8193                      "Mark all unread articles as read? ")))
8194           (if (and not-mark
8195                    (not gnus-newsgroup-adaptive)
8196                    (not gnus-newsgroup-auto-expire)
8197                    (not gnus-suppress-duplicates)
8198                    (or (not gnus-use-cache)
8199                        (eq gnus-use-cache 'passive)))
8200               (progn
8201                 (when all
8202                   (setq gnus-newsgroup-marked nil
8203                         gnus-newsgroup-dormant nil))
8204                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8205             ;; We actually mark all articles as canceled, which we
8206             ;; have to do when using auto-expiry or adaptive scoring.
8207             (gnus-summary-show-all-threads)
8208             (when (gnus-summary-first-subject (not all) t)
8209               (while (and
8210                       (if to-here (< (point) to-here) t)
8211                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8212                       (gnus-summary-find-next (not all) nil nil t))))
8213             (gnus-set-mode-line 'summary))
8214           t))
8215     (gnus-summary-position-point)))
8216
8217 (defun gnus-summary-catchup-to-here (&optional all)
8218   "Mark all unticked articles before the current one as read.
8219 If ALL is non-nil, also mark ticked and dormant articles as read."
8220   (interactive "P")
8221   (save-excursion
8222     (gnus-save-hidden-threads
8223       (let ((beg (point)))
8224         ;; We check that there are unread articles.
8225         (when (or all (gnus-summary-find-prev))
8226           (gnus-summary-catchup all t beg)))))
8227   (gnus-summary-position-point))
8228
8229 (defun gnus-summary-catchup-all (&optional quietly)
8230   "Mark all articles in this newsgroup as read."
8231   (interactive "P")
8232   (gnus-summary-catchup t quietly))
8233
8234 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8235   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8236 If prefix argument ALL is non-nil, all articles are marked as read."
8237   (interactive "P")
8238   (when (gnus-summary-catchup all quietly nil 'fast)
8239     ;; Select next newsgroup or exit.
8240     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8241              (eq gnus-auto-select-next 'quietly))
8242         (gnus-summary-next-group nil)
8243       (gnus-summary-exit))))
8244
8245 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8246   "Mark all articles in this newsgroup as read, and then exit."
8247   (interactive "P")
8248   (gnus-summary-catchup-and-exit t quietly))
8249
8250 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8251 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8252   "Mark all articles in this group as read and select the next group.
8253 If given a prefix, mark all articles, unread as well as ticked, as
8254 read."
8255   (interactive "P")
8256   (save-excursion
8257     (gnus-summary-catchup all))
8258   (gnus-summary-next-article t nil nil t))
8259
8260 ;; Thread-based commands.
8261
8262 (defun gnus-summary-articles-in-thread (&optional article)
8263   "Return a list of all articles in the current thread.
8264 If ARTICLE is non-nil, return all articles in the thread that starts
8265 with that article."
8266   (let* ((article (or article (gnus-summary-article-number)))
8267          (data (gnus-data-find-list article))
8268          (top-level (gnus-data-level (car data)))
8269          (top-subject
8270           (cond ((null gnus-thread-operation-ignore-subject)
8271                  (gnus-simplify-subject-re
8272                   (mail-header-subject (gnus-data-header (car data)))))
8273                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8274                  (gnus-simplify-subject-fuzzy
8275                   (mail-header-subject (gnus-data-header (car data)))))
8276                 (t nil)))
8277          (end-point (save-excursion
8278                       (if (gnus-summary-go-to-next-thread)
8279                           (point) (point-max))))
8280          articles)
8281     (while (and data
8282                 (< (gnus-data-pos (car data)) end-point))
8283       (when (or (not top-subject)
8284                 (string= top-subject
8285                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8286                              (gnus-simplify-subject-fuzzy
8287                               (mail-header-subject
8288                                (gnus-data-header (car data))))
8289                            (gnus-simplify-subject-re
8290                             (mail-header-subject
8291                              (gnus-data-header (car data)))))))
8292         (push (gnus-data-number (car data)) articles))
8293       (unless (and (setq data (cdr data))
8294                    (> (gnus-data-level (car data)) top-level))
8295         (setq data nil)))
8296     ;; Return the list of articles.
8297     (nreverse articles)))
8298
8299 (defun gnus-summary-rethread-current ()
8300   "Rethread the thread the current article is part of."
8301   (interactive)
8302   (let* ((gnus-show-threads t)
8303          (article (gnus-summary-article-number))
8304          (id (mail-header-id (gnus-summary-article-header)))
8305          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8306     (unless id
8307       (error "No article on the current line"))
8308     (gnus-rebuild-thread id)
8309     (gnus-summary-goto-subject article)))
8310
8311 (defun gnus-summary-reparent-thread ()
8312   "Make the current article child of the marked (or previous) article.
8313
8314 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8315 is non-nil or the Subject: of both articles are the same."
8316   (interactive)
8317   (unless (not (gnus-group-read-only-p))
8318     (error "The current newsgroup does not support article editing"))
8319   (unless (<= (length gnus-newsgroup-processable) 1)
8320     (error "No more than one article may be marked"))
8321   (save-window-excursion
8322     (let ((gnus-article-buffer " *reparent*")
8323           (current-article (gnus-summary-article-number))
8324           ;; First grab the marked article, otherwise one line up.
8325           (parent-article (if (not (null gnus-newsgroup-processable))
8326                               (car gnus-newsgroup-processable)
8327                             (save-excursion
8328                               (if (eq (forward-line -1) 0)
8329                                   (gnus-summary-article-number)
8330                                 (error "Beginning of summary buffer"))))))
8331       (unless (not (eq current-article parent-article))
8332         (error "An article may not be self-referential"))
8333       (let ((message-id (mail-header-id
8334                          (gnus-summary-article-header parent-article))))
8335         (unless (and message-id (not (equal message-id "")))
8336           (error "No message-id in desired parent"))
8337         (gnus-with-article current-article
8338           (goto-char (point-min))
8339           (if (re-search-forward "^References: " nil t)
8340               (progn
8341                 (re-search-forward "^[^ \t]" nil t)
8342                 (forward-line -1)
8343                 (end-of-line)
8344                 (insert " " message-id))
8345             (insert "References: " message-id "\n")))
8346         (set-buffer gnus-summary-buffer)
8347         (gnus-summary-unmark-all-processable)
8348         (gnus-summary-update-article current-article)
8349         (gnus-summary-rethread-current)
8350         (gnus-message 3 "Article %d is now the child of article %d"
8351                       current-article parent-article)))))
8352
8353 (defun gnus-summary-toggle-threads (&optional arg)
8354   "Toggle showing conversation threads.
8355 If ARG is positive number, turn showing conversation threads on."
8356   (interactive "P")
8357   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8358     (setq gnus-show-threads
8359           (if (null arg) (not gnus-show-threads)
8360             (> (prefix-numeric-value arg) 0)))
8361     (gnus-summary-prepare)
8362     (gnus-summary-goto-subject current)
8363     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8364     (gnus-summary-position-point)))
8365
8366 (defun gnus-summary-show-all-threads ()
8367   "Show all threads."
8368   (interactive)
8369   (save-excursion
8370     (let ((buffer-read-only nil))
8371       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8372   (gnus-summary-position-point))
8373
8374 (defun gnus-summary-show-thread ()
8375   "Show thread subtrees.
8376 Returns nil if no thread was there to be shown."
8377   (interactive)
8378   (let ((buffer-read-only nil)
8379         (orig (point))
8380         ;; first goto end then to beg, to have point at beg after let
8381         (end (progn (end-of-line) (point)))
8382         (beg (progn (beginning-of-line) (point))))
8383     (prog1
8384         ;; Any hidden lines here?
8385         (search-forward "\r" end t)
8386       (subst-char-in-region beg end ?\^M ?\n t)
8387       (goto-char orig)
8388       (gnus-summary-position-point))))
8389
8390 (defun gnus-summary-hide-all-threads ()
8391   "Hide all thread subtrees."
8392   (interactive)
8393   (save-excursion
8394     (goto-char (point-min))
8395     (gnus-summary-hide-thread)
8396     (while (zerop (gnus-summary-next-thread 1 t))
8397       (gnus-summary-hide-thread)))
8398   (gnus-summary-position-point))
8399
8400 (defun gnus-summary-hide-thread ()
8401   "Hide thread subtrees.
8402 Returns nil if no threads were there to be hidden."
8403   (interactive)
8404   (let ((buffer-read-only nil)
8405         (start (point))
8406         (article (gnus-summary-article-number)))
8407     (goto-char start)
8408     ;; Go forward until either the buffer ends or the subthread
8409     ;; ends.
8410     (when (and (not (eobp))
8411                (or (zerop (gnus-summary-next-thread 1 t))
8412                    (goto-char (point-max))))
8413       (prog1
8414           (if (and (> (point) start)
8415                    (search-backward "\n" start t))
8416               (progn
8417                 (subst-char-in-region start (point) ?\n ?\^M)
8418                 (gnus-summary-goto-subject article))
8419             (goto-char start)
8420             nil)
8421         ;;(gnus-summary-position-point)
8422         ))))
8423
8424 (defun gnus-summary-go-to-next-thread (&optional previous)
8425   "Go to the same level (or less) next thread.
8426 If PREVIOUS is non-nil, go to previous thread instead.
8427 Return the article number moved to, or nil if moving was impossible."
8428   (let ((level (gnus-summary-thread-level))
8429         (way (if previous -1 1))
8430         (beg (point)))
8431     (forward-line way)
8432     (while (and (not (eobp))
8433                 (< level (gnus-summary-thread-level)))
8434       (forward-line way))
8435     (if (eobp)
8436         (progn
8437           (goto-char beg)
8438           nil)
8439       (setq beg (point))
8440       (prog1
8441           (gnus-summary-article-number)
8442         (goto-char beg)))))
8443
8444 (defun gnus-summary-next-thread (n &optional silent)
8445   "Go to the same level next N'th thread.
8446 If N is negative, search backward instead.
8447 Returns the difference between N and the number of skips actually
8448 done.
8449
8450 If SILENT, don't output messages."
8451   (interactive "p")
8452   (let ((backward (< n 0))
8453         (n (abs n)))
8454     (while (and (> n 0)
8455                 (gnus-summary-go-to-next-thread backward))
8456       (decf n))
8457     (unless silent
8458       (gnus-summary-position-point))
8459     (when (and (not silent) (/= 0 n))
8460       (gnus-message 7 "No more threads"))
8461     n))
8462
8463 (defun gnus-summary-prev-thread (n)
8464   "Go to the same level previous N'th thread.
8465 Returns the difference between N and the number of skips actually
8466 done."
8467   (interactive "p")
8468   (gnus-summary-next-thread (- n)))
8469
8470 (defun gnus-summary-go-down-thread ()
8471   "Go down one level in the current thread."
8472   (let ((children (gnus-summary-article-children)))
8473     (when children
8474       (gnus-summary-goto-subject (car children)))))
8475
8476 (defun gnus-summary-go-up-thread ()
8477   "Go up one level in the current thread."
8478   (let ((parent (gnus-summary-article-parent)))
8479     (when parent
8480       (gnus-summary-goto-subject parent))))
8481
8482 (defun gnus-summary-down-thread (n)
8483   "Go down thread N steps.
8484 If N is negative, go up instead.
8485 Returns the difference between N and how many steps down that were
8486 taken."
8487   (interactive "p")
8488   (let ((up (< n 0))
8489         (n (abs n)))
8490     (while (and (> n 0)
8491                 (if up (gnus-summary-go-up-thread)
8492                   (gnus-summary-go-down-thread)))
8493       (setq n (1- n)))
8494     (gnus-summary-position-point)
8495     (when (/= 0 n)
8496       (gnus-message 7 "Can't go further"))
8497     n))
8498
8499 (defun gnus-summary-up-thread (n)
8500   "Go up thread N steps.
8501 If N is negative, go up instead.
8502 Returns the difference between N and how many steps down that were
8503 taken."
8504   (interactive "p")
8505   (gnus-summary-down-thread (- n)))
8506
8507 (defun gnus-summary-top-thread ()
8508   "Go to the top of the thread."
8509   (interactive)
8510   (while (gnus-summary-go-up-thread))
8511   (gnus-summary-article-number))
8512
8513 (defun gnus-summary-kill-thread (&optional unmark)
8514   "Mark articles under current thread as read.
8515 If the prefix argument is positive, remove any kinds of marks.
8516 If the prefix argument is negative, tick articles instead."
8517   (interactive "P")
8518   (when unmark
8519     (setq unmark (prefix-numeric-value unmark)))
8520   (let ((articles (gnus-summary-articles-in-thread)))
8521     (save-excursion
8522       ;; Expand the thread.
8523       (gnus-summary-show-thread)
8524       ;; Mark all the articles.
8525       (while articles
8526         (gnus-summary-goto-subject (car articles))
8527         (cond ((null unmark)
8528                (gnus-summary-mark-article-as-read gnus-killed-mark))
8529               ((> unmark 0)
8530                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8531               (t
8532                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8533         (setq articles (cdr articles))))
8534     ;; Hide killed subtrees.
8535     (and (null unmark)
8536          gnus-thread-hide-killed
8537          (gnus-summary-hide-thread))
8538     ;; If marked as read, go to next unread subject.
8539     (when (null unmark)
8540       ;; Go to next unread subject.
8541       (gnus-summary-next-subject 1 t)))
8542   (gnus-set-mode-line 'summary))
8543
8544 ;; Summary sorting commands
8545
8546 (defun gnus-summary-sort-by-number (&optional reverse)
8547   "Sort the summary buffer by article number.
8548 Argument REVERSE means reverse order."
8549   (interactive "P")
8550   (gnus-summary-sort 'number reverse))
8551
8552 (defun gnus-summary-sort-by-author (&optional reverse)
8553   "Sort the summary buffer by author name alphabetically.
8554 If case-fold-search is non-nil, case of letters is ignored.
8555 Argument REVERSE means reverse order."
8556   (interactive "P")
8557   (gnus-summary-sort 'author reverse))
8558
8559 (defun gnus-summary-sort-by-subject (&optional reverse)
8560   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8561 If case-fold-search is non-nil, case of letters is ignored.
8562 Argument REVERSE means reverse order."
8563   (interactive "P")
8564   (gnus-summary-sort 'subject reverse))
8565
8566 (defun gnus-summary-sort-by-date (&optional reverse)
8567   "Sort the summary buffer by date.
8568 Argument REVERSE means reverse order."
8569   (interactive "P")
8570   (gnus-summary-sort 'date reverse))
8571
8572 (defun gnus-summary-sort-by-score (&optional reverse)
8573   "Sort the summary buffer by score.
8574 Argument REVERSE means reverse order."
8575   (interactive "P")
8576   (gnus-summary-sort 'score reverse))
8577
8578 (defun gnus-summary-sort-by-lines (&optional reverse)
8579   "Sort the summary buffer by the number of lines.
8580 Argument REVERSE means reverse order."
8581   (interactive "P")
8582   (gnus-summary-sort 'lines reverse))
8583
8584 (defun gnus-summary-sort-by-chars (&optional reverse)
8585   "Sort the summary buffer by article length.
8586 Argument REVERSE means reverse order."
8587   (interactive "P")
8588   (gnus-summary-sort 'chars reverse))   
8589
8590 (defun gnus-summary-sort (predicate reverse)
8591   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8592   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8593          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8594          (gnus-thread-sort-functions
8595           (if (not reverse)
8596               thread
8597             `(lambda (t1 t2)
8598                (,thread t2 t1))))
8599          (gnus-article-sort-functions
8600           (if (not reverse)
8601               article
8602             `(lambda (t1 t2)
8603                (,article t2 t1))))
8604          (buffer-read-only)
8605          (gnus-summary-prepare-hook nil))
8606     ;; We do the sorting by regenerating the threads.
8607     (gnus-summary-prepare)
8608     ;; Hide subthreads if needed.
8609     (when (and gnus-show-threads gnus-thread-hide-subtree)
8610       (gnus-summary-hide-all-threads))))
8611
8612 ;; Summary saving commands.
8613
8614 (defun gnus-summary-save-article (&optional n not-saved)
8615   "Save the current article using the default saver function.
8616 If N is a positive number, save the N next articles.
8617 If N is a negative number, save the N previous articles.
8618 If N is nil and any articles have been marked with the process mark,
8619 save those articles instead.
8620 The variable `gnus-default-article-saver' specifies the saver function."
8621   (interactive "P")
8622   (let* ((articles (gnus-summary-work-articles n))
8623          (save-buffer (save-excursion
8624                         (nnheader-set-temp-buffer " *Gnus Save*")))
8625          (num (length articles))
8626          header file)
8627     (dolist (article articles)
8628       (setq header (gnus-summary-article-header article))
8629       (if (not (vectorp header))
8630           ;; This is a pseudo-article.
8631           (if (assq 'name header)
8632               (gnus-copy-file (cdr (assq 'name header)))
8633             (gnus-message 1 "Article %d is unsaveable" article))
8634         ;; This is a real article.
8635         (save-window-excursion
8636           (gnus-summary-select-article t nil nil article))
8637         (save-excursion
8638           (set-buffer save-buffer)
8639           (erase-buffer)
8640           (insert-buffer-substring gnus-original-article-buffer))
8641         (setq file (gnus-article-save save-buffer file num))
8642         (gnus-summary-remove-process-mark article)
8643         (unless not-saved
8644           (gnus-summary-set-saved-mark article))))
8645     (gnus-kill-buffer save-buffer)
8646     (gnus-summary-position-point)
8647     (gnus-set-mode-line 'summary)
8648     n))
8649
8650 (defun gnus-summary-pipe-output (&optional arg)
8651   "Pipe the current article to a subprocess.
8652 If N is a positive number, pipe the N next articles.
8653 If N is a negative number, pipe the N previous articles.
8654 If N is nil and any articles have been marked with the process mark,
8655 pipe those articles instead."
8656   (interactive "P")
8657   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8658     (gnus-summary-save-article arg t))
8659   (gnus-configure-windows 'pipe))
8660
8661 (defun gnus-summary-save-article-mail (&optional arg)
8662   "Append the current article to an mail file.
8663 If N is a positive number, save the N next articles.
8664 If N is a negative number, save the N previous articles.
8665 If N is nil and any articles have been marked with the process mark,
8666 save those articles instead."
8667   (interactive "P")
8668   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8669     (gnus-summary-save-article arg)))
8670
8671 (defun gnus-summary-save-article-rmail (&optional arg)
8672   "Append the current article to an rmail file.
8673 If N is a positive number, save the N next articles.
8674 If N is a negative number, save the N previous articles.
8675 If N is nil and any articles have been marked with the process mark,
8676 save those articles instead."
8677   (interactive "P")
8678   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8679     (gnus-summary-save-article arg)))
8680
8681 (defun gnus-summary-save-article-file (&optional arg)
8682   "Append the current article to a file.
8683 If N is a positive number, save the N next articles.
8684 If N is a negative number, save the N previous articles.
8685 If N is nil and any articles have been marked with the process mark,
8686 save those articles instead."
8687   (interactive "P")
8688   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8689     (gnus-summary-save-article arg)))
8690
8691 (defun gnus-summary-write-article-file (&optional arg)
8692   "Write the current article to a file, deleting the previous file.
8693 If N is a positive number, save the N next articles.
8694 If N is a negative number, save the N previous articles.
8695 If N is nil and any articles have been marked with the process mark,
8696 save those articles instead."
8697   (interactive "P")
8698   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8699     (gnus-summary-save-article arg)))
8700
8701 (defun gnus-summary-save-article-body-file (&optional arg)
8702   "Append the current article body to a file.
8703 If N is a positive number, save the N next articles.
8704 If N is a negative number, save the N previous articles.
8705 If N is nil and any articles have been marked with the process mark,
8706 save those articles instead."
8707   (interactive "P")
8708   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8709     (gnus-summary-save-article arg)))
8710
8711 (defun gnus-summary-pipe-message (program)
8712   "Pipe the current article through PROGRAM."
8713   (interactive "sProgram: ")
8714   (gnus-summary-select-article)
8715   (let ((mail-header-separator ""))
8716     (gnus-eval-in-buffer-window gnus-article-buffer
8717       (save-restriction
8718         (widen)
8719         (let ((start (window-start))
8720               buffer-read-only)
8721           (message-pipe-buffer-body program)
8722           (set-window-start (get-buffer-window (current-buffer)) start))))))
8723
8724 (defun gnus-get-split-value (methods)
8725   "Return a value based on the split METHODS."
8726   (let (split-name method result match)
8727     (when methods
8728       (save-excursion
8729         (set-buffer gnus-original-article-buffer)
8730         (save-restriction
8731           (nnheader-narrow-to-headers)
8732           (while methods
8733             (goto-char (point-min))
8734             (setq method (pop methods))
8735             (setq match (car method))
8736             (when (cond
8737                    ((stringp match)
8738                     ;; Regular expression.
8739                     (ignore-errors
8740                       (re-search-forward match nil t)))
8741                    ((gnus-functionp match)
8742                     ;; Function.
8743                     (save-restriction
8744                       (widen)
8745                       (setq result (funcall match gnus-newsgroup-name))))
8746                    ((consp match)
8747                     ;; Form.
8748                     (save-restriction
8749                       (widen)
8750                       (setq result (eval match)))))
8751               (setq split-name (append (cdr method) split-name))
8752               (cond ((stringp result)
8753                      (push (expand-file-name
8754                             result gnus-article-save-directory)
8755                            split-name))
8756                     ((consp result)
8757                      (setq split-name (append result split-name)))))))))
8758     split-name))
8759
8760 (defun gnus-valid-move-group-p (group)
8761   (and (boundp group)
8762        (symbol-name group)
8763        (symbol-value group)
8764        (memq 'respool
8765              (assoc (symbol-name
8766                      (car (gnus-find-method-for-group
8767                            (symbol-name group))))
8768                     gnus-valid-select-methods))))
8769
8770 (defun gnus-read-move-group-name (prompt default articles prefix)
8771   "Read a group name."
8772   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8773          (minibuffer-confirm-incomplete nil) ; XEmacs
8774          (prom
8775           (format "%s %s to:"
8776                   prompt
8777                   (if (> (length articles) 1)
8778                       (format "these %d articles" (length articles))
8779                     "this article")))
8780          (to-newsgroup
8781           (cond
8782            ((null split-name)
8783             (gnus-completing-read default prom
8784                                   gnus-active-hashtb
8785                                   'gnus-valid-move-group-p
8786                                   nil prefix
8787                                   'gnus-group-history))
8788            ((= 1 (length split-name))
8789             (gnus-completing-read (car split-name) prom
8790                                   gnus-active-hashtb
8791                                   'gnus-valid-move-group-p
8792                                   nil nil
8793                                   'gnus-group-history))
8794            (t
8795             (gnus-completing-read nil prom
8796                                   (mapcar (lambda (el) (list el))
8797                                           (nreverse split-name))
8798                                   nil nil nil
8799                                   'gnus-group-history)))))
8800     (when to-newsgroup
8801       (if (or (string= to-newsgroup "")
8802               (string= to-newsgroup prefix))
8803           (setq to-newsgroup default))
8804       (unless to-newsgroup
8805         (error "No group name entered"))
8806       (or (gnus-active to-newsgroup)
8807           (gnus-activate-group to-newsgroup)
8808           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8809                                      to-newsgroup))
8810               (or (and (gnus-request-create-group
8811                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8812                        (gnus-activate-group to-newsgroup nil nil
8813                                             (gnus-group-name-to-method
8814                                              to-newsgroup)))
8815                   (error "Couldn't create group %s" to-newsgroup)))
8816           (error "No such group: %s" to-newsgroup)))
8817     to-newsgroup))
8818
8819 ;; Summary extract commands
8820
8821 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8822   (let ((buffer-read-only nil)
8823         (article (gnus-summary-article-number))
8824         after-article b e)
8825     (unless (gnus-summary-goto-subject article)
8826       (error "No such article: %d" article))
8827     (gnus-summary-position-point)
8828     ;; If all commands are to be bunched up on one line, we collect
8829     ;; them here.
8830     (unless gnus-view-pseudos-separately
8831       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8832             files action)
8833         (while ps
8834           (setq action (cdr (assq 'action (car ps))))
8835           (setq files (list (cdr (assq 'name (car ps)))))
8836           (while (and ps (cdr ps)
8837                       (string= (or action "1")
8838                                (or (cdr (assq 'action (cadr ps))) "2")))
8839             (push (cdr (assq 'name (cadr ps))) files)
8840             (setcdr ps (cddr ps)))
8841           (when files
8842             (when (not (string-match "%s" action))
8843               (push " " files))
8844             (push " " files)
8845             (when (assq 'execute (car ps))
8846               (setcdr (assq 'execute (car ps))
8847                       (funcall (if (string-match "%s" action)
8848                                    'format 'concat)
8849                                action
8850                                (mapconcat
8851                                 (lambda (f)
8852                                   (if (equal f " ")
8853                                       f
8854                                     (gnus-quote-arg-for-sh-or-csh f)))
8855                                 files " ")))))
8856           (setq ps (cdr ps)))))
8857     (if (and gnus-view-pseudos (not not-view))
8858         (while pslist
8859           (when (assq 'execute (car pslist))
8860             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8861                                   (eq gnus-view-pseudos 'not-confirm)))
8862           (setq pslist (cdr pslist)))
8863       (save-excursion
8864         (while pslist
8865           (setq after-article (or (cdr (assq 'article (car pslist)))
8866                                   (gnus-summary-article-number)))
8867           (gnus-summary-goto-subject after-article)
8868           (forward-line 1)
8869           (setq b (point))
8870           (insert "    " (file-name-nondirectory
8871                           (cdr (assq 'name (car pslist))))
8872                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8873           (setq e (point))
8874           (forward-line -1)             ; back to `b'
8875           (gnus-add-text-properties
8876            b (1- e) (list 'gnus-number gnus-reffed-article-number
8877                           gnus-mouse-face-prop gnus-mouse-face))
8878           (gnus-data-enter
8879            after-article gnus-reffed-article-number
8880            gnus-unread-mark b (car pslist) 0 (- e b))
8881           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8882           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8883           (setq pslist (cdr pslist)))))))
8884
8885 (defun gnus-pseudos< (p1 p2)
8886   (let ((c1 (cdr (assq 'action p1)))
8887         (c2 (cdr (assq 'action p2))))
8888     (and c1 c2 (string< c1 c2))))
8889
8890 (defun gnus-request-pseudo-article (props)
8891   (cond ((assq 'execute props)
8892          (gnus-execute-command (cdr (assq 'execute props)))))
8893   (let ((gnus-current-article (gnus-summary-article-number)))
8894     (gnus-run-hooks 'gnus-mark-article-hook)))
8895
8896 (defun gnus-execute-command (command &optional automatic)
8897   (save-excursion
8898     (gnus-article-setup-buffer)
8899     (set-buffer gnus-article-buffer)
8900     (setq buffer-read-only nil)
8901     (let ((command (if automatic command
8902                      (read-string "Command: " (cons command 0)))))
8903       (erase-buffer)
8904       (insert "$ " command "\n\n")
8905       (if gnus-view-pseudo-asynchronously
8906           (start-process "gnus-execute" (current-buffer) shell-file-name
8907                          shell-command-switch command)
8908         (call-process shell-file-name nil t nil
8909                       shell-command-switch command)))))
8910
8911 ;; Summary kill commands.
8912
8913 (defun gnus-summary-edit-global-kill (article)
8914   "Edit the \"global\" kill file."
8915   (interactive (list (gnus-summary-article-number)))
8916   (gnus-group-edit-global-kill article))
8917
8918 (defun gnus-summary-edit-local-kill ()
8919   "Edit a local kill file applied to the current newsgroup."
8920   (interactive)
8921   (setq gnus-current-headers (gnus-summary-article-header))
8922   (gnus-group-edit-local-kill
8923    (gnus-summary-article-number) gnus-newsgroup-name))
8924
8925 ;;; Header reading.
8926
8927 (defun gnus-read-header (id &optional header)
8928   "Read the headers of article ID and enter them into the Gnus system."
8929   (let ((group gnus-newsgroup-name)
8930         (gnus-override-method
8931          (and (gnus-news-group-p gnus-newsgroup-name)
8932               gnus-refer-article-method))
8933         where)
8934     ;; First we check to see whether the header in question is already
8935     ;; fetched.
8936     (if (stringp id)
8937         ;; This is a Message-ID.
8938         (setq header (or header (gnus-id-to-header id)))
8939       ;; This is an article number.
8940       (setq header (or header (gnus-summary-article-header id))))
8941     (if (and header
8942              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8943         ;; We have found the header.
8944         header
8945       ;; If this is a sparse article, we have to nix out its
8946       ;; previous entry in the thread hashtb.
8947       (when (and header
8948                  (gnus-summary-article-sparse-p (mail-header-number header)))
8949         (let* ((parent (gnus-parent-id (mail-header-references header)))
8950                (thread (and parent (gnus-id-to-thread parent))))
8951           (when thread
8952             (delq (assq header thread) thread))))
8953       ;; We have to really fetch the header to this article.
8954       (save-excursion
8955         (set-buffer nntp-server-buffer)
8956         (when (setq where (gnus-request-head id group))
8957           (nnheader-fold-continuation-lines)
8958           (goto-char (point-max))
8959           (insert ".\n")
8960           (goto-char (point-min))
8961           (insert "211 ")
8962           (princ (cond
8963                   ((numberp id) id)
8964                   ((cdr where) (cdr where))
8965                   (header (mail-header-number header))
8966                   (t gnus-reffed-article-number))
8967                  (current-buffer))
8968           (insert " Article retrieved.\n"))
8969         (if (or (not where)
8970                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8971             ()                          ; Malformed head.
8972           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8973             (when (and (stringp id)
8974                        (not (string= (gnus-group-real-name group)
8975                                      (car where))))
8976               ;; If we fetched by Message-ID and the article came
8977               ;; from a different group, we fudge some bogus article
8978               ;; numbers for this article.
8979               (mail-header-set-number header gnus-reffed-article-number))
8980             (save-excursion
8981               (set-buffer gnus-summary-buffer)
8982               (decf gnus-reffed-article-number)
8983               (gnus-remove-header (mail-header-number header))
8984               (push header gnus-newsgroup-headers)
8985               (setq gnus-current-headers header)
8986               (push (mail-header-number header) gnus-newsgroup-limit)))
8987           header)))))
8988
8989 (defun gnus-remove-header (number)
8990   "Remove header NUMBER from `gnus-newsgroup-headers'."
8991   (if (and gnus-newsgroup-headers
8992            (= number (mail-header-number (car gnus-newsgroup-headers))))
8993       (pop gnus-newsgroup-headers)
8994     (let ((headers gnus-newsgroup-headers))
8995       (while (and (cdr headers)
8996                   (not (= number (mail-header-number (cadr headers)))))
8997         (pop headers))
8998       (when (cdr headers)
8999         (setcdr headers (cddr headers))))))
9000
9001 ;;;
9002 ;;; summary highlights
9003 ;;;
9004
9005 (defun gnus-highlight-selected-summary ()
9006   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9007   ;; Highlight selected article in summary buffer
9008   (when gnus-summary-selected-face
9009     (save-excursion
9010       (let* ((beg (progn (beginning-of-line) (point)))
9011              (end (progn (end-of-line) (point)))
9012              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9013              (from (if (get-text-property beg gnus-mouse-face-prop)
9014                        beg
9015                      (or (next-single-property-change
9016                           beg gnus-mouse-face-prop nil end)
9017                          beg)))
9018              (to
9019               (if (= from end)
9020                   (- from 2)
9021                 (or (next-single-property-change
9022                      from gnus-mouse-face-prop nil end)
9023                     end))))
9024         ;; If no mouse-face prop on line we will have to = from = end,
9025         ;; so we highlight the entire line instead.
9026         (when (= (+ to 2) from)
9027           (setq from beg)
9028           (setq to end))
9029         (if gnus-newsgroup-selected-overlay
9030             ;; Move old overlay.
9031             (gnus-move-overlay
9032              gnus-newsgroup-selected-overlay from to (current-buffer))
9033           ;; Create new overlay.
9034           (gnus-overlay-put
9035            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9036            'face gnus-summary-selected-face))))))
9037
9038 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9039 (defun gnus-summary-highlight-line ()
9040   "Highlight current line according to `gnus-summary-highlight'."
9041   (let* ((list gnus-summary-highlight)
9042          (p (point))
9043          (end (progn (end-of-line) (point)))
9044          ;; now find out where the line starts and leave point there.
9045          (beg (progn (beginning-of-line) (point)))
9046          (article (gnus-summary-article-number))
9047          (score (or (cdr (assq (or article gnus-current-article)
9048                                gnus-newsgroup-scored))
9049                     gnus-summary-default-score 0))
9050          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9051          (inhibit-read-only t))
9052     ;; Eval the cars of the lists until we find a match.
9053     (let ((default gnus-summary-default-score))
9054       (while (and list
9055                   (not (eval (caar list))))
9056         (setq list (cdr list))))
9057     (let ((face (cdar list)))
9058       (unless (eq face (get-text-property beg 'face))
9059         (gnus-put-text-property-excluding-characters-with-faces
9060          beg end 'face
9061          (setq face (if (boundp face) (symbol-value face) face)))
9062         (when gnus-summary-highlight-line-function
9063           (funcall gnus-summary-highlight-line-function article face))))
9064     (goto-char p)))
9065
9066 (defun gnus-update-read-articles (group unread &optional compute)
9067   "Update the list of read articles in GROUP."
9068   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9069          (entry (gnus-gethash group gnus-newsrc-hashtb))
9070          (info (nth 2 entry))
9071          (prev 1)
9072          (unread (sort (copy-sequence unread) '<))
9073          read)
9074     (if (or (not info) (not active))
9075         ;; There is no info on this group if it was, in fact,
9076         ;; killed.  Gnus stores no information on killed groups, so
9077         ;; there's nothing to be done.
9078         ;; One could store the information somewhere temporarily,
9079         ;; perhaps...  Hmmm...
9080         ()
9081       ;; Remove any negative articles numbers.
9082       (while (and unread (< (car unread) 0))
9083         (setq unread (cdr unread)))
9084       ;; Remove any expired article numbers
9085       (while (and unread (< (car unread) (car active)))
9086         (setq unread (cdr unread)))
9087       ;; Compute the ranges of read articles by looking at the list of
9088       ;; unread articles.
9089       (while unread
9090         (when (/= (car unread) prev)
9091           (push (if (= prev (1- (car unread))) prev
9092                   (cons prev (1- (car unread))))
9093                 read))
9094         (setq prev (1+ (car unread)))
9095         (setq unread (cdr unread)))
9096       (when (<= prev (cdr active))
9097         (push (cons prev (cdr active)) read))
9098       (setq read (if (> (length read) 1) (nreverse read) read))
9099       (if compute
9100           read
9101         (save-excursion
9102           (set-buffer gnus-group-buffer)
9103           (gnus-undo-register
9104             `(progn
9105                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9106                (gnus-info-set-read ',info ',(gnus-info-read info))
9107                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9108                (gnus-group-update-group ,group t))))
9109        ;; Propagate the read marks to the backend.
9110        (if (gnus-check-backend-function 'request-set-mark group)
9111            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9112                  (add (gnus-remove-from-range read (gnus-info-read info))))
9113              (when (or add del)
9114                (gnus-request-set-mark
9115                 group (delq nil (list (if add (list add 'add '(read)))
9116                                       (if del (list del 'del '(read)))))))))
9117         ;; Enter this list into the group info.
9118         (gnus-info-set-read info read)
9119         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9120         (gnus-get-unread-articles-in-group info (gnus-active group))
9121         t))))
9122
9123 (defun gnus-offer-save-summaries ()
9124   "Offer to save all active summary buffers."
9125   (save-excursion
9126     (let ((buflist (buffer-list))
9127           buffers bufname)
9128       ;; Go through all buffers and find all summaries.
9129       (while buflist
9130         (and (setq bufname (buffer-name (car buflist)))
9131              (string-match "Summary" bufname)
9132              (save-excursion
9133                (set-buffer bufname)
9134                ;; We check that this is, indeed, a summary buffer.
9135                (and (eq major-mode 'gnus-summary-mode)
9136                     ;; Also make sure this isn't bogus.
9137                     gnus-newsgroup-prepared
9138                     ;; Also make sure that this isn't a dead summary buffer.
9139                     (not gnus-dead-summary-mode)))
9140              (push bufname buffers))
9141         (setq buflist (cdr buflist)))
9142       ;; Go through all these summary buffers and offer to save them.
9143       (when buffers
9144         (map-y-or-n-p
9145          "Update summary buffer %s? "
9146          (lambda (buf)
9147            (switch-to-buffer buf)
9148            (gnus-summary-exit))
9149          buffers)))))
9150
9151
9152 ;;; @ for mime-partial
9153 ;;;
9154
9155 (defun gnus-request-partial-message ()
9156   (save-excursion
9157     (let ((number (gnus-summary-article-number))
9158           (group gnus-newsgroup-name)
9159           (mother gnus-article-buffer))
9160       (set-buffer (get-buffer-create " *Partial Article*"))
9161       (erase-buffer)
9162       (setq mime-preview-buffer mother)
9163       (gnus-request-article-this-buffer number group)
9164       (mime-parse-buffer)
9165       )))
9166
9167 (autoload 'mime-combine-message/partial-pieces-automatically
9168   "mime-partial"
9169   "Internal method to combine message/partial messages automatically.")
9170
9171 (mime-add-condition
9172  'action '((type . message)(subtype . partial)
9173            (major-mode . gnus-original-article-mode)
9174            (method . mime-combine-message/partial-pieces-automatically)
9175            (summary-buffer-exp . gnus-summary-buffer)
9176            (request-partial-message-method . gnus-request-partial-message)
9177            ))
9178
9179
9180 ;;; @ for message/rfc822
9181 ;;;
9182
9183 (defun gnus-mime-extract-message/rfc822 (entity situation)
9184   (let (group article num cwin swin cur)
9185     (with-current-buffer (mime-entity-buffer entity)
9186       (save-restriction
9187         (narrow-to-region (mime-entity-body-start entity)
9188                           (mime-entity-body-end entity))
9189         (setq group (or (cdr (assq 'group situation))
9190                         (completing-read "Group: "
9191                                          gnus-active-hashtb
9192                                          nil
9193                                          (gnus-read-active-file-p)
9194                                          gnus-newsgroup-name))
9195               article (gnus-request-accept-article group)
9196               )
9197         ))
9198     (when (and (consp article)
9199                (numberp (setq article (cdr article))))
9200       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9201             cwin (get-buffer-window (current-buffer) t)
9202             )
9203       (save-window-excursion
9204         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9205             (select-window swin)
9206           (set-buffer gnus-summary-buffer)
9207           )
9208         (setq cur gnus-current-article)
9209         (forward-line num)
9210         (let (gnus-show-threads)
9211           (gnus-summary-goto-subject article t)
9212           )
9213         (gnus-summary-clear-mark-forward 1)
9214         (gnus-summary-goto-subject cur)
9215         )
9216       (when (and cwin (window-frame cwin))
9217         (select-frame (window-frame cwin))
9218         )
9219       (when (boundp 'mime-acting-situation-to-override)
9220         (set-alist 'mime-acting-situation-to-override
9221                    'group
9222                    group)
9223         (set-alist 'mime-acting-situation-to-override
9224                    'after-method
9225                    `(progn
9226                       (save-current-buffer
9227                         (set-buffer gnus-group-buffer)
9228                         (gnus-activate-group ,group)
9229                         )
9230                       (gnus-summary-goto-article ,cur
9231                                                  gnus-show-all-headers)
9232                       ))
9233         (set-alist 'mime-acting-situation-to-override
9234                    'number num)
9235         )
9236       )))
9237
9238 (mime-add-condition
9239  'action '((type . message)(subtype . rfc822)
9240            (major-mode . gnus-original-article-mode)
9241            (method . gnus-mime-extract-message/rfc822)
9242            (mode . "extract")
9243            ))
9244
9245 (mime-add-condition
9246  'action '((type . message)(subtype . news)
9247            (major-mode . gnus-original-article-mode)
9248            (method . gnus-mime-extract-message/rfc822)
9249            (mode . "extract")
9250            ))
9251
9252 (defun gnus-mime-extract-multipart (entity situation)
9253   (let ((children (mime-entity-children entity))
9254         mime-acting-situation-to-override
9255         f)
9256     (while children
9257       (mime-play-entity (car children)
9258                         (cons (assq 'mode situation)
9259                               mime-acting-situation-to-override))
9260       (setq children (cdr children)))
9261     (if (setq f (cdr (assq 'after-method
9262                            mime-acting-situation-to-override)))
9263         (eval f)
9264       )))  
9265
9266 (mime-add-condition
9267  'action '((type . multipart)
9268            (method . gnus-mime-extract-multipart)
9269            (mode . "extract")
9270            )
9271  'with-default)
9272
9273
9274 ;;; @ end
9275 ;;;
9276
9277 (defun gnus-summary-setup-default-charset ()
9278   "Setup newsgroup default charset."
9279   (let ((name (and gnus-newsgroup-name
9280                    (gnus-group-real-name gnus-newsgroup-name))))
9281     (setq gnus-newsgroup-charset
9282           (or (and gnus-newsgroup-name
9283                    (or (gnus-group-find-parameter gnus-newsgroup-name
9284                                                   'charset)
9285                        (let ((alist gnus-group-charset-alist)
9286                              elem (charset nil))
9287                          (while (setq elem (pop alist))
9288                            (when (and name
9289                                       (string-match (car elem) name))
9290                              (setq alist nil
9291                                    charset (cadr elem))))
9292                          charset)))
9293               gnus-default-charset))))
9294
9295 ;;;
9296 ;;; Mime Commands
9297 ;;;
9298
9299 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9300   "Display the current article buffer fully MIME-buttonized.
9301 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9302 treated as multipart/mixed."
9303   (interactive "P")
9304   (require 'gnus-art)
9305   (let ((gnus-unbuttonized-mime-types nil)
9306         (gnus-mime-display-multipart-as-mixed show-all-parts))
9307     (gnus-summary-show-article)))
9308
9309 (defun gnus-summary-repair-multipart (article)
9310   "Add a Content-Type header to a multipart article without one."
9311   (interactive (list (gnus-summary-article-number)))
9312   (gnus-with-article article
9313     (message-narrow-to-head)
9314     (goto-char (point-max))
9315     (widen)
9316     (when (search-forward "\n--" nil t)
9317       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9318         (message-narrow-to-head)
9319         (message-remove-header "Mime-Version")
9320         (message-remove-header "Content-Type")
9321         (goto-char (point-max))
9322         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9323                         separator))
9324         (insert "Mime-Version: 1.0\n")
9325         (widen))))
9326   (let (gnus-mark-article-hook)
9327     (gnus-summary-select-article t t nil article)))
9328
9329 (defun gnus-summary-toggle-display-buttonized ()
9330   "Toggle the buttonizing of the article buffer."
9331   (interactive)
9332   (require 'gnus-art)
9333   (if (setq gnus-inhibit-mime-unbuttonizing
9334             (not gnus-inhibit-mime-unbuttonizing))
9335       (let ((gnus-unbuttonized-mime-types nil))
9336         (gnus-summary-show-article))
9337     (gnus-summary-show-article)))
9338
9339 (gnus-ems-redefine)
9340
9341 (provide 'gnus-sum)
9342
9343 (run-hooks 'gnus-sum-load-hook)
9344
9345 ;;; gnus-sum.el ends here