6c3e5c46edc195684d3234332d710cb6ae32dffd
[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 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include: `gnus-simplify-subject-re',
145 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
146   :group 'gnus-thread
147   :type '(repeat function))
148
149 (defcustom gnus-simplify-ignored-prefixes nil
150   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  regexp))
154
155 (defcustom gnus-build-sparse-threads nil
156   "*If non-nil, fill in the gaps in threads.
157 If `some', only fill in the gaps that are needed to tie loose threads
158 together.  If `more', fill in all leaf nodes that Gnus can find.  If
159 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
160   :group 'gnus-thread
161   :type '(choice (const :tag "off" nil)
162                  (const some)
163                  (const more)
164                  (sexp :menu-tag "all" t)))
165
166 (defcustom gnus-summary-thread-gathering-function
167   'gnus-gather-threads-by-subject
168   "*Function used for gathering loose threads.
169 There are two pre-defined functions: `gnus-gather-threads-by-subject',
170 which only takes Subjects into consideration; and
171 `gnus-gather-threads-by-references', which compared the References
172 headers of the articles to find matches."
173   :group 'gnus-thread
174   :type '(radio (function-item gnus-gather-threads-by-subject)
175                 (function-item gnus-gather-threads-by-references)
176                 (function :tag "other")))
177
178 (defcustom gnus-summary-same-subject ""
179   "*String indicating that the current article has the same subject as the previous.
180 This variable will only be used if the value of
181 `gnus-summary-make-false-root' is `empty'."
182   :group 'gnus-summary-format
183   :type 'string)
184
185 (defcustom gnus-summary-goto-unread t
186   "*If t, many commands will go to the next unread article.
187 This applies to marking commands as well as other commands that
188 \"naturally\" select the next article, like, for instance, `SPC' at
189 the end of an article.
190
191 If nil, the marking commands do NOT go to the next unread article
192 (they go to the next article instead).  If `never', commands that
193 usually go to the next unread article, will go to the next article,
194 whether it is read or not."
195   :group 'gnus-summary-marks
196   :link '(custom-manual "(gnus)Setting Marks")
197   :type '(choice (const :tag "off" nil)
198                  (const never)
199                  (sexp :menu-tag "on" t)))
200
201 (defcustom gnus-summary-default-score 0
202   "*Default article score level.
203 All scores generated by the score files will be added to this score.
204 If this variable is nil, scoring will be disabled."
205   :group 'gnus-score-default
206   :type '(choice (const :tag "disable")
207                  integer))
208
209 (defcustom gnus-summary-zcore-fuzz 0
210   "*Fuzziness factor for the zcore in the summary buffer.
211 Articles with scores closer than this to `gnus-summary-default-score'
212 will not be marked."
213   :group 'gnus-summary-format
214   :type 'integer)
215
216 (defcustom gnus-simplify-subject-fuzzy-regexp nil
217   "*Strings to be removed when doing fuzzy matches.
218 This can either be a regular expression or list of regular expressions
219 that will be removed from subject strings if fuzzy subject
220 simplification is selected."
221   :group 'gnus-thread
222   :type '(repeat regexp))
223
224 (defcustom gnus-show-threads t
225   "*If non-nil, display threads in summary mode."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-hide-subtree nil
230   "*If non-nil, hide all threads initially.
231 If threads are hidden, you have to run the command
232 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
233 to expose hidden threads."
234   :group 'gnus-thread
235   :type 'boolean)
236
237 (defcustom gnus-thread-hide-killed t
238   "*If non-nil, hide killed threads automatically."
239   :group 'gnus-thread
240   :type 'boolean)
241
242 (defcustom gnus-thread-ignore-subject t
243   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
244 If nil, articles that have different subjects from their parents will
245 start separate threads."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-operation-ignore-subject t
250   "*If non-nil, subjects will be ignored when doing thread commands.
251 This affects commands like `gnus-summary-kill-thread' and
252 `gnus-summary-lower-thread'.
253
254 If this variable is nil, articles in the same thread with different
255 subjects will not be included in the operation in question.  If this
256 variable is `fuzzy', only articles that have subjects that are fuzzily
257 equal will be included."
258   :group 'gnus-thread
259   :type '(choice (const :tag "off" nil)
260                  (const fuzzy)
261                  (sexp :tag "on" t)))
262
263 (defcustom gnus-thread-indent-level 4
264   "*Number that says how much each sub-thread should be indented."
265   :group 'gnus-thread
266   :type 'integer)
267
268 (defcustom gnus-auto-extend-newsgroup t
269   "*If non-nil, extend newsgroup forward and backward when requested."
270   :group 'gnus-summary-choose
271   :type 'boolean)
272
273 (defcustom gnus-auto-select-first t
274   "*If nil, don't select the first unread article when entering a group.
275 If this variable is `best', select the highest-scored unread article
276 in the group.  If t, select the first unread article.
277
278 This variable can also be a function to place point on a likely
279 subject line.  Useful values include `gnus-summary-first-unread-subject',
280 `gnus-summary-first-unread-article' and
281 `gnus-summary-best-unread-article'.
282
283 If you want to prevent automatic selection of the first unread article
284 in some newsgroups, set the variable to nil in
285 `gnus-select-group-hook'."
286   :group 'gnus-group-select
287   :type '(choice (const :tag "none" nil)
288                  (const best)
289                  (sexp :menu-tag "first" t)
290                  (function-item gnus-summary-first-unread-subject)
291                  (function-item gnus-summary-first-unread-article)
292                  (function-item gnus-summary-best-unread-article)))
293
294 (defcustom gnus-dont-select-after-jump-to-other-group nil
295   "If non-nil, don't select the first unread article after entering the
296 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
297 it is depend on the value of `gnus-auto-select-first' whether to select
298 or not."
299   :group 'gnus-group-select
300   :type 'boolean)
301
302 (defcustom gnus-auto-select-next t
303   "*If non-nil, offer to go to the next group from the end of the previous.
304 If the value is t and the next newsgroup is empty, Gnus will exit
305 summary mode and go back to group mode.  If the value is neither nil
306 nor t, Gnus will select the following unread newsgroup.  In
307 particular, if the value is the symbol `quietly', the next unread
308 newsgroup will be selected without any confirmation, and if it is
309 `almost-quietly', the next group will be selected without any
310 confirmation if you are located on the last article in the group.
311 Finally, if this variable is `slightly-quietly', the `Z n' command
312 will go to the next group without confirmation."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "off" nil)
315                  (const quietly)
316                  (const almost-quietly)
317                  (const slightly-quietly)
318                  (sexp :menu-tag "on" t)))
319
320 (defcustom gnus-auto-select-same nil
321   "*If non-nil, select the next article with the same subject.
322 If there are no more articles with the same subject, go to
323 the first unread article."
324   :group 'gnus-summary-maneuvering
325   :type 'boolean)
326
327 (defcustom gnus-summary-check-current nil
328   "*If non-nil, consider the current article when moving.
329 The \"unread\" movement commands will stay on the same line if the
330 current article is unread."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-auto-center-summary t
335   "*If non-nil, always center the current summary buffer.
336 In particular, if `vertical' do only vertical recentering.  If non-nil
337 and non-`vertical', do both horizontal and vertical recentering."
338   :group 'gnus-summary-maneuvering
339   :type '(choice (const :tag "none" nil)
340                  (const vertical)
341                  (integer :tag "height")
342                  (sexp :menu-tag "both" t)))
343
344 (defcustom gnus-show-all-headers nil
345   "*If non-nil, don't hide any headers."
346   :group 'gnus-article-hiding
347   :group 'gnus-article-headers
348   :type 'boolean)
349
350 (defcustom gnus-summary-ignore-duplicates nil
351   "*If non-nil, ignore articles with identical Message-ID headers."
352   :group 'gnus-summary
353   :type 'boolean)
354
355 (defcustom gnus-single-article-buffer t
356   "*If non-nil, display all articles in the same buffer.
357 If nil, each group will get its own article buffer."
358   :group 'gnus-article-various
359   :type 'boolean)
360
361 (defcustom gnus-break-pages t
362   "*If non-nil, do page breaking on articles.
363 The page delimiter is specified by the `gnus-page-delimiter'
364 variable."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-show-mime t
369   "*If non-nil, do mime processing of articles.
370 The articles will simply be fed to the function given by
371 `gnus-article-display-method-for-mime'."
372   :group 'gnus-article-mime
373   :type 'boolean)
374
375 (defcustom gnus-move-split-methods nil
376   "*Variable used to suggest where articles are to be moved to.
377 It uses the same syntax as the `gnus-split-methods' variable."
378   :group 'gnus-summary-mail
379   :type '(repeat (choice (list :value (fun) function)
380                          (cons :value ("" "") regexp (repeat string))
381                          (sexp :value nil))))
382
383 (defcustom gnus-unread-mark ?  ;Whitespace
384   "*Mark used for unread articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-ticked-mark ?!
389   "*Mark used for ticked articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-dormant-mark ??
394   "*Mark used for dormant articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-del-mark ?r
399   "*Mark used for del'd articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-read-mark ?R
404   "*Mark used for read articles."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-expirable-mark ?E
409   "*Mark used for expirable articles."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-killed-mark ?K
414   "*Mark used for killed articles."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-souped-mark ?F
419   "*Mark used for killed articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-kill-file-mark ?X
424   "*Mark used for articles killed by kill files."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-low-score-mark ?Y
429   "*Mark used for articles with a low score."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-catchup-mark ?C
434   "*Mark used for articles that are caught up."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-replied-mark ?A
439   "*Mark used for articles that have been replied to."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-cached-mark ?*
444   "*Mark used for articles that are in the cache."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-saved-mark ?S
449   "*Mark used for articles that have been saved to."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-ancient-mark ?O
454   "*Mark used for ancient articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-sparse-mark ?Q
459   "*Mark used for sparsely reffed articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-canceled-mark ?G
464   "*Mark used for canceled articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-duplicate-mark ?M
469   "*Mark used for duplicate articles."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-undownloaded-mark ?@
474   "*Mark used for articles that weren't downloaded."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-downloadable-mark ?%
479   "*Mark used for articles that are to be downloaded."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-unsendable-mark ?=
484   "*Mark used for articles that won't be sent."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-score-over-mark ?+
489   "*Score mark used for articles with high scores."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-score-below-mark ?-
494   "*Score mark used for articles with low scores."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-empty-thread-mark ?  ;Whitespace
499   "*There is no thread under the article."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-not-empty-thread-mark ?=
504   "*There is a thread under the article."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-view-pseudo-asynchronously nil
509   "*If non-nil, Gnus will view pseudo-articles asynchronously."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-auto-expirable-marks
514   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
515         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
516         gnus-souped-mark gnus-duplicate-mark)
517   "*The list of marks converted into expiration if a group is auto-expirable."
518   :group 'gnus-summary
519   :type '(repeat character))
520
521 (defcustom gnus-inhibit-user-auto-expire t
522   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
523   :group 'gnus-summary
524   :type 'boolean)
525
526 (defcustom gnus-view-pseudos nil
527   "*If `automatic', pseudo-articles will be viewed automatically.
528 If `not-confirm', pseudos will be viewed automatically, and the user
529 will not be asked to confirm the command."
530   :group 'gnus-extract-view
531   :type '(choice (const :tag "off" nil)
532                  (const automatic)
533                  (const not-confirm)))
534
535 (defcustom gnus-view-pseudos-separately t
536   "*If non-nil, one pseudo-article will be created for each file to be viewed.
537 If nil, all files that use the same viewing command will be given as a
538 list of parameters to that command."
539   :group 'gnus-extract-view
540   :type 'boolean)
541
542 (defcustom gnus-insert-pseudo-articles t
543   "*If non-nil, insert pseudo-articles when decoding articles."
544   :group 'gnus-extract-view
545   :type 'boolean)
546
547 (defcustom gnus-summary-dummy-line-format
548   "  %(:                          :%) %S\n"
549   "*The format specification for the dummy roots in the summary buffer.
550 It works along the same lines as a normal formatting string,
551 with some simple extensions.
552
553 %S  The subject"
554   :group 'gnus-threading
555   :type 'string)
556
557 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
558   "*The format specification for the summary mode line.
559 It works along the same lines as a normal formatting string,
560 with some simple extensions:
561
562 %G  Group name
563 %p  Unprefixed group name
564 %A  Current article number
565 %z  Current article score
566 %V  Gnus version
567 %U  Number of unread articles in the group
568 %e  Number of unselected articles in the group
569 %Z  A string with unread/unselected article counts
570 %g  Shortish group name
571 %S  Subject of the current article
572 %u  User-defined spec
573 %s  Current score file name
574 %d  Number of dormant articles
575 %r  Number of articles that have been marked as read in this session
576 %E  Number of articles expunged by the score files"
577   :group 'gnus-summary-format
578   :type 'string)
579
580 (defcustom gnus-list-identifiers nil
581   "Regexp that matches list identifiers to be removed from subject.
582 This can also be a list of regexps."
583   :group 'gnus-summary-format
584   :group 'gnus-article-hiding
585   :type '(choice (const :tag "none" nil)
586                  (regexp :value ".*")
587                  (repeat :value (".*") regexp)))
588
589 (defcustom gnus-summary-mark-below 0
590   "*Mark all articles with a score below this variable as read.
591 This variable is local to each summary buffer and usually set by the
592 score file."
593   :group 'gnus-score-default
594   :type 'integer)
595
596 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
597   "*List of functions used for sorting articles in the summary buffer.
598 This variable is only used when not using a threaded display."
599   :group 'gnus-summary-sort
600   :type '(repeat (choice (function-item gnus-article-sort-by-number)
601                          (function-item gnus-article-sort-by-author)
602                          (function-item gnus-article-sort-by-subject)
603                          (function-item gnus-article-sort-by-date)
604                          (function-item gnus-article-sort-by-score)
605                          (function :tag "other"))))
606
607 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
608   "*List of functions used for sorting threads in the summary buffer.
609 By default, threads are sorted by article number.
610
611 Each function takes two threads and return non-nil if the first thread
612 should be sorted before the other.  If you use more than one function,
613 the primary sort function should be the last.  You should probably
614 always include `gnus-thread-sort-by-number' in the list of sorting
615 functions -- preferably first.
616
617 Ready-made functions include `gnus-thread-sort-by-number',
618 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
619 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
620 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
621   :group 'gnus-summary-sort
622   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
623                          (function-item gnus-thread-sort-by-author)
624                          (function-item gnus-thread-sort-by-subject)
625                          (function-item gnus-thread-sort-by-date)
626                          (function-item gnus-thread-sort-by-score)
627                          (function-item gnus-thread-sort-by-total-score)
628                          (function :tag "other"))))
629
630 (defcustom gnus-thread-score-function '+
631   "*Function used for calculating the total score of a thread.
632
633 The function is called with the scores of the article and each
634 subthread and should then return the score of the thread.
635
636 Some functions you can use are `+', `max', or `min'."
637   :group 'gnus-summary-sort
638   :type 'function)
639
640 (defcustom gnus-summary-expunge-below nil
641   "All articles that have a score less than this variable will be expunged.
642 This variable is local to the summary buffers."
643   :group 'gnus-score-default
644   :type '(choice (const :tag "off" nil)
645                  integer))
646
647 (defcustom gnus-thread-expunge-below nil
648   "All threads that have a total score less than this variable will be expunged.
649 See `gnus-thread-score-function' for en explanation of what a
650 \"thread score\" is.
651
652 This variable is local to the summary buffers."
653   :group 'gnus-threading
654   :group 'gnus-score-default
655   :type '(choice (const :tag "off" nil)
656                  integer))
657
658 (defcustom gnus-summary-mode-hook nil
659   "*A hook for Gnus summary mode.
660 This hook is run before any variables are set in the summary buffer."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-summary-menu-hook nil
665   "*Hook run after the creation of the summary mode menu."
666   :group 'gnus-summary-visual
667   :type 'hook)
668
669 (defcustom gnus-summary-exit-hook nil
670   "*A hook called on exit from the summary buffer.
671 It will be called with point in the group buffer."
672   :group 'gnus-summary-exit
673   :type 'hook)
674
675 (defcustom gnus-summary-prepare-hook nil
676   "*A hook called after the summary buffer has been generated.
677 If you want to modify the summary buffer, you can use this hook."
678   :group 'gnus-summary-various
679   :type 'hook)
680
681 (defcustom gnus-summary-prepared-hook nil
682   "*A hook called as the last thing after the summary buffer has been generated."
683   :group 'gnus-summary-various
684   :type 'hook)
685
686 (defcustom gnus-summary-generate-hook nil
687   "*A hook run just before generating the summary buffer.
688 This hook is commonly used to customize threading variables and the
689 like."
690   :group 'gnus-summary-various
691   :type 'hook)
692
693 (defcustom gnus-select-group-hook nil
694   "*A hook called when a newsgroup is selected.
695
696 If you'd like to simplify subjects like the
697 `gnus-summary-next-same-subject' command does, you can use the
698 following hook:
699
700  (setq gnus-select-group-hook
701       (list
702         (lambda ()
703           (mapcar (lambda (header)
704                      (mail-header-set-subject
705                       header
706                       (gnus-simplify-subject
707                        (mail-header-subject header) 're-only)))
708                   gnus-newsgroup-headers))))"
709   :group 'gnus-group-select
710   :type 'hook)
711
712 (defcustom gnus-select-article-hook nil
713   "*A hook called when an article is selected."
714   :group 'gnus-summary-choose
715   :type 'hook)
716
717 (defcustom gnus-visual-mark-article-hook
718   (list 'gnus-highlight-selected-summary)
719   "*Hook run after selecting an article in the summary buffer.
720 It is meant to be used for highlighting the article in some way.  It
721 is not run if `gnus-visual' is nil."
722   :group 'gnus-summary-visual
723   :type 'hook)
724
725 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
726   "*A hook called before parsing the headers."
727   :group 'gnus-various
728   :type 'hook)
729
730 (defcustom gnus-exit-group-hook nil
731   "*A hook called when exiting summary mode.
732 This hook is not called from the non-updating exit commands like `Q'."
733   :group 'gnus-various
734   :type 'hook)
735
736 (defcustom gnus-summary-update-hook
737   (list 'gnus-summary-highlight-line)
738   "*A hook called when a summary line is changed.
739 The hook will not be called if `gnus-visual' is nil.
740
741 The default function `gnus-summary-highlight-line' will
742 highlight the line according to the `gnus-summary-highlight'
743 variable."
744   :group 'gnus-summary-visual
745   :type 'hook)
746
747 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
748   "*A hook called when an article is selected for the first time.
749 The hook is intended to mark an article as read (or unread)
750 automatically when it is selected."
751   :group 'gnus-summary-choose
752   :type 'hook)
753
754 (defcustom gnus-group-no-more-groups-hook nil
755   "*A hook run when returning to group mode having no more (unread) groups."
756   :group 'gnus-group-select
757   :type 'hook)
758
759 (defcustom gnus-ps-print-hook nil
760   "*A hook run before ps-printing something from Gnus."
761   :group 'gnus-summary
762   :type 'hook)
763
764 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
765   "Face used for highlighting the current article in the summary buffer."
766   :group 'gnus-summary-visual
767   :type 'face)
768
769 (defcustom gnus-summary-highlight
770   '(((= mark gnus-canceled-mark)
771      . gnus-summary-cancelled-face)
772     ((and (> score default)
773           (or (= mark gnus-dormant-mark)
774               (= mark gnus-ticked-mark)))
775      . gnus-summary-high-ticked-face)
776     ((and (< score default)
777           (or (= mark gnus-dormant-mark)
778               (= mark gnus-ticked-mark)))
779      . gnus-summary-low-ticked-face)
780     ((or (= mark gnus-dormant-mark)
781          (= mark gnus-ticked-mark))
782      . gnus-summary-normal-ticked-face)
783     ((and (> score default) (= mark gnus-ancient-mark))
784      . gnus-summary-high-ancient-face)
785     ((and (< score default) (= mark gnus-ancient-mark))
786      . gnus-summary-low-ancient-face)
787     ((= mark gnus-ancient-mark)
788      . gnus-summary-normal-ancient-face)
789     ((and (> score default) (= mark gnus-unread-mark))
790      . gnus-summary-high-unread-face)
791     ((and (< score default) (= mark gnus-unread-mark))
792      . gnus-summary-low-unread-face)
793     ((and (memq article gnus-newsgroup-incorporated)
794           (= mark gnus-unread-mark))
795      . gnus-summary-incorporated-face)
796     ((= mark gnus-unread-mark)
797      . gnus-summary-normal-unread-face)
798     ((and (> score default) (memq mark (list gnus-downloadable-mark
799                                              gnus-undownloaded-mark)))
800      . gnus-summary-high-unread-face)
801     ((and (< score default) (memq mark (list gnus-downloadable-mark
802                                              gnus-undownloaded-mark)))
803      . gnus-summary-low-unread-face)
804     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
805      . gnus-summary-normal-unread-face)
806     ((> score default)
807      . gnus-summary-high-read-face)
808     ((< score default)
809      . gnus-summary-low-read-face)
810     (t
811      . gnus-summary-normal-read-face))
812   "*Controls the highlighting of summary buffer lines.
813
814 A list of (FORM . FACE) pairs.  When deciding how a a particular
815 summary line should be displayed, each form is evaluated.  The content
816 of the face field after the first true form is used.  You can change
817 how those summary lines are displayed, by editing the face field.
818
819 You can use the following variables in the FORM field.
820
821 score:   The articles score
822 default: The default article score.
823 below:   The score below which articles are automatically marked as read.
824 mark:    The articles mark."
825   :group 'gnus-summary-visual
826   :type '(repeat (cons (sexp :tag "Form" nil)
827                        face)))
828
829 (defcustom gnus-alter-header-function nil
830   "Function called to allow alteration of article header structures.
831 The function is called with one parameter, the article header vector,
832 which it may alter in any way.")
833
834 (defvar gnus-decode-encoded-word-function
835   (mime-find-field-decoder 'From 'nov)
836   "Variable that says which function should be used to decode a string with encoded words.")
837
838 (defcustom gnus-extra-headers nil
839   "*Extra headers to parse."
840   :group 'gnus-summary
841   :type '(repeat symbol))
842
843 (defcustom gnus-ignored-from-addresses
844   (and user-mail-address (regexp-quote user-mail-address))
845   "*Regexp of From headers that may be suppressed in favor of To headers."
846   :group 'gnus-summary
847   :type 'regexp)
848
849 (defcustom gnus-group-charset-alist
850   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
851     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
852     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
853     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
854     ("^relcom\\>" koi8-r)
855     ("^fido7\\>" koi8-r)
856     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
857     ("^israel\\>" iso-8859-1)
858     ("^han\\>" euc-kr)
859     ("^alt.chinese.text.big5\\>" chinese-big5)
860     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
861     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
862     (".*" iso-8859-1))
863   "Alist of regexps (to match group names) and default charsets to be used when reading."
864   :type '(repeat (list (regexp :tag "Group")
865                        (symbol :tag "Charset")))
866   :group 'gnus-charset)
867
868 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
869   "List of charsets that should be ignored.
870 When these charsets are used in the \"charset\" parameter, the
871 default charset will be used instead."
872   :type '(repeat symbol)
873   :group 'gnus-charset)
874
875 (defcustom gnus-group-ignored-charsets-alist
876   '(("alt\\.chinese\\.text" iso-8859-1))
877   "Alist of regexps (to match group names) and charsets that should be ignored.
878 When these charsets are used in the \"charset\" parameter, the
879 default charset will be used instead."
880   :type '(repeat (cons (regexp :tag "Group")
881                        (repeat symbol)))
882   :group 'gnus-charset)
883
884 (defcustom gnus-group-highlight-words-alist nil
885   "Alist of group regexps and highlight regexps.
886 This variable uses the same syntax as `gnus-emphasis-alist'."
887   :type '(repeat (cons (regexp :tag "Group")
888                        (repeat (list (regexp :tag "Highlight regexp")
889                                      (number :tag "Group for entire word" 0)
890                                      (number :tag "Group for displayed part" 0)
891                                      (symbol :tag "Face"
892                                              gnus-emphasis-highlight-words)))))
893   :group 'gnus-summary-visual)
894
895 (defcustom gnus-use-wheel nil
896   "Use Intelli-mouse on summary movement"
897   :type 'boolean
898   :group 'gnus-summary-maneuvering)
899
900 (defcustom gnus-wheel-scroll-amount '(5 . 1)
901   "Amount to scroll messages by spinning the mouse wheel.
902 This is actually a cons cell, where the first item is the amount to scroll
903 on a normal wheel event, and the second is the amount to scroll when the
904 wheel is moved with the shift key depressed."
905   :type '(cons (integer :tag "Shift") integer)
906   :group 'gnus-summary-maneuvering)
907
908 (defcustom gnus-wheel-edge-resistance 2
909   "How hard it should be to change the current article
910 by moving the mouse over the edge of the article window."
911   :type 'integer
912   :group 'gnus-summary-maneuvering)
913
914 (defcustom gnus-summary-show-article-charset-alist
915   nil
916   "Alist of number and charset.
917 The article will be shown with the charset corresponding to the
918 numbered argument.
919 For example: ((1 . cn-gb-2312) (2 . big5))."
920   :type '(repeat (cons (number :tag "Argument" 1)
921                        (symbol :tag "Charset")))
922   :group 'gnus-charset)
923
924 (defcustom gnus-preserve-marks t
925   "Whether marks are preserved when moving, copying and respooling messages."
926   :type 'boolean
927   :group 'gnus-summary-marks)
928
929 (defcustom gnus-alter-articles-to-read-function nil
930   "Function to be called to alter the list of articles to be selected."
931   :type 'function
932   :group 'gnus-summary)
933
934 (defcustom gnus-orphan-score nil
935   "*All orphans get this score added.  Set in the score file."
936   :group 'gnus-score-default
937   :type '(choice (const nil)
938                  integer))
939
940 (defcustom gnus-summary-save-parts-default-mime "image/.*"
941   "*A regexp to match MIME parts when saving multiple parts of a message
942 with gnus-summary-save-parts (X m). This regexp will be used by default
943 when prompting the user for which type of files to save."
944   :group 'gnus-summary
945   :type 'regexp)
946
947
948 ;;; Internal variables
949
950 (defvar gnus-article-mime-handles nil)
951 (defvar gnus-article-decoded-p nil)
952 (defvar gnus-scores-exclude-files nil)
953 (defvar gnus-page-broken nil)
954 (defvar gnus-inhibit-mime-unbuttonizing nil)
955
956 (defvar gnus-original-article nil)
957 (defvar gnus-article-internal-prepare-hook nil)
958 (defvar gnus-newsgroup-process-stack nil)
959
960 (defvar gnus-thread-indent-array nil)
961 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
962 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
963   "Function called to sort the articles within a thread after it has been gathered together.")
964
965 (defvar gnus-summary-save-parts-type-history nil)
966 (defvar gnus-summary-save-parts-last-directory nil)
967
968 ;; Avoid highlighting in kill files.
969 (defvar gnus-summary-inhibit-highlight nil)
970 (defvar gnus-newsgroup-selected-overlay nil)
971 (defvar gnus-inhibit-limiting nil)
972 (defvar gnus-newsgroup-adaptive-score-file nil)
973 (defvar gnus-current-score-file nil)
974 (defvar gnus-current-move-group nil)
975 (defvar gnus-current-copy-group nil)
976 (defvar gnus-current-crosspost-group nil)
977
978 (defvar gnus-newsgroup-dependencies nil)
979 (defvar gnus-newsgroup-adaptive nil)
980 (defvar gnus-summary-display-article-function nil)
981 (defvar gnus-summary-highlight-line-function nil
982   "Function called after highlighting a summary line.")
983
984 (defvar gnus-summary-line-format-alist
985   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
986     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
987     (?s gnus-tmp-subject-or-nil ?s)
988     (?n gnus-tmp-name ?s)
989     (?A (std11-address-string
990          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
991     (?a (or (std11-full-name-string
992              (car (mime-entity-read-field gnus-tmp-header 'From)))
993             gnus-tmp-from) ?s)
994     (?F gnus-tmp-from ?s)
995     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
996     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
997     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
998     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
999     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1000     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1001     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1002     (?L gnus-tmp-lines ?d)
1003     (?I gnus-tmp-indentation ?s)
1004     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1005     (?R gnus-tmp-replied ?c)
1006     (?\[ gnus-tmp-opening-bracket ?c)
1007     (?\] gnus-tmp-closing-bracket ?c)
1008     (?\> (make-string gnus-tmp-level ? ) ?s)
1009     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1010     (?i gnus-tmp-score ?d)
1011     (?z gnus-tmp-score-char ?c)
1012     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1013     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1014     (?U gnus-tmp-unread ?c)
1015     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1016     (?t (gnus-summary-number-of-articles-in-thread
1017          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1018         ?d)
1019     (?e (gnus-summary-number-of-articles-in-thread
1020          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1021         ?c)
1022     (?u gnus-tmp-user-defined ?s)
1023     (?P (gnus-pick-line-number) ?d))
1024   "An alist of format specifications that can appear in summary lines.
1025 These are paired with what variables they correspond with, along with
1026 the type of the variable (string, integer, character, etc).")
1027
1028 (defvar gnus-summary-dummy-line-format-alist
1029   `((?S gnus-tmp-subject ?s)
1030     (?N gnus-tmp-number ?d)
1031     (?u gnus-tmp-user-defined ?s)))
1032
1033 (defvar gnus-summary-mode-line-format-alist
1034   `((?G gnus-tmp-group-name ?s)
1035     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1036     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1037     (?A gnus-tmp-article-number ?d)
1038     (?Z gnus-tmp-unread-and-unselected ?s)
1039     (?V gnus-version ?s)
1040     (?U gnus-tmp-unread-and-unticked ?d)
1041     (?S gnus-tmp-subject ?s)
1042     (?e gnus-tmp-unselected ?d)
1043     (?u gnus-tmp-user-defined ?s)
1044     (?d (length gnus-newsgroup-dormant) ?d)
1045     (?t (length gnus-newsgroup-marked) ?d)
1046     (?r (length gnus-newsgroup-reads) ?d)
1047     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1048     (?E gnus-newsgroup-expunged-tally ?d)
1049     (?s (gnus-current-score-file-nondirectory) ?s)))
1050
1051 (defvar gnus-last-search-regexp nil
1052   "Default regexp for article search command.")
1053
1054 (defvar gnus-summary-search-article-matched-data nil
1055   "Last matched data of article search command.  It is the local variable
1056 in `gnus-article-buffer' which consists of the list of start position,
1057 end position and text.")
1058
1059 (defvar gnus-last-shell-command nil
1060   "Default shell command on article.")
1061
1062 (defvar gnus-newsgroup-begin nil)
1063 (defvar gnus-newsgroup-end nil)
1064 (defvar gnus-newsgroup-last-rmail nil)
1065 (defvar gnus-newsgroup-last-mail nil)
1066 (defvar gnus-newsgroup-last-folder nil)
1067 (defvar gnus-newsgroup-last-file nil)
1068 (defvar gnus-newsgroup-auto-expire nil)
1069 (defvar gnus-newsgroup-active nil)
1070
1071 (defvar gnus-newsgroup-data nil)
1072 (defvar gnus-newsgroup-data-reverse nil)
1073 (defvar gnus-newsgroup-limit nil)
1074 (defvar gnus-newsgroup-limits nil)
1075
1076 (defvar gnus-newsgroup-unreads nil
1077   "List of unread articles in the current newsgroup.")
1078
1079 (defvar gnus-newsgroup-unselected nil
1080   "List of unselected unread articles in the current newsgroup.")
1081
1082 (defvar gnus-newsgroup-reads nil
1083   "Alist of read articles and article marks in the current newsgroup.")
1084
1085 (defvar gnus-newsgroup-expunged-tally nil)
1086
1087 (defvar gnus-newsgroup-marked nil
1088   "List of ticked articles in the current newsgroup (a subset of unread art).")
1089
1090 (defvar gnus-newsgroup-killed nil
1091   "List of ranges of articles that have been through the scoring process.")
1092
1093 (defvar gnus-newsgroup-cached nil
1094   "List of articles that come from the article cache.")
1095
1096 (defvar gnus-newsgroup-saved nil
1097   "List of articles that have been saved.")
1098
1099 (defvar gnus-newsgroup-kill-headers nil)
1100
1101 (defvar gnus-newsgroup-replied nil
1102   "List of articles that have been replied to in the current newsgroup.")
1103
1104 (defvar gnus-newsgroup-expirable nil
1105   "List of articles in the current newsgroup that can be expired.")
1106
1107 (defvar gnus-newsgroup-processable nil
1108   "List of articles in the current newsgroup that can be processed.")
1109
1110 (defvar gnus-newsgroup-downloadable nil
1111   "List of articles in the current newsgroup that can be processed.")
1112
1113 (defvar gnus-newsgroup-undownloaded nil
1114   "List of articles in the current newsgroup that haven't been downloaded..")
1115
1116 (defvar gnus-newsgroup-unsendable nil
1117   "List of articles in the current newsgroup that won't be sent.")
1118
1119 (defvar gnus-newsgroup-bookmarks nil
1120   "List of articles in the current newsgroup that have bookmarks.")
1121
1122 (defvar gnus-newsgroup-dormant nil
1123   "List of dormant articles in the current newsgroup.")
1124
1125 (defvar gnus-newsgroup-scored nil
1126   "List of scored articles in the current newsgroup.")
1127
1128 (defvar gnus-newsgroup-incorporated nil
1129   "List of incorporated articles in the current newsgroup.")
1130
1131 (defvar gnus-newsgroup-headers nil
1132   "List of article headers in the current newsgroup.")
1133
1134 (defvar gnus-newsgroup-threads nil)
1135
1136 (defvar gnus-newsgroup-prepared nil
1137   "Whether the current group has been prepared properly.")
1138
1139 (defvar gnus-newsgroup-ancient nil
1140   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1141
1142 (defvar gnus-newsgroup-sparse nil)
1143
1144 (defvar gnus-current-article nil)
1145 (defvar gnus-article-current nil)
1146 (defvar gnus-current-headers nil)
1147 (defvar gnus-have-all-headers nil)
1148 (defvar gnus-last-article nil)
1149 (defvar gnus-newsgroup-history nil)
1150 (defvar gnus-newsgroup-charset nil)
1151 (defvar gnus-newsgroup-ephemeral-charset nil)
1152 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1153
1154 (defconst gnus-summary-local-variables
1155   '(gnus-newsgroup-name
1156     gnus-newsgroup-begin gnus-newsgroup-end
1157     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1158     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1159     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1160     gnus-newsgroup-unselected gnus-newsgroup-marked
1161     gnus-newsgroup-reads gnus-newsgroup-saved
1162     gnus-newsgroup-replied gnus-newsgroup-expirable
1163     gnus-newsgroup-processable gnus-newsgroup-killed
1164     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1165     gnus-newsgroup-unsendable
1166     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1167     gnus-newsgroup-headers gnus-newsgroup-threads
1168     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1169     gnus-current-article gnus-current-headers gnus-have-all-headers
1170     gnus-last-article gnus-article-internal-prepare-hook
1171     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1172     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1173     gnus-thread-expunge-below
1174     gnus-score-alist gnus-current-score-file
1175     (gnus-summary-expunge-below . global)
1176     (gnus-summary-mark-below . global)
1177     (gnus-orphan-score . global)
1178     gnus-newsgroup-active gnus-scores-exclude-files
1179     gnus-newsgroup-history gnus-newsgroup-ancient
1180     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1181     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1182     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1183     (gnus-newsgroup-expunged-tally . 0)
1184     gnus-cache-removable-articles gnus-newsgroup-cached
1185     gnus-newsgroup-data gnus-newsgroup-data-reverse
1186     gnus-newsgroup-limit gnus-newsgroup-limits
1187     gnus-newsgroup-charset
1188     gnus-newsgroup-incorporated)
1189   "Variables that are buffer-local to the summary buffers.")
1190
1191 ;; Byte-compiler warning.
1192 (defvar gnus-article-mode-map)
1193
1194 ;; Subject simplification.
1195
1196 (defun gnus-simplify-whitespace (str)
1197   "Remove excessive whitespace from STR."
1198   (let ((mystr str))
1199     ;; Multiple spaces.
1200     (while (string-match "[ \t][ \t]+" mystr)
1201       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1202                           " "
1203                           (substring mystr (match-end 0)))))
1204     ;; Leading spaces.
1205     (when (string-match "^[ \t]+" mystr)
1206       (setq mystr (substring mystr (match-end 0))))
1207     ;; Trailing spaces.
1208     (when (string-match "[ \t]+$" mystr)
1209       (setq mystr (substring mystr 0 (match-beginning 0))))
1210     mystr))
1211
1212 (defsubst gnus-simplify-subject-re (subject)
1213   "Remove \"Re:\" from subject lines."
1214   (if (string-match "^[Rr][Ee]: *" subject)
1215       (substring subject (match-end 0))
1216     subject))
1217
1218 (defun gnus-simplify-subject (subject &optional re-only)
1219   "Remove `Re:' and words in parentheses.
1220 If RE-ONLY is non-nil, strip leading `Re:'s only."
1221   (let ((case-fold-search t))           ;Ignore case.
1222     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1223     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1224       (setq subject (substring subject (match-end 0))))
1225     ;; Remove uninteresting prefixes.
1226     (when (and (not re-only)
1227                gnus-simplify-ignored-prefixes
1228                (string-match gnus-simplify-ignored-prefixes subject))
1229       (setq subject (substring subject (match-end 0))))
1230     ;; Remove words in parentheses from end.
1231     (unless re-only
1232       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1233         (setq subject (substring subject 0 (match-beginning 0)))))
1234     ;; Return subject string.
1235     subject))
1236
1237 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1238 ;; all whitespace.
1239 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1240   (goto-char (point-min))
1241   (while (re-search-forward regexp nil t)
1242     (replace-match (or newtext ""))))
1243
1244 (defun gnus-simplify-buffer-fuzzy ()
1245   "Simplify string in the buffer fuzzily.
1246 The string in the accessible portion of the current buffer is simplified.
1247 It is assumed to be a single-line subject.
1248 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1249 matter is removed.  Additional things can be deleted by setting
1250 `gnus-simplify-subject-fuzzy-regexp'."
1251   (let ((case-fold-search t)
1252         (modified-tick))
1253     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1254
1255     (while (not (eq modified-tick (buffer-modified-tick)))
1256       (setq modified-tick (buffer-modified-tick))
1257       (cond
1258        ((listp gnus-simplify-subject-fuzzy-regexp)
1259         (mapcar 'gnus-simplify-buffer-fuzzy-step
1260                 gnus-simplify-subject-fuzzy-regexp))
1261        (gnus-simplify-subject-fuzzy-regexp
1262         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1263       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1264       (gnus-simplify-buffer-fuzzy-step
1265        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1266       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1267
1268     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1269     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1270     (gnus-simplify-buffer-fuzzy-step " $")
1271     (gnus-simplify-buffer-fuzzy-step "^ +")))
1272
1273 (defun gnus-simplify-subject-fuzzy (subject)
1274   "Simplify a subject string fuzzily.
1275 See `gnus-simplify-buffer-fuzzy' for details."
1276   (save-excursion
1277     (gnus-set-work-buffer)
1278     (let ((case-fold-search t))
1279       ;; Remove uninteresting prefixes.
1280       (when (and gnus-simplify-ignored-prefixes
1281                  (string-match gnus-simplify-ignored-prefixes subject))
1282         (setq subject (substring subject (match-end 0))))
1283       (insert subject)
1284       (inline (gnus-simplify-buffer-fuzzy))
1285       (buffer-string))))
1286
1287 (defsubst gnus-simplify-subject-fully (subject)
1288   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1289   (cond
1290    (gnus-simplify-subject-functions
1291     (gnus-map-function gnus-simplify-subject-functions subject))
1292    ((null gnus-summary-gather-subject-limit)
1293     (gnus-simplify-subject-re subject))
1294    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1295     (gnus-simplify-subject-fuzzy subject))
1296    ((numberp gnus-summary-gather-subject-limit)
1297     (gnus-limit-string (gnus-simplify-subject-re subject)
1298                        gnus-summary-gather-subject-limit))
1299    (t
1300     subject)))
1301
1302 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1303   "Check whether two subjects are equal.
1304 If optional argument simple-first is t, first argument is already
1305 simplified."
1306   (cond
1307    ((null simple-first)
1308     (equal (gnus-simplify-subject-fully s1)
1309            (gnus-simplify-subject-fully s2)))
1310    (t
1311     (equal s1
1312            (gnus-simplify-subject-fully s2)))))
1313
1314 (defun gnus-summary-bubble-group ()
1315   "Increase the score of the current group.
1316 This is a handy function to add to `gnus-summary-exit-hook' to
1317 increase the score of each group you read."
1318   (gnus-group-add-score gnus-newsgroup-name))
1319
1320 \f
1321 ;;;
1322 ;;; Gnus summary mode
1323 ;;;
1324
1325 (put 'gnus-summary-mode 'mode-class 'special)
1326
1327 (when t
1328   ;; Non-orthogonal keys
1329
1330   (gnus-define-keys gnus-summary-mode-map
1331     " " gnus-summary-next-page
1332     "\177" gnus-summary-prev-page
1333     [delete] gnus-summary-prev-page
1334     [backspace] gnus-summary-prev-page
1335     "\r" gnus-summary-scroll-up
1336     "\M-\r" gnus-summary-scroll-down
1337     "n" gnus-summary-next-unread-article
1338     "p" gnus-summary-prev-unread-article
1339     "N" gnus-summary-next-article
1340     "P" gnus-summary-prev-article
1341     "\M-\C-n" gnus-summary-next-same-subject
1342     "\M-\C-p" gnus-summary-prev-same-subject
1343     "\M-n" gnus-summary-next-unread-subject
1344     "\M-p" gnus-summary-prev-unread-subject
1345     "." gnus-summary-first-unread-article
1346     "," gnus-summary-best-unread-article
1347     "\M-s" gnus-summary-search-article-forward
1348     "\M-r" gnus-summary-search-article-backward
1349     "<" gnus-summary-beginning-of-article
1350     ">" gnus-summary-end-of-article
1351     "j" gnus-summary-goto-article
1352     "^" gnus-summary-refer-parent-article
1353     "\M-^" gnus-summary-refer-article
1354     "u" gnus-summary-tick-article-forward
1355     "!" gnus-summary-tick-article-forward
1356     "U" gnus-summary-tick-article-backward
1357     "d" gnus-summary-mark-as-read-forward
1358     "D" gnus-summary-mark-as-read-backward
1359     "E" gnus-summary-mark-as-expirable
1360     "\M-u" gnus-summary-clear-mark-forward
1361     "\M-U" gnus-summary-clear-mark-backward
1362     "k" gnus-summary-kill-same-subject-and-select
1363     "\C-k" gnus-summary-kill-same-subject
1364     "\M-\C-k" gnus-summary-kill-thread
1365     "\M-\C-l" gnus-summary-lower-thread
1366     "e" gnus-summary-edit-article
1367     "#" gnus-summary-mark-as-processable
1368     "\M-#" gnus-summary-unmark-as-processable
1369     "\M-\C-t" gnus-summary-toggle-threads
1370     "\M-\C-s" gnus-summary-show-thread
1371     "\M-\C-h" gnus-summary-hide-thread
1372     "\M-\C-f" gnus-summary-next-thread
1373     "\M-\C-b" gnus-summary-prev-thread
1374     [(meta down)] gnus-summary-next-thread
1375     [(meta up)] gnus-summary-prev-thread
1376     "\M-\C-u" gnus-summary-up-thread
1377     "\M-\C-d" gnus-summary-down-thread
1378     "&" gnus-summary-execute-command
1379     "c" gnus-summary-catchup-and-exit
1380     "\C-w" gnus-summary-mark-region-as-read
1381     "\C-t" gnus-summary-toggle-truncation
1382     "?" gnus-summary-mark-as-dormant
1383     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1384     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1385     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1386     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1387     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1388     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1389     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1390     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1391     "=" gnus-summary-expand-window
1392     "\C-x\C-s" gnus-summary-reselect-current-group
1393     "\M-g" gnus-summary-rescan-group
1394     "w" gnus-summary-stop-page-breaking
1395     "\C-c\C-r" gnus-summary-caesar-message
1396     "\M-t" gnus-summary-toggle-mime
1397     "f" gnus-summary-followup
1398     "F" gnus-summary-followup-with-original
1399     "C" gnus-summary-cancel-article
1400     "r" gnus-summary-reply
1401     "R" gnus-summary-reply-with-original
1402     "\C-c\C-f" gnus-summary-mail-forward
1403     "o" gnus-summary-save-article
1404     "\C-o" gnus-summary-save-article-mail
1405     "|" gnus-summary-pipe-output
1406     "\M-k" gnus-summary-edit-local-kill
1407     "\M-K" gnus-summary-edit-global-kill
1408     ;; "V" gnus-version
1409     "\C-c\C-d" gnus-summary-describe-group
1410     "q" gnus-summary-exit
1411     "Q" gnus-summary-exit-no-update
1412     "\C-c\C-i" gnus-info-find-node
1413     gnus-mouse-2 gnus-mouse-pick-article
1414     "m" gnus-summary-mail-other-window
1415     "a" gnus-summary-post-news
1416     "x" gnus-summary-limit-to-unread
1417     "s" gnus-summary-isearch-article
1418     "t" gnus-article-toggle-headers
1419     "g" gnus-summary-show-article
1420     "l" gnus-summary-goto-last-article
1421     "v" gnus-summary-preview-mime-message
1422     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1423     "\C-d" gnus-summary-enter-digest-group
1424     "\M-\C-d" gnus-summary-read-document
1425     "\M-\C-e" gnus-summary-edit-parameters
1426     "\M-\C-a" gnus-summary-customize-parameters
1427     "\C-c\C-b" gnus-bug
1428     "*" gnus-cache-enter-article
1429     "\M-*" gnus-cache-remove-article
1430     "\M-&" gnus-summary-universal-argument
1431     "\C-l" gnus-recenter
1432     "I" gnus-summary-increase-score
1433     "L" gnus-summary-lower-score
1434     "\M-i" gnus-symbolic-argument
1435     "h" gnus-summary-select-article-buffer
1436
1437     "V" gnus-summary-score-map
1438     "X" gnus-uu-extract-map
1439     "S" gnus-summary-send-map)
1440
1441   ;; Sort of orthogonal keymap
1442   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1443     "t" gnus-summary-tick-article-forward
1444     "!" gnus-summary-tick-article-forward
1445     "d" gnus-summary-mark-as-read-forward
1446     "r" gnus-summary-mark-as-read-forward
1447     "c" gnus-summary-clear-mark-forward
1448     " " gnus-summary-clear-mark-forward
1449     "e" gnus-summary-mark-as-expirable
1450     "x" gnus-summary-mark-as-expirable
1451     "?" gnus-summary-mark-as-dormant
1452     "b" gnus-summary-set-bookmark
1453     "B" gnus-summary-remove-bookmark
1454     "#" gnus-summary-mark-as-processable
1455     "\M-#" gnus-summary-unmark-as-processable
1456     "S" gnus-summary-limit-include-expunged
1457     "C" gnus-summary-catchup
1458     "H" gnus-summary-catchup-to-here
1459     "\C-c" gnus-summary-catchup-all
1460     "k" gnus-summary-kill-same-subject-and-select
1461     "K" gnus-summary-kill-same-subject
1462     "P" gnus-uu-mark-map)
1463
1464   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1465     "c" gnus-summary-clear-above
1466     "u" gnus-summary-tick-above
1467     "m" gnus-summary-mark-above
1468     "k" gnus-summary-kill-below)
1469
1470   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1471     "/" gnus-summary-limit-to-subject
1472     "n" gnus-summary-limit-to-articles
1473     "w" gnus-summary-pop-limit
1474     "s" gnus-summary-limit-to-subject
1475     "a" gnus-summary-limit-to-author
1476     "u" gnus-summary-limit-to-unread
1477     "m" gnus-summary-limit-to-marks
1478     "M" gnus-summary-limit-exclude-marks
1479     "v" gnus-summary-limit-to-score
1480     "*" gnus-summary-limit-include-cached
1481     "D" gnus-summary-limit-include-dormant
1482     "T" gnus-summary-limit-include-thread
1483     "d" gnus-summary-limit-exclude-dormant
1484     "t" gnus-summary-limit-to-age
1485     "x" gnus-summary-limit-to-extra
1486     "E" gnus-summary-limit-include-expunged
1487     "c" gnus-summary-limit-exclude-childless-dormant
1488     "C" gnus-summary-limit-mark-excluded-as-read)
1489
1490   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1491     "n" gnus-summary-next-unread-article
1492     "p" gnus-summary-prev-unread-article
1493     "N" gnus-summary-next-article
1494     "P" gnus-summary-prev-article
1495     "\C-n" gnus-summary-next-same-subject
1496     "\C-p" gnus-summary-prev-same-subject
1497     "\M-n" gnus-summary-next-unread-subject
1498     "\M-p" gnus-summary-prev-unread-subject
1499     "f" gnus-summary-first-unread-article
1500     "b" gnus-summary-best-unread-article
1501     "j" gnus-summary-goto-article
1502     "g" gnus-summary-goto-subject
1503     "l" gnus-summary-goto-last-article
1504     "o" gnus-summary-pop-article)
1505
1506   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1507     "k" gnus-summary-kill-thread
1508     "l" gnus-summary-lower-thread
1509     "i" gnus-summary-raise-thread
1510     "T" gnus-summary-toggle-threads
1511     "t" gnus-summary-rethread-current
1512     "^" gnus-summary-reparent-thread
1513     "s" gnus-summary-show-thread
1514     "S" gnus-summary-show-all-threads
1515     "h" gnus-summary-hide-thread
1516     "H" gnus-summary-hide-all-threads
1517     "n" gnus-summary-next-thread
1518     "p" gnus-summary-prev-thread
1519     "u" gnus-summary-up-thread
1520     "o" gnus-summary-top-thread
1521     "d" gnus-summary-down-thread
1522     "#" gnus-uu-mark-thread
1523     "\M-#" gnus-uu-unmark-thread)
1524
1525   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1526     "g" gnus-summary-prepare
1527     "c" gnus-summary-insert-cached-articles)
1528
1529   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1530     "c" gnus-summary-catchup-and-exit
1531     "C" gnus-summary-catchup-all-and-exit
1532     "E" gnus-summary-exit-no-update
1533     "J" gnus-summary-jump-to-other-group
1534     "Q" gnus-summary-exit
1535     "Z" gnus-summary-exit
1536     "n" gnus-summary-catchup-and-goto-next-group
1537     "R" gnus-summary-reselect-current-group
1538     "G" gnus-summary-rescan-group
1539     "N" gnus-summary-next-group
1540     "s" gnus-summary-save-newsrc
1541     "P" gnus-summary-prev-group)
1542
1543   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1544     " " gnus-summary-next-page
1545     "n" gnus-summary-next-page
1546     "\177" gnus-summary-prev-page
1547     [delete] gnus-summary-prev-page
1548     "p" gnus-summary-prev-page
1549     "\r" gnus-summary-scroll-up
1550     "\M-\r" gnus-summary-scroll-down
1551     "<" gnus-summary-beginning-of-article
1552     ">" gnus-summary-end-of-article
1553     "b" gnus-summary-beginning-of-article
1554     "e" gnus-summary-end-of-article
1555     "^" gnus-summary-refer-parent-article
1556     "r" gnus-summary-refer-parent-article
1557     "D" gnus-summary-enter-digest-group
1558     "R" gnus-summary-refer-references
1559     "T" gnus-summary-refer-thread
1560     "g" gnus-summary-show-article
1561     "s" gnus-summary-isearch-article
1562     "P" gnus-summary-print-article
1563     "t" gnus-article-babel
1564     "d" gnus-summary-decrypt-article
1565     "v" gnus-summary-verify-article)
1566
1567   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1568     "b" gnus-article-add-buttons
1569     "B" gnus-article-add-buttons-to-head
1570     "o" gnus-article-treat-overstrike
1571     "e" gnus-article-emphasize
1572     "w" gnus-article-fill-cited-article
1573     "Q" gnus-article-fill-long-lines
1574     "C" gnus-article-capitalize-sentences
1575     "c" gnus-article-remove-cr
1576     "Z" gnus-article-decode-HZ
1577     "f" gnus-article-display-x-face
1578     "l" gnus-summary-stop-page-breaking
1579     "r" gnus-summary-caesar-message
1580     "t" gnus-article-toggle-headers
1581     "v" gnus-summary-verbose-headers
1582     "m" gnus-summary-toggle-mime
1583     "H" gnus-article-strip-headers-in-body
1584     "d" gnus-article-treat-dumbquotes
1585     "s" gnus-smiley-display)
1586
1587   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1588     "a" gnus-article-hide
1589     "h" gnus-article-toggle-headers
1590     "b" gnus-article-hide-boring-headers
1591     "s" gnus-article-hide-signature
1592     "c" gnus-article-hide-citation
1593     "C" gnus-article-hide-citation-in-followups
1594     "l" gnus-article-hide-list-identifiers
1595     "p" gnus-article-hide-pgp
1596     "B" gnus-article-strip-banner
1597     "P" gnus-article-hide-pem
1598     "\C-c" gnus-article-hide-citation-maybe)
1599
1600   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1601     "a" gnus-article-highlight
1602     "h" gnus-article-highlight-headers
1603     "c" gnus-article-highlight-citation
1604     "s" gnus-article-highlight-signature)
1605
1606   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1607     "z" gnus-article-date-ut
1608     "u" gnus-article-date-ut
1609     "l" gnus-article-date-local
1610     "e" gnus-article-date-lapsed
1611     "o" gnus-article-date-original
1612     "i" gnus-article-date-iso8601
1613     "s" gnus-article-date-user)
1614
1615   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1616     "t" gnus-article-remove-trailing-blank-lines
1617     "l" gnus-article-strip-leading-blank-lines
1618     "m" gnus-article-strip-multiple-blank-lines
1619     "a" gnus-article-strip-blank-lines
1620     "A" gnus-article-strip-all-blank-lines
1621     "s" gnus-article-strip-leading-space
1622     "e" gnus-article-strip-trailing-space)
1623
1624   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1625     "v" gnus-version
1626     "f" gnus-summary-fetch-faq
1627     "d" gnus-summary-describe-group
1628     "h" gnus-summary-describe-briefly
1629     "i" gnus-info-find-node)
1630
1631   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1632     "e" gnus-summary-expire-articles
1633     "\M-\C-e" gnus-summary-expire-articles-now
1634     "\177" gnus-summary-delete-article
1635     [delete] gnus-summary-delete-article
1636     [backspace] gnus-summary-delete-article
1637     "m" gnus-summary-move-article
1638     "r" gnus-summary-respool-article
1639     "w" gnus-summary-edit-article
1640     "c" gnus-summary-copy-article
1641     "B" gnus-summary-crosspost-article
1642     "q" gnus-summary-respool-query
1643     "t" gnus-summary-respool-trace
1644     "i" gnus-summary-import-article
1645     "p" gnus-summary-article-posted-p)
1646
1647   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1648     "o" gnus-summary-save-article
1649     "m" gnus-summary-save-article-mail
1650     "F" gnus-summary-write-article-file
1651     "r" gnus-summary-save-article-rmail
1652     "f" gnus-summary-save-article-file
1653     "b" gnus-summary-save-article-body-file
1654     "h" gnus-summary-save-article-folder
1655     "v" gnus-summary-save-article-vm
1656     "p" gnus-summary-pipe-output
1657     "s" gnus-soup-add-article)
1658
1659   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1660     "b" gnus-summary-display-buttonized
1661     "m" gnus-summary-repair-multipart
1662     "v" gnus-article-view-part
1663     "o" gnus-article-save-part
1664     "c" gnus-article-copy-part
1665     "e" gnus-article-externalize-part
1666     "i" gnus-article-inline-part
1667     "|" gnus-article-pipe-part))
1668
1669 (defun gnus-summary-make-menu-bar ()
1670   (gnus-turn-off-edit-menu 'summary)
1671
1672   (unless (boundp 'gnus-summary-misc-menu)
1673
1674     (easy-menu-define
1675      gnus-summary-kill-menu gnus-summary-mode-map ""
1676      (cons
1677       "Score"
1678       (nconc
1679        (list
1680         ["Enter score..." gnus-summary-score-entry t]
1681         ["Customize" gnus-score-customize t])
1682        (gnus-make-score-map 'increase)
1683        (gnus-make-score-map 'lower)
1684        '(("Mark"
1685           ["Kill below" gnus-summary-kill-below t]
1686           ["Mark above" gnus-summary-mark-above t]
1687           ["Tick above" gnus-summary-tick-above t]
1688           ["Clear above" gnus-summary-clear-above t])
1689          ["Current score" gnus-summary-current-score t]
1690          ["Set score" gnus-summary-set-score t]
1691          ["Switch current score file..." gnus-score-change-score-file t]
1692          ["Set mark below..." gnus-score-set-mark-below t]
1693          ["Set expunge below..." gnus-score-set-expunge-below t]
1694          ["Edit current score file" gnus-score-edit-current-scores t]
1695          ["Edit score file" gnus-score-edit-file t]
1696          ["Trace score" gnus-score-find-trace t]
1697          ["Find words" gnus-score-find-favourite-words t]
1698          ["Rescore buffer" gnus-summary-rescore t]
1699          ["Increase score..." gnus-summary-increase-score t]
1700          ["Lower score..." gnus-summary-lower-score t]))))
1701
1702     ;; Define both the Article menu in the summary buffer and the equivalent
1703     ;; Commands menu in the article buffer here for consistency.
1704     (let ((innards
1705            '(("Hide"
1706               ["All" gnus-article-hide t]
1707               ["Headers" gnus-article-toggle-headers t]
1708               ["Signature" gnus-article-hide-signature t]
1709               ["Citation" gnus-article-hide-citation t]
1710               ["List identifiers" gnus-article-hide-list-identifiers t]
1711               ["PGP" gnus-article-hide-pgp t]
1712               ["Banner" gnus-article-strip-banner t]
1713               ["Boring headers" gnus-article-hide-boring-headers t])
1714              ("Highlight"
1715               ["All" gnus-article-highlight t]
1716               ["Headers" gnus-article-highlight-headers t]
1717               ["Signature" gnus-article-highlight-signature t]
1718               ["Citation" gnus-article-highlight-citation t])
1719              ("Date"
1720               ["Local" gnus-article-date-local t]
1721               ["ISO8601" gnus-article-date-iso8601 t]
1722               ["UT" gnus-article-date-ut t]
1723               ["Original" gnus-article-date-original t]
1724               ["Lapsed" gnus-article-date-lapsed t]
1725               ["User-defined" gnus-article-date-user t])
1726              ("Washing"
1727               ("Remove Blanks"
1728                ["Leading" gnus-article-strip-leading-blank-lines t]
1729                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1730                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1731                ["All of the above" gnus-article-strip-blank-lines t]
1732                ["All" gnus-article-strip-all-blank-lines t]
1733                ["Leading space" gnus-article-strip-leading-space t]
1734                ["Trailing space" gnus-article-strip-trailing-space t])
1735               ["Overstrike" gnus-article-treat-overstrike t]
1736               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1737               ["Emphasis" gnus-article-emphasize t]
1738               ["Word wrap" gnus-article-fill-cited-article t]
1739               ["Fill long lines" gnus-article-fill-long-lines t]
1740               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1741               ["CR" gnus-article-remove-cr t]
1742               ["Show X-Face" gnus-article-display-x-face t]
1743               ["Rot 13" gnus-summary-caesar-message t]
1744               ["Unix pipe" gnus-summary-pipe-message t]
1745               ["Add buttons" gnus-article-add-buttons t]
1746               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1747               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1748               ["Toggle MIME" gnus-summary-toggle-mime t]
1749               ["Verbose header" gnus-summary-verbose-headers t]
1750               ["Toggle header" gnus-summary-toggle-header t]
1751               ["Toggle smileys" gnus-smiley-display t]
1752               ["HZ" gnus-article-decode-HZ t])
1753              ("Output"
1754               ["Save in default format" gnus-summary-save-article t]
1755               ["Save in file" gnus-summary-save-article-file t]
1756               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1757               ["Save in MH folder" gnus-summary-save-article-folder t]
1758               ["Save in VM folder" gnus-summary-save-article-vm t]
1759               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1760               ["Save body in file" gnus-summary-save-article-body-file t]
1761               ["Pipe through a filter" gnus-summary-pipe-output t]
1762               ["Add to SOUP packet" gnus-soup-add-article t]
1763               ["Print" gnus-summary-print-article t])
1764              ("Backend"
1765               ["Respool article..." gnus-summary-respool-article t]
1766               ["Move article..." gnus-summary-move-article
1767                (gnus-check-backend-function
1768                 'request-move-article gnus-newsgroup-name)]
1769               ["Copy article..." gnus-summary-copy-article t]
1770               ["Crosspost article..." gnus-summary-crosspost-article
1771                (gnus-check-backend-function
1772                 'request-replace-article gnus-newsgroup-name)]
1773               ["Import file..." gnus-summary-import-article t]
1774               ["Check if posted" gnus-summary-article-posted-p t]
1775               ["Edit article" gnus-summary-edit-article
1776                (not (gnus-group-read-only-p))]
1777               ["Delete article" gnus-summary-delete-article
1778                (gnus-check-backend-function
1779                 'request-expire-articles gnus-newsgroup-name)]
1780               ["Query respool" gnus-summary-respool-query t]
1781               ["Trace respool" gnus-summary-respool-trace t]
1782               ["Delete expirable articles" gnus-summary-expire-articles-now
1783                (gnus-check-backend-function
1784                 'request-expire-articles gnus-newsgroup-name)])
1785              ("Extract"
1786               ["Uudecode" gnus-uu-decode-uu t]
1787               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1788               ["Unshar" gnus-uu-decode-unshar t]
1789               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1790               ["Save" gnus-uu-decode-save t]
1791               ["Binhex" gnus-uu-decode-binhex t]
1792               ["Postscript" gnus-uu-decode-postscript t])
1793              ("Cache"
1794               ["Enter article" gnus-cache-enter-article t]
1795               ["Remove article" gnus-cache-remove-article t])
1796              ["Translate" gnus-article-babel t]
1797              ["Select article buffer" gnus-summary-select-article-buffer t]
1798              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1799              ["Isearch article..." gnus-summary-isearch-article t]
1800              ["Beginning of the article" gnus-summary-beginning-of-article t]
1801              ["End of the article" gnus-summary-end-of-article t]
1802              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1803              ["Fetch referenced articles" gnus-summary-refer-references t]
1804              ["Fetch current thread" gnus-summary-refer-thread t]
1805              ["Fetch article with id..." gnus-summary-refer-article t]
1806              ["Redisplay" gnus-summary-show-article t])))
1807       (easy-menu-define
1808        gnus-summary-article-menu gnus-summary-mode-map ""
1809        (cons "Article" innards))
1810
1811       (easy-menu-define
1812        gnus-article-commands-menu gnus-article-mode-map ""
1813        (cons "Commands" innards)))
1814
1815     (easy-menu-define
1816      gnus-summary-thread-menu gnus-summary-mode-map ""
1817      '("Threads"
1818        ["Toggle threading" gnus-summary-toggle-threads t]
1819        ["Hide threads" gnus-summary-hide-all-threads t]
1820        ["Show threads" gnus-summary-show-all-threads t]
1821        ["Hide thread" gnus-summary-hide-thread t]
1822        ["Show thread" gnus-summary-show-thread t]
1823        ["Go to next thread" gnus-summary-next-thread t]
1824        ["Go to previous thread" gnus-summary-prev-thread t]
1825        ["Go down thread" gnus-summary-down-thread t]
1826        ["Go up thread" gnus-summary-up-thread t]
1827        ["Top of thread" gnus-summary-top-thread t]
1828        ["Mark thread as read" gnus-summary-kill-thread t]
1829        ["Lower thread score" gnus-summary-lower-thread t]
1830        ["Raise thread score" gnus-summary-raise-thread t]
1831        ["Rethread current" gnus-summary-rethread-current t]))
1832
1833     (easy-menu-define
1834      gnus-summary-post-menu gnus-summary-mode-map ""
1835      '("Post"
1836        ["Post an article" gnus-summary-post-news t]
1837        ["Followup" gnus-summary-followup t]
1838        ["Followup and yank" gnus-summary-followup-with-original t]
1839        ["Supersede article" gnus-summary-supersede-article t]
1840        ["Cancel article" gnus-summary-cancel-article t]
1841        ["Reply" gnus-summary-reply t]
1842        ["Reply and yank" gnus-summary-reply-with-original t]
1843        ["Wide reply" gnus-summary-wide-reply t]
1844        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1845        ["Mail forward" gnus-summary-mail-forward t]
1846        ["Post forward" gnus-summary-post-forward t]
1847        ["Digest and mail" gnus-uu-digest-mail-forward t]
1848        ["Digest and post" gnus-uu-digest-post-forward t]
1849        ["Resend message" gnus-summary-resend-message t]
1850        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1851        ["Send a mail" gnus-summary-mail-other-window t]
1852        ["Uuencode and post" gnus-uu-post-news t]
1853        ["Followup via news" gnus-summary-followup-to-mail t]
1854        ["Followup via news and yank"
1855         gnus-summary-followup-to-mail-with-original t]
1856        ;;("Draft"
1857        ;;["Send" gnus-summary-send-draft t]
1858        ;;["Send bounced" gnus-resend-bounced-mail t])
1859        ))
1860
1861     (easy-menu-define
1862      gnus-summary-misc-menu gnus-summary-mode-map ""
1863      '("Misc"
1864        ("Mark Read"
1865         ["Mark as read" gnus-summary-mark-as-read-forward t]
1866         ["Mark same subject and select"
1867          gnus-summary-kill-same-subject-and-select t]
1868         ["Mark same subject" gnus-summary-kill-same-subject t]
1869         ["Catchup" gnus-summary-catchup t]
1870         ["Catchup all" gnus-summary-catchup-all t]
1871         ["Catchup to here" gnus-summary-catchup-to-here t]
1872         ["Catchup region" gnus-summary-mark-region-as-read t]
1873         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1874        ("Mark Various"
1875         ["Tick" gnus-summary-tick-article-forward t]
1876         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1877         ["Remove marks" gnus-summary-clear-mark-forward t]
1878         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1879         ["Set bookmark" gnus-summary-set-bookmark t]
1880         ["Remove bookmark" gnus-summary-remove-bookmark t])
1881        ("Mark Limit"
1882         ["Marks..." gnus-summary-limit-to-marks t]
1883         ["Subject..." gnus-summary-limit-to-subject t]
1884         ["Author..." gnus-summary-limit-to-author t]
1885         ["Age..." gnus-summary-limit-to-age t]
1886         ["Extra..." gnus-summary-limit-to-extra t]
1887         ["Score" gnus-summary-limit-to-score t]
1888         ["Unread" gnus-summary-limit-to-unread t]
1889         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1890         ["Articles" gnus-summary-limit-to-articles t]
1891         ["Pop limit" gnus-summary-pop-limit t]
1892         ["Show dormant" gnus-summary-limit-include-dormant t]
1893         ["Hide childless dormant"
1894          gnus-summary-limit-exclude-childless-dormant t]
1895         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1896         ["Hide marked" gnus-summary-limit-exclude-marks t]
1897         ["Show expunged" gnus-summary-show-all-expunged t])
1898        ("Process Mark"
1899         ["Set mark" gnus-summary-mark-as-processable t]
1900         ["Remove mark" gnus-summary-unmark-as-processable t]
1901         ["Remove all marks" gnus-summary-unmark-all-processable t]
1902         ["Mark above" gnus-uu-mark-over t]
1903         ["Mark series" gnus-uu-mark-series t]
1904         ["Mark region" gnus-uu-mark-region t]
1905         ["Unmark region" gnus-uu-unmark-region t]
1906         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1907         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1908         ["Mark all" gnus-uu-mark-all t]
1909         ["Mark buffer" gnus-uu-mark-buffer t]
1910         ["Mark sparse" gnus-uu-mark-sparse t]
1911         ["Mark thread" gnus-uu-mark-thread t]
1912         ["Unmark thread" gnus-uu-unmark-thread t]
1913         ("Process Mark Sets"
1914          ["Kill" gnus-summary-kill-process-mark t]
1915          ["Yank" gnus-summary-yank-process-mark
1916           gnus-newsgroup-process-stack]
1917          ["Save" gnus-summary-save-process-mark t]))
1918        ("Scroll article"
1919         ["Page forward" gnus-summary-next-page t]
1920         ["Page backward" gnus-summary-prev-page t]
1921         ["Line forward" gnus-summary-scroll-up t])
1922        ("Move"
1923         ["Next unread article" gnus-summary-next-unread-article t]
1924         ["Previous unread article" gnus-summary-prev-unread-article t]
1925         ["Next article" gnus-summary-next-article t]
1926         ["Previous article" gnus-summary-prev-article t]
1927         ["Next unread subject" gnus-summary-next-unread-subject t]
1928         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1929         ["Next article same subject" gnus-summary-next-same-subject t]
1930         ["Previous article same subject" gnus-summary-prev-same-subject t]
1931         ["First unread article" gnus-summary-first-unread-article t]
1932         ["Best unread article" gnus-summary-best-unread-article t]
1933         ["Go to subject number..." gnus-summary-goto-subject t]
1934         ["Go to article number..." gnus-summary-goto-article t]
1935         ["Go to the last article" gnus-summary-goto-last-article t]
1936         ["Pop article off history" gnus-summary-pop-article t])
1937        ("Sort"
1938         ["Sort by number" gnus-summary-sort-by-number t]
1939         ["Sort by author" gnus-summary-sort-by-author t]
1940         ["Sort by subject" gnus-summary-sort-by-subject t]
1941         ["Sort by date" gnus-summary-sort-by-date t]
1942         ["Sort by score" gnus-summary-sort-by-score t]
1943         ["Sort by lines" gnus-summary-sort-by-lines t]
1944         ["Sort by characters" gnus-summary-sort-by-chars t])
1945        ("Help"
1946         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1947         ["Describe group" gnus-summary-describe-group t]
1948         ["Read manual" gnus-info-find-node t])
1949        ("Modes"
1950         ["Pick and read" gnus-pick-mode t]
1951         ["Binary" gnus-binary-mode t])
1952        ("Regeneration"
1953         ["Regenerate" gnus-summary-prepare t]
1954         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1955         ["Toggle threading" gnus-summary-toggle-threads t])
1956        ["Filter articles..." gnus-summary-execute-command t]
1957        ["Run command on subjects..." gnus-summary-universal-argument t]
1958        ["Search articles forward..." gnus-summary-search-article-forward t]
1959        ["Search articles backward..." gnus-summary-search-article-backward t]
1960        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1961        ["Expand window" gnus-summary-expand-window t]
1962        ["Expire expirable articles" gnus-summary-expire-articles
1963         (gnus-check-backend-function
1964          'request-expire-articles gnus-newsgroup-name)]
1965        ["Edit local kill file" gnus-summary-edit-local-kill t]
1966        ["Edit main kill file" gnus-summary-edit-global-kill t]
1967        ["Edit group parameters" gnus-summary-edit-parameters t]
1968        ["Customize group parameters" gnus-summary-customize-parameters t]
1969        ["Send a bug report" gnus-bug t]
1970        ("Exit"
1971         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1972         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1973         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1974         ["Exit group" gnus-summary-exit t]
1975         ["Exit group without updating" gnus-summary-exit-no-update t]
1976         ["Exit and goto next group" gnus-summary-next-group t]
1977         ["Exit and goto prev group" gnus-summary-prev-group t]
1978         ["Reselect group" gnus-summary-reselect-current-group t]
1979         ["Rescan group" gnus-summary-rescan-group t]
1980         ["Update dribble" gnus-summary-save-newsrc t])))
1981
1982     (gnus-run-hooks 'gnus-summary-menu-hook)))
1983
1984 (defun gnus-score-set-default (var value)
1985   "A version of set that updates the GNU Emacs menu-bar."
1986   (set var value)
1987   ;; It is the message that forces the active status to be updated.
1988   (message ""))
1989
1990 (defun gnus-make-score-map (type)
1991   "Make a summary score map of type TYPE."
1992   (if t
1993       nil
1994     (let ((headers '(("author" "from" string)
1995                      ("subject" "subject" string)
1996                      ("article body" "body" string)
1997                      ("article head" "head" string)
1998                      ("xref" "xref" string)
1999                      ("extra header" "extra" string)
2000                      ("lines" "lines" number)
2001                      ("followups to author" "followup" string)))
2002           (types '((number ("less than" <)
2003                            ("greater than" >)
2004                            ("equal" =))
2005                    (string ("substring" s)
2006                            ("exact string" e)
2007                            ("fuzzy string" f)
2008                            ("regexp" r))))
2009           (perms '(("temporary" (current-time-string))
2010                    ("permanent" nil)
2011                    ("immediate" now)))
2012           header)
2013       (list
2014        (apply
2015         'nconc
2016         (list
2017          (if (eq type 'lower)
2018              "Lower score"
2019            "Increase score"))
2020         (let (outh)
2021           (while headers
2022             (setq header (car headers))
2023             (setq outh
2024                   (cons
2025                    (apply
2026                     'nconc
2027                     (list (car header))
2028                     (let ((ts (cdr (assoc (nth 2 header) types)))
2029                           outt)
2030                       (while ts
2031                         (setq outt
2032                               (cons
2033                                (apply
2034                                 'nconc
2035                                 (list (caar ts))
2036                                 (let ((ps perms)
2037                                       outp)
2038                                   (while ps
2039                                     (setq outp
2040                                           (cons
2041                                            (vector
2042                                             (caar ps)
2043                                             (list
2044                                              'gnus-summary-score-entry
2045                                              (nth 1 header)
2046                                              (if (or (string= (nth 1 header)
2047                                                               "head")
2048                                                      (string= (nth 1 header)
2049                                                               "body"))
2050                                                  ""
2051                                                (list 'gnus-summary-header
2052                                                      (nth 1 header)))
2053                                              (list 'quote (nth 1 (car ts)))
2054                                              (list 'gnus-score-delta-default
2055                                                    nil)
2056                                              (nth 1 (car ps))
2057                                              t)
2058                                             t)
2059                                            outp))
2060                                     (setq ps (cdr ps)))
2061                                   (list (nreverse outp))))
2062                                outt))
2063                         (setq ts (cdr ts)))
2064                       (list (nreverse outt))))
2065                    outh))
2066             (setq headers (cdr headers)))
2067           (list (nreverse outh))))))))
2068
2069 \f
2070
2071 (defun gnus-summary-mode (&optional group)
2072   "Major mode for reading articles.
2073
2074 All normal editing commands are switched off.
2075 \\<gnus-summary-mode-map>
2076 Each line in this buffer represents one article.  To read an
2077 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2078 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2079 respectively.
2080
2081 You can also post articles and send mail from this buffer.  To
2082 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2083 of an article, type `\\[gnus-summary-reply]'.
2084
2085 There are approx. one gazillion commands you can execute in this
2086 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2087
2088 The following commands are available:
2089
2090 \\{gnus-summary-mode-map}"
2091   (interactive)
2092   (when (gnus-visual-p 'summary-menu 'menu)
2093     (gnus-summary-make-menu-bar))
2094   (kill-all-local-variables)
2095   (gnus-summary-make-local-variables)
2096   (gnus-make-thread-indent-array)
2097   (gnus-simplify-mode-line)
2098   (setq major-mode 'gnus-summary-mode)
2099   (setq mode-name "Summary")
2100   (make-local-variable 'minor-mode-alist)
2101   (use-local-map gnus-summary-mode-map)
2102   (buffer-disable-undo)
2103   (setq buffer-read-only t)             ;Disable modification
2104   (setq truncate-lines t)
2105   (setq selective-display t)
2106   (setq selective-display-ellipses t)   ;Display `...'
2107   (gnus-summary-set-display-table)
2108   (gnus-set-default-directory)
2109   (setq gnus-newsgroup-name group)
2110   (unless (gnus-news-group-p group)
2111     (setq gnus-newsgroup-incorporated
2112           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2113   (make-local-variable 'gnus-summary-line-format)
2114   (make-local-variable 'gnus-summary-line-format-spec)
2115   (make-local-variable 'gnus-summary-dummy-line-format)
2116   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2117   (make-local-variable 'gnus-summary-mark-positions)
2118   (make-local-hook 'pre-command-hook)
2119   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2120   (gnus-run-hooks 'gnus-summary-mode-hook)
2121   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2122   (gnus-update-summary-mark-positions))
2123
2124 (defun gnus-summary-make-local-variables ()
2125   "Make all the local summary buffer variables."
2126   (let (global)
2127     (dolist (local gnus-summary-local-variables)
2128       (if (consp local)
2129           (progn
2130             (if (eq (cdr local) 'global)
2131                 ;; Copy the global value of the variable.
2132                 (setq global (symbol-value (car local)))
2133               ;; Use the value from the list.
2134               (setq global (eval (cdr local))))
2135             (set (make-local-variable (car local)) global))
2136         ;; Simple nil-valued local variable.
2137         (set (make-local-variable local) nil)))))
2138
2139 (defun gnus-summary-clear-local-variables ()
2140   (let ((locals gnus-summary-local-variables))
2141     (while locals
2142       (if (consp (car locals))
2143           (and (vectorp (caar locals))
2144                (set (caar locals) nil))
2145         (and (vectorp (car locals))
2146              (set (car locals) nil)))
2147       (setq locals (cdr locals)))))
2148
2149 ;; Summary data functions.
2150
2151 (defmacro gnus-data-number (data)
2152   `(car ,data))
2153
2154 (defmacro gnus-data-set-number (data number)
2155   `(setcar ,data ,number))
2156
2157 (defmacro gnus-data-mark (data)
2158   `(nth 1 ,data))
2159
2160 (defmacro gnus-data-set-mark (data mark)
2161   `(setcar (nthcdr 1 ,data) ,mark))
2162
2163 (defmacro gnus-data-pos (data)
2164   `(nth 2 ,data))
2165
2166 (defmacro gnus-data-set-pos (data pos)
2167   `(setcar (nthcdr 2 ,data) ,pos))
2168
2169 (defmacro gnus-data-header (data)
2170   `(nth 3 ,data))
2171
2172 (defmacro gnus-data-set-header (data header)
2173   `(setcar (nthcdr 3 ,data) ,header))
2174
2175 (defmacro gnus-data-level (data)
2176   `(nth 4 ,data))
2177
2178 (defmacro gnus-data-unread-p (data)
2179   `(= (nth 1 ,data) gnus-unread-mark))
2180
2181 (defmacro gnus-data-read-p (data)
2182   `(/= (nth 1 ,data) gnus-unread-mark))
2183
2184 (defmacro gnus-data-pseudo-p (data)
2185   `(consp (nth 3 ,data)))
2186
2187 (defmacro gnus-data-find (number)
2188   `(assq ,number gnus-newsgroup-data))
2189
2190 (defmacro gnus-data-find-list (number &optional data)
2191   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2192      (memq (assq ,number bdata)
2193            bdata)))
2194
2195 (defmacro gnus-data-make (number mark pos header level)
2196   `(list ,number ,mark ,pos ,header ,level))
2197
2198 (defun gnus-data-enter (after-article number mark pos header level offset)
2199   (let ((data (gnus-data-find-list after-article)))
2200     (unless data
2201       (error "No such article: %d" after-article))
2202     (setcdr data (cons (gnus-data-make number mark pos header level)
2203                        (cdr data)))
2204     (setq gnus-newsgroup-data-reverse nil)
2205     (gnus-data-update-list (cddr data) offset)))
2206
2207 (defun gnus-data-enter-list (after-article list &optional offset)
2208   (when list
2209     (let ((data (and after-article (gnus-data-find-list after-article)))
2210           (ilist list))
2211       (if (not (or data
2212                    after-article))
2213           (let ((odata gnus-newsgroup-data))
2214             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2215             (when offset
2216               (gnus-data-update-list odata offset)))
2217         ;; Find the last element in the list to be spliced into the main
2218         ;; list.
2219         (while (cdr list)
2220           (setq list (cdr list)))
2221         (if (not data)
2222             (progn
2223               (setcdr list gnus-newsgroup-data)
2224               (setq gnus-newsgroup-data ilist)
2225               (when offset
2226                 (gnus-data-update-list (cdr list) offset)))
2227           (setcdr list (cdr data))
2228           (setcdr data ilist)
2229           (when offset
2230             (gnus-data-update-list (cdr list) offset))))
2231       (setq gnus-newsgroup-data-reverse nil))))
2232
2233 (defun gnus-data-remove (article &optional offset)
2234   (let ((data gnus-newsgroup-data))
2235     (if (= (gnus-data-number (car data)) article)
2236         (progn
2237           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2238                 gnus-newsgroup-data-reverse nil)
2239           (when offset
2240             (gnus-data-update-list gnus-newsgroup-data offset)))
2241       (while (cdr data)
2242         (when (= (gnus-data-number (cadr data)) article)
2243           (setcdr data (cddr data))
2244           (when offset
2245             (gnus-data-update-list (cdr data) offset))
2246           (setq data nil
2247                 gnus-newsgroup-data-reverse nil))
2248         (setq data (cdr data))))))
2249
2250 (defmacro gnus-data-list (backward)
2251   `(if ,backward
2252        (or gnus-newsgroup-data-reverse
2253            (setq gnus-newsgroup-data-reverse
2254                  (reverse gnus-newsgroup-data)))
2255      gnus-newsgroup-data))
2256
2257 (defun gnus-data-update-list (data offset)
2258   "Add OFFSET to the POS of all data entries in DATA."
2259   (setq gnus-newsgroup-data-reverse nil)
2260   (while data
2261     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2262     (setq data (cdr data))))
2263
2264 (defun gnus-summary-article-pseudo-p (article)
2265   "Say whether this article is a pseudo article or not."
2266   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2267
2268 (defmacro gnus-summary-article-sparse-p (article)
2269   "Say whether this article is a sparse article or not."
2270   `(memq ,article gnus-newsgroup-sparse))
2271
2272 (defmacro gnus-summary-article-ancient-p (article)
2273   "Say whether this article is a sparse article or not."
2274   `(memq ,article gnus-newsgroup-ancient))
2275
2276 (defun gnus-article-parent-p (number)
2277   "Say whether this article is a parent or not."
2278   (let ((data (gnus-data-find-list number)))
2279     (and (cdr data)                     ; There has to be an article after...
2280          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2281             (gnus-data-level (nth 1 data))))))
2282
2283 (defun gnus-article-children (number)
2284   "Return a list of all children to NUMBER."
2285   (let* ((data (gnus-data-find-list number))
2286          (level (gnus-data-level (car data)))
2287          children)
2288     (setq data (cdr data))
2289     (while (and data
2290                 (= (gnus-data-level (car data)) (1+ level)))
2291       (push (gnus-data-number (car data)) children)
2292       (setq data (cdr data)))
2293     children))
2294
2295 (defmacro gnus-summary-skip-intangible ()
2296   "If the current article is intangible, then jump to a different article."
2297   '(let ((to (get-text-property (point) 'gnus-intangible)))
2298      (and to (gnus-summary-goto-subject to))))
2299
2300 (defmacro gnus-summary-article-intangible-p ()
2301   "Say whether this article is intangible or not."
2302   '(get-text-property (point) 'gnus-intangible))
2303
2304 (defun gnus-article-read-p (article)
2305   "Say whether ARTICLE is read or not."
2306   (not (or (memq article gnus-newsgroup-marked)
2307            (memq article gnus-newsgroup-unreads)
2308            (memq article gnus-newsgroup-unselected)
2309            (memq article gnus-newsgroup-dormant))))
2310
2311 ;; Some summary mode macros.
2312
2313 (defmacro gnus-summary-article-number ()
2314   "The article number of the article on the current line.
2315 If there isn's an article number here, then we return the current
2316 article number."
2317   '(progn
2318      (gnus-summary-skip-intangible)
2319      (or (get-text-property (point) 'gnus-number)
2320          (gnus-summary-last-subject))))
2321
2322 (defmacro gnus-summary-article-header (&optional number)
2323   "Return the header of article NUMBER."
2324   `(gnus-data-header (gnus-data-find
2325                       ,(or number '(gnus-summary-article-number)))))
2326
2327 (defmacro gnus-summary-thread-level (&optional number)
2328   "Return the level of thread that starts with article NUMBER."
2329   `(if (and (eq gnus-summary-make-false-root 'dummy)
2330             (get-text-property (point) 'gnus-intangible))
2331        0
2332      (gnus-data-level (gnus-data-find
2333                        ,(or number '(gnus-summary-article-number))))))
2334
2335 (defmacro gnus-summary-article-mark (&optional number)
2336   "Return the mark of article NUMBER."
2337   `(gnus-data-mark (gnus-data-find
2338                     ,(or number '(gnus-summary-article-number)))))
2339
2340 (defmacro gnus-summary-article-pos (&optional number)
2341   "Return the position of the line of article NUMBER."
2342   `(gnus-data-pos (gnus-data-find
2343                    ,(or number '(gnus-summary-article-number)))))
2344
2345 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2346 (defmacro gnus-summary-article-subject (&optional number)
2347   "Return current subject string or nil if nothing."
2348   `(let ((headers
2349           ,(if number
2350                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2351              '(gnus-data-header (assq (gnus-summary-article-number)
2352                                       gnus-newsgroup-data)))))
2353      (and headers
2354           (vectorp headers)
2355           (mail-header-subject headers))))
2356
2357 (defmacro gnus-summary-article-score (&optional number)
2358   "Return current article score."
2359   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2360                   gnus-newsgroup-scored))
2361        gnus-summary-default-score 0))
2362
2363 (defun gnus-summary-article-children (&optional number)
2364   "Return a list of article numbers that are children of article NUMBER."
2365   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2366          (level (gnus-data-level (car data)))
2367          l children)
2368     (while (and (setq data (cdr data))
2369                 (> (setq l (gnus-data-level (car data))) level))
2370       (and (= (1+ level) l)
2371            (push (gnus-data-number (car data))
2372                  children)))
2373     (nreverse children)))
2374
2375 (defun gnus-summary-article-parent (&optional number)
2376   "Return the article number of the parent of article NUMBER."
2377   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2378                                     (gnus-data-list t)))
2379          (level (gnus-data-level (car data))))
2380     (if (zerop level)
2381         ()                              ; This is a root.
2382       ;; We search until we find an article with a level less than
2383       ;; this one.  That function has to be the parent.
2384       (while (and (setq data (cdr data))
2385                   (not (< (gnus-data-level (car data)) level))))
2386       (and data (gnus-data-number (car data))))))
2387
2388 (defun gnus-unread-mark-p (mark)
2389   "Say whether MARK is the unread mark."
2390   (= mark gnus-unread-mark))
2391
2392 (defun gnus-read-mark-p (mark)
2393   "Say whether MARK is one of the marks that mark as read.
2394 This is all marks except unread, ticked, dormant, and expirable."
2395   (not (or (= mark gnus-unread-mark)
2396            (= mark gnus-ticked-mark)
2397            (= mark gnus-dormant-mark)
2398            (= mark gnus-expirable-mark))))
2399
2400 (defmacro gnus-article-mark (number)
2401   "Return the MARK of article NUMBER.
2402 This macro should only be used when computing the mark the \"first\"
2403 time; i.e., when generating the summary lines.  After that,
2404 `gnus-summary-article-mark' should be used to examine the
2405 marks of articles."
2406   `(cond
2407     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2408     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2409     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2410     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2411     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2412     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2413     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2414     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2415            gnus-ancient-mark))))
2416
2417 ;; Saving hidden threads.
2418
2419 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2420 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2421
2422 (defmacro gnus-save-hidden-threads (&rest forms)
2423   "Save hidden threads, eval FORMS, and restore the hidden threads."
2424   (let ((config (make-symbol "config")))
2425     `(let ((,config (gnus-hidden-threads-configuration)))
2426        (unwind-protect
2427            (save-excursion
2428              ,@forms)
2429          (gnus-restore-hidden-threads-configuration ,config)))))
2430
2431 (defun gnus-data-compute-positions ()
2432   "Compute the positions of all articles."
2433   (setq gnus-newsgroup-data-reverse nil)
2434   (let ((data gnus-newsgroup-data))
2435     (save-excursion
2436       (gnus-save-hidden-threads
2437         (gnus-summary-show-all-threads)
2438         (goto-char (point-min))
2439         (while data
2440           (while (get-text-property (point) 'gnus-intangible)
2441             (forward-line 1))
2442           (gnus-data-set-pos (car data) (+ (point) 3))
2443           (setq data (cdr data))
2444           (forward-line 1))))))
2445
2446 (defun gnus-hidden-threads-configuration ()
2447   "Return the current hidden threads configuration."
2448   (save-excursion
2449     (let (config)
2450       (goto-char (point-min))
2451       (while (search-forward "\r" nil t)
2452         (push (1- (point)) config))
2453       config)))
2454
2455 (defun gnus-restore-hidden-threads-configuration (config)
2456   "Restore hidden threads configuration from CONFIG."
2457   (save-excursion
2458     (let (point buffer-read-only)
2459       (while (setq point (pop config))
2460         (when (and (< point (point-max))
2461                    (goto-char point)
2462                    (eq (char-after) ?\n))
2463           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2464
2465 ;; Various summary mode internalish functions.
2466
2467 (defun gnus-mouse-pick-article (e)
2468   (interactive "e")
2469   (mouse-set-point e)
2470   (gnus-summary-next-page nil t))
2471
2472 (defun gnus-summary-set-display-table ()
2473   "Change the display table.
2474 Odd characters have a tendency to mess
2475 up nicely formatted displays - we make all possible glyphs
2476 display only a single character."
2477
2478   ;; We start from the standard display table, if any.
2479   (let ((table (or (copy-sequence standard-display-table)
2480                    (make-display-table)))
2481         (i 32))
2482     ;; Nix out all the control chars...
2483     (while (>= (setq i (1- i)) 0)
2484       (aset table i [??]))
2485     ;; ... but not newline and cr, of course.  (cr is necessary for the
2486     ;; selective display).
2487     (aset table ?\n nil)
2488     (aset table ?\r nil)
2489     ;; We keep TAB as well.
2490     (aset table ?\t nil)
2491     ;; We nix out any glyphs over 126 that are not set already.
2492     (let ((i 256))
2493       (while (>= (setq i (1- i)) 127)
2494         ;; Only modify if the entry is nil.
2495         (unless (aref table i)
2496           (aset table i [??]))))
2497     (setq buffer-display-table table)))
2498
2499 (defun gnus-summary-setup-buffer (group)
2500   "Initialize summary buffer."
2501   (let ((buffer (concat "*Summary " group "*")))
2502     (if (get-buffer buffer)
2503         (progn
2504           (set-buffer buffer)
2505           (setq gnus-summary-buffer (current-buffer))
2506           (not gnus-newsgroup-prepared))
2507       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2508       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2509       (gnus-summary-mode group)
2510       (when gnus-carpal
2511         (gnus-carpal-setup-buffer 'summary))
2512       (unless gnus-single-article-buffer
2513         (make-local-variable 'gnus-article-buffer)
2514         (make-local-variable 'gnus-article-current)
2515         (make-local-variable 'gnus-original-article-buffer))
2516       (setq gnus-newsgroup-name group)
2517       t)))
2518
2519 (defun gnus-set-global-variables ()
2520   "Set the global equivalents of the buffer-local variables.
2521 They are set to the latest values they had.  These reflect the summary
2522 buffer that was in action when the last article was fetched."
2523   (when (eq major-mode 'gnus-summary-mode)
2524     (setq gnus-summary-buffer (current-buffer))
2525     (let ((name gnus-newsgroup-name)
2526           (marked gnus-newsgroup-marked)
2527           (unread gnus-newsgroup-unreads)
2528           (headers gnus-current-headers)
2529           (data gnus-newsgroup-data)
2530           (summary gnus-summary-buffer)
2531           (article-buffer gnus-article-buffer)
2532           (original gnus-original-article-buffer)
2533           (gac gnus-article-current)
2534           (reffed gnus-reffed-article-number)
2535           (score-file gnus-current-score-file)
2536           (default-charset gnus-newsgroup-charset))
2537       (save-excursion
2538         (set-buffer gnus-group-buffer)
2539         (setq gnus-newsgroup-name name
2540               gnus-newsgroup-marked marked
2541               gnus-newsgroup-unreads unread
2542               gnus-current-headers headers
2543               gnus-newsgroup-data data
2544               gnus-article-current gac
2545               gnus-summary-buffer summary
2546               gnus-article-buffer article-buffer
2547               gnus-original-article-buffer original
2548               gnus-reffed-article-number reffed
2549               gnus-current-score-file score-file
2550               gnus-newsgroup-charset default-charset)
2551         ;; The article buffer also has local variables.
2552         (when (gnus-buffer-live-p gnus-article-buffer)
2553           (set-buffer gnus-article-buffer)
2554           (setq gnus-summary-buffer summary))))))
2555
2556 (defun gnus-summary-article-unread-p (article)
2557   "Say whether ARTICLE is unread or not."
2558   (memq article gnus-newsgroup-unreads))
2559
2560 (defun gnus-summary-first-article-p (&optional article)
2561   "Return whether ARTICLE is the first article in the buffer."
2562   (if (not (setq article (or article (gnus-summary-article-number))))
2563       nil
2564     (eq article (caar gnus-newsgroup-data))))
2565
2566 (defun gnus-summary-last-article-p (&optional article)
2567   "Return whether ARTICLE is the last article in the buffer."
2568   (if (not (setq article (or article (gnus-summary-article-number))))
2569       ;; All non-existent numbers are the last article.  :-)
2570       t
2571     (not (cdr (gnus-data-find-list article)))))
2572
2573 (defun gnus-make-thread-indent-array ()
2574   (let ((n 200))
2575     (unless (and gnus-thread-indent-array
2576                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2577       (setq gnus-thread-indent-array (make-vector 201 "")
2578             gnus-thread-indent-array-level gnus-thread-indent-level)
2579       (while (>= n 0)
2580         (aset gnus-thread-indent-array n
2581               (make-string (* n gnus-thread-indent-level) ? ))
2582         (setq n (1- n))))))
2583
2584 (defun gnus-update-summary-mark-positions ()
2585   "Compute where the summary marks are to go."
2586   (save-excursion
2587     (when (gnus-buffer-exists-p gnus-summary-buffer)
2588       (set-buffer gnus-summary-buffer))
2589     (let ((gnus-replied-mark 129)
2590           (gnus-score-below-mark 130)
2591           (gnus-score-over-mark 130)
2592           (gnus-download-mark 131)
2593           (spec gnus-summary-line-format-spec)
2594           gnus-visual pos)
2595       (save-excursion
2596         (gnus-set-work-buffer)
2597         (let ((gnus-summary-line-format-spec spec)
2598               (gnus-newsgroup-downloadable '((0 . t))))
2599           (gnus-summary-insert-line
2600            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2601            0 nil 128 t nil "" nil 1)
2602           (goto-char (point-min))
2603           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2604                                              (- (point) 2)))))
2605           (goto-char (point-min))
2606           (push (cons 'replied (and (search-forward "\201" nil t)
2607                                     (- (point) 2)))
2608                 pos)
2609           (goto-char (point-min))
2610           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2611                 pos)
2612           (goto-char (point-min))
2613           (push (cons 'download
2614                       (and (search-forward "\203" nil t) (- (point) 2)))
2615                 pos)))
2616       (setq gnus-summary-mark-positions pos))))
2617
2618 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2619   "Insert a dummy root in the summary buffer."
2620   (beginning-of-line)
2621   (gnus-add-text-properties
2622    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2623    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2624
2625 (defun gnus-summary-from-or-to-or-newsgroups (header)
2626   (let ((to (cdr (assq 'To (mail-header-extra header))))
2627         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2628         (default-mime-charset (with-current-buffer gnus-summary-buffer
2629                                 default-mime-charset)))
2630     (cond
2631      ((and to
2632            gnus-ignored-from-addresses
2633            (string-match gnus-ignored-from-addresses
2634                          (mail-header-from header)))
2635       (concat "-> "
2636               (or (car (funcall gnus-extract-address-components
2637                                 (funcall
2638                                  gnus-decode-encoded-word-function to)))
2639                   (funcall gnus-decode-encoded-word-function to))))
2640      ((and newsgroups
2641            gnus-ignored-from-addresses
2642            (string-match gnus-ignored-from-addresses
2643                          (mail-header-from header)))
2644       (concat "=> " newsgroups))
2645      (t
2646       (or (car (funcall gnus-extract-address-components
2647                         (mail-header-from header)))
2648           (mail-header-from header))))))
2649
2650 (defun gnus-summary-insert-line (gnus-tmp-header
2651                                  gnus-tmp-level gnus-tmp-current
2652                                  gnus-tmp-unread gnus-tmp-replied
2653                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2654                                  &optional gnus-tmp-dummy gnus-tmp-score
2655                                  gnus-tmp-process)
2656   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2657          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2658          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2659          (gnus-tmp-score-char
2660           (if (or (null gnus-summary-default-score)
2661                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2662                       gnus-summary-zcore-fuzz))
2663               ?  ;Whitespace
2664             (if (< gnus-tmp-score gnus-summary-default-score)
2665                 gnus-score-below-mark gnus-score-over-mark)))
2666          (gnus-tmp-replied
2667           (cond (gnus-tmp-process gnus-process-mark)
2668                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2669                  gnus-cached-mark)
2670                 (gnus-tmp-replied gnus-replied-mark)
2671                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2672                  gnus-saved-mark)
2673                 (t gnus-unread-mark)))
2674          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2675          (gnus-tmp-name
2676           (cond
2677            ((string-match "<[^>]+> *$" gnus-tmp-from)
2678             (let ((beg (match-beginning 0)))
2679               (or (and (string-match "^\".+\"" gnus-tmp-from)
2680                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2681                   (substring gnus-tmp-from 0 beg))))
2682            ((string-match "(.+)" gnus-tmp-from)
2683             (substring gnus-tmp-from
2684                        (1+ (match-beginning 0)) (1- (match-end 0))))
2685            (t gnus-tmp-from)))
2686          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2687          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2688          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2689          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2690          (buffer-read-only nil))
2691     (when (string= gnus-tmp-name "")
2692       (setq gnus-tmp-name gnus-tmp-from))
2693     (unless (numberp gnus-tmp-lines)
2694       (setq gnus-tmp-lines 0))
2695     (gnus-put-text-property-excluding-characters-with-faces
2696      (point)
2697      (progn (eval gnus-summary-line-format-spec) (point))
2698      'gnus-number gnus-tmp-number)
2699     (when (gnus-visual-p 'summary-highlight 'highlight)
2700       (forward-line -1)
2701       (gnus-run-hooks 'gnus-summary-update-hook)
2702       (forward-line 1))))
2703
2704 (defun gnus-summary-update-line (&optional dont-update)
2705   "Update summary line after change."
2706   (when (and gnus-summary-default-score
2707              (not gnus-summary-inhibit-highlight))
2708     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2709            (article (gnus-summary-article-number))
2710            (score (gnus-summary-article-score article)))
2711       (unless dont-update
2712         (if (and gnus-summary-mark-below
2713                  (< (gnus-summary-article-score)
2714                     gnus-summary-mark-below))
2715             ;; This article has a low score, so we mark it as read.
2716             (when (memq article gnus-newsgroup-unreads)
2717               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2718           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2719             ;; This article was previously marked as read on account
2720             ;; of a low score, but now it has risen, so we mark it as
2721             ;; unread.
2722             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2723         (gnus-summary-update-mark
2724          (if (or (null gnus-summary-default-score)
2725                  (<= (abs (- score gnus-summary-default-score))
2726                      gnus-summary-zcore-fuzz))
2727              ?  ;Whitespace
2728            (if (< score gnus-summary-default-score)
2729                gnus-score-below-mark gnus-score-over-mark))
2730          'score))
2731       ;; Do visual highlighting.
2732       (when (gnus-visual-p 'summary-highlight 'highlight)
2733         (gnus-run-hooks 'gnus-summary-update-hook)))))
2734
2735 (defvar gnus-tmp-new-adopts nil)
2736
2737 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2738   "Return the number of articles in THREAD.
2739 This may be 0 in some cases -- if none of the articles in
2740 the thread are to be displayed."
2741   (let* ((number
2742           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2743           (cond
2744            ((not (listp thread))
2745             1)
2746            ((and (consp thread) (cdr thread))
2747             (apply
2748              '+ 1 (mapcar
2749                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2750            ((null thread)
2751             1)
2752            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2753             1)
2754            (t 0))))
2755     (when (and level (zerop level) gnus-tmp-new-adopts)
2756       (incf number
2757             (apply '+ (mapcar
2758                        'gnus-summary-number-of-articles-in-thread
2759                        gnus-tmp-new-adopts))))
2760     (if char
2761         (if (> number 1) gnus-not-empty-thread-mark
2762           gnus-empty-thread-mark)
2763       number)))
2764
2765 (defun gnus-summary-set-local-parameters (group)
2766   "Go through the local params of GROUP and set all variable specs in that list."
2767   (let ((params (gnus-group-find-parameter group))
2768         elem)
2769     (while params
2770       (setq elem (car params)
2771             params (cdr params))
2772       (and (consp elem)                 ; Has to be a cons.
2773            (consp (cdr elem))           ; The cdr has to be a list.
2774            (symbolp (car elem))         ; Has to be a symbol in there.
2775            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2776            (ignore-errors               ; So we set it.
2777              (make-local-variable (car elem))
2778              (set (car elem) (eval (nth 1 elem))))))))
2779
2780 (defun gnus-summary-read-group (group &optional show-all no-article
2781                                       kill-buffer no-display backward
2782                                       select-articles)
2783   "Start reading news in newsgroup GROUP.
2784 If SHOW-ALL is non-nil, already read articles are also listed.
2785 If NO-ARTICLE is non-nil, no article is selected initially.
2786 If NO-DISPLAY, don't generate a summary buffer."
2787   (let (result)
2788     (while (and group
2789                 (null (setq result
2790                             (let ((gnus-auto-select-next nil))
2791                               (or (gnus-summary-read-group-1
2792                                    group show-all no-article
2793                                    kill-buffer no-display
2794                                    select-articles)
2795                                   (setq show-all nil
2796                                         select-articles nil)))))
2797                 (eq gnus-auto-select-next 'quietly))
2798       (set-buffer gnus-group-buffer)
2799       ;; The entry function called above goes to the next
2800       ;; group automatically, so we go two groups back
2801       ;; if we are searching for the previous group.
2802       (when backward
2803         (gnus-group-prev-unread-group 2))
2804       (if (not (equal group (gnus-group-group-name)))
2805           (setq group (gnus-group-group-name))
2806         (setq group nil)))
2807     result))
2808
2809 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2810   "Directly jump to the other GROUP from summary buffer.
2811 If SHOW-ALL is non-nil, already read articles are also listed."
2812   (interactive
2813    (if (eq gnus-summary-buffer (current-buffer))
2814        (list (completing-read
2815               "Group: " gnus-active-hashtb nil t
2816               (when (and gnus-newsgroup-name
2817                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2818                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2819               'gnus-group-history)
2820              current-prefix-arg)
2821      (error "%s must be invoked from a gnus summary buffer." this-command)))
2822   (unless (or (zerop (length group))
2823               (and gnus-newsgroup-name
2824                    (string-equal gnus-newsgroup-name group)))
2825     (gnus-summary-exit)
2826     (gnus-summary-read-group group show-all
2827                              gnus-dont-select-after-jump-to-other-group)))
2828
2829 (defun gnus-summary-read-group-1 (group show-all no-article
2830                                         kill-buffer no-display
2831                                         &optional select-articles)
2832   ;; Killed foreign groups can't be entered.
2833   (when (and (not (gnus-group-native-p group))
2834              (not (gnus-gethash group gnus-newsrc-hashtb)))
2835     (error "Dead non-native groups can't be entered"))
2836   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2837   (let* ((new-group (gnus-summary-setup-buffer group))
2838          (quit-config (gnus-group-quit-config group))
2839          (did-select (and new-group (gnus-select-newsgroup
2840                                      group show-all select-articles))))
2841     (cond
2842      ;; This summary buffer exists already, so we just select it.
2843      ((not new-group)
2844       (gnus-set-global-variables)
2845       (when kill-buffer
2846         (gnus-kill-or-deaden-summary kill-buffer))
2847       (gnus-configure-windows 'summary 'force)
2848       (gnus-set-mode-line 'summary)
2849       (gnus-summary-position-point)
2850       (message "")
2851       t)
2852      ;; We couldn't select this group.
2853      ((null did-select)
2854       (when (and (eq major-mode 'gnus-summary-mode)
2855                  (not (equal (current-buffer) kill-buffer)))
2856         (kill-buffer (current-buffer))
2857         (if (not quit-config)
2858             (progn
2859               ;; Update the info -- marks might need to be removed,
2860               ;; for instance.
2861               (gnus-summary-update-info)
2862               (set-buffer gnus-group-buffer)
2863               (gnus-group-jump-to-group group)
2864               (gnus-group-next-unread-group 1))
2865           (gnus-handle-ephemeral-exit quit-config)))
2866       (gnus-message 3 "Can't select group")
2867       nil)
2868      ;; The user did a `C-g' while prompting for number of articles,
2869      ;; so we exit this group.
2870      ((eq did-select 'quit)
2871       (and (eq major-mode 'gnus-summary-mode)
2872            (not (equal (current-buffer) kill-buffer))
2873            (kill-buffer (current-buffer)))
2874       (when kill-buffer
2875         (gnus-kill-or-deaden-summary kill-buffer))
2876       (if (not quit-config)
2877           (progn
2878             (set-buffer gnus-group-buffer)
2879             (gnus-group-jump-to-group group)
2880             (gnus-group-next-unread-group 1)
2881             (gnus-configure-windows 'group 'force))
2882         (gnus-handle-ephemeral-exit quit-config))
2883       ;; Finally signal the quit.
2884       (signal 'quit nil))
2885      ;; The group was successfully selected.
2886      (t
2887       (gnus-set-global-variables)
2888       ;; Save the active value in effect when the group was entered.
2889       (setq gnus-newsgroup-active
2890             (gnus-copy-sequence
2891              (gnus-active gnus-newsgroup-name)))
2892       ;; You can change the summary buffer in some way with this hook.
2893       (gnus-run-hooks 'gnus-select-group-hook)
2894       ;; Set any local variables in the group parameters.
2895       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2896       (gnus-update-format-specifications
2897        nil 'summary 'summary-mode 'summary-dummy)
2898       (gnus-update-summary-mark-positions)
2899       ;; Do score processing.
2900       (when gnus-use-scoring
2901         (gnus-possibly-score-headers))
2902       ;; Check whether to fill in the gaps in the threads.
2903       (when gnus-build-sparse-threads
2904         (gnus-build-sparse-threads))
2905       ;; Find the initial limit.
2906       (if gnus-show-threads
2907           (if show-all
2908               (let ((gnus-newsgroup-dormant nil))
2909                 (gnus-summary-initial-limit show-all))
2910             (gnus-summary-initial-limit show-all))
2911         ;; When untreaded, all articles are always shown.
2912         (setq gnus-newsgroup-limit
2913               (mapcar
2914                (lambda (header) (mail-header-number header))
2915                gnus-newsgroup-headers)))
2916       ;; Generate the summary buffer.
2917       (unless no-display
2918         (gnus-summary-prepare))
2919       (when gnus-use-trees
2920         (gnus-tree-open group)
2921         (setq gnus-summary-highlight-line-function
2922               'gnus-tree-highlight-article))
2923       ;; If the summary buffer is empty, but there are some low-scored
2924       ;; articles or some excluded dormants, we include these in the
2925       ;; buffer.
2926       (when (and (zerop (buffer-size))
2927                  (not no-display))
2928         (cond (gnus-newsgroup-dormant
2929                (gnus-summary-limit-include-dormant))
2930               ((and gnus-newsgroup-scored show-all)
2931                (gnus-summary-limit-include-expunged t))))
2932       ;; Function `gnus-apply-kill-file' must be called in this hook.
2933       (gnus-run-hooks 'gnus-apply-kill-hook)
2934       (if (and (zerop (buffer-size))
2935                (not no-display))
2936           (progn
2937             ;; This newsgroup is empty.
2938             (gnus-summary-catchup-and-exit nil t)
2939             (gnus-message 6 "No unread news")
2940             (when kill-buffer
2941               (gnus-kill-or-deaden-summary kill-buffer))
2942             ;; Return nil from this function.
2943             nil)
2944         ;; Hide conversation thread subtrees.  We cannot do this in
2945         ;; gnus-summary-prepare-hook since kill processing may not
2946         ;; work with hidden articles.
2947         (and gnus-show-threads
2948              gnus-thread-hide-subtree
2949              (gnus-summary-hide-all-threads))
2950         (when kill-buffer
2951           (gnus-kill-or-deaden-summary kill-buffer))
2952         ;; Show first unread article if requested.
2953         (if (and (not no-article)
2954                  (not no-display)
2955                  gnus-newsgroup-unreads
2956                  gnus-auto-select-first)
2957             (progn
2958               (gnus-configure-windows 'summary)
2959               (cond
2960                ((eq gnus-auto-select-first 'best)
2961                 (gnus-summary-best-unread-article))
2962                ((eq gnus-auto-select-first t)
2963                 (gnus-summary-first-unread-article))
2964                ((gnus-functionp gnus-auto-select-first)
2965                 (funcall gnus-auto-select-first))))
2966           ;; Don't select any articles, just move point to the first
2967           ;; article in the group.
2968           (goto-char (point-min))
2969           (gnus-summary-position-point)
2970           (gnus-configure-windows 'summary 'force)
2971           (gnus-set-mode-line 'summary))
2972         (when (get-buffer-window gnus-group-buffer t)
2973           ;; Gotta use windows, because recenter does weird stuff if
2974           ;; the current buffer ain't the displayed window.
2975           (let ((owin (selected-window)))
2976             (select-window (get-buffer-window gnus-group-buffer t))
2977             (when (gnus-group-goto-group group)
2978               (recenter))
2979             (select-window owin)))
2980         ;; Mark this buffer as "prepared".
2981         (setq gnus-newsgroup-prepared t)
2982         (gnus-run-hooks 'gnus-summary-prepared-hook)
2983         t)))))
2984
2985 (defun gnus-summary-prepare ()
2986   "Generate the summary buffer."
2987   (interactive)
2988   (let ((buffer-read-only nil))
2989     (erase-buffer)
2990     (setq gnus-newsgroup-data nil
2991           gnus-newsgroup-data-reverse nil)
2992     (gnus-run-hooks 'gnus-summary-generate-hook)
2993     ;; Generate the buffer, either with threads or without.
2994     (when gnus-newsgroup-headers
2995       (gnus-summary-prepare-threads
2996        (if gnus-show-threads
2997            (gnus-sort-gathered-threads
2998             (funcall gnus-summary-thread-gathering-function
2999                      (gnus-sort-threads
3000                       (gnus-cut-threads (gnus-make-threads)))))
3001          ;; Unthreaded display.
3002          (gnus-sort-articles gnus-newsgroup-headers))))
3003     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3004     ;; Call hooks for modifying summary buffer.
3005     (goto-char (point-min))
3006     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3007
3008 (defsubst gnus-general-simplify-subject (subject)
3009   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3010   (setq subject
3011         (cond
3012          ;; Truncate the subject.
3013          (gnus-simplify-subject-functions
3014           (gnus-map-function gnus-simplify-subject-functions subject))
3015          ((numberp gnus-summary-gather-subject-limit)
3016           (setq subject (gnus-simplify-subject-re subject))
3017           (if (> (length subject) gnus-summary-gather-subject-limit)
3018               (substring subject 0 gnus-summary-gather-subject-limit)
3019             subject))
3020          ;; Fuzzily simplify it.
3021          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3022           (gnus-simplify-subject-fuzzy subject))
3023          ;; Just remove the leading "Re:".
3024          (t
3025           (gnus-simplify-subject-re subject))))
3026
3027   (if (and gnus-summary-gather-exclude-subject
3028            (string-match gnus-summary-gather-exclude-subject subject))
3029       nil                               ; This article shouldn't be gathered
3030     subject))
3031
3032 (defun gnus-summary-simplify-subject-query ()
3033   "Query where the respool algorithm would put this article."
3034   (interactive)
3035   (gnus-summary-select-article)
3036   (message "%s"
3037            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3038
3039 (defun gnus-gather-threads-by-subject (threads)
3040   "Gather threads by looking at Subject headers."
3041   (if (not gnus-summary-make-false-root)
3042       threads
3043     (let ((hashtb (gnus-make-hashtable 1024))
3044           (prev threads)
3045           (result threads)
3046           subject hthread whole-subject)
3047       (while threads
3048         (setq subject (gnus-general-simplify-subject
3049                        (setq whole-subject (mail-header-subject
3050                                             (caar threads)))))
3051         (when subject
3052           (if (setq hthread (gnus-gethash subject hashtb))
3053               (progn
3054                 ;; We enter a dummy root into the thread, if we
3055                 ;; haven't done that already.
3056                 (unless (stringp (caar hthread))
3057                   (setcar hthread (list whole-subject (car hthread))))
3058                 ;; We add this new gathered thread to this gathered
3059                 ;; thread.
3060                 (setcdr (car hthread)
3061                         (nconc (cdar hthread) (list (car threads))))
3062                 ;; Remove it from the list of threads.
3063                 (setcdr prev (cdr threads))
3064                 (setq threads prev))
3065             ;; Enter this thread into the hash table.
3066             (gnus-sethash subject threads hashtb)))
3067         (setq prev threads)
3068         (setq threads (cdr threads)))
3069       result)))
3070
3071 (defun gnus-gather-threads-by-references (threads)
3072   "Gather threads by looking at References headers."
3073   (let ((idhashtb (gnus-make-hashtable 1024))
3074         (thhashtb (gnus-make-hashtable 1024))
3075         (prev threads)
3076         (result threads)
3077         ids references id gthread gid entered ref)
3078     (while threads
3079       (when (setq references (mail-header-references (caar threads)))
3080         (setq id (mail-header-id (caar threads))
3081               ids (gnus-split-references references)
3082               entered nil)
3083         (while (setq ref (pop ids))
3084           (setq ids (delete ref ids))
3085           (if (not (setq gid (gnus-gethash ref idhashtb)))
3086               (progn
3087                 (gnus-sethash ref id idhashtb)
3088                 (gnus-sethash id threads thhashtb))
3089             (setq gthread (gnus-gethash gid thhashtb))
3090             (unless entered
3091               ;; We enter a dummy root into the thread, if we
3092               ;; haven't done that already.
3093               (unless (stringp (caar gthread))
3094                 (setcar gthread (list (mail-header-subject (caar gthread))
3095                                       (car gthread))))
3096               ;; We add this new gathered thread to this gathered
3097               ;; thread.
3098               (setcdr (car gthread)
3099                       (nconc (cdar gthread) (list (car threads)))))
3100             ;; Add it into the thread hash table.
3101             (gnus-sethash id gthread thhashtb)
3102             (setq entered t)
3103             ;; Remove it from the list of threads.
3104             (setcdr prev (cdr threads))
3105             (setq threads prev))))
3106       (setq prev threads)
3107       (setq threads (cdr threads)))
3108     result))
3109
3110 (defun gnus-sort-gathered-threads (threads)
3111   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3112   (let ((result threads))
3113     (while threads
3114       (when (stringp (caar threads))
3115         (setcdr (car threads)
3116                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3117       (setq threads (cdr threads)))
3118     result))
3119
3120 (defun gnus-thread-loop-p (root thread)
3121   "Say whether ROOT is in THREAD."
3122   (let ((stack (list thread))
3123         (infloop 0)
3124         th)
3125     (while (setq thread (pop stack))
3126       (setq th (cdr thread))
3127       (while (and th
3128                   (not (eq (caar th) root)))
3129         (pop th))
3130       (if th
3131           ;; We have found a loop.
3132           (let (ref-dep)
3133             (setcdr thread (delq (car th) (cdr thread)))
3134             (if (boundp (setq ref-dep (intern "none"
3135                                               gnus-newsgroup-dependencies)))
3136                 (setcdr (symbol-value ref-dep)
3137                         (nconc (cdr (symbol-value ref-dep))
3138                                (list (car th))))
3139               (set ref-dep (list nil (car th))))
3140             (setq infloop 1
3141                   stack nil))
3142         ;; Push all the subthreads onto the stack.
3143         (push (cdr thread) stack)))
3144     infloop))
3145
3146 (defun gnus-make-threads ()
3147   "Go through the dependency hashtb and find the roots.  Return all threads."
3148   (let (threads)
3149     (while (catch 'infloop
3150              (mapatoms
3151               (lambda (refs)
3152                 ;; Deal with self-referencing References loops.
3153                 (when (and (car (symbol-value refs))
3154                            (not (zerop
3155                                  (apply
3156                                   '+
3157                                   (mapcar
3158                                    (lambda (thread)
3159                                      (gnus-thread-loop-p
3160                                       (car (symbol-value refs)) thread))
3161                                    (cdr (symbol-value refs)))))))
3162                   (setq threads nil)
3163                   (throw 'infloop t))
3164                 (unless (car (symbol-value refs))
3165                   ;; These threads do not refer back to any other articles,
3166                   ;; so they're roots.
3167                   (setq threads (append (cdr (symbol-value refs)) threads))))
3168               gnus-newsgroup-dependencies)))
3169     threads))
3170
3171 ;; Build the thread tree.
3172 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3173   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3174
3175 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3176 if it was already present.
3177
3178 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3179 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3180 Message-IDs will be renamed be renamed to a unique Message-ID before
3181 being entered.
3182
3183 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3184   (let* ((id (mail-header-id header))
3185          (id-dep (and id (intern id dependencies)))
3186          ref ref-dep ref-header)
3187     ;; Enter this `header' in the `dependencies' table.
3188     (cond
3189      ((not id-dep)
3190       (setq header nil))
3191      ;; The first two cases do the normal part: enter a new `header'
3192      ;; in the `dependencies' table.
3193      ((not (boundp id-dep))
3194       (set id-dep (list header)))
3195      ((null (car (symbol-value id-dep)))
3196       (setcar (symbol-value id-dep) header))
3197
3198      ;; From here the `header' was already present in the
3199      ;; `dependencies' table.
3200      (force-new
3201       ;; Overrides an existing entry;
3202       ;; just set the header part of the entry.
3203       (setcar (symbol-value id-dep) header))
3204
3205      ;; Renames the existing `header' to a unique Message-ID.
3206      ((not gnus-summary-ignore-duplicates)
3207       ;; An article with this Message-ID has already been seen.
3208       ;; We rename the Message-ID.
3209       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3210            (list header))
3211       (mail-header-set-id header id))
3212
3213      ;; The last case ignores an existing entry, except it adds any
3214      ;; additional Xrefs (in case the two articles came from different
3215      ;; servers.
3216      ;; Also sets `header' to `nil' meaning that the `dependencies'
3217      ;; table was *not* modified.
3218      (t
3219       (mail-header-set-xref
3220        (car (symbol-value id-dep))
3221        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3222                    "")
3223                (or (mail-header-xref header) "")))
3224       (setq header nil)))
3225
3226     (when header
3227       ;; First check if that we are not creating a References loop.
3228       (setq ref (gnus-parent-id (mail-header-references header)))
3229       (while (and ref
3230                   (setq ref-dep (intern-soft ref dependencies))
3231                   (boundp ref-dep)
3232                   (setq ref-header (car (symbol-value ref-dep))))
3233         (if (string= id ref)
3234             ;; Yuk!  This is a reference loop.  Make the article be a
3235             ;; root article.
3236             (progn
3237               (mail-header-set-references (car (symbol-value id-dep)) "none")
3238               (setq ref nil))
3239           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3240       (setq ref (gnus-parent-id (mail-header-references header)))
3241       (setq ref-dep (intern (or ref "none") dependencies))
3242       (if (boundp ref-dep)
3243           (setcdr (symbol-value ref-dep)
3244                   (nconc (cdr (symbol-value ref-dep))
3245                          (list (symbol-value id-dep))))
3246         (set ref-dep (list nil (symbol-value id-dep)))))
3247     header))
3248
3249 (defun gnus-build-sparse-threads ()
3250   (let ((headers gnus-newsgroup-headers)
3251         (mail-parse-charset gnus-newsgroup-charset)
3252         (gnus-summary-ignore-duplicates t)
3253         header references generation relations
3254         subject child end new-child date)
3255     ;; First we create an alist of generations/relations, where
3256     ;; generations is how much we trust the relation, and the relation
3257     ;; is parent/child.
3258     (gnus-message 7 "Making sparse threads...")
3259     (save-excursion
3260       (nnheader-set-temp-buffer " *gnus sparse threads*")
3261       (while (setq header (pop headers))
3262         (when (and (setq references (mail-header-references header))
3263                    (not (string= references "")))
3264           (insert references)
3265           (setq child (mail-header-id header)
3266                 subject (mail-header-subject header)
3267                 date (mail-header-date header)
3268                 generation 0)
3269           (while (search-backward ">" nil t)
3270             (setq end (1+ (point)))
3271             (when (search-backward "<" nil t)
3272               (setq new-child (buffer-substring (point) end))
3273               (push (list (incf generation)
3274                           child (setq child new-child)
3275                           subject date)
3276                     relations)))
3277           (when child
3278             (push (list (1+ generation) child nil subject) relations))
3279           (erase-buffer)))
3280       (kill-buffer (current-buffer)))
3281     ;; Sort over trustworthiness.
3282     (mapcar
3283      (lambda (relation)
3284        (when (gnus-dependencies-add-header
3285               (make-full-mail-header-from-decoded-header
3286                gnus-reffed-article-number
3287                (nth 3 relation) "" (or (nth 4 relation) "")
3288                (nth 1 relation)
3289                (or (nth 2 relation) "") 0 0 "")
3290               gnus-newsgroup-dependencies nil)
3291          (push gnus-reffed-article-number gnus-newsgroup-limit)
3292          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3293          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3294                gnus-newsgroup-reads)
3295          (decf gnus-reffed-article-number)))
3296      (sort relations 'car-less-than-car))
3297     (gnus-message 7 "Making sparse threads...done")))
3298
3299 (defun gnus-build-old-threads ()
3300   ;; Look at all the articles that refer back to old articles, and
3301   ;; fetch the headers for the articles that aren't there.  This will
3302   ;; build complete threads - if the roots haven't been expired by the
3303   ;; server, that is.
3304   (let ((mail-parse-charset gnus-newsgroup-charset)
3305         id heads)
3306     (mapatoms
3307      (lambda (refs)
3308        (when (not (car (symbol-value refs)))
3309          (setq heads (cdr (symbol-value refs)))
3310          (while heads
3311            (if (memq (mail-header-number (caar heads))
3312                      gnus-newsgroup-dormant)
3313                (setq heads (cdr heads))
3314              (setq id (symbol-name refs))
3315              (while (and (setq id (gnus-build-get-header id))
3316                          (not (car (gnus-id-to-thread id)))))
3317              (setq heads nil)))))
3318      gnus-newsgroup-dependencies)))
3319
3320 ;; This function has to be called with point after the article number
3321 ;; on the beginning of the line.
3322 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3323   (let ((eol (gnus-point-at-eol))
3324         (buffer (current-buffer))
3325         header)
3326
3327     ;; overview: [num subject from date id refs chars lines misc]
3328     (unwind-protect
3329         (progn
3330           (narrow-to-region (point) eol)
3331           (unless (eobp)
3332             (forward-char))
3333
3334           (setq header
3335                 (make-full-mail-header
3336                  number                         ; number
3337                  (nnheader-nov-field)           ; subject
3338                  (nnheader-nov-field)           ; from
3339                  (nnheader-nov-field)           ; date
3340                  (nnheader-nov-read-message-id) ; id
3341                  (nnheader-nov-field)           ; refs
3342                  (nnheader-nov-read-integer)    ; chars
3343                  (nnheader-nov-read-integer)    ; lines
3344                  (unless (eobp)
3345                    (if (looking-at "Xref: ")
3346                        (goto-char (match-end 0)))
3347                    (nnheader-nov-field))        ; Xref
3348                  (nnheader-nov-parse-extra))))  ; extra
3349
3350       (widen))
3351
3352     (when gnus-alter-header-function
3353       (funcall gnus-alter-header-function header))
3354     (gnus-dependencies-add-header header dependencies force-new)))
3355
3356 (defun gnus-build-get-header (id)
3357   "Look through the buffer of NOV lines and find the header to ID.
3358 Enter this line into the dependencies hash table, and return
3359 the id of the parent article (if any)."
3360   (let ((deps gnus-newsgroup-dependencies)
3361         found header)
3362     (prog1
3363         (save-excursion
3364           (set-buffer nntp-server-buffer)
3365           (let ((case-fold-search nil))
3366             (goto-char (point-min))
3367             (while (and (not found)
3368                         (search-forward id nil t))
3369               (beginning-of-line)
3370               (setq found (looking-at
3371                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3372                                    (regexp-quote id))))
3373               (or found (beginning-of-line 2)))
3374             (when found
3375               (beginning-of-line)
3376               (and
3377                (setq header (gnus-nov-parse-line
3378                              (read (current-buffer)) deps))
3379                (gnus-parent-id (mail-header-references header))))))
3380       (when header
3381         (let ((number (mail-header-number header)))
3382           (push number gnus-newsgroup-limit)
3383           (push header gnus-newsgroup-headers)
3384           (if (memq number gnus-newsgroup-unselected)
3385               (progn
3386                 (push number gnus-newsgroup-unreads)
3387                 (setq gnus-newsgroup-unselected
3388                       (delq number gnus-newsgroup-unselected)))
3389             (push number gnus-newsgroup-ancient)))))))
3390
3391 (defun gnus-build-all-threads ()
3392   "Read all the headers."
3393   (let ((gnus-summary-ignore-duplicates t)
3394         (mail-parse-charset gnus-newsgroup-charset)
3395         (dependencies gnus-newsgroup-dependencies)
3396         header article)
3397     (save-excursion
3398       (set-buffer nntp-server-buffer)
3399       (let ((case-fold-search nil))
3400         (goto-char (point-min))
3401         (while (not (eobp))
3402           (ignore-errors
3403             (setq article (read (current-buffer))
3404                   header (gnus-nov-parse-line article dependencies)))
3405           (when header
3406             (save-excursion
3407               (set-buffer gnus-summary-buffer)
3408               (push header gnus-newsgroup-headers)
3409               (if (memq (setq article (mail-header-number header))
3410                         gnus-newsgroup-unselected)
3411                   (progn
3412                     (push article gnus-newsgroup-unreads)
3413                     (setq gnus-newsgroup-unselected
3414                           (delq article gnus-newsgroup-unselected)))
3415                 (push article gnus-newsgroup-ancient)))
3416             (forward-line 1)))))))
3417
3418 (defun gnus-summary-update-article-line (article header)
3419   "Update the line for ARTICLE using HEADERS."
3420   (let* ((id (mail-header-id header))
3421          (thread (gnus-id-to-thread id)))
3422     (unless thread
3423       (error "Article in no thread"))
3424     ;; Update the thread.
3425     (setcar thread header)
3426     (gnus-summary-goto-subject article)
3427     (let* ((datal (gnus-data-find-list article))
3428            (data (car datal))
3429            (length (when (cdr datal)
3430                      (- (gnus-data-pos data)
3431                         (gnus-data-pos (cadr datal)))))
3432            (buffer-read-only nil)
3433            (level (gnus-summary-thread-level)))
3434       (gnus-delete-line)
3435       (gnus-summary-insert-line
3436        header level nil (gnus-article-mark article)
3437        (memq article gnus-newsgroup-replied)
3438        (memq article gnus-newsgroup-expirable)
3439        ;; Only insert the Subject string when it's different
3440        ;; from the previous Subject string.
3441        (if (and
3442             gnus-show-threads
3443             (gnus-subject-equal
3444              (condition-case ()
3445                  (mail-header-subject
3446                   (gnus-data-header
3447                    (cadr
3448                     (gnus-data-find-list
3449                      article
3450                      (gnus-data-list t)))))
3451                ;; Error on the side of excessive subjects.
3452                (error ""))
3453              (mail-header-subject header)))
3454            ""
3455          (mail-header-subject header))
3456        nil (cdr (assq article gnus-newsgroup-scored))
3457        (memq article gnus-newsgroup-processable))
3458       (when length
3459         (gnus-data-update-list
3460          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3461
3462 (defun gnus-summary-update-article (article &optional iheader)
3463   "Update ARTICLE in the summary buffer."
3464   (set-buffer gnus-summary-buffer)
3465   (let* ((header (gnus-summary-article-header article))
3466          (id (mail-header-id header))
3467          (data (gnus-data-find article))
3468          (thread (gnus-id-to-thread id))
3469          (references (mail-header-references header))
3470          (parent
3471           (gnus-id-to-thread
3472            (or (gnus-parent-id
3473                 (when (and references
3474                            (not (equal "" references)))
3475                   references))
3476                "none")))
3477          (buffer-read-only nil)
3478          (old (car thread)))
3479     (when thread
3480       (unless iheader
3481         (setcar thread nil)
3482         (when parent
3483           (delq thread parent)))
3484       (if (gnus-summary-insert-subject id header)
3485           ;; Set the (possibly) new article number in the data structure.
3486           (gnus-data-set-number data (gnus-id-to-article id))
3487         (setcar thread old)
3488         nil))))
3489
3490 (defun gnus-rebuild-thread (id &optional line)
3491   "Rebuild the thread containing ID.
3492 If LINE, insert the rebuilt thread starting on line LINE."
3493   (let ((buffer-read-only nil)
3494         old-pos current thread data)
3495     (if (not gnus-show-threads)
3496         (setq thread (list (car (gnus-id-to-thread id))))
3497       ;; Get the thread this article is part of.
3498       (setq thread (gnus-remove-thread id)))
3499     (setq old-pos (gnus-point-at-bol))
3500     (setq current (save-excursion
3501                     (and (zerop (forward-line -1))
3502                          (gnus-summary-article-number))))
3503     ;; If this is a gathered thread, we have to go some re-gathering.
3504     (when (stringp (car thread))
3505       (let ((subject (car thread))
3506             roots thr)
3507         (setq thread (cdr thread))
3508         (while thread
3509           (unless (memq (setq thr (gnus-id-to-thread
3510                                    (gnus-root-id
3511                                     (mail-header-id (caar thread)))))
3512                         roots)
3513             (push thr roots))
3514           (setq thread (cdr thread)))
3515         ;; We now have all (unique) roots.
3516         (if (= (length roots) 1)
3517             ;; All the loose roots are now one solid root.
3518             (setq thread (car roots))
3519           (setq thread (cons subject (gnus-sort-threads roots))))))
3520     (let (threads)
3521       ;; We then insert this thread into the summary buffer.
3522       (when line
3523         (goto-char (point-min))
3524         (forward-line (1- line)))
3525       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3526         (if gnus-show-threads
3527             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3528           (gnus-summary-prepare-unthreaded thread))
3529         (setq data (nreverse gnus-newsgroup-data))
3530         (setq threads gnus-newsgroup-threads))
3531       ;; We splice the new data into the data structure.
3532       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3533       ;;!!! then we want to insert at the beginning of the buffer.
3534       ;;!!! That happens to be true with Gnus now, but that may
3535       ;;!!! change in the future.  Perhaps.
3536       (gnus-data-enter-list
3537        (if line nil current) data (- (point) old-pos))
3538       (setq gnus-newsgroup-threads
3539             (nconc threads gnus-newsgroup-threads))
3540       (gnus-data-compute-positions))))
3541
3542 (defun gnus-number-to-header (number)
3543   "Return the header for article NUMBER."
3544   (let ((headers gnus-newsgroup-headers))
3545     (while (and headers
3546                 (not (= number (mail-header-number (car headers)))))
3547       (pop headers))
3548     (when headers
3549       (car headers))))
3550
3551 (defun gnus-parent-headers (in-headers &optional generation)
3552   "Return the headers of the GENERATIONeth parent of HEADERS."
3553   (unless generation
3554     (setq generation 1))
3555   (let ((parent t)
3556         (headers in-headers)
3557         references)
3558     (while (and parent
3559                 (not (zerop generation))
3560                 (setq references (mail-header-references headers)))
3561       (setq headers (if (and references
3562                              (setq parent (gnus-parent-id references)))
3563                         (car (gnus-id-to-thread parent))
3564                       nil))
3565       (decf generation))
3566     (and (not (eq headers in-headers))
3567          headers)))
3568
3569 (defun gnus-id-to-thread (id)
3570   "Return the (sub-)thread where ID appears."
3571   (gnus-gethash id gnus-newsgroup-dependencies))
3572
3573 (defun gnus-id-to-article (id)
3574   "Return the article number of ID."
3575   (let ((thread (gnus-id-to-thread id)))
3576     (when (and thread
3577                (car thread))
3578       (mail-header-number (car thread)))))
3579
3580 (defun gnus-id-to-header (id)
3581   "Return the article headers of ID."
3582   (car (gnus-id-to-thread id)))
3583
3584 (defun gnus-article-displayed-root-p (article)
3585   "Say whether ARTICLE is a root(ish) article."
3586   (let ((level (gnus-summary-thread-level article))
3587         (refs (mail-header-references  (gnus-summary-article-header article)))
3588         particle)
3589     (cond
3590      ((null level) nil)
3591      ((zerop level) t)
3592      ((null refs) t)
3593      ((null (gnus-parent-id refs)) t)
3594      ((and (= 1 level)
3595            (null (setq particle (gnus-id-to-article
3596                                  (gnus-parent-id refs))))
3597            (null (gnus-summary-thread-level particle)))))))
3598
3599 (defun gnus-root-id (id)
3600   "Return the id of the root of the thread where ID appears."
3601   (let (last-id prev)
3602     (while (and id (setq prev (car (gnus-id-to-thread id))))
3603       (setq last-id id
3604             id (gnus-parent-id (mail-header-references prev))))
3605     last-id))
3606
3607 (defun gnus-articles-in-thread (thread)
3608   "Return the list of articles in THREAD."
3609   (cons (mail-header-number (car thread))
3610         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3611
3612 (defun gnus-remove-thread (id &optional dont-remove)
3613   "Remove the thread that has ID in it."
3614   (let (headers thread last-id)
3615     ;; First go up in this thread until we find the root.
3616     (setq last-id (gnus-root-id id)
3617           headers (message-flatten-list (gnus-id-to-thread last-id)))
3618     ;; We have now found the real root of this thread.  It might have
3619     ;; been gathered into some loose thread, so we have to search
3620     ;; through the threads to find the thread we wanted.
3621     (let ((threads gnus-newsgroup-threads)
3622           sub)
3623       (while threads
3624         (setq sub (car threads))
3625         (if (stringp (car sub))
3626             ;; This is a gathered thread, so we look at the roots
3627             ;; below it to find whether this article is in this
3628             ;; gathered root.
3629             (progn
3630               (setq sub (cdr sub))
3631               (while sub
3632                 (when (member (caar sub) headers)
3633                   (setq thread (car threads)
3634                         threads nil
3635                         sub nil))
3636                 (setq sub (cdr sub))))
3637           ;; It's an ordinary thread, so we check it.
3638           (when (eq (car sub) (car headers))
3639             (setq thread sub
3640                   threads nil)))
3641         (setq threads (cdr threads)))
3642       ;; If this article is in no thread, then it's a root.
3643       (if thread
3644           (unless dont-remove
3645             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3646         (setq thread (gnus-id-to-thread last-id)))
3647       (when thread
3648         (prog1
3649             thread                      ; We return this thread.
3650           (unless dont-remove
3651             (if (stringp (car thread))
3652                 (progn
3653                   ;; If we use dummy roots, then we have to remove the
3654                   ;; dummy root as well.
3655                   (when (eq gnus-summary-make-false-root 'dummy)
3656                     ;; We go to the dummy root by going to
3657                     ;; the first sub-"thread", and then one line up.
3658                     (gnus-summary-goto-article
3659                      (mail-header-number (caadr thread)))
3660                     (forward-line -1)
3661                     (gnus-delete-line)
3662                     (gnus-data-compute-positions))
3663                   (setq thread (cdr thread))
3664                   (while thread
3665                     (gnus-remove-thread-1 (car thread))
3666                     (setq thread (cdr thread))))
3667               (gnus-remove-thread-1 thread))))))))
3668
3669 (defun gnus-remove-thread-1 (thread)
3670   "Remove the thread THREAD recursively."
3671   (let ((number (mail-header-number (pop thread)))
3672         d)
3673     (setq thread (reverse thread))
3674     (while thread
3675       (gnus-remove-thread-1 (pop thread)))
3676     (when (setq d (gnus-data-find number))
3677       (goto-char (gnus-data-pos d))
3678       (gnus-summary-show-thread)
3679       (gnus-data-remove
3680        number
3681        (- (gnus-point-at-bol)
3682           (prog1
3683               (1+ (gnus-point-at-eol))
3684             (gnus-delete-line)))))))
3685
3686 (defun gnus-sort-threads-1 (threads func)
3687   (sort (mapcar (lambda (thread)
3688                   (cons (car thread)
3689                         (and (cdr thread)
3690                              (gnus-sort-threads-1 (cdr thread) func))))
3691                 threads) func))
3692
3693 (defun gnus-sort-threads (threads)
3694   "Sort THREADS."
3695   (if (not gnus-thread-sort-functions)
3696       threads
3697     (gnus-message 8 "Sorting threads...")
3698     (prog1
3699         (gnus-sort-threads-1 
3700          threads 
3701          (gnus-make-sort-function gnus-thread-sort-functions))
3702       (gnus-message 8 "Sorting threads...done"))))
3703
3704 (defun gnus-sort-articles (articles)
3705   "Sort ARTICLES."
3706   (when gnus-article-sort-functions
3707     (gnus-message 7 "Sorting articles...")
3708     (prog1
3709         (setq gnus-newsgroup-headers
3710               (sort articles (gnus-make-sort-function
3711                               gnus-article-sort-functions)))
3712       (gnus-message 7 "Sorting articles...done"))))
3713
3714 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3715 (defmacro gnus-thread-header (thread)
3716   "Return header of first article in THREAD.
3717 Note that THREAD must never, ever be anything else than a variable -
3718 using some other form will lead to serious barfage."
3719   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3720   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3721   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3722         (vector thread) 2))
3723
3724 (defsubst gnus-article-sort-by-number (h1 h2)
3725   "Sort articles by article number."
3726   (< (mail-header-number h1)
3727      (mail-header-number h2)))
3728
3729 (defun gnus-thread-sort-by-number (h1 h2)
3730   "Sort threads by root article number."
3731   (gnus-article-sort-by-number
3732    (gnus-thread-header h1) (gnus-thread-header h2)))
3733
3734 (defsubst gnus-article-sort-by-lines (h1 h2)
3735   "Sort articles by article Lines header."
3736   (< (mail-header-lines h1)
3737      (mail-header-lines h2)))
3738
3739 (defun gnus-thread-sort-by-lines (h1 h2)
3740   "Sort threads by root article Lines header."
3741   (gnus-article-sort-by-lines
3742    (gnus-thread-header h1) (gnus-thread-header h2)))
3743
3744 (defsubst gnus-article-sort-by-chars (h1 h2)
3745   "Sort articles by octet length."
3746   (< (mail-header-chars h1)
3747      (mail-header-chars h2)))
3748
3749 (defun gnus-thread-sort-by-chars (h1 h2)
3750   "Sort threads by root article octet length."
3751   (gnus-article-sort-by-chars
3752    (gnus-thread-header h1) (gnus-thread-header h2)))
3753
3754 (defsubst gnus-article-sort-by-author (h1 h2)
3755   "Sort articles by root author."
3756   (string-lessp
3757    (let ((addr (car (mime-entity-read-field h1 'From))))
3758      (or (std11-full-name-string addr)
3759          (std11-address-string addr)
3760          ""))
3761    (let ((addr (car (mime-entity-read-field h2 'From))))
3762      (or (std11-full-name-string addr)
3763          (std11-address-string addr)
3764          ""))
3765    ))
3766
3767 (defun gnus-thread-sort-by-author (h1 h2)
3768   "Sort threads by root author."
3769   (gnus-article-sort-by-author
3770    (gnus-thread-header h1)  (gnus-thread-header h2)))
3771
3772 (defsubst gnus-article-sort-by-subject (h1 h2)
3773   "Sort articles by root subject."
3774   (string-lessp
3775    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3776    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3777
3778 (defun gnus-thread-sort-by-subject (h1 h2)
3779   "Sort threads by root subject."
3780   (gnus-article-sort-by-subject
3781    (gnus-thread-header h1) (gnus-thread-header h2)))
3782
3783 (defsubst gnus-article-sort-by-date (h1 h2)
3784   "Sort articles by root article date."
3785   (time-less-p
3786    (gnus-date-get-time (mail-header-date h1))
3787    (gnus-date-get-time (mail-header-date h2))))
3788
3789 (defun gnus-thread-sort-by-date (h1 h2)
3790   "Sort threads by root article date."
3791   (gnus-article-sort-by-date
3792    (gnus-thread-header h1) (gnus-thread-header h2)))
3793
3794 (defsubst gnus-article-sort-by-score (h1 h2)
3795   "Sort articles by root article score.
3796 Unscored articles will be counted as having a score of zero."
3797   (> (or (cdr (assq (mail-header-number h1)
3798                     gnus-newsgroup-scored))
3799          gnus-summary-default-score 0)
3800      (or (cdr (assq (mail-header-number h2)
3801                     gnus-newsgroup-scored))
3802          gnus-summary-default-score 0)))
3803
3804 (defun gnus-thread-sort-by-score (h1 h2)
3805   "Sort threads by root article score."
3806   (gnus-article-sort-by-score
3807    (gnus-thread-header h1) (gnus-thread-header h2)))
3808
3809 (defun gnus-thread-sort-by-total-score (h1 h2)
3810   "Sort threads by the sum of all scores in the thread.
3811 Unscored articles will be counted as having a score of zero."
3812   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3813
3814 (defun gnus-thread-total-score (thread)
3815   ;; This function find the total score of THREAD.
3816   (cond ((null thread)
3817          0)
3818         ((consp thread)
3819          (if (stringp (car thread))
3820              (apply gnus-thread-score-function 0
3821                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3822            (gnus-thread-total-score-1 thread)))
3823         (t
3824          (gnus-thread-total-score-1 (list thread)))))
3825
3826 (defun gnus-thread-total-score-1 (root)
3827   ;; This function find the total score of the thread below ROOT.
3828   (setq root (car root))
3829   (apply gnus-thread-score-function
3830          (or (append
3831               (mapcar 'gnus-thread-total-score
3832                       (cdr (gnus-id-to-thread (mail-header-id root))))
3833               (when (> (mail-header-number root) 0)
3834                 (list (or (cdr (assq (mail-header-number root)
3835                                      gnus-newsgroup-scored))
3836                           gnus-summary-default-score 0))))
3837              (list gnus-summary-default-score)
3838              '(0))))
3839
3840 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3841 (defvar gnus-tmp-prev-subject nil)
3842 (defvar gnus-tmp-false-parent nil)
3843 (defvar gnus-tmp-root-expunged nil)
3844 (defvar gnus-tmp-dummy-line nil)
3845
3846 (defvar gnus-tmp-header)
3847 (defun gnus-extra-header (type &optional header)
3848   "Return the extra header of TYPE."
3849   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3850       ""))
3851
3852 (defun gnus-summary-prepare-threads (threads)
3853   "Prepare summary buffer from THREADS and indentation LEVEL.
3854 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3855 or a straight list of headers."
3856   (gnus-message 7 "Generating summary...")
3857
3858   (setq gnus-newsgroup-threads threads)
3859   (beginning-of-line)
3860
3861   (let ((gnus-tmp-level 0)
3862         (default-score (or gnus-summary-default-score 0))
3863         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3864         thread number subject stack state gnus-tmp-gathered beg-match
3865         new-roots gnus-tmp-new-adopts thread-end
3866         gnus-tmp-header gnus-tmp-unread
3867         gnus-tmp-replied gnus-tmp-subject-or-nil
3868         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3869         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3870         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3871
3872     (setq gnus-tmp-prev-subject nil)
3873
3874     (if (vectorp (car threads))
3875         ;; If this is a straight (sic) list of headers, then a
3876         ;; threaded summary display isn't required, so we just create
3877         ;; an unthreaded one.
3878         (gnus-summary-prepare-unthreaded threads)
3879
3880       ;; Do the threaded display.
3881
3882       (while (or threads stack gnus-tmp-new-adopts new-roots)
3883
3884         (if (and (= gnus-tmp-level 0)
3885                  (or (not stack)
3886                      (= (caar stack) 0))
3887                  (not gnus-tmp-false-parent)
3888                  (or gnus-tmp-new-adopts new-roots))
3889             (if gnus-tmp-new-adopts
3890                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3891                       thread (list (car gnus-tmp-new-adopts))
3892                       gnus-tmp-header (caar thread)
3893                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3894               (when new-roots
3895                 (setq thread (list (car new-roots))
3896                       gnus-tmp-header (caar thread)
3897                       new-roots (cdr new-roots))))
3898
3899           (if threads
3900               ;; If there are some threads, we do them before the
3901               ;; threads on the stack.
3902               (setq thread threads
3903                     gnus-tmp-header (caar thread))
3904             ;; There were no current threads, so we pop something off
3905             ;; the stack.
3906             (setq state (car stack)
3907                   gnus-tmp-level (car state)
3908                   thread (cdr state)
3909                   stack (cdr stack)
3910                   gnus-tmp-header (caar thread))))
3911
3912         (setq gnus-tmp-false-parent nil)
3913         (setq gnus-tmp-root-expunged nil)
3914         (setq thread-end nil)
3915
3916         (if (stringp gnus-tmp-header)
3917             ;; The header is a dummy root.
3918             (cond
3919              ((eq gnus-summary-make-false-root 'adopt)
3920               ;; We let the first article adopt the rest.
3921               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3922                                                (cddar thread)))
3923               (setq gnus-tmp-gathered
3924                     (nconc (mapcar
3925                             (lambda (h) (mail-header-number (car h)))
3926                             (cddar thread))
3927                            gnus-tmp-gathered))
3928               (setq thread (cons (list (caar thread)
3929                                        (cadar thread))
3930                                  (cdr thread)))
3931               (setq gnus-tmp-level -1
3932                     gnus-tmp-false-parent t))
3933              ((eq gnus-summary-make-false-root 'empty)
3934               ;; We print adopted articles with empty subject fields.
3935               (setq gnus-tmp-gathered
3936                     (nconc (mapcar
3937                             (lambda (h) (mail-header-number (car h)))
3938                             (cddar thread))
3939                            gnus-tmp-gathered))
3940               (setq gnus-tmp-level -1))
3941              ((eq gnus-summary-make-false-root 'dummy)
3942               ;; We remember that we probably want to output a dummy
3943               ;; root.
3944               (setq gnus-tmp-dummy-line gnus-tmp-header)
3945               (setq gnus-tmp-prev-subject gnus-tmp-header))
3946              (t
3947               ;; We do not make a root for the gathered
3948               ;; sub-threads at all.
3949               (setq gnus-tmp-level -1)))
3950
3951           (setq number (mail-header-number gnus-tmp-header)
3952                 subject (mail-header-subject gnus-tmp-header))
3953
3954           (cond
3955            ;; If the thread has changed subject, we might want to make
3956            ;; this subthread into a root.
3957            ((and (null gnus-thread-ignore-subject)
3958                  (not (zerop gnus-tmp-level))
3959                  gnus-tmp-prev-subject
3960                  (not (inline
3961                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3962             (setq new-roots (nconc new-roots (list (car thread)))
3963                   thread-end t
3964                   gnus-tmp-header nil))
3965            ;; If the article lies outside the current limit,
3966            ;; then we do not display it.
3967            ((not (memq number gnus-newsgroup-limit))
3968             (setq gnus-tmp-gathered
3969                   (nconc (mapcar
3970                           (lambda (h) (mail-header-number (car h)))
3971                           (cdar thread))
3972                          gnus-tmp-gathered))
3973             (setq gnus-tmp-new-adopts (if (cdar thread)
3974                                           (append gnus-tmp-new-adopts
3975                                                   (cdar thread))
3976                                         gnus-tmp-new-adopts)
3977                   thread-end t
3978                   gnus-tmp-header nil)
3979             (when (zerop gnus-tmp-level)
3980               (setq gnus-tmp-root-expunged t)))
3981            ;; Perhaps this article is to be marked as read?
3982            ((and gnus-summary-mark-below
3983                  (< (or (cdr (assq number gnus-newsgroup-scored))
3984                         default-score)
3985                     gnus-summary-mark-below)
3986                  ;; Don't touch sparse articles.
3987                  (not (gnus-summary-article-sparse-p number))
3988                  (not (gnus-summary-article-ancient-p number)))
3989             (setq gnus-newsgroup-unreads
3990                   (delq number gnus-newsgroup-unreads))
3991             (if gnus-newsgroup-auto-expire
3992                 (push number gnus-newsgroup-expirable)
3993               (push (cons number gnus-low-score-mark)
3994                     gnus-newsgroup-reads))))
3995
3996           (when gnus-tmp-header
3997             ;; We may have an old dummy line to output before this
3998             ;; article.
3999             (when (and gnus-tmp-dummy-line
4000                        (gnus-subject-equal
4001                         gnus-tmp-dummy-line
4002                         (mail-header-subject gnus-tmp-header)))
4003               (gnus-summary-insert-dummy-line
4004                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4005               (setq gnus-tmp-dummy-line nil))
4006
4007             ;; Compute the mark.
4008             (setq gnus-tmp-unread (gnus-article-mark number))
4009
4010             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4011                                   gnus-tmp-header gnus-tmp-level)
4012                   gnus-newsgroup-data)
4013
4014             ;; Actually insert the line.
4015             (setq
4016              gnus-tmp-subject-or-nil
4017              (cond
4018               ((and gnus-thread-ignore-subject
4019                     gnus-tmp-prev-subject
4020                     (not (inline (gnus-subject-equal
4021                                   gnus-tmp-prev-subject subject))))
4022                subject)
4023               ((zerop gnus-tmp-level)
4024                (if (and (eq gnus-summary-make-false-root 'empty)
4025                         (memq number gnus-tmp-gathered)
4026                         gnus-tmp-prev-subject
4027                         (inline (gnus-subject-equal
4028                                  gnus-tmp-prev-subject subject)))
4029                    gnus-summary-same-subject
4030                  subject))
4031               (t gnus-summary-same-subject)))
4032             (if (and (eq gnus-summary-make-false-root 'adopt)
4033                      (= gnus-tmp-level 1)
4034                      (memq number gnus-tmp-gathered))
4035                 (setq gnus-tmp-opening-bracket ?\<
4036                       gnus-tmp-closing-bracket ?\>)
4037               (setq gnus-tmp-opening-bracket ?\[
4038                     gnus-tmp-closing-bracket ?\]))
4039             (setq
4040              gnus-tmp-indentation
4041              (aref gnus-thread-indent-array gnus-tmp-level)
4042              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4043              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4044                                 gnus-summary-default-score 0)
4045              gnus-tmp-score-char
4046              (if (or (null gnus-summary-default-score)
4047                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4048                          gnus-summary-zcore-fuzz))
4049                  ?  ;Whitespace
4050                (if (< gnus-tmp-score gnus-summary-default-score)
4051                    gnus-score-below-mark gnus-score-over-mark))
4052              gnus-tmp-replied
4053              (cond ((memq number gnus-newsgroup-processable)
4054                     gnus-process-mark)
4055                    ((memq number gnus-newsgroup-cached)
4056                     gnus-cached-mark)
4057                    ((memq number gnus-newsgroup-replied)
4058                     gnus-replied-mark)
4059                    ((memq number gnus-newsgroup-saved)
4060                     gnus-saved-mark)
4061                    (t gnus-unread-mark))
4062              gnus-tmp-from (mail-header-from gnus-tmp-header)
4063              gnus-tmp-name
4064              (cond
4065               ((string-match "<[^>]+> *$" gnus-tmp-from)
4066                (setq beg-match (match-beginning 0))
4067                (or (and (string-match "^\".+\"" gnus-tmp-from)
4068                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4069                    (substring gnus-tmp-from 0 beg-match)))
4070               ((string-match "(.+)" gnus-tmp-from)
4071                (substring gnus-tmp-from
4072                           (1+ (match-beginning 0)) (1- (match-end 0))))
4073               (t gnus-tmp-from)))
4074             (when (string= gnus-tmp-name "")
4075               (setq gnus-tmp-name gnus-tmp-from))
4076             (unless (numberp gnus-tmp-lines)
4077               (setq gnus-tmp-lines 0))
4078             (gnus-put-text-property
4079              (point)
4080              (progn (eval gnus-summary-line-format-spec) (point))
4081              'gnus-number number)
4082             (when gnus-visual-p
4083               (forward-line -1)
4084               (gnus-run-hooks 'gnus-summary-update-hook)
4085               (forward-line 1))
4086
4087             (setq gnus-tmp-prev-subject subject)))
4088
4089         (when (nth 1 thread)
4090           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4091         (incf gnus-tmp-level)
4092         (setq threads (if thread-end nil (cdar thread)))
4093         (unless threads
4094           (setq gnus-tmp-level 0)))))
4095   (gnus-message 7 "Generating summary...done"))
4096
4097 (defun gnus-summary-prepare-unthreaded (headers)
4098   "Generate an unthreaded summary buffer based on HEADERS."
4099   (let (header number mark)
4100
4101     (beginning-of-line)
4102
4103     (while headers
4104       ;; We may have to root out some bad articles...
4105       (when (memq (setq number (mail-header-number
4106                                 (setq header (pop headers))))
4107                   gnus-newsgroup-limit)
4108         ;; Mark article as read when it has a low score.
4109         (when (and gnus-summary-mark-below
4110                    (< (or (cdr (assq number gnus-newsgroup-scored))
4111                           gnus-summary-default-score 0)
4112                       gnus-summary-mark-below)
4113                    (not (gnus-summary-article-ancient-p number)))
4114           (setq gnus-newsgroup-unreads
4115                 (delq number gnus-newsgroup-unreads))
4116           (if gnus-newsgroup-auto-expire
4117               (push number gnus-newsgroup-expirable)
4118             (push (cons number gnus-low-score-mark)
4119                   gnus-newsgroup-reads)))
4120
4121         (setq mark (gnus-article-mark number))
4122         (push (gnus-data-make number mark (1+ (point)) header 0)
4123               gnus-newsgroup-data)
4124         (gnus-summary-insert-line
4125          header 0 number
4126          mark (memq number gnus-newsgroup-replied)
4127          (memq number gnus-newsgroup-expirable)
4128          (mail-header-subject header) nil
4129          (cdr (assq number gnus-newsgroup-scored))
4130          (memq number gnus-newsgroup-processable))))))
4131
4132 (defun gnus-summary-remove-list-identifiers ()
4133   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4134   (let ((regexp (if (stringp gnus-list-identifiers)
4135                     gnus-list-identifiers
4136                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4137     (dolist (header gnus-newsgroup-headers)
4138       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4139                                   " *\\)\\)+\\(Re: +\\)?\\)")
4140                           (mail-header-subject header))
4141         (mail-header-set-subject
4142          header (concat (substring (mail-header-subject header)
4143                                    0 (match-beginning 1))
4144                         (or
4145                          (match-string 3 (mail-header-subject header))
4146                          (match-string 5 (mail-header-subject header)))
4147                         (substring (mail-header-subject header)
4148                                    (match-end 1))))))))
4149
4150 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4151   "Select newsgroup GROUP.
4152 If READ-ALL is non-nil, all articles in the group are selected.
4153 If SELECT-ARTICLES, only select those articles from GROUP."
4154   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4155          ;;!!! Dirty hack; should be removed.
4156          (gnus-summary-ignore-duplicates
4157           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4158               t
4159             gnus-summary-ignore-duplicates))
4160          (info (nth 2 entry))
4161          articles fetched-articles cached)
4162
4163     (unless (gnus-check-server
4164              (setq gnus-current-select-method
4165                    (gnus-find-method-for-group group)))
4166       (error "Couldn't open server"))
4167
4168     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4169         (gnus-activate-group group)     ; Or we can activate it...
4170         (progn                          ; Or we bug out.
4171           (when (equal major-mode 'gnus-summary-mode)
4172             (kill-buffer (current-buffer)))
4173           (error "Couldn't request group %s: %s"
4174                  group (gnus-status-message group))))
4175
4176     (unless (gnus-request-group group t)
4177       (when (equal major-mode 'gnus-summary-mode)
4178         (kill-buffer (current-buffer)))
4179       (error "Couldn't request group %s: %s"
4180              group (gnus-status-message group)))
4181
4182     (setq gnus-newsgroup-name group)
4183     (setq gnus-newsgroup-unselected nil)
4184     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4185     (gnus-summary-setup-default-charset)
4186
4187     ;; Adjust and set lists of article marks.
4188     (when info
4189       (gnus-adjust-marked-articles info))
4190
4191     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4192     (setq cached
4193           (if (gnus-virtual-group-p group)
4194               gnus-newsgroup-cached
4195             (gnus-cache-articles-in-group group)))
4196
4197     (setq gnus-newsgroup-unreads
4198           (gnus-set-difference
4199            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4200            gnus-newsgroup-dormant))
4201
4202     (setq gnus-newsgroup-processable nil)
4203
4204     (gnus-update-read-articles group gnus-newsgroup-unreads)
4205
4206     (if (setq articles select-articles)
4207         (setq gnus-newsgroup-unselected
4208               (gnus-sorted-intersection
4209                gnus-newsgroup-unreads
4210                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4211       (setq articles (gnus-articles-to-read group read-all)))
4212
4213     (cond
4214      ((null articles)
4215       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4216       'quit)
4217      ((eq articles 0) nil)
4218      (t
4219       ;; Init the dependencies hash table.
4220       (setq gnus-newsgroup-dependencies
4221             (gnus-make-hashtable (length articles)))
4222       (gnus-set-global-variables)
4223       ;; Retrieve the headers and read them in.
4224       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4225       (setq gnus-newsgroup-headers
4226             (gnus-retrieve-parsed-headers
4227              articles gnus-newsgroup-name
4228              ;; We might want to fetch old headers, but
4229              ;; not if there is only 1 article.
4230              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4231                            (not (numberp gnus-fetch-old-headers)))
4232                       (> (length articles) 1))
4233                   gnus-fetch-old-headers)))
4234       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4235
4236       ;; Suppress duplicates?
4237       (when gnus-suppress-duplicates
4238         (gnus-dup-suppress-articles))
4239
4240       ;; Set the initial limit.
4241       (setq gnus-newsgroup-limit (copy-sequence articles))
4242       ;; Remove canceled articles from the list of unread articles.
4243       (setq gnus-newsgroup-unreads
4244             (gnus-set-sorted-intersection
4245              gnus-newsgroup-unreads
4246              (setq fetched-articles
4247                    (mapcar (lambda (headers) (mail-header-number headers))
4248                            gnus-newsgroup-headers))))
4249       ;; Removed marked articles that do not exist.
4250       (gnus-update-missing-marks
4251        (gnus-sorted-complement fetched-articles articles))
4252
4253       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4254       (when cached
4255         (setq gnus-newsgroup-cached cached))
4256
4257       ;; We might want to build some more threads first.
4258       (when (and gnus-fetch-old-headers
4259                  (eq gnus-headers-retrieved-by 'nov))
4260         (if (eq gnus-fetch-old-headers 'invisible)
4261             (gnus-build-all-threads)
4262           (gnus-build-old-threads)))
4263       ;; Let the Gnus agent mark articles as read.
4264       (when gnus-agent
4265         (gnus-agent-get-undownloaded-list))
4266       ;; Remove list identifiers from subject
4267       (when gnus-list-identifiers
4268         (gnus-summary-remove-list-identifiers))
4269       ;; Check whether auto-expire is to be done in this group.
4270       (setq gnus-newsgroup-auto-expire
4271             (gnus-group-auto-expirable-p group))
4272       ;; Set up the article buffer now, if necessary.
4273       (unless gnus-single-article-buffer
4274         (gnus-article-setup-buffer))
4275       ;; First and last article in this newsgroup.
4276       (when gnus-newsgroup-headers
4277         (setq gnus-newsgroup-begin
4278               (mail-header-number (car gnus-newsgroup-headers))
4279               gnus-newsgroup-end
4280               (mail-header-number
4281                (gnus-last-element gnus-newsgroup-headers))))
4282       ;; GROUP is successfully selected.
4283       (or gnus-newsgroup-headers t)))))
4284
4285 (defun gnus-articles-to-read (group &optional read-all)
4286   "Find out what articles the user wants to read."
4287   (let* ((articles
4288           ;; Select all articles if `read-all' is non-nil, or if there
4289           ;; are no unread articles.
4290           (if (or read-all
4291                   (and (zerop (length gnus-newsgroup-marked))
4292                        (zerop (length gnus-newsgroup-unreads)))
4293                   (eq (gnus-group-find-parameter group 'display)
4294                       'all))
4295               (or
4296                (gnus-uncompress-range (gnus-active group))
4297                (gnus-cache-articles-in-group group))
4298             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4299                           (copy-sequence gnus-newsgroup-unreads))
4300                   '<)))
4301          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4302          (scored (length scored-list))
4303          (number (length articles))
4304          (marked (+ (length gnus-newsgroup-marked)
4305                     (length gnus-newsgroup-dormant)))
4306          (select
4307           (cond
4308            ((numberp read-all)
4309             read-all)
4310            (t
4311             (condition-case ()
4312                 (cond
4313                  ((and (or (<= scored marked) (= scored number))
4314                        (natnump gnus-large-newsgroup)
4315                        (> number gnus-large-newsgroup))
4316                   (let* ((cursor-in-echo-area nil)
4317                          (input (read-from-minibuffer
4318                                  (format
4319                                   "How many articles from %s (max %d): "
4320                                   (gnus-limit-string gnus-newsgroup-name 35)
4321                                   number)
4322                                  (cons (number-to-string gnus-large-newsgroup)
4323                                        0))))
4324                     (if (string-match "^[ \t]*$" input)
4325                         number
4326                       input)))
4327                  ((and (> scored marked) (< scored number)
4328                        (> (- scored number) 20))
4329                   (let ((input
4330                          (read-string
4331                           (format "%s %s (%d scored, %d total): "
4332                                   "How many articles from"
4333                                   group scored number))))
4334                     (if (string-match "^[ \t]*$" input)
4335                         number input)))
4336                  (t number))
4337               (quit
4338                (message "Quit getting the articles to read")
4339                nil))))))
4340     (setq select (if (stringp select) (string-to-number select) select))
4341     (if (or (null select) (zerop select))
4342         select
4343       (if (and (not (zerop scored)) (<= (abs select) scored))
4344           (progn
4345             (setq articles (sort scored-list '<))
4346             (setq number (length articles)))
4347         (setq articles (copy-sequence articles)))
4348
4349       (when (< (abs select) number)
4350         (if (< select 0)
4351             ;; Select the N oldest articles.
4352             (setcdr (nthcdr (1- (abs select)) articles) nil)
4353           ;; Select the N most recent articles.
4354           (setq articles (nthcdr (- number select) articles))))
4355       (setq gnus-newsgroup-unselected
4356             (gnus-sorted-intersection
4357              gnus-newsgroup-unreads
4358              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4359       (when gnus-alter-articles-to-read-function
4360         (setq gnus-newsgroup-unreads
4361               (sort
4362                (funcall gnus-alter-articles-to-read-function
4363                         gnus-newsgroup-name gnus-newsgroup-unreads)
4364                '<)))
4365       articles)))
4366
4367 (defun gnus-killed-articles (killed articles)
4368   (let (out)
4369     (while articles
4370       (when (inline (gnus-member-of-range (car articles) killed))
4371         (push (car articles) out))
4372       (setq articles (cdr articles)))
4373     out))
4374
4375 (defun gnus-uncompress-marks (marks)
4376   "Uncompress the mark ranges in MARKS."
4377   (let ((uncompressed '(score bookmark))
4378         out)
4379     (while marks
4380       (if (memq (caar marks) uncompressed)
4381           (push (car marks) out)
4382         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4383       (setq marks (cdr marks)))
4384     out))
4385
4386 (defun gnus-adjust-marked-articles (info)
4387   "Set all article lists and remove all marks that are no longer valid."
4388   (let* ((marked-lists (gnus-info-marks info))
4389          (active (gnus-active (gnus-info-group info)))
4390          (min (car active))
4391          (max (cdr active))
4392          (types gnus-article-mark-lists)
4393          (uncompressed '(score bookmark killed))
4394          marks var articles article mark)
4395
4396     (while marked-lists
4397       (setq marks (pop marked-lists))
4398       (set (setq var (intern (format "gnus-newsgroup-%s"
4399                                      (car (rassq (setq mark (car marks))
4400                                                  types)))))
4401            (if (memq (car marks) uncompressed) (cdr marks)
4402              (gnus-uncompress-range (cdr marks))))
4403
4404       (setq articles (symbol-value var))
4405
4406       ;; All articles have to be subsets of the active articles.
4407       (cond
4408        ;; Adjust "simple" lists.
4409        ((memq mark '(tick dormant expire reply save))
4410         (while articles
4411           (when (or (< (setq article (pop articles)) min) (> article max))
4412             (set var (delq article (symbol-value var))))))
4413        ;; Adjust assocs.
4414        ((memq mark uncompressed)
4415         (when (not (listp (cdr (symbol-value var))))
4416           (set var (list (symbol-value var))))
4417         (when (not (listp (cdr articles)))
4418           (setq articles (list articles)))
4419         (while articles
4420           (when (or (not (consp (setq article (pop articles))))
4421                     (< (car article) min)
4422                     (> (car article) max))
4423             (set var (delq article (symbol-value var))))))))))
4424
4425 (defun gnus-update-missing-marks (missing)
4426   "Go through the list of MISSING articles and remove them from the mark lists."
4427   (when missing
4428     (let ((types gnus-article-mark-lists)
4429           var m)
4430       ;; Go through all types.
4431       (while types
4432         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4433         (when (symbol-value var)
4434           ;; This list has articles.  So we delete all missing articles
4435           ;; from it.
4436           (setq m missing)
4437           (while m
4438             (set var (delq (pop m) (symbol-value var)))))))))
4439
4440 (defun gnus-update-marks ()
4441   "Enter the various lists of marked articles into the newsgroup info list."
4442   (let ((types gnus-article-mark-lists)
4443         (info (gnus-get-info gnus-newsgroup-name))
4444         (uncompressed '(score bookmark killed))
4445         type list newmarked symbol delta-marks)
4446     (when info
4447       ;; Add all marks lists to the list of marks lists.
4448       (while (setq type (pop types))
4449         (setq list (symbol-value
4450                     (setq symbol
4451                           (intern (format "gnus-newsgroup-%s"
4452                                           (car type))))))
4453
4454         (when list
4455           ;; Get rid of the entries of the articles that have the
4456           ;; default score.
4457           (when (and (eq (cdr type) 'score)
4458                      gnus-save-score
4459                      list)
4460             (let* ((arts list)
4461                    (prev (cons nil list))
4462                    (all prev))
4463               (while arts
4464                 (if (or (not (consp (car arts)))
4465                         (= (cdar arts) gnus-summary-default-score))
4466                     (setcdr prev (cdr arts))
4467                   (setq prev arts))
4468                 (setq arts (cdr arts)))
4469               (setq list (cdr all)))))
4470
4471         (unless (memq (cdr type) uncompressed)
4472           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4473
4474         (when (gnus-check-backend-function
4475                'request-set-mark gnus-newsgroup-name)
4476           ;; propagate flags to server, with the following exceptions:
4477           ;; uncompressed:s are not proper flags (they are cons cells)
4478           ;; cache is a internal gnus flag
4479           ;; download are local to one gnus installation (well)
4480           ;; unsend are for nndraft groups only
4481           ;; xxx: generality of this?  this suits nnimap anyway
4482           (unless (memq (cdr type) (append '(cache download unsend)
4483                                            uncompressed))
4484             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4485                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4486                    (add (gnus-remove-from-range
4487                          (gnus-copy-sequence list) old)))
4488               (when add
4489                 (push (list add 'add (list (cdr type))) delta-marks))
4490               (when del
4491                 (push (list del 'del (list (cdr type))) delta-marks)))))
4492
4493         (when list
4494           (push (cons (cdr type) list) newmarked)))
4495
4496       (when delta-marks
4497         (unless (gnus-check-group gnus-newsgroup-name)
4498           (error "Can't open server for %s" gnus-newsgroup-name))
4499         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4500
4501       ;; Enter these new marks into the info of the group.
4502       (if (nthcdr 3 info)
4503           (setcar (nthcdr 3 info) newmarked)
4504         ;; Add the marks lists to the end of the info.
4505         (when newmarked
4506           (setcdr (nthcdr 2 info) (list newmarked))))
4507
4508       ;; Cut off the end of the info if there's nothing else there.
4509       (let ((i 5))
4510         (while (and (> i 2)
4511                     (not (nth i info)))
4512           (when (nthcdr (decf i) info)
4513             (setcdr (nthcdr i info) nil)))))))
4514
4515 (defun gnus-set-mode-line (where)
4516   "Set the mode line of the article or summary buffers.
4517 If WHERE is `summary', the summary mode line format will be used."
4518   ;; Is this mode line one we keep updated?
4519   (when (and (memq where gnus-updated-mode-lines)
4520              (symbol-value
4521               (intern (format "gnus-%s-mode-line-format-spec" where))))
4522     (let (mode-string)
4523       (save-excursion
4524         ;; We evaluate this in the summary buffer since these
4525         ;; variables are buffer-local to that buffer.
4526         (set-buffer gnus-summary-buffer)
4527         ;; We bind all these variables that are used in the `eval' form
4528         ;; below.
4529         (let* ((mformat (symbol-value
4530                          (intern
4531                           (format "gnus-%s-mode-line-format-spec" where))))
4532                (gnus-tmp-group-name (gnus-group-name-decode
4533                                      gnus-newsgroup-name
4534                                      (gnus-group-name-charset
4535                                       nil
4536                                       gnus-newsgroup-name)))
4537                (gnus-tmp-article-number (or gnus-current-article 0))
4538                (gnus-tmp-unread gnus-newsgroup-unreads)
4539                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4540                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4541                (gnus-tmp-unread-and-unselected
4542                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4543                             (zerop gnus-tmp-unselected))
4544                        "")
4545                       ((zerop gnus-tmp-unselected)
4546                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4547                       (t (format "{%d(+%d) more}"
4548                                  gnus-tmp-unread-and-unticked
4549                                  gnus-tmp-unselected))))
4550                (gnus-tmp-subject
4551                 (if (and gnus-current-headers
4552                          (vectorp gnus-current-headers))
4553                     (gnus-mode-string-quote
4554                      (mail-header-subject gnus-current-headers))
4555                   ""))
4556                bufname-length max-len
4557                gnus-tmp-header);; passed as argument to any user-format-funcs
4558           (setq mode-string (eval mformat))
4559           (setq bufname-length (if (string-match "%b" mode-string)
4560                                    (- (length
4561                                        (buffer-name
4562                                         (if (eq where 'summary)
4563                                             nil
4564                                           (get-buffer gnus-article-buffer))))
4565                                       2)
4566                                  0))
4567           (setq max-len (max 4 (if gnus-mode-non-string-length
4568                                    (- (window-width)
4569                                       gnus-mode-non-string-length
4570                                       bufname-length)
4571                                  (length mode-string))))
4572           ;; We might have to chop a bit of the string off...
4573           (when (> (length mode-string) max-len)
4574             (setq mode-string
4575                   (concat (gnus-truncate-string mode-string (- max-len 3))
4576                           "...")))
4577           ;; Pad the mode string a bit.
4578           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4579       ;; Update the mode line.
4580       (setq mode-line-buffer-identification
4581             (gnus-mode-line-buffer-identification (list mode-string)))
4582       (set-buffer-modified-p t))))
4583
4584 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4585   "Go through the HEADERS list and add all Xrefs to a hash table.
4586 The resulting hash table is returned, or nil if no Xrefs were found."
4587   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4588          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4589          (xref-hashtb (gnus-make-hashtable))
4590          start group entry number xrefs header)
4591     (while headers
4592       (setq header (pop headers))
4593       (when (and (setq xrefs (mail-header-xref header))
4594                  (not (memq (setq number (mail-header-number header))
4595                             unreads)))
4596         (setq start 0)
4597         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4598           (setq start (match-end 0))
4599           (setq group (if prefix
4600                           (concat prefix (substring xrefs (match-beginning 1)
4601                                                     (match-end 1)))
4602                         (substring xrefs (match-beginning 1) (match-end 1))))
4603           (setq number
4604                 (string-to-int (substring xrefs (match-beginning 2)
4605                                           (match-end 2))))
4606           (if (setq entry (gnus-gethash group xref-hashtb))
4607               (setcdr entry (cons number (cdr entry)))
4608             (gnus-sethash group (cons number nil) xref-hashtb)))))
4609     (and start xref-hashtb)))
4610
4611 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4612   "Look through all the headers and mark the Xrefs as read."
4613   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4614         name entry info xref-hashtb idlist method nth4)
4615     (save-excursion
4616       (set-buffer gnus-group-buffer)
4617       (when (setq xref-hashtb
4618                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4619         (mapatoms
4620          (lambda (group)
4621            (unless (string= from-newsgroup (setq name (symbol-name group)))
4622              (setq idlist (symbol-value group))
4623              ;; Dead groups are not updated.
4624              (and (prog1
4625                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4626                             info (nth 2 entry))
4627                     (when (stringp (setq nth4 (gnus-info-method info)))
4628                       (setq nth4 (gnus-server-to-method nth4))))
4629                   ;; Only do the xrefs if the group has the same
4630                   ;; select method as the group we have just read.
4631                   (or (gnus-methods-equal-p
4632                        nth4 (gnus-find-method-for-group from-newsgroup))
4633                       virtual
4634                       (equal nth4 (setq method (gnus-find-method-for-group
4635                                                 from-newsgroup)))
4636                       (and (equal (car nth4) (car method))
4637                            (equal (nth 1 nth4) (nth 1 method))))
4638                   gnus-use-cross-reference
4639                   (or (not (eq gnus-use-cross-reference t))
4640                       virtual
4641                       ;; Only do cross-references on subscribed
4642                       ;; groups, if that is what is wanted.
4643                       (<= (gnus-info-level info) gnus-level-subscribed))
4644                   (gnus-group-make-articles-read name idlist))))
4645          xref-hashtb)))))
4646
4647 (defun gnus-compute-read-articles (group articles)
4648   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4649          (info (nth 2 entry))
4650          (active (gnus-active group))
4651          ninfo)
4652     (when entry
4653       ;; First peel off all invalid article numbers.
4654       (when active
4655         (let ((ids articles)
4656               id first)
4657           (while (setq id (pop ids))
4658             (when (and first (> id (cdr active)))
4659               ;; We'll end up in this situation in one particular
4660               ;; obscure situation.  If you re-scan a group and get
4661               ;; a new article that is cross-posted to a different
4662               ;; group that has not been re-scanned, you might get
4663               ;; crossposted article that has a higher number than
4664               ;; Gnus believes possible.  So we re-activate this
4665               ;; group as well.  This might mean doing the
4666               ;; crossposting thingy will *increase* the number
4667               ;; of articles in some groups.  Tsk, tsk.
4668               (setq active (or (gnus-activate-group group) active)))
4669             (when (or (> id (cdr active))
4670                       (< id (car active)))
4671               (setq articles (delq id articles))))))
4672       ;; If the read list is nil, we init it.
4673       (if (and active
4674                (null (gnus-info-read info))
4675                (> (car active) 1))
4676           (setq ninfo (cons 1 (1- (car active))))
4677         (setq ninfo (gnus-info-read info)))
4678       ;; Then we add the read articles to the range.
4679       (gnus-add-to-range
4680        ninfo (setq articles (sort articles '<))))))
4681
4682 (defun gnus-group-make-articles-read (group articles)
4683   "Update the info of GROUP to say that ARTICLES are read."
4684   (let* ((num 0)
4685          (entry (gnus-gethash group gnus-newsrc-hashtb))
4686          (info (nth 2 entry))
4687          (active (gnus-active group))
4688          range)
4689     (when entry
4690       (setq range (gnus-compute-read-articles group articles))
4691       (save-excursion
4692         (set-buffer gnus-group-buffer)
4693         (gnus-undo-register
4694           `(progn
4695              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4696              (gnus-info-set-read ',info ',(gnus-info-read info))
4697              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4698              (gnus-group-update-group ,group t))))
4699       ;; Add the read articles to the range.
4700       (gnus-info-set-read info range)
4701       ;; Then we have to re-compute how many unread
4702       ;; articles there are in this group.
4703       (when active
4704         (cond
4705          ((not range)
4706           (setq num (- (1+ (cdr active)) (car active))))
4707          ((not (listp (cdr range)))
4708           (setq num (- (cdr active) (- (1+ (cdr range))
4709                                        (car range)))))
4710          (t
4711           (while range
4712             (if (numberp (car range))
4713                 (setq num (1+ num))
4714               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4715             (setq range (cdr range)))
4716           (setq num (- (cdr active) num))))
4717         ;; Update the number of unread articles.
4718         (setcar entry num)
4719         ;; Update the group buffer.
4720         (gnus-group-update-group group t)))))
4721
4722 (defvar gnus-newsgroup-none-id 0)
4723
4724 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4725   (let ((cur nntp-server-buffer)
4726         (dependencies
4727          (or dependencies
4728              (save-excursion (set-buffer gnus-summary-buffer)
4729                              gnus-newsgroup-dependencies)))
4730         headers id end ref
4731         (mail-parse-charset gnus-newsgroup-charset)
4732         (mail-parse-ignored-charsets
4733          (save-excursion (condition-case nil
4734                              (set-buffer gnus-summary-buffer)
4735                            (error))
4736                          gnus-newsgroup-ignored-charsets)))
4737     (save-excursion
4738       (set-buffer nntp-server-buffer)
4739       ;; Translate all TAB characters into SPACE characters.
4740       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4741       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4742       (gnus-run-hooks 'gnus-parse-headers-hook)
4743       (let ((case-fold-search t)
4744             in-reply-to header p lines chars ctype)
4745         (goto-char (point-min))
4746         ;; Search to the beginning of the next header.  Error messages
4747         ;; do not begin with 2 or 3.
4748         (while (re-search-forward "^[23][0-9]+ " nil t)
4749           (setq id nil
4750                 ref nil)
4751           ;; This implementation of this function, with nine
4752           ;; search-forwards instead of the one re-search-forward and
4753           ;; a case (which basically was the old function) is actually
4754           ;; about twice as fast, even though it looks messier.  You
4755           ;; can't have everything, I guess.  Speed and elegance
4756           ;; doesn't always go hand in hand.
4757           (setq
4758            header
4759            (make-full-mail-header
4760             ;; Number.
4761             (prog1
4762                 (read cur)
4763               (end-of-line)
4764               (setq p (point))
4765               (narrow-to-region (point)
4766                                 (or (and (search-forward "\n.\n" nil t)
4767                                          (- (point) 2))
4768                                     (point))))
4769             ;; Subject.
4770             (progn
4771               (goto-char p)
4772               (if (search-forward "\nsubject: " nil t)
4773                   (nnheader-header-value)
4774                 "(none)"))
4775             ;; From.
4776             (progn
4777               (goto-char p)
4778               (if (or (search-forward "\nfrom: " nil t)
4779                       (search-forward "\nfrom:" nil t))
4780                   (nnheader-header-value)
4781                 "(nobody)"))
4782             ;; Date.
4783             (progn
4784               (goto-char p)
4785               (if (search-forward "\ndate: " nil t)
4786                   (nnheader-header-value)
4787                 ""))
4788             ;; Message-ID.
4789             (progn
4790               (goto-char p)
4791               (setq id (if (re-search-forward
4792                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4793                            ;; We do it this way to make sure the Message-ID
4794                            ;; is (somewhat) syntactically valid.
4795                            (buffer-substring (match-beginning 1)
4796                                              (match-end 1))
4797                          ;; If there was no message-id, we just fake one
4798                          ;; to make subsequent routines simpler.
4799                          (nnheader-generate-fake-message-id))))
4800             ;; References.
4801             (progn
4802               (goto-char p)
4803               (if (search-forward "\nreferences: " nil t)
4804                   (progn
4805                     (setq end (point))
4806                     (prog1
4807                         (nnheader-header-value)
4808                       (setq ref
4809                             (buffer-substring
4810                              (progn
4811                                ;; (end-of-line)
4812                                (search-backward ">" end t)
4813                                (1+ (point)))
4814                              (progn
4815                                (search-backward "<" end t)
4816                                (point))))))
4817                 ;; Get the references from the in-reply-to header if there
4818                 ;; were no references and the in-reply-to header looks
4819                 ;; promising.
4820                 (if (and (search-forward "\nin-reply-to: " nil t)
4821                          (setq in-reply-to (nnheader-header-value))
4822                          (string-match "<[^>]+>" in-reply-to))
4823                     (let (ref2)
4824                       (setq ref (substring in-reply-to (match-beginning 0)
4825                                            (match-end 0)))
4826                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4827                         (setq ref2 (substring in-reply-to (match-beginning 0)
4828                                               (match-end 0)))
4829                         (when (> (length ref2) (length ref))
4830                           (setq ref ref2)))
4831                       ref)
4832                   (setq ref nil))))
4833             ;; Chars.
4834             (progn
4835               (goto-char p)
4836               (if (search-forward "\nchars: " nil t)
4837                   (if (numberp (setq chars (ignore-errors (read cur))))
4838                       chars 0)
4839                 0))
4840             ;; Lines.
4841             (progn
4842               (goto-char p)
4843               (if (search-forward "\nlines: " nil t)
4844                   (if (numberp (setq lines (ignore-errors (read cur))))
4845                       lines 0)
4846                 0))
4847             ;; Xref.
4848             (progn
4849               (goto-char p)
4850               (and (search-forward "\nxref: " nil t)
4851                    (nnheader-header-value)))
4852             ;; Extra.
4853             (when gnus-extra-headers
4854               (let ((extra gnus-extra-headers)
4855                     out)
4856                 (while extra
4857                   (goto-char p)
4858                   (when (search-forward
4859                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4860                     (push (cons (car extra) (nnheader-header-value)) out))
4861                   (pop extra))
4862                 out))))
4863           (goto-char p)
4864           (if (and (search-forward "\ncontent-type: " nil t)
4865                    (setq ctype (nnheader-header-value)))
4866               (mime-entity-set-content-type-internal
4867                header (mime-parse-Content-Type ctype)))
4868           (when (equal id ref)
4869             (setq ref nil))
4870
4871           (when gnus-alter-header-function
4872             (funcall gnus-alter-header-function header)
4873             (setq id (mail-header-id header)
4874                   ref (gnus-parent-id (mail-header-references header))))
4875
4876           (when (setq header
4877                       (gnus-dependencies-add-header
4878                        header dependencies force-new))
4879             (push header headers))
4880           (goto-char (point-max))
4881           (widen))
4882         (nreverse headers)))))
4883
4884 ;; Goes through the xover lines and returns a list of vectors
4885 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4886                                                   force-new dependencies
4887                                                   group also-fetch-heads)
4888   "Parse the news overview data in the server buffer.
4889 Return a list of headers that match SEQUENCE (see
4890 `nntp-retrieve-headers')."
4891   ;; Get the Xref when the users reads the articles since most/some
4892   ;; NNTP servers do not include Xrefs when using XOVER.
4893   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4894   (let ((mail-parse-charset gnus-newsgroup-charset)
4895         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4896         (cur nntp-server-buffer)
4897         (dependencies (or dependencies gnus-newsgroup-dependencies))
4898         number headers header)
4899     (save-excursion
4900       (set-buffer nntp-server-buffer)
4901       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4902       ;; Allow the user to mangle the headers before parsing them.
4903       (gnus-run-hooks 'gnus-parse-headers-hook)
4904       (goto-char (point-min))
4905       (while (not (eobp))
4906         (condition-case ()
4907             (while (and sequence (not (eobp)))
4908               (setq number (read cur))
4909               (while (and sequence
4910                           (< (car sequence) number))
4911                 (setq sequence (cdr sequence)))
4912               (and sequence
4913                    (eq number (car sequence))
4914                    (progn
4915                      (setq sequence (cdr sequence))
4916                      (setq header (inline
4917                                     (gnus-nov-parse-line
4918                                      number dependencies force-new))))
4919                    (push header headers))
4920               (forward-line 1))
4921           (error
4922            (gnus-error 4 "Strange nov line (%d)"
4923                        (count-lines (point-min) (point)))))
4924         (forward-line 1))
4925       ;; A common bug in inn is that if you have posted an article and
4926       ;; then retrieves the active file, it will answer correctly --
4927       ;; the new article is included.  However, a NOV entry for the
4928       ;; article may not have been generated yet, so this may fail.
4929       ;; We work around this problem by retrieving the last few
4930       ;; headers using HEAD.
4931       (if (or (not also-fetch-heads)
4932               (not sequence))
4933           ;; We (probably) got all the headers.
4934           (nreverse headers)
4935         (let ((gnus-nov-is-evil t))
4936           (nconc
4937            (nreverse headers)
4938            (gnus-retrieve-parsed-headers sequence group)
4939            ))))))
4940
4941 (defun gnus-article-get-xrefs ()
4942   "Fill in the Xref value in `gnus-current-headers', if necessary.
4943 This is meant to be called in `gnus-article-internal-prepare-hook'."
4944   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4945                                  gnus-current-headers)))
4946     (or (not gnus-use-cross-reference)
4947         (not headers)
4948         (and (mail-header-xref headers)
4949              (not (string= (mail-header-xref headers) "")))
4950         (let ((case-fold-search t)
4951               xref)
4952           (save-restriction
4953             (nnheader-narrow-to-headers)
4954             (goto-char (point-min))
4955             (when (or (and (not (eobp))
4956                            (eq (downcase (char-after)) ?x)
4957                            (looking-at "Xref:"))
4958                       (search-forward "\nXref:" nil t))
4959               (goto-char (1+ (match-end 0)))
4960               (setq xref (buffer-substring (point)
4961                                            (progn (end-of-line) (point))))
4962               (mail-header-set-xref headers xref)))))))
4963
4964 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4965   "Find article ID and insert the summary line for that article.
4966 OLD-HEADER can either be a header or a line number to insert
4967 the subject line on."
4968   (let* ((line (and (numberp old-header) old-header))
4969          (old-header (and (vectorp old-header) old-header))
4970          (header (cond ((and old-header use-old-header)
4971                         old-header)
4972                        ((and (numberp id)
4973                              (gnus-number-to-header id))
4974                         (gnus-number-to-header id))
4975                        (t
4976                         (gnus-read-header id))))
4977          (number (and (numberp id) id))
4978          d)
4979     (when header
4980       ;; Rebuild the thread that this article is part of and go to the
4981       ;; article we have fetched.
4982       (when (and (not gnus-show-threads)
4983                  old-header)
4984         (when (and number
4985                    (setq d (gnus-data-find (mail-header-number old-header))))
4986           (goto-char (gnus-data-pos d))
4987           (gnus-data-remove
4988            number
4989            (- (gnus-point-at-bol)
4990               (prog1
4991                   (1+ (gnus-point-at-eol))
4992                 (gnus-delete-line))))))
4993       (when old-header
4994         (mail-header-set-number header (mail-header-number old-header)))
4995       (setq gnus-newsgroup-sparse
4996             (delq (setq number (mail-header-number header))
4997                   gnus-newsgroup-sparse))
4998       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4999       (push number gnus-newsgroup-limit)
5000       (gnus-rebuild-thread (mail-header-id header) line)
5001       (gnus-summary-goto-subject number nil t))
5002     (when (and (numberp number)
5003                (> number 0))
5004       ;; We have to update the boundaries even if we can't fetch the
5005       ;; article if ID is a number -- so that the next `P' or `N'
5006       ;; command will fetch the previous (or next) article even
5007       ;; if the one we tried to fetch this time has been canceled.
5008       (when (> number gnus-newsgroup-end)
5009         (setq gnus-newsgroup-end number))
5010       (when (< number gnus-newsgroup-begin)
5011         (setq gnus-newsgroup-begin number))
5012       (setq gnus-newsgroup-unselected
5013             (delq number gnus-newsgroup-unselected)))
5014     ;; Report back a success?
5015     (and header (mail-header-number header))))
5016
5017 ;;; Process/prefix in the summary buffer
5018
5019 (defun gnus-summary-work-articles (n)
5020   "Return a list of articles to be worked upon.
5021 The prefix argument, the list of process marked articles, and the
5022 current article will be taken into consideration."
5023   (save-excursion
5024     (set-buffer gnus-summary-buffer)
5025     (cond
5026      (n
5027       ;; A numerical prefix has been given.
5028       (setq n (prefix-numeric-value n))
5029       (let ((backward (< n 0))
5030             (n (abs (prefix-numeric-value n)))
5031             articles article)
5032         (save-excursion
5033           (while
5034               (and (> n 0)
5035                    (push (setq article (gnus-summary-article-number))
5036                          articles)
5037                    (if backward
5038                        (gnus-summary-find-prev nil article)
5039                      (gnus-summary-find-next nil article)))
5040             (decf n)))
5041         (nreverse articles)))
5042      ((and (gnus-region-active-p) (mark))
5043       (message "region active")
5044       ;; Work on the region between point and mark.
5045       (let ((max (max (point) (mark)))
5046             articles article)
5047         (save-excursion
5048           (goto-char (min (point) (mark)))
5049           (while
5050               (and
5051                (push (setq article (gnus-summary-article-number)) articles)
5052                (gnus-summary-find-next nil article)
5053                (< (point) max)))
5054           (nreverse articles))))
5055      (gnus-newsgroup-processable
5056       ;; There are process-marked articles present.
5057       ;; Save current state.
5058       (gnus-summary-save-process-mark)
5059       ;; Return the list.
5060       (reverse gnus-newsgroup-processable))
5061      (t
5062       ;; Just return the current article.
5063       (list (gnus-summary-article-number))))))
5064
5065 (defmacro gnus-summary-iterate (arg &rest forms)
5066   "Iterate over the process/prefixed articles and do FORMS.
5067 ARG is the interactive prefix given to the command.  FORMS will be
5068 executed with point over the summary line of the articles."
5069   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5070     `(let ((,articles (gnus-summary-work-articles ,arg)))
5071        (while ,articles
5072          (gnus-summary-goto-subject (car ,articles))
5073          ,@forms
5074          (pop ,articles)))))
5075
5076 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5077 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5078
5079 (defun gnus-summary-save-process-mark ()
5080   "Push the current set of process marked articles on the stack."
5081   (interactive)
5082   (push (copy-sequence gnus-newsgroup-processable)
5083         gnus-newsgroup-process-stack))
5084
5085 (defun gnus-summary-kill-process-mark ()
5086   "Push the current set of process marked articles on the stack and unmark."
5087   (interactive)
5088   (gnus-summary-save-process-mark)
5089   (gnus-summary-unmark-all-processable))
5090
5091 (defun gnus-summary-yank-process-mark ()
5092   "Pop the last process mark state off the stack and restore it."
5093   (interactive)
5094   (unless gnus-newsgroup-process-stack
5095     (error "Empty mark stack"))
5096   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5097
5098 (defun gnus-summary-process-mark-set (set)
5099   "Make SET into the current process marked articles."
5100   (gnus-summary-unmark-all-processable)
5101   (while set
5102     (gnus-summary-set-process-mark (pop set))))
5103
5104 ;;; Searching and stuff
5105
5106 (defun gnus-summary-search-group (&optional backward use-level)
5107   "Search for next unread newsgroup.
5108 If optional argument BACKWARD is non-nil, search backward instead."
5109   (save-excursion
5110     (set-buffer gnus-group-buffer)
5111     (when (gnus-group-search-forward
5112            backward nil (if use-level (gnus-group-group-level) nil))
5113       (gnus-group-group-name))))
5114
5115 (defun gnus-summary-best-group (&optional exclude-group)
5116   "Find the name of the best unread group.
5117 If EXCLUDE-GROUP, do not go to this group."
5118   (save-excursion
5119     (set-buffer gnus-group-buffer)
5120     (save-excursion
5121       (gnus-group-best-unread-group exclude-group))))
5122
5123 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5124   (if backward (gnus-summary-find-prev)
5125     (let* ((dummy (gnus-summary-article-intangible-p))
5126            (article (or article (gnus-summary-article-number)))
5127            (arts (gnus-data-find-list article))
5128            result)
5129       (when (and (not dummy)
5130                  (or (not gnus-summary-check-current)
5131                      (not unread)
5132                      (not (gnus-data-unread-p (car arts)))))
5133         (setq arts (cdr arts)))
5134       (when (setq result
5135                   (if unread
5136                       (progn
5137                         (while arts
5138                           (when (or (and undownloaded
5139                                          (eq gnus-undownloaded-mark
5140                                              (gnus-data-mark (car arts))))
5141                                     (gnus-data-unread-p (car arts)))
5142                             (setq result (car arts)
5143                                   arts nil))
5144                           (setq arts (cdr arts)))
5145                         result)
5146                     (car arts)))
5147         (goto-char (gnus-data-pos result))
5148         (gnus-data-number result)))))
5149
5150 (defun gnus-summary-find-prev (&optional unread article)
5151   (let* ((eobp (eobp))
5152          (article (or article (gnus-summary-article-number)))
5153          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5154          result)
5155     (when (and (not eobp)
5156                (or (not gnus-summary-check-current)
5157                    (not unread)
5158                    (not (gnus-data-unread-p (car arts)))))
5159       (setq arts (cdr arts)))
5160     (when (setq result
5161                 (if unread
5162                     (progn
5163                       (while arts
5164                         (when (gnus-data-unread-p (car arts))
5165                           (setq result (car arts)
5166                                 arts nil))
5167                         (setq arts (cdr arts)))
5168                       result)
5169                   (car arts)))
5170       (goto-char (gnus-data-pos result))
5171       (gnus-data-number result))))
5172
5173 (defun gnus-summary-find-subject (subject &optional unread backward article)
5174   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5175          (article (or article (gnus-summary-article-number)))
5176          (articles (gnus-data-list backward))
5177          (arts (gnus-data-find-list article articles))
5178          result)
5179     (when (or (not gnus-summary-check-current)
5180               (not unread)
5181               (not (gnus-data-unread-p (car arts))))
5182       (setq arts (cdr arts)))
5183     (while arts
5184       (and (or (not unread)
5185                (gnus-data-unread-p (car arts)))
5186            (vectorp (gnus-data-header (car arts)))
5187            (gnus-subject-equal
5188             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5189            (setq result (car arts)
5190                  arts nil))
5191       (setq arts (cdr arts)))
5192     (and result
5193          (goto-char (gnus-data-pos result))
5194          (gnus-data-number result))))
5195
5196 (defun gnus-summary-search-forward (&optional unread subject backward)
5197   "Search forward for an article.
5198 If UNREAD, look for unread articles.  If SUBJECT, look for
5199 articles with that subject.  If BACKWARD, search backward instead."
5200   (cond (subject (gnus-summary-find-subject subject unread backward))
5201         (backward (gnus-summary-find-prev unread))
5202         (t (gnus-summary-find-next unread))))
5203
5204 (defun gnus-recenter (&optional n)
5205   "Center point in window and redisplay frame.
5206 Also do horizontal recentering."
5207   (interactive "P")
5208   (when (and gnus-auto-center-summary
5209              (not (eq gnus-auto-center-summary 'vertical)))
5210     (gnus-horizontal-recenter))
5211   (recenter n))
5212
5213 (defun gnus-summary-recenter ()
5214   "Center point in the summary window.
5215 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5216 displayed, no centering will be performed."
5217   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5218   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5219   (interactive)
5220   (let* ((top (cond ((< (window-height) 4) 0)
5221                     ((< (window-height) 7) 1)
5222                     (t (if (numberp gnus-auto-center-summary)
5223                            gnus-auto-center-summary
5224                          2))))
5225          (height (1- (window-height)))
5226          (bottom (save-excursion (goto-char (point-max))
5227                                  (forward-line (- height))
5228                                  (point)))
5229          (window (get-buffer-window (current-buffer))))
5230     ;; The user has to want it.
5231     (when gnus-auto-center-summary
5232       (when (get-buffer-window gnus-article-buffer)
5233         ;; Only do recentering when the article buffer is displayed,
5234         ;; Set the window start to either `bottom', which is the biggest
5235         ;; possible valid number, or the second line from the top,
5236         ;; whichever is the least.
5237         (set-window-start
5238          window (min bottom (save-excursion
5239                               (forward-line (- top)) (point)))
5240          t))
5241       ;; Do horizontal recentering while we're at it.
5242       (when (and (get-buffer-window (current-buffer) t)
5243                  (not (eq gnus-auto-center-summary 'vertical)))
5244         (let ((selected (selected-window)))
5245           (select-window (get-buffer-window (current-buffer) t))
5246           (gnus-summary-position-point)
5247           (gnus-horizontal-recenter)
5248           (select-window selected))))))
5249
5250 (defun gnus-summary-jump-to-group (newsgroup)
5251   "Move point to NEWSGROUP in group mode buffer."
5252   ;; Keep update point of group mode buffer if visible.
5253   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5254       (save-window-excursion
5255         ;; Take care of tree window mode.
5256         (when (get-buffer-window gnus-group-buffer)
5257           (pop-to-buffer gnus-group-buffer))
5258         (gnus-group-jump-to-group newsgroup))
5259     (save-excursion
5260       ;; Take care of tree window mode.
5261       (if (get-buffer-window gnus-group-buffer)
5262           (pop-to-buffer gnus-group-buffer)
5263         (set-buffer gnus-group-buffer))
5264       (gnus-group-jump-to-group newsgroup))))
5265
5266 ;; This function returns a list of article numbers based on the
5267 ;; difference between the ranges of read articles in this group and
5268 ;; the range of active articles.
5269 (defun gnus-list-of-unread-articles (group)
5270   (let* ((read (gnus-info-read (gnus-get-info group)))
5271          (active (or (gnus-active group) (gnus-activate-group group)))
5272          (last (cdr active))
5273          first nlast unread)
5274     ;; If none are read, then all are unread.
5275     (if (not read)
5276         (setq first (car active))
5277       ;; If the range of read articles is a single range, then the
5278       ;; first unread article is the article after the last read
5279       ;; article.  Sounds logical, doesn't it?
5280       (if (and (not (listp (cdr read)))
5281                (or (< (car read) (car active))
5282                    (progn (setq read (list read))
5283                           nil)))
5284           (setq first (max (car active) (1+ (cdr read))))
5285         ;; `read' is a list of ranges.
5286         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5287                                   (caar read)))
5288                   1)
5289           (setq first (car active)))
5290         (while read
5291           (when first
5292             (while (< first nlast)
5293               (push first unread)
5294               (setq first (1+ first))))
5295           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5296           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5297           (setq read (cdr read)))))
5298     ;; And add the last unread articles.
5299     (while (<= first last)
5300       (push first unread)
5301       (setq first (1+ first)))
5302     ;; Return the list of unread articles.
5303     (delq 0 (nreverse unread))))
5304
5305 (defun gnus-list-of-read-articles (group)
5306   "Return a list of unread, unticked and non-dormant articles."
5307   (let* ((info (gnus-get-info group))
5308          (marked (gnus-info-marks info))
5309          (active (gnus-active group)))
5310     (and info active
5311          (gnus-set-difference
5312           (gnus-sorted-complement
5313            (gnus-uncompress-range active)
5314            (gnus-list-of-unread-articles group))
5315           (append
5316            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5317            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5318
5319 ;; Various summary commands
5320
5321 (defun gnus-summary-select-article-buffer ()
5322   "Reconfigure windows to show article buffer."
5323   (interactive)
5324   (if (not (gnus-buffer-live-p gnus-article-buffer))
5325       (error "There is no article buffer for this summary buffer")
5326     (gnus-configure-windows 'article)
5327     (select-window (get-buffer-window gnus-article-buffer))))
5328
5329 (defun gnus-summary-universal-argument (arg)
5330   "Perform any operation on all articles that are process/prefixed."
5331   (interactive "P")
5332   (let ((articles (gnus-summary-work-articles arg))
5333         func article)
5334     (if (eq
5335          (setq
5336           func
5337           (key-binding
5338            (read-key-sequence
5339             (substitute-command-keys
5340              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5341          'undefined)
5342         (gnus-error 1 "Undefined key")
5343       (save-excursion
5344         (while articles
5345           (gnus-summary-goto-subject (setq article (pop articles)))
5346           (let (gnus-newsgroup-processable)
5347             (command-execute func))
5348           (gnus-summary-remove-process-mark article)))))
5349   (gnus-summary-position-point))
5350
5351 (defun gnus-summary-toggle-truncation (&optional arg)
5352   "Toggle truncation of summary lines.
5353 With arg, turn line truncation on iff arg is positive."
5354   (interactive "P")
5355   (setq truncate-lines
5356         (if (null arg) (not truncate-lines)
5357           (> (prefix-numeric-value arg) 0)))
5358   (redraw-display))
5359
5360 (defun gnus-summary-reselect-current-group (&optional all rescan)
5361   "Exit and then reselect the current newsgroup.
5362 The prefix argument ALL means to select all articles."
5363   (interactive "P")
5364   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5365     (error "Ephemeral groups can't be reselected"))
5366   (let ((current-subject (gnus-summary-article-number))
5367         (group gnus-newsgroup-name))
5368     (setq gnus-newsgroup-begin nil)
5369     (gnus-summary-exit)
5370     ;; We have to adjust the point of group mode buffer because
5371     ;; point was moved to the next unread newsgroup by exiting.
5372     (gnus-summary-jump-to-group group)
5373     (when rescan
5374       (save-excursion
5375         (save-window-excursion
5376           ;; Don't show group contents.
5377           (set-window-start (selected-window) (point-max))
5378           (gnus-group-get-new-news-this-group 1))))
5379     (gnus-group-read-group all t)
5380     (gnus-summary-goto-subject current-subject nil t)))
5381
5382 (defun gnus-summary-rescan-group (&optional all)
5383   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5384   (interactive "P")
5385   (gnus-summary-reselect-current-group all t))
5386
5387 (defun gnus-summary-update-info (&optional non-destructive)
5388   (save-excursion
5389     (let ((group gnus-newsgroup-name))
5390       (when group
5391         (when gnus-newsgroup-kill-headers
5392           (setq gnus-newsgroup-killed
5393                 (gnus-compress-sequence
5394                  (nconc
5395                   (gnus-set-sorted-intersection
5396                    (gnus-uncompress-range gnus-newsgroup-killed)
5397                    (setq gnus-newsgroup-unselected
5398                          (sort gnus-newsgroup-unselected '<)))
5399                   (setq gnus-newsgroup-unreads
5400                         (sort gnus-newsgroup-unreads '<)))
5401                  t)))
5402         (unless (listp (cdr gnus-newsgroup-killed))
5403           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5404         (let ((headers gnus-newsgroup-headers))
5405           ;; Set the new ranges of read articles.
5406           (save-excursion
5407             (set-buffer gnus-group-buffer)
5408             (gnus-undo-force-boundary))
5409           (gnus-update-read-articles
5410            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5411           ;; Set the current article marks.
5412           (let ((gnus-newsgroup-scored
5413                  (if (and (not gnus-save-score)
5414                           (not non-destructive))
5415                      nil
5416                    gnus-newsgroup-scored)))
5417             (save-excursion
5418               (gnus-update-marks)))
5419           ;; Do the cross-ref thing.
5420           (when gnus-use-cross-reference
5421             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5422           ;; Do not switch windows but change the buffer to work.
5423           (set-buffer gnus-group-buffer)
5424           (unless (gnus-ephemeral-group-p group)
5425             (gnus-group-update-group group)))))))
5426
5427 (defun gnus-summary-save-newsrc (&optional force)
5428   "Save the current number of read/marked articles in the dribble buffer.
5429 The dribble buffer will then be saved.
5430 If FORCE (the prefix), also save the .newsrc file(s)."
5431   (interactive "P")
5432   (gnus-summary-update-info t)
5433   (if force
5434       (gnus-save-newsrc-file)
5435     (gnus-dribble-save)))
5436
5437 (defun gnus-summary-exit (&optional temporary)
5438   "Exit reading current newsgroup, and then return to group selection mode.
5439 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5440   (interactive)
5441   (gnus-set-global-variables)
5442   (gnus-kill-save-kill-buffer)
5443   (gnus-async-halt-prefetch)
5444   (let* ((group gnus-newsgroup-name)
5445          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5446          (mode major-mode)
5447          (group-point nil)
5448          (buf (current-buffer)))
5449     (unless quit-config
5450       ;; Do adaptive scoring, and possibly save score files.
5451       (when gnus-newsgroup-adaptive
5452         (gnus-score-adaptive))
5453       (when gnus-use-scoring
5454         (gnus-score-save)))
5455     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5456     ;; If we have several article buffers, we kill them at exit.
5457     (unless gnus-single-article-buffer
5458       (gnus-kill-buffer gnus-original-article-buffer)
5459       (setq gnus-article-current nil))
5460     (when gnus-use-cache
5461       (gnus-cache-possibly-remove-articles)
5462       (gnus-cache-save-buffers))
5463     (gnus-async-prefetch-remove-group group)
5464     (when gnus-suppress-duplicates
5465       (gnus-dup-enter-articles))
5466     (when gnus-use-trees
5467       (gnus-tree-close group))
5468     (when gnus-use-cache
5469       (gnus-cache-write-active))
5470     ;; Remove entries for this group.
5471     (nnmail-purge-split-history (gnus-group-real-name group))
5472     ;; Make all changes in this group permanent.
5473     (unless quit-config
5474       (gnus-run-hooks 'gnus-exit-group-hook)
5475       (gnus-summary-update-info))
5476     (gnus-close-group group)
5477     ;; Make sure where we were, and go to next newsgroup.
5478     (set-buffer gnus-group-buffer)
5479     (unless quit-config
5480       (gnus-group-jump-to-group group))
5481     (gnus-run-hooks 'gnus-summary-exit-hook)
5482     (unless (or quit-config
5483                 ;; If this group has disappeared from the summary
5484                 ;; buffer, don't skip forwards.
5485                 (not (string= group (gnus-group-group-name))))
5486       (gnus-group-next-unread-group 1))
5487     (setq group-point (point))
5488     (if temporary
5489         nil                             ;Nothing to do.
5490       ;; If we have several article buffers, we kill them at exit.
5491       (unless gnus-single-article-buffer
5492         (gnus-kill-buffer gnus-article-buffer)
5493         (gnus-kill-buffer gnus-original-article-buffer)
5494         (setq gnus-article-current nil))
5495       (set-buffer buf)
5496       (if (not gnus-kill-summary-on-exit)
5497           (gnus-deaden-summary)
5498         ;; We set all buffer-local variables to nil.  It is unclear why
5499         ;; this is needed, but if we don't, buffer-local variables are
5500         ;; not garbage-collected, it seems.  This would the lead to en
5501         ;; ever-growing Emacs.
5502         (gnus-summary-clear-local-variables)
5503         (when (get-buffer gnus-article-buffer)
5504           (bury-buffer gnus-article-buffer))
5505         ;; We clear the global counterparts of the buffer-local
5506         ;; variables as well, just to be on the safe side.
5507         (set-buffer gnus-group-buffer)
5508         (gnus-summary-clear-local-variables)
5509         ;; Return to group mode buffer.
5510         (when (eq mode 'gnus-summary-mode)
5511           (gnus-kill-buffer buf)))
5512       (setq gnus-current-select-method gnus-select-method)
5513       (pop-to-buffer gnus-group-buffer)
5514       (if (not quit-config)
5515           (progn
5516             (goto-char group-point)
5517             (gnus-configure-windows 'group 'force)
5518             (unless (pos-visible-in-window-p)
5519               (forward-line (/ (static-if (featurep 'xemacs)
5520                                    (window-displayed-height)
5521                                  (1- (window-height)))
5522                                -2))
5523               (set-window-start (selected-window) (point))
5524               (goto-char group-point)))
5525         (gnus-handle-ephemeral-exit quit-config))
5526       ;; Clear the current group name.
5527       (unless quit-config
5528         (setq gnus-newsgroup-name nil)))))
5529
5530 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5531 (defun gnus-summary-exit-no-update (&optional no-questions)
5532   "Quit reading current newsgroup without updating read article info."
5533   (interactive)
5534   (let* ((group gnus-newsgroup-name)
5535          (quit-config (gnus-group-quit-config group)))
5536     (when (or no-questions
5537               gnus-expert-user
5538               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5539       (gnus-async-halt-prefetch)
5540       (mapcar 'funcall
5541               (delq 'gnus-summary-expire-articles
5542                     (copy-sequence gnus-summary-prepare-exit-hook)))
5543       ;; If we have several article buffers, we kill them at exit.
5544       (unless gnus-single-article-buffer
5545         (gnus-kill-buffer gnus-article-buffer)
5546         (gnus-kill-buffer gnus-original-article-buffer)
5547         (setq gnus-article-current nil))
5548       (if (not gnus-kill-summary-on-exit)
5549           (gnus-deaden-summary)
5550         (gnus-close-group group)
5551         (gnus-summary-clear-local-variables)
5552         (set-buffer gnus-group-buffer)
5553         (gnus-summary-clear-local-variables)
5554         (when (get-buffer gnus-summary-buffer)
5555           (kill-buffer gnus-summary-buffer)))
5556       (unless gnus-single-article-buffer
5557         (setq gnus-article-current nil))
5558       (when gnus-use-trees
5559         (gnus-tree-close group))
5560       (gnus-async-prefetch-remove-group group)
5561       (when (get-buffer gnus-article-buffer)
5562         (bury-buffer gnus-article-buffer))
5563       ;; Return to the group buffer.
5564       (gnus-configure-windows 'group 'force)
5565       ;; Clear the current group name.
5566       (setq gnus-newsgroup-name nil)
5567       (when (equal (gnus-group-group-name) group)
5568         (gnus-group-next-unread-group 1))
5569       (when quit-config
5570         (gnus-handle-ephemeral-exit quit-config)))))
5571
5572 (defun gnus-handle-ephemeral-exit (quit-config)
5573   "Handle movement when leaving an ephemeral group.
5574 The state which existed when entering the ephemeral is reset."
5575   (if (not (buffer-name (car quit-config)))
5576       (gnus-configure-windows 'group 'force)
5577     (set-buffer (car quit-config))
5578     (cond ((eq major-mode 'gnus-summary-mode)
5579            (gnus-set-global-variables))
5580           ((eq major-mode 'gnus-article-mode)
5581            (save-excursion
5582              ;; The `gnus-summary-buffer' variable may point
5583              ;; to the old summary buffer when using a single
5584              ;; article buffer.
5585              (unless (gnus-buffer-live-p gnus-summary-buffer)
5586                (set-buffer gnus-group-buffer))
5587              (set-buffer gnus-summary-buffer)
5588              (gnus-set-global-variables))))
5589     (if (or (eq (cdr quit-config) 'article)
5590             (eq (cdr quit-config) 'pick))
5591         (progn
5592           ;; The current article may be from the ephemeral group
5593           ;; thus it is best that we reload this article
5594           (gnus-summary-show-article)
5595           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5596               (gnus-configure-windows 'pick 'force)
5597             (gnus-configure-windows (cdr quit-config) 'force)))
5598       (gnus-configure-windows (cdr quit-config) 'force))
5599     (when (eq major-mode 'gnus-summary-mode)
5600       (gnus-summary-next-subject 1 nil t)
5601       (gnus-summary-recenter)
5602       (gnus-summary-position-point))))
5603
5604 (defun gnus-summary-preview-mime-message ()
5605   "MIME decode and play this message."
5606   (interactive)
5607   (let ((gnus-break-pages nil)
5608         (gnus-show-mime t))
5609     (gnus-summary-select-article gnus-show-all-headers t))
5610   (select-window (get-buffer-window gnus-article-buffer)))
5611
5612 ;;; Dead summaries.
5613
5614 (defvar gnus-dead-summary-mode-map nil)
5615
5616 (unless gnus-dead-summary-mode-map
5617   (setq gnus-dead-summary-mode-map (make-keymap))
5618   (suppress-keymap gnus-dead-summary-mode-map)
5619   (substitute-key-definition
5620    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5621   (let ((keys '("\C-d" "\r" "\177" [delete])))
5622     (while keys
5623       (define-key gnus-dead-summary-mode-map
5624         (pop keys) 'gnus-summary-wake-up-the-dead))))
5625
5626 (defvar gnus-dead-summary-mode nil
5627   "Minor mode for Gnus summary buffers.")
5628
5629 (defun gnus-dead-summary-mode (&optional arg)
5630   "Minor mode for Gnus summary buffers."
5631   (interactive "P")
5632   (when (eq major-mode 'gnus-summary-mode)
5633     (make-local-variable 'gnus-dead-summary-mode)
5634     (setq gnus-dead-summary-mode
5635           (if (null arg) (not gnus-dead-summary-mode)
5636             (> (prefix-numeric-value arg) 0)))
5637     (when gnus-dead-summary-mode
5638       (gnus-add-minor-mode
5639        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5640
5641 (defun gnus-deaden-summary ()
5642   "Make the current summary buffer into a dead summary buffer."
5643   ;; Kill any previous dead summary buffer.
5644   (when (and gnus-dead-summary
5645              (buffer-name gnus-dead-summary))
5646     (save-excursion
5647       (set-buffer gnus-dead-summary)
5648       (when gnus-dead-summary-mode
5649         (kill-buffer (current-buffer)))))
5650   ;; Make this the current dead summary.
5651   (setq gnus-dead-summary (current-buffer))
5652   (gnus-dead-summary-mode 1)
5653   (let ((name (buffer-name)))
5654     (when (string-match "Summary" name)
5655       (rename-buffer
5656        (concat (substring name 0 (match-beginning 0)) "Dead "
5657                (substring name (match-beginning 0)))
5658        t)
5659       (bury-buffer))))
5660
5661 (defun gnus-kill-or-deaden-summary (buffer)
5662   "Kill or deaden the summary BUFFER."
5663   (save-excursion
5664     (when (and (buffer-name buffer)
5665                (not gnus-single-article-buffer))
5666       (save-excursion
5667         (set-buffer buffer)
5668         (gnus-kill-buffer gnus-article-buffer)
5669         (gnus-kill-buffer gnus-original-article-buffer)))
5670     (cond (gnus-kill-summary-on-exit
5671            (when (and gnus-use-trees
5672                       (gnus-buffer-exists-p buffer))
5673              (save-excursion
5674                (set-buffer buffer)
5675                (gnus-tree-close gnus-newsgroup-name)))
5676            (gnus-kill-buffer buffer))
5677           ((gnus-buffer-exists-p buffer)
5678            (save-excursion
5679              (set-buffer buffer)
5680              (gnus-deaden-summary))))))
5681
5682 (defun gnus-summary-wake-up-the-dead (&rest args)
5683   "Wake up the dead summary buffer."
5684   (interactive)
5685   (gnus-dead-summary-mode -1)
5686   (let ((name (buffer-name)))
5687     (when (string-match "Dead " name)
5688       (rename-buffer
5689        (concat (substring name 0 (match-beginning 0))
5690                (substring name (match-end 0)))
5691        t)))
5692   (gnus-message 3 "This dead summary is now alive again"))
5693
5694 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5695 (defun gnus-summary-fetch-faq (&optional faq-dir)
5696   "Fetch the FAQ for the current group.
5697 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5698 in."
5699   (interactive
5700    (list
5701     (when current-prefix-arg
5702       (completing-read
5703        "Faq dir: " (and (listp gnus-group-faq-directory)
5704                         (mapcar (lambda (file) (list file))
5705                                 gnus-group-faq-directory))))))
5706   (let (gnus-faq-buffer)
5707     (when (setq gnus-faq-buffer
5708                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5709       (gnus-configure-windows 'summary-faq))))
5710
5711 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5712 (defun gnus-summary-describe-group (&optional force)
5713   "Describe the current newsgroup."
5714   (interactive "P")
5715   (gnus-group-describe-group force gnus-newsgroup-name))
5716
5717 (defun gnus-summary-describe-briefly ()
5718   "Describe summary mode commands briefly."
5719   (interactive)
5720   (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")))
5721
5722 ;; Walking around group mode buffer from summary mode.
5723
5724 (defun gnus-summary-next-group (&optional no-article target-group backward)
5725   "Exit current newsgroup and then select next unread newsgroup.
5726 If prefix argument NO-ARTICLE is non-nil, no article is selected
5727 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5728 previous group instead."
5729   (interactive "P")
5730   ;; Stop pre-fetching.
5731   (gnus-async-halt-prefetch)
5732   (let ((current-group gnus-newsgroup-name)
5733         (current-buffer (current-buffer))
5734         entered)
5735     ;; First we semi-exit this group to update Xrefs and all variables.
5736     ;; We can't do a real exit, because the window conf must remain
5737     ;; the same in case the user is prompted for info, and we don't
5738     ;; want the window conf to change before that...
5739     (gnus-summary-exit t)
5740     (while (not entered)
5741       ;; Then we find what group we are supposed to enter.
5742       (set-buffer gnus-group-buffer)
5743       (gnus-group-jump-to-group current-group)
5744       (setq target-group
5745             (or target-group
5746                 (if (eq gnus-keep-same-level 'best)
5747                     (gnus-summary-best-group gnus-newsgroup-name)
5748                   (gnus-summary-search-group backward gnus-keep-same-level))))
5749       (if (not target-group)
5750           ;; There are no further groups, so we return to the group
5751           ;; buffer.
5752           (progn
5753             (gnus-message 5 "Returning to the group buffer")
5754             (setq entered t)
5755             (when (gnus-buffer-live-p current-buffer)
5756               (set-buffer current-buffer)
5757               (gnus-summary-exit))
5758             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5759         ;; We try to enter the target group.
5760         (gnus-group-jump-to-group target-group)
5761         (let ((unreads (gnus-group-group-unread)))
5762           (if (and (or (eq t unreads)
5763                        (and unreads (not (zerop unreads))))
5764                    (gnus-summary-read-group
5765                     target-group nil no-article
5766                     (and (buffer-name current-buffer) current-buffer)
5767                     nil backward))
5768               (setq entered t)
5769             (setq current-group target-group
5770                   target-group nil)))))))
5771
5772 (defun gnus-summary-prev-group (&optional no-article)
5773   "Exit current newsgroup and then select previous unread newsgroup.
5774 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5775   (interactive "P")
5776   (gnus-summary-next-group no-article nil t))
5777
5778 ;; Walking around summary lines.
5779
5780 (defun gnus-summary-first-subject (&optional unread undownloaded)
5781   "Go to the first unread subject.
5782 If UNREAD is non-nil, go to the first unread article.
5783 Returns the article selected or nil if there are no unread articles."
5784   (interactive "P")
5785   (prog1
5786       (cond
5787        ;; Empty summary.
5788        ((null gnus-newsgroup-data)
5789         (gnus-message 3 "No articles in the group")
5790         nil)
5791        ;; Pick the first article.
5792        ((not unread)
5793         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5794         (gnus-data-number (car gnus-newsgroup-data)))
5795        ;; No unread articles.
5796        ((null gnus-newsgroup-unreads)
5797         (gnus-message 3 "No more unread articles")
5798         nil)
5799        ;; Find the first unread article.
5800        (t
5801         (let ((data gnus-newsgroup-data))
5802           (while (and data
5803                       (and (not (and undownloaded
5804                                      (eq gnus-undownloaded-mark
5805                                          (gnus-data-mark (car data)))))
5806                            (not (gnus-data-unread-p (car data)))))
5807             (setq data (cdr data)))
5808           (when data
5809             (goto-char (gnus-data-pos (car data)))
5810             (gnus-data-number (car data))))))
5811     (gnus-summary-position-point)))
5812
5813 (defun gnus-summary-next-subject (n &optional unread dont-display)
5814   "Go to next N'th summary line.
5815 If N is negative, go to the previous N'th subject line.
5816 If UNREAD is non-nil, only unread articles are selected.
5817 The difference between N and the actual number of steps taken is
5818 returned."
5819   (interactive "p")
5820   (let ((backward (< n 0))
5821         (n (abs n)))
5822     (while (and (> n 0)
5823                 (if backward
5824                     (gnus-summary-find-prev unread)
5825                   (gnus-summary-find-next unread)))
5826       (unless (zerop (setq n (1- n)))
5827         (gnus-summary-show-thread)))
5828     (when (/= 0 n)
5829       (gnus-message 7 "No more%s articles"
5830                     (if unread " unread" "")))
5831     (unless dont-display
5832       (gnus-summary-recenter)
5833       (gnus-summary-position-point))
5834     n))
5835
5836 (defun gnus-summary-next-unread-subject (n)
5837   "Go to next N'th unread summary line."
5838   (interactive "p")
5839   (gnus-summary-next-subject n t))
5840
5841 (defun gnus-summary-prev-subject (n &optional unread)
5842   "Go to previous N'th summary line.
5843 If optional argument UNREAD is non-nil, only unread article is selected."
5844   (interactive "p")
5845   (gnus-summary-next-subject (- n) unread))
5846
5847 (defun gnus-summary-prev-unread-subject (n)
5848   "Go to previous N'th unread summary line."
5849   (interactive "p")
5850   (gnus-summary-next-subject (- n) t))
5851
5852 (defun gnus-summary-goto-subject (article &optional force silent)
5853   "Go the subject line of ARTICLE.
5854 If FORCE, also allow jumping to articles not currently shown."
5855   (interactive "nArticle number: ")
5856   (let ((b (point))
5857         (data (gnus-data-find article)))
5858     ;; We read in the article if we have to.
5859     (and (not data)
5860          force
5861          (gnus-summary-insert-subject
5862           article
5863           (if (or (numberp force) (vectorp force)) force)
5864           t)
5865          (setq data (gnus-data-find article)))
5866     (goto-char b)
5867     (if (not data)
5868         (progn
5869           (unless silent
5870             (gnus-message 3 "Can't find article %d" article))
5871           nil)
5872       (goto-char (gnus-data-pos data))
5873       (gnus-summary-position-point)
5874       article)))
5875
5876 ;; Walking around summary lines with displaying articles.
5877
5878 (defun gnus-summary-expand-window (&optional arg)
5879   "Make the summary buffer take up the entire Emacs frame.
5880 Given a prefix, will force an `article' buffer configuration."
5881   (interactive "P")
5882   (if arg
5883       (gnus-configure-windows 'article 'force)
5884     (gnus-configure-windows 'summary 'force)))
5885
5886 (defun gnus-summary-display-article (article &optional all-header)
5887   "Display ARTICLE in article buffer."
5888   (gnus-set-global-variables)
5889   (if (null article)
5890       nil
5891     (prog1
5892         (if gnus-summary-display-article-function
5893             (funcall gnus-summary-display-article-function article all-header)
5894           (gnus-article-prepare article all-header))
5895       (with-current-buffer gnus-article-buffer
5896         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5897              nil))
5898       (gnus-run-hooks 'gnus-select-article-hook)
5899       (when (and gnus-current-article
5900                  (not (zerop gnus-current-article)))
5901         (gnus-summary-goto-subject gnus-current-article))
5902       (gnus-summary-recenter)
5903       (when (and gnus-use-trees gnus-show-threads)
5904         (gnus-possibly-generate-tree article)
5905         (gnus-highlight-selected-tree article))
5906       ;; Successfully display article.
5907       (gnus-article-set-window-start
5908        (cdr (assq article gnus-newsgroup-bookmarks))))))
5909
5910 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5911   "Select the current article.
5912 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5913 non-nil, the article will be re-fetched even if it already present in
5914 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5915 be displayed."
5916   ;; Make sure we are in the summary buffer to work around bbdb bug.
5917   (unless (eq major-mode 'gnus-summary-mode)
5918     (set-buffer gnus-summary-buffer))
5919   (let ((article (or article (gnus-summary-article-number)))
5920         (all-headers (not (not all-headers))) ;Must be T or NIL.
5921         gnus-summary-display-article-function)
5922     (and (not pseudo)
5923          (gnus-summary-article-pseudo-p article)
5924          (error "This is a pseudo-article"))
5925     (save-excursion
5926       (set-buffer gnus-summary-buffer)
5927       (if (or (and gnus-single-article-buffer
5928                    (or (null gnus-current-article)
5929                        (null gnus-article-current)
5930                        (null (get-buffer gnus-article-buffer))
5931                        (not (eq article (cdr gnus-article-current)))
5932                        (not (equal (car gnus-article-current)
5933                                    gnus-newsgroup-name))))
5934               (and (not gnus-single-article-buffer)
5935                    (or (null gnus-current-article)
5936                        (not (eq gnus-current-article article))))
5937               force)
5938           ;; The requested article is different from the current article.
5939           (progn
5940             (gnus-summary-display-article article all-headers)
5941             (when (or all-headers gnus-show-all-headers)
5942               (gnus-article-show-all-headers))
5943             (gnus-article-set-window-start
5944              (cdr (assq article gnus-newsgroup-bookmarks)))
5945             article)
5946         (when (or all-headers gnus-show-all-headers)
5947           (gnus-article-show-all-headers))
5948         'old))))
5949
5950 (defun gnus-summary-set-current-mark (&optional current-mark)
5951   "Obsolete function."
5952   nil)
5953
5954 (defun gnus-summary-next-article (&optional unread subject backward push)
5955   "Select the next article.
5956 If UNREAD, only unread articles are selected.
5957 If SUBJECT, only articles with SUBJECT are selected.
5958 If BACKWARD, the previous article is selected instead of the next."
5959   (interactive "P")
5960   (cond
5961    ;; Is there such an article?
5962    ((and (gnus-summary-search-forward unread subject backward)
5963          (or (gnus-summary-display-article (gnus-summary-article-number))
5964              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5965     (gnus-summary-position-point))
5966    ;; If not, we try the first unread, if that is wanted.
5967    ((and subject
5968          gnus-auto-select-same
5969          (gnus-summary-first-unread-article))
5970     (gnus-summary-position-point)
5971     (gnus-message 6 "Wrapped"))
5972    ;; Try to get next/previous article not displayed in this group.
5973    ((and gnus-auto-extend-newsgroup
5974          (not unread) (not subject))
5975     (gnus-summary-goto-article
5976      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5977      nil (count-lines (point-min) (point))))
5978    ;; Go to next/previous group.
5979    (t
5980     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5981       (gnus-summary-jump-to-group gnus-newsgroup-name))
5982     (let ((cmd last-command-char)
5983           (point
5984            (save-excursion
5985              (set-buffer gnus-group-buffer)
5986              (point)))
5987           (group
5988            (if (eq gnus-keep-same-level 'best)
5989                (gnus-summary-best-group gnus-newsgroup-name)
5990              (gnus-summary-search-group backward gnus-keep-same-level))))
5991       ;; For some reason, the group window gets selected.  We change
5992       ;; it back.
5993       (select-window (get-buffer-window (current-buffer)))
5994       ;; Select next unread newsgroup automagically.
5995       (cond
5996        ((or (not gnus-auto-select-next)
5997             (not cmd))
5998         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5999        ((or (eq gnus-auto-select-next 'quietly)
6000             (and (eq gnus-auto-select-next 'slightly-quietly)
6001                  push)
6002             (and (eq gnus-auto-select-next 'almost-quietly)
6003                  (gnus-summary-last-article-p)))
6004         ;; Select quietly.
6005         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6006             (gnus-summary-exit)
6007           (gnus-message 7 "No more%s articles (%s)..."
6008                         (if unread " unread" "")
6009                         (if group (concat "selecting " group)
6010                           "exiting"))
6011           (gnus-summary-next-group nil group backward)))
6012        (t
6013         (when (gnus-key-press-event-p last-input-event)
6014           (gnus-summary-walk-group-buffer
6015            gnus-newsgroup-name cmd unread backward point))))))))
6016
6017 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6018   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6019                       (?\C-p (gnus-group-prev-unread-group 1))))
6020         (cursor-in-echo-area t)
6021         keve key group ended)
6022     (save-excursion
6023       (set-buffer gnus-group-buffer)
6024       (goto-char start)
6025       (setq group
6026             (if (eq gnus-keep-same-level 'best)
6027                 (gnus-summary-best-group gnus-newsgroup-name)
6028               (gnus-summary-search-group backward gnus-keep-same-level))))
6029     (while (not ended)
6030       (gnus-message
6031        5 "No more%s articles%s" (if unread " unread" "")
6032        (if (and group
6033                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6034            (format " (Type %s for %s [%s])"
6035                    (single-key-description cmd) group
6036                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6037          (format " (Type %s to exit %s)"
6038                  (single-key-description cmd)
6039                  gnus-newsgroup-name)))
6040       ;; Confirm auto selection.
6041       (setq key (car (setq keve (gnus-read-event-char))))
6042       (setq ended t)
6043       (cond
6044        ((assq key keystrokes)
6045         (let ((obuf (current-buffer)))
6046           (switch-to-buffer gnus-group-buffer)
6047           (when group
6048             (gnus-group-jump-to-group group))
6049           (eval (cadr (assq key keystrokes)))
6050           (setq group (gnus-group-group-name))
6051           (switch-to-buffer obuf))
6052         (setq ended nil))
6053        ((equal key cmd)
6054         (if (or (not group)
6055                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6056             (gnus-summary-exit)
6057           (gnus-summary-next-group nil group backward)))
6058        (t
6059         (push (cdr keve) unread-command-events))))))
6060
6061 (defun gnus-summary-next-unread-article ()
6062   "Select unread article after current one."
6063   (interactive)
6064   (gnus-summary-next-article
6065    (or (not (eq gnus-summary-goto-unread 'never))
6066        (gnus-summary-last-article-p (gnus-summary-article-number)))
6067    (and gnus-auto-select-same
6068         (gnus-summary-article-subject))))
6069
6070 (defun gnus-summary-prev-article (&optional unread subject)
6071   "Select the article after the current one.
6072 If UNREAD is non-nil, only unread articles are selected."
6073   (interactive "P")
6074   (gnus-summary-next-article unread subject t))
6075
6076 (defun gnus-summary-prev-unread-article ()
6077   "Select unread article before current one."
6078   (interactive)
6079   (gnus-summary-prev-article
6080    (or (not (eq gnus-summary-goto-unread 'never))
6081        (gnus-summary-first-article-p (gnus-summary-article-number)))
6082    (and gnus-auto-select-same
6083         (gnus-summary-article-subject))))
6084
6085 (defun gnus-summary-next-page (&optional lines circular)
6086   "Show next page of the selected article.
6087 If at the end of the current article, select the next article.
6088 LINES says how many lines should be scrolled up.
6089
6090 If CIRCULAR is non-nil, go to the start of the article instead of
6091 selecting the next article when reaching the end of the current
6092 article."
6093   (interactive "P")
6094   (setq gnus-summary-buffer (current-buffer))
6095   (gnus-set-global-variables)
6096   (let ((article (gnus-summary-article-number))
6097         (article-window (get-buffer-window gnus-article-buffer t))
6098         endp)
6099     ;; If the buffer is empty, we have no article.
6100     (unless article
6101       (error "No article to select"))
6102     (gnus-configure-windows 'article)
6103     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6104         (if (and (eq gnus-summary-goto-unread 'never)
6105                  (not (gnus-summary-last-article-p article)))
6106             (gnus-summary-next-article)
6107           (gnus-summary-next-unread-article))
6108       (if (or (null gnus-current-article)
6109               (null gnus-article-current)
6110               (/= article (cdr gnus-article-current))
6111               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6112           ;; Selected subject is different from current article's.
6113           (gnus-summary-display-article article)
6114         (when article-window
6115           (gnus-eval-in-buffer-window gnus-article-buffer
6116             (setq endp (gnus-article-next-page lines)))
6117           (when endp
6118             (cond (circular
6119                    (gnus-summary-beginning-of-article))
6120                   (lines
6121                    (gnus-message 3 "End of message"))
6122                   ((null lines)
6123                    (if (and (eq gnus-summary-goto-unread 'never)
6124                             (not (gnus-summary-last-article-p article)))
6125                        (gnus-summary-next-article)
6126                      (gnus-summary-next-unread-article))))))))
6127     (gnus-summary-recenter)
6128     (gnus-summary-position-point)))
6129
6130 (defun gnus-summary-prev-page (&optional lines move)
6131   "Show previous page of selected article.
6132 Argument LINES specifies lines to be scrolled down.
6133 If MOVE, move to the previous unread article if point is at
6134 the beginning of the buffer."
6135   (interactive "P")
6136   (let ((article (gnus-summary-article-number))
6137         (article-window (get-buffer-window gnus-article-buffer t))
6138         endp)
6139     (gnus-configure-windows 'article)
6140     (if (or (null gnus-current-article)
6141             (null gnus-article-current)
6142             (/= article (cdr gnus-article-current))
6143             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6144         ;; Selected subject is different from current article's.
6145         (gnus-summary-display-article article)
6146       (gnus-summary-recenter)
6147       (when article-window
6148         (gnus-eval-in-buffer-window gnus-article-buffer
6149           (setq endp (gnus-article-prev-page lines)))
6150         (when (and move endp)
6151           (cond (lines
6152                  (gnus-message 3 "Beginning of message"))
6153                 ((null lines)
6154                  (if (and (eq gnus-summary-goto-unread 'never)
6155                           (not (gnus-summary-first-article-p article)))
6156                      (gnus-summary-prev-article)
6157                    (gnus-summary-prev-unread-article))))))))
6158   (gnus-summary-position-point))
6159
6160 (defun gnus-summary-prev-page-or-article (&optional lines)
6161   "Show previous page of selected article.
6162 Argument LINES specifies lines to be scrolled down.
6163 If at the beginning of the article, go to the next article."
6164   (interactive "P")
6165   (gnus-summary-prev-page lines t))
6166
6167 (defun gnus-summary-scroll-up (lines)
6168   "Scroll up (or down) one line current article.
6169 Argument LINES specifies lines to be scrolled up (or down if negative)."
6170   (interactive "p")
6171   (gnus-configure-windows 'article)
6172   (gnus-summary-show-thread)
6173   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6174     (gnus-eval-in-buffer-window gnus-article-buffer
6175       (cond ((> lines 0)
6176              (when (gnus-article-next-page lines)
6177                (gnus-message 3 "End of message")))
6178             ((< lines 0)
6179              (gnus-article-prev-page (- lines))))))
6180   (gnus-summary-recenter)
6181   (gnus-summary-position-point))
6182
6183 (defun gnus-summary-scroll-down (lines)
6184   "Scroll down (or up) one line current article.
6185 Argument LINES specifies lines to be scrolled down (or up if negative)."
6186   (interactive "p")
6187   (gnus-summary-scroll-up (- lines)))
6188
6189 (defun gnus-summary-next-same-subject ()
6190   "Select next article which has the same subject as current one."
6191   (interactive)
6192   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6193
6194 (defun gnus-summary-prev-same-subject ()
6195   "Select previous article which has the same subject as current one."
6196   (interactive)
6197   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6198
6199 (defun gnus-summary-next-unread-same-subject ()
6200   "Select next unread article which has the same subject as current one."
6201   (interactive)
6202   (gnus-summary-next-article t (gnus-summary-article-subject)))
6203
6204 (defun gnus-summary-prev-unread-same-subject ()
6205   "Select previous unread article which has the same subject as current one."
6206   (interactive)
6207   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6208
6209 (defun gnus-summary-first-unread-article ()
6210   "Select the first unread article.
6211 Return nil if there are no unread articles."
6212   (interactive)
6213   (prog1
6214       (when (gnus-summary-first-subject t)
6215         (gnus-summary-show-thread)
6216         (gnus-summary-first-subject t)
6217         (gnus-summary-display-article (gnus-summary-article-number)))
6218     (gnus-summary-position-point)))
6219
6220 (defun gnus-summary-first-unread-subject ()
6221   "Place the point on the subject line of the first unread article.
6222 Return nil if there are no unread articles."
6223   (interactive)
6224   (prog1
6225       (when (gnus-summary-first-subject t)
6226         (gnus-summary-show-thread)
6227         (gnus-summary-first-subject t))
6228     (gnus-summary-position-point)))
6229
6230 (defun gnus-summary-first-article ()
6231   "Select the first article.
6232 Return nil if there are no articles."
6233   (interactive)
6234   (prog1
6235       (when (gnus-summary-first-subject)
6236         (gnus-summary-show-thread)
6237         (gnus-summary-first-subject)
6238         (gnus-summary-display-article (gnus-summary-article-number)))
6239     (gnus-summary-position-point)))
6240
6241 (defun gnus-summary-best-unread-article ()
6242   "Select the unread article with the highest score."
6243   (interactive)
6244   (let ((best -1000000)
6245         (data gnus-newsgroup-data)
6246         article score)
6247     (while data
6248       (and (gnus-data-unread-p (car data))
6249            (> (setq score
6250                     (gnus-summary-article-score (gnus-data-number (car data))))
6251               best)
6252            (setq best score
6253                  article (gnus-data-number (car data))))
6254       (setq data (cdr data)))
6255     (prog1
6256         (if article
6257             (gnus-summary-goto-article article)
6258           (error "No unread articles"))
6259       (gnus-summary-position-point))))
6260
6261 (defun gnus-summary-last-subject ()
6262   "Go to the last displayed subject line in the group."
6263   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6264     (when article
6265       (gnus-summary-goto-subject article))))
6266
6267 (defun gnus-summary-goto-article (article &optional all-headers force)
6268   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6269 If ALL-HEADERS is non-nil, no header lines are hidden.
6270 If FORCE, go to the article even if it isn't displayed.  If FORCE
6271 is a number, it is the line the article is to be displayed on."
6272   (interactive
6273    (list
6274     (completing-read
6275      "Article number or Message-ID: "
6276      (mapcar (lambda (number) (list (int-to-string number)))
6277              gnus-newsgroup-limit))
6278     current-prefix-arg
6279     t))
6280   (prog1
6281       (if (and (stringp article)
6282                (string-match "@" article))
6283           (gnus-summary-refer-article article)
6284         (when (stringp article)
6285           (setq article (string-to-number article)))
6286         (if (gnus-summary-goto-subject article force)
6287             (gnus-summary-display-article article all-headers)
6288           (gnus-message 4 "Couldn't go to article %s" article) nil))
6289     (gnus-summary-position-point)))
6290
6291 (defun gnus-summary-goto-last-article ()
6292   "Go to the previously read article."
6293   (interactive)
6294   (prog1
6295       (when gnus-last-article
6296         (gnus-summary-goto-article gnus-last-article nil t))
6297     (gnus-summary-position-point)))
6298
6299 (defun gnus-summary-pop-article (number)
6300   "Pop one article off the history and go to the previous.
6301 NUMBER articles will be popped off."
6302   (interactive "p")
6303   (let (to)
6304     (setq gnus-newsgroup-history
6305           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6306     (if to
6307         (gnus-summary-goto-article (car to) nil t)
6308       (error "Article history empty")))
6309   (gnus-summary-position-point))
6310
6311 ;; Summary commands and functions for limiting the summary buffer.
6312
6313 (defun gnus-summary-limit-to-articles (n)
6314   "Limit the summary buffer to the next N articles.
6315 If not given a prefix, use the process marked articles instead."
6316   (interactive "P")
6317   (prog1
6318       (let ((articles (gnus-summary-work-articles n)))
6319         (setq gnus-newsgroup-processable nil)
6320         (gnus-summary-limit articles))
6321     (gnus-summary-position-point)))
6322
6323 (defun gnus-summary-pop-limit (&optional total)
6324   "Restore the previous limit.
6325 If given a prefix, remove all limits."
6326   (interactive "P")
6327   (when total
6328     (setq gnus-newsgroup-limits
6329           (list (mapcar (lambda (h) (mail-header-number h))
6330                         gnus-newsgroup-headers))))
6331   (unless gnus-newsgroup-limits
6332     (error "No limit to pop"))
6333   (prog1
6334       (gnus-summary-limit nil 'pop)
6335     (gnus-summary-position-point)))
6336
6337 (defun gnus-summary-limit-to-subject (subject &optional header)
6338   "Limit the summary buffer to articles that have subjects that match a regexp."
6339   (interactive "sLimit to subject (regexp): ")
6340   (unless header
6341     (setq header "subject"))
6342   (when (not (equal "" subject))
6343     (prog1
6344         (let ((articles (gnus-summary-find-matching
6345                          (or header "subject") subject 'all)))
6346           (unless articles
6347             (error "Found no matches for \"%s\"" subject))
6348           (gnus-summary-limit articles))
6349       (gnus-summary-position-point))))
6350
6351 (defun gnus-summary-limit-to-author (from)
6352   "Limit the summary buffer to articles that have authors that match a regexp."
6353   (interactive "sLimit to author (regexp): ")
6354   (gnus-summary-limit-to-subject from "from"))
6355
6356 (defun gnus-summary-limit-to-age (age &optional younger-p)
6357   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6358 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6359 articles that are younger than AGE days."
6360   (interactive
6361    (let ((younger current-prefix-arg)
6362          (days-got nil)
6363          days)
6364      (while (not days-got)
6365        (setq days (if younger
6366                       (read-string "Limit to articles within (in days): ")
6367                     (read-string "Limit to articles old than (in days): ")))
6368        (when (> (length days) 0)
6369          (setq days (read days)))
6370        (if (numberp days)
6371            (setq days-got t)
6372          (message "Please enter a number.")
6373          (sleep-for 1)))
6374      (list days younger)))
6375   (prog1
6376       (let ((data gnus-newsgroup-data)
6377             (cutoff (days-to-time age))
6378             articles d date is-younger)
6379         (while (setq d (pop data))
6380           (when (and (vectorp (gnus-data-header d))
6381                      (setq date (mail-header-date (gnus-data-header d))))
6382             (setq is-younger (time-less-p
6383                               (time-since (condition-case ()
6384                                               (date-to-time date)
6385                                             (error '(0 0))))
6386                               cutoff))
6387             (when (if younger-p
6388                       is-younger
6389                     (not is-younger))
6390               (push (gnus-data-number d) articles))))
6391         (gnus-summary-limit (nreverse articles)))
6392     (gnus-summary-position-point)))
6393
6394 (defun gnus-summary-limit-to-extra (header regexp)
6395   "Limit the summary buffer to articles that match an 'extra' header."
6396   (interactive
6397    (let ((header
6398           (intern
6399            (gnus-completing-read
6400             (symbol-name (car gnus-extra-headers))
6401             "Limit extra header:"
6402             (mapcar (lambda (x)
6403                       (cons (symbol-name x) x))
6404                     gnus-extra-headers)
6405             nil
6406             t))))
6407      (list header
6408            (read-string (format "Limit to header %s (regexp): " header)))))
6409   (when (not (equal "" regexp))
6410     (prog1
6411         (let ((articles (gnus-summary-find-matching
6412                          (cons 'extra header) regexp 'all)))
6413           (unless articles
6414             (error "Found no matches for \"%s\"" regexp))
6415           (gnus-summary-limit articles))
6416       (gnus-summary-position-point))))
6417
6418 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6419 (make-obsolete
6420  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6421
6422 (defun gnus-summary-limit-to-unread (&optional all)
6423   "Limit the summary buffer to articles that are not marked as read.
6424 If ALL is non-nil, limit strictly to unread articles."
6425   (interactive "P")
6426   (if all
6427       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6428     (gnus-summary-limit-to-marks
6429      ;; Concat all the marks that say that an article is read and have
6430      ;; those removed.
6431      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6432            gnus-killed-mark gnus-kill-file-mark
6433            gnus-low-score-mark gnus-expirable-mark
6434            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6435            gnus-duplicate-mark gnus-souped-mark)
6436      'reverse)))
6437
6438 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6439 (make-obsolete 'gnus-summary-delete-marked-with
6440                'gnus-summary-limit-exlude-marks)
6441
6442 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6443   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6444 If REVERSE, limit the summary buffer to articles that are marked
6445 with MARKS.  MARKS can either be a string of marks or a list of marks.
6446 Returns how many articles were removed."
6447   (interactive "sMarks: ")
6448   (gnus-summary-limit-to-marks marks t))
6449
6450 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6451   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6452 If REVERSE (the prefix), limit the summary buffer to articles that are
6453 not marked with MARKS.  MARKS can either be a string of marks or a
6454 list of marks.
6455 Returns how many articles were removed."
6456   (interactive "sMarks: \nP")
6457   (prog1
6458       (let ((data gnus-newsgroup-data)
6459             (marks (if (listp marks) marks
6460                      (append marks nil))) ; Transform to list.
6461             articles)
6462         (while data
6463           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6464                   (memq (gnus-data-mark (car data)) marks))
6465             (push (gnus-data-number (car data)) articles))
6466           (setq data (cdr data)))
6467         (gnus-summary-limit articles))
6468     (gnus-summary-position-point)))
6469
6470 (defun gnus-summary-limit-to-score (&optional score)
6471   "Limit to articles with score at or above SCORE."
6472   (interactive "P")
6473   (setq score (if score
6474                   (prefix-numeric-value score)
6475                 (or gnus-summary-default-score 0)))
6476   (let ((data gnus-newsgroup-data)
6477         articles)
6478     (while data
6479       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6480                 score)
6481         (push (gnus-data-number (car data)) articles))
6482       (setq data (cdr data)))
6483     (prog1
6484         (gnus-summary-limit articles)
6485       (gnus-summary-position-point))))
6486
6487 (defun gnus-summary-limit-include-thread (id)
6488   "Display all the hidden articles that in the current thread."
6489   (interactive (list (mail-header-id (gnus-summary-article-header))))
6490   (let ((articles (gnus-articles-in-thread
6491                    (gnus-id-to-thread (gnus-root-id id)))))
6492     (prog1
6493         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6494       (gnus-summary-position-point))))
6495
6496 (defun gnus-summary-limit-include-dormant ()
6497   "Display all the hidden articles that are marked as dormant.
6498 Note that this command only works on a subset of the articles currently
6499 fetched for this group."
6500   (interactive)
6501   (unless gnus-newsgroup-dormant
6502     (error "There are no dormant articles in this group"))
6503   (prog1
6504       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6505     (gnus-summary-position-point)))
6506
6507 (defun gnus-summary-limit-exclude-dormant ()
6508   "Hide all dormant articles."
6509   (interactive)
6510   (prog1
6511       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6512     (gnus-summary-position-point)))
6513
6514 (defun gnus-summary-limit-exclude-childless-dormant ()
6515   "Hide all dormant articles that have no children."
6516   (interactive)
6517   (let ((data (gnus-data-list t))
6518         articles d children)
6519     ;; Find all articles that are either not dormant or have
6520     ;; children.
6521     (while (setq d (pop data))
6522       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6523                 (and (setq children
6524                            (gnus-article-children (gnus-data-number d)))
6525                      (let (found)
6526                        (while children
6527                          (when (memq (car children) articles)
6528                            (setq children nil
6529                                  found t))
6530                          (pop children))
6531                        found)))
6532         (push (gnus-data-number d) articles)))
6533     ;; Do the limiting.
6534     (prog1
6535         (gnus-summary-limit articles)
6536       (gnus-summary-position-point))))
6537
6538 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6539   "Mark all unread excluded articles as read.
6540 If ALL, mark even excluded ticked and dormants as read."
6541   (interactive "P")
6542   (let ((articles (gnus-sorted-complement
6543                    (sort
6544                     (mapcar (lambda (h) (mail-header-number h))
6545                             gnus-newsgroup-headers)
6546                     '<)
6547                    (sort gnus-newsgroup-limit '<)))
6548         article)
6549     (setq gnus-newsgroup-unreads
6550           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6551     (if all
6552         (setq gnus-newsgroup-dormant nil
6553               gnus-newsgroup-marked nil
6554               gnus-newsgroup-reads
6555               (nconc
6556                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6557                gnus-newsgroup-reads))
6558       (while (setq article (pop articles))
6559         (unless (or (memq article gnus-newsgroup-dormant)
6560                     (memq article gnus-newsgroup-marked))
6561           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6562
6563 (defun gnus-summary-limit (articles &optional pop)
6564   (if pop
6565       ;; We pop the previous limit off the stack and use that.
6566       (setq articles (car gnus-newsgroup-limits)
6567             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6568     ;; We use the new limit, so we push the old limit on the stack.
6569     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6570   ;; Set the limit.
6571   (setq gnus-newsgroup-limit articles)
6572   (let ((total (length gnus-newsgroup-data))
6573         (data (gnus-data-find-list (gnus-summary-article-number)))
6574         (gnus-summary-mark-below nil)   ; Inhibit this.
6575         found)
6576     ;; This will do all the work of generating the new summary buffer
6577     ;; according to the new limit.
6578     (gnus-summary-prepare)
6579     ;; Hide any threads, possibly.
6580     (and gnus-show-threads
6581          gnus-thread-hide-subtree
6582          (gnus-summary-hide-all-threads))
6583     ;; Try to return to the article you were at, or one in the
6584     ;; neighborhood.
6585     (when data
6586       ;; We try to find some article after the current one.
6587       (while data
6588         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6589           (setq data nil
6590                 found t))
6591         (setq data (cdr data))))
6592     (unless found
6593       ;; If there is no data, that means that we were after the last
6594       ;; article.  The same goes when we can't find any articles
6595       ;; after the current one.
6596       (goto-char (point-max))
6597       (gnus-summary-find-prev))
6598     (gnus-set-mode-line 'summary)
6599     ;; We return how many articles were removed from the summary
6600     ;; buffer as a result of the new limit.
6601     (- total (length gnus-newsgroup-data))))
6602
6603 (defsubst gnus-invisible-cut-children (threads)
6604   (let ((num 0))
6605     (while threads
6606       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6607         (incf num))
6608       (pop threads))
6609     (< num 2)))
6610
6611 (defsubst gnus-cut-thread (thread)
6612   "Go forwards in the thread until we find an article that we want to display."
6613   (when (or (eq gnus-fetch-old-headers 'some)
6614             (eq gnus-fetch-old-headers 'invisible)
6615             (numberp gnus-fetch-old-headers)
6616             (eq gnus-build-sparse-threads 'some)
6617             (eq gnus-build-sparse-threads 'more))
6618     ;; Deal with old-fetched headers and sparse threads.
6619     (while (and
6620             thread
6621             (or
6622              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6623              (gnus-summary-article-ancient-p
6624               (mail-header-number (car thread))))
6625             (if (or (<= (length (cdr thread)) 1)
6626                     (eq gnus-fetch-old-headers 'invisible))
6627                 (setq gnus-newsgroup-limit
6628                       (delq (mail-header-number (car thread))
6629                             gnus-newsgroup-limit)
6630                       thread (cadr thread))
6631               (when (gnus-invisible-cut-children (cdr thread))
6632                 (let ((th (cdr thread)))
6633                   (while th
6634                     (if (memq (mail-header-number (caar th))
6635                               gnus-newsgroup-limit)
6636                         (setq thread (car th)
6637                               th nil)
6638                       (setq th (cdr th))))))))))
6639   thread)
6640
6641 (defun gnus-cut-threads (threads)
6642   "Cut off all uninteresting articles from the beginning of threads."
6643   (when (or (eq gnus-fetch-old-headers 'some)
6644             (eq gnus-fetch-old-headers 'invisible)
6645             (numberp gnus-fetch-old-headers)
6646             (eq gnus-build-sparse-threads 'some)
6647             (eq gnus-build-sparse-threads 'more))
6648     (let ((th threads))
6649       (while th
6650         (setcar th (gnus-cut-thread (car th)))
6651         (setq th (cdr th)))))
6652   ;; Remove nixed out threads.
6653   (delq nil threads))
6654
6655 (defun gnus-summary-initial-limit (&optional show-if-empty)
6656   "Figure out what the initial limit is supposed to be on group entry.
6657 This entails weeding out unwanted dormants, low-scored articles,
6658 fetch-old-headers verbiage, and so on."
6659   ;; Most groups have nothing to remove.
6660   (if (or gnus-inhibit-limiting
6661           (and (null gnus-newsgroup-dormant)
6662                (not (eq gnus-fetch-old-headers 'some))
6663                (not (numberp gnus-fetch-old-headers))
6664                (not (eq gnus-fetch-old-headers 'invisible))
6665                (null gnus-summary-expunge-below)
6666                (not (eq gnus-build-sparse-threads 'some))
6667                (not (eq gnus-build-sparse-threads 'more))
6668                (null gnus-thread-expunge-below)
6669                (not gnus-use-nocem)))
6670       ()                                ; Do nothing.
6671     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6672     (setq gnus-newsgroup-limit nil)
6673     (mapatoms
6674      (lambda (node)
6675        (unless (car (symbol-value node))
6676          ;; These threads have no parents -- they are roots.
6677          (let ((nodes (cdr (symbol-value node)))
6678                thread)
6679            (while nodes
6680              (if (and gnus-thread-expunge-below
6681                       (< (gnus-thread-total-score (car nodes))
6682                          gnus-thread-expunge-below))
6683                  (gnus-expunge-thread (pop nodes))
6684                (setq thread (pop nodes))
6685                (gnus-summary-limit-children thread))))))
6686      gnus-newsgroup-dependencies)
6687     ;; If this limitation resulted in an empty group, we might
6688     ;; pop the previous limit and use it instead.
6689     (when (and (not gnus-newsgroup-limit)
6690                show-if-empty)
6691       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6692     gnus-newsgroup-limit))
6693
6694 (defun gnus-summary-limit-children (thread)
6695   "Return 1 if this subthread is visible and 0 if it is not."
6696   ;; First we get the number of visible children to this thread.  This
6697   ;; is done by recursing down the thread using this function, so this
6698   ;; will really go down to a leaf article first, before slowly
6699   ;; working its way up towards the root.
6700   (when thread
6701     (let ((children
6702            (if (cdr thread)
6703                (apply '+ (mapcar 'gnus-summary-limit-children
6704                                  (cdr thread)))
6705              0))
6706           (number (mail-header-number (car thread)))
6707           score)
6708       (if (and
6709            (not (memq number gnus-newsgroup-marked))
6710            (or
6711             ;; If this article is dormant and has absolutely no visible
6712             ;; children, then this article isn't visible.
6713             (and (memq number gnus-newsgroup-dormant)
6714                  (zerop children))
6715             ;; If this is "fetch-old-headered" and there is no
6716             ;; visible children, then we don't want this article.
6717             (and (or (eq gnus-fetch-old-headers 'some)
6718                      (numberp gnus-fetch-old-headers))
6719                  (gnus-summary-article-ancient-p number)
6720                  (zerop children))
6721             ;; If this is "fetch-old-headered" and `invisible', then
6722             ;; we don't want this article.
6723             (and (eq gnus-fetch-old-headers 'invisible)
6724                  (gnus-summary-article-ancient-p number))
6725             ;; If this is a sparsely inserted article with no children,
6726             ;; we don't want it.
6727             (and (eq gnus-build-sparse-threads 'some)
6728                  (gnus-summary-article-sparse-p number)
6729                  (zerop children))
6730             ;; If we use expunging, and this article is really
6731             ;; low-scored, then we don't want this article.
6732             (when (and gnus-summary-expunge-below
6733                        (< (setq score
6734                                 (or (cdr (assq number gnus-newsgroup-scored))
6735                                     gnus-summary-default-score))
6736                           gnus-summary-expunge-below))
6737               ;; We increase the expunge-tally here, but that has
6738               ;; nothing to do with the limits, really.
6739               (incf gnus-newsgroup-expunged-tally)
6740               ;; We also mark as read here, if that's wanted.
6741               (when (and gnus-summary-mark-below
6742                          (< score gnus-summary-mark-below))
6743                 (setq gnus-newsgroup-unreads
6744                       (delq number gnus-newsgroup-unreads))
6745                 (if gnus-newsgroup-auto-expire
6746                     (push number gnus-newsgroup-expirable)
6747                   (push (cons number gnus-low-score-mark)
6748                         gnus-newsgroup-reads)))
6749               t)
6750             ;; Check NoCeM things.
6751             (if (and gnus-use-nocem
6752                      (gnus-nocem-unwanted-article-p
6753                       (mail-header-id (car thread))))
6754                 (progn
6755                   (setq gnus-newsgroup-unreads
6756                         (delq number gnus-newsgroup-unreads))
6757                   t))))
6758           ;; Nope, invisible article.
6759           0
6760         ;; Ok, this article is to be visible, so we add it to the limit
6761         ;; and return 1.
6762         (push number gnus-newsgroup-limit)
6763         1))))
6764
6765 (defun gnus-expunge-thread (thread)
6766   "Mark all articles in THREAD as read."
6767   (let* ((number (mail-header-number (car thread))))
6768     (incf gnus-newsgroup-expunged-tally)
6769     ;; We also mark as read here, if that's wanted.
6770     (setq gnus-newsgroup-unreads
6771           (delq number gnus-newsgroup-unreads))
6772     (if gnus-newsgroup-auto-expire
6773         (push number gnus-newsgroup-expirable)
6774       (push (cons number gnus-low-score-mark)
6775             gnus-newsgroup-reads)))
6776   ;; Go recursively through all subthreads.
6777   (mapcar 'gnus-expunge-thread (cdr thread)))
6778
6779 ;; Summary article oriented commands
6780
6781 (defun gnus-summary-refer-parent-article (n)
6782   "Refer parent article N times.
6783 If N is negative, go to ancestor -N instead.
6784 The difference between N and the number of articles fetched is returned."
6785   (interactive "p")
6786   (let ((skip 1)
6787         error header ref)
6788     (when (not (natnump n))
6789       (setq skip (abs n)
6790             n 1))
6791     (while (and (> n 0)
6792                 (not error))
6793       (setq header (gnus-summary-article-header))
6794       (if (and (eq (mail-header-number header)
6795                    (cdr gnus-article-current))
6796                (equal gnus-newsgroup-name
6797                       (car gnus-article-current)))
6798           ;; If we try to find the parent of the currently
6799           ;; displayed article, then we take a look at the actual
6800           ;; References header, since this is slightly more
6801           ;; reliable than the References field we got from the
6802           ;; server.
6803           (save-excursion
6804             (set-buffer gnus-original-article-buffer)
6805             (nnheader-narrow-to-headers)
6806             (unless (setq ref (message-fetch-field "references"))
6807               (setq ref (message-fetch-field "in-reply-to")))
6808             (widen))
6809         (setq ref
6810               ;; It's not the current article, so we take a bet on
6811               ;; the value we got from the server.
6812               (mail-header-references header)))
6813       (if (and ref
6814                (not (equal ref "")))
6815           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6816             (gnus-message 1 "Couldn't find parent"))
6817         (gnus-message 1 "No references in article %d"
6818                       (gnus-summary-article-number))
6819         (setq error t))
6820       (decf n))
6821     (gnus-summary-position-point)
6822     n))
6823
6824 (defun gnus-summary-refer-references ()
6825   "Fetch all articles mentioned in the References header.
6826 Return the number of articles fetched."
6827   (interactive)
6828   (let ((ref (mail-header-references (gnus-summary-article-header)))
6829         (current (gnus-summary-article-number))
6830         (n 0))
6831     (if (or (not ref)
6832             (equal ref ""))
6833         (error "No References in the current article")
6834       ;; For each Message-ID in the References header...
6835       (while (string-match "<[^>]*>" ref)
6836         (incf n)
6837         ;; ... fetch that article.
6838         (gnus-summary-refer-article
6839          (prog1 (match-string 0 ref)
6840            (setq ref (substring ref (match-end 0))))))
6841       (gnus-summary-goto-subject current)
6842       (gnus-summary-position-point)
6843       n)))
6844
6845 (defun gnus-summary-refer-thread (&optional limit)
6846   "Fetch all articles in the current thread.
6847 If LIMIT (the numerical prefix), fetch that many old headers instead
6848 of what's specified by the `gnus-refer-thread-limit' variable."
6849   (interactive "P")
6850   (let ((id (mail-header-id (gnus-summary-article-header)))
6851         (limit (if limit (prefix-numeric-value limit)
6852                  gnus-refer-thread-limit)))
6853     ;; We want to fetch LIMIT *old* headers, but we also have to
6854     ;; re-fetch all the headers in the current buffer, because many of
6855     ;; them may be undisplayed.  So we adjust LIMIT.
6856     (when (numberp limit)
6857       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6858     (unless (eq gnus-fetch-old-headers 'invisible)
6859       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6860       ;; Retrieve the headers and read them in.
6861       (if (eq (gnus-retrieve-headers
6862                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6863               'nov)
6864           (gnus-build-all-threads)
6865         (error "Can't fetch thread from backends that don't support NOV"))
6866       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6867     (gnus-summary-limit-include-thread id)))
6868
6869 (defun gnus-summary-refer-article (message-id)
6870   "Fetch an article specified by MESSAGE-ID."
6871   (interactive "sMessage-ID: ")
6872   (when (and (stringp message-id)
6873              (not (zerop (length message-id))))
6874     ;; Construct the correct Message-ID if necessary.
6875     ;; Suggested by tale@pawl.rpi.edu.
6876     (unless (string-match "^<" message-id)
6877       (setq message-id (concat "<" message-id)))
6878     (unless (string-match ">$" message-id)
6879       (setq message-id (concat message-id ">")))
6880     (let* ((header (gnus-id-to-header message-id))
6881            (sparse (and header
6882                         (gnus-summary-article-sparse-p
6883                          (mail-header-number header))
6884                         (memq (mail-header-number header)
6885                               gnus-newsgroup-limit)))
6886            number)
6887       (cond
6888        ;; If the article is present in the buffer we just go to it.
6889        ((and header
6890              (or (not (gnus-summary-article-sparse-p
6891                        (mail-header-number header)))
6892                  sparse))
6893         (prog1
6894             (gnus-summary-goto-article
6895              (mail-header-number header) nil t)
6896           (when sparse
6897             (gnus-summary-update-article (mail-header-number header)))))
6898        (t
6899         ;; We fetch the article.
6900         (catch 'found
6901           (dolist (gnus-override-method (gnus-refer-article-methods))
6902             (gnus-check-server gnus-override-method)
6903             ;; Fetch the header, and display the article.
6904             (when (setq number (gnus-summary-insert-subject message-id))
6905               (gnus-summary-select-article nil nil nil number)
6906               (throw 'found t)))
6907           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6908
6909 (defun gnus-refer-article-methods ()
6910   "Return a list of referrable methods."
6911   (cond
6912    ;; No method, so we default to current and native.
6913    ((null gnus-refer-article-method)
6914     (list gnus-current-select-method gnus-select-method))
6915    ;; Current.
6916    ((eq 'current gnus-refer-article-method)
6917     (list gnus-current-select-method))
6918    ;; List of select methods.
6919    ((not (and (symbolp (car gnus-refer-article-method))
6920               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6921     (let (out)
6922       (dolist (method gnus-refer-article-method)
6923         (push (if (eq 'current method)
6924                   gnus-current-select-method
6925                 method)
6926               out))
6927       (nreverse out)))
6928    ;; One single select method.
6929    (t
6930     (list gnus-refer-article-method))))
6931
6932 (defun gnus-summary-edit-parameters ()
6933   "Edit the group parameters of the current group."
6934   (interactive)
6935   (gnus-group-edit-group gnus-newsgroup-name 'params))
6936
6937 (defun gnus-summary-customize-parameters ()
6938   "Customize the group parameters of the current group."
6939   (interactive)
6940   (gnus-group-customize gnus-newsgroup-name))
6941
6942 (defun gnus-summary-enter-digest-group (&optional force)
6943   "Enter an nndoc group based on the current article.
6944 If FORCE, force a digest interpretation.  If not, try
6945 to guess what the document format is."
6946   (interactive "P")
6947   (let ((conf gnus-current-window-configuration))
6948     (save-excursion
6949       (gnus-summary-select-article))
6950     (setq gnus-current-window-configuration conf)
6951     (let* ((name (format "%s-%d"
6952                          (gnus-group-prefixed-name
6953                           gnus-newsgroup-name (list 'nndoc ""))
6954                          (save-excursion
6955                            (set-buffer gnus-summary-buffer)
6956                            gnus-current-article)))
6957            (ogroup gnus-newsgroup-name)
6958            (params (append (gnus-info-params (gnus-get-info ogroup))
6959                            (list (cons 'to-group ogroup))
6960                            (list (cons 'save-article-group ogroup))))
6961            (case-fold-search t)
6962            (buf (current-buffer))
6963            dig to-address)
6964       (save-excursion
6965         (set-buffer gnus-original-article-buffer)
6966         ;; Have the digest group inherit the main mail address of
6967         ;; the parent article.
6968         (when (setq to-address (or (message-fetch-field "reply-to")
6969                                    (message-fetch-field "from")))
6970           (setq params (append 
6971                         (list (cons 'to-address 
6972                                     (funcall gnus-decode-encoded-word-function
6973                                              to-address))))))
6974         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6975         (insert-buffer-substring gnus-original-article-buffer)
6976         ;; Remove lines that may lead nndoc to misinterpret the
6977         ;; document type.
6978         (narrow-to-region
6979          (goto-char (point-min))
6980          (or (search-forward "\n\n" nil t) (point)))
6981         (goto-char (point-min))
6982         (delete-matching-lines "^Path:\\|^From ")
6983         (widen))
6984       (unwind-protect
6985           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6986                     (gnus-newsgroup-ephemeral-ignored-charsets
6987                      gnus-newsgroup-ignored-charsets))
6988                 (gnus-group-read-ephemeral-group
6989                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6990                               (nndoc-article-type
6991                                ,(if force 'mbox 'guess))) t))
6992               ;; Make all postings to this group go to the parent group.
6993               (nconc (gnus-info-params (gnus-get-info name))
6994                      params)
6995             ;; Couldn't select this doc group.
6996             (switch-to-buffer buf)
6997             (gnus-set-global-variables)
6998             (gnus-configure-windows 'summary)
6999             (gnus-message 3 "Article couldn't be entered?"))
7000         (kill-buffer dig)))))
7001
7002 (defun gnus-summary-read-document (n)
7003   "Open a new group based on the current article(s).
7004 This will allow you to read digests and other similar
7005 documents as newsgroups.
7006 Obeys the standard process/prefix convention."
7007   (interactive "P")
7008   (let* ((articles (gnus-summary-work-articles n))
7009          (ogroup gnus-newsgroup-name)
7010          (params (append (gnus-info-params (gnus-get-info ogroup))
7011                          (list (cons 'to-group ogroup))))
7012          article group egroup groups vgroup)
7013     (while (setq article (pop articles))
7014       (setq group (format "%s-%d" gnus-newsgroup-name article))
7015       (gnus-summary-remove-process-mark article)
7016       (when (gnus-summary-display-article article)
7017         (save-excursion
7018           (with-temp-buffer
7019             (insert-buffer-substring gnus-original-article-buffer)
7020             ;; Remove some headers that may lead nndoc to make
7021             ;; the wrong guess.
7022             (message-narrow-to-head)
7023             (goto-char (point-min))
7024             (delete-matching-lines "^\\(Path\\):\\|^From ")
7025             (widen)
7026             (if (setq egroup
7027                       (gnus-group-read-ephemeral-group
7028                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7029                                      (nndoc-article-type guess))
7030                        t nil t))
7031                 (progn
7032                   ;; Make all postings to this group go to the parent group.
7033                   (nconc (gnus-info-params (gnus-get-info egroup))
7034                          params)
7035                   (push egroup groups))
7036               ;; Couldn't select this doc group.
7037               (gnus-error 3 "Article couldn't be entered"))))))
7038     ;; Now we have selected all the documents.
7039     (cond
7040      ((not groups)
7041       (error "None of the articles could be interpreted as documents"))
7042      ((gnus-group-read-ephemeral-group
7043        (setq vgroup (format
7044                      "nnvirtual:%s-%s" gnus-newsgroup-name
7045                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7046        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7047        t
7048        (cons (current-buffer) 'summary)))
7049      (t
7050       (error "Couldn't select virtual nndoc group")))))
7051
7052 (defun gnus-summary-isearch-article (&optional regexp-p)
7053   "Do incremental search forward on the current article.
7054 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7055   (interactive "P")
7056   (let* ((gnus-inhibit-treatment t)
7057          (old (gnus-summary-select-article)))
7058     (gnus-configure-windows 'article)
7059     (gnus-eval-in-buffer-window gnus-article-buffer
7060       (save-restriction
7061         (widen)
7062         (when (eq 'old old)
7063           (gnus-article-show-all-headers))
7064         (goto-char (point-min))
7065         (isearch-forward regexp-p)))))
7066
7067 (defun gnus-summary-search-article-forward (regexp &optional backward)
7068   "Search for an article containing REGEXP forward.
7069 If BACKWARD, search backward instead."
7070   (interactive
7071    (list (read-string
7072           (format "Search article %s (regexp%s): "
7073                   (if current-prefix-arg "backward" "forward")
7074                   (if gnus-last-search-regexp
7075                       (concat ", default " gnus-last-search-regexp)
7076                     "")))
7077          current-prefix-arg))
7078   (if (string-equal regexp "")
7079       (setq regexp (or gnus-last-search-regexp ""))
7080     (setq gnus-last-search-regexp regexp))
7081   (if (gnus-summary-search-article regexp backward)
7082       (gnus-summary-show-thread)
7083     (error "Search failed: \"%s\"" regexp)))
7084
7085 (defun gnus-summary-search-article-backward (regexp)
7086   "Search for an article containing REGEXP backward."
7087   (interactive
7088    (list (read-string
7089           (format "Search article backward (regexp%s): "
7090                   (if gnus-last-search-regexp
7091                       (concat ", default " gnus-last-search-regexp)
7092                     "")))))
7093   (gnus-summary-search-article-forward regexp 'backward))
7094
7095 (eval-when-compile
7096   (defmacro gnus-summary-search-article-position-point (regexp backward)
7097     "Dehighlight the last matched text and goto the beginning position."
7098     (` (if (and gnus-summary-search-article-matched-data
7099                 (let ((text (caddr gnus-summary-search-article-matched-data))
7100                       (inhibit-read-only t)
7101                       buffer-read-only)
7102                   (delete-region
7103                    (goto-char (car gnus-summary-search-article-matched-data))
7104                    (cadr gnus-summary-search-article-matched-data))
7105                   (insert text)
7106                   (string-match (, regexp) text)))
7107            (if (, backward) (beginning-of-line) (end-of-line))
7108          (goto-char (if (, backward) (point-max) (point-min))))))
7109
7110   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7111     "Place point where X-Face image is displayed."
7112     (if (featurep 'xemacs)
7113         (` (let ((end (if (search-forward "\n\n" nil t)
7114                           (goto-char (1- (point)))
7115                         (point-min)))
7116                  extent)
7117              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7118              (unless (and (re-search-forward "^From:" end t)
7119                           (setq extent (extent-at (point)))
7120                           (extent-begin-glyph extent))
7121                (goto-char (, opoint)))))
7122       (` (let ((end (if (search-forward "\n\n" nil t)
7123                         (goto-char (1- (point)))
7124                       (point-min))))
7125            (goto-char
7126             (or (text-property-any (or (search-backward "\n\n" nil t)
7127                                        (point-min))
7128                                    end 'x-face-mule-bitmap-image t)
7129                 (, opoint)))))))
7130
7131   (defmacro gnus-summary-search-article-highlight-matched-text
7132     (backward treated x-face)
7133     "Highlight matched text in the function `gnus-summary-search-article'."
7134     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7135              (end (set-marker (make-marker) (match-end 0)))
7136              (inhibit-read-only t)
7137              buffer-read-only)
7138          (unless treated
7139            (let ((,@
7140                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7141                     (mapcar
7142                      (lambda (item) (setq items (delq item items)))
7143                      '(gnus-treat-buttonize
7144                        gnus-treat-fill-article
7145                        gnus-treat-fill-long-lines
7146                        gnus-treat-emphasize
7147                        gnus-treat-highlight-headers
7148                        gnus-treat-highlight-citation
7149                        gnus-treat-highlight-signature
7150                        gnus-treat-overstrike
7151                        gnus-treat-display-xface
7152                        gnus-treat-buttonize-head
7153                        gnus-treat-decode-article-as-default-mime-charset))
7154                     (static-if (featurep 'xemacs)
7155                         items
7156                       (cons '(x-face-mule-delete-x-face-field
7157                               (quote never))
7158                             items))))
7159                  (gnus-treat-display-xface
7160                   (when (, x-face) gnus-treat-display-xface)))
7161              (gnus-article-prepare-mime-display)))
7162          (goto-char (if (, backward) start end))
7163          (when (, x-face)
7164            (gnus-summary-search-article-highlight-goto-x-face (point)))
7165          (setq gnus-summary-search-article-matched-data
7166                (list start end (buffer-substring start end)))
7167          (unless (eq start end);; matched text has been deleted. :-<
7168            (put-text-property start end 'face
7169                               (or (find-face 'isearch)
7170                                   'secondary-selection))))))
7171   )
7172
7173 (defun gnus-summary-search-article (regexp &optional backward)
7174   "Search for an article containing REGEXP.
7175 Optional argument BACKWARD means do search for backward.
7176 `gnus-select-article-hook' is not called during the search."
7177   ;; We have to require this here to make sure that the following
7178   ;; dynamic binding isn't shadowed by autoloading.
7179   (require 'gnus-async)
7180   (require 'gnus-art)
7181   (let ((gnus-select-article-hook nil)  ;Disable hook.
7182         (gnus-article-display-hook nil)
7183         (gnus-article-prepare-hook nil)
7184         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7185         (gnus-use-article-prefetch nil)
7186         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7187         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7188         (sum (current-buffer))
7189         (found nil)
7190         point treated)
7191     (gnus-save-hidden-threads
7192       (static-if (featurep 'xemacs)
7193           (let ((gnus-inhibit-treatment t))
7194             (setq treated (eq 'old (gnus-summary-select-article)))
7195             (when (and treated
7196                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7197                                  (window-live-p (get-buffer-window
7198                                                  gnus-article-buffer t)))))
7199               (gnus-summary-select-article nil t)
7200               (setq treated nil)))
7201         (let ((gnus-inhibit-treatment t)
7202               (x-face-mule-delete-x-face-field 'never))
7203           (setq treated (eq 'old (gnus-summary-select-article)))
7204           (when (and treated
7205                      (not
7206                       (and (gnus-buffer-live-p gnus-article-buffer)
7207                            (window-live-p (get-buffer-window
7208                                            gnus-article-buffer t))
7209                            (or (not (string-match "^\\^X-Face:" regexp))
7210                                (with-current-buffer gnus-article-buffer
7211                                  gnus-summary-search-article-matched-data)))))
7212             (gnus-summary-select-article nil t)
7213             (setq treated nil))))
7214       (set-buffer gnus-article-buffer)
7215       (widen)
7216       (if treated
7217           (progn
7218             (gnus-article-show-all-headers)
7219             (gnus-summary-search-article-position-point regexp backward))
7220         (goto-char (if backward (point-max) (point-min))))
7221       (while (not found)
7222         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7223         (if (if backward
7224                 (re-search-backward regexp nil t)
7225               (re-search-forward regexp nil t))
7226             ;; We found the regexp.
7227             (progn
7228               (gnus-summary-search-article-highlight-matched-text
7229                backward treated (string-match "^\\^X-Face:" regexp))
7230               (setq found 'found)
7231               (forward-line
7232                (/ (- 2 (window-height
7233                         (get-buffer-window gnus-article-buffer t)))
7234                   2))
7235               (set-window-start
7236                (get-buffer-window (current-buffer))
7237                (point))
7238               (set-buffer sum)
7239               (setq point (point)))
7240           ;; We didn't find it, so we go to the next article.
7241           (set-buffer sum)
7242           (setq found 'not)
7243           (while (eq found 'not)
7244             (if (not (if backward (gnus-summary-find-prev)
7245                        (gnus-summary-find-next)))
7246                 ;; No more articles.
7247                 (setq found t)
7248               ;; Select the next article and adjust point.
7249               (unless (gnus-summary-article-sparse-p
7250                        (gnus-summary-article-number))
7251                 (setq found nil)
7252                 (let ((gnus-inhibit-treatment t))
7253                   (gnus-summary-select-article))
7254                 (setq treated nil)
7255                 (set-buffer gnus-article-buffer)
7256                 (widen)
7257                 (goto-char (if backward (point-max) (point-min))))))))
7258       (gnus-message 7 ""))
7259     ;; Return whether we found the regexp.
7260     (when (eq found 'found)
7261       (goto-char point)
7262       (gnus-summary-show-thread)
7263       (gnus-summary-goto-subject gnus-current-article)
7264       (gnus-summary-position-point)
7265       t)))
7266
7267 (defun gnus-summary-find-matching (header regexp &optional backward unread
7268                                           not-case-fold)
7269   "Return a list of all articles that match REGEXP on HEADER.
7270 The search stars on the current article and goes forwards unless
7271 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7272 If UNREAD is non-nil, only unread articles will
7273 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7274 in the comparisons."
7275   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7276                 (gnus-data-find-list
7277                  (gnus-summary-article-number) (gnus-data-list backward))))
7278         (case-fold-search (not not-case-fold))
7279         articles d func)
7280     (if (consp header)
7281         (if (eq (car header) 'extra)
7282             (setq func
7283                   `(lambda (h)
7284                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7285                          "")))
7286           (error "%s is an invalid header" header))
7287       (unless (fboundp (intern (concat "mail-header-" header)))
7288         (error "%s is not a valid header" header))
7289       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7290     (while data
7291       (setq d (car data))
7292       (and (or (not unread)             ; We want all articles...
7293                (gnus-data-unread-p d))  ; Or just unreads.
7294            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7295            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7296            (push (gnus-data-number d) articles)) ; Success!
7297       (setq data (cdr data)))
7298     (nreverse articles)))
7299
7300 (defun gnus-summary-execute-command (header regexp command &optional backward)
7301   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7302 If HEADER is an empty string (or nil), the match is done on the entire
7303 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7304   (interactive
7305    (list (let ((completion-ignore-case t))
7306            (completing-read
7307             "Header name: "
7308             (mapcar (lambda (string) (list string))
7309                     '("Number" "Subject" "From" "Lines" "Date"
7310                       "Message-ID" "Xref" "References" "Body"))
7311             nil 'require-match))
7312          (read-string "Regexp: ")
7313          (read-key-sequence "Command: ")
7314          current-prefix-arg))
7315   (when (equal header "Body")
7316     (setq header ""))
7317   ;; Hidden thread subtrees must be searched as well.
7318   (gnus-summary-show-all-threads)
7319   ;; We don't want to change current point nor window configuration.
7320   (save-excursion
7321     (save-window-excursion
7322       (gnus-message 6 "Executing %s..." (key-description command))
7323       ;; We'd like to execute COMMAND interactively so as to give arguments.
7324       (gnus-execute header regexp
7325                     `(call-interactively ',(key-binding command))
7326                     backward)
7327       (gnus-message 6 "Executing %s...done" (key-description command)))))
7328
7329 (defun gnus-summary-beginning-of-article ()
7330   "Scroll the article back to the beginning."
7331   (interactive)
7332   (gnus-summary-select-article)
7333   (gnus-configure-windows 'article)
7334   (gnus-eval-in-buffer-window gnus-article-buffer
7335     (widen)
7336     (goto-char (point-min))
7337     (when gnus-page-broken
7338       (gnus-narrow-to-page))))
7339
7340 (defun gnus-summary-end-of-article ()
7341   "Scroll to the end of the article."
7342   (interactive)
7343   (gnus-summary-select-article)
7344   (gnus-configure-windows 'article)
7345   (gnus-eval-in-buffer-window gnus-article-buffer
7346     (widen)
7347     (goto-char (point-max))
7348     (recenter -3)
7349     (when gnus-page-broken
7350       (gnus-narrow-to-page))))
7351
7352 (defun gnus-summary-print-article (&optional filename n)
7353   "Generate and print a PostScript image of the N next (mail) articles.
7354
7355 If N is negative, print the N previous articles.  If N is nil and articles
7356 have been marked with the process mark, print these instead.
7357
7358 If the optional first argument FILENAME is nil, send the image to the
7359 printer.  If FILENAME is a string, save the PostScript image in a file with
7360 that name.  If FILENAME is a number, prompt the user for the name of the file
7361 to save in."
7362   (interactive (list (ps-print-preprint current-prefix-arg)
7363                      current-prefix-arg))
7364   (dolist (article (gnus-summary-work-articles n))
7365     (gnus-summary-select-article nil nil 'pseudo article)
7366     (gnus-eval-in-buffer-window gnus-article-buffer
7367       (let ((buffer (generate-new-buffer " *print*")))
7368         (unwind-protect
7369             (progn
7370               (copy-to-buffer buffer (point-min) (point-max))
7371               (set-buffer buffer)
7372               (gnus-article-delete-invisible-text)
7373               (let ((ps-left-header
7374                      (list
7375                       (concat "("
7376                               (mail-header-subject gnus-current-headers) ")")
7377                       (concat "("
7378                               (mail-header-from gnus-current-headers) ")")))
7379                     (ps-right-header
7380                      (list
7381                       "/pagenumberstring load"
7382                       (concat "("
7383                               (mail-header-date gnus-current-headers) ")"))))
7384                 (gnus-run-hooks 'gnus-ps-print-hook)
7385                 (save-excursion
7386                   (ps-print-buffer-with-faces filename))))
7387           (kill-buffer buffer))))))
7388
7389 (defun gnus-summary-show-article (&optional arg)
7390   "Force re-fetching of the current article.
7391 If ARG (the prefix) is a number, show the article with the charset
7392 defined in `gnus-summary-show-article-charset-alist', or the charset
7393 inputed.
7394 If ARG (the prefix) is non-nil and not a number, show the raw article
7395 without any article massaging functions being run."
7396   (interactive "P")
7397   (cond
7398    ((numberp arg)
7399     (let ((gnus-newsgroup-charset
7400            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7401                (read-coding-system "Charset: ")))
7402           (gnus-newsgroup-ignored-charsets 'gnus-all))
7403       (gnus-summary-select-article nil 'force)
7404       (let ((deps gnus-newsgroup-dependencies)
7405             head header)
7406         (save-excursion
7407           (set-buffer gnus-original-article-buffer)
7408           (save-restriction
7409             (message-narrow-to-head)
7410             (setq head (buffer-string)))
7411           (with-temp-buffer
7412             (insert (format "211 %d Article retrieved.\n"
7413                             (cdr gnus-article-current)))
7414             (insert head)
7415             (insert ".\n")
7416             (let ((nntp-server-buffer (current-buffer)))
7417               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7418         (gnus-data-set-header
7419          (gnus-data-find (cdr gnus-article-current))
7420          header)
7421         (gnus-summary-update-article-line
7422          (cdr gnus-article-current) header))))
7423    ((not arg)
7424     ;; Select the article the normal way.
7425     (gnus-summary-select-article nil 'force))
7426    (t
7427     ;; We have to require this here to make sure that the following
7428     ;; dynamic binding isn't shadowed by autoloading.
7429     (require 'gnus-async)
7430     (require 'gnus-art)
7431     ;; Bind the article treatment functions to nil.
7432     (let ((gnus-have-all-headers t)
7433           gnus-article-display-hook
7434           gnus-article-prepare-hook
7435           gnus-article-decode-hook
7436           gnus-break-pages
7437           gnus-show-mime)
7438       (gnus-summary-select-article nil 'force))))
7439   (gnus-summary-goto-subject gnus-current-article)
7440   (gnus-summary-position-point))
7441
7442 (defun gnus-summary-verbose-headers (&optional arg)
7443   "Toggle permanent full header display.
7444 If ARG is a positive number, turn header display on.
7445 If ARG is a negative number, turn header display off."
7446   (interactive "P")
7447   (setq gnus-show-all-headers
7448         (cond ((or (not (numberp arg))
7449                    (zerop arg))
7450                (not gnus-show-all-headers))
7451               ((natnump arg)
7452                t)))
7453   (gnus-summary-show-article))
7454
7455 (defun gnus-summary-toggle-header (&optional arg)
7456   "Show the headers if they are hidden, or hide them if they are shown.
7457 If ARG is a positive number, show the entire header.
7458 If ARG is a negative number, hide the unwanted header lines."
7459   (interactive "P")
7460   (save-excursion
7461     (set-buffer gnus-article-buffer)
7462     (save-restriction
7463       (let* ((buffer-read-only nil)
7464              (inhibit-point-motion-hooks t)
7465              hidden e)
7466         (setq hidden
7467               (if (numberp arg)
7468                   (>= arg 0)
7469                 (save-restriction
7470                   (article-narrow-to-head)
7471                   (gnus-article-hidden-text-p 'headers))))
7472         (goto-char (point-min))
7473         (when (search-forward "\n\n" nil t)
7474           (delete-region (point-min) (1- (point))))
7475         (goto-char (point-min))
7476         (save-excursion
7477           (set-buffer gnus-original-article-buffer)
7478           (goto-char (point-min))
7479           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7480         (insert-buffer-substring gnus-original-article-buffer 1 e)
7481         (save-restriction
7482           (narrow-to-region (point-min) (point))
7483           (article-decode-encoded-words)
7484           (if  hidden
7485               (let ((gnus-treat-hide-headers nil)
7486                     (gnus-treat-hide-boring-headers nil))
7487                 (setq gnus-article-wash-types
7488                       (delq 'headers gnus-article-wash-types))
7489                 (gnus-treat-article 'head))
7490             (gnus-treat-article 'head)))
7491         (gnus-set-mode-line 'article)))))
7492
7493 (defun gnus-summary-show-all-headers ()
7494   "Make all header lines visible."
7495   (interactive)
7496   (gnus-article-show-all-headers))
7497
7498 (defun gnus-summary-toggle-mime (&optional arg)
7499   "Toggle MIME processing.
7500 If ARG is a positive number, turn MIME processing on."
7501   (interactive "P")
7502   (setq gnus-show-mime
7503         (if (null arg)
7504             (not gnus-show-mime)
7505           (> (prefix-numeric-value arg) 0)))
7506   (gnus-summary-select-article t 'force))
7507
7508 (defun gnus-summary-caesar-message (&optional arg)
7509   "Caesar rotate the current article by 13.
7510 The numerical prefix specifies how many places to rotate each letter
7511 forward."
7512   (interactive "P")
7513   (gnus-summary-select-article)
7514   (let ((mail-header-separator ""))
7515     (gnus-eval-in-buffer-window gnus-article-buffer
7516       (save-restriction
7517         (widen)
7518         (let ((start (window-start))
7519               buffer-read-only)
7520           (message-caesar-buffer-body arg)
7521           (set-window-start (get-buffer-window (current-buffer)) start))))))
7522
7523 (defun gnus-summary-stop-page-breaking ()
7524   "Stop page breaking in the current article."
7525   (interactive)
7526   (gnus-summary-select-article)
7527   (gnus-eval-in-buffer-window gnus-article-buffer
7528     (widen)
7529     (when (gnus-visual-p 'page-marker)
7530       (let ((buffer-read-only nil))
7531         (gnus-remove-text-with-property 'gnus-prev)
7532         (gnus-remove-text-with-property 'gnus-next))
7533       (setq gnus-page-broken nil))))
7534
7535 (defun gnus-summary-move-article (&optional n to-newsgroup
7536                                             select-method action)
7537   "Move the current article to a different newsgroup.
7538 If N is a positive number, move the N next articles.
7539 If N is a negative number, move the N previous articles.
7540 If N is nil and any articles have been marked with the process mark,
7541 move those articles instead.
7542 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7543 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7544 re-spool using this method.
7545
7546 For this function to work, both the current newsgroup and the
7547 newsgroup that you want to move to have to support the `request-move'
7548 and `request-accept' functions.
7549
7550 ACTION can be either `move' (the default), `crosspost' or `copy'."
7551   (interactive "P")
7552   (unless action
7553     (setq action 'move))
7554   ;; Disable marking as read.
7555   (let (gnus-mark-article-hook)
7556     (save-window-excursion
7557       (gnus-summary-select-article)))
7558   ;; Check whether the source group supports the required functions.
7559   (cond ((and (eq action 'move)
7560               (not (gnus-check-backend-function
7561                     'request-move-article gnus-newsgroup-name)))
7562          (error "The current group does not support article moving"))
7563         ((and (eq action 'crosspost)
7564               (not (gnus-check-backend-function
7565                     'request-replace-article gnus-newsgroup-name)))
7566          (error "The current group does not support article editing")))
7567   (let ((articles (gnus-summary-work-articles n))
7568         (prefix (if (gnus-check-backend-function
7569                     'request-move-article gnus-newsgroup-name)
7570                     (gnus-group-real-prefix gnus-newsgroup-name)
7571                   ""))
7572         (names '((move "Move" "Moving")
7573                  (copy "Copy" "Copying")
7574                  (crosspost "Crosspost" "Crossposting")))
7575         (copy-buf (save-excursion
7576                     (nnheader-set-temp-buffer " *copy article*")))
7577         (default-marks gnus-article-mark-lists)
7578         (no-expire-marks (delete '(expirable . expire)
7579                                  (copy-sequence gnus-article-mark-lists)))
7580         art-group to-method new-xref article to-groups)
7581     (unless (assq action names)
7582       (error "Unknown action %s" action))
7583     ;; Read the newsgroup name.
7584     (when (and (not to-newsgroup)
7585                (not select-method))
7586       (setq to-newsgroup
7587             (gnus-read-move-group-name
7588              (cadr (assq action names))
7589              (symbol-value (intern (format "gnus-current-%s-group" action)))
7590              articles prefix))
7591       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7592     (setq to-method (or select-method
7593                         (gnus-server-to-method
7594                          (gnus-group-method to-newsgroup))))
7595     ;; Check the method we are to move this article to...
7596     (unless (gnus-check-backend-function
7597              'request-accept-article (car to-method))
7598       (error "%s does not support article copying" (car to-method)))
7599     (unless (gnus-check-server to-method)
7600       (error "Can't open server %s" (car to-method)))
7601     (gnus-message 6 "%s to %s: %s..."
7602                   (caddr (assq action names))
7603                   (or (car select-method) to-newsgroup) articles)
7604     (while articles
7605       (setq article (pop articles))
7606       (setq
7607        art-group
7608        (cond
7609         ;; Move the article.
7610         ((eq action 'move)
7611          ;; Remove this article from future suppression.
7612          (gnus-dup-unsuppress-article article)
7613          (gnus-request-move-article
7614           article                       ; Article to move
7615           gnus-newsgroup-name           ; From newsgroup
7616           (nth 1 (gnus-find-method-for-group
7617                   gnus-newsgroup-name)) ; Server
7618           (list 'gnus-request-accept-article
7619                 to-newsgroup (list 'quote select-method)
7620                 (not articles) t)       ; Accept form
7621           (not articles)))              ; Only save nov last time
7622         ;; Copy the article.
7623         ((eq action 'copy)
7624          (save-excursion
7625            (set-buffer copy-buf)
7626            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7627              (gnus-request-accept-article
7628               to-newsgroup select-method (not articles) t))))
7629         ;; Crosspost the article.
7630         ((eq action 'crosspost)
7631          (let ((xref (message-tokenize-header
7632                       (mail-header-xref (gnus-summary-article-header article))
7633                       " ")))
7634            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7635                                   ":" article))
7636            (unless xref
7637              (setq xref (list (system-name))))
7638            (setq new-xref
7639                  (concat
7640                   (mapconcat 'identity
7641                              (delete "Xref:" (delete new-xref xref))
7642                              " ")
7643                   " " new-xref))
7644            (save-excursion
7645              (set-buffer copy-buf)
7646              ;; First put the article in the destination group.
7647              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7648              (when (consp (setq art-group
7649                                 (gnus-request-accept-article
7650                                  to-newsgroup select-method (not articles))))
7651                (setq new-xref (concat new-xref " " (car art-group)
7652                                       ":" (cdr art-group)))
7653                ;; Now we have the new Xrefs header, so we insert
7654                ;; it and replace the new article.
7655                (nnheader-replace-header "Xref" new-xref)
7656                (gnus-request-replace-article
7657                 (cdr art-group) to-newsgroup (current-buffer))
7658                art-group))))))
7659       (cond
7660        ((not art-group)
7661         (gnus-message 1 "Couldn't %s article %s: %s"
7662                       (cadr (assq action names)) article
7663                       (nnheader-get-report (car to-method))))
7664        ((eq art-group 'junk)
7665         (when (eq action 'move)
7666           (gnus-summary-mark-article article gnus-canceled-mark)
7667           (gnus-message 4 "Deleted article %s" article)))
7668        (t
7669         (let* ((pto-group (gnus-group-prefixed-name
7670                            (car art-group) to-method))
7671                (entry
7672                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7673                (info (nth 2 entry))
7674                (to-group (gnus-info-group info))
7675                to-marks)
7676           ;; Update the group that has been moved to.
7677           (when (and info
7678                      (memq action '(move copy)))
7679             (unless (member to-group to-groups)
7680               (push to-group to-groups))
7681
7682             (unless (memq article gnus-newsgroup-unreads)
7683               (push 'read to-marks)
7684               (gnus-info-set-read
7685                info (gnus-add-to-range (gnus-info-read info)
7686                                        (list (cdr art-group)))))
7687
7688             ;; See whether the article is to be put in the cache.
7689             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7690                              default-marks
7691                            no-expire-marks))
7692                   (to-article (cdr art-group)))
7693
7694               ;; Enter the article into the cache in the new group,
7695               ;; if that is required.
7696               (when gnus-use-cache
7697                 (gnus-cache-possibly-enter-article
7698                  to-group to-article
7699                  (let ((header (copy-sequence
7700                                 (gnus-summary-article-header article))))
7701                    (mail-header-set-number header to-article)
7702                    header)
7703                  (memq article gnus-newsgroup-marked)
7704                  (memq article gnus-newsgroup-dormant)
7705                  (memq article gnus-newsgroup-unreads)))
7706
7707               (when gnus-preserve-marks
7708                 ;; Copy any marks over to the new group.
7709                 (when (and (equal to-group gnus-newsgroup-name)
7710                            (not (memq article gnus-newsgroup-unreads)))
7711                   ;; Mark this article as read in this group.
7712                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7713                   (setcdr (gnus-active to-group) to-article)
7714                   (setcdr gnus-newsgroup-active to-article))
7715
7716                 (while marks
7717                   (when (memq article (symbol-value
7718                                        (intern (format "gnus-newsgroup-%s"
7719                                                        (caar marks)))))
7720                     (push (cdar marks) to-marks)
7721                     ;; If the other group is the same as this group,
7722                     ;; then we have to add the mark to the list.
7723                     (when (equal to-group gnus-newsgroup-name)
7724                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7725                            (cons to-article
7726                                  (symbol-value
7727                                   (intern (format "gnus-newsgroup-%s"
7728                                                   (caar marks)))))))
7729                     ;; Copy the marks to other group.
7730                     (gnus-add-marked-articles
7731                      to-group (cdar marks) (list to-article) info))
7732                   (setq marks (cdr marks)))
7733
7734                 (gnus-request-set-mark to-group (list (list (list to-article)
7735                                                             'set
7736                                                             to-marks))))
7737
7738               (gnus-dribble-enter
7739                (concat "(gnus-group-set-info '"
7740                        (gnus-prin1-to-string (gnus-get-info to-group))
7741                        ")"))))
7742
7743           ;; Update the Xref header in this article to point to
7744           ;; the new crossposted article we have just created.
7745           (when (eq action 'crosspost)
7746             (save-excursion
7747               (set-buffer copy-buf)
7748               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7749               (nnheader-replace-header "Xref" new-xref)
7750               (gnus-request-replace-article
7751                article gnus-newsgroup-name (current-buffer)))))
7752
7753         ;;;!!!Why is this necessary?
7754         (set-buffer gnus-summary-buffer)
7755
7756         (gnus-summary-goto-subject article)
7757         (when (eq action 'move)
7758           (gnus-summary-mark-article article gnus-canceled-mark))))
7759       (gnus-summary-remove-process-mark article))
7760     ;; Re-activate all groups that have been moved to.
7761     (while to-groups
7762       (save-excursion
7763         (set-buffer gnus-group-buffer)
7764         (when (gnus-group-goto-group (car to-groups) t)
7765           (gnus-group-get-new-news-this-group 1 t))
7766         (pop to-groups)))
7767
7768     (gnus-kill-buffer copy-buf)
7769     (gnus-summary-position-point)
7770     (gnus-set-mode-line 'summary)))
7771
7772 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7773   "Move the current article to a different newsgroup.
7774 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7775 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7776 re-spool using this method."
7777   (interactive "P")
7778   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7779
7780 (defun gnus-summary-crosspost-article (&optional n)
7781   "Crosspost the current article to some other group."
7782   (interactive "P")
7783   (gnus-summary-move-article n nil nil 'crosspost))
7784
7785 (defcustom gnus-summary-respool-default-method nil
7786   "Default method for respooling an article.
7787 If nil, use to the current newsgroup method."
7788   :type '(choice (gnus-select-method :value (nnml ""))
7789                  (const nil))
7790   :group 'gnus-summary-mail)
7791
7792 (defun gnus-summary-respool-article (&optional n method)
7793   "Respool the current article.
7794 The article will be squeezed through the mail spooling process again,
7795 which means that it will be put in some mail newsgroup or other
7796 depending on `nnmail-split-methods'.
7797 If N is a positive number, respool the N next articles.
7798 If N is a negative number, respool the N previous articles.
7799 If N is nil and any articles have been marked with the process mark,
7800 respool those articles instead.
7801
7802 Respooling can be done both from mail groups and \"real\" newsgroups.
7803 In the former case, the articles in question will be moved from the
7804 current group into whatever groups they are destined to.  In the
7805 latter case, they will be copied into the relevant groups."
7806   (interactive
7807    (list current-prefix-arg
7808          (let* ((methods (gnus-methods-using 'respool))
7809                 (methname
7810                  (symbol-name (or gnus-summary-respool-default-method
7811                                   (car (gnus-find-method-for-group
7812                                         gnus-newsgroup-name)))))
7813                 (method
7814                  (gnus-completing-read
7815                   methname "What backend do you want to use when respooling?"
7816                   methods nil t nil 'gnus-mail-method-history))
7817                 ms)
7818            (cond
7819             ((zerop (length (setq ms (gnus-servers-using-backend
7820                                       (intern method)))))
7821              (list (intern method) ""))
7822             ((= 1 (length ms))
7823              (car ms))
7824             (t
7825              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7826                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7827                            ms-alist))))))))
7828   (unless method
7829     (error "No method given for respooling"))
7830   (if (assoc (symbol-name
7831               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7832              (gnus-methods-using 'respool))
7833       (gnus-summary-move-article n nil method)
7834     (gnus-summary-copy-article n nil method)))
7835
7836 (defun gnus-summary-import-article (file)
7837   "Import an arbitrary file into a mail newsgroup."
7838   (interactive "fImport file: ")
7839   (let ((group gnus-newsgroup-name)
7840         (now (current-time))
7841         atts lines)
7842     (unless (gnus-check-backend-function 'request-accept-article group)
7843       (error "%s does not support article importing" group))
7844     (or (file-readable-p file)
7845         (not (file-regular-p file))
7846         (error "Can't read %s" file))
7847     (save-excursion
7848       (set-buffer (gnus-get-buffer-create " *import file*"))
7849       (erase-buffer)
7850       (nnheader-insert-file-contents file)
7851       (goto-char (point-min))
7852       (unless (nnheader-article-p)
7853         ;; This doesn't look like an article, so we fudge some headers.
7854         (setq atts (file-attributes file)
7855               lines (count-lines (point-min) (point-max)))
7856         (insert "From: " (read-string "From: ") "\n"
7857                 "Subject: " (read-string "Subject: ") "\n"
7858                 "Date: " (message-make-date (nth 5 atts))
7859                 "\n"
7860                 "Message-ID: " (message-make-message-id) "\n"
7861                 "Lines: " (int-to-string lines) "\n"
7862                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7863       (gnus-request-accept-article group nil t)
7864       (kill-buffer (current-buffer)))))
7865
7866 (defun gnus-summary-article-posted-p ()
7867   "Say whether the current (mail) article is available from news as well.
7868 This will be the case if the article has both been mailed and posted."
7869   (interactive)
7870   (let ((id (mail-header-references (gnus-summary-article-header)))
7871         (gnus-override-method (car (gnus-refer-article-methods))))
7872     (if (gnus-request-head id "")
7873         (gnus-message 2 "The current message was found on %s"
7874                       gnus-override-method)
7875       (gnus-message 2 "The current message couldn't be found on %s"
7876                     gnus-override-method)
7877       nil)))
7878
7879 (defun gnus-summary-expire-articles (&optional now)
7880   "Expire all articles that are marked as expirable in the current group."
7881   (interactive)
7882   (when (gnus-check-backend-function
7883          'request-expire-articles gnus-newsgroup-name)
7884     ;; This backend supports expiry.
7885     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7886            (expirable (if total
7887                           (progn
7888                             ;; We need to update the info for
7889                             ;; this group for `gnus-list-of-read-articles'
7890                             ;; to give us the right answer.
7891                             (gnus-run-hooks 'gnus-exit-group-hook)
7892                             (gnus-summary-update-info)
7893                             (gnus-list-of-read-articles gnus-newsgroup-name))
7894                         (setq gnus-newsgroup-expirable
7895                               (sort gnus-newsgroup-expirable '<))))
7896            (expiry-wait (if now 'immediate
7897                           (gnus-group-find-parameter
7898                            gnus-newsgroup-name 'expiry-wait)))
7899            (nnmail-expiry-target
7900             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7901                 nnmail-expiry-target))
7902            es)
7903       (when expirable
7904         ;; There are expirable articles in this group, so we run them
7905         ;; through the expiry process.
7906         (gnus-message 6 "Expiring articles...")
7907         (unless (gnus-check-group gnus-newsgroup-name)
7908           (error "Can't open server for %s" gnus-newsgroup-name))
7909         ;; The list of articles that weren't expired is returned.
7910         (save-excursion
7911           (if expiry-wait
7912               (let ((nnmail-expiry-wait-function nil)
7913                     (nnmail-expiry-wait expiry-wait))
7914                 (setq es (gnus-request-expire-articles
7915                           expirable gnus-newsgroup-name)))
7916             (setq es (gnus-request-expire-articles
7917                       expirable gnus-newsgroup-name)))
7918           (unless total
7919             (setq gnus-newsgroup-expirable es))
7920           ;; We go through the old list of expirable, and mark all
7921           ;; really expired articles as nonexistent.
7922           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7923             (let ((gnus-use-cache nil))
7924               (while expirable
7925                 (unless (memq (car expirable) es)
7926                   (when (gnus-data-find (car expirable))
7927                     (gnus-summary-mark-article
7928                      (car expirable) gnus-canceled-mark)))
7929                 (setq expirable (cdr expirable))))))
7930         (gnus-message 6 "Expiring articles...done")))))
7931
7932 (defun gnus-summary-expire-articles-now ()
7933   "Expunge all expirable articles in the current group.
7934 This means that *all* articles that are marked as expirable will be
7935 deleted forever, right now."
7936   (interactive)
7937   (or gnus-expert-user
7938       (gnus-yes-or-no-p
7939        "Are you really, really, really sure you want to delete all these messages? ")
7940       (error "Phew!"))
7941   (gnus-summary-expire-articles t))
7942
7943 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7944 (defun gnus-summary-delete-article (&optional n)
7945   "Delete the N next (mail) articles.
7946 This command actually deletes articles.  This is not a marking
7947 command.  The article will disappear forever from your life, never to
7948 return.
7949 If N is negative, delete backwards.
7950 If N is nil and articles have been marked with the process mark,
7951 delete these instead."
7952   (interactive "P")
7953   (unless (gnus-check-backend-function 'request-expire-articles
7954                                        gnus-newsgroup-name)
7955     (error "The current newsgroup does not support article deletion"))
7956   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7957     (error "Couldn't open server"))
7958   ;; Compute the list of articles to delete.
7959   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7960         not-deleted)
7961     (if (and gnus-novice-user
7962              (not (gnus-yes-or-no-p
7963                    (format "Do you really want to delete %s forever? "
7964                            (if (> (length articles) 1)
7965                                (format "these %s articles" (length articles))
7966                              "this article")))))
7967         ()
7968       ;; Delete the articles.
7969       (setq not-deleted (gnus-request-expire-articles
7970                          articles gnus-newsgroup-name 'force))
7971       (while articles
7972         (gnus-summary-remove-process-mark (car articles))
7973         ;; The backend might not have been able to delete the article
7974         ;; after all.
7975         (unless (memq (car articles) not-deleted)
7976           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7977         (setq articles (cdr articles)))
7978       (when not-deleted
7979         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7980     (gnus-summary-position-point)
7981     (gnus-set-mode-line 'summary)
7982     not-deleted))
7983
7984 (defun gnus-summary-edit-article (&optional force)
7985   "Edit the current article.
7986 This will have permanent effect only in mail groups.
7987 If FORCE is non-nil, allow editing of articles even in read-only
7988 groups."
7989   (interactive "P")
7990   (save-excursion
7991     (set-buffer gnus-summary-buffer)
7992     (let ((mail-parse-charset gnus-newsgroup-charset)
7993           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7994       (gnus-set-global-variables)
7995       (when (and (not force)
7996                  (gnus-group-read-only-p))
7997         (error "The current newsgroup does not support article editing"))
7998       (gnus-summary-show-article t)
7999       (gnus-article-edit-article
8000        'ignore
8001        `(lambda (no-highlight)
8002           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8003                 (message-options message-options)
8004                 (message-options-set-recipient)
8005                 (mail-parse-ignored-charsets
8006                  ',gnus-newsgroup-ignored-charsets))
8007             (gnus-summary-edit-article-done
8008              ,(or (mail-header-references gnus-current-headers) "")
8009              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8010
8011 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8012
8013 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8014                                                  no-highlight)
8015   "Make edits to the current article permanent."
8016   (interactive)
8017   ;; Replace the article.
8018   (let ((buf (current-buffer)))
8019     (with-temp-buffer
8020       (insert-buffer-substring buf)
8021       (if (and (not read-only)
8022                (not (gnus-request-replace-article
8023                      (cdr gnus-article-current) (car gnus-article-current)
8024                      (current-buffer) t)))
8025           (error "Couldn't replace article")
8026         ;; Update the summary buffer.
8027         (if (and references
8028                  (equal (message-tokenize-header references " ")
8029                         (message-tokenize-header
8030                          (or (message-fetch-field "references") "") " ")))
8031             ;; We only have to update this line.
8032             (save-excursion
8033               (save-restriction
8034                 (message-narrow-to-head)
8035                 (let ((head (buffer-string))
8036                       header)
8037                   (with-temp-buffer
8038                     (insert (format "211 %d Article retrieved.\n"
8039                                     (cdr gnus-article-current)))
8040                     (insert head)
8041                     (insert ".\n")
8042                     (let ((nntp-server-buffer (current-buffer)))
8043                       (setq header (car (gnus-get-newsgroup-headers
8044                                          (save-excursion
8045                                            (set-buffer gnus-summary-buffer)
8046                                            gnus-newsgroup-dependencies)
8047                                          t))))
8048                     (save-excursion
8049                       (set-buffer gnus-summary-buffer)
8050                       (gnus-data-set-header
8051                        (gnus-data-find (cdr gnus-article-current))
8052                        header)
8053                       (gnus-summary-update-article-line
8054                        (cdr gnus-article-current) header))))))
8055           ;; Update threads.
8056           (set-buffer (or buffer gnus-summary-buffer))
8057           (gnus-summary-update-article (cdr gnus-article-current)))
8058         ;; Prettify the article buffer again.
8059         (unless no-highlight
8060           (save-excursion
8061             (set-buffer gnus-article-buffer)
8062             ;;;!!! Fix this -- article should be rehighlighted.
8063             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8064             (set-buffer gnus-original-article-buffer)
8065             (gnus-request-article
8066              (cdr gnus-article-current)
8067              (car gnus-article-current) (current-buffer))))
8068         ;; Prettify the summary buffer line.
8069         (when (gnus-visual-p 'summary-highlight 'highlight)
8070           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8071
8072 (defun gnus-summary-edit-wash (key)
8073   "Perform editing command KEY in the article buffer."
8074   (interactive
8075    (list
8076     (progn
8077       (message "%s" (concat (this-command-keys) "- "))
8078       (read-char))))
8079   (message "")
8080   (gnus-summary-edit-article)
8081   (execute-kbd-macro (concat (this-command-keys) key))
8082   (gnus-article-edit-done))
8083
8084 ;;; Respooling
8085
8086 (defun gnus-summary-respool-query (&optional silent trace)
8087   "Query where the respool algorithm would put this article."
8088   (interactive)
8089   (let (gnus-mark-article-hook)
8090     (gnus-summary-select-article)
8091     (save-excursion
8092       (set-buffer gnus-original-article-buffer)
8093       (save-restriction
8094         (message-narrow-to-head)
8095         (let ((groups (nnmail-article-group 'identity trace)))
8096           (unless silent
8097             (if groups
8098                 (message "This message would go to %s"
8099                          (mapconcat 'car groups ", "))
8100               (message "This message would go to no groups"))
8101             groups))))))
8102
8103 (defun gnus-summary-respool-trace ()
8104   "Trace where the respool algorithm would put this article.
8105 Display a buffer showing all fancy splitting patterns which matched."
8106   (interactive)
8107   (gnus-summary-respool-query nil t))
8108
8109 ;; Summary marking commands.
8110
8111 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8112   "Mark articles which has the same subject as read, and then select the next.
8113 If UNMARK is positive, remove any kind of mark.
8114 If UNMARK is negative, tick articles."
8115   (interactive "P")
8116   (when unmark
8117     (setq unmark (prefix-numeric-value unmark)))
8118   (let ((count
8119          (gnus-summary-mark-same-subject
8120           (gnus-summary-article-subject) unmark)))
8121     ;; Select next unread article.  If auto-select-same mode, should
8122     ;; select the first unread article.
8123     (gnus-summary-next-article t (and gnus-auto-select-same
8124                                       (gnus-summary-article-subject)))
8125     (gnus-message 7 "%d article%s marked as %s"
8126                   count (if (= count 1) " is" "s are")
8127                   (if unmark "unread" "read"))))
8128
8129 (defun gnus-summary-kill-same-subject (&optional unmark)
8130   "Mark articles which has the same subject as read.
8131 If UNMARK is positive, remove any kind of mark.
8132 If UNMARK is negative, tick articles."
8133   (interactive "P")
8134   (when unmark
8135     (setq unmark (prefix-numeric-value unmark)))
8136   (let ((count
8137          (gnus-summary-mark-same-subject
8138           (gnus-summary-article-subject) unmark)))
8139     ;; If marked as read, go to next unread subject.
8140     (when (null unmark)
8141       ;; Go to next unread subject.
8142       (gnus-summary-next-subject 1 t))
8143     (gnus-message 7 "%d articles are marked as %s"
8144                   count (if unmark "unread" "read"))))
8145
8146 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8147   "Mark articles with same SUBJECT as read, and return marked number.
8148 If optional argument UNMARK is positive, remove any kinds of marks.
8149 If optional argument UNMARK is negative, mark articles as unread instead."
8150   (let ((count 1))
8151     (save-excursion
8152       (cond
8153        ((null unmark)                   ; Mark as read.
8154         (while (and
8155                 (progn
8156                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8157                   (gnus-summary-show-thread) t)
8158                 (gnus-summary-find-subject subject))
8159           (setq count (1+ count))))
8160        ((> unmark 0)                    ; Tick.
8161         (while (and
8162                 (progn
8163                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8164                   (gnus-summary-show-thread) t)
8165                 (gnus-summary-find-subject subject))
8166           (setq count (1+ count))))
8167        (t                               ; Mark as unread.
8168         (while (and
8169                 (progn
8170                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8171                   (gnus-summary-show-thread) t)
8172                 (gnus-summary-find-subject subject))
8173           (setq count (1+ count)))))
8174       (gnus-set-mode-line 'summary)
8175       ;; Return the number of marked articles.
8176       count)))
8177
8178 (defun gnus-summary-mark-as-processable (n &optional unmark)
8179   "Set the process mark on the next N articles.
8180 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8181 the process mark instead.  The difference between N and the actual
8182 number of articles marked is returned."
8183   (interactive "p")
8184   (let ((backward (< n 0))
8185         (n (abs n)))
8186     (while (and
8187             (> n 0)
8188             (if unmark
8189                 (gnus-summary-remove-process-mark
8190                  (gnus-summary-article-number))
8191               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8192             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8193       (setq n (1- n)))
8194     (when (/= 0 n)
8195       (gnus-message 7 "No more articles"))
8196     (gnus-summary-recenter)
8197     (gnus-summary-position-point)
8198     n))
8199
8200 (defun gnus-summary-unmark-as-processable (n)
8201   "Remove the process mark from the next N articles.
8202 If N is negative, unmark backward instead.  The difference between N and
8203 the actual number of articles unmarked is returned."
8204   (interactive "p")
8205   (gnus-summary-mark-as-processable n t))
8206
8207 (defun gnus-summary-unmark-all-processable ()
8208   "Remove the process mark from all articles."
8209   (interactive)
8210   (save-excursion
8211     (while gnus-newsgroup-processable
8212       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8213   (gnus-summary-position-point))
8214
8215 (defun gnus-summary-mark-as-expirable (n)
8216   "Mark N articles forward as expirable.
8217 If N is negative, mark backward instead.  The difference between N and
8218 the actual number of articles marked is returned."
8219   (interactive "p")
8220   (gnus-summary-mark-forward n gnus-expirable-mark))
8221
8222 (defun gnus-summary-mark-article-as-replied (article)
8223   "Mark ARTICLE replied and update the summary line."
8224   (push article gnus-newsgroup-replied)
8225   (let ((buffer-read-only nil))
8226     (when (gnus-summary-goto-subject article nil t)
8227       (gnus-summary-update-secondary-mark article))))
8228
8229 (defun gnus-summary-set-bookmark (article)
8230   "Set a bookmark in current article."
8231   (interactive (list (gnus-summary-article-number)))
8232   (when (or (not (get-buffer gnus-article-buffer))
8233             (not gnus-current-article)
8234             (not gnus-article-current)
8235             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8236     (error "No current article selected"))
8237   ;; Remove old bookmark, if one exists.
8238   (let ((old (assq article gnus-newsgroup-bookmarks)))
8239     (when old
8240       (setq gnus-newsgroup-bookmarks
8241             (delq old gnus-newsgroup-bookmarks))))
8242   ;; Set the new bookmark, which is on the form
8243   ;; (article-number . line-number-in-body).
8244   (push
8245    (cons article
8246          (save-excursion
8247            (set-buffer gnus-article-buffer)
8248            (count-lines
8249             (min (point)
8250                  (save-excursion
8251                    (goto-char (point-min))
8252                    (search-forward "\n\n" nil t)
8253                    (point)))
8254             (point))))
8255    gnus-newsgroup-bookmarks)
8256   (gnus-message 6 "A bookmark has been added to the current article."))
8257
8258 (defun gnus-summary-remove-bookmark (article)
8259   "Remove the bookmark from the current article."
8260   (interactive (list (gnus-summary-article-number)))
8261   ;; Remove old bookmark, if one exists.
8262   (let ((old (assq article gnus-newsgroup-bookmarks)))
8263     (if old
8264         (progn
8265           (setq gnus-newsgroup-bookmarks
8266                 (delq old gnus-newsgroup-bookmarks))
8267           (gnus-message 6 "Removed bookmark."))
8268       (gnus-message 6 "No bookmark in current article."))))
8269
8270 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8271 (defun gnus-summary-mark-as-dormant (n)
8272   "Mark N articles forward as dormant.
8273 If N is negative, mark backward instead.  The difference between N and
8274 the actual number of articles marked is returned."
8275   (interactive "p")
8276   (gnus-summary-mark-forward n gnus-dormant-mark))
8277
8278 (defun gnus-summary-set-process-mark (article)
8279   "Set the process mark on ARTICLE and update the summary line."
8280   (setq gnus-newsgroup-processable
8281         (cons article
8282               (delq article gnus-newsgroup-processable)))
8283   (when (gnus-summary-goto-subject article)
8284     (gnus-summary-show-thread)
8285     (gnus-summary-goto-subject article)
8286     (gnus-summary-update-secondary-mark article)))
8287
8288 (defun gnus-summary-remove-process-mark (article)
8289   "Remove the process mark from ARTICLE and update the summary line."
8290   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8291   (when (gnus-summary-goto-subject article)
8292     (gnus-summary-show-thread)
8293     (gnus-summary-goto-subject article)
8294     (gnus-summary-update-secondary-mark article)))
8295
8296 (defun gnus-summary-set-saved-mark (article)
8297   "Set the process mark on ARTICLE and update the summary line."
8298   (push article gnus-newsgroup-saved)
8299   (when (gnus-summary-goto-subject article)
8300     (gnus-summary-update-secondary-mark article)))
8301
8302 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8303   "Mark N articles as read forwards.
8304 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8305 The difference between N and the actual number of articles marked is
8306 returned.
8307 Iff NO-EXPIRE, auto-expiry will be inhibited."
8308   (interactive "p")
8309   (gnus-summary-show-thread)
8310   (let ((backward (< n 0))
8311         (gnus-summary-goto-unread
8312          (and gnus-summary-goto-unread
8313               (not (eq gnus-summary-goto-unread 'never))
8314               (not (memq mark (list gnus-unread-mark
8315                                     gnus-ticked-mark gnus-dormant-mark)))))
8316         (n (abs n))
8317         (mark (or mark gnus-del-mark)))
8318     (while (and (> n 0)
8319                 (gnus-summary-mark-article nil mark no-expire)
8320                 (zerop (gnus-summary-next-subject
8321                         (if backward -1 1)
8322                         (and gnus-summary-goto-unread
8323                              (not (eq gnus-summary-goto-unread 'never)))
8324                         t)))
8325       (setq n (1- n)))
8326     (when (/= 0 n)
8327       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8328     (gnus-summary-recenter)
8329     (gnus-summary-position-point)
8330     (gnus-set-mode-line 'summary)
8331     n))
8332
8333 (defun gnus-summary-mark-article-as-read (mark)
8334   "Mark the current article quickly as read with MARK."
8335   (let ((article (gnus-summary-article-number)))
8336     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8337     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8338     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8339     (push (cons article mark) gnus-newsgroup-reads)
8340     ;; Possibly remove from cache, if that is used.
8341     (when gnus-use-cache
8342       (gnus-cache-enter-remove-article article))
8343     ;; Allow the backend to change the mark.
8344     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8345     ;; Check for auto-expiry.
8346     (when (and gnus-newsgroup-auto-expire
8347                (memq mark gnus-auto-expirable-marks))
8348       (setq mark gnus-expirable-mark)
8349       ;; Let the backend know about the mark change.
8350       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8351       (push article gnus-newsgroup-expirable))
8352     ;; Set the mark in the buffer.
8353     (gnus-summary-update-mark mark 'unread)
8354     t))
8355
8356 (defun gnus-summary-mark-article-as-unread (mark)
8357   "Mark the current article quickly as unread with MARK."
8358   (let* ((article (gnus-summary-article-number))
8359          (old-mark (gnus-summary-article-mark article)))
8360     ;; Allow the backend to change the mark.
8361     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8362     (if (eq mark old-mark)
8363         t
8364       (if (<= article 0)
8365           (progn
8366             (gnus-error 1 "Can't mark negative article numbers")
8367             nil)
8368         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8369         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8370         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8371         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8372         (cond ((= mark gnus-ticked-mark)
8373                (push article gnus-newsgroup-marked))
8374               ((= mark gnus-dormant-mark)
8375                (push article gnus-newsgroup-dormant))
8376               (t
8377                (push article gnus-newsgroup-unreads)))
8378         (gnus-pull article gnus-newsgroup-reads)
8379
8380         ;; See whether the article is to be put in the cache.
8381         (and gnus-use-cache
8382              (vectorp (gnus-summary-article-header article))
8383              (save-excursion
8384                (gnus-cache-possibly-enter-article
8385                 gnus-newsgroup-name article
8386                 (gnus-summary-article-header article)
8387                 (= mark gnus-ticked-mark)
8388                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8389
8390         ;; Fix the mark.
8391         (gnus-summary-update-mark mark 'unread)
8392         t))))
8393
8394 (defun gnus-summary-mark-article (&optional article mark no-expire)
8395   "Mark ARTICLE with MARK.  MARK can be any character.
8396 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8397 `??' (dormant) and `?E' (expirable).
8398 If MARK is nil, then the default character `?r' is used.
8399 If ARTICLE is nil, then the article on the current line will be
8400 marked.
8401 Iff NO-EXPIRE, auto-expiry will be inhibited."
8402   ;; The mark might be a string.
8403   (when (stringp mark)
8404     (setq mark (aref mark 0)))
8405   ;; If no mark is given, then we check auto-expiring.
8406   (when (null mark)
8407     (setq mark gnus-del-mark))
8408   (when (and (not no-expire)
8409              gnus-newsgroup-auto-expire
8410              (memq mark gnus-auto-expirable-marks))
8411     (setq mark gnus-expirable-mark))
8412   (let ((article (or article (gnus-summary-article-number)))
8413         (old-mark (gnus-summary-article-mark article)))
8414     ;; Allow the backend to change the mark.
8415     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8416     (if (eq mark old-mark)
8417         t
8418       (unless article
8419         (error "No article on current line"))
8420       (if (not (if (or (= mark gnus-unread-mark)
8421                        (= mark gnus-ticked-mark)
8422                        (= mark gnus-dormant-mark))
8423                    (gnus-mark-article-as-unread article mark)
8424                  (gnus-mark-article-as-read article mark)))
8425           t
8426         ;; See whether the article is to be put in the cache.
8427         (and gnus-use-cache
8428              (not (= mark gnus-canceled-mark))
8429              (vectorp (gnus-summary-article-header article))
8430              (save-excursion
8431                (gnus-cache-possibly-enter-article
8432                 gnus-newsgroup-name article
8433                 (gnus-summary-article-header article)
8434                 (= mark gnus-ticked-mark)
8435                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8436
8437         (when (gnus-summary-goto-subject article nil t)
8438           (let ((buffer-read-only nil))
8439             (gnus-summary-show-thread)
8440             ;; Fix the mark.
8441             (gnus-summary-update-mark mark 'unread)
8442             t))))))
8443
8444 (defun gnus-summary-update-secondary-mark (article)
8445   "Update the secondary (read, process, cache) mark."
8446   (gnus-summary-update-mark
8447    (cond ((memq article gnus-newsgroup-processable)
8448           gnus-process-mark)
8449          ((memq article gnus-newsgroup-cached)
8450           gnus-cached-mark)
8451          ((memq article gnus-newsgroup-replied)
8452           gnus-replied-mark)
8453          ((memq article gnus-newsgroup-saved)
8454           gnus-saved-mark)
8455          (t gnus-unread-mark))
8456    'replied)
8457   (when (gnus-visual-p 'summary-highlight 'highlight)
8458     (gnus-run-hooks 'gnus-summary-update-hook))
8459   t)
8460
8461 (defun gnus-summary-update-mark (mark type)
8462   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8463         (buffer-read-only nil))
8464     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8465     (when forward
8466       (when (looking-at "\r")
8467         (incf forward))
8468       (when (<= (+ forward (point)) (point-max))
8469         ;; Go to the right position on the line.
8470         (goto-char (+ forward (point)))
8471         ;; Replace the old mark with the new mark.
8472         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8473         ;; Optionally update the marks by some user rule.
8474         (when (eq type 'unread)
8475           (gnus-data-set-mark
8476            (gnus-data-find (gnus-summary-article-number)) mark)
8477           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8478
8479 (defun gnus-mark-article-as-read (article &optional mark)
8480   "Enter ARTICLE in the pertinent lists and remove it from others."
8481   ;; Make the article expirable.
8482   (let ((mark (or mark gnus-del-mark)))
8483     (if (= mark gnus-expirable-mark)
8484         (push article gnus-newsgroup-expirable)
8485       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8486     ;; Remove from unread and marked lists.
8487     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8488     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8489     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8490     (push (cons article mark) gnus-newsgroup-reads)
8491     ;; Possibly remove from cache, if that is used.
8492     (when gnus-use-cache
8493       (gnus-cache-enter-remove-article article))
8494     t))
8495
8496 (defun gnus-mark-article-as-unread (article &optional mark)
8497   "Enter ARTICLE in the pertinent lists and remove it from others."
8498   (let ((mark (or mark gnus-ticked-mark)))
8499     (if (<= article 0)
8500         (progn
8501           (gnus-error 1 "Can't mark negative article numbers")
8502           nil)
8503       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8504             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8505             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8506             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8507
8508       ;; Unsuppress duplicates?
8509       (when gnus-suppress-duplicates
8510         (gnus-dup-unsuppress-article article))
8511
8512       (cond ((= mark gnus-ticked-mark)
8513              (push article gnus-newsgroup-marked))
8514             ((= mark gnus-dormant-mark)
8515              (push article gnus-newsgroup-dormant))
8516             (t
8517              (push article gnus-newsgroup-unreads)))
8518       (gnus-pull article gnus-newsgroup-reads)
8519       t)))
8520
8521 (defalias 'gnus-summary-mark-as-unread-forward
8522   'gnus-summary-tick-article-forward)
8523 (make-obsolete 'gnus-summary-mark-as-unread-forward
8524                'gnus-summary-tick-article-forward)
8525 (defun gnus-summary-tick-article-forward (n)
8526   "Tick N articles forwards.
8527 If N is negative, tick backwards instead.
8528 The difference between N and the number of articles ticked is returned."
8529   (interactive "p")
8530   (gnus-summary-mark-forward n gnus-ticked-mark))
8531
8532 (defalias 'gnus-summary-mark-as-unread-backward
8533   'gnus-summary-tick-article-backward)
8534 (make-obsolete 'gnus-summary-mark-as-unread-backward
8535                'gnus-summary-tick-article-backward)
8536 (defun gnus-summary-tick-article-backward (n)
8537   "Tick N articles backwards.
8538 The difference between N and the number of articles ticked is returned."
8539   (interactive "p")
8540   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8541
8542 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8543 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8544 (defun gnus-summary-tick-article (&optional article clear-mark)
8545   "Mark current article as unread.
8546 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8547 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8548   (interactive)
8549   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8550                                        gnus-ticked-mark)))
8551
8552 (defun gnus-summary-mark-as-read-forward (n)
8553   "Mark N articles as read forwards.
8554 If N is negative, mark backwards instead.
8555 The difference between N and the actual number of articles marked is
8556 returned."
8557   (interactive "p")
8558   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8559
8560 (defun gnus-summary-mark-as-read-backward (n)
8561   "Mark the N articles as read backwards.
8562 The difference between N and the actual number of articles marked is
8563 returned."
8564   (interactive "p")
8565   (gnus-summary-mark-forward
8566    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8567
8568 (defun gnus-summary-mark-as-read (&optional article mark)
8569   "Mark current article as read.
8570 ARTICLE specifies the article to be marked as read.
8571 MARK specifies a string to be inserted at the beginning of the line."
8572   (gnus-summary-mark-article article mark))
8573
8574 (defun gnus-summary-clear-mark-forward (n)
8575   "Clear marks from N articles forward.
8576 If N is negative, clear backward instead.
8577 The difference between N and the number of marks cleared is returned."
8578   (interactive "p")
8579   (gnus-summary-mark-forward n gnus-unread-mark))
8580
8581 (defun gnus-summary-clear-mark-backward (n)
8582   "Clear marks from N articles backward.
8583 The difference between N and the number of marks cleared is returned."
8584   (interactive "p")
8585   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8586
8587 (defun gnus-summary-mark-unread-as-read ()
8588   "Intended to be used by `gnus-summary-mark-article-hook'."
8589   (when (memq gnus-current-article gnus-newsgroup-unreads)
8590     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8591
8592 (defun gnus-summary-mark-read-and-unread-as-read ()
8593   "Intended to be used by `gnus-summary-mark-article-hook'."
8594   (let ((mark (gnus-summary-article-mark)))
8595     (when (or (gnus-unread-mark-p mark)
8596               (gnus-read-mark-p mark))
8597       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8598
8599 (defun gnus-summary-mark-region-as-read (point mark all)
8600   "Mark all unread articles between point and mark as read.
8601 If given a prefix, mark all articles between point and mark as read,
8602 even ticked and dormant ones."
8603   (interactive "r\nP")
8604   (save-excursion
8605     (let (article)
8606       (goto-char point)
8607       (beginning-of-line)
8608       (while (and
8609               (< (point) mark)
8610               (progn
8611                 (when (or all
8612                           (memq (setq article (gnus-summary-article-number))
8613                                 gnus-newsgroup-unreads))
8614                   (gnus-summary-mark-article article gnus-del-mark))
8615                 t)
8616               (gnus-summary-find-next))))))
8617
8618 (defun gnus-summary-mark-below (score mark)
8619   "Mark articles with score less than SCORE with MARK."
8620   (interactive "P\ncMark: ")
8621   (setq score (if score
8622                   (prefix-numeric-value score)
8623                 (or gnus-summary-default-score 0)))
8624   (save-excursion
8625     (set-buffer gnus-summary-buffer)
8626     (goto-char (point-min))
8627     (while
8628         (progn
8629           (and (< (gnus-summary-article-score) score)
8630                (gnus-summary-mark-article nil mark))
8631           (gnus-summary-find-next)))))
8632
8633 (defun gnus-summary-kill-below (&optional score)
8634   "Mark articles with score below SCORE as read."
8635   (interactive "P")
8636   (gnus-summary-mark-below score gnus-killed-mark))
8637
8638 (defun gnus-summary-clear-above (&optional score)
8639   "Clear all marks from articles with score above SCORE."
8640   (interactive "P")
8641   (gnus-summary-mark-above score gnus-unread-mark))
8642
8643 (defun gnus-summary-tick-above (&optional score)
8644   "Tick all articles with score above SCORE."
8645   (interactive "P")
8646   (gnus-summary-mark-above score gnus-ticked-mark))
8647
8648 (defun gnus-summary-mark-above (score mark)
8649   "Mark articles with score over SCORE with MARK."
8650   (interactive "P\ncMark: ")
8651   (setq score (if score
8652                   (prefix-numeric-value score)
8653                 (or gnus-summary-default-score 0)))
8654   (save-excursion
8655     (set-buffer gnus-summary-buffer)
8656     (goto-char (point-min))
8657     (while (and (progn
8658                   (when (> (gnus-summary-article-score) score)
8659                     (gnus-summary-mark-article nil mark))
8660                   t)
8661                 (gnus-summary-find-next)))))
8662
8663 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8664 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8665 (defun gnus-summary-limit-include-expunged (&optional no-error)
8666   "Display all the hidden articles that were expunged for low scores."
8667   (interactive)
8668   (let ((buffer-read-only nil))
8669     (let ((scored gnus-newsgroup-scored)
8670           headers h)
8671       (while scored
8672         (unless (gnus-summary-goto-subject (caar scored))
8673           (and (setq h (gnus-summary-article-header (caar scored)))
8674                (< (cdar scored) gnus-summary-expunge-below)
8675                (push h headers)))
8676         (setq scored (cdr scored)))
8677       (if (not headers)
8678           (when (not no-error)
8679             (error "No expunged articles hidden"))
8680         (goto-char (point-min))
8681         (gnus-summary-prepare-unthreaded (nreverse headers))
8682         (goto-char (point-min))
8683         (gnus-summary-position-point)
8684         t))))
8685
8686 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8687   "Mark all unread articles in this newsgroup as read.
8688 If prefix argument ALL is non-nil, ticked and dormant articles will
8689 also be marked as read.
8690 If QUIETLY is non-nil, no questions will be asked.
8691 If TO-HERE is non-nil, it should be a point in the buffer.  All
8692 articles before this point will be marked as read.
8693 Note that this function will only catch up the unread article
8694 in the current summary buffer limitation.
8695 The number of articles marked as read is returned."
8696   (interactive "P")
8697   (prog1
8698       (save-excursion
8699         (when (or quietly
8700                   (not gnus-interactive-catchup) ;Without confirmation?
8701                   gnus-expert-user
8702                   (gnus-y-or-n-p
8703                    (if all
8704                        "Mark absolutely all articles as read? "
8705                      "Mark all unread articles as read? ")))
8706           (if (and not-mark
8707                    (not gnus-newsgroup-adaptive)
8708                    (not gnus-newsgroup-auto-expire)
8709                    (not gnus-suppress-duplicates)
8710                    (or (not gnus-use-cache)
8711                        (eq gnus-use-cache 'passive)))
8712               (progn
8713                 (when all
8714                   (setq gnus-newsgroup-marked nil
8715                         gnus-newsgroup-dormant nil))
8716                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8717             ;; We actually mark all articles as canceled, which we
8718             ;; have to do when using auto-expiry or adaptive scoring.
8719             (gnus-summary-show-all-threads)
8720             (when (gnus-summary-first-subject (not all) t)
8721               (while (and
8722                       (if to-here (< (point) to-here) t)
8723                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8724                       (gnus-summary-find-next (not all) nil nil t))))
8725             (gnus-set-mode-line 'summary))
8726           t))
8727     (gnus-summary-position-point)))
8728
8729 (defun gnus-summary-catchup-to-here (&optional all)
8730   "Mark all unticked articles before the current one as read.
8731 If ALL is non-nil, also mark ticked and dormant articles as read."
8732   (interactive "P")
8733   (save-excursion
8734     (gnus-save-hidden-threads
8735       (let ((beg (point)))
8736         ;; We check that there are unread articles.
8737         (when (or all (gnus-summary-find-prev))
8738           (gnus-summary-catchup all t beg)))))
8739   (gnus-summary-position-point))
8740
8741 (defun gnus-summary-catchup-all (&optional quietly)
8742   "Mark all articles in this newsgroup as read."
8743   (interactive "P")
8744   (gnus-summary-catchup t quietly))
8745
8746 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8747   "Mark all unread articles in this group as read, then exit.
8748 If prefix argument ALL is non-nil, all articles are marked as read."
8749   (interactive "P")
8750   (when (gnus-summary-catchup all quietly nil 'fast)
8751     ;; Select next newsgroup or exit.
8752     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8753              (eq gnus-auto-select-next 'quietly))
8754         (gnus-summary-next-group nil)
8755       (gnus-summary-exit))))
8756
8757 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8758   "Mark all articles in this newsgroup as read, and then exit."
8759   (interactive "P")
8760   (gnus-summary-catchup-and-exit t quietly))
8761
8762 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8763   "Mark all articles in this group as read and select the next group.
8764 If given a prefix, mark all articles, unread as well as ticked, as
8765 read."
8766   (interactive "P")
8767   (save-excursion
8768     (gnus-summary-catchup all))
8769   (gnus-summary-next-group))
8770
8771 ;;;
8772 ;;; with article
8773 ;;;
8774
8775 (defmacro gnus-with-article (article &rest forms)
8776   "Select ARTICLE and perform FORMS in the original article buffer.
8777 Then replace the article with the result."
8778   `(progn
8779      ;; We don't want the article to be marked as read.
8780      (let (gnus-mark-article-hook)
8781        (gnus-summary-select-article t t nil ,article))
8782      (set-buffer gnus-original-article-buffer)
8783      ,@forms
8784      (if (not (gnus-check-backend-function
8785                'request-replace-article (car gnus-article-current)))
8786          (gnus-message 5 "Read-only group; not replacing")
8787        (unless (gnus-request-replace-article
8788                 ,article (car gnus-article-current)
8789                 (current-buffer) t)
8790          (error "Couldn't replace article")))
8791      ;; The cache and backlog have to be flushed somewhat.
8792      (when gnus-keep-backlog
8793        (gnus-backlog-remove-article
8794         (car gnus-article-current) (cdr gnus-article-current)))
8795      (when gnus-use-cache
8796        (gnus-cache-update-article
8797         (car gnus-article-current) (cdr gnus-article-current)))))
8798
8799 (put 'gnus-with-article 'lisp-indent-function 1)
8800 (put 'gnus-with-article 'edebug-form-spec '(form body))
8801
8802 ;; Thread-based commands.
8803
8804 (defun gnus-summary-articles-in-thread (&optional article)
8805   "Return a list of all articles in the current thread.
8806 If ARTICLE is non-nil, return all articles in the thread that starts
8807 with that article."
8808   (let* ((article (or article (gnus-summary-article-number)))
8809          (data (gnus-data-find-list article))
8810          (top-level (gnus-data-level (car data)))
8811          (top-subject
8812           (cond ((null gnus-thread-operation-ignore-subject)
8813                  (gnus-simplify-subject-re
8814                   (mail-header-subject (gnus-data-header (car data)))))
8815                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8816                  (gnus-simplify-subject-fuzzy
8817                   (mail-header-subject (gnus-data-header (car data)))))
8818                 (t nil)))
8819          (end-point (save-excursion
8820                       (if (gnus-summary-go-to-next-thread)
8821                           (point) (point-max))))
8822          articles)
8823     (while (and data
8824                 (< (gnus-data-pos (car data)) end-point))
8825       (when (or (not top-subject)
8826                 (string= top-subject
8827                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8828                              (gnus-simplify-subject-fuzzy
8829                               (mail-header-subject
8830                                (gnus-data-header (car data))))
8831                            (gnus-simplify-subject-re
8832                             (mail-header-subject
8833                              (gnus-data-header (car data)))))))
8834         (push (gnus-data-number (car data)) articles))
8835       (unless (and (setq data (cdr data))
8836                    (> (gnus-data-level (car data)) top-level))
8837         (setq data nil)))
8838     ;; Return the list of articles.
8839     (nreverse articles)))
8840
8841 (defun gnus-summary-rethread-current ()
8842   "Rethread the thread the current article is part of."
8843   (interactive)
8844   (let* ((gnus-show-threads t)
8845          (article (gnus-summary-article-number))
8846          (id (mail-header-id (gnus-summary-article-header)))
8847          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8848     (unless id
8849       (error "No article on the current line"))
8850     (gnus-rebuild-thread id)
8851     (gnus-summary-goto-subject article)))
8852
8853 (defun gnus-summary-reparent-thread ()
8854   "Make the current article child of the marked (or previous) article.
8855
8856 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8857 is non-nil or the Subject: of both articles are the same."
8858   (interactive)
8859   (unless (not (gnus-group-read-only-p))
8860     (error "The current newsgroup does not support article editing"))
8861   (unless (<= (length gnus-newsgroup-processable) 1)
8862     (error "No more than one article may be marked"))
8863   (save-window-excursion
8864     (let ((gnus-article-buffer " *reparent*")
8865           (current-article (gnus-summary-article-number))
8866           ;; First grab the marked article, otherwise one line up.
8867           (parent-article (if (not (null gnus-newsgroup-processable))
8868                               (car gnus-newsgroup-processable)
8869                             (save-excursion
8870                               (if (eq (forward-line -1) 0)
8871                                   (gnus-summary-article-number)
8872                                 (error "Beginning of summary buffer"))))))
8873       (unless (not (eq current-article parent-article))
8874         (error "An article may not be self-referential"))
8875       (let ((message-id (mail-header-id
8876                          (gnus-summary-article-header parent-article))))
8877         (unless (and message-id (not (equal message-id "")))
8878           (error "No message-id in desired parent"))
8879         (gnus-with-article current-article
8880           (save-restriction
8881             (goto-char (point-min))
8882             (message-narrow-to-head)
8883             (if (re-search-forward "^References: " nil t)
8884                 (progn
8885                   (re-search-forward "^[^ \t]" nil t)
8886                   (forward-line -1)
8887                   (end-of-line)
8888                   (insert " " message-id))
8889               (insert "References: " message-id "\n"))))
8890         (set-buffer gnus-summary-buffer)
8891         (gnus-summary-unmark-all-processable)
8892         (gnus-summary-update-article current-article)
8893         (gnus-summary-rethread-current)
8894         (gnus-message 3 "Article %d is now the child of article %d"
8895                       current-article parent-article)))))
8896
8897 (defun gnus-summary-toggle-threads (&optional arg)
8898   "Toggle showing conversation threads.
8899 If ARG is positive number, turn showing conversation threads on."
8900   (interactive "P")
8901   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8902     (setq gnus-show-threads
8903           (if (null arg) (not gnus-show-threads)
8904             (> (prefix-numeric-value arg) 0)))
8905     (gnus-summary-prepare)
8906     (gnus-summary-goto-subject current)
8907     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8908     (gnus-summary-position-point)))
8909
8910 (defun gnus-summary-show-all-threads ()
8911   "Show all threads."
8912   (interactive)
8913   (save-excursion
8914     (let ((buffer-read-only nil))
8915       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8916   (gnus-summary-position-point))
8917
8918 (defun gnus-summary-show-thread ()
8919   "Show thread subtrees.
8920 Returns nil if no thread was there to be shown."
8921   (interactive)
8922   (let ((buffer-read-only nil)
8923         (orig (point))
8924         ;; first goto end then to beg, to have point at beg after let
8925         (end (progn (end-of-line) (point)))
8926         (beg (progn (beginning-of-line) (point))))
8927     (prog1
8928         ;; Any hidden lines here?
8929         (search-forward "\r" end t)
8930       (subst-char-in-region beg end ?\^M ?\n t)
8931       (goto-char orig)
8932       (gnus-summary-position-point))))
8933
8934 (defun gnus-summary-hide-all-threads ()
8935   "Hide all thread subtrees."
8936   (interactive)
8937   (save-excursion
8938     (goto-char (point-min))
8939     (gnus-summary-hide-thread)
8940     (while (zerop (gnus-summary-next-thread 1 t))
8941       (gnus-summary-hide-thread)))
8942   (gnus-summary-position-point))
8943
8944 (defun gnus-summary-hide-thread ()
8945   "Hide thread subtrees.
8946 Returns nil if no threads were there to be hidden."
8947   (interactive)
8948   (let ((buffer-read-only nil)
8949         (start (point))
8950         (article (gnus-summary-article-number)))
8951     (goto-char start)
8952     ;; Go forward until either the buffer ends or the subthread
8953     ;; ends.
8954     (when (and (not (eobp))
8955                (or (zerop (gnus-summary-next-thread 1 t))
8956                    (goto-char (point-max))))
8957       (prog1
8958           (if (and (> (point) start)
8959                    (search-backward "\n" start t))
8960               (progn
8961                 (subst-char-in-region start (point) ?\n ?\^M)
8962                 (gnus-summary-goto-subject article))
8963             (goto-char start)
8964             nil)))))
8965
8966 (defun gnus-summary-go-to-next-thread (&optional previous)
8967   "Go to the same level (or less) next thread.
8968 If PREVIOUS is non-nil, go to previous thread instead.
8969 Return the article number moved to, or nil if moving was impossible."
8970   (let ((level (gnus-summary-thread-level))
8971         (way (if previous -1 1))
8972         (beg (point)))
8973     (forward-line way)
8974     (while (and (not (eobp))
8975                 (< level (gnus-summary-thread-level)))
8976       (forward-line way))
8977     (if (eobp)
8978         (progn
8979           (goto-char beg)
8980           nil)
8981       (setq beg (point))
8982       (prog1
8983           (gnus-summary-article-number)
8984         (goto-char beg)))))
8985
8986 (defun gnus-summary-next-thread (n &optional silent)
8987   "Go to the same level next N'th thread.
8988 If N is negative, search backward instead.
8989 Returns the difference between N and the number of skips actually
8990 done.
8991
8992 If SILENT, don't output messages."
8993   (interactive "p")
8994   (let ((backward (< n 0))
8995         (n (abs n)))
8996     (while (and (> n 0)
8997                 (gnus-summary-go-to-next-thread backward))
8998       (decf n))
8999     (unless silent
9000       (gnus-summary-position-point))
9001     (when (and (not silent) (/= 0 n))
9002       (gnus-message 7 "No more threads"))
9003     n))
9004
9005 (defun gnus-summary-prev-thread (n)
9006   "Go to the same level previous N'th thread.
9007 Returns the difference between N and the number of skips actually
9008 done."
9009   (interactive "p")
9010   (gnus-summary-next-thread (- n)))
9011
9012 (defun gnus-summary-go-down-thread ()
9013   "Go down one level in the current thread."
9014   (let ((children (gnus-summary-article-children)))
9015     (when children
9016       (gnus-summary-goto-subject (car children)))))
9017
9018 (defun gnus-summary-go-up-thread ()
9019   "Go up one level in the current thread."
9020   (let ((parent (gnus-summary-article-parent)))
9021     (when parent
9022       (gnus-summary-goto-subject parent))))
9023
9024 (defun gnus-summary-down-thread (n)
9025   "Go down thread N steps.
9026 If N is negative, go up instead.
9027 Returns the difference between N and how many steps down that were
9028 taken."
9029   (interactive "p")
9030   (let ((up (< n 0))
9031         (n (abs n)))
9032     (while (and (> n 0)
9033                 (if up (gnus-summary-go-up-thread)
9034                   (gnus-summary-go-down-thread)))
9035       (setq n (1- n)))
9036     (gnus-summary-position-point)
9037     (when (/= 0 n)
9038       (gnus-message 7 "Can't go further"))
9039     n))
9040
9041 (defun gnus-summary-up-thread (n)
9042   "Go up thread N steps.
9043 If N is negative, go up instead.
9044 Returns the difference between N and how many steps down that were
9045 taken."
9046   (interactive "p")
9047   (gnus-summary-down-thread (- n)))
9048
9049 (defun gnus-summary-top-thread ()
9050   "Go to the top of the thread."
9051   (interactive)
9052   (while (gnus-summary-go-up-thread))
9053   (gnus-summary-article-number))
9054
9055 (defun gnus-summary-kill-thread (&optional unmark)
9056   "Mark articles under current thread as read.
9057 If the prefix argument is positive, remove any kinds of marks.
9058 If the prefix argument is negative, tick articles instead."
9059   (interactive "P")
9060   (when unmark
9061     (setq unmark (prefix-numeric-value unmark)))
9062   (let ((articles (gnus-summary-articles-in-thread)))
9063     (save-excursion
9064       ;; Expand the thread.
9065       (gnus-summary-show-thread)
9066       ;; Mark all the articles.
9067       (while articles
9068         (gnus-summary-goto-subject (car articles))
9069         (cond ((null unmark)
9070                (gnus-summary-mark-article-as-read gnus-killed-mark))
9071               ((> unmark 0)
9072                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9073               (t
9074                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9075         (setq articles (cdr articles))))
9076     ;; Hide killed subtrees.
9077     (and (null unmark)
9078          gnus-thread-hide-killed
9079          (gnus-summary-hide-thread))
9080     ;; If marked as read, go to next unread subject.
9081     (when (null unmark)
9082       ;; Go to next unread subject.
9083       (gnus-summary-next-subject 1 t)))
9084   (gnus-set-mode-line 'summary))
9085
9086 ;; Summary sorting commands
9087
9088 (defun gnus-summary-sort-by-number (&optional reverse)
9089   "Sort the summary buffer by article number.
9090 Argument REVERSE means reverse order."
9091   (interactive "P")
9092   (gnus-summary-sort 'number reverse))
9093
9094 (defun gnus-summary-sort-by-author (&optional reverse)
9095   "Sort the summary buffer by author name alphabetically.
9096 If `case-fold-search' is non-nil, case of letters is ignored.
9097 Argument REVERSE means reverse order."
9098   (interactive "P")
9099   (gnus-summary-sort 'author reverse))
9100
9101 (defun gnus-summary-sort-by-subject (&optional reverse)
9102   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9103 If `case-fold-search' is non-nil, case of letters is ignored.
9104 Argument REVERSE means reverse order."
9105   (interactive "P")
9106   (gnus-summary-sort 'subject reverse))
9107
9108 (defun gnus-summary-sort-by-date (&optional reverse)
9109   "Sort the summary buffer by date.
9110 Argument REVERSE means reverse order."
9111   (interactive "P")
9112   (gnus-summary-sort 'date reverse))
9113
9114 (defun gnus-summary-sort-by-score (&optional reverse)
9115   "Sort the summary buffer by score.
9116 Argument REVERSE means reverse order."
9117   (interactive "P")
9118   (gnus-summary-sort 'score reverse))
9119
9120 (defun gnus-summary-sort-by-lines (&optional reverse)
9121   "Sort the summary buffer by the number of lines.
9122 Argument REVERSE means reverse order."
9123   (interactive "P")
9124   (gnus-summary-sort 'lines reverse))
9125
9126 (defun gnus-summary-sort-by-chars (&optional reverse)
9127   "Sort the summary buffer by article length.
9128 Argument REVERSE means reverse order."
9129   (interactive "P")
9130   (gnus-summary-sort 'chars reverse))
9131
9132 (defun gnus-summary-sort (predicate reverse)
9133   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9134   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9135          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9136          (gnus-thread-sort-functions
9137           (if (not reverse)
9138               thread
9139             `(lambda (t1 t2)
9140                (,thread t2 t1))))
9141          (gnus-sort-gathered-threads-function
9142           gnus-thread-sort-functions)
9143          (gnus-article-sort-functions
9144           (if (not reverse)
9145               article
9146             `(lambda (t1 t2)
9147                (,article t2 t1))))
9148          (buffer-read-only)
9149          (gnus-summary-prepare-hook nil))
9150     ;; We do the sorting by regenerating the threads.
9151     (gnus-summary-prepare)
9152     ;; Hide subthreads if needed.
9153     (when (and gnus-show-threads gnus-thread-hide-subtree)
9154       (gnus-summary-hide-all-threads))))
9155
9156 ;; Summary saving commands.
9157
9158 (defun gnus-summary-save-article (&optional n not-saved)
9159   "Save the current article using the default saver function.
9160 If N is a positive number, save the N next articles.
9161 If N is a negative number, save the N previous articles.
9162 If N is nil and any articles have been marked with the process mark,
9163 save those articles instead.
9164 The variable `gnus-default-article-saver' specifies the saver function."
9165   (interactive "P")
9166   (let* ((articles (gnus-summary-work-articles n))
9167          (save-buffer (save-excursion
9168                         (nnheader-set-temp-buffer " *Gnus Save*")))
9169          (num (length articles))
9170          header file)
9171     (dolist (article articles)
9172       (setq header (gnus-summary-article-header article))
9173       (if (not (vectorp header))
9174           ;; This is a pseudo-article.
9175           (if (assq 'name header)
9176               (gnus-copy-file (cdr (assq 'name header)))
9177             (gnus-message 1 "Article %d is unsaveable" article))
9178         ;; This is a real article.
9179         (save-window-excursion
9180           (gnus-summary-select-article t nil nil article))
9181         (save-excursion
9182           (set-buffer save-buffer)
9183           (erase-buffer)
9184           (insert-buffer-substring gnus-original-article-buffer))
9185         (setq file (gnus-article-save save-buffer file num))
9186         (gnus-summary-remove-process-mark article)
9187         (unless not-saved
9188           (gnus-summary-set-saved-mark article))))
9189     (gnus-kill-buffer save-buffer)
9190     (gnus-summary-position-point)
9191     (gnus-set-mode-line 'summary)
9192     n))
9193
9194 (defun gnus-summary-pipe-output (&optional arg)
9195   "Pipe the current article to a subprocess.
9196 If N is a positive number, pipe the N next articles.
9197 If N is a negative number, pipe the N previous articles.
9198 If N is nil and any articles have been marked with the process mark,
9199 pipe those articles instead."
9200   (interactive "P")
9201   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9202     (gnus-summary-save-article arg t))
9203   (gnus-configure-windows 'pipe))
9204
9205 (defun gnus-summary-save-article-mail (&optional arg)
9206   "Append the current article to an mail file.
9207 If N is a positive number, save the N next articles.
9208 If N is a negative number, save the N previous articles.
9209 If N is nil and any articles have been marked with the process mark,
9210 save those articles instead."
9211   (interactive "P")
9212   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9213     (gnus-summary-save-article arg)))
9214
9215 (defun gnus-summary-save-article-rmail (&optional arg)
9216   "Append the current article to an rmail file.
9217 If N is a positive number, save the N next articles.
9218 If N is a negative number, save the N previous articles.
9219 If N is nil and any articles have been marked with the process mark,
9220 save those articles instead."
9221   (interactive "P")
9222   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9223     (gnus-summary-save-article arg)))
9224
9225 (defun gnus-summary-save-article-file (&optional arg)
9226   "Append the current article to a file.
9227 If N is a positive number, save the N next articles.
9228 If N is a negative number, save the N previous articles.
9229 If N is nil and any articles have been marked with the process mark,
9230 save those articles instead."
9231   (interactive "P")
9232   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9233     (gnus-summary-save-article arg)))
9234
9235 (defun gnus-summary-write-article-file (&optional arg)
9236   "Write the current article to a file, deleting the previous file.
9237 If N is a positive number, save the N next articles.
9238 If N is a negative number, save the N previous articles.
9239 If N is nil and any articles have been marked with the process mark,
9240 save those articles instead."
9241   (interactive "P")
9242   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9243     (gnus-summary-save-article arg)))
9244
9245 (defun gnus-summary-save-article-body-file (&optional arg)
9246   "Append the current article body to a file.
9247 If N is a positive number, save the N next articles.
9248 If N is a negative number, save the N previous articles.
9249 If N is nil and any articles have been marked with the process mark,
9250 save those articles instead."
9251   (interactive "P")
9252   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9253     (gnus-summary-save-article arg)))
9254
9255 (defun gnus-summary-pipe-message (program)
9256   "Pipe the current article through PROGRAM."
9257   (interactive "sProgram: ")
9258   (gnus-summary-select-article)
9259   (let ((mail-header-separator ""))
9260     (gnus-eval-in-buffer-window gnus-article-buffer
9261       (save-restriction
9262         (widen)
9263         (let ((start (window-start))
9264               buffer-read-only)
9265           (message-pipe-buffer-body program)
9266           (set-window-start (get-buffer-window (current-buffer)) start))))))
9267
9268 (defun gnus-get-split-value (methods)
9269   "Return a value based on the split METHODS."
9270   (let (split-name method result match)
9271     (when methods
9272       (save-excursion
9273         (set-buffer gnus-original-article-buffer)
9274         (save-restriction
9275           (nnheader-narrow-to-headers)
9276           (while (and methods (not split-name))
9277             (goto-char (point-min))
9278             (setq method (pop methods))
9279             (setq match (car method))
9280             (when (cond
9281                    ((stringp match)
9282                     ;; Regular expression.
9283                     (ignore-errors
9284                       (re-search-forward match nil t)))
9285                    ((gnus-functionp match)
9286                     ;; Function.
9287                     (save-restriction
9288                       (widen)
9289                       (setq result (funcall match gnus-newsgroup-name))))
9290                    ((consp match)
9291                     ;; Form.
9292                     (save-restriction
9293                       (widen)
9294                       (setq result (eval match)))))
9295               (setq split-name (cdr method))
9296               (cond ((stringp result)
9297                      (push (expand-file-name
9298                             result gnus-article-save-directory)
9299                            split-name))
9300                     ((consp result)
9301                      (setq split-name (append result split-name)))))))))
9302     (nreverse split-name)))
9303
9304 (defun gnus-valid-move-group-p (group)
9305   (and (boundp group)
9306        (symbol-name group)
9307        (symbol-value group)
9308        (gnus-get-function (gnus-find-method-for-group
9309                            (symbol-name group)) 'request-accept-article t)))
9310
9311 (defun gnus-read-move-group-name (prompt default articles prefix)
9312   "Read a group name."
9313   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9314          (minibuffer-confirm-incomplete nil) ; XEmacs
9315          (prom
9316           (format "%s %s to:"
9317                   prompt
9318                   (if (> (length articles) 1)
9319                       (format "these %d articles" (length articles))
9320                     "this article")))
9321          (to-newsgroup
9322           (cond
9323            ((null split-name)
9324             (gnus-completing-read default prom
9325                                   gnus-active-hashtb
9326                                   'gnus-valid-move-group-p
9327                                   nil prefix
9328                                   'gnus-group-history))
9329            ((= 1 (length split-name))
9330             (gnus-completing-read (car split-name) prom
9331                                   gnus-active-hashtb
9332                                   'gnus-valid-move-group-p
9333                                   nil nil
9334                                   'gnus-group-history))
9335            (t
9336             (gnus-completing-read nil prom
9337                                   (mapcar (lambda (el) (list el))
9338                                           (nreverse split-name))
9339                                   nil nil nil
9340                                   'gnus-group-history))))
9341          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9342     (when to-newsgroup
9343       (if (or (string= to-newsgroup "")
9344               (string= to-newsgroup prefix))
9345           (setq to-newsgroup default))
9346       (unless to-newsgroup
9347         (error "No group name entered"))
9348       (or (gnus-active to-newsgroup)
9349           (gnus-activate-group to-newsgroup nil nil to-method)
9350           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9351                                      to-newsgroup))
9352               (or (and (gnus-request-create-group to-newsgroup to-method)
9353                        (gnus-activate-group
9354                         to-newsgroup nil nil to-method)
9355                        (gnus-subscribe-group to-newsgroup))
9356                   (error "Couldn't create group %s" to-newsgroup)))
9357           (error "No such group: %s" to-newsgroup)))
9358     to-newsgroup))
9359
9360 (defun gnus-summary-save-parts (type dir n &optional reverse)
9361   "Save parts matching TYPE to DIR.
9362 If REVERSE, save parts that do not match TYPE."
9363   (interactive
9364    (list (read-string "Save parts of type: " 
9365                       (or (car gnus-summary-save-parts-type-history)
9366                           gnus-summary-save-parts-default-mime)
9367                       'gnus-summary-save-parts-type-history)
9368          (setq gnus-summary-save-parts-last-directory
9369                (read-file-name "Save to directory: " 
9370                                gnus-summary-save-parts-last-directory
9371                                nil t))
9372          current-prefix-arg))
9373   (gnus-summary-iterate n
9374     (let ((gnus-display-mime-function nil)
9375           (gnus-inhibit-treatment t))
9376       (gnus-summary-select-article))
9377     (save-excursion
9378       (set-buffer gnus-article-buffer)
9379       (let ((handles (or gnus-article-mime-handles
9380                          (mm-dissect-buffer) (mm-uu-dissect))))
9381         (when handles
9382           (gnus-summary-save-parts-1 type dir handles reverse)
9383           (unless gnus-article-mime-handles ;; Don't destroy this case.
9384             (mm-destroy-parts handles)))))))
9385
9386 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9387   (if (stringp (car handle))
9388       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9389               (cdr handle))
9390     (when (if reverse
9391               (not (string-match type (mm-handle-media-type handle)))
9392             (string-match type (mm-handle-media-type handle)))
9393       (let ((file (expand-file-name
9394                    (file-name-nondirectory
9395                     (or
9396                      (mail-content-type-get
9397                       (mm-handle-disposition handle) 'filename)
9398                      (concat gnus-newsgroup-name
9399                              "." (number-to-string
9400                                   (cdr gnus-article-current)))))
9401                    dir)))
9402         (unless (file-exists-p file)
9403           (mm-save-part-to-file handle file))))))
9404
9405 ;; Summary extract commands
9406
9407 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9408   (let ((buffer-read-only nil)
9409         (article (gnus-summary-article-number))
9410         after-article b e)
9411     (unless (gnus-summary-goto-subject article)
9412       (error "No such article: %d" article))
9413     (gnus-summary-position-point)
9414     ;; If all commands are to be bunched up on one line, we collect
9415     ;; them here.
9416     (unless gnus-view-pseudos-separately
9417       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9418             files action)
9419         (while ps
9420           (setq action (cdr (assq 'action (car ps))))
9421           (setq files (list (cdr (assq 'name (car ps)))))
9422           (while (and ps (cdr ps)
9423                       (string= (or action "1")
9424                                (or (cdr (assq 'action (cadr ps))) "2")))
9425             (push (cdr (assq 'name (cadr ps))) files)
9426             (setcdr ps (cddr ps)))
9427           (when files
9428             (when (not (string-match "%s" action))
9429               (push " " files))
9430             (push " " files)
9431             (when (assq 'execute (car ps))
9432               (setcdr (assq 'execute (car ps))
9433                       (funcall (if (string-match "%s" action)
9434                                    'format 'concat)
9435                                action
9436                                (mapconcat
9437                                 (lambda (f)
9438                                   (if (equal f " ")
9439                                       f
9440                                     (gnus-quote-arg-for-sh-or-csh f)))
9441                                 files " ")))))
9442           (setq ps (cdr ps)))))
9443     (if (and gnus-view-pseudos (not not-view))
9444         (while pslist
9445           (when (assq 'execute (car pslist))
9446             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9447                                   (eq gnus-view-pseudos 'not-confirm)))
9448           (setq pslist (cdr pslist)))
9449       (save-excursion
9450         (while pslist
9451           (setq after-article (or (cdr (assq 'article (car pslist)))
9452                                   (gnus-summary-article-number)))
9453           (gnus-summary-goto-subject after-article)
9454           (forward-line 1)
9455           (setq b (point))
9456           (insert "    " (file-name-nondirectory
9457                           (cdr (assq 'name (car pslist))))
9458                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9459           (setq e (point))
9460           (forward-line -1)             ; back to `b'
9461           (gnus-add-text-properties
9462            b (1- e) (list 'gnus-number gnus-reffed-article-number
9463                           gnus-mouse-face-prop gnus-mouse-face))
9464           (gnus-data-enter
9465            after-article gnus-reffed-article-number
9466            gnus-unread-mark b (car pslist) 0 (- e b))
9467           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9468           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9469           (setq pslist (cdr pslist)))))))
9470
9471 (defun gnus-pseudos< (p1 p2)
9472   (let ((c1 (cdr (assq 'action p1)))
9473         (c2 (cdr (assq 'action p2))))
9474     (and c1 c2 (string< c1 c2))))
9475
9476 (defun gnus-request-pseudo-article (props)
9477   (cond ((assq 'execute props)
9478          (gnus-execute-command (cdr (assq 'execute props)))))
9479   (let ((gnus-current-article (gnus-summary-article-number)))
9480     (gnus-run-hooks 'gnus-mark-article-hook)))
9481
9482 (defun gnus-execute-command (command &optional automatic)
9483   (save-excursion
9484     (gnus-article-setup-buffer)
9485     (set-buffer gnus-article-buffer)
9486     (setq buffer-read-only nil)
9487     (let ((command (if automatic command
9488                      (read-string "Command: " (cons command 0)))))
9489       (erase-buffer)
9490       (insert "$ " command "\n\n")
9491       (if gnus-view-pseudo-asynchronously
9492           (start-process "gnus-execute" (current-buffer) shell-file-name
9493                          shell-command-switch command)
9494         (call-process shell-file-name nil t nil
9495                       shell-command-switch command)))))
9496
9497 ;; Summary kill commands.
9498
9499 (defun gnus-summary-edit-global-kill (article)
9500   "Edit the \"global\" kill file."
9501   (interactive (list (gnus-summary-article-number)))
9502   (gnus-group-edit-global-kill article))
9503
9504 (defun gnus-summary-edit-local-kill ()
9505   "Edit a local kill file applied to the current newsgroup."
9506   (interactive)
9507   (setq gnus-current-headers (gnus-summary-article-header))
9508   (gnus-group-edit-local-kill
9509    (gnus-summary-article-number) gnus-newsgroup-name))
9510
9511 ;;; Header reading.
9512
9513 (defun gnus-read-header (id &optional header)
9514   "Read the headers of article ID and enter them into the Gnus system."
9515   (let ((group gnus-newsgroup-name)
9516         (gnus-override-method
9517          (or
9518           gnus-override-method
9519           (and (gnus-news-group-p gnus-newsgroup-name)
9520                (car (gnus-refer-article-methods)))))
9521         where)
9522     ;; First we check to see whether the header in question is already
9523     ;; fetched.
9524     (if (stringp id)
9525         ;; This is a Message-ID.
9526         (setq header (or header (gnus-id-to-header id)))
9527       ;; This is an article number.
9528       (setq header (or header (gnus-summary-article-header id))))
9529     (if (and header
9530              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9531         ;; We have found the header.
9532         header
9533       ;; If this is a sparse article, we have to nix out its
9534       ;; previous entry in the thread hashtb.
9535       (when (and header
9536                  (gnus-summary-article-sparse-p (mail-header-number header)))
9537         (let* ((parent (gnus-parent-id (mail-header-references header)))
9538                (thread (and parent (gnus-id-to-thread parent))))
9539           (when thread
9540             (delq (assq header thread) thread))))
9541       ;; We have to really fetch the header to this article.
9542       (save-excursion
9543         (set-buffer nntp-server-buffer)
9544         (when (setq where (gnus-request-head id group))
9545           (nnheader-fold-continuation-lines)
9546           (goto-char (point-max))
9547           (insert ".\n")
9548           (goto-char (point-min))
9549           (insert "211 ")
9550           (princ (cond
9551                   ((numberp id) id)
9552                   ((cdr where) (cdr where))
9553                   (header (mail-header-number header))
9554                   (t gnus-reffed-article-number))
9555                  (current-buffer))
9556           (insert " Article retrieved.\n"))
9557         (if (or (not where)
9558                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9559             ()                          ; Malformed head.
9560           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9561             (when (and (stringp id)
9562                        (not (string= (gnus-group-real-name group)
9563                                      (car where))))
9564               ;; If we fetched by Message-ID and the article came
9565               ;; from a different group, we fudge some bogus article
9566               ;; numbers for this article.
9567               (mail-header-set-number header gnus-reffed-article-number))
9568             (save-excursion
9569               (set-buffer gnus-summary-buffer)
9570               (decf gnus-reffed-article-number)
9571               (gnus-remove-header (mail-header-number header))
9572               (push header gnus-newsgroup-headers)
9573               (setq gnus-current-headers header)
9574               (push (mail-header-number header) gnus-newsgroup-limit)))
9575           header)))))
9576
9577 (defun gnus-remove-header (number)
9578   "Remove header NUMBER from `gnus-newsgroup-headers'."
9579   (if (and gnus-newsgroup-headers
9580            (= number (mail-header-number (car gnus-newsgroup-headers))))
9581       (pop gnus-newsgroup-headers)
9582     (let ((headers gnus-newsgroup-headers))
9583       (while (and (cdr headers)
9584                   (not (= number (mail-header-number (cadr headers)))))
9585         (pop headers))
9586       (when (cdr headers)
9587         (setcdr headers (cddr headers))))))
9588
9589 ;;;
9590 ;;; summary highlights
9591 ;;;
9592
9593 (defun gnus-highlight-selected-summary ()
9594   "Highlight selected article in summary buffer."
9595   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9596   (when gnus-summary-selected-face
9597     (save-excursion
9598       (let* ((beg (progn (beginning-of-line) (point)))
9599              (end (progn (end-of-line) (point)))
9600              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9601              (from (if (get-text-property beg gnus-mouse-face-prop)
9602                        beg
9603                      (or (next-single-property-change
9604                           beg gnus-mouse-face-prop nil end)
9605                          beg)))
9606              (to
9607               (if (= from end)
9608                   (- from 2)
9609                 (or (next-single-property-change
9610                      from gnus-mouse-face-prop nil end)
9611                     end))))
9612         ;; If no mouse-face prop on line we will have to = from = end,
9613         ;; so we highlight the entire line instead.
9614         (when (= (+ to 2) from)
9615           (setq from beg)
9616           (setq to end))
9617         (if gnus-newsgroup-selected-overlay
9618             ;; Move old overlay.
9619             (gnus-move-overlay
9620              gnus-newsgroup-selected-overlay from to (current-buffer))
9621           ;; Create new overlay.
9622           (gnus-overlay-put
9623            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9624            'face gnus-summary-selected-face))))))
9625
9626 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9627 (defun gnus-summary-highlight-line ()
9628   "Highlight current line according to `gnus-summary-highlight'."
9629   (let* ((list gnus-summary-highlight)
9630          (p (point))
9631          (end (progn (end-of-line) (point)))
9632          ;; now find out where the line starts and leave point there.
9633          (beg (progn (beginning-of-line) (point)))
9634          (article (gnus-summary-article-number))
9635          (score (or (cdr (assq (or article gnus-current-article)
9636                                gnus-newsgroup-scored))
9637                     gnus-summary-default-score 0))
9638          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9639          (inhibit-read-only t))
9640     ;; Eval the cars of the lists until we find a match.
9641     (let ((default gnus-summary-default-score))
9642       (while (and list
9643                   (not (eval (caar list))))
9644         (setq list (cdr list))))
9645     (let ((face (cdar list)))
9646       (unless (eq face (get-text-property beg 'face))
9647         (gnus-put-text-property-excluding-characters-with-faces
9648          beg end 'face
9649          (setq face (if (boundp face) (symbol-value face) face)))
9650         (when gnus-summary-highlight-line-function
9651           (funcall gnus-summary-highlight-line-function article face))))
9652     (goto-char p)))
9653
9654 (defun gnus-update-read-articles (group unread &optional compute)
9655   "Update the list of read articles in GROUP."
9656   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9657          (entry (gnus-gethash group gnus-newsrc-hashtb))
9658          (info (nth 2 entry))
9659          (prev 1)
9660          (unread (sort (copy-sequence unread) '<))
9661          read)
9662     (if (or (not info) (not active))
9663         ;; There is no info on this group if it was, in fact,
9664         ;; killed.  Gnus stores no information on killed groups, so
9665         ;; there's nothing to be done.
9666         ;; One could store the information somewhere temporarily,
9667         ;; perhaps...  Hmmm...
9668         ()
9669       ;; Remove any negative articles numbers.
9670       (while (and unread (< (car unread) 0))
9671         (setq unread (cdr unread)))
9672       ;; Remove any expired article numbers
9673       (while (and unread (< (car unread) (car active)))
9674         (setq unread (cdr unread)))
9675       ;; Compute the ranges of read articles by looking at the list of
9676       ;; unread articles.
9677       (while unread
9678         (when (/= (car unread) prev)
9679           (push (if (= prev (1- (car unread))) prev
9680                   (cons prev (1- (car unread))))
9681                 read))
9682         (setq prev (1+ (car unread)))
9683         (setq unread (cdr unread)))
9684       (when (<= prev (cdr active))
9685         (push (cons prev (cdr active)) read))
9686       (setq read (if (> (length read) 1) (nreverse read) read))
9687       (if compute
9688           read
9689         (save-excursion
9690           (let (setmarkundo)
9691             ;; Propagate the read marks to the backend.
9692             (when (gnus-check-backend-function 'request-set-mark group)
9693               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9694                     (add (gnus-remove-from-range read (gnus-info-read info))))
9695                 (when (or add del)
9696                   (unless (gnus-check-group group)
9697                     (error "Can't open server for %s" group))
9698                   (gnus-request-set-mark
9699                    group (delq nil (list (if add (list add 'add '(read)))
9700                                          (if del (list del 'del '(read))))))
9701                   (setq setmarkundo
9702                         `(gnus-request-set-mark
9703                           ,group
9704                           ',(delq nil (list
9705                                        (if del (list del 'add '(read)))
9706                                        (if add (list add 'del '(read))))))))))
9707             (set-buffer gnus-group-buffer)
9708             (gnus-undo-register
9709               `(progn
9710                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9711                  (gnus-info-set-read ',info ',(gnus-info-read info))
9712                  (gnus-get-unread-articles-in-group ',info 
9713                                                     (gnus-active ,group))
9714                  (gnus-group-update-group ,group t)
9715                  ,setmarkundo))))
9716         ;; Enter this list into the group info.
9717         (gnus-info-set-read info read)
9718         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9719         (gnus-get-unread-articles-in-group info (gnus-active group))
9720         t))))
9721
9722 (defun gnus-offer-save-summaries ()
9723   "Offer to save all active summary buffers."
9724   (save-excursion
9725     (let ((buflist (buffer-list))
9726           buffers bufname)
9727       ;; Go through all buffers and find all summaries.
9728       (while buflist
9729         (and (setq bufname (buffer-name (car buflist)))
9730              (string-match "Summary" bufname)
9731              (save-excursion
9732                (set-buffer bufname)
9733                ;; We check that this is, indeed, a summary buffer.
9734                (and (eq major-mode 'gnus-summary-mode)
9735                     ;; Also make sure this isn't bogus.
9736                     gnus-newsgroup-prepared
9737                     ;; Also make sure that this isn't a dead summary buffer.
9738                     (not gnus-dead-summary-mode)))
9739              (push bufname buffers))
9740         (setq buflist (cdr buflist)))
9741       ;; Go through all these summary buffers and offer to save them.
9742       (when buffers
9743         (map-y-or-n-p
9744          "Update summary buffer %s? "
9745          (lambda (buf)
9746            (switch-to-buffer buf)
9747            (gnus-summary-exit))
9748          buffers)))))
9749
9750
9751 ;;; @ for mime-partial
9752 ;;;
9753
9754 (defun gnus-request-partial-message ()
9755   (save-excursion
9756     (let ((number (gnus-summary-article-number))
9757           (group gnus-newsgroup-name)
9758           (mother gnus-article-buffer))
9759       (set-buffer (get-buffer-create " *Partial Article*"))
9760       (erase-buffer)
9761       (setq mime-preview-buffer mother)
9762       (gnus-request-article-this-buffer number group)
9763       (mime-parse-buffer)
9764       )))
9765
9766 (autoload 'mime-combine-message/partial-pieces-automatically
9767   "mime-partial"
9768   "Internal method to combine message/partial messages automatically.")
9769
9770 (mime-add-condition
9771  'action '((type . message)(subtype . partial)
9772            (major-mode . gnus-original-article-mode)
9773            (method . mime-combine-message/partial-pieces-automatically)
9774            (summary-buffer-exp . gnus-summary-buffer)
9775            (request-partial-message-method . gnus-request-partial-message)
9776            ))
9777
9778
9779 ;;; @ for message/rfc822
9780 ;;;
9781
9782 (defun gnus-mime-extract-message/rfc822 (entity situation)
9783   (let (group article num cwin swin cur)
9784     (with-temp-buffer
9785       (mime-insert-entity-content entity)
9786       (setq group (or (cdr (assq 'group situation))
9787                       (completing-read "Group: "
9788                                        gnus-active-hashtb
9789                                        nil
9790                                        (gnus-read-active-file-p)
9791                                        gnus-newsgroup-name))
9792             article (gnus-request-accept-article group)))
9793     (when (and (consp article)
9794                (numberp (setq article (cdr article))))
9795       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9796             cwin (get-buffer-window (current-buffer) t))
9797       (save-window-excursion
9798         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9799             (select-window swin)
9800           (set-buffer gnus-summary-buffer))
9801         (setq cur gnus-current-article)
9802         (forward-line num)
9803         (let (gnus-show-threads)
9804           (gnus-summary-goto-subject article t))
9805         (gnus-summary-clear-mark-forward 1)
9806         (gnus-summary-goto-subject cur))
9807       (when (and cwin (window-frame cwin))
9808         (select-frame (window-frame cwin)))
9809       (when (boundp 'mime-acting-situation-to-override)
9810         (set-alist 'mime-acting-situation-to-override
9811                    'group
9812                    group)
9813         (set-alist 'mime-acting-situation-to-override
9814                    'after-method
9815                    `(progn
9816                       (save-current-buffer
9817                         (set-buffer gnus-group-buffer)
9818                         (gnus-activate-group ,group))
9819                       (gnus-summary-goto-article ,cur
9820                                                  gnus-show-all-headers)))
9821         (set-alist 'mime-acting-situation-to-override
9822                    'number num)))))
9823
9824 (mime-add-condition
9825  'action '((type . message)(subtype . rfc822)
9826            (major-mode . gnus-original-article-mode)
9827            (method . gnus-mime-extract-message/rfc822)
9828            (mode . "extract")
9829            ))
9830
9831 (mime-add-condition
9832  'action '((type . message)(subtype . news)
9833            (major-mode . gnus-original-article-mode)
9834            (method . gnus-mime-extract-message/rfc822)
9835            (mode . "extract")
9836            ))
9837
9838 (defun gnus-mime-extract-multipart (entity situation)
9839   (let ((children (mime-entity-children entity))
9840         mime-acting-situation-to-override
9841         f)
9842     (while children
9843       (mime-play-entity (car children)
9844                         (cons (assq 'mode situation)
9845                               mime-acting-situation-to-override))
9846       (setq children (cdr children)))
9847     (if (setq f (cdr (assq 'after-method
9848                            mime-acting-situation-to-override)))
9849         (eval f)
9850       )))
9851
9852 (mime-add-condition
9853  'action '((type . multipart)
9854            (method . gnus-mime-extract-multipart)
9855            (mode . "extract")
9856            )
9857  'with-default)
9858
9859
9860 ;;; @ end
9861 ;;;
9862
9863 (defun gnus-summary-setup-default-charset ()
9864   "Setup newsgroup default charset."
9865   (if (equal gnus-newsgroup-name "nndraft:drafts")
9866       (setq gnus-newsgroup-charset nil)
9867     (let* ((name (and gnus-newsgroup-name
9868                       (gnus-group-real-name gnus-newsgroup-name)))
9869            (ignored-charsets
9870             (or gnus-newsgroup-ephemeral-ignored-charsets
9871                 (append
9872                  (and gnus-newsgroup-name
9873                       (or (gnus-group-find-parameter gnus-newsgroup-name
9874                                                      'ignored-charsets t)
9875                           (let ((alist gnus-group-ignored-charsets-alist)
9876                                 elem (charsets nil))
9877                             (while (setq elem (pop alist))
9878                               (when (and name
9879                                          (string-match (car elem) name))
9880                                 (setq alist nil
9881                                       charsets (cdr elem))))
9882                             charsets)))
9883                  gnus-newsgroup-ignored-charsets))))
9884       (setq gnus-newsgroup-charset
9885             (or gnus-newsgroup-ephemeral-charset
9886                 (and gnus-newsgroup-name
9887                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9888                          (let ((alist gnus-group-charset-alist)
9889                                elem charset)
9890                            (while (setq elem (pop alist))
9891                              (when (and name
9892                                         (string-match (car elem) name))
9893                                (setq alist nil
9894                                      charset (cadr elem))))
9895                            charset)))
9896                 gnus-default-charset))
9897       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9898            ignored-charsets))))
9899
9900 ;;;
9901 ;;; Mime Commands
9902 ;;;
9903
9904 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9905   "Display the current article buffer fully MIME-buttonized.
9906 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9907 treated as multipart/mixed."
9908   (interactive "P")
9909   (require 'gnus-art)
9910   (let ((gnus-unbuttonized-mime-types nil)
9911         (gnus-mime-display-multipart-as-mixed show-all-parts))
9912     (gnus-summary-show-article)))
9913
9914 (defun gnus-summary-repair-multipart (article)
9915   "Add a Content-Type header to a multipart article without one."
9916   (interactive (list (gnus-summary-article-number)))
9917   (gnus-with-article article
9918     (message-narrow-to-head)
9919     (goto-char (point-max))
9920     (widen)
9921     (when (search-forward "\n--" nil t)
9922       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9923         (message-narrow-to-head)
9924         (message-remove-header "Mime-Version")
9925         (message-remove-header "Content-Type")
9926         (goto-char (point-max))
9927         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9928                         separator))
9929         (insert "Mime-Version: 1.0\n")
9930         (widen))))
9931   (let (gnus-mark-article-hook)
9932     (gnus-summary-select-article t t nil article)))
9933
9934 (defun gnus-summary-toggle-display-buttonized ()
9935   "Toggle the buttonizing of the article buffer."
9936   (interactive)
9937   (require 'gnus-art)
9938   (if (setq gnus-inhibit-mime-unbuttonizing
9939             (not gnus-inhibit-mime-unbuttonizing))
9940       (let ((gnus-unbuttonized-mime-types nil))
9941         (gnus-summary-show-article))
9942     (gnus-summary-show-article)))
9943
9944 ;;;
9945 ;;; Intelli-mouse commmands
9946 ;;;
9947
9948 (defun gnus-wheel-summary-scroll (event)
9949   (interactive "e")
9950   (let ((amount (if (memq 'shift (event-modifiers event))
9951                     (car gnus-wheel-scroll-amount)
9952                   (cdr gnus-wheel-scroll-amount)))
9953         (direction (- (* (static-if (featurep 'xemacs)
9954                              (event-button event)
9955                            (cond ((eq 'mouse-4 (event-basic-type event))
9956                                   4)
9957                                  ((eq 'mouse-5 (event-basic-type event))
9958                                   5)))
9959                          2) 9))
9960         edge)
9961     (gnus-summary-scroll-up (* amount direction))
9962     (when (gnus-eval-in-buffer-window gnus-article-buffer
9963             (save-restriction
9964               (widen)
9965               (and (if (< 0 direction)
9966                        (gnus-article-next-page 0)
9967                      (gnus-article-prev-page 0)
9968                      (bobp))
9969                    (if (setq edge (get-text-property
9970                                    (point-min) 'gnus-wheel-edge))
9971                        (setq edge (* edge direction))
9972                      (setq edge -1))
9973                    (or (plusp edge)
9974                        (let ((buffer-read-only nil)
9975                              (inhibit-read-only t))
9976                          (put-text-property (point-min) (point-max)
9977                                             'gnus-wheel-edge direction)
9978                          nil))
9979                    (or (> edge gnus-wheel-edge-resistance)
9980                        (let ((buffer-read-only nil)
9981                              (inhibit-read-only t))
9982                          (put-text-property (point-min) (point-max)
9983                                             'gnus-wheel-edge
9984                                             (* (1+ edge) direction))
9985                          nil))
9986                    (eq last-command 'gnus-wheel-summary-scroll))))
9987       (gnus-summary-next-article nil nil (minusp direction)))))
9988
9989 (defun gnus-wheel-install ()
9990   "Enable mouse wheel support on summary window."
9991   (when gnus-use-wheel
9992     (let ((keys
9993            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9994       (dolist (key keys)
9995         (define-key gnus-summary-mode-map key
9996           'gnus-wheel-summary-scroll)))))
9997
9998 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9999
10000 ;;;
10001 ;;; Traditional PGP commmands
10002 ;;;
10003
10004 (defun gnus-summary-decrypt-article (&optional force)
10005   "Decrypt the current article in traditional PGP way.
10006 This will have permanent effect only in mail groups.
10007 If FORCE is non-nil, allow editing of articles even in read-only
10008 groups."
10009   (interactive "P")
10010   (gnus-summary-select-article t)
10011   (gnus-eval-in-buffer-window gnus-article-buffer
10012     (save-excursion
10013       (save-restriction
10014         (widen)
10015         (goto-char (point-min))
10016         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10017           (error "Not a traditional PGP message!"))
10018         (let ((armor-start (match-beginning 0)))
10019           (if (and (pgg-decrypt-region armor-start (point-max))
10020                    (or force (not (gnus-group-read-only-p))))
10021               (let ((inhibit-read-only t)
10022                     buffer-read-only)
10023                 (delete-region armor-start
10024                                (progn
10025                                  (re-search-forward "^-+END PGP" nil t)
10026                                  (beginning-of-line 2)
10027                                  (point)))
10028                 (insert-buffer-substring pgg-output-buffer))))))))
10029
10030 (defun gnus-summary-verify-article ()
10031   "Verify the current article in traditional PGP way."
10032   (interactive)
10033   (save-excursion
10034     (set-buffer gnus-original-article-buffer)
10035     (goto-char (point-min))
10036     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10037       (error "Not a traditional PGP message!"))
10038     (re-search-forward "^-+END PGP" nil t)
10039     (beginning-of-line 2)
10040     (call-interactively (function pgg-verify-region))))
10041
10042 ;;;
10043 ;;; Generic summary marking commands
10044 ;;;
10045
10046 (defvar gnus-summary-marking-alist
10047   '((read gnus-del-mark "d")
10048     (unread gnus-unread-mark "u")
10049     (ticked gnus-ticked-mark "!")
10050     (dormant gnus-dormant-mark "?")
10051     (expirable gnus-expirable-mark "e"))
10052   "An alist of names/marks/keystrokes.")
10053
10054 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10055 (defvar gnus-summary-mark-map)
10056
10057 (defun gnus-summary-make-all-marking-commands ()
10058   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10059   (dolist (elem gnus-summary-marking-alist)
10060     (apply 'gnus-summary-make-marking-command elem)))
10061
10062 (defun gnus-summary-make-marking-command (name mark keystroke)
10063   (let ((map (make-sparse-keymap)))
10064     (define-key gnus-summary-generic-mark-map keystroke map)
10065     (dolist (lway `((next "next" next nil "n")
10066                     (next-unread "next unread" next t "N")
10067                     (prev "previous" prev nil "p")
10068                     (prev-unread "previous unread" prev t "P")
10069                     (nomove "" nil nil ,keystroke)))
10070       (let ((func (gnus-summary-make-marking-command-1
10071                    mark (car lway) lway name)))
10072         (setq func (eval func))
10073         (define-key map (nth 4 lway) func)))))
10074
10075 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10076   `(defun ,(intern
10077             (format "gnus-summary-put-mark-as-%s%s"
10078                     name (if (eq way 'nomove)
10079                              ""
10080                            (concat "-" (symbol-name way)))))
10081      (n)
10082      ,(format
10083        "Mark the current article as %s%s.
10084 If N, the prefix, then repeat N times.
10085 If N is negative, move in reverse order.
10086 The difference between N and the actual number of articles marked is
10087 returned."
10088        name (car (cdr lway)))
10089      (interactive "p")
10090      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10091
10092 (defun gnus-summary-generic-mark (n mark move unread)
10093   "Mark N articles with MARK."
10094   (unless (eq major-mode 'gnus-summary-mode)
10095     (error "This command can only be used in the summary buffer"))
10096   (gnus-summary-show-thread)
10097   (let ((nummove
10098          (cond
10099           ((eq move 'next) 1)
10100           ((eq move 'prev) -1)
10101           (t 0))))
10102     (if (zerop nummove)
10103         (setq n 1)
10104       (when (< n 0)
10105         (setq n (abs n)
10106               nummove (* -1 nummove))))
10107     (while (and (> n 0)
10108                 (gnus-summary-mark-article nil mark)
10109                 (zerop (gnus-summary-next-subject nummove unread t)))
10110       (setq n (1- n)))
10111     (when (/= 0 n)
10112       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10113     (gnus-summary-recenter)
10114     (gnus-summary-position-point)
10115     (gnus-set-mode-line 'summary)
10116     n))
10117
10118 (gnus-summary-make-all-marking-commands)
10119
10120 (gnus-ems-redefine)
10121
10122 (provide 'gnus-sum)
10123
10124 (run-hooks 'gnus-sum-load-hook)
10125
10126 ;;; gnus-sum.el ends here