T-gnus 6.15.16 r00.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'nnoo)
42 (require 'mime-view)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-fetch-old-headers nil
72   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
73 If an unread article in the group refers to an older, already read (or
74 just marked as read) article, the old article will not normally be
75 displayed in the Summary buffer.  If this variable is non-nil, Gnus
76 will attempt to grab the headers to the old articles, and thereby
77 build complete threads.  If it has the value `some', only enough
78 headers to connect otherwise loose threads will be displayed.  This
79 variable can also be a number.  In that case, no more than that number
80 of old headers will be fetched.  If it has the value `invisible', all
81 old headers will be fetched, but none will be displayed.
82
83 The server has to support NOV for any of this to work."
84   :group 'gnus-thread
85   :type '(choice (const :tag "off" nil)
86                  (const some)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 200
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :group 'gnus-thread
129   :type 'boolean)
130
131 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
132   "*A regexp to match subjects to be excluded from loose thread gathering.
133 As loose thread gathering is done on subjects only, that means that
134 there can be many false gatherings performed.  By rooting out certain
135 common subjects, gathering might become saner."
136   :group 'gnus-thread
137   :type 'regexp)
138
139 (defcustom gnus-summary-gather-subject-limit nil
140   "*Maximum length of subject comparisons when gathering loose threads.
141 Use nil to compare full subjects.  Setting this variable to a low
142 number will help gather threads that have been corrupted by
143 newsreaders chopping off subject lines, but it might also mean that
144 unrelated articles that have subject that happen to begin with the
145 same few characters will be incorrectly gathered.
146
147 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
148 comparing subjects."
149   :group 'gnus-thread
150   :type '(choice (const :tag "off" nil)
151                  (const fuzzy)
152                  (sexp :menu-tag "on" t)))
153
154 (defcustom gnus-simplify-subject-functions nil
155   "List of functions taking a string argument that simplify subjects.
156 The functions are applied recursively.
157
158 Useful functions to put in this list include:
159 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
160 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
161   :group 'gnus-thread
162   :type '(repeat function))
163
164 (defcustom gnus-simplify-ignored-prefixes nil
165   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  regexp))
169
170 (defcustom gnus-build-sparse-threads nil
171   "*If non-nil, fill in the gaps in threads.
172 If `some', only fill in the gaps that are needed to tie loose threads
173 together.  If `more', fill in all leaf nodes that Gnus can find.  If
174 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
175   :group 'gnus-thread
176   :type '(choice (const :tag "off" nil)
177                  (const some)
178                  (const more)
179                  (sexp :menu-tag "all" t)))
180
181 (defcustom gnus-summary-thread-gathering-function
182   'gnus-gather-threads-by-subject
183   "*Function used for gathering loose threads.
184 There are two pre-defined functions: `gnus-gather-threads-by-subject',
185 which only takes Subjects into consideration; and
186 `gnus-gather-threads-by-references', which compared the References
187 headers of the articles to find matches."
188   :group 'gnus-thread
189   :type '(radio (function-item gnus-gather-threads-by-subject)
190                 (function-item gnus-gather-threads-by-references)
191                 (function :tag "other")))
192
193 (defcustom gnus-summary-same-subject ""
194   "*String indicating that the current article has the same subject as the previous.
195 This variable will only be used if the value of
196 `gnus-summary-make-false-root' is `empty'."
197   :group 'gnus-summary-format
198   :type 'string)
199
200 (defcustom gnus-summary-goto-unread t
201   "*If t, many commands will go to the next unread article.
202 This applies to marking commands as well as other commands that
203 \"naturally\" select the next article, like, for instance, `SPC' at
204 the end of an article.
205
206 If nil, the marking commands do NOT go to the next unread article
207 \(they go to the next article instead).  If `never', commands that
208 usually go to the next unread article, will go to the next article,
209 whether it is read or not."
210   :group 'gnus-summary-marks
211   :link '(custom-manual "(gnus)Setting Marks")
212   :type '(choice (const :tag "off" nil)
213                  (const never)
214                  (sexp :menu-tag "on" t)))
215
216 (defcustom gnus-summary-default-score 0
217   "*Default article score level.
218 All scores generated by the score files will be added to this score.
219 If this variable is nil, scoring will be disabled."
220   :group 'gnus-score-default
221   :type '(choice (const :tag "disable")
222                  integer))
223
224 (defcustom gnus-summary-default-high-score 0
225   "*Default threshold for a high scored article.
226 An article will be highlighted as high scored if its score is greater
227 than this score."
228   :group 'gnus-score-default
229   :type 'integer)
230
231 (defcustom gnus-summary-default-low-score 0
232   "*Default threshold for a low scored article.
233 An article will be highlighted as low scored if its score is smaller
234 than this score."
235   :group 'gnus-score-default
236   :type 'integer)
237
238 (defcustom gnus-summary-zcore-fuzz 0
239   "*Fuzziness factor for the zcore in the summary buffer.
240 Articles with scores closer than this to `gnus-summary-default-score'
241 will not be marked."
242   :group 'gnus-summary-format
243   :type 'integer)
244
245 (defcustom gnus-simplify-subject-fuzzy-regexp nil
246   "*Strings to be removed when doing fuzzy matches.
247 This can either be a regular expression or list of regular expressions
248 that will be removed from subject strings if fuzzy subject
249 simplification is selected."
250   :group 'gnus-thread
251   :type '(repeat regexp))
252
253 (defcustom gnus-show-threads t
254   "*If non-nil, display threads in summary mode."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-subtree nil
259   "*If non-nil, hide all threads initially.
260 This can be a predicate specifier which says which threads to hide.
261 If threads are hidden, you have to run the command
262 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
263 to expose hidden threads."
264   :group 'gnus-thread
265   :type 'boolean)
266
267 (defcustom gnus-thread-hide-killed t
268   "*If non-nil, hide killed threads automatically."
269   :group 'gnus-thread
270   :type 'boolean)
271
272 (defcustom gnus-thread-ignore-subject t
273   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
274 If nil, articles that have different subjects from their parents will
275 start separate threads."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-operation-ignore-subject t
280   "*If non-nil, subjects will be ignored when doing thread commands.
281 This affects commands like `gnus-summary-kill-thread' and
282 `gnus-summary-lower-thread'.
283
284 If this variable is nil, articles in the same thread with different
285 subjects will not be included in the operation in question.  If this
286 variable is `fuzzy', only articles that have subjects that are fuzzily
287 equal will be included."
288   :group 'gnus-thread
289   :type '(choice (const :tag "off" nil)
290                  (const fuzzy)
291                  (sexp :tag "on" t)))
292
293 (defcustom gnus-thread-indent-level 4
294   "*Number that says how much each sub-thread should be indented."
295   :group 'gnus-thread
296   :type 'integer)
297
298 (defcustom gnus-auto-extend-newsgroup t
299   "*If non-nil, extend newsgroup forward and backward when requested."
300   :group 'gnus-summary-choose
301   :type 'boolean)
302
303 (defcustom gnus-auto-select-first t
304   "*If non-nil, select the article under point.
305 Which article this is is controlled by the `gnus-auto-select-subject'
306 variable.
307
308 If you want to prevent automatic selection of articles in some
309 newsgroups, set the variable to nil in `gnus-select-group-hook'."
310   :group 'gnus-group-select
311   :type '(choice (const :tag "none" nil)
312                  (sexp :menu-tag "first" t)))
313
314 (defcustom gnus-auto-select-subject 'unread
315   "*Says what subject to place under point when entering a group.
316
317 This variable can either be the symbols `first' (place point on the
318 first subject), `unread' (place point on the subject line of the first
319 unread article), `best' (place point on the subject line of the
320 higest-scored article), `unseen' (place point on the subject line of
321 the first unseen article), 'unseen-or-unread' (place point on the subject
322 line of the first unseen article or, if all article have been seen, on the
323 subject line of the first unread article), or a function to be called to
324 place point on some subject line."
325   :group 'gnus-group-select
326   :type '(choice (const best)
327                  (const unread)
328                  (const first)
329                  (const unseen)
330                  (const unseen-or-unread)))
331
332 (defcustom gnus-dont-select-after-jump-to-other-group nil
333   "If non-nil, don't select the first unread article after entering the
334 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
335 it is depend on the value of `gnus-auto-select-first' whether to select
336 or not."
337   :group 'gnus-group-select
338   :type 'boolean)
339
340 (defcustom gnus-auto-select-next t
341   "*If non-nil, offer to go to the next group from the end of the previous.
342 If the value is t and the next newsgroup is empty, Gnus will exit
343 summary mode and go back to group mode.  If the value is neither nil
344 nor t, Gnus will select the following unread newsgroup.  In
345 particular, if the value is the symbol `quietly', the next unread
346 newsgroup will be selected without any confirmation, and if it is
347 `almost-quietly', the next group will be selected without any
348 confirmation if you are located on the last article in the group.
349 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
350 will go to the next group without confirmation."
351   :group 'gnus-summary-maneuvering
352   :type '(choice (const :tag "off" nil)
353                  (const quietly)
354                  (const almost-quietly)
355                  (const slightly-quietly)
356                  (sexp :menu-tag "on" t)))
357
358 (defcustom gnus-auto-select-same nil
359   "*If non-nil, select the next article with the same subject.
360 If there are no more articles with the same subject, go to
361 the first unread article."
362   :group 'gnus-summary-maneuvering
363   :type 'boolean)
364
365 (defcustom gnus-auto-goto-ignores 'unfetched
366   "*Says how to handle unfetched articles when maneuvering.
367
368 This variable can either be the symbols `nil' (maneuver to any
369 article), `undownloaded' (maneuvering while unplugged ignores articles
370 that have not been fetched), `always-undownloaded' (maneuvering always
371 ignores articles that have not been fetched), `unfetched' (maneuvering
372 ignores articles whose headers have not been fetched).
373
374 NOTE: The list of unfetched articles will always be nil when plugged
375 and, when unplugged, a subset of the undownloaded article list."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "None" nil)
378                  (const :tag "Undownloaded when unplugged" undownloaded)
379                  (const :tag "Undownloaded" always-undownloaded)
380                  (const :tag "Unfetched" unfetched)))
381
382 (defcustom gnus-summary-check-current nil
383   "*If non-nil, consider the current article when moving.
384 The \"unread\" movement commands will stay on the same line if the
385 current article is unread."
386   :group 'gnus-summary-maneuvering
387   :type 'boolean)
388
389 (defcustom gnus-auto-center-summary t
390   "*If non-nil, always center the current summary buffer.
391 In particular, if `vertical' do only vertical recentering.  If non-nil
392 and non-`vertical', do both horizontal and vertical recentering."
393   :group 'gnus-summary-maneuvering
394   :type '(choice (const :tag "none" nil)
395                  (const vertical)
396                  (integer :tag "height")
397                  (sexp :menu-tag "both" t)))
398
399 (defvar gnus-auto-center-group t
400   "*If non-nil, always center the group buffer.")
401
402 (defcustom gnus-show-all-headers nil
403   "*If non-nil, don't hide any headers."
404   :group 'gnus-article-hiding
405   :group 'gnus-article-headers
406   :type 'boolean)
407
408 (defcustom gnus-summary-ignore-duplicates nil
409   "*If non-nil, ignore articles with identical Message-ID headers."
410   :group 'gnus-summary
411   :type 'boolean)
412
413 (defcustom gnus-single-article-buffer t
414   "*If non-nil, display all articles in the same buffer.
415 If nil, each group will get its own article buffer."
416   :group 'gnus-article-various
417   :type 'boolean)
418
419 (defcustom gnus-break-pages t
420   "*If non-nil, do page breaking on articles.
421 The page delimiter is specified by the `gnus-page-delimiter'
422 variable."
423   :group 'gnus-article-various
424   :type 'boolean)
425
426 (defcustom gnus-show-mime t
427   "*If non-nil, do mime processing of articles.
428 The articles will simply be fed to the function given by
429 `gnus-article-display-method-for-mime'."
430   :group 'gnus-article-mime
431   :type 'boolean)
432
433 (defcustom gnus-move-split-methods nil
434   "*Variable used to suggest where articles are to be moved to.
435 It uses the same syntax as the `gnus-split-methods' variable.
436 However, whereas `gnus-split-methods' specifies file names as targets,
437 this variable specifies group names."
438   :group 'gnus-summary-mail
439   :type '(repeat (choice (list :value (fun) function)
440                          (cons :value ("" "") regexp (repeat string))
441                          (sexp :value nil))))
442
443 (defcustom gnus-unread-mark ?\ ;;;Whitespace
444   "*Mark used for unread articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-ticked-mark ?!
449   "*Mark used for ticked articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-dormant-mark ??
454   "*Mark used for dormant articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-del-mark ?r
459   "*Mark used for del'd articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-read-mark ?R
464   "*Mark used for read articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-expirable-mark ?E
469   "*Mark used for expirable articles."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-killed-mark ?K
474   "*Mark used for killed articles."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-spam-mark ?$
479   "*Mark used for spam articles."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-souped-mark ?F
484   "*Mark used for souped articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-kill-file-mark ?X
489   "*Mark used for articles killed by kill files."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-low-score-mark ?Y
494   "*Mark used for articles with a low score."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-catchup-mark ?C
499   "*Mark used for articles that are caught up."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-replied-mark ?A
504   "*Mark used for articles that have been replied to."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-forwarded-mark ?F
509   "*Mark used for articles that have been forwarded."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-recent-mark ?N
514   "*Mark used for articles that are recent."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-cached-mark ?*
519   "*Mark used for articles that are in the cache."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-saved-mark ?S
524   "*Mark used for articles that have been saved."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-unseen-mark ?.
529   "*Mark used for articles that haven't been seen."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-no-mark ?\ ;;;Whitespace
534   "*Mark used for articles that have no other secondary mark."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-ancient-mark ?O
539   "*Mark used for ancient articles."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-sparse-mark ?Q
544   "*Mark used for sparsely reffed articles."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-canceled-mark ?G
549   "*Mark used for canceled articles."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-duplicate-mark ?M
554   "*Mark used for duplicate articles."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-undownloaded-mark ?-
559   "*Mark used for articles that weren't downloaded."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-downloaded-mark ?+
564   "*Mark used for articles that were downloaded."
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-downloadable-mark ?%
569   "*Mark used for articles that are to be downloaded."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-unsendable-mark ?=
574   "*Mark used for articles that won't be sent."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-score-over-mark ?+
579   "*Score mark used for articles with high scores."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-score-below-mark ?-
584   "*Score mark used for articles with low scores."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
589   "*There is no thread under the article."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-not-empty-thread-mark ?=
594   "*There is a thread under the article."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-view-pseudo-asynchronously nil
599   "*If non-nil, Gnus will view pseudo-articles asynchronously."
600   :group 'gnus-extract-view
601   :type 'boolean)
602
603 (defcustom gnus-auto-expirable-marks
604   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
605         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
606         gnus-souped-mark gnus-duplicate-mark)
607   "*The list of marks converted into expiration if a group is auto-expirable."
608   :version "21.1"
609   :group 'gnus-summary
610   :type '(repeat character))
611
612 (defcustom gnus-inhibit-user-auto-expire t
613   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
614   :version "21.1"
615   :group 'gnus-summary
616   :type 'boolean)
617
618 (defcustom gnus-view-pseudos nil
619   "*If `automatic', pseudo-articles will be viewed automatically.
620 If `not-confirm', pseudos will be viewed automatically, and the user
621 will not be asked to confirm the command."
622   :group 'gnus-extract-view
623   :type '(choice (const :tag "off" nil)
624                  (const automatic)
625                  (const not-confirm)))
626
627 (defcustom gnus-view-pseudos-separately t
628   "*If non-nil, one pseudo-article will be created for each file to be viewed.
629 If nil, all files that use the same viewing command will be given as a
630 list of parameters to that command."
631   :group 'gnus-extract-view
632   :type 'boolean)
633
634 (defcustom gnus-insert-pseudo-articles t
635   "*If non-nil, insert pseudo-articles when decoding articles."
636   :group 'gnus-extract-view
637   :type 'boolean)
638
639 (defcustom gnus-summary-dummy-line-format
640   "   %(:                             :%) %S\n"
641   "*The format specification for the dummy roots in the summary buffer.
642 It works along the same lines as a normal formatting string,
643 with some simple extensions.
644
645 %S  The subject
646
647 General format specifiers can also be used.
648 See `(gnus)Formatting Variables'."
649   :link '(custom-manual "(gnus)Formatting Variables")
650   :group 'gnus-threading
651   :type 'string)
652
653 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
654   "*The format specification for the summary mode line.
655 It works along the same lines as a normal formatting string,
656 with some simple extensions:
657
658 %G  Group name
659 %p  Unprefixed group name
660 %A  Current article number
661 %z  Current article score
662 %V  Gnus version
663 %U  Number of unread articles in the group
664 %e  Number of unselected articles in the group
665 %Z  A string with unread/unselected article counts
666 %g  Shortish group name
667 %S  Subject of the current article
668 %u  User-defined spec
669 %s  Current score file name
670 %d  Number of dormant articles
671 %r  Number of articles that have been marked as read in this session
672 %E  Number of articles expunged by the score files"
673   :group 'gnus-summary-format
674   :type 'string)
675
676 (defcustom gnus-list-identifiers nil
677   "Regexp that matches list identifiers to be removed from subject.
678 This can also be a list of regexps."
679   :version "21.1"
680   :group 'gnus-summary-format
681   :group 'gnus-article-hiding
682   :type '(choice (const :tag "none" nil)
683                  (regexp :value ".*")
684                  (repeat :value (".*") regexp)))
685
686 (defcustom gnus-summary-mark-below 0
687   "*Mark all articles with a score below this variable as read.
688 This variable is local to each summary buffer and usually set by the
689 score file."
690   :group 'gnus-score-default
691   :type 'integer)
692
693 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
694   "*List of functions used for sorting articles in the summary buffer.
695
696 Each function takes two articles and returns non-nil if the first
697 article should be sorted before the other.  If you use more than one
698 function, the primary sort function should be the last.  You should
699 probably always include `gnus-article-sort-by-number' in the list of
700 sorting functions -- preferably first.  Also note that sorting by date
701 is often much slower than sorting by number, and the sorting order is
702 very similar.  (Sorting by date means sorting by the time the message
703 was sent, sorting by number means sorting by arrival time.)
704
705 Ready-made functions include `gnus-article-sort-by-number',
706 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
707 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
708 and `gnus-article-sort-by-score'.
709
710 When threading is turned on, the variable `gnus-thread-sort-functions'
711 controls how articles are sorted."
712   :group 'gnus-summary-sort
713   :type '(repeat (choice (function-item gnus-article-sort-by-number)
714                          (function-item gnus-article-sort-by-author)
715                          (function-item gnus-article-sort-by-subject)
716                          (function-item gnus-article-sort-by-date)
717                          (function-item gnus-article-sort-by-score)
718                          (function-item gnus-article-sort-by-random)
719                          (function :tag "other"))))
720
721 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
722   "*List of functions used for sorting threads in the summary buffer.
723 By default, threads are sorted by article number.
724
725 Each function takes two threads and returns non-nil if the first
726 thread should be sorted before the other.  If you use more than one
727 function, the primary sort function should be the last.  You should
728 probably always include `gnus-thread-sort-by-number' in the list of
729 sorting functions -- preferably first.  Also note that sorting by date
730 is often much slower than sorting by number, and the sorting order is
731 very similar.  (Sorting by date means sorting by the time the message
732 was sent, sorting by number means sorting by arrival time.)
733
734 Ready-made functions include `gnus-thread-sort-by-number',
735 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
736 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
737 `gnus-thread-sort-by-most-recent-number',
738 `gnus-thread-sort-by-most-recent-date',
739 `gnus-thread-sort-by-random', and
740 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
741
742 When threading is turned off, the variable
743 `gnus-article-sort-functions' controls how articles are sorted."
744   :group 'gnus-summary-sort
745   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
746                          (function-item gnus-thread-sort-by-author)
747                          (function-item gnus-thread-sort-by-subject)
748                          (function-item gnus-thread-sort-by-date)
749                          (function-item gnus-thread-sort-by-score)
750                          (function-item gnus-thread-sort-by-total-score)
751                          (function-item gnus-thread-sort-by-random)
752                          (function :tag "other"))))
753
754 (defcustom gnus-thread-score-function '+
755   "*Function used for calculating the total score of a thread.
756
757 The function is called with the scores of the article and each
758 subthread and should then return the score of the thread.
759
760 Some functions you can use are `+', `max', or `min'."
761   :group 'gnus-summary-sort
762   :type 'function)
763
764 (defcustom gnus-summary-expunge-below nil
765   "All articles that have a score less than this variable will be expunged.
766 This variable is local to the summary buffers."
767   :group 'gnus-score-default
768   :type '(choice (const :tag "off" nil)
769                  integer))
770
771 (defcustom gnus-thread-expunge-below nil
772   "All threads that have a total score less than this variable will be expunged.
773 See `gnus-thread-score-function' for en explanation of what a
774 \"thread score\" is.
775
776 This variable is local to the summary buffers."
777   :group 'gnus-threading
778   :group 'gnus-score-default
779   :type '(choice (const :tag "off" nil)
780                  integer))
781
782 (defcustom gnus-summary-mode-hook nil
783   "*A hook for Gnus summary mode.
784 This hook is run before any variables are set in the summary buffer."
785   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
790 (when (featurep 'xemacs)
791   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
792   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
793   (add-hook 'gnus-summary-mode-hook
794             'gnus-xmas-switch-horizontal-scrollbar-off))
795
796 (defcustom gnus-summary-menu-hook nil
797   "*Hook run after the creation of the summary mode menu."
798   :group 'gnus-summary-visual
799   :type 'hook)
800
801 (defcustom gnus-summary-exit-hook nil
802   "*A hook called on exit from the summary buffer.
803 It will be called with point in the group buffer."
804   :group 'gnus-summary-exit
805   :type 'hook)
806
807 (defcustom gnus-summary-prepare-hook nil
808   "*A hook called after the summary buffer has been generated.
809 If you want to modify the summary buffer, you can use this hook."
810   :group 'gnus-summary-various
811   :type 'hook)
812
813 (defcustom gnus-summary-prepared-hook nil
814   "*A hook called as the last thing after the summary buffer has been generated."
815   :group 'gnus-summary-various
816   :type 'hook)
817
818 (defcustom gnus-summary-generate-hook nil
819   "*A hook run just before generating the summary buffer.
820 This hook is commonly used to customize threading variables and the
821 like."
822   :group 'gnus-summary-various
823   :type 'hook)
824
825 (defcustom gnus-select-group-hook nil
826   "*A hook called when a newsgroup is selected.
827
828 If you'd like to simplify subjects like the
829 `gnus-summary-next-same-subject' command does, you can use the
830 following hook:
831
832  (add-hook gnus-select-group-hook
833            (lambda ()
834              (mapcar (lambda (header)
835                        (mail-header-set-subject
836                         header
837                         (gnus-simplify-subject
838                          (mail-header-subject header) 're-only)))
839                      gnus-newsgroup-headers)))"
840   :group 'gnus-group-select
841   :type 'hook)
842
843 (defcustom gnus-select-article-hook nil
844   "*A hook called when an article is selected."
845   :group 'gnus-summary-choose
846   :options '(gnus-agent-fetch-selected-article)
847   :type 'hook)
848
849 (defcustom gnus-visual-mark-article-hook
850   (list 'gnus-highlight-selected-summary)
851   "*Hook run after selecting an article in the summary buffer.
852 It is meant to be used for highlighting the article in some way.  It
853 is not run if `gnus-visual' is nil."
854   :group 'gnus-summary-visual
855   :type 'hook)
856
857 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
858   "*A hook called before parsing the headers."
859   :group 'gnus-various
860   :type 'hook)
861
862 (defcustom gnus-exit-group-hook nil
863   "*A hook called when exiting summary mode.
864 This hook is not called from the non-updating exit commands like `Q'."
865   :group 'gnus-various
866   :type 'hook)
867
868 (defcustom gnus-summary-update-hook
869   (list 'gnus-summary-highlight-line)
870   "*A hook called when a summary line is changed.
871 The hook will not be called if `gnus-visual' is nil.
872
873 The default function `gnus-summary-highlight-line' will
874 highlight the line according to the `gnus-summary-highlight'
875 variable."
876   :group 'gnus-summary-visual
877   :type 'hook)
878
879 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
880   "*A hook called when an article is selected for the first time.
881 The hook is intended to mark an article as read (or unread)
882 automatically when it is selected."
883   :group 'gnus-summary-choose
884   :type 'hook)
885
886 (defcustom gnus-group-no-more-groups-hook nil
887   "*A hook run when returning to group mode having no more (unread) groups."
888   :group 'gnus-group-select
889   :type 'hook)
890
891 (defcustom gnus-ps-print-hook nil
892   "*A hook run before ps-printing something from Gnus."
893   :group 'gnus-summary
894   :type 'hook)
895
896 (defcustom gnus-summary-article-move-hook nil
897   "*A hook called after an article is moved, copied, respooled, or crossposted."
898   :group 'gnus-summary
899   :type 'hook)
900
901 (defcustom gnus-summary-article-delete-hook nil
902   "*A hook called after an article is deleted."
903   :group 'gnus-summary
904   :type 'hook)
905
906 (defcustom gnus-summary-article-expire-hook nil
907   "*A hook called after an article is expired."
908   :group 'gnus-summary
909   :type 'hook)
910
911 (defcustom gnus-summary-display-arrow
912   (and (fboundp 'display-graphic-p)
913        (display-graphic-p))
914   "*If non-nil, display an arrow highlighting the current article."
915   :version "21.1"
916   :group 'gnus-summary
917   :type 'boolean)
918
919 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
920   "Face used for highlighting the current article in the summary buffer."
921   :group 'gnus-summary-visual
922   :type 'face)
923
924 (defvar gnus-tmp-downloaded nil)
925
926 (defcustom gnus-summary-highlight
927   '(((eq mark gnus-canceled-mark)
928      . gnus-summary-cancelled-face)
929     ((and uncached (> score default-high))
930      . gnus-summary-high-undownloaded-face)
931     ((and uncached (< score default-low))
932      . gnus-summary-low-undownloaded-face)
933     (uncached
934      . gnus-summary-normal-undownloaded-face)
935     ((and (> score default-high)
936           (or (eq mark gnus-dormant-mark)
937               (eq mark gnus-ticked-mark)))
938      . gnus-summary-high-ticked-face)
939     ((and (< score default-low)
940           (or (eq mark gnus-dormant-mark)
941               (eq mark gnus-ticked-mark)))
942      . gnus-summary-low-ticked-face)
943     ((or (eq mark gnus-dormant-mark)
944          (eq mark gnus-ticked-mark))
945      . gnus-summary-normal-ticked-face)
946     ((and (> score default-high) (eq mark gnus-ancient-mark))
947      . gnus-summary-high-ancient-face)
948     ((and (< score default-low) (eq mark gnus-ancient-mark))
949      . gnus-summary-low-ancient-face)
950     ((eq mark gnus-ancient-mark)
951      . gnus-summary-normal-ancient-face)
952     ((and (> score default-high) (eq mark gnus-unread-mark))
953      . gnus-summary-high-unread-face)
954     ((and (< score default-low) (eq mark gnus-unread-mark))
955      . gnus-summary-low-unread-face)
956     ((eq mark gnus-unread-mark)
957      . gnus-summary-normal-unread-face)
958     ((> score default-high)
959      . gnus-summary-high-read-face)
960     ((< score default-low)
961      . gnus-summary-low-read-face)
962     (t
963      . gnus-summary-normal-read-face))
964   "*Controls the highlighting of summary buffer lines.
965
966 A list of (FORM . FACE) pairs.  When deciding how a a particular
967 summary line should be displayed, each form is evaluated.  The content
968 of the face field after the first true form is used.  You can change
969 how those summary lines are displayed, by editing the face field.
970
971 You can use the following variables in the FORM field.
972
973 score:        The article's score
974 default:      The default article score.
975 default-high: The default score for high scored articles.
976 default-low:  The default score for low scored articles.
977 below:        The score below which articles are automatically marked as read.
978 mark:         The articles mark."
979   :group 'gnus-summary-visual
980   :type '(repeat (cons (sexp :tag "Form" nil)
981                        face)))
982
983 (defcustom gnus-alter-header-function nil
984   "Function called to allow alteration of article header structures.
985 The function is called with one parameter, the article header vector,
986 which it may alter in any way."
987   :type '(choice (const :tag "None" nil)
988                  function)
989   :group 'gnus-summary)
990
991 (defvar gnus-decode-encoded-word-function
992   (mime-find-field-decoder 'From 'nov)
993   "Variable that says which function should be used to decode a string with encoded words.")
994
995 (defcustom gnus-extra-headers '(To Newsgroups)
996   "*Extra headers to parse."
997   :version "21.1"
998   :group 'gnus-summary
999   :type '(repeat symbol))
1000
1001 (defcustom gnus-ignored-from-addresses
1002   (and user-mail-address (regexp-quote user-mail-address))
1003   "*Regexp of From headers that may be suppressed in favor of To headers."
1004   :version "21.1"
1005   :group 'gnus-summary
1006   :type 'regexp)
1007
1008 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1009   "List of charsets that should be ignored.
1010 When these charsets are used in the \"charset\" parameter, the
1011 default charset will be used instead."
1012   :version "21.1"
1013   :type '(repeat symbol)
1014   :group 'gnus-charset)
1015
1016 (gnus-define-group-parameter
1017  ignored-charsets
1018  :type list
1019  :function-document
1020  "Return the ignored charsets of GROUP."
1021  :variable gnus-group-ignored-charsets-alist
1022  :variable-default
1023  '(("alt\\.chinese\\.text" iso-8859-1))
1024  :variable-document
1025  "Alist of regexps (to match group names) and charsets that should be ignored.
1026 When these charsets are used in the \"charset\" parameter, the
1027 default charset will be used instead."
1028  :variable-group gnus-charset
1029  :variable-type '(repeat (cons (regexp :tag "Group")
1030                                (repeat symbol)))
1031  :parameter-type '(choice :tag "Ignored charsets"
1032                           :value nil
1033                           (repeat (symbol)))
1034  :parameter-document       "\
1035 List of charsets that should be ignored.
1036
1037 When these charsets are used in the \"charset\" parameter, the
1038 default charset will be used instead.")
1039
1040 (defcustom gnus-group-highlight-words-alist nil
1041   "Alist of group regexps and highlight regexps.
1042 This variable uses the same syntax as `gnus-emphasis-alist'."
1043   :version "21.1"
1044   :type '(repeat (cons (regexp :tag "Group")
1045                        (repeat (list (regexp :tag "Highlight regexp")
1046                                      (number :tag "Group for entire word" 0)
1047                                      (number :tag "Group for displayed part" 0)
1048                                      (symbol :tag "Face"
1049                                              gnus-emphasis-highlight-words)))))
1050   :group 'gnus-summary-visual)
1051
1052 (defcustom gnus-use-wheel nil
1053   "Use Intelli-mouse on summary movement"
1054   :type 'boolean
1055   :group 'gnus-summary-maneuvering)
1056
1057 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1058   "Amount to scroll messages by spinning the mouse wheel.
1059 This is actually a cons cell, where the first item is the amount to scroll
1060 on a normal wheel event, and the second is the amount to scroll when the
1061 wheel is moved with the shift key depressed."
1062   :type '(cons (integer :tag "Shift") integer)
1063   :group 'gnus-summary-maneuvering)
1064
1065 (defcustom gnus-wheel-edge-resistance 2
1066   "How hard it should be to change the current article
1067 by moving the mouse over the edge of the article window."
1068   :type 'integer
1069   :group 'gnus-summary-maneuvering)
1070
1071 (defcustom gnus-summary-show-article-charset-alist
1072   nil
1073   "Alist of number and charset.
1074 The article will be shown with the charset corresponding to the
1075 numbered argument.
1076 For example: ((1 . cn-gb-2312) (2 . big5))."
1077   :version "21.1"
1078   :type '(repeat (cons (number :tag "Argument" 1)
1079                        (symbol :tag "Charset")))
1080   :group 'gnus-charset)
1081
1082 (defcustom gnus-preserve-marks t
1083   "Whether marks are preserved when moving, copying and respooling messages."
1084   :version "21.1"
1085   :type 'boolean
1086   :group 'gnus-summary-marks)
1087
1088 (defcustom gnus-alter-articles-to-read-function nil
1089   "Function to be called to alter the list of articles to be selected."
1090   :type '(choice (const nil) function)
1091   :group 'gnus-summary)
1092
1093 (defcustom gnus-orphan-score nil
1094   "*All orphans get this score added.  Set in the score file."
1095   :group 'gnus-score-default
1096   :type '(choice (const nil)
1097                  integer))
1098
1099 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1100   "*A regexp to match MIME parts when saving multiple parts of a
1101 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1102 This regexp will be used by default when prompting the user for which
1103 type of files to save."
1104   :group 'gnus-summary
1105   :type 'regexp)
1106
1107 (defcustom gnus-read-all-available-headers nil
1108   "Whether Gnus should parse all headers made available to it.
1109 This is mostly relevant for slow backends where the user may
1110 wish to widen the summary buffer to include all headers
1111 that were fetched.  Say, for nnultimate groups."
1112   :group 'gnus-summary
1113   :type '(choice boolean regexp))
1114
1115 (defcustom gnus-summary-muttprint-program "muttprint"
1116   "Command (and optional arguments) used to run Muttprint."
1117   :version "21.3"
1118   :group 'gnus-summary
1119   :type 'string)
1120
1121 (defcustom gnus-article-loose-mime nil
1122   "If non-nil, don't require MIME-Version header.
1123 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1124 supply the MIME-Version header or deliberately strip it From the mail.
1125 Set it to non-nil, Gnus will treat some articles as MIME even if
1126 the MIME-Version header is missed."
1127   :version "21.3"
1128   :type 'boolean
1129   :group 'gnus-article)
1130
1131 (defcustom gnus-article-emulate-mime t
1132   "If non-nil, use MIME emulation for uuencode and the like.
1133 This means that Gnus will search message bodies for text that look
1134 like uuencoded bits, yEncoded bits, and so on, and present that using
1135 the normal Gnus MIME machinery."
1136   :type 'boolean
1137   :group 'gnus-article)
1138
1139 ;;; Internal variables
1140
1141 (defvar gnus-summary-display-cache nil)
1142 (defvar gnus-article-mime-handles nil)
1143 (defvar gnus-article-decoded-p nil)
1144 (defvar gnus-article-charset nil)
1145 (defvar gnus-article-ignored-charsets nil)
1146 (defvar gnus-scores-exclude-files nil)
1147 (defvar gnus-page-broken nil)
1148
1149 (defvar gnus-original-article nil)
1150 (defvar gnus-article-internal-prepare-hook nil)
1151 (defvar gnus-newsgroup-process-stack nil)
1152
1153 (defvar gnus-thread-indent-array nil)
1154 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1155 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1156   "Function called to sort the articles within a thread after it has been gathered together.")
1157
1158 (defvar gnus-summary-save-parts-type-history nil)
1159 (defvar gnus-summary-save-parts-last-directory nil)
1160
1161 ;; Avoid highlighting in kill files.
1162 (defvar gnus-summary-inhibit-highlight nil)
1163 (defvar gnus-newsgroup-selected-overlay nil)
1164 (defvar gnus-inhibit-limiting nil)
1165 (defvar gnus-newsgroup-adaptive-score-file nil)
1166 (defvar gnus-current-score-file nil)
1167 (defvar gnus-current-move-group nil)
1168 (defvar gnus-current-copy-group nil)
1169 (defvar gnus-current-crosspost-group nil)
1170 (defvar gnus-newsgroup-display nil)
1171
1172 (defvar gnus-newsgroup-dependencies nil)
1173 (defvar gnus-newsgroup-adaptive nil)
1174 (defvar gnus-summary-display-article-function nil)
1175 (defvar gnus-summary-highlight-line-function nil
1176   "Function called after highlighting a summary line.")
1177
1178 (defvar gnus-summary-line-format-alist
1179   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1180     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1181     (?s gnus-tmp-subject-or-nil ?s)
1182     (?n gnus-tmp-name ?s)
1183     (?A (std11-address-string
1184          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1185     (?a (or (std11-full-name-string
1186              (car (mime-entity-read-field gnus-tmp-header 'From)))
1187             gnus-tmp-from) ?s)
1188     (?F gnus-tmp-from ?s)
1189     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1190     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1191     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1192     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1193     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1194     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1195     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1196     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1197     (?L gnus-tmp-lines ?s)
1198     (?O gnus-tmp-downloaded ?c)
1199     (?I gnus-tmp-indentation ?s)
1200     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1201     (?R gnus-tmp-replied ?c)
1202     (?\[ gnus-tmp-opening-bracket ?c)
1203     (?\] gnus-tmp-closing-bracket ?c)
1204     (?\> (make-string gnus-tmp-level ? ) ?s)
1205     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1206     (?i gnus-tmp-score ?d)
1207     (?z gnus-tmp-score-char ?c)
1208     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1209     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1210     (?U gnus-tmp-unread ?c)
1211     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1212         ?s)
1213     (?t (gnus-summary-number-of-articles-in-thread
1214          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1215         ?d)
1216     (?e (gnus-summary-number-of-articles-in-thread
1217          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1218         ?c)
1219     (?u gnus-tmp-user-defined ?s)
1220     (?P (gnus-pick-line-number) ?d)
1221     (?B gnus-tmp-thread-tree-header-string ?s)
1222     (user-date (gnus-user-date
1223                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1224   "An alist of format specifications that can appear in summary lines.
1225 These are paired with what variables they correspond with, along with
1226 the type of the variable (string, integer, character, etc).")
1227
1228 (defvar gnus-summary-dummy-line-format-alist
1229   `((?S gnus-tmp-subject ?s)
1230     (?N gnus-tmp-number ?d)
1231     (?u gnus-tmp-user-defined ?s)))
1232
1233 (defvar gnus-summary-mode-line-format-alist
1234   `((?G gnus-tmp-group-name ?s)
1235     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1236     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1237     (?A gnus-tmp-article-number ?d)
1238     (?Z gnus-tmp-unread-and-unselected ?s)
1239     (?V gnus-version ?s)
1240     (?U gnus-tmp-unread-and-unticked ?d)
1241     (?S gnus-tmp-subject ?s)
1242     (?e gnus-tmp-unselected ?d)
1243     (?u gnus-tmp-user-defined ?s)
1244     (?d (length gnus-newsgroup-dormant) ?d)
1245     (?t (length gnus-newsgroup-marked) ?d)
1246     (?h (length gnus-newsgroup-spam-marked) ?d)
1247     (?r (length gnus-newsgroup-reads) ?d)
1248     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1249     (?E gnus-newsgroup-expunged-tally ?d)
1250     (?s (gnus-current-score-file-nondirectory) ?s)))
1251
1252 (defvar gnus-last-search-regexp nil
1253   "Default regexp for article search command.")
1254
1255 (defvar gnus-summary-search-article-matched-data nil
1256   "Last matched data of article search command.  It is the local variable
1257 in `gnus-article-buffer' which consists of the list of start position,
1258 end position and text.")
1259
1260 (defvar gnus-last-shell-command nil
1261   "Default shell command on article.")
1262
1263 (defvar gnus-newsgroup-agentized nil
1264   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1265 (defvar gnus-newsgroup-begin nil)
1266 (defvar gnus-newsgroup-end nil)
1267 (defvar gnus-newsgroup-last-rmail nil)
1268 (defvar gnus-newsgroup-last-mail nil)
1269 (defvar gnus-newsgroup-last-folder nil)
1270 (defvar gnus-newsgroup-last-file nil)
1271 (defvar gnus-newsgroup-auto-expire nil)
1272 (defvar gnus-newsgroup-active nil)
1273
1274 (defvar gnus-newsgroup-data nil)
1275 (defvar gnus-newsgroup-data-reverse nil)
1276 (defvar gnus-newsgroup-limit nil)
1277 (defvar gnus-newsgroup-limits nil)
1278
1279 (defvar gnus-newsgroup-unreads nil
1280   "Sorted list of unread articles in the current newsgroup.")
1281
1282 (defvar gnus-newsgroup-unselected nil
1283   "Sorted list of unselected unread articles in the current newsgroup.")
1284
1285 (defvar gnus-newsgroup-reads nil
1286   "Alist of read articles and article marks in the current newsgroup.")
1287
1288 (defvar gnus-newsgroup-expunged-tally nil)
1289
1290 (defvar gnus-newsgroup-marked nil
1291   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1292
1293 (defvar gnus-newsgroup-spam-marked nil
1294   "List of ranges of articles that have been marked as spam.")
1295
1296 (defvar gnus-newsgroup-killed nil
1297   "List of ranges of articles that have been through the scoring process.")
1298
1299 (defvar gnus-newsgroup-cached nil
1300   "Sorted list of articles that come from the article cache.")
1301
1302 (defvar gnus-newsgroup-saved nil
1303   "List of articles that have been saved.")
1304
1305 (defvar gnus-newsgroup-kill-headers nil)
1306
1307 (defvar gnus-newsgroup-replied nil
1308   "List of articles that have been replied to in the current newsgroup.")
1309
1310 (defvar gnus-newsgroup-forwarded nil
1311   "List of articles that have been forwarded in the current newsgroup.")
1312
1313 (defvar gnus-newsgroup-recent nil
1314   "List of articles that have are recent in the current newsgroup.")
1315
1316 (defvar gnus-newsgroup-expirable nil
1317   "Sorted list of articles in the current newsgroup that can be expired.")
1318
1319 (defvar gnus-newsgroup-processable nil
1320   "List of articles in the current newsgroup that can be processed.")
1321
1322 (defvar gnus-newsgroup-downloadable nil
1323   "Sorted list of articles in the current newsgroup that can be processed.")
1324
1325 (defvar gnus-newsgroup-unfetched nil
1326   "Sorted list of articles in the current newsgroup whose headers have
1327 not been fetched into the agent.
1328
1329 This list will always be a subset of gnus-newsgroup-undownloaded.")
1330
1331 (defvar gnus-newsgroup-undownloaded nil
1332   "List of articles in the current newsgroup that haven't been downloaded.")
1333
1334 (defvar gnus-newsgroup-unsendable nil
1335   "List of articles in the current newsgroup that won't be sent.")
1336
1337 (defvar gnus-newsgroup-bookmarks nil
1338   "List of articles in the current newsgroup that have bookmarks.")
1339
1340 (defvar gnus-newsgroup-dormant nil
1341   "Sorted list of dormant articles in the current newsgroup.")
1342
1343 (defvar gnus-newsgroup-unseen nil
1344   "List of unseen articles in the current newsgroup.")
1345
1346 (defvar gnus-newsgroup-seen nil
1347   "Range of seen articles in the current newsgroup.")
1348
1349 (defvar gnus-newsgroup-articles nil
1350   "List of articles in the current newsgroup.")
1351
1352 (defvar gnus-newsgroup-scored nil
1353   "List of scored articles in the current newsgroup.")
1354
1355 (defvar gnus-newsgroup-incorporated nil
1356   "List of incorporated articles in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-headers nil
1359   "List of article headers in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-threads nil)
1362
1363 (defvar gnus-newsgroup-prepared nil
1364   "Whether the current group has been prepared properly.")
1365
1366 (defvar gnus-newsgroup-ancient nil
1367   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-sparse nil)
1370
1371 (defvar gnus-current-article nil)
1372 (defvar gnus-article-current nil)
1373 (defvar gnus-current-headers nil)
1374 (defvar gnus-have-all-headers nil)
1375 (defvar gnus-last-article nil)
1376 (defvar gnus-newsgroup-history nil)
1377 (defvar gnus-newsgroup-charset nil)
1378 (defvar gnus-newsgroup-ephemeral-charset nil)
1379 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1380
1381 (defvar gnus-article-before-search nil)
1382
1383 (defconst gnus-summary-local-variables
1384   '(gnus-newsgroup-name
1385     gnus-newsgroup-begin gnus-newsgroup-end
1386     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1387     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1388     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1389     gnus-newsgroup-unselected gnus-newsgroup-marked
1390     gnus-newsgroup-spam-marked
1391     gnus-newsgroup-reads gnus-newsgroup-saved
1392     gnus-newsgroup-replied gnus-newsgroup-forwarded
1393     gnus-newsgroup-recent
1394     gnus-newsgroup-expirable
1395     gnus-newsgroup-processable gnus-newsgroup-killed
1396     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1397     gnus-newsgroup-unfetched
1398     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1399     gnus-newsgroup-seen gnus-newsgroup-articles
1400     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1401     gnus-newsgroup-headers gnus-newsgroup-threads
1402     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1403     gnus-current-article gnus-current-headers gnus-have-all-headers
1404     gnus-last-article gnus-article-internal-prepare-hook
1405     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1406     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1407     gnus-thread-expunge-below
1408     gnus-score-alist gnus-current-score-file
1409     (gnus-summary-expunge-below . global)
1410     (gnus-summary-mark-below . global)
1411     (gnus-orphan-score . global)
1412     gnus-newsgroup-active gnus-scores-exclude-files
1413     gnus-newsgroup-history gnus-newsgroup-ancient
1414     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1415     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1416     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1417     (gnus-newsgroup-expunged-tally . 0)
1418     gnus-cache-removable-articles gnus-newsgroup-cached
1419     gnus-newsgroup-data gnus-newsgroup-data-reverse
1420     gnus-newsgroup-limit gnus-newsgroup-limits
1421     gnus-newsgroup-charset gnus-newsgroup-display
1422     gnus-newsgroup-incorporated)
1423   "Variables that are buffer-local to the summary buffers.")
1424
1425 (defvar gnus-newsgroup-variables nil
1426   "A list of variables that have separate values in different newsgroups.
1427 A list of newsgroup (summary buffer) local variables, or cons of
1428 variables and their default values (when the default values are not
1429 nil), that should be made global while the summary buffer is active.
1430 These variables can be used to set variables in the group parameters
1431 while still allowing them to affect operations done in other
1432 buffers. For example:
1433
1434 \(setq gnus-newsgroup-variables
1435      '(message-use-followup-to
1436        (gnus-visible-headers .
1437          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1438 ")
1439
1440 ;; Byte-compiler warning.
1441 ;(eval-when-compile (defvar gnus-article-mode-map))
1442 (eval-when-compile
1443   (let ((features (cons 'gnus-sum features)))
1444     (require 'gnus)
1445     (require 'gnus-agent)
1446     (require 'gnus-art)))
1447
1448 ;; Subject simplification.
1449
1450 (defun gnus-simplify-whitespace (str)
1451   "Remove excessive whitespace from STR."
1452   ;; Multiple spaces.
1453   (while (string-match "[ \t][ \t]+" str)
1454     (setq str (concat (substring str 0 (match-beginning 0))
1455                         " "
1456                         (substring str (match-end 0)))))
1457   ;; Leading spaces.
1458   (when (string-match "^[ \t]+" str)
1459     (setq str (substring str (match-end 0))))
1460   ;; Trailing spaces.
1461   (when (string-match "[ \t]+$" str)
1462     (setq str (substring str 0 (match-beginning 0))))
1463   str)
1464
1465 (defun gnus-simplify-all-whitespace (str)
1466   "Remove all whitespace from STR."
1467   (while (string-match "[ \t\n]+" str)
1468     (setq str (replace-match "" nil nil str)))
1469   str)
1470
1471 (defsubst gnus-simplify-subject-re (subject)
1472   "Remove \"Re:\" from subject lines."
1473   (if (string-match message-subject-re-regexp subject)
1474       (substring subject (match-end 0))
1475     subject))
1476
1477 (defun gnus-simplify-subject (subject &optional re-only)
1478   "Remove `Re:' and words in parentheses.
1479 If RE-ONLY is non-nil, strip leading `Re:'s only."
1480   (let ((case-fold-search t))           ;Ignore case.
1481     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1482     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1483       (setq subject (substring subject (match-end 0))))
1484     ;; Remove uninteresting prefixes.
1485     (when (and (not re-only)
1486                gnus-simplify-ignored-prefixes
1487                (string-match gnus-simplify-ignored-prefixes subject))
1488       (setq subject (substring subject (match-end 0))))
1489     ;; Remove words in parentheses from end.
1490     (unless re-only
1491       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1492         (setq subject (substring subject 0 (match-beginning 0)))))
1493     ;; Return subject string.
1494     subject))
1495
1496 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1497 ;; all whitespace.
1498 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1499   (goto-char (point-min))
1500   (while (re-search-forward regexp nil t)
1501     (replace-match (or newtext ""))))
1502
1503 (defun gnus-simplify-buffer-fuzzy ()
1504   "Simplify string in the buffer fuzzily.
1505 The string in the accessible portion of the current buffer is simplified.
1506 It is assumed to be a single-line subject.
1507 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1508 matter is removed.  Additional things can be deleted by setting
1509 `gnus-simplify-subject-fuzzy-regexp'."
1510   (let ((case-fold-search t)
1511         (modified-tick))
1512     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1513
1514     (while (not (eq modified-tick (buffer-modified-tick)))
1515       (setq modified-tick (buffer-modified-tick))
1516       (cond
1517        ((listp gnus-simplify-subject-fuzzy-regexp)
1518         (mapcar 'gnus-simplify-buffer-fuzzy-step
1519                 gnus-simplify-subject-fuzzy-regexp))
1520        (gnus-simplify-subject-fuzzy-regexp
1521         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1522       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1523       (gnus-simplify-buffer-fuzzy-step
1524        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1525       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1526
1527     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1528     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1529     (gnus-simplify-buffer-fuzzy-step " $")
1530     (gnus-simplify-buffer-fuzzy-step "^ +")))
1531
1532 (defun gnus-simplify-subject-fuzzy (subject)
1533   "Simplify a subject string fuzzily.
1534 See `gnus-simplify-buffer-fuzzy' for details."
1535   (save-excursion
1536     (gnus-set-work-buffer)
1537     (let ((case-fold-search t))
1538       ;; Remove uninteresting prefixes.
1539       (when (and gnus-simplify-ignored-prefixes
1540                  (string-match gnus-simplify-ignored-prefixes subject))
1541         (setq subject (substring subject (match-end 0))))
1542       (insert subject)
1543       (inline (gnus-simplify-buffer-fuzzy))
1544       (buffer-string))))
1545
1546 (defsubst gnus-simplify-subject-fully (subject)
1547   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1548   (cond
1549    (gnus-simplify-subject-functions
1550     (gnus-map-function gnus-simplify-subject-functions subject))
1551    ((null gnus-summary-gather-subject-limit)
1552     (gnus-simplify-subject-re subject))
1553    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1554     (gnus-simplify-subject-fuzzy subject))
1555    ((numberp gnus-summary-gather-subject-limit)
1556     (gnus-limit-string (gnus-simplify-subject-re subject)
1557                        gnus-summary-gather-subject-limit))
1558    (t
1559     subject)))
1560
1561 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1562   "Check whether two subjects are equal.
1563 If optional argument SIMPLE-FIRST is t, first argument is already
1564 simplified."
1565   (cond
1566    ((null simple-first)
1567     (equal (gnus-simplify-subject-fully s1)
1568            (gnus-simplify-subject-fully s2)))
1569    (t
1570     (equal s1
1571            (gnus-simplify-subject-fully s2)))))
1572
1573 (defun gnus-summary-bubble-group ()
1574   "Increase the score of the current group.
1575 This is a handy function to add to `gnus-summary-exit-hook' to
1576 increase the score of each group you read."
1577   (gnus-group-add-score gnus-newsgroup-name))
1578
1579 \f
1580 ;;;
1581 ;;; Gnus summary mode
1582 ;;;
1583
1584 (put 'gnus-summary-mode 'mode-class 'special)
1585
1586 (defvar gnus-article-commands-menu)
1587
1588 ;; Non-orthogonal keys
1589
1590 (gnus-define-keys gnus-summary-mode-map
1591   " " gnus-summary-next-page
1592   "\177" gnus-summary-prev-page
1593   [delete] gnus-summary-prev-page
1594   [backspace] gnus-summary-prev-page
1595   "\r" gnus-summary-scroll-up
1596   "\M-\r" gnus-summary-scroll-down
1597   "n" gnus-summary-next-unread-article
1598   "p" gnus-summary-prev-unread-article
1599   "N" gnus-summary-next-article
1600   "P" gnus-summary-prev-article
1601   "\M-\C-n" gnus-summary-next-same-subject
1602   "\M-\C-p" gnus-summary-prev-same-subject
1603   "\M-n" gnus-summary-next-unread-subject
1604   "\M-p" gnus-summary-prev-unread-subject
1605   "." gnus-summary-first-unread-article
1606   "," gnus-summary-best-unread-article
1607   "\M-s" gnus-summary-search-article-forward
1608   "\M-r" gnus-summary-search-article-backward
1609   "<" gnus-summary-beginning-of-article
1610   ">" gnus-summary-end-of-article
1611   "j" gnus-summary-goto-article
1612   "^" gnus-summary-refer-parent-article
1613   "\M-^" gnus-summary-refer-article
1614   "u" gnus-summary-tick-article-forward
1615   "!" gnus-summary-tick-article-forward
1616   "U" gnus-summary-tick-article-backward
1617   "d" gnus-summary-mark-as-read-forward
1618   "D" gnus-summary-mark-as-read-backward
1619   "E" gnus-summary-mark-as-expirable
1620   "\M-u" gnus-summary-clear-mark-forward
1621   "\M-U" gnus-summary-clear-mark-backward
1622   "k" gnus-summary-kill-same-subject-and-select
1623   "\C-k" gnus-summary-kill-same-subject
1624   "\M-\C-k" gnus-summary-kill-thread
1625   "\M-\C-l" gnus-summary-lower-thread
1626   "e" gnus-summary-edit-article
1627   "#" gnus-summary-mark-as-processable
1628   "\M-#" gnus-summary-unmark-as-processable
1629   "\M-\C-t" gnus-summary-toggle-threads
1630   "\M-\C-s" gnus-summary-show-thread
1631   "\M-\C-h" gnus-summary-hide-thread
1632   "\M-\C-f" gnus-summary-next-thread
1633   "\M-\C-b" gnus-summary-prev-thread
1634   [(meta down)] gnus-summary-next-thread
1635   [(meta up)] gnus-summary-prev-thread
1636   "\M-\C-u" gnus-summary-up-thread
1637   "\M-\C-d" gnus-summary-down-thread
1638   "&" gnus-summary-execute-command
1639   "c" gnus-summary-catchup-and-exit
1640   "\C-w" gnus-summary-mark-region-as-read
1641   "\C-t" gnus-summary-toggle-truncation
1642   "?" gnus-summary-mark-as-dormant
1643   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1644   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1645   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1646   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1647   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1648   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1649   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1650   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1651   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1652   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1653   "=" gnus-summary-expand-window
1654   "\C-x\C-s" gnus-summary-reselect-current-group
1655   "\M-g" gnus-summary-rescan-group
1656   "w" gnus-summary-stop-page-breaking
1657   "\C-c\C-r" gnus-summary-caesar-message
1658   "\M-t" gnus-summary-toggle-mime
1659   "f" gnus-summary-followup
1660   "F" gnus-summary-followup-with-original
1661   "C" gnus-summary-cancel-article
1662   "r" gnus-summary-reply
1663   "R" gnus-summary-reply-with-original
1664   "\C-c\C-f" gnus-summary-mail-forward
1665   "o" gnus-summary-save-article
1666   "\C-o" gnus-summary-save-article-mail
1667   "|" gnus-summary-pipe-output
1668   "\M-k" gnus-summary-edit-local-kill
1669   "\M-K" gnus-summary-edit-global-kill
1670   ;; "V" gnus-version
1671   "\C-c\C-d" gnus-summary-describe-group
1672   "q" gnus-summary-exit
1673   "Q" gnus-summary-exit-no-update
1674   "\C-c\C-i" gnus-info-find-node
1675   gnus-mouse-2 gnus-mouse-pick-article
1676   "m" gnus-summary-mail-other-window
1677   "a" gnus-summary-post-news
1678   "i" gnus-summary-news-other-window
1679   "x" gnus-summary-limit-to-unread
1680   "s" gnus-summary-isearch-article
1681   "t" gnus-summary-toggle-header
1682   "g" gnus-summary-show-article
1683   "l" gnus-summary-goto-last-article
1684   "v" gnus-summary-preview-mime-message
1685   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1686   "\C-d" gnus-summary-enter-digest-group
1687   "\M-\C-d" gnus-summary-read-document
1688   "\M-\C-e" gnus-summary-edit-parameters
1689   "\M-\C-a" gnus-summary-customize-parameters
1690   "\C-c\C-b" gnus-bug
1691   "\C-c\C-n" gnus-namazu-search
1692   "*" gnus-cache-enter-article
1693   "\M-*" gnus-cache-remove-article
1694   "\M-&" gnus-summary-universal-argument
1695   "\C-l" gnus-recenter
1696   "I" gnus-summary-increase-score
1697   "L" gnus-summary-lower-score
1698   "\M-i" gnus-symbolic-argument
1699   "h" gnus-summary-select-article-buffer
1700
1701   "V" gnus-summary-score-map
1702   "X" gnus-uu-extract-map
1703   "S" gnus-summary-send-map)
1704
1705   ;; Sort of orthogonal keymap
1706 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1707   "t" gnus-summary-tick-article-forward
1708   "!" gnus-summary-tick-article-forward
1709   "d" gnus-summary-mark-as-read-forward
1710   "r" gnus-summary-mark-as-read-forward
1711   "c" gnus-summary-clear-mark-forward
1712   " " gnus-summary-clear-mark-forward
1713   "e" gnus-summary-mark-as-expirable
1714   "x" gnus-summary-mark-as-expirable
1715   "?" gnus-summary-mark-as-dormant
1716   "b" gnus-summary-set-bookmark
1717   "B" gnus-summary-remove-bookmark
1718   "#" gnus-summary-mark-as-processable
1719   "\M-#" gnus-summary-unmark-as-processable
1720   "S" gnus-summary-limit-include-expunged
1721   "C" gnus-summary-catchup
1722   "H" gnus-summary-catchup-to-here
1723   "h" gnus-summary-catchup-from-here
1724   "\C-c" gnus-summary-catchup-all
1725   "k" gnus-summary-kill-same-subject-and-select
1726   "K" gnus-summary-kill-same-subject
1727   "P" gnus-uu-mark-map)
1728
1729 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1730   "c" gnus-summary-clear-above
1731   "u" gnus-summary-tick-above
1732   "m" gnus-summary-mark-above
1733   "k" gnus-summary-kill-below)
1734
1735 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1736   "/" gnus-summary-limit-to-subject
1737   "n" gnus-summary-limit-to-articles
1738   "w" gnus-summary-pop-limit
1739   "s" gnus-summary-limit-to-subject
1740   "a" gnus-summary-limit-to-author
1741   "u" gnus-summary-limit-to-unread
1742   "m" gnus-summary-limit-to-marks
1743   "M" gnus-summary-limit-exclude-marks
1744   "v" gnus-summary-limit-to-score
1745   "*" gnus-summary-limit-include-cached
1746   "D" gnus-summary-limit-include-dormant
1747   "T" gnus-summary-limit-include-thread
1748   "d" gnus-summary-limit-exclude-dormant
1749   "t" gnus-summary-limit-to-age
1750   "." gnus-summary-limit-to-unseen
1751   "x" gnus-summary-limit-to-extra
1752   "p" gnus-summary-limit-to-display-predicate
1753   "E" gnus-summary-limit-include-expunged
1754   "c" gnus-summary-limit-exclude-childless-dormant
1755   "C" gnus-summary-limit-mark-excluded-as-read
1756   "o" gnus-summary-insert-old-articles
1757   "N" gnus-summary-insert-new-articles)
1758
1759 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1760   "n" gnus-summary-next-unread-article
1761   "p" gnus-summary-prev-unread-article
1762   "N" gnus-summary-next-article
1763   "P" gnus-summary-prev-article
1764   "\C-n" gnus-summary-next-same-subject
1765   "\C-p" gnus-summary-prev-same-subject
1766   "\M-n" gnus-summary-next-unread-subject
1767   "\M-p" gnus-summary-prev-unread-subject
1768   "f" gnus-summary-first-unread-article
1769   "b" gnus-summary-best-unread-article
1770   "j" gnus-summary-goto-article
1771   "g" gnus-summary-goto-subject
1772   "l" gnus-summary-goto-last-article
1773   "o" gnus-summary-pop-article)
1774
1775 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1776   "k" gnus-summary-kill-thread
1777   "l" gnus-summary-lower-thread
1778   "i" gnus-summary-raise-thread
1779   "T" gnus-summary-toggle-threads
1780   "t" gnus-summary-rethread-current
1781   "^" gnus-summary-reparent-thread
1782   "s" gnus-summary-show-thread
1783   "S" gnus-summary-show-all-threads
1784   "h" gnus-summary-hide-thread
1785   "H" gnus-summary-hide-all-threads
1786   "n" gnus-summary-next-thread
1787   "p" gnus-summary-prev-thread
1788   "u" gnus-summary-up-thread
1789   "o" gnus-summary-top-thread
1790   "d" gnus-summary-down-thread
1791   "#" gnus-uu-mark-thread
1792   "\M-#" gnus-uu-unmark-thread)
1793
1794 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1795   "g" gnus-summary-prepare
1796   "c" gnus-summary-insert-cached-articles
1797   "d" gnus-summary-insert-dormant-articles)
1798
1799 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1800   "c" gnus-summary-catchup-and-exit
1801   "C" gnus-summary-catchup-all-and-exit
1802   "E" gnus-summary-exit-no-update
1803   "J" gnus-summary-jump-to-other-group
1804   "Q" gnus-summary-exit
1805   "Z" gnus-summary-exit
1806   "n" gnus-summary-catchup-and-goto-next-group
1807   "R" gnus-summary-reselect-current-group
1808   "G" gnus-summary-rescan-group
1809   "N" gnus-summary-next-group
1810   "s" gnus-summary-save-newsrc
1811   "P" gnus-summary-prev-group)
1812
1813 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1814   " " gnus-summary-next-page
1815   "n" gnus-summary-next-page
1816   "\177" gnus-summary-prev-page
1817   [delete] gnus-summary-prev-page
1818   "p" gnus-summary-prev-page
1819   "\r" gnus-summary-scroll-up
1820   "\M-\r" gnus-summary-scroll-down
1821   "<" gnus-summary-beginning-of-article
1822   ">" gnus-summary-end-of-article
1823   "b" gnus-summary-beginning-of-article
1824   "e" gnus-summary-end-of-article
1825   "^" gnus-summary-refer-parent-article
1826   "r" gnus-summary-refer-parent-article
1827   "D" gnus-summary-enter-digest-group
1828   "R" gnus-summary-refer-references
1829   "T" gnus-summary-refer-thread
1830   "g" gnus-summary-show-article
1831   "s" gnus-summary-isearch-article
1832   "P" gnus-summary-print-article
1833   "M" gnus-mailing-list-insinuate
1834   "t" gnus-article-babel)
1835
1836 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1837   "b" gnus-article-add-buttons
1838   "B" gnus-article-add-buttons-to-head
1839   "o" gnus-article-treat-overstrike
1840   "e" gnus-article-emphasize
1841   "w" gnus-article-fill-cited-article
1842   "Q" gnus-article-fill-long-lines
1843   "C" gnus-article-capitalize-sentences
1844   "c" gnus-article-remove-cr
1845   "Z" gnus-article-decode-HZ
1846   "h" gnus-article-wash-html
1847   "u" gnus-article-unsplit-urls
1848   "f" gnus-article-display-x-face
1849   "l" gnus-summary-stop-page-breaking
1850   "r" gnus-summary-caesar-message
1851   "m" gnus-summary-morse-message
1852   "t" gnus-summary-toggle-header
1853   "g" gnus-treat-smiley
1854   "v" gnus-summary-verbose-headers
1855   "m" gnus-summary-toggle-mime
1856   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1857   "p" gnus-article-verify-x-pgp-sig
1858   "d" gnus-article-treat-dumbquotes)
1859
1860 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1861   ;; mnemonic: deuglif*Y*
1862   "u" gnus-article-outlook-unwrap-lines
1863   "a" gnus-article-outlook-repair-attribution
1864   "c" gnus-article-outlook-rearrange-citation
1865   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1866
1867 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1868   "a" gnus-article-hide
1869   "h" gnus-article-hide-headers
1870   "b" gnus-article-hide-boring-headers
1871   "s" gnus-article-hide-signature
1872   "c" gnus-article-hide-citation
1873   "C" gnus-article-hide-citation-in-followups
1874   "l" gnus-article-hide-list-identifiers
1875   "B" gnus-article-strip-banner
1876   "P" gnus-article-hide-pem
1877   "\C-c" gnus-article-hide-citation-maybe)
1878
1879 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1880   "a" gnus-article-highlight
1881   "h" gnus-article-highlight-headers
1882   "c" gnus-article-highlight-citation
1883   "s" gnus-article-highlight-signature)
1884
1885 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1886   "f" gnus-article-treat-fold-headers
1887   "u" gnus-article-treat-unfold-headers
1888   "n" gnus-article-treat-fold-newsgroups)
1889
1890 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1891   "x" gnus-article-display-x-face
1892   "d" gnus-article-display-face
1893   "s" gnus-treat-smiley
1894   "D" gnus-article-remove-images
1895   "f" gnus-treat-from-picon
1896   "m" gnus-treat-mail-picon
1897   "n" gnus-treat-newsgroups-picon)
1898
1899 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1900   "z" gnus-article-date-ut
1901   "u" gnus-article-date-ut
1902   "l" gnus-article-date-local
1903   "p" gnus-article-date-english
1904   "e" gnus-article-date-lapsed
1905   "o" gnus-article-date-original
1906   "i" gnus-article-date-iso8601
1907   "s" gnus-article-date-user)
1908
1909 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1910   "t" gnus-article-remove-trailing-blank-lines
1911   "l" gnus-article-strip-leading-blank-lines
1912   "m" gnus-article-strip-multiple-blank-lines
1913   "a" gnus-article-strip-blank-lines
1914   "A" gnus-article-strip-all-blank-lines
1915   "s" gnus-article-strip-leading-space
1916   "e" gnus-article-strip-trailing-space
1917   "w" gnus-article-remove-leading-whitespace)
1918
1919 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1920   "v" gnus-version
1921   "f" gnus-summary-fetch-faq
1922   "d" gnus-summary-describe-group
1923   "h" gnus-summary-describe-briefly
1924   "i" gnus-info-find-node
1925   "c" gnus-group-fetch-charter
1926   "C" gnus-group-fetch-control)
1927
1928 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1929   "e" gnus-summary-expire-articles
1930   "\M-\C-e" gnus-summary-expire-articles-now
1931   "\177" gnus-summary-delete-article
1932   [delete] gnus-summary-delete-article
1933   [backspace] gnus-summary-delete-article
1934   "m" gnus-summary-move-article
1935   "r" gnus-summary-respool-article
1936   "w" gnus-summary-edit-article
1937   "c" gnus-summary-copy-article
1938   "B" gnus-summary-crosspost-article
1939   "q" gnus-summary-respool-query
1940   "t" gnus-summary-respool-trace
1941   "i" gnus-summary-import-article
1942   "I" gnus-summary-create-article
1943   "p" gnus-summary-article-posted-p)
1944
1945 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1946   "o" gnus-summary-save-article
1947   "m" gnus-summary-save-article-mail
1948   "F" gnus-summary-write-article-file
1949   "r" gnus-summary-save-article-rmail
1950   "f" gnus-summary-save-article-file
1951   "b" gnus-summary-save-article-body-file
1952   "h" gnus-summary-save-article-folder
1953   "v" gnus-summary-save-article-vm
1954   "p" gnus-summary-pipe-output
1955   "P" gnus-summary-muttprint
1956   "s" gnus-soup-add-article)
1957
1958 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1959   "b" gnus-summary-display-buttonized
1960   "m" gnus-summary-repair-multipart
1961   "v" gnus-article-view-part
1962   "o" gnus-article-save-part
1963   "c" gnus-article-copy-part
1964   "C" gnus-article-view-part-as-charset
1965   "e" gnus-article-view-part-externally
1966   "E" gnus-article-encrypt-body
1967   "i" gnus-article-inline-part
1968   "|" gnus-article-pipe-part)
1969
1970 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1971   "p" gnus-summary-mark-as-processable
1972   "u" gnus-summary-unmark-as-processable
1973   "U" gnus-summary-unmark-all-processable
1974   "v" gnus-uu-mark-over
1975   "s" gnus-uu-mark-series
1976   "r" gnus-uu-mark-region
1977   "g" gnus-uu-unmark-region
1978   "R" gnus-uu-mark-by-regexp
1979   "G" gnus-uu-unmark-by-regexp
1980   "t" gnus-uu-mark-thread
1981   "T" gnus-uu-unmark-thread
1982   "a" gnus-uu-mark-all
1983   "b" gnus-uu-mark-buffer
1984   "S" gnus-uu-mark-sparse
1985   "k" gnus-summary-kill-process-mark
1986   "y" gnus-summary-yank-process-mark
1987   "w" gnus-summary-save-process-mark
1988   "i" gnus-uu-invert-processable)
1989
1990 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1991   ;;"x" gnus-uu-extract-any
1992   "m" gnus-summary-save-parts
1993   "u" gnus-uu-decode-uu
1994   "U" gnus-uu-decode-uu-and-save
1995   "s" gnus-uu-decode-unshar
1996   "S" gnus-uu-decode-unshar-and-save
1997   "o" gnus-uu-decode-save
1998   "O" gnus-uu-decode-save
1999   "b" gnus-uu-decode-binhex
2000   "B" gnus-uu-decode-binhex
2001   "p" gnus-uu-decode-postscript
2002   "P" gnus-uu-decode-postscript-and-save)
2003
2004 (gnus-define-keys
2005     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2006   "u" gnus-uu-decode-uu-view
2007   "U" gnus-uu-decode-uu-and-save-view
2008   "s" gnus-uu-decode-unshar-view
2009   "S" gnus-uu-decode-unshar-and-save-view
2010   "o" gnus-uu-decode-save-view
2011   "O" gnus-uu-decode-save-view
2012   "b" gnus-uu-decode-binhex-view
2013   "B" gnus-uu-decode-binhex-view
2014   "p" gnus-uu-decode-postscript-view
2015   "P" gnus-uu-decode-postscript-and-save-view)
2016
2017 (defvar gnus-article-post-menu nil)
2018
2019 (defconst gnus-summary-menu-maxlen 20)
2020
2021 (defun gnus-summary-menu-split (menu)
2022   ;; If we have lots of elements, divide them into groups of 20
2023   ;; and make a pane (or submenu) for each one.
2024   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2025       (let ((menu menu) sublists next
2026             (i 1))
2027         (while menu
2028           ;; Pull off the next gnus-summary-menu-maxlen elements
2029           ;; and make them the next element of sublist.
2030           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2031           (if next
2032               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2033                       nil))
2034           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2035                                              (aref (car (last menu)) 0)) menu)
2036                                sublists))
2037           (setq i (1+ i))
2038           (setq menu next))
2039         (nreverse sublists))
2040     ;; Few elements--put them all in one pane.
2041     menu))
2042
2043 (defun gnus-summary-make-menu-bar ()
2044   (gnus-turn-off-edit-menu 'summary)
2045
2046   (unless (boundp 'gnus-summary-misc-menu)
2047
2048     (easy-menu-define
2049      gnus-summary-kill-menu gnus-summary-mode-map ""
2050      (cons
2051       "Score"
2052       (nconc
2053        (list
2054         ["Customize" gnus-score-customize t])
2055        (gnus-make-score-map 'increase)
2056        (gnus-make-score-map 'lower)
2057        '(("Mark"
2058           ["Kill below" gnus-summary-kill-below t]
2059           ["Mark above" gnus-summary-mark-above t]
2060           ["Tick above" gnus-summary-tick-above t]
2061           ["Clear above" gnus-summary-clear-above t])
2062          ["Current score" gnus-summary-current-score t]
2063          ["Set score" gnus-summary-set-score t]
2064          ["Switch current score file..." gnus-score-change-score-file t]
2065          ["Set mark below..." gnus-score-set-mark-below t]
2066          ["Set expunge below..." gnus-score-set-expunge-below t]
2067          ["Edit current score file" gnus-score-edit-current-scores t]
2068          ["Edit score file" gnus-score-edit-file t]
2069          ["Trace score" gnus-score-find-trace t]
2070          ["Find words" gnus-score-find-favourite-words t]
2071          ["Rescore buffer" gnus-summary-rescore t]
2072          ["Increase score..." gnus-summary-increase-score t]
2073          ["Lower score..." gnus-summary-lower-score t]))))
2074
2075     ;; Define both the Article menu in the summary buffer and the
2076     ;; equivalent Commands menu in the article buffer here for
2077     ;; consistency.
2078     (let ((innards
2079            `(("Hide"
2080               ["All" gnus-article-hide t]
2081               ["Headers" gnus-article-hide-headers t]
2082               ["Signature" gnus-article-hide-signature t]
2083               ["Citation" gnus-article-hide-citation t]
2084               ["List identifiers" gnus-article-hide-list-identifiers t]
2085               ["Banner" gnus-article-strip-banner t]
2086               ["Boring headers" gnus-article-hide-boring-headers t])
2087              ("Highlight"
2088               ["All" gnus-article-highlight t]
2089               ["Headers" gnus-article-highlight-headers t]
2090               ["Signature" gnus-article-highlight-signature t]
2091               ["Citation" gnus-article-highlight-citation t])
2092              ("Date"
2093               ["Local" gnus-article-date-local t]
2094               ["ISO8601" gnus-article-date-iso8601 t]
2095               ["UT" gnus-article-date-ut t]
2096               ["Original" gnus-article-date-original t]
2097               ["Lapsed" gnus-article-date-lapsed t]
2098               ["User-defined" gnus-article-date-user t])
2099              ("Display"
2100               ["Remove images" gnus-article-remove-images t]
2101               ["Toggle smiley" gnus-treat-smiley t]
2102               ["Show X-Face" gnus-article-display-x-face t]
2103               ["Show picons in From" gnus-treat-from-picon t]
2104               ["Show picons in mail headers" gnus-treat-mail-picon t]
2105               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2106               ("View as different encoding"
2107                ,@(gnus-summary-menu-split
2108                   (mapcar
2109                    (lambda (cs)
2110                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2111                      ;; forms for menu commands, we should provide intern'ed
2112                      ;; function symbols.
2113                      (let ((command (intern (format "\
2114 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2115                        (fset command
2116                              `(lambda ()
2117                                 (interactive)
2118                                 (let ((gnus-summary-show-article-charset-alist
2119                                        '((1 . ,cs))))
2120                                   (gnus-summary-show-article 1))))
2121                        `[,(symbol-name cs) ,command t]))
2122                    (sort (if (fboundp 'coding-system-list)
2123                              (coding-system-list)
2124                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2125                            )
2126                          'string<)))))
2127              ("Washing"
2128               ("Remove Blanks"
2129                ["Leading" gnus-article-strip-leading-blank-lines t]
2130                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2131                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2132                ["All of the above" gnus-article-strip-blank-lines t]
2133                ["All" gnus-article-strip-all-blank-lines t]
2134                ["Leading space" gnus-article-strip-leading-space t]
2135                ["Trailing space" gnus-article-strip-trailing-space t]
2136                ["Leading space in headers"
2137                 gnus-article-remove-leading-whitespace t])
2138               ["Overstrike" gnus-article-treat-overstrike t]
2139               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2140               ["Emphasis" gnus-article-emphasize t]
2141               ["Word wrap" gnus-article-fill-cited-article t]
2142               ["Fill long lines" gnus-article-fill-long-lines t]
2143               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2144               ["CR" gnus-article-remove-cr t]
2145               ["Rot 13" gnus-summary-caesar-message
2146                ,@(if (featurep 'xemacs) '(t)
2147                    '(:help "\"Caesar rotate\" article by 13"))]
2148               ["Morse decode" gnus-summary-morse-message t]
2149               ["Unix pipe..." gnus-summary-pipe-message t]
2150               ["Add buttons" gnus-article-add-buttons t]
2151               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2152               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2153               ["Toggle MIME" gnus-summary-toggle-mime t]
2154               ["Verbose header" gnus-summary-verbose-headers t]
2155               ["Toggle header" gnus-summary-toggle-header t]
2156               ["Unfold headers" gnus-article-treat-unfold-headers t]
2157               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2158               ["Html" gnus-article-wash-html t]
2159               ["URLs" gnus-article-unsplit-urls t]
2160               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2161               ["HZ" gnus-article-decode-HZ t]
2162               ("(Outlook) Deuglify"
2163                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2164                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2165                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2166                ["Full (Outlook) deuglify"
2167                 gnus-article-outlook-deuglify-article t])
2168               )
2169              ("Output"
2170               ["Save in default format" gnus-summary-save-article
2171                ,@(if (featurep 'xemacs) '(t)
2172                    '(:help "Save article using default method"))]
2173               ["Save in file" gnus-summary-save-article-file
2174                ,@(if (featurep 'xemacs) '(t)
2175                    '(:help "Save article in file"))]
2176               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2177               ["Save in MH folder" gnus-summary-save-article-folder t]
2178               ["Save in VM folder" gnus-summary-save-article-vm t]
2179               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2180               ["Save body in file" gnus-summary-save-article-body-file t]
2181               ["Pipe through a filter" gnus-summary-pipe-output t]
2182               ["Add to SOUP packet" gnus-soup-add-article t]
2183               ["Print with Muttprint" gnus-summary-muttprint t]
2184               ["Print" gnus-summary-print-article t])
2185              ("Backend"
2186               ["Respool article..." gnus-summary-respool-article t]
2187               ["Move article..." gnus-summary-move-article
2188                (gnus-check-backend-function
2189                 'request-move-article gnus-newsgroup-name)]
2190               ["Copy article..." gnus-summary-copy-article t]
2191               ["Crosspost article..." gnus-summary-crosspost-article
2192                (gnus-check-backend-function
2193                 'request-replace-article gnus-newsgroup-name)]
2194               ["Import file..." gnus-summary-import-article t]
2195               ["Create article..." gnus-summary-create-article t]
2196               ["Check if posted" gnus-summary-article-posted-p t]
2197               ["Edit article" gnus-summary-edit-article
2198                (not (gnus-group-read-only-p))]
2199               ["Delete article" gnus-summary-delete-article
2200                (gnus-check-backend-function
2201                 'request-expire-articles gnus-newsgroup-name)]
2202               ["Query respool" gnus-summary-respool-query t]
2203               ["Trace respool" gnus-summary-respool-trace t]
2204               ["Delete expirable articles" gnus-summary-expire-articles-now
2205                (gnus-check-backend-function
2206                 'request-expire-articles gnus-newsgroup-name)])
2207              ("Extract"
2208               ["Uudecode" gnus-uu-decode-uu
2209                ,@(if (featurep 'xemacs) '(t)
2210                    '(:help "Decode uuencoded article(s)"))]
2211               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2212               ["Unshar" gnus-uu-decode-unshar t]
2213               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2214               ["Save" gnus-uu-decode-save t]
2215               ["Binhex" gnus-uu-decode-binhex t]
2216               ["Postscript" gnus-uu-decode-postscript t])
2217              ("Cache"
2218               ["Enter article" gnus-cache-enter-article t]
2219               ["Remove article" gnus-cache-remove-article t])
2220              ["Translate" gnus-article-babel t]
2221              ["Select article buffer" gnus-summary-select-article-buffer t]
2222              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2223              ["Isearch article..." gnus-summary-isearch-article t]
2224              ["Beginning of the article" gnus-summary-beginning-of-article t]
2225              ["End of the article" gnus-summary-end-of-article t]
2226              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2227              ["Fetch referenced articles" gnus-summary-refer-references t]
2228              ["Fetch current thread" gnus-summary-refer-thread t]
2229              ["Fetch article with id..." gnus-summary-refer-article t]
2230              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2231              ["Redisplay" gnus-summary-show-article t]
2232              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2233       (easy-menu-define
2234        gnus-summary-article-menu gnus-summary-mode-map ""
2235        (cons "Article" innards))
2236
2237       (if (not (keymapp gnus-summary-article-menu))
2238           (easy-menu-define
2239            gnus-article-commands-menu gnus-article-mode-map ""
2240            (cons "Commands" innards))
2241         ;; in Emacs, don't share menu.
2242         (setq gnus-article-commands-menu
2243               (copy-keymap gnus-summary-article-menu))
2244         (define-key gnus-article-mode-map [menu-bar commands]
2245           (cons "Commands" gnus-article-commands-menu))))
2246
2247     (easy-menu-define
2248      gnus-summary-thread-menu gnus-summary-mode-map ""
2249      '("Threads"
2250        ["Find all messages in thread" gnus-summary-refer-thread t]
2251        ["Toggle threading" gnus-summary-toggle-threads t]
2252        ["Hide threads" gnus-summary-hide-all-threads t]
2253        ["Show threads" gnus-summary-show-all-threads t]
2254        ["Hide thread" gnus-summary-hide-thread t]
2255        ["Show thread" gnus-summary-show-thread t]
2256        ["Go to next thread" gnus-summary-next-thread t]
2257        ["Go to previous thread" gnus-summary-prev-thread t]
2258        ["Go down thread" gnus-summary-down-thread t]
2259        ["Go up thread" gnus-summary-up-thread t]
2260        ["Top of thread" gnus-summary-top-thread t]
2261        ["Mark thread as read" gnus-summary-kill-thread t]
2262        ["Lower thread score" gnus-summary-lower-thread t]
2263        ["Raise thread score" gnus-summary-raise-thread t]
2264        ["Rethread current" gnus-summary-rethread-current t]))
2265
2266     (easy-menu-define
2267      gnus-summary-post-menu gnus-summary-mode-map ""
2268      `("Post"
2269        ["Send a message (mail or news)" gnus-summary-post-news
2270         ,@(if (featurep 'xemacs) '(t)
2271             '(:help "Post an article"))]
2272        ["Followup" gnus-summary-followup
2273         ,@(if (featurep 'xemacs) '(t)
2274             '(:help "Post followup to this article"))]
2275        ["Followup and yank" gnus-summary-followup-with-original
2276         ,@(if (featurep 'xemacs) '(t)
2277             '(:help "Post followup to this article, quoting its contents"))]
2278        ["Supersede article" gnus-summary-supersede-article t]
2279        ["Cancel article" gnus-summary-cancel-article
2280         ,@(if (featurep 'xemacs) '(t)
2281             '(:help "Cancel an article you posted"))]
2282        ["Reply" gnus-summary-reply t]
2283        ["Reply and yank" gnus-summary-reply-with-original t]
2284        ["Wide reply" gnus-summary-wide-reply t]
2285        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2286         ,@(if (featurep 'xemacs) '(t)
2287             '(:help "Mail a reply, quoting this article"))]
2288        ["Very wide reply" gnus-summary-very-wide-reply t]
2289        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2290         ,@(if (featurep 'xemacs) '(t)
2291             '(:help "Mail a very wide reply, quoting this article"))]
2292        ["Mail forward" gnus-summary-mail-forward t]
2293        ["Post forward" gnus-summary-post-forward t]
2294        ["Digest and mail" gnus-summary-digest-mail-forward t]
2295        ["Digest and post" gnus-summary-digest-post-forward t]
2296        ["Resend message" gnus-summary-resend-message t]
2297        ["Resend message edit" gnus-summary-resend-message-edit t]
2298        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2299        ["Send a mail" gnus-summary-mail-other-window t]
2300        ["Create a local message" gnus-summary-news-other-window t]
2301        ["Uuencode and post" gnus-uu-post-news
2302         ,@(if (featurep 'xemacs) '(t)
2303             '(:help "Post a uuencoded article"))]
2304        ["Followup via news" gnus-summary-followup-to-mail t]
2305        ["Followup via news and yank"
2306         gnus-summary-followup-to-mail-with-original t]
2307        ;;("Draft"
2308        ;;["Send" gnus-summary-send-draft t]
2309        ;;["Send bounced" gnus-resend-bounced-mail t])
2310        ))
2311
2312     (cond
2313      ((not (keymapp gnus-summary-post-menu))
2314       (setq gnus-article-post-menu gnus-summary-post-menu))
2315      ((not gnus-article-post-menu)
2316       ;; Don't share post menu.
2317       (setq gnus-article-post-menu
2318             (copy-keymap gnus-summary-post-menu))))
2319     (define-key gnus-article-mode-map [menu-bar post]
2320       (cons "Post" gnus-article-post-menu))
2321
2322     (easy-menu-define
2323      gnus-summary-misc-menu gnus-summary-mode-map ""
2324      `("Gnus"
2325        ("Mark Read"
2326         ["Mark as read" gnus-summary-mark-as-read-forward t]
2327         ["Mark same subject and select"
2328          gnus-summary-kill-same-subject-and-select t]
2329         ["Mark same subject" gnus-summary-kill-same-subject t]
2330         ["Catchup" gnus-summary-catchup
2331          ,@(if (featurep 'xemacs) '(t)
2332              '(:help "Mark unread articles in this group as read"))]
2333         ["Catchup all" gnus-summary-catchup-all t]
2334         ["Catchup to here" gnus-summary-catchup-to-here t]
2335         ["Catchup from here" gnus-summary-catchup-from-here t]
2336         ["Catchup region" gnus-summary-mark-region-as-read
2337          (gnus-mark-active-p)]
2338         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2339        ("Mark Various"
2340         ["Tick" gnus-summary-tick-article-forward t]
2341         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2342         ["Remove marks" gnus-summary-clear-mark-forward t]
2343         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2344         ["Set bookmark" gnus-summary-set-bookmark t]
2345         ["Remove bookmark" gnus-summary-remove-bookmark t])
2346        ("Limit to"
2347         ["Marks..." gnus-summary-limit-to-marks t]
2348         ["Subject..." gnus-summary-limit-to-subject t]
2349         ["Author..." gnus-summary-limit-to-author t]
2350         ["Age..." gnus-summary-limit-to-age t]
2351         ["Extra..." gnus-summary-limit-to-extra t]
2352         ["Score..." gnus-summary-limit-to-score t]
2353         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2354         ["Unread" gnus-summary-limit-to-unread t]
2355         ["Unseen" gnus-summary-limit-to-unseen t]
2356         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2357         ["Next articles" gnus-summary-limit-to-articles t]
2358         ["Pop limit" gnus-summary-pop-limit t]
2359         ["Show dormant" gnus-summary-limit-include-dormant t]
2360         ["Hide childless dormant"
2361          gnus-summary-limit-exclude-childless-dormant t]
2362         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2363         ["Hide marked" gnus-summary-limit-exclude-marks t]
2364         ["Show expunged" gnus-summary-limit-include-expunged t])
2365        ("Process Mark"
2366         ["Set mark" gnus-summary-mark-as-processable t]
2367         ["Remove mark" gnus-summary-unmark-as-processable t]
2368         ["Remove all marks" gnus-summary-unmark-all-processable t]
2369         ["Mark above" gnus-uu-mark-over t]
2370         ["Mark series" gnus-uu-mark-series t]
2371         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2372         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2373         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2374         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2375         ["Mark all" gnus-uu-mark-all t]
2376         ["Mark buffer" gnus-uu-mark-buffer t]
2377         ["Mark sparse" gnus-uu-mark-sparse t]
2378         ["Mark thread" gnus-uu-mark-thread t]
2379         ["Unmark thread" gnus-uu-unmark-thread t]
2380         ("Process Mark Sets"
2381          ["Kill" gnus-summary-kill-process-mark t]
2382          ["Yank" gnus-summary-yank-process-mark
2383           gnus-newsgroup-process-stack]
2384          ["Save" gnus-summary-save-process-mark t]
2385          ["Run command on marked..." gnus-summary-universal-argument t]))
2386        ("Scroll article"
2387         ["Page forward" gnus-summary-next-page
2388          ,@(if (featurep 'xemacs) '(t)
2389              '(:help "Show next page of article"))]
2390         ["Page backward" gnus-summary-prev-page
2391          ,@(if (featurep 'xemacs) '(t)
2392              '(:help "Show previous page of article"))]
2393         ["Line forward" gnus-summary-scroll-up t])
2394        ("Move"
2395         ["Next unread article" gnus-summary-next-unread-article t]
2396         ["Previous unread article" gnus-summary-prev-unread-article t]
2397         ["Next article" gnus-summary-next-article t]
2398         ["Previous article" gnus-summary-prev-article t]
2399         ["Next unread subject" gnus-summary-next-unread-subject t]
2400         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2401         ["Next article same subject" gnus-summary-next-same-subject t]
2402         ["Previous article same subject" gnus-summary-prev-same-subject t]
2403         ["First unread article" gnus-summary-first-unread-article t]
2404         ["Best unread article" gnus-summary-best-unread-article t]
2405         ["Go to subject number..." gnus-summary-goto-subject t]
2406         ["Go to article number..." gnus-summary-goto-article t]
2407         ["Go to the last article" gnus-summary-goto-last-article t]
2408         ["Pop article off history" gnus-summary-pop-article t])
2409        ("Sort"
2410         ["Sort by number" gnus-summary-sort-by-number t]
2411         ["Sort by author" gnus-summary-sort-by-author t]
2412         ["Sort by subject" gnus-summary-sort-by-subject t]
2413         ["Sort by date" gnus-summary-sort-by-date t]
2414         ["Sort by score" gnus-summary-sort-by-score t]
2415         ["Sort by lines" gnus-summary-sort-by-lines t]
2416         ["Sort by characters" gnus-summary-sort-by-chars t]
2417         ["Randomize" gnus-summary-sort-by-random t]
2418         ["Original sort" gnus-summary-sort-by-original t])
2419        ("Help"
2420         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2421         ["Describe group" gnus-summary-describe-group t]
2422         ["Fetch charter" gnus-group-fetch-charter
2423          ,@(if (featurep 'xemacs) nil
2424              '(:help "Display the charter of the current group"))]
2425         ["Fetch control message" gnus-group-fetch-control
2426          ,@(if (featurep 'xemacs) nil
2427              '(:help "Display the archived control message for the current group"))]
2428         ["Read manual" gnus-info-find-node t])
2429        ("Modes"
2430         ["Pick and read" gnus-pick-mode t]
2431         ["Binary" gnus-binary-mode t])
2432        ("Regeneration"
2433         ["Regenerate" gnus-summary-prepare t]
2434         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2435         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2436         ["Toggle threading" gnus-summary-toggle-threads t])
2437        ["See old articles" gnus-summary-insert-old-articles t]
2438        ["See new articles" gnus-summary-insert-new-articles t]
2439        ["Filter articles..." gnus-summary-execute-command t]
2440        ["Run command on articles..." gnus-summary-universal-argument t]
2441        ["Search articles forward..." gnus-summary-search-article-forward t]
2442        ["Search articles backward..." gnus-summary-search-article-backward t]
2443        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2444        ["Expand window" gnus-summary-expand-window t]
2445        ["Expire expirable articles" gnus-summary-expire-articles
2446         (gnus-check-backend-function
2447          'request-expire-articles gnus-newsgroup-name)]
2448        ["Edit local kill file" gnus-summary-edit-local-kill t]
2449        ["Edit main kill file" gnus-summary-edit-global-kill t]
2450        ["Edit group parameters" gnus-summary-edit-parameters t]
2451        ["Customize group parameters" gnus-summary-customize-parameters t]
2452        ["Send a bug report" gnus-bug t]
2453        ("Exit"
2454         ["Catchup and exit" gnus-summary-catchup-and-exit
2455          ,@(if (featurep 'xemacs) '(t)
2456              '(:help "Mark unread articles in this group as read, then exit"))]
2457         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2458         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2459         ["Exit group" gnus-summary-exit
2460          ,@(if (featurep 'xemacs) '(t)
2461              '(:help "Exit current group, return to group selection mode"))]
2462         ["Exit group without updating" gnus-summary-exit-no-update t]
2463         ["Exit and goto next group" gnus-summary-next-group t]
2464         ["Exit and goto prev group" gnus-summary-prev-group t]
2465         ["Reselect group" gnus-summary-reselect-current-group t]
2466         ["Rescan group" gnus-summary-rescan-group t]
2467         ["Update dribble" gnus-summary-save-newsrc t])))
2468
2469     (gnus-run-hooks 'gnus-summary-menu-hook)))
2470
2471 (defvar gnus-summary-tool-bar-map nil)
2472
2473 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2474 (defun gnus-summary-make-tool-bar ()
2475   (if (and (fboundp 'tool-bar-add-item-from-menu)
2476            (default-value 'tool-bar-mode)
2477            (not gnus-summary-tool-bar-map))
2478       (setq gnus-summary-tool-bar-map
2479             (let ((tool-bar-map (make-sparse-keymap))
2480                   (load-path (mm-image-load-path)))
2481               (tool-bar-add-item-from-menu
2482                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2483               (tool-bar-add-item-from-menu
2484                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2485               (tool-bar-add-item-from-menu
2486                'gnus-summary-post-news "post" gnus-summary-mode-map)
2487               (tool-bar-add-item-from-menu
2488                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2489               (tool-bar-add-item-from-menu
2490                'gnus-summary-followup "followup" gnus-summary-mode-map)
2491               (tool-bar-add-item-from-menu
2492                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2493               (tool-bar-add-item-from-menu
2494                'gnus-summary-reply "reply" gnus-summary-mode-map)
2495               (tool-bar-add-item-from-menu
2496                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2497               (tool-bar-add-item-from-menu
2498                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2499               (tool-bar-add-item-from-menu
2500                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2501               (tool-bar-add-item-from-menu
2502                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2503               (tool-bar-add-item-from-menu
2504                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2505               (tool-bar-add-item-from-menu
2506                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2507               (tool-bar-add-item-from-menu
2508                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2509               (tool-bar-add-item-from-menu
2510                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2511               tool-bar-map)))
2512   (if gnus-summary-tool-bar-map
2513       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2514
2515 (defun gnus-score-set-default (var value)
2516   "A version of set that updates the GNU Emacs menu-bar."
2517   (set var value)
2518   ;; It is the message that forces the active status to be updated.
2519   (message ""))
2520
2521 (defun gnus-make-score-map (type)
2522   "Make a summary score map of type TYPE."
2523   (if t
2524       nil
2525     (let ((headers '(("author" "from" string)
2526                      ("subject" "subject" string)
2527                      ("article body" "body" string)
2528                      ("article head" "head" string)
2529                      ("xref" "xref" string)
2530                      ("extra header" "extra" string)
2531                      ("lines" "lines" number)
2532                      ("followups to author" "followup" string)))
2533           (types '((number ("less than" <)
2534                            ("greater than" >)
2535                            ("equal" =))
2536                    (string ("substring" s)
2537                            ("exact string" e)
2538                            ("fuzzy string" f)
2539                            ("regexp" r))))
2540           (perms '(("temporary" (current-time-string))
2541                    ("permanent" nil)
2542                    ("immediate" now)))
2543           header)
2544       (list
2545        (apply
2546         'nconc
2547         (list
2548          (if (eq type 'lower)
2549              "Lower score"
2550            "Increase score"))
2551         (let (outh)
2552           (while headers
2553             (setq header (car headers))
2554             (setq outh
2555                   (cons
2556                    (apply
2557                     'nconc
2558                     (list (car header))
2559                     (let ((ts (cdr (assoc (nth 2 header) types)))
2560                           outt)
2561                       (while ts
2562                         (setq outt
2563                               (cons
2564                                (apply
2565                                 'nconc
2566                                 (list (caar ts))
2567                                 (let ((ps perms)
2568                                       outp)
2569                                   (while ps
2570                                     (setq outp
2571                                           (cons
2572                                            (vector
2573                                             (caar ps)
2574                                             (list
2575                                              'gnus-summary-score-entry
2576                                              (nth 1 header)
2577                                              (if (or (string= (nth 1 header)
2578                                                               "head")
2579                                                      (string= (nth 1 header)
2580                                                               "body"))
2581                                                  ""
2582                                                (list 'gnus-summary-header
2583                                                      (nth 1 header)))
2584                                              (list 'quote (nth 1 (car ts)))
2585                                              (list 'gnus-score-delta-default
2586                                                    nil)
2587                                              (nth 1 (car ps))
2588                                              t)
2589                                             t)
2590                                            outp))
2591                                     (setq ps (cdr ps)))
2592                                   (list (nreverse outp))))
2593                                outt))
2594                         (setq ts (cdr ts)))
2595                       (list (nreverse outt))))
2596                    outh))
2597             (setq headers (cdr headers)))
2598           (list (nreverse outh))))))))
2599
2600 \f
2601
2602 (defun gnus-summary-mode (&optional group)
2603   "Major mode for reading articles.
2604
2605 All normal editing commands are switched off.
2606 \\<gnus-summary-mode-map>
2607 Each line in this buffer represents one article.  To read an
2608 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2609 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2610 respectively.
2611
2612 You can also post articles and send mail from this buffer.  To
2613 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2614 of an article, type `\\[gnus-summary-reply]'.
2615
2616 There are approx. one gazillion commands you can execute in this
2617 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2618
2619 The following commands are available:
2620
2621 \\{gnus-summary-mode-map}"
2622   (interactive)
2623   (kill-all-local-variables)
2624   (when (gnus-visual-p 'summary-menu 'menu)
2625     (gnus-summary-make-menu-bar)
2626     (gnus-summary-make-tool-bar))
2627   (gnus-summary-make-local-variables)
2628   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2629     (gnus-summary-make-local-variables))
2630   (gnus-make-thread-indent-array)
2631   (gnus-simplify-mode-line)
2632   (setq major-mode 'gnus-summary-mode)
2633   (setq mode-name "Summary")
2634   (make-local-variable 'minor-mode-alist)
2635   (use-local-map gnus-summary-mode-map)
2636   (buffer-disable-undo)
2637   (setq buffer-read-only t)             ;Disable modification
2638   (setq truncate-lines t)
2639   (setq selective-display t)
2640   (setq selective-display-ellipses t)   ;Display `...'
2641   (gnus-summary-set-display-table)
2642   (gnus-set-default-directory)
2643   (setq gnus-newsgroup-name group)
2644   (unless (gnus-news-group-p group)
2645     (setq gnus-newsgroup-incorporated
2646           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2647   (make-local-variable 'gnus-summary-line-format)
2648   (make-local-variable 'gnus-summary-line-format-spec)
2649   (make-local-variable 'gnus-summary-dummy-line-format)
2650   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2651   (make-local-variable 'gnus-summary-mark-positions)
2652   (make-local-hook 'pre-command-hook)
2653   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2654   (gnus-run-hooks 'gnus-summary-mode-hook)
2655   (turn-on-gnus-mailing-list-mode)
2656   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2657   (gnus-update-summary-mark-positions))
2658
2659 (defun gnus-summary-make-local-variables ()
2660   "Make all the local summary buffer variables."
2661   (let (global)
2662     (dolist (local gnus-summary-local-variables)
2663       (if (consp local)
2664           (progn
2665             (if (eq (cdr local) 'global)
2666                 ;; Copy the global value of the variable.
2667                 (setq global (symbol-value (car local)))
2668               ;; Use the value from the list.
2669               (setq global (eval (cdr local))))
2670             (set (make-local-variable (car local)) global))
2671         ;; Simple nil-valued local variable.
2672         (set (make-local-variable local) nil)))))
2673
2674 (defun gnus-summary-clear-local-variables ()
2675   (let ((locals gnus-summary-local-variables))
2676     (while locals
2677       (if (consp (car locals))
2678           (and (vectorp (caar locals))
2679                (set (caar locals) nil))
2680         (and (vectorp (car locals))
2681              (set (car locals) nil)))
2682       (setq locals (cdr locals)))))
2683
2684 ;; Summary data functions.
2685
2686 (defmacro gnus-data-number (data)
2687   `(car ,data))
2688
2689 (defmacro gnus-data-set-number (data number)
2690   `(setcar ,data ,number))
2691
2692 (defmacro gnus-data-mark (data)
2693   `(nth 1 ,data))
2694
2695 (defmacro gnus-data-set-mark (data mark)
2696   `(setcar (nthcdr 1 ,data) ,mark))
2697
2698 (defmacro gnus-data-pos (data)
2699   `(nth 2 ,data))
2700
2701 (defmacro gnus-data-set-pos (data pos)
2702   `(setcar (nthcdr 2 ,data) ,pos))
2703
2704 (defmacro gnus-data-header (data)
2705   `(nth 3 ,data))
2706
2707 (defmacro gnus-data-set-header (data header)
2708   `(setcar (nthcdr 3 ,data) ,header))
2709
2710 (defmacro gnus-data-level (data)
2711   `(nth 4 ,data))
2712
2713 (defmacro gnus-data-unread-p (data)
2714   `(= (nth 1 ,data) gnus-unread-mark))
2715
2716 (defmacro gnus-data-read-p (data)
2717   `(/= (nth 1 ,data) gnus-unread-mark))
2718
2719 (defmacro gnus-data-pseudo-p (data)
2720   `(consp (nth 3 ,data)))
2721
2722 (defmacro gnus-data-find (number)
2723   `(assq ,number gnus-newsgroup-data))
2724
2725 (defmacro gnus-data-find-list (number &optional data)
2726   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2727      (memq (assq ,number bdata)
2728            bdata)))
2729
2730 (defmacro gnus-data-make (number mark pos header level)
2731   `(list ,number ,mark ,pos ,header ,level))
2732
2733 (defun gnus-data-enter (after-article number mark pos header level offset)
2734   (let ((data (gnus-data-find-list after-article)))
2735     (unless data
2736       (error "No such article: %d" after-article))
2737     (setcdr data (cons (gnus-data-make number mark pos header level)
2738                        (cdr data)))
2739     (setq gnus-newsgroup-data-reverse nil)
2740     (gnus-data-update-list (cddr data) offset)))
2741
2742 (defun gnus-data-enter-list (after-article list &optional offset)
2743   (when list
2744     (let ((data (and after-article (gnus-data-find-list after-article)))
2745           (ilist list))
2746       (if (not (or data
2747                    after-article))
2748           (let ((odata gnus-newsgroup-data))
2749             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2750             (when offset
2751               (gnus-data-update-list odata offset)))
2752         ;; Find the last element in the list to be spliced into the main
2753         ;; list.
2754         (while (cdr list)
2755           (setq list (cdr list)))
2756         (if (not data)
2757             (progn
2758               (setcdr list gnus-newsgroup-data)
2759               (setq gnus-newsgroup-data ilist)
2760               (when offset
2761                 (gnus-data-update-list (cdr list) offset)))
2762           (setcdr list (cdr data))
2763           (setcdr data ilist)
2764           (when offset
2765             (gnus-data-update-list (cdr list) offset))))
2766       (setq gnus-newsgroup-data-reverse nil))))
2767
2768 (defun gnus-data-remove (article &optional offset)
2769   (let ((data gnus-newsgroup-data))
2770     (if (= (gnus-data-number (car data)) article)
2771         (progn
2772           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2773                 gnus-newsgroup-data-reverse nil)
2774           (when offset
2775             (gnus-data-update-list gnus-newsgroup-data offset)))
2776       (while (cdr data)
2777         (when (= (gnus-data-number (cadr data)) article)
2778           (setcdr data (cddr data))
2779           (when offset
2780             (gnus-data-update-list (cdr data) offset))
2781           (setq data nil
2782                 gnus-newsgroup-data-reverse nil))
2783         (setq data (cdr data))))))
2784
2785 (defmacro gnus-data-list (backward)
2786   `(if ,backward
2787        (or gnus-newsgroup-data-reverse
2788            (setq gnus-newsgroup-data-reverse
2789                  (reverse gnus-newsgroup-data)))
2790      gnus-newsgroup-data))
2791
2792 (defun gnus-data-update-list (data offset)
2793   "Add OFFSET to the POS of all data entries in DATA."
2794   (setq gnus-newsgroup-data-reverse nil)
2795   (while data
2796     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2797     (setq data (cdr data))))
2798
2799 (defun gnus-summary-article-pseudo-p (article)
2800   "Say whether this article is a pseudo article or not."
2801   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2802
2803 (defmacro gnus-summary-article-sparse-p (article)
2804   "Say whether this article is a sparse article or not."
2805   `(memq ,article gnus-newsgroup-sparse))
2806
2807 (defmacro gnus-summary-article-ancient-p (article)
2808   "Say whether this article is a sparse article or not."
2809   `(memq ,article gnus-newsgroup-ancient))
2810
2811 (defun gnus-article-parent-p (number)
2812   "Say whether this article is a parent or not."
2813   (let ((data (gnus-data-find-list number)))
2814     (and (cdr data)                     ; There has to be an article after...
2815          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2816             (gnus-data-level (nth 1 data))))))
2817
2818 (defun gnus-article-children (number)
2819   "Return a list of all children to NUMBER."
2820   (let* ((data (gnus-data-find-list number))
2821          (level (gnus-data-level (car data)))
2822          children)
2823     (setq data (cdr data))
2824     (while (and data
2825                 (= (gnus-data-level (car data)) (1+ level)))
2826       (push (gnus-data-number (car data)) children)
2827       (setq data (cdr data)))
2828     children))
2829
2830 (defmacro gnus-summary-skip-intangible ()
2831   "If the current article is intangible, then jump to a different article."
2832   '(let ((to (get-text-property (point) 'gnus-intangible)))
2833      (and to (gnus-summary-goto-subject to))))
2834
2835 (defmacro gnus-summary-article-intangible-p ()
2836   "Say whether this article is intangible or not."
2837   '(get-text-property (point) 'gnus-intangible))
2838
2839 (defun gnus-article-read-p (article)
2840   "Say whether ARTICLE is read or not."
2841   (not (or (memq article gnus-newsgroup-marked)
2842            (memq article gnus-newsgroup-spam-marked)
2843            (memq article gnus-newsgroup-unreads)
2844            (memq article gnus-newsgroup-unselected)
2845            (memq article gnus-newsgroup-dormant))))
2846
2847 ;; Some summary mode macros.
2848
2849 (defmacro gnus-summary-article-number ()
2850   "The article number of the article on the current line.
2851 If there isn't an article number here, then we return the current
2852 article number."
2853   '(progn
2854      (gnus-summary-skip-intangible)
2855      (or (get-text-property (point) 'gnus-number)
2856          (gnus-summary-last-subject))))
2857
2858 (defmacro gnus-summary-article-header (&optional number)
2859   "Return the header of article NUMBER."
2860   `(gnus-data-header (gnus-data-find
2861                       ,(or number '(gnus-summary-article-number)))))
2862
2863 (defmacro gnus-summary-thread-level (&optional number)
2864   "Return the level of thread that starts with article NUMBER."
2865   `(if (and (eq gnus-summary-make-false-root 'dummy)
2866             (get-text-property (point) 'gnus-intangible))
2867        0
2868      (gnus-data-level (gnus-data-find
2869                        ,(or number '(gnus-summary-article-number))))))
2870
2871 (defmacro gnus-summary-article-mark (&optional number)
2872   "Return the mark of article NUMBER."
2873   `(gnus-data-mark (gnus-data-find
2874                     ,(or number '(gnus-summary-article-number)))))
2875
2876 (defmacro gnus-summary-article-pos (&optional number)
2877   "Return the position of the line of article NUMBER."
2878   `(gnus-data-pos (gnus-data-find
2879                    ,(or number '(gnus-summary-article-number)))))
2880
2881 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2882 (defmacro gnus-summary-article-subject (&optional number)
2883   "Return current subject string or nil if nothing."
2884   `(let ((headers
2885           ,(if number
2886                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2887              '(gnus-data-header (assq (gnus-summary-article-number)
2888                                       gnus-newsgroup-data)))))
2889      (and headers
2890           (vectorp headers)
2891           (mail-header-subject headers))))
2892
2893 (defmacro gnus-summary-article-score (&optional number)
2894   "Return current article score."
2895   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2896                   gnus-newsgroup-scored))
2897        gnus-summary-default-score 0))
2898
2899 (defun gnus-summary-article-children (&optional number)
2900   "Return a list of article numbers that are children of article NUMBER."
2901   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2902          (level (gnus-data-level (car data)))
2903          l children)
2904     (while (and (setq data (cdr data))
2905                 (> (setq l (gnus-data-level (car data))) level))
2906       (and (= (1+ level) l)
2907            (push (gnus-data-number (car data))
2908                  children)))
2909     (nreverse children)))
2910
2911 (defun gnus-summary-article-parent (&optional number)
2912   "Return the article number of the parent of article NUMBER."
2913   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2914                                     (gnus-data-list t)))
2915          (level (gnus-data-level (car data))))
2916     (if (zerop level)
2917         ()                              ; This is a root.
2918       ;; We search until we find an article with a level less than
2919       ;; this one.  That function has to be the parent.
2920       (while (and (setq data (cdr data))
2921                   (not (< (gnus-data-level (car data)) level))))
2922       (and data (gnus-data-number (car data))))))
2923
2924 (defun gnus-unread-mark-p (mark)
2925   "Say whether MARK is the unread mark."
2926   (= mark gnus-unread-mark))
2927
2928 (defun gnus-read-mark-p (mark)
2929   "Say whether MARK is one of the marks that mark as read.
2930 This is all marks except unread, ticked, dormant, and expirable."
2931   (not (or (= mark gnus-unread-mark)
2932            (= mark gnus-ticked-mark)
2933            (= mark gnus-spam-mark)
2934            (= mark gnus-dormant-mark)
2935            (= mark gnus-expirable-mark))))
2936
2937 (defmacro gnus-article-mark (number)
2938   "Return the MARK of article NUMBER.
2939 This macro should only be used when computing the mark the \"first\"
2940 time; i.e., when generating the summary lines.  After that,
2941 `gnus-summary-article-mark' should be used to examine the
2942 marks of articles."
2943   `(cond
2944     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2945     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2946     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2947     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2948     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2949     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2950     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2951     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2952            gnus-ancient-mark))))
2953
2954 ;; Saving hidden threads.
2955
2956 (defmacro gnus-save-hidden-threads (&rest forms)
2957   "Save hidden threads, eval FORMS, and restore the hidden threads."
2958   (let ((config (make-symbol "config")))
2959     `(let ((,config (gnus-hidden-threads-configuration)))
2960        (unwind-protect
2961            (save-excursion
2962              ,@forms)
2963          (gnus-restore-hidden-threads-configuration ,config)))))
2964 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2965 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2966
2967 (defun gnus-data-compute-positions ()
2968   "Compute the positions of all articles."
2969   (setq gnus-newsgroup-data-reverse nil)
2970   (let ((data gnus-newsgroup-data))
2971     (save-excursion
2972       (gnus-save-hidden-threads
2973         (gnus-summary-show-all-threads)
2974         (goto-char (point-min))
2975         (while data
2976           (while (get-text-property (point) 'gnus-intangible)
2977             (forward-line 1))
2978           (gnus-data-set-pos (car data) (+ (point) 3))
2979           (setq data (cdr data))
2980           (forward-line 1))))))
2981
2982 (defun gnus-hidden-threads-configuration ()
2983   "Return the current hidden threads configuration."
2984   (save-excursion
2985     (let (config)
2986       (goto-char (point-min))
2987       (while (search-forward "\r" nil t)
2988         (push (1- (point)) config))
2989       config)))
2990
2991 (defun gnus-restore-hidden-threads-configuration (config)
2992   "Restore hidden threads configuration from CONFIG."
2993   (save-excursion
2994     (let (point buffer-read-only)
2995       (while (setq point (pop config))
2996         (when (and (< point (point-max))
2997                    (goto-char point)
2998                    (eq (char-after) ?\n))
2999           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3000
3001 ;; Various summary mode internalish functions.
3002
3003 (defun gnus-mouse-pick-article (e)
3004   (interactive "e")
3005   (mouse-set-point e)
3006   (gnus-summary-next-page nil t))
3007
3008 (defun gnus-summary-set-display-table ()
3009   "Change the display table.
3010 Odd characters have a tendency to mess
3011 up nicely formatted displays - we make all possible glyphs
3012 display only a single character."
3013
3014   ;; We start from the standard display table, if any.
3015   (let ((table (or (copy-sequence standard-display-table)
3016                    (make-display-table)))
3017         (i 32))
3018     ;; Nix out all the control chars...
3019     (while (>= (setq i (1- i)) 0)
3020       (aset table i [??]))
3021     ;; ... but not newline and cr, of course.  (cr is necessary for the
3022     ;; selective display).
3023     (aset table ?\n nil)
3024     (aset table ?\r nil)
3025     ;; We keep TAB as well.
3026     (aset table ?\t nil)
3027     ;; We nix out any glyphs over 126 that are not set already.
3028     (let ((i 256))
3029       (while (>= (setq i (1- i)) 127)
3030         ;; Only modify if the entry is nil.
3031         (unless (aref table i)
3032           (aset table i [??]))))
3033     (setq buffer-display-table table)))
3034
3035 (defun gnus-summary-set-article-display-arrow (pos)
3036   "Update the overlay arrow to point to line at position POS."
3037   (when (and gnus-summary-display-arrow
3038              (boundp 'overlay-arrow-position)
3039              (boundp 'overlay-arrow-string))
3040     (save-excursion
3041       (goto-char pos)
3042       (beginning-of-line)
3043       (unless overlay-arrow-position
3044         (setq overlay-arrow-position (make-marker)))
3045       (setq overlay-arrow-string "=>"
3046             overlay-arrow-position (set-marker overlay-arrow-position
3047                                                (point)
3048                                                (current-buffer))))))
3049
3050 (defun gnus-summary-setup-buffer (group)
3051   "Initialize summary buffer."
3052   (let ((buffer (gnus-summary-buffer-name group))
3053         (dead-name (concat "*Dead Summary "
3054                            (gnus-group-decoded-name group) "*")))
3055     ;; If a dead summary buffer exists, we kill it.
3056     (when (gnus-buffer-live-p dead-name)
3057       (gnus-kill-buffer dead-name))
3058     (if (get-buffer buffer)
3059         (progn
3060           (set-buffer buffer)
3061           (setq gnus-summary-buffer (current-buffer))
3062           (not gnus-newsgroup-prepared))
3063       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3064       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3065       (gnus-summary-mode group)
3066       (when gnus-carpal
3067         (gnus-carpal-setup-buffer 'summary))
3068       (unless gnus-single-article-buffer
3069         (make-local-variable 'gnus-article-buffer)
3070         (make-local-variable 'gnus-article-current)
3071         (make-local-variable 'gnus-original-article-buffer))
3072       (setq gnus-newsgroup-name group)
3073       ;; Set any local variables in the group parameters.
3074       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3075       t)))
3076
3077 (defun gnus-set-global-variables ()
3078   "Set the global equivalents of the buffer-local variables.
3079 They are set to the latest values they had.  These reflect the summary
3080 buffer that was in action when the last article was fetched."
3081   (when (eq major-mode 'gnus-summary-mode)
3082     (setq gnus-summary-buffer (current-buffer))
3083     (let ((name gnus-newsgroup-name)
3084           (marked gnus-newsgroup-marked)
3085           (spam gnus-newsgroup-spam-marked)
3086           (unread gnus-newsgroup-unreads)
3087           (headers gnus-current-headers)
3088           (data gnus-newsgroup-data)
3089           (summary gnus-summary-buffer)
3090           (article-buffer gnus-article-buffer)
3091           (original gnus-original-article-buffer)
3092           (gac gnus-article-current)
3093           (reffed gnus-reffed-article-number)
3094           (score-file gnus-current-score-file)
3095           (default-charset gnus-newsgroup-charset)
3096           vlist)
3097       (let ((locals gnus-newsgroup-variables))
3098         (while locals
3099           (if (consp (car locals))
3100               (push (eval (caar locals)) vlist)
3101             (push (eval (car locals)) vlist))
3102           (setq locals (cdr locals)))
3103         (setq vlist (nreverse vlist)))
3104       (save-excursion
3105         (set-buffer gnus-group-buffer)
3106         (setq gnus-newsgroup-name name
3107               gnus-newsgroup-marked marked
3108               gnus-newsgroup-spam-marked spam
3109               gnus-newsgroup-unreads unread
3110               gnus-current-headers headers
3111               gnus-newsgroup-data data
3112               gnus-article-current gac
3113               gnus-summary-buffer summary
3114               gnus-article-buffer article-buffer
3115               gnus-original-article-buffer original
3116               gnus-reffed-article-number reffed
3117               gnus-current-score-file score-file
3118               gnus-newsgroup-charset default-charset)
3119         (let ((locals gnus-newsgroup-variables))
3120           (while locals
3121             (if (consp (car locals))
3122                 (set (caar locals) (pop vlist))
3123               (set (car locals) (pop vlist)))
3124             (setq locals (cdr locals))))
3125         ;; The article buffer also has local variables.
3126         (when (gnus-buffer-live-p gnus-article-buffer)
3127           (set-buffer gnus-article-buffer)
3128           (setq gnus-summary-buffer summary))))))
3129
3130 (defun gnus-summary-article-unread-p (article)
3131   "Say whether ARTICLE is unread or not."
3132   (memq article gnus-newsgroup-unreads))
3133
3134 (defun gnus-summary-first-article-p (&optional article)
3135   "Return whether ARTICLE is the first article in the buffer."
3136   (if (not (setq article (or article (gnus-summary-article-number))))
3137       nil
3138     (eq article (caar gnus-newsgroup-data))))
3139
3140 (defun gnus-summary-last-article-p (&optional article)
3141   "Return whether ARTICLE is the last article in the buffer."
3142   (if (not (setq article (or article (gnus-summary-article-number))))
3143       ;; All non-existent numbers are the last article.  :-)
3144       t
3145     (not (cdr (gnus-data-find-list article)))))
3146
3147 (defun gnus-make-thread-indent-array ()
3148   (let ((n 200))
3149     (unless (and gnus-thread-indent-array
3150                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3151       (setq gnus-thread-indent-array (make-vector 201 "")
3152             gnus-thread-indent-array-level gnus-thread-indent-level)
3153       (while (>= n 0)
3154         (aset gnus-thread-indent-array n
3155               (make-string (* n gnus-thread-indent-level) ? ))
3156         (setq n (1- n))))))
3157
3158 (defun gnus-update-summary-mark-positions ()
3159   "Compute where the summary marks are to go."
3160   (save-excursion
3161     (when (gnus-buffer-exists-p gnus-summary-buffer)
3162       (set-buffer gnus-summary-buffer))
3163     (let ((gnus-replied-mark 129)
3164           (gnus-score-below-mark 130)
3165           (gnus-score-over-mark 130)
3166           (gnus-undownloaded-mark 131)
3167           (spec gnus-summary-line-format-spec)
3168           gnus-visual pos)
3169       (save-excursion
3170         (gnus-set-work-buffer)
3171         (let ((gnus-summary-line-format-spec spec)
3172               (gnus-newsgroup-downloadable '(0)))
3173           (gnus-summary-insert-line
3174            (make-full-mail-header 0 "" "nobody"
3175                                   "05 Apr 2001 23:33:09 +0400"
3176                                   "" "" 0 0 "" nil)
3177            0 nil t 128 t nil "" nil 1)
3178           (goto-char (point-min))
3179           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3180                                              (- (point) (point-min) 1)))))
3181           (goto-char (point-min))
3182           (push (cons 'replied (and (search-forward "\201" nil t)
3183                                     (- (point) (point-min) 1)))
3184                 pos)
3185           (goto-char (point-min))
3186           (push (cons 'score (and (search-forward "\202" nil t)
3187                                   (- (point) (point-min) 1)))
3188                 pos)
3189           (goto-char (point-min))
3190           (push (cons 'download
3191                       (and (search-forward "\203" nil t)
3192                            (- (point) (point-min) 1)))
3193                 pos)))
3194       (setq gnus-summary-mark-positions pos))))
3195
3196 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3197   "Insert a dummy root in the summary buffer."
3198   (beginning-of-line)
3199   (gnus-add-text-properties
3200    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3201    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3202
3203 (defun gnus-summary-extract-address-component (from)
3204   (or (car (funcall gnus-extract-address-components from))
3205       from))
3206
3207 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3208   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3209                                 default-mime-charset)))
3210     ;; Is it really necessary to do this next part for each summary line?
3211     ;; Luckily, doesn't seem to slow things down much.
3212     (or
3213      (and gnus-ignored-from-addresses
3214           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3215           (let ((extra-headers (mail-header-extra header))
3216                 to
3217                 newsgroups)
3218             (cond
3219              ((setq to (cdr (assq 'To extra-headers)))
3220               (concat "-> "
3221                       (inline
3222                         (gnus-summary-extract-address-component
3223                          (funcall gnus-decode-encoded-word-function to)))))
3224              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3225               (concat "=> " newsgroups)))))
3226      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3227
3228 (defun gnus-summary-insert-line (gnus-tmp-header
3229                                  gnus-tmp-level gnus-tmp-current
3230                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3231                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3232                                  &optional gnus-tmp-dummy gnus-tmp-score
3233                                  gnus-tmp-process)
3234   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3235          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3236          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3237          (gnus-tmp-score-char
3238           (if (or (null gnus-summary-default-score)
3239                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3240                       gnus-summary-zcore-fuzz))
3241               ?\ ;;;Whitespace
3242             (if (< gnus-tmp-score gnus-summary-default-score)
3243                 gnus-score-below-mark gnus-score-over-mark)))
3244          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3245          (gnus-tmp-replied
3246           (cond (gnus-tmp-process gnus-process-mark)
3247                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3248                  gnus-cached-mark)
3249                 (gnus-tmp-replied gnus-replied-mark)
3250                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3251                  gnus-forwarded-mark)
3252                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3253                  gnus-saved-mark)
3254                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3255                  gnus-recent-mark)
3256                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3257                  gnus-unseen-mark)
3258                 (t gnus-no-mark)))
3259          (gnus-tmp-downloaded
3260           (cond (undownloaded 
3261                  gnus-undownloaded-mark)
3262                 (gnus-newsgroup-agentized
3263                  gnus-downloaded-mark)
3264                 (t
3265                  gnus-no-mark)))
3266          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3267          (gnus-tmp-name
3268           (cond
3269            ((string-match "<[^>]+> *$" gnus-tmp-from)
3270             (let ((beg (match-beginning 0)))
3271               (or (and (string-match "^\".+\"" gnus-tmp-from)
3272                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3273                   (substring gnus-tmp-from 0 beg))))
3274            ((string-match "(.+)" gnus-tmp-from)
3275             (substring gnus-tmp-from
3276                        (1+ (match-beginning 0)) (1- (match-end 0))))
3277            (t gnus-tmp-from)))
3278          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3279          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3280          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3281          (buffer-read-only nil))
3282     (when (string= gnus-tmp-name "")
3283       (setq gnus-tmp-name gnus-tmp-from))
3284     (unless (numberp gnus-tmp-lines)
3285       (setq gnus-tmp-lines -1))
3286     (if (= gnus-tmp-lines -1)
3287         (setq gnus-tmp-lines "?")
3288       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3289     (gnus-put-text-property-excluding-characters-with-faces
3290      (point)
3291      (progn (eval gnus-summary-line-format-spec) (point))
3292      'gnus-number gnus-tmp-number)
3293     (when (gnus-visual-p 'summary-highlight 'highlight)
3294       (forward-line -1)
3295       (gnus-run-hooks 'gnus-summary-update-hook)
3296       (forward-line 1))))
3297
3298 (defun gnus-summary-update-line (&optional dont-update)
3299   "Update summary line after change."
3300   (when (and gnus-summary-default-score
3301              (not gnus-summary-inhibit-highlight))
3302     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3303            (article (gnus-summary-article-number))
3304            (score (gnus-summary-article-score article)))
3305       (unless dont-update
3306         (if (and gnus-summary-mark-below
3307                  (< (gnus-summary-article-score)
3308                     gnus-summary-mark-below))
3309             ;; This article has a low score, so we mark it as read.
3310             (when (memq article gnus-newsgroup-unreads)
3311               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3312           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3313             ;; This article was previously marked as read on account
3314             ;; of a low score, but now it has risen, so we mark it as
3315             ;; unread.
3316             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3317         (gnus-summary-update-mark
3318          (if (or (null gnus-summary-default-score)
3319                  (<= (abs (- score gnus-summary-default-score))
3320                      gnus-summary-zcore-fuzz))
3321              ?\ ;;;Whitespace
3322            (if (< score gnus-summary-default-score)
3323                gnus-score-below-mark gnus-score-over-mark))
3324          'score))
3325       ;; Do visual highlighting.
3326       (when (gnus-visual-p 'summary-highlight 'highlight)
3327         (gnus-run-hooks 'gnus-summary-update-hook)))))
3328
3329 (defvar gnus-tmp-new-adopts nil)
3330
3331 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3332   "Return the number of articles in THREAD.
3333 This may be 0 in some cases -- if none of the articles in
3334 the thread are to be displayed."
3335   (let* ((number
3336           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3337           (cond
3338            ((not (listp thread))
3339             1)
3340            ((and (consp thread) (cdr thread))
3341             (apply
3342              '+ 1 (mapcar
3343                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3344            ((null thread)
3345             1)
3346            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3347             1)
3348            (t 0))))
3349     (when (and level (zerop level) gnus-tmp-new-adopts)
3350       (incf number
3351             (apply '+ (mapcar
3352                        'gnus-summary-number-of-articles-in-thread
3353                        gnus-tmp-new-adopts))))
3354     (if char
3355         (if (> number 1) gnus-not-empty-thread-mark
3356           gnus-empty-thread-mark)
3357       number)))
3358
3359 (defsubst gnus-summary-line-message-size (head)
3360   "Return pretty-printed version of message size.
3361 This function is intended to be used in
3362 `gnus-summary-line-format-alist', which see."
3363   (let ((c (or (mail-header-chars head) -1)))
3364     (cond ((< c 0) "n/a")               ; chars not available
3365           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3366           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3367           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3368           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3369
3370
3371 (defun gnus-summary-set-local-parameters (group)
3372   "Go through the local params of GROUP and set all variable specs in that list."
3373   (let ((params (gnus-group-find-parameter group))
3374         (vars '(quit-config))           ; Ignore quit-config.
3375         elem)
3376     (while params
3377       (setq elem (car params)
3378             params (cdr params))
3379       (and (consp elem)                 ; Has to be a cons.
3380            (consp (cdr elem))           ; The cdr has to be a list.
3381            (symbolp (car elem))         ; Has to be a symbol in there.
3382            (not (memq (car elem) vars))
3383            (ignore-errors               ; So we set it.
3384              (push (car elem) vars)
3385              (make-local-variable (car elem))
3386              (set (car elem) (eval (nth 1 elem))))))))
3387
3388 (defun gnus-summary-read-group (group &optional show-all no-article
3389                                       kill-buffer no-display backward
3390                                       select-articles)
3391   "Start reading news in newsgroup GROUP.
3392 If SHOW-ALL is non-nil, already read articles are also listed.
3393 If NO-ARTICLE is non-nil, no article is selected initially.
3394 If NO-DISPLAY, don't generate a summary buffer."
3395   (let (result)
3396     (while (and group
3397                 (null (setq result
3398                             (let ((gnus-auto-select-next nil))
3399                               (or (gnus-summary-read-group-1
3400                                    group show-all no-article
3401                                    kill-buffer no-display
3402                                    select-articles)
3403                                   (setq show-all nil
3404                                         select-articles nil)))))
3405                 (eq gnus-auto-select-next 'quietly))
3406       (set-buffer gnus-group-buffer)
3407       ;; The entry function called above goes to the next
3408       ;; group automatically, so we go two groups back
3409       ;; if we are searching for the previous group.
3410       (when backward
3411         (gnus-group-prev-unread-group 2))
3412       (if (not (equal group (gnus-group-group-name)))
3413           (setq group (gnus-group-group-name))
3414         (setq group nil)))
3415     result))
3416
3417 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3418   "Directly jump to the other GROUP from summary buffer.
3419 If SHOW-ALL is non-nil, already read articles are also listed."
3420   (interactive
3421    (if (eq gnus-summary-buffer (current-buffer))
3422        (list (completing-read
3423               "Group: " gnus-active-hashtb nil t
3424               (when (and gnus-newsgroup-name
3425                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3426                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3427               'gnus-group-history)
3428              current-prefix-arg)
3429      (error "%s must be invoked from a gnus summary buffer." this-command)))
3430   (unless (or (zerop (length group))
3431               (and gnus-newsgroup-name
3432                    (string-equal gnus-newsgroup-name group)))
3433     (gnus-summary-exit)
3434     (gnus-summary-read-group group show-all
3435                              gnus-dont-select-after-jump-to-other-group)))
3436
3437 (defun gnus-summary-read-group-1 (group show-all no-article
3438                                         kill-buffer no-display
3439                                         &optional select-articles)
3440   ;; Killed foreign groups can't be entered.
3441   ;;  (when (and (not (gnus-group-native-p group))
3442   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3443   ;;    (error "Dead non-native groups can't be entered"))
3444   (gnus-message 5 "Retrieving newsgroup: %s..."
3445                 (gnus-group-decoded-name group))
3446   (let* ((new-group (gnus-summary-setup-buffer group))
3447          (quit-config (gnus-group-quit-config group))
3448          (did-select (and new-group (gnus-select-newsgroup
3449                                      group show-all select-articles))))
3450     (cond
3451      ;; This summary buffer exists already, so we just select it.
3452      ((not new-group)
3453       (gnus-set-global-variables)
3454       (when kill-buffer
3455         (gnus-kill-or-deaden-summary kill-buffer))
3456       (gnus-configure-windows 'summary 'force)
3457       (gnus-set-mode-line 'summary)
3458       (gnus-summary-position-point)
3459       (message "")
3460       t)
3461      ;; We couldn't select this group.
3462      ((null did-select)
3463       (when (and (eq major-mode 'gnus-summary-mode)
3464                  (not (equal (current-buffer) kill-buffer)))
3465         (kill-buffer (current-buffer))
3466         (if (not quit-config)
3467             (progn
3468               ;; Update the info -- marks might need to be removed,
3469               ;; for instance.
3470               (gnus-summary-update-info)
3471               (set-buffer gnus-group-buffer)
3472               (gnus-group-jump-to-group group)
3473               (gnus-group-next-unread-group 1))
3474           (gnus-handle-ephemeral-exit quit-config)))
3475       (let ((grpinfo (gnus-get-info group)))
3476         (if (null (gnus-info-read grpinfo))
3477             (gnus-message 3 "Group %s contains no messages"
3478                           (gnus-group-decoded-name group))
3479           (gnus-message 3 "Can't select group")))
3480       nil)
3481      ;; The user did a `C-g' while prompting for number of articles,
3482      ;; so we exit this group.
3483      ((eq did-select 'quit)
3484       (and (eq major-mode 'gnus-summary-mode)
3485            (not (equal (current-buffer) kill-buffer))
3486            (kill-buffer (current-buffer)))
3487       (when kill-buffer
3488         (gnus-kill-or-deaden-summary kill-buffer))
3489       (if (not quit-config)
3490           (progn
3491             (set-buffer gnus-group-buffer)
3492             (gnus-group-jump-to-group group)
3493             (gnus-group-next-unread-group 1)
3494             (gnus-configure-windows 'group 'force))
3495         (gnus-handle-ephemeral-exit quit-config))
3496       ;; Finally signal the quit.
3497       (signal 'quit nil))
3498      ;; The group was successfully selected.
3499      (t
3500       (gnus-set-global-variables)
3501       ;; Save the active value in effect when the group was entered.
3502       (setq gnus-newsgroup-active
3503             (gnus-copy-sequence
3504              (gnus-active gnus-newsgroup-name)))
3505       ;; You can change the summary buffer in some way with this hook.
3506       (gnus-run-hooks 'gnus-select-group-hook)
3507       (gnus-update-format-specifications
3508        nil 'summary 'summary-mode 'summary-dummy)
3509       (gnus-update-summary-mark-positions)
3510       ;; Do score processing.
3511       (when gnus-use-scoring
3512         (gnus-possibly-score-headers))
3513       ;; Check whether to fill in the gaps in the threads.
3514       (when gnus-build-sparse-threads
3515         (gnus-build-sparse-threads))
3516       ;; Find the initial limit.
3517       (if gnus-show-threads
3518           (if show-all
3519               (let ((gnus-newsgroup-dormant nil))
3520                 (gnus-summary-initial-limit show-all))
3521             (gnus-summary-initial-limit show-all))
3522         ;; When unthreaded, all articles are always shown.
3523         (setq gnus-newsgroup-limit
3524               (mapcar
3525                (lambda (header) (mail-header-number header))
3526                gnus-newsgroup-headers)))
3527       ;; Generate the summary buffer.
3528       (unless no-display
3529         (gnus-summary-prepare))
3530       (when gnus-use-trees
3531         (gnus-tree-open group)
3532         (setq gnus-summary-highlight-line-function
3533               'gnus-tree-highlight-article))
3534       ;; If the summary buffer is empty, but there are some low-scored
3535       ;; articles or some excluded dormants, we include these in the
3536       ;; buffer.
3537       (when (and (zerop (buffer-size))
3538                  (not no-display))
3539         (cond (gnus-newsgroup-dormant
3540                (gnus-summary-limit-include-dormant))
3541               ((and gnus-newsgroup-scored show-all)
3542                (gnus-summary-limit-include-expunged t))))
3543       ;; Function `gnus-apply-kill-file' must be called in this hook.
3544       (gnus-run-hooks 'gnus-apply-kill-hook)
3545       (if (and (zerop (buffer-size))
3546                (not no-display))
3547           (progn
3548             ;; This newsgroup is empty.
3549             (gnus-summary-catchup-and-exit nil t)
3550             (gnus-message 6 "No unread news")
3551             (when kill-buffer
3552               (gnus-kill-or-deaden-summary kill-buffer))
3553             ;; Return nil from this function.
3554             nil)
3555         ;; Hide conversation thread subtrees.  We cannot do this in
3556         ;; gnus-summary-prepare-hook since kill processing may not
3557         ;; work with hidden articles.
3558         (gnus-summary-maybe-hide-threads)
3559         (when kill-buffer
3560           (gnus-kill-or-deaden-summary kill-buffer))
3561         (gnus-summary-auto-select-subject)
3562         ;; Show first unread article if requested.
3563         (if (and (not no-article)
3564                  (not no-display)
3565                  gnus-newsgroup-unreads
3566                  gnus-auto-select-first)
3567             (progn
3568               (gnus-configure-windows 'summary)
3569               (let ((art (gnus-summary-article-number)))
3570                 (unless (and (not gnus-plugged)
3571                              (or (memq art gnus-newsgroup-undownloaded)
3572                                  (memq art gnus-newsgroup-downloadable)))
3573                   (gnus-summary-goto-article art))))
3574           ;; Don't select any articles.
3575           (gnus-summary-position-point)
3576           (gnus-configure-windows 'summary 'force)
3577           (gnus-set-mode-line 'summary))
3578         (when (and gnus-auto-center-group
3579                    (get-buffer-window gnus-group-buffer t))
3580           ;; Gotta use windows, because recenter does weird stuff if
3581           ;; the current buffer ain't the displayed window.
3582           (let ((owin (selected-window)))
3583             (select-window (get-buffer-window gnus-group-buffer t))
3584             (when (gnus-group-goto-group group)
3585               (recenter))
3586             (select-window owin)))
3587         ;; Mark this buffer as "prepared".
3588         (setq gnus-newsgroup-prepared t)
3589         (gnus-run-hooks 'gnus-summary-prepared-hook)
3590         (unless (gnus-ephemeral-group-p group)
3591           (gnus-group-update-group group))
3592         t)))))
3593
3594 (defun gnus-summary-auto-select-subject ()
3595   "Select the subject line on initial group entry."
3596   (goto-char (point-min))
3597   (cond
3598    ((eq gnus-auto-select-subject 'best)
3599     (gnus-summary-best-unread-subject))
3600    ((eq gnus-auto-select-subject 'unread)
3601     (gnus-summary-first-unread-subject))
3602    ((eq gnus-auto-select-subject 'unseen)
3603     (gnus-summary-first-unseen-subject))
3604    ((eq gnus-auto-select-subject 'unseen-or-unread)
3605     (gnus-summary-first-unseen-or-unread-subject))
3606    ((eq gnus-auto-select-subject 'first)
3607     ;; Do nothing.
3608     )
3609    ((gnus-functionp gnus-auto-select-subject)
3610     (funcall gnus-auto-select-subject))))
3611
3612 (defun gnus-summary-prepare ()
3613   "Generate the summary buffer."
3614   (interactive)
3615   (let ((buffer-read-only nil))
3616     (erase-buffer)
3617     (setq gnus-newsgroup-data nil
3618           gnus-newsgroup-data-reverse nil)
3619     (gnus-run-hooks 'gnus-summary-generate-hook)
3620     ;; Generate the buffer, either with threads or without.
3621     (when gnus-newsgroup-headers
3622       (gnus-summary-prepare-threads
3623        (if gnus-show-threads
3624            (gnus-sort-gathered-threads
3625             (funcall gnus-summary-thread-gathering-function
3626                      (gnus-sort-threads
3627                       (gnus-cut-threads (gnus-make-threads)))))
3628          ;; Unthreaded display.
3629          (gnus-sort-articles gnus-newsgroup-headers))))
3630     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3631     ;; Call hooks for modifying summary buffer.
3632     (goto-char (point-min))
3633     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3634
3635 (defsubst gnus-general-simplify-subject (subject)
3636   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3637   (setq subject
3638         (cond
3639          ;; Truncate the subject.
3640          (gnus-simplify-subject-functions
3641           (gnus-map-function gnus-simplify-subject-functions subject))
3642          ((numberp gnus-summary-gather-subject-limit)
3643           (setq subject (gnus-simplify-subject-re subject))
3644           (if (> (length subject) gnus-summary-gather-subject-limit)
3645               (substring subject 0 gnus-summary-gather-subject-limit)
3646             subject))
3647          ;; Fuzzily simplify it.
3648          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3649           (gnus-simplify-subject-fuzzy subject))
3650          ;; Just remove the leading "Re:".
3651          (t
3652           (gnus-simplify-subject-re subject))))
3653
3654   (if (and gnus-summary-gather-exclude-subject
3655            (string-match gnus-summary-gather-exclude-subject subject))
3656       nil                               ; This article shouldn't be gathered
3657     subject))
3658
3659 (defun gnus-summary-simplify-subject-query ()
3660   "Query where the respool algorithm would put this article."
3661   (interactive)
3662   (gnus-summary-select-article)
3663   (message "%s"
3664            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3665
3666 (defun gnus-gather-threads-by-subject (threads)
3667   "Gather threads by looking at Subject headers."
3668   (if (not gnus-summary-make-false-root)
3669       threads
3670     (let ((hashtb (gnus-make-hashtable 1024))
3671           (prev threads)
3672           (result threads)
3673           subject hthread whole-subject)
3674       (while threads
3675         (setq subject (gnus-general-simplify-subject
3676                        (setq whole-subject (mail-header-subject
3677                                             (caar threads)))))
3678         (when subject
3679           (if (setq hthread (gnus-gethash subject hashtb))
3680               (progn
3681                 ;; We enter a dummy root into the thread, if we
3682                 ;; haven't done that already.
3683                 (unless (stringp (caar hthread))
3684                   (setcar hthread (list whole-subject (car hthread))))
3685                 ;; We add this new gathered thread to this gathered
3686                 ;; thread.
3687                 (setcdr (car hthread)
3688                         (nconc (cdar hthread) (list (car threads))))
3689                 ;; Remove it from the list of threads.
3690                 (setcdr prev (cdr threads))
3691                 (setq threads prev))
3692             ;; Enter this thread into the hash table.
3693             (gnus-sethash subject
3694                           (if gnus-summary-make-false-root-always
3695                               (progn
3696                                 ;; If you want a dummy root above all
3697                                 ;; threads...
3698                                 (setcar threads (list whole-subject
3699                                                       (car threads)))
3700                                 threads)
3701                             threads)
3702                           hashtb)))
3703         (setq prev threads)
3704         (setq threads (cdr threads)))
3705       result)))
3706
3707 (defun gnus-gather-threads-by-references (threads)
3708   "Gather threads by looking at References headers."
3709   (let ((idhashtb (gnus-make-hashtable 1024))
3710         (thhashtb (gnus-make-hashtable 1024))
3711         (prev threads)
3712         (result threads)
3713         ids references id gthread gid entered ref)
3714     (while threads
3715       (when (setq references (mail-header-references (caar threads)))
3716         (setq id (mail-header-id (caar threads))
3717               ids (inline (gnus-split-references references))
3718               entered nil)
3719         (while (setq ref (pop ids))
3720           (setq ids (delete ref ids))
3721           (if (not (setq gid (gnus-gethash ref idhashtb)))
3722               (progn
3723                 (gnus-sethash ref id idhashtb)
3724                 (gnus-sethash id threads thhashtb))
3725             (setq gthread (gnus-gethash gid thhashtb))
3726             (unless entered
3727               ;; We enter a dummy root into the thread, if we
3728               ;; haven't done that already.
3729               (unless (stringp (caar gthread))
3730                 (setcar gthread (list (mail-header-subject (caar gthread))
3731                                       (car gthread))))
3732               ;; We add this new gathered thread to this gathered
3733               ;; thread.
3734               (setcdr (car gthread)
3735                       (nconc (cdar gthread) (list (car threads)))))
3736             ;; Add it into the thread hash table.
3737             (gnus-sethash id gthread thhashtb)
3738             (setq entered t)
3739             ;; Remove it from the list of threads.
3740             (setcdr prev (cdr threads))
3741             (setq threads prev))))
3742       (setq prev threads)
3743       (setq threads (cdr threads)))
3744     result))
3745
3746 (defun gnus-sort-gathered-threads (threads)
3747   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3748   (let ((result threads))
3749     (while threads
3750       (when (stringp (caar threads))
3751         (setcdr (car threads)
3752                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3753       (setq threads (cdr threads)))
3754     result))
3755
3756 (defun gnus-thread-loop-p (root thread)
3757   "Say whether ROOT is in THREAD."
3758   (let ((stack (list thread))
3759         (infloop 0)
3760         th)
3761     (while (setq thread (pop stack))
3762       (setq th (cdr thread))
3763       (while (and th
3764                   (not (eq (caar th) root)))
3765         (pop th))
3766       (if th
3767           ;; We have found a loop.
3768           (let (ref-dep)
3769             (setcdr thread (delq (car th) (cdr thread)))
3770             (if (boundp (setq ref-dep (intern "none"
3771                                               gnus-newsgroup-dependencies)))
3772                 (setcdr (symbol-value ref-dep)
3773                         (nconc (cdr (symbol-value ref-dep))
3774                                (list (car th))))
3775               (set ref-dep (list nil (car th))))
3776             (setq infloop 1
3777                   stack nil))
3778         ;; Push all the subthreads onto the stack.
3779         (push (cdr thread) stack)))
3780     infloop))
3781
3782 (defun gnus-make-threads ()
3783   "Go through the dependency hashtb and find the roots.  Return all threads."
3784   (let (threads)
3785     (while (catch 'infloop
3786              (mapatoms
3787               (lambda (refs)
3788                 ;; Deal with self-referencing References loops.
3789                 (when (and (car (symbol-value refs))
3790                            (not (zerop
3791                                  (apply
3792                                   '+
3793                                   (mapcar
3794                                    (lambda (thread)
3795                                      (gnus-thread-loop-p
3796                                       (car (symbol-value refs)) thread))
3797                                    (cdr (symbol-value refs)))))))
3798                   (setq threads nil)
3799                   (throw 'infloop t))
3800                 (unless (car (symbol-value refs))
3801                   ;; These threads do not refer back to any other articles,
3802                   ;; so they're roots.
3803                   (setq threads (append (cdr (symbol-value refs)) threads))))
3804               gnus-newsgroup-dependencies)))
3805     threads))
3806
3807 ;; Build the thread tree.
3808 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3809   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3810
3811 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3812 if it was already present.
3813
3814 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3815 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3816 Message-IDs will be renamed to a unique Message-ID before being
3817 entered.
3818
3819 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3820   (let* ((id (mail-header-id header))
3821          (id-dep (and id (intern id dependencies)))
3822          parent-id ref ref-dep ref-header replaced)
3823     ;; Enter this `header' in the `dependencies' table.
3824     (cond
3825      ((not id-dep)
3826       (setq header nil))
3827      ;; The first two cases do the normal part: enter a new `header'
3828      ;; in the `dependencies' table.
3829      ((not (boundp id-dep))
3830       (set id-dep (list header)))
3831      ((null (car (symbol-value id-dep)))
3832       (setcar (symbol-value id-dep) header))
3833
3834      ;; From here the `header' was already present in the
3835      ;; `dependencies' table.
3836      (force-new
3837       ;; Overrides an existing entry;
3838       ;; just set the header part of the entry.
3839       (setcar (symbol-value id-dep) header)
3840       (setq replaced t))
3841
3842      ;; Renames the existing `header' to a unique Message-ID.
3843      ((not gnus-summary-ignore-duplicates)
3844       ;; An article with this Message-ID has already been seen.
3845       ;; We rename the Message-ID.
3846       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3847            (list header))
3848       (mail-header-set-id header id))
3849
3850      ;; The last case ignores an existing entry, except it adds any
3851      ;; additional Xrefs (in case the two articles came from different
3852      ;; servers.
3853      ;; Also sets `header' to `nil' meaning that the `dependencies'
3854      ;; table was *not* modified.
3855      (t
3856       (mail-header-set-xref
3857        (car (symbol-value id-dep))
3858        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3859                    "")
3860                (or (mail-header-xref header) "")))
3861       (setq header nil)))
3862
3863     (when (and header (not replaced))
3864       ;; First check that we are not creating a References loop.
3865       (setq parent-id (gnus-parent-id (mail-header-references header)))
3866       (setq ref parent-id)
3867       (while (and ref
3868                   (setq ref-dep (intern-soft ref dependencies))
3869                   (boundp ref-dep)
3870                   (setq ref-header (car (symbol-value ref-dep))))
3871         (if (string= id ref)
3872             ;; Yuk!  This is a reference loop.  Make the article be a
3873             ;; root article.
3874             (progn
3875               (mail-header-set-references (car (symbol-value id-dep)) "none")
3876               (setq ref nil)
3877               (setq parent-id nil))
3878           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3879       (setq ref-dep (intern (or parent-id "none") dependencies))
3880       (if (boundp ref-dep)
3881           (setcdr (symbol-value ref-dep)
3882                   (nconc (cdr (symbol-value ref-dep))
3883                          (list (symbol-value id-dep))))
3884         (set ref-dep (list nil (symbol-value id-dep)))))
3885     header))
3886
3887 (defun gnus-extract-message-id-from-in-reply-to (string)
3888   (if (string-match "<[^>]+>" string)
3889       (substring string (match-beginning 0) (match-end 0))
3890     nil))
3891
3892 (defun gnus-build-sparse-threads ()
3893   (let ((headers gnus-newsgroup-headers)
3894         (mail-parse-charset gnus-newsgroup-charset)
3895         (gnus-summary-ignore-duplicates t)
3896         header references generation relations
3897         subject child end new-child date)
3898     ;; First we create an alist of generations/relations, where
3899     ;; generations is how much we trust the relation, and the relation
3900     ;; is parent/child.
3901     (gnus-message 7 "Making sparse threads...")
3902     (save-excursion
3903       (nnheader-set-temp-buffer " *gnus sparse threads*")
3904       (while (setq header (pop headers))
3905         (when (and (setq references (mail-header-references header))
3906                    (not (string= references "")))
3907           (insert references)
3908           (setq child (mail-header-id header)
3909                 subject (mail-header-subject header)
3910                 date (mail-header-date header)
3911                 generation 0)
3912           (while (search-backward ">" nil t)
3913             (setq end (1+ (point)))
3914             (when (search-backward "<" nil t)
3915               (setq new-child (buffer-substring (point) end))
3916               (push (list (incf generation)
3917                           child (setq child new-child)
3918                           subject date)
3919                     relations)))
3920           (when child
3921             (push (list (1+ generation) child nil subject) relations))
3922           (erase-buffer)))
3923       (kill-buffer (current-buffer)))
3924     ;; Sort over trustworthiness.
3925     (mapcar
3926      (lambda (relation)
3927        (when (gnus-dependencies-add-header
3928               (make-full-mail-header-from-decoded-header
3929                gnus-reffed-article-number
3930                (nth 3 relation) "" (or (nth 4 relation) "")
3931                (nth 1 relation)
3932                (or (nth 2 relation) "") 0 0 "")
3933               gnus-newsgroup-dependencies nil)
3934          (push gnus-reffed-article-number gnus-newsgroup-limit)
3935          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3936          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3937                gnus-newsgroup-reads)
3938          (decf gnus-reffed-article-number)))
3939      (sort relations 'car-less-than-car))
3940     (gnus-message 7 "Making sparse threads...done")))
3941
3942 (defun gnus-build-old-threads ()
3943   ;; Look at all the articles that refer back to old articles, and
3944   ;; fetch the headers for the articles that aren't there.  This will
3945   ;; build complete threads - if the roots haven't been expired by the
3946   ;; server, that is.
3947   (let ((mail-parse-charset gnus-newsgroup-charset)
3948         id heads)
3949     (mapatoms
3950      (lambda (refs)
3951        (when (not (car (symbol-value refs)))
3952          (setq heads (cdr (symbol-value refs)))
3953          (while heads
3954            (if (memq (mail-header-number (caar heads))
3955                      gnus-newsgroup-dormant)
3956                (setq heads (cdr heads))
3957              (setq id (symbol-name refs))
3958              (while (and (setq id (gnus-build-get-header id))
3959                          (not (car (gnus-id-to-thread id)))))
3960              (setq heads nil)))))
3961      gnus-newsgroup-dependencies)))
3962
3963 ;; This function has to be called with point after the article number
3964 ;; on the beginning of the line.
3965 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3966   (let ((eol (gnus-point-at-eol))
3967         (buffer (current-buffer))
3968         header references in-reply-to)
3969
3970     ;; overview: [num subject from date id refs chars lines misc]
3971     (unwind-protect
3972         (progn
3973           (narrow-to-region (point) eol)
3974           (unless (eobp)
3975             (forward-char))
3976
3977           (setq header
3978                 (make-full-mail-header
3979                  number                         ; number
3980                  (nnheader-nov-field)           ; subject
3981                  (nnheader-nov-field)           ; from
3982                  (nnheader-nov-field)           ; date
3983                  (nnheader-nov-read-message-id) ; id
3984                  (nnheader-nov-field)           ; refs
3985                  (nnheader-nov-read-integer)    ; chars
3986                  (nnheader-nov-read-integer)    ; lines
3987                  (unless (eobp)
3988                    (if (looking-at "Xref: ")
3989                        (goto-char (match-end 0)))
3990                    (nnheader-nov-field))        ; Xref
3991                  (nnheader-nov-parse-extra))))  ; extra
3992
3993       (widen))
3994
3995     (when (and (string= references "")
3996                (setq in-reply-to (mail-header-extra header))
3997                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3998       (mail-header-set-references
3999        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4000
4001     (when gnus-alter-header-function
4002       (funcall gnus-alter-header-function header))
4003     (gnus-dependencies-add-header header dependencies force-new)))
4004
4005 (defun gnus-build-get-header (id)
4006   "Look through the buffer of NOV lines and find the header to ID.
4007 Enter this line into the dependencies hash table, and return
4008 the id of the parent article (if any)."
4009   (let ((deps gnus-newsgroup-dependencies)
4010         found header)
4011     (prog1
4012         (save-excursion
4013           (set-buffer nntp-server-buffer)
4014           (let ((case-fold-search nil))
4015             (goto-char (point-min))
4016             (while (and (not found)
4017                         (search-forward id nil t))
4018               (beginning-of-line)
4019               (setq found (looking-at
4020                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4021                                    (regexp-quote id))))
4022               (or found (beginning-of-line 2)))
4023             (when found
4024               (beginning-of-line)
4025               (and
4026                (setq header (gnus-nov-parse-line
4027                              (read (current-buffer)) deps))
4028                (gnus-parent-id (mail-header-references header))))))
4029       (when header
4030         (let ((number (mail-header-number header)))
4031           (push number gnus-newsgroup-limit)
4032           (push header gnus-newsgroup-headers)
4033           (if (memq number gnus-newsgroup-unselected)
4034               (progn
4035                 (setq gnus-newsgroup-unreads
4036                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4037                                                number))
4038                 (setq gnus-newsgroup-unselected
4039                       (delq number gnus-newsgroup-unselected)))
4040             (push number gnus-newsgroup-ancient)))))))
4041
4042 (defun gnus-build-all-threads ()
4043   "Read all the headers."
4044   (let ((gnus-summary-ignore-duplicates t)
4045         (mail-parse-charset gnus-newsgroup-charset)
4046         (dependencies gnus-newsgroup-dependencies)
4047         header article)
4048     (save-excursion
4049       (set-buffer nntp-server-buffer)
4050       (let ((case-fold-search nil))
4051         (goto-char (point-min))
4052         (while (not (eobp))
4053           (ignore-errors
4054             (setq article (read (current-buffer))
4055                   header (gnus-nov-parse-line article dependencies)))
4056           (when header
4057             (save-excursion
4058               (set-buffer gnus-summary-buffer)
4059               (push header gnus-newsgroup-headers)
4060               (if (memq (setq article (mail-header-number header))
4061                         gnus-newsgroup-unselected)
4062                   (progn
4063                     (setq gnus-newsgroup-unreads
4064                           (gnus-add-to-sorted-list
4065                            gnus-newsgroup-unreads article))
4066                     (setq gnus-newsgroup-unselected
4067                           (delq article gnus-newsgroup-unselected)))
4068                 (push article gnus-newsgroup-ancient)))
4069             (forward-line 1)))))))
4070
4071 (defun gnus-summary-update-article-line (article header)
4072   "Update the line for ARTICLE using HEADERS."
4073   (let* ((id (mail-header-id header))
4074          (thread (gnus-id-to-thread id)))
4075     (unless thread
4076       (error "Article in no thread"))
4077     ;; Update the thread.
4078     (setcar thread header)
4079     (gnus-summary-goto-subject article)
4080     (let* ((datal (gnus-data-find-list article))
4081            (data (car datal))
4082            (buffer-read-only nil)
4083            (level (gnus-summary-thread-level)))
4084       (gnus-delete-line)
4085       (let ((inserted (- (point)
4086                          (progn
4087                            (gnus-summary-insert-line
4088                             header level nil 
4089                             (memq article gnus-newsgroup-undownloaded)
4090                             (gnus-article-mark article)
4091                             (memq article gnus-newsgroup-replied)
4092                             (memq article gnus-newsgroup-expirable)
4093                             ;; Only insert the Subject string when it's different
4094                             ;; from the previous Subject string.
4095                             (if (and
4096                                  gnus-show-threads
4097                                  (gnus-subject-equal
4098                                   (condition-case ()
4099                                       (mail-header-subject
4100                                        (gnus-data-header
4101                                         (cadr
4102                                          (gnus-data-find-list
4103                                           article
4104                                           (gnus-data-list t)))))
4105                                     ;; Error on the side of excessive subjects.
4106                                     (error ""))
4107                                   (mail-header-subject header)))
4108                                 ""
4109                               (mail-header-subject header))
4110                             nil (cdr (assq article gnus-newsgroup-scored))
4111                             (memq article gnus-newsgroup-processable))
4112                            (point)))))
4113         (when (cdr datal)
4114           (gnus-data-update-list
4115            (cdr datal) 
4116            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4117
4118 (defun gnus-summary-update-article (article &optional iheader)
4119   "Update ARTICLE in the summary buffer."
4120   (set-buffer gnus-summary-buffer)
4121   (let* ((header (gnus-summary-article-header article))
4122          (id (mail-header-id header))
4123          (data (gnus-data-find article))
4124          (thread (gnus-id-to-thread id))
4125          (references (mail-header-references header))
4126          (parent
4127           (gnus-id-to-thread
4128            (or (gnus-parent-id
4129                 (when (and references
4130                            (not (equal "" references)))
4131                   references))
4132                "none")))
4133          (buffer-read-only nil)
4134          (old (car thread)))
4135     (when thread
4136       (unless iheader
4137         (setcar thread nil)
4138         (when parent
4139           (delq thread parent)))
4140       (if (gnus-summary-insert-subject id header)
4141           ;; Set the (possibly) new article number in the data structure.
4142           (gnus-data-set-number data (gnus-id-to-article id))
4143         (setcar thread old)
4144         nil))))
4145
4146 (defun gnus-rebuild-thread (id &optional line)
4147   "Rebuild the thread containing ID.
4148 If LINE, insert the rebuilt thread starting on line LINE."
4149   (let ((buffer-read-only nil)
4150         old-pos current thread data)
4151     (if (not gnus-show-threads)
4152         (setq thread (list (car (gnus-id-to-thread id))))
4153       ;; Get the thread this article is part of.
4154       (setq thread (gnus-remove-thread id)))
4155     (setq old-pos (gnus-point-at-bol))
4156     (setq current (save-excursion
4157                     (and (re-search-backward "[\r\n]" nil t)
4158                          (gnus-summary-article-number))))
4159     ;; If this is a gathered thread, we have to go some re-gathering.
4160     (when (stringp (car thread))
4161       (let ((subject (car thread))
4162             roots thr)
4163         (setq thread (cdr thread))
4164         (while thread
4165           (unless (memq (setq thr (gnus-id-to-thread
4166                                    (gnus-root-id
4167                                     (mail-header-id (caar thread)))))
4168                         roots)
4169             (push thr roots))
4170           (setq thread (cdr thread)))
4171         ;; We now have all (unique) roots.
4172         (if (= (length roots) 1)
4173             ;; All the loose roots are now one solid root.
4174             (setq thread (car roots))
4175           (setq thread (cons subject (gnus-sort-threads roots))))))
4176     (let (threads)
4177       ;; We then insert this thread into the summary buffer.
4178       (when line
4179         (goto-char (point-min))
4180         (forward-line (1- line)))
4181       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4182         (if gnus-show-threads
4183             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4184           (gnus-summary-prepare-unthreaded thread))
4185         (setq data (nreverse gnus-newsgroup-data))
4186         (setq threads gnus-newsgroup-threads))
4187       ;; We splice the new data into the data structure.
4188       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4189       ;;!!! then we want to insert at the beginning of the buffer.
4190       ;;!!! That happens to be true with Gnus now, but that may
4191       ;;!!! change in the future.  Perhaps.
4192       (gnus-data-enter-list
4193        (if line nil current) data (- (point) old-pos))
4194       (setq gnus-newsgroup-threads
4195             (nconc threads gnus-newsgroup-threads))
4196       (gnus-data-compute-positions))))
4197
4198 (defun gnus-number-to-header (number)
4199   "Return the header for article NUMBER."
4200   (let ((headers gnus-newsgroup-headers))
4201     (while (and headers
4202                 (not (= number (mail-header-number (car headers)))))
4203       (pop headers))
4204     (when headers
4205       (car headers))))
4206
4207 (defun gnus-parent-headers (in-headers &optional generation)
4208   "Return the headers of the GENERATIONeth parent of HEADERS."
4209   (unless generation
4210     (setq generation 1))
4211   (let ((parent t)
4212         (headers in-headers)
4213         references)
4214     (while (and parent
4215                 (not (zerop generation))
4216                 (setq references (mail-header-references headers)))
4217       (setq headers (if (and references
4218                              (setq parent (gnus-parent-id references)))
4219                         (car (gnus-id-to-thread parent))
4220                       nil))
4221       (decf generation))
4222     (and (not (eq headers in-headers))
4223          headers)))
4224
4225 (defun gnus-id-to-thread (id)
4226   "Return the (sub-)thread where ID appears."
4227   (gnus-gethash id gnus-newsgroup-dependencies))
4228
4229 (defun gnus-id-to-article (id)
4230   "Return the article number of ID."
4231   (let ((thread (gnus-id-to-thread id)))
4232     (when (and thread
4233                (car thread))
4234       (mail-header-number (car thread)))))
4235
4236 (defun gnus-id-to-header (id)
4237   "Return the article headers of ID."
4238   (car (gnus-id-to-thread id)))
4239
4240 (defun gnus-article-displayed-root-p (article)
4241   "Say whether ARTICLE is a root(ish) article."
4242   (let ((level (gnus-summary-thread-level article))
4243         (refs (mail-header-references  (gnus-summary-article-header article)))
4244         particle)
4245     (cond
4246      ((null level) nil)
4247      ((zerop level) t)
4248      ((null refs) t)
4249      ((null (gnus-parent-id refs)) t)
4250      ((and (= 1 level)
4251            (null (setq particle (gnus-id-to-article
4252                                  (gnus-parent-id refs))))
4253            (null (gnus-summary-thread-level particle)))))))
4254
4255 (defun gnus-root-id (id)
4256   "Return the id of the root of the thread where ID appears."
4257   (let (last-id prev)
4258     (while (and id (setq prev (car (gnus-id-to-thread id))))
4259       (setq last-id id
4260             id (gnus-parent-id (mail-header-references prev))))
4261     last-id))
4262
4263 (defun gnus-articles-in-thread (thread)
4264   "Return the list of articles in THREAD."
4265   (cons (mail-header-number (car thread))
4266         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4267
4268 (defun gnus-remove-thread (id &optional dont-remove)
4269   "Remove the thread that has ID in it."
4270   (let (headers thread last-id)
4271     ;; First go up in this thread until we find the root.
4272     (setq last-id (gnus-root-id id)
4273           headers (message-flatten-list (gnus-id-to-thread last-id)))
4274     ;; We have now found the real root of this thread.  It might have
4275     ;; been gathered into some loose thread, so we have to search
4276     ;; through the threads to find the thread we wanted.
4277     (let ((threads gnus-newsgroup-threads)
4278           sub)
4279       (while threads
4280         (setq sub (car threads))
4281         (if (stringp (car sub))
4282             ;; This is a gathered thread, so we look at the roots
4283             ;; below it to find whether this article is in this
4284             ;; gathered root.
4285             (progn
4286               (setq sub (cdr sub))
4287               (while sub
4288                 (when (member (caar sub) headers)
4289                   (setq thread (car threads)
4290                         threads nil
4291                         sub nil))
4292                 (setq sub (cdr sub))))
4293           ;; It's an ordinary thread, so we check it.
4294           (when (eq (car sub) (car headers))
4295             (setq thread sub
4296                   threads nil)))
4297         (setq threads (cdr threads)))
4298       ;; If this article is in no thread, then it's a root.
4299       (if thread
4300           (unless dont-remove
4301             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4302         (setq thread (gnus-id-to-thread last-id)))
4303       (when thread
4304         (prog1
4305             thread                      ; We return this thread.
4306           (unless dont-remove
4307             (if (stringp (car thread))
4308                 (progn
4309                   ;; If we use dummy roots, then we have to remove the
4310                   ;; dummy root as well.
4311                   (when (eq gnus-summary-make-false-root 'dummy)
4312                     ;; We go to the dummy root by going to
4313                     ;; the first sub-"thread", and then one line up.
4314                     (gnus-summary-goto-article
4315                      (mail-header-number (caadr thread)))
4316                     (forward-line -1)
4317                     (gnus-delete-line)
4318                     (gnus-data-compute-positions))
4319                   (setq thread (cdr thread))
4320                   (while thread
4321                     (gnus-remove-thread-1 (car thread))
4322                     (setq thread (cdr thread))))
4323               (gnus-remove-thread-1 thread))))))))
4324
4325 (defun gnus-remove-thread-1 (thread)
4326   "Remove the thread THREAD recursively."
4327   (let ((number (mail-header-number (pop thread)))
4328         d)
4329     (setq thread (reverse thread))
4330     (while thread
4331       (gnus-remove-thread-1 (pop thread)))
4332     (when (setq d (gnus-data-find number))
4333       (goto-char (gnus-data-pos d))
4334       (gnus-summary-show-thread)
4335       (gnus-data-remove
4336        number
4337        (- (gnus-point-at-bol)
4338           (prog1
4339               (1+ (gnus-point-at-eol))
4340             (gnus-delete-line)))))))
4341
4342 (defun gnus-sort-threads-1 (threads func)
4343   (sort (mapcar (lambda (thread)
4344                   (cons (car thread)
4345                         (and (cdr thread)
4346                              (gnus-sort-threads-1 (cdr thread) func))))
4347                 threads) func))
4348
4349 (defun gnus-sort-threads (threads)
4350   "Sort THREADS."
4351   (if (not gnus-thread-sort-functions)
4352       threads
4353     (gnus-message 8 "Sorting threads...")
4354     (let ((max-lisp-eval-depth 5000))
4355       (prog1 (gnus-sort-threads-1
4356          threads
4357          (gnus-make-sort-function gnus-thread-sort-functions))
4358         (gnus-message 8 "Sorting threads...done")))))
4359
4360 (defun gnus-sort-articles (articles)
4361   "Sort ARTICLES."
4362   (when gnus-article-sort-functions
4363     (gnus-message 7 "Sorting articles...")
4364     (prog1
4365         (setq gnus-newsgroup-headers
4366               (sort articles (gnus-make-sort-function
4367                               gnus-article-sort-functions)))
4368       (gnus-message 7 "Sorting articles...done"))))
4369
4370 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4371 (defmacro gnus-thread-header (thread)
4372   "Return header of first article in THREAD.
4373 Note that THREAD must never, ever be anything else than a variable -
4374 using some other form will lead to serious barfage."
4375   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4376   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4377   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4378         (vector thread) 2))
4379
4380 (defsubst gnus-article-sort-by-number (h1 h2)
4381   "Sort articles by article number."
4382   (< (mail-header-number h1)
4383      (mail-header-number h2)))
4384
4385 (defun gnus-thread-sort-by-number (h1 h2)
4386   "Sort threads by root article number."
4387   (gnus-article-sort-by-number
4388    (gnus-thread-header h1) (gnus-thread-header h2)))
4389
4390 (defsubst gnus-article-sort-by-random (h1 h2)
4391   "Sort articles by article number."
4392   (zerop (random 2)))
4393
4394 (defun gnus-thread-sort-by-random (h1 h2)
4395   "Sort threads by root article number."
4396   (gnus-article-sort-by-random
4397    (gnus-thread-header h1) (gnus-thread-header h2)))
4398
4399 (defsubst gnus-article-sort-by-lines (h1 h2)
4400   "Sort articles by article Lines header."
4401   (< (mail-header-lines h1)
4402      (mail-header-lines h2)))
4403
4404 (defun gnus-thread-sort-by-lines (h1 h2)
4405   "Sort threads by root article Lines header."
4406   (gnus-article-sort-by-lines
4407    (gnus-thread-header h1) (gnus-thread-header h2)))
4408
4409 (defsubst gnus-article-sort-by-chars (h1 h2)
4410   "Sort articles by octet length."
4411   (< (mail-header-chars h1)
4412      (mail-header-chars h2)))
4413
4414 (defun gnus-thread-sort-by-chars (h1 h2)
4415   "Sort threads by root article octet length."
4416   (gnus-article-sort-by-chars
4417    (gnus-thread-header h1) (gnus-thread-header h2)))
4418
4419 (defsubst gnus-article-sort-by-author (h1 h2)
4420   "Sort articles by root author."
4421   (string-lessp
4422    (let ((addr (car (mime-entity-read-field h1 'From))))
4423      (or (std11-full-name-string addr)
4424          (std11-address-string addr)
4425          ""))
4426    (let ((addr (car (mime-entity-read-field h2 'From))))
4427      (or (std11-full-name-string addr)
4428          (std11-address-string addr)
4429          ""))
4430    ))
4431
4432 (defun gnus-thread-sort-by-author (h1 h2)
4433   "Sort threads by root author."
4434   (gnus-article-sort-by-author
4435    (gnus-thread-header h1)  (gnus-thread-header h2)))
4436
4437 (defsubst gnus-article-sort-by-subject (h1 h2)
4438   "Sort articles by root subject."
4439   (string-lessp
4440    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4441    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4442
4443 (defun gnus-thread-sort-by-subject (h1 h2)
4444   "Sort threads by root subject."
4445   (gnus-article-sort-by-subject
4446    (gnus-thread-header h1) (gnus-thread-header h2)))
4447
4448 (defsubst gnus-article-sort-by-date (h1 h2)
4449   "Sort articles by root article date."
4450   (time-less-p
4451    (gnus-date-get-time (mail-header-date h1))
4452    (gnus-date-get-time (mail-header-date h2))))
4453
4454 (defun gnus-thread-sort-by-date (h1 h2)
4455   "Sort threads by root article date."
4456   (gnus-article-sort-by-date
4457    (gnus-thread-header h1) (gnus-thread-header h2)))
4458
4459 (defsubst gnus-article-sort-by-score (h1 h2)
4460   "Sort articles by root article score.
4461 Unscored articles will be counted as having a score of zero."
4462   (> (or (cdr (assq (mail-header-number h1)
4463                     gnus-newsgroup-scored))
4464          gnus-summary-default-score 0)
4465      (or (cdr (assq (mail-header-number h2)
4466                     gnus-newsgroup-scored))
4467          gnus-summary-default-score 0)))
4468
4469 (defun gnus-thread-sort-by-score (h1 h2)
4470   "Sort threads by root article score."
4471   (gnus-article-sort-by-score
4472    (gnus-thread-header h1) (gnus-thread-header h2)))
4473
4474 (defun gnus-thread-sort-by-total-score (h1 h2)
4475   "Sort threads by the sum of all scores in the thread.
4476 Unscored articles will be counted as having a score of zero."
4477   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4478
4479 (defun gnus-thread-total-score (thread)
4480   ;; This function find the total score of THREAD.
4481   (cond
4482    ((null thread)
4483     0)
4484    ((consp thread)
4485     (if (stringp (car thread))
4486         (apply gnus-thread-score-function 0
4487                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4488       (gnus-thread-total-score-1 thread)))
4489    (t
4490     (gnus-thread-total-score-1 (list thread)))))
4491
4492 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4493   "Sort threads such that the thread with the most recently arrived article comes first."
4494   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4495
4496 (defun gnus-thread-highest-number (thread)
4497   "Return the highest article number in THREAD."
4498   (apply 'max (mapcar (lambda (header)
4499                         (mail-header-number header))
4500                       (message-flatten-list thread))))
4501
4502 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4503   "Sort threads such that the thread with the most recently dated article comes first."
4504   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4505
4506 (defun gnus-thread-latest-date (thread)
4507   "Return the highest article date in THREAD."
4508   (let ((previous-time 0))
4509     (apply 'max
4510            (mapcar
4511             (lambda (header)
4512               (setq previous-time
4513                     (time-to-seconds
4514                      (condition-case ()
4515                          (mail-header-parse-date (mail-header-date header))
4516                        (error previous-time)))))
4517             (sort
4518              (message-flatten-list thread)
4519              (lambda (h1 h2)
4520                (< (mail-header-number h1)
4521                   (mail-header-number h2))))))))
4522
4523 (defun gnus-thread-total-score-1 (root)
4524   ;; This function find the total score of the thread below ROOT.
4525   (setq root (car root))
4526   (apply gnus-thread-score-function
4527          (or (append
4528               (mapcar 'gnus-thread-total-score
4529                       (cdr (gnus-id-to-thread (mail-header-id root))))
4530               (when (> (mail-header-number root) 0)
4531                 (list (or (cdr (assq (mail-header-number root)
4532                                      gnus-newsgroup-scored))
4533                           gnus-summary-default-score 0))))
4534              (list gnus-summary-default-score)
4535              '(0))))
4536
4537 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4538 (defvar gnus-tmp-prev-subject nil)
4539 (defvar gnus-tmp-false-parent nil)
4540 (defvar gnus-tmp-root-expunged nil)
4541 (defvar gnus-tmp-dummy-line nil)
4542
4543 (eval-when-compile (defvar gnus-tmp-header))
4544 (defun gnus-extra-header (type &optional header)
4545   "Return the extra header of TYPE."
4546   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4547       ""))
4548
4549 (defvar gnus-tmp-thread-tree-header-string "")
4550
4551 (defcustom gnus-sum-thread-tree-root "> "
4552   "With %B spec, used for the root of a thread.
4553 If nil, use subject instead."
4554   :type 'string
4555   :group 'gnus-thread)
4556 (defcustom gnus-sum-thread-tree-single-indent ""
4557   "With %B spec, used for a thread with just one message.
4558 If nil, use subject instead."
4559   :type 'string
4560   :group 'gnus-thread)
4561 (defcustom gnus-sum-thread-tree-vertical "| "
4562   "With %B spec, used for drawing a vertical line."
4563   :type 'string
4564   :group 'gnus-thread)
4565 (defcustom gnus-sum-thread-tree-indent "  "
4566   "With %B spec, used for indenting."
4567   :type 'string
4568   :group 'gnus-thread)
4569 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4570   "With %B spec, used for a leaf with brothers."
4571   :type 'string
4572   :group 'gnus-thread)
4573 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4574   "With %B spec, used for a leaf without brothers."
4575   :type 'string
4576   :group 'gnus-thread)
4577
4578 (defun gnus-summary-prepare-threads (threads)
4579   "Prepare summary buffer from THREADS and indentation LEVEL.
4580 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4581 or a straight list of headers."
4582   (gnus-message 7 "Generating summary...")
4583
4584   (setq gnus-newsgroup-threads threads)
4585   (beginning-of-line)
4586
4587   (let ((gnus-tmp-level 0)
4588         (default-score (or gnus-summary-default-score 0))
4589         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4590         (building-line-count gnus-summary-display-while-building)
4591         (building-count (integerp gnus-summary-display-while-building))
4592         thread number subject stack state gnus-tmp-gathered beg-match
4593         new-roots gnus-tmp-new-adopts thread-end simp-subject
4594         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4595         gnus-tmp-replied gnus-tmp-subject-or-nil
4596         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4597         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4598         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4599         tree-stack)
4600
4601     (setq gnus-tmp-prev-subject nil
4602           gnus-tmp-thread-tree-header-string "")
4603
4604     (if (vectorp (car threads))
4605         ;; If this is a straight (sic) list of headers, then a
4606         ;; threaded summary display isn't required, so we just create
4607         ;; an unthreaded one.
4608         (gnus-summary-prepare-unthreaded threads)
4609
4610       ;; Do the threaded display.
4611
4612       (if gnus-summary-display-while-building
4613           (switch-to-buffer (buffer-name)))
4614       (while (or threads stack gnus-tmp-new-adopts new-roots)
4615
4616         (if (and (= gnus-tmp-level 0)
4617                  (or (not stack)
4618                      (= (caar stack) 0))
4619                  (not gnus-tmp-false-parent)
4620                  (or gnus-tmp-new-adopts new-roots))
4621             (if gnus-tmp-new-adopts
4622                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4623                       thread (list (car gnus-tmp-new-adopts))
4624                       gnus-tmp-header (caar thread)
4625                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4626               (when new-roots
4627                 (setq thread (list (car new-roots))
4628                       gnus-tmp-header (caar thread)
4629                       new-roots (cdr new-roots))))
4630
4631           (if threads
4632               ;; If there are some threads, we do them before the
4633               ;; threads on the stack.
4634               (setq thread threads
4635                     gnus-tmp-header (caar thread))
4636             ;; There were no current threads, so we pop something off
4637             ;; the stack.
4638             (setq state (car stack)
4639                   gnus-tmp-level (car state)
4640                   tree-stack (cadr state)
4641                   thread (caddr state)
4642                   stack (cdr stack)
4643                   gnus-tmp-header (caar thread))))
4644
4645         (setq gnus-tmp-false-parent nil)
4646         (setq gnus-tmp-root-expunged nil)
4647         (setq thread-end nil)
4648
4649         (if (stringp gnus-tmp-header)
4650             ;; The header is a dummy root.
4651             (cond
4652              ((eq gnus-summary-make-false-root 'adopt)
4653               ;; We let the first article adopt the rest.
4654               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4655                                                (cddar thread)))
4656               (setq gnus-tmp-gathered
4657                     (nconc (mapcar
4658                             (lambda (h) (mail-header-number (car h)))
4659                             (cddar thread))
4660                            gnus-tmp-gathered))
4661               (setq thread (cons (list (caar thread)
4662                                        (cadar thread))
4663                                  (cdr thread)))
4664               (setq gnus-tmp-level -1
4665                     gnus-tmp-false-parent t))
4666              ((eq gnus-summary-make-false-root 'empty)
4667               ;; We print adopted articles with empty subject fields.
4668               (setq gnus-tmp-gathered
4669                     (nconc (mapcar
4670                             (lambda (h) (mail-header-number (car h)))
4671                             (cddar thread))
4672                            gnus-tmp-gathered))
4673               (setq gnus-tmp-level -1))
4674              ((eq gnus-summary-make-false-root 'dummy)
4675               ;; We remember that we probably want to output a dummy
4676               ;; root.
4677               (setq gnus-tmp-dummy-line gnus-tmp-header)
4678               (setq gnus-tmp-prev-subject gnus-tmp-header))
4679              (t
4680               ;; We do not make a root for the gathered
4681               ;; sub-threads at all.
4682               (setq gnus-tmp-level -1)))
4683
4684           (setq number (mail-header-number gnus-tmp-header)
4685                 subject (mail-header-subject gnus-tmp-header)
4686                 simp-subject (gnus-simplify-subject-fully subject))
4687
4688           (cond
4689            ;; If the thread has changed subject, we might want to make
4690            ;; this subthread into a root.
4691            ((and (null gnus-thread-ignore-subject)
4692                  (not (zerop gnus-tmp-level))
4693                  gnus-tmp-prev-subject
4694                  (not (string= gnus-tmp-prev-subject simp-subject)))
4695             (setq new-roots (nconc new-roots (list (car thread)))
4696                   thread-end t
4697                   gnus-tmp-header nil))
4698            ;; If the article lies outside the current limit,
4699            ;; then we do not display it.
4700            ((not (memq number gnus-newsgroup-limit))
4701             (setq gnus-tmp-gathered
4702                   (nconc (mapcar
4703                           (lambda (h) (mail-header-number (car h)))
4704                           (cdar thread))
4705                          gnus-tmp-gathered))
4706             (setq gnus-tmp-new-adopts (if (cdar thread)
4707                                           (append gnus-tmp-new-adopts
4708                                                   (cdar thread))
4709                                         gnus-tmp-new-adopts)
4710                   thread-end t
4711                   gnus-tmp-header nil)
4712             (when (zerop gnus-tmp-level)
4713               (setq gnus-tmp-root-expunged t)))
4714            ;; Perhaps this article is to be marked as read?
4715            ((and gnus-summary-mark-below
4716                  (< (or (cdr (assq number gnus-newsgroup-scored))
4717                         default-score)
4718                     gnus-summary-mark-below)
4719                  ;; Don't touch sparse articles.
4720                  (not (gnus-summary-article-sparse-p number))
4721                  (not (gnus-summary-article-ancient-p number)))
4722             (setq gnus-newsgroup-unreads
4723                   (delq number gnus-newsgroup-unreads))
4724             (if gnus-newsgroup-auto-expire
4725                 (setq gnus-newsgroup-expirable
4726                       (gnus-add-to-sorted-list
4727                        gnus-newsgroup-expirable number))
4728               (push (cons number gnus-low-score-mark)
4729                     gnus-newsgroup-reads))))
4730
4731           (when gnus-tmp-header
4732             ;; We may have an old dummy line to output before this
4733             ;; article.
4734             (when (and gnus-tmp-dummy-line
4735                        (gnus-subject-equal
4736                         gnus-tmp-dummy-line
4737                         (mail-header-subject gnus-tmp-header)))
4738               (gnus-summary-insert-dummy-line
4739                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4740               (setq gnus-tmp-dummy-line nil))
4741
4742             ;; Compute the mark.
4743             (setq gnus-tmp-unread (gnus-article-mark number))
4744
4745             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4746                                   gnus-tmp-header gnus-tmp-level)
4747                   gnus-newsgroup-data)
4748
4749             ;; Actually insert the line.
4750             (setq
4751              gnus-tmp-subject-or-nil
4752              (cond
4753               ((and gnus-thread-ignore-subject
4754                     gnus-tmp-prev-subject
4755                     (not (string= gnus-tmp-prev-subject simp-subject)))
4756                subject)
4757               ((zerop gnus-tmp-level)
4758                (if (and (eq gnus-summary-make-false-root 'empty)
4759                         (memq number gnus-tmp-gathered)
4760                         gnus-tmp-prev-subject
4761                         (string= gnus-tmp-prev-subject simp-subject))
4762                    gnus-summary-same-subject
4763                  subject))
4764               (t gnus-summary-same-subject)))
4765             (if (and (eq gnus-summary-make-false-root 'adopt)
4766                      (= gnus-tmp-level 1)
4767                      (memq number gnus-tmp-gathered))
4768                 (setq gnus-tmp-opening-bracket ?\<
4769                       gnus-tmp-closing-bracket ?\>)
4770               (setq gnus-tmp-opening-bracket ?\[
4771                     gnus-tmp-closing-bracket ?\]))
4772             (setq
4773              gnus-tmp-indentation
4774              (aref gnus-thread-indent-array gnus-tmp-level)
4775              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4776              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4777                                 gnus-summary-default-score 0)
4778              gnus-tmp-score-char
4779              (if (or (null gnus-summary-default-score)
4780                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4781                          gnus-summary-zcore-fuzz))
4782                  ?\ ;;;Whitespace
4783                (if (< gnus-tmp-score gnus-summary-default-score)
4784                    gnus-score-below-mark gnus-score-over-mark))
4785              gnus-tmp-replied
4786              (cond ((memq number gnus-newsgroup-processable)
4787                     gnus-process-mark)
4788                    ((memq number gnus-newsgroup-cached)
4789                     gnus-cached-mark)
4790                    ((memq number gnus-newsgroup-replied)
4791                     gnus-replied-mark)
4792                    ((memq number gnus-newsgroup-forwarded)
4793                     gnus-forwarded-mark)
4794                    ((memq number gnus-newsgroup-saved)
4795                     gnus-saved-mark)
4796                    ((memq number gnus-newsgroup-recent)
4797                     gnus-recent-mark)
4798                    ((memq number gnus-newsgroup-unseen)
4799                     gnus-unseen-mark)
4800                    (t gnus-no-mark))
4801              gnus-tmp-downloaded
4802              (cond ((memq number gnus-newsgroup-undownloaded) 
4803                     gnus-undownloaded-mark)
4804                    (gnus-newsgroup-agentized
4805                     gnus-downloaded-mark)
4806                    (t
4807                     gnus-no-mark))
4808              gnus-tmp-from (mail-header-from gnus-tmp-header)
4809              gnus-tmp-name
4810              (cond
4811               ((string-match "<[^>]+> *$" gnus-tmp-from)
4812                (setq beg-match (match-beginning 0))
4813                (or (and (string-match "^\".+\"" gnus-tmp-from)
4814                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4815                    (substring gnus-tmp-from 0 beg-match)))
4816               ((string-match "(.+)" gnus-tmp-from)
4817                (substring gnus-tmp-from
4818                           (1+ (match-beginning 0)) (1- (match-end 0))))
4819               (t gnus-tmp-from))
4820              gnus-tmp-thread-tree-header-string
4821              (cond
4822               ((not gnus-show-threads) "")
4823               ((zerop gnus-tmp-level)
4824                (if (cdar thread)
4825                    (or gnus-sum-thread-tree-root subject)
4826                  (or gnus-sum-thread-tree-single-indent subject)))
4827               (t
4828                (concat (apply 'concat
4829                               (mapcar (lambda (item)
4830                                         (if (= item 1)
4831                                             gnus-sum-thread-tree-vertical
4832                                           gnus-sum-thread-tree-indent))
4833                                       (cdr (reverse tree-stack))))
4834                        (if (nth 1 thread)
4835                            gnus-sum-thread-tree-leaf-with-other
4836                          gnus-sum-thread-tree-single-leaf)))))
4837             (when (string= gnus-tmp-name "")
4838               (setq gnus-tmp-name gnus-tmp-from))
4839             (unless (numberp gnus-tmp-lines)
4840               (setq gnus-tmp-lines -1))
4841             (if (= gnus-tmp-lines -1)
4842                 (setq gnus-tmp-lines "?")
4843               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4844               (gnus-put-text-property
4845              (point)
4846              (progn (eval gnus-summary-line-format-spec) (point))
4847                'gnus-number number)
4848             (when gnus-visual-p
4849               (forward-line -1)
4850               (gnus-run-hooks 'gnus-summary-update-hook)
4851               (forward-line 1))
4852
4853             (setq gnus-tmp-prev-subject simp-subject)))
4854
4855         (when (nth 1 thread)
4856           (push (list (max 0 gnus-tmp-level)
4857                       (copy-list tree-stack)
4858                       (nthcdr 1 thread))
4859                 stack))
4860         (push (if (nth 1 thread) 1 0) tree-stack)
4861         (incf gnus-tmp-level)
4862         (setq threads (if thread-end nil (cdar thread)))
4863         (if gnus-summary-display-while-building
4864             (if building-count
4865                 (progn
4866                   ;; use a set frequency
4867                   (setq building-line-count (1- building-line-count))
4868                   (when (= building-line-count 0)
4869                     (sit-for 0)
4870                     (setq building-line-count
4871                           gnus-summary-display-while-building)))
4872               ;; always
4873               (sit-for 0)))
4874         (unless threads
4875           (setq gnus-tmp-level 0)))))
4876   (gnus-message 7 "Generating summary...done"))
4877
4878 (defun gnus-summary-prepare-unthreaded (headers)
4879   "Generate an unthreaded summary buffer based on HEADERS."
4880   (let (header number mark)
4881
4882     (beginning-of-line)
4883
4884     (while headers
4885       ;; We may have to root out some bad articles...
4886       (when (memq (setq number (mail-header-number
4887                                 (setq header (pop headers))))
4888                   gnus-newsgroup-limit)
4889         ;; Mark article as read when it has a low score.
4890         (when (and gnus-summary-mark-below
4891                    (< (or (cdr (assq number gnus-newsgroup-scored))
4892                           gnus-summary-default-score 0)
4893                       gnus-summary-mark-below)
4894                    (not (gnus-summary-article-ancient-p number)))
4895           (setq gnus-newsgroup-unreads
4896                 (delq number gnus-newsgroup-unreads))
4897           (if gnus-newsgroup-auto-expire
4898               (push number gnus-newsgroup-expirable)
4899             (push (cons number gnus-low-score-mark)
4900                   gnus-newsgroup-reads)))
4901
4902         (setq mark (gnus-article-mark number))
4903         (push (gnus-data-make number mark (1+ (point)) header 0)
4904               gnus-newsgroup-data)
4905         (gnus-summary-insert-line
4906          header 0 number
4907          (memq number gnus-newsgroup-undownloaded)
4908          mark (memq number gnus-newsgroup-replied)
4909          (memq number gnus-newsgroup-expirable)
4910          (mail-header-subject header) nil
4911          (cdr (assq number gnus-newsgroup-scored))
4912          (memq number gnus-newsgroup-processable))))))
4913
4914 (defun gnus-summary-remove-list-identifiers ()
4915   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4916   (let ((regexp (if (consp gnus-list-identifiers)
4917                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4918                   gnus-list-identifiers))
4919         changed subject)
4920     (when regexp
4921       (dolist (header gnus-newsgroup-headers)
4922         (setq subject (mail-header-subject header)
4923               changed nil)
4924         (while (string-match
4925                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4926                 subject)
4927           (setq subject
4928                 (concat (substring subject 0 (match-beginning 2))
4929                         (substring subject (match-end 0)))
4930                 changed t))
4931         (when (and changed
4932                    (string-match
4933                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4934           (setq subject
4935                 (concat (substring subject 0 (match-beginning 1))
4936                         (substring subject (match-end 1)))))
4937         (when changed
4938           (mail-header-set-subject header subject))))))
4939
4940 (defun gnus-fetch-headers (articles)
4941   "Fetch headers of ARTICLES."
4942   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4943     (gnus-message 5 "Fetching headers for %s..." name)
4944     (prog1
4945         (if (eq 'nov
4946                 (setq gnus-headers-retrieved-by
4947                       (gnus-retrieve-headers
4948                        articles gnus-newsgroup-name
4949                        ;; We might want to fetch old headers, but
4950                        ;; not if there is only 1 article.
4951                        (and (or (and
4952                                  (not (eq gnus-fetch-old-headers 'some))
4953                                  (not (numberp gnus-fetch-old-headers)))
4954                                 (> (length articles) 1))
4955                             gnus-fetch-old-headers))))
4956             (gnus-get-newsgroup-headers-xover
4957              articles nil nil gnus-newsgroup-name t)
4958           (gnus-get-newsgroup-headers))
4959       (gnus-message 5 "Fetching headers for %s...done" name))))
4960
4961 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4962   "Select newsgroup GROUP.
4963 If READ-ALL is non-nil, all articles in the group are selected.
4964 If SELECT-ARTICLES, only select those articles from GROUP."
4965   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4966          ;;!!! Dirty hack; should be removed.
4967          (gnus-summary-ignore-duplicates
4968           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4969               t
4970             gnus-summary-ignore-duplicates))
4971          (info (nth 2 entry))
4972          articles fetched-articles cached)
4973
4974     (unless (gnus-check-server
4975              (set (make-local-variable 'gnus-current-select-method)
4976                   (gnus-find-method-for-group group)))
4977       (error "Couldn't open server"))
4978
4979     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4980         (gnus-activate-group group)     ; Or we can activate it...
4981         (progn                          ; Or we bug out.
4982           (when (equal major-mode 'gnus-summary-mode)
4983             (gnus-kill-buffer (current-buffer)))
4984           (error "Couldn't activate group %s: %s"
4985                  group (gnus-status-message group))))
4986
4987     (unless (gnus-request-group group t)
4988       (when (equal major-mode 'gnus-summary-mode)
4989         (gnus-kill-buffer (current-buffer)))
4990       (error "Couldn't request group %s: %s"
4991              group (gnus-status-message group)))
4992
4993     (setq gnus-newsgroup-name group
4994           gnus-newsgroup-unselected nil
4995           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4996
4997     (let ((display (gnus-group-find-parameter group 'display)))
4998       (setq gnus-newsgroup-display
4999             (cond
5000              ((not (zerop (or (car-safe read-all) 0)))
5001               ;; The user entered the group with C-u SPC/RET, let's show
5002               ;; all articles.
5003               'gnus-not-ignore)
5004              ((eq display 'all)
5005               'gnus-not-ignore)
5006              ((arrayp display)
5007               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5008              ((numberp display)
5009               ;; The following is probably the "correct" solution, but
5010               ;; it makes Gnus fetch all headers and then limit the
5011               ;; articles (which is slow), so instead we hack the
5012               ;; select-articles parameter instead. -- Simon Josefsson
5013               ;; <jas@kth.se>
5014               ;;
5015               ;; (gnus-byte-compile
5016               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5017               ;;                         display)))))
5018               (setq select-articles
5019                     (gnus-uncompress-range
5020                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5021                              (if (> tmp 0)
5022                                  tmp
5023                                1))
5024                            (cdr (gnus-active group)))))
5025               nil)
5026              (t
5027               nil))))
5028
5029     (gnus-summary-setup-default-charset)
5030
5031     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5032     (when (gnus-virtual-group-p group)
5033       (setq cached gnus-newsgroup-cached))
5034
5035     (setq gnus-newsgroup-unreads
5036           (gnus-sorted-ndifference
5037            (gnus-sorted-ndifference gnus-newsgroup-unreads
5038                                     gnus-newsgroup-marked)
5039            gnus-newsgroup-dormant))
5040
5041     (setq gnus-newsgroup-processable nil)
5042
5043     (gnus-update-read-articles group gnus-newsgroup-unreads)
5044
5045     ;; Adjust and set lists of article marks.
5046     (when info
5047       (gnus-adjust-marked-articles info))
5048     (if (setq articles select-articles)
5049         (setq gnus-newsgroup-unselected
5050               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5051       (setq articles (gnus-articles-to-read group read-all)))
5052
5053     (cond
5054      ((null articles)
5055       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5056       'quit)
5057      ((eq articles 0) nil)
5058      (t
5059       ;; Init the dependencies hash table.
5060       (setq gnus-newsgroup-dependencies
5061             (gnus-make-hashtable (length articles)))
5062       (gnus-set-global-variables)
5063       ;; Retrieve the headers and read them in.
5064
5065       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5066
5067       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5068       (when cached
5069         (setq gnus-newsgroup-cached cached))
5070
5071       ;; Suppress duplicates?
5072       (when gnus-suppress-duplicates
5073         (gnus-dup-suppress-articles))
5074
5075       ;; Set the initial limit.
5076       (setq gnus-newsgroup-limit (copy-sequence articles))
5077       ;; Remove canceled articles from the list of unread articles.
5078       (setq fetched-articles
5079             (mapcar (lambda (headers) (mail-header-number headers))
5080                     gnus-newsgroup-headers))
5081       (setq gnus-newsgroup-articles fetched-articles)
5082       (setq gnus-newsgroup-unreads
5083             (gnus-sorted-nintersection
5084              gnus-newsgroup-unreads fetched-articles))
5085       (gnus-compute-unseen-list)
5086
5087       ;; Removed marked articles that do not exist.
5088       (gnus-update-missing-marks
5089        (gnus-sorted-difference articles fetched-articles))
5090       ;; We might want to build some more threads first.
5091       (when (and gnus-fetch-old-headers
5092                  (eq gnus-headers-retrieved-by 'nov))
5093         (if (eq gnus-fetch-old-headers 'invisible)
5094             (gnus-build-all-threads)
5095           (gnus-build-old-threads)))
5096       ;; Let the Gnus agent mark articles as read.
5097       (when gnus-agent
5098         (gnus-agent-get-undownloaded-list))
5099       ;; Remove list identifiers from subject
5100       (when gnus-list-identifiers
5101         (gnus-summary-remove-list-identifiers))
5102       ;; Check whether auto-expire is to be done in this group.
5103       (setq gnus-newsgroup-auto-expire
5104             (gnus-group-auto-expirable-p group))
5105       ;; Set up the article buffer now, if necessary.
5106       (unless gnus-single-article-buffer
5107         (gnus-article-setup-buffer))
5108       ;; First and last article in this newsgroup.
5109       (when gnus-newsgroup-headers
5110         (setq gnus-newsgroup-begin
5111               (mail-header-number (car gnus-newsgroup-headers))
5112               gnus-newsgroup-end
5113               (mail-header-number
5114                (gnus-last-element gnus-newsgroup-headers))))
5115       ;; GROUP is successfully selected.
5116       (or gnus-newsgroup-headers t)))))
5117
5118 (defun gnus-compute-unseen-list ()
5119   ;; The `seen' marks are treated specially.
5120   (if (not gnus-newsgroup-seen)
5121       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5122     (setq gnus-newsgroup-unseen
5123           (gnus-inverse-list-range-intersection
5124            gnus-newsgroup-articles gnus-newsgroup-seen))))
5125
5126 (defun gnus-summary-display-make-predicate (display)
5127   (require 'gnus-agent)
5128   (when (= (length display) 1)
5129     (setq display (car display)))
5130   (unless gnus-summary-display-cache
5131     (dolist (elem (append '((unread . unread)
5132                             (read . read)
5133                             (unseen . unseen))
5134                           gnus-article-mark-lists))
5135       (push (cons (cdr elem)
5136                   (gnus-byte-compile
5137                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5138             gnus-summary-display-cache)))
5139   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5140         (gnus-category-predicate-cache gnus-summary-display-cache))
5141     (gnus-get-predicate display)))
5142
5143 ;; Uses the dynamically bound `number' variable.
5144 (eval-when-compile
5145   (defvar number))
5146 (defun gnus-article-marked-p (type &optional article)
5147   (let ((article (or article number)))
5148     (cond
5149      ((eq type 'tick)
5150       (memq article gnus-newsgroup-marked))
5151      ((eq type 'spam)
5152       (memq article gnus-newsgroup-spam-marked))
5153      ((eq type 'unsend)
5154       (memq article gnus-newsgroup-unsendable))
5155      ((eq type 'undownload)
5156       (memq article gnus-newsgroup-undownloaded))
5157      ((eq type 'download)
5158       (memq article gnus-newsgroup-downloadable))
5159      ((eq type 'unread)
5160       (memq article gnus-newsgroup-unreads))
5161      ((eq type 'read)
5162       (memq article gnus-newsgroup-reads))
5163      ((eq type 'dormant)
5164       (memq article gnus-newsgroup-dormant) )
5165      ((eq type 'expire)
5166       (memq article gnus-newsgroup-expirable))
5167      ((eq type 'reply)
5168       (memq article gnus-newsgroup-replied))
5169      ((eq type 'killed)
5170       (memq article gnus-newsgroup-killed))
5171      ((eq type 'bookmark)
5172       (assq article gnus-newsgroup-bookmarks))
5173      ((eq type 'score)
5174       (assq article gnus-newsgroup-scored))
5175      ((eq type 'save)
5176       (memq article gnus-newsgroup-saved))
5177      ((eq type 'cache)
5178       (memq article gnus-newsgroup-cached))
5179      ((eq type 'forward)
5180       (memq article gnus-newsgroup-forwarded))
5181      ((eq type 'seen)
5182       (not (memq article gnus-newsgroup-unseen)))
5183      ((eq type 'recent)
5184       (memq article gnus-newsgroup-recent))
5185      (t t))))
5186
5187 (defun gnus-articles-to-read (group &optional read-all)
5188   "Find out what articles the user wants to read."
5189   (let* ((display (gnus-group-find-parameter group 'display))
5190          (articles
5191           ;; Select all articles if `read-all' is non-nil, or if there
5192           ;; are no unread articles.
5193           (if (or read-all
5194                   (and (zerop (length gnus-newsgroup-marked))
5195                        (zerop (length gnus-newsgroup-unreads)))
5196                   ;; Fetch all if the predicate is non-nil.
5197                   gnus-newsgroup-display)
5198               ;; We want to select the headers for all the articles in
5199               ;; the group, so we select either all the active
5200               ;; articles in the group, or (if that's nil), the
5201               ;; articles in the cache.
5202               (or
5203                (gnus-uncompress-range (gnus-active group))
5204                (gnus-cache-articles-in-group group))
5205             ;; Select only the "normal" subset of articles.
5206             (gnus-sorted-nunion
5207              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5208              gnus-newsgroup-unreads)))
5209          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5210          (scored (length scored-list))
5211          (number (length articles))
5212          (marked (+ (length gnus-newsgroup-marked)
5213                     (length gnus-newsgroup-dormant)))
5214          (select
5215           (cond
5216            ((numberp read-all)
5217             read-all)
5218            ((numberp gnus-newsgroup-display)
5219             gnus-newsgroup-display)
5220            (t
5221             (condition-case ()
5222                 (cond
5223                  ((and (or (<= scored marked) (= scored number))
5224                        (numberp gnus-large-newsgroup)
5225                        (> number gnus-large-newsgroup))
5226                   (let* ((cursor-in-echo-area nil)
5227                          (initial (gnus-parameter-large-newsgroup-initial
5228                                    gnus-newsgroup-name))
5229                          (input
5230                           (read-string
5231                            (format
5232                             "How many articles from %s (%s %d): "
5233                             (gnus-limit-string
5234                              (gnus-group-decoded-name gnus-newsgroup-name)
5235                              35)
5236                             (if initial "max" "default")
5237                             number)
5238                            (if initial
5239                                (cons (number-to-string initial)
5240                                      0)))))
5241                     (if (string-match "^[ \t]*$" input) number input)))
5242                  ((and (> scored marked) (< scored number)
5243                        (> (- scored number) 20))
5244                   (let ((input
5245                          (read-string
5246                           (format "%s %s (%d scored, %d total): "
5247                                   "How many articles from"
5248                                   (gnus-group-decoded-name group)
5249                                   scored number))))
5250                     (if (string-match "^[ \t]*$" input)
5251                         number input)))
5252                  (t number))
5253               (quit
5254                (message "Quit getting the articles to read")
5255                nil))))))
5256     (setq select (if (stringp select) (string-to-number select) select))
5257     (if (or (null select) (zerop select))
5258         select
5259       (if (and (not (zerop scored)) (<= (abs select) scored))
5260           (progn
5261             (setq articles (sort scored-list '<))
5262             (setq number (length articles)))
5263         (setq articles (copy-sequence articles)))
5264
5265       (when (< (abs select) number)
5266         (if (< select 0)
5267             ;; Select the N oldest articles.
5268             (setcdr (nthcdr (1- (abs select)) articles) nil)
5269           ;; Select the N most recent articles.
5270           (setq articles (nthcdr (- number select) articles))))
5271       (setq gnus-newsgroup-unselected
5272             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5273       (when gnus-alter-articles-to-read-function
5274         (setq articles
5275               (sort
5276                (funcall gnus-alter-articles-to-read-function
5277                         gnus-newsgroup-name articles)
5278                '<)))
5279       articles)))
5280
5281 (defun gnus-killed-articles (killed articles)
5282   (let (out)
5283     (while articles
5284       (when (inline (gnus-member-of-range (car articles) killed))
5285         (push (car articles) out))
5286       (setq articles (cdr articles)))
5287     out))
5288
5289 (defun gnus-uncompress-marks (marks)
5290   "Uncompress the mark ranges in MARKS."
5291   (let ((uncompressed '(score bookmark))
5292         out)
5293     (while marks
5294       (if (memq (caar marks) uncompressed)
5295           (push (car marks) out)
5296         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5297       (setq marks (cdr marks)))
5298     out))
5299
5300 (defun gnus-article-mark-to-type (mark)
5301   "Return the type of MARK."
5302   (or (cadr (assq mark gnus-article-special-mark-lists))
5303       'list))
5304
5305 (defun gnus-article-unpropagatable-p (mark)
5306   "Return whether MARK should be propagated to backend."
5307   (memq mark gnus-article-unpropagated-mark-lists))
5308
5309 (defun gnus-adjust-marked-articles (info)
5310   "Set all article lists and remove all marks that are no longer valid."
5311   (let* ((marked-lists (gnus-info-marks info))
5312          (active (gnus-active (gnus-info-group info)))
5313          (min (car active))
5314          (max (cdr active))
5315          (types gnus-article-mark-lists)
5316          marks var articles article mark mark-type)
5317
5318     (dolist (marks marked-lists)
5319       (setq mark (car marks)
5320             mark-type (gnus-article-mark-to-type mark)
5321             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5322
5323       ;; We set the variable according to the type of the marks list,
5324       ;; and then adjust the marks to a subset of the active articles.
5325       (cond
5326        ;; Adjust "simple" lists.
5327        ((eq mark-type 'list)
5328         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5329         (when (memq mark '(tick dormant expire reply save))
5330           (while articles
5331             (when (or (< (setq article (pop articles)) min) (> article max))
5332               (set var (delq article (symbol-value var)))))))
5333        ;; Adjust assocs.
5334        ((eq mark-type 'tuple)
5335         (set var (setq articles (cdr marks)))
5336         (when (not (listp (cdr (symbol-value var))))
5337           (set var (list (symbol-value var))))
5338         (when (not (listp (cdr articles)))
5339           (setq articles (list articles)))
5340         (while articles
5341           (when (or (not (consp (setq article (pop articles))))
5342                     (< (car article) min)
5343                     (> (car article) max))
5344             (set var (delq article (symbol-value var))))))
5345        ;; Adjust ranges (sloppily).
5346        ((eq mark-type 'range)
5347         (cond
5348          ((eq mark 'seen)
5349           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5350           ;; It should be (seen (NUM1 . NUM2)).
5351           (when (numberp (cddr marks))
5352             (setcdr marks (list (cdr marks))))
5353           (setq articles (cdr marks))
5354           (while (and articles
5355                       (or (and (consp (car articles))
5356                                (> min (cdar articles)))
5357                           (and (numberp (car articles))
5358                                (> min (car articles)))))
5359             (pop articles))
5360           (set var articles))))))))
5361
5362 (defun gnus-update-missing-marks (missing)
5363   "Go through the list of MISSING articles and remove them from the mark lists."
5364   (when missing
5365     (let (var m)
5366       ;; Go through all types.
5367       (dolist (elem gnus-article-mark-lists)
5368         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5369           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5370           (when (symbol-value var)
5371             ;; This list has articles.  So we delete all missing
5372             ;; articles from it.
5373             (setq m missing)
5374             (while m
5375               (set var (delq (pop m) (symbol-value var))))))))))
5376
5377 (defun gnus-update-marks ()
5378   "Enter the various lists of marked articles into the newsgroup info list."
5379   (let ((types gnus-article-mark-lists)
5380         (info (gnus-get-info gnus-newsgroup-name))
5381         type list newmarked symbol delta-marks)
5382     (when info
5383       ;; Add all marks lists to the list of marks lists.
5384       (while (setq type (pop types))
5385         (setq list (symbol-value
5386                     (setq symbol
5387                           (intern (format "gnus-newsgroup-%s" (car type))))))
5388
5389         (when list
5390           ;; Get rid of the entries of the articles that have the
5391           ;; default score.
5392           (when (and (eq (cdr type) 'score)
5393                      gnus-save-score
5394                      list)
5395             (let* ((arts list)
5396                    (prev (cons nil list))
5397                    (all prev))
5398               (while arts
5399                 (if (or (not (consp (car arts)))
5400                         (= (cdar arts) gnus-summary-default-score))
5401                     (setcdr prev (cdr arts))
5402                   (setq prev arts))
5403                 (setq arts (cdr arts)))
5404               (setq list (cdr all)))))
5405
5406         (when (eq (cdr type) 'seen)
5407           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5408
5409         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5410           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5411
5412         (when (and (gnus-check-backend-function
5413                     'request-set-mark gnus-newsgroup-name)
5414                    (not (gnus-article-unpropagatable-p (cdr type))))
5415           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5416                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5417                  (add (gnus-remove-from-range
5418                        (gnus-copy-sequence list) old)))
5419             (when add
5420               (push (list add 'add (list (cdr type))) delta-marks))
5421             (when del
5422               (push (list del 'del (list (cdr type))) delta-marks))))
5423
5424         (when list
5425           (push (cons (cdr type) list) newmarked)))
5426
5427       (when delta-marks
5428         (unless (gnus-check-group gnus-newsgroup-name)
5429           (error "Can't open server for %s" gnus-newsgroup-name))
5430         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5431
5432       ;; Enter these new marks into the info of the group.
5433       (if (nthcdr 3 info)
5434           (setcar (nthcdr 3 info) newmarked)
5435         ;; Add the marks lists to the end of the info.
5436         (when newmarked
5437           (setcdr (nthcdr 2 info) (list newmarked))))
5438
5439       ;; Cut off the end of the info if there's nothing else there.
5440       (let ((i 5))
5441         (while (and (> i 2)
5442                     (not (nth i info)))
5443           (when (nthcdr (decf i) info)
5444             (setcdr (nthcdr i info) nil)))))))
5445
5446 (defun gnus-set-mode-line (where)
5447   "Set the mode line of the article or summary buffers.
5448 If WHERE is `summary', the summary mode line format will be used."
5449   ;; Is this mode line one we keep updated?
5450   (when (and (memq where gnus-updated-mode-lines)
5451              (symbol-value
5452               (intern (format "gnus-%s-mode-line-format-spec" where))))
5453     (let (mode-string)
5454       (save-excursion
5455         ;; We evaluate this in the summary buffer since these
5456         ;; variables are buffer-local to that buffer.
5457         (set-buffer gnus-summary-buffer)
5458         ;; We bind all these variables that are used in the `eval' form
5459         ;; below.
5460         (let* ((mformat (symbol-value
5461                          (intern
5462                           (format "gnus-%s-mode-line-format-spec" where))))
5463                (gnus-tmp-group-name (gnus-group-decoded-name
5464                                      gnus-newsgroup-name))
5465                (gnus-tmp-article-number (or gnus-current-article 0))
5466                (gnus-tmp-unread gnus-newsgroup-unreads)
5467                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5468                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5469                (gnus-tmp-unread-and-unselected
5470                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5471                             (zerop gnus-tmp-unselected))
5472                        "")
5473                       ((zerop gnus-tmp-unselected)
5474                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5475                       (t (format "{%d(+%d) more}"
5476                                  gnus-tmp-unread-and-unticked
5477                                  gnus-tmp-unselected))))
5478                (gnus-tmp-subject
5479                 (if (and gnus-current-headers
5480                          (vectorp gnus-current-headers))
5481                     (gnus-mode-string-quote
5482                      (mail-header-subject gnus-current-headers))
5483                   ""))
5484                bufname-length max-len
5485                gnus-tmp-header);; passed as argument to any user-format-funcs
5486           (setq mode-string (eval mformat))
5487           (setq bufname-length (if (string-match "%b" mode-string)
5488                                    (- (length
5489                                        (buffer-name
5490                                         (if (eq where 'summary)
5491                                             nil
5492                                           (get-buffer gnus-article-buffer))))
5493                                       2)
5494                                  0))
5495           (setq max-len (max 4 (if gnus-mode-non-string-length
5496                                    (- (window-width)
5497                                       gnus-mode-non-string-length
5498                                       bufname-length)
5499                                  (length mode-string))))
5500           ;; We might have to chop a bit of the string off...
5501           (when (> (length mode-string) max-len)
5502             (setq mode-string
5503                   (concat (gnus-truncate-string mode-string (- max-len 3))
5504                           "...")))
5505           ;; Pad the mode string a bit.
5506           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5507       ;; Update the mode line.
5508       (setq mode-line-buffer-identification
5509             (gnus-mode-line-buffer-identification (list mode-string)))
5510       (set-buffer-modified-p t))))
5511
5512 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5513   "Go through the HEADERS list and add all Xrefs to a hash table.
5514 The resulting hash table is returned, or nil if no Xrefs were found."
5515   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5516          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5517          (xref-hashtb (gnus-make-hashtable))
5518          start group entry number xrefs header)
5519     (while headers
5520       (setq header (pop headers))
5521       (when (and (setq xrefs (mail-header-xref header))
5522                  (not (memq (setq number (mail-header-number header))
5523                             unreads)))
5524         (setq start 0)
5525         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5526           (setq start (match-end 0))
5527           (setq group (if prefix
5528                           (concat prefix (substring xrefs (match-beginning 1)
5529                                                     (match-end 1)))
5530                         (substring xrefs (match-beginning 1) (match-end 1))))
5531           (setq number
5532                 (string-to-int (substring xrefs (match-beginning 2)
5533                                           (match-end 2))))
5534           (if (setq entry (gnus-gethash group xref-hashtb))
5535               (setcdr entry (cons number (cdr entry)))
5536             (gnus-sethash group (cons number nil) xref-hashtb)))))
5537     (and start xref-hashtb)))
5538
5539 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5540   "Look through all the headers and mark the Xrefs as read."
5541   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5542         name entry info xref-hashtb idlist method nth4)
5543     (save-excursion
5544       (set-buffer gnus-group-buffer)
5545       (when (setq xref-hashtb
5546                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5547         (mapatoms
5548          (lambda (group)
5549            (unless (string= from-newsgroup (setq name (symbol-name group)))
5550              (setq idlist (symbol-value group))
5551              ;; Dead groups are not updated.
5552              (and (prog1
5553                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5554                             info (nth 2 entry))
5555                     (when (stringp (setq nth4 (gnus-info-method info)))
5556                       (setq nth4 (gnus-server-to-method nth4))))
5557                   ;; Only do the xrefs if the group has the same
5558                   ;; select method as the group we have just read.
5559                   (or (gnus-methods-equal-p
5560                        nth4 (gnus-find-method-for-group from-newsgroup))
5561                       virtual
5562                       (equal nth4 (setq method (gnus-find-method-for-group
5563                                                 from-newsgroup)))
5564                       (and (equal (car nth4) (car method))
5565                            (equal (nth 1 nth4) (nth 1 method))))
5566                   gnus-use-cross-reference
5567                   (or (not (eq gnus-use-cross-reference t))
5568                       virtual
5569                       ;; Only do cross-references on subscribed
5570                       ;; groups, if that is what is wanted.
5571                       (<= (gnus-info-level info) gnus-level-subscribed))
5572                   (gnus-group-make-articles-read name idlist))))
5573          xref-hashtb)))))
5574
5575 (defun gnus-compute-read-articles (group articles)
5576   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5577          (info (nth 2 entry))
5578          (active (gnus-active group))
5579          ninfo)
5580     (when entry
5581       ;; First peel off all invalid article numbers.
5582       (when active
5583         (let ((ids articles)
5584               id first)
5585           (while (setq id (pop ids))
5586             (when (and first (> id (cdr active)))
5587               ;; We'll end up in this situation in one particular
5588               ;; obscure situation.  If you re-scan a group and get
5589               ;; a new article that is cross-posted to a different
5590               ;; group that has not been re-scanned, you might get
5591               ;; crossposted article that has a higher number than
5592               ;; Gnus believes possible.  So we re-activate this
5593               ;; group as well.  This might mean doing the
5594               ;; crossposting thingy will *increase* the number
5595               ;; of articles in some groups.  Tsk, tsk.
5596               (setq active (or (gnus-activate-group group) active)))
5597             (when (or (> id (cdr active))
5598                       (< id (car active)))
5599               (setq articles (delq id articles))))))
5600       ;; If the read list is nil, we init it.
5601       (if (and active
5602                (null (gnus-info-read info))
5603                (> (car active) 1))
5604           (setq ninfo (cons 1 (1- (car active))))
5605         (setq ninfo (gnus-info-read info)))
5606       ;; Then we add the read articles to the range.
5607       (gnus-add-to-range
5608        ninfo (setq articles (sort articles '<))))))
5609
5610 (defun gnus-group-make-articles-read (group articles)
5611   "Update the info of GROUP to say that ARTICLES are read."
5612   (let* ((num 0)
5613          (entry (gnus-gethash group gnus-newsrc-hashtb))
5614          (info (nth 2 entry))
5615          (active (gnus-active group))
5616          range)
5617     (when entry
5618       (setq range (gnus-compute-read-articles group articles))
5619       (save-excursion
5620         (set-buffer gnus-group-buffer)
5621         (gnus-undo-register
5622           `(progn
5623              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5624              (gnus-info-set-read ',info ',(gnus-info-read info))
5625              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5626              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5627              (gnus-group-update-group ,group t))))
5628       ;; Add the read articles to the range.
5629       (gnus-info-set-read info range)
5630       (gnus-request-set-mark group (list (list range 'add '(read))))
5631       ;; Then we have to re-compute how many unread
5632       ;; articles there are in this group.
5633       (when active
5634         (cond
5635          ((not range)
5636           (setq num (- (1+ (cdr active)) (car active))))
5637          ((not (listp (cdr range)))
5638           (setq num (- (cdr active) (- (1+ (cdr range))
5639                                        (car range)))))
5640          (t
5641           (while range
5642             (if (numberp (car range))
5643                 (setq num (1+ num))
5644               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5645             (setq range (cdr range)))
5646           (setq num (- (cdr active) num))))
5647         ;; Update the number of unread articles.
5648         (setcar entry num)
5649         ;; Update the group buffer.
5650         (unless (gnus-ephemeral-group-p group)
5651           (gnus-group-update-group group t))))))
5652
5653 (defvar gnus-newsgroup-none-id 0)
5654
5655 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5656   (let ((cur nntp-server-buffer)
5657         (dependencies
5658          (or dependencies
5659              (save-excursion (set-buffer gnus-summary-buffer)
5660                              gnus-newsgroup-dependencies)))
5661         headers id end ref
5662         (mail-parse-charset gnus-newsgroup-charset)
5663         (mail-parse-ignored-charsets
5664          (save-excursion (condition-case nil
5665                              (set-buffer gnus-summary-buffer)
5666                            (error))
5667                          gnus-newsgroup-ignored-charsets)))
5668     (save-excursion
5669       (set-buffer nntp-server-buffer)
5670       ;; Translate all TAB characters into SPACE characters.
5671       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5672       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5673       (gnus-run-hooks 'gnus-parse-headers-hook)
5674       (let ((case-fold-search t)
5675             in-reply-to header p lines chars ctype)
5676         (goto-char (point-min))
5677         ;; Search to the beginning of the next header.  Error messages
5678         ;; do not begin with 2 or 3.
5679         (while (re-search-forward "^[23][0-9]+ " nil t)
5680           (setq id nil
5681                 ref nil)
5682           ;; This implementation of this function, with nine
5683           ;; search-forwards instead of the one re-search-forward and
5684           ;; a case (which basically was the old function) is actually
5685           ;; about twice as fast, even though it looks messier.  You
5686           ;; can't have everything, I guess.  Speed and elegance
5687           ;; doesn't always go hand in hand.
5688           (setq
5689            header
5690            (make-full-mail-header
5691             ;; Number.
5692             (prog1
5693                 (read cur)
5694               (end-of-line)
5695               (setq p (point))
5696               (narrow-to-region (point)
5697                                 (or (and (search-forward "\n.\n" nil t)
5698                                          (- (point) 2))
5699                                     (point))))
5700             ;; Subject.
5701             (progn
5702               (goto-char p)
5703               (if (search-forward "\nsubject:" nil t)
5704                   (nnheader-header-value)
5705                 "(none)"))
5706             ;; From.
5707             (progn
5708               (goto-char p)
5709               (if (search-forward "\nfrom:" nil t)
5710                   (nnheader-header-value)
5711                 "(nobody)"))
5712             ;; Date.
5713             (progn
5714               (goto-char p)
5715               (if (search-forward "\ndate:" nil t)
5716                   (nnheader-header-value) ""))
5717             ;; Message-ID.
5718             (progn
5719               (goto-char p)
5720               (setq id (if (re-search-forward
5721                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5722                            ;; We do it this way to make sure the Message-ID
5723                            ;; is (somewhat) syntactically valid.
5724                            (buffer-substring (match-beginning 1)
5725                                              (match-end 1))
5726                          ;; If there was no message-id, we just fake one
5727                          ;; to make subsequent routines simpler.
5728                          (nnheader-generate-fake-message-id))))
5729             ;; References.
5730             (progn
5731               (goto-char p)
5732               (if (search-forward "\nreferences:" nil t)
5733                   (progn
5734                     (setq end (point))
5735                     (prog1
5736                         (nnheader-header-value)
5737                       (setq ref
5738                             (buffer-substring
5739                              (progn
5740                                ;; (end-of-line)
5741                                (search-backward ">" end t)
5742                                (1+ (point)))
5743                              (progn
5744                                (search-backward "<" end t)
5745                                (point))))))
5746                 ;; Get the references from the in-reply-to header if there
5747                 ;; were no references and the in-reply-to header looks
5748                 ;; promising.
5749                 (if (and (search-forward "\nin-reply-to:" nil t)
5750                          (setq in-reply-to (nnheader-header-value))
5751                          (string-match "<[^>]+>" in-reply-to))
5752                     (let (ref2)
5753                       (setq ref (substring in-reply-to (match-beginning 0)
5754                                            (match-end 0)))
5755                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5756                         (setq ref2 (substring in-reply-to (match-beginning 0)
5757                                               (match-end 0)))
5758                         (when (> (length ref2) (length ref))
5759                           (setq ref ref2)))
5760                       ref)
5761                   (setq ref nil))))
5762             ;; Chars.
5763             (progn
5764               (goto-char p)
5765               (if (search-forward "\nchars: " nil t)
5766                   (if (numberp (setq chars (ignore-errors (read cur))))
5767                       chars -1)
5768                 -1))
5769             ;; Lines.
5770             (progn
5771               (goto-char p)
5772               (if (search-forward "\nlines: " nil t)
5773                   (if (numberp (setq lines (ignore-errors (read cur))))
5774                       lines -1)
5775                 -1))
5776             ;; Xref.
5777             (progn
5778               (goto-char p)
5779               (and (search-forward "\nxref:" nil t)
5780                    (nnheader-header-value)))
5781             ;; Extra.
5782             (when gnus-extra-headers
5783               (let ((extra gnus-extra-headers)
5784                     out)
5785                 (while extra
5786                   (goto-char p)
5787                   (when (search-forward
5788                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5789                     (push (cons (car extra) (nnheader-header-value)) out))
5790                   (pop extra))
5791                 out))))
5792           (goto-char p)
5793           (if (and (search-forward "\ncontent-type: " nil t)
5794                    (setq ctype (nnheader-header-value)))
5795               (mime-entity-set-content-type-internal
5796                header (mime-parse-Content-Type ctype)))
5797           (when (equal id ref)
5798             (setq ref nil))
5799
5800           (when gnus-alter-header-function
5801             (funcall gnus-alter-header-function header)
5802             (setq id (mail-header-id header)
5803                   ref (gnus-parent-id (mail-header-references header))))
5804
5805           (when (setq header
5806                       (gnus-dependencies-add-header
5807                        header dependencies force-new))
5808             (push header headers))
5809           (goto-char (point-max))
5810           (widen))
5811         (nreverse headers)))))
5812
5813 ;; Goes through the xover lines and returns a list of vectors
5814 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5815                                                   force-new dependencies
5816                                                   group also-fetch-heads)
5817   "Parse the news overview data in the server buffer.
5818 Return a list of headers that match SEQUENCE (see
5819 `nntp-retrieve-headers')."
5820   ;; Get the Xref when the users reads the articles since most/some
5821   ;; NNTP servers do not include Xrefs when using XOVER.
5822   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5823   (let ((mail-parse-charset gnus-newsgroup-charset)
5824         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5825         (cur nntp-server-buffer)
5826         (dependencies (or dependencies gnus-newsgroup-dependencies))
5827         (allp (cond
5828                ((eq gnus-read-all-available-headers t)
5829                 t)
5830                ((stringp gnus-read-all-available-headers)
5831                 (string-match gnus-read-all-available-headers group))
5832                (t
5833                 nil)))
5834         number headers header)
5835     (save-excursion
5836       (set-buffer nntp-server-buffer)
5837       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5838       ;; Allow the user to mangle the headers before parsing them.
5839       (gnus-run-hooks 'gnus-parse-headers-hook)
5840       (goto-char (point-min))
5841       (gnus-parse-without-error
5842         (while (and (or sequence allp)
5843                     (not (eobp)))
5844           (setq number (read cur))
5845           (when (not allp)
5846             (while (and sequence
5847                         (< (car sequence) number))
5848               (setq sequence (cdr sequence))))
5849           (when (and (or allp
5850                          (and sequence
5851                               (eq number (car sequence))))
5852                      (progn
5853                        (setq sequence (cdr sequence))
5854                        (setq header (inline
5855                                       (gnus-nov-parse-line
5856                                        number dependencies force-new)))))
5857             (push header headers))
5858           (forward-line 1)))
5859       ;; A common bug in inn is that if you have posted an article and
5860       ;; then retrieves the active file, it will answer correctly --
5861       ;; the new article is included.  However, a NOV entry for the
5862       ;; article may not have been generated yet, so this may fail.
5863       ;; We work around this problem by retrieving the last few
5864       ;; headers using HEAD.
5865       (if (or (not also-fetch-heads)
5866               (not sequence))
5867           ;; We (probably) got all the headers.
5868           (nreverse headers)
5869         (let ((gnus-nov-is-evil t))
5870           (nconc
5871            (nreverse headers)
5872            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5873              (gnus-get-newsgroup-headers))))))))
5874
5875 (defun gnus-article-get-xrefs ()
5876   "Fill in the Xref value in `gnus-current-headers', if necessary.
5877 This is meant to be called in `gnus-article-internal-prepare-hook'."
5878   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5879                                  gnus-current-headers)))
5880     (or (not gnus-use-cross-reference)
5881         (not headers)
5882         (and (mail-header-xref headers)
5883              (not (string= (mail-header-xref headers) "")))
5884         (let ((case-fold-search t)
5885               xref)
5886           (save-restriction
5887             (nnheader-narrow-to-headers)
5888             (goto-char (point-min))
5889             (when (or (and (not (eobp))
5890                            (eq (downcase (char-after)) ?x)
5891                            (looking-at "Xref:"))
5892                       (search-forward "\nXref:" nil t))
5893               (goto-char (1+ (match-end 0)))
5894               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5895               (mail-header-set-xref headers xref)))))))
5896
5897 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5898   "Find article ID and insert the summary line for that article.
5899 OLD-HEADER can either be a header or a line number to insert
5900 the subject line on."
5901   (let* ((line (and (numberp old-header) old-header))
5902          (old-header (and (vectorp old-header) old-header))
5903          (header (cond ((and old-header use-old-header)
5904                         old-header)
5905                        ((and (numberp id)
5906                              (gnus-number-to-header id))
5907                         (gnus-number-to-header id))
5908                        (t
5909                         (gnus-read-header id))))
5910          (number (and (numberp id) id))
5911          d)
5912     (when header
5913       ;; Rebuild the thread that this article is part of and go to the
5914       ;; article we have fetched.
5915       (when (and (not gnus-show-threads)
5916                  old-header)
5917         (when (and number
5918                    (setq d (gnus-data-find (mail-header-number old-header))))
5919           (goto-char (gnus-data-pos d))
5920           (gnus-data-remove
5921            number
5922            (- (gnus-point-at-bol)
5923               (prog1
5924                   (1+ (gnus-point-at-eol))
5925                 (gnus-delete-line))))))
5926       (when old-header
5927         (mail-header-set-number header (mail-header-number old-header)))
5928       (setq gnus-newsgroup-sparse
5929             (delq (setq number (mail-header-number header))
5930                   gnus-newsgroup-sparse))
5931       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5932       (push number gnus-newsgroup-limit)
5933       (gnus-rebuild-thread (mail-header-id header) line)
5934       (gnus-summary-goto-subject number nil t))
5935     (when (and (numberp number)
5936                (> number 0))
5937       ;; We have to update the boundaries even if we can't fetch the
5938       ;; article if ID is a number -- so that the next `P' or `N'
5939       ;; command will fetch the previous (or next) article even
5940       ;; if the one we tried to fetch this time has been canceled.
5941       (when (> number gnus-newsgroup-end)
5942         (setq gnus-newsgroup-end number))
5943       (when (< number gnus-newsgroup-begin)
5944         (setq gnus-newsgroup-begin number))
5945       (setq gnus-newsgroup-unselected
5946             (delq number gnus-newsgroup-unselected)))
5947     ;; Report back a success?
5948     (and header (mail-header-number header))))
5949
5950 ;;; Process/prefix in the summary buffer
5951
5952 (defun gnus-summary-work-articles (n)
5953   "Return a list of articles to be worked upon.
5954 The prefix argument, the list of process marked articles, and the
5955 current article will be taken into consideration."
5956   (save-excursion
5957     (set-buffer gnus-summary-buffer)
5958     (cond
5959      (n
5960       ;; A numerical prefix has been given.
5961       (setq n (prefix-numeric-value n))
5962       (let ((backward (< n 0))
5963             (n (abs (prefix-numeric-value n)))
5964             articles article)
5965         (save-excursion
5966           (while
5967               (and (> n 0)
5968                    (push (setq article (gnus-summary-article-number))
5969                          articles)
5970                    (if backward
5971                        (gnus-summary-find-prev nil article)
5972                      (gnus-summary-find-next nil article)))
5973             (decf n)))
5974         (nreverse articles)))
5975      ((and (gnus-region-active-p) (mark))
5976       (message "region active")
5977       ;; Work on the region between point and mark.
5978       (let ((max (max (point) (mark)))
5979             articles article)
5980         (save-excursion
5981           (goto-char (min (point) (mark)))
5982           (while
5983               (and
5984                (push (setq article (gnus-summary-article-number)) articles)
5985                (gnus-summary-find-next nil article)
5986                (< (point) max)))
5987           (nreverse articles))))
5988      (gnus-newsgroup-processable
5989       ;; There are process-marked articles present.
5990       ;; Save current state.
5991       (gnus-summary-save-process-mark)
5992       ;; Return the list.
5993       (reverse gnus-newsgroup-processable))
5994      (t
5995       ;; Just return the current article.
5996       (list (gnus-summary-article-number))))))
5997
5998 (defmacro gnus-summary-iterate (arg &rest forms)
5999   "Iterate over the process/prefixed articles and do FORMS.
6000 ARG is the interactive prefix given to the command.  FORMS will be
6001 executed with point over the summary line of the articles."
6002   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6003     `(let ((,articles (gnus-summary-work-articles ,arg)))
6004        (while ,articles
6005          (gnus-summary-goto-subject (car ,articles))
6006          ,@forms
6007          (pop ,articles)))))
6008
6009 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6010 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6011
6012 (defun gnus-summary-save-process-mark ()
6013   "Push the current set of process marked articles on the stack."
6014   (interactive)
6015   (push (copy-sequence gnus-newsgroup-processable)
6016         gnus-newsgroup-process-stack))
6017
6018 (defun gnus-summary-kill-process-mark ()
6019   "Push the current set of process marked articles on the stack and unmark."
6020   (interactive)
6021   (gnus-summary-save-process-mark)
6022   (gnus-summary-unmark-all-processable))
6023
6024 (defun gnus-summary-yank-process-mark ()
6025   "Pop the last process mark state off the stack and restore it."
6026   (interactive)
6027   (unless gnus-newsgroup-process-stack
6028     (error "Empty mark stack"))
6029   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6030
6031 (defun gnus-summary-process-mark-set (set)
6032   "Make SET into the current process marked articles."
6033   (gnus-summary-unmark-all-processable)
6034   (while set
6035     (gnus-summary-set-process-mark (pop set))))
6036
6037 ;;; Searching and stuff
6038
6039 (defun gnus-summary-search-group (&optional backward use-level)
6040   "Search for next unread newsgroup.
6041 If optional argument BACKWARD is non-nil, search backward instead."
6042   (save-excursion
6043     (set-buffer gnus-group-buffer)
6044     (when (gnus-group-search-forward
6045            backward nil (if use-level (gnus-group-group-level) nil))
6046       (gnus-group-group-name))))
6047
6048 (defun gnus-summary-best-group (&optional exclude-group)
6049   "Find the name of the best unread group.
6050 If EXCLUDE-GROUP, do not go to this group."
6051   (save-excursion
6052     (set-buffer gnus-group-buffer)
6053     (save-excursion
6054       (gnus-group-best-unread-group exclude-group))))
6055
6056 (defun gnus-summary-find-next (&optional unread article backward)
6057   (if backward (gnus-summary-find-prev unread article)
6058     (let* ((dummy (gnus-summary-article-intangible-p))
6059            (article (or article (gnus-summary-article-number)))
6060            (data (gnus-data-find-list article))
6061            result)
6062       (when (and (not dummy)
6063                  (or (not gnus-summary-check-current)
6064                      (not unread)
6065                      (not (gnus-data-unread-p (car data)))))
6066         (setq data (cdr data)))
6067       (when (setq result
6068                   (if unread
6069                       (progn
6070                         (while data
6071                           (unless (memq (gnus-data-number (car data)) 
6072                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6073                                                gnus-newsgroup-undownloaded)
6074                                               (gnus-plugged
6075                                                nil)
6076                                               ((eq gnus-auto-goto-ignores 'unfetched)
6077                                                gnus-newsgroup-unfetched)
6078                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6079                                                gnus-newsgroup-undownloaded)))
6080                             (when (gnus-data-unread-p (car data))
6081                               (setq result (car data)
6082                                     data nil)))
6083                           (setq data (cdr data)))
6084                         result)
6085                     (car data)))
6086         (goto-char (gnus-data-pos result))
6087         (gnus-data-number result)))))
6088
6089 (defun gnus-summary-find-prev (&optional unread article)
6090   (let* ((eobp (eobp))
6091          (article (or article (gnus-summary-article-number)))
6092          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6093          result)
6094     (when (and (not eobp)
6095                (or (not gnus-summary-check-current)
6096                    (not unread)
6097                    (not (gnus-data-unread-p (car data)))))
6098       (setq data (cdr data)))
6099     (when (setq result
6100                 (if unread
6101                     (progn
6102                       (while data
6103                         (unless (memq (gnus-data-number (car data))
6104                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6105                                              gnus-newsgroup-undownloaded)
6106                                             (gnus-plugged
6107                                              nil)
6108                                             ((eq gnus-auto-goto-ignores 'unfetched)
6109                                              gnus-newsgroup-unfetched)
6110                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6111                                              gnus-newsgroup-undownloaded)))
6112                           (when (gnus-data-unread-p (car data))
6113                             (setq result (car data)
6114                                   data nil)))
6115                         (setq data (cdr data)))
6116                       result)
6117                   (car data)))
6118       (goto-char (gnus-data-pos result))
6119       (gnus-data-number result))))
6120
6121 (defun gnus-summary-find-subject (subject &optional unread backward article)
6122   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6123          (article (or article (gnus-summary-article-number)))
6124          (articles (gnus-data-list backward))
6125          (arts (gnus-data-find-list article articles))
6126          result)
6127     (when (or (not gnus-summary-check-current)
6128               (not unread)
6129               (not (gnus-data-unread-p (car arts))))
6130       (setq arts (cdr arts)))
6131     (while arts
6132       (and (or (not unread)
6133                (gnus-data-unread-p (car arts)))
6134            (vectorp (gnus-data-header (car arts)))
6135            (gnus-subject-equal
6136             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6137            (setq result (car arts)
6138                  arts nil))
6139       (setq arts (cdr arts)))
6140     (and result
6141          (goto-char (gnus-data-pos result))
6142          (gnus-data-number result))))
6143
6144 (defun gnus-summary-search-forward (&optional unread subject backward)
6145   "Search forward for an article.
6146 If UNREAD, look for unread articles.  If SUBJECT, look for
6147 articles with that subject.  If BACKWARD, search backward instead."
6148   (cond (subject (gnus-summary-find-subject subject unread backward))
6149         (backward (gnus-summary-find-prev unread))
6150         (t (gnus-summary-find-next unread))))
6151
6152 (defun gnus-recenter (&optional n)
6153   "Center point in window and redisplay frame.
6154 Also do horizontal recentering."
6155   (interactive "P")
6156   (when (and gnus-auto-center-summary
6157              (not (eq gnus-auto-center-summary 'vertical)))
6158     (gnus-horizontal-recenter))
6159   (recenter n))
6160
6161 (defun gnus-summary-recenter ()
6162   "Center point in the summary window.
6163 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6164 displayed, no centering will be performed."
6165   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6166   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6167   (interactive)
6168   ;; The user has to want it.
6169   (when gnus-auto-center-summary
6170     (let* ((top (cond ((< (window-height) 4) 0)
6171                       ((< (window-height) 7) 1)
6172                       (t (if (numberp gnus-auto-center-summary)
6173                              gnus-auto-center-summary
6174                            2))))
6175            (height (1- (window-height)))
6176            (bottom (save-excursion (goto-char (point-max))
6177                                    (forward-line (- height))
6178                                    (point)))
6179            (window (get-buffer-window (current-buffer))))
6180       (when (get-buffer-window gnus-article-buffer)
6181         ;; Only do recentering when the article buffer is displayed,
6182         ;; Set the window start to either `bottom', which is the biggest
6183         ;; possible valid number, or the second line from the top,
6184         ;; whichever is the least.
6185         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6186           (if (> bottom top-pos)
6187               ;; Keep the second line from the top visible
6188               (set-window-start window top-pos t)
6189             ;; Try to keep the bottom line visible; if it's partially
6190             ;; obscured, either scroll one more line to make it fully
6191             ;; visible, or revert to using TOP-POS.
6192             (save-excursion
6193               (goto-char (point-max))
6194               (forward-line -1)
6195               (let ((last-line-start (point)))
6196                 (goto-char bottom)
6197                 (set-window-start window (point) t)
6198                 (when (not (pos-visible-in-window-p last-line-start window))
6199                   (forward-line 1)
6200                   (set-window-start window (min (point) top-pos) t)))))))
6201       ;; Do horizontal recentering while we're at it.
6202       (when (and (get-buffer-window (current-buffer) t)
6203                  (not (eq gnus-auto-center-summary 'vertical)))
6204         (let ((selected (selected-window)))
6205           (select-window (get-buffer-window (current-buffer) t))
6206           (gnus-summary-position-point)
6207           (gnus-horizontal-recenter)
6208           (select-window selected))))))
6209
6210 (defun gnus-summary-jump-to-group (newsgroup)
6211   "Move point to NEWSGROUP in group mode buffer."
6212   ;; Keep update point of group mode buffer if visible.
6213   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6214       (save-window-excursion
6215         ;; Take care of tree window mode.
6216         (when (get-buffer-window gnus-group-buffer)
6217           (pop-to-buffer gnus-group-buffer))
6218         (gnus-group-jump-to-group newsgroup))
6219     (save-excursion
6220       ;; Take care of tree window mode.
6221       (if (get-buffer-window gnus-group-buffer)
6222           (pop-to-buffer gnus-group-buffer)
6223         (set-buffer gnus-group-buffer))
6224       (gnus-group-jump-to-group newsgroup))))
6225
6226 ;; This function returns a list of article numbers based on the
6227 ;; difference between the ranges of read articles in this group and
6228 ;; the range of active articles.
6229 (defun gnus-list-of-unread-articles (group)
6230   (let* ((read (gnus-info-read (gnus-get-info group)))
6231          (active (or (gnus-active group) (gnus-activate-group group)))
6232          (last (cdr active))
6233          first nlast unread)
6234     ;; If none are read, then all are unread.
6235     (if (not read)
6236         (setq first (car active))
6237       ;; If the range of read articles is a single range, then the
6238       ;; first unread article is the article after the last read
6239       ;; article.  Sounds logical, doesn't it?
6240       (if (and (not (listp (cdr read)))
6241                (or (< (car read) (car active))
6242                    (progn (setq read (list read))
6243                           nil)))
6244           (setq first (max (car active) (1+ (cdr read))))
6245         ;; `read' is a list of ranges.
6246         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6247                                   (caar read)))
6248                   1)
6249           (setq first (car active)))
6250         (while read
6251           (when first
6252             (while (< first nlast)
6253               (push first unread)
6254               (setq first (1+ first))))
6255           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6256           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6257           (setq read (cdr read)))))
6258     ;; And add the last unread articles.
6259     (while (<= first last)
6260       (push first unread)
6261       (setq first (1+ first)))
6262     ;; Return the list of unread articles.
6263     (delq 0 (nreverse unread))))
6264
6265 (defun gnus-list-of-read-articles (group)
6266   "Return a list of unread, unticked and non-dormant articles."
6267   (let* ((info (gnus-get-info group))
6268          (marked (gnus-info-marks info))
6269          (active (gnus-active group)))
6270     (and info active
6271          (gnus-list-range-difference
6272           (gnus-list-range-difference
6273            (gnus-sorted-complement
6274             (gnus-uncompress-range active)
6275             (gnus-list-of-unread-articles group))
6276            (cdr (assq 'dormant marked)))
6277           (cdr (assq 'tick marked))))))
6278
6279 ;; Various summary commands
6280
6281 (defun gnus-summary-select-article-buffer ()
6282   "Reconfigure windows to show article buffer."
6283   (interactive)
6284   (if (not (gnus-buffer-live-p gnus-article-buffer))
6285       (error "There is no article buffer for this summary buffer")
6286     (gnus-configure-windows 'article)
6287     (select-window (get-buffer-window gnus-article-buffer))))
6288
6289 (defun gnus-summary-universal-argument (arg)
6290   "Perform any operation on all articles that are process/prefixed."
6291   (interactive "P")
6292   (let ((articles (gnus-summary-work-articles arg))
6293         func article)
6294     (if (eq
6295          (setq
6296           func
6297           (key-binding
6298            (read-key-sequence
6299             (substitute-command-keys
6300              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6301          'undefined)
6302         (gnus-error 1 "Undefined key")
6303       (save-excursion
6304         (while articles
6305           (gnus-summary-goto-subject (setq article (pop articles)))
6306           (let (gnus-newsgroup-processable)
6307             (command-execute func))
6308           (gnus-summary-remove-process-mark article)))))
6309   (gnus-summary-position-point))
6310
6311 (defun gnus-summary-toggle-truncation (&optional arg)
6312   "Toggle truncation of summary lines.
6313 With arg, turn line truncation on if arg is positive."
6314   (interactive "P")
6315   (setq truncate-lines
6316         (if (null arg) (not truncate-lines)
6317           (> (prefix-numeric-value arg) 0)))
6318   (redraw-display))
6319
6320 (defun gnus-summary-find-uncancelled ()
6321   "Return the number of an uncancelled article.
6322 The current article is considered, then following articles, then previous
6323 articles.  If all articles are cancelled then return a dummy 0."
6324   (let (found)
6325     (dolist (rev '(nil t))
6326       (unless found      ; don't demand the reverse list if we don't need it
6327         (let ((data (gnus-data-find-list
6328                      (gnus-summary-article-number) (gnus-data-list rev))))
6329           (while (and data (not found))
6330             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6331                 (setq found (gnus-data-number (car data))))
6332             (setq data (cdr data))))))
6333     (or found 0)))
6334
6335 (defun gnus-summary-reselect-current-group (&optional all rescan)
6336   "Exit and then reselect the current newsgroup.
6337 The prefix argument ALL means to select all articles."
6338   (interactive "P")
6339   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6340     (error "Ephemeral groups can't be reselected"))
6341   (let ((current-subject (gnus-summary-find-uncancelled))
6342         (group gnus-newsgroup-name))
6343     (setq gnus-newsgroup-begin nil)
6344     (gnus-summary-exit)
6345     ;; We have to adjust the point of group mode buffer because
6346     ;; point was moved to the next unread newsgroup by exiting.
6347     (gnus-summary-jump-to-group group)
6348     (when rescan
6349       (save-excursion
6350         (save-window-excursion
6351           ;; Don't show group contents.
6352           (set-window-start (selected-window) (point-max))
6353           (gnus-group-get-new-news-this-group 1))))
6354     (gnus-group-read-group all t)
6355     (gnus-summary-goto-subject current-subject nil t)))
6356
6357 (defun gnus-summary-rescan-group (&optional all)
6358   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6359   (interactive "P")
6360   (gnus-summary-reselect-current-group all t))
6361
6362 (defun gnus-summary-update-info (&optional non-destructive)
6363   (save-excursion
6364     (let ((group gnus-newsgroup-name))
6365       (when group
6366         (when gnus-newsgroup-kill-headers
6367           (setq gnus-newsgroup-killed
6368                 (gnus-compress-sequence
6369                  (gnus-sorted-union
6370                   (gnus-list-range-intersection
6371                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6372                   gnus-newsgroup-unreads)
6373                  t)))
6374         (unless (listp (cdr gnus-newsgroup-killed))
6375           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6376         (let ((headers gnus-newsgroup-headers))
6377           ;; Set the new ranges of read articles.
6378           (save-excursion
6379             (set-buffer gnus-group-buffer)
6380             (gnus-undo-force-boundary))
6381           (gnus-update-read-articles
6382            group (gnus-sorted-union
6383                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6384           ;; Set the current article marks.
6385           (let ((gnus-newsgroup-scored
6386                  (if (and (not gnus-save-score)
6387                           (not non-destructive))
6388                      nil
6389                    gnus-newsgroup-scored)))
6390             (save-excursion
6391               (gnus-update-marks)))
6392           ;; Do the cross-ref thing.
6393           (when gnus-use-cross-reference
6394             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6395           ;; Do not switch windows but change the buffer to work.
6396           (set-buffer gnus-group-buffer)
6397           (unless (gnus-ephemeral-group-p group)
6398             (gnus-group-update-group group)))))))
6399
6400 (defun gnus-summary-save-newsrc (&optional force)
6401   "Save the current number of read/marked articles in the dribble buffer.
6402 The dribble buffer will then be saved.
6403 If FORCE (the prefix), also save the .newsrc file(s)."
6404   (interactive "P")
6405   (gnus-summary-update-info t)
6406   (if force
6407       (gnus-save-newsrc-file)
6408     (gnus-dribble-save)))
6409
6410 (defun gnus-summary-exit (&optional temporary)
6411   "Exit reading current newsgroup, and then return to group selection mode.
6412 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6413   (interactive)
6414   (gnus-set-global-variables)
6415   (gnus-kill-save-kill-buffer)
6416   (gnus-async-halt-prefetch)
6417   (let* ((group gnus-newsgroup-name)
6418          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6419          (gnus-group-is-exiting-p t)
6420          (mode major-mode)
6421          (group-point nil)
6422          (buf (current-buffer)))
6423     (unless quit-config
6424       ;; Do adaptive scoring, and possibly save score files.
6425       (when gnus-newsgroup-adaptive
6426         (gnus-score-adaptive))
6427       (when gnus-use-scoring
6428         (gnus-score-save)))
6429     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6430     ;; If we have several article buffers, we kill them at exit.
6431     (unless gnus-single-article-buffer
6432       (gnus-kill-buffer gnus-original-article-buffer)
6433       (setq gnus-article-current nil))
6434     (when gnus-use-cache
6435       (gnus-cache-possibly-remove-articles)
6436       (gnus-cache-save-buffers))
6437     (gnus-async-prefetch-remove-group group)
6438     (when gnus-suppress-duplicates
6439       (gnus-dup-enter-articles))
6440     (when gnus-use-trees
6441       (gnus-tree-close group))
6442     (when gnus-use-cache
6443       (gnus-cache-write-active))
6444     ;; Remove entries for this group.
6445     (nnmail-purge-split-history (gnus-group-real-name group))
6446     ;; Make all changes in this group permanent.
6447     (unless quit-config
6448       (gnus-run-hooks 'gnus-exit-group-hook)
6449       (gnus-summary-update-info))
6450     (gnus-close-group group)
6451     ;; Make sure where we were, and go to next newsgroup.
6452     (set-buffer gnus-group-buffer)
6453     (unless quit-config
6454       (gnus-group-jump-to-group group))
6455     (gnus-run-hooks 'gnus-summary-exit-hook)
6456     (unless (or quit-config
6457                 ;; If this group has disappeared from the summary
6458                 ;; buffer, don't skip forwards.
6459                 (not (string= group (gnus-group-group-name))))
6460       (gnus-group-next-unread-group 1))
6461     (setq group-point (point))
6462     (if temporary
6463         nil                             ;Nothing to do.
6464       ;; If we have several article buffers, we kill them at exit.
6465       (unless gnus-single-article-buffer
6466         (gnus-kill-buffer gnus-article-buffer)
6467         (gnus-kill-buffer gnus-original-article-buffer)
6468         (setq gnus-article-current nil))
6469       (set-buffer buf)
6470       (if (not gnus-kill-summary-on-exit)
6471           (progn
6472             (gnus-deaden-summary)
6473             (setq mode nil))
6474         ;; We set all buffer-local variables to nil.  It is unclear why
6475         ;; this is needed, but if we don't, buffer-local variables are
6476         ;; not garbage-collected, it seems.  This would the lead to en
6477         ;; ever-growing Emacs.
6478         (gnus-summary-clear-local-variables)
6479         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6480           (gnus-summary-clear-local-variables))
6481         (when (get-buffer gnus-article-buffer)
6482           (bury-buffer gnus-article-buffer))
6483         ;; We clear the global counterparts of the buffer-local
6484         ;; variables as well, just to be on the safe side.
6485         (set-buffer gnus-group-buffer)
6486         (gnus-summary-clear-local-variables)
6487         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6488           (gnus-summary-clear-local-variables))
6489         ;; Return to group mode buffer.
6490         (when (eq mode 'gnus-summary-mode)
6491           (gnus-kill-buffer buf)))
6492       (setq gnus-current-select-method gnus-select-method)
6493       (pop-to-buffer gnus-group-buffer)
6494       (if (not quit-config)
6495           (progn
6496             (goto-char group-point)
6497             (gnus-configure-windows 'group 'force)
6498             (unless (pos-visible-in-window-p)
6499               (forward-line (/ (static-if (featurep 'xemacs)
6500                                    (window-displayed-height)
6501                                  (1- (window-height)))
6502                                -2))
6503               (set-window-start (selected-window) (point))
6504               (goto-char group-point)))
6505         (gnus-handle-ephemeral-exit quit-config))
6506       ;; Clear the current group name.
6507       (unless quit-config
6508         (setq gnus-newsgroup-name nil)))))
6509
6510 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6511 (defun gnus-summary-exit-no-update (&optional no-questions)
6512   "Quit reading current newsgroup without updating read article info."
6513   (interactive)
6514   (let* ((group gnus-newsgroup-name)
6515          (gnus-group-is-exiting-p t)
6516          (gnus-group-is-exiting-without-update-p t)
6517          (quit-config (gnus-group-quit-config group)))
6518     (when (or no-questions
6519               gnus-expert-user
6520               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6521       (gnus-async-halt-prefetch)
6522       (run-hooks 'gnus-summary-prepare-exit-hook)
6523       ;; If we have several article buffers, we kill them at exit.
6524       (unless gnus-single-article-buffer
6525         (gnus-kill-buffer gnus-article-buffer)
6526         (gnus-kill-buffer gnus-original-article-buffer)
6527         (setq gnus-article-current nil))
6528       (if (not gnus-kill-summary-on-exit)
6529           (gnus-deaden-summary)
6530         (gnus-close-group group)
6531         (gnus-summary-clear-local-variables)
6532         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6533           (gnus-summary-clear-local-variables))
6534         (set-buffer gnus-group-buffer)
6535         (gnus-summary-clear-local-variables)
6536         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6537           (gnus-summary-clear-local-variables))
6538         (gnus-kill-buffer gnus-summary-buffer))
6539       (unless gnus-single-article-buffer
6540         (setq gnus-article-current nil))
6541       (when gnus-use-trees
6542         (gnus-tree-close group))
6543       (gnus-async-prefetch-remove-group group)
6544       (when (get-buffer gnus-article-buffer)
6545         (bury-buffer gnus-article-buffer))
6546       ;; Return to the group buffer.
6547       (gnus-configure-windows 'group 'force)
6548       ;; Clear the current group name.
6549       (setq gnus-newsgroup-name nil)
6550       (unless (gnus-ephemeral-group-p group)
6551         (gnus-group-update-group group))
6552       (when (equal (gnus-group-group-name) group)
6553         (gnus-group-next-unread-group 1))
6554       (when quit-config
6555         (gnus-handle-ephemeral-exit quit-config)))))
6556
6557 (defun gnus-handle-ephemeral-exit (quit-config)
6558   "Handle movement when leaving an ephemeral group.
6559 The state which existed when entering the ephemeral is reset."
6560   (if (not (buffer-name (car quit-config)))
6561       (gnus-configure-windows 'group 'force)
6562     (set-buffer (car quit-config))
6563     (cond ((eq major-mode 'gnus-summary-mode)
6564            (gnus-set-global-variables))
6565           ((eq major-mode 'gnus-article-mode)
6566            (save-excursion
6567              ;; The `gnus-summary-buffer' variable may point
6568              ;; to the old summary buffer when using a single
6569              ;; article buffer.
6570              (unless (gnus-buffer-live-p gnus-summary-buffer)
6571                (set-buffer gnus-group-buffer))
6572              (set-buffer gnus-summary-buffer)
6573              (gnus-set-global-variables))))
6574     (if (or (eq (cdr quit-config) 'article)
6575             (eq (cdr quit-config) 'pick))
6576         (progn
6577           ;; The current article may be from the ephemeral group
6578           ;; thus it is best that we reload this article
6579           (gnus-summary-show-article)
6580           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6581               (gnus-configure-windows 'pick 'force)
6582             (gnus-configure-windows (cdr quit-config) 'force)))
6583       (gnus-configure-windows (cdr quit-config) 'force))
6584     (when (eq major-mode 'gnus-summary-mode)
6585       (gnus-summary-next-subject 1 nil t)
6586       (gnus-summary-recenter)
6587       (gnus-summary-position-point))))
6588
6589 (defun gnus-summary-preview-mime-message ()
6590   "MIME decode and play this message."
6591   (interactive)
6592   (let ((gnus-break-pages nil)
6593         (gnus-show-mime t))
6594     (gnus-summary-select-article gnus-show-all-headers t))
6595   (let ((w (get-buffer-window gnus-article-buffer)))
6596     (when w
6597       (select-window (get-buffer-window gnus-article-buffer)))))
6598
6599 ;;; Dead summaries.
6600
6601 (defvar gnus-dead-summary-mode-map nil)
6602
6603 (unless gnus-dead-summary-mode-map
6604   (setq gnus-dead-summary-mode-map (make-keymap))
6605   (suppress-keymap gnus-dead-summary-mode-map)
6606   (substitute-key-definition
6607    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6608   (dolist (key '("\C-d" "\r" "\177" [delete]))
6609     (define-key gnus-dead-summary-mode-map
6610       key 'gnus-summary-wake-up-the-dead))
6611   (dolist (key '("q" "Q"))
6612     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6613
6614 (defvar gnus-dead-summary-mode nil
6615   "Minor mode for Gnus summary buffers.")
6616
6617 (defun gnus-dead-summary-mode (&optional arg)
6618   "Minor mode for Gnus summary buffers."
6619   (interactive "P")
6620   (when (eq major-mode 'gnus-summary-mode)
6621     (make-local-variable 'gnus-dead-summary-mode)
6622     (setq gnus-dead-summary-mode
6623           (if (null arg) (not gnus-dead-summary-mode)
6624             (> (prefix-numeric-value arg) 0)))
6625     (when gnus-dead-summary-mode
6626       (gnus-add-minor-mode
6627        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6628
6629 (defun gnus-deaden-summary ()
6630   "Make the current summary buffer into a dead summary buffer."
6631   ;; Kill any previous dead summary buffer.
6632   (when (and gnus-dead-summary
6633              (buffer-name gnus-dead-summary))
6634     (save-excursion
6635       (set-buffer gnus-dead-summary)
6636       (when gnus-dead-summary-mode
6637         (kill-buffer (current-buffer)))))
6638   ;; Make this the current dead summary.
6639   (setq gnus-dead-summary (current-buffer))
6640   (gnus-dead-summary-mode 1)
6641   (let ((name (buffer-name)))
6642     (when (string-match "Summary" name)
6643       (rename-buffer
6644        (concat (substring name 0 (match-beginning 0)) "Dead "
6645                (substring name (match-beginning 0)))
6646        t)
6647       (bury-buffer))))
6648
6649 (defun gnus-kill-or-deaden-summary (buffer)
6650   "Kill or deaden the summary BUFFER."
6651   (save-excursion
6652     (when (and (buffer-name buffer)
6653                (not gnus-single-article-buffer))
6654       (save-excursion
6655         (set-buffer buffer)
6656         (gnus-kill-buffer gnus-article-buffer)
6657         (gnus-kill-buffer gnus-original-article-buffer)))
6658     (cond
6659      ;; Kill the buffer.
6660      (gnus-kill-summary-on-exit
6661       (when (and gnus-use-trees
6662                  (gnus-buffer-exists-p buffer))
6663         (save-excursion
6664           (set-buffer buffer)
6665           (gnus-tree-close gnus-newsgroup-name)))
6666       (gnus-kill-buffer buffer))
6667      ;; Deaden the buffer.
6668      ((gnus-buffer-exists-p buffer)
6669       (save-excursion
6670         (set-buffer buffer)
6671         (gnus-deaden-summary))))))
6672
6673 (defun gnus-summary-wake-up-the-dead (&rest args)
6674   "Wake up the dead summary buffer."
6675   (interactive)
6676   (gnus-dead-summary-mode -1)
6677   (let ((name (buffer-name)))
6678     (when (string-match "Dead " name)
6679       (rename-buffer
6680        (concat (substring name 0 (match-beginning 0))
6681                (substring name (match-end 0)))
6682        t)))
6683   (gnus-message 3 "This dead summary is now alive again"))
6684
6685 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6686 (defun gnus-summary-fetch-faq (&optional faq-dir)
6687   "Fetch the FAQ for the current group.
6688 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6689 in."
6690   (interactive
6691    (list
6692     (when current-prefix-arg
6693       (completing-read
6694        "FAQ dir: " (and (listp gnus-group-faq-directory)
6695                         (mapcar (lambda (file) (list file))
6696                                 gnus-group-faq-directory))))))
6697   (let (gnus-faq-buffer)
6698     (when (setq gnus-faq-buffer
6699                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6700       (gnus-configure-windows 'summary-faq))))
6701
6702 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6703 (defun gnus-summary-describe-group (&optional force)
6704   "Describe the current newsgroup."
6705   (interactive "P")
6706   (gnus-group-describe-group force gnus-newsgroup-name))
6707
6708 (defun gnus-summary-describe-briefly ()
6709   "Describe summary mode commands briefly."
6710   (interactive)
6711   (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")))
6712
6713 ;; Walking around group mode buffer from summary mode.
6714
6715 (defun gnus-summary-next-group (&optional no-article target-group backward)
6716   "Exit current newsgroup and then select next unread newsgroup.
6717 If prefix argument NO-ARTICLE is non-nil, no article is selected
6718 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6719 previous group instead."
6720   (interactive "P")
6721   ;; Stop pre-fetching.
6722   (gnus-async-halt-prefetch)
6723   (let ((current-group gnus-newsgroup-name)
6724         (current-buffer (current-buffer))
6725         entered)
6726     ;; First we semi-exit this group to update Xrefs and all variables.
6727     ;; We can't do a real exit, because the window conf must remain
6728     ;; the same in case the user is prompted for info, and we don't
6729     ;; want the window conf to change before that...
6730     (gnus-summary-exit t)
6731     (while (not entered)
6732       ;; Then we find what group we are supposed to enter.
6733       (set-buffer gnus-group-buffer)
6734       (gnus-group-jump-to-group current-group)
6735       (setq target-group
6736             (or target-group
6737                 (if (eq gnus-keep-same-level 'best)
6738                     (gnus-summary-best-group gnus-newsgroup-name)
6739                   (gnus-summary-search-group backward gnus-keep-same-level))))
6740       (if (not target-group)
6741           ;; There are no further groups, so we return to the group
6742           ;; buffer.
6743           (progn
6744             (gnus-message 5 "Returning to the group buffer")
6745             (setq entered t)
6746             (when (gnus-buffer-live-p current-buffer)
6747               (set-buffer current-buffer)
6748               (gnus-summary-exit))
6749             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6750         ;; We try to enter the target group.
6751         (gnus-group-jump-to-group target-group)
6752         (let ((unreads (gnus-group-group-unread)))
6753           (if (and (or (eq t unreads)
6754                        (and unreads (not (zerop unreads))))
6755                    (gnus-summary-read-group
6756                     target-group nil no-article
6757                     (and (buffer-name current-buffer) current-buffer)
6758                     nil backward))
6759               (setq entered t)
6760             (setq current-group target-group
6761                   target-group nil)))))))
6762
6763 (defun gnus-summary-prev-group (&optional no-article)
6764   "Exit current newsgroup and then select previous unread newsgroup.
6765 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6766   (interactive "P")
6767   (gnus-summary-next-group no-article nil t))
6768
6769 ;; Walking around summary lines.
6770
6771 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6772   "Go to the first subject satisfying any non-nil constraint.
6773 If UNREAD is non-nil, the article should be unread.
6774 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6775 If UNSEED is non-nil, the article should be unseen.
6776 Returns the article selected or nil if there are no matching articles."
6777   (interactive "P")
6778   (cond
6779    ;; Empty summary.
6780    ((null gnus-newsgroup-data)
6781     (gnus-message 3 "No articles in the group")
6782     nil)
6783    ;; Pick the first article.
6784    ((not (or unread undownloaded unseen))
6785     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6786     (gnus-data-number (car gnus-newsgroup-data)))
6787    ;; Find the first unread article.
6788    (t
6789     (let ((data gnus-newsgroup-data))
6790       (while (and data
6791                   (let ((num (gnus-data-number (car data))))
6792                     (or (memq num gnus-newsgroup-unfetched)
6793                         (not (or (and unread
6794                                       (memq num gnus-newsgroup-unreads))
6795                                  (and undownloaded
6796                                       (memq num gnus-newsgroup-undownloaded))
6797                                  (and unseen
6798                                       (memq num gnus-newsgroup-unseen)))))))
6799         (setq data (cdr data)))
6800       (prog1 
6801           (if data
6802               (progn
6803                 (goto-char (gnus-data-pos (car data)))
6804                 (gnus-data-number (car data)))
6805             (gnus-message 3 "No more%s articles"
6806                           (let* ((r (when unread " unread"))
6807                                  (d (when undownloaded " undownloaded"))
6808                                  (s (when unseen " unseen"))
6809                                  (l (delq nil (list r d s))))
6810                             (cond ((= 3 (length l))
6811                                    (concat r "," d ", or" s))
6812                                   ((= 2 (length l))
6813                                    (concat (car l) ", or" (cadr l)))
6814                                   ((= 1 (length l))
6815                                    (car l))
6816                                   (t
6817                                    ""))))
6818             nil
6819             )
6820         (gnus-summary-position-point))))))
6821
6822 (defun gnus-summary-next-subject (n &optional unread dont-display)
6823   "Go to next N'th summary line.
6824 If N is negative, go to the previous N'th subject line.
6825 If UNREAD is non-nil, only unread articles are selected.
6826 The difference between N and the actual number of steps taken is
6827 returned."
6828   (interactive "p")
6829   (let ((backward (< n 0))
6830         (n (abs n)))
6831     (while (and (> n 0)
6832                 (if backward
6833                     (gnus-summary-find-prev unread)
6834                   (gnus-summary-find-next unread)))
6835       (unless (zerop (setq n (1- n)))
6836         (gnus-summary-show-thread)))
6837     (when (/= 0 n)
6838       (gnus-message 7 "No more%s articles"
6839                     (if unread " unread" "")))
6840     (unless dont-display
6841       (gnus-summary-recenter)
6842       (gnus-summary-position-point))
6843     n))
6844
6845 (defun gnus-summary-next-unread-subject (n)
6846   "Go to next N'th unread summary line."
6847   (interactive "p")
6848   (gnus-summary-next-subject n t))
6849
6850 (defun gnus-summary-prev-subject (n &optional unread)
6851   "Go to previous N'th summary line.
6852 If optional argument UNREAD is non-nil, only unread article is selected."
6853   (interactive "p")
6854   (gnus-summary-next-subject (- n) unread))
6855
6856 (defun gnus-summary-prev-unread-subject (n)
6857   "Go to previous N'th unread summary line."
6858   (interactive "p")
6859   (gnus-summary-next-subject (- n) t))
6860
6861 (defun gnus-summary-goto-subjects (articles)
6862   "Insert the subject header for ARTICLES in the current buffer."
6863   (save-excursion
6864     (dolist (article articles)
6865       (gnus-summary-goto-subject article t)))
6866   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6867   (gnus-summary-position-point))
6868  
6869 (defun gnus-summary-goto-subject (article &optional force silent)
6870   "Go the subject line of ARTICLE.
6871 If FORCE, also allow jumping to articles not currently shown."
6872   (interactive "nArticle number: ")
6873   (unless (numberp article)
6874     (error "Article %s is not a number" article))
6875   (let ((b (point))
6876         (data (gnus-data-find article)))
6877     ;; We read in the article if we have to.
6878     (and (not data)
6879          force
6880          (gnus-summary-insert-subject
6881           article
6882           (if (or (numberp force) (vectorp force)) force)
6883           t)
6884          (setq data (gnus-data-find article)))
6885     (goto-char b)
6886     (if (not data)
6887         (progn
6888           (unless silent
6889             (gnus-message 3 "Can't find article %d" article))
6890           nil)
6891       (let ((pt (gnus-data-pos data)))
6892         (goto-char pt)
6893         (gnus-summary-set-article-display-arrow pt))
6894       (gnus-summary-position-point)
6895       article)))
6896
6897 ;; Walking around summary lines with displaying articles.
6898
6899 (defun gnus-summary-expand-window (&optional arg)
6900   "Make the summary buffer take up the entire Emacs frame.
6901 Given a prefix, will force an `article' buffer configuration."
6902   (interactive "P")
6903   (if arg
6904       (gnus-configure-windows 'article 'force)
6905     (gnus-configure-windows 'summary 'force)))
6906
6907 (defun gnus-summary-display-article (article &optional all-header)
6908   "Display ARTICLE in article buffer."
6909   (when (gnus-buffer-live-p gnus-article-buffer)
6910     (with-current-buffer gnus-article-buffer
6911       (set-buffer-multibyte t)))
6912   (gnus-set-global-variables)
6913   (when (gnus-buffer-live-p gnus-article-buffer)
6914     (with-current-buffer gnus-article-buffer
6915       (setq gnus-article-charset gnus-newsgroup-charset)
6916       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6917       (set-buffer-multibyte t)))
6918   (if (null article)
6919       nil
6920     (prog1
6921         (if gnus-summary-display-article-function
6922             (funcall gnus-summary-display-article-function article all-header)
6923           (gnus-article-prepare article all-header))
6924       (with-current-buffer gnus-article-buffer
6925         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6926              nil))
6927       (gnus-run-hooks 'gnus-select-article-hook)
6928       (when (and gnus-current-article
6929                  (not (zerop gnus-current-article)))
6930         (gnus-summary-goto-subject gnus-current-article))
6931       (gnus-summary-recenter)
6932       (when (and gnus-use-trees gnus-show-threads)
6933         (gnus-possibly-generate-tree article)
6934         (gnus-highlight-selected-tree article))
6935       ;; Successfully display article.
6936       (gnus-article-set-window-start
6937        (cdr (assq article gnus-newsgroup-bookmarks))))))
6938
6939 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6940   "Select the current article.
6941 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6942 non-nil, the article will be re-fetched even if it already present in
6943 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6944 be displayed."
6945   ;; Make sure we are in the summary buffer to work around bbdb bug.
6946   (unless (eq major-mode 'gnus-summary-mode)
6947     (set-buffer gnus-summary-buffer))
6948   (let ((article (or article (gnus-summary-article-number)))
6949         (all-headers (not (not all-headers))) ;Must be T or NIL.
6950         gnus-summary-display-article-function)
6951     (and (not pseudo)
6952          (gnus-summary-article-pseudo-p article)
6953          (error "This is a pseudo-article"))
6954     (save-excursion
6955       (set-buffer gnus-summary-buffer)
6956       (if (or (and gnus-single-article-buffer
6957                    (or (null gnus-current-article)
6958                        (null gnus-article-current)
6959                        (null (get-buffer gnus-article-buffer))
6960                        (not (eq article (cdr gnus-article-current)))
6961                        (not (equal (car gnus-article-current)
6962                                    gnus-newsgroup-name))))
6963               (and (not gnus-single-article-buffer)
6964                    (or (null gnus-current-article)
6965                        (not (eq gnus-current-article article))))
6966               force)
6967           ;; The requested article is different from the current article.
6968           (progn
6969             (gnus-summary-display-article article all-headers)
6970             (gnus-article-set-window-start
6971              (cdr (assq article gnus-newsgroup-bookmarks)))
6972             article)
6973         'old))))
6974
6975 (defun gnus-summary-force-verify-and-decrypt ()
6976   "Display buttons for signed/encrypted parts and verify/decrypt them."
6977   (interactive)
6978   (let ((mm-verify-option 'known)
6979         (mm-decrypt-option 'known)
6980         (gnus-buttonized-mime-types (append (list "multipart/signed"
6981                                                   "multipart/encrypted")
6982                                             gnus-buttonized-mime-types)))
6983     (gnus-summary-select-article nil 'force)))
6984
6985 (defun gnus-summary-set-current-mark (&optional current-mark)
6986   "Obsolete function."
6987   nil)
6988
6989 (defun gnus-summary-next-article (&optional unread subject backward push)
6990   "Select the next article.
6991 If UNREAD, only unread articles are selected.
6992 If SUBJECT, only articles with SUBJECT are selected.
6993 If BACKWARD, the previous article is selected instead of the next."
6994   (interactive "P")
6995   (cond
6996    ;; Is there such an article?
6997    ((and (gnus-summary-search-forward unread subject backward)
6998          (or (gnus-summary-display-article (gnus-summary-article-number))
6999              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7000     (gnus-summary-position-point))
7001    ;; If not, we try the first unread, if that is wanted.
7002    ((and subject
7003          gnus-auto-select-same
7004          (gnus-summary-first-unread-article))
7005     (gnus-summary-position-point)
7006     (gnus-message 6 "Wrapped"))
7007    ;; Try to get next/previous article not displayed in this group.
7008    ((and gnus-auto-extend-newsgroup
7009          (not unread) (not subject))
7010     (gnus-summary-goto-article
7011      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7012      nil (count-lines (point-min) (point))))
7013    ;; Go to next/previous group.
7014    (t
7015     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7016       (gnus-summary-jump-to-group gnus-newsgroup-name))
7017     (let ((cmd last-command-char)
7018           (point
7019            (save-excursion
7020              (set-buffer gnus-group-buffer)
7021              (point)))
7022           (group
7023            (if (eq gnus-keep-same-level 'best)
7024                (gnus-summary-best-group gnus-newsgroup-name)
7025              (gnus-summary-search-group backward gnus-keep-same-level))))
7026       ;; For some reason, the group window gets selected.  We change
7027       ;; it back.
7028       (select-window (get-buffer-window (current-buffer)))
7029       ;; Select next unread newsgroup automagically.
7030       (cond
7031        ((or (not gnus-auto-select-next)
7032             (not cmd))
7033         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7034        ((or (eq gnus-auto-select-next 'quietly)
7035             (and (eq gnus-auto-select-next 'slightly-quietly)
7036                  push)
7037             (and (eq gnus-auto-select-next 'almost-quietly)
7038                  (gnus-summary-last-article-p)))
7039         ;; Select quietly.
7040         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7041             (gnus-summary-exit)
7042           (gnus-message 7 "No more%s articles (%s)..."
7043                         (if unread " unread" "")
7044                         (if group (concat "selecting " group)
7045                           "exiting"))
7046           (gnus-summary-next-group nil group backward)))
7047        (t
7048         (when (gnus-key-press-event-p last-input-event)
7049           (gnus-summary-walk-group-buffer
7050            gnus-newsgroup-name cmd unread backward point))))))))
7051
7052 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7053   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7054                       (?\C-p (gnus-group-prev-unread-group 1))))
7055         (cursor-in-echo-area t)
7056         keve key group ended prompt)
7057     (save-excursion
7058       (set-buffer gnus-group-buffer)
7059       (goto-char start)
7060       (setq group
7061             (if (eq gnus-keep-same-level 'best)
7062                 (gnus-summary-best-group gnus-newsgroup-name)
7063               (gnus-summary-search-group backward gnus-keep-same-level))))
7064     (while (not ended)
7065       (setq prompt
7066             (format
7067              "No more%s articles%s " (if unread " unread" "")
7068              (if (and group
7069                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7070                  (format " (Type %s for %s [%s])"
7071                          (single-key-description cmd) group
7072                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7073                (format " (Type %s to exit %s)"
7074                        (single-key-description cmd)
7075                        gnus-newsgroup-name))))
7076       ;; Confirm auto selection.
7077       (setq key (car (setq keve (gnus-read-event-char prompt)))
7078             ended t)
7079       (cond
7080        ((assq key keystrokes)
7081         (let ((obuf (current-buffer)))
7082           (switch-to-buffer gnus-group-buffer)
7083           (when group
7084             (gnus-group-jump-to-group group))
7085           (eval (cadr (assq key keystrokes)))
7086           (setq group (gnus-group-group-name))
7087           (switch-to-buffer obuf))
7088         (setq ended nil))
7089        ((equal key cmd)
7090         (if (or (not group)
7091                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7092             (gnus-summary-exit)
7093           (gnus-summary-next-group nil group backward)))
7094        (t
7095         (push (cdr keve) unread-command-events))))))
7096
7097 (defun gnus-summary-next-unread-article ()
7098   "Select unread article after current one."
7099   (interactive)
7100   (gnus-summary-next-article
7101    (or (not (eq gnus-summary-goto-unread 'never))
7102        (gnus-summary-last-article-p (gnus-summary-article-number)))
7103    (and gnus-auto-select-same
7104         (gnus-summary-article-subject))))
7105
7106 (defun gnus-summary-prev-article (&optional unread subject)
7107   "Select the article after the current one.
7108 If UNREAD is non-nil, only unread articles are selected."
7109   (interactive "P")
7110   (gnus-summary-next-article unread subject t))
7111
7112 (defun gnus-summary-prev-unread-article ()
7113   "Select unread article before current one."
7114   (interactive)
7115   (gnus-summary-prev-article
7116    (or (not (eq gnus-summary-goto-unread 'never))
7117        (gnus-summary-first-article-p (gnus-summary-article-number)))
7118    (and gnus-auto-select-same
7119         (gnus-summary-article-subject))))
7120
7121 (defun gnus-summary-next-page (&optional lines circular stop)
7122   "Show next page of the selected article.
7123 If at the end of the current article, select the next article.
7124 LINES says how many lines should be scrolled up.
7125
7126 If CIRCULAR is non-nil, go to the start of the article instead of
7127 selecting the next article when reaching the end of the current
7128 article.
7129
7130 If STOP is non-nil, just stop when reaching the end of the message."
7131   (interactive "P")
7132   (setq gnus-summary-buffer (current-buffer))
7133   (gnus-set-global-variables)
7134   (let ((article (gnus-summary-article-number))
7135         (article-window (get-buffer-window gnus-article-buffer t))
7136         endp)
7137     ;; If the buffer is empty, we have no article.
7138     (unless article
7139       (error "No article to select"))
7140     (gnus-configure-windows 'article)
7141     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7142         (if (and (eq gnus-summary-goto-unread 'never)
7143                  (not (gnus-summary-last-article-p article)))
7144             (gnus-summary-next-article)
7145           (gnus-summary-next-unread-article))
7146       (if (or (null gnus-current-article)
7147               (null gnus-article-current)
7148               (/= article (cdr gnus-article-current))
7149               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7150           ;; Selected subject is different from current article's.
7151           (gnus-summary-display-article article)
7152         (when article-window
7153           (gnus-eval-in-buffer-window gnus-article-buffer
7154             (setq endp (or (gnus-article-next-page lines)
7155                            (gnus-article-only-boring-p))))
7156           (when endp
7157             (cond (stop
7158                    (gnus-message 3 "End of message"))
7159                   (circular
7160                    (gnus-summary-beginning-of-article))
7161                   (lines
7162                    (gnus-message 3 "End of message"))
7163                   ((null lines)
7164                    (if (and (eq gnus-summary-goto-unread 'never)
7165                             (not (gnus-summary-last-article-p article)))
7166                        (gnus-summary-next-article)
7167                      (gnus-summary-next-unread-article))))))))
7168     (gnus-summary-recenter)
7169     (gnus-summary-position-point)))
7170
7171 (defun gnus-summary-prev-page (&optional lines move)
7172   "Show previous page of selected article.
7173 Argument LINES specifies lines to be scrolled down.
7174 If MOVE, move to the previous unread article if point is at
7175 the beginning of the buffer."
7176   (interactive "P")
7177   (let ((article (gnus-summary-article-number))
7178         (article-window (get-buffer-window gnus-article-buffer t))
7179         endp)
7180     (gnus-configure-windows 'article)
7181     (if (or (null gnus-current-article)
7182             (null gnus-article-current)
7183             (/= article (cdr gnus-article-current))
7184             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7185         ;; Selected subject is different from current article's.
7186         (gnus-summary-display-article article)
7187       (gnus-summary-recenter)
7188       (when article-window
7189         (gnus-eval-in-buffer-window gnus-article-buffer
7190           (setq endp (gnus-article-prev-page lines)))
7191         (when (and move endp)
7192           (cond (lines
7193                  (gnus-message 3 "Beginning of message"))
7194                 ((null lines)
7195                  (if (and (eq gnus-summary-goto-unread 'never)
7196                           (not (gnus-summary-first-article-p article)))
7197                      (gnus-summary-prev-article)
7198                    (gnus-summary-prev-unread-article))))))))
7199   (gnus-summary-position-point))
7200
7201 (defun gnus-summary-prev-page-or-article (&optional lines)
7202   "Show previous page of selected article.
7203 Argument LINES specifies lines to be scrolled down.
7204 If at the beginning of the article, go to the next article."
7205   (interactive "P")
7206   (gnus-summary-prev-page lines t))
7207
7208 (defun gnus-summary-scroll-up (lines)
7209   "Scroll up (or down) one line current article.
7210 Argument LINES specifies lines to be scrolled up (or down if negative)."
7211   (interactive "p")
7212   (gnus-configure-windows 'article)
7213   (gnus-summary-show-thread)
7214   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7215     (gnus-eval-in-buffer-window gnus-article-buffer
7216       (cond ((> lines 0)
7217              (when (gnus-article-next-page lines)
7218                (gnus-message 3 "End of message")))
7219             ((< lines 0)
7220              (gnus-article-prev-page (- lines))))))
7221   (gnus-summary-recenter)
7222   (gnus-summary-position-point))
7223
7224 (defun gnus-summary-scroll-down (lines)
7225   "Scroll down (or up) one line current article.
7226 Argument LINES specifies lines to be scrolled down (or up if negative)."
7227   (interactive "p")
7228   (gnus-summary-scroll-up (- lines)))
7229
7230 (defun gnus-summary-next-same-subject ()
7231   "Select next article which has the same subject as current one."
7232   (interactive)
7233   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7234
7235 (defun gnus-summary-prev-same-subject ()
7236   "Select previous article which has the same subject as current one."
7237   (interactive)
7238   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7239
7240 (defun gnus-summary-next-unread-same-subject ()
7241   "Select next unread article which has the same subject as current one."
7242   (interactive)
7243   (gnus-summary-next-article t (gnus-summary-article-subject)))
7244
7245 (defun gnus-summary-prev-unread-same-subject ()
7246   "Select previous unread article which has the same subject as current one."
7247   (interactive)
7248   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7249
7250 (defun gnus-summary-first-unread-article ()
7251   "Select the first unread article.
7252 Return nil if there are no unread articles."
7253   (interactive)
7254   (prog1
7255       (when (gnus-summary-first-subject t)
7256         (gnus-summary-show-thread)
7257         (gnus-summary-first-subject t)
7258         (gnus-summary-display-article (gnus-summary-article-number)))
7259     (gnus-summary-position-point)))
7260
7261 (defun gnus-summary-first-unread-subject ()
7262   "Place the point on the subject line of the first unread article.
7263 Return nil if there are no unread articles."
7264   (interactive)
7265   (prog1
7266       (when (gnus-summary-first-subject t)
7267         (gnus-summary-show-thread)
7268         (gnus-summary-first-subject t))
7269     (gnus-summary-position-point)))
7270
7271 (defun gnus-summary-first-unseen-subject ()
7272   "Place the point on the subject line of the first unseen article.
7273 Return nil if there are no unseen articles."
7274   (interactive)
7275   (prog1
7276       (when (gnus-summary-first-subject nil nil t)
7277         (gnus-summary-show-thread)
7278         (gnus-summary-first-subject nil nil t))
7279     (gnus-summary-position-point)))
7280
7281 (defun gnus-summary-first-unseen-or-unread-subject ()
7282   "Place the point on the subject line of the first unseen article or,
7283 if all article have been seen, on the subject line of the first unread
7284 article."
7285   (interactive)
7286   (prog1
7287       (unless (when (gnus-summary-first-subject nil nil t)
7288                 (gnus-summary-show-thread)
7289                 (gnus-summary-first-subject nil nil t))
7290         (when (gnus-summary-first-subject t)
7291           (gnus-summary-show-thread)
7292           (gnus-summary-first-subject t)))
7293     (gnus-summary-position-point)))
7294
7295 (defun gnus-summary-first-article ()
7296   "Select the first article.
7297 Return nil if there are no articles."
7298   (interactive)
7299   (prog1
7300       (when (gnus-summary-first-subject)
7301         (gnus-summary-show-thread)
7302         (gnus-summary-first-subject)
7303         (gnus-summary-display-article (gnus-summary-article-number)))
7304     (gnus-summary-position-point)))
7305
7306 (defun gnus-summary-best-unread-article (&optional arg)
7307   "Select the unread article with the highest score.
7308 If given a prefix argument, select the next unread article that has a
7309 score higher than the default score."
7310   (interactive "P")
7311   (let ((article (if arg
7312                      (gnus-summary-better-unread-subject)
7313                    (gnus-summary-best-unread-subject))))
7314     (if article
7315         (gnus-summary-goto-article article)
7316       (error "No unread articles"))))
7317
7318 (defun gnus-summary-best-unread-subject ()
7319   "Select the unread subject with the highest score."
7320   (interactive)
7321   (let ((best -1000000)
7322         (data gnus-newsgroup-data)
7323         article score)
7324     (while data
7325       (and (gnus-data-unread-p (car data))
7326            (> (setq score
7327                     (gnus-summary-article-score (gnus-data-number (car data))))
7328               best)
7329            (setq best score
7330                  article (gnus-data-number (car data))))
7331       (setq data (cdr data)))
7332     (when article
7333       (gnus-summary-goto-subject article))
7334     (gnus-summary-position-point)
7335     article))
7336
7337 (defun gnus-summary-better-unread-subject ()
7338   "Select the first unread subject that has a score over the default score."
7339   (interactive)
7340   (let ((data gnus-newsgroup-data)
7341         article score)
7342     (while (and (setq article (gnus-data-number (car data)))
7343                 (or (gnus-data-read-p (car data))
7344                     (not (> (gnus-summary-article-score article)
7345                             gnus-summary-default-score))))
7346       (setq data (cdr data)))
7347     (when article
7348       (gnus-summary-goto-subject article))
7349     (gnus-summary-position-point)
7350     article))
7351
7352 (defun gnus-summary-last-subject ()
7353   "Go to the last displayed subject line in the group."
7354   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7355     (when article
7356       (gnus-summary-goto-subject article))))
7357
7358 (defun gnus-summary-goto-article (article &optional all-headers force)
7359   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7360 If ALL-HEADERS is non-nil, no header lines are hidden.
7361 If FORCE, go to the article even if it isn't displayed.  If FORCE
7362 is a number, it is the line the article is to be displayed on."
7363   (interactive
7364    (list
7365     (completing-read
7366      "Article number or Message-ID: "
7367      (mapcar (lambda (number) (list (int-to-string number)))
7368              gnus-newsgroup-limit))
7369     current-prefix-arg
7370     t))
7371   (prog1
7372       (if (and (stringp article)
7373                (string-match "@" article))
7374           (gnus-summary-refer-article article)
7375         (when (stringp article)
7376           (setq article (string-to-number article)))
7377         (if (gnus-summary-goto-subject article force)
7378             (gnus-summary-display-article article all-headers)
7379           (gnus-message 4 "Couldn't go to article %s" article) nil))
7380     (gnus-summary-position-point)))
7381
7382 (defun gnus-summary-goto-last-article ()
7383   "Go to the previously read article."
7384   (interactive)
7385   (prog1
7386       (when gnus-last-article
7387         (gnus-summary-goto-article gnus-last-article nil t))
7388     (gnus-summary-position-point)))
7389
7390 (defun gnus-summary-pop-article (number)
7391   "Pop one article off the history and go to the previous.
7392 NUMBER articles will be popped off."
7393   (interactive "p")
7394   (let (to)
7395     (setq gnus-newsgroup-history
7396           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7397     (if to
7398         (gnus-summary-goto-article (car to) nil t)
7399       (error "Article history empty")))
7400   (gnus-summary-position-point))
7401
7402 ;; Summary commands and functions for limiting the summary buffer.
7403
7404 (defun gnus-summary-limit-to-articles (n)
7405   "Limit the summary buffer to the next N articles.
7406 If not given a prefix, use the process marked articles instead."
7407   (interactive "P")
7408   (prog1
7409       (let ((articles (gnus-summary-work-articles n)))
7410         (setq gnus-newsgroup-processable nil)
7411         (gnus-summary-limit articles))
7412     (gnus-summary-position-point)))
7413
7414 (defun gnus-summary-pop-limit (&optional total)
7415   "Restore the previous limit.
7416 If given a prefix, remove all limits."
7417   (interactive "P")
7418   (when total
7419     (setq gnus-newsgroup-limits
7420           (list (mapcar (lambda (h) (mail-header-number h))
7421                         gnus-newsgroup-headers))))
7422   (unless gnus-newsgroup-limits
7423     (error "No limit to pop"))
7424   (prog1
7425       (gnus-summary-limit nil 'pop)
7426     (gnus-summary-position-point)))
7427
7428 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7429   "Limit the summary buffer to articles that have subjects that match a regexp.
7430 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7431   (interactive
7432    (list (read-string (if current-prefix-arg
7433                           "Exclude subject (regexp): "
7434                         "Limit to subject (regexp): "))
7435          nil current-prefix-arg))
7436   (unless header
7437     (setq header "subject"))
7438   (when (not (equal "" subject))
7439     (prog1
7440         (let ((articles (gnus-summary-find-matching
7441                          (or header "subject") subject 'all nil nil
7442                          not-matching)))
7443           (unless articles
7444             (error "Found no matches for \"%s\"" subject))
7445           (gnus-summary-limit articles))
7446       (gnus-summary-position-point))))
7447
7448 (defun gnus-summary-limit-to-author (from &optional not-matching)
7449   "Limit the summary buffer to articles that have authors that match a regexp.
7450 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7451   (interactive
7452    (list (read-string (if current-prefix-arg
7453                           "Exclude author (regexp): "
7454                         "Limit to author (regexp): "))
7455          current-prefix-arg))
7456   (gnus-summary-limit-to-subject from "from" not-matching))
7457
7458 (defun gnus-summary-limit-to-age (age &optional younger-p)
7459   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7460 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7461 articles that are younger than AGE days."
7462   (interactive
7463    (let ((younger current-prefix-arg)
7464          (days-got nil)
7465          days)
7466      (while (not days-got)
7467        (setq days (if younger
7468                       (read-string "Limit to articles younger than (in days, older when negative): ")
7469                     (read-string
7470                      "Limit to articles older than (in days, younger when negative): ")))
7471        (when (> (length days) 0)
7472          (setq days (read days)))
7473        (if (numberp days)
7474            (progn
7475              (setq days-got t)
7476              (if (< days 0)
7477                  (progn
7478                    (setq younger (not younger))
7479                    (setq days (* days -1)))))
7480          (message "Please enter a number.")
7481          (sleep-for 1)))
7482      (list days younger)))
7483   (prog1
7484       (let ((data gnus-newsgroup-data)
7485             (cutoff (days-to-time age))
7486             articles d date is-younger)
7487         (while (setq d (pop data))
7488           (when (and (vectorp (gnus-data-header d))
7489                      (setq date (mail-header-date (gnus-data-header d))))
7490             (setq is-younger (time-less-p
7491                               (time-since (condition-case ()
7492                                               (date-to-time date)
7493                                             (error '(0 0))))
7494                               cutoff))
7495             (when (if younger-p
7496                       is-younger
7497                     (not is-younger))
7498               (push (gnus-data-number d) articles))))
7499         (gnus-summary-limit (nreverse articles)))
7500     (gnus-summary-position-point)))
7501
7502 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7503   "Limit the summary buffer to articles that match an 'extra' header."
7504   (interactive
7505    (let ((header
7506           (intern
7507            (gnus-completing-read-with-default
7508             (symbol-name (car gnus-extra-headers))
7509             (if current-prefix-arg
7510                 "Exclude extra header:"
7511               "Limit extra header:")
7512             (mapcar (lambda (x)
7513                       (cons (symbol-name x) x))
7514                     gnus-extra-headers)
7515             nil
7516             t))))
7517      (list header
7518            (read-string (format "%s header %s (regexp): "
7519                                 (if current-prefix-arg "Exclude" "Limit to")
7520                                 header))
7521            current-prefix-arg)))
7522   (when (not (equal "" regexp))
7523     (prog1
7524         (let ((articles (gnus-summary-find-matching
7525                          (cons 'extra header) regexp 'all nil nil
7526                          not-matching)))
7527           (unless articles
7528             (error "Found no matches for \"%s\"" regexp))
7529           (gnus-summary-limit articles))
7530       (gnus-summary-position-point))))
7531
7532 (defun gnus-summary-limit-to-display-predicate ()
7533   "Limit the summary buffer to the predicated in the `display' group parameter."
7534   (interactive)
7535   (unless gnus-newsgroup-display
7536     (error "There is no `display' group parameter"))
7537   (let (articles)
7538     (dolist (number gnus-newsgroup-articles)
7539       (when (funcall gnus-newsgroup-display)
7540         (push number articles)))
7541     (gnus-summary-limit articles))
7542   (gnus-summary-position-point))
7543
7544 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7545 (make-obsolete
7546  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7547
7548 (defun gnus-summary-limit-to-unread (&optional all)
7549   "Limit the summary buffer to articles that are not marked as read.
7550 If ALL is non-nil, limit strictly to unread articles."
7551   (interactive "P")
7552   (if all
7553       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7554     (gnus-summary-limit-to-marks
7555      ;; Concat all the marks that say that an article is read and have
7556      ;; those removed.
7557      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7558            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7559            gnus-low-score-mark gnus-expirable-mark
7560            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7561            gnus-duplicate-mark gnus-souped-mark)
7562      'reverse)))
7563
7564 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7565 (make-obsolete 'gnus-summary-delete-marked-with
7566                'gnus-summary-limit-exclude-marks)
7567
7568 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7569   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7570 If REVERSE, limit the summary buffer to articles that are marked
7571 with MARKS.  MARKS can either be a string of marks or a list of marks.
7572 Returns how many articles were removed."
7573   (interactive "sMarks: ")
7574   (gnus-summary-limit-to-marks marks t))
7575
7576 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7577   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7578 If REVERSE (the prefix), limit the summary buffer to articles that are
7579 not marked with MARKS.  MARKS can either be a string of marks or a
7580 list of marks.
7581 Returns how many articles were removed."
7582   (interactive "sMarks: \nP")
7583   (prog1
7584       (let ((data gnus-newsgroup-data)
7585             (marks (if (listp marks) marks
7586                      (append marks nil))) ; Transform to list.
7587             articles)
7588         (while data
7589           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7590                   (memq (gnus-data-mark (car data)) marks))
7591             (push (gnus-data-number (car data)) articles))
7592           (setq data (cdr data)))
7593         (gnus-summary-limit articles))
7594     (gnus-summary-position-point)))
7595
7596 (defun gnus-summary-limit-to-score (score)
7597   "Limit to articles with score at or above SCORE."
7598   (interactive "NLimit to articles with score of at least: ")
7599   (let ((data gnus-newsgroup-data)
7600         articles)
7601     (while data
7602       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7603                 score)
7604         (push (gnus-data-number (car data)) articles))
7605       (setq data (cdr data)))
7606     (prog1
7607         (gnus-summary-limit articles)
7608       (gnus-summary-position-point))))
7609
7610 (defun gnus-summary-limit-to-unseen ()
7611   "Limit to unseen articles."
7612   (interactive)
7613   (prog1
7614       (gnus-summary-limit gnus-newsgroup-unseen)
7615     (gnus-summary-position-point)))
7616
7617 (defun gnus-summary-limit-include-thread (id)
7618   "Display all the hidden articles that is in the thread with ID in it.
7619 When called interactively, ID is the Message-ID of the current
7620 article."
7621   (interactive (list (mail-header-id (gnus-summary-article-header))))
7622   (let ((articles (gnus-articles-in-thread
7623                    (gnus-id-to-thread (gnus-root-id id)))))
7624     (prog1
7625         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7626       (gnus-summary-limit-include-matching-articles
7627        "subject"
7628        (regexp-quote (gnus-simplify-subject-re
7629                       (mail-header-subject (gnus-id-to-header id)))))
7630       (gnus-summary-position-point))))
7631
7632 (defun gnus-summary-limit-include-matching-articles (header regexp)
7633   "Display all the hidden articles that have HEADERs that match REGEXP."
7634   (interactive (list (read-string "Match on header: ")
7635                      (read-string "Regexp: ")))
7636   (let ((articles (gnus-find-matching-articles header regexp)))
7637     (prog1
7638         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7639       (gnus-summary-position-point))))
7640
7641 (defun gnus-summary-insert-dormant-articles ()
7642   "Insert all the dormat articles for this group into the current buffer."
7643   (interactive)
7644   (let ((gnus-verbose (max 6 gnus-verbose)))
7645     (if (not gnus-newsgroup-dormant)
7646         (gnus-message 3 "No cached articles for this group")
7647       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7648
7649 (defun gnus-summary-limit-include-dormant ()
7650   "Display all the hidden articles that are marked as dormant.
7651 Note that this command only works on a subset of the articles currently
7652 fetched for this group."
7653   (interactive)
7654   (unless gnus-newsgroup-dormant
7655     (error "There are no dormant articles in this group"))
7656   (prog1
7657       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7658     (gnus-summary-position-point)))
7659
7660 (defun gnus-summary-limit-exclude-dormant ()
7661   "Hide all dormant articles."
7662   (interactive)
7663   (prog1
7664       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7665     (gnus-summary-position-point)))
7666
7667 (defun gnus-summary-limit-exclude-childless-dormant ()
7668   "Hide all dormant articles that have no children."
7669   (interactive)
7670   (let ((data (gnus-data-list t))
7671         articles d children)
7672     ;; Find all articles that are either not dormant or have
7673     ;; children.
7674     (while (setq d (pop data))
7675       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7676                 (and (setq children
7677                            (gnus-article-children (gnus-data-number d)))
7678                      (let (found)
7679                        (while children
7680                          (when (memq (car children) articles)
7681                            (setq children nil
7682                                  found t))
7683                          (pop children))
7684                        found)))
7685         (push (gnus-data-number d) articles)))
7686     ;; Do the limiting.
7687     (prog1
7688         (gnus-summary-limit articles)
7689       (gnus-summary-position-point))))
7690
7691 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7692   "Mark all unread excluded articles as read.
7693 If ALL, mark even excluded ticked and dormants as read."
7694   (interactive "P")
7695   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7696   (let ((articles (gnus-sorted-ndifference
7697                    (sort
7698                     (mapcar (lambda (h) (mail-header-number h))
7699                             gnus-newsgroup-headers)
7700                     '<)
7701                    gnus-newsgroup-limit))
7702         article)
7703     (setq gnus-newsgroup-unreads
7704           (gnus-sorted-intersection gnus-newsgroup-unreads
7705                                     gnus-newsgroup-limit))
7706     (if all
7707         (setq gnus-newsgroup-dormant nil
7708               gnus-newsgroup-marked nil
7709               gnus-newsgroup-reads
7710               (nconc
7711                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7712                gnus-newsgroup-reads))
7713       (while (setq article (pop articles))
7714         (unless (or (memq article gnus-newsgroup-dormant)
7715                     (memq article gnus-newsgroup-marked))
7716           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7717
7718 (defun gnus-summary-limit (articles &optional pop)
7719   (if pop
7720       ;; We pop the previous limit off the stack and use that.
7721       (setq articles (car gnus-newsgroup-limits)
7722             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7723     ;; We use the new limit, so we push the old limit on the stack.
7724     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7725   ;; Set the limit.
7726   (setq gnus-newsgroup-limit articles)
7727   (let ((total (length gnus-newsgroup-data))
7728         (data (gnus-data-find-list (gnus-summary-article-number)))
7729         (gnus-summary-mark-below nil)   ; Inhibit this.
7730         found)
7731     ;; This will do all the work of generating the new summary buffer
7732     ;; according to the new limit.
7733     (gnus-summary-prepare)
7734     ;; Hide any threads, possibly.
7735     (gnus-summary-maybe-hide-threads)
7736     ;; Try to return to the article you were at, or one in the
7737     ;; neighborhood.
7738     (when data
7739       ;; We try to find some article after the current one.
7740       (while data
7741         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7742           (setq data nil
7743                 found t))
7744         (setq data (cdr data))))
7745     (unless found
7746       ;; If there is no data, that means that we were after the last
7747       ;; article.  The same goes when we can't find any articles
7748       ;; after the current one.
7749       (goto-char (point-max))
7750       (gnus-summary-find-prev))
7751     (gnus-set-mode-line 'summary)
7752     ;; We return how many articles were removed from the summary
7753     ;; buffer as a result of the new limit.
7754     (- total (length gnus-newsgroup-data))))
7755
7756 (defsubst gnus-invisible-cut-children (threads)
7757   (let ((num 0))
7758     (while threads
7759       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7760         (incf num))
7761       (pop threads))
7762     (< num 2)))
7763
7764 (defsubst gnus-cut-thread (thread)
7765   "Go forwards in the thread until we find an article that we want to display."
7766   (when (or (eq gnus-fetch-old-headers 'some)
7767             (eq gnus-fetch-old-headers 'invisible)
7768             (numberp gnus-fetch-old-headers)
7769             (eq gnus-build-sparse-threads 'some)
7770             (eq gnus-build-sparse-threads 'more))
7771     ;; Deal with old-fetched headers and sparse threads.
7772     (while (and
7773             thread
7774             (or
7775              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7776              (gnus-summary-article-ancient-p
7777               (mail-header-number (car thread))))
7778             (if (or (<= (length (cdr thread)) 1)
7779                     (eq gnus-fetch-old-headers 'invisible))
7780                 (setq gnus-newsgroup-limit
7781                       (delq (mail-header-number (car thread))
7782                             gnus-newsgroup-limit)
7783                       thread (cadr thread))
7784               (when (gnus-invisible-cut-children (cdr thread))
7785                 (let ((th (cdr thread)))
7786                   (while th
7787                     (if (memq (mail-header-number (caar th))
7788                               gnus-newsgroup-limit)
7789                         (setq thread (car th)
7790                               th nil)
7791                       (setq th (cdr th))))))))))
7792   thread)
7793
7794 (defun gnus-cut-threads (threads)
7795   "Cut off all uninteresting articles from the beginning of threads."
7796   (when (or (eq gnus-fetch-old-headers 'some)
7797             (eq gnus-fetch-old-headers 'invisible)
7798             (numberp gnus-fetch-old-headers)
7799             (eq gnus-build-sparse-threads 'some)
7800             (eq gnus-build-sparse-threads 'more))
7801     (let ((th threads))
7802       (while th
7803         (setcar th (gnus-cut-thread (car th)))
7804         (setq th (cdr th)))))
7805   ;; Remove nixed out threads.
7806   (delq nil threads))
7807
7808 (defun gnus-summary-initial-limit (&optional show-if-empty)
7809   "Figure out what the initial limit is supposed to be on group entry.
7810 This entails weeding out unwanted dormants, low-scored articles,
7811 fetch-old-headers verbiage, and so on."
7812   ;; Most groups have nothing to remove.
7813   (if (or gnus-inhibit-limiting
7814           (and (null gnus-newsgroup-dormant)
7815                (eq gnus-newsgroup-display 'gnus-not-ignore)
7816                (not (eq gnus-fetch-old-headers 'some))
7817                (not (numberp gnus-fetch-old-headers))
7818                (not (eq gnus-fetch-old-headers 'invisible))
7819                (null gnus-summary-expunge-below)
7820                (not (eq gnus-build-sparse-threads 'some))
7821                (not (eq gnus-build-sparse-threads 'more))
7822                (null gnus-thread-expunge-below)
7823                (not gnus-use-nocem)))
7824       ()                                ; Do nothing.
7825     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7826     (setq gnus-newsgroup-limit nil)
7827     (mapatoms
7828      (lambda (node)
7829        (unless (car (symbol-value node))
7830          ;; These threads have no parents -- they are roots.
7831          (let ((nodes (cdr (symbol-value node)))
7832                thread)
7833            (while nodes
7834              (if (and gnus-thread-expunge-below
7835                       (< (gnus-thread-total-score (car nodes))
7836                          gnus-thread-expunge-below))
7837                  (gnus-expunge-thread (pop nodes))
7838                (setq thread (pop nodes))
7839                (gnus-summary-limit-children thread))))))
7840      gnus-newsgroup-dependencies)
7841     ;; If this limitation resulted in an empty group, we might
7842     ;; pop the previous limit and use it instead.
7843     (when (and (not gnus-newsgroup-limit)
7844                show-if-empty)
7845       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7846     gnus-newsgroup-limit))
7847
7848 (defun gnus-summary-limit-children (thread)
7849   "Return 1 if this subthread is visible and 0 if it is not."
7850   ;; First we get the number of visible children to this thread.  This
7851   ;; is done by recursing down the thread using this function, so this
7852   ;; will really go down to a leaf article first, before slowly
7853   ;; working its way up towards the root.
7854   (when thread
7855     (let* ((max-lisp-eval-depth 5000)
7856            (children
7857            (if (cdr thread)
7858                (apply '+ (mapcar 'gnus-summary-limit-children
7859                                  (cdr thread)))
7860              0))
7861           (number (mail-header-number (car thread)))
7862           score)
7863       (if (and
7864            (not (memq number gnus-newsgroup-marked))
7865            (or
7866             ;; If this article is dormant and has absolutely no visible
7867             ;; children, then this article isn't visible.
7868             (and (memq number gnus-newsgroup-dormant)
7869                  (zerop children))
7870             ;; If this is "fetch-old-headered" and there is no
7871             ;; visible children, then we don't want this article.
7872             (and (or (eq gnus-fetch-old-headers 'some)
7873                      (numberp gnus-fetch-old-headers))
7874                  (gnus-summary-article-ancient-p number)
7875                  (zerop children))
7876             ;; If this is "fetch-old-headered" and `invisible', then
7877             ;; we don't want this article.
7878             (and (eq gnus-fetch-old-headers 'invisible)
7879                  (gnus-summary-article-ancient-p number))
7880             ;; If this is a sparsely inserted article with no children,
7881             ;; we don't want it.
7882             (and (eq gnus-build-sparse-threads 'some)
7883                  (gnus-summary-article-sparse-p number)
7884                  (zerop children))
7885             ;; If we use expunging, and this article is really
7886             ;; low-scored, then we don't want this article.
7887             (when (and gnus-summary-expunge-below
7888                        (< (setq score
7889                                 (or (cdr (assq number gnus-newsgroup-scored))
7890                                     gnus-summary-default-score))
7891                           gnus-summary-expunge-below))
7892               ;; We increase the expunge-tally here, but that has
7893               ;; nothing to do with the limits, really.
7894               (incf gnus-newsgroup-expunged-tally)
7895               ;; We also mark as read here, if that's wanted.
7896               (when (and gnus-summary-mark-below
7897                          (< score gnus-summary-mark-below))
7898                 (setq gnus-newsgroup-unreads
7899                       (delq number gnus-newsgroup-unreads))
7900                 (if gnus-newsgroup-auto-expire
7901                     (push number gnus-newsgroup-expirable)
7902                   (push (cons number gnus-low-score-mark)
7903                         gnus-newsgroup-reads)))
7904               t)
7905             ;; Do the `display' group parameter.
7906             (and gnus-newsgroup-display
7907                  (not (funcall gnus-newsgroup-display)))
7908             ;; Check NoCeM things.
7909             (if (and gnus-use-nocem
7910                      (gnus-nocem-unwanted-article-p
7911                       (mail-header-id (car thread))))
7912                 (progn
7913                   (setq gnus-newsgroup-unreads
7914                         (delq number gnus-newsgroup-unreads))
7915                   t))))
7916           ;; Nope, invisible article.
7917           0
7918         ;; Ok, this article is to be visible, so we add it to the limit
7919         ;; and return 1.
7920         (push number gnus-newsgroup-limit)
7921         1))))
7922
7923 (defun gnus-expunge-thread (thread)
7924   "Mark all articles in THREAD as read."
7925   (let* ((number (mail-header-number (car thread))))
7926     (incf gnus-newsgroup-expunged-tally)
7927     ;; We also mark as read here, if that's wanted.
7928     (setq gnus-newsgroup-unreads
7929           (delq number gnus-newsgroup-unreads))
7930     (if gnus-newsgroup-auto-expire
7931         (push number gnus-newsgroup-expirable)
7932       (push (cons number gnus-low-score-mark)
7933             gnus-newsgroup-reads)))
7934   ;; Go recursively through all subthreads.
7935   (mapcar 'gnus-expunge-thread (cdr thread)))
7936
7937 ;; Summary article oriented commands
7938
7939 (defun gnus-summary-refer-parent-article (n)
7940   "Refer parent article N times.
7941 If N is negative, go to ancestor -N instead.
7942 The difference between N and the number of articles fetched is returned."
7943   (interactive "p")
7944   (let ((skip 1)
7945         error header ref)
7946     (when (not (natnump n))
7947       (setq skip (abs n)
7948             n 1))
7949     (while (and (> n 0)
7950                 (not error))
7951       (setq header (gnus-summary-article-header))
7952       (if (and (eq (mail-header-number header)
7953                    (cdr gnus-article-current))
7954                (equal gnus-newsgroup-name
7955                       (car gnus-article-current)))
7956           ;; If we try to find the parent of the currently
7957           ;; displayed article, then we take a look at the actual
7958           ;; References header, since this is slightly more
7959           ;; reliable than the References field we got from the
7960           ;; server.
7961           (save-excursion
7962             (set-buffer gnus-original-article-buffer)
7963             (nnheader-narrow-to-headers)
7964             (unless (setq ref (message-fetch-field "references"))
7965               (setq ref (message-fetch-field "in-reply-to")))
7966             (widen))
7967         (setq ref
7968               ;; It's not the current article, so we take a bet on
7969               ;; the value we got from the server.
7970               (mail-header-references header)))
7971       (if (and ref
7972                (not (equal ref "")))
7973           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7974             (gnus-message 1 "Couldn't find parent"))
7975         (gnus-message 1 "No references in article %d"
7976                       (gnus-summary-article-number))
7977         (setq error t))
7978       (decf n))
7979     (gnus-summary-position-point)
7980     n))
7981
7982 (defun gnus-summary-refer-references ()
7983   "Fetch all articles mentioned in the References header.
7984 Return the number of articles fetched."
7985   (interactive)
7986   (let ((ref (mail-header-references (gnus-summary-article-header)))
7987         (current (gnus-summary-article-number))
7988         (n 0))
7989     (if (or (not ref)
7990             (equal ref ""))
7991         (error "No References in the current article")
7992       ;; For each Message-ID in the References header...
7993       (while (string-match "<[^>]*>" ref)
7994         (incf n)
7995         ;; ... fetch that article.
7996         (gnus-summary-refer-article
7997          (prog1 (match-string 0 ref)
7998            (setq ref (substring ref (match-end 0))))))
7999       (gnus-summary-goto-subject current)
8000       (gnus-summary-position-point)
8001       n)))
8002
8003 (defun gnus-summary-refer-thread (&optional limit)
8004   "Fetch all articles in the current thread.
8005 If LIMIT (the numerical prefix), fetch that many old headers instead
8006 of what's specified by the `gnus-refer-thread-limit' variable."
8007   (interactive "P")
8008   (let ((id (mail-header-id (gnus-summary-article-header)))
8009         (limit (if limit (prefix-numeric-value limit)
8010                  gnus-refer-thread-limit)))
8011     (unless (eq gnus-fetch-old-headers 'invisible)
8012       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8013       ;; Retrieve the headers and read them in.
8014       (if (eq (if (numberp limit)
8015                   (gnus-retrieve-headers
8016                    (list (min
8017                           (+ (mail-header-number
8018                               (gnus-summary-article-header))
8019                              limit)
8020                           gnus-newsgroup-end))
8021                    gnus-newsgroup-name (* limit 2))
8022                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8023                 ;; headers.
8024                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8025                                        gnus-newsgroup-name limit))
8026               'nov)
8027           (gnus-build-all-threads)
8028         (error "Can't fetch thread from backends that don't support NOV"))
8029       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8030     (gnus-summary-limit-include-thread id)))
8031
8032 (defun gnus-summary-refer-article (message-id)
8033   "Fetch an article specified by MESSAGE-ID."
8034   (interactive "sMessage-ID: ")
8035   (when (and (stringp message-id)
8036              (not (zerop (length message-id))))
8037     ;; Construct the correct Message-ID if necessary.
8038     ;; Suggested by tale@pawl.rpi.edu.
8039     (unless (string-match "^<" message-id)
8040       (setq message-id (concat "<" message-id)))
8041     (unless (string-match ">$" message-id)
8042       (setq message-id (concat message-id ">")))
8043     (let* ((header (gnus-id-to-header message-id))
8044            (sparse (and header
8045                         (gnus-summary-article-sparse-p
8046                          (mail-header-number header))
8047                         (memq (mail-header-number header)
8048                               gnus-newsgroup-limit)))
8049            number)
8050       (cond
8051        ;; If the article is present in the buffer we just go to it.
8052        ((and header
8053              (or (not (gnus-summary-article-sparse-p
8054                        (mail-header-number header)))
8055                  sparse))
8056         (prog1
8057             (gnus-summary-goto-article
8058              (mail-header-number header) nil t)
8059           (when sparse
8060             (gnus-summary-update-article (mail-header-number header)))))
8061        (t
8062         ;; We fetch the article.
8063         (catch 'found
8064           (dolist (gnus-override-method (gnus-refer-article-methods))
8065             (when (and (gnus-check-server gnus-override-method)
8066                        ;; Fetch the header,
8067                        (setq number (gnus-summary-insert-subject message-id)))
8068               ;; and display the article.
8069               (gnus-summary-select-article nil nil nil number)
8070               (throw 'found t)))
8071           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8072
8073 (defun gnus-refer-article-methods ()
8074   "Return a list of referable methods."
8075   (cond
8076    ;; No method, so we default to current and native.
8077    ((null gnus-refer-article-method)
8078     (list gnus-current-select-method gnus-select-method))
8079    ;; Current.
8080    ((eq 'current gnus-refer-article-method)
8081     (list gnus-current-select-method))
8082    ;; List of select methods.
8083    ((not (and (symbolp (car gnus-refer-article-method))
8084               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8085     (let (out)
8086       (dolist (method gnus-refer-article-method)
8087         (push (if (eq 'current method)
8088                   gnus-current-select-method
8089                 method)
8090               out))
8091       (nreverse out)))
8092    ;; One single select method.
8093    (t
8094     (list gnus-refer-article-method))))
8095
8096 (defun gnus-summary-edit-parameters ()
8097   "Edit the group parameters of the current group."
8098   (interactive)
8099   (gnus-group-edit-group gnus-newsgroup-name 'params))
8100
8101 (defun gnus-summary-customize-parameters ()
8102   "Customize the group parameters of the current group."
8103   (interactive)
8104   (gnus-group-customize gnus-newsgroup-name))
8105
8106 (defun gnus-summary-enter-digest-group (&optional force)
8107   "Enter an nndoc group based on the current article.
8108 If FORCE, force a digest interpretation.  If not, try
8109 to guess what the document format is."
8110   (interactive "P")
8111   (let ((conf gnus-current-window-configuration))
8112     (save-excursion
8113       (gnus-summary-select-article))
8114     (setq gnus-current-window-configuration conf)
8115     (let* ((name (format "%s-%d"
8116                          (gnus-group-prefixed-name
8117                           gnus-newsgroup-name (list 'nndoc ""))
8118                          (save-excursion
8119                            (set-buffer gnus-summary-buffer)
8120                            gnus-current-article)))
8121            (ogroup gnus-newsgroup-name)
8122            (params (append (gnus-info-params (gnus-get-info ogroup))
8123                            (list (cons 'to-group ogroup))
8124                            (list (cons 'save-article-group ogroup))))
8125            (case-fold-search t)
8126            (buf (current-buffer))
8127            dig to-address)
8128       (save-excursion
8129         (set-buffer gnus-original-article-buffer)
8130         ;; Have the digest group inherit the main mail address of
8131         ;; the parent article.
8132         (when (setq to-address (or (gnus-fetch-field "reply-to")
8133                                    (gnus-fetch-field "from")))
8134           (setq params (append
8135                         (list (cons 'to-address
8136                                     (funcall gnus-decode-encoded-word-function
8137                                              to-address))))))
8138         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8139         (insert-buffer-substring gnus-original-article-buffer)
8140         ;; Remove lines that may lead nndoc to misinterpret the
8141         ;; document type.
8142         (narrow-to-region
8143          (goto-char (point-min))
8144          (or (search-forward "\n\n" nil t) (point)))
8145         (goto-char (point-min))
8146         (delete-matching-lines "^Path:\\|^From ")
8147         (widen))
8148       (unwind-protect
8149           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8150                     (gnus-newsgroup-ephemeral-ignored-charsets
8151                      gnus-newsgroup-ignored-charsets))
8152                 (gnus-group-read-ephemeral-group
8153                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8154                               (nndoc-article-type
8155                                ,(if force 'mbox 'guess)))
8156                  t nil nil nil
8157                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8158                                                         "ADAPT")))))
8159               ;; Make all postings to this group go to the parent group.
8160               (nconc (gnus-info-params (gnus-get-info name))
8161                      params)
8162             ;; Couldn't select this doc group.
8163             (switch-to-buffer buf)
8164             (gnus-set-global-variables)
8165             (gnus-configure-windows 'summary)
8166             (gnus-message 3 "Article couldn't be entered?"))
8167         (kill-buffer dig)))))
8168
8169 (defun gnus-summary-read-document (n)
8170   "Open a new group based on the current article(s).
8171 This will allow you to read digests and other similar
8172 documents as newsgroups.
8173 Obeys the standard process/prefix convention."
8174   (interactive "P")
8175   (let* ((articles (gnus-summary-work-articles n))
8176          (ogroup gnus-newsgroup-name)
8177          (params (append (gnus-info-params (gnus-get-info ogroup))
8178                          (list (cons 'to-group ogroup))))
8179          article group egroup groups vgroup)
8180     (while (setq article (pop articles))
8181       (setq group (format "%s-%d" gnus-newsgroup-name article))
8182       (gnus-summary-remove-process-mark article)
8183       (when (gnus-summary-display-article article)
8184         (save-excursion
8185           (with-temp-buffer
8186             (insert-buffer-substring gnus-original-article-buffer)
8187             ;; Remove some headers that may lead nndoc to make
8188             ;; the wrong guess.
8189             (message-narrow-to-head)
8190             (goto-char (point-min))
8191             (delete-matching-lines "^\\(Path\\):\\|^From ")
8192             (widen)
8193             (if (setq egroup
8194                       (gnus-group-read-ephemeral-group
8195                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8196                                      (nndoc-article-type guess))
8197                        t nil t))
8198                 (progn
8199                   ;; Make all postings to this group go to the parent group.
8200                   (nconc (gnus-info-params (gnus-get-info egroup))
8201                          params)
8202                   (push egroup groups))
8203               ;; Couldn't select this doc group.
8204               (gnus-error 3 "Article couldn't be entered"))))))
8205     ;; Now we have selected all the documents.
8206     (cond
8207      ((not groups)
8208       (error "None of the articles could be interpreted as documents"))
8209      ((gnus-group-read-ephemeral-group
8210        (setq vgroup (format
8211                      "nnvirtual:%s-%s" gnus-newsgroup-name
8212                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8213        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8214        t
8215        (cons (current-buffer) 'summary)))
8216      (t
8217       (error "Couldn't select virtual nndoc group")))))
8218
8219 (defun gnus-summary-isearch-article (&optional regexp-p)
8220   "Do incremental search forward on the current article.
8221 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8222   (interactive "P")
8223   (let* ((gnus-inhibit-treatment t)
8224          (old (gnus-summary-select-article)))
8225     (gnus-configure-windows 'article)
8226     (gnus-eval-in-buffer-window gnus-article-buffer
8227       (save-restriction
8228         (widen)
8229         (when (eq 'old old)
8230           (gnus-article-show-all-headers))
8231         (goto-char (point-min))
8232         (isearch-forward regexp-p)))))
8233
8234 (defun gnus-summary-search-article-forward (regexp &optional backward)
8235   "Search for an article containing REGEXP forward.
8236 If BACKWARD, search backward instead."
8237   (interactive
8238    (list (read-string
8239           (format "Search article %s (regexp%s): "
8240                   (if current-prefix-arg "backward" "forward")
8241                   (if gnus-last-search-regexp
8242                       (concat ", default " gnus-last-search-regexp)
8243                     "")))
8244          current-prefix-arg))
8245   (if (string-equal regexp "")
8246       (setq regexp (or gnus-last-search-regexp ""))
8247     (setq gnus-last-search-regexp regexp)
8248     (setq gnus-article-before-search gnus-current-article))
8249   ;; Intentionally set gnus-last-article.
8250   (setq gnus-last-article gnus-article-before-search)
8251   (let ((gnus-last-article gnus-last-article))
8252     (if (gnus-summary-search-article regexp backward)
8253         (gnus-summary-show-thread)
8254       (error "Search failed: \"%s\"" regexp))))
8255
8256 (defun gnus-summary-search-article-backward (regexp)
8257   "Search for an article containing REGEXP backward."
8258   (interactive
8259    (list (read-string
8260           (format "Search article backward (regexp%s): "
8261                   (if gnus-last-search-regexp
8262                       (concat ", default " gnus-last-search-regexp)
8263                     "")))))
8264   (gnus-summary-search-article-forward regexp 'backward))
8265
8266 (eval-when-compile
8267   (defmacro gnus-summary-search-article-position-point (regexp backward)
8268     "Dehighlight the last matched text and goto the beginning position."
8269     (` (if (and gnus-summary-search-article-matched-data
8270                 (let ((text (caddr gnus-summary-search-article-matched-data))
8271                       (inhibit-read-only t)
8272                       buffer-read-only)
8273                   (delete-region
8274                    (goto-char (car gnus-summary-search-article-matched-data))
8275                    (cadr gnus-summary-search-article-matched-data))
8276                   (insert text)
8277                   (string-match (, regexp) text)))
8278            (if (, backward) (beginning-of-line) (end-of-line))
8279          (goto-char (if (, backward) (point-max) (point-min))))))
8280
8281   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8282     "Place point where X-Face image is displayed."
8283     (if (featurep 'xemacs)
8284         (` (let ((end (if (search-forward "\n\n" nil t)
8285                           (goto-char (1- (point)))
8286                         (point-min)))
8287                  extent)
8288              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8289              (unless (and (re-search-forward "^From:" end t)
8290                           (setq extent (extent-at (point)))
8291                           (extent-begin-glyph extent))
8292                (goto-char (, opoint)))))
8293       (` (let ((end (if (search-forward "\n\n" nil t)
8294                         (goto-char (1- (point)))
8295                       (point-min)))
8296                (start (or (search-backward "\n\n" nil t) (point-min))))
8297            (goto-char
8298             (or (text-property-any start end 'x-face-image t);; x-face-e21
8299                 (text-property-any start end 'x-face-mule-bitmap-image t)
8300                 (, opoint)))))))
8301
8302   (defmacro gnus-summary-search-article-highlight-matched-text
8303     (backward treated x-face)
8304     "Highlight matched text in the function `gnus-summary-search-article'."
8305     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8306              (end (set-marker (make-marker) (match-end 0)))
8307              (inhibit-read-only t)
8308              buffer-read-only)
8309          (unless treated
8310            (let ((,@
8311                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8312                     (mapcar
8313                      (lambda (item) (setq items (delq item items)))
8314                      '(gnus-treat-buttonize
8315                        gnus-treat-fill-article
8316                        gnus-treat-fill-long-lines
8317                        gnus-treat-emphasize
8318                        gnus-treat-highlight-headers
8319                        gnus-treat-highlight-citation
8320                        gnus-treat-highlight-signature
8321                        gnus-treat-overstrike
8322                        gnus-treat-display-xface
8323                        gnus-treat-buttonize-head
8324                        gnus-treat-decode-article-as-default-mime-charset))
8325                     (static-if (featurep 'xemacs)
8326                         items
8327                       (cons '(x-face-mule-delete-x-face-field
8328                               (quote never))
8329                             items))))
8330                  (gnus-treat-display-xface
8331                   (when (, x-face) gnus-treat-display-xface)))
8332              (gnus-article-prepare-mime-display)))
8333          (goto-char (if (, backward) start end))
8334          (when (, x-face)
8335            (gnus-summary-search-article-highlight-goto-x-face (point)))
8336          (setq gnus-summary-search-article-matched-data
8337                (list start end (buffer-substring start end)))
8338          (unless (eq start end);; matched text has been deleted. :-<
8339            (put-text-property start end 'face
8340                               (or (find-face 'isearch)
8341                                   'secondary-selection))))))
8342   )
8343
8344 (defun gnus-summary-search-article (regexp &optional backward)
8345   "Search for an article containing REGEXP.
8346 Optional argument BACKWARD means do search for backward.
8347 `gnus-select-article-hook' is not called during the search."
8348   ;; We have to require this here to make sure that the following
8349   ;; dynamic binding isn't shadowed by autoloading.
8350   (require 'gnus-async)
8351   (require 'gnus-art)
8352   (let ((gnus-select-article-hook nil)  ;Disable hook.
8353         (gnus-article-prepare-hook nil)
8354         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8355         (gnus-use-article-prefetch nil)
8356         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8357         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8358         (gnus-visual nil)
8359         (gnus-keep-backlog nil)
8360         (gnus-break-pages nil)
8361         (gnus-summary-display-arrow nil)
8362         (gnus-updated-mode-lines nil)
8363         (gnus-auto-center-summary nil)
8364         (sum (current-buffer))
8365         (found nil)
8366         point treated)
8367     (gnus-save-hidden-threads
8368       (static-if (featurep 'xemacs)
8369           (let ((gnus-inhibit-treatment t))
8370             (setq treated (eq 'old (gnus-summary-select-article)))
8371             (when (and treated
8372                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8373                                  (window-live-p (get-buffer-window
8374                                                  gnus-article-buffer t)))))
8375               (gnus-summary-select-article nil t)
8376               (setq treated nil)))
8377         (let ((gnus-inhibit-treatment t)
8378               (x-face-mule-delete-x-face-field 'never))
8379           (setq treated (eq 'old (gnus-summary-select-article)))
8380           (when (and treated
8381                      (not
8382                       (and (gnus-buffer-live-p gnus-article-buffer)
8383                            (window-live-p (get-buffer-window
8384                                            gnus-article-buffer t))
8385                            (or (not (string-match "^\\^X-Face:" regexp))
8386                                (with-current-buffer gnus-article-buffer
8387                                  gnus-summary-search-article-matched-data)))))
8388             (gnus-summary-select-article nil t)
8389             (setq treated nil))))
8390       (set-buffer gnus-article-buffer)
8391       (widen)
8392       (if treated
8393           (progn
8394             (gnus-article-show-all-headers)
8395             (gnus-summary-search-article-position-point regexp backward))
8396         (goto-char (if backward (point-max) (point-min))))
8397       (while (not found)
8398         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8399         (if (if backward
8400                 (re-search-backward regexp nil t)
8401               (re-search-forward regexp nil t))
8402             ;; We found the regexp.
8403             (progn
8404               (gnus-summary-search-article-highlight-matched-text
8405                backward treated (string-match "^\\^X-Face:" regexp))
8406               (setq found 'found)
8407               (forward-line
8408                (/ (- 2 (window-height
8409                         (get-buffer-window gnus-article-buffer t)))
8410                   2))
8411               (set-window-start
8412                (get-buffer-window (current-buffer))
8413                (point))
8414               (set-buffer sum)
8415               (setq point (point)))
8416           ;; We didn't find it, so we go to the next article.
8417           (set-buffer sum)
8418           (setq found 'not)
8419           (while (eq found 'not)
8420             (if (not (if backward (gnus-summary-find-prev)
8421                        (gnus-summary-find-next)))
8422                 ;; No more articles.
8423                 (setq found t)
8424               ;; Select the next article and adjust point.
8425               (unless (gnus-summary-article-sparse-p
8426                        (gnus-summary-article-number))
8427                 (setq found nil)
8428                 (let ((gnus-inhibit-treatment t))
8429                   (gnus-summary-select-article))
8430                 (setq treated nil)
8431                 (set-buffer gnus-article-buffer)
8432                 (widen)
8433                 (goto-char (if backward (point-max) (point-min))))))))
8434       (gnus-message 7 ""))
8435     ;; Return whether we found the regexp.
8436     (when (eq found 'found)
8437       (goto-char point)
8438       (gnus-summary-show-thread)
8439       (gnus-summary-goto-subject gnus-current-article)
8440       (gnus-summary-position-point)
8441       t)))
8442
8443 (defun gnus-find-matching-articles (header regexp)
8444   "Return a list of all articles that match REGEXP on HEADER.
8445 This search includes all articles in the current group that Gnus has
8446 fetched headers for, whether they are displayed or not."
8447   (let ((articles nil)
8448         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8449         (case-fold-search t))
8450     (dolist (header gnus-newsgroup-headers)
8451       (when (string-match regexp (funcall func header))
8452         (push (mail-header-number header) articles)))
8453     (nreverse articles)))
8454
8455 (defun gnus-summary-find-matching (header regexp &optional backward unread
8456                                           not-case-fold not-matching)
8457   "Return a list of all articles that match REGEXP on HEADER.
8458 The search stars on the current article and goes forwards unless
8459 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8460 If UNREAD is non-nil, only unread articles will
8461 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8462 in the comparisons. If NOT-MATCHING, return a list of all articles that
8463 not match REGEXP on HEADER."
8464   (let ((case-fold-search (not not-case-fold))
8465         articles d func)
8466     (if (consp header)
8467         (if (eq (car header) 'extra)
8468             (setq func
8469                   `(lambda (h)
8470                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8471                          "")))
8472           (error "%s is an invalid header" header))
8473       (unless (fboundp (intern (concat "mail-header-" header)))
8474         (error "%s is not a valid header" header))
8475       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8476     (dolist (d (if (eq backward 'all)
8477                    gnus-newsgroup-data
8478                  (gnus-data-find-list
8479                   (gnus-summary-article-number)
8480                   (gnus-data-list backward))))
8481       (when (and (or (not unread)       ; We want all articles...
8482                      (gnus-data-unread-p d)) ; Or just unreads.
8483                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8484                  (if not-matching
8485                      (not (string-match
8486                            regexp
8487                            (funcall func (gnus-data-header d))))
8488                    (string-match regexp
8489                                  (funcall func (gnus-data-header d)))))
8490         (push (gnus-data-number d) articles))) ; Success!
8491     (nreverse articles)))
8492
8493 (defun gnus-summary-execute-command (header regexp command &optional backward)
8494   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8495 If HEADER is an empty string (or nil), the match is done on the entire
8496 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8497   (interactive
8498    (list (let ((completion-ignore-case t))
8499            (completing-read
8500             "Header name: "
8501             (mapcar (lambda (header) (list (format "%s" header)))
8502                     (append
8503                      '("Number" "Subject" "From" "Lines" "Date"
8504                        "Message-ID" "Xref" "References" "Body")
8505                      gnus-extra-headers))
8506             nil 'require-match))
8507          (read-string "Regexp: ")
8508          (read-key-sequence "Command: ")
8509          current-prefix-arg))
8510   (when (equal header "Body")
8511     (setq header ""))
8512   ;; Hidden thread subtrees must be searched as well.
8513   (gnus-summary-show-all-threads)
8514   ;; We don't want to change current point nor window configuration.
8515   (save-excursion
8516     (save-window-excursion
8517       (let (gnus-visual
8518             gnus-treat-strip-trailing-blank-lines
8519             gnus-treat-strip-leading-blank-lines
8520             gnus-treat-strip-multiple-blank-lines
8521             gnus-treat-hide-boring-headers
8522             gnus-treat-fold-newsgroups
8523             gnus-article-prepare-hook)
8524         (gnus-message 6 "Executing %s..." (key-description command))
8525         ;; We'd like to execute COMMAND interactively so as to give arguments.
8526         (gnus-execute header regexp
8527                       `(call-interactively ',(key-binding command))
8528                       backward)
8529         (gnus-message 6 "Executing %s...done" (key-description command))))))
8530
8531 (defun gnus-summary-beginning-of-article ()
8532   "Scroll the article back to the beginning."
8533   (interactive)
8534   (gnus-summary-select-article)
8535   (gnus-configure-windows 'article)
8536   (gnus-eval-in-buffer-window gnus-article-buffer
8537     (widen)
8538     (goto-char (point-min))
8539     (when gnus-page-broken
8540       (gnus-narrow-to-page))))
8541
8542 (defun gnus-summary-end-of-article ()
8543   "Scroll to the end of the article."
8544   (interactive)
8545   (gnus-summary-select-article)
8546   (gnus-configure-windows 'article)
8547   (gnus-eval-in-buffer-window gnus-article-buffer
8548     (widen)
8549     (goto-char (point-max))
8550     (recenter -3)
8551     (when gnus-page-broken
8552       (gnus-narrow-to-page))))
8553
8554 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8555   "Truncate to LEN and quote all \"(\"'s in STRING."
8556   (gnus-replace-in-string (if (and len (> (length string) len))
8557                               (substring string 0 len)
8558                             string)
8559                           "[()]" "\\\\\\&"))
8560
8561 (defun gnus-summary-print-article (&optional filename n)
8562   "Generate and print a PostScript image of the N next (mail) articles.
8563
8564 If N is negative, print the N previous articles.  If N is nil and articles
8565 have been marked with the process mark, print these instead.
8566
8567 If the optional first argument FILENAME is nil, send the image to the
8568 printer.  If FILENAME is a string, save the PostScript image in a file with
8569 that name.  If FILENAME is a number, prompt the user for the name of the file
8570 to save in."
8571   (interactive (list (ps-print-preprint current-prefix-arg)))
8572   (dolist (article (gnus-summary-work-articles n))
8573     (gnus-summary-select-article nil nil 'pseudo article)
8574     (gnus-eval-in-buffer-window gnus-article-buffer
8575       (gnus-print-buffer))
8576     (gnus-summary-remove-process-mark article))
8577   (ps-despool filename))
8578
8579 (defun gnus-print-buffer ()
8580   (let ((buffer (generate-new-buffer " *print*")))
8581     (unwind-protect
8582         (progn
8583           (copy-to-buffer buffer (point-min) (point-max))
8584           (set-buffer buffer)
8585           (gnus-article-delete-invisible-text)
8586           (gnus-remove-text-with-property 'gnus-decoration)
8587           (when (gnus-visual-p 'article-highlight 'highlight)
8588             ;; Copy-to-buffer doesn't copy overlay.  So redo
8589             ;; highlight.
8590             (let ((gnus-article-buffer buffer))
8591               (gnus-article-highlight-citation t)
8592               (gnus-article-highlight-signature)))
8593           (let ((ps-left-header
8594                  (list
8595                   (concat "("
8596                           (gnus-summary-print-truncate-and-quote
8597                            (mail-header-subject gnus-current-headers)
8598                            66) ")")
8599                   (concat "("
8600                           (gnus-summary-print-truncate-and-quote
8601                            (mail-header-from gnus-current-headers)
8602                            45) ")")))
8603                 (ps-right-header
8604                  (list
8605                   "/pagenumberstring load"
8606                   (concat "("
8607                           (mail-header-date gnus-current-headers) ")"))))
8608             (gnus-run-hooks 'gnus-ps-print-hook)
8609             (save-excursion
8610               (if window-system
8611                   (ps-spool-buffer-with-faces)
8612                 (ps-spool-buffer)))))
8613       (kill-buffer buffer))))
8614
8615 (defun gnus-summary-show-article (&optional arg)
8616   "Force redisplaying of the current article.
8617 If ARG (the prefix) is a number, show the article with the charset
8618 defined in `gnus-summary-show-article-charset-alist', or the charset
8619 input.
8620 If ARG (the prefix) is non-nil and not a number, show the raw article
8621 without any article massaging functions being run.  Normally, the key strokes
8622 are `C-u g'."
8623   (interactive "P")
8624   (cond
8625    ((numberp arg)
8626     (gnus-summary-show-article t)
8627     (let* ((gnus-newsgroup-charset
8628             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8629                 (mm-read-coding-system
8630                  "View as charset: " ;; actually it is coding system.
8631                  (save-excursion
8632                    (set-buffer gnus-article-buffer)
8633                    (mm-detect-coding-region (point) (point-max))))))
8634            (default-mime-charset gnus-newsgroup-charset)
8635            (gnus-newsgroup-ignored-charsets 'gnus-all))
8636       (gnus-summary-select-article nil 'force)
8637       (let ((deps gnus-newsgroup-dependencies)
8638             head header lines)
8639         (save-excursion
8640           (set-buffer gnus-original-article-buffer)
8641           (save-restriction
8642             (message-narrow-to-head)
8643             (setq head (buffer-string))
8644             (goto-char (point-min))
8645             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8646               (goto-char (point-max))
8647               (widen)
8648               (setq lines (1- (count-lines (point) (point-max))))))
8649           (with-temp-buffer
8650             (insert (format "211 %d Article retrieved.\n"
8651                             (cdr gnus-article-current)))
8652             (insert head)
8653             (if lines (insert (format "Lines: %d\n" lines)))
8654             (insert ".\n")
8655             (let ((nntp-server-buffer (current-buffer)))
8656               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8657         (gnus-data-set-header
8658          (gnus-data-find (cdr gnus-article-current))
8659          header)
8660         (gnus-summary-update-article-line
8661          (cdr gnus-article-current) header)
8662         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8663           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8664    ((not arg)
8665     ;; Select the article the normal way.
8666     (gnus-summary-select-article nil 'force))
8667    (t
8668     ;; We have to require this here to make sure that the following
8669     ;; dynamic binding isn't shadowed by autoloading.
8670     (require 'gnus-async)
8671     (require 'gnus-art)
8672     ;; Bind the article treatment functions to nil.
8673     (let ((gnus-have-all-headers t)
8674           gnus-article-prepare-hook
8675           gnus-article-decode-hook
8676           gnus-break-pages
8677           gnus-show-mime
8678           (gnus-inhibit-treatment t))
8679       (gnus-summary-select-article nil 'force))))
8680   (gnus-summary-goto-subject gnus-current-article)
8681   (gnus-summary-position-point))
8682
8683 (defun gnus-summary-show-raw-article ()
8684   "Show the raw article without any article massaging functions being run."
8685   (interactive)
8686   (gnus-summary-show-article t))
8687
8688 (defun gnus-summary-verbose-headers (&optional arg)
8689   "Toggle permanent full header display.
8690 If ARG is a positive number, turn header display on.
8691 If ARG is a negative number, turn header display off."
8692   (interactive "P")
8693   (setq gnus-show-all-headers
8694         (cond ((or (not (numberp arg))
8695                    (zerop arg))
8696                (not gnus-show-all-headers))
8697               ((natnump arg)
8698                t)))
8699   (gnus-summary-show-article))
8700
8701 (defun gnus-summary-toggle-header (&optional arg)
8702   "Show the headers if they are hidden, or hide them if they are shown.
8703 If ARG is a positive number, show the entire header.
8704 If ARG is a negative number, hide the unwanted header lines."
8705   (interactive "P")
8706   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8707                      (get-buffer-window gnus-article-buffer t))))
8708     (with-current-buffer gnus-article-buffer
8709       (widen)
8710       (article-narrow-to-head)
8711       (let* ((buffer-read-only nil)
8712              (inhibit-point-motion-hooks t)
8713              (hidden (if (numberp arg)
8714                          (>= arg 0)
8715                        (gnus-article-hidden-text-p 'headers)))
8716              s e)
8717         (delete-region (point-min) (point-max))
8718         (with-current-buffer gnus-original-article-buffer
8719           (goto-char (setq s (point-min)))
8720           (setq e (if (search-forward "\n\n" nil t)
8721                       (1- (point))
8722                     (point-max))))
8723         (insert-buffer-substring gnus-original-article-buffer s e)
8724         (run-hooks 'gnus-article-decode-hook)
8725         (if hidden
8726             (let ((gnus-treat-hide-headers nil)
8727                   (gnus-treat-hide-boring-headers nil))
8728               (gnus-delete-wash-type 'headers)
8729               (gnus-treat-article 'head))
8730           (gnus-treat-article 'head))
8731         (widen)
8732         (if window
8733             (set-window-start window (goto-char (point-min))))
8734         (setq gnus-page-broken
8735               (when gnus-break-pages
8736                 (gnus-narrow-to-page)
8737                 t))
8738         (gnus-set-mode-line 'article)))))
8739
8740 (defun gnus-summary-show-all-headers ()
8741   "Make all header lines visible."
8742   (interactive)
8743   (gnus-summary-toggle-header 1))
8744
8745 (defun gnus-summary-toggle-mime (&optional arg)
8746   "Toggle MIME processing.
8747 If ARG is a positive number, turn MIME processing on."
8748   (interactive "P")
8749   (setq gnus-show-mime
8750         (if (null arg)
8751             (not gnus-show-mime)
8752           (> (prefix-numeric-value arg) 0)))
8753   (gnus-summary-select-article t 'force))
8754
8755 (defun gnus-summary-caesar-message (&optional arg)
8756   "Caesar rotate the current article by 13.
8757 The numerical prefix specifies how many places to rotate each letter
8758 forward."
8759   (interactive "P")
8760   (gnus-summary-select-article)
8761   (let ((mail-header-separator ""))
8762     (gnus-eval-in-buffer-window gnus-article-buffer
8763       (save-restriction
8764         (widen)
8765         (let ((start (window-start))
8766               buffer-read-only)
8767           (message-caesar-buffer-body arg)
8768           (set-window-start (get-buffer-window (current-buffer)) start))))))
8769
8770 (autoload 'unmorse-region "morse"
8771   "Convert morse coded text in region to ordinary ASCII text."
8772   t)
8773
8774 (defun gnus-summary-morse-message (&optional arg)
8775   "Morse decode the current article."
8776   (interactive "P")
8777   (gnus-summary-select-article)
8778   (let ((mail-header-separator ""))
8779     (gnus-eval-in-buffer-window gnus-article-buffer
8780       (save-excursion
8781         (save-restriction
8782           (widen)
8783           (let ((pos (window-start))
8784                 buffer-read-only)
8785             (goto-char (point-min))
8786             (when (message-goto-body)
8787               (gnus-narrow-to-body))
8788             (goto-char (point-min))
8789             (while (re-search-forward "·" (point-max) t)
8790               (replace-match "."))
8791             (unmorse-region (point-min) (point-max))
8792             (widen)
8793             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8794
8795 (defun gnus-summary-stop-page-breaking ()
8796   "Stop page breaking in the current article."
8797   (interactive)
8798   (gnus-summary-select-article)
8799   (gnus-eval-in-buffer-window gnus-article-buffer
8800     (widen)
8801     (when (gnus-visual-p 'page-marker)
8802       (let ((buffer-read-only nil))
8803         (gnus-remove-text-with-property 'gnus-prev)
8804         (gnus-remove-text-with-property 'gnus-next))
8805       (setq gnus-page-broken nil))))
8806
8807 (defun gnus-summary-move-article (&optional n to-newsgroup
8808                                             select-method action)
8809   "Move the current article to a different newsgroup.
8810 If N is a positive number, move the N next articles.
8811 If N is a negative number, move the N previous articles.
8812 If N is nil and any articles have been marked with the process mark,
8813 move those articles instead.
8814 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8815 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8816 re-spool using this method.
8817
8818 When called interactively with TO-NEWSGROUP being nil, the value of
8819 the variable `gnus-move-split-methods' is used for finding a default
8820 for the target newsgroup.
8821
8822 For this function to work, both the current newsgroup and the
8823 newsgroup that you want to move to have to support the `request-move'
8824 and `request-accept' functions.
8825
8826 ACTION can be either `move' (the default), `crosspost' or `copy'."
8827   (interactive "P")
8828   (unless action
8829     (setq action 'move))
8830   ;; Check whether the source group supports the required functions.
8831   (cond ((and (eq action 'move)
8832               (not (gnus-check-backend-function
8833                     'request-move-article gnus-newsgroup-name)))
8834          (error "The current group does not support article moving"))
8835         ((and (eq action 'crosspost)
8836               (not (gnus-check-backend-function
8837                     'request-replace-article gnus-newsgroup-name)))
8838          (error "The current group does not support article editing")))
8839   (let ((articles (gnus-summary-work-articles n))
8840         (prefix (if (gnus-check-backend-function
8841                      'request-move-article gnus-newsgroup-name)
8842                     (gnus-group-real-prefix gnus-newsgroup-name)
8843                   ""))
8844         (names '((move "Move" "Moving")
8845                  (copy "Copy" "Copying")
8846                  (crosspost "Crosspost" "Crossposting")))
8847         (copy-buf (save-excursion
8848                     (nnheader-set-temp-buffer " *copy article*")))
8849         (default-marks gnus-article-mark-lists)
8850         (no-expire-marks (delete '(expirable . expire)
8851                                  (copy-sequence gnus-article-mark-lists)))
8852         art-group to-method new-xref article to-groups)
8853     (unless (assq action names)
8854       (error "Unknown action %s" action))
8855     ;; Read the newsgroup name.
8856     (when (and (not to-newsgroup)
8857                (not select-method))
8858       (if (and gnus-move-split-methods
8859                (not
8860                 (and (memq gnus-current-article articles)
8861                      (gnus-buffer-live-p gnus-original-article-buffer))))
8862           ;; When `gnus-move-split-methods' is non-nil, we have to
8863           ;; select an article to give `gnus-read-move-group-name' an
8864           ;; opportunity to suggest an appropriate default.  However,
8865           ;; we needn't render or mark the article.
8866           (let ((gnus-display-mime-function nil)
8867                 (gnus-article-prepare-hook nil)
8868                 (gnus-mark-article-hook nil))
8869             (gnus-summary-select-article nil nil nil (car articles))))
8870       (setq to-newsgroup
8871             (gnus-read-move-group-name
8872              (cadr (assq action names))
8873              (symbol-value (intern (format "gnus-current-%s-group" action)))
8874              articles prefix))
8875       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8876     (setq to-method (or select-method
8877                         (gnus-server-to-method
8878                          (gnus-group-method to-newsgroup))))
8879     ;; Check the method we are to move this article to...
8880     (unless (gnus-check-backend-function
8881              'request-accept-article (car to-method))
8882       (error "%s does not support article copying" (car to-method)))
8883     (unless (gnus-check-server to-method)
8884       (error "Can't open server %s" (car to-method)))
8885     (gnus-message 6 "%s to %s: %s..."
8886                   (caddr (assq action names))
8887                   (or (car select-method) to-newsgroup) articles)
8888     (while articles
8889       (setq article (pop articles))
8890       (setq
8891        art-group
8892        (cond
8893         ;; Move the article.
8894         ((eq action 'move)
8895          ;; Remove this article from future suppression.
8896          (gnus-dup-unsuppress-article article)
8897          (gnus-request-move-article
8898           article                       ; Article to move
8899           gnus-newsgroup-name           ; From newsgroup
8900           (nth 1 (gnus-find-method-for-group
8901                   gnus-newsgroup-name)) ; Server
8902           (list 'gnus-request-accept-article
8903                 to-newsgroup (list 'quote select-method)
8904                 (not articles) t)       ; Accept form
8905           (not articles)))              ; Only save nov last time
8906         ;; Copy the article.
8907         ((eq action 'copy)
8908          (save-excursion
8909            (set-buffer copy-buf)
8910            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8911              (gnus-request-accept-article
8912               to-newsgroup select-method (not articles) t))))
8913         ;; Crosspost the article.
8914         ((eq action 'crosspost)
8915          (let ((xref (message-tokenize-header
8916                       (mail-header-xref (gnus-summary-article-header article))
8917                       " ")))
8918            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8919                                   ":" (number-to-string article)))
8920            (unless xref
8921              (setq xref (list (system-name))))
8922            (setq new-xref
8923                  (concat
8924                   (mapconcat 'identity
8925                              (delete "Xref:" (delete new-xref xref))
8926                              " ")
8927                   " " new-xref))
8928            (save-excursion
8929              (set-buffer copy-buf)
8930              ;; First put the article in the destination group.
8931              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8932              (when (consp (setq art-group
8933                                 (gnus-request-accept-article
8934                                  to-newsgroup select-method (not articles))))
8935                (setq new-xref (concat new-xref " " (car art-group)
8936                                       ":"
8937                                       (number-to-string (cdr art-group))))
8938                ;; Now we have the new Xrefs header, so we insert
8939                ;; it and replace the new article.
8940                (nnheader-replace-header "Xref" new-xref)
8941                (gnus-request-replace-article
8942                 (cdr art-group) to-newsgroup (current-buffer))
8943                art-group))))))
8944       (cond
8945        ((not art-group)
8946         (gnus-message 1 "Couldn't %s article %s: %s"
8947                       (cadr (assq action names)) article
8948                       (nnheader-get-report (car to-method))))
8949        ((eq art-group 'junk)
8950         (when (eq action 'move)
8951           (gnus-summary-mark-article article gnus-canceled-mark)
8952           (gnus-message 4 "Deleted article %s" article)
8953           ;; run the delete hook
8954           (run-hook-with-args 'gnus-summary-article-delete-hook
8955                               action
8956                               (gnus-data-header
8957                                (assoc article (gnus-data-list nil)))
8958                               gnus-newsgroup-name nil
8959                               select-method)))
8960        (t
8961         (let* ((pto-group (gnus-group-prefixed-name
8962                            (car art-group) to-method))
8963                (entry
8964                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8965                (info (nth 2 entry))
8966                (to-group (gnus-info-group info))
8967                to-marks)
8968           ;; Update the group that has been moved to.
8969           (when (and info
8970                      (memq action '(move copy)))
8971             (unless (member to-group to-groups)
8972               (push to-group to-groups))
8973
8974             (unless (memq article gnus-newsgroup-unreads)
8975               (push 'read to-marks)
8976               (gnus-info-set-read
8977                info (gnus-add-to-range (gnus-info-read info)
8978                                        (list (cdr art-group)))))
8979
8980             ;; See whether the article is to be put in the cache.
8981             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8982                              default-marks
8983                            no-expire-marks))
8984                   (to-article (cdr art-group)))
8985
8986               ;; Enter the article into the cache in the new group,
8987               ;; if that is required.
8988               (when gnus-use-cache
8989                 (gnus-cache-possibly-enter-article
8990                  to-group to-article
8991                  (let ((header (copy-sequence
8992                                 (gnus-summary-article-header article))))
8993                    (mail-header-set-number header to-article)
8994                    header)
8995                  (memq article gnus-newsgroup-marked)
8996                  (memq article gnus-newsgroup-dormant)
8997                  (memq article gnus-newsgroup-unreads)))
8998
8999               (when gnus-preserve-marks
9000                 ;; Copy any marks over to the new group.
9001                 (when (and (equal to-group gnus-newsgroup-name)
9002                            (not (memq article gnus-newsgroup-unreads)))
9003                   ;; Mark this article as read in this group.
9004                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9005                   (setcdr (gnus-active to-group) to-article)
9006                   (setcdr gnus-newsgroup-active to-article))
9007
9008                 (while marks
9009                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9010                     (when (memq article (symbol-value
9011                                          (intern (format "gnus-newsgroup-%s"
9012                                                          (caar marks)))))
9013                       (push (cdar marks) to-marks)
9014                       ;; If the other group is the same as this group,
9015                       ;; then we have to add the mark to the list.
9016                       (when (equal to-group gnus-newsgroup-name)
9017                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9018                              (cons to-article
9019                                    (symbol-value
9020                                     (intern (format "gnus-newsgroup-%s"
9021                                                     (caar marks)))))))
9022                       ;; Copy the marks to other group.
9023                       (gnus-add-marked-articles
9024                        to-group (cdar marks) (list to-article) info)))
9025                   (setq marks (cdr marks)))
9026
9027                 (gnus-request-set-mark
9028                  to-group (list (list (list to-article) 'add to-marks))))
9029
9030               (gnus-dribble-enter
9031                (concat "(gnus-group-set-info '"
9032                        (gnus-prin1-to-string (gnus-get-info to-group))
9033                        ")"))))
9034
9035           ;; Update the Xref header in this article to point to
9036           ;; the new crossposted article we have just created.
9037           (when (eq action 'crosspost)
9038             (save-excursion
9039               (set-buffer copy-buf)
9040               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9041               (nnheader-replace-header "Xref" new-xref)
9042               (gnus-request-replace-article
9043                article gnus-newsgroup-name (current-buffer))))
9044
9045           ;; run the move/copy/crosspost/respool hook
9046           (run-hook-with-args 'gnus-summary-article-move-hook 
9047                               action
9048                               (gnus-data-header 
9049                                (assoc article (gnus-data-list nil)))
9050                               gnus-newsgroup-name
9051                               to-newsgroup
9052                               select-method))
9053
9054         ;;;!!!Why is this necessary?
9055         (set-buffer gnus-summary-buffer)
9056         
9057         (gnus-summary-goto-subject article)
9058         (when (eq action 'move)
9059           (gnus-summary-mark-article article gnus-canceled-mark))))
9060       (gnus-summary-remove-process-mark article))
9061     ;; Re-activate all groups that have been moved to.
9062     (save-excursion
9063       (set-buffer gnus-group-buffer)
9064       (let ((gnus-group-marked to-groups))
9065         (gnus-group-get-new-news-this-group nil t)))
9066
9067     (gnus-kill-buffer copy-buf)
9068     (gnus-summary-position-point)
9069     (gnus-set-mode-line 'summary)))
9070
9071 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9072   "Move the current article to a different newsgroup.
9073 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9074 When called interactively, if TO-NEWSGROUP is nil, use the value of
9075 the variable `gnus-move-split-methods' for finding a default target
9076 newsgroup.
9077 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9078 re-spool using this method."
9079   (interactive "P")
9080   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9081
9082 (defun gnus-summary-crosspost-article (&optional n)
9083   "Crosspost the current article to some other group."
9084   (interactive "P")
9085   (gnus-summary-move-article n nil nil 'crosspost))
9086
9087 (defcustom gnus-summary-respool-default-method nil
9088   "Default method type for respooling an article.
9089 If nil, use to the current newsgroup method."
9090   :type 'symbol
9091   :group 'gnus-summary-mail)
9092
9093 (defcustom gnus-summary-display-while-building nil
9094   "If not-nil, show and update the summary buffer as it's being built.
9095 If the value is t, update the buffer after every line is inserted.  If
9096 the value is an integer (N), update the display every N lines."
9097   :group 'gnus-thread
9098   :type '(choice (const :tag "off" nil)
9099                  number
9100                  (const :tag "frequently" t)))
9101
9102 (defun gnus-summary-respool-article (&optional n method)
9103   "Respool the current article.
9104 The article will be squeezed through the mail spooling process again,
9105 which means that it will be put in some mail newsgroup or other
9106 depending on `nnmail-split-methods'.
9107 If N is a positive number, respool the N next articles.
9108 If N is a negative number, respool the N previous articles.
9109 If N is nil and any articles have been marked with the process mark,
9110 respool those articles instead.
9111
9112 Respooling can be done both from mail groups and \"real\" newsgroups.
9113 In the former case, the articles in question will be moved from the
9114 current group into whatever groups they are destined to.  In the
9115 latter case, they will be copied into the relevant groups."
9116   (interactive
9117    (list current-prefix-arg
9118          (let* ((methods (gnus-methods-using 'respool))
9119                 (methname
9120                  (symbol-name (or gnus-summary-respool-default-method
9121                                   (car (gnus-find-method-for-group
9122                                         gnus-newsgroup-name)))))
9123                 (method
9124                  (gnus-completing-read-with-default
9125                   methname "What backend do you want to use when respooling?"
9126                   methods nil t nil 'gnus-mail-method-history))
9127                 ms)
9128            (cond
9129             ((zerop (length (setq ms (gnus-servers-using-backend
9130                                       (intern method)))))
9131              (list (intern method) ""))
9132             ((= 1 (length ms))
9133              (car ms))
9134             (t
9135              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9136                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9137                            ms-alist))))))))
9138   (unless method
9139     (error "No method given for respooling"))
9140   (if (assoc (symbol-name
9141               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9142              (gnus-methods-using 'respool))
9143       (gnus-summary-move-article n nil method)
9144     (gnus-summary-copy-article n nil method)))
9145
9146 (defun gnus-summary-import-article (file &optional edit)
9147   "Import an arbitrary file into a mail newsgroup."
9148   (interactive "fImport file: \nP")
9149   (let ((group gnus-newsgroup-name)
9150         (now (current-time))
9151         atts lines group-art)
9152     (unless (gnus-check-backend-function 'request-accept-article group)
9153       (error "%s does not support article importing" group))
9154     (or (file-readable-p file)
9155         (not (file-regular-p file))
9156         (error "Can't read %s" file))
9157     (save-excursion
9158       (set-buffer (gnus-get-buffer-create " *import file*"))
9159       (erase-buffer)
9160       (nnheader-insert-file-contents file)
9161       (goto-char (point-min))
9162       (if (nnheader-article-p)
9163           (save-restriction
9164             (goto-char (point-min))
9165             (search-forward "\n\n" nil t)
9166             (narrow-to-region (point-min) (1- (point)))
9167             (goto-char (point-min))
9168             (unless (re-search-forward "^date:" nil t)
9169               (goto-char (point-max))
9170               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9171         ;; This doesn't look like an article, so we fudge some headers.
9172         (setq atts (file-attributes file)
9173               lines (count-lines (point-min) (point-max)))
9174         (insert "From: " (read-string "From: ") "\n"
9175                 "Subject: " (read-string "Subject: ") "\n"
9176                 "Date: " (message-make-date (nth 5 atts)) "\n"
9177                 "Message-ID: " (message-make-message-id) "\n"
9178                 "Lines: " (int-to-string lines) "\n"
9179                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9180       (setq group-art (gnus-request-accept-article group nil t))
9181       (kill-buffer (current-buffer)))
9182     (setq gnus-newsgroup-active (gnus-activate-group group))
9183     (forward-line 1)
9184     (gnus-summary-goto-article (cdr group-art) nil t)
9185     (when edit
9186       (gnus-summary-edit-article))))
9187
9188 (defun gnus-summary-create-article ()
9189   "Create an article in a mail newsgroup."
9190   (interactive)
9191   (let ((group gnus-newsgroup-name)
9192         (now (current-time))
9193         group-art)
9194     (unless (gnus-check-backend-function 'request-accept-article group)
9195       (error "%s does not support article importing" group))
9196     (save-excursion
9197       (set-buffer (gnus-get-buffer-create " *import file*"))
9198       (erase-buffer)
9199       (goto-char (point-min))
9200       ;; This doesn't look like an article, so we fudge some headers.
9201       (insert "From: " (read-string "From: ") "\n"
9202               "Subject: " (read-string "Subject: ") "\n"
9203               "Date: " (message-make-date now) "\n"
9204               "Message-ID: " (message-make-message-id) "\n")
9205       (setq group-art (gnus-request-accept-article group nil t))
9206       (kill-buffer (current-buffer)))
9207     (setq gnus-newsgroup-active (gnus-activate-group group))
9208     (forward-line 1)
9209     (gnus-summary-goto-article (cdr group-art) nil t)
9210     (gnus-summary-edit-article)))
9211
9212 (defun gnus-summary-article-posted-p ()
9213   "Say whether the current (mail) article is available from news as well.
9214 This will be the case if the article has both been mailed and posted."
9215   (interactive)
9216   (let ((id (mail-header-references (gnus-summary-article-header)))
9217         (gnus-override-method (car (gnus-refer-article-methods))))
9218     (if (gnus-request-head id "")
9219         (gnus-message 2 "The current message was found on %s"
9220                       gnus-override-method)
9221       (gnus-message 2 "The current message couldn't be found on %s"
9222                     gnus-override-method)
9223       nil)))
9224
9225 (defun gnus-summary-expire-articles (&optional now)
9226   "Expire all articles that are marked as expirable in the current group."
9227   (interactive)
9228   (when (and (not gnus-group-is-exiting-without-update-p)
9229              (gnus-check-backend-function
9230               'request-expire-articles gnus-newsgroup-name))
9231     ;; This backend supports expiry.
9232     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9233            (expirable (if total
9234                           (progn
9235                             ;; We need to update the info for
9236                             ;; this group for `gnus-list-of-read-articles'
9237                             ;; to give us the right answer.
9238                             (gnus-run-hooks 'gnus-exit-group-hook)
9239                             (gnus-summary-update-info)
9240                             (gnus-list-of-read-articles gnus-newsgroup-name))
9241                         (setq gnus-newsgroup-expirable
9242                               (sort gnus-newsgroup-expirable '<))))
9243            (expiry-wait (if now 'immediate
9244                           (gnus-group-find-parameter
9245                            gnus-newsgroup-name 'expiry-wait)))
9246            (nnmail-expiry-target
9247             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9248                 nnmail-expiry-target))
9249            es)
9250       (when expirable
9251         ;; There are expirable articles in this group, so we run them
9252         ;; through the expiry process.
9253         (gnus-message 6 "Expiring articles...")
9254         (unless (gnus-check-group gnus-newsgroup-name)
9255           (error "Can't open server for %s" gnus-newsgroup-name))
9256         ;; The list of articles that weren't expired is returned.
9257         (save-excursion
9258           (if expiry-wait
9259               (let ((nnmail-expiry-wait-function nil)
9260                     (nnmail-expiry-wait expiry-wait))
9261                 (setq es (gnus-request-expire-articles
9262                           expirable gnus-newsgroup-name)))
9263             (setq es (gnus-request-expire-articles
9264                       expirable gnus-newsgroup-name)))
9265           (unless total
9266             (setq gnus-newsgroup-expirable es))
9267           ;; We go through the old list of expirable, and mark all
9268           ;; really expired articles as nonexistent.
9269           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9270             (let ((gnus-use-cache nil))
9271               (dolist (article expirable)
9272                 (when (and (not (memq article es))
9273                            (gnus-data-find article))
9274                   (gnus-summary-mark-article article gnus-canceled-mark)
9275                   (run-hook-with-args 'gnus-summary-article-expire-hook
9276                                       'delete
9277                                       (gnus-data-header
9278                                        (assoc article (gnus-data-list nil)))
9279                                       gnus-newsgroup-name
9280                                       nil
9281                                       nil))))))
9282         (gnus-message 6 "Expiring articles...done")))))
9283
9284 (defun gnus-summary-expire-articles-now ()
9285   "Expunge all expirable articles in the current group.
9286 This means that *all* articles that are marked as expirable will be
9287 deleted forever, right now."
9288   (interactive)
9289   (or gnus-expert-user
9290       (gnus-yes-or-no-p
9291        "Are you really, really, really sure you want to delete all these messages? ")
9292       (error "Phew!"))
9293   (gnus-summary-expire-articles t))
9294
9295 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9296 (defun gnus-summary-delete-article (&optional n)
9297   "Delete the N next (mail) articles.
9298 This command actually deletes articles.  This is not a marking
9299 command.  The article will disappear forever from your life, never to
9300 return.
9301 If N is negative, delete backwards.
9302 If N is nil and articles have been marked with the process mark,
9303 delete these instead."
9304   (interactive "P")
9305   (unless (gnus-check-backend-function 'request-expire-articles
9306                                        gnus-newsgroup-name)
9307     (error "The current newsgroup does not support article deletion"))
9308   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9309     (error "Couldn't open server"))
9310   ;; Compute the list of articles to delete.
9311   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9312         (nnmail-expiry-target 'delete)
9313         not-deleted)
9314     (if (and gnus-novice-user
9315              (not (gnus-yes-or-no-p
9316                    (format "Do you really want to delete %s forever? "
9317                            (if (> (length articles) 1)
9318                                (format "these %s articles" (length articles))
9319                              "this article")))))
9320         ()
9321       ;; Delete the articles.
9322       (setq not-deleted (gnus-request-expire-articles
9323                          articles gnus-newsgroup-name 'force))
9324       (while articles
9325         (gnus-summary-remove-process-mark (car articles))
9326         ;; The backend might not have been able to delete the article
9327         ;; after all.
9328         (unless (memq (car articles) not-deleted)
9329           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9330         (let* ((article (car articles))
9331                (id (mail-header-id (gnus-data-header 
9332                                     (assoc article (gnus-data-list nil))))))
9333           (run-hook-with-args 'gnus-summary-article-delete-hook
9334                               'delete id gnus-newsgroup-name nil
9335                               nil))
9336         (setq articles (cdr articles)))
9337       (when not-deleted
9338         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9339     (gnus-summary-position-point)
9340     (gnus-set-mode-line 'summary)
9341     not-deleted))
9342
9343 (defun gnus-summary-edit-article (&optional force)
9344   "Edit the current article.
9345 This will have permanent effect only in mail groups.
9346 If FORCE is non-nil, allow editing of articles even in read-only
9347 groups."
9348   (interactive "P")
9349   (save-excursion
9350     (set-buffer gnus-summary-buffer)
9351     (let ((mail-parse-charset gnus-newsgroup-charset)
9352           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9353       (gnus-set-global-variables)
9354       (when (and (not force)
9355                  (gnus-group-read-only-p))
9356         (error "The current newsgroup does not support article editing"))
9357       (gnus-summary-show-article t)
9358       (gnus-article-edit-article
9359        'ignore
9360        `(lambda (no-highlight)
9361           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9362                 (message-options message-options)
9363                 (message-options-set-recipient)
9364                 (mail-parse-ignored-charsets
9365                  ',gnus-newsgroup-ignored-charsets))
9366             (gnus-summary-edit-article-done
9367              ,(or (mail-header-references gnus-current-headers) "")
9368              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9369
9370 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9371
9372 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9373                                                  no-highlight)
9374   "Make edits to the current article permanent."
9375   (interactive)
9376   (save-excursion
9377     ;; The buffer restriction contains the entire article if it exists.
9378     (when (article-goto-body)
9379       (let ((lines (count-lines (point) (point-max)))
9380             (length (- (point-max) (point)))
9381             (case-fold-search t)
9382             (body (copy-marker (point))))
9383         (goto-char (point-min))
9384         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9385           (delete-region (match-beginning 1) (match-end 1))
9386           (insert (number-to-string length)))
9387         (goto-char (point-min))
9388         (when (re-search-forward
9389                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9390           (delete-region (match-beginning 1) (match-end 1))
9391           (insert (number-to-string length)))
9392         (goto-char (point-min))
9393         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9394           (delete-region (match-beginning 1) (match-end 1))
9395           (insert (number-to-string lines))))))
9396   ;; Replace the article.
9397   (let ((buf (current-buffer)))
9398     (with-temp-buffer
9399       (insert-buffer-substring buf)
9400
9401       (if (and (not read-only)
9402                (not (gnus-request-replace-article
9403                      (cdr gnus-article-current) (car gnus-article-current)
9404                      (current-buffer) t)))
9405           (error "Couldn't replace article")
9406         ;; Update the summary buffer.
9407         (if (and references
9408                  (equal (message-tokenize-header references " ")
9409                         (message-tokenize-header
9410                          (or (message-fetch-field "references") "") " ")))
9411             ;; We only have to update this line.
9412             (save-excursion
9413               (save-restriction
9414                 (message-narrow-to-head)
9415                 (let ((head (buffer-string))
9416                       header)
9417                   (with-temp-buffer
9418                     (insert (format "211 %d Article retrieved.\n"
9419                                     (cdr gnus-article-current)))
9420                     (insert head)
9421                     (insert ".\n")
9422                     (let ((nntp-server-buffer (current-buffer)))
9423                       (setq header (car (gnus-get-newsgroup-headers
9424                                          nil t))))
9425                     (save-excursion
9426                       (set-buffer gnus-summary-buffer)
9427                       (gnus-data-set-header
9428                        (gnus-data-find (cdr gnus-article-current))
9429                        header)
9430                       (gnus-summary-update-article-line
9431                        (cdr gnus-article-current) header)
9432                       (if (gnus-summary-goto-subject
9433                            (cdr gnus-article-current) nil t)
9434                           (gnus-summary-update-secondary-mark
9435                            (cdr gnus-article-current))))))))
9436           ;; Update threads.
9437           (set-buffer (or buffer gnus-summary-buffer))
9438           (gnus-summary-update-article (cdr gnus-article-current))
9439           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9440               (gnus-summary-update-secondary-mark
9441                (cdr gnus-article-current))))
9442         ;; Prettify the article buffer again.
9443         (unless no-highlight
9444           (save-excursion
9445             (set-buffer gnus-article-buffer)
9446             ;;;!!! Fix this -- article should be rehighlighted.
9447             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9448             (set-buffer gnus-original-article-buffer)
9449             (gnus-request-article
9450              (cdr gnus-article-current)
9451              (car gnus-article-current) (current-buffer))))
9452         ;; Prettify the summary buffer line.
9453         (when (gnus-visual-p 'summary-highlight 'highlight)
9454           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9455
9456 (defun gnus-summary-edit-wash (key)
9457   "Perform editing command KEY in the article buffer."
9458   (interactive
9459    (list
9460     (progn
9461       (message "%s" (concat (this-command-keys) "- "))
9462       (read-char))))
9463   (message "")
9464   (gnus-summary-edit-article)
9465   (execute-kbd-macro (concat (this-command-keys) key))
9466   (gnus-article-edit-done))
9467
9468 ;;; Respooling
9469
9470 (defun gnus-summary-respool-query (&optional silent trace)
9471   "Query where the respool algorithm would put this article."
9472   (interactive)
9473   (let (gnus-mark-article-hook)
9474     (gnus-summary-select-article)
9475     (save-excursion
9476       (set-buffer gnus-original-article-buffer)
9477       (save-restriction
9478         (message-narrow-to-head)
9479         (let ((groups (nnmail-article-group 'identity trace)))
9480           (unless silent
9481             (if groups
9482                 (message "This message would go to %s"
9483                          (mapconcat 'car groups ", "))
9484               (message "This message would go to no groups"))
9485             groups))))))
9486
9487 (defun gnus-summary-respool-trace ()
9488   "Trace where the respool algorithm would put this article.
9489 Display a buffer showing all fancy splitting patterns which matched."
9490   (interactive)
9491   (gnus-summary-respool-query nil t))
9492
9493 ;; Summary marking commands.
9494
9495 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9496   "Mark articles which has the same subject as read, and then select the next.
9497 If UNMARK is positive, remove any kind of mark.
9498 If UNMARK is negative, tick articles."
9499   (interactive "P")
9500   (when unmark
9501     (setq unmark (prefix-numeric-value unmark)))
9502   (let ((count
9503          (gnus-summary-mark-same-subject
9504           (gnus-summary-article-subject) unmark)))
9505     ;; Select next unread article.  If auto-select-same mode, should
9506     ;; select the first unread article.
9507     (gnus-summary-next-article t (and gnus-auto-select-same
9508                                       (gnus-summary-article-subject)))
9509     (gnus-message 7 "%d article%s marked as %s"
9510                   count (if (= count 1) " is" "s are")
9511                   (if unmark "unread" "read"))))
9512
9513 (defun gnus-summary-kill-same-subject (&optional unmark)
9514   "Mark articles which has the same subject as read.
9515 If UNMARK is positive, remove any kind of mark.
9516 If UNMARK is negative, tick articles."
9517   (interactive "P")
9518   (when unmark
9519     (setq unmark (prefix-numeric-value unmark)))
9520   (let ((count
9521          (gnus-summary-mark-same-subject
9522           (gnus-summary-article-subject) unmark)))
9523     ;; If marked as read, go to next unread subject.
9524     (when (null unmark)
9525       ;; Go to next unread subject.
9526       (gnus-summary-next-subject 1 t))
9527     (gnus-message 7 "%d articles are marked as %s"
9528                   count (if unmark "unread" "read"))))
9529
9530 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9531   "Mark articles with same SUBJECT as read, and return marked number.
9532 If optional argument UNMARK is positive, remove any kinds of marks.
9533 If optional argument UNMARK is negative, mark articles as unread instead."
9534   (let ((count 1))
9535     (save-excursion
9536       (cond
9537        ((null unmark)                   ; Mark as read.
9538         (while (and
9539                 (progn
9540                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9541                   (gnus-summary-show-thread) t)
9542                 (gnus-summary-find-subject subject))
9543           (setq count (1+ count))))
9544        ((> unmark 0)                    ; Tick.
9545         (while (and
9546                 (progn
9547                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9548                   (gnus-summary-show-thread) t)
9549                 (gnus-summary-find-subject subject))
9550           (setq count (1+ count))))
9551        (t                               ; Mark as unread.
9552         (while (and
9553                 (progn
9554                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9555                   (gnus-summary-show-thread) t)
9556                 (gnus-summary-find-subject subject))
9557           (setq count (1+ count)))))
9558       (gnus-set-mode-line 'summary)
9559       ;; Return the number of marked articles.
9560       count)))
9561
9562 (defun gnus-summary-mark-as-processable (n &optional unmark)
9563   "Set the process mark on the next N articles.
9564 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9565 the process mark instead.  The difference between N and the actual
9566 number of articles marked is returned."
9567   (interactive "P")
9568   (if (and (null n) (gnus-region-active-p))
9569       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9570     (setq n (prefix-numeric-value n))
9571     (let ((backward (< n 0))
9572           (n (abs n)))
9573       (while (and
9574               (> n 0)
9575               (if unmark
9576                   (gnus-summary-remove-process-mark
9577                    (gnus-summary-article-number))
9578                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9579               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9580         (setq n (1- n)))
9581       (when (/= 0 n)
9582         (gnus-message 7 "No more articles"))
9583       (gnus-summary-recenter)
9584       (gnus-summary-position-point)
9585       n)))
9586
9587 (defun gnus-summary-unmark-as-processable (n)
9588   "Remove the process mark from the next N articles.
9589 If N is negative, unmark backward instead.  The difference between N and
9590 the actual number of articles unmarked is returned."
9591   (interactive "P")
9592   (gnus-summary-mark-as-processable n t))
9593
9594 (defun gnus-summary-unmark-all-processable ()
9595   "Remove the process mark from all articles."
9596   (interactive)
9597   (save-excursion
9598     (while gnus-newsgroup-processable
9599       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9600   (gnus-summary-position-point))
9601
9602 (defun gnus-summary-add-mark (article type)
9603   "Mark ARTICLE with a mark of TYPE."
9604   (let ((vtype (car (assq type gnus-article-mark-lists)))
9605         var)
9606     (if (not vtype)
9607         (error "No such mark type: %s" type)
9608       (setq var (intern (format "gnus-newsgroup-%s" type)))
9609       (set var (cons article (symbol-value var)))
9610       (if (memq type '(processable cached replied forwarded recent saved))
9611           (gnus-summary-update-secondary-mark article)
9612         ;;; !!! This is bogus.  We should find out what primary
9613         ;;; !!! mark we want to set.
9614         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9615
9616 (defun gnus-summary-mark-as-expirable (n)
9617   "Mark N articles forward as expirable.
9618 If N is negative, mark backward instead.  The difference between N and
9619 the actual number of articles marked is returned."
9620   (interactive "p")
9621   (gnus-summary-mark-forward n gnus-expirable-mark))
9622
9623 (defun gnus-summary-mark-as-spam (n)
9624   "Mark N articles forward as spam.
9625 If N is negative, mark backward instead.  The difference between N and
9626 the actual number of articles marked is returned."
9627   (interactive "p")
9628   (gnus-summary-mark-forward n gnus-spam-mark))
9629
9630 (defun gnus-summary-mark-article-as-replied (article)
9631   "Mark ARTICLE as replied to and update the summary line.
9632 ARTICLE can also be a list of articles."
9633   (interactive (list (gnus-summary-article-number)))
9634   (let ((articles (if (listp article) article (list article))))
9635     (dolist (article articles)
9636       (unless (numberp article)
9637         (error "%s is not a number" article))
9638       (push article gnus-newsgroup-replied)
9639       (let ((buffer-read-only nil))
9640         (when (gnus-summary-goto-subject article nil t)
9641           (gnus-summary-update-secondary-mark article))))))
9642
9643 (defun gnus-summary-mark-article-as-forwarded (article)
9644   "Mark ARTICLE as forwarded and update the summary line.
9645 ARTICLE can also be a list of articles."
9646   (let ((articles (if (listp article) article (list article))))
9647     (dolist (article articles)
9648       (push article gnus-newsgroup-forwarded)
9649       (let ((buffer-read-only nil))
9650         (when (gnus-summary-goto-subject article nil t)
9651           (gnus-summary-update-secondary-mark article))))))
9652
9653 (defun gnus-summary-set-bookmark (article)
9654   "Set a bookmark in current article."
9655   (interactive (list (gnus-summary-article-number)))
9656   (when (or (not (get-buffer gnus-article-buffer))
9657             (not gnus-current-article)
9658             (not gnus-article-current)
9659             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9660     (error "No current article selected"))
9661   ;; Remove old bookmark, if one exists.
9662   (gnus-pull article gnus-newsgroup-bookmarks)
9663   ;; Set the new bookmark, which is on the form
9664   ;; (article-number . line-number-in-body).
9665   (push
9666    (cons article
9667          (save-excursion
9668            (set-buffer gnus-article-buffer)
9669            (count-lines
9670             (min (point)
9671                  (save-excursion
9672                    (article-goto-body)
9673                    (point)))
9674             (point))))
9675    gnus-newsgroup-bookmarks)
9676   (gnus-message 6 "A bookmark has been added to the current article."))
9677
9678 (defun gnus-summary-remove-bookmark (article)
9679   "Remove the bookmark from the current article."
9680   (interactive (list (gnus-summary-article-number)))
9681   ;; Remove old bookmark, if one exists.
9682   (if (not (assq article gnus-newsgroup-bookmarks))
9683       (gnus-message 6 "No bookmark in current article.")
9684     (gnus-pull article gnus-newsgroup-bookmarks)
9685     (gnus-message 6 "Removed bookmark.")))
9686
9687 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9688 (defun gnus-summary-mark-as-dormant (n)
9689   "Mark N articles forward as dormant.
9690 If N is negative, mark backward instead.  The difference between N and
9691 the actual number of articles marked is returned."
9692   (interactive "p")
9693   (gnus-summary-mark-forward n gnus-dormant-mark))
9694
9695 (defun gnus-summary-set-process-mark (article)
9696   "Set the process mark on ARTICLE and update the summary line."
9697   (setq gnus-newsgroup-processable
9698         (cons article
9699               (delq article gnus-newsgroup-processable)))
9700   (when (gnus-summary-goto-subject article)
9701     (gnus-summary-show-thread)
9702     (gnus-summary-goto-subject article)
9703     (gnus-summary-update-secondary-mark article)))
9704
9705 (defun gnus-summary-remove-process-mark (article)
9706   "Remove the process mark from ARTICLE and update the summary line."
9707   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9708   (when (gnus-summary-goto-subject article)
9709     (gnus-summary-show-thread)
9710     (gnus-summary-goto-subject article)
9711     (gnus-summary-update-secondary-mark article)))
9712
9713 (defun gnus-summary-set-saved-mark (article)
9714   "Set the process mark on ARTICLE and update the summary line."
9715   (push article gnus-newsgroup-saved)
9716   (when (gnus-summary-goto-subject article)
9717     (gnus-summary-update-secondary-mark article)))
9718
9719 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9720   "Mark N articles as read forwards.
9721 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9722 The difference between N and the actual number of articles marked is
9723 returned.
9724 If NO-EXPIRE, auto-expiry will be inhibited."
9725   (interactive "p")
9726   (gnus-summary-show-thread)
9727   (let ((backward (< n 0))
9728         (gnus-summary-goto-unread
9729          (and gnus-summary-goto-unread
9730               (not (eq gnus-summary-goto-unread 'never))
9731               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9732                                     gnus-ticked-mark gnus-dormant-mark)))))
9733         (n (abs n))
9734         (mark (or mark gnus-del-mark)))
9735     (while (and (> n 0)
9736                 (gnus-summary-mark-article nil mark no-expire)
9737                 (zerop (gnus-summary-next-subject
9738                         (if backward -1 1)
9739                         (and gnus-summary-goto-unread
9740                              (not (eq gnus-summary-goto-unread 'never)))
9741                         t)))
9742       (setq n (1- n)))
9743     (when (/= 0 n)
9744       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9745     (gnus-summary-recenter)
9746     (gnus-summary-position-point)
9747     (gnus-set-mode-line 'summary)
9748     n))
9749
9750 (defun gnus-summary-mark-article-as-read (mark)
9751   "Mark the current article quickly as read with MARK."
9752   (let ((article (gnus-summary-article-number)))
9753     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9754     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9755     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9756     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9757     (push (cons article mark) gnus-newsgroup-reads)
9758     ;; Possibly remove from cache, if that is used.
9759     (when gnus-use-cache
9760       (gnus-cache-enter-remove-article article))
9761     ;; Allow the backend to change the mark.
9762     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9763     ;; Check for auto-expiry.
9764     (when (and gnus-newsgroup-auto-expire
9765                (memq mark gnus-auto-expirable-marks))
9766       (setq mark gnus-expirable-mark)
9767       ;; Let the backend know about the mark change.
9768       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9769       (push article gnus-newsgroup-expirable))
9770     ;; Set the mark in the buffer.
9771     (gnus-summary-update-mark mark 'unread)
9772     t))
9773
9774 (defun gnus-summary-mark-article-as-unread (mark)
9775   "Mark the current article quickly as unread with MARK."
9776   (let* ((article (gnus-summary-article-number))
9777          (old-mark (gnus-summary-article-mark article)))
9778     ;; Allow the backend to change the mark.
9779     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9780     (if (eq mark old-mark)
9781         t
9782       (if (<= article 0)
9783           (progn
9784             (gnus-error 1 "Can't mark negative article numbers")
9785             nil)
9786         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9787         (setq gnus-newsgroup-spam-marked
9788               (delq article gnus-newsgroup-spam-marked))
9789         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9790         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9791         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9792         (cond ((= mark gnus-ticked-mark)
9793                (setq gnus-newsgroup-marked
9794                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9795                                               article)))
9796               ((= mark gnus-spam-mark)
9797                (setq gnus-newsgroup-spam-marked
9798                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9799                                               article)))
9800               ((= mark gnus-dormant-mark)
9801                (setq gnus-newsgroup-dormant
9802                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9803                                               article)))
9804               (t
9805                (setq gnus-newsgroup-unreads
9806                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9807                                               article))))
9808         (gnus-pull article gnus-newsgroup-reads)
9809
9810         ;; See whether the article is to be put in the cache.
9811         (and gnus-use-cache
9812              (vectorp (gnus-summary-article-header article))
9813              (save-excursion
9814                (gnus-cache-possibly-enter-article
9815                 gnus-newsgroup-name article
9816                 (gnus-summary-article-header article)
9817                 (= mark gnus-ticked-mark)
9818                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9819
9820         ;; Fix the mark.
9821         (gnus-summary-update-mark mark 'unread)
9822         t))))
9823
9824 (defun gnus-summary-mark-article (&optional article mark no-expire)
9825   "Mark ARTICLE with MARK.  MARK can be any character.
9826 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9827 `??' (dormant) and `?E' (expirable).
9828 If MARK is nil, then the default character `?r' is used.
9829 If ARTICLE is nil, then the article on the current line will be
9830 marked.
9831 If NO-EXPIRE, auto-expiry will be inhibited."
9832   ;; The mark might be a string.
9833   (when (stringp mark)
9834     (setq mark (aref mark 0)))
9835   ;; If no mark is given, then we check auto-expiring.
9836   (when (null mark)
9837     (setq mark gnus-del-mark))
9838   (when (and (not no-expire)
9839              gnus-newsgroup-auto-expire
9840              (memq mark gnus-auto-expirable-marks))
9841     (setq mark gnus-expirable-mark))
9842   (let ((article (or article (gnus-summary-article-number)))
9843         (old-mark (gnus-summary-article-mark article)))
9844     ;; Allow the backend to change the mark.
9845     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9846     (if (eq mark old-mark)
9847         t
9848       (unless article
9849         (error "No article on current line"))
9850       (if (not (if (or (= mark gnus-unread-mark)
9851                        (= mark gnus-ticked-mark)
9852                        (= mark gnus-spam-mark)
9853                        (= mark gnus-dormant-mark))
9854                    (gnus-mark-article-as-unread article mark)
9855                  (gnus-mark-article-as-read article mark)))
9856           t
9857         ;; See whether the article is to be put in the cache.
9858         (and gnus-use-cache
9859              (not (= mark gnus-canceled-mark))
9860              (vectorp (gnus-summary-article-header article))
9861              (save-excursion
9862                (gnus-cache-possibly-enter-article
9863                 gnus-newsgroup-name article
9864                 (gnus-summary-article-header article)
9865                 (= mark gnus-ticked-mark)
9866                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9867
9868         (when (gnus-summary-goto-subject article nil t)
9869           (let ((buffer-read-only nil))
9870             (gnus-summary-show-thread)
9871             ;; Fix the mark.
9872             (gnus-summary-update-mark mark 'unread)
9873             t))))))
9874
9875 (defun gnus-summary-update-secondary-mark (article)
9876   "Update the secondary (read, process, cache) mark."
9877   (gnus-summary-update-mark
9878    (cond ((memq article gnus-newsgroup-processable)
9879           gnus-process-mark)
9880          ((memq article gnus-newsgroup-cached)
9881           gnus-cached-mark)
9882          ((memq article gnus-newsgroup-replied)
9883           gnus-replied-mark)
9884          ((memq article gnus-newsgroup-forwarded)
9885           gnus-forwarded-mark)
9886          ((memq article gnus-newsgroup-saved)
9887           gnus-saved-mark)
9888          ((memq article gnus-newsgroup-recent)
9889           gnus-recent-mark)
9890          ((memq article gnus-newsgroup-unseen)
9891           gnus-unseen-mark)
9892          (t gnus-no-mark))
9893    'replied)
9894   (when (gnus-visual-p 'summary-highlight 'highlight)
9895     (gnus-run-hooks 'gnus-summary-update-hook))
9896   t)
9897
9898 (defun gnus-summary-update-download-mark (article)
9899   "Update the download mark."
9900   (gnus-summary-update-mark
9901    (cond ((memq article gnus-newsgroup-undownloaded) 
9902           gnus-undownloaded-mark)
9903          (gnus-newsgroup-agentized
9904           gnus-downloaded-mark)
9905          (t
9906           gnus-no-mark))
9907    'download)
9908   (gnus-summary-update-line t)
9909   t)
9910
9911 (defun gnus-summary-update-mark (mark type)
9912   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9913         (buffer-read-only nil))
9914     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9915     (when forward
9916       (when (looking-at "\r")
9917         (incf forward))
9918       (when (<= (+ forward (point)) (point-max))
9919         ;; Go to the right position on the line.
9920         (goto-char (+ forward (point)))
9921         ;; Replace the old mark with the new mark.
9922         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9923         ;; Optionally update the marks by some user rule.
9924         (when (eq type 'unread)
9925           (gnus-data-set-mark
9926            (gnus-data-find (gnus-summary-article-number)) mark)
9927           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9928
9929 (defun gnus-mark-article-as-read (article &optional mark)
9930   "Enter ARTICLE in the pertinent lists and remove it from others."
9931   ;; Make the article expirable.
9932   (let ((mark (or mark gnus-del-mark)))
9933     (setq gnus-newsgroup-expirable
9934           (if (= mark gnus-expirable-mark)
9935               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9936             (delq article gnus-newsgroup-expirable)))
9937     ;; Remove from unread and marked lists.
9938     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9939     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9940     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9941     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9942     (push (cons article mark) gnus-newsgroup-reads)
9943     ;; Possibly remove from cache, if that is used.
9944     (when gnus-use-cache
9945       (gnus-cache-enter-remove-article article))
9946     t))
9947
9948 (defun gnus-mark-article-as-unread (article &optional mark)
9949   "Enter ARTICLE in the pertinent lists and remove it from others."
9950   (let ((mark (or mark gnus-ticked-mark)))
9951     (if (<= article 0)
9952         (progn
9953           (gnus-error 1 "Can't mark negative article numbers")
9954           nil)
9955       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9956             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9957             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9958             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9959             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9960
9961       ;; Unsuppress duplicates?
9962       (when gnus-suppress-duplicates
9963         (gnus-dup-unsuppress-article article))
9964
9965       (cond ((= mark gnus-ticked-mark)
9966              (setq gnus-newsgroup-marked
9967                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9968             ((= mark gnus-spam-mark)
9969              (setq gnus-newsgroup-spam-marked
9970                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9971                                             article)))
9972             ((= mark gnus-dormant-mark)
9973              (setq gnus-newsgroup-dormant
9974                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9975             (t
9976              (setq gnus-newsgroup-unreads
9977                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9978       (gnus-pull article gnus-newsgroup-reads)
9979       t)))
9980
9981 (defalias 'gnus-summary-mark-as-unread-forward
9982   'gnus-summary-tick-article-forward)
9983 (make-obsolete 'gnus-summary-mark-as-unread-forward
9984                'gnus-summary-tick-article-forward)
9985 (defun gnus-summary-tick-article-forward (n)
9986   "Tick N articles forwards.
9987 If N is negative, tick backwards instead.
9988 The difference between N and the number of articles ticked is returned."
9989   (interactive "p")
9990   (gnus-summary-mark-forward n gnus-ticked-mark))
9991
9992 (defalias 'gnus-summary-mark-as-unread-backward
9993   'gnus-summary-tick-article-backward)
9994 (make-obsolete 'gnus-summary-mark-as-unread-backward
9995                'gnus-summary-tick-article-backward)
9996 (defun gnus-summary-tick-article-backward (n)
9997   "Tick N articles backwards.
9998 The difference between N and the number of articles ticked is returned."
9999   (interactive "p")
10000   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10001
10002 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10003 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10004 (defun gnus-summary-tick-article (&optional article clear-mark)
10005   "Mark current article as unread.
10006 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10007 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10008   (interactive)
10009   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10010                                        gnus-ticked-mark)))
10011
10012 (defun gnus-summary-mark-as-read-forward (n)
10013   "Mark N articles as read forwards.
10014 If N is negative, mark backwards instead.
10015 The difference between N and the actual number of articles marked is
10016 returned."
10017   (interactive "p")
10018   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10019
10020 (defun gnus-summary-mark-as-read-backward (n)
10021   "Mark the N articles as read backwards.
10022 The difference between N and the actual number of articles marked is
10023 returned."
10024   (interactive "p")
10025   (gnus-summary-mark-forward
10026    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10027
10028 (defun gnus-summary-mark-as-read (&optional article mark)
10029   "Mark current article as read.
10030 ARTICLE specifies the article to be marked as read.
10031 MARK specifies a string to be inserted at the beginning of the line."
10032   (gnus-summary-mark-article article mark))
10033
10034 (defun gnus-summary-clear-mark-forward (n)
10035   "Clear marks from N articles forward.
10036 If N is negative, clear backward instead.
10037 The difference between N and the number of marks cleared is returned."
10038   (interactive "p")
10039   (gnus-summary-mark-forward n gnus-unread-mark))
10040
10041 (defun gnus-summary-clear-mark-backward (n)
10042   "Clear marks from N articles backward.
10043 The difference between N and the number of marks cleared is returned."
10044   (interactive "p")
10045   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10046
10047 (defun gnus-summary-mark-unread-as-read ()
10048   "Intended to be used by `gnus-summary-mark-article-hook'."
10049   (when (memq gnus-current-article gnus-newsgroup-unreads)
10050     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10051
10052 (defun gnus-summary-mark-read-and-unread-as-read ()
10053   "Intended to be used by `gnus-summary-mark-article-hook'."
10054   (let ((mark (gnus-summary-article-mark)))
10055     (when (or (gnus-unread-mark-p mark)
10056               (gnus-read-mark-p mark))
10057       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
10058
10059 (defun gnus-summary-mark-unread-as-ticked ()
10060   "Intended to be used by `gnus-summary-mark-article-hook'."
10061   (when (memq gnus-current-article gnus-newsgroup-unreads)
10062     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10063
10064 (defun gnus-summary-mark-region-as-read (point mark all)
10065   "Mark all unread articles between point and mark as read.
10066 If given a prefix, mark all articles between point and mark as read,
10067 even ticked and dormant ones."
10068   (interactive "r\nP")
10069   (save-excursion
10070     (let (article)
10071       (goto-char point)
10072       (beginning-of-line)
10073       (while (and
10074               (< (point) mark)
10075               (progn
10076                 (when (or all
10077                           (memq (setq article (gnus-summary-article-number))
10078                                 gnus-newsgroup-unreads))
10079                   (gnus-summary-mark-article article gnus-del-mark))
10080                 t)
10081               (gnus-summary-find-next))))))
10082
10083 (defun gnus-summary-mark-below (score mark)
10084   "Mark articles with score less than SCORE with MARK."
10085   (interactive "P\ncMark: ")
10086   (setq score (if score
10087                   (prefix-numeric-value score)
10088                 (or gnus-summary-default-score 0)))
10089   (save-excursion
10090     (set-buffer gnus-summary-buffer)
10091     (goto-char (point-min))
10092     (while
10093         (progn
10094           (and (< (gnus-summary-article-score) score)
10095                (gnus-summary-mark-article nil mark))
10096           (gnus-summary-find-next)))))
10097
10098 (defun gnus-summary-kill-below (&optional score)
10099   "Mark articles with score below SCORE as read."
10100   (interactive "P")
10101   (gnus-summary-mark-below score gnus-killed-mark))
10102
10103 (defun gnus-summary-clear-above (&optional score)
10104   "Clear all marks from articles with score above SCORE."
10105   (interactive "P")
10106   (gnus-summary-mark-above score gnus-unread-mark))
10107
10108 (defun gnus-summary-tick-above (&optional score)
10109   "Tick all articles with score above SCORE."
10110   (interactive "P")
10111   (gnus-summary-mark-above score gnus-ticked-mark))
10112
10113 (defun gnus-summary-mark-above (score mark)
10114   "Mark articles with score over SCORE with MARK."
10115   (interactive "P\ncMark: ")
10116   (setq score (if score
10117                   (prefix-numeric-value score)
10118                 (or gnus-summary-default-score 0)))
10119   (save-excursion
10120     (set-buffer gnus-summary-buffer)
10121     (goto-char (point-min))
10122     (while (and (progn
10123                   (when (> (gnus-summary-article-score) score)
10124                     (gnus-summary-mark-article nil mark))
10125                   t)
10126                 (gnus-summary-find-next)))))
10127
10128 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10129 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10130 (defun gnus-summary-limit-include-expunged (&optional no-error)
10131   "Display all the hidden articles that were expunged for low scores."
10132   (interactive)
10133   (let ((buffer-read-only nil))
10134     (let ((scored gnus-newsgroup-scored)
10135           headers h)
10136       (while scored
10137         (unless (gnus-summary-article-header (caar scored))
10138           (and (setq h (gnus-number-to-header (caar scored)))
10139                (< (cdar scored) gnus-summary-expunge-below)
10140                (push h headers)))
10141         (setq scored (cdr scored)))
10142       (if (not headers)
10143           (when (not no-error)
10144             (error "No expunged articles hidden"))
10145         (goto-char (point-min))
10146         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10147         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10148         (mapcar (lambda (x) (push (mail-header-number x)
10149                                   gnus-newsgroup-limit))
10150                 headers)
10151         (gnus-summary-prepare-unthreaded (nreverse headers))
10152         (goto-char (point-min))
10153         (gnus-summary-position-point)
10154         t))))
10155
10156 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10157   "Mark all unread articles in this newsgroup as read.
10158 If prefix argument ALL is non-nil, ticked and dormant articles will
10159 also be marked as read.
10160 If QUIETLY is non-nil, no questions will be asked.
10161 If TO-HERE is non-nil, it should be a point in the buffer.  All
10162 articles before (after, if REVERSE is set) this point will be marked as read.
10163 Note that this function will only catch up the unread article
10164 in the current summary buffer limitation.
10165 The number of articles marked as read is returned."
10166   (interactive "P")
10167   (prog1
10168       (save-excursion
10169         (when (or quietly
10170                   (not gnus-interactive-catchup) ;Without confirmation?
10171                   gnus-expert-user
10172                   (gnus-y-or-n-p
10173                    (if all
10174                        "Mark absolutely all articles as read? "
10175                      "Mark all unread articles as read? ")))
10176           (if (and not-mark
10177                    (not gnus-newsgroup-adaptive)
10178                    (not gnus-newsgroup-auto-expire)
10179                    (not gnus-suppress-duplicates)
10180                    (or (not gnus-use-cache)
10181                        (eq gnus-use-cache 'passive)))
10182               (progn
10183                 (when all
10184                   (setq gnus-newsgroup-marked nil
10185                         gnus-newsgroup-spam-marked nil
10186                         gnus-newsgroup-dormant nil))
10187                 (setq gnus-newsgroup-unreads
10188                       (gnus-sorted-nunion
10189                        (gnus-intersection gnus-newsgroup-unreads
10190                                           gnus-newsgroup-downloadable)
10191                        gnus-newsgroup-unfetched)))
10192             ;; We actually mark all articles as canceled, which we
10193             ;; have to do when using auto-expiry or adaptive scoring.
10194             (gnus-summary-show-all-threads)
10195             (if (and to-here reverse)
10196                 (progn
10197                   (goto-char to-here)
10198                   (while (and
10199                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10200                           (gnus-summary-find-next (not all)))))
10201               (when (gnus-summary-first-subject (not all))
10202                 (while (and
10203                         (if to-here (< (point) to-here) t)
10204                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10205                         (gnus-summary-find-next (not all))))))
10206             (gnus-set-mode-line 'summary))
10207           t))
10208     (gnus-summary-position-point)))
10209
10210 (defun gnus-summary-catchup-to-here (&optional all)
10211   "Mark all unticked articles before the current one as read.
10212 If ALL is non-nil, also mark ticked and dormant articles as read."
10213   (interactive "P")
10214   (save-excursion
10215     (gnus-save-hidden-threads
10216       (let ((beg (point)))
10217         ;; We check that there are unread articles.
10218         (when (or all (gnus-summary-find-prev))
10219           (gnus-summary-catchup all t beg)))))
10220   (gnus-summary-position-point))
10221
10222 (defun gnus-summary-catchup-from-here (&optional all)
10223   "Mark all unticked articles after the current one as read.
10224 If ALL is non-nil, also mark ticked and dormant articles as read."
10225   (interactive "P")
10226   (save-excursion
10227     (gnus-save-hidden-threads
10228       (let ((beg (point)))
10229         ;; We check that there are unread articles.
10230         (when (or all (gnus-summary-find-next))
10231           (gnus-summary-catchup all t beg nil t)))))
10232   (gnus-summary-position-point))
10233
10234 (defun gnus-summary-catchup-all (&optional quietly)
10235   "Mark all articles in this newsgroup as read.
10236 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10237 instead, which marks only unread articles as read."
10238   (interactive "P")
10239   (gnus-summary-catchup t quietly))
10240
10241 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10242   "Mark all unread articles in this group as read, then exit.
10243 If prefix argument ALL is non-nil, all articles are marked as read.
10244 If QUIETLY is non-nil, no questions will be asked."
10245   (interactive "P")
10246   (when (gnus-summary-catchup all quietly nil 'fast)
10247     ;; Select next newsgroup or exit.
10248     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10249              (eq gnus-auto-select-next 'quietly))
10250         (gnus-summary-next-group nil)
10251       (gnus-summary-exit))))
10252
10253 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10254   "Mark all articles in this newsgroup as read, and then exit.
10255 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10256 instead, which marks only unread articles as read."
10257   (interactive "P")
10258   (gnus-summary-catchup-and-exit t quietly))
10259
10260 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10261   "Mark all articles in this group as read and select the next group.
10262 If given a prefix, mark all articles, unread as well as ticked, as
10263 read."
10264   (interactive "P")
10265   (save-excursion
10266     (gnus-summary-catchup all))
10267   (gnus-summary-next-group))
10268
10269 ;;;
10270 ;;; with article
10271 ;;;
10272
10273 (defmacro gnus-with-article (article &rest forms)
10274   "Select ARTICLE and perform FORMS in the original article buffer.
10275 Then replace the article with the result."
10276   `(progn
10277      ;; We don't want the article to be marked as read.
10278      (let (gnus-mark-article-hook)
10279        (gnus-summary-select-article t t nil ,article))
10280      (set-buffer gnus-original-article-buffer)
10281      ,@forms
10282      (if (not (gnus-check-backend-function
10283                'request-replace-article (car gnus-article-current)))
10284          (gnus-message 5 "Read-only group; not replacing")
10285        (unless (gnus-request-replace-article
10286                 ,article (car gnus-article-current)
10287                 (current-buffer) t)
10288          (error "Couldn't replace article")))
10289      ;; The cache and backlog have to be flushed somewhat.
10290      (when gnus-keep-backlog
10291        (gnus-backlog-remove-article
10292         (car gnus-article-current) (cdr gnus-article-current)))
10293      (when gnus-use-cache
10294        (gnus-cache-update-article
10295         (car gnus-article-current) (cdr gnus-article-current)))))
10296
10297 (put 'gnus-with-article 'lisp-indent-function 1)
10298 (put 'gnus-with-article 'edebug-form-spec '(form body))
10299
10300 ;; Thread-based commands.
10301
10302 (defun gnus-summary-articles-in-thread (&optional article)
10303   "Return a list of all articles in the current thread.
10304 If ARTICLE is non-nil, return all articles in the thread that starts
10305 with that article."
10306   (let* ((article (or article (gnus-summary-article-number)))
10307          (data (gnus-data-find-list article))
10308          (top-level (gnus-data-level (car data)))
10309          (top-subject
10310           (cond ((null gnus-thread-operation-ignore-subject)
10311                  (gnus-simplify-subject-re
10312                   (mail-header-subject (gnus-data-header (car data)))))
10313                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10314                  (gnus-simplify-subject-fuzzy
10315                   (mail-header-subject (gnus-data-header (car data)))))
10316                 (t nil)))
10317          (end-point (save-excursion
10318                       (if (gnus-summary-go-to-next-thread)
10319                           (point) (point-max))))
10320          articles)
10321     (while (and data
10322                 (< (gnus-data-pos (car data)) end-point))
10323       (when (or (not top-subject)
10324                 (string= top-subject
10325                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10326                              (gnus-simplify-subject-fuzzy
10327                               (mail-header-subject
10328                                (gnus-data-header (car data))))
10329                            (gnus-simplify-subject-re
10330                             (mail-header-subject
10331                              (gnus-data-header (car data)))))))
10332         (push (gnus-data-number (car data)) articles))
10333       (unless (and (setq data (cdr data))
10334                    (> (gnus-data-level (car data)) top-level))
10335         (setq data nil)))
10336     ;; Return the list of articles.
10337     (nreverse articles)))
10338
10339 (defun gnus-summary-rethread-current ()
10340   "Rethread the thread the current article is part of."
10341   (interactive)
10342   (let* ((gnus-show-threads t)
10343          (article (gnus-summary-article-number))
10344          (id (mail-header-id (gnus-summary-article-header)))
10345          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10346     (unless id
10347       (error "No article on the current line"))
10348     (gnus-rebuild-thread id)
10349     (gnus-summary-goto-subject article)))
10350
10351 (defun gnus-summary-reparent-thread ()
10352   "Make the current article child of the marked (or previous) article.
10353
10354 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10355 is non-nil or the Subject: of both articles are the same."
10356   (interactive)
10357   (unless (not (gnus-group-read-only-p))
10358     (error "The current newsgroup does not support article editing"))
10359   (unless (<= (length gnus-newsgroup-processable) 1)
10360     (error "No more than one article may be marked"))
10361   (save-window-excursion
10362     (let ((gnus-article-buffer " *reparent*")
10363           (current-article (gnus-summary-article-number))
10364           ;; First grab the marked article, otherwise one line up.
10365           (parent-article (if (not (null gnus-newsgroup-processable))
10366                               (car gnus-newsgroup-processable)
10367                             (save-excursion
10368                               (if (eq (forward-line -1) 0)
10369                                   (gnus-summary-article-number)
10370                                 (error "Beginning of summary buffer"))))))
10371       (unless (not (eq current-article parent-article))
10372         (error "An article may not be self-referential"))
10373       (let ((message-id (mail-header-id
10374                          (gnus-summary-article-header parent-article))))
10375         (unless (and message-id (not (equal message-id "")))
10376           (error "No message-id in desired parent"))
10377         (gnus-with-article current-article
10378           (save-restriction
10379             (goto-char (point-min))
10380             (message-narrow-to-head)
10381             (if (re-search-forward "^References: " nil t)
10382                 (progn
10383                   (re-search-forward "^[^ \t]" nil t)
10384                   (forward-line -1)
10385                   (end-of-line)
10386                   (insert " " message-id))
10387               (insert "References: " message-id "\n"))))
10388         (set-buffer gnus-summary-buffer)
10389         (gnus-summary-unmark-all-processable)
10390         (gnus-summary-update-article current-article)
10391         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10392             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10393         (gnus-summary-rethread-current)
10394         (gnus-message 3 "Article %d is now the child of article %d"
10395                       current-article parent-article)))))
10396
10397 (defun gnus-summary-toggle-threads (&optional arg)
10398   "Toggle showing conversation threads.
10399 If ARG is positive number, turn showing conversation threads on."
10400   (interactive "P")
10401   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10402     (setq gnus-show-threads
10403           (if (null arg) (not gnus-show-threads)
10404             (> (prefix-numeric-value arg) 0)))
10405     (gnus-summary-prepare)
10406     (gnus-summary-goto-subject current)
10407     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10408     (gnus-summary-position-point)))
10409
10410 (defun gnus-summary-show-all-threads ()
10411   "Show all threads."
10412   (interactive)
10413   (save-excursion
10414     (let ((buffer-read-only nil))
10415       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10416   (gnus-summary-position-point))
10417
10418 (defun gnus-summary-show-thread ()
10419   "Show thread subtrees.
10420 Returns nil if no thread was there to be shown."
10421   (interactive)
10422   (let ((buffer-read-only nil)
10423         (orig (point))
10424         (end (gnus-point-at-eol))
10425         ;; Leave point at bol
10426         (beg (progn (beginning-of-line) (point))))
10427     (prog1
10428         ;; Any hidden lines here?
10429         (search-forward "\r" end t)
10430       (subst-char-in-region beg end ?\^M ?\n t)
10431       (goto-char orig)
10432       (gnus-summary-position-point))))
10433
10434 (defun gnus-summary-maybe-hide-threads ()
10435   "If requested, hide the threads that should be hidden."
10436   (when (and gnus-show-threads
10437              gnus-thread-hide-subtree)
10438     (gnus-summary-hide-all-threads
10439      (if (or (consp gnus-thread-hide-subtree)
10440              (gnus-functionp gnus-thread-hide-subtree))
10441          (gnus-make-predicate gnus-thread-hide-subtree)
10442        nil))))
10443
10444 ;;; Hiding predicates.
10445
10446 (defun gnus-article-unread-p (header)
10447   (memq (mail-header-number header) gnus-newsgroup-unreads))
10448
10449 (defun gnus-article-unseen-p (header)
10450   (memq (mail-header-number header) gnus-newsgroup-unseen))
10451
10452 (defun gnus-map-articles (predicate articles)
10453   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10454   (apply 'gnus-or (mapcar predicate
10455                           (mapcar 'gnus-summary-article-header articles))))
10456
10457 (defun gnus-summary-hide-all-threads (&optional predicate)
10458   "Hide all thread subtrees.
10459 If PREDICATE is supplied, threads that satisfy this predicate
10460 will not be hidden."
10461   (interactive)
10462   (save-excursion
10463     (goto-char (point-min))
10464     (let ((end nil))
10465       (while (not end)
10466         (when (or (not predicate)
10467                   (gnus-map-articles
10468                    predicate (gnus-summary-article-children)))
10469             (gnus-summary-hide-thread))
10470         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10471   (gnus-summary-position-point))
10472
10473 (defun gnus-summary-hide-thread ()
10474   "Hide thread subtrees.
10475 If PREDICATE is supplied, threads that satisfy this predicate
10476 will not be hidden.
10477 Returns nil if no threads were there to be hidden."
10478   (interactive)
10479   (let ((buffer-read-only nil)
10480         (start (point))
10481         (article (gnus-summary-article-number)))
10482     (goto-char start)
10483     ;; Go forward until either the buffer ends or the subthread
10484     ;; ends.
10485     (when (and (not (eobp))
10486                (or (zerop (gnus-summary-next-thread 1 t))
10487                    (goto-char (point-max))))
10488       (prog1
10489           (if (and (> (point) start)
10490                    (search-backward "\n" start t))
10491               (progn
10492                 (subst-char-in-region start (point) ?\n ?\^M)
10493                 (gnus-summary-goto-subject article))
10494             (goto-char start)
10495             nil)))))
10496
10497 (defun gnus-summary-go-to-next-thread (&optional previous)
10498   "Go to the same level (or less) next thread.
10499 If PREVIOUS is non-nil, go to previous thread instead.
10500 Return the article number moved to, or nil if moving was impossible."
10501   (let ((level (gnus-summary-thread-level))
10502         (way (if previous -1 1))
10503         (beg (point)))
10504     (forward-line way)
10505     (while (and (not (eobp))
10506                 (< level (gnus-summary-thread-level)))
10507       (forward-line way))
10508     (if (eobp)
10509         (progn
10510           (goto-char beg)
10511           nil)
10512       (setq beg (point))
10513       (prog1
10514           (gnus-summary-article-number)
10515         (goto-char beg)))))
10516
10517 (defun gnus-summary-next-thread (n &optional silent)
10518   "Go to the same level next N'th thread.
10519 If N is negative, search backward instead.
10520 Returns the difference between N and the number of skips actually
10521 done.
10522
10523 If SILENT, don't output messages."
10524   (interactive "p")
10525   (let ((backward (< n 0))
10526         (n (abs n)))
10527     (while (and (> n 0)
10528                 (gnus-summary-go-to-next-thread backward))
10529       (decf n))
10530     (unless silent
10531       (gnus-summary-position-point))
10532     (when (and (not silent) (/= 0 n))
10533       (gnus-message 7 "No more threads"))
10534     n))
10535
10536 (defun gnus-summary-prev-thread (n)
10537   "Go to the same level previous N'th thread.
10538 Returns the difference between N and the number of skips actually
10539 done."
10540   (interactive "p")
10541   (gnus-summary-next-thread (- n)))
10542
10543 (defun gnus-summary-go-down-thread ()
10544   "Go down one level in the current thread."
10545   (let ((children (gnus-summary-article-children)))
10546     (when children
10547       (gnus-summary-goto-subject (car children)))))
10548
10549 (defun gnus-summary-go-up-thread ()
10550   "Go up one level in the current thread."
10551   (let ((parent (gnus-summary-article-parent)))
10552     (when parent
10553       (gnus-summary-goto-subject parent))))
10554
10555 (defun gnus-summary-down-thread (n)
10556   "Go down thread N steps.
10557 If N is negative, go up instead.
10558 Returns the difference between N and how many steps down that were
10559 taken."
10560   (interactive "p")
10561   (let ((up (< n 0))
10562         (n (abs n)))
10563     (while (and (> n 0)
10564                 (if up (gnus-summary-go-up-thread)
10565                   (gnus-summary-go-down-thread)))
10566       (setq n (1- n)))
10567     (gnus-summary-position-point)
10568     (when (/= 0 n)
10569       (gnus-message 7 "Can't go further"))
10570     n))
10571
10572 (defun gnus-summary-up-thread (n)
10573   "Go up thread N steps.
10574 If N is negative, go down instead.
10575 Returns the difference between N and how many steps down that were
10576 taken."
10577   (interactive "p")
10578   (gnus-summary-down-thread (- n)))
10579
10580 (defun gnus-summary-top-thread ()
10581   "Go to the top of the thread."
10582   (interactive)
10583   (while (gnus-summary-go-up-thread))
10584   (gnus-summary-article-number))
10585
10586 (defun gnus-summary-kill-thread (&optional unmark)
10587   "Mark articles under current thread as read.
10588 If the prefix argument is positive, remove any kinds of marks.
10589 If the prefix argument is negative, tick articles instead."
10590   (interactive "P")
10591   (when unmark
10592     (setq unmark (prefix-numeric-value unmark)))
10593   (let ((articles (gnus-summary-articles-in-thread)))
10594     (save-excursion
10595       ;; Expand the thread.
10596       (gnus-summary-show-thread)
10597       ;; Mark all the articles.
10598       (while articles
10599         (gnus-summary-goto-subject (car articles))
10600         (cond ((null unmark)
10601                (gnus-summary-mark-article-as-read gnus-killed-mark))
10602               ((> unmark 0)
10603                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10604               (t
10605                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10606         (setq articles (cdr articles))))
10607     ;; Hide killed subtrees.
10608     (and (null unmark)
10609          gnus-thread-hide-killed
10610          (gnus-summary-hide-thread))
10611     ;; If marked as read, go to next unread subject.
10612     (when (null unmark)
10613       ;; Go to next unread subject.
10614       (gnus-summary-next-subject 1 t)))
10615   (gnus-set-mode-line 'summary))
10616
10617 ;; Summary sorting commands
10618
10619 (defun gnus-summary-sort-by-number (&optional reverse)
10620   "Sort the summary buffer by article number.
10621 Argument REVERSE means reverse order."
10622   (interactive "P")
10623   (gnus-summary-sort 'number reverse))
10624
10625 (defun gnus-summary-sort-by-random (&optional reverse)
10626   "Randomize the order in the summary buffer.
10627 Argument REVERSE means to randomize in reverse order."
10628   (interactive "P")
10629   (gnus-summary-sort 'random reverse))
10630
10631 (defun gnus-summary-sort-by-author (&optional reverse)
10632   "Sort the summary buffer by author name alphabetically.
10633 If `case-fold-search' is non-nil, case of letters is ignored.
10634 Argument REVERSE means reverse order."
10635   (interactive "P")
10636   (gnus-summary-sort 'author reverse))
10637
10638 (defun gnus-summary-sort-by-subject (&optional reverse)
10639   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10640 If `case-fold-search' is non-nil, case of letters is ignored.
10641 Argument REVERSE means reverse order."
10642   (interactive "P")
10643   (gnus-summary-sort 'subject reverse))
10644
10645 (defun gnus-summary-sort-by-date (&optional reverse)
10646   "Sort the summary buffer by date.
10647 Argument REVERSE means reverse order."
10648   (interactive "P")
10649   (gnus-summary-sort 'date reverse))
10650
10651 (defun gnus-summary-sort-by-score (&optional reverse)
10652   "Sort the summary buffer by score.
10653 Argument REVERSE means reverse order."
10654   (interactive "P")
10655   (gnus-summary-sort 'score reverse))
10656
10657 (defun gnus-summary-sort-by-lines (&optional reverse)
10658   "Sort the summary buffer by the number of lines.
10659 Argument REVERSE means reverse order."
10660   (interactive "P")
10661   (gnus-summary-sort 'lines reverse))
10662
10663 (defun gnus-summary-sort-by-chars (&optional reverse)
10664   "Sort the summary buffer by article length.
10665 Argument REVERSE means reverse order."
10666   (interactive "P")
10667   (gnus-summary-sort 'chars reverse))
10668
10669 (defun gnus-summary-sort-by-original (&optional reverse)
10670   "Sort the summary buffer using the default sorting method.
10671 Argument REVERSE means reverse order."
10672   (interactive "P")
10673   (let* ((buffer-read-only)
10674          (gnus-summary-prepare-hook nil))
10675     ;; We do the sorting by regenerating the threads.
10676     (gnus-summary-prepare)
10677     ;; Hide subthreads if needed.
10678     (gnus-summary-maybe-hide-threads)))
10679
10680 (defun gnus-summary-sort (predicate reverse)
10681   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10682   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10683          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10684          (gnus-thread-sort-functions
10685           (if (not reverse)
10686               thread
10687             `(lambda (t1 t2)
10688                (,thread t2 t1))))
10689          (gnus-sort-gathered-threads-function
10690           gnus-thread-sort-functions)
10691          (gnus-article-sort-functions
10692           (if (not reverse)
10693               article
10694             `(lambda (t1 t2)
10695                (,article t2 t1))))
10696          (buffer-read-only)
10697          (gnus-summary-prepare-hook nil))
10698     ;; We do the sorting by regenerating the threads.
10699     (gnus-summary-prepare)
10700     ;; Hide subthreads if needed.
10701     (gnus-summary-maybe-hide-threads)))
10702
10703 ;; Summary saving commands.
10704
10705 (defun gnus-summary-save-article (&optional n not-saved)
10706   "Save the current article using the default saver function.
10707 If N is a positive number, save the N next articles.
10708 If N is a negative number, save the N previous articles.
10709 If N is nil and any articles have been marked with the process mark,
10710 save those articles instead.
10711 The variable `gnus-default-article-saver' specifies the saver function."
10712   (interactive "P")
10713   (let* ((articles (gnus-summary-work-articles n))
10714          (save-buffer (save-excursion
10715                         (nnheader-set-temp-buffer " *Gnus Save*")))
10716          (num (length articles))
10717          header file)
10718     (dolist (article articles)
10719       (setq header (gnus-summary-article-header article))
10720       (if (not (vectorp header))
10721           ;; This is a pseudo-article.
10722           (if (assq 'name header)
10723               (gnus-copy-file (cdr (assq 'name header)))
10724             (gnus-message 1 "Article %d is unsaveable" article))
10725         ;; This is a real article.
10726         (save-window-excursion
10727           (let ((gnus-display-mime-function nil)
10728                 (gnus-article-prepare-hook nil))
10729             (gnus-summary-select-article t nil nil article)))
10730         (save-excursion
10731           (set-buffer save-buffer)
10732           (erase-buffer)
10733           (insert-buffer-substring gnus-original-article-buffer))
10734         (setq file (gnus-article-save save-buffer file num))
10735         (gnus-summary-remove-process-mark article)
10736         (unless not-saved
10737           (gnus-summary-set-saved-mark article))))
10738     (gnus-kill-buffer save-buffer)
10739     (gnus-summary-position-point)
10740     (gnus-set-mode-line 'summary)
10741     n))
10742
10743 (defun gnus-summary-pipe-output (&optional arg headers)
10744   "Pipe the current article to a subprocess.
10745 If N is a positive number, pipe the N next articles.
10746 If N is a negative number, pipe the N previous articles.
10747 If N is nil and any articles have been marked with the process mark,
10748 pipe those articles instead.
10749 If HEADERS (the symbolic prefix), include the headers, too."
10750   (interactive (gnus-interactive "P\ny"))
10751   (require 'gnus-art)
10752   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10753         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10754     (gnus-summary-save-article arg t))
10755   (let ((buffer (get-buffer "*Shell Command Output*")))
10756     (when (and buffer
10757                (not (zerop (buffer-size buffer))))
10758       (gnus-configure-windows 'pipe))))
10759
10760 (defun gnus-summary-save-article-mail (&optional arg)
10761   "Append the current article to an mail file.
10762 If N is a positive number, save the N next articles.
10763 If N is a negative number, save the N previous articles.
10764 If N is nil and any articles have been marked with the process mark,
10765 save those articles instead."
10766   (interactive "P")
10767   (require 'gnus-art)
10768   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10769     (gnus-summary-save-article arg)))
10770
10771 (defun gnus-summary-save-article-rmail (&optional arg)
10772   "Append the current article to an rmail file.
10773 If N is a positive number, save the N next articles.
10774 If N is a negative number, save the N previous articles.
10775 If N is nil and any articles have been marked with the process mark,
10776 save those articles instead."
10777   (interactive "P")
10778   (require 'gnus-art)
10779   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10780     (gnus-summary-save-article arg)))
10781
10782 (defun gnus-summary-save-article-file (&optional arg)
10783   "Append the current article to a file.
10784 If N is a positive number, save the N next articles.
10785 If N is a negative number, save the N previous articles.
10786 If N is nil and any articles have been marked with the process mark,
10787 save those articles instead."
10788   (interactive "P")
10789   (require 'gnus-art)
10790   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10791     (gnus-summary-save-article arg)))
10792
10793 (defun gnus-summary-write-article-file (&optional arg)
10794   "Write the current article to a file, deleting the previous file.
10795 If N is a positive number, save the N next articles.
10796 If N is a negative number, save the N previous articles.
10797 If N is nil and any articles have been marked with the process mark,
10798 save those articles instead."
10799   (interactive "P")
10800   (require 'gnus-art)
10801   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10802     (gnus-summary-save-article arg)))
10803
10804 (defun gnus-summary-save-article-body-file (&optional arg)
10805   "Append the current article body to a file.
10806 If N is a positive number, save the N next articles.
10807 If N is a negative number, save the N previous articles.
10808 If N is nil and any articles have been marked with the process mark,
10809 save those articles instead."
10810   (interactive "P")
10811   (require 'gnus-art)
10812   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10813     (gnus-summary-save-article arg)))
10814
10815 (defun gnus-summary-muttprint (&optional arg)
10816   "Print the current article using Muttprint.
10817 If N is a positive number, save the N next articles.
10818 If N is a negative number, save the N previous articles.
10819 If N is nil and any articles have been marked with the process mark,
10820 save those articles instead."
10821   (interactive "P")
10822   (require 'gnus-art)
10823   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10824     (gnus-summary-save-article arg t)))
10825
10826 (defun gnus-summary-pipe-message (program)
10827   "Pipe the current article through PROGRAM."
10828   (interactive "sProgram: ")
10829   (gnus-summary-select-article)
10830   (let ((mail-header-separator ""))
10831     (gnus-eval-in-buffer-window gnus-article-buffer
10832       (save-restriction
10833         (widen)
10834         (let ((start (window-start))
10835               buffer-read-only)
10836           (message-pipe-buffer-body program)
10837           (set-window-start (get-buffer-window (current-buffer)) start))))))
10838
10839 (defun gnus-get-split-value (methods)
10840   "Return a value based on the split METHODS."
10841   (let (split-name method result match)
10842     (when methods
10843       (save-excursion
10844         (set-buffer gnus-original-article-buffer)
10845         (save-restriction
10846           (nnheader-narrow-to-headers)
10847           (while (and methods (not split-name))
10848             (goto-char (point-min))
10849             (setq method (pop methods))
10850             (setq match (car method))
10851             (when (cond
10852                    ((stringp match)
10853                     ;; Regular expression.
10854                     (ignore-errors
10855                       (re-search-forward match nil t)))
10856                    ((gnus-functionp match)
10857                     ;; Function.
10858                     (save-restriction
10859                       (widen)
10860                       (setq result (funcall match gnus-newsgroup-name))))
10861                    ((consp match)
10862                     ;; Form.
10863                     (save-restriction
10864                       (widen)
10865                       (setq result (eval match)))))
10866               (setq split-name (cdr method))
10867               (cond ((stringp result)
10868                      (push (expand-file-name
10869                             result gnus-article-save-directory)
10870                            split-name))
10871                     ((consp result)
10872                      (setq split-name (append result split-name)))))))))
10873     (nreverse split-name)))
10874
10875 (defun gnus-valid-move-group-p (group)
10876   (and (boundp group)
10877        (symbol-name group)
10878        (symbol-value group)
10879        (gnus-get-function (gnus-find-method-for-group
10880                            (symbol-name group)) 'request-accept-article t)))
10881
10882 (defun gnus-read-move-group-name (prompt default articles prefix)
10883   "Read a group name."
10884   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10885          (minibuffer-confirm-incomplete nil) ; XEmacs
10886          (prom
10887           (format "%s %s to:"
10888                   prompt
10889                   (if (> (length articles) 1)
10890                       (format "these %d articles" (length articles))
10891                     "this article")))
10892          (to-newsgroup
10893           (cond
10894            ((null split-name)
10895             (gnus-completing-read-with-default
10896              default prom
10897              gnus-active-hashtb
10898              'gnus-valid-move-group-p
10899              nil prefix
10900              'gnus-group-history))
10901            ((= 1 (length split-name))
10902             (gnus-completing-read-with-default
10903              (car split-name) prom
10904              gnus-active-hashtb
10905              'gnus-valid-move-group-p
10906              nil nil
10907              'gnus-group-history))
10908            (t
10909             (gnus-completing-read-with-default
10910              nil prom
10911              (mapcar (lambda (el) (list el))
10912                      (nreverse split-name))
10913              nil nil nil
10914              'gnus-group-history))))
10915          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10916     (when to-newsgroup
10917       (if (or (string= to-newsgroup "")
10918               (string= to-newsgroup prefix))
10919           (setq to-newsgroup default))
10920       (unless to-newsgroup
10921         (error "No group name entered"))
10922       (or (gnus-active to-newsgroup)
10923           (gnus-activate-group to-newsgroup nil nil to-method)
10924           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10925                                      to-newsgroup))
10926               (or (and (gnus-request-create-group to-newsgroup to-method)
10927                        (gnus-activate-group
10928                         to-newsgroup nil nil to-method)
10929                        (gnus-subscribe-group to-newsgroup))
10930                   (error "Couldn't create group %s" to-newsgroup)))
10931           (error "No such group: %s" to-newsgroup)))
10932     to-newsgroup))
10933
10934 (defun gnus-summary-save-parts (type dir n &optional reverse)
10935   "Save parts matching TYPE to DIR.
10936 If REVERSE, save parts that do not match TYPE."
10937   (interactive
10938    (list (read-string "Save parts of type: "
10939                       (or (car gnus-summary-save-parts-type-history)
10940                           gnus-summary-save-parts-default-mime)
10941                       'gnus-summary-save-parts-type-history)
10942          (setq gnus-summary-save-parts-last-directory
10943                (read-file-name "Save to directory: "
10944                                gnus-summary-save-parts-last-directory
10945                                nil t))
10946          current-prefix-arg))
10947   (gnus-summary-iterate n
10948     (let ((gnus-display-mime-function nil)
10949           (gnus-inhibit-treatment t))
10950       (gnus-summary-select-article))
10951     (save-excursion
10952       (set-buffer gnus-article-buffer)
10953       (let ((handles (or gnus-article-mime-handles
10954                          (mm-dissect-buffer nil gnus-article-loose-mime)
10955                          (and gnus-article-emulate-mime
10956                               (mm-uu-dissect)))))
10957         (when handles
10958           (gnus-summary-save-parts-1 type dir handles reverse)
10959           (unless gnus-article-mime-handles ;; Don't destroy this case.
10960             (mm-destroy-parts handles)))))))
10961
10962 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10963   (if (stringp (car handle))
10964       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10965               (cdr handle))
10966     (when (if reverse
10967               (not (string-match type (mm-handle-media-type handle)))
10968             (string-match type (mm-handle-media-type handle)))
10969       (let ((file (expand-file-name
10970                    (file-name-nondirectory
10971                     (or
10972                      (mail-content-type-get
10973                       (mm-handle-disposition handle) 'filename)
10974                      (concat gnus-newsgroup-name
10975                              "." (number-to-string
10976                                   (cdr gnus-article-current)))))
10977                    dir)))
10978         (unless (file-exists-p file)
10979           (mm-save-part-to-file handle file))))))
10980
10981 ;; Summary extract commands
10982
10983 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10984   (let ((buffer-read-only nil)
10985         (article (gnus-summary-article-number))
10986         after-article b e)
10987     (unless (gnus-summary-goto-subject article)
10988       (error "No such article: %d" article))
10989     (gnus-summary-position-point)
10990     ;; If all commands are to be bunched up on one line, we collect
10991     ;; them here.
10992     (unless gnus-view-pseudos-separately
10993       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10994             files action)
10995         (while ps
10996           (setq action (cdr (assq 'action (car ps))))
10997           (setq files (list (cdr (assq 'name (car ps)))))
10998           (while (and ps (cdr ps)
10999                       (string= (or action "1")
11000                                (or (cdr (assq 'action (cadr ps))) "2")))
11001             (push (cdr (assq 'name (cadr ps))) files)
11002             (setcdr ps (cddr ps)))
11003           (when files
11004             (when (not (string-match "%s" action))
11005               (push " " files))
11006             (push " " files)
11007             (when (assq 'execute (car ps))
11008               (setcdr (assq 'execute (car ps))
11009                       (funcall (if (string-match "%s" action)
11010                                    'format 'concat)
11011                                action
11012                                (mapconcat
11013                                 (lambda (f)
11014                                   (if (equal f " ")
11015                                       f
11016                                     (gnus-quote-arg-for-sh-or-csh f)))
11017                                 files " ")))))
11018           (setq ps (cdr ps)))))
11019     (if (and gnus-view-pseudos (not not-view))
11020         (while pslist
11021           (when (assq 'execute (car pslist))
11022             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11023                                   (eq gnus-view-pseudos 'not-confirm)))
11024           (setq pslist (cdr pslist)))
11025       (save-excursion
11026         (while pslist
11027           (setq after-article (or (cdr (assq 'article (car pslist)))
11028                                   (gnus-summary-article-number)))
11029           (gnus-summary-goto-subject after-article)
11030           (forward-line 1)
11031           (setq b (point))
11032           (insert "    " (file-name-nondirectory
11033                           (cdr (assq 'name (car pslist))))
11034                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11035           (setq e (point))
11036           (forward-line -1)             ; back to `b'
11037           (gnus-add-text-properties
11038            b (1- e) (list 'gnus-number gnus-reffed-article-number
11039                           gnus-mouse-face-prop gnus-mouse-face))
11040           (gnus-data-enter
11041            after-article gnus-reffed-article-number
11042            gnus-unread-mark b (car pslist) 0 (- e b))
11043           (setq gnus-newsgroup-unreads
11044                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11045                                          gnus-reffed-article-number))
11046           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11047           (setq pslist (cdr pslist)))))))
11048
11049 (defun gnus-pseudos< (p1 p2)
11050   (let ((c1 (cdr (assq 'action p1)))
11051         (c2 (cdr (assq 'action p2))))
11052     (and c1 c2 (string< c1 c2))))
11053
11054 (defun gnus-request-pseudo-article (props)
11055   (cond ((assq 'execute props)
11056          (gnus-execute-command (cdr (assq 'execute props)))))
11057   (let ((gnus-current-article (gnus-summary-article-number)))
11058     (gnus-run-hooks 'gnus-mark-article-hook)))
11059
11060 (defun gnus-execute-command (command &optional automatic)
11061   (save-excursion
11062     (gnus-article-setup-buffer)
11063     (set-buffer gnus-article-buffer)
11064     (setq buffer-read-only nil)
11065     (let ((command (if automatic command
11066                      (read-string "Command: " (cons command 0)))))
11067       (erase-buffer)
11068       (insert "$ " command "\n\n")
11069       (if gnus-view-pseudo-asynchronously
11070           (start-process "gnus-execute" (current-buffer) shell-file-name
11071                          shell-command-switch command)
11072         (call-process shell-file-name nil t nil
11073                       shell-command-switch command)))))
11074
11075 ;; Summary kill commands.
11076
11077 (defun gnus-summary-edit-global-kill (article)
11078   "Edit the \"global\" kill file."
11079   (interactive (list (gnus-summary-article-number)))
11080   (gnus-group-edit-global-kill article))
11081
11082 (defun gnus-summary-edit-local-kill ()
11083   "Edit a local kill file applied to the current newsgroup."
11084   (interactive)
11085   (setq gnus-current-headers (gnus-summary-article-header))
11086   (gnus-group-edit-local-kill
11087    (gnus-summary-article-number) gnus-newsgroup-name))
11088
11089 ;;; Header reading.
11090
11091 (defun gnus-read-header (id &optional header)
11092   "Read the headers of article ID and enter them into the Gnus system."
11093   (let ((group gnus-newsgroup-name)
11094         (gnus-override-method
11095          (or
11096           gnus-override-method
11097           (and (gnus-news-group-p gnus-newsgroup-name)
11098                (car (gnus-refer-article-methods)))))
11099         where)
11100     ;; First we check to see whether the header in question is already
11101     ;; fetched.
11102     (if (stringp id)
11103         ;; This is a Message-ID.
11104         (setq header (or header (gnus-id-to-header id)))
11105       ;; This is an article number.
11106       (setq header (or header (gnus-summary-article-header id))))
11107     (if (and header
11108              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11109         ;; We have found the header.
11110         header
11111       ;; If this is a sparse article, we have to nix out its
11112       ;; previous entry in the thread hashtb.
11113       (when (and header
11114                  (gnus-summary-article-sparse-p (mail-header-number header)))
11115         (let* ((parent (gnus-parent-id (mail-header-references header)))
11116                (thread (and parent (gnus-id-to-thread parent))))
11117           (when thread
11118             (delq (assq header thread) thread))))
11119       ;; We have to really fetch the header to this article.
11120       (save-excursion
11121         (set-buffer nntp-server-buffer)
11122         (when (setq where (gnus-request-head id group))
11123           (nnheader-fold-continuation-lines)
11124           (goto-char (point-max))
11125           (insert ".\n")
11126           (goto-char (point-min))
11127           (insert "211 ")
11128           (princ (cond
11129                   ((numberp id) id)
11130                   ((cdr where) (cdr where))
11131                   (header (mail-header-number header))
11132                   (t gnus-reffed-article-number))
11133                  (current-buffer))
11134           (insert " Article retrieved.\n"))
11135         (if (or (not where)
11136                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11137             ()                          ; Malformed head.
11138           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11139             (when (and (stringp id)
11140                        (not (string= (gnus-group-real-name group)
11141                                      (car where))))
11142               ;; If we fetched by Message-ID and the article came
11143               ;; from a different group, we fudge some bogus article
11144               ;; numbers for this article.
11145               (mail-header-set-number header gnus-reffed-article-number))
11146             (save-excursion
11147               (set-buffer gnus-summary-buffer)
11148               (decf gnus-reffed-article-number)
11149               (gnus-remove-header (mail-header-number header))
11150               (push header gnus-newsgroup-headers)
11151               (setq gnus-current-headers header)
11152               (push (mail-header-number header) gnus-newsgroup-limit)))
11153           header)))))
11154
11155 (defun gnus-remove-header (number)
11156   "Remove header NUMBER from `gnus-newsgroup-headers'."
11157   (if (and gnus-newsgroup-headers
11158            (= number (mail-header-number (car gnus-newsgroup-headers))))
11159       (pop gnus-newsgroup-headers)
11160     (let ((headers gnus-newsgroup-headers))
11161       (while (and (cdr headers)
11162                   (not (= number (mail-header-number (cadr headers)))))
11163         (pop headers))
11164       (when (cdr headers)
11165         (setcdr headers (cddr headers))))))
11166
11167 ;;;
11168 ;;; summary highlights
11169 ;;;
11170
11171 (defun gnus-highlight-selected-summary ()
11172   "Highlight selected article in summary buffer."
11173   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11174   (when gnus-summary-selected-face
11175     (save-excursion
11176       (let* ((beg (gnus-point-at-bol))
11177              (end (gnus-point-at-eol))
11178              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11179              (from (if (get-text-property beg gnus-mouse-face-prop)
11180                        beg
11181                      (or (next-single-property-change
11182                           beg gnus-mouse-face-prop nil end)
11183                          beg)))
11184              (to
11185               (if (= from end)
11186                   (- from 2)
11187                 (or (next-single-property-change
11188                      from gnus-mouse-face-prop nil end)
11189                     end))))
11190         ;; If no mouse-face prop on line we will have to = from = end,
11191         ;; so we highlight the entire line instead.
11192         (when (= (+ to 2) from)
11193           (setq from beg)
11194           (setq to end))
11195         (if gnus-newsgroup-selected-overlay
11196             ;; Move old overlay.
11197             (gnus-move-overlay
11198              gnus-newsgroup-selected-overlay from to (current-buffer))
11199           ;; Create new overlay.
11200           (gnus-overlay-put
11201            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11202            'face gnus-summary-selected-face))))))
11203
11204 (defvar gnus-summary-highlight-line-cached nil)
11205 (defvar gnus-summary-highlight-line-trigger nil)
11206
11207 (defun gnus-summary-highlight-line-0 ()
11208   (if (and (eq gnus-summary-highlight-line-trigger 
11209                gnus-summary-highlight)
11210            gnus-summary-highlight-line-cached)
11211       gnus-summary-highlight-line-cached
11212     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11213           gnus-summary-highlight-line-cached
11214           (let* ((cond (list 'cond))
11215                  (c cond)
11216                  (list gnus-summary-highlight))
11217             (while list
11218               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11219               (setq c (cdr c)
11220                     list (cdr list)))
11221             (gnus-byte-compile (list 'lambda nil cond))))))
11222
11223 (defun gnus-summary-highlight-line ()
11224   "Highlight current line according to `gnus-summary-highlight'."
11225   (let* ((beg (gnus-point-at-bol))
11226          (article (or (gnus-summary-article-number) gnus-current-article))
11227          (score (or (cdr (assq article
11228                                gnus-newsgroup-scored))
11229                     gnus-summary-default-score 0))
11230          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11231          (inhibit-read-only t)
11232          (default gnus-summary-default-score)
11233          (default-high gnus-summary-default-high-score)
11234          (default-low gnus-summary-default-low-score)
11235          (uncached (memq article gnus-newsgroup-undownloaded))
11236          (downloaded (not uncached)))
11237     (let ((face (funcall (gnus-summary-highlight-line-0))))
11238       (unless (eq face (get-text-property beg 'face))
11239         (gnus-put-text-property-excluding-characters-with-faces
11240          beg (gnus-point-at-eol) 'face
11241          (setq face (if (boundp face) (symbol-value face) face)))
11242         (when gnus-summary-highlight-line-function
11243           (funcall gnus-summary-highlight-line-function article face))))))
11244
11245 (defun gnus-update-read-articles (group unread &optional compute)
11246   "Update the list of read articles in GROUP.
11247 UNREAD is a sorted list."
11248   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11249          (entry (gnus-gethash group gnus-newsrc-hashtb))
11250          (info (nth 2 entry))
11251          (prev 1)
11252          read)
11253     (if (or (not info) (not active))
11254         ;; There is no info on this group if it was, in fact,
11255         ;; killed.  Gnus stores no information on killed groups, so
11256         ;; there's nothing to be done.
11257         ;; One could store the information somewhere temporarily,
11258         ;; perhaps...  Hmmm...
11259         ()
11260       ;; Remove any negative articles numbers.
11261       (while (and unread (< (car unread) 0))
11262         (setq unread (cdr unread)))
11263       ;; Remove any expired article numbers
11264       (while (and unread (< (car unread) (car active)))
11265         (setq unread (cdr unread)))
11266       ;; Compute the ranges of read articles by looking at the list of
11267       ;; unread articles.
11268       (while unread
11269         (when (/= (car unread) prev)
11270           (push (if (= prev (1- (car unread))) prev
11271                   (cons prev (1- (car unread))))
11272                 read))
11273         (setq prev (1+ (car unread)))
11274         (setq unread (cdr unread)))
11275       (when (<= prev (cdr active))
11276         (push (cons prev (cdr active)) read))
11277       (setq read (if (> (length read) 1) (nreverse read) read))
11278       (if compute
11279           read
11280         (save-excursion
11281           (let (setmarkundo)
11282             ;; Propagate the read marks to the backend.
11283             (when (gnus-check-backend-function 'request-set-mark group)
11284               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11285                     (add (gnus-remove-from-range read (gnus-info-read info))))
11286                 (when (or add del)
11287                   (unless (gnus-check-group group)
11288                     (error "Can't open server for %s" group))
11289                   (gnus-request-set-mark
11290                    group (delq nil (list (if add (list add 'add '(read)))
11291                                          (if del (list del 'del '(read))))))
11292                   (setq setmarkundo
11293                         `(gnus-request-set-mark
11294                           ,group
11295                           ',(delq nil (list
11296                                        (if del (list del 'add '(read)))
11297                                        (if add (list add 'del '(read))))))))))
11298             (set-buffer gnus-group-buffer)
11299             (gnus-undo-register
11300               `(progn
11301                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11302                  (gnus-info-set-read ',info ',(gnus-info-read info))
11303                  (gnus-get-unread-articles-in-group ',info
11304                                                     (gnus-active ,group))
11305                  (gnus-group-update-group ,group t)
11306                  ,setmarkundo))))
11307         ;; Enter this list into the group info.
11308         (gnus-info-set-read info read)
11309         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11310         (gnus-get-unread-articles-in-group info (gnus-active group))
11311         t))))
11312
11313 (defun gnus-offer-save-summaries ()
11314   "Offer to save all active summary buffers."
11315   (let (buffers)
11316     ;; Go through all buffers and find all summaries.
11317     (dolist (buffer (buffer-list))
11318       (when (and (setq buffer (buffer-name buffer))
11319                  (string-match "Summary" buffer)
11320                  (save-excursion
11321                    (set-buffer buffer)
11322                    ;; We check that this is, indeed, a summary buffer.
11323                    (and (eq major-mode 'gnus-summary-mode)
11324                         ;; Also make sure this isn't bogus.
11325                         gnus-newsgroup-prepared
11326                         ;; Also make sure that this isn't a
11327                         ;; dead summary buffer.
11328                         (not gnus-dead-summary-mode))))
11329         (push buffer buffers)))
11330     ;; Go through all these summary buffers and offer to save them.
11331     (when buffers
11332       (save-excursion
11333         (map-y-or-n-p
11334          "Update summary buffer %s? "
11335          (lambda (buf)
11336            (switch-to-buffer buf)
11337            (gnus-summary-exit))
11338          buffers)))))
11339
11340
11341 ;;; @ for mime-partial
11342 ;;;
11343
11344 (defun gnus-request-partial-message ()
11345   (save-excursion
11346     (let ((number (gnus-summary-article-number))
11347           (group gnus-newsgroup-name)
11348           (mother gnus-article-buffer))
11349       (set-buffer (get-buffer-create " *Partial Article*"))
11350       (erase-buffer)
11351       (setq mime-preview-buffer mother)
11352       (gnus-request-article-this-buffer number group)
11353       (mime-parse-buffer)
11354       )))
11355
11356 (autoload 'mime-combine-message/partial-pieces-automatically
11357   "mime-partial"
11358   "Internal method to combine message/partial messages automatically.")
11359
11360 (mime-add-condition
11361  'action '((type . message)(subtype . partial)
11362            (major-mode . gnus-original-article-mode)
11363            (method . mime-combine-message/partial-pieces-automatically)
11364            (summary-buffer-exp . gnus-summary-buffer)
11365            (request-partial-message-method . gnus-request-partial-message)
11366            ))
11367
11368
11369 ;;; @ for message/rfc822
11370 ;;;
11371
11372 (defun gnus-mime-extract-message/rfc822 (entity situation)
11373   "Burst a forwarded article."
11374   (save-excursion
11375     (set-buffer gnus-summary-buffer)
11376     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11377                                    gnus-newsgroup-name 'gnus-group-history))
11378            (gnus-group-marked (list group))
11379            article info)
11380       (with-temp-buffer
11381         (mime-insert-entity-content entity)
11382         (setq article (gnus-request-accept-article group)))
11383       (when (and (consp article)
11384                  (numberp (setq article (cdr article))))
11385         (setq info (gnus-get-info group))
11386         (gnus-info-set-read info
11387                             (gnus-remove-from-range (gnus-info-read info)
11388                                                     (list article)))
11389         (when (string-equal group gnus-newsgroup-name)
11390           (forward-line 1)
11391           (let (gnus-show-threads)
11392             (gnus-summary-goto-subject article t))
11393           (gnus-summary-clear-mark-forward 1))
11394         (set-buffer gnus-group-buffer)
11395         (gnus-group-get-new-news-this-group nil t)))))
11396
11397 (mime-add-condition
11398  'action '((type . message)(subtype . rfc822)
11399            (major-mode . gnus-original-article-mode)
11400            (method . gnus-mime-extract-message/rfc822)
11401            (mode . "extract")
11402            ))
11403
11404 (mime-add-condition
11405  'action '((type . message)(subtype . news)
11406            (major-mode . gnus-original-article-mode)
11407            (method . gnus-mime-extract-message/rfc822)
11408            (mode . "extract")
11409            ))
11410
11411 (defun gnus-mime-extract-multipart (entity situation)
11412   (let ((children (mime-entity-children entity))
11413         mime-acting-situation-to-override
11414         f)
11415     (while children
11416       (mime-play-entity (car children)
11417                         (cons (assq 'mode situation)
11418                               mime-acting-situation-to-override))
11419       (setq children (cdr children)))
11420     (if (setq f (cdr (assq 'after-method
11421                            mime-acting-situation-to-override)))
11422         (eval f)
11423       )))
11424
11425 (mime-add-condition
11426  'action '((type . multipart)
11427            (method . gnus-mime-extract-multipart)
11428            (mode . "extract")
11429            )
11430  'with-default)
11431
11432
11433 ;;; @ end
11434 ;;;
11435
11436 (defun gnus-summary-inherit-default-charset ()
11437   "Import `default-mime-charset' from summary buffer.
11438 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11439 of FLIM is used."
11440   (if (buffer-live-p gnus-summary-buffer)
11441       (let (d-m-c d-m-c-u)
11442         (with-current-buffer gnus-summary-buffer
11443           (setq d-m-c (if (local-variable-p 'default-mime-charset
11444                                             gnus-summary-buffer)
11445                           default-mime-charset
11446                         t)
11447                 ;; LIMIT
11448                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11449                                               gnus-summary-buffer)
11450                             (symbol-value 'default-mime-charset-unlimited)
11451                           t)))
11452         (if (eq t d-m-c)
11453             (kill-local-variable 'default-mime-charset)
11454           (set (make-local-variable 'default-mime-charset) d-m-c))
11455         (if (eq t d-m-c-u)
11456             (kill-local-variable 'default-mime-charset-unlimited)
11457           (set (make-local-variable 'default-mime-charset-unlimited)
11458                d-m-c-u)))))
11459
11460 (defun gnus-summary-setup-default-charset ()
11461   "Setup newsgroup default charset."
11462   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11463       (progn
11464         (setq gnus-newsgroup-charset nil)
11465         (set (make-local-variable 'default-mime-charset) nil)
11466         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11467           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11468     (let ((ignored-charsets
11469            (or gnus-newsgroup-ephemeral-ignored-charsets
11470                (append
11471                 (and gnus-newsgroup-name
11472                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11473                 gnus-newsgroup-ignored-charsets)))
11474           charset)
11475       (setq gnus-newsgroup-charset
11476             (or gnus-newsgroup-ephemeral-charset
11477                 (when (and gnus-newsgroup-name
11478                            (setq charset (gnus-parameter-charset
11479                                           gnus-newsgroup-name)))
11480                   (make-local-variable 'default-mime-charset)
11481                   (setq default-mime-charset charset))
11482                 gnus-default-charset))
11483       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11484            ignored-charsets))))
11485
11486 ;;;
11487 ;;; Mime Commands
11488 ;;;
11489
11490 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11491   "Display the current article buffer fully MIME-buttonized.
11492 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11493 treated as multipart/mixed."
11494   (interactive "P")
11495   (require 'gnus-art)
11496   (let ((gnus-unbuttonized-mime-types nil)
11497         (gnus-mime-display-multipart-as-mixed show-all-parts))
11498     (gnus-summary-show-article)))
11499
11500 (defun gnus-summary-repair-multipart (article)
11501   "Add a Content-Type header to a multipart article without one."
11502   (interactive (list (gnus-summary-article-number)))
11503   (gnus-with-article article
11504     (message-narrow-to-head)
11505     (message-remove-header "Mime-Version")
11506     (goto-char (point-max))
11507     (insert "Mime-Version: 1.0\n")
11508     (widen)
11509     (when (search-forward "\n--" nil t)
11510       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11511         (message-narrow-to-head)
11512         (message-remove-header "Content-Type")
11513         (goto-char (point-max))
11514         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11515                         separator))
11516         (widen))))
11517   (let (gnus-mark-article-hook)
11518     (gnus-summary-select-article t t nil article)))
11519
11520 (defun gnus-summary-toggle-display-buttonized ()
11521   "Toggle the buttonizing of the article buffer."
11522   (interactive)
11523   (require 'gnus-art)
11524   (if (setq gnus-inhibit-mime-unbuttonizing
11525             (not gnus-inhibit-mime-unbuttonizing))
11526       (let ((gnus-unbuttonized-mime-types nil))
11527         (gnus-summary-show-article))
11528     (gnus-summary-show-article)))
11529
11530 ;;;
11531 ;;; Intelli-mouse commmands
11532 ;;;
11533
11534 (defun gnus-wheel-summary-scroll (event)
11535   (interactive "e")
11536   (let ((amount (if (memq 'shift (event-modifiers event))
11537                     (car gnus-wheel-scroll-amount)
11538                   (cdr gnus-wheel-scroll-amount)))
11539         (direction (- (* (static-if (featurep 'xemacs)
11540                              (event-button event)
11541                            (cond ((eq 'mouse-4 (event-basic-type event))
11542                                   4)
11543                                  ((eq 'mouse-5 (event-basic-type event))
11544                                   5)))
11545                          2) 9))
11546         edge)
11547     (gnus-summary-scroll-up (* amount direction))
11548     (when (gnus-eval-in-buffer-window gnus-article-buffer
11549             (save-restriction
11550               (widen)
11551               (and (if (< 0 direction)
11552                        (gnus-article-next-page 0)
11553                      (gnus-article-prev-page 0)
11554                      (bobp))
11555                    (if (setq edge (get-text-property
11556                                    (point-min) 'gnus-wheel-edge))
11557                        (setq edge (* edge direction))
11558                      (setq edge -1))
11559                    (or (plusp edge)
11560                        (let ((buffer-read-only nil)
11561                              (inhibit-read-only t))
11562                          (put-text-property (point-min) (point-max)
11563                                             'gnus-wheel-edge direction)
11564                          nil))
11565                    (or (> edge gnus-wheel-edge-resistance)
11566                        (let ((buffer-read-only nil)
11567                              (inhibit-read-only t))
11568                          (put-text-property (point-min) (point-max)
11569                                             'gnus-wheel-edge
11570                                             (* (1+ edge) direction))
11571                          nil))
11572                    (eq last-command 'gnus-wheel-summary-scroll))))
11573       (gnus-summary-next-article nil nil (minusp direction)))))
11574
11575 (defun gnus-wheel-install ()
11576   "Enable mouse wheel support on summary window."
11577   (when gnus-use-wheel
11578     (let ((keys
11579            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11580       (dolist (key keys)
11581         (define-key gnus-summary-mode-map key
11582           'gnus-wheel-summary-scroll)))))
11583
11584 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11585
11586 ;;;
11587 ;;; Traditional PGP commmands
11588 ;;;
11589
11590 (defun gnus-summary-decrypt-article (&optional force)
11591   "Decrypt the current article in traditional PGP way.
11592 This will have permanent effect only in mail groups.
11593 If FORCE is non-nil, allow editing of articles even in read-only
11594 groups."
11595   (interactive "P")
11596   (gnus-summary-select-article t)
11597   (gnus-eval-in-buffer-window gnus-article-buffer
11598     (save-excursion
11599       (save-restriction
11600         (widen)
11601         (goto-char (point-min))
11602         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11603           (error "Not a traditional PGP message!"))
11604         (let ((armor-start (match-beginning 0)))
11605           (if (and (pgg-decrypt-region armor-start (point-max))
11606                    (or force (not (gnus-group-read-only-p))))
11607               (let ((inhibit-read-only t)
11608                     buffer-read-only)
11609                 (delete-region armor-start
11610                                (progn
11611                                  (re-search-forward "^-+END PGP" nil t)
11612                                  (beginning-of-line 2)
11613                                  (point)))
11614                 (insert-buffer-substring pgg-output-buffer))))))))
11615
11616 (defun gnus-summary-verify-article ()
11617   "Verify the current article in traditional PGP way."
11618   (interactive)
11619   (save-excursion
11620     (set-buffer gnus-original-article-buffer)
11621     (goto-char (point-min))
11622     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11623       (error "Not a traditional PGP message!"))
11624     (re-search-forward "^-+END PGP" nil t)
11625     (beginning-of-line 2)
11626     (call-interactively (function pgg-verify-region))))
11627
11628 ;;;
11629 ;;; Generic summary marking commands
11630 ;;;
11631
11632 (defvar gnus-summary-marking-alist
11633   '((read gnus-del-mark "d")
11634     (unread gnus-unread-mark "u")
11635     (ticked gnus-ticked-mark "!")
11636     (dormant gnus-dormant-mark "?")
11637     (expirable gnus-expirable-mark "e"))
11638   "An alist of names/marks/keystrokes.")
11639
11640 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11641 (defvar gnus-summary-mark-map)
11642
11643 (defun gnus-summary-make-all-marking-commands ()
11644   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11645   (dolist (elem gnus-summary-marking-alist)
11646     (apply 'gnus-summary-make-marking-command elem)))
11647
11648 (defun gnus-summary-make-marking-command (name mark keystroke)
11649   (let ((map (make-sparse-keymap)))
11650     (define-key gnus-summary-generic-mark-map keystroke map)
11651     (dolist (lway `((next "next" next nil "n")
11652                     (next-unread "next unread" next t "N")
11653                     (prev "previous" prev nil "p")
11654                     (prev-unread "previous unread" prev t "P")
11655                     (nomove "" nil nil ,keystroke)))
11656       (let ((func (gnus-summary-make-marking-command-1
11657                    mark (car lway) lway name)))
11658         (setq func (eval func))
11659         (define-key map (nth 4 lway) func)))))
11660
11661 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11662   `(defun ,(intern
11663             (format "gnus-summary-put-mark-as-%s%s"
11664                     name (if (eq way 'nomove)
11665                              ""
11666                            (concat "-" (symbol-name way)))))
11667      (n)
11668      ,(format
11669        "Mark the current article as %s%s.
11670 If N, the prefix, then repeat N times.
11671 If N is negative, move in reverse order.
11672 The difference between N and the actual number of articles marked is
11673 returned."
11674        name (car (cdr lway)))
11675      (interactive "p")
11676      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11677
11678 (defun gnus-summary-generic-mark (n mark move unread)
11679   "Mark N articles with MARK."
11680   (unless (eq major-mode 'gnus-summary-mode)
11681     (error "This command can only be used in the summary buffer"))
11682   (gnus-summary-show-thread)
11683   (let ((nummove
11684          (cond
11685           ((eq move 'next) 1)
11686           ((eq move 'prev) -1)
11687           (t 0))))
11688     (if (zerop nummove)
11689         (setq n 1)
11690       (when (< n 0)
11691         (setq n (abs n)
11692               nummove (* -1 nummove))))
11693     (while (and (> n 0)
11694                 (gnus-summary-mark-article nil mark)
11695                 (zerop (gnus-summary-next-subject nummove unread t)))
11696       (setq n (1- n)))
11697     (when (/= 0 n)
11698       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11699     (gnus-summary-recenter)
11700     (gnus-summary-position-point)
11701     (gnus-set-mode-line 'summary)
11702     n))
11703
11704 (defun gnus-summary-insert-articles (articles)
11705   (when (setq articles
11706               (gnus-sorted-difference articles
11707                                       (mapcar (lambda (h)
11708                                                 (mail-header-number h))
11709                                               gnus-newsgroup-headers)))
11710     (setq gnus-newsgroup-headers
11711           (merge 'list
11712                  gnus-newsgroup-headers
11713                  (gnus-fetch-headers articles)
11714                  'gnus-article-sort-by-number))
11715     ;; Suppress duplicates?
11716     (when gnus-suppress-duplicates
11717       (gnus-dup-suppress-articles))
11718
11719     ;; We might want to build some more threads first.
11720     (when (and gnus-fetch-old-headers
11721                (eq gnus-headers-retrieved-by 'nov))
11722       (if (eq gnus-fetch-old-headers 'invisible)
11723           (gnus-build-all-threads)
11724         (gnus-build-old-threads)))
11725     ;; Let the Gnus agent mark articles as read.
11726     (when gnus-agent
11727       (gnus-agent-get-undownloaded-list))
11728     ;; Remove list identifiers from subject
11729     (when gnus-list-identifiers
11730       (gnus-summary-remove-list-identifiers))
11731     ;; First and last article in this newsgroup.
11732     (when gnus-newsgroup-headers
11733       (setq gnus-newsgroup-begin
11734             (mail-header-number (car gnus-newsgroup-headers))
11735             gnus-newsgroup-end
11736             (mail-header-number
11737              (gnus-last-element gnus-newsgroup-headers))))
11738     (when gnus-use-scoring
11739       (gnus-possibly-score-headers))))
11740
11741 (defun gnus-summary-insert-old-articles (&optional all)
11742   "Insert all old articles in this group.
11743 If ALL is non-nil, already read articles become readable.
11744 If ALL is a number, fetch this number of articles."
11745   (interactive "P")
11746   (prog1
11747       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11748             older len)
11749         (setq older
11750               ;; Some nntp servers lie about their active range.  When
11751               ;; this happens, the active range can be in the millions.
11752               ;; Use a compressed range to avoid creating a huge list.
11753               (gnus-range-difference (list gnus-newsgroup-active) old))
11754         (setq len (gnus-range-length older))
11755         (cond
11756          ((null older) nil)
11757          ((numberp all)
11758           (if (< all len)
11759               (let ((older-range (nreverse older)))
11760                 (setq older nil)
11761
11762                 (while (> all 0)
11763                   (let* ((r (pop older-range))
11764                          (min (if (numberp r) r (car r)))
11765                          (max (if (numberp r) r (cdr r))))
11766                     (while (and (<= min max)
11767                                 (> all 0))
11768                       (push max older)
11769                       (setq all (1- all)
11770                             max (1- max))))))
11771             (setq older (gnus-uncompress-range older))))
11772          (all
11773           (setq older (gnus-uncompress-range older)))
11774          (t
11775           (when (and (numberp gnus-large-newsgroup)
11776                    (> len gnus-large-newsgroup))
11777               (let* ((cursor-in-echo-area nil)
11778                      (initial (gnus-parameter-large-newsgroup-initial
11779                                gnus-newsgroup-name))
11780                      (input
11781                       (read-string
11782                        (format
11783                         "How many articles from %s (%s %d): "
11784                         (gnus-limit-string
11785                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11786                         (if initial "max" "default")
11787                         len)
11788                        (if initial
11789                            (cons (number-to-string initial)
11790                                  0)))))
11791                 (unless (string-match "^[ \t]*$" input)
11792                   (setq all (string-to-number input))
11793                   (if (< all len)
11794                       (let ((older-range (nreverse older)))
11795                         (setq older nil)
11796
11797                         (while (> all 0)
11798                           (let* ((r (pop older-range))
11799                                  (min (if (numberp r) r (car r)))
11800                                  (max (if (numberp r) r (cdr r))))
11801                             (while (and (<= min max)
11802                                         (> all 0))
11803                               (push max older)
11804                               (setq all (1- all)
11805                                     max (1- max))))))))))
11806           (setq older (gnus-uncompress-range older))))
11807         (if (not older)
11808             (message "No old news.")
11809           (gnus-summary-insert-articles older)
11810           (gnus-summary-limit (gnus-sorted-nunion old older))))
11811     (gnus-summary-position-point)))
11812
11813 (defun gnus-summary-insert-new-articles ()
11814   "Insert all new articles in this group."
11815   (interactive)
11816   (prog1
11817       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11818             (old-active gnus-newsgroup-active)
11819             (nnmail-fetched-sources (list t))
11820             i new)
11821         (setq gnus-newsgroup-active
11822               (gnus-activate-group gnus-newsgroup-name 'scan))
11823         (setq i (cdr gnus-newsgroup-active))
11824         (while (> i (cdr old-active))
11825           (push i new)
11826           (decf i))
11827         (if (not new)
11828             (message "No gnus is bad news.")
11829           (gnus-summary-insert-articles new)
11830           (setq gnus-newsgroup-unreads
11831                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11832           (gnus-summary-limit (gnus-sorted-nunion old new))))
11833     (gnus-summary-position-point)))
11834
11835 (gnus-summary-make-all-marking-commands)
11836
11837 (gnus-ems-redefine)
11838
11839 (provide 'gnus-sum)
11840
11841 (run-hooks 'gnus-sum-load-hook)
11842
11843 ;;; gnus-sum.el ends here