Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
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 (defcustom gnus-summary-menu-hook nil
682   "*Hook run after the creation of the summary mode menu."
683   :group 'gnus-summary-visual
684   :type 'hook)
685
686 (defcustom gnus-summary-exit-hook nil
687   "*A hook called on exit from the summary buffer.
688 It will be called with point in the group buffer."
689   :group 'gnus-summary-exit
690   :type 'hook)
691
692 (defcustom gnus-summary-prepare-hook nil
693   "*A hook called after the summary buffer has been generated.
694 If you want to modify the summary buffer, you can use this hook."
695   :group 'gnus-summary-various
696   :type 'hook)
697
698 (defcustom gnus-summary-prepared-hook nil
699   "*A hook called as the last thing after the summary buffer has been generated."
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 (defcustom gnus-summary-generate-hook nil
704   "*A hook run just before generating the summary buffer.
705 This hook is commonly used to customize threading variables and the
706 like."
707   :group 'gnus-summary-various
708   :type 'hook)
709
710 (defcustom gnus-select-group-hook nil
711   "*A hook called when a newsgroup is selected.
712
713 If you'd like to simplify subjects like the
714 `gnus-summary-next-same-subject' command does, you can use the
715 following hook:
716
717  (setq gnus-select-group-hook
718       (list
719         (lambda ()
720           (mapcar (lambda (header)
721                      (mail-header-set-subject
722                       header
723                       (gnus-simplify-subject
724                        (mail-header-subject header) 're-only)))
725                   gnus-newsgroup-headers))))"
726   :group 'gnus-group-select
727   :type 'hook)
728
729 (defcustom gnus-select-article-hook nil
730   "*A hook called when an article is selected."
731   :group 'gnus-summary-choose
732   :type 'hook)
733
734 (defcustom gnus-visual-mark-article-hook
735   (list 'gnus-highlight-selected-summary)
736   "*Hook run after selecting an article in the summary buffer.
737 It is meant to be used for highlighting the article in some way.  It
738 is not run if `gnus-visual' is nil."
739   :group 'gnus-summary-visual
740   :type 'hook)
741
742 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
743   "*A hook called before parsing the headers."
744   :group 'gnus-various
745   :type 'hook)
746
747 (defcustom gnus-exit-group-hook nil
748   "*A hook called when exiting summary mode.
749 This hook is not called from the non-updating exit commands like `Q'."
750   :group 'gnus-various
751   :type 'hook)
752
753 (defcustom gnus-summary-update-hook
754   (list 'gnus-summary-highlight-line)
755   "*A hook called when a summary line is changed.
756 The hook will not be called if `gnus-visual' is nil.
757
758 The default function `gnus-summary-highlight-line' will
759 highlight the line according to the `gnus-summary-highlight'
760 variable."
761   :group 'gnus-summary-visual
762   :type 'hook)
763
764 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
765   "*A hook called when an article is selected for the first time.
766 The hook is intended to mark an article as read (or unread)
767 automatically when it is selected."
768   :group 'gnus-summary-choose
769   :type 'hook)
770
771 (defcustom gnus-group-no-more-groups-hook nil
772   "*A hook run when returning to group mode having no more (unread) groups."
773   :group 'gnus-group-select
774   :type 'hook)
775
776 (defcustom gnus-ps-print-hook nil
777   "*A hook run before ps-printing something from Gnus."
778   :group 'gnus-summary
779   :type 'hook)
780
781 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
782   "Face used for highlighting the current article in the summary buffer."
783   :group 'gnus-summary-visual
784   :type 'face)
785
786 (defcustom gnus-summary-highlight
787   '(((= mark gnus-canceled-mark)
788      . gnus-summary-cancelled-face)
789     ((and (> score default)
790           (or (= mark gnus-dormant-mark)
791               (= mark gnus-ticked-mark)))
792      . gnus-summary-high-ticked-face)
793     ((and (< score default)
794           (or (= mark gnus-dormant-mark)
795               (= mark gnus-ticked-mark)))
796      . gnus-summary-low-ticked-face)
797     ((or (= mark gnus-dormant-mark)
798          (= mark gnus-ticked-mark))
799      . gnus-summary-normal-ticked-face)
800     ((and (> score default) (= mark gnus-ancient-mark))
801      . gnus-summary-high-ancient-face)
802     ((and (< score default) (= mark gnus-ancient-mark))
803      . gnus-summary-low-ancient-face)
804     ((= mark gnus-ancient-mark)
805      . gnus-summary-normal-ancient-face)
806     ((and (> score default) (= mark gnus-unread-mark))
807      . gnus-summary-high-unread-face)
808     ((and (< score default) (= mark gnus-unread-mark))
809      . gnus-summary-low-unread-face)
810     ((and (memq article gnus-newsgroup-incorporated)
811           (= mark gnus-unread-mark))
812      . gnus-summary-incorporated-face)
813     ((= mark gnus-unread-mark)
814      . gnus-summary-normal-unread-face)
815     ((and (> score default) (memq mark (list gnus-downloadable-mark
816                                              gnus-undownloaded-mark)))
817      . gnus-summary-high-unread-face)
818     ((and (< score default) (memq mark (list gnus-downloadable-mark
819                                              gnus-undownloaded-mark)))
820      . gnus-summary-low-unread-face)
821     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
822      . gnus-summary-normal-unread-face)
823     ((> score default)
824      . gnus-summary-high-read-face)
825     ((< score default)
826      . gnus-summary-low-read-face)
827     (t
828      . gnus-summary-normal-read-face))
829   "*Controls the highlighting of summary buffer lines.
830
831 A list of (FORM . FACE) pairs.  When deciding how a a particular
832 summary line should be displayed, each form is evaluated.  The content
833 of the face field after the first true form is used.  You can change
834 how those summary lines are displayed, by editing the face field.
835
836 You can use the following variables in the FORM field.
837
838 score:   The articles score
839 default: The default article score.
840 below:   The score below which articles are automatically marked as read.
841 mark:    The articles mark."
842   :group 'gnus-summary-visual
843   :type '(repeat (cons (sexp :tag "Form" nil)
844                        face)))
845
846 (defcustom gnus-alter-header-function nil
847   "Function called to allow alteration of article header structures.
848 The function is called with one parameter, the article header vector,
849 which it may alter in any way.")
850
851 (defvar gnus-decode-encoded-word-function
852   (mime-find-field-decoder 'From 'nov)
853   "Variable that says which function should be used to decode a string with encoded words.")
854
855 (defcustom gnus-extra-headers nil
856   "*Extra headers to parse."
857   :version "21.1"
858   :group 'gnus-summary
859   :type '(repeat symbol))
860
861 (defcustom gnus-ignored-from-addresses
862   (and user-mail-address (regexp-quote user-mail-address))
863   "*Regexp of From headers that may be suppressed in favor of To headers."
864   :version "21.1"
865   :group 'gnus-summary
866   :type 'regexp)
867
868 (defcustom gnus-group-charset-alist
869   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
870     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
871     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
872     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
873     ("^relcom\\>" koi8-r)
874     ("^fido7\\>" koi8-r)
875     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
876     ("^israel\\>" iso-8859-1)
877     ("^han\\>" euc-kr)
878     ("^alt.chinese.text.big5\\>" chinese-big5)
879     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
880     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
881     (".*" iso-8859-1))
882   "Alist of regexps (to match group names) and default charsets to be used when reading."
883   :type '(repeat (list (regexp :tag "Group")
884                        (symbol :tag "Charset")))
885   :group 'gnus-charset)
886
887 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
888   "List of charsets that should be ignored.
889 When these charsets are used in the \"charset\" parameter, the
890 default charset will be used instead."
891   :version "21.1"
892   :type '(repeat symbol)
893   :group 'gnus-charset)
894
895 (defcustom gnus-group-ignored-charsets-alist
896   '(("alt\\.chinese\\.text" iso-8859-1))
897   "Alist of regexps (to match group names) and charsets that should be ignored.
898 When these charsets are used in the \"charset\" parameter, the
899 default charset will be used instead."
900   :type '(repeat (cons (regexp :tag "Group")
901                        (repeat symbol)))
902   :group 'gnus-charset)
903
904 (defcustom gnus-group-highlight-words-alist nil
905   "Alist of group regexps and highlight regexps.
906 This variable uses the same syntax as `gnus-emphasis-alist'."
907   :version "21.1"
908   :type '(repeat (cons (regexp :tag "Group")
909                        (repeat (list (regexp :tag "Highlight regexp")
910                                      (number :tag "Group for entire word" 0)
911                                      (number :tag "Group for displayed part" 0)
912                                      (symbol :tag "Face"
913                                              gnus-emphasis-highlight-words)))))
914   :group 'gnus-summary-visual)
915
916 (defcustom gnus-use-wheel nil
917   "Use Intelli-mouse on summary movement"
918   :type 'boolean
919   :group 'gnus-summary-maneuvering)
920
921 (defcustom gnus-wheel-scroll-amount '(5 . 1)
922   "Amount to scroll messages by spinning the mouse wheel.
923 This is actually a cons cell, where the first item is the amount to scroll
924 on a normal wheel event, and the second is the amount to scroll when the
925 wheel is moved with the shift key depressed."
926   :type '(cons (integer :tag "Shift") integer)
927   :group 'gnus-summary-maneuvering)
928
929 (defcustom gnus-wheel-edge-resistance 2
930   "How hard it should be to change the current article
931 by moving the mouse over the edge of the article window."
932   :type 'integer
933   :group 'gnus-summary-maneuvering)
934
935 (defcustom gnus-summary-show-article-charset-alist
936   nil
937   "Alist of number and charset.
938 The article will be shown with the charset corresponding to the
939 numbered argument.
940 For example: ((1 . cn-gb-2312) (2 . big5))."
941   :version "21.1"
942   :type '(repeat (cons (number :tag "Argument" 1)
943                        (symbol :tag "Charset")))
944   :group 'gnus-charset)
945
946 (defcustom gnus-preserve-marks t
947   "Whether marks are preserved when moving, copying and respooling messages."
948   :version "21.1"
949   :type 'boolean
950   :group 'gnus-summary-marks)
951
952 (defcustom gnus-alter-articles-to-read-function nil
953   "Function to be called to alter the list of articles to be selected."
954   :type 'function
955   :group 'gnus-summary)
956
957 (defcustom gnus-orphan-score nil
958   "*All orphans get this score added.  Set in the score file."
959   :group 'gnus-score-default
960   :type '(choice (const nil)
961                  integer))
962
963 (defcustom gnus-summary-save-parts-default-mime "image/.*"
964   "*A regexp to match MIME parts when saving multiple parts of a message
965 with gnus-summary-save-parts (X m). This regexp will be used by default
966 when prompting the user for which type of files to save."
967   :group 'gnus-summary
968   :type 'regexp)
969
970
971 (defcustom gnus-summary-save-parts-default-mime "image/.*"
972   "*A regexp to match MIME parts when saving multiple parts of a message
973 with gnus-summary-save-parts (X m). This regexp will be used by default
974 when prompting the user for which type of files to save."
975   :group 'gnus-summary
976   :type 'regexp)
977
978
979 ;;; Internal variables
980
981 (defvar gnus-article-mime-handles nil)
982 (defvar gnus-article-decoded-p nil)
983 (defvar gnus-article-charset nil)
984 (defvar gnus-article-ignored-charsets nil)
985 (defvar gnus-scores-exclude-files nil)
986 (defvar gnus-page-broken nil)
987 (defvar gnus-inhibit-mime-unbuttonizing nil)
988
989 (defvar gnus-original-article nil)
990 (defvar gnus-article-internal-prepare-hook nil)
991 (defvar gnus-newsgroup-process-stack nil)
992
993 (defvar gnus-thread-indent-array nil)
994 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
995 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
996   "Function called to sort the articles within a thread after it has been gathered together.")
997
998 (defvar gnus-summary-save-parts-type-history nil)
999 (defvar gnus-summary-save-parts-last-directory nil)
1000
1001 (defvar gnus-summary-save-parts-type-history nil)
1002 (defvar gnus-summary-save-parts-last-directory nil)
1003
1004 ;; Avoid highlighting in kill files.
1005 (defvar gnus-summary-inhibit-highlight nil)
1006 (defvar gnus-newsgroup-selected-overlay nil)
1007 (defvar gnus-inhibit-limiting nil)
1008 (defvar gnus-newsgroup-adaptive-score-file nil)
1009 (defvar gnus-current-score-file nil)
1010 (defvar gnus-current-move-group nil)
1011 (defvar gnus-current-copy-group nil)
1012 (defvar gnus-current-crosspost-group nil)
1013
1014 (defvar gnus-newsgroup-dependencies nil)
1015 (defvar gnus-newsgroup-adaptive nil)
1016 (defvar gnus-summary-display-article-function nil)
1017 (defvar gnus-summary-highlight-line-function nil
1018   "Function called after highlighting a summary line.")
1019
1020 (defvar gnus-summary-line-format-alist
1021   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1022     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1023     (?s gnus-tmp-subject-or-nil ?s)
1024     (?n gnus-tmp-name ?s)
1025     (?A (std11-address-string
1026          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1027     (?a (or (std11-full-name-string
1028              (car (mime-entity-read-field gnus-tmp-header 'From)))
1029             gnus-tmp-from) ?s)
1030     (?F gnus-tmp-from ?s)
1031     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1032     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1033     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1034     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1035     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1036     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1037     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1038     (?L gnus-tmp-lines ?d)
1039     (?I gnus-tmp-indentation ?s)
1040     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1041     (?R gnus-tmp-replied ?c)
1042     (?\[ gnus-tmp-opening-bracket ?c)
1043     (?\] gnus-tmp-closing-bracket ?c)
1044     (?\> (make-string gnus-tmp-level ? ) ?s)
1045     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1046     (?i gnus-tmp-score ?d)
1047     (?z gnus-tmp-score-char ?c)
1048     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1049     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1050     (?U gnus-tmp-unread ?c)
1051     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1052     (?t (gnus-summary-number-of-articles-in-thread
1053          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1054         ?d)
1055     (?e (gnus-summary-number-of-articles-in-thread
1056          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1057         ?c)
1058     (?u gnus-tmp-user-defined ?s)
1059     (?P (gnus-pick-line-number) ?d))
1060   "An alist of format specifications that can appear in summary lines.
1061 These are paired with what variables they correspond with, along with
1062 the type of the variable (string, integer, character, etc).")
1063
1064 (defvar gnus-summary-dummy-line-format-alist
1065   `((?S gnus-tmp-subject ?s)
1066     (?N gnus-tmp-number ?d)
1067     (?u gnus-tmp-user-defined ?s)))
1068
1069 (defvar gnus-summary-mode-line-format-alist
1070   `((?G gnus-tmp-group-name ?s)
1071     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1072     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1073     (?A gnus-tmp-article-number ?d)
1074     (?Z gnus-tmp-unread-and-unselected ?s)
1075     (?V gnus-version ?s)
1076     (?U gnus-tmp-unread-and-unticked ?d)
1077     (?S gnus-tmp-subject ?s)
1078     (?e gnus-tmp-unselected ?d)
1079     (?u gnus-tmp-user-defined ?s)
1080     (?d (length gnus-newsgroup-dormant) ?d)
1081     (?t (length gnus-newsgroup-marked) ?d)
1082     (?r (length gnus-newsgroup-reads) ?d)
1083     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1084     (?E gnus-newsgroup-expunged-tally ?d)
1085     (?s (gnus-current-score-file-nondirectory) ?s)))
1086
1087 (defvar gnus-last-search-regexp nil
1088   "Default regexp for article search command.")
1089
1090 (defvar gnus-summary-search-article-matched-data nil
1091   "Last matched data of article search command.  It is the local variable
1092 in `gnus-article-buffer' which consists of the list of start position,
1093 end position and text.")
1094
1095 (defvar gnus-last-shell-command nil
1096   "Default shell command on article.")
1097
1098 (defvar gnus-newsgroup-begin nil)
1099 (defvar gnus-newsgroup-end nil)
1100 (defvar gnus-newsgroup-last-rmail nil)
1101 (defvar gnus-newsgroup-last-mail nil)
1102 (defvar gnus-newsgroup-last-folder nil)
1103 (defvar gnus-newsgroup-last-file nil)
1104 (defvar gnus-newsgroup-auto-expire nil)
1105 (defvar gnus-newsgroup-active nil)
1106
1107 (defvar gnus-newsgroup-data nil)
1108 (defvar gnus-newsgroup-data-reverse nil)
1109 (defvar gnus-newsgroup-limit nil)
1110 (defvar gnus-newsgroup-limits nil)
1111
1112 (defvar gnus-newsgroup-unreads nil
1113   "List of unread articles in the current newsgroup.")
1114
1115 (defvar gnus-newsgroup-unselected nil
1116   "List of unselected unread articles in the current newsgroup.")
1117
1118 (defvar gnus-newsgroup-reads nil
1119   "Alist of read articles and article marks in the current newsgroup.")
1120
1121 (defvar gnus-newsgroup-expunged-tally nil)
1122
1123 (defvar gnus-newsgroup-marked nil
1124   "List of ticked articles in the current newsgroup (a subset of unread art).")
1125
1126 (defvar gnus-newsgroup-killed nil
1127   "List of ranges of articles that have been through the scoring process.")
1128
1129 (defvar gnus-newsgroup-cached nil
1130   "List of articles that come from the article cache.")
1131
1132 (defvar gnus-newsgroup-saved nil
1133   "List of articles that have been saved.")
1134
1135 (defvar gnus-newsgroup-kill-headers nil)
1136
1137 (defvar gnus-newsgroup-replied nil
1138   "List of articles that have been replied to in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-expirable nil
1141   "List of articles in the current newsgroup that can be expired.")
1142
1143 (defvar gnus-newsgroup-processable nil
1144   "List of articles in the current newsgroup that can be processed.")
1145
1146 (defvar gnus-newsgroup-downloadable nil
1147   "List of articles in the current newsgroup that can be processed.")
1148
1149 (defvar gnus-newsgroup-undownloaded nil
1150   "List of articles in the current newsgroup that haven't been downloaded..")
1151
1152 (defvar gnus-newsgroup-unsendable nil
1153   "List of articles in the current newsgroup that won't be sent.")
1154
1155 (defvar gnus-newsgroup-bookmarks nil
1156   "List of articles in the current newsgroup that have bookmarks.")
1157
1158 (defvar gnus-newsgroup-dormant nil
1159   "List of dormant articles in the current newsgroup.")
1160
1161 (defvar gnus-newsgroup-scored nil
1162   "List of scored articles in the current newsgroup.")
1163
1164 (defvar gnus-newsgroup-incorporated nil
1165   "List of incorporated articles in the current newsgroup.")
1166
1167 (defvar gnus-newsgroup-headers nil
1168   "List of article headers in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-threads nil)
1171
1172 (defvar gnus-newsgroup-prepared nil
1173   "Whether the current group has been prepared properly.")
1174
1175 (defvar gnus-newsgroup-ancient nil
1176   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1177
1178 (defvar gnus-newsgroup-sparse nil)
1179
1180 (defvar gnus-current-article nil)
1181 (defvar gnus-article-current nil)
1182 (defvar gnus-current-headers nil)
1183 (defvar gnus-have-all-headers nil)
1184 (defvar gnus-last-article nil)
1185 (defvar gnus-newsgroup-history nil)
1186 (defvar gnus-newsgroup-charset nil)
1187 (defvar gnus-newsgroup-ephemeral-charset nil)
1188 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1189
1190 (defvar gnus-article-before-search nil)
1191
1192 (defconst gnus-summary-local-variables
1193   '(gnus-newsgroup-name
1194     gnus-newsgroup-begin gnus-newsgroup-end
1195     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1196     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1197     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1198     gnus-newsgroup-unselected gnus-newsgroup-marked
1199     gnus-newsgroup-reads gnus-newsgroup-saved
1200     gnus-newsgroup-replied gnus-newsgroup-expirable
1201     gnus-newsgroup-processable gnus-newsgroup-killed
1202     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1203     gnus-newsgroup-unsendable
1204     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1205     gnus-newsgroup-headers gnus-newsgroup-threads
1206     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1207     gnus-current-article gnus-current-headers gnus-have-all-headers
1208     gnus-last-article gnus-article-internal-prepare-hook
1209     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1210     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1211     gnus-thread-expunge-below
1212     gnus-score-alist gnus-current-score-file
1213     (gnus-summary-expunge-below . global)
1214     (gnus-summary-mark-below . global)
1215     (gnus-orphan-score . global)
1216     gnus-newsgroup-active gnus-scores-exclude-files
1217     gnus-newsgroup-history gnus-newsgroup-ancient
1218     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1219     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1220     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1221     (gnus-newsgroup-expunged-tally . 0)
1222     gnus-cache-removable-articles gnus-newsgroup-cached
1223     gnus-newsgroup-data gnus-newsgroup-data-reverse
1224     gnus-newsgroup-limit gnus-newsgroup-limits
1225     gnus-newsgroup-charset
1226     gnus-newsgroup-incorporated)
1227   "Variables that are buffer-local to the summary buffers.")
1228
1229 (defvar gnus-newsgroup-variables nil
1230   "Variables that have separate values in the newsgroups.")
1231
1232 ;; Byte-compiler warning.
1233 (eval-when-compile (defvar gnus-article-mode-map))
1234
1235 ;; Subject simplification.
1236
1237 (defun gnus-simplify-whitespace (str)
1238   "Remove excessive whitespace from STR."
1239   (let ((mystr str))
1240     ;; Multiple spaces.
1241     (while (string-match "[ \t][ \t]+" mystr)
1242       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1243                           " "
1244                           (substring mystr (match-end 0)))))
1245     ;; Leading spaces.
1246     (when (string-match "^[ \t]+" mystr)
1247       (setq mystr (substring mystr (match-end 0))))
1248     ;; Trailing spaces.
1249     (when (string-match "[ \t]+$" mystr)
1250       (setq mystr (substring mystr 0 (match-beginning 0))))
1251     mystr))
1252
1253 (defsubst gnus-simplify-subject-re (subject)
1254   "Remove \"Re:\" from subject lines."
1255   (if (string-match "^[Rr][Ee]: *" subject)
1256       (substring subject (match-end 0))
1257     subject))
1258
1259 (defun gnus-simplify-subject (subject &optional re-only)
1260   "Remove `Re:' and words in parentheses.
1261 If RE-ONLY is non-nil, strip leading `Re:'s only."
1262   (let ((case-fold-search t))           ;Ignore case.
1263     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1264     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1265       (setq subject (substring subject (match-end 0))))
1266     ;; Remove uninteresting prefixes.
1267     (when (and (not re-only)
1268                gnus-simplify-ignored-prefixes
1269                (string-match gnus-simplify-ignored-prefixes subject))
1270       (setq subject (substring subject (match-end 0))))
1271     ;; Remove words in parentheses from end.
1272     (unless re-only
1273       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1274         (setq subject (substring subject 0 (match-beginning 0)))))
1275     ;; Return subject string.
1276     subject))
1277
1278 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1279 ;; all whitespace.
1280 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1281   (goto-char (point-min))
1282   (while (re-search-forward regexp nil t)
1283     (replace-match (or newtext ""))))
1284
1285 (defun gnus-simplify-buffer-fuzzy ()
1286   "Simplify string in the buffer fuzzily.
1287 The string in the accessible portion of the current buffer is simplified.
1288 It is assumed to be a single-line subject.
1289 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1290 matter is removed.  Additional things can be deleted by setting
1291 `gnus-simplify-subject-fuzzy-regexp'."
1292   (let ((case-fold-search t)
1293         (modified-tick))
1294     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1295
1296     (while (not (eq modified-tick (buffer-modified-tick)))
1297       (setq modified-tick (buffer-modified-tick))
1298       (cond
1299        ((listp gnus-simplify-subject-fuzzy-regexp)
1300         (mapcar 'gnus-simplify-buffer-fuzzy-step
1301                 gnus-simplify-subject-fuzzy-regexp))
1302        (gnus-simplify-subject-fuzzy-regexp
1303         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1304       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1305       (gnus-simplify-buffer-fuzzy-step
1306        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1307       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1308
1309     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1310     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1311     (gnus-simplify-buffer-fuzzy-step " $")
1312     (gnus-simplify-buffer-fuzzy-step "^ +")))
1313
1314 (defun gnus-simplify-subject-fuzzy (subject)
1315   "Simplify a subject string fuzzily.
1316 See `gnus-simplify-buffer-fuzzy' for details."
1317   (save-excursion
1318     (gnus-set-work-buffer)
1319     (let ((case-fold-search t))
1320       ;; Remove uninteresting prefixes.
1321       (when (and gnus-simplify-ignored-prefixes
1322                  (string-match gnus-simplify-ignored-prefixes subject))
1323         (setq subject (substring subject (match-end 0))))
1324       (insert subject)
1325       (inline (gnus-simplify-buffer-fuzzy))
1326       (buffer-string))))
1327
1328 (defsubst gnus-simplify-subject-fully (subject)
1329   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1330   (cond
1331    (gnus-simplify-subject-functions
1332     (gnus-map-function gnus-simplify-subject-functions subject))
1333    ((null gnus-summary-gather-subject-limit)
1334     (gnus-simplify-subject-re subject))
1335    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1336     (gnus-simplify-subject-fuzzy subject))
1337    ((numberp gnus-summary-gather-subject-limit)
1338     (gnus-limit-string (gnus-simplify-subject-re subject)
1339                        gnus-summary-gather-subject-limit))
1340    (t
1341     subject)))
1342
1343 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1344   "Check whether two subjects are equal.
1345 If optional argument simple-first is t, first argument is already
1346 simplified."
1347   (cond
1348    ((null simple-first)
1349     (equal (gnus-simplify-subject-fully s1)
1350            (gnus-simplify-subject-fully s2)))
1351    (t
1352     (equal s1
1353            (gnus-simplify-subject-fully s2)))))
1354
1355 (defun gnus-summary-bubble-group ()
1356   "Increase the score of the current group.
1357 This is a handy function to add to `gnus-summary-exit-hook' to
1358 increase the score of each group you read."
1359   (gnus-group-add-score gnus-newsgroup-name))
1360
1361 \f
1362 ;;;
1363 ;;; Gnus summary mode
1364 ;;;
1365
1366 (put 'gnus-summary-mode 'mode-class 'special)
1367
1368 (when t
1369   ;; Non-orthogonal keys
1370
1371   (gnus-define-keys gnus-summary-mode-map
1372     " " gnus-summary-next-page
1373     "\177" gnus-summary-prev-page
1374     [delete] gnus-summary-prev-page
1375     [backspace] gnus-summary-prev-page
1376     "\r" gnus-summary-scroll-up
1377     "\M-\r" gnus-summary-scroll-down
1378     "n" gnus-summary-next-unread-article
1379     "p" gnus-summary-prev-unread-article
1380     "N" gnus-summary-next-article
1381     "P" gnus-summary-prev-article
1382     "\M-\C-n" gnus-summary-next-same-subject
1383     "\M-\C-p" gnus-summary-prev-same-subject
1384     "\M-n" gnus-summary-next-unread-subject
1385     "\M-p" gnus-summary-prev-unread-subject
1386     "." gnus-summary-first-unread-article
1387     "," gnus-summary-best-unread-article
1388     "\M-s" gnus-summary-search-article-forward
1389     "\M-r" gnus-summary-search-article-backward
1390     "<" gnus-summary-beginning-of-article
1391     ">" gnus-summary-end-of-article
1392     "j" gnus-summary-goto-article
1393     "^" gnus-summary-refer-parent-article
1394     "\M-^" gnus-summary-refer-article
1395     "u" gnus-summary-tick-article-forward
1396     "!" gnus-summary-tick-article-forward
1397     "U" gnus-summary-tick-article-backward
1398     "d" gnus-summary-mark-as-read-forward
1399     "D" gnus-summary-mark-as-read-backward
1400     "E" gnus-summary-mark-as-expirable
1401     "\M-u" gnus-summary-clear-mark-forward
1402     "\M-U" gnus-summary-clear-mark-backward
1403     "k" gnus-summary-kill-same-subject-and-select
1404     "\C-k" gnus-summary-kill-same-subject
1405     "\M-\C-k" gnus-summary-kill-thread
1406     "\M-\C-l" gnus-summary-lower-thread
1407     "e" gnus-summary-edit-article
1408     "#" gnus-summary-mark-as-processable
1409     "\M-#" gnus-summary-unmark-as-processable
1410     "\M-\C-t" gnus-summary-toggle-threads
1411     "\M-\C-s" gnus-summary-show-thread
1412     "\M-\C-h" gnus-summary-hide-thread
1413     "\M-\C-f" gnus-summary-next-thread
1414     "\M-\C-b" gnus-summary-prev-thread
1415     [(meta down)] gnus-summary-next-thread
1416     [(meta up)] gnus-summary-prev-thread
1417     "\M-\C-u" gnus-summary-up-thread
1418     "\M-\C-d" gnus-summary-down-thread
1419     "&" gnus-summary-execute-command
1420     "c" gnus-summary-catchup-and-exit
1421     "\C-w" gnus-summary-mark-region-as-read
1422     "\C-t" gnus-summary-toggle-truncation
1423     "?" gnus-summary-mark-as-dormant
1424     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1425     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1426     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1427     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1428     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1429     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1430     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1431     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1432     "=" gnus-summary-expand-window
1433     "\C-x\C-s" gnus-summary-reselect-current-group
1434     "\M-g" gnus-summary-rescan-group
1435     "w" gnus-summary-stop-page-breaking
1436     "\C-c\C-r" gnus-summary-caesar-message
1437     "\M-t" gnus-summary-toggle-mime
1438     "f" gnus-summary-followup
1439     "F" gnus-summary-followup-with-original
1440     "C" gnus-summary-cancel-article
1441     "r" gnus-summary-reply
1442     "R" gnus-summary-reply-with-original
1443     "\C-c\C-f" gnus-summary-mail-forward
1444     "o" gnus-summary-save-article
1445     "\C-o" gnus-summary-save-article-mail
1446     "|" gnus-summary-pipe-output
1447     "\M-k" gnus-summary-edit-local-kill
1448     "\M-K" gnus-summary-edit-global-kill
1449     ;; "V" gnus-version
1450     "\C-c\C-d" gnus-summary-describe-group
1451     "q" gnus-summary-exit
1452     "Q" gnus-summary-exit-no-update
1453     "\C-c\C-i" gnus-info-find-node
1454     gnus-mouse-2 gnus-mouse-pick-article
1455     "m" gnus-summary-mail-other-window
1456     "a" gnus-summary-post-news
1457     "x" gnus-summary-limit-to-unread
1458     "s" gnus-summary-isearch-article
1459     "t" gnus-article-toggle-headers
1460     "g" gnus-summary-show-article
1461     "l" gnus-summary-goto-last-article
1462     "v" gnus-summary-preview-mime-message
1463     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1464     "\C-d" gnus-summary-enter-digest-group
1465     "\M-\C-d" gnus-summary-read-document
1466     "\M-\C-e" gnus-summary-edit-parameters
1467     "\M-\C-a" gnus-summary-customize-parameters
1468     "\C-c\C-b" gnus-bug
1469     "*" gnus-cache-enter-article
1470     "\M-*" gnus-cache-remove-article
1471     "\M-&" gnus-summary-universal-argument
1472     "\C-l" gnus-recenter
1473     "I" gnus-summary-increase-score
1474     "L" gnus-summary-lower-score
1475     "\M-i" gnus-symbolic-argument
1476     "h" gnus-summary-select-article-buffer
1477
1478     "V" gnus-summary-score-map
1479     "X" gnus-uu-extract-map
1480     "S" gnus-summary-send-map)
1481
1482   ;; Sort of orthogonal keymap
1483   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1484     "t" gnus-summary-tick-article-forward
1485     "!" gnus-summary-tick-article-forward
1486     "d" gnus-summary-mark-as-read-forward
1487     "r" gnus-summary-mark-as-read-forward
1488     "c" gnus-summary-clear-mark-forward
1489     " " gnus-summary-clear-mark-forward
1490     "e" gnus-summary-mark-as-expirable
1491     "x" gnus-summary-mark-as-expirable
1492     "?" gnus-summary-mark-as-dormant
1493     "b" gnus-summary-set-bookmark
1494     "B" gnus-summary-remove-bookmark
1495     "#" gnus-summary-mark-as-processable
1496     "\M-#" gnus-summary-unmark-as-processable
1497     "S" gnus-summary-limit-include-expunged
1498     "C" gnus-summary-catchup
1499     "H" gnus-summary-catchup-to-here
1500     "\C-c" gnus-summary-catchup-all
1501     "k" gnus-summary-kill-same-subject-and-select
1502     "K" gnus-summary-kill-same-subject
1503     "P" gnus-uu-mark-map)
1504
1505   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1506     "c" gnus-summary-clear-above
1507     "u" gnus-summary-tick-above
1508     "m" gnus-summary-mark-above
1509     "k" gnus-summary-kill-below)
1510
1511   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1512     "/" gnus-summary-limit-to-subject
1513     "n" gnus-summary-limit-to-articles
1514     "w" gnus-summary-pop-limit
1515     "s" gnus-summary-limit-to-subject
1516     "a" gnus-summary-limit-to-author
1517     "u" gnus-summary-limit-to-unread
1518     "m" gnus-summary-limit-to-marks
1519     "M" gnus-summary-limit-exclude-marks
1520     "v" gnus-summary-limit-to-score
1521     "*" gnus-summary-limit-include-cached
1522     "D" gnus-summary-limit-include-dormant
1523     "T" gnus-summary-limit-include-thread
1524     "d" gnus-summary-limit-exclude-dormant
1525     "t" gnus-summary-limit-to-age
1526     "x" gnus-summary-limit-to-extra
1527     "E" gnus-summary-limit-include-expunged
1528     "c" gnus-summary-limit-exclude-childless-dormant
1529     "C" gnus-summary-limit-mark-excluded-as-read)
1530
1531   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1532     "n" gnus-summary-next-unread-article
1533     "p" gnus-summary-prev-unread-article
1534     "N" gnus-summary-next-article
1535     "P" gnus-summary-prev-article
1536     "\C-n" gnus-summary-next-same-subject
1537     "\C-p" gnus-summary-prev-same-subject
1538     "\M-n" gnus-summary-next-unread-subject
1539     "\M-p" gnus-summary-prev-unread-subject
1540     "f" gnus-summary-first-unread-article
1541     "b" gnus-summary-best-unread-article
1542     "j" gnus-summary-goto-article
1543     "g" gnus-summary-goto-subject
1544     "l" gnus-summary-goto-last-article
1545     "o" gnus-summary-pop-article)
1546
1547   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1548     "k" gnus-summary-kill-thread
1549     "l" gnus-summary-lower-thread
1550     "i" gnus-summary-raise-thread
1551     "T" gnus-summary-toggle-threads
1552     "t" gnus-summary-rethread-current
1553     "^" gnus-summary-reparent-thread
1554     "s" gnus-summary-show-thread
1555     "S" gnus-summary-show-all-threads
1556     "h" gnus-summary-hide-thread
1557     "H" gnus-summary-hide-all-threads
1558     "n" gnus-summary-next-thread
1559     "p" gnus-summary-prev-thread
1560     "u" gnus-summary-up-thread
1561     "o" gnus-summary-top-thread
1562     "d" gnus-summary-down-thread
1563     "#" gnus-uu-mark-thread
1564     "\M-#" gnus-uu-unmark-thread)
1565
1566   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1567     "g" gnus-summary-prepare
1568     "c" gnus-summary-insert-cached-articles)
1569
1570   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1571     "c" gnus-summary-catchup-and-exit
1572     "C" gnus-summary-catchup-all-and-exit
1573     "E" gnus-summary-exit-no-update
1574     "J" gnus-summary-jump-to-other-group
1575     "Q" gnus-summary-exit
1576     "Z" gnus-summary-exit
1577     "n" gnus-summary-catchup-and-goto-next-group
1578     "R" gnus-summary-reselect-current-group
1579     "G" gnus-summary-rescan-group
1580     "N" gnus-summary-next-group
1581     "s" gnus-summary-save-newsrc
1582     "P" gnus-summary-prev-group)
1583
1584   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1585     " " gnus-summary-next-page
1586     "n" gnus-summary-next-page
1587     "\177" gnus-summary-prev-page
1588     [delete] gnus-summary-prev-page
1589     "p" gnus-summary-prev-page
1590     "\r" gnus-summary-scroll-up
1591     "\M-\r" gnus-summary-scroll-down
1592     "<" gnus-summary-beginning-of-article
1593     ">" gnus-summary-end-of-article
1594     "b" gnus-summary-beginning-of-article
1595     "e" gnus-summary-end-of-article
1596     "^" gnus-summary-refer-parent-article
1597     "r" gnus-summary-refer-parent-article
1598     "D" gnus-summary-enter-digest-group
1599     "R" gnus-summary-refer-references
1600     "T" gnus-summary-refer-thread
1601     "g" gnus-summary-show-article
1602     "s" gnus-summary-isearch-article
1603     "P" gnus-summary-print-article
1604     "M" gnus-mailing-list-insinuate
1605     "t" gnus-article-babel)
1606
1607   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1608     "b" gnus-article-add-buttons
1609     "B" gnus-article-add-buttons-to-head
1610     "o" gnus-article-treat-overstrike
1611     "e" gnus-article-emphasize
1612     "w" gnus-article-fill-cited-article
1613     "Q" gnus-article-fill-long-lines
1614     "C" gnus-article-capitalize-sentences
1615     "c" gnus-article-remove-cr
1616     "Z" gnus-article-decode-HZ
1617     "f" gnus-article-display-x-face
1618     "l" gnus-summary-stop-page-breaking
1619     "r" gnus-summary-caesar-message
1620     "t" gnus-article-toggle-headers
1621     "v" gnus-summary-verbose-headers
1622     "m" gnus-summary-toggle-mime
1623     "H" gnus-article-strip-headers-in-body
1624     "p" gnus-article-verify-x-pgp-sig
1625     "d" gnus-article-treat-dumbquotes
1626     "s" gnus-smiley-display)
1627
1628   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1629     "a" gnus-article-hide
1630     "h" gnus-article-toggle-headers
1631     "b" gnus-article-hide-boring-headers
1632     "s" gnus-article-hide-signature
1633     "c" gnus-article-hide-citation
1634     "C" gnus-article-hide-citation-in-followups
1635     "l" gnus-article-hide-list-identifiers
1636     "p" gnus-article-hide-pgp
1637     "B" gnus-article-strip-banner
1638     "P" gnus-article-hide-pem
1639     "\C-c" gnus-article-hide-citation-maybe)
1640
1641   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1642     "a" gnus-article-highlight
1643     "h" gnus-article-highlight-headers
1644     "c" gnus-article-highlight-citation
1645     "s" gnus-article-highlight-signature)
1646
1647   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1648     "z" gnus-article-date-ut
1649     "u" gnus-article-date-ut
1650     "l" gnus-article-date-local
1651     "p" gnus-article-date-english
1652     "e" gnus-article-date-lapsed
1653     "o" gnus-article-date-original
1654     "i" gnus-article-date-iso8601
1655     "s" gnus-article-date-user)
1656
1657   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1658     "t" gnus-article-remove-trailing-blank-lines
1659     "l" gnus-article-strip-leading-blank-lines
1660     "m" gnus-article-strip-multiple-blank-lines
1661     "a" gnus-article-strip-blank-lines
1662     "A" gnus-article-strip-all-blank-lines
1663     "s" gnus-article-strip-leading-space
1664     "e" gnus-article-strip-trailing-space)
1665
1666   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1667     "v" gnus-version
1668     "f" gnus-summary-fetch-faq
1669     "d" gnus-summary-describe-group
1670     "h" gnus-summary-describe-briefly
1671     "i" gnus-info-find-node)
1672
1673   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1674     "e" gnus-summary-expire-articles
1675     "\M-\C-e" gnus-summary-expire-articles-now
1676     "\177" gnus-summary-delete-article
1677     [delete] gnus-summary-delete-article
1678     [backspace] gnus-summary-delete-article
1679     "m" gnus-summary-move-article
1680     "r" gnus-summary-respool-article
1681     "w" gnus-summary-edit-article
1682     "c" gnus-summary-copy-article
1683     "B" gnus-summary-crosspost-article
1684     "q" gnus-summary-respool-query
1685     "t" gnus-summary-respool-trace
1686     "i" gnus-summary-import-article
1687     "p" gnus-summary-article-posted-p)
1688
1689   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1690     "o" gnus-summary-save-article
1691     "m" gnus-summary-save-article-mail
1692     "F" gnus-summary-write-article-file
1693     "r" gnus-summary-save-article-rmail
1694     "f" gnus-summary-save-article-file
1695     "b" gnus-summary-save-article-body-file
1696     "h" gnus-summary-save-article-folder
1697     "v" gnus-summary-save-article-vm
1698     "p" gnus-summary-pipe-output
1699     "s" gnus-soup-add-article)
1700
1701   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1702     "b" gnus-summary-display-buttonized
1703     "m" gnus-summary-repair-multipart
1704     "v" gnus-article-view-part
1705     "o" gnus-article-save-part
1706     "c" gnus-article-copy-part
1707     "C" gnus-article-view-part-as-charset
1708     "e" gnus-article-externalize-part
1709     "E" gnus-article-encrypt-body
1710     "i" gnus-article-inline-part
1711     "|" gnus-article-pipe-part))
1712
1713 (defun gnus-summary-make-menu-bar ()
1714   (gnus-turn-off-edit-menu 'summary)
1715
1716   (unless (boundp 'gnus-summary-misc-menu)
1717
1718     (easy-menu-define
1719      gnus-summary-kill-menu gnus-summary-mode-map ""
1720      (cons
1721       "Score"
1722       (nconc
1723        (list
1724         ["Customize" gnus-score-customize t])
1725        (gnus-make-score-map 'increase)
1726        (gnus-make-score-map 'lower)
1727        '(("Mark"
1728           ["Kill below" gnus-summary-kill-below t]
1729           ["Mark above" gnus-summary-mark-above t]
1730           ["Tick above" gnus-summary-tick-above t]
1731           ["Clear above" gnus-summary-clear-above t])
1732          ["Current score" gnus-summary-current-score t]
1733          ["Set score" gnus-summary-set-score t]
1734          ["Switch current score file..." gnus-score-change-score-file t]
1735          ["Set mark below..." gnus-score-set-mark-below t]
1736          ["Set expunge below..." gnus-score-set-expunge-below t]
1737          ["Edit current score file" gnus-score-edit-current-scores t]
1738          ["Edit score file" gnus-score-edit-file t]
1739          ["Trace score" gnus-score-find-trace t]
1740          ["Find words" gnus-score-find-favourite-words t]
1741          ["Rescore buffer" gnus-summary-rescore t]
1742          ["Increase score..." gnus-summary-increase-score t]
1743          ["Lower score..." gnus-summary-lower-score t]))))
1744
1745     ;; Define both the Article menu in the summary buffer and the equivalent
1746     ;; Commands menu in the article buffer here for consistency.
1747     (let ((innards
1748            `(("Hide"
1749               ["All" gnus-article-hide t]
1750               ["Headers" gnus-article-toggle-headers t]
1751               ["Signature" gnus-article-hide-signature t]
1752               ["Citation" gnus-article-hide-citation t]
1753               ["List identifiers" gnus-article-hide-list-identifiers t]
1754               ["PGP" gnus-article-hide-pgp t]
1755               ["Banner" gnus-article-strip-banner t]
1756               ["Boring headers" gnus-article-hide-boring-headers t])
1757              ("Highlight"
1758               ["All" gnus-article-highlight t]
1759               ["Headers" gnus-article-highlight-headers t]
1760               ["Signature" gnus-article-highlight-signature t]
1761               ["Citation" gnus-article-highlight-citation t])
1762              ("Date"
1763               ["Local" gnus-article-date-local t]
1764               ["ISO8601" gnus-article-date-iso8601 t]
1765               ["UT" gnus-article-date-ut t]
1766               ["Original" gnus-article-date-original t]
1767               ["Lapsed" gnus-article-date-lapsed t]
1768               ["User-defined" gnus-article-date-user t])
1769              ("Washing"
1770               ("Remove Blanks"
1771                ["Leading" gnus-article-strip-leading-blank-lines t]
1772                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1773                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1774                ["All of the above" gnus-article-strip-blank-lines t]
1775                ["All" gnus-article-strip-all-blank-lines t]
1776                ["Leading space" gnus-article-strip-leading-space t]
1777                ["Trailing space" gnus-article-strip-trailing-space t])
1778               ["Overstrike" gnus-article-treat-overstrike t]
1779               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1780               ["Emphasis" gnus-article-emphasize t]
1781               ["Word wrap" gnus-article-fill-cited-article t]
1782               ["Fill long lines" gnus-article-fill-long-lines t]
1783               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1784               ["CR" gnus-article-remove-cr t]
1785               ["Show X-Face" gnus-article-display-x-face t]
1786               ["Rot 13" gnus-summary-caesar-message
1787                ,@(if (featurep 'xemacs) nil
1788                    '(:help "\"Caesar rotate\" article by 13"))]
1789               ["Unix pipe" gnus-summary-pipe-message t]
1790               ["Add buttons" gnus-article-add-buttons t]
1791               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1792               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1793               ["Toggle MIME" gnus-summary-toggle-mime t]
1794               ["Verbose header" gnus-summary-verbose-headers t]
1795               ["Toggle header" gnus-summary-toggle-header t]
1796               ["Toggle smileys" gnus-smiley-display t]
1797               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1798               ["HZ" gnus-article-decode-HZ t])
1799              ("Output"
1800               ["Save in default format" gnus-summary-save-article
1801                ,@(if (featurep 'xemacs) nil
1802                    '(:help "Save article using default method"))]
1803               ["Save in file" gnus-summary-save-article-file
1804                ,@(if (featurep 'xemacs) nil
1805                    '(:help "Save article in file"))]
1806               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1807               ["Save in MH folder" gnus-summary-save-article-folder t]
1808               ["Save in VM folder" gnus-summary-save-article-vm t]
1809               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1810               ["Save body in file" gnus-summary-save-article-body-file t]
1811               ["Pipe through a filter" gnus-summary-pipe-output t]
1812               ["Add to SOUP packet" gnus-soup-add-article t]
1813               ["Print" gnus-summary-print-article t])
1814              ("Backend"
1815               ["Respool article..." gnus-summary-respool-article t]
1816               ["Move article..." gnus-summary-move-article
1817                (gnus-check-backend-function
1818                 'request-move-article gnus-newsgroup-name)]
1819               ["Copy article..." gnus-summary-copy-article t]
1820               ["Crosspost article..." gnus-summary-crosspost-article
1821                (gnus-check-backend-function
1822                 'request-replace-article gnus-newsgroup-name)]
1823               ["Import file..." gnus-summary-import-article t]
1824               ["Check if posted" gnus-summary-article-posted-p t]
1825               ["Edit article" gnus-summary-edit-article
1826                (not (gnus-group-read-only-p))]
1827               ["Delete article" gnus-summary-delete-article
1828                (gnus-check-backend-function
1829                 'request-expire-articles gnus-newsgroup-name)]
1830               ["Query respool" gnus-summary-respool-query t]
1831               ["Trace respool" gnus-summary-respool-trace t]
1832               ["Delete expirable articles" gnus-summary-expire-articles-now
1833                (gnus-check-backend-function
1834                 'request-expire-articles gnus-newsgroup-name)])
1835              ("Extract"
1836               ["Uudecode" gnus-uu-decode-uu
1837                ,@(if (featurep 'xemacs) nil
1838                    '(:help "Decode uuencoded article(s)"))]
1839               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1840               ["Unshar" gnus-uu-decode-unshar t]
1841               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1842               ["Save" gnus-uu-decode-save t]
1843               ["Binhex" gnus-uu-decode-binhex t]
1844               ["Postscript" gnus-uu-decode-postscript t])
1845              ("Cache"
1846               ["Enter article" gnus-cache-enter-article t]
1847               ["Remove article" gnus-cache-remove-article t])
1848              ["Translate" gnus-article-babel t]
1849              ["Select article buffer" gnus-summary-select-article-buffer t]
1850              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1851              ["Isearch article..." gnus-summary-isearch-article t]
1852              ["Beginning of the article" gnus-summary-beginning-of-article t]
1853              ["End of the article" gnus-summary-end-of-article t]
1854              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1855              ["Fetch referenced articles" gnus-summary-refer-references t]
1856              ["Fetch current thread" gnus-summary-refer-thread t]
1857              ["Fetch article with id..." gnus-summary-refer-article t]
1858              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1859              ["Redisplay" gnus-summary-show-article t])))
1860       (easy-menu-define
1861        gnus-summary-article-menu gnus-summary-mode-map ""
1862        (cons "Article" innards))
1863
1864       (easy-menu-define
1865        gnus-article-commands-menu gnus-article-mode-map ""
1866        (cons "Commands" innards)))
1867
1868     (easy-menu-define
1869      gnus-summary-thread-menu gnus-summary-mode-map ""
1870      '("Threads"
1871        ["Toggle threading" gnus-summary-toggle-threads t]
1872        ["Hide threads" gnus-summary-hide-all-threads t]
1873        ["Show threads" gnus-summary-show-all-threads t]
1874        ["Hide thread" gnus-summary-hide-thread t]
1875        ["Show thread" gnus-summary-show-thread t]
1876        ["Go to next thread" gnus-summary-next-thread t]
1877        ["Go to previous thread" gnus-summary-prev-thread t]
1878        ["Go down thread" gnus-summary-down-thread t]
1879        ["Go up thread" gnus-summary-up-thread t]
1880        ["Top of thread" gnus-summary-top-thread t]
1881        ["Mark thread as read" gnus-summary-kill-thread t]
1882        ["Lower thread score" gnus-summary-lower-thread t]
1883        ["Raise thread score" gnus-summary-raise-thread t]
1884        ["Rethread current" gnus-summary-rethread-current t]))
1885
1886     (easy-menu-define
1887      gnus-summary-post-menu gnus-summary-mode-map ""
1888      `("Post"
1889        ["Post an article" gnus-summary-post-news
1890         ,@(if (featurep 'xemacs) nil
1891             '(:help "Post an article"))]
1892        ["Followup" gnus-summary-followup
1893         ,@(if (featurep 'xemacs) nil
1894             '(:help "Post followup to this article"))]
1895        ["Followup and yank" gnus-summary-followup-with-original
1896         ,@(if (featurep 'xemacs) nil
1897             '(:help "Post followup to this article, quoting its contents"))]
1898        ["Supersede article" gnus-summary-supersede-article t]
1899        ["Cancel article" gnus-summary-cancel-article
1900         ,@(if (featurep 'xemacs) nil
1901             '(:help "Cancel an article you posted"))]
1902        ["Reply" gnus-summary-reply t]
1903        ["Reply and yank" gnus-summary-reply-with-original t]
1904        ["Wide reply" gnus-summary-wide-reply t]
1905        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1906         ,@(if (featurep 'xemacs) nil
1907             '(:help "Mail a reply, quoting this article"))]
1908        ["Mail forward" gnus-summary-mail-forward t]
1909        ["Post forward" gnus-summary-post-forward t]
1910        ["Digest and mail" gnus-uu-digest-mail-forward t]
1911        ["Digest and post" gnus-uu-digest-post-forward t]
1912        ["Resend message" gnus-summary-resend-message t]
1913        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1914        ["Send a mail" gnus-summary-mail-other-window t]
1915        ["Uuencode and post" gnus-uu-post-news
1916         ,@(if (featurep 'xemacs) nil
1917             '(:help "Post a uuencoded article"))]
1918        ["Followup via news" gnus-summary-followup-to-mail t]
1919        ["Followup via news and yank"
1920         gnus-summary-followup-to-mail-with-original t]
1921        ;;("Draft"
1922        ;;["Send" gnus-summary-send-draft t]
1923        ;;["Send bounced" gnus-resend-bounced-mail t])
1924        ))
1925
1926     (easy-menu-define
1927      gnus-summary-misc-menu gnus-summary-mode-map ""
1928      `("Misc"
1929        ("Mark Read"
1930         ["Mark as read" gnus-summary-mark-as-read-forward t]
1931         ["Mark same subject and select"
1932          gnus-summary-kill-same-subject-and-select t]
1933         ["Mark same subject" gnus-summary-kill-same-subject t]
1934         ["Catchup" gnus-summary-catchup
1935          ,@(if (featurep 'xemacs) nil
1936              '(:help "Mark unread articles in this group as read"))]
1937         ["Catchup all" gnus-summary-catchup-all t]
1938         ["Catchup to here" gnus-summary-catchup-to-here t]
1939         ["Catchup region" gnus-summary-mark-region-as-read t]
1940         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1941        ("Mark Various"
1942         ["Tick" gnus-summary-tick-article-forward t]
1943         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1944         ["Remove marks" gnus-summary-clear-mark-forward t]
1945         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1946         ["Set bookmark" gnus-summary-set-bookmark t]
1947         ["Remove bookmark" gnus-summary-remove-bookmark t])
1948        ("Mark Limit"
1949         ["Marks..." gnus-summary-limit-to-marks t]
1950         ["Subject..." gnus-summary-limit-to-subject t]
1951         ["Author..." gnus-summary-limit-to-author t]
1952         ["Age..." gnus-summary-limit-to-age t]
1953         ["Extra..." gnus-summary-limit-to-extra t]
1954         ["Score" gnus-summary-limit-to-score t]
1955         ["Unread" gnus-summary-limit-to-unread t]
1956         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1957         ["Articles" gnus-summary-limit-to-articles t]
1958         ["Pop limit" gnus-summary-pop-limit t]
1959         ["Show dormant" gnus-summary-limit-include-dormant t]
1960         ["Hide childless dormant"
1961          gnus-summary-limit-exclude-childless-dormant t]
1962         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1963         ["Hide marked" gnus-summary-limit-exclude-marks t]
1964         ["Show expunged" gnus-summary-show-all-expunged t])
1965        ("Process Mark"
1966         ["Set mark" gnus-summary-mark-as-processable t]
1967         ["Remove mark" gnus-summary-unmark-as-processable t]
1968         ["Remove all marks" gnus-summary-unmark-all-processable t]
1969         ["Mark above" gnus-uu-mark-over t]
1970         ["Mark series" gnus-uu-mark-series t]
1971         ["Mark region" gnus-uu-mark-region t]
1972         ["Unmark region" gnus-uu-unmark-region t]
1973         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1974         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1975         ["Mark all" gnus-uu-mark-all t]
1976         ["Mark buffer" gnus-uu-mark-buffer t]
1977         ["Mark sparse" gnus-uu-mark-sparse t]
1978         ["Mark thread" gnus-uu-mark-thread t]
1979         ["Unmark thread" gnus-uu-unmark-thread t]
1980         ("Process Mark Sets"
1981          ["Kill" gnus-summary-kill-process-mark t]
1982          ["Yank" gnus-summary-yank-process-mark
1983           gnus-newsgroup-process-stack]
1984          ["Save" gnus-summary-save-process-mark t]))
1985        ("Scroll article"
1986         ["Page forward" gnus-summary-next-page
1987          ,@(if (featurep 'xemacs) nil
1988              '(:help "Show next page of article"))]
1989         ["Page backward" gnus-summary-prev-page
1990          ,@(if (featurep 'xemacs) nil
1991              '(:help "Show previous page of article"))]
1992         ["Line forward" gnus-summary-scroll-up t])
1993        ("Move"
1994         ["Next unread article" gnus-summary-next-unread-article t]
1995         ["Previous unread article" gnus-summary-prev-unread-article t]
1996         ["Next article" gnus-summary-next-article t]
1997         ["Previous article" gnus-summary-prev-article t]
1998         ["Next unread subject" gnus-summary-next-unread-subject t]
1999         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2000         ["Next article same subject" gnus-summary-next-same-subject t]
2001         ["Previous article same subject" gnus-summary-prev-same-subject t]
2002         ["First unread article" gnus-summary-first-unread-article t]
2003         ["Best unread article" gnus-summary-best-unread-article t]
2004         ["Go to subject number..." gnus-summary-goto-subject t]
2005         ["Go to article number..." gnus-summary-goto-article t]
2006         ["Go to the last article" gnus-summary-goto-last-article t]
2007         ["Pop article off history" gnus-summary-pop-article t])
2008        ("Sort"
2009         ["Sort by number" gnus-summary-sort-by-number t]
2010         ["Sort by author" gnus-summary-sort-by-author t]
2011         ["Sort by subject" gnus-summary-sort-by-subject t]
2012         ["Sort by date" gnus-summary-sort-by-date t]
2013         ["Sort by score" gnus-summary-sort-by-score t]
2014         ["Sort by lines" gnus-summary-sort-by-lines t]
2015         ["Sort by characters" gnus-summary-sort-by-chars t])
2016        ("Help"
2017         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2018         ["Describe group" gnus-summary-describe-group t]
2019         ["Read manual" gnus-info-find-node t])
2020        ("Modes"
2021         ["Pick and read" gnus-pick-mode t]
2022         ["Binary" gnus-binary-mode t])
2023        ("Regeneration"
2024         ["Regenerate" gnus-summary-prepare t]
2025         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2026         ["Toggle threading" gnus-summary-toggle-threads t])
2027        ["Filter articles..." gnus-summary-execute-command t]
2028        ["Run command on subjects..." gnus-summary-universal-argument t]
2029        ["Search articles forward..." gnus-summary-search-article-forward t]
2030        ["Search articles backward..." gnus-summary-search-article-backward t]
2031        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2032        ["Expand window" gnus-summary-expand-window t]
2033        ["Expire expirable articles" gnus-summary-expire-articles
2034         (gnus-check-backend-function
2035          'request-expire-articles gnus-newsgroup-name)]
2036        ["Edit local kill file" gnus-summary-edit-local-kill t]
2037        ["Edit main kill file" gnus-summary-edit-global-kill t]
2038        ["Edit group parameters" gnus-summary-edit-parameters t]
2039        ["Customize group parameters" gnus-summary-customize-parameters t]
2040        ["Send a bug report" gnus-bug t]
2041        ("Exit"
2042         ["Catchup and exit" gnus-summary-catchup-and-exit
2043          ,@(if (featurep 'xemacs) nil
2044              '(:help "Mark unread articles in this group as read, then exit"))]
2045         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2046         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2047         ["Exit group" gnus-summary-exit
2048          ,@(if (featurep 'xemacs) nil
2049              '(:help "Exit current group, return to group selection mode"))]
2050         ["Exit group without updating" gnus-summary-exit-no-update t]
2051         ["Exit and goto next group" gnus-summary-next-group t]
2052         ["Exit and goto prev group" gnus-summary-prev-group t]
2053         ["Reselect group" gnus-summary-reselect-current-group t]
2054         ["Rescan group" gnus-summary-rescan-group t]
2055         ["Update dribble" gnus-summary-save-newsrc t])))
2056
2057     (gnus-run-hooks 'gnus-summary-menu-hook)))
2058
2059 (defvar gnus-summary-tool-bar-map nil)
2060
2061 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2062 (defun gnus-summary-make-tool-bar ()
2063   (if (and (fboundp 'tool-bar-add-item-from-menu)
2064            (default-value 'tool-bar-mode)
2065            (not gnus-summary-tool-bar-map))
2066       (setq gnus-summary-tool-bar-map
2067             (let ((tool-bar-map (make-sparse-keymap))
2068                   (load-path (mm-image-load-path)))
2069               (tool-bar-add-item-from-menu
2070                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2071               (tool-bar-add-item-from-menu
2072                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2073               (tool-bar-add-item-from-menu
2074                'gnus-summary-post-news "post" gnus-summary-mode-map)
2075               (tool-bar-add-item-from-menu
2076                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2077               (tool-bar-add-item-from-menu
2078                'gnus-summary-followup "followup" gnus-summary-mode-map)
2079               (tool-bar-add-item-from-menu
2080                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2081               (tool-bar-add-item-from-menu
2082                'gnus-summary-reply "reply" gnus-summary-mode-map)
2083               (tool-bar-add-item-from-menu
2084                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2085               (tool-bar-add-item-from-menu
2086                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2087               (tool-bar-add-item-from-menu
2088                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2089               (tool-bar-add-item-from-menu
2090                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2091               (tool-bar-add-item-from-menu
2092                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2093               (tool-bar-add-item-from-menu
2094                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2095               (tool-bar-add-item-from-menu
2096                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2097               (tool-bar-add-item-from-menu
2098                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2099               tool-bar-map)))
2100   (if gnus-summary-tool-bar-map
2101       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2102
2103 (defun gnus-score-set-default (var value)
2104   "A version of set that updates the GNU Emacs menu-bar."
2105   (set var value)
2106   ;; It is the message that forces the active status to be updated.
2107   (message ""))
2108
2109 (defun gnus-make-score-map (type)
2110   "Make a summary score map of type TYPE."
2111   (if t
2112       nil
2113     (let ((headers '(("author" "from" string)
2114                      ("subject" "subject" string)
2115                      ("article body" "body" string)
2116                      ("article head" "head" string)
2117                      ("xref" "xref" string)
2118                      ("extra header" "extra" string)
2119                      ("lines" "lines" number)
2120                      ("followups to author" "followup" string)))
2121           (types '((number ("less than" <)
2122                            ("greater than" >)
2123                            ("equal" =))
2124                    (string ("substring" s)
2125                            ("exact string" e)
2126                            ("fuzzy string" f)
2127                            ("regexp" r))))
2128           (perms '(("temporary" (current-time-string))
2129                    ("permanent" nil)
2130                    ("immediate" now)))
2131           header)
2132       (list
2133        (apply
2134         'nconc
2135         (list
2136          (if (eq type 'lower)
2137              "Lower score"
2138            "Increase score"))
2139         (let (outh)
2140           (while headers
2141             (setq header (car headers))
2142             (setq outh
2143                   (cons
2144                    (apply
2145                     'nconc
2146                     (list (car header))
2147                     (let ((ts (cdr (assoc (nth 2 header) types)))
2148                           outt)
2149                       (while ts
2150                         (setq outt
2151                               (cons
2152                                (apply
2153                                 'nconc
2154                                 (list (caar ts))
2155                                 (let ((ps perms)
2156                                       outp)
2157                                   (while ps
2158                                     (setq outp
2159                                           (cons
2160                                            (vector
2161                                             (caar ps)
2162                                             (list
2163                                              'gnus-summary-score-entry
2164                                              (nth 1 header)
2165                                              (if (or (string= (nth 1 header)
2166                                                               "head")
2167                                                      (string= (nth 1 header)
2168                                                               "body"))
2169                                                  ""
2170                                                (list 'gnus-summary-header
2171                                                      (nth 1 header)))
2172                                              (list 'quote (nth 1 (car ts)))
2173                                              (list 'gnus-score-delta-default
2174                                                    nil)
2175                                              (nth 1 (car ps))
2176                                              t)
2177                                             t)
2178                                            outp))
2179                                     (setq ps (cdr ps)))
2180                                   (list (nreverse outp))))
2181                                outt))
2182                         (setq ts (cdr ts)))
2183                       (list (nreverse outt))))
2184                    outh))
2185             (setq headers (cdr headers)))
2186           (list (nreverse outh))))))))
2187
2188 \f
2189
2190 (defun gnus-summary-mode (&optional group)
2191   "Major mode for reading articles.
2192
2193 All normal editing commands are switched off.
2194 \\<gnus-summary-mode-map>
2195 Each line in this buffer represents one article.  To read an
2196 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2197 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2198 respectively.
2199
2200 You can also post articles and send mail from this buffer.  To
2201 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2202 of an article, type `\\[gnus-summary-reply]'.
2203
2204 There are approx. one gazillion commands you can execute in this
2205 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2206
2207 The following commands are available:
2208
2209 \\{gnus-summary-mode-map}"
2210   (interactive)
2211   (kill-all-local-variables)
2212   (when (gnus-visual-p 'summary-menu 'menu)
2213     (gnus-summary-make-menu-bar)
2214     (gnus-summary-make-tool-bar))
2215   (gnus-summary-make-local-variables)
2216   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2217     (gnus-summary-make-local-variables))
2218   (gnus-make-thread-indent-array)
2219   (gnus-simplify-mode-line)
2220   (setq major-mode 'gnus-summary-mode)
2221   (setq mode-name "Summary")
2222   (make-local-variable 'minor-mode-alist)
2223   (use-local-map gnus-summary-mode-map)
2224   (buffer-disable-undo)
2225   (setq buffer-read-only t)             ;Disable modification
2226   (setq truncate-lines t)
2227   (setq selective-display t)
2228   (setq selective-display-ellipses t)   ;Display `...'
2229   (gnus-summary-set-display-table)
2230   (gnus-set-default-directory)
2231   (setq gnus-newsgroup-name group)
2232   (unless (gnus-news-group-p group)
2233     (setq gnus-newsgroup-incorporated
2234           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2235   (make-local-variable 'gnus-summary-line-format)
2236   (make-local-variable 'gnus-summary-line-format-spec)
2237   (make-local-variable 'gnus-summary-dummy-line-format)
2238   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2239   (make-local-variable 'gnus-summary-mark-positions)
2240   (make-local-hook 'pre-command-hook)
2241   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2242   (gnus-run-hooks 'gnus-summary-mode-hook)
2243   (turn-on-gnus-mailing-list-mode)
2244   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2245   (gnus-update-summary-mark-positions))
2246
2247 (defun gnus-summary-make-local-variables ()
2248   "Make all the local summary buffer variables."
2249   (let (global)
2250     (dolist (local gnus-summary-local-variables)
2251       (if (consp local)
2252           (progn
2253             (if (eq (cdr local) 'global)
2254                 ;; Copy the global value of the variable.
2255                 (setq global (symbol-value (car local)))
2256               ;; Use the value from the list.
2257               (setq global (eval (cdr local))))
2258             (set (make-local-variable (car local)) global))
2259         ;; Simple nil-valued local variable.
2260         (set (make-local-variable local) nil)))))
2261
2262 (defun gnus-summary-clear-local-variables ()
2263   (let ((locals gnus-summary-local-variables))
2264     (while locals
2265       (if (consp (car locals))
2266           (and (vectorp (caar locals))
2267                (set (caar locals) nil))
2268         (and (vectorp (car locals))
2269              (set (car locals) nil)))
2270       (setq locals (cdr locals)))))
2271
2272 ;; Summary data functions.
2273
2274 (defmacro gnus-data-number (data)
2275   `(car ,data))
2276
2277 (defmacro gnus-data-set-number (data number)
2278   `(setcar ,data ,number))
2279
2280 (defmacro gnus-data-mark (data)
2281   `(nth 1 ,data))
2282
2283 (defmacro gnus-data-set-mark (data mark)
2284   `(setcar (nthcdr 1 ,data) ,mark))
2285
2286 (defmacro gnus-data-pos (data)
2287   `(nth 2 ,data))
2288
2289 (defmacro gnus-data-set-pos (data pos)
2290   `(setcar (nthcdr 2 ,data) ,pos))
2291
2292 (defmacro gnus-data-header (data)
2293   `(nth 3 ,data))
2294
2295 (defmacro gnus-data-set-header (data header)
2296   `(setcar (nthcdr 3 ,data) ,header))
2297
2298 (defmacro gnus-data-level (data)
2299   `(nth 4 ,data))
2300
2301 (defmacro gnus-data-unread-p (data)
2302   `(= (nth 1 ,data) gnus-unread-mark))
2303
2304 (defmacro gnus-data-read-p (data)
2305   `(/= (nth 1 ,data) gnus-unread-mark))
2306
2307 (defmacro gnus-data-pseudo-p (data)
2308   `(consp (nth 3 ,data)))
2309
2310 (defmacro gnus-data-find (number)
2311   `(assq ,number gnus-newsgroup-data))
2312
2313 (defmacro gnus-data-find-list (number &optional data)
2314   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2315      (memq (assq ,number bdata)
2316            bdata)))
2317
2318 (defmacro gnus-data-make (number mark pos header level)
2319   `(list ,number ,mark ,pos ,header ,level))
2320
2321 (defun gnus-data-enter (after-article number mark pos header level offset)
2322   (let ((data (gnus-data-find-list after-article)))
2323     (unless data
2324       (error "No such article: %d" after-article))
2325     (setcdr data (cons (gnus-data-make number mark pos header level)
2326                        (cdr data)))
2327     (setq gnus-newsgroup-data-reverse nil)
2328     (gnus-data-update-list (cddr data) offset)))
2329
2330 (defun gnus-data-enter-list (after-article list &optional offset)
2331   (when list
2332     (let ((data (and after-article (gnus-data-find-list after-article)))
2333           (ilist list))
2334       (if (not (or data
2335                    after-article))
2336           (let ((odata gnus-newsgroup-data))
2337             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2338             (when offset
2339               (gnus-data-update-list odata offset)))
2340         ;; Find the last element in the list to be spliced into the main
2341         ;; list.
2342         (while (cdr list)
2343           (setq list (cdr list)))
2344         (if (not data)
2345             (progn
2346               (setcdr list gnus-newsgroup-data)
2347               (setq gnus-newsgroup-data ilist)
2348               (when offset
2349                 (gnus-data-update-list (cdr list) offset)))
2350           (setcdr list (cdr data))
2351           (setcdr data ilist)
2352           (when offset
2353             (gnus-data-update-list (cdr list) offset))))
2354       (setq gnus-newsgroup-data-reverse nil))))
2355
2356 (defun gnus-data-remove (article &optional offset)
2357   (let ((data gnus-newsgroup-data))
2358     (if (= (gnus-data-number (car data)) article)
2359         (progn
2360           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2361                 gnus-newsgroup-data-reverse nil)
2362           (when offset
2363             (gnus-data-update-list gnus-newsgroup-data offset)))
2364       (while (cdr data)
2365         (when (= (gnus-data-number (cadr data)) article)
2366           (setcdr data (cddr data))
2367           (when offset
2368             (gnus-data-update-list (cdr data) offset))
2369           (setq data nil
2370                 gnus-newsgroup-data-reverse nil))
2371         (setq data (cdr data))))))
2372
2373 (defmacro gnus-data-list (backward)
2374   `(if ,backward
2375        (or gnus-newsgroup-data-reverse
2376            (setq gnus-newsgroup-data-reverse
2377                  (reverse gnus-newsgroup-data)))
2378      gnus-newsgroup-data))
2379
2380 (defun gnus-data-update-list (data offset)
2381   "Add OFFSET to the POS of all data entries in DATA."
2382   (setq gnus-newsgroup-data-reverse nil)
2383   (while data
2384     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2385     (setq data (cdr data))))
2386
2387 (defun gnus-summary-article-pseudo-p (article)
2388   "Say whether this article is a pseudo article or not."
2389   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2390
2391 (defmacro gnus-summary-article-sparse-p (article)
2392   "Say whether this article is a sparse article or not."
2393   `(memq ,article gnus-newsgroup-sparse))
2394
2395 (defmacro gnus-summary-article-ancient-p (article)
2396   "Say whether this article is a sparse article or not."
2397   `(memq ,article gnus-newsgroup-ancient))
2398
2399 (defun gnus-article-parent-p (number)
2400   "Say whether this article is a parent or not."
2401   (let ((data (gnus-data-find-list number)))
2402     (and (cdr data)                     ; There has to be an article after...
2403          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2404             (gnus-data-level (nth 1 data))))))
2405
2406 (defun gnus-article-children (number)
2407   "Return a list of all children to NUMBER."
2408   (let* ((data (gnus-data-find-list number))
2409          (level (gnus-data-level (car data)))
2410          children)
2411     (setq data (cdr data))
2412     (while (and data
2413                 (= (gnus-data-level (car data)) (1+ level)))
2414       (push (gnus-data-number (car data)) children)
2415       (setq data (cdr data)))
2416     children))
2417
2418 (defmacro gnus-summary-skip-intangible ()
2419   "If the current article is intangible, then jump to a different article."
2420   '(let ((to (get-text-property (point) 'gnus-intangible)))
2421      (and to (gnus-summary-goto-subject to))))
2422
2423 (defmacro gnus-summary-article-intangible-p ()
2424   "Say whether this article is intangible or not."
2425   '(get-text-property (point) 'gnus-intangible))
2426
2427 (defun gnus-article-read-p (article)
2428   "Say whether ARTICLE is read or not."
2429   (not (or (memq article gnus-newsgroup-marked)
2430            (memq article gnus-newsgroup-unreads)
2431            (memq article gnus-newsgroup-unselected)
2432            (memq article gnus-newsgroup-dormant))))
2433
2434 ;; Some summary mode macros.
2435
2436 (defmacro gnus-summary-article-number ()
2437   "The article number of the article on the current line.
2438 If there isn's an article number here, then we return the current
2439 article number."
2440   '(progn
2441      (gnus-summary-skip-intangible)
2442      (or (get-text-property (point) 'gnus-number)
2443          (gnus-summary-last-subject))))
2444
2445 (defmacro gnus-summary-article-header (&optional number)
2446   "Return the header of article NUMBER."
2447   `(gnus-data-header (gnus-data-find
2448                       ,(or number '(gnus-summary-article-number)))))
2449
2450 (defmacro gnus-summary-thread-level (&optional number)
2451   "Return the level of thread that starts with article NUMBER."
2452   `(if (and (eq gnus-summary-make-false-root 'dummy)
2453             (get-text-property (point) 'gnus-intangible))
2454        0
2455      (gnus-data-level (gnus-data-find
2456                        ,(or number '(gnus-summary-article-number))))))
2457
2458 (defmacro gnus-summary-article-mark (&optional number)
2459   "Return the mark of article NUMBER."
2460   `(gnus-data-mark (gnus-data-find
2461                     ,(or number '(gnus-summary-article-number)))))
2462
2463 (defmacro gnus-summary-article-pos (&optional number)
2464   "Return the position of the line of article NUMBER."
2465   `(gnus-data-pos (gnus-data-find
2466                    ,(or number '(gnus-summary-article-number)))))
2467
2468 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2469 (defmacro gnus-summary-article-subject (&optional number)
2470   "Return current subject string or nil if nothing."
2471   `(let ((headers
2472           ,(if number
2473                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2474              '(gnus-data-header (assq (gnus-summary-article-number)
2475                                       gnus-newsgroup-data)))))
2476      (and headers
2477           (vectorp headers)
2478           (mail-header-subject headers))))
2479
2480 (defmacro gnus-summary-article-score (&optional number)
2481   "Return current article score."
2482   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2483                   gnus-newsgroup-scored))
2484        gnus-summary-default-score 0))
2485
2486 (defun gnus-summary-article-children (&optional number)
2487   "Return a list of article numbers that are children of article NUMBER."
2488   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2489          (level (gnus-data-level (car data)))
2490          l children)
2491     (while (and (setq data (cdr data))
2492                 (> (setq l (gnus-data-level (car data))) level))
2493       (and (= (1+ level) l)
2494            (push (gnus-data-number (car data))
2495                  children)))
2496     (nreverse children)))
2497
2498 (defun gnus-summary-article-parent (&optional number)
2499   "Return the article number of the parent of article NUMBER."
2500   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2501                                     (gnus-data-list t)))
2502          (level (gnus-data-level (car data))))
2503     (if (zerop level)
2504         ()                              ; This is a root.
2505       ;; We search until we find an article with a level less than
2506       ;; this one.  That function has to be the parent.
2507       (while (and (setq data (cdr data))
2508                   (not (< (gnus-data-level (car data)) level))))
2509       (and data (gnus-data-number (car data))))))
2510
2511 (defun gnus-unread-mark-p (mark)
2512   "Say whether MARK is the unread mark."
2513   (= mark gnus-unread-mark))
2514
2515 (defun gnus-read-mark-p (mark)
2516   "Say whether MARK is one of the marks that mark as read.
2517 This is all marks except unread, ticked, dormant, and expirable."
2518   (not (or (= mark gnus-unread-mark)
2519            (= mark gnus-ticked-mark)
2520            (= mark gnus-dormant-mark)
2521            (= mark gnus-expirable-mark))))
2522
2523 (defmacro gnus-article-mark (number)
2524   "Return the MARK of article NUMBER.
2525 This macro should only be used when computing the mark the \"first\"
2526 time; i.e., when generating the summary lines.  After that,
2527 `gnus-summary-article-mark' should be used to examine the
2528 marks of articles."
2529   `(cond
2530     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2531     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2532     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2533     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2534     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2535     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2536     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2537     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2538            gnus-ancient-mark))))
2539
2540 ;; Saving hidden threads.
2541
2542 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2543 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2544
2545 (defmacro gnus-save-hidden-threads (&rest forms)
2546   "Save hidden threads, eval FORMS, and restore the hidden threads."
2547   (let ((config (make-symbol "config")))
2548     `(let ((,config (gnus-hidden-threads-configuration)))
2549        (unwind-protect
2550            (save-excursion
2551              ,@forms)
2552          (gnus-restore-hidden-threads-configuration ,config)))))
2553
2554 (defun gnus-data-compute-positions ()
2555   "Compute the positions of all articles."
2556   (setq gnus-newsgroup-data-reverse nil)
2557   (let ((data gnus-newsgroup-data))
2558     (save-excursion
2559       (gnus-save-hidden-threads
2560         (gnus-summary-show-all-threads)
2561         (goto-char (point-min))
2562         (while data
2563           (while (get-text-property (point) 'gnus-intangible)
2564             (forward-line 1))
2565           (gnus-data-set-pos (car data) (+ (point) 3))
2566           (setq data (cdr data))
2567           (forward-line 1))))))
2568
2569 (defun gnus-hidden-threads-configuration ()
2570   "Return the current hidden threads configuration."
2571   (save-excursion
2572     (let (config)
2573       (goto-char (point-min))
2574       (while (search-forward "\r" nil t)
2575         (push (1- (point)) config))
2576       config)))
2577
2578 (defun gnus-restore-hidden-threads-configuration (config)
2579   "Restore hidden threads configuration from CONFIG."
2580   (save-excursion
2581     (let (point buffer-read-only)
2582       (while (setq point (pop config))
2583         (when (and (< point (point-max))
2584                    (goto-char point)
2585                    (eq (char-after) ?\n))
2586           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2587
2588 ;; Various summary mode internalish functions.
2589
2590 (defun gnus-mouse-pick-article (e)
2591   (interactive "e")
2592   (mouse-set-point e)
2593   (gnus-summary-next-page nil t))
2594
2595 (defun gnus-summary-set-display-table ()
2596   "Change the display table.
2597 Odd characters have a tendency to mess
2598 up nicely formatted displays - we make all possible glyphs
2599 display only a single character."
2600
2601   ;; We start from the standard display table, if any.
2602   (let ((table (or (copy-sequence standard-display-table)
2603                    (make-display-table)))
2604         (i 32))
2605     ;; Nix out all the control chars...
2606     (while (>= (setq i (1- i)) 0)
2607       (aset table i [??]))
2608     ;; ... but not newline and cr, of course.  (cr is necessary for the
2609     ;; selective display).
2610     (aset table ?\n nil)
2611     (aset table ?\r nil)
2612     ;; We keep TAB as well.
2613     (aset table ?\t nil)
2614     ;; We nix out any glyphs over 126 that are not set already.
2615     (let ((i 256))
2616       (while (>= (setq i (1- i)) 127)
2617         ;; Only modify if the entry is nil.
2618         (unless (aref table i)
2619           (aset table i [??]))))
2620     (setq buffer-display-table table)))
2621
2622 (defun gnus-summary-buffer-name (group)
2623   "Return the summary buffer name of GROUP."
2624   (concat "*Summary " group "*"))
2625
2626 (defun gnus-summary-setup-buffer (group)
2627   "Initialize summary buffer."
2628   (let ((buffer (gnus-summary-buffer-name group)))
2629     (if (get-buffer buffer)
2630         (progn
2631           (set-buffer buffer)
2632           (setq gnus-summary-buffer (current-buffer))
2633           (not gnus-newsgroup-prepared))
2634       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2635       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2636       (gnus-summary-mode group)
2637       (when gnus-carpal
2638         (gnus-carpal-setup-buffer 'summary))
2639       (unless gnus-single-article-buffer
2640         (make-local-variable 'gnus-article-buffer)
2641         (make-local-variable 'gnus-article-current)
2642         (make-local-variable 'gnus-original-article-buffer))
2643       (setq gnus-newsgroup-name group)
2644       t)))
2645
2646 (defun gnus-set-global-variables ()
2647   "Set the global equivalents of the buffer-local variables.
2648 They are set to the latest values they had.  These reflect the summary
2649 buffer that was in action when the last article was fetched."
2650   (when (eq major-mode 'gnus-summary-mode)
2651     (setq gnus-summary-buffer (current-buffer))
2652     (let ((name gnus-newsgroup-name)
2653           (marked gnus-newsgroup-marked)
2654           (unread gnus-newsgroup-unreads)
2655           (headers gnus-current-headers)
2656           (data gnus-newsgroup-data)
2657           (summary gnus-summary-buffer)
2658           (article-buffer gnus-article-buffer)
2659           (original gnus-original-article-buffer)
2660           (gac gnus-article-current)
2661           (reffed gnus-reffed-article-number)
2662           (score-file gnus-current-score-file)
2663           (default-charset gnus-newsgroup-charset)
2664           vlist)
2665       (let ((locals gnus-newsgroup-variables))
2666         (while locals
2667           (if (consp (car locals))
2668               (push (eval (caar locals)) vlist)
2669             (push (eval (car locals)) vlist))
2670           (setq locals (cdr locals)))
2671         (setq vlist (nreverse vlist)))
2672       (save-excursion
2673         (set-buffer gnus-group-buffer)
2674         (setq gnus-newsgroup-name name
2675               gnus-newsgroup-marked marked
2676               gnus-newsgroup-unreads unread
2677               gnus-current-headers headers
2678               gnus-newsgroup-data data
2679               gnus-article-current gac
2680               gnus-summary-buffer summary
2681               gnus-article-buffer article-buffer
2682               gnus-original-article-buffer original
2683               gnus-reffed-article-number reffed
2684               gnus-current-score-file score-file
2685               gnus-newsgroup-charset default-charset)
2686         (let ((locals gnus-newsgroup-variables))
2687           (while locals
2688             (if (consp (car locals))
2689                 (set (caar locals) (pop vlist))
2690               (set (car locals) (pop vlist)))
2691             (setq locals (cdr locals))))
2692         ;; The article buffer also has local variables.
2693         (when (gnus-buffer-live-p gnus-article-buffer)
2694           (set-buffer gnus-article-buffer)
2695           (setq gnus-summary-buffer summary))))))
2696
2697 (defun gnus-summary-article-unread-p (article)
2698   "Say whether ARTICLE is unread or not."
2699   (memq article gnus-newsgroup-unreads))
2700
2701 (defun gnus-summary-first-article-p (&optional article)
2702   "Return whether ARTICLE is the first article in the buffer."
2703   (if (not (setq article (or article (gnus-summary-article-number))))
2704       nil
2705     (eq article (caar gnus-newsgroup-data))))
2706
2707 (defun gnus-summary-last-article-p (&optional article)
2708   "Return whether ARTICLE is the last article in the buffer."
2709   (if (not (setq article (or article (gnus-summary-article-number))))
2710       ;; All non-existent numbers are the last article.  :-)
2711       t
2712     (not (cdr (gnus-data-find-list article)))))
2713
2714 (defun gnus-make-thread-indent-array ()
2715   (let ((n 200))
2716     (unless (and gnus-thread-indent-array
2717                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2718       (setq gnus-thread-indent-array (make-vector 201 "")
2719             gnus-thread-indent-array-level gnus-thread-indent-level)
2720       (while (>= n 0)
2721         (aset gnus-thread-indent-array n
2722               (make-string (* n gnus-thread-indent-level) ? ))
2723         (setq n (1- n))))))
2724
2725 (defun gnus-update-summary-mark-positions ()
2726   "Compute where the summary marks are to go."
2727   (save-excursion
2728     (when (gnus-buffer-exists-p gnus-summary-buffer)
2729       (set-buffer gnus-summary-buffer))
2730     (let ((gnus-replied-mark 129)
2731           (gnus-score-below-mark 130)
2732           (gnus-score-over-mark 130)
2733           (gnus-download-mark 131)
2734           (spec gnus-summary-line-format-spec)
2735           gnus-visual pos)
2736       (save-excursion
2737         (gnus-set-work-buffer)
2738         (let ((gnus-summary-line-format-spec spec)
2739               (gnus-newsgroup-downloadable '((0 . t))))
2740           (gnus-summary-insert-line
2741            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2742            0 nil 128 t nil "" nil 1)
2743           (goto-char (point-min))
2744           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2745                                              (- (point) 2)))))
2746           (goto-char (point-min))
2747           (push (cons 'replied (and (search-forward "\201" nil t)
2748                                     (- (point) 2)))
2749                 pos)
2750           (goto-char (point-min))
2751           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2752                 pos)
2753           (goto-char (point-min))
2754           (push (cons 'download
2755                       (and (search-forward "\203" nil t) (- (point) 2)))
2756                 pos)))
2757       (setq gnus-summary-mark-positions pos))))
2758
2759 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2760   "Insert a dummy root in the summary buffer."
2761   (beginning-of-line)
2762   (gnus-add-text-properties
2763    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2764    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2765
2766 (defun gnus-summary-from-or-to-or-newsgroups (header)
2767   (let ((to (cdr (assq 'To (mail-header-extra header))))
2768         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2769         (default-mime-charset (with-current-buffer gnus-summary-buffer
2770                                 default-mime-charset)))
2771     (cond
2772      ((and to
2773            gnus-ignored-from-addresses
2774            (string-match gnus-ignored-from-addresses
2775                          (mail-header-from header)))
2776       (concat "-> "
2777               (or (car (funcall gnus-extract-address-components
2778                                 (funcall
2779                                  gnus-decode-encoded-word-function to)))
2780                   (funcall gnus-decode-encoded-word-function to))))
2781      ((and newsgroups
2782            gnus-ignored-from-addresses
2783            (string-match gnus-ignored-from-addresses
2784                          (mail-header-from header)))
2785       (concat "=> " newsgroups))
2786      (t
2787       (or (car (funcall gnus-extract-address-components
2788                         (mail-header-from header)))
2789           (mail-header-from header))))))
2790
2791 (defun gnus-summary-insert-line (gnus-tmp-header
2792                                  gnus-tmp-level gnus-tmp-current
2793                                  gnus-tmp-unread gnus-tmp-replied
2794                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2795                                  &optional gnus-tmp-dummy gnus-tmp-score
2796                                  gnus-tmp-process)
2797   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2798          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2799          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2800          (gnus-tmp-score-char
2801           (if (or (null gnus-summary-default-score)
2802                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2803                       gnus-summary-zcore-fuzz))
2804               ?  ;Whitespace
2805             (if (< gnus-tmp-score gnus-summary-default-score)
2806                 gnus-score-below-mark gnus-score-over-mark)))
2807          (gnus-tmp-replied
2808           (cond (gnus-tmp-process gnus-process-mark)
2809                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2810                  gnus-cached-mark)
2811                 (gnus-tmp-replied gnus-replied-mark)
2812                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2813                  gnus-saved-mark)
2814                 (t gnus-no-mark)))
2815          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2816          (gnus-tmp-name
2817           (cond
2818            ((string-match "<[^>]+> *$" gnus-tmp-from)
2819             (let ((beg (match-beginning 0)))
2820               (or (and (string-match "^\".+\"" gnus-tmp-from)
2821                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2822                   (substring gnus-tmp-from 0 beg))))
2823            ((string-match "(.+)" gnus-tmp-from)
2824             (substring gnus-tmp-from
2825                        (1+ (match-beginning 0)) (1- (match-end 0))))
2826            (t gnus-tmp-from)))
2827          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2828          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2829          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2830          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2831          (buffer-read-only nil))
2832     (when (string= gnus-tmp-name "")
2833       (setq gnus-tmp-name gnus-tmp-from))
2834     (unless (numberp gnus-tmp-lines)
2835       (setq gnus-tmp-lines 0))
2836     (gnus-put-text-property-excluding-characters-with-faces
2837      (point)
2838      (progn (eval gnus-summary-line-format-spec) (point))
2839      'gnus-number gnus-tmp-number)
2840     (when (gnus-visual-p 'summary-highlight 'highlight)
2841       (forward-line -1)
2842       (gnus-run-hooks 'gnus-summary-update-hook)
2843       (forward-line 1))))
2844
2845 (defun gnus-summary-update-line (&optional dont-update)
2846   "Update summary line after change."
2847   (when (and gnus-summary-default-score
2848              (not gnus-summary-inhibit-highlight))
2849     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2850            (article (gnus-summary-article-number))
2851            (score (gnus-summary-article-score article)))
2852       (unless dont-update
2853         (if (and gnus-summary-mark-below
2854                  (< (gnus-summary-article-score)
2855                     gnus-summary-mark-below))
2856             ;; This article has a low score, so we mark it as read.
2857             (when (memq article gnus-newsgroup-unreads)
2858               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2859           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2860             ;; This article was previously marked as read on account
2861             ;; of a low score, but now it has risen, so we mark it as
2862             ;; unread.
2863             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2864         (gnus-summary-update-mark
2865          (if (or (null gnus-summary-default-score)
2866                  (<= (abs (- score gnus-summary-default-score))
2867                      gnus-summary-zcore-fuzz))
2868              ?  ;Whitespace
2869            (if (< score gnus-summary-default-score)
2870                gnus-score-below-mark gnus-score-over-mark))
2871          'score))
2872       ;; Do visual highlighting.
2873       (when (gnus-visual-p 'summary-highlight 'highlight)
2874         (gnus-run-hooks 'gnus-summary-update-hook)))))
2875
2876 (defvar gnus-tmp-new-adopts nil)
2877
2878 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2879   "Return the number of articles in THREAD.
2880 This may be 0 in some cases -- if none of the articles in
2881 the thread are to be displayed."
2882   (let* ((number
2883           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2884           (cond
2885            ((not (listp thread))
2886             1)
2887            ((and (consp thread) (cdr thread))
2888             (apply
2889              '+ 1 (mapcar
2890                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2891            ((null thread)
2892             1)
2893            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2894             1)
2895            (t 0))))
2896     (when (and level (zerop level) gnus-tmp-new-adopts)
2897       (incf number
2898             (apply '+ (mapcar
2899                        'gnus-summary-number-of-articles-in-thread
2900                        gnus-tmp-new-adopts))))
2901     (if char
2902         (if (> number 1) gnus-not-empty-thread-mark
2903           gnus-empty-thread-mark)
2904       number)))
2905
2906 (defun gnus-summary-set-local-parameters (group)
2907   "Go through the local params of GROUP and set all variable specs in that list."
2908   (let ((params (gnus-group-find-parameter group))
2909         elem)
2910     (while params
2911       (setq elem (car params)
2912             params (cdr params))
2913       (and (consp elem)                 ; Has to be a cons.
2914            (consp (cdr elem))           ; The cdr has to be a list.
2915            (symbolp (car elem))         ; Has to be a symbol in there.
2916            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2917            (ignore-errors               ; So we set it.
2918              (make-local-variable (car elem))
2919              (set (car elem) (eval (nth 1 elem))))))))
2920
2921 (defun gnus-summary-read-group (group &optional show-all no-article
2922                                       kill-buffer no-display backward
2923                                       select-articles)
2924   "Start reading news in newsgroup GROUP.
2925 If SHOW-ALL is non-nil, already read articles are also listed.
2926 If NO-ARTICLE is non-nil, no article is selected initially.
2927 If NO-DISPLAY, don't generate a summary buffer."
2928   (let (result)
2929     (while (and group
2930                 (null (setq result
2931                             (let ((gnus-auto-select-next nil))
2932                               (or (gnus-summary-read-group-1
2933                                    group show-all no-article
2934                                    kill-buffer no-display
2935                                    select-articles)
2936                                   (setq show-all nil
2937                                         select-articles nil)))))
2938                 (eq gnus-auto-select-next 'quietly))
2939       (set-buffer gnus-group-buffer)
2940       ;; The entry function called above goes to the next
2941       ;; group automatically, so we go two groups back
2942       ;; if we are searching for the previous group.
2943       (when backward
2944         (gnus-group-prev-unread-group 2))
2945       (if (not (equal group (gnus-group-group-name)))
2946           (setq group (gnus-group-group-name))
2947         (setq group nil)))
2948     result))
2949
2950 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2951   "Directly jump to the other GROUP from summary buffer.
2952 If SHOW-ALL is non-nil, already read articles are also listed."
2953   (interactive
2954    (if (eq gnus-summary-buffer (current-buffer))
2955        (list (completing-read
2956               "Group: " gnus-active-hashtb nil t
2957               (when (and gnus-newsgroup-name
2958                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2959                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2960               'gnus-group-history)
2961              current-prefix-arg)
2962      (error "%s must be invoked from a gnus summary buffer." this-command)))
2963   (unless (or (zerop (length group))
2964               (and gnus-newsgroup-name
2965                    (string-equal gnus-newsgroup-name group)))
2966     (gnus-summary-exit)
2967     (gnus-summary-read-group group show-all
2968                              gnus-dont-select-after-jump-to-other-group)))
2969
2970 (defun gnus-summary-read-group-1 (group show-all no-article
2971                                         kill-buffer no-display
2972                                         &optional select-articles)
2973   ;; Killed foreign groups can't be entered.
2974   (when (and (not (gnus-group-native-p group))
2975              (not (gnus-gethash group gnus-newsrc-hashtb)))
2976     (error "Dead non-native groups can't be entered"))
2977   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2978   (let* ((new-group (gnus-summary-setup-buffer group))
2979          (quit-config (gnus-group-quit-config group))
2980          (did-select (and new-group (gnus-select-newsgroup
2981                                      group show-all select-articles))))
2982     (cond
2983      ;; This summary buffer exists already, so we just select it.
2984      ((not new-group)
2985       (gnus-set-global-variables)
2986       (when kill-buffer
2987         (gnus-kill-or-deaden-summary kill-buffer))
2988       (gnus-configure-windows 'summary 'force)
2989       (gnus-set-mode-line 'summary)
2990       (gnus-summary-position-point)
2991       (message "")
2992       t)
2993      ;; We couldn't select this group.
2994      ((null did-select)
2995       (when (and (eq major-mode 'gnus-summary-mode)
2996                  (not (equal (current-buffer) kill-buffer)))
2997         (kill-buffer (current-buffer))
2998         (if (not quit-config)
2999             (progn
3000               ;; Update the info -- marks might need to be removed,
3001               ;; for instance.
3002               (gnus-summary-update-info)
3003               (set-buffer gnus-group-buffer)
3004               (gnus-group-jump-to-group group)
3005               (gnus-group-next-unread-group 1))
3006           (gnus-handle-ephemeral-exit quit-config)))
3007       (gnus-message 3 "Can't select group")
3008       nil)
3009      ;; The user did a `C-g' while prompting for number of articles,
3010      ;; so we exit this group.
3011      ((eq did-select 'quit)
3012       (and (eq major-mode 'gnus-summary-mode)
3013            (not (equal (current-buffer) kill-buffer))
3014            (kill-buffer (current-buffer)))
3015       (when kill-buffer
3016         (gnus-kill-or-deaden-summary kill-buffer))
3017       (if (not quit-config)
3018           (progn
3019             (set-buffer gnus-group-buffer)
3020             (gnus-group-jump-to-group group)
3021             (gnus-group-next-unread-group 1)
3022             (gnus-configure-windows 'group 'force))
3023         (gnus-handle-ephemeral-exit quit-config))
3024       ;; Finally signal the quit.
3025       (signal 'quit nil))
3026      ;; The group was successfully selected.
3027      (t
3028       (gnus-set-global-variables)
3029       ;; Save the active value in effect when the group was entered.
3030       (setq gnus-newsgroup-active
3031             (gnus-copy-sequence
3032              (gnus-active gnus-newsgroup-name)))
3033       ;; You can change the summary buffer in some way with this hook.
3034       (gnus-run-hooks 'gnus-select-group-hook)
3035       ;; Set any local variables in the group parameters.
3036       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3037       (gnus-update-format-specifications
3038        nil 'summary 'summary-mode 'summary-dummy)
3039       (gnus-update-summary-mark-positions)
3040       ;; Do score processing.
3041       (when gnus-use-scoring
3042         (gnus-possibly-score-headers))
3043       ;; Check whether to fill in the gaps in the threads.
3044       (when gnus-build-sparse-threads
3045         (gnus-build-sparse-threads))
3046       ;; Find the initial limit.
3047       (if gnus-show-threads
3048           (if show-all
3049               (let ((gnus-newsgroup-dormant nil))
3050                 (gnus-summary-initial-limit show-all))
3051             (gnus-summary-initial-limit show-all))
3052         ;; When untreaded, all articles are always shown.
3053         (setq gnus-newsgroup-limit
3054               (mapcar
3055                (lambda (header) (mail-header-number header))
3056                gnus-newsgroup-headers)))
3057       ;; Generate the summary buffer.
3058       (unless no-display
3059         (gnus-summary-prepare))
3060       (when gnus-use-trees
3061         (gnus-tree-open group)
3062         (setq gnus-summary-highlight-line-function
3063               'gnus-tree-highlight-article))
3064       ;; If the summary buffer is empty, but there are some low-scored
3065       ;; articles or some excluded dormants, we include these in the
3066       ;; buffer.
3067       (when (and (zerop (buffer-size))
3068                  (not no-display))
3069         (cond (gnus-newsgroup-dormant
3070                (gnus-summary-limit-include-dormant))
3071               ((and gnus-newsgroup-scored show-all)
3072                (gnus-summary-limit-include-expunged t))))
3073       ;; Function `gnus-apply-kill-file' must be called in this hook.
3074       (gnus-run-hooks 'gnus-apply-kill-hook)
3075       (if (and (zerop (buffer-size))
3076                (not no-display))
3077           (progn
3078             ;; This newsgroup is empty.
3079             (gnus-summary-catchup-and-exit nil t)
3080             (gnus-message 6 "No unread news")
3081             (when kill-buffer
3082               (gnus-kill-or-deaden-summary kill-buffer))
3083             ;; Return nil from this function.
3084             nil)
3085         ;; Hide conversation thread subtrees.  We cannot do this in
3086         ;; gnus-summary-prepare-hook since kill processing may not
3087         ;; work with hidden articles.
3088         (and gnus-show-threads
3089              gnus-thread-hide-subtree
3090              (gnus-summary-hide-all-threads))
3091         (when kill-buffer
3092           (gnus-kill-or-deaden-summary kill-buffer))
3093         ;; Show first unread article if requested.
3094         (if (and (not no-article)
3095                  (not no-display)
3096                  gnus-newsgroup-unreads
3097                  gnus-auto-select-first)
3098             (progn
3099               (gnus-configure-windows 'summary)
3100               (cond
3101                ((eq gnus-auto-select-first 'best)
3102                 (gnus-summary-best-unread-article))
3103                ((eq gnus-auto-select-first t)
3104                 (gnus-summary-first-unread-article))
3105                ((gnus-functionp gnus-auto-select-first)
3106                 (funcall gnus-auto-select-first))))
3107           ;; Don't select any articles, just move point to the first
3108           ;; article in the group.
3109           (goto-char (point-min))
3110           (gnus-summary-position-point)
3111           (gnus-configure-windows 'summary 'force)
3112           (gnus-set-mode-line 'summary))
3113         (when (get-buffer-window gnus-group-buffer t)
3114           ;; Gotta use windows, because recenter does weird stuff if
3115           ;; the current buffer ain't the displayed window.
3116           (let ((owin (selected-window)))
3117             (select-window (get-buffer-window gnus-group-buffer t))
3118             (when (gnus-group-goto-group group)
3119               (recenter))
3120             (select-window owin)))
3121         ;; Mark this buffer as "prepared".
3122         (setq gnus-newsgroup-prepared t)
3123         (gnus-run-hooks 'gnus-summary-prepared-hook)
3124         t)))))
3125
3126 (defun gnus-summary-prepare ()
3127   "Generate the summary buffer."
3128   (interactive)
3129   (let ((buffer-read-only nil))
3130     (erase-buffer)
3131     (setq gnus-newsgroup-data nil
3132           gnus-newsgroup-data-reverse nil)
3133     (gnus-run-hooks 'gnus-summary-generate-hook)
3134     ;; Generate the buffer, either with threads or without.
3135     (when gnus-newsgroup-headers
3136       (gnus-summary-prepare-threads
3137        (if gnus-show-threads
3138            (gnus-sort-gathered-threads
3139             (funcall gnus-summary-thread-gathering-function
3140                      (gnus-sort-threads
3141                       (gnus-cut-threads (gnus-make-threads)))))
3142          ;; Unthreaded display.
3143          (gnus-sort-articles gnus-newsgroup-headers))))
3144     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3145     ;; Call hooks for modifying summary buffer.
3146     (goto-char (point-min))
3147     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3148
3149 (defsubst gnus-general-simplify-subject (subject)
3150   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3151   (setq subject
3152         (cond
3153          ;; Truncate the subject.
3154          (gnus-simplify-subject-functions
3155           (gnus-map-function gnus-simplify-subject-functions subject))
3156          ((numberp gnus-summary-gather-subject-limit)
3157           (setq subject (gnus-simplify-subject-re subject))
3158           (if (> (length subject) gnus-summary-gather-subject-limit)
3159               (substring subject 0 gnus-summary-gather-subject-limit)
3160             subject))
3161          ;; Fuzzily simplify it.
3162          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3163           (gnus-simplify-subject-fuzzy subject))
3164          ;; Just remove the leading "Re:".
3165          (t
3166           (gnus-simplify-subject-re subject))))
3167
3168   (if (and gnus-summary-gather-exclude-subject
3169            (string-match gnus-summary-gather-exclude-subject subject))
3170       nil                               ; This article shouldn't be gathered
3171     subject))
3172
3173 (defun gnus-summary-simplify-subject-query ()
3174   "Query where the respool algorithm would put this article."
3175   (interactive)
3176   (gnus-summary-select-article)
3177   (message "%s"
3178            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3179
3180 (defun gnus-gather-threads-by-subject (threads)
3181   "Gather threads by looking at Subject headers."
3182   (if (not gnus-summary-make-false-root)
3183       threads
3184     (let ((hashtb (gnus-make-hashtable 1024))
3185           (prev threads)
3186           (result threads)
3187           subject hthread whole-subject)
3188       (while threads
3189         (setq subject (gnus-general-simplify-subject
3190                        (setq whole-subject (mail-header-subject
3191                                             (caar threads)))))
3192         (when subject
3193           (if (setq hthread (gnus-gethash subject hashtb))
3194               (progn
3195                 ;; We enter a dummy root into the thread, if we
3196                 ;; haven't done that already.
3197                 (unless (stringp (caar hthread))
3198                   (setcar hthread (list whole-subject (car hthread))))
3199                 ;; We add this new gathered thread to this gathered
3200                 ;; thread.
3201                 (setcdr (car hthread)
3202                         (nconc (cdar hthread) (list (car threads))))
3203                 ;; Remove it from the list of threads.
3204                 (setcdr prev (cdr threads))
3205                 (setq threads prev))
3206             ;; Enter this thread into the hash table.
3207             (gnus-sethash subject threads hashtb)))
3208         (setq prev threads)
3209         (setq threads (cdr threads)))
3210       result)))
3211
3212 (defun gnus-gather-threads-by-references (threads)
3213   "Gather threads by looking at References headers."
3214   (let ((idhashtb (gnus-make-hashtable 1024))
3215         (thhashtb (gnus-make-hashtable 1024))
3216         (prev threads)
3217         (result threads)
3218         ids references id gthread gid entered ref)
3219     (while threads
3220       (when (setq references (mail-header-references (caar threads)))
3221         (setq id (mail-header-id (caar threads))
3222               ids (gnus-split-references references)
3223               entered nil)
3224         (while (setq ref (pop ids))
3225           (setq ids (delete ref ids))
3226           (if (not (setq gid (gnus-gethash ref idhashtb)))
3227               (progn
3228                 (gnus-sethash ref id idhashtb)
3229                 (gnus-sethash id threads thhashtb))
3230             (setq gthread (gnus-gethash gid thhashtb))
3231             (unless entered
3232               ;; We enter a dummy root into the thread, if we
3233               ;; haven't done that already.
3234               (unless (stringp (caar gthread))
3235                 (setcar gthread (list (mail-header-subject (caar gthread))
3236                                       (car gthread))))
3237               ;; We add this new gathered thread to this gathered
3238               ;; thread.
3239               (setcdr (car gthread)
3240                       (nconc (cdar gthread) (list (car threads)))))
3241             ;; Add it into the thread hash table.
3242             (gnus-sethash id gthread thhashtb)
3243             (setq entered t)
3244             ;; Remove it from the list of threads.
3245             (setcdr prev (cdr threads))
3246             (setq threads prev))))
3247       (setq prev threads)
3248       (setq threads (cdr threads)))
3249     result))
3250
3251 (defun gnus-sort-gathered-threads (threads)
3252   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3253   (let ((result threads))
3254     (while threads
3255       (when (stringp (caar threads))
3256         (setcdr (car threads)
3257                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3258       (setq threads (cdr threads)))
3259     result))
3260
3261 (defun gnus-thread-loop-p (root thread)
3262   "Say whether ROOT is in THREAD."
3263   (let ((stack (list thread))
3264         (infloop 0)
3265         th)
3266     (while (setq thread (pop stack))
3267       (setq th (cdr thread))
3268       (while (and th
3269                   (not (eq (caar th) root)))
3270         (pop th))
3271       (if th
3272           ;; We have found a loop.
3273           (let (ref-dep)
3274             (setcdr thread (delq (car th) (cdr thread)))
3275             (if (boundp (setq ref-dep (intern "none"
3276                                               gnus-newsgroup-dependencies)))
3277                 (setcdr (symbol-value ref-dep)
3278                         (nconc (cdr (symbol-value ref-dep))
3279                                (list (car th))))
3280               (set ref-dep (list nil (car th))))
3281             (setq infloop 1
3282                   stack nil))
3283         ;; Push all the subthreads onto the stack.
3284         (push (cdr thread) stack)))
3285     infloop))
3286
3287 (defun gnus-make-threads ()
3288   "Go through the dependency hashtb and find the roots.  Return all threads."
3289   (let (threads)
3290     (while (catch 'infloop
3291              (mapatoms
3292               (lambda (refs)
3293                 ;; Deal with self-referencing References loops.
3294                 (when (and (car (symbol-value refs))
3295                            (not (zerop
3296                                  (apply
3297                                   '+
3298                                   (mapcar
3299                                    (lambda (thread)
3300                                      (gnus-thread-loop-p
3301                                       (car (symbol-value refs)) thread))
3302                                    (cdr (symbol-value refs)))))))
3303                   (setq threads nil)
3304                   (throw 'infloop t))
3305                 (unless (car (symbol-value refs))
3306                   ;; These threads do not refer back to any other articles,
3307                   ;; so they're roots.
3308                   (setq threads (append (cdr (symbol-value refs)) threads))))
3309               gnus-newsgroup-dependencies)))
3310     threads))
3311
3312 ;; Build the thread tree.
3313 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3314   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3315
3316 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3317 if it was already present.
3318
3319 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3320 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3321 Message-IDs will be renamed be renamed to a unique Message-ID before
3322 being entered.
3323
3324 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3325   (let* ((id (mail-header-id header))
3326          (id-dep (and id (intern id dependencies)))
3327          ref ref-dep ref-header)
3328     ;; Enter this `header' in the `dependencies' table.
3329     (cond
3330      ((not id-dep)
3331       (setq header nil))
3332      ;; The first two cases do the normal part: enter a new `header'
3333      ;; in the `dependencies' table.
3334      ((not (boundp id-dep))
3335       (set id-dep (list header)))
3336      ((null (car (symbol-value id-dep)))
3337       (setcar (symbol-value id-dep) header))
3338
3339      ;; From here the `header' was already present in the
3340      ;; `dependencies' table.
3341      (force-new
3342       ;; Overrides an existing entry;
3343       ;; just set the header part of the entry.
3344       (setcar (symbol-value id-dep) header))
3345
3346      ;; Renames the existing `header' to a unique Message-ID.
3347      ((not gnus-summary-ignore-duplicates)
3348       ;; An article with this Message-ID has already been seen.
3349       ;; We rename the Message-ID.
3350       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3351            (list header))
3352       (mail-header-set-id header id))
3353
3354      ;; The last case ignores an existing entry, except it adds any
3355      ;; additional Xrefs (in case the two articles came from different
3356      ;; servers.
3357      ;; Also sets `header' to `nil' meaning that the `dependencies'
3358      ;; table was *not* modified.
3359      (t
3360       (mail-header-set-xref
3361        (car (symbol-value id-dep))
3362        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3363                    "")
3364                (or (mail-header-xref header) "")))
3365       (setq header nil)))
3366
3367     (when header
3368       ;; First check if that we are not creating a References loop.
3369       (setq ref (gnus-parent-id (mail-header-references header)))
3370       (while (and ref
3371                   (setq ref-dep (intern-soft ref dependencies))
3372                   (boundp ref-dep)
3373                   (setq ref-header (car (symbol-value ref-dep))))
3374         (if (string= id ref)
3375             ;; Yuk!  This is a reference loop.  Make the article be a
3376             ;; root article.
3377             (progn
3378               (mail-header-set-references (car (symbol-value id-dep)) "none")
3379               (setq ref nil))
3380           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3381       (setq ref (gnus-parent-id (mail-header-references header)))
3382       (setq ref-dep (intern (or ref "none") dependencies))
3383       (if (boundp ref-dep)
3384           (setcdr (symbol-value ref-dep)
3385                   (nconc (cdr (symbol-value ref-dep))
3386                          (list (symbol-value id-dep))))
3387         (set ref-dep (list nil (symbol-value id-dep)))))
3388     header))
3389
3390 (defun gnus-build-sparse-threads ()
3391   (let ((headers gnus-newsgroup-headers)
3392         (mail-parse-charset gnus-newsgroup-charset)
3393         (gnus-summary-ignore-duplicates t)
3394         header references generation relations
3395         subject child end new-child date)
3396     ;; First we create an alist of generations/relations, where
3397     ;; generations is how much we trust the relation, and the relation
3398     ;; is parent/child.
3399     (gnus-message 7 "Making sparse threads...")
3400     (save-excursion
3401       (nnheader-set-temp-buffer " *gnus sparse threads*")
3402       (while (setq header (pop headers))
3403         (when (and (setq references (mail-header-references header))
3404                    (not (string= references "")))
3405           (insert references)
3406           (setq child (mail-header-id header)
3407                 subject (mail-header-subject header)
3408                 date (mail-header-date header)
3409                 generation 0)
3410           (while (search-backward ">" nil t)
3411             (setq end (1+ (point)))
3412             (when (search-backward "<" nil t)
3413               (setq new-child (buffer-substring (point) end))
3414               (push (list (incf generation)
3415                           child (setq child new-child)
3416                           subject date)
3417                     relations)))
3418           (when child
3419             (push (list (1+ generation) child nil subject) relations))
3420           (erase-buffer)))
3421       (kill-buffer (current-buffer)))
3422     ;; Sort over trustworthiness.
3423     (mapcar
3424      (lambda (relation)
3425        (when (gnus-dependencies-add-header
3426               (make-full-mail-header-from-decoded-header
3427                gnus-reffed-article-number
3428                (nth 3 relation) "" (or (nth 4 relation) "")
3429                (nth 1 relation)
3430                (or (nth 2 relation) "") 0 0 "")
3431               gnus-newsgroup-dependencies nil)
3432          (push gnus-reffed-article-number gnus-newsgroup-limit)
3433          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3434          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3435                gnus-newsgroup-reads)
3436          (decf gnus-reffed-article-number)))
3437      (sort relations 'car-less-than-car))
3438     (gnus-message 7 "Making sparse threads...done")))
3439
3440 (defun gnus-build-old-threads ()
3441   ;; Look at all the articles that refer back to old articles, and
3442   ;; fetch the headers for the articles that aren't there.  This will
3443   ;; build complete threads - if the roots haven't been expired by the
3444   ;; server, that is.
3445   (let ((mail-parse-charset gnus-newsgroup-charset)
3446         id heads)
3447     (mapatoms
3448      (lambda (refs)
3449        (when (not (car (symbol-value refs)))
3450          (setq heads (cdr (symbol-value refs)))
3451          (while heads
3452            (if (memq (mail-header-number (caar heads))
3453                      gnus-newsgroup-dormant)
3454                (setq heads (cdr heads))
3455              (setq id (symbol-name refs))
3456              (while (and (setq id (gnus-build-get-header id))
3457                          (not (car (gnus-id-to-thread id)))))
3458              (setq heads nil)))))
3459      gnus-newsgroup-dependencies)))
3460
3461 ;; This function has to be called with point after the article number
3462 ;; on the beginning of the line.
3463 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3464   (let ((eol (gnus-point-at-eol))
3465         (buffer (current-buffer))
3466         header)
3467
3468     ;; overview: [num subject from date id refs chars lines misc]
3469     (unwind-protect
3470         (progn
3471           (narrow-to-region (point) eol)
3472           (unless (eobp)
3473             (forward-char))
3474
3475           (setq header
3476                 (make-full-mail-header
3477                  number                         ; number
3478                  (nnheader-nov-field)           ; subject
3479                  (nnheader-nov-field)           ; from
3480                  (nnheader-nov-field)           ; date
3481                  (nnheader-nov-read-message-id) ; id
3482                  (nnheader-nov-field)           ; refs
3483                  (nnheader-nov-read-integer)    ; chars
3484                  (nnheader-nov-read-integer)    ; lines
3485                  (unless (eobp)
3486                    (if (looking-at "Xref: ")
3487                        (goto-char (match-end 0)))
3488                    (nnheader-nov-field))        ; Xref
3489                  (nnheader-nov-parse-extra))))  ; extra
3490
3491       (widen))
3492
3493     (when gnus-alter-header-function
3494       (funcall gnus-alter-header-function header))
3495     (gnus-dependencies-add-header header dependencies force-new)))
3496
3497 (defun gnus-build-get-header (id)
3498   "Look through the buffer of NOV lines and find the header to ID.
3499 Enter this line into the dependencies hash table, and return
3500 the id of the parent article (if any)."
3501   (let ((deps gnus-newsgroup-dependencies)
3502         found header)
3503     (prog1
3504         (save-excursion
3505           (set-buffer nntp-server-buffer)
3506           (let ((case-fold-search nil))
3507             (goto-char (point-min))
3508             (while (and (not found)
3509                         (search-forward id nil t))
3510               (beginning-of-line)
3511               (setq found (looking-at
3512                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3513                                    (regexp-quote id))))
3514               (or found (beginning-of-line 2)))
3515             (when found
3516               (beginning-of-line)
3517               (and
3518                (setq header (gnus-nov-parse-line
3519                              (read (current-buffer)) deps))
3520                (gnus-parent-id (mail-header-references header))))))
3521       (when header
3522         (let ((number (mail-header-number header)))
3523           (push number gnus-newsgroup-limit)
3524           (push header gnus-newsgroup-headers)
3525           (if (memq number gnus-newsgroup-unselected)
3526               (progn
3527                 (push number gnus-newsgroup-unreads)
3528                 (setq gnus-newsgroup-unselected
3529                       (delq number gnus-newsgroup-unselected)))
3530             (push number gnus-newsgroup-ancient)))))))
3531
3532 (defun gnus-build-all-threads ()
3533   "Read all the headers."
3534   (let ((gnus-summary-ignore-duplicates t)
3535         (mail-parse-charset gnus-newsgroup-charset)
3536         (dependencies gnus-newsgroup-dependencies)
3537         header article)
3538     (save-excursion
3539       (set-buffer nntp-server-buffer)
3540       (let ((case-fold-search nil))
3541         (goto-char (point-min))
3542         (while (not (eobp))
3543           (ignore-errors
3544             (setq article (read (current-buffer))
3545                   header (gnus-nov-parse-line article dependencies)))
3546           (when header
3547             (save-excursion
3548               (set-buffer gnus-summary-buffer)
3549               (push header gnus-newsgroup-headers)
3550               (if (memq (setq article (mail-header-number header))
3551                         gnus-newsgroup-unselected)
3552                   (progn
3553                     (push article gnus-newsgroup-unreads)
3554                     (setq gnus-newsgroup-unselected
3555                           (delq article gnus-newsgroup-unselected)))
3556                 (push article gnus-newsgroup-ancient)))
3557             (forward-line 1)))))))
3558
3559 (defun gnus-summary-update-article-line (article header)
3560   "Update the line for ARTICLE using HEADERS."
3561   (let* ((id (mail-header-id header))
3562          (thread (gnus-id-to-thread id)))
3563     (unless thread
3564       (error "Article in no thread"))
3565     ;; Update the thread.
3566     (setcar thread header)
3567     (gnus-summary-goto-subject article)
3568     (let* ((datal (gnus-data-find-list article))
3569            (data (car datal))
3570            (length (when (cdr datal)
3571                      (- (gnus-data-pos data)
3572                         (gnus-data-pos (cadr datal)))))
3573            (buffer-read-only nil)
3574            (level (gnus-summary-thread-level)))
3575       (gnus-delete-line)
3576       (gnus-summary-insert-line
3577        header level nil (gnus-article-mark article)
3578        (memq article gnus-newsgroup-replied)
3579        (memq article gnus-newsgroup-expirable)
3580        ;; Only insert the Subject string when it's different
3581        ;; from the previous Subject string.
3582        (if (and
3583             gnus-show-threads
3584             (gnus-subject-equal
3585              (condition-case ()
3586                  (mail-header-subject
3587                   (gnus-data-header
3588                    (cadr
3589                     (gnus-data-find-list
3590                      article
3591                      (gnus-data-list t)))))
3592                ;; Error on the side of excessive subjects.
3593                (error ""))
3594              (mail-header-subject header)))
3595            ""
3596          (mail-header-subject header))
3597        nil (cdr (assq article gnus-newsgroup-scored))
3598        (memq article gnus-newsgroup-processable))
3599       (when length
3600         (gnus-data-update-list
3601          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3602
3603 (defun gnus-summary-update-article (article &optional iheader)
3604   "Update ARTICLE in the summary buffer."
3605   (set-buffer gnus-summary-buffer)
3606   (let* ((header (gnus-summary-article-header article))
3607          (id (mail-header-id header))
3608          (data (gnus-data-find article))
3609          (thread (gnus-id-to-thread id))
3610          (references (mail-header-references header))
3611          (parent
3612           (gnus-id-to-thread
3613            (or (gnus-parent-id
3614                 (when (and references
3615                            (not (equal "" references)))
3616                   references))
3617                "none")))
3618          (buffer-read-only nil)
3619          (old (car thread)))
3620     (when thread
3621       (unless iheader
3622         (setcar thread nil)
3623         (when parent
3624           (delq thread parent)))
3625       (if (gnus-summary-insert-subject id header)
3626           ;; Set the (possibly) new article number in the data structure.
3627           (gnus-data-set-number data (gnus-id-to-article id))
3628         (setcar thread old)
3629         nil))))
3630
3631 (defun gnus-rebuild-thread (id &optional line)
3632   "Rebuild the thread containing ID.
3633 If LINE, insert the rebuilt thread starting on line LINE."
3634   (let ((buffer-read-only nil)
3635         old-pos current thread data)
3636     (if (not gnus-show-threads)
3637         (setq thread (list (car (gnus-id-to-thread id))))
3638       ;; Get the thread this article is part of.
3639       (setq thread (gnus-remove-thread id)))
3640     (setq old-pos (gnus-point-at-bol))
3641     (setq current (save-excursion
3642                     (and (zerop (forward-line -1))
3643                          (gnus-summary-article-number))))
3644     ;; If this is a gathered thread, we have to go some re-gathering.
3645     (when (stringp (car thread))
3646       (let ((subject (car thread))
3647             roots thr)
3648         (setq thread (cdr thread))
3649         (while thread
3650           (unless (memq (setq thr (gnus-id-to-thread
3651                                    (gnus-root-id
3652                                     (mail-header-id (caar thread)))))
3653                         roots)
3654             (push thr roots))
3655           (setq thread (cdr thread)))
3656         ;; We now have all (unique) roots.
3657         (if (= (length roots) 1)
3658             ;; All the loose roots are now one solid root.
3659             (setq thread (car roots))
3660           (setq thread (cons subject (gnus-sort-threads roots))))))
3661     (let (threads)
3662       ;; We then insert this thread into the summary buffer.
3663       (when line
3664         (goto-char (point-min))
3665         (forward-line (1- line)))
3666       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3667         (if gnus-show-threads
3668             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3669           (gnus-summary-prepare-unthreaded thread))
3670         (setq data (nreverse gnus-newsgroup-data))
3671         (setq threads gnus-newsgroup-threads))
3672       ;; We splice the new data into the data structure.
3673       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3674       ;;!!! then we want to insert at the beginning of the buffer.
3675       ;;!!! That happens to be true with Gnus now, but that may
3676       ;;!!! change in the future.  Perhaps.
3677       (gnus-data-enter-list
3678        (if line nil current) data (- (point) old-pos))
3679       (setq gnus-newsgroup-threads
3680             (nconc threads gnus-newsgroup-threads))
3681       (gnus-data-compute-positions))))
3682
3683 (defun gnus-number-to-header (number)
3684   "Return the header for article NUMBER."
3685   (let ((headers gnus-newsgroup-headers))
3686     (while (and headers
3687                 (not (= number (mail-header-number (car headers)))))
3688       (pop headers))
3689     (when headers
3690       (car headers))))
3691
3692 (defun gnus-parent-headers (in-headers &optional generation)
3693   "Return the headers of the GENERATIONeth parent of HEADERS."
3694   (unless generation
3695     (setq generation 1))
3696   (let ((parent t)
3697         (headers in-headers)
3698         references)
3699     (while (and parent
3700                 (not (zerop generation))
3701                 (setq references (mail-header-references headers)))
3702       (setq headers (if (and references
3703                              (setq parent (gnus-parent-id references)))
3704                         (car (gnus-id-to-thread parent))
3705                       nil))
3706       (decf generation))
3707     (and (not (eq headers in-headers))
3708          headers)))
3709
3710 (defun gnus-id-to-thread (id)
3711   "Return the (sub-)thread where ID appears."
3712   (gnus-gethash id gnus-newsgroup-dependencies))
3713
3714 (defun gnus-id-to-article (id)
3715   "Return the article number of ID."
3716   (let ((thread (gnus-id-to-thread id)))
3717     (when (and thread
3718                (car thread))
3719       (mail-header-number (car thread)))))
3720
3721 (defun gnus-id-to-header (id)
3722   "Return the article headers of ID."
3723   (car (gnus-id-to-thread id)))
3724
3725 (defun gnus-article-displayed-root-p (article)
3726   "Say whether ARTICLE is a root(ish) article."
3727   (let ((level (gnus-summary-thread-level article))
3728         (refs (mail-header-references  (gnus-summary-article-header article)))
3729         particle)
3730     (cond
3731      ((null level) nil)
3732      ((zerop level) t)
3733      ((null refs) t)
3734      ((null (gnus-parent-id refs)) t)
3735      ((and (= 1 level)
3736            (null (setq particle (gnus-id-to-article
3737                                  (gnus-parent-id refs))))
3738            (null (gnus-summary-thread-level particle)))))))
3739
3740 (defun gnus-root-id (id)
3741   "Return the id of the root of the thread where ID appears."
3742   (let (last-id prev)
3743     (while (and id (setq prev (car (gnus-id-to-thread id))))
3744       (setq last-id id
3745             id (gnus-parent-id (mail-header-references prev))))
3746     last-id))
3747
3748 (defun gnus-articles-in-thread (thread)
3749   "Return the list of articles in THREAD."
3750   (cons (mail-header-number (car thread))
3751         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3752
3753 (defun gnus-remove-thread (id &optional dont-remove)
3754   "Remove the thread that has ID in it."
3755   (let (headers thread last-id)
3756     ;; First go up in this thread until we find the root.
3757     (setq last-id (gnus-root-id id)
3758           headers (message-flatten-list (gnus-id-to-thread last-id)))
3759     ;; We have now found the real root of this thread.  It might have
3760     ;; been gathered into some loose thread, so we have to search
3761     ;; through the threads to find the thread we wanted.
3762     (let ((threads gnus-newsgroup-threads)
3763           sub)
3764       (while threads
3765         (setq sub (car threads))
3766         (if (stringp (car sub))
3767             ;; This is a gathered thread, so we look at the roots
3768             ;; below it to find whether this article is in this
3769             ;; gathered root.
3770             (progn
3771               (setq sub (cdr sub))
3772               (while sub
3773                 (when (member (caar sub) headers)
3774                   (setq thread (car threads)
3775                         threads nil
3776                         sub nil))
3777                 (setq sub (cdr sub))))
3778           ;; It's an ordinary thread, so we check it.
3779           (when (eq (car sub) (car headers))
3780             (setq thread sub
3781                   threads nil)))
3782         (setq threads (cdr threads)))
3783       ;; If this article is in no thread, then it's a root.
3784       (if thread
3785           (unless dont-remove
3786             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3787         (setq thread (gnus-id-to-thread last-id)))
3788       (when thread
3789         (prog1
3790             thread                      ; We return this thread.
3791           (unless dont-remove
3792             (if (stringp (car thread))
3793                 (progn
3794                   ;; If we use dummy roots, then we have to remove the
3795                   ;; dummy root as well.
3796                   (when (eq gnus-summary-make-false-root 'dummy)
3797                     ;; We go to the dummy root by going to
3798                     ;; the first sub-"thread", and then one line up.
3799                     (gnus-summary-goto-article
3800                      (mail-header-number (caadr thread)))
3801                     (forward-line -1)
3802                     (gnus-delete-line)
3803                     (gnus-data-compute-positions))
3804                   (setq thread (cdr thread))
3805                   (while thread
3806                     (gnus-remove-thread-1 (car thread))
3807                     (setq thread (cdr thread))))
3808               (gnus-remove-thread-1 thread))))))))
3809
3810 (defun gnus-remove-thread-1 (thread)
3811   "Remove the thread THREAD recursively."
3812   (let ((number (mail-header-number (pop thread)))
3813         d)
3814     (setq thread (reverse thread))
3815     (while thread
3816       (gnus-remove-thread-1 (pop thread)))
3817     (when (setq d (gnus-data-find number))
3818       (goto-char (gnus-data-pos d))
3819       (gnus-summary-show-thread)
3820       (gnus-data-remove
3821        number
3822        (- (gnus-point-at-bol)
3823           (prog1
3824               (1+ (gnus-point-at-eol))
3825             (gnus-delete-line)))))))
3826
3827 (defun gnus-sort-threads-1 (threads func)
3828   (sort (mapcar (lambda (thread)
3829                   (cons (car thread)
3830                         (and (cdr thread)
3831                              (gnus-sort-threads-1 (cdr thread) func))))
3832                 threads) func))
3833
3834 (defun gnus-sort-threads (threads)
3835   "Sort THREADS."
3836   (if (not gnus-thread-sort-functions)
3837       threads
3838     (gnus-message 8 "Sorting threads...")
3839     (prog1
3840         (gnus-sort-threads-1 
3841          threads 
3842          (gnus-make-sort-function gnus-thread-sort-functions))
3843       (gnus-message 8 "Sorting threads...done"))))
3844
3845 (defun gnus-sort-articles (articles)
3846   "Sort ARTICLES."
3847   (when gnus-article-sort-functions
3848     (gnus-message 7 "Sorting articles...")
3849     (prog1
3850         (setq gnus-newsgroup-headers
3851               (sort articles (gnus-make-sort-function
3852                               gnus-article-sort-functions)))
3853       (gnus-message 7 "Sorting articles...done"))))
3854
3855 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3856 (defmacro gnus-thread-header (thread)
3857   "Return header of first article in THREAD.
3858 Note that THREAD must never, ever be anything else than a variable -
3859 using some other form will lead to serious barfage."
3860   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3861   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3862   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3863         (vector thread) 2))
3864
3865 (defsubst gnus-article-sort-by-number (h1 h2)
3866   "Sort articles by article number."
3867   (< (mail-header-number h1)
3868      (mail-header-number h2)))
3869
3870 (defun gnus-thread-sort-by-number (h1 h2)
3871   "Sort threads by root article number."
3872   (gnus-article-sort-by-number
3873    (gnus-thread-header h1) (gnus-thread-header h2)))
3874
3875 (defsubst gnus-article-sort-by-lines (h1 h2)
3876   "Sort articles by article Lines header."
3877   (< (mail-header-lines h1)
3878      (mail-header-lines h2)))
3879
3880 (defun gnus-thread-sort-by-lines (h1 h2)
3881   "Sort threads by root article Lines header."
3882   (gnus-article-sort-by-lines
3883    (gnus-thread-header h1) (gnus-thread-header h2)))
3884
3885 (defsubst gnus-article-sort-by-chars (h1 h2)
3886   "Sort articles by octet length."
3887   (< (mail-header-chars h1)
3888      (mail-header-chars h2)))
3889
3890 (defun gnus-thread-sort-by-chars (h1 h2)
3891   "Sort threads by root article octet length."
3892   (gnus-article-sort-by-chars
3893    (gnus-thread-header h1) (gnus-thread-header h2)))
3894
3895 (defsubst gnus-article-sort-by-author (h1 h2)
3896   "Sort articles by root author."
3897   (string-lessp
3898    (let ((addr (car (mime-entity-read-field h1 'From))))
3899      (or (std11-full-name-string addr)
3900          (std11-address-string addr)
3901          ""))
3902    (let ((addr (car (mime-entity-read-field h2 'From))))
3903      (or (std11-full-name-string addr)
3904          (std11-address-string addr)
3905          ""))
3906    ))
3907
3908 (defun gnus-thread-sort-by-author (h1 h2)
3909   "Sort threads by root author."
3910   (gnus-article-sort-by-author
3911    (gnus-thread-header h1)  (gnus-thread-header h2)))
3912
3913 (defsubst gnus-article-sort-by-subject (h1 h2)
3914   "Sort articles by root subject."
3915   (string-lessp
3916    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3917    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3918
3919 (defun gnus-thread-sort-by-subject (h1 h2)
3920   "Sort threads by root subject."
3921   (gnus-article-sort-by-subject
3922    (gnus-thread-header h1) (gnus-thread-header h2)))
3923
3924 (defsubst gnus-article-sort-by-date (h1 h2)
3925   "Sort articles by root article date."
3926   (time-less-p
3927    (gnus-date-get-time (mail-header-date h1))
3928    (gnus-date-get-time (mail-header-date h2))))
3929
3930 (defun gnus-thread-sort-by-date (h1 h2)
3931   "Sort threads by root article date."
3932   (gnus-article-sort-by-date
3933    (gnus-thread-header h1) (gnus-thread-header h2)))
3934
3935 (defsubst gnus-article-sort-by-score (h1 h2)
3936   "Sort articles by root article score.
3937 Unscored articles will be counted as having a score of zero."
3938   (> (or (cdr (assq (mail-header-number h1)
3939                     gnus-newsgroup-scored))
3940          gnus-summary-default-score 0)
3941      (or (cdr (assq (mail-header-number h2)
3942                     gnus-newsgroup-scored))
3943          gnus-summary-default-score 0)))
3944
3945 (defun gnus-thread-sort-by-score (h1 h2)
3946   "Sort threads by root article score."
3947   (gnus-article-sort-by-score
3948    (gnus-thread-header h1) (gnus-thread-header h2)))
3949
3950 (defun gnus-thread-sort-by-total-score (h1 h2)
3951   "Sort threads by the sum of all scores in the thread.
3952 Unscored articles will be counted as having a score of zero."
3953   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3954
3955 (defun gnus-thread-total-score (thread)
3956   ;; This function find the total score of THREAD.
3957   (cond ((null thread)
3958          0)
3959         ((consp thread)
3960          (if (stringp (car thread))
3961              (apply gnus-thread-score-function 0
3962                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3963            (gnus-thread-total-score-1 thread)))
3964         (t
3965          (gnus-thread-total-score-1 (list thread)))))
3966
3967 (defun gnus-thread-total-score-1 (root)
3968   ;; This function find the total score of the thread below ROOT.
3969   (setq root (car root))
3970   (apply gnus-thread-score-function
3971          (or (append
3972               (mapcar 'gnus-thread-total-score
3973                       (cdr (gnus-id-to-thread (mail-header-id root))))
3974               (when (> (mail-header-number root) 0)
3975                 (list (or (cdr (assq (mail-header-number root)
3976                                      gnus-newsgroup-scored))
3977                           gnus-summary-default-score 0))))
3978              (list gnus-summary-default-score)
3979              '(0))))
3980
3981 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3982 (defvar gnus-tmp-prev-subject nil)
3983 (defvar gnus-tmp-false-parent nil)
3984 (defvar gnus-tmp-root-expunged nil)
3985 (defvar gnus-tmp-dummy-line nil)
3986
3987 (eval-when-compile (defvar gnus-tmp-header))
3988 (defun gnus-extra-header (type &optional header)
3989   "Return the extra header of TYPE."
3990   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3991       ""))
3992
3993 (defun gnus-summary-prepare-threads (threads)
3994   "Prepare summary buffer from THREADS and indentation LEVEL.
3995 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3996 or a straight list of headers."
3997   (gnus-message 7 "Generating summary...")
3998
3999   (setq gnus-newsgroup-threads threads)
4000   (beginning-of-line)
4001
4002   (let ((gnus-tmp-level 0)
4003         (default-score (or gnus-summary-default-score 0))
4004         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4005         thread number subject stack state gnus-tmp-gathered beg-match
4006         new-roots gnus-tmp-new-adopts thread-end
4007         gnus-tmp-header gnus-tmp-unread
4008         gnus-tmp-replied gnus-tmp-subject-or-nil
4009         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4010         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4011         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4012
4013     (setq gnus-tmp-prev-subject nil)
4014
4015     (if (vectorp (car threads))
4016         ;; If this is a straight (sic) list of headers, then a
4017         ;; threaded summary display isn't required, so we just create
4018         ;; an unthreaded one.
4019         (gnus-summary-prepare-unthreaded threads)
4020
4021       ;; Do the threaded display.
4022
4023       (while (or threads stack gnus-tmp-new-adopts new-roots)
4024
4025         (if (and (= gnus-tmp-level 0)
4026                  (or (not stack)
4027                      (= (caar stack) 0))
4028                  (not gnus-tmp-false-parent)
4029                  (or gnus-tmp-new-adopts new-roots))
4030             (if gnus-tmp-new-adopts
4031                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4032                       thread (list (car gnus-tmp-new-adopts))
4033                       gnus-tmp-header (caar thread)
4034                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4035               (when new-roots
4036                 (setq thread (list (car new-roots))
4037                       gnus-tmp-header (caar thread)
4038                       new-roots (cdr new-roots))))
4039
4040           (if threads
4041               ;; If there are some threads, we do them before the
4042               ;; threads on the stack.
4043               (setq thread threads
4044                     gnus-tmp-header (caar thread))
4045             ;; There were no current threads, so we pop something off
4046             ;; the stack.
4047             (setq state (car stack)
4048                   gnus-tmp-level (car state)
4049                   thread (cdr state)
4050                   stack (cdr stack)
4051                   gnus-tmp-header (caar thread))))
4052
4053         (setq gnus-tmp-false-parent nil)
4054         (setq gnus-tmp-root-expunged nil)
4055         (setq thread-end nil)
4056
4057         (if (stringp gnus-tmp-header)
4058             ;; The header is a dummy root.
4059             (cond
4060              ((eq gnus-summary-make-false-root 'adopt)
4061               ;; We let the first article adopt the rest.
4062               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4063                                                (cddar thread)))
4064               (setq gnus-tmp-gathered
4065                     (nconc (mapcar
4066                             (lambda (h) (mail-header-number (car h)))
4067                             (cddar thread))
4068                            gnus-tmp-gathered))
4069               (setq thread (cons (list (caar thread)
4070                                        (cadar thread))
4071                                  (cdr thread)))
4072               (setq gnus-tmp-level -1
4073                     gnus-tmp-false-parent t))
4074              ((eq gnus-summary-make-false-root 'empty)
4075               ;; We print adopted articles with empty subject fields.
4076               (setq gnus-tmp-gathered
4077                     (nconc (mapcar
4078                             (lambda (h) (mail-header-number (car h)))
4079                             (cddar thread))
4080                            gnus-tmp-gathered))
4081               (setq gnus-tmp-level -1))
4082              ((eq gnus-summary-make-false-root 'dummy)
4083               ;; We remember that we probably want to output a dummy
4084               ;; root.
4085               (setq gnus-tmp-dummy-line gnus-tmp-header)
4086               (setq gnus-tmp-prev-subject gnus-tmp-header))
4087              (t
4088               ;; We do not make a root for the gathered
4089               ;; sub-threads at all.
4090               (setq gnus-tmp-level -1)))
4091
4092           (setq number (mail-header-number gnus-tmp-header)
4093                 subject (mail-header-subject gnus-tmp-header))
4094
4095           (cond
4096            ;; If the thread has changed subject, we might want to make
4097            ;; this subthread into a root.
4098            ((and (null gnus-thread-ignore-subject)
4099                  (not (zerop gnus-tmp-level))
4100                  gnus-tmp-prev-subject
4101                  (not (inline
4102                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4103             (setq new-roots (nconc new-roots (list (car thread)))
4104                   thread-end t
4105                   gnus-tmp-header nil))
4106            ;; If the article lies outside the current limit,
4107            ;; then we do not display it.
4108            ((not (memq number gnus-newsgroup-limit))
4109             (setq gnus-tmp-gathered
4110                   (nconc (mapcar
4111                           (lambda (h) (mail-header-number (car h)))
4112                           (cdar thread))
4113                          gnus-tmp-gathered))
4114             (setq gnus-tmp-new-adopts (if (cdar thread)
4115                                           (append gnus-tmp-new-adopts
4116                                                   (cdar thread))
4117                                         gnus-tmp-new-adopts)
4118                   thread-end t
4119                   gnus-tmp-header nil)
4120             (when (zerop gnus-tmp-level)
4121               (setq gnus-tmp-root-expunged t)))
4122            ;; Perhaps this article is to be marked as read?
4123            ((and gnus-summary-mark-below
4124                  (< (or (cdr (assq number gnus-newsgroup-scored))
4125                         default-score)
4126                     gnus-summary-mark-below)
4127                  ;; Don't touch sparse articles.
4128                  (not (gnus-summary-article-sparse-p number))
4129                  (not (gnus-summary-article-ancient-p number)))
4130             (setq gnus-newsgroup-unreads
4131                   (delq number gnus-newsgroup-unreads))
4132             (if gnus-newsgroup-auto-expire
4133                 (push number gnus-newsgroup-expirable)
4134               (push (cons number gnus-low-score-mark)
4135                     gnus-newsgroup-reads))))
4136
4137           (when gnus-tmp-header
4138             ;; We may have an old dummy line to output before this
4139             ;; article.
4140             (when (and gnus-tmp-dummy-line
4141                        (gnus-subject-equal
4142                         gnus-tmp-dummy-line
4143                         (mail-header-subject gnus-tmp-header)))
4144               (gnus-summary-insert-dummy-line
4145                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4146               (setq gnus-tmp-dummy-line nil))
4147
4148             ;; Compute the mark.
4149             (setq gnus-tmp-unread (gnus-article-mark number))
4150
4151             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4152                                   gnus-tmp-header gnus-tmp-level)
4153                   gnus-newsgroup-data)
4154
4155             ;; Actually insert the line.
4156             (setq
4157              gnus-tmp-subject-or-nil
4158              (cond
4159               ((and gnus-thread-ignore-subject
4160                     gnus-tmp-prev-subject
4161                     (not (inline (gnus-subject-equal
4162                                   gnus-tmp-prev-subject subject))))
4163                subject)
4164               ((zerop gnus-tmp-level)
4165                (if (and (eq gnus-summary-make-false-root 'empty)
4166                         (memq number gnus-tmp-gathered)
4167                         gnus-tmp-prev-subject
4168                         (inline (gnus-subject-equal
4169                                  gnus-tmp-prev-subject subject)))
4170                    gnus-summary-same-subject
4171                  subject))
4172               (t gnus-summary-same-subject)))
4173             (if (and (eq gnus-summary-make-false-root 'adopt)
4174                      (= gnus-tmp-level 1)
4175                      (memq number gnus-tmp-gathered))
4176                 (setq gnus-tmp-opening-bracket ?\<
4177                       gnus-tmp-closing-bracket ?\>)
4178               (setq gnus-tmp-opening-bracket ?\[
4179                     gnus-tmp-closing-bracket ?\]))
4180             (setq
4181              gnus-tmp-indentation
4182              (aref gnus-thread-indent-array gnus-tmp-level)
4183              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4184              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4185                                 gnus-summary-default-score 0)
4186              gnus-tmp-score-char
4187              (if (or (null gnus-summary-default-score)
4188                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4189                          gnus-summary-zcore-fuzz))
4190                  ?  ;Whitespace
4191                (if (< gnus-tmp-score gnus-summary-default-score)
4192                    gnus-score-below-mark gnus-score-over-mark))
4193              gnus-tmp-replied
4194              (cond ((memq number gnus-newsgroup-processable)
4195                     gnus-process-mark)
4196                    ((memq number gnus-newsgroup-cached)
4197                     gnus-cached-mark)
4198                    ((memq number gnus-newsgroup-replied)
4199                     gnus-replied-mark)
4200                    ((memq number gnus-newsgroup-saved)
4201                     gnus-saved-mark)
4202                    (t gnus-no-mark))
4203              gnus-tmp-from (mail-header-from gnus-tmp-header)
4204              gnus-tmp-name
4205              (cond
4206               ((string-match "<[^>]+> *$" gnus-tmp-from)
4207                (setq beg-match (match-beginning 0))
4208                (or (and (string-match "^\".+\"" gnus-tmp-from)
4209                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4210                    (substring gnus-tmp-from 0 beg-match)))
4211               ((string-match "(.+)" gnus-tmp-from)
4212                (substring gnus-tmp-from
4213                           (1+ (match-beginning 0)) (1- (match-end 0))))
4214               (t gnus-tmp-from)))
4215             (when (string= gnus-tmp-name "")
4216               (setq gnus-tmp-name gnus-tmp-from))
4217             (unless (numberp gnus-tmp-lines)
4218               (setq gnus-tmp-lines 0))
4219             (gnus-put-text-property
4220              (point)
4221              (progn (eval gnus-summary-line-format-spec) (point))
4222              'gnus-number number)
4223             (when gnus-visual-p
4224               (forward-line -1)
4225               (gnus-run-hooks 'gnus-summary-update-hook)
4226               (forward-line 1))
4227
4228             (setq gnus-tmp-prev-subject subject)))
4229
4230         (when (nth 1 thread)
4231           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4232         (incf gnus-tmp-level)
4233         (setq threads (if thread-end nil (cdar thread)))
4234         (unless threads
4235           (setq gnus-tmp-level 0)))))
4236   (gnus-message 7 "Generating summary...done"))
4237
4238 (defun gnus-summary-prepare-unthreaded (headers)
4239   "Generate an unthreaded summary buffer based on HEADERS."
4240   (let (header number mark)
4241
4242     (beginning-of-line)
4243
4244     (while headers
4245       ;; We may have to root out some bad articles...
4246       (when (memq (setq number (mail-header-number
4247                                 (setq header (pop headers))))
4248                   gnus-newsgroup-limit)
4249         ;; Mark article as read when it has a low score.
4250         (when (and gnus-summary-mark-below
4251                    (< (or (cdr (assq number gnus-newsgroup-scored))
4252                           gnus-summary-default-score 0)
4253                       gnus-summary-mark-below)
4254                    (not (gnus-summary-article-ancient-p number)))
4255           (setq gnus-newsgroup-unreads
4256                 (delq number gnus-newsgroup-unreads))
4257           (if gnus-newsgroup-auto-expire
4258               (push number gnus-newsgroup-expirable)
4259             (push (cons number gnus-low-score-mark)
4260                   gnus-newsgroup-reads)))
4261
4262         (setq mark (gnus-article-mark number))
4263         (push (gnus-data-make number mark (1+ (point)) header 0)
4264               gnus-newsgroup-data)
4265         (gnus-summary-insert-line
4266          header 0 number
4267          mark (memq number gnus-newsgroup-replied)
4268          (memq number gnus-newsgroup-expirable)
4269          (mail-header-subject header) nil
4270          (cdr (assq number gnus-newsgroup-scored))
4271          (memq number gnus-newsgroup-processable))))))
4272
4273 (defun gnus-summary-remove-list-identifiers ()
4274   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4275   (let ((regexp (if (stringp gnus-list-identifiers)
4276                     gnus-list-identifiers
4277                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4278     (dolist (header gnus-newsgroup-headers)
4279       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4280                                   " *\\)\\)+\\(Re: +\\)?\\)")
4281                           (mail-header-subject header))
4282         (mail-header-set-subject
4283          header (concat (substring (mail-header-subject header)
4284                                    0 (match-beginning 1))
4285                         (or
4286                          (match-string 3 (mail-header-subject header))
4287                          (match-string 5 (mail-header-subject header)))
4288                         (substring (mail-header-subject header)
4289                                    (match-end 1))))))))
4290
4291 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4292   "Select newsgroup GROUP.
4293 If READ-ALL is non-nil, all articles in the group are selected.
4294 If SELECT-ARTICLES, only select those articles from GROUP."
4295   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4296          ;;!!! Dirty hack; should be removed.
4297          (gnus-summary-ignore-duplicates
4298           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4299               t
4300             gnus-summary-ignore-duplicates))
4301          (info (nth 2 entry))
4302          articles fetched-articles cached)
4303
4304     (unless (gnus-check-server
4305              (setq gnus-current-select-method
4306                    (gnus-find-method-for-group group)))
4307       (error "Couldn't open server"))
4308
4309     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4310         (gnus-activate-group group)     ; Or we can activate it...
4311         (progn                          ; Or we bug out.
4312           (when (equal major-mode 'gnus-summary-mode)
4313             (kill-buffer (current-buffer)))
4314           (error "Couldn't activate group %s: %s"
4315                  group (gnus-status-message group))))
4316
4317     (unless (gnus-request-group group t)
4318       (when (equal major-mode 'gnus-summary-mode)
4319         (kill-buffer (current-buffer)))
4320       (error "Couldn't request group %s: %s"
4321              group (gnus-status-message group)))
4322
4323     (setq gnus-newsgroup-name group)
4324     (setq gnus-newsgroup-unselected nil)
4325     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4326     (gnus-summary-setup-default-charset)
4327
4328     ;; Adjust and set lists of article marks.
4329     (when info
4330       (gnus-adjust-marked-articles info))
4331
4332     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4333     (setq cached
4334           (if (gnus-virtual-group-p group)
4335               gnus-newsgroup-cached
4336             (gnus-cache-articles-in-group group)))
4337
4338     (setq gnus-newsgroup-unreads
4339           (gnus-set-difference
4340            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4341            gnus-newsgroup-dormant))
4342
4343     (setq gnus-newsgroup-processable nil)
4344
4345     (gnus-update-read-articles group gnus-newsgroup-unreads)
4346
4347     (if (setq articles select-articles)
4348         (setq gnus-newsgroup-unselected
4349               (gnus-sorted-intersection
4350                gnus-newsgroup-unreads
4351                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4352       (setq articles (gnus-articles-to-read group read-all)))
4353
4354     (cond
4355      ((null articles)
4356       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4357       'quit)
4358      ((eq articles 0) nil)
4359      (t
4360       ;; Init the dependencies hash table.
4361       (setq gnus-newsgroup-dependencies
4362             (gnus-make-hashtable (length articles)))
4363       (gnus-set-global-variables)
4364       ;; Retrieve the headers and read them in.
4365       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4366       (setq gnus-newsgroup-headers
4367             (gnus-retrieve-parsed-headers
4368              articles gnus-newsgroup-name
4369              ;; We might want to fetch old headers, but
4370              ;; not if there is only 1 article.
4371              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4372                            (not (numberp gnus-fetch-old-headers)))
4373                       (> (length articles) 1))
4374                   gnus-fetch-old-headers)))
4375       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4376
4377       ;; Suppress duplicates?
4378       (when gnus-suppress-duplicates
4379         (gnus-dup-suppress-articles))
4380
4381       ;; Set the initial limit.
4382       (setq gnus-newsgroup-limit (copy-sequence articles))
4383       ;; Remove canceled articles from the list of unread articles.
4384       (setq gnus-newsgroup-unreads
4385             (gnus-set-sorted-intersection
4386              gnus-newsgroup-unreads
4387              (setq fetched-articles
4388                    (mapcar (lambda (headers) (mail-header-number headers))
4389                            gnus-newsgroup-headers))))
4390       ;; Removed marked articles that do not exist.
4391       (gnus-update-missing-marks
4392        (gnus-sorted-complement fetched-articles articles))
4393
4394       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4395       (when cached
4396         (setq gnus-newsgroup-cached cached))
4397
4398       ;; We might want to build some more threads first.
4399       (when (and gnus-fetch-old-headers
4400                  (eq gnus-headers-retrieved-by 'nov))
4401         (if (eq gnus-fetch-old-headers 'invisible)
4402             (gnus-build-all-threads)
4403           (gnus-build-old-threads)))
4404       ;; Let the Gnus agent mark articles as read.
4405       (when gnus-agent
4406         (gnus-agent-get-undownloaded-list))
4407       ;; Remove list identifiers from subject
4408       (when gnus-list-identifiers
4409         (gnus-summary-remove-list-identifiers))
4410       ;; Check whether auto-expire is to be done in this group.
4411       (setq gnus-newsgroup-auto-expire
4412             (gnus-group-auto-expirable-p group))
4413       ;; Set up the article buffer now, if necessary.
4414       (unless gnus-single-article-buffer
4415         (gnus-article-setup-buffer))
4416       ;; First and last article in this newsgroup.
4417       (when gnus-newsgroup-headers
4418         (setq gnus-newsgroup-begin
4419               (mail-header-number (car gnus-newsgroup-headers))
4420               gnus-newsgroup-end
4421               (mail-header-number
4422                (gnus-last-element gnus-newsgroup-headers))))
4423       ;; GROUP is successfully selected.
4424       (or gnus-newsgroup-headers t)))))
4425
4426 (defun gnus-articles-to-read (group &optional read-all)
4427   "Find out what articles the user wants to read."
4428   (let* ((articles
4429           ;; Select all articles if `read-all' is non-nil, or if there
4430           ;; are no unread articles.
4431           (if (or read-all
4432                   (and (zerop (length gnus-newsgroup-marked))
4433                        (zerop (length gnus-newsgroup-unreads)))
4434                   (eq (gnus-group-find-parameter group 'display)
4435                       'all))
4436               (or
4437                (gnus-uncompress-range (gnus-active group))
4438                (gnus-cache-articles-in-group group))
4439             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4440                           (copy-sequence gnus-newsgroup-unreads))
4441                   '<)))
4442          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4443          (scored (length scored-list))
4444          (number (length articles))
4445          (marked (+ (length gnus-newsgroup-marked)
4446                     (length gnus-newsgroup-dormant)))
4447          (select
4448           (cond
4449            ((numberp read-all)
4450             read-all)
4451            (t
4452             (condition-case ()
4453                 (cond
4454                  ((and (or (<= scored marked) (= scored number))
4455                        (natnump gnus-large-newsgroup)
4456                        (> number gnus-large-newsgroup))
4457                   (let* ((cursor-in-echo-area nil)
4458                          (input (read-from-minibuffer
4459                                  (format
4460                                   "How many articles from %s (max %d): "
4461                                   (gnus-limit-string gnus-newsgroup-name 35)
4462                                   number)
4463                                  (cons (number-to-string gnus-large-newsgroup)
4464                                        0))))
4465                     (if (string-match "^[ \t]*$" input)
4466                         number
4467                       input)))
4468                  ((and (> scored marked) (< scored number)
4469                        (> (- scored number) 20))
4470                   (let ((input
4471                          (read-string
4472                           (format "%s %s (%d scored, %d total): "
4473                                   "How many articles from"
4474                                   group scored number))))
4475                     (if (string-match "^[ \t]*$" input)
4476                         number input)))
4477                  (t number))
4478               (quit
4479                (message "Quit getting the articles to read")
4480                nil))))))
4481     (setq select (if (stringp select) (string-to-number select) select))
4482     (if (or (null select) (zerop select))
4483         select
4484       (if (and (not (zerop scored)) (<= (abs select) scored))
4485           (progn
4486             (setq articles (sort scored-list '<))
4487             (setq number (length articles)))
4488         (setq articles (copy-sequence articles)))
4489
4490       (when (< (abs select) number)
4491         (if (< select 0)
4492             ;; Select the N oldest articles.
4493             (setcdr (nthcdr (1- (abs select)) articles) nil)
4494           ;; Select the N most recent articles.
4495           (setq articles (nthcdr (- number select) articles))))
4496       (setq gnus-newsgroup-unselected
4497             (gnus-sorted-intersection
4498              gnus-newsgroup-unreads
4499              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4500       (when gnus-alter-articles-to-read-function
4501         (setq gnus-newsgroup-unreads
4502               (sort
4503                (funcall gnus-alter-articles-to-read-function
4504                         gnus-newsgroup-name gnus-newsgroup-unreads)
4505                '<)))
4506       articles)))
4507
4508 (defun gnus-killed-articles (killed articles)
4509   (let (out)
4510     (while articles
4511       (when (inline (gnus-member-of-range (car articles) killed))
4512         (push (car articles) out))
4513       (setq articles (cdr articles)))
4514     out))
4515
4516 (defun gnus-uncompress-marks (marks)
4517   "Uncompress the mark ranges in MARKS."
4518   (let ((uncompressed '(score bookmark))
4519         out)
4520     (while marks
4521       (if (memq (caar marks) uncompressed)
4522           (push (car marks) out)
4523         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4524       (setq marks (cdr marks)))
4525     out))
4526
4527 (defun gnus-adjust-marked-articles (info)
4528   "Set all article lists and remove all marks that are no longer valid."
4529   (let* ((marked-lists (gnus-info-marks info))
4530          (active (gnus-active (gnus-info-group info)))
4531          (min (car active))
4532          (max (cdr active))
4533          (types gnus-article-mark-lists)
4534          (uncompressed '(score bookmark killed))
4535          marks var articles article mark)
4536
4537     (while marked-lists
4538       (setq marks (pop marked-lists))
4539       (set (setq var (intern (format "gnus-newsgroup-%s"
4540                                      (car (rassq (setq mark (car marks))
4541                                                  types)))))
4542            (if (memq (car marks) uncompressed) (cdr marks)
4543              (gnus-uncompress-range (cdr marks))))
4544
4545       (setq articles (symbol-value var))
4546
4547       ;; All articles have to be subsets of the active articles.
4548       (cond
4549        ;; Adjust "simple" lists.
4550        ((memq mark '(tick dormant expire reply save))
4551         (while articles
4552           (when (or (< (setq article (pop articles)) min) (> article max))
4553             (set var (delq article (symbol-value var))))))
4554        ;; Adjust assocs.
4555        ((memq mark uncompressed)
4556         (when (not (listp (cdr (symbol-value var))))
4557           (set var (list (symbol-value var))))
4558         (when (not (listp (cdr articles)))
4559           (setq articles (list articles)))
4560         (while articles
4561           (when (or (not (consp (setq article (pop articles))))
4562                     (< (car article) min)
4563                     (> (car article) max))
4564             (set var (delq article (symbol-value var))))))))))
4565
4566 (defun gnus-update-missing-marks (missing)
4567   "Go through the list of MISSING articles and remove them from the mark lists."
4568   (when missing
4569     (let ((types gnus-article-mark-lists)
4570           var m)
4571       ;; Go through all types.
4572       (while types
4573         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4574         (when (symbol-value var)
4575           ;; This list has articles.  So we delete all missing articles
4576           ;; from it.
4577           (setq m missing)
4578           (while m
4579             (set var (delq (pop m) (symbol-value var)))))))))
4580
4581 (defun gnus-update-marks ()
4582   "Enter the various lists of marked articles into the newsgroup info list."
4583   (let ((types gnus-article-mark-lists)
4584         (info (gnus-get-info gnus-newsgroup-name))
4585         (uncompressed '(score bookmark killed))
4586         type list newmarked symbol delta-marks)
4587     (when info
4588       ;; Add all marks lists to the list of marks lists.
4589       (while (setq type (pop types))
4590         (setq list (symbol-value
4591                     (setq symbol
4592                           (intern (format "gnus-newsgroup-%s"
4593                                           (car type))))))
4594
4595         (when list
4596           ;; Get rid of the entries of the articles that have the
4597           ;; default score.
4598           (when (and (eq (cdr type) 'score)
4599                      gnus-save-score
4600                      list)
4601             (let* ((arts list)
4602                    (prev (cons nil list))
4603                    (all prev))
4604               (while arts
4605                 (if (or (not (consp (car arts)))
4606                         (= (cdar arts) gnus-summary-default-score))
4607                     (setcdr prev (cdr arts))
4608                   (setq prev arts))
4609                 (setq arts (cdr arts)))
4610               (setq list (cdr all)))))
4611
4612         (unless (memq (cdr type) uncompressed)
4613           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4614
4615         (when (gnus-check-backend-function
4616                'request-set-mark gnus-newsgroup-name)
4617           ;; propagate flags to server, with the following exceptions:
4618           ;; uncompressed:s are not proper flags (they are cons cells)
4619           ;; cache is a internal gnus flag
4620           ;; download are local to one gnus installation (well)
4621           ;; unsend are for nndraft groups only
4622           ;; xxx: generality of this?  this suits nnimap anyway
4623           (unless (memq (cdr type) (append '(cache download unsend)
4624                                            uncompressed))
4625             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4626                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4627                    (add (gnus-remove-from-range
4628                          (gnus-copy-sequence list) old)))
4629               (when add
4630                 (push (list add 'add (list (cdr type))) delta-marks))
4631               (when del
4632                 (push (list del 'del (list (cdr type))) delta-marks)))))
4633
4634         (when list
4635           (push (cons (cdr type) list) newmarked)))
4636
4637       (when delta-marks
4638         (unless (gnus-check-group gnus-newsgroup-name)
4639           (error "Can't open server for %s" gnus-newsgroup-name))
4640         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4641
4642       ;; Enter these new marks into the info of the group.
4643       (if (nthcdr 3 info)
4644           (setcar (nthcdr 3 info) newmarked)
4645         ;; Add the marks lists to the end of the info.
4646         (when newmarked
4647           (setcdr (nthcdr 2 info) (list newmarked))))
4648
4649       ;; Cut off the end of the info if there's nothing else there.
4650       (let ((i 5))
4651         (while (and (> i 2)
4652                     (not (nth i info)))
4653           (when (nthcdr (decf i) info)
4654             (setcdr (nthcdr i info) nil)))))))
4655
4656 (defun gnus-set-mode-line (where)
4657   "Set the mode line of the article or summary buffers.
4658 If WHERE is `summary', the summary mode line format will be used."
4659   ;; Is this mode line one we keep updated?
4660   (when (and (memq where gnus-updated-mode-lines)
4661              (symbol-value
4662               (intern (format "gnus-%s-mode-line-format-spec" where))))
4663     (let (mode-string)
4664       (save-excursion
4665         ;; We evaluate this in the summary buffer since these
4666         ;; variables are buffer-local to that buffer.
4667         (set-buffer gnus-summary-buffer)
4668         ;; We bind all these variables that are used in the `eval' form
4669         ;; below.
4670         (let* ((mformat (symbol-value
4671                          (intern
4672                           (format "gnus-%s-mode-line-format-spec" where))))
4673                (gnus-tmp-group-name (gnus-group-name-decode
4674                                      gnus-newsgroup-name
4675                                      (gnus-group-name-charset
4676                                       nil
4677                                       gnus-newsgroup-name)))
4678                (gnus-tmp-article-number (or gnus-current-article 0))
4679                (gnus-tmp-unread gnus-newsgroup-unreads)
4680                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4681                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4682                (gnus-tmp-unread-and-unselected
4683                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4684                             (zerop gnus-tmp-unselected))
4685                        "")
4686                       ((zerop gnus-tmp-unselected)
4687                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4688                       (t (format "{%d(+%d) more}"
4689                                  gnus-tmp-unread-and-unticked
4690                                  gnus-tmp-unselected))))
4691                (gnus-tmp-subject
4692                 (if (and gnus-current-headers
4693                          (vectorp gnus-current-headers))
4694                     (gnus-mode-string-quote
4695                      (mail-header-subject gnus-current-headers))
4696                   ""))
4697                bufname-length max-len
4698                gnus-tmp-header);; passed as argument to any user-format-funcs
4699           (setq mode-string (eval mformat))
4700           (setq bufname-length (if (string-match "%b" mode-string)
4701                                    (- (length
4702                                        (buffer-name
4703                                         (if (eq where 'summary)
4704                                             nil
4705                                           (get-buffer gnus-article-buffer))))
4706                                       2)
4707                                  0))
4708           (setq max-len (max 4 (if gnus-mode-non-string-length
4709                                    (- (window-width)
4710                                       gnus-mode-non-string-length
4711                                       bufname-length)
4712                                  (length mode-string))))
4713           ;; We might have to chop a bit of the string off...
4714           (when (> (length mode-string) max-len)
4715             (setq mode-string
4716                   (concat (gnus-truncate-string mode-string (- max-len 3))
4717                           "...")))
4718           ;; Pad the mode string a bit.
4719           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4720       ;; Update the mode line.
4721       (setq mode-line-buffer-identification
4722             (gnus-mode-line-buffer-identification (list mode-string)))
4723       (set-buffer-modified-p t))))
4724
4725 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4726   "Go through the HEADERS list and add all Xrefs to a hash table.
4727 The resulting hash table is returned, or nil if no Xrefs were found."
4728   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4729          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4730          (xref-hashtb (gnus-make-hashtable))
4731          start group entry number xrefs header)
4732     (while headers
4733       (setq header (pop headers))
4734       (when (and (setq xrefs (mail-header-xref header))
4735                  (not (memq (setq number (mail-header-number header))
4736                             unreads)))
4737         (setq start 0)
4738         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4739           (setq start (match-end 0))
4740           (setq group (if prefix
4741                           (concat prefix (substring xrefs (match-beginning 1)
4742                                                     (match-end 1)))
4743                         (substring xrefs (match-beginning 1) (match-end 1))))
4744           (setq number
4745                 (string-to-int (substring xrefs (match-beginning 2)
4746                                           (match-end 2))))
4747           (if (setq entry (gnus-gethash group xref-hashtb))
4748               (setcdr entry (cons number (cdr entry)))
4749             (gnus-sethash group (cons number nil) xref-hashtb)))))
4750     (and start xref-hashtb)))
4751
4752 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4753   "Look through all the headers and mark the Xrefs as read."
4754   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4755         name entry info xref-hashtb idlist method nth4)
4756     (save-excursion
4757       (set-buffer gnus-group-buffer)
4758       (when (setq xref-hashtb
4759                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4760         (mapatoms
4761          (lambda (group)
4762            (unless (string= from-newsgroup (setq name (symbol-name group)))
4763              (setq idlist (symbol-value group))
4764              ;; Dead groups are not updated.
4765              (and (prog1
4766                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4767                             info (nth 2 entry))
4768                     (when (stringp (setq nth4 (gnus-info-method info)))
4769                       (setq nth4 (gnus-server-to-method nth4))))
4770                   ;; Only do the xrefs if the group has the same
4771                   ;; select method as the group we have just read.
4772                   (or (gnus-methods-equal-p
4773                        nth4 (gnus-find-method-for-group from-newsgroup))
4774                       virtual
4775                       (equal nth4 (setq method (gnus-find-method-for-group
4776                                                 from-newsgroup)))
4777                       (and (equal (car nth4) (car method))
4778                            (equal (nth 1 nth4) (nth 1 method))))
4779                   gnus-use-cross-reference
4780                   (or (not (eq gnus-use-cross-reference t))
4781                       virtual
4782                       ;; Only do cross-references on subscribed
4783                       ;; groups, if that is what is wanted.
4784                       (<= (gnus-info-level info) gnus-level-subscribed))
4785                   (gnus-group-make-articles-read name idlist))))
4786          xref-hashtb)))))
4787
4788 (defun gnus-compute-read-articles (group articles)
4789   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4790          (info (nth 2 entry))
4791          (active (gnus-active group))
4792          ninfo)
4793     (when entry
4794       ;; First peel off all invalid article numbers.
4795       (when active
4796         (let ((ids articles)
4797               id first)
4798           (while (setq id (pop ids))
4799             (when (and first (> id (cdr active)))
4800               ;; We'll end up in this situation in one particular
4801               ;; obscure situation.  If you re-scan a group and get
4802               ;; a new article that is cross-posted to a different
4803               ;; group that has not been re-scanned, you might get
4804               ;; crossposted article that has a higher number than
4805               ;; Gnus believes possible.  So we re-activate this
4806               ;; group as well.  This might mean doing the
4807               ;; crossposting thingy will *increase* the number
4808               ;; of articles in some groups.  Tsk, tsk.
4809               (setq active (or (gnus-activate-group group) active)))
4810             (when (or (> id (cdr active))
4811                       (< id (car active)))
4812               (setq articles (delq id articles))))))
4813       ;; If the read list is nil, we init it.
4814       (if (and active
4815                (null (gnus-info-read info))
4816                (> (car active) 1))
4817           (setq ninfo (cons 1 (1- (car active))))
4818         (setq ninfo (gnus-info-read info)))
4819       ;; Then we add the read articles to the range.
4820       (gnus-add-to-range
4821        ninfo (setq articles (sort articles '<))))))
4822
4823 (defun gnus-group-make-articles-read (group articles)
4824   "Update the info of GROUP to say that ARTICLES are read."
4825   (let* ((num 0)
4826          (entry (gnus-gethash group gnus-newsrc-hashtb))
4827          (info (nth 2 entry))
4828          (active (gnus-active group))
4829          range)
4830     (when entry
4831       (setq range (gnus-compute-read-articles group articles))
4832       (save-excursion
4833         (set-buffer gnus-group-buffer)
4834         (gnus-undo-register
4835           `(progn
4836              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4837              (gnus-info-set-read ',info ',(gnus-info-read info))
4838              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4839              (gnus-group-update-group ,group t))))
4840       ;; Add the read articles to the range.
4841       (gnus-info-set-read info range)
4842       ;; Then we have to re-compute how many unread
4843       ;; articles there are in this group.
4844       (when active
4845         (cond
4846          ((not range)
4847           (setq num (- (1+ (cdr active)) (car active))))
4848          ((not (listp (cdr range)))
4849           (setq num (- (cdr active) (- (1+ (cdr range))
4850                                        (car range)))))
4851          (t
4852           (while range
4853             (if (numberp (car range))
4854                 (setq num (1+ num))
4855               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4856             (setq range (cdr range)))
4857           (setq num (- (cdr active) num))))
4858         ;; Update the number of unread articles.
4859         (setcar entry num)
4860         ;; Update the group buffer.
4861         (gnus-group-update-group group t)))))
4862
4863 (defvar gnus-newsgroup-none-id 0)
4864
4865 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4866   (let ((cur nntp-server-buffer)
4867         (dependencies
4868          (or dependencies
4869              (save-excursion (set-buffer gnus-summary-buffer)
4870                              gnus-newsgroup-dependencies)))
4871         headers id end ref
4872         (mail-parse-charset gnus-newsgroup-charset)
4873         (mail-parse-ignored-charsets
4874          (save-excursion (condition-case nil
4875                              (set-buffer gnus-summary-buffer)
4876                            (error))
4877                          gnus-newsgroup-ignored-charsets)))
4878     (save-excursion
4879       (set-buffer nntp-server-buffer)
4880       ;; Translate all TAB characters into SPACE characters.
4881       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4882       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4883       (gnus-run-hooks 'gnus-parse-headers-hook)
4884       (let ((case-fold-search t)
4885             in-reply-to header p lines chars ctype)
4886         (goto-char (point-min))
4887         ;; Search to the beginning of the next header.  Error messages
4888         ;; do not begin with 2 or 3.
4889         (while (re-search-forward "^[23][0-9]+ " nil t)
4890           (setq id nil
4891                 ref nil)
4892           ;; This implementation of this function, with nine
4893           ;; search-forwards instead of the one re-search-forward and
4894           ;; a case (which basically was the old function) is actually
4895           ;; about twice as fast, even though it looks messier.  You
4896           ;; can't have everything, I guess.  Speed and elegance
4897           ;; doesn't always go hand in hand.
4898           (setq
4899            header
4900            (make-full-mail-header
4901             ;; Number.
4902             (prog1
4903                 (read cur)
4904               (end-of-line)
4905               (setq p (point))
4906               (narrow-to-region (point)
4907                                 (or (and (search-forward "\n.\n" nil t)
4908                                          (- (point) 2))
4909                                     (point))))
4910             ;; Subject.
4911             (progn
4912               (goto-char p)
4913               (if (search-forward "\nsubject: " nil t)
4914                   (nnheader-header-value)
4915                 "(none)"))
4916             ;; From.
4917             (progn
4918               (goto-char p)
4919               (if (or (search-forward "\nfrom: " nil t)
4920                       (search-forward "\nfrom:" nil t))
4921                   (nnheader-header-value)
4922                 "(nobody)"))
4923             ;; Date.
4924             (progn
4925               (goto-char p)
4926               (if (search-forward "\ndate: " nil t)
4927                   (nnheader-header-value)
4928                 ""))
4929             ;; Message-ID.
4930             (progn
4931               (goto-char p)
4932               (setq id (if (re-search-forward
4933                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4934                            ;; We do it this way to make sure the Message-ID
4935                            ;; is (somewhat) syntactically valid.
4936                            (buffer-substring (match-beginning 1)
4937                                              (match-end 1))
4938                          ;; If there was no message-id, we just fake one
4939                          ;; to make subsequent routines simpler.
4940                          (nnheader-generate-fake-message-id))))
4941             ;; References.
4942             (progn
4943               (goto-char p)
4944               (if (search-forward "\nreferences: " nil t)
4945                   (progn
4946                     (setq end (point))
4947                     (prog1
4948                         (nnheader-header-value)
4949                       (setq ref
4950                             (buffer-substring
4951                              (progn
4952                                ;; (end-of-line)
4953                                (search-backward ">" end t)
4954                                (1+ (point)))
4955                              (progn
4956                                (search-backward "<" end t)
4957                                (point))))))
4958                 ;; Get the references from the in-reply-to header if there
4959                 ;; were no references and the in-reply-to header looks
4960                 ;; promising.
4961                 (if (and (search-forward "\nin-reply-to: " nil t)
4962                          (setq in-reply-to (nnheader-header-value))
4963                          (string-match "<[^>]+>" in-reply-to))
4964                     (let (ref2)
4965                       (setq ref (substring in-reply-to (match-beginning 0)
4966                                            (match-end 0)))
4967                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4968                         (setq ref2 (substring in-reply-to (match-beginning 0)
4969                                               (match-end 0)))
4970                         (when (> (length ref2) (length ref))
4971                           (setq ref ref2)))
4972                       ref)
4973                   (setq ref nil))))
4974             ;; Chars.
4975             (progn
4976               (goto-char p)
4977               (if (search-forward "\nchars: " nil t)
4978                   (if (numberp (setq chars (ignore-errors (read cur))))
4979                       chars 0)
4980                 0))
4981             ;; Lines.
4982             (progn
4983               (goto-char p)
4984               (if (search-forward "\nlines: " nil t)
4985                   (if (numberp (setq lines (ignore-errors (read cur))))
4986                       lines 0)
4987                 0))
4988             ;; Xref.
4989             (progn
4990               (goto-char p)
4991               (and (search-forward "\nxref: " nil t)
4992                    (nnheader-header-value)))
4993             ;; Extra.
4994             (when gnus-extra-headers
4995               (let ((extra gnus-extra-headers)
4996                     out)
4997                 (while extra
4998                   (goto-char p)
4999                   (when (search-forward
5000                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5001                     (push (cons (car extra) (nnheader-header-value)) out))
5002                   (pop extra))
5003                 out))))
5004           (goto-char p)
5005           (if (and (search-forward "\ncontent-type: " nil t)
5006                    (setq ctype (nnheader-header-value)))
5007               (mime-entity-set-content-type-internal
5008                header (mime-parse-Content-Type ctype)))
5009           (when (equal id ref)
5010             (setq ref nil))
5011
5012           (when gnus-alter-header-function
5013             (funcall gnus-alter-header-function header)
5014             (setq id (mail-header-id header)
5015                   ref (gnus-parent-id (mail-header-references header))))
5016
5017           (when (setq header
5018                       (gnus-dependencies-add-header
5019                        header dependencies force-new))
5020             (push header headers))
5021           (goto-char (point-max))
5022           (widen))
5023         (nreverse headers)))))
5024
5025 ;; Goes through the xover lines and returns a list of vectors
5026 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5027                                                   force-new dependencies
5028                                                   group also-fetch-heads)
5029   "Parse the news overview data in the server buffer.
5030 Return a list of headers that match SEQUENCE (see
5031 `nntp-retrieve-headers')."
5032   ;; Get the Xref when the users reads the articles since most/some
5033   ;; NNTP servers do not include Xrefs when using XOVER.
5034   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5035   (let ((mail-parse-charset gnus-newsgroup-charset)
5036         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5037         (cur nntp-server-buffer)
5038         (dependencies (or dependencies gnus-newsgroup-dependencies))
5039         number headers header)
5040     (save-excursion
5041       (set-buffer nntp-server-buffer)
5042       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5043       ;; Allow the user to mangle the headers before parsing them.
5044       (gnus-run-hooks 'gnus-parse-headers-hook)
5045       (goto-char (point-min))
5046       (while (not (eobp))
5047         (condition-case ()
5048             (while (and sequence (not (eobp)))
5049               (setq number (read cur))
5050               (while (and sequence
5051                           (< (car sequence) number))
5052                 (setq sequence (cdr sequence)))
5053               (and sequence
5054                    (eq number (car sequence))
5055                    (progn
5056                      (setq sequence (cdr sequence))
5057                      (setq header (inline
5058                                     (gnus-nov-parse-line
5059                                      number dependencies force-new))))
5060                    (push header headers))
5061               (forward-line 1))
5062           (error
5063            (gnus-error 4 "Strange nov line (%d)"
5064                        (count-lines (point-min) (point)))))
5065         (forward-line 1))
5066       ;; A common bug in inn is that if you have posted an article and
5067       ;; then retrieves the active file, it will answer correctly --
5068       ;; the new article is included.  However, a NOV entry for the
5069       ;; article may not have been generated yet, so this may fail.
5070       ;; We work around this problem by retrieving the last few
5071       ;; headers using HEAD.
5072       (if (or (not also-fetch-heads)
5073               (not sequence))
5074           ;; We (probably) got all the headers.
5075           (nreverse headers)
5076         (let ((gnus-nov-is-evil t))
5077           (nconc
5078            (nreverse headers)
5079            (gnus-retrieve-parsed-headers sequence group)
5080            ))))))
5081
5082 (defun gnus-article-get-xrefs ()
5083   "Fill in the Xref value in `gnus-current-headers', if necessary.
5084 This is meant to be called in `gnus-article-internal-prepare-hook'."
5085   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5086                                  gnus-current-headers)))
5087     (or (not gnus-use-cross-reference)
5088         (not headers)
5089         (and (mail-header-xref headers)
5090              (not (string= (mail-header-xref headers) "")))
5091         (let ((case-fold-search t)
5092               xref)
5093           (save-restriction
5094             (nnheader-narrow-to-headers)
5095             (goto-char (point-min))
5096             (when (or (and (not (eobp))
5097                            (eq (downcase (char-after)) ?x)
5098                            (looking-at "Xref:"))
5099                       (search-forward "\nXref:" nil t))
5100               (goto-char (1+ (match-end 0)))
5101               (setq xref (buffer-substring (point)
5102                                            (progn (end-of-line) (point))))
5103               (mail-header-set-xref headers xref)))))))
5104
5105 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5106   "Find article ID and insert the summary line for that article.
5107 OLD-HEADER can either be a header or a line number to insert
5108 the subject line on."
5109   (let* ((line (and (numberp old-header) old-header))
5110          (old-header (and (vectorp old-header) old-header))
5111          (header (cond ((and old-header use-old-header)
5112                         old-header)
5113                        ((and (numberp id)
5114                              (gnus-number-to-header id))
5115                         (gnus-number-to-header id))
5116                        (t
5117                         (gnus-read-header id))))
5118          (number (and (numberp id) id))
5119          d)
5120     (when header
5121       ;; Rebuild the thread that this article is part of and go to the
5122       ;; article we have fetched.
5123       (when (and (not gnus-show-threads)
5124                  old-header)
5125         (when (and number
5126                    (setq d (gnus-data-find (mail-header-number old-header))))
5127           (goto-char (gnus-data-pos d))
5128           (gnus-data-remove
5129            number
5130            (- (gnus-point-at-bol)
5131               (prog1
5132                   (1+ (gnus-point-at-eol))
5133                 (gnus-delete-line))))))
5134       (when old-header
5135         (mail-header-set-number header (mail-header-number old-header)))
5136       (setq gnus-newsgroup-sparse
5137             (delq (setq number (mail-header-number header))
5138                   gnus-newsgroup-sparse))
5139       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5140       (push number gnus-newsgroup-limit)
5141       (gnus-rebuild-thread (mail-header-id header) line)
5142       (gnus-summary-goto-subject number nil t))
5143     (when (and (numberp number)
5144                (> number 0))
5145       ;; We have to update the boundaries even if we can't fetch the
5146       ;; article if ID is a number -- so that the next `P' or `N'
5147       ;; command will fetch the previous (or next) article even
5148       ;; if the one we tried to fetch this time has been canceled.
5149       (when (> number gnus-newsgroup-end)
5150         (setq gnus-newsgroup-end number))
5151       (when (< number gnus-newsgroup-begin)
5152         (setq gnus-newsgroup-begin number))
5153       (setq gnus-newsgroup-unselected
5154             (delq number gnus-newsgroup-unselected)))
5155     ;; Report back a success?
5156     (and header (mail-header-number header))))
5157
5158 ;;; Process/prefix in the summary buffer
5159
5160 (defun gnus-summary-work-articles (n)
5161   "Return a list of articles to be worked upon.
5162 The prefix argument, the list of process marked articles, and the
5163 current article will be taken into consideration."
5164   (save-excursion
5165     (set-buffer gnus-summary-buffer)
5166     (cond
5167      (n
5168       ;; A numerical prefix has been given.
5169       (setq n (prefix-numeric-value n))
5170       (let ((backward (< n 0))
5171             (n (abs (prefix-numeric-value n)))
5172             articles article)
5173         (save-excursion
5174           (while
5175               (and (> n 0)
5176                    (push (setq article (gnus-summary-article-number))
5177                          articles)
5178                    (if backward
5179                        (gnus-summary-find-prev nil article)
5180                      (gnus-summary-find-next nil article)))
5181             (decf n)))
5182         (nreverse articles)))
5183      ((and (gnus-region-active-p) (mark))
5184       (message "region active")
5185       ;; Work on the region between point and mark.
5186       (let ((max (max (point) (mark)))
5187             articles article)
5188         (save-excursion
5189           (goto-char (min (point) (mark)))
5190           (while
5191               (and
5192                (push (setq article (gnus-summary-article-number)) articles)
5193                (gnus-summary-find-next nil article)
5194                (< (point) max)))
5195           (nreverse articles))))
5196      (gnus-newsgroup-processable
5197       ;; There are process-marked articles present.
5198       ;; Save current state.
5199       (gnus-summary-save-process-mark)
5200       ;; Return the list.
5201       (reverse gnus-newsgroup-processable))
5202      (t
5203       ;; Just return the current article.
5204       (list (gnus-summary-article-number))))))
5205
5206 (defmacro gnus-summary-iterate (arg &rest forms)
5207   "Iterate over the process/prefixed articles and do FORMS.
5208 ARG is the interactive prefix given to the command.  FORMS will be
5209 executed with point over the summary line of the articles."
5210   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5211     `(let ((,articles (gnus-summary-work-articles ,arg)))
5212        (while ,articles
5213          (gnus-summary-goto-subject (car ,articles))
5214          ,@forms
5215          (pop ,articles)))))
5216
5217 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5218 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5219
5220 (defun gnus-summary-save-process-mark ()
5221   "Push the current set of process marked articles on the stack."
5222   (interactive)
5223   (push (copy-sequence gnus-newsgroup-processable)
5224         gnus-newsgroup-process-stack))
5225
5226 (defun gnus-summary-kill-process-mark ()
5227   "Push the current set of process marked articles on the stack and unmark."
5228   (interactive)
5229   (gnus-summary-save-process-mark)
5230   (gnus-summary-unmark-all-processable))
5231
5232 (defun gnus-summary-yank-process-mark ()
5233   "Pop the last process mark state off the stack and restore it."
5234   (interactive)
5235   (unless gnus-newsgroup-process-stack
5236     (error "Empty mark stack"))
5237   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5238
5239 (defun gnus-summary-process-mark-set (set)
5240   "Make SET into the current process marked articles."
5241   (gnus-summary-unmark-all-processable)
5242   (while set
5243     (gnus-summary-set-process-mark (pop set))))
5244
5245 ;;; Searching and stuff
5246
5247 (defun gnus-summary-search-group (&optional backward use-level)
5248   "Search for next unread newsgroup.
5249 If optional argument BACKWARD is non-nil, search backward instead."
5250   (save-excursion
5251     (set-buffer gnus-group-buffer)
5252     (when (gnus-group-search-forward
5253            backward nil (if use-level (gnus-group-group-level) nil))
5254       (gnus-group-group-name))))
5255
5256 (defun gnus-summary-best-group (&optional exclude-group)
5257   "Find the name of the best unread group.
5258 If EXCLUDE-GROUP, do not go to this group."
5259   (save-excursion
5260     (set-buffer gnus-group-buffer)
5261     (save-excursion
5262       (gnus-group-best-unread-group exclude-group))))
5263
5264 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5265   (if backward (gnus-summary-find-prev)
5266     (let* ((dummy (gnus-summary-article-intangible-p))
5267            (article (or article (gnus-summary-article-number)))
5268            (arts (gnus-data-find-list article))
5269            result)
5270       (when (and (not dummy)
5271                  (or (not gnus-summary-check-current)
5272                      (not unread)
5273                      (not (gnus-data-unread-p (car arts)))))
5274         (setq arts (cdr arts)))
5275       (when (setq result
5276                   (if unread
5277                       (progn
5278                         (while arts
5279                           (when (or (and undownloaded
5280                                          (eq gnus-undownloaded-mark
5281                                              (gnus-data-mark (car arts))))
5282                                     (gnus-data-unread-p (car arts)))
5283                             (setq result (car arts)
5284                                   arts nil))
5285                           (setq arts (cdr arts)))
5286                         result)
5287                     (car arts)))
5288         (goto-char (gnus-data-pos result))
5289         (gnus-data-number result)))))
5290
5291 (defun gnus-summary-find-prev (&optional unread article)
5292   (let* ((eobp (eobp))
5293          (article (or article (gnus-summary-article-number)))
5294          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5295          result)
5296     (when (and (not eobp)
5297                (or (not gnus-summary-check-current)
5298                    (not unread)
5299                    (not (gnus-data-unread-p (car arts)))))
5300       (setq arts (cdr arts)))
5301     (when (setq result
5302                 (if unread
5303                     (progn
5304                       (while arts
5305                         (when (gnus-data-unread-p (car arts))
5306                           (setq result (car arts)
5307                                 arts nil))
5308                         (setq arts (cdr arts)))
5309                       result)
5310                   (car arts)))
5311       (goto-char (gnus-data-pos result))
5312       (gnus-data-number result))))
5313
5314 (defun gnus-summary-find-subject (subject &optional unread backward article)
5315   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5316          (article (or article (gnus-summary-article-number)))
5317          (articles (gnus-data-list backward))
5318          (arts (gnus-data-find-list article articles))
5319          result)
5320     (when (or (not gnus-summary-check-current)
5321               (not unread)
5322               (not (gnus-data-unread-p (car arts))))
5323       (setq arts (cdr arts)))
5324     (while arts
5325       (and (or (not unread)
5326                (gnus-data-unread-p (car arts)))
5327            (vectorp (gnus-data-header (car arts)))
5328            (gnus-subject-equal
5329             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5330            (setq result (car arts)
5331                  arts nil))
5332       (setq arts (cdr arts)))
5333     (and result
5334          (goto-char (gnus-data-pos result))
5335          (gnus-data-number result))))
5336
5337 (defun gnus-summary-search-forward (&optional unread subject backward)
5338   "Search forward for an article.
5339 If UNREAD, look for unread articles.  If SUBJECT, look for
5340 articles with that subject.  If BACKWARD, search backward instead."
5341   (cond (subject (gnus-summary-find-subject subject unread backward))
5342         (backward (gnus-summary-find-prev unread))
5343         (t (gnus-summary-find-next unread))))
5344
5345 (defun gnus-recenter (&optional n)
5346   "Center point in window and redisplay frame.
5347 Also do horizontal recentering."
5348   (interactive "P")
5349   (when (and gnus-auto-center-summary
5350              (not (eq gnus-auto-center-summary 'vertical)))
5351     (gnus-horizontal-recenter))
5352   (recenter n))
5353
5354 (defun gnus-summary-recenter ()
5355   "Center point in the summary window.
5356 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5357 displayed, no centering will be performed."
5358   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5359   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5360   (interactive)
5361   (let* ((top (cond ((< (window-height) 4) 0)
5362                     ((< (window-height) 7) 1)
5363                     (t (if (numberp gnus-auto-center-summary)
5364                            gnus-auto-center-summary
5365                          2))))
5366          (height (1- (window-height)))
5367          (bottom (save-excursion (goto-char (point-max))
5368                                  (forward-line (- height))
5369                                  (point)))
5370          (window (get-buffer-window (current-buffer))))
5371     ;; The user has to want it.
5372     (when gnus-auto-center-summary
5373       (when (get-buffer-window gnus-article-buffer)
5374         ;; Only do recentering when the article buffer is displayed,
5375         ;; Set the window start to either `bottom', which is the biggest
5376         ;; possible valid number, or the second line from the top,
5377         ;; whichever is the least.
5378         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5379           (if (> bottom top-pos)
5380               ;; Keep the second line from the top visible
5381               (set-window-start window top-pos t)
5382             ;; Try to keep the bottom line visible; if it's partially
5383             ;; obscured, either scroll one more line to make it fully
5384             ;; visible, or revert to using TOP-POS.
5385             (save-excursion
5386               (goto-char (point-max))
5387               (forward-line -1)
5388               (let ((last-line-start (point)))
5389                 (goto-char bottom)
5390                 (set-window-start window (point) t)
5391                 (when (not (pos-visible-in-window-p last-line-start window))
5392                   (forward-line 1)
5393                   (set-window-start window (min (point) top-pos) t)))))))
5394       ;; Do horizontal recentering while we're at it.
5395       (when (and (get-buffer-window (current-buffer) t)
5396                  (not (eq gnus-auto-center-summary 'vertical)))
5397         (let ((selected (selected-window)))
5398           (select-window (get-buffer-window (current-buffer) t))
5399           (gnus-summary-position-point)
5400           (gnus-horizontal-recenter)
5401           (select-window selected))))))
5402
5403 (defun gnus-summary-jump-to-group (newsgroup)
5404   "Move point to NEWSGROUP in group mode buffer."
5405   ;; Keep update point of group mode buffer if visible.
5406   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5407       (save-window-excursion
5408         ;; Take care of tree window mode.
5409         (when (get-buffer-window gnus-group-buffer)
5410           (pop-to-buffer gnus-group-buffer))
5411         (gnus-group-jump-to-group newsgroup))
5412     (save-excursion
5413       ;; Take care of tree window mode.
5414       (if (get-buffer-window gnus-group-buffer)
5415           (pop-to-buffer gnus-group-buffer)
5416         (set-buffer gnus-group-buffer))
5417       (gnus-group-jump-to-group newsgroup))))
5418
5419 ;; This function returns a list of article numbers based on the
5420 ;; difference between the ranges of read articles in this group and
5421 ;; the range of active articles.
5422 (defun gnus-list-of-unread-articles (group)
5423   (let* ((read (gnus-info-read (gnus-get-info group)))
5424          (active (or (gnus-active group) (gnus-activate-group group)))
5425          (last (cdr active))
5426          first nlast unread)
5427     ;; If none are read, then all are unread.
5428     (if (not read)
5429         (setq first (car active))
5430       ;; If the range of read articles is a single range, then the
5431       ;; first unread article is the article after the last read
5432       ;; article.  Sounds logical, doesn't it?
5433       (if (and (not (listp (cdr read)))
5434                (or (< (car read) (car active))
5435                    (progn (setq read (list read))
5436                           nil)))
5437           (setq first (max (car active) (1+ (cdr read))))
5438         ;; `read' is a list of ranges.
5439         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5440                                   (caar read)))
5441                   1)
5442           (setq first (car active)))
5443         (while read
5444           (when first
5445             (while (< first nlast)
5446               (push first unread)
5447               (setq first (1+ first))))
5448           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5449           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5450           (setq read (cdr read)))))
5451     ;; And add the last unread articles.
5452     (while (<= first last)
5453       (push first unread)
5454       (setq first (1+ first)))
5455     ;; Return the list of unread articles.
5456     (delq 0 (nreverse unread))))
5457
5458 (defun gnus-list-of-read-articles (group)
5459   "Return a list of unread, unticked and non-dormant articles."
5460   (let* ((info (gnus-get-info group))
5461          (marked (gnus-info-marks info))
5462          (active (gnus-active group)))
5463     (and info active
5464          (gnus-set-difference
5465           (gnus-sorted-complement
5466            (gnus-uncompress-range active)
5467            (gnus-list-of-unread-articles group))
5468           (append
5469            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5470            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5471
5472 ;; Various summary commands
5473
5474 (defun gnus-summary-select-article-buffer ()
5475   "Reconfigure windows to show article buffer."
5476   (interactive)
5477   (if (not (gnus-buffer-live-p gnus-article-buffer))
5478       (error "There is no article buffer for this summary buffer")
5479     (gnus-configure-windows 'article)
5480     (select-window (get-buffer-window gnus-article-buffer))))
5481
5482 (defun gnus-summary-universal-argument (arg)
5483   "Perform any operation on all articles that are process/prefixed."
5484   (interactive "P")
5485   (let ((articles (gnus-summary-work-articles arg))
5486         func article)
5487     (if (eq
5488          (setq
5489           func
5490           (key-binding
5491            (read-key-sequence
5492             (substitute-command-keys
5493              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5494          'undefined)
5495         (gnus-error 1 "Undefined key")
5496       (save-excursion
5497         (while articles
5498           (gnus-summary-goto-subject (setq article (pop articles)))
5499           (let (gnus-newsgroup-processable)
5500             (command-execute func))
5501           (gnus-summary-remove-process-mark article)))))
5502   (gnus-summary-position-point))
5503
5504 (defun gnus-summary-toggle-truncation (&optional arg)
5505   "Toggle truncation of summary lines.
5506 With arg, turn line truncation on iff arg is positive."
5507   (interactive "P")
5508   (setq truncate-lines
5509         (if (null arg) (not truncate-lines)
5510           (> (prefix-numeric-value arg) 0)))
5511   (redraw-display))
5512
5513 (defun gnus-summary-reselect-current-group (&optional all rescan)
5514   "Exit and then reselect the current newsgroup.
5515 The prefix argument ALL means to select all articles."
5516   (interactive "P")
5517   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5518     (error "Ephemeral groups can't be reselected"))
5519   (let ((current-subject (gnus-summary-article-number))
5520         (group gnus-newsgroup-name))
5521     (setq gnus-newsgroup-begin nil)
5522     (gnus-summary-exit)
5523     ;; We have to adjust the point of group mode buffer because
5524     ;; point was moved to the next unread newsgroup by exiting.
5525     (gnus-summary-jump-to-group group)
5526     (when rescan
5527       (save-excursion
5528         (save-window-excursion
5529           ;; Don't show group contents.
5530           (set-window-start (selected-window) (point-max))
5531           (gnus-group-get-new-news-this-group 1))))
5532     (gnus-group-read-group all t)
5533     (gnus-summary-goto-subject current-subject nil t)))
5534
5535 (defun gnus-summary-rescan-group (&optional all)
5536   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5537   (interactive "P")
5538   (gnus-summary-reselect-current-group all t))
5539
5540 (defun gnus-summary-update-info (&optional non-destructive)
5541   (save-excursion
5542     (let ((group gnus-newsgroup-name))
5543       (when group
5544         (when gnus-newsgroup-kill-headers
5545           (setq gnus-newsgroup-killed
5546                 (gnus-compress-sequence
5547                  (nconc
5548                   (gnus-set-sorted-intersection
5549                    (gnus-uncompress-range gnus-newsgroup-killed)
5550                    (setq gnus-newsgroup-unselected
5551                          (sort gnus-newsgroup-unselected '<)))
5552                   (setq gnus-newsgroup-unreads
5553                         (sort gnus-newsgroup-unreads '<)))
5554                  t)))
5555         (unless (listp (cdr gnus-newsgroup-killed))
5556           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5557         (let ((headers gnus-newsgroup-headers))
5558           ;; Set the new ranges of read articles.
5559           (save-excursion
5560             (set-buffer gnus-group-buffer)
5561             (gnus-undo-force-boundary))
5562           (gnus-update-read-articles
5563            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5564           ;; Set the current article marks.
5565           (let ((gnus-newsgroup-scored
5566                  (if (and (not gnus-save-score)
5567                           (not non-destructive))
5568                      nil
5569                    gnus-newsgroup-scored)))
5570             (save-excursion
5571               (gnus-update-marks)))
5572           ;; Do the cross-ref thing.
5573           (when gnus-use-cross-reference
5574             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5575           ;; Do not switch windows but change the buffer to work.
5576           (set-buffer gnus-group-buffer)
5577           (unless (gnus-ephemeral-group-p group)
5578             (gnus-group-update-group group)))))))
5579
5580 (defun gnus-summary-save-newsrc (&optional force)
5581   "Save the current number of read/marked articles in the dribble buffer.
5582 The dribble buffer will then be saved.
5583 If FORCE (the prefix), also save the .newsrc file(s)."
5584   (interactive "P")
5585   (gnus-summary-update-info t)
5586   (if force
5587       (gnus-save-newsrc-file)
5588     (gnus-dribble-save)))
5589
5590 (defun gnus-summary-exit (&optional temporary)
5591   "Exit reading current newsgroup, and then return to group selection mode.
5592 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5593   (interactive)
5594   (gnus-set-global-variables)
5595   (gnus-kill-save-kill-buffer)
5596   (gnus-async-halt-prefetch)
5597   (let* ((group gnus-newsgroup-name)
5598          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5599          (mode major-mode)
5600          (group-point nil)
5601          (buf (current-buffer)))
5602     (unless quit-config
5603       ;; Do adaptive scoring, and possibly save score files.
5604       (when gnus-newsgroup-adaptive
5605         (gnus-score-adaptive))
5606       (when gnus-use-scoring
5607         (gnus-score-save)))
5608     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5609     ;; If we have several article buffers, we kill them at exit.
5610     (unless gnus-single-article-buffer
5611       (gnus-kill-buffer gnus-original-article-buffer)
5612       (setq gnus-article-current nil))
5613     (when gnus-use-cache
5614       (gnus-cache-possibly-remove-articles)
5615       (gnus-cache-save-buffers))
5616     (gnus-async-prefetch-remove-group group)
5617     (when gnus-suppress-duplicates
5618       (gnus-dup-enter-articles))
5619     (when gnus-use-trees
5620       (gnus-tree-close group))
5621     (when gnus-use-cache
5622       (gnus-cache-write-active))
5623     ;; Remove entries for this group.
5624     (nnmail-purge-split-history (gnus-group-real-name group))
5625     ;; Make all changes in this group permanent.
5626     (unless quit-config
5627       (gnus-run-hooks 'gnus-exit-group-hook)
5628       (gnus-summary-update-info))
5629     (gnus-close-group group)
5630     ;; Make sure where we were, and go to next newsgroup.
5631     (set-buffer gnus-group-buffer)
5632     (unless quit-config
5633       (gnus-group-jump-to-group group))
5634     (gnus-run-hooks 'gnus-summary-exit-hook)
5635     (unless (or quit-config
5636                 ;; If this group has disappeared from the summary
5637                 ;; buffer, don't skip forwards.
5638                 (not (string= group (gnus-group-group-name))))
5639       (gnus-group-next-unread-group 1))
5640     (setq group-point (point))
5641     (if temporary
5642         nil                             ;Nothing to do.
5643       ;; If we have several article buffers, we kill them at exit.
5644       (unless gnus-single-article-buffer
5645         (gnus-kill-buffer gnus-article-buffer)
5646         (gnus-kill-buffer gnus-original-article-buffer)
5647         (setq gnus-article-current nil))
5648       (set-buffer buf)
5649       (if (not gnus-kill-summary-on-exit)
5650           (gnus-deaden-summary)
5651         ;; We set all buffer-local variables to nil.  It is unclear why
5652         ;; this is needed, but if we don't, buffer-local variables are
5653         ;; not garbage-collected, it seems.  This would the lead to en
5654         ;; ever-growing Emacs.
5655         (gnus-summary-clear-local-variables)
5656         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5657           (gnus-summary-clear-local-variables))
5658         (when (get-buffer gnus-article-buffer)
5659           (bury-buffer gnus-article-buffer))
5660         ;; We clear the global counterparts of the buffer-local
5661         ;; variables as well, just to be on the safe side.
5662         (set-buffer gnus-group-buffer)
5663         (gnus-summary-clear-local-variables)
5664         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5665           (gnus-summary-clear-local-variables))
5666         ;; Return to group mode buffer.
5667         (when (eq mode 'gnus-summary-mode)
5668           (gnus-kill-buffer buf)))
5669       (setq gnus-current-select-method gnus-select-method)
5670       (pop-to-buffer gnus-group-buffer)
5671       (if (not quit-config)
5672           (progn
5673             (goto-char group-point)
5674             (gnus-configure-windows 'group 'force)
5675             (unless (pos-visible-in-window-p)
5676               (forward-line (/ (static-if (featurep 'xemacs)
5677                                    (window-displayed-height)
5678                                  (1- (window-height)))
5679                                -2))
5680               (set-window-start (selected-window) (point))
5681               (goto-char group-point)))
5682         (gnus-handle-ephemeral-exit quit-config))
5683       ;; Clear the current group name.
5684       (unless quit-config
5685         (setq gnus-newsgroup-name nil)))))
5686
5687 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5688 (defun gnus-summary-exit-no-update (&optional no-questions)
5689   "Quit reading current newsgroup without updating read article info."
5690   (interactive)
5691   (let* ((group gnus-newsgroup-name)
5692          (quit-config (gnus-group-quit-config group)))
5693     (when (or no-questions
5694               gnus-expert-user
5695               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5696       (gnus-async-halt-prefetch)
5697       (mapcar 'funcall
5698               (delq 'gnus-summary-expire-articles
5699                     (copy-sequence gnus-summary-prepare-exit-hook)))
5700       ;; If we have several article buffers, we kill them at exit.
5701       (unless gnus-single-article-buffer
5702         (gnus-kill-buffer gnus-article-buffer)
5703         (gnus-kill-buffer gnus-original-article-buffer)
5704         (setq gnus-article-current nil))
5705       (if (not gnus-kill-summary-on-exit)
5706           (gnus-deaden-summary)
5707         (gnus-close-group group)
5708         (gnus-summary-clear-local-variables)
5709         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5710           (gnus-summary-clear-local-variables))
5711         (set-buffer gnus-group-buffer)
5712         (gnus-summary-clear-local-variables)
5713         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5714           (gnus-summary-clear-local-variables))
5715         (when (get-buffer gnus-summary-buffer)
5716           (kill-buffer gnus-summary-buffer)))
5717       (unless gnus-single-article-buffer
5718         (setq gnus-article-current nil))
5719       (when gnus-use-trees
5720         (gnus-tree-close group))
5721       (gnus-async-prefetch-remove-group group)
5722       (when (get-buffer gnus-article-buffer)
5723         (bury-buffer gnus-article-buffer))
5724       ;; Return to the group buffer.
5725       (gnus-configure-windows 'group 'force)
5726       ;; Clear the current group name.
5727       (setq gnus-newsgroup-name nil)
5728       (when (equal (gnus-group-group-name) group)
5729         (gnus-group-next-unread-group 1))
5730       (when quit-config
5731         (gnus-handle-ephemeral-exit quit-config)))))
5732
5733 (defun gnus-handle-ephemeral-exit (quit-config)
5734   "Handle movement when leaving an ephemeral group.
5735 The state which existed when entering the ephemeral is reset."
5736   (if (not (buffer-name (car quit-config)))
5737       (gnus-configure-windows 'group 'force)
5738     (set-buffer (car quit-config))
5739     (cond ((eq major-mode 'gnus-summary-mode)
5740            (gnus-set-global-variables))
5741           ((eq major-mode 'gnus-article-mode)
5742            (save-excursion
5743              ;; The `gnus-summary-buffer' variable may point
5744              ;; to the old summary buffer when using a single
5745              ;; article buffer.
5746              (unless (gnus-buffer-live-p gnus-summary-buffer)
5747                (set-buffer gnus-group-buffer))
5748              (set-buffer gnus-summary-buffer)
5749              (gnus-set-global-variables))))
5750     (if (or (eq (cdr quit-config) 'article)
5751             (eq (cdr quit-config) 'pick))
5752         (progn
5753           ;; The current article may be from the ephemeral group
5754           ;; thus it is best that we reload this article
5755           (gnus-summary-show-article)
5756           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5757               (gnus-configure-windows 'pick 'force)
5758             (gnus-configure-windows (cdr quit-config) 'force)))
5759       (gnus-configure-windows (cdr quit-config) 'force))
5760     (when (eq major-mode 'gnus-summary-mode)
5761       (gnus-summary-next-subject 1 nil t)
5762       (gnus-summary-recenter)
5763       (gnus-summary-position-point))))
5764
5765 (defun gnus-summary-preview-mime-message ()
5766   "MIME decode and play this message."
5767   (interactive)
5768   (let ((gnus-break-pages nil)
5769         (gnus-show-mime t))
5770     (gnus-summary-select-article gnus-show-all-headers t))
5771   (select-window (get-buffer-window gnus-article-buffer)))
5772
5773 ;;; Dead summaries.
5774
5775 (defvar gnus-dead-summary-mode-map nil)
5776
5777 (unless gnus-dead-summary-mode-map
5778   (setq gnus-dead-summary-mode-map (make-keymap))
5779   (suppress-keymap gnus-dead-summary-mode-map)
5780   (substitute-key-definition
5781    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5782   (let ((keys '("\C-d" "\r" "\177" [delete])))
5783     (while keys
5784       (define-key gnus-dead-summary-mode-map
5785         (pop keys) 'gnus-summary-wake-up-the-dead))))
5786
5787 (defvar gnus-dead-summary-mode nil
5788   "Minor mode for Gnus summary buffers.")
5789
5790 (defun gnus-dead-summary-mode (&optional arg)
5791   "Minor mode for Gnus summary buffers."
5792   (interactive "P")
5793   (when (eq major-mode 'gnus-summary-mode)
5794     (make-local-variable 'gnus-dead-summary-mode)
5795     (setq gnus-dead-summary-mode
5796           (if (null arg) (not gnus-dead-summary-mode)
5797             (> (prefix-numeric-value arg) 0)))
5798     (when gnus-dead-summary-mode
5799       (gnus-add-minor-mode
5800        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5801
5802 (defun gnus-deaden-summary ()
5803   "Make the current summary buffer into a dead summary buffer."
5804   ;; Kill any previous dead summary buffer.
5805   (when (and gnus-dead-summary
5806              (buffer-name gnus-dead-summary))
5807     (save-excursion
5808       (set-buffer gnus-dead-summary)
5809       (when gnus-dead-summary-mode
5810         (kill-buffer (current-buffer)))))
5811   ;; Make this the current dead summary.
5812   (setq gnus-dead-summary (current-buffer))
5813   (gnus-dead-summary-mode 1)
5814   (let ((name (buffer-name)))
5815     (when (string-match "Summary" name)
5816       (rename-buffer
5817        (concat (substring name 0 (match-beginning 0)) "Dead "
5818                (substring name (match-beginning 0)))
5819        t)
5820       (bury-buffer))))
5821
5822 (defun gnus-kill-or-deaden-summary (buffer)
5823   "Kill or deaden the summary BUFFER."
5824   (save-excursion
5825     (when (and (buffer-name buffer)
5826                (not gnus-single-article-buffer))
5827       (save-excursion
5828         (set-buffer buffer)
5829         (gnus-kill-buffer gnus-article-buffer)
5830         (gnus-kill-buffer gnus-original-article-buffer)))
5831     (cond (gnus-kill-summary-on-exit
5832            (when (and gnus-use-trees
5833                       (gnus-buffer-exists-p buffer))
5834              (save-excursion
5835                (set-buffer buffer)
5836                (gnus-tree-close gnus-newsgroup-name)))
5837            (gnus-kill-buffer buffer))
5838           ((gnus-buffer-exists-p buffer)
5839            (save-excursion
5840              (set-buffer buffer)
5841              (gnus-deaden-summary))))))
5842
5843 (defun gnus-summary-wake-up-the-dead (&rest args)
5844   "Wake up the dead summary buffer."
5845   (interactive)
5846   (gnus-dead-summary-mode -1)
5847   (let ((name (buffer-name)))
5848     (when (string-match "Dead " name)
5849       (rename-buffer
5850        (concat (substring name 0 (match-beginning 0))
5851                (substring name (match-end 0)))
5852        t)))
5853   (gnus-message 3 "This dead summary is now alive again"))
5854
5855 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5856 (defun gnus-summary-fetch-faq (&optional faq-dir)
5857   "Fetch the FAQ for the current group.
5858 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5859 in."
5860   (interactive
5861    (list
5862     (when current-prefix-arg
5863       (completing-read
5864        "Faq dir: " (and (listp gnus-group-faq-directory)
5865                         (mapcar (lambda (file) (list file))
5866                                 gnus-group-faq-directory))))))
5867   (let (gnus-faq-buffer)
5868     (when (setq gnus-faq-buffer
5869                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5870       (gnus-configure-windows 'summary-faq))))
5871
5872 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5873 (defun gnus-summary-describe-group (&optional force)
5874   "Describe the current newsgroup."
5875   (interactive "P")
5876   (gnus-group-describe-group force gnus-newsgroup-name))
5877
5878 (defun gnus-summary-describe-briefly ()
5879   "Describe summary mode commands briefly."
5880   (interactive)
5881   (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")))
5882
5883 ;; Walking around group mode buffer from summary mode.
5884
5885 (defun gnus-summary-next-group (&optional no-article target-group backward)
5886   "Exit current newsgroup and then select next unread newsgroup.
5887 If prefix argument NO-ARTICLE is non-nil, no article is selected
5888 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5889 previous group instead."
5890   (interactive "P")
5891   ;; Stop pre-fetching.
5892   (gnus-async-halt-prefetch)
5893   (let ((current-group gnus-newsgroup-name)
5894         (current-buffer (current-buffer))
5895         entered)
5896     ;; First we semi-exit this group to update Xrefs and all variables.
5897     ;; We can't do a real exit, because the window conf must remain
5898     ;; the same in case the user is prompted for info, and we don't
5899     ;; want the window conf to change before that...
5900     (gnus-summary-exit t)
5901     (while (not entered)
5902       ;; Then we find what group we are supposed to enter.
5903       (set-buffer gnus-group-buffer)
5904       (gnus-group-jump-to-group current-group)
5905       (setq target-group
5906             (or target-group
5907                 (if (eq gnus-keep-same-level 'best)
5908                     (gnus-summary-best-group gnus-newsgroup-name)
5909                   (gnus-summary-search-group backward gnus-keep-same-level))))
5910       (if (not target-group)
5911           ;; There are no further groups, so we return to the group
5912           ;; buffer.
5913           (progn
5914             (gnus-message 5 "Returning to the group buffer")
5915             (setq entered t)
5916             (when (gnus-buffer-live-p current-buffer)
5917               (set-buffer current-buffer)
5918               (gnus-summary-exit))
5919             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5920         ;; We try to enter the target group.
5921         (gnus-group-jump-to-group target-group)
5922         (let ((unreads (gnus-group-group-unread)))
5923           (if (and (or (eq t unreads)
5924                        (and unreads (not (zerop unreads))))
5925                    (gnus-summary-read-group
5926                     target-group nil no-article
5927                     (and (buffer-name current-buffer) current-buffer)
5928                     nil backward))
5929               (setq entered t)
5930             (setq current-group target-group
5931                   target-group nil)))))))
5932
5933 (defun gnus-summary-prev-group (&optional no-article)
5934   "Exit current newsgroup and then select previous unread newsgroup.
5935 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5936   (interactive "P")
5937   (gnus-summary-next-group no-article nil t))
5938
5939 ;; Walking around summary lines.
5940
5941 (defun gnus-summary-first-subject (&optional unread undownloaded)
5942   "Go to the first unread subject.
5943 If UNREAD is non-nil, go to the first unread article.
5944 Returns the article selected or nil if there are no unread articles."
5945   (interactive "P")
5946   (prog1
5947       (cond
5948        ;; Empty summary.
5949        ((null gnus-newsgroup-data)
5950         (gnus-message 3 "No articles in the group")
5951         nil)
5952        ;; Pick the first article.
5953        ((not unread)
5954         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5955         (gnus-data-number (car gnus-newsgroup-data)))
5956        ;; No unread articles.
5957        ((null gnus-newsgroup-unreads)
5958         (gnus-message 3 "No more unread articles")
5959         nil)
5960        ;; Find the first unread article.
5961        (t
5962         (let ((data gnus-newsgroup-data))
5963           (while (and data
5964                       (and (not (and undownloaded
5965                                      (eq gnus-undownloaded-mark
5966                                          (gnus-data-mark (car data)))))
5967                            (not (gnus-data-unread-p (car data)))))
5968             (setq data (cdr data)))
5969           (when data
5970             (goto-char (gnus-data-pos (car data)))
5971             (gnus-data-number (car data))))))
5972     (gnus-summary-position-point)))
5973
5974 (defun gnus-summary-next-subject (n &optional unread dont-display)
5975   "Go to next N'th summary line.
5976 If N is negative, go to the previous N'th subject line.
5977 If UNREAD is non-nil, only unread articles are selected.
5978 The difference between N and the actual number of steps taken is
5979 returned."
5980   (interactive "p")
5981   (let ((backward (< n 0))
5982         (n (abs n)))
5983     (while (and (> n 0)
5984                 (if backward
5985                     (gnus-summary-find-prev unread)
5986                   (gnus-summary-find-next unread)))
5987       (unless (zerop (setq n (1- n)))
5988         (gnus-summary-show-thread)))
5989     (when (/= 0 n)
5990       (gnus-message 7 "No more%s articles"
5991                     (if unread " unread" "")))
5992     (unless dont-display
5993       (gnus-summary-recenter)
5994       (gnus-summary-position-point))
5995     n))
5996
5997 (defun gnus-summary-next-unread-subject (n)
5998   "Go to next N'th unread summary line."
5999   (interactive "p")
6000   (gnus-summary-next-subject n t))
6001
6002 (defun gnus-summary-prev-subject (n &optional unread)
6003   "Go to previous N'th summary line.
6004 If optional argument UNREAD is non-nil, only unread article is selected."
6005   (interactive "p")
6006   (gnus-summary-next-subject (- n) unread))
6007
6008 (defun gnus-summary-prev-unread-subject (n)
6009   "Go to previous N'th unread summary line."
6010   (interactive "p")
6011   (gnus-summary-next-subject (- n) t))
6012
6013 (defun gnus-summary-goto-subject (article &optional force silent)
6014   "Go the subject line of ARTICLE.
6015 If FORCE, also allow jumping to articles not currently shown."
6016   (interactive "nArticle number: ")
6017   (let ((b (point))
6018         (data (gnus-data-find article)))
6019     ;; We read in the article if we have to.
6020     (and (not data)
6021          force
6022          (gnus-summary-insert-subject
6023           article
6024           (if (or (numberp force) (vectorp force)) force)
6025           t)
6026          (setq data (gnus-data-find article)))
6027     (goto-char b)
6028     (if (not data)
6029         (progn
6030           (unless silent
6031             (gnus-message 3 "Can't find article %d" article))
6032           nil)
6033       (goto-char (gnus-data-pos data))
6034       (gnus-summary-position-point)
6035       article)))
6036
6037 ;; Walking around summary lines with displaying articles.
6038
6039 (defun gnus-summary-expand-window (&optional arg)
6040   "Make the summary buffer take up the entire Emacs frame.
6041 Given a prefix, will force an `article' buffer configuration."
6042   (interactive "P")
6043   (if arg
6044       (gnus-configure-windows 'article 'force)
6045     (gnus-configure-windows 'summary 'force)))
6046
6047 (defun gnus-summary-display-article (article &optional all-header)
6048   "Display ARTICLE in article buffer."
6049   (when (gnus-buffer-live-p gnus-article-buffer)
6050     (with-current-buffer gnus-article-buffer
6051       (mm-enable-multibyte-mule4)))
6052   (gnus-set-global-variables)
6053   (when (gnus-buffer-live-p gnus-article-buffer)
6054     (with-current-buffer gnus-article-buffer
6055       (setq gnus-article-charset gnus-newsgroup-charset)
6056       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6057   (if (null article)
6058       nil
6059     (prog1
6060         (if gnus-summary-display-article-function
6061             (funcall gnus-summary-display-article-function article all-header)
6062           (gnus-article-prepare article all-header))
6063       (with-current-buffer gnus-article-buffer
6064         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6065              nil))
6066       (gnus-run-hooks 'gnus-select-article-hook)
6067       (when (and gnus-current-article
6068                  (not (zerop gnus-current-article)))
6069         (gnus-summary-goto-subject gnus-current-article))
6070       (gnus-summary-recenter)
6071       (when (and gnus-use-trees gnus-show-threads)
6072         (gnus-possibly-generate-tree article)
6073         (gnus-highlight-selected-tree article))
6074       ;; Successfully display article.
6075       (gnus-article-set-window-start
6076        (cdr (assq article gnus-newsgroup-bookmarks))))))
6077
6078 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6079   "Select the current article.
6080 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6081 non-nil, the article will be re-fetched even if it already present in
6082 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6083 be displayed."
6084   ;; Make sure we are in the summary buffer to work around bbdb bug.
6085   (unless (eq major-mode 'gnus-summary-mode)
6086     (set-buffer gnus-summary-buffer))
6087   (let ((article (or article (gnus-summary-article-number)))
6088         (all-headers (not (not all-headers))) ;Must be T or NIL.
6089         gnus-summary-display-article-function)
6090     (and (not pseudo)
6091          (gnus-summary-article-pseudo-p article)
6092          (error "This is a pseudo-article"))
6093     (save-excursion
6094       (set-buffer gnus-summary-buffer)
6095       (if (or (and gnus-single-article-buffer
6096                    (or (null gnus-current-article)
6097                        (null gnus-article-current)
6098                        (null (get-buffer gnus-article-buffer))
6099                        (not (eq article (cdr gnus-article-current)))
6100                        (not (equal (car gnus-article-current)
6101                                    gnus-newsgroup-name))))
6102               (and (not gnus-single-article-buffer)
6103                    (or (null gnus-current-article)
6104                        (not (eq gnus-current-article article))))
6105               force)
6106           ;; The requested article is different from the current article.
6107           (progn
6108             (gnus-summary-display-article article all-headers)
6109             (when (or all-headers gnus-show-all-headers)
6110               (gnus-article-show-all-headers))
6111             (gnus-article-set-window-start
6112              (cdr (assq article gnus-newsgroup-bookmarks)))
6113             article)
6114         (when (or all-headers gnus-show-all-headers)
6115           (gnus-article-show-all-headers))
6116         'old))))
6117
6118 (defun gnus-summary-force-verify-and-decrypt ()
6119   (interactive)
6120   (let ((mm-verify-option 'known)
6121         (mm-decrypt-option 'known))
6122     (gnus-summary-select-article nil 'force)))
6123
6124 (defun gnus-summary-set-current-mark (&optional current-mark)
6125   "Obsolete function."
6126   nil)
6127
6128 (defun gnus-summary-next-article (&optional unread subject backward push)
6129   "Select the next article.
6130 If UNREAD, only unread articles are selected.
6131 If SUBJECT, only articles with SUBJECT are selected.
6132 If BACKWARD, the previous article is selected instead of the next."
6133   (interactive "P")
6134   (cond
6135    ;; Is there such an article?
6136    ((and (gnus-summary-search-forward unread subject backward)
6137          (or (gnus-summary-display-article (gnus-summary-article-number))
6138              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6139     (gnus-summary-position-point))
6140    ;; If not, we try the first unread, if that is wanted.
6141    ((and subject
6142          gnus-auto-select-same
6143          (gnus-summary-first-unread-article))
6144     (gnus-summary-position-point)
6145     (gnus-message 6 "Wrapped"))
6146    ;; Try to get next/previous article not displayed in this group.
6147    ((and gnus-auto-extend-newsgroup
6148          (not unread) (not subject))
6149     (gnus-summary-goto-article
6150      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6151      nil (count-lines (point-min) (point))))
6152    ;; Go to next/previous group.
6153    (t
6154     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6155       (gnus-summary-jump-to-group gnus-newsgroup-name))
6156     (let ((cmd last-command-char)
6157           (point
6158            (save-excursion
6159              (set-buffer gnus-group-buffer)
6160              (point)))
6161           (group
6162            (if (eq gnus-keep-same-level 'best)
6163                (gnus-summary-best-group gnus-newsgroup-name)
6164              (gnus-summary-search-group backward gnus-keep-same-level))))
6165       ;; For some reason, the group window gets selected.  We change
6166       ;; it back.
6167       (select-window (get-buffer-window (current-buffer)))
6168       ;; Select next unread newsgroup automagically.
6169       (cond
6170        ((or (not gnus-auto-select-next)
6171             (not cmd))
6172         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6173        ((or (eq gnus-auto-select-next 'quietly)
6174             (and (eq gnus-auto-select-next 'slightly-quietly)
6175                  push)
6176             (and (eq gnus-auto-select-next 'almost-quietly)
6177                  (gnus-summary-last-article-p)))
6178         ;; Select quietly.
6179         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6180             (gnus-summary-exit)
6181           (gnus-message 7 "No more%s articles (%s)..."
6182                         (if unread " unread" "")
6183                         (if group (concat "selecting " group)
6184                           "exiting"))
6185           (gnus-summary-next-group nil group backward)))
6186        (t
6187         (when (gnus-key-press-event-p last-input-event)
6188           (gnus-summary-walk-group-buffer
6189            gnus-newsgroup-name cmd unread backward point))))))))
6190
6191 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6192   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6193                       (?\C-p (gnus-group-prev-unread-group 1))))
6194         (cursor-in-echo-area t)
6195         keve key group ended)
6196     (save-excursion
6197       (set-buffer gnus-group-buffer)
6198       (goto-char start)
6199       (setq group
6200             (if (eq gnus-keep-same-level 'best)
6201                 (gnus-summary-best-group gnus-newsgroup-name)
6202               (gnus-summary-search-group backward gnus-keep-same-level))))
6203     (while (not ended)
6204       (gnus-message
6205        5 "No more%s articles%s" (if unread " unread" "")
6206        (if (and group
6207                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6208            (format " (Type %s for %s [%s])"
6209                    (single-key-description cmd) group
6210                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6211          (format " (Type %s to exit %s)"
6212                  (single-key-description cmd)
6213                  gnus-newsgroup-name)))
6214       ;; Confirm auto selection.
6215       (setq key (car (setq keve (gnus-read-event-char))))
6216       (setq ended t)
6217       (cond
6218        ((assq key keystrokes)
6219         (let ((obuf (current-buffer)))
6220           (switch-to-buffer gnus-group-buffer)
6221           (when group
6222             (gnus-group-jump-to-group group))
6223           (eval (cadr (assq key keystrokes)))
6224           (setq group (gnus-group-group-name))
6225           (switch-to-buffer obuf))
6226         (setq ended nil))
6227        ((equal key cmd)
6228         (if (or (not group)
6229                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6230             (gnus-summary-exit)
6231           (gnus-summary-next-group nil group backward)))
6232        (t
6233         (push (cdr keve) unread-command-events))))))
6234
6235 (defun gnus-summary-next-unread-article ()
6236   "Select unread article after current one."
6237   (interactive)
6238   (gnus-summary-next-article
6239    (or (not (eq gnus-summary-goto-unread 'never))
6240        (gnus-summary-last-article-p (gnus-summary-article-number)))
6241    (and gnus-auto-select-same
6242         (gnus-summary-article-subject))))
6243
6244 (defun gnus-summary-prev-article (&optional unread subject)
6245   "Select the article after the current one.
6246 If UNREAD is non-nil, only unread articles are selected."
6247   (interactive "P")
6248   (gnus-summary-next-article unread subject t))
6249
6250 (defun gnus-summary-prev-unread-article ()
6251   "Select unread article before current one."
6252   (interactive)
6253   (gnus-summary-prev-article
6254    (or (not (eq gnus-summary-goto-unread 'never))
6255        (gnus-summary-first-article-p (gnus-summary-article-number)))
6256    (and gnus-auto-select-same
6257         (gnus-summary-article-subject))))
6258
6259 (defun gnus-summary-next-page (&optional lines circular)
6260   "Show next page of the selected article.
6261 If at the end of the current article, select the next article.
6262 LINES says how many lines should be scrolled up.
6263
6264 If CIRCULAR is non-nil, go to the start of the article instead of
6265 selecting the next article when reaching the end of the current
6266 article."
6267   (interactive "P")
6268   (setq gnus-summary-buffer (current-buffer))
6269   (gnus-set-global-variables)
6270   (let ((article (gnus-summary-article-number))
6271         (article-window (get-buffer-window gnus-article-buffer t))
6272         endp)
6273     ;; If the buffer is empty, we have no article.
6274     (unless article
6275       (error "No article to select"))
6276     (gnus-configure-windows 'article)
6277     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6278         (if (and (eq gnus-summary-goto-unread 'never)
6279                  (not (gnus-summary-last-article-p article)))
6280             (gnus-summary-next-article)
6281           (gnus-summary-next-unread-article))
6282       (if (or (null gnus-current-article)
6283               (null gnus-article-current)
6284               (/= article (cdr gnus-article-current))
6285               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6286           ;; Selected subject is different from current article's.
6287           (gnus-summary-display-article article)
6288         (when article-window
6289           (gnus-eval-in-buffer-window gnus-article-buffer
6290             (setq endp (gnus-article-next-page lines)))
6291           (when endp
6292             (cond (circular
6293                    (gnus-summary-beginning-of-article))
6294                   (lines
6295                    (gnus-message 3 "End of message"))
6296                   ((null lines)
6297                    (if (and (eq gnus-summary-goto-unread 'never)
6298                             (not (gnus-summary-last-article-p article)))
6299                        (gnus-summary-next-article)
6300                      (gnus-summary-next-unread-article))))))))
6301     (gnus-summary-recenter)
6302     (gnus-summary-position-point)))
6303
6304 (defun gnus-summary-prev-page (&optional lines move)
6305   "Show previous page of selected article.
6306 Argument LINES specifies lines to be scrolled down.
6307 If MOVE, move to the previous unread article if point is at
6308 the beginning of the buffer."
6309   (interactive "P")
6310   (let ((article (gnus-summary-article-number))
6311         (article-window (get-buffer-window gnus-article-buffer t))
6312         endp)
6313     (gnus-configure-windows 'article)
6314     (if (or (null gnus-current-article)
6315             (null gnus-article-current)
6316             (/= article (cdr gnus-article-current))
6317             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6318         ;; Selected subject is different from current article's.
6319         (gnus-summary-display-article article)
6320       (gnus-summary-recenter)
6321       (when article-window
6322         (gnus-eval-in-buffer-window gnus-article-buffer
6323           (setq endp (gnus-article-prev-page lines)))
6324         (when (and move endp)
6325           (cond (lines
6326                  (gnus-message 3 "Beginning of message"))
6327                 ((null lines)
6328                  (if (and (eq gnus-summary-goto-unread 'never)
6329                           (not (gnus-summary-first-article-p article)))
6330                      (gnus-summary-prev-article)
6331                    (gnus-summary-prev-unread-article))))))))
6332   (gnus-summary-position-point))
6333
6334 (defun gnus-summary-prev-page-or-article (&optional lines)
6335   "Show previous page of selected article.
6336 Argument LINES specifies lines to be scrolled down.
6337 If at the beginning of the article, go to the next article."
6338   (interactive "P")
6339   (gnus-summary-prev-page lines t))
6340
6341 (defun gnus-summary-scroll-up (lines)
6342   "Scroll up (or down) one line current article.
6343 Argument LINES specifies lines to be scrolled up (or down if negative)."
6344   (interactive "p")
6345   (gnus-configure-windows 'article)
6346   (gnus-summary-show-thread)
6347   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6348     (gnus-eval-in-buffer-window gnus-article-buffer
6349       (cond ((> lines 0)
6350              (when (gnus-article-next-page lines)
6351                (gnus-message 3 "End of message")))
6352             ((< lines 0)
6353              (gnus-article-prev-page (- lines))))))
6354   (gnus-summary-recenter)
6355   (gnus-summary-position-point))
6356
6357 (defun gnus-summary-scroll-down (lines)
6358   "Scroll down (or up) one line current article.
6359 Argument LINES specifies lines to be scrolled down (or up if negative)."
6360   (interactive "p")
6361   (gnus-summary-scroll-up (- lines)))
6362
6363 (defun gnus-summary-next-same-subject ()
6364   "Select next article which has the same subject as current one."
6365   (interactive)
6366   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6367
6368 (defun gnus-summary-prev-same-subject ()
6369   "Select previous article which has the same subject as current one."
6370   (interactive)
6371   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6372
6373 (defun gnus-summary-next-unread-same-subject ()
6374   "Select next unread article which has the same subject as current one."
6375   (interactive)
6376   (gnus-summary-next-article t (gnus-summary-article-subject)))
6377
6378 (defun gnus-summary-prev-unread-same-subject ()
6379   "Select previous unread article which has the same subject as current one."
6380   (interactive)
6381   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6382
6383 (defun gnus-summary-first-unread-article ()
6384   "Select the first unread article.
6385 Return nil if there are no unread articles."
6386   (interactive)
6387   (prog1
6388       (when (gnus-summary-first-subject t)
6389         (gnus-summary-show-thread)
6390         (gnus-summary-first-subject t)
6391         (gnus-summary-display-article (gnus-summary-article-number)))
6392     (gnus-summary-position-point)))
6393
6394 (defun gnus-summary-first-unread-subject ()
6395   "Place the point on the subject line of the first unread article.
6396 Return nil if there are no unread articles."
6397   (interactive)
6398   (prog1
6399       (when (gnus-summary-first-subject t)
6400         (gnus-summary-show-thread)
6401         (gnus-summary-first-subject t))
6402     (gnus-summary-position-point)))
6403
6404 (defun gnus-summary-first-article ()
6405   "Select the first article.
6406 Return nil if there are no articles."
6407   (interactive)
6408   (prog1
6409       (when (gnus-summary-first-subject)
6410         (gnus-summary-show-thread)
6411         (gnus-summary-first-subject)
6412         (gnus-summary-display-article (gnus-summary-article-number)))
6413     (gnus-summary-position-point)))
6414
6415 (defun gnus-summary-best-unread-article ()
6416   "Select the unread article with the highest score."
6417   (interactive)
6418   (let ((best -1000000)
6419         (data gnus-newsgroup-data)
6420         article score)
6421     (while data
6422       (and (gnus-data-unread-p (car data))
6423            (> (setq score
6424                     (gnus-summary-article-score (gnus-data-number (car data))))
6425               best)
6426            (setq best score
6427                  article (gnus-data-number (car data))))
6428       (setq data (cdr data)))
6429     (prog1
6430         (if article
6431             (gnus-summary-goto-article article)
6432           (error "No unread articles"))
6433       (gnus-summary-position-point))))
6434
6435 (defun gnus-summary-last-subject ()
6436   "Go to the last displayed subject line in the group."
6437   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6438     (when article
6439       (gnus-summary-goto-subject article))))
6440
6441 (defun gnus-summary-goto-article (article &optional all-headers force)
6442   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6443 If ALL-HEADERS is non-nil, no header lines are hidden.
6444 If FORCE, go to the article even if it isn't displayed.  If FORCE
6445 is a number, it is the line the article is to be displayed on."
6446   (interactive
6447    (list
6448     (completing-read
6449      "Article number or Message-ID: "
6450      (mapcar (lambda (number) (list (int-to-string number)))
6451              gnus-newsgroup-limit))
6452     current-prefix-arg
6453     t))
6454   (prog1
6455       (if (and (stringp article)
6456                (string-match "@" article))
6457           (gnus-summary-refer-article article)
6458         (when (stringp article)
6459           (setq article (string-to-number article)))
6460         (if (gnus-summary-goto-subject article force)
6461             (gnus-summary-display-article article all-headers)
6462           (gnus-message 4 "Couldn't go to article %s" article) nil))
6463     (gnus-summary-position-point)))
6464
6465 (defun gnus-summary-goto-last-article ()
6466   "Go to the previously read article."
6467   (interactive)
6468   (prog1
6469       (when gnus-last-article
6470         (gnus-summary-goto-article gnus-last-article nil t))
6471     (gnus-summary-position-point)))
6472
6473 (defun gnus-summary-pop-article (number)
6474   "Pop one article off the history and go to the previous.
6475 NUMBER articles will be popped off."
6476   (interactive "p")
6477   (let (to)
6478     (setq gnus-newsgroup-history
6479           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6480     (if to
6481         (gnus-summary-goto-article (car to) nil t)
6482       (error "Article history empty")))
6483   (gnus-summary-position-point))
6484
6485 ;; Summary commands and functions for limiting the summary buffer.
6486
6487 (defun gnus-summary-limit-to-articles (n)
6488   "Limit the summary buffer to the next N articles.
6489 If not given a prefix, use the process marked articles instead."
6490   (interactive "P")
6491   (prog1
6492       (let ((articles (gnus-summary-work-articles n)))
6493         (setq gnus-newsgroup-processable nil)
6494         (gnus-summary-limit articles))
6495     (gnus-summary-position-point)))
6496
6497 (defun gnus-summary-pop-limit (&optional total)
6498   "Restore the previous limit.
6499 If given a prefix, remove all limits."
6500   (interactive "P")
6501   (when total
6502     (setq gnus-newsgroup-limits
6503           (list (mapcar (lambda (h) (mail-header-number h))
6504                         gnus-newsgroup-headers))))
6505   (unless gnus-newsgroup-limits
6506     (error "No limit to pop"))
6507   (prog1
6508       (gnus-summary-limit nil 'pop)
6509     (gnus-summary-position-point)))
6510
6511 (defun gnus-summary-limit-to-subject (subject &optional header)
6512   "Limit the summary buffer to articles that have subjects that match a regexp."
6513   (interactive "sLimit to subject (regexp): ")
6514   (unless header
6515     (setq header "subject"))
6516   (when (not (equal "" subject))
6517     (prog1
6518         (let ((articles (gnus-summary-find-matching
6519                          (or header "subject") subject 'all)))
6520           (unless articles
6521             (error "Found no matches for \"%s\"" subject))
6522           (gnus-summary-limit articles))
6523       (gnus-summary-position-point))))
6524
6525 (defun gnus-summary-limit-to-author (from)
6526   "Limit the summary buffer to articles that have authors that match a regexp."
6527   (interactive "sLimit to author (regexp): ")
6528   (gnus-summary-limit-to-subject from "from"))
6529
6530 (defun gnus-summary-limit-to-age (age &optional younger-p)
6531   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6532 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6533 articles that are younger than AGE days."
6534   (interactive
6535    (let ((younger current-prefix-arg)
6536          (days-got nil)
6537          days)
6538      (while (not days-got)
6539        (setq days (if younger
6540                       (read-string "Limit to articles within (in days): ")
6541                     (read-string "Limit to articles older than (in days): ")))
6542        (when (> (length days) 0)
6543          (setq days (read days)))
6544        (if (numberp days)
6545            (setq days-got t)
6546          (message "Please enter a number.")
6547          (sleep-for 1)))
6548      (list days younger)))
6549   (prog1
6550       (let ((data gnus-newsgroup-data)
6551             (cutoff (days-to-time age))
6552             articles d date is-younger)
6553         (while (setq d (pop data))
6554           (when (and (vectorp (gnus-data-header d))
6555                      (setq date (mail-header-date (gnus-data-header d))))
6556             (setq is-younger (time-less-p
6557                               (time-since (condition-case ()
6558                                               (date-to-time date)
6559                                             (error '(0 0))))
6560                               cutoff))
6561             (when (if younger-p
6562                       is-younger
6563                     (not is-younger))
6564               (push (gnus-data-number d) articles))))
6565         (gnus-summary-limit (nreverse articles)))
6566     (gnus-summary-position-point)))
6567
6568 (defun gnus-summary-limit-to-extra (header regexp)
6569   "Limit the summary buffer to articles that match an 'extra' header."
6570   (interactive
6571    (let ((header
6572           (intern
6573            (gnus-completing-read
6574             (symbol-name (car gnus-extra-headers))
6575             "Limit extra header:"
6576             (mapcar (lambda (x)
6577                       (cons (symbol-name x) x))
6578                     gnus-extra-headers)
6579             nil
6580             t))))
6581      (list header
6582            (read-string (format "Limit to header %s (regexp): " header)))))
6583   (when (not (equal "" regexp))
6584     (prog1
6585         (let ((articles (gnus-summary-find-matching
6586                          (cons 'extra header) regexp 'all)))
6587           (unless articles
6588             (error "Found no matches for \"%s\"" regexp))
6589           (gnus-summary-limit articles))
6590       (gnus-summary-position-point))))
6591
6592 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6593 (make-obsolete
6594  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6595
6596 (defun gnus-summary-limit-to-unread (&optional all)
6597   "Limit the summary buffer to articles that are not marked as read.
6598 If ALL is non-nil, limit strictly to unread articles."
6599   (interactive "P")
6600   (if all
6601       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6602     (gnus-summary-limit-to-marks
6603      ;; Concat all the marks that say that an article is read and have
6604      ;; those removed.
6605      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6606            gnus-killed-mark gnus-kill-file-mark
6607            gnus-low-score-mark gnus-expirable-mark
6608            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6609            gnus-duplicate-mark gnus-souped-mark)
6610      'reverse)))
6611
6612 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6613 (make-obsolete 'gnus-summary-delete-marked-with
6614                'gnus-summary-limit-exlude-marks)
6615
6616 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6617   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6618 If REVERSE, limit the summary buffer to articles that are marked
6619 with MARKS.  MARKS can either be a string of marks or a list of marks.
6620 Returns how many articles were removed."
6621   (interactive "sMarks: ")
6622   (gnus-summary-limit-to-marks marks t))
6623
6624 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6625   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6626 If REVERSE (the prefix), limit the summary buffer to articles that are
6627 not marked with MARKS.  MARKS can either be a string of marks or a
6628 list of marks.
6629 Returns how many articles were removed."
6630   (interactive "sMarks: \nP")
6631   (prog1
6632       (let ((data gnus-newsgroup-data)
6633             (marks (if (listp marks) marks
6634                      (append marks nil))) ; Transform to list.
6635             articles)
6636         (while data
6637           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6638                   (memq (gnus-data-mark (car data)) marks))
6639             (push (gnus-data-number (car data)) articles))
6640           (setq data (cdr data)))
6641         (gnus-summary-limit articles))
6642     (gnus-summary-position-point)))
6643
6644 (defun gnus-summary-limit-to-score (&optional score)
6645   "Limit to articles with score at or above SCORE."
6646   (interactive "P")
6647   (setq score (if score
6648                   (prefix-numeric-value score)
6649                 (or gnus-summary-default-score 0)))
6650   (let ((data gnus-newsgroup-data)
6651         articles)
6652     (while data
6653       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6654                 score)
6655         (push (gnus-data-number (car data)) articles))
6656       (setq data (cdr data)))
6657     (prog1
6658         (gnus-summary-limit articles)
6659       (gnus-summary-position-point))))
6660
6661 (defun gnus-summary-limit-include-thread (id)
6662   "Display all the hidden articles that in the current thread."
6663   (interactive (list (mail-header-id (gnus-summary-article-header))))
6664   (let ((articles (gnus-articles-in-thread
6665                    (gnus-id-to-thread (gnus-root-id id)))))
6666     (prog1
6667         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6668       (gnus-summary-position-point))))
6669
6670 (defun gnus-summary-limit-include-dormant ()
6671   "Display all the hidden articles that are marked as dormant.
6672 Note that this command only works on a subset of the articles currently
6673 fetched for this group."
6674   (interactive)
6675   (unless gnus-newsgroup-dormant
6676     (error "There are no dormant articles in this group"))
6677   (prog1
6678       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6679     (gnus-summary-position-point)))
6680
6681 (defun gnus-summary-limit-exclude-dormant ()
6682   "Hide all dormant articles."
6683   (interactive)
6684   (prog1
6685       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6686     (gnus-summary-position-point)))
6687
6688 (defun gnus-summary-limit-exclude-childless-dormant ()
6689   "Hide all dormant articles that have no children."
6690   (interactive)
6691   (let ((data (gnus-data-list t))
6692         articles d children)
6693     ;; Find all articles that are either not dormant or have
6694     ;; children.
6695     (while (setq d (pop data))
6696       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6697                 (and (setq children
6698                            (gnus-article-children (gnus-data-number d)))
6699                      (let (found)
6700                        (while children
6701                          (when (memq (car children) articles)
6702                            (setq children nil
6703                                  found t))
6704                          (pop children))
6705                        found)))
6706         (push (gnus-data-number d) articles)))
6707     ;; Do the limiting.
6708     (prog1
6709         (gnus-summary-limit articles)
6710       (gnus-summary-position-point))))
6711
6712 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6713   "Mark all unread excluded articles as read.
6714 If ALL, mark even excluded ticked and dormants as read."
6715   (interactive "P")
6716   (let ((articles (gnus-sorted-complement
6717                    (sort
6718                     (mapcar (lambda (h) (mail-header-number h))
6719                             gnus-newsgroup-headers)
6720                     '<)
6721                    (sort gnus-newsgroup-limit '<)))
6722         article)
6723     (setq gnus-newsgroup-unreads
6724           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6725     (if all
6726         (setq gnus-newsgroup-dormant nil
6727               gnus-newsgroup-marked nil
6728               gnus-newsgroup-reads
6729               (nconc
6730                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6731                gnus-newsgroup-reads))
6732       (while (setq article (pop articles))
6733         (unless (or (memq article gnus-newsgroup-dormant)
6734                     (memq article gnus-newsgroup-marked))
6735           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6736
6737 (defun gnus-summary-limit (articles &optional pop)
6738   (if pop
6739       ;; We pop the previous limit off the stack and use that.
6740       (setq articles (car gnus-newsgroup-limits)
6741             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6742     ;; We use the new limit, so we push the old limit on the stack.
6743     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6744   ;; Set the limit.
6745   (setq gnus-newsgroup-limit articles)
6746   (let ((total (length gnus-newsgroup-data))
6747         (data (gnus-data-find-list (gnus-summary-article-number)))
6748         (gnus-summary-mark-below nil)   ; Inhibit this.
6749         found)
6750     ;; This will do all the work of generating the new summary buffer
6751     ;; according to the new limit.
6752     (gnus-summary-prepare)
6753     ;; Hide any threads, possibly.
6754     (and gnus-show-threads
6755          gnus-thread-hide-subtree
6756          (gnus-summary-hide-all-threads))
6757     ;; Try to return to the article you were at, or one in the
6758     ;; neighborhood.
6759     (when data
6760       ;; We try to find some article after the current one.
6761       (while data
6762         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6763           (setq data nil
6764                 found t))
6765         (setq data (cdr data))))
6766     (unless found
6767       ;; If there is no data, that means that we were after the last
6768       ;; article.  The same goes when we can't find any articles
6769       ;; after the current one.
6770       (goto-char (point-max))
6771       (gnus-summary-find-prev))
6772     (gnus-set-mode-line 'summary)
6773     ;; We return how many articles were removed from the summary
6774     ;; buffer as a result of the new limit.
6775     (- total (length gnus-newsgroup-data))))
6776
6777 (defsubst gnus-invisible-cut-children (threads)
6778   (let ((num 0))
6779     (while threads
6780       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6781         (incf num))
6782       (pop threads))
6783     (< num 2)))
6784
6785 (defsubst gnus-cut-thread (thread)
6786   "Go forwards in the thread until we find an article that we want to display."
6787   (when (or (eq gnus-fetch-old-headers 'some)
6788             (eq gnus-fetch-old-headers 'invisible)
6789             (numberp gnus-fetch-old-headers)
6790             (eq gnus-build-sparse-threads 'some)
6791             (eq gnus-build-sparse-threads 'more))
6792     ;; Deal with old-fetched headers and sparse threads.
6793     (while (and
6794             thread
6795             (or
6796              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6797              (gnus-summary-article-ancient-p
6798               (mail-header-number (car thread))))
6799             (if (or (<= (length (cdr thread)) 1)
6800                     (eq gnus-fetch-old-headers 'invisible))
6801                 (setq gnus-newsgroup-limit
6802                       (delq (mail-header-number (car thread))
6803                             gnus-newsgroup-limit)
6804                       thread (cadr thread))
6805               (when (gnus-invisible-cut-children (cdr thread))
6806                 (let ((th (cdr thread)))
6807                   (while th
6808                     (if (memq (mail-header-number (caar th))
6809                               gnus-newsgroup-limit)
6810                         (setq thread (car th)
6811                               th nil)
6812                       (setq th (cdr th))))))))))
6813   thread)
6814
6815 (defun gnus-cut-threads (threads)
6816   "Cut off all uninteresting articles from the beginning of threads."
6817   (when (or (eq gnus-fetch-old-headers 'some)
6818             (eq gnus-fetch-old-headers 'invisible)
6819             (numberp gnus-fetch-old-headers)
6820             (eq gnus-build-sparse-threads 'some)
6821             (eq gnus-build-sparse-threads 'more))
6822     (let ((th threads))
6823       (while th
6824         (setcar th (gnus-cut-thread (car th)))
6825         (setq th (cdr th)))))
6826   ;; Remove nixed out threads.
6827   (delq nil threads))
6828
6829 (defun gnus-summary-initial-limit (&optional show-if-empty)
6830   "Figure out what the initial limit is supposed to be on group entry.
6831 This entails weeding out unwanted dormants, low-scored articles,
6832 fetch-old-headers verbiage, and so on."
6833   ;; Most groups have nothing to remove.
6834   (if (or gnus-inhibit-limiting
6835           (and (null gnus-newsgroup-dormant)
6836                (not (eq gnus-fetch-old-headers 'some))
6837                (not (numberp gnus-fetch-old-headers))
6838                (not (eq gnus-fetch-old-headers 'invisible))
6839                (null gnus-summary-expunge-below)
6840                (not (eq gnus-build-sparse-threads 'some))
6841                (not (eq gnus-build-sparse-threads 'more))
6842                (null gnus-thread-expunge-below)
6843                (not gnus-use-nocem)))
6844       ()                                ; Do nothing.
6845     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6846     (setq gnus-newsgroup-limit nil)
6847     (mapatoms
6848      (lambda (node)
6849        (unless (car (symbol-value node))
6850          ;; These threads have no parents -- they are roots.
6851          (let ((nodes (cdr (symbol-value node)))
6852                thread)
6853            (while nodes
6854              (if (and gnus-thread-expunge-below
6855                       (< (gnus-thread-total-score (car nodes))
6856                          gnus-thread-expunge-below))
6857                  (gnus-expunge-thread (pop nodes))
6858                (setq thread (pop nodes))
6859                (gnus-summary-limit-children thread))))))
6860      gnus-newsgroup-dependencies)
6861     ;; If this limitation resulted in an empty group, we might
6862     ;; pop the previous limit and use it instead.
6863     (when (and (not gnus-newsgroup-limit)
6864                show-if-empty)
6865       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6866     gnus-newsgroup-limit))
6867
6868 (defun gnus-summary-limit-children (thread)
6869   "Return 1 if this subthread is visible and 0 if it is not."
6870   ;; First we get the number of visible children to this thread.  This
6871   ;; is done by recursing down the thread using this function, so this
6872   ;; will really go down to a leaf article first, before slowly
6873   ;; working its way up towards the root.
6874   (when thread
6875     (let ((children
6876            (if (cdr thread)
6877                (apply '+ (mapcar 'gnus-summary-limit-children
6878                                  (cdr thread)))
6879              0))
6880           (number (mail-header-number (car thread)))
6881           score)
6882       (if (and
6883            (not (memq number gnus-newsgroup-marked))
6884            (or
6885             ;; If this article is dormant and has absolutely no visible
6886             ;; children, then this article isn't visible.
6887             (and (memq number gnus-newsgroup-dormant)
6888                  (zerop children))
6889             ;; If this is "fetch-old-headered" and there is no
6890             ;; visible children, then we don't want this article.
6891             (and (or (eq gnus-fetch-old-headers 'some)
6892                      (numberp gnus-fetch-old-headers))
6893                  (gnus-summary-article-ancient-p number)
6894                  (zerop children))
6895             ;; If this is "fetch-old-headered" and `invisible', then
6896             ;; we don't want this article.
6897             (and (eq gnus-fetch-old-headers 'invisible)
6898                  (gnus-summary-article-ancient-p number))
6899             ;; If this is a sparsely inserted article with no children,
6900             ;; we don't want it.
6901             (and (eq gnus-build-sparse-threads 'some)
6902                  (gnus-summary-article-sparse-p number)
6903                  (zerop children))
6904             ;; If we use expunging, and this article is really
6905             ;; low-scored, then we don't want this article.
6906             (when (and gnus-summary-expunge-below
6907                        (< (setq score
6908                                 (or (cdr (assq number gnus-newsgroup-scored))
6909                                     gnus-summary-default-score))
6910                           gnus-summary-expunge-below))
6911               ;; We increase the expunge-tally here, but that has
6912               ;; nothing to do with the limits, really.
6913               (incf gnus-newsgroup-expunged-tally)
6914               ;; We also mark as read here, if that's wanted.
6915               (when (and gnus-summary-mark-below
6916                          (< score gnus-summary-mark-below))
6917                 (setq gnus-newsgroup-unreads
6918                       (delq number gnus-newsgroup-unreads))
6919                 (if gnus-newsgroup-auto-expire
6920                     (push number gnus-newsgroup-expirable)
6921                   (push (cons number gnus-low-score-mark)
6922                         gnus-newsgroup-reads)))
6923               t)
6924             ;; Check NoCeM things.
6925             (if (and gnus-use-nocem
6926                      (gnus-nocem-unwanted-article-p
6927                       (mail-header-id (car thread))))
6928                 (progn
6929                   (setq gnus-newsgroup-unreads
6930                         (delq number gnus-newsgroup-unreads))
6931                   t))))
6932           ;; Nope, invisible article.
6933           0
6934         ;; Ok, this article is to be visible, so we add it to the limit
6935         ;; and return 1.
6936         (push number gnus-newsgroup-limit)
6937         1))))
6938
6939 (defun gnus-expunge-thread (thread)
6940   "Mark all articles in THREAD as read."
6941   (let* ((number (mail-header-number (car thread))))
6942     (incf gnus-newsgroup-expunged-tally)
6943     ;; We also mark as read here, if that's wanted.
6944     (setq gnus-newsgroup-unreads
6945           (delq number gnus-newsgroup-unreads))
6946     (if gnus-newsgroup-auto-expire
6947         (push number gnus-newsgroup-expirable)
6948       (push (cons number gnus-low-score-mark)
6949             gnus-newsgroup-reads)))
6950   ;; Go recursively through all subthreads.
6951   (mapcar 'gnus-expunge-thread (cdr thread)))
6952
6953 ;; Summary article oriented commands
6954
6955 (defun gnus-summary-refer-parent-article (n)
6956   "Refer parent article N times.
6957 If N is negative, go to ancestor -N instead.
6958 The difference between N and the number of articles fetched is returned."
6959   (interactive "p")
6960   (let ((skip 1)
6961         error header ref)
6962     (when (not (natnump n))
6963       (setq skip (abs n)
6964             n 1))
6965     (while (and (> n 0)
6966                 (not error))
6967       (setq header (gnus-summary-article-header))
6968       (if (and (eq (mail-header-number header)
6969                    (cdr gnus-article-current))
6970                (equal gnus-newsgroup-name
6971                       (car gnus-article-current)))
6972           ;; If we try to find the parent of the currently
6973           ;; displayed article, then we take a look at the actual
6974           ;; References header, since this is slightly more
6975           ;; reliable than the References field we got from the
6976           ;; server.
6977           (save-excursion
6978             (set-buffer gnus-original-article-buffer)
6979             (nnheader-narrow-to-headers)
6980             (unless (setq ref (message-fetch-field "references"))
6981               (setq ref (message-fetch-field "in-reply-to")))
6982             (widen))
6983         (setq ref
6984               ;; It's not the current article, so we take a bet on
6985               ;; the value we got from the server.
6986               (mail-header-references header)))
6987       (if (and ref
6988                (not (equal ref "")))
6989           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6990             (gnus-message 1 "Couldn't find parent"))
6991         (gnus-message 1 "No references in article %d"
6992                       (gnus-summary-article-number))
6993         (setq error t))
6994       (decf n))
6995     (gnus-summary-position-point)
6996     n))
6997
6998 (defun gnus-summary-refer-references ()
6999   "Fetch all articles mentioned in the References header.
7000 Return the number of articles fetched."
7001   (interactive)
7002   (let ((ref (mail-header-references (gnus-summary-article-header)))
7003         (current (gnus-summary-article-number))
7004         (n 0))
7005     (if (or (not ref)
7006             (equal ref ""))
7007         (error "No References in the current article")
7008       ;; For each Message-ID in the References header...
7009       (while (string-match "<[^>]*>" ref)
7010         (incf n)
7011         ;; ... fetch that article.
7012         (gnus-summary-refer-article
7013          (prog1 (match-string 0 ref)
7014            (setq ref (substring ref (match-end 0))))))
7015       (gnus-summary-goto-subject current)
7016       (gnus-summary-position-point)
7017       n)))
7018
7019 (defun gnus-summary-refer-thread (&optional limit)
7020   "Fetch all articles in the current thread.
7021 If LIMIT (the numerical prefix), fetch that many old headers instead
7022 of what's specified by the `gnus-refer-thread-limit' variable."
7023   (interactive "P")
7024   (let ((id (mail-header-id (gnus-summary-article-header)))
7025         (limit (if limit (prefix-numeric-value limit)
7026                  gnus-refer-thread-limit)))
7027     ;; We want to fetch LIMIT *old* headers, but we also have to
7028     ;; re-fetch all the headers in the current buffer, because many of
7029     ;; them may be undisplayed.  So we adjust LIMIT.
7030     (when (numberp limit)
7031       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7032     (unless (eq gnus-fetch-old-headers 'invisible)
7033       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7034       ;; Retrieve the headers and read them in.
7035       (if (eq (gnus-retrieve-headers
7036                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7037               'nov)
7038           (gnus-build-all-threads)
7039         (error "Can't fetch thread from backends that don't support NOV"))
7040       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7041     (gnus-summary-limit-include-thread id)))
7042
7043 (defun gnus-summary-refer-article (message-id)
7044   "Fetch an article specified by MESSAGE-ID."
7045   (interactive "sMessage-ID: ")
7046   (when (and (stringp message-id)
7047              (not (zerop (length message-id))))
7048     ;; Construct the correct Message-ID if necessary.
7049     ;; Suggested by tale@pawl.rpi.edu.
7050     (unless (string-match "^<" message-id)
7051       (setq message-id (concat "<" message-id)))
7052     (unless (string-match ">$" message-id)
7053       (setq message-id (concat message-id ">")))
7054     (let* ((header (gnus-id-to-header message-id))
7055            (sparse (and header
7056                         (gnus-summary-article-sparse-p
7057                          (mail-header-number header))
7058                         (memq (mail-header-number header)
7059                               gnus-newsgroup-limit)))
7060            number)
7061       (cond
7062        ;; If the article is present in the buffer we just go to it.
7063        ((and header
7064              (or (not (gnus-summary-article-sparse-p
7065                        (mail-header-number header)))
7066                  sparse))
7067         (prog1
7068             (gnus-summary-goto-article
7069              (mail-header-number header) nil t)
7070           (when sparse
7071             (gnus-summary-update-article (mail-header-number header)))))
7072        (t
7073         ;; We fetch the article.
7074         (catch 'found
7075           (dolist (gnus-override-method (gnus-refer-article-methods))
7076             (gnus-check-server gnus-override-method)
7077             ;; Fetch the header, and display the article.
7078             (when (setq number (gnus-summary-insert-subject message-id))
7079               (gnus-summary-select-article nil nil nil number)
7080               (throw 'found t)))
7081           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7082
7083 (defun gnus-refer-article-methods ()
7084   "Return a list of referrable methods."
7085   (cond
7086    ;; No method, so we default to current and native.
7087    ((null gnus-refer-article-method)
7088     (list gnus-current-select-method gnus-select-method))
7089    ;; Current.
7090    ((eq 'current gnus-refer-article-method)
7091     (list gnus-current-select-method))
7092    ;; List of select methods.
7093    ((not (and (symbolp (car gnus-refer-article-method))
7094               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7095     (let (out)
7096       (dolist (method gnus-refer-article-method)
7097         (push (if (eq 'current method)
7098                   gnus-current-select-method
7099                 method)
7100               out))
7101       (nreverse out)))
7102    ;; One single select method.
7103    (t
7104     (list gnus-refer-article-method))))
7105
7106 (defun gnus-summary-edit-parameters ()
7107   "Edit the group parameters of the current group."
7108   (interactive)
7109   (gnus-group-edit-group gnus-newsgroup-name 'params))
7110
7111 (defun gnus-summary-customize-parameters ()
7112   "Customize the group parameters of the current group."
7113   (interactive)
7114   (gnus-group-customize gnus-newsgroup-name))
7115
7116 (defun gnus-summary-enter-digest-group (&optional force)
7117   "Enter an nndoc group based on the current article.
7118 If FORCE, force a digest interpretation.  If not, try
7119 to guess what the document format is."
7120   (interactive "P")
7121   (let ((conf gnus-current-window-configuration))
7122     (save-excursion
7123       (gnus-summary-select-article))
7124     (setq gnus-current-window-configuration conf)
7125     (let* ((name (format "%s-%d"
7126                          (gnus-group-prefixed-name
7127                           gnus-newsgroup-name (list 'nndoc ""))
7128                          (save-excursion
7129                            (set-buffer gnus-summary-buffer)
7130                            gnus-current-article)))
7131            (ogroup gnus-newsgroup-name)
7132            (params (append (gnus-info-params (gnus-get-info ogroup))
7133                            (list (cons 'to-group ogroup))
7134                            (list (cons 'save-article-group ogroup))))
7135            (case-fold-search t)
7136            (buf (current-buffer))
7137            dig to-address)
7138       (save-excursion
7139         (set-buffer gnus-original-article-buffer)
7140         ;; Have the digest group inherit the main mail address of
7141         ;; the parent article.
7142         (when (setq to-address (or (message-fetch-field "reply-to")
7143                                    (message-fetch-field "from")))
7144           (setq params (append 
7145                         (list (cons 'to-address 
7146                                     (funcall gnus-decode-encoded-word-function
7147                                              to-address))))))
7148         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7149         (insert-buffer-substring gnus-original-article-buffer)
7150         ;; Remove lines that may lead nndoc to misinterpret the
7151         ;; document type.
7152         (narrow-to-region
7153          (goto-char (point-min))
7154          (or (search-forward "\n\n" nil t) (point)))
7155         (goto-char (point-min))
7156         (delete-matching-lines "^Path:\\|^From ")
7157         (widen))
7158       (unwind-protect
7159           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7160                     (gnus-newsgroup-ephemeral-ignored-charsets
7161                      gnus-newsgroup-ignored-charsets))
7162                 (gnus-group-read-ephemeral-group
7163                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7164                               (nndoc-article-type
7165                                ,(if force 'mbox 'guess))) t))
7166               ;; Make all postings to this group go to the parent group.
7167               (nconc (gnus-info-params (gnus-get-info name))
7168                      params)
7169             ;; Couldn't select this doc group.
7170             (switch-to-buffer buf)
7171             (gnus-set-global-variables)
7172             (gnus-configure-windows 'summary)
7173             (gnus-message 3 "Article couldn't be entered?"))
7174         (kill-buffer dig)))))
7175
7176 (defun gnus-summary-read-document (n)
7177   "Open a new group based on the current article(s).
7178 This will allow you to read digests and other similar
7179 documents as newsgroups.
7180 Obeys the standard process/prefix convention."
7181   (interactive "P")
7182   (let* ((articles (gnus-summary-work-articles n))
7183          (ogroup gnus-newsgroup-name)
7184          (params (append (gnus-info-params (gnus-get-info ogroup))
7185                          (list (cons 'to-group ogroup))))
7186          article group egroup groups vgroup)
7187     (while (setq article (pop articles))
7188       (setq group (format "%s-%d" gnus-newsgroup-name article))
7189       (gnus-summary-remove-process-mark article)
7190       (when (gnus-summary-display-article article)
7191         (save-excursion
7192           (with-temp-buffer
7193             (insert-buffer-substring gnus-original-article-buffer)
7194             ;; Remove some headers that may lead nndoc to make
7195             ;; the wrong guess.
7196             (message-narrow-to-head)
7197             (goto-char (point-min))
7198             (delete-matching-lines "^\\(Path\\):\\|^From ")
7199             (widen)
7200             (if (setq egroup
7201                       (gnus-group-read-ephemeral-group
7202                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7203                                      (nndoc-article-type guess))
7204                        t nil t))
7205                 (progn
7206                   ;; Make all postings to this group go to the parent group.
7207                   (nconc (gnus-info-params (gnus-get-info egroup))
7208                          params)
7209                   (push egroup groups))
7210               ;; Couldn't select this doc group.
7211               (gnus-error 3 "Article couldn't be entered"))))))
7212     ;; Now we have selected all the documents.
7213     (cond
7214      ((not groups)
7215       (error "None of the articles could be interpreted as documents"))
7216      ((gnus-group-read-ephemeral-group
7217        (setq vgroup (format
7218                      "nnvirtual:%s-%s" gnus-newsgroup-name
7219                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7220        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7221        t
7222        (cons (current-buffer) 'summary)))
7223      (t
7224       (error "Couldn't select virtual nndoc group")))))
7225
7226 (defun gnus-summary-isearch-article (&optional regexp-p)
7227   "Do incremental search forward on the current article.
7228 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7229   (interactive "P")
7230   (let* ((gnus-inhibit-treatment t)
7231          (old (gnus-summary-select-article)))
7232     (gnus-configure-windows 'article)
7233     (gnus-eval-in-buffer-window gnus-article-buffer
7234       (save-restriction
7235         (widen)
7236         (when (eq 'old old)
7237           (gnus-article-show-all-headers))
7238         (goto-char (point-min))
7239         (isearch-forward regexp-p)))))
7240
7241 (defun gnus-summary-search-article-forward (regexp &optional backward)
7242   "Search for an article containing REGEXP forward.
7243 If BACKWARD, search backward instead."
7244   (interactive
7245    (list (read-string
7246           (format "Search article %s (regexp%s): "
7247                   (if current-prefix-arg "backward" "forward")
7248                   (if gnus-last-search-regexp
7249                       (concat ", default " gnus-last-search-regexp)
7250                     "")))
7251          current-prefix-arg))
7252   (if (string-equal regexp "")
7253       (setq regexp (or gnus-last-search-regexp ""))
7254     (setq gnus-last-search-regexp regexp)
7255     (setq gnus-article-before-search gnus-current-article))
7256   ;; Intentionally set gnus-last-article.
7257   (setq gnus-last-article gnus-article-before-search)
7258   (let ((gnus-last-article gnus-last-article))
7259     (if (gnus-summary-search-article regexp backward)
7260         (gnus-summary-show-thread)
7261       (error "Search failed: \"%s\"" regexp))))
7262
7263 (defun gnus-summary-search-article-backward (regexp)
7264   "Search for an article containing REGEXP backward."
7265   (interactive
7266    (list (read-string
7267           (format "Search article backward (regexp%s): "
7268                   (if gnus-last-search-regexp
7269                       (concat ", default " gnus-last-search-regexp)
7270                     "")))))
7271   (gnus-summary-search-article-forward regexp 'backward))
7272
7273 (eval-when-compile
7274   (defmacro gnus-summary-search-article-position-point (regexp backward)
7275     "Dehighlight the last matched text and goto the beginning position."
7276     (` (if (and gnus-summary-search-article-matched-data
7277                 (let ((text (caddr gnus-summary-search-article-matched-data))
7278                       (inhibit-read-only t)
7279                       buffer-read-only)
7280                   (delete-region
7281                    (goto-char (car gnus-summary-search-article-matched-data))
7282                    (cadr gnus-summary-search-article-matched-data))
7283                   (insert text)
7284                   (string-match (, regexp) text)))
7285            (if (, backward) (beginning-of-line) (end-of-line))
7286          (goto-char (if (, backward) (point-max) (point-min))))))
7287
7288   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7289     "Place point where X-Face image is displayed."
7290     (if (featurep 'xemacs)
7291         (` (let ((end (if (search-forward "\n\n" nil t)
7292                           (goto-char (1- (point)))
7293                         (point-min)))
7294                  extent)
7295              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7296              (unless (and (re-search-forward "^From:" end t)
7297                           (setq extent (extent-at (point)))
7298                           (extent-begin-glyph extent))
7299                (goto-char (, opoint)))))
7300       (` (let ((end (if (search-forward "\n\n" nil t)
7301                         (goto-char (1- (point)))
7302                       (point-min)))
7303                (start (or (search-backward "\n\n" nil t) (point-min))))
7304            (goto-char
7305             (or (text-property-any start end 'x-face-image t);; x-face-e21
7306                 (text-property-any start end 'x-face-mule-bitmap-image t)
7307                 (, opoint)))))))
7308
7309   (defmacro gnus-summary-search-article-highlight-matched-text
7310     (backward treated x-face)
7311     "Highlight matched text in the function `gnus-summary-search-article'."
7312     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7313              (end (set-marker (make-marker) (match-end 0)))
7314              (inhibit-read-only t)
7315              buffer-read-only)
7316          (unless treated
7317            (let ((,@
7318                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7319                     (mapcar
7320                      (lambda (item) (setq items (delq item items)))
7321                      '(gnus-treat-buttonize
7322                        gnus-treat-fill-article
7323                        gnus-treat-fill-long-lines
7324                        gnus-treat-emphasize
7325                        gnus-treat-highlight-headers
7326                        gnus-treat-highlight-citation
7327                        gnus-treat-highlight-signature
7328                        gnus-treat-overstrike
7329                        gnus-treat-display-xface
7330                        gnus-treat-buttonize-head
7331                        gnus-treat-decode-article-as-default-mime-charset))
7332                     (static-if (featurep 'xemacs)
7333                         items
7334                       (cons '(x-face-mule-delete-x-face-field
7335                               (quote never))
7336                             items))))
7337                  (gnus-treat-display-xface
7338                   (when (, x-face) gnus-treat-display-xface)))
7339              (gnus-article-prepare-mime-display)))
7340          (goto-char (if (, backward) start end))
7341          (when (, x-face)
7342            (gnus-summary-search-article-highlight-goto-x-face (point)))
7343          (setq gnus-summary-search-article-matched-data
7344                (list start end (buffer-substring start end)))
7345          (unless (eq start end);; matched text has been deleted. :-<
7346            (put-text-property start end 'face
7347                               (or (find-face 'isearch)
7348                                   'secondary-selection))))))
7349   )
7350
7351 (defun gnus-summary-search-article (regexp &optional backward)
7352   "Search for an article containing REGEXP.
7353 Optional argument BACKWARD means do search for backward.
7354 `gnus-select-article-hook' is not called during the search."
7355   ;; We have to require this here to make sure that the following
7356   ;; dynamic binding isn't shadowed by autoloading.
7357   (require 'gnus-async)
7358   (require 'gnus-art)
7359   (let ((gnus-select-article-hook nil)  ;Disable hook.
7360         (gnus-article-display-hook nil)
7361         (gnus-article-prepare-hook nil)
7362         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7363         (gnus-use-article-prefetch nil)
7364         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7365         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7366         (sum (current-buffer))
7367         (found nil)
7368         point treated)
7369     (gnus-save-hidden-threads
7370       (static-if (featurep 'xemacs)
7371           (let ((gnus-inhibit-treatment t))
7372             (setq treated (eq 'old (gnus-summary-select-article)))
7373             (when (and treated
7374                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7375                                  (window-live-p (get-buffer-window
7376                                                  gnus-article-buffer t)))))
7377               (gnus-summary-select-article nil t)
7378               (setq treated nil)))
7379         (let ((gnus-inhibit-treatment t)
7380               (x-face-mule-delete-x-face-field 'never))
7381           (setq treated (eq 'old (gnus-summary-select-article)))
7382           (when (and treated
7383                      (not
7384                       (and (gnus-buffer-live-p gnus-article-buffer)
7385                            (window-live-p (get-buffer-window
7386                                            gnus-article-buffer t))
7387                            (or (not (string-match "^\\^X-Face:" regexp))
7388                                (with-current-buffer gnus-article-buffer
7389                                  gnus-summary-search-article-matched-data)))))
7390             (gnus-summary-select-article nil t)
7391             (setq treated nil))))
7392       (set-buffer gnus-article-buffer)
7393       (widen)
7394       (if treated
7395           (progn
7396             (gnus-article-show-all-headers)
7397             (gnus-summary-search-article-position-point regexp backward))
7398         (goto-char (if backward (point-max) (point-min))))
7399       (while (not found)
7400         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7401         (if (if backward
7402                 (re-search-backward regexp nil t)
7403               (re-search-forward regexp nil t))
7404             ;; We found the regexp.
7405             (progn
7406               (gnus-summary-search-article-highlight-matched-text
7407                backward treated (string-match "^\\^X-Face:" regexp))
7408               (setq found 'found)
7409               (forward-line
7410                (/ (- 2 (window-height
7411                         (get-buffer-window gnus-article-buffer t)))
7412                   2))
7413               (set-window-start
7414                (get-buffer-window (current-buffer))
7415                (point))
7416               (set-buffer sum)
7417               (setq point (point)))
7418           ;; We didn't find it, so we go to the next article.
7419           (set-buffer sum)
7420           (setq found 'not)
7421           (while (eq found 'not)
7422             (if (not (if backward (gnus-summary-find-prev)
7423                        (gnus-summary-find-next)))
7424                 ;; No more articles.
7425                 (setq found t)
7426               ;; Select the next article and adjust point.
7427               (unless (gnus-summary-article-sparse-p
7428                        (gnus-summary-article-number))
7429                 (setq found nil)
7430                 (let ((gnus-inhibit-treatment t))
7431                   (gnus-summary-select-article))
7432                 (setq treated nil)
7433                 (set-buffer gnus-article-buffer)
7434                 (widen)
7435                 (goto-char (if backward (point-max) (point-min))))))))
7436       (gnus-message 7 ""))
7437     ;; Return whether we found the regexp.
7438     (when (eq found 'found)
7439       (goto-char point)
7440       (gnus-summary-show-thread)
7441       (gnus-summary-goto-subject gnus-current-article)
7442       (gnus-summary-position-point)
7443       t)))
7444
7445 (defun gnus-summary-find-matching (header regexp &optional backward unread
7446                                           not-case-fold)
7447   "Return a list of all articles that match REGEXP on HEADER.
7448 The search stars on the current article and goes forwards unless
7449 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7450 If UNREAD is non-nil, only unread articles will
7451 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7452 in the comparisons."
7453   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7454                 (gnus-data-find-list
7455                  (gnus-summary-article-number) (gnus-data-list backward))))
7456         (case-fold-search (not not-case-fold))
7457         articles d func)
7458     (if (consp header)
7459         (if (eq (car header) 'extra)
7460             (setq func
7461                   `(lambda (h)
7462                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7463                          "")))
7464           (error "%s is an invalid header" header))
7465       (unless (fboundp (intern (concat "mail-header-" header)))
7466         (error "%s is not a valid header" header))
7467       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7468     (while data
7469       (setq d (car data))
7470       (and (or (not unread)             ; We want all articles...
7471                (gnus-data-unread-p d))  ; Or just unreads.
7472            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7473            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7474            (push (gnus-data-number d) articles)) ; Success!
7475       (setq data (cdr data)))
7476     (nreverse articles)))
7477
7478 (defun gnus-summary-execute-command (header regexp command &optional backward)
7479   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7480 If HEADER is an empty string (or nil), the match is done on the entire
7481 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7482   (interactive
7483    (list (let ((completion-ignore-case t))
7484            (completing-read
7485             "Header name: "
7486             (mapcar (lambda (string) (list string))
7487                     '("Number" "Subject" "From" "Lines" "Date"
7488                       "Message-ID" "Xref" "References" "Body"))
7489             nil 'require-match))
7490          (read-string "Regexp: ")
7491          (read-key-sequence "Command: ")
7492          current-prefix-arg))
7493   (when (equal header "Body")
7494     (setq header ""))
7495   ;; Hidden thread subtrees must be searched as well.
7496   (gnus-summary-show-all-threads)
7497   ;; We don't want to change current point nor window configuration.
7498   (save-excursion
7499     (save-window-excursion
7500       (gnus-message 6 "Executing %s..." (key-description command))
7501       ;; We'd like to execute COMMAND interactively so as to give arguments.
7502       (gnus-execute header regexp
7503                     `(call-interactively ',(key-binding command))
7504                     backward)
7505       (gnus-message 6 "Executing %s...done" (key-description command)))))
7506
7507 (defun gnus-summary-beginning-of-article ()
7508   "Scroll the article back to the beginning."
7509   (interactive)
7510   (gnus-summary-select-article)
7511   (gnus-configure-windows 'article)
7512   (gnus-eval-in-buffer-window gnus-article-buffer
7513     (widen)
7514     (goto-char (point-min))
7515     (when gnus-page-broken
7516       (gnus-narrow-to-page))))
7517
7518 (defun gnus-summary-end-of-article ()
7519   "Scroll to the end of the article."
7520   (interactive)
7521   (gnus-summary-select-article)
7522   (gnus-configure-windows 'article)
7523   (gnus-eval-in-buffer-window gnus-article-buffer
7524     (widen)
7525     (goto-char (point-max))
7526     (recenter -3)
7527     (when gnus-page-broken
7528       (gnus-narrow-to-page))))
7529
7530 (defun gnus-summary-print-article (&optional filename n)
7531   "Generate and print a PostScript image of the N next (mail) articles.
7532
7533 If N is negative, print the N previous articles.  If N is nil and articles
7534 have been marked with the process mark, print these instead.
7535
7536 If the optional first argument FILENAME is nil, send the image to the
7537 printer.  If FILENAME is a string, save the PostScript image in a file with
7538 that name.  If FILENAME is a number, prompt the user for the name of the file
7539 to save in."
7540   (interactive (list (ps-print-preprint current-prefix-arg)
7541                      current-prefix-arg))
7542   (dolist (article (gnus-summary-work-articles n))
7543     (gnus-summary-select-article nil nil 'pseudo article)
7544     (gnus-eval-in-buffer-window gnus-article-buffer
7545       (let ((buffer (generate-new-buffer " *print*")))
7546         (unwind-protect
7547             (progn
7548               (copy-to-buffer buffer (point-min) (point-max))
7549               (set-buffer buffer)
7550               (gnus-article-delete-invisible-text)
7551               (let ((ps-left-header
7552                      (list
7553                       (concat "("
7554                               (mail-header-subject gnus-current-headers) ")")
7555                       (concat "("
7556                               (mail-header-from gnus-current-headers) ")")))
7557                     (ps-right-header
7558                      (list
7559                       "/pagenumberstring load"
7560                       (concat "("
7561                               (mail-header-date gnus-current-headers) ")"))))
7562                 (gnus-run-hooks 'gnus-ps-print-hook)
7563                 (save-excursion
7564                   (ps-print-buffer-with-faces filename))))
7565           (kill-buffer buffer))))))
7566
7567 (defun gnus-summary-show-article (&optional arg)
7568   "Force re-fetching of the current article.
7569 If ARG (the prefix) is a number, show the article with the charset
7570 defined in `gnus-summary-show-article-charset-alist', or the charset
7571 inputed.
7572 If ARG (the prefix) is non-nil and not a number, show the raw article
7573 without any article massaging functions being run."
7574   (interactive "P")
7575   (cond
7576    ((numberp arg)
7577     (let ((gnus-newsgroup-charset
7578            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7579                (read-coding-system "Charset: ")))
7580           (gnus-newsgroup-ignored-charsets 'gnus-all))
7581       (gnus-summary-select-article nil 'force)
7582       (let ((deps gnus-newsgroup-dependencies)
7583             head header)
7584         (save-excursion
7585           (set-buffer gnus-original-article-buffer)
7586           (save-restriction
7587             (message-narrow-to-head)
7588             (setq head (buffer-string)))
7589           (with-temp-buffer
7590             (insert (format "211 %d Article retrieved.\n"
7591                             (cdr gnus-article-current)))
7592             (insert head)
7593             (insert ".\n")
7594             (let ((nntp-server-buffer (current-buffer)))
7595               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7596         (gnus-data-set-header
7597          (gnus-data-find (cdr gnus-article-current))
7598          header)
7599         (gnus-summary-update-article-line
7600          (cdr gnus-article-current) header))))
7601    ((not arg)
7602     ;; Select the article the normal way.
7603     (gnus-summary-select-article nil 'force))
7604    (t
7605     ;; We have to require this here to make sure that the following
7606     ;; dynamic binding isn't shadowed by autoloading.
7607     (require 'gnus-async)
7608     (require 'gnus-art)
7609     ;; Bind the article treatment functions to nil.
7610     (let ((gnus-have-all-headers t)
7611           gnus-article-display-hook
7612           gnus-article-prepare-hook
7613           gnus-article-decode-hook
7614           gnus-break-pages
7615           gnus-show-mime)
7616       (gnus-summary-select-article nil 'force))))
7617   (gnus-summary-goto-subject gnus-current-article)
7618   (gnus-summary-position-point))
7619
7620 (defun gnus-summary-verbose-headers (&optional arg)
7621   "Toggle permanent full header display.
7622 If ARG is a positive number, turn header display on.
7623 If ARG is a negative number, turn header display off."
7624   (interactive "P")
7625   (setq gnus-show-all-headers
7626         (cond ((or (not (numberp arg))
7627                    (zerop arg))
7628                (not gnus-show-all-headers))
7629               ((natnump arg)
7630                t)))
7631   (gnus-summary-show-article))
7632
7633 (defun gnus-summary-toggle-header (&optional arg)
7634   "Show the headers if they are hidden, or hide them if they are shown.
7635 If ARG is a positive number, show the entire header.
7636 If ARG is a negative number, hide the unwanted header lines."
7637   (interactive "P")
7638   (save-excursion
7639     (set-buffer gnus-article-buffer)
7640     (save-restriction
7641       (let* ((buffer-read-only nil)
7642              (inhibit-point-motion-hooks t)
7643              hidden e)
7644         (setq hidden
7645               (if (numberp arg)
7646                   (>= arg 0)
7647                 (save-restriction
7648                   (article-narrow-to-head)
7649                   (gnus-article-hidden-text-p 'headers))))
7650         (goto-char (point-min))
7651         (when (search-forward "\n\n" nil t)
7652           (delete-region (point-min) (1- (point))))
7653         (goto-char (point-min))
7654         (save-excursion
7655           (set-buffer gnus-original-article-buffer)
7656           (goto-char (point-min))
7657           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7658         (insert-buffer-substring gnus-original-article-buffer 1 e)
7659         (save-restriction
7660           (narrow-to-region (point-min) (point))
7661           (article-decode-encoded-words)
7662           (if  hidden
7663               (let ((gnus-treat-hide-headers nil)
7664                     (gnus-treat-hide-boring-headers nil))
7665                 (setq gnus-article-wash-types
7666                       (delq 'headers gnus-article-wash-types))
7667                 (gnus-treat-article 'head))
7668             (gnus-treat-article 'head)))
7669         (gnus-set-mode-line 'article)))))
7670
7671 (defun gnus-summary-show-all-headers ()
7672   "Make all header lines visible."
7673   (interactive)
7674   (gnus-article-show-all-headers))
7675
7676 (defun gnus-summary-toggle-mime (&optional arg)
7677   "Toggle MIME processing.
7678 If ARG is a positive number, turn MIME processing on."
7679   (interactive "P")
7680   (setq gnus-show-mime
7681         (if (null arg)
7682             (not gnus-show-mime)
7683           (> (prefix-numeric-value arg) 0)))
7684   (gnus-summary-select-article t 'force))
7685
7686 (defun gnus-summary-caesar-message (&optional arg)
7687   "Caesar rotate the current article by 13.
7688 The numerical prefix specifies how many places to rotate each letter
7689 forward."
7690   (interactive "P")
7691   (gnus-summary-select-article)
7692   (let ((mail-header-separator ""))
7693     (gnus-eval-in-buffer-window gnus-article-buffer
7694       (save-restriction
7695         (widen)
7696         (let ((start (window-start))
7697               buffer-read-only)
7698           (message-caesar-buffer-body arg)
7699           (set-window-start (get-buffer-window (current-buffer)) start))))))
7700
7701 (defun gnus-summary-stop-page-breaking ()
7702   "Stop page breaking in the current article."
7703   (interactive)
7704   (gnus-summary-select-article)
7705   (gnus-eval-in-buffer-window gnus-article-buffer
7706     (widen)
7707     (when (gnus-visual-p 'page-marker)
7708       (let ((buffer-read-only nil))
7709         (gnus-remove-text-with-property 'gnus-prev)
7710         (gnus-remove-text-with-property 'gnus-next))
7711       (setq gnus-page-broken nil))))
7712
7713 (defun gnus-summary-move-article (&optional n to-newsgroup
7714                                             select-method action)
7715   "Move the current article to a different newsgroup.
7716 If N is a positive number, move the N next articles.
7717 If N is a negative number, move the N previous articles.
7718 If N is nil and any articles have been marked with the process mark,
7719 move those articles instead.
7720 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7721 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7722 re-spool using this method.
7723
7724 For this function to work, both the current newsgroup and the
7725 newsgroup that you want to move to have to support the `request-move'
7726 and `request-accept' functions.
7727
7728 ACTION can be either `move' (the default), `crosspost' or `copy'."
7729   (interactive "P")
7730   (unless action
7731     (setq action 'move))
7732   ;; Disable marking as read.
7733   (let (gnus-mark-article-hook)
7734     (save-window-excursion
7735       (gnus-summary-select-article)))
7736   ;; Check whether the source group supports the required functions.
7737   (cond ((and (eq action 'move)
7738               (not (gnus-check-backend-function
7739                     'request-move-article gnus-newsgroup-name)))
7740          (error "The current group does not support article moving"))
7741         ((and (eq action 'crosspost)
7742               (not (gnus-check-backend-function
7743                     'request-replace-article gnus-newsgroup-name)))
7744          (error "The current group does not support article editing")))
7745   (let ((articles (gnus-summary-work-articles n))
7746         (prefix (if (gnus-check-backend-function
7747                     'request-move-article gnus-newsgroup-name)
7748                     (gnus-group-real-prefix gnus-newsgroup-name)
7749                   ""))
7750         (names '((move "Move" "Moving")
7751                  (copy "Copy" "Copying")
7752                  (crosspost "Crosspost" "Crossposting")))
7753         (copy-buf (save-excursion
7754                     (nnheader-set-temp-buffer " *copy article*")))
7755         (default-marks gnus-article-mark-lists)
7756         (no-expire-marks (delete '(expirable . expire)
7757                                  (copy-sequence gnus-article-mark-lists)))
7758         art-group to-method new-xref article to-groups)
7759     (unless (assq action names)
7760       (error "Unknown action %s" action))
7761     ;; Read the newsgroup name.
7762     (when (and (not to-newsgroup)
7763                (not select-method))
7764       (setq to-newsgroup
7765             (gnus-read-move-group-name
7766              (cadr (assq action names))
7767              (symbol-value (intern (format "gnus-current-%s-group" action)))
7768              articles prefix))
7769       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7770     (setq to-method (or select-method
7771                         (gnus-server-to-method
7772                          (gnus-group-method to-newsgroup))))
7773     ;; Check the method we are to move this article to...
7774     (unless (gnus-check-backend-function
7775              'request-accept-article (car to-method))
7776       (error "%s does not support article copying" (car to-method)))
7777     (unless (gnus-check-server to-method)
7778       (error "Can't open server %s" (car to-method)))
7779     (gnus-message 6 "%s to %s: %s..."
7780                   (caddr (assq action names))
7781                   (or (car select-method) to-newsgroup) articles)
7782     (while articles
7783       (setq article (pop articles))
7784       (setq
7785        art-group
7786        (cond
7787         ;; Move the article.
7788         ((eq action 'move)
7789          ;; Remove this article from future suppression.
7790          (gnus-dup-unsuppress-article article)
7791          (gnus-request-move-article
7792           article                       ; Article to move
7793           gnus-newsgroup-name           ; From newsgroup
7794           (nth 1 (gnus-find-method-for-group
7795                   gnus-newsgroup-name)) ; Server
7796           (list 'gnus-request-accept-article
7797                 to-newsgroup (list 'quote select-method)
7798                 (not articles) t)       ; Accept form
7799           (not articles)))              ; Only save nov last time
7800         ;; Copy the article.
7801         ((eq action 'copy)
7802          (save-excursion
7803            (set-buffer copy-buf)
7804            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7805              (gnus-request-accept-article
7806               to-newsgroup select-method (not articles) t))))
7807         ;; Crosspost the article.
7808         ((eq action 'crosspost)
7809          (let ((xref (message-tokenize-header
7810                       (mail-header-xref (gnus-summary-article-header article))
7811                       " ")))
7812            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7813                                   ":" article))
7814            (unless xref
7815              (setq xref (list (system-name))))
7816            (setq new-xref
7817                  (concat
7818                   (mapconcat 'identity
7819                              (delete "Xref:" (delete new-xref xref))
7820                              " ")
7821                   " " new-xref))
7822            (save-excursion
7823              (set-buffer copy-buf)
7824              ;; First put the article in the destination group.
7825              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7826              (when (consp (setq art-group
7827                                 (gnus-request-accept-article
7828                                  to-newsgroup select-method (not articles))))
7829                (setq new-xref (concat new-xref " " (car art-group)
7830                                       ":" (cdr art-group)))
7831                ;; Now we have the new Xrefs header, so we insert
7832                ;; it and replace the new article.
7833                (nnheader-replace-header "Xref" new-xref)
7834                (gnus-request-replace-article
7835                 (cdr art-group) to-newsgroup (current-buffer))
7836                art-group))))))
7837       (cond
7838        ((not art-group)
7839         (gnus-message 1 "Couldn't %s article %s: %s"
7840                       (cadr (assq action names)) article
7841                       (nnheader-get-report (car to-method))))
7842        ((eq art-group 'junk)
7843         (when (eq action 'move)
7844           (gnus-summary-mark-article article gnus-canceled-mark)
7845           (gnus-message 4 "Deleted article %s" article)))
7846        (t
7847         (let* ((pto-group (gnus-group-prefixed-name
7848                            (car art-group) to-method))
7849                (entry
7850                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7851                (info (nth 2 entry))
7852                (to-group (gnus-info-group info))
7853                to-marks)
7854           ;; Update the group that has been moved to.
7855           (when (and info
7856                      (memq action '(move copy)))
7857             (unless (member to-group to-groups)
7858               (push to-group to-groups))
7859
7860             (unless (memq article gnus-newsgroup-unreads)
7861               (push 'read to-marks)
7862               (gnus-info-set-read
7863                info (gnus-add-to-range (gnus-info-read info)
7864                                        (list (cdr art-group)))))
7865
7866             ;; See whether the article is to be put in the cache.
7867             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7868                              default-marks
7869                            no-expire-marks))
7870                   (to-article (cdr art-group)))
7871
7872               ;; Enter the article into the cache in the new group,
7873               ;; if that is required.
7874               (when gnus-use-cache
7875                 (gnus-cache-possibly-enter-article
7876                  to-group to-article
7877                  (let ((header (copy-sequence
7878                                 (gnus-summary-article-header article))))
7879                    (mail-header-set-number header to-article)
7880                    header)
7881                  (memq article gnus-newsgroup-marked)
7882                  (memq article gnus-newsgroup-dormant)
7883                  (memq article gnus-newsgroup-unreads)))
7884
7885               (when gnus-preserve-marks
7886                 ;; Copy any marks over to the new group.
7887                 (when (and (equal to-group gnus-newsgroup-name)
7888                            (not (memq article gnus-newsgroup-unreads)))
7889                   ;; Mark this article as read in this group.
7890                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7891                   (setcdr (gnus-active to-group) to-article)
7892                   (setcdr gnus-newsgroup-active to-article))
7893
7894                 (while marks
7895                   (when (memq article (symbol-value
7896                                        (intern (format "gnus-newsgroup-%s"
7897                                                        (caar marks)))))
7898                     (push (cdar marks) to-marks)
7899                     ;; If the other group is the same as this group,
7900                     ;; then we have to add the mark to the list.
7901                     (when (equal to-group gnus-newsgroup-name)
7902                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7903                            (cons to-article
7904                                  (symbol-value
7905                                   (intern (format "gnus-newsgroup-%s"
7906                                                   (caar marks)))))))
7907                     ;; Copy the marks to other group.
7908                     (gnus-add-marked-articles
7909                      to-group (cdar marks) (list to-article) info))
7910                   (setq marks (cdr marks)))
7911
7912                 (gnus-request-set-mark to-group (list (list (list to-article)
7913                                                             'set
7914                                                             to-marks))))
7915
7916               (gnus-dribble-enter
7917                (concat "(gnus-group-set-info '"
7918                        (gnus-prin1-to-string (gnus-get-info to-group))
7919                        ")"))))
7920
7921           ;; Update the Xref header in this article to point to
7922           ;; the new crossposted article we have just created.
7923           (when (eq action 'crosspost)
7924             (save-excursion
7925               (set-buffer copy-buf)
7926               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7927               (nnheader-replace-header "Xref" new-xref)
7928               (gnus-request-replace-article
7929                article gnus-newsgroup-name (current-buffer)))))
7930
7931         ;;;!!!Why is this necessary?
7932         (set-buffer gnus-summary-buffer)
7933
7934         (gnus-summary-goto-subject article)
7935         (when (eq action 'move)
7936           (gnus-summary-mark-article article gnus-canceled-mark))))
7937       (gnus-summary-remove-process-mark article))
7938     ;; Re-activate all groups that have been moved to.
7939     (while to-groups
7940       (save-excursion
7941         (set-buffer gnus-group-buffer)
7942         (when (gnus-group-goto-group (car to-groups) t)
7943           (gnus-group-get-new-news-this-group 1 t))
7944         (pop to-groups)))
7945
7946     (gnus-kill-buffer copy-buf)
7947     (gnus-summary-position-point)
7948     (gnus-set-mode-line 'summary)))
7949
7950 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7951   "Move the current article to a different newsgroup.
7952 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7953 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7954 re-spool using this method."
7955   (interactive "P")
7956   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7957
7958 (defun gnus-summary-crosspost-article (&optional n)
7959   "Crosspost the current article to some other group."
7960   (interactive "P")
7961   (gnus-summary-move-article n nil nil 'crosspost))
7962
7963 (defcustom gnus-summary-respool-default-method nil
7964   "Default method for respooling an article.
7965 If nil, use to the current newsgroup method."
7966   :type '(choice (gnus-select-method :value (nnml ""))
7967                  (const nil))
7968   :group 'gnus-summary-mail)
7969
7970 (defun gnus-summary-respool-article (&optional n method)
7971   "Respool the current article.
7972 The article will be squeezed through the mail spooling process again,
7973 which means that it will be put in some mail newsgroup or other
7974 depending on `nnmail-split-methods'.
7975 If N is a positive number, respool the N next articles.
7976 If N is a negative number, respool the N previous articles.
7977 If N is nil and any articles have been marked with the process mark,
7978 respool those articles instead.
7979
7980 Respooling can be done both from mail groups and \"real\" newsgroups.
7981 In the former case, the articles in question will be moved from the
7982 current group into whatever groups they are destined to.  In the
7983 latter case, they will be copied into the relevant groups."
7984   (interactive
7985    (list current-prefix-arg
7986          (let* ((methods (gnus-methods-using 'respool))
7987                 (methname
7988                  (symbol-name (or gnus-summary-respool-default-method
7989                                   (car (gnus-find-method-for-group
7990                                         gnus-newsgroup-name)))))
7991                 (method
7992                  (gnus-completing-read
7993                   methname "What backend do you want to use when respooling?"
7994                   methods nil t nil 'gnus-mail-method-history))
7995                 ms)
7996            (cond
7997             ((zerop (length (setq ms (gnus-servers-using-backend
7998                                       (intern method)))))
7999              (list (intern method) ""))
8000             ((= 1 (length ms))
8001              (car ms))
8002             (t
8003              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8004                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8005                            ms-alist))))))))
8006   (unless method
8007     (error "No method given for respooling"))
8008   (if (assoc (symbol-name
8009               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8010              (gnus-methods-using 'respool))
8011       (gnus-summary-move-article n nil method)
8012     (gnus-summary-copy-article n nil method)))
8013
8014 (defun gnus-summary-import-article (file)
8015   "Import an arbitrary file into a mail newsgroup."
8016   (interactive "fImport file: ")
8017   (let ((group gnus-newsgroup-name)
8018         (now (current-time))
8019         atts lines)
8020     (unless (gnus-check-backend-function 'request-accept-article group)
8021       (error "%s does not support article importing" group))
8022     (or (file-readable-p file)
8023         (not (file-regular-p file))
8024         (error "Can't read %s" file))
8025     (save-excursion
8026       (set-buffer (gnus-get-buffer-create " *import file*"))
8027       (erase-buffer)
8028       (nnheader-insert-file-contents file)
8029       (goto-char (point-min))
8030       (unless (nnheader-article-p)
8031         ;; This doesn't look like an article, so we fudge some headers.
8032         (setq atts (file-attributes file)
8033               lines (count-lines (point-min) (point-max)))
8034         (insert "From: " (read-string "From: ") "\n"
8035                 "Subject: " (read-string "Subject: ") "\n"
8036                 "Date: " (message-make-date (nth 5 atts))
8037                 "\n"
8038                 "Message-ID: " (message-make-message-id) "\n"
8039                 "Lines: " (int-to-string lines) "\n"
8040                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8041       (gnus-request-accept-article group nil t)
8042       (kill-buffer (current-buffer)))))
8043
8044 (defun gnus-summary-article-posted-p ()
8045   "Say whether the current (mail) article is available from news as well.
8046 This will be the case if the article has both been mailed and posted."
8047   (interactive)
8048   (let ((id (mail-header-references (gnus-summary-article-header)))
8049         (gnus-override-method (car (gnus-refer-article-methods))))
8050     (if (gnus-request-head id "")
8051         (gnus-message 2 "The current message was found on %s"
8052                       gnus-override-method)
8053       (gnus-message 2 "The current message couldn't be found on %s"
8054                     gnus-override-method)
8055       nil)))
8056
8057 (defun gnus-summary-expire-articles (&optional now)
8058   "Expire all articles that are marked as expirable in the current group."
8059   (interactive)
8060   (when (gnus-check-backend-function
8061          'request-expire-articles gnus-newsgroup-name)
8062     ;; This backend supports expiry.
8063     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8064            (expirable (if total
8065                           (progn
8066                             ;; We need to update the info for
8067                             ;; this group for `gnus-list-of-read-articles'
8068                             ;; to give us the right answer.
8069                             (gnus-run-hooks 'gnus-exit-group-hook)
8070                             (gnus-summary-update-info)
8071                             (gnus-list-of-read-articles gnus-newsgroup-name))
8072                         (setq gnus-newsgroup-expirable
8073                               (sort gnus-newsgroup-expirable '<))))
8074            (expiry-wait (if now 'immediate
8075                           (gnus-group-find-parameter
8076                            gnus-newsgroup-name 'expiry-wait)))
8077            (nnmail-expiry-target
8078             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8079                 nnmail-expiry-target))
8080            es)
8081       (when expirable
8082         ;; There are expirable articles in this group, so we run them
8083         ;; through the expiry process.
8084         (gnus-message 6 "Expiring articles...")
8085         (unless (gnus-check-group gnus-newsgroup-name)
8086           (error "Can't open server for %s" gnus-newsgroup-name))
8087         ;; The list of articles that weren't expired is returned.
8088         (save-excursion
8089           (if expiry-wait
8090               (let ((nnmail-expiry-wait-function nil)
8091                     (nnmail-expiry-wait expiry-wait))
8092                 (setq es (gnus-request-expire-articles
8093                           expirable gnus-newsgroup-name)))
8094             (setq es (gnus-request-expire-articles
8095                       expirable gnus-newsgroup-name)))
8096           (unless total
8097             (setq gnus-newsgroup-expirable es))
8098           ;; We go through the old list of expirable, and mark all
8099           ;; really expired articles as nonexistent.
8100           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8101             (let ((gnus-use-cache nil))
8102               (while expirable
8103                 (unless (memq (car expirable) es)
8104                   (when (gnus-data-find (car expirable))
8105                     (gnus-summary-mark-article
8106                      (car expirable) gnus-canceled-mark)))
8107                 (setq expirable (cdr expirable))))))
8108         (gnus-message 6 "Expiring articles...done")))))
8109
8110 (defun gnus-summary-expire-articles-now ()
8111   "Expunge all expirable articles in the current group.
8112 This means that *all* articles that are marked as expirable will be
8113 deleted forever, right now."
8114   (interactive)
8115   (or gnus-expert-user
8116       (gnus-yes-or-no-p
8117        "Are you really, really, really sure you want to delete all these messages? ")
8118       (error "Phew!"))
8119   (gnus-summary-expire-articles t))
8120
8121 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8122 (defun gnus-summary-delete-article (&optional n)
8123   "Delete the N next (mail) articles.
8124 This command actually deletes articles.  This is not a marking
8125 command.  The article will disappear forever from your life, never to
8126 return.
8127 If N is negative, delete backwards.
8128 If N is nil and articles have been marked with the process mark,
8129 delete these instead."
8130   (interactive "P")
8131   (unless (gnus-check-backend-function 'request-expire-articles
8132                                        gnus-newsgroup-name)
8133     (error "The current newsgroup does not support article deletion"))
8134   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8135     (error "Couldn't open server"))
8136   ;; Compute the list of articles to delete.
8137   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8138         not-deleted)
8139     (if (and gnus-novice-user
8140              (not (gnus-yes-or-no-p
8141                    (format "Do you really want to delete %s forever? "
8142                            (if (> (length articles) 1)
8143                                (format "these %s articles" (length articles))
8144                              "this article")))))
8145         ()
8146       ;; Delete the articles.
8147       (setq not-deleted (gnus-request-expire-articles
8148                          articles gnus-newsgroup-name 'force))
8149       (while articles
8150         (gnus-summary-remove-process-mark (car articles))
8151         ;; The backend might not have been able to delete the article
8152         ;; after all.
8153         (unless (memq (car articles) not-deleted)
8154           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8155         (setq articles (cdr articles)))
8156       (when not-deleted
8157         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8158     (gnus-summary-position-point)
8159     (gnus-set-mode-line 'summary)
8160     not-deleted))
8161
8162 (defun gnus-summary-edit-article (&optional force)
8163   "Edit the current article.
8164 This will have permanent effect only in mail groups.
8165 If FORCE is non-nil, allow editing of articles even in read-only
8166 groups."
8167   (interactive "P")
8168   (save-excursion
8169     (set-buffer gnus-summary-buffer)
8170     (let ((mail-parse-charset gnus-newsgroup-charset)
8171           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8172       (gnus-set-global-variables)
8173       (when (and (not force)
8174                  (gnus-group-read-only-p))
8175         (error "The current newsgroup does not support article editing"))
8176       (gnus-summary-show-article t)
8177       (gnus-article-edit-article
8178        'ignore
8179        `(lambda (no-highlight)
8180           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8181                 (message-options message-options)
8182                 (message-options-set-recipient)
8183                 (mail-parse-ignored-charsets
8184                  ',gnus-newsgroup-ignored-charsets))
8185             (gnus-summary-edit-article-done
8186              ,(or (mail-header-references gnus-current-headers) "")
8187              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8188
8189 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8190
8191 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8192                                                  no-highlight)
8193   "Make edits to the current article permanent."
8194   (interactive)
8195   (save-excursion
8196     ;; The buffer restriction contains the entire article if it exists.
8197     (when (article-goto-body)
8198       (let ((lines (count-lines (point) (point-max)))
8199             (length (- (point-max) (point)))
8200             (case-fold-search t)
8201             (body (copy-marker (point))))
8202         (goto-char (point-min))
8203         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8204           (delete-region (match-beginning 1) (match-end 1))
8205           (insert (number-to-string length)))
8206         (goto-char (point-min))
8207         (when (re-search-forward
8208                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8209           (delete-region (match-beginning 1) (match-end 1))
8210           (insert (number-to-string length)))
8211         (goto-char (point-min))
8212         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8213           (delete-region (match-beginning 1) (match-end 1))
8214           (insert (number-to-string lines))))))
8215   ;; Replace the article.
8216   (let ((buf (current-buffer)))
8217     (with-temp-buffer
8218       (insert-buffer-substring buf)
8219       
8220       (if (and (not read-only)
8221                (not (gnus-request-replace-article
8222                      (cdr gnus-article-current) (car gnus-article-current)
8223                      (current-buffer) t)))
8224           (error "Couldn't replace article")
8225         ;; Update the summary buffer.
8226         (if (and references
8227                  (equal (message-tokenize-header references " ")
8228                         (message-tokenize-header
8229                          (or (message-fetch-field "references") "") " ")))
8230             ;; We only have to update this line.
8231             (save-excursion
8232               (save-restriction
8233                 (message-narrow-to-head)
8234                 (let ((head (buffer-string))
8235                       header)
8236                   (with-temp-buffer
8237                     (insert (format "211 %d Article retrieved.\n"
8238                                     (cdr gnus-article-current)))
8239                     (insert head)
8240                     (insert ".\n")
8241                     (let ((nntp-server-buffer (current-buffer)))
8242                       (setq header (car (gnus-get-newsgroup-headers
8243                                          (save-excursion
8244                                            (set-buffer gnus-summary-buffer)
8245                                            gnus-newsgroup-dependencies)
8246                                          t))))
8247                     (save-excursion
8248                       (set-buffer gnus-summary-buffer)
8249                       (gnus-data-set-header
8250                        (gnus-data-find (cdr gnus-article-current))
8251                        header)
8252                       (gnus-summary-update-article-line
8253                        (cdr gnus-article-current) header))))))
8254           ;; Update threads.
8255           (set-buffer (or buffer gnus-summary-buffer))
8256           (gnus-summary-update-article (cdr gnus-article-current)))
8257         ;; Prettify the article buffer again.
8258         (unless no-highlight
8259           (save-excursion
8260             (set-buffer gnus-article-buffer)
8261             ;;;!!! Fix this -- article should be rehighlighted.
8262             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8263             (set-buffer gnus-original-article-buffer)
8264             (gnus-request-article
8265              (cdr gnus-article-current)
8266              (car gnus-article-current) (current-buffer))))
8267         ;; Prettify the summary buffer line.
8268         (when (gnus-visual-p 'summary-highlight 'highlight)
8269           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8270
8271 (defun gnus-summary-edit-wash (key)
8272   "Perform editing command KEY in the article buffer."
8273   (interactive
8274    (list
8275     (progn
8276       (message "%s" (concat (this-command-keys) "- "))
8277       (read-char))))
8278   (message "")
8279   (gnus-summary-edit-article)
8280   (execute-kbd-macro (concat (this-command-keys) key))
8281   (gnus-article-edit-done))
8282
8283 ;;; Respooling
8284
8285 (defun gnus-summary-respool-query (&optional silent trace)
8286   "Query where the respool algorithm would put this article."
8287   (interactive)
8288   (let (gnus-mark-article-hook)
8289     (gnus-summary-select-article)
8290     (save-excursion
8291       (set-buffer gnus-original-article-buffer)
8292       (save-restriction
8293         (message-narrow-to-head)
8294         (let ((groups (nnmail-article-group 'identity trace)))
8295           (unless silent
8296             (if groups
8297                 (message "This message would go to %s"
8298                          (mapconcat 'car groups ", "))
8299               (message "This message would go to no groups"))
8300             groups))))))
8301
8302 (defun gnus-summary-respool-trace ()
8303   "Trace where the respool algorithm would put this article.
8304 Display a buffer showing all fancy splitting patterns which matched."
8305   (interactive)
8306   (gnus-summary-respool-query nil t))
8307
8308 ;; Summary marking commands.
8309
8310 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8311   "Mark articles which has the same subject as read, and then select the next.
8312 If UNMARK is positive, remove any kind of mark.
8313 If UNMARK is negative, tick articles."
8314   (interactive "P")
8315   (when unmark
8316     (setq unmark (prefix-numeric-value unmark)))
8317   (let ((count
8318          (gnus-summary-mark-same-subject
8319           (gnus-summary-article-subject) unmark)))
8320     ;; Select next unread article.  If auto-select-same mode, should
8321     ;; select the first unread article.
8322     (gnus-summary-next-article t (and gnus-auto-select-same
8323                                       (gnus-summary-article-subject)))
8324     (gnus-message 7 "%d article%s marked as %s"
8325                   count (if (= count 1) " is" "s are")
8326                   (if unmark "unread" "read"))))
8327
8328 (defun gnus-summary-kill-same-subject (&optional unmark)
8329   "Mark articles which has the same subject as read.
8330 If UNMARK is positive, remove any kind of mark.
8331 If UNMARK is negative, tick articles."
8332   (interactive "P")
8333   (when unmark
8334     (setq unmark (prefix-numeric-value unmark)))
8335   (let ((count
8336          (gnus-summary-mark-same-subject
8337           (gnus-summary-article-subject) unmark)))
8338     ;; If marked as read, go to next unread subject.
8339     (when (null unmark)
8340       ;; Go to next unread subject.
8341       (gnus-summary-next-subject 1 t))
8342     (gnus-message 7 "%d articles are marked as %s"
8343                   count (if unmark "unread" "read"))))
8344
8345 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8346   "Mark articles with same SUBJECT as read, and return marked number.
8347 If optional argument UNMARK is positive, remove any kinds of marks.
8348 If optional argument UNMARK is negative, mark articles as unread instead."
8349   (let ((count 1))
8350     (save-excursion
8351       (cond
8352        ((null unmark)                   ; Mark as read.
8353         (while (and
8354                 (progn
8355                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8356                   (gnus-summary-show-thread) t)
8357                 (gnus-summary-find-subject subject))
8358           (setq count (1+ count))))
8359        ((> unmark 0)                    ; Tick.
8360         (while (and
8361                 (progn
8362                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8363                   (gnus-summary-show-thread) t)
8364                 (gnus-summary-find-subject subject))
8365           (setq count (1+ count))))
8366        (t                               ; Mark as unread.
8367         (while (and
8368                 (progn
8369                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8370                   (gnus-summary-show-thread) t)
8371                 (gnus-summary-find-subject subject))
8372           (setq count (1+ count)))))
8373       (gnus-set-mode-line 'summary)
8374       ;; Return the number of marked articles.
8375       count)))
8376
8377 (defun gnus-summary-mark-as-processable (n &optional unmark)
8378   "Set the process mark on the next N articles.
8379 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8380 the process mark instead.  The difference between N and the actual
8381 number of articles marked is returned."
8382   (interactive "p")
8383   (let ((backward (< n 0))
8384         (n (abs n)))
8385     (while (and
8386             (> n 0)
8387             (if unmark
8388                 (gnus-summary-remove-process-mark
8389                  (gnus-summary-article-number))
8390               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8391             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8392       (setq n (1- n)))
8393     (when (/= 0 n)
8394       (gnus-message 7 "No more articles"))
8395     (gnus-summary-recenter)
8396     (gnus-summary-position-point)
8397     n))
8398
8399 (defun gnus-summary-unmark-as-processable (n)
8400   "Remove the process mark from the next N articles.
8401 If N is negative, unmark backward instead.  The difference between N and
8402 the actual number of articles unmarked is returned."
8403   (interactive "p")
8404   (gnus-summary-mark-as-processable n t))
8405
8406 (defun gnus-summary-unmark-all-processable ()
8407   "Remove the process mark from all articles."
8408   (interactive)
8409   (save-excursion
8410     (while gnus-newsgroup-processable
8411       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8412   (gnus-summary-position-point))
8413
8414 (defun gnus-summary-add-mark (article type)
8415   "Mark ARTICLE with a mark of TYPE."
8416   (let ((vtype (car (assq type gnus-article-mark-lists)))
8417         var)
8418     (if (not vtype)
8419         (error "No such mark type: %s" type)
8420       (setq var (intern (format "gnus-newsgroup-%s" type)))
8421       (set var (cons article (symbol-value var)))
8422       (if (memq type '(processable cached replied saved))
8423           (gnus-summary-update-secondary-mark article)
8424         ;;; !!! This is bobus.  We should find out what primary
8425         ;;; !!! mark we want to set.
8426         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8427         
8428 (defun gnus-summary-mark-as-expirable (n)
8429   "Mark N articles forward as expirable.
8430 If N is negative, mark backward instead.  The difference between N and
8431 the actual number of articles marked is returned."
8432   (interactive "p")
8433   (gnus-summary-mark-forward n gnus-expirable-mark))
8434
8435 (defun gnus-summary-mark-article-as-replied (article)
8436   "Mark ARTICLE replied and update the summary line."
8437   (push article gnus-newsgroup-replied)
8438   (let ((buffer-read-only nil))
8439     (when (gnus-summary-goto-subject article nil t)
8440       (gnus-summary-update-secondary-mark article))))
8441
8442 (defun gnus-summary-set-bookmark (article)
8443   "Set a bookmark in current article."
8444   (interactive (list (gnus-summary-article-number)))
8445   (when (or (not (get-buffer gnus-article-buffer))
8446             (not gnus-current-article)
8447             (not gnus-article-current)
8448             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8449     (error "No current article selected"))
8450   ;; Remove old bookmark, if one exists.
8451   (let ((old (assq article gnus-newsgroup-bookmarks)))
8452     (when old
8453       (setq gnus-newsgroup-bookmarks
8454             (delq old gnus-newsgroup-bookmarks))))
8455   ;; Set the new bookmark, which is on the form
8456   ;; (article-number . line-number-in-body).
8457   (push
8458    (cons article
8459          (save-excursion
8460            (set-buffer gnus-article-buffer)
8461            (count-lines
8462             (min (point)
8463                  (save-excursion
8464                    (goto-char (point-min))
8465                    (search-forward "\n\n" nil t)
8466                    (point)))
8467             (point))))
8468    gnus-newsgroup-bookmarks)
8469   (gnus-message 6 "A bookmark has been added to the current article."))
8470
8471 (defun gnus-summary-remove-bookmark (article)
8472   "Remove the bookmark from the current article."
8473   (interactive (list (gnus-summary-article-number)))
8474   ;; Remove old bookmark, if one exists.
8475   (let ((old (assq article gnus-newsgroup-bookmarks)))
8476     (if old
8477         (progn
8478           (setq gnus-newsgroup-bookmarks
8479                 (delq old gnus-newsgroup-bookmarks))
8480           (gnus-message 6 "Removed bookmark."))
8481       (gnus-message 6 "No bookmark in current article."))))
8482
8483 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8484 (defun gnus-summary-mark-as-dormant (n)
8485   "Mark N articles forward as dormant.
8486 If N is negative, mark backward instead.  The difference between N and
8487 the actual number of articles marked is returned."
8488   (interactive "p")
8489   (gnus-summary-mark-forward n gnus-dormant-mark))
8490
8491 (defun gnus-summary-set-process-mark (article)
8492   "Set the process mark on ARTICLE and update the summary line."
8493   (setq gnus-newsgroup-processable
8494         (cons article
8495               (delq article gnus-newsgroup-processable)))
8496   (when (gnus-summary-goto-subject article)
8497     (gnus-summary-show-thread)
8498     (gnus-summary-goto-subject article)
8499     (gnus-summary-update-secondary-mark article)))
8500
8501 (defun gnus-summary-remove-process-mark (article)
8502   "Remove the process mark from ARTICLE and update the summary line."
8503   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8504   (when (gnus-summary-goto-subject article)
8505     (gnus-summary-show-thread)
8506     (gnus-summary-goto-subject article)
8507     (gnus-summary-update-secondary-mark article)))
8508
8509 (defun gnus-summary-set-saved-mark (article)
8510   "Set the process mark on ARTICLE and update the summary line."
8511   (push article gnus-newsgroup-saved)
8512   (when (gnus-summary-goto-subject article)
8513     (gnus-summary-update-secondary-mark article)))
8514
8515 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8516   "Mark N articles as read forwards.
8517 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8518 The difference between N and the actual number of articles marked is
8519 returned.
8520 Iff NO-EXPIRE, auto-expiry will be inhibited."
8521   (interactive "p")
8522   (gnus-summary-show-thread)
8523   (let ((backward (< n 0))
8524         (gnus-summary-goto-unread
8525          (and gnus-summary-goto-unread
8526               (not (eq gnus-summary-goto-unread 'never))
8527               (not (memq mark (list gnus-unread-mark
8528                                     gnus-ticked-mark gnus-dormant-mark)))))
8529         (n (abs n))
8530         (mark (or mark gnus-del-mark)))
8531     (while (and (> n 0)
8532                 (gnus-summary-mark-article nil mark no-expire)
8533                 (zerop (gnus-summary-next-subject
8534                         (if backward -1 1)
8535                         (and gnus-summary-goto-unread
8536                              (not (eq gnus-summary-goto-unread 'never)))
8537                         t)))
8538       (setq n (1- n)))
8539     (when (/= 0 n)
8540       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8541     (gnus-summary-recenter)
8542     (gnus-summary-position-point)
8543     (gnus-set-mode-line 'summary)
8544     n))
8545
8546 (defun gnus-summary-mark-article-as-read (mark)
8547   "Mark the current article quickly as read with MARK."
8548   (let ((article (gnus-summary-article-number)))
8549     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8550     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8551     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8552     (push (cons article mark) gnus-newsgroup-reads)
8553     ;; Possibly remove from cache, if that is used.
8554     (when gnus-use-cache
8555       (gnus-cache-enter-remove-article article))
8556     ;; Allow the backend to change the mark.
8557     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8558     ;; Check for auto-expiry.
8559     (when (and gnus-newsgroup-auto-expire
8560                (memq mark gnus-auto-expirable-marks))
8561       (setq mark gnus-expirable-mark)
8562       ;; Let the backend know about the mark change.
8563       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8564       (push article gnus-newsgroup-expirable))
8565     ;; Set the mark in the buffer.
8566     (gnus-summary-update-mark mark 'unread)
8567     t))
8568
8569 (defun gnus-summary-mark-article-as-unread (mark)
8570   "Mark the current article quickly as unread with MARK."
8571   (let* ((article (gnus-summary-article-number))
8572          (old-mark (gnus-summary-article-mark article)))
8573     ;; Allow the backend to change the mark.
8574     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8575     (if (eq mark old-mark)
8576         t
8577       (if (<= article 0)
8578           (progn
8579             (gnus-error 1 "Can't mark negative article numbers")
8580             nil)
8581         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8582         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8583         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8584         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8585         (cond ((= mark gnus-ticked-mark)
8586                (push article gnus-newsgroup-marked))
8587               ((= mark gnus-dormant-mark)
8588                (push article gnus-newsgroup-dormant))
8589               (t
8590                (push article gnus-newsgroup-unreads)))
8591         (gnus-pull article gnus-newsgroup-reads)
8592
8593         ;; See whether the article is to be put in the cache.
8594         (and gnus-use-cache
8595              (vectorp (gnus-summary-article-header article))
8596              (save-excursion
8597                (gnus-cache-possibly-enter-article
8598                 gnus-newsgroup-name article
8599                 (gnus-summary-article-header article)
8600                 (= mark gnus-ticked-mark)
8601                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8602
8603         ;; Fix the mark.
8604         (gnus-summary-update-mark mark 'unread)
8605         t))))
8606
8607 (defun gnus-summary-mark-article (&optional article mark no-expire)
8608   "Mark ARTICLE with MARK.  MARK can be any character.
8609 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8610 `??' (dormant) and `?E' (expirable).
8611 If MARK is nil, then the default character `?r' is used.
8612 If ARTICLE is nil, then the article on the current line will be
8613 marked.
8614 Iff NO-EXPIRE, auto-expiry will be inhibited."
8615   ;; The mark might be a string.
8616   (when (stringp mark)
8617     (setq mark (aref mark 0)))
8618   ;; If no mark is given, then we check auto-expiring.
8619   (when (null mark)
8620     (setq mark gnus-del-mark))
8621   (when (and (not no-expire)
8622              gnus-newsgroup-auto-expire
8623              (memq mark gnus-auto-expirable-marks))
8624     (setq mark gnus-expirable-mark))
8625   (let ((article (or article (gnus-summary-article-number)))
8626         (old-mark (gnus-summary-article-mark article)))
8627     ;; Allow the backend to change the mark.
8628     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8629     (if (eq mark old-mark)
8630         t
8631       (unless article
8632         (error "No article on current line"))
8633       (if (not (if (or (= mark gnus-unread-mark)
8634                        (= mark gnus-ticked-mark)
8635                        (= mark gnus-dormant-mark))
8636                    (gnus-mark-article-as-unread article mark)
8637                  (gnus-mark-article-as-read article mark)))
8638           t
8639         ;; See whether the article is to be put in the cache.
8640         (and gnus-use-cache
8641              (not (= mark gnus-canceled-mark))
8642              (vectorp (gnus-summary-article-header article))
8643              (save-excursion
8644                (gnus-cache-possibly-enter-article
8645                 gnus-newsgroup-name article
8646                 (gnus-summary-article-header article)
8647                 (= mark gnus-ticked-mark)
8648                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8649
8650         (when (gnus-summary-goto-subject article nil t)
8651           (let ((buffer-read-only nil))
8652             (gnus-summary-show-thread)
8653             ;; Fix the mark.
8654             (gnus-summary-update-mark mark 'unread)
8655             t))))))
8656
8657 (defun gnus-summary-update-secondary-mark (article)
8658   "Update the secondary (read, process, cache) mark."
8659   (gnus-summary-update-mark
8660    (cond ((memq article gnus-newsgroup-processable)
8661           gnus-process-mark)
8662          ((memq article gnus-newsgroup-cached)
8663           gnus-cached-mark)
8664          ((memq article gnus-newsgroup-replied)
8665           gnus-replied-mark)
8666          ((memq article gnus-newsgroup-saved)
8667           gnus-saved-mark)
8668          (t gnus-no-mark))
8669    'replied)
8670   (when (gnus-visual-p 'summary-highlight 'highlight)
8671     (gnus-run-hooks 'gnus-summary-update-hook))
8672   t)
8673
8674 (defun gnus-summary-update-mark (mark type)
8675   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8676         (buffer-read-only nil))
8677     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8678     (when forward
8679       (when (looking-at "\r")
8680         (incf forward))
8681       (when (<= (+ forward (point)) (point-max))
8682         ;; Go to the right position on the line.
8683         (goto-char (+ forward (point)))
8684         ;; Replace the old mark with the new mark.
8685         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8686         ;; Optionally update the marks by some user rule.
8687         (when (eq type 'unread)
8688           (gnus-data-set-mark
8689            (gnus-data-find (gnus-summary-article-number)) mark)
8690           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8691
8692 (defun gnus-mark-article-as-read (article &optional mark)
8693   "Enter ARTICLE in the pertinent lists and remove it from others."
8694   ;; Make the article expirable.
8695   (let ((mark (or mark gnus-del-mark)))
8696     (if (= mark gnus-expirable-mark)
8697         (push article gnus-newsgroup-expirable)
8698       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8699     ;; Remove from unread and marked lists.
8700     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8701     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8702     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8703     (push (cons article mark) gnus-newsgroup-reads)
8704     ;; Possibly remove from cache, if that is used.
8705     (when gnus-use-cache
8706       (gnus-cache-enter-remove-article article))
8707     t))
8708
8709 (defun gnus-mark-article-as-unread (article &optional mark)
8710   "Enter ARTICLE in the pertinent lists and remove it from others."
8711   (let ((mark (or mark gnus-ticked-mark)))
8712     (if (<= article 0)
8713         (progn
8714           (gnus-error 1 "Can't mark negative article numbers")
8715           nil)
8716       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8717             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8718             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8719             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8720
8721       ;; Unsuppress duplicates?
8722       (when gnus-suppress-duplicates
8723         (gnus-dup-unsuppress-article article))
8724
8725       (cond ((= mark gnus-ticked-mark)
8726              (push article gnus-newsgroup-marked))
8727             ((= mark gnus-dormant-mark)
8728              (push article gnus-newsgroup-dormant))
8729             (t
8730              (push article gnus-newsgroup-unreads)))
8731       (gnus-pull article gnus-newsgroup-reads)
8732       t)))
8733
8734 (defalias 'gnus-summary-mark-as-unread-forward
8735   'gnus-summary-tick-article-forward)
8736 (make-obsolete 'gnus-summary-mark-as-unread-forward
8737                'gnus-summary-tick-article-forward)
8738 (defun gnus-summary-tick-article-forward (n)
8739   "Tick N articles forwards.
8740 If N is negative, tick backwards instead.
8741 The difference between N and the number of articles ticked is returned."
8742   (interactive "p")
8743   (gnus-summary-mark-forward n gnus-ticked-mark))
8744
8745 (defalias 'gnus-summary-mark-as-unread-backward
8746   'gnus-summary-tick-article-backward)
8747 (make-obsolete 'gnus-summary-mark-as-unread-backward
8748                'gnus-summary-tick-article-backward)
8749 (defun gnus-summary-tick-article-backward (n)
8750   "Tick N articles backwards.
8751 The difference between N and the number of articles ticked is returned."
8752   (interactive "p")
8753   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8754
8755 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8756 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8757 (defun gnus-summary-tick-article (&optional article clear-mark)
8758   "Mark current article as unread.
8759 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8760 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8761   (interactive)
8762   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8763                                        gnus-ticked-mark)))
8764
8765 (defun gnus-summary-mark-as-read-forward (n)
8766   "Mark N articles as read forwards.
8767 If N is negative, mark backwards instead.
8768 The difference between N and the actual number of articles marked is
8769 returned."
8770   (interactive "p")
8771   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8772
8773 (defun gnus-summary-mark-as-read-backward (n)
8774   "Mark the N articles as read backwards.
8775 The difference between N and the actual number of articles marked is
8776 returned."
8777   (interactive "p")
8778   (gnus-summary-mark-forward
8779    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8780
8781 (defun gnus-summary-mark-as-read (&optional article mark)
8782   "Mark current article as read.
8783 ARTICLE specifies the article to be marked as read.
8784 MARK specifies a string to be inserted at the beginning of the line."
8785   (gnus-summary-mark-article article mark))
8786
8787 (defun gnus-summary-clear-mark-forward (n)
8788   "Clear marks from N articles forward.
8789 If N is negative, clear backward instead.
8790 The difference between N and the number of marks cleared is returned."
8791   (interactive "p")
8792   (gnus-summary-mark-forward n gnus-unread-mark))
8793
8794 (defun gnus-summary-clear-mark-backward (n)
8795   "Clear marks from N articles backward.
8796 The difference between N and the number of marks cleared is returned."
8797   (interactive "p")
8798   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8799
8800 (defun gnus-summary-mark-unread-as-read ()
8801   "Intended to be used by `gnus-summary-mark-article-hook'."
8802   (when (memq gnus-current-article gnus-newsgroup-unreads)
8803     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8804
8805 (defun gnus-summary-mark-read-and-unread-as-read ()
8806   "Intended to be used by `gnus-summary-mark-article-hook'."
8807   (let ((mark (gnus-summary-article-mark)))
8808     (when (or (gnus-unread-mark-p mark)
8809               (gnus-read-mark-p mark))
8810       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8811
8812 (defun gnus-summary-mark-unread-as-ticked ()
8813    "Intended to be used by `gnus-summary-mark-article-hook'."
8814   (when (memq gnus-current-article gnus-newsgroup-unreads)
8815     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8816
8817 (defun gnus-summary-mark-region-as-read (point mark all)
8818   "Mark all unread articles between point and mark as read.
8819 If given a prefix, mark all articles between point and mark as read,
8820 even ticked and dormant ones."
8821   (interactive "r\nP")
8822   (save-excursion
8823     (let (article)
8824       (goto-char point)
8825       (beginning-of-line)
8826       (while (and
8827               (< (point) mark)
8828               (progn
8829                 (when (or all
8830                           (memq (setq article (gnus-summary-article-number))
8831                                 gnus-newsgroup-unreads))
8832                   (gnus-summary-mark-article article gnus-del-mark))
8833                 t)
8834               (gnus-summary-find-next))))))
8835
8836 (defun gnus-summary-mark-below (score mark)
8837   "Mark articles with score less than SCORE with MARK."
8838   (interactive "P\ncMark: ")
8839   (setq score (if score
8840                   (prefix-numeric-value score)
8841                 (or gnus-summary-default-score 0)))
8842   (save-excursion
8843     (set-buffer gnus-summary-buffer)
8844     (goto-char (point-min))
8845     (while
8846         (progn
8847           (and (< (gnus-summary-article-score) score)
8848                (gnus-summary-mark-article nil mark))
8849           (gnus-summary-find-next)))))
8850
8851 (defun gnus-summary-kill-below (&optional score)
8852   "Mark articles with score below SCORE as read."
8853   (interactive "P")
8854   (gnus-summary-mark-below score gnus-killed-mark))
8855
8856 (defun gnus-summary-clear-above (&optional score)
8857   "Clear all marks from articles with score above SCORE."
8858   (interactive "P")
8859   (gnus-summary-mark-above score gnus-unread-mark))
8860
8861 (defun gnus-summary-tick-above (&optional score)
8862   "Tick all articles with score above SCORE."
8863   (interactive "P")
8864   (gnus-summary-mark-above score gnus-ticked-mark))
8865
8866 (defun gnus-summary-mark-above (score mark)
8867   "Mark articles with score over SCORE with MARK."
8868   (interactive "P\ncMark: ")
8869   (setq score (if score
8870                   (prefix-numeric-value score)
8871                 (or gnus-summary-default-score 0)))
8872   (save-excursion
8873     (set-buffer gnus-summary-buffer)
8874     (goto-char (point-min))
8875     (while (and (progn
8876                   (when (> (gnus-summary-article-score) score)
8877                     (gnus-summary-mark-article nil mark))
8878                   t)
8879                 (gnus-summary-find-next)))))
8880
8881 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8882 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8883 (defun gnus-summary-limit-include-expunged (&optional no-error)
8884   "Display all the hidden articles that were expunged for low scores."
8885   (interactive)
8886   (let ((buffer-read-only nil))
8887     (let ((scored gnus-newsgroup-scored)
8888           headers h)
8889       (while scored
8890         (unless (gnus-number-to-header (caar scored))
8891           (and (setq h (gnus-summary-article-header (caar scored)))
8892                (< (cdar scored) gnus-summary-expunge-below)
8893                (push h headers)))
8894         (setq scored (cdr scored)))
8895       (if (not headers)
8896           (when (not no-error)
8897             (error "No expunged articles hidden"))
8898         (goto-char (point-min))
8899         (gnus-summary-prepare-unthreaded (nreverse headers))
8900         (goto-char (point-min))
8901         (gnus-summary-position-point)
8902         t))))
8903
8904 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8905   "Mark all unread articles in this newsgroup as read.
8906 If prefix argument ALL is non-nil, ticked and dormant articles will
8907 also be marked as read.
8908 If QUIETLY is non-nil, no questions will be asked.
8909 If TO-HERE is non-nil, it should be a point in the buffer.  All
8910 articles before this point will be marked as read.
8911 Note that this function will only catch up the unread article
8912 in the current summary buffer limitation.
8913 The number of articles marked as read is returned."
8914   (interactive "P")
8915   (prog1
8916       (save-excursion
8917         (when (or quietly
8918                   (not gnus-interactive-catchup) ;Without confirmation?
8919                   gnus-expert-user
8920                   (gnus-y-or-n-p
8921                    (if all
8922                        "Mark absolutely all articles as read? "
8923                      "Mark all unread articles as read? ")))
8924           (if (and not-mark
8925                    (not gnus-newsgroup-adaptive)
8926                    (not gnus-newsgroup-auto-expire)
8927                    (not gnus-suppress-duplicates)
8928                    (or (not gnus-use-cache)
8929                        (eq gnus-use-cache 'passive)))
8930               (progn
8931                 (when all
8932                   (setq gnus-newsgroup-marked nil
8933                         gnus-newsgroup-dormant nil))
8934                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8935             ;; We actually mark all articles as canceled, which we
8936             ;; have to do when using auto-expiry or adaptive scoring.
8937             (gnus-summary-show-all-threads)
8938             (when (gnus-summary-first-subject (not all) t)
8939               (while (and
8940                       (if to-here (< (point) to-here) t)
8941                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8942                       (gnus-summary-find-next (not all) nil nil t))))
8943             (gnus-set-mode-line 'summary))
8944           t))
8945     (gnus-summary-position-point)))
8946
8947 (defun gnus-summary-catchup-to-here (&optional all)
8948   "Mark all unticked articles before the current one as read.
8949 If ALL is non-nil, also mark ticked and dormant articles as read."
8950   (interactive "P")
8951   (save-excursion
8952     (gnus-save-hidden-threads
8953       (let ((beg (point)))
8954         ;; We check that there are unread articles.
8955         (when (or all (gnus-summary-find-prev))
8956           (gnus-summary-catchup all t beg)))))
8957   (gnus-summary-position-point))
8958
8959 (defun gnus-summary-catchup-all (&optional quietly)
8960   "Mark all articles in this newsgroup as read."
8961   (interactive "P")
8962   (gnus-summary-catchup t quietly))
8963
8964 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8965   "Mark all unread articles in this group as read, then exit.
8966 If prefix argument ALL is non-nil, all articles are marked as read."
8967   (interactive "P")
8968   (when (gnus-summary-catchup all quietly nil 'fast)
8969     ;; Select next newsgroup or exit.
8970     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8971              (eq gnus-auto-select-next 'quietly))
8972         (gnus-summary-next-group nil)
8973       (gnus-summary-exit))))
8974
8975 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8976   "Mark all articles in this newsgroup as read, and then exit."
8977   (interactive "P")
8978   (gnus-summary-catchup-and-exit t quietly))
8979
8980 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8981   "Mark all articles in this group as read and select the next group.
8982 If given a prefix, mark all articles, unread as well as ticked, as
8983 read."
8984   (interactive "P")
8985   (save-excursion
8986     (gnus-summary-catchup all))
8987   (gnus-summary-next-group))
8988
8989 ;;;
8990 ;;; with article
8991 ;;;
8992
8993 (defmacro gnus-with-article (article &rest forms)
8994   "Select ARTICLE and perform FORMS in the original article buffer.
8995 Then replace the article with the result."
8996   `(progn
8997      ;; We don't want the article to be marked as read.
8998      (let (gnus-mark-article-hook)
8999        (gnus-summary-select-article t t nil ,article))
9000      (set-buffer gnus-original-article-buffer)
9001      ,@forms
9002      (if (not (gnus-check-backend-function
9003                'request-replace-article (car gnus-article-current)))
9004          (gnus-message 5 "Read-only group; not replacing")
9005        (unless (gnus-request-replace-article
9006                 ,article (car gnus-article-current)
9007                 (current-buffer) t)
9008          (error "Couldn't replace article")))
9009      ;; The cache and backlog have to be flushed somewhat.
9010      (when gnus-keep-backlog
9011        (gnus-backlog-remove-article
9012         (car gnus-article-current) (cdr gnus-article-current)))
9013      (when gnus-use-cache
9014        (gnus-cache-update-article
9015         (car gnus-article-current) (cdr gnus-article-current)))))
9016
9017 (put 'gnus-with-article 'lisp-indent-function 1)
9018 (put 'gnus-with-article 'edebug-form-spec '(form body))
9019
9020 ;; Thread-based commands.
9021
9022 (defun gnus-summary-articles-in-thread (&optional article)
9023   "Return a list of all articles in the current thread.
9024 If ARTICLE is non-nil, return all articles in the thread that starts
9025 with that article."
9026   (let* ((article (or article (gnus-summary-article-number)))
9027          (data (gnus-data-find-list article))
9028          (top-level (gnus-data-level (car data)))
9029          (top-subject
9030           (cond ((null gnus-thread-operation-ignore-subject)
9031                  (gnus-simplify-subject-re
9032                   (mail-header-subject (gnus-data-header (car data)))))
9033                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9034                  (gnus-simplify-subject-fuzzy
9035                   (mail-header-subject (gnus-data-header (car data)))))
9036                 (t nil)))
9037          (end-point (save-excursion
9038                       (if (gnus-summary-go-to-next-thread)
9039                           (point) (point-max))))
9040          articles)
9041     (while (and data
9042                 (< (gnus-data-pos (car data)) end-point))
9043       (when (or (not top-subject)
9044                 (string= top-subject
9045                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9046                              (gnus-simplify-subject-fuzzy
9047                               (mail-header-subject
9048                                (gnus-data-header (car data))))
9049                            (gnus-simplify-subject-re
9050                             (mail-header-subject
9051                              (gnus-data-header (car data)))))))
9052         (push (gnus-data-number (car data)) articles))
9053       (unless (and (setq data (cdr data))
9054                    (> (gnus-data-level (car data)) top-level))
9055         (setq data nil)))
9056     ;; Return the list of articles.
9057     (nreverse articles)))
9058
9059 (defun gnus-summary-rethread-current ()
9060   "Rethread the thread the current article is part of."
9061   (interactive)
9062   (let* ((gnus-show-threads t)
9063          (article (gnus-summary-article-number))
9064          (id (mail-header-id (gnus-summary-article-header)))
9065          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9066     (unless id
9067       (error "No article on the current line"))
9068     (gnus-rebuild-thread id)
9069     (gnus-summary-goto-subject article)))
9070
9071 (defun gnus-summary-reparent-thread ()
9072   "Make the current article child of the marked (or previous) article.
9073
9074 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9075 is non-nil or the Subject: of both articles are the same."
9076   (interactive)
9077   (unless (not (gnus-group-read-only-p))
9078     (error "The current newsgroup does not support article editing"))
9079   (unless (<= (length gnus-newsgroup-processable) 1)
9080     (error "No more than one article may be marked"))
9081   (save-window-excursion
9082     (let ((gnus-article-buffer " *reparent*")
9083           (current-article (gnus-summary-article-number))
9084           ;; First grab the marked article, otherwise one line up.
9085           (parent-article (if (not (null gnus-newsgroup-processable))
9086                               (car gnus-newsgroup-processable)
9087                             (save-excursion
9088                               (if (eq (forward-line -1) 0)
9089                                   (gnus-summary-article-number)
9090                                 (error "Beginning of summary buffer"))))))
9091       (unless (not (eq current-article parent-article))
9092         (error "An article may not be self-referential"))
9093       (let ((message-id (mail-header-id
9094                          (gnus-summary-article-header parent-article))))
9095         (unless (and message-id (not (equal message-id "")))
9096           (error "No message-id in desired parent"))
9097         (gnus-with-article current-article
9098           (save-restriction
9099             (goto-char (point-min))
9100             (message-narrow-to-head)
9101             (if (re-search-forward "^References: " nil t)
9102                 (progn
9103                   (re-search-forward "^[^ \t]" nil t)
9104                   (forward-line -1)
9105                   (end-of-line)
9106                   (insert " " message-id))
9107               (insert "References: " message-id "\n"))))
9108         (set-buffer gnus-summary-buffer)
9109         (gnus-summary-unmark-all-processable)
9110         (gnus-summary-update-article current-article)
9111         (gnus-summary-rethread-current)
9112         (gnus-message 3 "Article %d is now the child of article %d"
9113                       current-article parent-article)))))
9114
9115 (defun gnus-summary-toggle-threads (&optional arg)
9116   "Toggle showing conversation threads.
9117 If ARG is positive number, turn showing conversation threads on."
9118   (interactive "P")
9119   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9120     (setq gnus-show-threads
9121           (if (null arg) (not gnus-show-threads)
9122             (> (prefix-numeric-value arg) 0)))
9123     (gnus-summary-prepare)
9124     (gnus-summary-goto-subject current)
9125     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9126     (gnus-summary-position-point)))
9127
9128 (defun gnus-summary-show-all-threads ()
9129   "Show all threads."
9130   (interactive)
9131   (save-excursion
9132     (let ((buffer-read-only nil))
9133       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9134   (gnus-summary-position-point))
9135
9136 (defun gnus-summary-show-thread ()
9137   "Show thread subtrees.
9138 Returns nil if no thread was there to be shown."
9139   (interactive)
9140   (let ((buffer-read-only nil)
9141         (orig (point))
9142         ;; first goto end then to beg, to have point at beg after let
9143         (end (progn (end-of-line) (point)))
9144         (beg (progn (beginning-of-line) (point))))
9145     (prog1
9146         ;; Any hidden lines here?
9147         (search-forward "\r" end t)
9148       (subst-char-in-region beg end ?\^M ?\n t)
9149       (goto-char orig)
9150       (gnus-summary-position-point))))
9151
9152 (defun gnus-summary-hide-all-threads ()
9153   "Hide all thread subtrees."
9154   (interactive)
9155   (save-excursion
9156     (goto-char (point-min))
9157     (gnus-summary-hide-thread)
9158     (while (zerop (gnus-summary-next-thread 1 t))
9159       (gnus-summary-hide-thread)))
9160   (gnus-summary-position-point))
9161
9162 (defun gnus-summary-hide-thread ()
9163   "Hide thread subtrees.
9164 Returns nil if no threads were there to be hidden."
9165   (interactive)
9166   (let ((buffer-read-only nil)
9167         (start (point))
9168         (article (gnus-summary-article-number)))
9169     (goto-char start)
9170     ;; Go forward until either the buffer ends or the subthread
9171     ;; ends.
9172     (when (and (not (eobp))
9173                (or (zerop (gnus-summary-next-thread 1 t))
9174                    (goto-char (point-max))))
9175       (prog1
9176           (if (and (> (point) start)
9177                    (search-backward "\n" start t))
9178               (progn
9179                 (subst-char-in-region start (point) ?\n ?\^M)
9180                 (gnus-summary-goto-subject article))
9181             (goto-char start)
9182             nil)))))
9183
9184 (defun gnus-summary-go-to-next-thread (&optional previous)
9185   "Go to the same level (or less) next thread.
9186 If PREVIOUS is non-nil, go to previous thread instead.
9187 Return the article number moved to, or nil if moving was impossible."
9188   (let ((level (gnus-summary-thread-level))
9189         (way (if previous -1 1))
9190         (beg (point)))
9191     (forward-line way)
9192     (while (and (not (eobp))
9193                 (< level (gnus-summary-thread-level)))
9194       (forward-line way))
9195     (if (eobp)
9196         (progn
9197           (goto-char beg)
9198           nil)
9199       (setq beg (point))
9200       (prog1
9201           (gnus-summary-article-number)
9202         (goto-char beg)))))
9203
9204 (defun gnus-summary-next-thread (n &optional silent)
9205   "Go to the same level next N'th thread.
9206 If N is negative, search backward instead.
9207 Returns the difference between N and the number of skips actually
9208 done.
9209
9210 If SILENT, don't output messages."
9211   (interactive "p")
9212   (let ((backward (< n 0))
9213         (n (abs n)))
9214     (while (and (> n 0)
9215                 (gnus-summary-go-to-next-thread backward))
9216       (decf n))
9217     (unless silent
9218       (gnus-summary-position-point))
9219     (when (and (not silent) (/= 0 n))
9220       (gnus-message 7 "No more threads"))
9221     n))
9222
9223 (defun gnus-summary-prev-thread (n)
9224   "Go to the same level previous N'th thread.
9225 Returns the difference between N and the number of skips actually
9226 done."
9227   (interactive "p")
9228   (gnus-summary-next-thread (- n)))
9229
9230 (defun gnus-summary-go-down-thread ()
9231   "Go down one level in the current thread."
9232   (let ((children (gnus-summary-article-children)))
9233     (when children
9234       (gnus-summary-goto-subject (car children)))))
9235
9236 (defun gnus-summary-go-up-thread ()
9237   "Go up one level in the current thread."
9238   (let ((parent (gnus-summary-article-parent)))
9239     (when parent
9240       (gnus-summary-goto-subject parent))))
9241
9242 (defun gnus-summary-down-thread (n)
9243   "Go down thread N steps.
9244 If N is negative, go up instead.
9245 Returns the difference between N and how many steps down that were
9246 taken."
9247   (interactive "p")
9248   (let ((up (< n 0))
9249         (n (abs n)))
9250     (while (and (> n 0)
9251                 (if up (gnus-summary-go-up-thread)
9252                   (gnus-summary-go-down-thread)))
9253       (setq n (1- n)))
9254     (gnus-summary-position-point)
9255     (when (/= 0 n)
9256       (gnus-message 7 "Can't go further"))
9257     n))
9258
9259 (defun gnus-summary-up-thread (n)
9260   "Go up thread N steps.
9261 If N is negative, go up instead.
9262 Returns the difference between N and how many steps down that were
9263 taken."
9264   (interactive "p")
9265   (gnus-summary-down-thread (- n)))
9266
9267 (defun gnus-summary-top-thread ()
9268   "Go to the top of the thread."
9269   (interactive)
9270   (while (gnus-summary-go-up-thread))
9271   (gnus-summary-article-number))
9272
9273 (defun gnus-summary-kill-thread (&optional unmark)
9274   "Mark articles under current thread as read.
9275 If the prefix argument is positive, remove any kinds of marks.
9276 If the prefix argument is negative, tick articles instead."
9277   (interactive "P")
9278   (when unmark
9279     (setq unmark (prefix-numeric-value unmark)))
9280   (let ((articles (gnus-summary-articles-in-thread)))
9281     (save-excursion
9282       ;; Expand the thread.
9283       (gnus-summary-show-thread)
9284       ;; Mark all the articles.
9285       (while articles
9286         (gnus-summary-goto-subject (car articles))
9287         (cond ((null unmark)
9288                (gnus-summary-mark-article-as-read gnus-killed-mark))
9289               ((> unmark 0)
9290                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9291               (t
9292                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9293         (setq articles (cdr articles))))
9294     ;; Hide killed subtrees.
9295     (and (null unmark)
9296          gnus-thread-hide-killed
9297          (gnus-summary-hide-thread))
9298     ;; If marked as read, go to next unread subject.
9299     (when (null unmark)
9300       ;; Go to next unread subject.
9301       (gnus-summary-next-subject 1 t)))
9302   (gnus-set-mode-line 'summary))
9303
9304 ;; Summary sorting commands
9305
9306 (defun gnus-summary-sort-by-number (&optional reverse)
9307   "Sort the summary buffer by article number.
9308 Argument REVERSE means reverse order."
9309   (interactive "P")
9310   (gnus-summary-sort 'number reverse))
9311
9312 (defun gnus-summary-sort-by-author (&optional reverse)
9313   "Sort the summary buffer by author name alphabetically.
9314 If `case-fold-search' is non-nil, case of letters is ignored.
9315 Argument REVERSE means reverse order."
9316   (interactive "P")
9317   (gnus-summary-sort 'author reverse))
9318
9319 (defun gnus-summary-sort-by-subject (&optional reverse)
9320   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9321 If `case-fold-search' is non-nil, case of letters is ignored.
9322 Argument REVERSE means reverse order."
9323   (interactive "P")
9324   (gnus-summary-sort 'subject reverse))
9325
9326 (defun gnus-summary-sort-by-date (&optional reverse)
9327   "Sort the summary buffer by date.
9328 Argument REVERSE means reverse order."
9329   (interactive "P")
9330   (gnus-summary-sort 'date reverse))
9331
9332 (defun gnus-summary-sort-by-score (&optional reverse)
9333   "Sort the summary buffer by score.
9334 Argument REVERSE means reverse order."
9335   (interactive "P")
9336   (gnus-summary-sort 'score reverse))
9337
9338 (defun gnus-summary-sort-by-lines (&optional reverse)
9339   "Sort the summary buffer by the number of lines.
9340 Argument REVERSE means reverse order."
9341   (interactive "P")
9342   (gnus-summary-sort 'lines reverse))
9343
9344 (defun gnus-summary-sort-by-chars (&optional reverse)
9345   "Sort the summary buffer by article length.
9346 Argument REVERSE means reverse order."
9347   (interactive "P")
9348   (gnus-summary-sort 'chars reverse))
9349
9350 (defun gnus-summary-sort (predicate reverse)
9351   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9352   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9353          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9354          (gnus-thread-sort-functions
9355           (if (not reverse)
9356               thread
9357             `(lambda (t1 t2)
9358                (,thread t2 t1))))
9359          (gnus-sort-gathered-threads-function
9360           gnus-thread-sort-functions)
9361          (gnus-article-sort-functions
9362           (if (not reverse)
9363               article
9364             `(lambda (t1 t2)
9365                (,article t2 t1))))
9366          (buffer-read-only)
9367          (gnus-summary-prepare-hook nil))
9368     ;; We do the sorting by regenerating the threads.
9369     (gnus-summary-prepare)
9370     ;; Hide subthreads if needed.
9371     (when (and gnus-show-threads gnus-thread-hide-subtree)
9372       (gnus-summary-hide-all-threads))))
9373
9374 ;; Summary saving commands.
9375
9376 (defun gnus-summary-save-article (&optional n not-saved)
9377   "Save the current article using the default saver function.
9378 If N is a positive number, save the N next articles.
9379 If N is a negative number, save the N previous articles.
9380 If N is nil and any articles have been marked with the process mark,
9381 save those articles instead.
9382 The variable `gnus-default-article-saver' specifies the saver function."
9383   (interactive "P")
9384   (let* ((articles (gnus-summary-work-articles n))
9385          (save-buffer (save-excursion
9386                         (nnheader-set-temp-buffer " *Gnus Save*")))
9387          (num (length articles))
9388          header file)
9389     (dolist (article articles)
9390       (setq header (gnus-summary-article-header article))
9391       (if (not (vectorp header))
9392           ;; This is a pseudo-article.
9393           (if (assq 'name header)
9394               (gnus-copy-file (cdr (assq 'name header)))
9395             (gnus-message 1 "Article %d is unsaveable" article))
9396         ;; This is a real article.
9397         (save-window-excursion
9398           (gnus-summary-select-article t nil nil article))
9399         (save-excursion
9400           (set-buffer save-buffer)
9401           (erase-buffer)
9402           (insert-buffer-substring gnus-original-article-buffer))
9403         (setq file (gnus-article-save save-buffer file num))
9404         (gnus-summary-remove-process-mark article)
9405         (unless not-saved
9406           (gnus-summary-set-saved-mark article))))
9407     (gnus-kill-buffer save-buffer)
9408     (gnus-summary-position-point)
9409     (gnus-set-mode-line 'summary)
9410     n))
9411
9412 (defun gnus-summary-pipe-output (&optional arg)
9413   "Pipe the current article to a subprocess.
9414 If N is a positive number, pipe the N next articles.
9415 If N is a negative number, pipe the N previous articles.
9416 If N is nil and any articles have been marked with the process mark,
9417 pipe those articles instead."
9418   (interactive "P")
9419   (require 'gnus-art)
9420   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9421     (gnus-summary-save-article arg t))
9422   (gnus-configure-windows 'pipe))
9423
9424 (defun gnus-summary-save-article-mail (&optional arg)
9425   "Append the current article to an mail file.
9426 If N is a positive number, save the N next articles.
9427 If N is a negative number, save the N previous articles.
9428 If N is nil and any articles have been marked with the process mark,
9429 save those articles instead."
9430   (interactive "P")
9431   (require 'gnus-art)
9432   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9433     (gnus-summary-save-article arg)))
9434
9435 (defun gnus-summary-save-article-rmail (&optional arg)
9436   "Append the current article to an rmail 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-rmail))
9444     (gnus-summary-save-article arg)))
9445
9446 (defun gnus-summary-save-article-file (&optional arg)
9447   "Append the current article to a 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-file))
9455     (gnus-summary-save-article arg)))
9456
9457 (defun gnus-summary-write-article-file (&optional arg)
9458   "Write the current article to a file, deleting the previous 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-write-to-file))
9466     (gnus-summary-save-article arg)))
9467
9468 (defun gnus-summary-save-article-body-file (&optional arg)
9469   "Append the current article body to a 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-save-body-in-file))
9477     (gnus-summary-save-article arg)))
9478
9479 (defun gnus-summary-pipe-message (program)
9480   "Pipe the current article through PROGRAM."
9481   (interactive "sProgram: ")
9482   (gnus-summary-select-article)
9483   (let ((mail-header-separator ""))
9484     (gnus-eval-in-buffer-window gnus-article-buffer
9485       (save-restriction
9486         (widen)
9487         (let ((start (window-start))
9488               buffer-read-only)
9489           (message-pipe-buffer-body program)
9490           (set-window-start (get-buffer-window (current-buffer)) start))))))
9491
9492 (defun gnus-get-split-value (methods)
9493   "Return a value based on the split METHODS."
9494   (let (split-name method result match)
9495     (when methods
9496       (save-excursion
9497         (set-buffer gnus-original-article-buffer)
9498         (save-restriction
9499           (nnheader-narrow-to-headers)
9500           (while (and methods (not split-name))
9501             (goto-char (point-min))
9502             (setq method (pop methods))
9503             (setq match (car method))
9504             (when (cond
9505                    ((stringp match)
9506                     ;; Regular expression.
9507                     (ignore-errors
9508                       (re-search-forward match nil t)))
9509                    ((gnus-functionp match)
9510                     ;; Function.
9511                     (save-restriction
9512                       (widen)
9513                       (setq result (funcall match gnus-newsgroup-name))))
9514                    ((consp match)
9515                     ;; Form.
9516                     (save-restriction
9517                       (widen)
9518                       (setq result (eval match)))))
9519               (setq split-name (cdr method))
9520               (cond ((stringp result)
9521                      (push (expand-file-name
9522                             result gnus-article-save-directory)
9523                            split-name))
9524                     ((consp result)
9525                      (setq split-name (append result split-name)))))))))
9526     (nreverse split-name)))
9527
9528 (defun gnus-valid-move-group-p (group)
9529   (and (boundp group)
9530        (symbol-name group)
9531        (symbol-value group)
9532        (gnus-get-function (gnus-find-method-for-group
9533                            (symbol-name group)) 'request-accept-article t)))
9534
9535 (defun gnus-read-move-group-name (prompt default articles prefix)
9536   "Read a group name."
9537   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9538          (minibuffer-confirm-incomplete nil) ; XEmacs
9539          (prom
9540           (format "%s %s to:"
9541                   prompt
9542                   (if (> (length articles) 1)
9543                       (format "these %d articles" (length articles))
9544                     "this article")))
9545          (to-newsgroup
9546           (cond
9547            ((null split-name)
9548             (gnus-completing-read default prom
9549                                   gnus-active-hashtb
9550                                   'gnus-valid-move-group-p
9551                                   nil prefix
9552                                   'gnus-group-history))
9553            ((= 1 (length split-name))
9554             (gnus-completing-read (car split-name) prom
9555                                   gnus-active-hashtb
9556                                   'gnus-valid-move-group-p
9557                                   nil nil
9558                                   'gnus-group-history))
9559            (t
9560             (gnus-completing-read nil prom
9561                                   (mapcar (lambda (el) (list el))
9562                                           (nreverse split-name))
9563                                   nil nil nil
9564                                   'gnus-group-history))))
9565          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9566     (when to-newsgroup
9567       (if (or (string= to-newsgroup "")
9568               (string= to-newsgroup prefix))
9569           (setq to-newsgroup default))
9570       (unless to-newsgroup
9571         (error "No group name entered"))
9572       (or (gnus-active to-newsgroup)
9573           (gnus-activate-group to-newsgroup nil nil to-method)
9574           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9575                                      to-newsgroup))
9576               (or (and (gnus-request-create-group to-newsgroup to-method)
9577                        (gnus-activate-group
9578                         to-newsgroup nil nil to-method)
9579                        (gnus-subscribe-group to-newsgroup))
9580                   (error "Couldn't create group %s" to-newsgroup)))
9581           (error "No such group: %s" to-newsgroup)))
9582     to-newsgroup))
9583
9584 (defun gnus-summary-save-parts (type dir n &optional reverse)
9585   "Save parts matching TYPE to DIR.
9586 If REVERSE, save parts that do not match TYPE."
9587   (interactive
9588    (list (read-string "Save parts of type: " 
9589                       (or (car gnus-summary-save-parts-type-history)
9590                           gnus-summary-save-parts-default-mime)
9591                       'gnus-summary-save-parts-type-history)
9592          (setq gnus-summary-save-parts-last-directory
9593                (read-file-name "Save to directory: " 
9594                                gnus-summary-save-parts-last-directory
9595                                nil t))
9596          current-prefix-arg))
9597   (gnus-summary-iterate n
9598     (let ((gnus-display-mime-function nil)
9599           (gnus-inhibit-treatment t))
9600       (gnus-summary-select-article))
9601     (save-excursion
9602       (set-buffer gnus-article-buffer)
9603       (let ((handles (or gnus-article-mime-handles
9604                          (mm-dissect-buffer) (mm-uu-dissect))))
9605         (when handles
9606           (gnus-summary-save-parts-1 type dir handles reverse)
9607           (unless gnus-article-mime-handles ;; Don't destroy this case.
9608             (mm-destroy-parts handles)))))))
9609
9610 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9611   (if (stringp (car handle))
9612       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9613               (cdr handle))
9614     (when (if reverse
9615               (not (string-match type (mm-handle-media-type handle)))
9616             (string-match type (mm-handle-media-type handle)))
9617       (let ((file (expand-file-name
9618                    (file-name-nondirectory
9619                     (or
9620                      (mail-content-type-get
9621                       (mm-handle-disposition handle) 'filename)
9622                      (concat gnus-newsgroup-name
9623                              "." (number-to-string
9624                                   (cdr gnus-article-current)))))
9625                    dir)))
9626         (unless (file-exists-p file)
9627           (mm-save-part-to-file handle file))))))
9628
9629 ;; Summary extract commands
9630
9631 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9632   (let ((buffer-read-only nil)
9633         (article (gnus-summary-article-number))
9634         after-article b e)
9635     (unless (gnus-summary-goto-subject article)
9636       (error "No such article: %d" article))
9637     (gnus-summary-position-point)
9638     ;; If all commands are to be bunched up on one line, we collect
9639     ;; them here.
9640     (unless gnus-view-pseudos-separately
9641       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9642             files action)
9643         (while ps
9644           (setq action (cdr (assq 'action (car ps))))
9645           (setq files (list (cdr (assq 'name (car ps)))))
9646           (while (and ps (cdr ps)
9647                       (string= (or action "1")
9648                                (or (cdr (assq 'action (cadr ps))) "2")))
9649             (push (cdr (assq 'name (cadr ps))) files)
9650             (setcdr ps (cddr ps)))
9651           (when files
9652             (when (not (string-match "%s" action))
9653               (push " " files))
9654             (push " " files)
9655             (when (assq 'execute (car ps))
9656               (setcdr (assq 'execute (car ps))
9657                       (funcall (if (string-match "%s" action)
9658                                    'format 'concat)
9659                                action
9660                                (mapconcat
9661                                 (lambda (f)
9662                                   (if (equal f " ")
9663                                       f
9664                                     (gnus-quote-arg-for-sh-or-csh f)))
9665                                 files " ")))))
9666           (setq ps (cdr ps)))))
9667     (if (and gnus-view-pseudos (not not-view))
9668         (while pslist
9669           (when (assq 'execute (car pslist))
9670             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9671                                   (eq gnus-view-pseudos 'not-confirm)))
9672           (setq pslist (cdr pslist)))
9673       (save-excursion
9674         (while pslist
9675           (setq after-article (or (cdr (assq 'article (car pslist)))
9676                                   (gnus-summary-article-number)))
9677           (gnus-summary-goto-subject after-article)
9678           (forward-line 1)
9679           (setq b (point))
9680           (insert "    " (file-name-nondirectory
9681                           (cdr (assq 'name (car pslist))))
9682                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9683           (setq e (point))
9684           (forward-line -1)             ; back to `b'
9685           (gnus-add-text-properties
9686            b (1- e) (list 'gnus-number gnus-reffed-article-number
9687                           gnus-mouse-face-prop gnus-mouse-face))
9688           (gnus-data-enter
9689            after-article gnus-reffed-article-number
9690            gnus-unread-mark b (car pslist) 0 (- e b))
9691           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9692           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9693           (setq pslist (cdr pslist)))))))
9694
9695 (defun gnus-pseudos< (p1 p2)
9696   (let ((c1 (cdr (assq 'action p1)))
9697         (c2 (cdr (assq 'action p2))))
9698     (and c1 c2 (string< c1 c2))))
9699
9700 (defun gnus-request-pseudo-article (props)
9701   (cond ((assq 'execute props)
9702          (gnus-execute-command (cdr (assq 'execute props)))))
9703   (let ((gnus-current-article (gnus-summary-article-number)))
9704     (gnus-run-hooks 'gnus-mark-article-hook)))
9705
9706 (defun gnus-execute-command (command &optional automatic)
9707   (save-excursion
9708     (gnus-article-setup-buffer)
9709     (set-buffer gnus-article-buffer)
9710     (setq buffer-read-only nil)
9711     (let ((command (if automatic command
9712                      (read-string "Command: " (cons command 0)))))
9713       (erase-buffer)
9714       (insert "$ " command "\n\n")
9715       (if gnus-view-pseudo-asynchronously
9716           (start-process "gnus-execute" (current-buffer) shell-file-name
9717                          shell-command-switch command)
9718         (call-process shell-file-name nil t nil
9719                       shell-command-switch command)))))
9720
9721 ;; Summary kill commands.
9722
9723 (defun gnus-summary-edit-global-kill (article)
9724   "Edit the \"global\" kill file."
9725   (interactive (list (gnus-summary-article-number)))
9726   (gnus-group-edit-global-kill article))
9727
9728 (defun gnus-summary-edit-local-kill ()
9729   "Edit a local kill file applied to the current newsgroup."
9730   (interactive)
9731   (setq gnus-current-headers (gnus-summary-article-header))
9732   (gnus-group-edit-local-kill
9733    (gnus-summary-article-number) gnus-newsgroup-name))
9734
9735 ;;; Header reading.
9736
9737 (defun gnus-read-header (id &optional header)
9738   "Read the headers of article ID and enter them into the Gnus system."
9739   (let ((group gnus-newsgroup-name)
9740         (gnus-override-method
9741          (or
9742           gnus-override-method
9743           (and (gnus-news-group-p gnus-newsgroup-name)
9744                (car (gnus-refer-article-methods)))))
9745         where)
9746     ;; First we check to see whether the header in question is already
9747     ;; fetched.
9748     (if (stringp id)
9749         ;; This is a Message-ID.
9750         (setq header (or header (gnus-id-to-header id)))
9751       ;; This is an article number.
9752       (setq header (or header (gnus-summary-article-header id))))
9753     (if (and header
9754              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9755         ;; We have found the header.
9756         header
9757       ;; If this is a sparse article, we have to nix out its
9758       ;; previous entry in the thread hashtb.
9759       (when (and header
9760                  (gnus-summary-article-sparse-p (mail-header-number header)))
9761         (let* ((parent (gnus-parent-id (mail-header-references header)))
9762                (thread (and parent (gnus-id-to-thread parent))))
9763           (when thread
9764             (delq (assq header thread) thread))))
9765       ;; We have to really fetch the header to this article.
9766       (save-excursion
9767         (set-buffer nntp-server-buffer)
9768         (when (setq where (gnus-request-head id group))
9769           (nnheader-fold-continuation-lines)
9770           (goto-char (point-max))
9771           (insert ".\n")
9772           (goto-char (point-min))
9773           (insert "211 ")
9774           (princ (cond
9775                   ((numberp id) id)
9776                   ((cdr where) (cdr where))
9777                   (header (mail-header-number header))
9778                   (t gnus-reffed-article-number))
9779                  (current-buffer))
9780           (insert " Article retrieved.\n"))
9781         (if (or (not where)
9782                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9783             ()                          ; Malformed head.
9784           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9785             (when (and (stringp id)
9786                        (not (string= (gnus-group-real-name group)
9787                                      (car where))))
9788               ;; If we fetched by Message-ID and the article came
9789               ;; from a different group, we fudge some bogus article
9790               ;; numbers for this article.
9791               (mail-header-set-number header gnus-reffed-article-number))
9792             (save-excursion
9793               (set-buffer gnus-summary-buffer)
9794               (decf gnus-reffed-article-number)
9795               (gnus-remove-header (mail-header-number header))
9796               (push header gnus-newsgroup-headers)
9797               (setq gnus-current-headers header)
9798               (push (mail-header-number header) gnus-newsgroup-limit)))
9799           header)))))
9800
9801 (defun gnus-remove-header (number)
9802   "Remove header NUMBER from `gnus-newsgroup-headers'."
9803   (if (and gnus-newsgroup-headers
9804            (= number (mail-header-number (car gnus-newsgroup-headers))))
9805       (pop gnus-newsgroup-headers)
9806     (let ((headers gnus-newsgroup-headers))
9807       (while (and (cdr headers)
9808                   (not (= number (mail-header-number (cadr headers)))))
9809         (pop headers))
9810       (when (cdr headers)
9811         (setcdr headers (cddr headers))))))
9812
9813 ;;;
9814 ;;; summary highlights
9815 ;;;
9816
9817 (defun gnus-highlight-selected-summary ()
9818   "Highlight selected article in summary buffer."
9819   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9820   (when gnus-summary-selected-face
9821     (save-excursion
9822       (let* ((beg (progn (beginning-of-line) (point)))
9823              (end (progn (end-of-line) (point)))
9824              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9825              (from (if (get-text-property beg gnus-mouse-face-prop)
9826                        beg
9827                      (or (next-single-property-change
9828                           beg gnus-mouse-face-prop nil end)
9829                          beg)))
9830              (to
9831               (if (= from end)
9832                   (- from 2)
9833                 (or (next-single-property-change
9834                      from gnus-mouse-face-prop nil end)
9835                     end))))
9836         ;; If no mouse-face prop on line we will have to = from = end,
9837         ;; so we highlight the entire line instead.
9838         (when (= (+ to 2) from)
9839           (setq from beg)
9840           (setq to end))
9841         (if gnus-newsgroup-selected-overlay
9842             ;; Move old overlay.
9843             (gnus-move-overlay
9844              gnus-newsgroup-selected-overlay from to (current-buffer))
9845           ;; Create new overlay.
9846           (gnus-overlay-put
9847            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9848            'face gnus-summary-selected-face))))))
9849
9850 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9851 (defun gnus-summary-highlight-line ()
9852   "Highlight current line according to `gnus-summary-highlight'."
9853   (let* ((list gnus-summary-highlight)
9854          (p (point))
9855          (end (progn (end-of-line) (point)))
9856          ;; now find out where the line starts and leave point there.
9857          (beg (progn (beginning-of-line) (point)))
9858          (article (gnus-summary-article-number))
9859          (score (or (cdr (assq (or article gnus-current-article)
9860                                gnus-newsgroup-scored))
9861                     gnus-summary-default-score 0))
9862          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9863          (inhibit-read-only t))
9864     ;; Eval the cars of the lists until we find a match.
9865     (let ((default gnus-summary-default-score))
9866       (while (and list
9867                   (not (eval (caar list))))
9868         (setq list (cdr list))))
9869     (let ((face (cdar list)))
9870       (unless (eq face (get-text-property beg 'face))
9871         (gnus-put-text-property-excluding-characters-with-faces
9872          beg end 'face
9873          (setq face (if (boundp face) (symbol-value face) face)))
9874         (when gnus-summary-highlight-line-function
9875           (funcall gnus-summary-highlight-line-function article face))))
9876     (goto-char p)))
9877
9878 (defun gnus-update-read-articles (group unread &optional compute)
9879   "Update the list of read articles in GROUP."
9880   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9881          (entry (gnus-gethash group gnus-newsrc-hashtb))
9882          (info (nth 2 entry))
9883          (prev 1)
9884          (unread (sort (copy-sequence unread) '<))
9885          read)
9886     (if (or (not info) (not active))
9887         ;; There is no info on this group if it was, in fact,
9888         ;; killed.  Gnus stores no information on killed groups, so
9889         ;; there's nothing to be done.
9890         ;; One could store the information somewhere temporarily,
9891         ;; perhaps...  Hmmm...
9892         ()
9893       ;; Remove any negative articles numbers.
9894       (while (and unread (< (car unread) 0))
9895         (setq unread (cdr unread)))
9896       ;; Remove any expired article numbers
9897       (while (and unread (< (car unread) (car active)))
9898         (setq unread (cdr unread)))
9899       ;; Compute the ranges of read articles by looking at the list of
9900       ;; unread articles.
9901       (while unread
9902         (when (/= (car unread) prev)
9903           (push (if (= prev (1- (car unread))) prev
9904                   (cons prev (1- (car unread))))
9905                 read))
9906         (setq prev (1+ (car unread)))
9907         (setq unread (cdr unread)))
9908       (when (<= prev (cdr active))
9909         (push (cons prev (cdr active)) read))
9910       (setq read (if (> (length read) 1) (nreverse read) read))
9911       (if compute
9912           read
9913         (save-excursion
9914           (let (setmarkundo)
9915             ;; Propagate the read marks to the backend.
9916             (when (gnus-check-backend-function 'request-set-mark group)
9917               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9918                     (add (gnus-remove-from-range read (gnus-info-read info))))
9919                 (when (or add del)
9920                   (unless (gnus-check-group group)
9921                     (error "Can't open server for %s" group))
9922                   (gnus-request-set-mark
9923                    group (delq nil (list (if add (list add 'add '(read)))
9924                                          (if del (list del 'del '(read))))))
9925                   (setq setmarkundo
9926                         `(gnus-request-set-mark
9927                           ,group
9928                           ',(delq nil (list
9929                                        (if del (list del 'add '(read)))
9930                                        (if add (list add 'del '(read))))))))))
9931             (set-buffer gnus-group-buffer)
9932             (gnus-undo-register
9933               `(progn
9934                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9935                  (gnus-info-set-read ',info ',(gnus-info-read info))
9936                  (gnus-get-unread-articles-in-group ',info 
9937                                                     (gnus-active ,group))
9938                  (gnus-group-update-group ,group t)
9939                  ,setmarkundo))))
9940         ;; Enter this list into the group info.
9941         (gnus-info-set-read info read)
9942         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9943         (gnus-get-unread-articles-in-group info (gnus-active group))
9944         t))))
9945
9946 (defun gnus-offer-save-summaries ()
9947   "Offer to save all active summary buffers."
9948   (save-excursion
9949     (let ((buflist (buffer-list))
9950           buffers bufname)
9951       ;; Go through all buffers and find all summaries.
9952       (while buflist
9953         (and (setq bufname (buffer-name (car buflist)))
9954              (string-match "Summary" bufname)
9955              (save-excursion
9956                (set-buffer bufname)
9957                ;; We check that this is, indeed, a summary buffer.
9958                (and (eq major-mode 'gnus-summary-mode)
9959                     ;; Also make sure this isn't bogus.
9960                     gnus-newsgroup-prepared
9961                     ;; Also make sure that this isn't a dead summary buffer.
9962                     (not gnus-dead-summary-mode)))
9963              (push bufname buffers))
9964         (setq buflist (cdr buflist)))
9965       ;; Go through all these summary buffers and offer to save them.
9966       (when buffers
9967         (map-y-or-n-p
9968          "Update summary buffer %s? "
9969          (lambda (buf)
9970            (switch-to-buffer buf)
9971            (gnus-summary-exit))
9972          buffers)))))
9973
9974
9975 ;;; @ for mime-partial
9976 ;;;
9977
9978 (defun gnus-request-partial-message ()
9979   (save-excursion
9980     (let ((number (gnus-summary-article-number))
9981           (group gnus-newsgroup-name)
9982           (mother gnus-article-buffer))
9983       (set-buffer (get-buffer-create " *Partial Article*"))
9984       (erase-buffer)
9985       (setq mime-preview-buffer mother)
9986       (gnus-request-article-this-buffer number group)
9987       (mime-parse-buffer)
9988       )))
9989
9990 (autoload 'mime-combine-message/partial-pieces-automatically
9991   "mime-partial"
9992   "Internal method to combine message/partial messages automatically.")
9993
9994 (mime-add-condition
9995  'action '((type . message)(subtype . partial)
9996            (major-mode . gnus-original-article-mode)
9997            (method . mime-combine-message/partial-pieces-automatically)
9998            (summary-buffer-exp . gnus-summary-buffer)
9999            (request-partial-message-method . gnus-request-partial-message)
10000            ))
10001
10002
10003 ;;; @ for message/rfc822
10004 ;;;
10005
10006 (defun gnus-mime-extract-message/rfc822 (entity situation)
10007   (let (group article num cwin swin cur)
10008     (with-temp-buffer
10009       (mime-insert-entity-content entity)
10010       (setq group (or (cdr (assq 'group situation))
10011                       (completing-read "Group: "
10012                                        gnus-active-hashtb
10013                                        nil
10014                                        (gnus-read-active-file-p)
10015                                        gnus-newsgroup-name))
10016             article (gnus-request-accept-article group)))
10017     (when (and (consp article)
10018                (numberp (setq article (cdr article))))
10019       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10020             cwin (get-buffer-window (current-buffer) t))
10021       (save-window-excursion
10022         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10023             (select-window swin)
10024           (set-buffer gnus-summary-buffer))
10025         (setq cur gnus-current-article)
10026         (forward-line num)
10027         (let (gnus-show-threads)
10028           (gnus-summary-goto-subject article t))
10029         (gnus-summary-clear-mark-forward 1)
10030         (gnus-summary-goto-subject cur))
10031       (when (and cwin (window-frame cwin))
10032         (select-frame (window-frame cwin)))
10033       (when (boundp 'mime-acting-situation-to-override)
10034         (set-alist 'mime-acting-situation-to-override
10035                    'group
10036                    group)
10037         (set-alist 'mime-acting-situation-to-override
10038                    'after-method
10039                    `(progn
10040                       (save-current-buffer
10041                         (set-buffer gnus-group-buffer)
10042                         (gnus-activate-group ,group))
10043                       (gnus-summary-goto-article ,cur
10044                                                  gnus-show-all-headers)))
10045         (set-alist 'mime-acting-situation-to-override
10046                    'number num)))))
10047
10048 (mime-add-condition
10049  'action '((type . message)(subtype . rfc822)
10050            (major-mode . gnus-original-article-mode)
10051            (method . gnus-mime-extract-message/rfc822)
10052            (mode . "extract")
10053            ))
10054
10055 (mime-add-condition
10056  'action '((type . message)(subtype . news)
10057            (major-mode . gnus-original-article-mode)
10058            (method . gnus-mime-extract-message/rfc822)
10059            (mode . "extract")
10060            ))
10061
10062 (defun gnus-mime-extract-multipart (entity situation)
10063   (let ((children (mime-entity-children entity))
10064         mime-acting-situation-to-override
10065         f)
10066     (while children
10067       (mime-play-entity (car children)
10068                         (cons (assq 'mode situation)
10069                               mime-acting-situation-to-override))
10070       (setq children (cdr children)))
10071     (if (setq f (cdr (assq 'after-method
10072                            mime-acting-situation-to-override)))
10073         (eval f)
10074       )))
10075
10076 (mime-add-condition
10077  'action '((type . multipart)
10078            (method . gnus-mime-extract-multipart)
10079            (mode . "extract")
10080            )
10081  'with-default)
10082
10083
10084 ;;; @ end
10085 ;;;
10086
10087 (defun gnus-summary-setup-default-charset ()
10088   "Setup newsgroup default charset."
10089   (if (equal gnus-newsgroup-name "nndraft:drafts")
10090       (setq gnus-newsgroup-charset nil)
10091     (let* ((name (and gnus-newsgroup-name
10092                       (gnus-group-real-name gnus-newsgroup-name)))
10093            (ignored-charsets
10094             (or gnus-newsgroup-ephemeral-ignored-charsets
10095                 (append
10096                  (and gnus-newsgroup-name
10097                       (or (gnus-group-find-parameter gnus-newsgroup-name
10098                                                      'ignored-charsets t)
10099                           (let ((alist gnus-group-ignored-charsets-alist)
10100                                 elem (charsets nil))
10101                             (while (setq elem (pop alist))
10102                               (when (and name
10103                                          (string-match (car elem) name))
10104                                 (setq alist nil
10105                                       charsets (cdr elem))))
10106                             charsets)))
10107                  gnus-newsgroup-ignored-charsets))))
10108       (setq gnus-newsgroup-charset
10109             (or gnus-newsgroup-ephemeral-charset
10110                 (and gnus-newsgroup-name
10111                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
10112                          (let ((alist gnus-group-charset-alist)
10113                                elem charset)
10114                            (while (setq elem (pop alist))
10115                              (when (and name
10116                                         (string-match (car elem) name))
10117                                (setq alist nil
10118                                      charset (cadr elem))))
10119                            charset)))
10120                 gnus-default-charset))
10121       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10122            ignored-charsets))))
10123
10124 ;;;
10125 ;;; Mime Commands
10126 ;;;
10127
10128 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10129   "Display the current article buffer fully MIME-buttonized.
10130 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10131 treated as multipart/mixed."
10132   (interactive "P")
10133   (require 'gnus-art)
10134   (let ((gnus-unbuttonized-mime-types nil)
10135         (gnus-mime-display-multipart-as-mixed show-all-parts))
10136     (gnus-summary-show-article)))
10137
10138 (defun gnus-summary-repair-multipart (article)
10139   "Add a Content-Type header to a multipart article without one."
10140   (interactive (list (gnus-summary-article-number)))
10141   (gnus-with-article article
10142     (message-narrow-to-head)
10143     (message-remove-header "Mime-Version")
10144     (goto-char (point-max))
10145     (insert "Mime-Version: 1.0\n")
10146     (widen)
10147     (when (search-forward "\n--" nil t)
10148       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10149         (message-narrow-to-head)
10150         (message-remove-header "Content-Type")
10151         (goto-char (point-max))
10152         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10153                         separator))
10154         (widen))))
10155   (let (gnus-mark-article-hook)
10156     (gnus-summary-select-article t t nil article)))
10157
10158 (defun gnus-summary-toggle-display-buttonized ()
10159   "Toggle the buttonizing of the article buffer."
10160   (interactive)
10161   (require 'gnus-art)
10162   (if (setq gnus-inhibit-mime-unbuttonizing
10163             (not gnus-inhibit-mime-unbuttonizing))
10164       (let ((gnus-unbuttonized-mime-types nil))
10165         (gnus-summary-show-article))
10166     (gnus-summary-show-article)))
10167
10168 ;;;
10169 ;;; Intelli-mouse commmands
10170 ;;;
10171
10172 (defun gnus-wheel-summary-scroll (event)
10173   (interactive "e")
10174   (let ((amount (if (memq 'shift (event-modifiers event))
10175                     (car gnus-wheel-scroll-amount)
10176                   (cdr gnus-wheel-scroll-amount)))
10177         (direction (- (* (static-if (featurep 'xemacs)
10178                              (event-button event)
10179                            (cond ((eq 'mouse-4 (event-basic-type event))
10180                                   4)
10181                                  ((eq 'mouse-5 (event-basic-type event))
10182                                   5)))
10183                          2) 9))
10184         edge)
10185     (gnus-summary-scroll-up (* amount direction))
10186     (when (gnus-eval-in-buffer-window gnus-article-buffer
10187             (save-restriction
10188               (widen)
10189               (and (if (< 0 direction)
10190                        (gnus-article-next-page 0)
10191                      (gnus-article-prev-page 0)
10192                      (bobp))
10193                    (if (setq edge (get-text-property
10194                                    (point-min) 'gnus-wheel-edge))
10195                        (setq edge (* edge direction))
10196                      (setq edge -1))
10197                    (or (plusp edge)
10198                        (let ((buffer-read-only nil)
10199                              (inhibit-read-only t))
10200                          (put-text-property (point-min) (point-max)
10201                                             'gnus-wheel-edge direction)
10202                          nil))
10203                    (or (> edge gnus-wheel-edge-resistance)
10204                        (let ((buffer-read-only nil)
10205                              (inhibit-read-only t))
10206                          (put-text-property (point-min) (point-max)
10207                                             'gnus-wheel-edge
10208                                             (* (1+ edge) direction))
10209                          nil))
10210                    (eq last-command 'gnus-wheel-summary-scroll))))
10211       (gnus-summary-next-article nil nil (minusp direction)))))
10212
10213 (defun gnus-wheel-install ()
10214   "Enable mouse wheel support on summary window."
10215   (when gnus-use-wheel
10216     (let ((keys
10217            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10218       (dolist (key keys)
10219         (define-key gnus-summary-mode-map key
10220           'gnus-wheel-summary-scroll)))))
10221
10222 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10223
10224 ;;;
10225 ;;; Traditional PGP commmands
10226 ;;;
10227
10228 (defun gnus-summary-decrypt-article (&optional force)
10229   "Decrypt the current article in traditional PGP way.
10230 This will have permanent effect only in mail groups.
10231 If FORCE is non-nil, allow editing of articles even in read-only
10232 groups."
10233   (interactive "P")
10234   (gnus-summary-select-article t)
10235   (gnus-eval-in-buffer-window gnus-article-buffer
10236     (save-excursion
10237       (save-restriction
10238         (widen)
10239         (goto-char (point-min))
10240         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10241           (error "Not a traditional PGP message!"))
10242         (let ((armor-start (match-beginning 0)))
10243           (if (and (pgg-decrypt-region armor-start (point-max))
10244                    (or force (not (gnus-group-read-only-p))))
10245               (let ((inhibit-read-only t)
10246                     buffer-read-only)
10247                 (delete-region armor-start
10248                                (progn
10249                                  (re-search-forward "^-+END PGP" nil t)
10250                                  (beginning-of-line 2)
10251                                  (point)))
10252                 (insert-buffer-substring pgg-output-buffer))))))))
10253
10254 (defun gnus-summary-verify-article ()
10255   "Verify the current article in traditional PGP way."
10256   (interactive)
10257   (save-excursion
10258     (set-buffer gnus-original-article-buffer)
10259     (goto-char (point-min))
10260     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10261       (error "Not a traditional PGP message!"))
10262     (re-search-forward "^-+END PGP" nil t)
10263     (beginning-of-line 2)
10264     (call-interactively (function pgg-verify-region))))
10265
10266 ;;;
10267 ;;; Generic summary marking commands
10268 ;;;
10269
10270 (defvar gnus-summary-marking-alist
10271   '((read gnus-del-mark "d")
10272     (unread gnus-unread-mark "u")
10273     (ticked gnus-ticked-mark "!")
10274     (dormant gnus-dormant-mark "?")
10275     (expirable gnus-expirable-mark "e"))
10276   "An alist of names/marks/keystrokes.")
10277
10278 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10279 (defvar gnus-summary-mark-map)
10280
10281 (defun gnus-summary-make-all-marking-commands ()
10282   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10283   (dolist (elem gnus-summary-marking-alist)
10284     (apply 'gnus-summary-make-marking-command elem)))
10285
10286 (defun gnus-summary-make-marking-command (name mark keystroke)
10287   (let ((map (make-sparse-keymap)))
10288     (define-key gnus-summary-generic-mark-map keystroke map)
10289     (dolist (lway `((next "next" next nil "n")
10290                     (next-unread "next unread" next t "N")
10291                     (prev "previous" prev nil "p")
10292                     (prev-unread "previous unread" prev t "P")
10293                     (nomove "" nil nil ,keystroke)))
10294       (let ((func (gnus-summary-make-marking-command-1
10295                    mark (car lway) lway name)))
10296         (setq func (eval func))
10297         (define-key map (nth 4 lway) func)))))
10298
10299 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10300   `(defun ,(intern
10301             (format "gnus-summary-put-mark-as-%s%s"
10302                     name (if (eq way 'nomove)
10303                              ""
10304                            (concat "-" (symbol-name way)))))
10305      (n)
10306      ,(format
10307        "Mark the current article as %s%s.
10308 If N, the prefix, then repeat N times.
10309 If N is negative, move in reverse order.
10310 The difference between N and the actual number of articles marked is
10311 returned."
10312        name (car (cdr lway)))
10313      (interactive "p")
10314      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10315
10316 (defun gnus-summary-generic-mark (n mark move unread)
10317   "Mark N articles with MARK."
10318   (unless (eq major-mode 'gnus-summary-mode)
10319     (error "This command can only be used in the summary buffer"))
10320   (gnus-summary-show-thread)
10321   (let ((nummove
10322          (cond
10323           ((eq move 'next) 1)
10324           ((eq move 'prev) -1)
10325           (t 0))))
10326     (if (zerop nummove)
10327         (setq n 1)
10328       (when (< n 0)
10329         (setq n (abs n)
10330               nummove (* -1 nummove))))
10331     (while (and (> n 0)
10332                 (gnus-summary-mark-article nil mark)
10333                 (zerop (gnus-summary-next-subject nummove unread t)))
10334       (setq n (1- n)))
10335     (when (/= 0 n)
10336       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10337     (gnus-summary-recenter)
10338     (gnus-summary-position-point)
10339     (gnus-set-mode-line 'summary)
10340     n))
10341
10342 (gnus-summary-make-all-marking-commands)
10343
10344 (gnus-ems-redefine)
10345
10346 (provide 'gnus-sum)
10347
10348 (run-hooks 'gnus-sum-load-hook)
10349
10350 ;;; gnus-sum.el ends here