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