* nnshimbun.el (nnshimbun-request-expire-articles): Don't refer to the
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'gnus-cache-articles-in-group "gnus-cache")
52   (autoload 'pgg-decrypt-region "pgg" nil t)
53   (autoload 'pgg-verify-region "pgg" nil t))
54
55 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
56 (autoload 'gnus-cache-write-active "gnus-cache")
57 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
58 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
59 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
60 (autoload 'mm-uu-dissect "mm-uu")
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-fetch-old-headers nil
70   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
71 If an unread article in the group refers to an older, already read (or
72 just marked as read) article, the old article will not normally be
73 displayed in the Summary buffer.  If this variable is non-nil, Gnus
74 will attempt to grab the headers to the old articles, and thereby
75 build complete threads.  If it has the value `some', only enough
76 headers to connect otherwise loose threads will be displayed.  This
77 variable can also be a number.  In that case, no more than that number
78 of old headers will be fetched.  If it has the value `invisible', all
79 old headers will be fetched, but none will be displayed.
80
81 The server has to support NOV for any of this to work."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const some)
85                  number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-refer-thread-limit 200
89   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
90 If t, fetch all the available old headers."
91   :group 'gnus-thread
92   :type '(choice number
93                  (sexp :menu-tag "other" t)))
94
95 (defcustom gnus-summary-make-false-root 'adopt
96   "*nil means that Gnus won't gather loose threads.
97 If the root of a thread has expired or been read in a previous
98 session, the information necessary to build a complete thread has been
99 lost.  Instead of having many small sub-threads from this original thread
100 scattered all over the summary buffer, Gnus can gather them.
101
102 If non-nil, Gnus will try to gather all loose sub-threads from an
103 original thread into one large thread.
104
105 If this variable is non-nil, it should be one of `none', `adopt',
106 `dummy' or `empty'.
107
108 If this variable is `none', Gnus will not make a false root, but just
109 present the sub-threads after another.
110 If this variable is `dummy', Gnus will create a dummy root that will
111 have all the sub-threads as children.
112 If this variable is `adopt', Gnus will make one of the \"children\"
113 the parent and mark all the step-children as such.
114 If this variable is `empty', the \"children\" are printed with empty
115 subject fields.  (Or rather, they will be printed with a string
116 given by the `gnus-summary-same-subject' variable.)"
117   :group 'gnus-thread
118   :type '(choice (const :tag "off" nil)
119                  (const none)
120                  (const dummy)
121                  (const adopt)
122                  (const empty)))
123
124 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
125   "*A regexp to match subjects to be excluded from loose thread gathering.
126 As loose thread gathering is done on subjects only, that means that
127 there can be many false gatherings performed.  By rooting out certain
128 common subjects, gathering might become saner."
129   :group 'gnus-thread
130   :type 'regexp)
131
132 (defcustom gnus-summary-gather-subject-limit nil
133   "*Maximum length of subject comparisons when gathering loose threads.
134 Use nil to compare full subjects.  Setting this variable to a low
135 number will help gather threads that have been corrupted by
136 newsreaders chopping off subject lines, but it might also mean that
137 unrelated articles that have subject that happen to begin with the
138 same few characters will be incorrectly gathered.
139
140 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
141 comparing subjects."
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const fuzzy)
145                  (sexp :menu-tag "on" t)))
146
147 (defcustom gnus-simplify-subject-functions nil
148   "List of functions taking a string argument that simplify subjects.
149 The functions are applied recursively.
150
151 Useful functions to put in this list include: `gnus-simplify-subject-re',
152 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If nil, don't select the first unread article when entering a group.
296 If this variable is `best', select the highest-scored unread article
297 in the group.  If t, select the first unread article.
298
299 This variable can also be a function to place point on a likely
300 subject line.  Useful values include `gnus-summary-first-unread-subject',
301 `gnus-summary-first-unread-article' and
302 `gnus-summary-best-unread-article'.
303
304 If you want to prevent automatic selection of the first unread article
305 in some newsgroups, set the variable to nil in
306 `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (const best)
310                  (sexp :menu-tag "first" t)
311                  (function-item gnus-summary-first-unread-subject)
312                  (function-item gnus-summary-first-unread-article)
313                  (function-item gnus-summary-best-unread-article)))
314
315 (defcustom gnus-dont-select-after-jump-to-other-group nil
316   "If non-nil, don't select the first unread article after entering the
317 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
318 it is depend on the value of `gnus-auto-select-first' whether to select
319 or not."
320   :group 'gnus-group-select
321   :type 'boolean)
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `Z n' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-summary-check-current nil
349   "*If non-nil, consider the current article when moving.
350 The \"unread\" movement commands will stay on the same line if the
351 current article is unread."
352   :group 'gnus-summary-maneuvering
353   :type 'boolean)
354
355 (defcustom gnus-auto-center-summary t
356   "*If non-nil, always center the current summary buffer.
357 In particular, if `vertical' do only vertical recentering.  If non-nil
358 and non-`vertical', do both horizontal and vertical recentering."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "none" nil)
361                  (const vertical)
362                  (integer :tag "height")
363                  (sexp :menu-tag "both" t)))
364
365 (defcustom gnus-show-all-headers nil
366   "*If non-nil, don't hide any headers."
367   :group 'gnus-article-hiding
368   :group 'gnus-article-headers
369   :type 'boolean)
370
371 (defcustom gnus-summary-ignore-duplicates nil
372   "*If non-nil, ignore articles with identical Message-ID headers."
373   :group 'gnus-summary
374   :type 'boolean)
375
376 (defcustom gnus-single-article-buffer t
377   "*If non-nil, display all articles in the same buffer.
378 If nil, each group will get its own article buffer."
379   :group 'gnus-article-various
380   :type 'boolean)
381
382 (defcustom gnus-break-pages t
383   "*If non-nil, do page breaking on articles.
384 The page delimiter is specified by the `gnus-page-delimiter'
385 variable."
386   :group 'gnus-article-various
387   :type 'boolean)
388
389 (defcustom gnus-show-mime t
390   "*If non-nil, do mime processing of articles.
391 The articles will simply be fed to the function given by
392 `gnus-article-display-method-for-mime'."
393   :group 'gnus-article-mime
394   :type 'boolean)
395
396 (defcustom gnus-move-split-methods nil
397   "*Variable used to suggest where articles are to be moved to.
398 It uses the same syntax as the `gnus-split-methods' variable.
399 However, whereas `gnus-split-methods' specifies file names as targets,
400 this variable specifies group names."
401   :group 'gnus-summary-mail
402   :type '(repeat (choice (list :value (fun) function)
403                          (cons :value ("" "") regexp (repeat string))
404                          (sexp :value nil))))
405
406 (defcustom gnus-unread-mark ?  ;Whitespace
407   "*Mark used for unread articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-ticked-mark ?!
412   "*Mark used for ticked articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-dormant-mark ??
417   "*Mark used for dormant articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-del-mark ?r
422   "*Mark used for del'd articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-read-mark ?R
427   "*Mark used for read articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-expirable-mark ?E
432   "*Mark used for expirable articles."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-killed-mark ?K
437   "*Mark used for killed articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-souped-mark ?F
442   "*Mark used for souped articles."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-kill-file-mark ?X
447   "*Mark used for articles killed by kill files."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-low-score-mark ?Y
452   "*Mark used for articles with a low score."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-catchup-mark ?C
457   "*Mark used for articles that are caught up."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-replied-mark ?A
462   "*Mark used for articles that have been replied to."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-forwarded-mark ?O
467   "*Mark used for articles that have been forwarded."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-cached-mark ?*
472   "*Mark used for articles that are in the cache."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-saved-mark ?S
477   "*Mark used for articles that have been saved to."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-no-mark ?  ;Whitespace
482   "*Mark used for articles that have no other secondary mark."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-ancient-mark ?O
487   "*Mark used for ancient articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-sparse-mark ?Q
492   "*Mark used for sparsely reffed articles."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-canceled-mark ?G
497   "*Mark used for canceled articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-duplicate-mark ?M
502   "*Mark used for duplicate articles."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-undownloaded-mark ?@
507   "*Mark used for articles that weren't downloaded."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-downloadable-mark ?%
512   "*Mark used for articles that are to be downloaded."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-unsendable-mark ?=
517   "*Mark used for articles that won't be sent."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-score-over-mark ?+
522   "*Score mark used for articles with high scores."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-score-below-mark ?-
527   "*Score mark used for articles with low scores."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-empty-thread-mark ?  ;Whitespace
532   "*There is no thread under the article."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-not-empty-thread-mark ?=
537   "*There is a thread under the article."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-view-pseudo-asynchronously nil
542   "*If non-nil, Gnus will view pseudo-articles asynchronously."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-auto-expirable-marks
547   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
548         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
549         gnus-souped-mark gnus-duplicate-mark)
550   "*The list of marks converted into expiration if a group is auto-expirable."
551   :version "21.1"
552   :group 'gnus-summary
553   :type '(repeat character))
554
555 (defcustom gnus-inhibit-user-auto-expire t
556   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
557   :version "21.1"
558   :group 'gnus-summary
559   :type 'boolean)
560
561 (defcustom gnus-view-pseudos nil
562   "*If `automatic', pseudo-articles will be viewed automatically.
563 If `not-confirm', pseudos will be viewed automatically, and the user
564 will not be asked to confirm the command."
565   :group 'gnus-extract-view
566   :type '(choice (const :tag "off" nil)
567                  (const automatic)
568                  (const not-confirm)))
569
570 (defcustom gnus-view-pseudos-separately t
571   "*If non-nil, one pseudo-article will be created for each file to be viewed.
572 If nil, all files that use the same viewing command will be given as a
573 list of parameters to that command."
574   :group 'gnus-extract-view
575   :type 'boolean)
576
577 (defcustom gnus-insert-pseudo-articles t
578   "*If non-nil, insert pseudo-articles when decoding articles."
579   :group 'gnus-extract-view
580   :type 'boolean)
581
582 (defcustom gnus-summary-dummy-line-format
583   "  %(:                          :%) %S\n"
584   "*The format specification for the dummy roots in the summary buffer.
585 It works along the same lines as a normal formatting string,
586 with some simple extensions.
587
588 %S  The subject"
589   :group 'gnus-threading
590   :type 'string)
591
592 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
593   "*The format specification for the summary mode line.
594 It works along the same lines as a normal formatting string,
595 with some simple extensions:
596
597 %G  Group name
598 %p  Unprefixed group name
599 %A  Current article number
600 %z  Current article score
601 %V  Gnus version
602 %U  Number of unread articles in the group
603 %e  Number of unselected articles in the group
604 %Z  A string with unread/unselected article counts
605 %g  Shortish group name
606 %S  Subject of the current article
607 %u  User-defined spec
608 %s  Current score file name
609 %d  Number of dormant articles
610 %r  Number of articles that have been marked as read in this session
611 %E  Number of articles expunged by the score files"
612   :group 'gnus-summary-format
613   :type 'string)
614
615 (defcustom gnus-list-identifiers nil
616   "Regexp that matches list identifiers to be removed from subject.
617 This can also be a list of regexps."
618   :version "21.1"
619   :group 'gnus-summary-format
620   :group 'gnus-article-hiding
621   :type '(choice (const :tag "none" nil)
622                  (regexp :value ".*")
623                  (repeat :value (".*") regexp)))
624
625 (defcustom gnus-summary-mark-below 0
626   "*Mark all articles with a score below this variable as read.
627 This variable is local to each summary buffer and usually set by the
628 score file."
629   :group 'gnus-score-default
630   :type 'integer)
631
632 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
633   "*List of functions used for sorting articles in the summary buffer.
634
635 Each function takes two articles and returns non-nil if the first
636 article should be sorted before the other.  If you use more than one
637 function, the primary sort function should be the last.  You should
638 probably always include `gnus-article-sort-by-number' in the list of
639 sorting functions -- preferably first.  Also note that sorting by date
640 is often much slower than sorting by number, and the sorting order is
641 very similar.  (Sorting by date means sorting by the time the message
642 was sent, sorting by number means sorting by arrival time.)
643
644 Ready-made functions include `gnus-article-sort-by-number',
645 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
646 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
647
648 When threading is turned on, the variable `gnus-thread-sort-functions'
649 controls how articles are sorted."
650   :group 'gnus-summary-sort
651   :type '(repeat (choice (function-item gnus-article-sort-by-number)
652                          (function-item gnus-article-sort-by-author)
653                          (function-item gnus-article-sort-by-subject)
654                          (function-item gnus-article-sort-by-date)
655                          (function-item gnus-article-sort-by-score)
656                          (function :tag "other"))))
657
658 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
659   "*List of functions used for sorting threads in the summary buffer.
660 By default, threads are sorted by article number.
661
662 Each function takes two threads and returns non-nil if the first
663 thread should be sorted before the other.  If you use more than one
664 function, the primary sort function should be the last.  You should
665 probably always include `gnus-thread-sort-by-number' in the list of
666 sorting functions -- preferably first.  Also note that sorting by date
667 is often much slower than sorting by number, and the sorting order is
668 very similar.  (Sorting by date means sorting by the time the message
669 was sent, sorting by number means sorting by arrival time.)
670
671 Ready-made functions include `gnus-thread-sort-by-number',
672 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
673 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
674 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
675
676 When threading is turned off, the variable
677 `gnus-article-sort-functions' controls how articles are sorted."
678   :group 'gnus-summary-sort
679   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
680                          (function-item gnus-thread-sort-by-author)
681                          (function-item gnus-thread-sort-by-subject)
682                          (function-item gnus-thread-sort-by-date)
683                          (function-item gnus-thread-sort-by-score)
684                          (function-item gnus-thread-sort-by-total-score)
685                          (function :tag "other"))))
686
687 (defcustom gnus-thread-score-function '+
688   "*Function used for calculating the total score of a thread.
689
690 The function is called with the scores of the article and each
691 subthread and should then return the score of the thread.
692
693 Some functions you can use are `+', `max', or `min'."
694   :group 'gnus-summary-sort
695   :type 'function)
696
697 (defcustom gnus-summary-expunge-below nil
698   "All articles that have a score less than this variable will be expunged.
699 This variable is local to the summary buffers."
700   :group 'gnus-score-default
701   :type '(choice (const :tag "off" nil)
702                  integer))
703
704 (defcustom gnus-thread-expunge-below nil
705   "All threads that have a total score less than this variable will be expunged.
706 See `gnus-thread-score-function' for en explanation of what a
707 \"thread score\" is.
708
709 This variable is local to the summary buffers."
710   :group 'gnus-threading
711   :group 'gnus-score-default
712   :type '(choice (const :tag "off" nil)
713                  integer))
714
715 (defcustom gnus-summary-mode-hook nil
716   "*A hook for Gnus summary mode.
717 This hook is run before any variables are set in the summary buffer."
718   :options '(turn-on-gnus-mailing-list-mode)
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
723 (when (featurep 'xemacs)
724   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
725   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
726   (add-hook 'gnus-summary-mode-hook
727             'gnus-xmas-switch-horizontal-scrollbar-off))
728
729 (defcustom gnus-summary-menu-hook nil
730   "*Hook run after the creation of the summary mode menu."
731   :group 'gnus-summary-visual
732   :type 'hook)
733
734 (defcustom gnus-summary-exit-hook nil
735   "*A hook called on exit from the summary buffer.
736 It will be called with point in the group buffer."
737   :group 'gnus-summary-exit
738   :type 'hook)
739
740 (defcustom gnus-summary-prepare-hook nil
741   "*A hook called after the summary buffer has been generated.
742 If you want to modify the summary buffer, you can use this hook."
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 (defcustom gnus-summary-prepared-hook nil
747   "*A hook called as the last thing after the summary buffer has been generated."
748   :group 'gnus-summary-various
749   :type 'hook)
750
751 (defcustom gnus-summary-generate-hook nil
752   "*A hook run just before generating the summary buffer.
753 This hook is commonly used to customize threading variables and the
754 like."
755   :group 'gnus-summary-various
756   :type 'hook)
757
758 (defcustom gnus-select-group-hook nil
759   "*A hook called when a newsgroup is selected.
760
761 If you'd like to simplify subjects like the
762 `gnus-summary-next-same-subject' command does, you can use the
763 following hook:
764
765  (setq gnus-select-group-hook
766       (list
767         (lambda ()
768           (mapcar (lambda (header)
769                      (mail-header-set-subject
770                       header
771                       (gnus-simplify-subject
772                        (mail-header-subject header) 're-only)))
773                   gnus-newsgroup-headers))))"
774   :group 'gnus-group-select
775   :type 'hook)
776
777 (defcustom gnus-select-article-hook nil
778   "*A hook called when an article is selected."
779   :group 'gnus-summary-choose
780   :type 'hook)
781
782 (defcustom gnus-visual-mark-article-hook
783   (list 'gnus-highlight-selected-summary)
784   "*Hook run after selecting an article in the summary buffer.
785 It is meant to be used for highlighting the article in some way.  It
786 is not run if `gnus-visual' is nil."
787   :group 'gnus-summary-visual
788   :type 'hook)
789
790 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
791   "*A hook called before parsing the headers."
792   :group 'gnus-various
793   :type 'hook)
794
795 (defcustom gnus-exit-group-hook nil
796   "*A hook called when exiting summary mode.
797 This hook is not called from the non-updating exit commands like `Q'."
798   :group 'gnus-various
799   :type 'hook)
800
801 (defcustom gnus-summary-update-hook
802   (list 'gnus-summary-highlight-line)
803   "*A hook called when a summary line is changed.
804 The hook will not be called if `gnus-visual' is nil.
805
806 The default function `gnus-summary-highlight-line' will
807 highlight the line according to the `gnus-summary-highlight'
808 variable."
809   :group 'gnus-summary-visual
810   :type 'hook)
811
812 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
813   "*A hook called when an article is selected for the first time.
814 The hook is intended to mark an article as read (or unread)
815 automatically when it is selected."
816   :group 'gnus-summary-choose
817   :type 'hook)
818
819 (defcustom gnus-group-no-more-groups-hook nil
820   "*A hook run when returning to group mode having no more (unread) groups."
821   :group 'gnus-group-select
822   :type 'hook)
823
824 (defcustom gnus-ps-print-hook nil
825   "*A hook run before ps-printing something from Gnus."
826   :group 'gnus-summary
827   :type 'hook)
828
829 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
830   "Face used for highlighting the current article in the summary buffer."
831   :group 'gnus-summary-visual
832   :type 'face)
833
834 (defcustom gnus-summary-highlight
835   '(((= mark gnus-canceled-mark)
836      . gnus-summary-cancelled-face)
837     ((and (> score default-high)
838           (or (= mark gnus-dormant-mark)
839               (= mark gnus-ticked-mark)))
840      . gnus-summary-high-ticked-face)
841     ((and (< score default-low)
842           (or (= mark gnus-dormant-mark)
843               (= mark gnus-ticked-mark)))
844      . gnus-summary-low-ticked-face)
845     ((or (= mark gnus-dormant-mark)
846          (= mark gnus-ticked-mark))
847      . gnus-summary-normal-ticked-face)
848     ((and (> score default-high) (= mark gnus-ancient-mark))
849      . gnus-summary-high-ancient-face)
850     ((and (< score default-low) (= mark gnus-ancient-mark))
851      . gnus-summary-low-ancient-face)
852     ((= mark gnus-ancient-mark)
853      . gnus-summary-normal-ancient-face)
854     ((and (> score default-high) (= mark gnus-unread-mark))
855      . gnus-summary-high-unread-face)
856     ((and (< score default-low) (= mark gnus-unread-mark))
857      . gnus-summary-low-unread-face)
858     ((= mark gnus-unread-mark)
859      . gnus-summary-normal-unread-face)
860     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
861                                              gnus-undownloaded-mark)))
862      . gnus-summary-high-unread-face)
863     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
864                                              gnus-undownloaded-mark)))
865      . gnus-summary-low-unread-face)
866     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
867           (memq article gnus-newsgroup-unreads))
868      . gnus-summary-normal-unread-face)
869     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
870      . gnus-summary-normal-read-face)
871     ((> score default-high)
872      . gnus-summary-high-read-face)
873     ((< score default-low)
874      . gnus-summary-low-read-face)
875     (t
876      . gnus-summary-normal-read-face))
877   "*Controls the highlighting of summary buffer lines.
878
879 A list of (FORM . FACE) pairs.  When deciding how a a particular
880 summary line should be displayed, each form is evaluated.  The content
881 of the face field after the first true form is used.  You can change
882 how those summary lines are displayed, by editing the face field.
883
884 You can use the following variables in the FORM field.
885
886 score:        The article's score
887 default:      The default article score.
888 default-high: The default score for high scored articles.
889 default-low:  The default score for low scored articles.
890 below:        The score below which articles are automatically marked as read.
891 mark:         The articles mark."
892   :group 'gnus-summary-visual
893   :type '(repeat (cons (sexp :tag "Form" nil)
894                        face)))
895
896 (defcustom gnus-alter-header-function nil
897   "Function called to allow alteration of article header structures.
898 The function is called with one parameter, the article header vector,
899 which it may alter in any way.")
900
901 (defvar gnus-decode-encoded-word-function
902   (mime-find-field-decoder 'From 'nov)
903   "Variable that says which function should be used to decode a string with encoded words.")
904
905 (defcustom gnus-extra-headers nil
906   "*Extra headers to parse."
907   :version "21.1"
908   :group 'gnus-summary
909   :type '(repeat symbol))
910
911 (defcustom gnus-ignored-from-addresses
912   (and user-mail-address (regexp-quote user-mail-address))
913   "*Regexp of From headers that may be suppressed in favor of To headers."
914   :version "21.1"
915   :group 'gnus-summary
916   :type 'regexp)
917
918 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
919   "List of charsets that should be ignored.
920 When these charsets are used in the \"charset\" parameter, the
921 default charset will be used instead."
922   :version "21.1"
923   :type '(repeat symbol)
924   :group 'gnus-charset)
925
926 (gnus-define-group-parameter
927  ignored-charsets
928  :type list
929  :function-document
930  "Return the ignored charsets of GROUP."
931  :variable gnus-group-ignored-charsets-alist
932  :variable-default 
933  '(("alt\\.chinese\\.text" iso-8859-1))
934  :variable-document
935  "Alist of regexps (to match group names) and charsets that should be ignored.
936 When these charsets are used in the \"charset\" parameter, the
937 default charset will be used instead."
938  :variable-group gnus-charset
939  :variable-type '(repeat (cons (regexp :tag "Group")
940                                (repeat symbol)))
941  :parameter-type '(choice :tag "Ignored charsets" 
942                           :value nil
943                           (repeat (symbol)))
944  :parameter-document       "\
945 List of charsets that should be ignored.
946
947 When these charsets are used in the \"charset\" parameter, the
948 default charset will be used instead.")
949
950 (defcustom gnus-group-highlight-words-alist nil
951   "Alist of group regexps and highlight regexps.
952 This variable uses the same syntax as `gnus-emphasis-alist'."
953   :version "21.1"
954   :type '(repeat (cons (regexp :tag "Group")
955                        (repeat (list (regexp :tag "Highlight regexp")
956                                      (number :tag "Group for entire word" 0)
957                                      (number :tag "Group for displayed part" 0)
958                                      (symbol :tag "Face"
959                                              gnus-emphasis-highlight-words)))))
960   :group 'gnus-summary-visual)
961
962 (defcustom gnus-use-wheel nil
963   "Use Intelli-mouse on summary movement"
964   :type 'boolean
965   :group 'gnus-summary-maneuvering)
966
967 (defcustom gnus-wheel-scroll-amount '(5 . 1)
968   "Amount to scroll messages by spinning the mouse wheel.
969 This is actually a cons cell, where the first item is the amount to scroll
970 on a normal wheel event, and the second is the amount to scroll when the
971 wheel is moved with the shift key depressed."
972   :type '(cons (integer :tag "Shift") integer)
973   :group 'gnus-summary-maneuvering)
974
975 (defcustom gnus-wheel-edge-resistance 2
976   "How hard it should be to change the current article
977 by moving the mouse over the edge of the article window."
978   :type 'integer
979   :group 'gnus-summary-maneuvering)
980
981 (defcustom gnus-summary-show-article-charset-alist
982   nil
983   "Alist of number and charset.
984 The article will be shown with the charset corresponding to the
985 numbered argument.
986 For example: ((1 . cn-gb-2312) (2 . big5))."
987   :version "21.1"
988   :type '(repeat (cons (number :tag "Argument" 1)
989                        (symbol :tag "Charset")))
990   :group 'gnus-charset)
991
992 (defcustom gnus-preserve-marks t
993   "Whether marks are preserved when moving, copying and respooling messages."
994   :version "21.1"
995   :type 'boolean
996   :group 'gnus-summary-marks)
997
998 (defcustom gnus-alter-articles-to-read-function nil
999   "Function to be called to alter the list of articles to be selected."
1000   :type '(choice (const nil) function)
1001   :group 'gnus-summary)
1002
1003 (defcustom gnus-orphan-score nil
1004   "*All orphans get this score added.  Set in the score file."
1005   :group 'gnus-score-default
1006   :type '(choice (const nil)
1007                  integer))
1008
1009 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1010   "*A regexp to match MIME parts when saving multiple parts of a message
1011 with gnus-summary-save-parts (X m). This regexp will be used by default
1012 when prompting the user for which type of files to save."
1013   :group 'gnus-summary
1014   :type 'regexp)
1015
1016
1017 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1018   "*A regexp to match MIME parts when saving multiple parts of a message
1019 with gnus-summary-save-parts (X m). This regexp will be used by default
1020 when prompting the user for which type of files to save."
1021   :group 'gnus-summary
1022   :type 'regexp)
1023
1024 (defcustom gnus-read-all-available-headers nil
1025   "Whether Gnus should parse all headers made available to it.
1026 This is mostly relevant for slow backends where the user may
1027 wish to widen the summary buffer to include all headers
1028 that were fetched.  Say, for nnultimate groups."
1029   :group 'gnus-summary
1030   :type '(choice boolean regexp))
1031
1032 ;;; Internal variables
1033
1034 (defvar gnus-article-mime-handles nil)
1035 (defvar gnus-article-decoded-p nil)
1036 (defvar gnus-article-charset nil)
1037 (defvar gnus-article-ignored-charsets nil)
1038 (defvar gnus-scores-exclude-files nil)
1039 (defvar gnus-page-broken nil)
1040 (defvar gnus-inhibit-mime-unbuttonizing nil)
1041
1042 (defvar gnus-original-article nil)
1043 (defvar gnus-article-internal-prepare-hook nil)
1044 (defvar gnus-newsgroup-process-stack nil)
1045
1046 (defvar gnus-thread-indent-array nil)
1047 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1048 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1049   "Function called to sort the articles within a thread after it has been gathered together.")
1050
1051 (defvar gnus-summary-save-parts-type-history nil)
1052 (defvar gnus-summary-save-parts-last-directory nil)
1053
1054 (defvar gnus-summary-save-parts-type-history nil)
1055 (defvar gnus-summary-save-parts-last-directory nil)
1056
1057 ;; Avoid highlighting in kill files.
1058 (defvar gnus-summary-inhibit-highlight nil)
1059 (defvar gnus-newsgroup-selected-overlay nil)
1060 (defvar gnus-inhibit-limiting nil)
1061 (defvar gnus-newsgroup-adaptive-score-file nil)
1062 (defvar gnus-current-score-file nil)
1063 (defvar gnus-current-move-group nil)
1064 (defvar gnus-current-copy-group nil)
1065 (defvar gnus-current-crosspost-group nil)
1066
1067 (defvar gnus-newsgroup-dependencies nil)
1068 (defvar gnus-newsgroup-adaptive nil)
1069 (defvar gnus-summary-display-article-function nil)
1070 (defvar gnus-summary-highlight-line-function nil
1071   "Function called after highlighting a summary line.")
1072
1073 (defvar gnus-summary-line-format-alist
1074   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1075     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1076     (?s gnus-tmp-subject-or-nil ?s)
1077     (?n gnus-tmp-name ?s)
1078     (?A (std11-address-string
1079          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1080     (?a (or (std11-full-name-string
1081              (car (mime-entity-read-field gnus-tmp-header 'From)))
1082             gnus-tmp-from) ?s)
1083     (?F gnus-tmp-from ?s)
1084     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1085     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1086     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1087     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1088     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1089     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1090     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1091     (?L gnus-tmp-lines ?s)
1092     (?I gnus-tmp-indentation ?s)
1093     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1094     (?R gnus-tmp-replied ?c)
1095     (?\[ gnus-tmp-opening-bracket ?c)
1096     (?\] gnus-tmp-closing-bracket ?c)
1097     (?\> (make-string gnus-tmp-level ? ) ?s)
1098     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1099     (?i gnus-tmp-score ?d)
1100     (?z gnus-tmp-score-char ?c)
1101     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1102     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1103     (?U gnus-tmp-unread ?c)
1104     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1105     (?t (gnus-summary-number-of-articles-in-thread
1106          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1107         ?d)
1108     (?e (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1110         ?c)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?P (gnus-pick-line-number) ?d))
1113   "An alist of format specifications that can appear in summary lines.
1114 These are paired with what variables they correspond with, along with
1115 the type of the variable (string, integer, character, etc).")
1116
1117 (defvar gnus-summary-dummy-line-format-alist
1118   `((?S gnus-tmp-subject ?s)
1119     (?N gnus-tmp-number ?d)
1120     (?u gnus-tmp-user-defined ?s)))
1121
1122 (defvar gnus-summary-mode-line-format-alist
1123   `((?G gnus-tmp-group-name ?s)
1124     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1125     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1126     (?A gnus-tmp-article-number ?d)
1127     (?Z gnus-tmp-unread-and-unselected ?s)
1128     (?V gnus-version ?s)
1129     (?U gnus-tmp-unread-and-unticked ?d)
1130     (?S gnus-tmp-subject ?s)
1131     (?e gnus-tmp-unselected ?d)
1132     (?u gnus-tmp-user-defined ?s)
1133     (?d (length gnus-newsgroup-dormant) ?d)
1134     (?t (length gnus-newsgroup-marked) ?d)
1135     (?r (length gnus-newsgroup-reads) ?d)
1136     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1137     (?E gnus-newsgroup-expunged-tally ?d)
1138     (?s (gnus-current-score-file-nondirectory) ?s)))
1139
1140 (defvar gnus-last-search-regexp nil
1141   "Default regexp for article search command.")
1142
1143 (defvar gnus-summary-search-article-matched-data nil
1144   "Last matched data of article search command.  It is the local variable
1145 in `gnus-article-buffer' which consists of the list of start position,
1146 end position and text.")
1147
1148 (defvar gnus-last-shell-command nil
1149   "Default shell command on article.")
1150
1151 (defvar gnus-newsgroup-begin nil)
1152 (defvar gnus-newsgroup-end nil)
1153 (defvar gnus-newsgroup-last-rmail nil)
1154 (defvar gnus-newsgroup-last-mail nil)
1155 (defvar gnus-newsgroup-last-folder nil)
1156 (defvar gnus-newsgroup-last-file nil)
1157 (defvar gnus-newsgroup-auto-expire nil)
1158 (defvar gnus-newsgroup-active nil)
1159
1160 (defvar gnus-newsgroup-data nil)
1161 (defvar gnus-newsgroup-data-reverse nil)
1162 (defvar gnus-newsgroup-limit nil)
1163 (defvar gnus-newsgroup-limits nil)
1164
1165 (defvar gnus-newsgroup-unreads nil
1166   "List of unread articles in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-unselected nil
1169   "List of unselected unread articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-reads nil
1172   "Alist of read articles and article marks in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-expunged-tally nil)
1175
1176 (defvar gnus-newsgroup-marked nil
1177   "List of ticked articles in the current newsgroup (a subset of unread art).")
1178
1179 (defvar gnus-newsgroup-killed nil
1180   "List of ranges of articles that have been through the scoring process.")
1181
1182 (defvar gnus-newsgroup-cached nil
1183   "List of articles that come from the article cache.")
1184
1185 (defvar gnus-newsgroup-saved nil
1186   "List of articles that have been saved.")
1187
1188 (defvar gnus-newsgroup-kill-headers nil)
1189
1190 (defvar gnus-newsgroup-replied nil
1191   "List of articles that have been replied to in the current newsgroup.")
1192
1193 (defvar gnus-newsgroup-forwarded nil
1194   "List of articles that have been forwarded in the current newsgroup.")
1195
1196 (defvar gnus-newsgroup-expirable nil
1197   "List of articles in the current newsgroup that can be expired.")
1198
1199 (defvar gnus-newsgroup-processable nil
1200   "List of articles in the current newsgroup that can be processed.")
1201
1202 (defvar gnus-newsgroup-downloadable nil
1203   "List of articles in the current newsgroup that can be processed.")
1204
1205 (defvar gnus-newsgroup-undownloaded nil
1206   "List of articles in the current newsgroup that haven't been downloaded..")
1207
1208 (defvar gnus-newsgroup-unsendable nil
1209   "List of articles in the current newsgroup that won't be sent.")
1210
1211 (defvar gnus-newsgroup-bookmarks nil
1212   "List of articles in the current newsgroup that have bookmarks.")
1213
1214 (defvar gnus-newsgroup-dormant nil
1215   "List of dormant articles in the current newsgroup.")
1216
1217 (defvar gnus-newsgroup-scored nil
1218   "List of scored articles in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-incorporated nil
1221   "List of incorporated articles in the current newsgroup.")
1222
1223 (defvar gnus-newsgroup-headers nil
1224   "List of article headers in the current newsgroup.")
1225
1226 (defvar gnus-newsgroup-threads nil)
1227
1228 (defvar gnus-newsgroup-prepared nil
1229   "Whether the current group has been prepared properly.")
1230
1231 (defvar gnus-newsgroup-ancient nil
1232   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-sparse nil)
1235
1236 (defvar gnus-current-article nil)
1237 (defvar gnus-article-current nil)
1238 (defvar gnus-current-headers nil)
1239 (defvar gnus-have-all-headers nil)
1240 (defvar gnus-last-article nil)
1241 (defvar gnus-newsgroup-history nil)
1242 (defvar gnus-newsgroup-charset nil)
1243 (defvar gnus-newsgroup-ephemeral-charset nil)
1244 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1245
1246 (defvar gnus-article-before-search nil)
1247
1248 (defconst gnus-summary-local-variables
1249   '(gnus-newsgroup-name
1250     gnus-newsgroup-begin gnus-newsgroup-end
1251     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1252     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1253     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1254     gnus-newsgroup-unselected gnus-newsgroup-marked
1255     gnus-newsgroup-reads gnus-newsgroup-saved
1256     gnus-newsgroup-replied gnus-newsgroup-forwarded
1257     gnus-newsgroup-expirable
1258     gnus-newsgroup-processable gnus-newsgroup-killed
1259     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1260     gnus-newsgroup-unsendable
1261     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1262     gnus-newsgroup-headers gnus-newsgroup-threads
1263     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1264     gnus-current-article gnus-current-headers gnus-have-all-headers
1265     gnus-last-article gnus-article-internal-prepare-hook
1266     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1267     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1268     gnus-thread-expunge-below
1269     gnus-score-alist gnus-current-score-file
1270     (gnus-summary-expunge-below . global)
1271     (gnus-summary-mark-below . global)
1272     (gnus-orphan-score . global)
1273     gnus-newsgroup-active gnus-scores-exclude-files
1274     gnus-newsgroup-history gnus-newsgroup-ancient
1275     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1276     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1277     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1278     (gnus-newsgroup-expunged-tally . 0)
1279     gnus-cache-removable-articles gnus-newsgroup-cached
1280     gnus-newsgroup-data gnus-newsgroup-data-reverse
1281     gnus-newsgroup-limit gnus-newsgroup-limits
1282     gnus-newsgroup-charset
1283     gnus-newsgroup-incorporated)
1284   "Variables that are buffer-local to the summary buffers.")
1285
1286 (defvar gnus-newsgroup-variables nil
1287   "Variables that have separate values in the newsgroups.")
1288
1289 ;; Byte-compiler warning.
1290 (eval-when-compile (defvar gnus-article-mode-map))
1291
1292 ;; Subject simplification.
1293
1294 (defun gnus-simplify-whitespace (str)
1295   "Remove excessive whitespace from STR."
1296   (let ((mystr str))
1297     ;; Multiple spaces.
1298     (while (string-match "[ \t][ \t]+" mystr)
1299       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1300                           " "
1301                           (substring mystr (match-end 0)))))
1302     ;; Leading spaces.
1303     (when (string-match "^[ \t]+" mystr)
1304       (setq mystr (substring mystr (match-end 0))))
1305     ;; Trailing spaces.
1306     (when (string-match "[ \t]+$" mystr)
1307       (setq mystr (substring mystr 0 (match-beginning 0))))
1308     mystr))
1309
1310 (defsubst gnus-simplify-subject-re (subject)
1311   "Remove \"Re:\" from subject lines."
1312   (if (string-match message-subject-re-regexp subject)
1313       (substring subject (match-end 0))
1314     subject))
1315
1316 (defun gnus-simplify-subject (subject &optional re-only)
1317   "Remove `Re:' and words in parentheses.
1318 If RE-ONLY is non-nil, strip leading `Re:'s only."
1319   (let ((case-fold-search t))           ;Ignore case.
1320     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1321     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1322       (setq subject (substring subject (match-end 0))))
1323     ;; Remove uninteresting prefixes.
1324     (when (and (not re-only)
1325                gnus-simplify-ignored-prefixes
1326                (string-match gnus-simplify-ignored-prefixes subject))
1327       (setq subject (substring subject (match-end 0))))
1328     ;; Remove words in parentheses from end.
1329     (unless re-only
1330       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1331         (setq subject (substring subject 0 (match-beginning 0)))))
1332     ;; Return subject string.
1333     subject))
1334
1335 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1336 ;; all whitespace.
1337 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1338   (goto-char (point-min))
1339   (while (re-search-forward regexp nil t)
1340     (replace-match (or newtext ""))))
1341
1342 (defun gnus-simplify-buffer-fuzzy ()
1343   "Simplify string in the buffer fuzzily.
1344 The string in the accessible portion of the current buffer is simplified.
1345 It is assumed to be a single-line subject.
1346 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1347 matter is removed.  Additional things can be deleted by setting
1348 `gnus-simplify-subject-fuzzy-regexp'."
1349   (let ((case-fold-search t)
1350         (modified-tick))
1351     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1352
1353     (while (not (eq modified-tick (buffer-modified-tick)))
1354       (setq modified-tick (buffer-modified-tick))
1355       (cond
1356        ((listp gnus-simplify-subject-fuzzy-regexp)
1357         (mapcar 'gnus-simplify-buffer-fuzzy-step
1358                 gnus-simplify-subject-fuzzy-regexp))
1359        (gnus-simplify-subject-fuzzy-regexp
1360         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1361       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1362       (gnus-simplify-buffer-fuzzy-step
1363        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1364       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1365
1366     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1367     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1368     (gnus-simplify-buffer-fuzzy-step " $")
1369     (gnus-simplify-buffer-fuzzy-step "^ +")))
1370
1371 (defun gnus-simplify-subject-fuzzy (subject)
1372   "Simplify a subject string fuzzily.
1373 See `gnus-simplify-buffer-fuzzy' for details."
1374   (save-excursion
1375     (gnus-set-work-buffer)
1376     (let ((case-fold-search t))
1377       ;; Remove uninteresting prefixes.
1378       (when (and gnus-simplify-ignored-prefixes
1379                  (string-match gnus-simplify-ignored-prefixes subject))
1380         (setq subject (substring subject (match-end 0))))
1381       (insert subject)
1382       (inline (gnus-simplify-buffer-fuzzy))
1383       (buffer-string))))
1384
1385 (defsubst gnus-simplify-subject-fully (subject)
1386   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1387   (cond
1388    (gnus-simplify-subject-functions
1389     (gnus-map-function gnus-simplify-subject-functions subject))
1390    ((null gnus-summary-gather-subject-limit)
1391     (gnus-simplify-subject-re subject))
1392    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1393     (gnus-simplify-subject-fuzzy subject))
1394    ((numberp gnus-summary-gather-subject-limit)
1395     (gnus-limit-string (gnus-simplify-subject-re subject)
1396                        gnus-summary-gather-subject-limit))
1397    (t
1398     subject)))
1399
1400 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1401   "Check whether two subjects are equal.
1402 If optional argument simple-first is t, first argument is already
1403 simplified."
1404   (cond
1405    ((null simple-first)
1406     (equal (gnus-simplify-subject-fully s1)
1407            (gnus-simplify-subject-fully s2)))
1408    (t
1409     (equal s1
1410            (gnus-simplify-subject-fully s2)))))
1411
1412 (defun gnus-summary-bubble-group ()
1413   "Increase the score of the current group.
1414 This is a handy function to add to `gnus-summary-exit-hook' to
1415 increase the score of each group you read."
1416   (gnus-group-add-score gnus-newsgroup-name))
1417
1418 \f
1419 ;;;
1420 ;;; Gnus summary mode
1421 ;;;
1422
1423 (put 'gnus-summary-mode 'mode-class 'special)
1424
1425 (defvar gnus-article-commands-menu)
1426
1427 (when t
1428   ;; Non-orthogonal keys
1429
1430   (gnus-define-keys gnus-summary-mode-map
1431     " " gnus-summary-next-page
1432     "\177" gnus-summary-prev-page
1433     [delete] gnus-summary-prev-page
1434     [backspace] gnus-summary-prev-page
1435     "\r" gnus-summary-scroll-up
1436     "\M-\r" gnus-summary-scroll-down
1437     "n" gnus-summary-next-unread-article
1438     "p" gnus-summary-prev-unread-article
1439     "N" gnus-summary-next-article
1440     "P" gnus-summary-prev-article
1441     "\M-\C-n" gnus-summary-next-same-subject
1442     "\M-\C-p" gnus-summary-prev-same-subject
1443     "\M-n" gnus-summary-next-unread-subject
1444     "\M-p" gnus-summary-prev-unread-subject
1445     "." gnus-summary-first-unread-article
1446     "," gnus-summary-best-unread-article
1447     "\M-s" gnus-summary-search-article-forward
1448     "\M-r" gnus-summary-search-article-backward
1449     "<" gnus-summary-beginning-of-article
1450     ">" gnus-summary-end-of-article
1451     "j" gnus-summary-goto-article
1452     "^" gnus-summary-refer-parent-article
1453     "\M-^" gnus-summary-refer-article
1454     "u" gnus-summary-tick-article-forward
1455     "!" gnus-summary-tick-article-forward
1456     "U" gnus-summary-tick-article-backward
1457     "d" gnus-summary-mark-as-read-forward
1458     "D" gnus-summary-mark-as-read-backward
1459     "E" gnus-summary-mark-as-expirable
1460     "\M-u" gnus-summary-clear-mark-forward
1461     "\M-U" gnus-summary-clear-mark-backward
1462     "k" gnus-summary-kill-same-subject-and-select
1463     "\C-k" gnus-summary-kill-same-subject
1464     "\M-\C-k" gnus-summary-kill-thread
1465     "\M-\C-l" gnus-summary-lower-thread
1466     "e" gnus-summary-edit-article
1467     "#" gnus-summary-mark-as-processable
1468     "\M-#" gnus-summary-unmark-as-processable
1469     "\M-\C-t" gnus-summary-toggle-threads
1470     "\M-\C-s" gnus-summary-show-thread
1471     "\M-\C-h" gnus-summary-hide-thread
1472     "\M-\C-f" gnus-summary-next-thread
1473     "\M-\C-b" gnus-summary-prev-thread
1474     [(meta down)] gnus-summary-next-thread
1475     [(meta up)] gnus-summary-prev-thread
1476     "\M-\C-u" gnus-summary-up-thread
1477     "\M-\C-d" gnus-summary-down-thread
1478     "&" gnus-summary-execute-command
1479     "c" gnus-summary-catchup-and-exit
1480     "\C-w" gnus-summary-mark-region-as-read
1481     "\C-t" gnus-summary-toggle-truncation
1482     "?" gnus-summary-mark-as-dormant
1483     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1484     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1485     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1486     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1487     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1488     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1489     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1490     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1491     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1492     "=" gnus-summary-expand-window
1493     "\C-x\C-s" gnus-summary-reselect-current-group
1494     "\M-g" gnus-summary-rescan-group
1495     "w" gnus-summary-stop-page-breaking
1496     "\C-c\C-r" gnus-summary-caesar-message
1497     "\M-t" gnus-summary-toggle-mime
1498     "f" gnus-summary-followup
1499     "F" gnus-summary-followup-with-original
1500     "C" gnus-summary-cancel-article
1501     "r" gnus-summary-reply
1502     "R" gnus-summary-reply-with-original
1503     "\C-c\C-f" gnus-summary-mail-forward
1504     "o" gnus-summary-save-article
1505     "\C-o" gnus-summary-save-article-mail
1506     "|" gnus-summary-pipe-output
1507     "\M-k" gnus-summary-edit-local-kill
1508     "\M-K" gnus-summary-edit-global-kill
1509     ;; "V" gnus-version
1510     "\C-c\C-d" gnus-summary-describe-group
1511     "q" gnus-summary-exit
1512     "Q" gnus-summary-exit-no-update
1513     "\C-c\C-i" gnus-info-find-node
1514     gnus-mouse-2 gnus-mouse-pick-article
1515     "m" gnus-summary-mail-other-window
1516     "a" gnus-summary-post-news
1517     "x" gnus-summary-limit-to-unread
1518     "s" gnus-summary-isearch-article
1519     "t" gnus-article-toggle-headers
1520     "g" gnus-summary-show-article
1521     "l" gnus-summary-goto-last-article
1522     "v" gnus-summary-preview-mime-message
1523     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1524     "\C-d" gnus-summary-enter-digest-group
1525     "\M-\C-d" gnus-summary-read-document
1526     "\M-\C-e" gnus-summary-edit-parameters
1527     "\M-\C-a" gnus-summary-customize-parameters
1528     "\C-c\C-b" gnus-bug
1529     "*" gnus-cache-enter-article
1530     "\M-*" gnus-cache-remove-article
1531     "\M-&" gnus-summary-universal-argument
1532     "\C-l" gnus-recenter
1533     "I" gnus-summary-increase-score
1534     "L" gnus-summary-lower-score
1535     "\M-i" gnus-symbolic-argument
1536     "h" gnus-summary-select-article-buffer
1537
1538     "V" gnus-summary-score-map
1539     "X" gnus-uu-extract-map
1540     "S" gnus-summary-send-map)
1541
1542   ;; Sort of orthogonal keymap
1543   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1544     "t" gnus-summary-tick-article-forward
1545     "!" gnus-summary-tick-article-forward
1546     "d" gnus-summary-mark-as-read-forward
1547     "r" gnus-summary-mark-as-read-forward
1548     "c" gnus-summary-clear-mark-forward
1549     " " gnus-summary-clear-mark-forward
1550     "e" gnus-summary-mark-as-expirable
1551     "x" gnus-summary-mark-as-expirable
1552     "?" gnus-summary-mark-as-dormant
1553     "b" gnus-summary-set-bookmark
1554     "B" gnus-summary-remove-bookmark
1555     "#" gnus-summary-mark-as-processable
1556     "\M-#" gnus-summary-unmark-as-processable
1557     "S" gnus-summary-limit-include-expunged
1558     "C" gnus-summary-catchup
1559     "H" gnus-summary-catchup-to-here
1560     "h" gnus-summary-catchup-from-here
1561     "\C-c" gnus-summary-catchup-all
1562     "k" gnus-summary-kill-same-subject-and-select
1563     "K" gnus-summary-kill-same-subject
1564     "P" gnus-uu-mark-map)
1565
1566   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1567     "c" gnus-summary-clear-above
1568     "u" gnus-summary-tick-above
1569     "m" gnus-summary-mark-above
1570     "k" gnus-summary-kill-below)
1571
1572   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1573     "/" gnus-summary-limit-to-subject
1574     "n" gnus-summary-limit-to-articles
1575     "w" gnus-summary-pop-limit
1576     "s" gnus-summary-limit-to-subject
1577     "a" gnus-summary-limit-to-author
1578     "u" gnus-summary-limit-to-unread
1579     "m" gnus-summary-limit-to-marks
1580     "M" gnus-summary-limit-exclude-marks
1581     "v" gnus-summary-limit-to-score
1582     "*" gnus-summary-limit-include-cached
1583     "D" gnus-summary-limit-include-dormant
1584     "T" gnus-summary-limit-include-thread
1585     "d" gnus-summary-limit-exclude-dormant
1586     "t" gnus-summary-limit-to-age
1587     "x" gnus-summary-limit-to-extra
1588     "E" gnus-summary-limit-include-expunged
1589     "c" gnus-summary-limit-exclude-childless-dormant
1590     "C" gnus-summary-limit-mark-excluded-as-read
1591     "o" gnus-summary-insert-old-articles
1592     "N" gnus-summary-insert-new-articles)
1593
1594   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1595     "n" gnus-summary-next-unread-article
1596     "p" gnus-summary-prev-unread-article
1597     "N" gnus-summary-next-article
1598     "P" gnus-summary-prev-article
1599     "\C-n" gnus-summary-next-same-subject
1600     "\C-p" gnus-summary-prev-same-subject
1601     "\M-n" gnus-summary-next-unread-subject
1602     "\M-p" gnus-summary-prev-unread-subject
1603     "f" gnus-summary-first-unread-article
1604     "b" gnus-summary-best-unread-article
1605     "j" gnus-summary-goto-article
1606     "g" gnus-summary-goto-subject
1607     "l" gnus-summary-goto-last-article
1608     "o" gnus-summary-pop-article)
1609
1610   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1611     "k" gnus-summary-kill-thread
1612     "l" gnus-summary-lower-thread
1613     "i" gnus-summary-raise-thread
1614     "T" gnus-summary-toggle-threads
1615     "t" gnus-summary-rethread-current
1616     "^" gnus-summary-reparent-thread
1617     "s" gnus-summary-show-thread
1618     "S" gnus-summary-show-all-threads
1619     "h" gnus-summary-hide-thread
1620     "H" gnus-summary-hide-all-threads
1621     "n" gnus-summary-next-thread
1622     "p" gnus-summary-prev-thread
1623     "u" gnus-summary-up-thread
1624     "o" gnus-summary-top-thread
1625     "d" gnus-summary-down-thread
1626     "#" gnus-uu-mark-thread
1627     "\M-#" gnus-uu-unmark-thread)
1628
1629   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1630     "g" gnus-summary-prepare
1631     "c" gnus-summary-insert-cached-articles)
1632
1633   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1634     "c" gnus-summary-catchup-and-exit
1635     "C" gnus-summary-catchup-all-and-exit
1636     "E" gnus-summary-exit-no-update
1637     "J" gnus-summary-jump-to-other-group
1638     "Q" gnus-summary-exit
1639     "Z" gnus-summary-exit
1640     "n" gnus-summary-catchup-and-goto-next-group
1641     "R" gnus-summary-reselect-current-group
1642     "G" gnus-summary-rescan-group
1643     "N" gnus-summary-next-group
1644     "s" gnus-summary-save-newsrc
1645     "P" gnus-summary-prev-group)
1646
1647   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1648     " " gnus-summary-next-page
1649     "n" gnus-summary-next-page
1650     "\177" gnus-summary-prev-page
1651     [delete] gnus-summary-prev-page
1652     "p" gnus-summary-prev-page
1653     "\r" gnus-summary-scroll-up
1654     "\M-\r" gnus-summary-scroll-down
1655     "<" gnus-summary-beginning-of-article
1656     ">" gnus-summary-end-of-article
1657     "b" gnus-summary-beginning-of-article
1658     "e" gnus-summary-end-of-article
1659     "^" gnus-summary-refer-parent-article
1660     "r" gnus-summary-refer-parent-article
1661     "D" gnus-summary-enter-digest-group
1662     "R" gnus-summary-refer-references
1663     "T" gnus-summary-refer-thread
1664     "g" gnus-summary-show-article
1665     "s" gnus-summary-isearch-article
1666     "P" gnus-summary-print-article
1667     "M" gnus-mailing-list-insinuate
1668     "t" gnus-article-babel)
1669
1670   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1671     "b" gnus-article-add-buttons
1672     "B" gnus-article-add-buttons-to-head
1673     "o" gnus-article-treat-overstrike
1674     "e" gnus-article-emphasize
1675     "w" gnus-article-fill-cited-article
1676     "Q" gnus-article-fill-long-lines
1677     "C" gnus-article-capitalize-sentences
1678     "c" gnus-article-remove-cr
1679     "Z" gnus-article-decode-HZ
1680     "f" gnus-article-display-x-face
1681     "l" gnus-summary-stop-page-breaking
1682     "r" gnus-summary-caesar-message
1683     "t" gnus-article-toggle-headers
1684     "v" gnus-summary-verbose-headers
1685     "m" gnus-summary-toggle-mime
1686     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1687     "p" gnus-article-verify-x-pgp-sig
1688     "d" gnus-article-treat-dumbquotes
1689     "s" gnus-smiley-display)
1690
1691   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1692     "a" gnus-article-hide
1693     "h" gnus-article-toggle-headers
1694     "b" gnus-article-hide-boring-headers
1695     "s" gnus-article-hide-signature
1696     "c" gnus-article-hide-citation
1697     "C" gnus-article-hide-citation-in-followups
1698     "l" gnus-article-hide-list-identifiers
1699     "p" gnus-article-hide-pgp
1700     "B" gnus-article-strip-banner
1701     "P" gnus-article-hide-pem
1702     "\C-c" gnus-article-hide-citation-maybe)
1703
1704   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1705     "a" gnus-article-highlight
1706     "h" gnus-article-highlight-headers
1707     "c" gnus-article-highlight-citation
1708     "s" gnus-article-highlight-signature)
1709
1710   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1711     "z" gnus-article-date-ut
1712     "u" gnus-article-date-ut
1713     "l" gnus-article-date-local
1714     "p" gnus-article-date-english
1715     "e" gnus-article-date-lapsed
1716     "o" gnus-article-date-original
1717     "i" gnus-article-date-iso8601
1718     "s" gnus-article-date-user)
1719
1720   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1721     "t" gnus-article-remove-trailing-blank-lines
1722     "l" gnus-article-strip-leading-blank-lines
1723     "m" gnus-article-strip-multiple-blank-lines
1724     "a" gnus-article-strip-blank-lines
1725     "A" gnus-article-strip-all-blank-lines
1726     "s" gnus-article-strip-leading-space
1727     "e" gnus-article-strip-trailing-space
1728     "w" gnus-article-remove-leading-whitespace)
1729
1730   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1731     "v" gnus-version
1732     "f" gnus-summary-fetch-faq
1733     "d" gnus-summary-describe-group
1734     "h" gnus-summary-describe-briefly
1735     "i" gnus-info-find-node)
1736
1737   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1738     "e" gnus-summary-expire-articles
1739     "\M-\C-e" gnus-summary-expire-articles-now
1740     "\177" gnus-summary-delete-article
1741     [delete] gnus-summary-delete-article
1742     [backspace] gnus-summary-delete-article
1743     "m" gnus-summary-move-article
1744     "r" gnus-summary-respool-article
1745     "w" gnus-summary-edit-article
1746     "c" gnus-summary-copy-article
1747     "B" gnus-summary-crosspost-article
1748     "q" gnus-summary-respool-query
1749     "t" gnus-summary-respool-trace
1750     "i" gnus-summary-import-article
1751     "I" gnus-summary-create-article
1752     "p" gnus-summary-article-posted-p)
1753
1754   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1755     "o" gnus-summary-save-article
1756     "m" gnus-summary-save-article-mail
1757     "F" gnus-summary-write-article-file
1758     "r" gnus-summary-save-article-rmail
1759     "f" gnus-summary-save-article-file
1760     "b" gnus-summary-save-article-body-file
1761     "h" gnus-summary-save-article-folder
1762     "v" gnus-summary-save-article-vm
1763     "p" gnus-summary-pipe-output
1764     "s" gnus-soup-add-article)
1765
1766   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1767     "b" gnus-summary-display-buttonized
1768     "m" gnus-summary-repair-multipart
1769     "v" gnus-article-view-part
1770     "o" gnus-article-save-part
1771     "c" gnus-article-copy-part
1772     "C" gnus-article-view-part-as-charset
1773     "e" gnus-article-externalize-part
1774     "E" gnus-article-encrypt-body
1775     "i" gnus-article-inline-part
1776     "|" gnus-article-pipe-part))
1777
1778 (defvar gnus-article-post-menu nil)
1779
1780 (defun gnus-summary-make-menu-bar ()
1781   (gnus-turn-off-edit-menu 'summary)
1782
1783   (unless (boundp 'gnus-summary-misc-menu)
1784
1785     (easy-menu-define
1786      gnus-summary-kill-menu gnus-summary-mode-map ""
1787      (cons
1788       "Score"
1789       (nconc
1790        (list
1791         ["Customize" gnus-score-customize t])
1792        (gnus-make-score-map 'increase)
1793        (gnus-make-score-map 'lower)
1794        '(("Mark"
1795           ["Kill below" gnus-summary-kill-below t]
1796           ["Mark above" gnus-summary-mark-above t]
1797           ["Tick above" gnus-summary-tick-above t]
1798           ["Clear above" gnus-summary-clear-above t])
1799          ["Current score" gnus-summary-current-score t]
1800          ["Set score" gnus-summary-set-score t]
1801          ["Switch current score file..." gnus-score-change-score-file t]
1802          ["Set mark below..." gnus-score-set-mark-below t]
1803          ["Set expunge below..." gnus-score-set-expunge-below t]
1804          ["Edit current score file" gnus-score-edit-current-scores t]
1805          ["Edit score file" gnus-score-edit-file t]
1806          ["Trace score" gnus-score-find-trace t]
1807          ["Find words" gnus-score-find-favourite-words t]
1808          ["Rescore buffer" gnus-summary-rescore t]
1809          ["Increase score..." gnus-summary-increase-score t]
1810          ["Lower score..." gnus-summary-lower-score t]))))
1811
1812     ;; Define both the Article menu in the summary buffer and the equivalent
1813     ;; Commands menu in the article buffer here for consistency.
1814     (let ((innards
1815            `(("Hide"
1816               ["All" gnus-article-hide t]
1817               ["Headers" gnus-article-toggle-headers t]
1818               ["Signature" gnus-article-hide-signature t]
1819               ["Citation" gnus-article-hide-citation t]
1820               ["List identifiers" gnus-article-hide-list-identifiers t]
1821               ["PGP" gnus-article-hide-pgp t]
1822               ["Banner" gnus-article-strip-banner t]
1823               ["Boring headers" gnus-article-hide-boring-headers t])
1824              ("Highlight"
1825               ["All" gnus-article-highlight t]
1826               ["Headers" gnus-article-highlight-headers t]
1827               ["Signature" gnus-article-highlight-signature t]
1828               ["Citation" gnus-article-highlight-citation t])
1829              ("Date"
1830               ["Local" gnus-article-date-local t]
1831               ["ISO8601" gnus-article-date-iso8601 t]
1832               ["UT" gnus-article-date-ut t]
1833               ["Original" gnus-article-date-original t]
1834               ["Lapsed" gnus-article-date-lapsed t]
1835               ["User-defined" gnus-article-date-user t])
1836              ("Washing"
1837               ("Remove Blanks"
1838                ["Leading" gnus-article-strip-leading-blank-lines t]
1839                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1840                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1841                ["All of the above" gnus-article-strip-blank-lines t]
1842                ["All" gnus-article-strip-all-blank-lines t]
1843                ["Leading space" gnus-article-strip-leading-space t]
1844                ["Trailing space" gnus-article-strip-trailing-space t]
1845                ["Leading space in headers" 
1846                 gnus-article-remove-leading-whitespace t])
1847               ["Overstrike" gnus-article-treat-overstrike t]
1848               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1849               ["Emphasis" gnus-article-emphasize t]
1850               ["Word wrap" gnus-article-fill-cited-article t]
1851               ["Fill long lines" gnus-article-fill-long-lines t]
1852               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1853               ["CR" gnus-article-remove-cr t]
1854               ["Show X-Face" gnus-article-display-x-face t]
1855               ["Rot 13" gnus-summary-caesar-message
1856                ,@(if (featurep 'xemacs) '(t)
1857                    '(:help "\"Caesar rotate\" article by 13"))]
1858               ["Unix pipe" gnus-summary-pipe-message t]
1859               ["Add buttons" gnus-article-add-buttons t]
1860               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1861               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1862               ["Toggle MIME" gnus-summary-toggle-mime t]
1863               ["Verbose header" gnus-summary-verbose-headers t]
1864               ["Toggle header" gnus-summary-toggle-header t]
1865               ["Toggle smileys" gnus-smiley-display t]
1866               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1867               ["HZ" gnus-article-decode-HZ t])
1868              ("Output"
1869               ["Save in default format" gnus-summary-save-article
1870                ,@(if (featurep 'xemacs) '(t)
1871                    '(:help "Save article using default method"))]
1872               ["Save in file" gnus-summary-save-article-file
1873                ,@(if (featurep 'xemacs) '(t)
1874                    '(:help "Save article in file"))]
1875               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1876               ["Save in MH folder" gnus-summary-save-article-folder t]
1877               ["Save in VM folder" gnus-summary-save-article-vm t]
1878               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1879               ["Save body in file" gnus-summary-save-article-body-file t]
1880               ["Pipe through a filter" gnus-summary-pipe-output t]
1881               ["Add to SOUP packet" gnus-soup-add-article t]
1882               ["Print" gnus-summary-print-article t])
1883              ("Backend"
1884               ["Respool article..." gnus-summary-respool-article t]
1885               ["Move article..." gnus-summary-move-article
1886                (gnus-check-backend-function
1887                 'request-move-article gnus-newsgroup-name)]
1888               ["Copy article..." gnus-summary-copy-article t]
1889               ["Crosspost article..." gnus-summary-crosspost-article
1890                (gnus-check-backend-function
1891                 'request-replace-article gnus-newsgroup-name)]
1892               ["Import file..." gnus-summary-import-article t]
1893               ["Create article..." gnus-summary-create-article t]
1894               ["Check if posted" gnus-summary-article-posted-p t]
1895               ["Edit article" gnus-summary-edit-article
1896                (not (gnus-group-read-only-p))]
1897               ["Delete article" gnus-summary-delete-article
1898                (gnus-check-backend-function
1899                 'request-expire-articles gnus-newsgroup-name)]
1900               ["Query respool" gnus-summary-respool-query t]
1901               ["Trace respool" gnus-summary-respool-trace t]
1902               ["Delete expirable articles" gnus-summary-expire-articles-now
1903                (gnus-check-backend-function
1904                 'request-expire-articles gnus-newsgroup-name)])
1905              ("Extract"
1906               ["Uudecode" gnus-uu-decode-uu
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Decode uuencoded article(s)"))]
1909               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1910               ["Unshar" gnus-uu-decode-unshar t]
1911               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1912               ["Save" gnus-uu-decode-save t]
1913               ["Binhex" gnus-uu-decode-binhex t]
1914               ["Postscript" gnus-uu-decode-postscript t])
1915              ("Cache"
1916               ["Enter article" gnus-cache-enter-article t]
1917               ["Remove article" gnus-cache-remove-article t])
1918              ["Translate" gnus-article-babel t]
1919              ["Select article buffer" gnus-summary-select-article-buffer t]
1920              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1921              ["Isearch article..." gnus-summary-isearch-article t]
1922              ["Beginning of the article" gnus-summary-beginning-of-article t]
1923              ["End of the article" gnus-summary-end-of-article t]
1924              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1925              ["Fetch referenced articles" gnus-summary-refer-references t]
1926              ["Fetch current thread" gnus-summary-refer-thread t]
1927              ["Fetch article with id..." gnus-summary-refer-article t]
1928              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1929              ["Redisplay" gnus-summary-show-article t])))
1930       (easy-menu-define
1931        gnus-summary-article-menu gnus-summary-mode-map ""
1932        (cons "Article" innards))
1933
1934       (if (not (keymapp gnus-summary-article-menu))
1935           (easy-menu-define
1936             gnus-article-commands-menu gnus-article-mode-map ""
1937             (cons "Commands" innards))
1938         ;; in Emacs, don't share menu.
1939         (setq gnus-article-commands-menu 
1940               (copy-keymap gnus-summary-article-menu))
1941         (define-key gnus-article-mode-map [menu-bar commands]
1942           (cons "Commands" gnus-article-commands-menu))))
1943
1944     (easy-menu-define
1945      gnus-summary-thread-menu gnus-summary-mode-map ""
1946      '("Threads"
1947        ["Toggle threading" gnus-summary-toggle-threads t]
1948        ["Hide threads" gnus-summary-hide-all-threads t]
1949        ["Show threads" gnus-summary-show-all-threads t]
1950        ["Hide thread" gnus-summary-hide-thread t]
1951        ["Show thread" gnus-summary-show-thread t]
1952        ["Go to next thread" gnus-summary-next-thread t]
1953        ["Go to previous thread" gnus-summary-prev-thread t]
1954        ["Go down thread" gnus-summary-down-thread t]
1955        ["Go up thread" gnus-summary-up-thread t]
1956        ["Top of thread" gnus-summary-top-thread t]
1957        ["Mark thread as read" gnus-summary-kill-thread t]
1958        ["Lower thread score" gnus-summary-lower-thread t]
1959        ["Raise thread score" gnus-summary-raise-thread t]
1960        ["Rethread current" gnus-summary-rethread-current t]))
1961
1962     (easy-menu-define
1963      gnus-summary-post-menu gnus-summary-mode-map ""
1964      `("Post"
1965        ["Post an article" gnus-summary-post-news
1966         ,@(if (featurep 'xemacs) '(t)
1967             '(:help "Post an article"))]
1968        ["Followup" gnus-summary-followup
1969         ,@(if (featurep 'xemacs) '(t)
1970             '(:help "Post followup to this article"))]
1971        ["Followup and yank" gnus-summary-followup-with-original
1972         ,@(if (featurep 'xemacs) '(t)
1973             '(:help "Post followup to this article, quoting its contents"))]
1974        ["Supersede article" gnus-summary-supersede-article t]
1975        ["Cancel article" gnus-summary-cancel-article
1976         ,@(if (featurep 'xemacs) '(t)
1977             '(:help "Cancel an article you posted"))]
1978        ["Reply" gnus-summary-reply t]
1979        ["Reply and yank" gnus-summary-reply-with-original t]
1980        ["Wide reply" gnus-summary-wide-reply t]
1981        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1982         ,@(if (featurep 'xemacs) '(t)
1983             '(:help "Mail a reply, quoting this article"))]
1984        ["Mail forward" gnus-summary-mail-forward t]
1985        ["Post forward" gnus-summary-post-forward t]
1986        ["Digest and mail" gnus-summary-digest-mail-forward t]
1987        ["Digest and post" gnus-summary-digest-post-forward t]
1988        ["Resend message" gnus-summary-resend-message t]
1989        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1990        ["Send a mail" gnus-summary-mail-other-window t]
1991        ["Uuencode and post" gnus-uu-post-news
1992         ,@(if (featurep 'xemacs) '(t)
1993             '(:help "Post a uuencoded article"))]
1994        ["Followup via news" gnus-summary-followup-to-mail t]
1995        ["Followup via news and yank"
1996         gnus-summary-followup-to-mail-with-original t]
1997        ;;("Draft"
1998        ;;["Send" gnus-summary-send-draft t]
1999        ;;["Send bounced" gnus-resend-bounced-mail t])
2000        ))
2001
2002     (cond 
2003      ((not (keymapp gnus-summary-post-menu))
2004       (setq gnus-article-post-menu gnus-summary-post-menu))
2005      ((not gnus-article-post-menu)
2006       ;; Don't share post menu.
2007       (setq gnus-article-post-menu
2008             (copy-keymap gnus-summary-post-menu))))
2009     (define-key gnus-article-mode-map [menu-bar post]
2010       (cons "Post" gnus-article-post-menu))
2011
2012     (easy-menu-define
2013      gnus-summary-misc-menu gnus-summary-mode-map ""
2014      `("Misc"
2015        ("Mark Read"
2016         ["Mark as read" gnus-summary-mark-as-read-forward t]
2017         ["Mark same subject and select"
2018          gnus-summary-kill-same-subject-and-select t]
2019         ["Mark same subject" gnus-summary-kill-same-subject t]
2020         ["Catchup" gnus-summary-catchup
2021          ,@(if (featurep 'xemacs) '(t)
2022              '(:help "Mark unread articles in this group as read"))]
2023         ["Catchup all" gnus-summary-catchup-all t]
2024         ["Catchup to here" gnus-summary-catchup-to-here t]
2025         ["Catchup from here" gnus-summary-catchup-from-here t]
2026         ["Catchup region" gnus-summary-mark-region-as-read t]
2027         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2028        ("Mark Various"
2029         ["Tick" gnus-summary-tick-article-forward t]
2030         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2031         ["Remove marks" gnus-summary-clear-mark-forward t]
2032         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2033         ["Set bookmark" gnus-summary-set-bookmark t]
2034         ["Remove bookmark" gnus-summary-remove-bookmark t])
2035        ("Mark Limit"
2036         ["Marks..." gnus-summary-limit-to-marks t]
2037         ["Subject..." gnus-summary-limit-to-subject t]
2038         ["Author..." gnus-summary-limit-to-author t]
2039         ["Age..." gnus-summary-limit-to-age t]
2040         ["Extra..." gnus-summary-limit-to-extra t]
2041         ["Score" gnus-summary-limit-to-score t]
2042         ["Unread" gnus-summary-limit-to-unread t]
2043         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2044         ["Articles" gnus-summary-limit-to-articles t]
2045         ["Pop limit" gnus-summary-pop-limit t]
2046         ["Show dormant" gnus-summary-limit-include-dormant t]
2047         ["Hide childless dormant"
2048          gnus-summary-limit-exclude-childless-dormant t]
2049         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2050         ["Hide marked" gnus-summary-limit-exclude-marks t]
2051         ["Show expunged" gnus-summary-limit-include-expunged t])
2052        ("Process Mark"
2053         ["Set mark" gnus-summary-mark-as-processable t]
2054         ["Remove mark" gnus-summary-unmark-as-processable t]
2055         ["Remove all marks" gnus-summary-unmark-all-processable t]
2056         ["Mark above" gnus-uu-mark-over t]
2057         ["Mark series" gnus-uu-mark-series t]
2058         ["Mark region" gnus-uu-mark-region t]
2059         ["Unmark region" gnus-uu-unmark-region t]
2060         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2061         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2062         ["Mark all" gnus-uu-mark-all t]
2063         ["Mark buffer" gnus-uu-mark-buffer t]
2064         ["Mark sparse" gnus-uu-mark-sparse t]
2065         ["Mark thread" gnus-uu-mark-thread t]
2066         ["Unmark thread" gnus-uu-unmark-thread t]
2067         ("Process Mark Sets"
2068          ["Kill" gnus-summary-kill-process-mark t]
2069          ["Yank" gnus-summary-yank-process-mark
2070           gnus-newsgroup-process-stack]
2071          ["Save" gnus-summary-save-process-mark t]))
2072        ("Scroll article"
2073         ["Page forward" gnus-summary-next-page
2074          ,@(if (featurep 'xemacs) '(t)
2075              '(:help "Show next page of article"))]
2076         ["Page backward" gnus-summary-prev-page
2077          ,@(if (featurep 'xemacs) '(t)
2078              '(:help "Show previous page of article"))]
2079         ["Line forward" gnus-summary-scroll-up t])
2080        ("Move"
2081         ["Next unread article" gnus-summary-next-unread-article t]
2082         ["Previous unread article" gnus-summary-prev-unread-article t]
2083         ["Next article" gnus-summary-next-article t]
2084         ["Previous article" gnus-summary-prev-article t]
2085         ["Next unread subject" gnus-summary-next-unread-subject t]
2086         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2087         ["Next article same subject" gnus-summary-next-same-subject t]
2088         ["Previous article same subject" gnus-summary-prev-same-subject t]
2089         ["First unread article" gnus-summary-first-unread-article t]
2090         ["Best unread article" gnus-summary-best-unread-article t]
2091         ["Go to subject number..." gnus-summary-goto-subject t]
2092         ["Go to article number..." gnus-summary-goto-article t]
2093         ["Go to the last article" gnus-summary-goto-last-article t]
2094         ["Pop article off history" gnus-summary-pop-article t])
2095        ("Sort"
2096         ["Sort by number" gnus-summary-sort-by-number t]
2097         ["Sort by author" gnus-summary-sort-by-author t]
2098         ["Sort by subject" gnus-summary-sort-by-subject t]
2099         ["Sort by date" gnus-summary-sort-by-date t]
2100         ["Sort by score" gnus-summary-sort-by-score t]
2101         ["Sort by lines" gnus-summary-sort-by-lines t]
2102         ["Sort by characters" gnus-summary-sort-by-chars t]
2103         ["Original sort" gnus-summary-sort-by-original t])
2104        ("Help"
2105         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2106         ["Describe group" gnus-summary-describe-group t]
2107         ["Read manual" gnus-info-find-node t])
2108        ("Modes"
2109         ["Pick and read" gnus-pick-mode t]
2110         ["Binary" gnus-binary-mode t])
2111        ("Regeneration"
2112         ["Regenerate" gnus-summary-prepare t]
2113         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2114         ["Toggle threading" gnus-summary-toggle-threads t])
2115        ["See old articles" gnus-summary-insert-old-articles t]
2116        ["See new articles" gnus-summary-insert-new-articles t]
2117        ["Filter articles..." gnus-summary-execute-command t]
2118        ["Run command on subjects..." gnus-summary-universal-argument t]
2119        ["Search articles forward..." gnus-summary-search-article-forward t]
2120        ["Search articles backward..." gnus-summary-search-article-backward t]
2121        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2122        ["Expand window" gnus-summary-expand-window t]
2123        ["Expire expirable articles" gnus-summary-expire-articles
2124         (gnus-check-backend-function
2125          'request-expire-articles gnus-newsgroup-name)]
2126        ["Edit local kill file" gnus-summary-edit-local-kill t]
2127        ["Edit main kill file" gnus-summary-edit-global-kill t]
2128        ["Edit group parameters" gnus-summary-edit-parameters t]
2129        ["Customize group parameters" gnus-summary-customize-parameters t]
2130        ["Send a bug report" gnus-bug t]
2131        ("Exit"
2132         ["Catchup and exit" gnus-summary-catchup-and-exit
2133          ,@(if (featurep 'xemacs) '(t)
2134              '(:help "Mark unread articles in this group as read, then exit"))]
2135         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2136         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2137         ["Exit group" gnus-summary-exit
2138          ,@(if (featurep 'xemacs) '(t)
2139              '(:help "Exit current group, return to group selection mode"))]
2140         ["Exit group without updating" gnus-summary-exit-no-update t]
2141         ["Exit and goto next group" gnus-summary-next-group t]
2142         ["Exit and goto prev group" gnus-summary-prev-group t]
2143         ["Reselect group" gnus-summary-reselect-current-group t]
2144         ["Rescan group" gnus-summary-rescan-group t]
2145         ["Update dribble" gnus-summary-save-newsrc t])))
2146
2147     (gnus-run-hooks 'gnus-summary-menu-hook)))
2148
2149 (defvar gnus-summary-tool-bar-map nil)
2150
2151 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2152 (defun gnus-summary-make-tool-bar ()
2153   (if (and (fboundp 'tool-bar-add-item-from-menu)
2154            (default-value 'tool-bar-mode)
2155            (not gnus-summary-tool-bar-map))
2156       (setq gnus-summary-tool-bar-map
2157             (let ((tool-bar-map (make-sparse-keymap))
2158                   (load-path (mm-image-load-path)))
2159               (tool-bar-add-item-from-menu
2160                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2161               (tool-bar-add-item-from-menu
2162                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2163               (tool-bar-add-item-from-menu
2164                'gnus-summary-post-news "post" gnus-summary-mode-map)
2165               (tool-bar-add-item-from-menu
2166                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2167               (tool-bar-add-item-from-menu
2168                'gnus-summary-followup "followup" gnus-summary-mode-map)
2169               (tool-bar-add-item-from-menu
2170                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2171               (tool-bar-add-item-from-menu
2172                'gnus-summary-reply "reply" gnus-summary-mode-map)
2173               (tool-bar-add-item-from-menu
2174                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2175               (tool-bar-add-item-from-menu
2176                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2177               (tool-bar-add-item-from-menu
2178                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2179               (tool-bar-add-item-from-menu
2180                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2181               (tool-bar-add-item-from-menu
2182                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2183               (tool-bar-add-item-from-menu
2184                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2185               (tool-bar-add-item-from-menu
2186                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2187               (tool-bar-add-item-from-menu
2188                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2189               tool-bar-map)))
2190   (if gnus-summary-tool-bar-map
2191       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2192
2193 (defun gnus-score-set-default (var value)
2194   "A version of set that updates the GNU Emacs menu-bar."
2195   (set var value)
2196   ;; It is the message that forces the active status to be updated.
2197   (message ""))
2198
2199 (defun gnus-make-score-map (type)
2200   "Make a summary score map of type TYPE."
2201   (if t
2202       nil
2203     (let ((headers '(("author" "from" string)
2204                      ("subject" "subject" string)
2205                      ("article body" "body" string)
2206                      ("article head" "head" string)
2207                      ("xref" "xref" string)
2208                      ("extra header" "extra" string)
2209                      ("lines" "lines" number)
2210                      ("followups to author" "followup" string)))
2211           (types '((number ("less than" <)
2212                            ("greater than" >)
2213                            ("equal" =))
2214                    (string ("substring" s)
2215                            ("exact string" e)
2216                            ("fuzzy string" f)
2217                            ("regexp" r))))
2218           (perms '(("temporary" (current-time-string))
2219                    ("permanent" nil)
2220                    ("immediate" now)))
2221           header)
2222       (list
2223        (apply
2224         'nconc
2225         (list
2226          (if (eq type 'lower)
2227              "Lower score"
2228            "Increase score"))
2229         (let (outh)
2230           (while headers
2231             (setq header (car headers))
2232             (setq outh
2233                   (cons
2234                    (apply
2235                     'nconc
2236                     (list (car header))
2237                     (let ((ts (cdr (assoc (nth 2 header) types)))
2238                           outt)
2239                       (while ts
2240                         (setq outt
2241                               (cons
2242                                (apply
2243                                 'nconc
2244                                 (list (caar ts))
2245                                 (let ((ps perms)
2246                                       outp)
2247                                   (while ps
2248                                     (setq outp
2249                                           (cons
2250                                            (vector
2251                                             (caar ps)
2252                                             (list
2253                                              'gnus-summary-score-entry
2254                                              (nth 1 header)
2255                                              (if (or (string= (nth 1 header)
2256                                                               "head")
2257                                                      (string= (nth 1 header)
2258                                                               "body"))
2259                                                  ""
2260                                                (list 'gnus-summary-header
2261                                                      (nth 1 header)))
2262                                              (list 'quote (nth 1 (car ts)))
2263                                              (list 'gnus-score-delta-default
2264                                                    nil)
2265                                              (nth 1 (car ps))
2266                                              t)
2267                                             t)
2268                                            outp))
2269                                     (setq ps (cdr ps)))
2270                                   (list (nreverse outp))))
2271                                outt))
2272                         (setq ts (cdr ts)))
2273                       (list (nreverse outt))))
2274                    outh))
2275             (setq headers (cdr headers)))
2276           (list (nreverse outh))))))))
2277
2278 \f
2279
2280 (defun gnus-summary-mode (&optional group)
2281   "Major mode for reading articles.
2282
2283 All normal editing commands are switched off.
2284 \\<gnus-summary-mode-map>
2285 Each line in this buffer represents one article.  To read an
2286 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2287 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2288 respectively.
2289
2290 You can also post articles and send mail from this buffer.  To
2291 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2292 of an article, type `\\[gnus-summary-reply]'.
2293
2294 There are approx. one gazillion commands you can execute in this
2295 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2296
2297 The following commands are available:
2298
2299 \\{gnus-summary-mode-map}"
2300   (interactive)
2301   (kill-all-local-variables)
2302   (when (gnus-visual-p 'summary-menu 'menu)
2303     (gnus-summary-make-menu-bar)
2304     (gnus-summary-make-tool-bar))
2305   (gnus-summary-make-local-variables)
2306   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2307     (gnus-summary-make-local-variables))
2308   (gnus-make-thread-indent-array)
2309   (gnus-simplify-mode-line)
2310   (setq major-mode 'gnus-summary-mode)
2311   (setq mode-name "Summary")
2312   (make-local-variable 'minor-mode-alist)
2313   (use-local-map gnus-summary-mode-map)
2314   (buffer-disable-undo)
2315   (setq buffer-read-only t)             ;Disable modification
2316   (setq truncate-lines t)
2317   (setq selective-display t)
2318   (setq selective-display-ellipses t)   ;Display `...'
2319   (gnus-summary-set-display-table)
2320   (gnus-set-default-directory)
2321   (setq gnus-newsgroup-name group)
2322   (unless (gnus-news-group-p group)
2323     (setq gnus-newsgroup-incorporated
2324           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2325   (make-local-variable 'gnus-summary-line-format)
2326   (make-local-variable 'gnus-summary-line-format-spec)
2327   (make-local-variable 'gnus-summary-dummy-line-format)
2328   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2329   (make-local-variable 'gnus-summary-mark-positions)
2330   (make-local-hook 'pre-command-hook)
2331   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2332   (gnus-run-hooks 'gnus-summary-mode-hook)
2333   (turn-on-gnus-mailing-list-mode)
2334   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2335   (gnus-update-summary-mark-positions))
2336
2337 (defun gnus-summary-make-local-variables ()
2338   "Make all the local summary buffer variables."
2339   (let (global)
2340     (dolist (local gnus-summary-local-variables)
2341       (if (consp local)
2342           (progn
2343             (if (eq (cdr local) 'global)
2344                 ;; Copy the global value of the variable.
2345                 (setq global (symbol-value (car local)))
2346               ;; Use the value from the list.
2347               (setq global (eval (cdr local))))
2348             (set (make-local-variable (car local)) global))
2349         ;; Simple nil-valued local variable.
2350         (set (make-local-variable local) nil)))))
2351
2352 (defun gnus-summary-clear-local-variables ()
2353   (let ((locals gnus-summary-local-variables))
2354     (while locals
2355       (if (consp (car locals))
2356           (and (vectorp (caar locals))
2357                (set (caar locals) nil))
2358         (and (vectorp (car locals))
2359              (set (car locals) nil)))
2360       (setq locals (cdr locals)))))
2361
2362 ;; Summary data functions.
2363
2364 (defmacro gnus-data-number (data)
2365   `(car ,data))
2366
2367 (defmacro gnus-data-set-number (data number)
2368   `(setcar ,data ,number))
2369
2370 (defmacro gnus-data-mark (data)
2371   `(nth 1 ,data))
2372
2373 (defmacro gnus-data-set-mark (data mark)
2374   `(setcar (nthcdr 1 ,data) ,mark))
2375
2376 (defmacro gnus-data-pos (data)
2377   `(nth 2 ,data))
2378
2379 (defmacro gnus-data-set-pos (data pos)
2380   `(setcar (nthcdr 2 ,data) ,pos))
2381
2382 (defmacro gnus-data-header (data)
2383   `(nth 3 ,data))
2384
2385 (defmacro gnus-data-set-header (data header)
2386   `(setcar (nthcdr 3 ,data) ,header))
2387
2388 (defmacro gnus-data-level (data)
2389   `(nth 4 ,data))
2390
2391 (defmacro gnus-data-unread-p (data)
2392   `(= (nth 1 ,data) gnus-unread-mark))
2393
2394 (defmacro gnus-data-read-p (data)
2395   `(/= (nth 1 ,data) gnus-unread-mark))
2396
2397 (defmacro gnus-data-pseudo-p (data)
2398   `(consp (nth 3 ,data)))
2399
2400 (defmacro gnus-data-find (number)
2401   `(assq ,number gnus-newsgroup-data))
2402
2403 (defmacro gnus-data-find-list (number &optional data)
2404   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2405      (memq (assq ,number bdata)
2406            bdata)))
2407
2408 (defmacro gnus-data-make (number mark pos header level)
2409   `(list ,number ,mark ,pos ,header ,level))
2410
2411 (defun gnus-data-enter (after-article number mark pos header level offset)
2412   (let ((data (gnus-data-find-list after-article)))
2413     (unless data
2414       (error "No such article: %d" after-article))
2415     (setcdr data (cons (gnus-data-make number mark pos header level)
2416                        (cdr data)))
2417     (setq gnus-newsgroup-data-reverse nil)
2418     (gnus-data-update-list (cddr data) offset)))
2419
2420 (defun gnus-data-enter-list (after-article list &optional offset)
2421   (when list
2422     (let ((data (and after-article (gnus-data-find-list after-article)))
2423           (ilist list))
2424       (if (not (or data
2425                    after-article))
2426           (let ((odata gnus-newsgroup-data))
2427             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2428             (when offset
2429               (gnus-data-update-list odata offset)))
2430         ;; Find the last element in the list to be spliced into the main
2431         ;; list.
2432         (while (cdr list)
2433           (setq list (cdr list)))
2434         (if (not data)
2435             (progn
2436               (setcdr list gnus-newsgroup-data)
2437               (setq gnus-newsgroup-data ilist)
2438               (when offset
2439                 (gnus-data-update-list (cdr list) offset)))
2440           (setcdr list (cdr data))
2441           (setcdr data ilist)
2442           (when offset
2443             (gnus-data-update-list (cdr list) offset))))
2444       (setq gnus-newsgroup-data-reverse nil))))
2445
2446 (defun gnus-data-remove (article &optional offset)
2447   (let ((data gnus-newsgroup-data))
2448     (if (= (gnus-data-number (car data)) article)
2449         (progn
2450           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2451                 gnus-newsgroup-data-reverse nil)
2452           (when offset
2453             (gnus-data-update-list gnus-newsgroup-data offset)))
2454       (while (cdr data)
2455         (when (= (gnus-data-number (cadr data)) article)
2456           (setcdr data (cddr data))
2457           (when offset
2458             (gnus-data-update-list (cdr data) offset))
2459           (setq data nil
2460                 gnus-newsgroup-data-reverse nil))
2461         (setq data (cdr data))))))
2462
2463 (defmacro gnus-data-list (backward)
2464   `(if ,backward
2465        (or gnus-newsgroup-data-reverse
2466            (setq gnus-newsgroup-data-reverse
2467                  (reverse gnus-newsgroup-data)))
2468      gnus-newsgroup-data))
2469
2470 (defun gnus-data-update-list (data offset)
2471   "Add OFFSET to the POS of all data entries in DATA."
2472   (setq gnus-newsgroup-data-reverse nil)
2473   (while data
2474     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2475     (setq data (cdr data))))
2476
2477 (defun gnus-summary-article-pseudo-p (article)
2478   "Say whether this article is a pseudo article or not."
2479   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2480
2481 (defmacro gnus-summary-article-sparse-p (article)
2482   "Say whether this article is a sparse article or not."
2483   `(memq ,article gnus-newsgroup-sparse))
2484
2485 (defmacro gnus-summary-article-ancient-p (article)
2486   "Say whether this article is a sparse article or not."
2487   `(memq ,article gnus-newsgroup-ancient))
2488
2489 (defun gnus-article-parent-p (number)
2490   "Say whether this article is a parent or not."
2491   (let ((data (gnus-data-find-list number)))
2492     (and (cdr data)                     ; There has to be an article after...
2493          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2494             (gnus-data-level (nth 1 data))))))
2495
2496 (defun gnus-article-children (number)
2497   "Return a list of all children to NUMBER."
2498   (let* ((data (gnus-data-find-list number))
2499          (level (gnus-data-level (car data)))
2500          children)
2501     (setq data (cdr data))
2502     (while (and data
2503                 (= (gnus-data-level (car data)) (1+ level)))
2504       (push (gnus-data-number (car data)) children)
2505       (setq data (cdr data)))
2506     children))
2507
2508 (defmacro gnus-summary-skip-intangible ()
2509   "If the current article is intangible, then jump to a different article."
2510   '(let ((to (get-text-property (point) 'gnus-intangible)))
2511      (and to (gnus-summary-goto-subject to))))
2512
2513 (defmacro gnus-summary-article-intangible-p ()
2514   "Say whether this article is intangible or not."
2515   '(get-text-property (point) 'gnus-intangible))
2516
2517 (defun gnus-article-read-p (article)
2518   "Say whether ARTICLE is read or not."
2519   (not (or (memq article gnus-newsgroup-marked)
2520            (memq article gnus-newsgroup-unreads)
2521            (memq article gnus-newsgroup-unselected)
2522            (memq article gnus-newsgroup-dormant))))
2523
2524 ;; Some summary mode macros.
2525
2526 (defmacro gnus-summary-article-number ()
2527   "The article number of the article on the current line.
2528 If there isn's an article number here, then we return the current
2529 article number."
2530   '(progn
2531      (gnus-summary-skip-intangible)
2532      (or (get-text-property (point) 'gnus-number)
2533          (gnus-summary-last-subject))))
2534
2535 (defmacro gnus-summary-article-header (&optional number)
2536   "Return the header of article NUMBER."
2537   `(gnus-data-header (gnus-data-find
2538                       ,(or number '(gnus-summary-article-number)))))
2539
2540 (defmacro gnus-summary-thread-level (&optional number)
2541   "Return the level of thread that starts with article NUMBER."
2542   `(if (and (eq gnus-summary-make-false-root 'dummy)
2543             (get-text-property (point) 'gnus-intangible))
2544        0
2545      (gnus-data-level (gnus-data-find
2546                        ,(or number '(gnus-summary-article-number))))))
2547
2548 (defmacro gnus-summary-article-mark (&optional number)
2549   "Return the mark of article NUMBER."
2550   `(gnus-data-mark (gnus-data-find
2551                     ,(or number '(gnus-summary-article-number)))))
2552
2553 (defmacro gnus-summary-article-pos (&optional number)
2554   "Return the position of the line of article NUMBER."
2555   `(gnus-data-pos (gnus-data-find
2556                    ,(or number '(gnus-summary-article-number)))))
2557
2558 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2559 (defmacro gnus-summary-article-subject (&optional number)
2560   "Return current subject string or nil if nothing."
2561   `(let ((headers
2562           ,(if number
2563                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2564              '(gnus-data-header (assq (gnus-summary-article-number)
2565                                       gnus-newsgroup-data)))))
2566      (and headers
2567           (vectorp headers)
2568           (mail-header-subject headers))))
2569
2570 (defmacro gnus-summary-article-score (&optional number)
2571   "Return current article score."
2572   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2573                   gnus-newsgroup-scored))
2574        gnus-summary-default-score 0))
2575
2576 (defun gnus-summary-article-children (&optional number)
2577   "Return a list of article numbers that are children of article NUMBER."
2578   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2579          (level (gnus-data-level (car data)))
2580          l children)
2581     (while (and (setq data (cdr data))
2582                 (> (setq l (gnus-data-level (car data))) level))
2583       (and (= (1+ level) l)
2584            (push (gnus-data-number (car data))
2585                  children)))
2586     (nreverse children)))
2587
2588 (defun gnus-summary-article-parent (&optional number)
2589   "Return the article number of the parent of article NUMBER."
2590   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2591                                     (gnus-data-list t)))
2592          (level (gnus-data-level (car data))))
2593     (if (zerop level)
2594         ()                              ; This is a root.
2595       ;; We search until we find an article with a level less than
2596       ;; this one.  That function has to be the parent.
2597       (while (and (setq data (cdr data))
2598                   (not (< (gnus-data-level (car data)) level))))
2599       (and data (gnus-data-number (car data))))))
2600
2601 (defun gnus-unread-mark-p (mark)
2602   "Say whether MARK is the unread mark."
2603   (= mark gnus-unread-mark))
2604
2605 (defun gnus-read-mark-p (mark)
2606   "Say whether MARK is one of the marks that mark as read.
2607 This is all marks except unread, ticked, dormant, and expirable."
2608   (not (or (= mark gnus-unread-mark)
2609            (= mark gnus-ticked-mark)
2610            (= mark gnus-dormant-mark)
2611            (= mark gnus-expirable-mark))))
2612
2613 (defmacro gnus-article-mark (number)
2614   "Return the MARK of article NUMBER.
2615 This macro should only be used when computing the mark the \"first\"
2616 time; i.e., when generating the summary lines.  After that,
2617 `gnus-summary-article-mark' should be used to examine the
2618 marks of articles."
2619   `(cond
2620     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2621     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2622     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2623     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2624     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2625     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2626     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2627     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2628            gnus-ancient-mark))))
2629
2630 ;; Saving hidden threads.
2631
2632 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2633 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2634
2635 (defmacro gnus-save-hidden-threads (&rest forms)
2636   "Save hidden threads, eval FORMS, and restore the hidden threads."
2637   (let ((config (make-symbol "config")))
2638     `(let ((,config (gnus-hidden-threads-configuration)))
2639        (unwind-protect
2640            (save-excursion
2641              ,@forms)
2642          (gnus-restore-hidden-threads-configuration ,config)))))
2643
2644 (defun gnus-data-compute-positions ()
2645   "Compute the positions of all articles."
2646   (setq gnus-newsgroup-data-reverse nil)
2647   (let ((data gnus-newsgroup-data))
2648     (save-excursion
2649       (gnus-save-hidden-threads
2650         (gnus-summary-show-all-threads)
2651         (goto-char (point-min))
2652         (while data
2653           (while (get-text-property (point) 'gnus-intangible)
2654             (forward-line 1))
2655           (gnus-data-set-pos (car data) (+ (point) 3))
2656           (setq data (cdr data))
2657           (forward-line 1))))))
2658
2659 (defun gnus-hidden-threads-configuration ()
2660   "Return the current hidden threads configuration."
2661   (save-excursion
2662     (let (config)
2663       (goto-char (point-min))
2664       (while (search-forward "\r" nil t)
2665         (push (1- (point)) config))
2666       config)))
2667
2668 (defun gnus-restore-hidden-threads-configuration (config)
2669   "Restore hidden threads configuration from CONFIG."
2670   (save-excursion
2671     (let (point buffer-read-only)
2672       (while (setq point (pop config))
2673         (when (and (< point (point-max))
2674                    (goto-char point)
2675                    (eq (char-after) ?\n))
2676           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2677
2678 ;; Various summary mode internalish functions.
2679
2680 (defun gnus-mouse-pick-article (e)
2681   (interactive "e")
2682   (mouse-set-point e)
2683   (gnus-summary-next-page nil t))
2684
2685 (defun gnus-summary-set-display-table ()
2686   "Change the display table.
2687 Odd characters have a tendency to mess
2688 up nicely formatted displays - we make all possible glyphs
2689 display only a single character."
2690
2691   ;; We start from the standard display table, if any.
2692   (let ((table (or (copy-sequence standard-display-table)
2693                    (make-display-table)))
2694         (i 32))
2695     ;; Nix out all the control chars...
2696     (while (>= (setq i (1- i)) 0)
2697       (aset table i [??]))
2698     ;; ... but not newline and cr, of course.  (cr is necessary for the
2699     ;; selective display).
2700     (aset table ?\n nil)
2701     (aset table ?\r nil)
2702     ;; We keep TAB as well.
2703     (aset table ?\t nil)
2704     ;; We nix out any glyphs over 126 that are not set already.
2705     (let ((i 256))
2706       (while (>= (setq i (1- i)) 127)
2707         ;; Only modify if the entry is nil.
2708         (unless (aref table i)
2709           (aset table i [??]))))
2710     (setq buffer-display-table table)))
2711
2712 (defun gnus-summary-buffer-name (group)
2713   "Return the summary buffer name of GROUP."
2714   (concat "*Summary " group "*"))
2715
2716 (defun gnus-summary-setup-buffer (group)
2717   "Initialize summary buffer."
2718   (let ((buffer (gnus-summary-buffer-name group)))
2719     (if (get-buffer buffer)
2720         (progn
2721           (set-buffer buffer)
2722           (setq gnus-summary-buffer (current-buffer))
2723           (not gnus-newsgroup-prepared))
2724       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2725       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2726       (gnus-summary-mode group)
2727       (when gnus-carpal
2728         (gnus-carpal-setup-buffer 'summary))
2729       (unless gnus-single-article-buffer
2730         (make-local-variable 'gnus-article-buffer)
2731         (make-local-variable 'gnus-article-current)
2732         (make-local-variable 'gnus-original-article-buffer))
2733       (setq gnus-newsgroup-name group)
2734       ;; Set any local variables in the group parameters.
2735       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2736       t)))
2737
2738 (defun gnus-set-global-variables ()
2739   "Set the global equivalents of the buffer-local variables.
2740 They are set to the latest values they had.  These reflect the summary
2741 buffer that was in action when the last article was fetched."
2742   (when (eq major-mode 'gnus-summary-mode)
2743     (setq gnus-summary-buffer (current-buffer))
2744     (let ((name gnus-newsgroup-name)
2745           (marked gnus-newsgroup-marked)
2746           (unread gnus-newsgroup-unreads)
2747           (headers gnus-current-headers)
2748           (data gnus-newsgroup-data)
2749           (summary gnus-summary-buffer)
2750           (article-buffer gnus-article-buffer)
2751           (original gnus-original-article-buffer)
2752           (gac gnus-article-current)
2753           (reffed gnus-reffed-article-number)
2754           (score-file gnus-current-score-file)
2755           (default-charset gnus-newsgroup-charset)
2756           vlist)
2757       (let ((locals gnus-newsgroup-variables))
2758         (while locals
2759           (if (consp (car locals))
2760               (push (eval (caar locals)) vlist)
2761             (push (eval (car locals)) vlist))
2762           (setq locals (cdr locals)))
2763         (setq vlist (nreverse vlist)))
2764       (save-excursion
2765         (set-buffer gnus-group-buffer)
2766         (setq gnus-newsgroup-name name
2767               gnus-newsgroup-marked marked
2768               gnus-newsgroup-unreads unread
2769               gnus-current-headers headers
2770               gnus-newsgroup-data data
2771               gnus-article-current gac
2772               gnus-summary-buffer summary
2773               gnus-article-buffer article-buffer
2774               gnus-original-article-buffer original
2775               gnus-reffed-article-number reffed
2776               gnus-current-score-file score-file
2777               gnus-newsgroup-charset default-charset)
2778         (let ((locals gnus-newsgroup-variables))
2779           (while locals
2780             (if (consp (car locals))
2781                 (set (caar locals) (pop vlist))
2782               (set (car locals) (pop vlist)))
2783             (setq locals (cdr locals))))
2784         ;; The article buffer also has local variables.
2785         (when (gnus-buffer-live-p gnus-article-buffer)
2786           (set-buffer gnus-article-buffer)
2787           (setq gnus-summary-buffer summary))))))
2788
2789 (defun gnus-summary-article-unread-p (article)
2790   "Say whether ARTICLE is unread or not."
2791   (memq article gnus-newsgroup-unreads))
2792
2793 (defun gnus-summary-first-article-p (&optional article)
2794   "Return whether ARTICLE is the first article in the buffer."
2795   (if (not (setq article (or article (gnus-summary-article-number))))
2796       nil
2797     (eq article (caar gnus-newsgroup-data))))
2798
2799 (defun gnus-summary-last-article-p (&optional article)
2800   "Return whether ARTICLE is the last article in the buffer."
2801   (if (not (setq article (or article (gnus-summary-article-number))))
2802       ;; All non-existent numbers are the last article.  :-)
2803       t
2804     (not (cdr (gnus-data-find-list article)))))
2805
2806 (defun gnus-make-thread-indent-array ()
2807   (let ((n 200))
2808     (unless (and gnus-thread-indent-array
2809                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2810       (setq gnus-thread-indent-array (make-vector 201 "")
2811             gnus-thread-indent-array-level gnus-thread-indent-level)
2812       (while (>= n 0)
2813         (aset gnus-thread-indent-array n
2814               (make-string (* n gnus-thread-indent-level) ? ))
2815         (setq n (1- n))))))
2816
2817 (defun gnus-update-summary-mark-positions ()
2818   "Compute where the summary marks are to go."
2819   (save-excursion
2820     (when (gnus-buffer-exists-p gnus-summary-buffer)
2821       (set-buffer gnus-summary-buffer))
2822     (let ((gnus-replied-mark 129)
2823           (gnus-score-below-mark 130)
2824           (gnus-score-over-mark 130)
2825           (gnus-download-mark 131)
2826           (spec gnus-summary-line-format-spec)
2827           gnus-visual pos)
2828       (save-excursion
2829         (gnus-set-work-buffer)
2830         (let ((gnus-summary-line-format-spec spec)
2831               (gnus-newsgroup-downloadable '((0 . t))))
2832           (gnus-summary-insert-line
2833            (make-full-mail-header 0 "" "nobody"
2834                                   "05 Apr 2001 23:33:09 +0400"
2835                                   "" "" 0 0 "" nil)
2836            0 nil 128 t nil "" nil 1)
2837           (goto-char (point-min))
2838           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2839                                              (- (point) 2)))))
2840           (goto-char (point-min))
2841           (push (cons 'replied (and (search-forward "\201" nil t)
2842                                     (- (point) 2)))
2843                 pos)
2844           (goto-char (point-min))
2845           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2846                 pos)
2847           (goto-char (point-min))
2848           (push (cons 'download
2849                       (and (search-forward "\203" nil t) (- (point) 2)))
2850                 pos)))
2851       (setq gnus-summary-mark-positions pos))))
2852
2853 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2854   "Insert a dummy root in the summary buffer."
2855   (beginning-of-line)
2856   (gnus-add-text-properties
2857    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2858    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2859
2860 (defun gnus-summary-from-or-to-or-newsgroups (header)
2861   (let ((to (cdr (assq 'To (mail-header-extra header))))
2862         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2863         (default-mime-charset (with-current-buffer gnus-summary-buffer
2864                                 default-mime-charset)))
2865     (cond
2866      ((and to
2867            gnus-ignored-from-addresses
2868            (string-match gnus-ignored-from-addresses
2869                          (mail-header-from header)))
2870       (concat "-> "
2871               (or (car (funcall gnus-extract-address-components
2872                                 (funcall
2873                                  gnus-decode-encoded-word-function to)))
2874                   (funcall gnus-decode-encoded-word-function to))))
2875      ((and newsgroups
2876            gnus-ignored-from-addresses
2877            (string-match gnus-ignored-from-addresses
2878                          (mail-header-from header)))
2879       (concat "=> " newsgroups))
2880      (t
2881       (or (car (funcall gnus-extract-address-components
2882                         (mail-header-from header)))
2883           (mail-header-from header))))))
2884
2885 (defun gnus-summary-insert-line (gnus-tmp-header
2886                                  gnus-tmp-level gnus-tmp-current
2887                                  gnus-tmp-unread gnus-tmp-replied
2888                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2889                                  &optional gnus-tmp-dummy gnus-tmp-score
2890                                  gnus-tmp-process)
2891   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2892          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2893          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2894          (gnus-tmp-score-char
2895           (if (or (null gnus-summary-default-score)
2896                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2897                       gnus-summary-zcore-fuzz))
2898               ?  ;Whitespace
2899             (if (< gnus-tmp-score gnus-summary-default-score)
2900                 gnus-score-below-mark gnus-score-over-mark)))
2901          (gnus-tmp-replied
2902           (cond (gnus-tmp-process gnus-process-mark)
2903                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2904                  gnus-cached-mark)
2905                 (gnus-tmp-replied gnus-replied-mark)
2906                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2907                  gnus-saved-mark)
2908                 (t gnus-no-mark)))
2909          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2910          (gnus-tmp-name
2911           (cond
2912            ((string-match "<[^>]+> *$" gnus-tmp-from)
2913             (let ((beg (match-beginning 0)))
2914               (or (and (string-match "^\".+\"" gnus-tmp-from)
2915                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2916                   (substring gnus-tmp-from 0 beg))))
2917            ((string-match "(.+)" gnus-tmp-from)
2918             (substring gnus-tmp-from
2919                        (1+ (match-beginning 0)) (1- (match-end 0))))
2920            (t gnus-tmp-from)))
2921          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2922          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2923          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2924          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2925          (buffer-read-only nil))
2926     (when (string= gnus-tmp-name "")
2927       (setq gnus-tmp-name gnus-tmp-from))
2928     (unless (numberp gnus-tmp-lines)
2929       (setq gnus-tmp-lines -1))
2930     (when (= gnus-tmp-lines -1)
2931       (setq gnus-tmp-lines "?"))
2932     (gnus-put-text-property-excluding-characters-with-faces
2933      (point)
2934      (progn (eval gnus-summary-line-format-spec) (point))
2935      'gnus-number gnus-tmp-number)
2936     (when (gnus-visual-p 'summary-highlight 'highlight)
2937       (forward-line -1)
2938       (gnus-run-hooks 'gnus-summary-update-hook)
2939       (forward-line 1))))
2940
2941 (defun gnus-summary-update-line (&optional dont-update)
2942   "Update summary line after change."
2943   (when (and gnus-summary-default-score
2944              (not gnus-summary-inhibit-highlight))
2945     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2946            (article (gnus-summary-article-number))
2947            (score (gnus-summary-article-score article)))
2948       (unless dont-update
2949         (if (and gnus-summary-mark-below
2950                  (< (gnus-summary-article-score)
2951                     gnus-summary-mark-below))
2952             ;; This article has a low score, so we mark it as read.
2953             (when (memq article gnus-newsgroup-unreads)
2954               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2955           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2956             ;; This article was previously marked as read on account
2957             ;; of a low score, but now it has risen, so we mark it as
2958             ;; unread.
2959             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2960         (gnus-summary-update-mark
2961          (if (or (null gnus-summary-default-score)
2962                  (<= (abs (- score gnus-summary-default-score))
2963                      gnus-summary-zcore-fuzz))
2964              ?  ;Whitespace
2965            (if (< score gnus-summary-default-score)
2966                gnus-score-below-mark gnus-score-over-mark))
2967          'score))
2968       ;; Do visual highlighting.
2969       (when (gnus-visual-p 'summary-highlight 'highlight)
2970         (gnus-run-hooks 'gnus-summary-update-hook)))))
2971
2972 (defvar gnus-tmp-new-adopts nil)
2973
2974 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2975   "Return the number of articles in THREAD.
2976 This may be 0 in some cases -- if none of the articles in
2977 the thread are to be displayed."
2978   (let* ((number
2979           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2980           (cond
2981            ((not (listp thread))
2982             1)
2983            ((and (consp thread) (cdr thread))
2984             (apply
2985              '+ 1 (mapcar
2986                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2987            ((null thread)
2988             1)
2989            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2990             1)
2991            (t 0))))
2992     (when (and level (zerop level) gnus-tmp-new-adopts)
2993       (incf number
2994             (apply '+ (mapcar
2995                        'gnus-summary-number-of-articles-in-thread
2996                        gnus-tmp-new-adopts))))
2997     (if char
2998         (if (> number 1) gnus-not-empty-thread-mark
2999           gnus-empty-thread-mark)
3000       number)))
3001
3002 (defun gnus-summary-set-local-parameters (group)
3003   "Go through the local params of GROUP and set all variable specs in that list."
3004   (let ((params (gnus-group-find-parameter group))
3005         elem)
3006     (while params
3007       (setq elem (car params)
3008             params (cdr params))
3009       (and (consp elem)                 ; Has to be a cons.
3010            (consp (cdr elem))           ; The cdr has to be a list.
3011            (symbolp (car elem))         ; Has to be a symbol in there.
3012            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
3013            (ignore-errors               ; So we set it.
3014              (make-local-variable (car elem))
3015              (set (car elem) (eval (nth 1 elem))))))))
3016
3017 (defun gnus-summary-read-group (group &optional show-all no-article
3018                                       kill-buffer no-display backward
3019                                       select-articles)
3020   "Start reading news in newsgroup GROUP.
3021 If SHOW-ALL is non-nil, already read articles are also listed.
3022 If NO-ARTICLE is non-nil, no article is selected initially.
3023 If NO-DISPLAY, don't generate a summary buffer."
3024   (let (result)
3025     (while (and group
3026                 (null (setq result
3027                             (let ((gnus-auto-select-next nil))
3028                               (or (gnus-summary-read-group-1
3029                                    group show-all no-article
3030                                    kill-buffer no-display
3031                                    select-articles)
3032                                   (setq show-all nil
3033                                         select-articles nil)))))
3034                 (eq gnus-auto-select-next 'quietly))
3035       (set-buffer gnus-group-buffer)
3036       ;; The entry function called above goes to the next
3037       ;; group automatically, so we go two groups back
3038       ;; if we are searching for the previous group.
3039       (when backward
3040         (gnus-group-prev-unread-group 2))
3041       (if (not (equal group (gnus-group-group-name)))
3042           (setq group (gnus-group-group-name))
3043         (setq group nil)))
3044     result))
3045
3046 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3047   "Directly jump to the other GROUP from summary buffer.
3048 If SHOW-ALL is non-nil, already read articles are also listed."
3049   (interactive
3050    (if (eq gnus-summary-buffer (current-buffer))
3051        (list (completing-read
3052               "Group: " gnus-active-hashtb nil t
3053               (when (and gnus-newsgroup-name
3054                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3055                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3056               'gnus-group-history)
3057              current-prefix-arg)
3058      (error "%s must be invoked from a gnus summary buffer." this-command)))
3059   (unless (or (zerop (length group))
3060               (and gnus-newsgroup-name
3061                    (string-equal gnus-newsgroup-name group)))
3062     (gnus-summary-exit)
3063     (gnus-summary-read-group group show-all
3064                              gnus-dont-select-after-jump-to-other-group)))
3065
3066 (defun gnus-summary-read-group-1 (group show-all no-article
3067                                         kill-buffer no-display
3068                                         &optional select-articles)
3069   ;; Killed foreign groups can't be entered.
3070   ;;  (when (and (not (gnus-group-native-p group))
3071   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3072   ;;    (error "Dead non-native groups can't be entered"))
3073   (gnus-message 5 "Retrieving newsgroup: %s..."
3074                 (gnus-group-decoded-name group))
3075   (let* ((new-group (gnus-summary-setup-buffer group))
3076          (quit-config (gnus-group-quit-config group))
3077          (did-select (and new-group (gnus-select-newsgroup
3078                                      group show-all select-articles))))
3079     (cond
3080      ;; This summary buffer exists already, so we just select it.
3081      ((not new-group)
3082       (gnus-set-global-variables)
3083       (when kill-buffer
3084         (gnus-kill-or-deaden-summary kill-buffer))
3085       (gnus-configure-windows 'summary 'force)
3086       (gnus-set-mode-line 'summary)
3087       (gnus-summary-position-point)
3088       (message "")
3089       t)
3090      ;; We couldn't select this group.
3091      ((null did-select)
3092       (when (and (eq major-mode 'gnus-summary-mode)
3093                  (not (equal (current-buffer) kill-buffer)))
3094         (kill-buffer (current-buffer))
3095         (if (not quit-config)
3096             (progn
3097               ;; Update the info -- marks might need to be removed,
3098               ;; for instance.
3099               (gnus-summary-update-info)
3100               (set-buffer gnus-group-buffer)
3101               (gnus-group-jump-to-group group)
3102               (gnus-group-next-unread-group 1))
3103           (gnus-handle-ephemeral-exit quit-config)))
3104       (let ((grpinfo (gnus-get-info group)))
3105         (if (null (gnus-info-read grpinfo))
3106             (gnus-message 3 "Group %s contains no messages" 
3107                           (gnus-group-decoded-name group))
3108           (gnus-message 3 "Can't select group")))
3109       nil)
3110      ;; The user did a `C-g' while prompting for number of articles,
3111      ;; so we exit this group.
3112      ((eq did-select 'quit)
3113       (and (eq major-mode 'gnus-summary-mode)
3114            (not (equal (current-buffer) kill-buffer))
3115            (kill-buffer (current-buffer)))
3116       (when kill-buffer
3117         (gnus-kill-or-deaden-summary kill-buffer))
3118       (if (not quit-config)
3119           (progn
3120             (set-buffer gnus-group-buffer)
3121             (gnus-group-jump-to-group group)
3122             (gnus-group-next-unread-group 1)
3123             (gnus-configure-windows 'group 'force))
3124         (gnus-handle-ephemeral-exit quit-config))
3125       ;; Finally signal the quit.
3126       (signal 'quit nil))
3127      ;; The group was successfully selected.
3128      (t
3129       (gnus-set-global-variables)
3130       ;; Save the active value in effect when the group was entered.
3131       (setq gnus-newsgroup-active
3132             (gnus-copy-sequence
3133              (gnus-active gnus-newsgroup-name)))
3134       ;; You can change the summary buffer in some way with this hook.
3135       (gnus-run-hooks 'gnus-select-group-hook)
3136       (gnus-update-format-specifications
3137        nil 'summary 'summary-mode 'summary-dummy)
3138       (gnus-update-summary-mark-positions)
3139       ;; Do score processing.
3140       (when gnus-use-scoring
3141         (gnus-possibly-score-headers))
3142       ;; Check whether to fill in the gaps in the threads.
3143       (when gnus-build-sparse-threads
3144         (gnus-build-sparse-threads))
3145       ;; Find the initial limit.
3146       (if gnus-show-threads
3147           (if show-all
3148               (let ((gnus-newsgroup-dormant nil))
3149                 (gnus-summary-initial-limit show-all))
3150             (gnus-summary-initial-limit show-all))
3151         ;; When untreaded, all articles are always shown.
3152         (setq gnus-newsgroup-limit
3153               (mapcar
3154                (lambda (header) (mail-header-number header))
3155                gnus-newsgroup-headers)))
3156       ;; Generate the summary buffer.
3157       (unless no-display
3158         (gnus-summary-prepare))
3159       (when gnus-use-trees
3160         (gnus-tree-open group)
3161         (setq gnus-summary-highlight-line-function
3162               'gnus-tree-highlight-article))
3163       ;; If the summary buffer is empty, but there are some low-scored
3164       ;; articles or some excluded dormants, we include these in the
3165       ;; buffer.
3166       (when (and (zerop (buffer-size))
3167                  (not no-display))
3168         (cond (gnus-newsgroup-dormant
3169                (gnus-summary-limit-include-dormant))
3170               ((and gnus-newsgroup-scored show-all)
3171                (gnus-summary-limit-include-expunged t))))
3172       ;; Function `gnus-apply-kill-file' must be called in this hook.
3173       (gnus-run-hooks 'gnus-apply-kill-hook)
3174       (if (and (zerop (buffer-size))
3175                (not no-display))
3176           (progn
3177             ;; This newsgroup is empty.
3178             (gnus-summary-catchup-and-exit nil t)
3179             (gnus-message 6 "No unread news")
3180             (when kill-buffer
3181               (gnus-kill-or-deaden-summary kill-buffer))
3182             ;; Return nil from this function.
3183             nil)
3184         ;; Hide conversation thread subtrees.  We cannot do this in
3185         ;; gnus-summary-prepare-hook since kill processing may not
3186         ;; work with hidden articles.
3187         (and gnus-show-threads
3188              gnus-thread-hide-subtree
3189              (gnus-summary-hide-all-threads))
3190         (when kill-buffer
3191           (gnus-kill-or-deaden-summary kill-buffer))
3192         ;; Show first unread article if requested.
3193         (if (and (not no-article)
3194                  (not no-display)
3195                  gnus-newsgroup-unreads
3196                  gnus-auto-select-first)
3197             (progn
3198               (gnus-configure-windows 'summary)
3199               (cond
3200                ((eq gnus-auto-select-first 'best)
3201                 (gnus-summary-best-unread-article))
3202                ((eq gnus-auto-select-first t)
3203                 (gnus-summary-first-unread-article))
3204                ((gnus-functionp gnus-auto-select-first)
3205                 (funcall gnus-auto-select-first))))
3206           ;; Don't select any articles, just move point to the first
3207           ;; article in the group.
3208           (goto-char (point-min))
3209           (gnus-summary-position-point)
3210           (gnus-configure-windows 'summary 'force)
3211           (gnus-set-mode-line 'summary))
3212         (when (get-buffer-window gnus-group-buffer t)
3213           ;; Gotta use windows, because recenter does weird stuff if
3214           ;; the current buffer ain't the displayed window.
3215           (let ((owin (selected-window)))
3216             (select-window (get-buffer-window gnus-group-buffer t))
3217             (when (gnus-group-goto-group group)
3218               (recenter))
3219             (select-window owin)))
3220         ;; Mark this buffer as "prepared".
3221         (setq gnus-newsgroup-prepared t)
3222         (gnus-run-hooks 'gnus-summary-prepared-hook)
3223         t)))))
3224
3225 (defun gnus-summary-prepare ()
3226   "Generate the summary buffer."
3227   (interactive)
3228   (let ((buffer-read-only nil))
3229     (erase-buffer)
3230     (setq gnus-newsgroup-data nil
3231           gnus-newsgroup-data-reverse nil)
3232     (gnus-run-hooks 'gnus-summary-generate-hook)
3233     ;; Generate the buffer, either with threads or without.
3234     (when gnus-newsgroup-headers
3235       (gnus-summary-prepare-threads
3236        (if gnus-show-threads
3237            (gnus-sort-gathered-threads
3238             (funcall gnus-summary-thread-gathering-function
3239                      (gnus-sort-threads
3240                       (gnus-cut-threads (gnus-make-threads)))))
3241          ;; Unthreaded display.
3242          (gnus-sort-articles gnus-newsgroup-headers))))
3243     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3244     ;; Call hooks for modifying summary buffer.
3245     (goto-char (point-min))
3246     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3247
3248 (defsubst gnus-general-simplify-subject (subject)
3249   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3250   (setq subject
3251         (cond
3252          ;; Truncate the subject.
3253          (gnus-simplify-subject-functions
3254           (gnus-map-function gnus-simplify-subject-functions subject))
3255          ((numberp gnus-summary-gather-subject-limit)
3256           (setq subject (gnus-simplify-subject-re subject))
3257           (if (> (length subject) gnus-summary-gather-subject-limit)
3258               (substring subject 0 gnus-summary-gather-subject-limit)
3259             subject))
3260          ;; Fuzzily simplify it.
3261          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3262           (gnus-simplify-subject-fuzzy subject))
3263          ;; Just remove the leading "Re:".
3264          (t
3265           (gnus-simplify-subject-re subject))))
3266
3267   (if (and gnus-summary-gather-exclude-subject
3268            (string-match gnus-summary-gather-exclude-subject subject))
3269       nil                               ; This article shouldn't be gathered
3270     subject))
3271
3272 (defun gnus-summary-simplify-subject-query ()
3273   "Query where the respool algorithm would put this article."
3274   (interactive)
3275   (gnus-summary-select-article)
3276   (message "%s"
3277            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3278
3279 (defun gnus-gather-threads-by-subject (threads)
3280   "Gather threads by looking at Subject headers."
3281   (if (not gnus-summary-make-false-root)
3282       threads
3283     (let ((hashtb (gnus-make-hashtable 1024))
3284           (prev threads)
3285           (result threads)
3286           subject hthread whole-subject)
3287       (while threads
3288         (setq subject (gnus-general-simplify-subject
3289                        (setq whole-subject (mail-header-subject
3290                                             (caar threads)))))
3291         (when subject
3292           (if (setq hthread (gnus-gethash subject hashtb))
3293               (progn
3294                 ;; We enter a dummy root into the thread, if we
3295                 ;; haven't done that already.
3296                 (unless (stringp (caar hthread))
3297                   (setcar hthread (list whole-subject (car hthread))))
3298                 ;; We add this new gathered thread to this gathered
3299                 ;; thread.
3300                 (setcdr (car hthread)
3301                         (nconc (cdar hthread) (list (car threads))))
3302                 ;; Remove it from the list of threads.
3303                 (setcdr prev (cdr threads))
3304                 (setq threads prev))
3305             ;; Enter this thread into the hash table.
3306             (gnus-sethash subject threads hashtb)))
3307         (setq prev threads)
3308         (setq threads (cdr threads)))
3309       result)))
3310
3311 (defun gnus-gather-threads-by-references (threads)
3312   "Gather threads by looking at References headers."
3313   (let ((idhashtb (gnus-make-hashtable 1024))
3314         (thhashtb (gnus-make-hashtable 1024))
3315         (prev threads)
3316         (result threads)
3317         ids references id gthread gid entered ref)
3318     (while threads
3319       (when (setq references (mail-header-references (caar threads)))
3320         (setq id (mail-header-id (caar threads))
3321               ids (gnus-split-references references)
3322               entered nil)
3323         (while (setq ref (pop ids))
3324           (setq ids (delete ref ids))
3325           (if (not (setq gid (gnus-gethash ref idhashtb)))
3326               (progn
3327                 (gnus-sethash ref id idhashtb)
3328                 (gnus-sethash id threads thhashtb))
3329             (setq gthread (gnus-gethash gid thhashtb))
3330             (unless entered
3331               ;; We enter a dummy root into the thread, if we
3332               ;; haven't done that already.
3333               (unless (stringp (caar gthread))
3334                 (setcar gthread (list (mail-header-subject (caar gthread))
3335                                       (car gthread))))
3336               ;; We add this new gathered thread to this gathered
3337               ;; thread.
3338               (setcdr (car gthread)
3339                       (nconc (cdar gthread) (list (car threads)))))
3340             ;; Add it into the thread hash table.
3341             (gnus-sethash id gthread thhashtb)
3342             (setq entered t)
3343             ;; Remove it from the list of threads.
3344             (setcdr prev (cdr threads))
3345             (setq threads prev))))
3346       (setq prev threads)
3347       (setq threads (cdr threads)))
3348     result))
3349
3350 (defun gnus-sort-gathered-threads (threads)
3351   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3352   (let ((result threads))
3353     (while threads
3354       (when (stringp (caar threads))
3355         (setcdr (car threads)
3356                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3357       (setq threads (cdr threads)))
3358     result))
3359
3360 (defun gnus-thread-loop-p (root thread)
3361   "Say whether ROOT is in THREAD."
3362   (let ((stack (list thread))
3363         (infloop 0)
3364         th)
3365     (while (setq thread (pop stack))
3366       (setq th (cdr thread))
3367       (while (and th
3368                   (not (eq (caar th) root)))
3369         (pop th))
3370       (if th
3371           ;; We have found a loop.
3372           (let (ref-dep)
3373             (setcdr thread (delq (car th) (cdr thread)))
3374             (if (boundp (setq ref-dep (intern "none"
3375                                               gnus-newsgroup-dependencies)))
3376                 (setcdr (symbol-value ref-dep)
3377                         (nconc (cdr (symbol-value ref-dep))
3378                                (list (car th))))
3379               (set ref-dep (list nil (car th))))
3380             (setq infloop 1
3381                   stack nil))
3382         ;; Push all the subthreads onto the stack.
3383         (push (cdr thread) stack)))
3384     infloop))
3385
3386 (defun gnus-make-threads ()
3387   "Go through the dependency hashtb and find the roots.  Return all threads."
3388   (let (threads)
3389     (while (catch 'infloop
3390              (mapatoms
3391               (lambda (refs)
3392                 ;; Deal with self-referencing References loops.
3393                 (when (and (car (symbol-value refs))
3394                            (not (zerop
3395                                  (apply
3396                                   '+
3397                                   (mapcar
3398                                    (lambda (thread)
3399                                      (gnus-thread-loop-p
3400                                       (car (symbol-value refs)) thread))
3401                                    (cdr (symbol-value refs)))))))
3402                   (setq threads nil)
3403                   (throw 'infloop t))
3404                 (unless (car (symbol-value refs))
3405                   ;; These threads do not refer back to any other articles,
3406                   ;; so they're roots.
3407                   (setq threads (append (cdr (symbol-value refs)) threads))))
3408               gnus-newsgroup-dependencies)))
3409     threads))
3410
3411 ;; Build the thread tree.
3412 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3413   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3414
3415 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3416 if it was already present.
3417
3418 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3419 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3420 Message-IDs will be renamed be renamed to a unique Message-ID before
3421 being entered.
3422
3423 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3424   (let* ((id (mail-header-id header))
3425          (id-dep (and id (intern id dependencies)))
3426          ref ref-dep ref-header)
3427     ;; Enter this `header' in the `dependencies' table.
3428     (cond
3429      ((not id-dep)
3430       (setq header nil))
3431      ;; The first two cases do the normal part: enter a new `header'
3432      ;; in the `dependencies' table.
3433      ((not (boundp id-dep))
3434       (set id-dep (list header)))
3435      ((null (car (symbol-value id-dep)))
3436       (setcar (symbol-value id-dep) header))
3437
3438      ;; From here the `header' was already present in the
3439      ;; `dependencies' table.
3440      (force-new
3441       ;; Overrides an existing entry;
3442       ;; just set the header part of the entry.
3443       (setcar (symbol-value id-dep) header))
3444
3445      ;; Renames the existing `header' to a unique Message-ID.
3446      ((not gnus-summary-ignore-duplicates)
3447       ;; An article with this Message-ID has already been seen.
3448       ;; We rename the Message-ID.
3449       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3450            (list header))
3451       (mail-header-set-id header id))
3452
3453      ;; The last case ignores an existing entry, except it adds any
3454      ;; additional Xrefs (in case the two articles came from different
3455      ;; servers.
3456      ;; Also sets `header' to `nil' meaning that the `dependencies'
3457      ;; table was *not* modified.
3458      (t
3459       (mail-header-set-xref
3460        (car (symbol-value id-dep))
3461        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3462                    "")
3463                (or (mail-header-xref header) "")))
3464       (setq header nil)))
3465
3466     (when header
3467       ;; First check if that we are not creating a References loop.
3468       (setq ref (gnus-parent-id (mail-header-references header)))
3469       (while (and ref
3470                   (setq ref-dep (intern-soft ref dependencies))
3471                   (boundp ref-dep)
3472                   (setq ref-header (car (symbol-value ref-dep))))
3473         (if (string= id ref)
3474             ;; Yuk!  This is a reference loop.  Make the article be a
3475             ;; root article.
3476             (progn
3477               (mail-header-set-references (car (symbol-value id-dep)) "none")
3478               (setq ref nil))
3479           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3480       (setq ref (gnus-parent-id (mail-header-references header)))
3481       (setq ref-dep (intern (or ref "none") dependencies))
3482       (if (boundp ref-dep)
3483           (setcdr (symbol-value ref-dep)
3484                   (nconc (cdr (symbol-value ref-dep))
3485                          (list (symbol-value id-dep))))
3486         (set ref-dep (list nil (symbol-value id-dep)))))
3487     header))
3488
3489 (defun gnus-build-sparse-threads ()
3490   (let ((headers gnus-newsgroup-headers)
3491         (mail-parse-charset gnus-newsgroup-charset)
3492         (gnus-summary-ignore-duplicates t)
3493         header references generation relations
3494         subject child end new-child date)
3495     ;; First we create an alist of generations/relations, where
3496     ;; generations is how much we trust the relation, and the relation
3497     ;; is parent/child.
3498     (gnus-message 7 "Making sparse threads...")
3499     (save-excursion
3500       (nnheader-set-temp-buffer " *gnus sparse threads*")
3501       (while (setq header (pop headers))
3502         (when (and (setq references (mail-header-references header))
3503                    (not (string= references "")))
3504           (insert references)
3505           (setq child (mail-header-id header)
3506                 subject (mail-header-subject header)
3507                 date (mail-header-date header)
3508                 generation 0)
3509           (while (search-backward ">" nil t)
3510             (setq end (1+ (point)))
3511             (when (search-backward "<" nil t)
3512               (setq new-child (buffer-substring (point) end))
3513               (push (list (incf generation)
3514                           child (setq child new-child)
3515                           subject date)
3516                     relations)))
3517           (when child
3518             (push (list (1+ generation) child nil subject) relations))
3519           (erase-buffer)))
3520       (kill-buffer (current-buffer)))
3521     ;; Sort over trustworthiness.
3522     (mapcar
3523      (lambda (relation)
3524        (when (gnus-dependencies-add-header
3525               (make-full-mail-header-from-decoded-header
3526                gnus-reffed-article-number
3527                (nth 3 relation) "" (or (nth 4 relation) "")
3528                (nth 1 relation)
3529                (or (nth 2 relation) "") 0 0 "")
3530               gnus-newsgroup-dependencies nil)
3531          (push gnus-reffed-article-number gnus-newsgroup-limit)
3532          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3533          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3534                gnus-newsgroup-reads)
3535          (decf gnus-reffed-article-number)))
3536      (sort relations 'car-less-than-car))
3537     (gnus-message 7 "Making sparse threads...done")))
3538
3539 (defun gnus-build-old-threads ()
3540   ;; Look at all the articles that refer back to old articles, and
3541   ;; fetch the headers for the articles that aren't there.  This will
3542   ;; build complete threads - if the roots haven't been expired by the
3543   ;; server, that is.
3544   (let ((mail-parse-charset gnus-newsgroup-charset)
3545         id heads)
3546     (mapatoms
3547      (lambda (refs)
3548        (when (not (car (symbol-value refs)))
3549          (setq heads (cdr (symbol-value refs)))
3550          (while heads
3551            (if (memq (mail-header-number (caar heads))
3552                      gnus-newsgroup-dormant)
3553                (setq heads (cdr heads))
3554              (setq id (symbol-name refs))
3555              (while (and (setq id (gnus-build-get-header id))
3556                          (not (car (gnus-id-to-thread id)))))
3557              (setq heads nil)))))
3558      gnus-newsgroup-dependencies)))
3559
3560 ;; This function has to be called with point after the article number
3561 ;; on the beginning of the line.
3562 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3563   (let ((eol (gnus-point-at-eol))
3564         (buffer (current-buffer))
3565         header)
3566
3567     ;; overview: [num subject from date id refs chars lines misc]
3568     (unwind-protect
3569         (progn
3570           (narrow-to-region (point) eol)
3571           (unless (eobp)
3572             (forward-char))
3573
3574           (setq header
3575                 (make-full-mail-header
3576                  number                         ; number
3577                  (nnheader-nov-field)           ; subject
3578                  (nnheader-nov-field)           ; from
3579                  (nnheader-nov-field)           ; date
3580                  (nnheader-nov-read-message-id) ; id
3581                  (nnheader-nov-field)           ; refs
3582                  (nnheader-nov-read-integer)    ; chars
3583                  (nnheader-nov-read-integer)    ; lines
3584                  (unless (eobp)
3585                    (if (looking-at "Xref: ")
3586                        (goto-char (match-end 0)))
3587                    (nnheader-nov-field))        ; Xref
3588                  (nnheader-nov-parse-extra))))  ; extra
3589
3590       (widen))
3591
3592     (when gnus-alter-header-function
3593       (funcall gnus-alter-header-function header))
3594     (gnus-dependencies-add-header header dependencies force-new)))
3595
3596 (defun gnus-build-get-header (id)
3597   "Look through the buffer of NOV lines and find the header to ID.
3598 Enter this line into the dependencies hash table, and return
3599 the id of the parent article (if any)."
3600   (let ((deps gnus-newsgroup-dependencies)
3601         found header)
3602     (prog1
3603         (save-excursion
3604           (set-buffer nntp-server-buffer)
3605           (let ((case-fold-search nil))
3606             (goto-char (point-min))
3607             (while (and (not found)
3608                         (search-forward id nil t))
3609               (beginning-of-line)
3610               (setq found (looking-at
3611                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3612                                    (regexp-quote id))))
3613               (or found (beginning-of-line 2)))
3614             (when found
3615               (beginning-of-line)
3616               (and
3617                (setq header (gnus-nov-parse-line
3618                              (read (current-buffer)) deps))
3619                (gnus-parent-id (mail-header-references header))))))
3620       (when header
3621         (let ((number (mail-header-number header)))
3622           (push number gnus-newsgroup-limit)
3623           (push header gnus-newsgroup-headers)
3624           (if (memq number gnus-newsgroup-unselected)
3625               (progn
3626                 (push number gnus-newsgroup-unreads)
3627                 (setq gnus-newsgroup-unselected
3628                       (delq number gnus-newsgroup-unselected)))
3629             (push number gnus-newsgroup-ancient)))))))
3630
3631 (defun gnus-build-all-threads ()
3632   "Read all the headers."
3633   (let ((gnus-summary-ignore-duplicates t)
3634         (mail-parse-charset gnus-newsgroup-charset)
3635         (dependencies gnus-newsgroup-dependencies)
3636         header article)
3637     (save-excursion
3638       (set-buffer nntp-server-buffer)
3639       (let ((case-fold-search nil))
3640         (goto-char (point-min))
3641         (while (not (eobp))
3642           (ignore-errors
3643             (setq article (read (current-buffer))
3644                   header (gnus-nov-parse-line article dependencies)))
3645           (when header
3646             (save-excursion
3647               (set-buffer gnus-summary-buffer)
3648               (push header gnus-newsgroup-headers)
3649               (if (memq (setq article (mail-header-number header))
3650                         gnus-newsgroup-unselected)
3651                   (progn
3652                     (push article gnus-newsgroup-unreads)
3653                     (setq gnus-newsgroup-unselected
3654                           (delq article gnus-newsgroup-unselected)))
3655                 (push article gnus-newsgroup-ancient)))
3656             (forward-line 1)))))))
3657
3658 (defun gnus-summary-update-article-line (article header)
3659   "Update the line for ARTICLE using HEADERS."
3660   (let* ((id (mail-header-id header))
3661          (thread (gnus-id-to-thread id)))
3662     (unless thread
3663       (error "Article in no thread"))
3664     ;; Update the thread.
3665     (setcar thread header)
3666     (gnus-summary-goto-subject article)
3667     (let* ((datal (gnus-data-find-list article))
3668            (data (car datal))
3669            (length (when (cdr datal)
3670                      (- (gnus-data-pos data)
3671                         (gnus-data-pos (cadr datal)))))
3672            (buffer-read-only nil)
3673            (level (gnus-summary-thread-level)))
3674       (gnus-delete-line)
3675       (gnus-summary-insert-line
3676        header level nil (gnus-article-mark article)
3677        (memq article gnus-newsgroup-replied)
3678        (memq article gnus-newsgroup-expirable)
3679        ;; Only insert the Subject string when it's different
3680        ;; from the previous Subject string.
3681        (if (and
3682             gnus-show-threads
3683             (gnus-subject-equal
3684              (condition-case ()
3685                  (mail-header-subject
3686                   (gnus-data-header
3687                    (cadr
3688                     (gnus-data-find-list
3689                      article
3690                      (gnus-data-list t)))))
3691                ;; Error on the side of excessive subjects.
3692                (error ""))
3693              (mail-header-subject header)))
3694            ""
3695          (mail-header-subject header))
3696        nil (cdr (assq article gnus-newsgroup-scored))
3697        (memq article gnus-newsgroup-processable))
3698       (when length
3699         (gnus-data-update-list
3700          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3701
3702 (defun gnus-summary-update-article (article &optional iheader)
3703   "Update ARTICLE in the summary buffer."
3704   (set-buffer gnus-summary-buffer)
3705   (let* ((header (gnus-summary-article-header article))
3706          (id (mail-header-id header))
3707          (data (gnus-data-find article))
3708          (thread (gnus-id-to-thread id))
3709          (references (mail-header-references header))
3710          (parent
3711           (gnus-id-to-thread
3712            (or (gnus-parent-id
3713                 (when (and references
3714                            (not (equal "" references)))
3715                   references))
3716                "none")))
3717          (buffer-read-only nil)
3718          (old (car thread)))
3719     (when thread
3720       (unless iheader
3721         (setcar thread nil)
3722         (when parent
3723           (delq thread parent)))
3724       (if (gnus-summary-insert-subject id header)
3725           ;; Set the (possibly) new article number in the data structure.
3726           (gnus-data-set-number data (gnus-id-to-article id))
3727         (setcar thread old)
3728         nil))))
3729
3730 (defun gnus-rebuild-thread (id &optional line)
3731   "Rebuild the thread containing ID.
3732 If LINE, insert the rebuilt thread starting on line LINE."
3733   (let ((buffer-read-only nil)
3734         old-pos current thread data)
3735     (if (not gnus-show-threads)
3736         (setq thread (list (car (gnus-id-to-thread id))))
3737       ;; Get the thread this article is part of.
3738       (setq thread (gnus-remove-thread id)))
3739     (setq old-pos (gnus-point-at-bol))
3740     (setq current (save-excursion
3741                     (and (zerop (forward-line -1))
3742                          (gnus-summary-article-number))))
3743     ;; If this is a gathered thread, we have to go some re-gathering.
3744     (when (stringp (car thread))
3745       (let ((subject (car thread))
3746             roots thr)
3747         (setq thread (cdr thread))
3748         (while thread
3749           (unless (memq (setq thr (gnus-id-to-thread
3750                                    (gnus-root-id
3751                                     (mail-header-id (caar thread)))))
3752                         roots)
3753             (push thr roots))
3754           (setq thread (cdr thread)))
3755         ;; We now have all (unique) roots.
3756         (if (= (length roots) 1)
3757             ;; All the loose roots are now one solid root.
3758             (setq thread (car roots))
3759           (setq thread (cons subject (gnus-sort-threads roots))))))
3760     (let (threads)
3761       ;; We then insert this thread into the summary buffer.
3762       (when line
3763         (goto-char (point-min))
3764         (forward-line (1- line)))
3765       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3766         (if gnus-show-threads
3767             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3768           (gnus-summary-prepare-unthreaded thread))
3769         (setq data (nreverse gnus-newsgroup-data))
3770         (setq threads gnus-newsgroup-threads))
3771       ;; We splice the new data into the data structure.
3772       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3773       ;;!!! then we want to insert at the beginning of the buffer.
3774       ;;!!! That happens to be true with Gnus now, but that may
3775       ;;!!! change in the future.  Perhaps.
3776       (gnus-data-enter-list
3777        (if line nil current) data (- (point) old-pos))
3778       (setq gnus-newsgroup-threads
3779             (nconc threads gnus-newsgroup-threads))
3780       (gnus-data-compute-positions))))
3781
3782 (defun gnus-number-to-header (number)
3783   "Return the header for article NUMBER."
3784   (let ((headers gnus-newsgroup-headers))
3785     (while (and headers
3786                 (not (= number (mail-header-number (car headers)))))
3787       (pop headers))
3788     (when headers
3789       (car headers))))
3790
3791 (defun gnus-parent-headers (in-headers &optional generation)
3792   "Return the headers of the GENERATIONeth parent of HEADERS."
3793   (unless generation
3794     (setq generation 1))
3795   (let ((parent t)
3796         (headers in-headers)
3797         references)
3798     (while (and parent
3799                 (not (zerop generation))
3800                 (setq references (mail-header-references headers)))
3801       (setq headers (if (and references
3802                              (setq parent (gnus-parent-id references)))
3803                         (car (gnus-id-to-thread parent))
3804                       nil))
3805       (decf generation))
3806     (and (not (eq headers in-headers))
3807          headers)))
3808
3809 (defun gnus-id-to-thread (id)
3810   "Return the (sub-)thread where ID appears."
3811   (gnus-gethash id gnus-newsgroup-dependencies))
3812
3813 (defun gnus-id-to-article (id)
3814   "Return the article number of ID."
3815   (let ((thread (gnus-id-to-thread id)))
3816     (when (and thread
3817                (car thread))
3818       (mail-header-number (car thread)))))
3819
3820 (defun gnus-id-to-header (id)
3821   "Return the article headers of ID."
3822   (car (gnus-id-to-thread id)))
3823
3824 (defun gnus-article-displayed-root-p (article)
3825   "Say whether ARTICLE is a root(ish) article."
3826   (let ((level (gnus-summary-thread-level article))
3827         (refs (mail-header-references  (gnus-summary-article-header article)))
3828         particle)
3829     (cond
3830      ((null level) nil)
3831      ((zerop level) t)
3832      ((null refs) t)
3833      ((null (gnus-parent-id refs)) t)
3834      ((and (= 1 level)
3835            (null (setq particle (gnus-id-to-article
3836                                  (gnus-parent-id refs))))
3837            (null (gnus-summary-thread-level particle)))))))
3838
3839 (defun gnus-root-id (id)
3840   "Return the id of the root of the thread where ID appears."
3841   (let (last-id prev)
3842     (while (and id (setq prev (car (gnus-id-to-thread id))))
3843       (setq last-id id
3844             id (gnus-parent-id (mail-header-references prev))))
3845     last-id))
3846
3847 (defun gnus-articles-in-thread (thread)
3848   "Return the list of articles in THREAD."
3849   (cons (mail-header-number (car thread))
3850         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3851
3852 (defun gnus-remove-thread (id &optional dont-remove)
3853   "Remove the thread that has ID in it."
3854   (let (headers thread last-id)
3855     ;; First go up in this thread until we find the root.
3856     (setq last-id (gnus-root-id id)
3857           headers (message-flatten-list (gnus-id-to-thread last-id)))
3858     ;; We have now found the real root of this thread.  It might have
3859     ;; been gathered into some loose thread, so we have to search
3860     ;; through the threads to find the thread we wanted.
3861     (let ((threads gnus-newsgroup-threads)
3862           sub)
3863       (while threads
3864         (setq sub (car threads))
3865         (if (stringp (car sub))
3866             ;; This is a gathered thread, so we look at the roots
3867             ;; below it to find whether this article is in this
3868             ;; gathered root.
3869             (progn
3870               (setq sub (cdr sub))
3871               (while sub
3872                 (when (member (caar sub) headers)
3873                   (setq thread (car threads)
3874                         threads nil
3875                         sub nil))
3876                 (setq sub (cdr sub))))
3877           ;; It's an ordinary thread, so we check it.
3878           (when (eq (car sub) (car headers))
3879             (setq thread sub
3880                   threads nil)))
3881         (setq threads (cdr threads)))
3882       ;; If this article is in no thread, then it's a root.
3883       (if thread
3884           (unless dont-remove
3885             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3886         (setq thread (gnus-id-to-thread last-id)))
3887       (when thread
3888         (prog1
3889             thread                      ; We return this thread.
3890           (unless dont-remove
3891             (if (stringp (car thread))
3892                 (progn
3893                   ;; If we use dummy roots, then we have to remove the
3894                   ;; dummy root as well.
3895                   (when (eq gnus-summary-make-false-root 'dummy)
3896                     ;; We go to the dummy root by going to
3897                     ;; the first sub-"thread", and then one line up.
3898                     (gnus-summary-goto-article
3899                      (mail-header-number (caadr thread)))
3900                     (forward-line -1)
3901                     (gnus-delete-line)
3902                     (gnus-data-compute-positions))
3903                   (setq thread (cdr thread))
3904                   (while thread
3905                     (gnus-remove-thread-1 (car thread))
3906                     (setq thread (cdr thread))))
3907               (gnus-remove-thread-1 thread))))))))
3908
3909 (defun gnus-remove-thread-1 (thread)
3910   "Remove the thread THREAD recursively."
3911   (let ((number (mail-header-number (pop thread)))
3912         d)
3913     (setq thread (reverse thread))
3914     (while thread
3915       (gnus-remove-thread-1 (pop thread)))
3916     (when (setq d (gnus-data-find number))
3917       (goto-char (gnus-data-pos d))
3918       (gnus-summary-show-thread)
3919       (gnus-data-remove
3920        number
3921        (- (gnus-point-at-bol)
3922           (prog1
3923               (1+ (gnus-point-at-eol))
3924             (gnus-delete-line)))))))
3925
3926 (defun gnus-sort-threads-1 (threads func)
3927   (sort (mapcar (lambda (thread)
3928                   (cons (car thread)
3929                         (and (cdr thread)
3930                              (gnus-sort-threads-1 (cdr thread) func))))
3931                 threads) func))
3932
3933 (defun gnus-sort-threads (threads)
3934   "Sort THREADS."
3935   (if (not gnus-thread-sort-functions)
3936       threads
3937     (gnus-message 8 "Sorting threads...")
3938     (prog1
3939         (gnus-sort-threads-1
3940          threads
3941          (gnus-make-sort-function gnus-thread-sort-functions))
3942       (gnus-message 8 "Sorting threads...done"))))
3943
3944 (defun gnus-sort-articles (articles)
3945   "Sort ARTICLES."
3946   (when gnus-article-sort-functions
3947     (gnus-message 7 "Sorting articles...")
3948     (prog1
3949         (setq gnus-newsgroup-headers
3950               (sort articles (gnus-make-sort-function
3951                               gnus-article-sort-functions)))
3952       (gnus-message 7 "Sorting articles...done"))))
3953
3954 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3955 (defmacro gnus-thread-header (thread)
3956   "Return header of first article in THREAD.
3957 Note that THREAD must never, ever be anything else than a variable -
3958 using some other form will lead to serious barfage."
3959   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3960   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3961   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3962         (vector thread) 2))
3963
3964 (defsubst gnus-article-sort-by-number (h1 h2)
3965   "Sort articles by article number."
3966   (< (mail-header-number h1)
3967      (mail-header-number h2)))
3968
3969 (defun gnus-thread-sort-by-number (h1 h2)
3970   "Sort threads by root article number."
3971   (gnus-article-sort-by-number
3972    (gnus-thread-header h1) (gnus-thread-header h2)))
3973
3974 (defsubst gnus-article-sort-by-lines (h1 h2)
3975   "Sort articles by article Lines header."
3976   (< (mail-header-lines h1)
3977      (mail-header-lines h2)))
3978
3979 (defun gnus-thread-sort-by-lines (h1 h2)
3980   "Sort threads by root article Lines header."
3981   (gnus-article-sort-by-lines
3982    (gnus-thread-header h1) (gnus-thread-header h2)))
3983
3984 (defsubst gnus-article-sort-by-chars (h1 h2)
3985   "Sort articles by octet length."
3986   (< (mail-header-chars h1)
3987      (mail-header-chars h2)))
3988
3989 (defun gnus-thread-sort-by-chars (h1 h2)
3990   "Sort threads by root article octet length."
3991   (gnus-article-sort-by-chars
3992    (gnus-thread-header h1) (gnus-thread-header h2)))
3993
3994 (defsubst gnus-article-sort-by-author (h1 h2)
3995   "Sort articles by root author."
3996   (string-lessp
3997    (let ((addr (car (mime-entity-read-field h1 'From))))
3998      (or (std11-full-name-string addr)
3999          (std11-address-string addr)
4000          ""))
4001    (let ((addr (car (mime-entity-read-field h2 'From))))
4002      (or (std11-full-name-string addr)
4003          (std11-address-string addr)
4004          ""))
4005    ))
4006
4007 (defun gnus-thread-sort-by-author (h1 h2)
4008   "Sort threads by root author."
4009   (gnus-article-sort-by-author
4010    (gnus-thread-header h1)  (gnus-thread-header h2)))
4011
4012 (defsubst gnus-article-sort-by-subject (h1 h2)
4013   "Sort articles by root subject."
4014   (string-lessp
4015    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4016    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4017
4018 (defun gnus-thread-sort-by-subject (h1 h2)
4019   "Sort threads by root subject."
4020   (gnus-article-sort-by-subject
4021    (gnus-thread-header h1) (gnus-thread-header h2)))
4022
4023 (defsubst gnus-article-sort-by-date (h1 h2)
4024   "Sort articles by root article date."
4025   (time-less-p
4026    (gnus-date-get-time (mail-header-date h1))
4027    (gnus-date-get-time (mail-header-date h2))))
4028
4029 (defun gnus-thread-sort-by-date (h1 h2)
4030   "Sort threads by root article date."
4031   (gnus-article-sort-by-date
4032    (gnus-thread-header h1) (gnus-thread-header h2)))
4033
4034 (defsubst gnus-article-sort-by-score (h1 h2)
4035   "Sort articles by root article score.
4036 Unscored articles will be counted as having a score of zero."
4037   (> (or (cdr (assq (mail-header-number h1)
4038                     gnus-newsgroup-scored))
4039          gnus-summary-default-score 0)
4040      (or (cdr (assq (mail-header-number h2)
4041                     gnus-newsgroup-scored))
4042          gnus-summary-default-score 0)))
4043
4044 (defun gnus-thread-sort-by-score (h1 h2)
4045   "Sort threads by root article score."
4046   (gnus-article-sort-by-score
4047    (gnus-thread-header h1) (gnus-thread-header h2)))
4048
4049 (defun gnus-thread-sort-by-total-score (h1 h2)
4050   "Sort threads by the sum of all scores in the thread.
4051 Unscored articles will be counted as having a score of zero."
4052   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4053
4054 (defun gnus-thread-total-score (thread)
4055   ;; This function find the total score of THREAD.
4056   (cond ((null thread)
4057          0)
4058         ((consp thread)
4059          (if (stringp (car thread))
4060              (apply gnus-thread-score-function 0
4061                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4062            (gnus-thread-total-score-1 thread)))
4063         (t
4064          (gnus-thread-total-score-1 (list thread)))))
4065
4066 (defun gnus-thread-total-score-1 (root)
4067   ;; This function find the total score of the thread below ROOT.
4068   (setq root (car root))
4069   (apply gnus-thread-score-function
4070          (or (append
4071               (mapcar 'gnus-thread-total-score
4072                       (cdr (gnus-id-to-thread (mail-header-id root))))
4073               (when (> (mail-header-number root) 0)
4074                 (list (or (cdr (assq (mail-header-number root)
4075                                      gnus-newsgroup-scored))
4076                           gnus-summary-default-score 0))))
4077              (list gnus-summary-default-score)
4078              '(0))))
4079
4080 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4081 (defvar gnus-tmp-prev-subject nil)
4082 (defvar gnus-tmp-false-parent nil)
4083 (defvar gnus-tmp-root-expunged nil)
4084 (defvar gnus-tmp-dummy-line nil)
4085
4086 (eval-when-compile (defvar gnus-tmp-header))
4087 (defun gnus-extra-header (type &optional header)
4088   "Return the extra header of TYPE."
4089   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4090       ""))
4091
4092 (defun gnus-summary-prepare-threads (threads)
4093   "Prepare summary buffer from THREADS and indentation LEVEL.
4094 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4095 or a straight list of headers."
4096   (gnus-message 7 "Generating summary...")
4097
4098   (setq gnus-newsgroup-threads threads)
4099   (beginning-of-line)
4100
4101   (let ((gnus-tmp-level 0)
4102         (default-score (or gnus-summary-default-score 0))
4103         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4104         thread number subject stack state gnus-tmp-gathered beg-match
4105         new-roots gnus-tmp-new-adopts thread-end
4106         gnus-tmp-header gnus-tmp-unread
4107         gnus-tmp-replied gnus-tmp-subject-or-nil
4108         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4109         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4110         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4111
4112     (setq gnus-tmp-prev-subject nil)
4113
4114     (if (vectorp (car threads))
4115         ;; If this is a straight (sic) list of headers, then a
4116         ;; threaded summary display isn't required, so we just create
4117         ;; an unthreaded one.
4118         (gnus-summary-prepare-unthreaded threads)
4119
4120       ;; Do the threaded display.
4121
4122       (while (or threads stack gnus-tmp-new-adopts new-roots)
4123
4124         (if (and (= gnus-tmp-level 0)
4125                  (or (not stack)
4126                      (= (caar stack) 0))
4127                  (not gnus-tmp-false-parent)
4128                  (or gnus-tmp-new-adopts new-roots))
4129             (if gnus-tmp-new-adopts
4130                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4131                       thread (list (car gnus-tmp-new-adopts))
4132                       gnus-tmp-header (caar thread)
4133                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4134               (when new-roots
4135                 (setq thread (list (car new-roots))
4136                       gnus-tmp-header (caar thread)
4137                       new-roots (cdr new-roots))))
4138
4139           (if threads
4140               ;; If there are some threads, we do them before the
4141               ;; threads on the stack.
4142               (setq thread threads
4143                     gnus-tmp-header (caar thread))
4144             ;; There were no current threads, so we pop something off
4145             ;; the stack.
4146             (setq state (car stack)
4147                   gnus-tmp-level (car state)
4148                   thread (cdr state)
4149                   stack (cdr stack)
4150                   gnus-tmp-header (caar thread))))
4151
4152         (setq gnus-tmp-false-parent nil)
4153         (setq gnus-tmp-root-expunged nil)
4154         (setq thread-end nil)
4155
4156         (if (stringp gnus-tmp-header)
4157             ;; The header is a dummy root.
4158             (cond
4159              ((eq gnus-summary-make-false-root 'adopt)
4160               ;; We let the first article adopt the rest.
4161               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4162                                                (cddar thread)))
4163               (setq gnus-tmp-gathered
4164                     (nconc (mapcar
4165                             (lambda (h) (mail-header-number (car h)))
4166                             (cddar thread))
4167                            gnus-tmp-gathered))
4168               (setq thread (cons (list (caar thread)
4169                                        (cadar thread))
4170                                  (cdr thread)))
4171               (setq gnus-tmp-level -1
4172                     gnus-tmp-false-parent t))
4173              ((eq gnus-summary-make-false-root 'empty)
4174               ;; We print adopted articles with empty subject fields.
4175               (setq gnus-tmp-gathered
4176                     (nconc (mapcar
4177                             (lambda (h) (mail-header-number (car h)))
4178                             (cddar thread))
4179                            gnus-tmp-gathered))
4180               (setq gnus-tmp-level -1))
4181              ((eq gnus-summary-make-false-root 'dummy)
4182               ;; We remember that we probably want to output a dummy
4183               ;; root.
4184               (setq gnus-tmp-dummy-line gnus-tmp-header)
4185               (setq gnus-tmp-prev-subject gnus-tmp-header))
4186              (t
4187               ;; We do not make a root for the gathered
4188               ;; sub-threads at all.
4189               (setq gnus-tmp-level -1)))
4190
4191           (setq number (mail-header-number gnus-tmp-header)
4192                 subject (mail-header-subject gnus-tmp-header))
4193
4194           (cond
4195            ;; If the thread has changed subject, we might want to make
4196            ;; this subthread into a root.
4197            ((and (null gnus-thread-ignore-subject)
4198                  (not (zerop gnus-tmp-level))
4199                  gnus-tmp-prev-subject
4200                  (not (inline
4201                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4202             (setq new-roots (nconc new-roots (list (car thread)))
4203                   thread-end t
4204                   gnus-tmp-header nil))
4205            ;; If the article lies outside the current limit,
4206            ;; then we do not display it.
4207            ((not (memq number gnus-newsgroup-limit))
4208             (setq gnus-tmp-gathered
4209                   (nconc (mapcar
4210                           (lambda (h) (mail-header-number (car h)))
4211                           (cdar thread))
4212                          gnus-tmp-gathered))
4213             (setq gnus-tmp-new-adopts (if (cdar thread)
4214                                           (append gnus-tmp-new-adopts
4215                                                   (cdar thread))
4216                                         gnus-tmp-new-adopts)
4217                   thread-end t
4218                   gnus-tmp-header nil)
4219             (when (zerop gnus-tmp-level)
4220               (setq gnus-tmp-root-expunged t)))
4221            ;; Perhaps this article is to be marked as read?
4222            ((and gnus-summary-mark-below
4223                  (< (or (cdr (assq number gnus-newsgroup-scored))
4224                         default-score)
4225                     gnus-summary-mark-below)
4226                  ;; Don't touch sparse articles.
4227                  (not (gnus-summary-article-sparse-p number))
4228                  (not (gnus-summary-article-ancient-p number)))
4229             (setq gnus-newsgroup-unreads
4230                   (delq number gnus-newsgroup-unreads))
4231             (if gnus-newsgroup-auto-expire
4232                 (push number gnus-newsgroup-expirable)
4233               (push (cons number gnus-low-score-mark)
4234                     gnus-newsgroup-reads))))
4235
4236           (when gnus-tmp-header
4237             ;; We may have an old dummy line to output before this
4238             ;; article.
4239             (when (and gnus-tmp-dummy-line
4240                        (gnus-subject-equal
4241                         gnus-tmp-dummy-line
4242                         (mail-header-subject gnus-tmp-header)))
4243               (gnus-summary-insert-dummy-line
4244                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4245               (setq gnus-tmp-dummy-line nil))
4246
4247             ;; Compute the mark.
4248             (setq gnus-tmp-unread (gnus-article-mark number))
4249
4250             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4251                                   gnus-tmp-header gnus-tmp-level)
4252                   gnus-newsgroup-data)
4253
4254             ;; Actually insert the line.
4255             (setq
4256              gnus-tmp-subject-or-nil
4257              (cond
4258               ((and gnus-thread-ignore-subject
4259                     gnus-tmp-prev-subject
4260                     (not (inline (gnus-subject-equal
4261                                   gnus-tmp-prev-subject subject))))
4262                subject)
4263               ((zerop gnus-tmp-level)
4264                (if (and (eq gnus-summary-make-false-root 'empty)
4265                         (memq number gnus-tmp-gathered)
4266                         gnus-tmp-prev-subject
4267                         (inline (gnus-subject-equal
4268                                  gnus-tmp-prev-subject subject)))
4269                    gnus-summary-same-subject
4270                  subject))
4271               (t gnus-summary-same-subject)))
4272             (if (and (eq gnus-summary-make-false-root 'adopt)
4273                      (= gnus-tmp-level 1)
4274                      (memq number gnus-tmp-gathered))
4275                 (setq gnus-tmp-opening-bracket ?\<
4276                       gnus-tmp-closing-bracket ?\>)
4277               (setq gnus-tmp-opening-bracket ?\[
4278                     gnus-tmp-closing-bracket ?\]))
4279             (setq
4280              gnus-tmp-indentation
4281              (aref gnus-thread-indent-array gnus-tmp-level)
4282              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4283              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4284                                 gnus-summary-default-score 0)
4285              gnus-tmp-score-char
4286              (if (or (null gnus-summary-default-score)
4287                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4288                          gnus-summary-zcore-fuzz))
4289                  ?  ;Whitespace
4290                (if (< gnus-tmp-score gnus-summary-default-score)
4291                    gnus-score-below-mark gnus-score-over-mark))
4292              gnus-tmp-replied
4293              (cond ((memq number gnus-newsgroup-processable)
4294                     gnus-process-mark)
4295                    ((memq number gnus-newsgroup-cached)
4296                     gnus-cached-mark)
4297                    ((memq number gnus-newsgroup-replied)
4298                     gnus-replied-mark)
4299                    ((memq number gnus-newsgroup-forwarded)
4300                     gnus-forwarded-mark)
4301                    ((memq number gnus-newsgroup-saved)
4302                     gnus-saved-mark)
4303                    (t gnus-no-mark))
4304              gnus-tmp-from (mail-header-from gnus-tmp-header)
4305              gnus-tmp-name
4306              (cond
4307               ((string-match "<[^>]+> *$" gnus-tmp-from)
4308                (setq beg-match (match-beginning 0))
4309                (or (and (string-match "^\".+\"" gnus-tmp-from)
4310                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4311                    (substring gnus-tmp-from 0 beg-match)))
4312               ((string-match "(.+)" gnus-tmp-from)
4313                (substring gnus-tmp-from
4314                           (1+ (match-beginning 0)) (1- (match-end 0))))
4315               (t gnus-tmp-from)))
4316             (when (string= gnus-tmp-name "")
4317               (setq gnus-tmp-name gnus-tmp-from))
4318             (unless (numberp gnus-tmp-lines)
4319               (setq gnus-tmp-lines -1))
4320             (when (= gnus-tmp-lines -1)
4321               (setq gnus-tmp-lines "?"))
4322             (gnus-put-text-property
4323              (point)
4324              (progn (eval gnus-summary-line-format-spec) (point))
4325              'gnus-number number)
4326             (when gnus-visual-p
4327               (forward-line -1)
4328               (gnus-run-hooks 'gnus-summary-update-hook)
4329               (forward-line 1))
4330
4331             (setq gnus-tmp-prev-subject subject)))
4332
4333         (when (nth 1 thread)
4334           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4335         (incf gnus-tmp-level)
4336         (setq threads (if thread-end nil (cdar thread)))
4337         (unless threads
4338           (setq gnus-tmp-level 0)))))
4339   (gnus-message 7 "Generating summary...done"))
4340
4341 (defun gnus-summary-prepare-unthreaded (headers)
4342   "Generate an unthreaded summary buffer based on HEADERS."
4343   (let (header number mark)
4344
4345     (beginning-of-line)
4346
4347     (while headers
4348       ;; We may have to root out some bad articles...
4349       (when (memq (setq number (mail-header-number
4350                                 (setq header (pop headers))))
4351                   gnus-newsgroup-limit)
4352         ;; Mark article as read when it has a low score.
4353         (when (and gnus-summary-mark-below
4354                    (< (or (cdr (assq number gnus-newsgroup-scored))
4355                           gnus-summary-default-score 0)
4356                       gnus-summary-mark-below)
4357                    (not (gnus-summary-article-ancient-p number)))
4358           (setq gnus-newsgroup-unreads
4359                 (delq number gnus-newsgroup-unreads))
4360           (if gnus-newsgroup-auto-expire
4361               (push number gnus-newsgroup-expirable)
4362             (push (cons number gnus-low-score-mark)
4363                   gnus-newsgroup-reads)))
4364
4365         (setq mark (gnus-article-mark number))
4366         (push (gnus-data-make number mark (1+ (point)) header 0)
4367               gnus-newsgroup-data)
4368         (gnus-summary-insert-line
4369          header 0 number
4370          mark (memq number gnus-newsgroup-replied)
4371          (memq number gnus-newsgroup-expirable)
4372          (mail-header-subject header) nil
4373          (cdr (assq number gnus-newsgroup-scored))
4374          (memq number gnus-newsgroup-processable))))))
4375
4376 (defun gnus-summary-remove-list-identifiers ()
4377   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4378   (let ((regexp (if (consp gnus-list-identifiers)
4379                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4380                   gnus-list-identifiers))
4381         changed subject)
4382     (when regexp
4383       (dolist (header gnus-newsgroup-headers)
4384         (setq subject (mail-header-subject header)
4385               changed nil)
4386         (while (string-match
4387                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4388                 subject)
4389           (setq subject
4390                 (concat (substring subject 0 (match-beginning 2))
4391                         (substring subject (match-end 0)))
4392                 changed t))
4393         (when (and changed
4394                    (string-match
4395                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4396           (setq subject
4397                 (concat (substring subject 0 (match-beginning 1))
4398                         (substring subject (match-end 1)))))
4399         (when changed
4400           (mail-header-set-subject header subject))))))
4401
4402 (defun gnus-fetch-headers (articles)
4403   "Fetch headers of ARTICLES."
4404   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4405     (gnus-message 5 "Fetching headers for %s..." name)
4406     (prog1
4407         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4408         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4409             (if (eq 'nov
4410                     (setq gnus-headers-retrieved-by
4411                           (gnus-retrieve-headers
4412                            articles gnus-newsgroup-name
4413                            ;; We might want to fetch old headers, but
4414                            ;; not if there is only 1 article.
4415                            (and (or (and
4416                                      (not (eq gnus-fetch-old-headers 'some))
4417                                      (not (numberp gnus-fetch-old-headers)))
4418                                     (> (length articles) 1))
4419                                 gnus-fetch-old-headers))))
4420                 (gnus-get-newsgroup-headers-xover
4421                  articles nil nil gnus-newsgroup-name t)
4422               (gnus-get-newsgroup-headers))
4423           (gnus-retrieve-parsed-headers
4424            articles gnus-newsgroup-name
4425            ;; We might want to fetch old headers, but
4426            ;; not if there is only 1 article.
4427            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4428                          (not (numberp gnus-fetch-old-headers)))
4429                     (> (length articles) 1))
4430                 gnus-fetch-old-headers)))
4431       (gnus-message 5 "Fetching headers for %s...done" name))))
4432
4433 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4434   "Select newsgroup GROUP.
4435 If READ-ALL is non-nil, all articles in the group are selected.
4436 If SELECT-ARTICLES, only select those articles from GROUP."
4437   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4438          ;;!!! Dirty hack; should be removed.
4439          (gnus-summary-ignore-duplicates
4440           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4441               t
4442             gnus-summary-ignore-duplicates))
4443          (info (nth 2 entry))
4444          articles fetched-articles cached)
4445
4446     (unless (gnus-check-server
4447              (setq gnus-current-select-method
4448                    (gnus-find-method-for-group group)))
4449       (error "Couldn't open server"))
4450
4451     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4452         (gnus-activate-group group)     ; Or we can activate it...
4453         (progn                          ; Or we bug out.
4454           (when (equal major-mode 'gnus-summary-mode)
4455             (kill-buffer (current-buffer)))
4456           (error "Couldn't activate group %s: %s"
4457                  group (gnus-status-message group))))
4458
4459     (unless (gnus-request-group group t)
4460       (when (equal major-mode 'gnus-summary-mode)
4461         (kill-buffer (current-buffer)))
4462       (error "Couldn't request group %s: %s"
4463              group (gnus-status-message group)))
4464
4465     (setq gnus-newsgroup-name group)
4466     (setq gnus-newsgroup-unselected nil)
4467     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4468     (gnus-summary-setup-default-charset)
4469
4470     ;; Adjust and set lists of article marks.
4471     (when info
4472       (gnus-adjust-marked-articles info))
4473
4474     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4475     (setq cached
4476           (if (gnus-virtual-group-p group)
4477               gnus-newsgroup-cached
4478             (gnus-cache-articles-in-group group)))
4479
4480     (setq gnus-newsgroup-unreads
4481           (gnus-set-difference
4482            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4483            gnus-newsgroup-dormant))
4484
4485     (setq gnus-newsgroup-processable nil)
4486
4487     (gnus-update-read-articles group gnus-newsgroup-unreads)
4488
4489     (if (setq articles select-articles)
4490         (setq gnus-newsgroup-unselected
4491               (gnus-sorted-intersection
4492                gnus-newsgroup-unreads
4493                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4494       (setq articles (gnus-articles-to-read group read-all)))
4495
4496     (cond
4497      ((null articles)
4498       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4499       'quit)
4500      ((eq articles 0) nil)
4501      (t
4502       ;; Init the dependencies hash table.
4503       (setq gnus-newsgroup-dependencies
4504             (gnus-make-hashtable (length articles)))
4505       (gnus-set-global-variables)
4506       ;; Retrieve the headers and read them in.
4507       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4508
4509       ;; Suppress duplicates?
4510       (when gnus-suppress-duplicates
4511         (gnus-dup-suppress-articles))
4512
4513       ;; Set the initial limit.
4514       (setq gnus-newsgroup-limit (copy-sequence articles))
4515       ;; Remove canceled articles from the list of unread articles.
4516       (setq gnus-newsgroup-unreads
4517             (gnus-set-sorted-intersection
4518              gnus-newsgroup-unreads
4519              (setq fetched-articles
4520                    (mapcar (lambda (headers) (mail-header-number headers))
4521                            gnus-newsgroup-headers))))
4522       ;; Removed marked articles that do not exist.
4523       (gnus-update-missing-marks
4524        (gnus-sorted-complement fetched-articles articles))
4525
4526       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4527       (when cached
4528         (setq gnus-newsgroup-cached cached))
4529
4530       ;; We might want to build some more threads first.
4531       (when (and gnus-fetch-old-headers
4532                  (eq gnus-headers-retrieved-by 'nov))
4533         (if (eq gnus-fetch-old-headers 'invisible)
4534             (gnus-build-all-threads)
4535           (gnus-build-old-threads)))
4536       ;; Let the Gnus agent mark articles as read.
4537       (when gnus-agent
4538         (gnus-agent-get-undownloaded-list))
4539       ;; Remove list identifiers from subject
4540       (when gnus-list-identifiers
4541         (gnus-summary-remove-list-identifiers))
4542       ;; Check whether auto-expire is to be done in this group.
4543       (setq gnus-newsgroup-auto-expire
4544             (gnus-group-auto-expirable-p group))
4545       ;; Set up the article buffer now, if necessary.
4546       (unless gnus-single-article-buffer
4547         (gnus-article-setup-buffer))
4548       ;; First and last article in this newsgroup.
4549       (when gnus-newsgroup-headers
4550         (setq gnus-newsgroup-begin
4551               (mail-header-number (car gnus-newsgroup-headers))
4552               gnus-newsgroup-end
4553               (mail-header-number
4554                (gnus-last-element gnus-newsgroup-headers))))
4555       ;; GROUP is successfully selected.
4556       (or gnus-newsgroup-headers t)))))
4557
4558 (defun gnus-articles-to-read (group &optional read-all)
4559   "Find out what articles the user wants to read."
4560   (let* ((articles
4561           ;; Select all articles if `read-all' is non-nil, or if there
4562           ;; are no unread articles.
4563           (if (or read-all
4564                   (and (zerop (length gnus-newsgroup-marked))
4565                        (zerop (length gnus-newsgroup-unreads)))
4566                   (eq (gnus-group-find-parameter group 'display)
4567                       'all))
4568               (or
4569                (gnus-uncompress-range (gnus-active group))
4570                (gnus-cache-articles-in-group group))
4571             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4572                           (copy-sequence gnus-newsgroup-unreads))
4573                   '<)))
4574          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4575          (scored (length scored-list))
4576          (number (length articles))
4577          (marked (+ (length gnus-newsgroup-marked)
4578                     (length gnus-newsgroup-dormant)))
4579          (select
4580           (cond
4581            ((numberp read-all)
4582             read-all)
4583            (t
4584             (condition-case ()
4585                 (cond
4586                  ((and (or (<= scored marked) (= scored number))
4587                        (natnump gnus-large-newsgroup)
4588                        (> number gnus-large-newsgroup))
4589                   (let* ((cursor-in-echo-area nil)
4590                          (input (read-from-minibuffer
4591                                  (format
4592                                   "How many articles from %s (max %d): "
4593                                   (gnus-limit-string gnus-newsgroup-name 35)
4594                                   number)
4595                                  (cons (number-to-string gnus-large-newsgroup)
4596                                        0))))
4597                     (if (string-match "^[ \t]*$" input)
4598                         number
4599                       input)))
4600                  ((and (> scored marked) (< scored number)
4601                        (> (- scored number) 20))
4602                   (let ((input
4603                          (read-string
4604                           (format "%s %s (%d scored, %d total): "
4605                                   "How many articles from"
4606                                   group scored number))))
4607                     (if (string-match "^[ \t]*$" input)
4608                         number input)))
4609                  (t number))
4610               (quit
4611                (message "Quit getting the articles to read")
4612                nil))))))
4613     (setq select (if (stringp select) (string-to-number select) select))
4614     (if (or (null select) (zerop select))
4615         select
4616       (if (and (not (zerop scored)) (<= (abs select) scored))
4617           (progn
4618             (setq articles (sort scored-list '<))
4619             (setq number (length articles)))
4620         (setq articles (copy-sequence articles)))
4621
4622       (when (< (abs select) number)
4623         (if (< select 0)
4624             ;; Select the N oldest articles.
4625             (setcdr (nthcdr (1- (abs select)) articles) nil)
4626           ;; Select the N most recent articles.
4627           (setq articles (nthcdr (- number select) articles))))
4628       (setq gnus-newsgroup-unselected
4629             (gnus-sorted-intersection
4630              gnus-newsgroup-unreads
4631              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4632       (when gnus-alter-articles-to-read-function
4633         (setq gnus-newsgroup-unreads
4634               (sort
4635                (funcall gnus-alter-articles-to-read-function
4636                         gnus-newsgroup-name gnus-newsgroup-unreads)
4637                '<)))
4638       articles)))
4639
4640 (defun gnus-killed-articles (killed articles)
4641   (let (out)
4642     (while articles
4643       (when (inline (gnus-member-of-range (car articles) killed))
4644         (push (car articles) out))
4645       (setq articles (cdr articles)))
4646     out))
4647
4648 (defun gnus-uncompress-marks (marks)
4649   "Uncompress the mark ranges in MARKS."
4650   (let ((uncompressed '(score bookmark))
4651         out)
4652     (while marks
4653       (if (memq (caar marks) uncompressed)
4654           (push (car marks) out)
4655         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4656       (setq marks (cdr marks)))
4657     out))
4658
4659 (defun gnus-adjust-marked-articles (info)
4660   "Set all article lists and remove all marks that are no longer valid."
4661   (let* ((marked-lists (gnus-info-marks info))
4662          (active (gnus-active (gnus-info-group info)))
4663          (min (car active))
4664          (max (cdr active))
4665          (types gnus-article-mark-lists)
4666          (uncompressed '(score bookmark killed))
4667          marks var articles article mark)
4668
4669     (while marked-lists
4670       (setq marks (pop marked-lists))
4671       (set (setq var (intern (format "gnus-newsgroup-%s"
4672                                      (car (rassq (setq mark (car marks))
4673                                                  types)))))
4674            (if (memq (car marks) uncompressed) (cdr marks)
4675              (gnus-uncompress-range (cdr marks))))
4676
4677       (setq articles (symbol-value var))
4678
4679       ;; All articles have to be subsets of the active articles.
4680       (cond
4681        ;; Adjust "simple" lists.
4682        ((memq mark '(tick dormant expire reply save))
4683         (while articles
4684           (when (or (< (setq article (pop articles)) min) (> article max))
4685             (set var (delq article (symbol-value var))))))
4686        ;; Adjust assocs.
4687        ((memq mark uncompressed)
4688         (when (not (listp (cdr (symbol-value var))))
4689           (set var (list (symbol-value var))))
4690         (when (not (listp (cdr articles)))
4691           (setq articles (list articles)))
4692         (while articles
4693           (when (or (not (consp (setq article (pop articles))))
4694                     (< (car article) min)
4695                     (> (car article) max))
4696             (set var (delq article (symbol-value var))))))))))
4697
4698 (defun gnus-update-missing-marks (missing)
4699   "Go through the list of MISSING articles and remove them from the mark lists."
4700   (when missing
4701     (let ((types gnus-article-mark-lists)
4702           var m)
4703       ;; Go through all types.
4704       (while types
4705         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4706         (when (symbol-value var)
4707           ;; This list has articles.  So we delete all missing articles
4708           ;; from it.
4709           (setq m missing)
4710           (while m
4711             (set var (delq (pop m) (symbol-value var)))))))))
4712
4713 (defun gnus-update-marks ()
4714   "Enter the various lists of marked articles into the newsgroup info list."
4715   (let ((types gnus-article-mark-lists)
4716         (info (gnus-get-info gnus-newsgroup-name))
4717         (uncompressed '(score bookmark killed))
4718         type list newmarked symbol delta-marks)
4719     (when info
4720       ;; Add all marks lists to the list of marks lists.
4721       (while (setq type (pop types))
4722         (setq list (symbol-value
4723                     (setq symbol
4724                           (intern (format "gnus-newsgroup-%s"
4725                                           (car type))))))
4726
4727         (when list
4728           ;; Get rid of the entries of the articles that have the
4729           ;; default score.
4730           (when (and (eq (cdr type) 'score)
4731                      gnus-save-score
4732                      list)
4733             (let* ((arts list)
4734                    (prev (cons nil list))
4735                    (all prev))
4736               (while arts
4737                 (if (or (not (consp (car arts)))
4738                         (= (cdar arts) gnus-summary-default-score))
4739                     (setcdr prev (cdr arts))
4740                   (setq prev arts))
4741                 (setq arts (cdr arts)))
4742               (setq list (cdr all)))))
4743
4744         (unless (memq (cdr type) uncompressed)
4745           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4746
4747         (when (gnus-check-backend-function
4748                'request-set-mark gnus-newsgroup-name)
4749           ;; propagate flags to server, with the following exceptions:
4750           ;; uncompressed:s are not proper flags (they are cons cells)
4751           ;; cache is a internal gnus flag
4752           ;; download are local to one gnus installation (well)
4753           ;; unsend are for nndraft groups only
4754           ;; xxx: generality of this?  this suits nnimap anyway
4755           (unless (memq (cdr type) (append '(cache download unsend)
4756                                            uncompressed))
4757             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4758                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4759                    (add (gnus-remove-from-range
4760                          (gnus-copy-sequence list) old)))
4761               (when add
4762                 (push (list add 'add (list (cdr type))) delta-marks))
4763               (when del
4764                 (push (list del 'del (list (cdr type))) delta-marks)))))
4765
4766         (when list
4767           (push (cons (cdr type) list) newmarked)))
4768
4769       (when delta-marks
4770         (unless (gnus-check-group gnus-newsgroup-name)
4771           (error "Can't open server for %s" gnus-newsgroup-name))
4772         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4773
4774       ;; Enter these new marks into the info of the group.
4775       (if (nthcdr 3 info)
4776           (setcar (nthcdr 3 info) newmarked)
4777         ;; Add the marks lists to the end of the info.
4778         (when newmarked
4779           (setcdr (nthcdr 2 info) (list newmarked))))
4780
4781       ;; Cut off the end of the info if there's nothing else there.
4782       (let ((i 5))
4783         (while (and (> i 2)
4784                     (not (nth i info)))
4785           (when (nthcdr (decf i) info)
4786             (setcdr (nthcdr i info) nil)))))))
4787
4788 (defun gnus-set-mode-line (where)
4789   "Set the mode line of the article or summary buffers.
4790 If WHERE is `summary', the summary mode line format will be used."
4791   ;; Is this mode line one we keep updated?
4792   (when (and (memq where gnus-updated-mode-lines)
4793              (symbol-value
4794               (intern (format "gnus-%s-mode-line-format-spec" where))))
4795     (let (mode-string)
4796       (save-excursion
4797         ;; We evaluate this in the summary buffer since these
4798         ;; variables are buffer-local to that buffer.
4799         (set-buffer gnus-summary-buffer)
4800         ;; We bind all these variables that are used in the `eval' form
4801         ;; below.
4802         (let* ((mformat (symbol-value
4803                          (intern
4804                           (format "gnus-%s-mode-line-format-spec" where))))
4805                (gnus-tmp-group-name (gnus-group-decoded-name 
4806                                      gnus-newsgroup-name))
4807                (gnus-tmp-article-number (or gnus-current-article 0))
4808                (gnus-tmp-unread gnus-newsgroup-unreads)
4809                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4810                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4811                (gnus-tmp-unread-and-unselected
4812                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4813                             (zerop gnus-tmp-unselected))
4814                        "")
4815                       ((zerop gnus-tmp-unselected)
4816                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4817                       (t (format "{%d(+%d) more}"
4818                                  gnus-tmp-unread-and-unticked
4819                                  gnus-tmp-unselected))))
4820                (gnus-tmp-subject
4821                 (if (and gnus-current-headers
4822                          (vectorp gnus-current-headers))
4823                     (gnus-mode-string-quote
4824                      (mail-header-subject gnus-current-headers))
4825                   ""))
4826                bufname-length max-len
4827                gnus-tmp-header);; passed as argument to any user-format-funcs
4828           (setq mode-string (eval mformat))
4829           (setq bufname-length (if (string-match "%b" mode-string)
4830                                    (- (length
4831                                        (buffer-name
4832                                         (if (eq where 'summary)
4833                                             nil
4834                                           (get-buffer gnus-article-buffer))))
4835                                       2)
4836                                  0))
4837           (setq max-len (max 4 (if gnus-mode-non-string-length
4838                                    (- (window-width)
4839                                       gnus-mode-non-string-length
4840                                       bufname-length)
4841                                  (length mode-string))))
4842           ;; We might have to chop a bit of the string off...
4843           (when (> (length mode-string) max-len)
4844             (setq mode-string
4845                   (concat (gnus-truncate-string mode-string (- max-len 3))
4846                           "...")))
4847           ;; Pad the mode string a bit.
4848           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4849       ;; Update the mode line.
4850       (setq mode-line-buffer-identification
4851             (gnus-mode-line-buffer-identification (list mode-string)))
4852       (set-buffer-modified-p t))))
4853
4854 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4855   "Go through the HEADERS list and add all Xrefs to a hash table.
4856 The resulting hash table is returned, or nil if no Xrefs were found."
4857   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4858          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4859          (xref-hashtb (gnus-make-hashtable))
4860          start group entry number xrefs header)
4861     (while headers
4862       (setq header (pop headers))
4863       (when (and (setq xrefs (mail-header-xref header))
4864                  (not (memq (setq number (mail-header-number header))
4865                             unreads)))
4866         (setq start 0)
4867         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4868           (setq start (match-end 0))
4869           (setq group (if prefix
4870                           (concat prefix (substring xrefs (match-beginning 1)
4871                                                     (match-end 1)))
4872                         (substring xrefs (match-beginning 1) (match-end 1))))
4873           (setq number
4874                 (string-to-int (substring xrefs (match-beginning 2)
4875                                           (match-end 2))))
4876           (if (setq entry (gnus-gethash group xref-hashtb))
4877               (setcdr entry (cons number (cdr entry)))
4878             (gnus-sethash group (cons number nil) xref-hashtb)))))
4879     (and start xref-hashtb)))
4880
4881 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4882   "Look through all the headers and mark the Xrefs as read."
4883   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4884         name entry info xref-hashtb idlist method nth4)
4885     (save-excursion
4886       (set-buffer gnus-group-buffer)
4887       (when (setq xref-hashtb
4888                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4889         (mapatoms
4890          (lambda (group)
4891            (unless (string= from-newsgroup (setq name (symbol-name group)))
4892              (setq idlist (symbol-value group))
4893              ;; Dead groups are not updated.
4894              (and (prog1
4895                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4896                             info (nth 2 entry))
4897                     (when (stringp (setq nth4 (gnus-info-method info)))
4898                       (setq nth4 (gnus-server-to-method nth4))))
4899                   ;; Only do the xrefs if the group has the same
4900                   ;; select method as the group we have just read.
4901                   (or (gnus-methods-equal-p
4902                        nth4 (gnus-find-method-for-group from-newsgroup))
4903                       virtual
4904                       (equal nth4 (setq method (gnus-find-method-for-group
4905                                                 from-newsgroup)))
4906                       (and (equal (car nth4) (car method))
4907                            (equal (nth 1 nth4) (nth 1 method))))
4908                   gnus-use-cross-reference
4909                   (or (not (eq gnus-use-cross-reference t))
4910                       virtual
4911                       ;; Only do cross-references on subscribed
4912                       ;; groups, if that is what is wanted.
4913                       (<= (gnus-info-level info) gnus-level-subscribed))
4914                   (gnus-group-make-articles-read name idlist))))
4915          xref-hashtb)))))
4916
4917 (defun gnus-compute-read-articles (group articles)
4918   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4919          (info (nth 2 entry))
4920          (active (gnus-active group))
4921          ninfo)
4922     (when entry
4923       ;; First peel off all invalid article numbers.
4924       (when active
4925         (let ((ids articles)
4926               id first)
4927           (while (setq id (pop ids))
4928             (when (and first (> id (cdr active)))
4929               ;; We'll end up in this situation in one particular
4930               ;; obscure situation.  If you re-scan a group and get
4931               ;; a new article that is cross-posted to a different
4932               ;; group that has not been re-scanned, you might get
4933               ;; crossposted article that has a higher number than
4934               ;; Gnus believes possible.  So we re-activate this
4935               ;; group as well.  This might mean doing the
4936               ;; crossposting thingy will *increase* the number
4937               ;; of articles in some groups.  Tsk, tsk.
4938               (setq active (or (gnus-activate-group group) active)))
4939             (when (or (> id (cdr active))
4940                       (< id (car active)))
4941               (setq articles (delq id articles))))))
4942       ;; If the read list is nil, we init it.
4943       (if (and active
4944                (null (gnus-info-read info))
4945                (> (car active) 1))
4946           (setq ninfo (cons 1 (1- (car active))))
4947         (setq ninfo (gnus-info-read info)))
4948       ;; Then we add the read articles to the range.
4949       (gnus-add-to-range
4950        ninfo (setq articles (sort articles '<))))))
4951
4952 (defun gnus-group-make-articles-read (group articles)
4953   "Update the info of GROUP to say that ARTICLES are read."
4954   (let* ((num 0)
4955          (entry (gnus-gethash group gnus-newsrc-hashtb))
4956          (info (nth 2 entry))
4957          (active (gnus-active group))
4958          range)
4959     (when entry
4960       (setq range (gnus-compute-read-articles group articles))
4961       (save-excursion
4962         (set-buffer gnus-group-buffer)
4963         (gnus-undo-register
4964           `(progn
4965              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4966              (gnus-info-set-read ',info ',(gnus-info-read info))
4967              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4968              (gnus-group-update-group ,group t))))
4969       ;; Add the read articles to the range.
4970       (gnus-info-set-read info range)
4971       ;; Then we have to re-compute how many unread
4972       ;; articles there are in this group.
4973       (when active
4974         (cond
4975          ((not range)
4976           (setq num (- (1+ (cdr active)) (car active))))
4977          ((not (listp (cdr range)))
4978           (setq num (- (cdr active) (- (1+ (cdr range))
4979                                        (car range)))))
4980          (t
4981           (while range
4982             (if (numberp (car range))
4983                 (setq num (1+ num))
4984               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4985             (setq range (cdr range)))
4986           (setq num (- (cdr active) num))))
4987         ;; Update the number of unread articles.
4988         (setcar entry num)
4989         ;; Update the group buffer.
4990         (gnus-group-update-group group t)))))
4991
4992 (defvar gnus-newsgroup-none-id 0)
4993
4994 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4995   (let ((cur nntp-server-buffer)
4996         (dependencies
4997          (or dependencies
4998              (save-excursion (set-buffer gnus-summary-buffer)
4999                              gnus-newsgroup-dependencies)))
5000         headers id end ref
5001         (mail-parse-charset gnus-newsgroup-charset)
5002         (mail-parse-ignored-charsets
5003          (save-excursion (condition-case nil
5004                              (set-buffer gnus-summary-buffer)
5005                            (error))
5006                          gnus-newsgroup-ignored-charsets)))
5007     (save-excursion
5008       (set-buffer nntp-server-buffer)
5009       ;; Translate all TAB characters into SPACE characters.
5010       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5011       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5012       (gnus-run-hooks 'gnus-parse-headers-hook)
5013       (let ((case-fold-search t)
5014             in-reply-to header p lines chars ctype)
5015         (goto-char (point-min))
5016         ;; Search to the beginning of the next header.  Error messages
5017         ;; do not begin with 2 or 3.
5018         (while (re-search-forward "^[23][0-9]+ " nil t)
5019           (setq id nil
5020                 ref nil)
5021           ;; This implementation of this function, with nine
5022           ;; search-forwards instead of the one re-search-forward and
5023           ;; a case (which basically was the old function) is actually
5024           ;; about twice as fast, even though it looks messier.  You
5025           ;; can't have everything, I guess.  Speed and elegance
5026           ;; doesn't always go hand in hand.
5027           (setq
5028            header
5029            (make-full-mail-header
5030             ;; Number.
5031             (prog1
5032                 (read cur)
5033               (end-of-line)
5034               (setq p (point))
5035               (narrow-to-region (point)
5036                                 (or (and (search-forward "\n.\n" nil t)
5037                                          (- (point) 2))
5038                                     (point))))
5039             ;; Subject.
5040             (progn
5041               (goto-char p)
5042               (if (search-forward "\nsubject: " nil t)
5043                   (nnheader-header-value)
5044                 "(none)"))
5045             ;; From.
5046             (progn
5047               (goto-char p)
5048               (if (or (search-forward "\nfrom: " nil t)
5049                       (search-forward "\nfrom:" nil t))
5050                   (nnheader-header-value)
5051                 "(nobody)"))
5052             ;; Date.
5053             (progn
5054               (goto-char p)
5055               (if (search-forward "\ndate: " nil t)
5056                   (nnheader-header-value)
5057                 ""))
5058             ;; Message-ID.
5059             (progn
5060               (goto-char p)
5061               (setq id (if (re-search-forward
5062                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5063                            ;; We do it this way to make sure the Message-ID
5064                            ;; is (somewhat) syntactically valid.
5065                            (buffer-substring (match-beginning 1)
5066                                              (match-end 1))
5067                          ;; If there was no message-id, we just fake one
5068                          ;; to make subsequent routines simpler.
5069                          (nnheader-generate-fake-message-id))))
5070             ;; References.
5071             (progn
5072               (goto-char p)
5073               (if (search-forward "\nreferences: " nil t)
5074                   (progn
5075                     (setq end (point))
5076                     (prog1
5077                         (nnheader-header-value)
5078                       (setq ref
5079                             (buffer-substring
5080                              (progn
5081                                ;; (end-of-line)
5082                                (search-backward ">" end t)
5083                                (1+ (point)))
5084                              (progn
5085                                (search-backward "<" end t)
5086                                (point))))))
5087                 ;; Get the references from the in-reply-to header if there
5088                 ;; were no references and the in-reply-to header looks
5089                 ;; promising.
5090                 (if (and (search-forward "\nin-reply-to: " nil t)
5091                          (setq in-reply-to (nnheader-header-value))
5092                          (string-match "<[^>]+>" in-reply-to))
5093                     (let (ref2)
5094                       (setq ref (substring in-reply-to (match-beginning 0)
5095                                            (match-end 0)))
5096                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5097                         (setq ref2 (substring in-reply-to (match-beginning 0)
5098                                               (match-end 0)))
5099                         (when (> (length ref2) (length ref))
5100                           (setq ref ref2)))
5101                       ref)
5102                   (setq ref nil))))
5103             ;; Chars.
5104             (progn
5105               (goto-char p)
5106               (if (search-forward "\nchars: " nil t)
5107                   (if (numberp (setq chars (ignore-errors (read cur))))
5108                       chars -1)
5109                 -1))
5110             ;; Lines.
5111             (progn
5112               (goto-char p)
5113               (if (search-forward "\nlines: " nil t)
5114                   (if (numberp (setq lines (ignore-errors (read cur))))
5115                       lines -1)
5116                 -1))
5117             ;; Xref.
5118             (progn
5119               (goto-char p)
5120               (and (search-forward "\nxref: " nil t)
5121                    (nnheader-header-value)))
5122             ;; Extra.
5123             (when gnus-extra-headers
5124               (let ((extra gnus-extra-headers)
5125                     out)
5126                 (while extra
5127                   (goto-char p)
5128                   (when (search-forward
5129                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5130                     (push (cons (car extra) (nnheader-header-value)) out))
5131                   (pop extra))
5132                 out))))
5133           (goto-char p)
5134           (if (and (search-forward "\ncontent-type: " nil t)
5135                    (setq ctype (nnheader-header-value)))
5136               (mime-entity-set-content-type-internal
5137                header (mime-parse-Content-Type ctype)))
5138           (when (equal id ref)
5139             (setq ref nil))
5140
5141           (when gnus-alter-header-function
5142             (funcall gnus-alter-header-function header)
5143             (setq id (mail-header-id header)
5144                   ref (gnus-parent-id (mail-header-references header))))
5145
5146           (when (setq header
5147                       (gnus-dependencies-add-header
5148                        header dependencies force-new))
5149             (push header headers))
5150           (goto-char (point-max))
5151           (widen))
5152         (nreverse headers)))))
5153
5154 ;; Goes through the xover lines and returns a list of vectors
5155 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5156                                                   force-new dependencies
5157                                                   group also-fetch-heads)
5158   "Parse the news overview data in the server buffer.
5159 Return a list of headers that match SEQUENCE (see
5160 `nntp-retrieve-headers')."
5161   ;; Get the Xref when the users reads the articles since most/some
5162   ;; NNTP servers do not include Xrefs when using XOVER.
5163   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5164   (let ((mail-parse-charset gnus-newsgroup-charset)
5165         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5166         (cur nntp-server-buffer)
5167         (dependencies (or dependencies gnus-newsgroup-dependencies))
5168         (allp (cond
5169                ((eq gnus-read-all-available-headers t)
5170                 t)
5171                ((stringp gnus-read-all-available-headers)
5172                 (string-match gnus-read-all-available-headers group))
5173                (t
5174                 nil)))
5175         number headers header)
5176     (save-excursion
5177       (set-buffer nntp-server-buffer)
5178       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5179       ;; Allow the user to mangle the headers before parsing them.
5180       (gnus-run-hooks 'gnus-parse-headers-hook)
5181       (goto-char (point-min))
5182       (while (not (eobp))
5183         (condition-case ()
5184             (while (and (or sequence allp)
5185                         (not (eobp)))
5186               (setq number (read cur))
5187               (when (not allp)
5188                 (while (and sequence
5189                             (< (car sequence) number))
5190                   (setq sequence (cdr sequence))))
5191               (when (and (or allp
5192                              (and sequence
5193                                   (eq number (car sequence))))
5194                          (progn
5195                            (setq sequence (cdr sequence))
5196                            (setq header (inline
5197                                           (gnus-nov-parse-line
5198                                            number dependencies force-new)))))
5199                 (push header headers))
5200               (forward-line 1))
5201           (error
5202            (gnus-error 4 "Strange nov line (%d)"
5203                        (count-lines (point-min) (point)))))
5204         (forward-line 1))
5205       ;; A common bug in inn is that if you have posted an article and
5206       ;; then retrieves the active file, it will answer correctly --
5207       ;; the new article is included.  However, a NOV entry for the
5208       ;; article may not have been generated yet, so this may fail.
5209       ;; We work around this problem by retrieving the last few
5210       ;; headers using HEAD.
5211       (if (or (not also-fetch-heads)
5212               (not sequence))
5213           ;; We (probably) got all the headers.
5214           (nreverse headers)
5215         (let ((gnus-nov-is-evil t))
5216           (nconc
5217            (nreverse headers)
5218            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5219            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5220                (when (gnus-retrieve-headers sequence group)
5221                  (gnus-get-newsgroup-headers))
5222              (gnus-retrieve-parsed-headers sequence group))))))))
5223
5224 (defun gnus-article-get-xrefs ()
5225   "Fill in the Xref value in `gnus-current-headers', if necessary.
5226 This is meant to be called in `gnus-article-internal-prepare-hook'."
5227   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5228                                  gnus-current-headers)))
5229     (or (not gnus-use-cross-reference)
5230         (not headers)
5231         (and (mail-header-xref headers)
5232              (not (string= (mail-header-xref headers) "")))
5233         (let ((case-fold-search t)
5234               xref)
5235           (save-restriction
5236             (nnheader-narrow-to-headers)
5237             (goto-char (point-min))
5238             (when (or (and (not (eobp))
5239                            (eq (downcase (char-after)) ?x)
5240                            (looking-at "Xref:"))
5241                       (search-forward "\nXref:" nil t))
5242               (goto-char (1+ (match-end 0)))
5243               (setq xref (buffer-substring (point)
5244                                            (progn (end-of-line) (point))))
5245               (mail-header-set-xref headers xref)))))))
5246
5247 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5248   "Find article ID and insert the summary line for that article.
5249 OLD-HEADER can either be a header or a line number to insert
5250 the subject line on."
5251   (let* ((line (and (numberp old-header) old-header))
5252          (old-header (and (vectorp old-header) old-header))
5253          (header (cond ((and old-header use-old-header)
5254                         old-header)
5255                        ((and (numberp id)
5256                              (gnus-number-to-header id))
5257                         (gnus-number-to-header id))
5258                        (t
5259                         (gnus-read-header id))))
5260          (number (and (numberp id) id))
5261          d)
5262     (when header
5263       ;; Rebuild the thread that this article is part of and go to the
5264       ;; article we have fetched.
5265       (when (and (not gnus-show-threads)
5266                  old-header)
5267         (when (and number
5268                    (setq d (gnus-data-find (mail-header-number old-header))))
5269           (goto-char (gnus-data-pos d))
5270           (gnus-data-remove
5271            number
5272            (- (gnus-point-at-bol)
5273               (prog1
5274                   (1+ (gnus-point-at-eol))
5275                 (gnus-delete-line))))))
5276       (when old-header
5277         (mail-header-set-number header (mail-header-number old-header)))
5278       (setq gnus-newsgroup-sparse
5279             (delq (setq number (mail-header-number header))
5280                   gnus-newsgroup-sparse))
5281       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5282       (push number gnus-newsgroup-limit)
5283       (gnus-rebuild-thread (mail-header-id header) line)
5284       (gnus-summary-goto-subject number nil t))
5285     (when (and (numberp number)
5286                (> number 0))
5287       ;; We have to update the boundaries even if we can't fetch the
5288       ;; article if ID is a number -- so that the next `P' or `N'
5289       ;; command will fetch the previous (or next) article even
5290       ;; if the one we tried to fetch this time has been canceled.
5291       (when (> number gnus-newsgroup-end)
5292         (setq gnus-newsgroup-end number))
5293       (when (< number gnus-newsgroup-begin)
5294         (setq gnus-newsgroup-begin number))
5295       (setq gnus-newsgroup-unselected
5296             (delq number gnus-newsgroup-unselected)))
5297     ;; Report back a success?
5298     (and header (mail-header-number header))))
5299
5300 ;;; Process/prefix in the summary buffer
5301
5302 (defun gnus-summary-work-articles (n)
5303   "Return a list of articles to be worked upon.
5304 The prefix argument, the list of process marked articles, and the
5305 current article will be taken into consideration."
5306   (save-excursion
5307     (set-buffer gnus-summary-buffer)
5308     (cond
5309      (n
5310       ;; A numerical prefix has been given.
5311       (setq n (prefix-numeric-value n))
5312       (let ((backward (< n 0))
5313             (n (abs (prefix-numeric-value n)))
5314             articles article)
5315         (save-excursion
5316           (while
5317               (and (> n 0)
5318                    (push (setq article (gnus-summary-article-number))
5319                          articles)
5320                    (if backward
5321                        (gnus-summary-find-prev nil article)
5322                      (gnus-summary-find-next nil article)))
5323             (decf n)))
5324         (nreverse articles)))
5325      ((and (gnus-region-active-p) (mark))
5326       (message "region active")
5327       ;; Work on the region between point and mark.
5328       (let ((max (max (point) (mark)))
5329             articles article)
5330         (save-excursion
5331           (goto-char (min (point) (mark)))
5332           (while
5333               (and
5334                (push (setq article (gnus-summary-article-number)) articles)
5335                (gnus-summary-find-next nil article)
5336                (< (point) max)))
5337           (nreverse articles))))
5338      (gnus-newsgroup-processable
5339       ;; There are process-marked articles present.
5340       ;; Save current state.
5341       (gnus-summary-save-process-mark)
5342       ;; Return the list.
5343       (reverse gnus-newsgroup-processable))
5344      (t
5345       ;; Just return the current article.
5346       (list (gnus-summary-article-number))))))
5347
5348 (defmacro gnus-summary-iterate (arg &rest forms)
5349   "Iterate over the process/prefixed articles and do FORMS.
5350 ARG is the interactive prefix given to the command.  FORMS will be
5351 executed with point over the summary line of the articles."
5352   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5353     `(let ((,articles (gnus-summary-work-articles ,arg)))
5354        (while ,articles
5355          (gnus-summary-goto-subject (car ,articles))
5356          ,@forms
5357          (pop ,articles)))))
5358
5359 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5360 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5361
5362 (defun gnus-summary-save-process-mark ()
5363   "Push the current set of process marked articles on the stack."
5364   (interactive)
5365   (push (copy-sequence gnus-newsgroup-processable)
5366         gnus-newsgroup-process-stack))
5367
5368 (defun gnus-summary-kill-process-mark ()
5369   "Push the current set of process marked articles on the stack and unmark."
5370   (interactive)
5371   (gnus-summary-save-process-mark)
5372   (gnus-summary-unmark-all-processable))
5373
5374 (defun gnus-summary-yank-process-mark ()
5375   "Pop the last process mark state off the stack and restore it."
5376   (interactive)
5377   (unless gnus-newsgroup-process-stack
5378     (error "Empty mark stack"))
5379   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5380
5381 (defun gnus-summary-process-mark-set (set)
5382   "Make SET into the current process marked articles."
5383   (gnus-summary-unmark-all-processable)
5384   (while set
5385     (gnus-summary-set-process-mark (pop set))))
5386
5387 ;;; Searching and stuff
5388
5389 (defun gnus-summary-search-group (&optional backward use-level)
5390   "Search for next unread newsgroup.
5391 If optional argument BACKWARD is non-nil, search backward instead."
5392   (save-excursion
5393     (set-buffer gnus-group-buffer)
5394     (when (gnus-group-search-forward
5395            backward nil (if use-level (gnus-group-group-level) nil))
5396       (gnus-group-group-name))))
5397
5398 (defun gnus-summary-best-group (&optional exclude-group)
5399   "Find the name of the best unread group.
5400 If EXCLUDE-GROUP, do not go to this group."
5401   (save-excursion
5402     (set-buffer gnus-group-buffer)
5403     (save-excursion
5404       (gnus-group-best-unread-group exclude-group))))
5405
5406 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5407   (if backward (gnus-summary-find-prev)
5408     (let* ((dummy (gnus-summary-article-intangible-p))
5409            (article (or article (gnus-summary-article-number)))
5410            (arts (gnus-data-find-list article))
5411            result)
5412       (when (and (not dummy)
5413                  (or (not gnus-summary-check-current)
5414                      (not unread)
5415                      (not (gnus-data-unread-p (car arts)))))
5416         (setq arts (cdr arts)))
5417       (when (setq result
5418                   (if unread
5419                       (progn
5420                         (while arts
5421                           (when (or (and undownloaded
5422                                          (eq gnus-undownloaded-mark
5423                                              (gnus-data-mark (car arts))))
5424                                     (gnus-data-unread-p (car arts)))
5425                             (setq result (car arts)
5426                                   arts nil))
5427                           (setq arts (cdr arts)))
5428                         result)
5429                     (car arts)))
5430         (goto-char (gnus-data-pos result))
5431         (gnus-data-number result)))))
5432
5433 (defun gnus-summary-find-prev (&optional unread article)
5434   (let* ((eobp (eobp))
5435          (article (or article (gnus-summary-article-number)))
5436          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5437          result)
5438     (when (and (not eobp)
5439                (or (not gnus-summary-check-current)
5440                    (not unread)
5441                    (not (gnus-data-unread-p (car arts)))))
5442       (setq arts (cdr arts)))
5443     (when (setq result
5444                 (if unread
5445                     (progn
5446                       (while arts
5447                         (when (gnus-data-unread-p (car arts))
5448                           (setq result (car arts)
5449                                 arts nil))
5450                         (setq arts (cdr arts)))
5451                       result)
5452                   (car arts)))
5453       (goto-char (gnus-data-pos result))
5454       (gnus-data-number result))))
5455
5456 (defun gnus-summary-find-subject (subject &optional unread backward article)
5457   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5458          (article (or article (gnus-summary-article-number)))
5459          (articles (gnus-data-list backward))
5460          (arts (gnus-data-find-list article articles))
5461          result)
5462     (when (or (not gnus-summary-check-current)
5463               (not unread)
5464               (not (gnus-data-unread-p (car arts))))
5465       (setq arts (cdr arts)))
5466     (while arts
5467       (and (or (not unread)
5468                (gnus-data-unread-p (car arts)))
5469            (vectorp (gnus-data-header (car arts)))
5470            (gnus-subject-equal
5471             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5472            (setq result (car arts)
5473                  arts nil))
5474       (setq arts (cdr arts)))
5475     (and result
5476          (goto-char (gnus-data-pos result))
5477          (gnus-data-number result))))
5478
5479 (defun gnus-summary-search-forward (&optional unread subject backward)
5480   "Search forward for an article.
5481 If UNREAD, look for unread articles.  If SUBJECT, look for
5482 articles with that subject.  If BACKWARD, search backward instead."
5483   (cond (subject (gnus-summary-find-subject subject unread backward))
5484         (backward (gnus-summary-find-prev unread))
5485         (t (gnus-summary-find-next unread))))
5486
5487 (defun gnus-recenter (&optional n)
5488   "Center point in window and redisplay frame.
5489 Also do horizontal recentering."
5490   (interactive "P")
5491   (when (and gnus-auto-center-summary
5492              (not (eq gnus-auto-center-summary 'vertical)))
5493     (gnus-horizontal-recenter))
5494   (recenter n))
5495
5496 (defun gnus-summary-recenter ()
5497   "Center point in the summary window.
5498 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5499 displayed, no centering will be performed."
5500   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5501   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5502   (interactive)
5503   (let* ((top (cond ((< (window-height) 4) 0)
5504                     ((< (window-height) 7) 1)
5505                     (t (if (numberp gnus-auto-center-summary)
5506                            gnus-auto-center-summary
5507                          2))))
5508          (height (1- (window-height)))
5509          (bottom (save-excursion (goto-char (point-max))
5510                                  (forward-line (- height))
5511                                  (point)))
5512          (window (get-buffer-window (current-buffer))))
5513     ;; The user has to want it.
5514     (when gnus-auto-center-summary
5515       (when (get-buffer-window gnus-article-buffer)
5516         ;; Only do recentering when the article buffer is displayed,
5517         ;; Set the window start to either `bottom', which is the biggest
5518         ;; possible valid number, or the second line from the top,
5519         ;; whichever is the least.
5520         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5521           (if (> bottom top-pos)
5522               ;; Keep the second line from the top visible
5523               (set-window-start window top-pos t)
5524             ;; Try to keep the bottom line visible; if it's partially
5525             ;; obscured, either scroll one more line to make it fully
5526             ;; visible, or revert to using TOP-POS.
5527             (save-excursion
5528               (goto-char (point-max))
5529               (forward-line -1)
5530               (let ((last-line-start (point)))
5531                 (goto-char bottom)
5532                 (set-window-start window (point) t)
5533                 (when (not (pos-visible-in-window-p last-line-start window))
5534                   (forward-line 1)
5535                   (set-window-start window (min (point) top-pos) t)))))))
5536       ;; Do horizontal recentering while we're at it.
5537       (when (and (get-buffer-window (current-buffer) t)
5538                  (not (eq gnus-auto-center-summary 'vertical)))
5539         (let ((selected (selected-window)))
5540           (select-window (get-buffer-window (current-buffer) t))
5541           (gnus-summary-position-point)
5542           (gnus-horizontal-recenter)
5543           (select-window selected))))))
5544
5545 (defun gnus-summary-jump-to-group (newsgroup)
5546   "Move point to NEWSGROUP in group mode buffer."
5547   ;; Keep update point of group mode buffer if visible.
5548   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5549       (save-window-excursion
5550         ;; Take care of tree window mode.
5551         (when (get-buffer-window gnus-group-buffer)
5552           (pop-to-buffer gnus-group-buffer))
5553         (gnus-group-jump-to-group newsgroup))
5554     (save-excursion
5555       ;; Take care of tree window mode.
5556       (if (get-buffer-window gnus-group-buffer)
5557           (pop-to-buffer gnus-group-buffer)
5558         (set-buffer gnus-group-buffer))
5559       (gnus-group-jump-to-group newsgroup))))
5560
5561 ;; This function returns a list of article numbers based on the
5562 ;; difference between the ranges of read articles in this group and
5563 ;; the range of active articles.
5564 (defun gnus-list-of-unread-articles (group)
5565   (let* ((read (gnus-info-read (gnus-get-info group)))
5566          (active (or (gnus-active group) (gnus-activate-group group)))
5567          (last (cdr active))
5568          first nlast unread)
5569     ;; If none are read, then all are unread.
5570     (if (not read)
5571         (setq first (car active))
5572       ;; If the range of read articles is a single range, then the
5573       ;; first unread article is the article after the last read
5574       ;; article.  Sounds logical, doesn't it?
5575       (if (and (not (listp (cdr read)))
5576                (or (< (car read) (car active))
5577                    (progn (setq read (list read))
5578                           nil)))
5579           (setq first (max (car active) (1+ (cdr read))))
5580         ;; `read' is a list of ranges.
5581         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5582                                   (caar read)))
5583                   1)
5584           (setq first (car active)))
5585         (while read
5586           (when first
5587             (while (< first nlast)
5588               (push first unread)
5589               (setq first (1+ first))))
5590           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5591           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5592           (setq read (cdr read)))))
5593     ;; And add the last unread articles.
5594     (while (<= first last)
5595       (push first unread)
5596       (setq first (1+ first)))
5597     ;; Return the list of unread articles.
5598     (delq 0 (nreverse unread))))
5599
5600 (defun gnus-list-of-read-articles (group)
5601   "Return a list of unread, unticked and non-dormant articles."
5602   (let* ((info (gnus-get-info group))
5603          (marked (gnus-info-marks info))
5604          (active (gnus-active group)))
5605     (and info active
5606          (gnus-set-difference
5607           (gnus-sorted-complement
5608            (gnus-uncompress-range active)
5609            (gnus-list-of-unread-articles group))
5610           (append
5611            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5612            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5613
5614 ;; Various summary commands
5615
5616 (defun gnus-summary-select-article-buffer ()
5617   "Reconfigure windows to show article buffer."
5618   (interactive)
5619   (if (not (gnus-buffer-live-p gnus-article-buffer))
5620       (error "There is no article buffer for this summary buffer")
5621     (gnus-configure-windows 'article)
5622     (select-window (get-buffer-window gnus-article-buffer))))
5623
5624 (defun gnus-summary-universal-argument (arg)
5625   "Perform any operation on all articles that are process/prefixed."
5626   (interactive "P")
5627   (let ((articles (gnus-summary-work-articles arg))
5628         func article)
5629     (if (eq
5630          (setq
5631           func
5632           (key-binding
5633            (read-key-sequence
5634             (substitute-command-keys
5635              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5636          'undefined)
5637         (gnus-error 1 "Undefined key")
5638       (save-excursion
5639         (while articles
5640           (gnus-summary-goto-subject (setq article (pop articles)))
5641           (let (gnus-newsgroup-processable)
5642             (command-execute func))
5643           (gnus-summary-remove-process-mark article)))))
5644   (gnus-summary-position-point))
5645
5646 (defun gnus-summary-toggle-truncation (&optional arg)
5647   "Toggle truncation of summary lines.
5648 With arg, turn line truncation on iff arg is positive."
5649   (interactive "P")
5650   (setq truncate-lines
5651         (if (null arg) (not truncate-lines)
5652           (> (prefix-numeric-value arg) 0)))
5653   (redraw-display))
5654
5655 (defun gnus-summary-reselect-current-group (&optional all rescan)
5656   "Exit and then reselect the current newsgroup.
5657 The prefix argument ALL means to select all articles."
5658   (interactive "P")
5659   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5660     (error "Ephemeral groups can't be reselected"))
5661   (let ((current-subject (gnus-summary-article-number))
5662         (group gnus-newsgroup-name))
5663     (setq gnus-newsgroup-begin nil)
5664     (gnus-summary-exit)
5665     ;; We have to adjust the point of group mode buffer because
5666     ;; point was moved to the next unread newsgroup by exiting.
5667     (gnus-summary-jump-to-group group)
5668     (when rescan
5669       (save-excursion
5670         (save-window-excursion
5671           ;; Don't show group contents.
5672           (set-window-start (selected-window) (point-max))
5673           (gnus-group-get-new-news-this-group 1))))
5674     (gnus-group-read-group all t)
5675     (gnus-summary-goto-subject current-subject nil t)))
5676
5677 (defun gnus-summary-rescan-group (&optional all)
5678   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5679   (interactive "P")
5680   (gnus-summary-reselect-current-group all t))
5681
5682 (defun gnus-summary-update-info (&optional non-destructive)
5683   (save-excursion
5684     (let ((group gnus-newsgroup-name))
5685       (when group
5686         (when gnus-newsgroup-kill-headers
5687           (setq gnus-newsgroup-killed
5688                 (gnus-compress-sequence
5689                  (nconc
5690                   (gnus-set-sorted-intersection
5691                    (gnus-uncompress-range gnus-newsgroup-killed)
5692                    (setq gnus-newsgroup-unselected
5693                          (sort gnus-newsgroup-unselected '<)))
5694                   (setq gnus-newsgroup-unreads
5695                         (sort gnus-newsgroup-unreads '<)))
5696                  t)))
5697         (unless (listp (cdr gnus-newsgroup-killed))
5698           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5699         (let ((headers gnus-newsgroup-headers))
5700           ;; Set the new ranges of read articles.
5701           (save-excursion
5702             (set-buffer gnus-group-buffer)
5703             (gnus-undo-force-boundary))
5704           (gnus-update-read-articles
5705            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5706           ;; Set the current article marks.
5707           (let ((gnus-newsgroup-scored
5708                  (if (and (not gnus-save-score)
5709                           (not non-destructive))
5710                      nil
5711                    gnus-newsgroup-scored)))
5712             (save-excursion
5713               (gnus-update-marks)))
5714           ;; Do the cross-ref thing.
5715           (when gnus-use-cross-reference
5716             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5717           ;; Do not switch windows but change the buffer to work.
5718           (set-buffer gnus-group-buffer)
5719           (unless (gnus-ephemeral-group-p group)
5720             (gnus-group-update-group group)))))))
5721
5722 (defun gnus-summary-save-newsrc (&optional force)
5723   "Save the current number of read/marked articles in the dribble buffer.
5724 The dribble buffer will then be saved.
5725 If FORCE (the prefix), also save the .newsrc file(s)."
5726   (interactive "P")
5727   (gnus-summary-update-info t)
5728   (if force
5729       (gnus-save-newsrc-file)
5730     (gnus-dribble-save)))
5731
5732 (defun gnus-summary-exit (&optional temporary)
5733   "Exit reading current newsgroup, and then return to group selection mode.
5734 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5735   (interactive)
5736   (gnus-set-global-variables)
5737   (gnus-kill-save-kill-buffer)
5738   (gnus-async-halt-prefetch)
5739   (let* ((group gnus-newsgroup-name)
5740          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5741          (mode major-mode)
5742          (group-point nil)
5743          (buf (current-buffer)))
5744     (unless quit-config
5745       ;; Do adaptive scoring, and possibly save score files.
5746       (when gnus-newsgroup-adaptive
5747         (gnus-score-adaptive))
5748       (when gnus-use-scoring
5749         (gnus-score-save)))
5750     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5751     ;; If we have several article buffers, we kill them at exit.
5752     (unless gnus-single-article-buffer
5753       (gnus-kill-buffer gnus-original-article-buffer)
5754       (setq gnus-article-current nil))
5755     (when gnus-use-cache
5756       (gnus-cache-possibly-remove-articles)
5757       (gnus-cache-save-buffers))
5758     (gnus-async-prefetch-remove-group group)
5759     (when gnus-suppress-duplicates
5760       (gnus-dup-enter-articles))
5761     (when gnus-use-trees
5762       (gnus-tree-close group))
5763     (when gnus-use-cache
5764       (gnus-cache-write-active))
5765     ;; Remove entries for this group.
5766     (nnmail-purge-split-history (gnus-group-real-name group))
5767     ;; Make all changes in this group permanent.
5768     (unless quit-config
5769       (gnus-run-hooks 'gnus-exit-group-hook)
5770       (gnus-summary-update-info))
5771     (gnus-close-group group)
5772     ;; Make sure where we were, and go to next newsgroup.
5773     (set-buffer gnus-group-buffer)
5774     (unless quit-config
5775       (gnus-group-jump-to-group group))
5776     (gnus-run-hooks 'gnus-summary-exit-hook)
5777     (unless (or quit-config
5778                 ;; If this group has disappeared from the summary
5779                 ;; buffer, don't skip forwards.
5780                 (not (string= group (gnus-group-group-name))))
5781       (gnus-group-next-unread-group 1))
5782     (setq group-point (point))
5783     (if temporary
5784         nil                             ;Nothing to do.
5785       ;; If we have several article buffers, we kill them at exit.
5786       (unless gnus-single-article-buffer
5787         (gnus-kill-buffer gnus-article-buffer)
5788         (gnus-kill-buffer gnus-original-article-buffer)
5789         (setq gnus-article-current nil))
5790       (set-buffer buf)
5791       (if (not gnus-kill-summary-on-exit)
5792           (progn
5793             (gnus-deaden-summary)
5794             (setq mode nil))
5795         ;; We set all buffer-local variables to nil.  It is unclear why
5796         ;; this is needed, but if we don't, buffer-local variables are
5797         ;; not garbage-collected, it seems.  This would the lead to en
5798         ;; ever-growing Emacs.
5799         (gnus-summary-clear-local-variables)
5800         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5801           (gnus-summary-clear-local-variables))
5802         (when (get-buffer gnus-article-buffer)
5803           (bury-buffer gnus-article-buffer))
5804         ;; We clear the global counterparts of the buffer-local
5805         ;; variables as well, just to be on the safe side.
5806         (set-buffer gnus-group-buffer)
5807         (gnus-summary-clear-local-variables)
5808         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5809           (gnus-summary-clear-local-variables)))
5810       (setq gnus-current-select-method gnus-select-method)
5811       (pop-to-buffer gnus-group-buffer)
5812       (if (not quit-config)
5813           (progn
5814             (goto-char group-point)
5815             (gnus-configure-windows 'group 'force)
5816             (unless (pos-visible-in-window-p)
5817               (forward-line (/ (static-if (featurep 'xemacs)
5818                                    (window-displayed-height)
5819                                  (1- (window-height)))
5820                                -2))
5821               (set-window-start (selected-window) (point))
5822               (goto-char group-point)))
5823         (gnus-handle-ephemeral-exit quit-config))
5824       ;; Return to group mode buffer.
5825       (when (eq mode 'gnus-summary-mode)
5826         (gnus-kill-buffer buf))
5827       ;; Clear the current group name.
5828       (unless quit-config
5829         (setq gnus-newsgroup-name nil)))))
5830
5831 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5832 (defun gnus-summary-exit-no-update (&optional no-questions)
5833   "Quit reading current newsgroup without updating read article info."
5834   (interactive)
5835   (let* ((group gnus-newsgroup-name)
5836          (quit-config (gnus-group-quit-config group)))
5837     (when (or no-questions
5838               gnus-expert-user
5839               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5840       (gnus-async-halt-prefetch)
5841       (mapcar 'funcall
5842               (delq 'gnus-summary-expire-articles
5843                     (copy-sequence gnus-summary-prepare-exit-hook)))
5844       ;; If we have several article buffers, we kill them at exit.
5845       (unless gnus-single-article-buffer
5846         (gnus-kill-buffer gnus-article-buffer)
5847         (gnus-kill-buffer gnus-original-article-buffer)
5848         (setq gnus-article-current nil))
5849       (if (not gnus-kill-summary-on-exit)
5850           (gnus-deaden-summary)
5851         (gnus-close-group group)
5852         (gnus-summary-clear-local-variables)
5853         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5854           (gnus-summary-clear-local-variables))
5855         (set-buffer gnus-group-buffer)
5856         (gnus-summary-clear-local-variables)
5857         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5858           (gnus-summary-clear-local-variables))
5859         (when (get-buffer gnus-summary-buffer)
5860           (kill-buffer gnus-summary-buffer)))
5861       (unless gnus-single-article-buffer
5862         (setq gnus-article-current nil))
5863       (when gnus-use-trees
5864         (gnus-tree-close group))
5865       (gnus-async-prefetch-remove-group group)
5866       (when (get-buffer gnus-article-buffer)
5867         (bury-buffer gnus-article-buffer))
5868       ;; Return to the group buffer.
5869       (gnus-configure-windows 'group 'force)
5870       ;; Clear the current group name.
5871       (setq gnus-newsgroup-name nil)
5872       (when (equal (gnus-group-group-name) group)
5873         (gnus-group-next-unread-group 1))
5874       (when quit-config
5875         (gnus-handle-ephemeral-exit quit-config)))))
5876
5877 (defun gnus-handle-ephemeral-exit (quit-config)
5878   "Handle movement when leaving an ephemeral group.
5879 The state which existed when entering the ephemeral is reset."
5880   (if (not (buffer-name (car quit-config)))
5881       (gnus-configure-windows 'group 'force)
5882     (set-buffer (car quit-config))
5883     (cond ((eq major-mode 'gnus-summary-mode)
5884            (gnus-set-global-variables))
5885           ((eq major-mode 'gnus-article-mode)
5886            (save-excursion
5887              ;; The `gnus-summary-buffer' variable may point
5888              ;; to the old summary buffer when using a single
5889              ;; article buffer.
5890              (unless (gnus-buffer-live-p gnus-summary-buffer)
5891                (set-buffer gnus-group-buffer))
5892              (set-buffer gnus-summary-buffer)
5893              (gnus-set-global-variables))))
5894     (if (or (eq (cdr quit-config) 'article)
5895             (eq (cdr quit-config) 'pick))
5896         (progn
5897           ;; The current article may be from the ephemeral group
5898           ;; thus it is best that we reload this article
5899           (gnus-summary-show-article)
5900           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5901               (gnus-configure-windows 'pick 'force)
5902             (gnus-configure-windows (cdr quit-config) 'force)))
5903       (gnus-configure-windows (cdr quit-config) 'force))
5904     (when (eq major-mode 'gnus-summary-mode)
5905       (gnus-summary-next-subject 1 nil t)
5906       (gnus-summary-recenter)
5907       (gnus-summary-position-point))))
5908
5909 (defun gnus-summary-preview-mime-message ()
5910   "MIME decode and play this message."
5911   (interactive)
5912   (let ((gnus-break-pages nil)
5913         (gnus-show-mime t))
5914     (gnus-summary-select-article gnus-show-all-headers t))
5915   (select-window (get-buffer-window gnus-article-buffer)))
5916
5917 ;;; Dead summaries.
5918
5919 (defvar gnus-dead-summary-mode-map nil)
5920
5921 (unless gnus-dead-summary-mode-map
5922   (setq gnus-dead-summary-mode-map (make-keymap))
5923   (suppress-keymap gnus-dead-summary-mode-map)
5924   (substitute-key-definition
5925    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5926   (let ((keys '("\C-d" "\r" "\177" [delete])))
5927     (while keys
5928       (define-key gnus-dead-summary-mode-map
5929         (pop keys) 'gnus-summary-wake-up-the-dead))))
5930
5931 (defvar gnus-dead-summary-mode nil
5932   "Minor mode for Gnus summary buffers.")
5933
5934 (defun gnus-dead-summary-mode (&optional arg)
5935   "Minor mode for Gnus summary buffers."
5936   (interactive "P")
5937   (when (eq major-mode 'gnus-summary-mode)
5938     (make-local-variable 'gnus-dead-summary-mode)
5939     (setq gnus-dead-summary-mode
5940           (if (null arg) (not gnus-dead-summary-mode)
5941             (> (prefix-numeric-value arg) 0)))
5942     (when gnus-dead-summary-mode
5943       (gnus-add-minor-mode
5944        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5945
5946 (defun gnus-deaden-summary ()
5947   "Make the current summary buffer into a dead summary buffer."
5948   ;; Kill any previous dead summary buffer.
5949   (when (and gnus-dead-summary
5950              (buffer-name gnus-dead-summary))
5951     (save-excursion
5952       (set-buffer gnus-dead-summary)
5953       (when gnus-dead-summary-mode
5954         (kill-buffer (current-buffer)))))
5955   ;; Make this the current dead summary.
5956   (setq gnus-dead-summary (current-buffer))
5957   (gnus-dead-summary-mode 1)
5958   (let ((name (buffer-name)))
5959     (when (string-match "Summary" name)
5960       (rename-buffer
5961        (concat (substring name 0 (match-beginning 0)) "Dead "
5962                (substring name (match-beginning 0)))
5963        t)
5964       (bury-buffer))))
5965
5966 (defun gnus-kill-or-deaden-summary (buffer)
5967   "Kill or deaden the summary BUFFER."
5968   (save-excursion
5969     (when (and (buffer-name buffer)
5970                (not gnus-single-article-buffer))
5971       (save-excursion
5972         (set-buffer buffer)
5973         (gnus-kill-buffer gnus-article-buffer)
5974         (gnus-kill-buffer gnus-original-article-buffer)))
5975     (cond (gnus-kill-summary-on-exit
5976            (when (and gnus-use-trees
5977                       (gnus-buffer-exists-p buffer))
5978              (save-excursion
5979                (set-buffer buffer)
5980                (gnus-tree-close gnus-newsgroup-name)))
5981            (gnus-kill-buffer buffer))
5982           ((gnus-buffer-exists-p buffer)
5983            (save-excursion
5984              (set-buffer buffer)
5985              (gnus-deaden-summary))))))
5986
5987 (defun gnus-summary-wake-up-the-dead (&rest args)
5988   "Wake up the dead summary buffer."
5989   (interactive)
5990   (gnus-dead-summary-mode -1)
5991   (let ((name (buffer-name)))
5992     (when (string-match "Dead " name)
5993       (rename-buffer
5994        (concat (substring name 0 (match-beginning 0))
5995                (substring name (match-end 0)))
5996        t)))
5997   (gnus-message 3 "This dead summary is now alive again"))
5998
5999 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6000 (defun gnus-summary-fetch-faq (&optional faq-dir)
6001   "Fetch the FAQ for the current group.
6002 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6003 in."
6004   (interactive
6005    (list
6006     (when current-prefix-arg
6007       (completing-read
6008        "Faq dir: " (and (listp gnus-group-faq-directory)
6009                         (mapcar (lambda (file) (list file))
6010                                 gnus-group-faq-directory))))))
6011   (let (gnus-faq-buffer)
6012     (when (setq gnus-faq-buffer
6013                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6014       (gnus-configure-windows 'summary-faq))))
6015
6016 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6017 (defun gnus-summary-describe-group (&optional force)
6018   "Describe the current newsgroup."
6019   (interactive "P")
6020   (gnus-group-describe-group force gnus-newsgroup-name))
6021
6022 (defun gnus-summary-describe-briefly ()
6023   "Describe summary mode commands briefly."
6024   (interactive)
6025   (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")))
6026
6027 ;; Walking around group mode buffer from summary mode.
6028
6029 (defun gnus-summary-next-group (&optional no-article target-group backward)
6030   "Exit current newsgroup and then select next unread newsgroup.
6031 If prefix argument NO-ARTICLE is non-nil, no article is selected
6032 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6033 previous group instead."
6034   (interactive "P")
6035   ;; Stop pre-fetching.
6036   (gnus-async-halt-prefetch)
6037   (let ((current-group gnus-newsgroup-name)
6038         (current-buffer (current-buffer))
6039         entered)
6040     ;; First we semi-exit this group to update Xrefs and all variables.
6041     ;; We can't do a real exit, because the window conf must remain
6042     ;; the same in case the user is prompted for info, and we don't
6043     ;; want the window conf to change before that...
6044     (gnus-summary-exit t)
6045     (while (not entered)
6046       ;; Then we find what group we are supposed to enter.
6047       (set-buffer gnus-group-buffer)
6048       (gnus-group-jump-to-group current-group)
6049       (setq target-group
6050             (or target-group
6051                 (if (eq gnus-keep-same-level 'best)
6052                     (gnus-summary-best-group gnus-newsgroup-name)
6053                   (gnus-summary-search-group backward gnus-keep-same-level))))
6054       (if (not target-group)
6055           ;; There are no further groups, so we return to the group
6056           ;; buffer.
6057           (progn
6058             (gnus-message 5 "Returning to the group buffer")
6059             (setq entered t)
6060             (when (gnus-buffer-live-p current-buffer)
6061               (set-buffer current-buffer)
6062               (gnus-summary-exit))
6063             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6064         ;; We try to enter the target group.
6065         (gnus-group-jump-to-group target-group)
6066         (let ((unreads (gnus-group-group-unread)))
6067           (if (and (or (eq t unreads)
6068                        (and unreads (not (zerop unreads))))
6069                    (gnus-summary-read-group
6070                     target-group nil no-article
6071                     (and (buffer-name current-buffer) current-buffer)
6072                     nil backward))
6073               (setq entered t)
6074             (setq current-group target-group
6075                   target-group nil)))))))
6076
6077 (defun gnus-summary-prev-group (&optional no-article)
6078   "Exit current newsgroup and then select previous unread newsgroup.
6079 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6080   (interactive "P")
6081   (gnus-summary-next-group no-article nil t))
6082
6083 ;; Walking around summary lines.
6084
6085 (defun gnus-summary-first-subject (&optional unread undownloaded)
6086   "Go to the first unread subject.
6087 If UNREAD is non-nil, go to the first unread article.
6088 Returns the article selected or nil if there are no unread articles."
6089   (interactive "P")
6090   (prog1
6091       (cond
6092        ;; Empty summary.
6093        ((null gnus-newsgroup-data)
6094         (gnus-message 3 "No articles in the group")
6095         nil)
6096        ;; Pick the first article.
6097        ((not unread)
6098         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6099         (gnus-data-number (car gnus-newsgroup-data)))
6100        ;; No unread articles.
6101        ((null gnus-newsgroup-unreads)
6102         (gnus-message 3 "No more unread articles")
6103         nil)
6104        ;; Find the first unread article.
6105        (t
6106         (let ((data gnus-newsgroup-data))
6107           (while (and data
6108                       (and (not (and undownloaded
6109                                      (eq gnus-undownloaded-mark
6110                                          (gnus-data-mark (car data)))))
6111                            (not (gnus-data-unread-p (car data)))))
6112             (setq data (cdr data)))
6113           (when data
6114             (goto-char (gnus-data-pos (car data)))
6115             (gnus-data-number (car data))))))
6116     (gnus-summary-position-point)))
6117
6118 (defun gnus-summary-next-subject (n &optional unread dont-display)
6119   "Go to next N'th summary line.
6120 If N is negative, go to the previous N'th subject line.
6121 If UNREAD is non-nil, only unread articles are selected.
6122 The difference between N and the actual number of steps taken is
6123 returned."
6124   (interactive "p")
6125   (let ((backward (< n 0))
6126         (n (abs n)))
6127     (while (and (> n 0)
6128                 (if backward
6129                     (gnus-summary-find-prev unread)
6130                   (gnus-summary-find-next unread)))
6131       (unless (zerop (setq n (1- n)))
6132         (gnus-summary-show-thread)))
6133     (when (/= 0 n)
6134       (gnus-message 7 "No more%s articles"
6135                     (if unread " unread" "")))
6136     (unless dont-display
6137       (gnus-summary-recenter)
6138       (gnus-summary-position-point))
6139     n))
6140
6141 (defun gnus-summary-next-unread-subject (n)
6142   "Go to next N'th unread summary line."
6143   (interactive "p")
6144   (gnus-summary-next-subject n t))
6145
6146 (defun gnus-summary-prev-subject (n &optional unread)
6147   "Go to previous N'th summary line.
6148 If optional argument UNREAD is non-nil, only unread article is selected."
6149   (interactive "p")
6150   (gnus-summary-next-subject (- n) unread))
6151
6152 (defun gnus-summary-prev-unread-subject (n)
6153   "Go to previous N'th unread summary line."
6154   (interactive "p")
6155   (gnus-summary-next-subject (- n) t))
6156
6157 (defun gnus-summary-goto-subject (article &optional force silent)
6158   "Go the subject line of ARTICLE.
6159 If FORCE, also allow jumping to articles not currently shown."
6160   (interactive "nArticle number: ")
6161   (let ((b (point))
6162         (data (gnus-data-find article)))
6163     ;; We read in the article if we have to.
6164     (and (not data)
6165          force
6166          (gnus-summary-insert-subject
6167           article
6168           (if (or (numberp force) (vectorp force)) force)
6169           t)
6170          (setq data (gnus-data-find article)))
6171     (goto-char b)
6172     (if (not data)
6173         (progn
6174           (unless silent
6175             (gnus-message 3 "Can't find article %d" article))
6176           nil)
6177       (goto-char (gnus-data-pos data))
6178       (gnus-summary-position-point)
6179       article)))
6180
6181 ;; Walking around summary lines with displaying articles.
6182
6183 (defun gnus-summary-expand-window (&optional arg)
6184   "Make the summary buffer take up the entire Emacs frame.
6185 Given a prefix, will force an `article' buffer configuration."
6186   (interactive "P")
6187   (if arg
6188       (gnus-configure-windows 'article 'force)
6189     (gnus-configure-windows 'summary 'force)))
6190
6191 (defun gnus-summary-display-article (article &optional all-header)
6192   "Display ARTICLE in article buffer."
6193   (when (gnus-buffer-live-p gnus-article-buffer)
6194     (with-current-buffer gnus-article-buffer
6195       (set-buffer-multibyte t)))
6196   (gnus-set-global-variables)
6197   (when (gnus-buffer-live-p gnus-article-buffer)
6198     (with-current-buffer gnus-article-buffer
6199       (setq gnus-article-charset gnus-newsgroup-charset)
6200       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6201   (if (null article)
6202       nil
6203     (prog1
6204         (if gnus-summary-display-article-function
6205             (funcall gnus-summary-display-article-function article all-header)
6206           (gnus-article-prepare article all-header))
6207       (with-current-buffer gnus-article-buffer
6208         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6209              nil))
6210       (gnus-run-hooks 'gnus-select-article-hook)
6211       (when (and gnus-current-article
6212                  (not (zerop gnus-current-article)))
6213         (gnus-summary-goto-subject gnus-current-article))
6214       (gnus-summary-recenter)
6215       (when (and gnus-use-trees gnus-show-threads)
6216         (gnus-possibly-generate-tree article)
6217         (gnus-highlight-selected-tree article))
6218       ;; Successfully display article.
6219       (gnus-article-set-window-start
6220        (cdr (assq article gnus-newsgroup-bookmarks))))))
6221
6222 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6223   "Select the current article.
6224 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6225 non-nil, the article will be re-fetched even if it already present in
6226 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6227 be displayed."
6228   ;; Make sure we are in the summary buffer to work around bbdb bug.
6229   (unless (eq major-mode 'gnus-summary-mode)
6230     (set-buffer gnus-summary-buffer))
6231   (let ((article (or article (gnus-summary-article-number)))
6232         (all-headers (not (not all-headers))) ;Must be T or NIL.
6233         gnus-summary-display-article-function)
6234     (and (not pseudo)
6235          (gnus-summary-article-pseudo-p article)
6236          (error "This is a pseudo-article"))
6237     (save-excursion
6238       (set-buffer gnus-summary-buffer)
6239       (if (or (and gnus-single-article-buffer
6240                    (or (null gnus-current-article)
6241                        (null gnus-article-current)
6242                        (null (get-buffer gnus-article-buffer))
6243                        (not (eq article (cdr gnus-article-current)))
6244                        (not (equal (car gnus-article-current)
6245                                    gnus-newsgroup-name))))
6246               (and (not gnus-single-article-buffer)
6247                    (or (null gnus-current-article)
6248                        (not (eq gnus-current-article article))))
6249               force)
6250           ;; The requested article is different from the current article.
6251           (progn
6252             (gnus-summary-display-article article all-headers)
6253             (when (or all-headers gnus-show-all-headers)
6254               (gnus-article-show-all-headers))
6255             (gnus-article-set-window-start
6256              (cdr (assq article gnus-newsgroup-bookmarks)))
6257             article)
6258         (when (or all-headers gnus-show-all-headers)
6259           (gnus-article-show-all-headers))
6260         'old))))
6261
6262 (defun gnus-summary-force-verify-and-decrypt ()
6263   (interactive)
6264   (let ((mm-verify-option 'known)
6265         (mm-decrypt-option 'known))
6266     (gnus-summary-select-article nil 'force)))
6267
6268 (defun gnus-summary-set-current-mark (&optional current-mark)
6269   "Obsolete function."
6270   nil)
6271
6272 (defun gnus-summary-next-article (&optional unread subject backward push)
6273   "Select the next article.
6274 If UNREAD, only unread articles are selected.
6275 If SUBJECT, only articles with SUBJECT are selected.
6276 If BACKWARD, the previous article is selected instead of the next."
6277   (interactive "P")
6278   (cond
6279    ;; Is there such an article?
6280    ((and (gnus-summary-search-forward unread subject backward)
6281          (or (gnus-summary-display-article (gnus-summary-article-number))
6282              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6283     (gnus-summary-position-point))
6284    ;; If not, we try the first unread, if that is wanted.
6285    ((and subject
6286          gnus-auto-select-same
6287          (gnus-summary-first-unread-article))
6288     (gnus-summary-position-point)
6289     (gnus-message 6 "Wrapped"))
6290    ;; Try to get next/previous article not displayed in this group.
6291    ((and gnus-auto-extend-newsgroup
6292          (not unread) (not subject))
6293     (gnus-summary-goto-article
6294      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6295      nil (count-lines (point-min) (point))))
6296    ;; Go to next/previous group.
6297    (t
6298     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6299       (gnus-summary-jump-to-group gnus-newsgroup-name))
6300     (let ((cmd last-command-char)
6301           (point
6302            (save-excursion
6303              (set-buffer gnus-group-buffer)
6304              (point)))
6305           (group
6306            (if (eq gnus-keep-same-level 'best)
6307                (gnus-summary-best-group gnus-newsgroup-name)
6308              (gnus-summary-search-group backward gnus-keep-same-level))))
6309       ;; For some reason, the group window gets selected.  We change
6310       ;; it back.
6311       (select-window (get-buffer-window (current-buffer)))
6312       ;; Select next unread newsgroup automagically.
6313       (cond
6314        ((or (not gnus-auto-select-next)
6315             (not cmd))
6316         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6317        ((or (eq gnus-auto-select-next 'quietly)
6318             (and (eq gnus-auto-select-next 'slightly-quietly)
6319                  push)
6320             (and (eq gnus-auto-select-next 'almost-quietly)
6321                  (gnus-summary-last-article-p)))
6322         ;; Select quietly.
6323         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6324             (gnus-summary-exit)
6325           (gnus-message 7 "No more%s articles (%s)..."
6326                         (if unread " unread" "")
6327                         (if group (concat "selecting " group)
6328                           "exiting"))
6329           (gnus-summary-next-group nil group backward)))
6330        (t
6331         (when (gnus-key-press-event-p last-input-event)
6332           (gnus-summary-walk-group-buffer
6333            gnus-newsgroup-name cmd unread backward point))))))))
6334
6335 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6336   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6337                       (?\C-p (gnus-group-prev-unread-group 1))))
6338         (cursor-in-echo-area t)
6339         keve key group ended)
6340     (save-excursion
6341       (set-buffer gnus-group-buffer)
6342       (goto-char start)
6343       (setq group
6344             (if (eq gnus-keep-same-level 'best)
6345                 (gnus-summary-best-group gnus-newsgroup-name)
6346               (gnus-summary-search-group backward gnus-keep-same-level))))
6347     (while (not ended)
6348       (gnus-message
6349        5 "No more%s articles%s" (if unread " unread" "")
6350        (if (and group
6351                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6352            (format " (Type %s for %s [%s])"
6353                    (single-key-description cmd) group
6354                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6355          (format " (Type %s to exit %s)"
6356                  (single-key-description cmd)
6357                  gnus-newsgroup-name)))
6358       ;; Confirm auto selection.
6359       (setq key (car (setq keve (gnus-read-event-char))))
6360       (setq ended t)
6361       (cond
6362        ((assq key keystrokes)
6363         (let ((obuf (current-buffer)))
6364           (switch-to-buffer gnus-group-buffer)
6365           (when group
6366             (gnus-group-jump-to-group group))
6367           (eval (cadr (assq key keystrokes)))
6368           (setq group (gnus-group-group-name))
6369           (switch-to-buffer obuf))
6370         (setq ended nil))
6371        ((equal key cmd)
6372         (if (or (not group)
6373                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6374             (gnus-summary-exit)
6375           (gnus-summary-next-group nil group backward)))
6376        (t
6377         (push (cdr keve) unread-command-events))))))
6378
6379 (defun gnus-summary-next-unread-article ()
6380   "Select unread article after current one."
6381   (interactive)
6382   (gnus-summary-next-article
6383    (or (not (eq gnus-summary-goto-unread 'never))
6384        (gnus-summary-last-article-p (gnus-summary-article-number)))
6385    (and gnus-auto-select-same
6386         (gnus-summary-article-subject))))
6387
6388 (defun gnus-summary-prev-article (&optional unread subject)
6389   "Select the article after the current one.
6390 If UNREAD is non-nil, only unread articles are selected."
6391   (interactive "P")
6392   (gnus-summary-next-article unread subject t))
6393
6394 (defun gnus-summary-prev-unread-article ()
6395   "Select unread article before current one."
6396   (interactive)
6397   (gnus-summary-prev-article
6398    (or (not (eq gnus-summary-goto-unread 'never))
6399        (gnus-summary-first-article-p (gnus-summary-article-number)))
6400    (and gnus-auto-select-same
6401         (gnus-summary-article-subject))))
6402
6403 (defun gnus-summary-next-page (&optional lines circular)
6404   "Show next page of the selected article.
6405 If at the end of the current article, select the next article.
6406 LINES says how many lines should be scrolled up.
6407
6408 If CIRCULAR is non-nil, go to the start of the article instead of
6409 selecting the next article when reaching the end of the current
6410 article."
6411   (interactive "P")
6412   (setq gnus-summary-buffer (current-buffer))
6413   (gnus-set-global-variables)
6414   (let ((article (gnus-summary-article-number))
6415         (article-window (get-buffer-window gnus-article-buffer t))
6416         endp)
6417     ;; If the buffer is empty, we have no article.
6418     (unless article
6419       (error "No article to select"))
6420     (gnus-configure-windows 'article)
6421     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6422         (if (and (eq gnus-summary-goto-unread 'never)
6423                  (not (gnus-summary-last-article-p article)))
6424             (gnus-summary-next-article)
6425           (gnus-summary-next-unread-article))
6426       (if (or (null gnus-current-article)
6427               (null gnus-article-current)
6428               (/= article (cdr gnus-article-current))
6429               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6430           ;; Selected subject is different from current article's.
6431           (gnus-summary-display-article article)
6432         (when article-window
6433           (gnus-eval-in-buffer-window gnus-article-buffer
6434             (setq endp (gnus-article-next-page lines)))
6435           (when endp
6436             (cond (circular
6437                    (gnus-summary-beginning-of-article))
6438                   (lines
6439                    (gnus-message 3 "End of message"))
6440                   ((null lines)
6441                    (if (and (eq gnus-summary-goto-unread 'never)
6442                             (not (gnus-summary-last-article-p article)))
6443                        (gnus-summary-next-article)
6444                      (gnus-summary-next-unread-article))))))))
6445     (gnus-summary-recenter)
6446     (gnus-summary-position-point)))
6447
6448 (defun gnus-summary-prev-page (&optional lines move)
6449   "Show previous page of selected article.
6450 Argument LINES specifies lines to be scrolled down.
6451 If MOVE, move to the previous unread article if point is at
6452 the beginning of the buffer."
6453   (interactive "P")
6454   (let ((article (gnus-summary-article-number))
6455         (article-window (get-buffer-window gnus-article-buffer t))
6456         endp)
6457     (gnus-configure-windows 'article)
6458     (if (or (null gnus-current-article)
6459             (null gnus-article-current)
6460             (/= article (cdr gnus-article-current))
6461             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6462         ;; Selected subject is different from current article's.
6463         (gnus-summary-display-article article)
6464       (gnus-summary-recenter)
6465       (when article-window
6466         (gnus-eval-in-buffer-window gnus-article-buffer
6467           (setq endp (gnus-article-prev-page lines)))
6468         (when (and move endp)
6469           (cond (lines
6470                  (gnus-message 3 "Beginning of message"))
6471                 ((null lines)
6472                  (if (and (eq gnus-summary-goto-unread 'never)
6473                           (not (gnus-summary-first-article-p article)))
6474                      (gnus-summary-prev-article)
6475                    (gnus-summary-prev-unread-article))))))))
6476   (gnus-summary-position-point))
6477
6478 (defun gnus-summary-prev-page-or-article (&optional lines)
6479   "Show previous page of selected article.
6480 Argument LINES specifies lines to be scrolled down.
6481 If at the beginning of the article, go to the next article."
6482   (interactive "P")
6483   (gnus-summary-prev-page lines t))
6484
6485 (defun gnus-summary-scroll-up (lines)
6486   "Scroll up (or down) one line current article.
6487 Argument LINES specifies lines to be scrolled up (or down if negative)."
6488   (interactive "p")
6489   (gnus-configure-windows 'article)
6490   (gnus-summary-show-thread)
6491   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6492     (gnus-eval-in-buffer-window gnus-article-buffer
6493       (cond ((> lines 0)
6494              (when (gnus-article-next-page lines)
6495                (gnus-message 3 "End of message")))
6496             ((< lines 0)
6497              (gnus-article-prev-page (- lines))))))
6498   (gnus-summary-recenter)
6499   (gnus-summary-position-point))
6500
6501 (defun gnus-summary-scroll-down (lines)
6502   "Scroll down (or up) one line current article.
6503 Argument LINES specifies lines to be scrolled down (or up if negative)."
6504   (interactive "p")
6505   (gnus-summary-scroll-up (- lines)))
6506
6507 (defun gnus-summary-next-same-subject ()
6508   "Select next article which has the same subject as current one."
6509   (interactive)
6510   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6511
6512 (defun gnus-summary-prev-same-subject ()
6513   "Select previous article which has the same subject as current one."
6514   (interactive)
6515   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6516
6517 (defun gnus-summary-next-unread-same-subject ()
6518   "Select next unread article which has the same subject as current one."
6519   (interactive)
6520   (gnus-summary-next-article t (gnus-summary-article-subject)))
6521
6522 (defun gnus-summary-prev-unread-same-subject ()
6523   "Select previous unread article which has the same subject as current one."
6524   (interactive)
6525   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6526
6527 (defun gnus-summary-first-unread-article ()
6528   "Select the first unread article.
6529 Return nil if there are no unread articles."
6530   (interactive)
6531   (prog1
6532       (when (gnus-summary-first-subject t)
6533         (gnus-summary-show-thread)
6534         (gnus-summary-first-subject t)
6535         (gnus-summary-display-article (gnus-summary-article-number)))
6536     (gnus-summary-position-point)))
6537
6538 (defun gnus-summary-first-unread-subject ()
6539   "Place the point on the subject line of the first unread article.
6540 Return nil if there are no unread articles."
6541   (interactive)
6542   (prog1
6543       (when (gnus-summary-first-subject t)
6544         (gnus-summary-show-thread)
6545         (gnus-summary-first-subject t))
6546     (gnus-summary-position-point)))
6547
6548 (defun gnus-summary-first-article ()
6549   "Select the first article.
6550 Return nil if there are no articles."
6551   (interactive)
6552   (prog1
6553       (when (gnus-summary-first-subject)
6554         (gnus-summary-show-thread)
6555         (gnus-summary-first-subject)
6556         (gnus-summary-display-article (gnus-summary-article-number)))
6557     (gnus-summary-position-point)))
6558
6559 (defun gnus-summary-best-unread-article ()
6560   "Select the unread article with the highest score."
6561   (interactive)
6562   (let ((best -1000000)
6563         (data gnus-newsgroup-data)
6564         article score)
6565     (while data
6566       (and (gnus-data-unread-p (car data))
6567            (> (setq score
6568                     (gnus-summary-article-score (gnus-data-number (car data))))
6569               best)
6570            (setq best score
6571                  article (gnus-data-number (car data))))
6572       (setq data (cdr data)))
6573     (prog1
6574         (if article
6575             (gnus-summary-goto-article article)
6576           (error "No unread articles"))
6577       (gnus-summary-position-point))))
6578
6579 (defun gnus-summary-last-subject ()
6580   "Go to the last displayed subject line in the group."
6581   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6582     (when article
6583       (gnus-summary-goto-subject article))))
6584
6585 (defun gnus-summary-goto-article (article &optional all-headers force)
6586   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6587 If ALL-HEADERS is non-nil, no header lines are hidden.
6588 If FORCE, go to the article even if it isn't displayed.  If FORCE
6589 is a number, it is the line the article is to be displayed on."
6590   (interactive
6591    (list
6592     (completing-read
6593      "Article number or Message-ID: "
6594      (mapcar (lambda (number) (list (int-to-string number)))
6595              gnus-newsgroup-limit))
6596     current-prefix-arg
6597     t))
6598   (prog1
6599       (if (and (stringp article)
6600                (string-match "@" article))
6601           (gnus-summary-refer-article article)
6602         (when (stringp article)
6603           (setq article (string-to-number article)))
6604         (if (gnus-summary-goto-subject article force)
6605             (gnus-summary-display-article article all-headers)
6606           (gnus-message 4 "Couldn't go to article %s" article) nil))
6607     (gnus-summary-position-point)))
6608
6609 (defun gnus-summary-goto-last-article ()
6610   "Go to the previously read article."
6611   (interactive)
6612   (prog1
6613       (when gnus-last-article
6614         (gnus-summary-goto-article gnus-last-article nil t))
6615     (gnus-summary-position-point)))
6616
6617 (defun gnus-summary-pop-article (number)
6618   "Pop one article off the history and go to the previous.
6619 NUMBER articles will be popped off."
6620   (interactive "p")
6621   (let (to)
6622     (setq gnus-newsgroup-history
6623           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6624     (if to
6625         (gnus-summary-goto-article (car to) nil t)
6626       (error "Article history empty")))
6627   (gnus-summary-position-point))
6628
6629 ;; Summary commands and functions for limiting the summary buffer.
6630
6631 (defun gnus-summary-limit-to-articles (n)
6632   "Limit the summary buffer to the next N articles.
6633 If not given a prefix, use the process marked articles instead."
6634   (interactive "P")
6635   (prog1
6636       (let ((articles (gnus-summary-work-articles n)))
6637         (setq gnus-newsgroup-processable nil)
6638         (gnus-summary-limit articles))
6639     (gnus-summary-position-point)))
6640
6641 (defun gnus-summary-pop-limit (&optional total)
6642   "Restore the previous limit.
6643 If given a prefix, remove all limits."
6644   (interactive "P")
6645   (when total
6646     (setq gnus-newsgroup-limits
6647           (list (mapcar (lambda (h) (mail-header-number h))
6648                         gnus-newsgroup-headers))))
6649   (unless gnus-newsgroup-limits
6650     (error "No limit to pop"))
6651   (prog1
6652       (gnus-summary-limit nil 'pop)
6653     (gnus-summary-position-point)))
6654
6655 (defun gnus-summary-limit-to-subject (subject &optional header)
6656   "Limit the summary buffer to articles that have subjects that match a regexp."
6657   (interactive "sLimit to subject (regexp): ")
6658   (unless header
6659     (setq header "subject"))
6660   (when (not (equal "" subject))
6661     (prog1
6662         (let ((articles (gnus-summary-find-matching
6663                          (or header "subject") subject 'all)))
6664           (unless articles
6665             (error "Found no matches for \"%s\"" subject))
6666           (gnus-summary-limit articles))
6667       (gnus-summary-position-point))))
6668
6669 (defun gnus-summary-limit-to-author (from)
6670   "Limit the summary buffer to articles that have authors that match a regexp."
6671   (interactive "sLimit to author (regexp): ")
6672   (gnus-summary-limit-to-subject from "from"))
6673
6674 (defun gnus-summary-limit-to-age (age &optional younger-p)
6675   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6676 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6677 articles that are younger than AGE days."
6678   (interactive
6679    (let ((younger current-prefix-arg)
6680          (days-got nil)
6681          days)
6682      (while (not days-got)
6683        (setq days (if younger
6684                       (read-string "Limit to articles within (in days): ")
6685                     (read-string "Limit to articles older than (in days): ")))
6686        (when (> (length days) 0)
6687          (setq days (read days)))
6688        (if (numberp days)
6689            (setq days-got t)
6690          (message "Please enter a number.")
6691          (sleep-for 1)))
6692      (list days younger)))
6693   (prog1
6694       (let ((data gnus-newsgroup-data)
6695             (cutoff (days-to-time age))
6696             articles d date is-younger)
6697         (while (setq d (pop data))
6698           (when (and (vectorp (gnus-data-header d))
6699                      (setq date (mail-header-date (gnus-data-header d))))
6700             (setq is-younger (time-less-p
6701                               (time-since (condition-case ()
6702                                               (date-to-time date)
6703                                             (error '(0 0))))
6704                               cutoff))
6705             (when (if younger-p
6706                       is-younger
6707                     (not is-younger))
6708               (push (gnus-data-number d) articles))))
6709         (gnus-summary-limit (nreverse articles)))
6710     (gnus-summary-position-point)))
6711
6712 (defun gnus-summary-limit-to-extra (header regexp)
6713   "Limit the summary buffer to articles that match an 'extra' header."
6714   (interactive
6715    (let ((header
6716           (intern
6717            (gnus-completing-read
6718             (symbol-name (car gnus-extra-headers))
6719             "Limit extra header:"
6720             (mapcar (lambda (x)
6721                       (cons (symbol-name x) x))
6722                     gnus-extra-headers)
6723             nil
6724             t))))
6725      (list header
6726            (read-string (format "Limit to header %s (regexp): " header)))))
6727   (when (not (equal "" regexp))
6728     (prog1
6729         (let ((articles (gnus-summary-find-matching
6730                          (cons 'extra header) regexp 'all)))
6731           (unless articles
6732             (error "Found no matches for \"%s\"" regexp))
6733           (gnus-summary-limit articles))
6734       (gnus-summary-position-point))))
6735
6736 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6737 (make-obsolete
6738  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6739
6740 (defun gnus-summary-limit-to-unread (&optional all)
6741   "Limit the summary buffer to articles that are not marked as read.
6742 If ALL is non-nil, limit strictly to unread articles."
6743   (interactive "P")
6744   (if all
6745       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6746     (gnus-summary-limit-to-marks
6747      ;; Concat all the marks that say that an article is read and have
6748      ;; those removed.
6749      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6750            gnus-killed-mark gnus-kill-file-mark
6751            gnus-low-score-mark gnus-expirable-mark
6752            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6753            gnus-duplicate-mark gnus-souped-mark)
6754      'reverse)))
6755
6756 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6757 (make-obsolete 'gnus-summary-delete-marked-with
6758                'gnus-summary-limit-exlude-marks)
6759
6760 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6761   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6762 If REVERSE, limit the summary buffer to articles that are marked
6763 with MARKS.  MARKS can either be a string of marks or a list of marks.
6764 Returns how many articles were removed."
6765   (interactive "sMarks: ")
6766   (gnus-summary-limit-to-marks marks t))
6767
6768 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6769   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6770 If REVERSE (the prefix), limit the summary buffer to articles that are
6771 not marked with MARKS.  MARKS can either be a string of marks or a
6772 list of marks.
6773 Returns how many articles were removed."
6774   (interactive "sMarks: \nP")
6775   (prog1
6776       (let ((data gnus-newsgroup-data)
6777             (marks (if (listp marks) marks
6778                      (append marks nil))) ; Transform to list.
6779             articles)
6780         (while data
6781           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6782                   (memq (gnus-data-mark (car data)) marks))
6783             (push (gnus-data-number (car data)) articles))
6784           (setq data (cdr data)))
6785         (gnus-summary-limit articles))
6786     (gnus-summary-position-point)))
6787
6788 (defun gnus-summary-limit-to-score (&optional score)
6789   "Limit to articles with score at or above SCORE."
6790   (interactive "P")
6791   (setq score (if score
6792                   (prefix-numeric-value score)
6793                 (or gnus-summary-default-score 0)))
6794   (let ((data gnus-newsgroup-data)
6795         articles)
6796     (while data
6797       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6798                 score)
6799         (push (gnus-data-number (car data)) articles))
6800       (setq data (cdr data)))
6801     (prog1
6802         (gnus-summary-limit articles)
6803       (gnus-summary-position-point))))
6804
6805 (defun gnus-summary-limit-include-thread (id)
6806   "Display all the hidden articles that is in the thread with ID in it.
6807 When called interactively, ID is the Message-ID of the current
6808 article."
6809   (interactive (list (mail-header-id (gnus-summary-article-header))))
6810   (let ((articles (gnus-articles-in-thread
6811                    (gnus-id-to-thread (gnus-root-id id)))))
6812     (prog1
6813         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6814         (gnus-summary-limit-include-matching-articles
6815          "subject"
6816          (regexp-quote (gnus-simplify-subject-re
6817                         (mail-header-subject (gnus-id-to-header id)))))
6818       (gnus-summary-position-point))))
6819
6820 (defun gnus-summary-limit-include-matching-articles (header regexp)
6821   "Display all the hidden articles that have HEADERs that match REGEXP."
6822   (interactive (list (read-string "Match on header: ")
6823                      (read-string "Regexp: ")))
6824   (let ((articles (gnus-find-matching-articles header regexp)))
6825     (prog1
6826         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6827       (gnus-summary-position-point))))
6828
6829 (defun gnus-summary-limit-include-dormant ()
6830   "Display all the hidden articles that are marked as dormant.
6831 Note that this command only works on a subset of the articles currently
6832 fetched for this group."
6833   (interactive)
6834   (unless gnus-newsgroup-dormant
6835     (error "There are no dormant articles in this group"))
6836   (prog1
6837       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6838     (gnus-summary-position-point)))
6839
6840 (defun gnus-summary-limit-exclude-dormant ()
6841   "Hide all dormant articles."
6842   (interactive)
6843   (prog1
6844       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6845     (gnus-summary-position-point)))
6846
6847 (defun gnus-summary-limit-exclude-childless-dormant ()
6848   "Hide all dormant articles that have no children."
6849   (interactive)
6850   (let ((data (gnus-data-list t))
6851         articles d children)
6852     ;; Find all articles that are either not dormant or have
6853     ;; children.
6854     (while (setq d (pop data))
6855       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6856                 (and (setq children
6857                            (gnus-article-children (gnus-data-number d)))
6858                      (let (found)
6859                        (while children
6860                          (when (memq (car children) articles)
6861                            (setq children nil
6862                                  found t))
6863                          (pop children))
6864                        found)))
6865         (push (gnus-data-number d) articles)))
6866     ;; Do the limiting.
6867     (prog1
6868         (gnus-summary-limit articles)
6869       (gnus-summary-position-point))))
6870
6871 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6872   "Mark all unread excluded articles as read.
6873 If ALL, mark even excluded ticked and dormants as read."
6874   (interactive "P")
6875   (let ((articles (gnus-sorted-complement
6876                    (sort
6877                     (mapcar (lambda (h) (mail-header-number h))
6878                             gnus-newsgroup-headers)
6879                     '<)
6880                    (sort gnus-newsgroup-limit '<)))
6881         article)
6882     (setq gnus-newsgroup-unreads
6883           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6884     (if all
6885         (setq gnus-newsgroup-dormant nil
6886               gnus-newsgroup-marked nil
6887               gnus-newsgroup-reads
6888               (nconc
6889                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6890                gnus-newsgroup-reads))
6891       (while (setq article (pop articles))
6892         (unless (or (memq article gnus-newsgroup-dormant)
6893                     (memq article gnus-newsgroup-marked))
6894           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6895
6896 (defun gnus-summary-limit (articles &optional pop)
6897   (if pop
6898       ;; We pop the previous limit off the stack and use that.
6899       (setq articles (car gnus-newsgroup-limits)
6900             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6901     ;; We use the new limit, so we push the old limit on the stack.
6902     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6903   ;; Set the limit.
6904   (setq gnus-newsgroup-limit articles)
6905   (let ((total (length gnus-newsgroup-data))
6906         (data (gnus-data-find-list (gnus-summary-article-number)))
6907         (gnus-summary-mark-below nil)   ; Inhibit this.
6908         found)
6909     ;; This will do all the work of generating the new summary buffer
6910     ;; according to the new limit.
6911     (gnus-summary-prepare)
6912     ;; Hide any threads, possibly.
6913     (and gnus-show-threads
6914          gnus-thread-hide-subtree
6915          (gnus-summary-hide-all-threads))
6916     ;; Try to return to the article you were at, or one in the
6917     ;; neighborhood.
6918     (when data
6919       ;; We try to find some article after the current one.
6920       (while data
6921         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6922           (setq data nil
6923                 found t))
6924         (setq data (cdr data))))
6925     (unless found
6926       ;; If there is no data, that means that we were after the last
6927       ;; article.  The same goes when we can't find any articles
6928       ;; after the current one.
6929       (goto-char (point-max))
6930       (gnus-summary-find-prev))
6931     (gnus-set-mode-line 'summary)
6932     ;; We return how many articles were removed from the summary
6933     ;; buffer as a result of the new limit.
6934     (- total (length gnus-newsgroup-data))))
6935
6936 (defsubst gnus-invisible-cut-children (threads)
6937   (let ((num 0))
6938     (while threads
6939       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6940         (incf num))
6941       (pop threads))
6942     (< num 2)))
6943
6944 (defsubst gnus-cut-thread (thread)
6945   "Go forwards in the thread until we find an article that we want to display."
6946   (when (or (eq gnus-fetch-old-headers 'some)
6947             (eq gnus-fetch-old-headers 'invisible)
6948             (numberp gnus-fetch-old-headers)
6949             (eq gnus-build-sparse-threads 'some)
6950             (eq gnus-build-sparse-threads 'more))
6951     ;; Deal with old-fetched headers and sparse threads.
6952     (while (and
6953             thread
6954             (or
6955              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6956              (gnus-summary-article-ancient-p
6957               (mail-header-number (car thread))))
6958             (if (or (<= (length (cdr thread)) 1)
6959                     (eq gnus-fetch-old-headers 'invisible))
6960                 (setq gnus-newsgroup-limit
6961                       (delq (mail-header-number (car thread))
6962                             gnus-newsgroup-limit)
6963                       thread (cadr thread))
6964               (when (gnus-invisible-cut-children (cdr thread))
6965                 (let ((th (cdr thread)))
6966                   (while th
6967                     (if (memq (mail-header-number (caar th))
6968                               gnus-newsgroup-limit)
6969                         (setq thread (car th)
6970                               th nil)
6971                       (setq th (cdr th))))))))))
6972   thread)
6973
6974 (defun gnus-cut-threads (threads)
6975   "Cut off all uninteresting articles from the beginning of threads."
6976   (when (or (eq gnus-fetch-old-headers 'some)
6977             (eq gnus-fetch-old-headers 'invisible)
6978             (numberp gnus-fetch-old-headers)
6979             (eq gnus-build-sparse-threads 'some)
6980             (eq gnus-build-sparse-threads 'more))
6981     (let ((th threads))
6982       (while th
6983         (setcar th (gnus-cut-thread (car th)))
6984         (setq th (cdr th)))))
6985   ;; Remove nixed out threads.
6986   (delq nil threads))
6987
6988 (defun gnus-summary-initial-limit (&optional show-if-empty)
6989   "Figure out what the initial limit is supposed to be on group entry.
6990 This entails weeding out unwanted dormants, low-scored articles,
6991 fetch-old-headers verbiage, and so on."
6992   ;; Most groups have nothing to remove.
6993   (if (or gnus-inhibit-limiting
6994           (and (null gnus-newsgroup-dormant)
6995                (not (eq gnus-fetch-old-headers 'some))
6996                (not (numberp gnus-fetch-old-headers))
6997                (not (eq gnus-fetch-old-headers 'invisible))
6998                (null gnus-summary-expunge-below)
6999                (not (eq gnus-build-sparse-threads 'some))
7000                (not (eq gnus-build-sparse-threads 'more))
7001                (null gnus-thread-expunge-below)
7002                (not gnus-use-nocem)))
7003       ()                                ; Do nothing.
7004     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7005     (setq gnus-newsgroup-limit nil)
7006     (mapatoms
7007      (lambda (node)
7008        (unless (car (symbol-value node))
7009          ;; These threads have no parents -- they are roots.
7010          (let ((nodes (cdr (symbol-value node)))
7011                thread)
7012            (while nodes
7013              (if (and gnus-thread-expunge-below
7014                       (< (gnus-thread-total-score (car nodes))
7015                          gnus-thread-expunge-below))
7016                  (gnus-expunge-thread (pop nodes))
7017                (setq thread (pop nodes))
7018                (gnus-summary-limit-children thread))))))
7019      gnus-newsgroup-dependencies)
7020     ;; If this limitation resulted in an empty group, we might
7021     ;; pop the previous limit and use it instead.
7022     (when (and (not gnus-newsgroup-limit)
7023                show-if-empty)
7024       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7025     gnus-newsgroup-limit))
7026
7027 (defun gnus-summary-limit-children (thread)
7028   "Return 1 if this subthread is visible and 0 if it is not."
7029   ;; First we get the number of visible children to this thread.  This
7030   ;; is done by recursing down the thread using this function, so this
7031   ;; will really go down to a leaf article first, before slowly
7032   ;; working its way up towards the root.
7033   (when thread
7034     (let ((children
7035            (if (cdr thread)
7036                (apply '+ (mapcar 'gnus-summary-limit-children
7037                                  (cdr thread)))
7038              0))
7039           (number (mail-header-number (car thread)))
7040           score)
7041       (if (and
7042            (not (memq number gnus-newsgroup-marked))
7043            (or
7044             ;; If this article is dormant and has absolutely no visible
7045             ;; children, then this article isn't visible.
7046             (and (memq number gnus-newsgroup-dormant)
7047                  (zerop children))
7048             ;; If this is "fetch-old-headered" and there is no
7049             ;; visible children, then we don't want this article.
7050             (and (or (eq gnus-fetch-old-headers 'some)
7051                      (numberp gnus-fetch-old-headers))
7052                  (gnus-summary-article-ancient-p number)
7053                  (zerop children))
7054             ;; If this is "fetch-old-headered" and `invisible', then
7055             ;; we don't want this article.
7056             (and (eq gnus-fetch-old-headers 'invisible)
7057                  (gnus-summary-article-ancient-p number))
7058             ;; If this is a sparsely inserted article with no children,
7059             ;; we don't want it.
7060             (and (eq gnus-build-sparse-threads 'some)
7061                  (gnus-summary-article-sparse-p number)
7062                  (zerop children))
7063             ;; If we use expunging, and this article is really
7064             ;; low-scored, then we don't want this article.
7065             (when (and gnus-summary-expunge-below
7066                        (< (setq score
7067                                 (or (cdr (assq number gnus-newsgroup-scored))
7068                                     gnus-summary-default-score))
7069                           gnus-summary-expunge-below))
7070               ;; We increase the expunge-tally here, but that has
7071               ;; nothing to do with the limits, really.
7072               (incf gnus-newsgroup-expunged-tally)
7073               ;; We also mark as read here, if that's wanted.
7074               (when (and gnus-summary-mark-below
7075                          (< score gnus-summary-mark-below))
7076                 (setq gnus-newsgroup-unreads
7077                       (delq number gnus-newsgroup-unreads))
7078                 (if gnus-newsgroup-auto-expire
7079                     (push number gnus-newsgroup-expirable)
7080                   (push (cons number gnus-low-score-mark)
7081                         gnus-newsgroup-reads)))
7082               t)
7083             ;; Check NoCeM things.
7084             (if (and gnus-use-nocem
7085                      (gnus-nocem-unwanted-article-p
7086                       (mail-header-id (car thread))))
7087                 (progn
7088                   (setq gnus-newsgroup-unreads
7089                         (delq number gnus-newsgroup-unreads))
7090                   t))))
7091           ;; Nope, invisible article.
7092           0
7093         ;; Ok, this article is to be visible, so we add it to the limit
7094         ;; and return 1.
7095         (push number gnus-newsgroup-limit)
7096         1))))
7097
7098 (defun gnus-expunge-thread (thread)
7099   "Mark all articles in THREAD as read."
7100   (let* ((number (mail-header-number (car thread))))
7101     (incf gnus-newsgroup-expunged-tally)
7102     ;; We also mark as read here, if that's wanted.
7103     (setq gnus-newsgroup-unreads
7104           (delq number gnus-newsgroup-unreads))
7105     (if gnus-newsgroup-auto-expire
7106         (push number gnus-newsgroup-expirable)
7107       (push (cons number gnus-low-score-mark)
7108             gnus-newsgroup-reads)))
7109   ;; Go recursively through all subthreads.
7110   (mapcar 'gnus-expunge-thread (cdr thread)))
7111
7112 ;; Summary article oriented commands
7113
7114 (defun gnus-summary-refer-parent-article (n)
7115   "Refer parent article N times.
7116 If N is negative, go to ancestor -N instead.
7117 The difference between N and the number of articles fetched is returned."
7118   (interactive "p")
7119   (let ((skip 1)
7120         error header ref)
7121     (when (not (natnump n))
7122       (setq skip (abs n)
7123             n 1))
7124     (while (and (> n 0)
7125                 (not error))
7126       (setq header (gnus-summary-article-header))
7127       (if (and (eq (mail-header-number header)
7128                    (cdr gnus-article-current))
7129                (equal gnus-newsgroup-name
7130                       (car gnus-article-current)))
7131           ;; If we try to find the parent of the currently
7132           ;; displayed article, then we take a look at the actual
7133           ;; References header, since this is slightly more
7134           ;; reliable than the References field we got from the
7135           ;; server.
7136           (save-excursion
7137             (set-buffer gnus-original-article-buffer)
7138             (nnheader-narrow-to-headers)
7139             (unless (setq ref (message-fetch-field "references"))
7140               (setq ref (message-fetch-field "in-reply-to")))
7141             (widen))
7142         (setq ref
7143               ;; It's not the current article, so we take a bet on
7144               ;; the value we got from the server.
7145               (mail-header-references header)))
7146       (if (and ref
7147                (not (equal ref "")))
7148           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7149             (gnus-message 1 "Couldn't find parent"))
7150         (gnus-message 1 "No references in article %d"
7151                       (gnus-summary-article-number))
7152         (setq error t))
7153       (decf n))
7154     (gnus-summary-position-point)
7155     n))
7156
7157 (defun gnus-summary-refer-references ()
7158   "Fetch all articles mentioned in the References header.
7159 Return the number of articles fetched."
7160   (interactive)
7161   (let ((ref (mail-header-references (gnus-summary-article-header)))
7162         (current (gnus-summary-article-number))
7163         (n 0))
7164     (if (or (not ref)
7165             (equal ref ""))
7166         (error "No References in the current article")
7167       ;; For each Message-ID in the References header...
7168       (while (string-match "<[^>]*>" ref)
7169         (incf n)
7170         ;; ... fetch that article.
7171         (gnus-summary-refer-article
7172          (prog1 (match-string 0 ref)
7173            (setq ref (substring ref (match-end 0))))))
7174       (gnus-summary-goto-subject current)
7175       (gnus-summary-position-point)
7176       n)))
7177
7178 (defun gnus-summary-refer-thread (&optional limit)
7179   "Fetch all articles in the current thread.
7180 If LIMIT (the numerical prefix), fetch that many old headers instead
7181 of what's specified by the `gnus-refer-thread-limit' variable."
7182   (interactive "P")
7183   (let ((id (mail-header-id (gnus-summary-article-header)))
7184         (limit (if limit (prefix-numeric-value limit)
7185                  gnus-refer-thread-limit)))
7186     ;; We want to fetch LIMIT *old* headers, but we also have to
7187     ;; re-fetch all the headers in the current buffer, because many of
7188     ;; them may be undisplayed.  So we adjust LIMIT.
7189     (when (numberp limit)
7190       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7191     (unless (eq gnus-fetch-old-headers 'invisible)
7192       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7193       ;; Retrieve the headers and read them in.
7194       (if (eq (gnus-retrieve-headers
7195                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7196               'nov)
7197           (gnus-build-all-threads)
7198         (error "Can't fetch thread from backends that don't support NOV"))
7199       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7200     (gnus-summary-limit-include-thread id)))
7201
7202 (defun gnus-summary-refer-article (message-id)
7203   "Fetch an article specified by MESSAGE-ID."
7204   (interactive "sMessage-ID: ")
7205   (when (and (stringp message-id)
7206              (not (zerop (length message-id))))
7207     ;; Construct the correct Message-ID if necessary.
7208     ;; Suggested by tale@pawl.rpi.edu.
7209     (unless (string-match "^<" message-id)
7210       (setq message-id (concat "<" message-id)))
7211     (unless (string-match ">$" message-id)
7212       (setq message-id (concat message-id ">")))
7213     (let* ((header (gnus-id-to-header message-id))
7214            (sparse (and header
7215                         (gnus-summary-article-sparse-p
7216                          (mail-header-number header))
7217                         (memq (mail-header-number header)
7218                               gnus-newsgroup-limit)))
7219            number)
7220       (cond
7221        ;; If the article is present in the buffer we just go to it.
7222        ((and header
7223              (or (not (gnus-summary-article-sparse-p
7224                        (mail-header-number header)))
7225                  sparse))
7226         (prog1
7227             (gnus-summary-goto-article
7228              (mail-header-number header) nil t)
7229           (when sparse
7230             (gnus-summary-update-article (mail-header-number header)))))
7231        (t
7232         ;; We fetch the article.
7233         (catch 'found
7234           (dolist (gnus-override-method (gnus-refer-article-methods))
7235             (gnus-check-server gnus-override-method)
7236             ;; Fetch the header, and display the article.
7237             (when (setq number (gnus-summary-insert-subject message-id))
7238               (gnus-summary-select-article nil nil nil number)
7239               (throw 'found t)))
7240           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7241
7242 (defun gnus-refer-article-methods ()
7243   "Return a list of referrable methods."
7244   (cond
7245    ;; No method, so we default to current and native.
7246    ((null gnus-refer-article-method)
7247     (list gnus-current-select-method gnus-select-method))
7248    ;; Current.
7249    ((eq 'current gnus-refer-article-method)
7250     (list gnus-current-select-method))
7251    ;; List of select methods.
7252    ((not (and (symbolp (car gnus-refer-article-method))
7253               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7254     (let (out)
7255       (dolist (method gnus-refer-article-method)
7256         (push (if (eq 'current method)
7257                   gnus-current-select-method
7258                 method)
7259               out))
7260       (nreverse out)))
7261    ;; One single select method.
7262    (t
7263     (list gnus-refer-article-method))))
7264
7265 (defun gnus-summary-edit-parameters ()
7266   "Edit the group parameters of the current group."
7267   (interactive)
7268   (gnus-group-edit-group gnus-newsgroup-name 'params))
7269
7270 (defun gnus-summary-customize-parameters ()
7271   "Customize the group parameters of the current group."
7272   (interactive)
7273   (gnus-group-customize gnus-newsgroup-name))
7274
7275 (defun gnus-summary-enter-digest-group (&optional force)
7276   "Enter an nndoc group based on the current article.
7277 If FORCE, force a digest interpretation.  If not, try
7278 to guess what the document format is."
7279   (interactive "P")
7280   (let ((conf gnus-current-window-configuration))
7281     (save-excursion
7282       (gnus-summary-select-article))
7283     (setq gnus-current-window-configuration conf)
7284     (let* ((name (format "%s-%d"
7285                          (gnus-group-prefixed-name
7286                           gnus-newsgroup-name (list 'nndoc ""))
7287                          (save-excursion
7288                            (set-buffer gnus-summary-buffer)
7289                            gnus-current-article)))
7290            (ogroup gnus-newsgroup-name)
7291            (params (append (gnus-info-params (gnus-get-info ogroup))
7292                            (list (cons 'to-group ogroup))
7293                            (list (cons 'save-article-group ogroup))))
7294            (case-fold-search t)
7295            (buf (current-buffer))
7296            dig to-address)
7297       (save-excursion
7298         (set-buffer gnus-original-article-buffer)
7299         ;; Have the digest group inherit the main mail address of
7300         ;; the parent article.
7301         (when (setq to-address (or (message-fetch-field "reply-to")
7302                                    (message-fetch-field "from")))
7303           (setq params (append
7304                         (list (cons 'to-address
7305                                     (funcall gnus-decode-encoded-word-function
7306                                              to-address))))))
7307         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7308         (insert-buffer-substring gnus-original-article-buffer)
7309         ;; Remove lines that may lead nndoc to misinterpret the
7310         ;; document type.
7311         (narrow-to-region
7312          (goto-char (point-min))
7313          (or (search-forward "\n\n" nil t) (point)))
7314         (goto-char (point-min))
7315         (delete-matching-lines "^Path:\\|^From ")
7316         (widen))
7317       (unwind-protect
7318           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7319                     (gnus-newsgroup-ephemeral-ignored-charsets
7320                      gnus-newsgroup-ignored-charsets))
7321                 (gnus-group-read-ephemeral-group
7322                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7323                               (nndoc-article-type
7324                                ,(if force 'mbox 'guess))) t))
7325               ;; Make all postings to this group go to the parent group.
7326               (nconc (gnus-info-params (gnus-get-info name))
7327                      params)
7328             ;; Couldn't select this doc group.
7329             (switch-to-buffer buf)
7330             (gnus-set-global-variables)
7331             (gnus-configure-windows 'summary)
7332             (gnus-message 3 "Article couldn't be entered?"))
7333         (kill-buffer dig)))))
7334
7335 (defun gnus-summary-read-document (n)
7336   "Open a new group based on the current article(s).
7337 This will allow you to read digests and other similar
7338 documents as newsgroups.
7339 Obeys the standard process/prefix convention."
7340   (interactive "P")
7341   (let* ((articles (gnus-summary-work-articles n))
7342          (ogroup gnus-newsgroup-name)
7343          (params (append (gnus-info-params (gnus-get-info ogroup))
7344                          (list (cons 'to-group ogroup))))
7345          article group egroup groups vgroup)
7346     (while (setq article (pop articles))
7347       (setq group (format "%s-%d" gnus-newsgroup-name article))
7348       (gnus-summary-remove-process-mark article)
7349       (when (gnus-summary-display-article article)
7350         (save-excursion
7351           (with-temp-buffer
7352             (insert-buffer-substring gnus-original-article-buffer)
7353             ;; Remove some headers that may lead nndoc to make
7354             ;; the wrong guess.
7355             (message-narrow-to-head)
7356             (goto-char (point-min))
7357             (delete-matching-lines "^\\(Path\\):\\|^From ")
7358             (widen)
7359             (if (setq egroup
7360                       (gnus-group-read-ephemeral-group
7361                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7362                                      (nndoc-article-type guess))
7363                        t nil t))
7364                 (progn
7365                   ;; Make all postings to this group go to the parent group.
7366                   (nconc (gnus-info-params (gnus-get-info egroup))
7367                          params)
7368                   (push egroup groups))
7369               ;; Couldn't select this doc group.
7370               (gnus-error 3 "Article couldn't be entered"))))))
7371     ;; Now we have selected all the documents.
7372     (cond
7373      ((not groups)
7374       (error "None of the articles could be interpreted as documents"))
7375      ((gnus-group-read-ephemeral-group
7376        (setq vgroup (format
7377                      "nnvirtual:%s-%s" gnus-newsgroup-name
7378                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7379        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7380        t
7381        (cons (current-buffer) 'summary)))
7382      (t
7383       (error "Couldn't select virtual nndoc group")))))
7384
7385 (defun gnus-summary-isearch-article (&optional regexp-p)
7386   "Do incremental search forward on the current article.
7387 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7388   (interactive "P")
7389   (let* ((gnus-inhibit-treatment t)
7390          (old (gnus-summary-select-article)))
7391     (gnus-configure-windows 'article)
7392     (gnus-eval-in-buffer-window gnus-article-buffer
7393       (save-restriction
7394         (widen)
7395         (when (eq 'old old)
7396           (gnus-article-show-all-headers))
7397         (goto-char (point-min))
7398         (isearch-forward regexp-p)))))
7399
7400 (defun gnus-summary-search-article-forward (regexp &optional backward)
7401   "Search for an article containing REGEXP forward.
7402 If BACKWARD, search backward instead."
7403   (interactive
7404    (list (read-string
7405           (format "Search article %s (regexp%s): "
7406                   (if current-prefix-arg "backward" "forward")
7407                   (if gnus-last-search-regexp
7408                       (concat ", default " gnus-last-search-regexp)
7409                     "")))
7410          current-prefix-arg))
7411   (if (string-equal regexp "")
7412       (setq regexp (or gnus-last-search-regexp ""))
7413     (setq gnus-last-search-regexp regexp)
7414     (setq gnus-article-before-search gnus-current-article))
7415   ;; Intentionally set gnus-last-article.
7416   (setq gnus-last-article gnus-article-before-search)
7417   (let ((gnus-last-article gnus-last-article))
7418     (if (gnus-summary-search-article regexp backward)
7419         (gnus-summary-show-thread)
7420       (error "Search failed: \"%s\"" regexp))))
7421
7422 (defun gnus-summary-search-article-backward (regexp)
7423   "Search for an article containing REGEXP backward."
7424   (interactive
7425    (list (read-string
7426           (format "Search article backward (regexp%s): "
7427                   (if gnus-last-search-regexp
7428                       (concat ", default " gnus-last-search-regexp)
7429                     "")))))
7430   (gnus-summary-search-article-forward regexp 'backward))
7431
7432 (eval-when-compile
7433   (defmacro gnus-summary-search-article-position-point (regexp backward)
7434     "Dehighlight the last matched text and goto the beginning position."
7435     (` (if (and gnus-summary-search-article-matched-data
7436                 (let ((text (caddr gnus-summary-search-article-matched-data))
7437                       (inhibit-read-only t)
7438                       buffer-read-only)
7439                   (delete-region
7440                    (goto-char (car gnus-summary-search-article-matched-data))
7441                    (cadr gnus-summary-search-article-matched-data))
7442                   (insert text)
7443                   (string-match (, regexp) text)))
7444            (if (, backward) (beginning-of-line) (end-of-line))
7445          (goto-char (if (, backward) (point-max) (point-min))))))
7446
7447   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7448     "Place point where X-Face image is displayed."
7449     (if (featurep 'xemacs)
7450         (` (let ((end (if (search-forward "\n\n" nil t)
7451                           (goto-char (1- (point)))
7452                         (point-min)))
7453                  extent)
7454              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7455              (unless (and (re-search-forward "^From:" end t)
7456                           (setq extent (extent-at (point)))
7457                           (extent-begin-glyph extent))
7458                (goto-char (, opoint)))))
7459       (` (let ((end (if (search-forward "\n\n" nil t)
7460                         (goto-char (1- (point)))
7461                       (point-min)))
7462                (start (or (search-backward "\n\n" nil t) (point-min))))
7463            (goto-char
7464             (or (text-property-any start end 'x-face-image t);; x-face-e21
7465                 (text-property-any start end 'x-face-mule-bitmap-image t)
7466                 (, opoint)))))))
7467
7468   (defmacro gnus-summary-search-article-highlight-matched-text
7469     (backward treated x-face)
7470     "Highlight matched text in the function `gnus-summary-search-article'."
7471     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7472              (end (set-marker (make-marker) (match-end 0)))
7473              (inhibit-read-only t)
7474              buffer-read-only)
7475          (unless treated
7476            (let ((,@
7477                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7478                     (mapcar
7479                      (lambda (item) (setq items (delq item items)))
7480                      '(gnus-treat-buttonize
7481                        gnus-treat-fill-article
7482                        gnus-treat-fill-long-lines
7483                        gnus-treat-emphasize
7484                        gnus-treat-highlight-headers
7485                        gnus-treat-highlight-citation
7486                        gnus-treat-highlight-signature
7487                        gnus-treat-overstrike
7488                        gnus-treat-display-xface
7489                        gnus-treat-buttonize-head
7490                        gnus-treat-decode-article-as-default-mime-charset))
7491                     (static-if (featurep 'xemacs)
7492                         items
7493                       (cons '(x-face-mule-delete-x-face-field
7494                               (quote never))
7495                             items))))
7496                  (gnus-treat-display-xface
7497                   (when (, x-face) gnus-treat-display-xface)))
7498              (gnus-article-prepare-mime-display)))
7499          (goto-char (if (, backward) start end))
7500          (when (, x-face)
7501            (gnus-summary-search-article-highlight-goto-x-face (point)))
7502          (setq gnus-summary-search-article-matched-data
7503                (list start end (buffer-substring start end)))
7504          (unless (eq start end);; matched text has been deleted. :-<
7505            (put-text-property start end 'face
7506                               (or (find-face 'isearch)
7507                                   'secondary-selection))))))
7508   )
7509
7510 (defun gnus-summary-search-article (regexp &optional backward)
7511   "Search for an article containing REGEXP.
7512 Optional argument BACKWARD means do search for backward.
7513 `gnus-select-article-hook' is not called during the search."
7514   ;; We have to require this here to make sure that the following
7515   ;; dynamic binding isn't shadowed by autoloading.
7516   (require 'gnus-async)
7517   (require 'gnus-art)
7518   (let ((gnus-select-article-hook nil)  ;Disable hook.
7519         (gnus-article-prepare-hook nil)
7520         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7521         (gnus-use-article-prefetch nil)
7522         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7523         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7524         (sum (current-buffer))
7525         (found nil)
7526         point treated)
7527     (gnus-save-hidden-threads
7528       (static-if (featurep 'xemacs)
7529           (let ((gnus-inhibit-treatment t))
7530             (setq treated (eq 'old (gnus-summary-select-article)))
7531             (when (and treated
7532                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7533                                  (window-live-p (get-buffer-window
7534                                                  gnus-article-buffer t)))))
7535               (gnus-summary-select-article nil t)
7536               (setq treated nil)))
7537         (let ((gnus-inhibit-treatment t)
7538               (x-face-mule-delete-x-face-field 'never))
7539           (setq treated (eq 'old (gnus-summary-select-article)))
7540           (when (and treated
7541                      (not
7542                       (and (gnus-buffer-live-p gnus-article-buffer)
7543                            (window-live-p (get-buffer-window
7544                                            gnus-article-buffer t))
7545                            (or (not (string-match "^\\^X-Face:" regexp))
7546                                (with-current-buffer gnus-article-buffer
7547                                  gnus-summary-search-article-matched-data)))))
7548             (gnus-summary-select-article nil t)
7549             (setq treated nil))))
7550       (set-buffer gnus-article-buffer)
7551       (widen)
7552       (if treated
7553           (progn
7554             (gnus-article-show-all-headers)
7555             (gnus-summary-search-article-position-point regexp backward))
7556         (goto-char (if backward (point-max) (point-min))))
7557       (while (not found)
7558         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7559         (if (if backward
7560                 (re-search-backward regexp nil t)
7561               (re-search-forward regexp nil t))
7562             ;; We found the regexp.
7563             (progn
7564               (gnus-summary-search-article-highlight-matched-text
7565                backward treated (string-match "^\\^X-Face:" regexp))
7566               (setq found 'found)
7567               (forward-line
7568                (/ (- 2 (window-height
7569                         (get-buffer-window gnus-article-buffer t)))
7570                   2))
7571               (set-window-start
7572                (get-buffer-window (current-buffer))
7573                (point))
7574               (set-buffer sum)
7575               (setq point (point)))
7576           ;; We didn't find it, so we go to the next article.
7577           (set-buffer sum)
7578           (setq found 'not)
7579           (while (eq found 'not)
7580             (if (not (if backward (gnus-summary-find-prev)
7581                        (gnus-summary-find-next)))
7582                 ;; No more articles.
7583                 (setq found t)
7584               ;; Select the next article and adjust point.
7585               (unless (gnus-summary-article-sparse-p
7586                        (gnus-summary-article-number))
7587                 (setq found nil)
7588                 (let ((gnus-inhibit-treatment t))
7589                   (gnus-summary-select-article))
7590                 (setq treated nil)
7591                 (set-buffer gnus-article-buffer)
7592                 (widen)
7593                 (goto-char (if backward (point-max) (point-min))))))))
7594       (gnus-message 7 ""))
7595     ;; Return whether we found the regexp.
7596     (when (eq found 'found)
7597       (goto-char point)
7598       (gnus-summary-show-thread)
7599       (gnus-summary-goto-subject gnus-current-article)
7600       (gnus-summary-position-point)
7601       t)))
7602
7603 (defun gnus-find-matching-articles (header regexp)
7604   "Return a list of all articles that match REGEXP on HEADER.
7605 This search includes all articles in the current group that Gnus has
7606 fetched headers for, whether they are displayed or not."
7607   (let ((articles nil)
7608         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7609         (case-fold-search t))
7610     (dolist (header gnus-newsgroup-headers)
7611       (when (string-match regexp (funcall func header))
7612         (push (mail-header-number header) articles)))
7613     (nreverse articles)))
7614
7615 (defun gnus-summary-find-matching (header regexp &optional backward unread
7616                                           not-case-fold)
7617   "Return a list of all articles that match REGEXP on HEADER.
7618 The search stars on the current article and goes forwards unless
7619 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7620 If UNREAD is non-nil, only unread articles will
7621 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7622 in the comparisons."
7623   (let ((case-fold-search (not not-case-fold))
7624         articles d func)
7625     (if (consp header)
7626         (if (eq (car header) 'extra)
7627             (setq func
7628                   `(lambda (h)
7629                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7630                          "")))
7631           (error "%s is an invalid header" header))
7632       (unless (fboundp (intern (concat "mail-header-" header)))
7633         (error "%s is not a valid header" header))
7634       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7635     (dolist (d (if (eq backward 'all)
7636                    gnus-newsgroup-data
7637                  (gnus-data-find-list
7638                   (gnus-summary-article-number)
7639                   (gnus-data-list backward))))
7640       (when (and (or (not unread)       ; We want all articles...
7641                      (gnus-data-unread-p d)) ; Or just unreads.
7642                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7643                  (string-match regexp
7644                                (funcall func (gnus-data-header d)))) ; Match.
7645         (push (gnus-data-number d) articles))) ; Success!
7646     (nreverse articles)))
7647
7648 (defun gnus-summary-execute-command (header regexp command &optional backward)
7649   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7650 If HEADER is an empty string (or nil), the match is done on the entire
7651 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7652   (interactive
7653    (list (let ((completion-ignore-case t))
7654            (completing-read
7655             "Header name: "
7656             (mapcar (lambda (header) (list (format "%s" header)))
7657                     (append
7658                      '("Number" "Subject" "From" "Lines" "Date"
7659                        "Message-ID" "Xref" "References" "Body")
7660                      gnus-extra-headers))
7661             nil 'require-match))
7662          (read-string "Regexp: ")
7663          (read-key-sequence "Command: ")
7664          current-prefix-arg))
7665   (when (equal header "Body")
7666     (setq header ""))
7667   ;; Hidden thread subtrees must be searched as well.
7668   (gnus-summary-show-all-threads)
7669   ;; We don't want to change current point nor window configuration.
7670   (save-excursion
7671     (save-window-excursion
7672       (gnus-message 6 "Executing %s..." (key-description command))
7673       ;; We'd like to execute COMMAND interactively so as to give arguments.
7674       (gnus-execute header regexp
7675                     `(call-interactively ',(key-binding command))
7676                     backward)
7677       (gnus-message 6 "Executing %s...done" (key-description command)))))
7678
7679 (defun gnus-summary-beginning-of-article ()
7680   "Scroll the article back to the beginning."
7681   (interactive)
7682   (gnus-summary-select-article)
7683   (gnus-configure-windows 'article)
7684   (gnus-eval-in-buffer-window gnus-article-buffer
7685     (widen)
7686     (goto-char (point-min))
7687     (when gnus-page-broken
7688       (gnus-narrow-to-page))))
7689
7690 (defun gnus-summary-end-of-article ()
7691   "Scroll to the end of the article."
7692   (interactive)
7693   (gnus-summary-select-article)
7694   (gnus-configure-windows 'article)
7695   (gnus-eval-in-buffer-window gnus-article-buffer
7696     (widen)
7697     (goto-char (point-max))
7698     (recenter -3)
7699     (when gnus-page-broken
7700       (gnus-narrow-to-page))))
7701
7702 (defun gnus-summary-print-article (&optional filename n)
7703   "Generate and print a PostScript image of the N next (mail) articles.
7704
7705 If N is negative, print the N previous articles.  If N is nil and articles
7706 have been marked with the process mark, print these instead.
7707
7708 If the optional first argument FILENAME is nil, send the image to the
7709 printer.  If FILENAME is a string, save the PostScript image in a file with
7710 that name.  If FILENAME is a number, prompt the user for the name of the file
7711 to save in."
7712   (interactive (list (ps-print-preprint current-prefix-arg)))
7713   (dolist (article (gnus-summary-work-articles n))
7714     (gnus-summary-select-article nil nil 'pseudo article)
7715     (gnus-eval-in-buffer-window gnus-article-buffer
7716       (let ((buffer (generate-new-buffer " *print*")))
7717         (unwind-protect
7718             (progn
7719               (copy-to-buffer buffer (point-min) (point-max))
7720               (set-buffer buffer)
7721               (gnus-article-delete-invisible-text)
7722               (when (gnus-visual-p 'article-highlight 'highlight)
7723                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7724                 ;; highlight.
7725                 (let ((gnus-article-buffer buffer))
7726                   (gnus-article-highlight-citation t)
7727                   (gnus-article-highlight-signature)))
7728               (let ((ps-left-header
7729                      (list
7730                       (concat "("
7731                               (mail-header-subject gnus-current-headers) ")")
7732                       (concat "("
7733                               (mail-header-from gnus-current-headers) ")")))
7734                     (ps-right-header
7735                      (list
7736                       "/pagenumberstring load"
7737                       (concat "("
7738                               (mail-header-date gnus-current-headers) ")"))))
7739                 (gnus-run-hooks 'gnus-ps-print-hook)
7740                 (save-excursion
7741                   (if window-system
7742                       (ps-spool-buffer-with-faces)
7743                     (ps-spool-buffer)))))
7744           (kill-buffer buffer))))
7745     (gnus-summary-remove-process-mark article))
7746   (ps-despool filename))
7747
7748 (defun gnus-summary-show-article (&optional arg)
7749   "Force re-fetching of the current article.
7750 If ARG (the prefix) is a number, show the article with the charset
7751 defined in `gnus-summary-show-article-charset-alist', or the charset
7752 inputed.
7753 If ARG (the prefix) is non-nil and not a number, show the raw article
7754 without any article massaging functions being run."
7755   (interactive "P")
7756   (cond
7757    ((numberp arg)
7758     (let ((gnus-newsgroup-charset
7759            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7760                (read-coding-system "Charset: ")))
7761           (gnus-newsgroup-ignored-charsets 'gnus-all))
7762       (gnus-summary-select-article nil 'force)
7763       (let ((deps gnus-newsgroup-dependencies)
7764             head header)
7765         (save-excursion
7766           (set-buffer gnus-original-article-buffer)
7767           (save-restriction
7768             (message-narrow-to-head)
7769             (setq head (buffer-string)))
7770           (with-temp-buffer
7771             (insert (format "211 %d Article retrieved.\n"
7772                             (cdr gnus-article-current)))
7773             (insert head)
7774             (insert ".\n")
7775             (let ((nntp-server-buffer (current-buffer)))
7776               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7777         (gnus-data-set-header
7778          (gnus-data-find (cdr gnus-article-current))
7779          header)
7780         (gnus-summary-update-article-line
7781          (cdr gnus-article-current) header))))
7782    ((not arg)
7783     ;; Select the article the normal way.
7784     (gnus-summary-select-article nil 'force))
7785    (t
7786     ;; We have to require this here to make sure that the following
7787     ;; dynamic binding isn't shadowed by autoloading.
7788     (require 'gnus-async)
7789     (require 'gnus-art)
7790     ;; Bind the article treatment functions to nil.
7791     (let ((gnus-have-all-headers t)
7792           gnus-article-prepare-hook
7793           gnus-article-decode-hook
7794           gnus-break-pages
7795           gnus-show-mime
7796           (gnus-inhibit-treatment t))
7797       (gnus-summary-select-article nil 'force))))
7798   (gnus-summary-goto-subject gnus-current-article)
7799   (gnus-summary-position-point))
7800
7801 (defun gnus-summary-verbose-headers (&optional arg)
7802   "Toggle permanent full header display.
7803 If ARG is a positive number, turn header display on.
7804 If ARG is a negative number, turn header display off."
7805   (interactive "P")
7806   (setq gnus-show-all-headers
7807         (cond ((or (not (numberp arg))
7808                    (zerop arg))
7809                (not gnus-show-all-headers))
7810               ((natnump arg)
7811                t)))
7812   (gnus-summary-show-article))
7813
7814 (defun gnus-summary-toggle-header (&optional arg)
7815   "Show the headers if they are hidden, or hide them if they are shown.
7816 If ARG is a positive number, show the entire header.
7817 If ARG is a negative number, hide the unwanted header lines."
7818   (interactive "P")
7819   (save-excursion
7820     (set-buffer gnus-article-buffer)
7821     (save-restriction
7822       (let* ((buffer-read-only nil)
7823              (inhibit-point-motion-hooks t)
7824              hidden e)
7825         (setq hidden
7826               (if (numberp arg)
7827                   (>= arg 0)
7828                 (save-restriction
7829                   (article-narrow-to-head)
7830                   (gnus-article-hidden-text-p 'headers))))
7831         (goto-char (point-min))
7832         (when (search-forward "\n\n" nil t)
7833           (delete-region (point-min) (1- (point))))
7834         (goto-char (point-min))
7835         (save-excursion
7836           (set-buffer gnus-original-article-buffer)
7837           (goto-char (point-min))
7838           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7839         (insert-buffer-substring gnus-original-article-buffer 1 e)
7840         (save-restriction
7841           (narrow-to-region (point-min) (point))
7842           (article-decode-encoded-words)
7843           (if  hidden
7844               (let ((gnus-treat-hide-headers nil)
7845                     (gnus-treat-hide-boring-headers nil))
7846                 (setq gnus-article-wash-types
7847                       (delq 'headers gnus-article-wash-types))
7848                 (gnus-treat-article 'head))
7849             (gnus-treat-article 'head)))
7850         (gnus-set-mode-line 'article)))))
7851
7852 (defun gnus-summary-show-all-headers ()
7853   "Make all header lines visible."
7854   (interactive)
7855   (gnus-summary-toggle-header 1))
7856
7857 (defun gnus-summary-toggle-mime (&optional arg)
7858   "Toggle MIME processing.
7859 If ARG is a positive number, turn MIME processing on."
7860   (interactive "P")
7861   (setq gnus-show-mime
7862         (if (null arg)
7863             (not gnus-show-mime)
7864           (> (prefix-numeric-value arg) 0)))
7865   (gnus-summary-select-article t 'force))
7866
7867 (defun gnus-summary-caesar-message (&optional arg)
7868   "Caesar rotate the current article by 13.
7869 The numerical prefix specifies how many places to rotate each letter
7870 forward."
7871   (interactive "P")
7872   (gnus-summary-select-article)
7873   (let ((mail-header-separator ""))
7874     (gnus-eval-in-buffer-window gnus-article-buffer
7875       (save-restriction
7876         (widen)
7877         (let ((start (window-start))
7878               buffer-read-only)
7879           (message-caesar-buffer-body arg)
7880           (set-window-start (get-buffer-window (current-buffer)) start))))))
7881
7882 (defun gnus-summary-stop-page-breaking ()
7883   "Stop page breaking in the current article."
7884   (interactive)
7885   (gnus-summary-select-article)
7886   (gnus-eval-in-buffer-window gnus-article-buffer
7887     (widen)
7888     (when (gnus-visual-p 'page-marker)
7889       (let ((buffer-read-only nil))
7890         (gnus-remove-text-with-property 'gnus-prev)
7891         (gnus-remove-text-with-property 'gnus-next))
7892       (setq gnus-page-broken nil))))
7893
7894 (defun gnus-summary-move-article (&optional n to-newsgroup
7895                                             select-method action)
7896   "Move the current article to a different newsgroup.
7897 If N is a positive number, move the N next articles.
7898 If N is a negative number, move the N previous articles.
7899 If N is nil and any articles have been marked with the process mark,
7900 move those articles instead.
7901 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7902 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7903 re-spool using this method.
7904
7905 For this function to work, both the current newsgroup and the
7906 newsgroup that you want to move to have to support the `request-move'
7907 and `request-accept' functions.
7908
7909 ACTION can be either `move' (the default), `crosspost' or `copy'."
7910   (interactive "P")
7911   (unless action
7912     (setq action 'move))
7913   ;; Disable marking as read.
7914   (let (gnus-mark-article-hook)
7915     (save-window-excursion
7916       (gnus-summary-select-article)))
7917   ;; Check whether the source group supports the required functions.
7918   (cond ((and (eq action 'move)
7919               (not (gnus-check-backend-function
7920                     'request-move-article gnus-newsgroup-name)))
7921          (error "The current group does not support article moving"))
7922         ((and (eq action 'crosspost)
7923               (not (gnus-check-backend-function
7924                     'request-replace-article gnus-newsgroup-name)))
7925          (error "The current group does not support article editing")))
7926   (let ((articles (gnus-summary-work-articles n))
7927         (prefix (if (gnus-check-backend-function
7928                     'request-move-article gnus-newsgroup-name)
7929                     (gnus-group-real-prefix gnus-newsgroup-name)
7930                   ""))
7931         (names '((move "Move" "Moving")
7932                  (copy "Copy" "Copying")
7933                  (crosspost "Crosspost" "Crossposting")))
7934         (copy-buf (save-excursion
7935                     (nnheader-set-temp-buffer " *copy article*")))
7936         (default-marks gnus-article-mark-lists)
7937         (no-expire-marks (delete '(expirable . expire)
7938                                  (copy-sequence gnus-article-mark-lists)))
7939         art-group to-method new-xref article to-groups)
7940     (unless (assq action names)
7941       (error "Unknown action %s" action))
7942     ;; Read the newsgroup name.
7943     (when (and (not to-newsgroup)
7944                (not select-method))
7945       (setq to-newsgroup
7946             (gnus-read-move-group-name
7947              (cadr (assq action names))
7948              (symbol-value (intern (format "gnus-current-%s-group" action)))
7949              articles prefix))
7950       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7951     (setq to-method (or select-method
7952                         (gnus-server-to-method
7953                          (gnus-group-method to-newsgroup))))
7954     ;; Check the method we are to move this article to...
7955     (unless (gnus-check-backend-function
7956              'request-accept-article (car to-method))
7957       (error "%s does not support article copying" (car to-method)))
7958     (unless (gnus-check-server to-method)
7959       (error "Can't open server %s" (car to-method)))
7960     (gnus-message 6 "%s to %s: %s..."
7961                   (caddr (assq action names))
7962                   (or (car select-method) to-newsgroup) articles)
7963     (while articles
7964       (setq article (pop articles))
7965       (setq
7966        art-group
7967        (cond
7968         ;; Move the article.
7969         ((eq action 'move)
7970          ;; Remove this article from future suppression.
7971          (gnus-dup-unsuppress-article article)
7972          (gnus-request-move-article
7973           article                       ; Article to move
7974           gnus-newsgroup-name           ; From newsgroup
7975           (nth 1 (gnus-find-method-for-group
7976                   gnus-newsgroup-name)) ; Server
7977           (list 'gnus-request-accept-article
7978                 to-newsgroup (list 'quote select-method)
7979                 (not articles) t)       ; Accept form
7980           (not articles)))              ; Only save nov last time
7981         ;; Copy the article.
7982         ((eq action 'copy)
7983          (save-excursion
7984            (set-buffer copy-buf)
7985            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7986              (gnus-request-accept-article
7987               to-newsgroup select-method (not articles) t))))
7988         ;; Crosspost the article.
7989         ((eq action 'crosspost)
7990          (let ((xref (message-tokenize-header
7991                       (mail-header-xref (gnus-summary-article-header article))
7992                       " ")))
7993            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7994                                   ":" article))
7995            (unless xref
7996              (setq xref (list (system-name))))
7997            (setq new-xref
7998                  (concat
7999                   (mapconcat 'identity
8000                              (delete "Xref:" (delete new-xref xref))
8001                              " ")
8002                   " " new-xref))
8003            (save-excursion
8004              (set-buffer copy-buf)
8005              ;; First put the article in the destination group.
8006              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8007              (when (consp (setq art-group
8008                                 (gnus-request-accept-article
8009                                  to-newsgroup select-method (not articles))))
8010                (setq new-xref (concat new-xref " " (car art-group)
8011                                       ":" (cdr art-group)))
8012                ;; Now we have the new Xrefs header, so we insert
8013                ;; it and replace the new article.
8014                (nnheader-replace-header "Xref" new-xref)
8015                (gnus-request-replace-article
8016                 (cdr art-group) to-newsgroup (current-buffer))
8017                art-group))))))
8018       (cond
8019        ((not art-group)
8020         (gnus-message 1 "Couldn't %s article %s: %s"
8021                       (cadr (assq action names)) article
8022                       (nnheader-get-report (car to-method))))
8023        ((eq art-group 'junk)
8024         (when (eq action 'move)
8025           (gnus-summary-mark-article article gnus-canceled-mark)
8026           (gnus-message 4 "Deleted article %s" article)))
8027        (t
8028         (let* ((pto-group (gnus-group-prefixed-name
8029                            (car art-group) to-method))
8030                (entry
8031                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8032                (info (nth 2 entry))
8033                (to-group (gnus-info-group info))
8034                to-marks)
8035           ;; Update the group that has been moved to.
8036           (when (and info
8037                      (memq action '(move copy)))
8038             (unless (member to-group to-groups)
8039               (push to-group to-groups))
8040
8041             (unless (memq article gnus-newsgroup-unreads)
8042               (push 'read to-marks)
8043               (gnus-info-set-read
8044                info (gnus-add-to-range (gnus-info-read info)
8045                                        (list (cdr art-group)))))
8046
8047             ;; See whether the article is to be put in the cache.
8048             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8049                              default-marks
8050                            no-expire-marks))
8051                   (to-article (cdr art-group)))
8052
8053               ;; Enter the article into the cache in the new group,
8054               ;; if that is required.
8055               (when gnus-use-cache
8056                 (gnus-cache-possibly-enter-article
8057                  to-group to-article
8058                  (let ((header (copy-sequence
8059                                 (gnus-summary-article-header article))))
8060                    (mail-header-set-number header to-article)
8061                    header)
8062                  (memq article gnus-newsgroup-marked)
8063                  (memq article gnus-newsgroup-dormant)
8064                  (memq article gnus-newsgroup-unreads)))
8065
8066               (when gnus-preserve-marks
8067                 ;; Copy any marks over to the new group.
8068                 (when (and (equal to-group gnus-newsgroup-name)
8069                            (not (memq article gnus-newsgroup-unreads)))
8070                   ;; Mark this article as read in this group.
8071                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8072                   (setcdr (gnus-active to-group) to-article)
8073                   (setcdr gnus-newsgroup-active to-article))
8074
8075                 (while marks
8076                   (when (memq article (symbol-value
8077                                        (intern (format "gnus-newsgroup-%s"
8078                                                        (caar marks)))))
8079                     (push (cdar marks) to-marks)
8080                     ;; If the other group is the same as this group,
8081                     ;; then we have to add the mark to the list.
8082                     (when (equal to-group gnus-newsgroup-name)
8083                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8084                            (cons to-article
8085                                  (symbol-value
8086                                   (intern (format "gnus-newsgroup-%s"
8087                                                   (caar marks)))))))
8088                     ;; Copy the marks to other group.
8089                     (gnus-add-marked-articles
8090                      to-group (cdar marks) (list to-article) info))
8091                   (setq marks (cdr marks)))
8092
8093                 (gnus-request-set-mark to-group (list (list (list to-article)
8094                                                             'set
8095                                                             to-marks))))
8096
8097               (gnus-dribble-enter
8098                (concat "(gnus-group-set-info '"
8099                        (gnus-prin1-to-string (gnus-get-info to-group))
8100                        ")"))))
8101
8102           ;; Update the Xref header in this article to point to
8103           ;; the new crossposted article we have just created.
8104           (when (eq action 'crosspost)
8105             (save-excursion
8106               (set-buffer copy-buf)
8107               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8108               (nnheader-replace-header "Xref" new-xref)
8109               (gnus-request-replace-article
8110                article gnus-newsgroup-name (current-buffer)))))
8111
8112         ;;;!!!Why is this necessary?
8113         (set-buffer gnus-summary-buffer)
8114
8115         (gnus-summary-goto-subject article)
8116         (when (eq action 'move)
8117           (gnus-summary-mark-article article gnus-canceled-mark))))
8118       (gnus-summary-remove-process-mark article))
8119     ;; Re-activate all groups that have been moved to.
8120     (while to-groups
8121       (save-excursion
8122         (set-buffer gnus-group-buffer)
8123         (when (gnus-group-goto-group (car to-groups) t)
8124           (gnus-group-get-new-news-this-group 1 t))
8125         (pop to-groups)))
8126
8127     (gnus-kill-buffer copy-buf)
8128     (gnus-summary-position-point)
8129     (gnus-set-mode-line 'summary)))
8130
8131 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8132   "Move the current article to a different newsgroup.
8133 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8134 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8135 re-spool using this method."
8136   (interactive "P")
8137   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8138
8139 (defun gnus-summary-crosspost-article (&optional n)
8140   "Crosspost the current article to some other group."
8141   (interactive "P")
8142   (gnus-summary-move-article n nil nil 'crosspost))
8143
8144 (defcustom gnus-summary-respool-default-method nil
8145   "Default method for respooling an article.
8146 If nil, use to the current newsgroup method."
8147   :type '(choice (gnus-select-method :value (nnml ""))
8148                  (const nil))
8149   :group 'gnus-summary-mail)
8150
8151 (defun gnus-summary-respool-article (&optional n method)
8152   "Respool the current article.
8153 The article will be squeezed through the mail spooling process again,
8154 which means that it will be put in some mail newsgroup or other
8155 depending on `nnmail-split-methods'.
8156 If N is a positive number, respool the N next articles.
8157 If N is a negative number, respool the N previous articles.
8158 If N is nil and any articles have been marked with the process mark,
8159 respool those articles instead.
8160
8161 Respooling can be done both from mail groups and \"real\" newsgroups.
8162 In the former case, the articles in question will be moved from the
8163 current group into whatever groups they are destined to.  In the
8164 latter case, they will be copied into the relevant groups."
8165   (interactive
8166    (list current-prefix-arg
8167          (let* ((methods (gnus-methods-using 'respool))
8168                 (methname
8169                  (symbol-name (or gnus-summary-respool-default-method
8170                                   (car (gnus-find-method-for-group
8171                                         gnus-newsgroup-name)))))
8172                 (method
8173                  (gnus-completing-read
8174                   methname "What backend do you want to use when respooling?"
8175                   methods nil t nil 'gnus-mail-method-history))
8176                 ms)
8177            (cond
8178             ((zerop (length (setq ms (gnus-servers-using-backend
8179                                       (intern method)))))
8180              (list (intern method) ""))
8181             ((= 1 (length ms))
8182              (car ms))
8183             (t
8184              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8185                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8186                            ms-alist))))))))
8187   (unless method
8188     (error "No method given for respooling"))
8189   (if (assoc (symbol-name
8190               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8191              (gnus-methods-using 'respool))
8192       (gnus-summary-move-article n nil method)
8193     (gnus-summary-copy-article n nil method)))
8194
8195 (defun gnus-summary-import-article (file &optional edit)
8196   "Import an arbitrary file into a mail newsgroup."
8197   (interactive "fImport file: \nP")
8198   (let ((group gnus-newsgroup-name)
8199         (now (current-time))
8200         atts lines group-art)
8201     (unless (gnus-check-backend-function 'request-accept-article group)
8202       (error "%s does not support article importing" group))
8203     (or (file-readable-p file)
8204         (not (file-regular-p file))
8205         (error "Can't read %s" file))
8206     (save-excursion
8207       (set-buffer (gnus-get-buffer-create " *import file*"))
8208       (erase-buffer)
8209       (nnheader-insert-file-contents file)
8210       (goto-char (point-min))
8211       (unless (nnheader-article-p)
8212         ;; This doesn't look like an article, so we fudge some headers.
8213         (setq atts (file-attributes file)
8214               lines (count-lines (point-min) (point-max)))
8215         (insert "From: " (read-string "From: ") "\n"
8216                 "Subject: " (read-string "Subject: ") "\n"
8217                 "Date: " (message-make-date (nth 5 atts)) "\n"
8218                 "Message-ID: " (message-make-message-id) "\n"
8219                 "Lines: " (int-to-string lines) "\n"
8220                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8221       (setq group-art (gnus-request-accept-article group nil t))
8222       (kill-buffer (current-buffer)))
8223     (setq gnus-newsgroup-active (gnus-activate-group group))
8224     (forward-line 1)
8225     (gnus-summary-goto-article (cdr group-art) nil t)
8226     (when edit
8227       (gnus-summary-edit-article))))
8228
8229 (defun gnus-summary-create-article ()
8230   "Create an article in a mail newsgroup."
8231   (interactive)
8232   (let ((group gnus-newsgroup-name)
8233         (now (current-time))
8234         group-art)
8235     (unless (gnus-check-backend-function 'request-accept-article group)
8236       (error "%s does not support article importing" group))
8237     (save-excursion
8238       (set-buffer (gnus-get-buffer-create " *import file*"))
8239       (erase-buffer)
8240       (goto-char (point-min))
8241       ;; This doesn't look like an article, so we fudge some headers.
8242       (insert "From: " (read-string "From: ") "\n"
8243               "Subject: " (read-string "Subject: ") "\n"
8244               "Date: " (message-make-date now) "\n"
8245               "Message-ID: " (message-make-message-id) "\n")
8246       (setq group-art (gnus-request-accept-article group nil t))
8247       (kill-buffer (current-buffer)))
8248     (setq gnus-newsgroup-active (gnus-activate-group group))
8249     (forward-line 1)
8250     (gnus-summary-goto-article (cdr group-art) nil t)
8251     (gnus-summary-edit-article)))
8252
8253 (defun gnus-summary-article-posted-p ()
8254   "Say whether the current (mail) article is available from news as well.
8255 This will be the case if the article has both been mailed and posted."
8256   (interactive)
8257   (let ((id (mail-header-references (gnus-summary-article-header)))
8258         (gnus-override-method (car (gnus-refer-article-methods))))
8259     (if (gnus-request-head id "")
8260         (gnus-message 2 "The current message was found on %s"
8261                       gnus-override-method)
8262       (gnus-message 2 "The current message couldn't be found on %s"
8263                     gnus-override-method)
8264       nil)))
8265
8266 (defun gnus-summary-expire-articles (&optional now)
8267   "Expire all articles that are marked as expirable in the current group."
8268   (interactive)
8269   (when (gnus-check-backend-function
8270          'request-expire-articles gnus-newsgroup-name)
8271     ;; This backend supports expiry.
8272     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8273            (expirable (if total
8274                           (progn
8275                             ;; We need to update the info for
8276                             ;; this group for `gnus-list-of-read-articles'
8277                             ;; to give us the right answer.
8278                             (gnus-run-hooks 'gnus-exit-group-hook)
8279                             (gnus-summary-update-info)
8280                             (gnus-list-of-read-articles gnus-newsgroup-name))
8281                         (setq gnus-newsgroup-expirable
8282                               (sort gnus-newsgroup-expirable '<))))
8283            (expiry-wait (if now 'immediate
8284                           (gnus-group-find-parameter
8285                            gnus-newsgroup-name 'expiry-wait)))
8286            (nnmail-expiry-target
8287             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8288                 nnmail-expiry-target))
8289            es)
8290       (when expirable
8291         ;; There are expirable articles in this group, so we run them
8292         ;; through the expiry process.
8293         (gnus-message 6 "Expiring articles...")
8294         (unless (gnus-check-group gnus-newsgroup-name)
8295           (error "Can't open server for %s" gnus-newsgroup-name))
8296         ;; The list of articles that weren't expired is returned.
8297         (save-excursion
8298           (if expiry-wait
8299               (let ((nnmail-expiry-wait-function nil)
8300                     (nnmail-expiry-wait expiry-wait))
8301                 (setq es (gnus-request-expire-articles
8302                           expirable gnus-newsgroup-name)))
8303             (setq es (gnus-request-expire-articles
8304                       expirable gnus-newsgroup-name)))
8305           (unless total
8306             (setq gnus-newsgroup-expirable es))
8307           ;; We go through the old list of expirable, and mark all
8308           ;; really expired articles as nonexistent.
8309           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8310             (let ((gnus-use-cache nil))
8311               (while expirable
8312                 (unless (memq (car expirable) es)
8313                   (when (gnus-data-find (car expirable))
8314                     (gnus-summary-mark-article
8315                      (car expirable) gnus-canceled-mark)))
8316                 (setq expirable (cdr expirable))))))
8317         (gnus-message 6 "Expiring articles...done")))))
8318
8319 (defun gnus-summary-expire-articles-now ()
8320   "Expunge all expirable articles in the current group.
8321 This means that *all* articles that are marked as expirable will be
8322 deleted forever, right now."
8323   (interactive)
8324   (or gnus-expert-user
8325       (gnus-yes-or-no-p
8326        "Are you really, really, really sure you want to delete all these messages? ")
8327       (error "Phew!"))
8328   (gnus-summary-expire-articles t))
8329
8330 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8331 (defun gnus-summary-delete-article (&optional n)
8332   "Delete the N next (mail) articles.
8333 This command actually deletes articles.  This is not a marking
8334 command.  The article will disappear forever from your life, never to
8335 return.
8336 If N is negative, delete backwards.
8337 If N is nil and articles have been marked with the process mark,
8338 delete these instead."
8339   (interactive "P")
8340   (unless (gnus-check-backend-function 'request-expire-articles
8341                                        gnus-newsgroup-name)
8342     (error "The current newsgroup does not support article deletion"))
8343   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8344     (error "Couldn't open server"))
8345   ;; Compute the list of articles to delete.
8346   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8347         not-deleted)
8348     (if (and gnus-novice-user
8349              (not (gnus-yes-or-no-p
8350                    (format "Do you really want to delete %s forever? "
8351                            (if (> (length articles) 1)
8352                                (format "these %s articles" (length articles))
8353                              "this article")))))
8354         ()
8355       ;; Delete the articles.
8356       (setq not-deleted (gnus-request-expire-articles
8357                          articles gnus-newsgroup-name 'force))
8358       (while articles
8359         (gnus-summary-remove-process-mark (car articles))
8360         ;; The backend might not have been able to delete the article
8361         ;; after all.
8362         (unless (memq (car articles) not-deleted)
8363           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8364         (setq articles (cdr articles)))
8365       (when not-deleted
8366         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8367     (gnus-summary-position-point)
8368     (gnus-set-mode-line 'summary)
8369     not-deleted))
8370
8371 (defun gnus-summary-edit-article (&optional force)
8372   "Edit the current article.
8373 This will have permanent effect only in mail groups.
8374 If FORCE is non-nil, allow editing of articles even in read-only
8375 groups."
8376   (interactive "P")
8377   (save-excursion
8378     (set-buffer gnus-summary-buffer)
8379     (let ((mail-parse-charset gnus-newsgroup-charset)
8380           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8381       (gnus-set-global-variables)
8382       (when (and (not force)
8383                  (gnus-group-read-only-p))
8384         (error "The current newsgroup does not support article editing"))
8385       (gnus-summary-show-article t)
8386       (gnus-article-edit-article
8387        'ignore
8388        `(lambda (no-highlight)
8389           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8390                 (message-options message-options)
8391                 (message-options-set-recipient)
8392                 (mail-parse-ignored-charsets
8393                  ',gnus-newsgroup-ignored-charsets))
8394             (gnus-summary-edit-article-done
8395              ,(or (mail-header-references gnus-current-headers) "")
8396              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8397
8398 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8399
8400 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8401                                                  no-highlight)
8402   "Make edits to the current article permanent."
8403   (interactive)
8404   (save-excursion
8405     ;; The buffer restriction contains the entire article if it exists.
8406     (when (article-goto-body)
8407       (let ((lines (count-lines (point) (point-max)))
8408             (length (- (point-max) (point)))
8409             (case-fold-search t)
8410             (body (copy-marker (point))))
8411         (goto-char (point-min))
8412         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8413           (delete-region (match-beginning 1) (match-end 1))
8414           (insert (number-to-string length)))
8415         (goto-char (point-min))
8416         (when (re-search-forward
8417                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8418           (delete-region (match-beginning 1) (match-end 1))
8419           (insert (number-to-string length)))
8420         (goto-char (point-min))
8421         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8422           (delete-region (match-beginning 1) (match-end 1))
8423           (insert (number-to-string lines))))))
8424   ;; Replace the article.
8425   (let ((buf (current-buffer)))
8426     (with-temp-buffer
8427       (insert-buffer-substring buf)
8428
8429       (if (and (not read-only)
8430                (not (gnus-request-replace-article
8431                      (cdr gnus-article-current) (car gnus-article-current)
8432                      (current-buffer) t)))
8433           (error "Couldn't replace article")
8434         ;; Update the summary buffer.
8435         (if (and references
8436                  (equal (message-tokenize-header references " ")
8437                         (message-tokenize-header
8438                          (or (message-fetch-field "references") "") " ")))
8439             ;; We only have to update this line.
8440             (save-excursion
8441               (save-restriction
8442                 (message-narrow-to-head)
8443                 (let ((head (buffer-string))
8444                       header)
8445                   (with-temp-buffer
8446                     (insert (format "211 %d Article retrieved.\n"
8447                                     (cdr gnus-article-current)))
8448                     (insert head)
8449                     (insert ".\n")
8450                     (let ((nntp-server-buffer (current-buffer)))
8451                       (setq header (car (gnus-get-newsgroup-headers
8452                                          (save-excursion
8453                                            (set-buffer gnus-summary-buffer)
8454                                            gnus-newsgroup-dependencies)
8455                                          t))))
8456                     (save-excursion
8457                       (set-buffer gnus-summary-buffer)
8458                       (gnus-data-set-header
8459                        (gnus-data-find (cdr gnus-article-current))
8460                        header)
8461                       (gnus-summary-update-article-line
8462                        (cdr gnus-article-current) header))))))
8463           ;; Update threads.
8464           (set-buffer (or buffer gnus-summary-buffer))
8465           (gnus-summary-update-article (cdr gnus-article-current)))
8466         ;; Prettify the article buffer again.
8467         (unless no-highlight
8468           (save-excursion
8469             (set-buffer gnus-article-buffer)
8470             ;;;!!! Fix this -- article should be rehighlighted.
8471             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8472             (set-buffer gnus-original-article-buffer)
8473             (gnus-request-article
8474              (cdr gnus-article-current)
8475              (car gnus-article-current) (current-buffer))))
8476         ;; Prettify the summary buffer line.
8477         (when (gnus-visual-p 'summary-highlight 'highlight)
8478           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8479
8480 (defun gnus-summary-edit-wash (key)
8481   "Perform editing command KEY in the article buffer."
8482   (interactive
8483    (list
8484     (progn
8485       (message "%s" (concat (this-command-keys) "- "))
8486       (read-char))))
8487   (message "")
8488   (gnus-summary-edit-article)
8489   (execute-kbd-macro (concat (this-command-keys) key))
8490   (gnus-article-edit-done))
8491
8492 ;;; Respooling
8493
8494 (defun gnus-summary-respool-query (&optional silent trace)
8495   "Query where the respool algorithm would put this article."
8496   (interactive)
8497   (let (gnus-mark-article-hook)
8498     (gnus-summary-select-article)
8499     (save-excursion
8500       (set-buffer gnus-original-article-buffer)
8501       (save-restriction
8502         (message-narrow-to-head)
8503         (let ((groups (nnmail-article-group 'identity trace)))
8504           (unless silent
8505             (if groups
8506                 (message "This message would go to %s"
8507                          (mapconcat 'car groups ", "))
8508               (message "This message would go to no groups"))
8509             groups))))))
8510
8511 (defun gnus-summary-respool-trace ()
8512   "Trace where the respool algorithm would put this article.
8513 Display a buffer showing all fancy splitting patterns which matched."
8514   (interactive)
8515   (gnus-summary-respool-query nil t))
8516
8517 ;; Summary marking commands.
8518
8519 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8520   "Mark articles which has the same subject as read, and then select the next.
8521 If UNMARK is positive, remove any kind of mark.
8522 If UNMARK is negative, tick articles."
8523   (interactive "P")
8524   (when unmark
8525     (setq unmark (prefix-numeric-value unmark)))
8526   (let ((count
8527          (gnus-summary-mark-same-subject
8528           (gnus-summary-article-subject) unmark)))
8529     ;; Select next unread article.  If auto-select-same mode, should
8530     ;; select the first unread article.
8531     (gnus-summary-next-article t (and gnus-auto-select-same
8532                                       (gnus-summary-article-subject)))
8533     (gnus-message 7 "%d article%s marked as %s"
8534                   count (if (= count 1) " is" "s are")
8535                   (if unmark "unread" "read"))))
8536
8537 (defun gnus-summary-kill-same-subject (&optional unmark)
8538   "Mark articles which has the same subject as read.
8539 If UNMARK is positive, remove any kind of mark.
8540 If UNMARK is negative, tick articles."
8541   (interactive "P")
8542   (when unmark
8543     (setq unmark (prefix-numeric-value unmark)))
8544   (let ((count
8545          (gnus-summary-mark-same-subject
8546           (gnus-summary-article-subject) unmark)))
8547     ;; If marked as read, go to next unread subject.
8548     (when (null unmark)
8549       ;; Go to next unread subject.
8550       (gnus-summary-next-subject 1 t))
8551     (gnus-message 7 "%d articles are marked as %s"
8552                   count (if unmark "unread" "read"))))
8553
8554 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8555   "Mark articles with same SUBJECT as read, and return marked number.
8556 If optional argument UNMARK is positive, remove any kinds of marks.
8557 If optional argument UNMARK is negative, mark articles as unread instead."
8558   (let ((count 1))
8559     (save-excursion
8560       (cond
8561        ((null unmark)                   ; Mark as read.
8562         (while (and
8563                 (progn
8564                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8565                   (gnus-summary-show-thread) t)
8566                 (gnus-summary-find-subject subject))
8567           (setq count (1+ count))))
8568        ((> unmark 0)                    ; Tick.
8569         (while (and
8570                 (progn
8571                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8572                   (gnus-summary-show-thread) t)
8573                 (gnus-summary-find-subject subject))
8574           (setq count (1+ count))))
8575        (t                               ; Mark as unread.
8576         (while (and
8577                 (progn
8578                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8579                   (gnus-summary-show-thread) t)
8580                 (gnus-summary-find-subject subject))
8581           (setq count (1+ count)))))
8582       (gnus-set-mode-line 'summary)
8583       ;; Return the number of marked articles.
8584       count)))
8585
8586 (defun gnus-summary-mark-as-processable (n &optional unmark)
8587   "Set the process mark on the next N articles.
8588 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8589 the process mark instead.  The difference between N and the actual
8590 number of articles marked is returned."
8591   (interactive "P")
8592   (if (and (null n) (gnus-region-active-p))
8593       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8594     (setq n (prefix-numeric-value n))
8595     (let ((backward (< n 0))
8596           (n (abs n)))
8597       (while (and
8598               (> n 0)
8599               (if unmark
8600                 (gnus-summary-remove-process-mark
8601                  (gnus-summary-article-number))
8602                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8603               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8604         (setq n (1- n)))
8605       (when (/= 0 n)
8606         (gnus-message 7 "No more articles"))
8607       (gnus-summary-recenter)
8608       (gnus-summary-position-point)
8609       n)))
8610
8611 (defun gnus-summary-unmark-as-processable (n)
8612   "Remove the process mark from the next N articles.
8613 If N is negative, unmark backward instead.  The difference between N and
8614 the actual number of articles unmarked is returned."
8615   (interactive "P")
8616   (gnus-summary-mark-as-processable n t))
8617
8618 (defun gnus-summary-unmark-all-processable ()
8619   "Remove the process mark from all articles."
8620   (interactive)
8621   (save-excursion
8622     (while gnus-newsgroup-processable
8623       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8624   (gnus-summary-position-point))
8625
8626 (defun gnus-summary-add-mark (article type)
8627   "Mark ARTICLE with a mark of TYPE."
8628   (let ((vtype (car (assq type gnus-article-mark-lists)))
8629         var)
8630     (if (not vtype)
8631         (error "No such mark type: %s" type)
8632       (setq var (intern (format "gnus-newsgroup-%s" type)))
8633       (set var (cons article (symbol-value var)))
8634       (if (memq type '(processable cached replied forwarded saved))
8635           (gnus-summary-update-secondary-mark article)
8636         ;;; !!! This is bobus.  We should find out what primary
8637         ;;; !!! mark we want to set.
8638         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8639
8640 (defun gnus-summary-mark-as-expirable (n)
8641   "Mark N articles forward as expirable.
8642 If N is negative, mark backward instead.  The difference between N and
8643 the actual number of articles marked is returned."
8644   (interactive "p")
8645   (gnus-summary-mark-forward n gnus-expirable-mark))
8646
8647 (defun gnus-summary-mark-article-as-replied (article)
8648   "Mark ARTICLE as replied to and update the summary line.
8649 ARTICLE can also be a list of articles."
8650   (interactive (list (gnus-summary-article-number)))
8651   (let ((articles (if (listp article) article (list article))))
8652     (dolist (article articles)
8653       (push article gnus-newsgroup-replied)
8654       (let ((buffer-read-only nil))
8655         (when (gnus-summary-goto-subject article nil t)
8656           (gnus-summary-update-secondary-mark article))))))
8657
8658 (defun gnus-summary-mark-article-as-forwarded (article)
8659   "Mark ARTICLE as forwarded and update the summary line.
8660 ARTICLE can also be a list of articles."
8661   (let ((articles (if (listp article) article (list article))))
8662     (dolist (article articles)
8663       (push article gnus-newsgroup-forwarded)
8664       (let ((buffer-read-only nil))
8665         (when (gnus-summary-goto-subject article nil t)
8666           (gnus-summary-update-secondary-mark article))))))
8667
8668 (defun gnus-summary-set-bookmark (article)
8669   "Set a bookmark in current article."
8670   (interactive (list (gnus-summary-article-number)))
8671   (when (or (not (get-buffer gnus-article-buffer))
8672             (not gnus-current-article)
8673             (not gnus-article-current)
8674             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8675     (error "No current article selected"))
8676   ;; Remove old bookmark, if one exists.
8677   (let ((old (assq article gnus-newsgroup-bookmarks)))
8678     (when old
8679       (setq gnus-newsgroup-bookmarks
8680             (delq old gnus-newsgroup-bookmarks))))
8681   ;; Set the new bookmark, which is on the form
8682   ;; (article-number . line-number-in-body).
8683   (push
8684    (cons article
8685          (save-excursion
8686            (set-buffer gnus-article-buffer)
8687            (count-lines
8688             (min (point)
8689                  (save-excursion
8690                    (goto-char (point-min))
8691                    (search-forward "\n\n" nil t)
8692                    (point)))
8693             (point))))
8694    gnus-newsgroup-bookmarks)
8695   (gnus-message 6 "A bookmark has been added to the current article."))
8696
8697 (defun gnus-summary-remove-bookmark (article)
8698   "Remove the bookmark from the current article."
8699   (interactive (list (gnus-summary-article-number)))
8700   ;; Remove old bookmark, if one exists.
8701   (let ((old (assq article gnus-newsgroup-bookmarks)))
8702     (if old
8703         (progn
8704           (setq gnus-newsgroup-bookmarks
8705                 (delq old gnus-newsgroup-bookmarks))
8706           (gnus-message 6 "Removed bookmark."))
8707       (gnus-message 6 "No bookmark in current article."))))
8708
8709 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8710 (defun gnus-summary-mark-as-dormant (n)
8711   "Mark N articles forward as dormant.
8712 If N is negative, mark backward instead.  The difference between N and
8713 the actual number of articles marked is returned."
8714   (interactive "p")
8715   (gnus-summary-mark-forward n gnus-dormant-mark))
8716
8717 (defun gnus-summary-set-process-mark (article)
8718   "Set the process mark on ARTICLE and update the summary line."
8719   (setq gnus-newsgroup-processable
8720         (cons article
8721               (delq article gnus-newsgroup-processable)))
8722   (when (gnus-summary-goto-subject article)
8723     (gnus-summary-show-thread)
8724     (gnus-summary-goto-subject article)
8725     (gnus-summary-update-secondary-mark article)))
8726
8727 (defun gnus-summary-remove-process-mark (article)
8728   "Remove the process mark from ARTICLE and update the summary line."
8729   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8730   (when (gnus-summary-goto-subject article)
8731     (gnus-summary-show-thread)
8732     (gnus-summary-goto-subject article)
8733     (gnus-summary-update-secondary-mark article)))
8734
8735 (defun gnus-summary-set-saved-mark (article)
8736   "Set the process mark on ARTICLE and update the summary line."
8737   (push article gnus-newsgroup-saved)
8738   (when (gnus-summary-goto-subject article)
8739     (gnus-summary-update-secondary-mark article)))
8740
8741 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8742   "Mark N articles as read forwards.
8743 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8744 The difference between N and the actual number of articles marked is
8745 returned.
8746 Iff NO-EXPIRE, auto-expiry will be inhibited."
8747   (interactive "p")
8748   (gnus-summary-show-thread)
8749   (let ((backward (< n 0))
8750         (gnus-summary-goto-unread
8751          (and gnus-summary-goto-unread
8752               (not (eq gnus-summary-goto-unread 'never))
8753               (not (memq mark (list gnus-unread-mark
8754                                     gnus-ticked-mark gnus-dormant-mark)))))
8755         (n (abs n))
8756         (mark (or mark gnus-del-mark)))
8757     (while (and (> n 0)
8758                 (gnus-summary-mark-article nil mark no-expire)
8759                 (zerop (gnus-summary-next-subject
8760                         (if backward -1 1)
8761                         (and gnus-summary-goto-unread
8762                              (not (eq gnus-summary-goto-unread 'never)))
8763                         t)))
8764       (setq n (1- n)))
8765     (when (/= 0 n)
8766       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8767     (gnus-summary-recenter)
8768     (gnus-summary-position-point)
8769     (gnus-set-mode-line 'summary)
8770     n))
8771
8772 (defun gnus-summary-mark-article-as-read (mark)
8773   "Mark the current article quickly as read with MARK."
8774   (let ((article (gnus-summary-article-number)))
8775     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8776     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8777     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8778     (push (cons article mark) gnus-newsgroup-reads)
8779     ;; Possibly remove from cache, if that is used.
8780     (when gnus-use-cache
8781       (gnus-cache-enter-remove-article article))
8782     ;; Allow the backend to change the mark.
8783     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8784     ;; Check for auto-expiry.
8785     (when (and gnus-newsgroup-auto-expire
8786                (memq mark gnus-auto-expirable-marks))
8787       (setq mark gnus-expirable-mark)
8788       ;; Let the backend know about the mark change.
8789       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8790       (push article gnus-newsgroup-expirable))
8791     ;; Set the mark in the buffer.
8792     (gnus-summary-update-mark mark 'unread)
8793     t))
8794
8795 (defun gnus-summary-mark-article-as-unread (mark)
8796   "Mark the current article quickly as unread with MARK."
8797   (let* ((article (gnus-summary-article-number))
8798          (old-mark (gnus-summary-article-mark article)))
8799     ;; Allow the backend to change the mark.
8800     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8801     (if (eq mark old-mark)
8802         t
8803       (if (<= article 0)
8804           (progn
8805             (gnus-error 1 "Can't mark negative article numbers")
8806             nil)
8807         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8808         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8809         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8810         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8811         (cond ((= mark gnus-ticked-mark)
8812                (push article gnus-newsgroup-marked))
8813               ((= mark gnus-dormant-mark)
8814                (push article gnus-newsgroup-dormant))
8815               (t
8816                (push article gnus-newsgroup-unreads)))
8817         (gnus-pull article gnus-newsgroup-reads)
8818
8819         ;; See whether the article is to be put in the cache.
8820         (and gnus-use-cache
8821              (vectorp (gnus-summary-article-header article))
8822              (save-excursion
8823                (gnus-cache-possibly-enter-article
8824                 gnus-newsgroup-name article
8825                 (gnus-summary-article-header article)
8826                 (= mark gnus-ticked-mark)
8827                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8828
8829         ;; Fix the mark.
8830         (gnus-summary-update-mark mark 'unread)
8831         t))))
8832
8833 (defun gnus-summary-mark-article (&optional article mark no-expire)
8834   "Mark ARTICLE with MARK.  MARK can be any character.
8835 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8836 `??' (dormant) and `?E' (expirable).
8837 If MARK is nil, then the default character `?r' is used.
8838 If ARTICLE is nil, then the article on the current line will be
8839 marked.
8840 Iff NO-EXPIRE, auto-expiry will be inhibited."
8841   ;; The mark might be a string.
8842   (when (stringp mark)
8843     (setq mark (aref mark 0)))
8844   ;; If no mark is given, then we check auto-expiring.
8845   (when (null mark)
8846     (setq mark gnus-del-mark))
8847   (when (and (not no-expire)
8848              gnus-newsgroup-auto-expire
8849              (memq mark gnus-auto-expirable-marks))
8850     (setq mark gnus-expirable-mark))
8851   (let ((article (or article (gnus-summary-article-number)))
8852         (old-mark (gnus-summary-article-mark article)))
8853     ;; Allow the backend to change the mark.
8854     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8855     (if (eq mark old-mark)
8856         t
8857       (unless article
8858         (error "No article on current line"))
8859       (if (not (if (or (= mark gnus-unread-mark)
8860                        (= mark gnus-ticked-mark)
8861                        (= mark gnus-dormant-mark))
8862                    (gnus-mark-article-as-unread article mark)
8863                  (gnus-mark-article-as-read article mark)))
8864           t
8865         ;; See whether the article is to be put in the cache.
8866         (and gnus-use-cache
8867              (not (= mark gnus-canceled-mark))
8868              (vectorp (gnus-summary-article-header article))
8869              (save-excursion
8870                (gnus-cache-possibly-enter-article
8871                 gnus-newsgroup-name article
8872                 (gnus-summary-article-header article)
8873                 (= mark gnus-ticked-mark)
8874                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8875
8876         (when (gnus-summary-goto-subject article nil t)
8877           (let ((buffer-read-only nil))
8878             (gnus-summary-show-thread)
8879             ;; Fix the mark.
8880             (gnus-summary-update-mark mark 'unread)
8881             t))))))
8882
8883 (defun gnus-summary-update-secondary-mark (article)
8884   "Update the secondary (read, process, cache) mark."
8885   (gnus-summary-update-mark
8886    (cond ((memq article gnus-newsgroup-processable)
8887           gnus-process-mark)
8888          ((memq article gnus-newsgroup-cached)
8889           gnus-cached-mark)
8890          ((memq article gnus-newsgroup-replied)
8891           gnus-replied-mark)
8892          ((memq article gnus-newsgroup-forwarded)
8893           gnus-forwarded-mark)
8894          ((memq article gnus-newsgroup-saved)
8895           gnus-saved-mark)
8896          (t gnus-no-mark))
8897    'replied)
8898   (when (gnus-visual-p 'summary-highlight 'highlight)
8899     (gnus-run-hooks 'gnus-summary-update-hook))
8900   t)
8901
8902 (defun gnus-summary-update-mark (mark type)
8903   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8904         (buffer-read-only nil))
8905     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8906     (when forward
8907       (when (looking-at "\r")
8908         (incf forward))
8909       (when (<= (+ forward (point)) (point-max))
8910         ;; Go to the right position on the line.
8911         (goto-char (+ forward (point)))
8912         ;; Replace the old mark with the new mark.
8913         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8914         ;; Optionally update the marks by some user rule.
8915         (when (eq type 'unread)
8916           (gnus-data-set-mark
8917            (gnus-data-find (gnus-summary-article-number)) mark)
8918           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8919
8920 (defun gnus-mark-article-as-read (article &optional mark)
8921   "Enter ARTICLE in the pertinent lists and remove it from others."
8922   ;; Make the article expirable.
8923   (let ((mark (or mark gnus-del-mark)))
8924     (if (= mark gnus-expirable-mark)
8925         (push article gnus-newsgroup-expirable)
8926       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8927     ;; Remove from unread and marked lists.
8928     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8929     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8930     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8931     (push (cons article mark) gnus-newsgroup-reads)
8932     ;; Possibly remove from cache, if that is used.
8933     (when gnus-use-cache
8934       (gnus-cache-enter-remove-article article))
8935     t))
8936
8937 (defun gnus-mark-article-as-unread (article &optional mark)
8938   "Enter ARTICLE in the pertinent lists and remove it from others."
8939   (let ((mark (or mark gnus-ticked-mark)))
8940     (if (<= article 0)
8941         (progn
8942           (gnus-error 1 "Can't mark negative article numbers")
8943           nil)
8944       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8945             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8946             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8947             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8948
8949       ;; Unsuppress duplicates?
8950       (when gnus-suppress-duplicates
8951         (gnus-dup-unsuppress-article article))
8952
8953       (cond ((= mark gnus-ticked-mark)
8954              (push article gnus-newsgroup-marked))
8955             ((= mark gnus-dormant-mark)
8956              (push article gnus-newsgroup-dormant))
8957             (t
8958              (push article gnus-newsgroup-unreads)))
8959       (gnus-pull article gnus-newsgroup-reads)
8960       t)))
8961
8962 (defalias 'gnus-summary-mark-as-unread-forward
8963   'gnus-summary-tick-article-forward)
8964 (make-obsolete 'gnus-summary-mark-as-unread-forward
8965                'gnus-summary-tick-article-forward)
8966 (defun gnus-summary-tick-article-forward (n)
8967   "Tick N articles forwards.
8968 If N is negative, tick backwards instead.
8969 The difference between N and the number of articles ticked is returned."
8970   (interactive "p")
8971   (gnus-summary-mark-forward n gnus-ticked-mark))
8972
8973 (defalias 'gnus-summary-mark-as-unread-backward
8974   'gnus-summary-tick-article-backward)
8975 (make-obsolete 'gnus-summary-mark-as-unread-backward
8976                'gnus-summary-tick-article-backward)
8977 (defun gnus-summary-tick-article-backward (n)
8978   "Tick N articles backwards.
8979 The difference between N and the number of articles ticked is returned."
8980   (interactive "p")
8981   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8982
8983 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8984 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8985 (defun gnus-summary-tick-article (&optional article clear-mark)
8986   "Mark current article as unread.
8987 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8988 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8989   (interactive)
8990   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8991                                        gnus-ticked-mark)))
8992
8993 (defun gnus-summary-mark-as-read-forward (n)
8994   "Mark N articles as read forwards.
8995 If N is negative, mark backwards instead.
8996 The difference between N and the actual number of articles marked is
8997 returned."
8998   (interactive "p")
8999   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9000
9001 (defun gnus-summary-mark-as-read-backward (n)
9002   "Mark the N articles as read backwards.
9003 The difference between N and the actual number of articles marked is
9004 returned."
9005   (interactive "p")
9006   (gnus-summary-mark-forward
9007    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9008
9009 (defun gnus-summary-mark-as-read (&optional article mark)
9010   "Mark current article as read.
9011 ARTICLE specifies the article to be marked as read.
9012 MARK specifies a string to be inserted at the beginning of the line."
9013   (gnus-summary-mark-article article mark))
9014
9015 (defun gnus-summary-clear-mark-forward (n)
9016   "Clear marks from N articles forward.
9017 If N is negative, clear backward instead.
9018 The difference between N and the number of marks cleared is returned."
9019   (interactive "p")
9020   (gnus-summary-mark-forward n gnus-unread-mark))
9021
9022 (defun gnus-summary-clear-mark-backward (n)
9023   "Clear marks from N articles backward.
9024 The difference between N and the number of marks cleared is returned."
9025   (interactive "p")
9026   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9027
9028 (defun gnus-summary-mark-unread-as-read ()
9029   "Intended to be used by `gnus-summary-mark-article-hook'."
9030   (when (memq gnus-current-article gnus-newsgroup-unreads)
9031     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9032
9033 (defun gnus-summary-mark-read-and-unread-as-read ()
9034   "Intended to be used by `gnus-summary-mark-article-hook'."
9035   (let ((mark (gnus-summary-article-mark)))
9036     (when (or (gnus-unread-mark-p mark)
9037               (gnus-read-mark-p mark))
9038       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9039
9040 (defun gnus-summary-mark-unread-as-ticked ()
9041    "Intended to be used by `gnus-summary-mark-article-hook'."
9042   (when (memq gnus-current-article gnus-newsgroup-unreads)
9043     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9044
9045 (defun gnus-summary-mark-region-as-read (point mark all)
9046   "Mark all unread articles between point and mark as read.
9047 If given a prefix, mark all articles between point and mark as read,
9048 even ticked and dormant ones."
9049   (interactive "r\nP")
9050   (save-excursion
9051     (let (article)
9052       (goto-char point)
9053       (beginning-of-line)
9054       (while (and
9055               (< (point) mark)
9056               (progn
9057                 (when (or all
9058                           (memq (setq article (gnus-summary-article-number))
9059                                 gnus-newsgroup-unreads))
9060                   (gnus-summary-mark-article article gnus-del-mark))
9061                 t)
9062               (gnus-summary-find-next))))))
9063
9064 (defun gnus-summary-mark-below (score mark)
9065   "Mark articles with score less than SCORE with MARK."
9066   (interactive "P\ncMark: ")
9067   (setq score (if score
9068                   (prefix-numeric-value score)
9069                 (or gnus-summary-default-score 0)))
9070   (save-excursion
9071     (set-buffer gnus-summary-buffer)
9072     (goto-char (point-min))
9073     (while
9074         (progn
9075           (and (< (gnus-summary-article-score) score)
9076                (gnus-summary-mark-article nil mark))
9077           (gnus-summary-find-next)))))
9078
9079 (defun gnus-summary-kill-below (&optional score)
9080   "Mark articles with score below SCORE as read."
9081   (interactive "P")
9082   (gnus-summary-mark-below score gnus-killed-mark))
9083
9084 (defun gnus-summary-clear-above (&optional score)
9085   "Clear all marks from articles with score above SCORE."
9086   (interactive "P")
9087   (gnus-summary-mark-above score gnus-unread-mark))
9088
9089 (defun gnus-summary-tick-above (&optional score)
9090   "Tick all articles with score above SCORE."
9091   (interactive "P")
9092   (gnus-summary-mark-above score gnus-ticked-mark))
9093
9094 (defun gnus-summary-mark-above (score mark)
9095   "Mark articles with score over SCORE with MARK."
9096   (interactive "P\ncMark: ")
9097   (setq score (if score
9098                   (prefix-numeric-value score)
9099                 (or gnus-summary-default-score 0)))
9100   (save-excursion
9101     (set-buffer gnus-summary-buffer)
9102     (goto-char (point-min))
9103     (while (and (progn
9104                   (when (> (gnus-summary-article-score) score)
9105                     (gnus-summary-mark-article nil mark))
9106                   t)
9107                 (gnus-summary-find-next)))))
9108
9109 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9110 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9111 (defun gnus-summary-limit-include-expunged (&optional no-error)
9112   "Display all the hidden articles that were expunged for low scores."
9113   (interactive)
9114   (let ((buffer-read-only nil))
9115     (let ((scored gnus-newsgroup-scored)
9116           headers h)
9117       (while scored
9118         (unless (gnus-summary-article-header (caar scored))
9119           (and (setq h (gnus-number-to-header (caar scored)))
9120                (< (cdar scored) gnus-summary-expunge-below)
9121                (push h headers)))
9122         (setq scored (cdr scored)))
9123       (if (not headers)
9124           (when (not no-error)
9125             (error "No expunged articles hidden"))
9126         (goto-char (point-min))
9127         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9128         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9129         (mapcar (lambda (x) (push (mail-header-number x) 
9130                                   gnus-newsgroup-limit))
9131                 headers)
9132         (gnus-summary-prepare-unthreaded (nreverse headers))
9133         (goto-char (point-min))
9134         (gnus-summary-position-point)
9135         t))))
9136
9137 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9138   "Mark all unread articles in this newsgroup as read.
9139 If prefix argument ALL is non-nil, ticked and dormant articles will
9140 also be marked as read.
9141 If QUIETLY is non-nil, no questions will be asked.
9142 If TO-HERE is non-nil, it should be a point in the buffer.  All
9143 articles before (after, if REVERSE is set) this point will be marked as read.
9144 Note that this function will only catch up the unread article
9145 in the current summary buffer limitation.
9146 The number of articles marked as read is returned."
9147   (interactive "P")
9148   (prog1
9149       (save-excursion
9150         (when (or quietly
9151                   (not gnus-interactive-catchup) ;Without confirmation?
9152                   gnus-expert-user
9153                   (gnus-y-or-n-p
9154                    (if all
9155                        "Mark absolutely all articles as read? "
9156                      "Mark all unread articles as read? ")))
9157           (if (and not-mark
9158                    (not gnus-newsgroup-adaptive)
9159                    (not gnus-newsgroup-auto-expire)
9160                    (not gnus-suppress-duplicates)
9161                    (or (not gnus-use-cache)
9162                        (eq gnus-use-cache 'passive)))
9163               (progn
9164                 (when all
9165                   (setq gnus-newsgroup-marked nil
9166                         gnus-newsgroup-dormant nil))
9167                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9168             ;; We actually mark all articles as canceled, which we
9169             ;; have to do when using auto-expiry or adaptive scoring.
9170             (gnus-summary-show-all-threads)
9171             (if (and to-here reverse)
9172                 (progn
9173                   (goto-char to-here)
9174                   (while (and
9175                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9176                           (gnus-summary-find-next (not all) nil nil t))))
9177               (when (gnus-summary-first-subject (not all) t)
9178                 (while (and
9179                         (if to-here (< (point) to-here) t)
9180                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9181                         (gnus-summary-find-next (not all) nil nil t)))))
9182             (gnus-set-mode-line 'summary))
9183           t))
9184     (gnus-summary-position-point)))
9185
9186 (defun gnus-summary-catchup-to-here (&optional all)
9187   "Mark all unticked articles before the current one as read.
9188 If ALL is non-nil, also mark ticked and dormant articles as read."
9189   (interactive "P")
9190   (save-excursion
9191     (gnus-save-hidden-threads
9192       (let ((beg (point)))
9193         ;; We check that there are unread articles.
9194         (when (or all (gnus-summary-find-prev))
9195           (gnus-summary-catchup all t beg)))))
9196   (gnus-summary-position-point))
9197
9198 (defun gnus-summary-catchup-from-here (&optional all)
9199   "Mark all unticked articles after the current one as read.
9200 If ALL is non-nil, also mark ticked and dormant articles as read."
9201   (interactive "P")
9202   (save-excursion
9203     (gnus-save-hidden-threads
9204       (let ((beg (point)))
9205         ;; We check that there are unread articles.
9206         (when (or all (gnus-summary-find-next))
9207           (gnus-summary-catchup all t beg nil t)))))
9208   (gnus-summary-position-point))
9209
9210 (defun gnus-summary-catchup-all (&optional quietly)
9211   "Mark all articles in this newsgroup as read."
9212   (interactive "P")
9213   (gnus-summary-catchup t quietly))
9214
9215 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9216   "Mark all unread articles in this group as read, then exit.
9217 If prefix argument ALL is non-nil, all articles are marked as read.
9218 If QUIETLY is non-nil, no questions will be asked."
9219   (interactive "P")
9220   (when (gnus-summary-catchup all quietly nil 'fast)
9221     ;; Select next newsgroup or exit.
9222     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9223              (eq gnus-auto-select-next 'quietly))
9224         (gnus-summary-next-group nil)
9225       (gnus-summary-exit))))
9226
9227 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9228   "Mark all articles in this newsgroup as read, and then exit."
9229   (interactive "P")
9230   (gnus-summary-catchup-and-exit t quietly))
9231
9232 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9233   "Mark all articles in this group as read and select the next group.
9234 If given a prefix, mark all articles, unread as well as ticked, as
9235 read."
9236   (interactive "P")
9237   (save-excursion
9238     (gnus-summary-catchup all))
9239   (gnus-summary-next-group))
9240
9241 ;;;
9242 ;;; with article
9243 ;;;
9244
9245 (defmacro gnus-with-article (article &rest forms)
9246   "Select ARTICLE and perform FORMS in the original article buffer.
9247 Then replace the article with the result."
9248   `(progn
9249      ;; We don't want the article to be marked as read.
9250      (let (gnus-mark-article-hook)
9251        (gnus-summary-select-article t t nil ,article))
9252      (set-buffer gnus-original-article-buffer)
9253      ,@forms
9254      (if (not (gnus-check-backend-function
9255                'request-replace-article (car gnus-article-current)))
9256          (gnus-message 5 "Read-only group; not replacing")
9257        (unless (gnus-request-replace-article
9258                 ,article (car gnus-article-current)
9259                 (current-buffer) t)
9260          (error "Couldn't replace article")))
9261      ;; The cache and backlog have to be flushed somewhat.
9262      (when gnus-keep-backlog
9263        (gnus-backlog-remove-article
9264         (car gnus-article-current) (cdr gnus-article-current)))
9265      (when gnus-use-cache
9266        (gnus-cache-update-article
9267         (car gnus-article-current) (cdr gnus-article-current)))))
9268
9269 (put 'gnus-with-article 'lisp-indent-function 1)
9270 (put 'gnus-with-article 'edebug-form-spec '(form body))
9271
9272 ;; Thread-based commands.
9273
9274 (defun gnus-summary-articles-in-thread (&optional article)
9275   "Return a list of all articles in the current thread.
9276 If ARTICLE is non-nil, return all articles in the thread that starts
9277 with that article."
9278   (let* ((article (or article (gnus-summary-article-number)))
9279          (data (gnus-data-find-list article))
9280          (top-level (gnus-data-level (car data)))
9281          (top-subject
9282           (cond ((null gnus-thread-operation-ignore-subject)
9283                  (gnus-simplify-subject-re
9284                   (mail-header-subject (gnus-data-header (car data)))))
9285                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9286                  (gnus-simplify-subject-fuzzy
9287                   (mail-header-subject (gnus-data-header (car data)))))
9288                 (t nil)))
9289          (end-point (save-excursion
9290                       (if (gnus-summary-go-to-next-thread)
9291                           (point) (point-max))))
9292          articles)
9293     (while (and data
9294                 (< (gnus-data-pos (car data)) end-point))
9295       (when (or (not top-subject)
9296                 (string= top-subject
9297                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9298                              (gnus-simplify-subject-fuzzy
9299                               (mail-header-subject
9300                                (gnus-data-header (car data))))
9301                            (gnus-simplify-subject-re
9302                             (mail-header-subject
9303                              (gnus-data-header (car data)))))))
9304         (push (gnus-data-number (car data)) articles))
9305       (unless (and (setq data (cdr data))
9306                    (> (gnus-data-level (car data)) top-level))
9307         (setq data nil)))
9308     ;; Return the list of articles.
9309     (nreverse articles)))
9310
9311 (defun gnus-summary-rethread-current ()
9312   "Rethread the thread the current article is part of."
9313   (interactive)
9314   (let* ((gnus-show-threads t)
9315          (article (gnus-summary-article-number))
9316          (id (mail-header-id (gnus-summary-article-header)))
9317          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9318     (unless id
9319       (error "No article on the current line"))
9320     (gnus-rebuild-thread id)
9321     (gnus-summary-goto-subject article)))
9322
9323 (defun gnus-summary-reparent-thread ()
9324   "Make the current article child of the marked (or previous) article.
9325
9326 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9327 is non-nil or the Subject: of both articles are the same."
9328   (interactive)
9329   (unless (not (gnus-group-read-only-p))
9330     (error "The current newsgroup does not support article editing"))
9331   (unless (<= (length gnus-newsgroup-processable) 1)
9332     (error "No more than one article may be marked"))
9333   (save-window-excursion
9334     (let ((gnus-article-buffer " *reparent*")
9335           (current-article (gnus-summary-article-number))
9336           ;; First grab the marked article, otherwise one line up.
9337           (parent-article (if (not (null gnus-newsgroup-processable))
9338                               (car gnus-newsgroup-processable)
9339                             (save-excursion
9340                               (if (eq (forward-line -1) 0)
9341                                   (gnus-summary-article-number)
9342                                 (error "Beginning of summary buffer"))))))
9343       (unless (not (eq current-article parent-article))
9344         (error "An article may not be self-referential"))
9345       (let ((message-id (mail-header-id
9346                          (gnus-summary-article-header parent-article))))
9347         (unless (and message-id (not (equal message-id "")))
9348           (error "No message-id in desired parent"))
9349         (gnus-with-article current-article
9350           (save-restriction
9351             (goto-char (point-min))
9352             (message-narrow-to-head)
9353             (if (re-search-forward "^References: " nil t)
9354                 (progn
9355                   (re-search-forward "^[^ \t]" nil t)
9356                   (forward-line -1)
9357                   (end-of-line)
9358                   (insert " " message-id))
9359               (insert "References: " message-id "\n"))))
9360         (set-buffer gnus-summary-buffer)
9361         (gnus-summary-unmark-all-processable)
9362         (gnus-summary-update-article current-article)
9363         (gnus-summary-rethread-current)
9364         (gnus-message 3 "Article %d is now the child of article %d"
9365                       current-article parent-article)))))
9366
9367 (defun gnus-summary-toggle-threads (&optional arg)
9368   "Toggle showing conversation threads.
9369 If ARG is positive number, turn showing conversation threads on."
9370   (interactive "P")
9371   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9372     (setq gnus-show-threads
9373           (if (null arg) (not gnus-show-threads)
9374             (> (prefix-numeric-value arg) 0)))
9375     (gnus-summary-prepare)
9376     (gnus-summary-goto-subject current)
9377     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9378     (gnus-summary-position-point)))
9379
9380 (defun gnus-summary-show-all-threads ()
9381   "Show all threads."
9382   (interactive)
9383   (save-excursion
9384     (let ((buffer-read-only nil))
9385       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9386   (gnus-summary-position-point))
9387
9388 (defun gnus-summary-show-thread ()
9389   "Show thread subtrees.
9390 Returns nil if no thread was there to be shown."
9391   (interactive)
9392   (let ((buffer-read-only nil)
9393         (orig (point))
9394         ;; first goto end then to beg, to have point at beg after let
9395         (end (progn (end-of-line) (point)))
9396         (beg (progn (beginning-of-line) (point))))
9397     (prog1
9398         ;; Any hidden lines here?
9399         (search-forward "\r" end t)
9400       (subst-char-in-region beg end ?\^M ?\n t)
9401       (goto-char orig)
9402       (gnus-summary-position-point))))
9403
9404 (defun gnus-summary-hide-all-threads ()
9405   "Hide all thread subtrees."
9406   (interactive)
9407   (save-excursion
9408     (goto-char (point-min))
9409     (gnus-summary-hide-thread)
9410     (while (zerop (gnus-summary-next-thread 1 t))
9411       (gnus-summary-hide-thread)))
9412   (gnus-summary-position-point))
9413
9414 (defun gnus-summary-hide-thread ()
9415   "Hide thread subtrees.
9416 Returns nil if no threads were there to be hidden."
9417   (interactive)
9418   (let ((buffer-read-only nil)
9419         (start (point))
9420         (article (gnus-summary-article-number)))
9421     (goto-char start)
9422     ;; Go forward until either the buffer ends or the subthread
9423     ;; ends.
9424     (when (and (not (eobp))
9425                (or (zerop (gnus-summary-next-thread 1 t))
9426                    (goto-char (point-max))))
9427       (prog1
9428           (if (and (> (point) start)
9429                    (search-backward "\n" start t))
9430               (progn
9431                 (subst-char-in-region start (point) ?\n ?\^M)
9432                 (gnus-summary-goto-subject article))
9433             (goto-char start)
9434             nil)))))
9435
9436 (defun gnus-summary-go-to-next-thread (&optional previous)
9437   "Go to the same level (or less) next thread.
9438 If PREVIOUS is non-nil, go to previous thread instead.
9439 Return the article number moved to, or nil if moving was impossible."
9440   (let ((level (gnus-summary-thread-level))
9441         (way (if previous -1 1))
9442         (beg (point)))
9443     (forward-line way)
9444     (while (and (not (eobp))
9445                 (< level (gnus-summary-thread-level)))
9446       (forward-line way))
9447     (if (eobp)
9448         (progn
9449           (goto-char beg)
9450           nil)
9451       (setq beg (point))
9452       (prog1
9453           (gnus-summary-article-number)
9454         (goto-char beg)))))
9455
9456 (defun gnus-summary-next-thread (n &optional silent)
9457   "Go to the same level next N'th thread.
9458 If N is negative, search backward instead.
9459 Returns the difference between N and the number of skips actually
9460 done.
9461
9462 If SILENT, don't output messages."
9463   (interactive "p")
9464   (let ((backward (< n 0))
9465         (n (abs n)))
9466     (while (and (> n 0)
9467                 (gnus-summary-go-to-next-thread backward))
9468       (decf n))
9469     (unless silent
9470       (gnus-summary-position-point))
9471     (when (and (not silent) (/= 0 n))
9472       (gnus-message 7 "No more threads"))
9473     n))
9474
9475 (defun gnus-summary-prev-thread (n)
9476   "Go to the same level previous N'th thread.
9477 Returns the difference between N and the number of skips actually
9478 done."
9479   (interactive "p")
9480   (gnus-summary-next-thread (- n)))
9481
9482 (defun gnus-summary-go-down-thread ()
9483   "Go down one level in the current thread."
9484   (let ((children (gnus-summary-article-children)))
9485     (when children
9486       (gnus-summary-goto-subject (car children)))))
9487
9488 (defun gnus-summary-go-up-thread ()
9489   "Go up one level in the current thread."
9490   (let ((parent (gnus-summary-article-parent)))
9491     (when parent
9492       (gnus-summary-goto-subject parent))))
9493
9494 (defun gnus-summary-down-thread (n)
9495   "Go down thread N steps.
9496 If N is negative, go up instead.
9497 Returns the difference between N and how many steps down that were
9498 taken."
9499   (interactive "p")
9500   (let ((up (< n 0))
9501         (n (abs n)))
9502     (while (and (> n 0)
9503                 (if up (gnus-summary-go-up-thread)
9504                   (gnus-summary-go-down-thread)))
9505       (setq n (1- n)))
9506     (gnus-summary-position-point)
9507     (when (/= 0 n)
9508       (gnus-message 7 "Can't go further"))
9509     n))
9510
9511 (defun gnus-summary-up-thread (n)
9512   "Go up thread N steps.
9513 If N is negative, go down instead.
9514 Returns the difference between N and how many steps down that were
9515 taken."
9516   (interactive "p")
9517   (gnus-summary-down-thread (- n)))
9518
9519 (defun gnus-summary-top-thread ()
9520   "Go to the top of the thread."
9521   (interactive)
9522   (while (gnus-summary-go-up-thread))
9523   (gnus-summary-article-number))
9524
9525 (defun gnus-summary-kill-thread (&optional unmark)
9526   "Mark articles under current thread as read.
9527 If the prefix argument is positive, remove any kinds of marks.
9528 If the prefix argument is negative, tick articles instead."
9529   (interactive "P")
9530   (when unmark
9531     (setq unmark (prefix-numeric-value unmark)))
9532   (let ((articles (gnus-summary-articles-in-thread)))
9533     (save-excursion
9534       ;; Expand the thread.
9535       (gnus-summary-show-thread)
9536       ;; Mark all the articles.
9537       (while articles
9538         (gnus-summary-goto-subject (car articles))
9539         (cond ((null unmark)
9540                (gnus-summary-mark-article-as-read gnus-killed-mark))
9541               ((> unmark 0)
9542                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9543               (t
9544                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9545         (setq articles (cdr articles))))
9546     ;; Hide killed subtrees.
9547     (and (null unmark)
9548          gnus-thread-hide-killed
9549          (gnus-summary-hide-thread))
9550     ;; If marked as read, go to next unread subject.
9551     (when (null unmark)
9552       ;; Go to next unread subject.
9553       (gnus-summary-next-subject 1 t)))
9554   (gnus-set-mode-line 'summary))
9555
9556 ;; Summary sorting commands
9557
9558 (defun gnus-summary-sort-by-number (&optional reverse)
9559   "Sort the summary buffer by article number.
9560 Argument REVERSE means reverse order."
9561   (interactive "P")
9562   (gnus-summary-sort 'number reverse))
9563
9564 (defun gnus-summary-sort-by-author (&optional reverse)
9565   "Sort the summary buffer by author name alphabetically.
9566 If `case-fold-search' is non-nil, case of letters is ignored.
9567 Argument REVERSE means reverse order."
9568   (interactive "P")
9569   (gnus-summary-sort 'author reverse))
9570
9571 (defun gnus-summary-sort-by-subject (&optional reverse)
9572   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9573 If `case-fold-search' is non-nil, case of letters is ignored.
9574 Argument REVERSE means reverse order."
9575   (interactive "P")
9576   (gnus-summary-sort 'subject reverse))
9577
9578 (defun gnus-summary-sort-by-date (&optional reverse)
9579   "Sort the summary buffer by date.
9580 Argument REVERSE means reverse order."
9581   (interactive "P")
9582   (gnus-summary-sort 'date reverse))
9583
9584 (defun gnus-summary-sort-by-score (&optional reverse)
9585   "Sort the summary buffer by score.
9586 Argument REVERSE means reverse order."
9587   (interactive "P")
9588   (gnus-summary-sort 'score reverse))
9589
9590 (defun gnus-summary-sort-by-lines (&optional reverse)
9591   "Sort the summary buffer by the number of lines.
9592 Argument REVERSE means reverse order."
9593   (interactive "P")
9594   (gnus-summary-sort 'lines reverse))
9595
9596 (defun gnus-summary-sort-by-chars (&optional reverse)
9597   "Sort the summary buffer by article length.
9598 Argument REVERSE means reverse order."
9599   (interactive "P")
9600   (gnus-summary-sort 'chars reverse))
9601
9602 (defun gnus-summary-sort-by-original (&optional reverse)
9603   "Sort the summary buffer using the default sorting method.
9604 Argument REVERSE means reverse order."
9605   (interactive "P")
9606   (let* ((buffer-read-only)
9607          (gnus-summary-prepare-hook nil))
9608     ;; We do the sorting by regenerating the threads.
9609     (gnus-summary-prepare)
9610     ;; Hide subthreads if needed.
9611     (when (and gnus-show-threads gnus-thread-hide-subtree)
9612       (gnus-summary-hide-all-threads))))
9613
9614 (defun gnus-summary-sort (predicate reverse)
9615   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9616   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9617          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9618          (gnus-thread-sort-functions
9619           (if (not reverse)
9620               thread
9621             `(lambda (t1 t2)
9622                (,thread t2 t1))))
9623          (gnus-sort-gathered-threads-function
9624           gnus-thread-sort-functions)
9625          (gnus-article-sort-functions
9626           (if (not reverse)
9627               article
9628             `(lambda (t1 t2)
9629                (,article t2 t1))))
9630          (buffer-read-only)
9631          (gnus-summary-prepare-hook nil))
9632     ;; We do the sorting by regenerating the threads.
9633     (gnus-summary-prepare)
9634     ;; Hide subthreads if needed.
9635     (when (and gnus-show-threads gnus-thread-hide-subtree)
9636       (gnus-summary-hide-all-threads))))
9637
9638 ;; Summary saving commands.
9639
9640 (defun gnus-summary-save-article (&optional n not-saved)
9641   "Save the current article using the default saver function.
9642 If N is a positive number, save the N next articles.
9643 If N is a negative number, save the N previous articles.
9644 If N is nil and any articles have been marked with the process mark,
9645 save those articles instead.
9646 The variable `gnus-default-article-saver' specifies the saver function."
9647   (interactive "P")
9648   (let* ((articles (gnus-summary-work-articles n))
9649          (save-buffer (save-excursion
9650                         (nnheader-set-temp-buffer " *Gnus Save*")))
9651          (num (length articles))
9652          header file)
9653     (dolist (article articles)
9654       (setq header (gnus-summary-article-header article))
9655       (if (not (vectorp header))
9656           ;; This is a pseudo-article.
9657           (if (assq 'name header)
9658               (gnus-copy-file (cdr (assq 'name header)))
9659             (gnus-message 1 "Article %d is unsaveable" article))
9660         ;; This is a real article.
9661         (save-window-excursion
9662           (gnus-summary-select-article t nil nil article))
9663         (save-excursion
9664           (set-buffer save-buffer)
9665           (erase-buffer)
9666           (insert-buffer-substring gnus-original-article-buffer))
9667         (setq file (gnus-article-save save-buffer file num))
9668         (gnus-summary-remove-process-mark article)
9669         (unless not-saved
9670           (gnus-summary-set-saved-mark article))))
9671     (gnus-kill-buffer save-buffer)
9672     (gnus-summary-position-point)
9673     (gnus-set-mode-line 'summary)
9674     n))
9675
9676 (defun gnus-summary-pipe-output (&optional arg)
9677   "Pipe the current article to a subprocess.
9678 If N is a positive number, pipe the N next articles.
9679 If N is a negative number, pipe the N previous articles.
9680 If N is nil and any articles have been marked with the process mark,
9681 pipe those articles instead."
9682   (interactive "P")
9683   (require 'gnus-art)
9684   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9685     (gnus-summary-save-article arg t))
9686   (gnus-configure-windows 'pipe))
9687
9688 (defun gnus-summary-save-article-mail (&optional arg)
9689   "Append the current article to an mail file.
9690 If N is a positive number, save the N next articles.
9691 If N is a negative number, save the N previous articles.
9692 If N is nil and any articles have been marked with the process mark,
9693 save those articles instead."
9694   (interactive "P")
9695   (require 'gnus-art)
9696   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9697     (gnus-summary-save-article arg)))
9698
9699 (defun gnus-summary-save-article-rmail (&optional arg)
9700   "Append the current article to an rmail file.
9701 If N is a positive number, save the N next articles.
9702 If N is a negative number, save the N previous articles.
9703 If N is nil and any articles have been marked with the process mark,
9704 save those articles instead."
9705   (interactive "P")
9706   (require 'gnus-art)
9707   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9708     (gnus-summary-save-article arg)))
9709
9710 (defun gnus-summary-save-article-file (&optional arg)
9711   "Append the current article to a file.
9712 If N is a positive number, save the N next articles.
9713 If N is a negative number, save the N previous articles.
9714 If N is nil and any articles have been marked with the process mark,
9715 save those articles instead."
9716   (interactive "P")
9717   (require 'gnus-art)
9718   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9719     (gnus-summary-save-article arg)))
9720
9721 (defun gnus-summary-write-article-file (&optional arg)
9722   "Write the current article to a file, deleting the previous file.
9723 If N is a positive number, save the N next articles.
9724 If N is a negative number, save the N previous articles.
9725 If N is nil and any articles have been marked with the process mark,
9726 save those articles instead."
9727   (interactive "P")
9728   (require 'gnus-art)
9729   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9730     (gnus-summary-save-article arg)))
9731
9732 (defun gnus-summary-save-article-body-file (&optional arg)
9733   "Append the current article body to a file.
9734 If N is a positive number, save the N next articles.
9735 If N is a negative number, save the N previous articles.
9736 If N is nil and any articles have been marked with the process mark,
9737 save those articles instead."
9738   (interactive "P")
9739   (require 'gnus-art)
9740   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9741     (gnus-summary-save-article arg)))
9742
9743 (defun gnus-summary-pipe-message (program)
9744   "Pipe the current article through PROGRAM."
9745   (interactive "sProgram: ")
9746   (gnus-summary-select-article)
9747   (let ((mail-header-separator ""))
9748     (gnus-eval-in-buffer-window gnus-article-buffer
9749       (save-restriction
9750         (widen)
9751         (let ((start (window-start))
9752               buffer-read-only)
9753           (message-pipe-buffer-body program)
9754           (set-window-start (get-buffer-window (current-buffer)) start))))))
9755
9756 (defun gnus-get-split-value (methods)
9757   "Return a value based on the split METHODS."
9758   (let (split-name method result match)
9759     (when methods
9760       (save-excursion
9761         (set-buffer gnus-original-article-buffer)
9762         (save-restriction
9763           (nnheader-narrow-to-headers)
9764           (while (and methods (not split-name))
9765             (goto-char (point-min))
9766             (setq method (pop methods))
9767             (setq match (car method))
9768             (when (cond
9769                    ((stringp match)
9770                     ;; Regular expression.
9771                     (ignore-errors
9772                       (re-search-forward match nil t)))
9773                    ((gnus-functionp match)
9774                     ;; Function.
9775                     (save-restriction
9776                       (widen)
9777                       (setq result (funcall match gnus-newsgroup-name))))
9778                    ((consp match)
9779                     ;; Form.
9780                     (save-restriction
9781                       (widen)
9782                       (setq result (eval match)))))
9783               (setq split-name (cdr method))
9784               (cond ((stringp result)
9785                      (push (expand-file-name
9786                             result gnus-article-save-directory)
9787                            split-name))
9788                     ((consp result)
9789                      (setq split-name (append result split-name)))))))))
9790     (nreverse split-name)))
9791
9792 (defun gnus-valid-move-group-p (group)
9793   (and (boundp group)
9794        (symbol-name group)
9795        (symbol-value group)
9796        (gnus-get-function (gnus-find-method-for-group
9797                            (symbol-name group)) 'request-accept-article t)))
9798
9799 (defun gnus-read-move-group-name (prompt default articles prefix)
9800   "Read a group name."
9801   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9802          (minibuffer-confirm-incomplete nil) ; XEmacs
9803          (prom
9804           (format "%s %s to:"
9805                   prompt
9806                   (if (> (length articles) 1)
9807                       (format "these %d articles" (length articles))
9808                     "this article")))
9809          (to-newsgroup
9810           (cond
9811            ((null split-name)
9812             (gnus-completing-read default prom
9813                                   gnus-active-hashtb
9814                                   'gnus-valid-move-group-p
9815                                   nil prefix
9816                                   'gnus-group-history))
9817            ((= 1 (length split-name))
9818             (gnus-completing-read (car split-name) prom
9819                                   gnus-active-hashtb
9820                                   'gnus-valid-move-group-p
9821                                   nil nil
9822                                   'gnus-group-history))
9823            (t
9824             (gnus-completing-read nil prom
9825                                   (mapcar (lambda (el) (list el))
9826                                           (nreverse split-name))
9827                                   nil nil nil
9828                                   'gnus-group-history))))
9829          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9830     (when to-newsgroup
9831       (if (or (string= to-newsgroup "")
9832               (string= to-newsgroup prefix))
9833           (setq to-newsgroup default))
9834       (unless to-newsgroup
9835         (error "No group name entered"))
9836       (or (gnus-active to-newsgroup)
9837           (gnus-activate-group to-newsgroup nil nil to-method)
9838           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9839                                      to-newsgroup))
9840               (or (and (gnus-request-create-group to-newsgroup to-method)
9841                        (gnus-activate-group
9842                         to-newsgroup nil nil to-method)
9843                        (gnus-subscribe-group to-newsgroup))
9844                   (error "Couldn't create group %s" to-newsgroup)))
9845           (error "No such group: %s" to-newsgroup)))
9846     to-newsgroup))
9847
9848 (defun gnus-summary-save-parts (type dir n &optional reverse)
9849   "Save parts matching TYPE to DIR.
9850 If REVERSE, save parts that do not match TYPE."
9851   (interactive
9852    (list (read-string "Save parts of type: "
9853                       (or (car gnus-summary-save-parts-type-history)
9854                           gnus-summary-save-parts-default-mime)
9855                       'gnus-summary-save-parts-type-history)
9856          (setq gnus-summary-save-parts-last-directory
9857                (read-file-name "Save to directory: "
9858                                gnus-summary-save-parts-last-directory
9859                                nil t))
9860          current-prefix-arg))
9861   (gnus-summary-iterate n
9862     (let ((gnus-display-mime-function nil)
9863           (gnus-inhibit-treatment t))
9864       (gnus-summary-select-article))
9865     (save-excursion
9866       (set-buffer gnus-article-buffer)
9867       (let ((handles (or gnus-article-mime-handles
9868                          (mm-dissect-buffer) (mm-uu-dissect))))
9869         (when handles
9870           (gnus-summary-save-parts-1 type dir handles reverse)
9871           (unless gnus-article-mime-handles ;; Don't destroy this case.
9872             (mm-destroy-parts handles)))))))
9873
9874 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9875   (if (stringp (car handle))
9876       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9877               (cdr handle))
9878     (when (if reverse
9879               (not (string-match type (mm-handle-media-type handle)))
9880             (string-match type (mm-handle-media-type handle)))
9881       (let ((file (expand-file-name
9882                    (file-name-nondirectory
9883                     (or
9884                      (mail-content-type-get
9885                       (mm-handle-disposition handle) 'filename)
9886                      (concat gnus-newsgroup-name
9887                              "." (number-to-string
9888                                   (cdr gnus-article-current)))))
9889                    dir)))
9890         (unless (file-exists-p file)
9891           (mm-save-part-to-file handle file))))))
9892
9893 ;; Summary extract commands
9894
9895 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9896   (let ((buffer-read-only nil)
9897         (article (gnus-summary-article-number))
9898         after-article b e)
9899     (unless (gnus-summary-goto-subject article)
9900       (error "No such article: %d" article))
9901     (gnus-summary-position-point)
9902     ;; If all commands are to be bunched up on one line, we collect
9903     ;; them here.
9904     (unless gnus-view-pseudos-separately
9905       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9906             files action)
9907         (while ps
9908           (setq action (cdr (assq 'action (car ps))))
9909           (setq files (list (cdr (assq 'name (car ps)))))
9910           (while (and ps (cdr ps)
9911                       (string= (or action "1")
9912                                (or (cdr (assq 'action (cadr ps))) "2")))
9913             (push (cdr (assq 'name (cadr ps))) files)
9914             (setcdr ps (cddr ps)))
9915           (when files
9916             (when (not (string-match "%s" action))
9917               (push " " files))
9918             (push " " files)
9919             (when (assq 'execute (car ps))
9920               (setcdr (assq 'execute (car ps))
9921                       (funcall (if (string-match "%s" action)
9922                                    'format 'concat)
9923                                action
9924                                (mapconcat
9925                                 (lambda (f)
9926                                   (if (equal f " ")
9927                                       f
9928                                     (gnus-quote-arg-for-sh-or-csh f)))
9929                                 files " ")))))
9930           (setq ps (cdr ps)))))
9931     (if (and gnus-view-pseudos (not not-view))
9932         (while pslist
9933           (when (assq 'execute (car pslist))
9934             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9935                                   (eq gnus-view-pseudos 'not-confirm)))
9936           (setq pslist (cdr pslist)))
9937       (save-excursion
9938         (while pslist
9939           (setq after-article (or (cdr (assq 'article (car pslist)))
9940                                   (gnus-summary-article-number)))
9941           (gnus-summary-goto-subject after-article)
9942           (forward-line 1)
9943           (setq b (point))
9944           (insert "    " (file-name-nondirectory
9945                           (cdr (assq 'name (car pslist))))
9946                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9947           (setq e (point))
9948           (forward-line -1)             ; back to `b'
9949           (gnus-add-text-properties
9950            b (1- e) (list 'gnus-number gnus-reffed-article-number
9951                           gnus-mouse-face-prop gnus-mouse-face))
9952           (gnus-data-enter
9953            after-article gnus-reffed-article-number
9954            gnus-unread-mark b (car pslist) 0 (- e b))
9955           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9956           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9957           (setq pslist (cdr pslist)))))))
9958
9959 (defun gnus-pseudos< (p1 p2)
9960   (let ((c1 (cdr (assq 'action p1)))
9961         (c2 (cdr (assq 'action p2))))
9962     (and c1 c2 (string< c1 c2))))
9963
9964 (defun gnus-request-pseudo-article (props)
9965   (cond ((assq 'execute props)
9966          (gnus-execute-command (cdr (assq 'execute props)))))
9967   (let ((gnus-current-article (gnus-summary-article-number)))
9968     (gnus-run-hooks 'gnus-mark-article-hook)))
9969
9970 (defun gnus-execute-command (command &optional automatic)
9971   (save-excursion
9972     (gnus-article-setup-buffer)
9973     (set-buffer gnus-article-buffer)
9974     (setq buffer-read-only nil)
9975     (let ((command (if automatic command
9976                      (read-string "Command: " (cons command 0)))))
9977       (erase-buffer)
9978       (insert "$ " command "\n\n")
9979       (if gnus-view-pseudo-asynchronously
9980           (start-process "gnus-execute" (current-buffer) shell-file-name
9981                          shell-command-switch command)
9982         (call-process shell-file-name nil t nil
9983                       shell-command-switch command)))))
9984
9985 ;; Summary kill commands.
9986
9987 (defun gnus-summary-edit-global-kill (article)
9988   "Edit the \"global\" kill file."
9989   (interactive (list (gnus-summary-article-number)))
9990   (gnus-group-edit-global-kill article))
9991
9992 (defun gnus-summary-edit-local-kill ()
9993   "Edit a local kill file applied to the current newsgroup."
9994   (interactive)
9995   (setq gnus-current-headers (gnus-summary-article-header))
9996   (gnus-group-edit-local-kill
9997    (gnus-summary-article-number) gnus-newsgroup-name))
9998
9999 ;;; Header reading.
10000
10001 (defun gnus-read-header (id &optional header)
10002   "Read the headers of article ID and enter them into the Gnus system."
10003   (let ((group gnus-newsgroup-name)
10004         (gnus-override-method
10005          (or
10006           gnus-override-method
10007           (and (gnus-news-group-p gnus-newsgroup-name)
10008                (car (gnus-refer-article-methods)))))
10009         where)
10010     ;; First we check to see whether the header in question is already
10011     ;; fetched.
10012     (if (stringp id)
10013         ;; This is a Message-ID.
10014         (setq header (or header (gnus-id-to-header id)))
10015       ;; This is an article number.
10016       (setq header (or header (gnus-summary-article-header id))))
10017     (if (and header
10018              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10019         ;; We have found the header.
10020         header
10021       ;; If this is a sparse article, we have to nix out its
10022       ;; previous entry in the thread hashtb.
10023       (when (and header
10024                  (gnus-summary-article-sparse-p (mail-header-number header)))
10025         (let* ((parent (gnus-parent-id (mail-header-references header)))
10026                (thread (and parent (gnus-id-to-thread parent))))
10027           (when thread
10028             (delq (assq header thread) thread))))
10029       ;; We have to really fetch the header to this article.
10030       (save-excursion
10031         (set-buffer nntp-server-buffer)
10032         (when (setq where (gnus-request-head id group))
10033           (nnheader-fold-continuation-lines)
10034           (goto-char (point-max))
10035           (insert ".\n")
10036           (goto-char (point-min))
10037           (insert "211 ")
10038           (princ (cond
10039                   ((numberp id) id)
10040                   ((cdr where) (cdr where))
10041                   (header (mail-header-number header))
10042                   (t gnus-reffed-article-number))
10043                  (current-buffer))
10044           (insert " Article retrieved.\n"))
10045         (if (or (not where)
10046                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10047             ()                          ; Malformed head.
10048           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10049             (when (and (stringp id)
10050                        (not (string= (gnus-group-real-name group)
10051                                      (car where))))
10052               ;; If we fetched by Message-ID and the article came
10053               ;; from a different group, we fudge some bogus article
10054               ;; numbers for this article.
10055               (mail-header-set-number header gnus-reffed-article-number))
10056             (save-excursion
10057               (set-buffer gnus-summary-buffer)
10058               (decf gnus-reffed-article-number)
10059               (gnus-remove-header (mail-header-number header))
10060               (push header gnus-newsgroup-headers)
10061               (setq gnus-current-headers header)
10062               (push (mail-header-number header) gnus-newsgroup-limit)))
10063           header)))))
10064
10065 (defun gnus-remove-header (number)
10066   "Remove header NUMBER from `gnus-newsgroup-headers'."
10067   (if (and gnus-newsgroup-headers
10068            (= number (mail-header-number (car gnus-newsgroup-headers))))
10069       (pop gnus-newsgroup-headers)
10070     (let ((headers gnus-newsgroup-headers))
10071       (while (and (cdr headers)
10072                   (not (= number (mail-header-number (cadr headers)))))
10073         (pop headers))
10074       (when (cdr headers)
10075         (setcdr headers (cddr headers))))))
10076
10077 ;;;
10078 ;;; summary highlights
10079 ;;;
10080
10081 (defun gnus-highlight-selected-summary ()
10082   "Highlight selected article in summary buffer."
10083   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10084   (when gnus-summary-selected-face
10085     (save-excursion
10086       (let* ((beg (progn (beginning-of-line) (point)))
10087              (end (progn (end-of-line) (point)))
10088              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10089              (from (if (get-text-property beg gnus-mouse-face-prop)
10090                        beg
10091                      (or (next-single-property-change
10092                           beg gnus-mouse-face-prop nil end)
10093                          beg)))
10094              (to
10095               (if (= from end)
10096                   (- from 2)
10097                 (or (next-single-property-change
10098                      from gnus-mouse-face-prop nil end)
10099                     end))))
10100         ;; If no mouse-face prop on line we will have to = from = end,
10101         ;; so we highlight the entire line instead.
10102         (when (= (+ to 2) from)
10103           (setq from beg)
10104           (setq to end))
10105         (if gnus-newsgroup-selected-overlay
10106             ;; Move old overlay.
10107             (gnus-move-overlay
10108              gnus-newsgroup-selected-overlay from to (current-buffer))
10109           ;; Create new overlay.
10110           (gnus-overlay-put
10111            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10112            'face gnus-summary-selected-face))))))
10113
10114 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10115 (defun gnus-summary-highlight-line ()
10116   "Highlight current line according to `gnus-summary-highlight'."
10117   (let* ((list gnus-summary-highlight)
10118          (p (point))
10119          (end (progn (end-of-line) (point)))
10120          ;; now find out where the line starts and leave point there.
10121          (beg (progn (beginning-of-line) (point)))
10122          (article (gnus-summary-article-number))
10123          (score (or (cdr (assq (or article gnus-current-article)
10124                                gnus-newsgroup-scored))
10125                     gnus-summary-default-score 0))
10126          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10127          (inhibit-read-only t))
10128     ;; Eval the cars of the lists until we find a match.
10129     (let ((default gnus-summary-default-score)
10130           (default-high gnus-summary-default-high-score)
10131           (default-low gnus-summary-default-low-score))
10132       (while (and list
10133                   (not (eval (caar list))))
10134         (setq list (cdr list))))
10135     (let ((face (cdar list)))
10136       (unless (eq face (get-text-property beg 'face))
10137         (gnus-put-text-property-excluding-characters-with-faces
10138          beg end 'face
10139          (setq face (if (boundp face) (symbol-value face) face)))
10140         (when gnus-summary-highlight-line-function
10141           (funcall gnus-summary-highlight-line-function article face))))
10142     (goto-char p)))
10143
10144 (defun gnus-update-read-articles (group unread &optional compute)
10145   "Update the list of read articles in GROUP."
10146   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10147          (entry (gnus-gethash group gnus-newsrc-hashtb))
10148          (info (nth 2 entry))
10149          (prev 1)
10150          (unread (sort (copy-sequence unread) '<))
10151          read)
10152     (if (or (not info) (not active))
10153         ;; There is no info on this group if it was, in fact,
10154         ;; killed.  Gnus stores no information on killed groups, so
10155         ;; there's nothing to be done.
10156         ;; One could store the information somewhere temporarily,
10157         ;; perhaps...  Hmmm...
10158         ()
10159       ;; Remove any negative articles numbers.
10160       (while (and unread (< (car unread) 0))
10161         (setq unread (cdr unread)))
10162       ;; Remove any expired article numbers
10163       (while (and unread (< (car unread) (car active)))
10164         (setq unread (cdr unread)))
10165       ;; Compute the ranges of read articles by looking at the list of
10166       ;; unread articles.
10167       (while unread
10168         (when (/= (car unread) prev)
10169           (push (if (= prev (1- (car unread))) prev
10170                   (cons prev (1- (car unread))))
10171                 read))
10172         (setq prev (1+ (car unread)))
10173         (setq unread (cdr unread)))
10174       (when (<= prev (cdr active))
10175         (push (cons prev (cdr active)) read))
10176       (setq read (if (> (length read) 1) (nreverse read) read))
10177       (if compute
10178           read
10179         (save-excursion
10180           (let (setmarkundo)
10181             ;; Propagate the read marks to the backend.
10182             (when (gnus-check-backend-function 'request-set-mark group)
10183               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10184                     (add (gnus-remove-from-range read (gnus-info-read info))))
10185                 (when (or add del)
10186                   (unless (gnus-check-group group)
10187                     (error "Can't open server for %s" group))
10188                   (gnus-request-set-mark
10189                    group (delq nil (list (if add (list add 'add '(read)))
10190                                          (if del (list del 'del '(read))))))
10191                   (setq setmarkundo
10192                         `(gnus-request-set-mark
10193                           ,group
10194                           ',(delq nil (list
10195                                        (if del (list del 'add '(read)))
10196                                        (if add (list add 'del '(read))))))))))
10197             (set-buffer gnus-group-buffer)
10198             (gnus-undo-register
10199               `(progn
10200                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10201                  (gnus-info-set-read ',info ',(gnus-info-read info))
10202                  (gnus-get-unread-articles-in-group ',info
10203                                                     (gnus-active ,group))
10204                  (gnus-group-update-group ,group t)
10205                  ,setmarkundo))))
10206         ;; Enter this list into the group info.
10207         (gnus-info-set-read info read)
10208         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10209         (gnus-get-unread-articles-in-group info (gnus-active group))
10210         t))))
10211
10212 (defun gnus-offer-save-summaries ()
10213   "Offer to save all active summary buffers."
10214   (let (buffers)
10215     ;; Go through all buffers and find all summaries.
10216     (dolist (buffer (buffer-list))
10217       (when (and (setq buffer (buffer-name buffer))
10218                  (string-match "Summary" buffer)
10219                  (save-excursion
10220                    (set-buffer buffer)
10221                    ;; We check that this is, indeed, a summary buffer.
10222                    (and (eq major-mode 'gnus-summary-mode)
10223                         ;; Also make sure this isn't bogus.
10224                         gnus-newsgroup-prepared
10225                         ;; Also make sure that this isn't a
10226                         ;; dead summary buffer.
10227                         (not gnus-dead-summary-mode))))
10228         (push buffer buffers)))
10229     ;; Go through all these summary buffers and offer to save them.
10230     (when buffers
10231       (save-excursion
10232         (map-y-or-n-p
10233          "Update summary buffer %s? "
10234          (lambda (buf)
10235            (switch-to-buffer buf)
10236            (gnus-summary-exit))
10237          buffers)))))
10238
10239
10240 ;;; @ for mime-partial
10241 ;;;
10242
10243 (defun gnus-request-partial-message ()
10244   (save-excursion
10245     (let ((number (gnus-summary-article-number))
10246           (group gnus-newsgroup-name)
10247           (mother gnus-article-buffer))
10248       (set-buffer (get-buffer-create " *Partial Article*"))
10249       (erase-buffer)
10250       (setq mime-preview-buffer mother)
10251       (gnus-request-article-this-buffer number group)
10252       (mime-parse-buffer)
10253       )))
10254
10255 (autoload 'mime-combine-message/partial-pieces-automatically
10256   "mime-partial"
10257   "Internal method to combine message/partial messages automatically.")
10258
10259 (mime-add-condition
10260  'action '((type . message)(subtype . partial)
10261            (major-mode . gnus-original-article-mode)
10262            (method . mime-combine-message/partial-pieces-automatically)
10263            (summary-buffer-exp . gnus-summary-buffer)
10264            (request-partial-message-method . gnus-request-partial-message)
10265            ))
10266
10267
10268 ;;; @ for message/rfc822
10269 ;;;
10270
10271 (defun gnus-mime-extract-message/rfc822 (entity situation)
10272   (let (group article num cwin swin cur)
10273     (with-temp-buffer
10274       (mime-insert-entity-content entity)
10275       (setq group (or (cdr (assq 'group situation))
10276                       (completing-read "Group: "
10277                                        gnus-active-hashtb
10278                                        nil
10279                                        (gnus-read-active-file-p)
10280                                        gnus-newsgroup-name))
10281             article (gnus-request-accept-article group)))
10282     (when (and (consp article)
10283                (numberp (setq article (cdr article))))
10284       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10285             cwin (get-buffer-window (current-buffer) t))
10286       (save-window-excursion
10287         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10288             (select-window swin)
10289           (set-buffer gnus-summary-buffer))
10290         (setq cur gnus-current-article)
10291         (forward-line num)
10292         (let (gnus-show-threads)
10293           (gnus-summary-goto-subject article t))
10294         (gnus-summary-clear-mark-forward 1)
10295         (gnus-summary-goto-subject cur))
10296       (when (and cwin (window-frame cwin))
10297         (select-frame (window-frame cwin)))
10298       (when (boundp 'mime-acting-situation-to-override)
10299         (set-alist 'mime-acting-situation-to-override
10300                    'group
10301                    group)
10302         (set-alist 'mime-acting-situation-to-override
10303                    'after-method
10304                    `(progn
10305                       (save-current-buffer
10306                         (set-buffer gnus-group-buffer)
10307                         (gnus-activate-group ,group))
10308                       (gnus-summary-goto-article ,cur
10309                                                  gnus-show-all-headers)))
10310         (set-alist 'mime-acting-situation-to-override
10311                    'number num)))))
10312
10313 (mime-add-condition
10314  'action '((type . message)(subtype . rfc822)
10315            (major-mode . gnus-original-article-mode)
10316            (method . gnus-mime-extract-message/rfc822)
10317            (mode . "extract")
10318            ))
10319
10320 (mime-add-condition
10321  'action '((type . message)(subtype . news)
10322            (major-mode . gnus-original-article-mode)
10323            (method . gnus-mime-extract-message/rfc822)
10324            (mode . "extract")
10325            ))
10326
10327 (defun gnus-mime-extract-multipart (entity situation)
10328   (let ((children (mime-entity-children entity))
10329         mime-acting-situation-to-override
10330         f)
10331     (while children
10332       (mime-play-entity (car children)
10333                         (cons (assq 'mode situation)
10334                               mime-acting-situation-to-override))
10335       (setq children (cdr children)))
10336     (if (setq f (cdr (assq 'after-method
10337                            mime-acting-situation-to-override)))
10338         (eval f)
10339       )))
10340
10341 (mime-add-condition
10342  'action '((type . multipart)
10343            (method . gnus-mime-extract-multipart)
10344            (mode . "extract")
10345            )
10346  'with-default)
10347
10348
10349 ;;; @ end
10350 ;;;
10351
10352 (defun gnus-summary-setup-default-charset ()
10353   "Setup newsgroup default charset."
10354   (if (equal gnus-newsgroup-name "nndraft:drafts")
10355       (setq gnus-newsgroup-charset nil)
10356     (let* ((ignored-charsets
10357             (or gnus-newsgroup-ephemeral-ignored-charsets
10358                 (append
10359                  (and gnus-newsgroup-name
10360                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10361                  gnus-newsgroup-ignored-charsets))))
10362       (setq gnus-newsgroup-charset
10363             (or gnus-newsgroup-ephemeral-charset
10364                 (and gnus-newsgroup-name
10365                      (gnus-parameter-charset gnus-newsgroup-name))
10366                 gnus-default-charset))
10367       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10368            ignored-charsets))))
10369
10370 ;;;
10371 ;;; Mime Commands
10372 ;;;
10373
10374 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10375   "Display the current article buffer fully MIME-buttonized.
10376 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10377 treated as multipart/mixed."
10378   (interactive "P")
10379   (require 'gnus-art)
10380   (let ((gnus-unbuttonized-mime-types nil)
10381         (gnus-mime-display-multipart-as-mixed show-all-parts))
10382     (gnus-summary-show-article)))
10383
10384 (defun gnus-summary-repair-multipart (article)
10385   "Add a Content-Type header to a multipart article without one."
10386   (interactive (list (gnus-summary-article-number)))
10387   (gnus-with-article article
10388     (message-narrow-to-head)
10389     (message-remove-header "Mime-Version")
10390     (goto-char (point-max))
10391     (insert "Mime-Version: 1.0\n")
10392     (widen)
10393     (when (search-forward "\n--" nil t)
10394       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10395         (message-narrow-to-head)
10396         (message-remove-header "Content-Type")
10397         (goto-char (point-max))
10398         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10399                         separator))
10400         (widen))))
10401   (let (gnus-mark-article-hook)
10402     (gnus-summary-select-article t t nil article)))
10403
10404 (defun gnus-summary-toggle-display-buttonized ()
10405   "Toggle the buttonizing of the article buffer."
10406   (interactive)
10407   (require 'gnus-art)
10408   (if (setq gnus-inhibit-mime-unbuttonizing
10409             (not gnus-inhibit-mime-unbuttonizing))
10410       (let ((gnus-unbuttonized-mime-types nil))
10411         (gnus-summary-show-article))
10412     (gnus-summary-show-article)))
10413
10414 ;;;
10415 ;;; Intelli-mouse commmands
10416 ;;;
10417
10418 (defun gnus-wheel-summary-scroll (event)
10419   (interactive "e")
10420   (let ((amount (if (memq 'shift (event-modifiers event))
10421                     (car gnus-wheel-scroll-amount)
10422                   (cdr gnus-wheel-scroll-amount)))
10423         (direction (- (* (static-if (featurep 'xemacs)
10424                              (event-button event)
10425                            (cond ((eq 'mouse-4 (event-basic-type event))
10426                                   4)
10427                                  ((eq 'mouse-5 (event-basic-type event))
10428                                   5)))
10429                          2) 9))
10430         edge)
10431     (gnus-summary-scroll-up (* amount direction))
10432     (when (gnus-eval-in-buffer-window gnus-article-buffer
10433             (save-restriction
10434               (widen)
10435               (and (if (< 0 direction)
10436                        (gnus-article-next-page 0)
10437                      (gnus-article-prev-page 0)
10438                      (bobp))
10439                    (if (setq edge (get-text-property
10440                                    (point-min) 'gnus-wheel-edge))
10441                        (setq edge (* edge direction))
10442                      (setq edge -1))
10443                    (or (plusp edge)
10444                        (let ((buffer-read-only nil)
10445                              (inhibit-read-only t))
10446                          (put-text-property (point-min) (point-max)
10447                                             'gnus-wheel-edge direction)
10448                          nil))
10449                    (or (> edge gnus-wheel-edge-resistance)
10450                        (let ((buffer-read-only nil)
10451                              (inhibit-read-only t))
10452                          (put-text-property (point-min) (point-max)
10453                                             'gnus-wheel-edge
10454                                             (* (1+ edge) direction))
10455                          nil))
10456                    (eq last-command 'gnus-wheel-summary-scroll))))
10457       (gnus-summary-next-article nil nil (minusp direction)))))
10458
10459 (defun gnus-wheel-install ()
10460   "Enable mouse wheel support on summary window."
10461   (when gnus-use-wheel
10462     (let ((keys
10463            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10464       (dolist (key keys)
10465         (define-key gnus-summary-mode-map key
10466           'gnus-wheel-summary-scroll)))))
10467
10468 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10469
10470 ;;;
10471 ;;; Traditional PGP commmands
10472 ;;;
10473
10474 (defun gnus-summary-decrypt-article (&optional force)
10475   "Decrypt the current article in traditional PGP way.
10476 This will have permanent effect only in mail groups.
10477 If FORCE is non-nil, allow editing of articles even in read-only
10478 groups."
10479   (interactive "P")
10480   (gnus-summary-select-article t)
10481   (gnus-eval-in-buffer-window gnus-article-buffer
10482     (save-excursion
10483       (save-restriction
10484         (widen)
10485         (goto-char (point-min))
10486         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10487           (error "Not a traditional PGP message!"))
10488         (let ((armor-start (match-beginning 0)))
10489           (if (and (pgg-decrypt-region armor-start (point-max))
10490                    (or force (not (gnus-group-read-only-p))))
10491               (let ((inhibit-read-only t)
10492                     buffer-read-only)
10493                 (delete-region armor-start
10494                                (progn
10495                                  (re-search-forward "^-+END PGP" nil t)
10496                                  (beginning-of-line 2)
10497                                  (point)))
10498                 (insert-buffer-substring pgg-output-buffer))))))))
10499
10500 (defun gnus-summary-verify-article ()
10501   "Verify the current article in traditional PGP way."
10502   (interactive)
10503   (save-excursion
10504     (set-buffer gnus-original-article-buffer)
10505     (goto-char (point-min))
10506     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10507       (error "Not a traditional PGP message!"))
10508     (re-search-forward "^-+END PGP" nil t)
10509     (beginning-of-line 2)
10510     (call-interactively (function pgg-verify-region))))
10511
10512 ;;;
10513 ;;; Generic summary marking commands
10514 ;;;
10515
10516 (defvar gnus-summary-marking-alist
10517   '((read gnus-del-mark "d")
10518     (unread gnus-unread-mark "u")
10519     (ticked gnus-ticked-mark "!")
10520     (dormant gnus-dormant-mark "?")
10521     (expirable gnus-expirable-mark "e"))
10522   "An alist of names/marks/keystrokes.")
10523
10524 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10525 (defvar gnus-summary-mark-map)
10526
10527 (defun gnus-summary-make-all-marking-commands ()
10528   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10529   (dolist (elem gnus-summary-marking-alist)
10530     (apply 'gnus-summary-make-marking-command elem)))
10531
10532 (defun gnus-summary-make-marking-command (name mark keystroke)
10533   (let ((map (make-sparse-keymap)))
10534     (define-key gnus-summary-generic-mark-map keystroke map)
10535     (dolist (lway `((next "next" next nil "n")
10536                     (next-unread "next unread" next t "N")
10537                     (prev "previous" prev nil "p")
10538                     (prev-unread "previous unread" prev t "P")
10539                     (nomove "" nil nil ,keystroke)))
10540       (let ((func (gnus-summary-make-marking-command-1
10541                    mark (car lway) lway name)))
10542         (setq func (eval func))
10543         (define-key map (nth 4 lway) func)))))
10544
10545 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10546   `(defun ,(intern
10547             (format "gnus-summary-put-mark-as-%s%s"
10548                     name (if (eq way 'nomove)
10549                              ""
10550                            (concat "-" (symbol-name way)))))
10551      (n)
10552      ,(format
10553        "Mark the current article as %s%s.
10554 If N, the prefix, then repeat N times.
10555 If N is negative, move in reverse order.
10556 The difference between N and the actual number of articles marked is
10557 returned."
10558        name (car (cdr lway)))
10559      (interactive "p")
10560      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10561
10562 (defun gnus-summary-generic-mark (n mark move unread)
10563   "Mark N articles with MARK."
10564   (unless (eq major-mode 'gnus-summary-mode)
10565     (error "This command can only be used in the summary buffer"))
10566   (gnus-summary-show-thread)
10567   (let ((nummove
10568          (cond
10569           ((eq move 'next) 1)
10570           ((eq move 'prev) -1)
10571           (t 0))))
10572     (if (zerop nummove)
10573         (setq n 1)
10574       (when (< n 0)
10575         (setq n (abs n)
10576               nummove (* -1 nummove))))
10577     (while (and (> n 0)
10578                 (gnus-summary-mark-article nil mark)
10579                 (zerop (gnus-summary-next-subject nummove unread t)))
10580       (setq n (1- n)))
10581     (when (/= 0 n)
10582       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10583     (gnus-summary-recenter)
10584     (gnus-summary-position-point)
10585     (gnus-set-mode-line 'summary)
10586     n))
10587
10588 (defun gnus-summary-insert-articles (articles)
10589   (when (setq articles
10590               (gnus-set-difference articles
10591                                    (mapcar (lambda (h) (mail-header-number h))
10592                                            gnus-newsgroup-headers)))
10593     (setq gnus-newsgroup-headers 
10594           (merge 'list
10595                  gnus-newsgroup-headers
10596                  (gnus-fetch-headers articles)
10597                  'gnus-article-sort-by-number))
10598     ;; Suppress duplicates?
10599     (when gnus-suppress-duplicates
10600       (gnus-dup-suppress-articles))
10601     
10602     ;; We might want to build some more threads first.
10603     (when (and gnus-fetch-old-headers
10604                (eq gnus-headers-retrieved-by 'nov))
10605       (if (eq gnus-fetch-old-headers 'invisible)
10606         (gnus-build-all-threads)
10607         (gnus-build-old-threads)))
10608     ;; Let the Gnus agent mark articles as read.
10609     (when gnus-agent
10610       (gnus-agent-get-undownloaded-list))
10611     ;; Remove list identifiers from subject
10612     (when gnus-list-identifiers
10613       (gnus-summary-remove-list-identifiers))
10614     ;; First and last article in this newsgroup.
10615     (when gnus-newsgroup-headers
10616       (setq gnus-newsgroup-begin
10617             (mail-header-number (car gnus-newsgroup-headers))
10618             gnus-newsgroup-end
10619             (mail-header-number
10620              (gnus-last-element gnus-newsgroup-headers))))
10621     (when gnus-use-scoring
10622       (gnus-possibly-score-headers))))
10623
10624 (defun gnus-summary-insert-old-articles (&optional all)
10625   "Insert all old articles in this group.
10626 If ALL is non-nil, already read articles become readable.
10627 If ALL is a number, fetch this number of articles."
10628   (interactive "P")
10629   (prog1
10630       (let ((old (mapcar 'car gnus-newsgroup-data))
10631             (i (car gnus-newsgroup-active))
10632             older len)
10633         (while (<= i (cdr gnus-newsgroup-active))
10634           (or (memq i old) (push i older))
10635           (incf i))
10636         (setq len (length older))
10637         (cond 
10638          ((null older) nil)
10639          ((numberp all) 
10640           (if (< all len)
10641               (setq older (subseq older 0 all))))
10642          (all nil)
10643          (t
10644           (if (and (numberp gnus-large-newsgroup)
10645                    (> len gnus-large-newsgroup))
10646               (let ((input
10647                      (read-string
10648                       (format
10649                        "How many articles from %s (default %d): "
10650                        (gnus-limit-string 
10651                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10652                        len))))
10653                 (unless (string-match "^[ \t]*$" input) 
10654                   (setq all (string-to-number input))
10655                   (if (< all len)
10656                       (setq older (subseq older 0 all))))))))
10657         (if (not older)
10658             (message "No old news.")
10659           (gnus-summary-insert-articles older)
10660           (gnus-summary-limit (gnus-union older old))))
10661     (gnus-summary-position-point)))
10662
10663 (defun gnus-summary-insert-new-articles ()
10664   "Insert all new articles in this group."
10665   (interactive)
10666   (prog1
10667       (let ((old (mapcar 'car gnus-newsgroup-data))
10668             (old-active gnus-newsgroup-active)
10669             (nnmail-fetched-sources (list t))
10670             i new)
10671         (setq gnus-newsgroup-active 
10672               (gnus-activate-group gnus-newsgroup-name 'scan))
10673         (setq i (1+ (cdr old-active)))
10674         (while (<= i (cdr gnus-newsgroup-active))
10675           (push i new)
10676           (incf i))
10677         (if (not new)
10678             (message "No gnus is bad news.")
10679           (setq new (nreverse new))
10680           (gnus-summary-insert-articles new)
10681           (setq gnus-newsgroup-unreads
10682                 (append gnus-newsgroup-unreads new))
10683           (gnus-summary-limit (gnus-union old new))))
10684     (gnus-summary-position-point)))
10685
10686 (gnus-summary-make-all-marking-commands)
10687
10688 (gnus-ems-redefine)
10689
10690 (provide 'gnus-sum)
10691
10692 (run-hooks 'gnus-sum-load-hook)
10693
10694 ;;; gnus-sum.el ends here