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