* lisp/gnus-namazu.el (gnus-namazu-search): Slightly modified for
[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 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 If threads are hidden, you have to run the command
238 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
239 to expose hidden threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-hide-killed t
244   "*If non-nil, hide killed threads automatically."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-ignore-subject t
249   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
250 If nil, articles that have different subjects from their parents will
251 start separate threads."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-operation-ignore-subject t
256   "*If non-nil, subjects will be ignored when doing thread commands.
257 This affects commands like `gnus-summary-kill-thread' and
258 `gnus-summary-lower-thread'.
259
260 If this variable is nil, articles in the same thread with different
261 subjects will not be included in the operation in question.  If this
262 variable is `fuzzy', only articles that have subjects that are fuzzily
263 equal will be included."
264   :group 'gnus-thread
265   :type '(choice (const :tag "off" nil)
266                  (const fuzzy)
267                  (sexp :tag "on" t)))
268
269 (defcustom gnus-thread-indent-level 4
270   "*Number that says how much each sub-thread should be indented."
271   :group 'gnus-thread
272   :type 'integer)
273
274 (defcustom gnus-auto-extend-newsgroup t
275   "*If non-nil, extend newsgroup forward and backward when requested."
276   :group 'gnus-summary-choose
277   :type 'boolean)
278
279 (defcustom gnus-auto-select-first t
280   "*If nil, don't select the first unread article when entering a group.
281 If this variable is `best', select the highest-scored unread article
282 in the group.  If t, select the first unread article.
283
284 This variable can also be a function to place point on a likely
285 subject line.  Useful values include `gnus-summary-first-unread-subject',
286 `gnus-summary-first-unread-article' and
287 `gnus-summary-best-unread-article'.
288
289 If you want to prevent automatic selection of the first unread article
290 in some newsgroups, set the variable to nil in
291 `gnus-select-group-hook'."
292   :group 'gnus-group-select
293   :type '(choice (const :tag "none" nil)
294                  (const best)
295                  (sexp :menu-tag "first" t)
296                  (function-item gnus-summary-first-unread-subject)
297                  (function-item gnus-summary-first-unread-article)
298                  (function-item gnus-summary-best-unread-article)))
299
300 (defcustom gnus-dont-select-after-jump-to-other-group nil
301   "If non-nil, don't select the first unread article after entering the
302 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
303 it is depend on the value of `gnus-auto-select-first' whether to select
304 or not."
305   :group 'gnus-group-select
306   :type 'boolean)
307
308 (defcustom gnus-auto-select-next t
309   "*If non-nil, offer to go to the next group from the end of the previous.
310 If the value is t and the next newsgroup is empty, Gnus will exit
311 summary mode and go back to group mode.  If the value is neither nil
312 nor t, Gnus will select the following unread newsgroup.  In
313 particular, if the value is the symbol `quietly', the next unread
314 newsgroup will be selected without any confirmation, and if it is
315 `almost-quietly', the next group will be selected without any
316 confirmation if you are located on the last article in the group.
317 Finally, if this variable is `slightly-quietly', the `Z n' command
318 will go to the next group without confirmation."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "off" nil)
321                  (const quietly)
322                  (const almost-quietly)
323                  (const slightly-quietly)
324                  (sexp :menu-tag "on" t)))
325
326 (defcustom gnus-auto-select-same nil
327   "*If non-nil, select the next article with the same subject.
328 If there are no more articles with the same subject, go to
329 the first unread article."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-summary-check-current nil
334   "*If non-nil, consider the current article when moving.
335 The \"unread\" movement commands will stay on the same line if the
336 current article is unread."
337   :group 'gnus-summary-maneuvering
338   :type 'boolean)
339
340 (defcustom gnus-auto-center-summary t
341   "*If non-nil, always center the current summary buffer.
342 In particular, if `vertical' do only vertical recentering.  If non-nil
343 and non-`vertical', do both horizontal and vertical recentering."
344   :group 'gnus-summary-maneuvering
345   :type '(choice (const :tag "none" nil)
346                  (const vertical)
347                  (integer :tag "height")
348                  (sexp :menu-tag "both" t)))
349
350 (defcustom gnus-show-all-headers nil
351   "*If non-nil, don't hide any headers."
352   :group 'gnus-article-hiding
353   :group 'gnus-article-headers
354   :type 'boolean)
355
356 (defcustom gnus-summary-ignore-duplicates nil
357   "*If non-nil, ignore articles with identical Message-ID headers."
358   :group 'gnus-summary
359   :type 'boolean)
360
361 (defcustom gnus-single-article-buffer t
362   "*If non-nil, display all articles in the same buffer.
363 If nil, each group will get its own article buffer."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-break-pages t
368   "*If non-nil, do page breaking on articles.
369 The page delimiter is specified by the `gnus-page-delimiter'
370 variable."
371   :group 'gnus-article-various
372   :type 'boolean)
373
374 (defcustom gnus-show-mime t
375   "*If non-nil, do mime processing of articles.
376 The articles will simply be fed to the function given by
377 `gnus-article-display-method-for-mime'."
378   :group 'gnus-article-mime
379   :type 'boolean)
380
381 (defcustom gnus-move-split-methods nil
382   "*Variable used to suggest where articles are to be moved to.
383 It uses the same syntax as the `gnus-split-methods' variable.
384 However, whereas `gnus-split-methods' specifies file names as targets,
385 this variable specifies group names."
386   :group 'gnus-summary-mail
387   :type '(repeat (choice (list :value (fun) function)
388                          (cons :value ("" "") regexp (repeat string))
389                          (sexp :value nil))))
390
391 (defcustom gnus-unread-mark ?\ ;;;Whitespace
392   "*Mark used for unread articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-ticked-mark ?!
397   "*Mark used for ticked articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-dormant-mark ??
402   "*Mark used for dormant articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-del-mark ?r
407   "*Mark used for del'd articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-read-mark ?R
412   "*Mark used for read articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-expirable-mark ?E
417   "*Mark used for expirable articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-killed-mark ?K
422   "*Mark used for killed articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-souped-mark ?F
427   "*Mark used for souped articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-kill-file-mark ?X
432   "*Mark used for articles killed by kill files."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-low-score-mark ?Y
437   "*Mark used for articles with a low score."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-catchup-mark ?C
442   "*Mark used for articles that are caught up."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-replied-mark ?A
447   "*Mark used for articles that have been replied to."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-forwarded-mark ?O
452   "*Mark used for articles that have been forwarded."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-cached-mark ?*
457   "*Mark used for articles that are in the cache."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-saved-mark ?S
462   "*Mark used for articles that have been saved to."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-no-mark ?\ ;;;Whitespace
467   "*Mark used for articles that have no other secondary mark."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-ancient-mark ?O
472   "*Mark used for ancient articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-sparse-mark ?Q
477   "*Mark used for sparsely reffed articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-canceled-mark ?G
482   "*Mark used for canceled articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-duplicate-mark ?M
487   "*Mark used for duplicate articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-undownloaded-mark ?@
492   "*Mark used for articles that weren't downloaded."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-downloadable-mark ?%
497   "*Mark used for articles that are to be downloaded."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-unsendable-mark ?=
502   "*Mark used for articles that won't be sent."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-score-over-mark ?+
507   "*Score mark used for articles with high scores."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-score-below-mark ?-
512   "*Score mark used for articles with low scores."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
517   "*There is no thread under the article."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-not-empty-thread-mark ?=
522   "*There is a thread under the article."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-view-pseudo-asynchronously nil
527   "*If non-nil, Gnus will view pseudo-articles asynchronously."
528   :group 'gnus-extract-view
529   :type 'boolean)
530
531 (defcustom gnus-auto-expirable-marks
532   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
533         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
534         gnus-souped-mark gnus-duplicate-mark)
535   "*The list of marks converted into expiration if a group is auto-expirable."
536   :version "21.1"
537   :group 'gnus-summary
538   :type '(repeat character))
539
540 (defcustom gnus-inhibit-user-auto-expire t
541   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
542   :version "21.1"
543   :group 'gnus-summary
544   :type 'boolean)
545
546 (defcustom gnus-view-pseudos nil
547   "*If `automatic', pseudo-articles will be viewed automatically.
548 If `not-confirm', pseudos will be viewed automatically, and the user
549 will not be asked to confirm the command."
550   :group 'gnus-extract-view
551   :type '(choice (const :tag "off" nil)
552                  (const automatic)
553                  (const not-confirm)))
554
555 (defcustom gnus-view-pseudos-separately t
556   "*If non-nil, one pseudo-article will be created for each file to be viewed.
557 If nil, all files that use the same viewing command will be given as a
558 list of parameters to that command."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-insert-pseudo-articles t
563   "*If non-nil, insert pseudo-articles when decoding articles."
564   :group 'gnus-extract-view
565   :type 'boolean)
566
567 (defcustom gnus-summary-dummy-line-format
568   "  %(:                          :%) %S\n"
569   "*The format specification for the dummy roots in the summary buffer.
570 It works along the same lines as a normal formatting string,
571 with some simple extensions.
572
573 %S  The subject"
574   :group 'gnus-threading
575   :type 'string)
576
577 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
578   "*The format specification for the summary mode line.
579 It works along the same lines as a normal formatting string,
580 with some simple extensions:
581
582 %G  Group name
583 %p  Unprefixed group name
584 %A  Current article number
585 %z  Current article score
586 %V  Gnus version
587 %U  Number of unread articles in the group
588 %e  Number of unselected articles in the group
589 %Z  A string with unread/unselected article counts
590 %g  Shortish group name
591 %S  Subject of the current article
592 %u  User-defined spec
593 %s  Current score file name
594 %d  Number of dormant articles
595 %r  Number of articles that have been marked as read in this session
596 %E  Number of articles expunged by the score files"
597   :group 'gnus-summary-format
598   :type 'string)
599
600 (defcustom gnus-list-identifiers nil
601   "Regexp that matches list identifiers to be removed from subject.
602 This can also be a list of regexps."
603   :version "21.1"
604   :group 'gnus-summary-format
605   :group 'gnus-article-hiding
606   :type '(choice (const :tag "none" nil)
607                  (regexp :value ".*")
608                  (repeat :value (".*") regexp)))
609
610 (defcustom gnus-summary-mark-below 0
611   "*Mark all articles with a score below this variable as read.
612 This variable is local to each summary buffer and usually set by the
613 score file."
614   :group 'gnus-score-default
615   :type 'integer)
616
617 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
618   "*List of functions used for sorting articles in the summary buffer.
619
620 Each function takes two articles and returns non-nil if the first
621 article should be sorted before the other.  If you use more than one
622 function, the primary sort function should be the last.  You should
623 probably always include `gnus-article-sort-by-number' in the list of
624 sorting functions -- preferably first.  Also note that sorting by date
625 is often much slower than sorting by number, and the sorting order is
626 very similar.  (Sorting by date means sorting by the time the message
627 was sent, sorting by number means sorting by arrival time.)
628
629 Ready-made functions include `gnus-article-sort-by-number',
630 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
631 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
632
633 When threading is turned on, the variable `gnus-thread-sort-functions'
634 controls how articles are sorted."
635   :group 'gnus-summary-sort
636   :type '(repeat (choice (function-item gnus-article-sort-by-number)
637                          (function-item gnus-article-sort-by-author)
638                          (function-item gnus-article-sort-by-subject)
639                          (function-item gnus-article-sort-by-date)
640                          (function-item gnus-article-sort-by-score)
641                          (function :tag "other"))))
642
643 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
644   "*List of functions used for sorting threads in the summary buffer.
645 By default, threads are sorted by article number.
646
647 Each function takes two threads and returns non-nil if the first
648 thread should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-thread-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-thread-sort-by-number',
657 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
658 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
659 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
660
661 When threading is turned off, the variable
662 `gnus-article-sort-functions' controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
665                          (function-item gnus-thread-sort-by-author)
666                          (function-item gnus-thread-sort-by-subject)
667                          (function-item gnus-thread-sort-by-date)
668                          (function-item gnus-thread-sort-by-score)
669                          (function-item gnus-thread-sort-by-total-score)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-score-function '+
673   "*Function used for calculating the total score of a thread.
674
675 The function is called with the scores of the article and each
676 subthread and should then return the score of the thread.
677
678 Some functions you can use are `+', `max', or `min'."
679   :group 'gnus-summary-sort
680   :type 'function)
681
682 (defcustom gnus-summary-expunge-below nil
683   "All articles that have a score less than this variable will be expunged.
684 This variable is local to the summary buffers."
685   :group 'gnus-score-default
686   :type '(choice (const :tag "off" nil)
687                  integer))
688
689 (defcustom gnus-thread-expunge-below nil
690   "All threads that have a total score less than this variable will be expunged.
691 See `gnus-thread-score-function' for en explanation of what a
692 \"thread score\" is.
693
694 This variable is local to the summary buffers."
695   :group 'gnus-threading
696   :group 'gnus-score-default
697   :type '(choice (const :tag "off" nil)
698                  integer))
699
700 (defcustom gnus-summary-mode-hook nil
701   "*A hook for Gnus summary mode.
702 This hook is run before any variables are set in the summary buffer."
703   :options '(turn-on-gnus-mailing-list-mode)
704   :group 'gnus-summary-various
705   :type 'hook)
706
707 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708 (when (featurep 'xemacs)
709   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
710   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
711   (add-hook 'gnus-summary-mode-hook
712             'gnus-xmas-switch-horizontal-scrollbar-off))
713
714 (defcustom gnus-summary-menu-hook nil
715   "*Hook run after the creation of the summary mode menu."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-summary-exit-hook nil
720   "*A hook called on exit from the summary buffer.
721 It will be called with point in the group buffer."
722   :group 'gnus-summary-exit
723   :type 'hook)
724
725 (defcustom gnus-summary-prepare-hook nil
726   "*A hook called after the summary buffer has been generated.
727 If you want to modify the summary buffer, you can use this hook."
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 (defcustom gnus-summary-prepared-hook nil
732   "*A hook called as the last thing after the summary buffer has been generated."
733   :group 'gnus-summary-various
734   :type 'hook)
735
736 (defcustom gnus-summary-generate-hook nil
737   "*A hook run just before generating the summary buffer.
738 This hook is commonly used to customize threading variables and the
739 like."
740   :group 'gnus-summary-various
741   :type 'hook)
742
743 (defcustom gnus-select-group-hook nil
744   "*A hook called when a newsgroup is selected.
745
746 If you'd like to simplify subjects like the
747 `gnus-summary-next-same-subject' command does, you can use the
748 following hook:
749
750  (setq gnus-select-group-hook
751       (list
752         (lambda ()
753           (mapcar (lambda (header)
754                      (mail-header-set-subject
755                       header
756                       (gnus-simplify-subject
757                        (mail-header-subject header) 're-only)))
758                   gnus-newsgroup-headers))))"
759   :group 'gnus-group-select
760   :type 'hook)
761
762 (defcustom gnus-select-article-hook nil
763   "*A hook called when an article is selected."
764   :group 'gnus-summary-choose
765   :type 'hook)
766
767 (defcustom gnus-visual-mark-article-hook
768   (list 'gnus-highlight-selected-summary)
769   "*Hook run after selecting an article in the summary buffer.
770 It is meant to be used for highlighting the article in some way.  It
771 is not run if `gnus-visual' is nil."
772   :group 'gnus-summary-visual
773   :type 'hook)
774
775 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
776   "*A hook called before parsing the headers."
777   :group 'gnus-various
778   :type 'hook)
779
780 (defcustom gnus-exit-group-hook nil
781   "*A hook called when exiting summary mode.
782 This hook is not called from the non-updating exit commands like `Q'."
783   :group 'gnus-various
784   :type 'hook)
785
786 (defcustom gnus-summary-update-hook
787   (list 'gnus-summary-highlight-line)
788   "*A hook called when a summary line is changed.
789 The hook will not be called if `gnus-visual' is nil.
790
791 The default function `gnus-summary-highlight-line' will
792 highlight the line according to the `gnus-summary-highlight'
793 variable."
794   :group 'gnus-summary-visual
795   :type 'hook)
796
797 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
798   "*A hook called when an article is selected for the first time.
799 The hook is intended to mark an article as read (or unread)
800 automatically when it is selected."
801   :group 'gnus-summary-choose
802   :type 'hook)
803
804 (defcustom gnus-group-no-more-groups-hook nil
805   "*A hook run when returning to group mode having no more (unread) groups."
806   :group 'gnus-group-select
807   :type 'hook)
808
809 (defcustom gnus-ps-print-hook nil
810   "*A hook run before ps-printing something from Gnus."
811   :group 'gnus-summary
812   :type 'hook)
813
814 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
815   "Face used for highlighting the current article in the summary buffer."
816   :group 'gnus-summary-visual
817   :type 'face)
818
819 (defcustom gnus-summary-highlight
820   '(((= mark gnus-canceled-mark)
821      . gnus-summary-cancelled-face)
822     ((and (> score default)
823           (or (= mark gnus-dormant-mark)
824               (= mark gnus-ticked-mark)))
825      . gnus-summary-high-ticked-face)
826     ((and (< score default)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-low-ticked-face)
830     ((or (= mark gnus-dormant-mark)
831          (= mark gnus-ticked-mark))
832      . gnus-summary-normal-ticked-face)
833     ((and (> score default) (= mark gnus-ancient-mark))
834      . gnus-summary-high-ancient-face)
835     ((and (< score default) (= mark gnus-ancient-mark))
836      . gnus-summary-low-ancient-face)
837     ((= mark gnus-ancient-mark)
838      . gnus-summary-normal-ancient-face)
839     ((and (> score default) (= mark gnus-unread-mark))
840      . gnus-summary-high-unread-face)
841     ((and (< score default) (= mark gnus-unread-mark))
842      . gnus-summary-low-unread-face)
843     ((and (memq article gnus-newsgroup-incorporated)
844           (= mark gnus-unread-mark))
845      . gnus-summary-incorporated-face)
846     ((= mark gnus-unread-mark)
847      . gnus-summary-normal-unread-face)
848     ((and (> score default) (memq mark (list gnus-downloadable-mark
849                                              gnus-undownloaded-mark)))
850      . gnus-summary-high-unread-face)
851     ((and (< score default) (memq mark (list gnus-downloadable-mark
852                                              gnus-undownloaded-mark)))
853      . gnus-summary-low-unread-face)
854     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
855           (memq article gnus-newsgroup-unreads))
856      . gnus-summary-normal-unread-face)
857     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
858      . gnus-summary-normal-read-face)
859     ((> score default)
860      . gnus-summary-high-read-face)
861     ((< score default)
862      . gnus-summary-low-read-face)
863     (t
864      . gnus-summary-normal-read-face))
865   "*Controls the highlighting of summary buffer lines.
866
867 A list of (FORM . FACE) pairs.  When deciding how a a particular
868 summary line should be displayed, each form is evaluated.  The content
869 of the face field after the first true form is used.  You can change
870 how those summary lines are displayed, by editing the face field.
871
872 You can use the following variables in the FORM field.
873
874 score:   The articles score
875 default: The default article score.
876 below:   The score below which articles are automatically marked as read.
877 mark:    The articles mark."
878   :group 'gnus-summary-visual
879   :type '(repeat (cons (sexp :tag "Form" nil)
880                        face)))
881
882 (defcustom gnus-alter-header-function nil
883   "Function called to allow alteration of article header structures.
884 The function is called with one parameter, the article header vector,
885 which it may alter in any way.")
886
887 (defvar gnus-decode-encoded-word-function
888   (mime-find-field-decoder 'From 'nov)
889   "Variable that says which function should be used to decode a string with encoded words.")
890
891 (defcustom gnus-extra-headers nil
892   "*Extra headers to parse."
893   :version "21.1"
894   :group 'gnus-summary
895   :type '(repeat symbol))
896
897 (defcustom gnus-ignored-from-addresses
898   (and user-mail-address (regexp-quote user-mail-address))
899   "*Regexp of From headers that may be suppressed in favor of To headers."
900   :version "21.1"
901   :group 'gnus-summary
902   :type 'regexp)
903
904 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
905   "List of charsets that should be ignored.
906 When these charsets are used in the \"charset\" parameter, the
907 default charset will be used instead."
908   :version "21.1"
909   :type '(repeat symbol)
910   :group 'gnus-charset)
911
912 (gnus-define-group-parameter
913  ignored-charsets
914  :type list
915  :function-document
916  "Return the ignored charsets of GROUP."
917  :variable gnus-group-ignored-charsets-alist
918  :variable-default 
919  '(("alt\\.chinese\\.text" iso-8859-1))
920  :variable-document
921  "Alist of regexps (to match group names) and charsets that should be ignored.
922 When these charsets are used in the \"charset\" parameter, the
923 default charset will be used instead."
924  :variable-group gnus-charset
925  :variable-type '(repeat (cons (regexp :tag "Group")
926                                (repeat symbol)))
927  :parameter-type '(choice :tag "Ignored charsets" 
928                           :value nil
929                           (repeat (symbol)))
930  :parameter-document       "\
931 List of charsets that should be ignored.
932
933 When these charsets are used in the \"charset\" parameter, the
934 default charset will be used instead.")
935
936 (defcustom gnus-group-highlight-words-alist nil
937   "Alist of group regexps and highlight regexps.
938 This variable uses the same syntax as `gnus-emphasis-alist'."
939   :version "21.1"
940   :type '(repeat (cons (regexp :tag "Group")
941                        (repeat (list (regexp :tag "Highlight regexp")
942                                      (number :tag "Group for entire word" 0)
943                                      (number :tag "Group for displayed part" 0)
944                                      (symbol :tag "Face"
945                                              gnus-emphasis-highlight-words)))))
946   :group 'gnus-summary-visual)
947
948 (defcustom gnus-use-wheel nil
949   "Use Intelli-mouse on summary movement"
950   :type 'boolean
951   :group 'gnus-summary-maneuvering)
952
953 (defcustom gnus-wheel-scroll-amount '(5 . 1)
954   "Amount to scroll messages by spinning the mouse wheel.
955 This is actually a cons cell, where the first item is the amount to scroll
956 on a normal wheel event, and the second is the amount to scroll when the
957 wheel is moved with the shift key depressed."
958   :type '(cons (integer :tag "Shift") integer)
959   :group 'gnus-summary-maneuvering)
960
961 (defcustom gnus-wheel-edge-resistance 2
962   "How hard it should be to change the current article
963 by moving the mouse over the edge of the article window."
964   :type 'integer
965   :group 'gnus-summary-maneuvering)
966
967 (defcustom gnus-summary-show-article-charset-alist
968   nil
969   "Alist of number and charset.
970 The article will be shown with the charset corresponding to the
971 numbered argument.
972 For example: ((1 . cn-gb-2312) (2 . big5))."
973   :version "21.1"
974   :type '(repeat (cons (number :tag "Argument" 1)
975                        (symbol :tag "Charset")))
976   :group 'gnus-charset)
977
978 (defcustom gnus-preserve-marks t
979   "Whether marks are preserved when moving, copying and respooling messages."
980   :version "21.1"
981   :type 'boolean
982   :group 'gnus-summary-marks)
983
984 (defcustom gnus-alter-articles-to-read-function nil
985   "Function to be called to alter the list of articles to be selected."
986   :type '(choice (const nil) function)
987   :group 'gnus-summary)
988
989 (defcustom gnus-orphan-score nil
990   "*All orphans get this score added.  Set in the score file."
991   :group 'gnus-score-default
992   :type '(choice (const nil)
993                  integer))
994
995 (defcustom gnus-summary-save-parts-default-mime "image/.*"
996   "*A regexp to match MIME parts when saving multiple parts of a message
997 with gnus-summary-save-parts (X m). This regexp will be used by default
998 when prompting the user for which type of files to save."
999   :group 'gnus-summary
1000   :type 'regexp)
1001
1002
1003 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1004   "*A regexp to match MIME parts when saving multiple parts of a message
1005 with gnus-summary-save-parts (X m). This regexp will be used by default
1006 when prompting the user for which type of files to save."
1007   :group 'gnus-summary
1008   :type 'regexp)
1009
1010 (defcustom gnus-read-all-available-headers nil
1011   "Whether Gnus should parse all headers made available to it.
1012 This is mostly relevant for slow backends where the user may
1013 wish to widen the summary buffer to include all headers
1014 that were fetched.  Say, for nnultimate groups."
1015   :group 'gnus-summary
1016   :type '(choice boolean regexp))
1017
1018 ;;; Internal variables
1019
1020 (defvar gnus-article-mime-handles nil)
1021 (defvar gnus-article-decoded-p nil)
1022 (defvar gnus-article-charset nil)
1023 (defvar gnus-article-ignored-charsets nil)
1024 (defvar gnus-scores-exclude-files nil)
1025 (defvar gnus-page-broken nil)
1026 (defvar gnus-inhibit-mime-unbuttonizing nil)
1027
1028 (defvar gnus-original-article nil)
1029 (defvar gnus-article-internal-prepare-hook nil)
1030 (defvar gnus-newsgroup-process-stack nil)
1031
1032 (defvar gnus-thread-indent-array nil)
1033 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1034 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1035   "Function called to sort the articles within a thread after it has been gathered together.")
1036
1037 (defvar gnus-summary-save-parts-type-history nil)
1038 (defvar gnus-summary-save-parts-last-directory nil)
1039
1040 (defvar gnus-summary-save-parts-type-history nil)
1041 (defvar gnus-summary-save-parts-last-directory nil)
1042
1043 ;; Avoid highlighting in kill files.
1044 (defvar gnus-summary-inhibit-highlight nil)
1045 (defvar gnus-newsgroup-selected-overlay nil)
1046 (defvar gnus-inhibit-limiting nil)
1047 (defvar gnus-newsgroup-adaptive-score-file nil)
1048 (defvar gnus-current-score-file nil)
1049 (defvar gnus-current-move-group nil)
1050 (defvar gnus-current-copy-group nil)
1051 (defvar gnus-current-crosspost-group nil)
1052
1053 (defvar gnus-newsgroup-dependencies nil)
1054 (defvar gnus-newsgroup-adaptive nil)
1055 (defvar gnus-summary-display-article-function nil)
1056 (defvar gnus-summary-highlight-line-function nil
1057   "Function called after highlighting a summary line.")
1058
1059 (defvar gnus-summary-line-format-alist
1060   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1061     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1062     (?s gnus-tmp-subject-or-nil ?s)
1063     (?n gnus-tmp-name ?s)
1064     (?A (std11-address-string
1065          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1066     (?a (or (std11-full-name-string
1067              (car (mime-entity-read-field gnus-tmp-header 'From)))
1068             gnus-tmp-from) ?s)
1069     (?F gnus-tmp-from ?s)
1070     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1071     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1072     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1073     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1074     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1075     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1076     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1077     (?L gnus-tmp-lines ?s)
1078     (?I gnus-tmp-indentation ?s)
1079     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1080     (?R gnus-tmp-replied ?c)
1081     (?\[ gnus-tmp-opening-bracket ?c)
1082     (?\] gnus-tmp-closing-bracket ?c)
1083     (?\> (make-string gnus-tmp-level ? ) ?s)
1084     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1085     (?i gnus-tmp-score ?d)
1086     (?z gnus-tmp-score-char ?c)
1087     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1088     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1089     (?U gnus-tmp-unread ?c)
1090     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1091     (?t (gnus-summary-number-of-articles-in-thread
1092          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1093         ?d)
1094     (?e (gnus-summary-number-of-articles-in-thread
1095          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1096         ?c)
1097     (?u gnus-tmp-user-defined ?s)
1098     (?P (gnus-pick-line-number) ?d))
1099   "An alist of format specifications that can appear in summary lines.
1100 These are paired with what variables they correspond with, along with
1101 the type of the variable (string, integer, character, etc).")
1102
1103 (defvar gnus-summary-dummy-line-format-alist
1104   `((?S gnus-tmp-subject ?s)
1105     (?N gnus-tmp-number ?d)
1106     (?u gnus-tmp-user-defined ?s)))
1107
1108 (defvar gnus-summary-mode-line-format-alist
1109   `((?G gnus-tmp-group-name ?s)
1110     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1111     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1112     (?A gnus-tmp-article-number ?d)
1113     (?Z gnus-tmp-unread-and-unselected ?s)
1114     (?V gnus-version ?s)
1115     (?U gnus-tmp-unread-and-unticked ?d)
1116     (?S gnus-tmp-subject ?s)
1117     (?e gnus-tmp-unselected ?d)
1118     (?u gnus-tmp-user-defined ?s)
1119     (?d (length gnus-newsgroup-dormant) ?d)
1120     (?t (length gnus-newsgroup-marked) ?d)
1121     (?r (length gnus-newsgroup-reads) ?d)
1122     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1123     (?E gnus-newsgroup-expunged-tally ?d)
1124     (?s (gnus-current-score-file-nondirectory) ?s)))
1125
1126 (defvar gnus-last-search-regexp nil
1127   "Default regexp for article search command.")
1128
1129 (defvar gnus-summary-search-article-matched-data nil
1130   "Last matched data of article search command.  It is the local variable
1131 in `gnus-article-buffer' which consists of the list of start position,
1132 end position and text.")
1133
1134 (defvar gnus-last-shell-command nil
1135   "Default shell command on article.")
1136
1137 (defvar gnus-newsgroup-begin nil)
1138 (defvar gnus-newsgroup-end nil)
1139 (defvar gnus-newsgroup-last-rmail nil)
1140 (defvar gnus-newsgroup-last-mail nil)
1141 (defvar gnus-newsgroup-last-folder nil)
1142 (defvar gnus-newsgroup-last-file nil)
1143 (defvar gnus-newsgroup-auto-expire nil)
1144 (defvar gnus-newsgroup-active nil)
1145
1146 (defvar gnus-newsgroup-data nil)
1147 (defvar gnus-newsgroup-data-reverse nil)
1148 (defvar gnus-newsgroup-limit nil)
1149 (defvar gnus-newsgroup-limits nil)
1150
1151 (defvar gnus-newsgroup-unreads nil
1152   "List of unread articles in the current newsgroup.")
1153
1154 (defvar gnus-newsgroup-unselected nil
1155   "List of unselected unread articles in the current newsgroup.")
1156
1157 (defvar gnus-newsgroup-reads nil
1158   "Alist of read articles and article marks in the current newsgroup.")
1159
1160 (defvar gnus-newsgroup-expunged-tally nil)
1161
1162 (defvar gnus-newsgroup-marked nil
1163   "List of ticked articles in the current newsgroup (a subset of unread art).")
1164
1165 (defvar gnus-newsgroup-killed nil
1166   "List of ranges of articles that have been through the scoring process.")
1167
1168 (defvar gnus-newsgroup-cached nil
1169   "List of articles that come from the article cache.")
1170
1171 (defvar gnus-newsgroup-saved nil
1172   "List of articles that have been saved.")
1173
1174 (defvar gnus-newsgroup-kill-headers nil)
1175
1176 (defvar gnus-newsgroup-replied nil
1177   "List of articles that have been replied to in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-forwarded nil
1180   "List of articles that have been forwarded in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-expirable nil
1183   "List of articles in the current newsgroup that can be expired.")
1184
1185 (defvar gnus-newsgroup-processable nil
1186   "List of articles in the current newsgroup that can be processed.")
1187
1188 (defvar gnus-newsgroup-downloadable nil
1189   "List of articles in the current newsgroup that can be processed.")
1190
1191 (defvar gnus-newsgroup-undownloaded nil
1192   "List of articles in the current newsgroup that haven't been downloaded..")
1193
1194 (defvar gnus-newsgroup-unsendable nil
1195   "List of articles in the current newsgroup that won't be sent.")
1196
1197 (defvar gnus-newsgroup-bookmarks nil
1198   "List of articles in the current newsgroup that have bookmarks.")
1199
1200 (defvar gnus-newsgroup-dormant nil
1201   "List of dormant articles in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-scored nil
1204   "List of scored articles in the current newsgroup.")
1205
1206 (defvar gnus-newsgroup-incorporated nil
1207   "List of incorporated articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-headers nil
1210   "List of article headers in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-threads nil)
1213
1214 (defvar gnus-newsgroup-prepared nil
1215   "Whether the current group has been prepared properly.")
1216
1217 (defvar gnus-newsgroup-ancient nil
1218   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-sparse nil)
1221
1222 (defvar gnus-current-article nil)
1223 (defvar gnus-article-current nil)
1224 (defvar gnus-current-headers nil)
1225 (defvar gnus-have-all-headers nil)
1226 (defvar gnus-last-article nil)
1227 (defvar gnus-newsgroup-history nil)
1228 (defvar gnus-newsgroup-charset nil)
1229 (defvar gnus-newsgroup-ephemeral-charset nil)
1230 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1231
1232 (defvar gnus-article-before-search nil)
1233
1234 (defconst gnus-summary-local-variables
1235   '(gnus-newsgroup-name
1236     gnus-newsgroup-begin gnus-newsgroup-end
1237     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1238     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1239     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1240     gnus-newsgroup-unselected gnus-newsgroup-marked
1241     gnus-newsgroup-reads gnus-newsgroup-saved
1242     gnus-newsgroup-replied gnus-newsgroup-forwarded
1243     gnus-newsgroup-expirable
1244     gnus-newsgroup-processable gnus-newsgroup-killed
1245     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1246     gnus-newsgroup-unsendable
1247     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1248     gnus-newsgroup-headers gnus-newsgroup-threads
1249     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1250     gnus-current-article gnus-current-headers gnus-have-all-headers
1251     gnus-last-article gnus-article-internal-prepare-hook
1252     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1253     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1254     gnus-thread-expunge-below
1255     gnus-score-alist gnus-current-score-file
1256     (gnus-summary-expunge-below . global)
1257     (gnus-summary-mark-below . global)
1258     (gnus-orphan-score . global)
1259     gnus-newsgroup-active gnus-scores-exclude-files
1260     gnus-newsgroup-history gnus-newsgroup-ancient
1261     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1262     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1263     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1264     (gnus-newsgroup-expunged-tally . 0)
1265     gnus-cache-removable-articles gnus-newsgroup-cached
1266     gnus-newsgroup-data gnus-newsgroup-data-reverse
1267     gnus-newsgroup-limit gnus-newsgroup-limits
1268     gnus-newsgroup-charset
1269     gnus-newsgroup-incorporated)
1270   "Variables that are buffer-local to the summary buffers.")
1271
1272 (defvar gnus-newsgroup-variables nil
1273   "Variables that have separate values in the newsgroups.")
1274
1275 ;; Byte-compiler warning.
1276 (eval-when-compile (defvar gnus-article-mode-map))
1277
1278 ;; Subject simplification.
1279
1280 (defun gnus-simplify-whitespace (str)
1281   "Remove excessive whitespace from STR."
1282   (let ((mystr str))
1283     ;; Multiple spaces.
1284     (while (string-match "[ \t][ \t]+" mystr)
1285       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1286                           " "
1287                           (substring mystr (match-end 0)))))
1288     ;; Leading spaces.
1289     (when (string-match "^[ \t]+" mystr)
1290       (setq mystr (substring mystr (match-end 0))))
1291     ;; Trailing spaces.
1292     (when (string-match "[ \t]+$" mystr)
1293       (setq mystr (substring mystr 0 (match-beginning 0))))
1294     mystr))
1295
1296 (defsubst gnus-simplify-subject-re (subject)
1297   "Remove \"Re:\" from subject lines."
1298   (if (string-match message-subject-re-regexp subject)
1299       (substring subject (match-end 0))
1300     subject))
1301
1302 (defun gnus-simplify-subject (subject &optional re-only)
1303   "Remove `Re:' and words in parentheses.
1304 If RE-ONLY is non-nil, strip leading `Re:'s only."
1305   (let ((case-fold-search t))           ;Ignore case.
1306     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1307     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1308       (setq subject (substring subject (match-end 0))))
1309     ;; Remove uninteresting prefixes.
1310     (when (and (not re-only)
1311                gnus-simplify-ignored-prefixes
1312                (string-match gnus-simplify-ignored-prefixes subject))
1313       (setq subject (substring subject (match-end 0))))
1314     ;; Remove words in parentheses from end.
1315     (unless re-only
1316       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1317         (setq subject (substring subject 0 (match-beginning 0)))))
1318     ;; Return subject string.
1319     subject))
1320
1321 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1322 ;; all whitespace.
1323 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1324   (goto-char (point-min))
1325   (while (re-search-forward regexp nil t)
1326     (replace-match (or newtext ""))))
1327
1328 (defun gnus-simplify-buffer-fuzzy ()
1329   "Simplify string in the buffer fuzzily.
1330 The string in the accessible portion of the current buffer is simplified.
1331 It is assumed to be a single-line subject.
1332 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1333 matter is removed.  Additional things can be deleted by setting
1334 `gnus-simplify-subject-fuzzy-regexp'."
1335   (let ((case-fold-search t)
1336         (modified-tick))
1337     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1338
1339     (while (not (eq modified-tick (buffer-modified-tick)))
1340       (setq modified-tick (buffer-modified-tick))
1341       (cond
1342        ((listp gnus-simplify-subject-fuzzy-regexp)
1343         (mapcar 'gnus-simplify-buffer-fuzzy-step
1344                 gnus-simplify-subject-fuzzy-regexp))
1345        (gnus-simplify-subject-fuzzy-regexp
1346         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1347       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1348       (gnus-simplify-buffer-fuzzy-step
1349        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1350       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1351
1352     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1353     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1354     (gnus-simplify-buffer-fuzzy-step " $")
1355     (gnus-simplify-buffer-fuzzy-step "^ +")))
1356
1357 (defun gnus-simplify-subject-fuzzy (subject)
1358   "Simplify a subject string fuzzily.
1359 See `gnus-simplify-buffer-fuzzy' for details."
1360   (save-excursion
1361     (gnus-set-work-buffer)
1362     (let ((case-fold-search t))
1363       ;; Remove uninteresting prefixes.
1364       (when (and gnus-simplify-ignored-prefixes
1365                  (string-match gnus-simplify-ignored-prefixes subject))
1366         (setq subject (substring subject (match-end 0))))
1367       (insert subject)
1368       (inline (gnus-simplify-buffer-fuzzy))
1369       (buffer-string))))
1370
1371 (defsubst gnus-simplify-subject-fully (subject)
1372   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1373   (cond
1374    (gnus-simplify-subject-functions
1375     (gnus-map-function gnus-simplify-subject-functions subject))
1376    ((null gnus-summary-gather-subject-limit)
1377     (gnus-simplify-subject-re subject))
1378    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1379     (gnus-simplify-subject-fuzzy subject))
1380    ((numberp gnus-summary-gather-subject-limit)
1381     (gnus-limit-string (gnus-simplify-subject-re subject)
1382                        gnus-summary-gather-subject-limit))
1383    (t
1384     subject)))
1385
1386 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1387   "Check whether two subjects are equal.
1388 If optional argument simple-first is t, first argument is already
1389 simplified."
1390   (cond
1391    ((null simple-first)
1392     (equal (gnus-simplify-subject-fully s1)
1393            (gnus-simplify-subject-fully s2)))
1394    (t
1395     (equal s1
1396            (gnus-simplify-subject-fully s2)))))
1397
1398 (defun gnus-summary-bubble-group ()
1399   "Increase the score of the current group.
1400 This is a handy function to add to `gnus-summary-exit-hook' to
1401 increase the score of each group you read."
1402   (gnus-group-add-score gnus-newsgroup-name))
1403
1404 \f
1405 ;;;
1406 ;;; Gnus summary mode
1407 ;;;
1408
1409 (put 'gnus-summary-mode 'mode-class 'special)
1410
1411 (defvar gnus-article-commands-menu)
1412
1413 (when t
1414   ;; Non-orthogonal keys
1415
1416   (gnus-define-keys gnus-summary-mode-map
1417     " " gnus-summary-next-page
1418     "\177" gnus-summary-prev-page
1419     [delete] gnus-summary-prev-page
1420     [backspace] gnus-summary-prev-page
1421     "\r" gnus-summary-scroll-up
1422     "\M-\r" gnus-summary-scroll-down
1423     "n" gnus-summary-next-unread-article
1424     "p" gnus-summary-prev-unread-article
1425     "N" gnus-summary-next-article
1426     "P" gnus-summary-prev-article
1427     "\M-\C-n" gnus-summary-next-same-subject
1428     "\M-\C-p" gnus-summary-prev-same-subject
1429     "\M-n" gnus-summary-next-unread-subject
1430     "\M-p" gnus-summary-prev-unread-subject
1431     "." gnus-summary-first-unread-article
1432     "," gnus-summary-best-unread-article
1433     "\M-s" gnus-summary-search-article-forward
1434     "\M-r" gnus-summary-search-article-backward
1435     "<" gnus-summary-beginning-of-article
1436     ">" gnus-summary-end-of-article
1437     "j" gnus-summary-goto-article
1438     "^" gnus-summary-refer-parent-article
1439     "\M-^" gnus-summary-refer-article
1440     "u" gnus-summary-tick-article-forward
1441     "!" gnus-summary-tick-article-forward
1442     "U" gnus-summary-tick-article-backward
1443     "d" gnus-summary-mark-as-read-forward
1444     "D" gnus-summary-mark-as-read-backward
1445     "E" gnus-summary-mark-as-expirable
1446     "\M-u" gnus-summary-clear-mark-forward
1447     "\M-U" gnus-summary-clear-mark-backward
1448     "k" gnus-summary-kill-same-subject-and-select
1449     "\C-k" gnus-summary-kill-same-subject
1450     "\M-\C-k" gnus-summary-kill-thread
1451     "\M-\C-l" gnus-summary-lower-thread
1452     "e" gnus-summary-edit-article
1453     "#" gnus-summary-mark-as-processable
1454     "\M-#" gnus-summary-unmark-as-processable
1455     "\M-\C-t" gnus-summary-toggle-threads
1456     "\M-\C-s" gnus-summary-show-thread
1457     "\M-\C-h" gnus-summary-hide-thread
1458     "\M-\C-f" gnus-summary-next-thread
1459     "\M-\C-b" gnus-summary-prev-thread
1460     [(meta down)] gnus-summary-next-thread
1461     [(meta up)] gnus-summary-prev-thread
1462     "\M-\C-u" gnus-summary-up-thread
1463     "\M-\C-d" gnus-summary-down-thread
1464     "&" gnus-summary-execute-command
1465     "c" gnus-summary-catchup-and-exit
1466     "\C-w" gnus-summary-mark-region-as-read
1467     "\C-t" gnus-summary-toggle-truncation
1468     "?" gnus-summary-mark-as-dormant
1469     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1470     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1471     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1472     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1473     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1474     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1475     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1476     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1477     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1478     "=" gnus-summary-expand-window
1479     "\C-x\C-s" gnus-summary-reselect-current-group
1480     "\M-g" gnus-summary-rescan-group
1481     "w" gnus-summary-stop-page-breaking
1482     "\C-c\C-r" gnus-summary-caesar-message
1483     "\M-t" gnus-summary-toggle-mime
1484     "f" gnus-summary-followup
1485     "F" gnus-summary-followup-with-original
1486     "C" gnus-summary-cancel-article
1487     "r" gnus-summary-reply
1488     "R" gnus-summary-reply-with-original
1489     "\C-c\C-f" gnus-summary-mail-forward
1490     "o" gnus-summary-save-article
1491     "\C-o" gnus-summary-save-article-mail
1492     "|" gnus-summary-pipe-output
1493     "\M-k" gnus-summary-edit-local-kill
1494     "\M-K" gnus-summary-edit-global-kill
1495     ;; "V" gnus-version
1496     "\C-c\C-d" gnus-summary-describe-group
1497     "q" gnus-summary-exit
1498     "Q" gnus-summary-exit-no-update
1499     "\C-c\C-i" gnus-info-find-node
1500     gnus-mouse-2 gnus-mouse-pick-article
1501     "m" gnus-summary-mail-other-window
1502     "a" gnus-summary-post-news
1503     "x" gnus-summary-limit-to-unread
1504     "s" gnus-summary-isearch-article
1505     "t" gnus-article-toggle-headers
1506     "g" gnus-summary-show-article
1507     "l" gnus-summary-goto-last-article
1508     "v" gnus-summary-preview-mime-message
1509     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1510     "\C-d" gnus-summary-enter-digest-group
1511     "\M-\C-d" gnus-summary-read-document
1512     "\M-\C-e" gnus-summary-edit-parameters
1513     "\M-\C-a" gnus-summary-customize-parameters
1514     "\C-c\C-b" gnus-bug
1515     "\C-c\C-n" gnus-namazu-search
1516     "*" gnus-cache-enter-article
1517     "\M-*" gnus-cache-remove-article
1518     "\M-&" gnus-summary-universal-argument
1519     "\C-l" gnus-recenter
1520     "I" gnus-summary-increase-score
1521     "L" gnus-summary-lower-score
1522     "\M-i" gnus-symbolic-argument
1523     "h" gnus-summary-select-article-buffer
1524
1525     "V" gnus-summary-score-map
1526     "X" gnus-uu-extract-map
1527     "S" gnus-summary-send-map)
1528
1529   ;; Sort of orthogonal keymap
1530   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1531     "t" gnus-summary-tick-article-forward
1532     "!" gnus-summary-tick-article-forward
1533     "d" gnus-summary-mark-as-read-forward
1534     "r" gnus-summary-mark-as-read-forward
1535     "c" gnus-summary-clear-mark-forward
1536     " " gnus-summary-clear-mark-forward
1537     "e" gnus-summary-mark-as-expirable
1538     "x" gnus-summary-mark-as-expirable
1539     "?" gnus-summary-mark-as-dormant
1540     "b" gnus-summary-set-bookmark
1541     "B" gnus-summary-remove-bookmark
1542     "#" gnus-summary-mark-as-processable
1543     "\M-#" gnus-summary-unmark-as-processable
1544     "S" gnus-summary-limit-include-expunged
1545     "C" gnus-summary-catchup
1546     "H" gnus-summary-catchup-to-here
1547     "\C-c" gnus-summary-catchup-all
1548     "k" gnus-summary-kill-same-subject-and-select
1549     "K" gnus-summary-kill-same-subject
1550     "P" gnus-uu-mark-map)
1551
1552   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1553     "c" gnus-summary-clear-above
1554     "u" gnus-summary-tick-above
1555     "m" gnus-summary-mark-above
1556     "k" gnus-summary-kill-below)
1557
1558   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1559     "/" gnus-summary-limit-to-subject
1560     "n" gnus-summary-limit-to-articles
1561     "w" gnus-summary-pop-limit
1562     "s" gnus-summary-limit-to-subject
1563     "a" gnus-summary-limit-to-author
1564     "u" gnus-summary-limit-to-unread
1565     "m" gnus-summary-limit-to-marks
1566     "M" gnus-summary-limit-exclude-marks
1567     "v" gnus-summary-limit-to-score
1568     "*" gnus-summary-limit-include-cached
1569     "D" gnus-summary-limit-include-dormant
1570     "T" gnus-summary-limit-include-thread
1571     "d" gnus-summary-limit-exclude-dormant
1572     "t" gnus-summary-limit-to-age
1573     "x" gnus-summary-limit-to-extra
1574     "E" gnus-summary-limit-include-expunged
1575     "c" gnus-summary-limit-exclude-childless-dormant
1576     "C" gnus-summary-limit-mark-excluded-as-read
1577     "o" gnus-summary-insert-old-articles
1578     "N" gnus-summary-insert-new-articles)
1579
1580   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1581     "n" gnus-summary-next-unread-article
1582     "p" gnus-summary-prev-unread-article
1583     "N" gnus-summary-next-article
1584     "P" gnus-summary-prev-article
1585     "\C-n" gnus-summary-next-same-subject
1586     "\C-p" gnus-summary-prev-same-subject
1587     "\M-n" gnus-summary-next-unread-subject
1588     "\M-p" gnus-summary-prev-unread-subject
1589     "f" gnus-summary-first-unread-article
1590     "b" gnus-summary-best-unread-article
1591     "j" gnus-summary-goto-article
1592     "g" gnus-summary-goto-subject
1593     "l" gnus-summary-goto-last-article
1594     "o" gnus-summary-pop-article)
1595
1596   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1597     "k" gnus-summary-kill-thread
1598     "l" gnus-summary-lower-thread
1599     "i" gnus-summary-raise-thread
1600     "T" gnus-summary-toggle-threads
1601     "t" gnus-summary-rethread-current
1602     "^" gnus-summary-reparent-thread
1603     "s" gnus-summary-show-thread
1604     "S" gnus-summary-show-all-threads
1605     "h" gnus-summary-hide-thread
1606     "H" gnus-summary-hide-all-threads
1607     "n" gnus-summary-next-thread
1608     "p" gnus-summary-prev-thread
1609     "u" gnus-summary-up-thread
1610     "o" gnus-summary-top-thread
1611     "d" gnus-summary-down-thread
1612     "#" gnus-uu-mark-thread
1613     "\M-#" gnus-uu-unmark-thread)
1614
1615   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1616     "g" gnus-summary-prepare
1617     "c" gnus-summary-insert-cached-articles)
1618
1619   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1620     "c" gnus-summary-catchup-and-exit
1621     "C" gnus-summary-catchup-all-and-exit
1622     "E" gnus-summary-exit-no-update
1623     "J" gnus-summary-jump-to-other-group
1624     "Q" gnus-summary-exit
1625     "Z" gnus-summary-exit
1626     "n" gnus-summary-catchup-and-goto-next-group
1627     "R" gnus-summary-reselect-current-group
1628     "G" gnus-summary-rescan-group
1629     "N" gnus-summary-next-group
1630     "s" gnus-summary-save-newsrc
1631     "P" gnus-summary-prev-group)
1632
1633   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1634     " " gnus-summary-next-page
1635     "n" gnus-summary-next-page
1636     "\177" gnus-summary-prev-page
1637     [delete] gnus-summary-prev-page
1638     "p" gnus-summary-prev-page
1639     "\r" gnus-summary-scroll-up
1640     "\M-\r" gnus-summary-scroll-down
1641     "<" gnus-summary-beginning-of-article
1642     ">" gnus-summary-end-of-article
1643     "b" gnus-summary-beginning-of-article
1644     "e" gnus-summary-end-of-article
1645     "^" gnus-summary-refer-parent-article
1646     "r" gnus-summary-refer-parent-article
1647     "D" gnus-summary-enter-digest-group
1648     "R" gnus-summary-refer-references
1649     "T" gnus-summary-refer-thread
1650     "g" gnus-summary-show-article
1651     "s" gnus-summary-isearch-article
1652     "P" gnus-summary-print-article
1653     "M" gnus-mailing-list-insinuate
1654     "t" gnus-article-babel)
1655
1656   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1657     "b" gnus-article-add-buttons
1658     "B" gnus-article-add-buttons-to-head
1659     "o" gnus-article-treat-overstrike
1660     "e" gnus-article-emphasize
1661     "w" gnus-article-fill-cited-article
1662     "Q" gnus-article-fill-long-lines
1663     "C" gnus-article-capitalize-sentences
1664     "c" gnus-article-remove-cr
1665     "Z" gnus-article-decode-HZ
1666     "f" gnus-article-display-x-face
1667     "l" gnus-summary-stop-page-breaking
1668     "r" gnus-summary-caesar-message
1669     "t" gnus-article-toggle-headers
1670     "v" gnus-summary-verbose-headers
1671     "m" gnus-summary-toggle-mime
1672     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1673     "p" gnus-article-verify-x-pgp-sig
1674     "d" gnus-article-treat-dumbquotes
1675     "s" gnus-smiley-display)
1676
1677   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1678     "a" gnus-article-hide
1679     "h" gnus-article-toggle-headers
1680     "b" gnus-article-hide-boring-headers
1681     "s" gnus-article-hide-signature
1682     "c" gnus-article-hide-citation
1683     "C" gnus-article-hide-citation-in-followups
1684     "l" gnus-article-hide-list-identifiers
1685     "p" gnus-article-hide-pgp
1686     "B" gnus-article-strip-banner
1687     "P" gnus-article-hide-pem
1688     "\C-c" gnus-article-hide-citation-maybe)
1689
1690   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1691     "a" gnus-article-highlight
1692     "h" gnus-article-highlight-headers
1693     "c" gnus-article-highlight-citation
1694     "s" gnus-article-highlight-signature)
1695
1696   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1697     "z" gnus-article-date-ut
1698     "u" gnus-article-date-ut
1699     "l" gnus-article-date-local
1700     "p" gnus-article-date-english
1701     "e" gnus-article-date-lapsed
1702     "o" gnus-article-date-original
1703     "i" gnus-article-date-iso8601
1704     "s" gnus-article-date-user)
1705
1706   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1707     "t" gnus-article-remove-trailing-blank-lines
1708     "l" gnus-article-strip-leading-blank-lines
1709     "m" gnus-article-strip-multiple-blank-lines
1710     "a" gnus-article-strip-blank-lines
1711     "A" gnus-article-strip-all-blank-lines
1712     "s" gnus-article-strip-leading-space
1713     "e" gnus-article-strip-trailing-space
1714     "w" gnus-article-remove-leading-whitespace)
1715
1716   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1717     "v" gnus-version
1718     "f" gnus-summary-fetch-faq
1719     "d" gnus-summary-describe-group
1720     "h" gnus-summary-describe-briefly
1721     "i" gnus-info-find-node)
1722
1723   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1724     "e" gnus-summary-expire-articles
1725     "\M-\C-e" gnus-summary-expire-articles-now
1726     "\177" gnus-summary-delete-article
1727     [delete] gnus-summary-delete-article
1728     [backspace] gnus-summary-delete-article
1729     "m" gnus-summary-move-article
1730     "r" gnus-summary-respool-article
1731     "w" gnus-summary-edit-article
1732     "c" gnus-summary-copy-article
1733     "B" gnus-summary-crosspost-article
1734     "q" gnus-summary-respool-query
1735     "t" gnus-summary-respool-trace
1736     "i" gnus-summary-import-article
1737     "I" gnus-summary-create-article
1738     "p" gnus-summary-article-posted-p)
1739
1740   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1741     "o" gnus-summary-save-article
1742     "m" gnus-summary-save-article-mail
1743     "F" gnus-summary-write-article-file
1744     "r" gnus-summary-save-article-rmail
1745     "f" gnus-summary-save-article-file
1746     "b" gnus-summary-save-article-body-file
1747     "h" gnus-summary-save-article-folder
1748     "v" gnus-summary-save-article-vm
1749     "p" gnus-summary-pipe-output
1750     "s" gnus-soup-add-article)
1751
1752   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1753     "b" gnus-summary-display-buttonized
1754     "m" gnus-summary-repair-multipart
1755     "v" gnus-article-view-part
1756     "o" gnus-article-save-part
1757     "c" gnus-article-copy-part
1758     "C" gnus-article-view-part-as-charset
1759     "e" gnus-article-externalize-part
1760     "E" gnus-article-encrypt-body
1761     "i" gnus-article-inline-part
1762     "|" gnus-article-pipe-part))
1763
1764 (defvar gnus-article-post-menu nil)
1765
1766 (defun gnus-summary-make-menu-bar ()
1767   (gnus-turn-off-edit-menu 'summary)
1768
1769   (unless (boundp 'gnus-summary-misc-menu)
1770
1771     (easy-menu-define
1772      gnus-summary-kill-menu gnus-summary-mode-map ""
1773      (cons
1774       "Score"
1775       (nconc
1776        (list
1777         ["Customize" gnus-score-customize t])
1778        (gnus-make-score-map 'increase)
1779        (gnus-make-score-map 'lower)
1780        '(("Mark"
1781           ["Kill below" gnus-summary-kill-below t]
1782           ["Mark above" gnus-summary-mark-above t]
1783           ["Tick above" gnus-summary-tick-above t]
1784           ["Clear above" gnus-summary-clear-above t])
1785          ["Current score" gnus-summary-current-score t]
1786          ["Set score" gnus-summary-set-score t]
1787          ["Switch current score file..." gnus-score-change-score-file t]
1788          ["Set mark below..." gnus-score-set-mark-below t]
1789          ["Set expunge below..." gnus-score-set-expunge-below t]
1790          ["Edit current score file" gnus-score-edit-current-scores t]
1791          ["Edit score file" gnus-score-edit-file t]
1792          ["Trace score" gnus-score-find-trace t]
1793          ["Find words" gnus-score-find-favourite-words t]
1794          ["Rescore buffer" gnus-summary-rescore t]
1795          ["Increase score..." gnus-summary-increase-score t]
1796          ["Lower score..." gnus-summary-lower-score t]))))
1797
1798     ;; Define both the Article menu in the summary buffer and the equivalent
1799     ;; Commands menu in the article buffer here for consistency.
1800     (let ((innards
1801            `(("Hide"
1802               ["All" gnus-article-hide t]
1803               ["Headers" gnus-article-toggle-headers t]
1804               ["Signature" gnus-article-hide-signature t]
1805               ["Citation" gnus-article-hide-citation t]
1806               ["List identifiers" gnus-article-hide-list-identifiers t]
1807               ["PGP" gnus-article-hide-pgp t]
1808               ["Banner" gnus-article-strip-banner t]
1809               ["Boring headers" gnus-article-hide-boring-headers t])
1810              ("Highlight"
1811               ["All" gnus-article-highlight t]
1812               ["Headers" gnus-article-highlight-headers t]
1813               ["Signature" gnus-article-highlight-signature t]
1814               ["Citation" gnus-article-highlight-citation t])
1815              ("Date"
1816               ["Local" gnus-article-date-local t]
1817               ["ISO8601" gnus-article-date-iso8601 t]
1818               ["UT" gnus-article-date-ut t]
1819               ["Original" gnus-article-date-original t]
1820               ["Lapsed" gnus-article-date-lapsed t]
1821               ["User-defined" gnus-article-date-user t])
1822              ("Washing"
1823               ("Remove Blanks"
1824                ["Leading" gnus-article-strip-leading-blank-lines t]
1825                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1826                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1827                ["All of the above" gnus-article-strip-blank-lines t]
1828                ["All" gnus-article-strip-all-blank-lines t]
1829                ["Leading space" gnus-article-strip-leading-space t]
1830                ["Trailing space" gnus-article-strip-trailing-space t]
1831                ["Leading space in headers" 
1832                 gnus-article-remove-leading-whitespace t])
1833               ["Overstrike" gnus-article-treat-overstrike t]
1834               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1835               ["Emphasis" gnus-article-emphasize t]
1836               ["Word wrap" gnus-article-fill-cited-article t]
1837               ["Fill long lines" gnus-article-fill-long-lines t]
1838               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1839               ["CR" gnus-article-remove-cr t]
1840               ["Show X-Face" gnus-article-display-x-face t]
1841               ["Rot 13" gnus-summary-caesar-message
1842                ,@(if (featurep 'xemacs) '(t)
1843                    '(:help "\"Caesar rotate\" article by 13"))]
1844               ["Unix pipe" gnus-summary-pipe-message t]
1845               ["Add buttons" gnus-article-add-buttons t]
1846               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1847               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1848               ["Toggle MIME" gnus-summary-toggle-mime t]
1849               ["Verbose header" gnus-summary-verbose-headers t]
1850               ["Toggle header" gnus-summary-toggle-header t]
1851               ["Toggle smileys" gnus-smiley-display t]
1852               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1853               ["HZ" gnus-article-decode-HZ t])
1854              ("Output"
1855               ["Save in default format" gnus-summary-save-article
1856                ,@(if (featurep 'xemacs) '(t)
1857                    '(:help "Save article using default method"))]
1858               ["Save in file" gnus-summary-save-article-file
1859                ,@(if (featurep 'xemacs) '(t)
1860                    '(:help "Save article in file"))]
1861               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1862               ["Save in MH folder" gnus-summary-save-article-folder t]
1863               ["Save in VM folder" gnus-summary-save-article-vm t]
1864               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1865               ["Save body in file" gnus-summary-save-article-body-file t]
1866               ["Pipe through a filter" gnus-summary-pipe-output t]
1867               ["Add to SOUP packet" gnus-soup-add-article t]
1868               ["Print" gnus-summary-print-article t])
1869              ("Backend"
1870               ["Respool article..." gnus-summary-respool-article t]
1871               ["Move article..." gnus-summary-move-article
1872                (gnus-check-backend-function
1873                 'request-move-article gnus-newsgroup-name)]
1874               ["Copy article..." gnus-summary-copy-article t]
1875               ["Crosspost article..." gnus-summary-crosspost-article
1876                (gnus-check-backend-function
1877                 'request-replace-article gnus-newsgroup-name)]
1878               ["Import file..." gnus-summary-import-article t]
1879               ["Create article..." gnus-summary-create-article t]
1880               ["Check if posted" gnus-summary-article-posted-p t]
1881               ["Edit article" gnus-summary-edit-article
1882                (not (gnus-group-read-only-p))]
1883               ["Delete article" gnus-summary-delete-article
1884                (gnus-check-backend-function
1885                 'request-expire-articles gnus-newsgroup-name)]
1886               ["Query respool" gnus-summary-respool-query t]
1887               ["Trace respool" gnus-summary-respool-trace t]
1888               ["Delete expirable articles" gnus-summary-expire-articles-now
1889                (gnus-check-backend-function
1890                 'request-expire-articles gnus-newsgroup-name)])
1891              ("Extract"
1892               ["Uudecode" gnus-uu-decode-uu
1893                ,@(if (featurep 'xemacs) '(t)
1894                    '(:help "Decode uuencoded article(s)"))]
1895               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1896               ["Unshar" gnus-uu-decode-unshar t]
1897               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1898               ["Save" gnus-uu-decode-save t]
1899               ["Binhex" gnus-uu-decode-binhex t]
1900               ["Postscript" gnus-uu-decode-postscript t])
1901              ("Cache"
1902               ["Enter article" gnus-cache-enter-article t]
1903               ["Remove article" gnus-cache-remove-article t])
1904              ["Translate" gnus-article-babel t]
1905              ["Select article buffer" gnus-summary-select-article-buffer t]
1906              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1907              ["Isearch article..." gnus-summary-isearch-article t]
1908              ["Beginning of the article" gnus-summary-beginning-of-article t]
1909              ["End of the article" gnus-summary-end-of-article t]
1910              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1911              ["Fetch referenced articles" gnus-summary-refer-references t]
1912              ["Fetch current thread" gnus-summary-refer-thread t]
1913              ["Fetch article with id..." gnus-summary-refer-article t]
1914              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1915              ["Redisplay" gnus-summary-show-article t])))
1916       (easy-menu-define
1917        gnus-summary-article-menu gnus-summary-mode-map ""
1918        (cons "Article" innards))
1919
1920       (if (not (keymapp gnus-summary-article-menu))
1921           (easy-menu-define
1922             gnus-article-commands-menu gnus-article-mode-map ""
1923             (cons "Commands" innards))
1924         ;; in Emacs, don't share menu.
1925         (setq gnus-article-commands-menu 
1926               (copy-keymap gnus-summary-article-menu))
1927         (define-key gnus-article-mode-map [menu-bar commands]
1928           (cons "Commands" gnus-article-commands-menu))))
1929
1930     (easy-menu-define
1931      gnus-summary-thread-menu gnus-summary-mode-map ""
1932      '("Threads"
1933        ["Toggle threading" gnus-summary-toggle-threads t]
1934        ["Hide threads" gnus-summary-hide-all-threads t]
1935        ["Show threads" gnus-summary-show-all-threads t]
1936        ["Hide thread" gnus-summary-hide-thread t]
1937        ["Show thread" gnus-summary-show-thread t]
1938        ["Go to next thread" gnus-summary-next-thread t]
1939        ["Go to previous thread" gnus-summary-prev-thread t]
1940        ["Go down thread" gnus-summary-down-thread t]
1941        ["Go up thread" gnus-summary-up-thread t]
1942        ["Top of thread" gnus-summary-top-thread t]
1943        ["Mark thread as read" gnus-summary-kill-thread t]
1944        ["Lower thread score" gnus-summary-lower-thread t]
1945        ["Raise thread score" gnus-summary-raise-thread t]
1946        ["Rethread current" gnus-summary-rethread-current t]))
1947
1948     (easy-menu-define
1949      gnus-summary-post-menu gnus-summary-mode-map ""
1950      `("Post"
1951        ["Post an article" gnus-summary-post-news
1952         ,@(if (featurep 'xemacs) '(t)
1953             '(:help "Post an article"))]
1954        ["Followup" gnus-summary-followup
1955         ,@(if (featurep 'xemacs) '(t)
1956             '(:help "Post followup to this article"))]
1957        ["Followup and yank" gnus-summary-followup-with-original
1958         ,@(if (featurep 'xemacs) '(t)
1959             '(:help "Post followup to this article, quoting its contents"))]
1960        ["Supersede article" gnus-summary-supersede-article t]
1961        ["Cancel article" gnus-summary-cancel-article
1962         ,@(if (featurep 'xemacs) '(t)
1963             '(:help "Cancel an article you posted"))]
1964        ["Reply" gnus-summary-reply t]
1965        ["Reply and yank" gnus-summary-reply-with-original t]
1966        ["Wide reply" gnus-summary-wide-reply t]
1967        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1968         ,@(if (featurep 'xemacs) '(t)
1969             '(:help "Mail a reply, quoting this article"))]
1970        ["Mail forward" gnus-summary-mail-forward t]
1971        ["Post forward" gnus-summary-post-forward t]
1972        ["Digest and mail" gnus-summary-digest-mail-forward t]
1973        ["Digest and post" gnus-summary-digest-post-forward t]
1974        ["Resend message" gnus-summary-resend-message t]
1975        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1976        ["Send a mail" gnus-summary-mail-other-window t]
1977        ["Uuencode and post" gnus-uu-post-news
1978         ,@(if (featurep 'xemacs) '(t)
1979             '(:help "Post a uuencoded article"))]
1980        ["Followup via news" gnus-summary-followup-to-mail t]
1981        ["Followup via news and yank"
1982         gnus-summary-followup-to-mail-with-original t]
1983        ;;("Draft"
1984        ;;["Send" gnus-summary-send-draft t]
1985        ;;["Send bounced" gnus-resend-bounced-mail t])
1986        ))
1987
1988     (cond 
1989      ((not (keymapp gnus-summary-post-menu))
1990       (setq gnus-article-post-menu gnus-summary-post-menu))
1991      ((not gnus-article-post-menu)
1992       ;; Don't share post menu.
1993       (setq gnus-article-post-menu
1994             (copy-keymap gnus-summary-post-menu))))
1995     (define-key gnus-article-mode-map [menu-bar post]
1996       (cons "Post" gnus-article-post-menu))
1997
1998     (easy-menu-define
1999      gnus-summary-misc-menu gnus-summary-mode-map ""
2000      `("Misc"
2001        ("Mark Read"
2002         ["Mark as read" gnus-summary-mark-as-read-forward t]
2003         ["Mark same subject and select"
2004          gnus-summary-kill-same-subject-and-select t]
2005         ["Mark same subject" gnus-summary-kill-same-subject t]
2006         ["Catchup" gnus-summary-catchup
2007          ,@(if (featurep 'xemacs) '(t)
2008              '(:help "Mark unread articles in this group as read"))]
2009         ["Catchup all" gnus-summary-catchup-all t]
2010         ["Catchup to here" gnus-summary-catchup-to-here t]
2011         ["Catchup region" gnus-summary-mark-region-as-read t]
2012         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2013        ("Mark Various"
2014         ["Tick" gnus-summary-tick-article-forward t]
2015         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2016         ["Remove marks" gnus-summary-clear-mark-forward t]
2017         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2018         ["Set bookmark" gnus-summary-set-bookmark t]
2019         ["Remove bookmark" gnus-summary-remove-bookmark t])
2020        ("Mark Limit"
2021         ["Marks..." gnus-summary-limit-to-marks t]
2022         ["Subject..." gnus-summary-limit-to-subject t]
2023         ["Author..." gnus-summary-limit-to-author t]
2024         ["Age..." gnus-summary-limit-to-age t]
2025         ["Extra..." gnus-summary-limit-to-extra t]
2026         ["Score" gnus-summary-limit-to-score t]
2027         ["Unread" gnus-summary-limit-to-unread t]
2028         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2029         ["Articles" gnus-summary-limit-to-articles t]
2030         ["Pop limit" gnus-summary-pop-limit t]
2031         ["Show dormant" gnus-summary-limit-include-dormant t]
2032         ["Hide childless dormant"
2033          gnus-summary-limit-exclude-childless-dormant t]
2034         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2035         ["Hide marked" gnus-summary-limit-exclude-marks t]
2036         ["Show expunged" gnus-summary-limit-include-expunged t])
2037        ("Process Mark"
2038         ["Set mark" gnus-summary-mark-as-processable t]
2039         ["Remove mark" gnus-summary-unmark-as-processable t]
2040         ["Remove all marks" gnus-summary-unmark-all-processable t]
2041         ["Mark above" gnus-uu-mark-over t]
2042         ["Mark series" gnus-uu-mark-series t]
2043         ["Mark region" gnus-uu-mark-region t]
2044         ["Unmark region" gnus-uu-unmark-region t]
2045         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2046         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2047         ["Mark all" gnus-uu-mark-all t]
2048         ["Mark buffer" gnus-uu-mark-buffer t]
2049         ["Mark sparse" gnus-uu-mark-sparse t]
2050         ["Mark thread" gnus-uu-mark-thread t]
2051         ["Unmark thread" gnus-uu-unmark-thread t]
2052         ("Process Mark Sets"
2053          ["Kill" gnus-summary-kill-process-mark t]
2054          ["Yank" gnus-summary-yank-process-mark
2055           gnus-newsgroup-process-stack]
2056          ["Save" gnus-summary-save-process-mark t]))
2057        ("Scroll article"
2058         ["Page forward" gnus-summary-next-page
2059          ,@(if (featurep 'xemacs) '(t)
2060              '(:help "Show next page of article"))]
2061         ["Page backward" gnus-summary-prev-page
2062          ,@(if (featurep 'xemacs) '(t)
2063              '(:help "Show previous page of article"))]
2064         ["Line forward" gnus-summary-scroll-up t])
2065        ("Move"
2066         ["Next unread article" gnus-summary-next-unread-article t]
2067         ["Previous unread article" gnus-summary-prev-unread-article t]
2068         ["Next article" gnus-summary-next-article t]
2069         ["Previous article" gnus-summary-prev-article t]
2070         ["Next unread subject" gnus-summary-next-unread-subject t]
2071         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2072         ["Next article same subject" gnus-summary-next-same-subject t]
2073         ["Previous article same subject" gnus-summary-prev-same-subject t]
2074         ["First unread article" gnus-summary-first-unread-article t]
2075         ["Best unread article" gnus-summary-best-unread-article t]
2076         ["Go to subject number..." gnus-summary-goto-subject t]
2077         ["Go to article number..." gnus-summary-goto-article t]
2078         ["Go to the last article" gnus-summary-goto-last-article t]
2079         ["Pop article off history" gnus-summary-pop-article t])
2080        ("Sort"
2081         ["Sort by number" gnus-summary-sort-by-number t]
2082         ["Sort by author" gnus-summary-sort-by-author t]
2083         ["Sort by subject" gnus-summary-sort-by-subject t]
2084         ["Sort by date" gnus-summary-sort-by-date t]
2085         ["Sort by score" gnus-summary-sort-by-score t]
2086         ["Sort by lines" gnus-summary-sort-by-lines t]
2087         ["Sort by characters" gnus-summary-sort-by-chars t]
2088         ["Original sort" gnus-summary-sort-by-original t])
2089        ("Help"
2090         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2091         ["Describe group" gnus-summary-describe-group t]
2092         ["Read manual" gnus-info-find-node t])
2093        ("Modes"
2094         ["Pick and read" gnus-pick-mode t]
2095         ["Binary" gnus-binary-mode t])
2096        ("Regeneration"
2097         ["Regenerate" gnus-summary-prepare t]
2098         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2099         ["Toggle threading" gnus-summary-toggle-threads t])
2100        ["See old articles" gnus-summary-insert-old-articles t]
2101        ["See new articles" gnus-summary-insert-new-articles t]
2102        ["Filter articles..." gnus-summary-execute-command t]
2103        ["Run command on subjects..." gnus-summary-universal-argument t]
2104        ["Search articles forward..." gnus-summary-search-article-forward t]
2105        ["Search articles backward..." gnus-summary-search-article-backward t]
2106        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2107        ["Expand window" gnus-summary-expand-window t]
2108        ["Expire expirable articles" gnus-summary-expire-articles
2109         (gnus-check-backend-function
2110          'request-expire-articles gnus-newsgroup-name)]
2111        ["Edit local kill file" gnus-summary-edit-local-kill t]
2112        ["Edit main kill file" gnus-summary-edit-global-kill t]
2113        ["Edit group parameters" gnus-summary-edit-parameters t]
2114        ["Customize group parameters" gnus-summary-customize-parameters t]
2115        ["Send a bug report" gnus-bug t]
2116        ("Exit"
2117         ["Catchup and exit" gnus-summary-catchup-and-exit
2118          ,@(if (featurep 'xemacs) '(t)
2119              '(:help "Mark unread articles in this group as read, then exit"))]
2120         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2121         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2122         ["Exit group" gnus-summary-exit
2123          ,@(if (featurep 'xemacs) '(t)
2124              '(:help "Exit current group, return to group selection mode"))]
2125         ["Exit group without updating" gnus-summary-exit-no-update t]
2126         ["Exit and goto next group" gnus-summary-next-group t]
2127         ["Exit and goto prev group" gnus-summary-prev-group t]
2128         ["Reselect group" gnus-summary-reselect-current-group t]
2129         ["Rescan group" gnus-summary-rescan-group t]
2130         ["Update dribble" gnus-summary-save-newsrc t])))
2131
2132     (gnus-run-hooks 'gnus-summary-menu-hook)))
2133
2134 (defvar gnus-summary-tool-bar-map nil)
2135
2136 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2137 (defun gnus-summary-make-tool-bar ()
2138   (if (and (fboundp 'tool-bar-add-item-from-menu)
2139            (default-value 'tool-bar-mode)
2140            (not gnus-summary-tool-bar-map))
2141       (setq gnus-summary-tool-bar-map
2142             (let ((tool-bar-map (make-sparse-keymap))
2143                   (load-path (mm-image-load-path)))
2144               (tool-bar-add-item-from-menu
2145                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2146               (tool-bar-add-item-from-menu
2147                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2148               (tool-bar-add-item-from-menu
2149                'gnus-summary-post-news "post" gnus-summary-mode-map)
2150               (tool-bar-add-item-from-menu
2151                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2152               (tool-bar-add-item-from-menu
2153                'gnus-summary-followup "followup" gnus-summary-mode-map)
2154               (tool-bar-add-item-from-menu
2155                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2156               (tool-bar-add-item-from-menu
2157                'gnus-summary-reply "reply" gnus-summary-mode-map)
2158               (tool-bar-add-item-from-menu
2159                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2160               (tool-bar-add-item-from-menu
2161                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2162               (tool-bar-add-item-from-menu
2163                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2164               (tool-bar-add-item-from-menu
2165                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2166               (tool-bar-add-item-from-menu
2167                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2168               (tool-bar-add-item-from-menu
2169                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2170               (tool-bar-add-item-from-menu
2171                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2172               (tool-bar-add-item-from-menu
2173                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2174               tool-bar-map)))
2175   (if gnus-summary-tool-bar-map
2176       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2177
2178 (defun gnus-score-set-default (var value)
2179   "A version of set that updates the GNU Emacs menu-bar."
2180   (set var value)
2181   ;; It is the message that forces the active status to be updated.
2182   (message ""))
2183
2184 (defun gnus-make-score-map (type)
2185   "Make a summary score map of type TYPE."
2186   (if t
2187       nil
2188     (let ((headers '(("author" "from" string)
2189                      ("subject" "subject" string)
2190                      ("article body" "body" string)
2191                      ("article head" "head" string)
2192                      ("xref" "xref" string)
2193                      ("extra header" "extra" string)
2194                      ("lines" "lines" number)
2195                      ("followups to author" "followup" string)))
2196           (types '((number ("less than" <)
2197                            ("greater than" >)
2198                            ("equal" =))
2199                    (string ("substring" s)
2200                            ("exact string" e)
2201                            ("fuzzy string" f)
2202                            ("regexp" r))))
2203           (perms '(("temporary" (current-time-string))
2204                    ("permanent" nil)
2205                    ("immediate" now)))
2206           header)
2207       (list
2208        (apply
2209         'nconc
2210         (list
2211          (if (eq type 'lower)
2212              "Lower score"
2213            "Increase score"))
2214         (let (outh)
2215           (while headers
2216             (setq header (car headers))
2217             (setq outh
2218                   (cons
2219                    (apply
2220                     'nconc
2221                     (list (car header))
2222                     (let ((ts (cdr (assoc (nth 2 header) types)))
2223                           outt)
2224                       (while ts
2225                         (setq outt
2226                               (cons
2227                                (apply
2228                                 'nconc
2229                                 (list (caar ts))
2230                                 (let ((ps perms)
2231                                       outp)
2232                                   (while ps
2233                                     (setq outp
2234                                           (cons
2235                                            (vector
2236                                             (caar ps)
2237                                             (list
2238                                              'gnus-summary-score-entry
2239                                              (nth 1 header)
2240                                              (if (or (string= (nth 1 header)
2241                                                               "head")
2242                                                      (string= (nth 1 header)
2243                                                               "body"))
2244                                                  ""
2245                                                (list 'gnus-summary-header
2246                                                      (nth 1 header)))
2247                                              (list 'quote (nth 1 (car ts)))
2248                                              (list 'gnus-score-delta-default
2249                                                    nil)
2250                                              (nth 1 (car ps))
2251                                              t)
2252                                             t)
2253                                            outp))
2254                                     (setq ps (cdr ps)))
2255                                   (list (nreverse outp))))
2256                                outt))
2257                         (setq ts (cdr ts)))
2258                       (list (nreverse outt))))
2259                    outh))
2260             (setq headers (cdr headers)))
2261           (list (nreverse outh))))))))
2262
2263 \f
2264
2265 (defun gnus-summary-mode (&optional group)
2266   "Major mode for reading articles.
2267
2268 All normal editing commands are switched off.
2269 \\<gnus-summary-mode-map>
2270 Each line in this buffer represents one article.  To read an
2271 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2272 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2273 respectively.
2274
2275 You can also post articles and send mail from this buffer.  To
2276 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2277 of an article, type `\\[gnus-summary-reply]'.
2278
2279 There are approx. one gazillion commands you can execute in this
2280 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2281
2282 The following commands are available:
2283
2284 \\{gnus-summary-mode-map}"
2285   (interactive)
2286   (kill-all-local-variables)
2287   (when (gnus-visual-p 'summary-menu 'menu)
2288     (gnus-summary-make-menu-bar)
2289     (gnus-summary-make-tool-bar))
2290   (gnus-summary-make-local-variables)
2291   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2292     (gnus-summary-make-local-variables))
2293   (gnus-make-thread-indent-array)
2294   (gnus-simplify-mode-line)
2295   (setq major-mode 'gnus-summary-mode)
2296   (setq mode-name "Summary")
2297   (make-local-variable 'minor-mode-alist)
2298   (use-local-map gnus-summary-mode-map)
2299   (buffer-disable-undo)
2300   (setq buffer-read-only t)             ;Disable modification
2301   (setq truncate-lines t)
2302   (setq selective-display t)
2303   (setq selective-display-ellipses t)   ;Display `...'
2304   (gnus-summary-set-display-table)
2305   (gnus-set-default-directory)
2306   (setq gnus-newsgroup-name group)
2307   (unless (gnus-news-group-p group)
2308     (setq gnus-newsgroup-incorporated
2309           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2310   (make-local-variable 'gnus-summary-line-format)
2311   (make-local-variable 'gnus-summary-line-format-spec)
2312   (make-local-variable 'gnus-summary-dummy-line-format)
2313   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2314   (make-local-variable 'gnus-summary-mark-positions)
2315   (make-local-hook 'pre-command-hook)
2316   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2317   (gnus-run-hooks 'gnus-summary-mode-hook)
2318   (turn-on-gnus-mailing-list-mode)
2319   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2320   (gnus-update-summary-mark-positions))
2321
2322 (defun gnus-summary-make-local-variables ()
2323   "Make all the local summary buffer variables."
2324   (let (global)
2325     (dolist (local gnus-summary-local-variables)
2326       (if (consp local)
2327           (progn
2328             (if (eq (cdr local) 'global)
2329                 ;; Copy the global value of the variable.
2330                 (setq global (symbol-value (car local)))
2331               ;; Use the value from the list.
2332               (setq global (eval (cdr local))))
2333             (set (make-local-variable (car local)) global))
2334         ;; Simple nil-valued local variable.
2335         (set (make-local-variable local) nil)))))
2336
2337 (defun gnus-summary-clear-local-variables ()
2338   (let ((locals gnus-summary-local-variables))
2339     (while locals
2340       (if (consp (car locals))
2341           (and (vectorp (caar locals))
2342                (set (caar locals) nil))
2343         (and (vectorp (car locals))
2344              (set (car locals) nil)))
2345       (setq locals (cdr locals)))))
2346
2347 ;; Summary data functions.
2348
2349 (defmacro gnus-data-number (data)
2350   `(car ,data))
2351
2352 (defmacro gnus-data-set-number (data number)
2353   `(setcar ,data ,number))
2354
2355 (defmacro gnus-data-mark (data)
2356   `(nth 1 ,data))
2357
2358 (defmacro gnus-data-set-mark (data mark)
2359   `(setcar (nthcdr 1 ,data) ,mark))
2360
2361 (defmacro gnus-data-pos (data)
2362   `(nth 2 ,data))
2363
2364 (defmacro gnus-data-set-pos (data pos)
2365   `(setcar (nthcdr 2 ,data) ,pos))
2366
2367 (defmacro gnus-data-header (data)
2368   `(nth 3 ,data))
2369
2370 (defmacro gnus-data-set-header (data header)
2371   `(setcar (nthcdr 3 ,data) ,header))
2372
2373 (defmacro gnus-data-level (data)
2374   `(nth 4 ,data))
2375
2376 (defmacro gnus-data-unread-p (data)
2377   `(= (nth 1 ,data) gnus-unread-mark))
2378
2379 (defmacro gnus-data-read-p (data)
2380   `(/= (nth 1 ,data) gnus-unread-mark))
2381
2382 (defmacro gnus-data-pseudo-p (data)
2383   `(consp (nth 3 ,data)))
2384
2385 (defmacro gnus-data-find (number)
2386   `(assq ,number gnus-newsgroup-data))
2387
2388 (defmacro gnus-data-find-list (number &optional data)
2389   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2390      (memq (assq ,number bdata)
2391            bdata)))
2392
2393 (defmacro gnus-data-make (number mark pos header level)
2394   `(list ,number ,mark ,pos ,header ,level))
2395
2396 (defun gnus-data-enter (after-article number mark pos header level offset)
2397   (let ((data (gnus-data-find-list after-article)))
2398     (unless data
2399       (error "No such article: %d" after-article))
2400     (setcdr data (cons (gnus-data-make number mark pos header level)
2401                        (cdr data)))
2402     (setq gnus-newsgroup-data-reverse nil)
2403     (gnus-data-update-list (cddr data) offset)))
2404
2405 (defun gnus-data-enter-list (after-article list &optional offset)
2406   (when list
2407     (let ((data (and after-article (gnus-data-find-list after-article)))
2408           (ilist list))
2409       (if (not (or data
2410                    after-article))
2411           (let ((odata gnus-newsgroup-data))
2412             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2413             (when offset
2414               (gnus-data-update-list odata offset)))
2415         ;; Find the last element in the list to be spliced into the main
2416         ;; list.
2417         (while (cdr list)
2418           (setq list (cdr list)))
2419         (if (not data)
2420             (progn
2421               (setcdr list gnus-newsgroup-data)
2422               (setq gnus-newsgroup-data ilist)
2423               (when offset
2424                 (gnus-data-update-list (cdr list) offset)))
2425           (setcdr list (cdr data))
2426           (setcdr data ilist)
2427           (when offset
2428             (gnus-data-update-list (cdr list) offset))))
2429       (setq gnus-newsgroup-data-reverse nil))))
2430
2431 (defun gnus-data-remove (article &optional offset)
2432   (let ((data gnus-newsgroup-data))
2433     (if (= (gnus-data-number (car data)) article)
2434         (progn
2435           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2436                 gnus-newsgroup-data-reverse nil)
2437           (when offset
2438             (gnus-data-update-list gnus-newsgroup-data offset)))
2439       (while (cdr data)
2440         (when (= (gnus-data-number (cadr data)) article)
2441           (setcdr data (cddr data))
2442           (when offset
2443             (gnus-data-update-list (cdr data) offset))
2444           (setq data nil
2445                 gnus-newsgroup-data-reverse nil))
2446         (setq data (cdr data))))))
2447
2448 (defmacro gnus-data-list (backward)
2449   `(if ,backward
2450        (or gnus-newsgroup-data-reverse
2451            (setq gnus-newsgroup-data-reverse
2452                  (reverse gnus-newsgroup-data)))
2453      gnus-newsgroup-data))
2454
2455 (defun gnus-data-update-list (data offset)
2456   "Add OFFSET to the POS of all data entries in DATA."
2457   (setq gnus-newsgroup-data-reverse nil)
2458   (while data
2459     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2460     (setq data (cdr data))))
2461
2462 (defun gnus-summary-article-pseudo-p (article)
2463   "Say whether this article is a pseudo article or not."
2464   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2465
2466 (defmacro gnus-summary-article-sparse-p (article)
2467   "Say whether this article is a sparse article or not."
2468   `(memq ,article gnus-newsgroup-sparse))
2469
2470 (defmacro gnus-summary-article-ancient-p (article)
2471   "Say whether this article is a sparse article or not."
2472   `(memq ,article gnus-newsgroup-ancient))
2473
2474 (defun gnus-article-parent-p (number)
2475   "Say whether this article is a parent or not."
2476   (let ((data (gnus-data-find-list number)))
2477     (and (cdr data)                     ; There has to be an article after...
2478          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2479             (gnus-data-level (nth 1 data))))))
2480
2481 (defun gnus-article-children (number)
2482   "Return a list of all children to NUMBER."
2483   (let* ((data (gnus-data-find-list number))
2484          (level (gnus-data-level (car data)))
2485          children)
2486     (setq data (cdr data))
2487     (while (and data
2488                 (= (gnus-data-level (car data)) (1+ level)))
2489       (push (gnus-data-number (car data)) children)
2490       (setq data (cdr data)))
2491     children))
2492
2493 (defmacro gnus-summary-skip-intangible ()
2494   "If the current article is intangible, then jump to a different article."
2495   '(let ((to (get-text-property (point) 'gnus-intangible)))
2496      (and to (gnus-summary-goto-subject to))))
2497
2498 (defmacro gnus-summary-article-intangible-p ()
2499   "Say whether this article is intangible or not."
2500   '(get-text-property (point) 'gnus-intangible))
2501
2502 (defun gnus-article-read-p (article)
2503   "Say whether ARTICLE is read or not."
2504   (not (or (memq article gnus-newsgroup-marked)
2505            (memq article gnus-newsgroup-unreads)
2506            (memq article gnus-newsgroup-unselected)
2507            (memq article gnus-newsgroup-dormant))))
2508
2509 ;; Some summary mode macros.
2510
2511 (defmacro gnus-summary-article-number ()
2512   "The article number of the article on the current line.
2513 If there isn's an article number here, then we return the current
2514 article number."
2515   '(progn
2516      (gnus-summary-skip-intangible)
2517      (or (get-text-property (point) 'gnus-number)
2518          (gnus-summary-last-subject))))
2519
2520 (defmacro gnus-summary-article-header (&optional number)
2521   "Return the header of article NUMBER."
2522   `(gnus-data-header (gnus-data-find
2523                       ,(or number '(gnus-summary-article-number)))))
2524
2525 (defmacro gnus-summary-thread-level (&optional number)
2526   "Return the level of thread that starts with article NUMBER."
2527   `(if (and (eq gnus-summary-make-false-root 'dummy)
2528             (get-text-property (point) 'gnus-intangible))
2529        0
2530      (gnus-data-level (gnus-data-find
2531                        ,(or number '(gnus-summary-article-number))))))
2532
2533 (defmacro gnus-summary-article-mark (&optional number)
2534   "Return the mark of article NUMBER."
2535   `(gnus-data-mark (gnus-data-find
2536                     ,(or number '(gnus-summary-article-number)))))
2537
2538 (defmacro gnus-summary-article-pos (&optional number)
2539   "Return the position of the line of article NUMBER."
2540   `(gnus-data-pos (gnus-data-find
2541                    ,(or number '(gnus-summary-article-number)))))
2542
2543 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2544 (defmacro gnus-summary-article-subject (&optional number)
2545   "Return current subject string or nil if nothing."
2546   `(let ((headers
2547           ,(if number
2548                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2549              '(gnus-data-header (assq (gnus-summary-article-number)
2550                                       gnus-newsgroup-data)))))
2551      (and headers
2552           (vectorp headers)
2553           (mail-header-subject headers))))
2554
2555 (defmacro gnus-summary-article-score (&optional number)
2556   "Return current article score."
2557   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2558                   gnus-newsgroup-scored))
2559        gnus-summary-default-score 0))
2560
2561 (defun gnus-summary-article-children (&optional number)
2562   "Return a list of article numbers that are children of article NUMBER."
2563   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2564          (level (gnus-data-level (car data)))
2565          l children)
2566     (while (and (setq data (cdr data))
2567                 (> (setq l (gnus-data-level (car data))) level))
2568       (and (= (1+ level) l)
2569            (push (gnus-data-number (car data))
2570                  children)))
2571     (nreverse children)))
2572
2573 (defun gnus-summary-article-parent (&optional number)
2574   "Return the article number of the parent of article NUMBER."
2575   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2576                                     (gnus-data-list t)))
2577          (level (gnus-data-level (car data))))
2578     (if (zerop level)
2579         ()                              ; This is a root.
2580       ;; We search until we find an article with a level less than
2581       ;; this one.  That function has to be the parent.
2582       (while (and (setq data (cdr data))
2583                   (not (< (gnus-data-level (car data)) level))))
2584       (and data (gnus-data-number (car data))))))
2585
2586 (defun gnus-unread-mark-p (mark)
2587   "Say whether MARK is the unread mark."
2588   (= mark gnus-unread-mark))
2589
2590 (defun gnus-read-mark-p (mark)
2591   "Say whether MARK is one of the marks that mark as read.
2592 This is all marks except unread, ticked, dormant, and expirable."
2593   (not (or (= mark gnus-unread-mark)
2594            (= mark gnus-ticked-mark)
2595            (= mark gnus-dormant-mark)
2596            (= mark gnus-expirable-mark))))
2597
2598 (defmacro gnus-article-mark (number)
2599   "Return the MARK of article NUMBER.
2600 This macro should only be used when computing the mark the \"first\"
2601 time; i.e., when generating the summary lines.  After that,
2602 `gnus-summary-article-mark' should be used to examine the
2603 marks of articles."
2604   `(cond
2605     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2606     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2607     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2608     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2609     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2610     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2611     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2612     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2613            gnus-ancient-mark))))
2614
2615 ;; Saving hidden threads.
2616
2617 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2618 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2619
2620 (defmacro gnus-save-hidden-threads (&rest forms)
2621   "Save hidden threads, eval FORMS, and restore the hidden threads."
2622   (let ((config (make-symbol "config")))
2623     `(let ((,config (gnus-hidden-threads-configuration)))
2624        (unwind-protect
2625            (save-excursion
2626              ,@forms)
2627          (gnus-restore-hidden-threads-configuration ,config)))))
2628
2629 (defun gnus-data-compute-positions ()
2630   "Compute the positions of all articles."
2631   (setq gnus-newsgroup-data-reverse nil)
2632   (let ((data gnus-newsgroup-data))
2633     (save-excursion
2634       (gnus-save-hidden-threads
2635         (gnus-summary-show-all-threads)
2636         (goto-char (point-min))
2637         (while data
2638           (while (get-text-property (point) 'gnus-intangible)
2639             (forward-line 1))
2640           (gnus-data-set-pos (car data) (+ (point) 3))
2641           (setq data (cdr data))
2642           (forward-line 1))))))
2643
2644 (defun gnus-hidden-threads-configuration ()
2645   "Return the current hidden threads configuration."
2646   (save-excursion
2647     (let (config)
2648       (goto-char (point-min))
2649       (while (search-forward "\r" nil t)
2650         (push (1- (point)) config))
2651       config)))
2652
2653 (defun gnus-restore-hidden-threads-configuration (config)
2654   "Restore hidden threads configuration from CONFIG."
2655   (save-excursion
2656     (let (point buffer-read-only)
2657       (while (setq point (pop config))
2658         (when (and (< point (point-max))
2659                    (goto-char point)
2660                    (eq (char-after) ?\n))
2661           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2662
2663 ;; Various summary mode internalish functions.
2664
2665 (defun gnus-mouse-pick-article (e)
2666   (interactive "e")
2667   (mouse-set-point e)
2668   (gnus-summary-next-page nil t))
2669
2670 (defun gnus-summary-set-display-table ()
2671   "Change the display table.
2672 Odd characters have a tendency to mess
2673 up nicely formatted displays - we make all possible glyphs
2674 display only a single character."
2675
2676   ;; We start from the standard display table, if any.
2677   (let ((table (or (copy-sequence standard-display-table)
2678                    (make-display-table)))
2679         (i 32))
2680     ;; Nix out all the control chars...
2681     (while (>= (setq i (1- i)) 0)
2682       (aset table i [??]))
2683     ;; ... but not newline and cr, of course.  (cr is necessary for the
2684     ;; selective display).
2685     (aset table ?\n nil)
2686     (aset table ?\r nil)
2687     ;; We keep TAB as well.
2688     (aset table ?\t nil)
2689     ;; We nix out any glyphs over 126 that are not set already.
2690     (let ((i 256))
2691       (while (>= (setq i (1- i)) 127)
2692         ;; Only modify if the entry is nil.
2693         (unless (aref table i)
2694           (aset table i [??]))))
2695     (setq buffer-display-table table)))
2696
2697 (defun gnus-summary-buffer-name (group)
2698   "Return the summary buffer name of GROUP."
2699   (concat "*Summary " group "*"))
2700
2701 (defun gnus-summary-setup-buffer (group)
2702   "Initialize summary buffer."
2703   (let ((buffer (gnus-summary-buffer-name group)))
2704     (if (get-buffer buffer)
2705         (progn
2706           (set-buffer buffer)
2707           (setq gnus-summary-buffer (current-buffer))
2708           (not gnus-newsgroup-prepared))
2709       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2710       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2711       (gnus-summary-mode group)
2712       (when gnus-carpal
2713         (gnus-carpal-setup-buffer 'summary))
2714       (unless gnus-single-article-buffer
2715         (make-local-variable 'gnus-article-buffer)
2716         (make-local-variable 'gnus-article-current)
2717         (make-local-variable 'gnus-original-article-buffer))
2718       (setq gnus-newsgroup-name group)
2719       ;; Set any local variables in the group parameters.
2720       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2721       t)))
2722
2723 (defun gnus-set-global-variables ()
2724   "Set the global equivalents of the buffer-local variables.
2725 They are set to the latest values they had.  These reflect the summary
2726 buffer that was in action when the last article was fetched."
2727   (when (eq major-mode 'gnus-summary-mode)
2728     (setq gnus-summary-buffer (current-buffer))
2729     (let ((name gnus-newsgroup-name)
2730           (marked gnus-newsgroup-marked)
2731           (unread gnus-newsgroup-unreads)
2732           (headers gnus-current-headers)
2733           (data gnus-newsgroup-data)
2734           (summary gnus-summary-buffer)
2735           (article-buffer gnus-article-buffer)
2736           (original gnus-original-article-buffer)
2737           (gac gnus-article-current)
2738           (reffed gnus-reffed-article-number)
2739           (score-file gnus-current-score-file)
2740           (default-charset gnus-newsgroup-charset)
2741           vlist)
2742       (let ((locals gnus-newsgroup-variables))
2743         (while locals
2744           (if (consp (car locals))
2745               (push (eval (caar locals)) vlist)
2746             (push (eval (car locals)) vlist))
2747           (setq locals (cdr locals)))
2748         (setq vlist (nreverse vlist)))
2749       (save-excursion
2750         (set-buffer gnus-group-buffer)
2751         (setq gnus-newsgroup-name name
2752               gnus-newsgroup-marked marked
2753               gnus-newsgroup-unreads unread
2754               gnus-current-headers headers
2755               gnus-newsgroup-data data
2756               gnus-article-current gac
2757               gnus-summary-buffer summary
2758               gnus-article-buffer article-buffer
2759               gnus-original-article-buffer original
2760               gnus-reffed-article-number reffed
2761               gnus-current-score-file score-file
2762               gnus-newsgroup-charset default-charset)
2763         (let ((locals gnus-newsgroup-variables))
2764           (while locals
2765             (if (consp (car locals))
2766                 (set (caar locals) (pop vlist))
2767               (set (car locals) (pop vlist)))
2768             (setq locals (cdr locals))))
2769         ;; The article buffer also has local variables.
2770         (when (gnus-buffer-live-p gnus-article-buffer)
2771           (set-buffer gnus-article-buffer)
2772           (setq gnus-summary-buffer summary))))))
2773
2774 (defun gnus-summary-article-unread-p (article)
2775   "Say whether ARTICLE is unread or not."
2776   (memq article gnus-newsgroup-unreads))
2777
2778 (defun gnus-summary-first-article-p (&optional article)
2779   "Return whether ARTICLE is the first article in the buffer."
2780   (if (not (setq article (or article (gnus-summary-article-number))))
2781       nil
2782     (eq article (caar gnus-newsgroup-data))))
2783
2784 (defun gnus-summary-last-article-p (&optional article)
2785   "Return whether ARTICLE is the last article in the buffer."
2786   (if (not (setq article (or article (gnus-summary-article-number))))
2787       ;; All non-existent numbers are the last article.  :-)
2788       t
2789     (not (cdr (gnus-data-find-list article)))))
2790
2791 (defun gnus-make-thread-indent-array ()
2792   (let ((n 200))
2793     (unless (and gnus-thread-indent-array
2794                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2795       (setq gnus-thread-indent-array (make-vector 201 "")
2796             gnus-thread-indent-array-level gnus-thread-indent-level)
2797       (while (>= n 0)
2798         (aset gnus-thread-indent-array n
2799               (make-string (* n gnus-thread-indent-level) ? ))
2800         (setq n (1- n))))))
2801
2802 (defun gnus-update-summary-mark-positions ()
2803   "Compute where the summary marks are to go."
2804   (save-excursion
2805     (when (gnus-buffer-exists-p gnus-summary-buffer)
2806       (set-buffer gnus-summary-buffer))
2807     (let ((gnus-replied-mark 129)
2808           (gnus-score-below-mark 130)
2809           (gnus-score-over-mark 130)
2810           (gnus-download-mark 131)
2811           (spec gnus-summary-line-format-spec)
2812           gnus-visual pos)
2813       (save-excursion
2814         (gnus-set-work-buffer)
2815         (let ((gnus-summary-line-format-spec spec)
2816               (gnus-newsgroup-downloadable '((0 . t))))
2817           (gnus-summary-insert-line
2818            (make-full-mail-header 0 "" "nobody"
2819                                   "05 Apr 2001 23:33:09 +0400"
2820                                   "" "" 0 0 "" nil)
2821            0 nil 128 t nil "" nil 1)
2822           (goto-char (point-min))
2823           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2824                                              (- (point) 2)))))
2825           (goto-char (point-min))
2826           (push (cons 'replied (and (search-forward "\201" nil t)
2827                                     (- (point) 2)))
2828                 pos)
2829           (goto-char (point-min))
2830           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2831                 pos)
2832           (goto-char (point-min))
2833           (push (cons 'download
2834                       (and (search-forward "\203" nil t) (- (point) 2)))
2835                 pos)))
2836       (setq gnus-summary-mark-positions pos))))
2837
2838 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2839   "Insert a dummy root in the summary buffer."
2840   (beginning-of-line)
2841   (gnus-add-text-properties
2842    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2843    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2844
2845 (defun gnus-summary-from-or-to-or-newsgroups (header)
2846   (let ((to (cdr (assq 'To (mail-header-extra header))))
2847         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2848         (default-mime-charset (with-current-buffer gnus-summary-buffer
2849                                 default-mime-charset)))
2850     (cond
2851      ((and to
2852            gnus-ignored-from-addresses
2853            (string-match gnus-ignored-from-addresses
2854                          (mail-header-from header)))
2855       (concat "-> "
2856               (or (car (funcall gnus-extract-address-components
2857                                 (funcall
2858                                  gnus-decode-encoded-word-function to)))
2859                   (funcall gnus-decode-encoded-word-function to))))
2860      ((and newsgroups
2861            gnus-ignored-from-addresses
2862            (string-match gnus-ignored-from-addresses
2863                          (mail-header-from header)))
2864       (concat "=> " newsgroups))
2865      (t
2866       (or (car (funcall gnus-extract-address-components
2867                         (mail-header-from header)))
2868           (mail-header-from header))))))
2869
2870 (defun gnus-summary-insert-line (gnus-tmp-header
2871                                  gnus-tmp-level gnus-tmp-current
2872                                  gnus-tmp-unread gnus-tmp-replied
2873                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2874                                  &optional gnus-tmp-dummy gnus-tmp-score
2875                                  gnus-tmp-process)
2876   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2877          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2878          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2879          (gnus-tmp-score-char
2880           (if (or (null gnus-summary-default-score)
2881                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2882                       gnus-summary-zcore-fuzz))
2883               ?\ ;;;Whitespace
2884             (if (< gnus-tmp-score gnus-summary-default-score)
2885                 gnus-score-below-mark gnus-score-over-mark)))
2886          (gnus-tmp-replied
2887           (cond (gnus-tmp-process gnus-process-mark)
2888                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2889                  gnus-cached-mark)
2890                 (gnus-tmp-replied gnus-replied-mark)
2891                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2892                  gnus-saved-mark)
2893                 (t gnus-no-mark)))
2894          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2895          (gnus-tmp-name
2896           (cond
2897            ((string-match "<[^>]+> *$" gnus-tmp-from)
2898             (let ((beg (match-beginning 0)))
2899               (or (and (string-match "^\".+\"" gnus-tmp-from)
2900                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2901                   (substring gnus-tmp-from 0 beg))))
2902            ((string-match "(.+)" gnus-tmp-from)
2903             (substring gnus-tmp-from
2904                        (1+ (match-beginning 0)) (1- (match-end 0))))
2905            (t gnus-tmp-from)))
2906          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2907          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2908          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2909          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2910          (buffer-read-only nil))
2911     (when (string= gnus-tmp-name "")
2912       (setq gnus-tmp-name gnus-tmp-from))
2913     (unless (numberp gnus-tmp-lines)
2914       (setq gnus-tmp-lines -1))
2915     (when (= gnus-tmp-lines -1)
2916       (setq gnus-tmp-lines "?"))
2917     (gnus-put-text-property-excluding-characters-with-faces
2918      (point)
2919      (progn (eval gnus-summary-line-format-spec) (point))
2920      'gnus-number gnus-tmp-number)
2921     (when (gnus-visual-p 'summary-highlight 'highlight)
2922       (forward-line -1)
2923       (gnus-run-hooks 'gnus-summary-update-hook)
2924       (forward-line 1))))
2925
2926 (defun gnus-summary-update-line (&optional dont-update)
2927   "Update summary line after change."
2928   (when (and gnus-summary-default-score
2929              (not gnus-summary-inhibit-highlight))
2930     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2931            (article (gnus-summary-article-number))
2932            (score (gnus-summary-article-score article)))
2933       (unless dont-update
2934         (if (and gnus-summary-mark-below
2935                  (< (gnus-summary-article-score)
2936                     gnus-summary-mark-below))
2937             ;; This article has a low score, so we mark it as read.
2938             (when (memq article gnus-newsgroup-unreads)
2939               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2940           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2941             ;; This article was previously marked as read on account
2942             ;; of a low score, but now it has risen, so we mark it as
2943             ;; unread.
2944             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2945         (gnus-summary-update-mark
2946          (if (or (null gnus-summary-default-score)
2947                  (<= (abs (- score gnus-summary-default-score))
2948                      gnus-summary-zcore-fuzz))
2949              ?\ ;;;Whitespace
2950            (if (< score gnus-summary-default-score)
2951                gnus-score-below-mark gnus-score-over-mark))
2952          'score))
2953       ;; Do visual highlighting.
2954       (when (gnus-visual-p 'summary-highlight 'highlight)
2955         (gnus-run-hooks 'gnus-summary-update-hook)))))
2956
2957 (defvar gnus-tmp-new-adopts nil)
2958
2959 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2960   "Return the number of articles in THREAD.
2961 This may be 0 in some cases -- if none of the articles in
2962 the thread are to be displayed."
2963   (let* ((number
2964           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2965           (cond
2966            ((not (listp thread))
2967             1)
2968            ((and (consp thread) (cdr thread))
2969             (apply
2970              '+ 1 (mapcar
2971                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2972            ((null thread)
2973             1)
2974            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2975             1)
2976            (t 0))))
2977     (when (and level (zerop level) gnus-tmp-new-adopts)
2978       (incf number
2979             (apply '+ (mapcar
2980                        'gnus-summary-number-of-articles-in-thread
2981                        gnus-tmp-new-adopts))))
2982     (if char
2983         (if (> number 1) gnus-not-empty-thread-mark
2984           gnus-empty-thread-mark)
2985       number)))
2986
2987 (defun gnus-summary-set-local-parameters (group)
2988   "Go through the local params of GROUP and set all variable specs in that list."
2989   (let ((params (gnus-group-find-parameter group))
2990         elem)
2991     (while params
2992       (setq elem (car params)
2993             params (cdr params))
2994       (and (consp elem)                 ; Has to be a cons.
2995            (consp (cdr elem))           ; The cdr has to be a list.
2996            (symbolp (car elem))         ; Has to be a symbol in there.
2997            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2998            (ignore-errors               ; So we set it.
2999              (make-local-variable (car elem))
3000              (set (car elem) (eval (nth 1 elem))))))))
3001
3002 (defun gnus-summary-read-group (group &optional show-all no-article
3003                                       kill-buffer no-display backward
3004                                       select-articles)
3005   "Start reading news in newsgroup GROUP.
3006 If SHOW-ALL is non-nil, already read articles are also listed.
3007 If NO-ARTICLE is non-nil, no article is selected initially.
3008 If NO-DISPLAY, don't generate a summary buffer."
3009   (let (result)
3010     (while (and group
3011                 (null (setq result
3012                             (let ((gnus-auto-select-next nil))
3013                               (or (gnus-summary-read-group-1
3014                                    group show-all no-article
3015                                    kill-buffer no-display
3016                                    select-articles)
3017                                   (setq show-all nil
3018                                         select-articles nil)))))
3019                 (eq gnus-auto-select-next 'quietly))
3020       (set-buffer gnus-group-buffer)
3021       ;; The entry function called above goes to the next
3022       ;; group automatically, so we go two groups back
3023       ;; if we are searching for the previous group.
3024       (when backward
3025         (gnus-group-prev-unread-group 2))
3026       (if (not (equal group (gnus-group-group-name)))
3027           (setq group (gnus-group-group-name))
3028         (setq group nil)))
3029     result))
3030
3031 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3032   "Directly jump to the other GROUP from summary buffer.
3033 If SHOW-ALL is non-nil, already read articles are also listed."
3034   (interactive
3035    (if (eq gnus-summary-buffer (current-buffer))
3036        (list (completing-read
3037               "Group: " gnus-active-hashtb nil t
3038               (when (and gnus-newsgroup-name
3039                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3040                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3041               'gnus-group-history)
3042              current-prefix-arg)
3043      (error "%s must be invoked from a gnus summary buffer." this-command)))
3044   (unless (or (zerop (length group))
3045               (and gnus-newsgroup-name
3046                    (string-equal gnus-newsgroup-name group)))
3047     (gnus-summary-exit)
3048     (gnus-summary-read-group group show-all
3049                              gnus-dont-select-after-jump-to-other-group)))
3050
3051 (defun gnus-summary-read-group-1 (group show-all no-article
3052                                         kill-buffer no-display
3053                                         &optional select-articles)
3054   ;; Killed foreign groups can't be entered.
3055   ;;  (when (and (not (gnus-group-native-p group))
3056   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3057   ;;    (error "Dead non-native groups can't be entered"))
3058   (gnus-message 5 "Retrieving newsgroup: %s..."
3059                 (gnus-group-decoded-name group))
3060   (let* ((new-group (gnus-summary-setup-buffer group))
3061          (quit-config (gnus-group-quit-config group))
3062          (did-select (and new-group (gnus-select-newsgroup
3063                                      group show-all select-articles))))
3064     (cond
3065      ;; This summary buffer exists already, so we just select it.
3066      ((not new-group)
3067       (gnus-set-global-variables)
3068       (when kill-buffer
3069         (gnus-kill-or-deaden-summary kill-buffer))
3070       (gnus-configure-windows 'summary 'force)
3071       (gnus-set-mode-line 'summary)
3072       (gnus-summary-position-point)
3073       (message "")
3074       t)
3075      ;; We couldn't select this group.
3076      ((null did-select)
3077       (when (and (eq major-mode 'gnus-summary-mode)
3078                  (not (equal (current-buffer) kill-buffer)))
3079         (kill-buffer (current-buffer))
3080         (if (not quit-config)
3081             (progn
3082               ;; Update the info -- marks might need to be removed,
3083               ;; for instance.
3084               (gnus-summary-update-info)
3085               (set-buffer gnus-group-buffer)
3086               (gnus-group-jump-to-group group)
3087               (gnus-group-next-unread-group 1))
3088           (gnus-handle-ephemeral-exit quit-config)))
3089       (let ((grpinfo (gnus-get-info group)))
3090         (if (null (gnus-info-read grpinfo))
3091             (gnus-message 3 "Group %s contains no messages" 
3092                           (gnus-group-decoded-name group))
3093           (gnus-message 3 "Can't select group")))
3094       nil)
3095      ;; The user did a `C-g' while prompting for number of articles,
3096      ;; so we exit this group.
3097      ((eq did-select 'quit)
3098       (and (eq major-mode 'gnus-summary-mode)
3099            (not (equal (current-buffer) kill-buffer))
3100            (kill-buffer (current-buffer)))
3101       (when kill-buffer
3102         (gnus-kill-or-deaden-summary kill-buffer))
3103       (if (not quit-config)
3104           (progn
3105             (set-buffer gnus-group-buffer)
3106             (gnus-group-jump-to-group group)
3107             (gnus-group-next-unread-group 1)
3108             (gnus-configure-windows 'group 'force))
3109         (gnus-handle-ephemeral-exit quit-config))
3110       ;; Finally signal the quit.
3111       (signal 'quit nil))
3112      ;; The group was successfully selected.
3113      (t
3114       (gnus-set-global-variables)
3115       ;; Save the active value in effect when the group was entered.
3116       (setq gnus-newsgroup-active
3117             (gnus-copy-sequence
3118              (gnus-active gnus-newsgroup-name)))
3119       ;; You can change the summary buffer in some way with this hook.
3120       (gnus-run-hooks 'gnus-select-group-hook)
3121       (gnus-update-format-specifications
3122        nil 'summary 'summary-mode 'summary-dummy)
3123       (gnus-update-summary-mark-positions)
3124       ;; Do score processing.
3125       (when gnus-use-scoring
3126         (gnus-possibly-score-headers))
3127       ;; Check whether to fill in the gaps in the threads.
3128       (when gnus-build-sparse-threads
3129         (gnus-build-sparse-threads))
3130       ;; Find the initial limit.
3131       (if gnus-show-threads
3132           (if show-all
3133               (let ((gnus-newsgroup-dormant nil))
3134                 (gnus-summary-initial-limit show-all))
3135             (gnus-summary-initial-limit show-all))
3136         ;; When untreaded, all articles are always shown.
3137         (setq gnus-newsgroup-limit
3138               (mapcar
3139                (lambda (header) (mail-header-number header))
3140                gnus-newsgroup-headers)))
3141       ;; Generate the summary buffer.
3142       (unless no-display
3143         (gnus-summary-prepare))
3144       (when gnus-use-trees
3145         (gnus-tree-open group)
3146         (setq gnus-summary-highlight-line-function
3147               'gnus-tree-highlight-article))
3148       ;; If the summary buffer is empty, but there are some low-scored
3149       ;; articles or some excluded dormants, we include these in the
3150       ;; buffer.
3151       (when (and (zerop (buffer-size))
3152                  (not no-display))
3153         (cond (gnus-newsgroup-dormant
3154                (gnus-summary-limit-include-dormant))
3155               ((and gnus-newsgroup-scored show-all)
3156                (gnus-summary-limit-include-expunged t))))
3157       ;; Function `gnus-apply-kill-file' must be called in this hook.
3158       (gnus-run-hooks 'gnus-apply-kill-hook)
3159       (if (and (zerop (buffer-size))
3160                (not no-display))
3161           (progn
3162             ;; This newsgroup is empty.
3163             (gnus-summary-catchup-and-exit nil t)
3164             (gnus-message 6 "No unread news")
3165             (when kill-buffer
3166               (gnus-kill-or-deaden-summary kill-buffer))
3167             ;; Return nil from this function.
3168             nil)
3169         ;; Hide conversation thread subtrees.  We cannot do this in
3170         ;; gnus-summary-prepare-hook since kill processing may not
3171         ;; work with hidden articles.
3172         (and gnus-show-threads
3173              gnus-thread-hide-subtree
3174              (gnus-summary-hide-all-threads))
3175         (when kill-buffer
3176           (gnus-kill-or-deaden-summary kill-buffer))
3177         ;; Show first unread article if requested.
3178         (if (and (not no-article)
3179                  (not no-display)
3180                  gnus-newsgroup-unreads
3181                  gnus-auto-select-first)
3182             (progn
3183               (gnus-configure-windows 'summary)
3184               (cond
3185                ((eq gnus-auto-select-first 'best)
3186                 (gnus-summary-best-unread-article))
3187                ((eq gnus-auto-select-first t)
3188                 (gnus-summary-first-unread-article))
3189                ((gnus-functionp gnus-auto-select-first)
3190                 (funcall gnus-auto-select-first))))
3191           ;; Don't select any articles, just move point to the first
3192           ;; article in the group.
3193           (goto-char (point-min))
3194           (gnus-summary-position-point)
3195           (gnus-configure-windows 'summary 'force)
3196           (gnus-set-mode-line 'summary))
3197         (when (get-buffer-window gnus-group-buffer t)
3198           ;; Gotta use windows, because recenter does weird stuff if
3199           ;; the current buffer ain't the displayed window.
3200           (let ((owin (selected-window)))
3201             (select-window (get-buffer-window gnus-group-buffer t))
3202             (when (gnus-group-goto-group group)
3203               (recenter))
3204             (select-window owin)))
3205         ;; Mark this buffer as "prepared".
3206         (setq gnus-newsgroup-prepared t)
3207         (gnus-run-hooks 'gnus-summary-prepared-hook)
3208         t)))))
3209
3210 (defun gnus-summary-prepare ()
3211   "Generate the summary buffer."
3212   (interactive)
3213   (let ((buffer-read-only nil))
3214     (erase-buffer)
3215     (setq gnus-newsgroup-data nil
3216           gnus-newsgroup-data-reverse nil)
3217     (gnus-run-hooks 'gnus-summary-generate-hook)
3218     ;; Generate the buffer, either with threads or without.
3219     (when gnus-newsgroup-headers
3220       (gnus-summary-prepare-threads
3221        (if gnus-show-threads
3222            (gnus-sort-gathered-threads
3223             (funcall gnus-summary-thread-gathering-function
3224                      (gnus-sort-threads
3225                       (gnus-cut-threads (gnus-make-threads)))))
3226          ;; Unthreaded display.
3227          (gnus-sort-articles gnus-newsgroup-headers))))
3228     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3229     ;; Call hooks for modifying summary buffer.
3230     (goto-char (point-min))
3231     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3232
3233 (defsubst gnus-general-simplify-subject (subject)
3234   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3235   (setq subject
3236         (cond
3237          ;; Truncate the subject.
3238          (gnus-simplify-subject-functions
3239           (gnus-map-function gnus-simplify-subject-functions subject))
3240          ((numberp gnus-summary-gather-subject-limit)
3241           (setq subject (gnus-simplify-subject-re subject))
3242           (if (> (length subject) gnus-summary-gather-subject-limit)
3243               (substring subject 0 gnus-summary-gather-subject-limit)
3244             subject))
3245          ;; Fuzzily simplify it.
3246          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3247           (gnus-simplify-subject-fuzzy subject))
3248          ;; Just remove the leading "Re:".
3249          (t
3250           (gnus-simplify-subject-re subject))))
3251
3252   (if (and gnus-summary-gather-exclude-subject
3253            (string-match gnus-summary-gather-exclude-subject subject))
3254       nil                               ; This article shouldn't be gathered
3255     subject))
3256
3257 (defun gnus-summary-simplify-subject-query ()
3258   "Query where the respool algorithm would put this article."
3259   (interactive)
3260   (gnus-summary-select-article)
3261   (message "%s"
3262            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3263
3264 (defun gnus-gather-threads-by-subject (threads)
3265   "Gather threads by looking at Subject headers."
3266   (if (not gnus-summary-make-false-root)
3267       threads
3268     (let ((hashtb (gnus-make-hashtable 1024))
3269           (prev threads)
3270           (result threads)
3271           subject hthread whole-subject)
3272       (while threads
3273         (setq subject (gnus-general-simplify-subject
3274                        (setq whole-subject (mail-header-subject
3275                                             (caar threads)))))
3276         (when subject
3277           (if (setq hthread (gnus-gethash subject hashtb))
3278               (progn
3279                 ;; We enter a dummy root into the thread, if we
3280                 ;; haven't done that already.
3281                 (unless (stringp (caar hthread))
3282                   (setcar hthread (list whole-subject (car hthread))))
3283                 ;; We add this new gathered thread to this gathered
3284                 ;; thread.
3285                 (setcdr (car hthread)
3286                         (nconc (cdar hthread) (list (car threads))))
3287                 ;; Remove it from the list of threads.
3288                 (setcdr prev (cdr threads))
3289                 (setq threads prev))
3290             ;; Enter this thread into the hash table.
3291             (gnus-sethash subject threads hashtb)))
3292         (setq prev threads)
3293         (setq threads (cdr threads)))
3294       result)))
3295
3296 (defun gnus-gather-threads-by-references (threads)
3297   "Gather threads by looking at References headers."
3298   (let ((idhashtb (gnus-make-hashtable 1024))
3299         (thhashtb (gnus-make-hashtable 1024))
3300         (prev threads)
3301         (result threads)
3302         ids references id gthread gid entered ref)
3303     (while threads
3304       (when (setq references (mail-header-references (caar threads)))
3305         (setq id (mail-header-id (caar threads))
3306               ids (gnus-split-references references)
3307               entered nil)
3308         (while (setq ref (pop ids))
3309           (setq ids (delete ref ids))
3310           (if (not (setq gid (gnus-gethash ref idhashtb)))
3311               (progn
3312                 (gnus-sethash ref id idhashtb)
3313                 (gnus-sethash id threads thhashtb))
3314             (setq gthread (gnus-gethash gid thhashtb))
3315             (unless entered
3316               ;; We enter a dummy root into the thread, if we
3317               ;; haven't done that already.
3318               (unless (stringp (caar gthread))
3319                 (setcar gthread (list (mail-header-subject (caar gthread))
3320                                       (car gthread))))
3321               ;; We add this new gathered thread to this gathered
3322               ;; thread.
3323               (setcdr (car gthread)
3324                       (nconc (cdar gthread) (list (car threads)))))
3325             ;; Add it into the thread hash table.
3326             (gnus-sethash id gthread thhashtb)
3327             (setq entered t)
3328             ;; Remove it from the list of threads.
3329             (setcdr prev (cdr threads))
3330             (setq threads prev))))
3331       (setq prev threads)
3332       (setq threads (cdr threads)))
3333     result))
3334
3335 (defun gnus-sort-gathered-threads (threads)
3336   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3337   (let ((result threads))
3338     (while threads
3339       (when (stringp (caar threads))
3340         (setcdr (car threads)
3341                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3342       (setq threads (cdr threads)))
3343     result))
3344
3345 (defun gnus-thread-loop-p (root thread)
3346   "Say whether ROOT is in THREAD."
3347   (let ((stack (list thread))
3348         (infloop 0)
3349         th)
3350     (while (setq thread (pop stack))
3351       (setq th (cdr thread))
3352       (while (and th
3353                   (not (eq (caar th) root)))
3354         (pop th))
3355       (if th
3356           ;; We have found a loop.
3357           (let (ref-dep)
3358             (setcdr thread (delq (car th) (cdr thread)))
3359             (if (boundp (setq ref-dep (intern "none"
3360                                               gnus-newsgroup-dependencies)))
3361                 (setcdr (symbol-value ref-dep)
3362                         (nconc (cdr (symbol-value ref-dep))
3363                                (list (car th))))
3364               (set ref-dep (list nil (car th))))
3365             (setq infloop 1
3366                   stack nil))
3367         ;; Push all the subthreads onto the stack.
3368         (push (cdr thread) stack)))
3369     infloop))
3370
3371 (defun gnus-make-threads ()
3372   "Go through the dependency hashtb and find the roots.  Return all threads."
3373   (let (threads)
3374     (while (catch 'infloop
3375              (mapatoms
3376               (lambda (refs)
3377                 ;; Deal with self-referencing References loops.
3378                 (when (and (car (symbol-value refs))
3379                            (not (zerop
3380                                  (apply
3381                                   '+
3382                                   (mapcar
3383                                    (lambda (thread)
3384                                      (gnus-thread-loop-p
3385                                       (car (symbol-value refs)) thread))
3386                                    (cdr (symbol-value refs)))))))
3387                   (setq threads nil)
3388                   (throw 'infloop t))
3389                 (unless (car (symbol-value refs))
3390                   ;; These threads do not refer back to any other articles,
3391                   ;; so they're roots.
3392                   (setq threads (append (cdr (symbol-value refs)) threads))))
3393               gnus-newsgroup-dependencies)))
3394     threads))
3395
3396 ;; Build the thread tree.
3397 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3398   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3399
3400 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3401 if it was already present.
3402
3403 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3404 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3405 Message-IDs will be renamed be renamed to a unique Message-ID before
3406 being entered.
3407
3408 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3409   (let* ((id (mail-header-id header))
3410          (id-dep (and id (intern id dependencies)))
3411          ref ref-dep ref-header)
3412     ;; Enter this `header' in the `dependencies' table.
3413     (cond
3414      ((not id-dep)
3415       (setq header nil))
3416      ;; The first two cases do the normal part: enter a new `header'
3417      ;; in the `dependencies' table.
3418      ((not (boundp id-dep))
3419       (set id-dep (list header)))
3420      ((null (car (symbol-value id-dep)))
3421       (setcar (symbol-value id-dep) header))
3422
3423      ;; From here the `header' was already present in the
3424      ;; `dependencies' table.
3425      (force-new
3426       ;; Overrides an existing entry;
3427       ;; just set the header part of the entry.
3428       (setcar (symbol-value id-dep) header))
3429
3430      ;; Renames the existing `header' to a unique Message-ID.
3431      ((not gnus-summary-ignore-duplicates)
3432       ;; An article with this Message-ID has already been seen.
3433       ;; We rename the Message-ID.
3434       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3435            (list header))
3436       (mail-header-set-id header id))
3437
3438      ;; The last case ignores an existing entry, except it adds any
3439      ;; additional Xrefs (in case the two articles came from different
3440      ;; servers.
3441      ;; Also sets `header' to `nil' meaning that the `dependencies'
3442      ;; table was *not* modified.
3443      (t
3444       (mail-header-set-xref
3445        (car (symbol-value id-dep))
3446        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3447                    "")
3448                (or (mail-header-xref header) "")))
3449       (setq header nil)))
3450
3451     (when header
3452       ;; First check if that we are not creating a References loop.
3453       (setq ref (gnus-parent-id (mail-header-references header)))
3454       (while (and ref
3455                   (setq ref-dep (intern-soft ref dependencies))
3456                   (boundp ref-dep)
3457                   (setq ref-header (car (symbol-value ref-dep))))
3458         (if (string= id ref)
3459             ;; Yuk!  This is a reference loop.  Make the article be a
3460             ;; root article.
3461             (progn
3462               (mail-header-set-references (car (symbol-value id-dep)) "none")
3463               (setq ref nil))
3464           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3465       (setq ref (gnus-parent-id (mail-header-references header)))
3466       (setq ref-dep (intern (or ref "none") dependencies))
3467       (if (boundp ref-dep)
3468           (setcdr (symbol-value ref-dep)
3469                   (nconc (cdr (symbol-value ref-dep))
3470                          (list (symbol-value id-dep))))
3471         (set ref-dep (list nil (symbol-value id-dep)))))
3472     header))
3473
3474 (defun gnus-build-sparse-threads ()
3475   (let ((headers gnus-newsgroup-headers)
3476         (mail-parse-charset gnus-newsgroup-charset)
3477         (gnus-summary-ignore-duplicates t)
3478         header references generation relations
3479         subject child end new-child date)
3480     ;; First we create an alist of generations/relations, where
3481     ;; generations is how much we trust the relation, and the relation
3482     ;; is parent/child.
3483     (gnus-message 7 "Making sparse threads...")
3484     (save-excursion
3485       (nnheader-set-temp-buffer " *gnus sparse threads*")
3486       (while (setq header (pop headers))
3487         (when (and (setq references (mail-header-references header))
3488                    (not (string= references "")))
3489           (insert references)
3490           (setq child (mail-header-id header)
3491                 subject (mail-header-subject header)
3492                 date (mail-header-date header)
3493                 generation 0)
3494           (while (search-backward ">" nil t)
3495             (setq end (1+ (point)))
3496             (when (search-backward "<" nil t)
3497               (setq new-child (buffer-substring (point) end))
3498               (push (list (incf generation)
3499                           child (setq child new-child)
3500                           subject date)
3501                     relations)))
3502           (when child
3503             (push (list (1+ generation) child nil subject) relations))
3504           (erase-buffer)))
3505       (kill-buffer (current-buffer)))
3506     ;; Sort over trustworthiness.
3507     (mapcar
3508      (lambda (relation)
3509        (when (gnus-dependencies-add-header
3510               (make-full-mail-header-from-decoded-header
3511                gnus-reffed-article-number
3512                (nth 3 relation) "" (or (nth 4 relation) "")
3513                (nth 1 relation)
3514                (or (nth 2 relation) "") 0 0 "")
3515               gnus-newsgroup-dependencies nil)
3516          (push gnus-reffed-article-number gnus-newsgroup-limit)
3517          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3518          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3519                gnus-newsgroup-reads)
3520          (decf gnus-reffed-article-number)))
3521      (sort relations 'car-less-than-car))
3522     (gnus-message 7 "Making sparse threads...done")))
3523
3524 (defun gnus-build-old-threads ()
3525   ;; Look at all the articles that refer back to old articles, and
3526   ;; fetch the headers for the articles that aren't there.  This will
3527   ;; build complete threads - if the roots haven't been expired by the
3528   ;; server, that is.
3529   (let ((mail-parse-charset gnus-newsgroup-charset)
3530         id heads)
3531     (mapatoms
3532      (lambda (refs)
3533        (when (not (car (symbol-value refs)))
3534          (setq heads (cdr (symbol-value refs)))
3535          (while heads
3536            (if (memq (mail-header-number (caar heads))
3537                      gnus-newsgroup-dormant)
3538                (setq heads (cdr heads))
3539              (setq id (symbol-name refs))
3540              (while (and (setq id (gnus-build-get-header id))
3541                          (not (car (gnus-id-to-thread id)))))
3542              (setq heads nil)))))
3543      gnus-newsgroup-dependencies)))
3544
3545 ;; This function has to be called with point after the article number
3546 ;; on the beginning of the line.
3547 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3548   (let ((eol (gnus-point-at-eol))
3549         (buffer (current-buffer))
3550         header)
3551
3552     ;; overview: [num subject from date id refs chars lines misc]
3553     (unwind-protect
3554         (progn
3555           (narrow-to-region (point) eol)
3556           (unless (eobp)
3557             (forward-char))
3558
3559           (setq header
3560                 (make-full-mail-header
3561                  number                         ; number
3562                  (nnheader-nov-field)           ; subject
3563                  (nnheader-nov-field)           ; from
3564                  (nnheader-nov-field)           ; date
3565                  (nnheader-nov-read-message-id) ; id
3566                  (nnheader-nov-field)           ; refs
3567                  (nnheader-nov-read-integer)    ; chars
3568                  (nnheader-nov-read-integer)    ; lines
3569                  (unless (eobp)
3570                    (if (looking-at "Xref: ")
3571                        (goto-char (match-end 0)))
3572                    (nnheader-nov-field))        ; Xref
3573                  (nnheader-nov-parse-extra))))  ; extra
3574
3575       (widen))
3576
3577     (when gnus-alter-header-function
3578       (funcall gnus-alter-header-function header))
3579     (gnus-dependencies-add-header header dependencies force-new)))
3580
3581 (defun gnus-build-get-header (id)
3582   "Look through the buffer of NOV lines and find the header to ID.
3583 Enter this line into the dependencies hash table, and return
3584 the id of the parent article (if any)."
3585   (let ((deps gnus-newsgroup-dependencies)
3586         found header)
3587     (prog1
3588         (save-excursion
3589           (set-buffer nntp-server-buffer)
3590           (let ((case-fold-search nil))
3591             (goto-char (point-min))
3592             (while (and (not found)
3593                         (search-forward id nil t))
3594               (beginning-of-line)
3595               (setq found (looking-at
3596                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3597                                    (regexp-quote id))))
3598               (or found (beginning-of-line 2)))
3599             (when found
3600               (beginning-of-line)
3601               (and
3602                (setq header (gnus-nov-parse-line
3603                              (read (current-buffer)) deps))
3604                (gnus-parent-id (mail-header-references header))))))
3605       (when header
3606         (let ((number (mail-header-number header)))
3607           (push number gnus-newsgroup-limit)
3608           (push header gnus-newsgroup-headers)
3609           (if (memq number gnus-newsgroup-unselected)
3610               (progn
3611                 (push number gnus-newsgroup-unreads)
3612                 (setq gnus-newsgroup-unselected
3613                       (delq number gnus-newsgroup-unselected)))
3614             (push number gnus-newsgroup-ancient)))))))
3615
3616 (defun gnus-build-all-threads ()
3617   "Read all the headers."
3618   (let ((gnus-summary-ignore-duplicates t)
3619         (mail-parse-charset gnus-newsgroup-charset)
3620         (dependencies gnus-newsgroup-dependencies)
3621         header article)
3622     (save-excursion
3623       (set-buffer nntp-server-buffer)
3624       (let ((case-fold-search nil))
3625         (goto-char (point-min))
3626         (while (not (eobp))
3627           (ignore-errors
3628             (setq article (read (current-buffer))
3629                   header (gnus-nov-parse-line article dependencies)))
3630           (when header
3631             (save-excursion
3632               (set-buffer gnus-summary-buffer)
3633               (push header gnus-newsgroup-headers)
3634               (if (memq (setq article (mail-header-number header))
3635                         gnus-newsgroup-unselected)
3636                   (progn
3637                     (push article gnus-newsgroup-unreads)
3638                     (setq gnus-newsgroup-unselected
3639                           (delq article gnus-newsgroup-unselected)))
3640                 (push article gnus-newsgroup-ancient)))
3641             (forward-line 1)))))))
3642
3643 (defun gnus-summary-update-article-line (article header)
3644   "Update the line for ARTICLE using HEADERS."
3645   (let* ((id (mail-header-id header))
3646          (thread (gnus-id-to-thread id)))
3647     (unless thread
3648       (error "Article in no thread"))
3649     ;; Update the thread.
3650     (setcar thread header)
3651     (gnus-summary-goto-subject article)
3652     (let* ((datal (gnus-data-find-list article))
3653            (data (car datal))
3654            (length (when (cdr datal)
3655                      (- (gnus-data-pos data)
3656                         (gnus-data-pos (cadr datal)))))
3657            (buffer-read-only nil)
3658            (level (gnus-summary-thread-level)))
3659       (gnus-delete-line)
3660       (gnus-summary-insert-line
3661        header level nil (gnus-article-mark article)
3662        (memq article gnus-newsgroup-replied)
3663        (memq article gnus-newsgroup-expirable)
3664        ;; Only insert the Subject string when it's different
3665        ;; from the previous Subject string.
3666        (if (and
3667             gnus-show-threads
3668             (gnus-subject-equal
3669              (condition-case ()
3670                  (mail-header-subject
3671                   (gnus-data-header
3672                    (cadr
3673                     (gnus-data-find-list
3674                      article
3675                      (gnus-data-list t)))))
3676                ;; Error on the side of excessive subjects.
3677                (error ""))
3678              (mail-header-subject header)))
3679            ""
3680          (mail-header-subject header))
3681        nil (cdr (assq article gnus-newsgroup-scored))
3682        (memq article gnus-newsgroup-processable))
3683       (when length
3684         (gnus-data-update-list
3685          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3686
3687 (defun gnus-summary-update-article (article &optional iheader)
3688   "Update ARTICLE in the summary buffer."
3689   (set-buffer gnus-summary-buffer)
3690   (let* ((header (gnus-summary-article-header article))
3691          (id (mail-header-id header))
3692          (data (gnus-data-find article))
3693          (thread (gnus-id-to-thread id))
3694          (references (mail-header-references header))
3695          (parent
3696           (gnus-id-to-thread
3697            (or (gnus-parent-id
3698                 (when (and references
3699                            (not (equal "" references)))
3700                   references))
3701                "none")))
3702          (buffer-read-only nil)
3703          (old (car thread)))
3704     (when thread
3705       (unless iheader
3706         (setcar thread nil)
3707         (when parent
3708           (delq thread parent)))
3709       (if (gnus-summary-insert-subject id header)
3710           ;; Set the (possibly) new article number in the data structure.
3711           (gnus-data-set-number data (gnus-id-to-article id))
3712         (setcar thread old)
3713         nil))))
3714
3715 (defun gnus-rebuild-thread (id &optional line)
3716   "Rebuild the thread containing ID.
3717 If LINE, insert the rebuilt thread starting on line LINE."
3718   (let ((buffer-read-only nil)
3719         old-pos current thread data)
3720     (if (not gnus-show-threads)
3721         (setq thread (list (car (gnus-id-to-thread id))))
3722       ;; Get the thread this article is part of.
3723       (setq thread (gnus-remove-thread id)))
3724     (setq old-pos (gnus-point-at-bol))
3725     (setq current (save-excursion
3726                     (and (zerop (forward-line -1))
3727                          (gnus-summary-article-number))))
3728     ;; If this is a gathered thread, we have to go some re-gathering.
3729     (when (stringp (car thread))
3730       (let ((subject (car thread))
3731             roots thr)
3732         (setq thread (cdr thread))
3733         (while thread
3734           (unless (memq (setq thr (gnus-id-to-thread
3735                                    (gnus-root-id
3736                                     (mail-header-id (caar thread)))))
3737                         roots)
3738             (push thr roots))
3739           (setq thread (cdr thread)))
3740         ;; We now have all (unique) roots.
3741         (if (= (length roots) 1)
3742             ;; All the loose roots are now one solid root.
3743             (setq thread (car roots))
3744           (setq thread (cons subject (gnus-sort-threads roots))))))
3745     (let (threads)
3746       ;; We then insert this thread into the summary buffer.
3747       (when line
3748         (goto-char (point-min))
3749         (forward-line (1- line)))
3750       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3751         (if gnus-show-threads
3752             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3753           (gnus-summary-prepare-unthreaded thread))
3754         (setq data (nreverse gnus-newsgroup-data))
3755         (setq threads gnus-newsgroup-threads))
3756       ;; We splice the new data into the data structure.
3757       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3758       ;;!!! then we want to insert at the beginning of the buffer.
3759       ;;!!! That happens to be true with Gnus now, but that may
3760       ;;!!! change in the future.  Perhaps.
3761       (gnus-data-enter-list
3762        (if line nil current) data (- (point) old-pos))
3763       (setq gnus-newsgroup-threads
3764             (nconc threads gnus-newsgroup-threads))
3765       (gnus-data-compute-positions))))
3766
3767 (defun gnus-number-to-header (number)
3768   "Return the header for article NUMBER."
3769   (let ((headers gnus-newsgroup-headers))
3770     (while (and headers
3771                 (not (= number (mail-header-number (car headers)))))
3772       (pop headers))
3773     (when headers
3774       (car headers))))
3775
3776 (defun gnus-parent-headers (in-headers &optional generation)
3777   "Return the headers of the GENERATIONeth parent of HEADERS."
3778   (unless generation
3779     (setq generation 1))
3780   (let ((parent t)
3781         (headers in-headers)
3782         references)
3783     (while (and parent
3784                 (not (zerop generation))
3785                 (setq references (mail-header-references headers)))
3786       (setq headers (if (and references
3787                              (setq parent (gnus-parent-id references)))
3788                         (car (gnus-id-to-thread parent))
3789                       nil))
3790       (decf generation))
3791     (and (not (eq headers in-headers))
3792          headers)))
3793
3794 (defun gnus-id-to-thread (id)
3795   "Return the (sub-)thread where ID appears."
3796   (gnus-gethash id gnus-newsgroup-dependencies))
3797
3798 (defun gnus-id-to-article (id)
3799   "Return the article number of ID."
3800   (let ((thread (gnus-id-to-thread id)))
3801     (when (and thread
3802                (car thread))
3803       (mail-header-number (car thread)))))
3804
3805 (defun gnus-id-to-header (id)
3806   "Return the article headers of ID."
3807   (car (gnus-id-to-thread id)))
3808
3809 (defun gnus-article-displayed-root-p (article)
3810   "Say whether ARTICLE is a root(ish) article."
3811   (let ((level (gnus-summary-thread-level article))
3812         (refs (mail-header-references  (gnus-summary-article-header article)))
3813         particle)
3814     (cond
3815      ((null level) nil)
3816      ((zerop level) t)
3817      ((null refs) t)
3818      ((null (gnus-parent-id refs)) t)
3819      ((and (= 1 level)
3820            (null (setq particle (gnus-id-to-article
3821                                  (gnus-parent-id refs))))
3822            (null (gnus-summary-thread-level particle)))))))
3823
3824 (defun gnus-root-id (id)
3825   "Return the id of the root of the thread where ID appears."
3826   (let (last-id prev)
3827     (while (and id (setq prev (car (gnus-id-to-thread id))))
3828       (setq last-id id
3829             id (gnus-parent-id (mail-header-references prev))))
3830     last-id))
3831
3832 (defun gnus-articles-in-thread (thread)
3833   "Return the list of articles in THREAD."
3834   (cons (mail-header-number (car thread))
3835         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3836
3837 (defun gnus-remove-thread (id &optional dont-remove)
3838   "Remove the thread that has ID in it."
3839   (let (headers thread last-id)
3840     ;; First go up in this thread until we find the root.
3841     (setq last-id (gnus-root-id id)
3842           headers (message-flatten-list (gnus-id-to-thread last-id)))
3843     ;; We have now found the real root of this thread.  It might have
3844     ;; been gathered into some loose thread, so we have to search
3845     ;; through the threads to find the thread we wanted.
3846     (let ((threads gnus-newsgroup-threads)
3847           sub)
3848       (while threads
3849         (setq sub (car threads))
3850         (if (stringp (car sub))
3851             ;; This is a gathered thread, so we look at the roots
3852             ;; below it to find whether this article is in this
3853             ;; gathered root.
3854             (progn
3855               (setq sub (cdr sub))
3856               (while sub
3857                 (when (member (caar sub) headers)
3858                   (setq thread (car threads)
3859                         threads nil
3860                         sub nil))
3861                 (setq sub (cdr sub))))
3862           ;; It's an ordinary thread, so we check it.
3863           (when (eq (car sub) (car headers))
3864             (setq thread sub
3865                   threads nil)))
3866         (setq threads (cdr threads)))
3867       ;; If this article is in no thread, then it's a root.
3868       (if thread
3869           (unless dont-remove
3870             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3871         (setq thread (gnus-id-to-thread last-id)))
3872       (when thread
3873         (prog1
3874             thread                      ; We return this thread.
3875           (unless dont-remove
3876             (if (stringp (car thread))
3877                 (progn
3878                   ;; If we use dummy roots, then we have to remove the
3879                   ;; dummy root as well.
3880                   (when (eq gnus-summary-make-false-root 'dummy)
3881                     ;; We go to the dummy root by going to
3882                     ;; the first sub-"thread", and then one line up.
3883                     (gnus-summary-goto-article
3884                      (mail-header-number (caadr thread)))
3885                     (forward-line -1)
3886                     (gnus-delete-line)
3887                     (gnus-data-compute-positions))
3888                   (setq thread (cdr thread))
3889                   (while thread
3890                     (gnus-remove-thread-1 (car thread))
3891                     (setq thread (cdr thread))))
3892               (gnus-remove-thread-1 thread))))))))
3893
3894 (defun gnus-remove-thread-1 (thread)
3895   "Remove the thread THREAD recursively."
3896   (let ((number (mail-header-number (pop thread)))
3897         d)
3898     (setq thread (reverse thread))
3899     (while thread
3900       (gnus-remove-thread-1 (pop thread)))
3901     (when (setq d (gnus-data-find number))
3902       (goto-char (gnus-data-pos d))
3903       (gnus-summary-show-thread)
3904       (gnus-data-remove
3905        number
3906        (- (gnus-point-at-bol)
3907           (prog1
3908               (1+ (gnus-point-at-eol))
3909             (gnus-delete-line)))))))
3910
3911 (defun gnus-sort-threads-1 (threads func)
3912   (sort (mapcar (lambda (thread)
3913                   (cons (car thread)
3914                         (and (cdr thread)
3915                              (gnus-sort-threads-1 (cdr thread) func))))
3916                 threads) func))
3917
3918 (defun gnus-sort-threads (threads)
3919   "Sort THREADS."
3920   (if (not gnus-thread-sort-functions)
3921       threads
3922     (gnus-message 8 "Sorting threads...")
3923     (prog1
3924         (gnus-sort-threads-1
3925          threads
3926          (gnus-make-sort-function gnus-thread-sort-functions))
3927       (gnus-message 8 "Sorting threads...done"))))
3928
3929 (defun gnus-sort-articles (articles)
3930   "Sort ARTICLES."
3931   (when gnus-article-sort-functions
3932     (gnus-message 7 "Sorting articles...")
3933     (prog1
3934         (setq gnus-newsgroup-headers
3935               (sort articles (gnus-make-sort-function
3936                               gnus-article-sort-functions)))
3937       (gnus-message 7 "Sorting articles...done"))))
3938
3939 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3940 (defmacro gnus-thread-header (thread)
3941   "Return header of first article in THREAD.
3942 Note that THREAD must never, ever be anything else than a variable -
3943 using some other form will lead to serious barfage."
3944   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3945   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3946   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3947         (vector thread) 2))
3948
3949 (defsubst gnus-article-sort-by-number (h1 h2)
3950   "Sort articles by article number."
3951   (< (mail-header-number h1)
3952      (mail-header-number h2)))
3953
3954 (defun gnus-thread-sort-by-number (h1 h2)
3955   "Sort threads by root article number."
3956   (gnus-article-sort-by-number
3957    (gnus-thread-header h1) (gnus-thread-header h2)))
3958
3959 (defsubst gnus-article-sort-by-lines (h1 h2)
3960   "Sort articles by article Lines header."
3961   (< (mail-header-lines h1)
3962      (mail-header-lines h2)))
3963
3964 (defun gnus-thread-sort-by-lines (h1 h2)
3965   "Sort threads by root article Lines header."
3966   (gnus-article-sort-by-lines
3967    (gnus-thread-header h1) (gnus-thread-header h2)))
3968
3969 (defsubst gnus-article-sort-by-chars (h1 h2)
3970   "Sort articles by octet length."
3971   (< (mail-header-chars h1)
3972      (mail-header-chars h2)))
3973
3974 (defun gnus-thread-sort-by-chars (h1 h2)
3975   "Sort threads by root article octet length."
3976   (gnus-article-sort-by-chars
3977    (gnus-thread-header h1) (gnus-thread-header h2)))
3978
3979 (defsubst gnus-article-sort-by-author (h1 h2)
3980   "Sort articles by root author."
3981   (string-lessp
3982    (let ((addr (car (mime-entity-read-field h1 'From))))
3983      (or (std11-full-name-string addr)
3984          (std11-address-string addr)
3985          ""))
3986    (let ((addr (car (mime-entity-read-field h2 'From))))
3987      (or (std11-full-name-string addr)
3988          (std11-address-string addr)
3989          ""))
3990    ))
3991
3992 (defun gnus-thread-sort-by-author (h1 h2)
3993   "Sort threads by root author."
3994   (gnus-article-sort-by-author
3995    (gnus-thread-header h1)  (gnus-thread-header h2)))
3996
3997 (defsubst gnus-article-sort-by-subject (h1 h2)
3998   "Sort articles by root subject."
3999   (string-lessp
4000    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4001    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4002
4003 (defun gnus-thread-sort-by-subject (h1 h2)
4004   "Sort threads by root subject."
4005   (gnus-article-sort-by-subject
4006    (gnus-thread-header h1) (gnus-thread-header h2)))
4007
4008 (defsubst gnus-article-sort-by-date (h1 h2)
4009   "Sort articles by root article date."
4010   (time-less-p
4011    (gnus-date-get-time (mail-header-date h1))
4012    (gnus-date-get-time (mail-header-date h2))))
4013
4014 (defun gnus-thread-sort-by-date (h1 h2)
4015   "Sort threads by root article date."
4016   (gnus-article-sort-by-date
4017    (gnus-thread-header h1) (gnus-thread-header h2)))
4018
4019 (defsubst gnus-article-sort-by-score (h1 h2)
4020   "Sort articles by root article score.
4021 Unscored articles will be counted as having a score of zero."
4022   (> (or (cdr (assq (mail-header-number h1)
4023                     gnus-newsgroup-scored))
4024          gnus-summary-default-score 0)
4025      (or (cdr (assq (mail-header-number h2)
4026                     gnus-newsgroup-scored))
4027          gnus-summary-default-score 0)))
4028
4029 (defun gnus-thread-sort-by-score (h1 h2)
4030   "Sort threads by root article score."
4031   (gnus-article-sort-by-score
4032    (gnus-thread-header h1) (gnus-thread-header h2)))
4033
4034 (defun gnus-thread-sort-by-total-score (h1 h2)
4035   "Sort threads by the sum of all scores in the thread.
4036 Unscored articles will be counted as having a score of zero."
4037   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4038
4039 (defun gnus-thread-total-score (thread)
4040   ;; This function find the total score of THREAD.
4041   (cond ((null thread)
4042          0)
4043         ((consp thread)
4044          (if (stringp (car thread))
4045              (apply gnus-thread-score-function 0
4046                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4047            (gnus-thread-total-score-1 thread)))
4048         (t
4049          (gnus-thread-total-score-1 (list thread)))))
4050
4051 (defun gnus-thread-total-score-1 (root)
4052   ;; This function find the total score of the thread below ROOT.
4053   (setq root (car root))
4054   (apply gnus-thread-score-function
4055          (or (append
4056               (mapcar 'gnus-thread-total-score
4057                       (cdr (gnus-id-to-thread (mail-header-id root))))
4058               (when (> (mail-header-number root) 0)
4059                 (list (or (cdr (assq (mail-header-number root)
4060                                      gnus-newsgroup-scored))
4061                           gnus-summary-default-score 0))))
4062              (list gnus-summary-default-score)
4063              '(0))))
4064
4065 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4066 (defvar gnus-tmp-prev-subject nil)
4067 (defvar gnus-tmp-false-parent nil)
4068 (defvar gnus-tmp-root-expunged nil)
4069 (defvar gnus-tmp-dummy-line nil)
4070
4071 (eval-when-compile (defvar gnus-tmp-header))
4072 (defun gnus-extra-header (type &optional header)
4073   "Return the extra header of TYPE."
4074   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4075       ""))
4076
4077 (defun gnus-summary-prepare-threads (threads)
4078   "Prepare summary buffer from THREADS and indentation LEVEL.
4079 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4080 or a straight list of headers."
4081   (gnus-message 7 "Generating summary...")
4082
4083   (setq gnus-newsgroup-threads threads)
4084   (beginning-of-line)
4085
4086   (let ((gnus-tmp-level 0)
4087         (default-score (or gnus-summary-default-score 0))
4088         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4089         thread number subject stack state gnus-tmp-gathered beg-match
4090         new-roots gnus-tmp-new-adopts thread-end
4091         gnus-tmp-header gnus-tmp-unread
4092         gnus-tmp-replied gnus-tmp-subject-or-nil
4093         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4094         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4095         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4096
4097     (setq gnus-tmp-prev-subject nil)
4098
4099     (if (vectorp (car threads))
4100         ;; If this is a straight (sic) list of headers, then a
4101         ;; threaded summary display isn't required, so we just create
4102         ;; an unthreaded one.
4103         (gnus-summary-prepare-unthreaded threads)
4104
4105       ;; Do the threaded display.
4106
4107       (while (or threads stack gnus-tmp-new-adopts new-roots)
4108
4109         (if (and (= gnus-tmp-level 0)
4110                  (or (not stack)
4111                      (= (caar stack) 0))
4112                  (not gnus-tmp-false-parent)
4113                  (or gnus-tmp-new-adopts new-roots))
4114             (if gnus-tmp-new-adopts
4115                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4116                       thread (list (car gnus-tmp-new-adopts))
4117                       gnus-tmp-header (caar thread)
4118                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4119               (when new-roots
4120                 (setq thread (list (car new-roots))
4121                       gnus-tmp-header (caar thread)
4122                       new-roots (cdr new-roots))))
4123
4124           (if threads
4125               ;; If there are some threads, we do them before the
4126               ;; threads on the stack.
4127               (setq thread threads
4128                     gnus-tmp-header (caar thread))
4129             ;; There were no current threads, so we pop something off
4130             ;; the stack.
4131             (setq state (car stack)
4132                   gnus-tmp-level (car state)
4133                   thread (cdr state)
4134                   stack (cdr stack)
4135                   gnus-tmp-header (caar thread))))
4136
4137         (setq gnus-tmp-false-parent nil)
4138         (setq gnus-tmp-root-expunged nil)
4139         (setq thread-end nil)
4140
4141         (if (stringp gnus-tmp-header)
4142             ;; The header is a dummy root.
4143             (cond
4144              ((eq gnus-summary-make-false-root 'adopt)
4145               ;; We let the first article adopt the rest.
4146               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4147                                                (cddar thread)))
4148               (setq gnus-tmp-gathered
4149                     (nconc (mapcar
4150                             (lambda (h) (mail-header-number (car h)))
4151                             (cddar thread))
4152                            gnus-tmp-gathered))
4153               (setq thread (cons (list (caar thread)
4154                                        (cadar thread))
4155                                  (cdr thread)))
4156               (setq gnus-tmp-level -1
4157                     gnus-tmp-false-parent t))
4158              ((eq gnus-summary-make-false-root 'empty)
4159               ;; We print adopted articles with empty subject fields.
4160               (setq gnus-tmp-gathered
4161                     (nconc (mapcar
4162                             (lambda (h) (mail-header-number (car h)))
4163                             (cddar thread))
4164                            gnus-tmp-gathered))
4165               (setq gnus-tmp-level -1))
4166              ((eq gnus-summary-make-false-root 'dummy)
4167               ;; We remember that we probably want to output a dummy
4168               ;; root.
4169               (setq gnus-tmp-dummy-line gnus-tmp-header)
4170               (setq gnus-tmp-prev-subject gnus-tmp-header))
4171              (t
4172               ;; We do not make a root for the gathered
4173               ;; sub-threads at all.
4174               (setq gnus-tmp-level -1)))
4175
4176           (setq number (mail-header-number gnus-tmp-header)
4177                 subject (mail-header-subject gnus-tmp-header))
4178
4179           (cond
4180            ;; If the thread has changed subject, we might want to make
4181            ;; this subthread into a root.
4182            ((and (null gnus-thread-ignore-subject)
4183                  (not (zerop gnus-tmp-level))
4184                  gnus-tmp-prev-subject
4185                  (not (inline
4186                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4187             (setq new-roots (nconc new-roots (list (car thread)))
4188                   thread-end t
4189                   gnus-tmp-header nil))
4190            ;; If the article lies outside the current limit,
4191            ;; then we do not display it.
4192            ((not (memq number gnus-newsgroup-limit))
4193             (setq gnus-tmp-gathered
4194                   (nconc (mapcar
4195                           (lambda (h) (mail-header-number (car h)))
4196                           (cdar thread))
4197                          gnus-tmp-gathered))
4198             (setq gnus-tmp-new-adopts (if (cdar thread)
4199                                           (append gnus-tmp-new-adopts
4200                                                   (cdar thread))
4201                                         gnus-tmp-new-adopts)
4202                   thread-end t
4203                   gnus-tmp-header nil)
4204             (when (zerop gnus-tmp-level)
4205               (setq gnus-tmp-root-expunged t)))
4206            ;; Perhaps this article is to be marked as read?
4207            ((and gnus-summary-mark-below
4208                  (< (or (cdr (assq number gnus-newsgroup-scored))
4209                         default-score)
4210                     gnus-summary-mark-below)
4211                  ;; Don't touch sparse articles.
4212                  (not (gnus-summary-article-sparse-p number))
4213                  (not (gnus-summary-article-ancient-p number)))
4214             (setq gnus-newsgroup-unreads
4215                   (delq number gnus-newsgroup-unreads))
4216             (if gnus-newsgroup-auto-expire
4217                 (push number gnus-newsgroup-expirable)
4218               (push (cons number gnus-low-score-mark)
4219                     gnus-newsgroup-reads))))
4220
4221           (when gnus-tmp-header
4222             ;; We may have an old dummy line to output before this
4223             ;; article.
4224             (when (and gnus-tmp-dummy-line
4225                        (gnus-subject-equal
4226                         gnus-tmp-dummy-line
4227                         (mail-header-subject gnus-tmp-header)))
4228               (gnus-summary-insert-dummy-line
4229                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4230               (setq gnus-tmp-dummy-line nil))
4231
4232             ;; Compute the mark.
4233             (setq gnus-tmp-unread (gnus-article-mark number))
4234
4235             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4236                                   gnus-tmp-header gnus-tmp-level)
4237                   gnus-newsgroup-data)
4238
4239             ;; Actually insert the line.
4240             (setq
4241              gnus-tmp-subject-or-nil
4242              (cond
4243               ((and gnus-thread-ignore-subject
4244                     gnus-tmp-prev-subject
4245                     (not (inline (gnus-subject-equal
4246                                   gnus-tmp-prev-subject subject))))
4247                subject)
4248               ((zerop gnus-tmp-level)
4249                (if (and (eq gnus-summary-make-false-root 'empty)
4250                         (memq number gnus-tmp-gathered)
4251                         gnus-tmp-prev-subject
4252                         (inline (gnus-subject-equal
4253                                  gnus-tmp-prev-subject subject)))
4254                    gnus-summary-same-subject
4255                  subject))
4256               (t gnus-summary-same-subject)))
4257             (if (and (eq gnus-summary-make-false-root 'adopt)
4258                      (= gnus-tmp-level 1)
4259                      (memq number gnus-tmp-gathered))
4260                 (setq gnus-tmp-opening-bracket ?\<
4261                       gnus-tmp-closing-bracket ?\>)
4262               (setq gnus-tmp-opening-bracket ?\[
4263                     gnus-tmp-closing-bracket ?\]))
4264             (setq
4265              gnus-tmp-indentation
4266              (aref gnus-thread-indent-array gnus-tmp-level)
4267              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4268              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4269                                 gnus-summary-default-score 0)
4270              gnus-tmp-score-char
4271              (if (or (null gnus-summary-default-score)
4272                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4273                          gnus-summary-zcore-fuzz))
4274                  ?\ ;;;Whitespace
4275                (if (< gnus-tmp-score gnus-summary-default-score)
4276                    gnus-score-below-mark gnus-score-over-mark))
4277              gnus-tmp-replied
4278              (cond ((memq number gnus-newsgroup-processable)
4279                     gnus-process-mark)
4280                    ((memq number gnus-newsgroup-cached)
4281                     gnus-cached-mark)
4282                    ((memq number gnus-newsgroup-replied)
4283                     gnus-replied-mark)
4284                    ((memq number gnus-newsgroup-forwarded)
4285                     gnus-forwarded-mark)
4286                    ((memq number gnus-newsgroup-saved)
4287                     gnus-saved-mark)
4288                    (t gnus-no-mark))
4289              gnus-tmp-from (mail-header-from gnus-tmp-header)
4290              gnus-tmp-name
4291              (cond
4292               ((string-match "<[^>]+> *$" gnus-tmp-from)
4293                (setq beg-match (match-beginning 0))
4294                (or (and (string-match "^\".+\"" gnus-tmp-from)
4295                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4296                    (substring gnus-tmp-from 0 beg-match)))
4297               ((string-match "(.+)" gnus-tmp-from)
4298                (substring gnus-tmp-from
4299                           (1+ (match-beginning 0)) (1- (match-end 0))))
4300               (t gnus-tmp-from)))
4301             (when (string= gnus-tmp-name "")
4302               (setq gnus-tmp-name gnus-tmp-from))
4303             (unless (numberp gnus-tmp-lines)
4304               (setq gnus-tmp-lines -1))
4305             (when (= gnus-tmp-lines -1)
4306               (setq gnus-tmp-lines "?"))
4307             (gnus-put-text-property
4308              (point)
4309              (progn (eval gnus-summary-line-format-spec) (point))
4310              'gnus-number number)
4311             (when gnus-visual-p
4312               (forward-line -1)
4313               (gnus-run-hooks 'gnus-summary-update-hook)
4314               (forward-line 1))
4315
4316             (setq gnus-tmp-prev-subject subject)))
4317
4318         (when (nth 1 thread)
4319           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4320         (incf gnus-tmp-level)
4321         (setq threads (if thread-end nil (cdar thread)))
4322         (unless threads
4323           (setq gnus-tmp-level 0)))))
4324   (gnus-message 7 "Generating summary...done"))
4325
4326 (defun gnus-summary-prepare-unthreaded (headers)
4327   "Generate an unthreaded summary buffer based on HEADERS."
4328   (let (header number mark)
4329
4330     (beginning-of-line)
4331
4332     (while headers
4333       ;; We may have to root out some bad articles...
4334       (when (memq (setq number (mail-header-number
4335                                 (setq header (pop headers))))
4336                   gnus-newsgroup-limit)
4337         ;; Mark article as read when it has a low score.
4338         (when (and gnus-summary-mark-below
4339                    (< (or (cdr (assq number gnus-newsgroup-scored))
4340                           gnus-summary-default-score 0)
4341                       gnus-summary-mark-below)
4342                    (not (gnus-summary-article-ancient-p number)))
4343           (setq gnus-newsgroup-unreads
4344                 (delq number gnus-newsgroup-unreads))
4345           (if gnus-newsgroup-auto-expire
4346               (push number gnus-newsgroup-expirable)
4347             (push (cons number gnus-low-score-mark)
4348                   gnus-newsgroup-reads)))
4349
4350         (setq mark (gnus-article-mark number))
4351         (push (gnus-data-make number mark (1+ (point)) header 0)
4352               gnus-newsgroup-data)
4353         (gnus-summary-insert-line
4354          header 0 number
4355          mark (memq number gnus-newsgroup-replied)
4356          (memq number gnus-newsgroup-expirable)
4357          (mail-header-subject header) nil
4358          (cdr (assq number gnus-newsgroup-scored))
4359          (memq number gnus-newsgroup-processable))))))
4360
4361 (defun gnus-summary-remove-list-identifiers ()
4362   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4363   (let ((regexp (if (consp gnus-list-identifiers)
4364                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4365                   gnus-list-identifiers))
4366         changed subject)
4367     (when regexp
4368       (dolist (header gnus-newsgroup-headers)
4369         (setq subject (mail-header-subject header)
4370               changed nil)
4371         (while (string-match
4372                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4373                 subject)
4374           (setq subject
4375                 (concat (substring subject 0 (match-beginning 2))
4376                         (substring subject (match-end 0)))
4377                 changed t))
4378         (when (and changed
4379                    (string-match
4380                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4381           (setq subject
4382                 (concat (substring subject 0 (match-beginning 1))
4383                         (substring subject (match-end 1)))))
4384         (when changed
4385           (mail-header-set-subject header subject))))))
4386
4387 (defun gnus-fetch-headers (articles)
4388   "Fetch headers of ARTICLES."
4389   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4390     (gnus-message 5 "Fetching headers for %s..." name)
4391     (prog1
4392         (if (eq 'nov
4393                 (setq gnus-headers-retrieved-by
4394                       (gnus-retrieve-headers
4395                        articles gnus-newsgroup-name
4396                        ;; We might want to fetch old headers, but
4397                        ;; not if there is only 1 article.
4398                        (and (or (and
4399                                  (not (eq gnus-fetch-old-headers 'some))
4400                                  (not (numberp gnus-fetch-old-headers)))
4401                                 (> (length articles) 1))
4402                             gnus-fetch-old-headers))))
4403             (gnus-get-newsgroup-headers-xover
4404              articles nil nil gnus-newsgroup-name t)
4405           (gnus-get-newsgroup-headers))
4406       (gnus-message 5 "Fetching headers for %s...done" name))))
4407
4408 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4409   "Select newsgroup GROUP.
4410 If READ-ALL is non-nil, all articles in the group are selected.
4411 If SELECT-ARTICLES, only select those articles from GROUP."
4412   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4413          ;;!!! Dirty hack; should be removed.
4414          (gnus-summary-ignore-duplicates
4415           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4416               t
4417             gnus-summary-ignore-duplicates))
4418          (info (nth 2 entry))
4419          articles fetched-articles cached)
4420
4421     (unless (gnus-check-server
4422              (setq gnus-current-select-method
4423                    (gnus-find-method-for-group group)))
4424       (error "Couldn't open server"))
4425
4426     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4427         (gnus-activate-group group)     ; Or we can activate it...
4428         (progn                          ; Or we bug out.
4429           (when (equal major-mode 'gnus-summary-mode)
4430             (kill-buffer (current-buffer)))
4431           (error "Couldn't activate group %s: %s"
4432                  group (gnus-status-message group))))
4433
4434     (unless (gnus-request-group group t)
4435       (when (equal major-mode 'gnus-summary-mode)
4436         (kill-buffer (current-buffer)))
4437       (error "Couldn't request group %s: %s"
4438              group (gnus-status-message group)))
4439
4440     (setq gnus-newsgroup-name group)
4441     (setq gnus-newsgroup-unselected nil)
4442     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4443     (gnus-summary-setup-default-charset)
4444
4445     ;; Adjust and set lists of article marks.
4446     (when info
4447       (gnus-adjust-marked-articles info))
4448
4449     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4450     (when (gnus-virtual-group-p group)
4451       (setq cached gnus-newsgroup-cached))
4452
4453     (setq gnus-newsgroup-unreads
4454           (gnus-set-difference
4455            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4456            gnus-newsgroup-dormant))
4457
4458     (setq gnus-newsgroup-processable nil)
4459
4460     (gnus-update-read-articles group gnus-newsgroup-unreads)
4461
4462     (if (setq articles select-articles)
4463         (setq gnus-newsgroup-unselected
4464               (gnus-sorted-intersection
4465                gnus-newsgroup-unreads
4466                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4467       (setq articles (gnus-articles-to-read group read-all)))
4468
4469     (cond
4470      ((null articles)
4471       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4472       'quit)
4473      ((eq articles 0) nil)
4474      (t
4475       ;; Init the dependencies hash table.
4476       (setq gnus-newsgroup-dependencies
4477             (gnus-make-hashtable (length articles)))
4478       (gnus-set-global-variables)
4479       ;; Retrieve the headers and read them in.
4480       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4481
4482       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4483       (when cached
4484         (setq gnus-newsgroup-cached cached))
4485
4486       ;; Suppress duplicates?
4487       (when gnus-suppress-duplicates
4488         (gnus-dup-suppress-articles))
4489
4490       ;; Set the initial limit.
4491       (setq gnus-newsgroup-limit (copy-sequence articles))
4492       ;; Remove canceled articles from the list of unread articles.
4493       (setq gnus-newsgroup-unreads
4494             (gnus-set-sorted-intersection
4495              gnus-newsgroup-unreads
4496              (setq fetched-articles
4497                    (mapcar (lambda (headers) (mail-header-number headers))
4498                            gnus-newsgroup-headers))))
4499       ;; Removed marked articles that do not exist.
4500       (gnus-update-missing-marks
4501        (gnus-sorted-complement fetched-articles articles))
4502       ;; We might want to build some more threads first.
4503       (when (and gnus-fetch-old-headers
4504                  (eq gnus-headers-retrieved-by 'nov))
4505         (if (eq gnus-fetch-old-headers 'invisible)
4506             (gnus-build-all-threads)
4507           (gnus-build-old-threads)))
4508       ;; Let the Gnus agent mark articles as read.
4509       (when gnus-agent
4510         (gnus-agent-get-undownloaded-list))
4511       ;; Remove list identifiers from subject
4512       (when gnus-list-identifiers
4513         (gnus-summary-remove-list-identifiers))
4514       ;; Check whether auto-expire is to be done in this group.
4515       (setq gnus-newsgroup-auto-expire
4516             (gnus-group-auto-expirable-p group))
4517       ;; Set up the article buffer now, if necessary.
4518       (unless gnus-single-article-buffer
4519         (gnus-article-setup-buffer))
4520       ;; First and last article in this newsgroup.
4521       (when gnus-newsgroup-headers
4522         (setq gnus-newsgroup-begin
4523               (mail-header-number (car gnus-newsgroup-headers))
4524               gnus-newsgroup-end
4525               (mail-header-number
4526                (gnus-last-element gnus-newsgroup-headers))))
4527       ;; GROUP is successfully selected.
4528       (or gnus-newsgroup-headers t)))))
4529
4530 (defun gnus-articles-to-read (group &optional read-all)
4531   "Find out what articles the user wants to read."
4532   (let* ((articles
4533           ;; Select all articles if `read-all' is non-nil, or if there
4534           ;; are no unread articles.
4535           (if (or read-all
4536                   (and (zerop (length gnus-newsgroup-marked))
4537                        (zerop (length gnus-newsgroup-unreads)))
4538                   (eq (gnus-group-find-parameter group 'display)
4539                       'all))
4540               (or
4541                (gnus-uncompress-range (gnus-active group))
4542                (gnus-cache-articles-in-group group))
4543             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4544                           (copy-sequence gnus-newsgroup-unreads))
4545                   '<)))
4546          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4547          (scored (length scored-list))
4548          (number (length articles))
4549          (marked (+ (length gnus-newsgroup-marked)
4550                     (length gnus-newsgroup-dormant)))
4551          (select
4552           (cond
4553            ((numberp read-all)
4554             read-all)
4555            (t
4556             (condition-case ()
4557                 (cond
4558                  ((and (or (<= scored marked) (= scored number))
4559                        (natnump gnus-large-newsgroup)
4560                        (> number gnus-large-newsgroup))
4561                   (let* ((cursor-in-echo-area nil)
4562                          (input (read-from-minibuffer
4563                                  (format
4564                                   "How many articles from %s (max %d): "
4565                                   (gnus-limit-string gnus-newsgroup-name 35)
4566                                   number)
4567                                  (cons (number-to-string gnus-large-newsgroup)
4568                                        0))))
4569                     (if (string-match "^[ \t]*$" input)
4570                         number
4571                       input)))
4572                  ((and (> scored marked) (< scored number)
4573                        (> (- scored number) 20))
4574                   (let ((input
4575                          (read-string
4576                           (format "%s %s (%d scored, %d total): "
4577                                   "How many articles from"
4578                                   group scored number))))
4579                     (if (string-match "^[ \t]*$" input)
4580                         number input)))
4581                  (t number))
4582               (quit
4583                (message "Quit getting the articles to read")
4584                nil))))))
4585     (setq select (if (stringp select) (string-to-number select) select))
4586     (if (or (null select) (zerop select))
4587         select
4588       (if (and (not (zerop scored)) (<= (abs select) scored))
4589           (progn
4590             (setq articles (sort scored-list '<))
4591             (setq number (length articles)))
4592         (setq articles (copy-sequence articles)))
4593
4594       (when (< (abs select) number)
4595         (if (< select 0)
4596             ;; Select the N oldest articles.
4597             (setcdr (nthcdr (1- (abs select)) articles) nil)
4598           ;; Select the N most recent articles.
4599           (setq articles (nthcdr (- number select) articles))))
4600       (setq gnus-newsgroup-unselected
4601             (gnus-sorted-intersection
4602              gnus-newsgroup-unreads
4603              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4604       (when gnus-alter-articles-to-read-function
4605         (setq gnus-newsgroup-unreads
4606               (sort
4607                (funcall gnus-alter-articles-to-read-function
4608                         gnus-newsgroup-name gnus-newsgroup-unreads)
4609                '<)))
4610       articles)))
4611
4612 (defun gnus-killed-articles (killed articles)
4613   (let (out)
4614     (while articles
4615       (when (inline (gnus-member-of-range (car articles) killed))
4616         (push (car articles) out))
4617       (setq articles (cdr articles)))
4618     out))
4619
4620 (defun gnus-uncompress-marks (marks)
4621   "Uncompress the mark ranges in MARKS."
4622   (let ((uncompressed '(score bookmark))
4623         out)
4624     (while marks
4625       (if (memq (caar marks) uncompressed)
4626           (push (car marks) out)
4627         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4628       (setq marks (cdr marks)))
4629     out))
4630
4631 (defun gnus-adjust-marked-articles (info)
4632   "Set all article lists and remove all marks that are no longer valid."
4633   (let* ((marked-lists (gnus-info-marks info))
4634          (active (gnus-active (gnus-info-group info)))
4635          (min (car active))
4636          (max (cdr active))
4637          (types gnus-article-mark-lists)
4638          (uncompressed '(score bookmark killed))
4639          marks var articles article mark)
4640
4641     (while marked-lists
4642       (setq marks (pop marked-lists))
4643       (set (setq var (intern (format "gnus-newsgroup-%s"
4644                                      (car (rassq (setq mark (car marks))
4645                                                  types)))))
4646            (if (memq (car marks) uncompressed) (cdr marks)
4647              (gnus-uncompress-range (cdr marks))))
4648
4649       (setq articles (symbol-value var))
4650
4651       ;; All articles have to be subsets of the active articles.
4652       (cond
4653        ;; Adjust "simple" lists.
4654        ((memq mark '(tick dormant expire reply save))
4655         (while articles
4656           (when (or (< (setq article (pop articles)) min) (> article max))
4657             (set var (delq article (symbol-value var))))))
4658        ;; Adjust assocs.
4659        ((memq mark uncompressed)
4660         (when (not (listp (cdr (symbol-value var))))
4661           (set var (list (symbol-value var))))
4662         (when (not (listp (cdr articles)))
4663           (setq articles (list articles)))
4664         (while articles
4665           (when (or (not (consp (setq article (pop articles))))
4666                     (< (car article) min)
4667                     (> (car article) max))
4668             (set var (delq article (symbol-value var))))))))))
4669
4670 (defun gnus-update-missing-marks (missing)
4671   "Go through the list of MISSING articles and remove them from the mark lists."
4672   (when missing
4673     (let ((types gnus-article-mark-lists)
4674           var m)
4675       ;; Go through all types.
4676       (while types
4677         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4678         (when (symbol-value var)
4679           ;; This list has articles.  So we delete all missing articles
4680           ;; from it.
4681           (setq m missing)
4682           (while m
4683             (set var (delq (pop m) (symbol-value var)))))))))
4684
4685 (defun gnus-update-marks ()
4686   "Enter the various lists of marked articles into the newsgroup info list."
4687   (let ((types gnus-article-mark-lists)
4688         (info (gnus-get-info gnus-newsgroup-name))
4689         (uncompressed '(score bookmark killed))
4690         type list newmarked symbol delta-marks)
4691     (when info
4692       ;; Add all marks lists to the list of marks lists.
4693       (while (setq type (pop types))
4694         (setq list (symbol-value
4695                     (setq symbol
4696                           (intern (format "gnus-newsgroup-%s"
4697                                           (car type))))))
4698
4699         (when list
4700           ;; Get rid of the entries of the articles that have the
4701           ;; default score.
4702           (when (and (eq (cdr type) 'score)
4703                      gnus-save-score
4704                      list)
4705             (let* ((arts list)
4706                    (prev (cons nil list))
4707                    (all prev))
4708               (while arts
4709                 (if (or (not (consp (car arts)))
4710                         (= (cdar arts) gnus-summary-default-score))
4711                     (setcdr prev (cdr arts))
4712                   (setq prev arts))
4713                 (setq arts (cdr arts)))
4714               (setq list (cdr all)))))
4715
4716         (unless (memq (cdr type) uncompressed)
4717           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4718
4719         (when (gnus-check-backend-function
4720                'request-set-mark gnus-newsgroup-name)
4721           ;; propagate flags to server, with the following exceptions:
4722           ;; uncompressed:s are not proper flags (they are cons cells)
4723           ;; cache is a internal gnus flag
4724           ;; download are local to one gnus installation (well)
4725           ;; unsend are for nndraft groups only
4726           ;; xxx: generality of this?  this suits nnimap anyway
4727           (unless (memq (cdr type) (append '(cache download unsend)
4728                                            uncompressed))
4729             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4730                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4731                    (add (gnus-remove-from-range
4732                          (gnus-copy-sequence list) old)))
4733               (when add
4734                 (push (list add 'add (list (cdr type))) delta-marks))
4735               (when del
4736                 (push (list del 'del (list (cdr type))) delta-marks)))))
4737
4738         (when list
4739           (push (cons (cdr type) list) newmarked)))
4740
4741       (when delta-marks
4742         (unless (gnus-check-group gnus-newsgroup-name)
4743           (error "Can't open server for %s" gnus-newsgroup-name))
4744         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4745
4746       ;; Enter these new marks into the info of the group.
4747       (if (nthcdr 3 info)
4748           (setcar (nthcdr 3 info) newmarked)
4749         ;; Add the marks lists to the end of the info.
4750         (when newmarked
4751           (setcdr (nthcdr 2 info) (list newmarked))))
4752
4753       ;; Cut off the end of the info if there's nothing else there.
4754       (let ((i 5))
4755         (while (and (> i 2)
4756                     (not (nth i info)))
4757           (when (nthcdr (decf i) info)
4758             (setcdr (nthcdr i info) nil)))))))
4759
4760 (defun gnus-set-mode-line (where)
4761   "Set the mode line of the article or summary buffers.
4762 If WHERE is `summary', the summary mode line format will be used."
4763   ;; Is this mode line one we keep updated?
4764   (when (and (memq where gnus-updated-mode-lines)
4765              (symbol-value
4766               (intern (format "gnus-%s-mode-line-format-spec" where))))
4767     (let (mode-string)
4768       (save-excursion
4769         ;; We evaluate this in the summary buffer since these
4770         ;; variables are buffer-local to that buffer.
4771         (set-buffer gnus-summary-buffer)
4772         ;; We bind all these variables that are used in the `eval' form
4773         ;; below.
4774         (let* ((mformat (symbol-value
4775                          (intern
4776                           (format "gnus-%s-mode-line-format-spec" where))))
4777                (gnus-tmp-group-name (gnus-group-decoded-name 
4778                                      gnus-newsgroup-name))
4779                (gnus-tmp-article-number (or gnus-current-article 0))
4780                (gnus-tmp-unread gnus-newsgroup-unreads)
4781                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4782                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4783                (gnus-tmp-unread-and-unselected
4784                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4785                             (zerop gnus-tmp-unselected))
4786                        "")
4787                       ((zerop gnus-tmp-unselected)
4788                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4789                       (t (format "{%d(+%d) more}"
4790                                  gnus-tmp-unread-and-unticked
4791                                  gnus-tmp-unselected))))
4792                (gnus-tmp-subject
4793                 (if (and gnus-current-headers
4794                          (vectorp gnus-current-headers))
4795                     (gnus-mode-string-quote
4796                      (mail-header-subject gnus-current-headers))
4797                   ""))
4798                bufname-length max-len
4799                gnus-tmp-header);; passed as argument to any user-format-funcs
4800           (setq mode-string (eval mformat))
4801           (setq bufname-length (if (string-match "%b" mode-string)
4802                                    (- (length
4803                                        (buffer-name
4804                                         (if (eq where 'summary)
4805                                             nil
4806                                           (get-buffer gnus-article-buffer))))
4807                                       2)
4808                                  0))
4809           (setq max-len (max 4 (if gnus-mode-non-string-length
4810                                    (- (window-width)
4811                                       gnus-mode-non-string-length
4812                                       bufname-length)
4813                                  (length mode-string))))
4814           ;; We might have to chop a bit of the string off...
4815           (when (> (length mode-string) max-len)
4816             (setq mode-string
4817                   (concat (gnus-truncate-string mode-string (- max-len 3))
4818                           "...")))
4819           ;; Pad the mode string a bit.
4820           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4821       ;; Update the mode line.
4822       (setq mode-line-buffer-identification
4823             (gnus-mode-line-buffer-identification (list mode-string)))
4824       (set-buffer-modified-p t))))
4825
4826 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4827   "Go through the HEADERS list and add all Xrefs to a hash table.
4828 The resulting hash table is returned, or nil if no Xrefs were found."
4829   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4830          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4831          (xref-hashtb (gnus-make-hashtable))
4832          start group entry number xrefs header)
4833     (while headers
4834       (setq header (pop headers))
4835       (when (and (setq xrefs (mail-header-xref header))
4836                  (not (memq (setq number (mail-header-number header))
4837                             unreads)))
4838         (setq start 0)
4839         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4840           (setq start (match-end 0))
4841           (setq group (if prefix
4842                           (concat prefix (substring xrefs (match-beginning 1)
4843                                                     (match-end 1)))
4844                         (substring xrefs (match-beginning 1) (match-end 1))))
4845           (setq number
4846                 (string-to-int (substring xrefs (match-beginning 2)
4847                                           (match-end 2))))
4848           (if (setq entry (gnus-gethash group xref-hashtb))
4849               (setcdr entry (cons number (cdr entry)))
4850             (gnus-sethash group (cons number nil) xref-hashtb)))))
4851     (and start xref-hashtb)))
4852
4853 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4854   "Look through all the headers and mark the Xrefs as read."
4855   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4856         name entry info xref-hashtb idlist method nth4)
4857     (save-excursion
4858       (set-buffer gnus-group-buffer)
4859       (when (setq xref-hashtb
4860                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4861         (mapatoms
4862          (lambda (group)
4863            (unless (string= from-newsgroup (setq name (symbol-name group)))
4864              (setq idlist (symbol-value group))
4865              ;; Dead groups are not updated.
4866              (and (prog1
4867                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4868                             info (nth 2 entry))
4869                     (when (stringp (setq nth4 (gnus-info-method info)))
4870                       (setq nth4 (gnus-server-to-method nth4))))
4871                   ;; Only do the xrefs if the group has the same
4872                   ;; select method as the group we have just read.
4873                   (or (gnus-methods-equal-p
4874                        nth4 (gnus-find-method-for-group from-newsgroup))
4875                       virtual
4876                       (equal nth4 (setq method (gnus-find-method-for-group
4877                                                 from-newsgroup)))
4878                       (and (equal (car nth4) (car method))
4879                            (equal (nth 1 nth4) (nth 1 method))))
4880                   gnus-use-cross-reference
4881                   (or (not (eq gnus-use-cross-reference t))
4882                       virtual
4883                       ;; Only do cross-references on subscribed
4884                       ;; groups, if that is what is wanted.
4885                       (<= (gnus-info-level info) gnus-level-subscribed))
4886                   (gnus-group-make-articles-read name idlist))))
4887          xref-hashtb)))))
4888
4889 (defun gnus-compute-read-articles (group articles)
4890   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4891          (info (nth 2 entry))
4892          (active (gnus-active group))
4893          ninfo)
4894     (when entry
4895       ;; First peel off all invalid article numbers.
4896       (when active
4897         (let ((ids articles)
4898               id first)
4899           (while (setq id (pop ids))
4900             (when (and first (> id (cdr active)))
4901               ;; We'll end up in this situation in one particular
4902               ;; obscure situation.  If you re-scan a group and get
4903               ;; a new article that is cross-posted to a different
4904               ;; group that has not been re-scanned, you might get
4905               ;; crossposted article that has a higher number than
4906               ;; Gnus believes possible.  So we re-activate this
4907               ;; group as well.  This might mean doing the
4908               ;; crossposting thingy will *increase* the number
4909               ;; of articles in some groups.  Tsk, tsk.
4910               (setq active (or (gnus-activate-group group) active)))
4911             (when (or (> id (cdr active))
4912                       (< id (car active)))
4913               (setq articles (delq id articles))))))
4914       ;; If the read list is nil, we init it.
4915       (if (and active
4916                (null (gnus-info-read info))
4917                (> (car active) 1))
4918           (setq ninfo (cons 1 (1- (car active))))
4919         (setq ninfo (gnus-info-read info)))
4920       ;; Then we add the read articles to the range.
4921       (gnus-add-to-range
4922        ninfo (setq articles (sort articles '<))))))
4923
4924 (defun gnus-group-make-articles-read (group articles)
4925   "Update the info of GROUP to say that ARTICLES are read."
4926   (let* ((num 0)
4927          (entry (gnus-gethash group gnus-newsrc-hashtb))
4928          (info (nth 2 entry))
4929          (active (gnus-active group))
4930          range)
4931     (when entry
4932       (setq range (gnus-compute-read-articles group articles))
4933       (save-excursion
4934         (set-buffer gnus-group-buffer)
4935         (gnus-undo-register
4936           `(progn
4937              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4938              (gnus-info-set-read ',info ',(gnus-info-read info))
4939              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4940              (gnus-group-update-group ,group t))))
4941       ;; Add the read articles to the range.
4942       (gnus-info-set-read info range)
4943       ;; Then we have to re-compute how many unread
4944       ;; articles there are in this group.
4945       (when active
4946         (cond
4947          ((not range)
4948           (setq num (- (1+ (cdr active)) (car active))))
4949          ((not (listp (cdr range)))
4950           (setq num (- (cdr active) (- (1+ (cdr range))
4951                                        (car range)))))
4952          (t
4953           (while range
4954             (if (numberp (car range))
4955                 (setq num (1+ num))
4956               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4957             (setq range (cdr range)))
4958           (setq num (- (cdr active) num))))
4959         ;; Update the number of unread articles.
4960         (setcar entry num)
4961         ;; Update the group buffer.
4962         (gnus-group-update-group group t)))))
4963
4964 (defvar gnus-newsgroup-none-id 0)
4965
4966 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4967   (let ((cur nntp-server-buffer)
4968         (dependencies
4969          (or dependencies
4970              (save-excursion (set-buffer gnus-summary-buffer)
4971                              gnus-newsgroup-dependencies)))
4972         headers id end ref
4973         (mail-parse-charset gnus-newsgroup-charset)
4974         (mail-parse-ignored-charsets
4975          (save-excursion (condition-case nil
4976                              (set-buffer gnus-summary-buffer)
4977                            (error))
4978                          gnus-newsgroup-ignored-charsets)))
4979     (save-excursion
4980       (set-buffer nntp-server-buffer)
4981       ;; Translate all TAB characters into SPACE characters.
4982       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4983       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4984       (gnus-run-hooks 'gnus-parse-headers-hook)
4985       (let ((case-fold-search t)
4986             in-reply-to header p lines chars ctype)
4987         (goto-char (point-min))
4988         ;; Search to the beginning of the next header.  Error messages
4989         ;; do not begin with 2 or 3.
4990         (while (re-search-forward "^[23][0-9]+ " nil t)
4991           (setq id nil
4992                 ref nil)
4993           ;; This implementation of this function, with nine
4994           ;; search-forwards instead of the one re-search-forward and
4995           ;; a case (which basically was the old function) is actually
4996           ;; about twice as fast, even though it looks messier.  You
4997           ;; can't have everything, I guess.  Speed and elegance
4998           ;; doesn't always go hand in hand.
4999           (setq
5000            header
5001            (make-full-mail-header
5002             ;; Number.
5003             (prog1
5004                 (read cur)
5005               (end-of-line)
5006               (setq p (point))
5007               (narrow-to-region (point)
5008                                 (or (and (search-forward "\n.\n" nil t)
5009                                          (- (point) 2))
5010                                     (point))))
5011             ;; Subject.
5012             (progn
5013               (goto-char p)
5014               (if (search-forward "\nsubject: " nil t)
5015                   (nnheader-header-value)
5016                 "(none)"))
5017             ;; From.
5018             (progn
5019               (goto-char p)
5020               (if (or (search-forward "\nfrom: " nil t)
5021                       (search-forward "\nfrom:" nil t))
5022                   (nnheader-header-value)
5023                 "(nobody)"))
5024             ;; Date.
5025             (progn
5026               (goto-char p)
5027               (if (search-forward "\ndate: " nil t)
5028                   (nnheader-header-value)
5029                 ""))
5030             ;; Message-ID.
5031             (progn
5032               (goto-char p)
5033               (setq id (if (re-search-forward
5034                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5035                            ;; We do it this way to make sure the Message-ID
5036                            ;; is (somewhat) syntactically valid.
5037                            (buffer-substring (match-beginning 1)
5038                                              (match-end 1))
5039                          ;; If there was no message-id, we just fake one
5040                          ;; to make subsequent routines simpler.
5041                          (nnheader-generate-fake-message-id))))
5042             ;; References.
5043             (progn
5044               (goto-char p)
5045               (if (search-forward "\nreferences: " nil t)
5046                   (progn
5047                     (setq end (point))
5048                     (prog1
5049                         (nnheader-header-value)
5050                       (setq ref
5051                             (buffer-substring
5052                              (progn
5053                                ;; (end-of-line)
5054                                (search-backward ">" end t)
5055                                (1+ (point)))
5056                              (progn
5057                                (search-backward "<" end t)
5058                                (point))))))
5059                 ;; Get the references from the in-reply-to header if there
5060                 ;; were no references and the in-reply-to header looks
5061                 ;; promising.
5062                 (if (and (search-forward "\nin-reply-to: " nil t)
5063                          (setq in-reply-to (nnheader-header-value))
5064                          (string-match "<[^>]+>" in-reply-to))
5065                     (let (ref2)
5066                       (setq ref (substring in-reply-to (match-beginning 0)
5067                                            (match-end 0)))
5068                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5069                         (setq ref2 (substring in-reply-to (match-beginning 0)
5070                                               (match-end 0)))
5071                         (when (> (length ref2) (length ref))
5072                           (setq ref ref2)))
5073                       ref)
5074                   (setq ref nil))))
5075             ;; Chars.
5076             (progn
5077               (goto-char p)
5078               (if (search-forward "\nchars: " nil t)
5079                   (if (numberp (setq chars (ignore-errors (read cur))))
5080                       chars -1)
5081                 -1))
5082             ;; Lines.
5083             (progn
5084               (goto-char p)
5085               (if (search-forward "\nlines: " nil t)
5086                   (if (numberp (setq lines (ignore-errors (read cur))))
5087                       lines -1)
5088                 -1))
5089             ;; Xref.
5090             (progn
5091               (goto-char p)
5092               (and (search-forward "\nxref: " nil t)
5093                    (nnheader-header-value)))
5094             ;; Extra.
5095             (when gnus-extra-headers
5096               (let ((extra gnus-extra-headers)
5097                     out)
5098                 (while extra
5099                   (goto-char p)
5100                   (when (search-forward
5101                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5102                     (push (cons (car extra) (nnheader-header-value)) out))
5103                   (pop extra))
5104                 out))))
5105           (goto-char p)
5106           (if (and (search-forward "\ncontent-type: " nil t)
5107                    (setq ctype (nnheader-header-value)))
5108               (mime-entity-set-content-type-internal
5109                header (mime-parse-Content-Type ctype)))
5110           (when (equal id ref)
5111             (setq ref nil))
5112
5113           (when gnus-alter-header-function
5114             (funcall gnus-alter-header-function header)
5115             (setq id (mail-header-id header)
5116                   ref (gnus-parent-id (mail-header-references header))))
5117
5118           (when (setq header
5119                       (gnus-dependencies-add-header
5120                        header dependencies force-new))
5121             (push header headers))
5122           (goto-char (point-max))
5123           (widen))
5124         (nreverse headers)))))
5125
5126 ;; Goes through the xover lines and returns a list of vectors
5127 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5128                                                   force-new dependencies
5129                                                   group also-fetch-heads)
5130   "Parse the news overview data in the server buffer.
5131 Return a list of headers that match SEQUENCE (see
5132 `nntp-retrieve-headers')."
5133   ;; Get the Xref when the users reads the articles since most/some
5134   ;; NNTP servers do not include Xrefs when using XOVER.
5135   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5136   (let ((mail-parse-charset gnus-newsgroup-charset)
5137         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5138         (cur nntp-server-buffer)
5139         (dependencies (or dependencies gnus-newsgroup-dependencies))
5140         (allp (cond
5141                ((eq gnus-read-all-available-headers t)
5142                 t)
5143                ((stringp gnus-read-all-available-headers)
5144                 (string-match gnus-read-all-available-headers group))
5145                (t
5146                 nil)))
5147         number headers header)
5148     (save-excursion
5149       (set-buffer nntp-server-buffer)
5150       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5151       ;; Allow the user to mangle the headers before parsing them.
5152       (gnus-run-hooks 'gnus-parse-headers-hook)
5153       (goto-char (point-min))
5154       (while (not (eobp))
5155         (condition-case ()
5156             (while (and (or sequence allp)
5157                         (not (eobp)))
5158               (setq number (read cur))
5159               (when (not allp)
5160                 (while (and sequence
5161                             (< (car sequence) number))
5162                   (setq sequence (cdr sequence))))
5163               (when (and (or allp
5164                              (and sequence
5165                                   (eq number (car sequence))))
5166                          (progn
5167                            (setq sequence (cdr sequence))
5168                            (setq header (inline
5169                                           (gnus-nov-parse-line
5170                                            number dependencies force-new)))))
5171                 (push header headers))
5172               (forward-line 1))
5173           (error
5174            (gnus-error 4 "Strange nov line (%d)"
5175                        (count-lines (point-min) (point)))))
5176         (forward-line 1))
5177       ;; A common bug in inn is that if you have posted an article and
5178       ;; then retrieves the active file, it will answer correctly --
5179       ;; the new article is included.  However, a NOV entry for the
5180       ;; article may not have been generated yet, so this may fail.
5181       ;; We work around this problem by retrieving the last few
5182       ;; headers using HEAD.
5183       (if (or (not also-fetch-heads)
5184               (not sequence))
5185           ;; We (probably) got all the headers.
5186           (nreverse headers)
5187         (let ((gnus-nov-is-evil t))
5188           (nconc
5189            (nreverse headers)
5190            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5191              (gnus-get-newsgroup-headers))))))))
5192
5193 (defun gnus-article-get-xrefs ()
5194   "Fill in the Xref value in `gnus-current-headers', if necessary.
5195 This is meant to be called in `gnus-article-internal-prepare-hook'."
5196   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5197                                  gnus-current-headers)))
5198     (or (not gnus-use-cross-reference)
5199         (not headers)
5200         (and (mail-header-xref headers)
5201              (not (string= (mail-header-xref headers) "")))
5202         (let ((case-fold-search t)
5203               xref)
5204           (save-restriction
5205             (nnheader-narrow-to-headers)
5206             (goto-char (point-min))
5207             (when (or (and (not (eobp))
5208                            (eq (downcase (char-after)) ?x)
5209                            (looking-at "Xref:"))
5210                       (search-forward "\nXref:" nil t))
5211               (goto-char (1+ (match-end 0)))
5212               (setq xref (buffer-substring (point)
5213                                            (progn (end-of-line) (point))))
5214               (mail-header-set-xref headers xref)))))))
5215
5216 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5217   "Find article ID and insert the summary line for that article.
5218 OLD-HEADER can either be a header or a line number to insert
5219 the subject line on."
5220   (let* ((line (and (numberp old-header) old-header))
5221          (old-header (and (vectorp old-header) old-header))
5222          (header (cond ((and old-header use-old-header)
5223                         old-header)
5224                        ((and (numberp id)
5225                              (gnus-number-to-header id))
5226                         (gnus-number-to-header id))
5227                        (t
5228                         (gnus-read-header id))))
5229          (number (and (numberp id) id))
5230          d)
5231     (when header
5232       ;; Rebuild the thread that this article is part of and go to the
5233       ;; article we have fetched.
5234       (when (and (not gnus-show-threads)
5235                  old-header)
5236         (when (and number
5237                    (setq d (gnus-data-find (mail-header-number old-header))))
5238           (goto-char (gnus-data-pos d))
5239           (gnus-data-remove
5240            number
5241            (- (gnus-point-at-bol)
5242               (prog1
5243                   (1+ (gnus-point-at-eol))
5244                 (gnus-delete-line))))))
5245       (when old-header
5246         (mail-header-set-number header (mail-header-number old-header)))
5247       (setq gnus-newsgroup-sparse
5248             (delq (setq number (mail-header-number header))
5249                   gnus-newsgroup-sparse))
5250       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5251       (push number gnus-newsgroup-limit)
5252       (gnus-rebuild-thread (mail-header-id header) line)
5253       (gnus-summary-goto-subject number nil t))
5254     (when (and (numberp number)
5255                (> number 0))
5256       ;; We have to update the boundaries even if we can't fetch the
5257       ;; article if ID is a number -- so that the next `P' or `N'
5258       ;; command will fetch the previous (or next) article even
5259       ;; if the one we tried to fetch this time has been canceled.
5260       (when (> number gnus-newsgroup-end)
5261         (setq gnus-newsgroup-end number))
5262       (when (< number gnus-newsgroup-begin)
5263         (setq gnus-newsgroup-begin number))
5264       (setq gnus-newsgroup-unselected
5265             (delq number gnus-newsgroup-unselected)))
5266     ;; Report back a success?
5267     (and header (mail-header-number header))))
5268
5269 ;;; Process/prefix in the summary buffer
5270
5271 (defun gnus-summary-work-articles (n)
5272   "Return a list of articles to be worked upon.
5273 The prefix argument, the list of process marked articles, and the
5274 current article will be taken into consideration."
5275   (save-excursion
5276     (set-buffer gnus-summary-buffer)
5277     (cond
5278      (n
5279       ;; A numerical prefix has been given.
5280       (setq n (prefix-numeric-value n))
5281       (let ((backward (< n 0))
5282             (n (abs (prefix-numeric-value n)))
5283             articles article)
5284         (save-excursion
5285           (while
5286               (and (> n 0)
5287                    (push (setq article (gnus-summary-article-number))
5288                          articles)
5289                    (if backward
5290                        (gnus-summary-find-prev nil article)
5291                      (gnus-summary-find-next nil article)))
5292             (decf n)))
5293         (nreverse articles)))
5294      ((and (gnus-region-active-p) (mark))
5295       (message "region active")
5296       ;; Work on the region between point and mark.
5297       (let ((max (max (point) (mark)))
5298             articles article)
5299         (save-excursion
5300           (goto-char (min (point) (mark)))
5301           (while
5302               (and
5303                (push (setq article (gnus-summary-article-number)) articles)
5304                (gnus-summary-find-next nil article)
5305                (< (point) max)))
5306           (nreverse articles))))
5307      (gnus-newsgroup-processable
5308       ;; There are process-marked articles present.
5309       ;; Save current state.
5310       (gnus-summary-save-process-mark)
5311       ;; Return the list.
5312       (reverse gnus-newsgroup-processable))
5313      (t
5314       ;; Just return the current article.
5315       (list (gnus-summary-article-number))))))
5316
5317 (defmacro gnus-summary-iterate (arg &rest forms)
5318   "Iterate over the process/prefixed articles and do FORMS.
5319 ARG is the interactive prefix given to the command.  FORMS will be
5320 executed with point over the summary line of the articles."
5321   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5322     `(let ((,articles (gnus-summary-work-articles ,arg)))
5323        (while ,articles
5324          (gnus-summary-goto-subject (car ,articles))
5325          ,@forms
5326          (pop ,articles)))))
5327
5328 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5329 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5330
5331 (defun gnus-summary-save-process-mark ()
5332   "Push the current set of process marked articles on the stack."
5333   (interactive)
5334   (push (copy-sequence gnus-newsgroup-processable)
5335         gnus-newsgroup-process-stack))
5336
5337 (defun gnus-summary-kill-process-mark ()
5338   "Push the current set of process marked articles on the stack and unmark."
5339   (interactive)
5340   (gnus-summary-save-process-mark)
5341   (gnus-summary-unmark-all-processable))
5342
5343 (defun gnus-summary-yank-process-mark ()
5344   "Pop the last process mark state off the stack and restore it."
5345   (interactive)
5346   (unless gnus-newsgroup-process-stack
5347     (error "Empty mark stack"))
5348   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5349
5350 (defun gnus-summary-process-mark-set (set)
5351   "Make SET into the current process marked articles."
5352   (gnus-summary-unmark-all-processable)
5353   (while set
5354     (gnus-summary-set-process-mark (pop set))))
5355
5356 ;;; Searching and stuff
5357
5358 (defun gnus-summary-search-group (&optional backward use-level)
5359   "Search for next unread newsgroup.
5360 If optional argument BACKWARD is non-nil, search backward instead."
5361   (save-excursion
5362     (set-buffer gnus-group-buffer)
5363     (when (gnus-group-search-forward
5364            backward nil (if use-level (gnus-group-group-level) nil))
5365       (gnus-group-group-name))))
5366
5367 (defun gnus-summary-best-group (&optional exclude-group)
5368   "Find the name of the best unread group.
5369 If EXCLUDE-GROUP, do not go to this group."
5370   (save-excursion
5371     (set-buffer gnus-group-buffer)
5372     (save-excursion
5373       (gnus-group-best-unread-group exclude-group))))
5374
5375 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5376   (if backward (gnus-summary-find-prev)
5377     (let* ((dummy (gnus-summary-article-intangible-p))
5378            (article (or article (gnus-summary-article-number)))
5379            (arts (gnus-data-find-list article))
5380            result)
5381       (when (and (not dummy)
5382                  (or (not gnus-summary-check-current)
5383                      (not unread)
5384                      (not (gnus-data-unread-p (car arts)))))
5385         (setq arts (cdr arts)))
5386       (when (setq result
5387                   (if unread
5388                       (progn
5389                         (while arts
5390                           (when (or (and undownloaded
5391                                          (eq gnus-undownloaded-mark
5392                                              (gnus-data-mark (car arts))))
5393                                     (gnus-data-unread-p (car arts)))
5394                             (setq result (car arts)
5395                                   arts nil))
5396                           (setq arts (cdr arts)))
5397                         result)
5398                     (car arts)))
5399         (goto-char (gnus-data-pos result))
5400         (gnus-data-number result)))))
5401
5402 (defun gnus-summary-find-prev (&optional unread article)
5403   (let* ((eobp (eobp))
5404          (article (or article (gnus-summary-article-number)))
5405          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5406          result)
5407     (when (and (not eobp)
5408                (or (not gnus-summary-check-current)
5409                    (not unread)
5410                    (not (gnus-data-unread-p (car arts)))))
5411       (setq arts (cdr arts)))
5412     (when (setq result
5413                 (if unread
5414                     (progn
5415                       (while arts
5416                         (when (gnus-data-unread-p (car arts))
5417                           (setq result (car arts)
5418                                 arts nil))
5419                         (setq arts (cdr arts)))
5420                       result)
5421                   (car arts)))
5422       (goto-char (gnus-data-pos result))
5423       (gnus-data-number result))))
5424
5425 (defun gnus-summary-find-subject (subject &optional unread backward article)
5426   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5427          (article (or article (gnus-summary-article-number)))
5428          (articles (gnus-data-list backward))
5429          (arts (gnus-data-find-list article articles))
5430          result)
5431     (when (or (not gnus-summary-check-current)
5432               (not unread)
5433               (not (gnus-data-unread-p (car arts))))
5434       (setq arts (cdr arts)))
5435     (while arts
5436       (and (or (not unread)
5437                (gnus-data-unread-p (car arts)))
5438            (vectorp (gnus-data-header (car arts)))
5439            (gnus-subject-equal
5440             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5441            (setq result (car arts)
5442                  arts nil))
5443       (setq arts (cdr arts)))
5444     (and result
5445          (goto-char (gnus-data-pos result))
5446          (gnus-data-number result))))
5447
5448 (defun gnus-summary-search-forward (&optional unread subject backward)
5449   "Search forward for an article.
5450 If UNREAD, look for unread articles.  If SUBJECT, look for
5451 articles with that subject.  If BACKWARD, search backward instead."
5452   (cond (subject (gnus-summary-find-subject subject unread backward))
5453         (backward (gnus-summary-find-prev unread))
5454         (t (gnus-summary-find-next unread))))
5455
5456 (defun gnus-recenter (&optional n)
5457   "Center point in window and redisplay frame.
5458 Also do horizontal recentering."
5459   (interactive "P")
5460   (when (and gnus-auto-center-summary
5461              (not (eq gnus-auto-center-summary 'vertical)))
5462     (gnus-horizontal-recenter))
5463   (recenter n))
5464
5465 (defun gnus-summary-recenter ()
5466   "Center point in the summary window.
5467 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5468 displayed, no centering will be performed."
5469   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5470   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5471   (interactive)
5472   (let* ((top (cond ((< (window-height) 4) 0)
5473                     ((< (window-height) 7) 1)
5474                     (t (if (numberp gnus-auto-center-summary)
5475                            gnus-auto-center-summary
5476                          2))))
5477          (height (1- (window-height)))
5478          (bottom (save-excursion (goto-char (point-max))
5479                                  (forward-line (- height))
5480                                  (point)))
5481          (window (get-buffer-window (current-buffer))))
5482     ;; The user has to want it.
5483     (when gnus-auto-center-summary
5484       (when (get-buffer-window gnus-article-buffer)
5485         ;; Only do recentering when the article buffer is displayed,
5486         ;; Set the window start to either `bottom', which is the biggest
5487         ;; possible valid number, or the second line from the top,
5488         ;; whichever is the least.
5489         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5490           (if (> bottom top-pos)
5491               ;; Keep the second line from the top visible
5492               (set-window-start window top-pos t)
5493             ;; Try to keep the bottom line visible; if it's partially
5494             ;; obscured, either scroll one more line to make it fully
5495             ;; visible, or revert to using TOP-POS.
5496             (save-excursion
5497               (goto-char (point-max))
5498               (forward-line -1)
5499               (let ((last-line-start (point)))
5500                 (goto-char bottom)
5501                 (set-window-start window (point) t)
5502                 (when (not (pos-visible-in-window-p last-line-start window))
5503                   (forward-line 1)
5504                   (set-window-start window (min (point) top-pos) t)))))))
5505       ;; Do horizontal recentering while we're at it.
5506       (when (and (get-buffer-window (current-buffer) t)
5507                  (not (eq gnus-auto-center-summary 'vertical)))
5508         (let ((selected (selected-window)))
5509           (select-window (get-buffer-window (current-buffer) t))
5510           (gnus-summary-position-point)
5511           (gnus-horizontal-recenter)
5512           (select-window selected))))))
5513
5514 (defun gnus-summary-jump-to-group (newsgroup)
5515   "Move point to NEWSGROUP in group mode buffer."
5516   ;; Keep update point of group mode buffer if visible.
5517   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5518       (save-window-excursion
5519         ;; Take care of tree window mode.
5520         (when (get-buffer-window gnus-group-buffer)
5521           (pop-to-buffer gnus-group-buffer))
5522         (gnus-group-jump-to-group newsgroup))
5523     (save-excursion
5524       ;; Take care of tree window mode.
5525       (if (get-buffer-window gnus-group-buffer)
5526           (pop-to-buffer gnus-group-buffer)
5527         (set-buffer gnus-group-buffer))
5528       (gnus-group-jump-to-group newsgroup))))
5529
5530 ;; This function returns a list of article numbers based on the
5531 ;; difference between the ranges of read articles in this group and
5532 ;; the range of active articles.
5533 (defun gnus-list-of-unread-articles (group)
5534   (let* ((read (gnus-info-read (gnus-get-info group)))
5535          (active (or (gnus-active group) (gnus-activate-group group)))
5536          (last (cdr active))
5537          first nlast unread)
5538     ;; If none are read, then all are unread.
5539     (if (not read)
5540         (setq first (car active))
5541       ;; If the range of read articles is a single range, then the
5542       ;; first unread article is the article after the last read
5543       ;; article.  Sounds logical, doesn't it?
5544       (if (and (not (listp (cdr read)))
5545                (or (< (car read) (car active))
5546                    (progn (setq read (list read))
5547                           nil)))
5548           (setq first (max (car active) (1+ (cdr read))))
5549         ;; `read' is a list of ranges.
5550         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5551                                   (caar read)))
5552                   1)
5553           (setq first (car active)))
5554         (while read
5555           (when first
5556             (while (< first nlast)
5557               (push first unread)
5558               (setq first (1+ first))))
5559           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5560           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5561           (setq read (cdr read)))))
5562     ;; And add the last unread articles.
5563     (while (<= first last)
5564       (push first unread)
5565       (setq first (1+ first)))
5566     ;; Return the list of unread articles.
5567     (delq 0 (nreverse unread))))
5568
5569 (defun gnus-list-of-read-articles (group)
5570   "Return a list of unread, unticked and non-dormant articles."
5571   (let* ((info (gnus-get-info group))
5572          (marked (gnus-info-marks info))
5573          (active (gnus-active group)))
5574     (and info active
5575          (gnus-set-difference
5576           (gnus-sorted-complement
5577            (gnus-uncompress-range active)
5578            (gnus-list-of-unread-articles group))
5579           (append
5580            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5581            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5582
5583 ;; Various summary commands
5584
5585 (defun gnus-summary-select-article-buffer ()
5586   "Reconfigure windows to show article buffer."
5587   (interactive)
5588   (if (not (gnus-buffer-live-p gnus-article-buffer))
5589       (error "There is no article buffer for this summary buffer")
5590     (gnus-configure-windows 'article)
5591     (select-window (get-buffer-window gnus-article-buffer))))
5592
5593 (defun gnus-summary-universal-argument (arg)
5594   "Perform any operation on all articles that are process/prefixed."
5595   (interactive "P")
5596   (let ((articles (gnus-summary-work-articles arg))
5597         func article)
5598     (if (eq
5599          (setq
5600           func
5601           (key-binding
5602            (read-key-sequence
5603             (substitute-command-keys
5604              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5605          'undefined)
5606         (gnus-error 1 "Undefined key")
5607       (save-excursion
5608         (while articles
5609           (gnus-summary-goto-subject (setq article (pop articles)))
5610           (let (gnus-newsgroup-processable)
5611             (command-execute func))
5612           (gnus-summary-remove-process-mark article)))))
5613   (gnus-summary-position-point))
5614
5615 (defun gnus-summary-toggle-truncation (&optional arg)
5616   "Toggle truncation of summary lines.
5617 With arg, turn line truncation on iff arg is positive."
5618   (interactive "P")
5619   (setq truncate-lines
5620         (if (null arg) (not truncate-lines)
5621           (> (prefix-numeric-value arg) 0)))
5622   (redraw-display))
5623
5624 (defun gnus-summary-reselect-current-group (&optional all rescan)
5625   "Exit and then reselect the current newsgroup.
5626 The prefix argument ALL means to select all articles."
5627   (interactive "P")
5628   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5629     (error "Ephemeral groups can't be reselected"))
5630   (let ((current-subject (gnus-summary-article-number))
5631         (group gnus-newsgroup-name))
5632     (setq gnus-newsgroup-begin nil)
5633     (gnus-summary-exit)
5634     ;; We have to adjust the point of group mode buffer because
5635     ;; point was moved to the next unread newsgroup by exiting.
5636     (gnus-summary-jump-to-group group)
5637     (when rescan
5638       (save-excursion
5639         (save-window-excursion
5640           ;; Don't show group contents.
5641           (set-window-start (selected-window) (point-max))
5642           (gnus-group-get-new-news-this-group 1))))
5643     (gnus-group-read-group all t)
5644     (gnus-summary-goto-subject current-subject nil t)))
5645
5646 (defun gnus-summary-rescan-group (&optional all)
5647   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5648   (interactive "P")
5649   (gnus-summary-reselect-current-group all t))
5650
5651 (defun gnus-summary-update-info (&optional non-destructive)
5652   (save-excursion
5653     (let ((group gnus-newsgroup-name))
5654       (when group
5655         (when gnus-newsgroup-kill-headers
5656           (setq gnus-newsgroup-killed
5657                 (gnus-compress-sequence
5658                  (nconc
5659                   (gnus-set-sorted-intersection
5660                    (gnus-uncompress-range gnus-newsgroup-killed)
5661                    (setq gnus-newsgroup-unselected
5662                          (sort gnus-newsgroup-unselected '<)))
5663                   (setq gnus-newsgroup-unreads
5664                         (sort gnus-newsgroup-unreads '<)))
5665                  t)))
5666         (unless (listp (cdr gnus-newsgroup-killed))
5667           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5668         (let ((headers gnus-newsgroup-headers))
5669           ;; Set the new ranges of read articles.
5670           (save-excursion
5671             (set-buffer gnus-group-buffer)
5672             (gnus-undo-force-boundary))
5673           (gnus-update-read-articles
5674            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5675           ;; Set the current article marks.
5676           (let ((gnus-newsgroup-scored
5677                  (if (and (not gnus-save-score)
5678                           (not non-destructive))
5679                      nil
5680                    gnus-newsgroup-scored)))
5681             (save-excursion
5682               (gnus-update-marks)))
5683           ;; Do the cross-ref thing.
5684           (when gnus-use-cross-reference
5685             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5686           ;; Do not switch windows but change the buffer to work.
5687           (set-buffer gnus-group-buffer)
5688           (unless (gnus-ephemeral-group-p group)
5689             (gnus-group-update-group group)))))))
5690
5691 (defun gnus-summary-save-newsrc (&optional force)
5692   "Save the current number of read/marked articles in the dribble buffer.
5693 The dribble buffer will then be saved.
5694 If FORCE (the prefix), also save the .newsrc file(s)."
5695   (interactive "P")
5696   (gnus-summary-update-info t)
5697   (if force
5698       (gnus-save-newsrc-file)
5699     (gnus-dribble-save)))
5700
5701 (defun gnus-summary-exit (&optional temporary)
5702   "Exit reading current newsgroup, and then return to group selection mode.
5703 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5704   (interactive)
5705   (gnus-set-global-variables)
5706   (gnus-kill-save-kill-buffer)
5707   (gnus-async-halt-prefetch)
5708   (let* ((group gnus-newsgroup-name)
5709          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5710          (mode major-mode)
5711          (group-point nil)
5712          (buf (current-buffer)))
5713     (unless quit-config
5714       ;; Do adaptive scoring, and possibly save score files.
5715       (when gnus-newsgroup-adaptive
5716         (gnus-score-adaptive))
5717       (when gnus-use-scoring
5718         (gnus-score-save)))
5719     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5720     ;; If we have several article buffers, we kill them at exit.
5721     (unless gnus-single-article-buffer
5722       (gnus-kill-buffer gnus-original-article-buffer)
5723       (setq gnus-article-current nil))
5724     (when gnus-use-cache
5725       (gnus-cache-possibly-remove-articles)
5726       (gnus-cache-save-buffers))
5727     (gnus-async-prefetch-remove-group group)
5728     (when gnus-suppress-duplicates
5729       (gnus-dup-enter-articles))
5730     (when gnus-use-trees
5731       (gnus-tree-close group))
5732     (when gnus-use-cache
5733       (gnus-cache-write-active))
5734     ;; Remove entries for this group.
5735     (nnmail-purge-split-history (gnus-group-real-name group))
5736     ;; Make all changes in this group permanent.
5737     (unless quit-config
5738       (gnus-run-hooks 'gnus-exit-group-hook)
5739       (gnus-summary-update-info))
5740     (gnus-close-group group)
5741     ;; Make sure where we were, and go to next newsgroup.
5742     (set-buffer gnus-group-buffer)
5743     (unless quit-config
5744       (gnus-group-jump-to-group group))
5745     (gnus-run-hooks 'gnus-summary-exit-hook)
5746     (unless (or quit-config
5747                 ;; If this group has disappeared from the summary
5748                 ;; buffer, don't skip forwards.
5749                 (not (string= group (gnus-group-group-name))))
5750       (gnus-group-next-unread-group 1))
5751     (setq group-point (point))
5752     (if temporary
5753         nil                             ;Nothing to do.
5754       ;; If we have several article buffers, we kill them at exit.
5755       (unless gnus-single-article-buffer
5756         (gnus-kill-buffer gnus-article-buffer)
5757         (gnus-kill-buffer gnus-original-article-buffer)
5758         (setq gnus-article-current nil))
5759       (set-buffer buf)
5760       (if (not gnus-kill-summary-on-exit)
5761           (progn
5762             (gnus-deaden-summary)
5763             (setq mode nil))
5764         ;; We set all buffer-local variables to nil.  It is unclear why
5765         ;; this is needed, but if we don't, buffer-local variables are
5766         ;; not garbage-collected, it seems.  This would the lead to en
5767         ;; ever-growing Emacs.
5768         (gnus-summary-clear-local-variables)
5769         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5770           (gnus-summary-clear-local-variables))
5771         (when (get-buffer gnus-article-buffer)
5772           (bury-buffer gnus-article-buffer))
5773         ;; We clear the global counterparts of the buffer-local
5774         ;; variables as well, just to be on the safe side.
5775         (set-buffer gnus-group-buffer)
5776         (gnus-summary-clear-local-variables)
5777         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5778           (gnus-summary-clear-local-variables)))
5779       (setq gnus-current-select-method gnus-select-method)
5780       (pop-to-buffer gnus-group-buffer)
5781       (if (not quit-config)
5782           (progn
5783             (goto-char group-point)
5784             (gnus-configure-windows 'group 'force)
5785             (unless (pos-visible-in-window-p)
5786               (forward-line (/ (static-if (featurep 'xemacs)
5787                                    (window-displayed-height)
5788                                  (1- (window-height)))
5789                                -2))
5790               (set-window-start (selected-window) (point))
5791               (goto-char group-point)))
5792         (gnus-handle-ephemeral-exit quit-config))
5793       ;; Return to group mode buffer.
5794       (when (eq mode 'gnus-summary-mode)
5795         (gnus-kill-buffer buf))
5796       ;; Clear the current group name.
5797       (unless quit-config
5798         (setq gnus-newsgroup-name nil)))))
5799
5800 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5801 (defun gnus-summary-exit-no-update (&optional no-questions)
5802   "Quit reading current newsgroup without updating read article info."
5803   (interactive)
5804   (let* ((group gnus-newsgroup-name)
5805          (quit-config (gnus-group-quit-config group)))
5806     (when (or no-questions
5807               gnus-expert-user
5808               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5809       (gnus-async-halt-prefetch)
5810       (mapcar 'funcall
5811               (delq 'gnus-summary-expire-articles
5812                     (copy-sequence gnus-summary-prepare-exit-hook)))
5813       ;; If we have several article buffers, we kill them at exit.
5814       (unless gnus-single-article-buffer
5815         (gnus-kill-buffer gnus-article-buffer)
5816         (gnus-kill-buffer gnus-original-article-buffer)
5817         (setq gnus-article-current nil))
5818       (if (not gnus-kill-summary-on-exit)
5819           (gnus-deaden-summary)
5820         (gnus-close-group group)
5821         (gnus-summary-clear-local-variables)
5822         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5823           (gnus-summary-clear-local-variables))
5824         (set-buffer gnus-group-buffer)
5825         (gnus-summary-clear-local-variables)
5826         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5827           (gnus-summary-clear-local-variables))
5828         (when (get-buffer gnus-summary-buffer)
5829           (kill-buffer gnus-summary-buffer)))
5830       (unless gnus-single-article-buffer
5831         (setq gnus-article-current nil))
5832       (when gnus-use-trees
5833         (gnus-tree-close group))
5834       (gnus-async-prefetch-remove-group group)
5835       (when (get-buffer gnus-article-buffer)
5836         (bury-buffer gnus-article-buffer))
5837       ;; Return to the group buffer.
5838       (gnus-configure-windows 'group 'force)
5839       ;; Clear the current group name.
5840       (setq gnus-newsgroup-name nil)
5841       (when (equal (gnus-group-group-name) group)
5842         (gnus-group-next-unread-group 1))
5843       (when quit-config
5844         (gnus-handle-ephemeral-exit quit-config)))))
5845
5846 (defun gnus-handle-ephemeral-exit (quit-config)
5847   "Handle movement when leaving an ephemeral group.
5848 The state which existed when entering the ephemeral is reset."
5849   (if (not (buffer-name (car quit-config)))
5850       (gnus-configure-windows 'group 'force)
5851     (set-buffer (car quit-config))
5852     (cond ((eq major-mode 'gnus-summary-mode)
5853            (gnus-set-global-variables))
5854           ((eq major-mode 'gnus-article-mode)
5855            (save-excursion
5856              ;; The `gnus-summary-buffer' variable may point
5857              ;; to the old summary buffer when using a single
5858              ;; article buffer.
5859              (unless (gnus-buffer-live-p gnus-summary-buffer)
5860                (set-buffer gnus-group-buffer))
5861              (set-buffer gnus-summary-buffer)
5862              (gnus-set-global-variables))))
5863     (if (or (eq (cdr quit-config) 'article)
5864             (eq (cdr quit-config) 'pick))
5865         (progn
5866           ;; The current article may be from the ephemeral group
5867           ;; thus it is best that we reload this article
5868           (gnus-summary-show-article)
5869           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5870               (gnus-configure-windows 'pick 'force)
5871             (gnus-configure-windows (cdr quit-config) 'force)))
5872       (gnus-configure-windows (cdr quit-config) 'force))
5873     (when (eq major-mode 'gnus-summary-mode)
5874       (gnus-summary-next-subject 1 nil t)
5875       (gnus-summary-recenter)
5876       (gnus-summary-position-point))))
5877
5878 (defun gnus-summary-preview-mime-message ()
5879   "MIME decode and play this message."
5880   (interactive)
5881   (let ((gnus-break-pages nil)
5882         (gnus-show-mime t))
5883     (gnus-summary-select-article gnus-show-all-headers t))
5884   (select-window (get-buffer-window gnus-article-buffer)))
5885
5886 ;;; Dead summaries.
5887
5888 (defvar gnus-dead-summary-mode-map nil)
5889
5890 (unless gnus-dead-summary-mode-map
5891   (setq gnus-dead-summary-mode-map (make-keymap))
5892   (suppress-keymap gnus-dead-summary-mode-map)
5893   (substitute-key-definition
5894    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5895   (let ((keys '("\C-d" "\r" "\177" [delete])))
5896     (while keys
5897       (define-key gnus-dead-summary-mode-map
5898         (pop keys) 'gnus-summary-wake-up-the-dead))))
5899
5900 (defvar gnus-dead-summary-mode nil
5901   "Minor mode for Gnus summary buffers.")
5902
5903 (defun gnus-dead-summary-mode (&optional arg)
5904   "Minor mode for Gnus summary buffers."
5905   (interactive "P")
5906   (when (eq major-mode 'gnus-summary-mode)
5907     (make-local-variable 'gnus-dead-summary-mode)
5908     (setq gnus-dead-summary-mode
5909           (if (null arg) (not gnus-dead-summary-mode)
5910             (> (prefix-numeric-value arg) 0)))
5911     (when gnus-dead-summary-mode
5912       (gnus-add-minor-mode
5913        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5914
5915 (defun gnus-deaden-summary ()
5916   "Make the current summary buffer into a dead summary buffer."
5917   ;; Kill any previous dead summary buffer.
5918   (when (and gnus-dead-summary
5919              (buffer-name gnus-dead-summary))
5920     (save-excursion
5921       (set-buffer gnus-dead-summary)
5922       (when gnus-dead-summary-mode
5923         (kill-buffer (current-buffer)))))
5924   ;; Make this the current dead summary.
5925   (setq gnus-dead-summary (current-buffer))
5926   (gnus-dead-summary-mode 1)
5927   (let ((name (buffer-name)))
5928     (when (string-match "Summary" name)
5929       (rename-buffer
5930        (concat (substring name 0 (match-beginning 0)) "Dead "
5931                (substring name (match-beginning 0)))
5932        t)
5933       (bury-buffer))))
5934
5935 (defun gnus-kill-or-deaden-summary (buffer)
5936   "Kill or deaden the summary BUFFER."
5937   (save-excursion
5938     (when (and (buffer-name buffer)
5939                (not gnus-single-article-buffer))
5940       (save-excursion
5941         (set-buffer buffer)
5942         (gnus-kill-buffer gnus-article-buffer)
5943         (gnus-kill-buffer gnus-original-article-buffer)))
5944     (cond (gnus-kill-summary-on-exit
5945            (when (and gnus-use-trees
5946                       (gnus-buffer-exists-p buffer))
5947              (save-excursion
5948                (set-buffer buffer)
5949                (gnus-tree-close gnus-newsgroup-name)))
5950            (gnus-kill-buffer buffer))
5951           ((gnus-buffer-exists-p buffer)
5952            (save-excursion
5953              (set-buffer buffer)
5954              (gnus-deaden-summary))))))
5955
5956 (defun gnus-summary-wake-up-the-dead (&rest args)
5957   "Wake up the dead summary buffer."
5958   (interactive)
5959   (gnus-dead-summary-mode -1)
5960   (let ((name (buffer-name)))
5961     (when (string-match "Dead " name)
5962       (rename-buffer
5963        (concat (substring name 0 (match-beginning 0))
5964                (substring name (match-end 0)))
5965        t)))
5966   (gnus-message 3 "This dead summary is now alive again"))
5967
5968 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5969 (defun gnus-summary-fetch-faq (&optional faq-dir)
5970   "Fetch the FAQ for the current group.
5971 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5972 in."
5973   (interactive
5974    (list
5975     (when current-prefix-arg
5976       (completing-read
5977        "Faq dir: " (and (listp gnus-group-faq-directory)
5978                         (mapcar (lambda (file) (list file))
5979                                 gnus-group-faq-directory))))))
5980   (let (gnus-faq-buffer)
5981     (when (setq gnus-faq-buffer
5982                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5983       (gnus-configure-windows 'summary-faq))))
5984
5985 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5986 (defun gnus-summary-describe-group (&optional force)
5987   "Describe the current newsgroup."
5988   (interactive "P")
5989   (gnus-group-describe-group force gnus-newsgroup-name))
5990
5991 (defun gnus-summary-describe-briefly ()
5992   "Describe summary mode commands briefly."
5993   (interactive)
5994   (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")))
5995
5996 ;; Walking around group mode buffer from summary mode.
5997
5998 (defun gnus-summary-next-group (&optional no-article target-group backward)
5999   "Exit current newsgroup and then select next unread newsgroup.
6000 If prefix argument NO-ARTICLE is non-nil, no article is selected
6001 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6002 previous group instead."
6003   (interactive "P")
6004   ;; Stop pre-fetching.
6005   (gnus-async-halt-prefetch)
6006   (let ((current-group gnus-newsgroup-name)
6007         (current-buffer (current-buffer))
6008         entered)
6009     ;; First we semi-exit this group to update Xrefs and all variables.
6010     ;; We can't do a real exit, because the window conf must remain
6011     ;; the same in case the user is prompted for info, and we don't
6012     ;; want the window conf to change before that...
6013     (gnus-summary-exit t)
6014     (while (not entered)
6015       ;; Then we find what group we are supposed to enter.
6016       (set-buffer gnus-group-buffer)
6017       (gnus-group-jump-to-group current-group)
6018       (setq target-group
6019             (or target-group
6020                 (if (eq gnus-keep-same-level 'best)
6021                     (gnus-summary-best-group gnus-newsgroup-name)
6022                   (gnus-summary-search-group backward gnus-keep-same-level))))
6023       (if (not target-group)
6024           ;; There are no further groups, so we return to the group
6025           ;; buffer.
6026           (progn
6027             (gnus-message 5 "Returning to the group buffer")
6028             (setq entered t)
6029             (when (gnus-buffer-live-p current-buffer)
6030               (set-buffer current-buffer)
6031               (gnus-summary-exit))
6032             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6033         ;; We try to enter the target group.
6034         (gnus-group-jump-to-group target-group)
6035         (let ((unreads (gnus-group-group-unread)))
6036           (if (and (or (eq t unreads)
6037                        (and unreads (not (zerop unreads))))
6038                    (gnus-summary-read-group
6039                     target-group nil no-article
6040                     (and (buffer-name current-buffer) current-buffer)
6041                     nil backward))
6042               (setq entered t)
6043             (setq current-group target-group
6044                   target-group nil)))))))
6045
6046 (defun gnus-summary-prev-group (&optional no-article)
6047   "Exit current newsgroup and then select previous unread newsgroup.
6048 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6049   (interactive "P")
6050   (gnus-summary-next-group no-article nil t))
6051
6052 ;; Walking around summary lines.
6053
6054 (defun gnus-summary-first-subject (&optional unread undownloaded)
6055   "Go to the first unread subject.
6056 If UNREAD is non-nil, go to the first unread article.
6057 Returns the article selected or nil if there are no unread articles."
6058   (interactive "P")
6059   (prog1
6060       (cond
6061        ;; Empty summary.
6062        ((null gnus-newsgroup-data)
6063         (gnus-message 3 "No articles in the group")
6064         nil)
6065        ;; Pick the first article.
6066        ((not unread)
6067         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6068         (gnus-data-number (car gnus-newsgroup-data)))
6069        ;; No unread articles.
6070        ((null gnus-newsgroup-unreads)
6071         (gnus-message 3 "No more unread articles")
6072         nil)
6073        ;; Find the first unread article.
6074        (t
6075         (let ((data gnus-newsgroup-data))
6076           (while (and data
6077                       (and (not (and undownloaded
6078                                      (eq gnus-undownloaded-mark
6079                                          (gnus-data-mark (car data)))))
6080                            (not (gnus-data-unread-p (car data)))))
6081             (setq data (cdr data)))
6082           (when data
6083             (goto-char (gnus-data-pos (car data)))
6084             (gnus-data-number (car data))))))
6085     (gnus-summary-position-point)))
6086
6087 (defun gnus-summary-next-subject (n &optional unread dont-display)
6088   "Go to next N'th summary line.
6089 If N is negative, go to the previous N'th subject line.
6090 If UNREAD is non-nil, only unread articles are selected.
6091 The difference between N and the actual number of steps taken is
6092 returned."
6093   (interactive "p")
6094   (let ((backward (< n 0))
6095         (n (abs n)))
6096     (while (and (> n 0)
6097                 (if backward
6098                     (gnus-summary-find-prev unread)
6099                   (gnus-summary-find-next unread)))
6100       (unless (zerop (setq n (1- n)))
6101         (gnus-summary-show-thread)))
6102     (when (/= 0 n)
6103       (gnus-message 7 "No more%s articles"
6104                     (if unread " unread" "")))
6105     (unless dont-display
6106       (gnus-summary-recenter)
6107       (gnus-summary-position-point))
6108     n))
6109
6110 (defun gnus-summary-next-unread-subject (n)
6111   "Go to next N'th unread summary line."
6112   (interactive "p")
6113   (gnus-summary-next-subject n t))
6114
6115 (defun gnus-summary-prev-subject (n &optional unread)
6116   "Go to previous N'th summary line.
6117 If optional argument UNREAD is non-nil, only unread article is selected."
6118   (interactive "p")
6119   (gnus-summary-next-subject (- n) unread))
6120
6121 (defun gnus-summary-prev-unread-subject (n)
6122   "Go to previous N'th unread summary line."
6123   (interactive "p")
6124   (gnus-summary-next-subject (- n) t))
6125
6126 (defun gnus-summary-goto-subject (article &optional force silent)
6127   "Go the subject line of ARTICLE.
6128 If FORCE, also allow jumping to articles not currently shown."
6129   (interactive "nArticle number: ")
6130   (let ((b (point))
6131         (data (gnus-data-find article)))
6132     ;; We read in the article if we have to.
6133     (and (not data)
6134          force
6135          (gnus-summary-insert-subject
6136           article
6137           (if (or (numberp force) (vectorp force)) force)
6138           t)
6139          (setq data (gnus-data-find article)))
6140     (goto-char b)
6141     (if (not data)
6142         (progn
6143           (unless silent
6144             (gnus-message 3 "Can't find article %d" article))
6145           nil)
6146       (goto-char (gnus-data-pos data))
6147       (gnus-summary-position-point)
6148       article)))
6149
6150 ;; Walking around summary lines with displaying articles.
6151
6152 (defun gnus-summary-expand-window (&optional arg)
6153   "Make the summary buffer take up the entire Emacs frame.
6154 Given a prefix, will force an `article' buffer configuration."
6155   (interactive "P")
6156   (if arg
6157       (gnus-configure-windows 'article 'force)
6158     (gnus-configure-windows 'summary 'force)))
6159
6160 (defun gnus-summary-display-article (article &optional all-header)
6161   "Display ARTICLE in article buffer."
6162   (when (gnus-buffer-live-p gnus-article-buffer)
6163     (with-current-buffer gnus-article-buffer
6164       (set-buffer-multibyte t)))
6165   (gnus-set-global-variables)
6166   (when (gnus-buffer-live-p gnus-article-buffer)
6167     (with-current-buffer gnus-article-buffer
6168       (setq gnus-article-charset gnus-newsgroup-charset)
6169       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6170   (if (null article)
6171       nil
6172     (prog1
6173         (if gnus-summary-display-article-function
6174             (funcall gnus-summary-display-article-function article all-header)
6175           (gnus-article-prepare article all-header))
6176       (with-current-buffer gnus-article-buffer
6177         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6178              nil))
6179       (gnus-run-hooks 'gnus-select-article-hook)
6180       (when (and gnus-current-article
6181                  (not (zerop gnus-current-article)))
6182         (gnus-summary-goto-subject gnus-current-article))
6183       (gnus-summary-recenter)
6184       (when (and gnus-use-trees gnus-show-threads)
6185         (gnus-possibly-generate-tree article)
6186         (gnus-highlight-selected-tree article))
6187       ;; Successfully display article.
6188       (gnus-article-set-window-start
6189        (cdr (assq article gnus-newsgroup-bookmarks))))))
6190
6191 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6192   "Select the current article.
6193 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6194 non-nil, the article will be re-fetched even if it already present in
6195 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6196 be displayed."
6197   ;; Make sure we are in the summary buffer to work around bbdb bug.
6198   (unless (eq major-mode 'gnus-summary-mode)
6199     (set-buffer gnus-summary-buffer))
6200   (let ((article (or article (gnus-summary-article-number)))
6201         (all-headers (not (not all-headers))) ;Must be T or NIL.
6202         gnus-summary-display-article-function)
6203     (and (not pseudo)
6204          (gnus-summary-article-pseudo-p article)
6205          (error "This is a pseudo-article"))
6206     (save-excursion
6207       (set-buffer gnus-summary-buffer)
6208       (if (or (and gnus-single-article-buffer
6209                    (or (null gnus-current-article)
6210                        (null gnus-article-current)
6211                        (null (get-buffer gnus-article-buffer))
6212                        (not (eq article (cdr gnus-article-current)))
6213                        (not (equal (car gnus-article-current)
6214                                    gnus-newsgroup-name))))
6215               (and (not gnus-single-article-buffer)
6216                    (or (null gnus-current-article)
6217                        (not (eq gnus-current-article article))))
6218               force)
6219           ;; The requested article is different from the current article.
6220           (progn
6221             (gnus-summary-display-article article all-headers)
6222             (when (or all-headers gnus-show-all-headers)
6223               (gnus-article-show-all-headers))
6224             (gnus-article-set-window-start
6225              (cdr (assq article gnus-newsgroup-bookmarks)))
6226             article)
6227         (when (or all-headers gnus-show-all-headers)
6228           (gnus-article-show-all-headers))
6229         'old))))
6230
6231 (defun gnus-summary-force-verify-and-decrypt ()
6232   (interactive)
6233   (let ((mm-verify-option 'known)
6234         (mm-decrypt-option 'known))
6235     (gnus-summary-select-article nil 'force)))
6236
6237 (defun gnus-summary-set-current-mark (&optional current-mark)
6238   "Obsolete function."
6239   nil)
6240
6241 (defun gnus-summary-next-article (&optional unread subject backward push)
6242   "Select the next article.
6243 If UNREAD, only unread articles are selected.
6244 If SUBJECT, only articles with SUBJECT are selected.
6245 If BACKWARD, the previous article is selected instead of the next."
6246   (interactive "P")
6247   (cond
6248    ;; Is there such an article?
6249    ((and (gnus-summary-search-forward unread subject backward)
6250          (or (gnus-summary-display-article (gnus-summary-article-number))
6251              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6252     (gnus-summary-position-point))
6253    ;; If not, we try the first unread, if that is wanted.
6254    ((and subject
6255          gnus-auto-select-same
6256          (gnus-summary-first-unread-article))
6257     (gnus-summary-position-point)
6258     (gnus-message 6 "Wrapped"))
6259    ;; Try to get next/previous article not displayed in this group.
6260    ((and gnus-auto-extend-newsgroup
6261          (not unread) (not subject))
6262     (gnus-summary-goto-article
6263      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6264      nil (count-lines (point-min) (point))))
6265    ;; Go to next/previous group.
6266    (t
6267     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6268       (gnus-summary-jump-to-group gnus-newsgroup-name))
6269     (let ((cmd last-command-char)
6270           (point
6271            (save-excursion
6272              (set-buffer gnus-group-buffer)
6273              (point)))
6274           (group
6275            (if (eq gnus-keep-same-level 'best)
6276                (gnus-summary-best-group gnus-newsgroup-name)
6277              (gnus-summary-search-group backward gnus-keep-same-level))))
6278       ;; For some reason, the group window gets selected.  We change
6279       ;; it back.
6280       (select-window (get-buffer-window (current-buffer)))
6281       ;; Select next unread newsgroup automagically.
6282       (cond
6283        ((or (not gnus-auto-select-next)
6284             (not cmd))
6285         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6286        ((or (eq gnus-auto-select-next 'quietly)
6287             (and (eq gnus-auto-select-next 'slightly-quietly)
6288                  push)
6289             (and (eq gnus-auto-select-next 'almost-quietly)
6290                  (gnus-summary-last-article-p)))
6291         ;; Select quietly.
6292         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6293             (gnus-summary-exit)
6294           (gnus-message 7 "No more%s articles (%s)..."
6295                         (if unread " unread" "")
6296                         (if group (concat "selecting " group)
6297                           "exiting"))
6298           (gnus-summary-next-group nil group backward)))
6299        (t
6300         (when (gnus-key-press-event-p last-input-event)
6301           (gnus-summary-walk-group-buffer
6302            gnus-newsgroup-name cmd unread backward point))))))))
6303
6304 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6305   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6306                       (?\C-p (gnus-group-prev-unread-group 1))))
6307         (cursor-in-echo-area t)
6308         keve key group ended)
6309     (save-excursion
6310       (set-buffer gnus-group-buffer)
6311       (goto-char start)
6312       (setq group
6313             (if (eq gnus-keep-same-level 'best)
6314                 (gnus-summary-best-group gnus-newsgroup-name)
6315               (gnus-summary-search-group backward gnus-keep-same-level))))
6316     (while (not ended)
6317       (gnus-message
6318        5 "No more%s articles%s" (if unread " unread" "")
6319        (if (and group
6320                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6321            (format " (Type %s for %s [%s])"
6322                    (single-key-description cmd) group
6323                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6324          (format " (Type %s to exit %s)"
6325                  (single-key-description cmd)
6326                  gnus-newsgroup-name)))
6327       ;; Confirm auto selection.
6328       (setq key (car (setq keve (gnus-read-event-char))))
6329       (setq ended t)
6330       (cond
6331        ((assq key keystrokes)
6332         (let ((obuf (current-buffer)))
6333           (switch-to-buffer gnus-group-buffer)
6334           (when group
6335             (gnus-group-jump-to-group group))
6336           (eval (cadr (assq key keystrokes)))
6337           (setq group (gnus-group-group-name))
6338           (switch-to-buffer obuf))
6339         (setq ended nil))
6340        ((equal key cmd)
6341         (if (or (not group)
6342                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6343             (gnus-summary-exit)
6344           (gnus-summary-next-group nil group backward)))
6345        (t
6346         (push (cdr keve) unread-command-events))))))
6347
6348 (defun gnus-summary-next-unread-article ()
6349   "Select unread article after current one."
6350   (interactive)
6351   (gnus-summary-next-article
6352    (or (not (eq gnus-summary-goto-unread 'never))
6353        (gnus-summary-last-article-p (gnus-summary-article-number)))
6354    (and gnus-auto-select-same
6355         (gnus-summary-article-subject))))
6356
6357 (defun gnus-summary-prev-article (&optional unread subject)
6358   "Select the article after the current one.
6359 If UNREAD is non-nil, only unread articles are selected."
6360   (interactive "P")
6361   (gnus-summary-next-article unread subject t))
6362
6363 (defun gnus-summary-prev-unread-article ()
6364   "Select unread article before current one."
6365   (interactive)
6366   (gnus-summary-prev-article
6367    (or (not (eq gnus-summary-goto-unread 'never))
6368        (gnus-summary-first-article-p (gnus-summary-article-number)))
6369    (and gnus-auto-select-same
6370         (gnus-summary-article-subject))))
6371
6372 (defun gnus-summary-next-page (&optional lines circular)
6373   "Show next page of the selected article.
6374 If at the end of the current article, select the next article.
6375 LINES says how many lines should be scrolled up.
6376
6377 If CIRCULAR is non-nil, go to the start of the article instead of
6378 selecting the next article when reaching the end of the current
6379 article."
6380   (interactive "P")
6381   (setq gnus-summary-buffer (current-buffer))
6382   (gnus-set-global-variables)
6383   (let ((article (gnus-summary-article-number))
6384         (article-window (get-buffer-window gnus-article-buffer t))
6385         endp)
6386     ;; If the buffer is empty, we have no article.
6387     (unless article
6388       (error "No article to select"))
6389     (gnus-configure-windows 'article)
6390     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6391         (if (and (eq gnus-summary-goto-unread 'never)
6392                  (not (gnus-summary-last-article-p article)))
6393             (gnus-summary-next-article)
6394           (gnus-summary-next-unread-article))
6395       (if (or (null gnus-current-article)
6396               (null gnus-article-current)
6397               (/= article (cdr gnus-article-current))
6398               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6399           ;; Selected subject is different from current article's.
6400           (gnus-summary-display-article article)
6401         (when article-window
6402           (gnus-eval-in-buffer-window gnus-article-buffer
6403             (setq endp (gnus-article-next-page lines)))
6404           (when endp
6405             (cond (circular
6406                    (gnus-summary-beginning-of-article))
6407                   (lines
6408                    (gnus-message 3 "End of message"))
6409                   ((null lines)
6410                    (if (and (eq gnus-summary-goto-unread 'never)
6411                             (not (gnus-summary-last-article-p article)))
6412                        (gnus-summary-next-article)
6413                      (gnus-summary-next-unread-article))))))))
6414     (gnus-summary-recenter)
6415     (gnus-summary-position-point)))
6416
6417 (defun gnus-summary-prev-page (&optional lines move)
6418   "Show previous page of selected article.
6419 Argument LINES specifies lines to be scrolled down.
6420 If MOVE, move to the previous unread article if point is at
6421 the beginning of the buffer."
6422   (interactive "P")
6423   (let ((article (gnus-summary-article-number))
6424         (article-window (get-buffer-window gnus-article-buffer t))
6425         endp)
6426     (gnus-configure-windows 'article)
6427     (if (or (null gnus-current-article)
6428             (null gnus-article-current)
6429             (/= article (cdr gnus-article-current))
6430             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6431         ;; Selected subject is different from current article's.
6432         (gnus-summary-display-article article)
6433       (gnus-summary-recenter)
6434       (when article-window
6435         (gnus-eval-in-buffer-window gnus-article-buffer
6436           (setq endp (gnus-article-prev-page lines)))
6437         (when (and move endp)
6438           (cond (lines
6439                  (gnus-message 3 "Beginning of message"))
6440                 ((null lines)
6441                  (if (and (eq gnus-summary-goto-unread 'never)
6442                           (not (gnus-summary-first-article-p article)))
6443                      (gnus-summary-prev-article)
6444                    (gnus-summary-prev-unread-article))))))))
6445   (gnus-summary-position-point))
6446
6447 (defun gnus-summary-prev-page-or-article (&optional lines)
6448   "Show previous page of selected article.
6449 Argument LINES specifies lines to be scrolled down.
6450 If at the beginning of the article, go to the next article."
6451   (interactive "P")
6452   (gnus-summary-prev-page lines t))
6453
6454 (defun gnus-summary-scroll-up (lines)
6455   "Scroll up (or down) one line current article.
6456 Argument LINES specifies lines to be scrolled up (or down if negative)."
6457   (interactive "p")
6458   (gnus-configure-windows 'article)
6459   (gnus-summary-show-thread)
6460   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6461     (gnus-eval-in-buffer-window gnus-article-buffer
6462       (cond ((> lines 0)
6463              (when (gnus-article-next-page lines)
6464                (gnus-message 3 "End of message")))
6465             ((< lines 0)
6466              (gnus-article-prev-page (- lines))))))
6467   (gnus-summary-recenter)
6468   (gnus-summary-position-point))
6469
6470 (defun gnus-summary-scroll-down (lines)
6471   "Scroll down (or up) one line current article.
6472 Argument LINES specifies lines to be scrolled down (or up if negative)."
6473   (interactive "p")
6474   (gnus-summary-scroll-up (- lines)))
6475
6476 (defun gnus-summary-next-same-subject ()
6477   "Select next article which has the same subject as current one."
6478   (interactive)
6479   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6480
6481 (defun gnus-summary-prev-same-subject ()
6482   "Select previous article which has the same subject as current one."
6483   (interactive)
6484   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6485
6486 (defun gnus-summary-next-unread-same-subject ()
6487   "Select next unread article which has the same subject as current one."
6488   (interactive)
6489   (gnus-summary-next-article t (gnus-summary-article-subject)))
6490
6491 (defun gnus-summary-prev-unread-same-subject ()
6492   "Select previous unread article which has the same subject as current one."
6493   (interactive)
6494   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6495
6496 (defun gnus-summary-first-unread-article ()
6497   "Select the first unread article.
6498 Return nil if there are no unread articles."
6499   (interactive)
6500   (prog1
6501       (when (gnus-summary-first-subject t)
6502         (gnus-summary-show-thread)
6503         (gnus-summary-first-subject t)
6504         (gnus-summary-display-article (gnus-summary-article-number)))
6505     (gnus-summary-position-point)))
6506
6507 (defun gnus-summary-first-unread-subject ()
6508   "Place the point on the subject line of the first unread article.
6509 Return nil if there are no unread articles."
6510   (interactive)
6511   (prog1
6512       (when (gnus-summary-first-subject t)
6513         (gnus-summary-show-thread)
6514         (gnus-summary-first-subject t))
6515     (gnus-summary-position-point)))
6516
6517 (defun gnus-summary-first-article ()
6518   "Select the first article.
6519 Return nil if there are no articles."
6520   (interactive)
6521   (prog1
6522       (when (gnus-summary-first-subject)
6523         (gnus-summary-show-thread)
6524         (gnus-summary-first-subject)
6525         (gnus-summary-display-article (gnus-summary-article-number)))
6526     (gnus-summary-position-point)))
6527
6528 (defun gnus-summary-best-unread-article ()
6529   "Select the unread article with the highest score."
6530   (interactive)
6531   (let ((best -1000000)
6532         (data gnus-newsgroup-data)
6533         article score)
6534     (while data
6535       (and (gnus-data-unread-p (car data))
6536            (> (setq score
6537                     (gnus-summary-article-score (gnus-data-number (car data))))
6538               best)
6539            (setq best score
6540                  article (gnus-data-number (car data))))
6541       (setq data (cdr data)))
6542     (prog1
6543         (if article
6544             (gnus-summary-goto-article article)
6545           (error "No unread articles"))
6546       (gnus-summary-position-point))))
6547
6548 (defun gnus-summary-last-subject ()
6549   "Go to the last displayed subject line in the group."
6550   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6551     (when article
6552       (gnus-summary-goto-subject article))))
6553
6554 (defun gnus-summary-goto-article (article &optional all-headers force)
6555   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6556 If ALL-HEADERS is non-nil, no header lines are hidden.
6557 If FORCE, go to the article even if it isn't displayed.  If FORCE
6558 is a number, it is the line the article is to be displayed on."
6559   (interactive
6560    (list
6561     (completing-read
6562      "Article number or Message-ID: "
6563      (mapcar (lambda (number) (list (int-to-string number)))
6564              gnus-newsgroup-limit))
6565     current-prefix-arg
6566     t))
6567   (prog1
6568       (if (and (stringp article)
6569                (string-match "@" article))
6570           (gnus-summary-refer-article article)
6571         (when (stringp article)
6572           (setq article (string-to-number article)))
6573         (if (gnus-summary-goto-subject article force)
6574             (gnus-summary-display-article article all-headers)
6575           (gnus-message 4 "Couldn't go to article %s" article) nil))
6576     (gnus-summary-position-point)))
6577
6578 (defun gnus-summary-goto-last-article ()
6579   "Go to the previously read article."
6580   (interactive)
6581   (prog1
6582       (when gnus-last-article
6583         (gnus-summary-goto-article gnus-last-article nil t))
6584     (gnus-summary-position-point)))
6585
6586 (defun gnus-summary-pop-article (number)
6587   "Pop one article off the history and go to the previous.
6588 NUMBER articles will be popped off."
6589   (interactive "p")
6590   (let (to)
6591     (setq gnus-newsgroup-history
6592           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6593     (if to
6594         (gnus-summary-goto-article (car to) nil t)
6595       (error "Article history empty")))
6596   (gnus-summary-position-point))
6597
6598 ;; Summary commands and functions for limiting the summary buffer.
6599
6600 (defun gnus-summary-limit-to-articles (n)
6601   "Limit the summary buffer to the next N articles.
6602 If not given a prefix, use the process marked articles instead."
6603   (interactive "P")
6604   (prog1
6605       (let ((articles (gnus-summary-work-articles n)))
6606         (setq gnus-newsgroup-processable nil)
6607         (gnus-summary-limit articles))
6608     (gnus-summary-position-point)))
6609
6610 (defun gnus-summary-pop-limit (&optional total)
6611   "Restore the previous limit.
6612 If given a prefix, remove all limits."
6613   (interactive "P")
6614   (when total
6615     (setq gnus-newsgroup-limits
6616           (list (mapcar (lambda (h) (mail-header-number h))
6617                         gnus-newsgroup-headers))))
6618   (unless gnus-newsgroup-limits
6619     (error "No limit to pop"))
6620   (prog1
6621       (gnus-summary-limit nil 'pop)
6622     (gnus-summary-position-point)))
6623
6624 (defun gnus-summary-limit-to-subject (subject &optional header)
6625   "Limit the summary buffer to articles that have subjects that match a regexp."
6626   (interactive "sLimit to subject (regexp): ")
6627   (unless header
6628     (setq header "subject"))
6629   (when (not (equal "" subject))
6630     (prog1
6631         (let ((articles (gnus-summary-find-matching
6632                          (or header "subject") subject 'all)))
6633           (unless articles
6634             (error "Found no matches for \"%s\"" subject))
6635           (gnus-summary-limit articles))
6636       (gnus-summary-position-point))))
6637
6638 (defun gnus-summary-limit-to-author (from)
6639   "Limit the summary buffer to articles that have authors that match a regexp."
6640   (interactive "sLimit to author (regexp): ")
6641   (gnus-summary-limit-to-subject from "from"))
6642
6643 (defun gnus-summary-limit-to-age (age &optional younger-p)
6644   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6645 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6646 articles that are younger than AGE days."
6647   (interactive
6648    (let ((younger current-prefix-arg)
6649          (days-got nil)
6650          days)
6651      (while (not days-got)
6652        (setq days (if younger
6653                       (read-string "Limit to articles within (in days): ")
6654                     (read-string "Limit to articles older than (in days): ")))
6655        (when (> (length days) 0)
6656          (setq days (read days)))
6657        (if (numberp days)
6658            (setq days-got t)
6659          (message "Please enter a number.")
6660          (sleep-for 1)))
6661      (list days younger)))
6662   (prog1
6663       (let ((data gnus-newsgroup-data)
6664             (cutoff (days-to-time age))
6665             articles d date is-younger)
6666         (while (setq d (pop data))
6667           (when (and (vectorp (gnus-data-header d))
6668                      (setq date (mail-header-date (gnus-data-header d))))
6669             (setq is-younger (time-less-p
6670                               (time-since (condition-case ()
6671                                               (date-to-time date)
6672                                             (error '(0 0))))
6673                               cutoff))
6674             (when (if younger-p
6675                       is-younger
6676                     (not is-younger))
6677               (push (gnus-data-number d) articles))))
6678         (gnus-summary-limit (nreverse articles)))
6679     (gnus-summary-position-point)))
6680
6681 (defun gnus-summary-limit-to-extra (header regexp)
6682   "Limit the summary buffer to articles that match an 'extra' header."
6683   (interactive
6684    (let ((header
6685           (intern
6686            (gnus-completing-read
6687             (symbol-name (car gnus-extra-headers))
6688             "Limit extra header:"
6689             (mapcar (lambda (x)
6690                       (cons (symbol-name x) x))
6691                     gnus-extra-headers)
6692             nil
6693             t))))
6694      (list header
6695            (read-string (format "Limit to header %s (regexp): " header)))))
6696   (when (not (equal "" regexp))
6697     (prog1
6698         (let ((articles (gnus-summary-find-matching
6699                          (cons 'extra header) regexp 'all)))
6700           (unless articles
6701             (error "Found no matches for \"%s\"" regexp))
6702           (gnus-summary-limit articles))
6703       (gnus-summary-position-point))))
6704
6705 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6706 (make-obsolete
6707  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6708
6709 (defun gnus-summary-limit-to-unread (&optional all)
6710   "Limit the summary buffer to articles that are not marked as read.
6711 If ALL is non-nil, limit strictly to unread articles."
6712   (interactive "P")
6713   (if all
6714       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6715     (gnus-summary-limit-to-marks
6716      ;; Concat all the marks that say that an article is read and have
6717      ;; those removed.
6718      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6719            gnus-killed-mark gnus-kill-file-mark
6720            gnus-low-score-mark gnus-expirable-mark
6721            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6722            gnus-duplicate-mark gnus-souped-mark)
6723      'reverse)))
6724
6725 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6726 (make-obsolete 'gnus-summary-delete-marked-with
6727                'gnus-summary-limit-exlude-marks)
6728
6729 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6730   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6731 If REVERSE, limit the summary buffer to articles that are marked
6732 with MARKS.  MARKS can either be a string of marks or a list of marks.
6733 Returns how many articles were removed."
6734   (interactive "sMarks: ")
6735   (gnus-summary-limit-to-marks marks t))
6736
6737 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6738   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6739 If REVERSE (the prefix), limit the summary buffer to articles that are
6740 not marked with MARKS.  MARKS can either be a string of marks or a
6741 list of marks.
6742 Returns how many articles were removed."
6743   (interactive "sMarks: \nP")
6744   (prog1
6745       (let ((data gnus-newsgroup-data)
6746             (marks (if (listp marks) marks
6747                      (append marks nil))) ; Transform to list.
6748             articles)
6749         (while data
6750           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6751                   (memq (gnus-data-mark (car data)) marks))
6752             (push (gnus-data-number (car data)) articles))
6753           (setq data (cdr data)))
6754         (gnus-summary-limit articles))
6755     (gnus-summary-position-point)))
6756
6757 (defun gnus-summary-limit-to-score (&optional score)
6758   "Limit to articles with score at or above SCORE."
6759   (interactive "P")
6760   (setq score (if score
6761                   (prefix-numeric-value score)
6762                 (or gnus-summary-default-score 0)))
6763   (let ((data gnus-newsgroup-data)
6764         articles)
6765     (while data
6766       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6767                 score)
6768         (push (gnus-data-number (car data)) articles))
6769       (setq data (cdr data)))
6770     (prog1
6771         (gnus-summary-limit articles)
6772       (gnus-summary-position-point))))
6773
6774 (defun gnus-summary-limit-include-thread (id)
6775   "Display all the hidden articles that is in the thread with ID in it.
6776 When called interactively, ID is the Message-ID of the current
6777 article."
6778   (interactive (list (mail-header-id (gnus-summary-article-header))))
6779   (let ((articles (gnus-articles-in-thread
6780                    (gnus-id-to-thread (gnus-root-id id)))))
6781     (prog1
6782         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6783         (gnus-summary-limit-include-matching-articles
6784          "subject"
6785          (regexp-quote (gnus-simplify-subject-re
6786                         (mail-header-subject (gnus-id-to-header id)))))
6787       (gnus-summary-position-point))))
6788
6789 (defun gnus-summary-limit-include-matching-articles (header regexp)
6790   "Display all the hidden articles that have HEADERs that match REGEXP."
6791   (interactive (list (read-string "Match on header: ")
6792                      (read-string "Regexp: ")))
6793   (let ((articles (gnus-find-matching-articles header regexp)))
6794     (prog1
6795         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6796       (gnus-summary-position-point))))
6797
6798 (defun gnus-summary-limit-include-dormant ()
6799   "Display all the hidden articles that are marked as dormant.
6800 Note that this command only works on a subset of the articles currently
6801 fetched for this group."
6802   (interactive)
6803   (unless gnus-newsgroup-dormant
6804     (error "There are no dormant articles in this group"))
6805   (prog1
6806       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6807     (gnus-summary-position-point)))
6808
6809 (defun gnus-summary-limit-exclude-dormant ()
6810   "Hide all dormant articles."
6811   (interactive)
6812   (prog1
6813       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6814     (gnus-summary-position-point)))
6815
6816 (defun gnus-summary-limit-exclude-childless-dormant ()
6817   "Hide all dormant articles that have no children."
6818   (interactive)
6819   (let ((data (gnus-data-list t))
6820         articles d children)
6821     ;; Find all articles that are either not dormant or have
6822     ;; children.
6823     (while (setq d (pop data))
6824       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6825                 (and (setq children
6826                            (gnus-article-children (gnus-data-number d)))
6827                      (let (found)
6828                        (while children
6829                          (when (memq (car children) articles)
6830                            (setq children nil
6831                                  found t))
6832                          (pop children))
6833                        found)))
6834         (push (gnus-data-number d) articles)))
6835     ;; Do the limiting.
6836     (prog1
6837         (gnus-summary-limit articles)
6838       (gnus-summary-position-point))))
6839
6840 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6841   "Mark all unread excluded articles as read.
6842 If ALL, mark even excluded ticked and dormants as read."
6843   (interactive "P")
6844   (let ((articles (gnus-sorted-complement
6845                    (sort
6846                     (mapcar (lambda (h) (mail-header-number h))
6847                             gnus-newsgroup-headers)
6848                     '<)
6849                    (sort gnus-newsgroup-limit '<)))
6850         article)
6851     (setq gnus-newsgroup-unreads
6852           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6853     (if all
6854         (setq gnus-newsgroup-dormant nil
6855               gnus-newsgroup-marked nil
6856               gnus-newsgroup-reads
6857               (nconc
6858                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6859                gnus-newsgroup-reads))
6860       (while (setq article (pop articles))
6861         (unless (or (memq article gnus-newsgroup-dormant)
6862                     (memq article gnus-newsgroup-marked))
6863           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6864
6865 (defun gnus-summary-limit (articles &optional pop)
6866   (if pop
6867       ;; We pop the previous limit off the stack and use that.
6868       (setq articles (car gnus-newsgroup-limits)
6869             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6870     ;; We use the new limit, so we push the old limit on the stack.
6871     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6872   ;; Set the limit.
6873   (setq gnus-newsgroup-limit articles)
6874   (let ((total (length gnus-newsgroup-data))
6875         (data (gnus-data-find-list (gnus-summary-article-number)))
6876         (gnus-summary-mark-below nil)   ; Inhibit this.
6877         found)
6878     ;; This will do all the work of generating the new summary buffer
6879     ;; according to the new limit.
6880     (gnus-summary-prepare)
6881     ;; Hide any threads, possibly.
6882     (and gnus-show-threads
6883          gnus-thread-hide-subtree
6884          (gnus-summary-hide-all-threads))
6885     ;; Try to return to the article you were at, or one in the
6886     ;; neighborhood.
6887     (when data
6888       ;; We try to find some article after the current one.
6889       (while data
6890         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6891           (setq data nil
6892                 found t))
6893         (setq data (cdr data))))
6894     (unless found
6895       ;; If there is no data, that means that we were after the last
6896       ;; article.  The same goes when we can't find any articles
6897       ;; after the current one.
6898       (goto-char (point-max))
6899       (gnus-summary-find-prev))
6900     (gnus-set-mode-line 'summary)
6901     ;; We return how many articles were removed from the summary
6902     ;; buffer as a result of the new limit.
6903     (- total (length gnus-newsgroup-data))))
6904
6905 (defsubst gnus-invisible-cut-children (threads)
6906   (let ((num 0))
6907     (while threads
6908       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6909         (incf num))
6910       (pop threads))
6911     (< num 2)))
6912
6913 (defsubst gnus-cut-thread (thread)
6914   "Go forwards in the thread until we find an article that we want to display."
6915   (when (or (eq gnus-fetch-old-headers 'some)
6916             (eq gnus-fetch-old-headers 'invisible)
6917             (numberp gnus-fetch-old-headers)
6918             (eq gnus-build-sparse-threads 'some)
6919             (eq gnus-build-sparse-threads 'more))
6920     ;; Deal with old-fetched headers and sparse threads.
6921     (while (and
6922             thread
6923             (or
6924              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6925              (gnus-summary-article-ancient-p
6926               (mail-header-number (car thread))))
6927             (if (or (<= (length (cdr thread)) 1)
6928                     (eq gnus-fetch-old-headers 'invisible))
6929                 (setq gnus-newsgroup-limit
6930                       (delq (mail-header-number (car thread))
6931                             gnus-newsgroup-limit)
6932                       thread (cadr thread))
6933               (when (gnus-invisible-cut-children (cdr thread))
6934                 (let ((th (cdr thread)))
6935                   (while th
6936                     (if (memq (mail-header-number (caar th))
6937                               gnus-newsgroup-limit)
6938                         (setq thread (car th)
6939                               th nil)
6940                       (setq th (cdr th))))))))))
6941   thread)
6942
6943 (defun gnus-cut-threads (threads)
6944   "Cut off all uninteresting articles from the beginning of threads."
6945   (when (or (eq gnus-fetch-old-headers 'some)
6946             (eq gnus-fetch-old-headers 'invisible)
6947             (numberp gnus-fetch-old-headers)
6948             (eq gnus-build-sparse-threads 'some)
6949             (eq gnus-build-sparse-threads 'more))
6950     (let ((th threads))
6951       (while th
6952         (setcar th (gnus-cut-thread (car th)))
6953         (setq th (cdr th)))))
6954   ;; Remove nixed out threads.
6955   (delq nil threads))
6956
6957 (defun gnus-summary-initial-limit (&optional show-if-empty)
6958   "Figure out what the initial limit is supposed to be on group entry.
6959 This entails weeding out unwanted dormants, low-scored articles,
6960 fetch-old-headers verbiage, and so on."
6961   ;; Most groups have nothing to remove.
6962   (if (or gnus-inhibit-limiting
6963           (and (null gnus-newsgroup-dormant)
6964                (not (eq gnus-fetch-old-headers 'some))
6965                (not (numberp gnus-fetch-old-headers))
6966                (not (eq gnus-fetch-old-headers 'invisible))
6967                (null gnus-summary-expunge-below)
6968                (not (eq gnus-build-sparse-threads 'some))
6969                (not (eq gnus-build-sparse-threads 'more))
6970                (null gnus-thread-expunge-below)
6971                (not gnus-use-nocem)))
6972       ()                                ; Do nothing.
6973     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6974     (setq gnus-newsgroup-limit nil)
6975     (mapatoms
6976      (lambda (node)
6977        (unless (car (symbol-value node))
6978          ;; These threads have no parents -- they are roots.
6979          (let ((nodes (cdr (symbol-value node)))
6980                thread)
6981            (while nodes
6982              (if (and gnus-thread-expunge-below
6983                       (< (gnus-thread-total-score (car nodes))
6984                          gnus-thread-expunge-below))
6985                  (gnus-expunge-thread (pop nodes))
6986                (setq thread (pop nodes))
6987                (gnus-summary-limit-children thread))))))
6988      gnus-newsgroup-dependencies)
6989     ;; If this limitation resulted in an empty group, we might
6990     ;; pop the previous limit and use it instead.
6991     (when (and (not gnus-newsgroup-limit)
6992                show-if-empty)
6993       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6994     gnus-newsgroup-limit))
6995
6996 (defun gnus-summary-limit-children (thread)
6997   "Return 1 if this subthread is visible and 0 if it is not."
6998   ;; First we get the number of visible children to this thread.  This
6999   ;; is done by recursing down the thread using this function, so this
7000   ;; will really go down to a leaf article first, before slowly
7001   ;; working its way up towards the root.
7002   (when thread
7003     (let ((children
7004            (if (cdr thread)
7005                (apply '+ (mapcar 'gnus-summary-limit-children
7006                                  (cdr thread)))
7007              0))
7008           (number (mail-header-number (car thread)))
7009           score)
7010       (if (and
7011            (not (memq number gnus-newsgroup-marked))
7012            (or
7013             ;; If this article is dormant and has absolutely no visible
7014             ;; children, then this article isn't visible.
7015             (and (memq number gnus-newsgroup-dormant)
7016                  (zerop children))
7017             ;; If this is "fetch-old-headered" and there is no
7018             ;; visible children, then we don't want this article.
7019             (and (or (eq gnus-fetch-old-headers 'some)
7020                      (numberp gnus-fetch-old-headers))
7021                  (gnus-summary-article-ancient-p number)
7022                  (zerop children))
7023             ;; If this is "fetch-old-headered" and `invisible', then
7024             ;; we don't want this article.
7025             (and (eq gnus-fetch-old-headers 'invisible)
7026                  (gnus-summary-article-ancient-p number))
7027             ;; If this is a sparsely inserted article with no children,
7028             ;; we don't want it.
7029             (and (eq gnus-build-sparse-threads 'some)
7030                  (gnus-summary-article-sparse-p number)
7031                  (zerop children))
7032             ;; If we use expunging, and this article is really
7033             ;; low-scored, then we don't want this article.
7034             (when (and gnus-summary-expunge-below
7035                        (< (setq score
7036                                 (or (cdr (assq number gnus-newsgroup-scored))
7037                                     gnus-summary-default-score))
7038                           gnus-summary-expunge-below))
7039               ;; We increase the expunge-tally here, but that has
7040               ;; nothing to do with the limits, really.
7041               (incf gnus-newsgroup-expunged-tally)
7042               ;; We also mark as read here, if that's wanted.
7043               (when (and gnus-summary-mark-below
7044                          (< score gnus-summary-mark-below))
7045                 (setq gnus-newsgroup-unreads
7046                       (delq number gnus-newsgroup-unreads))
7047                 (if gnus-newsgroup-auto-expire
7048                     (push number gnus-newsgroup-expirable)
7049                   (push (cons number gnus-low-score-mark)
7050                         gnus-newsgroup-reads)))
7051               t)
7052             ;; Check NoCeM things.
7053             (if (and gnus-use-nocem
7054                      (gnus-nocem-unwanted-article-p
7055                       (mail-header-id (car thread))))
7056                 (progn
7057                   (setq gnus-newsgroup-unreads
7058                         (delq number gnus-newsgroup-unreads))
7059                   t))))
7060           ;; Nope, invisible article.
7061           0
7062         ;; Ok, this article is to be visible, so we add it to the limit
7063         ;; and return 1.
7064         (push number gnus-newsgroup-limit)
7065         1))))
7066
7067 (defun gnus-expunge-thread (thread)
7068   "Mark all articles in THREAD as read."
7069   (let* ((number (mail-header-number (car thread))))
7070     (incf gnus-newsgroup-expunged-tally)
7071     ;; We also mark as read here, if that's wanted.
7072     (setq gnus-newsgroup-unreads
7073           (delq number gnus-newsgroup-unreads))
7074     (if gnus-newsgroup-auto-expire
7075         (push number gnus-newsgroup-expirable)
7076       (push (cons number gnus-low-score-mark)
7077             gnus-newsgroup-reads)))
7078   ;; Go recursively through all subthreads.
7079   (mapcar 'gnus-expunge-thread (cdr thread)))
7080
7081 ;; Summary article oriented commands
7082
7083 (defun gnus-summary-refer-parent-article (n)
7084   "Refer parent article N times.
7085 If N is negative, go to ancestor -N instead.
7086 The difference between N and the number of articles fetched is returned."
7087   (interactive "p")
7088   (let ((skip 1)
7089         error header ref)
7090     (when (not (natnump n))
7091       (setq skip (abs n)
7092             n 1))
7093     (while (and (> n 0)
7094                 (not error))
7095       (setq header (gnus-summary-article-header))
7096       (if (and (eq (mail-header-number header)
7097                    (cdr gnus-article-current))
7098                (equal gnus-newsgroup-name
7099                       (car gnus-article-current)))
7100           ;; If we try to find the parent of the currently
7101           ;; displayed article, then we take a look at the actual
7102           ;; References header, since this is slightly more
7103           ;; reliable than the References field we got from the
7104           ;; server.
7105           (save-excursion
7106             (set-buffer gnus-original-article-buffer)
7107             (nnheader-narrow-to-headers)
7108             (unless (setq ref (message-fetch-field "references"))
7109               (setq ref (message-fetch-field "in-reply-to")))
7110             (widen))
7111         (setq ref
7112               ;; It's not the current article, so we take a bet on
7113               ;; the value we got from the server.
7114               (mail-header-references header)))
7115       (if (and ref
7116                (not (equal ref "")))
7117           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7118             (gnus-message 1 "Couldn't find parent"))
7119         (gnus-message 1 "No references in article %d"
7120                       (gnus-summary-article-number))
7121         (setq error t))
7122       (decf n))
7123     (gnus-summary-position-point)
7124     n))
7125
7126 (defun gnus-summary-refer-references ()
7127   "Fetch all articles mentioned in the References header.
7128 Return the number of articles fetched."
7129   (interactive)
7130   (let ((ref (mail-header-references (gnus-summary-article-header)))
7131         (current (gnus-summary-article-number))
7132         (n 0))
7133     (if (or (not ref)
7134             (equal ref ""))
7135         (error "No References in the current article")
7136       ;; For each Message-ID in the References header...
7137       (while (string-match "<[^>]*>" ref)
7138         (incf n)
7139         ;; ... fetch that article.
7140         (gnus-summary-refer-article
7141          (prog1 (match-string 0 ref)
7142            (setq ref (substring ref (match-end 0))))))
7143       (gnus-summary-goto-subject current)
7144       (gnus-summary-position-point)
7145       n)))
7146
7147 (defun gnus-summary-refer-thread (&optional limit)
7148   "Fetch all articles in the current thread.
7149 If LIMIT (the numerical prefix), fetch that many old headers instead
7150 of what's specified by the `gnus-refer-thread-limit' variable."
7151   (interactive "P")
7152   (let ((id (mail-header-id (gnus-summary-article-header)))
7153         (limit (if limit (prefix-numeric-value limit)
7154                  gnus-refer-thread-limit)))
7155     ;; We want to fetch LIMIT *old* headers, but we also have to
7156     ;; re-fetch all the headers in the current buffer, because many of
7157     ;; them may be undisplayed.  So we adjust LIMIT.
7158     (when (numberp limit)
7159       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7160     (unless (eq gnus-fetch-old-headers 'invisible)
7161       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7162       ;; Retrieve the headers and read them in.
7163       (if (eq (gnus-retrieve-headers
7164                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7165               'nov)
7166           (gnus-build-all-threads)
7167         (error "Can't fetch thread from backends that don't support NOV"))
7168       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7169     (gnus-summary-limit-include-thread id)))
7170
7171 (defun gnus-summary-refer-article (message-id)
7172   "Fetch an article specified by MESSAGE-ID."
7173   (interactive "sMessage-ID: ")
7174   (when (and (stringp message-id)
7175              (not (zerop (length message-id))))
7176     ;; Construct the correct Message-ID if necessary.
7177     ;; Suggested by tale@pawl.rpi.edu.
7178     (unless (string-match "^<" message-id)
7179       (setq message-id (concat "<" message-id)))
7180     (unless (string-match ">$" message-id)
7181       (setq message-id (concat message-id ">")))
7182     (let* ((header (gnus-id-to-header message-id))
7183            (sparse (and header
7184                         (gnus-summary-article-sparse-p
7185                          (mail-header-number header))
7186                         (memq (mail-header-number header)
7187                               gnus-newsgroup-limit)))
7188            number)
7189       (cond
7190        ;; If the article is present in the buffer we just go to it.
7191        ((and header
7192              (or (not (gnus-summary-article-sparse-p
7193                        (mail-header-number header)))
7194                  sparse))
7195         (prog1
7196             (gnus-summary-goto-article
7197              (mail-header-number header) nil t)
7198           (when sparse
7199             (gnus-summary-update-article (mail-header-number header)))))
7200        (t
7201         ;; We fetch the article.
7202         (catch 'found
7203           (dolist (gnus-override-method (gnus-refer-article-methods))
7204             (gnus-check-server gnus-override-method)
7205             ;; Fetch the header, and display the article.
7206             (when (setq number (gnus-summary-insert-subject message-id))
7207               (gnus-summary-select-article nil nil nil number)
7208               (throw 'found t)))
7209           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7210
7211 (defun gnus-refer-article-methods ()
7212   "Return a list of referrable methods."
7213   (cond
7214    ;; No method, so we default to current and native.
7215    ((null gnus-refer-article-method)
7216     (list gnus-current-select-method gnus-select-method))
7217    ;; Current.
7218    ((eq 'current gnus-refer-article-method)
7219     (list gnus-current-select-method))
7220    ;; List of select methods.
7221    ((not (and (symbolp (car gnus-refer-article-method))
7222               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7223     (let (out)
7224       (dolist (method gnus-refer-article-method)
7225         (push (if (eq 'current method)
7226                   gnus-current-select-method
7227                 method)
7228               out))
7229       (nreverse out)))
7230    ;; One single select method.
7231    (t
7232     (list gnus-refer-article-method))))
7233
7234 (defun gnus-summary-edit-parameters ()
7235   "Edit the group parameters of the current group."
7236   (interactive)
7237   (gnus-group-edit-group gnus-newsgroup-name 'params))
7238
7239 (defun gnus-summary-customize-parameters ()
7240   "Customize the group parameters of the current group."
7241   (interactive)
7242   (gnus-group-customize gnus-newsgroup-name))
7243
7244 (defun gnus-summary-enter-digest-group (&optional force)
7245   "Enter an nndoc group based on the current article.
7246 If FORCE, force a digest interpretation.  If not, try
7247 to guess what the document format is."
7248   (interactive "P")
7249   (let ((conf gnus-current-window-configuration))
7250     (save-excursion
7251       (gnus-summary-select-article))
7252     (setq gnus-current-window-configuration conf)
7253     (let* ((name (format "%s-%d"
7254                          (gnus-group-prefixed-name
7255                           gnus-newsgroup-name (list 'nndoc ""))
7256                          (save-excursion
7257                            (set-buffer gnus-summary-buffer)
7258                            gnus-current-article)))
7259            (ogroup gnus-newsgroup-name)
7260            (params (append (gnus-info-params (gnus-get-info ogroup))
7261                            (list (cons 'to-group ogroup))
7262                            (list (cons 'save-article-group ogroup))))
7263            (case-fold-search t)
7264            (buf (current-buffer))
7265            dig to-address)
7266       (save-excursion
7267         (set-buffer gnus-original-article-buffer)
7268         ;; Have the digest group inherit the main mail address of
7269         ;; the parent article.
7270         (when (setq to-address (or (message-fetch-field "reply-to")
7271                                    (message-fetch-field "from")))
7272           (setq params (append
7273                         (list (cons 'to-address
7274                                     (funcall gnus-decode-encoded-word-function
7275                                              to-address))))))
7276         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7277         (insert-buffer-substring gnus-original-article-buffer)
7278         ;; Remove lines that may lead nndoc to misinterpret the
7279         ;; document type.
7280         (narrow-to-region
7281          (goto-char (point-min))
7282          (or (search-forward "\n\n" nil t) (point)))
7283         (goto-char (point-min))
7284         (delete-matching-lines "^Path:\\|^From ")
7285         (widen))
7286       (unwind-protect
7287           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7288                     (gnus-newsgroup-ephemeral-ignored-charsets
7289                      gnus-newsgroup-ignored-charsets))
7290                 (gnus-group-read-ephemeral-group
7291                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7292                               (nndoc-article-type
7293                                ,(if force 'mbox 'guess))) t))
7294               ;; Make all postings to this group go to the parent group.
7295               (nconc (gnus-info-params (gnus-get-info name))
7296                      params)
7297             ;; Couldn't select this doc group.
7298             (switch-to-buffer buf)
7299             (gnus-set-global-variables)
7300             (gnus-configure-windows 'summary)
7301             (gnus-message 3 "Article couldn't be entered?"))
7302         (kill-buffer dig)))))
7303
7304 (defun gnus-summary-read-document (n)
7305   "Open a new group based on the current article(s).
7306 This will allow you to read digests and other similar
7307 documents as newsgroups.
7308 Obeys the standard process/prefix convention."
7309   (interactive "P")
7310   (let* ((articles (gnus-summary-work-articles n))
7311          (ogroup gnus-newsgroup-name)
7312          (params (append (gnus-info-params (gnus-get-info ogroup))
7313                          (list (cons 'to-group ogroup))))
7314          article group egroup groups vgroup)
7315     (while (setq article (pop articles))
7316       (setq group (format "%s-%d" gnus-newsgroup-name article))
7317       (gnus-summary-remove-process-mark article)
7318       (when (gnus-summary-display-article article)
7319         (save-excursion
7320           (with-temp-buffer
7321             (insert-buffer-substring gnus-original-article-buffer)
7322             ;; Remove some headers that may lead nndoc to make
7323             ;; the wrong guess.
7324             (message-narrow-to-head)
7325             (goto-char (point-min))
7326             (delete-matching-lines "^\\(Path\\):\\|^From ")
7327             (widen)
7328             (if (setq egroup
7329                       (gnus-group-read-ephemeral-group
7330                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7331                                      (nndoc-article-type guess))
7332                        t nil t))
7333                 (progn
7334                   ;; Make all postings to this group go to the parent group.
7335                   (nconc (gnus-info-params (gnus-get-info egroup))
7336                          params)
7337                   (push egroup groups))
7338               ;; Couldn't select this doc group.
7339               (gnus-error 3 "Article couldn't be entered"))))))
7340     ;; Now we have selected all the documents.
7341     (cond
7342      ((not groups)
7343       (error "None of the articles could be interpreted as documents"))
7344      ((gnus-group-read-ephemeral-group
7345        (setq vgroup (format
7346                      "nnvirtual:%s-%s" gnus-newsgroup-name
7347                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7348        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7349        t
7350        (cons (current-buffer) 'summary)))
7351      (t
7352       (error "Couldn't select virtual nndoc group")))))
7353
7354 (defun gnus-summary-isearch-article (&optional regexp-p)
7355   "Do incremental search forward on the current article.
7356 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7357   (interactive "P")
7358   (let* ((gnus-inhibit-treatment t)
7359          (old (gnus-summary-select-article)))
7360     (gnus-configure-windows 'article)
7361     (gnus-eval-in-buffer-window gnus-article-buffer
7362       (save-restriction
7363         (widen)
7364         (when (eq 'old old)
7365           (gnus-article-show-all-headers))
7366         (goto-char (point-min))
7367         (isearch-forward regexp-p)))))
7368
7369 (defun gnus-summary-search-article-forward (regexp &optional backward)
7370   "Search for an article containing REGEXP forward.
7371 If BACKWARD, search backward instead."
7372   (interactive
7373    (list (read-string
7374           (format "Search article %s (regexp%s): "
7375                   (if current-prefix-arg "backward" "forward")
7376                   (if gnus-last-search-regexp
7377                       (concat ", default " gnus-last-search-regexp)
7378                     "")))
7379          current-prefix-arg))
7380   (if (string-equal regexp "")
7381       (setq regexp (or gnus-last-search-regexp ""))
7382     (setq gnus-last-search-regexp regexp)
7383     (setq gnus-article-before-search gnus-current-article))
7384   ;; Intentionally set gnus-last-article.
7385   (setq gnus-last-article gnus-article-before-search)
7386   (let ((gnus-last-article gnus-last-article))
7387     (if (gnus-summary-search-article regexp backward)
7388         (gnus-summary-show-thread)
7389       (error "Search failed: \"%s\"" regexp))))
7390
7391 (defun gnus-summary-search-article-backward (regexp)
7392   "Search for an article containing REGEXP backward."
7393   (interactive
7394    (list (read-string
7395           (format "Search article backward (regexp%s): "
7396                   (if gnus-last-search-regexp
7397                       (concat ", default " gnus-last-search-regexp)
7398                     "")))))
7399   (gnus-summary-search-article-forward regexp 'backward))
7400
7401 (eval-when-compile
7402   (defmacro gnus-summary-search-article-position-point (regexp backward)
7403     "Dehighlight the last matched text and goto the beginning position."
7404     (` (if (and gnus-summary-search-article-matched-data
7405                 (let ((text (caddr gnus-summary-search-article-matched-data))
7406                       (inhibit-read-only t)
7407                       buffer-read-only)
7408                   (delete-region
7409                    (goto-char (car gnus-summary-search-article-matched-data))
7410                    (cadr gnus-summary-search-article-matched-data))
7411                   (insert text)
7412                   (string-match (, regexp) text)))
7413            (if (, backward) (beginning-of-line) (end-of-line))
7414          (goto-char (if (, backward) (point-max) (point-min))))))
7415
7416   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7417     "Place point where X-Face image is displayed."
7418     (if (featurep 'xemacs)
7419         (` (let ((end (if (search-forward "\n\n" nil t)
7420                           (goto-char (1- (point)))
7421                         (point-min)))
7422                  extent)
7423              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7424              (unless (and (re-search-forward "^From:" end t)
7425                           (setq extent (extent-at (point)))
7426                           (extent-begin-glyph extent))
7427                (goto-char (, opoint)))))
7428       (` (let ((end (if (search-forward "\n\n" nil t)
7429                         (goto-char (1- (point)))
7430                       (point-min)))
7431                (start (or (search-backward "\n\n" nil t) (point-min))))
7432            (goto-char
7433             (or (text-property-any start end 'x-face-image t);; x-face-e21
7434                 (text-property-any start end 'x-face-mule-bitmap-image t)
7435                 (, opoint)))))))
7436
7437   (defmacro gnus-summary-search-article-highlight-matched-text
7438     (backward treated x-face)
7439     "Highlight matched text in the function `gnus-summary-search-article'."
7440     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7441              (end (set-marker (make-marker) (match-end 0)))
7442              (inhibit-read-only t)
7443              buffer-read-only)
7444          (unless treated
7445            (let ((,@
7446                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7447                     (mapcar
7448                      (lambda (item) (setq items (delq item items)))
7449                      '(gnus-treat-buttonize
7450                        gnus-treat-fill-article
7451                        gnus-treat-fill-long-lines
7452                        gnus-treat-emphasize
7453                        gnus-treat-highlight-headers
7454                        gnus-treat-highlight-citation
7455                        gnus-treat-highlight-signature
7456                        gnus-treat-overstrike
7457                        gnus-treat-display-xface
7458                        gnus-treat-buttonize-head
7459                        gnus-treat-decode-article-as-default-mime-charset))
7460                     (static-if (featurep 'xemacs)
7461                         items
7462                       (cons '(x-face-mule-delete-x-face-field
7463                               (quote never))
7464                             items))))
7465                  (gnus-treat-display-xface
7466                   (when (, x-face) gnus-treat-display-xface)))
7467              (gnus-article-prepare-mime-display)))
7468          (goto-char (if (, backward) start end))
7469          (when (, x-face)
7470            (gnus-summary-search-article-highlight-goto-x-face (point)))
7471          (setq gnus-summary-search-article-matched-data
7472                (list start end (buffer-substring start end)))
7473          (unless (eq start end);; matched text has been deleted. :-<
7474            (put-text-property start end 'face
7475                               (or (find-face 'isearch)
7476                                   'secondary-selection))))))
7477   )
7478
7479 (defun gnus-summary-search-article (regexp &optional backward)
7480   "Search for an article containing REGEXP.
7481 Optional argument BACKWARD means do search for backward.
7482 `gnus-select-article-hook' is not called during the search."
7483   ;; We have to require this here to make sure that the following
7484   ;; dynamic binding isn't shadowed by autoloading.
7485   (require 'gnus-async)
7486   (require 'gnus-art)
7487   (let ((gnus-select-article-hook nil)  ;Disable hook.
7488         (gnus-article-prepare-hook nil)
7489         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7490         (gnus-use-article-prefetch nil)
7491         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7492         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7493         (sum (current-buffer))
7494         (found nil)
7495         point treated)
7496     (gnus-save-hidden-threads
7497       (static-if (featurep 'xemacs)
7498           (let ((gnus-inhibit-treatment t))
7499             (setq treated (eq 'old (gnus-summary-select-article)))
7500             (when (and treated
7501                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7502                                  (window-live-p (get-buffer-window
7503                                                  gnus-article-buffer t)))))
7504               (gnus-summary-select-article nil t)
7505               (setq treated nil)))
7506         (let ((gnus-inhibit-treatment t)
7507               (x-face-mule-delete-x-face-field 'never))
7508           (setq treated (eq 'old (gnus-summary-select-article)))
7509           (when (and treated
7510                      (not
7511                       (and (gnus-buffer-live-p gnus-article-buffer)
7512                            (window-live-p (get-buffer-window
7513                                            gnus-article-buffer t))
7514                            (or (not (string-match "^\\^X-Face:" regexp))
7515                                (with-current-buffer gnus-article-buffer
7516                                  gnus-summary-search-article-matched-data)))))
7517             (gnus-summary-select-article nil t)
7518             (setq treated nil))))
7519       (set-buffer gnus-article-buffer)
7520       (widen)
7521       (if treated
7522           (progn
7523             (gnus-article-show-all-headers)
7524             (gnus-summary-search-article-position-point regexp backward))
7525         (goto-char (if backward (point-max) (point-min))))
7526       (while (not found)
7527         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7528         (if (if backward
7529                 (re-search-backward regexp nil t)
7530               (re-search-forward regexp nil t))
7531             ;; We found the regexp.
7532             (progn
7533               (gnus-summary-search-article-highlight-matched-text
7534                backward treated (string-match "^\\^X-Face:" regexp))
7535               (setq found 'found)
7536               (forward-line
7537                (/ (- 2 (window-height
7538                         (get-buffer-window gnus-article-buffer t)))
7539                   2))
7540               (set-window-start
7541                (get-buffer-window (current-buffer))
7542                (point))
7543               (set-buffer sum)
7544               (setq point (point)))
7545           ;; We didn't find it, so we go to the next article.
7546           (set-buffer sum)
7547           (setq found 'not)
7548           (while (eq found 'not)
7549             (if (not (if backward (gnus-summary-find-prev)
7550                        (gnus-summary-find-next)))
7551                 ;; No more articles.
7552                 (setq found t)
7553               ;; Select the next article and adjust point.
7554               (unless (gnus-summary-article-sparse-p
7555                        (gnus-summary-article-number))
7556                 (setq found nil)
7557                 (let ((gnus-inhibit-treatment t))
7558                   (gnus-summary-select-article))
7559                 (setq treated nil)
7560                 (set-buffer gnus-article-buffer)
7561                 (widen)
7562                 (goto-char (if backward (point-max) (point-min))))))))
7563       (gnus-message 7 ""))
7564     ;; Return whether we found the regexp.
7565     (when (eq found 'found)
7566       (goto-char point)
7567       (gnus-summary-show-thread)
7568       (gnus-summary-goto-subject gnus-current-article)
7569       (gnus-summary-position-point)
7570       t)))
7571
7572 (defun gnus-find-matching-articles (header regexp)
7573   "Return a list of all articles that match REGEXP on HEADER.
7574 This search includes all articles in the current group that Gnus has
7575 fetched headers for, whether they are displayed or not."
7576   (let ((articles nil)
7577         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7578         (case-fold-search t))
7579     (dolist (header gnus-newsgroup-headers)
7580       (when (string-match regexp (funcall func header))
7581         (push (mail-header-number header) articles)))
7582     (nreverse articles)))
7583
7584 (defun gnus-summary-find-matching (header regexp &optional backward unread
7585                                           not-case-fold)
7586   "Return a list of all articles that match REGEXP on HEADER.
7587 The search stars on the current article and goes forwards unless
7588 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7589 If UNREAD is non-nil, only unread articles will
7590 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7591 in the comparisons."
7592   (let ((case-fold-search (not not-case-fold))
7593         articles d func)
7594     (if (consp header)
7595         (if (eq (car header) 'extra)
7596             (setq func
7597                   `(lambda (h)
7598                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7599                          "")))
7600           (error "%s is an invalid header" header))
7601       (unless (fboundp (intern (concat "mail-header-" header)))
7602         (error "%s is not a valid header" header))
7603       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7604     (dolist (d (if (eq backward 'all)
7605                    gnus-newsgroup-data
7606                  (gnus-data-find-list
7607                   (gnus-summary-article-number)
7608                   (gnus-data-list backward))))
7609       (when (and (or (not unread)       ; We want all articles...
7610                      (gnus-data-unread-p d)) ; Or just unreads.
7611                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7612                  (string-match regexp
7613                                (funcall func (gnus-data-header d)))) ; Match.
7614         (push (gnus-data-number d) articles))) ; Success!
7615     (nreverse articles)))
7616
7617 (defun gnus-summary-execute-command (header regexp command &optional backward)
7618   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7619 If HEADER is an empty string (or nil), the match is done on the entire
7620 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7621   (interactive
7622    (list (let ((completion-ignore-case t))
7623            (completing-read
7624             "Header name: "
7625             (mapcar (lambda (header) (list (format "%s" header)))
7626                     (append
7627                      '("Number" "Subject" "From" "Lines" "Date"
7628                        "Message-ID" "Xref" "References" "Body")
7629                      gnus-extra-headers))
7630             nil 'require-match))
7631          (read-string "Regexp: ")
7632          (read-key-sequence "Command: ")
7633          current-prefix-arg))
7634   (when (equal header "Body")
7635     (setq header ""))
7636   ;; Hidden thread subtrees must be searched as well.
7637   (gnus-summary-show-all-threads)
7638   ;; We don't want to change current point nor window configuration.
7639   (save-excursion
7640     (save-window-excursion
7641       (gnus-message 6 "Executing %s..." (key-description command))
7642       ;; We'd like to execute COMMAND interactively so as to give arguments.
7643       (gnus-execute header regexp
7644                     `(call-interactively ',(key-binding command))
7645                     backward)
7646       (gnus-message 6 "Executing %s...done" (key-description command)))))
7647
7648 (defun gnus-summary-beginning-of-article ()
7649   "Scroll the article back to the beginning."
7650   (interactive)
7651   (gnus-summary-select-article)
7652   (gnus-configure-windows 'article)
7653   (gnus-eval-in-buffer-window gnus-article-buffer
7654     (widen)
7655     (goto-char (point-min))
7656     (when gnus-page-broken
7657       (gnus-narrow-to-page))))
7658
7659 (defun gnus-summary-end-of-article ()
7660   "Scroll to the end of the article."
7661   (interactive)
7662   (gnus-summary-select-article)
7663   (gnus-configure-windows 'article)
7664   (gnus-eval-in-buffer-window gnus-article-buffer
7665     (widen)
7666     (goto-char (point-max))
7667     (recenter -3)
7668     (when gnus-page-broken
7669       (gnus-narrow-to-page))))
7670
7671 (defun gnus-summary-print-article (&optional filename n)
7672   "Generate and print a PostScript image of the N next (mail) articles.
7673
7674 If N is negative, print the N previous articles.  If N is nil and articles
7675 have been marked with the process mark, print these instead.
7676
7677 If the optional first argument FILENAME is nil, send the image to the
7678 printer.  If FILENAME is a string, save the PostScript image in a file with
7679 that name.  If FILENAME is a number, prompt the user for the name of the file
7680 to save in."
7681   (interactive (list (ps-print-preprint current-prefix-arg)))
7682   (dolist (article (gnus-summary-work-articles n))
7683     (gnus-summary-select-article nil nil 'pseudo article)
7684     (gnus-eval-in-buffer-window gnus-article-buffer
7685       (let ((buffer (generate-new-buffer " *print*")))
7686         (unwind-protect
7687             (progn
7688               (copy-to-buffer buffer (point-min) (point-max))
7689               (set-buffer buffer)
7690               (gnus-article-delete-invisible-text)
7691               (when (gnus-visual-p 'article-highlight 'highlight)
7692                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7693                 ;; highlight.
7694                 (let ((gnus-article-buffer buffer))
7695                   (gnus-article-highlight-citation t)
7696                   (gnus-article-highlight-signature)))
7697               (let ((ps-left-header
7698                      (list
7699                       (concat "("
7700                               (mail-header-subject gnus-current-headers) ")")
7701                       (concat "("
7702                               (mail-header-from gnus-current-headers) ")")))
7703                     (ps-right-header
7704                      (list
7705                       "/pagenumberstring load"
7706                       (concat "("
7707                               (mail-header-date gnus-current-headers) ")"))))
7708                 (gnus-run-hooks 'gnus-ps-print-hook)
7709                 (save-excursion
7710                   (if window-system
7711                       (ps-spool-buffer-with-faces)
7712                     (ps-spool-buffer)))))
7713           (kill-buffer buffer))))
7714     (gnus-summary-remove-process-mark article))
7715   (ps-despool filename))
7716
7717 (defun gnus-summary-show-article (&optional arg)
7718   "Force re-fetching of the current article.
7719 If ARG (the prefix) is a number, show the article with the charset
7720 defined in `gnus-summary-show-article-charset-alist', or the charset
7721 inputed.
7722 If ARG (the prefix) is non-nil and not a number, show the raw article
7723 without any article massaging functions being run."
7724   (interactive "P")
7725   (cond
7726    ((numberp arg)
7727     (let ((gnus-newsgroup-charset
7728            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7729                (read-coding-system "Charset: ")))
7730           (gnus-newsgroup-ignored-charsets 'gnus-all))
7731       (gnus-summary-select-article nil 'force)
7732       (let ((deps gnus-newsgroup-dependencies)
7733             head header)
7734         (save-excursion
7735           (set-buffer gnus-original-article-buffer)
7736           (save-restriction
7737             (message-narrow-to-head)
7738             (setq head (buffer-string)))
7739           (with-temp-buffer
7740             (insert (format "211 %d Article retrieved.\n"
7741                             (cdr gnus-article-current)))
7742             (insert head)
7743             (insert ".\n")
7744             (let ((nntp-server-buffer (current-buffer)))
7745               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7746         (gnus-data-set-header
7747          (gnus-data-find (cdr gnus-article-current))
7748          header)
7749         (gnus-summary-update-article-line
7750          (cdr gnus-article-current) header))))
7751    ((not arg)
7752     ;; Select the article the normal way.
7753     (gnus-summary-select-article nil 'force))
7754    (t
7755     ;; We have to require this here to make sure that the following
7756     ;; dynamic binding isn't shadowed by autoloading.
7757     (require 'gnus-async)
7758     (require 'gnus-art)
7759     ;; Bind the article treatment functions to nil.
7760     (let ((gnus-have-all-headers t)
7761           gnus-article-prepare-hook
7762           gnus-article-decode-hook
7763           gnus-break-pages
7764           gnus-show-mime
7765           (gnus-inhibit-treatment t))
7766       (gnus-summary-select-article nil 'force))))
7767   (gnus-summary-goto-subject gnus-current-article)
7768   (gnus-summary-position-point))
7769
7770 (defun gnus-summary-verbose-headers (&optional arg)
7771   "Toggle permanent full header display.
7772 If ARG is a positive number, turn header display on.
7773 If ARG is a negative number, turn header display off."
7774   (interactive "P")
7775   (setq gnus-show-all-headers
7776         (cond ((or (not (numberp arg))
7777                    (zerop arg))
7778                (not gnus-show-all-headers))
7779               ((natnump arg)
7780                t)))
7781   (gnus-summary-show-article))
7782
7783 (defun gnus-summary-toggle-header (&optional arg)
7784   "Show the headers if they are hidden, or hide them if they are shown.
7785 If ARG is a positive number, show the entire header.
7786 If ARG is a negative number, hide the unwanted header lines."
7787   (interactive "P")
7788   (save-excursion
7789     (set-buffer gnus-article-buffer)
7790     (save-restriction
7791       (let* ((buffer-read-only nil)
7792              (inhibit-point-motion-hooks t)
7793              hidden e)
7794         (setq hidden
7795               (if (numberp arg)
7796                   (>= arg 0)
7797                 (save-restriction
7798                   (article-narrow-to-head)
7799                   (gnus-article-hidden-text-p 'headers))))
7800         (goto-char (point-min))
7801         (when (search-forward "\n\n" nil t)
7802           (delete-region (point-min) (1- (point))))
7803         (goto-char (point-min))
7804         (save-excursion
7805           (set-buffer gnus-original-article-buffer)
7806           (goto-char (point-min))
7807           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7808         (insert-buffer-substring gnus-original-article-buffer 1 e)
7809         (save-restriction
7810           (narrow-to-region (point-min) (point))
7811           (article-decode-encoded-words)
7812           (if  hidden
7813               (let ((gnus-treat-hide-headers nil)
7814                     (gnus-treat-hide-boring-headers nil))
7815                 (setq gnus-article-wash-types
7816                       (delq 'headers gnus-article-wash-types))
7817                 (gnus-treat-article 'head))
7818             (gnus-treat-article 'head)))
7819         (gnus-set-mode-line 'article)))))
7820
7821 (defun gnus-summary-show-all-headers ()
7822   "Make all header lines visible."
7823   (interactive)
7824   (gnus-summary-toggle-header 1))
7825
7826 (defun gnus-summary-toggle-mime (&optional arg)
7827   "Toggle MIME processing.
7828 If ARG is a positive number, turn MIME processing on."
7829   (interactive "P")
7830   (setq gnus-show-mime
7831         (if (null arg)
7832             (not gnus-show-mime)
7833           (> (prefix-numeric-value arg) 0)))
7834   (gnus-summary-select-article t 'force))
7835
7836 (defun gnus-summary-caesar-message (&optional arg)
7837   "Caesar rotate the current article by 13.
7838 The numerical prefix specifies how many places to rotate each letter
7839 forward."
7840   (interactive "P")
7841   (gnus-summary-select-article)
7842   (let ((mail-header-separator ""))
7843     (gnus-eval-in-buffer-window gnus-article-buffer
7844       (save-restriction
7845         (widen)
7846         (let ((start (window-start))
7847               buffer-read-only)
7848           (message-caesar-buffer-body arg)
7849           (set-window-start (get-buffer-window (current-buffer)) start))))))
7850
7851 (defun gnus-summary-stop-page-breaking ()
7852   "Stop page breaking in the current article."
7853   (interactive)
7854   (gnus-summary-select-article)
7855   (gnus-eval-in-buffer-window gnus-article-buffer
7856     (widen)
7857     (when (gnus-visual-p 'page-marker)
7858       (let ((buffer-read-only nil))
7859         (gnus-remove-text-with-property 'gnus-prev)
7860         (gnus-remove-text-with-property 'gnus-next))
7861       (setq gnus-page-broken nil))))
7862
7863 (defun gnus-summary-move-article (&optional n to-newsgroup
7864                                             select-method action)
7865   "Move the current article to a different newsgroup.
7866 If N is a positive number, move the N next articles.
7867 If N is a negative number, move the N previous articles.
7868 If N is nil and any articles have been marked with the process mark,
7869 move those articles instead.
7870 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7871 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7872 re-spool using this method.
7873
7874 For this function to work, both the current newsgroup and the
7875 newsgroup that you want to move to have to support the `request-move'
7876 and `request-accept' functions.
7877
7878 ACTION can be either `move' (the default), `crosspost' or `copy'."
7879   (interactive "P")
7880   (unless action
7881     (setq action 'move))
7882   ;; Disable marking as read.
7883   (let (gnus-mark-article-hook)
7884     (save-window-excursion
7885       (gnus-summary-select-article)))
7886   ;; Check whether the source group supports the required functions.
7887   (cond ((and (eq action 'move)
7888               (not (gnus-check-backend-function
7889                     'request-move-article gnus-newsgroup-name)))
7890          (error "The current group does not support article moving"))
7891         ((and (eq action 'crosspost)
7892               (not (gnus-check-backend-function
7893                     'request-replace-article gnus-newsgroup-name)))
7894          (error "The current group does not support article editing")))
7895   (let ((articles (gnus-summary-work-articles n))
7896         (prefix (if (gnus-check-backend-function
7897                     'request-move-article gnus-newsgroup-name)
7898                     (gnus-group-real-prefix gnus-newsgroup-name)
7899                   ""))
7900         (names '((move "Move" "Moving")
7901                  (copy "Copy" "Copying")
7902                  (crosspost "Crosspost" "Crossposting")))
7903         (copy-buf (save-excursion
7904                     (nnheader-set-temp-buffer " *copy article*")))
7905         (default-marks gnus-article-mark-lists)
7906         (no-expire-marks (delete '(expirable . expire)
7907                                  (copy-sequence gnus-article-mark-lists)))
7908         art-group to-method new-xref article to-groups)
7909     (unless (assq action names)
7910       (error "Unknown action %s" action))
7911     ;; Read the newsgroup name.
7912     (when (and (not to-newsgroup)
7913                (not select-method))
7914       (setq to-newsgroup
7915             (gnus-read-move-group-name
7916              (cadr (assq action names))
7917              (symbol-value (intern (format "gnus-current-%s-group" action)))
7918              articles prefix))
7919       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7920     (setq to-method (or select-method
7921                         (gnus-server-to-method
7922                          (gnus-group-method to-newsgroup))))
7923     ;; Check the method we are to move this article to...
7924     (unless (gnus-check-backend-function
7925              'request-accept-article (car to-method))
7926       (error "%s does not support article copying" (car to-method)))
7927     (unless (gnus-check-server to-method)
7928       (error "Can't open server %s" (car to-method)))
7929     (gnus-message 6 "%s to %s: %s..."
7930                   (caddr (assq action names))
7931                   (or (car select-method) to-newsgroup) articles)
7932     (while articles
7933       (setq article (pop articles))
7934       (setq
7935        art-group
7936        (cond
7937         ;; Move the article.
7938         ((eq action 'move)
7939          ;; Remove this article from future suppression.
7940          (gnus-dup-unsuppress-article article)
7941          (gnus-request-move-article
7942           article                       ; Article to move
7943           gnus-newsgroup-name           ; From newsgroup
7944           (nth 1 (gnus-find-method-for-group
7945                   gnus-newsgroup-name)) ; Server
7946           (list 'gnus-request-accept-article
7947                 to-newsgroup (list 'quote select-method)
7948                 (not articles) t)       ; Accept form
7949           (not articles)))              ; Only save nov last time
7950         ;; Copy the article.
7951         ((eq action 'copy)
7952          (save-excursion
7953            (set-buffer copy-buf)
7954            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7955              (gnus-request-accept-article
7956               to-newsgroup select-method (not articles) t))))
7957         ;; Crosspost the article.
7958         ((eq action 'crosspost)
7959          (let ((xref (message-tokenize-header
7960                       (mail-header-xref (gnus-summary-article-header article))
7961                       " ")))
7962            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7963                                   ":" article))
7964            (unless xref
7965              (setq xref (list (system-name))))
7966            (setq new-xref
7967                  (concat
7968                   (mapconcat 'identity
7969                              (delete "Xref:" (delete new-xref xref))
7970                              " ")
7971                   " " new-xref))
7972            (save-excursion
7973              (set-buffer copy-buf)
7974              ;; First put the article in the destination group.
7975              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7976              (when (consp (setq art-group
7977                                 (gnus-request-accept-article
7978                                  to-newsgroup select-method (not articles))))
7979                (setq new-xref (concat new-xref " " (car art-group)
7980                                       ":" (cdr art-group)))
7981                ;; Now we have the new Xrefs header, so we insert
7982                ;; it and replace the new article.
7983                (nnheader-replace-header "Xref" new-xref)
7984                (gnus-request-replace-article
7985                 (cdr art-group) to-newsgroup (current-buffer))
7986                art-group))))))
7987       (cond
7988        ((not art-group)
7989         (gnus-message 1 "Couldn't %s article %s: %s"
7990                       (cadr (assq action names)) article
7991                       (nnheader-get-report (car to-method))))
7992        ((eq art-group 'junk)
7993         (when (eq action 'move)
7994           (gnus-summary-mark-article article gnus-canceled-mark)
7995           (gnus-message 4 "Deleted article %s" article)))
7996        (t
7997         (let* ((pto-group (gnus-group-prefixed-name
7998                            (car art-group) to-method))
7999                (entry
8000                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8001                (info (nth 2 entry))
8002                (to-group (gnus-info-group info))
8003                to-marks)
8004           ;; Update the group that has been moved to.
8005           (when (and info
8006                      (memq action '(move copy)))
8007             (unless (member to-group to-groups)
8008               (push to-group to-groups))
8009
8010             (unless (memq article gnus-newsgroup-unreads)
8011               (push 'read to-marks)
8012               (gnus-info-set-read
8013                info (gnus-add-to-range (gnus-info-read info)
8014                                        (list (cdr art-group)))))
8015
8016             ;; See whether the article is to be put in the cache.
8017             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8018                              default-marks
8019                            no-expire-marks))
8020                   (to-article (cdr art-group)))
8021
8022               ;; Enter the article into the cache in the new group,
8023               ;; if that is required.
8024               (when gnus-use-cache
8025                 (gnus-cache-possibly-enter-article
8026                  to-group to-article
8027                  (let ((header (copy-sequence
8028                                 (gnus-summary-article-header article))))
8029                    (mail-header-set-number header to-article)
8030                    header)
8031                  (memq article gnus-newsgroup-marked)
8032                  (memq article gnus-newsgroup-dormant)
8033                  (memq article gnus-newsgroup-unreads)))
8034
8035               (when gnus-preserve-marks
8036                 ;; Copy any marks over to the new group.
8037                 (when (and (equal to-group gnus-newsgroup-name)
8038                            (not (memq article gnus-newsgroup-unreads)))
8039                   ;; Mark this article as read in this group.
8040                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8041                   (setcdr (gnus-active to-group) to-article)
8042                   (setcdr gnus-newsgroup-active to-article))
8043
8044                 (while marks
8045                   (when (memq article (symbol-value
8046                                        (intern (format "gnus-newsgroup-%s"
8047                                                        (caar marks)))))
8048                     (push (cdar marks) to-marks)
8049                     ;; If the other group is the same as this group,
8050                     ;; then we have to add the mark to the list.
8051                     (when (equal to-group gnus-newsgroup-name)
8052                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8053                            (cons to-article
8054                                  (symbol-value
8055                                   (intern (format "gnus-newsgroup-%s"
8056                                                   (caar marks)))))))
8057                     ;; Copy the marks to other group.
8058                     (gnus-add-marked-articles
8059                      to-group (cdar marks) (list to-article) info))
8060                   (setq marks (cdr marks)))
8061
8062                 (gnus-request-set-mark to-group (list (list (list to-article)
8063                                                             'set
8064                                                             to-marks))))
8065
8066               (gnus-dribble-enter
8067                (concat "(gnus-group-set-info '"
8068                        (gnus-prin1-to-string (gnus-get-info to-group))
8069                        ")"))))
8070
8071           ;; Update the Xref header in this article to point to
8072           ;; the new crossposted article we have just created.
8073           (when (eq action 'crosspost)
8074             (save-excursion
8075               (set-buffer copy-buf)
8076               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8077               (nnheader-replace-header "Xref" new-xref)
8078               (gnus-request-replace-article
8079                article gnus-newsgroup-name (current-buffer)))))
8080
8081         ;;;!!!Why is this necessary?
8082         (set-buffer gnus-summary-buffer)
8083
8084         (gnus-summary-goto-subject article)
8085         (when (eq action 'move)
8086           (gnus-summary-mark-article article gnus-canceled-mark))))
8087       (gnus-summary-remove-process-mark article))
8088     ;; Re-activate all groups that have been moved to.
8089     (while to-groups
8090       (save-excursion
8091         (set-buffer gnus-group-buffer)
8092         (when (gnus-group-goto-group (car to-groups) t)
8093           (gnus-group-get-new-news-this-group 1 t))
8094         (pop to-groups)))
8095
8096     (gnus-kill-buffer copy-buf)
8097     (gnus-summary-position-point)
8098     (gnus-set-mode-line 'summary)))
8099
8100 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8101   "Move the current article to a different newsgroup.
8102 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8103 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8104 re-spool using this method."
8105   (interactive "P")
8106   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8107
8108 (defun gnus-summary-crosspost-article (&optional n)
8109   "Crosspost the current article to some other group."
8110   (interactive "P")
8111   (gnus-summary-move-article n nil nil 'crosspost))
8112
8113 (defcustom gnus-summary-respool-default-method nil
8114   "Default method for respooling an article.
8115 If nil, use to the current newsgroup method."
8116   :type '(choice (gnus-select-method :value (nnml ""))
8117                  (const nil))
8118   :group 'gnus-summary-mail)
8119
8120 (defun gnus-summary-respool-article (&optional n method)
8121   "Respool the current article.
8122 The article will be squeezed through the mail spooling process again,
8123 which means that it will be put in some mail newsgroup or other
8124 depending on `nnmail-split-methods'.
8125 If N is a positive number, respool the N next articles.
8126 If N is a negative number, respool the N previous articles.
8127 If N is nil and any articles have been marked with the process mark,
8128 respool those articles instead.
8129
8130 Respooling can be done both from mail groups and \"real\" newsgroups.
8131 In the former case, the articles in question will be moved from the
8132 current group into whatever groups they are destined to.  In the
8133 latter case, they will be copied into the relevant groups."
8134   (interactive
8135    (list current-prefix-arg
8136          (let* ((methods (gnus-methods-using 'respool))
8137                 (methname
8138                  (symbol-name (or gnus-summary-respool-default-method
8139                                   (car (gnus-find-method-for-group
8140                                         gnus-newsgroup-name)))))
8141                 (method
8142                  (gnus-completing-read
8143                   methname "What backend do you want to use when respooling?"
8144                   methods nil t nil 'gnus-mail-method-history))
8145                 ms)
8146            (cond
8147             ((zerop (length (setq ms (gnus-servers-using-backend
8148                                       (intern method)))))
8149              (list (intern method) ""))
8150             ((= 1 (length ms))
8151              (car ms))
8152             (t
8153              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8154                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8155                            ms-alist))))))))
8156   (unless method
8157     (error "No method given for respooling"))
8158   (if (assoc (symbol-name
8159               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8160              (gnus-methods-using 'respool))
8161       (gnus-summary-move-article n nil method)
8162     (gnus-summary-copy-article n nil method)))
8163
8164 (defun gnus-summary-import-article (file &optional edit)
8165   "Import an arbitrary file into a mail newsgroup."
8166   (interactive "fImport file: \nP")
8167   (let ((group gnus-newsgroup-name)
8168         (now (current-time))
8169         atts lines group-art)
8170     (unless (gnus-check-backend-function 'request-accept-article group)
8171       (error "%s does not support article importing" group))
8172     (or (file-readable-p file)
8173         (not (file-regular-p file))
8174         (error "Can't read %s" file))
8175     (save-excursion
8176       (set-buffer (gnus-get-buffer-create " *import file*"))
8177       (erase-buffer)
8178       (nnheader-insert-file-contents file)
8179       (goto-char (point-min))
8180       (unless (nnheader-article-p)
8181         ;; This doesn't look like an article, so we fudge some headers.
8182         (setq atts (file-attributes file)
8183               lines (count-lines (point-min) (point-max)))
8184         (insert "From: " (read-string "From: ") "\n"
8185                 "Subject: " (read-string "Subject: ") "\n"
8186                 "Date: " (message-make-date (nth 5 atts)) "\n"
8187                 "Message-ID: " (message-make-message-id) "\n"
8188                 "Lines: " (int-to-string lines) "\n"
8189                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8190       (setq group-art (gnus-request-accept-article group nil t))
8191       (kill-buffer (current-buffer)))
8192     (setq gnus-newsgroup-active (gnus-activate-group group))
8193     (forward-line 1)
8194     (gnus-summary-goto-article (cdr group-art) nil t)
8195     (when edit
8196       (gnus-summary-edit-article))))
8197
8198 (defun gnus-summary-create-article ()
8199   "Create an article in a mail newsgroup."
8200   (interactive)
8201   (let ((group gnus-newsgroup-name)
8202         (now (current-time))
8203         group-art)
8204     (unless (gnus-check-backend-function 'request-accept-article group)
8205       (error "%s does not support article importing" group))
8206     (save-excursion
8207       (set-buffer (gnus-get-buffer-create " *import file*"))
8208       (erase-buffer)
8209       (goto-char (point-min))
8210       ;; This doesn't look like an article, so we fudge some headers.
8211       (insert "From: " (read-string "From: ") "\n"
8212               "Subject: " (read-string "Subject: ") "\n"
8213               "Date: " (message-make-date now) "\n"
8214               "Message-ID: " (message-make-message-id) "\n")
8215       (setq group-art (gnus-request-accept-article group nil t))
8216       (kill-buffer (current-buffer)))
8217     (setq gnus-newsgroup-active (gnus-activate-group group))
8218     (forward-line 1)
8219     (gnus-summary-goto-article (cdr group-art) nil t)
8220     (gnus-summary-edit-article)))
8221
8222 (defun gnus-summary-article-posted-p ()
8223   "Say whether the current (mail) article is available from news as well.
8224 This will be the case if the article has both been mailed and posted."
8225   (interactive)
8226   (let ((id (mail-header-references (gnus-summary-article-header)))
8227         (gnus-override-method (car (gnus-refer-article-methods))))
8228     (if (gnus-request-head id "")
8229         (gnus-message 2 "The current message was found on %s"
8230                       gnus-override-method)
8231       (gnus-message 2 "The current message couldn't be found on %s"
8232                     gnus-override-method)
8233       nil)))
8234
8235 (defun gnus-summary-expire-articles (&optional now)
8236   "Expire all articles that are marked as expirable in the current group."
8237   (interactive)
8238   (when (gnus-check-backend-function
8239          'request-expire-articles gnus-newsgroup-name)
8240     ;; This backend supports expiry.
8241     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8242            (expirable (if total
8243                           (progn
8244                             ;; We need to update the info for
8245                             ;; this group for `gnus-list-of-read-articles'
8246                             ;; to give us the right answer.
8247                             (gnus-run-hooks 'gnus-exit-group-hook)
8248                             (gnus-summary-update-info)
8249                             (gnus-list-of-read-articles gnus-newsgroup-name))
8250                         (setq gnus-newsgroup-expirable
8251                               (sort gnus-newsgroup-expirable '<))))
8252            (expiry-wait (if now 'immediate
8253                           (gnus-group-find-parameter
8254                            gnus-newsgroup-name 'expiry-wait)))
8255            (nnmail-expiry-target
8256             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8257                 nnmail-expiry-target))
8258            es)
8259       (when expirable
8260         ;; There are expirable articles in this group, so we run them
8261         ;; through the expiry process.
8262         (gnus-message 6 "Expiring articles...")
8263         (unless (gnus-check-group gnus-newsgroup-name)
8264           (error "Can't open server for %s" gnus-newsgroup-name))
8265         ;; The list of articles that weren't expired is returned.
8266         (save-excursion
8267           (if expiry-wait
8268               (let ((nnmail-expiry-wait-function nil)
8269                     (nnmail-expiry-wait expiry-wait))
8270                 (setq es (gnus-request-expire-articles
8271                           expirable gnus-newsgroup-name)))
8272             (setq es (gnus-request-expire-articles
8273                       expirable gnus-newsgroup-name)))
8274           (unless total
8275             (setq gnus-newsgroup-expirable es))
8276           ;; We go through the old list of expirable, and mark all
8277           ;; really expired articles as nonexistent.
8278           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8279             (let ((gnus-use-cache nil))
8280               (while expirable
8281                 (unless (memq (car expirable) es)
8282                   (when (gnus-data-find (car expirable))
8283                     (gnus-summary-mark-article
8284                      (car expirable) gnus-canceled-mark)))
8285                 (setq expirable (cdr expirable))))))
8286         (gnus-message 6 "Expiring articles...done")))))
8287
8288 (defun gnus-summary-expire-articles-now ()
8289   "Expunge all expirable articles in the current group.
8290 This means that *all* articles that are marked as expirable will be
8291 deleted forever, right now."
8292   (interactive)
8293   (or gnus-expert-user
8294       (gnus-yes-or-no-p
8295        "Are you really, really, really sure you want to delete all these messages? ")
8296       (error "Phew!"))
8297   (gnus-summary-expire-articles t))
8298
8299 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8300 (defun gnus-summary-delete-article (&optional n)
8301   "Delete the N next (mail) articles.
8302 This command actually deletes articles.  This is not a marking
8303 command.  The article will disappear forever from your life, never to
8304 return.
8305 If N is negative, delete backwards.
8306 If N is nil and articles have been marked with the process mark,
8307 delete these instead."
8308   (interactive "P")
8309   (unless (gnus-check-backend-function 'request-expire-articles
8310                                        gnus-newsgroup-name)
8311     (error "The current newsgroup does not support article deletion"))
8312   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8313     (error "Couldn't open server"))
8314   ;; Compute the list of articles to delete.
8315   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8316         not-deleted)
8317     (if (and gnus-novice-user
8318              (not (gnus-yes-or-no-p
8319                    (format "Do you really want to delete %s forever? "
8320                            (if (> (length articles) 1)
8321                                (format "these %s articles" (length articles))
8322                              "this article")))))
8323         ()
8324       ;; Delete the articles.
8325       (setq not-deleted (gnus-request-expire-articles
8326                          articles gnus-newsgroup-name 'force))
8327       (while articles
8328         (gnus-summary-remove-process-mark (car articles))
8329         ;; The backend might not have been able to delete the article
8330         ;; after all.
8331         (unless (memq (car articles) not-deleted)
8332           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8333         (setq articles (cdr articles)))
8334       (when not-deleted
8335         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8336     (gnus-summary-position-point)
8337     (gnus-set-mode-line 'summary)
8338     not-deleted))
8339
8340 (defun gnus-summary-edit-article (&optional force)
8341   "Edit the current article.
8342 This will have permanent effect only in mail groups.
8343 If FORCE is non-nil, allow editing of articles even in read-only
8344 groups."
8345   (interactive "P")
8346   (save-excursion
8347     (set-buffer gnus-summary-buffer)
8348     (let ((mail-parse-charset gnus-newsgroup-charset)
8349           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8350       (gnus-set-global-variables)
8351       (when (and (not force)
8352                  (gnus-group-read-only-p))
8353         (error "The current newsgroup does not support article editing"))
8354       (gnus-summary-show-article t)
8355       (gnus-article-edit-article
8356        'ignore
8357        `(lambda (no-highlight)
8358           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8359                 (message-options message-options)
8360                 (message-options-set-recipient)
8361                 (mail-parse-ignored-charsets
8362                  ',gnus-newsgroup-ignored-charsets))
8363             (gnus-summary-edit-article-done
8364              ,(or (mail-header-references gnus-current-headers) "")
8365              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8366
8367 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8368
8369 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8370                                                  no-highlight)
8371   "Make edits to the current article permanent."
8372   (interactive)
8373   (save-excursion
8374     ;; The buffer restriction contains the entire article if it exists.
8375     (when (article-goto-body)
8376       (let ((lines (count-lines (point) (point-max)))
8377             (length (- (point-max) (point)))
8378             (case-fold-search t)
8379             (body (copy-marker (point))))
8380         (goto-char (point-min))
8381         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8382           (delete-region (match-beginning 1) (match-end 1))
8383           (insert (number-to-string length)))
8384         (goto-char (point-min))
8385         (when (re-search-forward
8386                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8387           (delete-region (match-beginning 1) (match-end 1))
8388           (insert (number-to-string length)))
8389         (goto-char (point-min))
8390         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8391           (delete-region (match-beginning 1) (match-end 1))
8392           (insert (number-to-string lines))))))
8393   ;; Replace the article.
8394   (let ((buf (current-buffer)))
8395     (with-temp-buffer
8396       (insert-buffer-substring buf)
8397
8398       (if (and (not read-only)
8399                (not (gnus-request-replace-article
8400                      (cdr gnus-article-current) (car gnus-article-current)
8401                      (current-buffer) t)))
8402           (error "Couldn't replace article")
8403         ;; Update the summary buffer.
8404         (if (and references
8405                  (equal (message-tokenize-header references " ")
8406                         (message-tokenize-header
8407                          (or (message-fetch-field "references") "") " ")))
8408             ;; We only have to update this line.
8409             (save-excursion
8410               (save-restriction
8411                 (message-narrow-to-head)
8412                 (let ((head (buffer-string))
8413                       header)
8414                   (with-temp-buffer
8415                     (insert (format "211 %d Article retrieved.\n"
8416                                     (cdr gnus-article-current)))
8417                     (insert head)
8418                     (insert ".\n")
8419                     (let ((nntp-server-buffer (current-buffer)))
8420                       (setq header (car (gnus-get-newsgroup-headers
8421                                          (save-excursion
8422                                            (set-buffer gnus-summary-buffer)
8423                                            gnus-newsgroup-dependencies)
8424                                          t))))
8425                     (save-excursion
8426                       (set-buffer gnus-summary-buffer)
8427                       (gnus-data-set-header
8428                        (gnus-data-find (cdr gnus-article-current))
8429                        header)
8430                       (gnus-summary-update-article-line
8431                        (cdr gnus-article-current) header))))))
8432           ;; Update threads.
8433           (set-buffer (or buffer gnus-summary-buffer))
8434           (gnus-summary-update-article (cdr gnus-article-current)))
8435         ;; Prettify the article buffer again.
8436         (unless no-highlight
8437           (save-excursion
8438             (set-buffer gnus-article-buffer)
8439             ;;;!!! Fix this -- article should be rehighlighted.
8440             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8441             (set-buffer gnus-original-article-buffer)
8442             (gnus-request-article
8443              (cdr gnus-article-current)
8444              (car gnus-article-current) (current-buffer))))
8445         ;; Prettify the summary buffer line.
8446         (when (gnus-visual-p 'summary-highlight 'highlight)
8447           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8448
8449 (defun gnus-summary-edit-wash (key)
8450   "Perform editing command KEY in the article buffer."
8451   (interactive
8452    (list
8453     (progn
8454       (message "%s" (concat (this-command-keys) "- "))
8455       (read-char))))
8456   (message "")
8457   (gnus-summary-edit-article)
8458   (execute-kbd-macro (concat (this-command-keys) key))
8459   (gnus-article-edit-done))
8460
8461 ;;; Respooling
8462
8463 (defun gnus-summary-respool-query (&optional silent trace)
8464   "Query where the respool algorithm would put this article."
8465   (interactive)
8466   (let (gnus-mark-article-hook)
8467     (gnus-summary-select-article)
8468     (save-excursion
8469       (set-buffer gnus-original-article-buffer)
8470       (save-restriction
8471         (message-narrow-to-head)
8472         (let ((groups (nnmail-article-group 'identity trace)))
8473           (unless silent
8474             (if groups
8475                 (message "This message would go to %s"
8476                          (mapconcat 'car groups ", "))
8477               (message "This message would go to no groups"))
8478             groups))))))
8479
8480 (defun gnus-summary-respool-trace ()
8481   "Trace where the respool algorithm would put this article.
8482 Display a buffer showing all fancy splitting patterns which matched."
8483   (interactive)
8484   (gnus-summary-respool-query nil t))
8485
8486 ;; Summary marking commands.
8487
8488 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8489   "Mark articles which has the same subject as read, and then select the next.
8490 If UNMARK is positive, remove any kind of mark.
8491 If UNMARK is negative, tick articles."
8492   (interactive "P")
8493   (when unmark
8494     (setq unmark (prefix-numeric-value unmark)))
8495   (let ((count
8496          (gnus-summary-mark-same-subject
8497           (gnus-summary-article-subject) unmark)))
8498     ;; Select next unread article.  If auto-select-same mode, should
8499     ;; select the first unread article.
8500     (gnus-summary-next-article t (and gnus-auto-select-same
8501                                       (gnus-summary-article-subject)))
8502     (gnus-message 7 "%d article%s marked as %s"
8503                   count (if (= count 1) " is" "s are")
8504                   (if unmark "unread" "read"))))
8505
8506 (defun gnus-summary-kill-same-subject (&optional unmark)
8507   "Mark articles which has the same subject as read.
8508 If UNMARK is positive, remove any kind of mark.
8509 If UNMARK is negative, tick articles."
8510   (interactive "P")
8511   (when unmark
8512     (setq unmark (prefix-numeric-value unmark)))
8513   (let ((count
8514          (gnus-summary-mark-same-subject
8515           (gnus-summary-article-subject) unmark)))
8516     ;; If marked as read, go to next unread subject.
8517     (when (null unmark)
8518       ;; Go to next unread subject.
8519       (gnus-summary-next-subject 1 t))
8520     (gnus-message 7 "%d articles are marked as %s"
8521                   count (if unmark "unread" "read"))))
8522
8523 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8524   "Mark articles with same SUBJECT as read, and return marked number.
8525 If optional argument UNMARK is positive, remove any kinds of marks.
8526 If optional argument UNMARK is negative, mark articles as unread instead."
8527   (let ((count 1))
8528     (save-excursion
8529       (cond
8530        ((null unmark)                   ; Mark as read.
8531         (while (and
8532                 (progn
8533                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8534                   (gnus-summary-show-thread) t)
8535                 (gnus-summary-find-subject subject))
8536           (setq count (1+ count))))
8537        ((> unmark 0)                    ; Tick.
8538         (while (and
8539                 (progn
8540                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8541                   (gnus-summary-show-thread) t)
8542                 (gnus-summary-find-subject subject))
8543           (setq count (1+ count))))
8544        (t                               ; Mark as unread.
8545         (while (and
8546                 (progn
8547                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8548                   (gnus-summary-show-thread) t)
8549                 (gnus-summary-find-subject subject))
8550           (setq count (1+ count)))))
8551       (gnus-set-mode-line 'summary)
8552       ;; Return the number of marked articles.
8553       count)))
8554
8555 (defun gnus-summary-mark-as-processable (n &optional unmark)
8556   "Set the process mark on the next N articles.
8557 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8558 the process mark instead.  The difference between N and the actual
8559 number of articles marked is returned."
8560   (interactive "P")
8561   (if (and (null n) (gnus-region-active-p))
8562       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8563     (setq n (prefix-numeric-value n))
8564     (let ((backward (< n 0))
8565           (n (abs n)))
8566       (while (and
8567               (> n 0)
8568               (if unmark
8569                 (gnus-summary-remove-process-mark
8570                  (gnus-summary-article-number))
8571                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8572               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8573         (setq n (1- n)))
8574       (when (/= 0 n)
8575         (gnus-message 7 "No more articles"))
8576       (gnus-summary-recenter)
8577       (gnus-summary-position-point)
8578       n)))
8579
8580 (defun gnus-summary-unmark-as-processable (n)
8581   "Remove the process mark from the next N articles.
8582 If N is negative, unmark backward instead.  The difference between N and
8583 the actual number of articles unmarked is returned."
8584   (interactive "P")
8585   (gnus-summary-mark-as-processable n t))
8586
8587 (defun gnus-summary-unmark-all-processable ()
8588   "Remove the process mark from all articles."
8589   (interactive)
8590   (save-excursion
8591     (while gnus-newsgroup-processable
8592       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8593   (gnus-summary-position-point))
8594
8595 (defun gnus-summary-add-mark (article type)
8596   "Mark ARTICLE with a mark of TYPE."
8597   (let ((vtype (car (assq type gnus-article-mark-lists)))
8598         var)
8599     (if (not vtype)
8600         (error "No such mark type: %s" type)
8601       (setq var (intern (format "gnus-newsgroup-%s" type)))
8602       (set var (cons article (symbol-value var)))
8603       (if (memq type '(processable cached replied forwarded saved))
8604           (gnus-summary-update-secondary-mark article)
8605         ;;; !!! This is bobus.  We should find out what primary
8606         ;;; !!! mark we want to set.
8607         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8608
8609 (defun gnus-summary-mark-as-expirable (n)
8610   "Mark N articles forward as expirable.
8611 If N is negative, mark backward instead.  The difference between N and
8612 the actual number of articles marked is returned."
8613   (interactive "p")
8614   (gnus-summary-mark-forward n gnus-expirable-mark))
8615
8616 (defun gnus-summary-mark-article-as-replied (article)
8617   "Mark ARTICLE as replied to and update the summary line.
8618 ARTICLE can also be a list of articles."
8619   (interactive (list (gnus-summary-article-number)))
8620   (let ((articles (if (listp article) article (list article))))
8621     (dolist (article articles)
8622       (push article gnus-newsgroup-replied)
8623       (let ((buffer-read-only nil))
8624         (when (gnus-summary-goto-subject article nil t)
8625           (gnus-summary-update-secondary-mark article))))))
8626
8627 (defun gnus-summary-mark-article-as-forwarded (article)
8628   "Mark ARTICLE as forwarded and update the summary line.
8629 ARTICLE can also be a list of articles."
8630   (let ((articles (if (listp article) article (list article))))
8631     (dolist (article articles)
8632       (push article gnus-newsgroup-forwarded)
8633       (let ((buffer-read-only nil))
8634         (when (gnus-summary-goto-subject article nil t)
8635           (gnus-summary-update-secondary-mark article))))))
8636
8637 (defun gnus-summary-set-bookmark (article)
8638   "Set a bookmark in current article."
8639   (interactive (list (gnus-summary-article-number)))
8640   (when (or (not (get-buffer gnus-article-buffer))
8641             (not gnus-current-article)
8642             (not gnus-article-current)
8643             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8644     (error "No current article selected"))
8645   ;; Remove old bookmark, if one exists.
8646   (let ((old (assq article gnus-newsgroup-bookmarks)))
8647     (when old
8648       (setq gnus-newsgroup-bookmarks
8649             (delq old gnus-newsgroup-bookmarks))))
8650   ;; Set the new bookmark, which is on the form
8651   ;; (article-number . line-number-in-body).
8652   (push
8653    (cons article
8654          (save-excursion
8655            (set-buffer gnus-article-buffer)
8656            (count-lines
8657             (min (point)
8658                  (save-excursion
8659                    (goto-char (point-min))
8660                    (search-forward "\n\n" nil t)
8661                    (point)))
8662             (point))))
8663    gnus-newsgroup-bookmarks)
8664   (gnus-message 6 "A bookmark has been added to the current article."))
8665
8666 (defun gnus-summary-remove-bookmark (article)
8667   "Remove the bookmark from the current article."
8668   (interactive (list (gnus-summary-article-number)))
8669   ;; Remove old bookmark, if one exists.
8670   (let ((old (assq article gnus-newsgroup-bookmarks)))
8671     (if old
8672         (progn
8673           (setq gnus-newsgroup-bookmarks
8674                 (delq old gnus-newsgroup-bookmarks))
8675           (gnus-message 6 "Removed bookmark."))
8676       (gnus-message 6 "No bookmark in current article."))))
8677
8678 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8679 (defun gnus-summary-mark-as-dormant (n)
8680   "Mark N articles forward as dormant.
8681 If N is negative, mark backward instead.  The difference between N and
8682 the actual number of articles marked is returned."
8683   (interactive "p")
8684   (gnus-summary-mark-forward n gnus-dormant-mark))
8685
8686 (defun gnus-summary-set-process-mark (article)
8687   "Set the process mark on ARTICLE and update the summary line."
8688   (setq gnus-newsgroup-processable
8689         (cons article
8690               (delq article gnus-newsgroup-processable)))
8691   (when (gnus-summary-goto-subject article)
8692     (gnus-summary-show-thread)
8693     (gnus-summary-goto-subject article)
8694     (gnus-summary-update-secondary-mark article)))
8695
8696 (defun gnus-summary-remove-process-mark (article)
8697   "Remove the process mark from ARTICLE and update the summary line."
8698   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8699   (when (gnus-summary-goto-subject article)
8700     (gnus-summary-show-thread)
8701     (gnus-summary-goto-subject article)
8702     (gnus-summary-update-secondary-mark article)))
8703
8704 (defun gnus-summary-set-saved-mark (article)
8705   "Set the process mark on ARTICLE and update the summary line."
8706   (push article gnus-newsgroup-saved)
8707   (when (gnus-summary-goto-subject article)
8708     (gnus-summary-update-secondary-mark article)))
8709
8710 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8711   "Mark N articles as read forwards.
8712 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8713 The difference between N and the actual number of articles marked is
8714 returned.
8715 Iff NO-EXPIRE, auto-expiry will be inhibited."
8716   (interactive "p")
8717   (gnus-summary-show-thread)
8718   (let ((backward (< n 0))
8719         (gnus-summary-goto-unread
8720          (and gnus-summary-goto-unread
8721               (not (eq gnus-summary-goto-unread 'never))
8722               (not (memq mark (list gnus-unread-mark
8723                                     gnus-ticked-mark gnus-dormant-mark)))))
8724         (n (abs n))
8725         (mark (or mark gnus-del-mark)))
8726     (while (and (> n 0)
8727                 (gnus-summary-mark-article nil mark no-expire)
8728                 (zerop (gnus-summary-next-subject
8729                         (if backward -1 1)
8730                         (and gnus-summary-goto-unread
8731                              (not (eq gnus-summary-goto-unread 'never)))
8732                         t)))
8733       (setq n (1- n)))
8734     (when (/= 0 n)
8735       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8736     (gnus-summary-recenter)
8737     (gnus-summary-position-point)
8738     (gnus-set-mode-line 'summary)
8739     n))
8740
8741 (defun gnus-summary-mark-article-as-read (mark)
8742   "Mark the current article quickly as read with MARK."
8743   (let ((article (gnus-summary-article-number)))
8744     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8745     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8746     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8747     (push (cons article mark) gnus-newsgroup-reads)
8748     ;; Possibly remove from cache, if that is used.
8749     (when gnus-use-cache
8750       (gnus-cache-enter-remove-article article))
8751     ;; Allow the backend to change the mark.
8752     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8753     ;; Check for auto-expiry.
8754     (when (and gnus-newsgroup-auto-expire
8755                (memq mark gnus-auto-expirable-marks))
8756       (setq mark gnus-expirable-mark)
8757       ;; Let the backend know about the mark change.
8758       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8759       (push article gnus-newsgroup-expirable))
8760     ;; Set the mark in the buffer.
8761     (gnus-summary-update-mark mark 'unread)
8762     t))
8763
8764 (defun gnus-summary-mark-article-as-unread (mark)
8765   "Mark the current article quickly as unread with MARK."
8766   (let* ((article (gnus-summary-article-number))
8767          (old-mark (gnus-summary-article-mark article)))
8768     ;; Allow the backend to change the mark.
8769     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8770     (if (eq mark old-mark)
8771         t
8772       (if (<= article 0)
8773           (progn
8774             (gnus-error 1 "Can't mark negative article numbers")
8775             nil)
8776         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8777         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8778         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8779         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8780         (cond ((= mark gnus-ticked-mark)
8781                (push article gnus-newsgroup-marked))
8782               ((= mark gnus-dormant-mark)
8783                (push article gnus-newsgroup-dormant))
8784               (t
8785                (push article gnus-newsgroup-unreads)))
8786         (gnus-pull article gnus-newsgroup-reads)
8787
8788         ;; See whether the article is to be put in the cache.
8789         (and gnus-use-cache
8790              (vectorp (gnus-summary-article-header article))
8791              (save-excursion
8792                (gnus-cache-possibly-enter-article
8793                 gnus-newsgroup-name article
8794                 (gnus-summary-article-header article)
8795                 (= mark gnus-ticked-mark)
8796                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8797
8798         ;; Fix the mark.
8799         (gnus-summary-update-mark mark 'unread)
8800         t))))
8801
8802 (defun gnus-summary-mark-article (&optional article mark no-expire)
8803   "Mark ARTICLE with MARK.  MARK can be any character.
8804 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8805 `??' (dormant) and `?E' (expirable).
8806 If MARK is nil, then the default character `?r' is used.
8807 If ARTICLE is nil, then the article on the current line will be
8808 marked.
8809 Iff NO-EXPIRE, auto-expiry will be inhibited."
8810   ;; The mark might be a string.
8811   (when (stringp mark)
8812     (setq mark (aref mark 0)))
8813   ;; If no mark is given, then we check auto-expiring.
8814   (when (null mark)
8815     (setq mark gnus-del-mark))
8816   (when (and (not no-expire)
8817              gnus-newsgroup-auto-expire
8818              (memq mark gnus-auto-expirable-marks))
8819     (setq mark gnus-expirable-mark))
8820   (let ((article (or article (gnus-summary-article-number)))
8821         (old-mark (gnus-summary-article-mark article)))
8822     ;; Allow the backend to change the mark.
8823     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8824     (if (eq mark old-mark)
8825         t
8826       (unless article
8827         (error "No article on current line"))
8828       (if (not (if (or (= mark gnus-unread-mark)
8829                        (= mark gnus-ticked-mark)
8830                        (= mark gnus-dormant-mark))
8831                    (gnus-mark-article-as-unread article mark)
8832                  (gnus-mark-article-as-read article mark)))
8833           t
8834         ;; See whether the article is to be put in the cache.
8835         (and gnus-use-cache
8836              (not (= mark gnus-canceled-mark))
8837              (vectorp (gnus-summary-article-header article))
8838              (save-excursion
8839                (gnus-cache-possibly-enter-article
8840                 gnus-newsgroup-name article
8841                 (gnus-summary-article-header article)
8842                 (= mark gnus-ticked-mark)
8843                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8844
8845         (when (gnus-summary-goto-subject article nil t)
8846           (let ((buffer-read-only nil))
8847             (gnus-summary-show-thread)
8848             ;; Fix the mark.
8849             (gnus-summary-update-mark mark 'unread)
8850             t))))))
8851
8852 (defun gnus-summary-update-secondary-mark (article)
8853   "Update the secondary (read, process, cache) mark."
8854   (gnus-summary-update-mark
8855    (cond ((memq article gnus-newsgroup-processable)
8856           gnus-process-mark)
8857          ((memq article gnus-newsgroup-cached)
8858           gnus-cached-mark)
8859          ((memq article gnus-newsgroup-replied)
8860           gnus-replied-mark)
8861          ((memq article gnus-newsgroup-forwarded)
8862           gnus-forwarded-mark)
8863          ((memq article gnus-newsgroup-saved)
8864           gnus-saved-mark)
8865          (t gnus-no-mark))
8866    'replied)
8867   (when (gnus-visual-p 'summary-highlight 'highlight)
8868     (gnus-run-hooks 'gnus-summary-update-hook))
8869   t)
8870
8871 (defun gnus-summary-update-mark (mark type)
8872   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8873         (buffer-read-only nil))
8874     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8875     (when forward
8876       (when (looking-at "\r")
8877         (incf forward))
8878       (when (<= (+ forward (point)) (point-max))
8879         ;; Go to the right position on the line.
8880         (goto-char (+ forward (point)))
8881         ;; Replace the old mark with the new mark.
8882         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8883         ;; Optionally update the marks by some user rule.
8884         (when (eq type 'unread)
8885           (gnus-data-set-mark
8886            (gnus-data-find (gnus-summary-article-number)) mark)
8887           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8888
8889 (defun gnus-mark-article-as-read (article &optional mark)
8890   "Enter ARTICLE in the pertinent lists and remove it from others."
8891   ;; Make the article expirable.
8892   (let ((mark (or mark gnus-del-mark)))
8893     (if (= mark gnus-expirable-mark)
8894         (push article gnus-newsgroup-expirable)
8895       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8896     ;; Remove from unread and marked lists.
8897     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8898     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8899     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8900     (push (cons article mark) gnus-newsgroup-reads)
8901     ;; Possibly remove from cache, if that is used.
8902     (when gnus-use-cache
8903       (gnus-cache-enter-remove-article article))
8904     t))
8905
8906 (defun gnus-mark-article-as-unread (article &optional mark)
8907   "Enter ARTICLE in the pertinent lists and remove it from others."
8908   (let ((mark (or mark gnus-ticked-mark)))
8909     (if (<= article 0)
8910         (progn
8911           (gnus-error 1 "Can't mark negative article numbers")
8912           nil)
8913       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8914             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8915             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8916             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8917
8918       ;; Unsuppress duplicates?
8919       (when gnus-suppress-duplicates
8920         (gnus-dup-unsuppress-article article))
8921
8922       (cond ((= mark gnus-ticked-mark)
8923              (push article gnus-newsgroup-marked))
8924             ((= mark gnus-dormant-mark)
8925              (push article gnus-newsgroup-dormant))
8926             (t
8927              (push article gnus-newsgroup-unreads)))
8928       (gnus-pull article gnus-newsgroup-reads)
8929       t)))
8930
8931 (defalias 'gnus-summary-mark-as-unread-forward
8932   'gnus-summary-tick-article-forward)
8933 (make-obsolete 'gnus-summary-mark-as-unread-forward
8934                'gnus-summary-tick-article-forward)
8935 (defun gnus-summary-tick-article-forward (n)
8936   "Tick N articles forwards.
8937 If N is negative, tick backwards instead.
8938 The difference between N and the number of articles ticked is returned."
8939   (interactive "p")
8940   (gnus-summary-mark-forward n gnus-ticked-mark))
8941
8942 (defalias 'gnus-summary-mark-as-unread-backward
8943   'gnus-summary-tick-article-backward)
8944 (make-obsolete 'gnus-summary-mark-as-unread-backward
8945                'gnus-summary-tick-article-backward)
8946 (defun gnus-summary-tick-article-backward (n)
8947   "Tick N articles backwards.
8948 The difference between N and the number of articles ticked is returned."
8949   (interactive "p")
8950   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8951
8952 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8953 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8954 (defun gnus-summary-tick-article (&optional article clear-mark)
8955   "Mark current article as unread.
8956 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8957 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8958   (interactive)
8959   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8960                                        gnus-ticked-mark)))
8961
8962 (defun gnus-summary-mark-as-read-forward (n)
8963   "Mark N articles as read forwards.
8964 If N is negative, mark backwards instead.
8965 The difference between N and the actual number of articles marked is
8966 returned."
8967   (interactive "p")
8968   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8969
8970 (defun gnus-summary-mark-as-read-backward (n)
8971   "Mark the N articles as read backwards.
8972 The difference between N and the actual number of articles marked is
8973 returned."
8974   (interactive "p")
8975   (gnus-summary-mark-forward
8976    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8977
8978 (defun gnus-summary-mark-as-read (&optional article mark)
8979   "Mark current article as read.
8980 ARTICLE specifies the article to be marked as read.
8981 MARK specifies a string to be inserted at the beginning of the line."
8982   (gnus-summary-mark-article article mark))
8983
8984 (defun gnus-summary-clear-mark-forward (n)
8985   "Clear marks from N articles forward.
8986 If N is negative, clear backward instead.
8987 The difference between N and the number of marks cleared is returned."
8988   (interactive "p")
8989   (gnus-summary-mark-forward n gnus-unread-mark))
8990
8991 (defun gnus-summary-clear-mark-backward (n)
8992   "Clear marks from N articles backward.
8993 The difference between N and the number of marks cleared is returned."
8994   (interactive "p")
8995   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8996
8997 (defun gnus-summary-mark-unread-as-read ()
8998   "Intended to be used by `gnus-summary-mark-article-hook'."
8999   (when (memq gnus-current-article gnus-newsgroup-unreads)
9000     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9001
9002 (defun gnus-summary-mark-read-and-unread-as-read ()
9003   "Intended to be used by `gnus-summary-mark-article-hook'."
9004   (let ((mark (gnus-summary-article-mark)))
9005     (when (or (gnus-unread-mark-p mark)
9006               (gnus-read-mark-p mark))
9007       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9008
9009 (defun gnus-summary-mark-unread-as-ticked ()
9010    "Intended to be used by `gnus-summary-mark-article-hook'."
9011   (when (memq gnus-current-article gnus-newsgroup-unreads)
9012     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9013
9014 (defun gnus-summary-mark-region-as-read (point mark all)
9015   "Mark all unread articles between point and mark as read.
9016 If given a prefix, mark all articles between point and mark as read,
9017 even ticked and dormant ones."
9018   (interactive "r\nP")
9019   (save-excursion
9020     (let (article)
9021       (goto-char point)
9022       (beginning-of-line)
9023       (while (and
9024               (< (point) mark)
9025               (progn
9026                 (when (or all
9027                           (memq (setq article (gnus-summary-article-number))
9028                                 gnus-newsgroup-unreads))
9029                   (gnus-summary-mark-article article gnus-del-mark))
9030                 t)
9031               (gnus-summary-find-next))))))
9032
9033 (defun gnus-summary-mark-below (score mark)
9034   "Mark articles with score less than SCORE with MARK."
9035   (interactive "P\ncMark: ")
9036   (setq score (if score
9037                   (prefix-numeric-value score)
9038                 (or gnus-summary-default-score 0)))
9039   (save-excursion
9040     (set-buffer gnus-summary-buffer)
9041     (goto-char (point-min))
9042     (while
9043         (progn
9044           (and (< (gnus-summary-article-score) score)
9045                (gnus-summary-mark-article nil mark))
9046           (gnus-summary-find-next)))))
9047
9048 (defun gnus-summary-kill-below (&optional score)
9049   "Mark articles with score below SCORE as read."
9050   (interactive "P")
9051   (gnus-summary-mark-below score gnus-killed-mark))
9052
9053 (defun gnus-summary-clear-above (&optional score)
9054   "Clear all marks from articles with score above SCORE."
9055   (interactive "P")
9056   (gnus-summary-mark-above score gnus-unread-mark))
9057
9058 (defun gnus-summary-tick-above (&optional score)
9059   "Tick all articles with score above SCORE."
9060   (interactive "P")
9061   (gnus-summary-mark-above score gnus-ticked-mark))
9062
9063 (defun gnus-summary-mark-above (score mark)
9064   "Mark articles with score over SCORE with MARK."
9065   (interactive "P\ncMark: ")
9066   (setq score (if score
9067                   (prefix-numeric-value score)
9068                 (or gnus-summary-default-score 0)))
9069   (save-excursion
9070     (set-buffer gnus-summary-buffer)
9071     (goto-char (point-min))
9072     (while (and (progn
9073                   (when (> (gnus-summary-article-score) score)
9074                     (gnus-summary-mark-article nil mark))
9075                   t)
9076                 (gnus-summary-find-next)))))
9077
9078 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9079 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9080 (defun gnus-summary-limit-include-expunged (&optional no-error)
9081   "Display all the hidden articles that were expunged for low scores."
9082   (interactive)
9083   (let ((buffer-read-only nil))
9084     (let ((scored gnus-newsgroup-scored)
9085           headers h)
9086       (while scored
9087         (unless (gnus-summary-article-header (caar scored))
9088           (and (setq h (gnus-number-to-header (caar scored)))
9089                (< (cdar scored) gnus-summary-expunge-below)
9090                (push h headers)))
9091         (setq scored (cdr scored)))
9092       (if (not headers)
9093           (when (not no-error)
9094             (error "No expunged articles hidden"))
9095         (goto-char (point-min))
9096         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9097         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9098         (mapcar (lambda (x) (push (mail-header-number x) 
9099                                   gnus-newsgroup-limit))
9100                 headers)
9101         (gnus-summary-prepare-unthreaded (nreverse headers))
9102         (goto-char (point-min))
9103         (gnus-summary-position-point)
9104         t))))
9105
9106 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9107   "Mark all unread articles in this newsgroup as read.
9108 If prefix argument ALL is non-nil, ticked and dormant articles will
9109 also be marked as read.
9110 If QUIETLY is non-nil, no questions will be asked.
9111 If TO-HERE is non-nil, it should be a point in the buffer.  All
9112 articles before this point will be marked as read.
9113 Note that this function will only catch up the unread article
9114 in the current summary buffer limitation.
9115 The number of articles marked as read is returned."
9116   (interactive "P")
9117   (prog1
9118       (save-excursion
9119         (when (or quietly
9120                   (not gnus-interactive-catchup) ;Without confirmation?
9121                   gnus-expert-user
9122                   (gnus-y-or-n-p
9123                    (if all
9124                        "Mark absolutely all articles as read? "
9125                      "Mark all unread articles as read? ")))
9126           (if (and not-mark
9127                    (not gnus-newsgroup-adaptive)
9128                    (not gnus-newsgroup-auto-expire)
9129                    (not gnus-suppress-duplicates)
9130                    (or (not gnus-use-cache)
9131                        (eq gnus-use-cache 'passive)))
9132               (progn
9133                 (when all
9134                   (setq gnus-newsgroup-marked nil
9135                         gnus-newsgroup-dormant nil))
9136                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9137             ;; We actually mark all articles as canceled, which we
9138             ;; have to do when using auto-expiry or adaptive scoring.
9139             (gnus-summary-show-all-threads)
9140             (when (gnus-summary-first-subject (not all) t)
9141               (while (and
9142                       (if to-here (< (point) to-here) t)
9143                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9144                       (gnus-summary-find-next (not all) nil nil t))))
9145             (gnus-set-mode-line 'summary))
9146           t))
9147     (gnus-summary-position-point)))
9148
9149 (defun gnus-summary-catchup-to-here (&optional all)
9150   "Mark all unticked articles before the current one as read.
9151 If ALL is non-nil, also mark ticked and dormant articles as read."
9152   (interactive "P")
9153   (save-excursion
9154     (gnus-save-hidden-threads
9155       (let ((beg (point)))
9156         ;; We check that there are unread articles.
9157         (when (or all (gnus-summary-find-prev))
9158           (gnus-summary-catchup all t beg)))))
9159   (gnus-summary-position-point))
9160
9161 (defun gnus-summary-catchup-all (&optional quietly)
9162   "Mark all articles in this newsgroup as read."
9163   (interactive "P")
9164   (gnus-summary-catchup t quietly))
9165
9166 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9167   "Mark all unread articles in this group as read, then exit.
9168 If prefix argument ALL is non-nil, all articles are marked as read.
9169 If QUIETLY is non-nil, no questions will be asked."
9170   (interactive "P")
9171   (when (gnus-summary-catchup all quietly nil 'fast)
9172     ;; Select next newsgroup or exit.
9173     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9174              (eq gnus-auto-select-next 'quietly))
9175         (gnus-summary-next-group nil)
9176       (gnus-summary-exit))))
9177
9178 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9179   "Mark all articles in this newsgroup as read, and then exit."
9180   (interactive "P")
9181   (gnus-summary-catchup-and-exit t quietly))
9182
9183 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9184   "Mark all articles in this group as read and select the next group.
9185 If given a prefix, mark all articles, unread as well as ticked, as
9186 read."
9187   (interactive "P")
9188   (save-excursion
9189     (gnus-summary-catchup all))
9190   (gnus-summary-next-group))
9191
9192 ;;;
9193 ;;; with article
9194 ;;;
9195
9196 (defmacro gnus-with-article (article &rest forms)
9197   "Select ARTICLE and perform FORMS in the original article buffer.
9198 Then replace the article with the result."
9199   `(progn
9200      ;; We don't want the article to be marked as read.
9201      (let (gnus-mark-article-hook)
9202        (gnus-summary-select-article t t nil ,article))
9203      (set-buffer gnus-original-article-buffer)
9204      ,@forms
9205      (if (not (gnus-check-backend-function
9206                'request-replace-article (car gnus-article-current)))
9207          (gnus-message 5 "Read-only group; not replacing")
9208        (unless (gnus-request-replace-article
9209                 ,article (car gnus-article-current)
9210                 (current-buffer) t)
9211          (error "Couldn't replace article")))
9212      ;; The cache and backlog have to be flushed somewhat.
9213      (when gnus-keep-backlog
9214        (gnus-backlog-remove-article
9215         (car gnus-article-current) (cdr gnus-article-current)))
9216      (when gnus-use-cache
9217        (gnus-cache-update-article
9218         (car gnus-article-current) (cdr gnus-article-current)))))
9219
9220 (put 'gnus-with-article 'lisp-indent-function 1)
9221 (put 'gnus-with-article 'edebug-form-spec '(form body))
9222
9223 ;; Thread-based commands.
9224
9225 (defun gnus-summary-articles-in-thread (&optional article)
9226   "Return a list of all articles in the current thread.
9227 If ARTICLE is non-nil, return all articles in the thread that starts
9228 with that article."
9229   (let* ((article (or article (gnus-summary-article-number)))
9230          (data (gnus-data-find-list article))
9231          (top-level (gnus-data-level (car data)))
9232          (top-subject
9233           (cond ((null gnus-thread-operation-ignore-subject)
9234                  (gnus-simplify-subject-re
9235                   (mail-header-subject (gnus-data-header (car data)))))
9236                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9237                  (gnus-simplify-subject-fuzzy
9238                   (mail-header-subject (gnus-data-header (car data)))))
9239                 (t nil)))
9240          (end-point (save-excursion
9241                       (if (gnus-summary-go-to-next-thread)
9242                           (point) (point-max))))
9243          articles)
9244     (while (and data
9245                 (< (gnus-data-pos (car data)) end-point))
9246       (when (or (not top-subject)
9247                 (string= top-subject
9248                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9249                              (gnus-simplify-subject-fuzzy
9250                               (mail-header-subject
9251                                (gnus-data-header (car data))))
9252                            (gnus-simplify-subject-re
9253                             (mail-header-subject
9254                              (gnus-data-header (car data)))))))
9255         (push (gnus-data-number (car data)) articles))
9256       (unless (and (setq data (cdr data))
9257                    (> (gnus-data-level (car data)) top-level))
9258         (setq data nil)))
9259     ;; Return the list of articles.
9260     (nreverse articles)))
9261
9262 (defun gnus-summary-rethread-current ()
9263   "Rethread the thread the current article is part of."
9264   (interactive)
9265   (let* ((gnus-show-threads t)
9266          (article (gnus-summary-article-number))
9267          (id (mail-header-id (gnus-summary-article-header)))
9268          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9269     (unless id
9270       (error "No article on the current line"))
9271     (gnus-rebuild-thread id)
9272     (gnus-summary-goto-subject article)))
9273
9274 (defun gnus-summary-reparent-thread ()
9275   "Make the current article child of the marked (or previous) article.
9276
9277 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9278 is non-nil or the Subject: of both articles are the same."
9279   (interactive)
9280   (unless (not (gnus-group-read-only-p))
9281     (error "The current newsgroup does not support article editing"))
9282   (unless (<= (length gnus-newsgroup-processable) 1)
9283     (error "No more than one article may be marked"))
9284   (save-window-excursion
9285     (let ((gnus-article-buffer " *reparent*")
9286           (current-article (gnus-summary-article-number))
9287           ;; First grab the marked article, otherwise one line up.
9288           (parent-article (if (not (null gnus-newsgroup-processable))
9289                               (car gnus-newsgroup-processable)
9290                             (save-excursion
9291                               (if (eq (forward-line -1) 0)
9292                                   (gnus-summary-article-number)
9293                                 (error "Beginning of summary buffer"))))))
9294       (unless (not (eq current-article parent-article))
9295         (error "An article may not be self-referential"))
9296       (let ((message-id (mail-header-id
9297                          (gnus-summary-article-header parent-article))))
9298         (unless (and message-id (not (equal message-id "")))
9299           (error "No message-id in desired parent"))
9300         (gnus-with-article current-article
9301           (save-restriction
9302             (goto-char (point-min))
9303             (message-narrow-to-head)
9304             (if (re-search-forward "^References: " nil t)
9305                 (progn
9306                   (re-search-forward "^[^ \t]" nil t)
9307                   (forward-line -1)
9308                   (end-of-line)
9309                   (insert " " message-id))
9310               (insert "References: " message-id "\n"))))
9311         (set-buffer gnus-summary-buffer)
9312         (gnus-summary-unmark-all-processable)
9313         (gnus-summary-update-article current-article)
9314         (gnus-summary-rethread-current)
9315         (gnus-message 3 "Article %d is now the child of article %d"
9316                       current-article parent-article)))))
9317
9318 (defun gnus-summary-toggle-threads (&optional arg)
9319   "Toggle showing conversation threads.
9320 If ARG is positive number, turn showing conversation threads on."
9321   (interactive "P")
9322   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9323     (setq gnus-show-threads
9324           (if (null arg) (not gnus-show-threads)
9325             (> (prefix-numeric-value arg) 0)))
9326     (gnus-summary-prepare)
9327     (gnus-summary-goto-subject current)
9328     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9329     (gnus-summary-position-point)))
9330
9331 (defun gnus-summary-show-all-threads ()
9332   "Show all threads."
9333   (interactive)
9334   (save-excursion
9335     (let ((buffer-read-only nil))
9336       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9337   (gnus-summary-position-point))
9338
9339 (defun gnus-summary-show-thread ()
9340   "Show thread subtrees.
9341 Returns nil if no thread was there to be shown."
9342   (interactive)
9343   (let ((buffer-read-only nil)
9344         (orig (point))
9345         ;; first goto end then to beg, to have point at beg after let
9346         (end (progn (end-of-line) (point)))
9347         (beg (progn (beginning-of-line) (point))))
9348     (prog1
9349         ;; Any hidden lines here?
9350         (search-forward "\r" end t)
9351       (subst-char-in-region beg end ?\^M ?\n t)
9352       (goto-char orig)
9353       (gnus-summary-position-point))))
9354
9355 (defun gnus-summary-hide-all-threads ()
9356   "Hide all thread subtrees."
9357   (interactive)
9358   (save-excursion
9359     (goto-char (point-min))
9360     (gnus-summary-hide-thread)
9361     (while (zerop (gnus-summary-next-thread 1 t))
9362       (gnus-summary-hide-thread)))
9363   (gnus-summary-position-point))
9364
9365 (defun gnus-summary-hide-thread ()
9366   "Hide thread subtrees.
9367 Returns nil if no threads were there to be hidden."
9368   (interactive)
9369   (let ((buffer-read-only nil)
9370         (start (point))
9371         (article (gnus-summary-article-number)))
9372     (goto-char start)
9373     ;; Go forward until either the buffer ends or the subthread
9374     ;; ends.
9375     (when (and (not (eobp))
9376                (or (zerop (gnus-summary-next-thread 1 t))
9377                    (goto-char (point-max))))
9378       (prog1
9379           (if (and (> (point) start)
9380                    (search-backward "\n" start t))
9381               (progn
9382                 (subst-char-in-region start (point) ?\n ?\^M)
9383                 (gnus-summary-goto-subject article))
9384             (goto-char start)
9385             nil)))))
9386
9387 (defun gnus-summary-go-to-next-thread (&optional previous)
9388   "Go to the same level (or less) next thread.
9389 If PREVIOUS is non-nil, go to previous thread instead.
9390 Return the article number moved to, or nil if moving was impossible."
9391   (let ((level (gnus-summary-thread-level))
9392         (way (if previous -1 1))
9393         (beg (point)))
9394     (forward-line way)
9395     (while (and (not (eobp))
9396                 (< level (gnus-summary-thread-level)))
9397       (forward-line way))
9398     (if (eobp)
9399         (progn
9400           (goto-char beg)
9401           nil)
9402       (setq beg (point))
9403       (prog1
9404           (gnus-summary-article-number)
9405         (goto-char beg)))))
9406
9407 (defun gnus-summary-next-thread (n &optional silent)
9408   "Go to the same level next N'th thread.
9409 If N is negative, search backward instead.
9410 Returns the difference between N and the number of skips actually
9411 done.
9412
9413 If SILENT, don't output messages."
9414   (interactive "p")
9415   (let ((backward (< n 0))
9416         (n (abs n)))
9417     (while (and (> n 0)
9418                 (gnus-summary-go-to-next-thread backward))
9419       (decf n))
9420     (unless silent
9421       (gnus-summary-position-point))
9422     (when (and (not silent) (/= 0 n))
9423       (gnus-message 7 "No more threads"))
9424     n))
9425
9426 (defun gnus-summary-prev-thread (n)
9427   "Go to the same level previous N'th thread.
9428 Returns the difference between N and the number of skips actually
9429 done."
9430   (interactive "p")
9431   (gnus-summary-next-thread (- n)))
9432
9433 (defun gnus-summary-go-down-thread ()
9434   "Go down one level in the current thread."
9435   (let ((children (gnus-summary-article-children)))
9436     (when children
9437       (gnus-summary-goto-subject (car children)))))
9438
9439 (defun gnus-summary-go-up-thread ()
9440   "Go up one level in the current thread."
9441   (let ((parent (gnus-summary-article-parent)))
9442     (when parent
9443       (gnus-summary-goto-subject parent))))
9444
9445 (defun gnus-summary-down-thread (n)
9446   "Go down thread N steps.
9447 If N is negative, go up instead.
9448 Returns the difference between N and how many steps down that were
9449 taken."
9450   (interactive "p")
9451   (let ((up (< n 0))
9452         (n (abs n)))
9453     (while (and (> n 0)
9454                 (if up (gnus-summary-go-up-thread)
9455                   (gnus-summary-go-down-thread)))
9456       (setq n (1- n)))
9457     (gnus-summary-position-point)
9458     (when (/= 0 n)
9459       (gnus-message 7 "Can't go further"))
9460     n))
9461
9462 (defun gnus-summary-up-thread (n)
9463   "Go up thread N steps.
9464 If N is negative, go down instead.
9465 Returns the difference between N and how many steps down that were
9466 taken."
9467   (interactive "p")
9468   (gnus-summary-down-thread (- n)))
9469
9470 (defun gnus-summary-top-thread ()
9471   "Go to the top of the thread."
9472   (interactive)
9473   (while (gnus-summary-go-up-thread))
9474   (gnus-summary-article-number))
9475
9476 (defun gnus-summary-kill-thread (&optional unmark)
9477   "Mark articles under current thread as read.
9478 If the prefix argument is positive, remove any kinds of marks.
9479 If the prefix argument is negative, tick articles instead."
9480   (interactive "P")
9481   (when unmark
9482     (setq unmark (prefix-numeric-value unmark)))
9483   (let ((articles (gnus-summary-articles-in-thread)))
9484     (save-excursion
9485       ;; Expand the thread.
9486       (gnus-summary-show-thread)
9487       ;; Mark all the articles.
9488       (while articles
9489         (gnus-summary-goto-subject (car articles))
9490         (cond ((null unmark)
9491                (gnus-summary-mark-article-as-read gnus-killed-mark))
9492               ((> unmark 0)
9493                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9494               (t
9495                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9496         (setq articles (cdr articles))))
9497     ;; Hide killed subtrees.
9498     (and (null unmark)
9499          gnus-thread-hide-killed
9500          (gnus-summary-hide-thread))
9501     ;; If marked as read, go to next unread subject.
9502     (when (null unmark)
9503       ;; Go to next unread subject.
9504       (gnus-summary-next-subject 1 t)))
9505   (gnus-set-mode-line 'summary))
9506
9507 ;; Summary sorting commands
9508
9509 (defun gnus-summary-sort-by-number (&optional reverse)
9510   "Sort the summary buffer by article number.
9511 Argument REVERSE means reverse order."
9512   (interactive "P")
9513   (gnus-summary-sort 'number reverse))
9514
9515 (defun gnus-summary-sort-by-author (&optional reverse)
9516   "Sort the summary buffer by author name alphabetically.
9517 If `case-fold-search' is non-nil, case of letters is ignored.
9518 Argument REVERSE means reverse order."
9519   (interactive "P")
9520   (gnus-summary-sort 'author reverse))
9521
9522 (defun gnus-summary-sort-by-subject (&optional reverse)
9523   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9524 If `case-fold-search' is non-nil, case of letters is ignored.
9525 Argument REVERSE means reverse order."
9526   (interactive "P")
9527   (gnus-summary-sort 'subject reverse))
9528
9529 (defun gnus-summary-sort-by-date (&optional reverse)
9530   "Sort the summary buffer by date.
9531 Argument REVERSE means reverse order."
9532   (interactive "P")
9533   (gnus-summary-sort 'date reverse))
9534
9535 (defun gnus-summary-sort-by-score (&optional reverse)
9536   "Sort the summary buffer by score.
9537 Argument REVERSE means reverse order."
9538   (interactive "P")
9539   (gnus-summary-sort 'score reverse))
9540
9541 (defun gnus-summary-sort-by-lines (&optional reverse)
9542   "Sort the summary buffer by the number of lines.
9543 Argument REVERSE means reverse order."
9544   (interactive "P")
9545   (gnus-summary-sort 'lines reverse))
9546
9547 (defun gnus-summary-sort-by-chars (&optional reverse)
9548   "Sort the summary buffer by article length.
9549 Argument REVERSE means reverse order."
9550   (interactive "P")
9551   (gnus-summary-sort 'chars reverse))
9552
9553 (defun gnus-summary-sort-by-original (&optional reverse)
9554   "Sort the summary buffer using the default sorting method.
9555 Argument REVERSE means reverse order."
9556   (interactive "P")
9557   (let* ((buffer-read-only)
9558          (gnus-summary-prepare-hook nil))
9559     ;; We do the sorting by regenerating the threads.
9560     (gnus-summary-prepare)
9561     ;; Hide subthreads if needed.
9562     (when (and gnus-show-threads gnus-thread-hide-subtree)
9563       (gnus-summary-hide-all-threads))))
9564
9565 (defun gnus-summary-sort (predicate reverse)
9566   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9567   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9568          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9569          (gnus-thread-sort-functions
9570           (if (not reverse)
9571               thread
9572             `(lambda (t1 t2)
9573                (,thread t2 t1))))
9574          (gnus-sort-gathered-threads-function
9575           gnus-thread-sort-functions)
9576          (gnus-article-sort-functions
9577           (if (not reverse)
9578               article
9579             `(lambda (t1 t2)
9580                (,article t2 t1))))
9581          (buffer-read-only)
9582          (gnus-summary-prepare-hook nil))
9583     ;; We do the sorting by regenerating the threads.
9584     (gnus-summary-prepare)
9585     ;; Hide subthreads if needed.
9586     (when (and gnus-show-threads gnus-thread-hide-subtree)
9587       (gnus-summary-hide-all-threads))))
9588
9589 ;; Summary saving commands.
9590
9591 (defun gnus-summary-save-article (&optional n not-saved)
9592   "Save the current article using the default saver function.
9593 If N is a positive number, save the N next articles.
9594 If N is a negative number, save the N previous articles.
9595 If N is nil and any articles have been marked with the process mark,
9596 save those articles instead.
9597 The variable `gnus-default-article-saver' specifies the saver function."
9598   (interactive "P")
9599   (let* ((articles (gnus-summary-work-articles n))
9600          (save-buffer (save-excursion
9601                         (nnheader-set-temp-buffer " *Gnus Save*")))
9602          (num (length articles))
9603          header file)
9604     (dolist (article articles)
9605       (setq header (gnus-summary-article-header article))
9606       (if (not (vectorp header))
9607           ;; This is a pseudo-article.
9608           (if (assq 'name header)
9609               (gnus-copy-file (cdr (assq 'name header)))
9610             (gnus-message 1 "Article %d is unsaveable" article))
9611         ;; This is a real article.
9612         (save-window-excursion
9613           (gnus-summary-select-article t nil nil article))
9614         (save-excursion
9615           (set-buffer save-buffer)
9616           (erase-buffer)
9617           (insert-buffer-substring gnus-original-article-buffer))
9618         (setq file (gnus-article-save save-buffer file num))
9619         (gnus-summary-remove-process-mark article)
9620         (unless not-saved
9621           (gnus-summary-set-saved-mark article))))
9622     (gnus-kill-buffer save-buffer)
9623     (gnus-summary-position-point)
9624     (gnus-set-mode-line 'summary)
9625     n))
9626
9627 (defun gnus-summary-pipe-output (&optional arg)
9628   "Pipe the current article to a subprocess.
9629 If N is a positive number, pipe the N next articles.
9630 If N is a negative number, pipe the N previous articles.
9631 If N is nil and any articles have been marked with the process mark,
9632 pipe those articles instead."
9633   (interactive "P")
9634   (require 'gnus-art)
9635   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9636     (gnus-summary-save-article arg t))
9637   (gnus-configure-windows 'pipe))
9638
9639 (defun gnus-summary-save-article-mail (&optional arg)
9640   "Append the current article to an mail file.
9641 If N is a positive number, save the N next articles.
9642 If N is a negative number, save the N previous articles.
9643 If N is nil and any articles have been marked with the process mark,
9644 save those articles instead."
9645   (interactive "P")
9646   (require 'gnus-art)
9647   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9648     (gnus-summary-save-article arg)))
9649
9650 (defun gnus-summary-save-article-rmail (&optional arg)
9651   "Append the current article to an rmail file.
9652 If N is a positive number, save the N next articles.
9653 If N is a negative number, save the N previous articles.
9654 If N is nil and any articles have been marked with the process mark,
9655 save those articles instead."
9656   (interactive "P")
9657   (require 'gnus-art)
9658   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9659     (gnus-summary-save-article arg)))
9660
9661 (defun gnus-summary-save-article-file (&optional arg)
9662   "Append the current article to a file.
9663 If N is a positive number, save the N next articles.
9664 If N is a negative number, save the N previous articles.
9665 If N is nil and any articles have been marked with the process mark,
9666 save those articles instead."
9667   (interactive "P")
9668   (require 'gnus-art)
9669   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9670     (gnus-summary-save-article arg)))
9671
9672 (defun gnus-summary-write-article-file (&optional arg)
9673   "Write the current article to a file, deleting the previous file.
9674 If N is a positive number, save the N next articles.
9675 If N is a negative number, save the N previous articles.
9676 If N is nil and any articles have been marked with the process mark,
9677 save those articles instead."
9678   (interactive "P")
9679   (require 'gnus-art)
9680   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9681     (gnus-summary-save-article arg)))
9682
9683 (defun gnus-summary-save-article-body-file (&optional arg)
9684   "Append the current article body to a file.
9685 If N is a positive number, save the N next articles.
9686 If N is a negative number, save the N previous articles.
9687 If N is nil and any articles have been marked with the process mark,
9688 save those articles instead."
9689   (interactive "P")
9690   (require 'gnus-art)
9691   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9692     (gnus-summary-save-article arg)))
9693
9694 (defun gnus-summary-pipe-message (program)
9695   "Pipe the current article through PROGRAM."
9696   (interactive "sProgram: ")
9697   (gnus-summary-select-article)
9698   (let ((mail-header-separator ""))
9699     (gnus-eval-in-buffer-window gnus-article-buffer
9700       (save-restriction
9701         (widen)
9702         (let ((start (window-start))
9703               buffer-read-only)
9704           (message-pipe-buffer-body program)
9705           (set-window-start (get-buffer-window (current-buffer)) start))))))
9706
9707 (defun gnus-get-split-value (methods)
9708   "Return a value based on the split METHODS."
9709   (let (split-name method result match)
9710     (when methods
9711       (save-excursion
9712         (set-buffer gnus-original-article-buffer)
9713         (save-restriction
9714           (nnheader-narrow-to-headers)
9715           (while (and methods (not split-name))
9716             (goto-char (point-min))
9717             (setq method (pop methods))
9718             (setq match (car method))
9719             (when (cond
9720                    ((stringp match)
9721                     ;; Regular expression.
9722                     (ignore-errors
9723                       (re-search-forward match nil t)))
9724                    ((gnus-functionp match)
9725                     ;; Function.
9726                     (save-restriction
9727                       (widen)
9728                       (setq result (funcall match gnus-newsgroup-name))))
9729                    ((consp match)
9730                     ;; Form.
9731                     (save-restriction
9732                       (widen)
9733                       (setq result (eval match)))))
9734               (setq split-name (cdr method))
9735               (cond ((stringp result)
9736                      (push (expand-file-name
9737                             result gnus-article-save-directory)
9738                            split-name))
9739                     ((consp result)
9740                      (setq split-name (append result split-name)))))))))
9741     (nreverse split-name)))
9742
9743 (defun gnus-valid-move-group-p (group)
9744   (and (boundp group)
9745        (symbol-name group)
9746        (symbol-value group)
9747        (gnus-get-function (gnus-find-method-for-group
9748                            (symbol-name group)) 'request-accept-article t)))
9749
9750 (defun gnus-read-move-group-name (prompt default articles prefix)
9751   "Read a group name."
9752   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9753          (minibuffer-confirm-incomplete nil) ; XEmacs
9754          (prom
9755           (format "%s %s to:"
9756                   prompt
9757                   (if (> (length articles) 1)
9758                       (format "these %d articles" (length articles))
9759                     "this article")))
9760          (to-newsgroup
9761           (cond
9762            ((null split-name)
9763             (gnus-completing-read default prom
9764                                   gnus-active-hashtb
9765                                   'gnus-valid-move-group-p
9766                                   nil prefix
9767                                   'gnus-group-history))
9768            ((= 1 (length split-name))
9769             (gnus-completing-read (car split-name) prom
9770                                   gnus-active-hashtb
9771                                   'gnus-valid-move-group-p
9772                                   nil nil
9773                                   'gnus-group-history))
9774            (t
9775             (gnus-completing-read nil prom
9776                                   (mapcar (lambda (el) (list el))
9777                                           (nreverse split-name))
9778                                   nil nil nil
9779                                   'gnus-group-history))))
9780          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9781     (when to-newsgroup
9782       (if (or (string= to-newsgroup "")
9783               (string= to-newsgroup prefix))
9784           (setq to-newsgroup default))
9785       (unless to-newsgroup
9786         (error "No group name entered"))
9787       (or (gnus-active to-newsgroup)
9788           (gnus-activate-group to-newsgroup nil nil to-method)
9789           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9790                                      to-newsgroup))
9791               (or (and (gnus-request-create-group to-newsgroup to-method)
9792                        (gnus-activate-group
9793                         to-newsgroup nil nil to-method)
9794                        (gnus-subscribe-group to-newsgroup))
9795                   (error "Couldn't create group %s" to-newsgroup)))
9796           (error "No such group: %s" to-newsgroup)))
9797     to-newsgroup))
9798
9799 (defun gnus-summary-save-parts (type dir n &optional reverse)
9800   "Save parts matching TYPE to DIR.
9801 If REVERSE, save parts that do not match TYPE."
9802   (interactive
9803    (list (read-string "Save parts of type: "
9804                       (or (car gnus-summary-save-parts-type-history)
9805                           gnus-summary-save-parts-default-mime)
9806                       'gnus-summary-save-parts-type-history)
9807          (setq gnus-summary-save-parts-last-directory
9808                (read-file-name "Save to directory: "
9809                                gnus-summary-save-parts-last-directory
9810                                nil t))
9811          current-prefix-arg))
9812   (gnus-summary-iterate n
9813     (let ((gnus-display-mime-function nil)
9814           (gnus-inhibit-treatment t))
9815       (gnus-summary-select-article))
9816     (save-excursion
9817       (set-buffer gnus-article-buffer)
9818       (let ((handles (or gnus-article-mime-handles
9819                          (mm-dissect-buffer) (mm-uu-dissect))))
9820         (when handles
9821           (gnus-summary-save-parts-1 type dir handles reverse)
9822           (unless gnus-article-mime-handles ;; Don't destroy this case.
9823             (mm-destroy-parts handles)))))))
9824
9825 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9826   (if (stringp (car handle))
9827       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9828               (cdr handle))
9829     (when (if reverse
9830               (not (string-match type (mm-handle-media-type handle)))
9831             (string-match type (mm-handle-media-type handle)))
9832       (let ((file (expand-file-name
9833                    (file-name-nondirectory
9834                     (or
9835                      (mail-content-type-get
9836                       (mm-handle-disposition handle) 'filename)
9837                      (concat gnus-newsgroup-name
9838                              "." (number-to-string
9839                                   (cdr gnus-article-current)))))
9840                    dir)))
9841         (unless (file-exists-p file)
9842           (mm-save-part-to-file handle file))))))
9843
9844 ;; Summary extract commands
9845
9846 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9847   (let ((buffer-read-only nil)
9848         (article (gnus-summary-article-number))
9849         after-article b e)
9850     (unless (gnus-summary-goto-subject article)
9851       (error "No such article: %d" article))
9852     (gnus-summary-position-point)
9853     ;; If all commands are to be bunched up on one line, we collect
9854     ;; them here.
9855     (unless gnus-view-pseudos-separately
9856       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9857             files action)
9858         (while ps
9859           (setq action (cdr (assq 'action (car ps))))
9860           (setq files (list (cdr (assq 'name (car ps)))))
9861           (while (and ps (cdr ps)
9862                       (string= (or action "1")
9863                                (or (cdr (assq 'action (cadr ps))) "2")))
9864             (push (cdr (assq 'name (cadr ps))) files)
9865             (setcdr ps (cddr ps)))
9866           (when files
9867             (when (not (string-match "%s" action))
9868               (push " " files))
9869             (push " " files)
9870             (when (assq 'execute (car ps))
9871               (setcdr (assq 'execute (car ps))
9872                       (funcall (if (string-match "%s" action)
9873                                    'format 'concat)
9874                                action
9875                                (mapconcat
9876                                 (lambda (f)
9877                                   (if (equal f " ")
9878                                       f
9879                                     (gnus-quote-arg-for-sh-or-csh f)))
9880                                 files " ")))))
9881           (setq ps (cdr ps)))))
9882     (if (and gnus-view-pseudos (not not-view))
9883         (while pslist
9884           (when (assq 'execute (car pslist))
9885             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9886                                   (eq gnus-view-pseudos 'not-confirm)))
9887           (setq pslist (cdr pslist)))
9888       (save-excursion
9889         (while pslist
9890           (setq after-article (or (cdr (assq 'article (car pslist)))
9891                                   (gnus-summary-article-number)))
9892           (gnus-summary-goto-subject after-article)
9893           (forward-line 1)
9894           (setq b (point))
9895           (insert "    " (file-name-nondirectory
9896                           (cdr (assq 'name (car pslist))))
9897                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9898           (setq e (point))
9899           (forward-line -1)             ; back to `b'
9900           (gnus-add-text-properties
9901            b (1- e) (list 'gnus-number gnus-reffed-article-number
9902                           gnus-mouse-face-prop gnus-mouse-face))
9903           (gnus-data-enter
9904            after-article gnus-reffed-article-number
9905            gnus-unread-mark b (car pslist) 0 (- e b))
9906           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9907           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9908           (setq pslist (cdr pslist)))))))
9909
9910 (defun gnus-pseudos< (p1 p2)
9911   (let ((c1 (cdr (assq 'action p1)))
9912         (c2 (cdr (assq 'action p2))))
9913     (and c1 c2 (string< c1 c2))))
9914
9915 (defun gnus-request-pseudo-article (props)
9916   (cond ((assq 'execute props)
9917          (gnus-execute-command (cdr (assq 'execute props)))))
9918   (let ((gnus-current-article (gnus-summary-article-number)))
9919     (gnus-run-hooks 'gnus-mark-article-hook)))
9920
9921 (defun gnus-execute-command (command &optional automatic)
9922   (save-excursion
9923     (gnus-article-setup-buffer)
9924     (set-buffer gnus-article-buffer)
9925     (setq buffer-read-only nil)
9926     (let ((command (if automatic command
9927                      (read-string "Command: " (cons command 0)))))
9928       (erase-buffer)
9929       (insert "$ " command "\n\n")
9930       (if gnus-view-pseudo-asynchronously
9931           (start-process "gnus-execute" (current-buffer) shell-file-name
9932                          shell-command-switch command)
9933         (call-process shell-file-name nil t nil
9934                       shell-command-switch command)))))
9935
9936 ;; Summary kill commands.
9937
9938 (defun gnus-summary-edit-global-kill (article)
9939   "Edit the \"global\" kill file."
9940   (interactive (list (gnus-summary-article-number)))
9941   (gnus-group-edit-global-kill article))
9942
9943 (defun gnus-summary-edit-local-kill ()
9944   "Edit a local kill file applied to the current newsgroup."
9945   (interactive)
9946   (setq gnus-current-headers (gnus-summary-article-header))
9947   (gnus-group-edit-local-kill
9948    (gnus-summary-article-number) gnus-newsgroup-name))
9949
9950 ;;; Header reading.
9951
9952 (defun gnus-read-header (id &optional header)
9953   "Read the headers of article ID and enter them into the Gnus system."
9954   (let ((group gnus-newsgroup-name)
9955         (gnus-override-method
9956          (or
9957           gnus-override-method
9958           (and (gnus-news-group-p gnus-newsgroup-name)
9959                (car (gnus-refer-article-methods)))))
9960         where)
9961     ;; First we check to see whether the header in question is already
9962     ;; fetched.
9963     (if (stringp id)
9964         ;; This is a Message-ID.
9965         (setq header (or header (gnus-id-to-header id)))
9966       ;; This is an article number.
9967       (setq header (or header (gnus-summary-article-header id))))
9968     (if (and header
9969              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9970         ;; We have found the header.
9971         header
9972       ;; If this is a sparse article, we have to nix out its
9973       ;; previous entry in the thread hashtb.
9974       (when (and header
9975                  (gnus-summary-article-sparse-p (mail-header-number header)))
9976         (let* ((parent (gnus-parent-id (mail-header-references header)))
9977                (thread (and parent (gnus-id-to-thread parent))))
9978           (when thread
9979             (delq (assq header thread) thread))))
9980       ;; We have to really fetch the header to this article.
9981       (save-excursion
9982         (set-buffer nntp-server-buffer)
9983         (when (setq where (gnus-request-head id group))
9984           (nnheader-fold-continuation-lines)
9985           (goto-char (point-max))
9986           (insert ".\n")
9987           (goto-char (point-min))
9988           (insert "211 ")
9989           (princ (cond
9990                   ((numberp id) id)
9991                   ((cdr where) (cdr where))
9992                   (header (mail-header-number header))
9993                   (t gnus-reffed-article-number))
9994                  (current-buffer))
9995           (insert " Article retrieved.\n"))
9996         (if (or (not where)
9997                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9998             ()                          ; Malformed head.
9999           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10000             (when (and (stringp id)
10001                        (not (string= (gnus-group-real-name group)
10002                                      (car where))))
10003               ;; If we fetched by Message-ID and the article came
10004               ;; from a different group, we fudge some bogus article
10005               ;; numbers for this article.
10006               (mail-header-set-number header gnus-reffed-article-number))
10007             (save-excursion
10008               (set-buffer gnus-summary-buffer)
10009               (decf gnus-reffed-article-number)
10010               (gnus-remove-header (mail-header-number header))
10011               (push header gnus-newsgroup-headers)
10012               (setq gnus-current-headers header)
10013               (push (mail-header-number header) gnus-newsgroup-limit)))
10014           header)))))
10015
10016 (defun gnus-remove-header (number)
10017   "Remove header NUMBER from `gnus-newsgroup-headers'."
10018   (if (and gnus-newsgroup-headers
10019            (= number (mail-header-number (car gnus-newsgroup-headers))))
10020       (pop gnus-newsgroup-headers)
10021     (let ((headers gnus-newsgroup-headers))
10022       (while (and (cdr headers)
10023                   (not (= number (mail-header-number (cadr headers)))))
10024         (pop headers))
10025       (when (cdr headers)
10026         (setcdr headers (cddr headers))))))
10027
10028 ;;;
10029 ;;; summary highlights
10030 ;;;
10031
10032 (defun gnus-highlight-selected-summary ()
10033   "Highlight selected article in summary buffer."
10034   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10035   (when gnus-summary-selected-face
10036     (save-excursion
10037       (let* ((beg (progn (beginning-of-line) (point)))
10038              (end (progn (end-of-line) (point)))
10039              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10040              (from (if (get-text-property beg gnus-mouse-face-prop)
10041                        beg
10042                      (or (next-single-property-change
10043                           beg gnus-mouse-face-prop nil end)
10044                          beg)))
10045              (to
10046               (if (= from end)
10047                   (- from 2)
10048                 (or (next-single-property-change
10049                      from gnus-mouse-face-prop nil end)
10050                     end))))
10051         ;; If no mouse-face prop on line we will have to = from = end,
10052         ;; so we highlight the entire line instead.
10053         (when (= (+ to 2) from)
10054           (setq from beg)
10055           (setq to end))
10056         (if gnus-newsgroup-selected-overlay
10057             ;; Move old overlay.
10058             (gnus-move-overlay
10059              gnus-newsgroup-selected-overlay from to (current-buffer))
10060           ;; Create new overlay.
10061           (gnus-overlay-put
10062            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10063            'face gnus-summary-selected-face))))))
10064
10065 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10066 (defun gnus-summary-highlight-line ()
10067   "Highlight current line according to `gnus-summary-highlight'."
10068   (let* ((list gnus-summary-highlight)
10069          (p (point))
10070          (end (progn (end-of-line) (point)))
10071          ;; now find out where the line starts and leave point there.
10072          (beg (progn (beginning-of-line) (point)))
10073          (article (gnus-summary-article-number))
10074          (score (or (cdr (assq (or article gnus-current-article)
10075                                gnus-newsgroup-scored))
10076                     gnus-summary-default-score 0))
10077          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10078          (inhibit-read-only t))
10079     ;; Eval the cars of the lists until we find a match.
10080     (let ((default gnus-summary-default-score))
10081       (while (and list
10082                   (not (eval (caar list))))
10083         (setq list (cdr list))))
10084     (let ((face (cdar list)))
10085       (unless (eq face (get-text-property beg 'face))
10086         (gnus-put-text-property-excluding-characters-with-faces
10087          beg end 'face
10088          (setq face (if (boundp face) (symbol-value face) face)))
10089         (when gnus-summary-highlight-line-function
10090           (funcall gnus-summary-highlight-line-function article face))))
10091     (goto-char p)))
10092
10093 (defun gnus-update-read-articles (group unread &optional compute)
10094   "Update the list of read articles in GROUP."
10095   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10096          (entry (gnus-gethash group gnus-newsrc-hashtb))
10097          (info (nth 2 entry))
10098          (prev 1)
10099          (unread (sort (copy-sequence unread) '<))
10100          read)
10101     (if (or (not info) (not active))
10102         ;; There is no info on this group if it was, in fact,
10103         ;; killed.  Gnus stores no information on killed groups, so
10104         ;; there's nothing to be done.
10105         ;; One could store the information somewhere temporarily,
10106         ;; perhaps...  Hmmm...
10107         ()
10108       ;; Remove any negative articles numbers.
10109       (while (and unread (< (car unread) 0))
10110         (setq unread (cdr unread)))
10111       ;; Remove any expired article numbers
10112       (while (and unread (< (car unread) (car active)))
10113         (setq unread (cdr unread)))
10114       ;; Compute the ranges of read articles by looking at the list of
10115       ;; unread articles.
10116       (while unread
10117         (when (/= (car unread) prev)
10118           (push (if (= prev (1- (car unread))) prev
10119                   (cons prev (1- (car unread))))
10120                 read))
10121         (setq prev (1+ (car unread)))
10122         (setq unread (cdr unread)))
10123       (when (<= prev (cdr active))
10124         (push (cons prev (cdr active)) read))
10125       (setq read (if (> (length read) 1) (nreverse read) read))
10126       (if compute
10127           read
10128         (save-excursion
10129           (let (setmarkundo)
10130             ;; Propagate the read marks to the backend.
10131             (when (gnus-check-backend-function 'request-set-mark group)
10132               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10133                     (add (gnus-remove-from-range read (gnus-info-read info))))
10134                 (when (or add del)
10135                   (unless (gnus-check-group group)
10136                     (error "Can't open server for %s" group))
10137                   (gnus-request-set-mark
10138                    group (delq nil (list (if add (list add 'add '(read)))
10139                                          (if del (list del 'del '(read))))))
10140                   (setq setmarkundo
10141                         `(gnus-request-set-mark
10142                           ,group
10143                           ',(delq nil (list
10144                                        (if del (list del 'add '(read)))
10145                                        (if add (list add 'del '(read))))))))))
10146             (set-buffer gnus-group-buffer)
10147             (gnus-undo-register
10148               `(progn
10149                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10150                  (gnus-info-set-read ',info ',(gnus-info-read info))
10151                  (gnus-get-unread-articles-in-group ',info
10152                                                     (gnus-active ,group))
10153                  (gnus-group-update-group ,group t)
10154                  ,setmarkundo))))
10155         ;; Enter this list into the group info.
10156         (gnus-info-set-read info read)
10157         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10158         (gnus-get-unread-articles-in-group info (gnus-active group))
10159         t))))
10160
10161 (defun gnus-offer-save-summaries ()
10162   "Offer to save all active summary buffers."
10163   (let (buffers)
10164     ;; Go through all buffers and find all summaries.
10165     (dolist (buffer (buffer-list))
10166       (when (and (setq buffer (buffer-name buffer))
10167                  (string-match "Summary" buffer)
10168                  (save-excursion
10169                    (set-buffer buffer)
10170                    ;; We check that this is, indeed, a summary buffer.
10171                    (and (eq major-mode 'gnus-summary-mode)
10172                         ;; Also make sure this isn't bogus.
10173                         gnus-newsgroup-prepared
10174                         ;; Also make sure that this isn't a
10175                         ;; dead summary buffer.
10176                         (not gnus-dead-summary-mode))))
10177         (push buffer buffers)))
10178     ;; Go through all these summary buffers and offer to save them.
10179     (when buffers
10180       (save-excursion
10181         (map-y-or-n-p
10182          "Update summary buffer %s? "
10183          (lambda (buf)
10184            (switch-to-buffer buf)
10185            (gnus-summary-exit))
10186          buffers)))))
10187
10188
10189 ;;; @ for mime-partial
10190 ;;;
10191
10192 (defun gnus-request-partial-message ()
10193   (save-excursion
10194     (let ((number (gnus-summary-article-number))
10195           (group gnus-newsgroup-name)
10196           (mother gnus-article-buffer))
10197       (set-buffer (get-buffer-create " *Partial Article*"))
10198       (erase-buffer)
10199       (setq mime-preview-buffer mother)
10200       (gnus-request-article-this-buffer number group)
10201       (mime-parse-buffer)
10202       )))
10203
10204 (autoload 'mime-combine-message/partial-pieces-automatically
10205   "mime-partial"
10206   "Internal method to combine message/partial messages automatically.")
10207
10208 (mime-add-condition
10209  'action '((type . message)(subtype . partial)
10210            (major-mode . gnus-original-article-mode)
10211            (method . mime-combine-message/partial-pieces-automatically)
10212            (summary-buffer-exp . gnus-summary-buffer)
10213            (request-partial-message-method . gnus-request-partial-message)
10214            ))
10215
10216
10217 ;;; @ for message/rfc822
10218 ;;;
10219
10220 (defun gnus-mime-extract-message/rfc822 (entity situation)
10221   (let (group article num cwin swin cur)
10222     (with-temp-buffer
10223       (mime-insert-entity-content entity)
10224       (setq group (or (cdr (assq 'group situation))
10225                       (completing-read "Group: "
10226                                        gnus-active-hashtb
10227                                        nil
10228                                        (gnus-read-active-file-p)
10229                                        gnus-newsgroup-name))
10230             article (gnus-request-accept-article group)))
10231     (when (and (consp article)
10232                (numberp (setq article (cdr article))))
10233       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10234             cwin (get-buffer-window (current-buffer) t))
10235       (save-window-excursion
10236         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10237             (select-window swin)
10238           (set-buffer gnus-summary-buffer))
10239         (setq cur gnus-current-article)
10240         (forward-line num)
10241         (let (gnus-show-threads)
10242           (gnus-summary-goto-subject article t))
10243         (gnus-summary-clear-mark-forward 1)
10244         (gnus-summary-goto-subject cur))
10245       (when (and cwin (window-frame cwin))
10246         (select-frame (window-frame cwin)))
10247       (when (boundp 'mime-acting-situation-to-override)
10248         (set-alist 'mime-acting-situation-to-override
10249                    'group
10250                    group)
10251         (set-alist 'mime-acting-situation-to-override
10252                    'after-method
10253                    `(progn
10254                       (save-current-buffer
10255                         (set-buffer gnus-group-buffer)
10256                         (gnus-activate-group ,group))
10257                       (gnus-summary-goto-article ,cur
10258                                                  gnus-show-all-headers)))
10259         (set-alist 'mime-acting-situation-to-override
10260                    'number num)))))
10261
10262 (mime-add-condition
10263  'action '((type . message)(subtype . rfc822)
10264            (major-mode . gnus-original-article-mode)
10265            (method . gnus-mime-extract-message/rfc822)
10266            (mode . "extract")
10267            ))
10268
10269 (mime-add-condition
10270  'action '((type . message)(subtype . news)
10271            (major-mode . gnus-original-article-mode)
10272            (method . gnus-mime-extract-message/rfc822)
10273            (mode . "extract")
10274            ))
10275
10276 (defun gnus-mime-extract-multipart (entity situation)
10277   (let ((children (mime-entity-children entity))
10278         mime-acting-situation-to-override
10279         f)
10280     (while children
10281       (mime-play-entity (car children)
10282                         (cons (assq 'mode situation)
10283                               mime-acting-situation-to-override))
10284       (setq children (cdr children)))
10285     (if (setq f (cdr (assq 'after-method
10286                            mime-acting-situation-to-override)))
10287         (eval f)
10288       )))
10289
10290 (mime-add-condition
10291  'action '((type . multipart)
10292            (method . gnus-mime-extract-multipart)
10293            (mode . "extract")
10294            )
10295  'with-default)
10296
10297
10298 ;;; @ end
10299 ;;;
10300
10301 (defun gnus-summary-setup-default-charset ()
10302   "Setup newsgroup default charset."
10303   (if (equal gnus-newsgroup-name "nndraft:drafts")
10304       (setq gnus-newsgroup-charset nil)
10305     (let* ((ignored-charsets
10306             (or gnus-newsgroup-ephemeral-ignored-charsets
10307                 (append
10308                  (and gnus-newsgroup-name
10309                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10310                  gnus-newsgroup-ignored-charsets))))
10311       (setq gnus-newsgroup-charset
10312             (or gnus-newsgroup-ephemeral-charset
10313                 (and gnus-newsgroup-name
10314                      (gnus-parameter-charset gnus-newsgroup-name))
10315                 gnus-default-charset))
10316       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10317            ignored-charsets))))
10318
10319 ;;;
10320 ;;; Mime Commands
10321 ;;;
10322
10323 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10324   "Display the current article buffer fully MIME-buttonized.
10325 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10326 treated as multipart/mixed."
10327   (interactive "P")
10328   (require 'gnus-art)
10329   (let ((gnus-unbuttonized-mime-types nil)
10330         (gnus-mime-display-multipart-as-mixed show-all-parts))
10331     (gnus-summary-show-article)))
10332
10333 (defun gnus-summary-repair-multipart (article)
10334   "Add a Content-Type header to a multipart article without one."
10335   (interactive (list (gnus-summary-article-number)))
10336   (gnus-with-article article
10337     (message-narrow-to-head)
10338     (message-remove-header "Mime-Version")
10339     (goto-char (point-max))
10340     (insert "Mime-Version: 1.0\n")
10341     (widen)
10342     (when (search-forward "\n--" nil t)
10343       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10344         (message-narrow-to-head)
10345         (message-remove-header "Content-Type")
10346         (goto-char (point-max))
10347         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10348                         separator))
10349         (widen))))
10350   (let (gnus-mark-article-hook)
10351     (gnus-summary-select-article t t nil article)))
10352
10353 (defun gnus-summary-toggle-display-buttonized ()
10354   "Toggle the buttonizing of the article buffer."
10355   (interactive)
10356   (require 'gnus-art)
10357   (if (setq gnus-inhibit-mime-unbuttonizing
10358             (not gnus-inhibit-mime-unbuttonizing))
10359       (let ((gnus-unbuttonized-mime-types nil))
10360         (gnus-summary-show-article))
10361     (gnus-summary-show-article)))
10362
10363 ;;;
10364 ;;; Intelli-mouse commmands
10365 ;;;
10366
10367 (defun gnus-wheel-summary-scroll (event)
10368   (interactive "e")
10369   (let ((amount (if (memq 'shift (event-modifiers event))
10370                     (car gnus-wheel-scroll-amount)
10371                   (cdr gnus-wheel-scroll-amount)))
10372         (direction (- (* (static-if (featurep 'xemacs)
10373                              (event-button event)
10374                            (cond ((eq 'mouse-4 (event-basic-type event))
10375                                   4)
10376                                  ((eq 'mouse-5 (event-basic-type event))
10377                                   5)))
10378                          2) 9))
10379         edge)
10380     (gnus-summary-scroll-up (* amount direction))
10381     (when (gnus-eval-in-buffer-window gnus-article-buffer
10382             (save-restriction
10383               (widen)
10384               (and (if (< 0 direction)
10385                        (gnus-article-next-page 0)
10386                      (gnus-article-prev-page 0)
10387                      (bobp))
10388                    (if (setq edge (get-text-property
10389                                    (point-min) 'gnus-wheel-edge))
10390                        (setq edge (* edge direction))
10391                      (setq edge -1))
10392                    (or (plusp edge)
10393                        (let ((buffer-read-only nil)
10394                              (inhibit-read-only t))
10395                          (put-text-property (point-min) (point-max)
10396                                             'gnus-wheel-edge direction)
10397                          nil))
10398                    (or (> edge gnus-wheel-edge-resistance)
10399                        (let ((buffer-read-only nil)
10400                              (inhibit-read-only t))
10401                          (put-text-property (point-min) (point-max)
10402                                             'gnus-wheel-edge
10403                                             (* (1+ edge) direction))
10404                          nil))
10405                    (eq last-command 'gnus-wheel-summary-scroll))))
10406       (gnus-summary-next-article nil nil (minusp direction)))))
10407
10408 (defun gnus-wheel-install ()
10409   "Enable mouse wheel support on summary window."
10410   (when gnus-use-wheel
10411     (let ((keys
10412            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10413       (dolist (key keys)
10414         (define-key gnus-summary-mode-map key
10415           'gnus-wheel-summary-scroll)))))
10416
10417 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10418
10419 ;;;
10420 ;;; Traditional PGP commmands
10421 ;;;
10422
10423 (defun gnus-summary-decrypt-article (&optional force)
10424   "Decrypt the current article in traditional PGP way.
10425 This will have permanent effect only in mail groups.
10426 If FORCE is non-nil, allow editing of articles even in read-only
10427 groups."
10428   (interactive "P")
10429   (gnus-summary-select-article t)
10430   (gnus-eval-in-buffer-window gnus-article-buffer
10431     (save-excursion
10432       (save-restriction
10433         (widen)
10434         (goto-char (point-min))
10435         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10436           (error "Not a traditional PGP message!"))
10437         (let ((armor-start (match-beginning 0)))
10438           (if (and (pgg-decrypt-region armor-start (point-max))
10439                    (or force (not (gnus-group-read-only-p))))
10440               (let ((inhibit-read-only t)
10441                     buffer-read-only)
10442                 (delete-region armor-start
10443                                (progn
10444                                  (re-search-forward "^-+END PGP" nil t)
10445                                  (beginning-of-line 2)
10446                                  (point)))
10447                 (insert-buffer-substring pgg-output-buffer))))))))
10448
10449 (defun gnus-summary-verify-article ()
10450   "Verify the current article in traditional PGP way."
10451   (interactive)
10452   (save-excursion
10453     (set-buffer gnus-original-article-buffer)
10454     (goto-char (point-min))
10455     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10456       (error "Not a traditional PGP message!"))
10457     (re-search-forward "^-+END PGP" nil t)
10458     (beginning-of-line 2)
10459     (call-interactively (function pgg-verify-region))))
10460
10461 ;;;
10462 ;;; Generic summary marking commands
10463 ;;;
10464
10465 (defvar gnus-summary-marking-alist
10466   '((read gnus-del-mark "d")
10467     (unread gnus-unread-mark "u")
10468     (ticked gnus-ticked-mark "!")
10469     (dormant gnus-dormant-mark "?")
10470     (expirable gnus-expirable-mark "e"))
10471   "An alist of names/marks/keystrokes.")
10472
10473 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10474 (defvar gnus-summary-mark-map)
10475
10476 (defun gnus-summary-make-all-marking-commands ()
10477   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10478   (dolist (elem gnus-summary-marking-alist)
10479     (apply 'gnus-summary-make-marking-command elem)))
10480
10481 (defun gnus-summary-make-marking-command (name mark keystroke)
10482   (let ((map (make-sparse-keymap)))
10483     (define-key gnus-summary-generic-mark-map keystroke map)
10484     (dolist (lway `((next "next" next nil "n")
10485                     (next-unread "next unread" next t "N")
10486                     (prev "previous" prev nil "p")
10487                     (prev-unread "previous unread" prev t "P")
10488                     (nomove "" nil nil ,keystroke)))
10489       (let ((func (gnus-summary-make-marking-command-1
10490                    mark (car lway) lway name)))
10491         (setq func (eval func))
10492         (define-key map (nth 4 lway) func)))))
10493
10494 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10495   `(defun ,(intern
10496             (format "gnus-summary-put-mark-as-%s%s"
10497                     name (if (eq way 'nomove)
10498                              ""
10499                            (concat "-" (symbol-name way)))))
10500      (n)
10501      ,(format
10502        "Mark the current article as %s%s.
10503 If N, the prefix, then repeat N times.
10504 If N is negative, move in reverse order.
10505 The difference between N and the actual number of articles marked is
10506 returned."
10507        name (car (cdr lway)))
10508      (interactive "p")
10509      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10510
10511 (defun gnus-summary-generic-mark (n mark move unread)
10512   "Mark N articles with MARK."
10513   (unless (eq major-mode 'gnus-summary-mode)
10514     (error "This command can only be used in the summary buffer"))
10515   (gnus-summary-show-thread)
10516   (let ((nummove
10517          (cond
10518           ((eq move 'next) 1)
10519           ((eq move 'prev) -1)
10520           (t 0))))
10521     (if (zerop nummove)
10522         (setq n 1)
10523       (when (< n 0)
10524         (setq n (abs n)
10525               nummove (* -1 nummove))))
10526     (while (and (> n 0)
10527                 (gnus-summary-mark-article nil mark)
10528                 (zerop (gnus-summary-next-subject nummove unread t)))
10529       (setq n (1- n)))
10530     (when (/= 0 n)
10531       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10532     (gnus-summary-recenter)
10533     (gnus-summary-position-point)
10534     (gnus-set-mode-line 'summary)
10535     n))
10536
10537 (defun gnus-summary-insert-articles (articles)
10538   (when (setq articles
10539               (gnus-set-difference articles
10540                                    (mapcar (lambda (h) (mail-header-number h))
10541                                            gnus-newsgroup-headers)))
10542     (setq gnus-newsgroup-headers 
10543           (merge 'list
10544                  gnus-newsgroup-headers
10545                  (gnus-fetch-headers articles)
10546                  'gnus-article-sort-by-number))
10547     ;; Suppress duplicates?
10548     (when gnus-suppress-duplicates
10549       (gnus-dup-suppress-articles))
10550     
10551     ;; We might want to build some more threads first.
10552     (when (and gnus-fetch-old-headers
10553                (eq gnus-headers-retrieved-by 'nov))
10554       (if (eq gnus-fetch-old-headers 'invisible)
10555         (gnus-build-all-threads)
10556         (gnus-build-old-threads)))
10557     ;; Let the Gnus agent mark articles as read.
10558     (when gnus-agent
10559       (gnus-agent-get-undownloaded-list))
10560     ;; Remove list identifiers from subject
10561     (when gnus-list-identifiers
10562       (gnus-summary-remove-list-identifiers))
10563     ;; First and last article in this newsgroup.
10564     (when gnus-newsgroup-headers
10565       (setq gnus-newsgroup-begin
10566             (mail-header-number (car gnus-newsgroup-headers))
10567             gnus-newsgroup-end
10568             (mail-header-number
10569              (gnus-last-element gnus-newsgroup-headers))))
10570     (when gnus-use-scoring
10571       (gnus-possibly-score-headers))))
10572
10573 (defun gnus-summary-insert-old-articles (&optional all)
10574   "Insert all old articles in this group.
10575 If ALL is non-nil, already read articles become readable.
10576 If ALL is a number, fetch this number of articles."
10577   (interactive "P")
10578   (prog1
10579       (let ((old (mapcar 'car gnus-newsgroup-data))
10580             (i (car gnus-newsgroup-active))
10581             older len)
10582         (while (<= i (cdr gnus-newsgroup-active))
10583           (or (memq i old) (push i older))
10584           (incf i))
10585         (setq len (length older))
10586         (cond 
10587          ((null older) nil)
10588          ((numberp all) 
10589           (if (< all len)
10590               (setq older (subseq older 0 all))))
10591          (all nil)
10592          (t
10593           (if (and (numberp gnus-large-newsgroup)
10594                    (> len gnus-large-newsgroup))
10595               (let ((input
10596                      (read-string
10597                       (format
10598                        "How many articles from %s (default %d): "
10599                        (gnus-limit-string 
10600                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10601                        len))))
10602                 (unless (string-match "^[ \t]*$" input) 
10603                   (setq all (string-to-number input))
10604                   (if (< all len)
10605                       (setq older (subseq older 0 all))))))))
10606         (if (not older)
10607             (message "No old news.")
10608           (gnus-summary-insert-articles older)
10609           (gnus-summary-limit (gnus-union older old))))
10610     (gnus-summary-position-point)))
10611
10612 (defun gnus-summary-insert-new-articles ()
10613   "Insert all new articles in this group."
10614   (interactive)
10615   (prog1
10616       (let ((old (mapcar 'car gnus-newsgroup-data))
10617             (old-active gnus-newsgroup-active)
10618             (nnmail-fetched-sources (list t))
10619             i new)
10620         (setq gnus-newsgroup-active 
10621               (gnus-activate-group gnus-newsgroup-name 'scan))
10622         (setq i (1+ (cdr old-active)))
10623         (while (<= i (cdr gnus-newsgroup-active))
10624           (push i new)
10625           (incf i))
10626         (if (not new)
10627             (message "No gnus is bad news.")
10628           (setq new (nreverse new))
10629           (gnus-summary-insert-articles new)
10630           (setq gnus-newsgroup-unreads
10631                 (append gnus-newsgroup-unreads new))
10632           (gnus-summary-limit (gnus-union old new))))
10633     (gnus-summary-position-point)))
10634
10635 (gnus-summary-make-all-marking-commands)
10636
10637 (gnus-ems-redefine)
10638
10639 (provide 'gnus-sum)
10640
10641 (run-hooks 'gnus-sum-load-hook)
10642
10643 ;;; gnus-sum.el ends here