7f7a3eed435435e91843d7483eff441c39994222
[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+ (match-beginning 0))
2681                                   (1- (match-end 0))))
2682                   (substring gnus-tmp-from 0 beg))))
2683            ((string-match "(.+)" gnus-tmp-from)
2684             (substring gnus-tmp-from
2685                        (1+ (match-beginning 0)) (1- (match-end 0))))
2686            (t gnus-tmp-from)))
2687          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2688          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2689          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2690          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2691          (buffer-read-only nil))
2692     (when (string= gnus-tmp-name "")
2693       (setq gnus-tmp-name gnus-tmp-from))
2694     (unless (numberp gnus-tmp-lines)
2695       (setq gnus-tmp-lines 0))
2696     (gnus-put-text-property-excluding-characters-with-faces
2697      (point)
2698      (progn (eval gnus-summary-line-format-spec) (point))
2699      'gnus-number gnus-tmp-number)
2700     (when (gnus-visual-p 'summary-highlight 'highlight)
2701       (forward-line -1)
2702       (gnus-run-hooks 'gnus-summary-update-hook)
2703       (forward-line 1))))
2704
2705 (defun gnus-summary-update-line (&optional dont-update)
2706   "Update summary line after change."
2707   (when (and gnus-summary-default-score
2708              (not gnus-summary-inhibit-highlight))
2709     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2710            (article (gnus-summary-article-number))
2711            (score (gnus-summary-article-score article)))
2712       (unless dont-update
2713         (if (and gnus-summary-mark-below
2714                  (< (gnus-summary-article-score)
2715                     gnus-summary-mark-below))
2716             ;; This article has a low score, so we mark it as read.
2717             (when (memq article gnus-newsgroup-unreads)
2718               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2719           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2720             ;; This article was previously marked as read on account
2721             ;; of a low score, but now it has risen, so we mark it as
2722             ;; unread.
2723             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2724         (gnus-summary-update-mark
2725          (if (or (null gnus-summary-default-score)
2726                  (<= (abs (- score gnus-summary-default-score))
2727                      gnus-summary-zcore-fuzz))
2728              ?  ;Whitespace
2729            (if (< score gnus-summary-default-score)
2730                gnus-score-below-mark gnus-score-over-mark))
2731          'score))
2732       ;; Do visual highlighting.
2733       (when (gnus-visual-p 'summary-highlight 'highlight)
2734         (gnus-run-hooks 'gnus-summary-update-hook)))))
2735
2736 (defvar gnus-tmp-new-adopts nil)
2737
2738 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2739   "Return the number of articles in THREAD.
2740 This may be 0 in some cases -- if none of the articles in
2741 the thread are to be displayed."
2742   (let* ((number
2743           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2744           (cond
2745            ((not (listp thread))
2746             1)
2747            ((and (consp thread) (cdr thread))
2748             (apply
2749              '+ 1 (mapcar
2750                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2751            ((null thread)
2752             1)
2753            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2754             1)
2755            (t 0))))
2756     (when (and level (zerop level) gnus-tmp-new-adopts)
2757       (incf number
2758             (apply '+ (mapcar
2759                        'gnus-summary-number-of-articles-in-thread
2760                        gnus-tmp-new-adopts))))
2761     (if char
2762         (if (> number 1) gnus-not-empty-thread-mark
2763           gnus-empty-thread-mark)
2764       number)))
2765
2766 (defun gnus-summary-set-local-parameters (group)
2767   "Go through the local params of GROUP and set all variable specs in that list."
2768   (let ((params (gnus-group-find-parameter group))
2769         elem)
2770     (while params
2771       (setq elem (car params)
2772             params (cdr params))
2773       (and (consp elem)                 ; Has to be a cons.
2774            (consp (cdr elem))           ; The cdr has to be a list.
2775            (symbolp (car elem))         ; Has to be a symbol in there.
2776            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2777            (ignore-errors               ; So we set it.
2778              (make-local-variable (car elem))
2779              (set (car elem) (eval (nth 1 elem))))))))
2780
2781 (defun gnus-summary-read-group (group &optional show-all no-article
2782                                       kill-buffer no-display backward
2783                                       select-articles)
2784   "Start reading news in newsgroup GROUP.
2785 If SHOW-ALL is non-nil, already read articles are also listed.
2786 If NO-ARTICLE is non-nil, no article is selected initially.
2787 If NO-DISPLAY, don't generate a summary buffer."
2788   (let (result)
2789     (while (and group
2790                 (null (setq result
2791                             (let ((gnus-auto-select-next nil))
2792                               (or (gnus-summary-read-group-1
2793                                    group show-all no-article
2794                                    kill-buffer no-display
2795                                    select-articles)
2796                                   (setq show-all nil
2797                                         select-articles nil)))))
2798                 (eq gnus-auto-select-next 'quietly))
2799       (set-buffer gnus-group-buffer)
2800       ;; The entry function called above goes to the next
2801       ;; group automatically, so we go two groups back
2802       ;; if we are searching for the previous group.
2803       (when backward
2804         (gnus-group-prev-unread-group 2))
2805       (if (not (equal group (gnus-group-group-name)))
2806           (setq group (gnus-group-group-name))
2807         (setq group nil)))
2808     result))
2809
2810 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2811   "Directly jump to the other GROUP from summary buffer.
2812 If SHOW-ALL is non-nil, already read articles are also listed."
2813   (interactive
2814    (if (eq gnus-summary-buffer (current-buffer))
2815        (list (completing-read
2816               "Group: " gnus-active-hashtb nil t
2817               (when (and gnus-newsgroup-name
2818                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2819                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2820               'gnus-group-history)
2821              current-prefix-arg)
2822      (error "%s must be invoked from a gnus summary buffer." this-command)))
2823   (unless (or (zerop (length group))
2824               (and gnus-newsgroup-name
2825                    (string-equal gnus-newsgroup-name group)))
2826     (gnus-summary-exit)
2827     (gnus-summary-read-group group show-all
2828                              gnus-dont-select-after-jump-to-other-group)))
2829
2830 (defun gnus-summary-read-group-1 (group show-all no-article
2831                                         kill-buffer no-display
2832                                         &optional select-articles)
2833   ;; Killed foreign groups can't be entered.
2834   (when (and (not (gnus-group-native-p group))
2835              (not (gnus-gethash group gnus-newsrc-hashtb)))
2836     (error "Dead non-native groups can't be entered"))
2837   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2838   (let* ((new-group (gnus-summary-setup-buffer group))
2839          (quit-config (gnus-group-quit-config group))
2840          (did-select (and new-group (gnus-select-newsgroup
2841                                      group show-all select-articles))))
2842     (cond
2843      ;; This summary buffer exists already, so we just select it.
2844      ((not new-group)
2845       (gnus-set-global-variables)
2846       (when kill-buffer
2847         (gnus-kill-or-deaden-summary kill-buffer))
2848       (gnus-configure-windows 'summary 'force)
2849       (gnus-set-mode-line 'summary)
2850       (gnus-summary-position-point)
2851       (message "")
2852       t)
2853      ;; We couldn't select this group.
2854      ((null did-select)
2855       (when (and (eq major-mode 'gnus-summary-mode)
2856                  (not (equal (current-buffer) kill-buffer)))
2857         (kill-buffer (current-buffer))
2858         (if (not quit-config)
2859             (progn
2860               ;; Update the info -- marks might need to be removed,
2861               ;; for instance.
2862               (gnus-summary-update-info)
2863               (set-buffer gnus-group-buffer)
2864               (gnus-group-jump-to-group group)
2865               (gnus-group-next-unread-group 1))
2866           (gnus-handle-ephemeral-exit quit-config)))
2867       (gnus-message 3 "Can't select group")
2868       nil)
2869      ;; The user did a `C-g' while prompting for number of articles,
2870      ;; so we exit this group.
2871      ((eq did-select 'quit)
2872       (and (eq major-mode 'gnus-summary-mode)
2873            (not (equal (current-buffer) kill-buffer))
2874            (kill-buffer (current-buffer)))
2875       (when kill-buffer
2876         (gnus-kill-or-deaden-summary kill-buffer))
2877       (if (not quit-config)
2878           (progn
2879             (set-buffer gnus-group-buffer)
2880             (gnus-group-jump-to-group group)
2881             (gnus-group-next-unread-group 1)
2882             (gnus-configure-windows 'group 'force))
2883         (gnus-handle-ephemeral-exit quit-config))
2884       ;; Finally signal the quit.
2885       (signal 'quit nil))
2886      ;; The group was successfully selected.
2887      (t
2888       (gnus-set-global-variables)
2889       ;; Save the active value in effect when the group was entered.
2890       (setq gnus-newsgroup-active
2891             (gnus-copy-sequence
2892              (gnus-active gnus-newsgroup-name)))
2893       ;; You can change the summary buffer in some way with this hook.
2894       (gnus-run-hooks 'gnus-select-group-hook)
2895       ;; Set any local variables in the group parameters.
2896       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2897       (gnus-update-format-specifications
2898        nil 'summary 'summary-mode 'summary-dummy)
2899       (gnus-update-summary-mark-positions)
2900       ;; Do score processing.
2901       (when gnus-use-scoring
2902         (gnus-possibly-score-headers))
2903       ;; Check whether to fill in the gaps in the threads.
2904       (when gnus-build-sparse-threads
2905         (gnus-build-sparse-threads))
2906       ;; Find the initial limit.
2907       (if gnus-show-threads
2908           (if show-all
2909               (let ((gnus-newsgroup-dormant nil))
2910                 (gnus-summary-initial-limit show-all))
2911             (gnus-summary-initial-limit show-all))
2912         ;; When untreaded, all articles are always shown.
2913         (setq gnus-newsgroup-limit
2914               (mapcar
2915                (lambda (header) (mail-header-number header))
2916                gnus-newsgroup-headers)))
2917       ;; Generate the summary buffer.
2918       (unless no-display
2919         (gnus-summary-prepare))
2920       (when gnus-use-trees
2921         (gnus-tree-open group)
2922         (setq gnus-summary-highlight-line-function
2923               'gnus-tree-highlight-article))
2924       ;; If the summary buffer is empty, but there are some low-scored
2925       ;; articles or some excluded dormants, we include these in the
2926       ;; buffer.
2927       (when (and (zerop (buffer-size))
2928                  (not no-display))
2929         (cond (gnus-newsgroup-dormant
2930                (gnus-summary-limit-include-dormant))
2931               ((and gnus-newsgroup-scored show-all)
2932                (gnus-summary-limit-include-expunged t))))
2933       ;; Function `gnus-apply-kill-file' must be called in this hook.
2934       (gnus-run-hooks 'gnus-apply-kill-hook)
2935       (if (and (zerop (buffer-size))
2936                (not no-display))
2937           (progn
2938             ;; This newsgroup is empty.
2939             (gnus-summary-catchup-and-exit nil t)
2940             (gnus-message 6 "No unread news")
2941             (when kill-buffer
2942               (gnus-kill-or-deaden-summary kill-buffer))
2943             ;; Return nil from this function.
2944             nil)
2945         ;; Hide conversation thread subtrees.  We cannot do this in
2946         ;; gnus-summary-prepare-hook since kill processing may not
2947         ;; work with hidden articles.
2948         (and gnus-show-threads
2949              gnus-thread-hide-subtree
2950              (gnus-summary-hide-all-threads))
2951         (when kill-buffer
2952           (gnus-kill-or-deaden-summary kill-buffer))
2953         ;; Show first unread article if requested.
2954         (if (and (not no-article)
2955                  (not no-display)
2956                  gnus-newsgroup-unreads
2957                  gnus-auto-select-first)
2958             (progn
2959               (gnus-configure-windows 'summary)
2960               (cond
2961                ((eq gnus-auto-select-first 'best)
2962                 (gnus-summary-best-unread-article))
2963                ((eq gnus-auto-select-first t)
2964                 (gnus-summary-first-unread-article))
2965                ((gnus-functionp gnus-auto-select-first)
2966                 (funcall gnus-auto-select-first))))
2967           ;; Don't select any articles, just move point to the first
2968           ;; article in the group.
2969           (goto-char (point-min))
2970           (gnus-summary-position-point)
2971           (gnus-configure-windows 'summary 'force)
2972           (gnus-set-mode-line 'summary))
2973         (when (get-buffer-window gnus-group-buffer t)
2974           ;; Gotta use windows, because recenter does weird stuff if
2975           ;; the current buffer ain't the displayed window.
2976           (let ((owin (selected-window)))
2977             (select-window (get-buffer-window gnus-group-buffer t))
2978             (when (gnus-group-goto-group group)
2979               (recenter))
2980             (select-window owin)))
2981         ;; Mark this buffer as "prepared".
2982         (setq gnus-newsgroup-prepared t)
2983         (gnus-run-hooks 'gnus-summary-prepared-hook)
2984         t)))))
2985
2986 (defun gnus-summary-prepare ()
2987   "Generate the summary buffer."
2988   (interactive)
2989   (let ((buffer-read-only nil))
2990     (erase-buffer)
2991     (setq gnus-newsgroup-data nil
2992           gnus-newsgroup-data-reverse nil)
2993     (gnus-run-hooks 'gnus-summary-generate-hook)
2994     ;; Generate the buffer, either with threads or without.
2995     (when gnus-newsgroup-headers
2996       (gnus-summary-prepare-threads
2997        (if gnus-show-threads
2998            (gnus-sort-gathered-threads
2999             (funcall gnus-summary-thread-gathering-function
3000                      (gnus-sort-threads
3001                       (gnus-cut-threads (gnus-make-threads)))))
3002          ;; Unthreaded display.
3003          (gnus-sort-articles gnus-newsgroup-headers))))
3004     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3005     ;; Call hooks for modifying summary buffer.
3006     (goto-char (point-min))
3007     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3008
3009 (defsubst gnus-general-simplify-subject (subject)
3010   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3011   (setq subject
3012         (cond
3013          ;; Truncate the subject.
3014          (gnus-simplify-subject-functions
3015           (gnus-map-function gnus-simplify-subject-functions subject))
3016          ((numberp gnus-summary-gather-subject-limit)
3017           (setq subject (gnus-simplify-subject-re subject))
3018           (if (> (length subject) gnus-summary-gather-subject-limit)
3019               (substring subject 0 gnus-summary-gather-subject-limit)
3020             subject))
3021          ;; Fuzzily simplify it.
3022          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3023           (gnus-simplify-subject-fuzzy subject))
3024          ;; Just remove the leading "Re:".
3025          (t
3026           (gnus-simplify-subject-re subject))))
3027
3028   (if (and gnus-summary-gather-exclude-subject
3029            (string-match gnus-summary-gather-exclude-subject subject))
3030       nil                               ; This article shouldn't be gathered
3031     subject))
3032
3033 (defun gnus-summary-simplify-subject-query ()
3034   "Query where the respool algorithm would put this article."
3035   (interactive)
3036   (gnus-summary-select-article)
3037   (message "%s"
3038            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3039
3040 (defun gnus-gather-threads-by-subject (threads)
3041   "Gather threads by looking at Subject headers."
3042   (if (not gnus-summary-make-false-root)
3043       threads
3044     (let ((hashtb (gnus-make-hashtable 1024))
3045           (prev threads)
3046           (result threads)
3047           subject hthread whole-subject)
3048       (while threads
3049         (setq subject (gnus-general-simplify-subject
3050                        (setq whole-subject (mail-header-subject
3051                                             (caar threads)))))
3052         (when subject
3053           (if (setq hthread (gnus-gethash subject hashtb))
3054               (progn
3055                 ;; We enter a dummy root into the thread, if we
3056                 ;; haven't done that already.
3057                 (unless (stringp (caar hthread))
3058                   (setcar hthread (list whole-subject (car hthread))))
3059                 ;; We add this new gathered thread to this gathered
3060                 ;; thread.
3061                 (setcdr (car hthread)
3062                         (nconc (cdar hthread) (list (car threads))))
3063                 ;; Remove it from the list of threads.
3064                 (setcdr prev (cdr threads))
3065                 (setq threads prev))
3066             ;; Enter this thread into the hash table.
3067             (gnus-sethash subject threads hashtb)))
3068         (setq prev threads)
3069         (setq threads (cdr threads)))
3070       result)))
3071
3072 (defun gnus-gather-threads-by-references (threads)
3073   "Gather threads by looking at References headers."
3074   (let ((idhashtb (gnus-make-hashtable 1024))
3075         (thhashtb (gnus-make-hashtable 1024))
3076         (prev threads)
3077         (result threads)
3078         ids references id gthread gid entered ref)
3079     (while threads
3080       (when (setq references (mail-header-references (caar threads)))
3081         (setq id (mail-header-id (caar threads))
3082               ids (gnus-split-references references)
3083               entered nil)
3084         (while (setq ref (pop ids))
3085           (setq ids (delete ref ids))
3086           (if (not (setq gid (gnus-gethash ref idhashtb)))
3087               (progn
3088                 (gnus-sethash ref id idhashtb)
3089                 (gnus-sethash id threads thhashtb))
3090             (setq gthread (gnus-gethash gid thhashtb))
3091             (unless entered
3092               ;; We enter a dummy root into the thread, if we
3093               ;; haven't done that already.
3094               (unless (stringp (caar gthread))
3095                 (setcar gthread (list (mail-header-subject (caar gthread))
3096                                       (car gthread))))
3097               ;; We add this new gathered thread to this gathered
3098               ;; thread.
3099               (setcdr (car gthread)
3100                       (nconc (cdar gthread) (list (car threads)))))
3101             ;; Add it into the thread hash table.
3102             (gnus-sethash id gthread thhashtb)
3103             (setq entered t)
3104             ;; Remove it from the list of threads.
3105             (setcdr prev (cdr threads))
3106             (setq threads prev))))
3107       (setq prev threads)
3108       (setq threads (cdr threads)))
3109     result))
3110
3111 (defun gnus-sort-gathered-threads (threads)
3112   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3113   (let ((result threads))
3114     (while threads
3115       (when (stringp (caar threads))
3116         (setcdr (car threads)
3117                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3118       (setq threads (cdr threads)))
3119     result))
3120
3121 (defun gnus-thread-loop-p (root thread)
3122   "Say whether ROOT is in THREAD."
3123   (let ((stack (list thread))
3124         (infloop 0)
3125         th)
3126     (while (setq thread (pop stack))
3127       (setq th (cdr thread))
3128       (while (and th
3129                   (not (eq (caar th) root)))
3130         (pop th))
3131       (if th
3132           ;; We have found a loop.
3133           (let (ref-dep)
3134             (setcdr thread (delq (car th) (cdr thread)))
3135             (if (boundp (setq ref-dep (intern "none"
3136                                               gnus-newsgroup-dependencies)))
3137                 (setcdr (symbol-value ref-dep)
3138                         (nconc (cdr (symbol-value ref-dep))
3139                                (list (car th))))
3140               (set ref-dep (list nil (car th))))
3141             (setq infloop 1
3142                   stack nil))
3143         ;; Push all the subthreads onto the stack.
3144         (push (cdr thread) stack)))
3145     infloop))
3146
3147 (defun gnus-make-threads ()
3148   "Go through the dependency hashtb and find the roots.  Return all threads."
3149   (let (threads)
3150     (while (catch 'infloop
3151              (mapatoms
3152               (lambda (refs)
3153                 ;; Deal with self-referencing References loops.
3154                 (when (and (car (symbol-value refs))
3155                            (not (zerop
3156                                  (apply
3157                                   '+
3158                                   (mapcar
3159                                    (lambda (thread)
3160                                      (gnus-thread-loop-p
3161                                       (car (symbol-value refs)) thread))
3162                                    (cdr (symbol-value refs)))))))
3163                   (setq threads nil)
3164                   (throw 'infloop t))
3165                 (unless (car (symbol-value refs))
3166                   ;; These threads do not refer back to any other articles,
3167                   ;; so they're roots.
3168                   (setq threads (append (cdr (symbol-value refs)) threads))))
3169               gnus-newsgroup-dependencies)))
3170     threads))
3171
3172 ;; Build the thread tree.
3173 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3174   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3175
3176 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3177 if it was already present.
3178
3179 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3180 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3181 Message-IDs will be renamed be renamed to a unique Message-ID before
3182 being entered.
3183
3184 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3185   (let* ((id (mail-header-id header))
3186          (id-dep (and id (intern id dependencies)))
3187          ref ref-dep ref-header)
3188     ;; Enter this `header' in the `dependencies' table.
3189     (cond
3190      ((not id-dep)
3191       (setq header nil))
3192      ;; The first two cases do the normal part: enter a new `header'
3193      ;; in the `dependencies' table.
3194      ((not (boundp id-dep))
3195       (set id-dep (list header)))
3196      ((null (car (symbol-value id-dep)))
3197       (setcar (symbol-value id-dep) header))
3198
3199      ;; From here the `header' was already present in the
3200      ;; `dependencies' table.
3201      (force-new
3202       ;; Overrides an existing entry;
3203       ;; just set the header part of the entry.
3204       (setcar (symbol-value id-dep) header))
3205
3206      ;; Renames the existing `header' to a unique Message-ID.
3207      ((not gnus-summary-ignore-duplicates)
3208       ;; An article with this Message-ID has already been seen.
3209       ;; We rename the Message-ID.
3210       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3211            (list header))
3212       (mail-header-set-id header id))
3213
3214      ;; The last case ignores an existing entry, except it adds any
3215      ;; additional Xrefs (in case the two articles came from different
3216      ;; servers.
3217      ;; Also sets `header' to `nil' meaning that the `dependencies'
3218      ;; table was *not* modified.
3219      (t
3220       (mail-header-set-xref
3221        (car (symbol-value id-dep))
3222        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3223                    "")
3224                (or (mail-header-xref header) "")))
3225       (setq header nil)))
3226
3227     (when header
3228       ;; First check if that we are not creating a References loop.
3229       (setq ref (gnus-parent-id (mail-header-references header)))
3230       (while (and ref
3231                   (setq ref-dep (intern-soft ref dependencies))
3232                   (boundp ref-dep)
3233                   (setq ref-header (car (symbol-value ref-dep))))
3234         (if (string= id ref)
3235             ;; Yuk!  This is a reference loop.  Make the article be a
3236             ;; root article.
3237             (progn
3238               (mail-header-set-references (car (symbol-value id-dep)) "none")
3239               (setq ref nil))
3240           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3241       (setq ref (gnus-parent-id (mail-header-references header)))
3242       (setq ref-dep (intern (or ref "none") dependencies))
3243       (if (boundp ref-dep)
3244           (setcdr (symbol-value ref-dep)
3245                   (nconc (cdr (symbol-value ref-dep))
3246                          (list (symbol-value id-dep))))
3247         (set ref-dep (list nil (symbol-value id-dep)))))
3248     header))
3249
3250 (defun gnus-build-sparse-threads ()
3251   (let ((headers gnus-newsgroup-headers)
3252         (mail-parse-charset gnus-newsgroup-charset)
3253         (gnus-summary-ignore-duplicates t)
3254         header references generation relations
3255         subject child end new-child date)
3256     ;; First we create an alist of generations/relations, where
3257     ;; generations is how much we trust the relation, and the relation
3258     ;; is parent/child.
3259     (gnus-message 7 "Making sparse threads...")
3260     (save-excursion
3261       (nnheader-set-temp-buffer " *gnus sparse threads*")
3262       (while (setq header (pop headers))
3263         (when (and (setq references (mail-header-references header))
3264                    (not (string= references "")))
3265           (insert references)
3266           (setq child (mail-header-id header)
3267                 subject (mail-header-subject header)
3268                 date (mail-header-date header)
3269                 generation 0)
3270           (while (search-backward ">" nil t)
3271             (setq end (1+ (point)))
3272             (when (search-backward "<" nil t)
3273               (setq new-child (buffer-substring (point) end))
3274               (push (list (incf generation)
3275                           child (setq child new-child)
3276                           subject date)
3277                     relations)))
3278           (when child
3279             (push (list (1+ generation) child nil subject) relations))
3280           (erase-buffer)))
3281       (kill-buffer (current-buffer)))
3282     ;; Sort over trustworthiness.
3283     (mapcar
3284      (lambda (relation)
3285        (when (gnus-dependencies-add-header
3286               (make-full-mail-header-from-decoded-header
3287                gnus-reffed-article-number
3288                (nth 3 relation) "" (or (nth 4 relation) "")
3289                (nth 1 relation)
3290                (or (nth 2 relation) "") 0 0 "")
3291               gnus-newsgroup-dependencies nil)
3292          (push gnus-reffed-article-number gnus-newsgroup-limit)
3293          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3294          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3295                gnus-newsgroup-reads)
3296          (decf gnus-reffed-article-number)))
3297      (sort relations 'car-less-than-car))
3298     (gnus-message 7 "Making sparse threads...done")))
3299
3300 (defun gnus-build-old-threads ()
3301   ;; Look at all the articles that refer back to old articles, and
3302   ;; fetch the headers for the articles that aren't there.  This will
3303   ;; build complete threads - if the roots haven't been expired by the
3304   ;; server, that is.
3305   (let ((mail-parse-charset gnus-newsgroup-charset)
3306         id heads)
3307     (mapatoms
3308      (lambda (refs)
3309        (when (not (car (symbol-value refs)))
3310          (setq heads (cdr (symbol-value refs)))
3311          (while heads
3312            (if (memq (mail-header-number (caar heads))
3313                      gnus-newsgroup-dormant)
3314                (setq heads (cdr heads))
3315              (setq id (symbol-name refs))
3316              (while (and (setq id (gnus-build-get-header id))
3317                          (not (car (gnus-id-to-thread id)))))
3318              (setq heads nil)))))
3319      gnus-newsgroup-dependencies)))
3320
3321 ;; This function has to be called with point after the article number
3322 ;; on the beginning of the line.
3323 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3324   (let ((eol (gnus-point-at-eol))
3325         (buffer (current-buffer))
3326         header)
3327
3328     ;; overview: [num subject from date id refs chars lines misc]
3329     (unwind-protect
3330         (progn
3331           (narrow-to-region (point) eol)
3332           (unless (eobp)
3333             (forward-char))
3334
3335           (setq header
3336                 (make-full-mail-header
3337                  number                         ; number
3338                  (nnheader-nov-field)           ; subject
3339                  (nnheader-nov-field)           ; from
3340                  (nnheader-nov-field)           ; date
3341                  (nnheader-nov-read-message-id) ; id
3342                  (nnheader-nov-field)           ; refs
3343                  (nnheader-nov-read-integer)    ; chars
3344                  (nnheader-nov-read-integer)    ; lines
3345                  (unless (eobp)
3346                    (if (looking-at "Xref: ")
3347                        (goto-char (match-end 0)))
3348                    (nnheader-nov-field))        ; Xref
3349                  (nnheader-nov-parse-extra))))  ; extra
3350
3351       (widen))
3352
3353     (when gnus-alter-header-function
3354       (funcall gnus-alter-header-function header))
3355     (gnus-dependencies-add-header header dependencies force-new)))
3356
3357 (defun gnus-build-get-header (id)
3358   "Look through the buffer of NOV lines and find the header to ID.
3359 Enter this line into the dependencies hash table, and return
3360 the id of the parent article (if any)."
3361   (let ((deps gnus-newsgroup-dependencies)
3362         found header)
3363     (prog1
3364         (save-excursion
3365           (set-buffer nntp-server-buffer)
3366           (let ((case-fold-search nil))
3367             (goto-char (point-min))
3368             (while (and (not found)
3369                         (search-forward id nil t))
3370               (beginning-of-line)
3371               (setq found (looking-at
3372                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3373                                    (regexp-quote id))))
3374               (or found (beginning-of-line 2)))
3375             (when found
3376               (beginning-of-line)
3377               (and
3378                (setq header (gnus-nov-parse-line
3379                              (read (current-buffer)) deps))
3380                (gnus-parent-id (mail-header-references header))))))
3381       (when header
3382         (let ((number (mail-header-number header)))
3383           (push number gnus-newsgroup-limit)
3384           (push header gnus-newsgroup-headers)
3385           (if (memq number gnus-newsgroup-unselected)
3386               (progn
3387                 (push number gnus-newsgroup-unreads)
3388                 (setq gnus-newsgroup-unselected
3389                       (delq number gnus-newsgroup-unselected)))
3390             (push number gnus-newsgroup-ancient)))))))
3391
3392 (defun gnus-build-all-threads ()
3393   "Read all the headers."
3394   (let ((gnus-summary-ignore-duplicates t)
3395         (mail-parse-charset gnus-newsgroup-charset)
3396         (dependencies gnus-newsgroup-dependencies)
3397         header article)
3398     (save-excursion
3399       (set-buffer nntp-server-buffer)
3400       (let ((case-fold-search nil))
3401         (goto-char (point-min))
3402         (while (not (eobp))
3403           (ignore-errors
3404             (setq article (read (current-buffer))
3405                   header (gnus-nov-parse-line article dependencies)))
3406           (when header
3407             (save-excursion
3408               (set-buffer gnus-summary-buffer)
3409               (push header gnus-newsgroup-headers)
3410               (if (memq (setq article (mail-header-number header))
3411                         gnus-newsgroup-unselected)
3412                   (progn
3413                     (push article gnus-newsgroup-unreads)
3414                     (setq gnus-newsgroup-unselected
3415                           (delq article gnus-newsgroup-unselected)))
3416                 (push article gnus-newsgroup-ancient)))
3417             (forward-line 1)))))))
3418
3419 (defun gnus-summary-update-article-line (article header)
3420   "Update the line for ARTICLE using HEADERS."
3421   (let* ((id (mail-header-id header))
3422          (thread (gnus-id-to-thread id)))
3423     (unless thread
3424       (error "Article in no thread"))
3425     ;; Update the thread.
3426     (setcar thread header)
3427     (gnus-summary-goto-subject article)
3428     (let* ((datal (gnus-data-find-list article))
3429            (data (car datal))
3430            (length (when (cdr datal)
3431                      (- (gnus-data-pos data)
3432                         (gnus-data-pos (cadr datal)))))
3433            (buffer-read-only nil)
3434            (level (gnus-summary-thread-level)))
3435       (gnus-delete-line)
3436       (gnus-summary-insert-line
3437        header level nil (gnus-article-mark article)
3438        (memq article gnus-newsgroup-replied)
3439        (memq article gnus-newsgroup-expirable)
3440        ;; Only insert the Subject string when it's different
3441        ;; from the previous Subject string.
3442        (if (and
3443             gnus-show-threads
3444             (gnus-subject-equal
3445              (condition-case ()
3446                  (mail-header-subject
3447                   (gnus-data-header
3448                    (cadr
3449                     (gnus-data-find-list
3450                      article
3451                      (gnus-data-list t)))))
3452                ;; Error on the side of excessive subjects.
3453                (error ""))
3454              (mail-header-subject header)))
3455            ""
3456          (mail-header-subject header))
3457        nil (cdr (assq article gnus-newsgroup-scored))
3458        (memq article gnus-newsgroup-processable))
3459       (when length
3460         (gnus-data-update-list
3461          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3462
3463 (defun gnus-summary-update-article (article &optional iheader)
3464   "Update ARTICLE in the summary buffer."
3465   (set-buffer gnus-summary-buffer)
3466   (let* ((header (gnus-summary-article-header article))
3467          (id (mail-header-id header))
3468          (data (gnus-data-find article))
3469          (thread (gnus-id-to-thread id))
3470          (references (mail-header-references header))
3471          (parent
3472           (gnus-id-to-thread
3473            (or (gnus-parent-id
3474                 (when (and references
3475                            (not (equal "" references)))
3476                   references))
3477                "none")))
3478          (buffer-read-only nil)
3479          (old (car thread)))
3480     (when thread
3481       (unless iheader
3482         (setcar thread nil)
3483         (when parent
3484           (delq thread parent)))
3485       (if (gnus-summary-insert-subject id header)
3486           ;; Set the (possibly) new article number in the data structure.
3487           (gnus-data-set-number data (gnus-id-to-article id))
3488         (setcar thread old)
3489         nil))))
3490
3491 (defun gnus-rebuild-thread (id &optional line)
3492   "Rebuild the thread containing ID.
3493 If LINE, insert the rebuilt thread starting on line LINE."
3494   (let ((buffer-read-only nil)
3495         old-pos current thread data)
3496     (if (not gnus-show-threads)
3497         (setq thread (list (car (gnus-id-to-thread id))))
3498       ;; Get the thread this article is part of.
3499       (setq thread (gnus-remove-thread id)))
3500     (setq old-pos (gnus-point-at-bol))
3501     (setq current (save-excursion
3502                     (and (zerop (forward-line -1))
3503                          (gnus-summary-article-number))))
3504     ;; If this is a gathered thread, we have to go some re-gathering.
3505     (when (stringp (car thread))
3506       (let ((subject (car thread))
3507             roots thr)
3508         (setq thread (cdr thread))
3509         (while thread
3510           (unless (memq (setq thr (gnus-id-to-thread
3511                                    (gnus-root-id
3512                                     (mail-header-id (caar thread)))))
3513                         roots)
3514             (push thr roots))
3515           (setq thread (cdr thread)))
3516         ;; We now have all (unique) roots.
3517         (if (= (length roots) 1)
3518             ;; All the loose roots are now one solid root.
3519             (setq thread (car roots))
3520           (setq thread (cons subject (gnus-sort-threads roots))))))
3521     (let (threads)
3522       ;; We then insert this thread into the summary buffer.
3523       (when line
3524         (goto-char (point-min))
3525         (forward-line (1- line)))
3526       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3527         (if gnus-show-threads
3528             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3529           (gnus-summary-prepare-unthreaded thread))
3530         (setq data (nreverse gnus-newsgroup-data))
3531         (setq threads gnus-newsgroup-threads))
3532       ;; We splice the new data into the data structure.
3533       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3534       ;;!!! then we want to insert at the beginning of the buffer.
3535       ;;!!! That happens to be true with Gnus now, but that may
3536       ;;!!! change in the future.  Perhaps.
3537       (gnus-data-enter-list
3538        (if line nil current) data (- (point) old-pos))
3539       (setq gnus-newsgroup-threads
3540             (nconc threads gnus-newsgroup-threads))
3541       (gnus-data-compute-positions))))
3542
3543 (defun gnus-number-to-header (number)
3544   "Return the header for article NUMBER."
3545   (let ((headers gnus-newsgroup-headers))
3546     (while (and headers
3547                 (not (= number (mail-header-number (car headers)))))
3548       (pop headers))
3549     (when headers
3550       (car headers))))
3551
3552 (defun gnus-parent-headers (in-headers &optional generation)
3553   "Return the headers of the GENERATIONeth parent of HEADERS."
3554   (unless generation
3555     (setq generation 1))
3556   (let ((parent t)
3557         (headers in-headers)
3558         references)
3559     (while (and parent
3560                 (not (zerop generation))
3561                 (setq references (mail-header-references headers)))
3562       (setq headers (if (and references
3563                              (setq parent (gnus-parent-id references)))
3564                         (car (gnus-id-to-thread parent))
3565                       nil))
3566       (decf generation))
3567     (and (not (eq headers in-headers))
3568          headers)))
3569
3570 (defun gnus-id-to-thread (id)
3571   "Return the (sub-)thread where ID appears."
3572   (gnus-gethash id gnus-newsgroup-dependencies))
3573
3574 (defun gnus-id-to-article (id)
3575   "Return the article number of ID."
3576   (let ((thread (gnus-id-to-thread id)))
3577     (when (and thread
3578                (car thread))
3579       (mail-header-number (car thread)))))
3580
3581 (defun gnus-id-to-header (id)
3582   "Return the article headers of ID."
3583   (car (gnus-id-to-thread id)))
3584
3585 (defun gnus-article-displayed-root-p (article)
3586   "Say whether ARTICLE is a root(ish) article."
3587   (let ((level (gnus-summary-thread-level article))
3588         (refs (mail-header-references  (gnus-summary-article-header article)))
3589         particle)
3590     (cond
3591      ((null level) nil)
3592      ((zerop level) t)
3593      ((null refs) t)
3594      ((null (gnus-parent-id refs)) t)
3595      ((and (= 1 level)
3596            (null (setq particle (gnus-id-to-article
3597                                  (gnus-parent-id refs))))
3598            (null (gnus-summary-thread-level particle)))))))
3599
3600 (defun gnus-root-id (id)
3601   "Return the id of the root of the thread where ID appears."
3602   (let (last-id prev)
3603     (while (and id (setq prev (car (gnus-id-to-thread id))))
3604       (setq last-id id
3605             id (gnus-parent-id (mail-header-references prev))))
3606     last-id))
3607
3608 (defun gnus-articles-in-thread (thread)
3609   "Return the list of articles in THREAD."
3610   (cons (mail-header-number (car thread))
3611         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3612
3613 (defun gnus-remove-thread (id &optional dont-remove)
3614   "Remove the thread that has ID in it."
3615   (let (headers thread last-id)
3616     ;; First go up in this thread until we find the root.
3617     (setq last-id (gnus-root-id id)
3618           headers (message-flatten-list (gnus-id-to-thread last-id)))
3619     ;; We have now found the real root of this thread.  It might have
3620     ;; been gathered into some loose thread, so we have to search
3621     ;; through the threads to find the thread we wanted.
3622     (let ((threads gnus-newsgroup-threads)
3623           sub)
3624       (while threads
3625         (setq sub (car threads))
3626         (if (stringp (car sub))
3627             ;; This is a gathered thread, so we look at the roots
3628             ;; below it to find whether this article is in this
3629             ;; gathered root.
3630             (progn
3631               (setq sub (cdr sub))
3632               (while sub
3633                 (when (member (caar sub) headers)
3634                   (setq thread (car threads)
3635                         threads nil
3636                         sub nil))
3637                 (setq sub (cdr sub))))
3638           ;; It's an ordinary thread, so we check it.
3639           (when (eq (car sub) (car headers))
3640             (setq thread sub
3641                   threads nil)))
3642         (setq threads (cdr threads)))
3643       ;; If this article is in no thread, then it's a root.
3644       (if thread
3645           (unless dont-remove
3646             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3647         (setq thread (gnus-id-to-thread last-id)))
3648       (when thread
3649         (prog1
3650             thread                      ; We return this thread.
3651           (unless dont-remove
3652             (if (stringp (car thread))
3653                 (progn
3654                   ;; If we use dummy roots, then we have to remove the
3655                   ;; dummy root as well.
3656                   (when (eq gnus-summary-make-false-root 'dummy)
3657                     ;; We go to the dummy root by going to
3658                     ;; the first sub-"thread", and then one line up.
3659                     (gnus-summary-goto-article
3660                      (mail-header-number (caadr thread)))
3661                     (forward-line -1)
3662                     (gnus-delete-line)
3663                     (gnus-data-compute-positions))
3664                   (setq thread (cdr thread))
3665                   (while thread
3666                     (gnus-remove-thread-1 (car thread))
3667                     (setq thread (cdr thread))))
3668               (gnus-remove-thread-1 thread))))))))
3669
3670 (defun gnus-remove-thread-1 (thread)
3671   "Remove the thread THREAD recursively."
3672   (let ((number (mail-header-number (pop thread)))
3673         d)
3674     (setq thread (reverse thread))
3675     (while thread
3676       (gnus-remove-thread-1 (pop thread)))
3677     (when (setq d (gnus-data-find number))
3678       (goto-char (gnus-data-pos d))
3679       (gnus-summary-show-thread)
3680       (gnus-data-remove
3681        number
3682        (- (gnus-point-at-bol)
3683           (prog1
3684               (1+ (gnus-point-at-eol))
3685             (gnus-delete-line)))))))
3686
3687 (defun gnus-sort-threads-1 (threads func)
3688   (sort (mapcar (lambda (thread)
3689                   (cons (car thread)
3690                         (and (cdr thread)
3691                              (gnus-sort-threads-1 (cdr thread) func))))
3692                 threads) func))
3693
3694 (defun gnus-sort-threads (threads)
3695   "Sort THREADS."
3696   (if (not gnus-thread-sort-functions)
3697       threads
3698     (gnus-message 8 "Sorting threads...")
3699     (prog1
3700         (gnus-sort-threads-1 
3701          threads 
3702          (gnus-make-sort-function gnus-thread-sort-functions))
3703       (gnus-message 8 "Sorting threads...done"))))
3704
3705 (defun gnus-sort-articles (articles)
3706   "Sort ARTICLES."
3707   (when gnus-article-sort-functions
3708     (gnus-message 7 "Sorting articles...")
3709     (prog1
3710         (setq gnus-newsgroup-headers
3711               (sort articles (gnus-make-sort-function
3712                               gnus-article-sort-functions)))
3713       (gnus-message 7 "Sorting articles...done"))))
3714
3715 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3716 (defmacro gnus-thread-header (thread)
3717   "Return header of first article in THREAD.
3718 Note that THREAD must never, ever be anything else than a variable -
3719 using some other form will lead to serious barfage."
3720   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3721   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3722   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3723         (vector thread) 2))
3724
3725 (defsubst gnus-article-sort-by-number (h1 h2)
3726   "Sort articles by article number."
3727   (< (mail-header-number h1)
3728      (mail-header-number h2)))
3729
3730 (defun gnus-thread-sort-by-number (h1 h2)
3731   "Sort threads by root article number."
3732   (gnus-article-sort-by-number
3733    (gnus-thread-header h1) (gnus-thread-header h2)))
3734
3735 (defsubst gnus-article-sort-by-lines (h1 h2)
3736   "Sort articles by article Lines header."
3737   (< (mail-header-lines h1)
3738      (mail-header-lines h2)))
3739
3740 (defun gnus-thread-sort-by-lines (h1 h2)
3741   "Sort threads by root article Lines header."
3742   (gnus-article-sort-by-lines
3743    (gnus-thread-header h1) (gnus-thread-header h2)))
3744
3745 (defsubst gnus-article-sort-by-chars (h1 h2)
3746   "Sort articles by octet length."
3747   (< (mail-header-chars h1)
3748      (mail-header-chars h2)))
3749
3750 (defun gnus-thread-sort-by-chars (h1 h2)
3751   "Sort threads by root article octet length."
3752   (gnus-article-sort-by-chars
3753    (gnus-thread-header h1) (gnus-thread-header h2)))
3754
3755 (defsubst gnus-article-sort-by-author (h1 h2)
3756   "Sort articles by root author."
3757   (string-lessp
3758    (let ((addr (car (mime-entity-read-field h1 'From))))
3759      (or (std11-full-name-string addr)
3760          (std11-address-string addr)
3761          ""))
3762    (let ((addr (car (mime-entity-read-field h2 'From))))
3763      (or (std11-full-name-string addr)
3764          (std11-address-string addr)
3765          ""))
3766    ))
3767
3768 (defun gnus-thread-sort-by-author (h1 h2)
3769   "Sort threads by root author."
3770   (gnus-article-sort-by-author
3771    (gnus-thread-header h1)  (gnus-thread-header h2)))
3772
3773 (defsubst gnus-article-sort-by-subject (h1 h2)
3774   "Sort articles by root subject."
3775   (string-lessp
3776    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3777    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3778
3779 (defun gnus-thread-sort-by-subject (h1 h2)
3780   "Sort threads by root subject."
3781   (gnus-article-sort-by-subject
3782    (gnus-thread-header h1) (gnus-thread-header h2)))
3783
3784 (defsubst gnus-article-sort-by-date (h1 h2)
3785   "Sort articles by root article date."
3786   (time-less-p
3787    (gnus-date-get-time (mail-header-date h1))
3788    (gnus-date-get-time (mail-header-date h2))))
3789
3790 (defun gnus-thread-sort-by-date (h1 h2)
3791   "Sort threads by root article date."
3792   (gnus-article-sort-by-date
3793    (gnus-thread-header h1) (gnus-thread-header h2)))
3794
3795 (defsubst gnus-article-sort-by-score (h1 h2)
3796   "Sort articles by root article score.
3797 Unscored articles will be counted as having a score of zero."
3798   (> (or (cdr (assq (mail-header-number h1)
3799                     gnus-newsgroup-scored))
3800          gnus-summary-default-score 0)
3801      (or (cdr (assq (mail-header-number h2)
3802                     gnus-newsgroup-scored))
3803          gnus-summary-default-score 0)))
3804
3805 (defun gnus-thread-sort-by-score (h1 h2)
3806   "Sort threads by root article score."
3807   (gnus-article-sort-by-score
3808    (gnus-thread-header h1) (gnus-thread-header h2)))
3809
3810 (defun gnus-thread-sort-by-total-score (h1 h2)
3811   "Sort threads by the sum of all scores in the thread.
3812 Unscored articles will be counted as having a score of zero."
3813   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3814
3815 (defun gnus-thread-total-score (thread)
3816   ;; This function find the total score of THREAD.
3817   (cond ((null thread)
3818          0)
3819         ((consp thread)
3820          (if (stringp (car thread))
3821              (apply gnus-thread-score-function 0
3822                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3823            (gnus-thread-total-score-1 thread)))
3824         (t
3825          (gnus-thread-total-score-1 (list thread)))))
3826
3827 (defun gnus-thread-total-score-1 (root)
3828   ;; This function find the total score of the thread below ROOT.
3829   (setq root (car root))
3830   (apply gnus-thread-score-function
3831          (or (append
3832               (mapcar 'gnus-thread-total-score
3833                       (cdr (gnus-id-to-thread (mail-header-id root))))
3834               (when (> (mail-header-number root) 0)
3835                 (list (or (cdr (assq (mail-header-number root)
3836                                      gnus-newsgroup-scored))
3837                           gnus-summary-default-score 0))))
3838              (list gnus-summary-default-score)
3839              '(0))))
3840
3841 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3842 (defvar gnus-tmp-prev-subject nil)
3843 (defvar gnus-tmp-false-parent nil)
3844 (defvar gnus-tmp-root-expunged nil)
3845 (defvar gnus-tmp-dummy-line nil)
3846
3847 (defvar gnus-tmp-header)
3848 (defun gnus-extra-header (type &optional header)
3849   "Return the extra header of TYPE."
3850   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3851       ""))
3852
3853 (defun gnus-summary-prepare-threads (threads)
3854   "Prepare summary buffer from THREADS and indentation LEVEL.
3855 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3856 or a straight list of headers."
3857   (gnus-message 7 "Generating summary...")
3858
3859   (setq gnus-newsgroup-threads threads)
3860   (beginning-of-line)
3861
3862   (let ((gnus-tmp-level 0)
3863         (default-score (or gnus-summary-default-score 0))
3864         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3865         thread number subject stack state gnus-tmp-gathered beg-match
3866         new-roots gnus-tmp-new-adopts thread-end
3867         gnus-tmp-header gnus-tmp-unread
3868         gnus-tmp-replied gnus-tmp-subject-or-nil
3869         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3870         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3871         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3872
3873     (setq gnus-tmp-prev-subject nil)
3874
3875     (if (vectorp (car threads))
3876         ;; If this is a straight (sic) list of headers, then a
3877         ;; threaded summary display isn't required, so we just create
3878         ;; an unthreaded one.
3879         (gnus-summary-prepare-unthreaded threads)
3880
3881       ;; Do the threaded display.
3882
3883       (while (or threads stack gnus-tmp-new-adopts new-roots)
3884
3885         (if (and (= gnus-tmp-level 0)
3886                  (or (not stack)
3887                      (= (caar stack) 0))
3888                  (not gnus-tmp-false-parent)
3889                  (or gnus-tmp-new-adopts new-roots))
3890             (if gnus-tmp-new-adopts
3891                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3892                       thread (list (car gnus-tmp-new-adopts))
3893                       gnus-tmp-header (caar thread)
3894                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3895               (when new-roots
3896                 (setq thread (list (car new-roots))
3897                       gnus-tmp-header (caar thread)
3898                       new-roots (cdr new-roots))))
3899
3900           (if threads
3901               ;; If there are some threads, we do them before the
3902               ;; threads on the stack.
3903               (setq thread threads
3904                     gnus-tmp-header (caar thread))
3905             ;; There were no current threads, so we pop something off
3906             ;; the stack.
3907             (setq state (car stack)
3908                   gnus-tmp-level (car state)
3909                   thread (cdr state)
3910                   stack (cdr stack)
3911                   gnus-tmp-header (caar thread))))
3912
3913         (setq gnus-tmp-false-parent nil)
3914         (setq gnus-tmp-root-expunged nil)
3915         (setq thread-end nil)
3916
3917         (if (stringp gnus-tmp-header)
3918             ;; The header is a dummy root.
3919             (cond
3920              ((eq gnus-summary-make-false-root 'adopt)
3921               ;; We let the first article adopt the rest.
3922               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3923                                                (cddar thread)))
3924               (setq gnus-tmp-gathered
3925                     (nconc (mapcar
3926                             (lambda (h) (mail-header-number (car h)))
3927                             (cddar thread))
3928                            gnus-tmp-gathered))
3929               (setq thread (cons (list (caar thread)
3930                                        (cadar thread))
3931                                  (cdr thread)))
3932               (setq gnus-tmp-level -1
3933                     gnus-tmp-false-parent t))
3934              ((eq gnus-summary-make-false-root 'empty)
3935               ;; We print adopted articles with empty subject fields.
3936               (setq gnus-tmp-gathered
3937                     (nconc (mapcar
3938                             (lambda (h) (mail-header-number (car h)))
3939                             (cddar thread))
3940                            gnus-tmp-gathered))
3941               (setq gnus-tmp-level -1))
3942              ((eq gnus-summary-make-false-root 'dummy)
3943               ;; We remember that we probably want to output a dummy
3944               ;; root.
3945               (setq gnus-tmp-dummy-line gnus-tmp-header)
3946               (setq gnus-tmp-prev-subject gnus-tmp-header))
3947              (t
3948               ;; We do not make a root for the gathered
3949               ;; sub-threads at all.
3950               (setq gnus-tmp-level -1)))
3951
3952           (setq number (mail-header-number gnus-tmp-header)
3953                 subject (mail-header-subject gnus-tmp-header))
3954
3955           (cond
3956            ;; If the thread has changed subject, we might want to make
3957            ;; this subthread into a root.
3958            ((and (null gnus-thread-ignore-subject)
3959                  (not (zerop gnus-tmp-level))
3960                  gnus-tmp-prev-subject
3961                  (not (inline
3962                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3963             (setq new-roots (nconc new-roots (list (car thread)))
3964                   thread-end t
3965                   gnus-tmp-header nil))
3966            ;; If the article lies outside the current limit,
3967            ;; then we do not display it.
3968            ((not (memq number gnus-newsgroup-limit))
3969             (setq gnus-tmp-gathered
3970                   (nconc (mapcar
3971                           (lambda (h) (mail-header-number (car h)))
3972                           (cdar thread))
3973                          gnus-tmp-gathered))
3974             (setq gnus-tmp-new-adopts (if (cdar thread)
3975                                           (append gnus-tmp-new-adopts
3976                                                   (cdar thread))
3977                                         gnus-tmp-new-adopts)
3978                   thread-end t
3979                   gnus-tmp-header nil)
3980             (when (zerop gnus-tmp-level)
3981               (setq gnus-tmp-root-expunged t)))
3982            ;; Perhaps this article is to be marked as read?
3983            ((and gnus-summary-mark-below
3984                  (< (or (cdr (assq number gnus-newsgroup-scored))
3985                         default-score)
3986                     gnus-summary-mark-below)
3987                  ;; Don't touch sparse articles.
3988                  (not (gnus-summary-article-sparse-p number))
3989                  (not (gnus-summary-article-ancient-p number)))
3990             (setq gnus-newsgroup-unreads
3991                   (delq number gnus-newsgroup-unreads))
3992             (if gnus-newsgroup-auto-expire
3993                 (push number gnus-newsgroup-expirable)
3994               (push (cons number gnus-low-score-mark)
3995                     gnus-newsgroup-reads))))
3996
3997           (when gnus-tmp-header
3998             ;; We may have an old dummy line to output before this
3999             ;; article.
4000             (when (and gnus-tmp-dummy-line
4001                        (gnus-subject-equal
4002                         gnus-tmp-dummy-line
4003                         (mail-header-subject gnus-tmp-header)))
4004               (gnus-summary-insert-dummy-line
4005                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4006               (setq gnus-tmp-dummy-line nil))
4007
4008             ;; Compute the mark.
4009             (setq gnus-tmp-unread (gnus-article-mark number))
4010
4011             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4012                                   gnus-tmp-header gnus-tmp-level)
4013                   gnus-newsgroup-data)
4014
4015             ;; Actually insert the line.
4016             (setq
4017              gnus-tmp-subject-or-nil
4018              (cond
4019               ((and gnus-thread-ignore-subject
4020                     gnus-tmp-prev-subject
4021                     (not (inline (gnus-subject-equal
4022                                   gnus-tmp-prev-subject subject))))
4023                subject)
4024               ((zerop gnus-tmp-level)
4025                (if (and (eq gnus-summary-make-false-root 'empty)
4026                         (memq number gnus-tmp-gathered)
4027                         gnus-tmp-prev-subject
4028                         (inline (gnus-subject-equal
4029                                  gnus-tmp-prev-subject subject)))
4030                    gnus-summary-same-subject
4031                  subject))
4032               (t gnus-summary-same-subject)))
4033             (if (and (eq gnus-summary-make-false-root 'adopt)
4034                      (= gnus-tmp-level 1)
4035                      (memq number gnus-tmp-gathered))
4036                 (setq gnus-tmp-opening-bracket ?\<
4037                       gnus-tmp-closing-bracket ?\>)
4038               (setq gnus-tmp-opening-bracket ?\[
4039                     gnus-tmp-closing-bracket ?\]))
4040             (setq
4041              gnus-tmp-indentation
4042              (aref gnus-thread-indent-array gnus-tmp-level)
4043              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4044              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4045                                 gnus-summary-default-score 0)
4046              gnus-tmp-score-char
4047              (if (or (null gnus-summary-default-score)
4048                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4049                          gnus-summary-zcore-fuzz))
4050                  ?  ;Whitespace
4051                (if (< gnus-tmp-score gnus-summary-default-score)
4052                    gnus-score-below-mark gnus-score-over-mark))
4053              gnus-tmp-replied
4054              (cond ((memq number gnus-newsgroup-processable)
4055                     gnus-process-mark)
4056                    ((memq number gnus-newsgroup-cached)
4057                     gnus-cached-mark)
4058                    ((memq number gnus-newsgroup-replied)
4059                     gnus-replied-mark)
4060                    ((memq number gnus-newsgroup-saved)
4061                     gnus-saved-mark)
4062                    (t gnus-unread-mark))
4063              gnus-tmp-from (mail-header-from gnus-tmp-header)
4064              gnus-tmp-name
4065              (cond
4066               ((string-match "<[^>]+> *$" gnus-tmp-from)
4067                (setq beg-match (match-beginning 0))
4068                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4069                         (substring gnus-tmp-from (1+ (match-beginning 0))
4070                                    (1- (match-end 0))))
4071                    (substring gnus-tmp-from 0 beg-match)))
4072               ((string-match "(.+)" gnus-tmp-from)
4073                (substring gnus-tmp-from
4074                           (1+ (match-beginning 0)) (1- (match-end 0))))
4075               (t gnus-tmp-from)))
4076             (when (string= gnus-tmp-name "")
4077               (setq gnus-tmp-name gnus-tmp-from))
4078             (unless (numberp gnus-tmp-lines)
4079               (setq gnus-tmp-lines 0))
4080             (gnus-put-text-property
4081              (point)
4082              (progn (eval gnus-summary-line-format-spec) (point))
4083              'gnus-number number)
4084             (when gnus-visual-p
4085               (forward-line -1)
4086               (gnus-run-hooks 'gnus-summary-update-hook)
4087               (forward-line 1))
4088
4089             (setq gnus-tmp-prev-subject subject)))
4090
4091         (when (nth 1 thread)
4092           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4093         (incf gnus-tmp-level)
4094         (setq threads (if thread-end nil (cdar thread)))
4095         (unless threads
4096           (setq gnus-tmp-level 0)))))
4097   (gnus-message 7 "Generating summary...done"))
4098
4099 (defun gnus-summary-prepare-unthreaded (headers)
4100   "Generate an unthreaded summary buffer based on HEADERS."
4101   (let (header number mark)
4102
4103     (beginning-of-line)
4104
4105     (while headers
4106       ;; We may have to root out some bad articles...
4107       (when (memq (setq number (mail-header-number
4108                                 (setq header (pop headers))))
4109                   gnus-newsgroup-limit)
4110         ;; Mark article as read when it has a low score.
4111         (when (and gnus-summary-mark-below
4112                    (< (or (cdr (assq number gnus-newsgroup-scored))
4113                           gnus-summary-default-score 0)
4114                       gnus-summary-mark-below)
4115                    (not (gnus-summary-article-ancient-p number)))
4116           (setq gnus-newsgroup-unreads
4117                 (delq number gnus-newsgroup-unreads))
4118           (if gnus-newsgroup-auto-expire
4119               (push number gnus-newsgroup-expirable)
4120             (push (cons number gnus-low-score-mark)
4121                   gnus-newsgroup-reads)))
4122
4123         (setq mark (gnus-article-mark number))
4124         (push (gnus-data-make number mark (1+ (point)) header 0)
4125               gnus-newsgroup-data)
4126         (gnus-summary-insert-line
4127          header 0 number
4128          mark (memq number gnus-newsgroup-replied)
4129          (memq number gnus-newsgroup-expirable)
4130          (mail-header-subject header) nil
4131          (cdr (assq number gnus-newsgroup-scored))
4132          (memq number gnus-newsgroup-processable))))))
4133
4134 (defun gnus-summary-remove-list-identifiers ()
4135   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4136   (let ((regexp (if (stringp gnus-list-identifiers)
4137                     gnus-list-identifiers
4138                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4139     (dolist (header gnus-newsgroup-headers)
4140       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4141                                   " *\\)\\)+\\(Re: +\\)?\\)")
4142                           (mail-header-subject header))
4143         (mail-header-set-subject
4144          header (concat (substring (mail-header-subject header)
4145                                    0 (match-beginning 1))
4146                         (or
4147                          (match-string 3 (mail-header-subject header))
4148                          (match-string 5 (mail-header-subject header)))
4149                         (substring (mail-header-subject header)
4150                                    (match-end 1))))))))
4151
4152 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4153   "Select newsgroup GROUP.
4154 If READ-ALL is non-nil, all articles in the group are selected.
4155 If SELECT-ARTICLES, only select those articles from GROUP."
4156   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4157          ;;!!! Dirty hack; should be removed.
4158          (gnus-summary-ignore-duplicates
4159           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4160               t
4161             gnus-summary-ignore-duplicates))
4162          (info (nth 2 entry))
4163          articles fetched-articles cached)
4164
4165     (unless (gnus-check-server
4166              (setq gnus-current-select-method
4167                    (gnus-find-method-for-group group)))
4168       (error "Couldn't open server"))
4169
4170     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4171         (gnus-activate-group group)     ; Or we can activate it...
4172         (progn                          ; Or we bug out.
4173           (when (equal major-mode 'gnus-summary-mode)
4174             (kill-buffer (current-buffer)))
4175           (error "Couldn't request group %s: %s"
4176                  group (gnus-status-message group))))
4177
4178     (unless (gnus-request-group group t)
4179       (when (equal major-mode 'gnus-summary-mode)
4180         (kill-buffer (current-buffer)))
4181       (error "Couldn't request group %s: %s"
4182              group (gnus-status-message group)))
4183
4184     (setq gnus-newsgroup-name group)
4185     (setq gnus-newsgroup-unselected nil)
4186     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4187     (gnus-summary-setup-default-charset)
4188
4189     ;; Adjust and set lists of article marks.
4190     (when info
4191       (gnus-adjust-marked-articles info))
4192
4193     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4194     (setq cached
4195           (if (gnus-virtual-group-p group)
4196               gnus-newsgroup-cached
4197             (gnus-cache-articles-in-group group)))
4198
4199     (setq gnus-newsgroup-unreads
4200           (gnus-set-difference
4201            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4202            gnus-newsgroup-dormant))
4203
4204     (setq gnus-newsgroup-processable nil)
4205
4206     (gnus-update-read-articles group gnus-newsgroup-unreads)
4207
4208     (if (setq articles select-articles)
4209         (setq gnus-newsgroup-unselected
4210               (gnus-sorted-intersection
4211                gnus-newsgroup-unreads
4212                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4213       (setq articles (gnus-articles-to-read group read-all)))
4214
4215     (cond
4216      ((null articles)
4217       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4218       'quit)
4219      ((eq articles 0) nil)
4220      (t
4221       ;; Init the dependencies hash table.
4222       (setq gnus-newsgroup-dependencies
4223             (gnus-make-hashtable (length articles)))
4224       (gnus-set-global-variables)
4225       ;; Retrieve the headers and read them in.
4226       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4227       (setq gnus-newsgroup-headers
4228             (gnus-retrieve-parsed-headers
4229              articles gnus-newsgroup-name
4230              ;; We might want to fetch old headers, but
4231              ;; not if there is only 1 article.
4232              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4233                            (not (numberp gnus-fetch-old-headers)))
4234                       (> (length articles) 1))
4235                   gnus-fetch-old-headers)))
4236       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4237
4238       ;; Suppress duplicates?
4239       (when gnus-suppress-duplicates
4240         (gnus-dup-suppress-articles))
4241
4242       ;; Set the initial limit.
4243       (setq gnus-newsgroup-limit (copy-sequence articles))
4244       ;; Remove canceled articles from the list of unread articles.
4245       (setq gnus-newsgroup-unreads
4246             (gnus-set-sorted-intersection
4247              gnus-newsgroup-unreads
4248              (setq fetched-articles
4249                    (mapcar (lambda (headers) (mail-header-number headers))
4250                            gnus-newsgroup-headers))))
4251       ;; Removed marked articles that do not exist.
4252       (gnus-update-missing-marks
4253        (gnus-sorted-complement fetched-articles articles))
4254
4255       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4256       (when cached
4257         (setq gnus-newsgroup-cached cached))
4258
4259       ;; We might want to build some more threads first.
4260       (when (and gnus-fetch-old-headers
4261                  (eq gnus-headers-retrieved-by 'nov))
4262         (if (eq gnus-fetch-old-headers 'invisible)
4263             (gnus-build-all-threads)
4264           (gnus-build-old-threads)))
4265       ;; Let the Gnus agent mark articles as read.
4266       (when gnus-agent
4267         (gnus-agent-get-undownloaded-list))
4268       ;; Remove list identifiers from subject
4269       (when gnus-list-identifiers
4270         (gnus-summary-remove-list-identifiers))
4271       ;; Check whether auto-expire is to be done in this group.
4272       (setq gnus-newsgroup-auto-expire
4273             (gnus-group-auto-expirable-p group))
4274       ;; Set up the article buffer now, if necessary.
4275       (unless gnus-single-article-buffer
4276         (gnus-article-setup-buffer))
4277       ;; First and last article in this newsgroup.
4278       (when gnus-newsgroup-headers
4279         (setq gnus-newsgroup-begin
4280               (mail-header-number (car gnus-newsgroup-headers))
4281               gnus-newsgroup-end
4282               (mail-header-number
4283                (gnus-last-element gnus-newsgroup-headers))))
4284       ;; GROUP is successfully selected.
4285       (or gnus-newsgroup-headers t)))))
4286
4287 (defun gnus-articles-to-read (group &optional read-all)
4288   "Find out what articles the user wants to read."
4289   (let* ((articles
4290           ;; Select all articles if `read-all' is non-nil, or if there
4291           ;; are no unread articles.
4292           (if (or read-all
4293                   (and (zerop (length gnus-newsgroup-marked))
4294                        (zerop (length gnus-newsgroup-unreads)))
4295                   (eq (gnus-group-find-parameter group 'display)
4296                       'all))
4297               (or
4298                (gnus-uncompress-range (gnus-active group))
4299                (gnus-cache-articles-in-group group))
4300             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4301                           (copy-sequence gnus-newsgroup-unreads))
4302                   '<)))
4303          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4304          (scored (length scored-list))
4305          (number (length articles))
4306          (marked (+ (length gnus-newsgroup-marked)
4307                     (length gnus-newsgroup-dormant)))
4308          (select
4309           (cond
4310            ((numberp read-all)
4311             read-all)
4312            (t
4313             (condition-case ()
4314                 (cond
4315                  ((and (or (<= scored marked) (= scored number))
4316                        (natnump gnus-large-newsgroup)
4317                        (> number gnus-large-newsgroup))
4318                   (let* ((cursor-in-echo-area nil)
4319                          (input (read-from-minibuffer
4320                                  (format
4321                                   "How many articles from %s (max %d): "
4322                                   (gnus-limit-string gnus-newsgroup-name 35)
4323                                   number)
4324                                  (cons (number-to-string gnus-large-newsgroup)
4325                                        0))))
4326                     (if (string-match "^[ \t]*$" input)
4327                         number
4328                       input)))
4329                  ((and (> scored marked) (< scored number)
4330                        (> (- scored number) 20))
4331                   (let ((input
4332                          (read-string
4333                           (format "%s %s (%d scored, %d total): "
4334                                   "How many articles from"
4335                                   group scored number))))
4336                     (if (string-match "^[ \t]*$" input)
4337                         number input)))
4338                  (t number))
4339               (quit
4340                (message "Quit getting the articles to read")
4341                nil))))))
4342     (setq select (if (stringp select) (string-to-number select) select))
4343     (if (or (null select) (zerop select))
4344         select
4345       (if (and (not (zerop scored)) (<= (abs select) scored))
4346           (progn
4347             (setq articles (sort scored-list '<))
4348             (setq number (length articles)))
4349         (setq articles (copy-sequence articles)))
4350
4351       (when (< (abs select) number)
4352         (if (< select 0)
4353             ;; Select the N oldest articles.
4354             (setcdr (nthcdr (1- (abs select)) articles) nil)
4355           ;; Select the N most recent articles.
4356           (setq articles (nthcdr (- number select) articles))))
4357       (setq gnus-newsgroup-unselected
4358             (gnus-sorted-intersection
4359              gnus-newsgroup-unreads
4360              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4361       (when gnus-alter-articles-to-read-function
4362         (setq gnus-newsgroup-unreads
4363               (sort
4364                (funcall gnus-alter-articles-to-read-function
4365                         gnus-newsgroup-name gnus-newsgroup-unreads)
4366                '<)))
4367       articles)))
4368
4369 (defun gnus-killed-articles (killed articles)
4370   (let (out)
4371     (while articles
4372       (when (inline (gnus-member-of-range (car articles) killed))
4373         (push (car articles) out))
4374       (setq articles (cdr articles)))
4375     out))
4376
4377 (defun gnus-uncompress-marks (marks)
4378   "Uncompress the mark ranges in MARKS."
4379   (let ((uncompressed '(score bookmark))
4380         out)
4381     (while marks
4382       (if (memq (caar marks) uncompressed)
4383           (push (car marks) out)
4384         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4385       (setq marks (cdr marks)))
4386     out))
4387
4388 (defun gnus-adjust-marked-articles (info)
4389   "Set all article lists and remove all marks that are no longer valid."
4390   (let* ((marked-lists (gnus-info-marks info))
4391          (active (gnus-active (gnus-info-group info)))
4392          (min (car active))
4393          (max (cdr active))
4394          (types gnus-article-mark-lists)
4395          (uncompressed '(score bookmark killed))
4396          marks var articles article mark)
4397
4398     (while marked-lists
4399       (setq marks (pop marked-lists))
4400       (set (setq var (intern (format "gnus-newsgroup-%s"
4401                                      (car (rassq (setq mark (car marks))
4402                                                  types)))))
4403            (if (memq (car marks) uncompressed) (cdr marks)
4404              (gnus-uncompress-range (cdr marks))))
4405
4406       (setq articles (symbol-value var))
4407
4408       ;; All articles have to be subsets of the active articles.
4409       (cond
4410        ;; Adjust "simple" lists.
4411        ((memq mark '(tick dormant expire reply save))
4412         (while articles
4413           (when (or (< (setq article (pop articles)) min) (> article max))
4414             (set var (delq article (symbol-value var))))))
4415        ;; Adjust assocs.
4416        ((memq mark uncompressed)
4417         (when (not (listp (cdr (symbol-value var))))
4418           (set var (list (symbol-value var))))
4419         (when (not (listp (cdr articles)))
4420           (setq articles (list articles)))
4421         (while articles
4422           (when (or (not (consp (setq article (pop articles))))
4423                     (< (car article) min)
4424                     (> (car article) max))
4425             (set var (delq article (symbol-value var))))))))))
4426
4427 (defun gnus-update-missing-marks (missing)
4428   "Go through the list of MISSING articles and remove them from the mark lists."
4429   (when missing
4430     (let ((types gnus-article-mark-lists)
4431           var m)
4432       ;; Go through all types.
4433       (while types
4434         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4435         (when (symbol-value var)
4436           ;; This list has articles.  So we delete all missing articles
4437           ;; from it.
4438           (setq m missing)
4439           (while m
4440             (set var (delq (pop m) (symbol-value var)))))))))
4441
4442 (defun gnus-update-marks ()
4443   "Enter the various lists of marked articles into the newsgroup info list."
4444   (let ((types gnus-article-mark-lists)
4445         (info (gnus-get-info gnus-newsgroup-name))
4446         (uncompressed '(score bookmark killed))
4447         type list newmarked symbol delta-marks)
4448     (when info
4449       ;; Add all marks lists to the list of marks lists.
4450       (while (setq type (pop types))
4451         (setq list (symbol-value
4452                     (setq symbol
4453                           (intern (format "gnus-newsgroup-%s"
4454                                           (car type))))))
4455
4456         (when list
4457           ;; Get rid of the entries of the articles that have the
4458           ;; default score.
4459           (when (and (eq (cdr type) 'score)
4460                      gnus-save-score
4461                      list)
4462             (let* ((arts list)
4463                    (prev (cons nil list))
4464                    (all prev))
4465               (while arts
4466                 (if (or (not (consp (car arts)))
4467                         (= (cdar arts) gnus-summary-default-score))
4468                     (setcdr prev (cdr arts))
4469                   (setq prev arts))
4470                 (setq arts (cdr arts)))
4471               (setq list (cdr all)))))
4472
4473         (unless (memq (cdr type) uncompressed)
4474           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4475
4476         (when (gnus-check-backend-function
4477                'request-set-mark gnus-newsgroup-name)
4478           ;; propagate flags to server, with the following exceptions:
4479           ;; uncompressed:s are not proper flags (they are cons cells)
4480           ;; cache is a internal gnus flag
4481           ;; download are local to one gnus installation (well)
4482           ;; unsend are for nndraft groups only
4483           ;; xxx: generality of this?  this suits nnimap anyway
4484           (unless (memq (cdr type) (append '(cache download unsend)
4485                                            uncompressed))
4486             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4487                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4488                    (add (gnus-remove-from-range
4489                          (gnus-copy-sequence list) old)))
4490               (when add
4491                 (push (list add 'add (list (cdr type))) delta-marks))
4492               (when del
4493                 (push (list del 'del (list (cdr type))) delta-marks)))))
4494
4495         (when list
4496           (push (cons (cdr type) list) newmarked)))
4497
4498       (when delta-marks
4499         (unless (gnus-check-group gnus-newsgroup-name)
4500           (error "Can't open server for %s" gnus-newsgroup-name))
4501         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4502
4503       ;; Enter these new marks into the info of the group.
4504       (if (nthcdr 3 info)
4505           (setcar (nthcdr 3 info) newmarked)
4506         ;; Add the marks lists to the end of the info.
4507         (when newmarked
4508           (setcdr (nthcdr 2 info) (list newmarked))))
4509
4510       ;; Cut off the end of the info if there's nothing else there.
4511       (let ((i 5))
4512         (while (and (> i 2)
4513                     (not (nth i info)))
4514           (when (nthcdr (decf i) info)
4515             (setcdr (nthcdr i info) nil)))))))
4516
4517 (defun gnus-set-mode-line (where)
4518   "Set the mode line of the article or summary buffers.
4519 If WHERE is `summary', the summary mode line format will be used."
4520   ;; Is this mode line one we keep updated?
4521   (when (and (memq where gnus-updated-mode-lines)
4522              (symbol-value
4523               (intern (format "gnus-%s-mode-line-format-spec" where))))
4524     (let (mode-string)
4525       (save-excursion
4526         ;; We evaluate this in the summary buffer since these
4527         ;; variables are buffer-local to that buffer.
4528         (set-buffer gnus-summary-buffer)
4529         ;; We bind all these variables that are used in the `eval' form
4530         ;; below.
4531         (let* ((mformat (symbol-value
4532                          (intern
4533                           (format "gnus-%s-mode-line-format-spec" where))))
4534                (gnus-tmp-group-name (gnus-group-name-decode
4535                                      gnus-newsgroup-name
4536                                      (gnus-group-name-charset
4537                                       nil
4538                                       gnus-newsgroup-name)))
4539                (gnus-tmp-article-number (or gnus-current-article 0))
4540                (gnus-tmp-unread gnus-newsgroup-unreads)
4541                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4542                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4543                (gnus-tmp-unread-and-unselected
4544                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4545                             (zerop gnus-tmp-unselected))
4546                        "")
4547                       ((zerop gnus-tmp-unselected)
4548                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4549                       (t (format "{%d(+%d) more}"
4550                                  gnus-tmp-unread-and-unticked
4551                                  gnus-tmp-unselected))))
4552                (gnus-tmp-subject
4553                 (if (and gnus-current-headers
4554                          (vectorp gnus-current-headers))
4555                     (gnus-mode-string-quote
4556                      (mail-header-subject gnus-current-headers))
4557                   ""))
4558                bufname-length max-len
4559                gnus-tmp-header);; passed as argument to any user-format-funcs
4560           (setq mode-string (eval mformat))
4561           (setq bufname-length (if (string-match "%b" mode-string)
4562                                    (- (length
4563                                        (buffer-name
4564                                         (if (eq where 'summary)
4565                                             nil
4566                                           (get-buffer gnus-article-buffer))))
4567                                       2)
4568                                  0))
4569           (setq max-len (max 4 (if gnus-mode-non-string-length
4570                                    (- (window-width)
4571                                       gnus-mode-non-string-length
4572                                       bufname-length)
4573                                  (length mode-string))))
4574           ;; We might have to chop a bit of the string off...
4575           (when (> (length mode-string) max-len)
4576             (setq mode-string
4577                   (concat (gnus-truncate-string mode-string (- max-len 3))
4578                           "...")))
4579           ;; Pad the mode string a bit.
4580           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4581       ;; Update the mode line.
4582       (setq mode-line-buffer-identification
4583             (gnus-mode-line-buffer-identification (list mode-string)))
4584       (set-buffer-modified-p t))))
4585
4586 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4587   "Go through the HEADERS list and add all Xrefs to a hash table.
4588 The resulting hash table is returned, or nil if no Xrefs were found."
4589   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4590          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4591          (xref-hashtb (gnus-make-hashtable))
4592          start group entry number xrefs header)
4593     (while headers
4594       (setq header (pop headers))
4595       (when (and (setq xrefs (mail-header-xref header))
4596                  (not (memq (setq number (mail-header-number header))
4597                             unreads)))
4598         (setq start 0)
4599         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4600           (setq start (match-end 0))
4601           (setq group (if prefix
4602                           (concat prefix (substring xrefs (match-beginning 1)
4603                                                     (match-end 1)))
4604                         (substring xrefs (match-beginning 1) (match-end 1))))
4605           (setq number
4606                 (string-to-int (substring xrefs (match-beginning 2)
4607                                           (match-end 2))))
4608           (if (setq entry (gnus-gethash group xref-hashtb))
4609               (setcdr entry (cons number (cdr entry)))
4610             (gnus-sethash group (cons number nil) xref-hashtb)))))
4611     (and start xref-hashtb)))
4612
4613 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4614   "Look through all the headers and mark the Xrefs as read."
4615   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4616         name entry info xref-hashtb idlist method nth4)
4617     (save-excursion
4618       (set-buffer gnus-group-buffer)
4619       (when (setq xref-hashtb
4620                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4621         (mapatoms
4622          (lambda (group)
4623            (unless (string= from-newsgroup (setq name (symbol-name group)))
4624              (setq idlist (symbol-value group))
4625              ;; Dead groups are not updated.
4626              (and (prog1
4627                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4628                             info (nth 2 entry))
4629                     (when (stringp (setq nth4 (gnus-info-method info)))
4630                       (setq nth4 (gnus-server-to-method nth4))))
4631                   ;; Only do the xrefs if the group has the same
4632                   ;; select method as the group we have just read.
4633                   (or (gnus-methods-equal-p
4634                        nth4 (gnus-find-method-for-group from-newsgroup))
4635                       virtual
4636                       (equal nth4 (setq method (gnus-find-method-for-group
4637                                                 from-newsgroup)))
4638                       (and (equal (car nth4) (car method))
4639                            (equal (nth 1 nth4) (nth 1 method))))
4640                   gnus-use-cross-reference
4641                   (or (not (eq gnus-use-cross-reference t))
4642                       virtual
4643                       ;; Only do cross-references on subscribed
4644                       ;; groups, if that is what is wanted.
4645                       (<= (gnus-info-level info) gnus-level-subscribed))
4646                   (gnus-group-make-articles-read name idlist))))
4647          xref-hashtb)))))
4648
4649 (defun gnus-compute-read-articles (group articles)
4650   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4651          (info (nth 2 entry))
4652          (active (gnus-active group))
4653          ninfo)
4654     (when entry
4655       ;; First peel off all invalid article numbers.
4656       (when active
4657         (let ((ids articles)
4658               id first)
4659           (while (setq id (pop ids))
4660             (when (and first (> id (cdr active)))
4661               ;; We'll end up in this situation in one particular
4662               ;; obscure situation.  If you re-scan a group and get
4663               ;; a new article that is cross-posted to a different
4664               ;; group that has not been re-scanned, you might get
4665               ;; crossposted article that has a higher number than
4666               ;; Gnus believes possible.  So we re-activate this
4667               ;; group as well.  This might mean doing the
4668               ;; crossposting thingy will *increase* the number
4669               ;; of articles in some groups.  Tsk, tsk.
4670               (setq active (or (gnus-activate-group group) active)))
4671             (when (or (> id (cdr active))
4672                       (< id (car active)))
4673               (setq articles (delq id articles))))))
4674       ;; If the read list is nil, we init it.
4675       (if (and active
4676                (null (gnus-info-read info))
4677                (> (car active) 1))
4678           (setq ninfo (cons 1 (1- (car active))))
4679         (setq ninfo (gnus-info-read info)))
4680       ;; Then we add the read articles to the range.
4681       (gnus-add-to-range
4682        ninfo (setq articles (sort articles '<))))))
4683
4684 (defun gnus-group-make-articles-read (group articles)
4685   "Update the info of GROUP to say that ARTICLES are read."
4686   (let* ((num 0)
4687          (entry (gnus-gethash group gnus-newsrc-hashtb))
4688          (info (nth 2 entry))
4689          (active (gnus-active group))
4690          range)
4691     (when entry
4692       (setq range (gnus-compute-read-articles group articles))
4693       (save-excursion
4694         (set-buffer gnus-group-buffer)
4695         (gnus-undo-register
4696           `(progn
4697              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4698              (gnus-info-set-read ',info ',(gnus-info-read info))
4699              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4700              (gnus-group-update-group ,group t))))
4701       ;; Add the read articles to the range.
4702       (gnus-info-set-read info range)
4703       ;; Then we have to re-compute how many unread
4704       ;; articles there are in this group.
4705       (when active
4706         (cond
4707          ((not range)
4708           (setq num (- (1+ (cdr active)) (car active))))
4709          ((not (listp (cdr range)))
4710           (setq num (- (cdr active) (- (1+ (cdr range))
4711                                        (car range)))))
4712          (t
4713           (while range
4714             (if (numberp (car range))
4715                 (setq num (1+ num))
4716               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4717             (setq range (cdr range)))
4718           (setq num (- (cdr active) num))))
4719         ;; Update the number of unread articles.
4720         (setcar entry num)
4721         ;; Update the group buffer.
4722         (gnus-group-update-group group t)))))
4723
4724 (defvar gnus-newsgroup-none-id 0)
4725
4726 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4727   (let ((cur nntp-server-buffer)
4728         (dependencies
4729          (or dependencies
4730              (save-excursion (set-buffer gnus-summary-buffer)
4731                              gnus-newsgroup-dependencies)))
4732         headers id end ref
4733         (mail-parse-charset gnus-newsgroup-charset)
4734         (mail-parse-ignored-charsets
4735          (save-excursion (condition-case nil
4736                              (set-buffer gnus-summary-buffer)
4737                            (error))
4738                          gnus-newsgroup-ignored-charsets)))
4739     (save-excursion
4740       (set-buffer nntp-server-buffer)
4741       ;; Translate all TAB characters into SPACE characters.
4742       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4743       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4744       (gnus-run-hooks 'gnus-parse-headers-hook)
4745       (let ((case-fold-search t)
4746             in-reply-to header p lines chars ctype)
4747         (goto-char (point-min))
4748         ;; Search to the beginning of the next header.  Error messages
4749         ;; do not begin with 2 or 3.
4750         (while (re-search-forward "^[23][0-9]+ " nil t)
4751           (setq id nil
4752                 ref nil)
4753           ;; This implementation of this function, with nine
4754           ;; search-forwards instead of the one re-search-forward and
4755           ;; a case (which basically was the old function) is actually
4756           ;; about twice as fast, even though it looks messier.  You
4757           ;; can't have everything, I guess.  Speed and elegance
4758           ;; doesn't always go hand in hand.
4759           (setq
4760            header
4761            (make-full-mail-header
4762             ;; Number.
4763             (prog1
4764                 (read cur)
4765               (end-of-line)
4766               (setq p (point))
4767               (narrow-to-region (point)
4768                                 (or (and (search-forward "\n.\n" nil t)
4769                                          (- (point) 2))
4770                                     (point))))
4771             ;; Subject.
4772             (progn
4773               (goto-char p)
4774               (if (search-forward "\nsubject: " nil t)
4775                   (nnheader-header-value)
4776                 "(none)"))
4777             ;; From.
4778             (progn
4779               (goto-char p)
4780               (if (or (search-forward "\nfrom: " nil t)
4781                       (search-forward "\nfrom:" nil t))
4782                   (nnheader-header-value)
4783                 "(nobody)"))
4784             ;; Date.
4785             (progn
4786               (goto-char p)
4787               (if (search-forward "\ndate: " nil t)
4788                   (nnheader-header-value)
4789                 ""))
4790             ;; Message-ID.
4791             (progn
4792               (goto-char p)
4793               (setq id (if (re-search-forward
4794                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4795                            ;; We do it this way to make sure the Message-ID
4796                            ;; is (somewhat) syntactically valid.
4797                            (buffer-substring (match-beginning 1)
4798                                              (match-end 1))
4799                          ;; If there was no message-id, we just fake one
4800                          ;; to make subsequent routines simpler.
4801                          (nnheader-generate-fake-message-id))))
4802             ;; References.
4803             (progn
4804               (goto-char p)
4805               (if (search-forward "\nreferences: " nil t)
4806                   (progn
4807                     (setq end (point))
4808                     (prog1
4809                         (nnheader-header-value)
4810                       (setq ref
4811                             (buffer-substring
4812                              (progn
4813                                ;; (end-of-line)
4814                                (search-backward ">" end t)
4815                                (1+ (point)))
4816                              (progn
4817                                (search-backward "<" end t)
4818                                (point))))))
4819                 ;; Get the references from the in-reply-to header if there
4820                 ;; were no references and the in-reply-to header looks
4821                 ;; promising.
4822                 (if (and (search-forward "\nin-reply-to: " nil t)
4823                          (setq in-reply-to (nnheader-header-value))
4824                          (string-match "<[^>]+>" in-reply-to))
4825                     (let (ref2)
4826                       (setq ref (substring in-reply-to (match-beginning 0)
4827                                            (match-end 0)))
4828                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4829                         (setq ref2 (substring in-reply-to (match-beginning 0)
4830                                               (match-end 0)))
4831                         (when (> (length ref2) (length ref))
4832                           (setq ref ref2)))
4833                       ref)
4834                   (setq ref nil))))
4835             ;; Chars.
4836             (progn
4837               (goto-char p)
4838               (if (search-forward "\nchars: " nil t)
4839                   (if (numberp (setq chars (ignore-errors (read cur))))
4840                       chars 0)
4841                 0))
4842             ;; Lines.
4843             (progn
4844               (goto-char p)
4845               (if (search-forward "\nlines: " nil t)
4846                   (if (numberp (setq lines (ignore-errors (read cur))))
4847                       lines 0)
4848                 0))
4849             ;; Xref.
4850             (progn
4851               (goto-char p)
4852               (and (search-forward "\nxref: " nil t)
4853                    (nnheader-header-value)))
4854             ;; Extra.
4855             (when gnus-extra-headers
4856               (let ((extra gnus-extra-headers)
4857                     out)
4858                 (while extra
4859                   (goto-char p)
4860                   (when (search-forward
4861                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4862                     (push (cons (car extra) (nnheader-header-value)) out))
4863                   (pop extra))
4864                 out))))
4865           (goto-char p)
4866           (if (and (search-forward "\ncontent-type: " nil t)
4867                    (setq ctype (nnheader-header-value)))
4868               (mime-entity-set-content-type-internal
4869                header (mime-parse-Content-Type ctype)))
4870           (when (equal id ref)
4871             (setq ref nil))
4872
4873           (when gnus-alter-header-function
4874             (funcall gnus-alter-header-function header)
4875             (setq id (mail-header-id header)
4876                   ref (gnus-parent-id (mail-header-references header))))
4877
4878           (when (setq header
4879                       (gnus-dependencies-add-header
4880                        header dependencies force-new))
4881             (push header headers))
4882           (goto-char (point-max))
4883           (widen))
4884         (nreverse headers)))))
4885
4886 ;; Goes through the xover lines and returns a list of vectors
4887 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4888                                                   force-new dependencies
4889                                                   group also-fetch-heads)
4890   "Parse the news overview data in the server buffer.
4891 Return a list of headers that match SEQUENCE (see
4892 `nntp-retrieve-headers')."
4893   ;; Get the Xref when the users reads the articles since most/some
4894   ;; NNTP servers do not include Xrefs when using XOVER.
4895   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4896   (let ((mail-parse-charset gnus-newsgroup-charset)
4897         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4898         (cur nntp-server-buffer)
4899         (dependencies (or dependencies gnus-newsgroup-dependencies))
4900         number headers header)
4901     (save-excursion
4902       (set-buffer nntp-server-buffer)
4903       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4904       ;; Allow the user to mangle the headers before parsing them.
4905       (gnus-run-hooks 'gnus-parse-headers-hook)
4906       (goto-char (point-min))
4907       (while (not (eobp))
4908         (condition-case ()
4909             (while (and sequence (not (eobp)))
4910               (setq number (read cur))
4911               (while (and sequence
4912                           (< (car sequence) number))
4913                 (setq sequence (cdr sequence)))
4914               (and sequence
4915                    (eq number (car sequence))
4916                    (progn
4917                      (setq sequence (cdr sequence))
4918                      (setq header (inline
4919                                     (gnus-nov-parse-line
4920                                      number dependencies force-new))))
4921                    (push header headers))
4922               (forward-line 1))
4923           (error
4924            (gnus-error 4 "Strange nov line (%d)"
4925                        (count-lines (point-min) (point)))))
4926         (forward-line 1))
4927       ;; A common bug in inn is that if you have posted an article and
4928       ;; then retrieves the active file, it will answer correctly --
4929       ;; the new article is included.  However, a NOV entry for the
4930       ;; article may not have been generated yet, so this may fail.
4931       ;; We work around this problem by retrieving the last few
4932       ;; headers using HEAD.
4933       (if (or (not also-fetch-heads)
4934               (not sequence))
4935           ;; We (probably) got all the headers.
4936           (nreverse headers)
4937         (let ((gnus-nov-is-evil t))
4938           (nconc
4939            (nreverse headers)
4940            (gnus-retrieve-parsed-headers sequence group)
4941            ))))))
4942
4943 (defun gnus-article-get-xrefs ()
4944   "Fill in the Xref value in `gnus-current-headers', if necessary.
4945 This is meant to be called in `gnus-article-internal-prepare-hook'."
4946   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4947                                  gnus-current-headers)))
4948     (or (not gnus-use-cross-reference)
4949         (not headers)
4950         (and (mail-header-xref headers)
4951              (not (string= (mail-header-xref headers) "")))
4952         (let ((case-fold-search t)
4953               xref)
4954           (save-restriction
4955             (nnheader-narrow-to-headers)
4956             (goto-char (point-min))
4957             (when (or (and (not (eobp))
4958                            (eq (downcase (char-after)) ?x)
4959                            (looking-at "Xref:"))
4960                       (search-forward "\nXref:" nil t))
4961               (goto-char (1+ (match-end 0)))
4962               (setq xref (buffer-substring (point)
4963                                            (progn (end-of-line) (point))))
4964               (mail-header-set-xref headers xref)))))))
4965
4966 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4967   "Find article ID and insert the summary line for that article.
4968 OLD-HEADER can either be a header or a line number to insert
4969 the subject line on."
4970   (let* ((line (and (numberp old-header) old-header))
4971          (old-header (and (vectorp old-header) old-header))
4972          (header (cond ((and old-header use-old-header)
4973                         old-header)
4974                        ((and (numberp id)
4975                              (gnus-number-to-header id))
4976                         (gnus-number-to-header id))
4977                        (t
4978                         (gnus-read-header id))))
4979          (number (and (numberp id) id))
4980          d)
4981     (when header
4982       ;; Rebuild the thread that this article is part of and go to the
4983       ;; article we have fetched.
4984       (when (and (not gnus-show-threads)
4985                  old-header)
4986         (when (and number
4987                    (setq d (gnus-data-find (mail-header-number old-header))))
4988           (goto-char (gnus-data-pos d))
4989           (gnus-data-remove
4990            number
4991            (- (gnus-point-at-bol)
4992               (prog1
4993                   (1+ (gnus-point-at-eol))
4994                 (gnus-delete-line))))))
4995       (when old-header
4996         (mail-header-set-number header (mail-header-number old-header)))
4997       (setq gnus-newsgroup-sparse
4998             (delq (setq number (mail-header-number header))
4999                   gnus-newsgroup-sparse))
5000       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5001       (push number gnus-newsgroup-limit)
5002       (gnus-rebuild-thread (mail-header-id header) line)
5003       (gnus-summary-goto-subject number nil t))
5004     (when (and (numberp number)
5005                (> number 0))
5006       ;; We have to update the boundaries even if we can't fetch the
5007       ;; article if ID is a number -- so that the next `P' or `N'
5008       ;; command will fetch the previous (or next) article even
5009       ;; if the one we tried to fetch this time has been canceled.
5010       (when (> number gnus-newsgroup-end)
5011         (setq gnus-newsgroup-end number))
5012       (when (< number gnus-newsgroup-begin)
5013         (setq gnus-newsgroup-begin number))
5014       (setq gnus-newsgroup-unselected
5015             (delq number gnus-newsgroup-unselected)))
5016     ;; Report back a success?
5017     (and header (mail-header-number header))))
5018
5019 ;;; Process/prefix in the summary buffer
5020
5021 (defun gnus-summary-work-articles (n)
5022   "Return a list of articles to be worked upon.
5023 The prefix argument, the list of process marked articles, and the
5024 current article will be taken into consideration."
5025   (save-excursion
5026     (set-buffer gnus-summary-buffer)
5027     (cond
5028      (n
5029       ;; A numerical prefix has been given.
5030       (setq n (prefix-numeric-value n))
5031       (let ((backward (< n 0))
5032             (n (abs (prefix-numeric-value n)))
5033             articles article)
5034         (save-excursion
5035           (while
5036               (and (> n 0)
5037                    (push (setq article (gnus-summary-article-number))
5038                          articles)
5039                    (if backward
5040                        (gnus-summary-find-prev nil article)
5041                      (gnus-summary-find-next nil article)))
5042             (decf n)))
5043         (nreverse articles)))
5044      ((and (gnus-region-active-p) (mark))
5045       (message "region active")
5046       ;; Work on the region between point and mark.
5047       (let ((max (max (point) (mark)))
5048             articles article)
5049         (save-excursion
5050           (goto-char (min (point) (mark)))
5051           (while
5052               (and
5053                (push (setq article (gnus-summary-article-number)) articles)
5054                (gnus-summary-find-next nil article)
5055                (< (point) max)))
5056           (nreverse articles))))
5057      (gnus-newsgroup-processable
5058       ;; There are process-marked articles present.
5059       ;; Save current state.
5060       (gnus-summary-save-process-mark)
5061       ;; Return the list.
5062       (reverse gnus-newsgroup-processable))
5063      (t
5064       ;; Just return the current article.
5065       (list (gnus-summary-article-number))))))
5066
5067 (defmacro gnus-summary-iterate (arg &rest forms)
5068   "Iterate over the process/prefixed articles and do FORMS.
5069 ARG is the interactive prefix given to the command.  FORMS will be
5070 executed with point over the summary line of the articles."
5071   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5072     `(let ((,articles (gnus-summary-work-articles ,arg)))
5073        (while ,articles
5074          (gnus-summary-goto-subject (car ,articles))
5075          ,@forms
5076          (pop ,articles)))))
5077
5078 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5079 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5080
5081 (defun gnus-summary-save-process-mark ()
5082   "Push the current set of process marked articles on the stack."
5083   (interactive)
5084   (push (copy-sequence gnus-newsgroup-processable)
5085         gnus-newsgroup-process-stack))
5086
5087 (defun gnus-summary-kill-process-mark ()
5088   "Push the current set of process marked articles on the stack and unmark."
5089   (interactive)
5090   (gnus-summary-save-process-mark)
5091   (gnus-summary-unmark-all-processable))
5092
5093 (defun gnus-summary-yank-process-mark ()
5094   "Pop the last process mark state off the stack and restore it."
5095   (interactive)
5096   (unless gnus-newsgroup-process-stack
5097     (error "Empty mark stack"))
5098   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5099
5100 (defun gnus-summary-process-mark-set (set)
5101   "Make SET into the current process marked articles."
5102   (gnus-summary-unmark-all-processable)
5103   (while set
5104     (gnus-summary-set-process-mark (pop set))))
5105
5106 ;;; Searching and stuff
5107
5108 (defun gnus-summary-search-group (&optional backward use-level)
5109   "Search for next unread newsgroup.
5110 If optional argument BACKWARD is non-nil, search backward instead."
5111   (save-excursion
5112     (set-buffer gnus-group-buffer)
5113     (when (gnus-group-search-forward
5114            backward nil (if use-level (gnus-group-group-level) nil))
5115       (gnus-group-group-name))))
5116
5117 (defun gnus-summary-best-group (&optional exclude-group)
5118   "Find the name of the best unread group.
5119 If EXCLUDE-GROUP, do not go to this group."
5120   (save-excursion
5121     (set-buffer gnus-group-buffer)
5122     (save-excursion
5123       (gnus-group-best-unread-group exclude-group))))
5124
5125 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5126   (if backward (gnus-summary-find-prev)
5127     (let* ((dummy (gnus-summary-article-intangible-p))
5128            (article (or article (gnus-summary-article-number)))
5129            (arts (gnus-data-find-list article))
5130            result)
5131       (when (and (not dummy)
5132                  (or (not gnus-summary-check-current)
5133                      (not unread)
5134                      (not (gnus-data-unread-p (car arts)))))
5135         (setq arts (cdr arts)))
5136       (when (setq result
5137                   (if unread
5138                       (progn
5139                         (while arts
5140                           (when (or (and undownloaded
5141                                          (eq gnus-undownloaded-mark
5142                                              (gnus-data-mark (car arts))))
5143                                     (gnus-data-unread-p (car arts)))
5144                             (setq result (car arts)
5145                                   arts nil))
5146                           (setq arts (cdr arts)))
5147                         result)
5148                     (car arts)))
5149         (goto-char (gnus-data-pos result))
5150         (gnus-data-number result)))))
5151
5152 (defun gnus-summary-find-prev (&optional unread article)
5153   (let* ((eobp (eobp))
5154          (article (or article (gnus-summary-article-number)))
5155          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5156          result)
5157     (when (and (not eobp)
5158                (or (not gnus-summary-check-current)
5159                    (not unread)
5160                    (not (gnus-data-unread-p (car arts)))))
5161       (setq arts (cdr arts)))
5162     (when (setq result
5163                 (if unread
5164                     (progn
5165                       (while arts
5166                         (when (gnus-data-unread-p (car arts))
5167                           (setq result (car arts)
5168                                 arts nil))
5169                         (setq arts (cdr arts)))
5170                       result)
5171                   (car arts)))
5172       (goto-char (gnus-data-pos result))
5173       (gnus-data-number result))))
5174
5175 (defun gnus-summary-find-subject (subject &optional unread backward article)
5176   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5177          (article (or article (gnus-summary-article-number)))
5178          (articles (gnus-data-list backward))
5179          (arts (gnus-data-find-list article articles))
5180          result)
5181     (when (or (not gnus-summary-check-current)
5182               (not unread)
5183               (not (gnus-data-unread-p (car arts))))
5184       (setq arts (cdr arts)))
5185     (while arts
5186       (and (or (not unread)
5187                (gnus-data-unread-p (car arts)))
5188            (vectorp (gnus-data-header (car arts)))
5189            (gnus-subject-equal
5190             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5191            (setq result (car arts)
5192                  arts nil))
5193       (setq arts (cdr arts)))
5194     (and result
5195          (goto-char (gnus-data-pos result))
5196          (gnus-data-number result))))
5197
5198 (defun gnus-summary-search-forward (&optional unread subject backward)
5199   "Search forward for an article.
5200 If UNREAD, look for unread articles.  If SUBJECT, look for
5201 articles with that subject.  If BACKWARD, search backward instead."
5202   (cond (subject (gnus-summary-find-subject subject unread backward))
5203         (backward (gnus-summary-find-prev unread))
5204         (t (gnus-summary-find-next unread))))
5205
5206 (defun gnus-recenter (&optional n)
5207   "Center point in window and redisplay frame.
5208 Also do horizontal recentering."
5209   (interactive "P")
5210   (when (and gnus-auto-center-summary
5211              (not (eq gnus-auto-center-summary 'vertical)))
5212     (gnus-horizontal-recenter))
5213   (recenter n))
5214
5215 (defun gnus-summary-recenter ()
5216   "Center point in the summary window.
5217 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5218 displayed, no centering will be performed."
5219   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5220   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5221   (interactive)
5222   (let* ((top (cond ((< (window-height) 4) 0)
5223                     ((< (window-height) 7) 1)
5224                     (t (if (numberp gnus-auto-center-summary)
5225                            gnus-auto-center-summary
5226                          2))))
5227          (height (1- (window-height)))
5228          (bottom (save-excursion (goto-char (point-max))
5229                                  (forward-line (- height))
5230                                  (point)))
5231          (window (get-buffer-window (current-buffer))))
5232     ;; The user has to want it.
5233     (when gnus-auto-center-summary
5234       (when (get-buffer-window gnus-article-buffer)
5235         ;; Only do recentering when the article buffer is displayed,
5236         ;; Set the window start to either `bottom', which is the biggest
5237         ;; possible valid number, or the second line from the top,
5238         ;; whichever is the least.
5239         (set-window-start
5240          window (min bottom (save-excursion
5241                               (forward-line (- top)) (point)))
5242          t))
5243       ;; Do horizontal recentering while we're at it.
5244       (when (and (get-buffer-window (current-buffer) t)
5245                  (not (eq gnus-auto-center-summary 'vertical)))
5246         (let ((selected (selected-window)))
5247           (select-window (get-buffer-window (current-buffer) t))
5248           (gnus-summary-position-point)
5249           (gnus-horizontal-recenter)
5250           (select-window selected))))))
5251
5252 (defun gnus-summary-jump-to-group (newsgroup)
5253   "Move point to NEWSGROUP in group mode buffer."
5254   ;; Keep update point of group mode buffer if visible.
5255   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5256       (save-window-excursion
5257         ;; Take care of tree window mode.
5258         (when (get-buffer-window gnus-group-buffer)
5259           (pop-to-buffer gnus-group-buffer))
5260         (gnus-group-jump-to-group newsgroup))
5261     (save-excursion
5262       ;; Take care of tree window mode.
5263       (if (get-buffer-window gnus-group-buffer)
5264           (pop-to-buffer gnus-group-buffer)
5265         (set-buffer gnus-group-buffer))
5266       (gnus-group-jump-to-group newsgroup))))
5267
5268 ;; This function returns a list of article numbers based on the
5269 ;; difference between the ranges of read articles in this group and
5270 ;; the range of active articles.
5271 (defun gnus-list-of-unread-articles (group)
5272   (let* ((read (gnus-info-read (gnus-get-info group)))
5273          (active (or (gnus-active group) (gnus-activate-group group)))
5274          (last (cdr active))
5275          first nlast unread)
5276     ;; If none are read, then all are unread.
5277     (if (not read)
5278         (setq first (car active))
5279       ;; If the range of read articles is a single range, then the
5280       ;; first unread article is the article after the last read
5281       ;; article.  Sounds logical, doesn't it?
5282       (if (and (not (listp (cdr read)))
5283                (or (< (car read) (car active))
5284                    (progn (setq read (list read))
5285                           nil)))
5286           (setq first (max (car active) (1+ (cdr read))))
5287         ;; `read' is a list of ranges.
5288         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5289                                   (caar read)))
5290                   1)
5291           (setq first (car active)))
5292         (while read
5293           (when first
5294             (while (< first nlast)
5295               (push first unread)
5296               (setq first (1+ first))))
5297           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5298           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5299           (setq read (cdr read)))))
5300     ;; And add the last unread articles.
5301     (while (<= first last)
5302       (push first unread)
5303       (setq first (1+ first)))
5304     ;; Return the list of unread articles.
5305     (delq 0 (nreverse unread))))
5306
5307 (defun gnus-list-of-read-articles (group)
5308   "Return a list of unread, unticked and non-dormant articles."
5309   (let* ((info (gnus-get-info group))
5310          (marked (gnus-info-marks info))
5311          (active (gnus-active group)))
5312     (and info active
5313          (gnus-set-difference
5314           (gnus-sorted-complement
5315            (gnus-uncompress-range active)
5316            (gnus-list-of-unread-articles group))
5317           (append
5318            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5319            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5320
5321 ;; Various summary commands
5322
5323 (defun gnus-summary-select-article-buffer ()
5324   "Reconfigure windows to show article buffer."
5325   (interactive)
5326   (if (not (gnus-buffer-live-p gnus-article-buffer))
5327       (error "There is no article buffer for this summary buffer")
5328     (gnus-configure-windows 'article)
5329     (select-window (get-buffer-window gnus-article-buffer))))
5330
5331 (defun gnus-summary-universal-argument (arg)
5332   "Perform any operation on all articles that are process/prefixed."
5333   (interactive "P")
5334   (let ((articles (gnus-summary-work-articles arg))
5335         func article)
5336     (if (eq
5337          (setq
5338           func
5339           (key-binding
5340            (read-key-sequence
5341             (substitute-command-keys
5342              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5343          'undefined)
5344         (gnus-error 1 "Undefined key")
5345       (save-excursion
5346         (while articles
5347           (gnus-summary-goto-subject (setq article (pop articles)))
5348           (let (gnus-newsgroup-processable)
5349             (command-execute func))
5350           (gnus-summary-remove-process-mark article)))))
5351   (gnus-summary-position-point))
5352
5353 (defun gnus-summary-toggle-truncation (&optional arg)
5354   "Toggle truncation of summary lines.
5355 With arg, turn line truncation on iff arg is positive."
5356   (interactive "P")
5357   (setq truncate-lines
5358         (if (null arg) (not truncate-lines)
5359           (> (prefix-numeric-value arg) 0)))
5360   (redraw-display))
5361
5362 (defun gnus-summary-reselect-current-group (&optional all rescan)
5363   "Exit and then reselect the current newsgroup.
5364 The prefix argument ALL means to select all articles."
5365   (interactive "P")
5366   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5367     (error "Ephemeral groups can't be reselected"))
5368   (let ((current-subject (gnus-summary-article-number))
5369         (group gnus-newsgroup-name))
5370     (setq gnus-newsgroup-begin nil)
5371     (gnus-summary-exit)
5372     ;; We have to adjust the point of group mode buffer because
5373     ;; point was moved to the next unread newsgroup by exiting.
5374     (gnus-summary-jump-to-group group)
5375     (when rescan
5376       (save-excursion
5377         (save-window-excursion
5378           ;; Don't show group contents.
5379           (set-window-start (selected-window) (point-max))
5380           (gnus-group-get-new-news-this-group 1))))
5381     (gnus-group-read-group all t)
5382     (gnus-summary-goto-subject current-subject nil t)))
5383
5384 (defun gnus-summary-rescan-group (&optional all)
5385   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5386   (interactive "P")
5387   (gnus-summary-reselect-current-group all t))
5388
5389 (defun gnus-summary-update-info (&optional non-destructive)
5390   (save-excursion
5391     (let ((group gnus-newsgroup-name))
5392       (when group
5393         (when gnus-newsgroup-kill-headers
5394           (setq gnus-newsgroup-killed
5395                 (gnus-compress-sequence
5396                  (nconc
5397                   (gnus-set-sorted-intersection
5398                    (gnus-uncompress-range gnus-newsgroup-killed)
5399                    (setq gnus-newsgroup-unselected
5400                          (sort gnus-newsgroup-unselected '<)))
5401                   (setq gnus-newsgroup-unreads
5402                         (sort gnus-newsgroup-unreads '<)))
5403                  t)))
5404         (unless (listp (cdr gnus-newsgroup-killed))
5405           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5406         (let ((headers gnus-newsgroup-headers))
5407           ;; Set the new ranges of read articles.
5408           (save-excursion
5409             (set-buffer gnus-group-buffer)
5410             (gnus-undo-force-boundary))
5411           (gnus-update-read-articles
5412            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5413           ;; Set the current article marks.
5414           (let ((gnus-newsgroup-scored
5415                  (if (and (not gnus-save-score)
5416                           (not non-destructive))
5417                      nil
5418                    gnus-newsgroup-scored)))
5419             (save-excursion
5420               (gnus-update-marks)))
5421           ;; Do the cross-ref thing.
5422           (when gnus-use-cross-reference
5423             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5424           ;; Do not switch windows but change the buffer to work.
5425           (set-buffer gnus-group-buffer)
5426           (unless (gnus-ephemeral-group-p group)
5427             (gnus-group-update-group group)))))))
5428
5429 (defun gnus-summary-save-newsrc (&optional force)
5430   "Save the current number of read/marked articles in the dribble buffer.
5431 The dribble buffer will then be saved.
5432 If FORCE (the prefix), also save the .newsrc file(s)."
5433   (interactive "P")
5434   (gnus-summary-update-info t)
5435   (if force
5436       (gnus-save-newsrc-file)
5437     (gnus-dribble-save)))
5438
5439 (defun gnus-summary-exit (&optional temporary)
5440   "Exit reading current newsgroup, and then return to group selection mode.
5441 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5442   (interactive)
5443   (gnus-set-global-variables)
5444   (gnus-kill-save-kill-buffer)
5445   (gnus-async-halt-prefetch)
5446   (let* ((group gnus-newsgroup-name)
5447          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5448          (mode major-mode)
5449          (group-point nil)
5450          (buf (current-buffer)))
5451     (unless quit-config
5452       ;; Do adaptive scoring, and possibly save score files.
5453       (when gnus-newsgroup-adaptive
5454         (gnus-score-adaptive))
5455       (when gnus-use-scoring
5456         (gnus-score-save)))
5457     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5458     ;; If we have several article buffers, we kill them at exit.
5459     (unless gnus-single-article-buffer
5460       (gnus-kill-buffer gnus-original-article-buffer)
5461       (setq gnus-article-current nil))
5462     (when gnus-use-cache
5463       (gnus-cache-possibly-remove-articles)
5464       (gnus-cache-save-buffers))
5465     (gnus-async-prefetch-remove-group group)
5466     (when gnus-suppress-duplicates
5467       (gnus-dup-enter-articles))
5468     (when gnus-use-trees
5469       (gnus-tree-close group))
5470     (when gnus-use-cache
5471       (gnus-cache-write-active))
5472     ;; Remove entries for this group.
5473     (nnmail-purge-split-history (gnus-group-real-name group))
5474     ;; Make all changes in this group permanent.
5475     (unless quit-config
5476       (gnus-run-hooks 'gnus-exit-group-hook)
5477       (gnus-summary-update-info))
5478     (gnus-close-group group)
5479     ;; Make sure where we were, and go to next newsgroup.
5480     (set-buffer gnus-group-buffer)
5481     (unless quit-config
5482       (gnus-group-jump-to-group group))
5483     (gnus-run-hooks 'gnus-summary-exit-hook)
5484     (unless (or quit-config
5485                 ;; If this group has disappeared from the summary
5486                 ;; buffer, don't skip forwards.
5487                 (not (string= group (gnus-group-group-name))))
5488       (gnus-group-next-unread-group 1))
5489     (setq group-point (point))
5490     (if temporary
5491         nil                             ;Nothing to do.
5492       ;; If we have several article buffers, we kill them at exit.
5493       (unless gnus-single-article-buffer
5494         (gnus-kill-buffer gnus-article-buffer)
5495         (gnus-kill-buffer gnus-original-article-buffer)
5496         (setq gnus-article-current nil))
5497       (set-buffer buf)
5498       (if (not gnus-kill-summary-on-exit)
5499           (gnus-deaden-summary)
5500         ;; We set all buffer-local variables to nil.  It is unclear why
5501         ;; this is needed, but if we don't, buffer-local variables are
5502         ;; not garbage-collected, it seems.  This would the lead to en
5503         ;; ever-growing Emacs.
5504         (gnus-summary-clear-local-variables)
5505         (when (get-buffer gnus-article-buffer)
5506           (bury-buffer gnus-article-buffer))
5507         ;; We clear the global counterparts of the buffer-local
5508         ;; variables as well, just to be on the safe side.
5509         (set-buffer gnus-group-buffer)
5510         (gnus-summary-clear-local-variables)
5511         ;; Return to group mode buffer.
5512         (when (eq mode 'gnus-summary-mode)
5513           (gnus-kill-buffer buf)))
5514       (setq gnus-current-select-method gnus-select-method)
5515       (pop-to-buffer gnus-group-buffer)
5516       (if (not quit-config)
5517           (progn
5518             (goto-char group-point)
5519             (gnus-configure-windows 'group 'force)
5520             (unless (pos-visible-in-window-p)
5521               (forward-line (/ (static-if (featurep 'xemacs)
5522                                    (window-displayed-height)
5523                                  (1- (window-height)))
5524                                -2))
5525               (set-window-start (selected-window) (point))
5526               (goto-char group-point)))
5527         (gnus-handle-ephemeral-exit quit-config))
5528       ;; Clear the current group name.
5529       (unless quit-config
5530         (setq gnus-newsgroup-name nil)))))
5531
5532 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5533 (defun gnus-summary-exit-no-update (&optional no-questions)
5534   "Quit reading current newsgroup without updating read article info."
5535   (interactive)
5536   (let* ((group gnus-newsgroup-name)
5537          (quit-config (gnus-group-quit-config group)))
5538     (when (or no-questions
5539               gnus-expert-user
5540               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5541       (gnus-async-halt-prefetch)
5542       (mapcar 'funcall
5543               (delq 'gnus-summary-expire-articles
5544                     (copy-sequence gnus-summary-prepare-exit-hook)))
5545       ;; If we have several article buffers, we kill them at exit.
5546       (unless gnus-single-article-buffer
5547         (gnus-kill-buffer gnus-article-buffer)
5548         (gnus-kill-buffer gnus-original-article-buffer)
5549         (setq gnus-article-current nil))
5550       (if (not gnus-kill-summary-on-exit)
5551           (gnus-deaden-summary)
5552         (gnus-close-group group)
5553         (gnus-summary-clear-local-variables)
5554         (set-buffer gnus-group-buffer)
5555         (gnus-summary-clear-local-variables)
5556         (when (get-buffer gnus-summary-buffer)
5557           (kill-buffer gnus-summary-buffer)))
5558       (unless gnus-single-article-buffer
5559         (setq gnus-article-current nil))
5560       (when gnus-use-trees
5561         (gnus-tree-close group))
5562       (gnus-async-prefetch-remove-group group)
5563       (when (get-buffer gnus-article-buffer)
5564         (bury-buffer gnus-article-buffer))
5565       ;; Return to the group buffer.
5566       (gnus-configure-windows 'group 'force)
5567       ;; Clear the current group name.
5568       (setq gnus-newsgroup-name nil)
5569       (when (equal (gnus-group-group-name) group)
5570         (gnus-group-next-unread-group 1))
5571       (when quit-config
5572         (gnus-handle-ephemeral-exit quit-config)))))
5573
5574 (defun gnus-handle-ephemeral-exit (quit-config)
5575   "Handle movement when leaving an ephemeral group.
5576 The state which existed when entering the ephemeral is reset."
5577   (if (not (buffer-name (car quit-config)))
5578       (gnus-configure-windows 'group 'force)
5579     (set-buffer (car quit-config))
5580     (cond ((eq major-mode 'gnus-summary-mode)
5581            (gnus-set-global-variables))
5582           ((eq major-mode 'gnus-article-mode)
5583            (save-excursion
5584              ;; The `gnus-summary-buffer' variable may point
5585              ;; to the old summary buffer when using a single
5586              ;; article buffer.
5587              (unless (gnus-buffer-live-p gnus-summary-buffer)
5588                (set-buffer gnus-group-buffer))
5589              (set-buffer gnus-summary-buffer)
5590              (gnus-set-global-variables))))
5591     (if (or (eq (cdr quit-config) 'article)
5592             (eq (cdr quit-config) 'pick))
5593         (progn
5594           ;; The current article may be from the ephemeral group
5595           ;; thus it is best that we reload this article
5596           (gnus-summary-show-article)
5597           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5598               (gnus-configure-windows 'pick 'force)
5599             (gnus-configure-windows (cdr quit-config) 'force)))
5600       (gnus-configure-windows (cdr quit-config) 'force))
5601     (when (eq major-mode 'gnus-summary-mode)
5602       (gnus-summary-next-subject 1 nil t)
5603       (gnus-summary-recenter)
5604       (gnus-summary-position-point))))
5605
5606 (defun gnus-summary-preview-mime-message ()
5607   "MIME decode and play this message."
5608   (interactive)
5609   (let ((gnus-break-pages nil)
5610         (gnus-show-mime t))
5611     (gnus-summary-select-article gnus-show-all-headers t))
5612   (select-window (get-buffer-window gnus-article-buffer)))
5613
5614 ;;; Dead summaries.
5615
5616 (defvar gnus-dead-summary-mode-map nil)
5617
5618 (unless gnus-dead-summary-mode-map
5619   (setq gnus-dead-summary-mode-map (make-keymap))
5620   (suppress-keymap gnus-dead-summary-mode-map)
5621   (substitute-key-definition
5622    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5623   (let ((keys '("\C-d" "\r" "\177" [delete])))
5624     (while keys
5625       (define-key gnus-dead-summary-mode-map
5626         (pop keys) 'gnus-summary-wake-up-the-dead))))
5627
5628 (defvar gnus-dead-summary-mode nil
5629   "Minor mode for Gnus summary buffers.")
5630
5631 (defun gnus-dead-summary-mode (&optional arg)
5632   "Minor mode for Gnus summary buffers."
5633   (interactive "P")
5634   (when (eq major-mode 'gnus-summary-mode)
5635     (make-local-variable 'gnus-dead-summary-mode)
5636     (setq gnus-dead-summary-mode
5637           (if (null arg) (not gnus-dead-summary-mode)
5638             (> (prefix-numeric-value arg) 0)))
5639     (when gnus-dead-summary-mode
5640       (gnus-add-minor-mode
5641        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5642
5643 (defun gnus-deaden-summary ()
5644   "Make the current summary buffer into a dead summary buffer."
5645   ;; Kill any previous dead summary buffer.
5646   (when (and gnus-dead-summary
5647              (buffer-name gnus-dead-summary))
5648     (save-excursion
5649       (set-buffer gnus-dead-summary)
5650       (when gnus-dead-summary-mode
5651         (kill-buffer (current-buffer)))))
5652   ;; Make this the current dead summary.
5653   (setq gnus-dead-summary (current-buffer))
5654   (gnus-dead-summary-mode 1)
5655   (let ((name (buffer-name)))
5656     (when (string-match "Summary" name)
5657       (rename-buffer
5658        (concat (substring name 0 (match-beginning 0)) "Dead "
5659                (substring name (match-beginning 0)))
5660        t)
5661       (bury-buffer))))
5662
5663 (defun gnus-kill-or-deaden-summary (buffer)
5664   "Kill or deaden the summary BUFFER."
5665   (save-excursion
5666     (when (and (buffer-name buffer)
5667                (not gnus-single-article-buffer))
5668       (save-excursion
5669         (set-buffer buffer)
5670         (gnus-kill-buffer gnus-article-buffer)
5671         (gnus-kill-buffer gnus-original-article-buffer)))
5672     (cond (gnus-kill-summary-on-exit
5673            (when (and gnus-use-trees
5674                       (gnus-buffer-exists-p buffer))
5675              (save-excursion
5676                (set-buffer buffer)
5677                (gnus-tree-close gnus-newsgroup-name)))
5678            (gnus-kill-buffer buffer))
5679           ((gnus-buffer-exists-p buffer)
5680            (save-excursion
5681              (set-buffer buffer)
5682              (gnus-deaden-summary))))))
5683
5684 (defun gnus-summary-wake-up-the-dead (&rest args)
5685   "Wake up the dead summary buffer."
5686   (interactive)
5687   (gnus-dead-summary-mode -1)
5688   (let ((name (buffer-name)))
5689     (when (string-match "Dead " name)
5690       (rename-buffer
5691        (concat (substring name 0 (match-beginning 0))
5692                (substring name (match-end 0)))
5693        t)))
5694   (gnus-message 3 "This dead summary is now alive again"))
5695
5696 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5697 (defun gnus-summary-fetch-faq (&optional faq-dir)
5698   "Fetch the FAQ for the current group.
5699 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5700 in."
5701   (interactive
5702    (list
5703     (when current-prefix-arg
5704       (completing-read
5705        "Faq dir: " (and (listp gnus-group-faq-directory)
5706                         (mapcar (lambda (file) (list file))
5707                                 gnus-group-faq-directory))))))
5708   (let (gnus-faq-buffer)
5709     (when (setq gnus-faq-buffer
5710                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5711       (gnus-configure-windows 'summary-faq))))
5712
5713 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5714 (defun gnus-summary-describe-group (&optional force)
5715   "Describe the current newsgroup."
5716   (interactive "P")
5717   (gnus-group-describe-group force gnus-newsgroup-name))
5718
5719 (defun gnus-summary-describe-briefly ()
5720   "Describe summary mode commands briefly."
5721   (interactive)
5722   (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")))
5723
5724 ;; Walking around group mode buffer from summary mode.
5725
5726 (defun gnus-summary-next-group (&optional no-article target-group backward)
5727   "Exit current newsgroup and then select next unread newsgroup.
5728 If prefix argument NO-ARTICLE is non-nil, no article is selected
5729 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5730 previous group instead."
5731   (interactive "P")
5732   ;; Stop pre-fetching.
5733   (gnus-async-halt-prefetch)
5734   (let ((current-group gnus-newsgroup-name)
5735         (current-buffer (current-buffer))
5736         entered)
5737     ;; First we semi-exit this group to update Xrefs and all variables.
5738     ;; We can't do a real exit, because the window conf must remain
5739     ;; the same in case the user is prompted for info, and we don't
5740     ;; want the window conf to change before that...
5741     (gnus-summary-exit t)
5742     (while (not entered)
5743       ;; Then we find what group we are supposed to enter.
5744       (set-buffer gnus-group-buffer)
5745       (gnus-group-jump-to-group current-group)
5746       (setq target-group
5747             (or target-group
5748                 (if (eq gnus-keep-same-level 'best)
5749                     (gnus-summary-best-group gnus-newsgroup-name)
5750                   (gnus-summary-search-group backward gnus-keep-same-level))))
5751       (if (not target-group)
5752           ;; There are no further groups, so we return to the group
5753           ;; buffer.
5754           (progn
5755             (gnus-message 5 "Returning to the group buffer")
5756             (setq entered t)
5757             (when (gnus-buffer-live-p current-buffer)
5758               (set-buffer current-buffer)
5759               (gnus-summary-exit))
5760             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5761         ;; We try to enter the target group.
5762         (gnus-group-jump-to-group target-group)
5763         (let ((unreads (gnus-group-group-unread)))
5764           (if (and (or (eq t unreads)
5765                        (and unreads (not (zerop unreads))))
5766                    (gnus-summary-read-group
5767                     target-group nil no-article
5768                     (and (buffer-name current-buffer) current-buffer)
5769                     nil backward))
5770               (setq entered t)
5771             (setq current-group target-group
5772                   target-group nil)))))))
5773
5774 (defun gnus-summary-prev-group (&optional no-article)
5775   "Exit current newsgroup and then select previous unread newsgroup.
5776 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5777   (interactive "P")
5778   (gnus-summary-next-group no-article nil t))
5779
5780 ;; Walking around summary lines.
5781
5782 (defun gnus-summary-first-subject (&optional unread undownloaded)
5783   "Go to the first unread subject.
5784 If UNREAD is non-nil, go to the first unread article.
5785 Returns the article selected or nil if there are no unread articles."
5786   (interactive "P")
5787   (prog1
5788       (cond
5789        ;; Empty summary.
5790        ((null gnus-newsgroup-data)
5791         (gnus-message 3 "No articles in the group")
5792         nil)
5793        ;; Pick the first article.
5794        ((not unread)
5795         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5796         (gnus-data-number (car gnus-newsgroup-data)))
5797        ;; No unread articles.
5798        ((null gnus-newsgroup-unreads)
5799         (gnus-message 3 "No more unread articles")
5800         nil)
5801        ;; Find the first unread article.
5802        (t
5803         (let ((data gnus-newsgroup-data))
5804           (while (and data
5805                       (and (not (and undownloaded
5806                                      (eq gnus-undownloaded-mark
5807                                          (gnus-data-mark (car data)))))
5808                            (not (gnus-data-unread-p (car data)))))
5809             (setq data (cdr data)))
5810           (when data
5811             (goto-char (gnus-data-pos (car data)))
5812             (gnus-data-number (car data))))))
5813     (gnus-summary-position-point)))
5814
5815 (defun gnus-summary-next-subject (n &optional unread dont-display)
5816   "Go to next N'th summary line.
5817 If N is negative, go to the previous N'th subject line.
5818 If UNREAD is non-nil, only unread articles are selected.
5819 The difference between N and the actual number of steps taken is
5820 returned."
5821   (interactive "p")
5822   (let ((backward (< n 0))
5823         (n (abs n)))
5824     (while (and (> n 0)
5825                 (if backward
5826                     (gnus-summary-find-prev unread)
5827                   (gnus-summary-find-next unread)))
5828       (unless (zerop (setq n (1- n)))
5829         (gnus-summary-show-thread)))
5830     (when (/= 0 n)
5831       (gnus-message 7 "No more%s articles"
5832                     (if unread " unread" "")))
5833     (unless dont-display
5834       (gnus-summary-recenter)
5835       (gnus-summary-position-point))
5836     n))
5837
5838 (defun gnus-summary-next-unread-subject (n)
5839   "Go to next N'th unread summary line."
5840   (interactive "p")
5841   (gnus-summary-next-subject n t))
5842
5843 (defun gnus-summary-prev-subject (n &optional unread)
5844   "Go to previous N'th summary line.
5845 If optional argument UNREAD is non-nil, only unread article is selected."
5846   (interactive "p")
5847   (gnus-summary-next-subject (- n) unread))
5848
5849 (defun gnus-summary-prev-unread-subject (n)
5850   "Go to previous N'th unread summary line."
5851   (interactive "p")
5852   (gnus-summary-next-subject (- n) t))
5853
5854 (defun gnus-summary-goto-subject (article &optional force silent)
5855   "Go the subject line of ARTICLE.
5856 If FORCE, also allow jumping to articles not currently shown."
5857   (interactive "nArticle number: ")
5858   (let ((b (point))
5859         (data (gnus-data-find article)))
5860     ;; We read in the article if we have to.
5861     (and (not data)
5862          force
5863          (gnus-summary-insert-subject
5864           article
5865           (if (or (numberp force) (vectorp force)) force)
5866           t)
5867          (setq data (gnus-data-find article)))
5868     (goto-char b)
5869     (if (not data)
5870         (progn
5871           (unless silent
5872             (gnus-message 3 "Can't find article %d" article))
5873           nil)
5874       (goto-char (gnus-data-pos data))
5875       (gnus-summary-position-point)
5876       article)))
5877
5878 ;; Walking around summary lines with displaying articles.
5879
5880 (defun gnus-summary-expand-window (&optional arg)
5881   "Make the summary buffer take up the entire Emacs frame.
5882 Given a prefix, will force an `article' buffer configuration."
5883   (interactive "P")
5884   (if arg
5885       (gnus-configure-windows 'article 'force)
5886     (gnus-configure-windows 'summary 'force)))
5887
5888 (defun gnus-summary-display-article (article &optional all-header)
5889   "Display ARTICLE in article buffer."
5890   (gnus-set-global-variables)
5891   (if (null article)
5892       nil
5893     (prog1
5894         (if gnus-summary-display-article-function
5895             (funcall gnus-summary-display-article-function article all-header)
5896           (gnus-article-prepare article all-header))
5897       (with-current-buffer gnus-article-buffer
5898         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5899              nil))
5900       (gnus-run-hooks 'gnus-select-article-hook)
5901       (when (and gnus-current-article
5902                  (not (zerop gnus-current-article)))
5903         (gnus-summary-goto-subject gnus-current-article))
5904       (gnus-summary-recenter)
5905       (when (and gnus-use-trees gnus-show-threads)
5906         (gnus-possibly-generate-tree article)
5907         (gnus-highlight-selected-tree article))
5908       ;; Successfully display article.
5909       (gnus-article-set-window-start
5910        (cdr (assq article gnus-newsgroup-bookmarks))))))
5911
5912 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5913   "Select the current article.
5914 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5915 non-nil, the article will be re-fetched even if it already present in
5916 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5917 be displayed."
5918   ;; Make sure we are in the summary buffer to work around bbdb bug.
5919   (unless (eq major-mode 'gnus-summary-mode)
5920     (set-buffer gnus-summary-buffer))
5921   (let ((article (or article (gnus-summary-article-number)))
5922         (all-headers (not (not all-headers))) ;Must be T or NIL.
5923         gnus-summary-display-article-function)
5924     (and (not pseudo)
5925          (gnus-summary-article-pseudo-p article)
5926          (error "This is a pseudo-article"))
5927     (save-excursion
5928       (set-buffer gnus-summary-buffer)
5929       (if (or (and gnus-single-article-buffer
5930                    (or (null gnus-current-article)
5931                        (null gnus-article-current)
5932                        (null (get-buffer gnus-article-buffer))
5933                        (not (eq article (cdr gnus-article-current)))
5934                        (not (equal (car gnus-article-current)
5935                                    gnus-newsgroup-name))))
5936               (and (not gnus-single-article-buffer)
5937                    (or (null gnus-current-article)
5938                        (not (eq gnus-current-article article))))
5939               force)
5940           ;; The requested article is different from the current article.
5941           (progn
5942             (gnus-summary-display-article article all-headers)
5943             (when (or all-headers gnus-show-all-headers)
5944               (gnus-article-show-all-headers))
5945             (gnus-article-set-window-start
5946              (cdr (assq article gnus-newsgroup-bookmarks)))
5947             article)
5948         (when (or all-headers gnus-show-all-headers)
5949           (gnus-article-show-all-headers))
5950         'old))))
5951
5952 (defun gnus-summary-set-current-mark (&optional current-mark)
5953   "Obsolete function."
5954   nil)
5955
5956 (defun gnus-summary-next-article (&optional unread subject backward push)
5957   "Select the next article.
5958 If UNREAD, only unread articles are selected.
5959 If SUBJECT, only articles with SUBJECT are selected.
5960 If BACKWARD, the previous article is selected instead of the next."
5961   (interactive "P")
5962   (cond
5963    ;; Is there such an article?
5964    ((and (gnus-summary-search-forward unread subject backward)
5965          (or (gnus-summary-display-article (gnus-summary-article-number))
5966              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5967     (gnus-summary-position-point))
5968    ;; If not, we try the first unread, if that is wanted.
5969    ((and subject
5970          gnus-auto-select-same
5971          (gnus-summary-first-unread-article))
5972     (gnus-summary-position-point)
5973     (gnus-message 6 "Wrapped"))
5974    ;; Try to get next/previous article not displayed in this group.
5975    ((and gnus-auto-extend-newsgroup
5976          (not unread) (not subject))
5977     (gnus-summary-goto-article
5978      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5979      nil (count-lines (point-min) (point))))
5980    ;; Go to next/previous group.
5981    (t
5982     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5983       (gnus-summary-jump-to-group gnus-newsgroup-name))
5984     (let ((cmd last-command-char)
5985           (point
5986            (save-excursion
5987              (set-buffer gnus-group-buffer)
5988              (point)))
5989           (group
5990            (if (eq gnus-keep-same-level 'best)
5991                (gnus-summary-best-group gnus-newsgroup-name)
5992              (gnus-summary-search-group backward gnus-keep-same-level))))
5993       ;; For some reason, the group window gets selected.  We change
5994       ;; it back.
5995       (select-window (get-buffer-window (current-buffer)))
5996       ;; Select next unread newsgroup automagically.
5997       (cond
5998        ((or (not gnus-auto-select-next)
5999             (not cmd))
6000         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6001        ((or (eq gnus-auto-select-next 'quietly)
6002             (and (eq gnus-auto-select-next 'slightly-quietly)
6003                  push)
6004             (and (eq gnus-auto-select-next 'almost-quietly)
6005                  (gnus-summary-last-article-p)))
6006         ;; Select quietly.
6007         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6008             (gnus-summary-exit)
6009           (gnus-message 7 "No more%s articles (%s)..."
6010                         (if unread " unread" "")
6011                         (if group (concat "selecting " group)
6012                           "exiting"))
6013           (gnus-summary-next-group nil group backward)))
6014        (t
6015         (when (gnus-key-press-event-p last-input-event)
6016           (gnus-summary-walk-group-buffer
6017            gnus-newsgroup-name cmd unread backward point))))))))
6018
6019 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6020   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6021                       (?\C-p (gnus-group-prev-unread-group 1))))
6022         (cursor-in-echo-area t)
6023         keve key group ended)
6024     (save-excursion
6025       (set-buffer gnus-group-buffer)
6026       (goto-char start)
6027       (setq group
6028             (if (eq gnus-keep-same-level 'best)
6029                 (gnus-summary-best-group gnus-newsgroup-name)
6030               (gnus-summary-search-group backward gnus-keep-same-level))))
6031     (while (not ended)
6032       (gnus-message
6033        5 "No more%s articles%s" (if unread " unread" "")
6034        (if (and group
6035                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6036            (format " (Type %s for %s [%s])"
6037                    (single-key-description cmd) group
6038                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6039          (format " (Type %s to exit %s)"
6040                  (single-key-description cmd)
6041                  gnus-newsgroup-name)))
6042       ;; Confirm auto selection.
6043       (setq key (car (setq keve (gnus-read-event-char))))
6044       (setq ended t)
6045       (cond
6046        ((assq key keystrokes)
6047         (let ((obuf (current-buffer)))
6048           (switch-to-buffer gnus-group-buffer)
6049           (when group
6050             (gnus-group-jump-to-group group))
6051           (eval (cadr (assq key keystrokes)))
6052           (setq group (gnus-group-group-name))
6053           (switch-to-buffer obuf))
6054         (setq ended nil))
6055        ((equal key cmd)
6056         (if (or (not group)
6057                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6058             (gnus-summary-exit)
6059           (gnus-summary-next-group nil group backward)))
6060        (t
6061         (push (cdr keve) unread-command-events))))))
6062
6063 (defun gnus-summary-next-unread-article ()
6064   "Select unread article after current one."
6065   (interactive)
6066   (gnus-summary-next-article
6067    (or (not (eq gnus-summary-goto-unread 'never))
6068        (gnus-summary-last-article-p (gnus-summary-article-number)))
6069    (and gnus-auto-select-same
6070         (gnus-summary-article-subject))))
6071
6072 (defun gnus-summary-prev-article (&optional unread subject)
6073   "Select the article after the current one.
6074 If UNREAD is non-nil, only unread articles are selected."
6075   (interactive "P")
6076   (gnus-summary-next-article unread subject t))
6077
6078 (defun gnus-summary-prev-unread-article ()
6079   "Select unread article before current one."
6080   (interactive)
6081   (gnus-summary-prev-article
6082    (or (not (eq gnus-summary-goto-unread 'never))
6083        (gnus-summary-first-article-p (gnus-summary-article-number)))
6084    (and gnus-auto-select-same
6085         (gnus-summary-article-subject))))
6086
6087 (defun gnus-summary-next-page (&optional lines circular)
6088   "Show next page of the selected article.
6089 If at the end of the current article, select the next article.
6090 LINES says how many lines should be scrolled up.
6091
6092 If CIRCULAR is non-nil, go to the start of the article instead of
6093 selecting the next article when reaching the end of the current
6094 article."
6095   (interactive "P")
6096   (setq gnus-summary-buffer (current-buffer))
6097   (gnus-set-global-variables)
6098   (let ((article (gnus-summary-article-number))
6099         (article-window (get-buffer-window gnus-article-buffer t))
6100         endp)
6101     ;; If the buffer is empty, we have no article.
6102     (unless article
6103       (error "No article to select"))
6104     (gnus-configure-windows 'article)
6105     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6106         (if (and (eq gnus-summary-goto-unread 'never)
6107                  (not (gnus-summary-last-article-p article)))
6108             (gnus-summary-next-article)
6109           (gnus-summary-next-unread-article))
6110       (if (or (null gnus-current-article)
6111               (null gnus-article-current)
6112               (/= article (cdr gnus-article-current))
6113               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6114           ;; Selected subject is different from current article's.
6115           (gnus-summary-display-article article)
6116         (when article-window
6117           (gnus-eval-in-buffer-window gnus-article-buffer
6118             (setq endp (gnus-article-next-page lines)))
6119           (when endp
6120             (cond (circular
6121                    (gnus-summary-beginning-of-article))
6122                   (lines
6123                    (gnus-message 3 "End of message"))
6124                   ((null lines)
6125                    (if (and (eq gnus-summary-goto-unread 'never)
6126                             (not (gnus-summary-last-article-p article)))
6127                        (gnus-summary-next-article)
6128                      (gnus-summary-next-unread-article))))))))
6129     (gnus-summary-recenter)
6130     (gnus-summary-position-point)))
6131
6132 (defun gnus-summary-prev-page (&optional lines move)
6133   "Show previous page of selected article.
6134 Argument LINES specifies lines to be scrolled down.
6135 If MOVE, move to the previous unread article if point is at
6136 the beginning of the buffer."
6137   (interactive "P")
6138   (let ((article (gnus-summary-article-number))
6139         (article-window (get-buffer-window gnus-article-buffer t))
6140         endp)
6141     (gnus-configure-windows 'article)
6142     (if (or (null gnus-current-article)
6143             (null gnus-article-current)
6144             (/= article (cdr gnus-article-current))
6145             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6146         ;; Selected subject is different from current article's.
6147         (gnus-summary-display-article article)
6148       (gnus-summary-recenter)
6149       (when article-window
6150         (gnus-eval-in-buffer-window gnus-article-buffer
6151           (setq endp (gnus-article-prev-page lines)))
6152         (when (and move endp)
6153           (cond (lines
6154                  (gnus-message 3 "Beginning of message"))
6155                 ((null lines)
6156                  (if (and (eq gnus-summary-goto-unread 'never)
6157                           (not (gnus-summary-first-article-p article)))
6158                      (gnus-summary-prev-article)
6159                    (gnus-summary-prev-unread-article))))))))
6160   (gnus-summary-position-point))
6161
6162 (defun gnus-summary-prev-page-or-article (&optional lines)
6163   "Show previous page of selected article.
6164 Argument LINES specifies lines to be scrolled down.
6165 If at the beginning of the article, go to the next article."
6166   (interactive "P")
6167   (gnus-summary-prev-page lines t))
6168
6169 (defun gnus-summary-scroll-up (lines)
6170   "Scroll up (or down) one line current article.
6171 Argument LINES specifies lines to be scrolled up (or down if negative)."
6172   (interactive "p")
6173   (gnus-configure-windows 'article)
6174   (gnus-summary-show-thread)
6175   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6176     (gnus-eval-in-buffer-window gnus-article-buffer
6177       (cond ((> lines 0)
6178              (when (gnus-article-next-page lines)
6179                (gnus-message 3 "End of message")))
6180             ((< lines 0)
6181              (gnus-article-prev-page (- lines))))))
6182   (gnus-summary-recenter)
6183   (gnus-summary-position-point))
6184
6185 (defun gnus-summary-scroll-down (lines)
6186   "Scroll down (or up) one line current article.
6187 Argument LINES specifies lines to be scrolled down (or up if negative)."
6188   (interactive "p")
6189   (gnus-summary-scroll-up (- lines)))
6190
6191 (defun gnus-summary-next-same-subject ()
6192   "Select next article which has the same subject as current one."
6193   (interactive)
6194   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6195
6196 (defun gnus-summary-prev-same-subject ()
6197   "Select previous article which has the same subject as current one."
6198   (interactive)
6199   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6200
6201 (defun gnus-summary-next-unread-same-subject ()
6202   "Select next unread article which has the same subject as current one."
6203   (interactive)
6204   (gnus-summary-next-article t (gnus-summary-article-subject)))
6205
6206 (defun gnus-summary-prev-unread-same-subject ()
6207   "Select previous unread article which has the same subject as current one."
6208   (interactive)
6209   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6210
6211 (defun gnus-summary-first-unread-article ()
6212   "Select the first unread article.
6213 Return nil if there are no unread articles."
6214   (interactive)
6215   (prog1
6216       (when (gnus-summary-first-subject t)
6217         (gnus-summary-show-thread)
6218         (gnus-summary-first-subject t)
6219         (gnus-summary-display-article (gnus-summary-article-number)))
6220     (gnus-summary-position-point)))
6221
6222 (defun gnus-summary-first-unread-subject ()
6223   "Place the point on the subject line of the first unread article.
6224 Return nil if there are no unread articles."
6225   (interactive)
6226   (prog1
6227       (when (gnus-summary-first-subject t)
6228         (gnus-summary-show-thread)
6229         (gnus-summary-first-subject t))
6230     (gnus-summary-position-point)))
6231
6232 (defun gnus-summary-first-article ()
6233   "Select the first article.
6234 Return nil if there are no articles."
6235   (interactive)
6236   (prog1
6237       (when (gnus-summary-first-subject)
6238         (gnus-summary-show-thread)
6239         (gnus-summary-first-subject)
6240         (gnus-summary-display-article (gnus-summary-article-number)))
6241     (gnus-summary-position-point)))
6242
6243 (defun gnus-summary-best-unread-article ()
6244   "Select the unread article with the highest score."
6245   (interactive)
6246   (let ((best -1000000)
6247         (data gnus-newsgroup-data)
6248         article score)
6249     (while data
6250       (and (gnus-data-unread-p (car data))
6251            (> (setq score
6252                     (gnus-summary-article-score (gnus-data-number (car data))))
6253               best)
6254            (setq best score
6255                  article (gnus-data-number (car data))))
6256       (setq data (cdr data)))
6257     (prog1
6258         (if article
6259             (gnus-summary-goto-article article)
6260           (error "No unread articles"))
6261       (gnus-summary-position-point))))
6262
6263 (defun gnus-summary-last-subject ()
6264   "Go to the last displayed subject line in the group."
6265   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6266     (when article
6267       (gnus-summary-goto-subject article))))
6268
6269 (defun gnus-summary-goto-article (article &optional all-headers force)
6270   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6271 If ALL-HEADERS is non-nil, no header lines are hidden.
6272 If FORCE, go to the article even if it isn't displayed.  If FORCE
6273 is a number, it is the line the article is to be displayed on."
6274   (interactive
6275    (list
6276     (completing-read
6277      "Article number or Message-ID: "
6278      (mapcar (lambda (number) (list (int-to-string number)))
6279              gnus-newsgroup-limit))
6280     current-prefix-arg
6281     t))
6282   (prog1
6283       (if (and (stringp article)
6284                (string-match "@" article))
6285           (gnus-summary-refer-article article)
6286         (when (stringp article)
6287           (setq article (string-to-number article)))
6288         (if (gnus-summary-goto-subject article force)
6289             (gnus-summary-display-article article all-headers)
6290           (gnus-message 4 "Couldn't go to article %s" article) nil))
6291     (gnus-summary-position-point)))
6292
6293 (defun gnus-summary-goto-last-article ()
6294   "Go to the previously read article."
6295   (interactive)
6296   (prog1
6297       (when gnus-last-article
6298         (gnus-summary-goto-article gnus-last-article nil t))
6299     (gnus-summary-position-point)))
6300
6301 (defun gnus-summary-pop-article (number)
6302   "Pop one article off the history and go to the previous.
6303 NUMBER articles will be popped off."
6304   (interactive "p")
6305   (let (to)
6306     (setq gnus-newsgroup-history
6307           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6308     (if to
6309         (gnus-summary-goto-article (car to) nil t)
6310       (error "Article history empty")))
6311   (gnus-summary-position-point))
6312
6313 ;; Summary commands and functions for limiting the summary buffer.
6314
6315 (defun gnus-summary-limit-to-articles (n)
6316   "Limit the summary buffer to the next N articles.
6317 If not given a prefix, use the process marked articles instead."
6318   (interactive "P")
6319   (prog1
6320       (let ((articles (gnus-summary-work-articles n)))
6321         (setq gnus-newsgroup-processable nil)
6322         (gnus-summary-limit articles))
6323     (gnus-summary-position-point)))
6324
6325 (defun gnus-summary-pop-limit (&optional total)
6326   "Restore the previous limit.
6327 If given a prefix, remove all limits."
6328   (interactive "P")
6329   (when total
6330     (setq gnus-newsgroup-limits
6331           (list (mapcar (lambda (h) (mail-header-number h))
6332                         gnus-newsgroup-headers))))
6333   (unless gnus-newsgroup-limits
6334     (error "No limit to pop"))
6335   (prog1
6336       (gnus-summary-limit nil 'pop)
6337     (gnus-summary-position-point)))
6338
6339 (defun gnus-summary-limit-to-subject (subject &optional header)
6340   "Limit the summary buffer to articles that have subjects that match a regexp."
6341   (interactive "sLimit to subject (regexp): ")
6342   (unless header
6343     (setq header "subject"))
6344   (when (not (equal "" subject))
6345     (prog1
6346         (let ((articles (gnus-summary-find-matching
6347                          (or header "subject") subject 'all)))
6348           (unless articles
6349             (error "Found no matches for \"%s\"" subject))
6350           (gnus-summary-limit articles))
6351       (gnus-summary-position-point))))
6352
6353 (defun gnus-summary-limit-to-author (from)
6354   "Limit the summary buffer to articles that have authors that match a regexp."
6355   (interactive "sLimit to author (regexp): ")
6356   (gnus-summary-limit-to-subject from "from"))
6357
6358 (defun gnus-summary-limit-to-age (age &optional younger-p)
6359   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6360 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6361 articles that are younger than AGE days."
6362   (interactive
6363    (let ((younger current-prefix-arg)
6364          (days-got nil)
6365          days)
6366      (while (not days-got)
6367        (setq days (if younger
6368                       (read-string "Limit to articles within (in days): ")
6369                     (read-string "Limit to articles old than (in days): ")))
6370        (when (> (length days) 0)
6371          (setq days (read days)))
6372        (if (numberp days)
6373            (setq days-got t)
6374          (message "Please enter a number.")
6375          (sleep-for 1)))
6376      (list days younger)))
6377   (prog1
6378       (let ((data gnus-newsgroup-data)
6379             (cutoff (days-to-time age))
6380             articles d date is-younger)
6381         (while (setq d (pop data))
6382           (when (and (vectorp (gnus-data-header d))
6383                      (setq date (mail-header-date (gnus-data-header d))))
6384             (setq is-younger (time-less-p
6385                               (time-since (condition-case ()
6386                                               (date-to-time date)
6387                                             (error '(0 0))))
6388                               cutoff))
6389             (when (if younger-p
6390                       is-younger
6391                     (not is-younger))
6392               (push (gnus-data-number d) articles))))
6393         (gnus-summary-limit (nreverse articles)))
6394     (gnus-summary-position-point)))
6395
6396 (defun gnus-summary-limit-to-extra (header regexp)
6397   "Limit the summary buffer to articles that match an 'extra' header."
6398   (interactive
6399    (let ((header
6400           (intern
6401            (gnus-completing-read
6402             (symbol-name (car gnus-extra-headers))
6403             "Limit extra header:"
6404             (mapcar (lambda (x)
6405                       (cons (symbol-name x) x))
6406                     gnus-extra-headers)
6407             nil
6408             t))))
6409      (list header
6410            (read-string (format "Limit to header %s (regexp): " header)))))
6411   (when (not (equal "" regexp))
6412     (prog1
6413         (let ((articles (gnus-summary-find-matching
6414                          (cons 'extra header) regexp 'all)))
6415           (unless articles
6416             (error "Found no matches for \"%s\"" regexp))
6417           (gnus-summary-limit articles))
6418       (gnus-summary-position-point))))
6419
6420 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6421 (make-obsolete
6422  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6423
6424 (defun gnus-summary-limit-to-unread (&optional all)
6425   "Limit the summary buffer to articles that are not marked as read.
6426 If ALL is non-nil, limit strictly to unread articles."
6427   (interactive "P")
6428   (if all
6429       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6430     (gnus-summary-limit-to-marks
6431      ;; Concat all the marks that say that an article is read and have
6432      ;; those removed.
6433      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6434            gnus-killed-mark gnus-kill-file-mark
6435            gnus-low-score-mark gnus-expirable-mark
6436            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6437            gnus-duplicate-mark gnus-souped-mark)
6438      'reverse)))
6439
6440 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6441 (make-obsolete 'gnus-summary-delete-marked-with
6442                'gnus-summary-limit-exlude-marks)
6443
6444 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6445   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6446 If REVERSE, limit the summary buffer to articles that are marked
6447 with MARKS.  MARKS can either be a string of marks or a list of marks.
6448 Returns how many articles were removed."
6449   (interactive "sMarks: ")
6450   (gnus-summary-limit-to-marks marks t))
6451
6452 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6453   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6454 If REVERSE (the prefix), limit the summary buffer to articles that are
6455 not marked with MARKS.  MARKS can either be a string of marks or a
6456 list of marks.
6457 Returns how many articles were removed."
6458   (interactive "sMarks: \nP")
6459   (prog1
6460       (let ((data gnus-newsgroup-data)
6461             (marks (if (listp marks) marks
6462                      (append marks nil))) ; Transform to list.
6463             articles)
6464         (while data
6465           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6466                   (memq (gnus-data-mark (car data)) marks))
6467             (push (gnus-data-number (car data)) articles))
6468           (setq data (cdr data)))
6469         (gnus-summary-limit articles))
6470     (gnus-summary-position-point)))
6471
6472 (defun gnus-summary-limit-to-score (&optional score)
6473   "Limit to articles with score at or above SCORE."
6474   (interactive "P")
6475   (setq score (if score
6476                   (prefix-numeric-value score)
6477                 (or gnus-summary-default-score 0)))
6478   (let ((data gnus-newsgroup-data)
6479         articles)
6480     (while data
6481       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6482                 score)
6483         (push (gnus-data-number (car data)) articles))
6484       (setq data (cdr data)))
6485     (prog1
6486         (gnus-summary-limit articles)
6487       (gnus-summary-position-point))))
6488
6489 (defun gnus-summary-limit-include-thread (id)
6490   "Display all the hidden articles that in the current thread."
6491   (interactive (list (mail-header-id (gnus-summary-article-header))))
6492   (let ((articles (gnus-articles-in-thread
6493                    (gnus-id-to-thread (gnus-root-id id)))))
6494     (prog1
6495         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6496       (gnus-summary-position-point))))
6497
6498 (defun gnus-summary-limit-include-dormant ()
6499   "Display all the hidden articles that are marked as dormant.
6500 Note that this command only works on a subset of the articles currently
6501 fetched for this group."
6502   (interactive)
6503   (unless gnus-newsgroup-dormant
6504     (error "There are no dormant articles in this group"))
6505   (prog1
6506       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6507     (gnus-summary-position-point)))
6508
6509 (defun gnus-summary-limit-exclude-dormant ()
6510   "Hide all dormant articles."
6511   (interactive)
6512   (prog1
6513       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6514     (gnus-summary-position-point)))
6515
6516 (defun gnus-summary-limit-exclude-childless-dormant ()
6517   "Hide all dormant articles that have no children."
6518   (interactive)
6519   (let ((data (gnus-data-list t))
6520         articles d children)
6521     ;; Find all articles that are either not dormant or have
6522     ;; children.
6523     (while (setq d (pop data))
6524       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6525                 (and (setq children
6526                            (gnus-article-children (gnus-data-number d)))
6527                      (let (found)
6528                        (while children
6529                          (when (memq (car children) articles)
6530                            (setq children nil
6531                                  found t))
6532                          (pop children))
6533                        found)))
6534         (push (gnus-data-number d) articles)))
6535     ;; Do the limiting.
6536     (prog1
6537         (gnus-summary-limit articles)
6538       (gnus-summary-position-point))))
6539
6540 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6541   "Mark all unread excluded articles as read.
6542 If ALL, mark even excluded ticked and dormants as read."
6543   (interactive "P")
6544   (let ((articles (gnus-sorted-complement
6545                    (sort
6546                     (mapcar (lambda (h) (mail-header-number h))
6547                             gnus-newsgroup-headers)
6548                     '<)
6549                    (sort gnus-newsgroup-limit '<)))
6550         article)
6551     (setq gnus-newsgroup-unreads
6552           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6553     (if all
6554         (setq gnus-newsgroup-dormant nil
6555               gnus-newsgroup-marked nil
6556               gnus-newsgroup-reads
6557               (nconc
6558                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6559                gnus-newsgroup-reads))
6560       (while (setq article (pop articles))
6561         (unless (or (memq article gnus-newsgroup-dormant)
6562                     (memq article gnus-newsgroup-marked))
6563           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6564
6565 (defun gnus-summary-limit (articles &optional pop)
6566   (if pop
6567       ;; We pop the previous limit off the stack and use that.
6568       (setq articles (car gnus-newsgroup-limits)
6569             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6570     ;; We use the new limit, so we push the old limit on the stack.
6571     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6572   ;; Set the limit.
6573   (setq gnus-newsgroup-limit articles)
6574   (let ((total (length gnus-newsgroup-data))
6575         (data (gnus-data-find-list (gnus-summary-article-number)))
6576         (gnus-summary-mark-below nil)   ; Inhibit this.
6577         found)
6578     ;; This will do all the work of generating the new summary buffer
6579     ;; according to the new limit.
6580     (gnus-summary-prepare)
6581     ;; Hide any threads, possibly.
6582     (and gnus-show-threads
6583          gnus-thread-hide-subtree
6584          (gnus-summary-hide-all-threads))
6585     ;; Try to return to the article you were at, or one in the
6586     ;; neighborhood.
6587     (when data
6588       ;; We try to find some article after the current one.
6589       (while data
6590         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6591           (setq data nil
6592                 found t))
6593         (setq data (cdr data))))
6594     (unless found
6595       ;; If there is no data, that means that we were after the last
6596       ;; article.  The same goes when we can't find any articles
6597       ;; after the current one.
6598       (goto-char (point-max))
6599       (gnus-summary-find-prev))
6600     (gnus-set-mode-line 'summary)
6601     ;; We return how many articles were removed from the summary
6602     ;; buffer as a result of the new limit.
6603     (- total (length gnus-newsgroup-data))))
6604
6605 (defsubst gnus-invisible-cut-children (threads)
6606   (let ((num 0))
6607     (while threads
6608       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6609         (incf num))
6610       (pop threads))
6611     (< num 2)))
6612
6613 (defsubst gnus-cut-thread (thread)
6614   "Go forwards in the thread until we find an article that we want to display."
6615   (when (or (eq gnus-fetch-old-headers 'some)
6616             (eq gnus-fetch-old-headers 'invisible)
6617             (numberp gnus-fetch-old-headers)
6618             (eq gnus-build-sparse-threads 'some)
6619             (eq gnus-build-sparse-threads 'more))
6620     ;; Deal with old-fetched headers and sparse threads.
6621     (while (and
6622             thread
6623             (or
6624              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6625              (gnus-summary-article-ancient-p
6626               (mail-header-number (car thread))))
6627             (if (or (<= (length (cdr thread)) 1)
6628                     (eq gnus-fetch-old-headers 'invisible))
6629                 (setq gnus-newsgroup-limit
6630                       (delq (mail-header-number (car thread))
6631                             gnus-newsgroup-limit)
6632                       thread (cadr thread))
6633               (when (gnus-invisible-cut-children (cdr thread))
6634                 (let ((th (cdr thread)))
6635                   (while th
6636                     (if (memq (mail-header-number (caar th))
6637                               gnus-newsgroup-limit)
6638                         (setq thread (car th)
6639                               th nil)
6640                       (setq th (cdr th))))))))))
6641   thread)
6642
6643 (defun gnus-cut-threads (threads)
6644   "Cut off all uninteresting articles from the beginning of threads."
6645   (when (or (eq gnus-fetch-old-headers 'some)
6646             (eq gnus-fetch-old-headers 'invisible)
6647             (numberp gnus-fetch-old-headers)
6648             (eq gnus-build-sparse-threads 'some)
6649             (eq gnus-build-sparse-threads 'more))
6650     (let ((th threads))
6651       (while th
6652         (setcar th (gnus-cut-thread (car th)))
6653         (setq th (cdr th)))))
6654   ;; Remove nixed out threads.
6655   (delq nil threads))
6656
6657 (defun gnus-summary-initial-limit (&optional show-if-empty)
6658   "Figure out what the initial limit is supposed to be on group entry.
6659 This entails weeding out unwanted dormants, low-scored articles,
6660 fetch-old-headers verbiage, and so on."
6661   ;; Most groups have nothing to remove.
6662   (if (or gnus-inhibit-limiting
6663           (and (null gnus-newsgroup-dormant)
6664                (not (eq gnus-fetch-old-headers 'some))
6665                (not (numberp gnus-fetch-old-headers))
6666                (not (eq gnus-fetch-old-headers 'invisible))
6667                (null gnus-summary-expunge-below)
6668                (not (eq gnus-build-sparse-threads 'some))
6669                (not (eq gnus-build-sparse-threads 'more))
6670                (null gnus-thread-expunge-below)
6671                (not gnus-use-nocem)))
6672       ()                                ; Do nothing.
6673     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6674     (setq gnus-newsgroup-limit nil)
6675     (mapatoms
6676      (lambda (node)
6677        (unless (car (symbol-value node))
6678          ;; These threads have no parents -- they are roots.
6679          (let ((nodes (cdr (symbol-value node)))
6680                thread)
6681            (while nodes
6682              (if (and gnus-thread-expunge-below
6683                       (< (gnus-thread-total-score (car nodes))
6684                          gnus-thread-expunge-below))
6685                  (gnus-expunge-thread (pop nodes))
6686                (setq thread (pop nodes))
6687                (gnus-summary-limit-children thread))))))
6688      gnus-newsgroup-dependencies)
6689     ;; If this limitation resulted in an empty group, we might
6690     ;; pop the previous limit and use it instead.
6691     (when (and (not gnus-newsgroup-limit)
6692                show-if-empty)
6693       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6694     gnus-newsgroup-limit))
6695
6696 (defun gnus-summary-limit-children (thread)
6697   "Return 1 if this subthread is visible and 0 if it is not."
6698   ;; First we get the number of visible children to this thread.  This
6699   ;; is done by recursing down the thread using this function, so this
6700   ;; will really go down to a leaf article first, before slowly
6701   ;; working its way up towards the root.
6702   (when thread
6703     (let ((children
6704            (if (cdr thread)
6705                (apply '+ (mapcar 'gnus-summary-limit-children
6706                                  (cdr thread)))
6707              0))
6708           (number (mail-header-number (car thread)))
6709           score)
6710       (if (and
6711            (not (memq number gnus-newsgroup-marked))
6712            (or
6713             ;; If this article is dormant and has absolutely no visible
6714             ;; children, then this article isn't visible.
6715             (and (memq number gnus-newsgroup-dormant)
6716                  (zerop children))
6717             ;; If this is "fetch-old-headered" and there is no
6718             ;; visible children, then we don't want this article.
6719             (and (or (eq gnus-fetch-old-headers 'some)
6720                      (numberp gnus-fetch-old-headers))
6721                  (gnus-summary-article-ancient-p number)
6722                  (zerop children))
6723             ;; If this is "fetch-old-headered" and `invisible', then
6724             ;; we don't want this article.
6725             (and (eq gnus-fetch-old-headers 'invisible)
6726                  (gnus-summary-article-ancient-p number))
6727             ;; If this is a sparsely inserted article with no children,
6728             ;; we don't want it.
6729             (and (eq gnus-build-sparse-threads 'some)
6730                  (gnus-summary-article-sparse-p number)
6731                  (zerop children))
6732             ;; If we use expunging, and this article is really
6733             ;; low-scored, then we don't want this article.
6734             (when (and gnus-summary-expunge-below
6735                        (< (setq score
6736                                 (or (cdr (assq number gnus-newsgroup-scored))
6737                                     gnus-summary-default-score))
6738                           gnus-summary-expunge-below))
6739               ;; We increase the expunge-tally here, but that has
6740               ;; nothing to do with the limits, really.
6741               (incf gnus-newsgroup-expunged-tally)
6742               ;; We also mark as read here, if that's wanted.
6743               (when (and gnus-summary-mark-below
6744                          (< score gnus-summary-mark-below))
6745                 (setq gnus-newsgroup-unreads
6746                       (delq number gnus-newsgroup-unreads))
6747                 (if gnus-newsgroup-auto-expire
6748                     (push number gnus-newsgroup-expirable)
6749                   (push (cons number gnus-low-score-mark)
6750                         gnus-newsgroup-reads)))
6751               t)
6752             ;; Check NoCeM things.
6753             (if (and gnus-use-nocem
6754                      (gnus-nocem-unwanted-article-p
6755                       (mail-header-id (car thread))))
6756                 (progn
6757                   (setq gnus-newsgroup-unreads
6758                         (delq number gnus-newsgroup-unreads))
6759                   t))))
6760           ;; Nope, invisible article.
6761           0
6762         ;; Ok, this article is to be visible, so we add it to the limit
6763         ;; and return 1.
6764         (push number gnus-newsgroup-limit)
6765         1))))
6766
6767 (defun gnus-expunge-thread (thread)
6768   "Mark all articles in THREAD as read."
6769   (let* ((number (mail-header-number (car thread))))
6770     (incf gnus-newsgroup-expunged-tally)
6771     ;; We also mark as read here, if that's wanted.
6772     (setq gnus-newsgroup-unreads
6773           (delq number gnus-newsgroup-unreads))
6774     (if gnus-newsgroup-auto-expire
6775         (push number gnus-newsgroup-expirable)
6776       (push (cons number gnus-low-score-mark)
6777             gnus-newsgroup-reads)))
6778   ;; Go recursively through all subthreads.
6779   (mapcar 'gnus-expunge-thread (cdr thread)))
6780
6781 ;; Summary article oriented commands
6782
6783 (defun gnus-summary-refer-parent-article (n)
6784   "Refer parent article N times.
6785 If N is negative, go to ancestor -N instead.
6786 The difference between N and the number of articles fetched is returned."
6787   (interactive "p")
6788   (let ((skip 1)
6789         error header ref)
6790     (when (not (natnump n))
6791       (setq skip (abs n)
6792             n 1))
6793     (while (and (> n 0)
6794                 (not error))
6795       (setq header (gnus-summary-article-header))
6796       (if (and (eq (mail-header-number header)
6797                    (cdr gnus-article-current))
6798                (equal gnus-newsgroup-name
6799                       (car gnus-article-current)))
6800           ;; If we try to find the parent of the currently
6801           ;; displayed article, then we take a look at the actual
6802           ;; References header, since this is slightly more
6803           ;; reliable than the References field we got from the
6804           ;; server.
6805           (save-excursion
6806             (set-buffer gnus-original-article-buffer)
6807             (nnheader-narrow-to-headers)
6808             (unless (setq ref (message-fetch-field "references"))
6809               (setq ref (message-fetch-field "in-reply-to")))
6810             (widen))
6811         (setq ref
6812               ;; It's not the current article, so we take a bet on
6813               ;; the value we got from the server.
6814               (mail-header-references header)))
6815       (if (and ref
6816                (not (equal ref "")))
6817           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6818             (gnus-message 1 "Couldn't find parent"))
6819         (gnus-message 1 "No references in article %d"
6820                       (gnus-summary-article-number))
6821         (setq error t))
6822       (decf n))
6823     (gnus-summary-position-point)
6824     n))
6825
6826 (defun gnus-summary-refer-references ()
6827   "Fetch all articles mentioned in the References header.
6828 Return the number of articles fetched."
6829   (interactive)
6830   (let ((ref (mail-header-references (gnus-summary-article-header)))
6831         (current (gnus-summary-article-number))
6832         (n 0))
6833     (if (or (not ref)
6834             (equal ref ""))
6835         (error "No References in the current article")
6836       ;; For each Message-ID in the References header...
6837       (while (string-match "<[^>]*>" ref)
6838         (incf n)
6839         ;; ... fetch that article.
6840         (gnus-summary-refer-article
6841          (prog1 (match-string 0 ref)
6842            (setq ref (substring ref (match-end 0))))))
6843       (gnus-summary-goto-subject current)
6844       (gnus-summary-position-point)
6845       n)))
6846
6847 (defun gnus-summary-refer-thread (&optional limit)
6848   "Fetch all articles in the current thread.
6849 If LIMIT (the numerical prefix), fetch that many old headers instead
6850 of what's specified by the `gnus-refer-thread-limit' variable."
6851   (interactive "P")
6852   (let ((id (mail-header-id (gnus-summary-article-header)))
6853         (limit (if limit (prefix-numeric-value limit)
6854                  gnus-refer-thread-limit)))
6855     ;; We want to fetch LIMIT *old* headers, but we also have to
6856     ;; re-fetch all the headers in the current buffer, because many of
6857     ;; them may be undisplayed.  So we adjust LIMIT.
6858     (when (numberp limit)
6859       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6860     (unless (eq gnus-fetch-old-headers 'invisible)
6861       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6862       ;; Retrieve the headers and read them in.
6863       (if (eq (gnus-retrieve-headers
6864                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6865               'nov)
6866           (gnus-build-all-threads)
6867         (error "Can't fetch thread from backends that don't support NOV"))
6868       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6869     (gnus-summary-limit-include-thread id)))
6870
6871 (defun gnus-summary-refer-article (message-id)
6872   "Fetch an article specified by MESSAGE-ID."
6873   (interactive "sMessage-ID: ")
6874   (when (and (stringp message-id)
6875              (not (zerop (length message-id))))
6876     ;; Construct the correct Message-ID if necessary.
6877     ;; Suggested by tale@pawl.rpi.edu.
6878     (unless (string-match "^<" message-id)
6879       (setq message-id (concat "<" message-id)))
6880     (unless (string-match ">$" message-id)
6881       (setq message-id (concat message-id ">")))
6882     (let* ((header (gnus-id-to-header message-id))
6883            (sparse (and header
6884                         (gnus-summary-article-sparse-p
6885                          (mail-header-number header))
6886                         (memq (mail-header-number header)
6887                               gnus-newsgroup-limit)))
6888            number)
6889       (cond
6890        ;; If the article is present in the buffer we just go to it.
6891        ((and header
6892              (or (not (gnus-summary-article-sparse-p
6893                        (mail-header-number header)))
6894                  sparse))
6895         (prog1
6896             (gnus-summary-goto-article
6897              (mail-header-number header) nil t)
6898           (when sparse
6899             (gnus-summary-update-article (mail-header-number header)))))
6900        (t
6901         ;; We fetch the article.
6902         (catch 'found
6903           (dolist (gnus-override-method (gnus-refer-article-methods))
6904             (gnus-check-server gnus-override-method)
6905             ;; Fetch the header, and display the article.
6906             (when (setq number (gnus-summary-insert-subject message-id))
6907               (gnus-summary-select-article nil nil nil number)
6908               (throw 'found t)))
6909           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6910
6911 (defun gnus-refer-article-methods ()
6912   "Return a list of referrable methods."
6913   (cond
6914    ;; No method, so we default to current and native.
6915    ((null gnus-refer-article-method)
6916     (list gnus-current-select-method gnus-select-method))
6917    ;; Current.
6918    ((eq 'current gnus-refer-article-method)
6919     (list gnus-current-select-method))
6920    ;; List of select methods.
6921    ((not (stringp (cadr gnus-refer-article-method)))
6922     (let (out)
6923       (dolist (method gnus-refer-article-method)
6924         (push (if (eq 'current method)
6925                   gnus-current-select-method
6926                 method)
6927               out))
6928       (nreverse out)))
6929    ;; One single select method.
6930    (t
6931     (list gnus-refer-article-method))))
6932
6933 (defun gnus-summary-edit-parameters ()
6934   "Edit the group parameters of the current group."
6935   (interactive)
6936   (gnus-group-edit-group gnus-newsgroup-name 'params))
6937
6938 (defun gnus-summary-customize-parameters ()
6939   "Customize the group parameters of the current group."
6940   (interactive)
6941   (gnus-group-customize gnus-newsgroup-name))
6942
6943 (defun gnus-summary-enter-digest-group (&optional force)
6944   "Enter an nndoc group based on the current article.
6945 If FORCE, force a digest interpretation.  If not, try
6946 to guess what the document format is."
6947   (interactive "P")
6948   (let ((conf gnus-current-window-configuration))
6949     (save-excursion
6950       (gnus-summary-select-article))
6951     (setq gnus-current-window-configuration conf)
6952     (let* ((name (format "%s-%d"
6953                          (gnus-group-prefixed-name
6954                           gnus-newsgroup-name (list 'nndoc ""))
6955                          (save-excursion
6956                            (set-buffer gnus-summary-buffer)
6957                            gnus-current-article)))
6958            (ogroup gnus-newsgroup-name)
6959            (params (append (gnus-info-params (gnus-get-info ogroup))
6960                            (list (cons 'to-group ogroup))
6961                            (list (cons 'save-article-group ogroup))))
6962            (case-fold-search t)
6963            (buf (current-buffer))
6964            dig to-address)
6965       (save-excursion
6966         (set-buffer gnus-original-article-buffer)
6967         ;; Have the digest group inherit the main mail address of
6968         ;; the parent article.
6969         (when (setq to-address (or (message-fetch-field "reply-to")
6970                                    (message-fetch-field "from")))
6971           (setq params (append 
6972                         (list (cons 'to-address 
6973                                     (funcall gnus-decode-encoded-word-function
6974                                              to-address))))))
6975         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6976         (insert-buffer-substring gnus-original-article-buffer)
6977         ;; Remove lines that may lead nndoc to misinterpret the
6978         ;; document type.
6979         (narrow-to-region
6980          (goto-char (point-min))
6981          (or (search-forward "\n\n" nil t) (point)))
6982         (goto-char (point-min))
6983         (delete-matching-lines "^Path:\\|^From ")
6984         (widen))
6985       (unwind-protect
6986           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6987                     (gnus-newsgroup-ephemeral-ignored-charsets
6988                      gnus-newsgroup-ignored-charsets))
6989                 (gnus-group-read-ephemeral-group
6990                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6991                               (nndoc-article-type
6992                                ,(if force 'mbox 'guess))) t))
6993               ;; Make all postings to this group go to the parent group.
6994               (nconc (gnus-info-params (gnus-get-info name))
6995                      params)
6996             ;; Couldn't select this doc group.
6997             (switch-to-buffer buf)
6998             (gnus-set-global-variables)
6999             (gnus-configure-windows 'summary)
7000             (gnus-message 3 "Article couldn't be entered?"))
7001         (kill-buffer dig)))))
7002
7003 (defun gnus-summary-read-document (n)
7004   "Open a new group based on the current article(s).
7005 This will allow you to read digests and other similar
7006 documents as newsgroups.
7007 Obeys the standard process/prefix convention."
7008   (interactive "P")
7009   (let* ((articles (gnus-summary-work-articles n))
7010          (ogroup gnus-newsgroup-name)
7011          (params (append (gnus-info-params (gnus-get-info ogroup))
7012                          (list (cons 'to-group ogroup))))
7013          article group egroup groups vgroup)
7014     (while (setq article (pop articles))
7015       (setq group (format "%s-%d" gnus-newsgroup-name article))
7016       (gnus-summary-remove-process-mark article)
7017       (when (gnus-summary-display-article article)
7018         (save-excursion
7019           (with-temp-buffer
7020             (insert-buffer-substring gnus-original-article-buffer)
7021             ;; Remove some headers that may lead nndoc to make
7022             ;; the wrong guess.
7023             (message-narrow-to-head)
7024             (goto-char (point-min))
7025             (delete-matching-lines "^\\(Path\\):\\|^From ")
7026             (widen)
7027             (if (setq egroup
7028                       (gnus-group-read-ephemeral-group
7029                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7030                                      (nndoc-article-type guess))
7031                        t nil t))
7032                 (progn
7033                   ;; Make all postings to this group go to the parent group.
7034                   (nconc (gnus-info-params (gnus-get-info egroup))
7035                          params)
7036                   (push egroup groups))
7037               ;; Couldn't select this doc group.
7038               (gnus-error 3 "Article couldn't be entered"))))))
7039     ;; Now we have selected all the documents.
7040     (cond
7041      ((not groups)
7042       (error "None of the articles could be interpreted as documents"))
7043      ((gnus-group-read-ephemeral-group
7044        (setq vgroup (format
7045                      "nnvirtual:%s-%s" gnus-newsgroup-name
7046                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7047        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7048        t
7049        (cons (current-buffer) 'summary)))
7050      (t
7051       (error "Couldn't select virtual nndoc group")))))
7052
7053 (defun gnus-summary-isearch-article (&optional regexp-p)
7054   "Do incremental search forward on the current article.
7055 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7056   (interactive "P")
7057   (let* ((gnus-inhibit-treatment t)
7058          (old (gnus-summary-select-article)))
7059     (gnus-configure-windows 'article)
7060     (gnus-eval-in-buffer-window gnus-article-buffer
7061       (save-restriction
7062         (widen)
7063         (when (eq 'old old)
7064           (gnus-article-show-all-headers))
7065         (goto-char (point-min))
7066         (isearch-forward regexp-p)))))
7067
7068 (defun gnus-summary-search-article-forward (regexp &optional backward)
7069   "Search for an article containing REGEXP forward.
7070 If BACKWARD, search backward instead."
7071   (interactive
7072    (list (read-string
7073           (format "Search article %s (regexp%s): "
7074                   (if current-prefix-arg "backward" "forward")
7075                   (if gnus-last-search-regexp
7076                       (concat ", default " gnus-last-search-regexp)
7077                     "")))
7078          current-prefix-arg))
7079   (if (string-equal regexp "")
7080       (setq regexp (or gnus-last-search-regexp ""))
7081     (setq gnus-last-search-regexp regexp))
7082   (if (gnus-summary-search-article regexp backward)
7083       (gnus-summary-show-thread)
7084     (error "Search failed: \"%s\"" regexp)))
7085
7086 (defun gnus-summary-search-article-backward (regexp)
7087   "Search for an article containing REGEXP backward."
7088   (interactive
7089    (list (read-string
7090           (format "Search article backward (regexp%s): "
7091                   (if gnus-last-search-regexp
7092                       (concat ", default " gnus-last-search-regexp)
7093                     "")))))
7094   (gnus-summary-search-article-forward regexp 'backward))
7095
7096 (eval-when-compile
7097   (defmacro gnus-summary-search-article-position-point (regexp backward)
7098     "Dehighlight the last matched text and goto the beginning position."
7099     (` (if (and gnus-summary-search-article-matched-data
7100                 (let ((text (caddr gnus-summary-search-article-matched-data))
7101                       (inhibit-read-only t)
7102                       buffer-read-only)
7103                   (delete-region
7104                    (goto-char (car gnus-summary-search-article-matched-data))
7105                    (cadr gnus-summary-search-article-matched-data))
7106                   (insert text)
7107                   (string-match (, regexp) text)))
7108            (if (, backward) (beginning-of-line) (end-of-line))
7109          (goto-char (if (, backward) (point-max) (point-min))))))
7110
7111   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7112     "Place point where X-Face image is displayed."
7113     (if (featurep 'xemacs)
7114         (` (let ((end (if (search-forward "\n\n" nil t)
7115                           (goto-char (1- (point)))
7116                         (point-min)))
7117                  extent)
7118              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7119              (unless (and (re-search-forward "^From:" end t)
7120                           (setq extent (extent-at (point)))
7121                           (extent-begin-glyph extent))
7122                (goto-char (, opoint)))))
7123       (` (let ((end (if (search-forward "\n\n" nil t)
7124                         (goto-char (1- (point)))
7125                       (point-min))))
7126            (goto-char
7127             (or (text-property-any (or (search-backward "\n\n" nil t)
7128                                        (point-min))
7129                                    end 'x-face-mule-bitmap-image t)
7130                 (, opoint)))))))
7131
7132   (defmacro gnus-summary-search-article-highlight-matched-text
7133     (backward treated x-face)
7134     "Highlight matched text in the function `gnus-summary-search-article'."
7135     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7136              (end (set-marker (make-marker) (match-end 0)))
7137              (inhibit-read-only t)
7138              buffer-read-only)
7139          (unless treated
7140            (let ((,@
7141                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7142                     (mapcar
7143                      (lambda (item) (setq items (delq item items)))
7144                      '(gnus-treat-buttonize
7145                        gnus-treat-fill-article
7146                        gnus-treat-fill-long-lines
7147                        gnus-treat-emphasize
7148                        gnus-treat-highlight-headers
7149                        gnus-treat-highlight-citation
7150                        gnus-treat-highlight-signature
7151                        gnus-treat-overstrike
7152                        gnus-treat-display-xface
7153                        gnus-treat-buttonize-head
7154                        gnus-treat-decode-article-as-default-mime-charset))
7155                     (static-if (featurep 'xemacs)
7156                         items
7157                       (cons '(x-face-mule-delete-x-face-field
7158                               (quote never))
7159                             items))))
7160                  (gnus-treat-display-xface
7161                   (when (, x-face) gnus-treat-display-xface)))
7162              (gnus-article-prepare-mime-display)))
7163          (goto-char (if (, backward) start end))
7164          (when (, x-face)
7165            (gnus-summary-search-article-highlight-goto-x-face (point)))
7166          (setq gnus-summary-search-article-matched-data
7167                (list start end (buffer-substring start end)))
7168          (unless (eq start end);; matched text has been deleted. :-<
7169            (put-text-property start end 'face
7170                               (or (find-face 'isearch)
7171                                   'secondary-selection))))))
7172   )
7173
7174 (defun gnus-summary-search-article (regexp &optional backward)
7175   "Search for an article containing REGEXP.
7176 Optional argument BACKWARD means do search for backward.
7177 `gnus-select-article-hook' is not called during the search."
7178   ;; We have to require this here to make sure that the following
7179   ;; dynamic binding isn't shadowed by autoloading.
7180   (require 'gnus-async)
7181   (require 'gnus-art)
7182   (let ((gnus-select-article-hook nil)  ;Disable hook.
7183         (gnus-article-display-hook nil)
7184         (gnus-article-prepare-hook nil)
7185         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7186         (gnus-use-article-prefetch nil)
7187         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7188         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7189         (sum (current-buffer))
7190         (found nil)
7191         point treated)
7192     (gnus-save-hidden-threads
7193       (static-if (featurep 'xemacs)
7194           (let ((gnus-inhibit-treatment t))
7195             (setq treated (eq 'old (gnus-summary-select-article)))
7196             (when (and treated
7197                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7198                                  (window-live-p (get-buffer-window
7199                                                  gnus-article-buffer t)))))
7200               (gnus-summary-select-article nil t)
7201               (setq treated nil)))
7202         (let ((gnus-inhibit-treatment t)
7203               (x-face-mule-delete-x-face-field 'never))
7204           (setq treated (eq 'old (gnus-summary-select-article)))
7205           (when (and treated
7206                      (not
7207                       (and (gnus-buffer-live-p gnus-article-buffer)
7208                            (window-live-p (get-buffer-window
7209                                            gnus-article-buffer t))
7210                            (or (not (string-match "^\\^X-Face:" regexp))
7211                                (with-current-buffer gnus-article-buffer
7212                                  gnus-summary-search-article-matched-data)))))
7213             (gnus-summary-select-article nil t)
7214             (setq treated nil))))
7215       (set-buffer gnus-article-buffer)
7216       (widen)
7217       (if treated
7218           (progn
7219             (gnus-article-show-all-headers)
7220             (gnus-summary-search-article-position-point regexp backward))
7221         (goto-char (if backward (point-max) (point-min))))
7222       (while (not found)
7223         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7224         (if (if backward
7225                 (re-search-backward regexp nil t)
7226               (re-search-forward regexp nil t))
7227             ;; We found the regexp.
7228             (progn
7229               (gnus-summary-search-article-highlight-matched-text
7230                backward treated (string-match "^\\^X-Face:" regexp))
7231               (setq found 'found)
7232               (forward-line
7233                (/ (- 2 (window-height
7234                         (get-buffer-window gnus-article-buffer t)))
7235                   2))
7236               (set-window-start
7237                (get-buffer-window (current-buffer))
7238                (point))
7239               (set-buffer sum)
7240               (setq point (point)))
7241           ;; We didn't find it, so we go to the next article.
7242           (set-buffer sum)
7243           (setq found 'not)
7244           (while (eq found 'not)
7245             (if (not (if backward (gnus-summary-find-prev)
7246                        (gnus-summary-find-next)))
7247                 ;; No more articles.
7248                 (setq found t)
7249               ;; Select the next article and adjust point.
7250               (unless (gnus-summary-article-sparse-p
7251                        (gnus-summary-article-number))
7252                 (setq found nil)
7253                 (let ((gnus-inhibit-treatment t))
7254                   (gnus-summary-select-article))
7255                 (setq treated nil)
7256                 (set-buffer gnus-article-buffer)
7257                 (widen)
7258                 (goto-char (if backward (point-max) (point-min))))))))
7259       (gnus-message 7 ""))
7260     ;; Return whether we found the regexp.
7261     (when (eq found 'found)
7262       (goto-char point)
7263       (gnus-summary-show-thread)
7264       (gnus-summary-goto-subject gnus-current-article)
7265       (gnus-summary-position-point)
7266       t)))
7267
7268 (defun gnus-summary-find-matching (header regexp &optional backward unread
7269                                           not-case-fold)
7270   "Return a list of all articles that match REGEXP on HEADER.
7271 The search stars on the current article and goes forwards unless
7272 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7273 If UNREAD is non-nil, only unread articles will
7274 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7275 in the comparisons."
7276   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7277                 (gnus-data-find-list
7278                  (gnus-summary-article-number) (gnus-data-list backward))))
7279         (case-fold-search (not not-case-fold))
7280         articles d func)
7281     (if (consp header)
7282         (if (eq (car header) 'extra)
7283             (setq func
7284                   `(lambda (h)
7285                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7286                          "")))
7287           (error "%s is an invalid header" header))
7288       (unless (fboundp (intern (concat "mail-header-" header)))
7289         (error "%s is not a valid header" header))
7290       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7291     (while data
7292       (setq d (car data))
7293       (and (or (not unread)             ; We want all articles...
7294                (gnus-data-unread-p d))  ; Or just unreads.
7295            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7296            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7297            (push (gnus-data-number d) articles)) ; Success!
7298       (setq data (cdr data)))
7299     (nreverse articles)))
7300
7301 (defun gnus-summary-execute-command (header regexp command &optional backward)
7302   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7303 If HEADER is an empty string (or nil), the match is done on the entire
7304 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7305   (interactive
7306    (list (let ((completion-ignore-case t))
7307            (completing-read
7308             "Header name: "
7309             (mapcar (lambda (string) (list string))
7310                     '("Number" "Subject" "From" "Lines" "Date"
7311                       "Message-ID" "Xref" "References" "Body"))
7312             nil 'require-match))
7313          (read-string "Regexp: ")
7314          (read-key-sequence "Command: ")
7315          current-prefix-arg))
7316   (when (equal header "Body")
7317     (setq header ""))
7318   ;; Hidden thread subtrees must be searched as well.
7319   (gnus-summary-show-all-threads)
7320   ;; We don't want to change current point nor window configuration.
7321   (save-excursion
7322     (save-window-excursion
7323       (gnus-message 6 "Executing %s..." (key-description command))
7324       ;; We'd like to execute COMMAND interactively so as to give arguments.
7325       (gnus-execute header regexp
7326                     `(call-interactively ',(key-binding command))
7327                     backward)
7328       (gnus-message 6 "Executing %s...done" (key-description command)))))
7329
7330 (defun gnus-summary-beginning-of-article ()
7331   "Scroll the article back to the beginning."
7332   (interactive)
7333   (gnus-summary-select-article)
7334   (gnus-configure-windows 'article)
7335   (gnus-eval-in-buffer-window gnus-article-buffer
7336     (widen)
7337     (goto-char (point-min))
7338     (when gnus-page-broken
7339       (gnus-narrow-to-page))))
7340
7341 (defun gnus-summary-end-of-article ()
7342   "Scroll to the end of the article."
7343   (interactive)
7344   (gnus-summary-select-article)
7345   (gnus-configure-windows 'article)
7346   (gnus-eval-in-buffer-window gnus-article-buffer
7347     (widen)
7348     (goto-char (point-max))
7349     (recenter -3)
7350     (when gnus-page-broken
7351       (gnus-narrow-to-page))))
7352
7353 (defun gnus-summary-print-article (&optional filename n)
7354   "Generate and print a PostScript image of the N next (mail) articles.
7355
7356 If N is negative, print the N previous articles.  If N is nil and articles
7357 have been marked with the process mark, print these instead.
7358
7359 If the optional first argument FILENAME is nil, send the image to the
7360 printer.  If FILENAME is a string, save the PostScript image in a file with
7361 that name.  If FILENAME is a number, prompt the user for the name of the file
7362 to save in."
7363   (interactive (list (ps-print-preprint current-prefix-arg)
7364                      current-prefix-arg))
7365   (dolist (article (gnus-summary-work-articles n))
7366     (gnus-summary-select-article nil nil 'pseudo article)
7367     (gnus-eval-in-buffer-window gnus-article-buffer
7368       (let ((buffer (generate-new-buffer " *print*")))
7369         (unwind-protect
7370             (progn
7371               (copy-to-buffer buffer (point-min) (point-max))
7372               (set-buffer buffer)
7373               (gnus-article-delete-invisible-text)
7374               (let ((ps-left-header
7375                      (list
7376                       (concat "("
7377                               (mail-header-subject gnus-current-headers) ")")
7378                       (concat "("
7379                               (mail-header-from gnus-current-headers) ")")))
7380                     (ps-right-header
7381                      (list
7382                       "/pagenumberstring load"
7383                       (concat "("
7384                               (mail-header-date gnus-current-headers) ")"))))
7385                 (gnus-run-hooks 'gnus-ps-print-hook)
7386                 (save-excursion
7387                   (ps-print-buffer-with-faces filename))))
7388           (kill-buffer buffer))))))
7389
7390 (defun gnus-summary-show-article (&optional arg)
7391   "Force re-fetching of the current article.
7392 If ARG (the prefix) is a number, show the article with the charset
7393 defined in `gnus-summary-show-article-charset-alist', or the charset
7394 inputed.
7395 If ARG (the prefix) is non-nil and not a number, show the raw article
7396 without any article massaging functions being run."
7397   (interactive "P")
7398   (cond
7399    ((numberp arg)
7400     (let ((gnus-newsgroup-charset
7401            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7402                (read-coding-system "Charset: ")))
7403           (gnus-newsgroup-ignored-charsets 'gnus-all))
7404       (gnus-summary-select-article nil 'force)))
7405    ((not arg)
7406     ;; Select the article the normal way.
7407     (gnus-summary-select-article nil 'force))
7408    (t
7409     ;; We have to require this here to make sure that the following
7410     ;; dynamic binding isn't shadowed by autoloading.
7411     (require 'gnus-async)
7412     (require 'gnus-art)
7413     ;; Bind the article treatment functions to nil.
7414     (let ((gnus-have-all-headers t)
7415           gnus-article-display-hook
7416           gnus-article-prepare-hook
7417           gnus-article-decode-hook
7418           gnus-break-pages
7419           gnus-show-mime)
7420       (gnus-summary-select-article nil 'force))))
7421   (gnus-summary-goto-subject gnus-current-article)
7422   (gnus-summary-position-point))
7423
7424 (defun gnus-summary-verbose-headers (&optional arg)
7425   "Toggle permanent full header display.
7426 If ARG is a positive number, turn header display on.
7427 If ARG is a negative number, turn header display off."
7428   (interactive "P")
7429   (setq gnus-show-all-headers
7430         (cond ((or (not (numberp arg))
7431                    (zerop arg))
7432                (not gnus-show-all-headers))
7433               ((natnump arg)
7434                t)))
7435   (gnus-summary-show-article))
7436
7437 (defun gnus-summary-toggle-header (&optional arg)
7438   "Show the headers if they are hidden, or hide them if they are shown.
7439 If ARG is a positive number, show the entire header.
7440 If ARG is a negative number, hide the unwanted header lines."
7441   (interactive "P")
7442   (save-excursion
7443     (set-buffer gnus-article-buffer)
7444     (save-restriction
7445       (let* ((buffer-read-only nil)
7446              (inhibit-point-motion-hooks t)
7447              hidden e)
7448         (setq hidden
7449               (if (numberp arg)
7450                   (>= arg 0)
7451                 (save-restriction
7452                   (article-narrow-to-head)
7453                   (gnus-article-hidden-text-p 'headers))))
7454         (goto-char (point-min))
7455         (when (search-forward "\n\n" nil t)
7456           (delete-region (point-min) (1- (point))))
7457         (goto-char (point-min))
7458         (save-excursion
7459           (set-buffer gnus-original-article-buffer)
7460           (goto-char (point-min))
7461           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7462         (insert-buffer-substring gnus-original-article-buffer 1 e)
7463         (save-restriction
7464           (narrow-to-region (point-min) (point))
7465           (article-decode-encoded-words)
7466           (if  hidden
7467               (let ((gnus-treat-hide-headers nil)
7468                     (gnus-treat-hide-boring-headers nil))
7469                 (setq gnus-article-wash-types
7470                       (delq 'headers gnus-article-wash-types))
7471                 (gnus-treat-article 'head))
7472             (gnus-treat-article 'head)))
7473         (gnus-set-mode-line 'article)))))
7474
7475 (defun gnus-summary-show-all-headers ()
7476   "Make all header lines visible."
7477   (interactive)
7478   (gnus-article-show-all-headers))
7479
7480 (defun gnus-summary-toggle-mime (&optional arg)
7481   "Toggle MIME processing.
7482 If ARG is a positive number, turn MIME processing on."
7483   (interactive "P")
7484   (setq gnus-show-mime
7485         (if (null arg)
7486             (not gnus-show-mime)
7487           (> (prefix-numeric-value arg) 0)))
7488   (gnus-summary-select-article t 'force))
7489
7490 (defun gnus-summary-caesar-message (&optional arg)
7491   "Caesar rotate the current article by 13.
7492 The numerical prefix specifies how many places to rotate each letter
7493 forward."
7494   (interactive "P")
7495   (gnus-summary-select-article)
7496   (let ((mail-header-separator ""))
7497     (gnus-eval-in-buffer-window gnus-article-buffer
7498       (save-restriction
7499         (widen)
7500         (let ((start (window-start))
7501               buffer-read-only)
7502           (message-caesar-buffer-body arg)
7503           (set-window-start (get-buffer-window (current-buffer)) start))))))
7504
7505 (defun gnus-summary-stop-page-breaking ()
7506   "Stop page breaking in the current article."
7507   (interactive)
7508   (gnus-summary-select-article)
7509   (gnus-eval-in-buffer-window gnus-article-buffer
7510     (widen)
7511     (when (gnus-visual-p 'page-marker)
7512       (let ((buffer-read-only nil))
7513         (gnus-remove-text-with-property 'gnus-prev)
7514         (gnus-remove-text-with-property 'gnus-next))
7515       (setq gnus-page-broken nil))))
7516
7517 (defun gnus-summary-move-article (&optional n to-newsgroup
7518                                             select-method action)
7519   "Move the current article to a different newsgroup.
7520 If N is a positive number, move the N next articles.
7521 If N is a negative number, move the N previous articles.
7522 If N is nil and any articles have been marked with the process mark,
7523 move those articles instead.
7524 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7525 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7526 re-spool using this method.
7527
7528 For this function to work, both the current newsgroup and the
7529 newsgroup that you want to move to have to support the `request-move'
7530 and `request-accept' functions.
7531
7532 ACTION can be either `move' (the default), `crosspost' or `copy'."
7533   (interactive "P")
7534   (unless action
7535     (setq action 'move))
7536   ;; Disable marking as read.
7537   (let (gnus-mark-article-hook)
7538     (save-window-excursion
7539       (gnus-summary-select-article)))
7540   ;; Check whether the source group supports the required functions.
7541   (cond ((and (eq action 'move)
7542               (not (gnus-check-backend-function
7543                     'request-move-article gnus-newsgroup-name)))
7544          (error "The current group does not support article moving"))
7545         ((and (eq action 'crosspost)
7546               (not (gnus-check-backend-function
7547                     'request-replace-article gnus-newsgroup-name)))
7548          (error "The current group does not support article editing")))
7549   (let ((articles (gnus-summary-work-articles n))
7550         (prefix (if (gnus-check-backend-function
7551                     'request-move-article gnus-newsgroup-name)
7552                     (gnus-group-real-prefix gnus-newsgroup-name)
7553                   ""))
7554         (names '((move "Move" "Moving")
7555                  (copy "Copy" "Copying")
7556                  (crosspost "Crosspost" "Crossposting")))
7557         (copy-buf (save-excursion
7558                     (nnheader-set-temp-buffer " *copy article*")))
7559         (default-marks gnus-article-mark-lists)
7560         (no-expire-marks (delete '(expirable . expire)
7561                                  (copy-sequence gnus-article-mark-lists)))
7562         art-group to-method new-xref article to-groups)
7563     (unless (assq action names)
7564       (error "Unknown action %s" action))
7565     ;; Read the newsgroup name.
7566     (when (and (not to-newsgroup)
7567                (not select-method))
7568       (setq to-newsgroup
7569             (gnus-read-move-group-name
7570              (cadr (assq action names))
7571              (symbol-value (intern (format "gnus-current-%s-group" action)))
7572              articles prefix))
7573       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7574     (setq to-method (or select-method
7575                         (gnus-server-to-method
7576                          (gnus-group-method to-newsgroup))))
7577     ;; Check the method we are to move this article to...
7578     (unless (gnus-check-backend-function
7579              'request-accept-article (car to-method))
7580       (error "%s does not support article copying" (car to-method)))
7581     (unless (gnus-check-server to-method)
7582       (error "Can't open server %s" (car to-method)))
7583     (gnus-message 6 "%s to %s: %s..."
7584                   (caddr (assq action names))
7585                   (or (car select-method) to-newsgroup) articles)
7586     (while articles
7587       (setq article (pop articles))
7588       (setq
7589        art-group
7590        (cond
7591         ;; Move the article.
7592         ((eq action 'move)
7593          ;; Remove this article from future suppression.
7594          (gnus-dup-unsuppress-article article)
7595          (gnus-request-move-article
7596           article                       ; Article to move
7597           gnus-newsgroup-name           ; From newsgroup
7598           (nth 1 (gnus-find-method-for-group
7599                   gnus-newsgroup-name)) ; Server
7600           (list 'gnus-request-accept-article
7601                 to-newsgroup (list 'quote select-method)
7602                 (not articles) t)       ; Accept form
7603           (not articles)))              ; Only save nov last time
7604         ;; Copy the article.
7605         ((eq action 'copy)
7606          (save-excursion
7607            (set-buffer copy-buf)
7608            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7609              (gnus-request-accept-article
7610               to-newsgroup select-method (not articles) t))))
7611         ;; Crosspost the article.
7612         ((eq action 'crosspost)
7613          (let ((xref (message-tokenize-header
7614                       (mail-header-xref (gnus-summary-article-header article))
7615                       " ")))
7616            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7617                                   ":" article))
7618            (unless xref
7619              (setq xref (list (system-name))))
7620            (setq new-xref
7621                  (concat
7622                   (mapconcat 'identity
7623                              (delete "Xref:" (delete new-xref xref))
7624                              " ")
7625                   " " new-xref))
7626            (save-excursion
7627              (set-buffer copy-buf)
7628              ;; First put the article in the destination group.
7629              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7630              (when (consp (setq art-group
7631                                 (gnus-request-accept-article
7632                                  to-newsgroup select-method (not articles))))
7633                (setq new-xref (concat new-xref " " (car art-group)
7634                                       ":" (cdr art-group)))
7635                ;; Now we have the new Xrefs header, so we insert
7636                ;; it and replace the new article.
7637                (nnheader-replace-header "Xref" new-xref)
7638                (gnus-request-replace-article
7639                 (cdr art-group) to-newsgroup (current-buffer))
7640                art-group))))))
7641       (cond
7642        ((not art-group)
7643         (gnus-message 1 "Couldn't %s article %s: %s"
7644                       (cadr (assq action names)) article
7645                       (nnheader-get-report (car to-method))))
7646        ((eq art-group 'junk)
7647         (when (eq action 'move)
7648           (gnus-summary-mark-article article gnus-canceled-mark)
7649           (gnus-message 4 "Deleted article %s" article)))
7650        (t
7651         (let* ((pto-group (gnus-group-prefixed-name
7652                            (car art-group) to-method))
7653                (entry
7654                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7655                (info (nth 2 entry))
7656                (to-group (gnus-info-group info))
7657                to-marks)
7658           ;; Update the group that has been moved to.
7659           (when (and info
7660                      (memq action '(move copy)))
7661             (unless (member to-group to-groups)
7662               (push to-group to-groups))
7663
7664             (unless (memq article gnus-newsgroup-unreads)
7665               (push 'read to-marks)
7666               (gnus-info-set-read
7667                info (gnus-add-to-range (gnus-info-read info)
7668                                        (list (cdr art-group)))))
7669
7670             ;; See whether the article is to be put in the cache.
7671             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7672                              default-marks
7673                            no-expire-marks))
7674                   (to-article (cdr art-group)))
7675
7676               ;; Enter the article into the cache in the new group,
7677               ;; if that is required.
7678               (when gnus-use-cache
7679                 (gnus-cache-possibly-enter-article
7680                  to-group to-article
7681                  (let ((header (copy-sequence
7682                                 (gnus-summary-article-header article))))
7683                    (mail-header-set-number header to-article)
7684                    header)
7685                  (memq article gnus-newsgroup-marked)
7686                  (memq article gnus-newsgroup-dormant)
7687                  (memq article gnus-newsgroup-unreads)))
7688
7689               (when gnus-preserve-marks
7690                 ;; Copy any marks over to the new group.
7691                 (when (and (equal to-group gnus-newsgroup-name)
7692                            (not (memq article gnus-newsgroup-unreads)))
7693                   ;; Mark this article as read in this group.
7694                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7695                   (setcdr (gnus-active to-group) to-article)
7696                   (setcdr gnus-newsgroup-active to-article))
7697
7698                 (while marks
7699                   (when (memq article (symbol-value
7700                                        (intern (format "gnus-newsgroup-%s"
7701                                                        (caar marks)))))
7702                     (push (cdar marks) to-marks)
7703                     ;; If the other group is the same as this group,
7704                     ;; then we have to add the mark to the list.
7705                     (when (equal to-group gnus-newsgroup-name)
7706                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7707                            (cons to-article
7708                                  (symbol-value
7709                                   (intern (format "gnus-newsgroup-%s"
7710                                                   (caar marks)))))))
7711                     ;; Copy the marks to other group.
7712                     (gnus-add-marked-articles
7713                      to-group (cdar marks) (list to-article) info))
7714                   (setq marks (cdr marks)))
7715
7716                 (gnus-request-set-mark to-group (list (list (list to-article)
7717                                                             'set
7718                                                             to-marks))))
7719
7720               (gnus-dribble-enter
7721                (concat "(gnus-group-set-info '"
7722                        (gnus-prin1-to-string (gnus-get-info to-group))
7723                        ")"))))
7724
7725           ;; Update the Xref header in this article to point to
7726           ;; the new crossposted article we have just created.
7727           (when (eq action 'crosspost)
7728             (save-excursion
7729               (set-buffer copy-buf)
7730               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7731               (nnheader-replace-header "Xref" new-xref)
7732               (gnus-request-replace-article
7733                article gnus-newsgroup-name (current-buffer)))))
7734
7735         ;;;!!!Why is this necessary?
7736         (set-buffer gnus-summary-buffer)
7737
7738         (gnus-summary-goto-subject article)
7739         (when (eq action 'move)
7740           (gnus-summary-mark-article article gnus-canceled-mark))))
7741       (gnus-summary-remove-process-mark article))
7742     ;; Re-activate all groups that have been moved to.
7743     (while to-groups
7744       (save-excursion
7745         (set-buffer gnus-group-buffer)
7746         (when (gnus-group-goto-group (car to-groups) t)
7747           (gnus-group-get-new-news-this-group 1 t))
7748         (pop to-groups)))
7749
7750     (gnus-kill-buffer copy-buf)
7751     (gnus-summary-position-point)
7752     (gnus-set-mode-line 'summary)))
7753
7754 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7755   "Move the current article to a different newsgroup.
7756 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7757 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7758 re-spool using this method."
7759   (interactive "P")
7760   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7761
7762 (defun gnus-summary-crosspost-article (&optional n)
7763   "Crosspost the current article to some other group."
7764   (interactive "P")
7765   (gnus-summary-move-article n nil nil 'crosspost))
7766
7767 (defcustom gnus-summary-respool-default-method nil
7768   "Default method for respooling an article.
7769 If nil, use to the current newsgroup method."
7770   :type '(choice (gnus-select-method :value (nnml ""))
7771                  (const nil))
7772   :group 'gnus-summary-mail)
7773
7774 (defun gnus-summary-respool-article (&optional n method)
7775   "Respool the current article.
7776 The article will be squeezed through the mail spooling process again,
7777 which means that it will be put in some mail newsgroup or other
7778 depending on `nnmail-split-methods'.
7779 If N is a positive number, respool the N next articles.
7780 If N is a negative number, respool the N previous articles.
7781 If N is nil and any articles have been marked with the process mark,
7782 respool those articles instead.
7783
7784 Respooling can be done both from mail groups and \"real\" newsgroups.
7785 In the former case, the articles in question will be moved from the
7786 current group into whatever groups they are destined to.  In the
7787 latter case, they will be copied into the relevant groups."
7788   (interactive
7789    (list current-prefix-arg
7790          (let* ((methods (gnus-methods-using 'respool))
7791                 (methname
7792                  (symbol-name (or gnus-summary-respool-default-method
7793                                   (car (gnus-find-method-for-group
7794                                         gnus-newsgroup-name)))))
7795                 (method
7796                  (gnus-completing-read
7797                   methname "What backend do you want to use when respooling?"
7798                   methods nil t nil 'gnus-mail-method-history))
7799                 ms)
7800            (cond
7801             ((zerop (length (setq ms (gnus-servers-using-backend
7802                                       (intern method)))))
7803              (list (intern method) ""))
7804             ((= 1 (length ms))
7805              (car ms))
7806             (t
7807              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7808                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7809                            ms-alist))))))))
7810   (unless method
7811     (error "No method given for respooling"))
7812   (if (assoc (symbol-name
7813               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7814              (gnus-methods-using 'respool))
7815       (gnus-summary-move-article n nil method)
7816     (gnus-summary-copy-article n nil method)))
7817
7818 (defun gnus-summary-import-article (file)
7819   "Import an arbitrary file into a mail newsgroup."
7820   (interactive "fImport file: ")
7821   (let ((group gnus-newsgroup-name)
7822         (now (current-time))
7823         atts lines)
7824     (unless (gnus-check-backend-function 'request-accept-article group)
7825       (error "%s does not support article importing" group))
7826     (or (file-readable-p file)
7827         (not (file-regular-p file))
7828         (error "Can't read %s" file))
7829     (save-excursion
7830       (set-buffer (gnus-get-buffer-create " *import file*"))
7831       (erase-buffer)
7832       (nnheader-insert-file-contents file)
7833       (goto-char (point-min))
7834       (unless (nnheader-article-p)
7835         ;; This doesn't look like an article, so we fudge some headers.
7836         (setq atts (file-attributes file)
7837               lines (count-lines (point-min) (point-max)))
7838         (insert "From: " (read-string "From: ") "\n"
7839                 "Subject: " (read-string "Subject: ") "\n"
7840                 "Date: " (message-make-date (nth 5 atts))
7841                 "\n"
7842                 "Message-ID: " (message-make-message-id) "\n"
7843                 "Lines: " (int-to-string lines) "\n"
7844                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7845       (gnus-request-accept-article group nil t)
7846       (kill-buffer (current-buffer)))))
7847
7848 (defun gnus-summary-article-posted-p ()
7849   "Say whether the current (mail) article is available from news as well.
7850 This will be the case if the article has both been mailed and posted."
7851   (interactive)
7852   (let ((id (mail-header-references (gnus-summary-article-header)))
7853         (gnus-override-method (car (gnus-refer-article-methods))))
7854     (if (gnus-request-head id "")
7855         (gnus-message 2 "The current message was found on %s"
7856                       gnus-override-method)
7857       (gnus-message 2 "The current message couldn't be found on %s"
7858                     gnus-override-method)
7859       nil)))
7860
7861 (defun gnus-summary-expire-articles (&optional now)
7862   "Expire all articles that are marked as expirable in the current group."
7863   (interactive)
7864   (when (gnus-check-backend-function
7865          'request-expire-articles gnus-newsgroup-name)
7866     ;; This backend supports expiry.
7867     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7868            (expirable (if total
7869                           (progn
7870                             ;; We need to update the info for
7871                             ;; this group for `gnus-list-of-read-articles'
7872                             ;; to give us the right answer.
7873                             (gnus-run-hooks 'gnus-exit-group-hook)
7874                             (gnus-summary-update-info)
7875                             (gnus-list-of-read-articles gnus-newsgroup-name))
7876                         (setq gnus-newsgroup-expirable
7877                               (sort gnus-newsgroup-expirable '<))))
7878            (expiry-wait (if now 'immediate
7879                           (gnus-group-find-parameter
7880                            gnus-newsgroup-name 'expiry-wait)))
7881            (nnmail-expiry-target
7882             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7883                 nnmail-expiry-target))
7884            es)
7885       (when expirable
7886         ;; There are expirable articles in this group, so we run them
7887         ;; through the expiry process.
7888         (gnus-message 6 "Expiring articles...")
7889         (unless (gnus-check-group gnus-newsgroup-name)
7890           (error "Can't open server for %s" gnus-newsgroup-name))
7891         ;; The list of articles that weren't expired is returned.
7892         (save-excursion
7893           (if expiry-wait
7894               (let ((nnmail-expiry-wait-function nil)
7895                     (nnmail-expiry-wait expiry-wait))
7896                 (setq es (gnus-request-expire-articles
7897                           expirable gnus-newsgroup-name)))
7898             (setq es (gnus-request-expire-articles
7899                       expirable gnus-newsgroup-name))))
7900         (unless total
7901           (setq gnus-newsgroup-expirable es))
7902         ;; We go through the old list of expirable, and mark all
7903         ;; really expired articles as nonexistent.
7904         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7905           (let ((gnus-use-cache nil))
7906             (while expirable
7907               (unless (memq (car expirable) es)
7908                 (when (gnus-data-find (car expirable))
7909                   (gnus-summary-mark-article
7910                    (car expirable) gnus-canceled-mark)))
7911               (setq expirable (cdr expirable)))))
7912         (gnus-message 6 "Expiring articles...done")))))
7913
7914 (defun gnus-summary-expire-articles-now ()
7915   "Expunge all expirable articles in the current group.
7916 This means that *all* articles that are marked as expirable will be
7917 deleted forever, right now."
7918   (interactive)
7919   (or gnus-expert-user
7920       (gnus-yes-or-no-p
7921        "Are you really, really, really sure you want to delete all these messages? ")
7922       (error "Phew!"))
7923   (gnus-summary-expire-articles t))
7924
7925 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7926 (defun gnus-summary-delete-article (&optional n)
7927   "Delete the N next (mail) articles.
7928 This command actually deletes articles.  This is not a marking
7929 command.  The article will disappear forever from your life, never to
7930 return.
7931 If N is negative, delete backwards.
7932 If N is nil and articles have been marked with the process mark,
7933 delete these instead."
7934   (interactive "P")
7935   (unless (gnus-check-backend-function 'request-expire-articles
7936                                        gnus-newsgroup-name)
7937     (error "The current newsgroup does not support article deletion"))
7938   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7939     (error "Couldn't open server"))
7940   ;; Compute the list of articles to delete.
7941   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7942         not-deleted)
7943     (if (and gnus-novice-user
7944              (not (gnus-yes-or-no-p
7945                    (format "Do you really want to delete %s forever? "
7946                            (if (> (length articles) 1)
7947                                (format "these %s articles" (length articles))
7948                              "this article")))))
7949         ()
7950       ;; Delete the articles.
7951       (setq not-deleted (gnus-request-expire-articles
7952                          articles gnus-newsgroup-name 'force))
7953       (while articles
7954         (gnus-summary-remove-process-mark (car articles))
7955         ;; The backend might not have been able to delete the article
7956         ;; after all.
7957         (unless (memq (car articles) not-deleted)
7958           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7959         (setq articles (cdr articles)))
7960       (when not-deleted
7961         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7962     (gnus-summary-position-point)
7963     (gnus-set-mode-line 'summary)
7964     not-deleted))
7965
7966 (defun gnus-summary-edit-article (&optional force)
7967   "Edit the current article.
7968 This will have permanent effect only in mail groups.
7969 If FORCE is non-nil, allow editing of articles even in read-only
7970 groups."
7971   (interactive "P")
7972   (save-excursion
7973     (set-buffer gnus-summary-buffer)
7974     (let ((mail-parse-charset gnus-newsgroup-charset)
7975           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7976       (gnus-set-global-variables)
7977       (when (and (not force)
7978                  (gnus-group-read-only-p))
7979         (error "The current newsgroup does not support article editing"))
7980       (gnus-summary-show-article t)
7981       (gnus-article-edit-article
7982        'ignore
7983        `(lambda (no-highlight)
7984           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7985                 (mail-parse-ignored-charsets
7986                  ',gnus-newsgroup-ignored-charsets))
7987             (gnus-summary-edit-article-done
7988              ,(or (mail-header-references gnus-current-headers) "")
7989              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7990
7991 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7992
7993 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7994                                                  no-highlight)
7995   "Make edits to the current article permanent."
7996   (interactive)
7997   ;; Replace the article.
7998   (let ((buf (current-buffer)))
7999     (with-temp-buffer
8000       (insert-buffer-substring buf)
8001       (if (and (not read-only)
8002                (not (gnus-request-replace-article
8003                      (cdr gnus-article-current) (car gnus-article-current)
8004                      (current-buffer) t)))
8005           (error "Couldn't replace article")
8006         ;; Update the summary buffer.
8007         (if (and references
8008                  (equal (message-tokenize-header references " ")
8009                         (message-tokenize-header
8010                          (or (message-fetch-field "references") "") " ")))
8011             ;; We only have to update this line.
8012             (save-excursion
8013               (save-restriction
8014                 (message-narrow-to-head)
8015                 (let ((head (buffer-string))
8016                       header)
8017                   (with-temp-buffer
8018                     (insert (format "211 %d Article retrieved.\n"
8019                                     (cdr gnus-article-current)))
8020                     (insert head)
8021                     (insert ".\n")
8022                     (let ((nntp-server-buffer (current-buffer)))
8023                       (setq header (car (gnus-get-newsgroup-headers
8024                                          (save-excursion
8025                                            (set-buffer gnus-summary-buffer)
8026                                            gnus-newsgroup-dependencies)
8027                                          t))))
8028                     (save-excursion
8029                       (set-buffer gnus-summary-buffer)
8030                       (gnus-data-set-header
8031                        (gnus-data-find (cdr gnus-article-current))
8032                        header)
8033                       (gnus-summary-update-article-line
8034                        (cdr gnus-article-current) header))))))
8035           ;; Update threads.
8036           (set-buffer (or buffer gnus-summary-buffer))
8037           (gnus-summary-update-article (cdr gnus-article-current)))
8038         ;; Prettify the article buffer again.
8039         (unless no-highlight
8040           (save-excursion
8041             (set-buffer gnus-article-buffer)
8042             ;;;!!! Fix this -- article should be rehighlighted.
8043             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8044             (set-buffer gnus-original-article-buffer)
8045             (gnus-request-article
8046              (cdr gnus-article-current)
8047              (car gnus-article-current) (current-buffer))))
8048         ;; Prettify the summary buffer line.
8049         (when (gnus-visual-p 'summary-highlight 'highlight)
8050           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8051
8052 (defun gnus-summary-edit-wash (key)
8053   "Perform editing command KEY in the article buffer."
8054   (interactive
8055    (list
8056     (progn
8057       (message "%s" (concat (this-command-keys) "- "))
8058       (read-char))))
8059   (message "")
8060   (gnus-summary-edit-article)
8061   (execute-kbd-macro (concat (this-command-keys) key))
8062   (gnus-article-edit-done))
8063
8064 ;;; Respooling
8065
8066 (defun gnus-summary-respool-query (&optional silent trace)
8067   "Query where the respool algorithm would put this article."
8068   (interactive)
8069   (let (gnus-mark-article-hook)
8070     (gnus-summary-select-article)
8071     (save-excursion
8072       (set-buffer gnus-original-article-buffer)
8073       (save-restriction
8074         (message-narrow-to-head)
8075         (let ((groups (nnmail-article-group 'identity trace)))
8076           (unless silent
8077             (if groups
8078                 (message "This message would go to %s"
8079                          (mapconcat 'car groups ", "))
8080               (message "This message would go to no groups"))
8081             groups))))))
8082
8083 (defun gnus-summary-respool-trace ()
8084   "Trace where the respool algorithm would put this article.
8085 Display a buffer showing all fancy splitting patterns which matched."
8086   (interactive)
8087   (gnus-summary-respool-query nil t))
8088
8089 ;; Summary marking commands.
8090
8091 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8092   "Mark articles which has the same subject as read, and then select the next.
8093 If UNMARK is positive, remove any kind of mark.
8094 If UNMARK is negative, tick articles."
8095   (interactive "P")
8096   (when unmark
8097     (setq unmark (prefix-numeric-value unmark)))
8098   (let ((count
8099          (gnus-summary-mark-same-subject
8100           (gnus-summary-article-subject) unmark)))
8101     ;; Select next unread article.  If auto-select-same mode, should
8102     ;; select the first unread article.
8103     (gnus-summary-next-article t (and gnus-auto-select-same
8104                                       (gnus-summary-article-subject)))
8105     (gnus-message 7 "%d article%s marked as %s"
8106                   count (if (= count 1) " is" "s are")
8107                   (if unmark "unread" "read"))))
8108
8109 (defun gnus-summary-kill-same-subject (&optional unmark)
8110   "Mark articles which has the same subject as read.
8111 If UNMARK is positive, remove any kind of mark.
8112 If UNMARK is negative, tick articles."
8113   (interactive "P")
8114   (when unmark
8115     (setq unmark (prefix-numeric-value unmark)))
8116   (let ((count
8117          (gnus-summary-mark-same-subject
8118           (gnus-summary-article-subject) unmark)))
8119     ;; If marked as read, go to next unread subject.
8120     (when (null unmark)
8121       ;; Go to next unread subject.
8122       (gnus-summary-next-subject 1 t))
8123     (gnus-message 7 "%d articles are marked as %s"
8124                   count (if unmark "unread" "read"))))
8125
8126 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8127   "Mark articles with same SUBJECT as read, and return marked number.
8128 If optional argument UNMARK is positive, remove any kinds of marks.
8129 If optional argument UNMARK is negative, mark articles as unread instead."
8130   (let ((count 1))
8131     (save-excursion
8132       (cond
8133        ((null unmark)                   ; Mark as read.
8134         (while (and
8135                 (progn
8136                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8137                   (gnus-summary-show-thread) t)
8138                 (gnus-summary-find-subject subject))
8139           (setq count (1+ count))))
8140        ((> unmark 0)                    ; Tick.
8141         (while (and
8142                 (progn
8143                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8144                   (gnus-summary-show-thread) t)
8145                 (gnus-summary-find-subject subject))
8146           (setq count (1+ count))))
8147        (t                               ; Mark as unread.
8148         (while (and
8149                 (progn
8150                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8151                   (gnus-summary-show-thread) t)
8152                 (gnus-summary-find-subject subject))
8153           (setq count (1+ count)))))
8154       (gnus-set-mode-line 'summary)
8155       ;; Return the number of marked articles.
8156       count)))
8157
8158 (defun gnus-summary-mark-as-processable (n &optional unmark)
8159   "Set the process mark on the next N articles.
8160 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8161 the process mark instead.  The difference between N and the actual
8162 number of articles marked is returned."
8163   (interactive "p")
8164   (let ((backward (< n 0))
8165         (n (abs n)))
8166     (while (and
8167             (> n 0)
8168             (if unmark
8169                 (gnus-summary-remove-process-mark
8170                  (gnus-summary-article-number))
8171               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8172             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8173       (setq n (1- n)))
8174     (when (/= 0 n)
8175       (gnus-message 7 "No more articles"))
8176     (gnus-summary-recenter)
8177     (gnus-summary-position-point)
8178     n))
8179
8180 (defun gnus-summary-unmark-as-processable (n)
8181   "Remove the process mark from the next N articles.
8182 If N is negative, unmark backward instead.  The difference between N and
8183 the actual number of articles unmarked is returned."
8184   (interactive "p")
8185   (gnus-summary-mark-as-processable n t))
8186
8187 (defun gnus-summary-unmark-all-processable ()
8188   "Remove the process mark from all articles."
8189   (interactive)
8190   (save-excursion
8191     (while gnus-newsgroup-processable
8192       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8193   (gnus-summary-position-point))
8194
8195 (defun gnus-summary-mark-as-expirable (n)
8196   "Mark N articles forward as expirable.
8197 If N is negative, mark backward instead.  The difference between N and
8198 the actual number of articles marked is returned."
8199   (interactive "p")
8200   (gnus-summary-mark-forward n gnus-expirable-mark))
8201
8202 (defun gnus-summary-mark-article-as-replied (article)
8203   "Mark ARTICLE replied and update the summary line."
8204   (push article gnus-newsgroup-replied)
8205   (let ((buffer-read-only nil))
8206     (when (gnus-summary-goto-subject article nil t)
8207       (gnus-summary-update-secondary-mark article))))
8208
8209 (defun gnus-summary-set-bookmark (article)
8210   "Set a bookmark in current article."
8211   (interactive (list (gnus-summary-article-number)))
8212   (when (or (not (get-buffer gnus-article-buffer))
8213             (not gnus-current-article)
8214             (not gnus-article-current)
8215             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8216     (error "No current article selected"))
8217   ;; Remove old bookmark, if one exists.
8218   (let ((old (assq article gnus-newsgroup-bookmarks)))
8219     (when old
8220       (setq gnus-newsgroup-bookmarks
8221             (delq old gnus-newsgroup-bookmarks))))
8222   ;; Set the new bookmark, which is on the form
8223   ;; (article-number . line-number-in-body).
8224   (push
8225    (cons article
8226          (save-excursion
8227            (set-buffer gnus-article-buffer)
8228            (count-lines
8229             (min (point)
8230                  (save-excursion
8231                    (goto-char (point-min))
8232                    (search-forward "\n\n" nil t)
8233                    (point)))
8234             (point))))
8235    gnus-newsgroup-bookmarks)
8236   (gnus-message 6 "A bookmark has been added to the current article."))
8237
8238 (defun gnus-summary-remove-bookmark (article)
8239   "Remove the bookmark from the current article."
8240   (interactive (list (gnus-summary-article-number)))
8241   ;; Remove old bookmark, if one exists.
8242   (let ((old (assq article gnus-newsgroup-bookmarks)))
8243     (if old
8244         (progn
8245           (setq gnus-newsgroup-bookmarks
8246                 (delq old gnus-newsgroup-bookmarks))
8247           (gnus-message 6 "Removed bookmark."))
8248       (gnus-message 6 "No bookmark in current article."))))
8249
8250 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8251 (defun gnus-summary-mark-as-dormant (n)
8252   "Mark N articles forward as dormant.
8253 If N is negative, mark backward instead.  The difference between N and
8254 the actual number of articles marked is returned."
8255   (interactive "p")
8256   (gnus-summary-mark-forward n gnus-dormant-mark))
8257
8258 (defun gnus-summary-set-process-mark (article)
8259   "Set the process mark on ARTICLE and update the summary line."
8260   (setq gnus-newsgroup-processable
8261         (cons article
8262               (delq article gnus-newsgroup-processable)))
8263   (when (gnus-summary-goto-subject article)
8264     (gnus-summary-show-thread)
8265     (gnus-summary-goto-subject article)
8266     (gnus-summary-update-secondary-mark article)))
8267
8268 (defun gnus-summary-remove-process-mark (article)
8269   "Remove the process mark from ARTICLE and update the summary line."
8270   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8271   (when (gnus-summary-goto-subject article)
8272     (gnus-summary-show-thread)
8273     (gnus-summary-goto-subject article)
8274     (gnus-summary-update-secondary-mark article)))
8275
8276 (defun gnus-summary-set-saved-mark (article)
8277   "Set the process mark on ARTICLE and update the summary line."
8278   (push article gnus-newsgroup-saved)
8279   (when (gnus-summary-goto-subject article)
8280     (gnus-summary-update-secondary-mark article)))
8281
8282 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8283   "Mark N articles as read forwards.
8284 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8285 The difference between N and the actual number of articles marked is
8286 returned.
8287 Iff NO-EXPIRE, auto-expiry will be inhibited."
8288   (interactive "p")
8289   (gnus-summary-show-thread)
8290   (let ((backward (< n 0))
8291         (gnus-summary-goto-unread
8292          (and gnus-summary-goto-unread
8293               (not (eq gnus-summary-goto-unread 'never))
8294               (not (memq mark (list gnus-unread-mark
8295                                     gnus-ticked-mark gnus-dormant-mark)))))
8296         (n (abs n))
8297         (mark (or mark gnus-del-mark)))
8298     (while (and (> n 0)
8299                 (gnus-summary-mark-article nil mark no-expire)
8300                 (zerop (gnus-summary-next-subject
8301                         (if backward -1 1)
8302                         (and gnus-summary-goto-unread
8303                              (not (eq gnus-summary-goto-unread 'never)))
8304                         t)))
8305       (setq n (1- n)))
8306     (when (/= 0 n)
8307       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8308     (gnus-summary-recenter)
8309     (gnus-summary-position-point)
8310     (gnus-set-mode-line 'summary)
8311     n))
8312
8313 (defun gnus-summary-mark-article-as-read (mark)
8314   "Mark the current article quickly as read with MARK."
8315   (let ((article (gnus-summary-article-number)))
8316     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8317     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8318     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8319     (push (cons article mark) gnus-newsgroup-reads)
8320     ;; Possibly remove from cache, if that is used.
8321     (when gnus-use-cache
8322       (gnus-cache-enter-remove-article article))
8323     ;; Allow the backend to change the mark.
8324     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8325     ;; Check for auto-expiry.
8326     (when (and gnus-newsgroup-auto-expire
8327                (memq mark gnus-auto-expirable-marks))
8328       (setq mark gnus-expirable-mark)
8329       ;; Let the backend know about the mark change.
8330       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8331       (push article gnus-newsgroup-expirable))
8332     ;; Set the mark in the buffer.
8333     (gnus-summary-update-mark mark 'unread)
8334     t))
8335
8336 (defun gnus-summary-mark-article-as-unread (mark)
8337   "Mark the current article quickly as unread with MARK."
8338   (let* ((article (gnus-summary-article-number))
8339          (old-mark (gnus-summary-article-mark article)))
8340     ;; Allow the backend to change the mark.
8341     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8342     (if (eq mark old-mark)
8343         t
8344       (if (<= article 0)
8345           (progn
8346             (gnus-error 1 "Can't mark negative article numbers")
8347             nil)
8348         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8349         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8350         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8351         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8352         (cond ((= mark gnus-ticked-mark)
8353                (push article gnus-newsgroup-marked))
8354               ((= mark gnus-dormant-mark)
8355                (push article gnus-newsgroup-dormant))
8356               (t
8357                (push article gnus-newsgroup-unreads)))
8358         (gnus-pull article gnus-newsgroup-reads)
8359
8360         ;; See whether the article is to be put in the cache.
8361         (and gnus-use-cache
8362              (vectorp (gnus-summary-article-header article))
8363              (save-excursion
8364                (gnus-cache-possibly-enter-article
8365                 gnus-newsgroup-name article
8366                 (gnus-summary-article-header article)
8367                 (= mark gnus-ticked-mark)
8368                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8369
8370         ;; Fix the mark.
8371         (gnus-summary-update-mark mark 'unread)
8372         t))))
8373
8374 (defun gnus-summary-mark-article (&optional article mark no-expire)
8375   "Mark ARTICLE with MARK.  MARK can be any character.
8376 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8377 `??' (dormant) and `?E' (expirable).
8378 If MARK is nil, then the default character `?r' is used.
8379 If ARTICLE is nil, then the article on the current line will be
8380 marked.
8381 Iff NO-EXPIRE, auto-expiry will be inhibited."
8382   ;; The mark might be a string.
8383   (when (stringp mark)
8384     (setq mark (aref mark 0)))
8385   ;; If no mark is given, then we check auto-expiring.
8386   (when (null mark)
8387     (setq mark gnus-del-mark))
8388   (when (and (not no-expire)
8389              gnus-newsgroup-auto-expire
8390              (memq mark gnus-auto-expirable-marks))
8391     (setq mark gnus-expirable-mark))
8392   (let ((article (or article (gnus-summary-article-number)))
8393         (old-mark (gnus-summary-article-mark article)))
8394     ;; Allow the backend to change the mark.
8395     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8396     (if (eq mark old-mark)
8397         t
8398       (unless article
8399         (error "No article on current line"))
8400       (if (not (if (or (= mark gnus-unread-mark)
8401                        (= mark gnus-ticked-mark)
8402                        (= mark gnus-dormant-mark))
8403                    (gnus-mark-article-as-unread article mark)
8404                  (gnus-mark-article-as-read article mark)))
8405           t
8406         ;; See whether the article is to be put in the cache.
8407         (and gnus-use-cache
8408              (not (= mark gnus-canceled-mark))
8409              (vectorp (gnus-summary-article-header article))
8410              (save-excursion
8411                (gnus-cache-possibly-enter-article
8412                 gnus-newsgroup-name article
8413                 (gnus-summary-article-header article)
8414                 (= mark gnus-ticked-mark)
8415                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8416
8417         (when (gnus-summary-goto-subject article nil t)
8418           (let ((buffer-read-only nil))
8419             (gnus-summary-show-thread)
8420             ;; Fix the mark.
8421             (gnus-summary-update-mark mark 'unread)
8422             t))))))
8423
8424 (defun gnus-summary-update-secondary-mark (article)
8425   "Update the secondary (read, process, cache) mark."
8426   (gnus-summary-update-mark
8427    (cond ((memq article gnus-newsgroup-processable)
8428           gnus-process-mark)
8429          ((memq article gnus-newsgroup-cached)
8430           gnus-cached-mark)
8431          ((memq article gnus-newsgroup-replied)
8432           gnus-replied-mark)
8433          ((memq article gnus-newsgroup-saved)
8434           gnus-saved-mark)
8435          (t gnus-unread-mark))
8436    'replied)
8437   (when (gnus-visual-p 'summary-highlight 'highlight)
8438     (gnus-run-hooks 'gnus-summary-update-hook))
8439   t)
8440
8441 (defun gnus-summary-update-mark (mark type)
8442   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8443         (buffer-read-only nil))
8444     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8445     (when forward
8446       (when (looking-at "\r")
8447         (incf forward))
8448       (when (<= (+ forward (point)) (point-max))
8449         ;; Go to the right position on the line.
8450         (goto-char (+ forward (point)))
8451         ;; Replace the old mark with the new mark.
8452         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8453         ;; Optionally update the marks by some user rule.
8454         (when (eq type 'unread)
8455           (gnus-data-set-mark
8456            (gnus-data-find (gnus-summary-article-number)) mark)
8457           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8458
8459 (defun gnus-mark-article-as-read (article &optional mark)
8460   "Enter ARTICLE in the pertinent lists and remove it from others."
8461   ;; Make the article expirable.
8462   (let ((mark (or mark gnus-del-mark)))
8463     (if (= mark gnus-expirable-mark)
8464         (push article gnus-newsgroup-expirable)
8465       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8466     ;; Remove from unread and marked lists.
8467     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8468     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8469     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8470     (push (cons article mark) gnus-newsgroup-reads)
8471     ;; Possibly remove from cache, if that is used.
8472     (when gnus-use-cache
8473       (gnus-cache-enter-remove-article article))
8474     t))
8475
8476 (defun gnus-mark-article-as-unread (article &optional mark)
8477   "Enter ARTICLE in the pertinent lists and remove it from others."
8478   (let ((mark (or mark gnus-ticked-mark)))
8479     (if (<= article 0)
8480         (progn
8481           (gnus-error 1 "Can't mark negative article numbers")
8482           nil)
8483       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8484             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8485             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8486             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8487
8488       ;; Unsuppress duplicates?
8489       (when gnus-suppress-duplicates
8490         (gnus-dup-unsuppress-article article))
8491
8492       (cond ((= mark gnus-ticked-mark)
8493              (push article gnus-newsgroup-marked))
8494             ((= mark gnus-dormant-mark)
8495              (push article gnus-newsgroup-dormant))
8496             (t
8497              (push article gnus-newsgroup-unreads)))
8498       (gnus-pull article gnus-newsgroup-reads)
8499       t)))
8500
8501 (defalias 'gnus-summary-mark-as-unread-forward
8502   'gnus-summary-tick-article-forward)
8503 (make-obsolete 'gnus-summary-mark-as-unread-forward
8504                'gnus-summary-tick-article-forward)
8505 (defun gnus-summary-tick-article-forward (n)
8506   "Tick N articles forwards.
8507 If N is negative, tick backwards instead.
8508 The difference between N and the number of articles ticked is returned."
8509   (interactive "p")
8510   (gnus-summary-mark-forward n gnus-ticked-mark))
8511
8512 (defalias 'gnus-summary-mark-as-unread-backward
8513   'gnus-summary-tick-article-backward)
8514 (make-obsolete 'gnus-summary-mark-as-unread-backward
8515                'gnus-summary-tick-article-backward)
8516 (defun gnus-summary-tick-article-backward (n)
8517   "Tick N articles backwards.
8518 The difference between N and the number of articles ticked is returned."
8519   (interactive "p")
8520   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8521
8522 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8523 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8524 (defun gnus-summary-tick-article (&optional article clear-mark)
8525   "Mark current article as unread.
8526 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8527 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8528   (interactive)
8529   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8530                                        gnus-ticked-mark)))
8531
8532 (defun gnus-summary-mark-as-read-forward (n)
8533   "Mark N articles as read forwards.
8534 If N is negative, mark backwards instead.
8535 The difference between N and the actual number of articles marked is
8536 returned."
8537   (interactive "p")
8538   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8539
8540 (defun gnus-summary-mark-as-read-backward (n)
8541   "Mark the N articles as read backwards.
8542 The difference between N and the actual number of articles marked is
8543 returned."
8544   (interactive "p")
8545   (gnus-summary-mark-forward
8546    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8547
8548 (defun gnus-summary-mark-as-read (&optional article mark)
8549   "Mark current article as read.
8550 ARTICLE specifies the article to be marked as read.
8551 MARK specifies a string to be inserted at the beginning of the line."
8552   (gnus-summary-mark-article article mark))
8553
8554 (defun gnus-summary-clear-mark-forward (n)
8555   "Clear marks from N articles forward.
8556 If N is negative, clear backward instead.
8557 The difference between N and the number of marks cleared is returned."
8558   (interactive "p")
8559   (gnus-summary-mark-forward n gnus-unread-mark))
8560
8561 (defun gnus-summary-clear-mark-backward (n)
8562   "Clear marks from N articles backward.
8563 The difference between N and the number of marks cleared is returned."
8564   (interactive "p")
8565   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8566
8567 (defun gnus-summary-mark-unread-as-read ()
8568   "Intended to be used by `gnus-summary-mark-article-hook'."
8569   (when (memq gnus-current-article gnus-newsgroup-unreads)
8570     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8571
8572 (defun gnus-summary-mark-read-and-unread-as-read ()
8573   "Intended to be used by `gnus-summary-mark-article-hook'."
8574   (let ((mark (gnus-summary-article-mark)))
8575     (when (or (gnus-unread-mark-p mark)
8576               (gnus-read-mark-p mark))
8577       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8578
8579 (defun gnus-summary-mark-region-as-read (point mark all)
8580   "Mark all unread articles between point and mark as read.
8581 If given a prefix, mark all articles between point and mark as read,
8582 even ticked and dormant ones."
8583   (interactive "r\nP")
8584   (save-excursion
8585     (let (article)
8586       (goto-char point)
8587       (beginning-of-line)
8588       (while (and
8589               (< (point) mark)
8590               (progn
8591                 (when (or all
8592                           (memq (setq article (gnus-summary-article-number))
8593                                 gnus-newsgroup-unreads))
8594                   (gnus-summary-mark-article article gnus-del-mark))
8595                 t)
8596               (gnus-summary-find-next))))))
8597
8598 (defun gnus-summary-mark-below (score mark)
8599   "Mark articles with score less than SCORE with MARK."
8600   (interactive "P\ncMark: ")
8601   (setq score (if score
8602                   (prefix-numeric-value score)
8603                 (or gnus-summary-default-score 0)))
8604   (save-excursion
8605     (set-buffer gnus-summary-buffer)
8606     (goto-char (point-min))
8607     (while
8608         (progn
8609           (and (< (gnus-summary-article-score) score)
8610                (gnus-summary-mark-article nil mark))
8611           (gnus-summary-find-next)))))
8612
8613 (defun gnus-summary-kill-below (&optional score)
8614   "Mark articles with score below SCORE as read."
8615   (interactive "P")
8616   (gnus-summary-mark-below score gnus-killed-mark))
8617
8618 (defun gnus-summary-clear-above (&optional score)
8619   "Clear all marks from articles with score above SCORE."
8620   (interactive "P")
8621   (gnus-summary-mark-above score gnus-unread-mark))
8622
8623 (defun gnus-summary-tick-above (&optional score)
8624   "Tick all articles with score above SCORE."
8625   (interactive "P")
8626   (gnus-summary-mark-above score gnus-ticked-mark))
8627
8628 (defun gnus-summary-mark-above (score mark)
8629   "Mark articles with score over SCORE with MARK."
8630   (interactive "P\ncMark: ")
8631   (setq score (if score
8632                   (prefix-numeric-value score)
8633                 (or gnus-summary-default-score 0)))
8634   (save-excursion
8635     (set-buffer gnus-summary-buffer)
8636     (goto-char (point-min))
8637     (while (and (progn
8638                   (when (> (gnus-summary-article-score) score)
8639                     (gnus-summary-mark-article nil mark))
8640                   t)
8641                 (gnus-summary-find-next)))))
8642
8643 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8644 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8645 (defun gnus-summary-limit-include-expunged (&optional no-error)
8646   "Display all the hidden articles that were expunged for low scores."
8647   (interactive)
8648   (let ((buffer-read-only nil))
8649     (let ((scored gnus-newsgroup-scored)
8650           headers h)
8651       (while scored
8652         (unless (gnus-summary-goto-subject (caar scored))
8653           (and (setq h (gnus-summary-article-header (caar scored)))
8654                (< (cdar scored) gnus-summary-expunge-below)
8655                (push h headers)))
8656         (setq scored (cdr scored)))
8657       (if (not headers)
8658           (when (not no-error)
8659             (error "No expunged articles hidden"))
8660         (goto-char (point-min))
8661         (gnus-summary-prepare-unthreaded (nreverse headers))
8662         (goto-char (point-min))
8663         (gnus-summary-position-point)
8664         t))))
8665
8666 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8667   "Mark all unread articles in this newsgroup as read.
8668 If prefix argument ALL is non-nil, ticked and dormant articles will
8669 also be marked as read.
8670 If QUIETLY is non-nil, no questions will be asked.
8671 If TO-HERE is non-nil, it should be a point in the buffer.  All
8672 articles before this point will be marked as read.
8673 Note that this function will only catch up the unread article
8674 in the current summary buffer limitation.
8675 The number of articles marked as read is returned."
8676   (interactive "P")
8677   (prog1
8678       (save-excursion
8679         (when (or quietly
8680                   (not gnus-interactive-catchup) ;Without confirmation?
8681                   gnus-expert-user
8682                   (gnus-y-or-n-p
8683                    (if all
8684                        "Mark absolutely all articles as read? "
8685                      "Mark all unread articles as read? ")))
8686           (if (and not-mark
8687                    (not gnus-newsgroup-adaptive)
8688                    (not gnus-newsgroup-auto-expire)
8689                    (not gnus-suppress-duplicates)
8690                    (or (not gnus-use-cache)
8691                        (eq gnus-use-cache 'passive)))
8692               (progn
8693                 (when all
8694                   (setq gnus-newsgroup-marked nil
8695                         gnus-newsgroup-dormant nil))
8696                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8697             ;; We actually mark all articles as canceled, which we
8698             ;; have to do when using auto-expiry or adaptive scoring.
8699             (gnus-summary-show-all-threads)
8700             (when (gnus-summary-first-subject (not all) t)
8701               (while (and
8702                       (if to-here (< (point) to-here) t)
8703                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8704                       (gnus-summary-find-next (not all) nil nil t))))
8705             (gnus-set-mode-line 'summary))
8706           t))
8707     (gnus-summary-position-point)))
8708
8709 (defun gnus-summary-catchup-to-here (&optional all)
8710   "Mark all unticked articles before the current one as read.
8711 If ALL is non-nil, also mark ticked and dormant articles as read."
8712   (interactive "P")
8713   (save-excursion
8714     (gnus-save-hidden-threads
8715       (let ((beg (point)))
8716         ;; We check that there are unread articles.
8717         (when (or all (gnus-summary-find-prev))
8718           (gnus-summary-catchup all t beg)))))
8719   (gnus-summary-position-point))
8720
8721 (defun gnus-summary-catchup-all (&optional quietly)
8722   "Mark all articles in this newsgroup as read."
8723   (interactive "P")
8724   (gnus-summary-catchup t quietly))
8725
8726 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8727   "Mark all unread articles in this group as read, then exit.
8728 If prefix argument ALL is non-nil, all articles are marked as read."
8729   (interactive "P")
8730   (when (gnus-summary-catchup all quietly nil 'fast)
8731     ;; Select next newsgroup or exit.
8732     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8733              (eq gnus-auto-select-next 'quietly))
8734         (gnus-summary-next-group nil)
8735       (gnus-summary-exit))))
8736
8737 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8738   "Mark all articles in this newsgroup as read, and then exit."
8739   (interactive "P")
8740   (gnus-summary-catchup-and-exit t quietly))
8741
8742 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8743   "Mark all articles in this group as read and select the next group.
8744 If given a prefix, mark all articles, unread as well as ticked, as
8745 read."
8746   (interactive "P")
8747   (save-excursion
8748     (gnus-summary-catchup all))
8749   (gnus-summary-next-group))
8750
8751 ;;;
8752 ;;; with article
8753 ;;;
8754
8755 (defmacro gnus-with-article (article &rest forms)
8756   "Select ARTICLE and perform FORMS in the original article buffer.
8757 Then replace the article with the result."
8758   `(progn
8759      ;; We don't want the article to be marked as read.
8760      (let (gnus-mark-article-hook)
8761        (gnus-summary-select-article t t nil ,article))
8762      (set-buffer gnus-original-article-buffer)
8763      ,@forms
8764      (if (not (gnus-check-backend-function
8765                'request-replace-article (car gnus-article-current)))
8766          (gnus-message 5 "Read-only group; not replacing")
8767        (unless (gnus-request-replace-article
8768                 ,article (car gnus-article-current)
8769                 (current-buffer) t)
8770          (error "Couldn't replace article")))
8771      ;; The cache and backlog have to be flushed somewhat.
8772      (when gnus-keep-backlog
8773        (gnus-backlog-remove-article
8774         (car gnus-article-current) (cdr gnus-article-current)))
8775      (when gnus-use-cache
8776        (gnus-cache-update-article
8777         (car gnus-article-current) (cdr gnus-article-current)))))
8778
8779 (put 'gnus-with-article 'lisp-indent-function 1)
8780 (put 'gnus-with-article 'edebug-form-spec '(form body))
8781
8782 ;; Thread-based commands.
8783
8784 (defun gnus-summary-articles-in-thread (&optional article)
8785   "Return a list of all articles in the current thread.
8786 If ARTICLE is non-nil, return all articles in the thread that starts
8787 with that article."
8788   (let* ((article (or article (gnus-summary-article-number)))
8789          (data (gnus-data-find-list article))
8790          (top-level (gnus-data-level (car data)))
8791          (top-subject
8792           (cond ((null gnus-thread-operation-ignore-subject)
8793                  (gnus-simplify-subject-re
8794                   (mail-header-subject (gnus-data-header (car data)))))
8795                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8796                  (gnus-simplify-subject-fuzzy
8797                   (mail-header-subject (gnus-data-header (car data)))))
8798                 (t nil)))
8799          (end-point (save-excursion
8800                       (if (gnus-summary-go-to-next-thread)
8801                           (point) (point-max))))
8802          articles)
8803     (while (and data
8804                 (< (gnus-data-pos (car data)) end-point))
8805       (when (or (not top-subject)
8806                 (string= top-subject
8807                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8808                              (gnus-simplify-subject-fuzzy
8809                               (mail-header-subject
8810                                (gnus-data-header (car data))))
8811                            (gnus-simplify-subject-re
8812                             (mail-header-subject
8813                              (gnus-data-header (car data)))))))
8814         (push (gnus-data-number (car data)) articles))
8815       (unless (and (setq data (cdr data))
8816                    (> (gnus-data-level (car data)) top-level))
8817         (setq data nil)))
8818     ;; Return the list of articles.
8819     (nreverse articles)))
8820
8821 (defun gnus-summary-rethread-current ()
8822   "Rethread the thread the current article is part of."
8823   (interactive)
8824   (let* ((gnus-show-threads t)
8825          (article (gnus-summary-article-number))
8826          (id (mail-header-id (gnus-summary-article-header)))
8827          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8828     (unless id
8829       (error "No article on the current line"))
8830     (gnus-rebuild-thread id)
8831     (gnus-summary-goto-subject article)))
8832
8833 (defun gnus-summary-reparent-thread ()
8834   "Make the current article child of the marked (or previous) article.
8835
8836 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8837 is non-nil or the Subject: of both articles are the same."
8838   (interactive)
8839   (unless (not (gnus-group-read-only-p))
8840     (error "The current newsgroup does not support article editing"))
8841   (unless (<= (length gnus-newsgroup-processable) 1)
8842     (error "No more than one article may be marked"))
8843   (save-window-excursion
8844     (let ((gnus-article-buffer " *reparent*")
8845           (current-article (gnus-summary-article-number))
8846           ;; First grab the marked article, otherwise one line up.
8847           (parent-article (if (not (null gnus-newsgroup-processable))
8848                               (car gnus-newsgroup-processable)
8849                             (save-excursion
8850                               (if (eq (forward-line -1) 0)
8851                                   (gnus-summary-article-number)
8852                                 (error "Beginning of summary buffer"))))))
8853       (unless (not (eq current-article parent-article))
8854         (error "An article may not be self-referential"))
8855       (let ((message-id (mail-header-id
8856                          (gnus-summary-article-header parent-article))))
8857         (unless (and message-id (not (equal message-id "")))
8858           (error "No message-id in desired parent"))
8859         (gnus-with-article current-article
8860           (save-restriction
8861             (goto-char (point-min))
8862             (message-narrow-to-head)
8863             (if (re-search-forward "^References: " nil t)
8864                 (progn
8865                   (re-search-forward "^[^ \t]" nil t)
8866                   (forward-line -1)
8867                   (end-of-line)
8868                   (insert " " message-id))
8869               (insert "References: " message-id "\n"))))
8870         (set-buffer gnus-summary-buffer)
8871         (gnus-summary-unmark-all-processable)
8872         (gnus-summary-update-article current-article)
8873         (gnus-summary-rethread-current)
8874         (gnus-message 3 "Article %d is now the child of article %d"
8875                       current-article parent-article)))))
8876
8877 (defun gnus-summary-toggle-threads (&optional arg)
8878   "Toggle showing conversation threads.
8879 If ARG is positive number, turn showing conversation threads on."
8880   (interactive "P")
8881   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8882     (setq gnus-show-threads
8883           (if (null arg) (not gnus-show-threads)
8884             (> (prefix-numeric-value arg) 0)))
8885     (gnus-summary-prepare)
8886     (gnus-summary-goto-subject current)
8887     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8888     (gnus-summary-position-point)))
8889
8890 (defun gnus-summary-show-all-threads ()
8891   "Show all threads."
8892   (interactive)
8893   (save-excursion
8894     (let ((buffer-read-only nil))
8895       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8896   (gnus-summary-position-point))
8897
8898 (defun gnus-summary-show-thread ()
8899   "Show thread subtrees.
8900 Returns nil if no thread was there to be shown."
8901   (interactive)
8902   (let ((buffer-read-only nil)
8903         (orig (point))
8904         ;; first goto end then to beg, to have point at beg after let
8905         (end (progn (end-of-line) (point)))
8906         (beg (progn (beginning-of-line) (point))))
8907     (prog1
8908         ;; Any hidden lines here?
8909         (search-forward "\r" end t)
8910       (subst-char-in-region beg end ?\^M ?\n t)
8911       (goto-char orig)
8912       (gnus-summary-position-point))))
8913
8914 (defun gnus-summary-hide-all-threads ()
8915   "Hide all thread subtrees."
8916   (interactive)
8917   (save-excursion
8918     (goto-char (point-min))
8919     (gnus-summary-hide-thread)
8920     (while (zerop (gnus-summary-next-thread 1 t))
8921       (gnus-summary-hide-thread)))
8922   (gnus-summary-position-point))
8923
8924 (defun gnus-summary-hide-thread ()
8925   "Hide thread subtrees.
8926 Returns nil if no threads were there to be hidden."
8927   (interactive)
8928   (let ((buffer-read-only nil)
8929         (start (point))
8930         (article (gnus-summary-article-number)))
8931     (goto-char start)
8932     ;; Go forward until either the buffer ends or the subthread
8933     ;; ends.
8934     (when (and (not (eobp))
8935                (or (zerop (gnus-summary-next-thread 1 t))
8936                    (goto-char (point-max))))
8937       (prog1
8938           (if (and (> (point) start)
8939                    (search-backward "\n" start t))
8940               (progn
8941                 (subst-char-in-region start (point) ?\n ?\^M)
8942                 (gnus-summary-goto-subject article))
8943             (goto-char start)
8944             nil)))))
8945
8946 (defun gnus-summary-go-to-next-thread (&optional previous)
8947   "Go to the same level (or less) next thread.
8948 If PREVIOUS is non-nil, go to previous thread instead.
8949 Return the article number moved to, or nil if moving was impossible."
8950   (let ((level (gnus-summary-thread-level))
8951         (way (if previous -1 1))
8952         (beg (point)))
8953     (forward-line way)
8954     (while (and (not (eobp))
8955                 (< level (gnus-summary-thread-level)))
8956       (forward-line way))
8957     (if (eobp)
8958         (progn
8959           (goto-char beg)
8960           nil)
8961       (setq beg (point))
8962       (prog1
8963           (gnus-summary-article-number)
8964         (goto-char beg)))))
8965
8966 (defun gnus-summary-next-thread (n &optional silent)
8967   "Go to the same level next N'th thread.
8968 If N is negative, search backward instead.
8969 Returns the difference between N and the number of skips actually
8970 done.
8971
8972 If SILENT, don't output messages."
8973   (interactive "p")
8974   (let ((backward (< n 0))
8975         (n (abs n)))
8976     (while (and (> n 0)
8977                 (gnus-summary-go-to-next-thread backward))
8978       (decf n))
8979     (unless silent
8980       (gnus-summary-position-point))
8981     (when (and (not silent) (/= 0 n))
8982       (gnus-message 7 "No more threads"))
8983     n))
8984
8985 (defun gnus-summary-prev-thread (n)
8986   "Go to the same level previous N'th thread.
8987 Returns the difference between N and the number of skips actually
8988 done."
8989   (interactive "p")
8990   (gnus-summary-next-thread (- n)))
8991
8992 (defun gnus-summary-go-down-thread ()
8993   "Go down one level in the current thread."
8994   (let ((children (gnus-summary-article-children)))
8995     (when children
8996       (gnus-summary-goto-subject (car children)))))
8997
8998 (defun gnus-summary-go-up-thread ()
8999   "Go up one level in the current thread."
9000   (let ((parent (gnus-summary-article-parent)))
9001     (when parent
9002       (gnus-summary-goto-subject parent))))
9003
9004 (defun gnus-summary-down-thread (n)
9005   "Go down thread N steps.
9006 If N is negative, go up instead.
9007 Returns the difference between N and how many steps down that were
9008 taken."
9009   (interactive "p")
9010   (let ((up (< n 0))
9011         (n (abs n)))
9012     (while (and (> n 0)
9013                 (if up (gnus-summary-go-up-thread)
9014                   (gnus-summary-go-down-thread)))
9015       (setq n (1- n)))
9016     (gnus-summary-position-point)
9017     (when (/= 0 n)
9018       (gnus-message 7 "Can't go further"))
9019     n))
9020
9021 (defun gnus-summary-up-thread (n)
9022   "Go up thread N steps.
9023 If N is negative, go up instead.
9024 Returns the difference between N and how many steps down that were
9025 taken."
9026   (interactive "p")
9027   (gnus-summary-down-thread (- n)))
9028
9029 (defun gnus-summary-top-thread ()
9030   "Go to the top of the thread."
9031   (interactive)
9032   (while (gnus-summary-go-up-thread))
9033   (gnus-summary-article-number))
9034
9035 (defun gnus-summary-kill-thread (&optional unmark)
9036   "Mark articles under current thread as read.
9037 If the prefix argument is positive, remove any kinds of marks.
9038 If the prefix argument is negative, tick articles instead."
9039   (interactive "P")
9040   (when unmark
9041     (setq unmark (prefix-numeric-value unmark)))
9042   (let ((articles (gnus-summary-articles-in-thread)))
9043     (save-excursion
9044       ;; Expand the thread.
9045       (gnus-summary-show-thread)
9046       ;; Mark all the articles.
9047       (while articles
9048         (gnus-summary-goto-subject (car articles))
9049         (cond ((null unmark)
9050                (gnus-summary-mark-article-as-read gnus-killed-mark))
9051               ((> unmark 0)
9052                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9053               (t
9054                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9055         (setq articles (cdr articles))))
9056     ;; Hide killed subtrees.
9057     (and (null unmark)
9058          gnus-thread-hide-killed
9059          (gnus-summary-hide-thread))
9060     ;; If marked as read, go to next unread subject.
9061     (when (null unmark)
9062       ;; Go to next unread subject.
9063       (gnus-summary-next-subject 1 t)))
9064   (gnus-set-mode-line 'summary))
9065
9066 ;; Summary sorting commands
9067
9068 (defun gnus-summary-sort-by-number (&optional reverse)
9069   "Sort the summary buffer by article number.
9070 Argument REVERSE means reverse order."
9071   (interactive "P")
9072   (gnus-summary-sort 'number reverse))
9073
9074 (defun gnus-summary-sort-by-author (&optional reverse)
9075   "Sort the summary buffer by author name alphabetically.
9076 If `case-fold-search' is non-nil, case of letters is ignored.
9077 Argument REVERSE means reverse order."
9078   (interactive "P")
9079   (gnus-summary-sort 'author reverse))
9080
9081 (defun gnus-summary-sort-by-subject (&optional reverse)
9082   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9083 If `case-fold-search' is non-nil, case of letters is ignored.
9084 Argument REVERSE means reverse order."
9085   (interactive "P")
9086   (gnus-summary-sort 'subject reverse))
9087
9088 (defun gnus-summary-sort-by-date (&optional reverse)
9089   "Sort the summary buffer by date.
9090 Argument REVERSE means reverse order."
9091   (interactive "P")
9092   (gnus-summary-sort 'date reverse))
9093
9094 (defun gnus-summary-sort-by-score (&optional reverse)
9095   "Sort the summary buffer by score.
9096 Argument REVERSE means reverse order."
9097   (interactive "P")
9098   (gnus-summary-sort 'score reverse))
9099
9100 (defun gnus-summary-sort-by-lines (&optional reverse)
9101   "Sort the summary buffer by the number of lines.
9102 Argument REVERSE means reverse order."
9103   (interactive "P")
9104   (gnus-summary-sort 'lines reverse))
9105
9106 (defun gnus-summary-sort-by-chars (&optional reverse)
9107   "Sort the summary buffer by article length.
9108 Argument REVERSE means reverse order."
9109   (interactive "P")
9110   (gnus-summary-sort 'chars reverse))
9111
9112 (defun gnus-summary-sort (predicate reverse)
9113   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9114   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9115          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9116          (gnus-thread-sort-functions
9117           (if (not reverse)
9118               thread
9119             `(lambda (t1 t2)
9120                (,thread t2 t1))))
9121          (gnus-sort-gathered-threads-function
9122           gnus-thread-sort-functions)
9123          (gnus-article-sort-functions
9124           (if (not reverse)
9125               article
9126             `(lambda (t1 t2)
9127                (,article t2 t1))))
9128          (buffer-read-only)
9129          (gnus-summary-prepare-hook nil))
9130     ;; We do the sorting by regenerating the threads.
9131     (gnus-summary-prepare)
9132     ;; Hide subthreads if needed.
9133     (when (and gnus-show-threads gnus-thread-hide-subtree)
9134       (gnus-summary-hide-all-threads))))
9135
9136 ;; Summary saving commands.
9137
9138 (defun gnus-summary-save-article (&optional n not-saved)
9139   "Save the current article using the default saver function.
9140 If N is a positive number, save the N next articles.
9141 If N is a negative number, save the N previous articles.
9142 If N is nil and any articles have been marked with the process mark,
9143 save those articles instead.
9144 The variable `gnus-default-article-saver' specifies the saver function."
9145   (interactive "P")
9146   (let* ((articles (gnus-summary-work-articles n))
9147          (save-buffer (save-excursion
9148                         (nnheader-set-temp-buffer " *Gnus Save*")))
9149          (num (length articles))
9150          header file)
9151     (dolist (article articles)
9152       (setq header (gnus-summary-article-header article))
9153       (if (not (vectorp header))
9154           ;; This is a pseudo-article.
9155           (if (assq 'name header)
9156               (gnus-copy-file (cdr (assq 'name header)))
9157             (gnus-message 1 "Article %d is unsaveable" article))
9158         ;; This is a real article.
9159         (save-window-excursion
9160           (gnus-summary-select-article t nil nil article))
9161         (save-excursion
9162           (set-buffer save-buffer)
9163           (erase-buffer)
9164           (insert-buffer-substring gnus-original-article-buffer))
9165         (setq file (gnus-article-save save-buffer file num))
9166         (gnus-summary-remove-process-mark article)
9167         (unless not-saved
9168           (gnus-summary-set-saved-mark article))))
9169     (gnus-kill-buffer save-buffer)
9170     (gnus-summary-position-point)
9171     (gnus-set-mode-line 'summary)
9172     n))
9173
9174 (defun gnus-summary-pipe-output (&optional arg)
9175   "Pipe the current article to a subprocess.
9176 If N is a positive number, pipe the N next articles.
9177 If N is a negative number, pipe the N previous articles.
9178 If N is nil and any articles have been marked with the process mark,
9179 pipe those articles instead."
9180   (interactive "P")
9181   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9182     (gnus-summary-save-article arg t))
9183   (gnus-configure-windows 'pipe))
9184
9185 (defun gnus-summary-save-article-mail (&optional arg)
9186   "Append the current article to an mail file.
9187 If N is a positive number, save the N next articles.
9188 If N is a negative number, save the N previous articles.
9189 If N is nil and any articles have been marked with the process mark,
9190 save those articles instead."
9191   (interactive "P")
9192   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9193     (gnus-summary-save-article arg)))
9194
9195 (defun gnus-summary-save-article-rmail (&optional arg)
9196   "Append the current article to an rmail file.
9197 If N is a positive number, save the N next articles.
9198 If N is a negative number, save the N previous articles.
9199 If N is nil and any articles have been marked with the process mark,
9200 save those articles instead."
9201   (interactive "P")
9202   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9203     (gnus-summary-save-article arg)))
9204
9205 (defun gnus-summary-save-article-file (&optional arg)
9206   "Append the current article to a 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-file))
9213     (gnus-summary-save-article arg)))
9214
9215 (defun gnus-summary-write-article-file (&optional arg)
9216   "Write the current article to a file, deleting the previous 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-write-to-file))
9223     (gnus-summary-save-article arg)))
9224
9225 (defun gnus-summary-save-article-body-file (&optional arg)
9226   "Append the current article body 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-body-in-file))
9233     (gnus-summary-save-article arg)))
9234
9235 (defun gnus-summary-pipe-message (program)
9236   "Pipe the current article through PROGRAM."
9237   (interactive "sProgram: ")
9238   (gnus-summary-select-article)
9239   (let ((mail-header-separator ""))
9240     (gnus-eval-in-buffer-window gnus-article-buffer
9241       (save-restriction
9242         (widen)
9243         (let ((start (window-start))
9244               buffer-read-only)
9245           (message-pipe-buffer-body program)
9246           (set-window-start (get-buffer-window (current-buffer)) start))))))
9247
9248 (defun gnus-get-split-value (methods)
9249   "Return a value based on the split METHODS."
9250   (let (split-name method result match)
9251     (when methods
9252       (save-excursion
9253         (set-buffer gnus-original-article-buffer)
9254         (save-restriction
9255           (nnheader-narrow-to-headers)
9256           (while (and methods (not split-name))
9257             (goto-char (point-min))
9258             (setq method (pop methods))
9259             (setq match (car method))
9260             (when (cond
9261                    ((stringp match)
9262                     ;; Regular expression.
9263                     (ignore-errors
9264                       (re-search-forward match nil t)))
9265                    ((gnus-functionp match)
9266                     ;; Function.
9267                     (save-restriction
9268                       (widen)
9269                       (setq result (funcall match gnus-newsgroup-name))))
9270                    ((consp match)
9271                     ;; Form.
9272                     (save-restriction
9273                       (widen)
9274                       (setq result (eval match)))))
9275               (setq split-name (cdr method))
9276               (cond ((stringp result)
9277                      (push (expand-file-name
9278                             result gnus-article-save-directory)
9279                            split-name))
9280                     ((consp result)
9281                      (setq split-name (append result split-name)))))))))
9282     (nreverse split-name)))
9283
9284 (defun gnus-valid-move-group-p (group)
9285   (and (boundp group)
9286        (symbol-name group)
9287        (symbol-value group)
9288        (gnus-get-function (gnus-find-method-for-group
9289                            (symbol-name group)) 'request-accept-article t)))
9290
9291 (defun gnus-read-move-group-name (prompt default articles prefix)
9292   "Read a group name."
9293   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9294          (minibuffer-confirm-incomplete nil) ; XEmacs
9295          (prom
9296           (format "%s %s to:"
9297                   prompt
9298                   (if (> (length articles) 1)
9299                       (format "these %d articles" (length articles))
9300                     "this article")))
9301          (to-newsgroup
9302           (cond
9303            ((null split-name)
9304             (gnus-completing-read default prom
9305                                   gnus-active-hashtb
9306                                   'gnus-valid-move-group-p
9307                                   nil prefix
9308                                   'gnus-group-history))
9309            ((= 1 (length split-name))
9310             (gnus-completing-read (car split-name) prom
9311                                   gnus-active-hashtb
9312                                   'gnus-valid-move-group-p
9313                                   nil nil
9314                                   'gnus-group-history))
9315            (t
9316             (gnus-completing-read nil prom
9317                                   (mapcar (lambda (el) (list el))
9318                                           (nreverse split-name))
9319                                   nil nil nil
9320                                   'gnus-group-history))))
9321          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9322     (when to-newsgroup
9323       (if (or (string= to-newsgroup "")
9324               (string= to-newsgroup prefix))
9325           (setq to-newsgroup default))
9326       (unless to-newsgroup
9327         (error "No group name entered"))
9328       (or (gnus-active to-newsgroup)
9329           (gnus-activate-group to-newsgroup nil nil to-method)
9330           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9331                                      to-newsgroup))
9332               (or (and (gnus-request-create-group to-newsgroup to-method)
9333                        (gnus-activate-group
9334                         to-newsgroup nil nil to-method)
9335                        (gnus-subscribe-group to-newsgroup))
9336                   (error "Couldn't create group %s" to-newsgroup)))
9337           (error "No such group: %s" to-newsgroup)))
9338     to-newsgroup))
9339
9340 (defun gnus-summary-save-parts (type dir n &optional reverse)
9341   "Save parts matching TYPE to DIR.
9342 If REVERSE, save parts that do not match TYPE."
9343   (interactive
9344    (list (read-string "Save parts of type: " 
9345                       (or (car gnus-summary-save-parts-type-history)
9346                           gnus-summary-save-parts-default-mime)
9347                       'gnus-summary-save-parts-type-history)
9348          (setq gnus-summary-save-parts-last-directory
9349                (read-file-name "Save to directory: " 
9350                                gnus-summary-save-parts-last-directory
9351                                nil t))
9352          current-prefix-arg))
9353   (gnus-summary-iterate n
9354     (let ((gnus-display-mime-function nil)
9355           (gnus-inhibit-treatment t))
9356       (gnus-summary-select-article))
9357     (save-excursion
9358       (set-buffer gnus-article-buffer)
9359       (let ((handles (or gnus-article-mime-handles
9360                          (mm-dissect-buffer) (mm-uu-dissect))))
9361         (when handles
9362           (gnus-summary-save-parts-1 type dir handles reverse)
9363           (unless gnus-article-mime-handles ;; Don't destroy this case.
9364             (mm-destroy-parts handles)))))))
9365
9366 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9367   (if (stringp (car handle))
9368       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9369               (cdr handle))
9370     (when (if reverse
9371               (not (string-match type (mm-handle-media-type handle)))
9372             (string-match type (mm-handle-media-type handle)))
9373       (let ((file (expand-file-name
9374                    (file-name-nondirectory
9375                     (or
9376                      (mail-content-type-get
9377                       (mm-handle-disposition handle) 'filename)
9378                      (concat gnus-newsgroup-name
9379                              "." (number-to-string
9380                                   (cdr gnus-article-current)))))
9381                    dir)))
9382         (unless (file-exists-p file)
9383           (mm-save-part-to-file handle file))))))
9384
9385 ;; Summary extract commands
9386
9387 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9388   (let ((buffer-read-only nil)
9389         (article (gnus-summary-article-number))
9390         after-article b e)
9391     (unless (gnus-summary-goto-subject article)
9392       (error "No such article: %d" article))
9393     (gnus-summary-position-point)
9394     ;; If all commands are to be bunched up on one line, we collect
9395     ;; them here.
9396     (unless gnus-view-pseudos-separately
9397       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9398             files action)
9399         (while ps
9400           (setq action (cdr (assq 'action (car ps))))
9401           (setq files (list (cdr (assq 'name (car ps)))))
9402           (while (and ps (cdr ps)
9403                       (string= (or action "1")
9404                                (or (cdr (assq 'action (cadr ps))) "2")))
9405             (push (cdr (assq 'name (cadr ps))) files)
9406             (setcdr ps (cddr ps)))
9407           (when files
9408             (when (not (string-match "%s" action))
9409               (push " " files))
9410             (push " " files)
9411             (when (assq 'execute (car ps))
9412               (setcdr (assq 'execute (car ps))
9413                       (funcall (if (string-match "%s" action)
9414                                    'format 'concat)
9415                                action
9416                                (mapconcat
9417                                 (lambda (f)
9418                                   (if (equal f " ")
9419                                       f
9420                                     (gnus-quote-arg-for-sh-or-csh f)))
9421                                 files " ")))))
9422           (setq ps (cdr ps)))))
9423     (if (and gnus-view-pseudos (not not-view))
9424         (while pslist
9425           (when (assq 'execute (car pslist))
9426             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9427                                   (eq gnus-view-pseudos 'not-confirm)))
9428           (setq pslist (cdr pslist)))
9429       (save-excursion
9430         (while pslist
9431           (setq after-article (or (cdr (assq 'article (car pslist)))
9432                                   (gnus-summary-article-number)))
9433           (gnus-summary-goto-subject after-article)
9434           (forward-line 1)
9435           (setq b (point))
9436           (insert "    " (file-name-nondirectory
9437                           (cdr (assq 'name (car pslist))))
9438                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9439           (setq e (point))
9440           (forward-line -1)             ; back to `b'
9441           (gnus-add-text-properties
9442            b (1- e) (list 'gnus-number gnus-reffed-article-number
9443                           gnus-mouse-face-prop gnus-mouse-face))
9444           (gnus-data-enter
9445            after-article gnus-reffed-article-number
9446            gnus-unread-mark b (car pslist) 0 (- e b))
9447           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9448           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9449           (setq pslist (cdr pslist)))))))
9450
9451 (defun gnus-pseudos< (p1 p2)
9452   (let ((c1 (cdr (assq 'action p1)))
9453         (c2 (cdr (assq 'action p2))))
9454     (and c1 c2 (string< c1 c2))))
9455
9456 (defun gnus-request-pseudo-article (props)
9457   (cond ((assq 'execute props)
9458          (gnus-execute-command (cdr (assq 'execute props)))))
9459   (let ((gnus-current-article (gnus-summary-article-number)))
9460     (gnus-run-hooks 'gnus-mark-article-hook)))
9461
9462 (defun gnus-execute-command (command &optional automatic)
9463   (save-excursion
9464     (gnus-article-setup-buffer)
9465     (set-buffer gnus-article-buffer)
9466     (setq buffer-read-only nil)
9467     (let ((command (if automatic command
9468                      (read-string "Command: " (cons command 0)))))
9469       (erase-buffer)
9470       (insert "$ " command "\n\n")
9471       (if gnus-view-pseudo-asynchronously
9472           (start-process "gnus-execute" (current-buffer) shell-file-name
9473                          shell-command-switch command)
9474         (call-process shell-file-name nil t nil
9475                       shell-command-switch command)))))
9476
9477 ;; Summary kill commands.
9478
9479 (defun gnus-summary-edit-global-kill (article)
9480   "Edit the \"global\" kill file."
9481   (interactive (list (gnus-summary-article-number)))
9482   (gnus-group-edit-global-kill article))
9483
9484 (defun gnus-summary-edit-local-kill ()
9485   "Edit a local kill file applied to the current newsgroup."
9486   (interactive)
9487   (setq gnus-current-headers (gnus-summary-article-header))
9488   (gnus-group-edit-local-kill
9489    (gnus-summary-article-number) gnus-newsgroup-name))
9490
9491 ;;; Header reading.
9492
9493 (defun gnus-read-header (id &optional header)
9494   "Read the headers of article ID and enter them into the Gnus system."
9495   (let ((group gnus-newsgroup-name)
9496         (gnus-override-method
9497          (or
9498           gnus-override-method
9499           (and (gnus-news-group-p gnus-newsgroup-name)
9500                (car (gnus-refer-article-methods)))))
9501         where)
9502     ;; First we check to see whether the header in question is already
9503     ;; fetched.
9504     (if (stringp id)
9505         ;; This is a Message-ID.
9506         (setq header (or header (gnus-id-to-header id)))
9507       ;; This is an article number.
9508       (setq header (or header (gnus-summary-article-header id))))
9509     (if (and header
9510              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9511         ;; We have found the header.
9512         header
9513       ;; If this is a sparse article, we have to nix out its
9514       ;; previous entry in the thread hashtb.
9515       (when (and header
9516                  (gnus-summary-article-sparse-p (mail-header-number header)))
9517         (let* ((parent (gnus-parent-id (mail-header-references header)))
9518                (thread (and parent (gnus-id-to-thread parent))))
9519           (when thread
9520             (delq (assq header thread) thread))))
9521       ;; We have to really fetch the header to this article.
9522       (save-excursion
9523         (set-buffer nntp-server-buffer)
9524         (when (setq where (gnus-request-head id group))
9525           (nnheader-fold-continuation-lines)
9526           (goto-char (point-max))
9527           (insert ".\n")
9528           (goto-char (point-min))
9529           (insert "211 ")
9530           (princ (cond
9531                   ((numberp id) id)
9532                   ((cdr where) (cdr where))
9533                   (header (mail-header-number header))
9534                   (t gnus-reffed-article-number))
9535                  (current-buffer))
9536           (insert " Article retrieved.\n"))
9537         (if (or (not where)
9538                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9539             ()                          ; Malformed head.
9540           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9541             (when (and (stringp id)
9542                        (not (string= (gnus-group-real-name group)
9543                                      (car where))))
9544               ;; If we fetched by Message-ID and the article came
9545               ;; from a different group, we fudge some bogus article
9546               ;; numbers for this article.
9547               (mail-header-set-number header gnus-reffed-article-number))
9548             (save-excursion
9549               (set-buffer gnus-summary-buffer)
9550               (decf gnus-reffed-article-number)
9551               (gnus-remove-header (mail-header-number header))
9552               (push header gnus-newsgroup-headers)
9553               (setq gnus-current-headers header)
9554               (push (mail-header-number header) gnus-newsgroup-limit)))
9555           header)))))
9556
9557 (defun gnus-remove-header (number)
9558   "Remove header NUMBER from `gnus-newsgroup-headers'."
9559   (if (and gnus-newsgroup-headers
9560            (= number (mail-header-number (car gnus-newsgroup-headers))))
9561       (pop gnus-newsgroup-headers)
9562     (let ((headers gnus-newsgroup-headers))
9563       (while (and (cdr headers)
9564                   (not (= number (mail-header-number (cadr headers)))))
9565         (pop headers))
9566       (when (cdr headers)
9567         (setcdr headers (cddr headers))))))
9568
9569 ;;;
9570 ;;; summary highlights
9571 ;;;
9572
9573 (defun gnus-highlight-selected-summary ()
9574   "Highlight selected article in summary buffer."
9575   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9576   (when gnus-summary-selected-face
9577     (save-excursion
9578       (let* ((beg (progn (beginning-of-line) (point)))
9579              (end (progn (end-of-line) (point)))
9580              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9581              (from (if (get-text-property beg gnus-mouse-face-prop)
9582                        beg
9583                      (or (next-single-property-change
9584                           beg gnus-mouse-face-prop nil end)
9585                          beg)))
9586              (to
9587               (if (= from end)
9588                   (- from 2)
9589                 (or (next-single-property-change
9590                      from gnus-mouse-face-prop nil end)
9591                     end))))
9592         ;; If no mouse-face prop on line we will have to = from = end,
9593         ;; so we highlight the entire line instead.
9594         (when (= (+ to 2) from)
9595           (setq from beg)
9596           (setq to end))
9597         (if gnus-newsgroup-selected-overlay
9598             ;; Move old overlay.
9599             (gnus-move-overlay
9600              gnus-newsgroup-selected-overlay from to (current-buffer))
9601           ;; Create new overlay.
9602           (gnus-overlay-put
9603            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9604            'face gnus-summary-selected-face))))))
9605
9606 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9607 (defun gnus-summary-highlight-line ()
9608   "Highlight current line according to `gnus-summary-highlight'."
9609   (let* ((list gnus-summary-highlight)
9610          (p (point))
9611          (end (progn (end-of-line) (point)))
9612          ;; now find out where the line starts and leave point there.
9613          (beg (progn (beginning-of-line) (point)))
9614          (article (gnus-summary-article-number))
9615          (score (or (cdr (assq (or article gnus-current-article)
9616                                gnus-newsgroup-scored))
9617                     gnus-summary-default-score 0))
9618          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9619          (inhibit-read-only t))
9620     ;; Eval the cars of the lists until we find a match.
9621     (let ((default gnus-summary-default-score))
9622       (while (and list
9623                   (not (eval (caar list))))
9624         (setq list (cdr list))))
9625     (let ((face (cdar list)))
9626       (unless (eq face (get-text-property beg 'face))
9627         (gnus-put-text-property-excluding-characters-with-faces
9628          beg end 'face
9629          (setq face (if (boundp face) (symbol-value face) face)))
9630         (when gnus-summary-highlight-line-function
9631           (funcall gnus-summary-highlight-line-function article face))))
9632     (goto-char p)))
9633
9634 (defun gnus-update-read-articles (group unread &optional compute)
9635   "Update the list of read articles in GROUP."
9636   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9637          (entry (gnus-gethash group gnus-newsrc-hashtb))
9638          (info (nth 2 entry))
9639          (prev 1)
9640          (unread (sort (copy-sequence unread) '<))
9641          read)
9642     (if (or (not info) (not active))
9643         ;; There is no info on this group if it was, in fact,
9644         ;; killed.  Gnus stores no information on killed groups, so
9645         ;; there's nothing to be done.
9646         ;; One could store the information somewhere temporarily,
9647         ;; perhaps...  Hmmm...
9648         ()
9649       ;; Remove any negative articles numbers.
9650       (while (and unread (< (car unread) 0))
9651         (setq unread (cdr unread)))
9652       ;; Remove any expired article numbers
9653       (while (and unread (< (car unread) (car active)))
9654         (setq unread (cdr unread)))
9655       ;; Compute the ranges of read articles by looking at the list of
9656       ;; unread articles.
9657       (while unread
9658         (when (/= (car unread) prev)
9659           (push (if (= prev (1- (car unread))) prev
9660                   (cons prev (1- (car unread))))
9661                 read))
9662         (setq prev (1+ (car unread)))
9663         (setq unread (cdr unread)))
9664       (when (<= prev (cdr active))
9665         (push (cons prev (cdr active)) read))
9666       (setq read (if (> (length read) 1) (nreverse read) read))
9667       (if compute
9668           read
9669         (save-excursion
9670           (let (setmarkundo)
9671             ;; Propagate the read marks to the backend.
9672             (when (gnus-check-backend-function 'request-set-mark group)
9673               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9674                     (add (gnus-remove-from-range read (gnus-info-read info))))
9675                 (when (or add del)
9676                   (unless (gnus-check-group group)
9677                     (error "Can't open server for %s" group))
9678                   (gnus-request-set-mark
9679                    group (delq nil (list (if add (list add 'add '(read)))
9680                                          (if del (list del 'del '(read))))))
9681                   (setq setmarkundo
9682                         `(gnus-request-set-mark
9683                           ,group
9684                           ',(delq nil (list
9685                                        (if del (list del 'add '(read)))
9686                                        (if add (list add 'del '(read))))))))))
9687             (set-buffer gnus-group-buffer)
9688             (gnus-undo-register
9689               `(progn
9690                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9691                  (gnus-info-set-read ',info ',(gnus-info-read info))
9692                  (gnus-get-unread-articles-in-group ',info 
9693                                                     (gnus-active ,group))
9694                  (gnus-group-update-group ,group t)
9695                  ,setmarkundo))))
9696         ;; Enter this list into the group info.
9697         (gnus-info-set-read info read)
9698         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9699         (gnus-get-unread-articles-in-group info (gnus-active group))
9700         t))))
9701
9702 (defun gnus-offer-save-summaries ()
9703   "Offer to save all active summary buffers."
9704   (save-excursion
9705     (let ((buflist (buffer-list))
9706           buffers bufname)
9707       ;; Go through all buffers and find all summaries.
9708       (while buflist
9709         (and (setq bufname (buffer-name (car buflist)))
9710              (string-match "Summary" bufname)
9711              (save-excursion
9712                (set-buffer bufname)
9713                ;; We check that this is, indeed, a summary buffer.
9714                (and (eq major-mode 'gnus-summary-mode)
9715                     ;; Also make sure this isn't bogus.
9716                     gnus-newsgroup-prepared
9717                     ;; Also make sure that this isn't a dead summary buffer.
9718                     (not gnus-dead-summary-mode)))
9719              (push bufname buffers))
9720         (setq buflist (cdr buflist)))
9721       ;; Go through all these summary buffers and offer to save them.
9722       (when buffers
9723         (map-y-or-n-p
9724          "Update summary buffer %s? "
9725          (lambda (buf)
9726            (switch-to-buffer buf)
9727            (gnus-summary-exit))
9728          buffers)))))
9729
9730
9731 ;;; @ for mime-partial
9732 ;;;
9733
9734 (defun gnus-request-partial-message ()
9735   (save-excursion
9736     (let ((number (gnus-summary-article-number))
9737           (group gnus-newsgroup-name)
9738           (mother gnus-article-buffer))
9739       (set-buffer (get-buffer-create " *Partial Article*"))
9740       (erase-buffer)
9741       (setq mime-preview-buffer mother)
9742       (gnus-request-article-this-buffer number group)
9743       (mime-parse-buffer)
9744       )))
9745
9746 (autoload 'mime-combine-message/partial-pieces-automatically
9747   "mime-partial"
9748   "Internal method to combine message/partial messages automatically.")
9749
9750 (mime-add-condition
9751  'action '((type . message)(subtype . partial)
9752            (major-mode . gnus-original-article-mode)
9753            (method . mime-combine-message/partial-pieces-automatically)
9754            (summary-buffer-exp . gnus-summary-buffer)
9755            (request-partial-message-method . gnus-request-partial-message)
9756            ))
9757
9758
9759 ;;; @ for message/rfc822
9760 ;;;
9761
9762 (defun gnus-mime-extract-message/rfc822 (entity situation)
9763   (let (group article num cwin swin cur)
9764     (with-temp-buffer
9765       (mime-insert-entity-content entity)
9766       (setq group (or (cdr (assq 'group situation))
9767                       (completing-read "Group: "
9768                                        gnus-active-hashtb
9769                                        nil
9770                                        (gnus-read-active-file-p)
9771                                        gnus-newsgroup-name))
9772             article (gnus-request-accept-article group)))
9773     (when (and (consp article)
9774                (numberp (setq article (cdr article))))
9775       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9776             cwin (get-buffer-window (current-buffer) t))
9777       (save-window-excursion
9778         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9779             (select-window swin)
9780           (set-buffer gnus-summary-buffer))
9781         (setq cur gnus-current-article)
9782         (forward-line num)
9783         (let (gnus-show-threads)
9784           (gnus-summary-goto-subject article t))
9785         (gnus-summary-clear-mark-forward 1)
9786         (gnus-summary-goto-subject cur))
9787       (when (and cwin (window-frame cwin))
9788         (select-frame (window-frame cwin)))
9789       (when (boundp 'mime-acting-situation-to-override)
9790         (set-alist 'mime-acting-situation-to-override
9791                    'group
9792                    group)
9793         (set-alist 'mime-acting-situation-to-override
9794                    'after-method
9795                    `(progn
9796                       (save-current-buffer
9797                         (set-buffer gnus-group-buffer)
9798                         (gnus-activate-group ,group))
9799                       (gnus-summary-goto-article ,cur
9800                                                  gnus-show-all-headers)))
9801         (set-alist 'mime-acting-situation-to-override
9802                    'number num)))))
9803
9804 (mime-add-condition
9805  'action '((type . message)(subtype . rfc822)
9806            (major-mode . gnus-original-article-mode)
9807            (method . gnus-mime-extract-message/rfc822)
9808            (mode . "extract")
9809            ))
9810
9811 (mime-add-condition
9812  'action '((type . message)(subtype . news)
9813            (major-mode . gnus-original-article-mode)
9814            (method . gnus-mime-extract-message/rfc822)
9815            (mode . "extract")
9816            ))
9817
9818 (defun gnus-mime-extract-multipart (entity situation)
9819   (let ((children (mime-entity-children entity))
9820         mime-acting-situation-to-override
9821         f)
9822     (while children
9823       (mime-play-entity (car children)
9824                         (cons (assq 'mode situation)
9825                               mime-acting-situation-to-override))
9826       (setq children (cdr children)))
9827     (if (setq f (cdr (assq 'after-method
9828                            mime-acting-situation-to-override)))
9829         (eval f)
9830       )))
9831
9832 (mime-add-condition
9833  'action '((type . multipart)
9834            (method . gnus-mime-extract-multipart)
9835            (mode . "extract")
9836            )
9837  'with-default)
9838
9839
9840 ;;; @ end
9841 ;;;
9842
9843 (defun gnus-summary-setup-default-charset ()
9844   "Setup newsgroup default charset."
9845   (if (equal gnus-newsgroup-name "nndraft:drafts")
9846       (setq gnus-newsgroup-charset nil)
9847     (let* ((name (and gnus-newsgroup-name
9848                       (gnus-group-real-name gnus-newsgroup-name)))
9849            (ignored-charsets
9850             (or gnus-newsgroup-ephemeral-ignored-charsets
9851                 (append
9852                  (and gnus-newsgroup-name
9853                       (or (gnus-group-find-parameter gnus-newsgroup-name
9854                                                      'ignored-charsets t)
9855                           (let ((alist gnus-group-ignored-charsets-alist)
9856                                 elem (charsets nil))
9857                             (while (setq elem (pop alist))
9858                               (when (and name
9859                                          (string-match (car elem) name))
9860                                 (setq alist nil
9861                                       charsets (cdr elem))))
9862                             charsets)))
9863                  gnus-newsgroup-ignored-charsets))))
9864       (setq gnus-newsgroup-charset
9865             (or gnus-newsgroup-ephemeral-charset
9866                 (and gnus-newsgroup-name
9867                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9868                          (let ((alist gnus-group-charset-alist)
9869                                elem charset)
9870                            (while (setq elem (pop alist))
9871                              (when (and name
9872                                         (string-match (car elem) name))
9873                                (setq alist nil
9874                                      charset (cadr elem))))
9875                            charset)))
9876                 gnus-default-charset))
9877       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9878            ignored-charsets))))
9879
9880 ;;;
9881 ;;; Mime Commands
9882 ;;;
9883
9884 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9885   "Display the current article buffer fully MIME-buttonized.
9886 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9887 treated as multipart/mixed."
9888   (interactive "P")
9889   (require 'gnus-art)
9890   (let ((gnus-unbuttonized-mime-types nil)
9891         (gnus-mime-display-multipart-as-mixed show-all-parts))
9892     (gnus-summary-show-article)))
9893
9894 (defun gnus-summary-repair-multipart (article)
9895   "Add a Content-Type header to a multipart article without one."
9896   (interactive (list (gnus-summary-article-number)))
9897   (gnus-with-article article
9898     (message-narrow-to-head)
9899     (goto-char (point-max))
9900     (widen)
9901     (when (search-forward "\n--" nil t)
9902       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9903         (message-narrow-to-head)
9904         (message-remove-header "Mime-Version")
9905         (message-remove-header "Content-Type")
9906         (goto-char (point-max))
9907         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9908                         separator))
9909         (insert "Mime-Version: 1.0\n")
9910         (widen))))
9911   (let (gnus-mark-article-hook)
9912     (gnus-summary-select-article t t nil article)))
9913
9914 (defun gnus-summary-toggle-display-buttonized ()
9915   "Toggle the buttonizing of the article buffer."
9916   (interactive)
9917   (require 'gnus-art)
9918   (if (setq gnus-inhibit-mime-unbuttonizing
9919             (not gnus-inhibit-mime-unbuttonizing))
9920       (let ((gnus-unbuttonized-mime-types nil))
9921         (gnus-summary-show-article))
9922     (gnus-summary-show-article)))
9923
9924 ;;;
9925 ;;; Intelli-mouse commmands
9926 ;;;
9927
9928 (defun gnus-wheel-summary-scroll (event)
9929   (interactive "e")
9930   (let ((amount (if (memq 'shift (event-modifiers event))
9931                     (car gnus-wheel-scroll-amount)
9932                   (cdr gnus-wheel-scroll-amount)))
9933         (direction (- (* (static-if (featurep 'xemacs)
9934                              (event-button event)
9935                            (cond ((eq 'mouse-4 (event-basic-type event))
9936                                   4)
9937                                  ((eq 'mouse-5 (event-basic-type event))
9938                                   5)))
9939                          2) 9))
9940         edge)
9941     (gnus-summary-scroll-up (* amount direction))
9942     (when (gnus-eval-in-buffer-window gnus-article-buffer
9943             (save-restriction
9944               (widen)
9945               (and (if (< 0 direction)
9946                        (gnus-article-next-page 0)
9947                      (gnus-article-prev-page 0)
9948                      (bobp))
9949                    (if (setq edge (get-text-property
9950                                    (point-min) 'gnus-wheel-edge))
9951                        (setq edge (* edge direction))
9952                      (setq edge -1))
9953                    (or (plusp edge)
9954                        (let ((buffer-read-only nil)
9955                              (inhibit-read-only t))
9956                          (put-text-property (point-min) (point-max)
9957                                             'gnus-wheel-edge direction)
9958                          nil))
9959                    (or (> edge gnus-wheel-edge-resistance)
9960                        (let ((buffer-read-only nil)
9961                              (inhibit-read-only t))
9962                          (put-text-property (point-min) (point-max)
9963                                             'gnus-wheel-edge
9964                                             (* (1+ edge) direction))
9965                          nil))
9966                    (eq last-command 'gnus-wheel-summary-scroll))))
9967       (gnus-summary-next-article nil nil (minusp direction)))))
9968
9969 (defun gnus-wheel-install ()
9970   "Enable mouse wheel support on summary window."
9971   (when gnus-use-wheel
9972     (let ((keys
9973            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9974       (dolist (key keys)
9975         (define-key gnus-summary-mode-map key
9976           'gnus-wheel-summary-scroll)))))
9977
9978 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9979
9980 ;;;
9981 ;;; Traditional PGP commmands
9982 ;;;
9983
9984 (defun gnus-summary-decrypt-article (&optional force)
9985   "Decrypt the current article in traditional PGP way.
9986 This will have permanent effect only in mail groups.
9987 If FORCE is non-nil, allow editing of articles even in read-only
9988 groups."
9989   (interactive "P")
9990   (gnus-summary-select-article t)
9991   (gnus-eval-in-buffer-window gnus-article-buffer
9992     (save-excursion
9993       (save-restriction
9994         (widen)
9995         (goto-char (point-min))
9996         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9997           (error "Not a traditional PGP message!"))
9998         (let ((armor-start (match-beginning 0)))
9999           (if (and (pgg-decrypt-region armor-start (point-max))
10000                    (or force (not (gnus-group-read-only-p))))
10001               (let ((inhibit-read-only t)
10002                     buffer-read-only)
10003                 (delete-region armor-start
10004                                (progn
10005                                  (re-search-forward "^-+END PGP" nil t)
10006                                  (beginning-of-line 2)
10007                                  (point)))
10008                 (insert-buffer-substring pgg-output-buffer))))))))
10009
10010 (defun gnus-summary-verify-article ()
10011   "Verify the current article in traditional PGP way."
10012   (interactive)
10013   (save-excursion
10014     (set-buffer gnus-original-article-buffer)
10015     (goto-char (point-min))
10016     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10017       (error "Not a traditional PGP message!"))
10018     (re-search-forward "^-+END PGP" nil t)
10019     (beginning-of-line 2)
10020     (call-interactively (function pgg-verify-region))))
10021
10022 ;;;
10023 ;;; Generic summary marking commands
10024 ;;;
10025
10026 (defvar gnus-summary-marking-alist
10027   '((read gnus-del-mark "d")
10028     (unread gnus-unread-mark "u")
10029     (ticked gnus-ticked-mark "!")
10030     (dormant gnus-dormant-mark "?")
10031     (expirable gnus-expirable-mark "e"))
10032   "An alist of names/marks/keystrokes.")
10033
10034 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10035 (defvar gnus-summary-mark-map)
10036
10037 (defun gnus-summary-make-all-marking-commands ()
10038   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10039   (dolist (elem gnus-summary-marking-alist)
10040     (apply 'gnus-summary-make-marking-command elem)))
10041
10042 (defun gnus-summary-make-marking-command (name mark keystroke)
10043   (let ((map (make-sparse-keymap)))
10044     (define-key gnus-summary-generic-mark-map keystroke map)
10045     (dolist (lway `((next "next" next nil "n")
10046                     (next-unread "next unread" next t "N")
10047                     (prev "previous" prev nil "p")
10048                     (prev-unread "previous unread" prev t "P")
10049                     (nomove "" nil nil ,keystroke)))
10050       (let ((func (gnus-summary-make-marking-command-1
10051                    mark (car lway) lway name)))
10052         (setq func (eval func))
10053         (define-key map (nth 4 lway) func)))))
10054
10055 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10056   `(defun ,(intern
10057             (format "gnus-summary-put-mark-as-%s%s"
10058                     name (if (eq way 'nomove)
10059                              ""
10060                            (concat "-" (symbol-name way)))))
10061      (n)
10062      ,(format
10063        "Mark the current article as %s%s.
10064 If N, the prefix, then repeat N times.
10065 If N is negative, move in reverse order.
10066 The difference between N and the actual number of articles marked is
10067 returned."
10068        name (car (cdr lway)))
10069      (interactive "p")
10070      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10071
10072 (defun gnus-summary-generic-mark (n mark move unread)
10073   "Mark N articles with MARK."
10074   (unless (eq major-mode 'gnus-summary-mode)
10075     (error "This command can only be used in the summary buffer"))
10076   (gnus-summary-show-thread)
10077   (let ((nummove
10078          (cond
10079           ((eq move 'next) 1)
10080           ((eq move 'prev) -1)
10081           (t 0))))
10082     (if (zerop nummove)
10083         (setq n 1)
10084       (when (< n 0)
10085         (setq n (abs n)
10086               nummove (* -1 nummove))))
10087     (while (and (> n 0)
10088                 (gnus-summary-mark-article nil mark)
10089                 (zerop (gnus-summary-next-subject nummove unread t)))
10090       (setq n (1- n)))
10091     (when (/= 0 n)
10092       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10093     (gnus-summary-recenter)
10094     (gnus-summary-position-point)
10095     (gnus-set-mode-line 'summary)
10096     n))
10097
10098 (gnus-summary-make-all-marking-commands)
10099
10100 (gnus-ems-redefine)
10101
10102 (provide 'gnus-sum)
10103
10104 (run-hooks 'gnus-sum-load-hook)
10105
10106 ;;; gnus-sum.el ends here