b4ce5cfecc54074bab5e0202258719de61f4f5e8
[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-display-arrow
830   (and (fboundp 'display-graphic-p)
831        (display-graphic-p))
832   "*If non-nil, display an arrow highlighting the current article."
833   :version "21.1"
834   :group 'gnus-summary
835   :type 'boolean)
836
837 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
838   "Face used for highlighting the current article in the summary buffer."
839   :group 'gnus-summary-visual
840   :type 'face)
841
842 (defcustom gnus-summary-highlight
843   '(((= mark gnus-canceled-mark)
844      . gnus-summary-cancelled-face)
845     ((and (> score default-high)
846           (or (= mark gnus-dormant-mark)
847               (= mark gnus-ticked-mark)))
848      . gnus-summary-high-ticked-face)
849     ((and (< score default-low)
850           (or (= mark gnus-dormant-mark)
851               (= mark gnus-ticked-mark)))
852      . gnus-summary-low-ticked-face)
853     ((or (= mark gnus-dormant-mark)
854          (= mark gnus-ticked-mark))
855      . gnus-summary-normal-ticked-face)
856     ((and (> score default-high) (= mark gnus-ancient-mark))
857      . gnus-summary-high-ancient-face)
858     ((and (< score default-low) (= mark gnus-ancient-mark))
859      . gnus-summary-low-ancient-face)
860     ((= mark gnus-ancient-mark)
861      . gnus-summary-normal-ancient-face)
862     ((and (> score default-high) (= mark gnus-unread-mark))
863      . gnus-summary-high-unread-face)
864     ((and (< score default-low) (= mark gnus-unread-mark))
865      . gnus-summary-low-unread-face)
866     ((= mark gnus-unread-mark)
867      . gnus-summary-normal-unread-face)
868     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
869                                                   gnus-undownloaded-mark)))
870      . gnus-summary-high-unread-face)
871     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
872                                                  gnus-undownloaded-mark)))
873      . gnus-summary-low-unread-face)
874     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
875           (memq article gnus-newsgroup-unreads))
876      . gnus-summary-normal-unread-face)
877     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
878      . gnus-summary-normal-read-face)
879     ((> score default-high)
880      . gnus-summary-high-read-face)
881     ((< score default-low)
882      . gnus-summary-low-read-face)
883     (t
884      . gnus-summary-normal-read-face))
885   "*Controls the highlighting of summary buffer lines.
886
887 A list of (FORM . FACE) pairs.  When deciding how a a particular
888 summary line should be displayed, each form is evaluated.  The content
889 of the face field after the first true form is used.  You can change
890 how those summary lines are displayed, by editing the face field.
891
892 You can use the following variables in the FORM field.
893
894 score:        The article's score
895 default:      The default article score.
896 default-high: The default score for high scored articles.
897 default-low:  The default score for low scored articles.
898 below:        The score below which articles are automatically marked as read.
899 mark:         The articles mark."
900   :group 'gnus-summary-visual
901   :type '(repeat (cons (sexp :tag "Form" nil)
902                        face)))
903
904 (defcustom gnus-alter-header-function nil
905   "Function called to allow alteration of article header structures.
906 The function is called with one parameter, the article header vector,
907 which it may alter in any way.")
908
909 (defvar gnus-decode-encoded-word-function
910   (mime-find-field-decoder 'From 'nov)
911   "Variable that says which function should be used to decode a string with encoded words.")
912
913 (defcustom gnus-extra-headers nil
914   "*Extra headers to parse."
915   :version "21.1"
916   :group 'gnus-summary
917   :type '(repeat symbol))
918
919 (defcustom gnus-ignored-from-addresses
920   (and user-mail-address (regexp-quote user-mail-address))
921   "*Regexp of From headers that may be suppressed in favor of To headers."
922   :version "21.1"
923   :group 'gnus-summary
924   :type 'regexp)
925
926 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
927   "List of charsets that should be ignored.
928 When these charsets are used in the \"charset\" parameter, the
929 default charset will be used instead."
930   :version "21.1"
931   :type '(repeat symbol)
932   :group 'gnus-charset)
933
934 (gnus-define-group-parameter
935  ignored-charsets
936  :type list
937  :function-document
938  "Return the ignored charsets of GROUP."
939  :variable gnus-group-ignored-charsets-alist
940  :variable-default
941  '(("alt\\.chinese\\.text" iso-8859-1))
942  :variable-document
943  "Alist of regexps (to match group names) and charsets that should be ignored.
944 When these charsets are used in the \"charset\" parameter, the
945 default charset will be used instead."
946  :variable-group gnus-charset
947  :variable-type '(repeat (cons (regexp :tag "Group")
948                                (repeat symbol)))
949  :parameter-type '(choice :tag "Ignored charsets"
950                           :value nil
951                           (repeat (symbol)))
952  :parameter-document       "\
953 List of charsets that should be ignored.
954
955 When these charsets are used in the \"charset\" parameter, the
956 default charset will be used instead.")
957
958 (defcustom gnus-group-highlight-words-alist nil
959   "Alist of group regexps and highlight regexps.
960 This variable uses the same syntax as `gnus-emphasis-alist'."
961   :version "21.1"
962   :type '(repeat (cons (regexp :tag "Group")
963                        (repeat (list (regexp :tag "Highlight regexp")
964                                      (number :tag "Group for entire word" 0)
965                                      (number :tag "Group for displayed part" 0)
966                                      (symbol :tag "Face"
967                                              gnus-emphasis-highlight-words)))))
968   :group 'gnus-summary-visual)
969
970 (defcustom gnus-use-wheel nil
971   "Use Intelli-mouse on summary movement"
972   :type 'boolean
973   :group 'gnus-summary-maneuvering)
974
975 (defcustom gnus-wheel-scroll-amount '(5 . 1)
976   "Amount to scroll messages by spinning the mouse wheel.
977 This is actually a cons cell, where the first item is the amount to scroll
978 on a normal wheel event, and the second is the amount to scroll when the
979 wheel is moved with the shift key depressed."
980   :type '(cons (integer :tag "Shift") integer)
981   :group 'gnus-summary-maneuvering)
982
983 (defcustom gnus-wheel-edge-resistance 2
984   "How hard it should be to change the current article
985 by moving the mouse over the edge of the article window."
986   :type 'integer
987   :group 'gnus-summary-maneuvering)
988
989 (defcustom gnus-summary-show-article-charset-alist
990   nil
991   "Alist of number and charset.
992 The article will be shown with the charset corresponding to the
993 numbered argument.
994 For example: ((1 . cn-gb-2312) (2 . big5))."
995   :version "21.1"
996   :type '(repeat (cons (number :tag "Argument" 1)
997                        (symbol :tag "Charset")))
998   :group 'gnus-charset)
999
1000 (defcustom gnus-preserve-marks t
1001   "Whether marks are preserved when moving, copying and respooling messages."
1002   :version "21.1"
1003   :type 'boolean
1004   :group 'gnus-summary-marks)
1005
1006 (defcustom gnus-alter-articles-to-read-function nil
1007   "Function to be called to alter the list of articles to be selected."
1008   :type '(choice (const nil) function)
1009   :group 'gnus-summary)
1010
1011 (defcustom gnus-orphan-score nil
1012   "*All orphans get this score added.  Set in the score file."
1013   :group 'gnus-score-default
1014   :type '(choice (const nil)
1015                  integer))
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
1025 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1026   "*A regexp to match MIME parts when saving multiple parts of a message
1027 with gnus-summary-save-parts (X m). This regexp will be used by default
1028 when prompting the user for which type of files to save."
1029   :group 'gnus-summary
1030   :type 'regexp)
1031
1032 (defcustom gnus-read-all-available-headers nil
1033   "Whether Gnus should parse all headers made available to it.
1034 This is mostly relevant for slow backends where the user may
1035 wish to widen the summary buffer to include all headers
1036 that were fetched.  Say, for nnultimate groups."
1037   :group 'gnus-summary
1038   :type '(choice boolean regexp))
1039
1040 ;;; Internal variables
1041
1042 (defvar gnus-article-mime-handles nil)
1043 (defvar gnus-article-decoded-p nil)
1044 (defvar gnus-article-charset nil)
1045 (defvar gnus-article-ignored-charsets nil)
1046 (defvar gnus-scores-exclude-files nil)
1047 (defvar gnus-page-broken nil)
1048 (defvar gnus-inhibit-mime-unbuttonizing nil)
1049
1050 (defvar gnus-original-article nil)
1051 (defvar gnus-article-internal-prepare-hook nil)
1052 (defvar gnus-newsgroup-process-stack nil)
1053
1054 (defvar gnus-thread-indent-array nil)
1055 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1056 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1057   "Function called to sort the articles within a thread after it has been gathered together.")
1058
1059 (defvar gnus-summary-save-parts-type-history nil)
1060 (defvar gnus-summary-save-parts-last-directory nil)
1061
1062 (defvar gnus-summary-save-parts-type-history nil)
1063 (defvar gnus-summary-save-parts-last-directory nil)
1064
1065 ;; Avoid highlighting in kill files.
1066 (defvar gnus-summary-inhibit-highlight nil)
1067 (defvar gnus-newsgroup-selected-overlay nil)
1068 (defvar gnus-inhibit-limiting nil)
1069 (defvar gnus-newsgroup-adaptive-score-file nil)
1070 (defvar gnus-current-score-file nil)
1071 (defvar gnus-current-move-group nil)
1072 (defvar gnus-current-copy-group nil)
1073 (defvar gnus-current-crosspost-group nil)
1074
1075 (defvar gnus-newsgroup-dependencies nil)
1076 (defvar gnus-newsgroup-adaptive nil)
1077 (defvar gnus-summary-display-article-function nil)
1078 (defvar gnus-summary-highlight-line-function nil
1079   "Function called after highlighting a summary line.")
1080
1081 (defvar gnus-summary-line-format-alist
1082   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1083     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1084     (?s gnus-tmp-subject-or-nil ?s)
1085     (?n gnus-tmp-name ?s)
1086     (?A (std11-address-string
1087          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1088     (?a (or (std11-full-name-string
1089              (car (mime-entity-read-field gnus-tmp-header 'From)))
1090             gnus-tmp-from) ?s)
1091     (?F gnus-tmp-from ?s)
1092     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1093     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1094     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1095     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1096     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1097     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1098     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1099     (?L gnus-tmp-lines ?s)
1100     (?I gnus-tmp-indentation ?s)
1101     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1102     (?R gnus-tmp-replied ?c)
1103     (?\[ gnus-tmp-opening-bracket ?c)
1104     (?\] gnus-tmp-closing-bracket ?c)
1105     (?\> (make-string gnus-tmp-level ? ) ?s)
1106     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1107     (?i gnus-tmp-score ?d)
1108     (?z gnus-tmp-score-char ?c)
1109     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1110     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1111     (?U gnus-tmp-unread ?c)
1112     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1113     (?t (gnus-summary-number-of-articles-in-thread
1114          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1115         ?d)
1116     (?e (gnus-summary-number-of-articles-in-thread
1117          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1118         ?c)
1119     (?u gnus-tmp-user-defined ?s)
1120     (?P (gnus-pick-line-number) ?d)
1121     (?B gnus-tmp-thread-tree-header-string ?s))
1122   "An alist of format specifications that can appear in summary lines.
1123 These are paired with what variables they correspond with, along with
1124 the type of the variable (string, integer, character, etc).")
1125
1126 (defvar gnus-summary-dummy-line-format-alist
1127   `((?S gnus-tmp-subject ?s)
1128     (?N gnus-tmp-number ?d)
1129     (?u gnus-tmp-user-defined ?s)))
1130
1131 (defvar gnus-summary-mode-line-format-alist
1132   `((?G gnus-tmp-group-name ?s)
1133     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1134     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1135     (?A gnus-tmp-article-number ?d)
1136     (?Z gnus-tmp-unread-and-unselected ?s)
1137     (?V gnus-version ?s)
1138     (?U gnus-tmp-unread-and-unticked ?d)
1139     (?S gnus-tmp-subject ?s)
1140     (?e gnus-tmp-unselected ?d)
1141     (?u gnus-tmp-user-defined ?s)
1142     (?d (length gnus-newsgroup-dormant) ?d)
1143     (?t (length gnus-newsgroup-marked) ?d)
1144     (?r (length gnus-newsgroup-reads) ?d)
1145     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1146     (?E gnus-newsgroup-expunged-tally ?d)
1147     (?s (gnus-current-score-file-nondirectory) ?s)))
1148
1149 (defvar gnus-last-search-regexp nil
1150   "Default regexp for article search command.")
1151
1152 (defvar gnus-summary-search-article-matched-data nil
1153   "Last matched data of article search command.  It is the local variable
1154 in `gnus-article-buffer' which consists of the list of start position,
1155 end position and text.")
1156
1157 (defvar gnus-last-shell-command nil
1158   "Default shell command on article.")
1159
1160 (defvar gnus-newsgroup-begin nil)
1161 (defvar gnus-newsgroup-end nil)
1162 (defvar gnus-newsgroup-last-rmail nil)
1163 (defvar gnus-newsgroup-last-mail nil)
1164 (defvar gnus-newsgroup-last-folder nil)
1165 (defvar gnus-newsgroup-last-file nil)
1166 (defvar gnus-newsgroup-auto-expire nil)
1167 (defvar gnus-newsgroup-active nil)
1168
1169 (defvar gnus-newsgroup-data nil)
1170 (defvar gnus-newsgroup-data-reverse nil)
1171 (defvar gnus-newsgroup-limit nil)
1172 (defvar gnus-newsgroup-limits nil)
1173
1174 (defvar gnus-newsgroup-unreads nil
1175   "List of unread articles in the current newsgroup.")
1176
1177 (defvar gnus-newsgroup-unselected nil
1178   "List of unselected unread articles in the current newsgroup.")
1179
1180 (defvar gnus-newsgroup-reads nil
1181   "Alist of read articles and article marks in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-expunged-tally nil)
1184
1185 (defvar gnus-newsgroup-marked nil
1186   "List of ticked articles in the current newsgroup (a subset of unread art).")
1187
1188 (defvar gnus-newsgroup-killed nil
1189   "List of ranges of articles that have been through the scoring process.")
1190
1191 (defvar gnus-newsgroup-cached nil
1192   "List of articles that come from the article cache.")
1193
1194 (defvar gnus-newsgroup-saved nil
1195   "List of articles that have been saved.")
1196
1197 (defvar gnus-newsgroup-kill-headers nil)
1198
1199 (defvar gnus-newsgroup-replied nil
1200   "List of articles that have been replied to in the current newsgroup.")
1201
1202 (defvar gnus-newsgroup-forwarded nil
1203   "List of articles that have been forwarded in the current newsgroup.")
1204
1205 (defvar gnus-newsgroup-expirable nil
1206   "List of articles in the current newsgroup that can be expired.")
1207
1208 (defvar gnus-newsgroup-processable nil
1209   "List of articles in the current newsgroup that can be processed.")
1210
1211 (defvar gnus-newsgroup-downloadable nil
1212   "List of articles in the current newsgroup that can be processed.")
1213
1214 (defvar gnus-newsgroup-undownloaded nil
1215   "List of articles in the current newsgroup that haven't been downloaded..")
1216
1217 (defvar gnus-newsgroup-unsendable nil
1218   "List of articles in the current newsgroup that won't be sent.")
1219
1220 (defvar gnus-newsgroup-bookmarks nil
1221   "List of articles in the current newsgroup that have bookmarks.")
1222
1223 (defvar gnus-newsgroup-dormant nil
1224   "List of dormant articles in the current newsgroup.")
1225
1226 (defvar gnus-newsgroup-scored nil
1227   "List of scored articles in the current newsgroup.")
1228
1229 (defvar gnus-newsgroup-incorporated nil
1230   "List of incorporated articles in the current newsgroup.")
1231
1232 (defvar gnus-newsgroup-headers nil
1233   "List of article headers in the current newsgroup.")
1234
1235 (defvar gnus-newsgroup-threads nil)
1236
1237 (defvar gnus-newsgroup-prepared nil
1238   "Whether the current group has been prepared properly.")
1239
1240 (defvar gnus-newsgroup-ancient nil
1241   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-sparse nil)
1244
1245 (defvar gnus-current-article nil)
1246 (defvar gnus-article-current nil)
1247 (defvar gnus-current-headers nil)
1248 (defvar gnus-have-all-headers nil)
1249 (defvar gnus-last-article nil)
1250 (defvar gnus-newsgroup-history nil)
1251 (defvar gnus-newsgroup-charset nil)
1252 (defvar gnus-newsgroup-ephemeral-charset nil)
1253 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1254
1255 (defvar gnus-article-before-search nil)
1256
1257 (defconst gnus-summary-local-variables
1258   '(gnus-newsgroup-name
1259     gnus-newsgroup-begin gnus-newsgroup-end
1260     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1261     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1262     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1263     gnus-newsgroup-unselected gnus-newsgroup-marked
1264     gnus-newsgroup-reads gnus-newsgroup-saved
1265     gnus-newsgroup-replied gnus-newsgroup-forwarded
1266     gnus-newsgroup-expirable
1267     gnus-newsgroup-processable gnus-newsgroup-killed
1268     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1269     gnus-newsgroup-unsendable
1270     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1271     gnus-newsgroup-headers gnus-newsgroup-threads
1272     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1273     gnus-current-article gnus-current-headers gnus-have-all-headers
1274     gnus-last-article gnus-article-internal-prepare-hook
1275     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1276     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1277     gnus-thread-expunge-below
1278     gnus-score-alist gnus-current-score-file
1279     (gnus-summary-expunge-below . global)
1280     (gnus-summary-mark-below . global)
1281     (gnus-orphan-score . global)
1282     gnus-newsgroup-active gnus-scores-exclude-files
1283     gnus-newsgroup-history gnus-newsgroup-ancient
1284     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1285     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1286     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1287     (gnus-newsgroup-expunged-tally . 0)
1288     gnus-cache-removable-articles gnus-newsgroup-cached
1289     gnus-newsgroup-data gnus-newsgroup-data-reverse
1290     gnus-newsgroup-limit gnus-newsgroup-limits
1291     gnus-newsgroup-charset
1292     gnus-newsgroup-incorporated)
1293   "Variables that are buffer-local to the summary buffers.")
1294
1295 (defvar gnus-newsgroup-variables nil
1296   "Variables that have separate values in the newsgroups.")
1297
1298 ;; Byte-compiler warning.
1299 (eval-when-compile (defvar gnus-article-mode-map))
1300
1301 ;; Subject simplification.
1302
1303 (defun gnus-simplify-whitespace (str)
1304   "Remove excessive whitespace from STR."
1305   (let ((mystr str))
1306     ;; Multiple spaces.
1307     (while (string-match "[ \t][ \t]+" mystr)
1308       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1309                           " "
1310                           (substring mystr (match-end 0)))))
1311     ;; Leading spaces.
1312     (when (string-match "^[ \t]+" mystr)
1313       (setq mystr (substring mystr (match-end 0))))
1314     ;; Trailing spaces.
1315     (when (string-match "[ \t]+$" mystr)
1316       (setq mystr (substring mystr 0 (match-beginning 0))))
1317     mystr))
1318
1319 (defsubst gnus-simplify-subject-re (subject)
1320   "Remove \"Re:\" from subject lines."
1321   (if (string-match message-subject-re-regexp subject)
1322       (substring subject (match-end 0))
1323     subject))
1324
1325 (defun gnus-simplify-subject (subject &optional re-only)
1326   "Remove `Re:' and words in parentheses.
1327 If RE-ONLY is non-nil, strip leading `Re:'s only."
1328   (let ((case-fold-search t))           ;Ignore case.
1329     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1330     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1331       (setq subject (substring subject (match-end 0))))
1332     ;; Remove uninteresting prefixes.
1333     (when (and (not re-only)
1334                gnus-simplify-ignored-prefixes
1335                (string-match gnus-simplify-ignored-prefixes subject))
1336       (setq subject (substring subject (match-end 0))))
1337     ;; Remove words in parentheses from end.
1338     (unless re-only
1339       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1340         (setq subject (substring subject 0 (match-beginning 0)))))
1341     ;; Return subject string.
1342     subject))
1343
1344 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1345 ;; all whitespace.
1346 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1347   (goto-char (point-min))
1348   (while (re-search-forward regexp nil t)
1349     (replace-match (or newtext ""))))
1350
1351 (defun gnus-simplify-buffer-fuzzy ()
1352   "Simplify string in the buffer fuzzily.
1353 The string in the accessible portion of the current buffer is simplified.
1354 It is assumed to be a single-line subject.
1355 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1356 matter is removed.  Additional things can be deleted by setting
1357 `gnus-simplify-subject-fuzzy-regexp'."
1358   (let ((case-fold-search t)
1359         (modified-tick))
1360     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1361
1362     (while (not (eq modified-tick (buffer-modified-tick)))
1363       (setq modified-tick (buffer-modified-tick))
1364       (cond
1365        ((listp gnus-simplify-subject-fuzzy-regexp)
1366         (mapcar 'gnus-simplify-buffer-fuzzy-step
1367                 gnus-simplify-subject-fuzzy-regexp))
1368        (gnus-simplify-subject-fuzzy-regexp
1369         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1370       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1371       (gnus-simplify-buffer-fuzzy-step
1372        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1373       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1374
1375     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1376     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1377     (gnus-simplify-buffer-fuzzy-step " $")
1378     (gnus-simplify-buffer-fuzzy-step "^ +")))
1379
1380 (defun gnus-simplify-subject-fuzzy (subject)
1381   "Simplify a subject string fuzzily.
1382 See `gnus-simplify-buffer-fuzzy' for details."
1383   (save-excursion
1384     (gnus-set-work-buffer)
1385     (let ((case-fold-search t))
1386       ;; Remove uninteresting prefixes.
1387       (when (and gnus-simplify-ignored-prefixes
1388                  (string-match gnus-simplify-ignored-prefixes subject))
1389         (setq subject (substring subject (match-end 0))))
1390       (insert subject)
1391       (inline (gnus-simplify-buffer-fuzzy))
1392       (buffer-string))))
1393
1394 (defsubst gnus-simplify-subject-fully (subject)
1395   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1396   (cond
1397    (gnus-simplify-subject-functions
1398     (gnus-map-function gnus-simplify-subject-functions subject))
1399    ((null gnus-summary-gather-subject-limit)
1400     (gnus-simplify-subject-re subject))
1401    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1402     (gnus-simplify-subject-fuzzy subject))
1403    ((numberp gnus-summary-gather-subject-limit)
1404     (gnus-limit-string (gnus-simplify-subject-re subject)
1405                        gnus-summary-gather-subject-limit))
1406    (t
1407     subject)))
1408
1409 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1410   "Check whether two subjects are equal.
1411 If optional argument simple-first is t, first argument is already
1412 simplified."
1413   (cond
1414    ((null simple-first)
1415     (equal (gnus-simplify-subject-fully s1)
1416            (gnus-simplify-subject-fully s2)))
1417    (t
1418     (equal s1
1419            (gnus-simplify-subject-fully s2)))))
1420
1421 (defun gnus-summary-bubble-group ()
1422   "Increase the score of the current group.
1423 This is a handy function to add to `gnus-summary-exit-hook' to
1424 increase the score of each group you read."
1425   (gnus-group-add-score gnus-newsgroup-name))
1426
1427 \f
1428 ;;;
1429 ;;; Gnus summary mode
1430 ;;;
1431
1432 (put 'gnus-summary-mode 'mode-class 'special)
1433
1434 (defvar gnus-article-commands-menu)
1435
1436 (when t
1437   ;; Non-orthogonal keys
1438
1439   (gnus-define-keys gnus-summary-mode-map
1440     " " gnus-summary-next-page
1441     "\177" gnus-summary-prev-page
1442     [delete] gnus-summary-prev-page
1443     [backspace] gnus-summary-prev-page
1444     "\r" gnus-summary-scroll-up
1445     "\M-\r" gnus-summary-scroll-down
1446     "n" gnus-summary-next-unread-article
1447     "p" gnus-summary-prev-unread-article
1448     "N" gnus-summary-next-article
1449     "P" gnus-summary-prev-article
1450     "\M-\C-n" gnus-summary-next-same-subject
1451     "\M-\C-p" gnus-summary-prev-same-subject
1452     "\M-n" gnus-summary-next-unread-subject
1453     "\M-p" gnus-summary-prev-unread-subject
1454     "." gnus-summary-first-unread-article
1455     "," gnus-summary-best-unread-article
1456     "\M-s" gnus-summary-search-article-forward
1457     "\M-r" gnus-summary-search-article-backward
1458     "<" gnus-summary-beginning-of-article
1459     ">" gnus-summary-end-of-article
1460     "j" gnus-summary-goto-article
1461     "^" gnus-summary-refer-parent-article
1462     "\M-^" gnus-summary-refer-article
1463     "u" gnus-summary-tick-article-forward
1464     "!" gnus-summary-tick-article-forward
1465     "U" gnus-summary-tick-article-backward
1466     "d" gnus-summary-mark-as-read-forward
1467     "D" gnus-summary-mark-as-read-backward
1468     "E" gnus-summary-mark-as-expirable
1469     "\M-u" gnus-summary-clear-mark-forward
1470     "\M-U" gnus-summary-clear-mark-backward
1471     "k" gnus-summary-kill-same-subject-and-select
1472     "\C-k" gnus-summary-kill-same-subject
1473     "\M-\C-k" gnus-summary-kill-thread
1474     "\M-\C-l" gnus-summary-lower-thread
1475     "e" gnus-summary-edit-article
1476     "#" gnus-summary-mark-as-processable
1477     "\M-#" gnus-summary-unmark-as-processable
1478     "\M-\C-t" gnus-summary-toggle-threads
1479     "\M-\C-s" gnus-summary-show-thread
1480     "\M-\C-h" gnus-summary-hide-thread
1481     "\M-\C-f" gnus-summary-next-thread
1482     "\M-\C-b" gnus-summary-prev-thread
1483     [(meta down)] gnus-summary-next-thread
1484     [(meta up)] gnus-summary-prev-thread
1485     "\M-\C-u" gnus-summary-up-thread
1486     "\M-\C-d" gnus-summary-down-thread
1487     "&" gnus-summary-execute-command
1488     "c" gnus-summary-catchup-and-exit
1489     "\C-w" gnus-summary-mark-region-as-read
1490     "\C-t" gnus-summary-toggle-truncation
1491     "?" gnus-summary-mark-as-dormant
1492     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1493     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1494     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1495     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1496     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1497     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1498     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1499     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1500     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1501     "=" gnus-summary-expand-window
1502     "\C-x\C-s" gnus-summary-reselect-current-group
1503     "\M-g" gnus-summary-rescan-group
1504     "w" gnus-summary-stop-page-breaking
1505     "\C-c\C-r" gnus-summary-caesar-message
1506     "\M-t" gnus-summary-toggle-mime
1507     "f" gnus-summary-followup
1508     "F" gnus-summary-followup-with-original
1509     "C" gnus-summary-cancel-article
1510     "r" gnus-summary-reply
1511     "R" gnus-summary-reply-with-original
1512     "\C-c\C-f" gnus-summary-mail-forward
1513     "o" gnus-summary-save-article
1514     "\C-o" gnus-summary-save-article-mail
1515     "|" gnus-summary-pipe-output
1516     "\M-k" gnus-summary-edit-local-kill
1517     "\M-K" gnus-summary-edit-global-kill
1518     ;; "V" gnus-version
1519     "\C-c\C-d" gnus-summary-describe-group
1520     "q" gnus-summary-exit
1521     "Q" gnus-summary-exit-no-update
1522     "\C-c\C-i" gnus-info-find-node
1523     gnus-mouse-2 gnus-mouse-pick-article
1524     "m" gnus-summary-mail-other-window
1525     "a" gnus-summary-post-news
1526     "x" gnus-summary-limit-to-unread
1527     "s" gnus-summary-isearch-article
1528     "t" gnus-article-toggle-headers
1529     "g" gnus-summary-show-article
1530     "l" gnus-summary-goto-last-article
1531     "v" gnus-summary-preview-mime-message
1532     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1533     "\C-d" gnus-summary-enter-digest-group
1534     "\M-\C-d" gnus-summary-read-document
1535     "\M-\C-e" gnus-summary-edit-parameters
1536     "\M-\C-a" gnus-summary-customize-parameters
1537     "\C-c\C-b" gnus-bug
1538     "*" gnus-cache-enter-article
1539     "\M-*" gnus-cache-remove-article
1540     "\M-&" gnus-summary-universal-argument
1541     "\C-l" gnus-recenter
1542     "I" gnus-summary-increase-score
1543     "L" gnus-summary-lower-score
1544     "\M-i" gnus-symbolic-argument
1545     "h" gnus-summary-select-article-buffer
1546
1547     "V" gnus-summary-score-map
1548     "X" gnus-uu-extract-map
1549     "S" gnus-summary-send-map)
1550
1551   ;; Sort of orthogonal keymap
1552   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1553     "t" gnus-summary-tick-article-forward
1554     "!" gnus-summary-tick-article-forward
1555     "d" gnus-summary-mark-as-read-forward
1556     "r" gnus-summary-mark-as-read-forward
1557     "c" gnus-summary-clear-mark-forward
1558     " " gnus-summary-clear-mark-forward
1559     "e" gnus-summary-mark-as-expirable
1560     "x" gnus-summary-mark-as-expirable
1561     "?" gnus-summary-mark-as-dormant
1562     "b" gnus-summary-set-bookmark
1563     "B" gnus-summary-remove-bookmark
1564     "#" gnus-summary-mark-as-processable
1565     "\M-#" gnus-summary-unmark-as-processable
1566     "S" gnus-summary-limit-include-expunged
1567     "C" gnus-summary-catchup
1568     "H" gnus-summary-catchup-to-here
1569     "h" gnus-summary-catchup-from-here
1570     "\C-c" gnus-summary-catchup-all
1571     "k" gnus-summary-kill-same-subject-and-select
1572     "K" gnus-summary-kill-same-subject
1573     "P" gnus-uu-mark-map)
1574
1575   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1576     "c" gnus-summary-clear-above
1577     "u" gnus-summary-tick-above
1578     "m" gnus-summary-mark-above
1579     "k" gnus-summary-kill-below)
1580
1581   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1582     "/" gnus-summary-limit-to-subject
1583     "n" gnus-summary-limit-to-articles
1584     "w" gnus-summary-pop-limit
1585     "s" gnus-summary-limit-to-subject
1586     "a" gnus-summary-limit-to-author
1587     "u" gnus-summary-limit-to-unread
1588     "m" gnus-summary-limit-to-marks
1589     "M" gnus-summary-limit-exclude-marks
1590     "v" gnus-summary-limit-to-score
1591     "*" gnus-summary-limit-include-cached
1592     "D" gnus-summary-limit-include-dormant
1593     "T" gnus-summary-limit-include-thread
1594     "d" gnus-summary-limit-exclude-dormant
1595     "t" gnus-summary-limit-to-age
1596     "x" gnus-summary-limit-to-extra
1597     "E" gnus-summary-limit-include-expunged
1598     "c" gnus-summary-limit-exclude-childless-dormant
1599     "C" gnus-summary-limit-mark-excluded-as-read
1600     "o" gnus-summary-insert-old-articles
1601     "N" gnus-summary-insert-new-articles)
1602
1603   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1604     "n" gnus-summary-next-unread-article
1605     "p" gnus-summary-prev-unread-article
1606     "N" gnus-summary-next-article
1607     "P" gnus-summary-prev-article
1608     "\C-n" gnus-summary-next-same-subject
1609     "\C-p" gnus-summary-prev-same-subject
1610     "\M-n" gnus-summary-next-unread-subject
1611     "\M-p" gnus-summary-prev-unread-subject
1612     "f" gnus-summary-first-unread-article
1613     "b" gnus-summary-best-unread-article
1614     "j" gnus-summary-goto-article
1615     "g" gnus-summary-goto-subject
1616     "l" gnus-summary-goto-last-article
1617     "o" gnus-summary-pop-article)
1618
1619   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1620     "k" gnus-summary-kill-thread
1621     "l" gnus-summary-lower-thread
1622     "i" gnus-summary-raise-thread
1623     "T" gnus-summary-toggle-threads
1624     "t" gnus-summary-rethread-current
1625     "^" gnus-summary-reparent-thread
1626     "s" gnus-summary-show-thread
1627     "S" gnus-summary-show-all-threads
1628     "h" gnus-summary-hide-thread
1629     "H" gnus-summary-hide-all-threads
1630     "n" gnus-summary-next-thread
1631     "p" gnus-summary-prev-thread
1632     "u" gnus-summary-up-thread
1633     "o" gnus-summary-top-thread
1634     "d" gnus-summary-down-thread
1635     "#" gnus-uu-mark-thread
1636     "\M-#" gnus-uu-unmark-thread)
1637
1638   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1639     "g" gnus-summary-prepare
1640     "c" gnus-summary-insert-cached-articles)
1641
1642   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1643     "c" gnus-summary-catchup-and-exit
1644     "C" gnus-summary-catchup-all-and-exit
1645     "E" gnus-summary-exit-no-update
1646     "J" gnus-summary-jump-to-other-group
1647     "Q" gnus-summary-exit
1648     "Z" gnus-summary-exit
1649     "n" gnus-summary-catchup-and-goto-next-group
1650     "R" gnus-summary-reselect-current-group
1651     "G" gnus-summary-rescan-group
1652     "N" gnus-summary-next-group
1653     "s" gnus-summary-save-newsrc
1654     "P" gnus-summary-prev-group)
1655
1656   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1657     " " gnus-summary-next-page
1658     "n" gnus-summary-next-page
1659     "\177" gnus-summary-prev-page
1660     [delete] gnus-summary-prev-page
1661     "p" gnus-summary-prev-page
1662     "\r" gnus-summary-scroll-up
1663     "\M-\r" gnus-summary-scroll-down
1664     "<" gnus-summary-beginning-of-article
1665     ">" gnus-summary-end-of-article
1666     "b" gnus-summary-beginning-of-article
1667     "e" gnus-summary-end-of-article
1668     "^" gnus-summary-refer-parent-article
1669     "r" gnus-summary-refer-parent-article
1670     "D" gnus-summary-enter-digest-group
1671     "R" gnus-summary-refer-references
1672     "T" gnus-summary-refer-thread
1673     "g" gnus-summary-show-article
1674     "s" gnus-summary-isearch-article
1675     "P" gnus-summary-print-article
1676     "M" gnus-mailing-list-insinuate
1677     "t" gnus-article-babel)
1678
1679   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1680     "b" gnus-article-add-buttons
1681     "B" gnus-article-add-buttons-to-head
1682     "o" gnus-article-treat-overstrike
1683     "e" gnus-article-emphasize
1684     "w" gnus-article-fill-cited-article
1685     "Q" gnus-article-fill-long-lines
1686     "C" gnus-article-capitalize-sentences
1687     "c" gnus-article-remove-cr
1688     "Z" gnus-article-decode-HZ
1689     "f" gnus-article-display-x-face
1690     "l" gnus-summary-stop-page-breaking
1691     "r" gnus-summary-caesar-message
1692     "t" gnus-article-toggle-headers
1693     "v" gnus-summary-verbose-headers
1694     "m" gnus-summary-toggle-mime
1695     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1696     "p" gnus-article-verify-x-pgp-sig
1697     "d" gnus-article-treat-dumbquotes
1698     "s" gnus-smiley-display)
1699
1700   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1701     "a" gnus-article-hide
1702     "h" gnus-article-toggle-headers
1703     "b" gnus-article-hide-boring-headers
1704     "s" gnus-article-hide-signature
1705     "c" gnus-article-hide-citation
1706     "C" gnus-article-hide-citation-in-followups
1707     "l" gnus-article-hide-list-identifiers
1708     "p" gnus-article-hide-pgp
1709     "B" gnus-article-strip-banner
1710     "P" gnus-article-hide-pem
1711     "\C-c" gnus-article-hide-citation-maybe)
1712
1713   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1714     "a" gnus-article-highlight
1715     "h" gnus-article-highlight-headers
1716     "c" gnus-article-highlight-citation
1717     "s" gnus-article-highlight-signature)
1718
1719   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1720     "z" gnus-article-date-ut
1721     "u" gnus-article-date-ut
1722     "l" gnus-article-date-local
1723     "p" gnus-article-date-english
1724     "e" gnus-article-date-lapsed
1725     "o" gnus-article-date-original
1726     "i" gnus-article-date-iso8601
1727     "s" gnus-article-date-user)
1728
1729   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1730     "t" gnus-article-remove-trailing-blank-lines
1731     "l" gnus-article-strip-leading-blank-lines
1732     "m" gnus-article-strip-multiple-blank-lines
1733     "a" gnus-article-strip-blank-lines
1734     "A" gnus-article-strip-all-blank-lines
1735     "s" gnus-article-strip-leading-space
1736     "e" gnus-article-strip-trailing-space
1737     "w" gnus-article-remove-leading-whitespace)
1738
1739   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1740     "v" gnus-version
1741     "f" gnus-summary-fetch-faq
1742     "d" gnus-summary-describe-group
1743     "h" gnus-summary-describe-briefly
1744     "i" gnus-info-find-node)
1745
1746   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1747     "e" gnus-summary-expire-articles
1748     "\M-\C-e" gnus-summary-expire-articles-now
1749     "\177" gnus-summary-delete-article
1750     [delete] gnus-summary-delete-article
1751     [backspace] gnus-summary-delete-article
1752     "m" gnus-summary-move-article
1753     "r" gnus-summary-respool-article
1754     "w" gnus-summary-edit-article
1755     "c" gnus-summary-copy-article
1756     "B" gnus-summary-crosspost-article
1757     "q" gnus-summary-respool-query
1758     "t" gnus-summary-respool-trace
1759     "i" gnus-summary-import-article
1760     "I" gnus-summary-create-article
1761     "p" gnus-summary-article-posted-p)
1762
1763   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1764     "o" gnus-summary-save-article
1765     "m" gnus-summary-save-article-mail
1766     "F" gnus-summary-write-article-file
1767     "r" gnus-summary-save-article-rmail
1768     "f" gnus-summary-save-article-file
1769     "b" gnus-summary-save-article-body-file
1770     "h" gnus-summary-save-article-folder
1771     "v" gnus-summary-save-article-vm
1772     "p" gnus-summary-pipe-output
1773     "s" gnus-soup-add-article)
1774
1775   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1776     "b" gnus-summary-display-buttonized
1777     "m" gnus-summary-repair-multipart
1778     "v" gnus-article-view-part
1779     "o" gnus-article-save-part
1780     "c" gnus-article-copy-part
1781     "C" gnus-article-view-part-as-charset
1782     "e" gnus-article-externalize-part
1783     "E" gnus-article-encrypt-body
1784     "i" gnus-article-inline-part
1785     "|" gnus-article-pipe-part))
1786
1787 (defvar gnus-article-post-menu nil)
1788
1789 (defun gnus-summary-make-menu-bar ()
1790   (gnus-turn-off-edit-menu 'summary)
1791
1792   (unless (boundp 'gnus-summary-misc-menu)
1793
1794     (easy-menu-define
1795      gnus-summary-kill-menu gnus-summary-mode-map ""
1796      (cons
1797       "Score"
1798       (nconc
1799        (list
1800         ["Customize" gnus-score-customize t])
1801        (gnus-make-score-map 'increase)
1802        (gnus-make-score-map 'lower)
1803        '(("Mark"
1804           ["Kill below" gnus-summary-kill-below t]
1805           ["Mark above" gnus-summary-mark-above t]
1806           ["Tick above" gnus-summary-tick-above t]
1807           ["Clear above" gnus-summary-clear-above t])
1808          ["Current score" gnus-summary-current-score t]
1809          ["Set score" gnus-summary-set-score t]
1810          ["Switch current score file..." gnus-score-change-score-file t]
1811          ["Set mark below..." gnus-score-set-mark-below t]
1812          ["Set expunge below..." gnus-score-set-expunge-below t]
1813          ["Edit current score file" gnus-score-edit-current-scores t]
1814          ["Edit score file" gnus-score-edit-file t]
1815          ["Trace score" gnus-score-find-trace t]
1816          ["Find words" gnus-score-find-favourite-words t]
1817          ["Rescore buffer" gnus-summary-rescore t]
1818          ["Increase score..." gnus-summary-increase-score t]
1819          ["Lower score..." gnus-summary-lower-score t]))))
1820
1821     ;; Define both the Article menu in the summary buffer and the equivalent
1822     ;; Commands menu in the article buffer here for consistency.
1823     (let ((innards
1824            `(("Hide"
1825               ["All" gnus-article-hide t]
1826               ["Headers" gnus-article-toggle-headers t]
1827               ["Signature" gnus-article-hide-signature t]
1828               ["Citation" gnus-article-hide-citation t]
1829               ["List identifiers" gnus-article-hide-list-identifiers t]
1830               ["PGP" gnus-article-hide-pgp t]
1831               ["Banner" gnus-article-strip-banner t]
1832               ["Boring headers" gnus-article-hide-boring-headers t])
1833              ("Highlight"
1834               ["All" gnus-article-highlight t]
1835               ["Headers" gnus-article-highlight-headers t]
1836               ["Signature" gnus-article-highlight-signature t]
1837               ["Citation" gnus-article-highlight-citation t])
1838              ("Date"
1839               ["Local" gnus-article-date-local t]
1840               ["ISO8601" gnus-article-date-iso8601 t]
1841               ["UT" gnus-article-date-ut t]
1842               ["Original" gnus-article-date-original t]
1843               ["Lapsed" gnus-article-date-lapsed t]
1844               ["User-defined" gnus-article-date-user t])
1845              ("Washing"
1846               ("Remove Blanks"
1847                ["Leading" gnus-article-strip-leading-blank-lines t]
1848                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1849                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1850                ["All of the above" gnus-article-strip-blank-lines t]
1851                ["All" gnus-article-strip-all-blank-lines t]
1852                ["Leading space" gnus-article-strip-leading-space t]
1853                ["Trailing space" gnus-article-strip-trailing-space t]
1854                ["Leading space in headers"
1855                 gnus-article-remove-leading-whitespace t])
1856               ["Overstrike" gnus-article-treat-overstrike t]
1857               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1858               ["Emphasis" gnus-article-emphasize t]
1859               ["Word wrap" gnus-article-fill-cited-article t]
1860               ["Fill long lines" gnus-article-fill-long-lines t]
1861               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1862               ["CR" gnus-article-remove-cr t]
1863               ["Show X-Face" gnus-article-display-x-face t]
1864               ["Rot 13" gnus-summary-caesar-message
1865                ,@(if (featurep 'xemacs) '(t)
1866                    '(:help "\"Caesar rotate\" article by 13"))]
1867               ["Unix pipe" gnus-summary-pipe-message t]
1868               ["Add buttons" gnus-article-add-buttons t]
1869               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1870               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1871               ["Toggle MIME" gnus-summary-toggle-mime t]
1872               ["Verbose header" gnus-summary-verbose-headers t]
1873               ["Toggle header" gnus-summary-toggle-header t]
1874               ["Toggle smileys" gnus-smiley-display t]
1875               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1876               ["HZ" gnus-article-decode-HZ t])
1877              ("Output"
1878               ["Save in default format" gnus-summary-save-article
1879                ,@(if (featurep 'xemacs) '(t)
1880                    '(:help "Save article using default method"))]
1881               ["Save in file" gnus-summary-save-article-file
1882                ,@(if (featurep 'xemacs) '(t)
1883                    '(:help "Save article in file"))]
1884               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1885               ["Save in MH folder" gnus-summary-save-article-folder t]
1886               ["Save in VM folder" gnus-summary-save-article-vm t]
1887               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1888               ["Save body in file" gnus-summary-save-article-body-file t]
1889               ["Pipe through a filter" gnus-summary-pipe-output t]
1890               ["Add to SOUP packet" gnus-soup-add-article t]
1891               ["Print" gnus-summary-print-article t])
1892              ("Backend"
1893               ["Respool article..." gnus-summary-respool-article t]
1894               ["Move article..." gnus-summary-move-article
1895                (gnus-check-backend-function
1896                 'request-move-article gnus-newsgroup-name)]
1897               ["Copy article..." gnus-summary-copy-article t]
1898               ["Crosspost article..." gnus-summary-crosspost-article
1899                (gnus-check-backend-function
1900                 'request-replace-article gnus-newsgroup-name)]
1901               ["Import file..." gnus-summary-import-article t]
1902               ["Create article..." gnus-summary-create-article t]
1903               ["Check if posted" gnus-summary-article-posted-p t]
1904               ["Edit article" gnus-summary-edit-article
1905                (not (gnus-group-read-only-p))]
1906               ["Delete article" gnus-summary-delete-article
1907                (gnus-check-backend-function
1908                 'request-expire-articles gnus-newsgroup-name)]
1909               ["Query respool" gnus-summary-respool-query t]
1910               ["Trace respool" gnus-summary-respool-trace t]
1911               ["Delete expirable articles" gnus-summary-expire-articles-now
1912                (gnus-check-backend-function
1913                 'request-expire-articles gnus-newsgroup-name)])
1914              ("Extract"
1915               ["Uudecode" gnus-uu-decode-uu
1916                ,@(if (featurep 'xemacs) '(t)
1917                    '(:help "Decode uuencoded article(s)"))]
1918               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1919               ["Unshar" gnus-uu-decode-unshar t]
1920               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1921               ["Save" gnus-uu-decode-save t]
1922               ["Binhex" gnus-uu-decode-binhex t]
1923               ["Postscript" gnus-uu-decode-postscript t])
1924              ("Cache"
1925               ["Enter article" gnus-cache-enter-article t]
1926               ["Remove article" gnus-cache-remove-article t])
1927              ["Translate" gnus-article-babel t]
1928              ["Select article buffer" gnus-summary-select-article-buffer t]
1929              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1930              ["Isearch article..." gnus-summary-isearch-article t]
1931              ["Beginning of the article" gnus-summary-beginning-of-article t]
1932              ["End of the article" gnus-summary-end-of-article t]
1933              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1934              ["Fetch referenced articles" gnus-summary-refer-references t]
1935              ["Fetch current thread" gnus-summary-refer-thread t]
1936              ["Fetch article with id..." gnus-summary-refer-article t]
1937              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1938              ["Redisplay" gnus-summary-show-article t]
1939              ["Raw article" gnus-summary-show-raw-article t])))
1940       (easy-menu-define
1941        gnus-summary-article-menu gnus-summary-mode-map ""
1942        (cons "Article" innards))
1943
1944       (if (not (keymapp gnus-summary-article-menu))
1945           (easy-menu-define
1946            gnus-article-commands-menu gnus-article-mode-map ""
1947            (cons "Commands" innards))
1948         ;; in Emacs, don't share menu.
1949         (setq gnus-article-commands-menu
1950               (copy-keymap gnus-summary-article-menu))
1951         (define-key gnus-article-mode-map [menu-bar commands]
1952           (cons "Commands" gnus-article-commands-menu))))
1953
1954     (easy-menu-define
1955      gnus-summary-thread-menu gnus-summary-mode-map ""
1956      '("Threads"
1957        ["Toggle threading" gnus-summary-toggle-threads t]
1958        ["Hide threads" gnus-summary-hide-all-threads t]
1959        ["Show threads" gnus-summary-show-all-threads t]
1960        ["Hide thread" gnus-summary-hide-thread t]
1961        ["Show thread" gnus-summary-show-thread t]
1962        ["Go to next thread" gnus-summary-next-thread t]
1963        ["Go to previous thread" gnus-summary-prev-thread t]
1964        ["Go down thread" gnus-summary-down-thread t]
1965        ["Go up thread" gnus-summary-up-thread t]
1966        ["Top of thread" gnus-summary-top-thread t]
1967        ["Mark thread as read" gnus-summary-kill-thread t]
1968        ["Lower thread score" gnus-summary-lower-thread t]
1969        ["Raise thread score" gnus-summary-raise-thread t]
1970        ["Rethread current" gnus-summary-rethread-current t]))
1971
1972     (easy-menu-define
1973      gnus-summary-post-menu gnus-summary-mode-map ""
1974      `("Post"
1975        ["Post an article" gnus-summary-post-news
1976         ,@(if (featurep 'xemacs) '(t)
1977             '(:help "Post an article"))]
1978        ["Followup" gnus-summary-followup
1979         ,@(if (featurep 'xemacs) '(t)
1980             '(:help "Post followup to this article"))]
1981        ["Followup and yank" gnus-summary-followup-with-original
1982         ,@(if (featurep 'xemacs) '(t)
1983             '(:help "Post followup to this article, quoting its contents"))]
1984        ["Supersede article" gnus-summary-supersede-article t]
1985        ["Cancel article" gnus-summary-cancel-article
1986         ,@(if (featurep 'xemacs) '(t)
1987             '(:help "Cancel an article you posted"))]
1988        ["Reply" gnus-summary-reply t]
1989        ["Reply and yank" gnus-summary-reply-with-original t]
1990        ["Wide reply" gnus-summary-wide-reply t]
1991        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1992         ,@(if (featurep 'xemacs) '(t)
1993             '(:help "Mail a reply, quoting this article"))]
1994        ["Mail forward" gnus-summary-mail-forward t]
1995        ["Post forward" gnus-summary-post-forward t]
1996        ["Digest and mail" gnus-summary-digest-mail-forward t]
1997        ["Digest and post" gnus-summary-digest-post-forward t]
1998        ["Resend message" gnus-summary-resend-message t]
1999        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2000        ["Send a mail" gnus-summary-mail-other-window t]
2001        ["Uuencode and post" gnus-uu-post-news
2002         ,@(if (featurep 'xemacs) '(t)
2003             '(:help "Post a uuencoded article"))]
2004        ["Followup via news" gnus-summary-followup-to-mail t]
2005        ["Followup via news and yank"
2006         gnus-summary-followup-to-mail-with-original t]
2007        ;;("Draft"
2008        ;;["Send" gnus-summary-send-draft t]
2009        ;;["Send bounced" gnus-resend-bounced-mail t])
2010        ))
2011
2012     (cond
2013      ((not (keymapp gnus-summary-post-menu))
2014       (setq gnus-article-post-menu gnus-summary-post-menu))
2015      ((not gnus-article-post-menu)
2016       ;; Don't share post menu.
2017       (setq gnus-article-post-menu
2018             (copy-keymap gnus-summary-post-menu))))
2019     (define-key gnus-article-mode-map [menu-bar post]
2020       (cons "Post" gnus-article-post-menu))
2021
2022     (easy-menu-define
2023      gnus-summary-misc-menu gnus-summary-mode-map ""
2024      `("Misc"
2025        ("Mark Read"
2026         ["Mark as read" gnus-summary-mark-as-read-forward t]
2027         ["Mark same subject and select"
2028          gnus-summary-kill-same-subject-and-select t]
2029         ["Mark same subject" gnus-summary-kill-same-subject t]
2030         ["Catchup" gnus-summary-catchup
2031          ,@(if (featurep 'xemacs) '(t)
2032              '(:help "Mark unread articles in this group as read"))]
2033         ["Catchup all" gnus-summary-catchup-all t]
2034         ["Catchup to here" gnus-summary-catchup-to-here t]
2035         ["Catchup from here" gnus-summary-catchup-from-here t]
2036         ["Catchup region" gnus-summary-mark-region-as-read t]
2037         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2038        ("Mark Various"
2039         ["Tick" gnus-summary-tick-article-forward t]
2040         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2041         ["Remove marks" gnus-summary-clear-mark-forward t]
2042         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2043         ["Set bookmark" gnus-summary-set-bookmark t]
2044         ["Remove bookmark" gnus-summary-remove-bookmark t])
2045        ("Mark Limit"
2046         ["Marks..." gnus-summary-limit-to-marks t]
2047         ["Subject..." gnus-summary-limit-to-subject t]
2048         ["Author..." gnus-summary-limit-to-author t]
2049         ["Age..." gnus-summary-limit-to-age t]
2050         ["Extra..." gnus-summary-limit-to-extra t]
2051         ["Score" gnus-summary-limit-to-score t]
2052         ["Unread" gnus-summary-limit-to-unread t]
2053         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2054         ["Articles" gnus-summary-limit-to-articles t]
2055         ["Pop limit" gnus-summary-pop-limit t]
2056         ["Show dormant" gnus-summary-limit-include-dormant t]
2057         ["Hide childless dormant"
2058          gnus-summary-limit-exclude-childless-dormant t]
2059         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2060         ["Hide marked" gnus-summary-limit-exclude-marks t]
2061         ["Show expunged" gnus-summary-limit-include-expunged t])
2062        ("Process Mark"
2063         ["Set mark" gnus-summary-mark-as-processable t]
2064         ["Remove mark" gnus-summary-unmark-as-processable t]
2065         ["Remove all marks" gnus-summary-unmark-all-processable t]
2066         ["Mark above" gnus-uu-mark-over t]
2067         ["Mark series" gnus-uu-mark-series t]
2068         ["Mark region" gnus-uu-mark-region t]
2069         ["Unmark region" gnus-uu-unmark-region t]
2070         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2071         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2072         ["Mark all" gnus-uu-mark-all t]
2073         ["Mark buffer" gnus-uu-mark-buffer t]
2074         ["Mark sparse" gnus-uu-mark-sparse t]
2075         ["Mark thread" gnus-uu-mark-thread t]
2076         ["Unmark thread" gnus-uu-unmark-thread t]
2077         ("Process Mark Sets"
2078          ["Kill" gnus-summary-kill-process-mark t]
2079          ["Yank" gnus-summary-yank-process-mark
2080           gnus-newsgroup-process-stack]
2081          ["Save" gnus-summary-save-process-mark t]))
2082        ("Scroll article"
2083         ["Page forward" gnus-summary-next-page
2084          ,@(if (featurep 'xemacs) '(t)
2085              '(:help "Show next page of article"))]
2086         ["Page backward" gnus-summary-prev-page
2087          ,@(if (featurep 'xemacs) '(t)
2088              '(:help "Show previous page of article"))]
2089         ["Line forward" gnus-summary-scroll-up t])
2090        ("Move"
2091         ["Next unread article" gnus-summary-next-unread-article t]
2092         ["Previous unread article" gnus-summary-prev-unread-article t]
2093         ["Next article" gnus-summary-next-article t]
2094         ["Previous article" gnus-summary-prev-article t]
2095         ["Next unread subject" gnus-summary-next-unread-subject t]
2096         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2097         ["Next article same subject" gnus-summary-next-same-subject t]
2098         ["Previous article same subject" gnus-summary-prev-same-subject t]
2099         ["First unread article" gnus-summary-first-unread-article t]
2100         ["Best unread article" gnus-summary-best-unread-article t]
2101         ["Go to subject number..." gnus-summary-goto-subject t]
2102         ["Go to article number..." gnus-summary-goto-article t]
2103         ["Go to the last article" gnus-summary-goto-last-article t]
2104         ["Pop article off history" gnus-summary-pop-article t])
2105        ("Sort"
2106         ["Sort by number" gnus-summary-sort-by-number t]
2107         ["Sort by author" gnus-summary-sort-by-author t]
2108         ["Sort by subject" gnus-summary-sort-by-subject t]
2109         ["Sort by date" gnus-summary-sort-by-date t]
2110         ["Sort by score" gnus-summary-sort-by-score t]
2111         ["Sort by lines" gnus-summary-sort-by-lines t]
2112         ["Sort by characters" gnus-summary-sort-by-chars t]
2113         ["Original sort" gnus-summary-sort-by-original t])
2114        ("Help"
2115         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2116         ["Describe group" gnus-summary-describe-group t]
2117         ["Read manual" gnus-info-find-node t])
2118        ("Modes"
2119         ["Pick and read" gnus-pick-mode t]
2120         ["Binary" gnus-binary-mode t])
2121        ("Regeneration"
2122         ["Regenerate" gnus-summary-prepare t]
2123         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2124         ["Toggle threading" gnus-summary-toggle-threads t])
2125        ["See old articles" gnus-summary-insert-old-articles t]
2126        ["See new articles" gnus-summary-insert-new-articles t]
2127        ["Filter articles..." gnus-summary-execute-command t]
2128        ["Run command on subjects..." gnus-summary-universal-argument t]
2129        ["Search articles forward..." gnus-summary-search-article-forward t]
2130        ["Search articles backward..." gnus-summary-search-article-backward t]
2131        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2132        ["Expand window" gnus-summary-expand-window t]
2133        ["Expire expirable articles" gnus-summary-expire-articles
2134         (gnus-check-backend-function
2135          'request-expire-articles gnus-newsgroup-name)]
2136        ["Edit local kill file" gnus-summary-edit-local-kill t]
2137        ["Edit main kill file" gnus-summary-edit-global-kill t]
2138        ["Edit group parameters" gnus-summary-edit-parameters t]
2139        ["Customize group parameters" gnus-summary-customize-parameters t]
2140        ["Send a bug report" gnus-bug t]
2141        ("Exit"
2142         ["Catchup and exit" gnus-summary-catchup-and-exit
2143          ,@(if (featurep 'xemacs) '(t)
2144              '(:help "Mark unread articles in this group as read, then exit"))]
2145         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2146         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2147         ["Exit group" gnus-summary-exit
2148          ,@(if (featurep 'xemacs) '(t)
2149              '(:help "Exit current group, return to group selection mode"))]
2150         ["Exit group without updating" gnus-summary-exit-no-update t]
2151         ["Exit and goto next group" gnus-summary-next-group t]
2152         ["Exit and goto prev group" gnus-summary-prev-group t]
2153         ["Reselect group" gnus-summary-reselect-current-group t]
2154         ["Rescan group" gnus-summary-rescan-group t]
2155         ["Update dribble" gnus-summary-save-newsrc t])))
2156
2157     (gnus-run-hooks 'gnus-summary-menu-hook)))
2158
2159 (defvar gnus-summary-tool-bar-map nil)
2160
2161 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2162 (defun gnus-summary-make-tool-bar ()
2163   (if (and (fboundp 'tool-bar-add-item-from-menu)
2164            (default-value 'tool-bar-mode)
2165            (not gnus-summary-tool-bar-map))
2166       (setq gnus-summary-tool-bar-map
2167             (let ((tool-bar-map (make-sparse-keymap))
2168                   (load-path (mm-image-load-path)))
2169               (tool-bar-add-item-from-menu
2170                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2171               (tool-bar-add-item-from-menu
2172                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2173               (tool-bar-add-item-from-menu
2174                'gnus-summary-post-news "post" gnus-summary-mode-map)
2175               (tool-bar-add-item-from-menu
2176                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2177               (tool-bar-add-item-from-menu
2178                'gnus-summary-followup "followup" gnus-summary-mode-map)
2179               (tool-bar-add-item-from-menu
2180                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2181               (tool-bar-add-item-from-menu
2182                'gnus-summary-reply "reply" gnus-summary-mode-map)
2183               (tool-bar-add-item-from-menu
2184                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2185               (tool-bar-add-item-from-menu
2186                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2187               (tool-bar-add-item-from-menu
2188                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2189               (tool-bar-add-item-from-menu
2190                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2191               (tool-bar-add-item-from-menu
2192                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2193               (tool-bar-add-item-from-menu
2194                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2195               (tool-bar-add-item-from-menu
2196                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2197               (tool-bar-add-item-from-menu
2198                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2199               tool-bar-map)))
2200   (if gnus-summary-tool-bar-map
2201       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2202
2203 (defun gnus-score-set-default (var value)
2204   "A version of set that updates the GNU Emacs menu-bar."
2205   (set var value)
2206   ;; It is the message that forces the active status to be updated.
2207   (message ""))
2208
2209 (defun gnus-make-score-map (type)
2210   "Make a summary score map of type TYPE."
2211   (if t
2212       nil
2213     (let ((headers '(("author" "from" string)
2214                      ("subject" "subject" string)
2215                      ("article body" "body" string)
2216                      ("article head" "head" string)
2217                      ("xref" "xref" string)
2218                      ("extra header" "extra" string)
2219                      ("lines" "lines" number)
2220                      ("followups to author" "followup" string)))
2221           (types '((number ("less than" <)
2222                            ("greater than" >)
2223                            ("equal" =))
2224                    (string ("substring" s)
2225                            ("exact string" e)
2226                            ("fuzzy string" f)
2227                            ("regexp" r))))
2228           (perms '(("temporary" (current-time-string))
2229                    ("permanent" nil)
2230                    ("immediate" now)))
2231           header)
2232       (list
2233        (apply
2234         'nconc
2235         (list
2236          (if (eq type 'lower)
2237              "Lower score"
2238            "Increase score"))
2239         (let (outh)
2240           (while headers
2241             (setq header (car headers))
2242             (setq outh
2243                   (cons
2244                    (apply
2245                     'nconc
2246                     (list (car header))
2247                     (let ((ts (cdr (assoc (nth 2 header) types)))
2248                           outt)
2249                       (while ts
2250                         (setq outt
2251                               (cons
2252                                (apply
2253                                 'nconc
2254                                 (list (caar ts))
2255                                 (let ((ps perms)
2256                                       outp)
2257                                   (while ps
2258                                     (setq outp
2259                                           (cons
2260                                            (vector
2261                                             (caar ps)
2262                                             (list
2263                                              'gnus-summary-score-entry
2264                                              (nth 1 header)
2265                                              (if (or (string= (nth 1 header)
2266                                                               "head")
2267                                                      (string= (nth 1 header)
2268                                                               "body"))
2269                                                  ""
2270                                                (list 'gnus-summary-header
2271                                                      (nth 1 header)))
2272                                              (list 'quote (nth 1 (car ts)))
2273                                              (list 'gnus-score-delta-default
2274                                                    nil)
2275                                              (nth 1 (car ps))
2276                                              t)
2277                                             t)
2278                                            outp))
2279                                     (setq ps (cdr ps)))
2280                                   (list (nreverse outp))))
2281                                outt))
2282                         (setq ts (cdr ts)))
2283                       (list (nreverse outt))))
2284                    outh))
2285             (setq headers (cdr headers)))
2286           (list (nreverse outh))))))))
2287
2288 \f
2289
2290 (defun gnus-summary-mode (&optional group)
2291   "Major mode for reading articles.
2292
2293 All normal editing commands are switched off.
2294 \\<gnus-summary-mode-map>
2295 Each line in this buffer represents one article.  To read an
2296 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2297 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2298 respectively.
2299
2300 You can also post articles and send mail from this buffer.  To
2301 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2302 of an article, type `\\[gnus-summary-reply]'.
2303
2304 There are approx. one gazillion commands you can execute in this
2305 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2306
2307 The following commands are available:
2308
2309 \\{gnus-summary-mode-map}"
2310   (interactive)
2311   (kill-all-local-variables)
2312   (when (gnus-visual-p 'summary-menu 'menu)
2313     (gnus-summary-make-menu-bar)
2314     (gnus-summary-make-tool-bar))
2315   (gnus-summary-make-local-variables)
2316   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2317     (gnus-summary-make-local-variables))
2318   (gnus-make-thread-indent-array)
2319   (gnus-simplify-mode-line)
2320   (setq major-mode 'gnus-summary-mode)
2321   (setq mode-name "Summary")
2322   (make-local-variable 'minor-mode-alist)
2323   (use-local-map gnus-summary-mode-map)
2324   (buffer-disable-undo)
2325   (setq buffer-read-only t)             ;Disable modification
2326   (setq truncate-lines t)
2327   (setq selective-display t)
2328   (setq selective-display-ellipses t)   ;Display `...'
2329   (gnus-summary-set-display-table)
2330   (gnus-set-default-directory)
2331   (setq gnus-newsgroup-name group)
2332   (unless (gnus-news-group-p group)
2333     (setq gnus-newsgroup-incorporated
2334           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2335   (make-local-variable 'gnus-summary-line-format)
2336   (make-local-variable 'gnus-summary-line-format-spec)
2337   (make-local-variable 'gnus-summary-dummy-line-format)
2338   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2339   (make-local-variable 'gnus-summary-mark-positions)
2340   (make-local-hook 'pre-command-hook)
2341   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2342   (gnus-run-hooks 'gnus-summary-mode-hook)
2343   (turn-on-gnus-mailing-list-mode)
2344   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2345   (gnus-update-summary-mark-positions))
2346
2347 (defun gnus-summary-make-local-variables ()
2348   "Make all the local summary buffer variables."
2349   (let (global)
2350     (dolist (local gnus-summary-local-variables)
2351       (if (consp local)
2352           (progn
2353             (if (eq (cdr local) 'global)
2354                 ;; Copy the global value of the variable.
2355                 (setq global (symbol-value (car local)))
2356               ;; Use the value from the list.
2357               (setq global (eval (cdr local))))
2358             (set (make-local-variable (car local)) global))
2359         ;; Simple nil-valued local variable.
2360         (set (make-local-variable local) nil)))))
2361
2362 (defun gnus-summary-clear-local-variables ()
2363   (let ((locals gnus-summary-local-variables))
2364     (while locals
2365       (if (consp (car locals))
2366           (and (vectorp (caar locals))
2367                (set (caar locals) nil))
2368         (and (vectorp (car locals))
2369              (set (car locals) nil)))
2370       (setq locals (cdr locals)))))
2371
2372 ;; Summary data functions.
2373
2374 (defmacro gnus-data-number (data)
2375   `(car ,data))
2376
2377 (defmacro gnus-data-set-number (data number)
2378   `(setcar ,data ,number))
2379
2380 (defmacro gnus-data-mark (data)
2381   `(nth 1 ,data))
2382
2383 (defmacro gnus-data-set-mark (data mark)
2384   `(setcar (nthcdr 1 ,data) ,mark))
2385
2386 (defmacro gnus-data-pos (data)
2387   `(nth 2 ,data))
2388
2389 (defmacro gnus-data-set-pos (data pos)
2390   `(setcar (nthcdr 2 ,data) ,pos))
2391
2392 (defmacro gnus-data-header (data)
2393   `(nth 3 ,data))
2394
2395 (defmacro gnus-data-set-header (data header)
2396   `(setcar (nthcdr 3 ,data) ,header))
2397
2398 (defmacro gnus-data-level (data)
2399   `(nth 4 ,data))
2400
2401 (defmacro gnus-data-unread-p (data)
2402   `(= (nth 1 ,data) gnus-unread-mark))
2403
2404 (defmacro gnus-data-read-p (data)
2405   `(/= (nth 1 ,data) gnus-unread-mark))
2406
2407 (defmacro gnus-data-pseudo-p (data)
2408   `(consp (nth 3 ,data)))
2409
2410 (defmacro gnus-data-find (number)
2411   `(assq ,number gnus-newsgroup-data))
2412
2413 (defmacro gnus-data-find-list (number &optional data)
2414   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2415      (memq (assq ,number bdata)
2416            bdata)))
2417
2418 (defmacro gnus-data-make (number mark pos header level)
2419   `(list ,number ,mark ,pos ,header ,level))
2420
2421 (defun gnus-data-enter (after-article number mark pos header level offset)
2422   (let ((data (gnus-data-find-list after-article)))
2423     (unless data
2424       (error "No such article: %d" after-article))
2425     (setcdr data (cons (gnus-data-make number mark pos header level)
2426                        (cdr data)))
2427     (setq gnus-newsgroup-data-reverse nil)
2428     (gnus-data-update-list (cddr data) offset)))
2429
2430 (defun gnus-data-enter-list (after-article list &optional offset)
2431   (when list
2432     (let ((data (and after-article (gnus-data-find-list after-article)))
2433           (ilist list))
2434       (if (not (or data
2435                    after-article))
2436           (let ((odata gnus-newsgroup-data))
2437             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2438             (when offset
2439               (gnus-data-update-list odata offset)))
2440         ;; Find the last element in the list to be spliced into the main
2441         ;; list.
2442         (while (cdr list)
2443           (setq list (cdr list)))
2444         (if (not data)
2445             (progn
2446               (setcdr list gnus-newsgroup-data)
2447               (setq gnus-newsgroup-data ilist)
2448               (when offset
2449                 (gnus-data-update-list (cdr list) offset)))
2450           (setcdr list (cdr data))
2451           (setcdr data ilist)
2452           (when offset
2453             (gnus-data-update-list (cdr list) offset))))
2454       (setq gnus-newsgroup-data-reverse nil))))
2455
2456 (defun gnus-data-remove (article &optional offset)
2457   (let ((data gnus-newsgroup-data))
2458     (if (= (gnus-data-number (car data)) article)
2459         (progn
2460           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2461                 gnus-newsgroup-data-reverse nil)
2462           (when offset
2463             (gnus-data-update-list gnus-newsgroup-data offset)))
2464       (while (cdr data)
2465         (when (= (gnus-data-number (cadr data)) article)
2466           (setcdr data (cddr data))
2467           (when offset
2468             (gnus-data-update-list (cdr data) offset))
2469           (setq data nil
2470                 gnus-newsgroup-data-reverse nil))
2471         (setq data (cdr data))))))
2472
2473 (defmacro gnus-data-list (backward)
2474   `(if ,backward
2475        (or gnus-newsgroup-data-reverse
2476            (setq gnus-newsgroup-data-reverse
2477                  (reverse gnus-newsgroup-data)))
2478      gnus-newsgroup-data))
2479
2480 (defun gnus-data-update-list (data offset)
2481   "Add OFFSET to the POS of all data entries in DATA."
2482   (setq gnus-newsgroup-data-reverse nil)
2483   (while data
2484     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2485     (setq data (cdr data))))
2486
2487 (defun gnus-summary-article-pseudo-p (article)
2488   "Say whether this article is a pseudo article or not."
2489   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2490
2491 (defmacro gnus-summary-article-sparse-p (article)
2492   "Say whether this article is a sparse article or not."
2493   `(memq ,article gnus-newsgroup-sparse))
2494
2495 (defmacro gnus-summary-article-ancient-p (article)
2496   "Say whether this article is a sparse article or not."
2497   `(memq ,article gnus-newsgroup-ancient))
2498
2499 (defun gnus-article-parent-p (number)
2500   "Say whether this article is a parent or not."
2501   (let ((data (gnus-data-find-list number)))
2502     (and (cdr data)                     ; There has to be an article after...
2503          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2504             (gnus-data-level (nth 1 data))))))
2505
2506 (defun gnus-article-children (number)
2507   "Return a list of all children to NUMBER."
2508   (let* ((data (gnus-data-find-list number))
2509          (level (gnus-data-level (car data)))
2510          children)
2511     (setq data (cdr data))
2512     (while (and data
2513                 (= (gnus-data-level (car data)) (1+ level)))
2514       (push (gnus-data-number (car data)) children)
2515       (setq data (cdr data)))
2516     children))
2517
2518 (defmacro gnus-summary-skip-intangible ()
2519   "If the current article is intangible, then jump to a different article."
2520   '(let ((to (get-text-property (point) 'gnus-intangible)))
2521      (and to (gnus-summary-goto-subject to))))
2522
2523 (defmacro gnus-summary-article-intangible-p ()
2524   "Say whether this article is intangible or not."
2525   '(get-text-property (point) 'gnus-intangible))
2526
2527 (defun gnus-article-read-p (article)
2528   "Say whether ARTICLE is read or not."
2529   (not (or (memq article gnus-newsgroup-marked)
2530            (memq article gnus-newsgroup-unreads)
2531            (memq article gnus-newsgroup-unselected)
2532            (memq article gnus-newsgroup-dormant))))
2533
2534 ;; Some summary mode macros.
2535
2536 (defmacro gnus-summary-article-number ()
2537   "The article number of the article on the current line.
2538 If there isn's an article number here, then we return the current
2539 article number."
2540   '(progn
2541      (gnus-summary-skip-intangible)
2542      (or (get-text-property (point) 'gnus-number)
2543          (gnus-summary-last-subject))))
2544
2545 (defmacro gnus-summary-article-header (&optional number)
2546   "Return the header of article NUMBER."
2547   `(gnus-data-header (gnus-data-find
2548                       ,(or number '(gnus-summary-article-number)))))
2549
2550 (defmacro gnus-summary-thread-level (&optional number)
2551   "Return the level of thread that starts with article NUMBER."
2552   `(if (and (eq gnus-summary-make-false-root 'dummy)
2553             (get-text-property (point) 'gnus-intangible))
2554        0
2555      (gnus-data-level (gnus-data-find
2556                        ,(or number '(gnus-summary-article-number))))))
2557
2558 (defmacro gnus-summary-article-mark (&optional number)
2559   "Return the mark of article NUMBER."
2560   `(gnus-data-mark (gnus-data-find
2561                     ,(or number '(gnus-summary-article-number)))))
2562
2563 (defmacro gnus-summary-article-pos (&optional number)
2564   "Return the position of the line of article NUMBER."
2565   `(gnus-data-pos (gnus-data-find
2566                    ,(or number '(gnus-summary-article-number)))))
2567
2568 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2569 (defmacro gnus-summary-article-subject (&optional number)
2570   "Return current subject string or nil if nothing."
2571   `(let ((headers
2572           ,(if number
2573                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2574              '(gnus-data-header (assq (gnus-summary-article-number)
2575                                       gnus-newsgroup-data)))))
2576      (and headers
2577           (vectorp headers)
2578           (mail-header-subject headers))))
2579
2580 (defmacro gnus-summary-article-score (&optional number)
2581   "Return current article score."
2582   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2583                   gnus-newsgroup-scored))
2584        gnus-summary-default-score 0))
2585
2586 (defun gnus-summary-article-children (&optional number)
2587   "Return a list of article numbers that are children of article NUMBER."
2588   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2589          (level (gnus-data-level (car data)))
2590          l children)
2591     (while (and (setq data (cdr data))
2592                 (> (setq l (gnus-data-level (car data))) level))
2593       (and (= (1+ level) l)
2594            (push (gnus-data-number (car data))
2595                  children)))
2596     (nreverse children)))
2597
2598 (defun gnus-summary-article-parent (&optional number)
2599   "Return the article number of the parent of article NUMBER."
2600   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2601                                     (gnus-data-list t)))
2602          (level (gnus-data-level (car data))))
2603     (if (zerop level)
2604         ()                              ; This is a root.
2605       ;; We search until we find an article with a level less than
2606       ;; this one.  That function has to be the parent.
2607       (while (and (setq data (cdr data))
2608                   (not (< (gnus-data-level (car data)) level))))
2609       (and data (gnus-data-number (car data))))))
2610
2611 (defun gnus-unread-mark-p (mark)
2612   "Say whether MARK is the unread mark."
2613   (= mark gnus-unread-mark))
2614
2615 (defun gnus-read-mark-p (mark)
2616   "Say whether MARK is one of the marks that mark as read.
2617 This is all marks except unread, ticked, dormant, and expirable."
2618   (not (or (= mark gnus-unread-mark)
2619            (= mark gnus-ticked-mark)
2620            (= mark gnus-dormant-mark)
2621            (= mark gnus-expirable-mark))))
2622
2623 (defmacro gnus-article-mark (number)
2624   "Return the MARK of article NUMBER.
2625 This macro should only be used when computing the mark the \"first\"
2626 time; i.e., when generating the summary lines.  After that,
2627 `gnus-summary-article-mark' should be used to examine the
2628 marks of articles."
2629   `(cond
2630     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2631     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2632     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2633     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2634     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2635     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2636     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2637     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2638            gnus-ancient-mark))))
2639
2640 ;; Saving hidden threads.
2641
2642 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2643 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2644
2645 (defmacro gnus-save-hidden-threads (&rest forms)
2646   "Save hidden threads, eval FORMS, and restore the hidden threads."
2647   (let ((config (make-symbol "config")))
2648     `(let ((,config (gnus-hidden-threads-configuration)))
2649        (unwind-protect
2650            (save-excursion
2651              ,@forms)
2652          (gnus-restore-hidden-threads-configuration ,config)))))
2653
2654 (defun gnus-data-compute-positions ()
2655   "Compute the positions of all articles."
2656   (setq gnus-newsgroup-data-reverse nil)
2657   (let ((data gnus-newsgroup-data))
2658     (save-excursion
2659       (gnus-save-hidden-threads
2660         (gnus-summary-show-all-threads)
2661         (goto-char (point-min))
2662         (while data
2663           (while (get-text-property (point) 'gnus-intangible)
2664             (forward-line 1))
2665           (gnus-data-set-pos (car data) (+ (point) 3))
2666           (setq data (cdr data))
2667           (forward-line 1))))))
2668
2669 (defun gnus-hidden-threads-configuration ()
2670   "Return the current hidden threads configuration."
2671   (save-excursion
2672     (let (config)
2673       (goto-char (point-min))
2674       (while (search-forward "\r" nil t)
2675         (push (1- (point)) config))
2676       config)))
2677
2678 (defun gnus-restore-hidden-threads-configuration (config)
2679   "Restore hidden threads configuration from CONFIG."
2680   (save-excursion
2681     (let (point buffer-read-only)
2682       (while (setq point (pop config))
2683         (when (and (< point (point-max))
2684                    (goto-char point)
2685                    (eq (char-after) ?\n))
2686           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2687
2688 ;; Various summary mode internalish functions.
2689
2690 (defun gnus-mouse-pick-article (e)
2691   (interactive "e")
2692   (mouse-set-point e)
2693   (gnus-summary-next-page nil t))
2694
2695 (defun gnus-summary-set-display-table ()
2696   "Change the display table.
2697 Odd characters have a tendency to mess
2698 up nicely formatted displays - we make all possible glyphs
2699 display only a single character."
2700
2701   ;; We start from the standard display table, if any.
2702   (let ((table (or (copy-sequence standard-display-table)
2703                    (make-display-table)))
2704         (i 32))
2705     ;; Nix out all the control chars...
2706     (while (>= (setq i (1- i)) 0)
2707       (aset table i [??]))
2708     ;; ... but not newline and cr, of course.  (cr is necessary for the
2709     ;; selective display).
2710     (aset table ?\n nil)
2711     (aset table ?\r nil)
2712     ;; We keep TAB as well.
2713     (aset table ?\t nil)
2714     ;; We nix out any glyphs over 126 that are not set already.
2715     (let ((i 256))
2716       (while (>= (setq i (1- i)) 127)
2717         ;; Only modify if the entry is nil.
2718         (unless (aref table i)
2719           (aset table i [??]))))
2720     (setq buffer-display-table table)))
2721
2722 (defun gnus-summary-set-article-display-arrow (pos)
2723   "Update the overlay arrow to point to line at position POS."
2724   (when (and gnus-summary-display-arrow
2725              (boundp 'overlay-arrow-position)
2726              (boundp 'overlay-arrow-string))
2727     (save-excursion
2728       (goto-char pos)
2729       (beginning-of-line)
2730       (unless overlay-arrow-position
2731         (setq overlay-arrow-position (make-marker)))
2732       (setq overlay-arrow-string "=>"
2733             overlay-arrow-position (set-marker overlay-arrow-position
2734                                                (point)
2735                                                (current-buffer))))))
2736
2737 (defun gnus-summary-buffer-name (group)
2738   "Return the summary buffer name of GROUP."
2739   (concat "*Summary " group "*"))
2740
2741 (defun gnus-summary-setup-buffer (group)
2742   "Initialize summary buffer."
2743   (let ((buffer (gnus-summary-buffer-name group)))
2744     (if (get-buffer buffer)
2745         (progn
2746           (set-buffer buffer)
2747           (setq gnus-summary-buffer (current-buffer))
2748           (not gnus-newsgroup-prepared))
2749       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2750       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2751       (gnus-summary-mode group)
2752       (when gnus-carpal
2753         (gnus-carpal-setup-buffer 'summary))
2754       (unless gnus-single-article-buffer
2755         (make-local-variable 'gnus-article-buffer)
2756         (make-local-variable 'gnus-article-current)
2757         (make-local-variable 'gnus-original-article-buffer))
2758       (setq gnus-newsgroup-name group)
2759       ;; Set any local variables in the group parameters.
2760       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2761       t)))
2762
2763 (defun gnus-set-global-variables ()
2764   "Set the global equivalents of the buffer-local variables.
2765 They are set to the latest values they had.  These reflect the summary
2766 buffer that was in action when the last article was fetched."
2767   (when (eq major-mode 'gnus-summary-mode)
2768     (setq gnus-summary-buffer (current-buffer))
2769     (let ((name gnus-newsgroup-name)
2770           (marked gnus-newsgroup-marked)
2771           (unread gnus-newsgroup-unreads)
2772           (headers gnus-current-headers)
2773           (data gnus-newsgroup-data)
2774           (summary gnus-summary-buffer)
2775           (article-buffer gnus-article-buffer)
2776           (original gnus-original-article-buffer)
2777           (gac gnus-article-current)
2778           (reffed gnus-reffed-article-number)
2779           (score-file gnus-current-score-file)
2780           (default-charset gnus-newsgroup-charset)
2781           vlist)
2782       (let ((locals gnus-newsgroup-variables))
2783         (while locals
2784           (if (consp (car locals))
2785               (push (eval (caar locals)) vlist)
2786             (push (eval (car locals)) vlist))
2787           (setq locals (cdr locals)))
2788         (setq vlist (nreverse vlist)))
2789       (save-excursion
2790         (set-buffer gnus-group-buffer)
2791         (setq gnus-newsgroup-name name
2792               gnus-newsgroup-marked marked
2793               gnus-newsgroup-unreads unread
2794               gnus-current-headers headers
2795               gnus-newsgroup-data data
2796               gnus-article-current gac
2797               gnus-summary-buffer summary
2798               gnus-article-buffer article-buffer
2799               gnus-original-article-buffer original
2800               gnus-reffed-article-number reffed
2801               gnus-current-score-file score-file
2802               gnus-newsgroup-charset default-charset)
2803         (let ((locals gnus-newsgroup-variables))
2804           (while locals
2805             (if (consp (car locals))
2806                 (set (caar locals) (pop vlist))
2807               (set (car locals) (pop vlist)))
2808             (setq locals (cdr locals))))
2809         ;; The article buffer also has local variables.
2810         (when (gnus-buffer-live-p gnus-article-buffer)
2811           (set-buffer gnus-article-buffer)
2812           (setq gnus-summary-buffer summary))))))
2813
2814 (defun gnus-summary-article-unread-p (article)
2815   "Say whether ARTICLE is unread or not."
2816   (memq article gnus-newsgroup-unreads))
2817
2818 (defun gnus-summary-first-article-p (&optional article)
2819   "Return whether ARTICLE is the first article in the buffer."
2820   (if (not (setq article (or article (gnus-summary-article-number))))
2821       nil
2822     (eq article (caar gnus-newsgroup-data))))
2823
2824 (defun gnus-summary-last-article-p (&optional article)
2825   "Return whether ARTICLE is the last article in the buffer."
2826   (if (not (setq article (or article (gnus-summary-article-number))))
2827       ;; All non-existent numbers are the last article.  :-)
2828       t
2829     (not (cdr (gnus-data-find-list article)))))
2830
2831 (defun gnus-make-thread-indent-array ()
2832   (let ((n 200))
2833     (unless (and gnus-thread-indent-array
2834                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2835       (setq gnus-thread-indent-array (make-vector 201 "")
2836             gnus-thread-indent-array-level gnus-thread-indent-level)
2837       (while (>= n 0)
2838         (aset gnus-thread-indent-array n
2839               (make-string (* n gnus-thread-indent-level) ? ))
2840         (setq n (1- n))))))
2841
2842 (defun gnus-update-summary-mark-positions ()
2843   "Compute where the summary marks are to go."
2844   (save-excursion
2845     (when (gnus-buffer-exists-p gnus-summary-buffer)
2846       (set-buffer gnus-summary-buffer))
2847     (let ((gnus-replied-mark 129)
2848           (gnus-score-below-mark 130)
2849           (gnus-score-over-mark 130)
2850           (gnus-download-mark 131)
2851           (spec gnus-summary-line-format-spec)
2852           gnus-visual pos)
2853       (save-excursion
2854         (gnus-set-work-buffer)
2855         (let ((gnus-summary-line-format-spec spec)
2856               (gnus-newsgroup-downloadable '((0 . t))))
2857           (gnus-summary-insert-line
2858            (make-full-mail-header 0 "" "nobody"
2859                                   "05 Apr 2001 23:33:09 +0400"
2860                                   "" "" 0 0 "" nil)
2861            0 nil 128 t nil "" nil 1)
2862           (goto-char (point-min))
2863           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2864                                              (- (point) 2)))))
2865           (goto-char (point-min))
2866           (push (cons 'replied (and (search-forward "\201" nil t)
2867                                     (- (point) 2)))
2868                 pos)
2869           (goto-char (point-min))
2870           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2871                 pos)
2872           (goto-char (point-min))
2873           (push (cons 'download
2874                       (and (search-forward "\203" nil t) (- (point) 2)))
2875                 pos)))
2876       (setq gnus-summary-mark-positions pos))))
2877
2878 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2879   "Insert a dummy root in the summary buffer."
2880   (beginning-of-line)
2881   (gnus-add-text-properties
2882    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2883    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2884
2885 (defun gnus-summary-from-or-to-or-newsgroups (header)
2886   (let ((to (cdr (assq 'To (mail-header-extra header))))
2887         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2888         (default-mime-charset (with-current-buffer gnus-summary-buffer
2889                                 default-mime-charset)))
2890     (cond
2891      ((and to
2892            gnus-ignored-from-addresses
2893            (string-match gnus-ignored-from-addresses
2894                          (mail-header-from header)))
2895       (concat "-> "
2896               (or (car (funcall gnus-extract-address-components
2897                                 (funcall
2898                                  gnus-decode-encoded-word-function to)))
2899                   (funcall gnus-decode-encoded-word-function to))))
2900      ((and newsgroups
2901            gnus-ignored-from-addresses
2902            (string-match gnus-ignored-from-addresses
2903                          (mail-header-from header)))
2904       (concat "=> " newsgroups))
2905      (t
2906       (or (car (funcall gnus-extract-address-components
2907                         (mail-header-from header)))
2908           (mail-header-from header))))))
2909
2910 (defun gnus-summary-insert-line (gnus-tmp-header
2911                                  gnus-tmp-level gnus-tmp-current
2912                                  gnus-tmp-unread gnus-tmp-replied
2913                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2914                                  &optional gnus-tmp-dummy gnus-tmp-score
2915                                  gnus-tmp-process)
2916   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2917          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2918          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2919          (gnus-tmp-score-char
2920           (if (or (null gnus-summary-default-score)
2921                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2922                       gnus-summary-zcore-fuzz))
2923               ?  ;Whitespace
2924             (if (< gnus-tmp-score gnus-summary-default-score)
2925                 gnus-score-below-mark gnus-score-over-mark)))
2926          (gnus-tmp-replied
2927           (cond (gnus-tmp-process gnus-process-mark)
2928                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2929                  gnus-cached-mark)
2930                 (gnus-tmp-replied gnus-replied-mark)
2931                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2932                  gnus-saved-mark)
2933                 (t gnus-no-mark)))
2934          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2935          (gnus-tmp-name
2936           (cond
2937            ((string-match "<[^>]+> *$" gnus-tmp-from)
2938             (let ((beg (match-beginning 0)))
2939               (or (and (string-match "^\".+\"" gnus-tmp-from)
2940                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2941                   (substring gnus-tmp-from 0 beg))))
2942            ((string-match "(.+)" gnus-tmp-from)
2943             (substring gnus-tmp-from
2944                        (1+ (match-beginning 0)) (1- (match-end 0))))
2945            (t gnus-tmp-from)))
2946          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2947          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2948          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2949          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2950          (buffer-read-only nil))
2951     (when (string= gnus-tmp-name "")
2952       (setq gnus-tmp-name gnus-tmp-from))
2953     (unless (numberp gnus-tmp-lines)
2954       (setq gnus-tmp-lines -1))
2955     (when (= gnus-tmp-lines -1)
2956       (setq gnus-tmp-lines "?"))
2957     (gnus-put-text-property-excluding-characters-with-faces
2958      (point)
2959      (progn (eval gnus-summary-line-format-spec) (point))
2960      'gnus-number gnus-tmp-number)
2961     (when (gnus-visual-p 'summary-highlight 'highlight)
2962       (forward-line -1)
2963       (gnus-run-hooks 'gnus-summary-update-hook)
2964       (forward-line 1))))
2965
2966 (defun gnus-summary-update-line (&optional dont-update)
2967   "Update summary line after change."
2968   (when (and gnus-summary-default-score
2969              (not gnus-summary-inhibit-highlight))
2970     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2971            (article (gnus-summary-article-number))
2972            (score (gnus-summary-article-score article)))
2973       (unless dont-update
2974         (if (and gnus-summary-mark-below
2975                  (< (gnus-summary-article-score)
2976                     gnus-summary-mark-below))
2977             ;; This article has a low score, so we mark it as read.
2978             (when (memq article gnus-newsgroup-unreads)
2979               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2980           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2981             ;; This article was previously marked as read on account
2982             ;; of a low score, but now it has risen, so we mark it as
2983             ;; unread.
2984             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2985         (gnus-summary-update-mark
2986          (if (or (null gnus-summary-default-score)
2987                  (<= (abs (- score gnus-summary-default-score))
2988                      gnus-summary-zcore-fuzz))
2989              ?  ;Whitespace
2990            (if (< score gnus-summary-default-score)
2991                gnus-score-below-mark gnus-score-over-mark))
2992          'score))
2993       ;; Do visual highlighting.
2994       (when (gnus-visual-p 'summary-highlight 'highlight)
2995         (gnus-run-hooks 'gnus-summary-update-hook)))))
2996
2997 (defvar gnus-tmp-new-adopts nil)
2998
2999 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3000   "Return the number of articles in THREAD.
3001 This may be 0 in some cases -- if none of the articles in
3002 the thread are to be displayed."
3003   (let* ((number
3004           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3005           (cond
3006            ((not (listp thread))
3007             1)
3008            ((and (consp thread) (cdr thread))
3009             (apply
3010              '+ 1 (mapcar
3011                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3012            ((null thread)
3013             1)
3014            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3015             1)
3016            (t 0))))
3017     (when (and level (zerop level) gnus-tmp-new-adopts)
3018       (incf number
3019             (apply '+ (mapcar
3020                        'gnus-summary-number-of-articles-in-thread
3021                        gnus-tmp-new-adopts))))
3022     (if char
3023         (if (> number 1) gnus-not-empty-thread-mark
3024           gnus-empty-thread-mark)
3025       number)))
3026
3027 (defun gnus-summary-set-local-parameters (group)
3028   "Go through the local params of GROUP and set all variable specs in that list."
3029   (let ((params (gnus-group-find-parameter group))
3030         (vars '(quit-config)) ; Ignore quit-config.
3031         elem)
3032     (while params
3033       (setq elem (car params)
3034             params (cdr params))
3035       (and (consp elem)                 ; Has to be a cons.
3036            (consp (cdr elem))           ; The cdr has to be a list.
3037            (symbolp (car elem))         ; Has to be a symbol in there.
3038            (not (memq (car elem) vars))
3039            (ignore-errors               ; So we set it.
3040              (push (car elem) vars)
3041              (make-local-variable (car elem))
3042              (set (car elem) (eval (nth 1 elem))))))))
3043
3044 (defun gnus-summary-read-group (group &optional show-all no-article
3045                                       kill-buffer no-display backward
3046                                       select-articles)
3047   "Start reading news in newsgroup GROUP.
3048 If SHOW-ALL is non-nil, already read articles are also listed.
3049 If NO-ARTICLE is non-nil, no article is selected initially.
3050 If NO-DISPLAY, don't generate a summary buffer."
3051   (let (result)
3052     (while (and group
3053                 (null (setq result
3054                             (let ((gnus-auto-select-next nil))
3055                               (or (gnus-summary-read-group-1
3056                                    group show-all no-article
3057                                    kill-buffer no-display
3058                                    select-articles)
3059                                   (setq show-all nil
3060                                         select-articles nil)))))
3061                 (eq gnus-auto-select-next 'quietly))
3062       (set-buffer gnus-group-buffer)
3063       ;; The entry function called above goes to the next
3064       ;; group automatically, so we go two groups back
3065       ;; if we are searching for the previous group.
3066       (when backward
3067         (gnus-group-prev-unread-group 2))
3068       (if (not (equal group (gnus-group-group-name)))
3069           (setq group (gnus-group-group-name))
3070         (setq group nil)))
3071     result))
3072
3073 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3074   "Directly jump to the other GROUP from summary buffer.
3075 If SHOW-ALL is non-nil, already read articles are also listed."
3076   (interactive
3077    (if (eq gnus-summary-buffer (current-buffer))
3078        (list (completing-read
3079               "Group: " gnus-active-hashtb nil t
3080               (when (and gnus-newsgroup-name
3081                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3082                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3083               'gnus-group-history)
3084              current-prefix-arg)
3085      (error "%s must be invoked from a gnus summary buffer." this-command)))
3086   (unless (or (zerop (length group))
3087               (and gnus-newsgroup-name
3088                    (string-equal gnus-newsgroup-name group)))
3089     (gnus-summary-exit)
3090     (gnus-summary-read-group group show-all
3091                              gnus-dont-select-after-jump-to-other-group)))
3092
3093 (defun gnus-summary-read-group-1 (group show-all no-article
3094                                         kill-buffer no-display
3095                                         &optional select-articles)
3096   ;; Killed foreign groups can't be entered.
3097   ;;  (when (and (not (gnus-group-native-p group))
3098   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3099   ;;    (error "Dead non-native groups can't be entered"))
3100   (gnus-message 5 "Retrieving newsgroup: %s..."
3101                 (gnus-group-decoded-name group))
3102   (let* ((new-group (gnus-summary-setup-buffer group))
3103          (quit-config (gnus-group-quit-config group))
3104          (did-select (and new-group (gnus-select-newsgroup
3105                                      group show-all select-articles))))
3106     (cond
3107      ;; This summary buffer exists already, so we just select it.
3108      ((not new-group)
3109       (gnus-set-global-variables)
3110       (when kill-buffer
3111         (gnus-kill-or-deaden-summary kill-buffer))
3112       (gnus-configure-windows 'summary 'force)
3113       (gnus-set-mode-line 'summary)
3114       (gnus-summary-position-point)
3115       (message "")
3116       t)
3117      ;; We couldn't select this group.
3118      ((null did-select)
3119       (when (and (eq major-mode 'gnus-summary-mode)
3120                  (not (equal (current-buffer) kill-buffer)))
3121         (kill-buffer (current-buffer))
3122         (if (not quit-config)
3123             (progn
3124               ;; Update the info -- marks might need to be removed,
3125               ;; for instance.
3126               (gnus-summary-update-info)
3127               (set-buffer gnus-group-buffer)
3128               (gnus-group-jump-to-group group)
3129               (gnus-group-next-unread-group 1))
3130           (gnus-handle-ephemeral-exit quit-config)))
3131       (let ((grpinfo (gnus-get-info group)))
3132         (if (null (gnus-info-read grpinfo))
3133             (gnus-message 3 "Group %s contains no messages"
3134                           (gnus-group-decoded-name group))
3135           (gnus-message 3 "Can't select group")))
3136       nil)
3137      ;; The user did a `C-g' while prompting for number of articles,
3138      ;; so we exit this group.
3139      ((eq did-select 'quit)
3140       (and (eq major-mode 'gnus-summary-mode)
3141            (not (equal (current-buffer) kill-buffer))
3142            (kill-buffer (current-buffer)))
3143       (when kill-buffer
3144         (gnus-kill-or-deaden-summary kill-buffer))
3145       (if (not quit-config)
3146           (progn
3147             (set-buffer gnus-group-buffer)
3148             (gnus-group-jump-to-group group)
3149             (gnus-group-next-unread-group 1)
3150             (gnus-configure-windows 'group 'force))
3151         (gnus-handle-ephemeral-exit quit-config))
3152       ;; Finally signal the quit.
3153       (signal 'quit nil))
3154      ;; The group was successfully selected.
3155      (t
3156       (gnus-set-global-variables)
3157       ;; Save the active value in effect when the group was entered.
3158       (setq gnus-newsgroup-active
3159             (gnus-copy-sequence
3160              (gnus-active gnus-newsgroup-name)))
3161       ;; You can change the summary buffer in some way with this hook.
3162       (gnus-run-hooks 'gnus-select-group-hook)
3163       (gnus-update-format-specifications
3164        nil 'summary 'summary-mode 'summary-dummy)
3165       (gnus-update-summary-mark-positions)
3166       ;; Do score processing.
3167       (when gnus-use-scoring
3168         (gnus-possibly-score-headers))
3169       ;; Check whether to fill in the gaps in the threads.
3170       (when gnus-build-sparse-threads
3171         (gnus-build-sparse-threads))
3172       ;; Find the initial limit.
3173       (if gnus-show-threads
3174           (if show-all
3175               (let ((gnus-newsgroup-dormant nil))
3176                 (gnus-summary-initial-limit show-all))
3177             (gnus-summary-initial-limit show-all))
3178         ;; When untreaded, all articles are always shown.
3179         (setq gnus-newsgroup-limit
3180               (mapcar
3181                (lambda (header) (mail-header-number header))
3182                gnus-newsgroup-headers)))
3183       ;; Generate the summary buffer.
3184       (unless no-display
3185         (gnus-summary-prepare))
3186       (when gnus-use-trees
3187         (gnus-tree-open group)
3188         (setq gnus-summary-highlight-line-function
3189               'gnus-tree-highlight-article))
3190       ;; If the summary buffer is empty, but there are some low-scored
3191       ;; articles or some excluded dormants, we include these in the
3192       ;; buffer.
3193       (when (and (zerop (buffer-size))
3194                  (not no-display))
3195         (cond (gnus-newsgroup-dormant
3196                (gnus-summary-limit-include-dormant))
3197               ((and gnus-newsgroup-scored show-all)
3198                (gnus-summary-limit-include-expunged t))))
3199       ;; Function `gnus-apply-kill-file' must be called in this hook.
3200       (gnus-run-hooks 'gnus-apply-kill-hook)
3201       (if (and (zerop (buffer-size))
3202                (not no-display))
3203           (progn
3204             ;; This newsgroup is empty.
3205             (gnus-summary-catchup-and-exit nil t)
3206             (gnus-message 6 "No unread news")
3207             (when kill-buffer
3208               (gnus-kill-or-deaden-summary kill-buffer))
3209             ;; Return nil from this function.
3210             nil)
3211         ;; Hide conversation thread subtrees.  We cannot do this in
3212         ;; gnus-summary-prepare-hook since kill processing may not
3213         ;; work with hidden articles.
3214         (and gnus-show-threads
3215              gnus-thread-hide-subtree
3216              (gnus-summary-hide-all-threads))
3217         (when kill-buffer
3218           (gnus-kill-or-deaden-summary kill-buffer))
3219         ;; Show first unread article if requested.
3220         (if (and (not no-article)
3221                  (not no-display)
3222                  gnus-newsgroup-unreads
3223                  gnus-auto-select-first)
3224             (progn
3225               (gnus-configure-windows 'summary)
3226               (cond
3227                ((eq gnus-auto-select-first 'best)
3228                 (gnus-summary-best-unread-article))
3229                ((eq gnus-auto-select-first t)
3230                 (gnus-summary-first-unread-article))
3231                ((gnus-functionp gnus-auto-select-first)
3232                 (funcall gnus-auto-select-first))))
3233           ;; Don't select any articles, just move point to the first
3234           ;; article in the group.
3235           (goto-char (point-min))
3236           (gnus-summary-position-point)
3237           (gnus-configure-windows 'summary 'force)
3238           (gnus-set-mode-line 'summary))
3239         (when (get-buffer-window gnus-group-buffer t)
3240           ;; Gotta use windows, because recenter does weird stuff if
3241           ;; the current buffer ain't the displayed window.
3242           (let ((owin (selected-window)))
3243             (select-window (get-buffer-window gnus-group-buffer t))
3244             (when (gnus-group-goto-group group)
3245               (recenter))
3246             (select-window owin)))
3247         ;; Mark this buffer as "prepared".
3248         (setq gnus-newsgroup-prepared t)
3249         (gnus-run-hooks 'gnus-summary-prepared-hook)
3250         t)))))
3251
3252 (defun gnus-summary-prepare ()
3253   "Generate the summary buffer."
3254   (interactive)
3255   (let ((buffer-read-only nil))
3256     (erase-buffer)
3257     (setq gnus-newsgroup-data nil
3258           gnus-newsgroup-data-reverse nil)
3259     (gnus-run-hooks 'gnus-summary-generate-hook)
3260     ;; Generate the buffer, either with threads or without.
3261     (when gnus-newsgroup-headers
3262       (gnus-summary-prepare-threads
3263        (if gnus-show-threads
3264            (gnus-sort-gathered-threads
3265             (funcall gnus-summary-thread-gathering-function
3266                      (gnus-sort-threads
3267                       (gnus-cut-threads (gnus-make-threads)))))
3268          ;; Unthreaded display.
3269          (gnus-sort-articles gnus-newsgroup-headers))))
3270     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3271     ;; Call hooks for modifying summary buffer.
3272     (goto-char (point-min))
3273     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3274
3275 (defsubst gnus-general-simplify-subject (subject)
3276   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3277   (setq subject
3278         (cond
3279          ;; Truncate the subject.
3280          (gnus-simplify-subject-functions
3281           (gnus-map-function gnus-simplify-subject-functions subject))
3282          ((numberp gnus-summary-gather-subject-limit)
3283           (setq subject (gnus-simplify-subject-re subject))
3284           (if (> (length subject) gnus-summary-gather-subject-limit)
3285               (substring subject 0 gnus-summary-gather-subject-limit)
3286             subject))
3287          ;; Fuzzily simplify it.
3288          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3289           (gnus-simplify-subject-fuzzy subject))
3290          ;; Just remove the leading "Re:".
3291          (t
3292           (gnus-simplify-subject-re subject))))
3293
3294   (if (and gnus-summary-gather-exclude-subject
3295            (string-match gnus-summary-gather-exclude-subject subject))
3296       nil                               ; This article shouldn't be gathered
3297     subject))
3298
3299 (defun gnus-summary-simplify-subject-query ()
3300   "Query where the respool algorithm would put this article."
3301   (interactive)
3302   (gnus-summary-select-article)
3303   (message "%s"
3304            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3305
3306 (defun gnus-gather-threads-by-subject (threads)
3307   "Gather threads by looking at Subject headers."
3308   (if (not gnus-summary-make-false-root)
3309       threads
3310     (let ((hashtb (gnus-make-hashtable 1024))
3311           (prev threads)
3312           (result threads)
3313           subject hthread whole-subject)
3314       (while threads
3315         (setq subject (gnus-general-simplify-subject
3316                        (setq whole-subject (mail-header-subject
3317                                             (caar threads)))))
3318         (when subject
3319           (if (setq hthread (gnus-gethash subject hashtb))
3320               (progn
3321                 ;; We enter a dummy root into the thread, if we
3322                 ;; haven't done that already.
3323                 (unless (stringp (caar hthread))
3324                   (setcar hthread (list whole-subject (car hthread))))
3325                 ;; We add this new gathered thread to this gathered
3326                 ;; thread.
3327                 (setcdr (car hthread)
3328                         (nconc (cdar hthread) (list (car threads))))
3329                 ;; Remove it from the list of threads.
3330                 (setcdr prev (cdr threads))
3331                 (setq threads prev))
3332             ;; Enter this thread into the hash table.
3333             (gnus-sethash subject threads hashtb)))
3334         (setq prev threads)
3335         (setq threads (cdr threads)))
3336       result)))
3337
3338 (defun gnus-gather-threads-by-references (threads)
3339   "Gather threads by looking at References headers."
3340   (let ((idhashtb (gnus-make-hashtable 1024))
3341         (thhashtb (gnus-make-hashtable 1024))
3342         (prev threads)
3343         (result threads)
3344         ids references id gthread gid entered ref)
3345     (while threads
3346       (when (setq references (mail-header-references (caar threads)))
3347         (setq id (mail-header-id (caar threads))
3348               ids (gnus-split-references references)
3349               entered nil)
3350         (while (setq ref (pop ids))
3351           (setq ids (delete ref ids))
3352           (if (not (setq gid (gnus-gethash ref idhashtb)))
3353               (progn
3354                 (gnus-sethash ref id idhashtb)
3355                 (gnus-sethash id threads thhashtb))
3356             (setq gthread (gnus-gethash gid thhashtb))
3357             (unless entered
3358               ;; We enter a dummy root into the thread, if we
3359               ;; haven't done that already.
3360               (unless (stringp (caar gthread))
3361                 (setcar gthread (list (mail-header-subject (caar gthread))
3362                                       (car gthread))))
3363               ;; We add this new gathered thread to this gathered
3364               ;; thread.
3365               (setcdr (car gthread)
3366                       (nconc (cdar gthread) (list (car threads)))))
3367             ;; Add it into the thread hash table.
3368             (gnus-sethash id gthread thhashtb)
3369             (setq entered t)
3370             ;; Remove it from the list of threads.
3371             (setcdr prev (cdr threads))
3372             (setq threads prev))))
3373       (setq prev threads)
3374       (setq threads (cdr threads)))
3375     result))
3376
3377 (defun gnus-sort-gathered-threads (threads)
3378   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3379   (let ((result threads))
3380     (while threads
3381       (when (stringp (caar threads))
3382         (setcdr (car threads)
3383                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3384       (setq threads (cdr threads)))
3385     result))
3386
3387 (defun gnus-thread-loop-p (root thread)
3388   "Say whether ROOT is in THREAD."
3389   (let ((stack (list thread))
3390         (infloop 0)
3391         th)
3392     (while (setq thread (pop stack))
3393       (setq th (cdr thread))
3394       (while (and th
3395                   (not (eq (caar th) root)))
3396         (pop th))
3397       (if th
3398           ;; We have found a loop.
3399           (let (ref-dep)
3400             (setcdr thread (delq (car th) (cdr thread)))
3401             (if (boundp (setq ref-dep (intern "none"
3402                                               gnus-newsgroup-dependencies)))
3403                 (setcdr (symbol-value ref-dep)
3404                         (nconc (cdr (symbol-value ref-dep))
3405                                (list (car th))))
3406               (set ref-dep (list nil (car th))))
3407             (setq infloop 1
3408                   stack nil))
3409         ;; Push all the subthreads onto the stack.
3410         (push (cdr thread) stack)))
3411     infloop))
3412
3413 (defun gnus-make-threads ()
3414   "Go through the dependency hashtb and find the roots.  Return all threads."
3415   (let (threads)
3416     (while (catch 'infloop
3417              (mapatoms
3418               (lambda (refs)
3419                 ;; Deal with self-referencing References loops.
3420                 (when (and (car (symbol-value refs))
3421                            (not (zerop
3422                                  (apply
3423                                   '+
3424                                   (mapcar
3425                                    (lambda (thread)
3426                                      (gnus-thread-loop-p
3427                                       (car (symbol-value refs)) thread))
3428                                    (cdr (symbol-value refs)))))))
3429                   (setq threads nil)
3430                   (throw 'infloop t))
3431                 (unless (car (symbol-value refs))
3432                   ;; These threads do not refer back to any other articles,
3433                   ;; so they're roots.
3434                   (setq threads (append (cdr (symbol-value refs)) threads))))
3435               gnus-newsgroup-dependencies)))
3436     threads))
3437
3438 ;; Build the thread tree.
3439 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3440   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3441
3442 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3443 if it was already present.
3444
3445 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3446 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3447 Message-IDs will be renamed be renamed to a unique Message-ID before
3448 being entered.
3449
3450 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3451   (let* ((id (mail-header-id header))
3452          (id-dep (and id (intern id dependencies)))
3453          ref ref-dep ref-header)
3454     ;; Enter this `header' in the `dependencies' table.
3455     (cond
3456      ((not id-dep)
3457       (setq header nil))
3458      ;; The first two cases do the normal part: enter a new `header'
3459      ;; in the `dependencies' table.
3460      ((not (boundp id-dep))
3461       (set id-dep (list header)))
3462      ((null (car (symbol-value id-dep)))
3463       (setcar (symbol-value id-dep) header))
3464
3465      ;; From here the `header' was already present in the
3466      ;; `dependencies' table.
3467      (force-new
3468       ;; Overrides an existing entry;
3469       ;; just set the header part of the entry.
3470       (setcar (symbol-value id-dep) header))
3471
3472      ;; Renames the existing `header' to a unique Message-ID.
3473      ((not gnus-summary-ignore-duplicates)
3474       ;; An article with this Message-ID has already been seen.
3475       ;; We rename the Message-ID.
3476       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3477            (list header))
3478       (mail-header-set-id header id))
3479
3480      ;; The last case ignores an existing entry, except it adds any
3481      ;; additional Xrefs (in case the two articles came from different
3482      ;; servers.
3483      ;; Also sets `header' to `nil' meaning that the `dependencies'
3484      ;; table was *not* modified.
3485      (t
3486       (mail-header-set-xref
3487        (car (symbol-value id-dep))
3488        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3489                    "")
3490                (or (mail-header-xref header) "")))
3491       (setq header nil)))
3492
3493     (when header
3494       ;; First check if that we are not creating a References loop.
3495       (setq ref (gnus-parent-id (mail-header-references header)))
3496       (while (and ref
3497                   (setq ref-dep (intern-soft ref dependencies))
3498                   (boundp ref-dep)
3499                   (setq ref-header (car (symbol-value ref-dep))))
3500         (if (string= id ref)
3501             ;; Yuk!  This is a reference loop.  Make the article be a
3502             ;; root article.
3503             (progn
3504               (mail-header-set-references (car (symbol-value id-dep)) "none")
3505               (setq ref nil))
3506           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3507       (setq ref (gnus-parent-id (mail-header-references header)))
3508       (setq ref-dep (intern (or ref "none") dependencies))
3509       (if (boundp ref-dep)
3510           (setcdr (symbol-value ref-dep)
3511                   (nconc (cdr (symbol-value ref-dep))
3512                          (list (symbol-value id-dep))))
3513         (set ref-dep (list nil (symbol-value id-dep)))))
3514     header))
3515
3516 (defun gnus-build-sparse-threads ()
3517   (let ((headers gnus-newsgroup-headers)
3518         (mail-parse-charset gnus-newsgroup-charset)
3519         (gnus-summary-ignore-duplicates t)
3520         header references generation relations
3521         subject child end new-child date)
3522     ;; First we create an alist of generations/relations, where
3523     ;; generations is how much we trust the relation, and the relation
3524     ;; is parent/child.
3525     (gnus-message 7 "Making sparse threads...")
3526     (save-excursion
3527       (nnheader-set-temp-buffer " *gnus sparse threads*")
3528       (while (setq header (pop headers))
3529         (when (and (setq references (mail-header-references header))
3530                    (not (string= references "")))
3531           (insert references)
3532           (setq child (mail-header-id header)
3533                 subject (mail-header-subject header)
3534                 date (mail-header-date header)
3535                 generation 0)
3536           (while (search-backward ">" nil t)
3537             (setq end (1+ (point)))
3538             (when (search-backward "<" nil t)
3539               (setq new-child (buffer-substring (point) end))
3540               (push (list (incf generation)
3541                           child (setq child new-child)
3542                           subject date)
3543                     relations)))
3544           (when child
3545             (push (list (1+ generation) child nil subject) relations))
3546           (erase-buffer)))
3547       (kill-buffer (current-buffer)))
3548     ;; Sort over trustworthiness.
3549     (mapcar
3550      (lambda (relation)
3551        (when (gnus-dependencies-add-header
3552               (make-full-mail-header-from-decoded-header
3553                gnus-reffed-article-number
3554                (nth 3 relation) "" (or (nth 4 relation) "")
3555                (nth 1 relation)
3556                (or (nth 2 relation) "") 0 0 "")
3557               gnus-newsgroup-dependencies nil)
3558          (push gnus-reffed-article-number gnus-newsgroup-limit)
3559          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3560          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3561                gnus-newsgroup-reads)
3562          (decf gnus-reffed-article-number)))
3563      (sort relations 'car-less-than-car))
3564     (gnus-message 7 "Making sparse threads...done")))
3565
3566 (defun gnus-build-old-threads ()
3567   ;; Look at all the articles that refer back to old articles, and
3568   ;; fetch the headers for the articles that aren't there.  This will
3569   ;; build complete threads - if the roots haven't been expired by the
3570   ;; server, that is.
3571   (let ((mail-parse-charset gnus-newsgroup-charset)
3572         id heads)
3573     (mapatoms
3574      (lambda (refs)
3575        (when (not (car (symbol-value refs)))
3576          (setq heads (cdr (symbol-value refs)))
3577          (while heads
3578            (if (memq (mail-header-number (caar heads))
3579                      gnus-newsgroup-dormant)
3580                (setq heads (cdr heads))
3581              (setq id (symbol-name refs))
3582              (while (and (setq id (gnus-build-get-header id))
3583                          (not (car (gnus-id-to-thread id)))))
3584              (setq heads nil)))))
3585      gnus-newsgroup-dependencies)))
3586
3587 ;; This function has to be called with point after the article number
3588 ;; on the beginning of the line.
3589 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3590   (let ((eol (gnus-point-at-eol))
3591         (buffer (current-buffer))
3592         header)
3593
3594     ;; overview: [num subject from date id refs chars lines misc]
3595     (unwind-protect
3596         (progn
3597           (narrow-to-region (point) eol)
3598           (unless (eobp)
3599             (forward-char))
3600
3601           (setq header
3602                 (make-full-mail-header
3603                  number                         ; number
3604                  (nnheader-nov-field)           ; subject
3605                  (nnheader-nov-field)           ; from
3606                  (nnheader-nov-field)           ; date
3607                  (nnheader-nov-read-message-id) ; id
3608                  (nnheader-nov-field)           ; refs
3609                  (nnheader-nov-read-integer)    ; chars
3610                  (nnheader-nov-read-integer)    ; lines
3611                  (unless (eobp)
3612                    (if (looking-at "Xref: ")
3613                        (goto-char (match-end 0)))
3614                    (nnheader-nov-field))        ; Xref
3615                  (nnheader-nov-parse-extra))))  ; extra
3616
3617       (widen))
3618
3619     (when gnus-alter-header-function
3620       (funcall gnus-alter-header-function header))
3621     (gnus-dependencies-add-header header dependencies force-new)))
3622
3623 (defun gnus-build-get-header (id)
3624   "Look through the buffer of NOV lines and find the header to ID.
3625 Enter this line into the dependencies hash table, and return
3626 the id of the parent article (if any)."
3627   (let ((deps gnus-newsgroup-dependencies)
3628         found header)
3629     (prog1
3630         (save-excursion
3631           (set-buffer nntp-server-buffer)
3632           (let ((case-fold-search nil))
3633             (goto-char (point-min))
3634             (while (and (not found)
3635                         (search-forward id nil t))
3636               (beginning-of-line)
3637               (setq found (looking-at
3638                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3639                                    (regexp-quote id))))
3640               (or found (beginning-of-line 2)))
3641             (when found
3642               (beginning-of-line)
3643               (and
3644                (setq header (gnus-nov-parse-line
3645                              (read (current-buffer)) deps))
3646                (gnus-parent-id (mail-header-references header))))))
3647       (when header
3648         (let ((number (mail-header-number header)))
3649           (push number gnus-newsgroup-limit)
3650           (push header gnus-newsgroup-headers)
3651           (if (memq number gnus-newsgroup-unselected)
3652               (progn
3653                 (push number gnus-newsgroup-unreads)
3654                 (setq gnus-newsgroup-unselected
3655                       (delq number gnus-newsgroup-unselected)))
3656             (push number gnus-newsgroup-ancient)))))))
3657
3658 (defun gnus-build-all-threads ()
3659   "Read all the headers."
3660   (let ((gnus-summary-ignore-duplicates t)
3661         (mail-parse-charset gnus-newsgroup-charset)
3662         (dependencies gnus-newsgroup-dependencies)
3663         header article)
3664     (save-excursion
3665       (set-buffer nntp-server-buffer)
3666       (let ((case-fold-search nil))
3667         (goto-char (point-min))
3668         (while (not (eobp))
3669           (ignore-errors
3670             (setq article (read (current-buffer))
3671                   header (gnus-nov-parse-line article dependencies)))
3672           (when header
3673             (save-excursion
3674               (set-buffer gnus-summary-buffer)
3675               (push header gnus-newsgroup-headers)
3676               (if (memq (setq article (mail-header-number header))
3677                         gnus-newsgroup-unselected)
3678                   (progn
3679                     (push article gnus-newsgroup-unreads)
3680                     (setq gnus-newsgroup-unselected
3681                           (delq article gnus-newsgroup-unselected)))
3682                 (push article gnus-newsgroup-ancient)))
3683             (forward-line 1)))))))
3684
3685 (defun gnus-summary-update-article-line (article header)
3686   "Update the line for ARTICLE using HEADERS."
3687   (let* ((id (mail-header-id header))
3688          (thread (gnus-id-to-thread id)))
3689     (unless thread
3690       (error "Article in no thread"))
3691     ;; Update the thread.
3692     (setcar thread header)
3693     (gnus-summary-goto-subject article)
3694     (let* ((datal (gnus-data-find-list article))
3695            (data (car datal))
3696            (length (when (cdr datal)
3697                      (- (gnus-data-pos data)
3698                         (gnus-data-pos (cadr datal)))))
3699            (buffer-read-only nil)
3700            (level (gnus-summary-thread-level)))
3701       (gnus-delete-line)
3702       (gnus-summary-insert-line
3703        header level nil (gnus-article-mark article)
3704        (memq article gnus-newsgroup-replied)
3705        (memq article gnus-newsgroup-expirable)
3706        ;; Only insert the Subject string when it's different
3707        ;; from the previous Subject string.
3708        (if (and
3709             gnus-show-threads
3710             (gnus-subject-equal
3711              (condition-case ()
3712                  (mail-header-subject
3713                   (gnus-data-header
3714                    (cadr
3715                     (gnus-data-find-list
3716                      article
3717                      (gnus-data-list t)))))
3718                ;; Error on the side of excessive subjects.
3719                (error ""))
3720              (mail-header-subject header)))
3721            ""
3722          (mail-header-subject header))
3723        nil (cdr (assq article gnus-newsgroup-scored))
3724        (memq article gnus-newsgroup-processable))
3725       (when length
3726         (gnus-data-update-list
3727          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3728
3729 (defun gnus-summary-update-article (article &optional iheader)
3730   "Update ARTICLE in the summary buffer."
3731   (set-buffer gnus-summary-buffer)
3732   (let* ((header (gnus-summary-article-header article))
3733          (id (mail-header-id header))
3734          (data (gnus-data-find article))
3735          (thread (gnus-id-to-thread id))
3736          (references (mail-header-references header))
3737          (parent
3738           (gnus-id-to-thread
3739            (or (gnus-parent-id
3740                 (when (and references
3741                            (not (equal "" references)))
3742                   references))
3743                "none")))
3744          (buffer-read-only nil)
3745          (old (car thread)))
3746     (when thread
3747       (unless iheader
3748         (setcar thread nil)
3749         (when parent
3750           (delq thread parent)))
3751       (if (gnus-summary-insert-subject id header)
3752           ;; Set the (possibly) new article number in the data structure.
3753           (gnus-data-set-number data (gnus-id-to-article id))
3754         (setcar thread old)
3755         nil))))
3756
3757 (defun gnus-rebuild-thread (id &optional line)
3758   "Rebuild the thread containing ID.
3759 If LINE, insert the rebuilt thread starting on line LINE."
3760   (let ((buffer-read-only nil)
3761         old-pos current thread data)
3762     (if (not gnus-show-threads)
3763         (setq thread (list (car (gnus-id-to-thread id))))
3764       ;; Get the thread this article is part of.
3765       (setq thread (gnus-remove-thread id)))
3766     (setq old-pos (gnus-point-at-bol))
3767     (setq current (save-excursion
3768                     (and (re-search-backward "[\r\n]" nil t)
3769                          (gnus-summary-article-number))))
3770     ;; If this is a gathered thread, we have to go some re-gathering.
3771     (when (stringp (car thread))
3772       (let ((subject (car thread))
3773             roots thr)
3774         (setq thread (cdr thread))
3775         (while thread
3776           (unless (memq (setq thr (gnus-id-to-thread
3777                                    (gnus-root-id
3778                                     (mail-header-id (caar thread)))))
3779                         roots)
3780             (push thr roots))
3781           (setq thread (cdr thread)))
3782         ;; We now have all (unique) roots.
3783         (if (= (length roots) 1)
3784             ;; All the loose roots are now one solid root.
3785             (setq thread (car roots))
3786           (setq thread (cons subject (gnus-sort-threads roots))))))
3787     (let (threads)
3788       ;; We then insert this thread into the summary buffer.
3789       (when line
3790         (goto-char (point-min))
3791         (forward-line (1- line)))
3792       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3793         (if gnus-show-threads
3794             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3795           (gnus-summary-prepare-unthreaded thread))
3796         (setq data (nreverse gnus-newsgroup-data))
3797         (setq threads gnus-newsgroup-threads))
3798       ;; We splice the new data into the data structure.
3799       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3800       ;;!!! then we want to insert at the beginning of the buffer.
3801       ;;!!! That happens to be true with Gnus now, but that may
3802       ;;!!! change in the future.  Perhaps.
3803       (gnus-data-enter-list
3804        (if line nil current) data (- (point) old-pos))
3805       (setq gnus-newsgroup-threads
3806             (nconc threads gnus-newsgroup-threads))
3807       (gnus-data-compute-positions))))
3808
3809 (defun gnus-number-to-header (number)
3810   "Return the header for article NUMBER."
3811   (let ((headers gnus-newsgroup-headers))
3812     (while (and headers
3813                 (not (= number (mail-header-number (car headers)))))
3814       (pop headers))
3815     (when headers
3816       (car headers))))
3817
3818 (defun gnus-parent-headers (in-headers &optional generation)
3819   "Return the headers of the GENERATIONeth parent of HEADERS."
3820   (unless generation
3821     (setq generation 1))
3822   (let ((parent t)
3823         (headers in-headers)
3824         references)
3825     (while (and parent
3826                 (not (zerop generation))
3827                 (setq references (mail-header-references headers)))
3828       (setq headers (if (and references
3829                              (setq parent (gnus-parent-id references)))
3830                         (car (gnus-id-to-thread parent))
3831                       nil))
3832       (decf generation))
3833     (and (not (eq headers in-headers))
3834          headers)))
3835
3836 (defun gnus-id-to-thread (id)
3837   "Return the (sub-)thread where ID appears."
3838   (gnus-gethash id gnus-newsgroup-dependencies))
3839
3840 (defun gnus-id-to-article (id)
3841   "Return the article number of ID."
3842   (let ((thread (gnus-id-to-thread id)))
3843     (when (and thread
3844                (car thread))
3845       (mail-header-number (car thread)))))
3846
3847 (defun gnus-id-to-header (id)
3848   "Return the article headers of ID."
3849   (car (gnus-id-to-thread id)))
3850
3851 (defun gnus-article-displayed-root-p (article)
3852   "Say whether ARTICLE is a root(ish) article."
3853   (let ((level (gnus-summary-thread-level article))
3854         (refs (mail-header-references  (gnus-summary-article-header article)))
3855         particle)
3856     (cond
3857      ((null level) nil)
3858      ((zerop level) t)
3859      ((null refs) t)
3860      ((null (gnus-parent-id refs)) t)
3861      ((and (= 1 level)
3862            (null (setq particle (gnus-id-to-article
3863                                  (gnus-parent-id refs))))
3864            (null (gnus-summary-thread-level particle)))))))
3865
3866 (defun gnus-root-id (id)
3867   "Return the id of the root of the thread where ID appears."
3868   (let (last-id prev)
3869     (while (and id (setq prev (car (gnus-id-to-thread id))))
3870       (setq last-id id
3871             id (gnus-parent-id (mail-header-references prev))))
3872     last-id))
3873
3874 (defun gnus-articles-in-thread (thread)
3875   "Return the list of articles in THREAD."
3876   (cons (mail-header-number (car thread))
3877         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3878
3879 (defun gnus-remove-thread (id &optional dont-remove)
3880   "Remove the thread that has ID in it."
3881   (let (headers thread last-id)
3882     ;; First go up in this thread until we find the root.
3883     (setq last-id (gnus-root-id id)
3884           headers (message-flatten-list (gnus-id-to-thread last-id)))
3885     ;; We have now found the real root of this thread.  It might have
3886     ;; been gathered into some loose thread, so we have to search
3887     ;; through the threads to find the thread we wanted.
3888     (let ((threads gnus-newsgroup-threads)
3889           sub)
3890       (while threads
3891         (setq sub (car threads))
3892         (if (stringp (car sub))
3893             ;; This is a gathered thread, so we look at the roots
3894             ;; below it to find whether this article is in this
3895             ;; gathered root.
3896             (progn
3897               (setq sub (cdr sub))
3898               (while sub
3899                 (when (member (caar sub) headers)
3900                   (setq thread (car threads)
3901                         threads nil
3902                         sub nil))
3903                 (setq sub (cdr sub))))
3904           ;; It's an ordinary thread, so we check it.
3905           (when (eq (car sub) (car headers))
3906             (setq thread sub
3907                   threads nil)))
3908         (setq threads (cdr threads)))
3909       ;; If this article is in no thread, then it's a root.
3910       (if thread
3911           (unless dont-remove
3912             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3913         (setq thread (gnus-id-to-thread last-id)))
3914       (when thread
3915         (prog1
3916             thread                      ; We return this thread.
3917           (unless dont-remove
3918             (if (stringp (car thread))
3919                 (progn
3920                   ;; If we use dummy roots, then we have to remove the
3921                   ;; dummy root as well.
3922                   (when (eq gnus-summary-make-false-root 'dummy)
3923                     ;; We go to the dummy root by going to
3924                     ;; the first sub-"thread", and then one line up.
3925                     (gnus-summary-goto-article
3926                      (mail-header-number (caadr thread)))
3927                     (forward-line -1)
3928                     (gnus-delete-line)
3929                     (gnus-data-compute-positions))
3930                   (setq thread (cdr thread))
3931                   (while thread
3932                     (gnus-remove-thread-1 (car thread))
3933                     (setq thread (cdr thread))))
3934               (gnus-remove-thread-1 thread))))))))
3935
3936 (defun gnus-remove-thread-1 (thread)
3937   "Remove the thread THREAD recursively."
3938   (let ((number (mail-header-number (pop thread)))
3939         d)
3940     (setq thread (reverse thread))
3941     (while thread
3942       (gnus-remove-thread-1 (pop thread)))
3943     (when (setq d (gnus-data-find number))
3944       (goto-char (gnus-data-pos d))
3945       (gnus-summary-show-thread)
3946       (gnus-data-remove
3947        number
3948        (- (gnus-point-at-bol)
3949           (prog1
3950               (1+ (gnus-point-at-eol))
3951             (gnus-delete-line)))))))
3952
3953 (defun gnus-sort-threads-1 (threads func)
3954   (sort (mapcar (lambda (thread)
3955                   (cons (car thread)
3956                         (and (cdr thread)
3957                              (gnus-sort-threads-1 (cdr thread) func))))
3958                 threads) func))
3959
3960 (defun gnus-sort-threads (threads)
3961   "Sort THREADS."
3962   (if (not gnus-thread-sort-functions)
3963       threads
3964     (gnus-message 8 "Sorting threads...")
3965     (prog1
3966         (gnus-sort-threads-1
3967          threads
3968          (gnus-make-sort-function gnus-thread-sort-functions))
3969       (gnus-message 8 "Sorting threads...done"))))
3970
3971 (defun gnus-sort-articles (articles)
3972   "Sort ARTICLES."
3973   (when gnus-article-sort-functions
3974     (gnus-message 7 "Sorting articles...")
3975     (prog1
3976         (setq gnus-newsgroup-headers
3977               (sort articles (gnus-make-sort-function
3978                               gnus-article-sort-functions)))
3979       (gnus-message 7 "Sorting articles...done"))))
3980
3981 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3982 (defmacro gnus-thread-header (thread)
3983   "Return header of first article in THREAD.
3984 Note that THREAD must never, ever be anything else than a variable -
3985 using some other form will lead to serious barfage."
3986   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3987   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3988   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3989         (vector thread) 2))
3990
3991 (defsubst gnus-article-sort-by-number (h1 h2)
3992   "Sort articles by article number."
3993   (< (mail-header-number h1)
3994      (mail-header-number h2)))
3995
3996 (defun gnus-thread-sort-by-number (h1 h2)
3997   "Sort threads by root article number."
3998   (gnus-article-sort-by-number
3999    (gnus-thread-header h1) (gnus-thread-header h2)))
4000
4001 (defsubst gnus-article-sort-by-lines (h1 h2)
4002   "Sort articles by article Lines header."
4003   (< (mail-header-lines h1)
4004      (mail-header-lines h2)))
4005
4006 (defun gnus-thread-sort-by-lines (h1 h2)
4007   "Sort threads by root article Lines header."
4008   (gnus-article-sort-by-lines
4009    (gnus-thread-header h1) (gnus-thread-header h2)))
4010
4011 (defsubst gnus-article-sort-by-chars (h1 h2)
4012   "Sort articles by octet length."
4013   (< (mail-header-chars h1)
4014      (mail-header-chars h2)))
4015
4016 (defun gnus-thread-sort-by-chars (h1 h2)
4017   "Sort threads by root article octet length."
4018   (gnus-article-sort-by-chars
4019    (gnus-thread-header h1) (gnus-thread-header h2)))
4020
4021 (defsubst gnus-article-sort-by-author (h1 h2)
4022   "Sort articles by root author."
4023   (string-lessp
4024    (let ((addr (car (mime-entity-read-field h1 'From))))
4025      (or (std11-full-name-string addr)
4026          (std11-address-string addr)
4027          ""))
4028    (let ((addr (car (mime-entity-read-field h2 'From))))
4029      (or (std11-full-name-string addr)
4030          (std11-address-string addr)
4031          ""))
4032    ))
4033
4034 (defun gnus-thread-sort-by-author (h1 h2)
4035   "Sort threads by root author."
4036   (gnus-article-sort-by-author
4037    (gnus-thread-header h1)  (gnus-thread-header h2)))
4038
4039 (defsubst gnus-article-sort-by-subject (h1 h2)
4040   "Sort articles by root subject."
4041   (string-lessp
4042    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4043    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4044
4045 (defun gnus-thread-sort-by-subject (h1 h2)
4046   "Sort threads by root subject."
4047   (gnus-article-sort-by-subject
4048    (gnus-thread-header h1) (gnus-thread-header h2)))
4049
4050 (defsubst gnus-article-sort-by-date (h1 h2)
4051   "Sort articles by root article date."
4052   (time-less-p
4053    (gnus-date-get-time (mail-header-date h1))
4054    (gnus-date-get-time (mail-header-date h2))))
4055
4056 (defun gnus-thread-sort-by-date (h1 h2)
4057   "Sort threads by root article date."
4058   (gnus-article-sort-by-date
4059    (gnus-thread-header h1) (gnus-thread-header h2)))
4060
4061 (defsubst gnus-article-sort-by-score (h1 h2)
4062   "Sort articles by root article score.
4063 Unscored articles will be counted as having a score of zero."
4064   (> (or (cdr (assq (mail-header-number h1)
4065                     gnus-newsgroup-scored))
4066          gnus-summary-default-score 0)
4067      (or (cdr (assq (mail-header-number h2)
4068                     gnus-newsgroup-scored))
4069          gnus-summary-default-score 0)))
4070
4071 (defun gnus-thread-sort-by-score (h1 h2)
4072   "Sort threads by root article score."
4073   (gnus-article-sort-by-score
4074    (gnus-thread-header h1) (gnus-thread-header h2)))
4075
4076 (defun gnus-thread-sort-by-total-score (h1 h2)
4077   "Sort threads by the sum of all scores in the thread.
4078 Unscored articles will be counted as having a score of zero."
4079   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4080
4081 (defun gnus-thread-total-score (thread)
4082   ;; This function find the total score of THREAD.
4083   (cond ((null thread)
4084          0)
4085         ((consp thread)
4086          (if (stringp (car thread))
4087              (apply gnus-thread-score-function 0
4088                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4089            (gnus-thread-total-score-1 thread)))
4090         (t
4091          (gnus-thread-total-score-1 (list thread)))))
4092
4093 (defun gnus-thread-total-score-1 (root)
4094   ;; This function find the total score of the thread below ROOT.
4095   (setq root (car root))
4096   (apply gnus-thread-score-function
4097          (or (append
4098               (mapcar 'gnus-thread-total-score
4099                       (cdr (gnus-id-to-thread (mail-header-id root))))
4100               (when (> (mail-header-number root) 0)
4101                 (list (or (cdr (assq (mail-header-number root)
4102                                      gnus-newsgroup-scored))
4103                           gnus-summary-default-score 0))))
4104              (list gnus-summary-default-score)
4105              '(0))))
4106
4107 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4108 (defvar gnus-tmp-prev-subject nil)
4109 (defvar gnus-tmp-false-parent nil)
4110 (defvar gnus-tmp-root-expunged nil)
4111 (defvar gnus-tmp-dummy-line nil)
4112
4113 (eval-when-compile (defvar gnus-tmp-header))
4114 (defun gnus-extra-header (type &optional header)
4115   "Return the extra header of TYPE."
4116   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4117       ""))
4118
4119 (defvar gnus-tmp-thread-tree-header-string "")
4120
4121 (defvar gnus-sum-thread-tree-root "> ")
4122 (defvar gnus-sum-thread-tree-single-indent "")
4123 (defvar gnus-sum-thread-tree-vertical "| ")
4124 (defvar gnus-sum-thread-tree-indent "  ")
4125 (defvar gnus-sum-thread-tree-leaf-with-other "+-> ")
4126 (defvar gnus-sum-thread-tree-single-leaf "\\-> ")
4127
4128 (defun gnus-summary-prepare-threads (threads)
4129   "Prepare summary buffer from THREADS and indentation LEVEL.
4130 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4131 or a straight list of headers."
4132   (gnus-message 7 "Generating summary...")
4133
4134   (setq gnus-newsgroup-threads threads)
4135   (beginning-of-line)
4136
4137   (let ((gnus-tmp-level 0)
4138         (default-score (or gnus-summary-default-score 0))
4139         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4140         thread number subject stack state gnus-tmp-gathered beg-match
4141         new-roots gnus-tmp-new-adopts thread-end
4142         gnus-tmp-header gnus-tmp-unread
4143         gnus-tmp-replied gnus-tmp-subject-or-nil
4144         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4145         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4146         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4147         tree-stack)
4148
4149     (setq gnus-tmp-prev-subject nil)
4150
4151     (if (vectorp (car threads))
4152         ;; If this is a straight (sic) list of headers, then a
4153         ;; threaded summary display isn't required, so we just create
4154         ;; an unthreaded one.
4155         (gnus-summary-prepare-unthreaded threads)
4156
4157       ;; Do the threaded display.
4158
4159       (while (or threads stack gnus-tmp-new-adopts new-roots)
4160
4161         (if (and (= gnus-tmp-level 0)
4162                  (or (not stack)
4163                      (= (caar stack) 0))
4164                  (not gnus-tmp-false-parent)
4165                  (or gnus-tmp-new-adopts new-roots))
4166             (if gnus-tmp-new-adopts
4167                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4168                       thread (list (car gnus-tmp-new-adopts))
4169                       gnus-tmp-header (caar thread)
4170                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4171               (when new-roots
4172                 (setq thread (list (car new-roots))
4173                       gnus-tmp-header (caar thread)
4174                       new-roots (cdr new-roots))))
4175
4176           (if threads
4177               ;; If there are some threads, we do them before the
4178               ;; threads on the stack.
4179               (setq thread threads
4180                     gnus-tmp-header (caar thread))
4181             ;; There were no current threads, so we pop something off
4182             ;; the stack.
4183             (setq state (car stack)
4184                   gnus-tmp-level (car state)
4185                   tree-stack (cadr state)
4186                   thread (caddr state)
4187                   stack (cdr stack)
4188                   gnus-tmp-header (caar thread))))
4189
4190         (setq gnus-tmp-false-parent nil)
4191         (setq gnus-tmp-root-expunged nil)
4192         (setq thread-end nil)
4193
4194         (if (stringp gnus-tmp-header)
4195             ;; The header is a dummy root.
4196             (cond
4197              ((eq gnus-summary-make-false-root 'adopt)
4198               ;; We let the first article adopt the rest.
4199               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4200                                                (cddar thread)))
4201               (setq gnus-tmp-gathered
4202                     (nconc (mapcar
4203                             (lambda (h) (mail-header-number (car h)))
4204                             (cddar thread))
4205                            gnus-tmp-gathered))
4206               (setq thread (cons (list (caar thread)
4207                                        (cadar thread))
4208                                  (cdr thread)))
4209               (setq gnus-tmp-level -1
4210                     gnus-tmp-false-parent t))
4211              ((eq gnus-summary-make-false-root 'empty)
4212               ;; We print adopted articles with empty subject fields.
4213               (setq gnus-tmp-gathered
4214                     (nconc (mapcar
4215                             (lambda (h) (mail-header-number (car h)))
4216                             (cddar thread))
4217                            gnus-tmp-gathered))
4218               (setq gnus-tmp-level -1))
4219              ((eq gnus-summary-make-false-root 'dummy)
4220               ;; We remember that we probably want to output a dummy
4221               ;; root.
4222               (setq gnus-tmp-dummy-line gnus-tmp-header)
4223               (setq gnus-tmp-prev-subject gnus-tmp-header))
4224              (t
4225               ;; We do not make a root for the gathered
4226               ;; sub-threads at all.
4227               (setq gnus-tmp-level -1)))
4228
4229           (setq number (mail-header-number gnus-tmp-header)
4230                 subject (mail-header-subject gnus-tmp-header))
4231
4232           (cond
4233            ;; If the thread has changed subject, we might want to make
4234            ;; this subthread into a root.
4235            ((and (null gnus-thread-ignore-subject)
4236                  (not (zerop gnus-tmp-level))
4237                  gnus-tmp-prev-subject
4238                  (not (inline
4239                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4240             (setq new-roots (nconc new-roots (list (car thread)))
4241                   thread-end t
4242                   gnus-tmp-header nil))
4243            ;; If the article lies outside the current limit,
4244            ;; then we do not display it.
4245            ((not (memq number gnus-newsgroup-limit))
4246             (setq gnus-tmp-gathered
4247                   (nconc (mapcar
4248                           (lambda (h) (mail-header-number (car h)))
4249                           (cdar thread))
4250                          gnus-tmp-gathered))
4251             (setq gnus-tmp-new-adopts (if (cdar thread)
4252                                           (append gnus-tmp-new-adopts
4253                                                   (cdar thread))
4254                                         gnus-tmp-new-adopts)
4255                   thread-end t
4256                   gnus-tmp-header nil)
4257             (when (zerop gnus-tmp-level)
4258               (setq gnus-tmp-root-expunged t)))
4259            ;; Perhaps this article is to be marked as read?
4260            ((and gnus-summary-mark-below
4261                  (< (or (cdr (assq number gnus-newsgroup-scored))
4262                         default-score)
4263                     gnus-summary-mark-below)
4264                  ;; Don't touch sparse articles.
4265                  (not (gnus-summary-article-sparse-p number))
4266                  (not (gnus-summary-article-ancient-p number)))
4267             (setq gnus-newsgroup-unreads
4268                   (delq number gnus-newsgroup-unreads))
4269             (if gnus-newsgroup-auto-expire
4270                 (push number gnus-newsgroup-expirable)
4271               (push (cons number gnus-low-score-mark)
4272                     gnus-newsgroup-reads))))
4273
4274           (when gnus-tmp-header
4275             ;; We may have an old dummy line to output before this
4276             ;; article.
4277             (when (and gnus-tmp-dummy-line
4278                        (gnus-subject-equal
4279                         gnus-tmp-dummy-line
4280                         (mail-header-subject gnus-tmp-header)))
4281               (gnus-summary-insert-dummy-line
4282                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4283               (setq gnus-tmp-dummy-line nil))
4284
4285             ;; Compute the mark.
4286             (setq gnus-tmp-unread (gnus-article-mark number))
4287
4288             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4289                                   gnus-tmp-header gnus-tmp-level)
4290                   gnus-newsgroup-data)
4291
4292             ;; Actually insert the line.
4293             (setq
4294              gnus-tmp-subject-or-nil
4295              (cond
4296               ((and gnus-thread-ignore-subject
4297                     gnus-tmp-prev-subject
4298                     (not (inline (gnus-subject-equal
4299                                   gnus-tmp-prev-subject subject))))
4300                subject)
4301               ((zerop gnus-tmp-level)
4302                (if (and (eq gnus-summary-make-false-root 'empty)
4303                         (memq number gnus-tmp-gathered)
4304                         gnus-tmp-prev-subject
4305                         (inline (gnus-subject-equal
4306                                  gnus-tmp-prev-subject subject)))
4307                    gnus-summary-same-subject
4308                  subject))
4309               (t gnus-summary-same-subject)))
4310             (if (and (eq gnus-summary-make-false-root 'adopt)
4311                      (= gnus-tmp-level 1)
4312                      (memq number gnus-tmp-gathered))
4313                 (setq gnus-tmp-opening-bracket ?\<
4314                       gnus-tmp-closing-bracket ?\>)
4315               (setq gnus-tmp-opening-bracket ?\[
4316                     gnus-tmp-closing-bracket ?\]))
4317             (setq
4318              gnus-tmp-indentation
4319              (aref gnus-thread-indent-array gnus-tmp-level)
4320              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4321              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4322                                 gnus-summary-default-score 0)
4323              gnus-tmp-score-char
4324              (if (or (null gnus-summary-default-score)
4325                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4326                          gnus-summary-zcore-fuzz))
4327                  ?  ;Whitespace
4328                (if (< gnus-tmp-score gnus-summary-default-score)
4329                    gnus-score-below-mark gnus-score-over-mark))
4330              gnus-tmp-replied
4331              (cond ((memq number gnus-newsgroup-processable)
4332                     gnus-process-mark)
4333                    ((memq number gnus-newsgroup-cached)
4334                     gnus-cached-mark)
4335                    ((memq number gnus-newsgroup-replied)
4336                     gnus-replied-mark)
4337                    ((memq number gnus-newsgroup-forwarded)
4338                     gnus-forwarded-mark)
4339                    ((memq number gnus-newsgroup-saved)
4340                     gnus-saved-mark)
4341                    (t gnus-no-mark))
4342              gnus-tmp-from (mail-header-from gnus-tmp-header)
4343              gnus-tmp-name
4344              (cond
4345               ((string-match "<[^>]+> *$" gnus-tmp-from)
4346                (setq beg-match (match-beginning 0))
4347                (or (and (string-match "^\".+\"" gnus-tmp-from)
4348                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4349                    (substring gnus-tmp-from 0 beg-match)))
4350               ((string-match "(.+)" gnus-tmp-from)
4351                (substring gnus-tmp-from
4352                           (1+ (match-beginning 0)) (1- (match-end 0))))
4353               (t gnus-tmp-from))
4354              gnus-tmp-thread-tree-header-string
4355              (cond
4356               ((not gnus-show-threads) "")
4357               ((zerop gnus-tmp-level)
4358                (if (cdar thread)
4359                    gnus-sum-thread-tree-root
4360                  gnus-sum-thread-tree-single-indent))
4361               (t
4362                (concat (apply 'concat
4363                               (mapcar (lambda (item)
4364                                         (if (= item 1)
4365                                             gnus-sum-thread-tree-vertical
4366                                           gnus-sum-thread-tree-indent))
4367                                       (cdr (reverse tree-stack))))
4368                        (if (nth 1 thread)
4369                            gnus-sum-thread-tree-leaf-with-other
4370                          gnus-sum-thread-tree-single-leaf)))))
4371             (when (string= gnus-tmp-name "")
4372               (setq gnus-tmp-name gnus-tmp-from))
4373             (unless (numberp gnus-tmp-lines)
4374               (setq gnus-tmp-lines -1))
4375             (when (= gnus-tmp-lines -1)
4376               (setq gnus-tmp-lines "?"))
4377             (gnus-put-text-property
4378              (point)
4379              (progn (eval gnus-summary-line-format-spec) (point))
4380              'gnus-number number)
4381             (when gnus-visual-p
4382               (forward-line -1)
4383               (gnus-run-hooks 'gnus-summary-update-hook)
4384               (forward-line 1))
4385
4386             (setq gnus-tmp-prev-subject subject)))
4387
4388         (when (nth 1 thread)
4389           (push (list (max 0 gnus-tmp-level)
4390                       (copy-list tree-stack)
4391                       (nthcdr 1 thread))
4392                 stack))
4393         (push (if (nth 1 thread) 1 0) tree-stack)
4394         (incf gnus-tmp-level)
4395         (setq threads (if thread-end nil (cdar thread)))
4396         (unless threads
4397           (setq gnus-tmp-level 0)))))
4398   (gnus-message 7 "Generating summary...done"))
4399
4400 (defun gnus-summary-prepare-unthreaded (headers)
4401   "Generate an unthreaded summary buffer based on HEADERS."
4402   (let (header number mark)
4403
4404     (beginning-of-line)
4405
4406     (while headers
4407       ;; We may have to root out some bad articles...
4408       (when (memq (setq number (mail-header-number
4409                                 (setq header (pop headers))))
4410                   gnus-newsgroup-limit)
4411         ;; Mark article as read when it has a low score.
4412         (when (and gnus-summary-mark-below
4413                    (< (or (cdr (assq number gnus-newsgroup-scored))
4414                           gnus-summary-default-score 0)
4415                       gnus-summary-mark-below)
4416                    (not (gnus-summary-article-ancient-p number)))
4417           (setq gnus-newsgroup-unreads
4418                 (delq number gnus-newsgroup-unreads))
4419           (if gnus-newsgroup-auto-expire
4420               (push number gnus-newsgroup-expirable)
4421             (push (cons number gnus-low-score-mark)
4422                   gnus-newsgroup-reads)))
4423
4424         (setq mark (gnus-article-mark number))
4425         (push (gnus-data-make number mark (1+ (point)) header 0)
4426               gnus-newsgroup-data)
4427         (gnus-summary-insert-line
4428          header 0 number
4429          mark (memq number gnus-newsgroup-replied)
4430          (memq number gnus-newsgroup-expirable)
4431          (mail-header-subject header) nil
4432          (cdr (assq number gnus-newsgroup-scored))
4433          (memq number gnus-newsgroup-processable))))))
4434
4435 (defun gnus-summary-remove-list-identifiers ()
4436   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4437   (let ((regexp (if (consp gnus-list-identifiers)
4438                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4439                   gnus-list-identifiers))
4440         changed subject)
4441     (when regexp
4442       (dolist (header gnus-newsgroup-headers)
4443         (setq subject (mail-header-subject header)
4444               changed nil)
4445         (while (string-match
4446                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4447                 subject)
4448           (setq subject
4449                 (concat (substring subject 0 (match-beginning 2))
4450                         (substring subject (match-end 0)))
4451                 changed t))
4452         (when (and changed
4453                    (string-match
4454                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4455           (setq subject
4456                 (concat (substring subject 0 (match-beginning 1))
4457                         (substring subject (match-end 1)))))
4458         (when changed
4459           (mail-header-set-subject header subject))))))
4460
4461 (defun gnus-fetch-headers (articles)
4462   "Fetch headers of ARTICLES."
4463   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4464     (gnus-message 5 "Fetching headers for %s..." name)
4465     (prog1
4466         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4467         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4468             (if (eq 'nov
4469                     (setq gnus-headers-retrieved-by
4470                           (gnus-retrieve-headers
4471                            articles gnus-newsgroup-name
4472                            ;; We might want to fetch old headers, but
4473                            ;; not if there is only 1 article.
4474                            (and (or (and
4475                                      (not (eq gnus-fetch-old-headers 'some))
4476                                      (not (numberp gnus-fetch-old-headers)))
4477                                     (> (length articles) 1))
4478                                 gnus-fetch-old-headers))))
4479                 (gnus-get-newsgroup-headers-xover
4480                  articles nil nil gnus-newsgroup-name t)
4481               (gnus-get-newsgroup-headers))
4482           (gnus-retrieve-parsed-headers
4483            articles gnus-newsgroup-name
4484            ;; We might want to fetch old headers, but
4485            ;; not if there is only 1 article.
4486            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4487                          (not (numberp gnus-fetch-old-headers)))
4488                     (> (length articles) 1))
4489                 gnus-fetch-old-headers)))
4490       (gnus-message 5 "Fetching headers for %s...done" name))))
4491
4492 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4493   "Select newsgroup GROUP.
4494 If READ-ALL is non-nil, all articles in the group are selected.
4495 If SELECT-ARTICLES, only select those articles from GROUP."
4496   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4497          ;;!!! Dirty hack; should be removed.
4498          (gnus-summary-ignore-duplicates
4499           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4500               t
4501             gnus-summary-ignore-duplicates))
4502          (info (nth 2 entry))
4503          articles fetched-articles cached)
4504
4505     (unless (gnus-check-server
4506              (setq gnus-current-select-method
4507                    (gnus-find-method-for-group group)))
4508       (error "Couldn't open server"))
4509
4510     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4511         (gnus-activate-group group)     ; Or we can activate it...
4512         (progn                          ; Or we bug out.
4513           (when (equal major-mode 'gnus-summary-mode)
4514             (kill-buffer (current-buffer)))
4515           (error "Couldn't activate group %s: %s"
4516                  group (gnus-status-message group))))
4517
4518     (unless (gnus-request-group group t)
4519       (when (equal major-mode 'gnus-summary-mode)
4520         (kill-buffer (current-buffer)))
4521       (error "Couldn't request group %s: %s"
4522              group (gnus-status-message group)))
4523
4524     (setq gnus-newsgroup-name group)
4525     (setq gnus-newsgroup-unselected nil)
4526     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4527     (gnus-summary-setup-default-charset)
4528
4529     ;; Adjust and set lists of article marks.
4530     (when info
4531       (gnus-adjust-marked-articles info))
4532
4533     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4534     (setq cached
4535           (if (gnus-virtual-group-p group)
4536               gnus-newsgroup-cached
4537             (gnus-cache-articles-in-group group)))
4538
4539     (setq gnus-newsgroup-unreads
4540           (gnus-set-difference
4541            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4542            gnus-newsgroup-dormant))
4543
4544     (setq gnus-newsgroup-processable nil)
4545
4546     (gnus-update-read-articles group gnus-newsgroup-unreads)
4547
4548     (if (setq articles select-articles)
4549         (setq gnus-newsgroup-unselected
4550               (gnus-sorted-intersection
4551                gnus-newsgroup-unreads
4552                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4553       (setq articles (gnus-articles-to-read group read-all)))
4554
4555     (cond
4556      ((null articles)
4557       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4558       'quit)
4559      ((eq articles 0) nil)
4560      (t
4561       ;; Init the dependencies hash table.
4562       (setq gnus-newsgroup-dependencies
4563             (gnus-make-hashtable (length articles)))
4564       (gnus-set-global-variables)
4565       ;; Retrieve the headers and read them in.
4566       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4567
4568       ;; Suppress duplicates?
4569       (when gnus-suppress-duplicates
4570         (gnus-dup-suppress-articles))
4571
4572       ;; Set the initial limit.
4573       (setq gnus-newsgroup-limit (copy-sequence articles))
4574       ;; Remove canceled articles from the list of unread articles.
4575       (setq gnus-newsgroup-unreads
4576             (gnus-set-sorted-intersection
4577              gnus-newsgroup-unreads
4578              (setq fetched-articles
4579                    (mapcar (lambda (headers) (mail-header-number headers))
4580                            gnus-newsgroup-headers))))
4581       ;; Removed marked articles that do not exist.
4582       (gnus-update-missing-marks
4583        (gnus-sorted-complement fetched-articles articles))
4584
4585       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4586       (when cached
4587         (setq gnus-newsgroup-cached cached))
4588
4589       ;; We might want to build some more threads first.
4590       (when (and gnus-fetch-old-headers
4591                  (eq gnus-headers-retrieved-by 'nov))
4592         (if (eq gnus-fetch-old-headers 'invisible)
4593             (gnus-build-all-threads)
4594           (gnus-build-old-threads)))
4595       ;; Let the Gnus agent mark articles as read.
4596       (when gnus-agent
4597         (gnus-agent-get-undownloaded-list))
4598       ;; Remove list identifiers from subject
4599       (when gnus-list-identifiers
4600         (gnus-summary-remove-list-identifiers))
4601       ;; Check whether auto-expire is to be done in this group.
4602       (setq gnus-newsgroup-auto-expire
4603             (gnus-group-auto-expirable-p group))
4604       ;; Set up the article buffer now, if necessary.
4605       (unless gnus-single-article-buffer
4606         (gnus-article-setup-buffer))
4607       ;; First and last article in this newsgroup.
4608       (when gnus-newsgroup-headers
4609         (setq gnus-newsgroup-begin
4610               (mail-header-number (car gnus-newsgroup-headers))
4611               gnus-newsgroup-end
4612               (mail-header-number
4613                (gnus-last-element gnus-newsgroup-headers))))
4614       ;; GROUP is successfully selected.
4615       (or gnus-newsgroup-headers t)))))
4616
4617 (defun gnus-articles-to-read (group &optional read-all)
4618   "Find out what articles the user wants to read."
4619   (let* ((articles
4620           ;; Select all articles if `read-all' is non-nil, or if there
4621           ;; are no unread articles.
4622           (if (or read-all
4623                   (and (zerop (length gnus-newsgroup-marked))
4624                        (zerop (length gnus-newsgroup-unreads)))
4625                   (eq (gnus-group-find-parameter group 'display)
4626                       'all))
4627               (or
4628                (gnus-uncompress-range (gnus-active group))
4629                (gnus-cache-articles-in-group group))
4630             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4631                           (copy-sequence gnus-newsgroup-unreads))
4632                   '<)))
4633          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4634          (scored (length scored-list))
4635          (number (length articles))
4636          (marked (+ (length gnus-newsgroup-marked)
4637                     (length gnus-newsgroup-dormant)))
4638          (select
4639           (cond
4640            ((numberp read-all)
4641             read-all)
4642            (t
4643             (condition-case ()
4644                 (cond
4645                  ((and (or (<= scored marked) (= scored number))
4646                        (natnump gnus-large-newsgroup)
4647                        (> number gnus-large-newsgroup))
4648                   (let* ((cursor-in-echo-area nil)
4649                          (input
4650                           (read-from-minibuffer
4651                            (format
4652                             "How many articles from %s (max %d): "
4653                             (gnus-limit-string
4654                              (gnus-group-decoded-name gnus-newsgroup-name)
4655                              35)
4656                             number)
4657                            (cons (number-to-string gnus-large-newsgroup)
4658                                  0))))
4659                     (if (string-match "^[ \t]*$" input)
4660                         number
4661                       input)))
4662                  ((and (> scored marked) (< scored number)
4663                        (> (- scored number) 20))
4664                   (let ((input
4665                          (read-string
4666                           (format "%s %s (%d scored, %d total): "
4667                                   "How many articles from"
4668                                   (gnus-group-decoded-name group)
4669                                   scored number))))
4670                     (if (string-match "^[ \t]*$" input)
4671                         number input)))
4672                  (t number))
4673               (quit
4674                (message "Quit getting the articles to read")
4675                nil))))))
4676     (setq select (if (stringp select) (string-to-number select) select))
4677     (if (or (null select) (zerop select))
4678         select
4679       (if (and (not (zerop scored)) (<= (abs select) scored))
4680           (progn
4681             (setq articles (sort scored-list '<))
4682             (setq number (length articles)))
4683         (setq articles (copy-sequence articles)))
4684
4685       (when (< (abs select) number)
4686         (if (< select 0)
4687             ;; Select the N oldest articles.
4688             (setcdr (nthcdr (1- (abs select)) articles) nil)
4689           ;; Select the N most recent articles.
4690           (setq articles (nthcdr (- number select) articles))))
4691       (setq gnus-newsgroup-unselected
4692             (gnus-sorted-intersection
4693              gnus-newsgroup-unreads
4694              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4695       (when gnus-alter-articles-to-read-function
4696         (setq gnus-newsgroup-unreads
4697               (sort
4698                (funcall gnus-alter-articles-to-read-function
4699                         gnus-newsgroup-name gnus-newsgroup-unreads)
4700                '<)))
4701       articles)))
4702
4703 (defun gnus-killed-articles (killed articles)
4704   (let (out)
4705     (while articles
4706       (when (inline (gnus-member-of-range (car articles) killed))
4707         (push (car articles) out))
4708       (setq articles (cdr articles)))
4709     out))
4710
4711 (defun gnus-uncompress-marks (marks)
4712   "Uncompress the mark ranges in MARKS."
4713   (let ((uncompressed '(score bookmark))
4714         out)
4715     (while marks
4716       (if (memq (caar marks) uncompressed)
4717           (push (car marks) out)
4718         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4719       (setq marks (cdr marks)))
4720     out))
4721
4722 (defun gnus-adjust-marked-articles (info)
4723   "Set all article lists and remove all marks that are no longer valid."
4724   (let* ((marked-lists (gnus-info-marks info))
4725          (active (gnus-active (gnus-info-group info)))
4726          (min (car active))
4727          (max (cdr active))
4728          (types gnus-article-mark-lists)
4729          (uncompressed '(score bookmark killed))
4730          marks var articles article mark)
4731
4732     (while marked-lists
4733       (setq marks (pop marked-lists))
4734       (set (setq var (intern (format "gnus-newsgroup-%s"
4735                                      (car (rassq (setq mark (car marks))
4736                                                  types)))))
4737            (if (memq (car marks) uncompressed) (cdr marks)
4738              (gnus-uncompress-range (cdr marks))))
4739
4740       (setq articles (symbol-value var))
4741
4742       ;; All articles have to be subsets of the active articles.
4743       (cond
4744        ;; Adjust "simple" lists.
4745        ((memq mark '(tick dormant expire reply save))
4746         (while articles
4747           (when (or (< (setq article (pop articles)) min) (> article max))
4748             (set var (delq article (symbol-value var))))))
4749        ;; Adjust assocs.
4750        ((memq mark uncompressed)
4751         (when (not (listp (cdr (symbol-value var))))
4752           (set var (list (symbol-value var))))
4753         (when (not (listp (cdr articles)))
4754           (setq articles (list articles)))
4755         (while articles
4756           (when (or (not (consp (setq article (pop articles))))
4757                     (< (car article) min)
4758                     (> (car article) max))
4759             (set var (delq article (symbol-value var))))))))))
4760
4761 (defun gnus-update-missing-marks (missing)
4762   "Go through the list of MISSING articles and remove them from the mark lists."
4763   (when missing
4764     (let ((types gnus-article-mark-lists)
4765           var m)
4766       ;; Go through all types.
4767       (while types
4768         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4769         (when (symbol-value var)
4770           ;; This list has articles.  So we delete all missing articles
4771           ;; from it.
4772           (setq m missing)
4773           (while m
4774             (set var (delq (pop m) (symbol-value var)))))))))
4775
4776 (defun gnus-update-marks ()
4777   "Enter the various lists of marked articles into the newsgroup info list."
4778   (let ((types gnus-article-mark-lists)
4779         (info (gnus-get-info gnus-newsgroup-name))
4780         (uncompressed '(score bookmark killed))
4781         type list newmarked symbol delta-marks)
4782     (when info
4783       ;; Add all marks lists to the list of marks lists.
4784       (while (setq type (pop types))
4785         (setq list (symbol-value
4786                     (setq symbol
4787                           (intern (format "gnus-newsgroup-%s"
4788                                           (car type))))))
4789
4790         (when list
4791           ;; Get rid of the entries of the articles that have the
4792           ;; default score.
4793           (when (and (eq (cdr type) 'score)
4794                      gnus-save-score
4795                      list)
4796             (let* ((arts list)
4797                    (prev (cons nil list))
4798                    (all prev))
4799               (while arts
4800                 (if (or (not (consp (car arts)))
4801                         (= (cdar arts) gnus-summary-default-score))
4802                     (setcdr prev (cdr arts))
4803                   (setq prev arts))
4804                 (setq arts (cdr arts)))
4805               (setq list (cdr all)))))
4806
4807         (unless (memq (cdr type) uncompressed)
4808           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4809
4810         (when (gnus-check-backend-function
4811                'request-set-mark gnus-newsgroup-name)
4812           ;; propagate flags to server, with the following exceptions:
4813           ;; uncompressed:s are not proper flags (they are cons cells)
4814           ;; cache is a internal gnus flag
4815           ;; download are local to one gnus installation (well)
4816           ;; unsend are for nndraft groups only
4817           ;; xxx: generality of this?  this suits nnimap anyway
4818           (unless (memq (cdr type) (append '(cache download unsend)
4819                                            uncompressed))
4820             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4821                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4822                    (add (gnus-remove-from-range
4823                          (gnus-copy-sequence list) old)))
4824               (when add
4825                 (push (list add 'add (list (cdr type))) delta-marks))
4826               (when del
4827                 (push (list del 'del (list (cdr type))) delta-marks)))))
4828
4829         (when list
4830           (push (cons (cdr type) list) newmarked)))
4831
4832       (when delta-marks
4833         (unless (gnus-check-group gnus-newsgroup-name)
4834           (error "Can't open server for %s" gnus-newsgroup-name))
4835         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4836
4837       ;; Enter these new marks into the info of the group.
4838       (if (nthcdr 3 info)
4839           (setcar (nthcdr 3 info) newmarked)
4840         ;; Add the marks lists to the end of the info.
4841         (when newmarked
4842           (setcdr (nthcdr 2 info) (list newmarked))))
4843
4844       ;; Cut off the end of the info if there's nothing else there.
4845       (let ((i 5))
4846         (while (and (> i 2)
4847                     (not (nth i info)))
4848           (when (nthcdr (decf i) info)
4849             (setcdr (nthcdr i info) nil)))))))
4850
4851 (defun gnus-set-mode-line (where)
4852   "Set the mode line of the article or summary buffers.
4853 If WHERE is `summary', the summary mode line format will be used."
4854   ;; Is this mode line one we keep updated?
4855   (when (and (memq where gnus-updated-mode-lines)
4856              (symbol-value
4857               (intern (format "gnus-%s-mode-line-format-spec" where))))
4858     (let (mode-string)
4859       (save-excursion
4860         ;; We evaluate this in the summary buffer since these
4861         ;; variables are buffer-local to that buffer.
4862         (set-buffer gnus-summary-buffer)
4863         ;; We bind all these variables that are used in the `eval' form
4864         ;; below.
4865         (let* ((mformat (symbol-value
4866                          (intern
4867                           (format "gnus-%s-mode-line-format-spec" where))))
4868                (gnus-tmp-group-name (gnus-group-decoded-name
4869                                      gnus-newsgroup-name))
4870                (gnus-tmp-article-number (or gnus-current-article 0))
4871                (gnus-tmp-unread gnus-newsgroup-unreads)
4872                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4873                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4874                (gnus-tmp-unread-and-unselected
4875                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4876                             (zerop gnus-tmp-unselected))
4877                        "")
4878                       ((zerop gnus-tmp-unselected)
4879                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4880                       (t (format "{%d(+%d) more}"
4881                                  gnus-tmp-unread-and-unticked
4882                                  gnus-tmp-unselected))))
4883                (gnus-tmp-subject
4884                 (if (and gnus-current-headers
4885                          (vectorp gnus-current-headers))
4886                     (gnus-mode-string-quote
4887                      (mail-header-subject gnus-current-headers))
4888                   ""))
4889                bufname-length max-len
4890                gnus-tmp-header);; passed as argument to any user-format-funcs
4891           (setq mode-string (eval mformat))
4892           (setq bufname-length (if (string-match "%b" mode-string)
4893                                    (- (length
4894                                        (buffer-name
4895                                         (if (eq where 'summary)
4896                                             nil
4897                                           (get-buffer gnus-article-buffer))))
4898                                       2)
4899                                  0))
4900           (setq max-len (max 4 (if gnus-mode-non-string-length
4901                                    (- (window-width)
4902                                       gnus-mode-non-string-length
4903                                       bufname-length)
4904                                  (length mode-string))))
4905           ;; We might have to chop a bit of the string off...
4906           (when (> (length mode-string) max-len)
4907             (setq mode-string
4908                   (concat (gnus-truncate-string mode-string (- max-len 3))
4909                           "...")))
4910           ;; Pad the mode string a bit.
4911           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4912       ;; Update the mode line.
4913       (setq mode-line-buffer-identification
4914             (gnus-mode-line-buffer-identification (list mode-string)))
4915       (set-buffer-modified-p t))))
4916
4917 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4918   "Go through the HEADERS list and add all Xrefs to a hash table.
4919 The resulting hash table is returned, or nil if no Xrefs were found."
4920   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4921          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4922          (xref-hashtb (gnus-make-hashtable))
4923          start group entry number xrefs header)
4924     (while headers
4925       (setq header (pop headers))
4926       (when (and (setq xrefs (mail-header-xref header))
4927                  (not (memq (setq number (mail-header-number header))
4928                             unreads)))
4929         (setq start 0)
4930         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4931           (setq start (match-end 0))
4932           (setq group (if prefix
4933                           (concat prefix (substring xrefs (match-beginning 1)
4934                                                     (match-end 1)))
4935                         (substring xrefs (match-beginning 1) (match-end 1))))
4936           (setq number
4937                 (string-to-int (substring xrefs (match-beginning 2)
4938                                           (match-end 2))))
4939           (if (setq entry (gnus-gethash group xref-hashtb))
4940               (setcdr entry (cons number (cdr entry)))
4941             (gnus-sethash group (cons number nil) xref-hashtb)))))
4942     (and start xref-hashtb)))
4943
4944 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4945   "Look through all the headers and mark the Xrefs as read."
4946   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4947         name entry info xref-hashtb idlist method nth4)
4948     (save-excursion
4949       (set-buffer gnus-group-buffer)
4950       (when (setq xref-hashtb
4951                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4952         (mapatoms
4953          (lambda (group)
4954            (unless (string= from-newsgroup (setq name (symbol-name group)))
4955              (setq idlist (symbol-value group))
4956              ;; Dead groups are not updated.
4957              (and (prog1
4958                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4959                             info (nth 2 entry))
4960                     (when (stringp (setq nth4 (gnus-info-method info)))
4961                       (setq nth4 (gnus-server-to-method nth4))))
4962                   ;; Only do the xrefs if the group has the same
4963                   ;; select method as the group we have just read.
4964                   (or (gnus-methods-equal-p
4965                        nth4 (gnus-find-method-for-group from-newsgroup))
4966                       virtual
4967                       (equal nth4 (setq method (gnus-find-method-for-group
4968                                                 from-newsgroup)))
4969                       (and (equal (car nth4) (car method))
4970                            (equal (nth 1 nth4) (nth 1 method))))
4971                   gnus-use-cross-reference
4972                   (or (not (eq gnus-use-cross-reference t))
4973                       virtual
4974                       ;; Only do cross-references on subscribed
4975                       ;; groups, if that is what is wanted.
4976                       (<= (gnus-info-level info) gnus-level-subscribed))
4977                   (gnus-group-make-articles-read name idlist))))
4978          xref-hashtb)))))
4979
4980 (defun gnus-compute-read-articles (group articles)
4981   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4982          (info (nth 2 entry))
4983          (active (gnus-active group))
4984          ninfo)
4985     (when entry
4986       ;; First peel off all invalid article numbers.
4987       (when active
4988         (let ((ids articles)
4989               id first)
4990           (while (setq id (pop ids))
4991             (when (and first (> id (cdr active)))
4992               ;; We'll end up in this situation in one particular
4993               ;; obscure situation.  If you re-scan a group and get
4994               ;; a new article that is cross-posted to a different
4995               ;; group that has not been re-scanned, you might get
4996               ;; crossposted article that has a higher number than
4997               ;; Gnus believes possible.  So we re-activate this
4998               ;; group as well.  This might mean doing the
4999               ;; crossposting thingy will *increase* the number
5000               ;; of articles in some groups.  Tsk, tsk.
5001               (setq active (or (gnus-activate-group group) active)))
5002             (when (or (> id (cdr active))
5003                       (< id (car active)))
5004               (setq articles (delq id articles))))))
5005       ;; If the read list is nil, we init it.
5006       (if (and active
5007                (null (gnus-info-read info))
5008                (> (car active) 1))
5009           (setq ninfo (cons 1 (1- (car active))))
5010         (setq ninfo (gnus-info-read info)))
5011       ;; Then we add the read articles to the range.
5012       (gnus-add-to-range
5013        ninfo (setq articles (sort articles '<))))))
5014
5015 (defun gnus-group-make-articles-read (group articles)
5016   "Update the info of GROUP to say that ARTICLES are read."
5017   (let* ((num 0)
5018          (entry (gnus-gethash group gnus-newsrc-hashtb))
5019          (info (nth 2 entry))
5020          (active (gnus-active group))
5021          range)
5022     (when entry
5023       (setq range (gnus-compute-read-articles group articles))
5024       (save-excursion
5025         (set-buffer gnus-group-buffer)
5026         (gnus-undo-register
5027           `(progn
5028              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5029              (gnus-info-set-read ',info ',(gnus-info-read info))
5030              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5031              (gnus-group-update-group ,group t))))
5032       ;; Add the read articles to the range.
5033       (gnus-info-set-read info range)
5034       ;; Then we have to re-compute how many unread
5035       ;; articles there are in this group.
5036       (when active
5037         (cond
5038          ((not range)
5039           (setq num (- (1+ (cdr active)) (car active))))
5040          ((not (listp (cdr range)))
5041           (setq num (- (cdr active) (- (1+ (cdr range))
5042                                        (car range)))))
5043          (t
5044           (while range
5045             (if (numberp (car range))
5046                 (setq num (1+ num))
5047               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5048             (setq range (cdr range)))
5049           (setq num (- (cdr active) num))))
5050         ;; Update the number of unread articles.
5051         (setcar entry num)
5052         ;; Update the group buffer.
5053         (gnus-group-update-group group t)))))
5054
5055 (defvar gnus-newsgroup-none-id 0)
5056
5057 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5058   (let ((cur nntp-server-buffer)
5059         (dependencies
5060          (or dependencies
5061              (save-excursion (set-buffer gnus-summary-buffer)
5062                              gnus-newsgroup-dependencies)))
5063         headers id end ref
5064         (mail-parse-charset gnus-newsgroup-charset)
5065         (mail-parse-ignored-charsets
5066          (save-excursion (condition-case nil
5067                              (set-buffer gnus-summary-buffer)
5068                            (error))
5069                          gnus-newsgroup-ignored-charsets)))
5070     (save-excursion
5071       (set-buffer nntp-server-buffer)
5072       ;; Translate all TAB characters into SPACE characters.
5073       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5074       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5075       (gnus-run-hooks 'gnus-parse-headers-hook)
5076       (let ((case-fold-search t)
5077             in-reply-to header p lines chars ctype)
5078         (goto-char (point-min))
5079         ;; Search to the beginning of the next header.  Error messages
5080         ;; do not begin with 2 or 3.
5081         (while (re-search-forward "^[23][0-9]+ " nil t)
5082           (setq id nil
5083                 ref nil)
5084           ;; This implementation of this function, with nine
5085           ;; search-forwards instead of the one re-search-forward and
5086           ;; a case (which basically was the old function) is actually
5087           ;; about twice as fast, even though it looks messier.  You
5088           ;; can't have everything, I guess.  Speed and elegance
5089           ;; doesn't always go hand in hand.
5090           (setq
5091            header
5092            (make-full-mail-header
5093             ;; Number.
5094             (prog1
5095                 (read cur)
5096               (end-of-line)
5097               (setq p (point))
5098               (narrow-to-region (point)
5099                                 (or (and (search-forward "\n.\n" nil t)
5100                                          (- (point) 2))
5101                                     (point))))
5102             ;; Subject.
5103             (progn
5104               (goto-char p)
5105               (if (search-forward "\nsubject:" nil t)
5106                   (nnheader-header-value)
5107                 "(none)"))
5108             ;; From.
5109             (progn
5110               (goto-char p)
5111               (if (search-forward "\nfrom:" nil t)
5112                   (nnheader-header-value)
5113                 "(nobody)"))
5114             ;; Date.
5115             (progn
5116               (goto-char p)
5117               (if (search-forward "\ndate:" nil t)
5118                   (nnheader-header-value) ""))
5119             ;; Message-ID.
5120             (progn
5121               (goto-char p)
5122               (setq id (if (re-search-forward
5123                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5124                            ;; We do it this way to make sure the Message-ID
5125                            ;; is (somewhat) syntactically valid.
5126                            (buffer-substring (match-beginning 1)
5127                                              (match-end 1))
5128                          ;; If there was no message-id, we just fake one
5129                          ;; to make subsequent routines simpler.
5130                          (nnheader-generate-fake-message-id))))
5131             ;; References.
5132             (progn
5133               (goto-char p)
5134               (if (search-forward "\nreferences:" nil t)
5135                   (progn
5136                     (setq end (point))
5137                     (prog1
5138                         (nnheader-header-value)
5139                       (setq ref
5140                             (buffer-substring
5141                              (progn
5142                                ;; (end-of-line)
5143                                (search-backward ">" end t)
5144                                (1+ (point)))
5145                              (progn
5146                                (search-backward "<" end t)
5147                                (point))))))
5148                 ;; Get the references from the in-reply-to header if there
5149                 ;; were no references and the in-reply-to header looks
5150                 ;; promising.
5151                 (if (and (search-forward "\nin-reply-to:" nil t)
5152                          (setq in-reply-to (nnheader-header-value))
5153                          (string-match "<[^>]+>" in-reply-to))
5154                     (let (ref2)
5155                       (setq ref (substring in-reply-to (match-beginning 0)
5156                                            (match-end 0)))
5157                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5158                         (setq ref2 (substring in-reply-to (match-beginning 0)
5159                                               (match-end 0)))
5160                         (when (> (length ref2) (length ref))
5161                           (setq ref ref2)))
5162                       ref)
5163                   (setq ref nil))))
5164             ;; Chars.
5165             (progn
5166               (goto-char p)
5167               (if (search-forward "\nchars: " nil t)
5168                   (if (numberp (setq chars (ignore-errors (read cur))))
5169                       chars -1)
5170                 -1))
5171             ;; Lines.
5172             (progn
5173               (goto-char p)
5174               (if (search-forward "\nlines: " nil t)
5175                   (if (numberp (setq lines (ignore-errors (read cur))))
5176                       lines -1)
5177                 -1))
5178             ;; Xref.
5179             (progn
5180               (goto-char p)
5181               (and (search-forward "\nxref:" nil t)
5182                    (nnheader-header-value)))
5183             ;; Extra.
5184             (when gnus-extra-headers
5185               (let ((extra gnus-extra-headers)
5186                     out)
5187                 (while extra
5188                   (goto-char p)
5189                   (when (search-forward
5190                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5191                     (push (cons (car extra) (nnheader-header-value)) out))
5192                   (pop extra))
5193                 out))))
5194           (goto-char p)
5195           (if (and (search-forward "\ncontent-type: " nil t)
5196                    (setq ctype (nnheader-header-value)))
5197               (mime-entity-set-content-type-internal
5198                header (mime-parse-Content-Type ctype)))
5199           (when (equal id ref)
5200             (setq ref nil))
5201
5202           (when gnus-alter-header-function
5203             (funcall gnus-alter-header-function header)
5204             (setq id (mail-header-id header)
5205                   ref (gnus-parent-id (mail-header-references header))))
5206
5207           (when (setq header
5208                       (gnus-dependencies-add-header
5209                        header dependencies force-new))
5210             (push header headers))
5211           (goto-char (point-max))
5212           (widen))
5213         (nreverse headers)))))
5214
5215 ;; Goes through the xover lines and returns a list of vectors
5216 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5217                                                   force-new dependencies
5218                                                   group also-fetch-heads)
5219   "Parse the news overview data in the server buffer.
5220 Return a list of headers that match SEQUENCE (see
5221 `nntp-retrieve-headers')."
5222   ;; Get the Xref when the users reads the articles since most/some
5223   ;; NNTP servers do not include Xrefs when using XOVER.
5224   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5225   (let ((mail-parse-charset gnus-newsgroup-charset)
5226         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5227         (cur nntp-server-buffer)
5228         (dependencies (or dependencies gnus-newsgroup-dependencies))
5229         (allp (cond
5230                ((eq gnus-read-all-available-headers t)
5231                 t)
5232                ((stringp gnus-read-all-available-headers)
5233                 (string-match gnus-read-all-available-headers group))
5234                (t
5235                 nil)))
5236         number headers header)
5237     (save-excursion
5238       (set-buffer nntp-server-buffer)
5239       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5240       ;; Allow the user to mangle the headers before parsing them.
5241       (gnus-run-hooks 'gnus-parse-headers-hook)
5242       (goto-char (point-min))
5243       (while (not (eobp))
5244         (condition-case ()
5245             (while (and (or sequence allp)
5246                         (not (eobp)))
5247               (setq number (read cur))
5248               (when (not allp)
5249                 (while (and sequence
5250                             (< (car sequence) number))
5251                   (setq sequence (cdr sequence))))
5252               (when (and (or allp
5253                              (and sequence
5254                                   (eq number (car sequence))))
5255                          (progn
5256                            (setq sequence (cdr sequence))
5257                            (setq header (inline
5258                                           (gnus-nov-parse-line
5259                                            number dependencies force-new)))))
5260                 (push header headers))
5261               (forward-line 1))
5262           (error
5263            (gnus-error 4 "Strange nov line (%d)"
5264                        (count-lines (point-min) (point)))))
5265         (forward-line 1))
5266       ;; A common bug in inn is that if you have posted an article and
5267       ;; then retrieves the active file, it will answer correctly --
5268       ;; the new article is included.  However, a NOV entry for the
5269       ;; article may not have been generated yet, so this may fail.
5270       ;; We work around this problem by retrieving the last few
5271       ;; headers using HEAD.
5272       (if (or (not also-fetch-heads)
5273               (not sequence))
5274           ;; We (probably) got all the headers.
5275           (nreverse headers)
5276         (let ((gnus-nov-is-evil t))
5277           (nconc
5278            (nreverse headers)
5279            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5280            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5281                (when (eq (gnus-retrieve-headers sequence group) 'headers)
5282                  (gnus-get-newsgroup-headers))
5283              (gnus-retrieve-parsed-headers sequence group))))))))
5284
5285 (defun gnus-article-get-xrefs ()
5286   "Fill in the Xref value in `gnus-current-headers', if necessary.
5287 This is meant to be called in `gnus-article-internal-prepare-hook'."
5288   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5289                                  gnus-current-headers)))
5290     (or (not gnus-use-cross-reference)
5291         (not headers)
5292         (and (mail-header-xref headers)
5293              (not (string= (mail-header-xref headers) "")))
5294         (let ((case-fold-search t)
5295               xref)
5296           (save-restriction
5297             (nnheader-narrow-to-headers)
5298             (goto-char (point-min))
5299             (when (or (and (not (eobp))
5300                            (eq (downcase (char-after)) ?x)
5301                            (looking-at "Xref:"))
5302                       (search-forward "\nXref:" nil t))
5303               (goto-char (1+ (match-end 0)))
5304               (setq xref (buffer-substring (point)
5305                                            (progn (end-of-line) (point))))
5306               (mail-header-set-xref headers xref)))))))
5307
5308 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5309   "Find article ID and insert the summary line for that article.
5310 OLD-HEADER can either be a header or a line number to insert
5311 the subject line on."
5312   (let* ((line (and (numberp old-header) old-header))
5313          (old-header (and (vectorp old-header) old-header))
5314          (header (cond ((and old-header use-old-header)
5315                         old-header)
5316                        ((and (numberp id)
5317                              (gnus-number-to-header id))
5318                         (gnus-number-to-header id))
5319                        (t
5320                         (gnus-read-header id))))
5321          (number (and (numberp id) id))
5322          d)
5323     (when header
5324       ;; Rebuild the thread that this article is part of and go to the
5325       ;; article we have fetched.
5326       (when (and (not gnus-show-threads)
5327                  old-header)
5328         (when (and number
5329                    (setq d (gnus-data-find (mail-header-number old-header))))
5330           (goto-char (gnus-data-pos d))
5331           (gnus-data-remove
5332            number
5333            (- (gnus-point-at-bol)
5334               (prog1
5335                   (1+ (gnus-point-at-eol))
5336                 (gnus-delete-line))))))
5337       (when old-header
5338         (mail-header-set-number header (mail-header-number old-header)))
5339       (setq gnus-newsgroup-sparse
5340             (delq (setq number (mail-header-number header))
5341                   gnus-newsgroup-sparse))
5342       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5343       (push number gnus-newsgroup-limit)
5344       (gnus-rebuild-thread (mail-header-id header) line)
5345       (gnus-summary-goto-subject number nil t))
5346     (when (and (numberp number)
5347                (> number 0))
5348       ;; We have to update the boundaries even if we can't fetch the
5349       ;; article if ID is a number -- so that the next `P' or `N'
5350       ;; command will fetch the previous (or next) article even
5351       ;; if the one we tried to fetch this time has been canceled.
5352       (when (> number gnus-newsgroup-end)
5353         (setq gnus-newsgroup-end number))
5354       (when (< number gnus-newsgroup-begin)
5355         (setq gnus-newsgroup-begin number))
5356       (setq gnus-newsgroup-unselected
5357             (delq number gnus-newsgroup-unselected)))
5358     ;; Report back a success?
5359     (and header (mail-header-number header))))
5360
5361 ;;; Process/prefix in the summary buffer
5362
5363 (defun gnus-summary-work-articles (n)
5364   "Return a list of articles to be worked upon.
5365 The prefix argument, the list of process marked articles, and the
5366 current article will be taken into consideration."
5367   (save-excursion
5368     (set-buffer gnus-summary-buffer)
5369     (cond
5370      (n
5371       ;; A numerical prefix has been given.
5372       (setq n (prefix-numeric-value n))
5373       (let ((backward (< n 0))
5374             (n (abs (prefix-numeric-value n)))
5375             articles article)
5376         (save-excursion
5377           (while
5378               (and (> n 0)
5379                    (push (setq article (gnus-summary-article-number))
5380                          articles)
5381                    (if backward
5382                        (gnus-summary-find-prev nil article)
5383                      (gnus-summary-find-next nil article)))
5384             (decf n)))
5385         (nreverse articles)))
5386      ((and (gnus-region-active-p) (mark))
5387       (message "region active")
5388       ;; Work on the region between point and mark.
5389       (let ((max (max (point) (mark)))
5390             articles article)
5391         (save-excursion
5392           (goto-char (min (point) (mark)))
5393           (while
5394               (and
5395                (push (setq article (gnus-summary-article-number)) articles)
5396                (gnus-summary-find-next nil article)
5397                (< (point) max)))
5398           (nreverse articles))))
5399      (gnus-newsgroup-processable
5400       ;; There are process-marked articles present.
5401       ;; Save current state.
5402       (gnus-summary-save-process-mark)
5403       ;; Return the list.
5404       (reverse gnus-newsgroup-processable))
5405      (t
5406       ;; Just return the current article.
5407       (list (gnus-summary-article-number))))))
5408
5409 (defmacro gnus-summary-iterate (arg &rest forms)
5410   "Iterate over the process/prefixed articles and do FORMS.
5411 ARG is the interactive prefix given to the command.  FORMS will be
5412 executed with point over the summary line of the articles."
5413   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5414     `(let ((,articles (gnus-summary-work-articles ,arg)))
5415        (while ,articles
5416          (gnus-summary-goto-subject (car ,articles))
5417          ,@forms
5418          (pop ,articles)))))
5419
5420 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5421 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5422
5423 (defun gnus-summary-save-process-mark ()
5424   "Push the current set of process marked articles on the stack."
5425   (interactive)
5426   (push (copy-sequence gnus-newsgroup-processable)
5427         gnus-newsgroup-process-stack))
5428
5429 (defun gnus-summary-kill-process-mark ()
5430   "Push the current set of process marked articles on the stack and unmark."
5431   (interactive)
5432   (gnus-summary-save-process-mark)
5433   (gnus-summary-unmark-all-processable))
5434
5435 (defun gnus-summary-yank-process-mark ()
5436   "Pop the last process mark state off the stack and restore it."
5437   (interactive)
5438   (unless gnus-newsgroup-process-stack
5439     (error "Empty mark stack"))
5440   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5441
5442 (defun gnus-summary-process-mark-set (set)
5443   "Make SET into the current process marked articles."
5444   (gnus-summary-unmark-all-processable)
5445   (while set
5446     (gnus-summary-set-process-mark (pop set))))
5447
5448 ;;; Searching and stuff
5449
5450 (defun gnus-summary-search-group (&optional backward use-level)
5451   "Search for next unread newsgroup.
5452 If optional argument BACKWARD is non-nil, search backward instead."
5453   (save-excursion
5454     (set-buffer gnus-group-buffer)
5455     (when (gnus-group-search-forward
5456            backward nil (if use-level (gnus-group-group-level) nil))
5457       (gnus-group-group-name))))
5458
5459 (defun gnus-summary-best-group (&optional exclude-group)
5460   "Find the name of the best unread group.
5461 If EXCLUDE-GROUP, do not go to this group."
5462   (save-excursion
5463     (set-buffer gnus-group-buffer)
5464     (save-excursion
5465       (gnus-group-best-unread-group exclude-group))))
5466
5467 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5468   (if backward (gnus-summary-find-prev)
5469     (let* ((dummy (gnus-summary-article-intangible-p))
5470            (article (or article (gnus-summary-article-number)))
5471            (arts (gnus-data-find-list article))
5472            result)
5473       (when (and (not dummy)
5474                  (or (not gnus-summary-check-current)
5475                      (not unread)
5476                      (not (gnus-data-unread-p (car arts)))))
5477         (setq arts (cdr arts)))
5478       (when (setq result
5479                   (if unread
5480                       (progn
5481                         (while arts
5482                           (when (or (and undownloaded
5483                                          (eq gnus-undownloaded-mark
5484                                              (gnus-data-mark (car arts))))
5485                                     (gnus-data-unread-p (car arts)))
5486                             (setq result (car arts)
5487                                   arts nil))
5488                           (setq arts (cdr arts)))
5489                         result)
5490                     (car arts)))
5491         (goto-char (gnus-data-pos result))
5492         (gnus-data-number result)))))
5493
5494 (defun gnus-summary-find-prev (&optional unread article)
5495   (let* ((eobp (eobp))
5496          (article (or article (gnus-summary-article-number)))
5497          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5498          result)
5499     (when (and (not eobp)
5500                (or (not gnus-summary-check-current)
5501                    (not unread)
5502                    (not (gnus-data-unread-p (car arts)))))
5503       (setq arts (cdr arts)))
5504     (when (setq result
5505                 (if unread
5506                     (progn
5507                       (while arts
5508                         (when (gnus-data-unread-p (car arts))
5509                           (setq result (car arts)
5510                                 arts nil))
5511                         (setq arts (cdr arts)))
5512                       result)
5513                   (car arts)))
5514       (goto-char (gnus-data-pos result))
5515       (gnus-data-number result))))
5516
5517 (defun gnus-summary-find-subject (subject &optional unread backward article)
5518   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5519          (article (or article (gnus-summary-article-number)))
5520          (articles (gnus-data-list backward))
5521          (arts (gnus-data-find-list article articles))
5522          result)
5523     (when (or (not gnus-summary-check-current)
5524               (not unread)
5525               (not (gnus-data-unread-p (car arts))))
5526       (setq arts (cdr arts)))
5527     (while arts
5528       (and (or (not unread)
5529                (gnus-data-unread-p (car arts)))
5530            (vectorp (gnus-data-header (car arts)))
5531            (gnus-subject-equal
5532             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5533            (setq result (car arts)
5534                  arts nil))
5535       (setq arts (cdr arts)))
5536     (and result
5537          (goto-char (gnus-data-pos result))
5538          (gnus-data-number result))))
5539
5540 (defun gnus-summary-search-forward (&optional unread subject backward)
5541   "Search forward for an article.
5542 If UNREAD, look for unread articles.  If SUBJECT, look for
5543 articles with that subject.  If BACKWARD, search backward instead."
5544   (cond (subject (gnus-summary-find-subject subject unread backward))
5545         (backward (gnus-summary-find-prev unread))
5546         (t (gnus-summary-find-next unread))))
5547
5548 (defun gnus-recenter (&optional n)
5549   "Center point in window and redisplay frame.
5550 Also do horizontal recentering."
5551   (interactive "P")
5552   (when (and gnus-auto-center-summary
5553              (not (eq gnus-auto-center-summary 'vertical)))
5554     (gnus-horizontal-recenter))
5555   (recenter n))
5556
5557 (defun gnus-summary-recenter ()
5558   "Center point in the summary window.
5559 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5560 displayed, no centering will be performed."
5561   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5562   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5563   (interactive)
5564   (let* ((top (cond ((< (window-height) 4) 0)
5565                     ((< (window-height) 7) 1)
5566                     (t (if (numberp gnus-auto-center-summary)
5567                            gnus-auto-center-summary
5568                          2))))
5569          (height (1- (window-height)))
5570          (bottom (save-excursion (goto-char (point-max))
5571                                  (forward-line (- height))
5572                                  (point)))
5573          (window (get-buffer-window (current-buffer))))
5574     ;; The user has to want it.
5575     (when gnus-auto-center-summary
5576       (when (get-buffer-window gnus-article-buffer)
5577         ;; Only do recentering when the article buffer is displayed,
5578         ;; Set the window start to either `bottom', which is the biggest
5579         ;; possible valid number, or the second line from the top,
5580         ;; whichever is the least.
5581         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5582           (if (> bottom top-pos)
5583               ;; Keep the second line from the top visible
5584               (set-window-start window top-pos t)
5585             ;; Try to keep the bottom line visible; if it's partially
5586             ;; obscured, either scroll one more line to make it fully
5587             ;; visible, or revert to using TOP-POS.
5588             (save-excursion
5589               (goto-char (point-max))
5590               (forward-line -1)
5591               (let ((last-line-start (point)))
5592                 (goto-char bottom)
5593                 (set-window-start window (point) t)
5594                 (when (not (pos-visible-in-window-p last-line-start window))
5595                   (forward-line 1)
5596                   (set-window-start window (min (point) top-pos) t)))))))
5597       ;; Do horizontal recentering while we're at it.
5598       (when (and (get-buffer-window (current-buffer) t)
5599                  (not (eq gnus-auto-center-summary 'vertical)))
5600         (let ((selected (selected-window)))
5601           (select-window (get-buffer-window (current-buffer) t))
5602           (gnus-summary-position-point)
5603           (gnus-horizontal-recenter)
5604           (select-window selected))))))
5605
5606 (defun gnus-summary-jump-to-group (newsgroup)
5607   "Move point to NEWSGROUP in group mode buffer."
5608   ;; Keep update point of group mode buffer if visible.
5609   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5610       (save-window-excursion
5611         ;; Take care of tree window mode.
5612         (when (get-buffer-window gnus-group-buffer)
5613           (pop-to-buffer gnus-group-buffer))
5614         (gnus-group-jump-to-group newsgroup))
5615     (save-excursion
5616       ;; Take care of tree window mode.
5617       (if (get-buffer-window gnus-group-buffer)
5618           (pop-to-buffer gnus-group-buffer)
5619         (set-buffer gnus-group-buffer))
5620       (gnus-group-jump-to-group newsgroup))))
5621
5622 ;; This function returns a list of article numbers based on the
5623 ;; difference between the ranges of read articles in this group and
5624 ;; the range of active articles.
5625 (defun gnus-list-of-unread-articles (group)
5626   (let* ((read (gnus-info-read (gnus-get-info group)))
5627          (active (or (gnus-active group) (gnus-activate-group group)))
5628          (last (cdr active))
5629          first nlast unread)
5630     ;; If none are read, then all are unread.
5631     (if (not read)
5632         (setq first (car active))
5633       ;; If the range of read articles is a single range, then the
5634       ;; first unread article is the article after the last read
5635       ;; article.  Sounds logical, doesn't it?
5636       (if (and (not (listp (cdr read)))
5637                (or (< (car read) (car active))
5638                    (progn (setq read (list read))
5639                           nil)))
5640           (setq first (max (car active) (1+ (cdr read))))
5641         ;; `read' is a list of ranges.
5642         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5643                                   (caar read)))
5644                   1)
5645           (setq first (car active)))
5646         (while read
5647           (when first
5648             (while (< first nlast)
5649               (push first unread)
5650               (setq first (1+ first))))
5651           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5652           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5653           (setq read (cdr read)))))
5654     ;; And add the last unread articles.
5655     (while (<= first last)
5656       (push first unread)
5657       (setq first (1+ first)))
5658     ;; Return the list of unread articles.
5659     (delq 0 (nreverse unread))))
5660
5661 (defun gnus-list-of-read-articles (group)
5662   "Return a list of unread, unticked and non-dormant articles."
5663   (let* ((info (gnus-get-info group))
5664          (marked (gnus-info-marks info))
5665          (active (gnus-active group)))
5666     (and info active
5667          (gnus-set-difference
5668           (gnus-sorted-complement
5669            (gnus-uncompress-range active)
5670            (gnus-list-of-unread-articles group))
5671           (append
5672            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5673            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5674
5675 ;; Various summary commands
5676
5677 (defun gnus-summary-select-article-buffer ()
5678   "Reconfigure windows to show article buffer."
5679   (interactive)
5680   (if (not (gnus-buffer-live-p gnus-article-buffer))
5681       (error "There is no article buffer for this summary buffer")
5682     (gnus-configure-windows 'article)
5683     (select-window (get-buffer-window gnus-article-buffer))))
5684
5685 (defun gnus-summary-universal-argument (arg)
5686   "Perform any operation on all articles that are process/prefixed."
5687   (interactive "P")
5688   (let ((articles (gnus-summary-work-articles arg))
5689         func article)
5690     (if (eq
5691          (setq
5692           func
5693           (key-binding
5694            (read-key-sequence
5695             (substitute-command-keys
5696              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5697          'undefined)
5698         (gnus-error 1 "Undefined key")
5699       (save-excursion
5700         (while articles
5701           (gnus-summary-goto-subject (setq article (pop articles)))
5702           (let (gnus-newsgroup-processable)
5703             (command-execute func))
5704           (gnus-summary-remove-process-mark article)))))
5705   (gnus-summary-position-point))
5706
5707 (defun gnus-summary-toggle-truncation (&optional arg)
5708   "Toggle truncation of summary lines.
5709 With arg, turn line truncation on iff arg is positive."
5710   (interactive "P")
5711   (setq truncate-lines
5712         (if (null arg) (not truncate-lines)
5713           (> (prefix-numeric-value arg) 0)))
5714   (redraw-display))
5715
5716 (defun gnus-summary-reselect-current-group (&optional all rescan)
5717   "Exit and then reselect the current newsgroup.
5718 The prefix argument ALL means to select all articles."
5719   (interactive "P")
5720   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5721     (error "Ephemeral groups can't be reselected"))
5722   (let ((current-subject (gnus-summary-article-number))
5723         (group gnus-newsgroup-name))
5724     (setq gnus-newsgroup-begin nil)
5725     (gnus-summary-exit)
5726     ;; We have to adjust the point of group mode buffer because
5727     ;; point was moved to the next unread newsgroup by exiting.
5728     (gnus-summary-jump-to-group group)
5729     (when rescan
5730       (save-excursion
5731         (save-window-excursion
5732           ;; Don't show group contents.
5733           (set-window-start (selected-window) (point-max))
5734           (gnus-group-get-new-news-this-group 1))))
5735     (gnus-group-read-group all t)
5736     (gnus-summary-goto-subject current-subject nil t)))
5737
5738 (defun gnus-summary-rescan-group (&optional all)
5739   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5740   (interactive "P")
5741   (gnus-summary-reselect-current-group all t))
5742
5743 (defun gnus-summary-update-info (&optional non-destructive)
5744   (save-excursion
5745     (let ((group gnus-newsgroup-name))
5746       (when group
5747         (when gnus-newsgroup-kill-headers
5748           (setq gnus-newsgroup-killed
5749                 (gnus-compress-sequence
5750                  (nconc
5751                   (gnus-set-sorted-intersection
5752                    (gnus-uncompress-range gnus-newsgroup-killed)
5753                    (setq gnus-newsgroup-unselected
5754                          (sort gnus-newsgroup-unselected '<)))
5755                   (setq gnus-newsgroup-unreads
5756                         (sort gnus-newsgroup-unreads '<)))
5757                  t)))
5758         (unless (listp (cdr gnus-newsgroup-killed))
5759           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5760         (let ((headers gnus-newsgroup-headers))
5761           ;; Set the new ranges of read articles.
5762           (save-excursion
5763             (set-buffer gnus-group-buffer)
5764             (gnus-undo-force-boundary))
5765           (gnus-update-read-articles
5766            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5767           ;; Set the current article marks.
5768           (let ((gnus-newsgroup-scored
5769                  (if (and (not gnus-save-score)
5770                           (not non-destructive))
5771                      nil
5772                    gnus-newsgroup-scored)))
5773             (save-excursion
5774               (gnus-update-marks)))
5775           ;; Do the cross-ref thing.
5776           (when gnus-use-cross-reference
5777             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5778           ;; Do not switch windows but change the buffer to work.
5779           (set-buffer gnus-group-buffer)
5780           (unless (gnus-ephemeral-group-p group)
5781             (gnus-group-update-group group)))))))
5782
5783 (defun gnus-summary-save-newsrc (&optional force)
5784   "Save the current number of read/marked articles in the dribble buffer.
5785 The dribble buffer will then be saved.
5786 If FORCE (the prefix), also save the .newsrc file(s)."
5787   (interactive "P")
5788   (gnus-summary-update-info t)
5789   (if force
5790       (gnus-save-newsrc-file)
5791     (gnus-dribble-save)))
5792
5793 (defun gnus-summary-exit (&optional temporary)
5794   "Exit reading current newsgroup, and then return to group selection mode.
5795 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5796   (interactive)
5797   (gnus-set-global-variables)
5798   (gnus-kill-save-kill-buffer)
5799   (gnus-async-halt-prefetch)
5800   (let* ((group gnus-newsgroup-name)
5801          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5802          (mode major-mode)
5803          (group-point nil)
5804          (buf (current-buffer)))
5805     (unless quit-config
5806       ;; Do adaptive scoring, and possibly save score files.
5807       (when gnus-newsgroup-adaptive
5808         (gnus-score-adaptive))
5809       (when gnus-use-scoring
5810         (gnus-score-save)))
5811     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5812     ;; If we have several article buffers, we kill them at exit.
5813     (unless gnus-single-article-buffer
5814       (gnus-kill-buffer gnus-original-article-buffer)
5815       (setq gnus-article-current nil))
5816     (when gnus-use-cache
5817       (gnus-cache-possibly-remove-articles)
5818       (gnus-cache-save-buffers))
5819     (gnus-async-prefetch-remove-group group)
5820     (when gnus-suppress-duplicates
5821       (gnus-dup-enter-articles))
5822     (when gnus-use-trees
5823       (gnus-tree-close group))
5824     (when gnus-use-cache
5825       (gnus-cache-write-active))
5826     ;; Remove entries for this group.
5827     (nnmail-purge-split-history (gnus-group-real-name group))
5828     ;; Make all changes in this group permanent.
5829     (unless quit-config
5830       (gnus-run-hooks 'gnus-exit-group-hook)
5831       (gnus-summary-update-info))
5832     (gnus-close-group group)
5833     ;; Make sure where we were, and go to next newsgroup.
5834     (set-buffer gnus-group-buffer)
5835     (unless quit-config
5836       (gnus-group-jump-to-group group))
5837     (gnus-run-hooks 'gnus-summary-exit-hook)
5838     (unless (or quit-config
5839                 ;; If this group has disappeared from the summary
5840                 ;; buffer, don't skip forwards.
5841                 (not (string= group (gnus-group-group-name))))
5842       (gnus-group-next-unread-group 1))
5843     (setq group-point (point))
5844     (if temporary
5845         nil                             ;Nothing to do.
5846       ;; If we have several article buffers, we kill them at exit.
5847       (unless gnus-single-article-buffer
5848         (gnus-kill-buffer gnus-article-buffer)
5849         (gnus-kill-buffer gnus-original-article-buffer)
5850         (setq gnus-article-current nil))
5851       (set-buffer buf)
5852       (if (not gnus-kill-summary-on-exit)
5853           (progn
5854             (gnus-deaden-summary)
5855             (setq mode nil))
5856         ;; We set all buffer-local variables to nil.  It is unclear why
5857         ;; this is needed, but if we don't, buffer-local variables are
5858         ;; not garbage-collected, it seems.  This would the lead to en
5859         ;; ever-growing Emacs.
5860         (gnus-summary-clear-local-variables)
5861         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5862           (gnus-summary-clear-local-variables))
5863         (when (get-buffer gnus-article-buffer)
5864           (bury-buffer gnus-article-buffer))
5865         ;; We clear the global counterparts of the buffer-local
5866         ;; variables as well, just to be on the safe side.
5867         (set-buffer gnus-group-buffer)
5868         (gnus-summary-clear-local-variables)
5869         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5870           (gnus-summary-clear-local-variables)))
5871       (setq gnus-current-select-method gnus-select-method)
5872       (pop-to-buffer gnus-group-buffer)
5873       (if (not quit-config)
5874           (progn
5875             (goto-char group-point)
5876             (gnus-configure-windows 'group 'force)
5877             (unless (pos-visible-in-window-p)
5878               (forward-line (/ (static-if (featurep 'xemacs)
5879                                    (window-displayed-height)
5880                                  (1- (window-height)))
5881                                -2))
5882               (set-window-start (selected-window) (point))
5883               (goto-char group-point)))
5884         (gnus-handle-ephemeral-exit quit-config))
5885       ;; Return to group mode buffer.
5886       (when (eq mode 'gnus-summary-mode)
5887         (gnus-kill-buffer buf))
5888       ;; Clear the current group name.
5889       (unless quit-config
5890         (setq gnus-newsgroup-name nil)))))
5891
5892 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5893 (defun gnus-summary-exit-no-update (&optional no-questions)
5894   "Quit reading current newsgroup without updating read article info."
5895   (interactive)
5896   (let* ((group gnus-newsgroup-name)
5897          (quit-config (gnus-group-quit-config group)))
5898     (when (or no-questions
5899               gnus-expert-user
5900               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5901       (gnus-async-halt-prefetch)
5902       (mapcar 'funcall
5903               (delq 'gnus-summary-expire-articles
5904                     (copy-sequence gnus-summary-prepare-exit-hook)))
5905       ;; If we have several article buffers, we kill them at exit.
5906       (unless gnus-single-article-buffer
5907         (gnus-kill-buffer gnus-article-buffer)
5908         (gnus-kill-buffer gnus-original-article-buffer)
5909         (setq gnus-article-current nil))
5910       (if (not gnus-kill-summary-on-exit)
5911           (gnus-deaden-summary)
5912         (gnus-close-group group)
5913         (gnus-summary-clear-local-variables)
5914         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5915           (gnus-summary-clear-local-variables))
5916         (set-buffer gnus-group-buffer)
5917         (gnus-summary-clear-local-variables)
5918         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5919           (gnus-summary-clear-local-variables))
5920         (when (get-buffer gnus-summary-buffer)
5921           (kill-buffer gnus-summary-buffer)))
5922       (unless gnus-single-article-buffer
5923         (setq gnus-article-current nil))
5924       (when gnus-use-trees
5925         (gnus-tree-close group))
5926       (gnus-async-prefetch-remove-group group)
5927       (when (get-buffer gnus-article-buffer)
5928         (bury-buffer gnus-article-buffer))
5929       ;; Return to the group buffer.
5930       (gnus-configure-windows 'group 'force)
5931       ;; Clear the current group name.
5932       (setq gnus-newsgroup-name nil)
5933       (when (equal (gnus-group-group-name) group)
5934         (gnus-group-next-unread-group 1))
5935       (when quit-config
5936         (gnus-handle-ephemeral-exit quit-config)))))
5937
5938 (defun gnus-handle-ephemeral-exit (quit-config)
5939   "Handle movement when leaving an ephemeral group.
5940 The state which existed when entering the ephemeral is reset."
5941   (if (not (buffer-name (car quit-config)))
5942       (gnus-configure-windows 'group 'force)
5943     (set-buffer (car quit-config))
5944     (cond ((eq major-mode 'gnus-summary-mode)
5945            (gnus-set-global-variables))
5946           ((eq major-mode 'gnus-article-mode)
5947            (save-excursion
5948              ;; The `gnus-summary-buffer' variable may point
5949              ;; to the old summary buffer when using a single
5950              ;; article buffer.
5951              (unless (gnus-buffer-live-p gnus-summary-buffer)
5952                (set-buffer gnus-group-buffer))
5953              (set-buffer gnus-summary-buffer)
5954              (gnus-set-global-variables))))
5955     (if (or (eq (cdr quit-config) 'article)
5956             (eq (cdr quit-config) 'pick))
5957         (progn
5958           ;; The current article may be from the ephemeral group
5959           ;; thus it is best that we reload this article
5960           (gnus-summary-show-article)
5961           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5962               (gnus-configure-windows 'pick 'force)
5963             (gnus-configure-windows (cdr quit-config) 'force)))
5964       (gnus-configure-windows (cdr quit-config) 'force))
5965     (when (eq major-mode 'gnus-summary-mode)
5966       (gnus-summary-next-subject 1 nil t)
5967       (gnus-summary-recenter)
5968       (gnus-summary-position-point))))
5969
5970 (defun gnus-summary-preview-mime-message ()
5971   "MIME decode and play this message."
5972   (interactive)
5973   (let ((gnus-break-pages nil)
5974         (gnus-show-mime t))
5975     (gnus-summary-select-article gnus-show-all-headers t))
5976   (select-window (get-buffer-window gnus-article-buffer)))
5977
5978 ;;; Dead summaries.
5979
5980 (defvar gnus-dead-summary-mode-map nil)
5981
5982 (unless gnus-dead-summary-mode-map
5983   (setq gnus-dead-summary-mode-map (make-keymap))
5984   (suppress-keymap gnus-dead-summary-mode-map)
5985   (substitute-key-definition
5986    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5987   (let ((keys '("\C-d" "\r" "\177" [delete])))
5988     (while keys
5989       (define-key gnus-dead-summary-mode-map
5990         (pop keys) 'gnus-summary-wake-up-the-dead))))
5991
5992 (defvar gnus-dead-summary-mode nil
5993   "Minor mode for Gnus summary buffers.")
5994
5995 (defun gnus-dead-summary-mode (&optional arg)
5996   "Minor mode for Gnus summary buffers."
5997   (interactive "P")
5998   (when (eq major-mode 'gnus-summary-mode)
5999     (make-local-variable 'gnus-dead-summary-mode)
6000     (setq gnus-dead-summary-mode
6001           (if (null arg) (not gnus-dead-summary-mode)
6002             (> (prefix-numeric-value arg) 0)))
6003     (when gnus-dead-summary-mode
6004       (gnus-add-minor-mode
6005        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6006
6007 (defun gnus-deaden-summary ()
6008   "Make the current summary buffer into a dead summary buffer."
6009   ;; Kill any previous dead summary buffer.
6010   (when (and gnus-dead-summary
6011              (buffer-name gnus-dead-summary))
6012     (save-excursion
6013       (set-buffer gnus-dead-summary)
6014       (when gnus-dead-summary-mode
6015         (kill-buffer (current-buffer)))))
6016   ;; Make this the current dead summary.
6017   (setq gnus-dead-summary (current-buffer))
6018   (gnus-dead-summary-mode 1)
6019   (let ((name (buffer-name)))
6020     (when (string-match "Summary" name)
6021       (rename-buffer
6022        (concat (substring name 0 (match-beginning 0)) "Dead "
6023                (substring name (match-beginning 0)))
6024        t)
6025       (bury-buffer))))
6026
6027 (defun gnus-kill-or-deaden-summary (buffer)
6028   "Kill or deaden the summary BUFFER."
6029   (save-excursion
6030     (when (and (buffer-name buffer)
6031                (not gnus-single-article-buffer))
6032       (save-excursion
6033         (set-buffer buffer)
6034         (gnus-kill-buffer gnus-article-buffer)
6035         (gnus-kill-buffer gnus-original-article-buffer)))
6036     (cond (gnus-kill-summary-on-exit
6037            (when (and gnus-use-trees
6038                       (gnus-buffer-exists-p buffer))
6039              (save-excursion
6040                (set-buffer buffer)
6041                (gnus-tree-close gnus-newsgroup-name)))
6042            (gnus-kill-buffer buffer))
6043           ((gnus-buffer-exists-p buffer)
6044            (save-excursion
6045              (set-buffer buffer)
6046              (gnus-deaden-summary))))))
6047
6048 (defun gnus-summary-wake-up-the-dead (&rest args)
6049   "Wake up the dead summary buffer."
6050   (interactive)
6051   (gnus-dead-summary-mode -1)
6052   (let ((name (buffer-name)))
6053     (when (string-match "Dead " name)
6054       (rename-buffer
6055        (concat (substring name 0 (match-beginning 0))
6056                (substring name (match-end 0)))
6057        t)))
6058   (gnus-message 3 "This dead summary is now alive again"))
6059
6060 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6061 (defun gnus-summary-fetch-faq (&optional faq-dir)
6062   "Fetch the FAQ for the current group.
6063 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6064 in."
6065   (interactive
6066    (list
6067     (when current-prefix-arg
6068       (completing-read
6069        "Faq dir: " (and (listp gnus-group-faq-directory)
6070                         (mapcar (lambda (file) (list file))
6071                                 gnus-group-faq-directory))))))
6072   (let (gnus-faq-buffer)
6073     (when (setq gnus-faq-buffer
6074                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6075       (gnus-configure-windows 'summary-faq))))
6076
6077 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6078 (defun gnus-summary-describe-group (&optional force)
6079   "Describe the current newsgroup."
6080   (interactive "P")
6081   (gnus-group-describe-group force gnus-newsgroup-name))
6082
6083 (defun gnus-summary-describe-briefly ()
6084   "Describe summary mode commands briefly."
6085   (interactive)
6086   (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")))
6087
6088 ;; Walking around group mode buffer from summary mode.
6089
6090 (defun gnus-summary-next-group (&optional no-article target-group backward)
6091   "Exit current newsgroup and then select next unread newsgroup.
6092 If prefix argument NO-ARTICLE is non-nil, no article is selected
6093 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6094 previous group instead."
6095   (interactive "P")
6096   ;; Stop pre-fetching.
6097   (gnus-async-halt-prefetch)
6098   (let ((current-group gnus-newsgroup-name)
6099         (current-buffer (current-buffer))
6100         entered)
6101     ;; First we semi-exit this group to update Xrefs and all variables.
6102     ;; We can't do a real exit, because the window conf must remain
6103     ;; the same in case the user is prompted for info, and we don't
6104     ;; want the window conf to change before that...
6105     (gnus-summary-exit t)
6106     (while (not entered)
6107       ;; Then we find what group we are supposed to enter.
6108       (set-buffer gnus-group-buffer)
6109       (gnus-group-jump-to-group current-group)
6110       (setq target-group
6111             (or target-group
6112                 (if (eq gnus-keep-same-level 'best)
6113                     (gnus-summary-best-group gnus-newsgroup-name)
6114                   (gnus-summary-search-group backward gnus-keep-same-level))))
6115       (if (not target-group)
6116           ;; There are no further groups, so we return to the group
6117           ;; buffer.
6118           (progn
6119             (gnus-message 5 "Returning to the group buffer")
6120             (setq entered t)
6121             (when (gnus-buffer-live-p current-buffer)
6122               (set-buffer current-buffer)
6123               (gnus-summary-exit))
6124             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6125         ;; We try to enter the target group.
6126         (gnus-group-jump-to-group target-group)
6127         (let ((unreads (gnus-group-group-unread)))
6128           (if (and (or (eq t unreads)
6129                        (and unreads (not (zerop unreads))))
6130                    (gnus-summary-read-group
6131                     target-group nil no-article
6132                     (and (buffer-name current-buffer) current-buffer)
6133                     nil backward))
6134               (setq entered t)
6135             (setq current-group target-group
6136                   target-group nil)))))))
6137
6138 (defun gnus-summary-prev-group (&optional no-article)
6139   "Exit current newsgroup and then select previous unread newsgroup.
6140 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6141   (interactive "P")
6142   (gnus-summary-next-group no-article nil t))
6143
6144 ;; Walking around summary lines.
6145
6146 (defun gnus-summary-first-subject (&optional unread undownloaded)
6147   "Go to the first unread subject.
6148 If UNREAD is non-nil, go to the first unread article.
6149 Returns the article selected or nil if there are no unread articles."
6150   (interactive "P")
6151   (prog1
6152       (cond
6153        ;; Empty summary.
6154        ((null gnus-newsgroup-data)
6155         (gnus-message 3 "No articles in the group")
6156         nil)
6157        ;; Pick the first article.
6158        ((not unread)
6159         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6160         (gnus-data-number (car gnus-newsgroup-data)))
6161        ;; No unread articles.
6162        ((null gnus-newsgroup-unreads)
6163         (gnus-message 3 "No more unread articles")
6164         nil)
6165        ;; Find the first unread article.
6166        (t
6167         (let ((data gnus-newsgroup-data))
6168           (while (and data
6169                       (and (not (and undownloaded
6170                                      (eq gnus-undownloaded-mark
6171                                          (gnus-data-mark (car data)))))
6172                            (not (gnus-data-unread-p (car data)))))
6173             (setq data (cdr data)))
6174           (when data
6175             (goto-char (gnus-data-pos (car data)))
6176             (gnus-data-number (car data))))))
6177     (gnus-summary-position-point)))
6178
6179 (defun gnus-summary-next-subject (n &optional unread dont-display)
6180   "Go to next N'th summary line.
6181 If N is negative, go to the previous N'th subject line.
6182 If UNREAD is non-nil, only unread articles are selected.
6183 The difference between N and the actual number of steps taken is
6184 returned."
6185   (interactive "p")
6186   (let ((backward (< n 0))
6187         (n (abs n)))
6188     (while (and (> n 0)
6189                 (if backward
6190                     (gnus-summary-find-prev unread)
6191                   (gnus-summary-find-next unread)))
6192       (unless (zerop (setq n (1- n)))
6193         (gnus-summary-show-thread)))
6194     (when (/= 0 n)
6195       (gnus-message 7 "No more%s articles"
6196                     (if unread " unread" "")))
6197     (unless dont-display
6198       (gnus-summary-recenter)
6199       (gnus-summary-position-point))
6200     n))
6201
6202 (defun gnus-summary-next-unread-subject (n)
6203   "Go to next N'th unread summary line."
6204   (interactive "p")
6205   (gnus-summary-next-subject n t))
6206
6207 (defun gnus-summary-prev-subject (n &optional unread)
6208   "Go to previous N'th summary line.
6209 If optional argument UNREAD is non-nil, only unread article is selected."
6210   (interactive "p")
6211   (gnus-summary-next-subject (- n) unread))
6212
6213 (defun gnus-summary-prev-unread-subject (n)
6214   "Go to previous N'th unread summary line."
6215   (interactive "p")
6216   (gnus-summary-next-subject (- n) t))
6217
6218 (defun gnus-summary-goto-subject (article &optional force silent)
6219   "Go the subject line of ARTICLE.
6220 If FORCE, also allow jumping to articles not currently shown."
6221   (interactive "nArticle number: ")
6222   (let ((b (point))
6223         (data (gnus-data-find article)))
6224     ;; We read in the article if we have to.
6225     (and (not data)
6226          force
6227          (gnus-summary-insert-subject
6228           article
6229           (if (or (numberp force) (vectorp force)) force)
6230           t)
6231          (setq data (gnus-data-find article)))
6232     (goto-char b)
6233     (if (not data)
6234         (progn
6235           (unless silent
6236             (gnus-message 3 "Can't find article %d" article))
6237           nil)
6238       (let ((pt (gnus-data-pos data)))
6239         (goto-char pt)
6240         (gnus-summary-set-article-display-arrow pt))
6241       (gnus-summary-position-point)
6242       article)))
6243
6244 ;; Walking around summary lines with displaying articles.
6245
6246 (defun gnus-summary-expand-window (&optional arg)
6247   "Make the summary buffer take up the entire Emacs frame.
6248 Given a prefix, will force an `article' buffer configuration."
6249   (interactive "P")
6250   (if arg
6251       (gnus-configure-windows 'article 'force)
6252     (gnus-configure-windows 'summary 'force)))
6253
6254 (defun gnus-summary-display-article (article &optional all-header)
6255   "Display ARTICLE in article buffer."
6256   (when (gnus-buffer-live-p gnus-article-buffer)
6257     (with-current-buffer gnus-article-buffer
6258       (set-buffer-multibyte t)))
6259   (gnus-set-global-variables)
6260   (when (gnus-buffer-live-p gnus-article-buffer)
6261     (with-current-buffer gnus-article-buffer
6262       (setq gnus-article-charset gnus-newsgroup-charset)
6263       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6264   (if (null article)
6265       nil
6266     (prog1
6267         (if gnus-summary-display-article-function
6268             (funcall gnus-summary-display-article-function article all-header)
6269           (gnus-article-prepare article all-header))
6270       (with-current-buffer gnus-article-buffer
6271         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6272              nil))
6273       (gnus-run-hooks 'gnus-select-article-hook)
6274       (when (and gnus-current-article
6275                  (not (zerop gnus-current-article)))
6276         (gnus-summary-goto-subject gnus-current-article))
6277       (gnus-summary-recenter)
6278       (when (and gnus-use-trees gnus-show-threads)
6279         (gnus-possibly-generate-tree article)
6280         (gnus-highlight-selected-tree article))
6281       ;; Successfully display article.
6282       (gnus-article-set-window-start
6283        (cdr (assq article gnus-newsgroup-bookmarks))))))
6284
6285 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6286   "Select the current article.
6287 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6288 non-nil, the article will be re-fetched even if it already present in
6289 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6290 be displayed."
6291   ;; Make sure we are in the summary buffer to work around bbdb bug.
6292   (unless (eq major-mode 'gnus-summary-mode)
6293     (set-buffer gnus-summary-buffer))
6294   (let ((article (or article (gnus-summary-article-number)))
6295         (all-headers (not (not all-headers))) ;Must be T or NIL.
6296         gnus-summary-display-article-function)
6297     (and (not pseudo)
6298          (gnus-summary-article-pseudo-p article)
6299          (error "This is a pseudo-article"))
6300     (save-excursion
6301       (set-buffer gnus-summary-buffer)
6302       (if (or (and gnus-single-article-buffer
6303                    (or (null gnus-current-article)
6304                        (null gnus-article-current)
6305                        (null (get-buffer gnus-article-buffer))
6306                        (not (eq article (cdr gnus-article-current)))
6307                        (not (equal (car gnus-article-current)
6308                                    gnus-newsgroup-name))))
6309               (and (not gnus-single-article-buffer)
6310                    (or (null gnus-current-article)
6311                        (not (eq gnus-current-article article))))
6312               force)
6313           ;; The requested article is different from the current article.
6314           (progn
6315             (gnus-summary-display-article article all-headers)
6316             (when (or all-headers gnus-show-all-headers)
6317               (gnus-article-show-all-headers))
6318             (gnus-article-set-window-start
6319              (cdr (assq article gnus-newsgroup-bookmarks)))
6320             article)
6321         (when (or all-headers gnus-show-all-headers)
6322           (gnus-article-show-all-headers))
6323         'old))))
6324
6325 (defun gnus-summary-force-verify-and-decrypt ()
6326   (interactive)
6327   (let ((mm-verify-option 'known)
6328         (mm-decrypt-option 'known))
6329     (gnus-summary-select-article nil 'force)))
6330
6331 (defun gnus-summary-set-current-mark (&optional current-mark)
6332   "Obsolete function."
6333   nil)
6334
6335 (defun gnus-summary-next-article (&optional unread subject backward push)
6336   "Select the next article.
6337 If UNREAD, only unread articles are selected.
6338 If SUBJECT, only articles with SUBJECT are selected.
6339 If BACKWARD, the previous article is selected instead of the next."
6340   (interactive "P")
6341   (cond
6342    ;; Is there such an article?
6343    ((and (gnus-summary-search-forward unread subject backward)
6344          (or (gnus-summary-display-article (gnus-summary-article-number))
6345              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6346     (gnus-summary-position-point))
6347    ;; If not, we try the first unread, if that is wanted.
6348    ((and subject
6349          gnus-auto-select-same
6350          (gnus-summary-first-unread-article))
6351     (gnus-summary-position-point)
6352     (gnus-message 6 "Wrapped"))
6353    ;; Try to get next/previous article not displayed in this group.
6354    ((and gnus-auto-extend-newsgroup
6355          (not unread) (not subject))
6356     (gnus-summary-goto-article
6357      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6358      nil (count-lines (point-min) (point))))
6359    ;; Go to next/previous group.
6360    (t
6361     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6362       (gnus-summary-jump-to-group gnus-newsgroup-name))
6363     (let ((cmd last-command-char)
6364           (point
6365            (save-excursion
6366              (set-buffer gnus-group-buffer)
6367              (point)))
6368           (group
6369            (if (eq gnus-keep-same-level 'best)
6370                (gnus-summary-best-group gnus-newsgroup-name)
6371              (gnus-summary-search-group backward gnus-keep-same-level))))
6372       ;; For some reason, the group window gets selected.  We change
6373       ;; it back.
6374       (select-window (get-buffer-window (current-buffer)))
6375       ;; Select next unread newsgroup automagically.
6376       (cond
6377        ((or (not gnus-auto-select-next)
6378             (not cmd))
6379         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6380        ((or (eq gnus-auto-select-next 'quietly)
6381             (and (eq gnus-auto-select-next 'slightly-quietly)
6382                  push)
6383             (and (eq gnus-auto-select-next 'almost-quietly)
6384                  (gnus-summary-last-article-p)))
6385         ;; Select quietly.
6386         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6387             (gnus-summary-exit)
6388           (gnus-message 7 "No more%s articles (%s)..."
6389                         (if unread " unread" "")
6390                         (if group (concat "selecting " group)
6391                           "exiting"))
6392           (gnus-summary-next-group nil group backward)))
6393        (t
6394         (when (gnus-key-press-event-p last-input-event)
6395           (gnus-summary-walk-group-buffer
6396            gnus-newsgroup-name cmd unread backward point))))))))
6397
6398 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6399   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6400                       (?\C-p (gnus-group-prev-unread-group 1))))
6401         (cursor-in-echo-area t)
6402         keve key group ended)
6403     (save-excursion
6404       (set-buffer gnus-group-buffer)
6405       (goto-char start)
6406       (setq group
6407             (if (eq gnus-keep-same-level 'best)
6408                 (gnus-summary-best-group gnus-newsgroup-name)
6409               (gnus-summary-search-group backward gnus-keep-same-level))))
6410     (while (not ended)
6411       (gnus-message
6412        5 "No more%s articles%s" (if unread " unread" "")
6413        (if (and group
6414                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6415            (format " (Type %s for %s [%s])"
6416                    (single-key-description cmd) group
6417                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6418          (format " (Type %s to exit %s)"
6419                  (single-key-description cmd)
6420                  gnus-newsgroup-name)))
6421       ;; Confirm auto selection.
6422       (setq key (car (setq keve (gnus-read-event-char))))
6423       (setq ended t)
6424       (cond
6425        ((assq key keystrokes)
6426         (let ((obuf (current-buffer)))
6427           (switch-to-buffer gnus-group-buffer)
6428           (when group
6429             (gnus-group-jump-to-group group))
6430           (eval (cadr (assq key keystrokes)))
6431           (setq group (gnus-group-group-name))
6432           (switch-to-buffer obuf))
6433         (setq ended nil))
6434        ((equal key cmd)
6435         (if (or (not group)
6436                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6437             (gnus-summary-exit)
6438           (gnus-summary-next-group nil group backward)))
6439        (t
6440         (push (cdr keve) unread-command-events))))))
6441
6442 (defun gnus-summary-next-unread-article ()
6443   "Select unread article after current one."
6444   (interactive)
6445   (gnus-summary-next-article
6446    (or (not (eq gnus-summary-goto-unread 'never))
6447        (gnus-summary-last-article-p (gnus-summary-article-number)))
6448    (and gnus-auto-select-same
6449         (gnus-summary-article-subject))))
6450
6451 (defun gnus-summary-prev-article (&optional unread subject)
6452   "Select the article after the current one.
6453 If UNREAD is non-nil, only unread articles are selected."
6454   (interactive "P")
6455   (gnus-summary-next-article unread subject t))
6456
6457 (defun gnus-summary-prev-unread-article ()
6458   "Select unread article before current one."
6459   (interactive)
6460   (gnus-summary-prev-article
6461    (or (not (eq gnus-summary-goto-unread 'never))
6462        (gnus-summary-first-article-p (gnus-summary-article-number)))
6463    (and gnus-auto-select-same
6464         (gnus-summary-article-subject))))
6465
6466 (defun gnus-summary-next-page (&optional lines circular)
6467   "Show next page of the selected article.
6468 If at the end of the current article, select the next article.
6469 LINES says how many lines should be scrolled up.
6470
6471 If CIRCULAR is non-nil, go to the start of the article instead of
6472 selecting the next article when reaching the end of the current
6473 article."
6474   (interactive "P")
6475   (setq gnus-summary-buffer (current-buffer))
6476   (gnus-set-global-variables)
6477   (let ((article (gnus-summary-article-number))
6478         (article-window (get-buffer-window gnus-article-buffer t))
6479         endp)
6480     ;; If the buffer is empty, we have no article.
6481     (unless article
6482       (error "No article to select"))
6483     (gnus-configure-windows 'article)
6484     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6485         (if (and (eq gnus-summary-goto-unread 'never)
6486                  (not (gnus-summary-last-article-p article)))
6487             (gnus-summary-next-article)
6488           (gnus-summary-next-unread-article))
6489       (if (or (null gnus-current-article)
6490               (null gnus-article-current)
6491               (/= article (cdr gnus-article-current))
6492               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6493           ;; Selected subject is different from current article's.
6494           (gnus-summary-display-article article)
6495         (when article-window
6496           (gnus-eval-in-buffer-window gnus-article-buffer
6497             (setq endp (gnus-article-next-page lines)))
6498           (when endp
6499             (cond (circular
6500                    (gnus-summary-beginning-of-article))
6501                   (lines
6502                    (gnus-message 3 "End of message"))
6503                   ((null lines)
6504                    (if (and (eq gnus-summary-goto-unread 'never)
6505                             (not (gnus-summary-last-article-p article)))
6506                        (gnus-summary-next-article)
6507                      (gnus-summary-next-unread-article))))))))
6508     (gnus-summary-recenter)
6509     (gnus-summary-position-point)))
6510
6511 (defun gnus-summary-prev-page (&optional lines move)
6512   "Show previous page of selected article.
6513 Argument LINES specifies lines to be scrolled down.
6514 If MOVE, move to the previous unread article if point is at
6515 the beginning of the buffer."
6516   (interactive "P")
6517   (let ((article (gnus-summary-article-number))
6518         (article-window (get-buffer-window gnus-article-buffer t))
6519         endp)
6520     (gnus-configure-windows 'article)
6521     (if (or (null gnus-current-article)
6522             (null gnus-article-current)
6523             (/= article (cdr gnus-article-current))
6524             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6525         ;; Selected subject is different from current article's.
6526         (gnus-summary-display-article article)
6527       (gnus-summary-recenter)
6528       (when article-window
6529         (gnus-eval-in-buffer-window gnus-article-buffer
6530           (setq endp (gnus-article-prev-page lines)))
6531         (when (and move endp)
6532           (cond (lines
6533                  (gnus-message 3 "Beginning of message"))
6534                 ((null lines)
6535                  (if (and (eq gnus-summary-goto-unread 'never)
6536                           (not (gnus-summary-first-article-p article)))
6537                      (gnus-summary-prev-article)
6538                    (gnus-summary-prev-unread-article))))))))
6539   (gnus-summary-position-point))
6540
6541 (defun gnus-summary-prev-page-or-article (&optional lines)
6542   "Show previous page of selected article.
6543 Argument LINES specifies lines to be scrolled down.
6544 If at the beginning of the article, go to the next article."
6545   (interactive "P")
6546   (gnus-summary-prev-page lines t))
6547
6548 (defun gnus-summary-scroll-up (lines)
6549   "Scroll up (or down) one line current article.
6550 Argument LINES specifies lines to be scrolled up (or down if negative)."
6551   (interactive "p")
6552   (gnus-configure-windows 'article)
6553   (gnus-summary-show-thread)
6554   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6555     (gnus-eval-in-buffer-window gnus-article-buffer
6556       (cond ((> lines 0)
6557              (when (gnus-article-next-page lines)
6558                (gnus-message 3 "End of message")))
6559             ((< lines 0)
6560              (gnus-article-prev-page (- lines))))))
6561   (gnus-summary-recenter)
6562   (gnus-summary-position-point))
6563
6564 (defun gnus-summary-scroll-down (lines)
6565   "Scroll down (or up) one line current article.
6566 Argument LINES specifies lines to be scrolled down (or up if negative)."
6567   (interactive "p")
6568   (gnus-summary-scroll-up (- lines)))
6569
6570 (defun gnus-summary-next-same-subject ()
6571   "Select next article which has the same subject as current one."
6572   (interactive)
6573   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6574
6575 (defun gnus-summary-prev-same-subject ()
6576   "Select previous article which has the same subject as current one."
6577   (interactive)
6578   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6579
6580 (defun gnus-summary-next-unread-same-subject ()
6581   "Select next unread article which has the same subject as current one."
6582   (interactive)
6583   (gnus-summary-next-article t (gnus-summary-article-subject)))
6584
6585 (defun gnus-summary-prev-unread-same-subject ()
6586   "Select previous unread article which has the same subject as current one."
6587   (interactive)
6588   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6589
6590 (defun gnus-summary-first-unread-article ()
6591   "Select the first unread article.
6592 Return nil if there are no unread articles."
6593   (interactive)
6594   (prog1
6595       (when (gnus-summary-first-subject t)
6596         (gnus-summary-show-thread)
6597         (gnus-summary-first-subject t)
6598         (gnus-summary-display-article (gnus-summary-article-number)))
6599     (gnus-summary-position-point)))
6600
6601 (defun gnus-summary-first-unread-subject ()
6602   "Place the point on the subject line of the first unread article.
6603 Return nil if there are no unread articles."
6604   (interactive)
6605   (prog1
6606       (when (gnus-summary-first-subject t)
6607         (gnus-summary-show-thread)
6608         (gnus-summary-first-subject t))
6609     (gnus-summary-position-point)))
6610
6611 (defun gnus-summary-first-article ()
6612   "Select the first article.
6613 Return nil if there are no articles."
6614   (interactive)
6615   (prog1
6616       (when (gnus-summary-first-subject)
6617         (gnus-summary-show-thread)
6618         (gnus-summary-first-subject)
6619         (gnus-summary-display-article (gnus-summary-article-number)))
6620     (gnus-summary-position-point)))
6621
6622 (defun gnus-summary-best-unread-article ()
6623   "Select the unread article with the highest score."
6624   (interactive)
6625   (let ((best -1000000)
6626         (data gnus-newsgroup-data)
6627         article score)
6628     (while data
6629       (and (gnus-data-unread-p (car data))
6630            (> (setq score
6631                     (gnus-summary-article-score (gnus-data-number (car data))))
6632               best)
6633            (setq best score
6634                  article (gnus-data-number (car data))))
6635       (setq data (cdr data)))
6636     (prog1
6637         (if article
6638             (gnus-summary-goto-article article)
6639           (error "No unread articles"))
6640       (gnus-summary-position-point))))
6641
6642 (defun gnus-summary-last-subject ()
6643   "Go to the last displayed subject line in the group."
6644   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6645     (when article
6646       (gnus-summary-goto-subject article))))
6647
6648 (defun gnus-summary-goto-article (article &optional all-headers force)
6649   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6650 If ALL-HEADERS is non-nil, no header lines are hidden.
6651 If FORCE, go to the article even if it isn't displayed.  If FORCE
6652 is a number, it is the line the article is to be displayed on."
6653   (interactive
6654    (list
6655     (completing-read
6656      "Article number or Message-ID: "
6657      (mapcar (lambda (number) (list (int-to-string number)))
6658              gnus-newsgroup-limit))
6659     current-prefix-arg
6660     t))
6661   (prog1
6662       (if (and (stringp article)
6663                (string-match "@" article))
6664           (gnus-summary-refer-article article)
6665         (when (stringp article)
6666           (setq article (string-to-number article)))
6667         (if (gnus-summary-goto-subject article force)
6668             (gnus-summary-display-article article all-headers)
6669           (gnus-message 4 "Couldn't go to article %s" article) nil))
6670     (gnus-summary-position-point)))
6671
6672 (defun gnus-summary-goto-last-article ()
6673   "Go to the previously read article."
6674   (interactive)
6675   (prog1
6676       (when gnus-last-article
6677         (gnus-summary-goto-article gnus-last-article nil t))
6678     (gnus-summary-position-point)))
6679
6680 (defun gnus-summary-pop-article (number)
6681   "Pop one article off the history and go to the previous.
6682 NUMBER articles will be popped off."
6683   (interactive "p")
6684   (let (to)
6685     (setq gnus-newsgroup-history
6686           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6687     (if to
6688         (gnus-summary-goto-article (car to) nil t)
6689       (error "Article history empty")))
6690   (gnus-summary-position-point))
6691
6692 ;; Summary commands and functions for limiting the summary buffer.
6693
6694 (defun gnus-summary-limit-to-articles (n)
6695   "Limit the summary buffer to the next N articles.
6696 If not given a prefix, use the process marked articles instead."
6697   (interactive "P")
6698   (prog1
6699       (let ((articles (gnus-summary-work-articles n)))
6700         (setq gnus-newsgroup-processable nil)
6701         (gnus-summary-limit articles))
6702     (gnus-summary-position-point)))
6703
6704 (defun gnus-summary-pop-limit (&optional total)
6705   "Restore the previous limit.
6706 If given a prefix, remove all limits."
6707   (interactive "P")
6708   (when total
6709     (setq gnus-newsgroup-limits
6710           (list (mapcar (lambda (h) (mail-header-number h))
6711                         gnus-newsgroup-headers))))
6712   (unless gnus-newsgroup-limits
6713     (error "No limit to pop"))
6714   (prog1
6715       (gnus-summary-limit nil 'pop)
6716     (gnus-summary-position-point)))
6717
6718 (defun gnus-summary-limit-to-subject (subject &optional header)
6719   "Limit the summary buffer to articles that have subjects that match a regexp."
6720   (interactive "sLimit to subject (regexp): ")
6721   (unless header
6722     (setq header "subject"))
6723   (when (not (equal "" subject))
6724     (prog1
6725         (let ((articles (gnus-summary-find-matching
6726                          (or header "subject") subject 'all)))
6727           (unless articles
6728             (error "Found no matches for \"%s\"" subject))
6729           (gnus-summary-limit articles))
6730       (gnus-summary-position-point))))
6731
6732 (defun gnus-summary-limit-to-author (from)
6733   "Limit the summary buffer to articles that have authors that match a regexp."
6734   (interactive "sLimit to author (regexp): ")
6735   (gnus-summary-limit-to-subject from "from"))
6736
6737 (defun gnus-summary-limit-to-age (age &optional younger-p)
6738   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6739 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6740 articles that are younger than AGE days."
6741   (interactive
6742    (let ((younger current-prefix-arg)
6743          (days-got nil)
6744          days)
6745      (while (not days-got)
6746        (setq days (if younger
6747                       (read-string "Limit to articles within (in days): ")
6748                     (read-string "Limit to articles older than (in days): ")))
6749        (when (> (length days) 0)
6750          (setq days (read days)))
6751        (if (numberp days)
6752            (setq days-got t)
6753          (message "Please enter a number.")
6754          (sleep-for 1)))
6755      (list days younger)))
6756   (prog1
6757       (let ((data gnus-newsgroup-data)
6758             (cutoff (days-to-time age))
6759             articles d date is-younger)
6760         (while (setq d (pop data))
6761           (when (and (vectorp (gnus-data-header d))
6762                      (setq date (mail-header-date (gnus-data-header d))))
6763             (setq is-younger (time-less-p
6764                               (time-since (condition-case ()
6765                                               (date-to-time date)
6766                                             (error '(0 0))))
6767                               cutoff))
6768             (when (if younger-p
6769                       is-younger
6770                     (not is-younger))
6771               (push (gnus-data-number d) articles))))
6772         (gnus-summary-limit (nreverse articles)))
6773     (gnus-summary-position-point)))
6774
6775 (defun gnus-summary-limit-to-extra (header regexp)
6776   "Limit the summary buffer to articles that match an 'extra' header."
6777   (interactive
6778    (let ((header
6779           (intern
6780            (gnus-completing-read
6781             (symbol-name (car gnus-extra-headers))
6782             "Limit extra header:"
6783             (mapcar (lambda (x)
6784                       (cons (symbol-name x) x))
6785                     gnus-extra-headers)
6786             nil
6787             t))))
6788      (list header
6789            (read-string (format "Limit to header %s (regexp): " header)))))
6790   (when (not (equal "" regexp))
6791     (prog1
6792         (let ((articles (gnus-summary-find-matching
6793                          (cons 'extra header) regexp 'all)))
6794           (unless articles
6795             (error "Found no matches for \"%s\"" regexp))
6796           (gnus-summary-limit articles))
6797       (gnus-summary-position-point))))
6798
6799 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6800 (make-obsolete
6801  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6802
6803 (defun gnus-summary-limit-to-unread (&optional all)
6804   "Limit the summary buffer to articles that are not marked as read.
6805 If ALL is non-nil, limit strictly to unread articles."
6806   (interactive "P")
6807   (if all
6808       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6809     (gnus-summary-limit-to-marks
6810      ;; Concat all the marks that say that an article is read and have
6811      ;; those removed.
6812      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6813            gnus-killed-mark gnus-kill-file-mark
6814            gnus-low-score-mark gnus-expirable-mark
6815            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6816            gnus-duplicate-mark gnus-souped-mark)
6817      'reverse)))
6818
6819 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6820 (make-obsolete 'gnus-summary-delete-marked-with
6821                'gnus-summary-limit-exlude-marks)
6822
6823 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6824   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6825 If REVERSE, limit the summary buffer to articles that are marked
6826 with MARKS.  MARKS can either be a string of marks or a list of marks.
6827 Returns how many articles were removed."
6828   (interactive "sMarks: ")
6829   (gnus-summary-limit-to-marks marks t))
6830
6831 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6832   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6833 If REVERSE (the prefix), limit the summary buffer to articles that are
6834 not marked with MARKS.  MARKS can either be a string of marks or a
6835 list of marks.
6836 Returns how many articles were removed."
6837   (interactive "sMarks: \nP")
6838   (prog1
6839       (let ((data gnus-newsgroup-data)
6840             (marks (if (listp marks) marks
6841                      (append marks nil))) ; Transform to list.
6842             articles)
6843         (while data
6844           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6845                   (memq (gnus-data-mark (car data)) marks))
6846             (push (gnus-data-number (car data)) articles))
6847           (setq data (cdr data)))
6848         (gnus-summary-limit articles))
6849     (gnus-summary-position-point)))
6850
6851 (defun gnus-summary-limit-to-score (&optional score)
6852   "Limit to articles with score at or above SCORE."
6853   (interactive "P")
6854   (setq score (if score
6855                   (prefix-numeric-value score)
6856                 (or gnus-summary-default-score 0)))
6857   (let ((data gnus-newsgroup-data)
6858         articles)
6859     (while data
6860       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6861                 score)
6862         (push (gnus-data-number (car data)) articles))
6863       (setq data (cdr data)))
6864     (prog1
6865         (gnus-summary-limit articles)
6866       (gnus-summary-position-point))))
6867
6868 (defun gnus-summary-limit-include-thread (id)
6869   "Display all the hidden articles that is in the thread with ID in it.
6870 When called interactively, ID is the Message-ID of the current
6871 article."
6872   (interactive (list (mail-header-id (gnus-summary-article-header))))
6873   (let ((articles (gnus-articles-in-thread
6874                    (gnus-id-to-thread (gnus-root-id id)))))
6875     (prog1
6876         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6877       (gnus-summary-limit-include-matching-articles
6878        "subject"
6879        (regexp-quote (gnus-simplify-subject-re
6880                       (mail-header-subject (gnus-id-to-header id)))))
6881       (gnus-summary-position-point))))
6882
6883 (defun gnus-summary-limit-include-matching-articles (header regexp)
6884   "Display all the hidden articles that have HEADERs that match REGEXP."
6885   (interactive (list (read-string "Match on header: ")
6886                      (read-string "Regexp: ")))
6887   (let ((articles (gnus-find-matching-articles header regexp)))
6888     (prog1
6889         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6890       (gnus-summary-position-point))))
6891
6892 (defun gnus-summary-limit-include-dormant ()
6893   "Display all the hidden articles that are marked as dormant.
6894 Note that this command only works on a subset of the articles currently
6895 fetched for this group."
6896   (interactive)
6897   (unless gnus-newsgroup-dormant
6898     (error "There are no dormant articles in this group"))
6899   (prog1
6900       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6901     (gnus-summary-position-point)))
6902
6903 (defun gnus-summary-limit-exclude-dormant ()
6904   "Hide all dormant articles."
6905   (interactive)
6906   (prog1
6907       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6908     (gnus-summary-position-point)))
6909
6910 (defun gnus-summary-limit-exclude-childless-dormant ()
6911   "Hide all dormant articles that have no children."
6912   (interactive)
6913   (let ((data (gnus-data-list t))
6914         articles d children)
6915     ;; Find all articles that are either not dormant or have
6916     ;; children.
6917     (while (setq d (pop data))
6918       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6919                 (and (setq children
6920                            (gnus-article-children (gnus-data-number d)))
6921                      (let (found)
6922                        (while children
6923                          (when (memq (car children) articles)
6924                            (setq children nil
6925                                  found t))
6926                          (pop children))
6927                        found)))
6928         (push (gnus-data-number d) articles)))
6929     ;; Do the limiting.
6930     (prog1
6931         (gnus-summary-limit articles)
6932       (gnus-summary-position-point))))
6933
6934 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6935   "Mark all unread excluded articles as read.
6936 If ALL, mark even excluded ticked and dormants as read."
6937   (interactive "P")
6938   (let ((articles (gnus-sorted-complement
6939                    (sort
6940                     (mapcar (lambda (h) (mail-header-number h))
6941                             gnus-newsgroup-headers)
6942                     '<)
6943                    (sort gnus-newsgroup-limit '<)))
6944         article)
6945     (setq gnus-newsgroup-unreads
6946           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6947     (if all
6948         (setq gnus-newsgroup-dormant nil
6949               gnus-newsgroup-marked nil
6950               gnus-newsgroup-reads
6951               (nconc
6952                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6953                gnus-newsgroup-reads))
6954       (while (setq article (pop articles))
6955         (unless (or (memq article gnus-newsgroup-dormant)
6956                     (memq article gnus-newsgroup-marked))
6957           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6958
6959 (defun gnus-summary-limit (articles &optional pop)
6960   (if pop
6961       ;; We pop the previous limit off the stack and use that.
6962       (setq articles (car gnus-newsgroup-limits)
6963             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6964     ;; We use the new limit, so we push the old limit on the stack.
6965     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6966   ;; Set the limit.
6967   (setq gnus-newsgroup-limit articles)
6968   (let ((total (length gnus-newsgroup-data))
6969         (data (gnus-data-find-list (gnus-summary-article-number)))
6970         (gnus-summary-mark-below nil)   ; Inhibit this.
6971         found)
6972     ;; This will do all the work of generating the new summary buffer
6973     ;; according to the new limit.
6974     (gnus-summary-prepare)
6975     ;; Hide any threads, possibly.
6976     (and gnus-show-threads
6977          gnus-thread-hide-subtree
6978          (gnus-summary-hide-all-threads))
6979     ;; Try to return to the article you were at, or one in the
6980     ;; neighborhood.
6981     (when data
6982       ;; We try to find some article after the current one.
6983       (while data
6984         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6985           (setq data nil
6986                 found t))
6987         (setq data (cdr data))))
6988     (unless found
6989       ;; If there is no data, that means that we were after the last
6990       ;; article.  The same goes when we can't find any articles
6991       ;; after the current one.
6992       (goto-char (point-max))
6993       (gnus-summary-find-prev))
6994     (gnus-set-mode-line 'summary)
6995     ;; We return how many articles were removed from the summary
6996     ;; buffer as a result of the new limit.
6997     (- total (length gnus-newsgroup-data))))
6998
6999 (defsubst gnus-invisible-cut-children (threads)
7000   (let ((num 0))
7001     (while threads
7002       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7003         (incf num))
7004       (pop threads))
7005     (< num 2)))
7006
7007 (defsubst gnus-cut-thread (thread)
7008   "Go forwards in the thread until we find an article that we want to display."
7009   (when (or (eq gnus-fetch-old-headers 'some)
7010             (eq gnus-fetch-old-headers 'invisible)
7011             (numberp gnus-fetch-old-headers)
7012             (eq gnus-build-sparse-threads 'some)
7013             (eq gnus-build-sparse-threads 'more))
7014     ;; Deal with old-fetched headers and sparse threads.
7015     (while (and
7016             thread
7017             (or
7018              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7019              (gnus-summary-article-ancient-p
7020               (mail-header-number (car thread))))
7021             (if (or (<= (length (cdr thread)) 1)
7022                     (eq gnus-fetch-old-headers 'invisible))
7023                 (setq gnus-newsgroup-limit
7024                       (delq (mail-header-number (car thread))
7025                             gnus-newsgroup-limit)
7026                       thread (cadr thread))
7027               (when (gnus-invisible-cut-children (cdr thread))
7028                 (let ((th (cdr thread)))
7029                   (while th
7030                     (if (memq (mail-header-number (caar th))
7031                               gnus-newsgroup-limit)
7032                         (setq thread (car th)
7033                               th nil)
7034                       (setq th (cdr th))))))))))
7035   thread)
7036
7037 (defun gnus-cut-threads (threads)
7038   "Cut off all uninteresting articles from the beginning of threads."
7039   (when (or (eq gnus-fetch-old-headers 'some)
7040             (eq gnus-fetch-old-headers 'invisible)
7041             (numberp gnus-fetch-old-headers)
7042             (eq gnus-build-sparse-threads 'some)
7043             (eq gnus-build-sparse-threads 'more))
7044     (let ((th threads))
7045       (while th
7046         (setcar th (gnus-cut-thread (car th)))
7047         (setq th (cdr th)))))
7048   ;; Remove nixed out threads.
7049   (delq nil threads))
7050
7051 (defun gnus-summary-initial-limit (&optional show-if-empty)
7052   "Figure out what the initial limit is supposed to be on group entry.
7053 This entails weeding out unwanted dormants, low-scored articles,
7054 fetch-old-headers verbiage, and so on."
7055   ;; Most groups have nothing to remove.
7056   (if (or gnus-inhibit-limiting
7057           (and (null gnus-newsgroup-dormant)
7058                (not (eq gnus-fetch-old-headers 'some))
7059                (not (numberp gnus-fetch-old-headers))
7060                (not (eq gnus-fetch-old-headers 'invisible))
7061                (null gnus-summary-expunge-below)
7062                (not (eq gnus-build-sparse-threads 'some))
7063                (not (eq gnus-build-sparse-threads 'more))
7064                (null gnus-thread-expunge-below)
7065                (not gnus-use-nocem)))
7066       ()                                ; Do nothing.
7067     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7068     (setq gnus-newsgroup-limit nil)
7069     (mapatoms
7070      (lambda (node)
7071        (unless (car (symbol-value node))
7072          ;; These threads have no parents -- they are roots.
7073          (let ((nodes (cdr (symbol-value node)))
7074                thread)
7075            (while nodes
7076              (if (and gnus-thread-expunge-below
7077                       (< (gnus-thread-total-score (car nodes))
7078                          gnus-thread-expunge-below))
7079                  (gnus-expunge-thread (pop nodes))
7080                (setq thread (pop nodes))
7081                (gnus-summary-limit-children thread))))))
7082      gnus-newsgroup-dependencies)
7083     ;; If this limitation resulted in an empty group, we might
7084     ;; pop the previous limit and use it instead.
7085     (when (and (not gnus-newsgroup-limit)
7086                show-if-empty)
7087       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7088     gnus-newsgroup-limit))
7089
7090 (defun gnus-summary-limit-children (thread)
7091   "Return 1 if this subthread is visible and 0 if it is not."
7092   ;; First we get the number of visible children to this thread.  This
7093   ;; is done by recursing down the thread using this function, so this
7094   ;; will really go down to a leaf article first, before slowly
7095   ;; working its way up towards the root.
7096   (when thread
7097     (let ((children
7098            (if (cdr thread)
7099                (apply '+ (mapcar 'gnus-summary-limit-children
7100                                  (cdr thread)))
7101              0))
7102           (number (mail-header-number (car thread)))
7103           score)
7104       (if (and
7105            (not (memq number gnus-newsgroup-marked))
7106            (or
7107             ;; If this article is dormant and has absolutely no visible
7108             ;; children, then this article isn't visible.
7109             (and (memq number gnus-newsgroup-dormant)
7110                  (zerop children))
7111             ;; If this is "fetch-old-headered" and there is no
7112             ;; visible children, then we don't want this article.
7113             (and (or (eq gnus-fetch-old-headers 'some)
7114                      (numberp gnus-fetch-old-headers))
7115                  (gnus-summary-article-ancient-p number)
7116                  (zerop children))
7117             ;; If this is "fetch-old-headered" and `invisible', then
7118             ;; we don't want this article.
7119             (and (eq gnus-fetch-old-headers 'invisible)
7120                  (gnus-summary-article-ancient-p number))
7121             ;; If this is a sparsely inserted article with no children,
7122             ;; we don't want it.
7123             (and (eq gnus-build-sparse-threads 'some)
7124                  (gnus-summary-article-sparse-p number)
7125                  (zerop children))
7126             ;; If we use expunging, and this article is really
7127             ;; low-scored, then we don't want this article.
7128             (when (and gnus-summary-expunge-below
7129                        (< (setq score
7130                                 (or (cdr (assq number gnus-newsgroup-scored))
7131                                     gnus-summary-default-score))
7132                           gnus-summary-expunge-below))
7133               ;; We increase the expunge-tally here, but that has
7134               ;; nothing to do with the limits, really.
7135               (incf gnus-newsgroup-expunged-tally)
7136               ;; We also mark as read here, if that's wanted.
7137               (when (and gnus-summary-mark-below
7138                          (< score gnus-summary-mark-below))
7139                 (setq gnus-newsgroup-unreads
7140                       (delq number gnus-newsgroup-unreads))
7141                 (if gnus-newsgroup-auto-expire
7142                     (push number gnus-newsgroup-expirable)
7143                   (push (cons number gnus-low-score-mark)
7144                         gnus-newsgroup-reads)))
7145               t)
7146             ;; Check NoCeM things.
7147             (if (and gnus-use-nocem
7148                      (gnus-nocem-unwanted-article-p
7149                       (mail-header-id (car thread))))
7150                 (progn
7151                   (setq gnus-newsgroup-unreads
7152                         (delq number gnus-newsgroup-unreads))
7153                   t))))
7154           ;; Nope, invisible article.
7155           0
7156         ;; Ok, this article is to be visible, so we add it to the limit
7157         ;; and return 1.
7158         (push number gnus-newsgroup-limit)
7159         1))))
7160
7161 (defun gnus-expunge-thread (thread)
7162   "Mark all articles in THREAD as read."
7163   (let* ((number (mail-header-number (car thread))))
7164     (incf gnus-newsgroup-expunged-tally)
7165     ;; We also mark as read here, if that's wanted.
7166     (setq gnus-newsgroup-unreads
7167           (delq number gnus-newsgroup-unreads))
7168     (if gnus-newsgroup-auto-expire
7169         (push number gnus-newsgroup-expirable)
7170       (push (cons number gnus-low-score-mark)
7171             gnus-newsgroup-reads)))
7172   ;; Go recursively through all subthreads.
7173   (mapcar 'gnus-expunge-thread (cdr thread)))
7174
7175 ;; Summary article oriented commands
7176
7177 (defun gnus-summary-refer-parent-article (n)
7178   "Refer parent article N times.
7179 If N is negative, go to ancestor -N instead.
7180 The difference between N and the number of articles fetched is returned."
7181   (interactive "p")
7182   (let ((skip 1)
7183         error header ref)
7184     (when (not (natnump n))
7185       (setq skip (abs n)
7186             n 1))
7187     (while (and (> n 0)
7188                 (not error))
7189       (setq header (gnus-summary-article-header))
7190       (if (and (eq (mail-header-number header)
7191                    (cdr gnus-article-current))
7192                (equal gnus-newsgroup-name
7193                       (car gnus-article-current)))
7194           ;; If we try to find the parent of the currently
7195           ;; displayed article, then we take a look at the actual
7196           ;; References header, since this is slightly more
7197           ;; reliable than the References field we got from the
7198           ;; server.
7199           (save-excursion
7200             (set-buffer gnus-original-article-buffer)
7201             (nnheader-narrow-to-headers)
7202             (unless (setq ref (message-fetch-field "references"))
7203               (setq ref (message-fetch-field "in-reply-to")))
7204             (widen))
7205         (setq ref
7206               ;; It's not the current article, so we take a bet on
7207               ;; the value we got from the server.
7208               (mail-header-references header)))
7209       (if (and ref
7210                (not (equal ref "")))
7211           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7212             (gnus-message 1 "Couldn't find parent"))
7213         (gnus-message 1 "No references in article %d"
7214                       (gnus-summary-article-number))
7215         (setq error t))
7216       (decf n))
7217     (gnus-summary-position-point)
7218     n))
7219
7220 (defun gnus-summary-refer-references ()
7221   "Fetch all articles mentioned in the References header.
7222 Return the number of articles fetched."
7223   (interactive)
7224   (let ((ref (mail-header-references (gnus-summary-article-header)))
7225         (current (gnus-summary-article-number))
7226         (n 0))
7227     (if (or (not ref)
7228             (equal ref ""))
7229         (error "No References in the current article")
7230       ;; For each Message-ID in the References header...
7231       (while (string-match "<[^>]*>" ref)
7232         (incf n)
7233         ;; ... fetch that article.
7234         (gnus-summary-refer-article
7235          (prog1 (match-string 0 ref)
7236            (setq ref (substring ref (match-end 0))))))
7237       (gnus-summary-goto-subject current)
7238       (gnus-summary-position-point)
7239       n)))
7240
7241 (defun gnus-summary-refer-thread (&optional limit)
7242   "Fetch all articles in the current thread.
7243 If LIMIT (the numerical prefix), fetch that many old headers instead
7244 of what's specified by the `gnus-refer-thread-limit' variable."
7245   (interactive "P")
7246   (let ((id (mail-header-id (gnus-summary-article-header)))
7247         (limit (if limit (prefix-numeric-value limit)
7248                  gnus-refer-thread-limit)))
7249     ;; We want to fetch LIMIT *old* headers, but we also have to
7250     ;; re-fetch all the headers in the current buffer, because many of
7251     ;; them may be undisplayed.  So we adjust LIMIT.
7252     (when (numberp limit)
7253       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7254     (unless (eq gnus-fetch-old-headers 'invisible)
7255       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7256       ;; Retrieve the headers and read them in.
7257       (if (eq (gnus-retrieve-headers
7258                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7259               'nov)
7260           (gnus-build-all-threads)
7261         (error "Can't fetch thread from backends that don't support NOV"))
7262       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7263     (gnus-summary-limit-include-thread id)))
7264
7265 (defun gnus-summary-refer-article (message-id)
7266   "Fetch an article specified by MESSAGE-ID."
7267   (interactive "sMessage-ID: ")
7268   (when (and (stringp message-id)
7269              (not (zerop (length message-id))))
7270     ;; Construct the correct Message-ID if necessary.
7271     ;; Suggested by tale@pawl.rpi.edu.
7272     (unless (string-match "^<" message-id)
7273       (setq message-id (concat "<" message-id)))
7274     (unless (string-match ">$" message-id)
7275       (setq message-id (concat message-id ">")))
7276     (let* ((header (gnus-id-to-header message-id))
7277            (sparse (and header
7278                         (gnus-summary-article-sparse-p
7279                          (mail-header-number header))
7280                         (memq (mail-header-number header)
7281                               gnus-newsgroup-limit)))
7282            number)
7283       (cond
7284        ;; If the article is present in the buffer we just go to it.
7285        ((and header
7286              (or (not (gnus-summary-article-sparse-p
7287                        (mail-header-number header)))
7288                  sparse))
7289         (prog1
7290             (gnus-summary-goto-article
7291              (mail-header-number header) nil t)
7292           (when sparse
7293             (gnus-summary-update-article (mail-header-number header)))))
7294        (t
7295         ;; We fetch the article.
7296         (catch 'found
7297           (dolist (gnus-override-method (gnus-refer-article-methods))
7298             (gnus-check-server gnus-override-method)
7299             ;; Fetch the header, and display the article.
7300             (when (setq number (gnus-summary-insert-subject message-id))
7301               (gnus-summary-select-article nil nil nil number)
7302               (throw 'found t)))
7303           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7304
7305 (defun gnus-refer-article-methods ()
7306   "Return a list of referrable methods."
7307   (cond
7308    ;; No method, so we default to current and native.
7309    ((null gnus-refer-article-method)
7310     (list gnus-current-select-method gnus-select-method))
7311    ;; Current.
7312    ((eq 'current gnus-refer-article-method)
7313     (list gnus-current-select-method))
7314    ;; List of select methods.
7315    ((not (and (symbolp (car gnus-refer-article-method))
7316               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7317     (let (out)
7318       (dolist (method gnus-refer-article-method)
7319         (push (if (eq 'current method)
7320                   gnus-current-select-method
7321                 method)
7322               out))
7323       (nreverse out)))
7324    ;; One single select method.
7325    (t
7326     (list gnus-refer-article-method))))
7327
7328 (defun gnus-summary-edit-parameters ()
7329   "Edit the group parameters of the current group."
7330   (interactive)
7331   (gnus-group-edit-group gnus-newsgroup-name 'params))
7332
7333 (defun gnus-summary-customize-parameters ()
7334   "Customize the group parameters of the current group."
7335   (interactive)
7336   (gnus-group-customize gnus-newsgroup-name))
7337
7338 (defun gnus-summary-enter-digest-group (&optional force)
7339   "Enter an nndoc group based on the current article.
7340 If FORCE, force a digest interpretation.  If not, try
7341 to guess what the document format is."
7342   (interactive "P")
7343   (let ((conf gnus-current-window-configuration))
7344     (save-excursion
7345       (gnus-summary-select-article))
7346     (setq gnus-current-window-configuration conf)
7347     (let* ((name (format "%s-%d"
7348                          (gnus-group-prefixed-name
7349                           gnus-newsgroup-name (list 'nndoc ""))
7350                          (save-excursion
7351                            (set-buffer gnus-summary-buffer)
7352                            gnus-current-article)))
7353            (ogroup gnus-newsgroup-name)
7354            (params (append (gnus-info-params (gnus-get-info ogroup))
7355                            (list (cons 'to-group ogroup))
7356                            (list (cons 'save-article-group ogroup))))
7357            (case-fold-search t)
7358            (buf (current-buffer))
7359            dig to-address)
7360       (save-excursion
7361         (set-buffer gnus-original-article-buffer)
7362         ;; Have the digest group inherit the main mail address of
7363         ;; the parent article.
7364         (when (setq to-address (or (message-fetch-field "reply-to")
7365                                    (message-fetch-field "from")))
7366           (setq params (append
7367                         (list (cons 'to-address
7368                                     (funcall gnus-decode-encoded-word-function
7369                                              to-address))))))
7370         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7371         (insert-buffer-substring gnus-original-article-buffer)
7372         ;; Remove lines that may lead nndoc to misinterpret the
7373         ;; document type.
7374         (narrow-to-region
7375          (goto-char (point-min))
7376          (or (search-forward "\n\n" nil t) (point)))
7377         (goto-char (point-min))
7378         (delete-matching-lines "^Path:\\|^From ")
7379         (widen))
7380       (unwind-protect
7381           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7382                     (gnus-newsgroup-ephemeral-ignored-charsets
7383                      gnus-newsgroup-ignored-charsets))
7384                 (gnus-group-read-ephemeral-group
7385                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7386                               (nndoc-article-type
7387                                ,(if force 'mbox 'guess))) t))
7388               ;; Make all postings to this group go to the parent group.
7389               (nconc (gnus-info-params (gnus-get-info name))
7390                      params)
7391             ;; Couldn't select this doc group.
7392             (switch-to-buffer buf)
7393             (gnus-set-global-variables)
7394             (gnus-configure-windows 'summary)
7395             (gnus-message 3 "Article couldn't be entered?"))
7396         (kill-buffer dig)))))
7397
7398 (defun gnus-summary-read-document (n)
7399   "Open a new group based on the current article(s).
7400 This will allow you to read digests and other similar
7401 documents as newsgroups.
7402 Obeys the standard process/prefix convention."
7403   (interactive "P")
7404   (let* ((articles (gnus-summary-work-articles n))
7405          (ogroup gnus-newsgroup-name)
7406          (params (append (gnus-info-params (gnus-get-info ogroup))
7407                          (list (cons 'to-group ogroup))))
7408          article group egroup groups vgroup)
7409     (while (setq article (pop articles))
7410       (setq group (format "%s-%d" gnus-newsgroup-name article))
7411       (gnus-summary-remove-process-mark article)
7412       (when (gnus-summary-display-article article)
7413         (save-excursion
7414           (with-temp-buffer
7415             (insert-buffer-substring gnus-original-article-buffer)
7416             ;; Remove some headers that may lead nndoc to make
7417             ;; the wrong guess.
7418             (message-narrow-to-head)
7419             (goto-char (point-min))
7420             (delete-matching-lines "^\\(Path\\):\\|^From ")
7421             (widen)
7422             (if (setq egroup
7423                       (gnus-group-read-ephemeral-group
7424                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7425                                      (nndoc-article-type guess))
7426                        t nil t))
7427                 (progn
7428                   ;; Make all postings to this group go to the parent group.
7429                   (nconc (gnus-info-params (gnus-get-info egroup))
7430                          params)
7431                   (push egroup groups))
7432               ;; Couldn't select this doc group.
7433               (gnus-error 3 "Article couldn't be entered"))))))
7434     ;; Now we have selected all the documents.
7435     (cond
7436      ((not groups)
7437       (error "None of the articles could be interpreted as documents"))
7438      ((gnus-group-read-ephemeral-group
7439        (setq vgroup (format
7440                      "nnvirtual:%s-%s" gnus-newsgroup-name
7441                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7442        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7443        t
7444        (cons (current-buffer) 'summary)))
7445      (t
7446       (error "Couldn't select virtual nndoc group")))))
7447
7448 (defun gnus-summary-isearch-article (&optional regexp-p)
7449   "Do incremental search forward on the current article.
7450 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7451   (interactive "P")
7452   (let* ((gnus-inhibit-treatment t)
7453          (old (gnus-summary-select-article)))
7454     (gnus-configure-windows 'article)
7455     (gnus-eval-in-buffer-window gnus-article-buffer
7456       (save-restriction
7457         (widen)
7458         (when (eq 'old old)
7459           (gnus-article-show-all-headers))
7460         (goto-char (point-min))
7461         (isearch-forward regexp-p)))))
7462
7463 (defun gnus-summary-search-article-forward (regexp &optional backward)
7464   "Search for an article containing REGEXP forward.
7465 If BACKWARD, search backward instead."
7466   (interactive
7467    (list (read-string
7468           (format "Search article %s (regexp%s): "
7469                   (if current-prefix-arg "backward" "forward")
7470                   (if gnus-last-search-regexp
7471                       (concat ", default " gnus-last-search-regexp)
7472                     "")))
7473          current-prefix-arg))
7474   (if (string-equal regexp "")
7475       (setq regexp (or gnus-last-search-regexp ""))
7476     (setq gnus-last-search-regexp regexp)
7477     (setq gnus-article-before-search gnus-current-article))
7478   ;; Intentionally set gnus-last-article.
7479   (setq gnus-last-article gnus-article-before-search)
7480   (let ((gnus-last-article gnus-last-article))
7481     (if (gnus-summary-search-article regexp backward)
7482         (gnus-summary-show-thread)
7483       (error "Search failed: \"%s\"" regexp))))
7484
7485 (defun gnus-summary-search-article-backward (regexp)
7486   "Search for an article containing REGEXP backward."
7487   (interactive
7488    (list (read-string
7489           (format "Search article backward (regexp%s): "
7490                   (if gnus-last-search-regexp
7491                       (concat ", default " gnus-last-search-regexp)
7492                     "")))))
7493   (gnus-summary-search-article-forward regexp 'backward))
7494
7495 (eval-when-compile
7496   (defmacro gnus-summary-search-article-position-point (regexp backward)
7497     "Dehighlight the last matched text and goto the beginning position."
7498     (` (if (and gnus-summary-search-article-matched-data
7499                 (let ((text (caddr gnus-summary-search-article-matched-data))
7500                       (inhibit-read-only t)
7501                       buffer-read-only)
7502                   (delete-region
7503                    (goto-char (car gnus-summary-search-article-matched-data))
7504                    (cadr gnus-summary-search-article-matched-data))
7505                   (insert text)
7506                   (string-match (, regexp) text)))
7507            (if (, backward) (beginning-of-line) (end-of-line))
7508          (goto-char (if (, backward) (point-max) (point-min))))))
7509
7510   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7511     "Place point where X-Face image is displayed."
7512     (if (featurep 'xemacs)
7513         (` (let ((end (if (search-forward "\n\n" nil t)
7514                           (goto-char (1- (point)))
7515                         (point-min)))
7516                  extent)
7517              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7518              (unless (and (re-search-forward "^From:" end t)
7519                           (setq extent (extent-at (point)))
7520                           (extent-begin-glyph extent))
7521                (goto-char (, opoint)))))
7522       (` (let ((end (if (search-forward "\n\n" nil t)
7523                         (goto-char (1- (point)))
7524                       (point-min)))
7525                (start (or (search-backward "\n\n" nil t) (point-min))))
7526            (goto-char
7527             (or (text-property-any start end 'x-face-image t);; x-face-e21
7528                 (text-property-any start end 'x-face-mule-bitmap-image t)
7529                 (, opoint)))))))
7530
7531   (defmacro gnus-summary-search-article-highlight-matched-text
7532     (backward treated x-face)
7533     "Highlight matched text in the function `gnus-summary-search-article'."
7534     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7535              (end (set-marker (make-marker) (match-end 0)))
7536              (inhibit-read-only t)
7537              buffer-read-only)
7538          (unless treated
7539            (let ((,@
7540                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7541                     (mapcar
7542                      (lambda (item) (setq items (delq item items)))
7543                      '(gnus-treat-buttonize
7544                        gnus-treat-fill-article
7545                        gnus-treat-fill-long-lines
7546                        gnus-treat-emphasize
7547                        gnus-treat-highlight-headers
7548                        gnus-treat-highlight-citation
7549                        gnus-treat-highlight-signature
7550                        gnus-treat-overstrike
7551                        gnus-treat-display-xface
7552                        gnus-treat-buttonize-head
7553                        gnus-treat-decode-article-as-default-mime-charset))
7554                     (static-if (featurep 'xemacs)
7555                         items
7556                       (cons '(x-face-mule-delete-x-face-field
7557                               (quote never))
7558                             items))))
7559                  (gnus-treat-display-xface
7560                   (when (, x-face) gnus-treat-display-xface)))
7561              (gnus-article-prepare-mime-display)))
7562          (goto-char (if (, backward) start end))
7563          (when (, x-face)
7564            (gnus-summary-search-article-highlight-goto-x-face (point)))
7565          (setq gnus-summary-search-article-matched-data
7566                (list start end (buffer-substring start end)))
7567          (unless (eq start end);; matched text has been deleted. :-<
7568            (put-text-property start end 'face
7569                               (or (find-face 'isearch)
7570                                   'secondary-selection))))))
7571   )
7572
7573 (defun gnus-summary-search-article (regexp &optional backward)
7574   "Search for an article containing REGEXP.
7575 Optional argument BACKWARD means do search for backward.
7576 `gnus-select-article-hook' is not called during the search."
7577   ;; We have to require this here to make sure that the following
7578   ;; dynamic binding isn't shadowed by autoloading.
7579   (require 'gnus-async)
7580   (require 'gnus-art)
7581   (let ((gnus-select-article-hook nil)  ;Disable hook.
7582         (gnus-article-prepare-hook nil)
7583         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7584         (gnus-use-article-prefetch nil)
7585         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7586         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7587         (sum (current-buffer))
7588         (found nil)
7589         point treated)
7590     (gnus-save-hidden-threads
7591       (static-if (featurep 'xemacs)
7592           (let ((gnus-inhibit-treatment t))
7593             (setq treated (eq 'old (gnus-summary-select-article)))
7594             (when (and treated
7595                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7596                                  (window-live-p (get-buffer-window
7597                                                  gnus-article-buffer t)))))
7598               (gnus-summary-select-article nil t)
7599               (setq treated nil)))
7600         (let ((gnus-inhibit-treatment t)
7601               (x-face-mule-delete-x-face-field 'never))
7602           (setq treated (eq 'old (gnus-summary-select-article)))
7603           (when (and treated
7604                      (not
7605                       (and (gnus-buffer-live-p gnus-article-buffer)
7606                            (window-live-p (get-buffer-window
7607                                            gnus-article-buffer t))
7608                            (or (not (string-match "^\\^X-Face:" regexp))
7609                                (with-current-buffer gnus-article-buffer
7610                                  gnus-summary-search-article-matched-data)))))
7611             (gnus-summary-select-article nil t)
7612             (setq treated nil))))
7613       (set-buffer gnus-article-buffer)
7614       (widen)
7615       (if treated
7616           (progn
7617             (gnus-article-show-all-headers)
7618             (gnus-summary-search-article-position-point regexp backward))
7619         (goto-char (if backward (point-max) (point-min))))
7620       (while (not found)
7621         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7622         (if (if backward
7623                 (re-search-backward regexp nil t)
7624               (re-search-forward regexp nil t))
7625             ;; We found the regexp.
7626             (progn
7627               (gnus-summary-search-article-highlight-matched-text
7628                backward treated (string-match "^\\^X-Face:" regexp))
7629               (setq found 'found)
7630               (forward-line
7631                (/ (- 2 (window-height
7632                         (get-buffer-window gnus-article-buffer t)))
7633                   2))
7634               (set-window-start
7635                (get-buffer-window (current-buffer))
7636                (point))
7637               (set-buffer sum)
7638               (setq point (point)))
7639           ;; We didn't find it, so we go to the next article.
7640           (set-buffer sum)
7641           (setq found 'not)
7642           (while (eq found 'not)
7643             (if (not (if backward (gnus-summary-find-prev)
7644                        (gnus-summary-find-next)))
7645                 ;; No more articles.
7646                 (setq found t)
7647               ;; Select the next article and adjust point.
7648               (unless (gnus-summary-article-sparse-p
7649                        (gnus-summary-article-number))
7650                 (setq found nil)
7651                 (let ((gnus-inhibit-treatment t))
7652                   (gnus-summary-select-article))
7653                 (setq treated nil)
7654                 (set-buffer gnus-article-buffer)
7655                 (widen)
7656                 (goto-char (if backward (point-max) (point-min))))))))
7657       (gnus-message 7 ""))
7658     ;; Return whether we found the regexp.
7659     (when (eq found 'found)
7660       (goto-char point)
7661       (gnus-summary-show-thread)
7662       (gnus-summary-goto-subject gnus-current-article)
7663       (gnus-summary-position-point)
7664       t)))
7665
7666 (defun gnus-find-matching-articles (header regexp)
7667   "Return a list of all articles that match REGEXP on HEADER.
7668 This search includes all articles in the current group that Gnus has
7669 fetched headers for, whether they are displayed or not."
7670   (let ((articles nil)
7671         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7672         (case-fold-search t))
7673     (dolist (header gnus-newsgroup-headers)
7674       (when (string-match regexp (funcall func header))
7675         (push (mail-header-number header) articles)))
7676     (nreverse articles)))
7677
7678 (defun gnus-summary-find-matching (header regexp &optional backward unread
7679                                           not-case-fold)
7680   "Return a list of all articles that match REGEXP on HEADER.
7681 The search stars on the current article and goes forwards unless
7682 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7683 If UNREAD is non-nil, only unread articles will
7684 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7685 in the comparisons."
7686   (let ((case-fold-search (not not-case-fold))
7687         articles d func)
7688     (if (consp header)
7689         (if (eq (car header) 'extra)
7690             (setq func
7691                   `(lambda (h)
7692                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7693                          "")))
7694           (error "%s is an invalid header" header))
7695       (unless (fboundp (intern (concat "mail-header-" header)))
7696         (error "%s is not a valid header" header))
7697       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7698     (dolist (d (if (eq backward 'all)
7699                    gnus-newsgroup-data
7700                  (gnus-data-find-list
7701                   (gnus-summary-article-number)
7702                   (gnus-data-list backward))))
7703       (when (and (or (not unread)       ; We want all articles...
7704                      (gnus-data-unread-p d)) ; Or just unreads.
7705                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7706                  (string-match regexp
7707                                (funcall func (gnus-data-header d)))) ; Match.
7708         (push (gnus-data-number d) articles))) ; Success!
7709     (nreverse articles)))
7710
7711 (defun gnus-summary-execute-command (header regexp command &optional backward)
7712   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7713 If HEADER is an empty string (or nil), the match is done on the entire
7714 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7715   (interactive
7716    (list (let ((completion-ignore-case t))
7717            (completing-read
7718             "Header name: "
7719             (mapcar (lambda (header) (list (format "%s" header)))
7720                     (append
7721                      '("Number" "Subject" "From" "Lines" "Date"
7722                        "Message-ID" "Xref" "References" "Body")
7723                      gnus-extra-headers))
7724             nil 'require-match))
7725          (read-string "Regexp: ")
7726          (read-key-sequence "Command: ")
7727          current-prefix-arg))
7728   (when (equal header "Body")
7729     (setq header ""))
7730   ;; Hidden thread subtrees must be searched as well.
7731   (gnus-summary-show-all-threads)
7732   ;; We don't want to change current point nor window configuration.
7733   (save-excursion
7734     (save-window-excursion
7735       (gnus-message 6 "Executing %s..." (key-description command))
7736       ;; We'd like to execute COMMAND interactively so as to give arguments.
7737       (gnus-execute header regexp
7738                     `(call-interactively ',(key-binding command))
7739                     backward)
7740       (gnus-message 6 "Executing %s...done" (key-description command)))))
7741
7742 (defun gnus-summary-beginning-of-article ()
7743   "Scroll the article back to the beginning."
7744   (interactive)
7745   (gnus-summary-select-article)
7746   (gnus-configure-windows 'article)
7747   (gnus-eval-in-buffer-window gnus-article-buffer
7748     (widen)
7749     (goto-char (point-min))
7750     (when gnus-page-broken
7751       (gnus-narrow-to-page))))
7752
7753 (defun gnus-summary-end-of-article ()
7754   "Scroll to the end of the article."
7755   (interactive)
7756   (gnus-summary-select-article)
7757   (gnus-configure-windows 'article)
7758   (gnus-eval-in-buffer-window gnus-article-buffer
7759     (widen)
7760     (goto-char (point-max))
7761     (recenter -3)
7762     (when gnus-page-broken
7763       (gnus-narrow-to-page))))
7764
7765 (defun gnus-summary-print-article (&optional filename n)
7766   "Generate and print a PostScript image of the N next (mail) articles.
7767
7768 If N is negative, print the N previous articles.  If N is nil and articles
7769 have been marked with the process mark, print these instead.
7770
7771 If the optional first argument FILENAME is nil, send the image to the
7772 printer.  If FILENAME is a string, save the PostScript image in a file with
7773 that name.  If FILENAME is a number, prompt the user for the name of the file
7774 to save in."
7775   (interactive (list (ps-print-preprint current-prefix-arg)))
7776   (dolist (article (gnus-summary-work-articles n))
7777     (gnus-summary-select-article nil nil 'pseudo article)
7778     (gnus-eval-in-buffer-window gnus-article-buffer
7779       (let ((buffer (generate-new-buffer " *print*")))
7780         (unwind-protect
7781             (progn
7782               (copy-to-buffer buffer (point-min) (point-max))
7783               (set-buffer buffer)
7784               (gnus-article-delete-invisible-text)
7785               (when (gnus-visual-p 'article-highlight 'highlight)
7786                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7787                 ;; highlight.
7788                 (let ((gnus-article-buffer buffer))
7789                   (gnus-article-highlight-citation t)
7790                   (gnus-article-highlight-signature)))
7791               (let ((ps-left-header
7792                      (list
7793                       (concat "("
7794                               (mail-header-subject gnus-current-headers) ")")
7795                       (concat "("
7796                               (mail-header-from gnus-current-headers) ")")))
7797                     (ps-right-header
7798                      (list
7799                       "/pagenumberstring load"
7800                       (concat "("
7801                               (mail-header-date gnus-current-headers) ")"))))
7802                 (gnus-run-hooks 'gnus-ps-print-hook)
7803                 (save-excursion
7804                   (if window-system
7805                       (ps-spool-buffer-with-faces)
7806                     (ps-spool-buffer)))))
7807           (kill-buffer buffer))))
7808     (gnus-summary-remove-process-mark article))
7809   (ps-despool filename))
7810
7811 (defun gnus-summary-show-article (&optional arg)
7812   "Force re-fetching of the current article.
7813 If ARG (the prefix) is a number, show the article with the charset
7814 defined in `gnus-summary-show-article-charset-alist', or the charset
7815 inputed.
7816 If ARG (the prefix) is non-nil and not a number, show the raw article
7817 without any article massaging functions being run."
7818   (interactive "P")
7819   (cond
7820    ((numberp arg)
7821     (let ((gnus-newsgroup-charset
7822            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7823                (mm-read-coding-system "Charset: ")))
7824           (gnus-newsgroup-ignored-charsets 'gnus-all))
7825       (gnus-summary-select-article nil 'force)
7826       (let ((deps gnus-newsgroup-dependencies)
7827             head header)
7828         (save-excursion
7829           (set-buffer gnus-original-article-buffer)
7830           (save-restriction
7831             (message-narrow-to-head)
7832             (setq head (buffer-string)))
7833           (with-temp-buffer
7834             (insert (format "211 %d Article retrieved.\n"
7835                             (cdr gnus-article-current)))
7836             (insert head)
7837             (insert ".\n")
7838             (let ((nntp-server-buffer (current-buffer)))
7839               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7840         (gnus-data-set-header
7841          (gnus-data-find (cdr gnus-article-current))
7842          header)
7843         (gnus-summary-update-article-line
7844          (cdr gnus-article-current) header))))
7845    ((not arg)
7846     ;; Select the article the normal way.
7847     (gnus-summary-select-article nil 'force))
7848    (t
7849     ;; We have to require this here to make sure that the following
7850     ;; dynamic binding isn't shadowed by autoloading.
7851     (require 'gnus-async)
7852     (require 'gnus-art)
7853     ;; Bind the article treatment functions to nil.
7854     (let ((gnus-have-all-headers t)
7855           gnus-article-prepare-hook
7856           gnus-article-decode-hook
7857           gnus-break-pages
7858           gnus-show-mime
7859           (gnus-inhibit-treatment t))
7860       (gnus-summary-select-article nil 'force))))
7861   (gnus-summary-goto-subject gnus-current-article)
7862   (gnus-summary-position-point))
7863
7864 (defun gnus-summary-show-raw-article ()
7865   "Show the raw article without any article massaging functions being run."
7866   (interactive)
7867   (gnus-summary-show-article t))
7868
7869 (defun gnus-summary-verbose-headers (&optional arg)
7870   "Toggle permanent full header display.
7871 If ARG is a positive number, turn header display on.
7872 If ARG is a negative number, turn header display off."
7873   (interactive "P")
7874   (setq gnus-show-all-headers
7875         (cond ((or (not (numberp arg))
7876                    (zerop arg))
7877                (not gnus-show-all-headers))
7878               ((natnump arg)
7879                t)))
7880   (gnus-summary-show-article))
7881
7882 (defun gnus-summary-toggle-header (&optional arg)
7883   "Show the headers if they are hidden, or hide them if they are shown.
7884 If ARG is a positive number, show the entire header.
7885 If ARG is a negative number, hide the unwanted header lines."
7886   (interactive "P")
7887   (save-excursion
7888     (set-buffer gnus-article-buffer)
7889     (save-restriction
7890       (let* ((buffer-read-only nil)
7891              (inhibit-point-motion-hooks t)
7892              hidden e)
7893         (setq hidden
7894               (if (numberp arg)
7895                   (>= arg 0)
7896                 (save-restriction
7897                   (article-narrow-to-head)
7898                   (gnus-article-hidden-text-p 'headers))))
7899         (goto-char (point-min))
7900         (when (search-forward "\n\n" nil t)
7901           (delete-region (point-min) (1- (point))))
7902         (goto-char (point-min))
7903         (save-excursion
7904           (set-buffer gnus-original-article-buffer)
7905           (goto-char (point-min))
7906           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7907         (insert-buffer-substring gnus-original-article-buffer 1 e)
7908         (save-restriction
7909           (narrow-to-region (point-min) (point))
7910           (article-decode-encoded-words)
7911           (if  hidden
7912               (let ((gnus-treat-hide-headers nil)
7913                     (gnus-treat-hide-boring-headers nil))
7914                 (setq gnus-article-wash-types
7915                       (delq 'headers gnus-article-wash-types))
7916                 (gnus-treat-article 'head))
7917             (gnus-treat-article 'head)))
7918         (gnus-set-mode-line 'article)))))
7919
7920 (defun gnus-summary-show-all-headers ()
7921   "Make all header lines visible."
7922   (interactive)
7923   (gnus-summary-toggle-header 1))
7924
7925 (defun gnus-summary-toggle-mime (&optional arg)
7926   "Toggle MIME processing.
7927 If ARG is a positive number, turn MIME processing on."
7928   (interactive "P")
7929   (setq gnus-show-mime
7930         (if (null arg)
7931             (not gnus-show-mime)
7932           (> (prefix-numeric-value arg) 0)))
7933   (gnus-summary-select-article t 'force))
7934
7935 (defun gnus-summary-caesar-message (&optional arg)
7936   "Caesar rotate the current article by 13.
7937 The numerical prefix specifies how many places to rotate each letter
7938 forward."
7939   (interactive "P")
7940   (gnus-summary-select-article)
7941   (let ((mail-header-separator ""))
7942     (gnus-eval-in-buffer-window gnus-article-buffer
7943       (save-restriction
7944         (widen)
7945         (let ((start (window-start))
7946               buffer-read-only)
7947           (message-caesar-buffer-body arg)
7948           (set-window-start (get-buffer-window (current-buffer)) start))))))
7949
7950 (defun gnus-summary-stop-page-breaking ()
7951   "Stop page breaking in the current article."
7952   (interactive)
7953   (gnus-summary-select-article)
7954   (gnus-eval-in-buffer-window gnus-article-buffer
7955     (widen)
7956     (when (gnus-visual-p 'page-marker)
7957       (let ((buffer-read-only nil))
7958         (gnus-remove-text-with-property 'gnus-prev)
7959         (gnus-remove-text-with-property 'gnus-next))
7960       (setq gnus-page-broken nil))))
7961
7962 (defun gnus-summary-move-article (&optional n to-newsgroup
7963                                             select-method action)
7964   "Move the current article to a different newsgroup.
7965 If N is a positive number, move the N next articles.
7966 If N is a negative number, move the N previous articles.
7967 If N is nil and any articles have been marked with the process mark,
7968 move those articles instead.
7969 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7970 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7971 re-spool using this method.
7972
7973 For this function to work, both the current newsgroup and the
7974 newsgroup that you want to move to have to support the `request-move'
7975 and `request-accept' functions.
7976
7977 ACTION can be either `move' (the default), `crosspost' or `copy'."
7978   (interactive "P")
7979   (unless action
7980     (setq action 'move))
7981   ;; Disable marking as read.
7982   (let (gnus-mark-article-hook)
7983     (save-window-excursion
7984       (gnus-summary-select-article)))
7985   ;; Check whether the source group supports the required functions.
7986   (cond ((and (eq action 'move)
7987               (not (gnus-check-backend-function
7988                     'request-move-article gnus-newsgroup-name)))
7989          (error "The current group does not support article moving"))
7990         ((and (eq action 'crosspost)
7991               (not (gnus-check-backend-function
7992                     'request-replace-article gnus-newsgroup-name)))
7993          (error "The current group does not support article editing")))
7994   (let ((articles (gnus-summary-work-articles n))
7995         (prefix (if (gnus-check-backend-function
7996                      'request-move-article gnus-newsgroup-name)
7997                     (gnus-group-real-prefix gnus-newsgroup-name)
7998                   ""))
7999         (names '((move "Move" "Moving")
8000                  (copy "Copy" "Copying")
8001                  (crosspost "Crosspost" "Crossposting")))
8002         (copy-buf (save-excursion
8003                     (nnheader-set-temp-buffer " *copy article*")))
8004         (default-marks gnus-article-mark-lists)
8005         (no-expire-marks (delete '(expirable . expire)
8006                                  (copy-sequence gnus-article-mark-lists)))
8007         art-group to-method new-xref article to-groups)
8008     (unless (assq action names)
8009       (error "Unknown action %s" action))
8010     ;; Read the newsgroup name.
8011     (when (and (not to-newsgroup)
8012                (not select-method))
8013       (setq to-newsgroup
8014             (gnus-read-move-group-name
8015              (cadr (assq action names))
8016              (symbol-value (intern (format "gnus-current-%s-group" action)))
8017              articles prefix))
8018       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8019     (setq to-method (or select-method
8020                         (gnus-server-to-method
8021                          (gnus-group-method to-newsgroup))))
8022     ;; Check the method we are to move this article to...
8023     (unless (gnus-check-backend-function
8024              'request-accept-article (car to-method))
8025       (error "%s does not support article copying" (car to-method)))
8026     (unless (gnus-check-server to-method)
8027       (error "Can't open server %s" (car to-method)))
8028     (gnus-message 6 "%s to %s: %s..."
8029                   (caddr (assq action names))
8030                   (or (car select-method) to-newsgroup) articles)
8031     (while articles
8032       (setq article (pop articles))
8033       (setq
8034        art-group
8035        (cond
8036         ;; Move the article.
8037         ((eq action 'move)
8038          ;; Remove this article from future suppression.
8039          (gnus-dup-unsuppress-article article)
8040          (gnus-request-move-article
8041           article                       ; Article to move
8042           gnus-newsgroup-name           ; From newsgroup
8043           (nth 1 (gnus-find-method-for-group
8044                   gnus-newsgroup-name)) ; Server
8045           (list 'gnus-request-accept-article
8046                 to-newsgroup (list 'quote select-method)
8047                 (not articles) t)       ; Accept form
8048           (not articles)))              ; Only save nov last time
8049         ;; Copy the article.
8050         ((eq action 'copy)
8051          (save-excursion
8052            (set-buffer copy-buf)
8053            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8054              (gnus-request-accept-article
8055               to-newsgroup select-method (not articles) t))))
8056         ;; Crosspost the article.
8057         ((eq action 'crosspost)
8058          (let ((xref (message-tokenize-header
8059                       (mail-header-xref (gnus-summary-article-header article))
8060                       " ")))
8061            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8062                                   ":" article))
8063            (unless xref
8064              (setq xref (list (system-name))))
8065            (setq new-xref
8066                  (concat
8067                   (mapconcat 'identity
8068                              (delete "Xref:" (delete new-xref xref))
8069                              " ")
8070                   " " new-xref))
8071            (save-excursion
8072              (set-buffer copy-buf)
8073              ;; First put the article in the destination group.
8074              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8075              (when (consp (setq art-group
8076                                 (gnus-request-accept-article
8077                                  to-newsgroup select-method (not articles))))
8078                (setq new-xref (concat new-xref " " (car art-group)
8079                                       ":" (cdr art-group)))
8080                ;; Now we have the new Xrefs header, so we insert
8081                ;; it and replace the new article.
8082                (nnheader-replace-header "Xref" new-xref)
8083                (gnus-request-replace-article
8084                 (cdr art-group) to-newsgroup (current-buffer))
8085                art-group))))))
8086       (cond
8087        ((not art-group)
8088         (gnus-message 1 "Couldn't %s article %s: %s"
8089                       (cadr (assq action names)) article
8090                       (nnheader-get-report (car to-method))))
8091        ((eq art-group 'junk)
8092         (when (eq action 'move)
8093           (gnus-summary-mark-article article gnus-canceled-mark)
8094           (gnus-message 4 "Deleted article %s" article)))
8095        (t
8096         (let* ((pto-group (gnus-group-prefixed-name
8097                            (car art-group) to-method))
8098                (entry
8099                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8100                (info (nth 2 entry))
8101                (to-group (gnus-info-group info))
8102                to-marks)
8103           ;; Update the group that has been moved to.
8104           (when (and info
8105                      (memq action '(move copy)))
8106             (unless (member to-group to-groups)
8107               (push to-group to-groups))
8108
8109             (unless (memq article gnus-newsgroup-unreads)
8110               (push 'read to-marks)
8111               (gnus-info-set-read
8112                info (gnus-add-to-range (gnus-info-read info)
8113                                        (list (cdr art-group)))))
8114
8115             ;; See whether the article is to be put in the cache.
8116             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8117                              default-marks
8118                            no-expire-marks))
8119                   (to-article (cdr art-group)))
8120
8121               ;; Enter the article into the cache in the new group,
8122               ;; if that is required.
8123               (when gnus-use-cache
8124                 (gnus-cache-possibly-enter-article
8125                  to-group to-article
8126                  (let ((header (copy-sequence
8127                                 (gnus-summary-article-header article))))
8128                    (mail-header-set-number header to-article)
8129                    header)
8130                  (memq article gnus-newsgroup-marked)
8131                  (memq article gnus-newsgroup-dormant)
8132                  (memq article gnus-newsgroup-unreads)))
8133
8134               (when gnus-preserve-marks
8135                 ;; Copy any marks over to the new group.
8136                 (when (and (equal to-group gnus-newsgroup-name)
8137                            (not (memq article gnus-newsgroup-unreads)))
8138                   ;; Mark this article as read in this group.
8139                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8140                   (setcdr (gnus-active to-group) to-article)
8141                   (setcdr gnus-newsgroup-active to-article))
8142
8143                 (while marks
8144                   (when (memq article (symbol-value
8145                                        (intern (format "gnus-newsgroup-%s"
8146                                                        (caar marks)))))
8147                     (push (cdar marks) to-marks)
8148                     ;; If the other group is the same as this group,
8149                     ;; then we have to add the mark to the list.
8150                     (when (equal to-group gnus-newsgroup-name)
8151                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8152                            (cons to-article
8153                                  (symbol-value
8154                                   (intern (format "gnus-newsgroup-%s"
8155                                                   (caar marks)))))))
8156                     ;; Copy the marks to other group.
8157                     (gnus-add-marked-articles
8158                      to-group (cdar marks) (list to-article) info))
8159                   (setq marks (cdr marks)))
8160
8161                 (gnus-request-set-mark to-group (list (list (list to-article)
8162                                                             'set
8163                                                             to-marks))))
8164
8165               (gnus-dribble-enter
8166                (concat "(gnus-group-set-info '"
8167                        (gnus-prin1-to-string (gnus-get-info to-group))
8168                        ")"))))
8169
8170           ;; Update the Xref header in this article to point to
8171           ;; the new crossposted article we have just created.
8172           (when (eq action 'crosspost)
8173             (save-excursion
8174               (set-buffer copy-buf)
8175               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8176               (nnheader-replace-header "Xref" new-xref)
8177               (gnus-request-replace-article
8178                article gnus-newsgroup-name (current-buffer)))))
8179
8180         ;;;!!!Why is this necessary?
8181         (set-buffer gnus-summary-buffer)
8182
8183         (gnus-summary-goto-subject article)
8184         (when (eq action 'move)
8185           (gnus-summary-mark-article article gnus-canceled-mark))))
8186       (gnus-summary-remove-process-mark article))
8187     ;; Re-activate all groups that have been moved to.
8188     (while to-groups
8189       (save-excursion
8190         (set-buffer gnus-group-buffer)
8191         (when (gnus-group-goto-group (car to-groups) t)
8192           (gnus-group-get-new-news-this-group 1 t))
8193         (pop to-groups)))
8194
8195     (gnus-kill-buffer copy-buf)
8196     (gnus-summary-position-point)
8197     (gnus-set-mode-line 'summary)))
8198
8199 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8200   "Move the current article to a different newsgroup.
8201 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8202 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8203 re-spool using this method."
8204   (interactive "P")
8205   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8206
8207 (defun gnus-summary-crosspost-article (&optional n)
8208   "Crosspost the current article to some other group."
8209   (interactive "P")
8210   (gnus-summary-move-article n nil nil 'crosspost))
8211
8212 (defcustom gnus-summary-respool-default-method nil
8213   "Default method for respooling an article.
8214 If nil, use to the current newsgroup method."
8215   :type '(choice (gnus-select-method :value (nnml ""))
8216                  (const nil))
8217   :group 'gnus-summary-mail)
8218
8219 (defun gnus-summary-respool-article (&optional n method)
8220   "Respool the current article.
8221 The article will be squeezed through the mail spooling process again,
8222 which means that it will be put in some mail newsgroup or other
8223 depending on `nnmail-split-methods'.
8224 If N is a positive number, respool the N next articles.
8225 If N is a negative number, respool the N previous articles.
8226 If N is nil and any articles have been marked with the process mark,
8227 respool those articles instead.
8228
8229 Respooling can be done both from mail groups and \"real\" newsgroups.
8230 In the former case, the articles in question will be moved from the
8231 current group into whatever groups they are destined to.  In the
8232 latter case, they will be copied into the relevant groups."
8233   (interactive
8234    (list current-prefix-arg
8235          (let* ((methods (gnus-methods-using 'respool))
8236                 (methname
8237                  (symbol-name (or gnus-summary-respool-default-method
8238                                   (car (gnus-find-method-for-group
8239                                         gnus-newsgroup-name)))))
8240                 (method
8241                  (gnus-completing-read
8242                   methname "What backend do you want to use when respooling?"
8243                   methods nil t nil 'gnus-mail-method-history))
8244                 ms)
8245            (cond
8246             ((zerop (length (setq ms (gnus-servers-using-backend
8247                                       (intern method)))))
8248              (list (intern method) ""))
8249             ((= 1 (length ms))
8250              (car ms))
8251             (t
8252              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8253                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8254                            ms-alist))))))))
8255   (unless method
8256     (error "No method given for respooling"))
8257   (if (assoc (symbol-name
8258               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8259              (gnus-methods-using 'respool))
8260       (gnus-summary-move-article n nil method)
8261     (gnus-summary-copy-article n nil method)))
8262
8263 (defun gnus-summary-import-article (file &optional edit)
8264   "Import an arbitrary file into a mail newsgroup."
8265   (interactive "fImport file: \nP")
8266   (let ((group gnus-newsgroup-name)
8267         (now (current-time))
8268         atts lines group-art)
8269     (unless (gnus-check-backend-function 'request-accept-article group)
8270       (error "%s does not support article importing" group))
8271     (or (file-readable-p file)
8272         (not (file-regular-p file))
8273         (error "Can't read %s" file))
8274     (save-excursion
8275       (set-buffer (gnus-get-buffer-create " *import file*"))
8276       (erase-buffer)
8277       (nnheader-insert-file-contents file)
8278       (goto-char (point-min))
8279       (if (nnheader-article-p)
8280           (save-restriction
8281             (goto-char (point-min))
8282             (search-forward "\n\n" nil t)
8283             (narrow-to-region (point-min) (1- (point)))
8284             (goto-char (point-min))
8285             (unless (re-search-forward "^date:" nil t)
8286               (goto-char (point-max))
8287               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8288         ;; This doesn't look like an article, so we fudge some headers.
8289         (setq atts (file-attributes file)
8290               lines (count-lines (point-min) (point-max)))
8291         (insert "From: " (read-string "From: ") "\n"
8292                 "Subject: " (read-string "Subject: ") "\n"
8293                 "Date: " (message-make-date (nth 5 atts)) "\n"
8294                 "Message-ID: " (message-make-message-id) "\n"
8295                 "Lines: " (int-to-string lines) "\n"
8296                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8297       (setq group-art (gnus-request-accept-article group nil t))
8298       (kill-buffer (current-buffer)))
8299     (setq gnus-newsgroup-active (gnus-activate-group group))
8300     (forward-line 1)
8301     (gnus-summary-goto-article (cdr group-art) nil t)
8302     (when edit
8303       (gnus-summary-edit-article))))
8304
8305 (defun gnus-summary-create-article ()
8306   "Create an article in a mail newsgroup."
8307   (interactive)
8308   (let ((group gnus-newsgroup-name)
8309         (now (current-time))
8310         group-art)
8311     (unless (gnus-check-backend-function 'request-accept-article group)
8312       (error "%s does not support article importing" group))
8313     (save-excursion
8314       (set-buffer (gnus-get-buffer-create " *import file*"))
8315       (erase-buffer)
8316       (goto-char (point-min))
8317       ;; This doesn't look like an article, so we fudge some headers.
8318       (insert "From: " (read-string "From: ") "\n"
8319               "Subject: " (read-string "Subject: ") "\n"
8320               "Date: " (message-make-date now) "\n"
8321               "Message-ID: " (message-make-message-id) "\n")
8322       (setq group-art (gnus-request-accept-article group nil t))
8323       (kill-buffer (current-buffer)))
8324     (setq gnus-newsgroup-active (gnus-activate-group group))
8325     (forward-line 1)
8326     (gnus-summary-goto-article (cdr group-art) nil t)
8327     (gnus-summary-edit-article)))
8328
8329 (defun gnus-summary-article-posted-p ()
8330   "Say whether the current (mail) article is available from news as well.
8331 This will be the case if the article has both been mailed and posted."
8332   (interactive)
8333   (let ((id (mail-header-references (gnus-summary-article-header)))
8334         (gnus-override-method (car (gnus-refer-article-methods))))
8335     (if (gnus-request-head id "")
8336         (gnus-message 2 "The current message was found on %s"
8337                       gnus-override-method)
8338       (gnus-message 2 "The current message couldn't be found on %s"
8339                     gnus-override-method)
8340       nil)))
8341
8342 (defun gnus-summary-expire-articles (&optional now)
8343   "Expire all articles that are marked as expirable in the current group."
8344   (interactive)
8345   (when (gnus-check-backend-function
8346          'request-expire-articles gnus-newsgroup-name)
8347     ;; This backend supports expiry.
8348     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8349            (expirable (if total
8350                           (progn
8351                             ;; We need to update the info for
8352                             ;; this group for `gnus-list-of-read-articles'
8353                             ;; to give us the right answer.
8354                             (gnus-run-hooks 'gnus-exit-group-hook)
8355                             (gnus-summary-update-info)
8356                             (gnus-list-of-read-articles gnus-newsgroup-name))
8357                         (setq gnus-newsgroup-expirable
8358                               (sort gnus-newsgroup-expirable '<))))
8359            (expiry-wait (if now 'immediate
8360                           (gnus-group-find-parameter
8361                            gnus-newsgroup-name 'expiry-wait)))
8362            (nnmail-expiry-target
8363             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8364                 nnmail-expiry-target))
8365            es)
8366       (when expirable
8367         ;; There are expirable articles in this group, so we run them
8368         ;; through the expiry process.
8369         (gnus-message 6 "Expiring articles...")
8370         (unless (gnus-check-group gnus-newsgroup-name)
8371           (error "Can't open server for %s" gnus-newsgroup-name))
8372         ;; The list of articles that weren't expired is returned.
8373         (save-excursion
8374           (if expiry-wait
8375               (let ((nnmail-expiry-wait-function nil)
8376                     (nnmail-expiry-wait expiry-wait))
8377                 (setq es (gnus-request-expire-articles
8378                           expirable gnus-newsgroup-name)))
8379             (setq es (gnus-request-expire-articles
8380                       expirable gnus-newsgroup-name)))
8381           (unless total
8382             (setq gnus-newsgroup-expirable es))
8383           ;; We go through the old list of expirable, and mark all
8384           ;; really expired articles as nonexistent.
8385           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8386             (let ((gnus-use-cache nil))
8387               (while expirable
8388                 (unless (memq (car expirable) es)
8389                   (when (gnus-data-find (car expirable))
8390                     (gnus-summary-mark-article
8391                      (car expirable) gnus-canceled-mark)))
8392                 (setq expirable (cdr expirable))))))
8393         (gnus-message 6 "Expiring articles...done")))))
8394
8395 (defun gnus-summary-expire-articles-now ()
8396   "Expunge all expirable articles in the current group.
8397 This means that *all* articles that are marked as expirable will be
8398 deleted forever, right now."
8399   (interactive)
8400   (or gnus-expert-user
8401       (gnus-yes-or-no-p
8402        "Are you really, really, really sure you want to delete all these messages? ")
8403       (error "Phew!"))
8404   (gnus-summary-expire-articles t))
8405
8406 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8407 (defun gnus-summary-delete-article (&optional n)
8408   "Delete the N next (mail) articles.
8409 This command actually deletes articles.  This is not a marking
8410 command.  The article will disappear forever from your life, never to
8411 return.
8412 If N is negative, delete backwards.
8413 If N is nil and articles have been marked with the process mark,
8414 delete these instead."
8415   (interactive "P")
8416   (unless (gnus-check-backend-function 'request-expire-articles
8417                                        gnus-newsgroup-name)
8418     (error "The current newsgroup does not support article deletion"))
8419   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8420     (error "Couldn't open server"))
8421   ;; Compute the list of articles to delete.
8422   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8423         not-deleted)
8424     (if (and gnus-novice-user
8425              (not (gnus-yes-or-no-p
8426                    (format "Do you really want to delete %s forever? "
8427                            (if (> (length articles) 1)
8428                                (format "these %s articles" (length articles))
8429                              "this article")))))
8430         ()
8431       ;; Delete the articles.
8432       (setq not-deleted (gnus-request-expire-articles
8433                          articles gnus-newsgroup-name 'force))
8434       (while articles
8435         (gnus-summary-remove-process-mark (car articles))
8436         ;; The backend might not have been able to delete the article
8437         ;; after all.
8438         (unless (memq (car articles) not-deleted)
8439           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8440         (setq articles (cdr articles)))
8441       (when not-deleted
8442         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8443     (gnus-summary-position-point)
8444     (gnus-set-mode-line 'summary)
8445     not-deleted))
8446
8447 (defun gnus-summary-edit-article (&optional force)
8448   "Edit the current article.
8449 This will have permanent effect only in mail groups.
8450 If FORCE is non-nil, allow editing of articles even in read-only
8451 groups."
8452   (interactive "P")
8453   (save-excursion
8454     (set-buffer gnus-summary-buffer)
8455     (let ((mail-parse-charset gnus-newsgroup-charset)
8456           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8457       (gnus-set-global-variables)
8458       (when (and (not force)
8459                  (gnus-group-read-only-p))
8460         (error "The current newsgroup does not support article editing"))
8461       (gnus-summary-show-article t)
8462       (gnus-article-edit-article
8463        'ignore
8464        `(lambda (no-highlight)
8465           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8466                 (message-options message-options)
8467                 (message-options-set-recipient)
8468                 (mail-parse-ignored-charsets
8469                  ',gnus-newsgroup-ignored-charsets))
8470             (gnus-summary-edit-article-done
8471              ,(or (mail-header-references gnus-current-headers) "")
8472              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8473
8474 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8475
8476 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8477                                                  no-highlight)
8478   "Make edits to the current article permanent."
8479   (interactive)
8480   (save-excursion
8481     ;; The buffer restriction contains the entire article if it exists.
8482     (when (article-goto-body)
8483       (let ((lines (count-lines (point) (point-max)))
8484             (length (- (point-max) (point)))
8485             (case-fold-search t)
8486             (body (copy-marker (point))))
8487         (goto-char (point-min))
8488         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8489           (delete-region (match-beginning 1) (match-end 1))
8490           (insert (number-to-string length)))
8491         (goto-char (point-min))
8492         (when (re-search-forward
8493                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8494           (delete-region (match-beginning 1) (match-end 1))
8495           (insert (number-to-string length)))
8496         (goto-char (point-min))
8497         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8498           (delete-region (match-beginning 1) (match-end 1))
8499           (insert (number-to-string lines))))))
8500   ;; Replace the article.
8501   (let ((buf (current-buffer)))
8502     (with-temp-buffer
8503       (insert-buffer-substring buf)
8504
8505       (if (and (not read-only)
8506                (not (gnus-request-replace-article
8507                      (cdr gnus-article-current) (car gnus-article-current)
8508                      (current-buffer) t)))
8509           (error "Couldn't replace article")
8510         ;; Update the summary buffer.
8511         (if (and references
8512                  (equal (message-tokenize-header references " ")
8513                         (message-tokenize-header
8514                          (or (message-fetch-field "references") "") " ")))
8515             ;; We only have to update this line.
8516             (save-excursion
8517               (save-restriction
8518                 (message-narrow-to-head)
8519                 (let ((head (buffer-string))
8520                       header)
8521                   (with-temp-buffer
8522                     (insert (format "211 %d Article retrieved.\n"
8523                                     (cdr gnus-article-current)))
8524                     (insert head)
8525                     (insert ".\n")
8526                     (let ((nntp-server-buffer (current-buffer)))
8527                       (setq header (car (gnus-get-newsgroup-headers
8528                                          (save-excursion
8529                                            (set-buffer gnus-summary-buffer)
8530                                            gnus-newsgroup-dependencies)
8531                                          t))))
8532                     (save-excursion
8533                       (set-buffer gnus-summary-buffer)
8534                       (gnus-data-set-header
8535                        (gnus-data-find (cdr gnus-article-current))
8536                        header)
8537                       (gnus-summary-update-article-line
8538                        (cdr gnus-article-current) header))))))
8539           ;; Update threads.
8540           (set-buffer (or buffer gnus-summary-buffer))
8541           (gnus-summary-update-article (cdr gnus-article-current)))
8542         ;; Prettify the article buffer again.
8543         (unless no-highlight
8544           (save-excursion
8545             (set-buffer gnus-article-buffer)
8546             ;;;!!! Fix this -- article should be rehighlighted.
8547             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8548             (set-buffer gnus-original-article-buffer)
8549             (gnus-request-article
8550              (cdr gnus-article-current)
8551              (car gnus-article-current) (current-buffer))))
8552         ;; Prettify the summary buffer line.
8553         (when (gnus-visual-p 'summary-highlight 'highlight)
8554           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8555
8556 (defun gnus-summary-edit-wash (key)
8557   "Perform editing command KEY in the article buffer."
8558   (interactive
8559    (list
8560     (progn
8561       (message "%s" (concat (this-command-keys) "- "))
8562       (read-char))))
8563   (message "")
8564   (gnus-summary-edit-article)
8565   (execute-kbd-macro (concat (this-command-keys) key))
8566   (gnus-article-edit-done))
8567
8568 ;;; Respooling
8569
8570 (defun gnus-summary-respool-query (&optional silent trace)
8571   "Query where the respool algorithm would put this article."
8572   (interactive)
8573   (let (gnus-mark-article-hook)
8574     (gnus-summary-select-article)
8575     (save-excursion
8576       (set-buffer gnus-original-article-buffer)
8577       (save-restriction
8578         (message-narrow-to-head)
8579         (let ((groups (nnmail-article-group 'identity trace)))
8580           (unless silent
8581             (if groups
8582                 (message "This message would go to %s"
8583                          (mapconcat 'car groups ", "))
8584               (message "This message would go to no groups"))
8585             groups))))))
8586
8587 (defun gnus-summary-respool-trace ()
8588   "Trace where the respool algorithm would put this article.
8589 Display a buffer showing all fancy splitting patterns which matched."
8590   (interactive)
8591   (gnus-summary-respool-query nil t))
8592
8593 ;; Summary marking commands.
8594
8595 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8596   "Mark articles which has the same subject as read, and then select the next.
8597 If UNMARK is positive, remove any kind of mark.
8598 If UNMARK is negative, tick articles."
8599   (interactive "P")
8600   (when unmark
8601     (setq unmark (prefix-numeric-value unmark)))
8602   (let ((count
8603          (gnus-summary-mark-same-subject
8604           (gnus-summary-article-subject) unmark)))
8605     ;; Select next unread article.  If auto-select-same mode, should
8606     ;; select the first unread article.
8607     (gnus-summary-next-article t (and gnus-auto-select-same
8608                                       (gnus-summary-article-subject)))
8609     (gnus-message 7 "%d article%s marked as %s"
8610                   count (if (= count 1) " is" "s are")
8611                   (if unmark "unread" "read"))))
8612
8613 (defun gnus-summary-kill-same-subject (&optional unmark)
8614   "Mark articles which has the same subject as read.
8615 If UNMARK is positive, remove any kind of mark.
8616 If UNMARK is negative, tick articles."
8617   (interactive "P")
8618   (when unmark
8619     (setq unmark (prefix-numeric-value unmark)))
8620   (let ((count
8621          (gnus-summary-mark-same-subject
8622           (gnus-summary-article-subject) unmark)))
8623     ;; If marked as read, go to next unread subject.
8624     (when (null unmark)
8625       ;; Go to next unread subject.
8626       (gnus-summary-next-subject 1 t))
8627     (gnus-message 7 "%d articles are marked as %s"
8628                   count (if unmark "unread" "read"))))
8629
8630 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8631   "Mark articles with same SUBJECT as read, and return marked number.
8632 If optional argument UNMARK is positive, remove any kinds of marks.
8633 If optional argument UNMARK is negative, mark articles as unread instead."
8634   (let ((count 1))
8635     (save-excursion
8636       (cond
8637        ((null unmark)                   ; Mark as read.
8638         (while (and
8639                 (progn
8640                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8641                   (gnus-summary-show-thread) t)
8642                 (gnus-summary-find-subject subject))
8643           (setq count (1+ count))))
8644        ((> unmark 0)                    ; Tick.
8645         (while (and
8646                 (progn
8647                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8648                   (gnus-summary-show-thread) t)
8649                 (gnus-summary-find-subject subject))
8650           (setq count (1+ count))))
8651        (t                               ; Mark as unread.
8652         (while (and
8653                 (progn
8654                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8655                   (gnus-summary-show-thread) t)
8656                 (gnus-summary-find-subject subject))
8657           (setq count (1+ count)))))
8658       (gnus-set-mode-line 'summary)
8659       ;; Return the number of marked articles.
8660       count)))
8661
8662 (defun gnus-summary-mark-as-processable (n &optional unmark)
8663   "Set the process mark on the next N articles.
8664 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8665 the process mark instead.  The difference between N and the actual
8666 number of articles marked is returned."
8667   (interactive "P")
8668   (if (and (null n) (gnus-region-active-p))
8669       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8670     (setq n (prefix-numeric-value n))
8671     (let ((backward (< n 0))
8672           (n (abs n)))
8673       (while (and
8674               (> n 0)
8675               (if unmark
8676                   (gnus-summary-remove-process-mark
8677                    (gnus-summary-article-number))
8678                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8679               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8680         (setq n (1- n)))
8681       (when (/= 0 n)
8682         (gnus-message 7 "No more articles"))
8683       (gnus-summary-recenter)
8684       (gnus-summary-position-point)
8685       n)))
8686
8687 (defun gnus-summary-unmark-as-processable (n)
8688   "Remove the process mark from the next N articles.
8689 If N is negative, unmark backward instead.  The difference between N and
8690 the actual number of articles unmarked is returned."
8691   (interactive "P")
8692   (gnus-summary-mark-as-processable n t))
8693
8694 (defun gnus-summary-unmark-all-processable ()
8695   "Remove the process mark from all articles."
8696   (interactive)
8697   (save-excursion
8698     (while gnus-newsgroup-processable
8699       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8700   (gnus-summary-position-point))
8701
8702 (defun gnus-summary-add-mark (article type)
8703   "Mark ARTICLE with a mark of TYPE."
8704   (let ((vtype (car (assq type gnus-article-mark-lists)))
8705         var)
8706     (if (not vtype)
8707         (error "No such mark type: %s" type)
8708       (setq var (intern (format "gnus-newsgroup-%s" type)))
8709       (set var (cons article (symbol-value var)))
8710       (if (memq type '(processable cached replied forwarded saved))
8711           (gnus-summary-update-secondary-mark article)
8712         ;;; !!! This is bobus.  We should find out what primary
8713         ;;; !!! mark we want to set.
8714         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8715
8716 (defun gnus-summary-mark-as-expirable (n)
8717   "Mark N articles forward as expirable.
8718 If N is negative, mark backward instead.  The difference between N and
8719 the actual number of articles marked is returned."
8720   (interactive "p")
8721   (gnus-summary-mark-forward n gnus-expirable-mark))
8722
8723 (defun gnus-summary-mark-article-as-replied (article)
8724   "Mark ARTICLE as replied to and update the summary line.
8725 ARTICLE can also be a list of articles."
8726   (interactive (list (gnus-summary-article-number)))
8727   (let ((articles (if (listp article) article (list article))))
8728     (dolist (article articles)
8729       (push article gnus-newsgroup-replied)
8730       (let ((buffer-read-only nil))
8731         (when (gnus-summary-goto-subject article nil t)
8732           (gnus-summary-update-secondary-mark article))))))
8733
8734 (defun gnus-summary-mark-article-as-forwarded (article)
8735   "Mark ARTICLE as forwarded and update the summary line.
8736 ARTICLE can also be a list of articles."
8737   (let ((articles (if (listp article) article (list article))))
8738     (dolist (article articles)
8739       (push article gnus-newsgroup-forwarded)
8740       (let ((buffer-read-only nil))
8741         (when (gnus-summary-goto-subject article nil t)
8742           (gnus-summary-update-secondary-mark article))))))
8743
8744 (defun gnus-summary-set-bookmark (article)
8745   "Set a bookmark in current article."
8746   (interactive (list (gnus-summary-article-number)))
8747   (when (or (not (get-buffer gnus-article-buffer))
8748             (not gnus-current-article)
8749             (not gnus-article-current)
8750             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8751     (error "No current article selected"))
8752   ;; Remove old bookmark, if one exists.
8753   (let ((old (assq article gnus-newsgroup-bookmarks)))
8754     (when old
8755       (setq gnus-newsgroup-bookmarks
8756             (delq old gnus-newsgroup-bookmarks))))
8757   ;; Set the new bookmark, which is on the form
8758   ;; (article-number . line-number-in-body).
8759   (push
8760    (cons article
8761          (save-excursion
8762            (set-buffer gnus-article-buffer)
8763            (count-lines
8764             (min (point)
8765                  (save-excursion
8766                    (goto-char (point-min))
8767                    (search-forward "\n\n" nil t)
8768                    (point)))
8769             (point))))
8770    gnus-newsgroup-bookmarks)
8771   (gnus-message 6 "A bookmark has been added to the current article."))
8772
8773 (defun gnus-summary-remove-bookmark (article)
8774   "Remove the bookmark from the current article."
8775   (interactive (list (gnus-summary-article-number)))
8776   ;; Remove old bookmark, if one exists.
8777   (let ((old (assq article gnus-newsgroup-bookmarks)))
8778     (if old
8779         (progn
8780           (setq gnus-newsgroup-bookmarks
8781                 (delq old gnus-newsgroup-bookmarks))
8782           (gnus-message 6 "Removed bookmark."))
8783       (gnus-message 6 "No bookmark in current article."))))
8784
8785 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8786 (defun gnus-summary-mark-as-dormant (n)
8787   "Mark N articles forward as dormant.
8788 If N is negative, mark backward instead.  The difference between N and
8789 the actual number of articles marked is returned."
8790   (interactive "p")
8791   (gnus-summary-mark-forward n gnus-dormant-mark))
8792
8793 (defun gnus-summary-set-process-mark (article)
8794   "Set the process mark on ARTICLE and update the summary line."
8795   (setq gnus-newsgroup-processable
8796         (cons article
8797               (delq article gnus-newsgroup-processable)))
8798   (when (gnus-summary-goto-subject article)
8799     (gnus-summary-show-thread)
8800     (gnus-summary-goto-subject article)
8801     (gnus-summary-update-secondary-mark article)))
8802
8803 (defun gnus-summary-remove-process-mark (article)
8804   "Remove the process mark from ARTICLE and update the summary line."
8805   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8806   (when (gnus-summary-goto-subject article)
8807     (gnus-summary-show-thread)
8808     (gnus-summary-goto-subject article)
8809     (gnus-summary-update-secondary-mark article)))
8810
8811 (defun gnus-summary-set-saved-mark (article)
8812   "Set the process mark on ARTICLE and update the summary line."
8813   (push article gnus-newsgroup-saved)
8814   (when (gnus-summary-goto-subject article)
8815     (gnus-summary-update-secondary-mark article)))
8816
8817 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8818   "Mark N articles as read forwards.
8819 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8820 The difference between N and the actual number of articles marked is
8821 returned.
8822 Iff NO-EXPIRE, auto-expiry will be inhibited."
8823   (interactive "p")
8824   (gnus-summary-show-thread)
8825   (let ((backward (< n 0))
8826         (gnus-summary-goto-unread
8827          (and gnus-summary-goto-unread
8828               (not (eq gnus-summary-goto-unread 'never))
8829               (not (memq mark (list gnus-unread-mark
8830                                     gnus-ticked-mark gnus-dormant-mark)))))
8831         (n (abs n))
8832         (mark (or mark gnus-del-mark)))
8833     (while (and (> n 0)
8834                 (gnus-summary-mark-article nil mark no-expire)
8835                 (zerop (gnus-summary-next-subject
8836                         (if backward -1 1)
8837                         (and gnus-summary-goto-unread
8838                              (not (eq gnus-summary-goto-unread 'never)))
8839                         t)))
8840       (setq n (1- n)))
8841     (when (/= 0 n)
8842       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8843     (gnus-summary-recenter)
8844     (gnus-summary-position-point)
8845     (gnus-set-mode-line 'summary)
8846     n))
8847
8848 (defun gnus-summary-mark-article-as-read (mark)
8849   "Mark the current article quickly as read with MARK."
8850   (let ((article (gnus-summary-article-number)))
8851     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8852     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8853     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8854     (push (cons article mark) gnus-newsgroup-reads)
8855     ;; Possibly remove from cache, if that is used.
8856     (when gnus-use-cache
8857       (gnus-cache-enter-remove-article article))
8858     ;; Allow the backend to change the mark.
8859     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8860     ;; Check for auto-expiry.
8861     (when (and gnus-newsgroup-auto-expire
8862                (memq mark gnus-auto-expirable-marks))
8863       (setq mark gnus-expirable-mark)
8864       ;; Let the backend know about the mark change.
8865       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8866       (push article gnus-newsgroup-expirable))
8867     ;; Set the mark in the buffer.
8868     (gnus-summary-update-mark mark 'unread)
8869     t))
8870
8871 (defun gnus-summary-mark-article-as-unread (mark)
8872   "Mark the current article quickly as unread with MARK."
8873   (let* ((article (gnus-summary-article-number))
8874          (old-mark (gnus-summary-article-mark article)))
8875     ;; Allow the backend to change the mark.
8876     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8877     (if (eq mark old-mark)
8878         t
8879       (if (<= article 0)
8880           (progn
8881             (gnus-error 1 "Can't mark negative article numbers")
8882             nil)
8883         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8884         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8885         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8886         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8887         (cond ((= mark gnus-ticked-mark)
8888                (push article gnus-newsgroup-marked))
8889               ((= mark gnus-dormant-mark)
8890                (push article gnus-newsgroup-dormant))
8891               (t
8892                (push article gnus-newsgroup-unreads)))
8893         (gnus-pull article gnus-newsgroup-reads)
8894
8895         ;; See whether the article is to be put in the cache.
8896         (and gnus-use-cache
8897              (vectorp (gnus-summary-article-header article))
8898              (save-excursion
8899                (gnus-cache-possibly-enter-article
8900                 gnus-newsgroup-name article
8901                 (gnus-summary-article-header article)
8902                 (= mark gnus-ticked-mark)
8903                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8904
8905         ;; Fix the mark.
8906         (gnus-summary-update-mark mark 'unread)
8907         t))))
8908
8909 (defun gnus-summary-mark-article (&optional article mark no-expire)
8910   "Mark ARTICLE with MARK.  MARK can be any character.
8911 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8912 `??' (dormant) and `?E' (expirable).
8913 If MARK is nil, then the default character `?r' is used.
8914 If ARTICLE is nil, then the article on the current line will be
8915 marked.
8916 Iff NO-EXPIRE, auto-expiry will be inhibited."
8917   ;; The mark might be a string.
8918   (when (stringp mark)
8919     (setq mark (aref mark 0)))
8920   ;; If no mark is given, then we check auto-expiring.
8921   (when (null mark)
8922     (setq mark gnus-del-mark))
8923   (when (and (not no-expire)
8924              gnus-newsgroup-auto-expire
8925              (memq mark gnus-auto-expirable-marks))
8926     (setq mark gnus-expirable-mark))
8927   (let ((article (or article (gnus-summary-article-number)))
8928         (old-mark (gnus-summary-article-mark article)))
8929     ;; Allow the backend to change the mark.
8930     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8931     (if (eq mark old-mark)
8932         t
8933       (unless article
8934         (error "No article on current line"))
8935       (if (not (if (or (= mark gnus-unread-mark)
8936                        (= mark gnus-ticked-mark)
8937                        (= mark gnus-dormant-mark))
8938                    (gnus-mark-article-as-unread article mark)
8939                  (gnus-mark-article-as-read article mark)))
8940           t
8941         ;; See whether the article is to be put in the cache.
8942         (and gnus-use-cache
8943              (not (= mark gnus-canceled-mark))
8944              (vectorp (gnus-summary-article-header article))
8945              (save-excursion
8946                (gnus-cache-possibly-enter-article
8947                 gnus-newsgroup-name article
8948                 (gnus-summary-article-header article)
8949                 (= mark gnus-ticked-mark)
8950                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8951
8952         (when (gnus-summary-goto-subject article nil t)
8953           (let ((buffer-read-only nil))
8954             (gnus-summary-show-thread)
8955             ;; Fix the mark.
8956             (gnus-summary-update-mark mark 'unread)
8957             t))))))
8958
8959 (defun gnus-summary-update-secondary-mark (article)
8960   "Update the secondary (read, process, cache) mark."
8961   (gnus-summary-update-mark
8962    (cond ((memq article gnus-newsgroup-processable)
8963           gnus-process-mark)
8964          ((memq article gnus-newsgroup-cached)
8965           gnus-cached-mark)
8966          ((memq article gnus-newsgroup-replied)
8967           gnus-replied-mark)
8968          ((memq article gnus-newsgroup-forwarded)
8969           gnus-forwarded-mark)
8970          ((memq article gnus-newsgroup-saved)
8971           gnus-saved-mark)
8972          (t gnus-no-mark))
8973    'replied)
8974   (when (gnus-visual-p 'summary-highlight 'highlight)
8975     (gnus-run-hooks 'gnus-summary-update-hook))
8976   t)
8977
8978 (defun gnus-summary-update-mark (mark type)
8979   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8980         (buffer-read-only nil))
8981     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8982     (when forward
8983       (when (looking-at "\r")
8984         (incf forward))
8985       (when (<= (+ forward (point)) (point-max))
8986         ;; Go to the right position on the line.
8987         (goto-char (+ forward (point)))
8988         ;; Replace the old mark with the new mark.
8989         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8990         ;; Optionally update the marks by some user rule.
8991         (when (eq type 'unread)
8992           (gnus-data-set-mark
8993            (gnus-data-find (gnus-summary-article-number)) mark)
8994           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8995
8996 (defun gnus-mark-article-as-read (article &optional mark)
8997   "Enter ARTICLE in the pertinent lists and remove it from others."
8998   ;; Make the article expirable.
8999   (let ((mark (or mark gnus-del-mark)))
9000     (if (= mark gnus-expirable-mark)
9001         (push article gnus-newsgroup-expirable)
9002       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9003     ;; Remove from unread and marked lists.
9004     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9005     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9006     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9007     (push (cons article mark) gnus-newsgroup-reads)
9008     ;; Possibly remove from cache, if that is used.
9009     (when gnus-use-cache
9010       (gnus-cache-enter-remove-article article))
9011     t))
9012
9013 (defun gnus-mark-article-as-unread (article &optional mark)
9014   "Enter ARTICLE in the pertinent lists and remove it from others."
9015   (let ((mark (or mark gnus-ticked-mark)))
9016     (if (<= article 0)
9017         (progn
9018           (gnus-error 1 "Can't mark negative article numbers")
9019           nil)
9020       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9021             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9022             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9023             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9024
9025       ;; Unsuppress duplicates?
9026       (when gnus-suppress-duplicates
9027         (gnus-dup-unsuppress-article article))
9028
9029       (cond ((= mark gnus-ticked-mark)
9030              (push article gnus-newsgroup-marked))
9031             ((= mark gnus-dormant-mark)
9032              (push article gnus-newsgroup-dormant))
9033             (t
9034              (push article gnus-newsgroup-unreads)))
9035       (gnus-pull article gnus-newsgroup-reads)
9036       t)))
9037
9038 (defalias 'gnus-summary-mark-as-unread-forward
9039   'gnus-summary-tick-article-forward)
9040 (make-obsolete 'gnus-summary-mark-as-unread-forward
9041                'gnus-summary-tick-article-forward)
9042 (defun gnus-summary-tick-article-forward (n)
9043   "Tick N articles forwards.
9044 If N is negative, tick backwards instead.
9045 The difference between N and the number of articles ticked is returned."
9046   (interactive "p")
9047   (gnus-summary-mark-forward n gnus-ticked-mark))
9048
9049 (defalias 'gnus-summary-mark-as-unread-backward
9050   'gnus-summary-tick-article-backward)
9051 (make-obsolete 'gnus-summary-mark-as-unread-backward
9052                'gnus-summary-tick-article-backward)
9053 (defun gnus-summary-tick-article-backward (n)
9054   "Tick N articles backwards.
9055 The difference between N and the number of articles ticked is returned."
9056   (interactive "p")
9057   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9058
9059 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9060 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9061 (defun gnus-summary-tick-article (&optional article clear-mark)
9062   "Mark current article as unread.
9063 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9064 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9065   (interactive)
9066   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9067                                        gnus-ticked-mark)))
9068
9069 (defun gnus-summary-mark-as-read-forward (n)
9070   "Mark N articles as read forwards.
9071 If N is negative, mark backwards instead.
9072 The difference between N and the actual number of articles marked is
9073 returned."
9074   (interactive "p")
9075   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9076
9077 (defun gnus-summary-mark-as-read-backward (n)
9078   "Mark the N articles as read backwards.
9079 The difference between N and the actual number of articles marked is
9080 returned."
9081   (interactive "p")
9082   (gnus-summary-mark-forward
9083    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9084
9085 (defun gnus-summary-mark-as-read (&optional article mark)
9086   "Mark current article as read.
9087 ARTICLE specifies the article to be marked as read.
9088 MARK specifies a string to be inserted at the beginning of the line."
9089   (gnus-summary-mark-article article mark))
9090
9091 (defun gnus-summary-clear-mark-forward (n)
9092   "Clear marks from N articles forward.
9093 If N is negative, clear backward instead.
9094 The difference between N and the number of marks cleared is returned."
9095   (interactive "p")
9096   (gnus-summary-mark-forward n gnus-unread-mark))
9097
9098 (defun gnus-summary-clear-mark-backward (n)
9099   "Clear marks from N articles backward.
9100 The difference between N and the number of marks cleared is returned."
9101   (interactive "p")
9102   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9103
9104 (defun gnus-summary-mark-unread-as-read ()
9105   "Intended to be used by `gnus-summary-mark-article-hook'."
9106   (when (memq gnus-current-article gnus-newsgroup-unreads)
9107     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9108
9109 (defun gnus-summary-mark-read-and-unread-as-read ()
9110   "Intended to be used by `gnus-summary-mark-article-hook'."
9111   (let ((mark (gnus-summary-article-mark)))
9112     (when (or (gnus-unread-mark-p mark)
9113               (gnus-read-mark-p mark))
9114       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9115
9116 (defun gnus-summary-mark-unread-as-ticked ()
9117   "Intended to be used by `gnus-summary-mark-article-hook'."
9118   (when (memq gnus-current-article gnus-newsgroup-unreads)
9119     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9120
9121 (defun gnus-summary-mark-region-as-read (point mark all)
9122   "Mark all unread articles between point and mark as read.
9123 If given a prefix, mark all articles between point and mark as read,
9124 even ticked and dormant ones."
9125   (interactive "r\nP")
9126   (save-excursion
9127     (let (article)
9128       (goto-char point)
9129       (beginning-of-line)
9130       (while (and
9131               (< (point) mark)
9132               (progn
9133                 (when (or all
9134                           (memq (setq article (gnus-summary-article-number))
9135                                 gnus-newsgroup-unreads))
9136                   (gnus-summary-mark-article article gnus-del-mark))
9137                 t)
9138               (gnus-summary-find-next))))))
9139
9140 (defun gnus-summary-mark-below (score mark)
9141   "Mark articles with score less than SCORE with MARK."
9142   (interactive "P\ncMark: ")
9143   (setq score (if score
9144                   (prefix-numeric-value score)
9145                 (or gnus-summary-default-score 0)))
9146   (save-excursion
9147     (set-buffer gnus-summary-buffer)
9148     (goto-char (point-min))
9149     (while
9150         (progn
9151           (and (< (gnus-summary-article-score) score)
9152                (gnus-summary-mark-article nil mark))
9153           (gnus-summary-find-next)))))
9154
9155 (defun gnus-summary-kill-below (&optional score)
9156   "Mark articles with score below SCORE as read."
9157   (interactive "P")
9158   (gnus-summary-mark-below score gnus-killed-mark))
9159
9160 (defun gnus-summary-clear-above (&optional score)
9161   "Clear all marks from articles with score above SCORE."
9162   (interactive "P")
9163   (gnus-summary-mark-above score gnus-unread-mark))
9164
9165 (defun gnus-summary-tick-above (&optional score)
9166   "Tick all articles with score above SCORE."
9167   (interactive "P")
9168   (gnus-summary-mark-above score gnus-ticked-mark))
9169
9170 (defun gnus-summary-mark-above (score mark)
9171   "Mark articles with score over SCORE with MARK."
9172   (interactive "P\ncMark: ")
9173   (setq score (if score
9174                   (prefix-numeric-value score)
9175                 (or gnus-summary-default-score 0)))
9176   (save-excursion
9177     (set-buffer gnus-summary-buffer)
9178     (goto-char (point-min))
9179     (while (and (progn
9180                   (when (> (gnus-summary-article-score) score)
9181                     (gnus-summary-mark-article nil mark))
9182                   t)
9183                 (gnus-summary-find-next)))))
9184
9185 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9186 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9187 (defun gnus-summary-limit-include-expunged (&optional no-error)
9188   "Display all the hidden articles that were expunged for low scores."
9189   (interactive)
9190   (let ((buffer-read-only nil))
9191     (let ((scored gnus-newsgroup-scored)
9192           headers h)
9193       (while scored
9194         (unless (gnus-summary-article-header (caar scored))
9195           (and (setq h (gnus-number-to-header (caar scored)))
9196                (< (cdar scored) gnus-summary-expunge-below)
9197                (push h headers)))
9198         (setq scored (cdr scored)))
9199       (if (not headers)
9200           (when (not no-error)
9201             (error "No expunged articles hidden"))
9202         (goto-char (point-min))
9203         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9204         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9205         (mapcar (lambda (x) (push (mail-header-number x)
9206                                   gnus-newsgroup-limit))
9207                 headers)
9208         (gnus-summary-prepare-unthreaded (nreverse headers))
9209         (goto-char (point-min))
9210         (gnus-summary-position-point)
9211         t))))
9212
9213 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9214   "Mark all unread articles in this newsgroup as read.
9215 If prefix argument ALL is non-nil, ticked and dormant articles will
9216 also be marked as read.
9217 If QUIETLY is non-nil, no questions will be asked.
9218 If TO-HERE is non-nil, it should be a point in the buffer.  All
9219 articles before (after, if REVERSE is set) this point will be marked as read.
9220 Note that this function will only catch up the unread article
9221 in the current summary buffer limitation.
9222 The number of articles marked as read is returned."
9223   (interactive "P")
9224   (prog1
9225       (save-excursion
9226         (when (or quietly
9227                   (not gnus-interactive-catchup) ;Without confirmation?
9228                   gnus-expert-user
9229                   (gnus-y-or-n-p
9230                    (if all
9231                        "Mark absolutely all articles as read? "
9232                      "Mark all unread articles as read? ")))
9233           (if (and not-mark
9234                    (not gnus-newsgroup-adaptive)
9235                    (not gnus-newsgroup-auto-expire)
9236                    (not gnus-suppress-duplicates)
9237                    (or (not gnus-use-cache)
9238                        (eq gnus-use-cache 'passive)))
9239               (progn
9240                 (when all
9241                   (setq gnus-newsgroup-marked nil
9242                         gnus-newsgroup-dormant nil))
9243                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9244             ;; We actually mark all articles as canceled, which we
9245             ;; have to do when using auto-expiry or adaptive scoring.
9246             (gnus-summary-show-all-threads)
9247             (if (and to-here reverse)
9248                 (progn
9249                   (goto-char to-here)
9250                   (while (and
9251                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9252                           (gnus-summary-find-next (not all) nil nil t))))
9253               (when (gnus-summary-first-subject (not all) t)
9254                 (while (and
9255                         (if to-here (< (point) to-here) t)
9256                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9257                         (gnus-summary-find-next (not all) nil nil t)))))
9258             (gnus-set-mode-line 'summary))
9259           t))
9260     (gnus-summary-position-point)))
9261
9262 (defun gnus-summary-catchup-to-here (&optional all)
9263   "Mark all unticked articles before the current one as read.
9264 If ALL is non-nil, also mark ticked and dormant articles as read."
9265   (interactive "P")
9266   (save-excursion
9267     (gnus-save-hidden-threads
9268       (let ((beg (point)))
9269         ;; We check that there are unread articles.
9270         (when (or all (gnus-summary-find-prev))
9271           (gnus-summary-catchup all t beg)))))
9272   (gnus-summary-position-point))
9273
9274 (defun gnus-summary-catchup-from-here (&optional all)
9275   "Mark all unticked articles after the current one as read.
9276 If ALL is non-nil, also mark ticked and dormant articles as read."
9277   (interactive "P")
9278   (save-excursion
9279     (gnus-save-hidden-threads
9280       (let ((beg (point)))
9281         ;; We check that there are unread articles.
9282         (when (or all (gnus-summary-find-next))
9283           (gnus-summary-catchup all t beg nil t)))))
9284   (gnus-summary-position-point))
9285
9286 (defun gnus-summary-catchup-all (&optional quietly)
9287   "Mark all articles in this newsgroup as read."
9288   (interactive "P")
9289   (gnus-summary-catchup t quietly))
9290
9291 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9292   "Mark all unread articles in this group as read, then exit.
9293 If prefix argument ALL is non-nil, all articles are marked as read.
9294 If QUIETLY is non-nil, no questions will be asked."
9295   (interactive "P")
9296   (when (gnus-summary-catchup all quietly nil 'fast)
9297     ;; Select next newsgroup or exit.
9298     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9299              (eq gnus-auto-select-next 'quietly))
9300         (gnus-summary-next-group nil)
9301       (gnus-summary-exit))))
9302
9303 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9304   "Mark all articles in this newsgroup as read, and then exit."
9305   (interactive "P")
9306   (gnus-summary-catchup-and-exit t quietly))
9307
9308 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9309   "Mark all articles in this group as read and select the next group.
9310 If given a prefix, mark all articles, unread as well as ticked, as
9311 read."
9312   (interactive "P")
9313   (save-excursion
9314     (gnus-summary-catchup all))
9315   (gnus-summary-next-group))
9316
9317 ;;;
9318 ;;; with article
9319 ;;;
9320
9321 (defmacro gnus-with-article (article &rest forms)
9322   "Select ARTICLE and perform FORMS in the original article buffer.
9323 Then replace the article with the result."
9324   `(progn
9325      ;; We don't want the article to be marked as read.
9326      (let (gnus-mark-article-hook)
9327        (gnus-summary-select-article t t nil ,article))
9328      (set-buffer gnus-original-article-buffer)
9329      ,@forms
9330      (if (not (gnus-check-backend-function
9331                'request-replace-article (car gnus-article-current)))
9332          (gnus-message 5 "Read-only group; not replacing")
9333        (unless (gnus-request-replace-article
9334                 ,article (car gnus-article-current)
9335                 (current-buffer) t)
9336          (error "Couldn't replace article")))
9337      ;; The cache and backlog have to be flushed somewhat.
9338      (when gnus-keep-backlog
9339        (gnus-backlog-remove-article
9340         (car gnus-article-current) (cdr gnus-article-current)))
9341      (when gnus-use-cache
9342        (gnus-cache-update-article
9343         (car gnus-article-current) (cdr gnus-article-current)))))
9344
9345 (put 'gnus-with-article 'lisp-indent-function 1)
9346 (put 'gnus-with-article 'edebug-form-spec '(form body))
9347
9348 ;; Thread-based commands.
9349
9350 (defun gnus-summary-articles-in-thread (&optional article)
9351   "Return a list of all articles in the current thread.
9352 If ARTICLE is non-nil, return all articles in the thread that starts
9353 with that article."
9354   (let* ((article (or article (gnus-summary-article-number)))
9355          (data (gnus-data-find-list article))
9356          (top-level (gnus-data-level (car data)))
9357          (top-subject
9358           (cond ((null gnus-thread-operation-ignore-subject)
9359                  (gnus-simplify-subject-re
9360                   (mail-header-subject (gnus-data-header (car data)))))
9361                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9362                  (gnus-simplify-subject-fuzzy
9363                   (mail-header-subject (gnus-data-header (car data)))))
9364                 (t nil)))
9365          (end-point (save-excursion
9366                       (if (gnus-summary-go-to-next-thread)
9367                           (point) (point-max))))
9368          articles)
9369     (while (and data
9370                 (< (gnus-data-pos (car data)) end-point))
9371       (when (or (not top-subject)
9372                 (string= top-subject
9373                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9374                              (gnus-simplify-subject-fuzzy
9375                               (mail-header-subject
9376                                (gnus-data-header (car data))))
9377                            (gnus-simplify-subject-re
9378                             (mail-header-subject
9379                              (gnus-data-header (car data)))))))
9380         (push (gnus-data-number (car data)) articles))
9381       (unless (and (setq data (cdr data))
9382                    (> (gnus-data-level (car data)) top-level))
9383         (setq data nil)))
9384     ;; Return the list of articles.
9385     (nreverse articles)))
9386
9387 (defun gnus-summary-rethread-current ()
9388   "Rethread the thread the current article is part of."
9389   (interactive)
9390   (let* ((gnus-show-threads t)
9391          (article (gnus-summary-article-number))
9392          (id (mail-header-id (gnus-summary-article-header)))
9393          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9394     (unless id
9395       (error "No article on the current line"))
9396     (gnus-rebuild-thread id)
9397     (gnus-summary-goto-subject article)))
9398
9399 (defun gnus-summary-reparent-thread ()
9400   "Make the current article child of the marked (or previous) article.
9401
9402 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9403 is non-nil or the Subject: of both articles are the same."
9404   (interactive)
9405   (unless (not (gnus-group-read-only-p))
9406     (error "The current newsgroup does not support article editing"))
9407   (unless (<= (length gnus-newsgroup-processable) 1)
9408     (error "No more than one article may be marked"))
9409   (save-window-excursion
9410     (let ((gnus-article-buffer " *reparent*")
9411           (current-article (gnus-summary-article-number))
9412           ;; First grab the marked article, otherwise one line up.
9413           (parent-article (if (not (null gnus-newsgroup-processable))
9414                               (car gnus-newsgroup-processable)
9415                             (save-excursion
9416                               (if (eq (forward-line -1) 0)
9417                                   (gnus-summary-article-number)
9418                                 (error "Beginning of summary buffer"))))))
9419       (unless (not (eq current-article parent-article))
9420         (error "An article may not be self-referential"))
9421       (let ((message-id (mail-header-id
9422                          (gnus-summary-article-header parent-article))))
9423         (unless (and message-id (not (equal message-id "")))
9424           (error "No message-id in desired parent"))
9425         (gnus-with-article current-article
9426           (save-restriction
9427             (goto-char (point-min))
9428             (message-narrow-to-head)
9429             (if (re-search-forward "^References: " nil t)
9430                 (progn
9431                   (re-search-forward "^[^ \t]" nil t)
9432                   (forward-line -1)
9433                   (end-of-line)
9434                   (insert " " message-id))
9435               (insert "References: " message-id "\n"))))
9436         (set-buffer gnus-summary-buffer)
9437         (gnus-summary-unmark-all-processable)
9438         (gnus-summary-update-article current-article)
9439         (gnus-summary-rethread-current)
9440         (gnus-message 3 "Article %d is now the child of article %d"
9441                       current-article parent-article)))))
9442
9443 (defun gnus-summary-toggle-threads (&optional arg)
9444   "Toggle showing conversation threads.
9445 If ARG is positive number, turn showing conversation threads on."
9446   (interactive "P")
9447   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9448     (setq gnus-show-threads
9449           (if (null arg) (not gnus-show-threads)
9450             (> (prefix-numeric-value arg) 0)))
9451     (gnus-summary-prepare)
9452     (gnus-summary-goto-subject current)
9453     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9454     (gnus-summary-position-point)))
9455
9456 (defun gnus-summary-show-all-threads ()
9457   "Show all threads."
9458   (interactive)
9459   (save-excursion
9460     (let ((buffer-read-only nil))
9461       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9462   (gnus-summary-position-point))
9463
9464 (defun gnus-summary-show-thread ()
9465   "Show thread subtrees.
9466 Returns nil if no thread was there to be shown."
9467   (interactive)
9468   (let ((buffer-read-only nil)
9469         (orig (point))
9470         ;; first goto end then to beg, to have point at beg after let
9471         (end (progn (end-of-line) (point)))
9472         (beg (progn (beginning-of-line) (point))))
9473     (prog1
9474         ;; Any hidden lines here?
9475         (search-forward "\r" end t)
9476       (subst-char-in-region beg end ?\^M ?\n t)
9477       (goto-char orig)
9478       (gnus-summary-position-point))))
9479
9480 (defun gnus-summary-hide-all-threads ()
9481   "Hide all thread subtrees."
9482   (interactive)
9483   (save-excursion
9484     (goto-char (point-min))
9485     (gnus-summary-hide-thread)
9486     (while (zerop (gnus-summary-next-thread 1 t))
9487       (gnus-summary-hide-thread)))
9488   (gnus-summary-position-point))
9489
9490 (defun gnus-summary-hide-thread ()
9491   "Hide thread subtrees.
9492 Returns nil if no threads were there to be hidden."
9493   (interactive)
9494   (let ((buffer-read-only nil)
9495         (start (point))
9496         (article (gnus-summary-article-number)))
9497     (goto-char start)
9498     ;; Go forward until either the buffer ends or the subthread
9499     ;; ends.
9500     (when (and (not (eobp))
9501                (or (zerop (gnus-summary-next-thread 1 t))
9502                    (goto-char (point-max))))
9503       (prog1
9504           (if (and (> (point) start)
9505                    (search-backward "\n" start t))
9506               (progn
9507                 (subst-char-in-region start (point) ?\n ?\^M)
9508                 (gnus-summary-goto-subject article))
9509             (goto-char start)
9510             nil)))))
9511
9512 (defun gnus-summary-go-to-next-thread (&optional previous)
9513   "Go to the same level (or less) next thread.
9514 If PREVIOUS is non-nil, go to previous thread instead.
9515 Return the article number moved to, or nil if moving was impossible."
9516   (let ((level (gnus-summary-thread-level))
9517         (way (if previous -1 1))
9518         (beg (point)))
9519     (forward-line way)
9520     (while (and (not (eobp))
9521                 (< level (gnus-summary-thread-level)))
9522       (forward-line way))
9523     (if (eobp)
9524         (progn
9525           (goto-char beg)
9526           nil)
9527       (setq beg (point))
9528       (prog1
9529           (gnus-summary-article-number)
9530         (goto-char beg)))))
9531
9532 (defun gnus-summary-next-thread (n &optional silent)
9533   "Go to the same level next N'th thread.
9534 If N is negative, search backward instead.
9535 Returns the difference between N and the number of skips actually
9536 done.
9537
9538 If SILENT, don't output messages."
9539   (interactive "p")
9540   (let ((backward (< n 0))
9541         (n (abs n)))
9542     (while (and (> n 0)
9543                 (gnus-summary-go-to-next-thread backward))
9544       (decf n))
9545     (unless silent
9546       (gnus-summary-position-point))
9547     (when (and (not silent) (/= 0 n))
9548       (gnus-message 7 "No more threads"))
9549     n))
9550
9551 (defun gnus-summary-prev-thread (n)
9552   "Go to the same level previous N'th thread.
9553 Returns the difference between N and the number of skips actually
9554 done."
9555   (interactive "p")
9556   (gnus-summary-next-thread (- n)))
9557
9558 (defun gnus-summary-go-down-thread ()
9559   "Go down one level in the current thread."
9560   (let ((children (gnus-summary-article-children)))
9561     (when children
9562       (gnus-summary-goto-subject (car children)))))
9563
9564 (defun gnus-summary-go-up-thread ()
9565   "Go up one level in the current thread."
9566   (let ((parent (gnus-summary-article-parent)))
9567     (when parent
9568       (gnus-summary-goto-subject parent))))
9569
9570 (defun gnus-summary-down-thread (n)
9571   "Go down thread N steps.
9572 If N is negative, go up instead.
9573 Returns the difference between N and how many steps down that were
9574 taken."
9575   (interactive "p")
9576   (let ((up (< n 0))
9577         (n (abs n)))
9578     (while (and (> n 0)
9579                 (if up (gnus-summary-go-up-thread)
9580                   (gnus-summary-go-down-thread)))
9581       (setq n (1- n)))
9582     (gnus-summary-position-point)
9583     (when (/= 0 n)
9584       (gnus-message 7 "Can't go further"))
9585     n))
9586
9587 (defun gnus-summary-up-thread (n)
9588   "Go up thread N steps.
9589 If N is negative, go down instead.
9590 Returns the difference between N and how many steps down that were
9591 taken."
9592   (interactive "p")
9593   (gnus-summary-down-thread (- n)))
9594
9595 (defun gnus-summary-top-thread ()
9596   "Go to the top of the thread."
9597   (interactive)
9598   (while (gnus-summary-go-up-thread))
9599   (gnus-summary-article-number))
9600
9601 (defun gnus-summary-kill-thread (&optional unmark)
9602   "Mark articles under current thread as read.
9603 If the prefix argument is positive, remove any kinds of marks.
9604 If the prefix argument is negative, tick articles instead."
9605   (interactive "P")
9606   (when unmark
9607     (setq unmark (prefix-numeric-value unmark)))
9608   (let ((articles (gnus-summary-articles-in-thread)))
9609     (save-excursion
9610       ;; Expand the thread.
9611       (gnus-summary-show-thread)
9612       ;; Mark all the articles.
9613       (while articles
9614         (gnus-summary-goto-subject (car articles))
9615         (cond ((null unmark)
9616                (gnus-summary-mark-article-as-read gnus-killed-mark))
9617               ((> unmark 0)
9618                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9619               (t
9620                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9621         (setq articles (cdr articles))))
9622     ;; Hide killed subtrees.
9623     (and (null unmark)
9624          gnus-thread-hide-killed
9625          (gnus-summary-hide-thread))
9626     ;; If marked as read, go to next unread subject.
9627     (when (null unmark)
9628       ;; Go to next unread subject.
9629       (gnus-summary-next-subject 1 t)))
9630   (gnus-set-mode-line 'summary))
9631
9632 ;; Summary sorting commands
9633
9634 (defun gnus-summary-sort-by-number (&optional reverse)
9635   "Sort the summary buffer by article number.
9636 Argument REVERSE means reverse order."
9637   (interactive "P")
9638   (gnus-summary-sort 'number reverse))
9639
9640 (defun gnus-summary-sort-by-author (&optional reverse)
9641   "Sort the summary buffer by author name alphabetically.
9642 If `case-fold-search' is non-nil, case of letters is ignored.
9643 Argument REVERSE means reverse order."
9644   (interactive "P")
9645   (gnus-summary-sort 'author reverse))
9646
9647 (defun gnus-summary-sort-by-subject (&optional reverse)
9648   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9649 If `case-fold-search' is non-nil, case of letters is ignored.
9650 Argument REVERSE means reverse order."
9651   (interactive "P")
9652   (gnus-summary-sort 'subject reverse))
9653
9654 (defun gnus-summary-sort-by-date (&optional reverse)
9655   "Sort the summary buffer by date.
9656 Argument REVERSE means reverse order."
9657   (interactive "P")
9658   (gnus-summary-sort 'date reverse))
9659
9660 (defun gnus-summary-sort-by-score (&optional reverse)
9661   "Sort the summary buffer by score.
9662 Argument REVERSE means reverse order."
9663   (interactive "P")
9664   (gnus-summary-sort 'score reverse))
9665
9666 (defun gnus-summary-sort-by-lines (&optional reverse)
9667   "Sort the summary buffer by the number of lines.
9668 Argument REVERSE means reverse order."
9669   (interactive "P")
9670   (gnus-summary-sort 'lines reverse))
9671
9672 (defun gnus-summary-sort-by-chars (&optional reverse)
9673   "Sort the summary buffer by article length.
9674 Argument REVERSE means reverse order."
9675   (interactive "P")
9676   (gnus-summary-sort 'chars reverse))
9677
9678 (defun gnus-summary-sort-by-original (&optional reverse)
9679   "Sort the summary buffer using the default sorting method.
9680 Argument REVERSE means reverse order."
9681   (interactive "P")
9682   (let* ((buffer-read-only)
9683          (gnus-summary-prepare-hook nil))
9684     ;; We do the sorting by regenerating the threads.
9685     (gnus-summary-prepare)
9686     ;; Hide subthreads if needed.
9687     (when (and gnus-show-threads gnus-thread-hide-subtree)
9688       (gnus-summary-hide-all-threads))))
9689
9690 (defun gnus-summary-sort (predicate reverse)
9691   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9692   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9693          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9694          (gnus-thread-sort-functions
9695           (if (not reverse)
9696               thread
9697             `(lambda (t1 t2)
9698                (,thread t2 t1))))
9699          (gnus-sort-gathered-threads-function
9700           gnus-thread-sort-functions)
9701          (gnus-article-sort-functions
9702           (if (not reverse)
9703               article
9704             `(lambda (t1 t2)
9705                (,article t2 t1))))
9706          (buffer-read-only)
9707          (gnus-summary-prepare-hook nil))
9708     ;; We do the sorting by regenerating the threads.
9709     (gnus-summary-prepare)
9710     ;; Hide subthreads if needed.
9711     (when (and gnus-show-threads gnus-thread-hide-subtree)
9712       (gnus-summary-hide-all-threads))))
9713
9714 ;; Summary saving commands.
9715
9716 (defun gnus-summary-save-article (&optional n not-saved)
9717   "Save the current article using the default saver function.
9718 If N is a positive number, save the N next articles.
9719 If N is a negative number, save the N previous articles.
9720 If N is nil and any articles have been marked with the process mark,
9721 save those articles instead.
9722 The variable `gnus-default-article-saver' specifies the saver function."
9723   (interactive "P")
9724   (let* ((articles (gnus-summary-work-articles n))
9725          (save-buffer (save-excursion
9726                         (nnheader-set-temp-buffer " *Gnus Save*")))
9727          (num (length articles))
9728          header file)
9729     (dolist (article articles)
9730       (setq header (gnus-summary-article-header article))
9731       (if (not (vectorp header))
9732           ;; This is a pseudo-article.
9733           (if (assq 'name header)
9734               (gnus-copy-file (cdr (assq 'name header)))
9735             (gnus-message 1 "Article %d is unsaveable" article))
9736         ;; This is a real article.
9737         (save-window-excursion
9738           (gnus-summary-select-article t nil nil article))
9739         (save-excursion
9740           (set-buffer save-buffer)
9741           (erase-buffer)
9742           (insert-buffer-substring gnus-original-article-buffer))
9743         (setq file (gnus-article-save save-buffer file num))
9744         (gnus-summary-remove-process-mark article)
9745         (unless not-saved
9746           (gnus-summary-set-saved-mark article))))
9747     (gnus-kill-buffer save-buffer)
9748     (gnus-summary-position-point)
9749     (gnus-set-mode-line 'summary)
9750     n))
9751
9752 (defun gnus-summary-pipe-output (&optional arg)
9753   "Pipe the current article to a subprocess.
9754 If N is a positive number, pipe the N next articles.
9755 If N is a negative number, pipe the N previous articles.
9756 If N is nil and any articles have been marked with the process mark,
9757 pipe those articles instead."
9758   (interactive "P")
9759   (require 'gnus-art)
9760   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9761     (gnus-summary-save-article arg t))
9762   (let ((buffer (get-buffer "*Shell Command Output*")))
9763     (if (and buffer
9764              (with-current-buffer buffer (> (point-max) (point-min))))
9765         (gnus-configure-windows 'pipe))))
9766
9767 (defun gnus-summary-save-article-mail (&optional arg)
9768   "Append the current article to an mail file.
9769 If N is a positive number, save the N next articles.
9770 If N is a negative number, save the N previous articles.
9771 If N is nil and any articles have been marked with the process mark,
9772 save those articles instead."
9773   (interactive "P")
9774   (require 'gnus-art)
9775   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9776     (gnus-summary-save-article arg)))
9777
9778 (defun gnus-summary-save-article-rmail (&optional arg)
9779   "Append the current article to an rmail file.
9780 If N is a positive number, save the N next articles.
9781 If N is a negative number, save the N previous articles.
9782 If N is nil and any articles have been marked with the process mark,
9783 save those articles instead."
9784   (interactive "P")
9785   (require 'gnus-art)
9786   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9787     (gnus-summary-save-article arg)))
9788
9789 (defun gnus-summary-save-article-file (&optional arg)
9790   "Append the current article to a file.
9791 If N is a positive number, save the N next articles.
9792 If N is a negative number, save the N previous articles.
9793 If N is nil and any articles have been marked with the process mark,
9794 save those articles instead."
9795   (interactive "P")
9796   (require 'gnus-art)
9797   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9798     (gnus-summary-save-article arg)))
9799
9800 (defun gnus-summary-write-article-file (&optional arg)
9801   "Write the current article to a file, deleting the previous file.
9802 If N is a positive number, save the N next articles.
9803 If N is a negative number, save the N previous articles.
9804 If N is nil and any articles have been marked with the process mark,
9805 save those articles instead."
9806   (interactive "P")
9807   (require 'gnus-art)
9808   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9809     (gnus-summary-save-article arg)))
9810
9811 (defun gnus-summary-save-article-body-file (&optional arg)
9812   "Append the current article body to a file.
9813 If N is a positive number, save the N next articles.
9814 If N is a negative number, save the N previous articles.
9815 If N is nil and any articles have been marked with the process mark,
9816 save those articles instead."
9817   (interactive "P")
9818   (require 'gnus-art)
9819   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9820     (gnus-summary-save-article arg)))
9821
9822 (defun gnus-summary-pipe-message (program)
9823   "Pipe the current article through PROGRAM."
9824   (interactive "sProgram: ")
9825   (gnus-summary-select-article)
9826   (let ((mail-header-separator ""))
9827     (gnus-eval-in-buffer-window gnus-article-buffer
9828       (save-restriction
9829         (widen)
9830         (let ((start (window-start))
9831               buffer-read-only)
9832           (message-pipe-buffer-body program)
9833           (set-window-start (get-buffer-window (current-buffer)) start))))))
9834
9835 (defun gnus-get-split-value (methods)
9836   "Return a value based on the split METHODS."
9837   (let (split-name method result match)
9838     (when methods
9839       (save-excursion
9840         (set-buffer gnus-original-article-buffer)
9841         (save-restriction
9842           (nnheader-narrow-to-headers)
9843           (while (and methods (not split-name))
9844             (goto-char (point-min))
9845             (setq method (pop methods))
9846             (setq match (car method))
9847             (when (cond
9848                    ((stringp match)
9849                     ;; Regular expression.
9850                     (ignore-errors
9851                       (re-search-forward match nil t)))
9852                    ((gnus-functionp match)
9853                     ;; Function.
9854                     (save-restriction
9855                       (widen)
9856                       (setq result (funcall match gnus-newsgroup-name))))
9857                    ((consp match)
9858                     ;; Form.
9859                     (save-restriction
9860                       (widen)
9861                       (setq result (eval match)))))
9862               (setq split-name (cdr method))
9863               (cond ((stringp result)
9864                      (push (expand-file-name
9865                             result gnus-article-save-directory)
9866                            split-name))
9867                     ((consp result)
9868                      (setq split-name (append result split-name)))))))))
9869     (nreverse split-name)))
9870
9871 (defun gnus-valid-move-group-p (group)
9872   (and (boundp group)
9873        (symbol-name group)
9874        (symbol-value group)
9875        (gnus-get-function (gnus-find-method-for-group
9876                            (symbol-name group)) 'request-accept-article t)))
9877
9878 (defun gnus-read-move-group-name (prompt default articles prefix)
9879   "Read a group name."
9880   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9881          (minibuffer-confirm-incomplete nil) ; XEmacs
9882          (prom
9883           (format "%s %s to:"
9884                   prompt
9885                   (if (> (length articles) 1)
9886                       (format "these %d articles" (length articles))
9887                     "this article")))
9888          (to-newsgroup
9889           (cond
9890            ((null split-name)
9891             (gnus-completing-read default prom
9892                                   gnus-active-hashtb
9893                                   'gnus-valid-move-group-p
9894                                   nil prefix
9895                                   'gnus-group-history))
9896            ((= 1 (length split-name))
9897             (gnus-completing-read (car split-name) prom
9898                                   gnus-active-hashtb
9899                                   'gnus-valid-move-group-p
9900                                   nil nil
9901                                   'gnus-group-history))
9902            (t
9903             (gnus-completing-read nil prom
9904                                   (mapcar (lambda (el) (list el))
9905                                           (nreverse split-name))
9906                                   nil nil nil
9907                                   'gnus-group-history))))
9908          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9909     (when to-newsgroup
9910       (if (or (string= to-newsgroup "")
9911               (string= to-newsgroup prefix))
9912           (setq to-newsgroup default))
9913       (unless to-newsgroup
9914         (error "No group name entered"))
9915       (or (gnus-active to-newsgroup)
9916           (gnus-activate-group to-newsgroup nil nil to-method)
9917           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9918                                      to-newsgroup))
9919               (or (and (gnus-request-create-group to-newsgroup to-method)
9920                        (gnus-activate-group
9921                         to-newsgroup nil nil to-method)
9922                        (gnus-subscribe-group to-newsgroup))
9923                   (error "Couldn't create group %s" to-newsgroup)))
9924           (error "No such group: %s" to-newsgroup)))
9925     to-newsgroup))
9926
9927 (defun gnus-summary-save-parts (type dir n &optional reverse)
9928   "Save parts matching TYPE to DIR.
9929 If REVERSE, save parts that do not match TYPE."
9930   (interactive
9931    (list (read-string "Save parts of type: "
9932                       (or (car gnus-summary-save-parts-type-history)
9933                           gnus-summary-save-parts-default-mime)
9934                       'gnus-summary-save-parts-type-history)
9935          (setq gnus-summary-save-parts-last-directory
9936                (read-file-name "Save to directory: "
9937                                gnus-summary-save-parts-last-directory
9938                                nil t))
9939          current-prefix-arg))
9940   (gnus-summary-iterate n
9941     (let ((gnus-display-mime-function nil)
9942           (gnus-inhibit-treatment t))
9943       (gnus-summary-select-article))
9944     (save-excursion
9945       (set-buffer gnus-article-buffer)
9946       (let ((handles (or gnus-article-mime-handles
9947                          (mm-dissect-buffer) (mm-uu-dissect))))
9948         (when handles
9949           (gnus-summary-save-parts-1 type dir handles reverse)
9950           (unless gnus-article-mime-handles ;; Don't destroy this case.
9951             (mm-destroy-parts handles)))))))
9952
9953 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9954   (if (stringp (car handle))
9955       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9956               (cdr handle))
9957     (when (if reverse
9958               (not (string-match type (mm-handle-media-type handle)))
9959             (string-match type (mm-handle-media-type handle)))
9960       (let ((file (expand-file-name
9961                    (file-name-nondirectory
9962                     (or
9963                      (mail-content-type-get
9964                       (mm-handle-disposition handle) 'filename)
9965                      (concat gnus-newsgroup-name
9966                              "." (number-to-string
9967                                   (cdr gnus-article-current)))))
9968                    dir)))
9969         (unless (file-exists-p file)
9970           (mm-save-part-to-file handle file))))))
9971
9972 ;; Summary extract commands
9973
9974 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9975   (let ((buffer-read-only nil)
9976         (article (gnus-summary-article-number))
9977         after-article b e)
9978     (unless (gnus-summary-goto-subject article)
9979       (error "No such article: %d" article))
9980     (gnus-summary-position-point)
9981     ;; If all commands are to be bunched up on one line, we collect
9982     ;; them here.
9983     (unless gnus-view-pseudos-separately
9984       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9985             files action)
9986         (while ps
9987           (setq action (cdr (assq 'action (car ps))))
9988           (setq files (list (cdr (assq 'name (car ps)))))
9989           (while (and ps (cdr ps)
9990                       (string= (or action "1")
9991                                (or (cdr (assq 'action (cadr ps))) "2")))
9992             (push (cdr (assq 'name (cadr ps))) files)
9993             (setcdr ps (cddr ps)))
9994           (when files
9995             (when (not (string-match "%s" action))
9996               (push " " files))
9997             (push " " files)
9998             (when (assq 'execute (car ps))
9999               (setcdr (assq 'execute (car ps))
10000                       (funcall (if (string-match "%s" action)
10001                                    'format 'concat)
10002                                action
10003                                (mapconcat
10004                                 (lambda (f)
10005                                   (if (equal f " ")
10006                                       f
10007                                     (gnus-quote-arg-for-sh-or-csh f)))
10008                                 files " ")))))
10009           (setq ps (cdr ps)))))
10010     (if (and gnus-view-pseudos (not not-view))
10011         (while pslist
10012           (when (assq 'execute (car pslist))
10013             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10014                                   (eq gnus-view-pseudos 'not-confirm)))
10015           (setq pslist (cdr pslist)))
10016       (save-excursion
10017         (while pslist
10018           (setq after-article (or (cdr (assq 'article (car pslist)))
10019                                   (gnus-summary-article-number)))
10020           (gnus-summary-goto-subject after-article)
10021           (forward-line 1)
10022           (setq b (point))
10023           (insert "    " (file-name-nondirectory
10024                           (cdr (assq 'name (car pslist))))
10025                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10026           (setq e (point))
10027           (forward-line -1)             ; back to `b'
10028           (gnus-add-text-properties
10029            b (1- e) (list 'gnus-number gnus-reffed-article-number
10030                           gnus-mouse-face-prop gnus-mouse-face))
10031           (gnus-data-enter
10032            after-article gnus-reffed-article-number
10033            gnus-unread-mark b (car pslist) 0 (- e b))
10034           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10035           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10036           (setq pslist (cdr pslist)))))))
10037
10038 (defun gnus-pseudos< (p1 p2)
10039   (let ((c1 (cdr (assq 'action p1)))
10040         (c2 (cdr (assq 'action p2))))
10041     (and c1 c2 (string< c1 c2))))
10042
10043 (defun gnus-request-pseudo-article (props)
10044   (cond ((assq 'execute props)
10045          (gnus-execute-command (cdr (assq 'execute props)))))
10046   (let ((gnus-current-article (gnus-summary-article-number)))
10047     (gnus-run-hooks 'gnus-mark-article-hook)))
10048
10049 (defun gnus-execute-command (command &optional automatic)
10050   (save-excursion
10051     (gnus-article-setup-buffer)
10052     (set-buffer gnus-article-buffer)
10053     (setq buffer-read-only nil)
10054     (let ((command (if automatic command
10055                      (read-string "Command: " (cons command 0)))))
10056       (erase-buffer)
10057       (insert "$ " command "\n\n")
10058       (if gnus-view-pseudo-asynchronously
10059           (start-process "gnus-execute" (current-buffer) shell-file-name
10060                          shell-command-switch command)
10061         (call-process shell-file-name nil t nil
10062                       shell-command-switch command)))))
10063
10064 ;; Summary kill commands.
10065
10066 (defun gnus-summary-edit-global-kill (article)
10067   "Edit the \"global\" kill file."
10068   (interactive (list (gnus-summary-article-number)))
10069   (gnus-group-edit-global-kill article))
10070
10071 (defun gnus-summary-edit-local-kill ()
10072   "Edit a local kill file applied to the current newsgroup."
10073   (interactive)
10074   (setq gnus-current-headers (gnus-summary-article-header))
10075   (gnus-group-edit-local-kill
10076    (gnus-summary-article-number) gnus-newsgroup-name))
10077
10078 ;;; Header reading.
10079
10080 (defun gnus-read-header (id &optional header)
10081   "Read the headers of article ID and enter them into the Gnus system."
10082   (let ((group gnus-newsgroup-name)
10083         (gnus-override-method
10084          (or
10085           gnus-override-method
10086           (and (gnus-news-group-p gnus-newsgroup-name)
10087                (car (gnus-refer-article-methods)))))
10088         where)
10089     ;; First we check to see whether the header in question is already
10090     ;; fetched.
10091     (if (stringp id)
10092         ;; This is a Message-ID.
10093         (setq header (or header (gnus-id-to-header id)))
10094       ;; This is an article number.
10095       (setq header (or header (gnus-summary-article-header id))))
10096     (if (and header
10097              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10098         ;; We have found the header.
10099         header
10100       ;; If this is a sparse article, we have to nix out its
10101       ;; previous entry in the thread hashtb.
10102       (when (and header
10103                  (gnus-summary-article-sparse-p (mail-header-number header)))
10104         (let* ((parent (gnus-parent-id (mail-header-references header)))
10105                (thread (and parent (gnus-id-to-thread parent))))
10106           (when thread
10107             (delq (assq header thread) thread))))
10108       ;; We have to really fetch the header to this article.
10109       (save-excursion
10110         (set-buffer nntp-server-buffer)
10111         (when (setq where (gnus-request-head id group))
10112           (nnheader-fold-continuation-lines)
10113           (goto-char (point-max))
10114           (insert ".\n")
10115           (goto-char (point-min))
10116           (insert "211 ")
10117           (princ (cond
10118                   ((numberp id) id)
10119                   ((cdr where) (cdr where))
10120                   (header (mail-header-number header))
10121                   (t gnus-reffed-article-number))
10122                  (current-buffer))
10123           (insert " Article retrieved.\n"))
10124         (if (or (not where)
10125                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10126             ()                          ; Malformed head.
10127           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10128             (when (and (stringp id)
10129                        (not (string= (gnus-group-real-name group)
10130                                      (car where))))
10131               ;; If we fetched by Message-ID and the article came
10132               ;; from a different group, we fudge some bogus article
10133               ;; numbers for this article.
10134               (mail-header-set-number header gnus-reffed-article-number))
10135             (save-excursion
10136               (set-buffer gnus-summary-buffer)
10137               (decf gnus-reffed-article-number)
10138               (gnus-remove-header (mail-header-number header))
10139               (push header gnus-newsgroup-headers)
10140               (setq gnus-current-headers header)
10141               (push (mail-header-number header) gnus-newsgroup-limit)))
10142           header)))))
10143
10144 (defun gnus-remove-header (number)
10145   "Remove header NUMBER from `gnus-newsgroup-headers'."
10146   (if (and gnus-newsgroup-headers
10147            (= number (mail-header-number (car gnus-newsgroup-headers))))
10148       (pop gnus-newsgroup-headers)
10149     (let ((headers gnus-newsgroup-headers))
10150       (while (and (cdr headers)
10151                   (not (= number (mail-header-number (cadr headers)))))
10152         (pop headers))
10153       (when (cdr headers)
10154         (setcdr headers (cddr headers))))))
10155
10156 ;;;
10157 ;;; summary highlights
10158 ;;;
10159
10160 (defun gnus-highlight-selected-summary ()
10161   "Highlight selected article in summary buffer."
10162   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10163   (when gnus-summary-selected-face
10164     (save-excursion
10165       (let* ((beg (progn (beginning-of-line) (point)))
10166              (end (progn (end-of-line) (point)))
10167              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10168              (from (if (get-text-property beg gnus-mouse-face-prop)
10169                        beg
10170                      (or (next-single-property-change
10171                           beg gnus-mouse-face-prop nil end)
10172                          beg)))
10173              (to
10174               (if (= from end)
10175                   (- from 2)
10176                 (or (next-single-property-change
10177                      from gnus-mouse-face-prop nil end)
10178                     end))))
10179         ;; If no mouse-face prop on line we will have to = from = end,
10180         ;; so we highlight the entire line instead.
10181         (when (= (+ to 2) from)
10182           (setq from beg)
10183           (setq to end))
10184         (if gnus-newsgroup-selected-overlay
10185             ;; Move old overlay.
10186             (gnus-move-overlay
10187              gnus-newsgroup-selected-overlay from to (current-buffer))
10188           ;; Create new overlay.
10189           (gnus-overlay-put
10190            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10191            'face gnus-summary-selected-face))))))
10192
10193 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10194 (defun gnus-summary-highlight-line ()
10195   "Highlight current line according to `gnus-summary-highlight'."
10196   (let* ((list gnus-summary-highlight)
10197          (p (point))
10198          (end (progn (end-of-line) (point)))
10199          ;; now find out where the line starts and leave point there.
10200          (beg (progn (beginning-of-line) (point)))
10201          (article (gnus-summary-article-number))
10202          (score (or (cdr (assq (or article gnus-current-article)
10203                                gnus-newsgroup-scored))
10204                     gnus-summary-default-score 0))
10205          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10206          (inhibit-read-only t))
10207     ;; Eval the cars of the lists until we find a match.
10208     (let ((default gnus-summary-default-score)
10209           (default-high gnus-summary-default-high-score)
10210           (default-low gnus-summary-default-low-score))
10211       (while (and list
10212                   (not (eval (caar list))))
10213         (setq list (cdr list))))
10214     (let ((face (cdar list)))
10215       (unless (eq face (get-text-property beg 'face))
10216         (gnus-put-text-property-excluding-characters-with-faces
10217          beg end 'face
10218          (setq face (if (boundp face) (symbol-value face) face)))
10219         (when gnus-summary-highlight-line-function
10220           (funcall gnus-summary-highlight-line-function article face))))
10221     (goto-char p)))
10222
10223 (defun gnus-update-read-articles (group unread &optional compute)
10224   "Update the list of read articles in GROUP."
10225   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10226          (entry (gnus-gethash group gnus-newsrc-hashtb))
10227          (info (nth 2 entry))
10228          (prev 1)
10229          (unread (sort (copy-sequence unread) '<))
10230          read)
10231     (if (or (not info) (not active))
10232         ;; There is no info on this group if it was, in fact,
10233         ;; killed.  Gnus stores no information on killed groups, so
10234         ;; there's nothing to be done.
10235         ;; One could store the information somewhere temporarily,
10236         ;; perhaps...  Hmmm...
10237         ()
10238       ;; Remove any negative articles numbers.
10239       (while (and unread (< (car unread) 0))
10240         (setq unread (cdr unread)))
10241       ;; Remove any expired article numbers
10242       (while (and unread (< (car unread) (car active)))
10243         (setq unread (cdr unread)))
10244       ;; Compute the ranges of read articles by looking at the list of
10245       ;; unread articles.
10246       (while unread
10247         (when (/= (car unread) prev)
10248           (push (if (= prev (1- (car unread))) prev
10249                   (cons prev (1- (car unread))))
10250                 read))
10251         (setq prev (1+ (car unread)))
10252         (setq unread (cdr unread)))
10253       (when (<= prev (cdr active))
10254         (push (cons prev (cdr active)) read))
10255       (setq read (if (> (length read) 1) (nreverse read) read))
10256       (if compute
10257           read
10258         (save-excursion
10259           (let (setmarkundo)
10260             ;; Propagate the read marks to the backend.
10261             (when (gnus-check-backend-function 'request-set-mark group)
10262               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10263                     (add (gnus-remove-from-range read (gnus-info-read info))))
10264                 (when (or add del)
10265                   (unless (gnus-check-group group)
10266                     (error "Can't open server for %s" group))
10267                   (gnus-request-set-mark
10268                    group (delq nil (list (if add (list add 'add '(read)))
10269                                          (if del (list del 'del '(read))))))
10270                   (setq setmarkundo
10271                         `(gnus-request-set-mark
10272                           ,group
10273                           ',(delq nil (list
10274                                        (if del (list del 'add '(read)))
10275                                        (if add (list add 'del '(read))))))))))
10276             (set-buffer gnus-group-buffer)
10277             (gnus-undo-register
10278               `(progn
10279                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10280                  (gnus-info-set-read ',info ',(gnus-info-read info))
10281                  (gnus-get-unread-articles-in-group ',info
10282                                                     (gnus-active ,group))
10283                  (gnus-group-update-group ,group t)
10284                  ,setmarkundo))))
10285         ;; Enter this list into the group info.
10286         (gnus-info-set-read info read)
10287         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10288         (gnus-get-unread-articles-in-group info (gnus-active group))
10289         t))))
10290
10291 (defun gnus-offer-save-summaries ()
10292   "Offer to save all active summary buffers."
10293   (let (buffers)
10294     ;; Go through all buffers and find all summaries.
10295     (dolist (buffer (buffer-list))
10296       (when (and (setq buffer (buffer-name buffer))
10297                  (string-match "Summary" buffer)
10298                  (save-excursion
10299                    (set-buffer buffer)
10300                    ;; We check that this is, indeed, a summary buffer.
10301                    (and (eq major-mode 'gnus-summary-mode)
10302                         ;; Also make sure this isn't bogus.
10303                         gnus-newsgroup-prepared
10304                         ;; Also make sure that this isn't a
10305                         ;; dead summary buffer.
10306                         (not gnus-dead-summary-mode))))
10307         (push buffer buffers)))
10308     ;; Go through all these summary buffers and offer to save them.
10309     (when buffers
10310       (save-excursion
10311         (map-y-or-n-p
10312          "Update summary buffer %s? "
10313          (lambda (buf)
10314            (switch-to-buffer buf)
10315            (gnus-summary-exit))
10316          buffers)))))
10317
10318
10319 ;;; @ for mime-partial
10320 ;;;
10321
10322 (defun gnus-request-partial-message ()
10323   (save-excursion
10324     (let ((number (gnus-summary-article-number))
10325           (group gnus-newsgroup-name)
10326           (mother gnus-article-buffer))
10327       (set-buffer (get-buffer-create " *Partial Article*"))
10328       (erase-buffer)
10329       (setq mime-preview-buffer mother)
10330       (gnus-request-article-this-buffer number group)
10331       (mime-parse-buffer)
10332       )))
10333
10334 (autoload 'mime-combine-message/partial-pieces-automatically
10335   "mime-partial"
10336   "Internal method to combine message/partial messages automatically.")
10337
10338 (mime-add-condition
10339  'action '((type . message)(subtype . partial)
10340            (major-mode . gnus-original-article-mode)
10341            (method . mime-combine-message/partial-pieces-automatically)
10342            (summary-buffer-exp . gnus-summary-buffer)
10343            (request-partial-message-method . gnus-request-partial-message)
10344            ))
10345
10346
10347 ;;; @ for message/rfc822
10348 ;;;
10349
10350 (defun gnus-mime-extract-message/rfc822 (entity situation)
10351   (let (group article num cwin swin cur)
10352     (with-temp-buffer
10353       (mime-insert-entity-content entity)
10354       (setq group (or (cdr (assq 'group situation))
10355                       (completing-read "Group: "
10356                                        gnus-active-hashtb
10357                                        nil
10358                                        (gnus-read-active-file-p)
10359                                        gnus-newsgroup-name))
10360             article (gnus-request-accept-article group)))
10361     (when (and (consp article)
10362                (numberp (setq article (cdr article))))
10363       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10364             cwin (get-buffer-window (current-buffer) t))
10365       (save-window-excursion
10366         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10367             (select-window swin)
10368           (set-buffer gnus-summary-buffer))
10369         (setq cur gnus-current-article)
10370         (forward-line num)
10371         (let (gnus-show-threads)
10372           (gnus-summary-goto-subject article t))
10373         (gnus-summary-clear-mark-forward 1)
10374         (gnus-summary-goto-subject cur))
10375       (when (and cwin (window-frame cwin))
10376         (select-frame (window-frame cwin)))
10377       (when (boundp 'mime-acting-situation-to-override)
10378         (set-alist 'mime-acting-situation-to-override
10379                    'group
10380                    group)
10381         (set-alist 'mime-acting-situation-to-override
10382                    'after-method
10383                    `(progn
10384                       (save-current-buffer
10385                         (set-buffer gnus-group-buffer)
10386                         (gnus-activate-group ,group))
10387                       (gnus-summary-goto-article ,cur
10388                                                  gnus-show-all-headers)))
10389         (set-alist 'mime-acting-situation-to-override
10390                    'number num)))))
10391
10392 (mime-add-condition
10393  'action '((type . message)(subtype . rfc822)
10394            (major-mode . gnus-original-article-mode)
10395            (method . gnus-mime-extract-message/rfc822)
10396            (mode . "extract")
10397            ))
10398
10399 (mime-add-condition
10400  'action '((type . message)(subtype . news)
10401            (major-mode . gnus-original-article-mode)
10402            (method . gnus-mime-extract-message/rfc822)
10403            (mode . "extract")
10404            ))
10405
10406 (defun gnus-mime-extract-multipart (entity situation)
10407   (let ((children (mime-entity-children entity))
10408         mime-acting-situation-to-override
10409         f)
10410     (while children
10411       (mime-play-entity (car children)
10412                         (cons (assq 'mode situation)
10413                               mime-acting-situation-to-override))
10414       (setq children (cdr children)))
10415     (if (setq f (cdr (assq 'after-method
10416                            mime-acting-situation-to-override)))
10417         (eval f)
10418       )))
10419
10420 (mime-add-condition
10421  'action '((type . multipart)
10422            (method . gnus-mime-extract-multipart)
10423            (mode . "extract")
10424            )
10425  'with-default)
10426
10427
10428 ;;; @ end
10429 ;;;
10430
10431 (defun gnus-summary-setup-default-charset ()
10432   "Setup newsgroup default charset."
10433   (if (equal gnus-newsgroup-name "nndraft:drafts")
10434       (setq gnus-newsgroup-charset nil)
10435     (let* ((ignored-charsets
10436             (or gnus-newsgroup-ephemeral-ignored-charsets
10437                 (append
10438                  (and gnus-newsgroup-name
10439                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10440                  gnus-newsgroup-ignored-charsets))))
10441       (setq gnus-newsgroup-charset
10442             (or gnus-newsgroup-ephemeral-charset
10443                 (and gnus-newsgroup-name
10444                      (gnus-parameter-charset gnus-newsgroup-name))
10445                 gnus-default-charset))
10446       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10447            ignored-charsets))))
10448
10449 ;;;
10450 ;;; Mime Commands
10451 ;;;
10452
10453 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10454   "Display the current article buffer fully MIME-buttonized.
10455 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10456 treated as multipart/mixed."
10457   (interactive "P")
10458   (require 'gnus-art)
10459   (let ((gnus-unbuttonized-mime-types nil)
10460         (gnus-mime-display-multipart-as-mixed show-all-parts))
10461     (gnus-summary-show-article)))
10462
10463 (defun gnus-summary-repair-multipart (article)
10464   "Add a Content-Type header to a multipart article without one."
10465   (interactive (list (gnus-summary-article-number)))
10466   (gnus-with-article article
10467     (message-narrow-to-head)
10468     (message-remove-header "Mime-Version")
10469     (goto-char (point-max))
10470     (insert "Mime-Version: 1.0\n")
10471     (widen)
10472     (when (search-forward "\n--" nil t)
10473       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10474         (message-narrow-to-head)
10475         (message-remove-header "Content-Type")
10476         (goto-char (point-max))
10477         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10478                         separator))
10479         (widen))))
10480   (let (gnus-mark-article-hook)
10481     (gnus-summary-select-article t t nil article)))
10482
10483 (defun gnus-summary-toggle-display-buttonized ()
10484   "Toggle the buttonizing of the article buffer."
10485   (interactive)
10486   (require 'gnus-art)
10487   (if (setq gnus-inhibit-mime-unbuttonizing
10488             (not gnus-inhibit-mime-unbuttonizing))
10489       (let ((gnus-unbuttonized-mime-types nil))
10490         (gnus-summary-show-article))
10491     (gnus-summary-show-article)))
10492
10493 ;;;
10494 ;;; Intelli-mouse commmands
10495 ;;;
10496
10497 (defun gnus-wheel-summary-scroll (event)
10498   (interactive "e")
10499   (let ((amount (if (memq 'shift (event-modifiers event))
10500                     (car gnus-wheel-scroll-amount)
10501                   (cdr gnus-wheel-scroll-amount)))
10502         (direction (- (* (static-if (featurep 'xemacs)
10503                              (event-button event)
10504                            (cond ((eq 'mouse-4 (event-basic-type event))
10505                                   4)
10506                                  ((eq 'mouse-5 (event-basic-type event))
10507                                   5)))
10508                          2) 9))
10509         edge)
10510     (gnus-summary-scroll-up (* amount direction))
10511     (when (gnus-eval-in-buffer-window gnus-article-buffer
10512             (save-restriction
10513               (widen)
10514               (and (if (< 0 direction)
10515                        (gnus-article-next-page 0)
10516                      (gnus-article-prev-page 0)
10517                      (bobp))
10518                    (if (setq edge (get-text-property
10519                                    (point-min) 'gnus-wheel-edge))
10520                        (setq edge (* edge direction))
10521                      (setq edge -1))
10522                    (or (plusp edge)
10523                        (let ((buffer-read-only nil)
10524                              (inhibit-read-only t))
10525                          (put-text-property (point-min) (point-max)
10526                                             'gnus-wheel-edge direction)
10527                          nil))
10528                    (or (> edge gnus-wheel-edge-resistance)
10529                        (let ((buffer-read-only nil)
10530                              (inhibit-read-only t))
10531                          (put-text-property (point-min) (point-max)
10532                                             'gnus-wheel-edge
10533                                             (* (1+ edge) direction))
10534                          nil))
10535                    (eq last-command 'gnus-wheel-summary-scroll))))
10536       (gnus-summary-next-article nil nil (minusp direction)))))
10537
10538 (defun gnus-wheel-install ()
10539   "Enable mouse wheel support on summary window."
10540   (when gnus-use-wheel
10541     (let ((keys
10542            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10543       (dolist (key keys)
10544         (define-key gnus-summary-mode-map key
10545           'gnus-wheel-summary-scroll)))))
10546
10547 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10548
10549 ;;;
10550 ;;; Traditional PGP commmands
10551 ;;;
10552
10553 (defun gnus-summary-decrypt-article (&optional force)
10554   "Decrypt the current article in traditional PGP way.
10555 This will have permanent effect only in mail groups.
10556 If FORCE is non-nil, allow editing of articles even in read-only
10557 groups."
10558   (interactive "P")
10559   (gnus-summary-select-article t)
10560   (gnus-eval-in-buffer-window gnus-article-buffer
10561     (save-excursion
10562       (save-restriction
10563         (widen)
10564         (goto-char (point-min))
10565         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10566           (error "Not a traditional PGP message!"))
10567         (let ((armor-start (match-beginning 0)))
10568           (if (and (pgg-decrypt-region armor-start (point-max))
10569                    (or force (not (gnus-group-read-only-p))))
10570               (let ((inhibit-read-only t)
10571                     buffer-read-only)
10572                 (delete-region armor-start
10573                                (progn
10574                                  (re-search-forward "^-+END PGP" nil t)
10575                                  (beginning-of-line 2)
10576                                  (point)))
10577                 (insert-buffer-substring pgg-output-buffer))))))))
10578
10579 (defun gnus-summary-verify-article ()
10580   "Verify the current article in traditional PGP way."
10581   (interactive)
10582   (save-excursion
10583     (set-buffer gnus-original-article-buffer)
10584     (goto-char (point-min))
10585     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10586       (error "Not a traditional PGP message!"))
10587     (re-search-forward "^-+END PGP" nil t)
10588     (beginning-of-line 2)
10589     (call-interactively (function pgg-verify-region))))
10590
10591 ;;;
10592 ;;; Generic summary marking commands
10593 ;;;
10594
10595 (defvar gnus-summary-marking-alist
10596   '((read gnus-del-mark "d")
10597     (unread gnus-unread-mark "u")
10598     (ticked gnus-ticked-mark "!")
10599     (dormant gnus-dormant-mark "?")
10600     (expirable gnus-expirable-mark "e"))
10601   "An alist of names/marks/keystrokes.")
10602
10603 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10604 (defvar gnus-summary-mark-map)
10605
10606 (defun gnus-summary-make-all-marking-commands ()
10607   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10608   (dolist (elem gnus-summary-marking-alist)
10609     (apply 'gnus-summary-make-marking-command elem)))
10610
10611 (defun gnus-summary-make-marking-command (name mark keystroke)
10612   (let ((map (make-sparse-keymap)))
10613     (define-key gnus-summary-generic-mark-map keystroke map)
10614     (dolist (lway `((next "next" next nil "n")
10615                     (next-unread "next unread" next t "N")
10616                     (prev "previous" prev nil "p")
10617                     (prev-unread "previous unread" prev t "P")
10618                     (nomove "" nil nil ,keystroke)))
10619       (let ((func (gnus-summary-make-marking-command-1
10620                    mark (car lway) lway name)))
10621         (setq func (eval func))
10622         (define-key map (nth 4 lway) func)))))
10623
10624 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10625   `(defun ,(intern
10626             (format "gnus-summary-put-mark-as-%s%s"
10627                     name (if (eq way 'nomove)
10628                              ""
10629                            (concat "-" (symbol-name way)))))
10630      (n)
10631      ,(format
10632        "Mark the current article as %s%s.
10633 If N, the prefix, then repeat N times.
10634 If N is negative, move in reverse order.
10635 The difference between N and the actual number of articles marked is
10636 returned."
10637        name (car (cdr lway)))
10638      (interactive "p")
10639      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10640
10641 (defun gnus-summary-generic-mark (n mark move unread)
10642   "Mark N articles with MARK."
10643   (unless (eq major-mode 'gnus-summary-mode)
10644     (error "This command can only be used in the summary buffer"))
10645   (gnus-summary-show-thread)
10646   (let ((nummove
10647          (cond
10648           ((eq move 'next) 1)
10649           ((eq move 'prev) -1)
10650           (t 0))))
10651     (if (zerop nummove)
10652         (setq n 1)
10653       (when (< n 0)
10654         (setq n (abs n)
10655               nummove (* -1 nummove))))
10656     (while (and (> n 0)
10657                 (gnus-summary-mark-article nil mark)
10658                 (zerop (gnus-summary-next-subject nummove unread t)))
10659       (setq n (1- n)))
10660     (when (/= 0 n)
10661       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10662     (gnus-summary-recenter)
10663     (gnus-summary-position-point)
10664     (gnus-set-mode-line 'summary)
10665     n))
10666
10667 (defun gnus-summary-insert-articles (articles)
10668   (when (setq articles
10669               (gnus-set-difference articles
10670                                    (mapcar (lambda (h) (mail-header-number h))
10671                                            gnus-newsgroup-headers)))
10672     (setq gnus-newsgroup-headers
10673           (merge 'list
10674                  gnus-newsgroup-headers
10675                  (gnus-fetch-headers articles)
10676                  'gnus-article-sort-by-number))
10677     ;; Suppress duplicates?
10678     (when gnus-suppress-duplicates
10679       (gnus-dup-suppress-articles))
10680
10681     ;; We might want to build some more threads first.
10682     (when (and gnus-fetch-old-headers
10683                (eq gnus-headers-retrieved-by 'nov))
10684       (if (eq gnus-fetch-old-headers 'invisible)
10685           (gnus-build-all-threads)
10686         (gnus-build-old-threads)))
10687     ;; Let the Gnus agent mark articles as read.
10688     (when gnus-agent
10689       (gnus-agent-get-undownloaded-list))
10690     ;; Remove list identifiers from subject
10691     (when gnus-list-identifiers
10692       (gnus-summary-remove-list-identifiers))
10693     ;; First and last article in this newsgroup.
10694     (when gnus-newsgroup-headers
10695       (setq gnus-newsgroup-begin
10696             (mail-header-number (car gnus-newsgroup-headers))
10697             gnus-newsgroup-end
10698             (mail-header-number
10699              (gnus-last-element gnus-newsgroup-headers))))
10700     (when gnus-use-scoring
10701       (gnus-possibly-score-headers))))
10702
10703 (defun gnus-summary-insert-old-articles (&optional all)
10704   "Insert all old articles in this group.
10705 If ALL is non-nil, already read articles become readable.
10706 If ALL is a number, fetch this number of articles."
10707   (interactive "P")
10708   (prog1
10709       (let ((old (mapcar 'car gnus-newsgroup-data))
10710             (i (car gnus-newsgroup-active))
10711             older len)
10712         (while (<= i (cdr gnus-newsgroup-active))
10713           (or (memq i old) (push i older))
10714           (incf i))
10715         (setq len (length older))
10716         (cond
10717          ((null older) nil)
10718          ((numberp all)
10719           (if (< all len)
10720               (setq older (subseq older 0 all))))
10721          (all nil)
10722          (t
10723           (if (and (numberp gnus-large-newsgroup)
10724                    (> len gnus-large-newsgroup))
10725               (let ((input
10726                      (read-string
10727                       (format
10728                        "How many articles from %s (default %d): "
10729                        (gnus-limit-string
10730                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10731                        len))))
10732                 (unless (string-match "^[ \t]*$" input)
10733                   (setq all (string-to-number input))
10734                   (if (< all len)
10735                       (setq older (subseq older 0 all))))))))
10736         (if (not older)
10737             (message "No old news.")
10738           (gnus-summary-insert-articles older)
10739           (gnus-summary-limit (gnus-union older old))))
10740     (gnus-summary-position-point)))
10741
10742 (defun gnus-summary-insert-new-articles ()
10743   "Insert all new articles in this group."
10744   (interactive)
10745   (prog1
10746       (let ((old (mapcar 'car gnus-newsgroup-data))
10747             (old-active gnus-newsgroup-active)
10748             (nnmail-fetched-sources (list t))
10749             i new)
10750         (setq gnus-newsgroup-active
10751               (gnus-activate-group gnus-newsgroup-name 'scan))
10752         (setq i (1+ (cdr old-active)))
10753         (while (<= i (cdr gnus-newsgroup-active))
10754           (push i new)
10755           (incf i))
10756         (if (not new)
10757             (message "No gnus is bad news.")
10758           (setq new (nreverse new))
10759           (gnus-summary-insert-articles new)
10760           (setq gnus-newsgroup-unreads
10761                 (append gnus-newsgroup-unreads new))
10762           (gnus-summary-limit (gnus-union old new))))
10763     (gnus-summary-position-point)))
10764
10765 (gnus-summary-make-all-marking-commands)
10766
10767 (gnus-ems-redefine)
10768
10769 (provide 'gnus-sum)
10770
10771 (run-hooks 'gnus-sum-load-hook)
10772
10773 ;;; gnus-sum.el ends here