Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'gnus-cache-articles-in-group "gnus-cache")
52   (autoload 'pgg-decrypt-region "pgg" nil t)
53   (autoload 'pgg-verify-region "pgg" nil t))
54
55 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
56 (autoload 'gnus-cache-write-active "gnus-cache")
57 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
58 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
59 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
60 (autoload 'mm-uu-dissect "mm-uu")
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-fetch-old-headers nil
70   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
71 If an unread article in the group refers to an older, already read (or
72 just marked as read) article, the old article will not normally be
73 displayed in the Summary buffer.  If this variable is non-nil, Gnus
74 will attempt to grab the headers to the old articles, and thereby
75 build complete threads.  If it has the value `some', only enough
76 headers to connect otherwise loose threads will be displayed.  This
77 variable can also be a number.  In that case, no more than that number
78 of old headers will be fetched.  If it has the value `invisible', all
79 old headers will be fetched, but none will be displayed.
80
81 The server has to support NOV for any of this to work."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const some)
85                  number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-refer-thread-limit 200
89   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
90 If t, fetch all the available old headers."
91   :group 'gnus-thread
92   :type '(choice number
93                  (sexp :menu-tag "other" t)))
94
95 (defcustom gnus-summary-make-false-root 'adopt
96   "*nil means that Gnus won't gather loose threads.
97 If the root of a thread has expired or been read in a previous
98 session, the information necessary to build a complete thread has been
99 lost.  Instead of having many small sub-threads from this original thread
100 scattered all over the summary buffer, Gnus can gather them.
101
102 If non-nil, Gnus will try to gather all loose sub-threads from an
103 original thread into one large thread.
104
105 If this variable is non-nil, it should be one of `none', `adopt',
106 `dummy' or `empty'.
107
108 If this variable is `none', Gnus will not make a false root, but just
109 present the sub-threads after another.
110 If this variable is `dummy', Gnus will create a dummy root that will
111 have all the sub-threads as children.
112 If this variable is `adopt', Gnus will make one of the \"children\"
113 the parent and mark all the step-children as such.
114 If this variable is `empty', the \"children\" are printed with empty
115 subject fields.  (Or rather, they will be printed with a string
116 given by the `gnus-summary-same-subject' variable.)"
117   :group 'gnus-thread
118   :type '(choice (const :tag "off" nil)
119                  (const none)
120                  (const dummy)
121                  (const adopt)
122                  (const empty)))
123
124 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
125   "*A regexp to match subjects to be excluded from loose thread gathering.
126 As loose thread gathering is done on subjects only, that means that
127 there can be many false gatherings performed.  By rooting out certain
128 common subjects, gathering might become saner."
129   :group 'gnus-thread
130   :type 'regexp)
131
132 (defcustom gnus-summary-gather-subject-limit nil
133   "*Maximum length of subject comparisons when gathering loose threads.
134 Use nil to compare full subjects.  Setting this variable to a low
135 number will help gather threads that have been corrupted by
136 newsreaders chopping off subject lines, but it might also mean that
137 unrelated articles that have subject that happen to begin with the
138 same few characters will be incorrectly gathered.
139
140 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
141 comparing subjects."
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const fuzzy)
145                  (sexp :menu-tag "on" t)))
146
147 (defcustom gnus-simplify-subject-functions nil
148   "List of functions taking a string argument that simplify subjects.
149 The functions are applied recursively.
150
151 Useful functions to put in this list include: `gnus-simplify-subject-re',
152 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If nil, don't select the first unread article when entering a group.
296 If this variable is `best', select the highest-scored unread article
297 in the group.  If t, select the first unread article.
298
299 This variable can also be a function to place point on a likely
300 subject line.  Useful values include `gnus-summary-first-unread-subject',
301 `gnus-summary-first-unread-article' and
302 `gnus-summary-best-unread-article'.
303
304 If you want to prevent automatic selection of the first unread article
305 in some newsgroups, set the variable to nil in
306 `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (const best)
310                  (sexp :menu-tag "first" t)
311                  (function-item gnus-summary-first-unread-subject)
312                  (function-item gnus-summary-first-unread-article)
313                  (function-item gnus-summary-best-unread-article)))
314
315 (defcustom gnus-dont-select-after-jump-to-other-group nil
316   "If non-nil, don't select the first unread article after entering the
317 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
318 it is depend on the value of `gnus-auto-select-first' whether to select
319 or not."
320   :group 'gnus-group-select
321   :type 'boolean)
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `Z n' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-summary-check-current nil
349   "*If non-nil, consider the current article when moving.
350 The \"unread\" movement commands will stay on the same line if the
351 current article is unread."
352   :group 'gnus-summary-maneuvering
353   :type 'boolean)
354
355 (defcustom gnus-auto-center-summary t
356   "*If non-nil, always center the current summary buffer.
357 In particular, if `vertical' do only vertical recentering.  If non-nil
358 and non-`vertical', do both horizontal and vertical recentering."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "none" nil)
361                  (const vertical)
362                  (integer :tag "height")
363                  (sexp :menu-tag "both" t)))
364
365 (defcustom gnus-show-all-headers nil
366   "*If non-nil, don't hide any headers."
367   :group 'gnus-article-hiding
368   :group 'gnus-article-headers
369   :type 'boolean)
370
371 (defcustom gnus-summary-ignore-duplicates nil
372   "*If non-nil, ignore articles with identical Message-ID headers."
373   :group 'gnus-summary
374   :type 'boolean)
375
376 (defcustom gnus-single-article-buffer t
377   "*If non-nil, display all articles in the same buffer.
378 If nil, each group will get its own article buffer."
379   :group 'gnus-article-various
380   :type 'boolean)
381
382 (defcustom gnus-break-pages t
383   "*If non-nil, do page breaking on articles.
384 The page delimiter is specified by the `gnus-page-delimiter'
385 variable."
386   :group 'gnus-article-various
387   :type 'boolean)
388
389 (defcustom gnus-show-mime t
390   "*If non-nil, do mime processing of articles.
391 The articles will simply be fed to the function given by
392 `gnus-article-display-method-for-mime'."
393   :group 'gnus-article-mime
394   :type 'boolean)
395
396 (defcustom gnus-move-split-methods nil
397   "*Variable used to suggest where articles are to be moved to.
398 It uses the same syntax as the `gnus-split-methods' variable.
399 However, whereas `gnus-split-methods' specifies file names as targets,
400 this variable specifies group names."
401   :group 'gnus-summary-mail
402   :type '(repeat (choice (list :value (fun) function)
403                          (cons :value ("" "") regexp (repeat string))
404                          (sexp :value nil))))
405
406 (defcustom gnus-unread-mark ?  ;Whitespace
407   "*Mark used for unread articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-ticked-mark ?!
412   "*Mark used for ticked articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-dormant-mark ??
417   "*Mark used for dormant articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-del-mark ?r
422   "*Mark used for del'd articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-read-mark ?R
427   "*Mark used for read articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-expirable-mark ?E
432   "*Mark used for expirable articles."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-killed-mark ?K
437   "*Mark used for killed articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-souped-mark ?F
442   "*Mark used for souped articles."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-kill-file-mark ?X
447   "*Mark used for articles killed by kill files."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-low-score-mark ?Y
452   "*Mark used for articles with a low score."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-catchup-mark ?C
457   "*Mark used for articles that are caught up."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-replied-mark ?A
462   "*Mark used for articles that have been replied to."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-forwarded-mark ?O
467   "*Mark used for articles that have been forwarded."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-cached-mark ?*
472   "*Mark used for articles that are in the cache."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-saved-mark ?S
477   "*Mark used for articles that have been saved to."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-no-mark ?  ;Whitespace
482   "*Mark used for articles that have no other secondary mark."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-ancient-mark ?O
487   "*Mark used for ancient articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-sparse-mark ?Q
492   "*Mark used for sparsely reffed articles."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-canceled-mark ?G
497   "*Mark used for canceled articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-duplicate-mark ?M
502   "*Mark used for duplicate articles."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-undownloaded-mark ?@
507   "*Mark used for articles that weren't downloaded."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-downloadable-mark ?%
512   "*Mark used for articles that are to be downloaded."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-unsendable-mark ?=
517   "*Mark used for articles that won't be sent."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-score-over-mark ?+
522   "*Score mark used for articles with high scores."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-score-below-mark ?-
527   "*Score mark used for articles with low scores."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-empty-thread-mark ?  ;Whitespace
532   "*There is no thread under the article."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-not-empty-thread-mark ?=
537   "*There is a thread under the article."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-view-pseudo-asynchronously nil
542   "*If non-nil, Gnus will view pseudo-articles asynchronously."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-auto-expirable-marks
547   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
548         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
549         gnus-souped-mark gnus-duplicate-mark)
550   "*The list of marks converted into expiration if a group is auto-expirable."
551   :version "21.1"
552   :group 'gnus-summary
553   :type '(repeat character))
554
555 (defcustom gnus-inhibit-user-auto-expire t
556   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
557   :version "21.1"
558   :group 'gnus-summary
559   :type 'boolean)
560
561 (defcustom gnus-view-pseudos nil
562   "*If `automatic', pseudo-articles will be viewed automatically.
563 If `not-confirm', pseudos will be viewed automatically, and the user
564 will not be asked to confirm the command."
565   :group 'gnus-extract-view
566   :type '(choice (const :tag "off" nil)
567                  (const automatic)
568                  (const not-confirm)))
569
570 (defcustom gnus-view-pseudos-separately t
571   "*If non-nil, one pseudo-article will be created for each file to be viewed.
572 If nil, all files that use the same viewing command will be given as a
573 list of parameters to that command."
574   :group 'gnus-extract-view
575   :type 'boolean)
576
577 (defcustom gnus-insert-pseudo-articles t
578   "*If non-nil, insert pseudo-articles when decoding articles."
579   :group 'gnus-extract-view
580   :type 'boolean)
581
582 (defcustom gnus-summary-dummy-line-format
583   "  %(:                          :%) %S\n"
584   "*The format specification for the dummy roots in the summary buffer.
585 It works along the same lines as a normal formatting string,
586 with some simple extensions.
587
588 %S  The subject"
589   :group 'gnus-threading
590   :type 'string)
591
592 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
593   "*The format specification for the summary mode line.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions:
596
597 %G  Group name
598 %p  Unprefixed group name
599 %A  Current article number
600 %z  Current article score
601 %V  Gnus version
602 %U  Number of unread articles in the group
603 %e  Number of unselected articles in the group
604 %Z  A string with unread/unselected article counts
605 %g  Shortish group name
606 %S  Subject of the current article
607 %u  User-defined spec
608 %s  Current score file name
609 %d  Number of dormant articles
610 %r  Number of articles that have been marked as read in this session
611 %E  Number of articles expunged by the score files"
612   :group 'gnus-summary-format
613   :type 'string)
614
615 (defcustom gnus-list-identifiers nil
616   "Regexp that matches list identifiers to be removed from subject.
617 This can also be a list of regexps."
618   :version "21.1"
619   :group 'gnus-summary-format
620   :group 'gnus-article-hiding
621   :type '(choice (const :tag "none" nil)
622                  (regexp :value ".*")
623                  (repeat :value (".*") regexp)))
624
625 (defcustom gnus-summary-mark-below 0
626   "*Mark all articles with a score below this variable as read.
627 This variable is local to each summary buffer and usually set by the
628 score file."
629   :group 'gnus-score-default
630   :type 'integer)
631
632 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
633   "*List of functions used for sorting articles in the summary buffer.
634
635 Each function takes two articles and returns non-nil if the first
636 article should be sorted before the other.  If you use more than one
637 function, the primary sort function should be the last.  You should
638 probably always include `gnus-article-sort-by-number' in the list of
639 sorting functions -- preferably first.  Also note that sorting by date
640 is often much slower than sorting by number, and the sorting order is
641 very similar.  (Sorting by date means sorting by the time the message
642 was sent, sorting by number means sorting by arrival time.)
643
644 Ready-made functions include `gnus-article-sort-by-number',
645 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
646 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
647
648 When threading is turned on, the variable `gnus-thread-sort-functions'
649 controls how articles are sorted."
650   :group 'gnus-summary-sort
651   :type '(repeat (choice (function-item gnus-article-sort-by-number)
652                          (function-item gnus-article-sort-by-author)
653                          (function-item gnus-article-sort-by-subject)
654                          (function-item gnus-article-sort-by-date)
655                          (function-item gnus-article-sort-by-score)
656                          (function :tag "other"))))
657
658 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
659   "*List of functions used for sorting threads in the summary buffer.
660 By default, threads are sorted by article number.
661
662 Each function takes two threads and returns non-nil if the first
663 thread should be sorted before the other.  If you use more than one
664 function, the primary sort function should be the last.  You should
665 probably always include `gnus-thread-sort-by-number' in the list of
666 sorting functions -- preferably first.  Also note that sorting by date
667 is often much slower than sorting by number, and the sorting order is
668 very similar.  (Sorting by date means sorting by the time the message
669 was sent, sorting by number means sorting by arrival time.)
670
671 Ready-made functions include `gnus-thread-sort-by-number',
672 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
673 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
674 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
675
676 When threading is turned off, the variable
677 `gnus-article-sort-functions' controls how articles are sorted."
678   :group 'gnus-summary-sort
679   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
680                          (function-item gnus-thread-sort-by-author)
681                          (function-item gnus-thread-sort-by-subject)
682                          (function-item gnus-thread-sort-by-date)
683                          (function-item gnus-thread-sort-by-score)
684                          (function-item gnus-thread-sort-by-total-score)
685                          (function :tag "other"))))
686
687 (defcustom gnus-thread-score-function '+
688   "*Function used for calculating the total score of a thread.
689
690 The function is called with the scores of the article and each
691 subthread and should then return the score of the thread.
692
693 Some functions you can use are `+', `max', or `min'."
694   :group 'gnus-summary-sort
695   :type 'function)
696
697 (defcustom gnus-summary-expunge-below nil
698   "All articles that have a score less than this variable will be expunged.
699 This variable is local to the summary buffers."
700   :group 'gnus-score-default
701   :type '(choice (const :tag "off" nil)
702                  integer))
703
704 (defcustom gnus-thread-expunge-below nil
705   "All threads that have a total score less than this variable will be expunged.
706 See `gnus-thread-score-function' for en explanation of what a
707 \"thread score\" is.
708
709 This variable is local to the summary buffers."
710   :group 'gnus-threading
711   :group 'gnus-score-default
712   :type '(choice (const :tag "off" nil)
713                  integer))
714
715 (defcustom gnus-summary-mode-hook nil
716   "*A hook for Gnus summary mode.
717 This hook is run before any variables are set in the summary buffer."
718   :options '(turn-on-gnus-mailing-list-mode)
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
723 (when (featurep 'xemacs)
724   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
725   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
726   (add-hook 'gnus-summary-mode-hook
727             'gnus-xmas-switch-horizontal-scrollbar-off))
728
729 (defcustom gnus-summary-menu-hook nil
730   "*Hook run after the creation of the summary mode menu."
731   :group 'gnus-summary-visual
732   :type 'hook)
733
734 (defcustom gnus-summary-exit-hook nil
735   "*A hook called on exit from the summary buffer.
736 It will be called with point in the group buffer."
737   :group 'gnus-summary-exit
738   :type 'hook)
739
740 (defcustom gnus-summary-prepare-hook nil
741   "*A hook called after the summary buffer has been generated.
742 If you want to modify the summary buffer, you can use this hook."
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 (defcustom gnus-summary-prepared-hook nil
747   "*A hook called as the last thing after the summary buffer has been generated."
748   :group 'gnus-summary-various
749   :type 'hook)
750
751 (defcustom gnus-summary-generate-hook nil
752   "*A hook run just before generating the summary buffer.
753 This hook is commonly used to customize threading variables and the
754 like."
755   :group 'gnus-summary-various
756   :type 'hook)
757
758 (defcustom gnus-select-group-hook nil
759   "*A hook called when a newsgroup is selected.
760
761 If you'd like to simplify subjects like the
762 `gnus-summary-next-same-subject' command does, you can use the
763 following hook:
764
765  (setq gnus-select-group-hook
766       (list
767         (lambda ()
768           (mapcar (lambda (header)
769                      (mail-header-set-subject
770                       header
771                       (gnus-simplify-subject
772                        (mail-header-subject header) 're-only)))
773                   gnus-newsgroup-headers))))"
774   :group 'gnus-group-select
775   :type 'hook)
776
777 (defcustom gnus-select-article-hook nil
778   "*A hook called when an article is selected."
779   :group 'gnus-summary-choose
780   :type 'hook)
781
782 (defcustom gnus-visual-mark-article-hook
783   (list 'gnus-highlight-selected-summary)
784   "*Hook run after selecting an article in the summary buffer.
785 It is meant to be used for highlighting the article in some way.  It
786 is not run if `gnus-visual' is nil."
787   :group 'gnus-summary-visual
788   :type 'hook)
789
790 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
791   "*A hook called before parsing the headers."
792   :group 'gnus-various
793   :type 'hook)
794
795 (defcustom gnus-exit-group-hook nil
796   "*A hook called when exiting summary mode.
797 This hook is not called from the non-updating exit commands like `Q'."
798   :group 'gnus-various
799   :type 'hook)
800
801 (defcustom gnus-summary-update-hook
802   (list 'gnus-summary-highlight-line)
803   "*A hook called when a summary line is changed.
804 The hook will not be called if `gnus-visual' is nil.
805
806 The default function `gnus-summary-highlight-line' will
807 highlight the line according to the `gnus-summary-highlight'
808 variable."
809   :group 'gnus-summary-visual
810   :type 'hook)
811
812 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
813   "*A hook called when an article is selected for the first time.
814 The hook is intended to mark an article as read (or unread)
815 automatically when it is selected."
816   :group 'gnus-summary-choose
817   :type 'hook)
818
819 (defcustom gnus-group-no-more-groups-hook nil
820   "*A hook run when returning to group mode having no more (unread) groups."
821   :group 'gnus-group-select
822   :type 'hook)
823
824 (defcustom gnus-ps-print-hook nil
825   "*A hook run before ps-printing something from Gnus."
826   :group 'gnus-summary
827   :type 'hook)
828
829 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
830   "Face used for highlighting the current article in the summary buffer."
831   :group 'gnus-summary-visual
832   :type 'face)
833
834 (defcustom gnus-summary-highlight
835   '(((= mark gnus-canceled-mark)
836      . gnus-summary-cancelled-face)
837     ((and (> score default-high)
838           (or (= mark gnus-dormant-mark)
839               (= mark gnus-ticked-mark)))
840      . gnus-summary-high-ticked-face)
841     ((and (< score default-low)
842           (or (= mark gnus-dormant-mark)
843               (= mark gnus-ticked-mark)))
844      . gnus-summary-low-ticked-face)
845     ((or (= mark gnus-dormant-mark)
846          (= mark gnus-ticked-mark))
847      . gnus-summary-normal-ticked-face)
848     ((and (> score default-high) (= mark gnus-ancient-mark))
849      . gnus-summary-high-ancient-face)
850     ((and (< score default-low) (= mark gnus-ancient-mark))
851      . gnus-summary-low-ancient-face)
852     ((= mark gnus-ancient-mark)
853      . gnus-summary-normal-ancient-face)
854     ((and (> score default-high) (= mark gnus-unread-mark))
855      . gnus-summary-high-unread-face)
856     ((and (< score default-low) (= mark gnus-unread-mark))
857      . gnus-summary-low-unread-face)
858     ((= mark gnus-unread-mark)
859      . gnus-summary-normal-unread-face)
860     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
861                                              gnus-undownloaded-mark)))
862      . gnus-summary-high-unread-face)
863     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
864                                              gnus-undownloaded-mark)))
865      . gnus-summary-low-unread-face)
866     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
867           (memq article gnus-newsgroup-unreads))
868      . gnus-summary-normal-unread-face)
869     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
870      . gnus-summary-normal-read-face)
871     ((> score default-high)
872      . gnus-summary-high-read-face)
873     ((< score default-low)
874      . gnus-summary-low-read-face)
875     (t
876      . gnus-summary-normal-read-face))
877   "*Controls the highlighting of summary buffer lines.
878
879 A list of (FORM . FACE) pairs.  When deciding how a a particular
880 summary line should be displayed, each form is evaluated.  The content
881 of the face field after the first true form is used.  You can change
882 how those summary lines are displayed, by editing the face field.
883
884 You can use the following variables in the FORM field.
885
886 score:        The article's score
887 default:      The default article score.
888 default-high: The default score for high scored articles.
889 default-low:  The default score for low scored articles.
890 below:        The score below which articles are automatically marked as read.
891 mark:         The articles mark."
892   :group 'gnus-summary-visual
893   :type '(repeat (cons (sexp :tag "Form" nil)
894                        face)))
895
896 (defcustom gnus-alter-header-function nil
897   "Function called to allow alteration of article header structures.
898 The function is called with one parameter, the article header vector,
899 which it may alter in any way.")
900
901 (defvar gnus-decode-encoded-word-function
902   (mime-find-field-decoder 'From 'nov)
903   "Variable that says which function should be used to decode a string with encoded words.")
904
905 (defcustom gnus-extra-headers nil
906   "*Extra headers to parse."
907   :version "21.1"
908   :group 'gnus-summary
909   :type '(repeat symbol))
910
911 (defcustom gnus-ignored-from-addresses
912   (and user-mail-address (regexp-quote user-mail-address))
913   "*Regexp of From headers that may be suppressed in favor of To headers."
914   :version "21.1"
915   :group 'gnus-summary
916   :type 'regexp)
917
918 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
919   "List of charsets that should be ignored.
920 When these charsets are used in the \"charset\" parameter, the
921 default charset will be used instead."
922   :version "21.1"
923   :type '(repeat symbol)
924   :group 'gnus-charset)
925
926 (gnus-define-group-parameter
927  ignored-charsets
928  :type list
929  :function-document
930  "Return the ignored charsets of GROUP."
931  :variable gnus-group-ignored-charsets-alist
932  :variable-default 
933  '(("alt\\.chinese\\.text" iso-8859-1))
934  :variable-document
935  "Alist of regexps (to match group names) and charsets that should be ignored.
936 When these charsets are used in the \"charset\" parameter, the
937 default charset will be used instead."
938  :variable-group gnus-charset
939  :variable-type '(repeat (cons (regexp :tag "Group")
940                                (repeat symbol)))
941  :parameter-type '(choice :tag "Ignored charsets" 
942                           :value nil
943                           (repeat (symbol)))
944  :parameter-document       "\
945 List of charsets that should be ignored.
946
947 When these charsets are used in the \"charset\" parameter, the
948 default charset will be used instead.")
949
950 (defcustom gnus-group-highlight-words-alist nil
951   "Alist of group regexps and highlight regexps.
952 This variable uses the same syntax as `gnus-emphasis-alist'."
953   :version "21.1"
954   :type '(repeat (cons (regexp :tag "Group")
955                        (repeat (list (regexp :tag "Highlight regexp")
956                                      (number :tag "Group for entire word" 0)
957                                      (number :tag "Group for displayed part" 0)
958                                      (symbol :tag "Face"
959                                              gnus-emphasis-highlight-words)))))
960   :group 'gnus-summary-visual)
961
962 (defcustom gnus-use-wheel nil
963   "Use Intelli-mouse on summary movement"
964   :type 'boolean
965   :group 'gnus-summary-maneuvering)
966
967 (defcustom gnus-wheel-scroll-amount '(5 . 1)
968   "Amount to scroll messages by spinning the mouse wheel.
969 This is actually a cons cell, where the first item is the amount to scroll
970 on a normal wheel event, and the second is the amount to scroll when the
971 wheel is moved with the shift key depressed."
972   :type '(cons (integer :tag "Shift") integer)
973   :group 'gnus-summary-maneuvering)
974
975 (defcustom gnus-wheel-edge-resistance 2
976   "How hard it should be to change the current article
977 by moving the mouse over the edge of the article window."
978   :type 'integer
979   :group 'gnus-summary-maneuvering)
980
981 (defcustom gnus-summary-show-article-charset-alist
982   nil
983   "Alist of number and charset.
984 The article will be shown with the charset corresponding to the
985 numbered argument.
986 For example: ((1 . cn-gb-2312) (2 . big5))."
987   :version "21.1"
988   :type '(repeat (cons (number :tag "Argument" 1)
989                        (symbol :tag "Charset")))
990   :group 'gnus-charset)
991
992 (defcustom gnus-preserve-marks t
993   "Whether marks are preserved when moving, copying and respooling messages."
994   :version "21.1"
995   :type 'boolean
996   :group 'gnus-summary-marks)
997
998 (defcustom gnus-alter-articles-to-read-function nil
999   "Function to be called to alter the list of articles to be selected."
1000   :type '(choice (const nil) function)
1001   :group 'gnus-summary)
1002
1003 (defcustom gnus-orphan-score nil
1004   "*All orphans get this score added.  Set in the score file."
1005   :group 'gnus-score-default
1006   :type '(choice (const nil)
1007                  integer))
1008
1009 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1010   "*A regexp to match MIME parts when saving multiple parts of a message
1011 with gnus-summary-save-parts (X m). This regexp will be used by default
1012 when prompting the user for which type of files to save."
1013   :group 'gnus-summary
1014   :type 'regexp)
1015
1016
1017 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1018   "*A regexp to match MIME parts when saving multiple parts of a message
1019 with gnus-summary-save-parts (X m). This regexp will be used by default
1020 when prompting the user for which type of files to save."
1021   :group 'gnus-summary
1022   :type 'regexp)
1023
1024 (defcustom gnus-read-all-available-headers nil
1025   "Whether Gnus should parse all headers made available to it.
1026 This is mostly relevant for slow backends where the user may
1027 wish to widen the summary buffer to include all headers
1028 that were fetched.  Say, for nnultimate groups."
1029   :group 'gnus-summary
1030   :type '(choice boolean regexp))
1031
1032 ;;; Internal variables
1033
1034 (defvar gnus-article-mime-handles nil)
1035 (defvar gnus-article-decoded-p nil)
1036 (defvar gnus-article-charset nil)
1037 (defvar gnus-article-ignored-charsets nil)
1038 (defvar gnus-scores-exclude-files nil)
1039 (defvar gnus-page-broken nil)
1040 (defvar gnus-inhibit-mime-unbuttonizing nil)
1041
1042 (defvar gnus-original-article nil)
1043 (defvar gnus-article-internal-prepare-hook nil)
1044 (defvar gnus-newsgroup-process-stack nil)
1045
1046 (defvar gnus-thread-indent-array nil)
1047 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1048 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1049   "Function called to sort the articles within a thread after it has been gathered together.")
1050
1051 (defvar gnus-summary-save-parts-type-history nil)
1052 (defvar gnus-summary-save-parts-last-directory nil)
1053
1054 (defvar gnus-summary-save-parts-type-history nil)
1055 (defvar gnus-summary-save-parts-last-directory nil)
1056
1057 ;; Avoid highlighting in kill files.
1058 (defvar gnus-summary-inhibit-highlight nil)
1059 (defvar gnus-newsgroup-selected-overlay nil)
1060 (defvar gnus-inhibit-limiting nil)
1061 (defvar gnus-newsgroup-adaptive-score-file nil)
1062 (defvar gnus-current-score-file nil)
1063 (defvar gnus-current-move-group nil)
1064 (defvar gnus-current-copy-group nil)
1065 (defvar gnus-current-crosspost-group nil)
1066
1067 (defvar gnus-newsgroup-dependencies nil)
1068 (defvar gnus-newsgroup-adaptive nil)
1069 (defvar gnus-summary-display-article-function nil)
1070 (defvar gnus-summary-highlight-line-function nil
1071   "Function called after highlighting a summary line.")
1072
1073 (defvar gnus-summary-line-format-alist
1074   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1075     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1076     (?s gnus-tmp-subject-or-nil ?s)
1077     (?n gnus-tmp-name ?s)
1078     (?A (std11-address-string
1079          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1080     (?a (or (std11-full-name-string
1081              (car (mime-entity-read-field gnus-tmp-header 'From)))
1082             gnus-tmp-from) ?s)
1083     (?F gnus-tmp-from ?s)
1084     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1085     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1086     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1087     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1088     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1089     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1090     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1091     (?L gnus-tmp-lines ?s)
1092     (?I gnus-tmp-indentation ?s)
1093     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1094     (?R gnus-tmp-replied ?c)
1095     (?\[ gnus-tmp-opening-bracket ?c)
1096     (?\] gnus-tmp-closing-bracket ?c)
1097     (?\> (make-string gnus-tmp-level ? ) ?s)
1098     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1099     (?i gnus-tmp-score ?d)
1100     (?z gnus-tmp-score-char ?c)
1101     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1102     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1103     (?U gnus-tmp-unread ?c)
1104     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1105     (?t (gnus-summary-number-of-articles-in-thread
1106          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1107         ?d)
1108     (?e (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1110         ?c)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?P (gnus-pick-line-number) ?d))
1113   "An alist of format specifications that can appear in summary lines.
1114 These are paired with what variables they correspond with, along with
1115 the type of the variable (string, integer, character, etc).")
1116
1117 (defvar gnus-summary-dummy-line-format-alist
1118   `((?S gnus-tmp-subject ?s)
1119     (?N gnus-tmp-number ?d)
1120     (?u gnus-tmp-user-defined ?s)))
1121
1122 (defvar gnus-summary-mode-line-format-alist
1123   `((?G gnus-tmp-group-name ?s)
1124     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1125     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1126     (?A gnus-tmp-article-number ?d)
1127     (?Z gnus-tmp-unread-and-unselected ?s)
1128     (?V gnus-version ?s)
1129     (?U gnus-tmp-unread-and-unticked ?d)
1130     (?S gnus-tmp-subject ?s)
1131     (?e gnus-tmp-unselected ?d)
1132     (?u gnus-tmp-user-defined ?s)
1133     (?d (length gnus-newsgroup-dormant) ?d)
1134     (?t (length gnus-newsgroup-marked) ?d)
1135     (?r (length gnus-newsgroup-reads) ?d)
1136     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1137     (?E gnus-newsgroup-expunged-tally ?d)
1138     (?s (gnus-current-score-file-nondirectory) ?s)))
1139
1140 (defvar gnus-last-search-regexp nil
1141   "Default regexp for article search command.")
1142
1143 (defvar gnus-summary-search-article-matched-data nil
1144   "Last matched data of article search command.  It is the local variable
1145 in `gnus-article-buffer' which consists of the list of start position,
1146 end position and text.")
1147
1148 (defvar gnus-last-shell-command nil
1149   "Default shell command on article.")
1150
1151 (defvar gnus-newsgroup-begin nil)
1152 (defvar gnus-newsgroup-end nil)
1153 (defvar gnus-newsgroup-last-rmail nil)
1154 (defvar gnus-newsgroup-last-mail nil)
1155 (defvar gnus-newsgroup-last-folder nil)
1156 (defvar gnus-newsgroup-last-file nil)
1157 (defvar gnus-newsgroup-auto-expire nil)
1158 (defvar gnus-newsgroup-active nil)
1159
1160 (defvar gnus-newsgroup-data nil)
1161 (defvar gnus-newsgroup-data-reverse nil)
1162 (defvar gnus-newsgroup-limit nil)
1163 (defvar gnus-newsgroup-limits nil)
1164
1165 (defvar gnus-newsgroup-unreads nil
1166   "List of unread articles in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-unselected nil
1169   "List of unselected unread articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-reads nil
1172   "Alist of read articles and article marks in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-expunged-tally nil)
1175
1176 (defvar gnus-newsgroup-marked nil
1177   "List of ticked articles in the current newsgroup (a subset of unread art).")
1178
1179 (defvar gnus-newsgroup-killed nil
1180   "List of ranges of articles that have been through the scoring process.")
1181
1182 (defvar gnus-newsgroup-cached nil
1183   "List of articles that come from the article cache.")
1184
1185 (defvar gnus-newsgroup-saved nil
1186   "List of articles that have been saved.")
1187
1188 (defvar gnus-newsgroup-kill-headers nil)
1189
1190 (defvar gnus-newsgroup-replied nil
1191   "List of articles that have been replied to in the current newsgroup.")
1192
1193 (defvar gnus-newsgroup-forwarded nil
1194   "List of articles that have been forwarded in the current newsgroup.")
1195
1196 (defvar gnus-newsgroup-expirable nil
1197   "List of articles in the current newsgroup that can be expired.")
1198
1199 (defvar gnus-newsgroup-processable nil
1200   "List of articles in the current newsgroup that can be processed.")
1201
1202 (defvar gnus-newsgroup-downloadable nil
1203   "List of articles in the current newsgroup that can be processed.")
1204
1205 (defvar gnus-newsgroup-undownloaded nil
1206   "List of articles in the current newsgroup that haven't been downloaded..")
1207
1208 (defvar gnus-newsgroup-unsendable nil
1209   "List of articles in the current newsgroup that won't be sent.")
1210
1211 (defvar gnus-newsgroup-bookmarks nil
1212   "List of articles in the current newsgroup that have bookmarks.")
1213
1214 (defvar gnus-newsgroup-dormant nil
1215   "List of dormant articles in the current newsgroup.")
1216
1217 (defvar gnus-newsgroup-scored nil
1218   "List of scored articles in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-incorporated nil
1221   "List of incorporated articles in the current newsgroup.")
1222
1223 (defvar gnus-newsgroup-headers nil
1224   "List of article headers in the current newsgroup.")
1225
1226 (defvar gnus-newsgroup-threads nil)
1227
1228 (defvar gnus-newsgroup-prepared nil
1229   "Whether the current group has been prepared properly.")
1230
1231 (defvar gnus-newsgroup-ancient nil
1232   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-sparse nil)
1235
1236 (defvar gnus-current-article nil)
1237 (defvar gnus-article-current nil)
1238 (defvar gnus-current-headers nil)
1239 (defvar gnus-have-all-headers nil)
1240 (defvar gnus-last-article nil)
1241 (defvar gnus-newsgroup-history nil)
1242 (defvar gnus-newsgroup-charset nil)
1243 (defvar gnus-newsgroup-ephemeral-charset nil)
1244 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1245
1246 (defvar gnus-article-before-search nil)
1247
1248 (defconst gnus-summary-local-variables
1249   '(gnus-newsgroup-name
1250     gnus-newsgroup-begin gnus-newsgroup-end
1251     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1252     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1253     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1254     gnus-newsgroup-unselected gnus-newsgroup-marked
1255     gnus-newsgroup-reads gnus-newsgroup-saved
1256     gnus-newsgroup-replied gnus-newsgroup-forwarded
1257     gnus-newsgroup-expirable
1258     gnus-newsgroup-processable gnus-newsgroup-killed
1259     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1260     gnus-newsgroup-unsendable
1261     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1262     gnus-newsgroup-headers gnus-newsgroup-threads
1263     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1264     gnus-current-article gnus-current-headers gnus-have-all-headers
1265     gnus-last-article gnus-article-internal-prepare-hook
1266     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1267     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1268     gnus-thread-expunge-below
1269     gnus-score-alist gnus-current-score-file
1270     (gnus-summary-expunge-below . global)
1271     (gnus-summary-mark-below . global)
1272     (gnus-orphan-score . global)
1273     gnus-newsgroup-active gnus-scores-exclude-files
1274     gnus-newsgroup-history gnus-newsgroup-ancient
1275     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1276     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1277     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1278     (gnus-newsgroup-expunged-tally . 0)
1279     gnus-cache-removable-articles gnus-newsgroup-cached
1280     gnus-newsgroup-data gnus-newsgroup-data-reverse
1281     gnus-newsgroup-limit gnus-newsgroup-limits
1282     gnus-newsgroup-charset
1283     gnus-newsgroup-incorporated)
1284   "Variables that are buffer-local to the summary buffers.")
1285
1286 (defvar gnus-newsgroup-variables nil
1287   "Variables that have separate values in the newsgroups.")
1288
1289 ;; Byte-compiler warning.
1290 (eval-when-compile (defvar gnus-article-mode-map))
1291
1292 ;; Subject simplification.
1293
1294 (defun gnus-simplify-whitespace (str)
1295   "Remove excessive whitespace from STR."
1296   (let ((mystr str))
1297     ;; Multiple spaces.
1298     (while (string-match "[ \t][ \t]+" mystr)
1299       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1300                           " "
1301                           (substring mystr (match-end 0)))))
1302     ;; Leading spaces.
1303     (when (string-match "^[ \t]+" mystr)
1304       (setq mystr (substring mystr (match-end 0))))
1305     ;; Trailing spaces.
1306     (when (string-match "[ \t]+$" mystr)
1307       (setq mystr (substring mystr 0 (match-beginning 0))))
1308     mystr))
1309
1310 (defsubst gnus-simplify-subject-re (subject)
1311   "Remove \"Re:\" from subject lines."
1312   (if (string-match message-subject-re-regexp subject)
1313       (substring subject (match-end 0))
1314     subject))
1315
1316 (defun gnus-simplify-subject (subject &optional re-only)
1317   "Remove `Re:' and words in parentheses.
1318 If RE-ONLY is non-nil, strip leading `Re:'s only."
1319   (let ((case-fold-search t))           ;Ignore case.
1320     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1321     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1322       (setq subject (substring subject (match-end 0))))
1323     ;; Remove uninteresting prefixes.
1324     (when (and (not re-only)
1325                gnus-simplify-ignored-prefixes
1326                (string-match gnus-simplify-ignored-prefixes subject))
1327       (setq subject (substring subject (match-end 0))))
1328     ;; Remove words in parentheses from end.
1329     (unless re-only
1330       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1331         (setq subject (substring subject 0 (match-beginning 0)))))
1332     ;; Return subject string.
1333     subject))
1334
1335 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1336 ;; all whitespace.
1337 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1338   (goto-char (point-min))
1339   (while (re-search-forward regexp nil t)
1340     (replace-match (or newtext ""))))
1341
1342 (defun gnus-simplify-buffer-fuzzy ()
1343   "Simplify string in the buffer fuzzily.
1344 The string in the accessible portion of the current buffer is simplified.
1345 It is assumed to be a single-line subject.
1346 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1347 matter is removed.  Additional things can be deleted by setting
1348 `gnus-simplify-subject-fuzzy-regexp'."
1349   (let ((case-fold-search t)
1350         (modified-tick))
1351     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1352
1353     (while (not (eq modified-tick (buffer-modified-tick)))
1354       (setq modified-tick (buffer-modified-tick))
1355       (cond
1356        ((listp gnus-simplify-subject-fuzzy-regexp)
1357         (mapcar 'gnus-simplify-buffer-fuzzy-step
1358                 gnus-simplify-subject-fuzzy-regexp))
1359        (gnus-simplify-subject-fuzzy-regexp
1360         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1361       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1362       (gnus-simplify-buffer-fuzzy-step
1363        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1364       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1365
1366     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1367     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1368     (gnus-simplify-buffer-fuzzy-step " $")
1369     (gnus-simplify-buffer-fuzzy-step "^ +")))
1370
1371 (defun gnus-simplify-subject-fuzzy (subject)
1372   "Simplify a subject string fuzzily.
1373 See `gnus-simplify-buffer-fuzzy' for details."
1374   (save-excursion
1375     (gnus-set-work-buffer)
1376     (let ((case-fold-search t))
1377       ;; Remove uninteresting prefixes.
1378       (when (and gnus-simplify-ignored-prefixes
1379                  (string-match gnus-simplify-ignored-prefixes subject))
1380         (setq subject (substring subject (match-end 0))))
1381       (insert subject)
1382       (inline (gnus-simplify-buffer-fuzzy))
1383       (buffer-string))))
1384
1385 (defsubst gnus-simplify-subject-fully (subject)
1386   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1387   (cond
1388    (gnus-simplify-subject-functions
1389     (gnus-map-function gnus-simplify-subject-functions subject))
1390    ((null gnus-summary-gather-subject-limit)
1391     (gnus-simplify-subject-re subject))
1392    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1393     (gnus-simplify-subject-fuzzy subject))
1394    ((numberp gnus-summary-gather-subject-limit)
1395     (gnus-limit-string (gnus-simplify-subject-re subject)
1396                        gnus-summary-gather-subject-limit))
1397    (t
1398     subject)))
1399
1400 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1401   "Check whether two subjects are equal.
1402 If optional argument simple-first is t, first argument is already
1403 simplified."
1404   (cond
1405    ((null simple-first)
1406     (equal (gnus-simplify-subject-fully s1)
1407            (gnus-simplify-subject-fully s2)))
1408    (t
1409     (equal s1
1410            (gnus-simplify-subject-fully s2)))))
1411
1412 (defun gnus-summary-bubble-group ()
1413   "Increase the score of the current group.
1414 This is a handy function to add to `gnus-summary-exit-hook' to
1415 increase the score of each group you read."
1416   (gnus-group-add-score gnus-newsgroup-name))
1417
1418 \f
1419 ;;;
1420 ;;; Gnus summary mode
1421 ;;;
1422
1423 (put 'gnus-summary-mode 'mode-class 'special)
1424
1425 (defvar gnus-article-commands-menu)
1426
1427 (when t
1428   ;; Non-orthogonal keys
1429
1430   (gnus-define-keys gnus-summary-mode-map
1431     " " gnus-summary-next-page
1432     "\177" gnus-summary-prev-page
1433     [delete] gnus-summary-prev-page
1434     [backspace] gnus-summary-prev-page
1435     "\r" gnus-summary-scroll-up
1436     "\M-\r" gnus-summary-scroll-down
1437     "n" gnus-summary-next-unread-article
1438     "p" gnus-summary-prev-unread-article
1439     "N" gnus-summary-next-article
1440     "P" gnus-summary-prev-article
1441     "\M-\C-n" gnus-summary-next-same-subject
1442     "\M-\C-p" gnus-summary-prev-same-subject
1443     "\M-n" gnus-summary-next-unread-subject
1444     "\M-p" gnus-summary-prev-unread-subject
1445     "." gnus-summary-first-unread-article
1446     "," gnus-summary-best-unread-article
1447     "\M-s" gnus-summary-search-article-forward
1448     "\M-r" gnus-summary-search-article-backward
1449     "<" gnus-summary-beginning-of-article
1450     ">" gnus-summary-end-of-article
1451     "j" gnus-summary-goto-article
1452     "^" gnus-summary-refer-parent-article
1453     "\M-^" gnus-summary-refer-article
1454     "u" gnus-summary-tick-article-forward
1455     "!" gnus-summary-tick-article-forward
1456     "U" gnus-summary-tick-article-backward
1457     "d" gnus-summary-mark-as-read-forward
1458     "D" gnus-summary-mark-as-read-backward
1459     "E" gnus-summary-mark-as-expirable
1460     "\M-u" gnus-summary-clear-mark-forward
1461     "\M-U" gnus-summary-clear-mark-backward
1462     "k" gnus-summary-kill-same-subject-and-select
1463     "\C-k" gnus-summary-kill-same-subject
1464     "\M-\C-k" gnus-summary-kill-thread
1465     "\M-\C-l" gnus-summary-lower-thread
1466     "e" gnus-summary-edit-article
1467     "#" gnus-summary-mark-as-processable
1468     "\M-#" gnus-summary-unmark-as-processable
1469     "\M-\C-t" gnus-summary-toggle-threads
1470     "\M-\C-s" gnus-summary-show-thread
1471     "\M-\C-h" gnus-summary-hide-thread
1472     "\M-\C-f" gnus-summary-next-thread
1473     "\M-\C-b" gnus-summary-prev-thread
1474     [(meta down)] gnus-summary-next-thread
1475     [(meta up)] gnus-summary-prev-thread
1476     "\M-\C-u" gnus-summary-up-thread
1477     "\M-\C-d" gnus-summary-down-thread
1478     "&" gnus-summary-execute-command
1479     "c" gnus-summary-catchup-and-exit
1480     "\C-w" gnus-summary-mark-region-as-read
1481     "\C-t" gnus-summary-toggle-truncation
1482     "?" gnus-summary-mark-as-dormant
1483     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1484     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1485     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1486     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1487     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1488     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1489     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1490     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1491     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1492     "=" gnus-summary-expand-window
1493     "\C-x\C-s" gnus-summary-reselect-current-group
1494     "\M-g" gnus-summary-rescan-group
1495     "w" gnus-summary-stop-page-breaking
1496     "\C-c\C-r" gnus-summary-caesar-message
1497     "\M-t" gnus-summary-toggle-mime
1498     "f" gnus-summary-followup
1499     "F" gnus-summary-followup-with-original
1500     "C" gnus-summary-cancel-article
1501     "r" gnus-summary-reply
1502     "R" gnus-summary-reply-with-original
1503     "\C-c\C-f" gnus-summary-mail-forward
1504     "o" gnus-summary-save-article
1505     "\C-o" gnus-summary-save-article-mail
1506     "|" gnus-summary-pipe-output
1507     "\M-k" gnus-summary-edit-local-kill
1508     "\M-K" gnus-summary-edit-global-kill
1509     ;; "V" gnus-version
1510     "\C-c\C-d" gnus-summary-describe-group
1511     "q" gnus-summary-exit
1512     "Q" gnus-summary-exit-no-update
1513     "\C-c\C-i" gnus-info-find-node
1514     gnus-mouse-2 gnus-mouse-pick-article
1515     "m" gnus-summary-mail-other-window
1516     "a" gnus-summary-post-news
1517     "x" gnus-summary-limit-to-unread
1518     "s" gnus-summary-isearch-article
1519     "t" gnus-article-toggle-headers
1520     "g" gnus-summary-show-article
1521     "l" gnus-summary-goto-last-article
1522     "v" gnus-summary-preview-mime-message
1523     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1524     "\C-d" gnus-summary-enter-digest-group
1525     "\M-\C-d" gnus-summary-read-document
1526     "\M-\C-e" gnus-summary-edit-parameters
1527     "\M-\C-a" gnus-summary-customize-parameters
1528     "\C-c\C-b" gnus-bug
1529     "*" gnus-cache-enter-article
1530     "\M-*" gnus-cache-remove-article
1531     "\M-&" gnus-summary-universal-argument
1532     "\C-l" gnus-recenter
1533     "I" gnus-summary-increase-score
1534     "L" gnus-summary-lower-score
1535     "\M-i" gnus-symbolic-argument
1536     "h" gnus-summary-select-article-buffer
1537
1538     "V" gnus-summary-score-map
1539     "X" gnus-uu-extract-map
1540     "S" gnus-summary-send-map)
1541
1542   ;; Sort of orthogonal keymap
1543   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1544     "t" gnus-summary-tick-article-forward
1545     "!" gnus-summary-tick-article-forward
1546     "d" gnus-summary-mark-as-read-forward
1547     "r" gnus-summary-mark-as-read-forward
1548     "c" gnus-summary-clear-mark-forward
1549     " " gnus-summary-clear-mark-forward
1550     "e" gnus-summary-mark-as-expirable
1551     "x" gnus-summary-mark-as-expirable
1552     "?" gnus-summary-mark-as-dormant
1553     "b" gnus-summary-set-bookmark
1554     "B" gnus-summary-remove-bookmark
1555     "#" gnus-summary-mark-as-processable
1556     "\M-#" gnus-summary-unmark-as-processable
1557     "S" gnus-summary-limit-include-expunged
1558     "C" gnus-summary-catchup
1559     "H" gnus-summary-catchup-to-here
1560     "\C-c" gnus-summary-catchup-all
1561     "k" gnus-summary-kill-same-subject-and-select
1562     "K" gnus-summary-kill-same-subject
1563     "P" gnus-uu-mark-map)
1564
1565   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1566     "c" gnus-summary-clear-above
1567     "u" gnus-summary-tick-above
1568     "m" gnus-summary-mark-above
1569     "k" gnus-summary-kill-below)
1570
1571   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1572     "/" gnus-summary-limit-to-subject
1573     "n" gnus-summary-limit-to-articles
1574     "w" gnus-summary-pop-limit
1575     "s" gnus-summary-limit-to-subject
1576     "a" gnus-summary-limit-to-author
1577     "u" gnus-summary-limit-to-unread
1578     "m" gnus-summary-limit-to-marks
1579     "M" gnus-summary-limit-exclude-marks
1580     "v" gnus-summary-limit-to-score
1581     "*" gnus-summary-limit-include-cached
1582     "D" gnus-summary-limit-include-dormant
1583     "T" gnus-summary-limit-include-thread
1584     "d" gnus-summary-limit-exclude-dormant
1585     "t" gnus-summary-limit-to-age
1586     "x" gnus-summary-limit-to-extra
1587     "E" gnus-summary-limit-include-expunged
1588     "c" gnus-summary-limit-exclude-childless-dormant
1589     "C" gnus-summary-limit-mark-excluded-as-read
1590     "o" gnus-summary-insert-old-articles
1591     "N" gnus-summary-insert-new-articles)
1592
1593   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1594     "n" gnus-summary-next-unread-article
1595     "p" gnus-summary-prev-unread-article
1596     "N" gnus-summary-next-article
1597     "P" gnus-summary-prev-article
1598     "\C-n" gnus-summary-next-same-subject
1599     "\C-p" gnus-summary-prev-same-subject
1600     "\M-n" gnus-summary-next-unread-subject
1601     "\M-p" gnus-summary-prev-unread-subject
1602     "f" gnus-summary-first-unread-article
1603     "b" gnus-summary-best-unread-article
1604     "j" gnus-summary-goto-article
1605     "g" gnus-summary-goto-subject
1606     "l" gnus-summary-goto-last-article
1607     "o" gnus-summary-pop-article)
1608
1609   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1610     "k" gnus-summary-kill-thread
1611     "l" gnus-summary-lower-thread
1612     "i" gnus-summary-raise-thread
1613     "T" gnus-summary-toggle-threads
1614     "t" gnus-summary-rethread-current
1615     "^" gnus-summary-reparent-thread
1616     "s" gnus-summary-show-thread
1617     "S" gnus-summary-show-all-threads
1618     "h" gnus-summary-hide-thread
1619     "H" gnus-summary-hide-all-threads
1620     "n" gnus-summary-next-thread
1621     "p" gnus-summary-prev-thread
1622     "u" gnus-summary-up-thread
1623     "o" gnus-summary-top-thread
1624     "d" gnus-summary-down-thread
1625     "#" gnus-uu-mark-thread
1626     "\M-#" gnus-uu-unmark-thread)
1627
1628   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1629     "g" gnus-summary-prepare
1630     "c" gnus-summary-insert-cached-articles)
1631
1632   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1633     "c" gnus-summary-catchup-and-exit
1634     "C" gnus-summary-catchup-all-and-exit
1635     "E" gnus-summary-exit-no-update
1636     "J" gnus-summary-jump-to-other-group
1637     "Q" gnus-summary-exit
1638     "Z" gnus-summary-exit
1639     "n" gnus-summary-catchup-and-goto-next-group
1640     "R" gnus-summary-reselect-current-group
1641     "G" gnus-summary-rescan-group
1642     "N" gnus-summary-next-group
1643     "s" gnus-summary-save-newsrc
1644     "P" gnus-summary-prev-group)
1645
1646   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1647     " " gnus-summary-next-page
1648     "n" gnus-summary-next-page
1649     "\177" gnus-summary-prev-page
1650     [delete] gnus-summary-prev-page
1651     "p" gnus-summary-prev-page
1652     "\r" gnus-summary-scroll-up
1653     "\M-\r" gnus-summary-scroll-down
1654     "<" gnus-summary-beginning-of-article
1655     ">" gnus-summary-end-of-article
1656     "b" gnus-summary-beginning-of-article
1657     "e" gnus-summary-end-of-article
1658     "^" gnus-summary-refer-parent-article
1659     "r" gnus-summary-refer-parent-article
1660     "D" gnus-summary-enter-digest-group
1661     "R" gnus-summary-refer-references
1662     "T" gnus-summary-refer-thread
1663     "g" gnus-summary-show-article
1664     "s" gnus-summary-isearch-article
1665     "P" gnus-summary-print-article
1666     "M" gnus-mailing-list-insinuate
1667     "t" gnus-article-babel)
1668
1669   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1670     "b" gnus-article-add-buttons
1671     "B" gnus-article-add-buttons-to-head
1672     "o" gnus-article-treat-overstrike
1673     "e" gnus-article-emphasize
1674     "w" gnus-article-fill-cited-article
1675     "Q" gnus-article-fill-long-lines
1676     "C" gnus-article-capitalize-sentences
1677     "c" gnus-article-remove-cr
1678     "Z" gnus-article-decode-HZ
1679     "f" gnus-article-display-x-face
1680     "l" gnus-summary-stop-page-breaking
1681     "r" gnus-summary-caesar-message
1682     "t" gnus-article-toggle-headers
1683     "v" gnus-summary-verbose-headers
1684     "m" gnus-summary-toggle-mime
1685     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1686     "p" gnus-article-verify-x-pgp-sig
1687     "d" gnus-article-treat-dumbquotes
1688     "s" gnus-smiley-display)
1689
1690   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1691     "a" gnus-article-hide
1692     "h" gnus-article-toggle-headers
1693     "b" gnus-article-hide-boring-headers
1694     "s" gnus-article-hide-signature
1695     "c" gnus-article-hide-citation
1696     "C" gnus-article-hide-citation-in-followups
1697     "l" gnus-article-hide-list-identifiers
1698     "p" gnus-article-hide-pgp
1699     "B" gnus-article-strip-banner
1700     "P" gnus-article-hide-pem
1701     "\C-c" gnus-article-hide-citation-maybe)
1702
1703   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1704     "a" gnus-article-highlight
1705     "h" gnus-article-highlight-headers
1706     "c" gnus-article-highlight-citation
1707     "s" gnus-article-highlight-signature)
1708
1709   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1710     "z" gnus-article-date-ut
1711     "u" gnus-article-date-ut
1712     "l" gnus-article-date-local
1713     "p" gnus-article-date-english
1714     "e" gnus-article-date-lapsed
1715     "o" gnus-article-date-original
1716     "i" gnus-article-date-iso8601
1717     "s" gnus-article-date-user)
1718
1719   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1720     "t" gnus-article-remove-trailing-blank-lines
1721     "l" gnus-article-strip-leading-blank-lines
1722     "m" gnus-article-strip-multiple-blank-lines
1723     "a" gnus-article-strip-blank-lines
1724     "A" gnus-article-strip-all-blank-lines
1725     "s" gnus-article-strip-leading-space
1726     "e" gnus-article-strip-trailing-space
1727     "w" gnus-article-remove-leading-whitespace)
1728
1729   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1730     "v" gnus-version
1731     "f" gnus-summary-fetch-faq
1732     "d" gnus-summary-describe-group
1733     "h" gnus-summary-describe-briefly
1734     "i" gnus-info-find-node)
1735
1736   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1737     "e" gnus-summary-expire-articles
1738     "\M-\C-e" gnus-summary-expire-articles-now
1739     "\177" gnus-summary-delete-article
1740     [delete] gnus-summary-delete-article
1741     [backspace] gnus-summary-delete-article
1742     "m" gnus-summary-move-article
1743     "r" gnus-summary-respool-article
1744     "w" gnus-summary-edit-article
1745     "c" gnus-summary-copy-article
1746     "B" gnus-summary-crosspost-article
1747     "q" gnus-summary-respool-query
1748     "t" gnus-summary-respool-trace
1749     "i" gnus-summary-import-article
1750     "I" gnus-summary-create-article
1751     "p" gnus-summary-article-posted-p)
1752
1753   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1754     "o" gnus-summary-save-article
1755     "m" gnus-summary-save-article-mail
1756     "F" gnus-summary-write-article-file
1757     "r" gnus-summary-save-article-rmail
1758     "f" gnus-summary-save-article-file
1759     "b" gnus-summary-save-article-body-file
1760     "h" gnus-summary-save-article-folder
1761     "v" gnus-summary-save-article-vm
1762     "p" gnus-summary-pipe-output
1763     "s" gnus-soup-add-article)
1764
1765   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1766     "b" gnus-summary-display-buttonized
1767     "m" gnus-summary-repair-multipart
1768     "v" gnus-article-view-part
1769     "o" gnus-article-save-part
1770     "c" gnus-article-copy-part
1771     "C" gnus-article-view-part-as-charset
1772     "e" gnus-article-externalize-part
1773     "E" gnus-article-encrypt-body
1774     "i" gnus-article-inline-part
1775     "|" gnus-article-pipe-part))
1776
1777 (defvar gnus-article-post-menu nil)
1778
1779 (defun gnus-summary-make-menu-bar ()
1780   (gnus-turn-off-edit-menu 'summary)
1781
1782   (unless (boundp 'gnus-summary-misc-menu)
1783
1784     (easy-menu-define
1785      gnus-summary-kill-menu gnus-summary-mode-map ""
1786      (cons
1787       "Score"
1788       (nconc
1789        (list
1790         ["Customize" gnus-score-customize t])
1791        (gnus-make-score-map 'increase)
1792        (gnus-make-score-map 'lower)
1793        '(("Mark"
1794           ["Kill below" gnus-summary-kill-below t]
1795           ["Mark above" gnus-summary-mark-above t]
1796           ["Tick above" gnus-summary-tick-above t]
1797           ["Clear above" gnus-summary-clear-above t])
1798          ["Current score" gnus-summary-current-score t]
1799          ["Set score" gnus-summary-set-score t]
1800          ["Switch current score file..." gnus-score-change-score-file t]
1801          ["Set mark below..." gnus-score-set-mark-below t]
1802          ["Set expunge below..." gnus-score-set-expunge-below t]
1803          ["Edit current score file" gnus-score-edit-current-scores t]
1804          ["Edit score file" gnus-score-edit-file t]
1805          ["Trace score" gnus-score-find-trace t]
1806          ["Find words" gnus-score-find-favourite-words t]
1807          ["Rescore buffer" gnus-summary-rescore t]
1808          ["Increase score..." gnus-summary-increase-score t]
1809          ["Lower score..." gnus-summary-lower-score t]))))
1810
1811     ;; Define both the Article menu in the summary buffer and the equivalent
1812     ;; Commands menu in the article buffer here for consistency.
1813     (let ((innards
1814            `(("Hide"
1815               ["All" gnus-article-hide t]
1816               ["Headers" gnus-article-toggle-headers t]
1817               ["Signature" gnus-article-hide-signature t]
1818               ["Citation" gnus-article-hide-citation t]
1819               ["List identifiers" gnus-article-hide-list-identifiers t]
1820               ["PGP" gnus-article-hide-pgp t]
1821               ["Banner" gnus-article-strip-banner t]
1822               ["Boring headers" gnus-article-hide-boring-headers t])
1823              ("Highlight"
1824               ["All" gnus-article-highlight t]
1825               ["Headers" gnus-article-highlight-headers t]
1826               ["Signature" gnus-article-highlight-signature t]
1827               ["Citation" gnus-article-highlight-citation t])
1828              ("Date"
1829               ["Local" gnus-article-date-local t]
1830               ["ISO8601" gnus-article-date-iso8601 t]
1831               ["UT" gnus-article-date-ut t]
1832               ["Original" gnus-article-date-original t]
1833               ["Lapsed" gnus-article-date-lapsed t]
1834               ["User-defined" gnus-article-date-user t])
1835              ("Washing"
1836               ("Remove Blanks"
1837                ["Leading" gnus-article-strip-leading-blank-lines t]
1838                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1839                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1840                ["All of the above" gnus-article-strip-blank-lines t]
1841                ["All" gnus-article-strip-all-blank-lines t]
1842                ["Leading space" gnus-article-strip-leading-space t]
1843                ["Trailing space" gnus-article-strip-trailing-space t]
1844                ["Leading space in headers" 
1845                 gnus-article-remove-leading-whitespace t])
1846               ["Overstrike" gnus-article-treat-overstrike t]
1847               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1848               ["Emphasis" gnus-article-emphasize t]
1849               ["Word wrap" gnus-article-fill-cited-article t]
1850               ["Fill long lines" gnus-article-fill-long-lines t]
1851               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1852               ["CR" gnus-article-remove-cr t]
1853               ["Show X-Face" gnus-article-display-x-face t]
1854               ["Rot 13" gnus-summary-caesar-message
1855                ,@(if (featurep 'xemacs) '(t)
1856                    '(:help "\"Caesar rotate\" article by 13"))]
1857               ["Unix pipe" gnus-summary-pipe-message t]
1858               ["Add buttons" gnus-article-add-buttons t]
1859               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1860               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1861               ["Toggle MIME" gnus-summary-toggle-mime t]
1862               ["Verbose header" gnus-summary-verbose-headers t]
1863               ["Toggle header" gnus-summary-toggle-header t]
1864               ["Toggle smileys" gnus-smiley-display t]
1865               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1866               ["HZ" gnus-article-decode-HZ t])
1867              ("Output"
1868               ["Save in default format" gnus-summary-save-article
1869                ,@(if (featurep 'xemacs) '(t)
1870                    '(:help "Save article using default method"))]
1871               ["Save in file" gnus-summary-save-article-file
1872                ,@(if (featurep 'xemacs) '(t)
1873                    '(:help "Save article in file"))]
1874               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1875               ["Save in MH folder" gnus-summary-save-article-folder t]
1876               ["Save in VM folder" gnus-summary-save-article-vm t]
1877               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1878               ["Save body in file" gnus-summary-save-article-body-file t]
1879               ["Pipe through a filter" gnus-summary-pipe-output t]
1880               ["Add to SOUP packet" gnus-soup-add-article t]
1881               ["Print" gnus-summary-print-article t])
1882              ("Backend"
1883               ["Respool article..." gnus-summary-respool-article t]
1884               ["Move article..." gnus-summary-move-article
1885                (gnus-check-backend-function
1886                 'request-move-article gnus-newsgroup-name)]
1887               ["Copy article..." gnus-summary-copy-article t]
1888               ["Crosspost article..." gnus-summary-crosspost-article
1889                (gnus-check-backend-function
1890                 'request-replace-article gnus-newsgroup-name)]
1891               ["Import file..." gnus-summary-import-article t]
1892               ["Create article..." gnus-summary-create-article t]
1893               ["Check if posted" gnus-summary-article-posted-p t]
1894               ["Edit article" gnus-summary-edit-article
1895                (not (gnus-group-read-only-p))]
1896               ["Delete article" gnus-summary-delete-article
1897                (gnus-check-backend-function
1898                 'request-expire-articles gnus-newsgroup-name)]
1899               ["Query respool" gnus-summary-respool-query t]
1900               ["Trace respool" gnus-summary-respool-trace t]
1901               ["Delete expirable articles" gnus-summary-expire-articles-now
1902                (gnus-check-backend-function
1903                 'request-expire-articles gnus-newsgroup-name)])
1904              ("Extract"
1905               ["Uudecode" gnus-uu-decode-uu
1906                ,@(if (featurep 'xemacs) '(t)
1907                    '(:help "Decode uuencoded article(s)"))]
1908               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1909               ["Unshar" gnus-uu-decode-unshar t]
1910               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1911               ["Save" gnus-uu-decode-save t]
1912               ["Binhex" gnus-uu-decode-binhex t]
1913               ["Postscript" gnus-uu-decode-postscript t])
1914              ("Cache"
1915               ["Enter article" gnus-cache-enter-article t]
1916               ["Remove article" gnus-cache-remove-article t])
1917              ["Translate" gnus-article-babel t]
1918              ["Select article buffer" gnus-summary-select-article-buffer t]
1919              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1920              ["Isearch article..." gnus-summary-isearch-article t]
1921              ["Beginning of the article" gnus-summary-beginning-of-article t]
1922              ["End of the article" gnus-summary-end-of-article t]
1923              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1924              ["Fetch referenced articles" gnus-summary-refer-references t]
1925              ["Fetch current thread" gnus-summary-refer-thread t]
1926              ["Fetch article with id..." gnus-summary-refer-article t]
1927              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1928              ["Redisplay" gnus-summary-show-article t])))
1929       (easy-menu-define
1930        gnus-summary-article-menu gnus-summary-mode-map ""
1931        (cons "Article" innards))
1932
1933       (if (not (keymapp gnus-summary-article-menu))
1934           (easy-menu-define
1935             gnus-article-commands-menu gnus-article-mode-map ""
1936             (cons "Commands" innards))
1937         ;; in Emacs, don't share menu.
1938         (setq gnus-article-commands-menu 
1939               (copy-keymap gnus-summary-article-menu))
1940         (define-key gnus-article-mode-map [menu-bar commands]
1941           (cons "Commands" gnus-article-commands-menu))))
1942
1943     (easy-menu-define
1944      gnus-summary-thread-menu gnus-summary-mode-map ""
1945      '("Threads"
1946        ["Toggle threading" gnus-summary-toggle-threads t]
1947        ["Hide threads" gnus-summary-hide-all-threads t]
1948        ["Show threads" gnus-summary-show-all-threads t]
1949        ["Hide thread" gnus-summary-hide-thread t]
1950        ["Show thread" gnus-summary-show-thread t]
1951        ["Go to next thread" gnus-summary-next-thread t]
1952        ["Go to previous thread" gnus-summary-prev-thread t]
1953        ["Go down thread" gnus-summary-down-thread t]
1954        ["Go up thread" gnus-summary-up-thread t]
1955        ["Top of thread" gnus-summary-top-thread t]
1956        ["Mark thread as read" gnus-summary-kill-thread t]
1957        ["Lower thread score" gnus-summary-lower-thread t]
1958        ["Raise thread score" gnus-summary-raise-thread t]
1959        ["Rethread current" gnus-summary-rethread-current t]))
1960
1961     (easy-menu-define
1962      gnus-summary-post-menu gnus-summary-mode-map ""
1963      `("Post"
1964        ["Post an article" gnus-summary-post-news
1965         ,@(if (featurep 'xemacs) '(t)
1966             '(:help "Post an article"))]
1967        ["Followup" gnus-summary-followup
1968         ,@(if (featurep 'xemacs) '(t)
1969             '(:help "Post followup to this article"))]
1970        ["Followup and yank" gnus-summary-followup-with-original
1971         ,@(if (featurep 'xemacs) '(t)
1972             '(:help "Post followup to this article, quoting its contents"))]
1973        ["Supersede article" gnus-summary-supersede-article t]
1974        ["Cancel article" gnus-summary-cancel-article
1975         ,@(if (featurep 'xemacs) '(t)
1976             '(:help "Cancel an article you posted"))]
1977        ["Reply" gnus-summary-reply t]
1978        ["Reply and yank" gnus-summary-reply-with-original t]
1979        ["Wide reply" gnus-summary-wide-reply t]
1980        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1981         ,@(if (featurep 'xemacs) '(t)
1982             '(:help "Mail a reply, quoting this article"))]
1983        ["Mail forward" gnus-summary-mail-forward t]
1984        ["Post forward" gnus-summary-post-forward t]
1985        ["Digest and mail" gnus-summary-digest-mail-forward t]
1986        ["Digest and post" gnus-summary-digest-post-forward t]
1987        ["Resend message" gnus-summary-resend-message t]
1988        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1989        ["Send a mail" gnus-summary-mail-other-window t]
1990        ["Uuencode and post" gnus-uu-post-news
1991         ,@(if (featurep 'xemacs) '(t)
1992             '(:help "Post a uuencoded article"))]
1993        ["Followup via news" gnus-summary-followup-to-mail t]
1994        ["Followup via news and yank"
1995         gnus-summary-followup-to-mail-with-original t]
1996        ;;("Draft"
1997        ;;["Send" gnus-summary-send-draft t]
1998        ;;["Send bounced" gnus-resend-bounced-mail t])
1999        ))
2000
2001     (cond 
2002      ((not (keymapp gnus-summary-post-menu))
2003       (setq gnus-article-post-menu gnus-summary-post-menu))
2004      ((not gnus-article-post-menu)
2005       ;; Don't share post menu.
2006       (setq gnus-article-post-menu
2007             (copy-keymap gnus-summary-post-menu))))
2008     (define-key gnus-article-mode-map [menu-bar post]
2009       (cons "Post" gnus-article-post-menu))
2010
2011     (easy-menu-define
2012      gnus-summary-misc-menu gnus-summary-mode-map ""
2013      `("Misc"
2014        ("Mark Read"
2015         ["Mark as read" gnus-summary-mark-as-read-forward t]
2016         ["Mark same subject and select"
2017          gnus-summary-kill-same-subject-and-select t]
2018         ["Mark same subject" gnus-summary-kill-same-subject t]
2019         ["Catchup" gnus-summary-catchup
2020          ,@(if (featurep 'xemacs) '(t)
2021              '(:help "Mark unread articles in this group as read"))]
2022         ["Catchup all" gnus-summary-catchup-all t]
2023         ["Catchup to here" gnus-summary-catchup-to-here t]
2024         ["Catchup region" gnus-summary-mark-region-as-read t]
2025         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2026        ("Mark Various"
2027         ["Tick" gnus-summary-tick-article-forward t]
2028         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2029         ["Remove marks" gnus-summary-clear-mark-forward t]
2030         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2031         ["Set bookmark" gnus-summary-set-bookmark t]
2032         ["Remove bookmark" gnus-summary-remove-bookmark t])
2033        ("Mark Limit"
2034         ["Marks..." gnus-summary-limit-to-marks t]
2035         ["Subject..." gnus-summary-limit-to-subject t]
2036         ["Author..." gnus-summary-limit-to-author t]
2037         ["Age..." gnus-summary-limit-to-age t]
2038         ["Extra..." gnus-summary-limit-to-extra t]
2039         ["Score" gnus-summary-limit-to-score t]
2040         ["Unread" gnus-summary-limit-to-unread t]
2041         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2042         ["Articles" gnus-summary-limit-to-articles t]
2043         ["Pop limit" gnus-summary-pop-limit t]
2044         ["Show dormant" gnus-summary-limit-include-dormant t]
2045         ["Hide childless dormant"
2046          gnus-summary-limit-exclude-childless-dormant t]
2047         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2048         ["Hide marked" gnus-summary-limit-exclude-marks t]
2049         ["Show expunged" gnus-summary-limit-include-expunged t])
2050        ("Process Mark"
2051         ["Set mark" gnus-summary-mark-as-processable t]
2052         ["Remove mark" gnus-summary-unmark-as-processable t]
2053         ["Remove all marks" gnus-summary-unmark-all-processable t]
2054         ["Mark above" gnus-uu-mark-over t]
2055         ["Mark series" gnus-uu-mark-series t]
2056         ["Mark region" gnus-uu-mark-region t]
2057         ["Unmark region" gnus-uu-unmark-region t]
2058         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2059         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2060         ["Mark all" gnus-uu-mark-all t]
2061         ["Mark buffer" gnus-uu-mark-buffer t]
2062         ["Mark sparse" gnus-uu-mark-sparse t]
2063         ["Mark thread" gnus-uu-mark-thread t]
2064         ["Unmark thread" gnus-uu-unmark-thread t]
2065         ("Process Mark Sets"
2066          ["Kill" gnus-summary-kill-process-mark t]
2067          ["Yank" gnus-summary-yank-process-mark
2068           gnus-newsgroup-process-stack]
2069          ["Save" gnus-summary-save-process-mark t]))
2070        ("Scroll article"
2071         ["Page forward" gnus-summary-next-page
2072          ,@(if (featurep 'xemacs) '(t)
2073              '(:help "Show next page of article"))]
2074         ["Page backward" gnus-summary-prev-page
2075          ,@(if (featurep 'xemacs) '(t)
2076              '(:help "Show previous page of article"))]
2077         ["Line forward" gnus-summary-scroll-up t])
2078        ("Move"
2079         ["Next unread article" gnus-summary-next-unread-article t]
2080         ["Previous unread article" gnus-summary-prev-unread-article t]
2081         ["Next article" gnus-summary-next-article t]
2082         ["Previous article" gnus-summary-prev-article t]
2083         ["Next unread subject" gnus-summary-next-unread-subject t]
2084         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2085         ["Next article same subject" gnus-summary-next-same-subject t]
2086         ["Previous article same subject" gnus-summary-prev-same-subject t]
2087         ["First unread article" gnus-summary-first-unread-article t]
2088         ["Best unread article" gnus-summary-best-unread-article t]
2089         ["Go to subject number..." gnus-summary-goto-subject t]
2090         ["Go to article number..." gnus-summary-goto-article t]
2091         ["Go to the last article" gnus-summary-goto-last-article t]
2092         ["Pop article off history" gnus-summary-pop-article t])
2093        ("Sort"
2094         ["Sort by number" gnus-summary-sort-by-number t]
2095         ["Sort by author" gnus-summary-sort-by-author t]
2096         ["Sort by subject" gnus-summary-sort-by-subject t]
2097         ["Sort by date" gnus-summary-sort-by-date t]
2098         ["Sort by score" gnus-summary-sort-by-score t]
2099         ["Sort by lines" gnus-summary-sort-by-lines t]
2100         ["Sort by characters" gnus-summary-sort-by-chars t]
2101         ["Original sort" gnus-summary-sort-by-original t])
2102        ("Help"
2103         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2104         ["Describe group" gnus-summary-describe-group t]
2105         ["Read manual" gnus-info-find-node t])
2106        ("Modes"
2107         ["Pick and read" gnus-pick-mode t]
2108         ["Binary" gnus-binary-mode t])
2109        ("Regeneration"
2110         ["Regenerate" gnus-summary-prepare t]
2111         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2112         ["Toggle threading" gnus-summary-toggle-threads t])
2113        ["See old articles" gnus-summary-insert-old-articles t]
2114        ["See new articles" gnus-summary-insert-new-articles t]
2115        ["Filter articles..." gnus-summary-execute-command t]
2116        ["Run command on subjects..." gnus-summary-universal-argument t]
2117        ["Search articles forward..." gnus-summary-search-article-forward t]
2118        ["Search articles backward..." gnus-summary-search-article-backward t]
2119        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2120        ["Expand window" gnus-summary-expand-window t]
2121        ["Expire expirable articles" gnus-summary-expire-articles
2122         (gnus-check-backend-function
2123          'request-expire-articles gnus-newsgroup-name)]
2124        ["Edit local kill file" gnus-summary-edit-local-kill t]
2125        ["Edit main kill file" gnus-summary-edit-global-kill t]
2126        ["Edit group parameters" gnus-summary-edit-parameters t]
2127        ["Customize group parameters" gnus-summary-customize-parameters t]
2128        ["Send a bug report" gnus-bug t]
2129        ("Exit"
2130         ["Catchup and exit" gnus-summary-catchup-and-exit
2131          ,@(if (featurep 'xemacs) '(t)
2132              '(:help "Mark unread articles in this group as read, then exit"))]
2133         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2134         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2135         ["Exit group" gnus-summary-exit
2136          ,@(if (featurep 'xemacs) '(t)
2137              '(:help "Exit current group, return to group selection mode"))]
2138         ["Exit group without updating" gnus-summary-exit-no-update t]
2139         ["Exit and goto next group" gnus-summary-next-group t]
2140         ["Exit and goto prev group" gnus-summary-prev-group t]
2141         ["Reselect group" gnus-summary-reselect-current-group t]
2142         ["Rescan group" gnus-summary-rescan-group t]
2143         ["Update dribble" gnus-summary-save-newsrc t])))
2144
2145     (gnus-run-hooks 'gnus-summary-menu-hook)))
2146
2147 (defvar gnus-summary-tool-bar-map nil)
2148
2149 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2150 (defun gnus-summary-make-tool-bar ()
2151   (if (and (fboundp 'tool-bar-add-item-from-menu)
2152            (default-value 'tool-bar-mode)
2153            (not gnus-summary-tool-bar-map))
2154       (setq gnus-summary-tool-bar-map
2155             (let ((tool-bar-map (make-sparse-keymap))
2156                   (load-path (mm-image-load-path)))
2157               (tool-bar-add-item-from-menu
2158                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2159               (tool-bar-add-item-from-menu
2160                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2161               (tool-bar-add-item-from-menu
2162                'gnus-summary-post-news "post" gnus-summary-mode-map)
2163               (tool-bar-add-item-from-menu
2164                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2165               (tool-bar-add-item-from-menu
2166                'gnus-summary-followup "followup" gnus-summary-mode-map)
2167               (tool-bar-add-item-from-menu
2168                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2169               (tool-bar-add-item-from-menu
2170                'gnus-summary-reply "reply" gnus-summary-mode-map)
2171               (tool-bar-add-item-from-menu
2172                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2173               (tool-bar-add-item-from-menu
2174                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2175               (tool-bar-add-item-from-menu
2176                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2177               (tool-bar-add-item-from-menu
2178                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2179               (tool-bar-add-item-from-menu
2180                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2181               (tool-bar-add-item-from-menu
2182                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2183               (tool-bar-add-item-from-menu
2184                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2185               (tool-bar-add-item-from-menu
2186                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2187               tool-bar-map)))
2188   (if gnus-summary-tool-bar-map
2189       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2190
2191 (defun gnus-score-set-default (var value)
2192   "A version of set that updates the GNU Emacs menu-bar."
2193   (set var value)
2194   ;; It is the message that forces the active status to be updated.
2195   (message ""))
2196
2197 (defun gnus-make-score-map (type)
2198   "Make a summary score map of type TYPE."
2199   (if t
2200       nil
2201     (let ((headers '(("author" "from" string)
2202                      ("subject" "subject" string)
2203                      ("article body" "body" string)
2204                      ("article head" "head" string)
2205                      ("xref" "xref" string)
2206                      ("extra header" "extra" string)
2207                      ("lines" "lines" number)
2208                      ("followups to author" "followup" string)))
2209           (types '((number ("less than" <)
2210                            ("greater than" >)
2211                            ("equal" =))
2212                    (string ("substring" s)
2213                            ("exact string" e)
2214                            ("fuzzy string" f)
2215                            ("regexp" r))))
2216           (perms '(("temporary" (current-time-string))
2217                    ("permanent" nil)
2218                    ("immediate" now)))
2219           header)
2220       (list
2221        (apply
2222         'nconc
2223         (list
2224          (if (eq type 'lower)
2225              "Lower score"
2226            "Increase score"))
2227         (let (outh)
2228           (while headers
2229             (setq header (car headers))
2230             (setq outh
2231                   (cons
2232                    (apply
2233                     'nconc
2234                     (list (car header))
2235                     (let ((ts (cdr (assoc (nth 2 header) types)))
2236                           outt)
2237                       (while ts
2238                         (setq outt
2239                               (cons
2240                                (apply
2241                                 'nconc
2242                                 (list (caar ts))
2243                                 (let ((ps perms)
2244                                       outp)
2245                                   (while ps
2246                                     (setq outp
2247                                           (cons
2248                                            (vector
2249                                             (caar ps)
2250                                             (list
2251                                              'gnus-summary-score-entry
2252                                              (nth 1 header)
2253                                              (if (or (string= (nth 1 header)
2254                                                               "head")
2255                                                      (string= (nth 1 header)
2256                                                               "body"))
2257                                                  ""
2258                                                (list 'gnus-summary-header
2259                                                      (nth 1 header)))
2260                                              (list 'quote (nth 1 (car ts)))
2261                                              (list 'gnus-score-delta-default
2262                                                    nil)
2263                                              (nth 1 (car ps))
2264                                              t)
2265                                             t)
2266                                            outp))
2267                                     (setq ps (cdr ps)))
2268                                   (list (nreverse outp))))
2269                                outt))
2270                         (setq ts (cdr ts)))
2271                       (list (nreverse outt))))
2272                    outh))
2273             (setq headers (cdr headers)))
2274           (list (nreverse outh))))))))
2275
2276 \f
2277
2278 (defun gnus-summary-mode (&optional group)
2279   "Major mode for reading articles.
2280
2281 All normal editing commands are switched off.
2282 \\<gnus-summary-mode-map>
2283 Each line in this buffer represents one article.  To read an
2284 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2285 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2286 respectively.
2287
2288 You can also post articles and send mail from this buffer.  To
2289 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2290 of an article, type `\\[gnus-summary-reply]'.
2291
2292 There are approx. one gazillion commands you can execute in this
2293 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2294
2295 The following commands are available:
2296
2297 \\{gnus-summary-mode-map}"
2298   (interactive)
2299   (kill-all-local-variables)
2300   (when (gnus-visual-p 'summary-menu 'menu)
2301     (gnus-summary-make-menu-bar)
2302     (gnus-summary-make-tool-bar))
2303   (gnus-summary-make-local-variables)
2304   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2305     (gnus-summary-make-local-variables))
2306   (gnus-make-thread-indent-array)
2307   (gnus-simplify-mode-line)
2308   (setq major-mode 'gnus-summary-mode)
2309   (setq mode-name "Summary")
2310   (make-local-variable 'minor-mode-alist)
2311   (use-local-map gnus-summary-mode-map)
2312   (buffer-disable-undo)
2313   (setq buffer-read-only t)             ;Disable modification
2314   (setq truncate-lines t)
2315   (setq selective-display t)
2316   (setq selective-display-ellipses t)   ;Display `...'
2317   (gnus-summary-set-display-table)
2318   (gnus-set-default-directory)
2319   (setq gnus-newsgroup-name group)
2320   (unless (gnus-news-group-p group)
2321     (setq gnus-newsgroup-incorporated
2322           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2323   (make-local-variable 'gnus-summary-line-format)
2324   (make-local-variable 'gnus-summary-line-format-spec)
2325   (make-local-variable 'gnus-summary-dummy-line-format)
2326   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2327   (make-local-variable 'gnus-summary-mark-positions)
2328   (make-local-hook 'pre-command-hook)
2329   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2330   (gnus-run-hooks 'gnus-summary-mode-hook)
2331   (turn-on-gnus-mailing-list-mode)
2332   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2333   (gnus-update-summary-mark-positions))
2334
2335 (defun gnus-summary-make-local-variables ()
2336   "Make all the local summary buffer variables."
2337   (let (global)
2338     (dolist (local gnus-summary-local-variables)
2339       (if (consp local)
2340           (progn
2341             (if (eq (cdr local) 'global)
2342                 ;; Copy the global value of the variable.
2343                 (setq global (symbol-value (car local)))
2344               ;; Use the value from the list.
2345               (setq global (eval (cdr local))))
2346             (set (make-local-variable (car local)) global))
2347         ;; Simple nil-valued local variable.
2348         (set (make-local-variable local) nil)))))
2349
2350 (defun gnus-summary-clear-local-variables ()
2351   (let ((locals gnus-summary-local-variables))
2352     (while locals
2353       (if (consp (car locals))
2354           (and (vectorp (caar locals))
2355                (set (caar locals) nil))
2356         (and (vectorp (car locals))
2357              (set (car locals) nil)))
2358       (setq locals (cdr locals)))))
2359
2360 ;; Summary data functions.
2361
2362 (defmacro gnus-data-number (data)
2363   `(car ,data))
2364
2365 (defmacro gnus-data-set-number (data number)
2366   `(setcar ,data ,number))
2367
2368 (defmacro gnus-data-mark (data)
2369   `(nth 1 ,data))
2370
2371 (defmacro gnus-data-set-mark (data mark)
2372   `(setcar (nthcdr 1 ,data) ,mark))
2373
2374 (defmacro gnus-data-pos (data)
2375   `(nth 2 ,data))
2376
2377 (defmacro gnus-data-set-pos (data pos)
2378   `(setcar (nthcdr 2 ,data) ,pos))
2379
2380 (defmacro gnus-data-header (data)
2381   `(nth 3 ,data))
2382
2383 (defmacro gnus-data-set-header (data header)
2384   `(setcar (nthcdr 3 ,data) ,header))
2385
2386 (defmacro gnus-data-level (data)
2387   `(nth 4 ,data))
2388
2389 (defmacro gnus-data-unread-p (data)
2390   `(= (nth 1 ,data) gnus-unread-mark))
2391
2392 (defmacro gnus-data-read-p (data)
2393   `(/= (nth 1 ,data) gnus-unread-mark))
2394
2395 (defmacro gnus-data-pseudo-p (data)
2396   `(consp (nth 3 ,data)))
2397
2398 (defmacro gnus-data-find (number)
2399   `(assq ,number gnus-newsgroup-data))
2400
2401 (defmacro gnus-data-find-list (number &optional data)
2402   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2403      (memq (assq ,number bdata)
2404            bdata)))
2405
2406 (defmacro gnus-data-make (number mark pos header level)
2407   `(list ,number ,mark ,pos ,header ,level))
2408
2409 (defun gnus-data-enter (after-article number mark pos header level offset)
2410   (let ((data (gnus-data-find-list after-article)))
2411     (unless data
2412       (error "No such article: %d" after-article))
2413     (setcdr data (cons (gnus-data-make number mark pos header level)
2414                        (cdr data)))
2415     (setq gnus-newsgroup-data-reverse nil)
2416     (gnus-data-update-list (cddr data) offset)))
2417
2418 (defun gnus-data-enter-list (after-article list &optional offset)
2419   (when list
2420     (let ((data (and after-article (gnus-data-find-list after-article)))
2421           (ilist list))
2422       (if (not (or data
2423                    after-article))
2424           (let ((odata gnus-newsgroup-data))
2425             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2426             (when offset
2427               (gnus-data-update-list odata offset)))
2428         ;; Find the last element in the list to be spliced into the main
2429         ;; list.
2430         (while (cdr list)
2431           (setq list (cdr list)))
2432         (if (not data)
2433             (progn
2434               (setcdr list gnus-newsgroup-data)
2435               (setq gnus-newsgroup-data ilist)
2436               (when offset
2437                 (gnus-data-update-list (cdr list) offset)))
2438           (setcdr list (cdr data))
2439           (setcdr data ilist)
2440           (when offset
2441             (gnus-data-update-list (cdr list) offset))))
2442       (setq gnus-newsgroup-data-reverse nil))))
2443
2444 (defun gnus-data-remove (article &optional offset)
2445   (let ((data gnus-newsgroup-data))
2446     (if (= (gnus-data-number (car data)) article)
2447         (progn
2448           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2449                 gnus-newsgroup-data-reverse nil)
2450           (when offset
2451             (gnus-data-update-list gnus-newsgroup-data offset)))
2452       (while (cdr data)
2453         (when (= (gnus-data-number (cadr data)) article)
2454           (setcdr data (cddr data))
2455           (when offset
2456             (gnus-data-update-list (cdr data) offset))
2457           (setq data nil
2458                 gnus-newsgroup-data-reverse nil))
2459         (setq data (cdr data))))))
2460
2461 (defmacro gnus-data-list (backward)
2462   `(if ,backward
2463        (or gnus-newsgroup-data-reverse
2464            (setq gnus-newsgroup-data-reverse
2465                  (reverse gnus-newsgroup-data)))
2466      gnus-newsgroup-data))
2467
2468 (defun gnus-data-update-list (data offset)
2469   "Add OFFSET to the POS of all data entries in DATA."
2470   (setq gnus-newsgroup-data-reverse nil)
2471   (while data
2472     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2473     (setq data (cdr data))))
2474
2475 (defun gnus-summary-article-pseudo-p (article)
2476   "Say whether this article is a pseudo article or not."
2477   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2478
2479 (defmacro gnus-summary-article-sparse-p (article)
2480   "Say whether this article is a sparse article or not."
2481   `(memq ,article gnus-newsgroup-sparse))
2482
2483 (defmacro gnus-summary-article-ancient-p (article)
2484   "Say whether this article is a sparse article or not."
2485   `(memq ,article gnus-newsgroup-ancient))
2486
2487 (defun gnus-article-parent-p (number)
2488   "Say whether this article is a parent or not."
2489   (let ((data (gnus-data-find-list number)))
2490     (and (cdr data)                     ; There has to be an article after...
2491          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2492             (gnus-data-level (nth 1 data))))))
2493
2494 (defun gnus-article-children (number)
2495   "Return a list of all children to NUMBER."
2496   (let* ((data (gnus-data-find-list number))
2497          (level (gnus-data-level (car data)))
2498          children)
2499     (setq data (cdr data))
2500     (while (and data
2501                 (= (gnus-data-level (car data)) (1+ level)))
2502       (push (gnus-data-number (car data)) children)
2503       (setq data (cdr data)))
2504     children))
2505
2506 (defmacro gnus-summary-skip-intangible ()
2507   "If the current article is intangible, then jump to a different article."
2508   '(let ((to (get-text-property (point) 'gnus-intangible)))
2509      (and to (gnus-summary-goto-subject to))))
2510
2511 (defmacro gnus-summary-article-intangible-p ()
2512   "Say whether this article is intangible or not."
2513   '(get-text-property (point) 'gnus-intangible))
2514
2515 (defun gnus-article-read-p (article)
2516   "Say whether ARTICLE is read or not."
2517   (not (or (memq article gnus-newsgroup-marked)
2518            (memq article gnus-newsgroup-unreads)
2519            (memq article gnus-newsgroup-unselected)
2520            (memq article gnus-newsgroup-dormant))))
2521
2522 ;; Some summary mode macros.
2523
2524 (defmacro gnus-summary-article-number ()
2525   "The article number of the article on the current line.
2526 If there isn's an article number here, then we return the current
2527 article number."
2528   '(progn
2529      (gnus-summary-skip-intangible)
2530      (or (get-text-property (point) 'gnus-number)
2531          (gnus-summary-last-subject))))
2532
2533 (defmacro gnus-summary-article-header (&optional number)
2534   "Return the header of article NUMBER."
2535   `(gnus-data-header (gnus-data-find
2536                       ,(or number '(gnus-summary-article-number)))))
2537
2538 (defmacro gnus-summary-thread-level (&optional number)
2539   "Return the level of thread that starts with article NUMBER."
2540   `(if (and (eq gnus-summary-make-false-root 'dummy)
2541             (get-text-property (point) 'gnus-intangible))
2542        0
2543      (gnus-data-level (gnus-data-find
2544                        ,(or number '(gnus-summary-article-number))))))
2545
2546 (defmacro gnus-summary-article-mark (&optional number)
2547   "Return the mark of article NUMBER."
2548   `(gnus-data-mark (gnus-data-find
2549                     ,(or number '(gnus-summary-article-number)))))
2550
2551 (defmacro gnus-summary-article-pos (&optional number)
2552   "Return the position of the line of article NUMBER."
2553   `(gnus-data-pos (gnus-data-find
2554                    ,(or number '(gnus-summary-article-number)))))
2555
2556 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2557 (defmacro gnus-summary-article-subject (&optional number)
2558   "Return current subject string or nil if nothing."
2559   `(let ((headers
2560           ,(if number
2561                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2562              '(gnus-data-header (assq (gnus-summary-article-number)
2563                                       gnus-newsgroup-data)))))
2564      (and headers
2565           (vectorp headers)
2566           (mail-header-subject headers))))
2567
2568 (defmacro gnus-summary-article-score (&optional number)
2569   "Return current article score."
2570   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2571                   gnus-newsgroup-scored))
2572        gnus-summary-default-score 0))
2573
2574 (defun gnus-summary-article-children (&optional number)
2575   "Return a list of article numbers that are children of article NUMBER."
2576   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2577          (level (gnus-data-level (car data)))
2578          l children)
2579     (while (and (setq data (cdr data))
2580                 (> (setq l (gnus-data-level (car data))) level))
2581       (and (= (1+ level) l)
2582            (push (gnus-data-number (car data))
2583                  children)))
2584     (nreverse children)))
2585
2586 (defun gnus-summary-article-parent (&optional number)
2587   "Return the article number of the parent of article NUMBER."
2588   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2589                                     (gnus-data-list t)))
2590          (level (gnus-data-level (car data))))
2591     (if (zerop level)
2592         ()                              ; This is a root.
2593       ;; We search until we find an article with a level less than
2594       ;; this one.  That function has to be the parent.
2595       (while (and (setq data (cdr data))
2596                   (not (< (gnus-data-level (car data)) level))))
2597       (and data (gnus-data-number (car data))))))
2598
2599 (defun gnus-unread-mark-p (mark)
2600   "Say whether MARK is the unread mark."
2601   (= mark gnus-unread-mark))
2602
2603 (defun gnus-read-mark-p (mark)
2604   "Say whether MARK is one of the marks that mark as read.
2605 This is all marks except unread, ticked, dormant, and expirable."
2606   (not (or (= mark gnus-unread-mark)
2607            (= mark gnus-ticked-mark)
2608            (= mark gnus-dormant-mark)
2609            (= mark gnus-expirable-mark))))
2610
2611 (defmacro gnus-article-mark (number)
2612   "Return the MARK of article NUMBER.
2613 This macro should only be used when computing the mark the \"first\"
2614 time; i.e., when generating the summary lines.  After that,
2615 `gnus-summary-article-mark' should be used to examine the
2616 marks of articles."
2617   `(cond
2618     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2619     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2620     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2621     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2622     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2623     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2624     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2625     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2626            gnus-ancient-mark))))
2627
2628 ;; Saving hidden threads.
2629
2630 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2631 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2632
2633 (defmacro gnus-save-hidden-threads (&rest forms)
2634   "Save hidden threads, eval FORMS, and restore the hidden threads."
2635   (let ((config (make-symbol "config")))
2636     `(let ((,config (gnus-hidden-threads-configuration)))
2637        (unwind-protect
2638            (save-excursion
2639              ,@forms)
2640          (gnus-restore-hidden-threads-configuration ,config)))))
2641
2642 (defun gnus-data-compute-positions ()
2643   "Compute the positions of all articles."
2644   (setq gnus-newsgroup-data-reverse nil)
2645   (let ((data gnus-newsgroup-data))
2646     (save-excursion
2647       (gnus-save-hidden-threads
2648         (gnus-summary-show-all-threads)
2649         (goto-char (point-min))
2650         (while data
2651           (while (get-text-property (point) 'gnus-intangible)
2652             (forward-line 1))
2653           (gnus-data-set-pos (car data) (+ (point) 3))
2654           (setq data (cdr data))
2655           (forward-line 1))))))
2656
2657 (defun gnus-hidden-threads-configuration ()
2658   "Return the current hidden threads configuration."
2659   (save-excursion
2660     (let (config)
2661       (goto-char (point-min))
2662       (while (search-forward "\r" nil t)
2663         (push (1- (point)) config))
2664       config)))
2665
2666 (defun gnus-restore-hidden-threads-configuration (config)
2667   "Restore hidden threads configuration from CONFIG."
2668   (save-excursion
2669     (let (point buffer-read-only)
2670       (while (setq point (pop config))
2671         (when (and (< point (point-max))
2672                    (goto-char point)
2673                    (eq (char-after) ?\n))
2674           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2675
2676 ;; Various summary mode internalish functions.
2677
2678 (defun gnus-mouse-pick-article (e)
2679   (interactive "e")
2680   (mouse-set-point e)
2681   (gnus-summary-next-page nil t))
2682
2683 (defun gnus-summary-set-display-table ()
2684   "Change the display table.
2685 Odd characters have a tendency to mess
2686 up nicely formatted displays - we make all possible glyphs
2687 display only a single character."
2688
2689   ;; We start from the standard display table, if any.
2690   (let ((table (or (copy-sequence standard-display-table)
2691                    (make-display-table)))
2692         (i 32))
2693     ;; Nix out all the control chars...
2694     (while (>= (setq i (1- i)) 0)
2695       (aset table i [??]))
2696     ;; ... but not newline and cr, of course.  (cr is necessary for the
2697     ;; selective display).
2698     (aset table ?\n nil)
2699     (aset table ?\r nil)
2700     ;; We keep TAB as well.
2701     (aset table ?\t nil)
2702     ;; We nix out any glyphs over 126 that are not set already.
2703     (let ((i 256))
2704       (while (>= (setq i (1- i)) 127)
2705         ;; Only modify if the entry is nil.
2706         (unless (aref table i)
2707           (aset table i [??]))))
2708     (setq buffer-display-table table)))
2709
2710 (defun gnus-summary-buffer-name (group)
2711   "Return the summary buffer name of GROUP."
2712   (concat "*Summary " group "*"))
2713
2714 (defun gnus-summary-setup-buffer (group)
2715   "Initialize summary buffer."
2716   (let ((buffer (gnus-summary-buffer-name group)))
2717     (if (get-buffer buffer)
2718         (progn
2719           (set-buffer buffer)
2720           (setq gnus-summary-buffer (current-buffer))
2721           (not gnus-newsgroup-prepared))
2722       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2723       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2724       (gnus-summary-mode group)
2725       (when gnus-carpal
2726         (gnus-carpal-setup-buffer 'summary))
2727       (unless gnus-single-article-buffer
2728         (make-local-variable 'gnus-article-buffer)
2729         (make-local-variable 'gnus-article-current)
2730         (make-local-variable 'gnus-original-article-buffer))
2731       (setq gnus-newsgroup-name group)
2732       ;; Set any local variables in the group parameters.
2733       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2734       t)))
2735
2736 (defun gnus-set-global-variables ()
2737   "Set the global equivalents of the buffer-local variables.
2738 They are set to the latest values they had.  These reflect the summary
2739 buffer that was in action when the last article was fetched."
2740   (when (eq major-mode 'gnus-summary-mode)
2741     (setq gnus-summary-buffer (current-buffer))
2742     (let ((name gnus-newsgroup-name)
2743           (marked gnus-newsgroup-marked)
2744           (unread gnus-newsgroup-unreads)
2745           (headers gnus-current-headers)
2746           (data gnus-newsgroup-data)
2747           (summary gnus-summary-buffer)
2748           (article-buffer gnus-article-buffer)
2749           (original gnus-original-article-buffer)
2750           (gac gnus-article-current)
2751           (reffed gnus-reffed-article-number)
2752           (score-file gnus-current-score-file)
2753           (default-charset gnus-newsgroup-charset)
2754           vlist)
2755       (let ((locals gnus-newsgroup-variables))
2756         (while locals
2757           (if (consp (car locals))
2758               (push (eval (caar locals)) vlist)
2759             (push (eval (car locals)) vlist))
2760           (setq locals (cdr locals)))
2761         (setq vlist (nreverse vlist)))
2762       (save-excursion
2763         (set-buffer gnus-group-buffer)
2764         (setq gnus-newsgroup-name name
2765               gnus-newsgroup-marked marked
2766               gnus-newsgroup-unreads unread
2767               gnus-current-headers headers
2768               gnus-newsgroup-data data
2769               gnus-article-current gac
2770               gnus-summary-buffer summary
2771               gnus-article-buffer article-buffer
2772               gnus-original-article-buffer original
2773               gnus-reffed-article-number reffed
2774               gnus-current-score-file score-file
2775               gnus-newsgroup-charset default-charset)
2776         (let ((locals gnus-newsgroup-variables))
2777           (while locals
2778             (if (consp (car locals))
2779                 (set (caar locals) (pop vlist))
2780               (set (car locals) (pop vlist)))
2781             (setq locals (cdr locals))))
2782         ;; The article buffer also has local variables.
2783         (when (gnus-buffer-live-p gnus-article-buffer)
2784           (set-buffer gnus-article-buffer)
2785           (setq gnus-summary-buffer summary))))))
2786
2787 (defun gnus-summary-article-unread-p (article)
2788   "Say whether ARTICLE is unread or not."
2789   (memq article gnus-newsgroup-unreads))
2790
2791 (defun gnus-summary-first-article-p (&optional article)
2792   "Return whether ARTICLE is the first article in the buffer."
2793   (if (not (setq article (or article (gnus-summary-article-number))))
2794       nil
2795     (eq article (caar gnus-newsgroup-data))))
2796
2797 (defun gnus-summary-last-article-p (&optional article)
2798   "Return whether ARTICLE is the last article in the buffer."
2799   (if (not (setq article (or article (gnus-summary-article-number))))
2800       ;; All non-existent numbers are the last article.  :-)
2801       t
2802     (not (cdr (gnus-data-find-list article)))))
2803
2804 (defun gnus-make-thread-indent-array ()
2805   (let ((n 200))
2806     (unless (and gnus-thread-indent-array
2807                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2808       (setq gnus-thread-indent-array (make-vector 201 "")
2809             gnus-thread-indent-array-level gnus-thread-indent-level)
2810       (while (>= n 0)
2811         (aset gnus-thread-indent-array n
2812               (make-string (* n gnus-thread-indent-level) ? ))
2813         (setq n (1- n))))))
2814
2815 (defun gnus-update-summary-mark-positions ()
2816   "Compute where the summary marks are to go."
2817   (save-excursion
2818     (when (gnus-buffer-exists-p gnus-summary-buffer)
2819       (set-buffer gnus-summary-buffer))
2820     (let ((gnus-replied-mark 129)
2821           (gnus-score-below-mark 130)
2822           (gnus-score-over-mark 130)
2823           (gnus-download-mark 131)
2824           (spec gnus-summary-line-format-spec)
2825           gnus-visual pos)
2826       (save-excursion
2827         (gnus-set-work-buffer)
2828         (let ((gnus-summary-line-format-spec spec)
2829               (gnus-newsgroup-downloadable '((0 . t))))
2830           (gnus-summary-insert-line
2831            (make-full-mail-header 0 "" "nobody"
2832                                   "05 Apr 2001 23:33:09 +0400"
2833                                   "" "" 0 0 "" nil)
2834            0 nil 128 t nil "" nil 1)
2835           (goto-char (point-min))
2836           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2837                                              (- (point) 2)))))
2838           (goto-char (point-min))
2839           (push (cons 'replied (and (search-forward "\201" nil t)
2840                                     (- (point) 2)))
2841                 pos)
2842           (goto-char (point-min))
2843           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2844                 pos)
2845           (goto-char (point-min))
2846           (push (cons 'download
2847                       (and (search-forward "\203" nil t) (- (point) 2)))
2848                 pos)))
2849       (setq gnus-summary-mark-positions pos))))
2850
2851 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2852   "Insert a dummy root in the summary buffer."
2853   (beginning-of-line)
2854   (gnus-add-text-properties
2855    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2856    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2857
2858 (defun gnus-summary-from-or-to-or-newsgroups (header)
2859   (let ((to (cdr (assq 'To (mail-header-extra header))))
2860         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2861         (default-mime-charset (with-current-buffer gnus-summary-buffer
2862                                 default-mime-charset)))
2863     (cond
2864      ((and to
2865            gnus-ignored-from-addresses
2866            (string-match gnus-ignored-from-addresses
2867                          (mail-header-from header)))
2868       (concat "-> "
2869               (or (car (funcall gnus-extract-address-components
2870                                 (funcall
2871                                  gnus-decode-encoded-word-function to)))
2872                   (funcall gnus-decode-encoded-word-function to))))
2873      ((and newsgroups
2874            gnus-ignored-from-addresses
2875            (string-match gnus-ignored-from-addresses
2876                          (mail-header-from header)))
2877       (concat "=> " newsgroups))
2878      (t
2879       (or (car (funcall gnus-extract-address-components
2880                         (mail-header-from header)))
2881           (mail-header-from header))))))
2882
2883 (defun gnus-summary-insert-line (gnus-tmp-header
2884                                  gnus-tmp-level gnus-tmp-current
2885                                  gnus-tmp-unread gnus-tmp-replied
2886                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2887                                  &optional gnus-tmp-dummy gnus-tmp-score
2888                                  gnus-tmp-process)
2889   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2890          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2891          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2892          (gnus-tmp-score-char
2893           (if (or (null gnus-summary-default-score)
2894                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2895                       gnus-summary-zcore-fuzz))
2896               ?  ;Whitespace
2897             (if (< gnus-tmp-score gnus-summary-default-score)
2898                 gnus-score-below-mark gnus-score-over-mark)))
2899          (gnus-tmp-replied
2900           (cond (gnus-tmp-process gnus-process-mark)
2901                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2902                  gnus-cached-mark)
2903                 (gnus-tmp-replied gnus-replied-mark)
2904                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2905                  gnus-saved-mark)
2906                 (t gnus-no-mark)))
2907          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2908          (gnus-tmp-name
2909           (cond
2910            ((string-match "<[^>]+> *$" gnus-tmp-from)
2911             (let ((beg (match-beginning 0)))
2912               (or (and (string-match "^\".+\"" gnus-tmp-from)
2913                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2914                   (substring gnus-tmp-from 0 beg))))
2915            ((string-match "(.+)" gnus-tmp-from)
2916             (substring gnus-tmp-from
2917                        (1+ (match-beginning 0)) (1- (match-end 0))))
2918            (t gnus-tmp-from)))
2919          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2920          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2921          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2922          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2923          (buffer-read-only nil))
2924     (when (string= gnus-tmp-name "")
2925       (setq gnus-tmp-name gnus-tmp-from))
2926     (unless (numberp gnus-tmp-lines)
2927       (setq gnus-tmp-lines -1))
2928     (when (= gnus-tmp-lines -1)
2929       (setq gnus-tmp-lines "?"))
2930     (gnus-put-text-property-excluding-characters-with-faces
2931      (point)
2932      (progn (eval gnus-summary-line-format-spec) (point))
2933      'gnus-number gnus-tmp-number)
2934     (when (gnus-visual-p 'summary-highlight 'highlight)
2935       (forward-line -1)
2936       (gnus-run-hooks 'gnus-summary-update-hook)
2937       (forward-line 1))))
2938
2939 (defun gnus-summary-update-line (&optional dont-update)
2940   "Update summary line after change."
2941   (when (and gnus-summary-default-score
2942              (not gnus-summary-inhibit-highlight))
2943     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2944            (article (gnus-summary-article-number))
2945            (score (gnus-summary-article-score article)))
2946       (unless dont-update
2947         (if (and gnus-summary-mark-below
2948                  (< (gnus-summary-article-score)
2949                     gnus-summary-mark-below))
2950             ;; This article has a low score, so we mark it as read.
2951             (when (memq article gnus-newsgroup-unreads)
2952               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2953           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2954             ;; This article was previously marked as read on account
2955             ;; of a low score, but now it has risen, so we mark it as
2956             ;; unread.
2957             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2958         (gnus-summary-update-mark
2959          (if (or (null gnus-summary-default-score)
2960                  (<= (abs (- score gnus-summary-default-score))
2961                      gnus-summary-zcore-fuzz))
2962              ?  ;Whitespace
2963            (if (< score gnus-summary-default-score)
2964                gnus-score-below-mark gnus-score-over-mark))
2965          'score))
2966       ;; Do visual highlighting.
2967       (when (gnus-visual-p 'summary-highlight 'highlight)
2968         (gnus-run-hooks 'gnus-summary-update-hook)))))
2969
2970 (defvar gnus-tmp-new-adopts nil)
2971
2972 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2973   "Return the number of articles in THREAD.
2974 This may be 0 in some cases -- if none of the articles in
2975 the thread are to be displayed."
2976   (let* ((number
2977           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2978           (cond
2979            ((not (listp thread))
2980             1)
2981            ((and (consp thread) (cdr thread))
2982             (apply
2983              '+ 1 (mapcar
2984                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2985            ((null thread)
2986             1)
2987            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2988             1)
2989            (t 0))))
2990     (when (and level (zerop level) gnus-tmp-new-adopts)
2991       (incf number
2992             (apply '+ (mapcar
2993                        'gnus-summary-number-of-articles-in-thread
2994                        gnus-tmp-new-adopts))))
2995     (if char
2996         (if (> number 1) gnus-not-empty-thread-mark
2997           gnus-empty-thread-mark)
2998       number)))
2999
3000 (defun gnus-summary-set-local-parameters (group)
3001   "Go through the local params of GROUP and set all variable specs in that list."
3002   (let ((params (gnus-group-find-parameter group))
3003         elem)
3004     (while params
3005       (setq elem (car params)
3006             params (cdr params))
3007       (and (consp elem)                 ; Has to be a cons.
3008            (consp (cdr elem))           ; The cdr has to be a list.
3009            (symbolp (car elem))         ; Has to be a symbol in there.
3010            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
3011            (ignore-errors               ; So we set it.
3012              (make-local-variable (car elem))
3013              (set (car elem) (eval (nth 1 elem))))))))
3014
3015 (defun gnus-summary-read-group (group &optional show-all no-article
3016                                       kill-buffer no-display backward
3017                                       select-articles)
3018   "Start reading news in newsgroup GROUP.
3019 If SHOW-ALL is non-nil, already read articles are also listed.
3020 If NO-ARTICLE is non-nil, no article is selected initially.
3021 If NO-DISPLAY, don't generate a summary buffer."
3022   (let (result)
3023     (while (and group
3024                 (null (setq result
3025                             (let ((gnus-auto-select-next nil))
3026                               (or (gnus-summary-read-group-1
3027                                    group show-all no-article
3028                                    kill-buffer no-display
3029                                    select-articles)
3030                                   (setq show-all nil
3031                                         select-articles nil)))))
3032                 (eq gnus-auto-select-next 'quietly))
3033       (set-buffer gnus-group-buffer)
3034       ;; The entry function called above goes to the next
3035       ;; group automatically, so we go two groups back
3036       ;; if we are searching for the previous group.
3037       (when backward
3038         (gnus-group-prev-unread-group 2))
3039       (if (not (equal group (gnus-group-group-name)))
3040           (setq group (gnus-group-group-name))
3041         (setq group nil)))
3042     result))
3043
3044 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3045   "Directly jump to the other GROUP from summary buffer.
3046 If SHOW-ALL is non-nil, already read articles are also listed."
3047   (interactive
3048    (if (eq gnus-summary-buffer (current-buffer))
3049        (list (completing-read
3050               "Group: " gnus-active-hashtb nil t
3051               (when (and gnus-newsgroup-name
3052                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3053                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3054               'gnus-group-history)
3055              current-prefix-arg)
3056      (error "%s must be invoked from a gnus summary buffer." this-command)))
3057   (unless (or (zerop (length group))
3058               (and gnus-newsgroup-name
3059                    (string-equal gnus-newsgroup-name group)))
3060     (gnus-summary-exit)
3061     (gnus-summary-read-group group show-all
3062                              gnus-dont-select-after-jump-to-other-group)))
3063
3064 (defun gnus-summary-read-group-1 (group show-all no-article
3065                                         kill-buffer no-display
3066                                         &optional select-articles)
3067   ;; Killed foreign groups can't be entered.
3068   ;;  (when (and (not (gnus-group-native-p group))
3069   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3070   ;;    (error "Dead non-native groups can't be entered"))
3071   (gnus-message 5 "Retrieving newsgroup: %s..."
3072                 (gnus-group-decoded-name group))
3073   (let* ((new-group (gnus-summary-setup-buffer group))
3074          (quit-config (gnus-group-quit-config group))
3075          (did-select (and new-group (gnus-select-newsgroup
3076                                      group show-all select-articles))))
3077     (cond
3078      ;; This summary buffer exists already, so we just select it.
3079      ((not new-group)
3080       (gnus-set-global-variables)
3081       (when kill-buffer
3082         (gnus-kill-or-deaden-summary kill-buffer))
3083       (gnus-configure-windows 'summary 'force)
3084       (gnus-set-mode-line 'summary)
3085       (gnus-summary-position-point)
3086       (message "")
3087       t)
3088      ;; We couldn't select this group.
3089      ((null did-select)
3090       (when (and (eq major-mode 'gnus-summary-mode)
3091                  (not (equal (current-buffer) kill-buffer)))
3092         (kill-buffer (current-buffer))
3093         (if (not quit-config)
3094             (progn
3095               ;; Update the info -- marks might need to be removed,
3096               ;; for instance.
3097               (gnus-summary-update-info)
3098               (set-buffer gnus-group-buffer)
3099               (gnus-group-jump-to-group group)
3100               (gnus-group-next-unread-group 1))
3101           (gnus-handle-ephemeral-exit quit-config)))
3102       (let ((grpinfo (gnus-get-info group)))
3103         (if (null (gnus-info-read grpinfo))
3104             (gnus-message 3 "Group %s contains no messages" 
3105                           (gnus-group-decoded-name group))
3106           (gnus-message 3 "Can't select group")))
3107       nil)
3108      ;; The user did a `C-g' while prompting for number of articles,
3109      ;; so we exit this group.
3110      ((eq did-select 'quit)
3111       (and (eq major-mode 'gnus-summary-mode)
3112            (not (equal (current-buffer) kill-buffer))
3113            (kill-buffer (current-buffer)))
3114       (when kill-buffer
3115         (gnus-kill-or-deaden-summary kill-buffer))
3116       (if (not quit-config)
3117           (progn
3118             (set-buffer gnus-group-buffer)
3119             (gnus-group-jump-to-group group)
3120             (gnus-group-next-unread-group 1)
3121             (gnus-configure-windows 'group 'force))
3122         (gnus-handle-ephemeral-exit quit-config))
3123       ;; Finally signal the quit.
3124       (signal 'quit nil))
3125      ;; The group was successfully selected.
3126      (t
3127       (gnus-set-global-variables)
3128       ;; Save the active value in effect when the group was entered.
3129       (setq gnus-newsgroup-active
3130             (gnus-copy-sequence
3131              (gnus-active gnus-newsgroup-name)))
3132       ;; You can change the summary buffer in some way with this hook.
3133       (gnus-run-hooks 'gnus-select-group-hook)
3134       (gnus-update-format-specifications
3135        nil 'summary 'summary-mode 'summary-dummy)
3136       (gnus-update-summary-mark-positions)
3137       ;; Do score processing.
3138       (when gnus-use-scoring
3139         (gnus-possibly-score-headers))
3140       ;; Check whether to fill in the gaps in the threads.
3141       (when gnus-build-sparse-threads
3142         (gnus-build-sparse-threads))
3143       ;; Find the initial limit.
3144       (if gnus-show-threads
3145           (if show-all
3146               (let ((gnus-newsgroup-dormant nil))
3147                 (gnus-summary-initial-limit show-all))
3148             (gnus-summary-initial-limit show-all))
3149         ;; When untreaded, all articles are always shown.
3150         (setq gnus-newsgroup-limit
3151               (mapcar
3152                (lambda (header) (mail-header-number header))
3153                gnus-newsgroup-headers)))
3154       ;; Generate the summary buffer.
3155       (unless no-display
3156         (gnus-summary-prepare))
3157       (when gnus-use-trees
3158         (gnus-tree-open group)
3159         (setq gnus-summary-highlight-line-function
3160               'gnus-tree-highlight-article))
3161       ;; If the summary buffer is empty, but there are some low-scored
3162       ;; articles or some excluded dormants, we include these in the
3163       ;; buffer.
3164       (when (and (zerop (buffer-size))
3165                  (not no-display))
3166         (cond (gnus-newsgroup-dormant
3167                (gnus-summary-limit-include-dormant))
3168               ((and gnus-newsgroup-scored show-all)
3169                (gnus-summary-limit-include-expunged t))))
3170       ;; Function `gnus-apply-kill-file' must be called in this hook.
3171       (gnus-run-hooks 'gnus-apply-kill-hook)
3172       (if (and (zerop (buffer-size))
3173                (not no-display))
3174           (progn
3175             ;; This newsgroup is empty.
3176             (gnus-summary-catchup-and-exit nil t)
3177             (gnus-message 6 "No unread news")
3178             (when kill-buffer
3179               (gnus-kill-or-deaden-summary kill-buffer))
3180             ;; Return nil from this function.
3181             nil)
3182         ;; Hide conversation thread subtrees.  We cannot do this in
3183         ;; gnus-summary-prepare-hook since kill processing may not
3184         ;; work with hidden articles.
3185         (and gnus-show-threads
3186              gnus-thread-hide-subtree
3187              (gnus-summary-hide-all-threads))
3188         (when kill-buffer
3189           (gnus-kill-or-deaden-summary kill-buffer))
3190         ;; Show first unread article if requested.
3191         (if (and (not no-article)
3192                  (not no-display)
3193                  gnus-newsgroup-unreads
3194                  gnus-auto-select-first)
3195             (progn
3196               (gnus-configure-windows 'summary)
3197               (cond
3198                ((eq gnus-auto-select-first 'best)
3199                 (gnus-summary-best-unread-article))
3200                ((eq gnus-auto-select-first t)
3201                 (gnus-summary-first-unread-article))
3202                ((gnus-functionp gnus-auto-select-first)
3203                 (funcall gnus-auto-select-first))))
3204           ;; Don't select any articles, just move point to the first
3205           ;; article in the group.
3206           (goto-char (point-min))
3207           (gnus-summary-position-point)
3208           (gnus-configure-windows 'summary 'force)
3209           (gnus-set-mode-line 'summary))
3210         (when (get-buffer-window gnus-group-buffer t)
3211           ;; Gotta use windows, because recenter does weird stuff if
3212           ;; the current buffer ain't the displayed window.
3213           (let ((owin (selected-window)))
3214             (select-window (get-buffer-window gnus-group-buffer t))
3215             (when (gnus-group-goto-group group)
3216               (recenter))
3217             (select-window owin)))
3218         ;; Mark this buffer as "prepared".
3219         (setq gnus-newsgroup-prepared t)
3220         (gnus-run-hooks 'gnus-summary-prepared-hook)
3221         t)))))
3222
3223 (defun gnus-summary-prepare ()
3224   "Generate the summary buffer."
3225   (interactive)
3226   (let ((buffer-read-only nil))
3227     (erase-buffer)
3228     (setq gnus-newsgroup-data nil
3229           gnus-newsgroup-data-reverse nil)
3230     (gnus-run-hooks 'gnus-summary-generate-hook)
3231     ;; Generate the buffer, either with threads or without.
3232     (when gnus-newsgroup-headers
3233       (gnus-summary-prepare-threads
3234        (if gnus-show-threads
3235            (gnus-sort-gathered-threads
3236             (funcall gnus-summary-thread-gathering-function
3237                      (gnus-sort-threads
3238                       (gnus-cut-threads (gnus-make-threads)))))
3239          ;; Unthreaded display.
3240          (gnus-sort-articles gnus-newsgroup-headers))))
3241     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3242     ;; Call hooks for modifying summary buffer.
3243     (goto-char (point-min))
3244     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3245
3246 (defsubst gnus-general-simplify-subject (subject)
3247   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3248   (setq subject
3249         (cond
3250          ;; Truncate the subject.
3251          (gnus-simplify-subject-functions
3252           (gnus-map-function gnus-simplify-subject-functions subject))
3253          ((numberp gnus-summary-gather-subject-limit)
3254           (setq subject (gnus-simplify-subject-re subject))
3255           (if (> (length subject) gnus-summary-gather-subject-limit)
3256               (substring subject 0 gnus-summary-gather-subject-limit)
3257             subject))
3258          ;; Fuzzily simplify it.
3259          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3260           (gnus-simplify-subject-fuzzy subject))
3261          ;; Just remove the leading "Re:".
3262          (t
3263           (gnus-simplify-subject-re subject))))
3264
3265   (if (and gnus-summary-gather-exclude-subject
3266            (string-match gnus-summary-gather-exclude-subject subject))
3267       nil                               ; This article shouldn't be gathered
3268     subject))
3269
3270 (defun gnus-summary-simplify-subject-query ()
3271   "Query where the respool algorithm would put this article."
3272   (interactive)
3273   (gnus-summary-select-article)
3274   (message "%s"
3275            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3276
3277 (defun gnus-gather-threads-by-subject (threads)
3278   "Gather threads by looking at Subject headers."
3279   (if (not gnus-summary-make-false-root)
3280       threads
3281     (let ((hashtb (gnus-make-hashtable 1024))
3282           (prev threads)
3283           (result threads)
3284           subject hthread whole-subject)
3285       (while threads
3286         (setq subject (gnus-general-simplify-subject
3287                        (setq whole-subject (mail-header-subject
3288                                             (caar threads)))))
3289         (when subject
3290           (if (setq hthread (gnus-gethash subject hashtb))
3291               (progn
3292                 ;; We enter a dummy root into the thread, if we
3293                 ;; haven't done that already.
3294                 (unless (stringp (caar hthread))
3295                   (setcar hthread (list whole-subject (car hthread))))
3296                 ;; We add this new gathered thread to this gathered
3297                 ;; thread.
3298                 (setcdr (car hthread)
3299                         (nconc (cdar hthread) (list (car threads))))
3300                 ;; Remove it from the list of threads.
3301                 (setcdr prev (cdr threads))
3302                 (setq threads prev))
3303             ;; Enter this thread into the hash table.
3304             (gnus-sethash subject threads hashtb)))
3305         (setq prev threads)
3306         (setq threads (cdr threads)))
3307       result)))
3308
3309 (defun gnus-gather-threads-by-references (threads)
3310   "Gather threads by looking at References headers."
3311   (let ((idhashtb (gnus-make-hashtable 1024))
3312         (thhashtb (gnus-make-hashtable 1024))
3313         (prev threads)
3314         (result threads)
3315         ids references id gthread gid entered ref)
3316     (while threads
3317       (when (setq references (mail-header-references (caar threads)))
3318         (setq id (mail-header-id (caar threads))
3319               ids (gnus-split-references references)
3320               entered nil)
3321         (while (setq ref (pop ids))
3322           (setq ids (delete ref ids))
3323           (if (not (setq gid (gnus-gethash ref idhashtb)))
3324               (progn
3325                 (gnus-sethash ref id idhashtb)
3326                 (gnus-sethash id threads thhashtb))
3327             (setq gthread (gnus-gethash gid thhashtb))
3328             (unless entered
3329               ;; We enter a dummy root into the thread, if we
3330               ;; haven't done that already.
3331               (unless (stringp (caar gthread))
3332                 (setcar gthread (list (mail-header-subject (caar gthread))
3333                                       (car gthread))))
3334               ;; We add this new gathered thread to this gathered
3335               ;; thread.
3336               (setcdr (car gthread)
3337                       (nconc (cdar gthread) (list (car threads)))))
3338             ;; Add it into the thread hash table.
3339             (gnus-sethash id gthread thhashtb)
3340             (setq entered t)
3341             ;; Remove it from the list of threads.
3342             (setcdr prev (cdr threads))
3343             (setq threads prev))))
3344       (setq prev threads)
3345       (setq threads (cdr threads)))
3346     result))
3347
3348 (defun gnus-sort-gathered-threads (threads)
3349   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3350   (let ((result threads))
3351     (while threads
3352       (when (stringp (caar threads))
3353         (setcdr (car threads)
3354                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3355       (setq threads (cdr threads)))
3356     result))
3357
3358 (defun gnus-thread-loop-p (root thread)
3359   "Say whether ROOT is in THREAD."
3360   (let ((stack (list thread))
3361         (infloop 0)
3362         th)
3363     (while (setq thread (pop stack))
3364       (setq th (cdr thread))
3365       (while (and th
3366                   (not (eq (caar th) root)))
3367         (pop th))
3368       (if th
3369           ;; We have found a loop.
3370           (let (ref-dep)
3371             (setcdr thread (delq (car th) (cdr thread)))
3372             (if (boundp (setq ref-dep (intern "none"
3373                                               gnus-newsgroup-dependencies)))
3374                 (setcdr (symbol-value ref-dep)
3375                         (nconc (cdr (symbol-value ref-dep))
3376                                (list (car th))))
3377               (set ref-dep (list nil (car th))))
3378             (setq infloop 1
3379                   stack nil))
3380         ;; Push all the subthreads onto the stack.
3381         (push (cdr thread) stack)))
3382     infloop))
3383
3384 (defun gnus-make-threads ()
3385   "Go through the dependency hashtb and find the roots.  Return all threads."
3386   (let (threads)
3387     (while (catch 'infloop
3388              (mapatoms
3389               (lambda (refs)
3390                 ;; Deal with self-referencing References loops.
3391                 (when (and (car (symbol-value refs))
3392                            (not (zerop
3393                                  (apply
3394                                   '+
3395                                   (mapcar
3396                                    (lambda (thread)
3397                                      (gnus-thread-loop-p
3398                                       (car (symbol-value refs)) thread))
3399                                    (cdr (symbol-value refs)))))))
3400                   (setq threads nil)
3401                   (throw 'infloop t))
3402                 (unless (car (symbol-value refs))
3403                   ;; These threads do not refer back to any other articles,
3404                   ;; so they're roots.
3405                   (setq threads (append (cdr (symbol-value refs)) threads))))
3406               gnus-newsgroup-dependencies)))
3407     threads))
3408
3409 ;; Build the thread tree.
3410 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3411   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3412
3413 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3414 if it was already present.
3415
3416 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3417 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3418 Message-IDs will be renamed be renamed to a unique Message-ID before
3419 being entered.
3420
3421 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3422   (let* ((id (mail-header-id header))
3423          (id-dep (and id (intern id dependencies)))
3424          ref ref-dep ref-header)
3425     ;; Enter this `header' in the `dependencies' table.
3426     (cond
3427      ((not id-dep)
3428       (setq header nil))
3429      ;; The first two cases do the normal part: enter a new `header'
3430      ;; in the `dependencies' table.
3431      ((not (boundp id-dep))
3432       (set id-dep (list header)))
3433      ((null (car (symbol-value id-dep)))
3434       (setcar (symbol-value id-dep) header))
3435
3436      ;; From here the `header' was already present in the
3437      ;; `dependencies' table.
3438      (force-new
3439       ;; Overrides an existing entry;
3440       ;; just set the header part of the entry.
3441       (setcar (symbol-value id-dep) header))
3442
3443      ;; Renames the existing `header' to a unique Message-ID.
3444      ((not gnus-summary-ignore-duplicates)
3445       ;; An article with this Message-ID has already been seen.
3446       ;; We rename the Message-ID.
3447       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3448            (list header))
3449       (mail-header-set-id header id))
3450
3451      ;; The last case ignores an existing entry, except it adds any
3452      ;; additional Xrefs (in case the two articles came from different
3453      ;; servers.
3454      ;; Also sets `header' to `nil' meaning that the `dependencies'
3455      ;; table was *not* modified.
3456      (t
3457       (mail-header-set-xref
3458        (car (symbol-value id-dep))
3459        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3460                    "")
3461                (or (mail-header-xref header) "")))
3462       (setq header nil)))
3463
3464     (when header
3465       ;; First check if that we are not creating a References loop.
3466       (setq ref (gnus-parent-id (mail-header-references header)))
3467       (while (and ref
3468                   (setq ref-dep (intern-soft ref dependencies))
3469                   (boundp ref-dep)
3470                   (setq ref-header (car (symbol-value ref-dep))))
3471         (if (string= id ref)
3472             ;; Yuk!  This is a reference loop.  Make the article be a
3473             ;; root article.
3474             (progn
3475               (mail-header-set-references (car (symbol-value id-dep)) "none")
3476               (setq ref nil))
3477           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3478       (setq ref (gnus-parent-id (mail-header-references header)))
3479       (setq ref-dep (intern (or ref "none") dependencies))
3480       (if (boundp ref-dep)
3481           (setcdr (symbol-value ref-dep)
3482                   (nconc (cdr (symbol-value ref-dep))
3483                          (list (symbol-value id-dep))))
3484         (set ref-dep (list nil (symbol-value id-dep)))))
3485     header))
3486
3487 (defun gnus-build-sparse-threads ()
3488   (let ((headers gnus-newsgroup-headers)
3489         (mail-parse-charset gnus-newsgroup-charset)
3490         (gnus-summary-ignore-duplicates t)
3491         header references generation relations
3492         subject child end new-child date)
3493     ;; First we create an alist of generations/relations, where
3494     ;; generations is how much we trust the relation, and the relation
3495     ;; is parent/child.
3496     (gnus-message 7 "Making sparse threads...")
3497     (save-excursion
3498       (nnheader-set-temp-buffer " *gnus sparse threads*")
3499       (while (setq header (pop headers))
3500         (when (and (setq references (mail-header-references header))
3501                    (not (string= references "")))
3502           (insert references)
3503           (setq child (mail-header-id header)
3504                 subject (mail-header-subject header)
3505                 date (mail-header-date header)
3506                 generation 0)
3507           (while (search-backward ">" nil t)
3508             (setq end (1+ (point)))
3509             (when (search-backward "<" nil t)
3510               (setq new-child (buffer-substring (point) end))
3511               (push (list (incf generation)
3512                           child (setq child new-child)
3513                           subject date)
3514                     relations)))
3515           (when child
3516             (push (list (1+ generation) child nil subject) relations))
3517           (erase-buffer)))
3518       (kill-buffer (current-buffer)))
3519     ;; Sort over trustworthiness.
3520     (mapcar
3521      (lambda (relation)
3522        (when (gnus-dependencies-add-header
3523               (make-full-mail-header-from-decoded-header
3524                gnus-reffed-article-number
3525                (nth 3 relation) "" (or (nth 4 relation) "")
3526                (nth 1 relation)
3527                (or (nth 2 relation) "") 0 0 "")
3528               gnus-newsgroup-dependencies nil)
3529          (push gnus-reffed-article-number gnus-newsgroup-limit)
3530          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3531          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3532                gnus-newsgroup-reads)
3533          (decf gnus-reffed-article-number)))
3534      (sort relations 'car-less-than-car))
3535     (gnus-message 7 "Making sparse threads...done")))
3536
3537 (defun gnus-build-old-threads ()
3538   ;; Look at all the articles that refer back to old articles, and
3539   ;; fetch the headers for the articles that aren't there.  This will
3540   ;; build complete threads - if the roots haven't been expired by the
3541   ;; server, that is.
3542   (let ((mail-parse-charset gnus-newsgroup-charset)
3543         id heads)
3544     (mapatoms
3545      (lambda (refs)
3546        (when (not (car (symbol-value refs)))
3547          (setq heads (cdr (symbol-value refs)))
3548          (while heads
3549            (if (memq (mail-header-number (caar heads))
3550                      gnus-newsgroup-dormant)
3551                (setq heads (cdr heads))
3552              (setq id (symbol-name refs))
3553              (while (and (setq id (gnus-build-get-header id))
3554                          (not (car (gnus-id-to-thread id)))))
3555              (setq heads nil)))))
3556      gnus-newsgroup-dependencies)))
3557
3558 ;; This function has to be called with point after the article number
3559 ;; on the beginning of the line.
3560 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3561   (let ((eol (gnus-point-at-eol))
3562         (buffer (current-buffer))
3563         header)
3564
3565     ;; overview: [num subject from date id refs chars lines misc]
3566     (unwind-protect
3567         (progn
3568           (narrow-to-region (point) eol)
3569           (unless (eobp)
3570             (forward-char))
3571
3572           (setq header
3573                 (make-full-mail-header
3574                  number                         ; number
3575                  (nnheader-nov-field)           ; subject
3576                  (nnheader-nov-field)           ; from
3577                  (nnheader-nov-field)           ; date
3578                  (nnheader-nov-read-message-id) ; id
3579                  (nnheader-nov-field)           ; refs
3580                  (nnheader-nov-read-integer)    ; chars
3581                  (nnheader-nov-read-integer)    ; lines
3582                  (unless (eobp)
3583                    (if (looking-at "Xref: ")
3584                        (goto-char (match-end 0)))
3585                    (nnheader-nov-field))        ; Xref
3586                  (nnheader-nov-parse-extra))))  ; extra
3587
3588       (widen))
3589
3590     (when gnus-alter-header-function
3591       (funcall gnus-alter-header-function header))
3592     (gnus-dependencies-add-header header dependencies force-new)))
3593
3594 (defun gnus-build-get-header (id)
3595   "Look through the buffer of NOV lines and find the header to ID.
3596 Enter this line into the dependencies hash table, and return
3597 the id of the parent article (if any)."
3598   (let ((deps gnus-newsgroup-dependencies)
3599         found header)
3600     (prog1
3601         (save-excursion
3602           (set-buffer nntp-server-buffer)
3603           (let ((case-fold-search nil))
3604             (goto-char (point-min))
3605             (while (and (not found)
3606                         (search-forward id nil t))
3607               (beginning-of-line)
3608               (setq found (looking-at
3609                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3610                                    (regexp-quote id))))
3611               (or found (beginning-of-line 2)))
3612             (when found
3613               (beginning-of-line)
3614               (and
3615                (setq header (gnus-nov-parse-line
3616                              (read (current-buffer)) deps))
3617                (gnus-parent-id (mail-header-references header))))))
3618       (when header
3619         (let ((number (mail-header-number header)))
3620           (push number gnus-newsgroup-limit)
3621           (push header gnus-newsgroup-headers)
3622           (if (memq number gnus-newsgroup-unselected)
3623               (progn
3624                 (push number gnus-newsgroup-unreads)
3625                 (setq gnus-newsgroup-unselected
3626                       (delq number gnus-newsgroup-unselected)))
3627             (push number gnus-newsgroup-ancient)))))))
3628
3629 (defun gnus-build-all-threads ()
3630   "Read all the headers."
3631   (let ((gnus-summary-ignore-duplicates t)
3632         (mail-parse-charset gnus-newsgroup-charset)
3633         (dependencies gnus-newsgroup-dependencies)
3634         header article)
3635     (save-excursion
3636       (set-buffer nntp-server-buffer)
3637       (let ((case-fold-search nil))
3638         (goto-char (point-min))
3639         (while (not (eobp))
3640           (ignore-errors
3641             (setq article (read (current-buffer))
3642                   header (gnus-nov-parse-line article dependencies)))
3643           (when header
3644             (save-excursion
3645               (set-buffer gnus-summary-buffer)
3646               (push header gnus-newsgroup-headers)
3647               (if (memq (setq article (mail-header-number header))
3648                         gnus-newsgroup-unselected)
3649                   (progn
3650                     (push article gnus-newsgroup-unreads)
3651                     (setq gnus-newsgroup-unselected
3652                           (delq article gnus-newsgroup-unselected)))
3653                 (push article gnus-newsgroup-ancient)))
3654             (forward-line 1)))))))
3655
3656 (defun gnus-summary-update-article-line (article header)
3657   "Update the line for ARTICLE using HEADERS."
3658   (let* ((id (mail-header-id header))
3659          (thread (gnus-id-to-thread id)))
3660     (unless thread
3661       (error "Article in no thread"))
3662     ;; Update the thread.
3663     (setcar thread header)
3664     (gnus-summary-goto-subject article)
3665     (let* ((datal (gnus-data-find-list article))
3666            (data (car datal))
3667            (length (when (cdr datal)
3668                      (- (gnus-data-pos data)
3669                         (gnus-data-pos (cadr datal)))))
3670            (buffer-read-only nil)
3671            (level (gnus-summary-thread-level)))
3672       (gnus-delete-line)
3673       (gnus-summary-insert-line
3674        header level nil (gnus-article-mark article)
3675        (memq article gnus-newsgroup-replied)
3676        (memq article gnus-newsgroup-expirable)
3677        ;; Only insert the Subject string when it's different
3678        ;; from the previous Subject string.
3679        (if (and
3680             gnus-show-threads
3681             (gnus-subject-equal
3682              (condition-case ()
3683                  (mail-header-subject
3684                   (gnus-data-header
3685                    (cadr
3686                     (gnus-data-find-list
3687                      article
3688                      (gnus-data-list t)))))
3689                ;; Error on the side of excessive subjects.
3690                (error ""))
3691              (mail-header-subject header)))
3692            ""
3693          (mail-header-subject header))
3694        nil (cdr (assq article gnus-newsgroup-scored))
3695        (memq article gnus-newsgroup-processable))
3696       (when length
3697         (gnus-data-update-list
3698          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3699
3700 (defun gnus-summary-update-article (article &optional iheader)
3701   "Update ARTICLE in the summary buffer."
3702   (set-buffer gnus-summary-buffer)
3703   (let* ((header (gnus-summary-article-header article))
3704          (id (mail-header-id header))
3705          (data (gnus-data-find article))
3706          (thread (gnus-id-to-thread id))
3707          (references (mail-header-references header))
3708          (parent
3709           (gnus-id-to-thread
3710            (or (gnus-parent-id
3711                 (when (and references
3712                            (not (equal "" references)))
3713                   references))
3714                "none")))
3715          (buffer-read-only nil)
3716          (old (car thread)))
3717     (when thread
3718       (unless iheader
3719         (setcar thread nil)
3720         (when parent
3721           (delq thread parent)))
3722       (if (gnus-summary-insert-subject id header)
3723           ;; Set the (possibly) new article number in the data structure.
3724           (gnus-data-set-number data (gnus-id-to-article id))
3725         (setcar thread old)
3726         nil))))
3727
3728 (defun gnus-rebuild-thread (id &optional line)
3729   "Rebuild the thread containing ID.
3730 If LINE, insert the rebuilt thread starting on line LINE."
3731   (let ((buffer-read-only nil)
3732         old-pos current thread data)
3733     (if (not gnus-show-threads)
3734         (setq thread (list (car (gnus-id-to-thread id))))
3735       ;; Get the thread this article is part of.
3736       (setq thread (gnus-remove-thread id)))
3737     (setq old-pos (gnus-point-at-bol))
3738     (setq current (save-excursion
3739                     (and (zerop (forward-line -1))
3740                          (gnus-summary-article-number))))
3741     ;; If this is a gathered thread, we have to go some re-gathering.
3742     (when (stringp (car thread))
3743       (let ((subject (car thread))
3744             roots thr)
3745         (setq thread (cdr thread))
3746         (while thread
3747           (unless (memq (setq thr (gnus-id-to-thread
3748                                    (gnus-root-id
3749                                     (mail-header-id (caar thread)))))
3750                         roots)
3751             (push thr roots))
3752           (setq thread (cdr thread)))
3753         ;; We now have all (unique) roots.
3754         (if (= (length roots) 1)
3755             ;; All the loose roots are now one solid root.
3756             (setq thread (car roots))
3757           (setq thread (cons subject (gnus-sort-threads roots))))))
3758     (let (threads)
3759       ;; We then insert this thread into the summary buffer.
3760       (when line
3761         (goto-char (point-min))
3762         (forward-line (1- line)))
3763       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3764         (if gnus-show-threads
3765             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3766           (gnus-summary-prepare-unthreaded thread))
3767         (setq data (nreverse gnus-newsgroup-data))
3768         (setq threads gnus-newsgroup-threads))
3769       ;; We splice the new data into the data structure.
3770       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3771       ;;!!! then we want to insert at the beginning of the buffer.
3772       ;;!!! That happens to be true with Gnus now, but that may
3773       ;;!!! change in the future.  Perhaps.
3774       (gnus-data-enter-list
3775        (if line nil current) data (- (point) old-pos))
3776       (setq gnus-newsgroup-threads
3777             (nconc threads gnus-newsgroup-threads))
3778       (gnus-data-compute-positions))))
3779
3780 (defun gnus-number-to-header (number)
3781   "Return the header for article NUMBER."
3782   (let ((headers gnus-newsgroup-headers))
3783     (while (and headers
3784                 (not (= number (mail-header-number (car headers)))))
3785       (pop headers))
3786     (when headers
3787       (car headers))))
3788
3789 (defun gnus-parent-headers (in-headers &optional generation)
3790   "Return the headers of the GENERATIONeth parent of HEADERS."
3791   (unless generation
3792     (setq generation 1))
3793   (let ((parent t)
3794         (headers in-headers)
3795         references)
3796     (while (and parent
3797                 (not (zerop generation))
3798                 (setq references (mail-header-references headers)))
3799       (setq headers (if (and references
3800                              (setq parent (gnus-parent-id references)))
3801                         (car (gnus-id-to-thread parent))
3802                       nil))
3803       (decf generation))
3804     (and (not (eq headers in-headers))
3805          headers)))
3806
3807 (defun gnus-id-to-thread (id)
3808   "Return the (sub-)thread where ID appears."
3809   (gnus-gethash id gnus-newsgroup-dependencies))
3810
3811 (defun gnus-id-to-article (id)
3812   "Return the article number of ID."
3813   (let ((thread (gnus-id-to-thread id)))
3814     (when (and thread
3815                (car thread))
3816       (mail-header-number (car thread)))))
3817
3818 (defun gnus-id-to-header (id)
3819   "Return the article headers of ID."
3820   (car (gnus-id-to-thread id)))
3821
3822 (defun gnus-article-displayed-root-p (article)
3823   "Say whether ARTICLE is a root(ish) article."
3824   (let ((level (gnus-summary-thread-level article))
3825         (refs (mail-header-references  (gnus-summary-article-header article)))
3826         particle)
3827     (cond
3828      ((null level) nil)
3829      ((zerop level) t)
3830      ((null refs) t)
3831      ((null (gnus-parent-id refs)) t)
3832      ((and (= 1 level)
3833            (null (setq particle (gnus-id-to-article
3834                                  (gnus-parent-id refs))))
3835            (null (gnus-summary-thread-level particle)))))))
3836
3837 (defun gnus-root-id (id)
3838   "Return the id of the root of the thread where ID appears."
3839   (let (last-id prev)
3840     (while (and id (setq prev (car (gnus-id-to-thread id))))
3841       (setq last-id id
3842             id (gnus-parent-id (mail-header-references prev))))
3843     last-id))
3844
3845 (defun gnus-articles-in-thread (thread)
3846   "Return the list of articles in THREAD."
3847   (cons (mail-header-number (car thread))
3848         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3849
3850 (defun gnus-remove-thread (id &optional dont-remove)
3851   "Remove the thread that has ID in it."
3852   (let (headers thread last-id)
3853     ;; First go up in this thread until we find the root.
3854     (setq last-id (gnus-root-id id)
3855           headers (message-flatten-list (gnus-id-to-thread last-id)))
3856     ;; We have now found the real root of this thread.  It might have
3857     ;; been gathered into some loose thread, so we have to search
3858     ;; through the threads to find the thread we wanted.
3859     (let ((threads gnus-newsgroup-threads)
3860           sub)
3861       (while threads
3862         (setq sub (car threads))
3863         (if (stringp (car sub))
3864             ;; This is a gathered thread, so we look at the roots
3865             ;; below it to find whether this article is in this
3866             ;; gathered root.
3867             (progn
3868               (setq sub (cdr sub))
3869               (while sub
3870                 (when (member (caar sub) headers)
3871                   (setq thread (car threads)
3872                         threads nil
3873                         sub nil))
3874                 (setq sub (cdr sub))))
3875           ;; It's an ordinary thread, so we check it.
3876           (when (eq (car sub) (car headers))
3877             (setq thread sub
3878                   threads nil)))
3879         (setq threads (cdr threads)))
3880       ;; If this article is in no thread, then it's a root.
3881       (if thread
3882           (unless dont-remove
3883             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3884         (setq thread (gnus-id-to-thread last-id)))
3885       (when thread
3886         (prog1
3887             thread                      ; We return this thread.
3888           (unless dont-remove
3889             (if (stringp (car thread))
3890                 (progn
3891                   ;; If we use dummy roots, then we have to remove the
3892                   ;; dummy root as well.
3893                   (when (eq gnus-summary-make-false-root 'dummy)
3894                     ;; We go to the dummy root by going to
3895                     ;; the first sub-"thread", and then one line up.
3896                     (gnus-summary-goto-article
3897                      (mail-header-number (caadr thread)))
3898                     (forward-line -1)
3899                     (gnus-delete-line)
3900                     (gnus-data-compute-positions))
3901                   (setq thread (cdr thread))
3902                   (while thread
3903                     (gnus-remove-thread-1 (car thread))
3904                     (setq thread (cdr thread))))
3905               (gnus-remove-thread-1 thread))))))))
3906
3907 (defun gnus-remove-thread-1 (thread)
3908   "Remove the thread THREAD recursively."
3909   (let ((number (mail-header-number (pop thread)))
3910         d)
3911     (setq thread (reverse thread))
3912     (while thread
3913       (gnus-remove-thread-1 (pop thread)))
3914     (when (setq d (gnus-data-find number))
3915       (goto-char (gnus-data-pos d))
3916       (gnus-summary-show-thread)
3917       (gnus-data-remove
3918        number
3919        (- (gnus-point-at-bol)
3920           (prog1
3921               (1+ (gnus-point-at-eol))
3922             (gnus-delete-line)))))))
3923
3924 (defun gnus-sort-threads-1 (threads func)
3925   (sort (mapcar (lambda (thread)
3926                   (cons (car thread)
3927                         (and (cdr thread)
3928                              (gnus-sort-threads-1 (cdr thread) func))))
3929                 threads) func))
3930
3931 (defun gnus-sort-threads (threads)
3932   "Sort THREADS."
3933   (if (not gnus-thread-sort-functions)
3934       threads
3935     (gnus-message 8 "Sorting threads...")
3936     (prog1
3937         (gnus-sort-threads-1
3938          threads
3939          (gnus-make-sort-function gnus-thread-sort-functions))
3940       (gnus-message 8 "Sorting threads...done"))))
3941
3942 (defun gnus-sort-articles (articles)
3943   "Sort ARTICLES."
3944   (when gnus-article-sort-functions
3945     (gnus-message 7 "Sorting articles...")
3946     (prog1
3947         (setq gnus-newsgroup-headers
3948               (sort articles (gnus-make-sort-function
3949                               gnus-article-sort-functions)))
3950       (gnus-message 7 "Sorting articles...done"))))
3951
3952 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3953 (defmacro gnus-thread-header (thread)
3954   "Return header of first article in THREAD.
3955 Note that THREAD must never, ever be anything else than a variable -
3956 using some other form will lead to serious barfage."
3957   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3958   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3959   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3960         (vector thread) 2))
3961
3962 (defsubst gnus-article-sort-by-number (h1 h2)
3963   "Sort articles by article number."
3964   (< (mail-header-number h1)
3965      (mail-header-number h2)))
3966
3967 (defun gnus-thread-sort-by-number (h1 h2)
3968   "Sort threads by root article number."
3969   (gnus-article-sort-by-number
3970    (gnus-thread-header h1) (gnus-thread-header h2)))
3971
3972 (defsubst gnus-article-sort-by-lines (h1 h2)
3973   "Sort articles by article Lines header."
3974   (< (mail-header-lines h1)
3975      (mail-header-lines h2)))
3976
3977 (defun gnus-thread-sort-by-lines (h1 h2)
3978   "Sort threads by root article Lines header."
3979   (gnus-article-sort-by-lines
3980    (gnus-thread-header h1) (gnus-thread-header h2)))
3981
3982 (defsubst gnus-article-sort-by-chars (h1 h2)
3983   "Sort articles by octet length."
3984   (< (mail-header-chars h1)
3985      (mail-header-chars h2)))
3986
3987 (defun gnus-thread-sort-by-chars (h1 h2)
3988   "Sort threads by root article octet length."
3989   (gnus-article-sort-by-chars
3990    (gnus-thread-header h1) (gnus-thread-header h2)))
3991
3992 (defsubst gnus-article-sort-by-author (h1 h2)
3993   "Sort articles by root author."
3994   (string-lessp
3995    (let ((addr (car (mime-entity-read-field h1 'From))))
3996      (or (std11-full-name-string addr)
3997          (std11-address-string addr)
3998          ""))
3999    (let ((addr (car (mime-entity-read-field h2 'From))))
4000      (or (std11-full-name-string addr)
4001          (std11-address-string addr)
4002          ""))
4003    ))
4004
4005 (defun gnus-thread-sort-by-author (h1 h2)
4006   "Sort threads by root author."
4007   (gnus-article-sort-by-author
4008    (gnus-thread-header h1)  (gnus-thread-header h2)))
4009
4010 (defsubst gnus-article-sort-by-subject (h1 h2)
4011   "Sort articles by root subject."
4012   (string-lessp
4013    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4014    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4015
4016 (defun gnus-thread-sort-by-subject (h1 h2)
4017   "Sort threads by root subject."
4018   (gnus-article-sort-by-subject
4019    (gnus-thread-header h1) (gnus-thread-header h2)))
4020
4021 (defsubst gnus-article-sort-by-date (h1 h2)
4022   "Sort articles by root article date."
4023   (time-less-p
4024    (gnus-date-get-time (mail-header-date h1))
4025    (gnus-date-get-time (mail-header-date h2))))
4026
4027 (defun gnus-thread-sort-by-date (h1 h2)
4028   "Sort threads by root article date."
4029   (gnus-article-sort-by-date
4030    (gnus-thread-header h1) (gnus-thread-header h2)))
4031
4032 (defsubst gnus-article-sort-by-score (h1 h2)
4033   "Sort articles by root article score.
4034 Unscored articles will be counted as having a score of zero."
4035   (> (or (cdr (assq (mail-header-number h1)
4036                     gnus-newsgroup-scored))
4037          gnus-summary-default-score 0)
4038      (or (cdr (assq (mail-header-number h2)
4039                     gnus-newsgroup-scored))
4040          gnus-summary-default-score 0)))
4041
4042 (defun gnus-thread-sort-by-score (h1 h2)
4043   "Sort threads by root article score."
4044   (gnus-article-sort-by-score
4045    (gnus-thread-header h1) (gnus-thread-header h2)))
4046
4047 (defun gnus-thread-sort-by-total-score (h1 h2)
4048   "Sort threads by the sum of all scores in the thread.
4049 Unscored articles will be counted as having a score of zero."
4050   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4051
4052 (defun gnus-thread-total-score (thread)
4053   ;; This function find the total score of THREAD.
4054   (cond ((null thread)
4055          0)
4056         ((consp thread)
4057          (if (stringp (car thread))
4058              (apply gnus-thread-score-function 0
4059                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4060            (gnus-thread-total-score-1 thread)))
4061         (t
4062          (gnus-thread-total-score-1 (list thread)))))
4063
4064 (defun gnus-thread-total-score-1 (root)
4065   ;; This function find the total score of the thread below ROOT.
4066   (setq root (car root))
4067   (apply gnus-thread-score-function
4068          (or (append
4069               (mapcar 'gnus-thread-total-score
4070                       (cdr (gnus-id-to-thread (mail-header-id root))))
4071               (when (> (mail-header-number root) 0)
4072                 (list (or (cdr (assq (mail-header-number root)
4073                                      gnus-newsgroup-scored))
4074                           gnus-summary-default-score 0))))
4075              (list gnus-summary-default-score)
4076              '(0))))
4077
4078 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4079 (defvar gnus-tmp-prev-subject nil)
4080 (defvar gnus-tmp-false-parent nil)
4081 (defvar gnus-tmp-root-expunged nil)
4082 (defvar gnus-tmp-dummy-line nil)
4083
4084 (eval-when-compile (defvar gnus-tmp-header))
4085 (defun gnus-extra-header (type &optional header)
4086   "Return the extra header of TYPE."
4087   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4088       ""))
4089
4090 (defun gnus-summary-prepare-threads (threads)
4091   "Prepare summary buffer from THREADS and indentation LEVEL.
4092 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4093 or a straight list of headers."
4094   (gnus-message 7 "Generating summary...")
4095
4096   (setq gnus-newsgroup-threads threads)
4097   (beginning-of-line)
4098
4099   (let ((gnus-tmp-level 0)
4100         (default-score (or gnus-summary-default-score 0))
4101         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4102         thread number subject stack state gnus-tmp-gathered beg-match
4103         new-roots gnus-tmp-new-adopts thread-end
4104         gnus-tmp-header gnus-tmp-unread
4105         gnus-tmp-replied gnus-tmp-subject-or-nil
4106         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4107         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4108         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4109
4110     (setq gnus-tmp-prev-subject nil)
4111
4112     (if (vectorp (car threads))
4113         ;; If this is a straight (sic) list of headers, then a
4114         ;; threaded summary display isn't required, so we just create
4115         ;; an unthreaded one.
4116         (gnus-summary-prepare-unthreaded threads)
4117
4118       ;; Do the threaded display.
4119
4120       (while (or threads stack gnus-tmp-new-adopts new-roots)
4121
4122         (if (and (= gnus-tmp-level 0)
4123                  (or (not stack)
4124                      (= (caar stack) 0))
4125                  (not gnus-tmp-false-parent)
4126                  (or gnus-tmp-new-adopts new-roots))
4127             (if gnus-tmp-new-adopts
4128                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4129                       thread (list (car gnus-tmp-new-adopts))
4130                       gnus-tmp-header (caar thread)
4131                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4132               (when new-roots
4133                 (setq thread (list (car new-roots))
4134                       gnus-tmp-header (caar thread)
4135                       new-roots (cdr new-roots))))
4136
4137           (if threads
4138               ;; If there are some threads, we do them before the
4139               ;; threads on the stack.
4140               (setq thread threads
4141                     gnus-tmp-header (caar thread))
4142             ;; There were no current threads, so we pop something off
4143             ;; the stack.
4144             (setq state (car stack)
4145                   gnus-tmp-level (car state)
4146                   thread (cdr state)
4147                   stack (cdr stack)
4148                   gnus-tmp-header (caar thread))))
4149
4150         (setq gnus-tmp-false-parent nil)
4151         (setq gnus-tmp-root-expunged nil)
4152         (setq thread-end nil)
4153
4154         (if (stringp gnus-tmp-header)
4155             ;; The header is a dummy root.
4156             (cond
4157              ((eq gnus-summary-make-false-root 'adopt)
4158               ;; We let the first article adopt the rest.
4159               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4160                                                (cddar thread)))
4161               (setq gnus-tmp-gathered
4162                     (nconc (mapcar
4163                             (lambda (h) (mail-header-number (car h)))
4164                             (cddar thread))
4165                            gnus-tmp-gathered))
4166               (setq thread (cons (list (caar thread)
4167                                        (cadar thread))
4168                                  (cdr thread)))
4169               (setq gnus-tmp-level -1
4170                     gnus-tmp-false-parent t))
4171              ((eq gnus-summary-make-false-root 'empty)
4172               ;; We print adopted articles with empty subject fields.
4173               (setq gnus-tmp-gathered
4174                     (nconc (mapcar
4175                             (lambda (h) (mail-header-number (car h)))
4176                             (cddar thread))
4177                            gnus-tmp-gathered))
4178               (setq gnus-tmp-level -1))
4179              ((eq gnus-summary-make-false-root 'dummy)
4180               ;; We remember that we probably want to output a dummy
4181               ;; root.
4182               (setq gnus-tmp-dummy-line gnus-tmp-header)
4183               (setq gnus-tmp-prev-subject gnus-tmp-header))
4184              (t
4185               ;; We do not make a root for the gathered
4186               ;; sub-threads at all.
4187               (setq gnus-tmp-level -1)))
4188
4189           (setq number (mail-header-number gnus-tmp-header)
4190                 subject (mail-header-subject gnus-tmp-header))
4191
4192           (cond
4193            ;; If the thread has changed subject, we might want to make
4194            ;; this subthread into a root.
4195            ((and (null gnus-thread-ignore-subject)
4196                  (not (zerop gnus-tmp-level))
4197                  gnus-tmp-prev-subject
4198                  (not (inline
4199                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4200             (setq new-roots (nconc new-roots (list (car thread)))
4201                   thread-end t
4202                   gnus-tmp-header nil))
4203            ;; If the article lies outside the current limit,
4204            ;; then we do not display it.
4205            ((not (memq number gnus-newsgroup-limit))
4206             (setq gnus-tmp-gathered
4207                   (nconc (mapcar
4208                           (lambda (h) (mail-header-number (car h)))
4209                           (cdar thread))
4210                          gnus-tmp-gathered))
4211             (setq gnus-tmp-new-adopts (if (cdar thread)
4212                                           (append gnus-tmp-new-adopts
4213                                                   (cdar thread))
4214                                         gnus-tmp-new-adopts)
4215                   thread-end t
4216                   gnus-tmp-header nil)
4217             (when (zerop gnus-tmp-level)
4218               (setq gnus-tmp-root-expunged t)))
4219            ;; Perhaps this article is to be marked as read?
4220            ((and gnus-summary-mark-below
4221                  (< (or (cdr (assq number gnus-newsgroup-scored))
4222                         default-score)
4223                     gnus-summary-mark-below)
4224                  ;; Don't touch sparse articles.
4225                  (not (gnus-summary-article-sparse-p number))
4226                  (not (gnus-summary-article-ancient-p number)))
4227             (setq gnus-newsgroup-unreads
4228                   (delq number gnus-newsgroup-unreads))
4229             (if gnus-newsgroup-auto-expire
4230                 (push number gnus-newsgroup-expirable)
4231               (push (cons number gnus-low-score-mark)
4232                     gnus-newsgroup-reads))))
4233
4234           (when gnus-tmp-header
4235             ;; We may have an old dummy line to output before this
4236             ;; article.
4237             (when (and gnus-tmp-dummy-line
4238                        (gnus-subject-equal
4239                         gnus-tmp-dummy-line
4240                         (mail-header-subject gnus-tmp-header)))
4241               (gnus-summary-insert-dummy-line
4242                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4243               (setq gnus-tmp-dummy-line nil))
4244
4245             ;; Compute the mark.
4246             (setq gnus-tmp-unread (gnus-article-mark number))
4247
4248             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4249                                   gnus-tmp-header gnus-tmp-level)
4250                   gnus-newsgroup-data)
4251
4252             ;; Actually insert the line.
4253             (setq
4254              gnus-tmp-subject-or-nil
4255              (cond
4256               ((and gnus-thread-ignore-subject
4257                     gnus-tmp-prev-subject
4258                     (not (inline (gnus-subject-equal
4259                                   gnus-tmp-prev-subject subject))))
4260                subject)
4261               ((zerop gnus-tmp-level)
4262                (if (and (eq gnus-summary-make-false-root 'empty)
4263                         (memq number gnus-tmp-gathered)
4264                         gnus-tmp-prev-subject
4265                         (inline (gnus-subject-equal
4266                                  gnus-tmp-prev-subject subject)))
4267                    gnus-summary-same-subject
4268                  subject))
4269               (t gnus-summary-same-subject)))
4270             (if (and (eq gnus-summary-make-false-root 'adopt)
4271                      (= gnus-tmp-level 1)
4272                      (memq number gnus-tmp-gathered))
4273                 (setq gnus-tmp-opening-bracket ?\<
4274                       gnus-tmp-closing-bracket ?\>)
4275               (setq gnus-tmp-opening-bracket ?\[
4276                     gnus-tmp-closing-bracket ?\]))
4277             (setq
4278              gnus-tmp-indentation
4279              (aref gnus-thread-indent-array gnus-tmp-level)
4280              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4281              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4282                                 gnus-summary-default-score 0)
4283              gnus-tmp-score-char
4284              (if (or (null gnus-summary-default-score)
4285                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4286                          gnus-summary-zcore-fuzz))
4287                  ?  ;Whitespace
4288                (if (< gnus-tmp-score gnus-summary-default-score)
4289                    gnus-score-below-mark gnus-score-over-mark))
4290              gnus-tmp-replied
4291              (cond ((memq number gnus-newsgroup-processable)
4292                     gnus-process-mark)
4293                    ((memq number gnus-newsgroup-cached)
4294                     gnus-cached-mark)
4295                    ((memq number gnus-newsgroup-replied)
4296                     gnus-replied-mark)
4297                    ((memq number gnus-newsgroup-forwarded)
4298                     gnus-forwarded-mark)
4299                    ((memq number gnus-newsgroup-saved)
4300                     gnus-saved-mark)
4301                    (t gnus-no-mark))
4302              gnus-tmp-from (mail-header-from gnus-tmp-header)
4303              gnus-tmp-name
4304              (cond
4305               ((string-match "<[^>]+> *$" gnus-tmp-from)
4306                (setq beg-match (match-beginning 0))
4307                (or (and (string-match "^\".+\"" gnus-tmp-from)
4308                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4309                    (substring gnus-tmp-from 0 beg-match)))
4310               ((string-match "(.+)" gnus-tmp-from)
4311                (substring gnus-tmp-from
4312                           (1+ (match-beginning 0)) (1- (match-end 0))))
4313               (t gnus-tmp-from)))
4314             (when (string= gnus-tmp-name "")
4315               (setq gnus-tmp-name gnus-tmp-from))
4316             (unless (numberp gnus-tmp-lines)
4317               (setq gnus-tmp-lines -1))
4318             (when (= gnus-tmp-lines -1)
4319               (setq gnus-tmp-lines "?"))
4320             (gnus-put-text-property
4321              (point)
4322              (progn (eval gnus-summary-line-format-spec) (point))
4323              'gnus-number number)
4324             (when gnus-visual-p
4325               (forward-line -1)
4326               (gnus-run-hooks 'gnus-summary-update-hook)
4327               (forward-line 1))
4328
4329             (setq gnus-tmp-prev-subject subject)))
4330
4331         (when (nth 1 thread)
4332           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4333         (incf gnus-tmp-level)
4334         (setq threads (if thread-end nil (cdar thread)))
4335         (unless threads
4336           (setq gnus-tmp-level 0)))))
4337   (gnus-message 7 "Generating summary...done"))
4338
4339 (defun gnus-summary-prepare-unthreaded (headers)
4340   "Generate an unthreaded summary buffer based on HEADERS."
4341   (let (header number mark)
4342
4343     (beginning-of-line)
4344
4345     (while headers
4346       ;; We may have to root out some bad articles...
4347       (when (memq (setq number (mail-header-number
4348                                 (setq header (pop headers))))
4349                   gnus-newsgroup-limit)
4350         ;; Mark article as read when it has a low score.
4351         (when (and gnus-summary-mark-below
4352                    (< (or (cdr (assq number gnus-newsgroup-scored))
4353                           gnus-summary-default-score 0)
4354                       gnus-summary-mark-below)
4355                    (not (gnus-summary-article-ancient-p number)))
4356           (setq gnus-newsgroup-unreads
4357                 (delq number gnus-newsgroup-unreads))
4358           (if gnus-newsgroup-auto-expire
4359               (push number gnus-newsgroup-expirable)
4360             (push (cons number gnus-low-score-mark)
4361                   gnus-newsgroup-reads)))
4362
4363         (setq mark (gnus-article-mark number))
4364         (push (gnus-data-make number mark (1+ (point)) header 0)
4365               gnus-newsgroup-data)
4366         (gnus-summary-insert-line
4367          header 0 number
4368          mark (memq number gnus-newsgroup-replied)
4369          (memq number gnus-newsgroup-expirable)
4370          (mail-header-subject header) nil
4371          (cdr (assq number gnus-newsgroup-scored))
4372          (memq number gnus-newsgroup-processable))))))
4373
4374 (defun gnus-summary-remove-list-identifiers ()
4375   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4376   (let ((regexp (if (consp gnus-list-identifiers)
4377                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4378                   gnus-list-identifiers))
4379         changed subject)
4380     (when regexp
4381       (dolist (header gnus-newsgroup-headers)
4382         (setq subject (mail-header-subject header)
4383               changed nil)
4384         (while (string-match
4385                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4386                 subject)
4387           (setq subject
4388                 (concat (substring subject 0 (match-beginning 2))
4389                         (substring subject (match-end 0)))
4390                 changed t))
4391         (when (and changed
4392                    (string-match
4393                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4394           (setq subject
4395                 (concat (substring subject 0 (match-beginning 1))
4396                         (substring subject (match-end 1)))))
4397         (when changed
4398           (mail-header-set-subject header subject))))))
4399
4400 (defun gnus-fetch-headers (articles)
4401   "Fetch headers of ARTICLES."
4402   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4403     (gnus-message 5 "Fetching headers for %s..." name)
4404     (prog1
4405         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4406         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4407             (if (eq 'nov
4408                     (setq gnus-headers-retrieved-by
4409                           (gnus-retrieve-headers
4410                            articles gnus-newsgroup-name
4411                            ;; We might want to fetch old headers, but
4412                            ;; not if there is only 1 article.
4413                            (and (or (and
4414                                      (not (eq gnus-fetch-old-headers 'some))
4415                                      (not (numberp gnus-fetch-old-headers)))
4416                                     (> (length articles) 1))
4417                                 gnus-fetch-old-headers))))
4418                 (gnus-get-newsgroup-headers-xover
4419                  articles nil nil gnus-newsgroup-name t)
4420               (gnus-get-newsgroup-headers))
4421           (gnus-retrieve-parsed-headers
4422            articles gnus-newsgroup-name
4423            ;; We might want to fetch old headers, but
4424            ;; not if there is only 1 article.
4425            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4426                          (not (numberp gnus-fetch-old-headers)))
4427                     (> (length articles) 1))
4428                 gnus-fetch-old-headers)))
4429       (gnus-message 5 "Fetching headers for %s...done" name))))
4430
4431 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4432   "Select newsgroup GROUP.
4433 If READ-ALL is non-nil, all articles in the group are selected.
4434 If SELECT-ARTICLES, only select those articles from GROUP."
4435   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4436          ;;!!! Dirty hack; should be removed.
4437          (gnus-summary-ignore-duplicates
4438           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4439               t
4440             gnus-summary-ignore-duplicates))
4441          (info (nth 2 entry))
4442          articles fetched-articles cached)
4443
4444     (unless (gnus-check-server
4445              (setq gnus-current-select-method
4446                    (gnus-find-method-for-group group)))
4447       (error "Couldn't open server"))
4448
4449     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4450         (gnus-activate-group group)     ; Or we can activate it...
4451         (progn                          ; Or we bug out.
4452           (when (equal major-mode 'gnus-summary-mode)
4453             (kill-buffer (current-buffer)))
4454           (error "Couldn't activate group %s: %s"
4455                  group (gnus-status-message group))))
4456
4457     (unless (gnus-request-group group t)
4458       (when (equal major-mode 'gnus-summary-mode)
4459         (kill-buffer (current-buffer)))
4460       (error "Couldn't request group %s: %s"
4461              group (gnus-status-message group)))
4462
4463     (setq gnus-newsgroup-name group)
4464     (setq gnus-newsgroup-unselected nil)
4465     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4466     (gnus-summary-setup-default-charset)
4467
4468     ;; Adjust and set lists of article marks.
4469     (when info
4470       (gnus-adjust-marked-articles info))
4471
4472     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4473     (setq cached
4474           (if (gnus-virtual-group-p group)
4475               gnus-newsgroup-cached
4476             (gnus-cache-articles-in-group group)))
4477
4478     (setq gnus-newsgroup-unreads
4479           (gnus-set-difference
4480            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4481            gnus-newsgroup-dormant))
4482
4483     (setq gnus-newsgroup-processable nil)
4484
4485     (gnus-update-read-articles group gnus-newsgroup-unreads)
4486
4487     (if (setq articles select-articles)
4488         (setq gnus-newsgroup-unselected
4489               (gnus-sorted-intersection
4490                gnus-newsgroup-unreads
4491                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4492       (setq articles (gnus-articles-to-read group read-all)))
4493
4494     (cond
4495      ((null articles)
4496       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4497       'quit)
4498      ((eq articles 0) nil)
4499      (t
4500       ;; Init the dependencies hash table.
4501       (setq gnus-newsgroup-dependencies
4502             (gnus-make-hashtable (length articles)))
4503       (gnus-set-global-variables)
4504       ;; Retrieve the headers and read them in.
4505       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4506
4507       ;; Suppress duplicates?
4508       (when gnus-suppress-duplicates
4509         (gnus-dup-suppress-articles))
4510
4511       ;; Set the initial limit.
4512       (setq gnus-newsgroup-limit (copy-sequence articles))
4513       ;; Remove canceled articles from the list of unread articles.
4514       (setq gnus-newsgroup-unreads
4515             (gnus-set-sorted-intersection
4516              gnus-newsgroup-unreads
4517              (setq fetched-articles
4518                    (mapcar (lambda (headers) (mail-header-number headers))
4519                            gnus-newsgroup-headers))))
4520       ;; Removed marked articles that do not exist.
4521       (gnus-update-missing-marks
4522        (gnus-sorted-complement fetched-articles articles))
4523
4524       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4525       (when cached
4526         (setq gnus-newsgroup-cached cached))
4527
4528       ;; We might want to build some more threads first.
4529       (when (and gnus-fetch-old-headers
4530                  (eq gnus-headers-retrieved-by 'nov))
4531         (if (eq gnus-fetch-old-headers 'invisible)
4532             (gnus-build-all-threads)
4533           (gnus-build-old-threads)))
4534       ;; Let the Gnus agent mark articles as read.
4535       (when gnus-agent
4536         (gnus-agent-get-undownloaded-list))
4537       ;; Remove list identifiers from subject
4538       (when gnus-list-identifiers
4539         (gnus-summary-remove-list-identifiers))
4540       ;; Check whether auto-expire is to be done in this group.
4541       (setq gnus-newsgroup-auto-expire
4542             (gnus-group-auto-expirable-p group))
4543       ;; Set up the article buffer now, if necessary.
4544       (unless gnus-single-article-buffer
4545         (gnus-article-setup-buffer))
4546       ;; First and last article in this newsgroup.
4547       (when gnus-newsgroup-headers
4548         (setq gnus-newsgroup-begin
4549               (mail-header-number (car gnus-newsgroup-headers))
4550               gnus-newsgroup-end
4551               (mail-header-number
4552                (gnus-last-element gnus-newsgroup-headers))))
4553       ;; GROUP is successfully selected.
4554       (or gnus-newsgroup-headers t)))))
4555
4556 (defun gnus-articles-to-read (group &optional read-all)
4557   "Find out what articles the user wants to read."
4558   (let* ((articles
4559           ;; Select all articles if `read-all' is non-nil, or if there
4560           ;; are no unread articles.
4561           (if (or read-all
4562                   (and (zerop (length gnus-newsgroup-marked))
4563                        (zerop (length gnus-newsgroup-unreads)))
4564                   (eq (gnus-group-find-parameter group 'display)
4565                       'all))
4566               (or
4567                (gnus-uncompress-range (gnus-active group))
4568                (gnus-cache-articles-in-group group))
4569             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4570                           (copy-sequence gnus-newsgroup-unreads))
4571                   '<)))
4572          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4573          (scored (length scored-list))
4574          (number (length articles))
4575          (marked (+ (length gnus-newsgroup-marked)
4576                     (length gnus-newsgroup-dormant)))
4577          (select
4578           (cond
4579            ((numberp read-all)
4580             read-all)
4581            (t
4582             (condition-case ()
4583                 (cond
4584                  ((and (or (<= scored marked) (= scored number))
4585                        (natnump gnus-large-newsgroup)
4586                        (> number gnus-large-newsgroup))
4587                   (let* ((cursor-in-echo-area nil)
4588                          (input (read-from-minibuffer
4589                                  (format
4590                                   "How many articles from %s (max %d): "
4591                                   (gnus-limit-string gnus-newsgroup-name 35)
4592                                   number)
4593                                  (cons (number-to-string gnus-large-newsgroup)
4594                                        0))))
4595                     (if (string-match "^[ \t]*$" input)
4596                         number
4597                       input)))
4598                  ((and (> scored marked) (< scored number)
4599                        (> (- scored number) 20))
4600                   (let ((input
4601                          (read-string
4602                           (format "%s %s (%d scored, %d total): "
4603                                   "How many articles from"
4604                                   group scored number))))
4605                     (if (string-match "^[ \t]*$" input)
4606                         number input)))
4607                  (t number))
4608               (quit
4609                (message "Quit getting the articles to read")
4610                nil))))))
4611     (setq select (if (stringp select) (string-to-number select) select))
4612     (if (or (null select) (zerop select))
4613         select
4614       (if (and (not (zerop scored)) (<= (abs select) scored))
4615           (progn
4616             (setq articles (sort scored-list '<))
4617             (setq number (length articles)))
4618         (setq articles (copy-sequence articles)))
4619
4620       (when (< (abs select) number)
4621         (if (< select 0)
4622             ;; Select the N oldest articles.
4623             (setcdr (nthcdr (1- (abs select)) articles) nil)
4624           ;; Select the N most recent articles.
4625           (setq articles (nthcdr (- number select) articles))))
4626       (setq gnus-newsgroup-unselected
4627             (gnus-sorted-intersection
4628              gnus-newsgroup-unreads
4629              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4630       (when gnus-alter-articles-to-read-function
4631         (setq gnus-newsgroup-unreads
4632               (sort
4633                (funcall gnus-alter-articles-to-read-function
4634                         gnus-newsgroup-name gnus-newsgroup-unreads)
4635                '<)))
4636       articles)))
4637
4638 (defun gnus-killed-articles (killed articles)
4639   (let (out)
4640     (while articles
4641       (when (inline (gnus-member-of-range (car articles) killed))
4642         (push (car articles) out))
4643       (setq articles (cdr articles)))
4644     out))
4645
4646 (defun gnus-uncompress-marks (marks)
4647   "Uncompress the mark ranges in MARKS."
4648   (let ((uncompressed '(score bookmark))
4649         out)
4650     (while marks
4651       (if (memq (caar marks) uncompressed)
4652           (push (car marks) out)
4653         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4654       (setq marks (cdr marks)))
4655     out))
4656
4657 (defun gnus-adjust-marked-articles (info)
4658   "Set all article lists and remove all marks that are no longer valid."
4659   (let* ((marked-lists (gnus-info-marks info))
4660          (active (gnus-active (gnus-info-group info)))
4661          (min (car active))
4662          (max (cdr active))
4663          (types gnus-article-mark-lists)
4664          (uncompressed '(score bookmark killed))
4665          marks var articles article mark)
4666
4667     (while marked-lists
4668       (setq marks (pop marked-lists))
4669       (set (setq var (intern (format "gnus-newsgroup-%s"
4670                                      (car (rassq (setq mark (car marks))
4671                                                  types)))))
4672            (if (memq (car marks) uncompressed) (cdr marks)
4673              (gnus-uncompress-range (cdr marks))))
4674
4675       (setq articles (symbol-value var))
4676
4677       ;; All articles have to be subsets of the active articles.
4678       (cond
4679        ;; Adjust "simple" lists.
4680        ((memq mark '(tick dormant expire reply save))
4681         (while articles
4682           (when (or (< (setq article (pop articles)) min) (> article max))
4683             (set var (delq article (symbol-value var))))))
4684        ;; Adjust assocs.
4685        ((memq mark uncompressed)
4686         (when (not (listp (cdr (symbol-value var))))
4687           (set var (list (symbol-value var))))
4688         (when (not (listp (cdr articles)))
4689           (setq articles (list articles)))
4690         (while articles
4691           (when (or (not (consp (setq article (pop articles))))
4692                     (< (car article) min)
4693                     (> (car article) max))
4694             (set var (delq article (symbol-value var))))))))))
4695
4696 (defun gnus-update-missing-marks (missing)
4697   "Go through the list of MISSING articles and remove them from the mark lists."
4698   (when missing
4699     (let ((types gnus-article-mark-lists)
4700           var m)
4701       ;; Go through all types.
4702       (while types
4703         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4704         (when (symbol-value var)
4705           ;; This list has articles.  So we delete all missing articles
4706           ;; from it.
4707           (setq m missing)
4708           (while m
4709             (set var (delq (pop m) (symbol-value var)))))))))
4710
4711 (defun gnus-update-marks ()
4712   "Enter the various lists of marked articles into the newsgroup info list."
4713   (let ((types gnus-article-mark-lists)
4714         (info (gnus-get-info gnus-newsgroup-name))
4715         (uncompressed '(score bookmark killed))
4716         type list newmarked symbol delta-marks)
4717     (when info
4718       ;; Add all marks lists to the list of marks lists.
4719       (while (setq type (pop types))
4720         (setq list (symbol-value
4721                     (setq symbol
4722                           (intern (format "gnus-newsgroup-%s"
4723                                           (car type))))))
4724
4725         (when list
4726           ;; Get rid of the entries of the articles that have the
4727           ;; default score.
4728           (when (and (eq (cdr type) 'score)
4729                      gnus-save-score
4730                      list)
4731             (let* ((arts list)
4732                    (prev (cons nil list))
4733                    (all prev))
4734               (while arts
4735                 (if (or (not (consp (car arts)))
4736                         (= (cdar arts) gnus-summary-default-score))
4737                     (setcdr prev (cdr arts))
4738                   (setq prev arts))
4739                 (setq arts (cdr arts)))
4740               (setq list (cdr all)))))
4741
4742         (unless (memq (cdr type) uncompressed)
4743           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4744
4745         (when (gnus-check-backend-function
4746                'request-set-mark gnus-newsgroup-name)
4747           ;; propagate flags to server, with the following exceptions:
4748           ;; uncompressed:s are not proper flags (they are cons cells)
4749           ;; cache is a internal gnus flag
4750           ;; download are local to one gnus installation (well)
4751           ;; unsend are for nndraft groups only
4752           ;; xxx: generality of this?  this suits nnimap anyway
4753           (unless (memq (cdr type) (append '(cache download unsend)
4754                                            uncompressed))
4755             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4756                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4757                    (add (gnus-remove-from-range
4758                          (gnus-copy-sequence list) old)))
4759               (when add
4760                 (push (list add 'add (list (cdr type))) delta-marks))
4761               (when del
4762                 (push (list del 'del (list (cdr type))) delta-marks)))))
4763
4764         (when list
4765           (push (cons (cdr type) list) newmarked)))
4766
4767       (when delta-marks
4768         (unless (gnus-check-group gnus-newsgroup-name)
4769           (error "Can't open server for %s" gnus-newsgroup-name))
4770         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4771
4772       ;; Enter these new marks into the info of the group.
4773       (if (nthcdr 3 info)
4774           (setcar (nthcdr 3 info) newmarked)
4775         ;; Add the marks lists to the end of the info.
4776         (when newmarked
4777           (setcdr (nthcdr 2 info) (list newmarked))))
4778
4779       ;; Cut off the end of the info if there's nothing else there.
4780       (let ((i 5))
4781         (while (and (> i 2)
4782                     (not (nth i info)))
4783           (when (nthcdr (decf i) info)
4784             (setcdr (nthcdr i info) nil)))))))
4785
4786 (defun gnus-set-mode-line (where)
4787   "Set the mode line of the article or summary buffers.
4788 If WHERE is `summary', the summary mode line format will be used."
4789   ;; Is this mode line one we keep updated?
4790   (when (and (memq where gnus-updated-mode-lines)
4791              (symbol-value
4792               (intern (format "gnus-%s-mode-line-format-spec" where))))
4793     (let (mode-string)
4794       (save-excursion
4795         ;; We evaluate this in the summary buffer since these
4796         ;; variables are buffer-local to that buffer.
4797         (set-buffer gnus-summary-buffer)
4798         ;; We bind all these variables that are used in the `eval' form
4799         ;; below.
4800         (let* ((mformat (symbol-value
4801                          (intern
4802                           (format "gnus-%s-mode-line-format-spec" where))))
4803                (gnus-tmp-group-name (gnus-group-decoded-name 
4804                                      gnus-newsgroup-name))
4805                (gnus-tmp-article-number (or gnus-current-article 0))
4806                (gnus-tmp-unread gnus-newsgroup-unreads)
4807                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4808                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4809                (gnus-tmp-unread-and-unselected
4810                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4811                             (zerop gnus-tmp-unselected))
4812                        "")
4813                       ((zerop gnus-tmp-unselected)
4814                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4815                       (t (format "{%d(+%d) more}"
4816                                  gnus-tmp-unread-and-unticked
4817                                  gnus-tmp-unselected))))
4818                (gnus-tmp-subject
4819                 (if (and gnus-current-headers
4820                          (vectorp gnus-current-headers))
4821                     (gnus-mode-string-quote
4822                      (mail-header-subject gnus-current-headers))
4823                   ""))
4824                bufname-length max-len
4825                gnus-tmp-header);; passed as argument to any user-format-funcs
4826           (setq mode-string (eval mformat))
4827           (setq bufname-length (if (string-match "%b" mode-string)
4828                                    (- (length
4829                                        (buffer-name
4830                                         (if (eq where 'summary)
4831                                             nil
4832                                           (get-buffer gnus-article-buffer))))
4833                                       2)
4834                                  0))
4835           (setq max-len (max 4 (if gnus-mode-non-string-length
4836                                    (- (window-width)
4837                                       gnus-mode-non-string-length
4838                                       bufname-length)
4839                                  (length mode-string))))
4840           ;; We might have to chop a bit of the string off...
4841           (when (> (length mode-string) max-len)
4842             (setq mode-string
4843                   (concat (gnus-truncate-string mode-string (- max-len 3))
4844                           "...")))
4845           ;; Pad the mode string a bit.
4846           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4847       ;; Update the mode line.
4848       (setq mode-line-buffer-identification
4849             (gnus-mode-line-buffer-identification (list mode-string)))
4850       (set-buffer-modified-p t))))
4851
4852 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4853   "Go through the HEADERS list and add all Xrefs to a hash table.
4854 The resulting hash table is returned, or nil if no Xrefs were found."
4855   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4856          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4857          (xref-hashtb (gnus-make-hashtable))
4858          start group entry number xrefs header)
4859     (while headers
4860       (setq header (pop headers))
4861       (when (and (setq xrefs (mail-header-xref header))
4862                  (not (memq (setq number (mail-header-number header))
4863                             unreads)))
4864         (setq start 0)
4865         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4866           (setq start (match-end 0))
4867           (setq group (if prefix
4868                           (concat prefix (substring xrefs (match-beginning 1)
4869                                                     (match-end 1)))
4870                         (substring xrefs (match-beginning 1) (match-end 1))))
4871           (setq number
4872                 (string-to-int (substring xrefs (match-beginning 2)
4873                                           (match-end 2))))
4874           (if (setq entry (gnus-gethash group xref-hashtb))
4875               (setcdr entry (cons number (cdr entry)))
4876             (gnus-sethash group (cons number nil) xref-hashtb)))))
4877     (and start xref-hashtb)))
4878
4879 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4880   "Look through all the headers and mark the Xrefs as read."
4881   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4882         name entry info xref-hashtb idlist method nth4)
4883     (save-excursion
4884       (set-buffer gnus-group-buffer)
4885       (when (setq xref-hashtb
4886                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4887         (mapatoms
4888          (lambda (group)
4889            (unless (string= from-newsgroup (setq name (symbol-name group)))
4890              (setq idlist (symbol-value group))
4891              ;; Dead groups are not updated.
4892              (and (prog1
4893                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4894                             info (nth 2 entry))
4895                     (when (stringp (setq nth4 (gnus-info-method info)))
4896                       (setq nth4 (gnus-server-to-method nth4))))
4897                   ;; Only do the xrefs if the group has the same
4898                   ;; select method as the group we have just read.
4899                   (or (gnus-methods-equal-p
4900                        nth4 (gnus-find-method-for-group from-newsgroup))
4901                       virtual
4902                       (equal nth4 (setq method (gnus-find-method-for-group
4903                                                 from-newsgroup)))
4904                       (and (equal (car nth4) (car method))
4905                            (equal (nth 1 nth4) (nth 1 method))))
4906                   gnus-use-cross-reference
4907                   (or (not (eq gnus-use-cross-reference t))
4908                       virtual
4909                       ;; Only do cross-references on subscribed
4910                       ;; groups, if that is what is wanted.
4911                       (<= (gnus-info-level info) gnus-level-subscribed))
4912                   (gnus-group-make-articles-read name idlist))))
4913          xref-hashtb)))))
4914
4915 (defun gnus-compute-read-articles (group articles)
4916   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4917          (info (nth 2 entry))
4918          (active (gnus-active group))
4919          ninfo)
4920     (when entry
4921       ;; First peel off all invalid article numbers.
4922       (when active
4923         (let ((ids articles)
4924               id first)
4925           (while (setq id (pop ids))
4926             (when (and first (> id (cdr active)))
4927               ;; We'll end up in this situation in one particular
4928               ;; obscure situation.  If you re-scan a group and get
4929               ;; a new article that is cross-posted to a different
4930               ;; group that has not been re-scanned, you might get
4931               ;; crossposted article that has a higher number than
4932               ;; Gnus believes possible.  So we re-activate this
4933               ;; group as well.  This might mean doing the
4934               ;; crossposting thingy will *increase* the number
4935               ;; of articles in some groups.  Tsk, tsk.
4936               (setq active (or (gnus-activate-group group) active)))
4937             (when (or (> id (cdr active))
4938                       (< id (car active)))
4939               (setq articles (delq id articles))))))
4940       ;; If the read list is nil, we init it.
4941       (if (and active
4942                (null (gnus-info-read info))
4943                (> (car active) 1))
4944           (setq ninfo (cons 1 (1- (car active))))
4945         (setq ninfo (gnus-info-read info)))
4946       ;; Then we add the read articles to the range.
4947       (gnus-add-to-range
4948        ninfo (setq articles (sort articles '<))))))
4949
4950 (defun gnus-group-make-articles-read (group articles)
4951   "Update the info of GROUP to say that ARTICLES are read."
4952   (let* ((num 0)
4953          (entry (gnus-gethash group gnus-newsrc-hashtb))
4954          (info (nth 2 entry))
4955          (active (gnus-active group))
4956          range)
4957     (when entry
4958       (setq range (gnus-compute-read-articles group articles))
4959       (save-excursion
4960         (set-buffer gnus-group-buffer)
4961         (gnus-undo-register
4962           `(progn
4963              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4964              (gnus-info-set-read ',info ',(gnus-info-read info))
4965              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4966              (gnus-group-update-group ,group t))))
4967       ;; Add the read articles to the range.
4968       (gnus-info-set-read info range)
4969       ;; Then we have to re-compute how many unread
4970       ;; articles there are in this group.
4971       (when active
4972         (cond
4973          ((not range)
4974           (setq num (- (1+ (cdr active)) (car active))))
4975          ((not (listp (cdr range)))
4976           (setq num (- (cdr active) (- (1+ (cdr range))
4977                                        (car range)))))
4978          (t
4979           (while range
4980             (if (numberp (car range))
4981                 (setq num (1+ num))
4982               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4983             (setq range (cdr range)))
4984           (setq num (- (cdr active) num))))
4985         ;; Update the number of unread articles.
4986         (setcar entry num)
4987         ;; Update the group buffer.
4988         (gnus-group-update-group group t)))))
4989
4990 (defvar gnus-newsgroup-none-id 0)
4991
4992 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4993   (let ((cur nntp-server-buffer)
4994         (dependencies
4995          (or dependencies
4996              (save-excursion (set-buffer gnus-summary-buffer)
4997                              gnus-newsgroup-dependencies)))
4998         headers id end ref
4999         (mail-parse-charset gnus-newsgroup-charset)
5000         (mail-parse-ignored-charsets
5001          (save-excursion (condition-case nil
5002                              (set-buffer gnus-summary-buffer)
5003                            (error))
5004                          gnus-newsgroup-ignored-charsets)))
5005     (save-excursion
5006       (set-buffer nntp-server-buffer)
5007       ;; Translate all TAB characters into SPACE characters.
5008       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5009       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5010       (gnus-run-hooks 'gnus-parse-headers-hook)
5011       (let ((case-fold-search t)
5012             in-reply-to header p lines chars ctype)
5013         (goto-char (point-min))
5014         ;; Search to the beginning of the next header.  Error messages
5015         ;; do not begin with 2 or 3.
5016         (while (re-search-forward "^[23][0-9]+ " nil t)
5017           (setq id nil
5018                 ref nil)
5019           ;; This implementation of this function, with nine
5020           ;; search-forwards instead of the one re-search-forward and
5021           ;; a case (which basically was the old function) is actually
5022           ;; about twice as fast, even though it looks messier.  You
5023           ;; can't have everything, I guess.  Speed and elegance
5024           ;; doesn't always go hand in hand.
5025           (setq
5026            header
5027            (make-full-mail-header
5028             ;; Number.
5029             (prog1
5030                 (read cur)
5031               (end-of-line)
5032               (setq p (point))
5033               (narrow-to-region (point)
5034                                 (or (and (search-forward "\n.\n" nil t)
5035                                          (- (point) 2))
5036                                     (point))))
5037             ;; Subject.
5038             (progn
5039               (goto-char p)
5040               (if (search-forward "\nsubject: " nil t)
5041                   (nnheader-header-value)
5042                 "(none)"))
5043             ;; From.
5044             (progn
5045               (goto-char p)
5046               (if (or (search-forward "\nfrom: " nil t)
5047                       (search-forward "\nfrom:" nil t))
5048                   (nnheader-header-value)
5049                 "(nobody)"))
5050             ;; Date.
5051             (progn
5052               (goto-char p)
5053               (if (search-forward "\ndate: " nil t)
5054                   (nnheader-header-value)
5055                 ""))
5056             ;; Message-ID.
5057             (progn
5058               (goto-char p)
5059               (setq id (if (re-search-forward
5060                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5061                            ;; We do it this way to make sure the Message-ID
5062                            ;; is (somewhat) syntactically valid.
5063                            (buffer-substring (match-beginning 1)
5064                                              (match-end 1))
5065                          ;; If there was no message-id, we just fake one
5066                          ;; to make subsequent routines simpler.
5067                          (nnheader-generate-fake-message-id))))
5068             ;; References.
5069             (progn
5070               (goto-char p)
5071               (if (search-forward "\nreferences: " nil t)
5072                   (progn
5073                     (setq end (point))
5074                     (prog1
5075                         (nnheader-header-value)
5076                       (setq ref
5077                             (buffer-substring
5078                              (progn
5079                                ;; (end-of-line)
5080                                (search-backward ">" end t)
5081                                (1+ (point)))
5082                              (progn
5083                                (search-backward "<" end t)
5084                                (point))))))
5085                 ;; Get the references from the in-reply-to header if there
5086                 ;; were no references and the in-reply-to header looks
5087                 ;; promising.
5088                 (if (and (search-forward "\nin-reply-to: " nil t)
5089                          (setq in-reply-to (nnheader-header-value))
5090                          (string-match "<[^>]+>" in-reply-to))
5091                     (let (ref2)
5092                       (setq ref (substring in-reply-to (match-beginning 0)
5093                                            (match-end 0)))
5094                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5095                         (setq ref2 (substring in-reply-to (match-beginning 0)
5096                                               (match-end 0)))
5097                         (when (> (length ref2) (length ref))
5098                           (setq ref ref2)))
5099                       ref)
5100                   (setq ref nil))))
5101             ;; Chars.
5102             (progn
5103               (goto-char p)
5104               (if (search-forward "\nchars: " nil t)
5105                   (if (numberp (setq chars (ignore-errors (read cur))))
5106                       chars -1)
5107                 -1))
5108             ;; Lines.
5109             (progn
5110               (goto-char p)
5111               (if (search-forward "\nlines: " nil t)
5112                   (if (numberp (setq lines (ignore-errors (read cur))))
5113                       lines -1)
5114                 -1))
5115             ;; Xref.
5116             (progn
5117               (goto-char p)
5118               (and (search-forward "\nxref: " nil t)
5119                    (nnheader-header-value)))
5120             ;; Extra.
5121             (when gnus-extra-headers
5122               (let ((extra gnus-extra-headers)
5123                     out)
5124                 (while extra
5125                   (goto-char p)
5126                   (when (search-forward
5127                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5128                     (push (cons (car extra) (nnheader-header-value)) out))
5129                   (pop extra))
5130                 out))))
5131           (goto-char p)
5132           (if (and (search-forward "\ncontent-type: " nil t)
5133                    (setq ctype (nnheader-header-value)))
5134               (mime-entity-set-content-type-internal
5135                header (mime-parse-Content-Type ctype)))
5136           (when (equal id ref)
5137             (setq ref nil))
5138
5139           (when gnus-alter-header-function
5140             (funcall gnus-alter-header-function header)
5141             (setq id (mail-header-id header)
5142                   ref (gnus-parent-id (mail-header-references header))))
5143
5144           (when (setq header
5145                       (gnus-dependencies-add-header
5146                        header dependencies force-new))
5147             (push header headers))
5148           (goto-char (point-max))
5149           (widen))
5150         (nreverse headers)))))
5151
5152 ;; Goes through the xover lines and returns a list of vectors
5153 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5154                                                   force-new dependencies
5155                                                   group also-fetch-heads)
5156   "Parse the news overview data in the server buffer.
5157 Return a list of headers that match SEQUENCE (see
5158 `nntp-retrieve-headers')."
5159   ;; Get the Xref when the users reads the articles since most/some
5160   ;; NNTP servers do not include Xrefs when using XOVER.
5161   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5162   (let ((mail-parse-charset gnus-newsgroup-charset)
5163         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5164         (cur nntp-server-buffer)
5165         (dependencies (or dependencies gnus-newsgroup-dependencies))
5166         (allp (cond
5167                ((eq gnus-read-all-available-headers t)
5168                 t)
5169                ((stringp gnus-read-all-available-headers)
5170                 (string-match gnus-read-all-available-headers group))
5171                (t
5172                 nil)))
5173         number headers header)
5174     (save-excursion
5175       (set-buffer nntp-server-buffer)
5176       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5177       ;; Allow the user to mangle the headers before parsing them.
5178       (gnus-run-hooks 'gnus-parse-headers-hook)
5179       (goto-char (point-min))
5180       (while (not (eobp))
5181         (condition-case ()
5182             (while (and (or sequence allp)
5183                         (not (eobp)))
5184               (setq number (read cur))
5185               (when (not allp)
5186                 (while (and sequence
5187                             (< (car sequence) number))
5188                   (setq sequence (cdr sequence))))
5189               (when (and (or allp
5190                              (and sequence
5191                                   (eq number (car sequence))))
5192                          (progn
5193                            (setq sequence (cdr sequence))
5194                            (setq header (inline
5195                                           (gnus-nov-parse-line
5196                                            number dependencies force-new)))))
5197                 (push header headers))
5198               (forward-line 1))
5199           (error
5200            (gnus-error 4 "Strange nov line (%d)"
5201                        (count-lines (point-min) (point)))))
5202         (forward-line 1))
5203       ;; A common bug in inn is that if you have posted an article and
5204       ;; then retrieves the active file, it will answer correctly --
5205       ;; the new article is included.  However, a NOV entry for the
5206       ;; article may not have been generated yet, so this may fail.
5207       ;; We work around this problem by retrieving the last few
5208       ;; headers using HEAD.
5209       (if (or (not also-fetch-heads)
5210               (not sequence))
5211           ;; We (probably) got all the headers.
5212           (nreverse headers)
5213         (let ((gnus-nov-is-evil t))
5214           (nconc
5215            (nreverse headers)
5216            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5217            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5218                (when (gnus-retrieve-headers sequence group)
5219                  (gnus-get-newsgroup-headers))
5220              (gnus-retrieve-parsed-headers sequence group))))))))
5221
5222 (defun gnus-article-get-xrefs ()
5223   "Fill in the Xref value in `gnus-current-headers', if necessary.
5224 This is meant to be called in `gnus-article-internal-prepare-hook'."
5225   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5226                                  gnus-current-headers)))
5227     (or (not gnus-use-cross-reference)
5228         (not headers)
5229         (and (mail-header-xref headers)
5230              (not (string= (mail-header-xref headers) "")))
5231         (let ((case-fold-search t)
5232               xref)
5233           (save-restriction
5234             (nnheader-narrow-to-headers)
5235             (goto-char (point-min))
5236             (when (or (and (not (eobp))
5237                            (eq (downcase (char-after)) ?x)
5238                            (looking-at "Xref:"))
5239                       (search-forward "\nXref:" nil t))
5240               (goto-char (1+ (match-end 0)))
5241               (setq xref (buffer-substring (point)
5242                                            (progn (end-of-line) (point))))
5243               (mail-header-set-xref headers xref)))))))
5244
5245 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5246   "Find article ID and insert the summary line for that article.
5247 OLD-HEADER can either be a header or a line number to insert
5248 the subject line on."
5249   (let* ((line (and (numberp old-header) old-header))
5250          (old-header (and (vectorp old-header) old-header))
5251          (header (cond ((and old-header use-old-header)
5252                         old-header)
5253                        ((and (numberp id)
5254                              (gnus-number-to-header id))
5255                         (gnus-number-to-header id))
5256                        (t
5257                         (gnus-read-header id))))
5258          (number (and (numberp id) id))
5259          d)
5260     (when header
5261       ;; Rebuild the thread that this article is part of and go to the
5262       ;; article we have fetched.
5263       (when (and (not gnus-show-threads)
5264                  old-header)
5265         (when (and number
5266                    (setq d (gnus-data-find (mail-header-number old-header))))
5267           (goto-char (gnus-data-pos d))
5268           (gnus-data-remove
5269            number
5270            (- (gnus-point-at-bol)
5271               (prog1
5272                   (1+ (gnus-point-at-eol))
5273                 (gnus-delete-line))))))
5274       (when old-header
5275         (mail-header-set-number header (mail-header-number old-header)))
5276       (setq gnus-newsgroup-sparse
5277             (delq (setq number (mail-header-number header))
5278                   gnus-newsgroup-sparse))
5279       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5280       (push number gnus-newsgroup-limit)
5281       (gnus-rebuild-thread (mail-header-id header) line)
5282       (gnus-summary-goto-subject number nil t))
5283     (when (and (numberp number)
5284                (> number 0))
5285       ;; We have to update the boundaries even if we can't fetch the
5286       ;; article if ID is a number -- so that the next `P' or `N'
5287       ;; command will fetch the previous (or next) article even
5288       ;; if the one we tried to fetch this time has been canceled.
5289       (when (> number gnus-newsgroup-end)
5290         (setq gnus-newsgroup-end number))
5291       (when (< number gnus-newsgroup-begin)
5292         (setq gnus-newsgroup-begin number))
5293       (setq gnus-newsgroup-unselected
5294             (delq number gnus-newsgroup-unselected)))
5295     ;; Report back a success?
5296     (and header (mail-header-number header))))
5297
5298 ;;; Process/prefix in the summary buffer
5299
5300 (defun gnus-summary-work-articles (n)
5301   "Return a list of articles to be worked upon.
5302 The prefix argument, the list of process marked articles, and the
5303 current article will be taken into consideration."
5304   (save-excursion
5305     (set-buffer gnus-summary-buffer)
5306     (cond
5307      (n
5308       ;; A numerical prefix has been given.
5309       (setq n (prefix-numeric-value n))
5310       (let ((backward (< n 0))
5311             (n (abs (prefix-numeric-value n)))
5312             articles article)
5313         (save-excursion
5314           (while
5315               (and (> n 0)
5316                    (push (setq article (gnus-summary-article-number))
5317                          articles)
5318                    (if backward
5319                        (gnus-summary-find-prev nil article)
5320                      (gnus-summary-find-next nil article)))
5321             (decf n)))
5322         (nreverse articles)))
5323      ((and (gnus-region-active-p) (mark))
5324       (message "region active")
5325       ;; Work on the region between point and mark.
5326       (let ((max (max (point) (mark)))
5327             articles article)
5328         (save-excursion
5329           (goto-char (min (point) (mark)))
5330           (while
5331               (and
5332                (push (setq article (gnus-summary-article-number)) articles)
5333                (gnus-summary-find-next nil article)
5334                (< (point) max)))
5335           (nreverse articles))))
5336      (gnus-newsgroup-processable
5337       ;; There are process-marked articles present.
5338       ;; Save current state.
5339       (gnus-summary-save-process-mark)
5340       ;; Return the list.
5341       (reverse gnus-newsgroup-processable))
5342      (t
5343       ;; Just return the current article.
5344       (list (gnus-summary-article-number))))))
5345
5346 (defmacro gnus-summary-iterate (arg &rest forms)
5347   "Iterate over the process/prefixed articles and do FORMS.
5348 ARG is the interactive prefix given to the command.  FORMS will be
5349 executed with point over the summary line of the articles."
5350   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5351     `(let ((,articles (gnus-summary-work-articles ,arg)))
5352        (while ,articles
5353          (gnus-summary-goto-subject (car ,articles))
5354          ,@forms
5355          (pop ,articles)))))
5356
5357 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5358 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5359
5360 (defun gnus-summary-save-process-mark ()
5361   "Push the current set of process marked articles on the stack."
5362   (interactive)
5363   (push (copy-sequence gnus-newsgroup-processable)
5364         gnus-newsgroup-process-stack))
5365
5366 (defun gnus-summary-kill-process-mark ()
5367   "Push the current set of process marked articles on the stack and unmark."
5368   (interactive)
5369   (gnus-summary-save-process-mark)
5370   (gnus-summary-unmark-all-processable))
5371
5372 (defun gnus-summary-yank-process-mark ()
5373   "Pop the last process mark state off the stack and restore it."
5374   (interactive)
5375   (unless gnus-newsgroup-process-stack
5376     (error "Empty mark stack"))
5377   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5378
5379 (defun gnus-summary-process-mark-set (set)
5380   "Make SET into the current process marked articles."
5381   (gnus-summary-unmark-all-processable)
5382   (while set
5383     (gnus-summary-set-process-mark (pop set))))
5384
5385 ;;; Searching and stuff
5386
5387 (defun gnus-summary-search-group (&optional backward use-level)
5388   "Search for next unread newsgroup.
5389 If optional argument BACKWARD is non-nil, search backward instead."
5390   (save-excursion
5391     (set-buffer gnus-group-buffer)
5392     (when (gnus-group-search-forward
5393            backward nil (if use-level (gnus-group-group-level) nil))
5394       (gnus-group-group-name))))
5395
5396 (defun gnus-summary-best-group (&optional exclude-group)
5397   "Find the name of the best unread group.
5398 If EXCLUDE-GROUP, do not go to this group."
5399   (save-excursion
5400     (set-buffer gnus-group-buffer)
5401     (save-excursion
5402       (gnus-group-best-unread-group exclude-group))))
5403
5404 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5405   (if backward (gnus-summary-find-prev)
5406     (let* ((dummy (gnus-summary-article-intangible-p))
5407            (article (or article (gnus-summary-article-number)))
5408            (arts (gnus-data-find-list article))
5409            result)
5410       (when (and (not dummy)
5411                  (or (not gnus-summary-check-current)
5412                      (not unread)
5413                      (not (gnus-data-unread-p (car arts)))))
5414         (setq arts (cdr arts)))
5415       (when (setq result
5416                   (if unread
5417                       (progn
5418                         (while arts
5419                           (when (or (and undownloaded
5420                                          (eq gnus-undownloaded-mark
5421                                              (gnus-data-mark (car arts))))
5422                                     (gnus-data-unread-p (car arts)))
5423                             (setq result (car arts)
5424                                   arts nil))
5425                           (setq arts (cdr arts)))
5426                         result)
5427                     (car arts)))
5428         (goto-char (gnus-data-pos result))
5429         (gnus-data-number result)))))
5430
5431 (defun gnus-summary-find-prev (&optional unread article)
5432   (let* ((eobp (eobp))
5433          (article (or article (gnus-summary-article-number)))
5434          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5435          result)
5436     (when (and (not eobp)
5437                (or (not gnus-summary-check-current)
5438                    (not unread)
5439                    (not (gnus-data-unread-p (car arts)))))
5440       (setq arts (cdr arts)))
5441     (when (setq result
5442                 (if unread
5443                     (progn
5444                       (while arts
5445                         (when (gnus-data-unread-p (car arts))
5446                           (setq result (car arts)
5447                                 arts nil))
5448                         (setq arts (cdr arts)))
5449                       result)
5450                   (car arts)))
5451       (goto-char (gnus-data-pos result))
5452       (gnus-data-number result))))
5453
5454 (defun gnus-summary-find-subject (subject &optional unread backward article)
5455   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5456          (article (or article (gnus-summary-article-number)))
5457          (articles (gnus-data-list backward))
5458          (arts (gnus-data-find-list article articles))
5459          result)
5460     (when (or (not gnus-summary-check-current)
5461               (not unread)
5462               (not (gnus-data-unread-p (car arts))))
5463       (setq arts (cdr arts)))
5464     (while arts
5465       (and (or (not unread)
5466                (gnus-data-unread-p (car arts)))
5467            (vectorp (gnus-data-header (car arts)))
5468            (gnus-subject-equal
5469             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5470            (setq result (car arts)
5471                  arts nil))
5472       (setq arts (cdr arts)))
5473     (and result
5474          (goto-char (gnus-data-pos result))
5475          (gnus-data-number result))))
5476
5477 (defun gnus-summary-search-forward (&optional unread subject backward)
5478   "Search forward for an article.
5479 If UNREAD, look for unread articles.  If SUBJECT, look for
5480 articles with that subject.  If BACKWARD, search backward instead."
5481   (cond (subject (gnus-summary-find-subject subject unread backward))
5482         (backward (gnus-summary-find-prev unread))
5483         (t (gnus-summary-find-next unread))))
5484
5485 (defun gnus-recenter (&optional n)
5486   "Center point in window and redisplay frame.
5487 Also do horizontal recentering."
5488   (interactive "P")
5489   (when (and gnus-auto-center-summary
5490              (not (eq gnus-auto-center-summary 'vertical)))
5491     (gnus-horizontal-recenter))
5492   (recenter n))
5493
5494 (defun gnus-summary-recenter ()
5495   "Center point in the summary window.
5496 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5497 displayed, no centering will be performed."
5498   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5499   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5500   (interactive)
5501   (let* ((top (cond ((< (window-height) 4) 0)
5502                     ((< (window-height) 7) 1)
5503                     (t (if (numberp gnus-auto-center-summary)
5504                            gnus-auto-center-summary
5505                          2))))
5506          (height (1- (window-height)))
5507          (bottom (save-excursion (goto-char (point-max))
5508                                  (forward-line (- height))
5509                                  (point)))
5510          (window (get-buffer-window (current-buffer))))
5511     ;; The user has to want it.
5512     (when gnus-auto-center-summary
5513       (when (get-buffer-window gnus-article-buffer)
5514         ;; Only do recentering when the article buffer is displayed,
5515         ;; Set the window start to either `bottom', which is the biggest
5516         ;; possible valid number, or the second line from the top,
5517         ;; whichever is the least.
5518         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5519           (if (> bottom top-pos)
5520               ;; Keep the second line from the top visible
5521               (set-window-start window top-pos t)
5522             ;; Try to keep the bottom line visible; if it's partially
5523             ;; obscured, either scroll one more line to make it fully
5524             ;; visible, or revert to using TOP-POS.
5525             (save-excursion
5526               (goto-char (point-max))
5527               (forward-line -1)
5528               (let ((last-line-start (point)))
5529                 (goto-char bottom)
5530                 (set-window-start window (point) t)
5531                 (when (not (pos-visible-in-window-p last-line-start window))
5532                   (forward-line 1)
5533                   (set-window-start window (min (point) top-pos) t)))))))
5534       ;; Do horizontal recentering while we're at it.
5535       (when (and (get-buffer-window (current-buffer) t)
5536                  (not (eq gnus-auto-center-summary 'vertical)))
5537         (let ((selected (selected-window)))
5538           (select-window (get-buffer-window (current-buffer) t))
5539           (gnus-summary-position-point)
5540           (gnus-horizontal-recenter)
5541           (select-window selected))))))
5542
5543 (defun gnus-summary-jump-to-group (newsgroup)
5544   "Move point to NEWSGROUP in group mode buffer."
5545   ;; Keep update point of group mode buffer if visible.
5546   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5547       (save-window-excursion
5548         ;; Take care of tree window mode.
5549         (when (get-buffer-window gnus-group-buffer)
5550           (pop-to-buffer gnus-group-buffer))
5551         (gnus-group-jump-to-group newsgroup))
5552     (save-excursion
5553       ;; Take care of tree window mode.
5554       (if (get-buffer-window gnus-group-buffer)
5555           (pop-to-buffer gnus-group-buffer)
5556         (set-buffer gnus-group-buffer))
5557       (gnus-group-jump-to-group newsgroup))))
5558
5559 ;; This function returns a list of article numbers based on the
5560 ;; difference between the ranges of read articles in this group and
5561 ;; the range of active articles.
5562 (defun gnus-list-of-unread-articles (group)
5563   (let* ((read (gnus-info-read (gnus-get-info group)))
5564          (active (or (gnus-active group) (gnus-activate-group group)))
5565          (last (cdr active))
5566          first nlast unread)
5567     ;; If none are read, then all are unread.
5568     (if (not read)
5569         (setq first (car active))
5570       ;; If the range of read articles is a single range, then the
5571       ;; first unread article is the article after the last read
5572       ;; article.  Sounds logical, doesn't it?
5573       (if (and (not (listp (cdr read)))
5574                (or (< (car read) (car active))
5575                    (progn (setq read (list read))
5576                           nil)))
5577           (setq first (max (car active) (1+ (cdr read))))
5578         ;; `read' is a list of ranges.
5579         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5580                                   (caar read)))
5581                   1)
5582           (setq first (car active)))
5583         (while read
5584           (when first
5585             (while (< first nlast)
5586               (push first unread)
5587               (setq first (1+ first))))
5588           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5589           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5590           (setq read (cdr read)))))
5591     ;; And add the last unread articles.
5592     (while (<= first last)
5593       (push first unread)
5594       (setq first (1+ first)))
5595     ;; Return the list of unread articles.
5596     (delq 0 (nreverse unread))))
5597
5598 (defun gnus-list-of-read-articles (group)
5599   "Return a list of unread, unticked and non-dormant articles."
5600   (let* ((info (gnus-get-info group))
5601          (marked (gnus-info-marks info))
5602          (active (gnus-active group)))
5603     (and info active
5604          (gnus-set-difference
5605           (gnus-sorted-complement
5606            (gnus-uncompress-range active)
5607            (gnus-list-of-unread-articles group))
5608           (append
5609            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5610            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5611
5612 ;; Various summary commands
5613
5614 (defun gnus-summary-select-article-buffer ()
5615   "Reconfigure windows to show article buffer."
5616   (interactive)
5617   (if (not (gnus-buffer-live-p gnus-article-buffer))
5618       (error "There is no article buffer for this summary buffer")
5619     (gnus-configure-windows 'article)
5620     (select-window (get-buffer-window gnus-article-buffer))))
5621
5622 (defun gnus-summary-universal-argument (arg)
5623   "Perform any operation on all articles that are process/prefixed."
5624   (interactive "P")
5625   (let ((articles (gnus-summary-work-articles arg))
5626         func article)
5627     (if (eq
5628          (setq
5629           func
5630           (key-binding
5631            (read-key-sequence
5632             (substitute-command-keys
5633              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5634          'undefined)
5635         (gnus-error 1 "Undefined key")
5636       (save-excursion
5637         (while articles
5638           (gnus-summary-goto-subject (setq article (pop articles)))
5639           (let (gnus-newsgroup-processable)
5640             (command-execute func))
5641           (gnus-summary-remove-process-mark article)))))
5642   (gnus-summary-position-point))
5643
5644 (defun gnus-summary-toggle-truncation (&optional arg)
5645   "Toggle truncation of summary lines.
5646 With arg, turn line truncation on iff arg is positive."
5647   (interactive "P")
5648   (setq truncate-lines
5649         (if (null arg) (not truncate-lines)
5650           (> (prefix-numeric-value arg) 0)))
5651   (redraw-display))
5652
5653 (defun gnus-summary-reselect-current-group (&optional all rescan)
5654   "Exit and then reselect the current newsgroup.
5655 The prefix argument ALL means to select all articles."
5656   (interactive "P")
5657   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5658     (error "Ephemeral groups can't be reselected"))
5659   (let ((current-subject (gnus-summary-article-number))
5660         (group gnus-newsgroup-name))
5661     (setq gnus-newsgroup-begin nil)
5662     (gnus-summary-exit)
5663     ;; We have to adjust the point of group mode buffer because
5664     ;; point was moved to the next unread newsgroup by exiting.
5665     (gnus-summary-jump-to-group group)
5666     (when rescan
5667       (save-excursion
5668         (save-window-excursion
5669           ;; Don't show group contents.
5670           (set-window-start (selected-window) (point-max))
5671           (gnus-group-get-new-news-this-group 1))))
5672     (gnus-group-read-group all t)
5673     (gnus-summary-goto-subject current-subject nil t)))
5674
5675 (defun gnus-summary-rescan-group (&optional all)
5676   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5677   (interactive "P")
5678   (gnus-summary-reselect-current-group all t))
5679
5680 (defun gnus-summary-update-info (&optional non-destructive)
5681   (save-excursion
5682     (let ((group gnus-newsgroup-name))
5683       (when group
5684         (when gnus-newsgroup-kill-headers
5685           (setq gnus-newsgroup-killed
5686                 (gnus-compress-sequence
5687                  (nconc
5688                   (gnus-set-sorted-intersection
5689                    (gnus-uncompress-range gnus-newsgroup-killed)
5690                    (setq gnus-newsgroup-unselected
5691                          (sort gnus-newsgroup-unselected '<)))
5692                   (setq gnus-newsgroup-unreads
5693                         (sort gnus-newsgroup-unreads '<)))
5694                  t)))
5695         (unless (listp (cdr gnus-newsgroup-killed))
5696           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5697         (let ((headers gnus-newsgroup-headers))
5698           ;; Set the new ranges of read articles.
5699           (save-excursion
5700             (set-buffer gnus-group-buffer)
5701             (gnus-undo-force-boundary))
5702           (gnus-update-read-articles
5703            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5704           ;; Set the current article marks.
5705           (let ((gnus-newsgroup-scored
5706                  (if (and (not gnus-save-score)
5707                           (not non-destructive))
5708                      nil
5709                    gnus-newsgroup-scored)))
5710             (save-excursion
5711               (gnus-update-marks)))
5712           ;; Do the cross-ref thing.
5713           (when gnus-use-cross-reference
5714             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5715           ;; Do not switch windows but change the buffer to work.
5716           (set-buffer gnus-group-buffer)
5717           (unless (gnus-ephemeral-group-p group)
5718             (gnus-group-update-group group)))))))
5719
5720 (defun gnus-summary-save-newsrc (&optional force)
5721   "Save the current number of read/marked articles in the dribble buffer.
5722 The dribble buffer will then be saved.
5723 If FORCE (the prefix), also save the .newsrc file(s)."
5724   (interactive "P")
5725   (gnus-summary-update-info t)
5726   (if force
5727       (gnus-save-newsrc-file)
5728     (gnus-dribble-save)))
5729
5730 (defun gnus-summary-exit (&optional temporary)
5731   "Exit reading current newsgroup, and then return to group selection mode.
5732 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5733   (interactive)
5734   (gnus-set-global-variables)
5735   (gnus-kill-save-kill-buffer)
5736   (gnus-async-halt-prefetch)
5737   (let* ((group gnus-newsgroup-name)
5738          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5739          (mode major-mode)
5740          (group-point nil)
5741          (buf (current-buffer)))
5742     (unless quit-config
5743       ;; Do adaptive scoring, and possibly save score files.
5744       (when gnus-newsgroup-adaptive
5745         (gnus-score-adaptive))
5746       (when gnus-use-scoring
5747         (gnus-score-save)))
5748     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5749     ;; If we have several article buffers, we kill them at exit.
5750     (unless gnus-single-article-buffer
5751       (gnus-kill-buffer gnus-original-article-buffer)
5752       (setq gnus-article-current nil))
5753     (when gnus-use-cache
5754       (gnus-cache-possibly-remove-articles)
5755       (gnus-cache-save-buffers))
5756     (gnus-async-prefetch-remove-group group)
5757     (when gnus-suppress-duplicates
5758       (gnus-dup-enter-articles))
5759     (when gnus-use-trees
5760       (gnus-tree-close group))
5761     (when gnus-use-cache
5762       (gnus-cache-write-active))
5763     ;; Remove entries for this group.
5764     (nnmail-purge-split-history (gnus-group-real-name group))
5765     ;; Make all changes in this group permanent.
5766     (unless quit-config
5767       (gnus-run-hooks 'gnus-exit-group-hook)
5768       (gnus-summary-update-info))
5769     (gnus-close-group group)
5770     ;; Make sure where we were, and go to next newsgroup.
5771     (set-buffer gnus-group-buffer)
5772     (unless quit-config
5773       (gnus-group-jump-to-group group))
5774     (gnus-run-hooks 'gnus-summary-exit-hook)
5775     (unless (or quit-config
5776                 ;; If this group has disappeared from the summary
5777                 ;; buffer, don't skip forwards.
5778                 (not (string= group (gnus-group-group-name))))
5779       (gnus-group-next-unread-group 1))
5780     (setq group-point (point))
5781     (if temporary
5782         nil                             ;Nothing to do.
5783       ;; If we have several article buffers, we kill them at exit.
5784       (unless gnus-single-article-buffer
5785         (gnus-kill-buffer gnus-article-buffer)
5786         (gnus-kill-buffer gnus-original-article-buffer)
5787         (setq gnus-article-current nil))
5788       (set-buffer buf)
5789       (if (not gnus-kill-summary-on-exit)
5790           (progn
5791             (gnus-deaden-summary)
5792             (setq mode nil))
5793         ;; We set all buffer-local variables to nil.  It is unclear why
5794         ;; this is needed, but if we don't, buffer-local variables are
5795         ;; not garbage-collected, it seems.  This would the lead to en
5796         ;; ever-growing Emacs.
5797         (gnus-summary-clear-local-variables)
5798         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5799           (gnus-summary-clear-local-variables))
5800         (when (get-buffer gnus-article-buffer)
5801           (bury-buffer gnus-article-buffer))
5802         ;; We clear the global counterparts of the buffer-local
5803         ;; variables as well, just to be on the safe side.
5804         (set-buffer gnus-group-buffer)
5805         (gnus-summary-clear-local-variables)
5806         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5807           (gnus-summary-clear-local-variables)))
5808       (setq gnus-current-select-method gnus-select-method)
5809       (pop-to-buffer gnus-group-buffer)
5810       (if (not quit-config)
5811           (progn
5812             (goto-char group-point)
5813             (gnus-configure-windows 'group 'force)
5814             (unless (pos-visible-in-window-p)
5815               (forward-line (/ (static-if (featurep 'xemacs)
5816                                    (window-displayed-height)
5817                                  (1- (window-height)))
5818                                -2))
5819               (set-window-start (selected-window) (point))
5820               (goto-char group-point)))
5821         (gnus-handle-ephemeral-exit quit-config))
5822       ;; Return to group mode buffer.
5823       (when (eq mode 'gnus-summary-mode)
5824         (gnus-kill-buffer buf))
5825       ;; Clear the current group name.
5826       (unless quit-config
5827         (setq gnus-newsgroup-name nil)))))
5828
5829 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5830 (defun gnus-summary-exit-no-update (&optional no-questions)
5831   "Quit reading current newsgroup without updating read article info."
5832   (interactive)
5833   (let* ((group gnus-newsgroup-name)
5834          (quit-config (gnus-group-quit-config group)))
5835     (when (or no-questions
5836               gnus-expert-user
5837               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5838       (gnus-async-halt-prefetch)
5839       (mapcar 'funcall
5840               (delq 'gnus-summary-expire-articles
5841                     (copy-sequence gnus-summary-prepare-exit-hook)))
5842       ;; If we have several article buffers, we kill them at exit.
5843       (unless gnus-single-article-buffer
5844         (gnus-kill-buffer gnus-article-buffer)
5845         (gnus-kill-buffer gnus-original-article-buffer)
5846         (setq gnus-article-current nil))
5847       (if (not gnus-kill-summary-on-exit)
5848           (gnus-deaden-summary)
5849         (gnus-close-group group)
5850         (gnus-summary-clear-local-variables)
5851         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5852           (gnus-summary-clear-local-variables))
5853         (set-buffer gnus-group-buffer)
5854         (gnus-summary-clear-local-variables)
5855         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5856           (gnus-summary-clear-local-variables))
5857         (when (get-buffer gnus-summary-buffer)
5858           (kill-buffer gnus-summary-buffer)))
5859       (unless gnus-single-article-buffer
5860         (setq gnus-article-current nil))
5861       (when gnus-use-trees
5862         (gnus-tree-close group))
5863       (gnus-async-prefetch-remove-group group)
5864       (when (get-buffer gnus-article-buffer)
5865         (bury-buffer gnus-article-buffer))
5866       ;; Return to the group buffer.
5867       (gnus-configure-windows 'group 'force)
5868       ;; Clear the current group name.
5869       (setq gnus-newsgroup-name nil)
5870       (when (equal (gnus-group-group-name) group)
5871         (gnus-group-next-unread-group 1))
5872       (when quit-config
5873         (gnus-handle-ephemeral-exit quit-config)))))
5874
5875 (defun gnus-handle-ephemeral-exit (quit-config)
5876   "Handle movement when leaving an ephemeral group.
5877 The state which existed when entering the ephemeral is reset."
5878   (if (not (buffer-name (car quit-config)))
5879       (gnus-configure-windows 'group 'force)
5880     (set-buffer (car quit-config))
5881     (cond ((eq major-mode 'gnus-summary-mode)
5882            (gnus-set-global-variables))
5883           ((eq major-mode 'gnus-article-mode)
5884            (save-excursion
5885              ;; The `gnus-summary-buffer' variable may point
5886              ;; to the old summary buffer when using a single
5887              ;; article buffer.
5888              (unless (gnus-buffer-live-p gnus-summary-buffer)
5889                (set-buffer gnus-group-buffer))
5890              (set-buffer gnus-summary-buffer)
5891              (gnus-set-global-variables))))
5892     (if (or (eq (cdr quit-config) 'article)
5893             (eq (cdr quit-config) 'pick))
5894         (progn
5895           ;; The current article may be from the ephemeral group
5896           ;; thus it is best that we reload this article
5897           (gnus-summary-show-article)
5898           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5899               (gnus-configure-windows 'pick 'force)
5900             (gnus-configure-windows (cdr quit-config) 'force)))
5901       (gnus-configure-windows (cdr quit-config) 'force))
5902     (when (eq major-mode 'gnus-summary-mode)
5903       (gnus-summary-next-subject 1 nil t)
5904       (gnus-summary-recenter)
5905       (gnus-summary-position-point))))
5906
5907 (defun gnus-summary-preview-mime-message ()
5908   "MIME decode and play this message."
5909   (interactive)
5910   (let ((gnus-break-pages nil)
5911         (gnus-show-mime t))
5912     (gnus-summary-select-article gnus-show-all-headers t))
5913   (select-window (get-buffer-window gnus-article-buffer)))
5914
5915 ;;; Dead summaries.
5916
5917 (defvar gnus-dead-summary-mode-map nil)
5918
5919 (unless gnus-dead-summary-mode-map
5920   (setq gnus-dead-summary-mode-map (make-keymap))
5921   (suppress-keymap gnus-dead-summary-mode-map)
5922   (substitute-key-definition
5923    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5924   (let ((keys '("\C-d" "\r" "\177" [delete])))
5925     (while keys
5926       (define-key gnus-dead-summary-mode-map
5927         (pop keys) 'gnus-summary-wake-up-the-dead))))
5928
5929 (defvar gnus-dead-summary-mode nil
5930   "Minor mode for Gnus summary buffers.")
5931
5932 (defun gnus-dead-summary-mode (&optional arg)
5933   "Minor mode for Gnus summary buffers."
5934   (interactive "P")
5935   (when (eq major-mode 'gnus-summary-mode)
5936     (make-local-variable 'gnus-dead-summary-mode)
5937     (setq gnus-dead-summary-mode
5938           (if (null arg) (not gnus-dead-summary-mode)
5939             (> (prefix-numeric-value arg) 0)))
5940     (when gnus-dead-summary-mode
5941       (gnus-add-minor-mode
5942        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5943
5944 (defun gnus-deaden-summary ()
5945   "Make the current summary buffer into a dead summary buffer."
5946   ;; Kill any previous dead summary buffer.
5947   (when (and gnus-dead-summary
5948              (buffer-name gnus-dead-summary))
5949     (save-excursion
5950       (set-buffer gnus-dead-summary)
5951       (when gnus-dead-summary-mode
5952         (kill-buffer (current-buffer)))))
5953   ;; Make this the current dead summary.
5954   (setq gnus-dead-summary (current-buffer))
5955   (gnus-dead-summary-mode 1)
5956   (let ((name (buffer-name)))
5957     (when (string-match "Summary" name)
5958       (rename-buffer
5959        (concat (substring name 0 (match-beginning 0)) "Dead "
5960                (substring name (match-beginning 0)))
5961        t)
5962       (bury-buffer))))
5963
5964 (defun gnus-kill-or-deaden-summary (buffer)
5965   "Kill or deaden the summary BUFFER."
5966   (save-excursion
5967     (when (and (buffer-name buffer)
5968                (not gnus-single-article-buffer))
5969       (save-excursion
5970         (set-buffer buffer)
5971         (gnus-kill-buffer gnus-article-buffer)
5972         (gnus-kill-buffer gnus-original-article-buffer)))
5973     (cond (gnus-kill-summary-on-exit
5974            (when (and gnus-use-trees
5975                       (gnus-buffer-exists-p buffer))
5976              (save-excursion
5977                (set-buffer buffer)
5978                (gnus-tree-close gnus-newsgroup-name)))
5979            (gnus-kill-buffer buffer))
5980           ((gnus-buffer-exists-p buffer)
5981            (save-excursion
5982              (set-buffer buffer)
5983              (gnus-deaden-summary))))))
5984
5985 (defun gnus-summary-wake-up-the-dead (&rest args)
5986   "Wake up the dead summary buffer."
5987   (interactive)
5988   (gnus-dead-summary-mode -1)
5989   (let ((name (buffer-name)))
5990     (when (string-match "Dead " name)
5991       (rename-buffer
5992        (concat (substring name 0 (match-beginning 0))
5993                (substring name (match-end 0)))
5994        t)))
5995   (gnus-message 3 "This dead summary is now alive again"))
5996
5997 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5998 (defun gnus-summary-fetch-faq (&optional faq-dir)
5999   "Fetch the FAQ for the current group.
6000 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6001 in."
6002   (interactive
6003    (list
6004     (when current-prefix-arg
6005       (completing-read
6006        "Faq dir: " (and (listp gnus-group-faq-directory)
6007                         (mapcar (lambda (file) (list file))
6008                                 gnus-group-faq-directory))))))
6009   (let (gnus-faq-buffer)
6010     (when (setq gnus-faq-buffer
6011                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6012       (gnus-configure-windows 'summary-faq))))
6013
6014 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6015 (defun gnus-summary-describe-group (&optional force)
6016   "Describe the current newsgroup."
6017   (interactive "P")
6018   (gnus-group-describe-group force gnus-newsgroup-name))
6019
6020 (defun gnus-summary-describe-briefly ()
6021   "Describe summary mode commands briefly."
6022   (interactive)
6023   (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")))
6024
6025 ;; Walking around group mode buffer from summary mode.
6026
6027 (defun gnus-summary-next-group (&optional no-article target-group backward)
6028   "Exit current newsgroup and then select next unread newsgroup.
6029 If prefix argument NO-ARTICLE is non-nil, no article is selected
6030 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6031 previous group instead."
6032   (interactive "P")
6033   ;; Stop pre-fetching.
6034   (gnus-async-halt-prefetch)
6035   (let ((current-group gnus-newsgroup-name)
6036         (current-buffer (current-buffer))
6037         entered)
6038     ;; First we semi-exit this group to update Xrefs and all variables.
6039     ;; We can't do a real exit, because the window conf must remain
6040     ;; the same in case the user is prompted for info, and we don't
6041     ;; want the window conf to change before that...
6042     (gnus-summary-exit t)
6043     (while (not entered)
6044       ;; Then we find what group we are supposed to enter.
6045       (set-buffer gnus-group-buffer)
6046       (gnus-group-jump-to-group current-group)
6047       (setq target-group
6048             (or target-group
6049                 (if (eq gnus-keep-same-level 'best)
6050                     (gnus-summary-best-group gnus-newsgroup-name)
6051                   (gnus-summary-search-group backward gnus-keep-same-level))))
6052       (if (not target-group)
6053           ;; There are no further groups, so we return to the group
6054           ;; buffer.
6055           (progn
6056             (gnus-message 5 "Returning to the group buffer")
6057             (setq entered t)
6058             (when (gnus-buffer-live-p current-buffer)
6059               (set-buffer current-buffer)
6060               (gnus-summary-exit))
6061             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6062         ;; We try to enter the target group.
6063         (gnus-group-jump-to-group target-group)
6064         (let ((unreads (gnus-group-group-unread)))
6065           (if (and (or (eq t unreads)
6066                        (and unreads (not (zerop unreads))))
6067                    (gnus-summary-read-group
6068                     target-group nil no-article
6069                     (and (buffer-name current-buffer) current-buffer)
6070                     nil backward))
6071               (setq entered t)
6072             (setq current-group target-group
6073                   target-group nil)))))))
6074
6075 (defun gnus-summary-prev-group (&optional no-article)
6076   "Exit current newsgroup and then select previous unread newsgroup.
6077 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6078   (interactive "P")
6079   (gnus-summary-next-group no-article nil t))
6080
6081 ;; Walking around summary lines.
6082
6083 (defun gnus-summary-first-subject (&optional unread undownloaded)
6084   "Go to the first unread subject.
6085 If UNREAD is non-nil, go to the first unread article.
6086 Returns the article selected or nil if there are no unread articles."
6087   (interactive "P")
6088   (prog1
6089       (cond
6090        ;; Empty summary.
6091        ((null gnus-newsgroup-data)
6092         (gnus-message 3 "No articles in the group")
6093         nil)
6094        ;; Pick the first article.
6095        ((not unread)
6096         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6097         (gnus-data-number (car gnus-newsgroup-data)))
6098        ;; No unread articles.
6099        ((null gnus-newsgroup-unreads)
6100         (gnus-message 3 "No more unread articles")
6101         nil)
6102        ;; Find the first unread article.
6103        (t
6104         (let ((data gnus-newsgroup-data))
6105           (while (and data
6106                       (and (not (and undownloaded
6107                                      (eq gnus-undownloaded-mark
6108                                          (gnus-data-mark (car data)))))
6109                            (not (gnus-data-unread-p (car data)))))
6110             (setq data (cdr data)))
6111           (when data
6112             (goto-char (gnus-data-pos (car data)))
6113             (gnus-data-number (car data))))))
6114     (gnus-summary-position-point)))
6115
6116 (defun gnus-summary-next-subject (n &optional unread dont-display)
6117   "Go to next N'th summary line.
6118 If N is negative, go to the previous N'th subject line.
6119 If UNREAD is non-nil, only unread articles are selected.
6120 The difference between N and the actual number of steps taken is
6121 returned."
6122   (interactive "p")
6123   (let ((backward (< n 0))
6124         (n (abs n)))
6125     (while (and (> n 0)
6126                 (if backward
6127                     (gnus-summary-find-prev unread)
6128                   (gnus-summary-find-next unread)))
6129       (unless (zerop (setq n (1- n)))
6130         (gnus-summary-show-thread)))
6131     (when (/= 0 n)
6132       (gnus-message 7 "No more%s articles"
6133                     (if unread " unread" "")))
6134     (unless dont-display
6135       (gnus-summary-recenter)
6136       (gnus-summary-position-point))
6137     n))
6138
6139 (defun gnus-summary-next-unread-subject (n)
6140   "Go to next N'th unread summary line."
6141   (interactive "p")
6142   (gnus-summary-next-subject n t))
6143
6144 (defun gnus-summary-prev-subject (n &optional unread)
6145   "Go to previous N'th summary line.
6146 If optional argument UNREAD is non-nil, only unread article is selected."
6147   (interactive "p")
6148   (gnus-summary-next-subject (- n) unread))
6149
6150 (defun gnus-summary-prev-unread-subject (n)
6151   "Go to previous N'th unread summary line."
6152   (interactive "p")
6153   (gnus-summary-next-subject (- n) t))
6154
6155 (defun gnus-summary-goto-subject (article &optional force silent)
6156   "Go the subject line of ARTICLE.
6157 If FORCE, also allow jumping to articles not currently shown."
6158   (interactive "nArticle number: ")
6159   (let ((b (point))
6160         (data (gnus-data-find article)))
6161     ;; We read in the article if we have to.
6162     (and (not data)
6163          force
6164          (gnus-summary-insert-subject
6165           article
6166           (if (or (numberp force) (vectorp force)) force)
6167           t)
6168          (setq data (gnus-data-find article)))
6169     (goto-char b)
6170     (if (not data)
6171         (progn
6172           (unless silent
6173             (gnus-message 3 "Can't find article %d" article))
6174           nil)
6175       (goto-char (gnus-data-pos data))
6176       (gnus-summary-position-point)
6177       article)))
6178
6179 ;; Walking around summary lines with displaying articles.
6180
6181 (defun gnus-summary-expand-window (&optional arg)
6182   "Make the summary buffer take up the entire Emacs frame.
6183 Given a prefix, will force an `article' buffer configuration."
6184   (interactive "P")
6185   (if arg
6186       (gnus-configure-windows 'article 'force)
6187     (gnus-configure-windows 'summary 'force)))
6188
6189 (defun gnus-summary-display-article (article &optional all-header)
6190   "Display ARTICLE in article buffer."
6191   (when (gnus-buffer-live-p gnus-article-buffer)
6192     (with-current-buffer gnus-article-buffer
6193       (set-buffer-multibyte t)))
6194   (gnus-set-global-variables)
6195   (when (gnus-buffer-live-p gnus-article-buffer)
6196     (with-current-buffer gnus-article-buffer
6197       (setq gnus-article-charset gnus-newsgroup-charset)
6198       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6199   (if (null article)
6200       nil
6201     (prog1
6202         (if gnus-summary-display-article-function
6203             (funcall gnus-summary-display-article-function article all-header)
6204           (gnus-article-prepare article all-header))
6205       (with-current-buffer gnus-article-buffer
6206         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6207              nil))
6208       (gnus-run-hooks 'gnus-select-article-hook)
6209       (when (and gnus-current-article
6210                  (not (zerop gnus-current-article)))
6211         (gnus-summary-goto-subject gnus-current-article))
6212       (gnus-summary-recenter)
6213       (when (and gnus-use-trees gnus-show-threads)
6214         (gnus-possibly-generate-tree article)
6215         (gnus-highlight-selected-tree article))
6216       ;; Successfully display article.
6217       (gnus-article-set-window-start
6218        (cdr (assq article gnus-newsgroup-bookmarks))))))
6219
6220 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6221   "Select the current article.
6222 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6223 non-nil, the article will be re-fetched even if it already present in
6224 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6225 be displayed."
6226   ;; Make sure we are in the summary buffer to work around bbdb bug.
6227   (unless (eq major-mode 'gnus-summary-mode)
6228     (set-buffer gnus-summary-buffer))
6229   (let ((article (or article (gnus-summary-article-number)))
6230         (all-headers (not (not all-headers))) ;Must be T or NIL.
6231         gnus-summary-display-article-function)
6232     (and (not pseudo)
6233          (gnus-summary-article-pseudo-p article)
6234          (error "This is a pseudo-article"))
6235     (save-excursion
6236       (set-buffer gnus-summary-buffer)
6237       (if (or (and gnus-single-article-buffer
6238                    (or (null gnus-current-article)
6239                        (null gnus-article-current)
6240                        (null (get-buffer gnus-article-buffer))
6241                        (not (eq article (cdr gnus-article-current)))
6242                        (not (equal (car gnus-article-current)
6243                                    gnus-newsgroup-name))))
6244               (and (not gnus-single-article-buffer)
6245                    (or (null gnus-current-article)
6246                        (not (eq gnus-current-article article))))
6247               force)
6248           ;; The requested article is different from the current article.
6249           (progn
6250             (gnus-summary-display-article article all-headers)
6251             (when (or all-headers gnus-show-all-headers)
6252               (gnus-article-show-all-headers))
6253             (gnus-article-set-window-start
6254              (cdr (assq article gnus-newsgroup-bookmarks)))
6255             article)
6256         (when (or all-headers gnus-show-all-headers)
6257           (gnus-article-show-all-headers))
6258         'old))))
6259
6260 (defun gnus-summary-force-verify-and-decrypt ()
6261   (interactive)
6262   (let ((mm-verify-option 'known)
6263         (mm-decrypt-option 'known))
6264     (gnus-summary-select-article nil 'force)))
6265
6266 (defun gnus-summary-set-current-mark (&optional current-mark)
6267   "Obsolete function."
6268   nil)
6269
6270 (defun gnus-summary-next-article (&optional unread subject backward push)
6271   "Select the next article.
6272 If UNREAD, only unread articles are selected.
6273 If SUBJECT, only articles with SUBJECT are selected.
6274 If BACKWARD, the previous article is selected instead of the next."
6275   (interactive "P")
6276   (cond
6277    ;; Is there such an article?
6278    ((and (gnus-summary-search-forward unread subject backward)
6279          (or (gnus-summary-display-article (gnus-summary-article-number))
6280              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6281     (gnus-summary-position-point))
6282    ;; If not, we try the first unread, if that is wanted.
6283    ((and subject
6284          gnus-auto-select-same
6285          (gnus-summary-first-unread-article))
6286     (gnus-summary-position-point)
6287     (gnus-message 6 "Wrapped"))
6288    ;; Try to get next/previous article not displayed in this group.
6289    ((and gnus-auto-extend-newsgroup
6290          (not unread) (not subject))
6291     (gnus-summary-goto-article
6292      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6293      nil (count-lines (point-min) (point))))
6294    ;; Go to next/previous group.
6295    (t
6296     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6297       (gnus-summary-jump-to-group gnus-newsgroup-name))
6298     (let ((cmd last-command-char)
6299           (point
6300            (save-excursion
6301              (set-buffer gnus-group-buffer)
6302              (point)))
6303           (group
6304            (if (eq gnus-keep-same-level 'best)
6305                (gnus-summary-best-group gnus-newsgroup-name)
6306              (gnus-summary-search-group backward gnus-keep-same-level))))
6307       ;; For some reason, the group window gets selected.  We change
6308       ;; it back.
6309       (select-window (get-buffer-window (current-buffer)))
6310       ;; Select next unread newsgroup automagically.
6311       (cond
6312        ((or (not gnus-auto-select-next)
6313             (not cmd))
6314         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6315        ((or (eq gnus-auto-select-next 'quietly)
6316             (and (eq gnus-auto-select-next 'slightly-quietly)
6317                  push)
6318             (and (eq gnus-auto-select-next 'almost-quietly)
6319                  (gnus-summary-last-article-p)))
6320         ;; Select quietly.
6321         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6322             (gnus-summary-exit)
6323           (gnus-message 7 "No more%s articles (%s)..."
6324                         (if unread " unread" "")
6325                         (if group (concat "selecting " group)
6326                           "exiting"))
6327           (gnus-summary-next-group nil group backward)))
6328        (t
6329         (when (gnus-key-press-event-p last-input-event)
6330           (gnus-summary-walk-group-buffer
6331            gnus-newsgroup-name cmd unread backward point))))))))
6332
6333 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6334   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6335                       (?\C-p (gnus-group-prev-unread-group 1))))
6336         (cursor-in-echo-area t)
6337         keve key group ended)
6338     (save-excursion
6339       (set-buffer gnus-group-buffer)
6340       (goto-char start)
6341       (setq group
6342             (if (eq gnus-keep-same-level 'best)
6343                 (gnus-summary-best-group gnus-newsgroup-name)
6344               (gnus-summary-search-group backward gnus-keep-same-level))))
6345     (while (not ended)
6346       (gnus-message
6347        5 "No more%s articles%s" (if unread " unread" "")
6348        (if (and group
6349                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6350            (format " (Type %s for %s [%s])"
6351                    (single-key-description cmd) group
6352                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6353          (format " (Type %s to exit %s)"
6354                  (single-key-description cmd)
6355                  gnus-newsgroup-name)))
6356       ;; Confirm auto selection.
6357       (setq key (car (setq keve (gnus-read-event-char))))
6358       (setq ended t)
6359       (cond
6360        ((assq key keystrokes)
6361         (let ((obuf (current-buffer)))
6362           (switch-to-buffer gnus-group-buffer)
6363           (when group
6364             (gnus-group-jump-to-group group))
6365           (eval (cadr (assq key keystrokes)))
6366           (setq group (gnus-group-group-name))
6367           (switch-to-buffer obuf))
6368         (setq ended nil))
6369        ((equal key cmd)
6370         (if (or (not group)
6371                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6372             (gnus-summary-exit)
6373           (gnus-summary-next-group nil group backward)))
6374        (t
6375         (push (cdr keve) unread-command-events))))))
6376
6377 (defun gnus-summary-next-unread-article ()
6378   "Select unread article after current one."
6379   (interactive)
6380   (gnus-summary-next-article
6381    (or (not (eq gnus-summary-goto-unread 'never))
6382        (gnus-summary-last-article-p (gnus-summary-article-number)))
6383    (and gnus-auto-select-same
6384         (gnus-summary-article-subject))))
6385
6386 (defun gnus-summary-prev-article (&optional unread subject)
6387   "Select the article after the current one.
6388 If UNREAD is non-nil, only unread articles are selected."
6389   (interactive "P")
6390   (gnus-summary-next-article unread subject t))
6391
6392 (defun gnus-summary-prev-unread-article ()
6393   "Select unread article before current one."
6394   (interactive)
6395   (gnus-summary-prev-article
6396    (or (not (eq gnus-summary-goto-unread 'never))
6397        (gnus-summary-first-article-p (gnus-summary-article-number)))
6398    (and gnus-auto-select-same
6399         (gnus-summary-article-subject))))
6400
6401 (defun gnus-summary-next-page (&optional lines circular)
6402   "Show next page of the selected article.
6403 If at the end of the current article, select the next article.
6404 LINES says how many lines should be scrolled up.
6405
6406 If CIRCULAR is non-nil, go to the start of the article instead of
6407 selecting the next article when reaching the end of the current
6408 article."
6409   (interactive "P")
6410   (setq gnus-summary-buffer (current-buffer))
6411   (gnus-set-global-variables)
6412   (let ((article (gnus-summary-article-number))
6413         (article-window (get-buffer-window gnus-article-buffer t))
6414         endp)
6415     ;; If the buffer is empty, we have no article.
6416     (unless article
6417       (error "No article to select"))
6418     (gnus-configure-windows 'article)
6419     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6420         (if (and (eq gnus-summary-goto-unread 'never)
6421                  (not (gnus-summary-last-article-p article)))
6422             (gnus-summary-next-article)
6423           (gnus-summary-next-unread-article))
6424       (if (or (null gnus-current-article)
6425               (null gnus-article-current)
6426               (/= article (cdr gnus-article-current))
6427               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6428           ;; Selected subject is different from current article's.
6429           (gnus-summary-display-article article)
6430         (when article-window
6431           (gnus-eval-in-buffer-window gnus-article-buffer
6432             (setq endp (gnus-article-next-page lines)))
6433           (when endp
6434             (cond (circular
6435                    (gnus-summary-beginning-of-article))
6436                   (lines
6437                    (gnus-message 3 "End of message"))
6438                   ((null lines)
6439                    (if (and (eq gnus-summary-goto-unread 'never)
6440                             (not (gnus-summary-last-article-p article)))
6441                        (gnus-summary-next-article)
6442                      (gnus-summary-next-unread-article))))))))
6443     (gnus-summary-recenter)
6444     (gnus-summary-position-point)))
6445
6446 (defun gnus-summary-prev-page (&optional lines move)
6447   "Show previous page of selected article.
6448 Argument LINES specifies lines to be scrolled down.
6449 If MOVE, move to the previous unread article if point is at
6450 the beginning of the buffer."
6451   (interactive "P")
6452   (let ((article (gnus-summary-article-number))
6453         (article-window (get-buffer-window gnus-article-buffer t))
6454         endp)
6455     (gnus-configure-windows 'article)
6456     (if (or (null gnus-current-article)
6457             (null gnus-article-current)
6458             (/= article (cdr gnus-article-current))
6459             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6460         ;; Selected subject is different from current article's.
6461         (gnus-summary-display-article article)
6462       (gnus-summary-recenter)
6463       (when article-window
6464         (gnus-eval-in-buffer-window gnus-article-buffer
6465           (setq endp (gnus-article-prev-page lines)))
6466         (when (and move endp)
6467           (cond (lines
6468                  (gnus-message 3 "Beginning of message"))
6469                 ((null lines)
6470                  (if (and (eq gnus-summary-goto-unread 'never)
6471                           (not (gnus-summary-first-article-p article)))
6472                      (gnus-summary-prev-article)
6473                    (gnus-summary-prev-unread-article))))))))
6474   (gnus-summary-position-point))
6475
6476 (defun gnus-summary-prev-page-or-article (&optional lines)
6477   "Show previous page of selected article.
6478 Argument LINES specifies lines to be scrolled down.
6479 If at the beginning of the article, go to the next article."
6480   (interactive "P")
6481   (gnus-summary-prev-page lines t))
6482
6483 (defun gnus-summary-scroll-up (lines)
6484   "Scroll up (or down) one line current article.
6485 Argument LINES specifies lines to be scrolled up (or down if negative)."
6486   (interactive "p")
6487   (gnus-configure-windows 'article)
6488   (gnus-summary-show-thread)
6489   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6490     (gnus-eval-in-buffer-window gnus-article-buffer
6491       (cond ((> lines 0)
6492              (when (gnus-article-next-page lines)
6493                (gnus-message 3 "End of message")))
6494             ((< lines 0)
6495              (gnus-article-prev-page (- lines))))))
6496   (gnus-summary-recenter)
6497   (gnus-summary-position-point))
6498
6499 (defun gnus-summary-scroll-down (lines)
6500   "Scroll down (or up) one line current article.
6501 Argument LINES specifies lines to be scrolled down (or up if negative)."
6502   (interactive "p")
6503   (gnus-summary-scroll-up (- lines)))
6504
6505 (defun gnus-summary-next-same-subject ()
6506   "Select next article which has the same subject as current one."
6507   (interactive)
6508   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6509
6510 (defun gnus-summary-prev-same-subject ()
6511   "Select previous article which has the same subject as current one."
6512   (interactive)
6513   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6514
6515 (defun gnus-summary-next-unread-same-subject ()
6516   "Select next unread article which has the same subject as current one."
6517   (interactive)
6518   (gnus-summary-next-article t (gnus-summary-article-subject)))
6519
6520 (defun gnus-summary-prev-unread-same-subject ()
6521   "Select previous unread article which has the same subject as current one."
6522   (interactive)
6523   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6524
6525 (defun gnus-summary-first-unread-article ()
6526   "Select the first unread article.
6527 Return nil if there are no unread articles."
6528   (interactive)
6529   (prog1
6530       (when (gnus-summary-first-subject t)
6531         (gnus-summary-show-thread)
6532         (gnus-summary-first-subject t)
6533         (gnus-summary-display-article (gnus-summary-article-number)))
6534     (gnus-summary-position-point)))
6535
6536 (defun gnus-summary-first-unread-subject ()
6537   "Place the point on the subject line of the first unread article.
6538 Return nil if there are no unread articles."
6539   (interactive)
6540   (prog1
6541       (when (gnus-summary-first-subject t)
6542         (gnus-summary-show-thread)
6543         (gnus-summary-first-subject t))
6544     (gnus-summary-position-point)))
6545
6546 (defun gnus-summary-first-article ()
6547   "Select the first article.
6548 Return nil if there are no articles."
6549   (interactive)
6550   (prog1
6551       (when (gnus-summary-first-subject)
6552         (gnus-summary-show-thread)
6553         (gnus-summary-first-subject)
6554         (gnus-summary-display-article (gnus-summary-article-number)))
6555     (gnus-summary-position-point)))
6556
6557 (defun gnus-summary-best-unread-article ()
6558   "Select the unread article with the highest score."
6559   (interactive)
6560   (let ((best -1000000)
6561         (data gnus-newsgroup-data)
6562         article score)
6563     (while data
6564       (and (gnus-data-unread-p (car data))
6565            (> (setq score
6566                     (gnus-summary-article-score (gnus-data-number (car data))))
6567               best)
6568            (setq best score
6569                  article (gnus-data-number (car data))))
6570       (setq data (cdr data)))
6571     (prog1
6572         (if article
6573             (gnus-summary-goto-article article)
6574           (error "No unread articles"))
6575       (gnus-summary-position-point))))
6576
6577 (defun gnus-summary-last-subject ()
6578   "Go to the last displayed subject line in the group."
6579   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6580     (when article
6581       (gnus-summary-goto-subject article))))
6582
6583 (defun gnus-summary-goto-article (article &optional all-headers force)
6584   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6585 If ALL-HEADERS is non-nil, no header lines are hidden.
6586 If FORCE, go to the article even if it isn't displayed.  If FORCE
6587 is a number, it is the line the article is to be displayed on."
6588   (interactive
6589    (list
6590     (completing-read
6591      "Article number or Message-ID: "
6592      (mapcar (lambda (number) (list (int-to-string number)))
6593              gnus-newsgroup-limit))
6594     current-prefix-arg
6595     t))
6596   (prog1
6597       (if (and (stringp article)
6598                (string-match "@" article))
6599           (gnus-summary-refer-article article)
6600         (when (stringp article)
6601           (setq article (string-to-number article)))
6602         (if (gnus-summary-goto-subject article force)
6603             (gnus-summary-display-article article all-headers)
6604           (gnus-message 4 "Couldn't go to article %s" article) nil))
6605     (gnus-summary-position-point)))
6606
6607 (defun gnus-summary-goto-last-article ()
6608   "Go to the previously read article."
6609   (interactive)
6610   (prog1
6611       (when gnus-last-article
6612         (gnus-summary-goto-article gnus-last-article nil t))
6613     (gnus-summary-position-point)))
6614
6615 (defun gnus-summary-pop-article (number)
6616   "Pop one article off the history and go to the previous.
6617 NUMBER articles will be popped off."
6618   (interactive "p")
6619   (let (to)
6620     (setq gnus-newsgroup-history
6621           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6622     (if to
6623         (gnus-summary-goto-article (car to) nil t)
6624       (error "Article history empty")))
6625   (gnus-summary-position-point))
6626
6627 ;; Summary commands and functions for limiting the summary buffer.
6628
6629 (defun gnus-summary-limit-to-articles (n)
6630   "Limit the summary buffer to the next N articles.
6631 If not given a prefix, use the process marked articles instead."
6632   (interactive "P")
6633   (prog1
6634       (let ((articles (gnus-summary-work-articles n)))
6635         (setq gnus-newsgroup-processable nil)
6636         (gnus-summary-limit articles))
6637     (gnus-summary-position-point)))
6638
6639 (defun gnus-summary-pop-limit (&optional total)
6640   "Restore the previous limit.
6641 If given a prefix, remove all limits."
6642   (interactive "P")
6643   (when total
6644     (setq gnus-newsgroup-limits
6645           (list (mapcar (lambda (h) (mail-header-number h))
6646                         gnus-newsgroup-headers))))
6647   (unless gnus-newsgroup-limits
6648     (error "No limit to pop"))
6649   (prog1
6650       (gnus-summary-limit nil 'pop)
6651     (gnus-summary-position-point)))
6652
6653 (defun gnus-summary-limit-to-subject (subject &optional header)
6654   "Limit the summary buffer to articles that have subjects that match a regexp."
6655   (interactive "sLimit to subject (regexp): ")
6656   (unless header
6657     (setq header "subject"))
6658   (when (not (equal "" subject))
6659     (prog1
6660         (let ((articles (gnus-summary-find-matching
6661                          (or header "subject") subject 'all)))
6662           (unless articles
6663             (error "Found no matches for \"%s\"" subject))
6664           (gnus-summary-limit articles))
6665       (gnus-summary-position-point))))
6666
6667 (defun gnus-summary-limit-to-author (from)
6668   "Limit the summary buffer to articles that have authors that match a regexp."
6669   (interactive "sLimit to author (regexp): ")
6670   (gnus-summary-limit-to-subject from "from"))
6671
6672 (defun gnus-summary-limit-to-age (age &optional younger-p)
6673   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6674 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6675 articles that are younger than AGE days."
6676   (interactive
6677    (let ((younger current-prefix-arg)
6678          (days-got nil)
6679          days)
6680      (while (not days-got)
6681        (setq days (if younger
6682                       (read-string "Limit to articles within (in days): ")
6683                     (read-string "Limit to articles older than (in days): ")))
6684        (when (> (length days) 0)
6685          (setq days (read days)))
6686        (if (numberp days)
6687            (setq days-got t)
6688          (message "Please enter a number.")
6689          (sleep-for 1)))
6690      (list days younger)))
6691   (prog1
6692       (let ((data gnus-newsgroup-data)
6693             (cutoff (days-to-time age))
6694             articles d date is-younger)
6695         (while (setq d (pop data))
6696           (when (and (vectorp (gnus-data-header d))
6697                      (setq date (mail-header-date (gnus-data-header d))))
6698             (setq is-younger (time-less-p
6699                               (time-since (condition-case ()
6700                                               (date-to-time date)
6701                                             (error '(0 0))))
6702                               cutoff))
6703             (when (if younger-p
6704                       is-younger
6705                     (not is-younger))
6706               (push (gnus-data-number d) articles))))
6707         (gnus-summary-limit (nreverse articles)))
6708     (gnus-summary-position-point)))
6709
6710 (defun gnus-summary-limit-to-extra (header regexp)
6711   "Limit the summary buffer to articles that match an 'extra' header."
6712   (interactive
6713    (let ((header
6714           (intern
6715            (gnus-completing-read
6716             (symbol-name (car gnus-extra-headers))
6717             "Limit extra header:"
6718             (mapcar (lambda (x)
6719                       (cons (symbol-name x) x))
6720                     gnus-extra-headers)
6721             nil
6722             t))))
6723      (list header
6724            (read-string (format "Limit to header %s (regexp): " header)))))
6725   (when (not (equal "" regexp))
6726     (prog1
6727         (let ((articles (gnus-summary-find-matching
6728                          (cons 'extra header) regexp 'all)))
6729           (unless articles
6730             (error "Found no matches for \"%s\"" regexp))
6731           (gnus-summary-limit articles))
6732       (gnus-summary-position-point))))
6733
6734 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6735 (make-obsolete
6736  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6737
6738 (defun gnus-summary-limit-to-unread (&optional all)
6739   "Limit the summary buffer to articles that are not marked as read.
6740 If ALL is non-nil, limit strictly to unread articles."
6741   (interactive "P")
6742   (if all
6743       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6744     (gnus-summary-limit-to-marks
6745      ;; Concat all the marks that say that an article is read and have
6746      ;; those removed.
6747      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6748            gnus-killed-mark gnus-kill-file-mark
6749            gnus-low-score-mark gnus-expirable-mark
6750            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6751            gnus-duplicate-mark gnus-souped-mark)
6752      'reverse)))
6753
6754 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6755 (make-obsolete 'gnus-summary-delete-marked-with
6756                'gnus-summary-limit-exlude-marks)
6757
6758 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6759   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6760 If REVERSE, limit the summary buffer to articles that are marked
6761 with MARKS.  MARKS can either be a string of marks or a list of marks.
6762 Returns how many articles were removed."
6763   (interactive "sMarks: ")
6764   (gnus-summary-limit-to-marks marks t))
6765
6766 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6767   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6768 If REVERSE (the prefix), limit the summary buffer to articles that are
6769 not marked with MARKS.  MARKS can either be a string of marks or a
6770 list of marks.
6771 Returns how many articles were removed."
6772   (interactive "sMarks: \nP")
6773   (prog1
6774       (let ((data gnus-newsgroup-data)
6775             (marks (if (listp marks) marks
6776                      (append marks nil))) ; Transform to list.
6777             articles)
6778         (while data
6779           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6780                   (memq (gnus-data-mark (car data)) marks))
6781             (push (gnus-data-number (car data)) articles))
6782           (setq data (cdr data)))
6783         (gnus-summary-limit articles))
6784     (gnus-summary-position-point)))
6785
6786 (defun gnus-summary-limit-to-score (&optional score)
6787   "Limit to articles with score at or above SCORE."
6788   (interactive "P")
6789   (setq score (if score
6790                   (prefix-numeric-value score)
6791                 (or gnus-summary-default-score 0)))
6792   (let ((data gnus-newsgroup-data)
6793         articles)
6794     (while data
6795       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6796                 score)
6797         (push (gnus-data-number (car data)) articles))
6798       (setq data (cdr data)))
6799     (prog1
6800         (gnus-summary-limit articles)
6801       (gnus-summary-position-point))))
6802
6803 (defun gnus-summary-limit-include-thread (id)
6804   "Display all the hidden articles that is in the thread with ID in it.
6805 When called interactively, ID is the Message-ID of the current
6806 article."
6807   (interactive (list (mail-header-id (gnus-summary-article-header))))
6808   (let ((articles (gnus-articles-in-thread
6809                    (gnus-id-to-thread (gnus-root-id id)))))
6810     (prog1
6811         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6812         (gnus-summary-limit-include-matching-articles
6813          "subject"
6814          (regexp-quote (gnus-simplify-subject-re
6815                         (mail-header-subject (gnus-id-to-header id)))))
6816       (gnus-summary-position-point))))
6817
6818 (defun gnus-summary-limit-include-matching-articles (header regexp)
6819   "Display all the hidden articles that have HEADERs that match REGEXP."
6820   (interactive (list (read-string "Match on header: ")
6821                      (read-string "Regexp: ")))
6822   (let ((articles (gnus-find-matching-articles header regexp)))
6823     (prog1
6824         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6825       (gnus-summary-position-point))))
6826
6827 (defun gnus-summary-limit-include-dormant ()
6828   "Display all the hidden articles that are marked as dormant.
6829 Note that this command only works on a subset of the articles currently
6830 fetched for this group."
6831   (interactive)
6832   (unless gnus-newsgroup-dormant
6833     (error "There are no dormant articles in this group"))
6834   (prog1
6835       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6836     (gnus-summary-position-point)))
6837
6838 (defun gnus-summary-limit-exclude-dormant ()
6839   "Hide all dormant articles."
6840   (interactive)
6841   (prog1
6842       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6843     (gnus-summary-position-point)))
6844
6845 (defun gnus-summary-limit-exclude-childless-dormant ()
6846   "Hide all dormant articles that have no children."
6847   (interactive)
6848   (let ((data (gnus-data-list t))
6849         articles d children)
6850     ;; Find all articles that are either not dormant or have
6851     ;; children.
6852     (while (setq d (pop data))
6853       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6854                 (and (setq children
6855                            (gnus-article-children (gnus-data-number d)))
6856                      (let (found)
6857                        (while children
6858                          (when (memq (car children) articles)
6859                            (setq children nil
6860                                  found t))
6861                          (pop children))
6862                        found)))
6863         (push (gnus-data-number d) articles)))
6864     ;; Do the limiting.
6865     (prog1
6866         (gnus-summary-limit articles)
6867       (gnus-summary-position-point))))
6868
6869 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6870   "Mark all unread excluded articles as read.
6871 If ALL, mark even excluded ticked and dormants as read."
6872   (interactive "P")
6873   (let ((articles (gnus-sorted-complement
6874                    (sort
6875                     (mapcar (lambda (h) (mail-header-number h))
6876                             gnus-newsgroup-headers)
6877                     '<)
6878                    (sort gnus-newsgroup-limit '<)))
6879         article)
6880     (setq gnus-newsgroup-unreads
6881           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6882     (if all
6883         (setq gnus-newsgroup-dormant nil
6884               gnus-newsgroup-marked nil
6885               gnus-newsgroup-reads
6886               (nconc
6887                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6888                gnus-newsgroup-reads))
6889       (while (setq article (pop articles))
6890         (unless (or (memq article gnus-newsgroup-dormant)
6891                     (memq article gnus-newsgroup-marked))
6892           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6893
6894 (defun gnus-summary-limit (articles &optional pop)
6895   (if pop
6896       ;; We pop the previous limit off the stack and use that.
6897       (setq articles (car gnus-newsgroup-limits)
6898             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6899     ;; We use the new limit, so we push the old limit on the stack.
6900     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6901   ;; Set the limit.
6902   (setq gnus-newsgroup-limit articles)
6903   (let ((total (length gnus-newsgroup-data))
6904         (data (gnus-data-find-list (gnus-summary-article-number)))
6905         (gnus-summary-mark-below nil)   ; Inhibit this.
6906         found)
6907     ;; This will do all the work of generating the new summary buffer
6908     ;; according to the new limit.
6909     (gnus-summary-prepare)
6910     ;; Hide any threads, possibly.
6911     (and gnus-show-threads
6912          gnus-thread-hide-subtree
6913          (gnus-summary-hide-all-threads))
6914     ;; Try to return to the article you were at, or one in the
6915     ;; neighborhood.
6916     (when data
6917       ;; We try to find some article after the current one.
6918       (while data
6919         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6920           (setq data nil
6921                 found t))
6922         (setq data (cdr data))))
6923     (unless found
6924       ;; If there is no data, that means that we were after the last
6925       ;; article.  The same goes when we can't find any articles
6926       ;; after the current one.
6927       (goto-char (point-max))
6928       (gnus-summary-find-prev))
6929     (gnus-set-mode-line 'summary)
6930     ;; We return how many articles were removed from the summary
6931     ;; buffer as a result of the new limit.
6932     (- total (length gnus-newsgroup-data))))
6933
6934 (defsubst gnus-invisible-cut-children (threads)
6935   (let ((num 0))
6936     (while threads
6937       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6938         (incf num))
6939       (pop threads))
6940     (< num 2)))
6941
6942 (defsubst gnus-cut-thread (thread)
6943   "Go forwards in the thread until we find an article that we want to display."
6944   (when (or (eq gnus-fetch-old-headers 'some)
6945             (eq gnus-fetch-old-headers 'invisible)
6946             (numberp gnus-fetch-old-headers)
6947             (eq gnus-build-sparse-threads 'some)
6948             (eq gnus-build-sparse-threads 'more))
6949     ;; Deal with old-fetched headers and sparse threads.
6950     (while (and
6951             thread
6952             (or
6953              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6954              (gnus-summary-article-ancient-p
6955               (mail-header-number (car thread))))
6956             (if (or (<= (length (cdr thread)) 1)
6957                     (eq gnus-fetch-old-headers 'invisible))
6958                 (setq gnus-newsgroup-limit
6959                       (delq (mail-header-number (car thread))
6960                             gnus-newsgroup-limit)
6961                       thread (cadr thread))
6962               (when (gnus-invisible-cut-children (cdr thread))
6963                 (let ((th (cdr thread)))
6964                   (while th
6965                     (if (memq (mail-header-number (caar th))
6966                               gnus-newsgroup-limit)
6967                         (setq thread (car th)
6968                               th nil)
6969                       (setq th (cdr th))))))))))
6970   thread)
6971
6972 (defun gnus-cut-threads (threads)
6973   "Cut off all uninteresting articles from the beginning of threads."
6974   (when (or (eq gnus-fetch-old-headers 'some)
6975             (eq gnus-fetch-old-headers 'invisible)
6976             (numberp gnus-fetch-old-headers)
6977             (eq gnus-build-sparse-threads 'some)
6978             (eq gnus-build-sparse-threads 'more))
6979     (let ((th threads))
6980       (while th
6981         (setcar th (gnus-cut-thread (car th)))
6982         (setq th (cdr th)))))
6983   ;; Remove nixed out threads.
6984   (delq nil threads))
6985
6986 (defun gnus-summary-initial-limit (&optional show-if-empty)
6987   "Figure out what the initial limit is supposed to be on group entry.
6988 This entails weeding out unwanted dormants, low-scored articles,
6989 fetch-old-headers verbiage, and so on."
6990   ;; Most groups have nothing to remove.
6991   (if (or gnus-inhibit-limiting
6992           (and (null gnus-newsgroup-dormant)
6993                (not (eq gnus-fetch-old-headers 'some))
6994                (not (numberp gnus-fetch-old-headers))
6995                (not (eq gnus-fetch-old-headers 'invisible))
6996                (null gnus-summary-expunge-below)
6997                (not (eq gnus-build-sparse-threads 'some))
6998                (not (eq gnus-build-sparse-threads 'more))
6999                (null gnus-thread-expunge-below)
7000                (not gnus-use-nocem)))
7001       ()                                ; Do nothing.
7002     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7003     (setq gnus-newsgroup-limit nil)
7004     (mapatoms
7005      (lambda (node)
7006        (unless (car (symbol-value node))
7007          ;; These threads have no parents -- they are roots.
7008          (let ((nodes (cdr (symbol-value node)))
7009                thread)
7010            (while nodes
7011              (if (and gnus-thread-expunge-below
7012                       (< (gnus-thread-total-score (car nodes))
7013                          gnus-thread-expunge-below))
7014                  (gnus-expunge-thread (pop nodes))
7015                (setq thread (pop nodes))
7016                (gnus-summary-limit-children thread))))))
7017      gnus-newsgroup-dependencies)
7018     ;; If this limitation resulted in an empty group, we might
7019     ;; pop the previous limit and use it instead.
7020     (when (and (not gnus-newsgroup-limit)
7021                show-if-empty)
7022       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7023     gnus-newsgroup-limit))
7024
7025 (defun gnus-summary-limit-children (thread)
7026   "Return 1 if this subthread is visible and 0 if it is not."
7027   ;; First we get the number of visible children to this thread.  This
7028   ;; is done by recursing down the thread using this function, so this
7029   ;; will really go down to a leaf article first, before slowly
7030   ;; working its way up towards the root.
7031   (when thread
7032     (let ((children
7033            (if (cdr thread)
7034                (apply '+ (mapcar 'gnus-summary-limit-children
7035                                  (cdr thread)))
7036              0))
7037           (number (mail-header-number (car thread)))
7038           score)
7039       (if (and
7040            (not (memq number gnus-newsgroup-marked))
7041            (or
7042             ;; If this article is dormant and has absolutely no visible
7043             ;; children, then this article isn't visible.
7044             (and (memq number gnus-newsgroup-dormant)
7045                  (zerop children))
7046             ;; If this is "fetch-old-headered" and there is no
7047             ;; visible children, then we don't want this article.
7048             (and (or (eq gnus-fetch-old-headers 'some)
7049                      (numberp gnus-fetch-old-headers))
7050                  (gnus-summary-article-ancient-p number)
7051                  (zerop children))
7052             ;; If this is "fetch-old-headered" and `invisible', then
7053             ;; we don't want this article.
7054             (and (eq gnus-fetch-old-headers 'invisible)
7055                  (gnus-summary-article-ancient-p number))
7056             ;; If this is a sparsely inserted article with no children,
7057             ;; we don't want it.
7058             (and (eq gnus-build-sparse-threads 'some)
7059                  (gnus-summary-article-sparse-p number)
7060                  (zerop children))
7061             ;; If we use expunging, and this article is really
7062             ;; low-scored, then we don't want this article.
7063             (when (and gnus-summary-expunge-below
7064                        (< (setq score
7065                                 (or (cdr (assq number gnus-newsgroup-scored))
7066                                     gnus-summary-default-score))
7067                           gnus-summary-expunge-below))
7068               ;; We increase the expunge-tally here, but that has
7069               ;; nothing to do with the limits, really.
7070               (incf gnus-newsgroup-expunged-tally)
7071               ;; We also mark as read here, if that's wanted.
7072               (when (and gnus-summary-mark-below
7073                          (< score gnus-summary-mark-below))
7074                 (setq gnus-newsgroup-unreads
7075                       (delq number gnus-newsgroup-unreads))
7076                 (if gnus-newsgroup-auto-expire
7077                     (push number gnus-newsgroup-expirable)
7078                   (push (cons number gnus-low-score-mark)
7079                         gnus-newsgroup-reads)))
7080               t)
7081             ;; Check NoCeM things.
7082             (if (and gnus-use-nocem
7083                      (gnus-nocem-unwanted-article-p
7084                       (mail-header-id (car thread))))
7085                 (progn
7086                   (setq gnus-newsgroup-unreads
7087                         (delq number gnus-newsgroup-unreads))
7088                   t))))
7089           ;; Nope, invisible article.
7090           0
7091         ;; Ok, this article is to be visible, so we add it to the limit
7092         ;; and return 1.
7093         (push number gnus-newsgroup-limit)
7094         1))))
7095
7096 (defun gnus-expunge-thread (thread)
7097   "Mark all articles in THREAD as read."
7098   (let* ((number (mail-header-number (car thread))))
7099     (incf gnus-newsgroup-expunged-tally)
7100     ;; We also mark as read here, if that's wanted.
7101     (setq gnus-newsgroup-unreads
7102           (delq number gnus-newsgroup-unreads))
7103     (if gnus-newsgroup-auto-expire
7104         (push number gnus-newsgroup-expirable)
7105       (push (cons number gnus-low-score-mark)
7106             gnus-newsgroup-reads)))
7107   ;; Go recursively through all subthreads.
7108   (mapcar 'gnus-expunge-thread (cdr thread)))
7109
7110 ;; Summary article oriented commands
7111
7112 (defun gnus-summary-refer-parent-article (n)
7113   "Refer parent article N times.
7114 If N is negative, go to ancestor -N instead.
7115 The difference between N and the number of articles fetched is returned."
7116   (interactive "p")
7117   (let ((skip 1)
7118         error header ref)
7119     (when (not (natnump n))
7120       (setq skip (abs n)
7121             n 1))
7122     (while (and (> n 0)
7123                 (not error))
7124       (setq header (gnus-summary-article-header))
7125       (if (and (eq (mail-header-number header)
7126                    (cdr gnus-article-current))
7127                (equal gnus-newsgroup-name
7128                       (car gnus-article-current)))
7129           ;; If we try to find the parent of the currently
7130           ;; displayed article, then we take a look at the actual
7131           ;; References header, since this is slightly more
7132           ;; reliable than the References field we got from the
7133           ;; server.
7134           (save-excursion
7135             (set-buffer gnus-original-article-buffer)
7136             (nnheader-narrow-to-headers)
7137             (unless (setq ref (message-fetch-field "references"))
7138               (setq ref (message-fetch-field "in-reply-to")))
7139             (widen))
7140         (setq ref
7141               ;; It's not the current article, so we take a bet on
7142               ;; the value we got from the server.
7143               (mail-header-references header)))
7144       (if (and ref
7145                (not (equal ref "")))
7146           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7147             (gnus-message 1 "Couldn't find parent"))
7148         (gnus-message 1 "No references in article %d"
7149                       (gnus-summary-article-number))
7150         (setq error t))
7151       (decf n))
7152     (gnus-summary-position-point)
7153     n))
7154
7155 (defun gnus-summary-refer-references ()
7156   "Fetch all articles mentioned in the References header.
7157 Return the number of articles fetched."
7158   (interactive)
7159   (let ((ref (mail-header-references (gnus-summary-article-header)))
7160         (current (gnus-summary-article-number))
7161         (n 0))
7162     (if (or (not ref)
7163             (equal ref ""))
7164         (error "No References in the current article")
7165       ;; For each Message-ID in the References header...
7166       (while (string-match "<[^>]*>" ref)
7167         (incf n)
7168         ;; ... fetch that article.
7169         (gnus-summary-refer-article
7170          (prog1 (match-string 0 ref)
7171            (setq ref (substring ref (match-end 0))))))
7172       (gnus-summary-goto-subject current)
7173       (gnus-summary-position-point)
7174       n)))
7175
7176 (defun gnus-summary-refer-thread (&optional limit)
7177   "Fetch all articles in the current thread.
7178 If LIMIT (the numerical prefix), fetch that many old headers instead
7179 of what's specified by the `gnus-refer-thread-limit' variable."
7180   (interactive "P")
7181   (let ((id (mail-header-id (gnus-summary-article-header)))
7182         (limit (if limit (prefix-numeric-value limit)
7183                  gnus-refer-thread-limit)))
7184     ;; We want to fetch LIMIT *old* headers, but we also have to
7185     ;; re-fetch all the headers in the current buffer, because many of
7186     ;; them may be undisplayed.  So we adjust LIMIT.
7187     (when (numberp limit)
7188       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7189     (unless (eq gnus-fetch-old-headers 'invisible)
7190       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7191       ;; Retrieve the headers and read them in.
7192       (if (eq (gnus-retrieve-headers
7193                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7194               'nov)
7195           (gnus-build-all-threads)
7196         (error "Can't fetch thread from backends that don't support NOV"))
7197       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7198     (gnus-summary-limit-include-thread id)))
7199
7200 (defun gnus-summary-refer-article (message-id)
7201   "Fetch an article specified by MESSAGE-ID."
7202   (interactive "sMessage-ID: ")
7203   (when (and (stringp message-id)
7204              (not (zerop (length message-id))))
7205     ;; Construct the correct Message-ID if necessary.
7206     ;; Suggested by tale@pawl.rpi.edu.
7207     (unless (string-match "^<" message-id)
7208       (setq message-id (concat "<" message-id)))
7209     (unless (string-match ">$" message-id)
7210       (setq message-id (concat message-id ">")))
7211     (let* ((header (gnus-id-to-header message-id))
7212            (sparse (and header
7213                         (gnus-summary-article-sparse-p
7214                          (mail-header-number header))
7215                         (memq (mail-header-number header)
7216                               gnus-newsgroup-limit)))
7217            number)
7218       (cond
7219        ;; If the article is present in the buffer we just go to it.
7220        ((and header
7221              (or (not (gnus-summary-article-sparse-p
7222                        (mail-header-number header)))
7223                  sparse))
7224         (prog1
7225             (gnus-summary-goto-article
7226              (mail-header-number header) nil t)
7227           (when sparse
7228             (gnus-summary-update-article (mail-header-number header)))))
7229        (t
7230         ;; We fetch the article.
7231         (catch 'found
7232           (dolist (gnus-override-method (gnus-refer-article-methods))
7233             (gnus-check-server gnus-override-method)
7234             ;; Fetch the header, and display the article.
7235             (when (setq number (gnus-summary-insert-subject message-id))
7236               (gnus-summary-select-article nil nil nil number)
7237               (throw 'found t)))
7238           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7239
7240 (defun gnus-refer-article-methods ()
7241   "Return a list of referrable methods."
7242   (cond
7243    ;; No method, so we default to current and native.
7244    ((null gnus-refer-article-method)
7245     (list gnus-current-select-method gnus-select-method))
7246    ;; Current.
7247    ((eq 'current gnus-refer-article-method)
7248     (list gnus-current-select-method))
7249    ;; List of select methods.
7250    ((not (and (symbolp (car gnus-refer-article-method))
7251               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7252     (let (out)
7253       (dolist (method gnus-refer-article-method)
7254         (push (if (eq 'current method)
7255                   gnus-current-select-method
7256                 method)
7257               out))
7258       (nreverse out)))
7259    ;; One single select method.
7260    (t
7261     (list gnus-refer-article-method))))
7262
7263 (defun gnus-summary-edit-parameters ()
7264   "Edit the group parameters of the current group."
7265   (interactive)
7266   (gnus-group-edit-group gnus-newsgroup-name 'params))
7267
7268 (defun gnus-summary-customize-parameters ()
7269   "Customize the group parameters of the current group."
7270   (interactive)
7271   (gnus-group-customize gnus-newsgroup-name))
7272
7273 (defun gnus-summary-enter-digest-group (&optional force)
7274   "Enter an nndoc group based on the current article.
7275 If FORCE, force a digest interpretation.  If not, try
7276 to guess what the document format is."
7277   (interactive "P")
7278   (let ((conf gnus-current-window-configuration))
7279     (save-excursion
7280       (gnus-summary-select-article))
7281     (setq gnus-current-window-configuration conf)
7282     (let* ((name (format "%s-%d"
7283                          (gnus-group-prefixed-name
7284                           gnus-newsgroup-name (list 'nndoc ""))
7285                          (save-excursion
7286                            (set-buffer gnus-summary-buffer)
7287                            gnus-current-article)))
7288            (ogroup gnus-newsgroup-name)
7289            (params (append (gnus-info-params (gnus-get-info ogroup))
7290                            (list (cons 'to-group ogroup))
7291                            (list (cons 'save-article-group ogroup))))
7292            (case-fold-search t)
7293            (buf (current-buffer))
7294            dig to-address)
7295       (save-excursion
7296         (set-buffer gnus-original-article-buffer)
7297         ;; Have the digest group inherit the main mail address of
7298         ;; the parent article.
7299         (when (setq to-address (or (message-fetch-field "reply-to")
7300                                    (message-fetch-field "from")))
7301           (setq params (append
7302                         (list (cons 'to-address
7303                                     (funcall gnus-decode-encoded-word-function
7304                                              to-address))))))
7305         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7306         (insert-buffer-substring gnus-original-article-buffer)
7307         ;; Remove lines that may lead nndoc to misinterpret the
7308         ;; document type.
7309         (narrow-to-region
7310          (goto-char (point-min))
7311          (or (search-forward "\n\n" nil t) (point)))
7312         (goto-char (point-min))
7313         (delete-matching-lines "^Path:\\|^From ")
7314         (widen))
7315       (unwind-protect
7316           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7317                     (gnus-newsgroup-ephemeral-ignored-charsets
7318                      gnus-newsgroup-ignored-charsets))
7319                 (gnus-group-read-ephemeral-group
7320                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7321                               (nndoc-article-type
7322                                ,(if force 'mbox 'guess))) t))
7323               ;; Make all postings to this group go to the parent group.
7324               (nconc (gnus-info-params (gnus-get-info name))
7325                      params)
7326             ;; Couldn't select this doc group.
7327             (switch-to-buffer buf)
7328             (gnus-set-global-variables)
7329             (gnus-configure-windows 'summary)
7330             (gnus-message 3 "Article couldn't be entered?"))
7331         (kill-buffer dig)))))
7332
7333 (defun gnus-summary-read-document (n)
7334   "Open a new group based on the current article(s).
7335 This will allow you to read digests and other similar
7336 documents as newsgroups.
7337 Obeys the standard process/prefix convention."
7338   (interactive "P")
7339   (let* ((articles (gnus-summary-work-articles n))
7340          (ogroup gnus-newsgroup-name)
7341          (params (append (gnus-info-params (gnus-get-info ogroup))
7342                          (list (cons 'to-group ogroup))))
7343          article group egroup groups vgroup)
7344     (while (setq article (pop articles))
7345       (setq group (format "%s-%d" gnus-newsgroup-name article))
7346       (gnus-summary-remove-process-mark article)
7347       (when (gnus-summary-display-article article)
7348         (save-excursion
7349           (with-temp-buffer
7350             (insert-buffer-substring gnus-original-article-buffer)
7351             ;; Remove some headers that may lead nndoc to make
7352             ;; the wrong guess.
7353             (message-narrow-to-head)
7354             (goto-char (point-min))
7355             (delete-matching-lines "^\\(Path\\):\\|^From ")
7356             (widen)
7357             (if (setq egroup
7358                       (gnus-group-read-ephemeral-group
7359                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7360                                      (nndoc-article-type guess))
7361                        t nil t))
7362                 (progn
7363                   ;; Make all postings to this group go to the parent group.
7364                   (nconc (gnus-info-params (gnus-get-info egroup))
7365                          params)
7366                   (push egroup groups))
7367               ;; Couldn't select this doc group.
7368               (gnus-error 3 "Article couldn't be entered"))))))
7369     ;; Now we have selected all the documents.
7370     (cond
7371      ((not groups)
7372       (error "None of the articles could be interpreted as documents"))
7373      ((gnus-group-read-ephemeral-group
7374        (setq vgroup (format
7375                      "nnvirtual:%s-%s" gnus-newsgroup-name
7376                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7377        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7378        t
7379        (cons (current-buffer) 'summary)))
7380      (t
7381       (error "Couldn't select virtual nndoc group")))))
7382
7383 (defun gnus-summary-isearch-article (&optional regexp-p)
7384   "Do incremental search forward on the current article.
7385 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7386   (interactive "P")
7387   (let* ((gnus-inhibit-treatment t)
7388          (old (gnus-summary-select-article)))
7389     (gnus-configure-windows 'article)
7390     (gnus-eval-in-buffer-window gnus-article-buffer
7391       (save-restriction
7392         (widen)
7393         (when (eq 'old old)
7394           (gnus-article-show-all-headers))
7395         (goto-char (point-min))
7396         (isearch-forward regexp-p)))))
7397
7398 (defun gnus-summary-search-article-forward (regexp &optional backward)
7399   "Search for an article containing REGEXP forward.
7400 If BACKWARD, search backward instead."
7401   (interactive
7402    (list (read-string
7403           (format "Search article %s (regexp%s): "
7404                   (if current-prefix-arg "backward" "forward")
7405                   (if gnus-last-search-regexp
7406                       (concat ", default " gnus-last-search-regexp)
7407                     "")))
7408          current-prefix-arg))
7409   (if (string-equal regexp "")
7410       (setq regexp (or gnus-last-search-regexp ""))
7411     (setq gnus-last-search-regexp regexp)
7412     (setq gnus-article-before-search gnus-current-article))
7413   ;; Intentionally set gnus-last-article.
7414   (setq gnus-last-article gnus-article-before-search)
7415   (let ((gnus-last-article gnus-last-article))
7416     (if (gnus-summary-search-article regexp backward)
7417         (gnus-summary-show-thread)
7418       (error "Search failed: \"%s\"" regexp))))
7419
7420 (defun gnus-summary-search-article-backward (regexp)
7421   "Search for an article containing REGEXP backward."
7422   (interactive
7423    (list (read-string
7424           (format "Search article backward (regexp%s): "
7425                   (if gnus-last-search-regexp
7426                       (concat ", default " gnus-last-search-regexp)
7427                     "")))))
7428   (gnus-summary-search-article-forward regexp 'backward))
7429
7430 (eval-when-compile
7431   (defmacro gnus-summary-search-article-position-point (regexp backward)
7432     "Dehighlight the last matched text and goto the beginning position."
7433     (` (if (and gnus-summary-search-article-matched-data
7434                 (let ((text (caddr gnus-summary-search-article-matched-data))
7435                       (inhibit-read-only t)
7436                       buffer-read-only)
7437                   (delete-region
7438                    (goto-char (car gnus-summary-search-article-matched-data))
7439                    (cadr gnus-summary-search-article-matched-data))
7440                   (insert text)
7441                   (string-match (, regexp) text)))
7442            (if (, backward) (beginning-of-line) (end-of-line))
7443          (goto-char (if (, backward) (point-max) (point-min))))))
7444
7445   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7446     "Place point where X-Face image is displayed."
7447     (if (featurep 'xemacs)
7448         (` (let ((end (if (search-forward "\n\n" nil t)
7449                           (goto-char (1- (point)))
7450                         (point-min)))
7451                  extent)
7452              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7453              (unless (and (re-search-forward "^From:" end t)
7454                           (setq extent (extent-at (point)))
7455                           (extent-begin-glyph extent))
7456                (goto-char (, opoint)))))
7457       (` (let ((end (if (search-forward "\n\n" nil t)
7458                         (goto-char (1- (point)))
7459                       (point-min)))
7460                (start (or (search-backward "\n\n" nil t) (point-min))))
7461            (goto-char
7462             (or (text-property-any start end 'x-face-image t);; x-face-e21
7463                 (text-property-any start end 'x-face-mule-bitmap-image t)
7464                 (, opoint)))))))
7465
7466   (defmacro gnus-summary-search-article-highlight-matched-text
7467     (backward treated x-face)
7468     "Highlight matched text in the function `gnus-summary-search-article'."
7469     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7470              (end (set-marker (make-marker) (match-end 0)))
7471              (inhibit-read-only t)
7472              buffer-read-only)
7473          (unless treated
7474            (let ((,@
7475                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7476                     (mapcar
7477                      (lambda (item) (setq items (delq item items)))
7478                      '(gnus-treat-buttonize
7479                        gnus-treat-fill-article
7480                        gnus-treat-fill-long-lines
7481                        gnus-treat-emphasize
7482                        gnus-treat-highlight-headers
7483                        gnus-treat-highlight-citation
7484                        gnus-treat-highlight-signature
7485                        gnus-treat-overstrike
7486                        gnus-treat-display-xface
7487                        gnus-treat-buttonize-head
7488                        gnus-treat-decode-article-as-default-mime-charset))
7489                     (static-if (featurep 'xemacs)
7490                         items
7491                       (cons '(x-face-mule-delete-x-face-field
7492                               (quote never))
7493                             items))))
7494                  (gnus-treat-display-xface
7495                   (when (, x-face) gnus-treat-display-xface)))
7496              (gnus-article-prepare-mime-display)))
7497          (goto-char (if (, backward) start end))
7498          (when (, x-face)
7499            (gnus-summary-search-article-highlight-goto-x-face (point)))
7500          (setq gnus-summary-search-article-matched-data
7501                (list start end (buffer-substring start end)))
7502          (unless (eq start end);; matched text has been deleted. :-<
7503            (put-text-property start end 'face
7504                               (or (find-face 'isearch)
7505                                   'secondary-selection))))))
7506   )
7507
7508 (defun gnus-summary-search-article (regexp &optional backward)
7509   "Search for an article containing REGEXP.
7510 Optional argument BACKWARD means do search for backward.
7511 `gnus-select-article-hook' is not called during the search."
7512   ;; We have to require this here to make sure that the following
7513   ;; dynamic binding isn't shadowed by autoloading.
7514   (require 'gnus-async)
7515   (require 'gnus-art)
7516   (let ((gnus-select-article-hook nil)  ;Disable hook.
7517         (gnus-article-prepare-hook nil)
7518         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7519         (gnus-use-article-prefetch nil)
7520         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7521         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7522         (sum (current-buffer))
7523         (found nil)
7524         point treated)
7525     (gnus-save-hidden-threads
7526       (static-if (featurep 'xemacs)
7527           (let ((gnus-inhibit-treatment t))
7528             (setq treated (eq 'old (gnus-summary-select-article)))
7529             (when (and treated
7530                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7531                                  (window-live-p (get-buffer-window
7532                                                  gnus-article-buffer t)))))
7533               (gnus-summary-select-article nil t)
7534               (setq treated nil)))
7535         (let ((gnus-inhibit-treatment t)
7536               (x-face-mule-delete-x-face-field 'never))
7537           (setq treated (eq 'old (gnus-summary-select-article)))
7538           (when (and treated
7539                      (not
7540                       (and (gnus-buffer-live-p gnus-article-buffer)
7541                            (window-live-p (get-buffer-window
7542                                            gnus-article-buffer t))
7543                            (or (not (string-match "^\\^X-Face:" regexp))
7544                                (with-current-buffer gnus-article-buffer
7545                                  gnus-summary-search-article-matched-data)))))
7546             (gnus-summary-select-article nil t)
7547             (setq treated nil))))
7548       (set-buffer gnus-article-buffer)
7549       (widen)
7550       (if treated
7551           (progn
7552             (gnus-article-show-all-headers)
7553             (gnus-summary-search-article-position-point regexp backward))
7554         (goto-char (if backward (point-max) (point-min))))
7555       (while (not found)
7556         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7557         (if (if backward
7558                 (re-search-backward regexp nil t)
7559               (re-search-forward regexp nil t))
7560             ;; We found the regexp.
7561             (progn
7562               (gnus-summary-search-article-highlight-matched-text
7563                backward treated (string-match "^\\^X-Face:" regexp))
7564               (setq found 'found)
7565               (forward-line
7566                (/ (- 2 (window-height
7567                         (get-buffer-window gnus-article-buffer t)))
7568                   2))
7569               (set-window-start
7570                (get-buffer-window (current-buffer))
7571                (point))
7572               (set-buffer sum)
7573               (setq point (point)))
7574           ;; We didn't find it, so we go to the next article.
7575           (set-buffer sum)
7576           (setq found 'not)
7577           (while (eq found 'not)
7578             (if (not (if backward (gnus-summary-find-prev)
7579                        (gnus-summary-find-next)))
7580                 ;; No more articles.
7581                 (setq found t)
7582               ;; Select the next article and adjust point.
7583               (unless (gnus-summary-article-sparse-p
7584                        (gnus-summary-article-number))
7585                 (setq found nil)
7586                 (let ((gnus-inhibit-treatment t))
7587                   (gnus-summary-select-article))
7588                 (setq treated nil)
7589                 (set-buffer gnus-article-buffer)
7590                 (widen)
7591                 (goto-char (if backward (point-max) (point-min))))))))
7592       (gnus-message 7 ""))
7593     ;; Return whether we found the regexp.
7594     (when (eq found 'found)
7595       (goto-char point)
7596       (gnus-summary-show-thread)
7597       (gnus-summary-goto-subject gnus-current-article)
7598       (gnus-summary-position-point)
7599       t)))
7600
7601 (defun gnus-find-matching-articles (header regexp)
7602   "Return a list of all articles that match REGEXP on HEADER.
7603 This search includes all articles in the current group that Gnus has
7604 fetched headers for, whether they are displayed or not."
7605   (let ((articles nil)
7606         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7607         (case-fold-search t))
7608     (dolist (header gnus-newsgroup-headers)
7609       (when (string-match regexp (funcall func header))
7610         (push (mail-header-number header) articles)))
7611     (nreverse articles)))
7612
7613 (defun gnus-summary-find-matching (header regexp &optional backward unread
7614                                           not-case-fold)
7615   "Return a list of all articles that match REGEXP on HEADER.
7616 The search stars on the current article and goes forwards unless
7617 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7618 If UNREAD is non-nil, only unread articles will
7619 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7620 in the comparisons."
7621   (let ((case-fold-search (not not-case-fold))
7622         articles d func)
7623     (if (consp header)
7624         (if (eq (car header) 'extra)
7625             (setq func
7626                   `(lambda (h)
7627                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7628                          "")))
7629           (error "%s is an invalid header" header))
7630       (unless (fboundp (intern (concat "mail-header-" header)))
7631         (error "%s is not a valid header" header))
7632       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7633     (dolist (d (if (eq backward 'all)
7634                    gnus-newsgroup-data
7635                  (gnus-data-find-list
7636                   (gnus-summary-article-number)
7637                   (gnus-data-list backward))))
7638       (when (and (or (not unread)       ; We want all articles...
7639                      (gnus-data-unread-p d)) ; Or just unreads.
7640                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7641                  (string-match regexp
7642                                (funcall func (gnus-data-header d)))) ; Match.
7643         (push (gnus-data-number d) articles))) ; Success!
7644     (nreverse articles)))
7645
7646 (defun gnus-summary-execute-command (header regexp command &optional backward)
7647   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7648 If HEADER is an empty string (or nil), the match is done on the entire
7649 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7650   (interactive
7651    (list (let ((completion-ignore-case t))
7652            (completing-read
7653             "Header name: "
7654             (mapcar (lambda (header) (list (format "%s" header)))
7655                     (append
7656                      '("Number" "Subject" "From" "Lines" "Date"
7657                        "Message-ID" "Xref" "References" "Body")
7658                      gnus-extra-headers))
7659             nil 'require-match))
7660          (read-string "Regexp: ")
7661          (read-key-sequence "Command: ")
7662          current-prefix-arg))
7663   (when (equal header "Body")
7664     (setq header ""))
7665   ;; Hidden thread subtrees must be searched as well.
7666   (gnus-summary-show-all-threads)
7667   ;; We don't want to change current point nor window configuration.
7668   (save-excursion
7669     (save-window-excursion
7670       (gnus-message 6 "Executing %s..." (key-description command))
7671       ;; We'd like to execute COMMAND interactively so as to give arguments.
7672       (gnus-execute header regexp
7673                     `(call-interactively ',(key-binding command))
7674                     backward)
7675       (gnus-message 6 "Executing %s...done" (key-description command)))))
7676
7677 (defun gnus-summary-beginning-of-article ()
7678   "Scroll the article back to the beginning."
7679   (interactive)
7680   (gnus-summary-select-article)
7681   (gnus-configure-windows 'article)
7682   (gnus-eval-in-buffer-window gnus-article-buffer
7683     (widen)
7684     (goto-char (point-min))
7685     (when gnus-page-broken
7686       (gnus-narrow-to-page))))
7687
7688 (defun gnus-summary-end-of-article ()
7689   "Scroll to the end of the article."
7690   (interactive)
7691   (gnus-summary-select-article)
7692   (gnus-configure-windows 'article)
7693   (gnus-eval-in-buffer-window gnus-article-buffer
7694     (widen)
7695     (goto-char (point-max))
7696     (recenter -3)
7697     (when gnus-page-broken
7698       (gnus-narrow-to-page))))
7699
7700 (defun gnus-summary-print-article (&optional filename n)
7701   "Generate and print a PostScript image of the N next (mail) articles.
7702
7703 If N is negative, print the N previous articles.  If N is nil and articles
7704 have been marked with the process mark, print these instead.
7705
7706 If the optional first argument FILENAME is nil, send the image to the
7707 printer.  If FILENAME is a string, save the PostScript image in a file with
7708 that name.  If FILENAME is a number, prompt the user for the name of the file
7709 to save in."
7710   (interactive (list (ps-print-preprint current-prefix-arg)))
7711   (dolist (article (gnus-summary-work-articles n))
7712     (gnus-summary-select-article nil nil 'pseudo article)
7713     (gnus-eval-in-buffer-window gnus-article-buffer
7714       (let ((buffer (generate-new-buffer " *print*")))
7715         (unwind-protect
7716             (progn
7717               (copy-to-buffer buffer (point-min) (point-max))
7718               (set-buffer buffer)
7719               (gnus-article-delete-invisible-text)
7720               (when (gnus-visual-p 'article-highlight 'highlight)
7721                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7722                 ;; highlight.
7723                 (let ((gnus-article-buffer buffer))
7724                   (gnus-article-highlight-citation t)
7725                   (gnus-article-highlight-signature)))
7726               (let ((ps-left-header
7727                      (list
7728                       (concat "("
7729                               (mail-header-subject gnus-current-headers) ")")
7730                       (concat "("
7731                               (mail-header-from gnus-current-headers) ")")))
7732                     (ps-right-header
7733                      (list
7734                       "/pagenumberstring load"
7735                       (concat "("
7736                               (mail-header-date gnus-current-headers) ")"))))
7737                 (gnus-run-hooks 'gnus-ps-print-hook)
7738                 (save-excursion
7739                   (if window-system
7740                       (ps-spool-buffer-with-faces)
7741                     (ps-spool-buffer)))))
7742           (kill-buffer buffer))))
7743     (gnus-summary-remove-process-mark article))
7744   (ps-despool filename))
7745
7746 (defun gnus-summary-show-article (&optional arg)
7747   "Force re-fetching of the current article.
7748 If ARG (the prefix) is a number, show the article with the charset
7749 defined in `gnus-summary-show-article-charset-alist', or the charset
7750 inputed.
7751 If ARG (the prefix) is non-nil and not a number, show the raw article
7752 without any article massaging functions being run."
7753   (interactive "P")
7754   (cond
7755    ((numberp arg)
7756     (let ((gnus-newsgroup-charset
7757            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7758                (read-coding-system "Charset: ")))
7759           (gnus-newsgroup-ignored-charsets 'gnus-all))
7760       (gnus-summary-select-article nil 'force)
7761       (let ((deps gnus-newsgroup-dependencies)
7762             head header)
7763         (save-excursion
7764           (set-buffer gnus-original-article-buffer)
7765           (save-restriction
7766             (message-narrow-to-head)
7767             (setq head (buffer-string)))
7768           (with-temp-buffer
7769             (insert (format "211 %d Article retrieved.\n"
7770                             (cdr gnus-article-current)))
7771             (insert head)
7772             (insert ".\n")
7773             (let ((nntp-server-buffer (current-buffer)))
7774               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7775         (gnus-data-set-header
7776          (gnus-data-find (cdr gnus-article-current))
7777          header)
7778         (gnus-summary-update-article-line
7779          (cdr gnus-article-current) header))))
7780    ((not arg)
7781     ;; Select the article the normal way.
7782     (gnus-summary-select-article nil 'force))
7783    (t
7784     ;; We have to require this here to make sure that the following
7785     ;; dynamic binding isn't shadowed by autoloading.
7786     (require 'gnus-async)
7787     (require 'gnus-art)
7788     ;; Bind the article treatment functions to nil.
7789     (let ((gnus-have-all-headers t)
7790           gnus-article-prepare-hook
7791           gnus-article-decode-hook
7792           gnus-break-pages
7793           gnus-show-mime
7794           (gnus-inhibit-treatment t))
7795       (gnus-summary-select-article nil 'force))))
7796   (gnus-summary-goto-subject gnus-current-article)
7797   (gnus-summary-position-point))
7798
7799 (defun gnus-summary-verbose-headers (&optional arg)
7800   "Toggle permanent full header display.
7801 If ARG is a positive number, turn header display on.
7802 If ARG is a negative number, turn header display off."
7803   (interactive "P")
7804   (setq gnus-show-all-headers
7805         (cond ((or (not (numberp arg))
7806                    (zerop arg))
7807                (not gnus-show-all-headers))
7808               ((natnump arg)
7809                t)))
7810   (gnus-summary-show-article))
7811
7812 (defun gnus-summary-toggle-header (&optional arg)
7813   "Show the headers if they are hidden, or hide them if they are shown.
7814 If ARG is a positive number, show the entire header.
7815 If ARG is a negative number, hide the unwanted header lines."
7816   (interactive "P")
7817   (save-excursion
7818     (set-buffer gnus-article-buffer)
7819     (save-restriction
7820       (let* ((buffer-read-only nil)
7821              (inhibit-point-motion-hooks t)
7822              hidden e)
7823         (setq hidden
7824               (if (numberp arg)
7825                   (>= arg 0)
7826                 (save-restriction
7827                   (article-narrow-to-head)
7828                   (gnus-article-hidden-text-p 'headers))))
7829         (goto-char (point-min))
7830         (when (search-forward "\n\n" nil t)
7831           (delete-region (point-min) (1- (point))))
7832         (goto-char (point-min))
7833         (save-excursion
7834           (set-buffer gnus-original-article-buffer)
7835           (goto-char (point-min))
7836           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7837         (insert-buffer-substring gnus-original-article-buffer 1 e)
7838         (save-restriction
7839           (narrow-to-region (point-min) (point))
7840           (article-decode-encoded-words)
7841           (if  hidden
7842               (let ((gnus-treat-hide-headers nil)
7843                     (gnus-treat-hide-boring-headers nil))
7844                 (setq gnus-article-wash-types
7845                       (delq 'headers gnus-article-wash-types))
7846                 (gnus-treat-article 'head))
7847             (gnus-treat-article 'head)))
7848         (gnus-set-mode-line 'article)))))
7849
7850 (defun gnus-summary-show-all-headers ()
7851   "Make all header lines visible."
7852   (interactive)
7853   (gnus-summary-toggle-header 1))
7854
7855 (defun gnus-summary-toggle-mime (&optional arg)
7856   "Toggle MIME processing.
7857 If ARG is a positive number, turn MIME processing on."
7858   (interactive "P")
7859   (setq gnus-show-mime
7860         (if (null arg)
7861             (not gnus-show-mime)
7862           (> (prefix-numeric-value arg) 0)))
7863   (gnus-summary-select-article t 'force))
7864
7865 (defun gnus-summary-caesar-message (&optional arg)
7866   "Caesar rotate the current article by 13.
7867 The numerical prefix specifies how many places to rotate each letter
7868 forward."
7869   (interactive "P")
7870   (gnus-summary-select-article)
7871   (let ((mail-header-separator ""))
7872     (gnus-eval-in-buffer-window gnus-article-buffer
7873       (save-restriction
7874         (widen)
7875         (let ((start (window-start))
7876               buffer-read-only)
7877           (message-caesar-buffer-body arg)
7878           (set-window-start (get-buffer-window (current-buffer)) start))))))
7879
7880 (defun gnus-summary-stop-page-breaking ()
7881   "Stop page breaking in the current article."
7882   (interactive)
7883   (gnus-summary-select-article)
7884   (gnus-eval-in-buffer-window gnus-article-buffer
7885     (widen)
7886     (when (gnus-visual-p 'page-marker)
7887       (let ((buffer-read-only nil))
7888         (gnus-remove-text-with-property 'gnus-prev)
7889         (gnus-remove-text-with-property 'gnus-next))
7890       (setq gnus-page-broken nil))))
7891
7892 (defun gnus-summary-move-article (&optional n to-newsgroup
7893                                             select-method action)
7894   "Move the current article to a different newsgroup.
7895 If N is a positive number, move the N next articles.
7896 If N is a negative number, move the N previous articles.
7897 If N is nil and any articles have been marked with the process mark,
7898 move those articles instead.
7899 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7900 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7901 re-spool using this method.
7902
7903 For this function to work, both the current newsgroup and the
7904 newsgroup that you want to move to have to support the `request-move'
7905 and `request-accept' functions.
7906
7907 ACTION can be either `move' (the default), `crosspost' or `copy'."
7908   (interactive "P")
7909   (unless action
7910     (setq action 'move))
7911   ;; Disable marking as read.
7912   (let (gnus-mark-article-hook)
7913     (save-window-excursion
7914       (gnus-summary-select-article)))
7915   ;; Check whether the source group supports the required functions.
7916   (cond ((and (eq action 'move)
7917               (not (gnus-check-backend-function
7918                     'request-move-article gnus-newsgroup-name)))
7919          (error "The current group does not support article moving"))
7920         ((and (eq action 'crosspost)
7921               (not (gnus-check-backend-function
7922                     'request-replace-article gnus-newsgroup-name)))
7923          (error "The current group does not support article editing")))
7924   (let ((articles (gnus-summary-work-articles n))
7925         (prefix (if (gnus-check-backend-function
7926                     'request-move-article gnus-newsgroup-name)
7927                     (gnus-group-real-prefix gnus-newsgroup-name)
7928                   ""))
7929         (names '((move "Move" "Moving")
7930                  (copy "Copy" "Copying")
7931                  (crosspost "Crosspost" "Crossposting")))
7932         (copy-buf (save-excursion
7933                     (nnheader-set-temp-buffer " *copy article*")))
7934         (default-marks gnus-article-mark-lists)
7935         (no-expire-marks (delete '(expirable . expire)
7936                                  (copy-sequence gnus-article-mark-lists)))
7937         art-group to-method new-xref article to-groups)
7938     (unless (assq action names)
7939       (error "Unknown action %s" action))
7940     ;; Read the newsgroup name.
7941     (when (and (not to-newsgroup)
7942                (not select-method))
7943       (setq to-newsgroup
7944             (gnus-read-move-group-name
7945              (cadr (assq action names))
7946              (symbol-value (intern (format "gnus-current-%s-group" action)))
7947              articles prefix))
7948       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7949     (setq to-method (or select-method
7950                         (gnus-server-to-method
7951                          (gnus-group-method to-newsgroup))))
7952     ;; Check the method we are to move this article to...
7953     (unless (gnus-check-backend-function
7954              'request-accept-article (car to-method))
7955       (error "%s does not support article copying" (car to-method)))
7956     (unless (gnus-check-server to-method)
7957       (error "Can't open server %s" (car to-method)))
7958     (gnus-message 6 "%s to %s: %s..."
7959                   (caddr (assq action names))
7960                   (or (car select-method) to-newsgroup) articles)
7961     (while articles
7962       (setq article (pop articles))
7963       (setq
7964        art-group
7965        (cond
7966         ;; Move the article.
7967         ((eq action 'move)
7968          ;; Remove this article from future suppression.
7969          (gnus-dup-unsuppress-article article)
7970          (gnus-request-move-article
7971           article                       ; Article to move
7972           gnus-newsgroup-name           ; From newsgroup
7973           (nth 1 (gnus-find-method-for-group
7974                   gnus-newsgroup-name)) ; Server
7975           (list 'gnus-request-accept-article
7976                 to-newsgroup (list 'quote select-method)
7977                 (not articles) t)       ; Accept form
7978           (not articles)))              ; Only save nov last time
7979         ;; Copy the article.
7980         ((eq action 'copy)
7981          (save-excursion
7982            (set-buffer copy-buf)
7983            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7984              (gnus-request-accept-article
7985               to-newsgroup select-method (not articles) t))))
7986         ;; Crosspost the article.
7987         ((eq action 'crosspost)
7988          (let ((xref (message-tokenize-header
7989                       (mail-header-xref (gnus-summary-article-header article))
7990                       " ")))
7991            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7992                                   ":" article))
7993            (unless xref
7994              (setq xref (list (system-name))))
7995            (setq new-xref
7996                  (concat
7997                   (mapconcat 'identity
7998                              (delete "Xref:" (delete new-xref xref))
7999                              " ")
8000                   " " new-xref))
8001            (save-excursion
8002              (set-buffer copy-buf)
8003              ;; First put the article in the destination group.
8004              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8005              (when (consp (setq art-group
8006                                 (gnus-request-accept-article
8007                                  to-newsgroup select-method (not articles))))
8008                (setq new-xref (concat new-xref " " (car art-group)
8009                                       ":" (cdr art-group)))
8010                ;; Now we have the new Xrefs header, so we insert
8011                ;; it and replace the new article.
8012                (nnheader-replace-header "Xref" new-xref)
8013                (gnus-request-replace-article
8014                 (cdr art-group) to-newsgroup (current-buffer))
8015                art-group))))))
8016       (cond
8017        ((not art-group)
8018         (gnus-message 1 "Couldn't %s article %s: %s"
8019                       (cadr (assq action names)) article
8020                       (nnheader-get-report (car to-method))))
8021        ((eq art-group 'junk)
8022         (when (eq action 'move)
8023           (gnus-summary-mark-article article gnus-canceled-mark)
8024           (gnus-message 4 "Deleted article %s" article)))
8025        (t
8026         (let* ((pto-group (gnus-group-prefixed-name
8027                            (car art-group) to-method))
8028                (entry
8029                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8030                (info (nth 2 entry))
8031                (to-group (gnus-info-group info))
8032                to-marks)
8033           ;; Update the group that has been moved to.
8034           (when (and info
8035                      (memq action '(move copy)))
8036             (unless (member to-group to-groups)
8037               (push to-group to-groups))
8038
8039             (unless (memq article gnus-newsgroup-unreads)
8040               (push 'read to-marks)
8041               (gnus-info-set-read
8042                info (gnus-add-to-range (gnus-info-read info)
8043                                        (list (cdr art-group)))))
8044
8045             ;; See whether the article is to be put in the cache.
8046             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8047                              default-marks
8048                            no-expire-marks))
8049                   (to-article (cdr art-group)))
8050
8051               ;; Enter the article into the cache in the new group,
8052               ;; if that is required.
8053               (when gnus-use-cache
8054                 (gnus-cache-possibly-enter-article
8055                  to-group to-article
8056                  (let ((header (copy-sequence
8057                                 (gnus-summary-article-header article))))
8058                    (mail-header-set-number header to-article)
8059                    header)
8060                  (memq article gnus-newsgroup-marked)
8061                  (memq article gnus-newsgroup-dormant)
8062                  (memq article gnus-newsgroup-unreads)))
8063
8064               (when gnus-preserve-marks
8065                 ;; Copy any marks over to the new group.
8066                 (when (and (equal to-group gnus-newsgroup-name)
8067                            (not (memq article gnus-newsgroup-unreads)))
8068                   ;; Mark this article as read in this group.
8069                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8070                   (setcdr (gnus-active to-group) to-article)
8071                   (setcdr gnus-newsgroup-active to-article))
8072
8073                 (while marks
8074                   (when (memq article (symbol-value
8075                                        (intern (format "gnus-newsgroup-%s"
8076                                                        (caar marks)))))
8077                     (push (cdar marks) to-marks)
8078                     ;; If the other group is the same as this group,
8079                     ;; then we have to add the mark to the list.
8080                     (when (equal to-group gnus-newsgroup-name)
8081                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8082                            (cons to-article
8083                                  (symbol-value
8084                                   (intern (format "gnus-newsgroup-%s"
8085                                                   (caar marks)))))))
8086                     ;; Copy the marks to other group.
8087                     (gnus-add-marked-articles
8088                      to-group (cdar marks) (list to-article) info))
8089                   (setq marks (cdr marks)))
8090
8091                 (gnus-request-set-mark to-group (list (list (list to-article)
8092                                                             'set
8093                                                             to-marks))))
8094
8095               (gnus-dribble-enter
8096                (concat "(gnus-group-set-info '"
8097                        (gnus-prin1-to-string (gnus-get-info to-group))
8098                        ")"))))
8099
8100           ;; Update the Xref header in this article to point to
8101           ;; the new crossposted article we have just created.
8102           (when (eq action 'crosspost)
8103             (save-excursion
8104               (set-buffer copy-buf)
8105               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8106               (nnheader-replace-header "Xref" new-xref)
8107               (gnus-request-replace-article
8108                article gnus-newsgroup-name (current-buffer)))))
8109
8110         ;;;!!!Why is this necessary?
8111         (set-buffer gnus-summary-buffer)
8112
8113         (gnus-summary-goto-subject article)
8114         (when (eq action 'move)
8115           (gnus-summary-mark-article article gnus-canceled-mark))))
8116       (gnus-summary-remove-process-mark article))
8117     ;; Re-activate all groups that have been moved to.
8118     (while to-groups
8119       (save-excursion
8120         (set-buffer gnus-group-buffer)
8121         (when (gnus-group-goto-group (car to-groups) t)
8122           (gnus-group-get-new-news-this-group 1 t))
8123         (pop to-groups)))
8124
8125     (gnus-kill-buffer copy-buf)
8126     (gnus-summary-position-point)
8127     (gnus-set-mode-line 'summary)))
8128
8129 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8130   "Move the current article to a different newsgroup.
8131 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8132 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8133 re-spool using this method."
8134   (interactive "P")
8135   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8136
8137 (defun gnus-summary-crosspost-article (&optional n)
8138   "Crosspost the current article to some other group."
8139   (interactive "P")
8140   (gnus-summary-move-article n nil nil 'crosspost))
8141
8142 (defcustom gnus-summary-respool-default-method nil
8143   "Default method for respooling an article.
8144 If nil, use to the current newsgroup method."
8145   :type '(choice (gnus-select-method :value (nnml ""))
8146                  (const nil))
8147   :group 'gnus-summary-mail)
8148
8149 (defun gnus-summary-respool-article (&optional n method)
8150   "Respool the current article.
8151 The article will be squeezed through the mail spooling process again,
8152 which means that it will be put in some mail newsgroup or other
8153 depending on `nnmail-split-methods'.
8154 If N is a positive number, respool the N next articles.
8155 If N is a negative number, respool the N previous articles.
8156 If N is nil and any articles have been marked with the process mark,
8157 respool those articles instead.
8158
8159 Respooling can be done both from mail groups and \"real\" newsgroups.
8160 In the former case, the articles in question will be moved from the
8161 current group into whatever groups they are destined to.  In the
8162 latter case, they will be copied into the relevant groups."
8163   (interactive
8164    (list current-prefix-arg
8165          (let* ((methods (gnus-methods-using 'respool))
8166                 (methname
8167                  (symbol-name (or gnus-summary-respool-default-method
8168                                   (car (gnus-find-method-for-group
8169                                         gnus-newsgroup-name)))))
8170                 (method
8171                  (gnus-completing-read
8172                   methname "What backend do you want to use when respooling?"
8173                   methods nil t nil 'gnus-mail-method-history))
8174                 ms)
8175            (cond
8176             ((zerop (length (setq ms (gnus-servers-using-backend
8177                                       (intern method)))))
8178              (list (intern method) ""))
8179             ((= 1 (length ms))
8180              (car ms))
8181             (t
8182              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8183                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8184                            ms-alist))))))))
8185   (unless method
8186     (error "No method given for respooling"))
8187   (if (assoc (symbol-name
8188               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8189              (gnus-methods-using 'respool))
8190       (gnus-summary-move-article n nil method)
8191     (gnus-summary-copy-article n nil method)))
8192
8193 (defun gnus-summary-import-article (file &optional edit)
8194   "Import an arbitrary file into a mail newsgroup."
8195   (interactive "fImport file: \nP")
8196   (let ((group gnus-newsgroup-name)
8197         (now (current-time))
8198         atts lines group-art)
8199     (unless (gnus-check-backend-function 'request-accept-article group)
8200       (error "%s does not support article importing" group))
8201     (or (file-readable-p file)
8202         (not (file-regular-p file))
8203         (error "Can't read %s" file))
8204     (save-excursion
8205       (set-buffer (gnus-get-buffer-create " *import file*"))
8206       (erase-buffer)
8207       (nnheader-insert-file-contents file)
8208       (goto-char (point-min))
8209       (unless (nnheader-article-p)
8210         ;; This doesn't look like an article, so we fudge some headers.
8211         (setq atts (file-attributes file)
8212               lines (count-lines (point-min) (point-max)))
8213         (insert "From: " (read-string "From: ") "\n"
8214                 "Subject: " (read-string "Subject: ") "\n"
8215                 "Date: " (message-make-date (nth 5 atts)) "\n"
8216                 "Message-ID: " (message-make-message-id) "\n"
8217                 "Lines: " (int-to-string lines) "\n"
8218                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8219       (setq group-art (gnus-request-accept-article group nil t))
8220       (kill-buffer (current-buffer)))
8221     (setq gnus-newsgroup-active (gnus-activate-group group))
8222     (forward-line 1)
8223     (gnus-summary-goto-article (cdr group-art) nil t)
8224     (when edit
8225       (gnus-summary-edit-article))))
8226
8227 (defun gnus-summary-create-article ()
8228   "Create an article in a mail newsgroup."
8229   (interactive)
8230   (let ((group gnus-newsgroup-name)
8231         (now (current-time))
8232         group-art)
8233     (unless (gnus-check-backend-function 'request-accept-article group)
8234       (error "%s does not support article importing" group))
8235     (save-excursion
8236       (set-buffer (gnus-get-buffer-create " *import file*"))
8237       (erase-buffer)
8238       (goto-char (point-min))
8239       ;; This doesn't look like an article, so we fudge some headers.
8240       (insert "From: " (read-string "From: ") "\n"
8241               "Subject: " (read-string "Subject: ") "\n"
8242               "Date: " (message-make-date now) "\n"
8243               "Message-ID: " (message-make-message-id) "\n")
8244       (setq group-art (gnus-request-accept-article group nil t))
8245       (kill-buffer (current-buffer)))
8246     (setq gnus-newsgroup-active (gnus-activate-group group))
8247     (forward-line 1)
8248     (gnus-summary-goto-article (cdr group-art) nil t)
8249     (gnus-summary-edit-article)))
8250
8251 (defun gnus-summary-article-posted-p ()
8252   "Say whether the current (mail) article is available from news as well.
8253 This will be the case if the article has both been mailed and posted."
8254   (interactive)
8255   (let ((id (mail-header-references (gnus-summary-article-header)))
8256         (gnus-override-method (car (gnus-refer-article-methods))))
8257     (if (gnus-request-head id "")
8258         (gnus-message 2 "The current message was found on %s"
8259                       gnus-override-method)
8260       (gnus-message 2 "The current message couldn't be found on %s"
8261                     gnus-override-method)
8262       nil)))
8263
8264 (defun gnus-summary-expire-articles (&optional now)
8265   "Expire all articles that are marked as expirable in the current group."
8266   (interactive)
8267   (when (gnus-check-backend-function
8268          'request-expire-articles gnus-newsgroup-name)
8269     ;; This backend supports expiry.
8270     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8271            (expirable (if total
8272                           (progn
8273                             ;; We need to update the info for
8274                             ;; this group for `gnus-list-of-read-articles'
8275                             ;; to give us the right answer.
8276                             (gnus-run-hooks 'gnus-exit-group-hook)
8277                             (gnus-summary-update-info)
8278                             (gnus-list-of-read-articles gnus-newsgroup-name))
8279                         (setq gnus-newsgroup-expirable
8280                               (sort gnus-newsgroup-expirable '<))))
8281            (expiry-wait (if now 'immediate
8282                           (gnus-group-find-parameter
8283                            gnus-newsgroup-name 'expiry-wait)))
8284            (nnmail-expiry-target
8285             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8286                 nnmail-expiry-target))
8287            es)
8288       (when expirable
8289         ;; There are expirable articles in this group, so we run them
8290         ;; through the expiry process.
8291         (gnus-message 6 "Expiring articles...")
8292         (unless (gnus-check-group gnus-newsgroup-name)
8293           (error "Can't open server for %s" gnus-newsgroup-name))
8294         ;; The list of articles that weren't expired is returned.
8295         (save-excursion
8296           (if expiry-wait
8297               (let ((nnmail-expiry-wait-function nil)
8298                     (nnmail-expiry-wait expiry-wait))
8299                 (setq es (gnus-request-expire-articles
8300                           expirable gnus-newsgroup-name)))
8301             (setq es (gnus-request-expire-articles
8302                       expirable gnus-newsgroup-name)))
8303           (unless total
8304             (setq gnus-newsgroup-expirable es))
8305           ;; We go through the old list of expirable, and mark all
8306           ;; really expired articles as nonexistent.
8307           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8308             (let ((gnus-use-cache nil))
8309               (while expirable
8310                 (unless (memq (car expirable) es)
8311                   (when (gnus-data-find (car expirable))
8312                     (gnus-summary-mark-article
8313                      (car expirable) gnus-canceled-mark)))
8314                 (setq expirable (cdr expirable))))))
8315         (gnus-message 6 "Expiring articles...done")))))
8316
8317 (defun gnus-summary-expire-articles-now ()
8318   "Expunge all expirable articles in the current group.
8319 This means that *all* articles that are marked as expirable will be
8320 deleted forever, right now."
8321   (interactive)
8322   (or gnus-expert-user
8323       (gnus-yes-or-no-p
8324        "Are you really, really, really sure you want to delete all these messages? ")
8325       (error "Phew!"))
8326   (gnus-summary-expire-articles t))
8327
8328 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8329 (defun gnus-summary-delete-article (&optional n)
8330   "Delete the N next (mail) articles.
8331 This command actually deletes articles.  This is not a marking
8332 command.  The article will disappear forever from your life, never to
8333 return.
8334 If N is negative, delete backwards.
8335 If N is nil and articles have been marked with the process mark,
8336 delete these instead."
8337   (interactive "P")
8338   (unless (gnus-check-backend-function 'request-expire-articles
8339                                        gnus-newsgroup-name)
8340     (error "The current newsgroup does not support article deletion"))
8341   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8342     (error "Couldn't open server"))
8343   ;; Compute the list of articles to delete.
8344   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8345         not-deleted)
8346     (if (and gnus-novice-user
8347              (not (gnus-yes-or-no-p
8348                    (format "Do you really want to delete %s forever? "
8349                            (if (> (length articles) 1)
8350                                (format "these %s articles" (length articles))
8351                              "this article")))))
8352         ()
8353       ;; Delete the articles.
8354       (setq not-deleted (gnus-request-expire-articles
8355                          articles gnus-newsgroup-name 'force))
8356       (while articles
8357         (gnus-summary-remove-process-mark (car articles))
8358         ;; The backend might not have been able to delete the article
8359         ;; after all.
8360         (unless (memq (car articles) not-deleted)
8361           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8362         (setq articles (cdr articles)))
8363       (when not-deleted
8364         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8365     (gnus-summary-position-point)
8366     (gnus-set-mode-line 'summary)
8367     not-deleted))
8368
8369 (defun gnus-summary-edit-article (&optional force)
8370   "Edit the current article.
8371 This will have permanent effect only in mail groups.
8372 If FORCE is non-nil, allow editing of articles even in read-only
8373 groups."
8374   (interactive "P")
8375   (save-excursion
8376     (set-buffer gnus-summary-buffer)
8377     (let ((mail-parse-charset gnus-newsgroup-charset)
8378           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8379       (gnus-set-global-variables)
8380       (when (and (not force)
8381                  (gnus-group-read-only-p))
8382         (error "The current newsgroup does not support article editing"))
8383       (gnus-summary-show-article t)
8384       (gnus-article-edit-article
8385        'ignore
8386        `(lambda (no-highlight)
8387           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8388                 (message-options message-options)
8389                 (message-options-set-recipient)
8390                 (mail-parse-ignored-charsets
8391                  ',gnus-newsgroup-ignored-charsets))
8392             (gnus-summary-edit-article-done
8393              ,(or (mail-header-references gnus-current-headers) "")
8394              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8395
8396 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8397
8398 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8399                                                  no-highlight)
8400   "Make edits to the current article permanent."
8401   (interactive)
8402   (save-excursion
8403     ;; The buffer restriction contains the entire article if it exists.
8404     (when (article-goto-body)
8405       (let ((lines (count-lines (point) (point-max)))
8406             (length (- (point-max) (point)))
8407             (case-fold-search t)
8408             (body (copy-marker (point))))
8409         (goto-char (point-min))
8410         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8411           (delete-region (match-beginning 1) (match-end 1))
8412           (insert (number-to-string length)))
8413         (goto-char (point-min))
8414         (when (re-search-forward
8415                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8416           (delete-region (match-beginning 1) (match-end 1))
8417           (insert (number-to-string length)))
8418         (goto-char (point-min))
8419         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8420           (delete-region (match-beginning 1) (match-end 1))
8421           (insert (number-to-string lines))))))
8422   ;; Replace the article.
8423   (let ((buf (current-buffer)))
8424     (with-temp-buffer
8425       (insert-buffer-substring buf)
8426
8427       (if (and (not read-only)
8428                (not (gnus-request-replace-article
8429                      (cdr gnus-article-current) (car gnus-article-current)
8430                      (current-buffer) t)))
8431           (error "Couldn't replace article")
8432         ;; Update the summary buffer.
8433         (if (and references
8434                  (equal (message-tokenize-header references " ")
8435                         (message-tokenize-header
8436                          (or (message-fetch-field "references") "") " ")))
8437             ;; We only have to update this line.
8438             (save-excursion
8439               (save-restriction
8440                 (message-narrow-to-head)
8441                 (let ((head (buffer-string))
8442                       header)
8443                   (with-temp-buffer
8444                     (insert (format "211 %d Article retrieved.\n"
8445                                     (cdr gnus-article-current)))
8446                     (insert head)
8447                     (insert ".\n")
8448                     (let ((nntp-server-buffer (current-buffer)))
8449                       (setq header (car (gnus-get-newsgroup-headers
8450                                          (save-excursion
8451                                            (set-buffer gnus-summary-buffer)
8452                                            gnus-newsgroup-dependencies)
8453                                          t))))
8454                     (save-excursion
8455                       (set-buffer gnus-summary-buffer)
8456                       (gnus-data-set-header
8457                        (gnus-data-find (cdr gnus-article-current))
8458                        header)
8459                       (gnus-summary-update-article-line
8460                        (cdr gnus-article-current) header))))))
8461           ;; Update threads.
8462           (set-buffer (or buffer gnus-summary-buffer))
8463           (gnus-summary-update-article (cdr gnus-article-current)))
8464         ;; Prettify the article buffer again.
8465         (unless no-highlight
8466           (save-excursion
8467             (set-buffer gnus-article-buffer)
8468             ;;;!!! Fix this -- article should be rehighlighted.
8469             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8470             (set-buffer gnus-original-article-buffer)
8471             (gnus-request-article
8472              (cdr gnus-article-current)
8473              (car gnus-article-current) (current-buffer))))
8474         ;; Prettify the summary buffer line.
8475         (when (gnus-visual-p 'summary-highlight 'highlight)
8476           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8477
8478 (defun gnus-summary-edit-wash (key)
8479   "Perform editing command KEY in the article buffer."
8480   (interactive
8481    (list
8482     (progn
8483       (message "%s" (concat (this-command-keys) "- "))
8484       (read-char))))
8485   (message "")
8486   (gnus-summary-edit-article)
8487   (execute-kbd-macro (concat (this-command-keys) key))
8488   (gnus-article-edit-done))
8489
8490 ;;; Respooling
8491
8492 (defun gnus-summary-respool-query (&optional silent trace)
8493   "Query where the respool algorithm would put this article."
8494   (interactive)
8495   (let (gnus-mark-article-hook)
8496     (gnus-summary-select-article)
8497     (save-excursion
8498       (set-buffer gnus-original-article-buffer)
8499       (save-restriction
8500         (message-narrow-to-head)
8501         (let ((groups (nnmail-article-group 'identity trace)))
8502           (unless silent
8503             (if groups
8504                 (message "This message would go to %s"
8505                          (mapconcat 'car groups ", "))
8506               (message "This message would go to no groups"))
8507             groups))))))
8508
8509 (defun gnus-summary-respool-trace ()
8510   "Trace where the respool algorithm would put this article.
8511 Display a buffer showing all fancy splitting patterns which matched."
8512   (interactive)
8513   (gnus-summary-respool-query nil t))
8514
8515 ;; Summary marking commands.
8516
8517 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8518   "Mark articles which has the same subject as read, and then select the next.
8519 If UNMARK is positive, remove any kind of mark.
8520 If UNMARK is negative, tick articles."
8521   (interactive "P")
8522   (when unmark
8523     (setq unmark (prefix-numeric-value unmark)))
8524   (let ((count
8525          (gnus-summary-mark-same-subject
8526           (gnus-summary-article-subject) unmark)))
8527     ;; Select next unread article.  If auto-select-same mode, should
8528     ;; select the first unread article.
8529     (gnus-summary-next-article t (and gnus-auto-select-same
8530                                       (gnus-summary-article-subject)))
8531     (gnus-message 7 "%d article%s marked as %s"
8532                   count (if (= count 1) " is" "s are")
8533                   (if unmark "unread" "read"))))
8534
8535 (defun gnus-summary-kill-same-subject (&optional unmark)
8536   "Mark articles which has the same subject as read.
8537 If UNMARK is positive, remove any kind of mark.
8538 If UNMARK is negative, tick articles."
8539   (interactive "P")
8540   (when unmark
8541     (setq unmark (prefix-numeric-value unmark)))
8542   (let ((count
8543          (gnus-summary-mark-same-subject
8544           (gnus-summary-article-subject) unmark)))
8545     ;; If marked as read, go to next unread subject.
8546     (when (null unmark)
8547       ;; Go to next unread subject.
8548       (gnus-summary-next-subject 1 t))
8549     (gnus-message 7 "%d articles are marked as %s"
8550                   count (if unmark "unread" "read"))))
8551
8552 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8553   "Mark articles with same SUBJECT as read, and return marked number.
8554 If optional argument UNMARK is positive, remove any kinds of marks.
8555 If optional argument UNMARK is negative, mark articles as unread instead."
8556   (let ((count 1))
8557     (save-excursion
8558       (cond
8559        ((null unmark)                   ; Mark as read.
8560         (while (and
8561                 (progn
8562                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8563                   (gnus-summary-show-thread) t)
8564                 (gnus-summary-find-subject subject))
8565           (setq count (1+ count))))
8566        ((> unmark 0)                    ; Tick.
8567         (while (and
8568                 (progn
8569                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8570                   (gnus-summary-show-thread) t)
8571                 (gnus-summary-find-subject subject))
8572           (setq count (1+ count))))
8573        (t                               ; Mark as unread.
8574         (while (and
8575                 (progn
8576                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8577                   (gnus-summary-show-thread) t)
8578                 (gnus-summary-find-subject subject))
8579           (setq count (1+ count)))))
8580       (gnus-set-mode-line 'summary)
8581       ;; Return the number of marked articles.
8582       count)))
8583
8584 (defun gnus-summary-mark-as-processable (n &optional unmark)
8585   "Set the process mark on the next N articles.
8586 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8587 the process mark instead.  The difference between N and the actual
8588 number of articles marked is returned."
8589   (interactive "P")
8590   (if (and (null n) (gnus-region-active-p))
8591       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8592     (setq n (prefix-numeric-value n))
8593     (let ((backward (< n 0))
8594           (n (abs n)))
8595       (while (and
8596               (> n 0)
8597               (if unmark
8598                 (gnus-summary-remove-process-mark
8599                  (gnus-summary-article-number))
8600                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8601               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8602         (setq n (1- n)))
8603       (when (/= 0 n)
8604         (gnus-message 7 "No more articles"))
8605       (gnus-summary-recenter)
8606       (gnus-summary-position-point)
8607       n)))
8608
8609 (defun gnus-summary-unmark-as-processable (n)
8610   "Remove the process mark from the next N articles.
8611 If N is negative, unmark backward instead.  The difference between N and
8612 the actual number of articles unmarked is returned."
8613   (interactive "P")
8614   (gnus-summary-mark-as-processable n t))
8615
8616 (defun gnus-summary-unmark-all-processable ()
8617   "Remove the process mark from all articles."
8618   (interactive)
8619   (save-excursion
8620     (while gnus-newsgroup-processable
8621       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8622   (gnus-summary-position-point))
8623
8624 (defun gnus-summary-add-mark (article type)
8625   "Mark ARTICLE with a mark of TYPE."
8626   (let ((vtype (car (assq type gnus-article-mark-lists)))
8627         var)
8628     (if (not vtype)
8629         (error "No such mark type: %s" type)
8630       (setq var (intern (format "gnus-newsgroup-%s" type)))
8631       (set var (cons article (symbol-value var)))
8632       (if (memq type '(processable cached replied forwarded saved))
8633           (gnus-summary-update-secondary-mark article)
8634         ;;; !!! This is bobus.  We should find out what primary
8635         ;;; !!! mark we want to set.
8636         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8637
8638 (defun gnus-summary-mark-as-expirable (n)
8639   "Mark N articles forward as expirable.
8640 If N is negative, mark backward instead.  The difference between N and
8641 the actual number of articles marked is returned."
8642   (interactive "p")
8643   (gnus-summary-mark-forward n gnus-expirable-mark))
8644
8645 (defun gnus-summary-mark-article-as-replied (article)
8646   "Mark ARTICLE as replied to and update the summary line.
8647 ARTICLE can also be a list of articles."
8648   (interactive (list (gnus-summary-article-number)))
8649   (let ((articles (if (listp article) article (list article))))
8650     (dolist (article articles)
8651       (push article gnus-newsgroup-replied)
8652       (let ((buffer-read-only nil))
8653         (when (gnus-summary-goto-subject article nil t)
8654           (gnus-summary-update-secondary-mark article))))))
8655
8656 (defun gnus-summary-mark-article-as-forwarded (article)
8657   "Mark ARTICLE as forwarded and update the summary line.
8658 ARTICLE can also be a list of articles."
8659   (let ((articles (if (listp article) article (list article))))
8660     (dolist (article articles)
8661       (push article gnus-newsgroup-forwarded)
8662       (let ((buffer-read-only nil))
8663         (when (gnus-summary-goto-subject article nil t)
8664           (gnus-summary-update-secondary-mark article))))))
8665
8666 (defun gnus-summary-set-bookmark (article)
8667   "Set a bookmark in current article."
8668   (interactive (list (gnus-summary-article-number)))
8669   (when (or (not (get-buffer gnus-article-buffer))
8670             (not gnus-current-article)
8671             (not gnus-article-current)
8672             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8673     (error "No current article selected"))
8674   ;; Remove old bookmark, if one exists.
8675   (let ((old (assq article gnus-newsgroup-bookmarks)))
8676     (when old
8677       (setq gnus-newsgroup-bookmarks
8678             (delq old gnus-newsgroup-bookmarks))))
8679   ;; Set the new bookmark, which is on the form
8680   ;; (article-number . line-number-in-body).
8681   (push
8682    (cons article
8683          (save-excursion
8684            (set-buffer gnus-article-buffer)
8685            (count-lines
8686             (min (point)
8687                  (save-excursion
8688                    (goto-char (point-min))
8689                    (search-forward "\n\n" nil t)
8690                    (point)))
8691             (point))))
8692    gnus-newsgroup-bookmarks)
8693   (gnus-message 6 "A bookmark has been added to the current article."))
8694
8695 (defun gnus-summary-remove-bookmark (article)
8696   "Remove the bookmark from the current article."
8697   (interactive (list (gnus-summary-article-number)))
8698   ;; Remove old bookmark, if one exists.
8699   (let ((old (assq article gnus-newsgroup-bookmarks)))
8700     (if old
8701         (progn
8702           (setq gnus-newsgroup-bookmarks
8703                 (delq old gnus-newsgroup-bookmarks))
8704           (gnus-message 6 "Removed bookmark."))
8705       (gnus-message 6 "No bookmark in current article."))))
8706
8707 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8708 (defun gnus-summary-mark-as-dormant (n)
8709   "Mark N articles forward as dormant.
8710 If N is negative, mark backward instead.  The difference between N and
8711 the actual number of articles marked is returned."
8712   (interactive "p")
8713   (gnus-summary-mark-forward n gnus-dormant-mark))
8714
8715 (defun gnus-summary-set-process-mark (article)
8716   "Set the process mark on ARTICLE and update the summary line."
8717   (setq gnus-newsgroup-processable
8718         (cons article
8719               (delq article gnus-newsgroup-processable)))
8720   (when (gnus-summary-goto-subject article)
8721     (gnus-summary-show-thread)
8722     (gnus-summary-goto-subject article)
8723     (gnus-summary-update-secondary-mark article)))
8724
8725 (defun gnus-summary-remove-process-mark (article)
8726   "Remove the process mark from ARTICLE and update the summary line."
8727   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8728   (when (gnus-summary-goto-subject article)
8729     (gnus-summary-show-thread)
8730     (gnus-summary-goto-subject article)
8731     (gnus-summary-update-secondary-mark article)))
8732
8733 (defun gnus-summary-set-saved-mark (article)
8734   "Set the process mark on ARTICLE and update the summary line."
8735   (push article gnus-newsgroup-saved)
8736   (when (gnus-summary-goto-subject article)
8737     (gnus-summary-update-secondary-mark article)))
8738
8739 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8740   "Mark N articles as read forwards.
8741 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8742 The difference between N and the actual number of articles marked is
8743 returned.
8744 Iff NO-EXPIRE, auto-expiry will be inhibited."
8745   (interactive "p")
8746   (gnus-summary-show-thread)
8747   (let ((backward (< n 0))
8748         (gnus-summary-goto-unread
8749          (and gnus-summary-goto-unread
8750               (not (eq gnus-summary-goto-unread 'never))
8751               (not (memq mark (list gnus-unread-mark
8752                                     gnus-ticked-mark gnus-dormant-mark)))))
8753         (n (abs n))
8754         (mark (or mark gnus-del-mark)))
8755     (while (and (> n 0)
8756                 (gnus-summary-mark-article nil mark no-expire)
8757                 (zerop (gnus-summary-next-subject
8758                         (if backward -1 1)
8759                         (and gnus-summary-goto-unread
8760                              (not (eq gnus-summary-goto-unread 'never)))
8761                         t)))
8762       (setq n (1- n)))
8763     (when (/= 0 n)
8764       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8765     (gnus-summary-recenter)
8766     (gnus-summary-position-point)
8767     (gnus-set-mode-line 'summary)
8768     n))
8769
8770 (defun gnus-summary-mark-article-as-read (mark)
8771   "Mark the current article quickly as read with MARK."
8772   (let ((article (gnus-summary-article-number)))
8773     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8774     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8775     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8776     (push (cons article mark) gnus-newsgroup-reads)
8777     ;; Possibly remove from cache, if that is used.
8778     (when gnus-use-cache
8779       (gnus-cache-enter-remove-article article))
8780     ;; Allow the backend to change the mark.
8781     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8782     ;; Check for auto-expiry.
8783     (when (and gnus-newsgroup-auto-expire
8784                (memq mark gnus-auto-expirable-marks))
8785       (setq mark gnus-expirable-mark)
8786       ;; Let the backend know about the mark change.
8787       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8788       (push article gnus-newsgroup-expirable))
8789     ;; Set the mark in the buffer.
8790     (gnus-summary-update-mark mark 'unread)
8791     t))
8792
8793 (defun gnus-summary-mark-article-as-unread (mark)
8794   "Mark the current article quickly as unread with MARK."
8795   (let* ((article (gnus-summary-article-number))
8796          (old-mark (gnus-summary-article-mark article)))
8797     ;; Allow the backend to change the mark.
8798     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8799     (if (eq mark old-mark)
8800         t
8801       (if (<= article 0)
8802           (progn
8803             (gnus-error 1 "Can't mark negative article numbers")
8804             nil)
8805         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8806         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8807         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8808         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8809         (cond ((= mark gnus-ticked-mark)
8810                (push article gnus-newsgroup-marked))
8811               ((= mark gnus-dormant-mark)
8812                (push article gnus-newsgroup-dormant))
8813               (t
8814                (push article gnus-newsgroup-unreads)))
8815         (gnus-pull article gnus-newsgroup-reads)
8816
8817         ;; See whether the article is to be put in the cache.
8818         (and gnus-use-cache
8819              (vectorp (gnus-summary-article-header article))
8820              (save-excursion
8821                (gnus-cache-possibly-enter-article
8822                 gnus-newsgroup-name article
8823                 (gnus-summary-article-header article)
8824                 (= mark gnus-ticked-mark)
8825                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8826
8827         ;; Fix the mark.
8828         (gnus-summary-update-mark mark 'unread)
8829         t))))
8830
8831 (defun gnus-summary-mark-article (&optional article mark no-expire)
8832   "Mark ARTICLE with MARK.  MARK can be any character.
8833 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8834 `??' (dormant) and `?E' (expirable).
8835 If MARK is nil, then the default character `?r' is used.
8836 If ARTICLE is nil, then the article on the current line will be
8837 marked.
8838 Iff NO-EXPIRE, auto-expiry will be inhibited."
8839   ;; The mark might be a string.
8840   (when (stringp mark)
8841     (setq mark (aref mark 0)))
8842   ;; If no mark is given, then we check auto-expiring.
8843   (when (null mark)
8844     (setq mark gnus-del-mark))
8845   (when (and (not no-expire)
8846              gnus-newsgroup-auto-expire
8847              (memq mark gnus-auto-expirable-marks))
8848     (setq mark gnus-expirable-mark))
8849   (let ((article (or article (gnus-summary-article-number)))
8850         (old-mark (gnus-summary-article-mark article)))
8851     ;; Allow the backend to change the mark.
8852     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8853     (if (eq mark old-mark)
8854         t
8855       (unless article
8856         (error "No article on current line"))
8857       (if (not (if (or (= mark gnus-unread-mark)
8858                        (= mark gnus-ticked-mark)
8859                        (= mark gnus-dormant-mark))
8860                    (gnus-mark-article-as-unread article mark)
8861                  (gnus-mark-article-as-read article mark)))
8862           t
8863         ;; See whether the article is to be put in the cache.
8864         (and gnus-use-cache
8865              (not (= mark gnus-canceled-mark))
8866              (vectorp (gnus-summary-article-header article))
8867              (save-excursion
8868                (gnus-cache-possibly-enter-article
8869                 gnus-newsgroup-name article
8870                 (gnus-summary-article-header article)
8871                 (= mark gnus-ticked-mark)
8872                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8873
8874         (when (gnus-summary-goto-subject article nil t)
8875           (let ((buffer-read-only nil))
8876             (gnus-summary-show-thread)
8877             ;; Fix the mark.
8878             (gnus-summary-update-mark mark 'unread)
8879             t))))))
8880
8881 (defun gnus-summary-update-secondary-mark (article)
8882   "Update the secondary (read, process, cache) mark."
8883   (gnus-summary-update-mark
8884    (cond ((memq article gnus-newsgroup-processable)
8885           gnus-process-mark)
8886          ((memq article gnus-newsgroup-cached)
8887           gnus-cached-mark)
8888          ((memq article gnus-newsgroup-replied)
8889           gnus-replied-mark)
8890          ((memq article gnus-newsgroup-forwarded)
8891           gnus-forwarded-mark)
8892          ((memq article gnus-newsgroup-saved)
8893           gnus-saved-mark)
8894          (t gnus-no-mark))
8895    'replied)
8896   (when (gnus-visual-p 'summary-highlight 'highlight)
8897     (gnus-run-hooks 'gnus-summary-update-hook))
8898   t)
8899
8900 (defun gnus-summary-update-mark (mark type)
8901   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8902         (buffer-read-only nil))
8903     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8904     (when forward
8905       (when (looking-at "\r")
8906         (incf forward))
8907       (when (<= (+ forward (point)) (point-max))
8908         ;; Go to the right position on the line.
8909         (goto-char (+ forward (point)))
8910         ;; Replace the old mark with the new mark.
8911         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8912         ;; Optionally update the marks by some user rule.
8913         (when (eq type 'unread)
8914           (gnus-data-set-mark
8915            (gnus-data-find (gnus-summary-article-number)) mark)
8916           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8917
8918 (defun gnus-mark-article-as-read (article &optional mark)
8919   "Enter ARTICLE in the pertinent lists and remove it from others."
8920   ;; Make the article expirable.
8921   (let ((mark (or mark gnus-del-mark)))
8922     (if (= mark gnus-expirable-mark)
8923         (push article gnus-newsgroup-expirable)
8924       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8925     ;; Remove from unread and marked lists.
8926     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8927     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8928     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8929     (push (cons article mark) gnus-newsgroup-reads)
8930     ;; Possibly remove from cache, if that is used.
8931     (when gnus-use-cache
8932       (gnus-cache-enter-remove-article article))
8933     t))
8934
8935 (defun gnus-mark-article-as-unread (article &optional mark)
8936   "Enter ARTICLE in the pertinent lists and remove it from others."
8937   (let ((mark (or mark gnus-ticked-mark)))
8938     (if (<= article 0)
8939         (progn
8940           (gnus-error 1 "Can't mark negative article numbers")
8941           nil)
8942       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8943             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8944             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8945             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8946
8947       ;; Unsuppress duplicates?
8948       (when gnus-suppress-duplicates
8949         (gnus-dup-unsuppress-article article))
8950
8951       (cond ((= mark gnus-ticked-mark)
8952              (push article gnus-newsgroup-marked))
8953             ((= mark gnus-dormant-mark)
8954              (push article gnus-newsgroup-dormant))
8955             (t
8956              (push article gnus-newsgroup-unreads)))
8957       (gnus-pull article gnus-newsgroup-reads)
8958       t)))
8959
8960 (defalias 'gnus-summary-mark-as-unread-forward
8961   'gnus-summary-tick-article-forward)
8962 (make-obsolete 'gnus-summary-mark-as-unread-forward
8963                'gnus-summary-tick-article-forward)
8964 (defun gnus-summary-tick-article-forward (n)
8965   "Tick N articles forwards.
8966 If N is negative, tick backwards instead.
8967 The difference between N and the number of articles ticked is returned."
8968   (interactive "p")
8969   (gnus-summary-mark-forward n gnus-ticked-mark))
8970
8971 (defalias 'gnus-summary-mark-as-unread-backward
8972   'gnus-summary-tick-article-backward)
8973 (make-obsolete 'gnus-summary-mark-as-unread-backward
8974                'gnus-summary-tick-article-backward)
8975 (defun gnus-summary-tick-article-backward (n)
8976   "Tick N articles backwards.
8977 The difference between N and the number of articles ticked is returned."
8978   (interactive "p")
8979   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8980
8981 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8982 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8983 (defun gnus-summary-tick-article (&optional article clear-mark)
8984   "Mark current article as unread.
8985 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8986 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8987   (interactive)
8988   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8989                                        gnus-ticked-mark)))
8990
8991 (defun gnus-summary-mark-as-read-forward (n)
8992   "Mark N articles as read forwards.
8993 If N is negative, mark backwards instead.
8994 The difference between N and the actual number of articles marked is
8995 returned."
8996   (interactive "p")
8997   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8998
8999 (defun gnus-summary-mark-as-read-backward (n)
9000   "Mark the N articles as read backwards.
9001 The difference between N and the actual number of articles marked is
9002 returned."
9003   (interactive "p")
9004   (gnus-summary-mark-forward
9005    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9006
9007 (defun gnus-summary-mark-as-read (&optional article mark)
9008   "Mark current article as read.
9009 ARTICLE specifies the article to be marked as read.
9010 MARK specifies a string to be inserted at the beginning of the line."
9011   (gnus-summary-mark-article article mark))
9012
9013 (defun gnus-summary-clear-mark-forward (n)
9014   "Clear marks from N articles forward.
9015 If N is negative, clear backward instead.
9016 The difference between N and the number of marks cleared is returned."
9017   (interactive "p")
9018   (gnus-summary-mark-forward n gnus-unread-mark))
9019
9020 (defun gnus-summary-clear-mark-backward (n)
9021   "Clear marks from N articles backward.
9022 The difference between N and the number of marks cleared is returned."
9023   (interactive "p")
9024   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9025
9026 (defun gnus-summary-mark-unread-as-read ()
9027   "Intended to be used by `gnus-summary-mark-article-hook'."
9028   (when (memq gnus-current-article gnus-newsgroup-unreads)
9029     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9030
9031 (defun gnus-summary-mark-read-and-unread-as-read ()
9032   "Intended to be used by `gnus-summary-mark-article-hook'."
9033   (let ((mark (gnus-summary-article-mark)))
9034     (when (or (gnus-unread-mark-p mark)
9035               (gnus-read-mark-p mark))
9036       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9037
9038 (defun gnus-summary-mark-unread-as-ticked ()
9039    "Intended to be used by `gnus-summary-mark-article-hook'."
9040   (when (memq gnus-current-article gnus-newsgroup-unreads)
9041     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9042
9043 (defun gnus-summary-mark-region-as-read (point mark all)
9044   "Mark all unread articles between point and mark as read.
9045 If given a prefix, mark all articles between point and mark as read,
9046 even ticked and dormant ones."
9047   (interactive "r\nP")
9048   (save-excursion
9049     (let (article)
9050       (goto-char point)
9051       (beginning-of-line)
9052       (while (and
9053               (< (point) mark)
9054               (progn
9055                 (when (or all
9056                           (memq (setq article (gnus-summary-article-number))
9057                                 gnus-newsgroup-unreads))
9058                   (gnus-summary-mark-article article gnus-del-mark))
9059                 t)
9060               (gnus-summary-find-next))))))
9061
9062 (defun gnus-summary-mark-below (score mark)
9063   "Mark articles with score less than SCORE with MARK."
9064   (interactive "P\ncMark: ")
9065   (setq score (if score
9066                   (prefix-numeric-value score)
9067                 (or gnus-summary-default-score 0)))
9068   (save-excursion
9069     (set-buffer gnus-summary-buffer)
9070     (goto-char (point-min))
9071     (while
9072         (progn
9073           (and (< (gnus-summary-article-score) score)
9074                (gnus-summary-mark-article nil mark))
9075           (gnus-summary-find-next)))))
9076
9077 (defun gnus-summary-kill-below (&optional score)
9078   "Mark articles with score below SCORE as read."
9079   (interactive "P")
9080   (gnus-summary-mark-below score gnus-killed-mark))
9081
9082 (defun gnus-summary-clear-above (&optional score)
9083   "Clear all marks from articles with score above SCORE."
9084   (interactive "P")
9085   (gnus-summary-mark-above score gnus-unread-mark))
9086
9087 (defun gnus-summary-tick-above (&optional score)
9088   "Tick all articles with score above SCORE."
9089   (interactive "P")
9090   (gnus-summary-mark-above score gnus-ticked-mark))
9091
9092 (defun gnus-summary-mark-above (score mark)
9093   "Mark articles with score over SCORE with MARK."
9094   (interactive "P\ncMark: ")
9095   (setq score (if score
9096                   (prefix-numeric-value score)
9097                 (or gnus-summary-default-score 0)))
9098   (save-excursion
9099     (set-buffer gnus-summary-buffer)
9100     (goto-char (point-min))
9101     (while (and (progn
9102                   (when (> (gnus-summary-article-score) score)
9103                     (gnus-summary-mark-article nil mark))
9104                   t)
9105                 (gnus-summary-find-next)))))
9106
9107 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9108 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9109 (defun gnus-summary-limit-include-expunged (&optional no-error)
9110   "Display all the hidden articles that were expunged for low scores."
9111   (interactive)
9112   (let ((buffer-read-only nil))
9113     (let ((scored gnus-newsgroup-scored)
9114           headers h)
9115       (while scored
9116         (unless (gnus-summary-article-header (caar scored))
9117           (and (setq h (gnus-number-to-header (caar scored)))
9118                (< (cdar scored) gnus-summary-expunge-below)
9119                (push h headers)))
9120         (setq scored (cdr scored)))
9121       (if (not headers)
9122           (when (not no-error)
9123             (error "No expunged articles hidden"))
9124         (goto-char (point-min))
9125         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9126         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9127         (mapcar (lambda (x) (push (mail-header-number x) 
9128                                   gnus-newsgroup-limit))
9129                 headers)
9130         (gnus-summary-prepare-unthreaded (nreverse headers))
9131         (goto-char (point-min))
9132         (gnus-summary-position-point)
9133         t))))
9134
9135 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9136   "Mark all unread articles in this newsgroup as read.
9137 If prefix argument ALL is non-nil, ticked and dormant articles will
9138 also be marked as read.
9139 If QUIETLY is non-nil, no questions will be asked.
9140 If TO-HERE is non-nil, it should be a point in the buffer.  All
9141 articles before this point will be marked as read.
9142 Note that this function will only catch up the unread article
9143 in the current summary buffer limitation.
9144 The number of articles marked as read is returned."
9145   (interactive "P")
9146   (prog1
9147       (save-excursion
9148         (when (or quietly
9149                   (not gnus-interactive-catchup) ;Without confirmation?
9150                   gnus-expert-user
9151                   (gnus-y-or-n-p
9152                    (if all
9153                        "Mark absolutely all articles as read? "
9154                      "Mark all unread articles as read? ")))
9155           (if (and not-mark
9156                    (not gnus-newsgroup-adaptive)
9157                    (not gnus-newsgroup-auto-expire)
9158                    (not gnus-suppress-duplicates)
9159                    (or (not gnus-use-cache)
9160                        (eq gnus-use-cache 'passive)))
9161               (progn
9162                 (when all
9163                   (setq gnus-newsgroup-marked nil
9164                         gnus-newsgroup-dormant nil))
9165                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9166             ;; We actually mark all articles as canceled, which we
9167             ;; have to do when using auto-expiry or adaptive scoring.
9168             (gnus-summary-show-all-threads)
9169             (when (gnus-summary-first-subject (not all) t)
9170               (while (and
9171                       (if to-here (< (point) to-here) t)
9172                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9173                       (gnus-summary-find-next (not all) nil nil t))))
9174             (gnus-set-mode-line 'summary))
9175           t))
9176     (gnus-summary-position-point)))
9177
9178 (defun gnus-summary-catchup-to-here (&optional all)
9179   "Mark all unticked articles before the current one as read.
9180 If ALL is non-nil, also mark ticked and dormant articles as read."
9181   (interactive "P")
9182   (save-excursion
9183     (gnus-save-hidden-threads
9184       (let ((beg (point)))
9185         ;; We check that there are unread articles.
9186         (when (or all (gnus-summary-find-prev))
9187           (gnus-summary-catchup all t beg)))))
9188   (gnus-summary-position-point))
9189
9190 (defun gnus-summary-catchup-all (&optional quietly)
9191   "Mark all articles in this newsgroup as read."
9192   (interactive "P")
9193   (gnus-summary-catchup t quietly))
9194
9195 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9196   "Mark all unread articles in this group as read, then exit.
9197 If prefix argument ALL is non-nil, all articles are marked as read.
9198 If QUIETLY is non-nil, no questions will be asked."
9199   (interactive "P")
9200   (when (gnus-summary-catchup all quietly nil 'fast)
9201     ;; Select next newsgroup or exit.
9202     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9203              (eq gnus-auto-select-next 'quietly))
9204         (gnus-summary-next-group nil)
9205       (gnus-summary-exit))))
9206
9207 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9208   "Mark all articles in this newsgroup as read, and then exit."
9209   (interactive "P")
9210   (gnus-summary-catchup-and-exit t quietly))
9211
9212 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9213   "Mark all articles in this group as read and select the next group.
9214 If given a prefix, mark all articles, unread as well as ticked, as
9215 read."
9216   (interactive "P")
9217   (save-excursion
9218     (gnus-summary-catchup all))
9219   (gnus-summary-next-group))
9220
9221 ;;;
9222 ;;; with article
9223 ;;;
9224
9225 (defmacro gnus-with-article (article &rest forms)
9226   "Select ARTICLE and perform FORMS in the original article buffer.
9227 Then replace the article with the result."
9228   `(progn
9229      ;; We don't want the article to be marked as read.
9230      (let (gnus-mark-article-hook)
9231        (gnus-summary-select-article t t nil ,article))
9232      (set-buffer gnus-original-article-buffer)
9233      ,@forms
9234      (if (not (gnus-check-backend-function
9235                'request-replace-article (car gnus-article-current)))
9236          (gnus-message 5 "Read-only group; not replacing")
9237        (unless (gnus-request-replace-article
9238                 ,article (car gnus-article-current)
9239                 (current-buffer) t)
9240          (error "Couldn't replace article")))
9241      ;; The cache and backlog have to be flushed somewhat.
9242      (when gnus-keep-backlog
9243        (gnus-backlog-remove-article
9244         (car gnus-article-current) (cdr gnus-article-current)))
9245      (when gnus-use-cache
9246        (gnus-cache-update-article
9247         (car gnus-article-current) (cdr gnus-article-current)))))
9248
9249 (put 'gnus-with-article 'lisp-indent-function 1)
9250 (put 'gnus-with-article 'edebug-form-spec '(form body))
9251
9252 ;; Thread-based commands.
9253
9254 (defun gnus-summary-articles-in-thread (&optional article)
9255   "Return a list of all articles in the current thread.
9256 If ARTICLE is non-nil, return all articles in the thread that starts
9257 with that article."
9258   (let* ((article (or article (gnus-summary-article-number)))
9259          (data (gnus-data-find-list article))
9260          (top-level (gnus-data-level (car data)))
9261          (top-subject
9262           (cond ((null gnus-thread-operation-ignore-subject)
9263                  (gnus-simplify-subject-re
9264                   (mail-header-subject (gnus-data-header (car data)))))
9265                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9266                  (gnus-simplify-subject-fuzzy
9267                   (mail-header-subject (gnus-data-header (car data)))))
9268                 (t nil)))
9269          (end-point (save-excursion
9270                       (if (gnus-summary-go-to-next-thread)
9271                           (point) (point-max))))
9272          articles)
9273     (while (and data
9274                 (< (gnus-data-pos (car data)) end-point))
9275       (when (or (not top-subject)
9276                 (string= top-subject
9277                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9278                              (gnus-simplify-subject-fuzzy
9279                               (mail-header-subject
9280                                (gnus-data-header (car data))))
9281                            (gnus-simplify-subject-re
9282                             (mail-header-subject
9283                              (gnus-data-header (car data)))))))
9284         (push (gnus-data-number (car data)) articles))
9285       (unless (and (setq data (cdr data))
9286                    (> (gnus-data-level (car data)) top-level))
9287         (setq data nil)))
9288     ;; Return the list of articles.
9289     (nreverse articles)))
9290
9291 (defun gnus-summary-rethread-current ()
9292   "Rethread the thread the current article is part of."
9293   (interactive)
9294   (let* ((gnus-show-threads t)
9295          (article (gnus-summary-article-number))
9296          (id (mail-header-id (gnus-summary-article-header)))
9297          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9298     (unless id
9299       (error "No article on the current line"))
9300     (gnus-rebuild-thread id)
9301     (gnus-summary-goto-subject article)))
9302
9303 (defun gnus-summary-reparent-thread ()
9304   "Make the current article child of the marked (or previous) article.
9305
9306 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9307 is non-nil or the Subject: of both articles are the same."
9308   (interactive)
9309   (unless (not (gnus-group-read-only-p))
9310     (error "The current newsgroup does not support article editing"))
9311   (unless (<= (length gnus-newsgroup-processable) 1)
9312     (error "No more than one article may be marked"))
9313   (save-window-excursion
9314     (let ((gnus-article-buffer " *reparent*")
9315           (current-article (gnus-summary-article-number))
9316           ;; First grab the marked article, otherwise one line up.
9317           (parent-article (if (not (null gnus-newsgroup-processable))
9318                               (car gnus-newsgroup-processable)
9319                             (save-excursion
9320                               (if (eq (forward-line -1) 0)
9321                                   (gnus-summary-article-number)
9322                                 (error "Beginning of summary buffer"))))))
9323       (unless (not (eq current-article parent-article))
9324         (error "An article may not be self-referential"))
9325       (let ((message-id (mail-header-id
9326                          (gnus-summary-article-header parent-article))))
9327         (unless (and message-id (not (equal message-id "")))
9328           (error "No message-id in desired parent"))
9329         (gnus-with-article current-article
9330           (save-restriction
9331             (goto-char (point-min))
9332             (message-narrow-to-head)
9333             (if (re-search-forward "^References: " nil t)
9334                 (progn
9335                   (re-search-forward "^[^ \t]" nil t)
9336                   (forward-line -1)
9337                   (end-of-line)
9338                   (insert " " message-id))
9339               (insert "References: " message-id "\n"))))
9340         (set-buffer gnus-summary-buffer)
9341         (gnus-summary-unmark-all-processable)
9342         (gnus-summary-update-article current-article)
9343         (gnus-summary-rethread-current)
9344         (gnus-message 3 "Article %d is now the child of article %d"
9345                       current-article parent-article)))))
9346
9347 (defun gnus-summary-toggle-threads (&optional arg)
9348   "Toggle showing conversation threads.
9349 If ARG is positive number, turn showing conversation threads on."
9350   (interactive "P")
9351   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9352     (setq gnus-show-threads
9353           (if (null arg) (not gnus-show-threads)
9354             (> (prefix-numeric-value arg) 0)))
9355     (gnus-summary-prepare)
9356     (gnus-summary-goto-subject current)
9357     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9358     (gnus-summary-position-point)))
9359
9360 (defun gnus-summary-show-all-threads ()
9361   "Show all threads."
9362   (interactive)
9363   (save-excursion
9364     (let ((buffer-read-only nil))
9365       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9366   (gnus-summary-position-point))
9367
9368 (defun gnus-summary-show-thread ()
9369   "Show thread subtrees.
9370 Returns nil if no thread was there to be shown."
9371   (interactive)
9372   (let ((buffer-read-only nil)
9373         (orig (point))
9374         ;; first goto end then to beg, to have point at beg after let
9375         (end (progn (end-of-line) (point)))
9376         (beg (progn (beginning-of-line) (point))))
9377     (prog1
9378         ;; Any hidden lines here?
9379         (search-forward "\r" end t)
9380       (subst-char-in-region beg end ?\^M ?\n t)
9381       (goto-char orig)
9382       (gnus-summary-position-point))))
9383
9384 (defun gnus-summary-hide-all-threads ()
9385   "Hide all thread subtrees."
9386   (interactive)
9387   (save-excursion
9388     (goto-char (point-min))
9389     (gnus-summary-hide-thread)
9390     (while (zerop (gnus-summary-next-thread 1 t))
9391       (gnus-summary-hide-thread)))
9392   (gnus-summary-position-point))
9393
9394 (defun gnus-summary-hide-thread ()
9395   "Hide thread subtrees.
9396 Returns nil if no threads were there to be hidden."
9397   (interactive)
9398   (let ((buffer-read-only nil)
9399         (start (point))
9400         (article (gnus-summary-article-number)))
9401     (goto-char start)
9402     ;; Go forward until either the buffer ends or the subthread
9403     ;; ends.
9404     (when (and (not (eobp))
9405                (or (zerop (gnus-summary-next-thread 1 t))
9406                    (goto-char (point-max))))
9407       (prog1
9408           (if (and (> (point) start)
9409                    (search-backward "\n" start t))
9410               (progn
9411                 (subst-char-in-region start (point) ?\n ?\^M)
9412                 (gnus-summary-goto-subject article))
9413             (goto-char start)
9414             nil)))))
9415
9416 (defun gnus-summary-go-to-next-thread (&optional previous)
9417   "Go to the same level (or less) next thread.
9418 If PREVIOUS is non-nil, go to previous thread instead.
9419 Return the article number moved to, or nil if moving was impossible."
9420   (let ((level (gnus-summary-thread-level))
9421         (way (if previous -1 1))
9422         (beg (point)))
9423     (forward-line way)
9424     (while (and (not (eobp))
9425                 (< level (gnus-summary-thread-level)))
9426       (forward-line way))
9427     (if (eobp)
9428         (progn
9429           (goto-char beg)
9430           nil)
9431       (setq beg (point))
9432       (prog1
9433           (gnus-summary-article-number)
9434         (goto-char beg)))))
9435
9436 (defun gnus-summary-next-thread (n &optional silent)
9437   "Go to the same level next N'th thread.
9438 If N is negative, search backward instead.
9439 Returns the difference between N and the number of skips actually
9440 done.
9441
9442 If SILENT, don't output messages."
9443   (interactive "p")
9444   (let ((backward (< n 0))
9445         (n (abs n)))
9446     (while (and (> n 0)
9447                 (gnus-summary-go-to-next-thread backward))
9448       (decf n))
9449     (unless silent
9450       (gnus-summary-position-point))
9451     (when (and (not silent) (/= 0 n))
9452       (gnus-message 7 "No more threads"))
9453     n))
9454
9455 (defun gnus-summary-prev-thread (n)
9456   "Go to the same level previous N'th thread.
9457 Returns the difference between N and the number of skips actually
9458 done."
9459   (interactive "p")
9460   (gnus-summary-next-thread (- n)))
9461
9462 (defun gnus-summary-go-down-thread ()
9463   "Go down one level in the current thread."
9464   (let ((children (gnus-summary-article-children)))
9465     (when children
9466       (gnus-summary-goto-subject (car children)))))
9467
9468 (defun gnus-summary-go-up-thread ()
9469   "Go up one level in the current thread."
9470   (let ((parent (gnus-summary-article-parent)))
9471     (when parent
9472       (gnus-summary-goto-subject parent))))
9473
9474 (defun gnus-summary-down-thread (n)
9475   "Go down thread N steps.
9476 If N is negative, go up instead.
9477 Returns the difference between N and how many steps down that were
9478 taken."
9479   (interactive "p")
9480   (let ((up (< n 0))
9481         (n (abs n)))
9482     (while (and (> n 0)
9483                 (if up (gnus-summary-go-up-thread)
9484                   (gnus-summary-go-down-thread)))
9485       (setq n (1- n)))
9486     (gnus-summary-position-point)
9487     (when (/= 0 n)
9488       (gnus-message 7 "Can't go further"))
9489     n))
9490
9491 (defun gnus-summary-up-thread (n)
9492   "Go up thread N steps.
9493 If N is negative, go down instead.
9494 Returns the difference between N and how many steps down that were
9495 taken."
9496   (interactive "p")
9497   (gnus-summary-down-thread (- n)))
9498
9499 (defun gnus-summary-top-thread ()
9500   "Go to the top of the thread."
9501   (interactive)
9502   (while (gnus-summary-go-up-thread))
9503   (gnus-summary-article-number))
9504
9505 (defun gnus-summary-kill-thread (&optional unmark)
9506   "Mark articles under current thread as read.
9507 If the prefix argument is positive, remove any kinds of marks.
9508 If the prefix argument is negative, tick articles instead."
9509   (interactive "P")
9510   (when unmark
9511     (setq unmark (prefix-numeric-value unmark)))
9512   (let ((articles (gnus-summary-articles-in-thread)))
9513     (save-excursion
9514       ;; Expand the thread.
9515       (gnus-summary-show-thread)
9516       ;; Mark all the articles.
9517       (while articles
9518         (gnus-summary-goto-subject (car articles))
9519         (cond ((null unmark)
9520                (gnus-summary-mark-article-as-read gnus-killed-mark))
9521               ((> unmark 0)
9522                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9523               (t
9524                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9525         (setq articles (cdr articles))))
9526     ;; Hide killed subtrees.
9527     (and (null unmark)
9528          gnus-thread-hide-killed
9529          (gnus-summary-hide-thread))
9530     ;; If marked as read, go to next unread subject.
9531     (when (null unmark)
9532       ;; Go to next unread subject.
9533       (gnus-summary-next-subject 1 t)))
9534   (gnus-set-mode-line 'summary))
9535
9536 ;; Summary sorting commands
9537
9538 (defun gnus-summary-sort-by-number (&optional reverse)
9539   "Sort the summary buffer by article number.
9540 Argument REVERSE means reverse order."
9541   (interactive "P")
9542   (gnus-summary-sort 'number reverse))
9543
9544 (defun gnus-summary-sort-by-author (&optional reverse)
9545   "Sort the summary buffer by author name alphabetically.
9546 If `case-fold-search' is non-nil, case of letters is ignored.
9547 Argument REVERSE means reverse order."
9548   (interactive "P")
9549   (gnus-summary-sort 'author reverse))
9550
9551 (defun gnus-summary-sort-by-subject (&optional reverse)
9552   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9553 If `case-fold-search' is non-nil, case of letters is ignored.
9554 Argument REVERSE means reverse order."
9555   (interactive "P")
9556   (gnus-summary-sort 'subject reverse))
9557
9558 (defun gnus-summary-sort-by-date (&optional reverse)
9559   "Sort the summary buffer by date.
9560 Argument REVERSE means reverse order."
9561   (interactive "P")
9562   (gnus-summary-sort 'date reverse))
9563
9564 (defun gnus-summary-sort-by-score (&optional reverse)
9565   "Sort the summary buffer by score.
9566 Argument REVERSE means reverse order."
9567   (interactive "P")
9568   (gnus-summary-sort 'score reverse))
9569
9570 (defun gnus-summary-sort-by-lines (&optional reverse)
9571   "Sort the summary buffer by the number of lines.
9572 Argument REVERSE means reverse order."
9573   (interactive "P")
9574   (gnus-summary-sort 'lines reverse))
9575
9576 (defun gnus-summary-sort-by-chars (&optional reverse)
9577   "Sort the summary buffer by article length.
9578 Argument REVERSE means reverse order."
9579   (interactive "P")
9580   (gnus-summary-sort 'chars reverse))
9581
9582 (defun gnus-summary-sort-by-original (&optional reverse)
9583   "Sort the summary buffer using the default sorting method.
9584 Argument REVERSE means reverse order."
9585   (interactive "P")
9586   (let* ((buffer-read-only)
9587          (gnus-summary-prepare-hook nil))
9588     ;; We do the sorting by regenerating the threads.
9589     (gnus-summary-prepare)
9590     ;; Hide subthreads if needed.
9591     (when (and gnus-show-threads gnus-thread-hide-subtree)
9592       (gnus-summary-hide-all-threads))))
9593
9594 (defun gnus-summary-sort (predicate reverse)
9595   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9596   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9597          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9598          (gnus-thread-sort-functions
9599           (if (not reverse)
9600               thread
9601             `(lambda (t1 t2)
9602                (,thread t2 t1))))
9603          (gnus-sort-gathered-threads-function
9604           gnus-thread-sort-functions)
9605          (gnus-article-sort-functions
9606           (if (not reverse)
9607               article
9608             `(lambda (t1 t2)
9609                (,article t2 t1))))
9610          (buffer-read-only)
9611          (gnus-summary-prepare-hook nil))
9612     ;; We do the sorting by regenerating the threads.
9613     (gnus-summary-prepare)
9614     ;; Hide subthreads if needed.
9615     (when (and gnus-show-threads gnus-thread-hide-subtree)
9616       (gnus-summary-hide-all-threads))))
9617
9618 ;; Summary saving commands.
9619
9620 (defun gnus-summary-save-article (&optional n not-saved)
9621   "Save the current article using the default saver function.
9622 If N is a positive number, save the N next articles.
9623 If N is a negative number, save the N previous articles.
9624 If N is nil and any articles have been marked with the process mark,
9625 save those articles instead.
9626 The variable `gnus-default-article-saver' specifies the saver function."
9627   (interactive "P")
9628   (let* ((articles (gnus-summary-work-articles n))
9629          (save-buffer (save-excursion
9630                         (nnheader-set-temp-buffer " *Gnus Save*")))
9631          (num (length articles))
9632          header file)
9633     (dolist (article articles)
9634       (setq header (gnus-summary-article-header article))
9635       (if (not (vectorp header))
9636           ;; This is a pseudo-article.
9637           (if (assq 'name header)
9638               (gnus-copy-file (cdr (assq 'name header)))
9639             (gnus-message 1 "Article %d is unsaveable" article))
9640         ;; This is a real article.
9641         (save-window-excursion
9642           (gnus-summary-select-article t nil nil article))
9643         (save-excursion
9644           (set-buffer save-buffer)
9645           (erase-buffer)
9646           (insert-buffer-substring gnus-original-article-buffer))
9647         (setq file (gnus-article-save save-buffer file num))
9648         (gnus-summary-remove-process-mark article)
9649         (unless not-saved
9650           (gnus-summary-set-saved-mark article))))
9651     (gnus-kill-buffer save-buffer)
9652     (gnus-summary-position-point)
9653     (gnus-set-mode-line 'summary)
9654     n))
9655
9656 (defun gnus-summary-pipe-output (&optional arg)
9657   "Pipe the current article to a subprocess.
9658 If N is a positive number, pipe the N next articles.
9659 If N is a negative number, pipe the N previous articles.
9660 If N is nil and any articles have been marked with the process mark,
9661 pipe those articles instead."
9662   (interactive "P")
9663   (require 'gnus-art)
9664   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9665     (gnus-summary-save-article arg t))
9666   (gnus-configure-windows 'pipe))
9667
9668 (defun gnus-summary-save-article-mail (&optional arg)
9669   "Append the current article to an mail file.
9670 If N is a positive number, save the N next articles.
9671 If N is a negative number, save the N previous articles.
9672 If N is nil and any articles have been marked with the process mark,
9673 save those articles instead."
9674   (interactive "P")
9675   (require 'gnus-art)
9676   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9677     (gnus-summary-save-article arg)))
9678
9679 (defun gnus-summary-save-article-rmail (&optional arg)
9680   "Append the current article to an rmail file.
9681 If N is a positive number, save the N next articles.
9682 If N is a negative number, save the N previous articles.
9683 If N is nil and any articles have been marked with the process mark,
9684 save those articles instead."
9685   (interactive "P")
9686   (require 'gnus-art)
9687   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9688     (gnus-summary-save-article arg)))
9689
9690 (defun gnus-summary-save-article-file (&optional arg)
9691   "Append the current article to a file.
9692 If N is a positive number, save the N next articles.
9693 If N is a negative number, save the N previous articles.
9694 If N is nil and any articles have been marked with the process mark,
9695 save those articles instead."
9696   (interactive "P")
9697   (require 'gnus-art)
9698   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9699     (gnus-summary-save-article arg)))
9700
9701 (defun gnus-summary-write-article-file (&optional arg)
9702   "Write the current article to a file, deleting the previous file.
9703 If N is a positive number, save the N next articles.
9704 If N is a negative number, save the N previous articles.
9705 If N is nil and any articles have been marked with the process mark,
9706 save those articles instead."
9707   (interactive "P")
9708   (require 'gnus-art)
9709   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9710     (gnus-summary-save-article arg)))
9711
9712 (defun gnus-summary-save-article-body-file (&optional arg)
9713   "Append the current article body to a file.
9714 If N is a positive number, save the N next articles.
9715 If N is a negative number, save the N previous articles.
9716 If N is nil and any articles have been marked with the process mark,
9717 save those articles instead."
9718   (interactive "P")
9719   (require 'gnus-art)
9720   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9721     (gnus-summary-save-article arg)))
9722
9723 (defun gnus-summary-pipe-message (program)
9724   "Pipe the current article through PROGRAM."
9725   (interactive "sProgram: ")
9726   (gnus-summary-select-article)
9727   (let ((mail-header-separator ""))
9728     (gnus-eval-in-buffer-window gnus-article-buffer
9729       (save-restriction
9730         (widen)
9731         (let ((start (window-start))
9732               buffer-read-only)
9733           (message-pipe-buffer-body program)
9734           (set-window-start (get-buffer-window (current-buffer)) start))))))
9735
9736 (defun gnus-get-split-value (methods)
9737   "Return a value based on the split METHODS."
9738   (let (split-name method result match)
9739     (when methods
9740       (save-excursion
9741         (set-buffer gnus-original-article-buffer)
9742         (save-restriction
9743           (nnheader-narrow-to-headers)
9744           (while (and methods (not split-name))
9745             (goto-char (point-min))
9746             (setq method (pop methods))
9747             (setq match (car method))
9748             (when (cond
9749                    ((stringp match)
9750                     ;; Regular expression.
9751                     (ignore-errors
9752                       (re-search-forward match nil t)))
9753                    ((gnus-functionp match)
9754                     ;; Function.
9755                     (save-restriction
9756                       (widen)
9757                       (setq result (funcall match gnus-newsgroup-name))))
9758                    ((consp match)
9759                     ;; Form.
9760                     (save-restriction
9761                       (widen)
9762                       (setq result (eval match)))))
9763               (setq split-name (cdr method))
9764               (cond ((stringp result)
9765                      (push (expand-file-name
9766                             result gnus-article-save-directory)
9767                            split-name))
9768                     ((consp result)
9769                      (setq split-name (append result split-name)))))))))
9770     (nreverse split-name)))
9771
9772 (defun gnus-valid-move-group-p (group)
9773   (and (boundp group)
9774        (symbol-name group)
9775        (symbol-value group)
9776        (gnus-get-function (gnus-find-method-for-group
9777                            (symbol-name group)) 'request-accept-article t)))
9778
9779 (defun gnus-read-move-group-name (prompt default articles prefix)
9780   "Read a group name."
9781   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9782          (minibuffer-confirm-incomplete nil) ; XEmacs
9783          (prom
9784           (format "%s %s to:"
9785                   prompt
9786                   (if (> (length articles) 1)
9787                       (format "these %d articles" (length articles))
9788                     "this article")))
9789          (to-newsgroup
9790           (cond
9791            ((null split-name)
9792             (gnus-completing-read default prom
9793                                   gnus-active-hashtb
9794                                   'gnus-valid-move-group-p
9795                                   nil prefix
9796                                   'gnus-group-history))
9797            ((= 1 (length split-name))
9798             (gnus-completing-read (car split-name) prom
9799                                   gnus-active-hashtb
9800                                   'gnus-valid-move-group-p
9801                                   nil nil
9802                                   'gnus-group-history))
9803            (t
9804             (gnus-completing-read nil prom
9805                                   (mapcar (lambda (el) (list el))
9806                                           (nreverse split-name))
9807                                   nil nil nil
9808                                   'gnus-group-history))))
9809          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9810     (when to-newsgroup
9811       (if (or (string= to-newsgroup "")
9812               (string= to-newsgroup prefix))
9813           (setq to-newsgroup default))
9814       (unless to-newsgroup
9815         (error "No group name entered"))
9816       (or (gnus-active to-newsgroup)
9817           (gnus-activate-group to-newsgroup nil nil to-method)
9818           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9819                                      to-newsgroup))
9820               (or (and (gnus-request-create-group to-newsgroup to-method)
9821                        (gnus-activate-group
9822                         to-newsgroup nil nil to-method)
9823                        (gnus-subscribe-group to-newsgroup))
9824                   (error "Couldn't create group %s" to-newsgroup)))
9825           (error "No such group: %s" to-newsgroup)))
9826     to-newsgroup))
9827
9828 (defun gnus-summary-save-parts (type dir n &optional reverse)
9829   "Save parts matching TYPE to DIR.
9830 If REVERSE, save parts that do not match TYPE."
9831   (interactive
9832    (list (read-string "Save parts of type: "
9833                       (or (car gnus-summary-save-parts-type-history)
9834                           gnus-summary-save-parts-default-mime)
9835                       'gnus-summary-save-parts-type-history)
9836          (setq gnus-summary-save-parts-last-directory
9837                (read-file-name "Save to directory: "
9838                                gnus-summary-save-parts-last-directory
9839                                nil t))
9840          current-prefix-arg))
9841   (gnus-summary-iterate n
9842     (let ((gnus-display-mime-function nil)
9843           (gnus-inhibit-treatment t))
9844       (gnus-summary-select-article))
9845     (save-excursion
9846       (set-buffer gnus-article-buffer)
9847       (let ((handles (or gnus-article-mime-handles
9848                          (mm-dissect-buffer) (mm-uu-dissect))))
9849         (when handles
9850           (gnus-summary-save-parts-1 type dir handles reverse)
9851           (unless gnus-article-mime-handles ;; Don't destroy this case.
9852             (mm-destroy-parts handles)))))))
9853
9854 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9855   (if (stringp (car handle))
9856       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9857               (cdr handle))
9858     (when (if reverse
9859               (not (string-match type (mm-handle-media-type handle)))
9860             (string-match type (mm-handle-media-type handle)))
9861       (let ((file (expand-file-name
9862                    (file-name-nondirectory
9863                     (or
9864                      (mail-content-type-get
9865                       (mm-handle-disposition handle) 'filename)
9866                      (concat gnus-newsgroup-name
9867                              "." (number-to-string
9868                                   (cdr gnus-article-current)))))
9869                    dir)))
9870         (unless (file-exists-p file)
9871           (mm-save-part-to-file handle file))))))
9872
9873 ;; Summary extract commands
9874
9875 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9876   (let ((buffer-read-only nil)
9877         (article (gnus-summary-article-number))
9878         after-article b e)
9879     (unless (gnus-summary-goto-subject article)
9880       (error "No such article: %d" article))
9881     (gnus-summary-position-point)
9882     ;; If all commands are to be bunched up on one line, we collect
9883     ;; them here.
9884     (unless gnus-view-pseudos-separately
9885       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9886             files action)
9887         (while ps
9888           (setq action (cdr (assq 'action (car ps))))
9889           (setq files (list (cdr (assq 'name (car ps)))))
9890           (while (and ps (cdr ps)
9891                       (string= (or action "1")
9892                                (or (cdr (assq 'action (cadr ps))) "2")))
9893             (push (cdr (assq 'name (cadr ps))) files)
9894             (setcdr ps (cddr ps)))
9895           (when files
9896             (when (not (string-match "%s" action))
9897               (push " " files))
9898             (push " " files)
9899             (when (assq 'execute (car ps))
9900               (setcdr (assq 'execute (car ps))
9901                       (funcall (if (string-match "%s" action)
9902                                    'format 'concat)
9903                                action
9904                                (mapconcat
9905                                 (lambda (f)
9906                                   (if (equal f " ")
9907                                       f
9908                                     (gnus-quote-arg-for-sh-or-csh f)))
9909                                 files " ")))))
9910           (setq ps (cdr ps)))))
9911     (if (and gnus-view-pseudos (not not-view))
9912         (while pslist
9913           (when (assq 'execute (car pslist))
9914             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9915                                   (eq gnus-view-pseudos 'not-confirm)))
9916           (setq pslist (cdr pslist)))
9917       (save-excursion
9918         (while pslist
9919           (setq after-article (or (cdr (assq 'article (car pslist)))
9920                                   (gnus-summary-article-number)))
9921           (gnus-summary-goto-subject after-article)
9922           (forward-line 1)
9923           (setq b (point))
9924           (insert "    " (file-name-nondirectory
9925                           (cdr (assq 'name (car pslist))))
9926                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9927           (setq e (point))
9928           (forward-line -1)             ; back to `b'
9929           (gnus-add-text-properties
9930            b (1- e) (list 'gnus-number gnus-reffed-article-number
9931                           gnus-mouse-face-prop gnus-mouse-face))
9932           (gnus-data-enter
9933            after-article gnus-reffed-article-number
9934            gnus-unread-mark b (car pslist) 0 (- e b))
9935           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9936           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9937           (setq pslist (cdr pslist)))))))
9938
9939 (defun gnus-pseudos< (p1 p2)
9940   (let ((c1 (cdr (assq 'action p1)))
9941         (c2 (cdr (assq 'action p2))))
9942     (and c1 c2 (string< c1 c2))))
9943
9944 (defun gnus-request-pseudo-article (props)
9945   (cond ((assq 'execute props)
9946          (gnus-execute-command (cdr (assq 'execute props)))))
9947   (let ((gnus-current-article (gnus-summary-article-number)))
9948     (gnus-run-hooks 'gnus-mark-article-hook)))
9949
9950 (defun gnus-execute-command (command &optional automatic)
9951   (save-excursion
9952     (gnus-article-setup-buffer)
9953     (set-buffer gnus-article-buffer)
9954     (setq buffer-read-only nil)
9955     (let ((command (if automatic command
9956                      (read-string "Command: " (cons command 0)))))
9957       (erase-buffer)
9958       (insert "$ " command "\n\n")
9959       (if gnus-view-pseudo-asynchronously
9960           (start-process "gnus-execute" (current-buffer) shell-file-name
9961                          shell-command-switch command)
9962         (call-process shell-file-name nil t nil
9963                       shell-command-switch command)))))
9964
9965 ;; Summary kill commands.
9966
9967 (defun gnus-summary-edit-global-kill (article)
9968   "Edit the \"global\" kill file."
9969   (interactive (list (gnus-summary-article-number)))
9970   (gnus-group-edit-global-kill article))
9971
9972 (defun gnus-summary-edit-local-kill ()
9973   "Edit a local kill file applied to the current newsgroup."
9974   (interactive)
9975   (setq gnus-current-headers (gnus-summary-article-header))
9976   (gnus-group-edit-local-kill
9977    (gnus-summary-article-number) gnus-newsgroup-name))
9978
9979 ;;; Header reading.
9980
9981 (defun gnus-read-header (id &optional header)
9982   "Read the headers of article ID and enter them into the Gnus system."
9983   (let ((group gnus-newsgroup-name)
9984         (gnus-override-method
9985          (or
9986           gnus-override-method
9987           (and (gnus-news-group-p gnus-newsgroup-name)
9988                (car (gnus-refer-article-methods)))))
9989         where)
9990     ;; First we check to see whether the header in question is already
9991     ;; fetched.
9992     (if (stringp id)
9993         ;; This is a Message-ID.
9994         (setq header (or header (gnus-id-to-header id)))
9995       ;; This is an article number.
9996       (setq header (or header (gnus-summary-article-header id))))
9997     (if (and header
9998              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9999         ;; We have found the header.
10000         header
10001       ;; If this is a sparse article, we have to nix out its
10002       ;; previous entry in the thread hashtb.
10003       (when (and header
10004                  (gnus-summary-article-sparse-p (mail-header-number header)))
10005         (let* ((parent (gnus-parent-id (mail-header-references header)))
10006                (thread (and parent (gnus-id-to-thread parent))))
10007           (when thread
10008             (delq (assq header thread) thread))))
10009       ;; We have to really fetch the header to this article.
10010       (save-excursion
10011         (set-buffer nntp-server-buffer)
10012         (when (setq where (gnus-request-head id group))
10013           (nnheader-fold-continuation-lines)
10014           (goto-char (point-max))
10015           (insert ".\n")
10016           (goto-char (point-min))
10017           (insert "211 ")
10018           (princ (cond
10019                   ((numberp id) id)
10020                   ((cdr where) (cdr where))
10021                   (header (mail-header-number header))
10022                   (t gnus-reffed-article-number))
10023                  (current-buffer))
10024           (insert " Article retrieved.\n"))
10025         (if (or (not where)
10026                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10027             ()                          ; Malformed head.
10028           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10029             (when (and (stringp id)
10030                        (not (string= (gnus-group-real-name group)
10031                                      (car where))))
10032               ;; If we fetched by Message-ID and the article came
10033               ;; from a different group, we fudge some bogus article
10034               ;; numbers for this article.
10035               (mail-header-set-number header gnus-reffed-article-number))
10036             (save-excursion
10037               (set-buffer gnus-summary-buffer)
10038               (decf gnus-reffed-article-number)
10039               (gnus-remove-header (mail-header-number header))
10040               (push header gnus-newsgroup-headers)
10041               (setq gnus-current-headers header)
10042               (push (mail-header-number header) gnus-newsgroup-limit)))
10043           header)))))
10044
10045 (defun gnus-remove-header (number)
10046   "Remove header NUMBER from `gnus-newsgroup-headers'."
10047   (if (and gnus-newsgroup-headers
10048            (= number (mail-header-number (car gnus-newsgroup-headers))))
10049       (pop gnus-newsgroup-headers)
10050     (let ((headers gnus-newsgroup-headers))
10051       (while (and (cdr headers)
10052                   (not (= number (mail-header-number (cadr headers)))))
10053         (pop headers))
10054       (when (cdr headers)
10055         (setcdr headers (cddr headers))))))
10056
10057 ;;;
10058 ;;; summary highlights
10059 ;;;
10060
10061 (defun gnus-highlight-selected-summary ()
10062   "Highlight selected article in summary buffer."
10063   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10064   (when gnus-summary-selected-face
10065     (save-excursion
10066       (let* ((beg (progn (beginning-of-line) (point)))
10067              (end (progn (end-of-line) (point)))
10068              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10069              (from (if (get-text-property beg gnus-mouse-face-prop)
10070                        beg
10071                      (or (next-single-property-change
10072                           beg gnus-mouse-face-prop nil end)
10073                          beg)))
10074              (to
10075               (if (= from end)
10076                   (- from 2)
10077                 (or (next-single-property-change
10078                      from gnus-mouse-face-prop nil end)
10079                     end))))
10080         ;; If no mouse-face prop on line we will have to = from = end,
10081         ;; so we highlight the entire line instead.
10082         (when (= (+ to 2) from)
10083           (setq from beg)
10084           (setq to end))
10085         (if gnus-newsgroup-selected-overlay
10086             ;; Move old overlay.
10087             (gnus-move-overlay
10088              gnus-newsgroup-selected-overlay from to (current-buffer))
10089           ;; Create new overlay.
10090           (gnus-overlay-put
10091            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10092            'face gnus-summary-selected-face))))))
10093
10094 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10095 (defun gnus-summary-highlight-line ()
10096   "Highlight current line according to `gnus-summary-highlight'."
10097   (let* ((list gnus-summary-highlight)
10098          (p (point))
10099          (end (progn (end-of-line) (point)))
10100          ;; now find out where the line starts and leave point there.
10101          (beg (progn (beginning-of-line) (point)))
10102          (article (gnus-summary-article-number))
10103          (score (or (cdr (assq (or article gnus-current-article)
10104                                gnus-newsgroup-scored))
10105                     gnus-summary-default-score 0))
10106          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10107          (inhibit-read-only t))
10108     ;; Eval the cars of the lists until we find a match.
10109     (let ((default gnus-summary-default-score)
10110           (default-high gnus-summary-default-high-score)
10111           (default-low gnus-summary-default-low-score))
10112       (while (and list
10113                   (not (eval (caar list))))
10114         (setq list (cdr list))))
10115     (let ((face (cdar list)))
10116       (unless (eq face (get-text-property beg 'face))
10117         (gnus-put-text-property-excluding-characters-with-faces
10118          beg end 'face
10119          (setq face (if (boundp face) (symbol-value face) face)))
10120         (when gnus-summary-highlight-line-function
10121           (funcall gnus-summary-highlight-line-function article face))))
10122     (goto-char p)))
10123
10124 (defun gnus-update-read-articles (group unread &optional compute)
10125   "Update the list of read articles in GROUP."
10126   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10127          (entry (gnus-gethash group gnus-newsrc-hashtb))
10128          (info (nth 2 entry))
10129          (prev 1)
10130          (unread (sort (copy-sequence unread) '<))
10131          read)
10132     (if (or (not info) (not active))
10133         ;; There is no info on this group if it was, in fact,
10134         ;; killed.  Gnus stores no information on killed groups, so
10135         ;; there's nothing to be done.
10136         ;; One could store the information somewhere temporarily,
10137         ;; perhaps...  Hmmm...
10138         ()
10139       ;; Remove any negative articles numbers.
10140       (while (and unread (< (car unread) 0))
10141         (setq unread (cdr unread)))
10142       ;; Remove any expired article numbers
10143       (while (and unread (< (car unread) (car active)))
10144         (setq unread (cdr unread)))
10145       ;; Compute the ranges of read articles by looking at the list of
10146       ;; unread articles.
10147       (while unread
10148         (when (/= (car unread) prev)
10149           (push (if (= prev (1- (car unread))) prev
10150                   (cons prev (1- (car unread))))
10151                 read))
10152         (setq prev (1+ (car unread)))
10153         (setq unread (cdr unread)))
10154       (when (<= prev (cdr active))
10155         (push (cons prev (cdr active)) read))
10156       (setq read (if (> (length read) 1) (nreverse read) read))
10157       (if compute
10158           read
10159         (save-excursion
10160           (let (setmarkundo)
10161             ;; Propagate the read marks to the backend.
10162             (when (gnus-check-backend-function 'request-set-mark group)
10163               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10164                     (add (gnus-remove-from-range read (gnus-info-read info))))
10165                 (when (or add del)
10166                   (unless (gnus-check-group group)
10167                     (error "Can't open server for %s" group))
10168                   (gnus-request-set-mark
10169                    group (delq nil (list (if add (list add 'add '(read)))
10170                                          (if del (list del 'del '(read))))))
10171                   (setq setmarkundo
10172                         `(gnus-request-set-mark
10173                           ,group
10174                           ',(delq nil (list
10175                                        (if del (list del 'add '(read)))
10176                                        (if add (list add 'del '(read))))))))))
10177             (set-buffer gnus-group-buffer)
10178             (gnus-undo-register
10179               `(progn
10180                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10181                  (gnus-info-set-read ',info ',(gnus-info-read info))
10182                  (gnus-get-unread-articles-in-group ',info
10183                                                     (gnus-active ,group))
10184                  (gnus-group-update-group ,group t)
10185                  ,setmarkundo))))
10186         ;; Enter this list into the group info.
10187         (gnus-info-set-read info read)
10188         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10189         (gnus-get-unread-articles-in-group info (gnus-active group))
10190         t))))
10191
10192 (defun gnus-offer-save-summaries ()
10193   "Offer to save all active summary buffers."
10194   (let (buffers)
10195     ;; Go through all buffers and find all summaries.
10196     (dolist (buffer (buffer-list))
10197       (when (and (setq buffer (buffer-name buffer))
10198                  (string-match "Summary" buffer)
10199                  (save-excursion
10200                    (set-buffer buffer)
10201                    ;; We check that this is, indeed, a summary buffer.
10202                    (and (eq major-mode 'gnus-summary-mode)
10203                         ;; Also make sure this isn't bogus.
10204                         gnus-newsgroup-prepared
10205                         ;; Also make sure that this isn't a
10206                         ;; dead summary buffer.
10207                         (not gnus-dead-summary-mode))))
10208         (push buffer buffers)))
10209     ;; Go through all these summary buffers and offer to save them.
10210     (when buffers
10211       (save-excursion
10212         (map-y-or-n-p
10213          "Update summary buffer %s? "
10214          (lambda (buf)
10215            (switch-to-buffer buf)
10216            (gnus-summary-exit))
10217          buffers)))))
10218
10219
10220 ;;; @ for mime-partial
10221 ;;;
10222
10223 (defun gnus-request-partial-message ()
10224   (save-excursion
10225     (let ((number (gnus-summary-article-number))
10226           (group gnus-newsgroup-name)
10227           (mother gnus-article-buffer))
10228       (set-buffer (get-buffer-create " *Partial Article*"))
10229       (erase-buffer)
10230       (setq mime-preview-buffer mother)
10231       (gnus-request-article-this-buffer number group)
10232       (mime-parse-buffer)
10233       )))
10234
10235 (autoload 'mime-combine-message/partial-pieces-automatically
10236   "mime-partial"
10237   "Internal method to combine message/partial messages automatically.")
10238
10239 (mime-add-condition
10240  'action '((type . message)(subtype . partial)
10241            (major-mode . gnus-original-article-mode)
10242            (method . mime-combine-message/partial-pieces-automatically)
10243            (summary-buffer-exp . gnus-summary-buffer)
10244            (request-partial-message-method . gnus-request-partial-message)
10245            ))
10246
10247
10248 ;;; @ for message/rfc822
10249 ;;;
10250
10251 (defun gnus-mime-extract-message/rfc822 (entity situation)
10252   (let (group article num cwin swin cur)
10253     (with-temp-buffer
10254       (mime-insert-entity-content entity)
10255       (setq group (or (cdr (assq 'group situation))
10256                       (completing-read "Group: "
10257                                        gnus-active-hashtb
10258                                        nil
10259                                        (gnus-read-active-file-p)
10260                                        gnus-newsgroup-name))
10261             article (gnus-request-accept-article group)))
10262     (when (and (consp article)
10263                (numberp (setq article (cdr article))))
10264       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10265             cwin (get-buffer-window (current-buffer) t))
10266       (save-window-excursion
10267         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10268             (select-window swin)
10269           (set-buffer gnus-summary-buffer))
10270         (setq cur gnus-current-article)
10271         (forward-line num)
10272         (let (gnus-show-threads)
10273           (gnus-summary-goto-subject article t))
10274         (gnus-summary-clear-mark-forward 1)
10275         (gnus-summary-goto-subject cur))
10276       (when (and cwin (window-frame cwin))
10277         (select-frame (window-frame cwin)))
10278       (when (boundp 'mime-acting-situation-to-override)
10279         (set-alist 'mime-acting-situation-to-override
10280                    'group
10281                    group)
10282         (set-alist 'mime-acting-situation-to-override
10283                    'after-method
10284                    `(progn
10285                       (save-current-buffer
10286                         (set-buffer gnus-group-buffer)
10287                         (gnus-activate-group ,group))
10288                       (gnus-summary-goto-article ,cur
10289                                                  gnus-show-all-headers)))
10290         (set-alist 'mime-acting-situation-to-override
10291                    'number num)))))
10292
10293 (mime-add-condition
10294  'action '((type . message)(subtype . rfc822)
10295            (major-mode . gnus-original-article-mode)
10296            (method . gnus-mime-extract-message/rfc822)
10297            (mode . "extract")
10298            ))
10299
10300 (mime-add-condition
10301  'action '((type . message)(subtype . news)
10302            (major-mode . gnus-original-article-mode)
10303            (method . gnus-mime-extract-message/rfc822)
10304            (mode . "extract")
10305            ))
10306
10307 (defun gnus-mime-extract-multipart (entity situation)
10308   (let ((children (mime-entity-children entity))
10309         mime-acting-situation-to-override
10310         f)
10311     (while children
10312       (mime-play-entity (car children)
10313                         (cons (assq 'mode situation)
10314                               mime-acting-situation-to-override))
10315       (setq children (cdr children)))
10316     (if (setq f (cdr (assq 'after-method
10317                            mime-acting-situation-to-override)))
10318         (eval f)
10319       )))
10320
10321 (mime-add-condition
10322  'action '((type . multipart)
10323            (method . gnus-mime-extract-multipart)
10324            (mode . "extract")
10325            )
10326  'with-default)
10327
10328
10329 ;;; @ end
10330 ;;;
10331
10332 (defun gnus-summary-setup-default-charset ()
10333   "Setup newsgroup default charset."
10334   (if (equal gnus-newsgroup-name "nndraft:drafts")
10335       (setq gnus-newsgroup-charset nil)
10336     (let* ((ignored-charsets
10337             (or gnus-newsgroup-ephemeral-ignored-charsets
10338                 (append
10339                  (and gnus-newsgroup-name
10340                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10341                  gnus-newsgroup-ignored-charsets))))
10342       (setq gnus-newsgroup-charset
10343             (or gnus-newsgroup-ephemeral-charset
10344                 (and gnus-newsgroup-name
10345                      (gnus-parameter-charset gnus-newsgroup-name))
10346                 gnus-default-charset))
10347       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10348            ignored-charsets))))
10349
10350 ;;;
10351 ;;; Mime Commands
10352 ;;;
10353
10354 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10355   "Display the current article buffer fully MIME-buttonized.
10356 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10357 treated as multipart/mixed."
10358   (interactive "P")
10359   (require 'gnus-art)
10360   (let ((gnus-unbuttonized-mime-types nil)
10361         (gnus-mime-display-multipart-as-mixed show-all-parts))
10362     (gnus-summary-show-article)))
10363
10364 (defun gnus-summary-repair-multipart (article)
10365   "Add a Content-Type header to a multipart article without one."
10366   (interactive (list (gnus-summary-article-number)))
10367   (gnus-with-article article
10368     (message-narrow-to-head)
10369     (message-remove-header "Mime-Version")
10370     (goto-char (point-max))
10371     (insert "Mime-Version: 1.0\n")
10372     (widen)
10373     (when (search-forward "\n--" nil t)
10374       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10375         (message-narrow-to-head)
10376         (message-remove-header "Content-Type")
10377         (goto-char (point-max))
10378         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10379                         separator))
10380         (widen))))
10381   (let (gnus-mark-article-hook)
10382     (gnus-summary-select-article t t nil article)))
10383
10384 (defun gnus-summary-toggle-display-buttonized ()
10385   "Toggle the buttonizing of the article buffer."
10386   (interactive)
10387   (require 'gnus-art)
10388   (if (setq gnus-inhibit-mime-unbuttonizing
10389             (not gnus-inhibit-mime-unbuttonizing))
10390       (let ((gnus-unbuttonized-mime-types nil))
10391         (gnus-summary-show-article))
10392     (gnus-summary-show-article)))
10393
10394 ;;;
10395 ;;; Intelli-mouse commmands
10396 ;;;
10397
10398 (defun gnus-wheel-summary-scroll (event)
10399   (interactive "e")
10400   (let ((amount (if (memq 'shift (event-modifiers event))
10401                     (car gnus-wheel-scroll-amount)
10402                   (cdr gnus-wheel-scroll-amount)))
10403         (direction (- (* (static-if (featurep 'xemacs)
10404                              (event-button event)
10405                            (cond ((eq 'mouse-4 (event-basic-type event))
10406                                   4)
10407                                  ((eq 'mouse-5 (event-basic-type event))
10408                                   5)))
10409                          2) 9))
10410         edge)
10411     (gnus-summary-scroll-up (* amount direction))
10412     (when (gnus-eval-in-buffer-window gnus-article-buffer
10413             (save-restriction
10414               (widen)
10415               (and (if (< 0 direction)
10416                        (gnus-article-next-page 0)
10417                      (gnus-article-prev-page 0)
10418                      (bobp))
10419                    (if (setq edge (get-text-property
10420                                    (point-min) 'gnus-wheel-edge))
10421                        (setq edge (* edge direction))
10422                      (setq edge -1))
10423                    (or (plusp edge)
10424                        (let ((buffer-read-only nil)
10425                              (inhibit-read-only t))
10426                          (put-text-property (point-min) (point-max)
10427                                             'gnus-wheel-edge direction)
10428                          nil))
10429                    (or (> edge gnus-wheel-edge-resistance)
10430                        (let ((buffer-read-only nil)
10431                              (inhibit-read-only t))
10432                          (put-text-property (point-min) (point-max)
10433                                             'gnus-wheel-edge
10434                                             (* (1+ edge) direction))
10435                          nil))
10436                    (eq last-command 'gnus-wheel-summary-scroll))))
10437       (gnus-summary-next-article nil nil (minusp direction)))))
10438
10439 (defun gnus-wheel-install ()
10440   "Enable mouse wheel support on summary window."
10441   (when gnus-use-wheel
10442     (let ((keys
10443            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10444       (dolist (key keys)
10445         (define-key gnus-summary-mode-map key
10446           'gnus-wheel-summary-scroll)))))
10447
10448 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10449
10450 ;;;
10451 ;;; Traditional PGP commmands
10452 ;;;
10453
10454 (defun gnus-summary-decrypt-article (&optional force)
10455   "Decrypt the current article in traditional PGP way.
10456 This will have permanent effect only in mail groups.
10457 If FORCE is non-nil, allow editing of articles even in read-only
10458 groups."
10459   (interactive "P")
10460   (gnus-summary-select-article t)
10461   (gnus-eval-in-buffer-window gnus-article-buffer
10462     (save-excursion
10463       (save-restriction
10464         (widen)
10465         (goto-char (point-min))
10466         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10467           (error "Not a traditional PGP message!"))
10468         (let ((armor-start (match-beginning 0)))
10469           (if (and (pgg-decrypt-region armor-start (point-max))
10470                    (or force (not (gnus-group-read-only-p))))
10471               (let ((inhibit-read-only t)
10472                     buffer-read-only)
10473                 (delete-region armor-start
10474                                (progn
10475                                  (re-search-forward "^-+END PGP" nil t)
10476                                  (beginning-of-line 2)
10477                                  (point)))
10478                 (insert-buffer-substring pgg-output-buffer))))))))
10479
10480 (defun gnus-summary-verify-article ()
10481   "Verify the current article in traditional PGP way."
10482   (interactive)
10483   (save-excursion
10484     (set-buffer gnus-original-article-buffer)
10485     (goto-char (point-min))
10486     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10487       (error "Not a traditional PGP message!"))
10488     (re-search-forward "^-+END PGP" nil t)
10489     (beginning-of-line 2)
10490     (call-interactively (function pgg-verify-region))))
10491
10492 ;;;
10493 ;;; Generic summary marking commands
10494 ;;;
10495
10496 (defvar gnus-summary-marking-alist
10497   '((read gnus-del-mark "d")
10498     (unread gnus-unread-mark "u")
10499     (ticked gnus-ticked-mark "!")
10500     (dormant gnus-dormant-mark "?")
10501     (expirable gnus-expirable-mark "e"))
10502   "An alist of names/marks/keystrokes.")
10503
10504 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10505 (defvar gnus-summary-mark-map)
10506
10507 (defun gnus-summary-make-all-marking-commands ()
10508   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10509   (dolist (elem gnus-summary-marking-alist)
10510     (apply 'gnus-summary-make-marking-command elem)))
10511
10512 (defun gnus-summary-make-marking-command (name mark keystroke)
10513   (let ((map (make-sparse-keymap)))
10514     (define-key gnus-summary-generic-mark-map keystroke map)
10515     (dolist (lway `((next "next" next nil "n")
10516                     (next-unread "next unread" next t "N")
10517                     (prev "previous" prev nil "p")
10518                     (prev-unread "previous unread" prev t "P")
10519                     (nomove "" nil nil ,keystroke)))
10520       (let ((func (gnus-summary-make-marking-command-1
10521                    mark (car lway) lway name)))
10522         (setq func (eval func))
10523         (define-key map (nth 4 lway) func)))))
10524
10525 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10526   `(defun ,(intern
10527             (format "gnus-summary-put-mark-as-%s%s"
10528                     name (if (eq way 'nomove)
10529                              ""
10530                            (concat "-" (symbol-name way)))))
10531      (n)
10532      ,(format
10533        "Mark the current article as %s%s.
10534 If N, the prefix, then repeat N times.
10535 If N is negative, move in reverse order.
10536 The difference between N and the actual number of articles marked is
10537 returned."
10538        name (car (cdr lway)))
10539      (interactive "p")
10540      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10541
10542 (defun gnus-summary-generic-mark (n mark move unread)
10543   "Mark N articles with MARK."
10544   (unless (eq major-mode 'gnus-summary-mode)
10545     (error "This command can only be used in the summary buffer"))
10546   (gnus-summary-show-thread)
10547   (let ((nummove
10548          (cond
10549           ((eq move 'next) 1)
10550           ((eq move 'prev) -1)
10551           (t 0))))
10552     (if (zerop nummove)
10553         (setq n 1)
10554       (when (< n 0)
10555         (setq n (abs n)
10556               nummove (* -1 nummove))))
10557     (while (and (> n 0)
10558                 (gnus-summary-mark-article nil mark)
10559                 (zerop (gnus-summary-next-subject nummove unread t)))
10560       (setq n (1- n)))
10561     (when (/= 0 n)
10562       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10563     (gnus-summary-recenter)
10564     (gnus-summary-position-point)
10565     (gnus-set-mode-line 'summary)
10566     n))
10567
10568 (defun gnus-summary-insert-articles (articles)
10569   (when (setq articles
10570               (gnus-set-difference articles
10571                                    (mapcar (lambda (h) (mail-header-number h))
10572                                            gnus-newsgroup-headers)))
10573     (setq gnus-newsgroup-headers 
10574           (merge 'list
10575                  gnus-newsgroup-headers
10576                  (gnus-fetch-headers articles)
10577                  'gnus-article-sort-by-number))
10578     ;; Suppress duplicates?
10579     (when gnus-suppress-duplicates
10580       (gnus-dup-suppress-articles))
10581     
10582     ;; We might want to build some more threads first.
10583     (when (and gnus-fetch-old-headers
10584                (eq gnus-headers-retrieved-by 'nov))
10585       (if (eq gnus-fetch-old-headers 'invisible)
10586         (gnus-build-all-threads)
10587         (gnus-build-old-threads)))
10588     ;; Let the Gnus agent mark articles as read.
10589     (when gnus-agent
10590       (gnus-agent-get-undownloaded-list))
10591     ;; Remove list identifiers from subject
10592     (when gnus-list-identifiers
10593       (gnus-summary-remove-list-identifiers))
10594     ;; First and last article in this newsgroup.
10595     (when gnus-newsgroup-headers
10596       (setq gnus-newsgroup-begin
10597             (mail-header-number (car gnus-newsgroup-headers))
10598             gnus-newsgroup-end
10599             (mail-header-number
10600              (gnus-last-element gnus-newsgroup-headers))))
10601     (when gnus-use-scoring
10602       (gnus-possibly-score-headers))))
10603
10604 (defun gnus-summary-insert-old-articles (&optional all)
10605   "Insert all old articles in this group.
10606 If ALL is non-nil, already read articles become readable.
10607 If ALL is a number, fetch this number of articles."
10608   (interactive "P")
10609   (prog1
10610       (let ((old (mapcar 'car gnus-newsgroup-data))
10611             (i (car gnus-newsgroup-active))
10612             older len)
10613         (while (<= i (cdr gnus-newsgroup-active))
10614           (or (memq i old) (push i older))
10615           (incf i))
10616         (setq len (length older))
10617         (cond 
10618          ((null older) nil)
10619          ((numberp all) 
10620           (if (< all len)
10621               (setq older (subseq older 0 all))))
10622          (all nil)
10623          (t
10624           (if (and (numberp gnus-large-newsgroup)
10625                    (> len gnus-large-newsgroup))
10626               (let ((input
10627                      (read-string
10628                       (format
10629                        "How many articles from %s (default %d): "
10630                        (gnus-limit-string 
10631                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10632                        len))))
10633                 (unless (string-match "^[ \t]*$" input) 
10634                   (setq all (string-to-number input))
10635                   (if (< all len)
10636                       (setq older (subseq older 0 all))))))))
10637         (if (not older)
10638             (message "No old news.")
10639           (gnus-summary-insert-articles older)
10640           (gnus-summary-limit (gnus-union older old))))
10641     (gnus-summary-position-point)))
10642
10643 (defun gnus-summary-insert-new-articles ()
10644   "Insert all new articles in this group."
10645   (interactive)
10646   (prog1
10647       (let ((old (mapcar 'car gnus-newsgroup-data))
10648             (old-active gnus-newsgroup-active)
10649             (nnmail-fetched-sources (list t))
10650             i new)
10651         (setq gnus-newsgroup-active 
10652               (gnus-activate-group gnus-newsgroup-name 'scan))
10653         (setq i (1+ (cdr old-active)))
10654         (while (<= i (cdr gnus-newsgroup-active))
10655           (push i new)
10656           (incf i))
10657         (if (not new)
10658             (message "No gnus is bad news.")
10659           (setq new (nreverse new))
10660           (gnus-summary-insert-articles new)
10661           (setq gnus-newsgroup-unreads
10662                 (append gnus-newsgroup-unreads new))
10663           (gnus-summary-limit (gnus-union old new))))
10664     (gnus-summary-position-point)))
10665
10666 (gnus-summary-make-all-marking-commands)
10667
10668 (gnus-ems-redefine)
10669
10670 (provide 'gnus-sum)
10671
10672 (run-hooks 'gnus-sum-load-hook)
10673
10674 ;;; gnus-sum.el ends here