1f256c22aea0c16c947b6bc88a87fe43b97eb088
[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     (dolist (header gnus-newsgroup-headers)
4289       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4290                                   " *\\)\\)+\\(Re: +\\)?\\)")
4291                           (mail-header-subject header))
4292         (mail-header-set-subject
4293          header (concat (substring (mail-header-subject header)
4294                                    0 (match-beginning 1))
4295                         (or
4296                          (match-string 3 (mail-header-subject header))
4297                          (match-string 5 (mail-header-subject header)))
4298                         (substring (mail-header-subject header)
4299                                    (match-end 1))))))))
4300
4301 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4302   "Select newsgroup GROUP.
4303 If READ-ALL is non-nil, all articles in the group are selected.
4304 If SELECT-ARTICLES, only select those articles from GROUP."
4305   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4306          ;;!!! Dirty hack; should be removed.
4307          (gnus-summary-ignore-duplicates
4308           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4309               t
4310             gnus-summary-ignore-duplicates))
4311          (info (nth 2 entry))
4312          articles fetched-articles cached)
4313
4314     (unless (gnus-check-server
4315              (setq gnus-current-select-method
4316                    (gnus-find-method-for-group group)))
4317       (error "Couldn't open server"))
4318
4319     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4320         (gnus-activate-group group)     ; Or we can activate it...
4321         (progn                          ; Or we bug out.
4322           (when (equal major-mode 'gnus-summary-mode)
4323             (kill-buffer (current-buffer)))
4324           (error "Couldn't activate group %s: %s"
4325                  group (gnus-status-message group))))
4326
4327     (unless (gnus-request-group group t)
4328       (when (equal major-mode 'gnus-summary-mode)
4329         (kill-buffer (current-buffer)))
4330       (error "Couldn't request group %s: %s"
4331              group (gnus-status-message group)))
4332
4333     (setq gnus-newsgroup-name group)
4334     (setq gnus-newsgroup-unselected nil)
4335     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4336     (gnus-summary-setup-default-charset)
4337
4338     ;; Adjust and set lists of article marks.
4339     (when info
4340       (gnus-adjust-marked-articles info))
4341
4342     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4343     (setq cached
4344           (if (gnus-virtual-group-p group)
4345               gnus-newsgroup-cached
4346             (gnus-cache-articles-in-group group)))
4347
4348     (setq gnus-newsgroup-unreads
4349           (gnus-set-difference
4350            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4351            gnus-newsgroup-dormant))
4352
4353     (setq gnus-newsgroup-processable nil)
4354
4355     (gnus-update-read-articles group gnus-newsgroup-unreads)
4356
4357     (if (setq articles select-articles)
4358         (setq gnus-newsgroup-unselected
4359               (gnus-sorted-intersection
4360                gnus-newsgroup-unreads
4361                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4362       (setq articles (gnus-articles-to-read group read-all)))
4363
4364     (cond
4365      ((null articles)
4366       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4367       'quit)
4368      ((eq articles 0) nil)
4369      (t
4370       ;; Init the dependencies hash table.
4371       (setq gnus-newsgroup-dependencies
4372             (gnus-make-hashtable (length articles)))
4373       (gnus-set-global-variables)
4374       ;; Retrieve the headers and read them in.
4375       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4376       (setq gnus-newsgroup-headers
4377             (gnus-retrieve-parsed-headers
4378              articles gnus-newsgroup-name
4379              ;; We might want to fetch old headers, but
4380              ;; not if there is only 1 article.
4381              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4382                            (not (numberp gnus-fetch-old-headers)))
4383                       (> (length articles) 1))
4384                   gnus-fetch-old-headers)))
4385       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4386
4387       ;; Suppress duplicates?
4388       (when gnus-suppress-duplicates
4389         (gnus-dup-suppress-articles))
4390
4391       ;; Set the initial limit.
4392       (setq gnus-newsgroup-limit (copy-sequence articles))
4393       ;; Remove canceled articles from the list of unread articles.
4394       (setq gnus-newsgroup-unreads
4395             (gnus-set-sorted-intersection
4396              gnus-newsgroup-unreads
4397              (setq fetched-articles
4398                    (mapcar (lambda (headers) (mail-header-number headers))
4399                            gnus-newsgroup-headers))))
4400       ;; Removed marked articles that do not exist.
4401       (gnus-update-missing-marks
4402        (gnus-sorted-complement fetched-articles articles))
4403
4404       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4405       (when cached
4406         (setq gnus-newsgroup-cached cached))
4407
4408       ;; We might want to build some more threads first.
4409       (when (and gnus-fetch-old-headers
4410                  (eq gnus-headers-retrieved-by 'nov))
4411         (if (eq gnus-fetch-old-headers 'invisible)
4412             (gnus-build-all-threads)
4413           (gnus-build-old-threads)))
4414       ;; Let the Gnus agent mark articles as read.
4415       (when gnus-agent
4416         (gnus-agent-get-undownloaded-list))
4417       ;; Remove list identifiers from subject
4418       (when gnus-list-identifiers
4419         (gnus-summary-remove-list-identifiers))
4420       ;; Check whether auto-expire is to be done in this group.
4421       (setq gnus-newsgroup-auto-expire
4422             (gnus-group-auto-expirable-p group))
4423       ;; Set up the article buffer now, if necessary.
4424       (unless gnus-single-article-buffer
4425         (gnus-article-setup-buffer))
4426       ;; First and last article in this newsgroup.
4427       (when gnus-newsgroup-headers
4428         (setq gnus-newsgroup-begin
4429               (mail-header-number (car gnus-newsgroup-headers))
4430               gnus-newsgroup-end
4431               (mail-header-number
4432                (gnus-last-element gnus-newsgroup-headers))))
4433       ;; GROUP is successfully selected.
4434       (or gnus-newsgroup-headers t)))))
4435
4436 (defun gnus-articles-to-read (group &optional read-all)
4437   "Find out what articles the user wants to read."
4438   (let* ((articles
4439           ;; Select all articles if `read-all' is non-nil, or if there
4440           ;; are no unread articles.
4441           (if (or read-all
4442                   (and (zerop (length gnus-newsgroup-marked))
4443                        (zerop (length gnus-newsgroup-unreads)))
4444                   (eq (gnus-group-find-parameter group 'display)
4445                       'all))
4446               (or
4447                (gnus-uncompress-range (gnus-active group))
4448                (gnus-cache-articles-in-group group))
4449             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4450                           (copy-sequence gnus-newsgroup-unreads))
4451                   '<)))
4452          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4453          (scored (length scored-list))
4454          (number (length articles))
4455          (marked (+ (length gnus-newsgroup-marked)
4456                     (length gnus-newsgroup-dormant)))
4457          (select
4458           (cond
4459            ((numberp read-all)
4460             read-all)
4461            (t
4462             (condition-case ()
4463                 (cond
4464                  ((and (or (<= scored marked) (= scored number))
4465                        (natnump gnus-large-newsgroup)
4466                        (> number gnus-large-newsgroup))
4467                   (let* ((cursor-in-echo-area nil)
4468                          (input (read-from-minibuffer
4469                                  (format
4470                                   "How many articles from %s (max %d): "
4471                                   (gnus-limit-string gnus-newsgroup-name 35)
4472                                   number)
4473                                  (cons (number-to-string gnus-large-newsgroup)
4474                                        0))))
4475                     (if (string-match "^[ \t]*$" input)
4476                         number
4477                       input)))
4478                  ((and (> scored marked) (< scored number)
4479                        (> (- scored number) 20))
4480                   (let ((input
4481                          (read-string
4482                           (format "%s %s (%d scored, %d total): "
4483                                   "How many articles from"
4484                                   group scored number))))
4485                     (if (string-match "^[ \t]*$" input)
4486                         number input)))
4487                  (t number))
4488               (quit
4489                (message "Quit getting the articles to read")
4490                nil))))))
4491     (setq select (if (stringp select) (string-to-number select) select))
4492     (if (or (null select) (zerop select))
4493         select
4494       (if (and (not (zerop scored)) (<= (abs select) scored))
4495           (progn
4496             (setq articles (sort scored-list '<))
4497             (setq number (length articles)))
4498         (setq articles (copy-sequence articles)))
4499
4500       (when (< (abs select) number)
4501         (if (< select 0)
4502             ;; Select the N oldest articles.
4503             (setcdr (nthcdr (1- (abs select)) articles) nil)
4504           ;; Select the N most recent articles.
4505           (setq articles (nthcdr (- number select) articles))))
4506       (setq gnus-newsgroup-unselected
4507             (gnus-sorted-intersection
4508              gnus-newsgroup-unreads
4509              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4510       (when gnus-alter-articles-to-read-function
4511         (setq gnus-newsgroup-unreads
4512               (sort
4513                (funcall gnus-alter-articles-to-read-function
4514                         gnus-newsgroup-name gnus-newsgroup-unreads)
4515                '<)))
4516       articles)))
4517
4518 (defun gnus-killed-articles (killed articles)
4519   (let (out)
4520     (while articles
4521       (when (inline (gnus-member-of-range (car articles) killed))
4522         (push (car articles) out))
4523       (setq articles (cdr articles)))
4524     out))
4525
4526 (defun gnus-uncompress-marks (marks)
4527   "Uncompress the mark ranges in MARKS."
4528   (let ((uncompressed '(score bookmark))
4529         out)
4530     (while marks
4531       (if (memq (caar marks) uncompressed)
4532           (push (car marks) out)
4533         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4534       (setq marks (cdr marks)))
4535     out))
4536
4537 (defun gnus-adjust-marked-articles (info)
4538   "Set all article lists and remove all marks that are no longer valid."
4539   (let* ((marked-lists (gnus-info-marks info))
4540          (active (gnus-active (gnus-info-group info)))
4541          (min (car active))
4542          (max (cdr active))
4543          (types gnus-article-mark-lists)
4544          (uncompressed '(score bookmark killed))
4545          marks var articles article mark)
4546
4547     (while marked-lists
4548       (setq marks (pop marked-lists))
4549       (set (setq var (intern (format "gnus-newsgroup-%s"
4550                                      (car (rassq (setq mark (car marks))
4551                                                  types)))))
4552            (if (memq (car marks) uncompressed) (cdr marks)
4553              (gnus-uncompress-range (cdr marks))))
4554
4555       (setq articles (symbol-value var))
4556
4557       ;; All articles have to be subsets of the active articles.
4558       (cond
4559        ;; Adjust "simple" lists.
4560        ((memq mark '(tick dormant expire reply save))
4561         (while articles
4562           (when (or (< (setq article (pop articles)) min) (> article max))
4563             (set var (delq article (symbol-value var))))))
4564        ;; Adjust assocs.
4565        ((memq mark uncompressed)
4566         (when (not (listp (cdr (symbol-value var))))
4567           (set var (list (symbol-value var))))
4568         (when (not (listp (cdr articles)))
4569           (setq articles (list articles)))
4570         (while articles
4571           (when (or (not (consp (setq article (pop articles))))
4572                     (< (car article) min)
4573                     (> (car article) max))
4574             (set var (delq article (symbol-value var))))))))))
4575
4576 (defun gnus-update-missing-marks (missing)
4577   "Go through the list of MISSING articles and remove them from the mark lists."
4578   (when missing
4579     (let ((types gnus-article-mark-lists)
4580           var m)
4581       ;; Go through all types.
4582       (while types
4583         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4584         (when (symbol-value var)
4585           ;; This list has articles.  So we delete all missing articles
4586           ;; from it.
4587           (setq m missing)
4588           (while m
4589             (set var (delq (pop m) (symbol-value var)))))))))
4590
4591 (defun gnus-update-marks ()
4592   "Enter the various lists of marked articles into the newsgroup info list."
4593   (let ((types gnus-article-mark-lists)
4594         (info (gnus-get-info gnus-newsgroup-name))
4595         (uncompressed '(score bookmark killed))
4596         type list newmarked symbol delta-marks)
4597     (when info
4598       ;; Add all marks lists to the list of marks lists.
4599       (while (setq type (pop types))
4600         (setq list (symbol-value
4601                     (setq symbol
4602                           (intern (format "gnus-newsgroup-%s"
4603                                           (car type))))))
4604
4605         (when list
4606           ;; Get rid of the entries of the articles that have the
4607           ;; default score.
4608           (when (and (eq (cdr type) 'score)
4609                      gnus-save-score
4610                      list)
4611             (let* ((arts list)
4612                    (prev (cons nil list))
4613                    (all prev))
4614               (while arts
4615                 (if (or (not (consp (car arts)))
4616                         (= (cdar arts) gnus-summary-default-score))
4617                     (setcdr prev (cdr arts))
4618                   (setq prev arts))
4619                 (setq arts (cdr arts)))
4620               (setq list (cdr all)))))
4621
4622         (unless (memq (cdr type) uncompressed)
4623           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4624
4625         (when (gnus-check-backend-function
4626                'request-set-mark gnus-newsgroup-name)
4627           ;; propagate flags to server, with the following exceptions:
4628           ;; uncompressed:s are not proper flags (they are cons cells)
4629           ;; cache is a internal gnus flag
4630           ;; download are local to one gnus installation (well)
4631           ;; unsend are for nndraft groups only
4632           ;; xxx: generality of this?  this suits nnimap anyway
4633           (unless (memq (cdr type) (append '(cache download unsend)
4634                                            uncompressed))
4635             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4636                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4637                    (add (gnus-remove-from-range
4638                          (gnus-copy-sequence list) old)))
4639               (when add
4640                 (push (list add 'add (list (cdr type))) delta-marks))
4641               (when del
4642                 (push (list del 'del (list (cdr type))) delta-marks)))))
4643
4644         (when list
4645           (push (cons (cdr type) list) newmarked)))
4646
4647       (when delta-marks
4648         (unless (gnus-check-group gnus-newsgroup-name)
4649           (error "Can't open server for %s" gnus-newsgroup-name))
4650         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4651
4652       ;; Enter these new marks into the info of the group.
4653       (if (nthcdr 3 info)
4654           (setcar (nthcdr 3 info) newmarked)
4655         ;; Add the marks lists to the end of the info.
4656         (when newmarked
4657           (setcdr (nthcdr 2 info) (list newmarked))))
4658
4659       ;; Cut off the end of the info if there's nothing else there.
4660       (let ((i 5))
4661         (while (and (> i 2)
4662                     (not (nth i info)))
4663           (when (nthcdr (decf i) info)
4664             (setcdr (nthcdr i info) nil)))))))
4665
4666 (defun gnus-set-mode-line (where)
4667   "Set the mode line of the article or summary buffers.
4668 If WHERE is `summary', the summary mode line format will be used."
4669   ;; Is this mode line one we keep updated?
4670   (when (and (memq where gnus-updated-mode-lines)
4671              (symbol-value
4672               (intern (format "gnus-%s-mode-line-format-spec" where))))
4673     (let (mode-string)
4674       (save-excursion
4675         ;; We evaluate this in the summary buffer since these
4676         ;; variables are buffer-local to that buffer.
4677         (set-buffer gnus-summary-buffer)
4678         ;; We bind all these variables that are used in the `eval' form
4679         ;; below.
4680         (let* ((mformat (symbol-value
4681                          (intern
4682                           (format "gnus-%s-mode-line-format-spec" where))))
4683                (gnus-tmp-group-name (gnus-group-name-decode
4684                                      gnus-newsgroup-name
4685                                      (gnus-group-name-charset
4686                                       nil
4687                                       gnus-newsgroup-name)))
4688                (gnus-tmp-article-number (or gnus-current-article 0))
4689                (gnus-tmp-unread gnus-newsgroup-unreads)
4690                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4691                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4692                (gnus-tmp-unread-and-unselected
4693                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4694                             (zerop gnus-tmp-unselected))
4695                        "")
4696                       ((zerop gnus-tmp-unselected)
4697                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4698                       (t (format "{%d(+%d) more}"
4699                                  gnus-tmp-unread-and-unticked
4700                                  gnus-tmp-unselected))))
4701                (gnus-tmp-subject
4702                 (if (and gnus-current-headers
4703                          (vectorp gnus-current-headers))
4704                     (gnus-mode-string-quote
4705                      (mail-header-subject gnus-current-headers))
4706                   ""))
4707                bufname-length max-len
4708                gnus-tmp-header);; passed as argument to any user-format-funcs
4709           (setq mode-string (eval mformat))
4710           (setq bufname-length (if (string-match "%b" mode-string)
4711                                    (- (length
4712                                        (buffer-name
4713                                         (if (eq where 'summary)
4714                                             nil
4715                                           (get-buffer gnus-article-buffer))))
4716                                       2)
4717                                  0))
4718           (setq max-len (max 4 (if gnus-mode-non-string-length
4719                                    (- (window-width)
4720                                       gnus-mode-non-string-length
4721                                       bufname-length)
4722                                  (length mode-string))))
4723           ;; We might have to chop a bit of the string off...
4724           (when (> (length mode-string) max-len)
4725             (setq mode-string
4726                   (concat (gnus-truncate-string mode-string (- max-len 3))
4727                           "...")))
4728           ;; Pad the mode string a bit.
4729           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4730       ;; Update the mode line.
4731       (setq mode-line-buffer-identification
4732             (gnus-mode-line-buffer-identification (list mode-string)))
4733       (set-buffer-modified-p t))))
4734
4735 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4736   "Go through the HEADERS list and add all Xrefs to a hash table.
4737 The resulting hash table is returned, or nil if no Xrefs were found."
4738   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4739          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4740          (xref-hashtb (gnus-make-hashtable))
4741          start group entry number xrefs header)
4742     (while headers
4743       (setq header (pop headers))
4744       (when (and (setq xrefs (mail-header-xref header))
4745                  (not (memq (setq number (mail-header-number header))
4746                             unreads)))
4747         (setq start 0)
4748         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4749           (setq start (match-end 0))
4750           (setq group (if prefix
4751                           (concat prefix (substring xrefs (match-beginning 1)
4752                                                     (match-end 1)))
4753                         (substring xrefs (match-beginning 1) (match-end 1))))
4754           (setq number
4755                 (string-to-int (substring xrefs (match-beginning 2)
4756                                           (match-end 2))))
4757           (if (setq entry (gnus-gethash group xref-hashtb))
4758               (setcdr entry (cons number (cdr entry)))
4759             (gnus-sethash group (cons number nil) xref-hashtb)))))
4760     (and start xref-hashtb)))
4761
4762 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4763   "Look through all the headers and mark the Xrefs as read."
4764   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4765         name entry info xref-hashtb idlist method nth4)
4766     (save-excursion
4767       (set-buffer gnus-group-buffer)
4768       (when (setq xref-hashtb
4769                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4770         (mapatoms
4771          (lambda (group)
4772            (unless (string= from-newsgroup (setq name (symbol-name group)))
4773              (setq idlist (symbol-value group))
4774              ;; Dead groups are not updated.
4775              (and (prog1
4776                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4777                             info (nth 2 entry))
4778                     (when (stringp (setq nth4 (gnus-info-method info)))
4779                       (setq nth4 (gnus-server-to-method nth4))))
4780                   ;; Only do the xrefs if the group has the same
4781                   ;; select method as the group we have just read.
4782                   (or (gnus-methods-equal-p
4783                        nth4 (gnus-find-method-for-group from-newsgroup))
4784                       virtual
4785                       (equal nth4 (setq method (gnus-find-method-for-group
4786                                                 from-newsgroup)))
4787                       (and (equal (car nth4) (car method))
4788                            (equal (nth 1 nth4) (nth 1 method))))
4789                   gnus-use-cross-reference
4790                   (or (not (eq gnus-use-cross-reference t))
4791                       virtual
4792                       ;; Only do cross-references on subscribed
4793                       ;; groups, if that is what is wanted.
4794                       (<= (gnus-info-level info) gnus-level-subscribed))
4795                   (gnus-group-make-articles-read name idlist))))
4796          xref-hashtb)))))
4797
4798 (defun gnus-compute-read-articles (group articles)
4799   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4800          (info (nth 2 entry))
4801          (active (gnus-active group))
4802          ninfo)
4803     (when entry
4804       ;; First peel off all invalid article numbers.
4805       (when active
4806         (let ((ids articles)
4807               id first)
4808           (while (setq id (pop ids))
4809             (when (and first (> id (cdr active)))
4810               ;; We'll end up in this situation in one particular
4811               ;; obscure situation.  If you re-scan a group and get
4812               ;; a new article that is cross-posted to a different
4813               ;; group that has not been re-scanned, you might get
4814               ;; crossposted article that has a higher number than
4815               ;; Gnus believes possible.  So we re-activate this
4816               ;; group as well.  This might mean doing the
4817               ;; crossposting thingy will *increase* the number
4818               ;; of articles in some groups.  Tsk, tsk.
4819               (setq active (or (gnus-activate-group group) active)))
4820             (when (or (> id (cdr active))
4821                       (< id (car active)))
4822               (setq articles (delq id articles))))))
4823       ;; If the read list is nil, we init it.
4824       (if (and active
4825                (null (gnus-info-read info))
4826                (> (car active) 1))
4827           (setq ninfo (cons 1 (1- (car active))))
4828         (setq ninfo (gnus-info-read info)))
4829       ;; Then we add the read articles to the range.
4830       (gnus-add-to-range
4831        ninfo (setq articles (sort articles '<))))))
4832
4833 (defun gnus-group-make-articles-read (group articles)
4834   "Update the info of GROUP to say that ARTICLES are read."
4835   (let* ((num 0)
4836          (entry (gnus-gethash group gnus-newsrc-hashtb))
4837          (info (nth 2 entry))
4838          (active (gnus-active group))
4839          range)
4840     (when entry
4841       (setq range (gnus-compute-read-articles group articles))
4842       (save-excursion
4843         (set-buffer gnus-group-buffer)
4844         (gnus-undo-register
4845           `(progn
4846              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4847              (gnus-info-set-read ',info ',(gnus-info-read info))
4848              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4849              (gnus-group-update-group ,group t))))
4850       ;; Add the read articles to the range.
4851       (gnus-info-set-read info range)
4852       ;; Then we have to re-compute how many unread
4853       ;; articles there are in this group.
4854       (when active
4855         (cond
4856          ((not range)
4857           (setq num (- (1+ (cdr active)) (car active))))
4858          ((not (listp (cdr range)))
4859           (setq num (- (cdr active) (- (1+ (cdr range))
4860                                        (car range)))))
4861          (t
4862           (while range
4863             (if (numberp (car range))
4864                 (setq num (1+ num))
4865               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4866             (setq range (cdr range)))
4867           (setq num (- (cdr active) num))))
4868         ;; Update the number of unread articles.
4869         (setcar entry num)
4870         ;; Update the group buffer.
4871         (gnus-group-update-group group t)))))
4872
4873 (defvar gnus-newsgroup-none-id 0)
4874
4875 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4876   (let ((cur nntp-server-buffer)
4877         (dependencies
4878          (or dependencies
4879              (save-excursion (set-buffer gnus-summary-buffer)
4880                              gnus-newsgroup-dependencies)))
4881         headers id end ref
4882         (mail-parse-charset gnus-newsgroup-charset)
4883         (mail-parse-ignored-charsets
4884          (save-excursion (condition-case nil
4885                              (set-buffer gnus-summary-buffer)
4886                            (error))
4887                          gnus-newsgroup-ignored-charsets)))
4888     (save-excursion
4889       (set-buffer nntp-server-buffer)
4890       ;; Translate all TAB characters into SPACE characters.
4891       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4892       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4893       (gnus-run-hooks 'gnus-parse-headers-hook)
4894       (let ((case-fold-search t)
4895             in-reply-to header p lines chars ctype)
4896         (goto-char (point-min))
4897         ;; Search to the beginning of the next header.  Error messages
4898         ;; do not begin with 2 or 3.
4899         (while (re-search-forward "^[23][0-9]+ " nil t)
4900           (setq id nil
4901                 ref nil)
4902           ;; This implementation of this function, with nine
4903           ;; search-forwards instead of the one re-search-forward and
4904           ;; a case (which basically was the old function) is actually
4905           ;; about twice as fast, even though it looks messier.  You
4906           ;; can't have everything, I guess.  Speed and elegance
4907           ;; doesn't always go hand in hand.
4908           (setq
4909            header
4910            (make-full-mail-header
4911             ;; Number.
4912             (prog1
4913                 (read cur)
4914               (end-of-line)
4915               (setq p (point))
4916               (narrow-to-region (point)
4917                                 (or (and (search-forward "\n.\n" nil t)
4918                                          (- (point) 2))
4919                                     (point))))
4920             ;; Subject.
4921             (progn
4922               (goto-char p)
4923               (if (search-forward "\nsubject: " nil t)
4924                   (nnheader-header-value)
4925                 "(none)"))
4926             ;; From.
4927             (progn
4928               (goto-char p)
4929               (if (or (search-forward "\nfrom: " nil t)
4930                       (search-forward "\nfrom:" nil t))
4931                   (nnheader-header-value)
4932                 "(nobody)"))
4933             ;; Date.
4934             (progn
4935               (goto-char p)
4936               (if (search-forward "\ndate: " nil t)
4937                   (nnheader-header-value)
4938                 ""))
4939             ;; Message-ID.
4940             (progn
4941               (goto-char p)
4942               (setq id (if (re-search-forward
4943                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4944                            ;; We do it this way to make sure the Message-ID
4945                            ;; is (somewhat) syntactically valid.
4946                            (buffer-substring (match-beginning 1)
4947                                              (match-end 1))
4948                          ;; If there was no message-id, we just fake one
4949                          ;; to make subsequent routines simpler.
4950                          (nnheader-generate-fake-message-id))))
4951             ;; References.
4952             (progn
4953               (goto-char p)
4954               (if (search-forward "\nreferences: " nil t)
4955                   (progn
4956                     (setq end (point))
4957                     (prog1
4958                         (nnheader-header-value)
4959                       (setq ref
4960                             (buffer-substring
4961                              (progn
4962                                ;; (end-of-line)
4963                                (search-backward ">" end t)
4964                                (1+ (point)))
4965                              (progn
4966                                (search-backward "<" end t)
4967                                (point))))))
4968                 ;; Get the references from the in-reply-to header if there
4969                 ;; were no references and the in-reply-to header looks
4970                 ;; promising.
4971                 (if (and (search-forward "\nin-reply-to: " nil t)
4972                          (setq in-reply-to (nnheader-header-value))
4973                          (string-match "<[^>]+>" in-reply-to))
4974                     (let (ref2)
4975                       (setq ref (substring in-reply-to (match-beginning 0)
4976                                            (match-end 0)))
4977                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4978                         (setq ref2 (substring in-reply-to (match-beginning 0)
4979                                               (match-end 0)))
4980                         (when (> (length ref2) (length ref))
4981                           (setq ref ref2)))
4982                       ref)
4983                   (setq ref nil))))
4984             ;; Chars.
4985             (progn
4986               (goto-char p)
4987               (if (search-forward "\nchars: " nil t)
4988                   (if (numberp (setq chars (ignore-errors (read cur))))
4989                       chars 0)
4990                 0))
4991             ;; Lines.
4992             (progn
4993               (goto-char p)
4994               (if (search-forward "\nlines: " nil t)
4995                   (if (numberp (setq lines (ignore-errors (read cur))))
4996                       lines 0)
4997                 0))
4998             ;; Xref.
4999             (progn
5000               (goto-char p)
5001               (and (search-forward "\nxref: " nil t)
5002                    (nnheader-header-value)))
5003             ;; Extra.
5004             (when gnus-extra-headers
5005               (let ((extra gnus-extra-headers)
5006                     out)
5007                 (while extra
5008                   (goto-char p)
5009                   (when (search-forward
5010                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5011                     (push (cons (car extra) (nnheader-header-value)) out))
5012                   (pop extra))
5013                 out))))
5014           (goto-char p)
5015           (if (and (search-forward "\ncontent-type: " nil t)
5016                    (setq ctype (nnheader-header-value)))
5017               (mime-entity-set-content-type-internal
5018                header (mime-parse-Content-Type ctype)))
5019           (when (equal id ref)
5020             (setq ref nil))
5021
5022           (when gnus-alter-header-function
5023             (funcall gnus-alter-header-function header)
5024             (setq id (mail-header-id header)
5025                   ref (gnus-parent-id (mail-header-references header))))
5026
5027           (when (setq header
5028                       (gnus-dependencies-add-header
5029                        header dependencies force-new))
5030             (push header headers))
5031           (goto-char (point-max))
5032           (widen))
5033         (nreverse headers)))))
5034
5035 ;; Goes through the xover lines and returns a list of vectors
5036 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5037                                                   force-new dependencies
5038                                                   group also-fetch-heads)
5039   "Parse the news overview data in the server buffer.
5040 Return a list of headers that match SEQUENCE (see
5041 `nntp-retrieve-headers')."
5042   ;; Get the Xref when the users reads the articles since most/some
5043   ;; NNTP servers do not include Xrefs when using XOVER.
5044   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5045   (let ((mail-parse-charset gnus-newsgroup-charset)
5046         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5047         (cur nntp-server-buffer)
5048         (dependencies (or dependencies gnus-newsgroup-dependencies))
5049         number headers header)
5050     (save-excursion
5051       (set-buffer nntp-server-buffer)
5052       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5053       ;; Allow the user to mangle the headers before parsing them.
5054       (gnus-run-hooks 'gnus-parse-headers-hook)
5055       (goto-char (point-min))
5056       (while (not (eobp))
5057         (condition-case ()
5058             (while (and sequence (not (eobp)))
5059               (setq number (read cur))
5060               (while (and sequence
5061                           (< (car sequence) number))
5062                 (setq sequence (cdr sequence)))
5063               (and sequence
5064                    (eq number (car sequence))
5065                    (progn
5066                      (setq sequence (cdr sequence))
5067                      (setq header (inline
5068                                     (gnus-nov-parse-line
5069                                      number dependencies force-new))))
5070                    (push header headers))
5071               (forward-line 1))
5072           (error
5073            (gnus-error 4 "Strange nov line (%d)"
5074                        (count-lines (point-min) (point)))))
5075         (forward-line 1))
5076       ;; A common bug in inn is that if you have posted an article and
5077       ;; then retrieves the active file, it will answer correctly --
5078       ;; the new article is included.  However, a NOV entry for the
5079       ;; article may not have been generated yet, so this may fail.
5080       ;; We work around this problem by retrieving the last few
5081       ;; headers using HEAD.
5082       (if (or (not also-fetch-heads)
5083               (not sequence))
5084           ;; We (probably) got all the headers.
5085           (nreverse headers)
5086         (let ((gnus-nov-is-evil t))
5087           (nconc
5088            (nreverse headers)
5089            (gnus-retrieve-parsed-headers sequence group)
5090            ))))))
5091
5092 (defun gnus-article-get-xrefs ()
5093   "Fill in the Xref value in `gnus-current-headers', if necessary.
5094 This is meant to be called in `gnus-article-internal-prepare-hook'."
5095   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5096                                  gnus-current-headers)))
5097     (or (not gnus-use-cross-reference)
5098         (not headers)
5099         (and (mail-header-xref headers)
5100              (not (string= (mail-header-xref headers) "")))
5101         (let ((case-fold-search t)
5102               xref)
5103           (save-restriction
5104             (nnheader-narrow-to-headers)
5105             (goto-char (point-min))
5106             (when (or (and (not (eobp))
5107                            (eq (downcase (char-after)) ?x)
5108                            (looking-at "Xref:"))
5109                       (search-forward "\nXref:" nil t))
5110               (goto-char (1+ (match-end 0)))
5111               (setq xref (buffer-substring (point)
5112                                            (progn (end-of-line) (point))))
5113               (mail-header-set-xref headers xref)))))))
5114
5115 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5116   "Find article ID and insert the summary line for that article.
5117 OLD-HEADER can either be a header or a line number to insert
5118 the subject line on."
5119   (let* ((line (and (numberp old-header) old-header))
5120          (old-header (and (vectorp old-header) old-header))
5121          (header (cond ((and old-header use-old-header)
5122                         old-header)
5123                        ((and (numberp id)
5124                              (gnus-number-to-header id))
5125                         (gnus-number-to-header id))
5126                        (t
5127                         (gnus-read-header id))))
5128          (number (and (numberp id) id))
5129          d)
5130     (when header
5131       ;; Rebuild the thread that this article is part of and go to the
5132       ;; article we have fetched.
5133       (when (and (not gnus-show-threads)
5134                  old-header)
5135         (when (and number
5136                    (setq d (gnus-data-find (mail-header-number old-header))))
5137           (goto-char (gnus-data-pos d))
5138           (gnus-data-remove
5139            number
5140            (- (gnus-point-at-bol)
5141               (prog1
5142                   (1+ (gnus-point-at-eol))
5143                 (gnus-delete-line))))))
5144       (when old-header
5145         (mail-header-set-number header (mail-header-number old-header)))
5146       (setq gnus-newsgroup-sparse
5147             (delq (setq number (mail-header-number header))
5148                   gnus-newsgroup-sparse))
5149       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5150       (push number gnus-newsgroup-limit)
5151       (gnus-rebuild-thread (mail-header-id header) line)
5152       (gnus-summary-goto-subject number nil t))
5153     (when (and (numberp number)
5154                (> number 0))
5155       ;; We have to update the boundaries even if we can't fetch the
5156       ;; article if ID is a number -- so that the next `P' or `N'
5157       ;; command will fetch the previous (or next) article even
5158       ;; if the one we tried to fetch this time has been canceled.
5159       (when (> number gnus-newsgroup-end)
5160         (setq gnus-newsgroup-end number))
5161       (when (< number gnus-newsgroup-begin)
5162         (setq gnus-newsgroup-begin number))
5163       (setq gnus-newsgroup-unselected
5164             (delq number gnus-newsgroup-unselected)))
5165     ;; Report back a success?
5166     (and header (mail-header-number header))))
5167
5168 ;;; Process/prefix in the summary buffer
5169
5170 (defun gnus-summary-work-articles (n)
5171   "Return a list of articles to be worked upon.
5172 The prefix argument, the list of process marked articles, and the
5173 current article will be taken into consideration."
5174   (save-excursion
5175     (set-buffer gnus-summary-buffer)
5176     (cond
5177      (n
5178       ;; A numerical prefix has been given.
5179       (setq n (prefix-numeric-value n))
5180       (let ((backward (< n 0))
5181             (n (abs (prefix-numeric-value n)))
5182             articles article)
5183         (save-excursion
5184           (while
5185               (and (> n 0)
5186                    (push (setq article (gnus-summary-article-number))
5187                          articles)
5188                    (if backward
5189                        (gnus-summary-find-prev nil article)
5190                      (gnus-summary-find-next nil article)))
5191             (decf n)))
5192         (nreverse articles)))
5193      ((and (gnus-region-active-p) (mark))
5194       (message "region active")
5195       ;; Work on the region between point and mark.
5196       (let ((max (max (point) (mark)))
5197             articles article)
5198         (save-excursion
5199           (goto-char (min (point) (mark)))
5200           (while
5201               (and
5202                (push (setq article (gnus-summary-article-number)) articles)
5203                (gnus-summary-find-next nil article)
5204                (< (point) max)))
5205           (nreverse articles))))
5206      (gnus-newsgroup-processable
5207       ;; There are process-marked articles present.
5208       ;; Save current state.
5209       (gnus-summary-save-process-mark)
5210       ;; Return the list.
5211       (reverse gnus-newsgroup-processable))
5212      (t
5213       ;; Just return the current article.
5214       (list (gnus-summary-article-number))))))
5215
5216 (defmacro gnus-summary-iterate (arg &rest forms)
5217   "Iterate over the process/prefixed articles and do FORMS.
5218 ARG is the interactive prefix given to the command.  FORMS will be
5219 executed with point over the summary line of the articles."
5220   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5221     `(let ((,articles (gnus-summary-work-articles ,arg)))
5222        (while ,articles
5223          (gnus-summary-goto-subject (car ,articles))
5224          ,@forms
5225          (pop ,articles)))))
5226
5227 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5228 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5229
5230 (defun gnus-summary-save-process-mark ()
5231   "Push the current set of process marked articles on the stack."
5232   (interactive)
5233   (push (copy-sequence gnus-newsgroup-processable)
5234         gnus-newsgroup-process-stack))
5235
5236 (defun gnus-summary-kill-process-mark ()
5237   "Push the current set of process marked articles on the stack and unmark."
5238   (interactive)
5239   (gnus-summary-save-process-mark)
5240   (gnus-summary-unmark-all-processable))
5241
5242 (defun gnus-summary-yank-process-mark ()
5243   "Pop the last process mark state off the stack and restore it."
5244   (interactive)
5245   (unless gnus-newsgroup-process-stack
5246     (error "Empty mark stack"))
5247   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5248
5249 (defun gnus-summary-process-mark-set (set)
5250   "Make SET into the current process marked articles."
5251   (gnus-summary-unmark-all-processable)
5252   (while set
5253     (gnus-summary-set-process-mark (pop set))))
5254
5255 ;;; Searching and stuff
5256
5257 (defun gnus-summary-search-group (&optional backward use-level)
5258   "Search for next unread newsgroup.
5259 If optional argument BACKWARD is non-nil, search backward instead."
5260   (save-excursion
5261     (set-buffer gnus-group-buffer)
5262     (when (gnus-group-search-forward
5263            backward nil (if use-level (gnus-group-group-level) nil))
5264       (gnus-group-group-name))))
5265
5266 (defun gnus-summary-best-group (&optional exclude-group)
5267   "Find the name of the best unread group.
5268 If EXCLUDE-GROUP, do not go to this group."
5269   (save-excursion
5270     (set-buffer gnus-group-buffer)
5271     (save-excursion
5272       (gnus-group-best-unread-group exclude-group))))
5273
5274 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5275   (if backward (gnus-summary-find-prev)
5276     (let* ((dummy (gnus-summary-article-intangible-p))
5277            (article (or article (gnus-summary-article-number)))
5278            (arts (gnus-data-find-list article))
5279            result)
5280       (when (and (not dummy)
5281                  (or (not gnus-summary-check-current)
5282                      (not unread)
5283                      (not (gnus-data-unread-p (car arts)))))
5284         (setq arts (cdr arts)))
5285       (when (setq result
5286                   (if unread
5287                       (progn
5288                         (while arts
5289                           (when (or (and undownloaded
5290                                          (eq gnus-undownloaded-mark
5291                                              (gnus-data-mark (car arts))))
5292                                     (gnus-data-unread-p (car arts)))
5293                             (setq result (car arts)
5294                                   arts nil))
5295                           (setq arts (cdr arts)))
5296                         result)
5297                     (car arts)))
5298         (goto-char (gnus-data-pos result))
5299         (gnus-data-number result)))))
5300
5301 (defun gnus-summary-find-prev (&optional unread article)
5302   (let* ((eobp (eobp))
5303          (article (or article (gnus-summary-article-number)))
5304          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5305          result)
5306     (when (and (not eobp)
5307                (or (not gnus-summary-check-current)
5308                    (not unread)
5309                    (not (gnus-data-unread-p (car arts)))))
5310       (setq arts (cdr arts)))
5311     (when (setq result
5312                 (if unread
5313                     (progn
5314                       (while arts
5315                         (when (gnus-data-unread-p (car arts))
5316                           (setq result (car arts)
5317                                 arts nil))
5318                         (setq arts (cdr arts)))
5319                       result)
5320                   (car arts)))
5321       (goto-char (gnus-data-pos result))
5322       (gnus-data-number result))))
5323
5324 (defun gnus-summary-find-subject (subject &optional unread backward article)
5325   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5326          (article (or article (gnus-summary-article-number)))
5327          (articles (gnus-data-list backward))
5328          (arts (gnus-data-find-list article articles))
5329          result)
5330     (when (or (not gnus-summary-check-current)
5331               (not unread)
5332               (not (gnus-data-unread-p (car arts))))
5333       (setq arts (cdr arts)))
5334     (while arts
5335       (and (or (not unread)
5336                (gnus-data-unread-p (car arts)))
5337            (vectorp (gnus-data-header (car arts)))
5338            (gnus-subject-equal
5339             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5340            (setq result (car arts)
5341                  arts nil))
5342       (setq arts (cdr arts)))
5343     (and result
5344          (goto-char (gnus-data-pos result))
5345          (gnus-data-number result))))
5346
5347 (defun gnus-summary-search-forward (&optional unread subject backward)
5348   "Search forward for an article.
5349 If UNREAD, look for unread articles.  If SUBJECT, look for
5350 articles with that subject.  If BACKWARD, search backward instead."
5351   (cond (subject (gnus-summary-find-subject subject unread backward))
5352         (backward (gnus-summary-find-prev unread))
5353         (t (gnus-summary-find-next unread))))
5354
5355 (defun gnus-recenter (&optional n)
5356   "Center point in window and redisplay frame.
5357 Also do horizontal recentering."
5358   (interactive "P")
5359   (when (and gnus-auto-center-summary
5360              (not (eq gnus-auto-center-summary 'vertical)))
5361     (gnus-horizontal-recenter))
5362   (recenter n))
5363
5364 (defun gnus-summary-recenter ()
5365   "Center point in the summary window.
5366 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5367 displayed, no centering will be performed."
5368   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5369   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5370   (interactive)
5371   (let* ((top (cond ((< (window-height) 4) 0)
5372                     ((< (window-height) 7) 1)
5373                     (t (if (numberp gnus-auto-center-summary)
5374                            gnus-auto-center-summary
5375                          2))))
5376          (height (1- (window-height)))
5377          (bottom (save-excursion (goto-char (point-max))
5378                                  (forward-line (- height))
5379                                  (point)))
5380          (window (get-buffer-window (current-buffer))))
5381     ;; The user has to want it.
5382     (when gnus-auto-center-summary
5383       (when (get-buffer-window gnus-article-buffer)
5384         ;; Only do recentering when the article buffer is displayed,
5385         ;; Set the window start to either `bottom', which is the biggest
5386         ;; possible valid number, or the second line from the top,
5387         ;; whichever is the least.
5388         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5389           (if (> bottom top-pos)
5390               ;; Keep the second line from the top visible
5391               (set-window-start window top-pos t)
5392             ;; Try to keep the bottom line visible; if it's partially
5393             ;; obscured, either scroll one more line to make it fully
5394             ;; visible, or revert to using TOP-POS.
5395             (save-excursion
5396               (goto-char (point-max))
5397               (forward-line -1)
5398               (let ((last-line-start (point)))
5399                 (goto-char bottom)
5400                 (set-window-start window (point) t)
5401                 (when (not (pos-visible-in-window-p last-line-start window))
5402                   (forward-line 1)
5403                   (set-window-start window (min (point) top-pos) t)))))))
5404       ;; Do horizontal recentering while we're at it.
5405       (when (and (get-buffer-window (current-buffer) t)
5406                  (not (eq gnus-auto-center-summary 'vertical)))
5407         (let ((selected (selected-window)))
5408           (select-window (get-buffer-window (current-buffer) t))
5409           (gnus-summary-position-point)
5410           (gnus-horizontal-recenter)
5411           (select-window selected))))))
5412
5413 (defun gnus-summary-jump-to-group (newsgroup)
5414   "Move point to NEWSGROUP in group mode buffer."
5415   ;; Keep update point of group mode buffer if visible.
5416   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5417       (save-window-excursion
5418         ;; Take care of tree window mode.
5419         (when (get-buffer-window gnus-group-buffer)
5420           (pop-to-buffer gnus-group-buffer))
5421         (gnus-group-jump-to-group newsgroup))
5422     (save-excursion
5423       ;; Take care of tree window mode.
5424       (if (get-buffer-window gnus-group-buffer)
5425           (pop-to-buffer gnus-group-buffer)
5426         (set-buffer gnus-group-buffer))
5427       (gnus-group-jump-to-group newsgroup))))
5428
5429 ;; This function returns a list of article numbers based on the
5430 ;; difference between the ranges of read articles in this group and
5431 ;; the range of active articles.
5432 (defun gnus-list-of-unread-articles (group)
5433   (let* ((read (gnus-info-read (gnus-get-info group)))
5434          (active (or (gnus-active group) (gnus-activate-group group)))
5435          (last (cdr active))
5436          first nlast unread)
5437     ;; If none are read, then all are unread.
5438     (if (not read)
5439         (setq first (car active))
5440       ;; If the range of read articles is a single range, then the
5441       ;; first unread article is the article after the last read
5442       ;; article.  Sounds logical, doesn't it?
5443       (if (and (not (listp (cdr read)))
5444                (or (< (car read) (car active))
5445                    (progn (setq read (list read))
5446                           nil)))
5447           (setq first (max (car active) (1+ (cdr read))))
5448         ;; `read' is a list of ranges.
5449         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5450                                   (caar read)))
5451                   1)
5452           (setq first (car active)))
5453         (while read
5454           (when first
5455             (while (< first nlast)
5456               (push first unread)
5457               (setq first (1+ first))))
5458           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5459           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5460           (setq read (cdr read)))))
5461     ;; And add the last unread articles.
5462     (while (<= first last)
5463       (push first unread)
5464       (setq first (1+ first)))
5465     ;; Return the list of unread articles.
5466     (delq 0 (nreverse unread))))
5467
5468 (defun gnus-list-of-read-articles (group)
5469   "Return a list of unread, unticked and non-dormant articles."
5470   (let* ((info (gnus-get-info group))
5471          (marked (gnus-info-marks info))
5472          (active (gnus-active group)))
5473     (and info active
5474          (gnus-set-difference
5475           (gnus-sorted-complement
5476            (gnus-uncompress-range active)
5477            (gnus-list-of-unread-articles group))
5478           (append
5479            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5480            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5481
5482 ;; Various summary commands
5483
5484 (defun gnus-summary-select-article-buffer ()
5485   "Reconfigure windows to show article buffer."
5486   (interactive)
5487   (if (not (gnus-buffer-live-p gnus-article-buffer))
5488       (error "There is no article buffer for this summary buffer")
5489     (gnus-configure-windows 'article)
5490     (select-window (get-buffer-window gnus-article-buffer))))
5491
5492 (defun gnus-summary-universal-argument (arg)
5493   "Perform any operation on all articles that are process/prefixed."
5494   (interactive "P")
5495   (let ((articles (gnus-summary-work-articles arg))
5496         func article)
5497     (if (eq
5498          (setq
5499           func
5500           (key-binding
5501            (read-key-sequence
5502             (substitute-command-keys
5503              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5504          'undefined)
5505         (gnus-error 1 "Undefined key")
5506       (save-excursion
5507         (while articles
5508           (gnus-summary-goto-subject (setq article (pop articles)))
5509           (let (gnus-newsgroup-processable)
5510             (command-execute func))
5511           (gnus-summary-remove-process-mark article)))))
5512   (gnus-summary-position-point))
5513
5514 (defun gnus-summary-toggle-truncation (&optional arg)
5515   "Toggle truncation of summary lines.
5516 With arg, turn line truncation on iff arg is positive."
5517   (interactive "P")
5518   (setq truncate-lines
5519         (if (null arg) (not truncate-lines)
5520           (> (prefix-numeric-value arg) 0)))
5521   (redraw-display))
5522
5523 (defun gnus-summary-reselect-current-group (&optional all rescan)
5524   "Exit and then reselect the current newsgroup.
5525 The prefix argument ALL means to select all articles."
5526   (interactive "P")
5527   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5528     (error "Ephemeral groups can't be reselected"))
5529   (let ((current-subject (gnus-summary-article-number))
5530         (group gnus-newsgroup-name))
5531     (setq gnus-newsgroup-begin nil)
5532     (gnus-summary-exit)
5533     ;; We have to adjust the point of group mode buffer because
5534     ;; point was moved to the next unread newsgroup by exiting.
5535     (gnus-summary-jump-to-group group)
5536     (when rescan
5537       (save-excursion
5538         (save-window-excursion
5539           ;; Don't show group contents.
5540           (set-window-start (selected-window) (point-max))
5541           (gnus-group-get-new-news-this-group 1))))
5542     (gnus-group-read-group all t)
5543     (gnus-summary-goto-subject current-subject nil t)))
5544
5545 (defun gnus-summary-rescan-group (&optional all)
5546   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5547   (interactive "P")
5548   (gnus-summary-reselect-current-group all t))
5549
5550 (defun gnus-summary-update-info (&optional non-destructive)
5551   (save-excursion
5552     (let ((group gnus-newsgroup-name))
5553       (when group
5554         (when gnus-newsgroup-kill-headers
5555           (setq gnus-newsgroup-killed
5556                 (gnus-compress-sequence
5557                  (nconc
5558                   (gnus-set-sorted-intersection
5559                    (gnus-uncompress-range gnus-newsgroup-killed)
5560                    (setq gnus-newsgroup-unselected
5561                          (sort gnus-newsgroup-unselected '<)))
5562                   (setq gnus-newsgroup-unreads
5563                         (sort gnus-newsgroup-unreads '<)))
5564                  t)))
5565         (unless (listp (cdr gnus-newsgroup-killed))
5566           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5567         (let ((headers gnus-newsgroup-headers))
5568           ;; Set the new ranges of read articles.
5569           (save-excursion
5570             (set-buffer gnus-group-buffer)
5571             (gnus-undo-force-boundary))
5572           (gnus-update-read-articles
5573            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5574           ;; Set the current article marks.
5575           (let ((gnus-newsgroup-scored
5576                  (if (and (not gnus-save-score)
5577                           (not non-destructive))
5578                      nil
5579                    gnus-newsgroup-scored)))
5580             (save-excursion
5581               (gnus-update-marks)))
5582           ;; Do the cross-ref thing.
5583           (when gnus-use-cross-reference
5584             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5585           ;; Do not switch windows but change the buffer to work.
5586           (set-buffer gnus-group-buffer)
5587           (unless (gnus-ephemeral-group-p group)
5588             (gnus-group-update-group group)))))))
5589
5590 (defun gnus-summary-save-newsrc (&optional force)
5591   "Save the current number of read/marked articles in the dribble buffer.
5592 The dribble buffer will then be saved.
5593 If FORCE (the prefix), also save the .newsrc file(s)."
5594   (interactive "P")
5595   (gnus-summary-update-info t)
5596   (if force
5597       (gnus-save-newsrc-file)
5598     (gnus-dribble-save)))
5599
5600 (defun gnus-summary-exit (&optional temporary)
5601   "Exit reading current newsgroup, and then return to group selection mode.
5602 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5603   (interactive)
5604   (gnus-set-global-variables)
5605   (gnus-kill-save-kill-buffer)
5606   (gnus-async-halt-prefetch)
5607   (let* ((group gnus-newsgroup-name)
5608          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5609          (mode major-mode)
5610          (group-point nil)
5611          (buf (current-buffer)))
5612     (unless quit-config
5613       ;; Do adaptive scoring, and possibly save score files.
5614       (when gnus-newsgroup-adaptive
5615         (gnus-score-adaptive))
5616       (when gnus-use-scoring
5617         (gnus-score-save)))
5618     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5619     ;; If we have several article buffers, we kill them at exit.
5620     (unless gnus-single-article-buffer
5621       (gnus-kill-buffer gnus-original-article-buffer)
5622       (setq gnus-article-current nil))
5623     (when gnus-use-cache
5624       (gnus-cache-possibly-remove-articles)
5625       (gnus-cache-save-buffers))
5626     (gnus-async-prefetch-remove-group group)
5627     (when gnus-suppress-duplicates
5628       (gnus-dup-enter-articles))
5629     (when gnus-use-trees
5630       (gnus-tree-close group))
5631     (when gnus-use-cache
5632       (gnus-cache-write-active))
5633     ;; Remove entries for this group.
5634     (nnmail-purge-split-history (gnus-group-real-name group))
5635     ;; Make all changes in this group permanent.
5636     (unless quit-config
5637       (gnus-run-hooks 'gnus-exit-group-hook)
5638       (gnus-summary-update-info))
5639     (gnus-close-group group)
5640     ;; Make sure where we were, and go to next newsgroup.
5641     (set-buffer gnus-group-buffer)
5642     (unless quit-config
5643       (gnus-group-jump-to-group group))
5644     (gnus-run-hooks 'gnus-summary-exit-hook)
5645     (unless (or quit-config
5646                 ;; If this group has disappeared from the summary
5647                 ;; buffer, don't skip forwards.
5648                 (not (string= group (gnus-group-group-name))))
5649       (gnus-group-next-unread-group 1))
5650     (setq group-point (point))
5651     (if temporary
5652         nil                             ;Nothing to do.
5653       ;; If we have several article buffers, we kill them at exit.
5654       (unless gnus-single-article-buffer
5655         (gnus-kill-buffer gnus-article-buffer)
5656         (gnus-kill-buffer gnus-original-article-buffer)
5657         (setq gnus-article-current nil))
5658       (set-buffer buf)
5659       (if (not gnus-kill-summary-on-exit)
5660           (gnus-deaden-summary)
5661         ;; We set all buffer-local variables to nil.  It is unclear why
5662         ;; this is needed, but if we don't, buffer-local variables are
5663         ;; not garbage-collected, it seems.  This would the lead to en
5664         ;; ever-growing Emacs.
5665         (gnus-summary-clear-local-variables)
5666         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5667           (gnus-summary-clear-local-variables))
5668         (when (get-buffer gnus-article-buffer)
5669           (bury-buffer gnus-article-buffer))
5670         ;; We clear the global counterparts of the buffer-local
5671         ;; variables as well, just to be on the safe side.
5672         (set-buffer gnus-group-buffer)
5673         (gnus-summary-clear-local-variables)
5674         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5675           (gnus-summary-clear-local-variables))
5676         ;; Return to group mode buffer.
5677         (when (eq mode 'gnus-summary-mode)
5678           (gnus-kill-buffer buf)))
5679       (setq gnus-current-select-method gnus-select-method)
5680       (pop-to-buffer gnus-group-buffer)
5681       (if (not quit-config)
5682           (progn
5683             (goto-char group-point)
5684             (gnus-configure-windows 'group 'force)
5685             (unless (pos-visible-in-window-p)
5686               (forward-line (/ (static-if (featurep 'xemacs)
5687                                    (window-displayed-height)
5688                                  (1- (window-height)))
5689                                -2))
5690               (set-window-start (selected-window) (point))
5691               (goto-char group-point)))
5692         (gnus-handle-ephemeral-exit quit-config))
5693       ;; Clear the current group name.
5694       (unless quit-config
5695         (setq gnus-newsgroup-name nil)))))
5696
5697 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5698 (defun gnus-summary-exit-no-update (&optional no-questions)
5699   "Quit reading current newsgroup without updating read article info."
5700   (interactive)
5701   (let* ((group gnus-newsgroup-name)
5702          (quit-config (gnus-group-quit-config group)))
5703     (when (or no-questions
5704               gnus-expert-user
5705               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5706       (gnus-async-halt-prefetch)
5707       (mapcar 'funcall
5708               (delq 'gnus-summary-expire-articles
5709                     (copy-sequence gnus-summary-prepare-exit-hook)))
5710       ;; If we have several article buffers, we kill them at exit.
5711       (unless gnus-single-article-buffer
5712         (gnus-kill-buffer gnus-article-buffer)
5713         (gnus-kill-buffer gnus-original-article-buffer)
5714         (setq gnus-article-current nil))
5715       (if (not gnus-kill-summary-on-exit)
5716           (gnus-deaden-summary)
5717         (gnus-close-group group)
5718         (gnus-summary-clear-local-variables)
5719         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5720           (gnus-summary-clear-local-variables))
5721         (set-buffer gnus-group-buffer)
5722         (gnus-summary-clear-local-variables)
5723         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5724           (gnus-summary-clear-local-variables))
5725         (when (get-buffer gnus-summary-buffer)
5726           (kill-buffer gnus-summary-buffer)))
5727       (unless gnus-single-article-buffer
5728         (setq gnus-article-current nil))
5729       (when gnus-use-trees
5730         (gnus-tree-close group))
5731       (gnus-async-prefetch-remove-group group)
5732       (when (get-buffer gnus-article-buffer)
5733         (bury-buffer gnus-article-buffer))
5734       ;; Return to the group buffer.
5735       (gnus-configure-windows 'group 'force)
5736       ;; Clear the current group name.
5737       (setq gnus-newsgroup-name nil)
5738       (when (equal (gnus-group-group-name) group)
5739         (gnus-group-next-unread-group 1))
5740       (when quit-config
5741         (gnus-handle-ephemeral-exit quit-config)))))
5742
5743 (defun gnus-handle-ephemeral-exit (quit-config)
5744   "Handle movement when leaving an ephemeral group.
5745 The state which existed when entering the ephemeral is reset."
5746   (if (not (buffer-name (car quit-config)))
5747       (gnus-configure-windows 'group 'force)
5748     (set-buffer (car quit-config))
5749     (cond ((eq major-mode 'gnus-summary-mode)
5750            (gnus-set-global-variables))
5751           ((eq major-mode 'gnus-article-mode)
5752            (save-excursion
5753              ;; The `gnus-summary-buffer' variable may point
5754              ;; to the old summary buffer when using a single
5755              ;; article buffer.
5756              (unless (gnus-buffer-live-p gnus-summary-buffer)
5757                (set-buffer gnus-group-buffer))
5758              (set-buffer gnus-summary-buffer)
5759              (gnus-set-global-variables))))
5760     (if (or (eq (cdr quit-config) 'article)
5761             (eq (cdr quit-config) 'pick))
5762         (progn
5763           ;; The current article may be from the ephemeral group
5764           ;; thus it is best that we reload this article
5765           (gnus-summary-show-article)
5766           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5767               (gnus-configure-windows 'pick 'force)
5768             (gnus-configure-windows (cdr quit-config) 'force)))
5769       (gnus-configure-windows (cdr quit-config) 'force))
5770     (when (eq major-mode 'gnus-summary-mode)
5771       (gnus-summary-next-subject 1 nil t)
5772       (gnus-summary-recenter)
5773       (gnus-summary-position-point))))
5774
5775 (defun gnus-summary-preview-mime-message ()
5776   "MIME decode and play this message."
5777   (interactive)
5778   (let ((gnus-break-pages nil)
5779         (gnus-show-mime t))
5780     (gnus-summary-select-article gnus-show-all-headers t))
5781   (select-window (get-buffer-window gnus-article-buffer)))
5782
5783 ;;; Dead summaries.
5784
5785 (defvar gnus-dead-summary-mode-map nil)
5786
5787 (unless gnus-dead-summary-mode-map
5788   (setq gnus-dead-summary-mode-map (make-keymap))
5789   (suppress-keymap gnus-dead-summary-mode-map)
5790   (substitute-key-definition
5791    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5792   (let ((keys '("\C-d" "\r" "\177" [delete])))
5793     (while keys
5794       (define-key gnus-dead-summary-mode-map
5795         (pop keys) 'gnus-summary-wake-up-the-dead))))
5796
5797 (defvar gnus-dead-summary-mode nil
5798   "Minor mode for Gnus summary buffers.")
5799
5800 (defun gnus-dead-summary-mode (&optional arg)
5801   "Minor mode for Gnus summary buffers."
5802   (interactive "P")
5803   (when (eq major-mode 'gnus-summary-mode)
5804     (make-local-variable 'gnus-dead-summary-mode)
5805     (setq gnus-dead-summary-mode
5806           (if (null arg) (not gnus-dead-summary-mode)
5807             (> (prefix-numeric-value arg) 0)))
5808     (when gnus-dead-summary-mode
5809       (gnus-add-minor-mode
5810        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5811
5812 (defun gnus-deaden-summary ()
5813   "Make the current summary buffer into a dead summary buffer."
5814   ;; Kill any previous dead summary buffer.
5815   (when (and gnus-dead-summary
5816              (buffer-name gnus-dead-summary))
5817     (save-excursion
5818       (set-buffer gnus-dead-summary)
5819       (when gnus-dead-summary-mode
5820         (kill-buffer (current-buffer)))))
5821   ;; Make this the current dead summary.
5822   (setq gnus-dead-summary (current-buffer))
5823   (gnus-dead-summary-mode 1)
5824   (let ((name (buffer-name)))
5825     (when (string-match "Summary" name)
5826       (rename-buffer
5827        (concat (substring name 0 (match-beginning 0)) "Dead "
5828                (substring name (match-beginning 0)))
5829        t)
5830       (bury-buffer))))
5831
5832 (defun gnus-kill-or-deaden-summary (buffer)
5833   "Kill or deaden the summary BUFFER."
5834   (save-excursion
5835     (when (and (buffer-name buffer)
5836                (not gnus-single-article-buffer))
5837       (save-excursion
5838         (set-buffer buffer)
5839         (gnus-kill-buffer gnus-article-buffer)
5840         (gnus-kill-buffer gnus-original-article-buffer)))
5841     (cond (gnus-kill-summary-on-exit
5842            (when (and gnus-use-trees
5843                       (gnus-buffer-exists-p buffer))
5844              (save-excursion
5845                (set-buffer buffer)
5846                (gnus-tree-close gnus-newsgroup-name)))
5847            (gnus-kill-buffer buffer))
5848           ((gnus-buffer-exists-p buffer)
5849            (save-excursion
5850              (set-buffer buffer)
5851              (gnus-deaden-summary))))))
5852
5853 (defun gnus-summary-wake-up-the-dead (&rest args)
5854   "Wake up the dead summary buffer."
5855   (interactive)
5856   (gnus-dead-summary-mode -1)
5857   (let ((name (buffer-name)))
5858     (when (string-match "Dead " name)
5859       (rename-buffer
5860        (concat (substring name 0 (match-beginning 0))
5861                (substring name (match-end 0)))
5862        t)))
5863   (gnus-message 3 "This dead summary is now alive again"))
5864
5865 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5866 (defun gnus-summary-fetch-faq (&optional faq-dir)
5867   "Fetch the FAQ for the current group.
5868 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5869 in."
5870   (interactive
5871    (list
5872     (when current-prefix-arg
5873       (completing-read
5874        "Faq dir: " (and (listp gnus-group-faq-directory)
5875                         (mapcar (lambda (file) (list file))
5876                                 gnus-group-faq-directory))))))
5877   (let (gnus-faq-buffer)
5878     (when (setq gnus-faq-buffer
5879                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5880       (gnus-configure-windows 'summary-faq))))
5881
5882 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5883 (defun gnus-summary-describe-group (&optional force)
5884   "Describe the current newsgroup."
5885   (interactive "P")
5886   (gnus-group-describe-group force gnus-newsgroup-name))
5887
5888 (defun gnus-summary-describe-briefly ()
5889   "Describe summary mode commands briefly."
5890   (interactive)
5891   (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")))
5892
5893 ;; Walking around group mode buffer from summary mode.
5894
5895 (defun gnus-summary-next-group (&optional no-article target-group backward)
5896   "Exit current newsgroup and then select next unread newsgroup.
5897 If prefix argument NO-ARTICLE is non-nil, no article is selected
5898 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5899 previous group instead."
5900   (interactive "P")
5901   ;; Stop pre-fetching.
5902   (gnus-async-halt-prefetch)
5903   (let ((current-group gnus-newsgroup-name)
5904         (current-buffer (current-buffer))
5905         entered)
5906     ;; First we semi-exit this group to update Xrefs and all variables.
5907     ;; We can't do a real exit, because the window conf must remain
5908     ;; the same in case the user is prompted for info, and we don't
5909     ;; want the window conf to change before that...
5910     (gnus-summary-exit t)
5911     (while (not entered)
5912       ;; Then we find what group we are supposed to enter.
5913       (set-buffer gnus-group-buffer)
5914       (gnus-group-jump-to-group current-group)
5915       (setq target-group
5916             (or target-group
5917                 (if (eq gnus-keep-same-level 'best)
5918                     (gnus-summary-best-group gnus-newsgroup-name)
5919                   (gnus-summary-search-group backward gnus-keep-same-level))))
5920       (if (not target-group)
5921           ;; There are no further groups, so we return to the group
5922           ;; buffer.
5923           (progn
5924             (gnus-message 5 "Returning to the group buffer")
5925             (setq entered t)
5926             (when (gnus-buffer-live-p current-buffer)
5927               (set-buffer current-buffer)
5928               (gnus-summary-exit))
5929             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5930         ;; We try to enter the target group.
5931         (gnus-group-jump-to-group target-group)
5932         (let ((unreads (gnus-group-group-unread)))
5933           (if (and (or (eq t unreads)
5934                        (and unreads (not (zerop unreads))))
5935                    (gnus-summary-read-group
5936                     target-group nil no-article
5937                     (and (buffer-name current-buffer) current-buffer)
5938                     nil backward))
5939               (setq entered t)
5940             (setq current-group target-group
5941                   target-group nil)))))))
5942
5943 (defun gnus-summary-prev-group (&optional no-article)
5944   "Exit current newsgroup and then select previous unread newsgroup.
5945 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5946   (interactive "P")
5947   (gnus-summary-next-group no-article nil t))
5948
5949 ;; Walking around summary lines.
5950
5951 (defun gnus-summary-first-subject (&optional unread undownloaded)
5952   "Go to the first unread subject.
5953 If UNREAD is non-nil, go to the first unread article.
5954 Returns the article selected or nil if there are no unread articles."
5955   (interactive "P")
5956   (prog1
5957       (cond
5958        ;; Empty summary.
5959        ((null gnus-newsgroup-data)
5960         (gnus-message 3 "No articles in the group")
5961         nil)
5962        ;; Pick the first article.
5963        ((not unread)
5964         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5965         (gnus-data-number (car gnus-newsgroup-data)))
5966        ;; No unread articles.
5967        ((null gnus-newsgroup-unreads)
5968         (gnus-message 3 "No more unread articles")
5969         nil)
5970        ;; Find the first unread article.
5971        (t
5972         (let ((data gnus-newsgroup-data))
5973           (while (and data
5974                       (and (not (and undownloaded
5975                                      (eq gnus-undownloaded-mark
5976                                          (gnus-data-mark (car data)))))
5977                            (not (gnus-data-unread-p (car data)))))
5978             (setq data (cdr data)))
5979           (when data
5980             (goto-char (gnus-data-pos (car data)))
5981             (gnus-data-number (car data))))))
5982     (gnus-summary-position-point)))
5983
5984 (defun gnus-summary-next-subject (n &optional unread dont-display)
5985   "Go to next N'th summary line.
5986 If N is negative, go to the previous N'th subject line.
5987 If UNREAD is non-nil, only unread articles are selected.
5988 The difference between N and the actual number of steps taken is
5989 returned."
5990   (interactive "p")
5991   (let ((backward (< n 0))
5992         (n (abs n)))
5993     (while (and (> n 0)
5994                 (if backward
5995                     (gnus-summary-find-prev unread)
5996                   (gnus-summary-find-next unread)))
5997       (unless (zerop (setq n (1- n)))
5998         (gnus-summary-show-thread)))
5999     (when (/= 0 n)
6000       (gnus-message 7 "No more%s articles"
6001                     (if unread " unread" "")))
6002     (unless dont-display
6003       (gnus-summary-recenter)
6004       (gnus-summary-position-point))
6005     n))
6006
6007 (defun gnus-summary-next-unread-subject (n)
6008   "Go to next N'th unread summary line."
6009   (interactive "p")
6010   (gnus-summary-next-subject n t))
6011
6012 (defun gnus-summary-prev-subject (n &optional unread)
6013   "Go to previous N'th summary line.
6014 If optional argument UNREAD is non-nil, only unread article is selected."
6015   (interactive "p")
6016   (gnus-summary-next-subject (- n) unread))
6017
6018 (defun gnus-summary-prev-unread-subject (n)
6019   "Go to previous N'th unread summary line."
6020   (interactive "p")
6021   (gnus-summary-next-subject (- n) t))
6022
6023 (defun gnus-summary-goto-subject (article &optional force silent)
6024   "Go the subject line of ARTICLE.
6025 If FORCE, also allow jumping to articles not currently shown."
6026   (interactive "nArticle number: ")
6027   (let ((b (point))
6028         (data (gnus-data-find article)))
6029     ;; We read in the article if we have to.
6030     (and (not data)
6031          force
6032          (gnus-summary-insert-subject
6033           article
6034           (if (or (numberp force) (vectorp force)) force)
6035           t)
6036          (setq data (gnus-data-find article)))
6037     (goto-char b)
6038     (if (not data)
6039         (progn
6040           (unless silent
6041             (gnus-message 3 "Can't find article %d" article))
6042           nil)
6043       (goto-char (gnus-data-pos data))
6044       (gnus-summary-position-point)
6045       article)))
6046
6047 ;; Walking around summary lines with displaying articles.
6048
6049 (defun gnus-summary-expand-window (&optional arg)
6050   "Make the summary buffer take up the entire Emacs frame.
6051 Given a prefix, will force an `article' buffer configuration."
6052   (interactive "P")
6053   (if arg
6054       (gnus-configure-windows 'article 'force)
6055     (gnus-configure-windows 'summary 'force)))
6056
6057 (defun gnus-summary-display-article (article &optional all-header)
6058   "Display ARTICLE in article buffer."
6059   (when (gnus-buffer-live-p gnus-article-buffer)
6060     (with-current-buffer gnus-article-buffer
6061       (mm-enable-multibyte-mule4)))
6062   (gnus-set-global-variables)
6063   (when (gnus-buffer-live-p gnus-article-buffer)
6064     (with-current-buffer gnus-article-buffer
6065       (setq gnus-article-charset gnus-newsgroup-charset)
6066       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6067   (if (null article)
6068       nil
6069     (prog1
6070         (if gnus-summary-display-article-function
6071             (funcall gnus-summary-display-article-function article all-header)
6072           (gnus-article-prepare article all-header))
6073       (with-current-buffer gnus-article-buffer
6074         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6075              nil))
6076       (gnus-run-hooks 'gnus-select-article-hook)
6077       (when (and gnus-current-article
6078                  (not (zerop gnus-current-article)))
6079         (gnus-summary-goto-subject gnus-current-article))
6080       (gnus-summary-recenter)
6081       (when (and gnus-use-trees gnus-show-threads)
6082         (gnus-possibly-generate-tree article)
6083         (gnus-highlight-selected-tree article))
6084       ;; Successfully display article.
6085       (gnus-article-set-window-start
6086        (cdr (assq article gnus-newsgroup-bookmarks))))))
6087
6088 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6089   "Select the current article.
6090 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6091 non-nil, the article will be re-fetched even if it already present in
6092 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6093 be displayed."
6094   ;; Make sure we are in the summary buffer to work around bbdb bug.
6095   (unless (eq major-mode 'gnus-summary-mode)
6096     (set-buffer gnus-summary-buffer))
6097   (let ((article (or article (gnus-summary-article-number)))
6098         (all-headers (not (not all-headers))) ;Must be T or NIL.
6099         gnus-summary-display-article-function)
6100     (and (not pseudo)
6101          (gnus-summary-article-pseudo-p article)
6102          (error "This is a pseudo-article"))
6103     (save-excursion
6104       (set-buffer gnus-summary-buffer)
6105       (if (or (and gnus-single-article-buffer
6106                    (or (null gnus-current-article)
6107                        (null gnus-article-current)
6108                        (null (get-buffer gnus-article-buffer))
6109                        (not (eq article (cdr gnus-article-current)))
6110                        (not (equal (car gnus-article-current)
6111                                    gnus-newsgroup-name))))
6112               (and (not gnus-single-article-buffer)
6113                    (or (null gnus-current-article)
6114                        (not (eq gnus-current-article article))))
6115               force)
6116           ;; The requested article is different from the current article.
6117           (progn
6118             (gnus-summary-display-article article all-headers)
6119             (when (or all-headers gnus-show-all-headers)
6120               (gnus-article-show-all-headers))
6121             (gnus-article-set-window-start
6122              (cdr (assq article gnus-newsgroup-bookmarks)))
6123             article)
6124         (when (or all-headers gnus-show-all-headers)
6125           (gnus-article-show-all-headers))
6126         'old))))
6127
6128 (defun gnus-summary-force-verify-and-decrypt ()
6129   (interactive)
6130   (let ((mm-verify-option 'known)
6131         (mm-decrypt-option 'known))
6132     (gnus-summary-select-article nil 'force)))
6133
6134 (defun gnus-summary-set-current-mark (&optional current-mark)
6135   "Obsolete function."
6136   nil)
6137
6138 (defun gnus-summary-next-article (&optional unread subject backward push)
6139   "Select the next article.
6140 If UNREAD, only unread articles are selected.
6141 If SUBJECT, only articles with SUBJECT are selected.
6142 If BACKWARD, the previous article is selected instead of the next."
6143   (interactive "P")
6144   (cond
6145    ;; Is there such an article?
6146    ((and (gnus-summary-search-forward unread subject backward)
6147          (or (gnus-summary-display-article (gnus-summary-article-number))
6148              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6149     (gnus-summary-position-point))
6150    ;; If not, we try the first unread, if that is wanted.
6151    ((and subject
6152          gnus-auto-select-same
6153          (gnus-summary-first-unread-article))
6154     (gnus-summary-position-point)
6155     (gnus-message 6 "Wrapped"))
6156    ;; Try to get next/previous article not displayed in this group.
6157    ((and gnus-auto-extend-newsgroup
6158          (not unread) (not subject))
6159     (gnus-summary-goto-article
6160      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6161      nil (count-lines (point-min) (point))))
6162    ;; Go to next/previous group.
6163    (t
6164     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6165       (gnus-summary-jump-to-group gnus-newsgroup-name))
6166     (let ((cmd last-command-char)
6167           (point
6168            (save-excursion
6169              (set-buffer gnus-group-buffer)
6170              (point)))
6171           (group
6172            (if (eq gnus-keep-same-level 'best)
6173                (gnus-summary-best-group gnus-newsgroup-name)
6174              (gnus-summary-search-group backward gnus-keep-same-level))))
6175       ;; For some reason, the group window gets selected.  We change
6176       ;; it back.
6177       (select-window (get-buffer-window (current-buffer)))
6178       ;; Select next unread newsgroup automagically.
6179       (cond
6180        ((or (not gnus-auto-select-next)
6181             (not cmd))
6182         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6183        ((or (eq gnus-auto-select-next 'quietly)
6184             (and (eq gnus-auto-select-next 'slightly-quietly)
6185                  push)
6186             (and (eq gnus-auto-select-next 'almost-quietly)
6187                  (gnus-summary-last-article-p)))
6188         ;; Select quietly.
6189         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6190             (gnus-summary-exit)
6191           (gnus-message 7 "No more%s articles (%s)..."
6192                         (if unread " unread" "")
6193                         (if group (concat "selecting " group)
6194                           "exiting"))
6195           (gnus-summary-next-group nil group backward)))
6196        (t
6197         (when (gnus-key-press-event-p last-input-event)
6198           (gnus-summary-walk-group-buffer
6199            gnus-newsgroup-name cmd unread backward point))))))))
6200
6201 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6202   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6203                       (?\C-p (gnus-group-prev-unread-group 1))))
6204         (cursor-in-echo-area t)
6205         keve key group ended)
6206     (save-excursion
6207       (set-buffer gnus-group-buffer)
6208       (goto-char start)
6209       (setq group
6210             (if (eq gnus-keep-same-level 'best)
6211                 (gnus-summary-best-group gnus-newsgroup-name)
6212               (gnus-summary-search-group backward gnus-keep-same-level))))
6213     (while (not ended)
6214       (gnus-message
6215        5 "No more%s articles%s" (if unread " unread" "")
6216        (if (and group
6217                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6218            (format " (Type %s for %s [%s])"
6219                    (single-key-description cmd) group
6220                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6221          (format " (Type %s to exit %s)"
6222                  (single-key-description cmd)
6223                  gnus-newsgroup-name)))
6224       ;; Confirm auto selection.
6225       (setq key (car (setq keve (gnus-read-event-char))))
6226       (setq ended t)
6227       (cond
6228        ((assq key keystrokes)
6229         (let ((obuf (current-buffer)))
6230           (switch-to-buffer gnus-group-buffer)
6231           (when group
6232             (gnus-group-jump-to-group group))
6233           (eval (cadr (assq key keystrokes)))
6234           (setq group (gnus-group-group-name))
6235           (switch-to-buffer obuf))
6236         (setq ended nil))
6237        ((equal key cmd)
6238         (if (or (not group)
6239                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6240             (gnus-summary-exit)
6241           (gnus-summary-next-group nil group backward)))
6242        (t
6243         (push (cdr keve) unread-command-events))))))
6244
6245 (defun gnus-summary-next-unread-article ()
6246   "Select unread article after current one."
6247   (interactive)
6248   (gnus-summary-next-article
6249    (or (not (eq gnus-summary-goto-unread 'never))
6250        (gnus-summary-last-article-p (gnus-summary-article-number)))
6251    (and gnus-auto-select-same
6252         (gnus-summary-article-subject))))
6253
6254 (defun gnus-summary-prev-article (&optional unread subject)
6255   "Select the article after the current one.
6256 If UNREAD is non-nil, only unread articles are selected."
6257   (interactive "P")
6258   (gnus-summary-next-article unread subject t))
6259
6260 (defun gnus-summary-prev-unread-article ()
6261   "Select unread article before current one."
6262   (interactive)
6263   (gnus-summary-prev-article
6264    (or (not (eq gnus-summary-goto-unread 'never))
6265        (gnus-summary-first-article-p (gnus-summary-article-number)))
6266    (and gnus-auto-select-same
6267         (gnus-summary-article-subject))))
6268
6269 (defun gnus-summary-next-page (&optional lines circular)
6270   "Show next page of the selected article.
6271 If at the end of the current article, select the next article.
6272 LINES says how many lines should be scrolled up.
6273
6274 If CIRCULAR is non-nil, go to the start of the article instead of
6275 selecting the next article when reaching the end of the current
6276 article."
6277   (interactive "P")
6278   (setq gnus-summary-buffer (current-buffer))
6279   (gnus-set-global-variables)
6280   (let ((article (gnus-summary-article-number))
6281         (article-window (get-buffer-window gnus-article-buffer t))
6282         endp)
6283     ;; If the buffer is empty, we have no article.
6284     (unless article
6285       (error "No article to select"))
6286     (gnus-configure-windows 'article)
6287     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6288         (if (and (eq gnus-summary-goto-unread 'never)
6289                  (not (gnus-summary-last-article-p article)))
6290             (gnus-summary-next-article)
6291           (gnus-summary-next-unread-article))
6292       (if (or (null gnus-current-article)
6293               (null gnus-article-current)
6294               (/= article (cdr gnus-article-current))
6295               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6296           ;; Selected subject is different from current article's.
6297           (gnus-summary-display-article article)
6298         (when article-window
6299           (gnus-eval-in-buffer-window gnus-article-buffer
6300             (setq endp (gnus-article-next-page lines)))
6301           (when endp
6302             (cond (circular
6303                    (gnus-summary-beginning-of-article))
6304                   (lines
6305                    (gnus-message 3 "End of message"))
6306                   ((null lines)
6307                    (if (and (eq gnus-summary-goto-unread 'never)
6308                             (not (gnus-summary-last-article-p article)))
6309                        (gnus-summary-next-article)
6310                      (gnus-summary-next-unread-article))))))))
6311     (gnus-summary-recenter)
6312     (gnus-summary-position-point)))
6313
6314 (defun gnus-summary-prev-page (&optional lines move)
6315   "Show previous page of selected article.
6316 Argument LINES specifies lines to be scrolled down.
6317 If MOVE, move to the previous unread article if point is at
6318 the beginning of the buffer."
6319   (interactive "P")
6320   (let ((article (gnus-summary-article-number))
6321         (article-window (get-buffer-window gnus-article-buffer t))
6322         endp)
6323     (gnus-configure-windows 'article)
6324     (if (or (null gnus-current-article)
6325             (null gnus-article-current)
6326             (/= article (cdr gnus-article-current))
6327             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6328         ;; Selected subject is different from current article's.
6329         (gnus-summary-display-article article)
6330       (gnus-summary-recenter)
6331       (when article-window
6332         (gnus-eval-in-buffer-window gnus-article-buffer
6333           (setq endp (gnus-article-prev-page lines)))
6334         (when (and move endp)
6335           (cond (lines
6336                  (gnus-message 3 "Beginning of message"))
6337                 ((null lines)
6338                  (if (and (eq gnus-summary-goto-unread 'never)
6339                           (not (gnus-summary-first-article-p article)))
6340                      (gnus-summary-prev-article)
6341                    (gnus-summary-prev-unread-article))))))))
6342   (gnus-summary-position-point))
6343
6344 (defun gnus-summary-prev-page-or-article (&optional lines)
6345   "Show previous page of selected article.
6346 Argument LINES specifies lines to be scrolled down.
6347 If at the beginning of the article, go to the next article."
6348   (interactive "P")
6349   (gnus-summary-prev-page lines t))
6350
6351 (defun gnus-summary-scroll-up (lines)
6352   "Scroll up (or down) one line current article.
6353 Argument LINES specifies lines to be scrolled up (or down if negative)."
6354   (interactive "p")
6355   (gnus-configure-windows 'article)
6356   (gnus-summary-show-thread)
6357   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6358     (gnus-eval-in-buffer-window gnus-article-buffer
6359       (cond ((> lines 0)
6360              (when (gnus-article-next-page lines)
6361                (gnus-message 3 "End of message")))
6362             ((< lines 0)
6363              (gnus-article-prev-page (- lines))))))
6364   (gnus-summary-recenter)
6365   (gnus-summary-position-point))
6366
6367 (defun gnus-summary-scroll-down (lines)
6368   "Scroll down (or up) one line current article.
6369 Argument LINES specifies lines to be scrolled down (or up if negative)."
6370   (interactive "p")
6371   (gnus-summary-scroll-up (- lines)))
6372
6373 (defun gnus-summary-next-same-subject ()
6374   "Select next article which has the same subject as current one."
6375   (interactive)
6376   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6377
6378 (defun gnus-summary-prev-same-subject ()
6379   "Select previous article which has the same subject as current one."
6380   (interactive)
6381   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6382
6383 (defun gnus-summary-next-unread-same-subject ()
6384   "Select next unread article which has the same subject as current one."
6385   (interactive)
6386   (gnus-summary-next-article t (gnus-summary-article-subject)))
6387
6388 (defun gnus-summary-prev-unread-same-subject ()
6389   "Select previous unread article which has the same subject as current one."
6390   (interactive)
6391   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6392
6393 (defun gnus-summary-first-unread-article ()
6394   "Select the first unread article.
6395 Return nil if there are no unread articles."
6396   (interactive)
6397   (prog1
6398       (when (gnus-summary-first-subject t)
6399         (gnus-summary-show-thread)
6400         (gnus-summary-first-subject t)
6401         (gnus-summary-display-article (gnus-summary-article-number)))
6402     (gnus-summary-position-point)))
6403
6404 (defun gnus-summary-first-unread-subject ()
6405   "Place the point on the subject line of the first unread article.
6406 Return nil if there are no unread articles."
6407   (interactive)
6408   (prog1
6409       (when (gnus-summary-first-subject t)
6410         (gnus-summary-show-thread)
6411         (gnus-summary-first-subject t))
6412     (gnus-summary-position-point)))
6413
6414 (defun gnus-summary-first-article ()
6415   "Select the first article.
6416 Return nil if there are no articles."
6417   (interactive)
6418   (prog1
6419       (when (gnus-summary-first-subject)
6420         (gnus-summary-show-thread)
6421         (gnus-summary-first-subject)
6422         (gnus-summary-display-article (gnus-summary-article-number)))
6423     (gnus-summary-position-point)))
6424
6425 (defun gnus-summary-best-unread-article ()
6426   "Select the unread article with the highest score."
6427   (interactive)
6428   (let ((best -1000000)
6429         (data gnus-newsgroup-data)
6430         article score)
6431     (while data
6432       (and (gnus-data-unread-p (car data))
6433            (> (setq score
6434                     (gnus-summary-article-score (gnus-data-number (car data))))
6435               best)
6436            (setq best score
6437                  article (gnus-data-number (car data))))
6438       (setq data (cdr data)))
6439     (prog1
6440         (if article
6441             (gnus-summary-goto-article article)
6442           (error "No unread articles"))
6443       (gnus-summary-position-point))))
6444
6445 (defun gnus-summary-last-subject ()
6446   "Go to the last displayed subject line in the group."
6447   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6448     (when article
6449       (gnus-summary-goto-subject article))))
6450
6451 (defun gnus-summary-goto-article (article &optional all-headers force)
6452   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6453 If ALL-HEADERS is non-nil, no header lines are hidden.
6454 If FORCE, go to the article even if it isn't displayed.  If FORCE
6455 is a number, it is the line the article is to be displayed on."
6456   (interactive
6457    (list
6458     (completing-read
6459      "Article number or Message-ID: "
6460      (mapcar (lambda (number) (list (int-to-string number)))
6461              gnus-newsgroup-limit))
6462     current-prefix-arg
6463     t))
6464   (prog1
6465       (if (and (stringp article)
6466                (string-match "@" article))
6467           (gnus-summary-refer-article article)
6468         (when (stringp article)
6469           (setq article (string-to-number article)))
6470         (if (gnus-summary-goto-subject article force)
6471             (gnus-summary-display-article article all-headers)
6472           (gnus-message 4 "Couldn't go to article %s" article) nil))
6473     (gnus-summary-position-point)))
6474
6475 (defun gnus-summary-goto-last-article ()
6476   "Go to the previously read article."
6477   (interactive)
6478   (prog1
6479       (when gnus-last-article
6480         (gnus-summary-goto-article gnus-last-article nil t))
6481     (gnus-summary-position-point)))
6482
6483 (defun gnus-summary-pop-article (number)
6484   "Pop one article off the history and go to the previous.
6485 NUMBER articles will be popped off."
6486   (interactive "p")
6487   (let (to)
6488     (setq gnus-newsgroup-history
6489           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6490     (if to
6491         (gnus-summary-goto-article (car to) nil t)
6492       (error "Article history empty")))
6493   (gnus-summary-position-point))
6494
6495 ;; Summary commands and functions for limiting the summary buffer.
6496
6497 (defun gnus-summary-limit-to-articles (n)
6498   "Limit the summary buffer to the next N articles.
6499 If not given a prefix, use the process marked articles instead."
6500   (interactive "P")
6501   (prog1
6502       (let ((articles (gnus-summary-work-articles n)))
6503         (setq gnus-newsgroup-processable nil)
6504         (gnus-summary-limit articles))
6505     (gnus-summary-position-point)))
6506
6507 (defun gnus-summary-pop-limit (&optional total)
6508   "Restore the previous limit.
6509 If given a prefix, remove all limits."
6510   (interactive "P")
6511   (when total
6512     (setq gnus-newsgroup-limits
6513           (list (mapcar (lambda (h) (mail-header-number h))
6514                         gnus-newsgroup-headers))))
6515   (unless gnus-newsgroup-limits
6516     (error "No limit to pop"))
6517   (prog1
6518       (gnus-summary-limit nil 'pop)
6519     (gnus-summary-position-point)))
6520
6521 (defun gnus-summary-limit-to-subject (subject &optional header)
6522   "Limit the summary buffer to articles that have subjects that match a regexp."
6523   (interactive "sLimit to subject (regexp): ")
6524   (unless header
6525     (setq header "subject"))
6526   (when (not (equal "" subject))
6527     (prog1
6528         (let ((articles (gnus-summary-find-matching
6529                          (or header "subject") subject 'all)))
6530           (unless articles
6531             (error "Found no matches for \"%s\"" subject))
6532           (gnus-summary-limit articles))
6533       (gnus-summary-position-point))))
6534
6535 (defun gnus-summary-limit-to-author (from)
6536   "Limit the summary buffer to articles that have authors that match a regexp."
6537   (interactive "sLimit to author (regexp): ")
6538   (gnus-summary-limit-to-subject from "from"))
6539
6540 (defun gnus-summary-limit-to-age (age &optional younger-p)
6541   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6542 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6543 articles that are younger than AGE days."
6544   (interactive
6545    (let ((younger current-prefix-arg)
6546          (days-got nil)
6547          days)
6548      (while (not days-got)
6549        (setq days (if younger
6550                       (read-string "Limit to articles within (in days): ")
6551                     (read-string "Limit to articles older than (in days): ")))
6552        (when (> (length days) 0)
6553          (setq days (read days)))
6554        (if (numberp days)
6555            (setq days-got t)
6556          (message "Please enter a number.")
6557          (sleep-for 1)))
6558      (list days younger)))
6559   (prog1
6560       (let ((data gnus-newsgroup-data)
6561             (cutoff (days-to-time age))
6562             articles d date is-younger)
6563         (while (setq d (pop data))
6564           (when (and (vectorp (gnus-data-header d))
6565                      (setq date (mail-header-date (gnus-data-header d))))
6566             (setq is-younger (time-less-p
6567                               (time-since (condition-case ()
6568                                               (date-to-time date)
6569                                             (error '(0 0))))
6570                               cutoff))
6571             (when (if younger-p
6572                       is-younger
6573                     (not is-younger))
6574               (push (gnus-data-number d) articles))))
6575         (gnus-summary-limit (nreverse articles)))
6576     (gnus-summary-position-point)))
6577
6578 (defun gnus-summary-limit-to-extra (header regexp)
6579   "Limit the summary buffer to articles that match an 'extra' header."
6580   (interactive
6581    (let ((header
6582           (intern
6583            (gnus-completing-read
6584             (symbol-name (car gnus-extra-headers))
6585             "Limit extra header:"
6586             (mapcar (lambda (x)
6587                       (cons (symbol-name x) x))
6588                     gnus-extra-headers)
6589             nil
6590             t))))
6591      (list header
6592            (read-string (format "Limit to header %s (regexp): " header)))))
6593   (when (not (equal "" regexp))
6594     (prog1
6595         (let ((articles (gnus-summary-find-matching
6596                          (cons 'extra header) regexp 'all)))
6597           (unless articles
6598             (error "Found no matches for \"%s\"" regexp))
6599           (gnus-summary-limit articles))
6600       (gnus-summary-position-point))))
6601
6602 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6603 (make-obsolete
6604  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6605
6606 (defun gnus-summary-limit-to-unread (&optional all)
6607   "Limit the summary buffer to articles that are not marked as read.
6608 If ALL is non-nil, limit strictly to unread articles."
6609   (interactive "P")
6610   (if all
6611       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6612     (gnus-summary-limit-to-marks
6613      ;; Concat all the marks that say that an article is read and have
6614      ;; those removed.
6615      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6616            gnus-killed-mark gnus-kill-file-mark
6617            gnus-low-score-mark gnus-expirable-mark
6618            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6619            gnus-duplicate-mark gnus-souped-mark)
6620      'reverse)))
6621
6622 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6623 (make-obsolete 'gnus-summary-delete-marked-with
6624                'gnus-summary-limit-exlude-marks)
6625
6626 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6627   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6628 If REVERSE, limit the summary buffer to articles that are marked
6629 with MARKS.  MARKS can either be a string of marks or a list of marks.
6630 Returns how many articles were removed."
6631   (interactive "sMarks: ")
6632   (gnus-summary-limit-to-marks marks t))
6633
6634 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6635   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6636 If REVERSE (the prefix), limit the summary buffer to articles that are
6637 not marked with MARKS.  MARKS can either be a string of marks or a
6638 list of marks.
6639 Returns how many articles were removed."
6640   (interactive "sMarks: \nP")
6641   (prog1
6642       (let ((data gnus-newsgroup-data)
6643             (marks (if (listp marks) marks
6644                      (append marks nil))) ; Transform to list.
6645             articles)
6646         (while data
6647           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6648                   (memq (gnus-data-mark (car data)) marks))
6649             (push (gnus-data-number (car data)) articles))
6650           (setq data (cdr data)))
6651         (gnus-summary-limit articles))
6652     (gnus-summary-position-point)))
6653
6654 (defun gnus-summary-limit-to-score (&optional score)
6655   "Limit to articles with score at or above SCORE."
6656   (interactive "P")
6657   (setq score (if score
6658                   (prefix-numeric-value score)
6659                 (or gnus-summary-default-score 0)))
6660   (let ((data gnus-newsgroup-data)
6661         articles)
6662     (while data
6663       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6664                 score)
6665         (push (gnus-data-number (car data)) articles))
6666       (setq data (cdr data)))
6667     (prog1
6668         (gnus-summary-limit articles)
6669       (gnus-summary-position-point))))
6670
6671 (defun gnus-summary-limit-include-thread (id)
6672   "Display all the hidden articles that in the current thread."
6673   (interactive (list (mail-header-id (gnus-summary-article-header))))
6674   (let ((articles (gnus-articles-in-thread
6675                    (gnus-id-to-thread (gnus-root-id id)))))
6676     (prog1
6677         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6678       (gnus-summary-position-point))))
6679
6680 (defun gnus-summary-limit-include-dormant ()
6681   "Display all the hidden articles that are marked as dormant.
6682 Note that this command only works on a subset of the articles currently
6683 fetched for this group."
6684   (interactive)
6685   (unless gnus-newsgroup-dormant
6686     (error "There are no dormant articles in this group"))
6687   (prog1
6688       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6689     (gnus-summary-position-point)))
6690
6691 (defun gnus-summary-limit-exclude-dormant ()
6692   "Hide all dormant articles."
6693   (interactive)
6694   (prog1
6695       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6696     (gnus-summary-position-point)))
6697
6698 (defun gnus-summary-limit-exclude-childless-dormant ()
6699   "Hide all dormant articles that have no children."
6700   (interactive)
6701   (let ((data (gnus-data-list t))
6702         articles d children)
6703     ;; Find all articles that are either not dormant or have
6704     ;; children.
6705     (while (setq d (pop data))
6706       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6707                 (and (setq children
6708                            (gnus-article-children (gnus-data-number d)))
6709                      (let (found)
6710                        (while children
6711                          (when (memq (car children) articles)
6712                            (setq children nil
6713                                  found t))
6714                          (pop children))
6715                        found)))
6716         (push (gnus-data-number d) articles)))
6717     ;; Do the limiting.
6718     (prog1
6719         (gnus-summary-limit articles)
6720       (gnus-summary-position-point))))
6721
6722 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6723   "Mark all unread excluded articles as read.
6724 If ALL, mark even excluded ticked and dormants as read."
6725   (interactive "P")
6726   (let ((articles (gnus-sorted-complement
6727                    (sort
6728                     (mapcar (lambda (h) (mail-header-number h))
6729                             gnus-newsgroup-headers)
6730                     '<)
6731                    (sort gnus-newsgroup-limit '<)))
6732         article)
6733     (setq gnus-newsgroup-unreads
6734           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6735     (if all
6736         (setq gnus-newsgroup-dormant nil
6737               gnus-newsgroup-marked nil
6738               gnus-newsgroup-reads
6739               (nconc
6740                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6741                gnus-newsgroup-reads))
6742       (while (setq article (pop articles))
6743         (unless (or (memq article gnus-newsgroup-dormant)
6744                     (memq article gnus-newsgroup-marked))
6745           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6746
6747 (defun gnus-summary-limit (articles &optional pop)
6748   (if pop
6749       ;; We pop the previous limit off the stack and use that.
6750       (setq articles (car gnus-newsgroup-limits)
6751             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6752     ;; We use the new limit, so we push the old limit on the stack.
6753     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6754   ;; Set the limit.
6755   (setq gnus-newsgroup-limit articles)
6756   (let ((total (length gnus-newsgroup-data))
6757         (data (gnus-data-find-list (gnus-summary-article-number)))
6758         (gnus-summary-mark-below nil)   ; Inhibit this.
6759         found)
6760     ;; This will do all the work of generating the new summary buffer
6761     ;; according to the new limit.
6762     (gnus-summary-prepare)
6763     ;; Hide any threads, possibly.
6764     (and gnus-show-threads
6765          gnus-thread-hide-subtree
6766          (gnus-summary-hide-all-threads))
6767     ;; Try to return to the article you were at, or one in the
6768     ;; neighborhood.
6769     (when data
6770       ;; We try to find some article after the current one.
6771       (while data
6772         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6773           (setq data nil
6774                 found t))
6775         (setq data (cdr data))))
6776     (unless found
6777       ;; If there is no data, that means that we were after the last
6778       ;; article.  The same goes when we can't find any articles
6779       ;; after the current one.
6780       (goto-char (point-max))
6781       (gnus-summary-find-prev))
6782     (gnus-set-mode-line 'summary)
6783     ;; We return how many articles were removed from the summary
6784     ;; buffer as a result of the new limit.
6785     (- total (length gnus-newsgroup-data))))
6786
6787 (defsubst gnus-invisible-cut-children (threads)
6788   (let ((num 0))
6789     (while threads
6790       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6791         (incf num))
6792       (pop threads))
6793     (< num 2)))
6794
6795 (defsubst gnus-cut-thread (thread)
6796   "Go forwards in the thread until we find an article that we want to display."
6797   (when (or (eq gnus-fetch-old-headers 'some)
6798             (eq gnus-fetch-old-headers 'invisible)
6799             (numberp gnus-fetch-old-headers)
6800             (eq gnus-build-sparse-threads 'some)
6801             (eq gnus-build-sparse-threads 'more))
6802     ;; Deal with old-fetched headers and sparse threads.
6803     (while (and
6804             thread
6805             (or
6806              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6807              (gnus-summary-article-ancient-p
6808               (mail-header-number (car thread))))
6809             (if (or (<= (length (cdr thread)) 1)
6810                     (eq gnus-fetch-old-headers 'invisible))
6811                 (setq gnus-newsgroup-limit
6812                       (delq (mail-header-number (car thread))
6813                             gnus-newsgroup-limit)
6814                       thread (cadr thread))
6815               (when (gnus-invisible-cut-children (cdr thread))
6816                 (let ((th (cdr thread)))
6817                   (while th
6818                     (if (memq (mail-header-number (caar th))
6819                               gnus-newsgroup-limit)
6820                         (setq thread (car th)
6821                               th nil)
6822                       (setq th (cdr th))))))))))
6823   thread)
6824
6825 (defun gnus-cut-threads (threads)
6826   "Cut off all uninteresting articles from the beginning of threads."
6827   (when (or (eq gnus-fetch-old-headers 'some)
6828             (eq gnus-fetch-old-headers 'invisible)
6829             (numberp gnus-fetch-old-headers)
6830             (eq gnus-build-sparse-threads 'some)
6831             (eq gnus-build-sparse-threads 'more))
6832     (let ((th threads))
6833       (while th
6834         (setcar th (gnus-cut-thread (car th)))
6835         (setq th (cdr th)))))
6836   ;; Remove nixed out threads.
6837   (delq nil threads))
6838
6839 (defun gnus-summary-initial-limit (&optional show-if-empty)
6840   "Figure out what the initial limit is supposed to be on group entry.
6841 This entails weeding out unwanted dormants, low-scored articles,
6842 fetch-old-headers verbiage, and so on."
6843   ;; Most groups have nothing to remove.
6844   (if (or gnus-inhibit-limiting
6845           (and (null gnus-newsgroup-dormant)
6846                (not (eq gnus-fetch-old-headers 'some))
6847                (not (numberp gnus-fetch-old-headers))
6848                (not (eq gnus-fetch-old-headers 'invisible))
6849                (null gnus-summary-expunge-below)
6850                (not (eq gnus-build-sparse-threads 'some))
6851                (not (eq gnus-build-sparse-threads 'more))
6852                (null gnus-thread-expunge-below)
6853                (not gnus-use-nocem)))
6854       ()                                ; Do nothing.
6855     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6856     (setq gnus-newsgroup-limit nil)
6857     (mapatoms
6858      (lambda (node)
6859        (unless (car (symbol-value node))
6860          ;; These threads have no parents -- they are roots.
6861          (let ((nodes (cdr (symbol-value node)))
6862                thread)
6863            (while nodes
6864              (if (and gnus-thread-expunge-below
6865                       (< (gnus-thread-total-score (car nodes))
6866                          gnus-thread-expunge-below))
6867                  (gnus-expunge-thread (pop nodes))
6868                (setq thread (pop nodes))
6869                (gnus-summary-limit-children thread))))))
6870      gnus-newsgroup-dependencies)
6871     ;; If this limitation resulted in an empty group, we might
6872     ;; pop the previous limit and use it instead.
6873     (when (and (not gnus-newsgroup-limit)
6874                show-if-empty)
6875       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6876     gnus-newsgroup-limit))
6877
6878 (defun gnus-summary-limit-children (thread)
6879   "Return 1 if this subthread is visible and 0 if it is not."
6880   ;; First we get the number of visible children to this thread.  This
6881   ;; is done by recursing down the thread using this function, so this
6882   ;; will really go down to a leaf article first, before slowly
6883   ;; working its way up towards the root.
6884   (when thread
6885     (let ((children
6886            (if (cdr thread)
6887                (apply '+ (mapcar 'gnus-summary-limit-children
6888                                  (cdr thread)))
6889              0))
6890           (number (mail-header-number (car thread)))
6891           score)
6892       (if (and
6893            (not (memq number gnus-newsgroup-marked))
6894            (or
6895             ;; If this article is dormant and has absolutely no visible
6896             ;; children, then this article isn't visible.
6897             (and (memq number gnus-newsgroup-dormant)
6898                  (zerop children))
6899             ;; If this is "fetch-old-headered" and there is no
6900             ;; visible children, then we don't want this article.
6901             (and (or (eq gnus-fetch-old-headers 'some)
6902                      (numberp gnus-fetch-old-headers))
6903                  (gnus-summary-article-ancient-p number)
6904                  (zerop children))
6905             ;; If this is "fetch-old-headered" and `invisible', then
6906             ;; we don't want this article.
6907             (and (eq gnus-fetch-old-headers 'invisible)
6908                  (gnus-summary-article-ancient-p number))
6909             ;; If this is a sparsely inserted article with no children,
6910             ;; we don't want it.
6911             (and (eq gnus-build-sparse-threads 'some)
6912                  (gnus-summary-article-sparse-p number)
6913                  (zerop children))
6914             ;; If we use expunging, and this article is really
6915             ;; low-scored, then we don't want this article.
6916             (when (and gnus-summary-expunge-below
6917                        (< (setq score
6918                                 (or (cdr (assq number gnus-newsgroup-scored))
6919                                     gnus-summary-default-score))
6920                           gnus-summary-expunge-below))
6921               ;; We increase the expunge-tally here, but that has
6922               ;; nothing to do with the limits, really.
6923               (incf gnus-newsgroup-expunged-tally)
6924               ;; We also mark as read here, if that's wanted.
6925               (when (and gnus-summary-mark-below
6926                          (< score gnus-summary-mark-below))
6927                 (setq gnus-newsgroup-unreads
6928                       (delq number gnus-newsgroup-unreads))
6929                 (if gnus-newsgroup-auto-expire
6930                     (push number gnus-newsgroup-expirable)
6931                   (push (cons number gnus-low-score-mark)
6932                         gnus-newsgroup-reads)))
6933               t)
6934             ;; Check NoCeM things.
6935             (if (and gnus-use-nocem
6936                      (gnus-nocem-unwanted-article-p
6937                       (mail-header-id (car thread))))
6938                 (progn
6939                   (setq gnus-newsgroup-unreads
6940                         (delq number gnus-newsgroup-unreads))
6941                   t))))
6942           ;; Nope, invisible article.
6943           0
6944         ;; Ok, this article is to be visible, so we add it to the limit
6945         ;; and return 1.
6946         (push number gnus-newsgroup-limit)
6947         1))))
6948
6949 (defun gnus-expunge-thread (thread)
6950   "Mark all articles in THREAD as read."
6951   (let* ((number (mail-header-number (car thread))))
6952     (incf gnus-newsgroup-expunged-tally)
6953     ;; We also mark as read here, if that's wanted.
6954     (setq gnus-newsgroup-unreads
6955           (delq number gnus-newsgroup-unreads))
6956     (if gnus-newsgroup-auto-expire
6957         (push number gnus-newsgroup-expirable)
6958       (push (cons number gnus-low-score-mark)
6959             gnus-newsgroup-reads)))
6960   ;; Go recursively through all subthreads.
6961   (mapcar 'gnus-expunge-thread (cdr thread)))
6962
6963 ;; Summary article oriented commands
6964
6965 (defun gnus-summary-refer-parent-article (n)
6966   "Refer parent article N times.
6967 If N is negative, go to ancestor -N instead.
6968 The difference between N and the number of articles fetched is returned."
6969   (interactive "p")
6970   (let ((skip 1)
6971         error header ref)
6972     (when (not (natnump n))
6973       (setq skip (abs n)
6974             n 1))
6975     (while (and (> n 0)
6976                 (not error))
6977       (setq header (gnus-summary-article-header))
6978       (if (and (eq (mail-header-number header)
6979                    (cdr gnus-article-current))
6980                (equal gnus-newsgroup-name
6981                       (car gnus-article-current)))
6982           ;; If we try to find the parent of the currently
6983           ;; displayed article, then we take a look at the actual
6984           ;; References header, since this is slightly more
6985           ;; reliable than the References field we got from the
6986           ;; server.
6987           (save-excursion
6988             (set-buffer gnus-original-article-buffer)
6989             (nnheader-narrow-to-headers)
6990             (unless (setq ref (message-fetch-field "references"))
6991               (setq ref (message-fetch-field "in-reply-to")))
6992             (widen))
6993         (setq ref
6994               ;; It's not the current article, so we take a bet on
6995               ;; the value we got from the server.
6996               (mail-header-references header)))
6997       (if (and ref
6998                (not (equal ref "")))
6999           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7000             (gnus-message 1 "Couldn't find parent"))
7001         (gnus-message 1 "No references in article %d"
7002                       (gnus-summary-article-number))
7003         (setq error t))
7004       (decf n))
7005     (gnus-summary-position-point)
7006     n))
7007
7008 (defun gnus-summary-refer-references ()
7009   "Fetch all articles mentioned in the References header.
7010 Return the number of articles fetched."
7011   (interactive)
7012   (let ((ref (mail-header-references (gnus-summary-article-header)))
7013         (current (gnus-summary-article-number))
7014         (n 0))
7015     (if (or (not ref)
7016             (equal ref ""))
7017         (error "No References in the current article")
7018       ;; For each Message-ID in the References header...
7019       (while (string-match "<[^>]*>" ref)
7020         (incf n)
7021         ;; ... fetch that article.
7022         (gnus-summary-refer-article
7023          (prog1 (match-string 0 ref)
7024            (setq ref (substring ref (match-end 0))))))
7025       (gnus-summary-goto-subject current)
7026       (gnus-summary-position-point)
7027       n)))
7028
7029 (defun gnus-summary-refer-thread (&optional limit)
7030   "Fetch all articles in the current thread.
7031 If LIMIT (the numerical prefix), fetch that many old headers instead
7032 of what's specified by the `gnus-refer-thread-limit' variable."
7033   (interactive "P")
7034   (let ((id (mail-header-id (gnus-summary-article-header)))
7035         (limit (if limit (prefix-numeric-value limit)
7036                  gnus-refer-thread-limit)))
7037     ;; We want to fetch LIMIT *old* headers, but we also have to
7038     ;; re-fetch all the headers in the current buffer, because many of
7039     ;; them may be undisplayed.  So we adjust LIMIT.
7040     (when (numberp limit)
7041       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7042     (unless (eq gnus-fetch-old-headers 'invisible)
7043       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7044       ;; Retrieve the headers and read them in.
7045       (if (eq (gnus-retrieve-headers
7046                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7047               'nov)
7048           (gnus-build-all-threads)
7049         (error "Can't fetch thread from backends that don't support NOV"))
7050       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7051     (gnus-summary-limit-include-thread id)))
7052
7053 (defun gnus-summary-refer-article (message-id)
7054   "Fetch an article specified by MESSAGE-ID."
7055   (interactive "sMessage-ID: ")
7056   (when (and (stringp message-id)
7057              (not (zerop (length message-id))))
7058     ;; Construct the correct Message-ID if necessary.
7059     ;; Suggested by tale@pawl.rpi.edu.
7060     (unless (string-match "^<" message-id)
7061       (setq message-id (concat "<" message-id)))
7062     (unless (string-match ">$" message-id)
7063       (setq message-id (concat message-id ">")))
7064     (let* ((header (gnus-id-to-header message-id))
7065            (sparse (and header
7066                         (gnus-summary-article-sparse-p
7067                          (mail-header-number header))
7068                         (memq (mail-header-number header)
7069                               gnus-newsgroup-limit)))
7070            number)
7071       (cond
7072        ;; If the article is present in the buffer we just go to it.
7073        ((and header
7074              (or (not (gnus-summary-article-sparse-p
7075                        (mail-header-number header)))
7076                  sparse))
7077         (prog1
7078             (gnus-summary-goto-article
7079              (mail-header-number header) nil t)
7080           (when sparse
7081             (gnus-summary-update-article (mail-header-number header)))))
7082        (t
7083         ;; We fetch the article.
7084         (catch 'found
7085           (dolist (gnus-override-method (gnus-refer-article-methods))
7086             (gnus-check-server gnus-override-method)
7087             ;; Fetch the header, and display the article.
7088             (when (setq number (gnus-summary-insert-subject message-id))
7089               (gnus-summary-select-article nil nil nil number)
7090               (throw 'found t)))
7091           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7092
7093 (defun gnus-refer-article-methods ()
7094   "Return a list of referrable methods."
7095   (cond
7096    ;; No method, so we default to current and native.
7097    ((null gnus-refer-article-method)
7098     (list gnus-current-select-method gnus-select-method))
7099    ;; Current.
7100    ((eq 'current gnus-refer-article-method)
7101     (list gnus-current-select-method))
7102    ;; List of select methods.
7103    ((not (and (symbolp (car gnus-refer-article-method))
7104               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7105     (let (out)
7106       (dolist (method gnus-refer-article-method)
7107         (push (if (eq 'current method)
7108                   gnus-current-select-method
7109                 method)
7110               out))
7111       (nreverse out)))
7112    ;; One single select method.
7113    (t
7114     (list gnus-refer-article-method))))
7115
7116 (defun gnus-summary-edit-parameters ()
7117   "Edit the group parameters of the current group."
7118   (interactive)
7119   (gnus-group-edit-group gnus-newsgroup-name 'params))
7120
7121 (defun gnus-summary-customize-parameters ()
7122   "Customize the group parameters of the current group."
7123   (interactive)
7124   (gnus-group-customize gnus-newsgroup-name))
7125
7126 (defun gnus-summary-enter-digest-group (&optional force)
7127   "Enter an nndoc group based on the current article.
7128 If FORCE, force a digest interpretation.  If not, try
7129 to guess what the document format is."
7130   (interactive "P")
7131   (let ((conf gnus-current-window-configuration))
7132     (save-excursion
7133       (gnus-summary-select-article))
7134     (setq gnus-current-window-configuration conf)
7135     (let* ((name (format "%s-%d"
7136                          (gnus-group-prefixed-name
7137                           gnus-newsgroup-name (list 'nndoc ""))
7138                          (save-excursion
7139                            (set-buffer gnus-summary-buffer)
7140                            gnus-current-article)))
7141            (ogroup gnus-newsgroup-name)
7142            (params (append (gnus-info-params (gnus-get-info ogroup))
7143                            (list (cons 'to-group ogroup))
7144                            (list (cons 'save-article-group ogroup))))
7145            (case-fold-search t)
7146            (buf (current-buffer))
7147            dig to-address)
7148       (save-excursion
7149         (set-buffer gnus-original-article-buffer)
7150         ;; Have the digest group inherit the main mail address of
7151         ;; the parent article.
7152         (when (setq to-address (or (message-fetch-field "reply-to")
7153                                    (message-fetch-field "from")))
7154           (setq params (append
7155                         (list (cons 'to-address
7156                                     (funcall gnus-decode-encoded-word-function
7157                                              to-address))))))
7158         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7159         (insert-buffer-substring gnus-original-article-buffer)
7160         ;; Remove lines that may lead nndoc to misinterpret the
7161         ;; document type.
7162         (narrow-to-region
7163          (goto-char (point-min))
7164          (or (search-forward "\n\n" nil t) (point)))
7165         (goto-char (point-min))
7166         (delete-matching-lines "^Path:\\|^From ")
7167         (widen))
7168       (unwind-protect
7169           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7170                     (gnus-newsgroup-ephemeral-ignored-charsets
7171                      gnus-newsgroup-ignored-charsets))
7172                 (gnus-group-read-ephemeral-group
7173                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7174                               (nndoc-article-type
7175                                ,(if force 'mbox 'guess))) t))
7176               ;; Make all postings to this group go to the parent group.
7177               (nconc (gnus-info-params (gnus-get-info name))
7178                      params)
7179             ;; Couldn't select this doc group.
7180             (switch-to-buffer buf)
7181             (gnus-set-global-variables)
7182             (gnus-configure-windows 'summary)
7183             (gnus-message 3 "Article couldn't be entered?"))
7184         (kill-buffer dig)))))
7185
7186 (defun gnus-summary-read-document (n)
7187   "Open a new group based on the current article(s).
7188 This will allow you to read digests and other similar
7189 documents as newsgroups.
7190 Obeys the standard process/prefix convention."
7191   (interactive "P")
7192   (let* ((articles (gnus-summary-work-articles n))
7193          (ogroup gnus-newsgroup-name)
7194          (params (append (gnus-info-params (gnus-get-info ogroup))
7195                          (list (cons 'to-group ogroup))))
7196          article group egroup groups vgroup)
7197     (while (setq article (pop articles))
7198       (setq group (format "%s-%d" gnus-newsgroup-name article))
7199       (gnus-summary-remove-process-mark article)
7200       (when (gnus-summary-display-article article)
7201         (save-excursion
7202           (with-temp-buffer
7203             (insert-buffer-substring gnus-original-article-buffer)
7204             ;; Remove some headers that may lead nndoc to make
7205             ;; the wrong guess.
7206             (message-narrow-to-head)
7207             (goto-char (point-min))
7208             (delete-matching-lines "^\\(Path\\):\\|^From ")
7209             (widen)
7210             (if (setq egroup
7211                       (gnus-group-read-ephemeral-group
7212                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7213                                      (nndoc-article-type guess))
7214                        t nil t))
7215                 (progn
7216                   ;; Make all postings to this group go to the parent group.
7217                   (nconc (gnus-info-params (gnus-get-info egroup))
7218                          params)
7219                   (push egroup groups))
7220               ;; Couldn't select this doc group.
7221               (gnus-error 3 "Article couldn't be entered"))))))
7222     ;; Now we have selected all the documents.
7223     (cond
7224      ((not groups)
7225       (error "None of the articles could be interpreted as documents"))
7226      ((gnus-group-read-ephemeral-group
7227        (setq vgroup (format
7228                      "nnvirtual:%s-%s" gnus-newsgroup-name
7229                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7230        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7231        t
7232        (cons (current-buffer) 'summary)))
7233      (t
7234       (error "Couldn't select virtual nndoc group")))))
7235
7236 (defun gnus-summary-isearch-article (&optional regexp-p)
7237   "Do incremental search forward on the current article.
7238 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7239   (interactive "P")
7240   (let* ((gnus-inhibit-treatment t)
7241          (old (gnus-summary-select-article)))
7242     (gnus-configure-windows 'article)
7243     (gnus-eval-in-buffer-window gnus-article-buffer
7244       (save-restriction
7245         (widen)
7246         (when (eq 'old old)
7247           (gnus-article-show-all-headers))
7248         (goto-char (point-min))
7249         (isearch-forward regexp-p)))))
7250
7251 (defun gnus-summary-search-article-forward (regexp &optional backward)
7252   "Search for an article containing REGEXP forward.
7253 If BACKWARD, search backward instead."
7254   (interactive
7255    (list (read-string
7256           (format "Search article %s (regexp%s): "
7257                   (if current-prefix-arg "backward" "forward")
7258                   (if gnus-last-search-regexp
7259                       (concat ", default " gnus-last-search-regexp)
7260                     "")))
7261          current-prefix-arg))
7262   (if (string-equal regexp "")
7263       (setq regexp (or gnus-last-search-regexp ""))
7264     (setq gnus-last-search-regexp regexp)
7265     (setq gnus-article-before-search gnus-current-article))
7266   ;; Intentionally set gnus-last-article.
7267   (setq gnus-last-article gnus-article-before-search)
7268   (let ((gnus-last-article gnus-last-article))
7269     (if (gnus-summary-search-article regexp backward)
7270         (gnus-summary-show-thread)
7271       (error "Search failed: \"%s\"" regexp))))
7272
7273 (defun gnus-summary-search-article-backward (regexp)
7274   "Search for an article containing REGEXP backward."
7275   (interactive
7276    (list (read-string
7277           (format "Search article backward (regexp%s): "
7278                   (if gnus-last-search-regexp
7279                       (concat ", default " gnus-last-search-regexp)
7280                     "")))))
7281   (gnus-summary-search-article-forward regexp 'backward))
7282
7283 (eval-when-compile
7284   (defmacro gnus-summary-search-article-position-point (regexp backward)
7285     "Dehighlight the last matched text and goto the beginning position."
7286     (` (if (and gnus-summary-search-article-matched-data
7287                 (let ((text (caddr gnus-summary-search-article-matched-data))
7288                       (inhibit-read-only t)
7289                       buffer-read-only)
7290                   (delete-region
7291                    (goto-char (car gnus-summary-search-article-matched-data))
7292                    (cadr gnus-summary-search-article-matched-data))
7293                   (insert text)
7294                   (string-match (, regexp) text)))
7295            (if (, backward) (beginning-of-line) (end-of-line))
7296          (goto-char (if (, backward) (point-max) (point-min))))))
7297
7298   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7299     "Place point where X-Face image is displayed."
7300     (if (featurep 'xemacs)
7301         (` (let ((end (if (search-forward "\n\n" nil t)
7302                           (goto-char (1- (point)))
7303                         (point-min)))
7304                  extent)
7305              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7306              (unless (and (re-search-forward "^From:" end t)
7307                           (setq extent (extent-at (point)))
7308                           (extent-begin-glyph extent))
7309                (goto-char (, opoint)))))
7310       (` (let ((end (if (search-forward "\n\n" nil t)
7311                         (goto-char (1- (point)))
7312                       (point-min)))
7313                (start (or (search-backward "\n\n" nil t) (point-min))))
7314            (goto-char
7315             (or (text-property-any start end 'x-face-image t);; x-face-e21
7316                 (text-property-any start end 'x-face-mule-bitmap-image t)
7317                 (, opoint)))))))
7318
7319   (defmacro gnus-summary-search-article-highlight-matched-text
7320     (backward treated x-face)
7321     "Highlight matched text in the function `gnus-summary-search-article'."
7322     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7323              (end (set-marker (make-marker) (match-end 0)))
7324              (inhibit-read-only t)
7325              buffer-read-only)
7326          (unless treated
7327            (let ((,@
7328                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7329                     (mapcar
7330                      (lambda (item) (setq items (delq item items)))
7331                      '(gnus-treat-buttonize
7332                        gnus-treat-fill-article
7333                        gnus-treat-fill-long-lines
7334                        gnus-treat-emphasize
7335                        gnus-treat-highlight-headers
7336                        gnus-treat-highlight-citation
7337                        gnus-treat-highlight-signature
7338                        gnus-treat-overstrike
7339                        gnus-treat-display-xface
7340                        gnus-treat-buttonize-head
7341                        gnus-treat-decode-article-as-default-mime-charset))
7342                     (static-if (featurep 'xemacs)
7343                         items
7344                       (cons '(x-face-mule-delete-x-face-field
7345                               (quote never))
7346                             items))))
7347                  (gnus-treat-display-xface
7348                   (when (, x-face) gnus-treat-display-xface)))
7349              (gnus-article-prepare-mime-display)))
7350          (goto-char (if (, backward) start end))
7351          (when (, x-face)
7352            (gnus-summary-search-article-highlight-goto-x-face (point)))
7353          (setq gnus-summary-search-article-matched-data
7354                (list start end (buffer-substring start end)))
7355          (unless (eq start end);; matched text has been deleted. :-<
7356            (put-text-property start end 'face
7357                               (or (find-face 'isearch)
7358                                   'secondary-selection))))))
7359   )
7360
7361 (defun gnus-summary-search-article (regexp &optional backward)
7362   "Search for an article containing REGEXP.
7363 Optional argument BACKWARD means do search for backward.
7364 `gnus-select-article-hook' is not called during the search."
7365   ;; We have to require this here to make sure that the following
7366   ;; dynamic binding isn't shadowed by autoloading.
7367   (require 'gnus-async)
7368   (require 'gnus-art)
7369   (let ((gnus-select-article-hook nil)  ;Disable hook.
7370         (gnus-article-display-hook nil)
7371         (gnus-article-prepare-hook nil)
7372         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7373         (gnus-use-article-prefetch nil)
7374         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7375         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7376         (sum (current-buffer))
7377         (found nil)
7378         point treated)
7379     (gnus-save-hidden-threads
7380       (static-if (featurep 'xemacs)
7381           (let ((gnus-inhibit-treatment t))
7382             (setq treated (eq 'old (gnus-summary-select-article)))
7383             (when (and treated
7384                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7385                                  (window-live-p (get-buffer-window
7386                                                  gnus-article-buffer t)))))
7387               (gnus-summary-select-article nil t)
7388               (setq treated nil)))
7389         (let ((gnus-inhibit-treatment t)
7390               (x-face-mule-delete-x-face-field 'never))
7391           (setq treated (eq 'old (gnus-summary-select-article)))
7392           (when (and treated
7393                      (not
7394                       (and (gnus-buffer-live-p gnus-article-buffer)
7395                            (window-live-p (get-buffer-window
7396                                            gnus-article-buffer t))
7397                            (or (not (string-match "^\\^X-Face:" regexp))
7398                                (with-current-buffer gnus-article-buffer
7399                                  gnus-summary-search-article-matched-data)))))
7400             (gnus-summary-select-article nil t)
7401             (setq treated nil))))
7402       (set-buffer gnus-article-buffer)
7403       (widen)
7404       (if treated
7405           (progn
7406             (gnus-article-show-all-headers)
7407             (gnus-summary-search-article-position-point regexp backward))
7408         (goto-char (if backward (point-max) (point-min))))
7409       (while (not found)
7410         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7411         (if (if backward
7412                 (re-search-backward regexp nil t)
7413               (re-search-forward regexp nil t))
7414             ;; We found the regexp.
7415             (progn
7416               (gnus-summary-search-article-highlight-matched-text
7417                backward treated (string-match "^\\^X-Face:" regexp))
7418               (setq found 'found)
7419               (forward-line
7420                (/ (- 2 (window-height
7421                         (get-buffer-window gnus-article-buffer t)))
7422                   2))
7423               (set-window-start
7424                (get-buffer-window (current-buffer))
7425                (point))
7426               (set-buffer sum)
7427               (setq point (point)))
7428           ;; We didn't find it, so we go to the next article.
7429           (set-buffer sum)
7430           (setq found 'not)
7431           (while (eq found 'not)
7432             (if (not (if backward (gnus-summary-find-prev)
7433                        (gnus-summary-find-next)))
7434                 ;; No more articles.
7435                 (setq found t)
7436               ;; Select the next article and adjust point.
7437               (unless (gnus-summary-article-sparse-p
7438                        (gnus-summary-article-number))
7439                 (setq found nil)
7440                 (let ((gnus-inhibit-treatment t))
7441                   (gnus-summary-select-article))
7442                 (setq treated nil)
7443                 (set-buffer gnus-article-buffer)
7444                 (widen)
7445                 (goto-char (if backward (point-max) (point-min))))))))
7446       (gnus-message 7 ""))
7447     ;; Return whether we found the regexp.
7448     (when (eq found 'found)
7449       (goto-char point)
7450       (gnus-summary-show-thread)
7451       (gnus-summary-goto-subject gnus-current-article)
7452       (gnus-summary-position-point)
7453       t)))
7454
7455 (defun gnus-summary-find-matching (header regexp &optional backward unread
7456                                           not-case-fold)
7457   "Return a list of all articles that match REGEXP on HEADER.
7458 The search stars on the current article and goes forwards unless
7459 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7460 If UNREAD is non-nil, only unread articles will
7461 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7462 in the comparisons."
7463   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7464                 (gnus-data-find-list
7465                  (gnus-summary-article-number) (gnus-data-list backward))))
7466         (case-fold-search (not not-case-fold))
7467         articles d func)
7468     (if (consp header)
7469         (if (eq (car header) 'extra)
7470             (setq func
7471                   `(lambda (h)
7472                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7473                          "")))
7474           (error "%s is an invalid header" header))
7475       (unless (fboundp (intern (concat "mail-header-" header)))
7476         (error "%s is not a valid header" header))
7477       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7478     (while data
7479       (setq d (car data))
7480       (and (or (not unread)             ; We want all articles...
7481                (gnus-data-unread-p d))  ; Or just unreads.
7482            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7483            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7484            (push (gnus-data-number d) articles)) ; Success!
7485       (setq data (cdr data)))
7486     (nreverse articles)))
7487
7488 (defun gnus-summary-execute-command (header regexp command &optional backward)
7489   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7490 If HEADER is an empty string (or nil), the match is done on the entire
7491 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7492   (interactive
7493    (list (let ((completion-ignore-case t))
7494            (completing-read
7495             "Header name: "
7496             (mapcar (lambda (string) (list string))
7497                     '("Number" "Subject" "From" "Lines" "Date"
7498                       "Message-ID" "Xref" "References" "Body"))
7499             nil 'require-match))
7500          (read-string "Regexp: ")
7501          (read-key-sequence "Command: ")
7502          current-prefix-arg))
7503   (when (equal header "Body")
7504     (setq header ""))
7505   ;; Hidden thread subtrees must be searched as well.
7506   (gnus-summary-show-all-threads)
7507   ;; We don't want to change current point nor window configuration.
7508   (save-excursion
7509     (save-window-excursion
7510       (gnus-message 6 "Executing %s..." (key-description command))
7511       ;; We'd like to execute COMMAND interactively so as to give arguments.
7512       (gnus-execute header regexp
7513                     `(call-interactively ',(key-binding command))
7514                     backward)
7515       (gnus-message 6 "Executing %s...done" (key-description command)))))
7516
7517 (defun gnus-summary-beginning-of-article ()
7518   "Scroll the article back to the beginning."
7519   (interactive)
7520   (gnus-summary-select-article)
7521   (gnus-configure-windows 'article)
7522   (gnus-eval-in-buffer-window gnus-article-buffer
7523     (widen)
7524     (goto-char (point-min))
7525     (when gnus-page-broken
7526       (gnus-narrow-to-page))))
7527
7528 (defun gnus-summary-end-of-article ()
7529   "Scroll to the end of the article."
7530   (interactive)
7531   (gnus-summary-select-article)
7532   (gnus-configure-windows 'article)
7533   (gnus-eval-in-buffer-window gnus-article-buffer
7534     (widen)
7535     (goto-char (point-max))
7536     (recenter -3)
7537     (when gnus-page-broken
7538       (gnus-narrow-to-page))))
7539
7540 (defun gnus-summary-print-article (&optional filename n)
7541   "Generate and print a PostScript image of the N next (mail) articles.
7542
7543 If N is negative, print the N previous articles.  If N is nil and articles
7544 have been marked with the process mark, print these instead.
7545
7546 If the optional first argument FILENAME is nil, send the image to the
7547 printer.  If FILENAME is a string, save the PostScript image in a file with
7548 that name.  If FILENAME is a number, prompt the user for the name of the file
7549 to save in."
7550   (interactive (list (ps-print-preprint current-prefix-arg)))
7551   (dolist (article (gnus-summary-work-articles n))
7552     (gnus-summary-select-article nil nil 'pseudo article)
7553     (gnus-eval-in-buffer-window gnus-article-buffer
7554       (let ((buffer (generate-new-buffer " *print*")))
7555         (unwind-protect
7556             (progn
7557               (copy-to-buffer buffer (point-min) (point-max))
7558               (set-buffer buffer)
7559               (gnus-article-delete-invisible-text)
7560               (let ((ps-left-header
7561                      (list
7562                       (concat "("
7563                               (mail-header-subject gnus-current-headers) ")")
7564                       (concat "("
7565                               (mail-header-from gnus-current-headers) ")")))
7566                     (ps-right-header
7567                      (list
7568                       "/pagenumberstring load"
7569                       (concat "("
7570                               (mail-header-date gnus-current-headers) ")"))))
7571                 (gnus-run-hooks 'gnus-ps-print-hook)
7572                 (save-excursion
7573                   (ps-spool-buffer-with-faces))))
7574           (kill-buffer buffer))))
7575     (gnus-summary-remove-process-mark article))
7576   (ps-despool filename))
7577
7578 (defun gnus-summary-show-article (&optional arg)
7579   "Force re-fetching of the current article.
7580 If ARG (the prefix) is a number, show the article with the charset
7581 defined in `gnus-summary-show-article-charset-alist', or the charset
7582 inputed.
7583 If ARG (the prefix) is non-nil and not a number, show the raw article
7584 without any article massaging functions being run."
7585   (interactive "P")
7586   (cond
7587    ((numberp arg)
7588     (let ((gnus-newsgroup-charset
7589            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7590                (read-coding-system "Charset: ")))
7591           (gnus-newsgroup-ignored-charsets 'gnus-all))
7592       (gnus-summary-select-article nil 'force)
7593       (let ((deps gnus-newsgroup-dependencies)
7594             head header)
7595         (save-excursion
7596           (set-buffer gnus-original-article-buffer)
7597           (save-restriction
7598             (message-narrow-to-head)
7599             (setq head (buffer-string)))
7600           (with-temp-buffer
7601             (insert (format "211 %d Article retrieved.\n"
7602                             (cdr gnus-article-current)))
7603             (insert head)
7604             (insert ".\n")
7605             (let ((nntp-server-buffer (current-buffer)))
7606               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7607         (gnus-data-set-header
7608          (gnus-data-find (cdr gnus-article-current))
7609          header)
7610         (gnus-summary-update-article-line
7611          (cdr gnus-article-current) header))))
7612    ((not arg)
7613     ;; Select the article the normal way.
7614     (gnus-summary-select-article nil 'force))
7615    (t
7616     ;; We have to require this here to make sure that the following
7617     ;; dynamic binding isn't shadowed by autoloading.
7618     (require 'gnus-async)
7619     (require 'gnus-art)
7620     ;; Bind the article treatment functions to nil.
7621     (let ((gnus-have-all-headers t)
7622           gnus-article-display-hook
7623           gnus-article-prepare-hook
7624           gnus-article-decode-hook
7625           gnus-break-pages
7626           gnus-show-mime)
7627       (gnus-summary-select-article nil 'force))))
7628   (gnus-summary-goto-subject gnus-current-article)
7629   (gnus-summary-position-point))
7630
7631 (defun gnus-summary-verbose-headers (&optional arg)
7632   "Toggle permanent full header display.
7633 If ARG is a positive number, turn header display on.
7634 If ARG is a negative number, turn header display off."
7635   (interactive "P")
7636   (setq gnus-show-all-headers
7637         (cond ((or (not (numberp arg))
7638                    (zerop arg))
7639                (not gnus-show-all-headers))
7640               ((natnump arg)
7641                t)))
7642   (gnus-summary-show-article))
7643
7644 (defun gnus-summary-toggle-header (&optional arg)
7645   "Show the headers if they are hidden, or hide them if they are shown.
7646 If ARG is a positive number, show the entire header.
7647 If ARG is a negative number, hide the unwanted header lines."
7648   (interactive "P")
7649   (save-excursion
7650     (set-buffer gnus-article-buffer)
7651     (save-restriction
7652       (let* ((buffer-read-only nil)
7653              (inhibit-point-motion-hooks t)
7654              hidden e)
7655         (setq hidden
7656               (if (numberp arg)
7657                   (>= arg 0)
7658                 (save-restriction
7659                   (article-narrow-to-head)
7660                   (gnus-article-hidden-text-p 'headers))))
7661         (goto-char (point-min))
7662         (when (search-forward "\n\n" nil t)
7663           (delete-region (point-min) (1- (point))))
7664         (goto-char (point-min))
7665         (save-excursion
7666           (set-buffer gnus-original-article-buffer)
7667           (goto-char (point-min))
7668           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7669         (insert-buffer-substring gnus-original-article-buffer 1 e)
7670         (save-restriction
7671           (narrow-to-region (point-min) (point))
7672           (article-decode-encoded-words)
7673           (if  hidden
7674               (let ((gnus-treat-hide-headers nil)
7675                     (gnus-treat-hide-boring-headers nil))
7676                 (setq gnus-article-wash-types
7677                       (delq 'headers gnus-article-wash-types))
7678                 (gnus-treat-article 'head))
7679             (gnus-treat-article 'head)))
7680         (gnus-set-mode-line 'article)))))
7681
7682 (defun gnus-summary-show-all-headers ()
7683   "Make all header lines visible."
7684   (interactive)
7685   (gnus-article-show-all-headers))
7686
7687 (defun gnus-summary-toggle-mime (&optional arg)
7688   "Toggle MIME processing.
7689 If ARG is a positive number, turn MIME processing on."
7690   (interactive "P")
7691   (setq gnus-show-mime
7692         (if (null arg)
7693             (not gnus-show-mime)
7694           (> (prefix-numeric-value arg) 0)))
7695   (gnus-summary-select-article t 'force))
7696
7697 (defun gnus-summary-caesar-message (&optional arg)
7698   "Caesar rotate the current article by 13.
7699 The numerical prefix specifies how many places to rotate each letter
7700 forward."
7701   (interactive "P")
7702   (gnus-summary-select-article)
7703   (let ((mail-header-separator ""))
7704     (gnus-eval-in-buffer-window gnus-article-buffer
7705       (save-restriction
7706         (widen)
7707         (let ((start (window-start))
7708               buffer-read-only)
7709           (message-caesar-buffer-body arg)
7710           (set-window-start (get-buffer-window (current-buffer)) start))))))
7711
7712 (defun gnus-summary-stop-page-breaking ()
7713   "Stop page breaking in the current article."
7714   (interactive)
7715   (gnus-summary-select-article)
7716   (gnus-eval-in-buffer-window gnus-article-buffer
7717     (widen)
7718     (when (gnus-visual-p 'page-marker)
7719       (let ((buffer-read-only nil))
7720         (gnus-remove-text-with-property 'gnus-prev)
7721         (gnus-remove-text-with-property 'gnus-next))
7722       (setq gnus-page-broken nil))))
7723
7724 (defun gnus-summary-move-article (&optional n to-newsgroup
7725                                             select-method action)
7726   "Move the current article to a different newsgroup.
7727 If N is a positive number, move the N next articles.
7728 If N is a negative number, move the N previous articles.
7729 If N is nil and any articles have been marked with the process mark,
7730 move those articles instead.
7731 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7732 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7733 re-spool using this method.
7734
7735 For this function to work, both the current newsgroup and the
7736 newsgroup that you want to move to have to support the `request-move'
7737 and `request-accept' functions.
7738
7739 ACTION can be either `move' (the default), `crosspost' or `copy'."
7740   (interactive "P")
7741   (unless action
7742     (setq action 'move))
7743   ;; Disable marking as read.
7744   (let (gnus-mark-article-hook)
7745     (save-window-excursion
7746       (gnus-summary-select-article)))
7747   ;; Check whether the source group supports the required functions.
7748   (cond ((and (eq action 'move)
7749               (not (gnus-check-backend-function
7750                     'request-move-article gnus-newsgroup-name)))
7751          (error "The current group does not support article moving"))
7752         ((and (eq action 'crosspost)
7753               (not (gnus-check-backend-function
7754                     'request-replace-article gnus-newsgroup-name)))
7755          (error "The current group does not support article editing")))
7756   (let ((articles (gnus-summary-work-articles n))
7757         (prefix (if (gnus-check-backend-function
7758                     'request-move-article gnus-newsgroup-name)
7759                     (gnus-group-real-prefix gnus-newsgroup-name)
7760                   ""))
7761         (names '((move "Move" "Moving")
7762                  (copy "Copy" "Copying")
7763                  (crosspost "Crosspost" "Crossposting")))
7764         (copy-buf (save-excursion
7765                     (nnheader-set-temp-buffer " *copy article*")))
7766         (default-marks gnus-article-mark-lists)
7767         (no-expire-marks (delete '(expirable . expire)
7768                                  (copy-sequence gnus-article-mark-lists)))
7769         art-group to-method new-xref article to-groups)
7770     (unless (assq action names)
7771       (error "Unknown action %s" action))
7772     ;; Read the newsgroup name.
7773     (when (and (not to-newsgroup)
7774                (not select-method))
7775       (setq to-newsgroup
7776             (gnus-read-move-group-name
7777              (cadr (assq action names))
7778              (symbol-value (intern (format "gnus-current-%s-group" action)))
7779              articles prefix))
7780       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7781     (setq to-method (or select-method
7782                         (gnus-server-to-method
7783                          (gnus-group-method to-newsgroup))))
7784     ;; Check the method we are to move this article to...
7785     (unless (gnus-check-backend-function
7786              'request-accept-article (car to-method))
7787       (error "%s does not support article copying" (car to-method)))
7788     (unless (gnus-check-server to-method)
7789       (error "Can't open server %s" (car to-method)))
7790     (gnus-message 6 "%s to %s: %s..."
7791                   (caddr (assq action names))
7792                   (or (car select-method) to-newsgroup) articles)
7793     (while articles
7794       (setq article (pop articles))
7795       (setq
7796        art-group
7797        (cond
7798         ;; Move the article.
7799         ((eq action 'move)
7800          ;; Remove this article from future suppression.
7801          (gnus-dup-unsuppress-article article)
7802          (gnus-request-move-article
7803           article                       ; Article to move
7804           gnus-newsgroup-name           ; From newsgroup
7805           (nth 1 (gnus-find-method-for-group
7806                   gnus-newsgroup-name)) ; Server
7807           (list 'gnus-request-accept-article
7808                 to-newsgroup (list 'quote select-method)
7809                 (not articles) t)       ; Accept form
7810           (not articles)))              ; Only save nov last time
7811         ;; Copy the article.
7812         ((eq action 'copy)
7813          (save-excursion
7814            (set-buffer copy-buf)
7815            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7816              (gnus-request-accept-article
7817               to-newsgroup select-method (not articles) t))))
7818         ;; Crosspost the article.
7819         ((eq action 'crosspost)
7820          (let ((xref (message-tokenize-header
7821                       (mail-header-xref (gnus-summary-article-header article))
7822                       " ")))
7823            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7824                                   ":" article))
7825            (unless xref
7826              (setq xref (list (system-name))))
7827            (setq new-xref
7828                  (concat
7829                   (mapconcat 'identity
7830                              (delete "Xref:" (delete new-xref xref))
7831                              " ")
7832                   " " new-xref))
7833            (save-excursion
7834              (set-buffer copy-buf)
7835              ;; First put the article in the destination group.
7836              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7837              (when (consp (setq art-group
7838                                 (gnus-request-accept-article
7839                                  to-newsgroup select-method (not articles))))
7840                (setq new-xref (concat new-xref " " (car art-group)
7841                                       ":" (cdr art-group)))
7842                ;; Now we have the new Xrefs header, so we insert
7843                ;; it and replace the new article.
7844                (nnheader-replace-header "Xref" new-xref)
7845                (gnus-request-replace-article
7846                 (cdr art-group) to-newsgroup (current-buffer))
7847                art-group))))))
7848       (cond
7849        ((not art-group)
7850         (gnus-message 1 "Couldn't %s article %s: %s"
7851                       (cadr (assq action names)) article
7852                       (nnheader-get-report (car to-method))))
7853        ((eq art-group 'junk)
7854         (when (eq action 'move)
7855           (gnus-summary-mark-article article gnus-canceled-mark)
7856           (gnus-message 4 "Deleted article %s" article)))
7857        (t
7858         (let* ((pto-group (gnus-group-prefixed-name
7859                            (car art-group) to-method))
7860                (entry
7861                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7862                (info (nth 2 entry))
7863                (to-group (gnus-info-group info))
7864                to-marks)
7865           ;; Update the group that has been moved to.
7866           (when (and info
7867                      (memq action '(move copy)))
7868             (unless (member to-group to-groups)
7869               (push to-group to-groups))
7870
7871             (unless (memq article gnus-newsgroup-unreads)
7872               (push 'read to-marks)
7873               (gnus-info-set-read
7874                info (gnus-add-to-range (gnus-info-read info)
7875                                        (list (cdr art-group)))))
7876
7877             ;; See whether the article is to be put in the cache.
7878             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7879                              default-marks
7880                            no-expire-marks))
7881                   (to-article (cdr art-group)))
7882
7883               ;; Enter the article into the cache in the new group,
7884               ;; if that is required.
7885               (when gnus-use-cache
7886                 (gnus-cache-possibly-enter-article
7887                  to-group to-article
7888                  (let ((header (copy-sequence
7889                                 (gnus-summary-article-header article))))
7890                    (mail-header-set-number header to-article)
7891                    header)
7892                  (memq article gnus-newsgroup-marked)
7893                  (memq article gnus-newsgroup-dormant)
7894                  (memq article gnus-newsgroup-unreads)))
7895
7896               (when gnus-preserve-marks
7897                 ;; Copy any marks over to the new group.
7898                 (when (and (equal to-group gnus-newsgroup-name)
7899                            (not (memq article gnus-newsgroup-unreads)))
7900                   ;; Mark this article as read in this group.
7901                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7902                   (setcdr (gnus-active to-group) to-article)
7903                   (setcdr gnus-newsgroup-active to-article))
7904
7905                 (while marks
7906                   (when (memq article (symbol-value
7907                                        (intern (format "gnus-newsgroup-%s"
7908                                                        (caar marks)))))
7909                     (push (cdar marks) to-marks)
7910                     ;; If the other group is the same as this group,
7911                     ;; then we have to add the mark to the list.
7912                     (when (equal to-group gnus-newsgroup-name)
7913                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7914                            (cons to-article
7915                                  (symbol-value
7916                                   (intern (format "gnus-newsgroup-%s"
7917                                                   (caar marks)))))))
7918                     ;; Copy the marks to other group.
7919                     (gnus-add-marked-articles
7920                      to-group (cdar marks) (list to-article) info))
7921                   (setq marks (cdr marks)))
7922
7923                 (gnus-request-set-mark to-group (list (list (list to-article)
7924                                                             'set
7925                                                             to-marks))))
7926
7927               (gnus-dribble-enter
7928                (concat "(gnus-group-set-info '"
7929                        (gnus-prin1-to-string (gnus-get-info to-group))
7930                        ")"))))
7931
7932           ;; Update the Xref header in this article to point to
7933           ;; the new crossposted article we have just created.
7934           (when (eq action 'crosspost)
7935             (save-excursion
7936               (set-buffer copy-buf)
7937               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7938               (nnheader-replace-header "Xref" new-xref)
7939               (gnus-request-replace-article
7940                article gnus-newsgroup-name (current-buffer)))))
7941
7942         ;;;!!!Why is this necessary?
7943         (set-buffer gnus-summary-buffer)
7944
7945         (gnus-summary-goto-subject article)
7946         (when (eq action 'move)
7947           (gnus-summary-mark-article article gnus-canceled-mark))))
7948       (gnus-summary-remove-process-mark article))
7949     ;; Re-activate all groups that have been moved to.
7950     (while to-groups
7951       (save-excursion
7952         (set-buffer gnus-group-buffer)
7953         (when (gnus-group-goto-group (car to-groups) t)
7954           (gnus-group-get-new-news-this-group 1 t))
7955         (pop to-groups)))
7956
7957     (gnus-kill-buffer copy-buf)
7958     (gnus-summary-position-point)
7959     (gnus-set-mode-line 'summary)))
7960
7961 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7962   "Move the current article to a different newsgroup.
7963 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7964 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7965 re-spool using this method."
7966   (interactive "P")
7967   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7968
7969 (defun gnus-summary-crosspost-article (&optional n)
7970   "Crosspost the current article to some other group."
7971   (interactive "P")
7972   (gnus-summary-move-article n nil nil 'crosspost))
7973
7974 (defcustom gnus-summary-respool-default-method nil
7975   "Default method for respooling an article.
7976 If nil, use to the current newsgroup method."
7977   :type '(choice (gnus-select-method :value (nnml ""))
7978                  (const nil))
7979   :group 'gnus-summary-mail)
7980
7981 (defun gnus-summary-respool-article (&optional n method)
7982   "Respool the current article.
7983 The article will be squeezed through the mail spooling process again,
7984 which means that it will be put in some mail newsgroup or other
7985 depending on `nnmail-split-methods'.
7986 If N is a positive number, respool the N next articles.
7987 If N is a negative number, respool the N previous articles.
7988 If N is nil and any articles have been marked with the process mark,
7989 respool those articles instead.
7990
7991 Respooling can be done both from mail groups and \"real\" newsgroups.
7992 In the former case, the articles in question will be moved from the
7993 current group into whatever groups they are destined to.  In the
7994 latter case, they will be copied into the relevant groups."
7995   (interactive
7996    (list current-prefix-arg
7997          (let* ((methods (gnus-methods-using 'respool))
7998                 (methname
7999                  (symbol-name (or gnus-summary-respool-default-method
8000                                   (car (gnus-find-method-for-group
8001                                         gnus-newsgroup-name)))))
8002                 (method
8003                  (gnus-completing-read
8004                   methname "What backend do you want to use when respooling?"
8005                   methods nil t nil 'gnus-mail-method-history))
8006                 ms)
8007            (cond
8008             ((zerop (length (setq ms (gnus-servers-using-backend
8009                                       (intern method)))))
8010              (list (intern method) ""))
8011             ((= 1 (length ms))
8012              (car ms))
8013             (t
8014              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8015                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8016                            ms-alist))))))))
8017   (unless method
8018     (error "No method given for respooling"))
8019   (if (assoc (symbol-name
8020               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8021              (gnus-methods-using 'respool))
8022       (gnus-summary-move-article n nil method)
8023     (gnus-summary-copy-article n nil method)))
8024
8025 (defun gnus-summary-import-article (file)
8026   "Import an arbitrary file into a mail newsgroup."
8027   (interactive "fImport file: ")
8028   (let ((group gnus-newsgroup-name)
8029         (now (current-time))
8030         atts lines)
8031     (unless (gnus-check-backend-function 'request-accept-article group)
8032       (error "%s does not support article importing" group))
8033     (or (file-readable-p file)
8034         (not (file-regular-p file))
8035         (error "Can't read %s" file))
8036     (save-excursion
8037       (set-buffer (gnus-get-buffer-create " *import file*"))
8038       (erase-buffer)
8039       (nnheader-insert-file-contents file)
8040       (goto-char (point-min))
8041       (unless (nnheader-article-p)
8042         ;; This doesn't look like an article, so we fudge some headers.
8043         (setq atts (file-attributes file)
8044               lines (count-lines (point-min) (point-max)))
8045         (insert "From: " (read-string "From: ") "\n"
8046                 "Subject: " (read-string "Subject: ") "\n"
8047                 "Date: " (message-make-date (nth 5 atts))
8048                 "\n"
8049                 "Message-ID: " (message-make-message-id) "\n"
8050                 "Lines: " (int-to-string lines) "\n"
8051                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8052       (gnus-request-accept-article group nil t)
8053       (kill-buffer (current-buffer)))))
8054
8055 (defun gnus-summary-article-posted-p ()
8056   "Say whether the current (mail) article is available from news as well.
8057 This will be the case if the article has both been mailed and posted."
8058   (interactive)
8059   (let ((id (mail-header-references (gnus-summary-article-header)))
8060         (gnus-override-method (car (gnus-refer-article-methods))))
8061     (if (gnus-request-head id "")
8062         (gnus-message 2 "The current message was found on %s"
8063                       gnus-override-method)
8064       (gnus-message 2 "The current message couldn't be found on %s"
8065                     gnus-override-method)
8066       nil)))
8067
8068 (defun gnus-summary-expire-articles (&optional now)
8069   "Expire all articles that are marked as expirable in the current group."
8070   (interactive)
8071   (when (gnus-check-backend-function
8072          'request-expire-articles gnus-newsgroup-name)
8073     ;; This backend supports expiry.
8074     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8075            (expirable (if total
8076                           (progn
8077                             ;; We need to update the info for
8078                             ;; this group for `gnus-list-of-read-articles'
8079                             ;; to give us the right answer.
8080                             (gnus-run-hooks 'gnus-exit-group-hook)
8081                             (gnus-summary-update-info)
8082                             (gnus-list-of-read-articles gnus-newsgroup-name))
8083                         (setq gnus-newsgroup-expirable
8084                               (sort gnus-newsgroup-expirable '<))))
8085            (expiry-wait (if now 'immediate
8086                           (gnus-group-find-parameter
8087                            gnus-newsgroup-name 'expiry-wait)))
8088            (nnmail-expiry-target
8089             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8090                 nnmail-expiry-target))
8091            es)
8092       (when expirable
8093         ;; There are expirable articles in this group, so we run them
8094         ;; through the expiry process.
8095         (gnus-message 6 "Expiring articles...")
8096         (unless (gnus-check-group gnus-newsgroup-name)
8097           (error "Can't open server for %s" gnus-newsgroup-name))
8098         ;; The list of articles that weren't expired is returned.
8099         (save-excursion
8100           (if expiry-wait
8101               (let ((nnmail-expiry-wait-function nil)
8102                     (nnmail-expiry-wait expiry-wait))
8103                 (setq es (gnus-request-expire-articles
8104                           expirable gnus-newsgroup-name)))
8105             (setq es (gnus-request-expire-articles
8106                       expirable gnus-newsgroup-name)))
8107           (unless total
8108             (setq gnus-newsgroup-expirable es))
8109           ;; We go through the old list of expirable, and mark all
8110           ;; really expired articles as nonexistent.
8111           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8112             (let ((gnus-use-cache nil))
8113               (while expirable
8114                 (unless (memq (car expirable) es)
8115                   (when (gnus-data-find (car expirable))
8116                     (gnus-summary-mark-article
8117                      (car expirable) gnus-canceled-mark)))
8118                 (setq expirable (cdr expirable))))))
8119         (gnus-message 6 "Expiring articles...done")))))
8120
8121 (defun gnus-summary-expire-articles-now ()
8122   "Expunge all expirable articles in the current group.
8123 This means that *all* articles that are marked as expirable will be
8124 deleted forever, right now."
8125   (interactive)
8126   (or gnus-expert-user
8127       (gnus-yes-or-no-p
8128        "Are you really, really, really sure you want to delete all these messages? ")
8129       (error "Phew!"))
8130   (gnus-summary-expire-articles t))
8131
8132 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8133 (defun gnus-summary-delete-article (&optional n)
8134   "Delete the N next (mail) articles.
8135 This command actually deletes articles.  This is not a marking
8136 command.  The article will disappear forever from your life, never to
8137 return.
8138 If N is negative, delete backwards.
8139 If N is nil and articles have been marked with the process mark,
8140 delete these instead."
8141   (interactive "P")
8142   (unless (gnus-check-backend-function 'request-expire-articles
8143                                        gnus-newsgroup-name)
8144     (error "The current newsgroup does not support article deletion"))
8145   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8146     (error "Couldn't open server"))
8147   ;; Compute the list of articles to delete.
8148   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8149         not-deleted)
8150     (if (and gnus-novice-user
8151              (not (gnus-yes-or-no-p
8152                    (format "Do you really want to delete %s forever? "
8153                            (if (> (length articles) 1)
8154                                (format "these %s articles" (length articles))
8155                              "this article")))))
8156         ()
8157       ;; Delete the articles.
8158       (setq not-deleted (gnus-request-expire-articles
8159                          articles gnus-newsgroup-name 'force))
8160       (while articles
8161         (gnus-summary-remove-process-mark (car articles))
8162         ;; The backend might not have been able to delete the article
8163         ;; after all.
8164         (unless (memq (car articles) not-deleted)
8165           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8166         (setq articles (cdr articles)))
8167       (when not-deleted
8168         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8169     (gnus-summary-position-point)
8170     (gnus-set-mode-line 'summary)
8171     not-deleted))
8172
8173 (defun gnus-summary-edit-article (&optional force)
8174   "Edit the current article.
8175 This will have permanent effect only in mail groups.
8176 If FORCE is non-nil, allow editing of articles even in read-only
8177 groups."
8178   (interactive "P")
8179   (save-excursion
8180     (set-buffer gnus-summary-buffer)
8181     (let ((mail-parse-charset gnus-newsgroup-charset)
8182           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8183       (gnus-set-global-variables)
8184       (when (and (not force)
8185                  (gnus-group-read-only-p))
8186         (error "The current newsgroup does not support article editing"))
8187       (gnus-summary-show-article t)
8188       (gnus-article-edit-article
8189        'ignore
8190        `(lambda (no-highlight)
8191           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8192                 (message-options message-options)
8193                 (message-options-set-recipient)
8194                 (mail-parse-ignored-charsets
8195                  ',gnus-newsgroup-ignored-charsets))
8196             (gnus-summary-edit-article-done
8197              ,(or (mail-header-references gnus-current-headers) "")
8198              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8199
8200 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8201
8202 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8203                                                  no-highlight)
8204   "Make edits to the current article permanent."
8205   (interactive)
8206   (save-excursion
8207     ;; The buffer restriction contains the entire article if it exists.
8208     (when (article-goto-body)
8209       (let ((lines (count-lines (point) (point-max)))
8210             (length (- (point-max) (point)))
8211             (case-fold-search t)
8212             (body (copy-marker (point))))
8213         (goto-char (point-min))
8214         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8215           (delete-region (match-beginning 1) (match-end 1))
8216           (insert (number-to-string length)))
8217         (goto-char (point-min))
8218         (when (re-search-forward
8219                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8220           (delete-region (match-beginning 1) (match-end 1))
8221           (insert (number-to-string length)))
8222         (goto-char (point-min))
8223         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8224           (delete-region (match-beginning 1) (match-end 1))
8225           (insert (number-to-string lines))))))
8226   ;; Replace the article.
8227   (let ((buf (current-buffer)))
8228     (with-temp-buffer
8229       (insert-buffer-substring buf)
8230
8231       (if (and (not read-only)
8232                (not (gnus-request-replace-article
8233                      (cdr gnus-article-current) (car gnus-article-current)
8234                      (current-buffer) t)))
8235           (error "Couldn't replace article")
8236         ;; Update the summary buffer.
8237         (if (and references
8238                  (equal (message-tokenize-header references " ")
8239                         (message-tokenize-header
8240                          (or (message-fetch-field "references") "") " ")))
8241             ;; We only have to update this line.
8242             (save-excursion
8243               (save-restriction
8244                 (message-narrow-to-head)
8245                 (let ((head (buffer-string))
8246                       header)
8247                   (with-temp-buffer
8248                     (insert (format "211 %d Article retrieved.\n"
8249                                     (cdr gnus-article-current)))
8250                     (insert head)
8251                     (insert ".\n")
8252                     (let ((nntp-server-buffer (current-buffer)))
8253                       (setq header (car (gnus-get-newsgroup-headers
8254                                          (save-excursion
8255                                            (set-buffer gnus-summary-buffer)
8256                                            gnus-newsgroup-dependencies)
8257                                          t))))
8258                     (save-excursion
8259                       (set-buffer gnus-summary-buffer)
8260                       (gnus-data-set-header
8261                        (gnus-data-find (cdr gnus-article-current))
8262                        header)
8263                       (gnus-summary-update-article-line
8264                        (cdr gnus-article-current) header))))))
8265           ;; Update threads.
8266           (set-buffer (or buffer gnus-summary-buffer))
8267           (gnus-summary-update-article (cdr gnus-article-current)))
8268         ;; Prettify the article buffer again.
8269         (unless no-highlight
8270           (save-excursion
8271             (set-buffer gnus-article-buffer)
8272             ;;;!!! Fix this -- article should be rehighlighted.
8273             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8274             (set-buffer gnus-original-article-buffer)
8275             (gnus-request-article
8276              (cdr gnus-article-current)
8277              (car gnus-article-current) (current-buffer))))
8278         ;; Prettify the summary buffer line.
8279         (when (gnus-visual-p 'summary-highlight 'highlight)
8280           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8281
8282 (defun gnus-summary-edit-wash (key)
8283   "Perform editing command KEY in the article buffer."
8284   (interactive
8285    (list
8286     (progn
8287       (message "%s" (concat (this-command-keys) "- "))
8288       (read-char))))
8289   (message "")
8290   (gnus-summary-edit-article)
8291   (execute-kbd-macro (concat (this-command-keys) key))
8292   (gnus-article-edit-done))
8293
8294 ;;; Respooling
8295
8296 (defun gnus-summary-respool-query (&optional silent trace)
8297   "Query where the respool algorithm would put this article."
8298   (interactive)
8299   (let (gnus-mark-article-hook)
8300     (gnus-summary-select-article)
8301     (save-excursion
8302       (set-buffer gnus-original-article-buffer)
8303       (save-restriction
8304         (message-narrow-to-head)
8305         (let ((groups (nnmail-article-group 'identity trace)))
8306           (unless silent
8307             (if groups
8308                 (message "This message would go to %s"
8309                          (mapconcat 'car groups ", "))
8310               (message "This message would go to no groups"))
8311             groups))))))
8312
8313 (defun gnus-summary-respool-trace ()
8314   "Trace where the respool algorithm would put this article.
8315 Display a buffer showing all fancy splitting patterns which matched."
8316   (interactive)
8317   (gnus-summary-respool-query nil t))
8318
8319 ;; Summary marking commands.
8320
8321 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8322   "Mark articles which has the same subject as read, and then select the next.
8323 If UNMARK is positive, remove any kind of mark.
8324 If UNMARK is negative, tick articles."
8325   (interactive "P")
8326   (when unmark
8327     (setq unmark (prefix-numeric-value unmark)))
8328   (let ((count
8329          (gnus-summary-mark-same-subject
8330           (gnus-summary-article-subject) unmark)))
8331     ;; Select next unread article.  If auto-select-same mode, should
8332     ;; select the first unread article.
8333     (gnus-summary-next-article t (and gnus-auto-select-same
8334                                       (gnus-summary-article-subject)))
8335     (gnus-message 7 "%d article%s marked as %s"
8336                   count (if (= count 1) " is" "s are")
8337                   (if unmark "unread" "read"))))
8338
8339 (defun gnus-summary-kill-same-subject (&optional unmark)
8340   "Mark articles which has the same subject as read.
8341 If UNMARK is positive, remove any kind of mark.
8342 If UNMARK is negative, tick articles."
8343   (interactive "P")
8344   (when unmark
8345     (setq unmark (prefix-numeric-value unmark)))
8346   (let ((count
8347          (gnus-summary-mark-same-subject
8348           (gnus-summary-article-subject) unmark)))
8349     ;; If marked as read, go to next unread subject.
8350     (when (null unmark)
8351       ;; Go to next unread subject.
8352       (gnus-summary-next-subject 1 t))
8353     (gnus-message 7 "%d articles are marked as %s"
8354                   count (if unmark "unread" "read"))))
8355
8356 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8357   "Mark articles with same SUBJECT as read, and return marked number.
8358 If optional argument UNMARK is positive, remove any kinds of marks.
8359 If optional argument UNMARK is negative, mark articles as unread instead."
8360   (let ((count 1))
8361     (save-excursion
8362       (cond
8363        ((null unmark)                   ; Mark as read.
8364         (while (and
8365                 (progn
8366                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8367                   (gnus-summary-show-thread) t)
8368                 (gnus-summary-find-subject subject))
8369           (setq count (1+ count))))
8370        ((> unmark 0)                    ; Tick.
8371         (while (and
8372                 (progn
8373                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8374                   (gnus-summary-show-thread) t)
8375                 (gnus-summary-find-subject subject))
8376           (setq count (1+ count))))
8377        (t                               ; Mark as unread.
8378         (while (and
8379                 (progn
8380                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8381                   (gnus-summary-show-thread) t)
8382                 (gnus-summary-find-subject subject))
8383           (setq count (1+ count)))))
8384       (gnus-set-mode-line 'summary)
8385       ;; Return the number of marked articles.
8386       count)))
8387
8388 (defun gnus-summary-mark-as-processable (n &optional unmark)
8389   "Set the process mark on the next N articles.
8390 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8391 the process mark instead.  The difference between N and the actual
8392 number of articles marked is returned."
8393   (interactive "p")
8394   (let ((backward (< n 0))
8395         (n (abs n)))
8396     (while (and
8397             (> n 0)
8398             (if unmark
8399                 (gnus-summary-remove-process-mark
8400                  (gnus-summary-article-number))
8401               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8402             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8403       (setq n (1- n)))
8404     (when (/= 0 n)
8405       (gnus-message 7 "No more articles"))
8406     (gnus-summary-recenter)
8407     (gnus-summary-position-point)
8408     n))
8409
8410 (defun gnus-summary-unmark-as-processable (n)
8411   "Remove the process mark from the next N articles.
8412 If N is negative, unmark backward instead.  The difference between N and
8413 the actual number of articles unmarked is returned."
8414   (interactive "p")
8415   (gnus-summary-mark-as-processable n t))
8416
8417 (defun gnus-summary-unmark-all-processable ()
8418   "Remove the process mark from all articles."
8419   (interactive)
8420   (save-excursion
8421     (while gnus-newsgroup-processable
8422       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8423   (gnus-summary-position-point))
8424
8425 (defun gnus-summary-add-mark (article type)
8426   "Mark ARTICLE with a mark of TYPE."
8427   (let ((vtype (car (assq type gnus-article-mark-lists)))
8428         var)
8429     (if (not vtype)
8430         (error "No such mark type: %s" type)
8431       (setq var (intern (format "gnus-newsgroup-%s" type)))
8432       (set var (cons article (symbol-value var)))
8433       (if (memq type '(processable cached replied saved))
8434           (gnus-summary-update-secondary-mark article)
8435         ;;; !!! This is bobus.  We should find out what primary
8436         ;;; !!! mark we want to set.
8437         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8438
8439 (defun gnus-summary-mark-as-expirable (n)
8440   "Mark N articles forward as expirable.
8441 If N is negative, mark backward instead.  The difference between N and
8442 the actual number of articles marked is returned."
8443   (interactive "p")
8444   (gnus-summary-mark-forward n gnus-expirable-mark))
8445
8446 (defun gnus-summary-mark-article-as-replied (article)
8447   "Mark ARTICLE replied and update the summary line."
8448   (push article gnus-newsgroup-replied)
8449   (let ((buffer-read-only nil))
8450     (when (gnus-summary-goto-subject article nil t)
8451       (gnus-summary-update-secondary-mark article))))
8452
8453 (defun gnus-summary-set-bookmark (article)
8454   "Set a bookmark in current article."
8455   (interactive (list (gnus-summary-article-number)))
8456   (when (or (not (get-buffer gnus-article-buffer))
8457             (not gnus-current-article)
8458             (not gnus-article-current)
8459             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8460     (error "No current article selected"))
8461   ;; Remove old bookmark, if one exists.
8462   (let ((old (assq article gnus-newsgroup-bookmarks)))
8463     (when old
8464       (setq gnus-newsgroup-bookmarks
8465             (delq old gnus-newsgroup-bookmarks))))
8466   ;; Set the new bookmark, which is on the form
8467   ;; (article-number . line-number-in-body).
8468   (push
8469    (cons article
8470          (save-excursion
8471            (set-buffer gnus-article-buffer)
8472            (count-lines
8473             (min (point)
8474                  (save-excursion
8475                    (goto-char (point-min))
8476                    (search-forward "\n\n" nil t)
8477                    (point)))
8478             (point))))
8479    gnus-newsgroup-bookmarks)
8480   (gnus-message 6 "A bookmark has been added to the current article."))
8481
8482 (defun gnus-summary-remove-bookmark (article)
8483   "Remove the bookmark from the current article."
8484   (interactive (list (gnus-summary-article-number)))
8485   ;; Remove old bookmark, if one exists.
8486   (let ((old (assq article gnus-newsgroup-bookmarks)))
8487     (if old
8488         (progn
8489           (setq gnus-newsgroup-bookmarks
8490                 (delq old gnus-newsgroup-bookmarks))
8491           (gnus-message 6 "Removed bookmark."))
8492       (gnus-message 6 "No bookmark in current article."))))
8493
8494 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8495 (defun gnus-summary-mark-as-dormant (n)
8496   "Mark N articles forward as dormant.
8497 If N is negative, mark backward instead.  The difference between N and
8498 the actual number of articles marked is returned."
8499   (interactive "p")
8500   (gnus-summary-mark-forward n gnus-dormant-mark))
8501
8502 (defun gnus-summary-set-process-mark (article)
8503   "Set the process mark on ARTICLE and update the summary line."
8504   (setq gnus-newsgroup-processable
8505         (cons article
8506               (delq article gnus-newsgroup-processable)))
8507   (when (gnus-summary-goto-subject article)
8508     (gnus-summary-show-thread)
8509     (gnus-summary-goto-subject article)
8510     (gnus-summary-update-secondary-mark article)))
8511
8512 (defun gnus-summary-remove-process-mark (article)
8513   "Remove the process mark from ARTICLE and update the summary line."
8514   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8515   (when (gnus-summary-goto-subject article)
8516     (gnus-summary-show-thread)
8517     (gnus-summary-goto-subject article)
8518     (gnus-summary-update-secondary-mark article)))
8519
8520 (defun gnus-summary-set-saved-mark (article)
8521   "Set the process mark on ARTICLE and update the summary line."
8522   (push article gnus-newsgroup-saved)
8523   (when (gnus-summary-goto-subject article)
8524     (gnus-summary-update-secondary-mark article)))
8525
8526 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8527   "Mark N articles as read forwards.
8528 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8529 The difference between N and the actual number of articles marked is
8530 returned.
8531 Iff NO-EXPIRE, auto-expiry will be inhibited."
8532   (interactive "p")
8533   (gnus-summary-show-thread)
8534   (let ((backward (< n 0))
8535         (gnus-summary-goto-unread
8536          (and gnus-summary-goto-unread
8537               (not (eq gnus-summary-goto-unread 'never))
8538               (not (memq mark (list gnus-unread-mark
8539                                     gnus-ticked-mark gnus-dormant-mark)))))
8540         (n (abs n))
8541         (mark (or mark gnus-del-mark)))
8542     (while (and (> n 0)
8543                 (gnus-summary-mark-article nil mark no-expire)
8544                 (zerop (gnus-summary-next-subject
8545                         (if backward -1 1)
8546                         (and gnus-summary-goto-unread
8547                              (not (eq gnus-summary-goto-unread 'never)))
8548                         t)))
8549       (setq n (1- n)))
8550     (when (/= 0 n)
8551       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8552     (gnus-summary-recenter)
8553     (gnus-summary-position-point)
8554     (gnus-set-mode-line 'summary)
8555     n))
8556
8557 (defun gnus-summary-mark-article-as-read (mark)
8558   "Mark the current article quickly as read with MARK."
8559   (let ((article (gnus-summary-article-number)))
8560     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8561     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8562     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8563     (push (cons article mark) gnus-newsgroup-reads)
8564     ;; Possibly remove from cache, if that is used.
8565     (when gnus-use-cache
8566       (gnus-cache-enter-remove-article article))
8567     ;; Allow the backend to change the mark.
8568     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8569     ;; Check for auto-expiry.
8570     (when (and gnus-newsgroup-auto-expire
8571                (memq mark gnus-auto-expirable-marks))
8572       (setq mark gnus-expirable-mark)
8573       ;; Let the backend know about the mark change.
8574       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8575       (push article gnus-newsgroup-expirable))
8576     ;; Set the mark in the buffer.
8577     (gnus-summary-update-mark mark 'unread)
8578     t))
8579
8580 (defun gnus-summary-mark-article-as-unread (mark)
8581   "Mark the current article quickly as unread with MARK."
8582   (let* ((article (gnus-summary-article-number))
8583          (old-mark (gnus-summary-article-mark article)))
8584     ;; Allow the backend to change the mark.
8585     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8586     (if (eq mark old-mark)
8587         t
8588       (if (<= article 0)
8589           (progn
8590             (gnus-error 1 "Can't mark negative article numbers")
8591             nil)
8592         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8593         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8594         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8595         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8596         (cond ((= mark gnus-ticked-mark)
8597                (push article gnus-newsgroup-marked))
8598               ((= mark gnus-dormant-mark)
8599                (push article gnus-newsgroup-dormant))
8600               (t
8601                (push article gnus-newsgroup-unreads)))
8602         (gnus-pull article gnus-newsgroup-reads)
8603
8604         ;; See whether the article is to be put in the cache.
8605         (and gnus-use-cache
8606              (vectorp (gnus-summary-article-header article))
8607              (save-excursion
8608                (gnus-cache-possibly-enter-article
8609                 gnus-newsgroup-name article
8610                 (gnus-summary-article-header article)
8611                 (= mark gnus-ticked-mark)
8612                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8613
8614         ;; Fix the mark.
8615         (gnus-summary-update-mark mark 'unread)
8616         t))))
8617
8618 (defun gnus-summary-mark-article (&optional article mark no-expire)
8619   "Mark ARTICLE with MARK.  MARK can be any character.
8620 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8621 `??' (dormant) and `?E' (expirable).
8622 If MARK is nil, then the default character `?r' is used.
8623 If ARTICLE is nil, then the article on the current line will be
8624 marked.
8625 Iff NO-EXPIRE, auto-expiry will be inhibited."
8626   ;; The mark might be a string.
8627   (when (stringp mark)
8628     (setq mark (aref mark 0)))
8629   ;; If no mark is given, then we check auto-expiring.
8630   (when (null mark)
8631     (setq mark gnus-del-mark))
8632   (when (and (not no-expire)
8633              gnus-newsgroup-auto-expire
8634              (memq mark gnus-auto-expirable-marks))
8635     (setq mark gnus-expirable-mark))
8636   (let ((article (or article (gnus-summary-article-number)))
8637         (old-mark (gnus-summary-article-mark article)))
8638     ;; Allow the backend to change the mark.
8639     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8640     (if (eq mark old-mark)
8641         t
8642       (unless article
8643         (error "No article on current line"))
8644       (if (not (if (or (= mark gnus-unread-mark)
8645                        (= mark gnus-ticked-mark)
8646                        (= mark gnus-dormant-mark))
8647                    (gnus-mark-article-as-unread article mark)
8648                  (gnus-mark-article-as-read article mark)))
8649           t
8650         ;; See whether the article is to be put in the cache.
8651         (and gnus-use-cache
8652              (not (= mark gnus-canceled-mark))
8653              (vectorp (gnus-summary-article-header article))
8654              (save-excursion
8655                (gnus-cache-possibly-enter-article
8656                 gnus-newsgroup-name article
8657                 (gnus-summary-article-header article)
8658                 (= mark gnus-ticked-mark)
8659                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8660
8661         (when (gnus-summary-goto-subject article nil t)
8662           (let ((buffer-read-only nil))
8663             (gnus-summary-show-thread)
8664             ;; Fix the mark.
8665             (gnus-summary-update-mark mark 'unread)
8666             t))))))
8667
8668 (defun gnus-summary-update-secondary-mark (article)
8669   "Update the secondary (read, process, cache) mark."
8670   (gnus-summary-update-mark
8671    (cond ((memq article gnus-newsgroup-processable)
8672           gnus-process-mark)
8673          ((memq article gnus-newsgroup-cached)
8674           gnus-cached-mark)
8675          ((memq article gnus-newsgroup-replied)
8676           gnus-replied-mark)
8677          ((memq article gnus-newsgroup-saved)
8678           gnus-saved-mark)
8679          (t gnus-no-mark))
8680    'replied)
8681   (when (gnus-visual-p 'summary-highlight 'highlight)
8682     (gnus-run-hooks 'gnus-summary-update-hook))
8683   t)
8684
8685 (defun gnus-summary-update-mark (mark type)
8686   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8687         (buffer-read-only nil))
8688     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8689     (when forward
8690       (when (looking-at "\r")
8691         (incf forward))
8692       (when (<= (+ forward (point)) (point-max))
8693         ;; Go to the right position on the line.
8694         (goto-char (+ forward (point)))
8695         ;; Replace the old mark with the new mark.
8696         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8697         ;; Optionally update the marks by some user rule.
8698         (when (eq type 'unread)
8699           (gnus-data-set-mark
8700            (gnus-data-find (gnus-summary-article-number)) mark)
8701           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8702
8703 (defun gnus-mark-article-as-read (article &optional mark)
8704   "Enter ARTICLE in the pertinent lists and remove it from others."
8705   ;; Make the article expirable.
8706   (let ((mark (or mark gnus-del-mark)))
8707     (if (= mark gnus-expirable-mark)
8708         (push article gnus-newsgroup-expirable)
8709       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8710     ;; Remove from unread and marked lists.
8711     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8712     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8713     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8714     (push (cons article mark) gnus-newsgroup-reads)
8715     ;; Possibly remove from cache, if that is used.
8716     (when gnus-use-cache
8717       (gnus-cache-enter-remove-article article))
8718     t))
8719
8720 (defun gnus-mark-article-as-unread (article &optional mark)
8721   "Enter ARTICLE in the pertinent lists and remove it from others."
8722   (let ((mark (or mark gnus-ticked-mark)))
8723     (if (<= article 0)
8724         (progn
8725           (gnus-error 1 "Can't mark negative article numbers")
8726           nil)
8727       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8728             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8729             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8730             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8731
8732       ;; Unsuppress duplicates?
8733       (when gnus-suppress-duplicates
8734         (gnus-dup-unsuppress-article article))
8735
8736       (cond ((= mark gnus-ticked-mark)
8737              (push article gnus-newsgroup-marked))
8738             ((= mark gnus-dormant-mark)
8739              (push article gnus-newsgroup-dormant))
8740             (t
8741              (push article gnus-newsgroup-unreads)))
8742       (gnus-pull article gnus-newsgroup-reads)
8743       t)))
8744
8745 (defalias 'gnus-summary-mark-as-unread-forward
8746   'gnus-summary-tick-article-forward)
8747 (make-obsolete 'gnus-summary-mark-as-unread-forward
8748                'gnus-summary-tick-article-forward)
8749 (defun gnus-summary-tick-article-forward (n)
8750   "Tick N articles forwards.
8751 If N is negative, tick backwards instead.
8752 The difference between N and the number of articles ticked is returned."
8753   (interactive "p")
8754   (gnus-summary-mark-forward n gnus-ticked-mark))
8755
8756 (defalias 'gnus-summary-mark-as-unread-backward
8757   'gnus-summary-tick-article-backward)
8758 (make-obsolete 'gnus-summary-mark-as-unread-backward
8759                'gnus-summary-tick-article-backward)
8760 (defun gnus-summary-tick-article-backward (n)
8761   "Tick N articles backwards.
8762 The difference between N and the number of articles ticked is returned."
8763   (interactive "p")
8764   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8765
8766 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8767 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8768 (defun gnus-summary-tick-article (&optional article clear-mark)
8769   "Mark current article as unread.
8770 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8771 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8772   (interactive)
8773   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8774                                        gnus-ticked-mark)))
8775
8776 (defun gnus-summary-mark-as-read-forward (n)
8777   "Mark N articles as read forwards.
8778 If N is negative, mark backwards instead.
8779 The difference between N and the actual number of articles marked is
8780 returned."
8781   (interactive "p")
8782   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8783
8784 (defun gnus-summary-mark-as-read-backward (n)
8785   "Mark the N articles as read backwards.
8786 The difference between N and the actual number of articles marked is
8787 returned."
8788   (interactive "p")
8789   (gnus-summary-mark-forward
8790    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8791
8792 (defun gnus-summary-mark-as-read (&optional article mark)
8793   "Mark current article as read.
8794 ARTICLE specifies the article to be marked as read.
8795 MARK specifies a string to be inserted at the beginning of the line."
8796   (gnus-summary-mark-article article mark))
8797
8798 (defun gnus-summary-clear-mark-forward (n)
8799   "Clear marks from N articles forward.
8800 If N is negative, clear backward instead.
8801 The difference between N and the number of marks cleared is returned."
8802   (interactive "p")
8803   (gnus-summary-mark-forward n gnus-unread-mark))
8804
8805 (defun gnus-summary-clear-mark-backward (n)
8806   "Clear marks from N articles backward.
8807 The difference between N and the number of marks cleared is returned."
8808   (interactive "p")
8809   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8810
8811 (defun gnus-summary-mark-unread-as-read ()
8812   "Intended to be used by `gnus-summary-mark-article-hook'."
8813   (when (memq gnus-current-article gnus-newsgroup-unreads)
8814     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8815
8816 (defun gnus-summary-mark-read-and-unread-as-read ()
8817   "Intended to be used by `gnus-summary-mark-article-hook'."
8818   (let ((mark (gnus-summary-article-mark)))
8819     (when (or (gnus-unread-mark-p mark)
8820               (gnus-read-mark-p mark))
8821       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8822
8823 (defun gnus-summary-mark-unread-as-ticked ()
8824    "Intended to be used by `gnus-summary-mark-article-hook'."
8825   (when (memq gnus-current-article gnus-newsgroup-unreads)
8826     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8827
8828 (defun gnus-summary-mark-region-as-read (point mark all)
8829   "Mark all unread articles between point and mark as read.
8830 If given a prefix, mark all articles between point and mark as read,
8831 even ticked and dormant ones."
8832   (interactive "r\nP")
8833   (save-excursion
8834     (let (article)
8835       (goto-char point)
8836       (beginning-of-line)
8837       (while (and
8838               (< (point) mark)
8839               (progn
8840                 (when (or all
8841                           (memq (setq article (gnus-summary-article-number))
8842                                 gnus-newsgroup-unreads))
8843                   (gnus-summary-mark-article article gnus-del-mark))
8844                 t)
8845               (gnus-summary-find-next))))))
8846
8847 (defun gnus-summary-mark-below (score mark)
8848   "Mark articles with score less than SCORE with MARK."
8849   (interactive "P\ncMark: ")
8850   (setq score (if score
8851                   (prefix-numeric-value score)
8852                 (or gnus-summary-default-score 0)))
8853   (save-excursion
8854     (set-buffer gnus-summary-buffer)
8855     (goto-char (point-min))
8856     (while
8857         (progn
8858           (and (< (gnus-summary-article-score) score)
8859                (gnus-summary-mark-article nil mark))
8860           (gnus-summary-find-next)))))
8861
8862 (defun gnus-summary-kill-below (&optional score)
8863   "Mark articles with score below SCORE as read."
8864   (interactive "P")
8865   (gnus-summary-mark-below score gnus-killed-mark))
8866
8867 (defun gnus-summary-clear-above (&optional score)
8868   "Clear all marks from articles with score above SCORE."
8869   (interactive "P")
8870   (gnus-summary-mark-above score gnus-unread-mark))
8871
8872 (defun gnus-summary-tick-above (&optional score)
8873   "Tick all articles with score above SCORE."
8874   (interactive "P")
8875   (gnus-summary-mark-above score gnus-ticked-mark))
8876
8877 (defun gnus-summary-mark-above (score mark)
8878   "Mark articles with score over SCORE with MARK."
8879   (interactive "P\ncMark: ")
8880   (setq score (if score
8881                   (prefix-numeric-value score)
8882                 (or gnus-summary-default-score 0)))
8883   (save-excursion
8884     (set-buffer gnus-summary-buffer)
8885     (goto-char (point-min))
8886     (while (and (progn
8887                   (when (> (gnus-summary-article-score) score)
8888                     (gnus-summary-mark-article nil mark))
8889                   t)
8890                 (gnus-summary-find-next)))))
8891
8892 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8893 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8894 (defun gnus-summary-limit-include-expunged (&optional no-error)
8895   "Display all the hidden articles that were expunged for low scores."
8896   (interactive)
8897   (let ((buffer-read-only nil))
8898     (let ((scored gnus-newsgroup-scored)
8899           headers h)
8900       (while scored
8901         (unless (gnus-number-to-header (caar scored))
8902           (and (setq h (gnus-summary-article-header (caar scored)))
8903                (< (cdar scored) gnus-summary-expunge-below)
8904                (push h headers)))
8905         (setq scored (cdr scored)))
8906       (if (not headers)
8907           (when (not no-error)
8908             (error "No expunged articles hidden"))
8909         (goto-char (point-min))
8910         (gnus-summary-prepare-unthreaded (nreverse headers))
8911         (goto-char (point-min))
8912         (gnus-summary-position-point)
8913         t))))
8914
8915 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8916   "Mark all unread articles in this newsgroup as read.
8917 If prefix argument ALL is non-nil, ticked and dormant articles will
8918 also be marked as read.
8919 If QUIETLY is non-nil, no questions will be asked.
8920 If TO-HERE is non-nil, it should be a point in the buffer.  All
8921 articles before this point will be marked as read.
8922 Note that this function will only catch up the unread article
8923 in the current summary buffer limitation.
8924 The number of articles marked as read is returned."
8925   (interactive "P")
8926   (prog1
8927       (save-excursion
8928         (when (or quietly
8929                   (not gnus-interactive-catchup) ;Without confirmation?
8930                   gnus-expert-user
8931                   (gnus-y-or-n-p
8932                    (if all
8933                        "Mark absolutely all articles as read? "
8934                      "Mark all unread articles as read? ")))
8935           (if (and not-mark
8936                    (not gnus-newsgroup-adaptive)
8937                    (not gnus-newsgroup-auto-expire)
8938                    (not gnus-suppress-duplicates)
8939                    (or (not gnus-use-cache)
8940                        (eq gnus-use-cache 'passive)))
8941               (progn
8942                 (when all
8943                   (setq gnus-newsgroup-marked nil
8944                         gnus-newsgroup-dormant nil))
8945                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8946             ;; We actually mark all articles as canceled, which we
8947             ;; have to do when using auto-expiry or adaptive scoring.
8948             (gnus-summary-show-all-threads)
8949             (when (gnus-summary-first-subject (not all) t)
8950               (while (and
8951                       (if to-here (< (point) to-here) t)
8952                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8953                       (gnus-summary-find-next (not all) nil nil t))))
8954             (gnus-set-mode-line 'summary))
8955           t))
8956     (gnus-summary-position-point)))
8957
8958 (defun gnus-summary-catchup-to-here (&optional all)
8959   "Mark all unticked articles before the current one as read.
8960 If ALL is non-nil, also mark ticked and dormant articles as read."
8961   (interactive "P")
8962   (save-excursion
8963     (gnus-save-hidden-threads
8964       (let ((beg (point)))
8965         ;; We check that there are unread articles.
8966         (when (or all (gnus-summary-find-prev))
8967           (gnus-summary-catchup all t beg)))))
8968   (gnus-summary-position-point))
8969
8970 (defun gnus-summary-catchup-all (&optional quietly)
8971   "Mark all articles in this newsgroup as read."
8972   (interactive "P")
8973   (gnus-summary-catchup t quietly))
8974
8975 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8976   "Mark all unread articles in this group as read, then exit.
8977 If prefix argument ALL is non-nil, all articles are marked as read."
8978   (interactive "P")
8979   (when (gnus-summary-catchup all quietly nil 'fast)
8980     ;; Select next newsgroup or exit.
8981     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8982              (eq gnus-auto-select-next 'quietly))
8983         (gnus-summary-next-group nil)
8984       (gnus-summary-exit))))
8985
8986 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8987   "Mark all articles in this newsgroup as read, and then exit."
8988   (interactive "P")
8989   (gnus-summary-catchup-and-exit t quietly))
8990
8991 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8992   "Mark all articles in this group as read and select the next group.
8993 If given a prefix, mark all articles, unread as well as ticked, as
8994 read."
8995   (interactive "P")
8996   (save-excursion
8997     (gnus-summary-catchup all))
8998   (gnus-summary-next-group))
8999
9000 ;;;
9001 ;;; with article
9002 ;;;
9003
9004 (defmacro gnus-with-article (article &rest forms)
9005   "Select ARTICLE and perform FORMS in the original article buffer.
9006 Then replace the article with the result."
9007   `(progn
9008      ;; We don't want the article to be marked as read.
9009      (let (gnus-mark-article-hook)
9010        (gnus-summary-select-article t t nil ,article))
9011      (set-buffer gnus-original-article-buffer)
9012      ,@forms
9013      (if (not (gnus-check-backend-function
9014                'request-replace-article (car gnus-article-current)))
9015          (gnus-message 5 "Read-only group; not replacing")
9016        (unless (gnus-request-replace-article
9017                 ,article (car gnus-article-current)
9018                 (current-buffer) t)
9019          (error "Couldn't replace article")))
9020      ;; The cache and backlog have to be flushed somewhat.
9021      (when gnus-keep-backlog
9022        (gnus-backlog-remove-article
9023         (car gnus-article-current) (cdr gnus-article-current)))
9024      (when gnus-use-cache
9025        (gnus-cache-update-article
9026         (car gnus-article-current) (cdr gnus-article-current)))))
9027
9028 (put 'gnus-with-article 'lisp-indent-function 1)
9029 (put 'gnus-with-article 'edebug-form-spec '(form body))
9030
9031 ;; Thread-based commands.
9032
9033 (defun gnus-summary-articles-in-thread (&optional article)
9034   "Return a list of all articles in the current thread.
9035 If ARTICLE is non-nil, return all articles in the thread that starts
9036 with that article."
9037   (let* ((article (or article (gnus-summary-article-number)))
9038          (data (gnus-data-find-list article))
9039          (top-level (gnus-data-level (car data)))
9040          (top-subject
9041           (cond ((null gnus-thread-operation-ignore-subject)
9042                  (gnus-simplify-subject-re
9043                   (mail-header-subject (gnus-data-header (car data)))))
9044                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9045                  (gnus-simplify-subject-fuzzy
9046                   (mail-header-subject (gnus-data-header (car data)))))
9047                 (t nil)))
9048          (end-point (save-excursion
9049                       (if (gnus-summary-go-to-next-thread)
9050                           (point) (point-max))))
9051          articles)
9052     (while (and data
9053                 (< (gnus-data-pos (car data)) end-point))
9054       (when (or (not top-subject)
9055                 (string= top-subject
9056                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9057                              (gnus-simplify-subject-fuzzy
9058                               (mail-header-subject
9059                                (gnus-data-header (car data))))
9060                            (gnus-simplify-subject-re
9061                             (mail-header-subject
9062                              (gnus-data-header (car data)))))))
9063         (push (gnus-data-number (car data)) articles))
9064       (unless (and (setq data (cdr data))
9065                    (> (gnus-data-level (car data)) top-level))
9066         (setq data nil)))
9067     ;; Return the list of articles.
9068     (nreverse articles)))
9069
9070 (defun gnus-summary-rethread-current ()
9071   "Rethread the thread the current article is part of."
9072   (interactive)
9073   (let* ((gnus-show-threads t)
9074          (article (gnus-summary-article-number))
9075          (id (mail-header-id (gnus-summary-article-header)))
9076          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9077     (unless id
9078       (error "No article on the current line"))
9079     (gnus-rebuild-thread id)
9080     (gnus-summary-goto-subject article)))
9081
9082 (defun gnus-summary-reparent-thread ()
9083   "Make the current article child of the marked (or previous) article.
9084
9085 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9086 is non-nil or the Subject: of both articles are the same."
9087   (interactive)
9088   (unless (not (gnus-group-read-only-p))
9089     (error "The current newsgroup does not support article editing"))
9090   (unless (<= (length gnus-newsgroup-processable) 1)
9091     (error "No more than one article may be marked"))
9092   (save-window-excursion
9093     (let ((gnus-article-buffer " *reparent*")
9094           (current-article (gnus-summary-article-number))
9095           ;; First grab the marked article, otherwise one line up.
9096           (parent-article (if (not (null gnus-newsgroup-processable))
9097                               (car gnus-newsgroup-processable)
9098                             (save-excursion
9099                               (if (eq (forward-line -1) 0)
9100                                   (gnus-summary-article-number)
9101                                 (error "Beginning of summary buffer"))))))
9102       (unless (not (eq current-article parent-article))
9103         (error "An article may not be self-referential"))
9104       (let ((message-id (mail-header-id
9105                          (gnus-summary-article-header parent-article))))
9106         (unless (and message-id (not (equal message-id "")))
9107           (error "No message-id in desired parent"))
9108         (gnus-with-article current-article
9109           (save-restriction
9110             (goto-char (point-min))
9111             (message-narrow-to-head)
9112             (if (re-search-forward "^References: " nil t)
9113                 (progn
9114                   (re-search-forward "^[^ \t]" nil t)
9115                   (forward-line -1)
9116                   (end-of-line)
9117                   (insert " " message-id))
9118               (insert "References: " message-id "\n"))))
9119         (set-buffer gnus-summary-buffer)
9120         (gnus-summary-unmark-all-processable)
9121         (gnus-summary-update-article current-article)
9122         (gnus-summary-rethread-current)
9123         (gnus-message 3 "Article %d is now the child of article %d"
9124                       current-article parent-article)))))
9125
9126 (defun gnus-summary-toggle-threads (&optional arg)
9127   "Toggle showing conversation threads.
9128 If ARG is positive number, turn showing conversation threads on."
9129   (interactive "P")
9130   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9131     (setq gnus-show-threads
9132           (if (null arg) (not gnus-show-threads)
9133             (> (prefix-numeric-value arg) 0)))
9134     (gnus-summary-prepare)
9135     (gnus-summary-goto-subject current)
9136     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9137     (gnus-summary-position-point)))
9138
9139 (defun gnus-summary-show-all-threads ()
9140   "Show all threads."
9141   (interactive)
9142   (save-excursion
9143     (let ((buffer-read-only nil))
9144       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9145   (gnus-summary-position-point))
9146
9147 (defun gnus-summary-show-thread ()
9148   "Show thread subtrees.
9149 Returns nil if no thread was there to be shown."
9150   (interactive)
9151   (let ((buffer-read-only nil)
9152         (orig (point))
9153         ;; first goto end then to beg, to have point at beg after let
9154         (end (progn (end-of-line) (point)))
9155         (beg (progn (beginning-of-line) (point))))
9156     (prog1
9157         ;; Any hidden lines here?
9158         (search-forward "\r" end t)
9159       (subst-char-in-region beg end ?\^M ?\n t)
9160       (goto-char orig)
9161       (gnus-summary-position-point))))
9162
9163 (defun gnus-summary-hide-all-threads ()
9164   "Hide all thread subtrees."
9165   (interactive)
9166   (save-excursion
9167     (goto-char (point-min))
9168     (gnus-summary-hide-thread)
9169     (while (zerop (gnus-summary-next-thread 1 t))
9170       (gnus-summary-hide-thread)))
9171   (gnus-summary-position-point))
9172
9173 (defun gnus-summary-hide-thread ()
9174   "Hide thread subtrees.
9175 Returns nil if no threads were there to be hidden."
9176   (interactive)
9177   (let ((buffer-read-only nil)
9178         (start (point))
9179         (article (gnus-summary-article-number)))
9180     (goto-char start)
9181     ;; Go forward until either the buffer ends or the subthread
9182     ;; ends.
9183     (when (and (not (eobp))
9184                (or (zerop (gnus-summary-next-thread 1 t))
9185                    (goto-char (point-max))))
9186       (prog1
9187           (if (and (> (point) start)
9188                    (search-backward "\n" start t))
9189               (progn
9190                 (subst-char-in-region start (point) ?\n ?\^M)
9191                 (gnus-summary-goto-subject article))
9192             (goto-char start)
9193             nil)))))
9194
9195 (defun gnus-summary-go-to-next-thread (&optional previous)
9196   "Go to the same level (or less) next thread.
9197 If PREVIOUS is non-nil, go to previous thread instead.
9198 Return the article number moved to, or nil if moving was impossible."
9199   (let ((level (gnus-summary-thread-level))
9200         (way (if previous -1 1))
9201         (beg (point)))
9202     (forward-line way)
9203     (while (and (not (eobp))
9204                 (< level (gnus-summary-thread-level)))
9205       (forward-line way))
9206     (if (eobp)
9207         (progn
9208           (goto-char beg)
9209           nil)
9210       (setq beg (point))
9211       (prog1
9212           (gnus-summary-article-number)
9213         (goto-char beg)))))
9214
9215 (defun gnus-summary-next-thread (n &optional silent)
9216   "Go to the same level next N'th thread.
9217 If N is negative, search backward instead.
9218 Returns the difference between N and the number of skips actually
9219 done.
9220
9221 If SILENT, don't output messages."
9222   (interactive "p")
9223   (let ((backward (< n 0))
9224         (n (abs n)))
9225     (while (and (> n 0)
9226                 (gnus-summary-go-to-next-thread backward))
9227       (decf n))
9228     (unless silent
9229       (gnus-summary-position-point))
9230     (when (and (not silent) (/= 0 n))
9231       (gnus-message 7 "No more threads"))
9232     n))
9233
9234 (defun gnus-summary-prev-thread (n)
9235   "Go to the same level previous N'th thread.
9236 Returns the difference between N and the number of skips actually
9237 done."
9238   (interactive "p")
9239   (gnus-summary-next-thread (- n)))
9240
9241 (defun gnus-summary-go-down-thread ()
9242   "Go down one level in the current thread."
9243   (let ((children (gnus-summary-article-children)))
9244     (when children
9245       (gnus-summary-goto-subject (car children)))))
9246
9247 (defun gnus-summary-go-up-thread ()
9248   "Go up one level in the current thread."
9249   (let ((parent (gnus-summary-article-parent)))
9250     (when parent
9251       (gnus-summary-goto-subject parent))))
9252
9253 (defun gnus-summary-down-thread (n)
9254   "Go down thread N steps.
9255 If N is negative, go up instead.
9256 Returns the difference between N and how many steps down that were
9257 taken."
9258   (interactive "p")
9259   (let ((up (< n 0))
9260         (n (abs n)))
9261     (while (and (> n 0)
9262                 (if up (gnus-summary-go-up-thread)
9263                   (gnus-summary-go-down-thread)))
9264       (setq n (1- n)))
9265     (gnus-summary-position-point)
9266     (when (/= 0 n)
9267       (gnus-message 7 "Can't go further"))
9268     n))
9269
9270 (defun gnus-summary-up-thread (n)
9271   "Go up thread N steps.
9272 If N is negative, go up instead.
9273 Returns the difference between N and how many steps down that were
9274 taken."
9275   (interactive "p")
9276   (gnus-summary-down-thread (- n)))
9277
9278 (defun gnus-summary-top-thread ()
9279   "Go to the top of the thread."
9280   (interactive)
9281   (while (gnus-summary-go-up-thread))
9282   (gnus-summary-article-number))
9283
9284 (defun gnus-summary-kill-thread (&optional unmark)
9285   "Mark articles under current thread as read.
9286 If the prefix argument is positive, remove any kinds of marks.
9287 If the prefix argument is negative, tick articles instead."
9288   (interactive "P")
9289   (when unmark
9290     (setq unmark (prefix-numeric-value unmark)))
9291   (let ((articles (gnus-summary-articles-in-thread)))
9292     (save-excursion
9293       ;; Expand the thread.
9294       (gnus-summary-show-thread)
9295       ;; Mark all the articles.
9296       (while articles
9297         (gnus-summary-goto-subject (car articles))
9298         (cond ((null unmark)
9299                (gnus-summary-mark-article-as-read gnus-killed-mark))
9300               ((> unmark 0)
9301                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9302               (t
9303                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9304         (setq articles (cdr articles))))
9305     ;; Hide killed subtrees.
9306     (and (null unmark)
9307          gnus-thread-hide-killed
9308          (gnus-summary-hide-thread))
9309     ;; If marked as read, go to next unread subject.
9310     (when (null unmark)
9311       ;; Go to next unread subject.
9312       (gnus-summary-next-subject 1 t)))
9313   (gnus-set-mode-line 'summary))
9314
9315 ;; Summary sorting commands
9316
9317 (defun gnus-summary-sort-by-number (&optional reverse)
9318   "Sort the summary buffer by article number.
9319 Argument REVERSE means reverse order."
9320   (interactive "P")
9321   (gnus-summary-sort 'number reverse))
9322
9323 (defun gnus-summary-sort-by-author (&optional reverse)
9324   "Sort the summary buffer by author name alphabetically.
9325 If `case-fold-search' is non-nil, case of letters is ignored.
9326 Argument REVERSE means reverse order."
9327   (interactive "P")
9328   (gnus-summary-sort 'author reverse))
9329
9330 (defun gnus-summary-sort-by-subject (&optional reverse)
9331   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
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 'subject reverse))
9336
9337 (defun gnus-summary-sort-by-date (&optional reverse)
9338   "Sort the summary buffer by date.
9339 Argument REVERSE means reverse order."
9340   (interactive "P")
9341   (gnus-summary-sort 'date reverse))
9342
9343 (defun gnus-summary-sort-by-score (&optional reverse)
9344   "Sort the summary buffer by score.
9345 Argument REVERSE means reverse order."
9346   (interactive "P")
9347   (gnus-summary-sort 'score reverse))
9348
9349 (defun gnus-summary-sort-by-lines (&optional reverse)
9350   "Sort the summary buffer by the number of lines.
9351 Argument REVERSE means reverse order."
9352   (interactive "P")
9353   (gnus-summary-sort 'lines reverse))
9354
9355 (defun gnus-summary-sort-by-chars (&optional reverse)
9356   "Sort the summary buffer by article length.
9357 Argument REVERSE means reverse order."
9358   (interactive "P")
9359   (gnus-summary-sort 'chars reverse))
9360
9361 (defun gnus-summary-sort (predicate reverse)
9362   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9363   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9364          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9365          (gnus-thread-sort-functions
9366           (if (not reverse)
9367               thread
9368             `(lambda (t1 t2)
9369                (,thread t2 t1))))
9370          (gnus-sort-gathered-threads-function
9371           gnus-thread-sort-functions)
9372          (gnus-article-sort-functions
9373           (if (not reverse)
9374               article
9375             `(lambda (t1 t2)
9376                (,article t2 t1))))
9377          (buffer-read-only)
9378          (gnus-summary-prepare-hook nil))
9379     ;; We do the sorting by regenerating the threads.
9380     (gnus-summary-prepare)
9381     ;; Hide subthreads if needed.
9382     (when (and gnus-show-threads gnus-thread-hide-subtree)
9383       (gnus-summary-hide-all-threads))))
9384
9385 ;; Summary saving commands.
9386
9387 (defun gnus-summary-save-article (&optional n not-saved)
9388   "Save the current article using the default saver function.
9389 If N is a positive number, save the N next articles.
9390 If N is a negative number, save the N previous articles.
9391 If N is nil and any articles have been marked with the process mark,
9392 save those articles instead.
9393 The variable `gnus-default-article-saver' specifies the saver function."
9394   (interactive "P")
9395   (let* ((articles (gnus-summary-work-articles n))
9396          (save-buffer (save-excursion
9397                         (nnheader-set-temp-buffer " *Gnus Save*")))
9398          (num (length articles))
9399          header file)
9400     (dolist (article articles)
9401       (setq header (gnus-summary-article-header article))
9402       (if (not (vectorp header))
9403           ;; This is a pseudo-article.
9404           (if (assq 'name header)
9405               (gnus-copy-file (cdr (assq 'name header)))
9406             (gnus-message 1 "Article %d is unsaveable" article))
9407         ;; This is a real article.
9408         (save-window-excursion
9409           (gnus-summary-select-article t nil nil article))
9410         (save-excursion
9411           (set-buffer save-buffer)
9412           (erase-buffer)
9413           (insert-buffer-substring gnus-original-article-buffer))
9414         (setq file (gnus-article-save save-buffer file num))
9415         (gnus-summary-remove-process-mark article)
9416         (unless not-saved
9417           (gnus-summary-set-saved-mark article))))
9418     (gnus-kill-buffer save-buffer)
9419     (gnus-summary-position-point)
9420     (gnus-set-mode-line 'summary)
9421     n))
9422
9423 (defun gnus-summary-pipe-output (&optional arg)
9424   "Pipe the current article to a subprocess.
9425 If N is a positive number, pipe the N next articles.
9426 If N is a negative number, pipe the N previous articles.
9427 If N is nil and any articles have been marked with the process mark,
9428 pipe those articles instead."
9429   (interactive "P")
9430   (require 'gnus-art)
9431   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9432     (gnus-summary-save-article arg t))
9433   (gnus-configure-windows 'pipe))
9434
9435 (defun gnus-summary-save-article-mail (&optional arg)
9436   "Append the current article to an mail file.
9437 If N is a positive number, save the N next articles.
9438 If N is a negative number, save the N previous articles.
9439 If N is nil and any articles have been marked with the process mark,
9440 save those articles instead."
9441   (interactive "P")
9442   (require 'gnus-art)
9443   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9444     (gnus-summary-save-article arg)))
9445
9446 (defun gnus-summary-save-article-rmail (&optional arg)
9447   "Append the current article to an rmail file.
9448 If N is a positive number, save the N next articles.
9449 If N is a negative number, save the N previous articles.
9450 If N is nil and any articles have been marked with the process mark,
9451 save those articles instead."
9452   (interactive "P")
9453   (require 'gnus-art)
9454   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9455     (gnus-summary-save-article arg)))
9456
9457 (defun gnus-summary-save-article-file (&optional arg)
9458   "Append the current article to a file.
9459 If N is a positive number, save the N next articles.
9460 If N is a negative number, save the N previous articles.
9461 If N is nil and any articles have been marked with the process mark,
9462 save those articles instead."
9463   (interactive "P")
9464   (require 'gnus-art)
9465   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9466     (gnus-summary-save-article arg)))
9467
9468 (defun gnus-summary-write-article-file (&optional arg)
9469   "Write the current article to a file, deleting the previous file.
9470 If N is a positive number, save the N next articles.
9471 If N is a negative number, save the N previous articles.
9472 If N is nil and any articles have been marked with the process mark,
9473 save those articles instead."
9474   (interactive "P")
9475   (require 'gnus-art)
9476   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9477     (gnus-summary-save-article arg)))
9478
9479 (defun gnus-summary-save-article-body-file (&optional arg)
9480   "Append the current article body to a file.
9481 If N is a positive number, save the N next articles.
9482 If N is a negative number, save the N previous articles.
9483 If N is nil and any articles have been marked with the process mark,
9484 save those articles instead."
9485   (interactive "P")
9486   (require 'gnus-art)
9487   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9488     (gnus-summary-save-article arg)))
9489
9490 (defun gnus-summary-pipe-message (program)
9491   "Pipe the current article through PROGRAM."
9492   (interactive "sProgram: ")
9493   (gnus-summary-select-article)
9494   (let ((mail-header-separator ""))
9495     (gnus-eval-in-buffer-window gnus-article-buffer
9496       (save-restriction
9497         (widen)
9498         (let ((start (window-start))
9499               buffer-read-only)
9500           (message-pipe-buffer-body program)
9501           (set-window-start (get-buffer-window (current-buffer)) start))))))
9502
9503 (defun gnus-get-split-value (methods)
9504   "Return a value based on the split METHODS."
9505   (let (split-name method result match)
9506     (when methods
9507       (save-excursion
9508         (set-buffer gnus-original-article-buffer)
9509         (save-restriction
9510           (nnheader-narrow-to-headers)
9511           (while (and methods (not split-name))
9512             (goto-char (point-min))
9513             (setq method (pop methods))
9514             (setq match (car method))
9515             (when (cond
9516                    ((stringp match)
9517                     ;; Regular expression.
9518                     (ignore-errors
9519                       (re-search-forward match nil t)))
9520                    ((gnus-functionp match)
9521                     ;; Function.
9522                     (save-restriction
9523                       (widen)
9524                       (setq result (funcall match gnus-newsgroup-name))))
9525                    ((consp match)
9526                     ;; Form.
9527                     (save-restriction
9528                       (widen)
9529                       (setq result (eval match)))))
9530               (setq split-name (cdr method))
9531               (cond ((stringp result)
9532                      (push (expand-file-name
9533                             result gnus-article-save-directory)
9534                            split-name))
9535                     ((consp result)
9536                      (setq split-name (append result split-name)))))))))
9537     (nreverse split-name)))
9538
9539 (defun gnus-valid-move-group-p (group)
9540   (and (boundp group)
9541        (symbol-name group)
9542        (symbol-value group)
9543        (gnus-get-function (gnus-find-method-for-group
9544                            (symbol-name group)) 'request-accept-article t)))
9545
9546 (defun gnus-read-move-group-name (prompt default articles prefix)
9547   "Read a group name."
9548   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9549          (minibuffer-confirm-incomplete nil) ; XEmacs
9550          (prom
9551           (format "%s %s to:"
9552                   prompt
9553                   (if (> (length articles) 1)
9554                       (format "these %d articles" (length articles))
9555                     "this article")))
9556          (to-newsgroup
9557           (cond
9558            ((null split-name)
9559             (gnus-completing-read default prom
9560                                   gnus-active-hashtb
9561                                   'gnus-valid-move-group-p
9562                                   nil prefix
9563                                   'gnus-group-history))
9564            ((= 1 (length split-name))
9565             (gnus-completing-read (car split-name) prom
9566                                   gnus-active-hashtb
9567                                   'gnus-valid-move-group-p
9568                                   nil nil
9569                                   'gnus-group-history))
9570            (t
9571             (gnus-completing-read nil prom
9572                                   (mapcar (lambda (el) (list el))
9573                                           (nreverse split-name))
9574                                   nil nil nil
9575                                   'gnus-group-history))))
9576          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9577     (when to-newsgroup
9578       (if (or (string= to-newsgroup "")
9579               (string= to-newsgroup prefix))
9580           (setq to-newsgroup default))
9581       (unless to-newsgroup
9582         (error "No group name entered"))
9583       (or (gnus-active to-newsgroup)
9584           (gnus-activate-group to-newsgroup nil nil to-method)
9585           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9586                                      to-newsgroup))
9587               (or (and (gnus-request-create-group to-newsgroup to-method)
9588                        (gnus-activate-group
9589                         to-newsgroup nil nil to-method)
9590                        (gnus-subscribe-group to-newsgroup))
9591                   (error "Couldn't create group %s" to-newsgroup)))
9592           (error "No such group: %s" to-newsgroup)))
9593     to-newsgroup))
9594
9595 (defun gnus-summary-save-parts (type dir n &optional reverse)
9596   "Save parts matching TYPE to DIR.
9597 If REVERSE, save parts that do not match TYPE."
9598   (interactive
9599    (list (read-string "Save parts of type: "
9600                       (or (car gnus-summary-save-parts-type-history)
9601                           gnus-summary-save-parts-default-mime)
9602                       'gnus-summary-save-parts-type-history)
9603          (setq gnus-summary-save-parts-last-directory
9604                (read-file-name "Save to directory: "
9605                                gnus-summary-save-parts-last-directory
9606                                nil t))
9607          current-prefix-arg))
9608   (gnus-summary-iterate n
9609     (let ((gnus-display-mime-function nil)
9610           (gnus-inhibit-treatment t))
9611       (gnus-summary-select-article))
9612     (save-excursion
9613       (set-buffer gnus-article-buffer)
9614       (let ((handles (or gnus-article-mime-handles
9615                          (mm-dissect-buffer) (mm-uu-dissect))))
9616         (when handles
9617           (gnus-summary-save-parts-1 type dir handles reverse)
9618           (unless gnus-article-mime-handles ;; Don't destroy this case.
9619             (mm-destroy-parts handles)))))))
9620
9621 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9622   (if (stringp (car handle))
9623       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9624               (cdr handle))
9625     (when (if reverse
9626               (not (string-match type (mm-handle-media-type handle)))
9627             (string-match type (mm-handle-media-type handle)))
9628       (let ((file (expand-file-name
9629                    (file-name-nondirectory
9630                     (or
9631                      (mail-content-type-get
9632                       (mm-handle-disposition handle) 'filename)
9633                      (concat gnus-newsgroup-name
9634                              "." (number-to-string
9635                                   (cdr gnus-article-current)))))
9636                    dir)))
9637         (unless (file-exists-p file)
9638           (mm-save-part-to-file handle file))))))
9639
9640 ;; Summary extract commands
9641
9642 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9643   (let ((buffer-read-only nil)
9644         (article (gnus-summary-article-number))
9645         after-article b e)
9646     (unless (gnus-summary-goto-subject article)
9647       (error "No such article: %d" article))
9648     (gnus-summary-position-point)
9649     ;; If all commands are to be bunched up on one line, we collect
9650     ;; them here.
9651     (unless gnus-view-pseudos-separately
9652       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9653             files action)
9654         (while ps
9655           (setq action (cdr (assq 'action (car ps))))
9656           (setq files (list (cdr (assq 'name (car ps)))))
9657           (while (and ps (cdr ps)
9658                       (string= (or action "1")
9659                                (or (cdr (assq 'action (cadr ps))) "2")))
9660             (push (cdr (assq 'name (cadr ps))) files)
9661             (setcdr ps (cddr ps)))
9662           (when files
9663             (when (not (string-match "%s" action))
9664               (push " " files))
9665             (push " " files)
9666             (when (assq 'execute (car ps))
9667               (setcdr (assq 'execute (car ps))
9668                       (funcall (if (string-match "%s" action)
9669                                    'format 'concat)
9670                                action
9671                                (mapconcat
9672                                 (lambda (f)
9673                                   (if (equal f " ")
9674                                       f
9675                                     (gnus-quote-arg-for-sh-or-csh f)))
9676                                 files " ")))))
9677           (setq ps (cdr ps)))))
9678     (if (and gnus-view-pseudos (not not-view))
9679         (while pslist
9680           (when (assq 'execute (car pslist))
9681             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9682                                   (eq gnus-view-pseudos 'not-confirm)))
9683           (setq pslist (cdr pslist)))
9684       (save-excursion
9685         (while pslist
9686           (setq after-article (or (cdr (assq 'article (car pslist)))
9687                                   (gnus-summary-article-number)))
9688           (gnus-summary-goto-subject after-article)
9689           (forward-line 1)
9690           (setq b (point))
9691           (insert "    " (file-name-nondirectory
9692                           (cdr (assq 'name (car pslist))))
9693                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9694           (setq e (point))
9695           (forward-line -1)             ; back to `b'
9696           (gnus-add-text-properties
9697            b (1- e) (list 'gnus-number gnus-reffed-article-number
9698                           gnus-mouse-face-prop gnus-mouse-face))
9699           (gnus-data-enter
9700            after-article gnus-reffed-article-number
9701            gnus-unread-mark b (car pslist) 0 (- e b))
9702           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9703           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9704           (setq pslist (cdr pslist)))))))
9705
9706 (defun gnus-pseudos< (p1 p2)
9707   (let ((c1 (cdr (assq 'action p1)))
9708         (c2 (cdr (assq 'action p2))))
9709     (and c1 c2 (string< c1 c2))))
9710
9711 (defun gnus-request-pseudo-article (props)
9712   (cond ((assq 'execute props)
9713          (gnus-execute-command (cdr (assq 'execute props)))))
9714   (let ((gnus-current-article (gnus-summary-article-number)))
9715     (gnus-run-hooks 'gnus-mark-article-hook)))
9716
9717 (defun gnus-execute-command (command &optional automatic)
9718   (save-excursion
9719     (gnus-article-setup-buffer)
9720     (set-buffer gnus-article-buffer)
9721     (setq buffer-read-only nil)
9722     (let ((command (if automatic command
9723                      (read-string "Command: " (cons command 0)))))
9724       (erase-buffer)
9725       (insert "$ " command "\n\n")
9726       (if gnus-view-pseudo-asynchronously
9727           (start-process "gnus-execute" (current-buffer) shell-file-name
9728                          shell-command-switch command)
9729         (call-process shell-file-name nil t nil
9730                       shell-command-switch command)))))
9731
9732 ;; Summary kill commands.
9733
9734 (defun gnus-summary-edit-global-kill (article)
9735   "Edit the \"global\" kill file."
9736   (interactive (list (gnus-summary-article-number)))
9737   (gnus-group-edit-global-kill article))
9738
9739 (defun gnus-summary-edit-local-kill ()
9740   "Edit a local kill file applied to the current newsgroup."
9741   (interactive)
9742   (setq gnus-current-headers (gnus-summary-article-header))
9743   (gnus-group-edit-local-kill
9744    (gnus-summary-article-number) gnus-newsgroup-name))
9745
9746 ;;; Header reading.
9747
9748 (defun gnus-read-header (id &optional header)
9749   "Read the headers of article ID and enter them into the Gnus system."
9750   (let ((group gnus-newsgroup-name)
9751         (gnus-override-method
9752          (or
9753           gnus-override-method
9754           (and (gnus-news-group-p gnus-newsgroup-name)
9755                (car (gnus-refer-article-methods)))))
9756         where)
9757     ;; First we check to see whether the header in question is already
9758     ;; fetched.
9759     (if (stringp id)
9760         ;; This is a Message-ID.
9761         (setq header (or header (gnus-id-to-header id)))
9762       ;; This is an article number.
9763       (setq header (or header (gnus-summary-article-header id))))
9764     (if (and header
9765              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9766         ;; We have found the header.
9767         header
9768       ;; If this is a sparse article, we have to nix out its
9769       ;; previous entry in the thread hashtb.
9770       (when (and header
9771                  (gnus-summary-article-sparse-p (mail-header-number header)))
9772         (let* ((parent (gnus-parent-id (mail-header-references header)))
9773                (thread (and parent (gnus-id-to-thread parent))))
9774           (when thread
9775             (delq (assq header thread) thread))))
9776       ;; We have to really fetch the header to this article.
9777       (save-excursion
9778         (set-buffer nntp-server-buffer)
9779         (when (setq where (gnus-request-head id group))
9780           (nnheader-fold-continuation-lines)
9781           (goto-char (point-max))
9782           (insert ".\n")
9783           (goto-char (point-min))
9784           (insert "211 ")
9785           (princ (cond
9786                   ((numberp id) id)
9787                   ((cdr where) (cdr where))
9788                   (header (mail-header-number header))
9789                   (t gnus-reffed-article-number))
9790                  (current-buffer))
9791           (insert " Article retrieved.\n"))
9792         (if (or (not where)
9793                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9794             ()                          ; Malformed head.
9795           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9796             (when (and (stringp id)
9797                        (not (string= (gnus-group-real-name group)
9798                                      (car where))))
9799               ;; If we fetched by Message-ID and the article came
9800               ;; from a different group, we fudge some bogus article
9801               ;; numbers for this article.
9802               (mail-header-set-number header gnus-reffed-article-number))
9803             (save-excursion
9804               (set-buffer gnus-summary-buffer)
9805               (decf gnus-reffed-article-number)
9806               (gnus-remove-header (mail-header-number header))
9807               (push header gnus-newsgroup-headers)
9808               (setq gnus-current-headers header)
9809               (push (mail-header-number header) gnus-newsgroup-limit)))
9810           header)))))
9811
9812 (defun gnus-remove-header (number)
9813   "Remove header NUMBER from `gnus-newsgroup-headers'."
9814   (if (and gnus-newsgroup-headers
9815            (= number (mail-header-number (car gnus-newsgroup-headers))))
9816       (pop gnus-newsgroup-headers)
9817     (let ((headers gnus-newsgroup-headers))
9818       (while (and (cdr headers)
9819                   (not (= number (mail-header-number (cadr headers)))))
9820         (pop headers))
9821       (when (cdr headers)
9822         (setcdr headers (cddr headers))))))
9823
9824 ;;;
9825 ;;; summary highlights
9826 ;;;
9827
9828 (defun gnus-highlight-selected-summary ()
9829   "Highlight selected article in summary buffer."
9830   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9831   (when gnus-summary-selected-face
9832     (save-excursion
9833       (let* ((beg (progn (beginning-of-line) (point)))
9834              (end (progn (end-of-line) (point)))
9835              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9836              (from (if (get-text-property beg gnus-mouse-face-prop)
9837                        beg
9838                      (or (next-single-property-change
9839                           beg gnus-mouse-face-prop nil end)
9840                          beg)))
9841              (to
9842               (if (= from end)
9843                   (- from 2)
9844                 (or (next-single-property-change
9845                      from gnus-mouse-face-prop nil end)
9846                     end))))
9847         ;; If no mouse-face prop on line we will have to = from = end,
9848         ;; so we highlight the entire line instead.
9849         (when (= (+ to 2) from)
9850           (setq from beg)
9851           (setq to end))
9852         (if gnus-newsgroup-selected-overlay
9853             ;; Move old overlay.
9854             (gnus-move-overlay
9855              gnus-newsgroup-selected-overlay from to (current-buffer))
9856           ;; Create new overlay.
9857           (gnus-overlay-put
9858            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9859            'face gnus-summary-selected-face))))))
9860
9861 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9862 (defun gnus-summary-highlight-line ()
9863   "Highlight current line according to `gnus-summary-highlight'."
9864   (let* ((list gnus-summary-highlight)
9865          (p (point))
9866          (end (progn (end-of-line) (point)))
9867          ;; now find out where the line starts and leave point there.
9868          (beg (progn (beginning-of-line) (point)))
9869          (article (gnus-summary-article-number))
9870          (score (or (cdr (assq (or article gnus-current-article)
9871                                gnus-newsgroup-scored))
9872                     gnus-summary-default-score 0))
9873          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9874          (inhibit-read-only t))
9875     ;; Eval the cars of the lists until we find a match.
9876     (let ((default gnus-summary-default-score))
9877       (while (and list
9878                   (not (eval (caar list))))
9879         (setq list (cdr list))))
9880     (let ((face (cdar list)))
9881       (unless (eq face (get-text-property beg 'face))
9882         (gnus-put-text-property-excluding-characters-with-faces
9883          beg end 'face
9884          (setq face (if (boundp face) (symbol-value face) face)))
9885         (when gnus-summary-highlight-line-function
9886           (funcall gnus-summary-highlight-line-function article face))))
9887     (goto-char p)))
9888
9889 (defun gnus-update-read-articles (group unread &optional compute)
9890   "Update the list of read articles in GROUP."
9891   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9892          (entry (gnus-gethash group gnus-newsrc-hashtb))
9893          (info (nth 2 entry))
9894          (prev 1)
9895          (unread (sort (copy-sequence unread) '<))
9896          read)
9897     (if (or (not info) (not active))
9898         ;; There is no info on this group if it was, in fact,
9899         ;; killed.  Gnus stores no information on killed groups, so
9900         ;; there's nothing to be done.
9901         ;; One could store the information somewhere temporarily,
9902         ;; perhaps...  Hmmm...
9903         ()
9904       ;; Remove any negative articles numbers.
9905       (while (and unread (< (car unread) 0))
9906         (setq unread (cdr unread)))
9907       ;; Remove any expired article numbers
9908       (while (and unread (< (car unread) (car active)))
9909         (setq unread (cdr unread)))
9910       ;; Compute the ranges of read articles by looking at the list of
9911       ;; unread articles.
9912       (while unread
9913         (when (/= (car unread) prev)
9914           (push (if (= prev (1- (car unread))) prev
9915                   (cons prev (1- (car unread))))
9916                 read))
9917         (setq prev (1+ (car unread)))
9918         (setq unread (cdr unread)))
9919       (when (<= prev (cdr active))
9920         (push (cons prev (cdr active)) read))
9921       (setq read (if (> (length read) 1) (nreverse read) read))
9922       (if compute
9923           read
9924         (save-excursion
9925           (let (setmarkundo)
9926             ;; Propagate the read marks to the backend.
9927             (when (gnus-check-backend-function 'request-set-mark group)
9928               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9929                     (add (gnus-remove-from-range read (gnus-info-read info))))
9930                 (when (or add del)
9931                   (unless (gnus-check-group group)
9932                     (error "Can't open server for %s" group))
9933                   (gnus-request-set-mark
9934                    group (delq nil (list (if add (list add 'add '(read)))
9935                                          (if del (list del 'del '(read))))))
9936                   (setq setmarkundo
9937                         `(gnus-request-set-mark
9938                           ,group
9939                           ',(delq nil (list
9940                                        (if del (list del 'add '(read)))
9941                                        (if add (list add 'del '(read))))))))))
9942             (set-buffer gnus-group-buffer)
9943             (gnus-undo-register
9944               `(progn
9945                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9946                  (gnus-info-set-read ',info ',(gnus-info-read info))
9947                  (gnus-get-unread-articles-in-group ',info
9948                                                     (gnus-active ,group))
9949                  (gnus-group-update-group ,group t)
9950                  ,setmarkundo))))
9951         ;; Enter this list into the group info.
9952         (gnus-info-set-read info read)
9953         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9954         (gnus-get-unread-articles-in-group info (gnus-active group))
9955         t))))
9956
9957 (defun gnus-offer-save-summaries ()
9958   "Offer to save all active summary buffers."
9959   (save-excursion
9960     (let ((buflist (buffer-list))
9961           buffers bufname)
9962       ;; Go through all buffers and find all summaries.
9963       (while buflist
9964         (and (setq bufname (buffer-name (car buflist)))
9965              (string-match "Summary" bufname)
9966              (save-excursion
9967                (set-buffer bufname)
9968                ;; We check that this is, indeed, a summary buffer.
9969                (and (eq major-mode 'gnus-summary-mode)
9970                     ;; Also make sure this isn't bogus.
9971                     gnus-newsgroup-prepared
9972                     ;; Also make sure that this isn't a dead summary buffer.
9973                     (not gnus-dead-summary-mode)))
9974              (push bufname buffers))
9975         (setq buflist (cdr buflist)))
9976       ;; Go through all these summary buffers and offer to save them.
9977       (when buffers
9978         (map-y-or-n-p
9979          "Update summary buffer %s? "
9980          (lambda (buf)
9981            (switch-to-buffer buf)
9982            (gnus-summary-exit))
9983          buffers)))))
9984
9985
9986 ;;; @ for mime-partial
9987 ;;;
9988
9989 (defun gnus-request-partial-message ()
9990   (save-excursion
9991     (let ((number (gnus-summary-article-number))
9992           (group gnus-newsgroup-name)
9993           (mother gnus-article-buffer))
9994       (set-buffer (get-buffer-create " *Partial Article*"))
9995       (erase-buffer)
9996       (setq mime-preview-buffer mother)
9997       (gnus-request-article-this-buffer number group)
9998       (mime-parse-buffer)
9999       )))
10000
10001 (autoload 'mime-combine-message/partial-pieces-automatically
10002   "mime-partial"
10003   "Internal method to combine message/partial messages automatically.")
10004
10005 (mime-add-condition
10006  'action '((type . message)(subtype . partial)
10007            (major-mode . gnus-original-article-mode)
10008            (method . mime-combine-message/partial-pieces-automatically)
10009            (summary-buffer-exp . gnus-summary-buffer)
10010            (request-partial-message-method . gnus-request-partial-message)
10011            ))
10012
10013
10014 ;;; @ for message/rfc822
10015 ;;;
10016
10017 (defun gnus-mime-extract-message/rfc822 (entity situation)
10018   (let (group article num cwin swin cur)
10019     (with-temp-buffer
10020       (mime-insert-entity-content entity)
10021       (setq group (or (cdr (assq 'group situation))
10022                       (completing-read "Group: "
10023                                        gnus-active-hashtb
10024                                        nil
10025                                        (gnus-read-active-file-p)
10026                                        gnus-newsgroup-name))
10027             article (gnus-request-accept-article group)))
10028     (when (and (consp article)
10029                (numberp (setq article (cdr article))))
10030       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10031             cwin (get-buffer-window (current-buffer) t))
10032       (save-window-excursion
10033         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10034             (select-window swin)
10035           (set-buffer gnus-summary-buffer))
10036         (setq cur gnus-current-article)
10037         (forward-line num)
10038         (let (gnus-show-threads)
10039           (gnus-summary-goto-subject article t))
10040         (gnus-summary-clear-mark-forward 1)
10041         (gnus-summary-goto-subject cur))
10042       (when (and cwin (window-frame cwin))
10043         (select-frame (window-frame cwin)))
10044       (when (boundp 'mime-acting-situation-to-override)
10045         (set-alist 'mime-acting-situation-to-override
10046                    'group
10047                    group)
10048         (set-alist 'mime-acting-situation-to-override
10049                    'after-method
10050                    `(progn
10051                       (save-current-buffer
10052                         (set-buffer gnus-group-buffer)
10053                         (gnus-activate-group ,group))
10054                       (gnus-summary-goto-article ,cur
10055                                                  gnus-show-all-headers)))
10056         (set-alist 'mime-acting-situation-to-override
10057                    'number num)))))
10058
10059 (mime-add-condition
10060  'action '((type . message)(subtype . rfc822)
10061            (major-mode . gnus-original-article-mode)
10062            (method . gnus-mime-extract-message/rfc822)
10063            (mode . "extract")
10064            ))
10065
10066 (mime-add-condition
10067  'action '((type . message)(subtype . news)
10068            (major-mode . gnus-original-article-mode)
10069            (method . gnus-mime-extract-message/rfc822)
10070            (mode . "extract")
10071            ))
10072
10073 (defun gnus-mime-extract-multipart (entity situation)
10074   (let ((children (mime-entity-children entity))
10075         mime-acting-situation-to-override
10076         f)
10077     (while children
10078       (mime-play-entity (car children)
10079                         (cons (assq 'mode situation)
10080                               mime-acting-situation-to-override))
10081       (setq children (cdr children)))
10082     (if (setq f (cdr (assq 'after-method
10083                            mime-acting-situation-to-override)))
10084         (eval f)
10085       )))
10086
10087 (mime-add-condition
10088  'action '((type . multipart)
10089            (method . gnus-mime-extract-multipart)
10090            (mode . "extract")
10091            )
10092  'with-default)
10093
10094
10095 ;;; @ end
10096 ;;;
10097
10098 (defun gnus-summary-setup-default-charset ()
10099   "Setup newsgroup default charset."
10100   (if (equal gnus-newsgroup-name "nndraft:drafts")
10101       (setq gnus-newsgroup-charset nil)
10102     (let* ((name (and gnus-newsgroup-name
10103                       (gnus-group-real-name gnus-newsgroup-name)))
10104            (ignored-charsets
10105             (or gnus-newsgroup-ephemeral-ignored-charsets
10106                 (append
10107                  (and gnus-newsgroup-name
10108                       (or (gnus-group-find-parameter gnus-newsgroup-name
10109                                                      'ignored-charsets t)
10110                           (let ((alist gnus-group-ignored-charsets-alist)
10111                                 elem (charsets nil))
10112                             (while (setq elem (pop alist))
10113                               (when (and name
10114                                          (string-match (car elem) name))
10115                                 (setq alist nil
10116                                       charsets (cdr elem))))
10117                             charsets)))
10118                  gnus-newsgroup-ignored-charsets))))
10119       (setq gnus-newsgroup-charset
10120             (or gnus-newsgroup-ephemeral-charset
10121                 (and gnus-newsgroup-name
10122                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
10123                          (let ((alist gnus-group-charset-alist)
10124                                elem charset)
10125                            (while (setq elem (pop alist))
10126                              (when (and name
10127                                         (string-match (car elem) name))
10128                                (setq alist nil
10129                                      charset (cadr elem))))
10130                            charset)))
10131                 gnus-default-charset))
10132       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10133            ignored-charsets))))
10134
10135 ;;;
10136 ;;; Mime Commands
10137 ;;;
10138
10139 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10140   "Display the current article buffer fully MIME-buttonized.
10141 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10142 treated as multipart/mixed."
10143   (interactive "P")
10144   (require 'gnus-art)
10145   (let ((gnus-unbuttonized-mime-types nil)
10146         (gnus-mime-display-multipart-as-mixed show-all-parts))
10147     (gnus-summary-show-article)))
10148
10149 (defun gnus-summary-repair-multipart (article)
10150   "Add a Content-Type header to a multipart article without one."
10151   (interactive (list (gnus-summary-article-number)))
10152   (gnus-with-article article
10153     (message-narrow-to-head)
10154     (message-remove-header "Mime-Version")
10155     (goto-char (point-max))
10156     (insert "Mime-Version: 1.0\n")
10157     (widen)
10158     (when (search-forward "\n--" nil t)
10159       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10160         (message-narrow-to-head)
10161         (message-remove-header "Content-Type")
10162         (goto-char (point-max))
10163         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10164                         separator))
10165         (widen))))
10166   (let (gnus-mark-article-hook)
10167     (gnus-summary-select-article t t nil article)))
10168
10169 (defun gnus-summary-toggle-display-buttonized ()
10170   "Toggle the buttonizing of the article buffer."
10171   (interactive)
10172   (require 'gnus-art)
10173   (if (setq gnus-inhibit-mime-unbuttonizing
10174             (not gnus-inhibit-mime-unbuttonizing))
10175       (let ((gnus-unbuttonized-mime-types nil))
10176         (gnus-summary-show-article))
10177     (gnus-summary-show-article)))
10178
10179 ;;;
10180 ;;; Intelli-mouse commmands
10181 ;;;
10182
10183 (defun gnus-wheel-summary-scroll (event)
10184   (interactive "e")
10185   (let ((amount (if (memq 'shift (event-modifiers event))
10186                     (car gnus-wheel-scroll-amount)
10187                   (cdr gnus-wheel-scroll-amount)))
10188         (direction (- (* (static-if (featurep 'xemacs)
10189                              (event-button event)
10190                            (cond ((eq 'mouse-4 (event-basic-type event))
10191                                   4)
10192                                  ((eq 'mouse-5 (event-basic-type event))
10193                                   5)))
10194                          2) 9))
10195         edge)
10196     (gnus-summary-scroll-up (* amount direction))
10197     (when (gnus-eval-in-buffer-window gnus-article-buffer
10198             (save-restriction
10199               (widen)
10200               (and (if (< 0 direction)
10201                        (gnus-article-next-page 0)
10202                      (gnus-article-prev-page 0)
10203                      (bobp))
10204                    (if (setq edge (get-text-property
10205                                    (point-min) 'gnus-wheel-edge))
10206                        (setq edge (* edge direction))
10207                      (setq edge -1))
10208                    (or (plusp edge)
10209                        (let ((buffer-read-only nil)
10210                              (inhibit-read-only t))
10211                          (put-text-property (point-min) (point-max)
10212                                             'gnus-wheel-edge direction)
10213                          nil))
10214                    (or (> edge gnus-wheel-edge-resistance)
10215                        (let ((buffer-read-only nil)
10216                              (inhibit-read-only t))
10217                          (put-text-property (point-min) (point-max)
10218                                             'gnus-wheel-edge
10219                                             (* (1+ edge) direction))
10220                          nil))
10221                    (eq last-command 'gnus-wheel-summary-scroll))))
10222       (gnus-summary-next-article nil nil (minusp direction)))))
10223
10224 (defun gnus-wheel-install ()
10225   "Enable mouse wheel support on summary window."
10226   (when gnus-use-wheel
10227     (let ((keys
10228            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10229       (dolist (key keys)
10230         (define-key gnus-summary-mode-map key
10231           'gnus-wheel-summary-scroll)))))
10232
10233 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10234
10235 ;;;
10236 ;;; Traditional PGP commmands
10237 ;;;
10238
10239 (defun gnus-summary-decrypt-article (&optional force)
10240   "Decrypt the current article in traditional PGP way.
10241 This will have permanent effect only in mail groups.
10242 If FORCE is non-nil, allow editing of articles even in read-only
10243 groups."
10244   (interactive "P")
10245   (gnus-summary-select-article t)
10246   (gnus-eval-in-buffer-window gnus-article-buffer
10247     (save-excursion
10248       (save-restriction
10249         (widen)
10250         (goto-char (point-min))
10251         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10252           (error "Not a traditional PGP message!"))
10253         (let ((armor-start (match-beginning 0)))
10254           (if (and (pgg-decrypt-region armor-start (point-max))
10255                    (or force (not (gnus-group-read-only-p))))
10256               (let ((inhibit-read-only t)
10257                     buffer-read-only)
10258                 (delete-region armor-start
10259                                (progn
10260                                  (re-search-forward "^-+END PGP" nil t)
10261                                  (beginning-of-line 2)
10262                                  (point)))
10263                 (insert-buffer-substring pgg-output-buffer))))))))
10264
10265 (defun gnus-summary-verify-article ()
10266   "Verify the current article in traditional PGP way."
10267   (interactive)
10268   (save-excursion
10269     (set-buffer gnus-original-article-buffer)
10270     (goto-char (point-min))
10271     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10272       (error "Not a traditional PGP message!"))
10273     (re-search-forward "^-+END PGP" nil t)
10274     (beginning-of-line 2)
10275     (call-interactively (function pgg-verify-region))))
10276
10277 ;;;
10278 ;;; Generic summary marking commands
10279 ;;;
10280
10281 (defvar gnus-summary-marking-alist
10282   '((read gnus-del-mark "d")
10283     (unread gnus-unread-mark "u")
10284     (ticked gnus-ticked-mark "!")
10285     (dormant gnus-dormant-mark "?")
10286     (expirable gnus-expirable-mark "e"))
10287   "An alist of names/marks/keystrokes.")
10288
10289 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10290 (defvar gnus-summary-mark-map)
10291
10292 (defun gnus-summary-make-all-marking-commands ()
10293   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10294   (dolist (elem gnus-summary-marking-alist)
10295     (apply 'gnus-summary-make-marking-command elem)))
10296
10297 (defun gnus-summary-make-marking-command (name mark keystroke)
10298   (let ((map (make-sparse-keymap)))
10299     (define-key gnus-summary-generic-mark-map keystroke map)
10300     (dolist (lway `((next "next" next nil "n")
10301                     (next-unread "next unread" next t "N")
10302                     (prev "previous" prev nil "p")
10303                     (prev-unread "previous unread" prev t "P")
10304                     (nomove "" nil nil ,keystroke)))
10305       (let ((func (gnus-summary-make-marking-command-1
10306                    mark (car lway) lway name)))
10307         (setq func (eval func))
10308         (define-key map (nth 4 lway) func)))))
10309
10310 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10311   `(defun ,(intern
10312             (format "gnus-summary-put-mark-as-%s%s"
10313                     name (if (eq way 'nomove)
10314                              ""
10315                            (concat "-" (symbol-name way)))))
10316      (n)
10317      ,(format
10318        "Mark the current article as %s%s.
10319 If N, the prefix, then repeat N times.
10320 If N is negative, move in reverse order.
10321 The difference between N and the actual number of articles marked is
10322 returned."
10323        name (car (cdr lway)))
10324      (interactive "p")
10325      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10326
10327 (defun gnus-summary-generic-mark (n mark move unread)
10328   "Mark N articles with MARK."
10329   (unless (eq major-mode 'gnus-summary-mode)
10330     (error "This command can only be used in the summary buffer"))
10331   (gnus-summary-show-thread)
10332   (let ((nummove
10333          (cond
10334           ((eq move 'next) 1)
10335           ((eq move 'prev) -1)
10336           (t 0))))
10337     (if (zerop nummove)
10338         (setq n 1)
10339       (when (< n 0)
10340         (setq n (abs n)
10341               nummove (* -1 nummove))))
10342     (while (and (> n 0)
10343                 (gnus-summary-mark-article nil mark)
10344                 (zerop (gnus-summary-next-subject nummove unread t)))
10345       (setq n (1- n)))
10346     (when (/= 0 n)
10347       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10348     (gnus-summary-recenter)
10349     (gnus-summary-position-point)
10350     (gnus-set-mode-line 'summary)
10351     n))
10352
10353 (gnus-summary-make-all-marking-commands)
10354
10355 (gnus-ems-redefine)
10356
10357 (provide 'gnus-sum)
10358
10359 (run-hooks 'gnus-sum-load-hook)
10360
10361 ;;; gnus-sum.el ends here