Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 ;; Recursive :-(.
41 ;; (require 'gnus-art)
42 (require 'nnoo)
43 (require 'mime-view)
44
45 (eval-when-compile
46   (require 'mime-play)
47   (require 'static))
48
49 (eval-and-compile
50   (autoload 'gnus-cache-articles-in-group "gnus-cache")
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 killed 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-cached-mark ?*
452   "*Mark used for articles that are in the cache."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-saved-mark ?S
457   "*Mark used for articles that have been saved to."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-no-mark ?  ;Whitespace
462   "*Mark used for articles that have no other secondary mark."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-ancient-mark ?O
467   "*Mark used for ancient articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-sparse-mark ?Q
472   "*Mark used for sparsely reffed articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-canceled-mark ?G
477   "*Mark used for canceled articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-duplicate-mark ?M
482   "*Mark used for duplicate articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-undownloaded-mark ?@
487   "*Mark used for articles that weren't downloaded."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-downloadable-mark ?%
492   "*Mark used for articles that are to be downloaded."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-unsendable-mark ?=
497   "*Mark used for articles that won't be sent."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-score-over-mark ?+
502   "*Score mark used for articles with high scores."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-score-below-mark ?-
507   "*Score mark used for articles with low scores."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-empty-thread-mark ?  ;Whitespace
512   "*There is no thread under the article."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-not-empty-thread-mark ?=
517   "*There is a thread under the article."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-view-pseudo-asynchronously nil
522   "*If non-nil, Gnus will view pseudo-articles asynchronously."
523   :group 'gnus-extract-view
524   :type 'boolean)
525
526 (defcustom gnus-auto-expirable-marks
527   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
528         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
529         gnus-souped-mark gnus-duplicate-mark)
530   "*The list of marks converted into expiration if a group is auto-expirable."
531   :version "21.1"
532   :group 'gnus-summary
533   :type '(repeat character))
534
535 (defcustom gnus-inhibit-user-auto-expire t
536   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
537   :version "21.1"
538   :group 'gnus-summary
539   :type 'boolean)
540
541 (defcustom gnus-view-pseudos nil
542   "*If `automatic', pseudo-articles will be viewed automatically.
543 If `not-confirm', pseudos will be viewed automatically, and the user
544 will not be asked to confirm the command."
545   :group 'gnus-extract-view
546   :type '(choice (const :tag "off" nil)
547                  (const automatic)
548                  (const not-confirm)))
549
550 (defcustom gnus-view-pseudos-separately t
551   "*If non-nil, one pseudo-article will be created for each file to be viewed.
552 If nil, all files that use the same viewing command will be given as a
553 list of parameters to that command."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-insert-pseudo-articles t
558   "*If non-nil, insert pseudo-articles when decoding articles."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-summary-dummy-line-format
563   "  %(:                          :%) %S\n"
564   "*The format specification for the dummy roots in the summary buffer.
565 It works along the same lines as a normal formatting string,
566 with some simple extensions.
567
568 %S  The subject"
569   :group 'gnus-threading
570   :type 'string)
571
572 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
573   "*The format specification for the summary mode line.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions:
576
577 %G  Group name
578 %p  Unprefixed group name
579 %A  Current article number
580 %z  Current article score
581 %V  Gnus version
582 %U  Number of unread articles in the group
583 %e  Number of unselected articles in the group
584 %Z  A string with unread/unselected article counts
585 %g  Shortish group name
586 %S  Subject of the current article
587 %u  User-defined spec
588 %s  Current score file name
589 %d  Number of dormant articles
590 %r  Number of articles that have been marked as read in this session
591 %E  Number of articles expunged by the score files"
592   :group 'gnus-summary-format
593   :type 'string)
594
595 (defcustom gnus-list-identifiers nil
596   "Regexp that matches list identifiers to be removed from subject.
597 This can also be a list of regexps."
598   :version "21.1"
599   :group 'gnus-summary-format
600   :group 'gnus-article-hiding
601   :type '(choice (const :tag "none" nil)
602                  (regexp :value ".*")
603                  (repeat :value (".*") regexp)))
604
605 (defcustom gnus-summary-mark-below 0
606   "*Mark all articles with a score below this variable as read.
607 This variable is local to each summary buffer and usually set by the
608 score file."
609   :group 'gnus-score-default
610   :type 'integer)
611
612 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
613   "*List of functions used for sorting articles in the summary buffer.
614 This variable is only used when not using a threaded display."
615   :group 'gnus-summary-sort
616   :type '(repeat (choice (function-item gnus-article-sort-by-number)
617                          (function-item gnus-article-sort-by-author)
618                          (function-item gnus-article-sort-by-subject)
619                          (function-item gnus-article-sort-by-date)
620                          (function-item gnus-article-sort-by-score)
621                          (function :tag "other"))))
622
623 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
624   "*List of functions used for sorting threads in the summary buffer.
625 By default, threads are sorted by article number.
626
627 Each function takes two threads and return non-nil if the first thread
628 should be sorted before the other.  If you use more than one function,
629 the primary sort function should be the last.  You should probably
630 always include `gnus-thread-sort-by-number' in the list of sorting
631 functions -- preferably first.
632
633 Ready-made functions include `gnus-thread-sort-by-number',
634 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
635 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
636 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
637   :group 'gnus-summary-sort
638   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
639                          (function-item gnus-thread-sort-by-author)
640                          (function-item gnus-thread-sort-by-subject)
641                          (function-item gnus-thread-sort-by-date)
642                          (function-item gnus-thread-sort-by-score)
643                          (function-item gnus-thread-sort-by-total-score)
644                          (function :tag "other"))))
645
646 (defcustom gnus-thread-score-function '+
647   "*Function used for calculating the total score of a thread.
648
649 The function is called with the scores of the article and each
650 subthread and should then return the score of the thread.
651
652 Some functions you can use are `+', `max', or `min'."
653   :group 'gnus-summary-sort
654   :type 'function)
655
656 (defcustom gnus-summary-expunge-below nil
657   "All articles that have a score less than this variable will be expunged.
658 This variable is local to the summary buffers."
659   :group 'gnus-score-default
660   :type '(choice (const :tag "off" nil)
661                  integer))
662
663 (defcustom gnus-thread-expunge-below nil
664   "All threads that have a total score less than this variable will be expunged.
665 See `gnus-thread-score-function' for en explanation of what a
666 \"thread score\" is.
667
668 This variable is local to the summary buffers."
669   :group 'gnus-threading
670   :group 'gnus-score-default
671   :type '(choice (const :tag "off" nil)
672                  integer))
673
674 (defcustom gnus-summary-mode-hook nil
675   "*A hook for Gnus summary mode.
676 This hook is run before any variables are set in the summary buffer."
677   :options '(turn-on-gnus-mailing-list-mode)
678   :group 'gnus-summary-various
679   :type 'hook)
680
681 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
682 (when (featurep 'xemacs)
683   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
684   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
685   (add-hook 'gnus-summary-mode-hook
686             'gnus-xmas-switch-horizontal-scrollbar-off))
687
688 (defcustom gnus-summary-menu-hook nil
689   "*Hook run after the creation of the summary mode menu."
690   :group 'gnus-summary-visual
691   :type 'hook)
692
693 (defcustom gnus-summary-exit-hook nil
694   "*A hook called on exit from the summary buffer.
695 It will be called with point in the group buffer."
696   :group 'gnus-summary-exit
697   :type 'hook)
698
699 (defcustom gnus-summary-prepare-hook nil
700   "*A hook called after the summary buffer has been generated.
701 If you want to modify the summary buffer, you can use this hook."
702   :group 'gnus-summary-various
703   :type 'hook)
704
705 (defcustom gnus-summary-prepared-hook nil
706   "*A hook called as the last thing after the summary buffer has been generated."
707   :group 'gnus-summary-various
708   :type 'hook)
709
710 (defcustom gnus-summary-generate-hook nil
711   "*A hook run just before generating the summary buffer.
712 This hook is commonly used to customize threading variables and the
713 like."
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 (defcustom gnus-select-group-hook nil
718   "*A hook called when a newsgroup is selected.
719
720 If you'd like to simplify subjects like the
721 `gnus-summary-next-same-subject' command does, you can use the
722 following hook:
723
724  (setq gnus-select-group-hook
725       (list
726         (lambda ()
727           (mapcar (lambda (header)
728                      (mail-header-set-subject
729                       header
730                       (gnus-simplify-subject
731                        (mail-header-subject header) 're-only)))
732                   gnus-newsgroup-headers))))"
733   :group 'gnus-group-select
734   :type 'hook)
735
736 (defcustom gnus-select-article-hook nil
737   "*A hook called when an article is selected."
738   :group 'gnus-summary-choose
739   :type 'hook)
740
741 (defcustom gnus-visual-mark-article-hook
742   (list 'gnus-highlight-selected-summary)
743   "*Hook run after selecting an article in the summary buffer.
744 It is meant to be used for highlighting the article in some way.  It
745 is not run if `gnus-visual' is nil."
746   :group 'gnus-summary-visual
747   :type 'hook)
748
749 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
750   "*A hook called before parsing the headers."
751   :group 'gnus-various
752   :type 'hook)
753
754 (defcustom gnus-exit-group-hook nil
755   "*A hook called when exiting summary mode.
756 This hook is not called from the non-updating exit commands like `Q'."
757   :group 'gnus-various
758   :type 'hook)
759
760 (defcustom gnus-summary-update-hook
761   (list 'gnus-summary-highlight-line)
762   "*A hook called when a summary line is changed.
763 The hook will not be called if `gnus-visual' is nil.
764
765 The default function `gnus-summary-highlight-line' will
766 highlight the line according to the `gnus-summary-highlight'
767 variable."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
772   "*A hook called when an article is selected for the first time.
773 The hook is intended to mark an article as read (or unread)
774 automatically when it is selected."
775   :group 'gnus-summary-choose
776   :type 'hook)
777
778 (defcustom gnus-group-no-more-groups-hook nil
779   "*A hook run when returning to group mode having no more (unread) groups."
780   :group 'gnus-group-select
781   :type 'hook)
782
783 (defcustom gnus-ps-print-hook nil
784   "*A hook run before ps-printing something from Gnus."
785   :group 'gnus-summary
786   :type 'hook)
787
788 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
789   "Face used for highlighting the current article in the summary buffer."
790   :group 'gnus-summary-visual
791   :type 'face)
792
793 (defcustom gnus-summary-highlight
794   '(((= mark gnus-canceled-mark)
795      . gnus-summary-cancelled-face)
796     ((and (> score default)
797           (or (= mark gnus-dormant-mark)
798               (= mark gnus-ticked-mark)))
799      . gnus-summary-high-ticked-face)
800     ((and (< score default)
801           (or (= mark gnus-dormant-mark)
802               (= mark gnus-ticked-mark)))
803      . gnus-summary-low-ticked-face)
804     ((or (= mark gnus-dormant-mark)
805          (= mark gnus-ticked-mark))
806      . gnus-summary-normal-ticked-face)
807     ((and (> score default) (= mark gnus-ancient-mark))
808      . gnus-summary-high-ancient-face)
809     ((and (< score default) (= mark gnus-ancient-mark))
810      . gnus-summary-low-ancient-face)
811     ((= mark gnus-ancient-mark)
812      . gnus-summary-normal-ancient-face)
813     ((and (> score default) (= mark gnus-unread-mark))
814      . gnus-summary-high-unread-face)
815     ((and (< score default) (= mark gnus-unread-mark))
816      . gnus-summary-low-unread-face)
817     ((and (memq article gnus-newsgroup-incorporated)
818           (= mark gnus-unread-mark))
819      . gnus-summary-incorporated-face)
820     ((= mark gnus-unread-mark)
821      . gnus-summary-normal-unread-face)
822     ((and (> score default) (memq mark (list gnus-downloadable-mark
823                                              gnus-undownloaded-mark)))
824      . gnus-summary-high-unread-face)
825     ((and (< score default) (memq mark (list gnus-downloadable-mark
826                                              gnus-undownloaded-mark)))
827      . gnus-summary-low-unread-face)
828     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
829      . gnus-summary-normal-unread-face)
830     ((> score default)
831      . gnus-summary-high-read-face)
832     ((< score default)
833      . gnus-summary-low-read-face)
834     (t
835      . gnus-summary-normal-read-face))
836   "*Controls the highlighting of summary buffer lines.
837
838 A list of (FORM . FACE) pairs.  When deciding how a a particular
839 summary line should be displayed, each form is evaluated.  The content
840 of the face field after the first true form is used.  You can change
841 how those summary lines are displayed, by editing the face field.
842
843 You can use the following variables in the FORM field.
844
845 score:   The articles score
846 default: The default article score.
847 below:   The score below which articles are automatically marked as read.
848 mark:    The articles mark."
849   :group 'gnus-summary-visual
850   :type '(repeat (cons (sexp :tag "Form" nil)
851                        face)))
852
853 (defcustom gnus-alter-header-function nil
854   "Function called to allow alteration of article header structures.
855 The function is called with one parameter, the article header vector,
856 which it may alter in any way.")
857
858 (defvar gnus-decode-encoded-word-function
859   (mime-find-field-decoder 'From 'nov)
860   "Variable that says which function should be used to decode a string with encoded words.")
861
862 (defcustom gnus-extra-headers nil
863   "*Extra headers to parse."
864   :version "21.1"
865   :group 'gnus-summary
866   :type '(repeat symbol))
867
868 (defcustom gnus-ignored-from-addresses
869   (and user-mail-address (regexp-quote user-mail-address))
870   "*Regexp of From headers that may be suppressed in favor of To headers."
871   :version "21.1"
872   :group 'gnus-summary
873   :type 'regexp)
874
875 (defcustom gnus-group-charset-alist
876   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
877     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
878     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
879     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
880     ("^relcom\\>" koi8-r)
881     ("^fido7\\>" koi8-r)
882     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
883     ("^israel\\>" iso-8859-1)
884     ("^han\\>" euc-kr)
885     ("^alt.chinese.text.big5\\>" chinese-big5)
886     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
887     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
888     (".*" iso-8859-1))
889   "Alist of regexps (to match group names) and default charsets to be used when reading."
890   :type '(repeat (list (regexp :tag "Group")
891                        (symbol :tag "Charset")))
892   :group 'gnus-charset)
893
894 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
895   "List of charsets that should be ignored.
896 When these charsets are used in the \"charset\" parameter, the
897 default charset will be used instead."
898   :version "21.1"
899   :type '(repeat symbol)
900   :group 'gnus-charset)
901
902 (defcustom gnus-group-ignored-charsets-alist
903   '(("alt\\.chinese\\.text" iso-8859-1))
904   "Alist of regexps (to match group names) and charsets that should be ignored.
905 When these charsets are used in the \"charset\" parameter, the
906 default charset will be used instead."
907   :type '(repeat (cons (regexp :tag "Group")
908                        (repeat symbol)))
909   :group 'gnus-charset)
910
911 (defcustom gnus-group-highlight-words-alist nil
912   "Alist of group regexps and highlight regexps.
913 This variable uses the same syntax as `gnus-emphasis-alist'."
914   :version "21.1"
915   :type '(repeat (cons (regexp :tag "Group")
916                        (repeat (list (regexp :tag "Highlight regexp")
917                                      (number :tag "Group for entire word" 0)
918                                      (number :tag "Group for displayed part" 0)
919                                      (symbol :tag "Face"
920                                              gnus-emphasis-highlight-words)))))
921   :group 'gnus-summary-visual)
922
923 (defcustom gnus-use-wheel nil
924   "Use Intelli-mouse on summary movement"
925   :type 'boolean
926   :group 'gnus-summary-maneuvering)
927
928 (defcustom gnus-wheel-scroll-amount '(5 . 1)
929   "Amount to scroll messages by spinning the mouse wheel.
930 This is actually a cons cell, where the first item is the amount to scroll
931 on a normal wheel event, and the second is the amount to scroll when the
932 wheel is moved with the shift key depressed."
933   :type '(cons (integer :tag "Shift") integer)
934   :group 'gnus-summary-maneuvering)
935
936 (defcustom gnus-wheel-edge-resistance 2
937   "How hard it should be to change the current article
938 by moving the mouse over the edge of the article window."
939   :type 'integer
940   :group 'gnus-summary-maneuvering)
941
942 (defcustom gnus-summary-show-article-charset-alist
943   nil
944   "Alist of number and charset.
945 The article will be shown with the charset corresponding to the
946 numbered argument.
947 For example: ((1 . cn-gb-2312) (2 . big5))."
948   :version "21.1"
949   :type '(repeat (cons (number :tag "Argument" 1)
950                        (symbol :tag "Charset")))
951   :group 'gnus-charset)
952
953 (defcustom gnus-preserve-marks t
954   "Whether marks are preserved when moving, copying and respooling messages."
955   :version "21.1"
956   :type 'boolean
957   :group 'gnus-summary-marks)
958
959 (defcustom gnus-alter-articles-to-read-function nil
960   "Function to be called to alter the list of articles to be selected."
961   :type 'function
962   :group 'gnus-summary)
963
964 (defcustom gnus-orphan-score nil
965   "*All orphans get this score added.  Set in the score file."
966   :group 'gnus-score-default
967   :type '(choice (const nil)
968                  integer))
969
970 (defcustom gnus-summary-save-parts-default-mime "image/.*"
971   "*A regexp to match MIME parts when saving multiple parts of a message
972 with gnus-summary-save-parts (X m). This regexp will be used by default
973 when prompting the user for which type of files to save."
974   :group 'gnus-summary
975   :type 'regexp)
976
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 ;;; Internal variables
987
988 (defvar gnus-article-mime-handles nil)
989 (defvar gnus-article-decoded-p nil)
990 (defvar gnus-article-charset nil)
991 (defvar gnus-article-ignored-charsets nil)
992 (defvar gnus-scores-exclude-files nil)
993 (defvar gnus-page-broken nil)
994 (defvar gnus-inhibit-mime-unbuttonizing nil)
995
996 (defvar gnus-original-article nil)
997 (defvar gnus-article-internal-prepare-hook nil)
998 (defvar gnus-newsgroup-process-stack nil)
999
1000 (defvar gnus-thread-indent-array nil)
1001 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1002 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1003   "Function called to sort the articles within a thread after it has been gathered together.")
1004
1005 (defvar gnus-summary-save-parts-type-history nil)
1006 (defvar gnus-summary-save-parts-last-directory nil)
1007
1008 (defvar gnus-summary-save-parts-type-history nil)
1009 (defvar gnus-summary-save-parts-last-directory nil)
1010
1011 ;; Avoid highlighting in kill files.
1012 (defvar gnus-summary-inhibit-highlight nil)
1013 (defvar gnus-newsgroup-selected-overlay nil)
1014 (defvar gnus-inhibit-limiting nil)
1015 (defvar gnus-newsgroup-adaptive-score-file nil)
1016 (defvar gnus-current-score-file nil)
1017 (defvar gnus-current-move-group nil)
1018 (defvar gnus-current-copy-group nil)
1019 (defvar gnus-current-crosspost-group nil)
1020
1021 (defvar gnus-newsgroup-dependencies nil)
1022 (defvar gnus-newsgroup-adaptive nil)
1023 (defvar gnus-summary-display-article-function nil)
1024 (defvar gnus-summary-highlight-line-function nil
1025   "Function called after highlighting a summary line.")
1026
1027 (defvar gnus-summary-line-format-alist
1028   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1029     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1030     (?s gnus-tmp-subject-or-nil ?s)
1031     (?n gnus-tmp-name ?s)
1032     (?A (std11-address-string
1033          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1034     (?a (or (std11-full-name-string
1035              (car (mime-entity-read-field gnus-tmp-header 'From)))
1036             gnus-tmp-from) ?s)
1037     (?F gnus-tmp-from ?s)
1038     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1039     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1040     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1041     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1042     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1043     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1044     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1045     (?L gnus-tmp-lines ?d)
1046     (?I gnus-tmp-indentation ?s)
1047     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1048     (?R gnus-tmp-replied ?c)
1049     (?\[ gnus-tmp-opening-bracket ?c)
1050     (?\] gnus-tmp-closing-bracket ?c)
1051     (?\> (make-string gnus-tmp-level ? ) ?s)
1052     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1053     (?i gnus-tmp-score ?d)
1054     (?z gnus-tmp-score-char ?c)
1055     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1056     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1057     (?U gnus-tmp-unread ?c)
1058     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1059     (?t (gnus-summary-number-of-articles-in-thread
1060          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1061         ?d)
1062     (?e (gnus-summary-number-of-articles-in-thread
1063          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1064         ?c)
1065     (?u gnus-tmp-user-defined ?s)
1066     (?P (gnus-pick-line-number) ?d))
1067   "An alist of format specifications that can appear in summary lines.
1068 These are paired with what variables they correspond with, along with
1069 the type of the variable (string, integer, character, etc).")
1070
1071 (defvar gnus-summary-dummy-line-format-alist
1072   `((?S gnus-tmp-subject ?s)
1073     (?N gnus-tmp-number ?d)
1074     (?u gnus-tmp-user-defined ?s)))
1075
1076 (defvar gnus-summary-mode-line-format-alist
1077   `((?G gnus-tmp-group-name ?s)
1078     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1079     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1080     (?A gnus-tmp-article-number ?d)
1081     (?Z gnus-tmp-unread-and-unselected ?s)
1082     (?V gnus-version ?s)
1083     (?U gnus-tmp-unread-and-unticked ?d)
1084     (?S gnus-tmp-subject ?s)
1085     (?e gnus-tmp-unselected ?d)
1086     (?u gnus-tmp-user-defined ?s)
1087     (?d (length gnus-newsgroup-dormant) ?d)
1088     (?t (length gnus-newsgroup-marked) ?d)
1089     (?r (length gnus-newsgroup-reads) ?d)
1090     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1091     (?E gnus-newsgroup-expunged-tally ?d)
1092     (?s (gnus-current-score-file-nondirectory) ?s)))
1093
1094 (defvar gnus-last-search-regexp nil
1095   "Default regexp for article search command.")
1096
1097 (defvar gnus-summary-search-article-matched-data nil
1098   "Last matched data of article search command.  It is the local variable
1099 in `gnus-article-buffer' which consists of the list of start position,
1100 end position and text.")
1101
1102 (defvar gnus-last-shell-command nil
1103   "Default shell command on article.")
1104
1105 (defvar gnus-newsgroup-begin nil)
1106 (defvar gnus-newsgroup-end nil)
1107 (defvar gnus-newsgroup-last-rmail nil)
1108 (defvar gnus-newsgroup-last-mail nil)
1109 (defvar gnus-newsgroup-last-folder nil)
1110 (defvar gnus-newsgroup-last-file nil)
1111 (defvar gnus-newsgroup-auto-expire nil)
1112 (defvar gnus-newsgroup-active nil)
1113
1114 (defvar gnus-newsgroup-data nil)
1115 (defvar gnus-newsgroup-data-reverse nil)
1116 (defvar gnus-newsgroup-limit nil)
1117 (defvar gnus-newsgroup-limits nil)
1118
1119 (defvar gnus-newsgroup-unreads nil
1120   "List of unread articles in the current newsgroup.")
1121
1122 (defvar gnus-newsgroup-unselected nil
1123   "List of unselected unread articles in the current newsgroup.")
1124
1125 (defvar gnus-newsgroup-reads nil
1126   "Alist of read articles and article marks in the current newsgroup.")
1127
1128 (defvar gnus-newsgroup-expunged-tally nil)
1129
1130 (defvar gnus-newsgroup-marked nil
1131   "List of ticked articles in the current newsgroup (a subset of unread art).")
1132
1133 (defvar gnus-newsgroup-killed nil
1134   "List of ranges of articles that have been through the scoring process.")
1135
1136 (defvar gnus-newsgroup-cached nil
1137   "List of articles that come from the article cache.")
1138
1139 (defvar gnus-newsgroup-saved nil
1140   "List of articles that have been saved.")
1141
1142 (defvar gnus-newsgroup-kill-headers nil)
1143
1144 (defvar gnus-newsgroup-replied nil
1145   "List of articles that have been replied to in the current newsgroup.")
1146
1147 (defvar gnus-newsgroup-expirable nil
1148   "List of articles in the current newsgroup that can be expired.")
1149
1150 (defvar gnus-newsgroup-processable nil
1151   "List of articles in the current newsgroup that can be processed.")
1152
1153 (defvar gnus-newsgroup-downloadable nil
1154   "List of articles in the current newsgroup that can be processed.")
1155
1156 (defvar gnus-newsgroup-undownloaded nil
1157   "List of articles in the current newsgroup that haven't been downloaded..")
1158
1159 (defvar gnus-newsgroup-unsendable nil
1160   "List of articles in the current newsgroup that won't be sent.")
1161
1162 (defvar gnus-newsgroup-bookmarks nil
1163   "List of articles in the current newsgroup that have bookmarks.")
1164
1165 (defvar gnus-newsgroup-dormant nil
1166   "List of dormant articles in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-scored nil
1169   "List of scored articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-incorporated nil
1172   "List of incorporated articles in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-headers nil
1175   "List of article headers in the current newsgroup.")
1176
1177 (defvar gnus-newsgroup-threads nil)
1178
1179 (defvar gnus-newsgroup-prepared nil
1180   "Whether the current group has been prepared properly.")
1181
1182 (defvar gnus-newsgroup-ancient nil
1183   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-sparse nil)
1186
1187 (defvar gnus-current-article nil)
1188 (defvar gnus-article-current nil)
1189 (defvar gnus-current-headers nil)
1190 (defvar gnus-have-all-headers nil)
1191 (defvar gnus-last-article nil)
1192 (defvar gnus-newsgroup-history nil)
1193 (defvar gnus-newsgroup-charset nil)
1194 (defvar gnus-newsgroup-ephemeral-charset nil)
1195 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1196
1197 (defvar gnus-article-before-search nil)
1198
1199 (defconst gnus-summary-local-variables
1200   '(gnus-newsgroup-name
1201     gnus-newsgroup-begin gnus-newsgroup-end
1202     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1203     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1204     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1205     gnus-newsgroup-unselected gnus-newsgroup-marked
1206     gnus-newsgroup-reads gnus-newsgroup-saved
1207     gnus-newsgroup-replied gnus-newsgroup-expirable
1208     gnus-newsgroup-processable gnus-newsgroup-killed
1209     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1210     gnus-newsgroup-unsendable
1211     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1212     gnus-newsgroup-headers gnus-newsgroup-threads
1213     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1214     gnus-current-article gnus-current-headers gnus-have-all-headers
1215     gnus-last-article gnus-article-internal-prepare-hook
1216     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1217     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1218     gnus-thread-expunge-below
1219     gnus-score-alist gnus-current-score-file
1220     (gnus-summary-expunge-below . global)
1221     (gnus-summary-mark-below . global)
1222     (gnus-orphan-score . global)
1223     gnus-newsgroup-active gnus-scores-exclude-files
1224     gnus-newsgroup-history gnus-newsgroup-ancient
1225     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1226     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1227     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1228     (gnus-newsgroup-expunged-tally . 0)
1229     gnus-cache-removable-articles gnus-newsgroup-cached
1230     gnus-newsgroup-data gnus-newsgroup-data-reverse
1231     gnus-newsgroup-limit gnus-newsgroup-limits
1232     gnus-newsgroup-charset
1233     gnus-newsgroup-incorporated)
1234   "Variables that are buffer-local to the summary buffers.")
1235
1236 (defvar gnus-newsgroup-variables nil
1237   "Variables that have separate values in the newsgroups.")
1238
1239 ;; Byte-compiler warning.
1240 (eval-when-compile (defvar gnus-article-mode-map))
1241
1242 ;; Subject simplification.
1243
1244 (defun gnus-simplify-whitespace (str)
1245   "Remove excessive whitespace from STR."
1246   (let ((mystr str))
1247     ;; Multiple spaces.
1248     (while (string-match "[ \t][ \t]+" mystr)
1249       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1250                           " "
1251                           (substring mystr (match-end 0)))))
1252     ;; Leading spaces.
1253     (when (string-match "^[ \t]+" mystr)
1254       (setq mystr (substring mystr (match-end 0))))
1255     ;; Trailing spaces.
1256     (when (string-match "[ \t]+$" mystr)
1257       (setq mystr (substring mystr 0 (match-beginning 0))))
1258     mystr))
1259
1260 (defsubst gnus-simplify-subject-re (subject)
1261   "Remove \"Re:\" from subject lines."
1262   (if (string-match "^[Rr][Ee]: *" subject)
1263       (substring subject (match-end 0))
1264     subject))
1265
1266 (defun gnus-simplify-subject (subject &optional re-only)
1267   "Remove `Re:' and words in parentheses.
1268 If RE-ONLY is non-nil, strip leading `Re:'s only."
1269   (let ((case-fold-search t))           ;Ignore case.
1270     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1271     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1272       (setq subject (substring subject (match-end 0))))
1273     ;; Remove uninteresting prefixes.
1274     (when (and (not re-only)
1275                gnus-simplify-ignored-prefixes
1276                (string-match gnus-simplify-ignored-prefixes subject))
1277       (setq subject (substring subject (match-end 0))))
1278     ;; Remove words in parentheses from end.
1279     (unless re-only
1280       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1281         (setq subject (substring subject 0 (match-beginning 0)))))
1282     ;; Return subject string.
1283     subject))
1284
1285 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1286 ;; all whitespace.
1287 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1288   (goto-char (point-min))
1289   (while (re-search-forward regexp nil t)
1290     (replace-match (or newtext ""))))
1291
1292 (defun gnus-simplify-buffer-fuzzy ()
1293   "Simplify string in the buffer fuzzily.
1294 The string in the accessible portion of the current buffer is simplified.
1295 It is assumed to be a single-line subject.
1296 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1297 matter is removed.  Additional things can be deleted by setting
1298 `gnus-simplify-subject-fuzzy-regexp'."
1299   (let ((case-fold-search t)
1300         (modified-tick))
1301     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1302
1303     (while (not (eq modified-tick (buffer-modified-tick)))
1304       (setq modified-tick (buffer-modified-tick))
1305       (cond
1306        ((listp gnus-simplify-subject-fuzzy-regexp)
1307         (mapcar 'gnus-simplify-buffer-fuzzy-step
1308                 gnus-simplify-subject-fuzzy-regexp))
1309        (gnus-simplify-subject-fuzzy-regexp
1310         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1311       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1312       (gnus-simplify-buffer-fuzzy-step
1313        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1314       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1315
1316     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1317     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1318     (gnus-simplify-buffer-fuzzy-step " $")
1319     (gnus-simplify-buffer-fuzzy-step "^ +")))
1320
1321 (defun gnus-simplify-subject-fuzzy (subject)
1322   "Simplify a subject string fuzzily.
1323 See `gnus-simplify-buffer-fuzzy' for details."
1324   (save-excursion
1325     (gnus-set-work-buffer)
1326     (let ((case-fold-search t))
1327       ;; Remove uninteresting prefixes.
1328       (when (and gnus-simplify-ignored-prefixes
1329                  (string-match gnus-simplify-ignored-prefixes subject))
1330         (setq subject (substring subject (match-end 0))))
1331       (insert subject)
1332       (inline (gnus-simplify-buffer-fuzzy))
1333       (buffer-string))))
1334
1335 (defsubst gnus-simplify-subject-fully (subject)
1336   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1337   (cond
1338    (gnus-simplify-subject-functions
1339     (gnus-map-function gnus-simplify-subject-functions subject))
1340    ((null gnus-summary-gather-subject-limit)
1341     (gnus-simplify-subject-re subject))
1342    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1343     (gnus-simplify-subject-fuzzy subject))
1344    ((numberp gnus-summary-gather-subject-limit)
1345     (gnus-limit-string (gnus-simplify-subject-re subject)
1346                        gnus-summary-gather-subject-limit))
1347    (t
1348     subject)))
1349
1350 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1351   "Check whether two subjects are equal.
1352 If optional argument simple-first is t, first argument is already
1353 simplified."
1354   (cond
1355    ((null simple-first)
1356     (equal (gnus-simplify-subject-fully s1)
1357            (gnus-simplify-subject-fully s2)))
1358    (t
1359     (equal s1
1360            (gnus-simplify-subject-fully s2)))))
1361
1362 (defun gnus-summary-bubble-group ()
1363   "Increase the score of the current group.
1364 This is a handy function to add to `gnus-summary-exit-hook' to
1365 increase the score of each group you read."
1366   (gnus-group-add-score gnus-newsgroup-name))
1367
1368 \f
1369 ;;;
1370 ;;; Gnus summary mode
1371 ;;;
1372
1373 (put 'gnus-summary-mode 'mode-class 'special)
1374
1375 (when t
1376   ;; Non-orthogonal keys
1377
1378   (gnus-define-keys gnus-summary-mode-map
1379     " " gnus-summary-next-page
1380     "\177" gnus-summary-prev-page
1381     [delete] gnus-summary-prev-page
1382     [backspace] gnus-summary-prev-page
1383     "\r" gnus-summary-scroll-up
1384     "\M-\r" gnus-summary-scroll-down
1385     "n" gnus-summary-next-unread-article
1386     "p" gnus-summary-prev-unread-article
1387     "N" gnus-summary-next-article
1388     "P" gnus-summary-prev-article
1389     "\M-\C-n" gnus-summary-next-same-subject
1390     "\M-\C-p" gnus-summary-prev-same-subject
1391     "\M-n" gnus-summary-next-unread-subject
1392     "\M-p" gnus-summary-prev-unread-subject
1393     "." gnus-summary-first-unread-article
1394     "," gnus-summary-best-unread-article
1395     "\M-s" gnus-summary-search-article-forward
1396     "\M-r" gnus-summary-search-article-backward
1397     "<" gnus-summary-beginning-of-article
1398     ">" gnus-summary-end-of-article
1399     "j" gnus-summary-goto-article
1400     "^" gnus-summary-refer-parent-article
1401     "\M-^" gnus-summary-refer-article
1402     "u" gnus-summary-tick-article-forward
1403     "!" gnus-summary-tick-article-forward
1404     "U" gnus-summary-tick-article-backward
1405     "d" gnus-summary-mark-as-read-forward
1406     "D" gnus-summary-mark-as-read-backward
1407     "E" gnus-summary-mark-as-expirable
1408     "\M-u" gnus-summary-clear-mark-forward
1409     "\M-U" gnus-summary-clear-mark-backward
1410     "k" gnus-summary-kill-same-subject-and-select
1411     "\C-k" gnus-summary-kill-same-subject
1412     "\M-\C-k" gnus-summary-kill-thread
1413     "\M-\C-l" gnus-summary-lower-thread
1414     "e" gnus-summary-edit-article
1415     "#" gnus-summary-mark-as-processable
1416     "\M-#" gnus-summary-unmark-as-processable
1417     "\M-\C-t" gnus-summary-toggle-threads
1418     "\M-\C-s" gnus-summary-show-thread
1419     "\M-\C-h" gnus-summary-hide-thread
1420     "\M-\C-f" gnus-summary-next-thread
1421     "\M-\C-b" gnus-summary-prev-thread
1422     [(meta down)] gnus-summary-next-thread
1423     [(meta up)] gnus-summary-prev-thread
1424     "\M-\C-u" gnus-summary-up-thread
1425     "\M-\C-d" gnus-summary-down-thread
1426     "&" gnus-summary-execute-command
1427     "c" gnus-summary-catchup-and-exit
1428     "\C-w" gnus-summary-mark-region-as-read
1429     "\C-t" gnus-summary-toggle-truncation
1430     "?" gnus-summary-mark-as-dormant
1431     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1432     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1433     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1434     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1435     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1436     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1437     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1438     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1439     "=" gnus-summary-expand-window
1440     "\C-x\C-s" gnus-summary-reselect-current-group
1441     "\M-g" gnus-summary-rescan-group
1442     "w" gnus-summary-stop-page-breaking
1443     "\C-c\C-r" gnus-summary-caesar-message
1444     "\M-t" gnus-summary-toggle-mime
1445     "f" gnus-summary-followup
1446     "F" gnus-summary-followup-with-original
1447     "C" gnus-summary-cancel-article
1448     "r" gnus-summary-reply
1449     "R" gnus-summary-reply-with-original
1450     "\C-c\C-f" gnus-summary-mail-forward
1451     "o" gnus-summary-save-article
1452     "\C-o" gnus-summary-save-article-mail
1453     "|" gnus-summary-pipe-output
1454     "\M-k" gnus-summary-edit-local-kill
1455     "\M-K" gnus-summary-edit-global-kill
1456     ;; "V" gnus-version
1457     "\C-c\C-d" gnus-summary-describe-group
1458     "q" gnus-summary-exit
1459     "Q" gnus-summary-exit-no-update
1460     "\C-c\C-i" gnus-info-find-node
1461     gnus-mouse-2 gnus-mouse-pick-article
1462     "m" gnus-summary-mail-other-window
1463     "a" gnus-summary-post-news
1464     "x" gnus-summary-limit-to-unread
1465     "s" gnus-summary-isearch-article
1466     "t" gnus-article-toggle-headers
1467     "g" gnus-summary-show-article
1468     "l" gnus-summary-goto-last-article
1469     "v" gnus-summary-preview-mime-message
1470     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1471     "\C-d" gnus-summary-enter-digest-group
1472     "\M-\C-d" gnus-summary-read-document
1473     "\M-\C-e" gnus-summary-edit-parameters
1474     "\M-\C-a" gnus-summary-customize-parameters
1475     "\C-c\C-b" gnus-bug
1476     "*" gnus-cache-enter-article
1477     "\M-*" gnus-cache-remove-article
1478     "\M-&" gnus-summary-universal-argument
1479     "\C-l" gnus-recenter
1480     "I" gnus-summary-increase-score
1481     "L" gnus-summary-lower-score
1482     "\M-i" gnus-symbolic-argument
1483     "h" gnus-summary-select-article-buffer
1484
1485     "V" gnus-summary-score-map
1486     "X" gnus-uu-extract-map
1487     "S" gnus-summary-send-map)
1488
1489   ;; Sort of orthogonal keymap
1490   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1491     "t" gnus-summary-tick-article-forward
1492     "!" gnus-summary-tick-article-forward
1493     "d" gnus-summary-mark-as-read-forward
1494     "r" gnus-summary-mark-as-read-forward
1495     "c" gnus-summary-clear-mark-forward
1496     " " gnus-summary-clear-mark-forward
1497     "e" gnus-summary-mark-as-expirable
1498     "x" gnus-summary-mark-as-expirable
1499     "?" gnus-summary-mark-as-dormant
1500     "b" gnus-summary-set-bookmark
1501     "B" gnus-summary-remove-bookmark
1502     "#" gnus-summary-mark-as-processable
1503     "\M-#" gnus-summary-unmark-as-processable
1504     "S" gnus-summary-limit-include-expunged
1505     "C" gnus-summary-catchup
1506     "H" gnus-summary-catchup-to-here
1507     "\C-c" gnus-summary-catchup-all
1508     "k" gnus-summary-kill-same-subject-and-select
1509     "K" gnus-summary-kill-same-subject
1510     "P" gnus-uu-mark-map)
1511
1512   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1513     "c" gnus-summary-clear-above
1514     "u" gnus-summary-tick-above
1515     "m" gnus-summary-mark-above
1516     "k" gnus-summary-kill-below)
1517
1518   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1519     "/" gnus-summary-limit-to-subject
1520     "n" gnus-summary-limit-to-articles
1521     "w" gnus-summary-pop-limit
1522     "s" gnus-summary-limit-to-subject
1523     "a" gnus-summary-limit-to-author
1524     "u" gnus-summary-limit-to-unread
1525     "m" gnus-summary-limit-to-marks
1526     "M" gnus-summary-limit-exclude-marks
1527     "v" gnus-summary-limit-to-score
1528     "*" gnus-summary-limit-include-cached
1529     "D" gnus-summary-limit-include-dormant
1530     "T" gnus-summary-limit-include-thread
1531     "d" gnus-summary-limit-exclude-dormant
1532     "t" gnus-summary-limit-to-age
1533     "x" gnus-summary-limit-to-extra
1534     "E" gnus-summary-limit-include-expunged
1535     "c" gnus-summary-limit-exclude-childless-dormant
1536     "C" gnus-summary-limit-mark-excluded-as-read)
1537
1538   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1539     "n" gnus-summary-next-unread-article
1540     "p" gnus-summary-prev-unread-article
1541     "N" gnus-summary-next-article
1542     "P" gnus-summary-prev-article
1543     "\C-n" gnus-summary-next-same-subject
1544     "\C-p" gnus-summary-prev-same-subject
1545     "\M-n" gnus-summary-next-unread-subject
1546     "\M-p" gnus-summary-prev-unread-subject
1547     "f" gnus-summary-first-unread-article
1548     "b" gnus-summary-best-unread-article
1549     "j" gnus-summary-goto-article
1550     "g" gnus-summary-goto-subject
1551     "l" gnus-summary-goto-last-article
1552     "o" gnus-summary-pop-article)
1553
1554   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1555     "k" gnus-summary-kill-thread
1556     "l" gnus-summary-lower-thread
1557     "i" gnus-summary-raise-thread
1558     "T" gnus-summary-toggle-threads
1559     "t" gnus-summary-rethread-current
1560     "^" gnus-summary-reparent-thread
1561     "s" gnus-summary-show-thread
1562     "S" gnus-summary-show-all-threads
1563     "h" gnus-summary-hide-thread
1564     "H" gnus-summary-hide-all-threads
1565     "n" gnus-summary-next-thread
1566     "p" gnus-summary-prev-thread
1567     "u" gnus-summary-up-thread
1568     "o" gnus-summary-top-thread
1569     "d" gnus-summary-down-thread
1570     "#" gnus-uu-mark-thread
1571     "\M-#" gnus-uu-unmark-thread)
1572
1573   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1574     "g" gnus-summary-prepare
1575     "c" gnus-summary-insert-cached-articles)
1576
1577   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1578     "c" gnus-summary-catchup-and-exit
1579     "C" gnus-summary-catchup-all-and-exit
1580     "E" gnus-summary-exit-no-update
1581     "J" gnus-summary-jump-to-other-group
1582     "Q" gnus-summary-exit
1583     "Z" gnus-summary-exit
1584     "n" gnus-summary-catchup-and-goto-next-group
1585     "R" gnus-summary-reselect-current-group
1586     "G" gnus-summary-rescan-group
1587     "N" gnus-summary-next-group
1588     "s" gnus-summary-save-newsrc
1589     "P" gnus-summary-prev-group)
1590
1591   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1592     " " gnus-summary-next-page
1593     "n" gnus-summary-next-page
1594     "\177" gnus-summary-prev-page
1595     [delete] gnus-summary-prev-page
1596     "p" gnus-summary-prev-page
1597     "\r" gnus-summary-scroll-up
1598     "\M-\r" gnus-summary-scroll-down
1599     "<" gnus-summary-beginning-of-article
1600     ">" gnus-summary-end-of-article
1601     "b" gnus-summary-beginning-of-article
1602     "e" gnus-summary-end-of-article
1603     "^" gnus-summary-refer-parent-article
1604     "r" gnus-summary-refer-parent-article
1605     "D" gnus-summary-enter-digest-group
1606     "R" gnus-summary-refer-references
1607     "T" gnus-summary-refer-thread
1608     "g" gnus-summary-show-article
1609     "s" gnus-summary-isearch-article
1610     "P" gnus-summary-print-article
1611     "M" gnus-mailing-list-insinuate
1612     "t" gnus-article-babel)
1613
1614   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1615     "b" gnus-article-add-buttons
1616     "B" gnus-article-add-buttons-to-head
1617     "o" gnus-article-treat-overstrike
1618     "e" gnus-article-emphasize
1619     "w" gnus-article-fill-cited-article
1620     "Q" gnus-article-fill-long-lines
1621     "C" gnus-article-capitalize-sentences
1622     "c" gnus-article-remove-cr
1623     "Z" gnus-article-decode-HZ
1624     "f" gnus-article-display-x-face
1625     "l" gnus-summary-stop-page-breaking
1626     "r" gnus-summary-caesar-message
1627     "t" gnus-article-toggle-headers
1628     "v" gnus-summary-verbose-headers
1629     "m" gnus-summary-toggle-mime
1630     "H" gnus-article-strip-headers-in-body
1631     "p" gnus-article-verify-x-pgp-sig
1632     "d" gnus-article-treat-dumbquotes
1633     "s" gnus-smiley-display)
1634
1635   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1636     "a" gnus-article-hide
1637     "h" gnus-article-toggle-headers
1638     "b" gnus-article-hide-boring-headers
1639     "s" gnus-article-hide-signature
1640     "c" gnus-article-hide-citation
1641     "C" gnus-article-hide-citation-in-followups
1642     "l" gnus-article-hide-list-identifiers
1643     "p" gnus-article-hide-pgp
1644     "B" gnus-article-strip-banner
1645     "P" gnus-article-hide-pem
1646     "\C-c" gnus-article-hide-citation-maybe)
1647
1648   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1649     "a" gnus-article-highlight
1650     "h" gnus-article-highlight-headers
1651     "c" gnus-article-highlight-citation
1652     "s" gnus-article-highlight-signature)
1653
1654   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1655     "z" gnus-article-date-ut
1656     "u" gnus-article-date-ut
1657     "l" gnus-article-date-local
1658     "p" gnus-article-date-english
1659     "e" gnus-article-date-lapsed
1660     "o" gnus-article-date-original
1661     "i" gnus-article-date-iso8601
1662     "s" gnus-article-date-user)
1663
1664   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1665     "t" gnus-article-remove-trailing-blank-lines
1666     "l" gnus-article-strip-leading-blank-lines
1667     "m" gnus-article-strip-multiple-blank-lines
1668     "a" gnus-article-strip-blank-lines
1669     "A" gnus-article-strip-all-blank-lines
1670     "s" gnus-article-strip-leading-space
1671     "e" gnus-article-strip-trailing-space)
1672
1673   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1674     "v" gnus-version
1675     "f" gnus-summary-fetch-faq
1676     "d" gnus-summary-describe-group
1677     "h" gnus-summary-describe-briefly
1678     "i" gnus-info-find-node)
1679
1680   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1681     "e" gnus-summary-expire-articles
1682     "\M-\C-e" gnus-summary-expire-articles-now
1683     "\177" gnus-summary-delete-article
1684     [delete] gnus-summary-delete-article
1685     [backspace] gnus-summary-delete-article
1686     "m" gnus-summary-move-article
1687     "r" gnus-summary-respool-article
1688     "w" gnus-summary-edit-article
1689     "c" gnus-summary-copy-article
1690     "B" gnus-summary-crosspost-article
1691     "q" gnus-summary-respool-query
1692     "t" gnus-summary-respool-trace
1693     "i" gnus-summary-import-article
1694     "p" gnus-summary-article-posted-p)
1695
1696   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1697     "o" gnus-summary-save-article
1698     "m" gnus-summary-save-article-mail
1699     "F" gnus-summary-write-article-file
1700     "r" gnus-summary-save-article-rmail
1701     "f" gnus-summary-save-article-file
1702     "b" gnus-summary-save-article-body-file
1703     "h" gnus-summary-save-article-folder
1704     "v" gnus-summary-save-article-vm
1705     "p" gnus-summary-pipe-output
1706     "s" gnus-soup-add-article)
1707
1708   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1709     "b" gnus-summary-display-buttonized
1710     "m" gnus-summary-repair-multipart
1711     "v" gnus-article-view-part
1712     "o" gnus-article-save-part
1713     "c" gnus-article-copy-part
1714     "C" gnus-article-view-part-as-charset
1715     "e" gnus-article-externalize-part
1716     "E" gnus-article-encrypt-body
1717     "i" gnus-article-inline-part
1718     "|" gnus-article-pipe-part))
1719
1720 (defun gnus-summary-make-menu-bar ()
1721   (gnus-turn-off-edit-menu 'summary)
1722
1723   (unless (boundp 'gnus-summary-misc-menu)
1724
1725     (easy-menu-define
1726      gnus-summary-kill-menu gnus-summary-mode-map ""
1727      (cons
1728       "Score"
1729       (nconc
1730        (list
1731         ["Customize" gnus-score-customize t])
1732        (gnus-make-score-map 'increase)
1733        (gnus-make-score-map 'lower)
1734        '(("Mark"
1735           ["Kill below" gnus-summary-kill-below t]
1736           ["Mark above" gnus-summary-mark-above t]
1737           ["Tick above" gnus-summary-tick-above t]
1738           ["Clear above" gnus-summary-clear-above t])
1739          ["Current score" gnus-summary-current-score t]
1740          ["Set score" gnus-summary-set-score t]
1741          ["Switch current score file..." gnus-score-change-score-file t]
1742          ["Set mark below..." gnus-score-set-mark-below t]
1743          ["Set expunge below..." gnus-score-set-expunge-below t]
1744          ["Edit current score file" gnus-score-edit-current-scores t]
1745          ["Edit score file" gnus-score-edit-file t]
1746          ["Trace score" gnus-score-find-trace t]
1747          ["Find words" gnus-score-find-favourite-words t]
1748          ["Rescore buffer" gnus-summary-rescore t]
1749          ["Increase score..." gnus-summary-increase-score t]
1750          ["Lower score..." gnus-summary-lower-score t]))))
1751
1752     ;; Define both the Article menu in the summary buffer and the equivalent
1753     ;; Commands menu in the article buffer here for consistency.
1754     (let ((innards
1755            `(("Hide"
1756               ["All" gnus-article-hide t]
1757               ["Headers" gnus-article-toggle-headers t]
1758               ["Signature" gnus-article-hide-signature t]
1759               ["Citation" gnus-article-hide-citation t]
1760               ["List identifiers" gnus-article-hide-list-identifiers t]
1761               ["PGP" gnus-article-hide-pgp t]
1762               ["Banner" gnus-article-strip-banner t]
1763               ["Boring headers" gnus-article-hide-boring-headers t])
1764              ("Highlight"
1765               ["All" gnus-article-highlight t]
1766               ["Headers" gnus-article-highlight-headers t]
1767               ["Signature" gnus-article-highlight-signature t]
1768               ["Citation" gnus-article-highlight-citation t])
1769              ("Date"
1770               ["Local" gnus-article-date-local t]
1771               ["ISO8601" gnus-article-date-iso8601 t]
1772               ["UT" gnus-article-date-ut t]
1773               ["Original" gnus-article-date-original t]
1774               ["Lapsed" gnus-article-date-lapsed t]
1775               ["User-defined" gnus-article-date-user t])
1776              ("Washing"
1777               ("Remove Blanks"
1778                ["Leading" gnus-article-strip-leading-blank-lines t]
1779                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1780                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1781                ["All of the above" gnus-article-strip-blank-lines t]
1782                ["All" gnus-article-strip-all-blank-lines t]
1783                ["Leading space" gnus-article-strip-leading-space t]
1784                ["Trailing space" gnus-article-strip-trailing-space t])
1785               ["Overstrike" gnus-article-treat-overstrike t]
1786               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1787               ["Emphasis" gnus-article-emphasize t]
1788               ["Word wrap" gnus-article-fill-cited-article t]
1789               ["Fill long lines" gnus-article-fill-long-lines t]
1790               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1791               ["CR" gnus-article-remove-cr t]
1792               ["Show X-Face" gnus-article-display-x-face t]
1793               ["Rot 13" gnus-summary-caesar-message
1794                ,@(if (featurep 'xemacs) nil
1795                    '(:help "\"Caesar rotate\" article by 13"))]
1796               ["Unix pipe" gnus-summary-pipe-message t]
1797               ["Add buttons" gnus-article-add-buttons t]
1798               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1799               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1800               ["Toggle MIME" gnus-summary-toggle-mime t]
1801               ["Verbose header" gnus-summary-verbose-headers t]
1802               ["Toggle header" gnus-summary-toggle-header t]
1803               ["Toggle smileys" gnus-smiley-display t]
1804               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1805               ["HZ" gnus-article-decode-HZ t])
1806              ("Output"
1807               ["Save in default format" gnus-summary-save-article
1808                ,@(if (featurep 'xemacs) nil
1809                    '(:help "Save article using default method"))]
1810               ["Save in file" gnus-summary-save-article-file
1811                ,@(if (featurep 'xemacs) nil
1812                    '(:help "Save article in file"))]
1813               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1814               ["Save in MH folder" gnus-summary-save-article-folder t]
1815               ["Save in VM folder" gnus-summary-save-article-vm t]
1816               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1817               ["Save body in file" gnus-summary-save-article-body-file t]
1818               ["Pipe through a filter" gnus-summary-pipe-output t]
1819               ["Add to SOUP packet" gnus-soup-add-article t]
1820               ["Print" gnus-summary-print-article t])
1821              ("Backend"
1822               ["Respool article..." gnus-summary-respool-article t]
1823               ["Move article..." gnus-summary-move-article
1824                (gnus-check-backend-function
1825                 'request-move-article gnus-newsgroup-name)]
1826               ["Copy article..." gnus-summary-copy-article t]
1827               ["Crosspost article..." gnus-summary-crosspost-article
1828                (gnus-check-backend-function
1829                 'request-replace-article gnus-newsgroup-name)]
1830               ["Import file..." gnus-summary-import-article t]
1831               ["Check if posted" gnus-summary-article-posted-p t]
1832               ["Edit article" gnus-summary-edit-article
1833                (not (gnus-group-read-only-p))]
1834               ["Delete article" gnus-summary-delete-article
1835                (gnus-check-backend-function
1836                 'request-expire-articles gnus-newsgroup-name)]
1837               ["Query respool" gnus-summary-respool-query t]
1838               ["Trace respool" gnus-summary-respool-trace t]
1839               ["Delete expirable articles" gnus-summary-expire-articles-now
1840                (gnus-check-backend-function
1841                 'request-expire-articles gnus-newsgroup-name)])
1842              ("Extract"
1843               ["Uudecode" gnus-uu-decode-uu
1844                ,@(if (featurep 'xemacs) nil
1845                    '(:help "Decode uuencoded article(s)"))]
1846               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1847               ["Unshar" gnus-uu-decode-unshar t]
1848               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1849               ["Save" gnus-uu-decode-save t]
1850               ["Binhex" gnus-uu-decode-binhex t]
1851               ["Postscript" gnus-uu-decode-postscript t])
1852              ("Cache"
1853               ["Enter article" gnus-cache-enter-article t]
1854               ["Remove article" gnus-cache-remove-article t])
1855              ["Translate" gnus-article-babel t]
1856              ["Select article buffer" gnus-summary-select-article-buffer t]
1857              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1858              ["Isearch article..." gnus-summary-isearch-article t]
1859              ["Beginning of the article" gnus-summary-beginning-of-article t]
1860              ["End of the article" gnus-summary-end-of-article t]
1861              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1862              ["Fetch referenced articles" gnus-summary-refer-references t]
1863              ["Fetch current thread" gnus-summary-refer-thread t]
1864              ["Fetch article with id..." gnus-summary-refer-article t]
1865              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1866              ["Redisplay" gnus-summary-show-article t])))
1867       (easy-menu-define
1868        gnus-summary-article-menu gnus-summary-mode-map ""
1869        (cons "Article" innards))
1870
1871       (easy-menu-define
1872        gnus-article-commands-menu gnus-article-mode-map ""
1873        (cons "Commands" innards)))
1874
1875     (easy-menu-define
1876      gnus-summary-thread-menu gnus-summary-mode-map ""
1877      '("Threads"
1878        ["Toggle threading" gnus-summary-toggle-threads t]
1879        ["Hide threads" gnus-summary-hide-all-threads t]
1880        ["Show threads" gnus-summary-show-all-threads t]
1881        ["Hide thread" gnus-summary-hide-thread t]
1882        ["Show thread" gnus-summary-show-thread t]
1883        ["Go to next thread" gnus-summary-next-thread t]
1884        ["Go to previous thread" gnus-summary-prev-thread t]
1885        ["Go down thread" gnus-summary-down-thread t]
1886        ["Go up thread" gnus-summary-up-thread t]
1887        ["Top of thread" gnus-summary-top-thread t]
1888        ["Mark thread as read" gnus-summary-kill-thread t]
1889        ["Lower thread score" gnus-summary-lower-thread t]
1890        ["Raise thread score" gnus-summary-raise-thread t]
1891        ["Rethread current" gnus-summary-rethread-current t]))
1892
1893     (easy-menu-define
1894      gnus-summary-post-menu gnus-summary-mode-map ""
1895      `("Post"
1896        ["Post an article" gnus-summary-post-news
1897         ,@(if (featurep 'xemacs) nil
1898             '(:help "Post an article"))]
1899        ["Followup" gnus-summary-followup
1900         ,@(if (featurep 'xemacs) nil
1901             '(:help "Post followup to this article"))]
1902        ["Followup and yank" gnus-summary-followup-with-original
1903         ,@(if (featurep 'xemacs) nil
1904             '(:help "Post followup to this article, quoting its contents"))]
1905        ["Supersede article" gnus-summary-supersede-article t]
1906        ["Cancel article" gnus-summary-cancel-article
1907         ,@(if (featurep 'xemacs) nil
1908             '(:help "Cancel an article you posted"))]
1909        ["Reply" gnus-summary-reply t]
1910        ["Reply and yank" gnus-summary-reply-with-original t]
1911        ["Wide reply" gnus-summary-wide-reply t]
1912        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1913         ,@(if (featurep 'xemacs) nil
1914             '(:help "Mail a reply, quoting this article"))]
1915        ["Mail forward" gnus-summary-mail-forward t]
1916        ["Post forward" gnus-summary-post-forward t]
1917        ["Digest and mail" gnus-uu-digest-mail-forward t]
1918        ["Digest and post" gnus-uu-digest-post-forward t]
1919        ["Resend message" gnus-summary-resend-message t]
1920        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1921        ["Send a mail" gnus-summary-mail-other-window t]
1922        ["Uuencode and post" gnus-uu-post-news
1923         ,@(if (featurep 'xemacs) nil
1924             '(:help "Post a uuencoded article"))]
1925        ["Followup via news" gnus-summary-followup-to-mail t]
1926        ["Followup via news and yank"
1927         gnus-summary-followup-to-mail-with-original t]
1928        ;;("Draft"
1929        ;;["Send" gnus-summary-send-draft t]
1930        ;;["Send bounced" gnus-resend-bounced-mail t])
1931        ))
1932
1933     (easy-menu-define
1934      gnus-summary-misc-menu gnus-summary-mode-map ""
1935      `("Misc"
1936        ("Mark Read"
1937         ["Mark as read" gnus-summary-mark-as-read-forward t]
1938         ["Mark same subject and select"
1939          gnus-summary-kill-same-subject-and-select t]
1940         ["Mark same subject" gnus-summary-kill-same-subject t]
1941         ["Catchup" gnus-summary-catchup
1942          ,@(if (featurep 'xemacs) nil
1943              '(:help "Mark unread articles in this group as read"))]
1944         ["Catchup all" gnus-summary-catchup-all t]
1945         ["Catchup to here" gnus-summary-catchup-to-here t]
1946         ["Catchup region" gnus-summary-mark-region-as-read t]
1947         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1948        ("Mark Various"
1949         ["Tick" gnus-summary-tick-article-forward t]
1950         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1951         ["Remove marks" gnus-summary-clear-mark-forward t]
1952         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1953         ["Set bookmark" gnus-summary-set-bookmark t]
1954         ["Remove bookmark" gnus-summary-remove-bookmark t])
1955        ("Mark Limit"
1956         ["Marks..." gnus-summary-limit-to-marks t]
1957         ["Subject..." gnus-summary-limit-to-subject t]
1958         ["Author..." gnus-summary-limit-to-author t]
1959         ["Age..." gnus-summary-limit-to-age t]
1960         ["Extra..." gnus-summary-limit-to-extra t]
1961         ["Score" gnus-summary-limit-to-score t]
1962         ["Unread" gnus-summary-limit-to-unread t]
1963         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1964         ["Articles" gnus-summary-limit-to-articles t]
1965         ["Pop limit" gnus-summary-pop-limit t]
1966         ["Show dormant" gnus-summary-limit-include-dormant t]
1967         ["Hide childless dormant"
1968          gnus-summary-limit-exclude-childless-dormant t]
1969         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1970         ["Hide marked" gnus-summary-limit-exclude-marks t]
1971         ["Show expunged" gnus-summary-show-all-expunged t])
1972        ("Process Mark"
1973         ["Set mark" gnus-summary-mark-as-processable t]
1974         ["Remove mark" gnus-summary-unmark-as-processable t]
1975         ["Remove all marks" gnus-summary-unmark-all-processable t]
1976         ["Mark above" gnus-uu-mark-over t]
1977         ["Mark series" gnus-uu-mark-series t]
1978         ["Mark region" gnus-uu-mark-region t]
1979         ["Unmark region" gnus-uu-unmark-region t]
1980         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1981         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1982         ["Mark all" gnus-uu-mark-all t]
1983         ["Mark buffer" gnus-uu-mark-buffer t]
1984         ["Mark sparse" gnus-uu-mark-sparse t]
1985         ["Mark thread" gnus-uu-mark-thread t]
1986         ["Unmark thread" gnus-uu-unmark-thread t]
1987         ("Process Mark Sets"
1988          ["Kill" gnus-summary-kill-process-mark t]
1989          ["Yank" gnus-summary-yank-process-mark
1990           gnus-newsgroup-process-stack]
1991          ["Save" gnus-summary-save-process-mark t]))
1992        ("Scroll article"
1993         ["Page forward" gnus-summary-next-page
1994          ,@(if (featurep 'xemacs) nil
1995              '(:help "Show next page of article"))]
1996         ["Page backward" gnus-summary-prev-page
1997          ,@(if (featurep 'xemacs) nil
1998              '(:help "Show previous page of article"))]
1999         ["Line forward" gnus-summary-scroll-up t])
2000        ("Move"
2001         ["Next unread article" gnus-summary-next-unread-article t]
2002         ["Previous unread article" gnus-summary-prev-unread-article t]
2003         ["Next article" gnus-summary-next-article t]
2004         ["Previous article" gnus-summary-prev-article t]
2005         ["Next unread subject" gnus-summary-next-unread-subject t]
2006         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2007         ["Next article same subject" gnus-summary-next-same-subject t]
2008         ["Previous article same subject" gnus-summary-prev-same-subject t]
2009         ["First unread article" gnus-summary-first-unread-article t]
2010         ["Best unread article" gnus-summary-best-unread-article t]
2011         ["Go to subject number..." gnus-summary-goto-subject t]
2012         ["Go to article number..." gnus-summary-goto-article t]
2013         ["Go to the last article" gnus-summary-goto-last-article t]
2014         ["Pop article off history" gnus-summary-pop-article t])
2015        ("Sort"
2016         ["Sort by number" gnus-summary-sort-by-number t]
2017         ["Sort by author" gnus-summary-sort-by-author t]
2018         ["Sort by subject" gnus-summary-sort-by-subject t]
2019         ["Sort by date" gnus-summary-sort-by-date t]
2020         ["Sort by score" gnus-summary-sort-by-score t]
2021         ["Sort by lines" gnus-summary-sort-by-lines t]
2022         ["Sort by characters" gnus-summary-sort-by-chars t])
2023        ("Help"
2024         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2025         ["Describe group" gnus-summary-describe-group t]
2026         ["Read manual" gnus-info-find-node t])
2027        ("Modes"
2028         ["Pick and read" gnus-pick-mode t]
2029         ["Binary" gnus-binary-mode t])
2030        ("Regeneration"
2031         ["Regenerate" gnus-summary-prepare t]
2032         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2033         ["Toggle threading" gnus-summary-toggle-threads t])
2034        ["Filter articles..." gnus-summary-execute-command t]
2035        ["Run command on subjects..." gnus-summary-universal-argument t]
2036        ["Search articles forward..." gnus-summary-search-article-forward t]
2037        ["Search articles backward..." gnus-summary-search-article-backward t]
2038        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2039        ["Expand window" gnus-summary-expand-window t]
2040        ["Expire expirable articles" gnus-summary-expire-articles
2041         (gnus-check-backend-function
2042          'request-expire-articles gnus-newsgroup-name)]
2043        ["Edit local kill file" gnus-summary-edit-local-kill t]
2044        ["Edit main kill file" gnus-summary-edit-global-kill t]
2045        ["Edit group parameters" gnus-summary-edit-parameters t]
2046        ["Customize group parameters" gnus-summary-customize-parameters t]
2047        ["Send a bug report" gnus-bug t]
2048        ("Exit"
2049         ["Catchup and exit" gnus-summary-catchup-and-exit
2050          ,@(if (featurep 'xemacs) nil
2051              '(:help "Mark unread articles in this group as read, then exit"))]
2052         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2053         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2054         ["Exit group" gnus-summary-exit
2055          ,@(if (featurep 'xemacs) nil
2056              '(:help "Exit current group, return to group selection mode"))]
2057         ["Exit group without updating" gnus-summary-exit-no-update t]
2058         ["Exit and goto next group" gnus-summary-next-group t]
2059         ["Exit and goto prev group" gnus-summary-prev-group t]
2060         ["Reselect group" gnus-summary-reselect-current-group t]
2061         ["Rescan group" gnus-summary-rescan-group t]
2062         ["Update dribble" gnus-summary-save-newsrc t])))
2063
2064     (gnus-run-hooks 'gnus-summary-menu-hook)))
2065
2066 (defvar gnus-summary-tool-bar-map nil)
2067
2068 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2069 (defun gnus-summary-make-tool-bar ()
2070   (if (and (fboundp 'tool-bar-add-item-from-menu)
2071            (default-value 'tool-bar-mode)
2072            (not gnus-summary-tool-bar-map))
2073       (setq gnus-summary-tool-bar-map
2074             (let ((tool-bar-map (make-sparse-keymap))
2075                   (load-path (mm-image-load-path)))
2076               (tool-bar-add-item-from-menu
2077                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2078               (tool-bar-add-item-from-menu
2079                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2080               (tool-bar-add-item-from-menu
2081                'gnus-summary-post-news "post" gnus-summary-mode-map)
2082               (tool-bar-add-item-from-menu
2083                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2084               (tool-bar-add-item-from-menu
2085                'gnus-summary-followup "followup" gnus-summary-mode-map)
2086               (tool-bar-add-item-from-menu
2087                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2088               (tool-bar-add-item-from-menu
2089                'gnus-summary-reply "reply" gnus-summary-mode-map)
2090               (tool-bar-add-item-from-menu
2091                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2092               (tool-bar-add-item-from-menu
2093                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2094               (tool-bar-add-item-from-menu
2095                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2096               (tool-bar-add-item-from-menu
2097                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2098               (tool-bar-add-item-from-menu
2099                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2100               (tool-bar-add-item-from-menu
2101                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2102               (tool-bar-add-item-from-menu
2103                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2104               (tool-bar-add-item-from-menu
2105                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2106               tool-bar-map)))
2107   (if gnus-summary-tool-bar-map
2108       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2109
2110 (defun gnus-score-set-default (var value)
2111   "A version of set that updates the GNU Emacs menu-bar."
2112   (set var value)
2113   ;; It is the message that forces the active status to be updated.
2114   (message ""))
2115
2116 (defun gnus-make-score-map (type)
2117   "Make a summary score map of type TYPE."
2118   (if t
2119       nil
2120     (let ((headers '(("author" "from" string)
2121                      ("subject" "subject" string)
2122                      ("article body" "body" string)
2123                      ("article head" "head" string)
2124                      ("xref" "xref" string)
2125                      ("extra header" "extra" string)
2126                      ("lines" "lines" number)
2127                      ("followups to author" "followup" string)))
2128           (types '((number ("less than" <)
2129                            ("greater than" >)
2130                            ("equal" =))
2131                    (string ("substring" s)
2132                            ("exact string" e)
2133                            ("fuzzy string" f)
2134                            ("regexp" r))))
2135           (perms '(("temporary" (current-time-string))
2136                    ("permanent" nil)
2137                    ("immediate" now)))
2138           header)
2139       (list
2140        (apply
2141         'nconc
2142         (list
2143          (if (eq type 'lower)
2144              "Lower score"
2145            "Increase score"))
2146         (let (outh)
2147           (while headers
2148             (setq header (car headers))
2149             (setq outh
2150                   (cons
2151                    (apply
2152                     'nconc
2153                     (list (car header))
2154                     (let ((ts (cdr (assoc (nth 2 header) types)))
2155                           outt)
2156                       (while ts
2157                         (setq outt
2158                               (cons
2159                                (apply
2160                                 'nconc
2161                                 (list (caar ts))
2162                                 (let ((ps perms)
2163                                       outp)
2164                                   (while ps
2165                                     (setq outp
2166                                           (cons
2167                                            (vector
2168                                             (caar ps)
2169                                             (list
2170                                              'gnus-summary-score-entry
2171                                              (nth 1 header)
2172                                              (if (or (string= (nth 1 header)
2173                                                               "head")
2174                                                      (string= (nth 1 header)
2175                                                               "body"))
2176                                                  ""
2177                                                (list 'gnus-summary-header
2178                                                      (nth 1 header)))
2179                                              (list 'quote (nth 1 (car ts)))
2180                                              (list 'gnus-score-delta-default
2181                                                    nil)
2182                                              (nth 1 (car ps))
2183                                              t)
2184                                             t)
2185                                            outp))
2186                                     (setq ps (cdr ps)))
2187                                   (list (nreverse outp))))
2188                                outt))
2189                         (setq ts (cdr ts)))
2190                       (list (nreverse outt))))
2191                    outh))
2192             (setq headers (cdr headers)))
2193           (list (nreverse outh))))))))
2194
2195 \f
2196
2197 (defun gnus-summary-mode (&optional group)
2198   "Major mode for reading articles.
2199
2200 All normal editing commands are switched off.
2201 \\<gnus-summary-mode-map>
2202 Each line in this buffer represents one article.  To read an
2203 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2204 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2205 respectively.
2206
2207 You can also post articles and send mail from this buffer.  To
2208 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2209 of an article, type `\\[gnus-summary-reply]'.
2210
2211 There are approx. one gazillion commands you can execute in this
2212 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2213
2214 The following commands are available:
2215
2216 \\{gnus-summary-mode-map}"
2217   (interactive)
2218   (kill-all-local-variables)
2219   (when (gnus-visual-p 'summary-menu 'menu)
2220     (gnus-summary-make-menu-bar)
2221     (gnus-summary-make-tool-bar))
2222   (gnus-summary-make-local-variables)
2223   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2224     (gnus-summary-make-local-variables))
2225   (gnus-make-thread-indent-array)
2226   (gnus-simplify-mode-line)
2227   (setq major-mode 'gnus-summary-mode)
2228   (setq mode-name "Summary")
2229   (make-local-variable 'minor-mode-alist)
2230   (use-local-map gnus-summary-mode-map)
2231   (buffer-disable-undo)
2232   (setq buffer-read-only t)             ;Disable modification
2233   (setq truncate-lines t)
2234   (setq selective-display t)
2235   (setq selective-display-ellipses t)   ;Display `...'
2236   (gnus-summary-set-display-table)
2237   (gnus-set-default-directory)
2238   (setq gnus-newsgroup-name group)
2239   (unless (gnus-news-group-p group)
2240     (setq gnus-newsgroup-incorporated
2241           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2242   (make-local-variable 'gnus-summary-line-format)
2243   (make-local-variable 'gnus-summary-line-format-spec)
2244   (make-local-variable 'gnus-summary-dummy-line-format)
2245   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2246   (make-local-variable 'gnus-summary-mark-positions)
2247   (make-local-hook 'pre-command-hook)
2248   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2249   (gnus-run-hooks 'gnus-summary-mode-hook)
2250   (turn-on-gnus-mailing-list-mode)
2251   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2252   (gnus-update-summary-mark-positions))
2253
2254 (defun gnus-summary-make-local-variables ()
2255   "Make all the local summary buffer variables."
2256   (let (global)
2257     (dolist (local gnus-summary-local-variables)
2258       (if (consp local)
2259           (progn
2260             (if (eq (cdr local) 'global)
2261                 ;; Copy the global value of the variable.
2262                 (setq global (symbol-value (car local)))
2263               ;; Use the value from the list.
2264               (setq global (eval (cdr local))))
2265             (set (make-local-variable (car local)) global))
2266         ;; Simple nil-valued local variable.
2267         (set (make-local-variable local) nil)))))
2268
2269 (defun gnus-summary-clear-local-variables ()
2270   (let ((locals gnus-summary-local-variables))
2271     (while locals
2272       (if (consp (car locals))
2273           (and (vectorp (caar locals))
2274                (set (caar locals) nil))
2275         (and (vectorp (car locals))
2276              (set (car locals) nil)))
2277       (setq locals (cdr locals)))))
2278
2279 ;; Summary data functions.
2280
2281 (defmacro gnus-data-number (data)
2282   `(car ,data))
2283
2284 (defmacro gnus-data-set-number (data number)
2285   `(setcar ,data ,number))
2286
2287 (defmacro gnus-data-mark (data)
2288   `(nth 1 ,data))
2289
2290 (defmacro gnus-data-set-mark (data mark)
2291   `(setcar (nthcdr 1 ,data) ,mark))
2292
2293 (defmacro gnus-data-pos (data)
2294   `(nth 2 ,data))
2295
2296 (defmacro gnus-data-set-pos (data pos)
2297   `(setcar (nthcdr 2 ,data) ,pos))
2298
2299 (defmacro gnus-data-header (data)
2300   `(nth 3 ,data))
2301
2302 (defmacro gnus-data-set-header (data header)
2303   `(setcar (nthcdr 3 ,data) ,header))
2304
2305 (defmacro gnus-data-level (data)
2306   `(nth 4 ,data))
2307
2308 (defmacro gnus-data-unread-p (data)
2309   `(= (nth 1 ,data) gnus-unread-mark))
2310
2311 (defmacro gnus-data-read-p (data)
2312   `(/= (nth 1 ,data) gnus-unread-mark))
2313
2314 (defmacro gnus-data-pseudo-p (data)
2315   `(consp (nth 3 ,data)))
2316
2317 (defmacro gnus-data-find (number)
2318   `(assq ,number gnus-newsgroup-data))
2319
2320 (defmacro gnus-data-find-list (number &optional data)
2321   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2322      (memq (assq ,number bdata)
2323            bdata)))
2324
2325 (defmacro gnus-data-make (number mark pos header level)
2326   `(list ,number ,mark ,pos ,header ,level))
2327
2328 (defun gnus-data-enter (after-article number mark pos header level offset)
2329   (let ((data (gnus-data-find-list after-article)))
2330     (unless data
2331       (error "No such article: %d" after-article))
2332     (setcdr data (cons (gnus-data-make number mark pos header level)
2333                        (cdr data)))
2334     (setq gnus-newsgroup-data-reverse nil)
2335     (gnus-data-update-list (cddr data) offset)))
2336
2337 (defun gnus-data-enter-list (after-article list &optional offset)
2338   (when list
2339     (let ((data (and after-article (gnus-data-find-list after-article)))
2340           (ilist list))
2341       (if (not (or data
2342                    after-article))
2343           (let ((odata gnus-newsgroup-data))
2344             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2345             (when offset
2346               (gnus-data-update-list odata offset)))
2347         ;; Find the last element in the list to be spliced into the main
2348         ;; list.
2349         (while (cdr list)
2350           (setq list (cdr list)))
2351         (if (not data)
2352             (progn
2353               (setcdr list gnus-newsgroup-data)
2354               (setq gnus-newsgroup-data ilist)
2355               (when offset
2356                 (gnus-data-update-list (cdr list) offset)))
2357           (setcdr list (cdr data))
2358           (setcdr data ilist)
2359           (when offset
2360             (gnus-data-update-list (cdr list) offset))))
2361       (setq gnus-newsgroup-data-reverse nil))))
2362
2363 (defun gnus-data-remove (article &optional offset)
2364   (let ((data gnus-newsgroup-data))
2365     (if (= (gnus-data-number (car data)) article)
2366         (progn
2367           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2368                 gnus-newsgroup-data-reverse nil)
2369           (when offset
2370             (gnus-data-update-list gnus-newsgroup-data offset)))
2371       (while (cdr data)
2372         (when (= (gnus-data-number (cadr data)) article)
2373           (setcdr data (cddr data))
2374           (when offset
2375             (gnus-data-update-list (cdr data) offset))
2376           (setq data nil
2377                 gnus-newsgroup-data-reverse nil))
2378         (setq data (cdr data))))))
2379
2380 (defmacro gnus-data-list (backward)
2381   `(if ,backward
2382        (or gnus-newsgroup-data-reverse
2383            (setq gnus-newsgroup-data-reverse
2384                  (reverse gnus-newsgroup-data)))
2385      gnus-newsgroup-data))
2386
2387 (defun gnus-data-update-list (data offset)
2388   "Add OFFSET to the POS of all data entries in DATA."
2389   (setq gnus-newsgroup-data-reverse nil)
2390   (while data
2391     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2392     (setq data (cdr data))))
2393
2394 (defun gnus-summary-article-pseudo-p (article)
2395   "Say whether this article is a pseudo article or not."
2396   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2397
2398 (defmacro gnus-summary-article-sparse-p (article)
2399   "Say whether this article is a sparse article or not."
2400   `(memq ,article gnus-newsgroup-sparse))
2401
2402 (defmacro gnus-summary-article-ancient-p (article)
2403   "Say whether this article is a sparse article or not."
2404   `(memq ,article gnus-newsgroup-ancient))
2405
2406 (defun gnus-article-parent-p (number)
2407   "Say whether this article is a parent or not."
2408   (let ((data (gnus-data-find-list number)))
2409     (and (cdr data)                     ; There has to be an article after...
2410          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2411             (gnus-data-level (nth 1 data))))))
2412
2413 (defun gnus-article-children (number)
2414   "Return a list of all children to NUMBER."
2415   (let* ((data (gnus-data-find-list number))
2416          (level (gnus-data-level (car data)))
2417          children)
2418     (setq data (cdr data))
2419     (while (and data
2420                 (= (gnus-data-level (car data)) (1+ level)))
2421       (push (gnus-data-number (car data)) children)
2422       (setq data (cdr data)))
2423     children))
2424
2425 (defmacro gnus-summary-skip-intangible ()
2426   "If the current article is intangible, then jump to a different article."
2427   '(let ((to (get-text-property (point) 'gnus-intangible)))
2428      (and to (gnus-summary-goto-subject to))))
2429
2430 (defmacro gnus-summary-article-intangible-p ()
2431   "Say whether this article is intangible or not."
2432   '(get-text-property (point) 'gnus-intangible))
2433
2434 (defun gnus-article-read-p (article)
2435   "Say whether ARTICLE is read or not."
2436   (not (or (memq article gnus-newsgroup-marked)
2437            (memq article gnus-newsgroup-unreads)
2438            (memq article gnus-newsgroup-unselected)
2439            (memq article gnus-newsgroup-dormant))))
2440
2441 ;; Some summary mode macros.
2442
2443 (defmacro gnus-summary-article-number ()
2444   "The article number of the article on the current line.
2445 If there isn's an article number here, then we return the current
2446 article number."
2447   '(progn
2448      (gnus-summary-skip-intangible)
2449      (or (get-text-property (point) 'gnus-number)
2450          (gnus-summary-last-subject))))
2451
2452 (defmacro gnus-summary-article-header (&optional number)
2453   "Return the header of article NUMBER."
2454   `(gnus-data-header (gnus-data-find
2455                       ,(or number '(gnus-summary-article-number)))))
2456
2457 (defmacro gnus-summary-thread-level (&optional number)
2458   "Return the level of thread that starts with article NUMBER."
2459   `(if (and (eq gnus-summary-make-false-root 'dummy)
2460             (get-text-property (point) 'gnus-intangible))
2461        0
2462      (gnus-data-level (gnus-data-find
2463                        ,(or number '(gnus-summary-article-number))))))
2464
2465 (defmacro gnus-summary-article-mark (&optional number)
2466   "Return the mark of article NUMBER."
2467   `(gnus-data-mark (gnus-data-find
2468                     ,(or number '(gnus-summary-article-number)))))
2469
2470 (defmacro gnus-summary-article-pos (&optional number)
2471   "Return the position of the line of article NUMBER."
2472   `(gnus-data-pos (gnus-data-find
2473                    ,(or number '(gnus-summary-article-number)))))
2474
2475 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2476 (defmacro gnus-summary-article-subject (&optional number)
2477   "Return current subject string or nil if nothing."
2478   `(let ((headers
2479           ,(if number
2480                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2481              '(gnus-data-header (assq (gnus-summary-article-number)
2482                                       gnus-newsgroup-data)))))
2483      (and headers
2484           (vectorp headers)
2485           (mail-header-subject headers))))
2486
2487 (defmacro gnus-summary-article-score (&optional number)
2488   "Return current article score."
2489   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2490                   gnus-newsgroup-scored))
2491        gnus-summary-default-score 0))
2492
2493 (defun gnus-summary-article-children (&optional number)
2494   "Return a list of article numbers that are children of article NUMBER."
2495   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2496          (level (gnus-data-level (car data)))
2497          l children)
2498     (while (and (setq data (cdr data))
2499                 (> (setq l (gnus-data-level (car data))) level))
2500       (and (= (1+ level) l)
2501            (push (gnus-data-number (car data))
2502                  children)))
2503     (nreverse children)))
2504
2505 (defun gnus-summary-article-parent (&optional number)
2506   "Return the article number of the parent of article NUMBER."
2507   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2508                                     (gnus-data-list t)))
2509          (level (gnus-data-level (car data))))
2510     (if (zerop level)
2511         ()                              ; This is a root.
2512       ;; We search until we find an article with a level less than
2513       ;; this one.  That function has to be the parent.
2514       (while (and (setq data (cdr data))
2515                   (not (< (gnus-data-level (car data)) level))))
2516       (and data (gnus-data-number (car data))))))
2517
2518 (defun gnus-unread-mark-p (mark)
2519   "Say whether MARK is the unread mark."
2520   (= mark gnus-unread-mark))
2521
2522 (defun gnus-read-mark-p (mark)
2523   "Say whether MARK is one of the marks that mark as read.
2524 This is all marks except unread, ticked, dormant, and expirable."
2525   (not (or (= mark gnus-unread-mark)
2526            (= mark gnus-ticked-mark)
2527            (= mark gnus-dormant-mark)
2528            (= mark gnus-expirable-mark))))
2529
2530 (defmacro gnus-article-mark (number)
2531   "Return the MARK of article NUMBER.
2532 This macro should only be used when computing the mark the \"first\"
2533 time; i.e., when generating the summary lines.  After that,
2534 `gnus-summary-article-mark' should be used to examine the
2535 marks of articles."
2536   `(cond
2537     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2538     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2539     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2540     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2541     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2542     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2543     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2544     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2545            gnus-ancient-mark))))
2546
2547 ;; Saving hidden threads.
2548
2549 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2550 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2551
2552 (defmacro gnus-save-hidden-threads (&rest forms)
2553   "Save hidden threads, eval FORMS, and restore the hidden threads."
2554   (let ((config (make-symbol "config")))
2555     `(let ((,config (gnus-hidden-threads-configuration)))
2556        (unwind-protect
2557            (save-excursion
2558              ,@forms)
2559          (gnus-restore-hidden-threads-configuration ,config)))))
2560
2561 (defun gnus-data-compute-positions ()
2562   "Compute the positions of all articles."
2563   (setq gnus-newsgroup-data-reverse nil)
2564   (let ((data gnus-newsgroup-data))
2565     (save-excursion
2566       (gnus-save-hidden-threads
2567         (gnus-summary-show-all-threads)
2568         (goto-char (point-min))
2569         (while data
2570           (while (get-text-property (point) 'gnus-intangible)
2571             (forward-line 1))
2572           (gnus-data-set-pos (car data) (+ (point) 3))
2573           (setq data (cdr data))
2574           (forward-line 1))))))
2575
2576 (defun gnus-hidden-threads-configuration ()
2577   "Return the current hidden threads configuration."
2578   (save-excursion
2579     (let (config)
2580       (goto-char (point-min))
2581       (while (search-forward "\r" nil t)
2582         (push (1- (point)) config))
2583       config)))
2584
2585 (defun gnus-restore-hidden-threads-configuration (config)
2586   "Restore hidden threads configuration from CONFIG."
2587   (save-excursion
2588     (let (point buffer-read-only)
2589       (while (setq point (pop config))
2590         (when (and (< point (point-max))
2591                    (goto-char point)
2592                    (eq (char-after) ?\n))
2593           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2594
2595 ;; Various summary mode internalish functions.
2596
2597 (defun gnus-mouse-pick-article (e)
2598   (interactive "e")
2599   (mouse-set-point e)
2600   (gnus-summary-next-page nil t))
2601
2602 (defun gnus-summary-set-display-table ()
2603   "Change the display table.
2604 Odd characters have a tendency to mess
2605 up nicely formatted displays - we make all possible glyphs
2606 display only a single character."
2607
2608   ;; We start from the standard display table, if any.
2609   (let ((table (or (copy-sequence standard-display-table)
2610                    (make-display-table)))
2611         (i 32))
2612     ;; Nix out all the control chars...
2613     (while (>= (setq i (1- i)) 0)
2614       (aset table i [??]))
2615     ;; ... but not newline and cr, of course.  (cr is necessary for the
2616     ;; selective display).
2617     (aset table ?\n nil)
2618     (aset table ?\r nil)
2619     ;; We keep TAB as well.
2620     (aset table ?\t nil)
2621     ;; We nix out any glyphs over 126 that are not set already.
2622     (let ((i 256))
2623       (while (>= (setq i (1- i)) 127)
2624         ;; Only modify if the entry is nil.
2625         (unless (aref table i)
2626           (aset table i [??]))))
2627     (setq buffer-display-table table)))
2628
2629 (defun gnus-summary-buffer-name (group)
2630   "Return the summary buffer name of GROUP."
2631   (concat "*Summary " group "*"))
2632
2633 (defun gnus-summary-setup-buffer (group)
2634   "Initialize summary buffer."
2635   (let ((buffer (gnus-summary-buffer-name group)))
2636     (if (get-buffer buffer)
2637         (progn
2638           (set-buffer buffer)
2639           (setq gnus-summary-buffer (current-buffer))
2640           (not gnus-newsgroup-prepared))
2641       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2642       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2643       (gnus-summary-mode group)
2644       (when gnus-carpal
2645         (gnus-carpal-setup-buffer 'summary))
2646       (unless gnus-single-article-buffer
2647         (make-local-variable 'gnus-article-buffer)
2648         (make-local-variable 'gnus-article-current)
2649         (make-local-variable 'gnus-original-article-buffer))
2650       (setq gnus-newsgroup-name group)
2651       t)))
2652
2653 (defun gnus-set-global-variables ()
2654   "Set the global equivalents of the buffer-local variables.
2655 They are set to the latest values they had.  These reflect the summary
2656 buffer that was in action when the last article was fetched."
2657   (when (eq major-mode 'gnus-summary-mode)
2658     (setq gnus-summary-buffer (current-buffer))
2659     (let ((name gnus-newsgroup-name)
2660           (marked gnus-newsgroup-marked)
2661           (unread gnus-newsgroup-unreads)
2662           (headers gnus-current-headers)
2663           (data gnus-newsgroup-data)
2664           (summary gnus-summary-buffer)
2665           (article-buffer gnus-article-buffer)
2666           (original gnus-original-article-buffer)
2667           (gac gnus-article-current)
2668           (reffed gnus-reffed-article-number)
2669           (score-file gnus-current-score-file)
2670           (default-charset gnus-newsgroup-charset)
2671           vlist)
2672       (let ((locals gnus-newsgroup-variables))
2673         (while locals
2674           (if (consp (car locals))
2675               (push (eval (caar locals)) vlist)
2676             (push (eval (car locals)) vlist))
2677           (setq locals (cdr locals)))
2678         (setq vlist (nreverse vlist)))
2679       (save-excursion
2680         (set-buffer gnus-group-buffer)
2681         (setq gnus-newsgroup-name name
2682               gnus-newsgroup-marked marked
2683               gnus-newsgroup-unreads unread
2684               gnus-current-headers headers
2685               gnus-newsgroup-data data
2686               gnus-article-current gac
2687               gnus-summary-buffer summary
2688               gnus-article-buffer article-buffer
2689               gnus-original-article-buffer original
2690               gnus-reffed-article-number reffed
2691               gnus-current-score-file score-file
2692               gnus-newsgroup-charset default-charset)
2693         (let ((locals gnus-newsgroup-variables))
2694           (while locals
2695             (if (consp (car locals))
2696                 (set (caar locals) (pop vlist))
2697               (set (car locals) (pop vlist)))
2698             (setq locals (cdr locals))))
2699         ;; The article buffer also has local variables.
2700         (when (gnus-buffer-live-p gnus-article-buffer)
2701           (set-buffer gnus-article-buffer)
2702           (setq gnus-summary-buffer summary))))))
2703
2704 (defun gnus-summary-article-unread-p (article)
2705   "Say whether ARTICLE is unread or not."
2706   (memq article gnus-newsgroup-unreads))
2707
2708 (defun gnus-summary-first-article-p (&optional article)
2709   "Return whether ARTICLE is the first article in the buffer."
2710   (if (not (setq article (or article (gnus-summary-article-number))))
2711       nil
2712     (eq article (caar gnus-newsgroup-data))))
2713
2714 (defun gnus-summary-last-article-p (&optional article)
2715   "Return whether ARTICLE is the last article in the buffer."
2716   (if (not (setq article (or article (gnus-summary-article-number))))
2717       ;; All non-existent numbers are the last article.  :-)
2718       t
2719     (not (cdr (gnus-data-find-list article)))))
2720
2721 (defun gnus-make-thread-indent-array ()
2722   (let ((n 200))
2723     (unless (and gnus-thread-indent-array
2724                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2725       (setq gnus-thread-indent-array (make-vector 201 "")
2726             gnus-thread-indent-array-level gnus-thread-indent-level)
2727       (while (>= n 0)
2728         (aset gnus-thread-indent-array n
2729               (make-string (* n gnus-thread-indent-level) ? ))
2730         (setq n (1- n))))))
2731
2732 (defun gnus-update-summary-mark-positions ()
2733   "Compute where the summary marks are to go."
2734   (save-excursion
2735     (when (gnus-buffer-exists-p gnus-summary-buffer)
2736       (set-buffer gnus-summary-buffer))
2737     (let ((gnus-replied-mark 129)
2738           (gnus-score-below-mark 130)
2739           (gnus-score-over-mark 130)
2740           (gnus-download-mark 131)
2741           (spec gnus-summary-line-format-spec)
2742           gnus-visual pos)
2743       (save-excursion
2744         (gnus-set-work-buffer)
2745         (let ((gnus-summary-line-format-spec spec)
2746               (gnus-newsgroup-downloadable '((0 . t))))
2747           (gnus-summary-insert-line
2748            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2749            0 nil 128 t nil "" nil 1)
2750           (goto-char (point-min))
2751           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2752                                              (- (point) 2)))))
2753           (goto-char (point-min))
2754           (push (cons 'replied (and (search-forward "\201" nil t)
2755                                     (- (point) 2)))
2756                 pos)
2757           (goto-char (point-min))
2758           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2759                 pos)
2760           (goto-char (point-min))
2761           (push (cons 'download
2762                       (and (search-forward "\203" nil t) (- (point) 2)))
2763                 pos)))
2764       (setq gnus-summary-mark-positions pos))))
2765
2766 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2767   "Insert a dummy root in the summary buffer."
2768   (beginning-of-line)
2769   (gnus-add-text-properties
2770    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2771    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2772
2773 (defun gnus-summary-from-or-to-or-newsgroups (header)
2774   (let ((to (cdr (assq 'To (mail-header-extra header))))
2775         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2776         (default-mime-charset (with-current-buffer gnus-summary-buffer
2777                                 default-mime-charset)))
2778     (cond
2779      ((and to
2780            gnus-ignored-from-addresses
2781            (string-match gnus-ignored-from-addresses
2782                          (mail-header-from header)))
2783       (concat "-> "
2784               (or (car (funcall gnus-extract-address-components
2785                                 (funcall
2786                                  gnus-decode-encoded-word-function to)))
2787                   (funcall gnus-decode-encoded-word-function to))))
2788      ((and newsgroups
2789            gnus-ignored-from-addresses
2790            (string-match gnus-ignored-from-addresses
2791                          (mail-header-from header)))
2792       (concat "=> " newsgroups))
2793      (t
2794       (or (car (funcall gnus-extract-address-components
2795                         (mail-header-from header)))
2796           (mail-header-from header))))))
2797
2798 (defun gnus-summary-insert-line (gnus-tmp-header
2799                                  gnus-tmp-level gnus-tmp-current
2800                                  gnus-tmp-unread gnus-tmp-replied
2801                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2802                                  &optional gnus-tmp-dummy gnus-tmp-score
2803                                  gnus-tmp-process)
2804   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2805          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2806          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2807          (gnus-tmp-score-char
2808           (if (or (null gnus-summary-default-score)
2809                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2810                       gnus-summary-zcore-fuzz))
2811               ?  ;Whitespace
2812             (if (< gnus-tmp-score gnus-summary-default-score)
2813                 gnus-score-below-mark gnus-score-over-mark)))
2814          (gnus-tmp-replied
2815           (cond (gnus-tmp-process gnus-process-mark)
2816                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2817                  gnus-cached-mark)
2818                 (gnus-tmp-replied gnus-replied-mark)
2819                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2820                  gnus-saved-mark)
2821                 (t gnus-no-mark)))
2822          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2823          (gnus-tmp-name
2824           (cond
2825            ((string-match "<[^>]+> *$" gnus-tmp-from)
2826             (let ((beg (match-beginning 0)))
2827               (or (and (string-match "^\".+\"" gnus-tmp-from)
2828                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2829                   (substring gnus-tmp-from 0 beg))))
2830            ((string-match "(.+)" gnus-tmp-from)
2831             (substring gnus-tmp-from
2832                        (1+ (match-beginning 0)) (1- (match-end 0))))
2833            (t gnus-tmp-from)))
2834          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2835          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2836          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2837          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2838          (buffer-read-only nil))
2839     (when (string= gnus-tmp-name "")
2840       (setq gnus-tmp-name gnus-tmp-from))
2841     (unless (numberp gnus-tmp-lines)
2842       (setq gnus-tmp-lines 0))
2843     (gnus-put-text-property-excluding-characters-with-faces
2844      (point)
2845      (progn (eval gnus-summary-line-format-spec) (point))
2846      'gnus-number gnus-tmp-number)
2847     (when (gnus-visual-p 'summary-highlight 'highlight)
2848       (forward-line -1)
2849       (gnus-run-hooks 'gnus-summary-update-hook)
2850       (forward-line 1))))
2851
2852 (defun gnus-summary-update-line (&optional dont-update)
2853   "Update summary line after change."
2854   (when (and gnus-summary-default-score
2855              (not gnus-summary-inhibit-highlight))
2856     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2857            (article (gnus-summary-article-number))
2858            (score (gnus-summary-article-score article)))
2859       (unless dont-update
2860         (if (and gnus-summary-mark-below
2861                  (< (gnus-summary-article-score)
2862                     gnus-summary-mark-below))
2863             ;; This article has a low score, so we mark it as read.
2864             (when (memq article gnus-newsgroup-unreads)
2865               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2866           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2867             ;; This article was previously marked as read on account
2868             ;; of a low score, but now it has risen, so we mark it as
2869             ;; unread.
2870             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2871         (gnus-summary-update-mark
2872          (if (or (null gnus-summary-default-score)
2873                  (<= (abs (- score gnus-summary-default-score))
2874                      gnus-summary-zcore-fuzz))
2875              ?  ;Whitespace
2876            (if (< score gnus-summary-default-score)
2877                gnus-score-below-mark gnus-score-over-mark))
2878          'score))
2879       ;; Do visual highlighting.
2880       (when (gnus-visual-p 'summary-highlight 'highlight)
2881         (gnus-run-hooks 'gnus-summary-update-hook)))))
2882
2883 (defvar gnus-tmp-new-adopts nil)
2884
2885 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2886   "Return the number of articles in THREAD.
2887 This may be 0 in some cases -- if none of the articles in
2888 the thread are to be displayed."
2889   (let* ((number
2890           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2891           (cond
2892            ((not (listp thread))
2893             1)
2894            ((and (consp thread) (cdr thread))
2895             (apply
2896              '+ 1 (mapcar
2897                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2898            ((null thread)
2899             1)
2900            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2901             1)
2902            (t 0))))
2903     (when (and level (zerop level) gnus-tmp-new-adopts)
2904       (incf number
2905             (apply '+ (mapcar
2906                        'gnus-summary-number-of-articles-in-thread
2907                        gnus-tmp-new-adopts))))
2908     (if char
2909         (if (> number 1) gnus-not-empty-thread-mark
2910           gnus-empty-thread-mark)
2911       number)))
2912
2913 (defun gnus-summary-set-local-parameters (group)
2914   "Go through the local params of GROUP and set all variable specs in that list."
2915   (let ((params (gnus-group-find-parameter group))
2916         elem)
2917     (while params
2918       (setq elem (car params)
2919             params (cdr params))
2920       (and (consp elem)                 ; Has to be a cons.
2921            (consp (cdr elem))           ; The cdr has to be a list.
2922            (symbolp (car elem))         ; Has to be a symbol in there.
2923            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2924            (ignore-errors               ; So we set it.
2925              (make-local-variable (car elem))
2926              (set (car elem) (eval (nth 1 elem))))))))
2927
2928 (defun gnus-summary-read-group (group &optional show-all no-article
2929                                       kill-buffer no-display backward
2930                                       select-articles)
2931   "Start reading news in newsgroup GROUP.
2932 If SHOW-ALL is non-nil, already read articles are also listed.
2933 If NO-ARTICLE is non-nil, no article is selected initially.
2934 If NO-DISPLAY, don't generate a summary buffer."
2935   (let (result)
2936     (while (and group
2937                 (null (setq result
2938                             (let ((gnus-auto-select-next nil))
2939                               (or (gnus-summary-read-group-1
2940                                    group show-all no-article
2941                                    kill-buffer no-display
2942                                    select-articles)
2943                                   (setq show-all nil
2944                                         select-articles nil)))))
2945                 (eq gnus-auto-select-next 'quietly))
2946       (set-buffer gnus-group-buffer)
2947       ;; The entry function called above goes to the next
2948       ;; group automatically, so we go two groups back
2949       ;; if we are searching for the previous group.
2950       (when backward
2951         (gnus-group-prev-unread-group 2))
2952       (if (not (equal group (gnus-group-group-name)))
2953           (setq group (gnus-group-group-name))
2954         (setq group nil)))
2955     result))
2956
2957 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2958   "Directly jump to the other GROUP from summary buffer.
2959 If SHOW-ALL is non-nil, already read articles are also listed."
2960   (interactive
2961    (if (eq gnus-summary-buffer (current-buffer))
2962        (list (completing-read
2963               "Group: " gnus-active-hashtb nil t
2964               (when (and gnus-newsgroup-name
2965                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2966                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2967               'gnus-group-history)
2968              current-prefix-arg)
2969      (error "%s must be invoked from a gnus summary buffer." this-command)))
2970   (unless (or (zerop (length group))
2971               (and gnus-newsgroup-name
2972                    (string-equal gnus-newsgroup-name group)))
2973     (gnus-summary-exit)
2974     (gnus-summary-read-group group show-all
2975                              gnus-dont-select-after-jump-to-other-group)))
2976
2977 (defun gnus-summary-read-group-1 (group show-all no-article
2978                                         kill-buffer no-display
2979                                         &optional select-articles)
2980   ;; Killed foreign groups can't be entered.
2981   ;;  (when (and (not (gnus-group-native-p group))
2982   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
2983   ;;    (error "Dead non-native groups can't be entered"))
2984   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2985   (let* ((new-group (gnus-summary-setup-buffer group))
2986          (quit-config (gnus-group-quit-config group))
2987          (did-select (and new-group (gnus-select-newsgroup
2988                                      group show-all select-articles))))
2989     (cond
2990      ;; This summary buffer exists already, so we just select it.
2991      ((not new-group)
2992       (gnus-set-global-variables)
2993       (when kill-buffer
2994         (gnus-kill-or-deaden-summary kill-buffer))
2995       (gnus-configure-windows 'summary 'force)
2996       (gnus-set-mode-line 'summary)
2997       (gnus-summary-position-point)
2998       (message "")
2999       t)
3000      ;; We couldn't select this group.
3001      ((null did-select)
3002       (when (and (eq major-mode 'gnus-summary-mode)
3003                  (not (equal (current-buffer) kill-buffer)))
3004         (kill-buffer (current-buffer))
3005         (if (not quit-config)
3006             (progn
3007               ;; Update the info -- marks might need to be removed,
3008               ;; for instance.
3009               (gnus-summary-update-info)
3010               (set-buffer gnus-group-buffer)
3011               (gnus-group-jump-to-group group)
3012               (gnus-group-next-unread-group 1))
3013           (gnus-handle-ephemeral-exit quit-config)))
3014       (let ((grpinfo (gnus-get-info group)))
3015         (if (null (gnus-info-read grpinfo))
3016             (gnus-message 3 "Group %s contains no messages" group)
3017           (gnus-message 3 "Can't select group")))
3018       nil)
3019      ;; The user did a `C-g' while prompting for number of articles,
3020      ;; so we exit this group.
3021      ((eq did-select 'quit)
3022       (and (eq major-mode 'gnus-summary-mode)
3023            (not (equal (current-buffer) kill-buffer))
3024            (kill-buffer (current-buffer)))
3025       (when kill-buffer
3026         (gnus-kill-or-deaden-summary kill-buffer))
3027       (if (not quit-config)
3028           (progn
3029             (set-buffer gnus-group-buffer)
3030             (gnus-group-jump-to-group group)
3031             (gnus-group-next-unread-group 1)
3032             (gnus-configure-windows 'group 'force))
3033         (gnus-handle-ephemeral-exit quit-config))
3034       ;; Finally signal the quit.
3035       (signal 'quit nil))
3036      ;; The group was successfully selected.
3037      (t
3038       (gnus-set-global-variables)
3039       ;; Save the active value in effect when the group was entered.
3040       (setq gnus-newsgroup-active
3041             (gnus-copy-sequence
3042              (gnus-active gnus-newsgroup-name)))
3043       ;; You can change the summary buffer in some way with this hook.
3044       (gnus-run-hooks 'gnus-select-group-hook)
3045       ;; Set any local variables in the group parameters.
3046       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3047       (gnus-update-format-specifications
3048        nil 'summary 'summary-mode 'summary-dummy)
3049       (gnus-update-summary-mark-positions)
3050       ;; Do score processing.
3051       (when gnus-use-scoring
3052         (gnus-possibly-score-headers))
3053       ;; Check whether to fill in the gaps in the threads.
3054       (when gnus-build-sparse-threads
3055         (gnus-build-sparse-threads))
3056       ;; Find the initial limit.
3057       (if gnus-show-threads
3058           (if show-all
3059               (let ((gnus-newsgroup-dormant nil))
3060                 (gnus-summary-initial-limit show-all))
3061             (gnus-summary-initial-limit show-all))
3062         ;; When untreaded, all articles are always shown.
3063         (setq gnus-newsgroup-limit
3064               (mapcar
3065                (lambda (header) (mail-header-number header))
3066                gnus-newsgroup-headers)))
3067       ;; Generate the summary buffer.
3068       (unless no-display
3069         (gnus-summary-prepare))
3070       (when gnus-use-trees
3071         (gnus-tree-open group)
3072         (setq gnus-summary-highlight-line-function
3073               'gnus-tree-highlight-article))
3074       ;; If the summary buffer is empty, but there are some low-scored
3075       ;; articles or some excluded dormants, we include these in the
3076       ;; buffer.
3077       (when (and (zerop (buffer-size))
3078                  (not no-display))
3079         (cond (gnus-newsgroup-dormant
3080                (gnus-summary-limit-include-dormant))
3081               ((and gnus-newsgroup-scored show-all)
3082                (gnus-summary-limit-include-expunged t))))
3083       ;; Function `gnus-apply-kill-file' must be called in this hook.
3084       (gnus-run-hooks 'gnus-apply-kill-hook)
3085       (if (and (zerop (buffer-size))
3086                (not no-display))
3087           (progn
3088             ;; This newsgroup is empty.
3089             (gnus-summary-catchup-and-exit nil t)
3090             (gnus-message 6 "No unread news")
3091             (when kill-buffer
3092               (gnus-kill-or-deaden-summary kill-buffer))
3093             ;; Return nil from this function.
3094             nil)
3095         ;; Hide conversation thread subtrees.  We cannot do this in
3096         ;; gnus-summary-prepare-hook since kill processing may not
3097         ;; work with hidden articles.
3098         (and gnus-show-threads
3099              gnus-thread-hide-subtree
3100              (gnus-summary-hide-all-threads))
3101         (when kill-buffer
3102           (gnus-kill-or-deaden-summary kill-buffer))
3103         ;; Show first unread article if requested.
3104         (if (and (not no-article)
3105                  (not no-display)
3106                  gnus-newsgroup-unreads
3107                  gnus-auto-select-first)
3108             (progn
3109               (gnus-configure-windows 'summary)
3110               (cond
3111                ((eq gnus-auto-select-first 'best)
3112                 (gnus-summary-best-unread-article))
3113                ((eq gnus-auto-select-first t)
3114                 (gnus-summary-first-unread-article))
3115                ((gnus-functionp gnus-auto-select-first)
3116                 (funcall gnus-auto-select-first))))
3117           ;; Don't select any articles, just move point to the first
3118           ;; article in the group.
3119           (goto-char (point-min))
3120           (gnus-summary-position-point)
3121           (gnus-configure-windows 'summary 'force)
3122           (gnus-set-mode-line 'summary))
3123         (when (get-buffer-window gnus-group-buffer t)
3124           ;; Gotta use windows, because recenter does weird stuff if
3125           ;; the current buffer ain't the displayed window.
3126           (let ((owin (selected-window)))
3127             (select-window (get-buffer-window gnus-group-buffer t))
3128             (when (gnus-group-goto-group group)
3129               (recenter))
3130             (select-window owin)))
3131         ;; Mark this buffer as "prepared".
3132         (setq gnus-newsgroup-prepared t)
3133         (gnus-run-hooks 'gnus-summary-prepared-hook)
3134         t)))))
3135
3136 (defun gnus-summary-prepare ()
3137   "Generate the summary buffer."
3138   (interactive)
3139   (let ((buffer-read-only nil))
3140     (erase-buffer)
3141     (setq gnus-newsgroup-data nil
3142           gnus-newsgroup-data-reverse nil)
3143     (gnus-run-hooks 'gnus-summary-generate-hook)
3144     ;; Generate the buffer, either with threads or without.
3145     (when gnus-newsgroup-headers
3146       (gnus-summary-prepare-threads
3147        (if gnus-show-threads
3148            (gnus-sort-gathered-threads
3149             (funcall gnus-summary-thread-gathering-function
3150                      (gnus-sort-threads
3151                       (gnus-cut-threads (gnus-make-threads)))))
3152          ;; Unthreaded display.
3153          (gnus-sort-articles gnus-newsgroup-headers))))
3154     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3155     ;; Call hooks for modifying summary buffer.
3156     (goto-char (point-min))
3157     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3158
3159 (defsubst gnus-general-simplify-subject (subject)
3160   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3161   (setq subject
3162         (cond
3163          ;; Truncate the subject.
3164          (gnus-simplify-subject-functions
3165           (gnus-map-function gnus-simplify-subject-functions subject))
3166          ((numberp gnus-summary-gather-subject-limit)
3167           (setq subject (gnus-simplify-subject-re subject))
3168           (if (> (length subject) gnus-summary-gather-subject-limit)
3169               (substring subject 0 gnus-summary-gather-subject-limit)
3170             subject))
3171          ;; Fuzzily simplify it.
3172          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3173           (gnus-simplify-subject-fuzzy subject))
3174          ;; Just remove the leading "Re:".
3175          (t
3176           (gnus-simplify-subject-re subject))))
3177
3178   (if (and gnus-summary-gather-exclude-subject
3179            (string-match gnus-summary-gather-exclude-subject subject))
3180       nil                               ; This article shouldn't be gathered
3181     subject))
3182
3183 (defun gnus-summary-simplify-subject-query ()
3184   "Query where the respool algorithm would put this article."
3185   (interactive)
3186   (gnus-summary-select-article)
3187   (message "%s"
3188            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3189
3190 (defun gnus-gather-threads-by-subject (threads)
3191   "Gather threads by looking at Subject headers."
3192   (if (not gnus-summary-make-false-root)
3193       threads
3194     (let ((hashtb (gnus-make-hashtable 1024))
3195           (prev threads)
3196           (result threads)
3197           subject hthread whole-subject)
3198       (while threads
3199         (setq subject (gnus-general-simplify-subject
3200                        (setq whole-subject (mail-header-subject
3201                                             (caar threads)))))
3202         (when subject
3203           (if (setq hthread (gnus-gethash subject hashtb))
3204               (progn
3205                 ;; We enter a dummy root into the thread, if we
3206                 ;; haven't done that already.
3207                 (unless (stringp (caar hthread))
3208                   (setcar hthread (list whole-subject (car hthread))))
3209                 ;; We add this new gathered thread to this gathered
3210                 ;; thread.
3211                 (setcdr (car hthread)
3212                         (nconc (cdar hthread) (list (car threads))))
3213                 ;; Remove it from the list of threads.
3214                 (setcdr prev (cdr threads))
3215                 (setq threads prev))
3216             ;; Enter this thread into the hash table.
3217             (gnus-sethash subject threads hashtb)))
3218         (setq prev threads)
3219         (setq threads (cdr threads)))
3220       result)))
3221
3222 (defun gnus-gather-threads-by-references (threads)
3223   "Gather threads by looking at References headers."
3224   (let ((idhashtb (gnus-make-hashtable 1024))
3225         (thhashtb (gnus-make-hashtable 1024))
3226         (prev threads)
3227         (result threads)
3228         ids references id gthread gid entered ref)
3229     (while threads
3230       (when (setq references (mail-header-references (caar threads)))
3231         (setq id (mail-header-id (caar threads))
3232               ids (gnus-split-references references)
3233               entered nil)
3234         (while (setq ref (pop ids))
3235           (setq ids (delete ref ids))
3236           (if (not (setq gid (gnus-gethash ref idhashtb)))
3237               (progn
3238                 (gnus-sethash ref id idhashtb)
3239                 (gnus-sethash id threads thhashtb))
3240             (setq gthread (gnus-gethash gid thhashtb))
3241             (unless entered
3242               ;; We enter a dummy root into the thread, if we
3243               ;; haven't done that already.
3244               (unless (stringp (caar gthread))
3245                 (setcar gthread (list (mail-header-subject (caar gthread))
3246                                       (car gthread))))
3247               ;; We add this new gathered thread to this gathered
3248               ;; thread.
3249               (setcdr (car gthread)
3250                       (nconc (cdar gthread) (list (car threads)))))
3251             ;; Add it into the thread hash table.
3252             (gnus-sethash id gthread thhashtb)
3253             (setq entered t)
3254             ;; Remove it from the list of threads.
3255             (setcdr prev (cdr threads))
3256             (setq threads prev))))
3257       (setq prev threads)
3258       (setq threads (cdr threads)))
3259     result))
3260
3261 (defun gnus-sort-gathered-threads (threads)
3262   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3263   (let ((result threads))
3264     (while threads
3265       (when (stringp (caar threads))
3266         (setcdr (car threads)
3267                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3268       (setq threads (cdr threads)))
3269     result))
3270
3271 (defun gnus-thread-loop-p (root thread)
3272   "Say whether ROOT is in THREAD."
3273   (let ((stack (list thread))
3274         (infloop 0)
3275         th)
3276     (while (setq thread (pop stack))
3277       (setq th (cdr thread))
3278       (while (and th
3279                   (not (eq (caar th) root)))
3280         (pop th))
3281       (if th
3282           ;; We have found a loop.
3283           (let (ref-dep)
3284             (setcdr thread (delq (car th) (cdr thread)))
3285             (if (boundp (setq ref-dep (intern "none"
3286                                               gnus-newsgroup-dependencies)))
3287                 (setcdr (symbol-value ref-dep)
3288                         (nconc (cdr (symbol-value ref-dep))
3289                                (list (car th))))
3290               (set ref-dep (list nil (car th))))
3291             (setq infloop 1
3292                   stack nil))
3293         ;; Push all the subthreads onto the stack.
3294         (push (cdr thread) stack)))
3295     infloop))
3296
3297 (defun gnus-make-threads ()
3298   "Go through the dependency hashtb and find the roots.  Return all threads."
3299   (let (threads)
3300     (while (catch 'infloop
3301              (mapatoms
3302               (lambda (refs)
3303                 ;; Deal with self-referencing References loops.
3304                 (when (and (car (symbol-value refs))
3305                            (not (zerop
3306                                  (apply
3307                                   '+
3308                                   (mapcar
3309                                    (lambda (thread)
3310                                      (gnus-thread-loop-p
3311                                       (car (symbol-value refs)) thread))
3312                                    (cdr (symbol-value refs)))))))
3313                   (setq threads nil)
3314                   (throw 'infloop t))
3315                 (unless (car (symbol-value refs))
3316                   ;; These threads do not refer back to any other articles,
3317                   ;; so they're roots.
3318                   (setq threads (append (cdr (symbol-value refs)) threads))))
3319               gnus-newsgroup-dependencies)))
3320     threads))
3321
3322 ;; Build the thread tree.
3323 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3324   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3325
3326 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3327 if it was already present.
3328
3329 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3330 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3331 Message-IDs will be renamed be renamed to a unique Message-ID before
3332 being entered.
3333
3334 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3335   (let* ((id (mail-header-id header))
3336          (id-dep (and id (intern id dependencies)))
3337          ref ref-dep ref-header)
3338     ;; Enter this `header' in the `dependencies' table.
3339     (cond
3340      ((not id-dep)
3341       (setq header nil))
3342      ;; The first two cases do the normal part: enter a new `header'
3343      ;; in the `dependencies' table.
3344      ((not (boundp id-dep))
3345       (set id-dep (list header)))
3346      ((null (car (symbol-value id-dep)))
3347       (setcar (symbol-value id-dep) header))
3348
3349      ;; From here the `header' was already present in the
3350      ;; `dependencies' table.
3351      (force-new
3352       ;; Overrides an existing entry;
3353       ;; just set the header part of the entry.
3354       (setcar (symbol-value id-dep) header))
3355
3356      ;; Renames the existing `header' to a unique Message-ID.
3357      ((not gnus-summary-ignore-duplicates)
3358       ;; An article with this Message-ID has already been seen.
3359       ;; We rename the Message-ID.
3360       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3361            (list header))
3362       (mail-header-set-id header id))
3363
3364      ;; The last case ignores an existing entry, except it adds any
3365      ;; additional Xrefs (in case the two articles came from different
3366      ;; servers.
3367      ;; Also sets `header' to `nil' meaning that the `dependencies'
3368      ;; table was *not* modified.
3369      (t
3370       (mail-header-set-xref
3371        (car (symbol-value id-dep))
3372        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3373                    "")
3374                (or (mail-header-xref header) "")))
3375       (setq header nil)))
3376
3377     (when header
3378       ;; First check if that we are not creating a References loop.
3379       (setq ref (gnus-parent-id (mail-header-references header)))
3380       (while (and ref
3381                   (setq ref-dep (intern-soft ref dependencies))
3382                   (boundp ref-dep)
3383                   (setq ref-header (car (symbol-value ref-dep))))
3384         (if (string= id ref)
3385             ;; Yuk!  This is a reference loop.  Make the article be a
3386             ;; root article.
3387             (progn
3388               (mail-header-set-references (car (symbol-value id-dep)) "none")
3389               (setq ref nil))
3390           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3391       (setq ref (gnus-parent-id (mail-header-references header)))
3392       (setq ref-dep (intern (or ref "none") dependencies))
3393       (if (boundp ref-dep)
3394           (setcdr (symbol-value ref-dep)
3395                   (nconc (cdr (symbol-value ref-dep))
3396                          (list (symbol-value id-dep))))
3397         (set ref-dep (list nil (symbol-value id-dep)))))
3398     header))
3399
3400 (defun gnus-build-sparse-threads ()
3401   (let ((headers gnus-newsgroup-headers)
3402         (mail-parse-charset gnus-newsgroup-charset)
3403         (gnus-summary-ignore-duplicates t)
3404         header references generation relations
3405         subject child end new-child date)
3406     ;; First we create an alist of generations/relations, where
3407     ;; generations is how much we trust the relation, and the relation
3408     ;; is parent/child.
3409     (gnus-message 7 "Making sparse threads...")
3410     (save-excursion
3411       (nnheader-set-temp-buffer " *gnus sparse threads*")
3412       (while (setq header (pop headers))
3413         (when (and (setq references (mail-header-references header))
3414                    (not (string= references "")))
3415           (insert references)
3416           (setq child (mail-header-id header)
3417                 subject (mail-header-subject header)
3418                 date (mail-header-date header)
3419                 generation 0)
3420           (while (search-backward ">" nil t)
3421             (setq end (1+ (point)))
3422             (when (search-backward "<" nil t)
3423               (setq new-child (buffer-substring (point) end))
3424               (push (list (incf generation)
3425                           child (setq child new-child)
3426                           subject date)
3427                     relations)))
3428           (when child
3429             (push (list (1+ generation) child nil subject) relations))
3430           (erase-buffer)))
3431       (kill-buffer (current-buffer)))
3432     ;; Sort over trustworthiness.
3433     (mapcar
3434      (lambda (relation)
3435        (when (gnus-dependencies-add-header
3436               (make-full-mail-header-from-decoded-header
3437                gnus-reffed-article-number
3438                (nth 3 relation) "" (or (nth 4 relation) "")
3439                (nth 1 relation)
3440                (or (nth 2 relation) "") 0 0 "")
3441               gnus-newsgroup-dependencies nil)
3442          (push gnus-reffed-article-number gnus-newsgroup-limit)
3443          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3444          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3445                gnus-newsgroup-reads)
3446          (decf gnus-reffed-article-number)))
3447      (sort relations 'car-less-than-car))
3448     (gnus-message 7 "Making sparse threads...done")))
3449
3450 (defun gnus-build-old-threads ()
3451   ;; Look at all the articles that refer back to old articles, and
3452   ;; fetch the headers for the articles that aren't there.  This will
3453   ;; build complete threads - if the roots haven't been expired by the
3454   ;; server, that is.
3455   (let ((mail-parse-charset gnus-newsgroup-charset)
3456         id heads)
3457     (mapatoms
3458      (lambda (refs)
3459        (when (not (car (symbol-value refs)))
3460          (setq heads (cdr (symbol-value refs)))
3461          (while heads
3462            (if (memq (mail-header-number (caar heads))
3463                      gnus-newsgroup-dormant)
3464                (setq heads (cdr heads))
3465              (setq id (symbol-name refs))
3466              (while (and (setq id (gnus-build-get-header id))
3467                          (not (car (gnus-id-to-thread id)))))
3468              (setq heads nil)))))
3469      gnus-newsgroup-dependencies)))
3470
3471 ;; This function has to be called with point after the article number
3472 ;; on the beginning of the line.
3473 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3474   (let ((eol (gnus-point-at-eol))
3475         (buffer (current-buffer))
3476         header)
3477
3478     ;; overview: [num subject from date id refs chars lines misc]
3479     (unwind-protect
3480         (progn
3481           (narrow-to-region (point) eol)
3482           (unless (eobp)
3483             (forward-char))
3484
3485           (setq header
3486                 (make-full-mail-header
3487                  number                         ; number
3488                  (nnheader-nov-field)           ; subject
3489                  (nnheader-nov-field)           ; from
3490                  (nnheader-nov-field)           ; date
3491                  (nnheader-nov-read-message-id) ; id
3492                  (nnheader-nov-field)           ; refs
3493                  (nnheader-nov-read-integer)    ; chars
3494                  (nnheader-nov-read-integer)    ; lines
3495                  (unless (eobp)
3496                    (if (looking-at "Xref: ")
3497                        (goto-char (match-end 0)))
3498                    (nnheader-nov-field))        ; Xref
3499                  (nnheader-nov-parse-extra))))  ; extra
3500
3501       (widen))
3502
3503     (when gnus-alter-header-function
3504       (funcall gnus-alter-header-function header))
3505     (gnus-dependencies-add-header header dependencies force-new)))
3506
3507 (defun gnus-build-get-header (id)
3508   "Look through the buffer of NOV lines and find the header to ID.
3509 Enter this line into the dependencies hash table, and return
3510 the id of the parent article (if any)."
3511   (let ((deps gnus-newsgroup-dependencies)
3512         found header)
3513     (prog1
3514         (save-excursion
3515           (set-buffer nntp-server-buffer)
3516           (let ((case-fold-search nil))
3517             (goto-char (point-min))
3518             (while (and (not found)
3519                         (search-forward id nil t))
3520               (beginning-of-line)
3521               (setq found (looking-at
3522                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3523                                    (regexp-quote id))))
3524               (or found (beginning-of-line 2)))
3525             (when found
3526               (beginning-of-line)
3527               (and
3528                (setq header (gnus-nov-parse-line
3529                              (read (current-buffer)) deps))
3530                (gnus-parent-id (mail-header-references header))))))
3531       (when header
3532         (let ((number (mail-header-number header)))
3533           (push number gnus-newsgroup-limit)
3534           (push header gnus-newsgroup-headers)
3535           (if (memq number gnus-newsgroup-unselected)
3536               (progn
3537                 (push number gnus-newsgroup-unreads)
3538                 (setq gnus-newsgroup-unselected
3539                       (delq number gnus-newsgroup-unselected)))
3540             (push number gnus-newsgroup-ancient)))))))
3541
3542 (defun gnus-build-all-threads ()
3543   "Read all the headers."
3544   (let ((gnus-summary-ignore-duplicates t)
3545         (mail-parse-charset gnus-newsgroup-charset)
3546         (dependencies gnus-newsgroup-dependencies)
3547         header article)
3548     (save-excursion
3549       (set-buffer nntp-server-buffer)
3550       (let ((case-fold-search nil))
3551         (goto-char (point-min))
3552         (while (not (eobp))
3553           (ignore-errors
3554             (setq article (read (current-buffer))
3555                   header (gnus-nov-parse-line article dependencies)))
3556           (when header
3557             (save-excursion
3558               (set-buffer gnus-summary-buffer)
3559               (push header gnus-newsgroup-headers)
3560               (if (memq (setq article (mail-header-number header))
3561                         gnus-newsgroup-unselected)
3562                   (progn
3563                     (push article gnus-newsgroup-unreads)
3564                     (setq gnus-newsgroup-unselected
3565                           (delq article gnus-newsgroup-unselected)))
3566                 (push article gnus-newsgroup-ancient)))
3567             (forward-line 1)))))))
3568
3569 (defun gnus-summary-update-article-line (article header)
3570   "Update the line for ARTICLE using HEADERS."
3571   (let* ((id (mail-header-id header))
3572          (thread (gnus-id-to-thread id)))
3573     (unless thread
3574       (error "Article in no thread"))
3575     ;; Update the thread.
3576     (setcar thread header)
3577     (gnus-summary-goto-subject article)
3578     (let* ((datal (gnus-data-find-list article))
3579            (data (car datal))
3580            (length (when (cdr datal)
3581                      (- (gnus-data-pos data)
3582                         (gnus-data-pos (cadr datal)))))
3583            (buffer-read-only nil)
3584            (level (gnus-summary-thread-level)))
3585       (gnus-delete-line)
3586       (gnus-summary-insert-line
3587        header level nil (gnus-article-mark article)
3588        (memq article gnus-newsgroup-replied)
3589        (memq article gnus-newsgroup-expirable)
3590        ;; Only insert the Subject string when it's different
3591        ;; from the previous Subject string.
3592        (if (and
3593             gnus-show-threads
3594             (gnus-subject-equal
3595              (condition-case ()
3596                  (mail-header-subject
3597                   (gnus-data-header
3598                    (cadr
3599                     (gnus-data-find-list
3600                      article
3601                      (gnus-data-list t)))))
3602                ;; Error on the side of excessive subjects.
3603                (error ""))
3604              (mail-header-subject header)))
3605            ""
3606          (mail-header-subject header))
3607        nil (cdr (assq article gnus-newsgroup-scored))
3608        (memq article gnus-newsgroup-processable))
3609       (when length
3610         (gnus-data-update-list
3611          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3612
3613 (defun gnus-summary-update-article (article &optional iheader)
3614   "Update ARTICLE in the summary buffer."
3615   (set-buffer gnus-summary-buffer)
3616   (let* ((header (gnus-summary-article-header article))
3617          (id (mail-header-id header))
3618          (data (gnus-data-find article))
3619          (thread (gnus-id-to-thread id))
3620          (references (mail-header-references header))
3621          (parent
3622           (gnus-id-to-thread
3623            (or (gnus-parent-id
3624                 (when (and references
3625                            (not (equal "" references)))
3626                   references))
3627                "none")))
3628          (buffer-read-only nil)
3629          (old (car thread)))
3630     (when thread
3631       (unless iheader
3632         (setcar thread nil)
3633         (when parent
3634           (delq thread parent)))
3635       (if (gnus-summary-insert-subject id header)
3636           ;; Set the (possibly) new article number in the data structure.
3637           (gnus-data-set-number data (gnus-id-to-article id))
3638         (setcar thread old)
3639         nil))))
3640
3641 (defun gnus-rebuild-thread (id &optional line)
3642   "Rebuild the thread containing ID.
3643 If LINE, insert the rebuilt thread starting on line LINE."
3644   (let ((buffer-read-only nil)
3645         old-pos current thread data)
3646     (if (not gnus-show-threads)
3647         (setq thread (list (car (gnus-id-to-thread id))))
3648       ;; Get the thread this article is part of.
3649       (setq thread (gnus-remove-thread id)))
3650     (setq old-pos (gnus-point-at-bol))
3651     (setq current (save-excursion
3652                     (and (zerop (forward-line -1))
3653                          (gnus-summary-article-number))))
3654     ;; If this is a gathered thread, we have to go some re-gathering.
3655     (when (stringp (car thread))
3656       (let ((subject (car thread))
3657             roots thr)
3658         (setq thread (cdr thread))
3659         (while thread
3660           (unless (memq (setq thr (gnus-id-to-thread
3661                                    (gnus-root-id
3662                                     (mail-header-id (caar thread)))))
3663                         roots)
3664             (push thr roots))
3665           (setq thread (cdr thread)))
3666         ;; We now have all (unique) roots.
3667         (if (= (length roots) 1)
3668             ;; All the loose roots are now one solid root.
3669             (setq thread (car roots))
3670           (setq thread (cons subject (gnus-sort-threads roots))))))
3671     (let (threads)
3672       ;; We then insert this thread into the summary buffer.
3673       (when line
3674         (goto-char (point-min))
3675         (forward-line (1- line)))
3676       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3677         (if gnus-show-threads
3678             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3679           (gnus-summary-prepare-unthreaded thread))
3680         (setq data (nreverse gnus-newsgroup-data))
3681         (setq threads gnus-newsgroup-threads))
3682       ;; We splice the new data into the data structure.
3683       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3684       ;;!!! then we want to insert at the beginning of the buffer.
3685       ;;!!! That happens to be true with Gnus now, but that may
3686       ;;!!! change in the future.  Perhaps.
3687       (gnus-data-enter-list
3688        (if line nil current) data (- (point) old-pos))
3689       (setq gnus-newsgroup-threads
3690             (nconc threads gnus-newsgroup-threads))
3691       (gnus-data-compute-positions))))
3692
3693 (defun gnus-number-to-header (number)
3694   "Return the header for article NUMBER."
3695   (let ((headers gnus-newsgroup-headers))
3696     (while (and headers
3697                 (not (= number (mail-header-number (car headers)))))
3698       (pop headers))
3699     (when headers
3700       (car headers))))
3701
3702 (defun gnus-parent-headers (in-headers &optional generation)
3703   "Return the headers of the GENERATIONeth parent of HEADERS."
3704   (unless generation
3705     (setq generation 1))
3706   (let ((parent t)
3707         (headers in-headers)
3708         references)
3709     (while (and parent
3710                 (not (zerop generation))
3711                 (setq references (mail-header-references headers)))
3712       (setq headers (if (and references
3713                              (setq parent (gnus-parent-id references)))
3714                         (car (gnus-id-to-thread parent))
3715                       nil))
3716       (decf generation))
3717     (and (not (eq headers in-headers))
3718          headers)))
3719
3720 (defun gnus-id-to-thread (id)
3721   "Return the (sub-)thread where ID appears."
3722   (gnus-gethash id gnus-newsgroup-dependencies))
3723
3724 (defun gnus-id-to-article (id)
3725   "Return the article number of ID."
3726   (let ((thread (gnus-id-to-thread id)))
3727     (when (and thread
3728                (car thread))
3729       (mail-header-number (car thread)))))
3730
3731 (defun gnus-id-to-header (id)
3732   "Return the article headers of ID."
3733   (car (gnus-id-to-thread id)))
3734
3735 (defun gnus-article-displayed-root-p (article)
3736   "Say whether ARTICLE is a root(ish) article."
3737   (let ((level (gnus-summary-thread-level article))
3738         (refs (mail-header-references  (gnus-summary-article-header article)))
3739         particle)
3740     (cond
3741      ((null level) nil)
3742      ((zerop level) t)
3743      ((null refs) t)
3744      ((null (gnus-parent-id refs)) t)
3745      ((and (= 1 level)
3746            (null (setq particle (gnus-id-to-article
3747                                  (gnus-parent-id refs))))
3748            (null (gnus-summary-thread-level particle)))))))
3749
3750 (defun gnus-root-id (id)
3751   "Return the id of the root of the thread where ID appears."
3752   (let (last-id prev)
3753     (while (and id (setq prev (car (gnus-id-to-thread id))))
3754       (setq last-id id
3755             id (gnus-parent-id (mail-header-references prev))))
3756     last-id))
3757
3758 (defun gnus-articles-in-thread (thread)
3759   "Return the list of articles in THREAD."
3760   (cons (mail-header-number (car thread))
3761         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3762
3763 (defun gnus-remove-thread (id &optional dont-remove)
3764   "Remove the thread that has ID in it."
3765   (let (headers thread last-id)
3766     ;; First go up in this thread until we find the root.
3767     (setq last-id (gnus-root-id id)
3768           headers (message-flatten-list (gnus-id-to-thread last-id)))
3769     ;; We have now found the real root of this thread.  It might have
3770     ;; been gathered into some loose thread, so we have to search
3771     ;; through the threads to find the thread we wanted.
3772     (let ((threads gnus-newsgroup-threads)
3773           sub)
3774       (while threads
3775         (setq sub (car threads))
3776         (if (stringp (car sub))
3777             ;; This is a gathered thread, so we look at the roots
3778             ;; below it to find whether this article is in this
3779             ;; gathered root.
3780             (progn
3781               (setq sub (cdr sub))
3782               (while sub
3783                 (when (member (caar sub) headers)
3784                   (setq thread (car threads)
3785                         threads nil
3786                         sub nil))
3787                 (setq sub (cdr sub))))
3788           ;; It's an ordinary thread, so we check it.
3789           (when (eq (car sub) (car headers))
3790             (setq thread sub
3791                   threads nil)))
3792         (setq threads (cdr threads)))
3793       ;; If this article is in no thread, then it's a root.
3794       (if thread
3795           (unless dont-remove
3796             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3797         (setq thread (gnus-id-to-thread last-id)))
3798       (when thread
3799         (prog1
3800             thread                      ; We return this thread.
3801           (unless dont-remove
3802             (if (stringp (car thread))
3803                 (progn
3804                   ;; If we use dummy roots, then we have to remove the
3805                   ;; dummy root as well.
3806                   (when (eq gnus-summary-make-false-root 'dummy)
3807                     ;; We go to the dummy root by going to
3808                     ;; the first sub-"thread", and then one line up.
3809                     (gnus-summary-goto-article
3810                      (mail-header-number (caadr thread)))
3811                     (forward-line -1)
3812                     (gnus-delete-line)
3813                     (gnus-data-compute-positions))
3814                   (setq thread (cdr thread))
3815                   (while thread
3816                     (gnus-remove-thread-1 (car thread))
3817                     (setq thread (cdr thread))))
3818               (gnus-remove-thread-1 thread))))))))
3819
3820 (defun gnus-remove-thread-1 (thread)
3821   "Remove the thread THREAD recursively."
3822   (let ((number (mail-header-number (pop thread)))
3823         d)
3824     (setq thread (reverse thread))
3825     (while thread
3826       (gnus-remove-thread-1 (pop thread)))
3827     (when (setq d (gnus-data-find number))
3828       (goto-char (gnus-data-pos d))
3829       (gnus-summary-show-thread)
3830       (gnus-data-remove
3831        number
3832        (- (gnus-point-at-bol)
3833           (prog1
3834               (1+ (gnus-point-at-eol))
3835             (gnus-delete-line)))))))
3836
3837 (defun gnus-sort-threads-1 (threads func)
3838   (sort (mapcar (lambda (thread)
3839                   (cons (car thread)
3840                         (and (cdr thread)
3841                              (gnus-sort-threads-1 (cdr thread) func))))
3842                 threads) func))
3843
3844 (defun gnus-sort-threads (threads)
3845   "Sort THREADS."
3846   (if (not gnus-thread-sort-functions)
3847       threads
3848     (gnus-message 8 "Sorting threads...")
3849     (prog1
3850         (gnus-sort-threads-1
3851          threads
3852          (gnus-make-sort-function gnus-thread-sort-functions))
3853       (gnus-message 8 "Sorting threads...done"))))
3854
3855 (defun gnus-sort-articles (articles)
3856   "Sort ARTICLES."
3857   (when gnus-article-sort-functions
3858     (gnus-message 7 "Sorting articles...")
3859     (prog1
3860         (setq gnus-newsgroup-headers
3861               (sort articles (gnus-make-sort-function
3862                               gnus-article-sort-functions)))
3863       (gnus-message 7 "Sorting articles...done"))))
3864
3865 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3866 (defmacro gnus-thread-header (thread)
3867   "Return header of first article in THREAD.
3868 Note that THREAD must never, ever be anything else than a variable -
3869 using some other form will lead to serious barfage."
3870   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3871   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3872   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3873         (vector thread) 2))
3874
3875 (defsubst gnus-article-sort-by-number (h1 h2)
3876   "Sort articles by article number."
3877   (< (mail-header-number h1)
3878      (mail-header-number h2)))
3879
3880 (defun gnus-thread-sort-by-number (h1 h2)
3881   "Sort threads by root article number."
3882   (gnus-article-sort-by-number
3883    (gnus-thread-header h1) (gnus-thread-header h2)))
3884
3885 (defsubst gnus-article-sort-by-lines (h1 h2)
3886   "Sort articles by article Lines header."
3887   (< (mail-header-lines h1)
3888      (mail-header-lines h2)))
3889
3890 (defun gnus-thread-sort-by-lines (h1 h2)
3891   "Sort threads by root article Lines header."
3892   (gnus-article-sort-by-lines
3893    (gnus-thread-header h1) (gnus-thread-header h2)))
3894
3895 (defsubst gnus-article-sort-by-chars (h1 h2)
3896   "Sort articles by octet length."
3897   (< (mail-header-chars h1)
3898      (mail-header-chars h2)))
3899
3900 (defun gnus-thread-sort-by-chars (h1 h2)
3901   "Sort threads by root article octet length."
3902   (gnus-article-sort-by-chars
3903    (gnus-thread-header h1) (gnus-thread-header h2)))
3904
3905 (defsubst gnus-article-sort-by-author (h1 h2)
3906   "Sort articles by root author."
3907   (string-lessp
3908    (let ((addr (car (mime-entity-read-field h1 'From))))
3909      (or (std11-full-name-string addr)
3910          (std11-address-string addr)
3911          ""))
3912    (let ((addr (car (mime-entity-read-field h2 'From))))
3913      (or (std11-full-name-string addr)
3914          (std11-address-string addr)
3915          ""))
3916    ))
3917
3918 (defun gnus-thread-sort-by-author (h1 h2)
3919   "Sort threads by root author."
3920   (gnus-article-sort-by-author
3921    (gnus-thread-header h1)  (gnus-thread-header h2)))
3922
3923 (defsubst gnus-article-sort-by-subject (h1 h2)
3924   "Sort articles by root subject."
3925   (string-lessp
3926    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3927    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3928
3929 (defun gnus-thread-sort-by-subject (h1 h2)
3930   "Sort threads by root subject."
3931   (gnus-article-sort-by-subject
3932    (gnus-thread-header h1) (gnus-thread-header h2)))
3933
3934 (defsubst gnus-article-sort-by-date (h1 h2)
3935   "Sort articles by root article date."
3936   (time-less-p
3937    (gnus-date-get-time (mail-header-date h1))
3938    (gnus-date-get-time (mail-header-date h2))))
3939
3940 (defun gnus-thread-sort-by-date (h1 h2)
3941   "Sort threads by root article date."
3942   (gnus-article-sort-by-date
3943    (gnus-thread-header h1) (gnus-thread-header h2)))
3944
3945 (defsubst gnus-article-sort-by-score (h1 h2)
3946   "Sort articles by root article score.
3947 Unscored articles will be counted as having a score of zero."
3948   (> (or (cdr (assq (mail-header-number h1)
3949                     gnus-newsgroup-scored))
3950          gnus-summary-default-score 0)
3951      (or (cdr (assq (mail-header-number h2)
3952                     gnus-newsgroup-scored))
3953          gnus-summary-default-score 0)))
3954
3955 (defun gnus-thread-sort-by-score (h1 h2)
3956   "Sort threads by root article score."
3957   (gnus-article-sort-by-score
3958    (gnus-thread-header h1) (gnus-thread-header h2)))
3959
3960 (defun gnus-thread-sort-by-total-score (h1 h2)
3961   "Sort threads by the sum of all scores in the thread.
3962 Unscored articles will be counted as having a score of zero."
3963   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3964
3965 (defun gnus-thread-total-score (thread)
3966   ;; This function find the total score of THREAD.
3967   (cond ((null thread)
3968          0)
3969         ((consp thread)
3970          (if (stringp (car thread))
3971              (apply gnus-thread-score-function 0
3972                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3973            (gnus-thread-total-score-1 thread)))
3974         (t
3975          (gnus-thread-total-score-1 (list thread)))))
3976
3977 (defun gnus-thread-total-score-1 (root)
3978   ;; This function find the total score of the thread below ROOT.
3979   (setq root (car root))
3980   (apply gnus-thread-score-function
3981          (or (append
3982               (mapcar 'gnus-thread-total-score
3983                       (cdr (gnus-id-to-thread (mail-header-id root))))
3984               (when (> (mail-header-number root) 0)
3985                 (list (or (cdr (assq (mail-header-number root)
3986                                      gnus-newsgroup-scored))
3987                           gnus-summary-default-score 0))))
3988              (list gnus-summary-default-score)
3989              '(0))))
3990
3991 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3992 (defvar gnus-tmp-prev-subject nil)
3993 (defvar gnus-tmp-false-parent nil)
3994 (defvar gnus-tmp-root-expunged nil)
3995 (defvar gnus-tmp-dummy-line nil)
3996
3997 (eval-when-compile (defvar gnus-tmp-header))
3998 (defun gnus-extra-header (type &optional header)
3999   "Return the extra header of TYPE."
4000   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4001       ""))
4002
4003 (defun gnus-summary-prepare-threads (threads)
4004   "Prepare summary buffer from THREADS and indentation LEVEL.
4005 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4006 or a straight list of headers."
4007   (gnus-message 7 "Generating summary...")
4008
4009   (setq gnus-newsgroup-threads threads)
4010   (beginning-of-line)
4011
4012   (let ((gnus-tmp-level 0)
4013         (default-score (or gnus-summary-default-score 0))
4014         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4015         thread number subject stack state gnus-tmp-gathered beg-match
4016         new-roots gnus-tmp-new-adopts thread-end
4017         gnus-tmp-header gnus-tmp-unread
4018         gnus-tmp-replied gnus-tmp-subject-or-nil
4019         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4020         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4021         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4022
4023     (setq gnus-tmp-prev-subject nil)
4024
4025     (if (vectorp (car threads))
4026         ;; If this is a straight (sic) list of headers, then a
4027         ;; threaded summary display isn't required, so we just create
4028         ;; an unthreaded one.
4029         (gnus-summary-prepare-unthreaded threads)
4030
4031       ;; Do the threaded display.
4032
4033       (while (or threads stack gnus-tmp-new-adopts new-roots)
4034
4035         (if (and (= gnus-tmp-level 0)
4036                  (or (not stack)
4037                      (= (caar stack) 0))
4038                  (not gnus-tmp-false-parent)
4039                  (or gnus-tmp-new-adopts new-roots))
4040             (if gnus-tmp-new-adopts
4041                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4042                       thread (list (car gnus-tmp-new-adopts))
4043                       gnus-tmp-header (caar thread)
4044                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4045               (when new-roots
4046                 (setq thread (list (car new-roots))
4047                       gnus-tmp-header (caar thread)
4048                       new-roots (cdr new-roots))))
4049
4050           (if threads
4051               ;; If there are some threads, we do them before the
4052               ;; threads on the stack.
4053               (setq thread threads
4054                     gnus-tmp-header (caar thread))
4055             ;; There were no current threads, so we pop something off
4056             ;; the stack.
4057             (setq state (car stack)
4058                   gnus-tmp-level (car state)
4059                   thread (cdr state)
4060                   stack (cdr stack)
4061                   gnus-tmp-header (caar thread))))
4062
4063         (setq gnus-tmp-false-parent nil)
4064         (setq gnus-tmp-root-expunged nil)
4065         (setq thread-end nil)
4066
4067         (if (stringp gnus-tmp-header)
4068             ;; The header is a dummy root.
4069             (cond
4070              ((eq gnus-summary-make-false-root 'adopt)
4071               ;; We let the first article adopt the rest.
4072               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4073                                                (cddar thread)))
4074               (setq gnus-tmp-gathered
4075                     (nconc (mapcar
4076                             (lambda (h) (mail-header-number (car h)))
4077                             (cddar thread))
4078                            gnus-tmp-gathered))
4079               (setq thread (cons (list (caar thread)
4080                                        (cadar thread))
4081                                  (cdr thread)))
4082               (setq gnus-tmp-level -1
4083                     gnus-tmp-false-parent t))
4084              ((eq gnus-summary-make-false-root 'empty)
4085               ;; We print adopted articles with empty subject fields.
4086               (setq gnus-tmp-gathered
4087                     (nconc (mapcar
4088                             (lambda (h) (mail-header-number (car h)))
4089                             (cddar thread))
4090                            gnus-tmp-gathered))
4091               (setq gnus-tmp-level -1))
4092              ((eq gnus-summary-make-false-root 'dummy)
4093               ;; We remember that we probably want to output a dummy
4094               ;; root.
4095               (setq gnus-tmp-dummy-line gnus-tmp-header)
4096               (setq gnus-tmp-prev-subject gnus-tmp-header))
4097              (t
4098               ;; We do not make a root for the gathered
4099               ;; sub-threads at all.
4100               (setq gnus-tmp-level -1)))
4101
4102           (setq number (mail-header-number gnus-tmp-header)
4103                 subject (mail-header-subject gnus-tmp-header))
4104
4105           (cond
4106            ;; If the thread has changed subject, we might want to make
4107            ;; this subthread into a root.
4108            ((and (null gnus-thread-ignore-subject)
4109                  (not (zerop gnus-tmp-level))
4110                  gnus-tmp-prev-subject
4111                  (not (inline
4112                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4113             (setq new-roots (nconc new-roots (list (car thread)))
4114                   thread-end t
4115                   gnus-tmp-header nil))
4116            ;; If the article lies outside the current limit,
4117            ;; then we do not display it.
4118            ((not (memq number gnus-newsgroup-limit))
4119             (setq gnus-tmp-gathered
4120                   (nconc (mapcar
4121                           (lambda (h) (mail-header-number (car h)))
4122                           (cdar thread))
4123                          gnus-tmp-gathered))
4124             (setq gnus-tmp-new-adopts (if (cdar thread)
4125                                           (append gnus-tmp-new-adopts
4126                                                   (cdar thread))
4127                                         gnus-tmp-new-adopts)
4128                   thread-end t
4129                   gnus-tmp-header nil)
4130             (when (zerop gnus-tmp-level)
4131               (setq gnus-tmp-root-expunged t)))
4132            ;; Perhaps this article is to be marked as read?
4133            ((and gnus-summary-mark-below
4134                  (< (or (cdr (assq number gnus-newsgroup-scored))
4135                         default-score)
4136                     gnus-summary-mark-below)
4137                  ;; Don't touch sparse articles.
4138                  (not (gnus-summary-article-sparse-p number))
4139                  (not (gnus-summary-article-ancient-p number)))
4140             (setq gnus-newsgroup-unreads
4141                   (delq number gnus-newsgroup-unreads))
4142             (if gnus-newsgroup-auto-expire
4143                 (push number gnus-newsgroup-expirable)
4144               (push (cons number gnus-low-score-mark)
4145                     gnus-newsgroup-reads))))
4146
4147           (when gnus-tmp-header
4148             ;; We may have an old dummy line to output before this
4149             ;; article.
4150             (when (and gnus-tmp-dummy-line
4151                        (gnus-subject-equal
4152                         gnus-tmp-dummy-line
4153                         (mail-header-subject gnus-tmp-header)))
4154               (gnus-summary-insert-dummy-line
4155                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4156               (setq gnus-tmp-dummy-line nil))
4157
4158             ;; Compute the mark.
4159             (setq gnus-tmp-unread (gnus-article-mark number))
4160
4161             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4162                                   gnus-tmp-header gnus-tmp-level)
4163                   gnus-newsgroup-data)
4164
4165             ;; Actually insert the line.
4166             (setq
4167              gnus-tmp-subject-or-nil
4168              (cond
4169               ((and gnus-thread-ignore-subject
4170                     gnus-tmp-prev-subject
4171                     (not (inline (gnus-subject-equal
4172                                   gnus-tmp-prev-subject subject))))
4173                subject)
4174               ((zerop gnus-tmp-level)
4175                (if (and (eq gnus-summary-make-false-root 'empty)
4176                         (memq number gnus-tmp-gathered)
4177                         gnus-tmp-prev-subject
4178                         (inline (gnus-subject-equal
4179                                  gnus-tmp-prev-subject subject)))
4180                    gnus-summary-same-subject
4181                  subject))
4182               (t gnus-summary-same-subject)))
4183             (if (and (eq gnus-summary-make-false-root 'adopt)
4184                      (= gnus-tmp-level 1)
4185                      (memq number gnus-tmp-gathered))
4186                 (setq gnus-tmp-opening-bracket ?\<
4187                       gnus-tmp-closing-bracket ?\>)
4188               (setq gnus-tmp-opening-bracket ?\[
4189                     gnus-tmp-closing-bracket ?\]))
4190             (setq
4191              gnus-tmp-indentation
4192              (aref gnus-thread-indent-array gnus-tmp-level)
4193              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4194              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4195                                 gnus-summary-default-score 0)
4196              gnus-tmp-score-char
4197              (if (or (null gnus-summary-default-score)
4198                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4199                          gnus-summary-zcore-fuzz))
4200                  ?  ;Whitespace
4201                (if (< gnus-tmp-score gnus-summary-default-score)
4202                    gnus-score-below-mark gnus-score-over-mark))
4203              gnus-tmp-replied
4204              (cond ((memq number gnus-newsgroup-processable)
4205                     gnus-process-mark)
4206                    ((memq number gnus-newsgroup-cached)
4207                     gnus-cached-mark)
4208                    ((memq number gnus-newsgroup-replied)
4209                     gnus-replied-mark)
4210                    ((memq number gnus-newsgroup-saved)
4211                     gnus-saved-mark)
4212                    (t gnus-no-mark))
4213              gnus-tmp-from (mail-header-from gnus-tmp-header)
4214              gnus-tmp-name
4215              (cond
4216               ((string-match "<[^>]+> *$" gnus-tmp-from)
4217                (setq beg-match (match-beginning 0))
4218                (or (and (string-match "^\".+\"" gnus-tmp-from)
4219                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4220                    (substring gnus-tmp-from 0 beg-match)))
4221               ((string-match "(.+)" gnus-tmp-from)
4222                (substring gnus-tmp-from
4223                           (1+ (match-beginning 0)) (1- (match-end 0))))
4224               (t gnus-tmp-from)))
4225             (when (string= gnus-tmp-name "")
4226               (setq gnus-tmp-name gnus-tmp-from))
4227             (unless (numberp gnus-tmp-lines)
4228               (setq gnus-tmp-lines 0))
4229             (gnus-put-text-property
4230              (point)
4231              (progn (eval gnus-summary-line-format-spec) (point))
4232              'gnus-number number)
4233             (when gnus-visual-p
4234               (forward-line -1)
4235               (gnus-run-hooks 'gnus-summary-update-hook)
4236               (forward-line 1))
4237
4238             (setq gnus-tmp-prev-subject subject)))
4239
4240         (when (nth 1 thread)
4241           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4242         (incf gnus-tmp-level)
4243         (setq threads (if thread-end nil (cdar thread)))
4244         (unless threads
4245           (setq gnus-tmp-level 0)))))
4246   (gnus-message 7 "Generating summary...done"))
4247
4248 (defun gnus-summary-prepare-unthreaded (headers)
4249   "Generate an unthreaded summary buffer based on HEADERS."
4250   (let (header number mark)
4251
4252     (beginning-of-line)
4253
4254     (while headers
4255       ;; We may have to root out some bad articles...
4256       (when (memq (setq number (mail-header-number
4257                                 (setq header (pop headers))))
4258                   gnus-newsgroup-limit)
4259         ;; Mark article as read when it has a low score.
4260         (when (and gnus-summary-mark-below
4261                    (< (or (cdr (assq number gnus-newsgroup-scored))
4262                           gnus-summary-default-score 0)
4263                       gnus-summary-mark-below)
4264                    (not (gnus-summary-article-ancient-p number)))
4265           (setq gnus-newsgroup-unreads
4266                 (delq number gnus-newsgroup-unreads))
4267           (if gnus-newsgroup-auto-expire
4268               (push number gnus-newsgroup-expirable)
4269             (push (cons number gnus-low-score-mark)
4270                   gnus-newsgroup-reads)))
4271
4272         (setq mark (gnus-article-mark number))
4273         (push (gnus-data-make number mark (1+ (point)) header 0)
4274               gnus-newsgroup-data)
4275         (gnus-summary-insert-line
4276          header 0 number
4277          mark (memq number gnus-newsgroup-replied)
4278          (memq number gnus-newsgroup-expirable)
4279          (mail-header-subject header) nil
4280          (cdr (assq number gnus-newsgroup-scored))
4281          (memq number gnus-newsgroup-processable))))))
4282
4283 (defun gnus-summary-remove-list-identifiers ()
4284   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4285   (let ((regexp (if (stringp gnus-list-identifiers)
4286                     gnus-list-identifiers
4287                   (mapconcat 'identity gnus-list-identifiers " *\\|")))
4288         changed subject)
4289     (dolist (header gnus-newsgroup-headers)
4290       (setq subject (mail-header-subject header)
4291             changed nil)
4292       (while (string-match
4293               (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4294               subject)
4295         (setq subject
4296               (concat (substring subject 0 (match-beginning 2))
4297                       (substring subject (match-end 0)))
4298               changed t))
4299       (when (and changed
4300                  (string-match
4301                    "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4302         (setq subject
4303               (concat (substring subject 0 (match-beginning 1))
4304                       (substring subject (match-end 1)))))
4305       (when changed
4306         (mail-header-set-subject header subject)))))
4307
4308 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4309   "Select newsgroup GROUP.
4310 If READ-ALL is non-nil, all articles in the group are selected.
4311 If SELECT-ARTICLES, only select those articles from GROUP."
4312   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4313          ;;!!! Dirty hack; should be removed.
4314          (gnus-summary-ignore-duplicates
4315           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4316               t
4317             gnus-summary-ignore-duplicates))
4318          (info (nth 2 entry))
4319          articles fetched-articles cached)
4320
4321     (unless (gnus-check-server
4322              (setq gnus-current-select-method
4323                    (gnus-find-method-for-group group)))
4324       (error "Couldn't open server"))
4325
4326     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4327         (gnus-activate-group group)     ; Or we can activate it...
4328         (progn                          ; Or we bug out.
4329           (when (equal major-mode 'gnus-summary-mode)
4330             (kill-buffer (current-buffer)))
4331           (error "Couldn't activate group %s: %s"
4332                  group (gnus-status-message group))))
4333
4334     (unless (gnus-request-group group t)
4335       (when (equal major-mode 'gnus-summary-mode)
4336         (kill-buffer (current-buffer)))
4337       (error "Couldn't request group %s: %s"
4338              group (gnus-status-message group)))
4339
4340     (setq gnus-newsgroup-name group)
4341     (setq gnus-newsgroup-unselected nil)
4342     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4343     (gnus-summary-setup-default-charset)
4344
4345     ;; Adjust and set lists of article marks.
4346     (when info
4347       (gnus-adjust-marked-articles info))
4348
4349     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4350     (setq cached
4351           (if (gnus-virtual-group-p group)
4352               gnus-newsgroup-cached
4353             (gnus-cache-articles-in-group group)))
4354
4355     (setq gnus-newsgroup-unreads
4356           (gnus-set-difference
4357            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4358            gnus-newsgroup-dormant))
4359
4360     (setq gnus-newsgroup-processable nil)
4361
4362     (gnus-update-read-articles group gnus-newsgroup-unreads)
4363
4364     (if (setq articles select-articles)
4365         (setq gnus-newsgroup-unselected
4366               (gnus-sorted-intersection
4367                gnus-newsgroup-unreads
4368                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4369       (setq articles (gnus-articles-to-read group read-all)))
4370
4371     (cond
4372      ((null articles)
4373       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4374       'quit)
4375      ((eq articles 0) nil)
4376      (t
4377       ;; Init the dependencies hash table.
4378       (setq gnus-newsgroup-dependencies
4379             (gnus-make-hashtable (length articles)))
4380       (gnus-set-global-variables)
4381       ;; Retrieve the headers and read them in.
4382       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4383       (setq gnus-newsgroup-headers
4384             (gnus-retrieve-parsed-headers
4385              articles gnus-newsgroup-name
4386              ;; We might want to fetch old headers, but
4387              ;; not if there is only 1 article.
4388              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4389                            (not (numberp gnus-fetch-old-headers)))
4390                       (> (length articles) 1))
4391                   gnus-fetch-old-headers)))
4392       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4393
4394       ;; Suppress duplicates?
4395       (when gnus-suppress-duplicates
4396         (gnus-dup-suppress-articles))
4397
4398       ;; Set the initial limit.
4399       (setq gnus-newsgroup-limit (copy-sequence articles))
4400       ;; Remove canceled articles from the list of unread articles.
4401       (setq gnus-newsgroup-unreads
4402             (gnus-set-sorted-intersection
4403              gnus-newsgroup-unreads
4404              (setq fetched-articles
4405                    (mapcar (lambda (headers) (mail-header-number headers))
4406                            gnus-newsgroup-headers))))
4407       ;; Removed marked articles that do not exist.
4408       (gnus-update-missing-marks
4409        (gnus-sorted-complement fetched-articles articles))
4410
4411       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4412       (when cached
4413         (setq gnus-newsgroup-cached cached))
4414
4415       ;; We might want to build some more threads first.
4416       (when (and gnus-fetch-old-headers
4417                  (eq gnus-headers-retrieved-by 'nov))
4418         (if (eq gnus-fetch-old-headers 'invisible)
4419             (gnus-build-all-threads)
4420           (gnus-build-old-threads)))
4421       ;; Let the Gnus agent mark articles as read.
4422       (when gnus-agent
4423         (gnus-agent-get-undownloaded-list))
4424       ;; Remove list identifiers from subject
4425       (when gnus-list-identifiers
4426         (gnus-summary-remove-list-identifiers))
4427       ;; Check whether auto-expire is to be done in this group.
4428       (setq gnus-newsgroup-auto-expire
4429             (gnus-group-auto-expirable-p group))
4430       ;; Set up the article buffer now, if necessary.
4431       (unless gnus-single-article-buffer
4432         (gnus-article-setup-buffer))
4433       ;; First and last article in this newsgroup.
4434       (when gnus-newsgroup-headers
4435         (setq gnus-newsgroup-begin
4436               (mail-header-number (car gnus-newsgroup-headers))
4437               gnus-newsgroup-end
4438               (mail-header-number
4439                (gnus-last-element gnus-newsgroup-headers))))
4440       ;; GROUP is successfully selected.
4441       (or gnus-newsgroup-headers t)))))
4442
4443 (defun gnus-articles-to-read (group &optional read-all)
4444   "Find out what articles the user wants to read."
4445   (let* ((articles
4446           ;; Select all articles if `read-all' is non-nil, or if there
4447           ;; are no unread articles.
4448           (if (or read-all
4449                   (and (zerop (length gnus-newsgroup-marked))
4450                        (zerop (length gnus-newsgroup-unreads)))
4451                   (eq (gnus-group-find-parameter group 'display)
4452                       'all))
4453               (or
4454                (gnus-uncompress-range (gnus-active group))
4455                (gnus-cache-articles-in-group group))
4456             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4457                           (copy-sequence gnus-newsgroup-unreads))
4458                   '<)))
4459          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4460          (scored (length scored-list))
4461          (number (length articles))
4462          (marked (+ (length gnus-newsgroup-marked)
4463                     (length gnus-newsgroup-dormant)))
4464          (select
4465           (cond
4466            ((numberp read-all)
4467             read-all)
4468            (t
4469             (condition-case ()
4470                 (cond
4471                  ((and (or (<= scored marked) (= scored number))
4472                        (natnump gnus-large-newsgroup)
4473                        (> number gnus-large-newsgroup))
4474                   (let* ((cursor-in-echo-area nil)
4475                          (input (read-from-minibuffer
4476                                  (format
4477                                   "How many articles from %s (max %d): "
4478                                   (gnus-limit-string gnus-newsgroup-name 35)
4479                                   number)
4480                                  (cons (number-to-string gnus-large-newsgroup)
4481                                        0))))
4482                     (if (string-match "^[ \t]*$" input)
4483                         number
4484                       input)))
4485                  ((and (> scored marked) (< scored number)
4486                        (> (- scored number) 20))
4487                   (let ((input
4488                          (read-string
4489                           (format "%s %s (%d scored, %d total): "
4490                                   "How many articles from"
4491                                   group scored number))))
4492                     (if (string-match "^[ \t]*$" input)
4493                         number input)))
4494                  (t number))
4495               (quit
4496                (message "Quit getting the articles to read")
4497                nil))))))
4498     (setq select (if (stringp select) (string-to-number select) select))
4499     (if (or (null select) (zerop select))
4500         select
4501       (if (and (not (zerop scored)) (<= (abs select) scored))
4502           (progn
4503             (setq articles (sort scored-list '<))
4504             (setq number (length articles)))
4505         (setq articles (copy-sequence articles)))
4506
4507       (when (< (abs select) number)
4508         (if (< select 0)
4509             ;; Select the N oldest articles.
4510             (setcdr (nthcdr (1- (abs select)) articles) nil)
4511           ;; Select the N most recent articles.
4512           (setq articles (nthcdr (- number select) articles))))
4513       (setq gnus-newsgroup-unselected
4514             (gnus-sorted-intersection
4515              gnus-newsgroup-unreads
4516              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4517       (when gnus-alter-articles-to-read-function
4518         (setq gnus-newsgroup-unreads
4519               (sort
4520                (funcall gnus-alter-articles-to-read-function
4521                         gnus-newsgroup-name gnus-newsgroup-unreads)
4522                '<)))
4523       articles)))
4524
4525 (defun gnus-killed-articles (killed articles)
4526   (let (out)
4527     (while articles
4528       (when (inline (gnus-member-of-range (car articles) killed))
4529         (push (car articles) out))
4530       (setq articles (cdr articles)))
4531     out))
4532
4533 (defun gnus-uncompress-marks (marks)
4534   "Uncompress the mark ranges in MARKS."
4535   (let ((uncompressed '(score bookmark))
4536         out)
4537     (while marks
4538       (if (memq (caar marks) uncompressed)
4539           (push (car marks) out)
4540         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4541       (setq marks (cdr marks)))
4542     out))
4543
4544 (defun gnus-adjust-marked-articles (info)
4545   "Set all article lists and remove all marks that are no longer valid."
4546   (let* ((marked-lists (gnus-info-marks info))
4547          (active (gnus-active (gnus-info-group info)))
4548          (min (car active))
4549          (max (cdr active))
4550          (types gnus-article-mark-lists)
4551          (uncompressed '(score bookmark killed))
4552          marks var articles article mark)
4553
4554     (while marked-lists
4555       (setq marks (pop marked-lists))
4556       (set (setq var (intern (format "gnus-newsgroup-%s"
4557                                      (car (rassq (setq mark (car marks))
4558                                                  types)))))
4559            (if (memq (car marks) uncompressed) (cdr marks)
4560              (gnus-uncompress-range (cdr marks))))
4561
4562       (setq articles (symbol-value var))
4563
4564       ;; All articles have to be subsets of the active articles.
4565       (cond
4566        ;; Adjust "simple" lists.
4567        ((memq mark '(tick dormant expire reply save))
4568         (while articles
4569           (when (or (< (setq article (pop articles)) min) (> article max))
4570             (set var (delq article (symbol-value var))))))
4571        ;; Adjust assocs.
4572        ((memq mark uncompressed)
4573         (when (not (listp (cdr (symbol-value var))))
4574           (set var (list (symbol-value var))))
4575         (when (not (listp (cdr articles)))
4576           (setq articles (list articles)))
4577         (while articles
4578           (when (or (not (consp (setq article (pop articles))))
4579                     (< (car article) min)
4580                     (> (car article) max))
4581             (set var (delq article (symbol-value var))))))))))
4582
4583 (defun gnus-update-missing-marks (missing)
4584   "Go through the list of MISSING articles and remove them from the mark lists."
4585   (when missing
4586     (let ((types gnus-article-mark-lists)
4587           var m)
4588       ;; Go through all types.
4589       (while types
4590         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4591         (when (symbol-value var)
4592           ;; This list has articles.  So we delete all missing articles
4593           ;; from it.
4594           (setq m missing)
4595           (while m
4596             (set var (delq (pop m) (symbol-value var)))))))))
4597
4598 (defun gnus-update-marks ()
4599   "Enter the various lists of marked articles into the newsgroup info list."
4600   (let ((types gnus-article-mark-lists)
4601         (info (gnus-get-info gnus-newsgroup-name))
4602         (uncompressed '(score bookmark killed))
4603         type list newmarked symbol delta-marks)
4604     (when info
4605       ;; Add all marks lists to the list of marks lists.
4606       (while (setq type (pop types))
4607         (setq list (symbol-value
4608                     (setq symbol
4609                           (intern (format "gnus-newsgroup-%s"
4610                                           (car type))))))
4611
4612         (when list
4613           ;; Get rid of the entries of the articles that have the
4614           ;; default score.
4615           (when (and (eq (cdr type) 'score)
4616                      gnus-save-score
4617                      list)
4618             (let* ((arts list)
4619                    (prev (cons nil list))
4620                    (all prev))
4621               (while arts
4622                 (if (or (not (consp (car arts)))
4623                         (= (cdar arts) gnus-summary-default-score))
4624                     (setcdr prev (cdr arts))
4625                   (setq prev arts))
4626                 (setq arts (cdr arts)))
4627               (setq list (cdr all)))))
4628
4629         (unless (memq (cdr type) uncompressed)
4630           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4631
4632         (when (gnus-check-backend-function
4633                'request-set-mark gnus-newsgroup-name)
4634           ;; propagate flags to server, with the following exceptions:
4635           ;; uncompressed:s are not proper flags (they are cons cells)
4636           ;; cache is a internal gnus flag
4637           ;; download are local to one gnus installation (well)
4638           ;; unsend are for nndraft groups only
4639           ;; xxx: generality of this?  this suits nnimap anyway
4640           (unless (memq (cdr type) (append '(cache download unsend)
4641                                            uncompressed))
4642             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4643                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4644                    (add (gnus-remove-from-range
4645                          (gnus-copy-sequence list) old)))
4646               (when add
4647                 (push (list add 'add (list (cdr type))) delta-marks))
4648               (when del
4649                 (push (list del 'del (list (cdr type))) delta-marks)))))
4650
4651         (when list
4652           (push (cons (cdr type) list) newmarked)))
4653
4654       (when delta-marks
4655         (unless (gnus-check-group gnus-newsgroup-name)
4656           (error "Can't open server for %s" gnus-newsgroup-name))
4657         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4658
4659       ;; Enter these new marks into the info of the group.
4660       (if (nthcdr 3 info)
4661           (setcar (nthcdr 3 info) newmarked)
4662         ;; Add the marks lists to the end of the info.
4663         (when newmarked
4664           (setcdr (nthcdr 2 info) (list newmarked))))
4665
4666       ;; Cut off the end of the info if there's nothing else there.
4667       (let ((i 5))
4668         (while (and (> i 2)
4669                     (not (nth i info)))
4670           (when (nthcdr (decf i) info)
4671             (setcdr (nthcdr i info) nil)))))))
4672
4673 (defun gnus-set-mode-line (where)
4674   "Set the mode line of the article or summary buffers.
4675 If WHERE is `summary', the summary mode line format will be used."
4676   ;; Is this mode line one we keep updated?
4677   (when (and (memq where gnus-updated-mode-lines)
4678              (symbol-value
4679               (intern (format "gnus-%s-mode-line-format-spec" where))))
4680     (let (mode-string)
4681       (save-excursion
4682         ;; We evaluate this in the summary buffer since these
4683         ;; variables are buffer-local to that buffer.
4684         (set-buffer gnus-summary-buffer)
4685         ;; We bind all these variables that are used in the `eval' form
4686         ;; below.
4687         (let* ((mformat (symbol-value
4688                          (intern
4689                           (format "gnus-%s-mode-line-format-spec" where))))
4690                (gnus-tmp-group-name (gnus-group-name-decode
4691                                      gnus-newsgroup-name
4692                                      (gnus-group-name-charset
4693                                       nil
4694                                       gnus-newsgroup-name)))
4695                (gnus-tmp-article-number (or gnus-current-article 0))
4696                (gnus-tmp-unread gnus-newsgroup-unreads)
4697                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4698                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4699                (gnus-tmp-unread-and-unselected
4700                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4701                             (zerop gnus-tmp-unselected))
4702                        "")
4703                       ((zerop gnus-tmp-unselected)
4704                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4705                       (t (format "{%d(+%d) more}"
4706                                  gnus-tmp-unread-and-unticked
4707                                  gnus-tmp-unselected))))
4708                (gnus-tmp-subject
4709                 (if (and gnus-current-headers
4710                          (vectorp gnus-current-headers))
4711                     (gnus-mode-string-quote
4712                      (mail-header-subject gnus-current-headers))
4713                   ""))
4714                bufname-length max-len
4715                gnus-tmp-header);; passed as argument to any user-format-funcs
4716           (setq mode-string (eval mformat))
4717           (setq bufname-length (if (string-match "%b" mode-string)
4718                                    (- (length
4719                                        (buffer-name
4720                                         (if (eq where 'summary)
4721                                             nil
4722                                           (get-buffer gnus-article-buffer))))
4723                                       2)
4724                                  0))
4725           (setq max-len (max 4 (if gnus-mode-non-string-length
4726                                    (- (window-width)
4727                                       gnus-mode-non-string-length
4728                                       bufname-length)
4729                                  (length mode-string))))
4730           ;; We might have to chop a bit of the string off...
4731           (when (> (length mode-string) max-len)
4732             (setq mode-string
4733                   (concat (gnus-truncate-string mode-string (- max-len 3))
4734                           "...")))
4735           ;; Pad the mode string a bit.
4736           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4737       ;; Update the mode line.
4738       (setq mode-line-buffer-identification
4739             (gnus-mode-line-buffer-identification (list mode-string)))
4740       (set-buffer-modified-p t))))
4741
4742 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4743   "Go through the HEADERS list and add all Xrefs to a hash table.
4744 The resulting hash table is returned, or nil if no Xrefs were found."
4745   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4746          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4747          (xref-hashtb (gnus-make-hashtable))
4748          start group entry number xrefs header)
4749     (while headers
4750       (setq header (pop headers))
4751       (when (and (setq xrefs (mail-header-xref header))
4752                  (not (memq (setq number (mail-header-number header))
4753                             unreads)))
4754         (setq start 0)
4755         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4756           (setq start (match-end 0))
4757           (setq group (if prefix
4758                           (concat prefix (substring xrefs (match-beginning 1)
4759                                                     (match-end 1)))
4760                         (substring xrefs (match-beginning 1) (match-end 1))))
4761           (setq number
4762                 (string-to-int (substring xrefs (match-beginning 2)
4763                                           (match-end 2))))
4764           (if (setq entry (gnus-gethash group xref-hashtb))
4765               (setcdr entry (cons number (cdr entry)))
4766             (gnus-sethash group (cons number nil) xref-hashtb)))))
4767     (and start xref-hashtb)))
4768
4769 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4770   "Look through all the headers and mark the Xrefs as read."
4771   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4772         name entry info xref-hashtb idlist method nth4)
4773     (save-excursion
4774       (set-buffer gnus-group-buffer)
4775       (when (setq xref-hashtb
4776                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4777         (mapatoms
4778          (lambda (group)
4779            (unless (string= from-newsgroup (setq name (symbol-name group)))
4780              (setq idlist (symbol-value group))
4781              ;; Dead groups are not updated.
4782              (and (prog1
4783                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4784                             info (nth 2 entry))
4785                     (when (stringp (setq nth4 (gnus-info-method info)))
4786                       (setq nth4 (gnus-server-to-method nth4))))
4787                   ;; Only do the xrefs if the group has the same
4788                   ;; select method as the group we have just read.
4789                   (or (gnus-methods-equal-p
4790                        nth4 (gnus-find-method-for-group from-newsgroup))
4791                       virtual
4792                       (equal nth4 (setq method (gnus-find-method-for-group
4793                                                 from-newsgroup)))
4794                       (and (equal (car nth4) (car method))
4795                            (equal (nth 1 nth4) (nth 1 method))))
4796                   gnus-use-cross-reference
4797                   (or (not (eq gnus-use-cross-reference t))
4798                       virtual
4799                       ;; Only do cross-references on subscribed
4800                       ;; groups, if that is what is wanted.
4801                       (<= (gnus-info-level info) gnus-level-subscribed))
4802                   (gnus-group-make-articles-read name idlist))))
4803          xref-hashtb)))))
4804
4805 (defun gnus-compute-read-articles (group articles)
4806   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4807          (info (nth 2 entry))
4808          (active (gnus-active group))
4809          ninfo)
4810     (when entry
4811       ;; First peel off all invalid article numbers.
4812       (when active
4813         (let ((ids articles)
4814               id first)
4815           (while (setq id (pop ids))
4816             (when (and first (> id (cdr active)))
4817               ;; We'll end up in this situation in one particular
4818               ;; obscure situation.  If you re-scan a group and get
4819               ;; a new article that is cross-posted to a different
4820               ;; group that has not been re-scanned, you might get
4821               ;; crossposted article that has a higher number than
4822               ;; Gnus believes possible.  So we re-activate this
4823               ;; group as well.  This might mean doing the
4824               ;; crossposting thingy will *increase* the number
4825               ;; of articles in some groups.  Tsk, tsk.
4826               (setq active (or (gnus-activate-group group) active)))
4827             (when (or (> id (cdr active))
4828                       (< id (car active)))
4829               (setq articles (delq id articles))))))
4830       ;; If the read list is nil, we init it.
4831       (if (and active
4832                (null (gnus-info-read info))
4833                (> (car active) 1))
4834           (setq ninfo (cons 1 (1- (car active))))
4835         (setq ninfo (gnus-info-read info)))
4836       ;; Then we add the read articles to the range.
4837       (gnus-add-to-range
4838        ninfo (setq articles (sort articles '<))))))
4839
4840 (defun gnus-group-make-articles-read (group articles)
4841   "Update the info of GROUP to say that ARTICLES are read."
4842   (let* ((num 0)
4843          (entry (gnus-gethash group gnus-newsrc-hashtb))
4844          (info (nth 2 entry))
4845          (active (gnus-active group))
4846          range)
4847     (when entry
4848       (setq range (gnus-compute-read-articles group articles))
4849       (save-excursion
4850         (set-buffer gnus-group-buffer)
4851         (gnus-undo-register
4852           `(progn
4853              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4854              (gnus-info-set-read ',info ',(gnus-info-read info))
4855              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4856              (gnus-group-update-group ,group t))))
4857       ;; Add the read articles to the range.
4858       (gnus-info-set-read info range)
4859       ;; Then we have to re-compute how many unread
4860       ;; articles there are in this group.
4861       (when active
4862         (cond
4863          ((not range)
4864           (setq num (- (1+ (cdr active)) (car active))))
4865          ((not (listp (cdr range)))
4866           (setq num (- (cdr active) (- (1+ (cdr range))
4867                                        (car range)))))
4868          (t
4869           (while range
4870             (if (numberp (car range))
4871                 (setq num (1+ num))
4872               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4873             (setq range (cdr range)))
4874           (setq num (- (cdr active) num))))
4875         ;; Update the number of unread articles.
4876         (setcar entry num)
4877         ;; Update the group buffer.
4878         (gnus-group-update-group group t)))))
4879
4880 (defvar gnus-newsgroup-none-id 0)
4881
4882 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4883   (let ((cur nntp-server-buffer)
4884         (dependencies
4885          (or dependencies
4886              (save-excursion (set-buffer gnus-summary-buffer)
4887                              gnus-newsgroup-dependencies)))
4888         headers id end ref
4889         (mail-parse-charset gnus-newsgroup-charset)
4890         (mail-parse-ignored-charsets
4891          (save-excursion (condition-case nil
4892                              (set-buffer gnus-summary-buffer)
4893                            (error))
4894                          gnus-newsgroup-ignored-charsets)))
4895     (save-excursion
4896       (set-buffer nntp-server-buffer)
4897       ;; Translate all TAB characters into SPACE characters.
4898       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4899       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4900       (gnus-run-hooks 'gnus-parse-headers-hook)
4901       (let ((case-fold-search t)
4902             in-reply-to header p lines chars ctype)
4903         (goto-char (point-min))
4904         ;; Search to the beginning of the next header.  Error messages
4905         ;; do not begin with 2 or 3.
4906         (while (re-search-forward "^[23][0-9]+ " nil t)
4907           (setq id nil
4908                 ref nil)
4909           ;; This implementation of this function, with nine
4910           ;; search-forwards instead of the one re-search-forward and
4911           ;; a case (which basically was the old function) is actually
4912           ;; about twice as fast, even though it looks messier.  You
4913           ;; can't have everything, I guess.  Speed and elegance
4914           ;; doesn't always go hand in hand.
4915           (setq
4916            header
4917            (make-full-mail-header
4918             ;; Number.
4919             (prog1
4920                 (read cur)
4921               (end-of-line)
4922               (setq p (point))
4923               (narrow-to-region (point)
4924                                 (or (and (search-forward "\n.\n" nil t)
4925                                          (- (point) 2))
4926                                     (point))))
4927             ;; Subject.
4928             (progn
4929               (goto-char p)
4930               (if (search-forward "\nsubject: " nil t)
4931                   (nnheader-header-value)
4932                 "(none)"))
4933             ;; From.
4934             (progn
4935               (goto-char p)
4936               (if (or (search-forward "\nfrom: " nil t)
4937                       (search-forward "\nfrom:" nil t))
4938                   (nnheader-header-value)
4939                 "(nobody)"))
4940             ;; Date.
4941             (progn
4942               (goto-char p)
4943               (if (search-forward "\ndate: " nil t)
4944                   (nnheader-header-value)
4945                 ""))
4946             ;; Message-ID.
4947             (progn
4948               (goto-char p)
4949               (setq id (if (re-search-forward
4950                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4951                            ;; We do it this way to make sure the Message-ID
4952                            ;; is (somewhat) syntactically valid.
4953                            (buffer-substring (match-beginning 1)
4954                                              (match-end 1))
4955                          ;; If there was no message-id, we just fake one
4956                          ;; to make subsequent routines simpler.
4957                          (nnheader-generate-fake-message-id))))
4958             ;; References.
4959             (progn
4960               (goto-char p)
4961               (if (search-forward "\nreferences: " nil t)
4962                   (progn
4963                     (setq end (point))
4964                     (prog1
4965                         (nnheader-header-value)
4966                       (setq ref
4967                             (buffer-substring
4968                              (progn
4969                                ;; (end-of-line)
4970                                (search-backward ">" end t)
4971                                (1+ (point)))
4972                              (progn
4973                                (search-backward "<" end t)
4974                                (point))))))
4975                 ;; Get the references from the in-reply-to header if there
4976                 ;; were no references and the in-reply-to header looks
4977                 ;; promising.
4978                 (if (and (search-forward "\nin-reply-to: " nil t)
4979                          (setq in-reply-to (nnheader-header-value))
4980                          (string-match "<[^>]+>" in-reply-to))
4981                     (let (ref2)
4982                       (setq ref (substring in-reply-to (match-beginning 0)
4983                                            (match-end 0)))
4984                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4985                         (setq ref2 (substring in-reply-to (match-beginning 0)
4986                                               (match-end 0)))
4987                         (when (> (length ref2) (length ref))
4988                           (setq ref ref2)))
4989                       ref)
4990                   (setq ref nil))))
4991             ;; Chars.
4992             (progn
4993               (goto-char p)
4994               (if (search-forward "\nchars: " nil t)
4995                   (if (numberp (setq chars (ignore-errors (read cur))))
4996                       chars 0)
4997                 0))
4998             ;; Lines.
4999             (progn
5000               (goto-char p)
5001               (if (search-forward "\nlines: " nil t)
5002                   (if (numberp (setq lines (ignore-errors (read cur))))
5003                       lines 0)
5004                 0))
5005             ;; Xref.
5006             (progn
5007               (goto-char p)
5008               (and (search-forward "\nxref: " nil t)
5009                    (nnheader-header-value)))
5010             ;; Extra.
5011             (when gnus-extra-headers
5012               (let ((extra gnus-extra-headers)
5013                     out)
5014                 (while extra
5015                   (goto-char p)
5016                   (when (search-forward
5017                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5018                     (push (cons (car extra) (nnheader-header-value)) out))
5019                   (pop extra))
5020                 out))))
5021           (goto-char p)
5022           (if (and (search-forward "\ncontent-type: " nil t)
5023                    (setq ctype (nnheader-header-value)))
5024               (mime-entity-set-content-type-internal
5025                header (mime-parse-Content-Type ctype)))
5026           (when (equal id ref)
5027             (setq ref nil))
5028
5029           (when gnus-alter-header-function
5030             (funcall gnus-alter-header-function header)
5031             (setq id (mail-header-id header)
5032                   ref (gnus-parent-id (mail-header-references header))))
5033
5034           (when (setq header
5035                       (gnus-dependencies-add-header
5036                        header dependencies force-new))
5037             (push header headers))
5038           (goto-char (point-max))
5039           (widen))
5040         (nreverse headers)))))
5041
5042 ;; Goes through the xover lines and returns a list of vectors
5043 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5044                                                   force-new dependencies
5045                                                   group also-fetch-heads)
5046   "Parse the news overview data in the server buffer.
5047 Return a list of headers that match SEQUENCE (see
5048 `nntp-retrieve-headers')."
5049   ;; Get the Xref when the users reads the articles since most/some
5050   ;; NNTP servers do not include Xrefs when using XOVER.
5051   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5052   (let ((mail-parse-charset gnus-newsgroup-charset)
5053         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5054         (cur nntp-server-buffer)
5055         (dependencies (or dependencies gnus-newsgroup-dependencies))
5056         number headers header)
5057     (save-excursion
5058       (set-buffer nntp-server-buffer)
5059       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5060       ;; Allow the user to mangle the headers before parsing them.
5061       (gnus-run-hooks 'gnus-parse-headers-hook)
5062       (goto-char (point-min))
5063       (while (not (eobp))
5064         (condition-case ()
5065             (while (and sequence (not (eobp)))
5066               (setq number (read cur))
5067               (while (and sequence
5068                           (< (car sequence) number))
5069                 (setq sequence (cdr sequence)))
5070               (and sequence
5071                    (eq number (car sequence))
5072                    (progn
5073                      (setq sequence (cdr sequence))
5074                      (setq header (inline
5075                                     (gnus-nov-parse-line
5076                                      number dependencies force-new))))
5077                    (push header headers))
5078               (forward-line 1))
5079           (error
5080            (gnus-error 4 "Strange nov line (%d)"
5081                        (count-lines (point-min) (point)))))
5082         (forward-line 1))
5083       ;; A common bug in inn is that if you have posted an article and
5084       ;; then retrieves the active file, it will answer correctly --
5085       ;; the new article is included.  However, a NOV entry for the
5086       ;; article may not have been generated yet, so this may fail.
5087       ;; We work around this problem by retrieving the last few
5088       ;; headers using HEAD.
5089       (if (or (not also-fetch-heads)
5090               (not sequence))
5091           ;; We (probably) got all the headers.
5092           (nreverse headers)
5093         (let ((gnus-nov-is-evil t))
5094           (nconc
5095            (nreverse headers)
5096            (gnus-retrieve-parsed-headers sequence group)
5097            ))))))
5098
5099 (defun gnus-article-get-xrefs ()
5100   "Fill in the Xref value in `gnus-current-headers', if necessary.
5101 This is meant to be called in `gnus-article-internal-prepare-hook'."
5102   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5103                                  gnus-current-headers)))
5104     (or (not gnus-use-cross-reference)
5105         (not headers)
5106         (and (mail-header-xref headers)
5107              (not (string= (mail-header-xref headers) "")))
5108         (let ((case-fold-search t)
5109               xref)
5110           (save-restriction
5111             (nnheader-narrow-to-headers)
5112             (goto-char (point-min))
5113             (when (or (and (not (eobp))
5114                            (eq (downcase (char-after)) ?x)
5115                            (looking-at "Xref:"))
5116                       (search-forward "\nXref:" nil t))
5117               (goto-char (1+ (match-end 0)))
5118               (setq xref (buffer-substring (point)
5119                                            (progn (end-of-line) (point))))
5120               (mail-header-set-xref headers xref)))))))
5121
5122 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5123   "Find article ID and insert the summary line for that article.
5124 OLD-HEADER can either be a header or a line number to insert
5125 the subject line on."
5126   (let* ((line (and (numberp old-header) old-header))
5127          (old-header (and (vectorp old-header) old-header))
5128          (header (cond ((and old-header use-old-header)
5129                         old-header)
5130                        ((and (numberp id)
5131                              (gnus-number-to-header id))
5132                         (gnus-number-to-header id))
5133                        (t
5134                         (gnus-read-header id))))
5135          (number (and (numberp id) id))
5136          d)
5137     (when header
5138       ;; Rebuild the thread that this article is part of and go to the
5139       ;; article we have fetched.
5140       (when (and (not gnus-show-threads)
5141                  old-header)
5142         (when (and number
5143                    (setq d (gnus-data-find (mail-header-number old-header))))
5144           (goto-char (gnus-data-pos d))
5145           (gnus-data-remove
5146            number
5147            (- (gnus-point-at-bol)
5148               (prog1
5149                   (1+ (gnus-point-at-eol))
5150                 (gnus-delete-line))))))
5151       (when old-header
5152         (mail-header-set-number header (mail-header-number old-header)))
5153       (setq gnus-newsgroup-sparse
5154             (delq (setq number (mail-header-number header))
5155                   gnus-newsgroup-sparse))
5156       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5157       (push number gnus-newsgroup-limit)
5158       (gnus-rebuild-thread (mail-header-id header) line)
5159       (gnus-summary-goto-subject number nil t))
5160     (when (and (numberp number)
5161                (> number 0))
5162       ;; We have to update the boundaries even if we can't fetch the
5163       ;; article if ID is a number -- so that the next `P' or `N'
5164       ;; command will fetch the previous (or next) article even
5165       ;; if the one we tried to fetch this time has been canceled.
5166       (when (> number gnus-newsgroup-end)
5167         (setq gnus-newsgroup-end number))
5168       (when (< number gnus-newsgroup-begin)
5169         (setq gnus-newsgroup-begin number))
5170       (setq gnus-newsgroup-unselected
5171             (delq number gnus-newsgroup-unselected)))
5172     ;; Report back a success?
5173     (and header (mail-header-number header))))
5174
5175 ;;; Process/prefix in the summary buffer
5176
5177 (defun gnus-summary-work-articles (n)
5178   "Return a list of articles to be worked upon.
5179 The prefix argument, the list of process marked articles, and the
5180 current article will be taken into consideration."
5181   (save-excursion
5182     (set-buffer gnus-summary-buffer)
5183     (cond
5184      (n
5185       ;; A numerical prefix has been given.
5186       (setq n (prefix-numeric-value n))
5187       (let ((backward (< n 0))
5188             (n (abs (prefix-numeric-value n)))
5189             articles article)
5190         (save-excursion
5191           (while
5192               (and (> n 0)
5193                    (push (setq article (gnus-summary-article-number))
5194                          articles)
5195                    (if backward
5196                        (gnus-summary-find-prev nil article)
5197                      (gnus-summary-find-next nil article)))
5198             (decf n)))
5199         (nreverse articles)))
5200      ((and (gnus-region-active-p) (mark))
5201       (message "region active")
5202       ;; Work on the region between point and mark.
5203       (let ((max (max (point) (mark)))
5204             articles article)
5205         (save-excursion
5206           (goto-char (min (point) (mark)))
5207           (while
5208               (and
5209                (push (setq article (gnus-summary-article-number)) articles)
5210                (gnus-summary-find-next nil article)
5211                (< (point) max)))
5212           (nreverse articles))))
5213      (gnus-newsgroup-processable
5214       ;; There are process-marked articles present.
5215       ;; Save current state.
5216       (gnus-summary-save-process-mark)
5217       ;; Return the list.
5218       (reverse gnus-newsgroup-processable))
5219      (t
5220       ;; Just return the current article.
5221       (list (gnus-summary-article-number))))))
5222
5223 (defmacro gnus-summary-iterate (arg &rest forms)
5224   "Iterate over the process/prefixed articles and do FORMS.
5225 ARG is the interactive prefix given to the command.  FORMS will be
5226 executed with point over the summary line of the articles."
5227   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5228     `(let ((,articles (gnus-summary-work-articles ,arg)))
5229        (while ,articles
5230          (gnus-summary-goto-subject (car ,articles))
5231          ,@forms
5232          (pop ,articles)))))
5233
5234 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5235 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5236
5237 (defun gnus-summary-save-process-mark ()
5238   "Push the current set of process marked articles on the stack."
5239   (interactive)
5240   (push (copy-sequence gnus-newsgroup-processable)
5241         gnus-newsgroup-process-stack))
5242
5243 (defun gnus-summary-kill-process-mark ()
5244   "Push the current set of process marked articles on the stack and unmark."
5245   (interactive)
5246   (gnus-summary-save-process-mark)
5247   (gnus-summary-unmark-all-processable))
5248
5249 (defun gnus-summary-yank-process-mark ()
5250   "Pop the last process mark state off the stack and restore it."
5251   (interactive)
5252   (unless gnus-newsgroup-process-stack
5253     (error "Empty mark stack"))
5254   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5255
5256 (defun gnus-summary-process-mark-set (set)
5257   "Make SET into the current process marked articles."
5258   (gnus-summary-unmark-all-processable)
5259   (while set
5260     (gnus-summary-set-process-mark (pop set))))
5261
5262 ;;; Searching and stuff
5263
5264 (defun gnus-summary-search-group (&optional backward use-level)
5265   "Search for next unread newsgroup.
5266 If optional argument BACKWARD is non-nil, search backward instead."
5267   (save-excursion
5268     (set-buffer gnus-group-buffer)
5269     (when (gnus-group-search-forward
5270            backward nil (if use-level (gnus-group-group-level) nil))
5271       (gnus-group-group-name))))
5272
5273 (defun gnus-summary-best-group (&optional exclude-group)
5274   "Find the name of the best unread group.
5275 If EXCLUDE-GROUP, do not go to this group."
5276   (save-excursion
5277     (set-buffer gnus-group-buffer)
5278     (save-excursion
5279       (gnus-group-best-unread-group exclude-group))))
5280
5281 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5282   (if backward (gnus-summary-find-prev)
5283     (let* ((dummy (gnus-summary-article-intangible-p))
5284            (article (or article (gnus-summary-article-number)))
5285            (arts (gnus-data-find-list article))
5286            result)
5287       (when (and (not dummy)
5288                  (or (not gnus-summary-check-current)
5289                      (not unread)
5290                      (not (gnus-data-unread-p (car arts)))))
5291         (setq arts (cdr arts)))
5292       (when (setq result
5293                   (if unread
5294                       (progn
5295                         (while arts
5296                           (when (or (and undownloaded
5297                                          (eq gnus-undownloaded-mark
5298                                              (gnus-data-mark (car arts))))
5299                                     (gnus-data-unread-p (car arts)))
5300                             (setq result (car arts)
5301                                   arts nil))
5302                           (setq arts (cdr arts)))
5303                         result)
5304                     (car arts)))
5305         (goto-char (gnus-data-pos result))
5306         (gnus-data-number result)))))
5307
5308 (defun gnus-summary-find-prev (&optional unread article)
5309   (let* ((eobp (eobp))
5310          (article (or article (gnus-summary-article-number)))
5311          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5312          result)
5313     (when (and (not eobp)
5314                (or (not gnus-summary-check-current)
5315                    (not unread)
5316                    (not (gnus-data-unread-p (car arts)))))
5317       (setq arts (cdr arts)))
5318     (when (setq result
5319                 (if unread
5320                     (progn
5321                       (while arts
5322                         (when (gnus-data-unread-p (car arts))
5323                           (setq result (car arts)
5324                                 arts nil))
5325                         (setq arts (cdr arts)))
5326                       result)
5327                   (car arts)))
5328       (goto-char (gnus-data-pos result))
5329       (gnus-data-number result))))
5330
5331 (defun gnus-summary-find-subject (subject &optional unread backward article)
5332   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5333          (article (or article (gnus-summary-article-number)))
5334          (articles (gnus-data-list backward))
5335          (arts (gnus-data-find-list article articles))
5336          result)
5337     (when (or (not gnus-summary-check-current)
5338               (not unread)
5339               (not (gnus-data-unread-p (car arts))))
5340       (setq arts (cdr arts)))
5341     (while arts
5342       (and (or (not unread)
5343                (gnus-data-unread-p (car arts)))
5344            (vectorp (gnus-data-header (car arts)))
5345            (gnus-subject-equal
5346             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5347            (setq result (car arts)
5348                  arts nil))
5349       (setq arts (cdr arts)))
5350     (and result
5351          (goto-char (gnus-data-pos result))
5352          (gnus-data-number result))))
5353
5354 (defun gnus-summary-search-forward (&optional unread subject backward)
5355   "Search forward for an article.
5356 If UNREAD, look for unread articles.  If SUBJECT, look for
5357 articles with that subject.  If BACKWARD, search backward instead."
5358   (cond (subject (gnus-summary-find-subject subject unread backward))
5359         (backward (gnus-summary-find-prev unread))
5360         (t (gnus-summary-find-next unread))))
5361
5362 (defun gnus-recenter (&optional n)
5363   "Center point in window and redisplay frame.
5364 Also do horizontal recentering."
5365   (interactive "P")
5366   (when (and gnus-auto-center-summary
5367              (not (eq gnus-auto-center-summary 'vertical)))
5368     (gnus-horizontal-recenter))
5369   (recenter n))
5370
5371 (defun gnus-summary-recenter ()
5372   "Center point in the summary window.
5373 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5374 displayed, no centering will be performed."
5375   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5376   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5377   (interactive)
5378   (let* ((top (cond ((< (window-height) 4) 0)
5379                     ((< (window-height) 7) 1)
5380                     (t (if (numberp gnus-auto-center-summary)
5381                            gnus-auto-center-summary
5382                          2))))
5383          (height (1- (window-height)))
5384          (bottom (save-excursion (goto-char (point-max))
5385                                  (forward-line (- height))
5386                                  (point)))
5387          (window (get-buffer-window (current-buffer))))
5388     ;; The user has to want it.
5389     (when gnus-auto-center-summary
5390       (when (get-buffer-window gnus-article-buffer)
5391         ;; Only do recentering when the article buffer is displayed,
5392         ;; Set the window start to either `bottom', which is the biggest
5393         ;; possible valid number, or the second line from the top,
5394         ;; whichever is the least.
5395         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5396           (if (> bottom top-pos)
5397               ;; Keep the second line from the top visible
5398               (set-window-start window top-pos t)
5399             ;; Try to keep the bottom line visible; if it's partially
5400             ;; obscured, either scroll one more line to make it fully
5401             ;; visible, or revert to using TOP-POS.
5402             (save-excursion
5403               (goto-char (point-max))
5404               (forward-line -1)
5405               (let ((last-line-start (point)))
5406                 (goto-char bottom)
5407                 (set-window-start window (point) t)
5408                 (when (not (pos-visible-in-window-p last-line-start window))
5409                   (forward-line 1)
5410                   (set-window-start window (min (point) top-pos) t)))))))
5411       ;; Do horizontal recentering while we're at it.
5412       (when (and (get-buffer-window (current-buffer) t)
5413                  (not (eq gnus-auto-center-summary 'vertical)))
5414         (let ((selected (selected-window)))
5415           (select-window (get-buffer-window (current-buffer) t))
5416           (gnus-summary-position-point)
5417           (gnus-horizontal-recenter)
5418           (select-window selected))))))
5419
5420 (defun gnus-summary-jump-to-group (newsgroup)
5421   "Move point to NEWSGROUP in group mode buffer."
5422   ;; Keep update point of group mode buffer if visible.
5423   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5424       (save-window-excursion
5425         ;; Take care of tree window mode.
5426         (when (get-buffer-window gnus-group-buffer)
5427           (pop-to-buffer gnus-group-buffer))
5428         (gnus-group-jump-to-group newsgroup))
5429     (save-excursion
5430       ;; Take care of tree window mode.
5431       (if (get-buffer-window gnus-group-buffer)
5432           (pop-to-buffer gnus-group-buffer)
5433         (set-buffer gnus-group-buffer))
5434       (gnus-group-jump-to-group newsgroup))))
5435
5436 ;; This function returns a list of article numbers based on the
5437 ;; difference between the ranges of read articles in this group and
5438 ;; the range of active articles.
5439 (defun gnus-list-of-unread-articles (group)
5440   (let* ((read (gnus-info-read (gnus-get-info group)))
5441          (active (or (gnus-active group) (gnus-activate-group group)))
5442          (last (cdr active))
5443          first nlast unread)
5444     ;; If none are read, then all are unread.
5445     (if (not read)
5446         (setq first (car active))
5447       ;; If the range of read articles is a single range, then the
5448       ;; first unread article is the article after the last read
5449       ;; article.  Sounds logical, doesn't it?
5450       (if (and (not (listp (cdr read)))
5451                (or (< (car read) (car active))
5452                    (progn (setq read (list read))
5453                           nil)))
5454           (setq first (max (car active) (1+ (cdr read))))
5455         ;; `read' is a list of ranges.
5456         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5457                                   (caar read)))
5458                   1)
5459           (setq first (car active)))
5460         (while read
5461           (when first
5462             (while (< first nlast)
5463               (push first unread)
5464               (setq first (1+ first))))
5465           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5466           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5467           (setq read (cdr read)))))
5468     ;; And add the last unread articles.
5469     (while (<= first last)
5470       (push first unread)
5471       (setq first (1+ first)))
5472     ;; Return the list of unread articles.
5473     (delq 0 (nreverse unread))))
5474
5475 (defun gnus-list-of-read-articles (group)
5476   "Return a list of unread, unticked and non-dormant articles."
5477   (let* ((info (gnus-get-info group))
5478          (marked (gnus-info-marks info))
5479          (active (gnus-active group)))
5480     (and info active
5481          (gnus-set-difference
5482           (gnus-sorted-complement
5483            (gnus-uncompress-range active)
5484            (gnus-list-of-unread-articles group))
5485           (append
5486            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5487            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5488
5489 ;; Various summary commands
5490
5491 (defun gnus-summary-select-article-buffer ()
5492   "Reconfigure windows to show article buffer."
5493   (interactive)
5494   (if (not (gnus-buffer-live-p gnus-article-buffer))
5495       (error "There is no article buffer for this summary buffer")
5496     (gnus-configure-windows 'article)
5497     (select-window (get-buffer-window gnus-article-buffer))))
5498
5499 (defun gnus-summary-universal-argument (arg)
5500   "Perform any operation on all articles that are process/prefixed."
5501   (interactive "P")
5502   (let ((articles (gnus-summary-work-articles arg))
5503         func article)
5504     (if (eq
5505          (setq
5506           func
5507           (key-binding
5508            (read-key-sequence
5509             (substitute-command-keys
5510              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5511          'undefined)
5512         (gnus-error 1 "Undefined key")
5513       (save-excursion
5514         (while articles
5515           (gnus-summary-goto-subject (setq article (pop articles)))
5516           (let (gnus-newsgroup-processable)
5517             (command-execute func))
5518           (gnus-summary-remove-process-mark article)))))
5519   (gnus-summary-position-point))
5520
5521 (defun gnus-summary-toggle-truncation (&optional arg)
5522   "Toggle truncation of summary lines.
5523 With arg, turn line truncation on iff arg is positive."
5524   (interactive "P")
5525   (setq truncate-lines
5526         (if (null arg) (not truncate-lines)
5527           (> (prefix-numeric-value arg) 0)))
5528   (redraw-display))
5529
5530 (defun gnus-summary-reselect-current-group (&optional all rescan)
5531   "Exit and then reselect the current newsgroup.
5532 The prefix argument ALL means to select all articles."
5533   (interactive "P")
5534   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5535     (error "Ephemeral groups can't be reselected"))
5536   (let ((current-subject (gnus-summary-article-number))
5537         (group gnus-newsgroup-name))
5538     (setq gnus-newsgroup-begin nil)
5539     (gnus-summary-exit)
5540     ;; We have to adjust the point of group mode buffer because
5541     ;; point was moved to the next unread newsgroup by exiting.
5542     (gnus-summary-jump-to-group group)
5543     (when rescan
5544       (save-excursion
5545         (save-window-excursion
5546           ;; Don't show group contents.
5547           (set-window-start (selected-window) (point-max))
5548           (gnus-group-get-new-news-this-group 1))))
5549     (gnus-group-read-group all t)
5550     (gnus-summary-goto-subject current-subject nil t)))
5551
5552 (defun gnus-summary-rescan-group (&optional all)
5553   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5554   (interactive "P")
5555   (gnus-summary-reselect-current-group all t))
5556
5557 (defun gnus-summary-update-info (&optional non-destructive)
5558   (save-excursion
5559     (let ((group gnus-newsgroup-name))
5560       (when group
5561         (when gnus-newsgroup-kill-headers
5562           (setq gnus-newsgroup-killed
5563                 (gnus-compress-sequence
5564                  (nconc
5565                   (gnus-set-sorted-intersection
5566                    (gnus-uncompress-range gnus-newsgroup-killed)
5567                    (setq gnus-newsgroup-unselected
5568                          (sort gnus-newsgroup-unselected '<)))
5569                   (setq gnus-newsgroup-unreads
5570                         (sort gnus-newsgroup-unreads '<)))
5571                  t)))
5572         (unless (listp (cdr gnus-newsgroup-killed))
5573           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5574         (let ((headers gnus-newsgroup-headers))
5575           ;; Set the new ranges of read articles.
5576           (save-excursion
5577             (set-buffer gnus-group-buffer)
5578             (gnus-undo-force-boundary))
5579           (gnus-update-read-articles
5580            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5581           ;; Set the current article marks.
5582           (let ((gnus-newsgroup-scored
5583                  (if (and (not gnus-save-score)
5584                           (not non-destructive))
5585                      nil
5586                    gnus-newsgroup-scored)))
5587             (save-excursion
5588               (gnus-update-marks)))
5589           ;; Do the cross-ref thing.
5590           (when gnus-use-cross-reference
5591             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5592           ;; Do not switch windows but change the buffer to work.
5593           (set-buffer gnus-group-buffer)
5594           (unless (gnus-ephemeral-group-p group)
5595             (gnus-group-update-group group)))))))
5596
5597 (defun gnus-summary-save-newsrc (&optional force)
5598   "Save the current number of read/marked articles in the dribble buffer.
5599 The dribble buffer will then be saved.
5600 If FORCE (the prefix), also save the .newsrc file(s)."
5601   (interactive "P")
5602   (gnus-summary-update-info t)
5603   (if force
5604       (gnus-save-newsrc-file)
5605     (gnus-dribble-save)))
5606
5607 (defun gnus-summary-exit (&optional temporary)
5608   "Exit reading current newsgroup, and then return to group selection mode.
5609 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5610   (interactive)
5611   (gnus-set-global-variables)
5612   (gnus-kill-save-kill-buffer)
5613   (gnus-async-halt-prefetch)
5614   (let* ((group gnus-newsgroup-name)
5615          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5616          (mode major-mode)
5617          (group-point nil)
5618          (buf (current-buffer)))
5619     (unless quit-config
5620       ;; Do adaptive scoring, and possibly save score files.
5621       (when gnus-newsgroup-adaptive
5622         (gnus-score-adaptive))
5623       (when gnus-use-scoring
5624         (gnus-score-save)))
5625     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5626     ;; If we have several article buffers, we kill them at exit.
5627     (unless gnus-single-article-buffer
5628       (gnus-kill-buffer gnus-original-article-buffer)
5629       (setq gnus-article-current nil))
5630     (when gnus-use-cache
5631       (gnus-cache-possibly-remove-articles)
5632       (gnus-cache-save-buffers))
5633     (gnus-async-prefetch-remove-group group)
5634     (when gnus-suppress-duplicates
5635       (gnus-dup-enter-articles))
5636     (when gnus-use-trees
5637       (gnus-tree-close group))
5638     (when gnus-use-cache
5639       (gnus-cache-write-active))
5640     ;; Remove entries for this group.
5641     (nnmail-purge-split-history (gnus-group-real-name group))
5642     ;; Make all changes in this group permanent.
5643     (unless quit-config
5644       (gnus-run-hooks 'gnus-exit-group-hook)
5645       (gnus-summary-update-info))
5646     (gnus-close-group group)
5647     ;; Make sure where we were, and go to next newsgroup.
5648     (set-buffer gnus-group-buffer)
5649     (unless quit-config
5650       (gnus-group-jump-to-group group))
5651     (gnus-run-hooks 'gnus-summary-exit-hook)
5652     (unless (or quit-config
5653                 ;; If this group has disappeared from the summary
5654                 ;; buffer, don't skip forwards.
5655                 (not (string= group (gnus-group-group-name))))
5656       (gnus-group-next-unread-group 1))
5657     (setq group-point (point))
5658     (if temporary
5659         nil                             ;Nothing to do.
5660       ;; If we have several article buffers, we kill them at exit.
5661       (unless gnus-single-article-buffer
5662         (gnus-kill-buffer gnus-article-buffer)
5663         (gnus-kill-buffer gnus-original-article-buffer)
5664         (setq gnus-article-current nil))
5665       (set-buffer buf)
5666       (if (not gnus-kill-summary-on-exit)
5667           (gnus-deaden-summary)
5668         ;; We set all buffer-local variables to nil.  It is unclear why
5669         ;; this is needed, but if we don't, buffer-local variables are
5670         ;; not garbage-collected, it seems.  This would the lead to en
5671         ;; ever-growing Emacs.
5672         (gnus-summary-clear-local-variables)
5673         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5674           (gnus-summary-clear-local-variables))
5675         (when (get-buffer gnus-article-buffer)
5676           (bury-buffer gnus-article-buffer))
5677         ;; We clear the global counterparts of the buffer-local
5678         ;; variables as well, just to be on the safe side.
5679         (set-buffer gnus-group-buffer)
5680         (gnus-summary-clear-local-variables)
5681         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5682           (gnus-summary-clear-local-variables))
5683         ;; Return to group mode buffer.
5684         (when (eq mode 'gnus-summary-mode)
5685           (gnus-kill-buffer buf)))
5686       (setq gnus-current-select-method gnus-select-method)
5687       (pop-to-buffer gnus-group-buffer)
5688       (if (not quit-config)
5689           (progn
5690             (goto-char group-point)
5691             (gnus-configure-windows 'group 'force)
5692             (unless (pos-visible-in-window-p)
5693               (forward-line (/ (static-if (featurep 'xemacs)
5694                                    (window-displayed-height)
5695                                  (1- (window-height)))
5696                                -2))
5697               (set-window-start (selected-window) (point))
5698               (goto-char group-point)))
5699         (gnus-handle-ephemeral-exit quit-config))
5700       ;; Clear the current group name.
5701       (unless quit-config
5702         (setq gnus-newsgroup-name nil)))))
5703
5704 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5705 (defun gnus-summary-exit-no-update (&optional no-questions)
5706   "Quit reading current newsgroup without updating read article info."
5707   (interactive)
5708   (let* ((group gnus-newsgroup-name)
5709          (quit-config (gnus-group-quit-config group)))
5710     (when (or no-questions
5711               gnus-expert-user
5712               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5713       (gnus-async-halt-prefetch)
5714       (mapcar 'funcall
5715               (delq 'gnus-summary-expire-articles
5716                     (copy-sequence gnus-summary-prepare-exit-hook)))
5717       ;; If we have several article buffers, we kill them at exit.
5718       (unless gnus-single-article-buffer
5719         (gnus-kill-buffer gnus-article-buffer)
5720         (gnus-kill-buffer gnus-original-article-buffer)
5721         (setq gnus-article-current nil))
5722       (if (not gnus-kill-summary-on-exit)
5723           (gnus-deaden-summary)
5724         (gnus-close-group group)
5725         (gnus-summary-clear-local-variables)
5726         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5727           (gnus-summary-clear-local-variables))
5728         (set-buffer gnus-group-buffer)
5729         (gnus-summary-clear-local-variables)
5730         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5731           (gnus-summary-clear-local-variables))
5732         (when (get-buffer gnus-summary-buffer)
5733           (kill-buffer gnus-summary-buffer)))
5734       (unless gnus-single-article-buffer
5735         (setq gnus-article-current nil))
5736       (when gnus-use-trees
5737         (gnus-tree-close group))
5738       (gnus-async-prefetch-remove-group group)
5739       (when (get-buffer gnus-article-buffer)
5740         (bury-buffer gnus-article-buffer))
5741       ;; Return to the group buffer.
5742       (gnus-configure-windows 'group 'force)
5743       ;; Clear the current group name.
5744       (setq gnus-newsgroup-name nil)
5745       (when (equal (gnus-group-group-name) group)
5746         (gnus-group-next-unread-group 1))
5747       (when quit-config
5748         (gnus-handle-ephemeral-exit quit-config)))))
5749
5750 (defun gnus-handle-ephemeral-exit (quit-config)
5751   "Handle movement when leaving an ephemeral group.
5752 The state which existed when entering the ephemeral is reset."
5753   (if (not (buffer-name (car quit-config)))
5754       (gnus-configure-windows 'group 'force)
5755     (set-buffer (car quit-config))
5756     (cond ((eq major-mode 'gnus-summary-mode)
5757            (gnus-set-global-variables))
5758           ((eq major-mode 'gnus-article-mode)
5759            (save-excursion
5760              ;; The `gnus-summary-buffer' variable may point
5761              ;; to the old summary buffer when using a single
5762              ;; article buffer.
5763              (unless (gnus-buffer-live-p gnus-summary-buffer)
5764                (set-buffer gnus-group-buffer))
5765              (set-buffer gnus-summary-buffer)
5766              (gnus-set-global-variables))))
5767     (if (or (eq (cdr quit-config) 'article)
5768             (eq (cdr quit-config) 'pick))
5769         (progn
5770           ;; The current article may be from the ephemeral group
5771           ;; thus it is best that we reload this article
5772           (gnus-summary-show-article)
5773           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5774               (gnus-configure-windows 'pick 'force)
5775             (gnus-configure-windows (cdr quit-config) 'force)))
5776       (gnus-configure-windows (cdr quit-config) 'force))
5777     (when (eq major-mode 'gnus-summary-mode)
5778       (gnus-summary-next-subject 1 nil t)
5779       (gnus-summary-recenter)
5780       (gnus-summary-position-point))))
5781
5782 (defun gnus-summary-preview-mime-message ()
5783   "MIME decode and play this message."
5784   (interactive)
5785   (let ((gnus-break-pages nil)
5786         (gnus-show-mime t))
5787     (gnus-summary-select-article gnus-show-all-headers t))
5788   (select-window (get-buffer-window gnus-article-buffer)))
5789
5790 ;;; Dead summaries.
5791
5792 (defvar gnus-dead-summary-mode-map nil)
5793
5794 (unless gnus-dead-summary-mode-map
5795   (setq gnus-dead-summary-mode-map (make-keymap))
5796   (suppress-keymap gnus-dead-summary-mode-map)
5797   (substitute-key-definition
5798    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5799   (let ((keys '("\C-d" "\r" "\177" [delete])))
5800     (while keys
5801       (define-key gnus-dead-summary-mode-map
5802         (pop keys) 'gnus-summary-wake-up-the-dead))))
5803
5804 (defvar gnus-dead-summary-mode nil
5805   "Minor mode for Gnus summary buffers.")
5806
5807 (defun gnus-dead-summary-mode (&optional arg)
5808   "Minor mode for Gnus summary buffers."
5809   (interactive "P")
5810   (when (eq major-mode 'gnus-summary-mode)
5811     (make-local-variable 'gnus-dead-summary-mode)
5812     (setq gnus-dead-summary-mode
5813           (if (null arg) (not gnus-dead-summary-mode)
5814             (> (prefix-numeric-value arg) 0)))
5815     (when gnus-dead-summary-mode
5816       (gnus-add-minor-mode
5817        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5818
5819 (defun gnus-deaden-summary ()
5820   "Make the current summary buffer into a dead summary buffer."
5821   ;; Kill any previous dead summary buffer.
5822   (when (and gnus-dead-summary
5823              (buffer-name gnus-dead-summary))
5824     (save-excursion
5825       (set-buffer gnus-dead-summary)
5826       (when gnus-dead-summary-mode
5827         (kill-buffer (current-buffer)))))
5828   ;; Make this the current dead summary.
5829   (setq gnus-dead-summary (current-buffer))
5830   (gnus-dead-summary-mode 1)
5831   (let ((name (buffer-name)))
5832     (when (string-match "Summary" name)
5833       (rename-buffer
5834        (concat (substring name 0 (match-beginning 0)) "Dead "
5835                (substring name (match-beginning 0)))
5836        t)
5837       (bury-buffer))))
5838
5839 (defun gnus-kill-or-deaden-summary (buffer)
5840   "Kill or deaden the summary BUFFER."
5841   (save-excursion
5842     (when (and (buffer-name buffer)
5843                (not gnus-single-article-buffer))
5844       (save-excursion
5845         (set-buffer buffer)
5846         (gnus-kill-buffer gnus-article-buffer)
5847         (gnus-kill-buffer gnus-original-article-buffer)))
5848     (cond (gnus-kill-summary-on-exit
5849            (when (and gnus-use-trees
5850                       (gnus-buffer-exists-p buffer))
5851              (save-excursion
5852                (set-buffer buffer)
5853                (gnus-tree-close gnus-newsgroup-name)))
5854            (gnus-kill-buffer buffer))
5855           ((gnus-buffer-exists-p buffer)
5856            (save-excursion
5857              (set-buffer buffer)
5858              (gnus-deaden-summary))))))
5859
5860 (defun gnus-summary-wake-up-the-dead (&rest args)
5861   "Wake up the dead summary buffer."
5862   (interactive)
5863   (gnus-dead-summary-mode -1)
5864   (let ((name (buffer-name)))
5865     (when (string-match "Dead " name)
5866       (rename-buffer
5867        (concat (substring name 0 (match-beginning 0))
5868                (substring name (match-end 0)))
5869        t)))
5870   (gnus-message 3 "This dead summary is now alive again"))
5871
5872 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5873 (defun gnus-summary-fetch-faq (&optional faq-dir)
5874   "Fetch the FAQ for the current group.
5875 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5876 in."
5877   (interactive
5878    (list
5879     (when current-prefix-arg
5880       (completing-read
5881        "Faq dir: " (and (listp gnus-group-faq-directory)
5882                         (mapcar (lambda (file) (list file))
5883                                 gnus-group-faq-directory))))))
5884   (let (gnus-faq-buffer)
5885     (when (setq gnus-faq-buffer
5886                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5887       (gnus-configure-windows 'summary-faq))))
5888
5889 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5890 (defun gnus-summary-describe-group (&optional force)
5891   "Describe the current newsgroup."
5892   (interactive "P")
5893   (gnus-group-describe-group force gnus-newsgroup-name))
5894
5895 (defun gnus-summary-describe-briefly ()
5896   "Describe summary mode commands briefly."
5897   (interactive)
5898   (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")))
5899
5900 ;; Walking around group mode buffer from summary mode.
5901
5902 (defun gnus-summary-next-group (&optional no-article target-group backward)
5903   "Exit current newsgroup and then select next unread newsgroup.
5904 If prefix argument NO-ARTICLE is non-nil, no article is selected
5905 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5906 previous group instead."
5907   (interactive "P")
5908   ;; Stop pre-fetching.
5909   (gnus-async-halt-prefetch)
5910   (let ((current-group gnus-newsgroup-name)
5911         (current-buffer (current-buffer))
5912         entered)
5913     ;; First we semi-exit this group to update Xrefs and all variables.
5914     ;; We can't do a real exit, because the window conf must remain
5915     ;; the same in case the user is prompted for info, and we don't
5916     ;; want the window conf to change before that...
5917     (gnus-summary-exit t)
5918     (while (not entered)
5919       ;; Then we find what group we are supposed to enter.
5920       (set-buffer gnus-group-buffer)
5921       (gnus-group-jump-to-group current-group)
5922       (setq target-group
5923             (or target-group
5924                 (if (eq gnus-keep-same-level 'best)
5925                     (gnus-summary-best-group gnus-newsgroup-name)
5926                   (gnus-summary-search-group backward gnus-keep-same-level))))
5927       (if (not target-group)
5928           ;; There are no further groups, so we return to the group
5929           ;; buffer.
5930           (progn
5931             (gnus-message 5 "Returning to the group buffer")
5932             (setq entered t)
5933             (when (gnus-buffer-live-p current-buffer)
5934               (set-buffer current-buffer)
5935               (gnus-summary-exit))
5936             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5937         ;; We try to enter the target group.
5938         (gnus-group-jump-to-group target-group)
5939         (let ((unreads (gnus-group-group-unread)))
5940           (if (and (or (eq t unreads)
5941                        (and unreads (not (zerop unreads))))
5942                    (gnus-summary-read-group
5943                     target-group nil no-article
5944                     (and (buffer-name current-buffer) current-buffer)
5945                     nil backward))
5946               (setq entered t)
5947             (setq current-group target-group
5948                   target-group nil)))))))
5949
5950 (defun gnus-summary-prev-group (&optional no-article)
5951   "Exit current newsgroup and then select previous unread newsgroup.
5952 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5953   (interactive "P")
5954   (gnus-summary-next-group no-article nil t))
5955
5956 ;; Walking around summary lines.
5957
5958 (defun gnus-summary-first-subject (&optional unread undownloaded)
5959   "Go to the first unread subject.
5960 If UNREAD is non-nil, go to the first unread article.
5961 Returns the article selected or nil if there are no unread articles."
5962   (interactive "P")
5963   (prog1
5964       (cond
5965        ;; Empty summary.
5966        ((null gnus-newsgroup-data)
5967         (gnus-message 3 "No articles in the group")
5968         nil)
5969        ;; Pick the first article.
5970        ((not unread)
5971         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5972         (gnus-data-number (car gnus-newsgroup-data)))
5973        ;; No unread articles.
5974        ((null gnus-newsgroup-unreads)
5975         (gnus-message 3 "No more unread articles")
5976         nil)
5977        ;; Find the first unread article.
5978        (t
5979         (let ((data gnus-newsgroup-data))
5980           (while (and data
5981                       (and (not (and undownloaded
5982                                      (eq gnus-undownloaded-mark
5983                                          (gnus-data-mark (car data)))))
5984                            (not (gnus-data-unread-p (car data)))))
5985             (setq data (cdr data)))
5986           (when data
5987             (goto-char (gnus-data-pos (car data)))
5988             (gnus-data-number (car data))))))
5989     (gnus-summary-position-point)))
5990
5991 (defun gnus-summary-next-subject (n &optional unread dont-display)
5992   "Go to next N'th summary line.
5993 If N is negative, go to the previous N'th subject line.
5994 If UNREAD is non-nil, only unread articles are selected.
5995 The difference between N and the actual number of steps taken is
5996 returned."
5997   (interactive "p")
5998   (let ((backward (< n 0))
5999         (n (abs n)))
6000     (while (and (> n 0)
6001                 (if backward
6002                     (gnus-summary-find-prev unread)
6003                   (gnus-summary-find-next unread)))
6004       (unless (zerop (setq n (1- n)))
6005         (gnus-summary-show-thread)))
6006     (when (/= 0 n)
6007       (gnus-message 7 "No more%s articles"
6008                     (if unread " unread" "")))
6009     (unless dont-display
6010       (gnus-summary-recenter)
6011       (gnus-summary-position-point))
6012     n))
6013
6014 (defun gnus-summary-next-unread-subject (n)
6015   "Go to next N'th unread summary line."
6016   (interactive "p")
6017   (gnus-summary-next-subject n t))
6018
6019 (defun gnus-summary-prev-subject (n &optional unread)
6020   "Go to previous N'th summary line.
6021 If optional argument UNREAD is non-nil, only unread article is selected."
6022   (interactive "p")
6023   (gnus-summary-next-subject (- n) unread))
6024
6025 (defun gnus-summary-prev-unread-subject (n)
6026   "Go to previous N'th unread summary line."
6027   (interactive "p")
6028   (gnus-summary-next-subject (- n) t))
6029
6030 (defun gnus-summary-goto-subject (article &optional force silent)
6031   "Go the subject line of ARTICLE.
6032 If FORCE, also allow jumping to articles not currently shown."
6033   (interactive "nArticle number: ")
6034   (let ((b (point))
6035         (data (gnus-data-find article)))
6036     ;; We read in the article if we have to.
6037     (and (not data)
6038          force
6039          (gnus-summary-insert-subject
6040           article
6041           (if (or (numberp force) (vectorp force)) force)
6042           t)
6043          (setq data (gnus-data-find article)))
6044     (goto-char b)
6045     (if (not data)
6046         (progn
6047           (unless silent
6048             (gnus-message 3 "Can't find article %d" article))
6049           nil)
6050       (goto-char (gnus-data-pos data))
6051       (gnus-summary-position-point)
6052       article)))
6053
6054 ;; Walking around summary lines with displaying articles.
6055
6056 (defun gnus-summary-expand-window (&optional arg)
6057   "Make the summary buffer take up the entire Emacs frame.
6058 Given a prefix, will force an `article' buffer configuration."
6059   (interactive "P")
6060   (if arg
6061       (gnus-configure-windows 'article 'force)
6062     (gnus-configure-windows 'summary 'force)))
6063
6064 (defun gnus-summary-display-article (article &optional all-header)
6065   "Display ARTICLE in article buffer."
6066   (when (gnus-buffer-live-p gnus-article-buffer)
6067     (with-current-buffer gnus-article-buffer
6068       (mm-enable-multibyte-mule4)))
6069   (gnus-set-global-variables)
6070   (when (gnus-buffer-live-p gnus-article-buffer)
6071     (with-current-buffer gnus-article-buffer
6072       (setq gnus-article-charset gnus-newsgroup-charset)
6073       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6074   (if (null article)
6075       nil
6076     (prog1
6077         (if gnus-summary-display-article-function
6078             (funcall gnus-summary-display-article-function article all-header)
6079           (gnus-article-prepare article all-header))
6080       (with-current-buffer gnus-article-buffer
6081         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6082              nil))
6083       (gnus-run-hooks 'gnus-select-article-hook)
6084       (when (and gnus-current-article
6085                  (not (zerop gnus-current-article)))
6086         (gnus-summary-goto-subject gnus-current-article))
6087       (gnus-summary-recenter)
6088       (when (and gnus-use-trees gnus-show-threads)
6089         (gnus-possibly-generate-tree article)
6090         (gnus-highlight-selected-tree article))
6091       ;; Successfully display article.
6092       (gnus-article-set-window-start
6093        (cdr (assq article gnus-newsgroup-bookmarks))))))
6094
6095 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6096   "Select the current article.
6097 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6098 non-nil, the article will be re-fetched even if it already present in
6099 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6100 be displayed."
6101   ;; Make sure we are in the summary buffer to work around bbdb bug.
6102   (unless (eq major-mode 'gnus-summary-mode)
6103     (set-buffer gnus-summary-buffer))
6104   (let ((article (or article (gnus-summary-article-number)))
6105         (all-headers (not (not all-headers))) ;Must be T or NIL.
6106         gnus-summary-display-article-function)
6107     (and (not pseudo)
6108          (gnus-summary-article-pseudo-p article)
6109          (error "This is a pseudo-article"))
6110     (save-excursion
6111       (set-buffer gnus-summary-buffer)
6112       (if (or (and gnus-single-article-buffer
6113                    (or (null gnus-current-article)
6114                        (null gnus-article-current)
6115                        (null (get-buffer gnus-article-buffer))
6116                        (not (eq article (cdr gnus-article-current)))
6117                        (not (equal (car gnus-article-current)
6118                                    gnus-newsgroup-name))))
6119               (and (not gnus-single-article-buffer)
6120                    (or (null gnus-current-article)
6121                        (not (eq gnus-current-article article))))
6122               force)
6123           ;; The requested article is different from the current article.
6124           (progn
6125             (gnus-summary-display-article article all-headers)
6126             (when (or all-headers gnus-show-all-headers)
6127               (gnus-article-show-all-headers))
6128             (gnus-article-set-window-start
6129              (cdr (assq article gnus-newsgroup-bookmarks)))
6130             article)
6131         (when (or all-headers gnus-show-all-headers)
6132           (gnus-article-show-all-headers))
6133         'old))))
6134
6135 (defun gnus-summary-force-verify-and-decrypt ()
6136   (interactive)
6137   (let ((mm-verify-option 'known)
6138         (mm-decrypt-option 'known))
6139     (gnus-summary-select-article nil 'force)))
6140
6141 (defun gnus-summary-set-current-mark (&optional current-mark)
6142   "Obsolete function."
6143   nil)
6144
6145 (defun gnus-summary-next-article (&optional unread subject backward push)
6146   "Select the next article.
6147 If UNREAD, only unread articles are selected.
6148 If SUBJECT, only articles with SUBJECT are selected.
6149 If BACKWARD, the previous article is selected instead of the next."
6150   (interactive "P")
6151   (cond
6152    ;; Is there such an article?
6153    ((and (gnus-summary-search-forward unread subject backward)
6154          (or (gnus-summary-display-article (gnus-summary-article-number))
6155              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6156     (gnus-summary-position-point))
6157    ;; If not, we try the first unread, if that is wanted.
6158    ((and subject
6159          gnus-auto-select-same
6160          (gnus-summary-first-unread-article))
6161     (gnus-summary-position-point)
6162     (gnus-message 6 "Wrapped"))
6163    ;; Try to get next/previous article not displayed in this group.
6164    ((and gnus-auto-extend-newsgroup
6165          (not unread) (not subject))
6166     (gnus-summary-goto-article
6167      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6168      nil (count-lines (point-min) (point))))
6169    ;; Go to next/previous group.
6170    (t
6171     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6172       (gnus-summary-jump-to-group gnus-newsgroup-name))
6173     (let ((cmd last-command-char)
6174           (point
6175            (save-excursion
6176              (set-buffer gnus-group-buffer)
6177              (point)))
6178           (group
6179            (if (eq gnus-keep-same-level 'best)
6180                (gnus-summary-best-group gnus-newsgroup-name)
6181              (gnus-summary-search-group backward gnus-keep-same-level))))
6182       ;; For some reason, the group window gets selected.  We change
6183       ;; it back.
6184       (select-window (get-buffer-window (current-buffer)))
6185       ;; Select next unread newsgroup automagically.
6186       (cond
6187        ((or (not gnus-auto-select-next)
6188             (not cmd))
6189         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6190        ((or (eq gnus-auto-select-next 'quietly)
6191             (and (eq gnus-auto-select-next 'slightly-quietly)
6192                  push)
6193             (and (eq gnus-auto-select-next 'almost-quietly)
6194                  (gnus-summary-last-article-p)))
6195         ;; Select quietly.
6196         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6197             (gnus-summary-exit)
6198           (gnus-message 7 "No more%s articles (%s)..."
6199                         (if unread " unread" "")
6200                         (if group (concat "selecting " group)
6201                           "exiting"))
6202           (gnus-summary-next-group nil group backward)))
6203        (t
6204         (when (gnus-key-press-event-p last-input-event)
6205           (gnus-summary-walk-group-buffer
6206            gnus-newsgroup-name cmd unread backward point))))))))
6207
6208 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6209   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6210                       (?\C-p (gnus-group-prev-unread-group 1))))
6211         (cursor-in-echo-area t)
6212         keve key group ended)
6213     (save-excursion
6214       (set-buffer gnus-group-buffer)
6215       (goto-char start)
6216       (setq group
6217             (if (eq gnus-keep-same-level 'best)
6218                 (gnus-summary-best-group gnus-newsgroup-name)
6219               (gnus-summary-search-group backward gnus-keep-same-level))))
6220     (while (not ended)
6221       (gnus-message
6222        5 "No more%s articles%s" (if unread " unread" "")
6223        (if (and group
6224                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6225            (format " (Type %s for %s [%s])"
6226                    (single-key-description cmd) group
6227                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6228          (format " (Type %s to exit %s)"
6229                  (single-key-description cmd)
6230                  gnus-newsgroup-name)))
6231       ;; Confirm auto selection.
6232       (setq key (car (setq keve (gnus-read-event-char))))
6233       (setq ended t)
6234       (cond
6235        ((assq key keystrokes)
6236         (let ((obuf (current-buffer)))
6237           (switch-to-buffer gnus-group-buffer)
6238           (when group
6239             (gnus-group-jump-to-group group))
6240           (eval (cadr (assq key keystrokes)))
6241           (setq group (gnus-group-group-name))
6242           (switch-to-buffer obuf))
6243         (setq ended nil))
6244        ((equal key cmd)
6245         (if (or (not group)
6246                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6247             (gnus-summary-exit)
6248           (gnus-summary-next-group nil group backward)))
6249        (t
6250         (push (cdr keve) unread-command-events))))))
6251
6252 (defun gnus-summary-next-unread-article ()
6253   "Select unread article after current one."
6254   (interactive)
6255   (gnus-summary-next-article
6256    (or (not (eq gnus-summary-goto-unread 'never))
6257        (gnus-summary-last-article-p (gnus-summary-article-number)))
6258    (and gnus-auto-select-same
6259         (gnus-summary-article-subject))))
6260
6261 (defun gnus-summary-prev-article (&optional unread subject)
6262   "Select the article after the current one.
6263 If UNREAD is non-nil, only unread articles are selected."
6264   (interactive "P")
6265   (gnus-summary-next-article unread subject t))
6266
6267 (defun gnus-summary-prev-unread-article ()
6268   "Select unread article before current one."
6269   (interactive)
6270   (gnus-summary-prev-article
6271    (or (not (eq gnus-summary-goto-unread 'never))
6272        (gnus-summary-first-article-p (gnus-summary-article-number)))
6273    (and gnus-auto-select-same
6274         (gnus-summary-article-subject))))
6275
6276 (defun gnus-summary-next-page (&optional lines circular)
6277   "Show next page of the selected article.
6278 If at the end of the current article, select the next article.
6279 LINES says how many lines should be scrolled up.
6280
6281 If CIRCULAR is non-nil, go to the start of the article instead of
6282 selecting the next article when reaching the end of the current
6283 article."
6284   (interactive "P")
6285   (setq gnus-summary-buffer (current-buffer))
6286   (gnus-set-global-variables)
6287   (let ((article (gnus-summary-article-number))
6288         (article-window (get-buffer-window gnus-article-buffer t))
6289         endp)
6290     ;; If the buffer is empty, we have no article.
6291     (unless article
6292       (error "No article to select"))
6293     (gnus-configure-windows 'article)
6294     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6295         (if (and (eq gnus-summary-goto-unread 'never)
6296                  (not (gnus-summary-last-article-p article)))
6297             (gnus-summary-next-article)
6298           (gnus-summary-next-unread-article))
6299       (if (or (null gnus-current-article)
6300               (null gnus-article-current)
6301               (/= article (cdr gnus-article-current))
6302               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6303           ;; Selected subject is different from current article's.
6304           (gnus-summary-display-article article)
6305         (when article-window
6306           (gnus-eval-in-buffer-window gnus-article-buffer
6307             (setq endp (gnus-article-next-page lines)))
6308           (when endp
6309             (cond (circular
6310                    (gnus-summary-beginning-of-article))
6311                   (lines
6312                    (gnus-message 3 "End of message"))
6313                   ((null lines)
6314                    (if (and (eq gnus-summary-goto-unread 'never)
6315                             (not (gnus-summary-last-article-p article)))
6316                        (gnus-summary-next-article)
6317                      (gnus-summary-next-unread-article))))))))
6318     (gnus-summary-recenter)
6319     (gnus-summary-position-point)))
6320
6321 (defun gnus-summary-prev-page (&optional lines move)
6322   "Show previous page of selected article.
6323 Argument LINES specifies lines to be scrolled down.
6324 If MOVE, move to the previous unread article if point is at
6325 the beginning of the buffer."
6326   (interactive "P")
6327   (let ((article (gnus-summary-article-number))
6328         (article-window (get-buffer-window gnus-article-buffer t))
6329         endp)
6330     (gnus-configure-windows 'article)
6331     (if (or (null gnus-current-article)
6332             (null gnus-article-current)
6333             (/= article (cdr gnus-article-current))
6334             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6335         ;; Selected subject is different from current article's.
6336         (gnus-summary-display-article article)
6337       (gnus-summary-recenter)
6338       (when article-window
6339         (gnus-eval-in-buffer-window gnus-article-buffer
6340           (setq endp (gnus-article-prev-page lines)))
6341         (when (and move endp)
6342           (cond (lines
6343                  (gnus-message 3 "Beginning of message"))
6344                 ((null lines)
6345                  (if (and (eq gnus-summary-goto-unread 'never)
6346                           (not (gnus-summary-first-article-p article)))
6347                      (gnus-summary-prev-article)
6348                    (gnus-summary-prev-unread-article))))))))
6349   (gnus-summary-position-point))
6350
6351 (defun gnus-summary-prev-page-or-article (&optional lines)
6352   "Show previous page of selected article.
6353 Argument LINES specifies lines to be scrolled down.
6354 If at the beginning of the article, go to the next article."
6355   (interactive "P")
6356   (gnus-summary-prev-page lines t))
6357
6358 (defun gnus-summary-scroll-up (lines)
6359   "Scroll up (or down) one line current article.
6360 Argument LINES specifies lines to be scrolled up (or down if negative)."
6361   (interactive "p")
6362   (gnus-configure-windows 'article)
6363   (gnus-summary-show-thread)
6364   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6365     (gnus-eval-in-buffer-window gnus-article-buffer
6366       (cond ((> lines 0)
6367              (when (gnus-article-next-page lines)
6368                (gnus-message 3 "End of message")))
6369             ((< lines 0)
6370              (gnus-article-prev-page (- lines))))))
6371   (gnus-summary-recenter)
6372   (gnus-summary-position-point))
6373
6374 (defun gnus-summary-scroll-down (lines)
6375   "Scroll down (or up) one line current article.
6376 Argument LINES specifies lines to be scrolled down (or up if negative)."
6377   (interactive "p")
6378   (gnus-summary-scroll-up (- lines)))
6379
6380 (defun gnus-summary-next-same-subject ()
6381   "Select next article which has the same subject as current one."
6382   (interactive)
6383   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6384
6385 (defun gnus-summary-prev-same-subject ()
6386   "Select previous article which has the same subject as current one."
6387   (interactive)
6388   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6389
6390 (defun gnus-summary-next-unread-same-subject ()
6391   "Select next unread article which has the same subject as current one."
6392   (interactive)
6393   (gnus-summary-next-article t (gnus-summary-article-subject)))
6394
6395 (defun gnus-summary-prev-unread-same-subject ()
6396   "Select previous unread article which has the same subject as current one."
6397   (interactive)
6398   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6399
6400 (defun gnus-summary-first-unread-article ()
6401   "Select the first unread article.
6402 Return nil if there are no unread articles."
6403   (interactive)
6404   (prog1
6405       (when (gnus-summary-first-subject t)
6406         (gnus-summary-show-thread)
6407         (gnus-summary-first-subject t)
6408         (gnus-summary-display-article (gnus-summary-article-number)))
6409     (gnus-summary-position-point)))
6410
6411 (defun gnus-summary-first-unread-subject ()
6412   "Place the point on the subject line of the first unread article.
6413 Return nil if there are no unread articles."
6414   (interactive)
6415   (prog1
6416       (when (gnus-summary-first-subject t)
6417         (gnus-summary-show-thread)
6418         (gnus-summary-first-subject t))
6419     (gnus-summary-position-point)))
6420
6421 (defun gnus-summary-first-article ()
6422   "Select the first article.
6423 Return nil if there are no articles."
6424   (interactive)
6425   (prog1
6426       (when (gnus-summary-first-subject)
6427         (gnus-summary-show-thread)
6428         (gnus-summary-first-subject)
6429         (gnus-summary-display-article (gnus-summary-article-number)))
6430     (gnus-summary-position-point)))
6431
6432 (defun gnus-summary-best-unread-article ()
6433   "Select the unread article with the highest score."
6434   (interactive)
6435   (let ((best -1000000)
6436         (data gnus-newsgroup-data)
6437         article score)
6438     (while data
6439       (and (gnus-data-unread-p (car data))
6440            (> (setq score
6441                     (gnus-summary-article-score (gnus-data-number (car data))))
6442               best)
6443            (setq best score
6444                  article (gnus-data-number (car data))))
6445       (setq data (cdr data)))
6446     (prog1
6447         (if article
6448             (gnus-summary-goto-article article)
6449           (error "No unread articles"))
6450       (gnus-summary-position-point))))
6451
6452 (defun gnus-summary-last-subject ()
6453   "Go to the last displayed subject line in the group."
6454   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6455     (when article
6456       (gnus-summary-goto-subject article))))
6457
6458 (defun gnus-summary-goto-article (article &optional all-headers force)
6459   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6460 If ALL-HEADERS is non-nil, no header lines are hidden.
6461 If FORCE, go to the article even if it isn't displayed.  If FORCE
6462 is a number, it is the line the article is to be displayed on."
6463   (interactive
6464    (list
6465     (completing-read
6466      "Article number or Message-ID: "
6467      (mapcar (lambda (number) (list (int-to-string number)))
6468              gnus-newsgroup-limit))
6469     current-prefix-arg
6470     t))
6471   (prog1
6472       (if (and (stringp article)
6473                (string-match "@" article))
6474           (gnus-summary-refer-article article)
6475         (when (stringp article)
6476           (setq article (string-to-number article)))
6477         (if (gnus-summary-goto-subject article force)
6478             (gnus-summary-display-article article all-headers)
6479           (gnus-message 4 "Couldn't go to article %s" article) nil))
6480     (gnus-summary-position-point)))
6481
6482 (defun gnus-summary-goto-last-article ()
6483   "Go to the previously read article."
6484   (interactive)
6485   (prog1
6486       (when gnus-last-article
6487         (gnus-summary-goto-article gnus-last-article nil t))
6488     (gnus-summary-position-point)))
6489
6490 (defun gnus-summary-pop-article (number)
6491   "Pop one article off the history and go to the previous.
6492 NUMBER articles will be popped off."
6493   (interactive "p")
6494   (let (to)
6495     (setq gnus-newsgroup-history
6496           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6497     (if to
6498         (gnus-summary-goto-article (car to) nil t)
6499       (error "Article history empty")))
6500   (gnus-summary-position-point))
6501
6502 ;; Summary commands and functions for limiting the summary buffer.
6503
6504 (defun gnus-summary-limit-to-articles (n)
6505   "Limit the summary buffer to the next N articles.
6506 If not given a prefix, use the process marked articles instead."
6507   (interactive "P")
6508   (prog1
6509       (let ((articles (gnus-summary-work-articles n)))
6510         (setq gnus-newsgroup-processable nil)
6511         (gnus-summary-limit articles))
6512     (gnus-summary-position-point)))
6513
6514 (defun gnus-summary-pop-limit (&optional total)
6515   "Restore the previous limit.
6516 If given a prefix, remove all limits."
6517   (interactive "P")
6518   (when total
6519     (setq gnus-newsgroup-limits
6520           (list (mapcar (lambda (h) (mail-header-number h))
6521                         gnus-newsgroup-headers))))
6522   (unless gnus-newsgroup-limits
6523     (error "No limit to pop"))
6524   (prog1
6525       (gnus-summary-limit nil 'pop)
6526     (gnus-summary-position-point)))
6527
6528 (defun gnus-summary-limit-to-subject (subject &optional header)
6529   "Limit the summary buffer to articles that have subjects that match a regexp."
6530   (interactive "sLimit to subject (regexp): ")
6531   (unless header
6532     (setq header "subject"))
6533   (when (not (equal "" subject))
6534     (prog1
6535         (let ((articles (gnus-summary-find-matching
6536                          (or header "subject") subject 'all)))
6537           (unless articles
6538             (error "Found no matches for \"%s\"" subject))
6539           (gnus-summary-limit articles))
6540       (gnus-summary-position-point))))
6541
6542 (defun gnus-summary-limit-to-author (from)
6543   "Limit the summary buffer to articles that have authors that match a regexp."
6544   (interactive "sLimit to author (regexp): ")
6545   (gnus-summary-limit-to-subject from "from"))
6546
6547 (defun gnus-summary-limit-to-age (age &optional younger-p)
6548   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6549 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6550 articles that are younger than AGE days."
6551   (interactive
6552    (let ((younger current-prefix-arg)
6553          (days-got nil)
6554          days)
6555      (while (not days-got)
6556        (setq days (if younger
6557                       (read-string "Limit to articles within (in days): ")
6558                     (read-string "Limit to articles older than (in days): ")))
6559        (when (> (length days) 0)
6560          (setq days (read days)))
6561        (if (numberp days)
6562            (setq days-got t)
6563          (message "Please enter a number.")
6564          (sleep-for 1)))
6565      (list days younger)))
6566   (prog1
6567       (let ((data gnus-newsgroup-data)
6568             (cutoff (days-to-time age))
6569             articles d date is-younger)
6570         (while (setq d (pop data))
6571           (when (and (vectorp (gnus-data-header d))
6572                      (setq date (mail-header-date (gnus-data-header d))))
6573             (setq is-younger (time-less-p
6574                               (time-since (condition-case ()
6575                                               (date-to-time date)
6576                                             (error '(0 0))))
6577                               cutoff))
6578             (when (if younger-p
6579                       is-younger
6580                     (not is-younger))
6581               (push (gnus-data-number d) articles))))
6582         (gnus-summary-limit (nreverse articles)))
6583     (gnus-summary-position-point)))
6584
6585 (defun gnus-summary-limit-to-extra (header regexp)
6586   "Limit the summary buffer to articles that match an 'extra' header."
6587   (interactive
6588    (let ((header
6589           (intern
6590            (gnus-completing-read
6591             (symbol-name (car gnus-extra-headers))
6592             "Limit extra header:"
6593             (mapcar (lambda (x)
6594                       (cons (symbol-name x) x))
6595                     gnus-extra-headers)
6596             nil
6597             t))))
6598      (list header
6599            (read-string (format "Limit to header %s (regexp): " header)))))
6600   (when (not (equal "" regexp))
6601     (prog1
6602         (let ((articles (gnus-summary-find-matching
6603                          (cons 'extra header) regexp 'all)))
6604           (unless articles
6605             (error "Found no matches for \"%s\"" regexp))
6606           (gnus-summary-limit articles))
6607       (gnus-summary-position-point))))
6608
6609 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6610 (make-obsolete
6611  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6612
6613 (defun gnus-summary-limit-to-unread (&optional all)
6614   "Limit the summary buffer to articles that are not marked as read.
6615 If ALL is non-nil, limit strictly to unread articles."
6616   (interactive "P")
6617   (if all
6618       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6619     (gnus-summary-limit-to-marks
6620      ;; Concat all the marks that say that an article is read and have
6621      ;; those removed.
6622      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6623            gnus-killed-mark gnus-kill-file-mark
6624            gnus-low-score-mark gnus-expirable-mark
6625            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6626            gnus-duplicate-mark gnus-souped-mark)
6627      'reverse)))
6628
6629 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6630 (make-obsolete 'gnus-summary-delete-marked-with
6631                'gnus-summary-limit-exlude-marks)
6632
6633 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6634   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6635 If REVERSE, limit the summary buffer to articles that are marked
6636 with MARKS.  MARKS can either be a string of marks or a list of marks.
6637 Returns how many articles were removed."
6638   (interactive "sMarks: ")
6639   (gnus-summary-limit-to-marks marks t))
6640
6641 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6642   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6643 If REVERSE (the prefix), limit the summary buffer to articles that are
6644 not marked with MARKS.  MARKS can either be a string of marks or a
6645 list of marks.
6646 Returns how many articles were removed."
6647   (interactive "sMarks: \nP")
6648   (prog1
6649       (let ((data gnus-newsgroup-data)
6650             (marks (if (listp marks) marks
6651                      (append marks nil))) ; Transform to list.
6652             articles)
6653         (while data
6654           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6655                   (memq (gnus-data-mark (car data)) marks))
6656             (push (gnus-data-number (car data)) articles))
6657           (setq data (cdr data)))
6658         (gnus-summary-limit articles))
6659     (gnus-summary-position-point)))
6660
6661 (defun gnus-summary-limit-to-score (&optional score)
6662   "Limit to articles with score at or above SCORE."
6663   (interactive "P")
6664   (setq score (if score
6665                   (prefix-numeric-value score)
6666                 (or gnus-summary-default-score 0)))
6667   (let ((data gnus-newsgroup-data)
6668         articles)
6669     (while data
6670       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6671                 score)
6672         (push (gnus-data-number (car data)) articles))
6673       (setq data (cdr data)))
6674     (prog1
6675         (gnus-summary-limit articles)
6676       (gnus-summary-position-point))))
6677
6678 (defun gnus-summary-limit-include-thread (id)
6679   "Display all the hidden articles that in the current thread."
6680   (interactive (list (mail-header-id (gnus-summary-article-header))))
6681   (let ((articles (gnus-articles-in-thread
6682                    (gnus-id-to-thread (gnus-root-id id)))))
6683     (prog1
6684         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6685       (gnus-summary-position-point))))
6686
6687 (defun gnus-summary-limit-include-dormant ()
6688   "Display all the hidden articles that are marked as dormant.
6689 Note that this command only works on a subset of the articles currently
6690 fetched for this group."
6691   (interactive)
6692   (unless gnus-newsgroup-dormant
6693     (error "There are no dormant articles in this group"))
6694   (prog1
6695       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6696     (gnus-summary-position-point)))
6697
6698 (defun gnus-summary-limit-exclude-dormant ()
6699   "Hide all dormant articles."
6700   (interactive)
6701   (prog1
6702       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6703     (gnus-summary-position-point)))
6704
6705 (defun gnus-summary-limit-exclude-childless-dormant ()
6706   "Hide all dormant articles that have no children."
6707   (interactive)
6708   (let ((data (gnus-data-list t))
6709         articles d children)
6710     ;; Find all articles that are either not dormant or have
6711     ;; children.
6712     (while (setq d (pop data))
6713       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6714                 (and (setq children
6715                            (gnus-article-children (gnus-data-number d)))
6716                      (let (found)
6717                        (while children
6718                          (when (memq (car children) articles)
6719                            (setq children nil
6720                                  found t))
6721                          (pop children))
6722                        found)))
6723         (push (gnus-data-number d) articles)))
6724     ;; Do the limiting.
6725     (prog1
6726         (gnus-summary-limit articles)
6727       (gnus-summary-position-point))))
6728
6729 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6730   "Mark all unread excluded articles as read.
6731 If ALL, mark even excluded ticked and dormants as read."
6732   (interactive "P")
6733   (let ((articles (gnus-sorted-complement
6734                    (sort
6735                     (mapcar (lambda (h) (mail-header-number h))
6736                             gnus-newsgroup-headers)
6737                     '<)
6738                    (sort gnus-newsgroup-limit '<)))
6739         article)
6740     (setq gnus-newsgroup-unreads
6741           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6742     (if all
6743         (setq gnus-newsgroup-dormant nil
6744               gnus-newsgroup-marked nil
6745               gnus-newsgroup-reads
6746               (nconc
6747                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6748                gnus-newsgroup-reads))
6749       (while (setq article (pop articles))
6750         (unless (or (memq article gnus-newsgroup-dormant)
6751                     (memq article gnus-newsgroup-marked))
6752           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6753
6754 (defun gnus-summary-limit (articles &optional pop)
6755   (if pop
6756       ;; We pop the previous limit off the stack and use that.
6757       (setq articles (car gnus-newsgroup-limits)
6758             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6759     ;; We use the new limit, so we push the old limit on the stack.
6760     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6761   ;; Set the limit.
6762   (setq gnus-newsgroup-limit articles)
6763   (let ((total (length gnus-newsgroup-data))
6764         (data (gnus-data-find-list (gnus-summary-article-number)))
6765         (gnus-summary-mark-below nil)   ; Inhibit this.
6766         found)
6767     ;; This will do all the work of generating the new summary buffer
6768     ;; according to the new limit.
6769     (gnus-summary-prepare)
6770     ;; Hide any threads, possibly.
6771     (and gnus-show-threads
6772          gnus-thread-hide-subtree
6773          (gnus-summary-hide-all-threads))
6774     ;; Try to return to the article you were at, or one in the
6775     ;; neighborhood.
6776     (when data
6777       ;; We try to find some article after the current one.
6778       (while data
6779         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6780           (setq data nil
6781                 found t))
6782         (setq data (cdr data))))
6783     (unless found
6784       ;; If there is no data, that means that we were after the last
6785       ;; article.  The same goes when we can't find any articles
6786       ;; after the current one.
6787       (goto-char (point-max))
6788       (gnus-summary-find-prev))
6789     (gnus-set-mode-line 'summary)
6790     ;; We return how many articles were removed from the summary
6791     ;; buffer as a result of the new limit.
6792     (- total (length gnus-newsgroup-data))))
6793
6794 (defsubst gnus-invisible-cut-children (threads)
6795   (let ((num 0))
6796     (while threads
6797       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6798         (incf num))
6799       (pop threads))
6800     (< num 2)))
6801
6802 (defsubst gnus-cut-thread (thread)
6803   "Go forwards in the thread until we find an article that we want to display."
6804   (when (or (eq gnus-fetch-old-headers 'some)
6805             (eq gnus-fetch-old-headers 'invisible)
6806             (numberp gnus-fetch-old-headers)
6807             (eq gnus-build-sparse-threads 'some)
6808             (eq gnus-build-sparse-threads 'more))
6809     ;; Deal with old-fetched headers and sparse threads.
6810     (while (and
6811             thread
6812             (or
6813              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6814              (gnus-summary-article-ancient-p
6815               (mail-header-number (car thread))))
6816             (if (or (<= (length (cdr thread)) 1)
6817                     (eq gnus-fetch-old-headers 'invisible))
6818                 (setq gnus-newsgroup-limit
6819                       (delq (mail-header-number (car thread))
6820                             gnus-newsgroup-limit)
6821                       thread (cadr thread))
6822               (when (gnus-invisible-cut-children (cdr thread))
6823                 (let ((th (cdr thread)))
6824                   (while th
6825                     (if (memq (mail-header-number (caar th))
6826                               gnus-newsgroup-limit)
6827                         (setq thread (car th)
6828                               th nil)
6829                       (setq th (cdr th))))))))))
6830   thread)
6831
6832 (defun gnus-cut-threads (threads)
6833   "Cut off all uninteresting articles from the beginning of threads."
6834   (when (or (eq gnus-fetch-old-headers 'some)
6835             (eq gnus-fetch-old-headers 'invisible)
6836             (numberp gnus-fetch-old-headers)
6837             (eq gnus-build-sparse-threads 'some)
6838             (eq gnus-build-sparse-threads 'more))
6839     (let ((th threads))
6840       (while th
6841         (setcar th (gnus-cut-thread (car th)))
6842         (setq th (cdr th)))))
6843   ;; Remove nixed out threads.
6844   (delq nil threads))
6845
6846 (defun gnus-summary-initial-limit (&optional show-if-empty)
6847   "Figure out what the initial limit is supposed to be on group entry.
6848 This entails weeding out unwanted dormants, low-scored articles,
6849 fetch-old-headers verbiage, and so on."
6850   ;; Most groups have nothing to remove.
6851   (if (or gnus-inhibit-limiting
6852           (and (null gnus-newsgroup-dormant)
6853                (not (eq gnus-fetch-old-headers 'some))
6854                (not (numberp gnus-fetch-old-headers))
6855                (not (eq gnus-fetch-old-headers 'invisible))
6856                (null gnus-summary-expunge-below)
6857                (not (eq gnus-build-sparse-threads 'some))
6858                (not (eq gnus-build-sparse-threads 'more))
6859                (null gnus-thread-expunge-below)
6860                (not gnus-use-nocem)))
6861       ()                                ; Do nothing.
6862     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6863     (setq gnus-newsgroup-limit nil)
6864     (mapatoms
6865      (lambda (node)
6866        (unless (car (symbol-value node))
6867          ;; These threads have no parents -- they are roots.
6868          (let ((nodes (cdr (symbol-value node)))
6869                thread)
6870            (while nodes
6871              (if (and gnus-thread-expunge-below
6872                       (< (gnus-thread-total-score (car nodes))
6873                          gnus-thread-expunge-below))
6874                  (gnus-expunge-thread (pop nodes))
6875                (setq thread (pop nodes))
6876                (gnus-summary-limit-children thread))))))
6877      gnus-newsgroup-dependencies)
6878     ;; If this limitation resulted in an empty group, we might
6879     ;; pop the previous limit and use it instead.
6880     (when (and (not gnus-newsgroup-limit)
6881                show-if-empty)
6882       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6883     gnus-newsgroup-limit))
6884
6885 (defun gnus-summary-limit-children (thread)
6886   "Return 1 if this subthread is visible and 0 if it is not."
6887   ;; First we get the number of visible children to this thread.  This
6888   ;; is done by recursing down the thread using this function, so this
6889   ;; will really go down to a leaf article first, before slowly
6890   ;; working its way up towards the root.
6891   (when thread
6892     (let ((children
6893            (if (cdr thread)
6894                (apply '+ (mapcar 'gnus-summary-limit-children
6895                                  (cdr thread)))
6896              0))
6897           (number (mail-header-number (car thread)))
6898           score)
6899       (if (and
6900            (not (memq number gnus-newsgroup-marked))
6901            (or
6902             ;; If this article is dormant and has absolutely no visible
6903             ;; children, then this article isn't visible.
6904             (and (memq number gnus-newsgroup-dormant)
6905                  (zerop children))
6906             ;; If this is "fetch-old-headered" and there is no
6907             ;; visible children, then we don't want this article.
6908             (and (or (eq gnus-fetch-old-headers 'some)
6909                      (numberp gnus-fetch-old-headers))
6910                  (gnus-summary-article-ancient-p number)
6911                  (zerop children))
6912             ;; If this is "fetch-old-headered" and `invisible', then
6913             ;; we don't want this article.
6914             (and (eq gnus-fetch-old-headers 'invisible)
6915                  (gnus-summary-article-ancient-p number))
6916             ;; If this is a sparsely inserted article with no children,
6917             ;; we don't want it.
6918             (and (eq gnus-build-sparse-threads 'some)
6919                  (gnus-summary-article-sparse-p number)
6920                  (zerop children))
6921             ;; If we use expunging, and this article is really
6922             ;; low-scored, then we don't want this article.
6923             (when (and gnus-summary-expunge-below
6924                        (< (setq score
6925                                 (or (cdr (assq number gnus-newsgroup-scored))
6926                                     gnus-summary-default-score))
6927                           gnus-summary-expunge-below))
6928               ;; We increase the expunge-tally here, but that has
6929               ;; nothing to do with the limits, really.
6930               (incf gnus-newsgroup-expunged-tally)
6931               ;; We also mark as read here, if that's wanted.
6932               (when (and gnus-summary-mark-below
6933                          (< score gnus-summary-mark-below))
6934                 (setq gnus-newsgroup-unreads
6935                       (delq number gnus-newsgroup-unreads))
6936                 (if gnus-newsgroup-auto-expire
6937                     (push number gnus-newsgroup-expirable)
6938                   (push (cons number gnus-low-score-mark)
6939                         gnus-newsgroup-reads)))
6940               t)
6941             ;; Check NoCeM things.
6942             (if (and gnus-use-nocem
6943                      (gnus-nocem-unwanted-article-p
6944                       (mail-header-id (car thread))))
6945                 (progn
6946                   (setq gnus-newsgroup-unreads
6947                         (delq number gnus-newsgroup-unreads))
6948                   t))))
6949           ;; Nope, invisible article.
6950           0
6951         ;; Ok, this article is to be visible, so we add it to the limit
6952         ;; and return 1.
6953         (push number gnus-newsgroup-limit)
6954         1))))
6955
6956 (defun gnus-expunge-thread (thread)
6957   "Mark all articles in THREAD as read."
6958   (let* ((number (mail-header-number (car thread))))
6959     (incf gnus-newsgroup-expunged-tally)
6960     ;; We also mark as read here, if that's wanted.
6961     (setq gnus-newsgroup-unreads
6962           (delq number gnus-newsgroup-unreads))
6963     (if gnus-newsgroup-auto-expire
6964         (push number gnus-newsgroup-expirable)
6965       (push (cons number gnus-low-score-mark)
6966             gnus-newsgroup-reads)))
6967   ;; Go recursively through all subthreads.
6968   (mapcar 'gnus-expunge-thread (cdr thread)))
6969
6970 ;; Summary article oriented commands
6971
6972 (defun gnus-summary-refer-parent-article (n)
6973   "Refer parent article N times.
6974 If N is negative, go to ancestor -N instead.
6975 The difference between N and the number of articles fetched is returned."
6976   (interactive "p")
6977   (let ((skip 1)
6978         error header ref)
6979     (when (not (natnump n))
6980       (setq skip (abs n)
6981             n 1))
6982     (while (and (> n 0)
6983                 (not error))
6984       (setq header (gnus-summary-article-header))
6985       (if (and (eq (mail-header-number header)
6986                    (cdr gnus-article-current))
6987                (equal gnus-newsgroup-name
6988                       (car gnus-article-current)))
6989           ;; If we try to find the parent of the currently
6990           ;; displayed article, then we take a look at the actual
6991           ;; References header, since this is slightly more
6992           ;; reliable than the References field we got from the
6993           ;; server.
6994           (save-excursion
6995             (set-buffer gnus-original-article-buffer)
6996             (nnheader-narrow-to-headers)
6997             (unless (setq ref (message-fetch-field "references"))
6998               (setq ref (message-fetch-field "in-reply-to")))
6999             (widen))
7000         (setq ref
7001               ;; It's not the current article, so we take a bet on
7002               ;; the value we got from the server.
7003               (mail-header-references header)))
7004       (if (and ref
7005                (not (equal ref "")))
7006           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7007             (gnus-message 1 "Couldn't find parent"))
7008         (gnus-message 1 "No references in article %d"
7009                       (gnus-summary-article-number))
7010         (setq error t))
7011       (decf n))
7012     (gnus-summary-position-point)
7013     n))
7014
7015 (defun gnus-summary-refer-references ()
7016   "Fetch all articles mentioned in the References header.
7017 Return the number of articles fetched."
7018   (interactive)
7019   (let ((ref (mail-header-references (gnus-summary-article-header)))
7020         (current (gnus-summary-article-number))
7021         (n 0))
7022     (if (or (not ref)
7023             (equal ref ""))
7024         (error "No References in the current article")
7025       ;; For each Message-ID in the References header...
7026       (while (string-match "<[^>]*>" ref)
7027         (incf n)
7028         ;; ... fetch that article.
7029         (gnus-summary-refer-article
7030          (prog1 (match-string 0 ref)
7031            (setq ref (substring ref (match-end 0))))))
7032       (gnus-summary-goto-subject current)
7033       (gnus-summary-position-point)
7034       n)))
7035
7036 (defun gnus-summary-refer-thread (&optional limit)
7037   "Fetch all articles in the current thread.
7038 If LIMIT (the numerical prefix), fetch that many old headers instead
7039 of what's specified by the `gnus-refer-thread-limit' variable."
7040   (interactive "P")
7041   (let ((id (mail-header-id (gnus-summary-article-header)))
7042         (limit (if limit (prefix-numeric-value limit)
7043                  gnus-refer-thread-limit)))
7044     ;; We want to fetch LIMIT *old* headers, but we also have to
7045     ;; re-fetch all the headers in the current buffer, because many of
7046     ;; them may be undisplayed.  So we adjust LIMIT.
7047     (when (numberp limit)
7048       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7049     (unless (eq gnus-fetch-old-headers 'invisible)
7050       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7051       ;; Retrieve the headers and read them in.
7052       (if (eq (gnus-retrieve-headers
7053                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7054               'nov)
7055           (gnus-build-all-threads)
7056         (error "Can't fetch thread from backends that don't support NOV"))
7057       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7058     (gnus-summary-limit-include-thread id)))
7059
7060 (defun gnus-summary-refer-article (message-id)
7061   "Fetch an article specified by MESSAGE-ID."
7062   (interactive "sMessage-ID: ")
7063   (when (and (stringp message-id)
7064              (not (zerop (length message-id))))
7065     ;; Construct the correct Message-ID if necessary.
7066     ;; Suggested by tale@pawl.rpi.edu.
7067     (unless (string-match "^<" message-id)
7068       (setq message-id (concat "<" message-id)))
7069     (unless (string-match ">$" message-id)
7070       (setq message-id (concat message-id ">")))
7071     (let* ((header (gnus-id-to-header message-id))
7072            (sparse (and header
7073                         (gnus-summary-article-sparse-p
7074                          (mail-header-number header))
7075                         (memq (mail-header-number header)
7076                               gnus-newsgroup-limit)))
7077            number)
7078       (cond
7079        ;; If the article is present in the buffer we just go to it.
7080        ((and header
7081              (or (not (gnus-summary-article-sparse-p
7082                        (mail-header-number header)))
7083                  sparse))
7084         (prog1
7085             (gnus-summary-goto-article
7086              (mail-header-number header) nil t)
7087           (when sparse
7088             (gnus-summary-update-article (mail-header-number header)))))
7089        (t
7090         ;; We fetch the article.
7091         (catch 'found
7092           (dolist (gnus-override-method (gnus-refer-article-methods))
7093             (gnus-check-server gnus-override-method)
7094             ;; Fetch the header, and display the article.
7095             (when (setq number (gnus-summary-insert-subject message-id))
7096               (gnus-summary-select-article nil nil nil number)
7097               (throw 'found t)))
7098           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7099
7100 (defun gnus-refer-article-methods ()
7101   "Return a list of referrable methods."
7102   (cond
7103    ;; No method, so we default to current and native.
7104    ((null gnus-refer-article-method)
7105     (list gnus-current-select-method gnus-select-method))
7106    ;; Current.
7107    ((eq 'current gnus-refer-article-method)
7108     (list gnus-current-select-method))
7109    ;; List of select methods.
7110    ((not (and (symbolp (car gnus-refer-article-method))
7111               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7112     (let (out)
7113       (dolist (method gnus-refer-article-method)
7114         (push (if (eq 'current method)
7115                   gnus-current-select-method
7116                 method)
7117               out))
7118       (nreverse out)))
7119    ;; One single select method.
7120    (t
7121     (list gnus-refer-article-method))))
7122
7123 (defun gnus-summary-edit-parameters ()
7124   "Edit the group parameters of the current group."
7125   (interactive)
7126   (gnus-group-edit-group gnus-newsgroup-name 'params))
7127
7128 (defun gnus-summary-customize-parameters ()
7129   "Customize the group parameters of the current group."
7130   (interactive)
7131   (gnus-group-customize gnus-newsgroup-name))
7132
7133 (defun gnus-summary-enter-digest-group (&optional force)
7134   "Enter an nndoc group based on the current article.
7135 If FORCE, force a digest interpretation.  If not, try
7136 to guess what the document format is."
7137   (interactive "P")
7138   (let ((conf gnus-current-window-configuration))
7139     (save-excursion
7140       (gnus-summary-select-article))
7141     (setq gnus-current-window-configuration conf)
7142     (let* ((name (format "%s-%d"
7143                          (gnus-group-prefixed-name
7144                           gnus-newsgroup-name (list 'nndoc ""))
7145                          (save-excursion
7146                            (set-buffer gnus-summary-buffer)
7147                            gnus-current-article)))
7148            (ogroup gnus-newsgroup-name)
7149            (params (append (gnus-info-params (gnus-get-info ogroup))
7150                            (list (cons 'to-group ogroup))
7151                            (list (cons 'save-article-group ogroup))))
7152            (case-fold-search t)
7153            (buf (current-buffer))
7154            dig to-address)
7155       (save-excursion
7156         (set-buffer gnus-original-article-buffer)
7157         ;; Have the digest group inherit the main mail address of
7158         ;; the parent article.
7159         (when (setq to-address (or (message-fetch-field "reply-to")
7160                                    (message-fetch-field "from")))
7161           (setq params (append
7162                         (list (cons 'to-address
7163                                     (funcall gnus-decode-encoded-word-function
7164                                              to-address))))))
7165         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7166         (insert-buffer-substring gnus-original-article-buffer)
7167         ;; Remove lines that may lead nndoc to misinterpret the
7168         ;; document type.
7169         (narrow-to-region
7170          (goto-char (point-min))
7171          (or (search-forward "\n\n" nil t) (point)))
7172         (goto-char (point-min))
7173         (delete-matching-lines "^Path:\\|^From ")
7174         (widen))
7175       (unwind-protect
7176           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7177                     (gnus-newsgroup-ephemeral-ignored-charsets
7178                      gnus-newsgroup-ignored-charsets))
7179                 (gnus-group-read-ephemeral-group
7180                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7181                               (nndoc-article-type
7182                                ,(if force 'mbox 'guess))) t))
7183               ;; Make all postings to this group go to the parent group.
7184               (nconc (gnus-info-params (gnus-get-info name))
7185                      params)
7186             ;; Couldn't select this doc group.
7187             (switch-to-buffer buf)
7188             (gnus-set-global-variables)
7189             (gnus-configure-windows 'summary)
7190             (gnus-message 3 "Article couldn't be entered?"))
7191         (kill-buffer dig)))))
7192
7193 (defun gnus-summary-read-document (n)
7194   "Open a new group based on the current article(s).
7195 This will allow you to read digests and other similar
7196 documents as newsgroups.
7197 Obeys the standard process/prefix convention."
7198   (interactive "P")
7199   (let* ((articles (gnus-summary-work-articles n))
7200          (ogroup gnus-newsgroup-name)
7201          (params (append (gnus-info-params (gnus-get-info ogroup))
7202                          (list (cons 'to-group ogroup))))
7203          article group egroup groups vgroup)
7204     (while (setq article (pop articles))
7205       (setq group (format "%s-%d" gnus-newsgroup-name article))
7206       (gnus-summary-remove-process-mark article)
7207       (when (gnus-summary-display-article article)
7208         (save-excursion
7209           (with-temp-buffer
7210             (insert-buffer-substring gnus-original-article-buffer)
7211             ;; Remove some headers that may lead nndoc to make
7212             ;; the wrong guess.
7213             (message-narrow-to-head)
7214             (goto-char (point-min))
7215             (delete-matching-lines "^\\(Path\\):\\|^From ")
7216             (widen)
7217             (if (setq egroup
7218                       (gnus-group-read-ephemeral-group
7219                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7220                                      (nndoc-article-type guess))
7221                        t nil t))
7222                 (progn
7223                   ;; Make all postings to this group go to the parent group.
7224                   (nconc (gnus-info-params (gnus-get-info egroup))
7225                          params)
7226                   (push egroup groups))
7227               ;; Couldn't select this doc group.
7228               (gnus-error 3 "Article couldn't be entered"))))))
7229     ;; Now we have selected all the documents.
7230     (cond
7231      ((not groups)
7232       (error "None of the articles could be interpreted as documents"))
7233      ((gnus-group-read-ephemeral-group
7234        (setq vgroup (format
7235                      "nnvirtual:%s-%s" gnus-newsgroup-name
7236                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7237        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7238        t
7239        (cons (current-buffer) 'summary)))
7240      (t
7241       (error "Couldn't select virtual nndoc group")))))
7242
7243 (defun gnus-summary-isearch-article (&optional regexp-p)
7244   "Do incremental search forward on the current article.
7245 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7246   (interactive "P")
7247   (let* ((gnus-inhibit-treatment t)
7248          (old (gnus-summary-select-article)))
7249     (gnus-configure-windows 'article)
7250     (gnus-eval-in-buffer-window gnus-article-buffer
7251       (save-restriction
7252         (widen)
7253         (when (eq 'old old)
7254           (gnus-article-show-all-headers))
7255         (goto-char (point-min))
7256         (isearch-forward regexp-p)))))
7257
7258 (defun gnus-summary-search-article-forward (regexp &optional backward)
7259   "Search for an article containing REGEXP forward.
7260 If BACKWARD, search backward instead."
7261   (interactive
7262    (list (read-string
7263           (format "Search article %s (regexp%s): "
7264                   (if current-prefix-arg "backward" "forward")
7265                   (if gnus-last-search-regexp
7266                       (concat ", default " gnus-last-search-regexp)
7267                     "")))
7268          current-prefix-arg))
7269   (if (string-equal regexp "")
7270       (setq regexp (or gnus-last-search-regexp ""))
7271     (setq gnus-last-search-regexp regexp)
7272     (setq gnus-article-before-search gnus-current-article))
7273   ;; Intentionally set gnus-last-article.
7274   (setq gnus-last-article gnus-article-before-search)
7275   (let ((gnus-last-article gnus-last-article))
7276     (if (gnus-summary-search-article regexp backward)
7277         (gnus-summary-show-thread)
7278       (error "Search failed: \"%s\"" regexp))))
7279
7280 (defun gnus-summary-search-article-backward (regexp)
7281   "Search for an article containing REGEXP backward."
7282   (interactive
7283    (list (read-string
7284           (format "Search article backward (regexp%s): "
7285                   (if gnus-last-search-regexp
7286                       (concat ", default " gnus-last-search-regexp)
7287                     "")))))
7288   (gnus-summary-search-article-forward regexp 'backward))
7289
7290 (eval-when-compile
7291   (defmacro gnus-summary-search-article-position-point (regexp backward)
7292     "Dehighlight the last matched text and goto the beginning position."
7293     (` (if (and gnus-summary-search-article-matched-data
7294                 (let ((text (caddr gnus-summary-search-article-matched-data))
7295                       (inhibit-read-only t)
7296                       buffer-read-only)
7297                   (delete-region
7298                    (goto-char (car gnus-summary-search-article-matched-data))
7299                    (cadr gnus-summary-search-article-matched-data))
7300                   (insert text)
7301                   (string-match (, regexp) text)))
7302            (if (, backward) (beginning-of-line) (end-of-line))
7303          (goto-char (if (, backward) (point-max) (point-min))))))
7304
7305   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7306     "Place point where X-Face image is displayed."
7307     (if (featurep 'xemacs)
7308         (` (let ((end (if (search-forward "\n\n" nil t)
7309                           (goto-char (1- (point)))
7310                         (point-min)))
7311                  extent)
7312              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7313              (unless (and (re-search-forward "^From:" end t)
7314                           (setq extent (extent-at (point)))
7315                           (extent-begin-glyph extent))
7316                (goto-char (, opoint)))))
7317       (` (let ((end (if (search-forward "\n\n" nil t)
7318                         (goto-char (1- (point)))
7319                       (point-min)))
7320                (start (or (search-backward "\n\n" nil t) (point-min))))
7321            (goto-char
7322             (or (text-property-any start end 'x-face-image t);; x-face-e21
7323                 (text-property-any start end 'x-face-mule-bitmap-image t)
7324                 (, opoint)))))))
7325
7326   (defmacro gnus-summary-search-article-highlight-matched-text
7327     (backward treated x-face)
7328     "Highlight matched text in the function `gnus-summary-search-article'."
7329     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7330              (end (set-marker (make-marker) (match-end 0)))
7331              (inhibit-read-only t)
7332              buffer-read-only)
7333          (unless treated
7334            (let ((,@
7335                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7336                     (mapcar
7337                      (lambda (item) (setq items (delq item items)))
7338                      '(gnus-treat-buttonize
7339                        gnus-treat-fill-article
7340                        gnus-treat-fill-long-lines
7341                        gnus-treat-emphasize
7342                        gnus-treat-highlight-headers
7343                        gnus-treat-highlight-citation
7344                        gnus-treat-highlight-signature
7345                        gnus-treat-overstrike
7346                        gnus-treat-display-xface
7347                        gnus-treat-buttonize-head
7348                        gnus-treat-decode-article-as-default-mime-charset))
7349                     (static-if (featurep 'xemacs)
7350                         items
7351                       (cons '(x-face-mule-delete-x-face-field
7352                               (quote never))
7353                             items))))
7354                  (gnus-treat-display-xface
7355                   (when (, x-face) gnus-treat-display-xface)))
7356              (gnus-article-prepare-mime-display)))
7357          (goto-char (if (, backward) start end))
7358          (when (, x-face)
7359            (gnus-summary-search-article-highlight-goto-x-face (point)))
7360          (setq gnus-summary-search-article-matched-data
7361                (list start end (buffer-substring start end)))
7362          (unless (eq start end);; matched text has been deleted. :-<
7363            (put-text-property start end 'face
7364                               (or (find-face 'isearch)
7365                                   'secondary-selection))))))
7366   )
7367
7368 (defun gnus-summary-search-article (regexp &optional backward)
7369   "Search for an article containing REGEXP.
7370 Optional argument BACKWARD means do search for backward.
7371 `gnus-select-article-hook' is not called during the search."
7372   ;; We have to require this here to make sure that the following
7373   ;; dynamic binding isn't shadowed by autoloading.
7374   (require 'gnus-async)
7375   (require 'gnus-art)
7376   (let ((gnus-select-article-hook nil)  ;Disable hook.
7377         (gnus-article-display-hook nil)
7378         (gnus-article-prepare-hook nil)
7379         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7380         (gnus-use-article-prefetch nil)
7381         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7382         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7383         (sum (current-buffer))
7384         (found nil)
7385         point treated)
7386     (gnus-save-hidden-threads
7387       (static-if (featurep 'xemacs)
7388           (let ((gnus-inhibit-treatment t))
7389             (setq treated (eq 'old (gnus-summary-select-article)))
7390             (when (and treated
7391                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7392                                  (window-live-p (get-buffer-window
7393                                                  gnus-article-buffer t)))))
7394               (gnus-summary-select-article nil t)
7395               (setq treated nil)))
7396         (let ((gnus-inhibit-treatment t)
7397               (x-face-mule-delete-x-face-field 'never))
7398           (setq treated (eq 'old (gnus-summary-select-article)))
7399           (when (and treated
7400                      (not
7401                       (and (gnus-buffer-live-p gnus-article-buffer)
7402                            (window-live-p (get-buffer-window
7403                                            gnus-article-buffer t))
7404                            (or (not (string-match "^\\^X-Face:" regexp))
7405                                (with-current-buffer gnus-article-buffer
7406                                  gnus-summary-search-article-matched-data)))))
7407             (gnus-summary-select-article nil t)
7408             (setq treated nil))))
7409       (set-buffer gnus-article-buffer)
7410       (widen)
7411       (if treated
7412           (progn
7413             (gnus-article-show-all-headers)
7414             (gnus-summary-search-article-position-point regexp backward))
7415         (goto-char (if backward (point-max) (point-min))))
7416       (while (not found)
7417         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7418         (if (if backward
7419                 (re-search-backward regexp nil t)
7420               (re-search-forward regexp nil t))
7421             ;; We found the regexp.
7422             (progn
7423               (gnus-summary-search-article-highlight-matched-text
7424                backward treated (string-match "^\\^X-Face:" regexp))
7425               (setq found 'found)
7426               (forward-line
7427                (/ (- 2 (window-height
7428                         (get-buffer-window gnus-article-buffer t)))
7429                   2))
7430               (set-window-start
7431                (get-buffer-window (current-buffer))
7432                (point))
7433               (set-buffer sum)
7434               (setq point (point)))
7435           ;; We didn't find it, so we go to the next article.
7436           (set-buffer sum)
7437           (setq found 'not)
7438           (while (eq found 'not)
7439             (if (not (if backward (gnus-summary-find-prev)
7440                        (gnus-summary-find-next)))
7441                 ;; No more articles.
7442                 (setq found t)
7443               ;; Select the next article and adjust point.
7444               (unless (gnus-summary-article-sparse-p
7445                        (gnus-summary-article-number))
7446                 (setq found nil)
7447                 (let ((gnus-inhibit-treatment t))
7448                   (gnus-summary-select-article))
7449                 (setq treated nil)
7450                 (set-buffer gnus-article-buffer)
7451                 (widen)
7452                 (goto-char (if backward (point-max) (point-min))))))))
7453       (gnus-message 7 ""))
7454     ;; Return whether we found the regexp.
7455     (when (eq found 'found)
7456       (goto-char point)
7457       (gnus-summary-show-thread)
7458       (gnus-summary-goto-subject gnus-current-article)
7459       (gnus-summary-position-point)
7460       t)))
7461
7462 (defun gnus-summary-find-matching (header regexp &optional backward unread
7463                                           not-case-fold)
7464   "Return a list of all articles that match REGEXP on HEADER.
7465 The search stars on the current article and goes forwards unless
7466 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7467 If UNREAD is non-nil, only unread articles will
7468 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7469 in the comparisons."
7470   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7471                 (gnus-data-find-list
7472                  (gnus-summary-article-number) (gnus-data-list backward))))
7473         (case-fold-search (not not-case-fold))
7474         articles d func)
7475     (if (consp header)
7476         (if (eq (car header) 'extra)
7477             (setq func
7478                   `(lambda (h)
7479                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7480                          "")))
7481           (error "%s is an invalid header" header))
7482       (unless (fboundp (intern (concat "mail-header-" header)))
7483         (error "%s is not a valid header" header))
7484       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7485     (while data
7486       (setq d (car data))
7487       (and (or (not unread)             ; We want all articles...
7488                (gnus-data-unread-p d))  ; Or just unreads.
7489            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7490            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7491            (push (gnus-data-number d) articles)) ; Success!
7492       (setq data (cdr data)))
7493     (nreverse articles)))
7494
7495 (defun gnus-summary-execute-command (header regexp command &optional backward)
7496   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7497 If HEADER is an empty string (or nil), the match is done on the entire
7498 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7499   (interactive
7500    (list (let ((completion-ignore-case t))
7501            (completing-read
7502             "Header name: "
7503             (mapcar (lambda (string) (list string))
7504                     '("Number" "Subject" "From" "Lines" "Date"
7505                       "Message-ID" "Xref" "References" "Body"))
7506             nil 'require-match))
7507          (read-string "Regexp: ")
7508          (read-key-sequence "Command: ")
7509          current-prefix-arg))
7510   (when (equal header "Body")
7511     (setq header ""))
7512   ;; Hidden thread subtrees must be searched as well.
7513   (gnus-summary-show-all-threads)
7514   ;; We don't want to change current point nor window configuration.
7515   (save-excursion
7516     (save-window-excursion
7517       (gnus-message 6 "Executing %s..." (key-description command))
7518       ;; We'd like to execute COMMAND interactively so as to give arguments.
7519       (gnus-execute header regexp
7520                     `(call-interactively ',(key-binding command))
7521                     backward)
7522       (gnus-message 6 "Executing %s...done" (key-description command)))))
7523
7524 (defun gnus-summary-beginning-of-article ()
7525   "Scroll the article back to the beginning."
7526   (interactive)
7527   (gnus-summary-select-article)
7528   (gnus-configure-windows 'article)
7529   (gnus-eval-in-buffer-window gnus-article-buffer
7530     (widen)
7531     (goto-char (point-min))
7532     (when gnus-page-broken
7533       (gnus-narrow-to-page))))
7534
7535 (defun gnus-summary-end-of-article ()
7536   "Scroll to the end of the article."
7537   (interactive)
7538   (gnus-summary-select-article)
7539   (gnus-configure-windows 'article)
7540   (gnus-eval-in-buffer-window gnus-article-buffer
7541     (widen)
7542     (goto-char (point-max))
7543     (recenter -3)
7544     (when gnus-page-broken
7545       (gnus-narrow-to-page))))
7546
7547 (defun gnus-summary-print-article (&optional filename n)
7548   "Generate and print a PostScript image of the N next (mail) articles.
7549
7550 If N is negative, print the N previous articles.  If N is nil and articles
7551 have been marked with the process mark, print these instead.
7552
7553 If the optional first argument FILENAME is nil, send the image to the
7554 printer.  If FILENAME is a string, save the PostScript image in a file with
7555 that name.  If FILENAME is a number, prompt the user for the name of the file
7556 to save in."
7557   (interactive (list (ps-print-preprint current-prefix-arg)))
7558   (dolist (article (gnus-summary-work-articles n))
7559     (gnus-summary-select-article nil nil 'pseudo article)
7560     (gnus-eval-in-buffer-window gnus-article-buffer
7561       (let ((buffer (generate-new-buffer " *print*")))
7562         (unwind-protect
7563             (progn
7564               (copy-to-buffer buffer (point-min) (point-max))
7565               (set-buffer buffer)
7566               (gnus-article-delete-invisible-text)
7567               (let ((ps-left-header
7568                      (list
7569                       (concat "("
7570                               (mail-header-subject gnus-current-headers) ")")
7571                       (concat "("
7572                               (mail-header-from gnus-current-headers) ")")))
7573                     (ps-right-header
7574                      (list
7575                       "/pagenumberstring load"
7576                       (concat "("
7577                               (mail-header-date gnus-current-headers) ")"))))
7578                 (gnus-run-hooks 'gnus-ps-print-hook)
7579                 (save-excursion
7580                   (ps-spool-buffer-with-faces))))
7581           (kill-buffer buffer))))
7582     (gnus-summary-remove-process-mark article))
7583   (ps-despool filename))
7584
7585 (defun gnus-summary-show-article (&optional arg)
7586   "Force re-fetching of the current article.
7587 If ARG (the prefix) is a number, show the article with the charset
7588 defined in `gnus-summary-show-article-charset-alist', or the charset
7589 inputed.
7590 If ARG (the prefix) is non-nil and not a number, show the raw article
7591 without any article massaging functions being run."
7592   (interactive "P")
7593   (cond
7594    ((numberp arg)
7595     (let ((gnus-newsgroup-charset
7596            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7597                (read-coding-system "Charset: ")))
7598           (gnus-newsgroup-ignored-charsets 'gnus-all))
7599       (gnus-summary-select-article nil 'force)
7600       (let ((deps gnus-newsgroup-dependencies)
7601             head header)
7602         (save-excursion
7603           (set-buffer gnus-original-article-buffer)
7604           (save-restriction
7605             (message-narrow-to-head)
7606             (setq head (buffer-string)))
7607           (with-temp-buffer
7608             (insert (format "211 %d Article retrieved.\n"
7609                             (cdr gnus-article-current)))
7610             (insert head)
7611             (insert ".\n")
7612             (let ((nntp-server-buffer (current-buffer)))
7613               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7614         (gnus-data-set-header
7615          (gnus-data-find (cdr gnus-article-current))
7616          header)
7617         (gnus-summary-update-article-line
7618          (cdr gnus-article-current) header))))
7619    ((not arg)
7620     ;; Select the article the normal way.
7621     (gnus-summary-select-article nil 'force))
7622    (t
7623     ;; We have to require this here to make sure that the following
7624     ;; dynamic binding isn't shadowed by autoloading.
7625     (require 'gnus-async)
7626     (require 'gnus-art)
7627     ;; Bind the article treatment functions to nil.
7628     (let ((gnus-have-all-headers t)
7629           gnus-article-display-hook
7630           gnus-article-prepare-hook
7631           gnus-article-decode-hook
7632           gnus-break-pages
7633           gnus-show-mime)
7634       (gnus-summary-select-article nil 'force))))
7635   (gnus-summary-goto-subject gnus-current-article)
7636   (gnus-summary-position-point))
7637
7638 (defun gnus-summary-verbose-headers (&optional arg)
7639   "Toggle permanent full header display.
7640 If ARG is a positive number, turn header display on.
7641 If ARG is a negative number, turn header display off."
7642   (interactive "P")
7643   (setq gnus-show-all-headers
7644         (cond ((or (not (numberp arg))
7645                    (zerop arg))
7646                (not gnus-show-all-headers))
7647               ((natnump arg)
7648                t)))
7649   (gnus-summary-show-article))
7650
7651 (defun gnus-summary-toggle-header (&optional arg)
7652   "Show the headers if they are hidden, or hide them if they are shown.
7653 If ARG is a positive number, show the entire header.
7654 If ARG is a negative number, hide the unwanted header lines."
7655   (interactive "P")
7656   (save-excursion
7657     (set-buffer gnus-article-buffer)
7658     (save-restriction
7659       (let* ((buffer-read-only nil)
7660              (inhibit-point-motion-hooks t)
7661              hidden e)
7662         (setq hidden
7663               (if (numberp arg)
7664                   (>= arg 0)
7665                 (save-restriction
7666                   (article-narrow-to-head)
7667                   (gnus-article-hidden-text-p 'headers))))
7668         (goto-char (point-min))
7669         (when (search-forward "\n\n" nil t)
7670           (delete-region (point-min) (1- (point))))
7671         (goto-char (point-min))
7672         (save-excursion
7673           (set-buffer gnus-original-article-buffer)
7674           (goto-char (point-min))
7675           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7676         (insert-buffer-substring gnus-original-article-buffer 1 e)
7677         (save-restriction
7678           (narrow-to-region (point-min) (point))
7679           (article-decode-encoded-words)
7680           (if  hidden
7681               (let ((gnus-treat-hide-headers nil)
7682                     (gnus-treat-hide-boring-headers nil))
7683                 (setq gnus-article-wash-types
7684                       (delq 'headers gnus-article-wash-types))
7685                 (gnus-treat-article 'head))
7686             (gnus-treat-article 'head)))
7687         (gnus-set-mode-line 'article)))))
7688
7689 (defun gnus-summary-show-all-headers ()
7690   "Make all header lines visible."
7691   (interactive)
7692   (gnus-article-show-all-headers))
7693
7694 (defun gnus-summary-toggle-mime (&optional arg)
7695   "Toggle MIME processing.
7696 If ARG is a positive number, turn MIME processing on."
7697   (interactive "P")
7698   (setq gnus-show-mime
7699         (if (null arg)
7700             (not gnus-show-mime)
7701           (> (prefix-numeric-value arg) 0)))
7702   (gnus-summary-select-article t 'force))
7703
7704 (defun gnus-summary-caesar-message (&optional arg)
7705   "Caesar rotate the current article by 13.
7706 The numerical prefix specifies how many places to rotate each letter
7707 forward."
7708   (interactive "P")
7709   (gnus-summary-select-article)
7710   (let ((mail-header-separator ""))
7711     (gnus-eval-in-buffer-window gnus-article-buffer
7712       (save-restriction
7713         (widen)
7714         (let ((start (window-start))
7715               buffer-read-only)
7716           (message-caesar-buffer-body arg)
7717           (set-window-start (get-buffer-window (current-buffer)) start))))))
7718
7719 (defun gnus-summary-stop-page-breaking ()
7720   "Stop page breaking in the current article."
7721   (interactive)
7722   (gnus-summary-select-article)
7723   (gnus-eval-in-buffer-window gnus-article-buffer
7724     (widen)
7725     (when (gnus-visual-p 'page-marker)
7726       (let ((buffer-read-only nil))
7727         (gnus-remove-text-with-property 'gnus-prev)
7728         (gnus-remove-text-with-property 'gnus-next))
7729       (setq gnus-page-broken nil))))
7730
7731 (defun gnus-summary-move-article (&optional n to-newsgroup
7732                                             select-method action)
7733   "Move the current article to a different newsgroup.
7734 If N is a positive number, move the N next articles.
7735 If N is a negative number, move the N previous articles.
7736 If N is nil and any articles have been marked with the process mark,
7737 move those articles instead.
7738 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7739 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7740 re-spool using this method.
7741
7742 For this function to work, both the current newsgroup and the
7743 newsgroup that you want to move to have to support the `request-move'
7744 and `request-accept' functions.
7745
7746 ACTION can be either `move' (the default), `crosspost' or `copy'."
7747   (interactive "P")
7748   (unless action
7749     (setq action 'move))
7750   ;; Disable marking as read.
7751   (let (gnus-mark-article-hook)
7752     (save-window-excursion
7753       (gnus-summary-select-article)))
7754   ;; Check whether the source group supports the required functions.
7755   (cond ((and (eq action 'move)
7756               (not (gnus-check-backend-function
7757                     'request-move-article gnus-newsgroup-name)))
7758          (error "The current group does not support article moving"))
7759         ((and (eq action 'crosspost)
7760               (not (gnus-check-backend-function
7761                     'request-replace-article gnus-newsgroup-name)))
7762          (error "The current group does not support article editing")))
7763   (let ((articles (gnus-summary-work-articles n))
7764         (prefix (if (gnus-check-backend-function
7765                     'request-move-article gnus-newsgroup-name)
7766                     (gnus-group-real-prefix gnus-newsgroup-name)
7767                   ""))
7768         (names '((move "Move" "Moving")
7769                  (copy "Copy" "Copying")
7770                  (crosspost "Crosspost" "Crossposting")))
7771         (copy-buf (save-excursion
7772                     (nnheader-set-temp-buffer " *copy article*")))
7773         (default-marks gnus-article-mark-lists)
7774         (no-expire-marks (delete '(expirable . expire)
7775                                  (copy-sequence gnus-article-mark-lists)))
7776         art-group to-method new-xref article to-groups)
7777     (unless (assq action names)
7778       (error "Unknown action %s" action))
7779     ;; Read the newsgroup name.
7780     (when (and (not to-newsgroup)
7781                (not select-method))
7782       (setq to-newsgroup
7783             (gnus-read-move-group-name
7784              (cadr (assq action names))
7785              (symbol-value (intern (format "gnus-current-%s-group" action)))
7786              articles prefix))
7787       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7788     (setq to-method (or select-method
7789                         (gnus-server-to-method
7790                          (gnus-group-method to-newsgroup))))
7791     ;; Check the method we are to move this article to...
7792     (unless (gnus-check-backend-function
7793              'request-accept-article (car to-method))
7794       (error "%s does not support article copying" (car to-method)))
7795     (unless (gnus-check-server to-method)
7796       (error "Can't open server %s" (car to-method)))
7797     (gnus-message 6 "%s to %s: %s..."
7798                   (caddr (assq action names))
7799                   (or (car select-method) to-newsgroup) articles)
7800     (while articles
7801       (setq article (pop articles))
7802       (setq
7803        art-group
7804        (cond
7805         ;; Move the article.
7806         ((eq action 'move)
7807          ;; Remove this article from future suppression.
7808          (gnus-dup-unsuppress-article article)
7809          (gnus-request-move-article
7810           article                       ; Article to move
7811           gnus-newsgroup-name           ; From newsgroup
7812           (nth 1 (gnus-find-method-for-group
7813                   gnus-newsgroup-name)) ; Server
7814           (list 'gnus-request-accept-article
7815                 to-newsgroup (list 'quote select-method)
7816                 (not articles) t)       ; Accept form
7817           (not articles)))              ; Only save nov last time
7818         ;; Copy the article.
7819         ((eq action 'copy)
7820          (save-excursion
7821            (set-buffer copy-buf)
7822            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7823              (gnus-request-accept-article
7824               to-newsgroup select-method (not articles) t))))
7825         ;; Crosspost the article.
7826         ((eq action 'crosspost)
7827          (let ((xref (message-tokenize-header
7828                       (mail-header-xref (gnus-summary-article-header article))
7829                       " ")))
7830            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7831                                   ":" article))
7832            (unless xref
7833              (setq xref (list (system-name))))
7834            (setq new-xref
7835                  (concat
7836                   (mapconcat 'identity
7837                              (delete "Xref:" (delete new-xref xref))
7838                              " ")
7839                   " " new-xref))
7840            (save-excursion
7841              (set-buffer copy-buf)
7842              ;; First put the article in the destination group.
7843              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7844              (when (consp (setq art-group
7845                                 (gnus-request-accept-article
7846                                  to-newsgroup select-method (not articles))))
7847                (setq new-xref (concat new-xref " " (car art-group)
7848                                       ":" (cdr art-group)))
7849                ;; Now we have the new Xrefs header, so we insert
7850                ;; it and replace the new article.
7851                (nnheader-replace-header "Xref" new-xref)
7852                (gnus-request-replace-article
7853                 (cdr art-group) to-newsgroup (current-buffer))
7854                art-group))))))
7855       (cond
7856        ((not art-group)
7857         (gnus-message 1 "Couldn't %s article %s: %s"
7858                       (cadr (assq action names)) article
7859                       (nnheader-get-report (car to-method))))
7860        ((eq art-group 'junk)
7861         (when (eq action 'move)
7862           (gnus-summary-mark-article article gnus-canceled-mark)
7863           (gnus-message 4 "Deleted article %s" article)))
7864        (t
7865         (let* ((pto-group (gnus-group-prefixed-name
7866                            (car art-group) to-method))
7867                (entry
7868                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7869                (info (nth 2 entry))
7870                (to-group (gnus-info-group info))
7871                to-marks)
7872           ;; Update the group that has been moved to.
7873           (when (and info
7874                      (memq action '(move copy)))
7875             (unless (member to-group to-groups)
7876               (push to-group to-groups))
7877
7878             (unless (memq article gnus-newsgroup-unreads)
7879               (push 'read to-marks)
7880               (gnus-info-set-read
7881                info (gnus-add-to-range (gnus-info-read info)
7882                                        (list (cdr art-group)))))
7883
7884             ;; See whether the article is to be put in the cache.
7885             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7886                              default-marks
7887                            no-expire-marks))
7888                   (to-article (cdr art-group)))
7889
7890               ;; Enter the article into the cache in the new group,
7891               ;; if that is required.
7892               (when gnus-use-cache
7893                 (gnus-cache-possibly-enter-article
7894                  to-group to-article
7895                  (let ((header (copy-sequence
7896                                 (gnus-summary-article-header article))))
7897                    (mail-header-set-number header to-article)
7898                    header)
7899                  (memq article gnus-newsgroup-marked)
7900                  (memq article gnus-newsgroup-dormant)
7901                  (memq article gnus-newsgroup-unreads)))
7902
7903               (when gnus-preserve-marks
7904                 ;; Copy any marks over to the new group.
7905                 (when (and (equal to-group gnus-newsgroup-name)
7906                            (not (memq article gnus-newsgroup-unreads)))
7907                   ;; Mark this article as read in this group.
7908                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7909                   (setcdr (gnus-active to-group) to-article)
7910                   (setcdr gnus-newsgroup-active to-article))
7911
7912                 (while marks
7913                   (when (memq article (symbol-value
7914                                        (intern (format "gnus-newsgroup-%s"
7915                                                        (caar marks)))))
7916                     (push (cdar marks) to-marks)
7917                     ;; If the other group is the same as this group,
7918                     ;; then we have to add the mark to the list.
7919                     (when (equal to-group gnus-newsgroup-name)
7920                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7921                            (cons to-article
7922                                  (symbol-value
7923                                   (intern (format "gnus-newsgroup-%s"
7924                                                   (caar marks)))))))
7925                     ;; Copy the marks to other group.
7926                     (gnus-add-marked-articles
7927                      to-group (cdar marks) (list to-article) info))
7928                   (setq marks (cdr marks)))
7929
7930                 (gnus-request-set-mark to-group (list (list (list to-article)
7931                                                             'set
7932                                                             to-marks))))
7933
7934               (gnus-dribble-enter
7935                (concat "(gnus-group-set-info '"
7936                        (gnus-prin1-to-string (gnus-get-info to-group))
7937                        ")"))))
7938
7939           ;; Update the Xref header in this article to point to
7940           ;; the new crossposted article we have just created.
7941           (when (eq action 'crosspost)
7942             (save-excursion
7943               (set-buffer copy-buf)
7944               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7945               (nnheader-replace-header "Xref" new-xref)
7946               (gnus-request-replace-article
7947                article gnus-newsgroup-name (current-buffer)))))
7948
7949         ;;;!!!Why is this necessary?
7950         (set-buffer gnus-summary-buffer)
7951
7952         (gnus-summary-goto-subject article)
7953         (when (eq action 'move)
7954           (gnus-summary-mark-article article gnus-canceled-mark))))
7955       (gnus-summary-remove-process-mark article))
7956     ;; Re-activate all groups that have been moved to.
7957     (while to-groups
7958       (save-excursion
7959         (set-buffer gnus-group-buffer)
7960         (when (gnus-group-goto-group (car to-groups) t)
7961           (gnus-group-get-new-news-this-group 1 t))
7962         (pop to-groups)))
7963
7964     (gnus-kill-buffer copy-buf)
7965     (gnus-summary-position-point)
7966     (gnus-set-mode-line 'summary)))
7967
7968 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7969   "Move the current article to a different newsgroup.
7970 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7971 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7972 re-spool using this method."
7973   (interactive "P")
7974   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7975
7976 (defun gnus-summary-crosspost-article (&optional n)
7977   "Crosspost the current article to some other group."
7978   (interactive "P")
7979   (gnus-summary-move-article n nil nil 'crosspost))
7980
7981 (defcustom gnus-summary-respool-default-method nil
7982   "Default method for respooling an article.
7983 If nil, use to the current newsgroup method."
7984   :type '(choice (gnus-select-method :value (nnml ""))
7985                  (const nil))
7986   :group 'gnus-summary-mail)
7987
7988 (defun gnus-summary-respool-article (&optional n method)
7989   "Respool the current article.
7990 The article will be squeezed through the mail spooling process again,
7991 which means that it will be put in some mail newsgroup or other
7992 depending on `nnmail-split-methods'.
7993 If N is a positive number, respool the N next articles.
7994 If N is a negative number, respool the N previous articles.
7995 If N is nil and any articles have been marked with the process mark,
7996 respool those articles instead.
7997
7998 Respooling can be done both from mail groups and \"real\" newsgroups.
7999 In the former case, the articles in question will be moved from the
8000 current group into whatever groups they are destined to.  In the
8001 latter case, they will be copied into the relevant groups."
8002   (interactive
8003    (list current-prefix-arg
8004          (let* ((methods (gnus-methods-using 'respool))
8005                 (methname
8006                  (symbol-name (or gnus-summary-respool-default-method
8007                                   (car (gnus-find-method-for-group
8008                                         gnus-newsgroup-name)))))
8009                 (method
8010                  (gnus-completing-read
8011                   methname "What backend do you want to use when respooling?"
8012                   methods nil t nil 'gnus-mail-method-history))
8013                 ms)
8014            (cond
8015             ((zerop (length (setq ms (gnus-servers-using-backend
8016                                       (intern method)))))
8017              (list (intern method) ""))
8018             ((= 1 (length ms))
8019              (car ms))
8020             (t
8021              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8022                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8023                            ms-alist))))))))
8024   (unless method
8025     (error "No method given for respooling"))
8026   (if (assoc (symbol-name
8027               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8028              (gnus-methods-using 'respool))
8029       (gnus-summary-move-article n nil method)
8030     (gnus-summary-copy-article n nil method)))
8031
8032 (defun gnus-summary-import-article (file)
8033   "Import an arbitrary file into a mail newsgroup."
8034   (interactive "fImport file: ")
8035   (let ((group gnus-newsgroup-name)
8036         (now (current-time))
8037         atts lines)
8038     (unless (gnus-check-backend-function 'request-accept-article group)
8039       (error "%s does not support article importing" group))
8040     (or (file-readable-p file)
8041         (not (file-regular-p file))
8042         (error "Can't read %s" file))
8043     (save-excursion
8044       (set-buffer (gnus-get-buffer-create " *import file*"))
8045       (erase-buffer)
8046       (nnheader-insert-file-contents file)
8047       (goto-char (point-min))
8048       (unless (nnheader-article-p)
8049         ;; This doesn't look like an article, so we fudge some headers.
8050         (setq atts (file-attributes file)
8051               lines (count-lines (point-min) (point-max)))
8052         (insert "From: " (read-string "From: ") "\n"
8053                 "Subject: " (read-string "Subject: ") "\n"
8054                 "Date: " (message-make-date (nth 5 atts))
8055                 "\n"
8056                 "Message-ID: " (message-make-message-id) "\n"
8057                 "Lines: " (int-to-string lines) "\n"
8058                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8059       (gnus-request-accept-article group nil t)
8060       (kill-buffer (current-buffer)))))
8061
8062 (defun gnus-summary-article-posted-p ()
8063   "Say whether the current (mail) article is available from news as well.
8064 This will be the case if the article has both been mailed and posted."
8065   (interactive)
8066   (let ((id (mail-header-references (gnus-summary-article-header)))
8067         (gnus-override-method (car (gnus-refer-article-methods))))
8068     (if (gnus-request-head id "")
8069         (gnus-message 2 "The current message was found on %s"
8070                       gnus-override-method)
8071       (gnus-message 2 "The current message couldn't be found on %s"
8072                     gnus-override-method)
8073       nil)))
8074
8075 (defun gnus-summary-expire-articles (&optional now)
8076   "Expire all articles that are marked as expirable in the current group."
8077   (interactive)
8078   (when (gnus-check-backend-function
8079          'request-expire-articles gnus-newsgroup-name)
8080     ;; This backend supports expiry.
8081     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8082            (expirable (if total
8083                           (progn
8084                             ;; We need to update the info for
8085                             ;; this group for `gnus-list-of-read-articles'
8086                             ;; to give us the right answer.
8087                             (gnus-run-hooks 'gnus-exit-group-hook)
8088                             (gnus-summary-update-info)
8089                             (gnus-list-of-read-articles gnus-newsgroup-name))
8090                         (setq gnus-newsgroup-expirable
8091                               (sort gnus-newsgroup-expirable '<))))
8092            (expiry-wait (if now 'immediate
8093                           (gnus-group-find-parameter
8094                            gnus-newsgroup-name 'expiry-wait)))
8095            (nnmail-expiry-target
8096             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8097                 nnmail-expiry-target))
8098            es)
8099       (when expirable
8100         ;; There are expirable articles in this group, so we run them
8101         ;; through the expiry process.
8102         (gnus-message 6 "Expiring articles...")
8103         (unless (gnus-check-group gnus-newsgroup-name)
8104           (error "Can't open server for %s" gnus-newsgroup-name))
8105         ;; The list of articles that weren't expired is returned.
8106         (save-excursion
8107           (if expiry-wait
8108               (let ((nnmail-expiry-wait-function nil)
8109                     (nnmail-expiry-wait expiry-wait))
8110                 (setq es (gnus-request-expire-articles
8111                           expirable gnus-newsgroup-name)))
8112             (setq es (gnus-request-expire-articles
8113                       expirable gnus-newsgroup-name)))
8114           (unless total
8115             (setq gnus-newsgroup-expirable es))
8116           ;; We go through the old list of expirable, and mark all
8117           ;; really expired articles as nonexistent.
8118           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8119             (let ((gnus-use-cache nil))
8120               (while expirable
8121                 (unless (memq (car expirable) es)
8122                   (when (gnus-data-find (car expirable))
8123                     (gnus-summary-mark-article
8124                      (car expirable) gnus-canceled-mark)))
8125                 (setq expirable (cdr expirable))))))
8126         (gnus-message 6 "Expiring articles...done")))))
8127
8128 (defun gnus-summary-expire-articles-now ()
8129   "Expunge all expirable articles in the current group.
8130 This means that *all* articles that are marked as expirable will be
8131 deleted forever, right now."
8132   (interactive)
8133   (or gnus-expert-user
8134       (gnus-yes-or-no-p
8135        "Are you really, really, really sure you want to delete all these messages? ")
8136       (error "Phew!"))
8137   (gnus-summary-expire-articles t))
8138
8139 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8140 (defun gnus-summary-delete-article (&optional n)
8141   "Delete the N next (mail) articles.
8142 This command actually deletes articles.  This is not a marking
8143 command.  The article will disappear forever from your life, never to
8144 return.
8145 If N is negative, delete backwards.
8146 If N is nil and articles have been marked with the process mark,
8147 delete these instead."
8148   (interactive "P")
8149   (unless (gnus-check-backend-function 'request-expire-articles
8150                                        gnus-newsgroup-name)
8151     (error "The current newsgroup does not support article deletion"))
8152   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8153     (error "Couldn't open server"))
8154   ;; Compute the list of articles to delete.
8155   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8156         not-deleted)
8157     (if (and gnus-novice-user
8158              (not (gnus-yes-or-no-p
8159                    (format "Do you really want to delete %s forever? "
8160                            (if (> (length articles) 1)
8161                                (format "these %s articles" (length articles))
8162                              "this article")))))
8163         ()
8164       ;; Delete the articles.
8165       (setq not-deleted (gnus-request-expire-articles
8166                          articles gnus-newsgroup-name 'force))
8167       (while articles
8168         (gnus-summary-remove-process-mark (car articles))
8169         ;; The backend might not have been able to delete the article
8170         ;; after all.
8171         (unless (memq (car articles) not-deleted)
8172           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8173         (setq articles (cdr articles)))
8174       (when not-deleted
8175         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8176     (gnus-summary-position-point)
8177     (gnus-set-mode-line 'summary)
8178     not-deleted))
8179
8180 (defun gnus-summary-edit-article (&optional force)
8181   "Edit the current article.
8182 This will have permanent effect only in mail groups.
8183 If FORCE is non-nil, allow editing of articles even in read-only
8184 groups."
8185   (interactive "P")
8186   (save-excursion
8187     (set-buffer gnus-summary-buffer)
8188     (let ((mail-parse-charset gnus-newsgroup-charset)
8189           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8190       (gnus-set-global-variables)
8191       (when (and (not force)
8192                  (gnus-group-read-only-p))
8193         (error "The current newsgroup does not support article editing"))
8194       (gnus-summary-show-article t)
8195       (gnus-article-edit-article
8196        'ignore
8197        `(lambda (no-highlight)
8198           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8199                 (message-options message-options)
8200                 (message-options-set-recipient)
8201                 (mail-parse-ignored-charsets
8202                  ',gnus-newsgroup-ignored-charsets))
8203             (gnus-summary-edit-article-done
8204              ,(or (mail-header-references gnus-current-headers) "")
8205              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8206
8207 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8208
8209 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8210                                                  no-highlight)
8211   "Make edits to the current article permanent."
8212   (interactive)
8213   (save-excursion
8214     ;; The buffer restriction contains the entire article if it exists.
8215     (when (article-goto-body)
8216       (let ((lines (count-lines (point) (point-max)))
8217             (length (- (point-max) (point)))
8218             (case-fold-search t)
8219             (body (copy-marker (point))))
8220         (goto-char (point-min))
8221         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8222           (delete-region (match-beginning 1) (match-end 1))
8223           (insert (number-to-string length)))
8224         (goto-char (point-min))
8225         (when (re-search-forward
8226                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8227           (delete-region (match-beginning 1) (match-end 1))
8228           (insert (number-to-string length)))
8229         (goto-char (point-min))
8230         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8231           (delete-region (match-beginning 1) (match-end 1))
8232           (insert (number-to-string lines))))))
8233   ;; Replace the article.
8234   (let ((buf (current-buffer)))
8235     (with-temp-buffer
8236       (insert-buffer-substring buf)
8237
8238       (if (and (not read-only)
8239                (not (gnus-request-replace-article
8240                      (cdr gnus-article-current) (car gnus-article-current)
8241                      (current-buffer) t)))
8242           (error "Couldn't replace article")
8243         ;; Update the summary buffer.
8244         (if (and references
8245                  (equal (message-tokenize-header references " ")
8246                         (message-tokenize-header
8247                          (or (message-fetch-field "references") "") " ")))
8248             ;; We only have to update this line.
8249             (save-excursion
8250               (save-restriction
8251                 (message-narrow-to-head)
8252                 (let ((head (buffer-string))
8253                       header)
8254                   (with-temp-buffer
8255                     (insert (format "211 %d Article retrieved.\n"
8256                                     (cdr gnus-article-current)))
8257                     (insert head)
8258                     (insert ".\n")
8259                     (let ((nntp-server-buffer (current-buffer)))
8260                       (setq header (car (gnus-get-newsgroup-headers
8261                                          (save-excursion
8262                                            (set-buffer gnus-summary-buffer)
8263                                            gnus-newsgroup-dependencies)
8264                                          t))))
8265                     (save-excursion
8266                       (set-buffer gnus-summary-buffer)
8267                       (gnus-data-set-header
8268                        (gnus-data-find (cdr gnus-article-current))
8269                        header)
8270                       (gnus-summary-update-article-line
8271                        (cdr gnus-article-current) header))))))
8272           ;; Update threads.
8273           (set-buffer (or buffer gnus-summary-buffer))
8274           (gnus-summary-update-article (cdr gnus-article-current)))
8275         ;; Prettify the article buffer again.
8276         (unless no-highlight
8277           (save-excursion
8278             (set-buffer gnus-article-buffer)
8279             ;;;!!! Fix this -- article should be rehighlighted.
8280             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8281             (set-buffer gnus-original-article-buffer)
8282             (gnus-request-article
8283              (cdr gnus-article-current)
8284              (car gnus-article-current) (current-buffer))))
8285         ;; Prettify the summary buffer line.
8286         (when (gnus-visual-p 'summary-highlight 'highlight)
8287           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8288
8289 (defun gnus-summary-edit-wash (key)
8290   "Perform editing command KEY in the article buffer."
8291   (interactive
8292    (list
8293     (progn
8294       (message "%s" (concat (this-command-keys) "- "))
8295       (read-char))))
8296   (message "")
8297   (gnus-summary-edit-article)
8298   (execute-kbd-macro (concat (this-command-keys) key))
8299   (gnus-article-edit-done))
8300
8301 ;;; Respooling
8302
8303 (defun gnus-summary-respool-query (&optional silent trace)
8304   "Query where the respool algorithm would put this article."
8305   (interactive)
8306   (let (gnus-mark-article-hook)
8307     (gnus-summary-select-article)
8308     (save-excursion
8309       (set-buffer gnus-original-article-buffer)
8310       (save-restriction
8311         (message-narrow-to-head)
8312         (let ((groups (nnmail-article-group 'identity trace)))
8313           (unless silent
8314             (if groups
8315                 (message "This message would go to %s"
8316                          (mapconcat 'car groups ", "))
8317               (message "This message would go to no groups"))
8318             groups))))))
8319
8320 (defun gnus-summary-respool-trace ()
8321   "Trace where the respool algorithm would put this article.
8322 Display a buffer showing all fancy splitting patterns which matched."
8323   (interactive)
8324   (gnus-summary-respool-query nil t))
8325
8326 ;; Summary marking commands.
8327
8328 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8329   "Mark articles which has the same subject as read, and then select the next.
8330 If UNMARK is positive, remove any kind of mark.
8331 If UNMARK is negative, tick articles."
8332   (interactive "P")
8333   (when unmark
8334     (setq unmark (prefix-numeric-value unmark)))
8335   (let ((count
8336          (gnus-summary-mark-same-subject
8337           (gnus-summary-article-subject) unmark)))
8338     ;; Select next unread article.  If auto-select-same mode, should
8339     ;; select the first unread article.
8340     (gnus-summary-next-article t (and gnus-auto-select-same
8341                                       (gnus-summary-article-subject)))
8342     (gnus-message 7 "%d article%s marked as %s"
8343                   count (if (= count 1) " is" "s are")
8344                   (if unmark "unread" "read"))))
8345
8346 (defun gnus-summary-kill-same-subject (&optional unmark)
8347   "Mark articles which has the same subject as read.
8348 If UNMARK is positive, remove any kind of mark.
8349 If UNMARK is negative, tick articles."
8350   (interactive "P")
8351   (when unmark
8352     (setq unmark (prefix-numeric-value unmark)))
8353   (let ((count
8354          (gnus-summary-mark-same-subject
8355           (gnus-summary-article-subject) unmark)))
8356     ;; If marked as read, go to next unread subject.
8357     (when (null unmark)
8358       ;; Go to next unread subject.
8359       (gnus-summary-next-subject 1 t))
8360     (gnus-message 7 "%d articles are marked as %s"
8361                   count (if unmark "unread" "read"))))
8362
8363 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8364   "Mark articles with same SUBJECT as read, and return marked number.
8365 If optional argument UNMARK is positive, remove any kinds of marks.
8366 If optional argument UNMARK is negative, mark articles as unread instead."
8367   (let ((count 1))
8368     (save-excursion
8369       (cond
8370        ((null unmark)                   ; Mark as read.
8371         (while (and
8372                 (progn
8373                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8374                   (gnus-summary-show-thread) t)
8375                 (gnus-summary-find-subject subject))
8376           (setq count (1+ count))))
8377        ((> unmark 0)                    ; Tick.
8378         (while (and
8379                 (progn
8380                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8381                   (gnus-summary-show-thread) t)
8382                 (gnus-summary-find-subject subject))
8383           (setq count (1+ count))))
8384        (t                               ; Mark as unread.
8385         (while (and
8386                 (progn
8387                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8388                   (gnus-summary-show-thread) t)
8389                 (gnus-summary-find-subject subject))
8390           (setq count (1+ count)))))
8391       (gnus-set-mode-line 'summary)
8392       ;; Return the number of marked articles.
8393       count)))
8394
8395 (defun gnus-summary-mark-as-processable (n &optional unmark)
8396   "Set the process mark on the next N articles.
8397 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8398 the process mark instead.  The difference between N and the actual
8399 number of articles marked is returned."
8400   (interactive "p")
8401   (let ((backward (< n 0))
8402         (n (abs n)))
8403     (while (and
8404             (> n 0)
8405             (if unmark
8406                 (gnus-summary-remove-process-mark
8407                  (gnus-summary-article-number))
8408               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8409             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8410       (setq n (1- n)))
8411     (when (/= 0 n)
8412       (gnus-message 7 "No more articles"))
8413     (gnus-summary-recenter)
8414     (gnus-summary-position-point)
8415     n))
8416
8417 (defun gnus-summary-unmark-as-processable (n)
8418   "Remove the process mark from the next N articles.
8419 If N is negative, unmark backward instead.  The difference between N and
8420 the actual number of articles unmarked is returned."
8421   (interactive "p")
8422   (gnus-summary-mark-as-processable n t))
8423
8424 (defun gnus-summary-unmark-all-processable ()
8425   "Remove the process mark from all articles."
8426   (interactive)
8427   (save-excursion
8428     (while gnus-newsgroup-processable
8429       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8430   (gnus-summary-position-point))
8431
8432 (defun gnus-summary-add-mark (article type)
8433   "Mark ARTICLE with a mark of TYPE."
8434   (let ((vtype (car (assq type gnus-article-mark-lists)))
8435         var)
8436     (if (not vtype)
8437         (error "No such mark type: %s" type)
8438       (setq var (intern (format "gnus-newsgroup-%s" type)))
8439       (set var (cons article (symbol-value var)))
8440       (if (memq type '(processable cached replied saved))
8441           (gnus-summary-update-secondary-mark article)
8442         ;;; !!! This is bobus.  We should find out what primary
8443         ;;; !!! mark we want to set.
8444         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8445
8446 (defun gnus-summary-mark-as-expirable (n)
8447   "Mark N articles forward as expirable.
8448 If N is negative, mark backward instead.  The difference between N and
8449 the actual number of articles marked is returned."
8450   (interactive "p")
8451   (gnus-summary-mark-forward n gnus-expirable-mark))
8452
8453 (defun gnus-summary-mark-article-as-replied (article)
8454   "Mark ARTICLE replied and update the summary line."
8455   (push article gnus-newsgroup-replied)
8456   (let ((buffer-read-only nil))
8457     (when (gnus-summary-goto-subject article nil t)
8458       (gnus-summary-update-secondary-mark article))))
8459
8460 (defun gnus-summary-set-bookmark (article)
8461   "Set a bookmark in current article."
8462   (interactive (list (gnus-summary-article-number)))
8463   (when (or (not (get-buffer gnus-article-buffer))
8464             (not gnus-current-article)
8465             (not gnus-article-current)
8466             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8467     (error "No current article selected"))
8468   ;; Remove old bookmark, if one exists.
8469   (let ((old (assq article gnus-newsgroup-bookmarks)))
8470     (when old
8471       (setq gnus-newsgroup-bookmarks
8472             (delq old gnus-newsgroup-bookmarks))))
8473   ;; Set the new bookmark, which is on the form
8474   ;; (article-number . line-number-in-body).
8475   (push
8476    (cons article
8477          (save-excursion
8478            (set-buffer gnus-article-buffer)
8479            (count-lines
8480             (min (point)
8481                  (save-excursion
8482                    (goto-char (point-min))
8483                    (search-forward "\n\n" nil t)
8484                    (point)))
8485             (point))))
8486    gnus-newsgroup-bookmarks)
8487   (gnus-message 6 "A bookmark has been added to the current article."))
8488
8489 (defun gnus-summary-remove-bookmark (article)
8490   "Remove the bookmark from the current article."
8491   (interactive (list (gnus-summary-article-number)))
8492   ;; Remove old bookmark, if one exists.
8493   (let ((old (assq article gnus-newsgroup-bookmarks)))
8494     (if old
8495         (progn
8496           (setq gnus-newsgroup-bookmarks
8497                 (delq old gnus-newsgroup-bookmarks))
8498           (gnus-message 6 "Removed bookmark."))
8499       (gnus-message 6 "No bookmark in current article."))))
8500
8501 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8502 (defun gnus-summary-mark-as-dormant (n)
8503   "Mark N articles forward as dormant.
8504 If N is negative, mark backward instead.  The difference between N and
8505 the actual number of articles marked is returned."
8506   (interactive "p")
8507   (gnus-summary-mark-forward n gnus-dormant-mark))
8508
8509 (defun gnus-summary-set-process-mark (article)
8510   "Set the process mark on ARTICLE and update the summary line."
8511   (setq gnus-newsgroup-processable
8512         (cons article
8513               (delq article gnus-newsgroup-processable)))
8514   (when (gnus-summary-goto-subject article)
8515     (gnus-summary-show-thread)
8516     (gnus-summary-goto-subject article)
8517     (gnus-summary-update-secondary-mark article)))
8518
8519 (defun gnus-summary-remove-process-mark (article)
8520   "Remove the process mark from ARTICLE and update the summary line."
8521   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8522   (when (gnus-summary-goto-subject article)
8523     (gnus-summary-show-thread)
8524     (gnus-summary-goto-subject article)
8525     (gnus-summary-update-secondary-mark article)))
8526
8527 (defun gnus-summary-set-saved-mark (article)
8528   "Set the process mark on ARTICLE and update the summary line."
8529   (push article gnus-newsgroup-saved)
8530   (when (gnus-summary-goto-subject article)
8531     (gnus-summary-update-secondary-mark article)))
8532
8533 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8534   "Mark N articles as read forwards.
8535 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8536 The difference between N and the actual number of articles marked is
8537 returned.
8538 Iff NO-EXPIRE, auto-expiry will be inhibited."
8539   (interactive "p")
8540   (gnus-summary-show-thread)
8541   (let ((backward (< n 0))
8542         (gnus-summary-goto-unread
8543          (and gnus-summary-goto-unread
8544               (not (eq gnus-summary-goto-unread 'never))
8545               (not (memq mark (list gnus-unread-mark
8546                                     gnus-ticked-mark gnus-dormant-mark)))))
8547         (n (abs n))
8548         (mark (or mark gnus-del-mark)))
8549     (while (and (> n 0)
8550                 (gnus-summary-mark-article nil mark no-expire)
8551                 (zerop (gnus-summary-next-subject
8552                         (if backward -1 1)
8553                         (and gnus-summary-goto-unread
8554                              (not (eq gnus-summary-goto-unread 'never)))
8555                         t)))
8556       (setq n (1- n)))
8557     (when (/= 0 n)
8558       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8559     (gnus-summary-recenter)
8560     (gnus-summary-position-point)
8561     (gnus-set-mode-line 'summary)
8562     n))
8563
8564 (defun gnus-summary-mark-article-as-read (mark)
8565   "Mark the current article quickly as read with MARK."
8566   (let ((article (gnus-summary-article-number)))
8567     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8568     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8569     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8570     (push (cons article mark) gnus-newsgroup-reads)
8571     ;; Possibly remove from cache, if that is used.
8572     (when gnus-use-cache
8573       (gnus-cache-enter-remove-article article))
8574     ;; Allow the backend to change the mark.
8575     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8576     ;; Check for auto-expiry.
8577     (when (and gnus-newsgroup-auto-expire
8578                (memq mark gnus-auto-expirable-marks))
8579       (setq mark gnus-expirable-mark)
8580       ;; Let the backend know about the mark change.
8581       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8582       (push article gnus-newsgroup-expirable))
8583     ;; Set the mark in the buffer.
8584     (gnus-summary-update-mark mark 'unread)
8585     t))
8586
8587 (defun gnus-summary-mark-article-as-unread (mark)
8588   "Mark the current article quickly as unread with MARK."
8589   (let* ((article (gnus-summary-article-number))
8590          (old-mark (gnus-summary-article-mark article)))
8591     ;; Allow the backend to change the mark.
8592     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8593     (if (eq mark old-mark)
8594         t
8595       (if (<= article 0)
8596           (progn
8597             (gnus-error 1 "Can't mark negative article numbers")
8598             nil)
8599         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8600         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8601         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8602         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8603         (cond ((= mark gnus-ticked-mark)
8604                (push article gnus-newsgroup-marked))
8605               ((= mark gnus-dormant-mark)
8606                (push article gnus-newsgroup-dormant))
8607               (t
8608                (push article gnus-newsgroup-unreads)))
8609         (gnus-pull article gnus-newsgroup-reads)
8610
8611         ;; See whether the article is to be put in the cache.
8612         (and gnus-use-cache
8613              (vectorp (gnus-summary-article-header article))
8614              (save-excursion
8615                (gnus-cache-possibly-enter-article
8616                 gnus-newsgroup-name article
8617                 (gnus-summary-article-header article)
8618                 (= mark gnus-ticked-mark)
8619                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8620
8621         ;; Fix the mark.
8622         (gnus-summary-update-mark mark 'unread)
8623         t))))
8624
8625 (defun gnus-summary-mark-article (&optional article mark no-expire)
8626   "Mark ARTICLE with MARK.  MARK can be any character.
8627 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8628 `??' (dormant) and `?E' (expirable).
8629 If MARK is nil, then the default character `?r' is used.
8630 If ARTICLE is nil, then the article on the current line will be
8631 marked.
8632 Iff NO-EXPIRE, auto-expiry will be inhibited."
8633   ;; The mark might be a string.
8634   (when (stringp mark)
8635     (setq mark (aref mark 0)))
8636   ;; If no mark is given, then we check auto-expiring.
8637   (when (null mark)
8638     (setq mark gnus-del-mark))
8639   (when (and (not no-expire)
8640              gnus-newsgroup-auto-expire
8641              (memq mark gnus-auto-expirable-marks))
8642     (setq mark gnus-expirable-mark))
8643   (let ((article (or article (gnus-summary-article-number)))
8644         (old-mark (gnus-summary-article-mark article)))
8645     ;; Allow the backend to change the mark.
8646     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8647     (if (eq mark old-mark)
8648         t
8649       (unless article
8650         (error "No article on current line"))
8651       (if (not (if (or (= mark gnus-unread-mark)
8652                        (= mark gnus-ticked-mark)
8653                        (= mark gnus-dormant-mark))
8654                    (gnus-mark-article-as-unread article mark)
8655                  (gnus-mark-article-as-read article mark)))
8656           t
8657         ;; See whether the article is to be put in the cache.
8658         (and gnus-use-cache
8659              (not (= mark gnus-canceled-mark))
8660              (vectorp (gnus-summary-article-header article))
8661              (save-excursion
8662                (gnus-cache-possibly-enter-article
8663                 gnus-newsgroup-name article
8664                 (gnus-summary-article-header article)
8665                 (= mark gnus-ticked-mark)
8666                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8667
8668         (when (gnus-summary-goto-subject article nil t)
8669           (let ((buffer-read-only nil))
8670             (gnus-summary-show-thread)
8671             ;; Fix the mark.
8672             (gnus-summary-update-mark mark 'unread)
8673             t))))))
8674
8675 (defun gnus-summary-update-secondary-mark (article)
8676   "Update the secondary (read, process, cache) mark."
8677   (gnus-summary-update-mark
8678    (cond ((memq article gnus-newsgroup-processable)
8679           gnus-process-mark)
8680          ((memq article gnus-newsgroup-cached)
8681           gnus-cached-mark)
8682          ((memq article gnus-newsgroup-replied)
8683           gnus-replied-mark)
8684          ((memq article gnus-newsgroup-saved)
8685           gnus-saved-mark)
8686          (t gnus-no-mark))
8687    'replied)
8688   (when (gnus-visual-p 'summary-highlight 'highlight)
8689     (gnus-run-hooks 'gnus-summary-update-hook))
8690   t)
8691
8692 (defun gnus-summary-update-mark (mark type)
8693   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8694         (buffer-read-only nil))
8695     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8696     (when forward
8697       (when (looking-at "\r")
8698         (incf forward))
8699       (when (<= (+ forward (point)) (point-max))
8700         ;; Go to the right position on the line.
8701         (goto-char (+ forward (point)))
8702         ;; Replace the old mark with the new mark.
8703         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8704         ;; Optionally update the marks by some user rule.
8705         (when (eq type 'unread)
8706           (gnus-data-set-mark
8707            (gnus-data-find (gnus-summary-article-number)) mark)
8708           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8709
8710 (defun gnus-mark-article-as-read (article &optional mark)
8711   "Enter ARTICLE in the pertinent lists and remove it from others."
8712   ;; Make the article expirable.
8713   (let ((mark (or mark gnus-del-mark)))
8714     (if (= mark gnus-expirable-mark)
8715         (push article gnus-newsgroup-expirable)
8716       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8717     ;; Remove from unread and marked lists.
8718     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8719     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8720     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8721     (push (cons article mark) gnus-newsgroup-reads)
8722     ;; Possibly remove from cache, if that is used.
8723     (when gnus-use-cache
8724       (gnus-cache-enter-remove-article article))
8725     t))
8726
8727 (defun gnus-mark-article-as-unread (article &optional mark)
8728   "Enter ARTICLE in the pertinent lists and remove it from others."
8729   (let ((mark (or mark gnus-ticked-mark)))
8730     (if (<= article 0)
8731         (progn
8732           (gnus-error 1 "Can't mark negative article numbers")
8733           nil)
8734       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8735             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8736             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8737             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8738
8739       ;; Unsuppress duplicates?
8740       (when gnus-suppress-duplicates
8741         (gnus-dup-unsuppress-article article))
8742
8743       (cond ((= mark gnus-ticked-mark)
8744              (push article gnus-newsgroup-marked))
8745             ((= mark gnus-dormant-mark)
8746              (push article gnus-newsgroup-dormant))
8747             (t
8748              (push article gnus-newsgroup-unreads)))
8749       (gnus-pull article gnus-newsgroup-reads)
8750       t)))
8751
8752 (defalias 'gnus-summary-mark-as-unread-forward
8753   'gnus-summary-tick-article-forward)
8754 (make-obsolete 'gnus-summary-mark-as-unread-forward
8755                'gnus-summary-tick-article-forward)
8756 (defun gnus-summary-tick-article-forward (n)
8757   "Tick N articles forwards.
8758 If N is negative, tick backwards instead.
8759 The difference between N and the number of articles ticked is returned."
8760   (interactive "p")
8761   (gnus-summary-mark-forward n gnus-ticked-mark))
8762
8763 (defalias 'gnus-summary-mark-as-unread-backward
8764   'gnus-summary-tick-article-backward)
8765 (make-obsolete 'gnus-summary-mark-as-unread-backward
8766                'gnus-summary-tick-article-backward)
8767 (defun gnus-summary-tick-article-backward (n)
8768   "Tick N articles backwards.
8769 The difference between N and the number of articles ticked is returned."
8770   (interactive "p")
8771   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8772
8773 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8774 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8775 (defun gnus-summary-tick-article (&optional article clear-mark)
8776   "Mark current article as unread.
8777 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8778 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8779   (interactive)
8780   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8781                                        gnus-ticked-mark)))
8782
8783 (defun gnus-summary-mark-as-read-forward (n)
8784   "Mark N articles as read forwards.
8785 If N is negative, mark backwards instead.
8786 The difference between N and the actual number of articles marked is
8787 returned."
8788   (interactive "p")
8789   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8790
8791 (defun gnus-summary-mark-as-read-backward (n)
8792   "Mark the N articles as read backwards.
8793 The difference between N and the actual number of articles marked is
8794 returned."
8795   (interactive "p")
8796   (gnus-summary-mark-forward
8797    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8798
8799 (defun gnus-summary-mark-as-read (&optional article mark)
8800   "Mark current article as read.
8801 ARTICLE specifies the article to be marked as read.
8802 MARK specifies a string to be inserted at the beginning of the line."
8803   (gnus-summary-mark-article article mark))
8804
8805 (defun gnus-summary-clear-mark-forward (n)
8806   "Clear marks from N articles forward.
8807 If N is negative, clear backward instead.
8808 The difference between N and the number of marks cleared is returned."
8809   (interactive "p")
8810   (gnus-summary-mark-forward n gnus-unread-mark))
8811
8812 (defun gnus-summary-clear-mark-backward (n)
8813   "Clear marks from N articles backward.
8814 The difference between N and the number of marks cleared is returned."
8815   (interactive "p")
8816   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8817
8818 (defun gnus-summary-mark-unread-as-read ()
8819   "Intended to be used by `gnus-summary-mark-article-hook'."
8820   (when (memq gnus-current-article gnus-newsgroup-unreads)
8821     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8822
8823 (defun gnus-summary-mark-read-and-unread-as-read ()
8824   "Intended to be used by `gnus-summary-mark-article-hook'."
8825   (let ((mark (gnus-summary-article-mark)))
8826     (when (or (gnus-unread-mark-p mark)
8827               (gnus-read-mark-p mark))
8828       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8829
8830 (defun gnus-summary-mark-unread-as-ticked ()
8831    "Intended to be used by `gnus-summary-mark-article-hook'."
8832   (when (memq gnus-current-article gnus-newsgroup-unreads)
8833     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8834
8835 (defun gnus-summary-mark-region-as-read (point mark all)
8836   "Mark all unread articles between point and mark as read.
8837 If given a prefix, mark all articles between point and mark as read,
8838 even ticked and dormant ones."
8839   (interactive "r\nP")
8840   (save-excursion
8841     (let (article)
8842       (goto-char point)
8843       (beginning-of-line)
8844       (while (and
8845               (< (point) mark)
8846               (progn
8847                 (when (or all
8848                           (memq (setq article (gnus-summary-article-number))
8849                                 gnus-newsgroup-unreads))
8850                   (gnus-summary-mark-article article gnus-del-mark))
8851                 t)
8852               (gnus-summary-find-next))))))
8853
8854 (defun gnus-summary-mark-below (score mark)
8855   "Mark articles with score less than SCORE with MARK."
8856   (interactive "P\ncMark: ")
8857   (setq score (if score
8858                   (prefix-numeric-value score)
8859                 (or gnus-summary-default-score 0)))
8860   (save-excursion
8861     (set-buffer gnus-summary-buffer)
8862     (goto-char (point-min))
8863     (while
8864         (progn
8865           (and (< (gnus-summary-article-score) score)
8866                (gnus-summary-mark-article nil mark))
8867           (gnus-summary-find-next)))))
8868
8869 (defun gnus-summary-kill-below (&optional score)
8870   "Mark articles with score below SCORE as read."
8871   (interactive "P")
8872   (gnus-summary-mark-below score gnus-killed-mark))
8873
8874 (defun gnus-summary-clear-above (&optional score)
8875   "Clear all marks from articles with score above SCORE."
8876   (interactive "P")
8877   (gnus-summary-mark-above score gnus-unread-mark))
8878
8879 (defun gnus-summary-tick-above (&optional score)
8880   "Tick all articles with score above SCORE."
8881   (interactive "P")
8882   (gnus-summary-mark-above score gnus-ticked-mark))
8883
8884 (defun gnus-summary-mark-above (score mark)
8885   "Mark articles with score over SCORE with MARK."
8886   (interactive "P\ncMark: ")
8887   (setq score (if score
8888                   (prefix-numeric-value score)
8889                 (or gnus-summary-default-score 0)))
8890   (save-excursion
8891     (set-buffer gnus-summary-buffer)
8892     (goto-char (point-min))
8893     (while (and (progn
8894                   (when (> (gnus-summary-article-score) score)
8895                     (gnus-summary-mark-article nil mark))
8896                   t)
8897                 (gnus-summary-find-next)))))
8898
8899 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8900 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8901 (defun gnus-summary-limit-include-expunged (&optional no-error)
8902   "Display all the hidden articles that were expunged for low scores."
8903   (interactive)
8904   (let ((buffer-read-only nil))
8905     (let ((scored gnus-newsgroup-scored)
8906           headers h)
8907       (while scored
8908         (unless (gnus-number-to-header (caar scored))
8909           (and (setq h (gnus-summary-article-header (caar scored)))
8910                (< (cdar scored) gnus-summary-expunge-below)
8911                (push h headers)))
8912         (setq scored (cdr scored)))
8913       (if (not headers)
8914           (when (not no-error)
8915             (error "No expunged articles hidden"))
8916         (goto-char (point-min))
8917         (gnus-summary-prepare-unthreaded (nreverse headers))
8918         (goto-char (point-min))
8919         (gnus-summary-position-point)
8920         t))))
8921
8922 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8923   "Mark all unread articles in this newsgroup as read.
8924 If prefix argument ALL is non-nil, ticked and dormant articles will
8925 also be marked as read.
8926 If QUIETLY is non-nil, no questions will be asked.
8927 If TO-HERE is non-nil, it should be a point in the buffer.  All
8928 articles before this point will be marked as read.
8929 Note that this function will only catch up the unread article
8930 in the current summary buffer limitation.
8931 The number of articles marked as read is returned."
8932   (interactive "P")
8933   (prog1
8934       (save-excursion
8935         (when (or quietly
8936                   (not gnus-interactive-catchup) ;Without confirmation?
8937                   gnus-expert-user
8938                   (gnus-y-or-n-p
8939                    (if all
8940                        "Mark absolutely all articles as read? "
8941                      "Mark all unread articles as read? ")))
8942           (if (and not-mark
8943                    (not gnus-newsgroup-adaptive)
8944                    (not gnus-newsgroup-auto-expire)
8945                    (not gnus-suppress-duplicates)
8946                    (or (not gnus-use-cache)
8947                        (eq gnus-use-cache 'passive)))
8948               (progn
8949                 (when all
8950                   (setq gnus-newsgroup-marked nil
8951                         gnus-newsgroup-dormant nil))
8952                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8953             ;; We actually mark all articles as canceled, which we
8954             ;; have to do when using auto-expiry or adaptive scoring.
8955             (gnus-summary-show-all-threads)
8956             (when (gnus-summary-first-subject (not all) t)
8957               (while (and
8958                       (if to-here (< (point) to-here) t)
8959                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8960                       (gnus-summary-find-next (not all) nil nil t))))
8961             (gnus-set-mode-line 'summary))
8962           t))
8963     (gnus-summary-position-point)))
8964
8965 (defun gnus-summary-catchup-to-here (&optional all)
8966   "Mark all unticked articles before the current one as read.
8967 If ALL is non-nil, also mark ticked and dormant articles as read."
8968   (interactive "P")
8969   (save-excursion
8970     (gnus-save-hidden-threads
8971       (let ((beg (point)))
8972         ;; We check that there are unread articles.
8973         (when (or all (gnus-summary-find-prev))
8974           (gnus-summary-catchup all t beg)))))
8975   (gnus-summary-position-point))
8976
8977 (defun gnus-summary-catchup-all (&optional quietly)
8978   "Mark all articles in this newsgroup as read."
8979   (interactive "P")
8980   (gnus-summary-catchup t quietly))
8981
8982 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8983   "Mark all unread articles in this group as read, then exit.
8984 If prefix argument ALL is non-nil, all articles are marked as read."
8985   (interactive "P")
8986   (when (gnus-summary-catchup all quietly nil 'fast)
8987     ;; Select next newsgroup or exit.
8988     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8989              (eq gnus-auto-select-next 'quietly))
8990         (gnus-summary-next-group nil)
8991       (gnus-summary-exit))))
8992
8993 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8994   "Mark all articles in this newsgroup as read, and then exit."
8995   (interactive "P")
8996   (gnus-summary-catchup-and-exit t quietly))
8997
8998 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8999   "Mark all articles in this group as read and select the next group.
9000 If given a prefix, mark all articles, unread as well as ticked, as
9001 read."
9002   (interactive "P")
9003   (save-excursion
9004     (gnus-summary-catchup all))
9005   (gnus-summary-next-group))
9006
9007 ;;;
9008 ;;; with article
9009 ;;;
9010
9011 (defmacro gnus-with-article (article &rest forms)
9012   "Select ARTICLE and perform FORMS in the original article buffer.
9013 Then replace the article with the result."
9014   `(progn
9015      ;; We don't want the article to be marked as read.
9016      (let (gnus-mark-article-hook)
9017        (gnus-summary-select-article t t nil ,article))
9018      (set-buffer gnus-original-article-buffer)
9019      ,@forms
9020      (if (not (gnus-check-backend-function
9021                'request-replace-article (car gnus-article-current)))
9022          (gnus-message 5 "Read-only group; not replacing")
9023        (unless (gnus-request-replace-article
9024                 ,article (car gnus-article-current)
9025                 (current-buffer) t)
9026          (error "Couldn't replace article")))
9027      ;; The cache and backlog have to be flushed somewhat.
9028      (when gnus-keep-backlog
9029        (gnus-backlog-remove-article
9030         (car gnus-article-current) (cdr gnus-article-current)))
9031      (when gnus-use-cache
9032        (gnus-cache-update-article
9033         (car gnus-article-current) (cdr gnus-article-current)))))
9034
9035 (put 'gnus-with-article 'lisp-indent-function 1)
9036 (put 'gnus-with-article 'edebug-form-spec '(form body))
9037
9038 ;; Thread-based commands.
9039
9040 (defun gnus-summary-articles-in-thread (&optional article)
9041   "Return a list of all articles in the current thread.
9042 If ARTICLE is non-nil, return all articles in the thread that starts
9043 with that article."
9044   (let* ((article (or article (gnus-summary-article-number)))
9045          (data (gnus-data-find-list article))
9046          (top-level (gnus-data-level (car data)))
9047          (top-subject
9048           (cond ((null gnus-thread-operation-ignore-subject)
9049                  (gnus-simplify-subject-re
9050                   (mail-header-subject (gnus-data-header (car data)))))
9051                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9052                  (gnus-simplify-subject-fuzzy
9053                   (mail-header-subject (gnus-data-header (car data)))))
9054                 (t nil)))
9055          (end-point (save-excursion
9056                       (if (gnus-summary-go-to-next-thread)
9057                           (point) (point-max))))
9058          articles)
9059     (while (and data
9060                 (< (gnus-data-pos (car data)) end-point))
9061       (when (or (not top-subject)
9062                 (string= top-subject
9063                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9064                              (gnus-simplify-subject-fuzzy
9065                               (mail-header-subject
9066                                (gnus-data-header (car data))))
9067                            (gnus-simplify-subject-re
9068                             (mail-header-subject
9069                              (gnus-data-header (car data)))))))
9070         (push (gnus-data-number (car data)) articles))
9071       (unless (and (setq data (cdr data))
9072                    (> (gnus-data-level (car data)) top-level))
9073         (setq data nil)))
9074     ;; Return the list of articles.
9075     (nreverse articles)))
9076
9077 (defun gnus-summary-rethread-current ()
9078   "Rethread the thread the current article is part of."
9079   (interactive)
9080   (let* ((gnus-show-threads t)
9081          (article (gnus-summary-article-number))
9082          (id (mail-header-id (gnus-summary-article-header)))
9083          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9084     (unless id
9085       (error "No article on the current line"))
9086     (gnus-rebuild-thread id)
9087     (gnus-summary-goto-subject article)))
9088
9089 (defun gnus-summary-reparent-thread ()
9090   "Make the current article child of the marked (or previous) article.
9091
9092 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9093 is non-nil or the Subject: of both articles are the same."
9094   (interactive)
9095   (unless (not (gnus-group-read-only-p))
9096     (error "The current newsgroup does not support article editing"))
9097   (unless (<= (length gnus-newsgroup-processable) 1)
9098     (error "No more than one article may be marked"))
9099   (save-window-excursion
9100     (let ((gnus-article-buffer " *reparent*")
9101           (current-article (gnus-summary-article-number))
9102           ;; First grab the marked article, otherwise one line up.
9103           (parent-article (if (not (null gnus-newsgroup-processable))
9104                               (car gnus-newsgroup-processable)
9105                             (save-excursion
9106                               (if (eq (forward-line -1) 0)
9107                                   (gnus-summary-article-number)
9108                                 (error "Beginning of summary buffer"))))))
9109       (unless (not (eq current-article parent-article))
9110         (error "An article may not be self-referential"))
9111       (let ((message-id (mail-header-id
9112                          (gnus-summary-article-header parent-article))))
9113         (unless (and message-id (not (equal message-id "")))
9114           (error "No message-id in desired parent"))
9115         (gnus-with-article current-article
9116           (save-restriction
9117             (goto-char (point-min))
9118             (message-narrow-to-head)
9119             (if (re-search-forward "^References: " nil t)
9120                 (progn
9121                   (re-search-forward "^[^ \t]" nil t)
9122                   (forward-line -1)
9123                   (end-of-line)
9124                   (insert " " message-id))
9125               (insert "References: " message-id "\n"))))
9126         (set-buffer gnus-summary-buffer)
9127         (gnus-summary-unmark-all-processable)
9128         (gnus-summary-update-article current-article)
9129         (gnus-summary-rethread-current)
9130         (gnus-message 3 "Article %d is now the child of article %d"
9131                       current-article parent-article)))))
9132
9133 (defun gnus-summary-toggle-threads (&optional arg)
9134   "Toggle showing conversation threads.
9135 If ARG is positive number, turn showing conversation threads on."
9136   (interactive "P")
9137   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9138     (setq gnus-show-threads
9139           (if (null arg) (not gnus-show-threads)
9140             (> (prefix-numeric-value arg) 0)))
9141     (gnus-summary-prepare)
9142     (gnus-summary-goto-subject current)
9143     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9144     (gnus-summary-position-point)))
9145
9146 (defun gnus-summary-show-all-threads ()
9147   "Show all threads."
9148   (interactive)
9149   (save-excursion
9150     (let ((buffer-read-only nil))
9151       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9152   (gnus-summary-position-point))
9153
9154 (defun gnus-summary-show-thread ()
9155   "Show thread subtrees.
9156 Returns nil if no thread was there to be shown."
9157   (interactive)
9158   (let ((buffer-read-only nil)
9159         (orig (point))
9160         ;; first goto end then to beg, to have point at beg after let
9161         (end (progn (end-of-line) (point)))
9162         (beg (progn (beginning-of-line) (point))))
9163     (prog1
9164         ;; Any hidden lines here?
9165         (search-forward "\r" end t)
9166       (subst-char-in-region beg end ?\^M ?\n t)
9167       (goto-char orig)
9168       (gnus-summary-position-point))))
9169
9170 (defun gnus-summary-hide-all-threads ()
9171   "Hide all thread subtrees."
9172   (interactive)
9173   (save-excursion
9174     (goto-char (point-min))
9175     (gnus-summary-hide-thread)
9176     (while (zerop (gnus-summary-next-thread 1 t))
9177       (gnus-summary-hide-thread)))
9178   (gnus-summary-position-point))
9179
9180 (defun gnus-summary-hide-thread ()
9181   "Hide thread subtrees.
9182 Returns nil if no threads were there to be hidden."
9183   (interactive)
9184   (let ((buffer-read-only nil)
9185         (start (point))
9186         (article (gnus-summary-article-number)))
9187     (goto-char start)
9188     ;; Go forward until either the buffer ends or the subthread
9189     ;; ends.
9190     (when (and (not (eobp))
9191                (or (zerop (gnus-summary-next-thread 1 t))
9192                    (goto-char (point-max))))
9193       (prog1
9194           (if (and (> (point) start)
9195                    (search-backward "\n" start t))
9196               (progn
9197                 (subst-char-in-region start (point) ?\n ?\^M)
9198                 (gnus-summary-goto-subject article))
9199             (goto-char start)
9200             nil)))))
9201
9202 (defun gnus-summary-go-to-next-thread (&optional previous)
9203   "Go to the same level (or less) next thread.
9204 If PREVIOUS is non-nil, go to previous thread instead.
9205 Return the article number moved to, or nil if moving was impossible."
9206   (let ((level (gnus-summary-thread-level))
9207         (way (if previous -1 1))
9208         (beg (point)))
9209     (forward-line way)
9210     (while (and (not (eobp))
9211                 (< level (gnus-summary-thread-level)))
9212       (forward-line way))
9213     (if (eobp)
9214         (progn
9215           (goto-char beg)
9216           nil)
9217       (setq beg (point))
9218       (prog1
9219           (gnus-summary-article-number)
9220         (goto-char beg)))))
9221
9222 (defun gnus-summary-next-thread (n &optional silent)
9223   "Go to the same level next N'th thread.
9224 If N is negative, search backward instead.
9225 Returns the difference between N and the number of skips actually
9226 done.
9227
9228 If SILENT, don't output messages."
9229   (interactive "p")
9230   (let ((backward (< n 0))
9231         (n (abs n)))
9232     (while (and (> n 0)
9233                 (gnus-summary-go-to-next-thread backward))
9234       (decf n))
9235     (unless silent
9236       (gnus-summary-position-point))
9237     (when (and (not silent) (/= 0 n))
9238       (gnus-message 7 "No more threads"))
9239     n))
9240
9241 (defun gnus-summary-prev-thread (n)
9242   "Go to the same level previous N'th thread.
9243 Returns the difference between N and the number of skips actually
9244 done."
9245   (interactive "p")
9246   (gnus-summary-next-thread (- n)))
9247
9248 (defun gnus-summary-go-down-thread ()
9249   "Go down one level in the current thread."
9250   (let ((children (gnus-summary-article-children)))
9251     (when children
9252       (gnus-summary-goto-subject (car children)))))
9253
9254 (defun gnus-summary-go-up-thread ()
9255   "Go up one level in the current thread."
9256   (let ((parent (gnus-summary-article-parent)))
9257     (when parent
9258       (gnus-summary-goto-subject parent))))
9259
9260 (defun gnus-summary-down-thread (n)
9261   "Go down thread N steps.
9262 If N is negative, go up instead.
9263 Returns the difference between N and how many steps down that were
9264 taken."
9265   (interactive "p")
9266   (let ((up (< n 0))
9267         (n (abs n)))
9268     (while (and (> n 0)
9269                 (if up (gnus-summary-go-up-thread)
9270                   (gnus-summary-go-down-thread)))
9271       (setq n (1- n)))
9272     (gnus-summary-position-point)
9273     (when (/= 0 n)
9274       (gnus-message 7 "Can't go further"))
9275     n))
9276
9277 (defun gnus-summary-up-thread (n)
9278   "Go up thread N steps.
9279 If N is negative, go up instead.
9280 Returns the difference between N and how many steps down that were
9281 taken."
9282   (interactive "p")
9283   (gnus-summary-down-thread (- n)))
9284
9285 (defun gnus-summary-top-thread ()
9286   "Go to the top of the thread."
9287   (interactive)
9288   (while (gnus-summary-go-up-thread))
9289   (gnus-summary-article-number))
9290
9291 (defun gnus-summary-kill-thread (&optional unmark)
9292   "Mark articles under current thread as read.
9293 If the prefix argument is positive, remove any kinds of marks.
9294 If the prefix argument is negative, tick articles instead."
9295   (interactive "P")
9296   (when unmark
9297     (setq unmark (prefix-numeric-value unmark)))
9298   (let ((articles (gnus-summary-articles-in-thread)))
9299     (save-excursion
9300       ;; Expand the thread.
9301       (gnus-summary-show-thread)
9302       ;; Mark all the articles.
9303       (while articles
9304         (gnus-summary-goto-subject (car articles))
9305         (cond ((null unmark)
9306                (gnus-summary-mark-article-as-read gnus-killed-mark))
9307               ((> unmark 0)
9308                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9309               (t
9310                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9311         (setq articles (cdr articles))))
9312     ;; Hide killed subtrees.
9313     (and (null unmark)
9314          gnus-thread-hide-killed
9315          (gnus-summary-hide-thread))
9316     ;; If marked as read, go to next unread subject.
9317     (when (null unmark)
9318       ;; Go to next unread subject.
9319       (gnus-summary-next-subject 1 t)))
9320   (gnus-set-mode-line 'summary))
9321
9322 ;; Summary sorting commands
9323
9324 (defun gnus-summary-sort-by-number (&optional reverse)
9325   "Sort the summary buffer by article number.
9326 Argument REVERSE means reverse order."
9327   (interactive "P")
9328   (gnus-summary-sort 'number reverse))
9329
9330 (defun gnus-summary-sort-by-author (&optional reverse)
9331   "Sort the summary buffer by author name alphabetically.
9332 If `case-fold-search' is non-nil, case of letters is ignored.
9333 Argument REVERSE means reverse order."
9334   (interactive "P")
9335   (gnus-summary-sort 'author reverse))
9336
9337 (defun gnus-summary-sort-by-subject (&optional reverse)
9338   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9339 If `case-fold-search' is non-nil, case of letters is ignored.
9340 Argument REVERSE means reverse order."
9341   (interactive "P")
9342   (gnus-summary-sort 'subject reverse))
9343
9344 (defun gnus-summary-sort-by-date (&optional reverse)
9345   "Sort the summary buffer by date.
9346 Argument REVERSE means reverse order."
9347   (interactive "P")
9348   (gnus-summary-sort 'date reverse))
9349
9350 (defun gnus-summary-sort-by-score (&optional reverse)
9351   "Sort the summary buffer by score.
9352 Argument REVERSE means reverse order."
9353   (interactive "P")
9354   (gnus-summary-sort 'score reverse))
9355
9356 (defun gnus-summary-sort-by-lines (&optional reverse)
9357   "Sort the summary buffer by the number of lines.
9358 Argument REVERSE means reverse order."
9359   (interactive "P")
9360   (gnus-summary-sort 'lines reverse))
9361
9362 (defun gnus-summary-sort-by-chars (&optional reverse)
9363   "Sort the summary buffer by article length.
9364 Argument REVERSE means reverse order."
9365   (interactive "P")
9366   (gnus-summary-sort 'chars reverse))
9367
9368 (defun gnus-summary-sort (predicate reverse)
9369   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9370   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9371          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9372          (gnus-thread-sort-functions
9373           (if (not reverse)
9374               thread
9375             `(lambda (t1 t2)
9376                (,thread t2 t1))))
9377          (gnus-sort-gathered-threads-function
9378           gnus-thread-sort-functions)
9379          (gnus-article-sort-functions
9380           (if (not reverse)
9381               article
9382             `(lambda (t1 t2)
9383                (,article t2 t1))))
9384          (buffer-read-only)
9385          (gnus-summary-prepare-hook nil))
9386     ;; We do the sorting by regenerating the threads.
9387     (gnus-summary-prepare)
9388     ;; Hide subthreads if needed.
9389     (when (and gnus-show-threads gnus-thread-hide-subtree)
9390       (gnus-summary-hide-all-threads))))
9391
9392 ;; Summary saving commands.
9393
9394 (defun gnus-summary-save-article (&optional n not-saved)
9395   "Save the current article using the default saver function.
9396 If N is a positive number, save the N next articles.
9397 If N is a negative number, save the N previous articles.
9398 If N is nil and any articles have been marked with the process mark,
9399 save those articles instead.
9400 The variable `gnus-default-article-saver' specifies the saver function."
9401   (interactive "P")
9402   (let* ((articles (gnus-summary-work-articles n))
9403          (save-buffer (save-excursion
9404                         (nnheader-set-temp-buffer " *Gnus Save*")))
9405          (num (length articles))
9406          header file)
9407     (dolist (article articles)
9408       (setq header (gnus-summary-article-header article))
9409       (if (not (vectorp header))
9410           ;; This is a pseudo-article.
9411           (if (assq 'name header)
9412               (gnus-copy-file (cdr (assq 'name header)))
9413             (gnus-message 1 "Article %d is unsaveable" article))
9414         ;; This is a real article.
9415         (save-window-excursion
9416           (gnus-summary-select-article t nil nil article))
9417         (save-excursion
9418           (set-buffer save-buffer)
9419           (erase-buffer)
9420           (insert-buffer-substring gnus-original-article-buffer))
9421         (setq file (gnus-article-save save-buffer file num))
9422         (gnus-summary-remove-process-mark article)
9423         (unless not-saved
9424           (gnus-summary-set-saved-mark article))))
9425     (gnus-kill-buffer save-buffer)
9426     (gnus-summary-position-point)
9427     (gnus-set-mode-line 'summary)
9428     n))
9429
9430 (defun gnus-summary-pipe-output (&optional arg)
9431   "Pipe the current article to a subprocess.
9432 If N is a positive number, pipe the N next articles.
9433 If N is a negative number, pipe the N previous articles.
9434 If N is nil and any articles have been marked with the process mark,
9435 pipe those articles instead."
9436   (interactive "P")
9437   (require 'gnus-art)
9438   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9439     (gnus-summary-save-article arg t))
9440   (gnus-configure-windows 'pipe))
9441
9442 (defun gnus-summary-save-article-mail (&optional arg)
9443   "Append the current article to an mail file.
9444 If N is a positive number, save the N next articles.
9445 If N is a negative number, save the N previous articles.
9446 If N is nil and any articles have been marked with the process mark,
9447 save those articles instead."
9448   (interactive "P")
9449   (require 'gnus-art)
9450   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9451     (gnus-summary-save-article arg)))
9452
9453 (defun gnus-summary-save-article-rmail (&optional arg)
9454   "Append the current article to an rmail file.
9455 If N is a positive number, save the N next articles.
9456 If N is a negative number, save the N previous articles.
9457 If N is nil and any articles have been marked with the process mark,
9458 save those articles instead."
9459   (interactive "P")
9460   (require 'gnus-art)
9461   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9462     (gnus-summary-save-article arg)))
9463
9464 (defun gnus-summary-save-article-file (&optional arg)
9465   "Append the current article to a file.
9466 If N is a positive number, save the N next articles.
9467 If N is a negative number, save the N previous articles.
9468 If N is nil and any articles have been marked with the process mark,
9469 save those articles instead."
9470   (interactive "P")
9471   (require 'gnus-art)
9472   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9473     (gnus-summary-save-article arg)))
9474
9475 (defun gnus-summary-write-article-file (&optional arg)
9476   "Write the current article to a file, deleting the previous file.
9477 If N is a positive number, save the N next articles.
9478 If N is a negative number, save the N previous articles.
9479 If N is nil and any articles have been marked with the process mark,
9480 save those articles instead."
9481   (interactive "P")
9482   (require 'gnus-art)
9483   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9484     (gnus-summary-save-article arg)))
9485
9486 (defun gnus-summary-save-article-body-file (&optional arg)
9487   "Append the current article body to a file.
9488 If N is a positive number, save the N next articles.
9489 If N is a negative number, save the N previous articles.
9490 If N is nil and any articles have been marked with the process mark,
9491 save those articles instead."
9492   (interactive "P")
9493   (require 'gnus-art)
9494   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9495     (gnus-summary-save-article arg)))
9496
9497 (defun gnus-summary-pipe-message (program)
9498   "Pipe the current article through PROGRAM."
9499   (interactive "sProgram: ")
9500   (gnus-summary-select-article)
9501   (let ((mail-header-separator ""))
9502     (gnus-eval-in-buffer-window gnus-article-buffer
9503       (save-restriction
9504         (widen)
9505         (let ((start (window-start))
9506               buffer-read-only)
9507           (message-pipe-buffer-body program)
9508           (set-window-start (get-buffer-window (current-buffer)) start))))))
9509
9510 (defun gnus-get-split-value (methods)
9511   "Return a value based on the split METHODS."
9512   (let (split-name method result match)
9513     (when methods
9514       (save-excursion
9515         (set-buffer gnus-original-article-buffer)
9516         (save-restriction
9517           (nnheader-narrow-to-headers)
9518           (while (and methods (not split-name))
9519             (goto-char (point-min))
9520             (setq method (pop methods))
9521             (setq match (car method))
9522             (when (cond
9523                    ((stringp match)
9524                     ;; Regular expression.
9525                     (ignore-errors
9526                       (re-search-forward match nil t)))
9527                    ((gnus-functionp match)
9528                     ;; Function.
9529                     (save-restriction
9530                       (widen)
9531                       (setq result (funcall match gnus-newsgroup-name))))
9532                    ((consp match)
9533                     ;; Form.
9534                     (save-restriction
9535                       (widen)
9536                       (setq result (eval match)))))
9537               (setq split-name (cdr method))
9538               (cond ((stringp result)
9539                      (push (expand-file-name
9540                             result gnus-article-save-directory)
9541                            split-name))
9542                     ((consp result)
9543                      (setq split-name (append result split-name)))))))))
9544     (nreverse split-name)))
9545
9546 (defun gnus-valid-move-group-p (group)
9547   (and (boundp group)
9548        (symbol-name group)
9549        (symbol-value group)
9550        (gnus-get-function (gnus-find-method-for-group
9551                            (symbol-name group)) 'request-accept-article t)))
9552
9553 (defun gnus-read-move-group-name (prompt default articles prefix)
9554   "Read a group name."
9555   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9556          (minibuffer-confirm-incomplete nil) ; XEmacs
9557          (prom
9558           (format "%s %s to:"
9559                   prompt
9560                   (if (> (length articles) 1)
9561                       (format "these %d articles" (length articles))
9562                     "this article")))
9563          (to-newsgroup
9564           (cond
9565            ((null split-name)
9566             (gnus-completing-read default prom
9567                                   gnus-active-hashtb
9568                                   'gnus-valid-move-group-p
9569                                   nil prefix
9570                                   'gnus-group-history))
9571            ((= 1 (length split-name))
9572             (gnus-completing-read (car split-name) prom
9573                                   gnus-active-hashtb
9574                                   'gnus-valid-move-group-p
9575                                   nil nil
9576                                   'gnus-group-history))
9577            (t
9578             (gnus-completing-read nil prom
9579                                   (mapcar (lambda (el) (list el))
9580                                           (nreverse split-name))
9581                                   nil nil nil
9582                                   'gnus-group-history))))
9583          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9584     (when to-newsgroup
9585       (if (or (string= to-newsgroup "")
9586               (string= to-newsgroup prefix))
9587           (setq to-newsgroup default))
9588       (unless to-newsgroup
9589         (error "No group name entered"))
9590       (or (gnus-active to-newsgroup)
9591           (gnus-activate-group to-newsgroup nil nil to-method)
9592           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9593                                      to-newsgroup))
9594               (or (and (gnus-request-create-group to-newsgroup to-method)
9595                        (gnus-activate-group
9596                         to-newsgroup nil nil to-method)
9597                        (gnus-subscribe-group to-newsgroup))
9598                   (error "Couldn't create group %s" to-newsgroup)))
9599           (error "No such group: %s" to-newsgroup)))
9600     to-newsgroup))
9601
9602 (defun gnus-summary-save-parts (type dir n &optional reverse)
9603   "Save parts matching TYPE to DIR.
9604 If REVERSE, save parts that do not match TYPE."
9605   (interactive
9606    (list (read-string "Save parts of type: "
9607                       (or (car gnus-summary-save-parts-type-history)
9608                           gnus-summary-save-parts-default-mime)
9609                       'gnus-summary-save-parts-type-history)
9610          (setq gnus-summary-save-parts-last-directory
9611                (read-file-name "Save to directory: "
9612                                gnus-summary-save-parts-last-directory
9613                                nil t))
9614          current-prefix-arg))
9615   (gnus-summary-iterate n
9616     (let ((gnus-display-mime-function nil)
9617           (gnus-inhibit-treatment t))
9618       (gnus-summary-select-article))
9619     (save-excursion
9620       (set-buffer gnus-article-buffer)
9621       (let ((handles (or gnus-article-mime-handles
9622                          (mm-dissect-buffer) (mm-uu-dissect))))
9623         (when handles
9624           (gnus-summary-save-parts-1 type dir handles reverse)
9625           (unless gnus-article-mime-handles ;; Don't destroy this case.
9626             (mm-destroy-parts handles)))))))
9627
9628 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9629   (if (stringp (car handle))
9630       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9631               (cdr handle))
9632     (when (if reverse
9633               (not (string-match type (mm-handle-media-type handle)))
9634             (string-match type (mm-handle-media-type handle)))
9635       (let ((file (expand-file-name
9636                    (file-name-nondirectory
9637                     (or
9638                      (mail-content-type-get
9639                       (mm-handle-disposition handle) 'filename)
9640                      (concat gnus-newsgroup-name
9641                              "." (number-to-string
9642                                   (cdr gnus-article-current)))))
9643                    dir)))
9644         (unless (file-exists-p file)
9645           (mm-save-part-to-file handle file))))))
9646
9647 ;; Summary extract commands
9648
9649 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9650   (let ((buffer-read-only nil)
9651         (article (gnus-summary-article-number))
9652         after-article b e)
9653     (unless (gnus-summary-goto-subject article)
9654       (error "No such article: %d" article))
9655     (gnus-summary-position-point)
9656     ;; If all commands are to be bunched up on one line, we collect
9657     ;; them here.
9658     (unless gnus-view-pseudos-separately
9659       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9660             files action)
9661         (while ps
9662           (setq action (cdr (assq 'action (car ps))))
9663           (setq files (list (cdr (assq 'name (car ps)))))
9664           (while (and ps (cdr ps)
9665                       (string= (or action "1")
9666                                (or (cdr (assq 'action (cadr ps))) "2")))
9667             (push (cdr (assq 'name (cadr ps))) files)
9668             (setcdr ps (cddr ps)))
9669           (when files
9670             (when (not (string-match "%s" action))
9671               (push " " files))
9672             (push " " files)
9673             (when (assq 'execute (car ps))
9674               (setcdr (assq 'execute (car ps))
9675                       (funcall (if (string-match "%s" action)
9676                                    'format 'concat)
9677                                action
9678                                (mapconcat
9679                                 (lambda (f)
9680                                   (if (equal f " ")
9681                                       f
9682                                     (gnus-quote-arg-for-sh-or-csh f)))
9683                                 files " ")))))
9684           (setq ps (cdr ps)))))
9685     (if (and gnus-view-pseudos (not not-view))
9686         (while pslist
9687           (when (assq 'execute (car pslist))
9688             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9689                                   (eq gnus-view-pseudos 'not-confirm)))
9690           (setq pslist (cdr pslist)))
9691       (save-excursion
9692         (while pslist
9693           (setq after-article (or (cdr (assq 'article (car pslist)))
9694                                   (gnus-summary-article-number)))
9695           (gnus-summary-goto-subject after-article)
9696           (forward-line 1)
9697           (setq b (point))
9698           (insert "    " (file-name-nondirectory
9699                           (cdr (assq 'name (car pslist))))
9700                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9701           (setq e (point))
9702           (forward-line -1)             ; back to `b'
9703           (gnus-add-text-properties
9704            b (1- e) (list 'gnus-number gnus-reffed-article-number
9705                           gnus-mouse-face-prop gnus-mouse-face))
9706           (gnus-data-enter
9707            after-article gnus-reffed-article-number
9708            gnus-unread-mark b (car pslist) 0 (- e b))
9709           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9710           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9711           (setq pslist (cdr pslist)))))))
9712
9713 (defun gnus-pseudos< (p1 p2)
9714   (let ((c1 (cdr (assq 'action p1)))
9715         (c2 (cdr (assq 'action p2))))
9716     (and c1 c2 (string< c1 c2))))
9717
9718 (defun gnus-request-pseudo-article (props)
9719   (cond ((assq 'execute props)
9720          (gnus-execute-command (cdr (assq 'execute props)))))
9721   (let ((gnus-current-article (gnus-summary-article-number)))
9722     (gnus-run-hooks 'gnus-mark-article-hook)))
9723
9724 (defun gnus-execute-command (command &optional automatic)
9725   (save-excursion
9726     (gnus-article-setup-buffer)
9727     (set-buffer gnus-article-buffer)
9728     (setq buffer-read-only nil)
9729     (let ((command (if automatic command
9730                      (read-string "Command: " (cons command 0)))))
9731       (erase-buffer)
9732       (insert "$ " command "\n\n")
9733       (if gnus-view-pseudo-asynchronously
9734           (start-process "gnus-execute" (current-buffer) shell-file-name
9735                          shell-command-switch command)
9736         (call-process shell-file-name nil t nil
9737                       shell-command-switch command)))))
9738
9739 ;; Summary kill commands.
9740
9741 (defun gnus-summary-edit-global-kill (article)
9742   "Edit the \"global\" kill file."
9743   (interactive (list (gnus-summary-article-number)))
9744   (gnus-group-edit-global-kill article))
9745
9746 (defun gnus-summary-edit-local-kill ()
9747   "Edit a local kill file applied to the current newsgroup."
9748   (interactive)
9749   (setq gnus-current-headers (gnus-summary-article-header))
9750   (gnus-group-edit-local-kill
9751    (gnus-summary-article-number) gnus-newsgroup-name))
9752
9753 ;;; Header reading.
9754
9755 (defun gnus-read-header (id &optional header)
9756   "Read the headers of article ID and enter them into the Gnus system."
9757   (let ((group gnus-newsgroup-name)
9758         (gnus-override-method
9759          (or
9760           gnus-override-method
9761           (and (gnus-news-group-p gnus-newsgroup-name)
9762                (car (gnus-refer-article-methods)))))
9763         where)
9764     ;; First we check to see whether the header in question is already
9765     ;; fetched.
9766     (if (stringp id)
9767         ;; This is a Message-ID.
9768         (setq header (or header (gnus-id-to-header id)))
9769       ;; This is an article number.
9770       (setq header (or header (gnus-summary-article-header id))))
9771     (if (and header
9772              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9773         ;; We have found the header.
9774         header
9775       ;; If this is a sparse article, we have to nix out its
9776       ;; previous entry in the thread hashtb.
9777       (when (and header
9778                  (gnus-summary-article-sparse-p (mail-header-number header)))
9779         (let* ((parent (gnus-parent-id (mail-header-references header)))
9780                (thread (and parent (gnus-id-to-thread parent))))
9781           (when thread
9782             (delq (assq header thread) thread))))
9783       ;; We have to really fetch the header to this article.
9784       (save-excursion
9785         (set-buffer nntp-server-buffer)
9786         (when (setq where (gnus-request-head id group))
9787           (nnheader-fold-continuation-lines)
9788           (goto-char (point-max))
9789           (insert ".\n")
9790           (goto-char (point-min))
9791           (insert "211 ")
9792           (princ (cond
9793                   ((numberp id) id)
9794                   ((cdr where) (cdr where))
9795                   (header (mail-header-number header))
9796                   (t gnus-reffed-article-number))
9797                  (current-buffer))
9798           (insert " Article retrieved.\n"))
9799         (if (or (not where)
9800                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9801             ()                          ; Malformed head.
9802           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9803             (when (and (stringp id)
9804                        (not (string= (gnus-group-real-name group)
9805                                      (car where))))
9806               ;; If we fetched by Message-ID and the article came
9807               ;; from a different group, we fudge some bogus article
9808               ;; numbers for this article.
9809               (mail-header-set-number header gnus-reffed-article-number))
9810             (save-excursion
9811               (set-buffer gnus-summary-buffer)
9812               (decf gnus-reffed-article-number)
9813               (gnus-remove-header (mail-header-number header))
9814               (push header gnus-newsgroup-headers)
9815               (setq gnus-current-headers header)
9816               (push (mail-header-number header) gnus-newsgroup-limit)))
9817           header)))))
9818
9819 (defun gnus-remove-header (number)
9820   "Remove header NUMBER from `gnus-newsgroup-headers'."
9821   (if (and gnus-newsgroup-headers
9822            (= number (mail-header-number (car gnus-newsgroup-headers))))
9823       (pop gnus-newsgroup-headers)
9824     (let ((headers gnus-newsgroup-headers))
9825       (while (and (cdr headers)
9826                   (not (= number (mail-header-number (cadr headers)))))
9827         (pop headers))
9828       (when (cdr headers)
9829         (setcdr headers (cddr headers))))))
9830
9831 ;;;
9832 ;;; summary highlights
9833 ;;;
9834
9835 (defun gnus-highlight-selected-summary ()
9836   "Highlight selected article in summary buffer."
9837   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9838   (when gnus-summary-selected-face
9839     (save-excursion
9840       (let* ((beg (progn (beginning-of-line) (point)))
9841              (end (progn (end-of-line) (point)))
9842              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9843              (from (if (get-text-property beg gnus-mouse-face-prop)
9844                        beg
9845                      (or (next-single-property-change
9846                           beg gnus-mouse-face-prop nil end)
9847                          beg)))
9848              (to
9849               (if (= from end)
9850                   (- from 2)
9851                 (or (next-single-property-change
9852                      from gnus-mouse-face-prop nil end)
9853                     end))))
9854         ;; If no mouse-face prop on line we will have to = from = end,
9855         ;; so we highlight the entire line instead.
9856         (when (= (+ to 2) from)
9857           (setq from beg)
9858           (setq to end))
9859         (if gnus-newsgroup-selected-overlay
9860             ;; Move old overlay.
9861             (gnus-move-overlay
9862              gnus-newsgroup-selected-overlay from to (current-buffer))
9863           ;; Create new overlay.
9864           (gnus-overlay-put
9865            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9866            'face gnus-summary-selected-face))))))
9867
9868 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9869 (defun gnus-summary-highlight-line ()
9870   "Highlight current line according to `gnus-summary-highlight'."
9871   (let* ((list gnus-summary-highlight)
9872          (p (point))
9873          (end (progn (end-of-line) (point)))
9874          ;; now find out where the line starts and leave point there.
9875          (beg (progn (beginning-of-line) (point)))
9876          (article (gnus-summary-article-number))
9877          (score (or (cdr (assq (or article gnus-current-article)
9878                                gnus-newsgroup-scored))
9879                     gnus-summary-default-score 0))
9880          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9881          (inhibit-read-only t))
9882     ;; Eval the cars of the lists until we find a match.
9883     (let ((default gnus-summary-default-score))
9884       (while (and list
9885                   (not (eval (caar list))))
9886         (setq list (cdr list))))
9887     (let ((face (cdar list)))
9888       (unless (eq face (get-text-property beg 'face))
9889         (gnus-put-text-property-excluding-characters-with-faces
9890          beg end 'face
9891          (setq face (if (boundp face) (symbol-value face) face)))
9892         (when gnus-summary-highlight-line-function
9893           (funcall gnus-summary-highlight-line-function article face))))
9894     (goto-char p)))
9895
9896 (defun gnus-update-read-articles (group unread &optional compute)
9897   "Update the list of read articles in GROUP."
9898   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9899          (entry (gnus-gethash group gnus-newsrc-hashtb))
9900          (info (nth 2 entry))
9901          (prev 1)
9902          (unread (sort (copy-sequence unread) '<))
9903          read)
9904     (if (or (not info) (not active))
9905         ;; There is no info on this group if it was, in fact,
9906         ;; killed.  Gnus stores no information on killed groups, so
9907         ;; there's nothing to be done.
9908         ;; One could store the information somewhere temporarily,
9909         ;; perhaps...  Hmmm...
9910         ()
9911       ;; Remove any negative articles numbers.
9912       (while (and unread (< (car unread) 0))
9913         (setq unread (cdr unread)))
9914       ;; Remove any expired article numbers
9915       (while (and unread (< (car unread) (car active)))
9916         (setq unread (cdr unread)))
9917       ;; Compute the ranges of read articles by looking at the list of
9918       ;; unread articles.
9919       (while unread
9920         (when (/= (car unread) prev)
9921           (push (if (= prev (1- (car unread))) prev
9922                   (cons prev (1- (car unread))))
9923                 read))
9924         (setq prev (1+ (car unread)))
9925         (setq unread (cdr unread)))
9926       (when (<= prev (cdr active))
9927         (push (cons prev (cdr active)) read))
9928       (setq read (if (> (length read) 1) (nreverse read) read))
9929       (if compute
9930           read
9931         (save-excursion
9932           (let (setmarkundo)
9933             ;; Propagate the read marks to the backend.
9934             (when (gnus-check-backend-function 'request-set-mark group)
9935               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9936                     (add (gnus-remove-from-range read (gnus-info-read info))))
9937                 (when (or add del)
9938                   (unless (gnus-check-group group)
9939                     (error "Can't open server for %s" group))
9940                   (gnus-request-set-mark
9941                    group (delq nil (list (if add (list add 'add '(read)))
9942                                          (if del (list del 'del '(read))))))
9943                   (setq setmarkundo
9944                         `(gnus-request-set-mark
9945                           ,group
9946                           ',(delq nil (list
9947                                        (if del (list del 'add '(read)))
9948                                        (if add (list add 'del '(read))))))))))
9949             (set-buffer gnus-group-buffer)
9950             (gnus-undo-register
9951               `(progn
9952                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9953                  (gnus-info-set-read ',info ',(gnus-info-read info))
9954                  (gnus-get-unread-articles-in-group ',info
9955                                                     (gnus-active ,group))
9956                  (gnus-group-update-group ,group t)
9957                  ,setmarkundo))))
9958         ;; Enter this list into the group info.
9959         (gnus-info-set-read info read)
9960         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9961         (gnus-get-unread-articles-in-group info (gnus-active group))
9962         t))))
9963
9964 (defun gnus-offer-save-summaries ()
9965   "Offer to save all active summary buffers."
9966   (save-excursion
9967     (let ((buflist (buffer-list))
9968           buffers bufname)
9969       ;; Go through all buffers and find all summaries.
9970       (while buflist
9971         (and (setq bufname (buffer-name (car buflist)))
9972              (string-match "Summary" bufname)
9973              (save-excursion
9974                (set-buffer bufname)
9975                ;; We check that this is, indeed, a summary buffer.
9976                (and (eq major-mode 'gnus-summary-mode)
9977                     ;; Also make sure this isn't bogus.
9978                     gnus-newsgroup-prepared
9979                     ;; Also make sure that this isn't a dead summary buffer.
9980                     (not gnus-dead-summary-mode)))
9981              (push bufname buffers))
9982         (setq buflist (cdr buflist)))
9983       ;; Go through all these summary buffers and offer to save them.
9984       (when buffers
9985         (map-y-or-n-p
9986          "Update summary buffer %s? "
9987          (lambda (buf)
9988            (switch-to-buffer buf)
9989            (gnus-summary-exit))
9990          buffers)))))
9991
9992
9993 ;;; @ for mime-partial
9994 ;;;
9995
9996 (defun gnus-request-partial-message ()
9997   (save-excursion
9998     (let ((number (gnus-summary-article-number))
9999           (group gnus-newsgroup-name)
10000           (mother gnus-article-buffer))
10001       (set-buffer (get-buffer-create " *Partial Article*"))
10002       (erase-buffer)
10003       (setq mime-preview-buffer mother)
10004       (gnus-request-article-this-buffer number group)
10005       (mime-parse-buffer)
10006       )))
10007
10008 (autoload 'mime-combine-message/partial-pieces-automatically
10009   "mime-partial"
10010   "Internal method to combine message/partial messages automatically.")
10011
10012 (mime-add-condition
10013  'action '((type . message)(subtype . partial)
10014            (major-mode . gnus-original-article-mode)
10015            (method . mime-combine-message/partial-pieces-automatically)
10016            (summary-buffer-exp . gnus-summary-buffer)
10017            (request-partial-message-method . gnus-request-partial-message)
10018            ))
10019
10020
10021 ;;; @ for message/rfc822
10022 ;;;
10023
10024 (defun gnus-mime-extract-message/rfc822 (entity situation)
10025   (let (group article num cwin swin cur)
10026     (with-temp-buffer
10027       (mime-insert-entity-content entity)
10028       (setq group (or (cdr (assq 'group situation))
10029                       (completing-read "Group: "
10030                                        gnus-active-hashtb
10031                                        nil
10032                                        (gnus-read-active-file-p)
10033                                        gnus-newsgroup-name))
10034             article (gnus-request-accept-article group)))
10035     (when (and (consp article)
10036                (numberp (setq article (cdr article))))
10037       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10038             cwin (get-buffer-window (current-buffer) t))
10039       (save-window-excursion
10040         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10041             (select-window swin)
10042           (set-buffer gnus-summary-buffer))
10043         (setq cur gnus-current-article)
10044         (forward-line num)
10045         (let (gnus-show-threads)
10046           (gnus-summary-goto-subject article t))
10047         (gnus-summary-clear-mark-forward 1)
10048         (gnus-summary-goto-subject cur))
10049       (when (and cwin (window-frame cwin))
10050         (select-frame (window-frame cwin)))
10051       (when (boundp 'mime-acting-situation-to-override)
10052         (set-alist 'mime-acting-situation-to-override
10053                    'group
10054                    group)
10055         (set-alist 'mime-acting-situation-to-override
10056                    'after-method
10057                    `(progn
10058                       (save-current-buffer
10059                         (set-buffer gnus-group-buffer)
10060                         (gnus-activate-group ,group))
10061                       (gnus-summary-goto-article ,cur
10062                                                  gnus-show-all-headers)))
10063         (set-alist 'mime-acting-situation-to-override
10064                    'number num)))))
10065
10066 (mime-add-condition
10067  'action '((type . message)(subtype . rfc822)
10068            (major-mode . gnus-original-article-mode)
10069            (method . gnus-mime-extract-message/rfc822)
10070            (mode . "extract")
10071            ))
10072
10073 (mime-add-condition
10074  'action '((type . message)(subtype . news)
10075            (major-mode . gnus-original-article-mode)
10076            (method . gnus-mime-extract-message/rfc822)
10077            (mode . "extract")
10078            ))
10079
10080 (defun gnus-mime-extract-multipart (entity situation)
10081   (let ((children (mime-entity-children entity))
10082         mime-acting-situation-to-override
10083         f)
10084     (while children
10085       (mime-play-entity (car children)
10086                         (cons (assq 'mode situation)
10087                               mime-acting-situation-to-override))
10088       (setq children (cdr children)))
10089     (if (setq f (cdr (assq 'after-method
10090                            mime-acting-situation-to-override)))
10091         (eval f)
10092       )))
10093
10094 (mime-add-condition
10095  'action '((type . multipart)
10096            (method . gnus-mime-extract-multipart)
10097            (mode . "extract")
10098            )
10099  'with-default)
10100
10101
10102 ;;; @ end
10103 ;;;
10104
10105 (defun gnus-summary-setup-default-charset ()
10106   "Setup newsgroup default charset."
10107   (if (equal gnus-newsgroup-name "nndraft:drafts")
10108       (setq gnus-newsgroup-charset nil)
10109     (let* ((name (and gnus-newsgroup-name
10110                       (gnus-group-real-name gnus-newsgroup-name)))
10111            (ignored-charsets
10112             (or gnus-newsgroup-ephemeral-ignored-charsets
10113                 (append
10114                  (and gnus-newsgroup-name
10115                       (or (gnus-group-find-parameter gnus-newsgroup-name
10116                                                      'ignored-charsets t)
10117                           (let ((alist gnus-group-ignored-charsets-alist)
10118                                 elem (charsets nil))
10119                             (while (setq elem (pop alist))
10120                               (when (and name
10121                                          (string-match (car elem) name))
10122                                 (setq alist nil
10123                                       charsets (cdr elem))))
10124                             charsets)))
10125                  gnus-newsgroup-ignored-charsets))))
10126       (setq gnus-newsgroup-charset
10127             (or gnus-newsgroup-ephemeral-charset
10128                 (and gnus-newsgroup-name
10129                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
10130                          (let ((alist gnus-group-charset-alist)
10131                                elem charset)
10132                            (while (setq elem (pop alist))
10133                              (when (and name
10134                                         (string-match (car elem) name))
10135                                (setq alist nil
10136                                      charset (cadr elem))))
10137                            charset)))
10138                 gnus-default-charset))
10139       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10140            ignored-charsets))))
10141
10142 ;;;
10143 ;;; Mime Commands
10144 ;;;
10145
10146 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10147   "Display the current article buffer fully MIME-buttonized.
10148 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10149 treated as multipart/mixed."
10150   (interactive "P")
10151   (require 'gnus-art)
10152   (let ((gnus-unbuttonized-mime-types nil)
10153         (gnus-mime-display-multipart-as-mixed show-all-parts))
10154     (gnus-summary-show-article)))
10155
10156 (defun gnus-summary-repair-multipart (article)
10157   "Add a Content-Type header to a multipart article without one."
10158   (interactive (list (gnus-summary-article-number)))
10159   (gnus-with-article article
10160     (message-narrow-to-head)
10161     (message-remove-header "Mime-Version")
10162     (goto-char (point-max))
10163     (insert "Mime-Version: 1.0\n")
10164     (widen)
10165     (when (search-forward "\n--" nil t)
10166       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10167         (message-narrow-to-head)
10168         (message-remove-header "Content-Type")
10169         (goto-char (point-max))
10170         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10171                         separator))
10172         (widen))))
10173   (let (gnus-mark-article-hook)
10174     (gnus-summary-select-article t t nil article)))
10175
10176 (defun gnus-summary-toggle-display-buttonized ()
10177   "Toggle the buttonizing of the article buffer."
10178   (interactive)
10179   (require 'gnus-art)
10180   (if (setq gnus-inhibit-mime-unbuttonizing
10181             (not gnus-inhibit-mime-unbuttonizing))
10182       (let ((gnus-unbuttonized-mime-types nil))
10183         (gnus-summary-show-article))
10184     (gnus-summary-show-article)))
10185
10186 ;;;
10187 ;;; Intelli-mouse commmands
10188 ;;;
10189
10190 (defun gnus-wheel-summary-scroll (event)
10191   (interactive "e")
10192   (let ((amount (if (memq 'shift (event-modifiers event))
10193                     (car gnus-wheel-scroll-amount)
10194                   (cdr gnus-wheel-scroll-amount)))
10195         (direction (- (* (static-if (featurep 'xemacs)
10196                              (event-button event)
10197                            (cond ((eq 'mouse-4 (event-basic-type event))
10198                                   4)
10199                                  ((eq 'mouse-5 (event-basic-type event))
10200                                   5)))
10201                          2) 9))
10202         edge)
10203     (gnus-summary-scroll-up (* amount direction))
10204     (when (gnus-eval-in-buffer-window gnus-article-buffer
10205             (save-restriction
10206               (widen)
10207               (and (if (< 0 direction)
10208                        (gnus-article-next-page 0)
10209                      (gnus-article-prev-page 0)
10210                      (bobp))
10211                    (if (setq edge (get-text-property
10212                                    (point-min) 'gnus-wheel-edge))
10213                        (setq edge (* edge direction))
10214                      (setq edge -1))
10215                    (or (plusp edge)
10216                        (let ((buffer-read-only nil)
10217                              (inhibit-read-only t))
10218                          (put-text-property (point-min) (point-max)
10219                                             'gnus-wheel-edge direction)
10220                          nil))
10221                    (or (> edge gnus-wheel-edge-resistance)
10222                        (let ((buffer-read-only nil)
10223                              (inhibit-read-only t))
10224                          (put-text-property (point-min) (point-max)
10225                                             'gnus-wheel-edge
10226                                             (* (1+ edge) direction))
10227                          nil))
10228                    (eq last-command 'gnus-wheel-summary-scroll))))
10229       (gnus-summary-next-article nil nil (minusp direction)))))
10230
10231 (defun gnus-wheel-install ()
10232   "Enable mouse wheel support on summary window."
10233   (when gnus-use-wheel
10234     (let ((keys
10235            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10236       (dolist (key keys)
10237         (define-key gnus-summary-mode-map key
10238           'gnus-wheel-summary-scroll)))))
10239
10240 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10241
10242 ;;;
10243 ;;; Traditional PGP commmands
10244 ;;;
10245
10246 (defun gnus-summary-decrypt-article (&optional force)
10247   "Decrypt the current article in traditional PGP way.
10248 This will have permanent effect only in mail groups.
10249 If FORCE is non-nil, allow editing of articles even in read-only
10250 groups."
10251   (interactive "P")
10252   (gnus-summary-select-article t)
10253   (gnus-eval-in-buffer-window gnus-article-buffer
10254     (save-excursion
10255       (save-restriction
10256         (widen)
10257         (goto-char (point-min))
10258         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10259           (error "Not a traditional PGP message!"))
10260         (let ((armor-start (match-beginning 0)))
10261           (if (and (pgg-decrypt-region armor-start (point-max))
10262                    (or force (not (gnus-group-read-only-p))))
10263               (let ((inhibit-read-only t)
10264                     buffer-read-only)
10265                 (delete-region armor-start
10266                                (progn
10267                                  (re-search-forward "^-+END PGP" nil t)
10268                                  (beginning-of-line 2)
10269                                  (point)))
10270                 (insert-buffer-substring pgg-output-buffer))))))))
10271
10272 (defun gnus-summary-verify-article ()
10273   "Verify the current article in traditional PGP way."
10274   (interactive)
10275   (save-excursion
10276     (set-buffer gnus-original-article-buffer)
10277     (goto-char (point-min))
10278     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10279       (error "Not a traditional PGP message!"))
10280     (re-search-forward "^-+END PGP" nil t)
10281     (beginning-of-line 2)
10282     (call-interactively (function pgg-verify-region))))
10283
10284 ;;;
10285 ;;; Generic summary marking commands
10286 ;;;
10287
10288 (defvar gnus-summary-marking-alist
10289   '((read gnus-del-mark "d")
10290     (unread gnus-unread-mark "u")
10291     (ticked gnus-ticked-mark "!")
10292     (dormant gnus-dormant-mark "?")
10293     (expirable gnus-expirable-mark "e"))
10294   "An alist of names/marks/keystrokes.")
10295
10296 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10297 (defvar gnus-summary-mark-map)
10298
10299 (defun gnus-summary-make-all-marking-commands ()
10300   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10301   (dolist (elem gnus-summary-marking-alist)
10302     (apply 'gnus-summary-make-marking-command elem)))
10303
10304 (defun gnus-summary-make-marking-command (name mark keystroke)
10305   (let ((map (make-sparse-keymap)))
10306     (define-key gnus-summary-generic-mark-map keystroke map)
10307     (dolist (lway `((next "next" next nil "n")
10308                     (next-unread "next unread" next t "N")
10309                     (prev "previous" prev nil "p")
10310                     (prev-unread "previous unread" prev t "P")
10311                     (nomove "" nil nil ,keystroke)))
10312       (let ((func (gnus-summary-make-marking-command-1
10313                    mark (car lway) lway name)))
10314         (setq func (eval func))
10315         (define-key map (nth 4 lway) func)))))
10316
10317 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10318   `(defun ,(intern
10319             (format "gnus-summary-put-mark-as-%s%s"
10320                     name (if (eq way 'nomove)
10321                              ""
10322                            (concat "-" (symbol-name way)))))
10323      (n)
10324      ,(format
10325        "Mark the current article as %s%s.
10326 If N, the prefix, then repeat N times.
10327 If N is negative, move in reverse order.
10328 The difference between N and the actual number of articles marked is
10329 returned."
10330        name (car (cdr lway)))
10331      (interactive "p")
10332      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10333
10334 (defun gnus-summary-generic-mark (n mark move unread)
10335   "Mark N articles with MARK."
10336   (unless (eq major-mode 'gnus-summary-mode)
10337     (error "This command can only be used in the summary buffer"))
10338   (gnus-summary-show-thread)
10339   (let ((nummove
10340          (cond
10341           ((eq move 'next) 1)
10342           ((eq move 'prev) -1)
10343           (t 0))))
10344     (if (zerop nummove)
10345         (setq n 1)
10346       (when (< n 0)
10347         (setq n (abs n)
10348               nummove (* -1 nummove))))
10349     (while (and (> n 0)
10350                 (gnus-summary-mark-article nil mark)
10351                 (zerop (gnus-summary-next-subject nummove unread t)))
10352       (setq n (1- n)))
10353     (when (/= 0 n)
10354       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10355     (gnus-summary-recenter)
10356     (gnus-summary-position-point)
10357     (gnus-set-mode-line 'summary)
10358     n))
10359
10360 (gnus-summary-make-all-marking-commands)
10361
10362 (gnus-ems-redefine)
10363
10364 (provide 'gnus-sum)
10365
10366 (run-hooks 'gnus-sum-load-hook)
10367
10368 ;;; gnus-sum.el ends here