* nnimap.el (nnimap-split-fancy): Ditto.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for 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 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If non-nil, select the article under point.
296 Which article this is is controlled by the `gnus-auto-select-subject'
297 variable.
298
299 If you want to prevent automatic selection of articles in some
300 newsgroups, set the variable to nil in `gnus-select-group-hook'."
301   :group 'gnus-group-select
302   :type '(choice (const :tag "none" nil)
303                  (sexp :menu-tag "first" t)))
304
305 (defcustom gnus-auto-select-subject 'unread
306   "*Says what subject to place under point when entering a group.
307
308 This variable can either be the symbols `first' (place point on the
309 first subject), `unread' (place point on the subject line of the first
310 unread article), `best' (place point on the subject line of the
311 higest-scored article), `unseen' (place point on the subject line of
312 the first unseen article), 'unseen-or-unread' (place point on the subject
313 line of the first unseen article or, if all article have been seen, on the
314 subject line of the first unread article), or a function to be called to
315 place point on some subject line."
316   :group 'gnus-group-select
317   :type '(choice (const best)
318                  (const unread)
319                  (const first)
320                  (const unseen)
321                  (const unseen-or-unread)))
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-auto-goto-ignores 'unfetched
349   "*Says how to handle unfetched articles when maneuvering.
350
351 This variable can either be the symbols `nil' (maneuver to any
352 article), `undownloaded' (maneuvering while unplugged ignores articles
353 that have not been fetched), `always-undownloaded' (maneuvering always
354 ignores articles that have not been fetched), `unfetched' (maneuvering
355 ignores articles whose headers have not been fetched).
356
357 NOTE: The list of unfetched articles will always be nil when plugged
358 and, when unplugged, a subset of the undownloaded article list."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "None" nil)
361                  (const :tag "Undownloaded when unplugged" undownloaded)
362                  (const :tag "Undownloaded" always-undownloaded)
363                  (const :tag "Unfetched" unfetched)))
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defvar gnus-auto-center-group t
383   "*If non-nil, always center the group buffer.")
384
385 (defcustom gnus-show-all-headers nil
386   "*If non-nil, don't hide any headers."
387   :group 'gnus-article-hiding
388   :group 'gnus-article-headers
389   :type 'boolean)
390
391 (defcustom gnus-summary-ignore-duplicates nil
392   "*If non-nil, ignore articles with identical Message-ID headers."
393   :group 'gnus-summary
394   :type 'boolean)
395
396 (defcustom gnus-single-article-buffer t
397   "*If non-nil, display all articles in the same buffer.
398 If nil, each group will get its own article buffer."
399   :group 'gnus-article-various
400   :type 'boolean)
401
402 (defcustom gnus-break-pages t
403   "*If non-nil, do page breaking on articles.
404 The page delimiter is specified by the `gnus-page-delimiter'
405 variable."
406   :group 'gnus-article-various
407   :type 'boolean)
408
409 (defcustom gnus-move-split-methods nil
410   "*Variable used to suggest where articles are to be moved to.
411 It uses the same syntax as the `gnus-split-methods' variable.
412 However, whereas `gnus-split-methods' specifies file names as targets,
413 this variable specifies group names."
414   :group 'gnus-summary-mail
415   :type '(repeat (choice (list :value (fun) function)
416                          (cons :value ("" "") regexp (repeat string))
417                          (sexp :value nil))))
418
419 (defcustom gnus-unread-mark ?           ;Whitespace
420   "*Mark used for unread articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ticked-mark ?!
425   "*Mark used for ticked articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-dormant-mark ??
430   "*Mark used for dormant articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-del-mark ?r
435   "*Mark used for del'd articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-read-mark ?R
440   "*Mark used for read articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-expirable-mark ?E
445   "*Mark used for expirable articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-killed-mark ?K
450   "*Mark used for killed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-spam-mark ?$
455   "*Mark used for spam articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-souped-mark ?F
460   "*Mark used for souped articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-kill-file-mark ?X
465   "*Mark used for articles killed by kill files."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-low-score-mark ?Y
470   "*Mark used for articles with a low score."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-catchup-mark ?C
475   "*Mark used for articles that are caught up."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-replied-mark ?A
480   "*Mark used for articles that have been replied to."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-forwarded-mark ?F
485   "*Mark used for articles that have been forwarded."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-recent-mark ?N
490   "*Mark used for articles that are recent."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-cached-mark ?*
495   "*Mark used for articles that are in the cache."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-saved-mark ?S
500   "*Mark used for articles that have been saved."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-unseen-mark ?.
505   "*Mark used for articles that haven't been seen."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-no-mark ?               ;Whitespace
510   "*Mark used for articles that have no other secondary mark."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-ancient-mark ?O
515   "*Mark used for ancient articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-sparse-mark ?Q
520   "*Mark used for sparsely reffed articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-canceled-mark ?G
525   "*Mark used for canceled articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-duplicate-mark ?M
530   "*Mark used for duplicate articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-undownloaded-mark ?-
535   "*Mark used for articles that weren't downloaded."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-downloaded-mark ?+
540   "*Mark used for articles that were downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloadable-mark ?%
545   "*Mark used for articles that are to be downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unsendable-mark ?=
550   "*Mark used for articles that won't be sent."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-score-over-mark ?+
555   "*Score mark used for articles with high scores."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-below-mark ?-
560   "*Score mark used for articles with low scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-empty-thread-mark ?     ;Whitespace
565   "*There is no thread under the article."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-not-empty-thread-mark ?=
570   "*There is a thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-view-pseudo-asynchronously nil
575   "*If non-nil, Gnus will view pseudo-articles asynchronously."
576   :group 'gnus-extract-view
577   :type 'boolean)
578
579 (defcustom gnus-auto-expirable-marks
580   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
581         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
582         gnus-souped-mark gnus-duplicate-mark)
583   "*The list of marks converted into expiration if a group is auto-expirable."
584   :version "21.1"
585   :group 'gnus-summary
586   :type '(repeat character))
587
588 (defcustom gnus-inhibit-user-auto-expire t
589   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
590   :version "21.1"
591   :group 'gnus-summary
592   :type 'boolean)
593
594 (defcustom gnus-view-pseudos nil
595   "*If `automatic', pseudo-articles will be viewed automatically.
596 If `not-confirm', pseudos will be viewed automatically, and the user
597 will not be asked to confirm the command."
598   :group 'gnus-extract-view
599   :type '(choice (const :tag "off" nil)
600                  (const automatic)
601                  (const not-confirm)))
602
603 (defcustom gnus-view-pseudos-separately t
604   "*If non-nil, one pseudo-article will be created for each file to be viewed.
605 If nil, all files that use the same viewing command will be given as a
606 list of parameters to that command."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-insert-pseudo-articles t
611   "*If non-nil, insert pseudo-articles when decoding articles."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-summary-dummy-line-format
616   "   %(:                             :%) %S\n"
617   "*The format specification for the dummy roots in the summary buffer.
618 It works along the same lines as a normal formatting string,
619 with some simple extensions.
620
621 %S  The subject
622
623 General format specifiers can also be used.
624 See `(gnus)Formatting Variables'."
625   :link '(custom-manual "(gnus)Formatting Variables")
626   :group 'gnus-threading
627   :type 'string)
628
629 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
630   "*The format specification for the summary mode line.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions:
633
634 %G  Group name
635 %p  Unprefixed group name
636 %A  Current article number
637 %z  Current article score
638 %V  Gnus version
639 %U  Number of unread articles in the group
640 %e  Number of unselected articles in the group
641 %Z  A string with unread/unselected article counts
642 %g  Shortish group name
643 %S  Subject of the current article
644 %u  User-defined spec
645 %s  Current score file name
646 %d  Number of dormant articles
647 %r  Number of articles that have been marked as read in this session
648 %E  Number of articles expunged by the score files"
649   :group 'gnus-summary-format
650   :type 'string)
651
652 (defcustom gnus-list-identifiers nil
653   "Regexp that matches list identifiers to be removed from subject.
654 This can also be a list of regexps."
655   :version "21.1"
656   :group 'gnus-summary-format
657   :group 'gnus-article-hiding
658   :type '(choice (const :tag "none" nil)
659                  (regexp :value ".*")
660                  (repeat :value (".*") regexp)))
661
662 (defcustom gnus-summary-mark-below 0
663   "*Mark all articles with a score below this variable as read.
664 This variable is local to each summary buffer and usually set by the
665 score file."
666   :group 'gnus-score-default
667   :type 'integer)
668
669 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
670   "*List of functions used for sorting articles in the summary buffer.
671
672 Each function takes two articles and returns non-nil if the first
673 article should be sorted before the other.  If you use more than one
674 function, the primary sort function should be the last.  You should
675 probably always include `gnus-article-sort-by-number' in the list of
676 sorting functions -- preferably first.  Also note that sorting by date
677 is often much slower than sorting by number, and the sorting order is
678 very similar.  (Sorting by date means sorting by the time the message
679 was sent, sorting by number means sorting by arrival time.)
680
681 Ready-made functions include `gnus-article-sort-by-number',
682 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
683 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
684 and `gnus-article-sort-by-score'.
685
686 When threading is turned on, the variable `gnus-thread-sort-functions'
687 controls how articles are sorted."
688   :group 'gnus-summary-sort
689   :type '(repeat (choice (function-item gnus-article-sort-by-number)
690                          (function-item gnus-article-sort-by-author)
691                          (function-item gnus-article-sort-by-subject)
692                          (function-item gnus-article-sort-by-date)
693                          (function-item gnus-article-sort-by-score)
694                          (function-item gnus-article-sort-by-random)
695                          (function :tag "other"))))
696
697 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
698   "*List of functions used for sorting threads in the summary buffer.
699 By default, threads are sorted by article number.
700
701 Each function takes two threads and returns non-nil if the first
702 thread should be sorted before the other.  If you use more than one
703 function, the primary sort function should be the last.  You should
704 probably always include `gnus-thread-sort-by-number' in the list of
705 sorting functions -- preferably first.  Also note that sorting by date
706 is often much slower than sorting by number, and the sorting order is
707 very similar.  (Sorting by date means sorting by the time the message
708 was sent, sorting by number means sorting by arrival time.)
709
710 Ready-made functions include `gnus-thread-sort-by-number',
711 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
712 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
713 `gnus-thread-sort-by-most-recent-number',
714 `gnus-thread-sort-by-most-recent-date',
715 `gnus-thread-sort-by-random', and
716 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
717
718 When threading is turned off, the variable
719 `gnus-article-sort-functions' controls how articles are sorted."
720   :group 'gnus-summary-sort
721   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
722                          (function-item gnus-thread-sort-by-author)
723                          (function-item gnus-thread-sort-by-subject)
724                          (function-item gnus-thread-sort-by-date)
725                          (function-item gnus-thread-sort-by-score)
726                          (function-item gnus-thread-sort-by-total-score)
727                          (function-item gnus-thread-sort-by-random)
728                          (function :tag "other"))))
729
730 (defcustom gnus-thread-score-function '+
731   "*Function used for calculating the total score of a thread.
732
733 The function is called with the scores of the article and each
734 subthread and should then return the score of the thread.
735
736 Some functions you can use are `+', `max', or `min'."
737   :group 'gnus-summary-sort
738   :type 'function)
739
740 (defcustom gnus-summary-expunge-below nil
741   "All articles that have a score less than this variable will be expunged.
742 This variable is local to the summary buffers."
743   :group 'gnus-score-default
744   :type '(choice (const :tag "off" nil)
745                  integer))
746
747 (defcustom gnus-thread-expunge-below nil
748   "All threads that have a total score less than this variable will be expunged.
749 See `gnus-thread-score-function' for en explanation of what a
750 \"thread score\" is.
751
752 This variable is local to the summary buffers."
753   :group 'gnus-threading
754   :group 'gnus-score-default
755   :type '(choice (const :tag "off" nil)
756                  integer))
757
758 (defcustom gnus-summary-mode-hook nil
759   "*A hook for Gnus summary mode.
760 This hook is run before any variables are set in the summary buffer."
761   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
762   :group 'gnus-summary-various
763   :type 'hook)
764
765 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
766 (when (featurep 'xemacs)
767   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
769   (add-hook 'gnus-summary-mode-hook
770             'gnus-xmas-switch-horizontal-scrollbar-off))
771
772 (defcustom gnus-summary-menu-hook nil
773   "*Hook run after the creation of the summary mode menu."
774   :group 'gnus-summary-visual
775   :type 'hook)
776
777 (defcustom gnus-summary-exit-hook nil
778   "*A hook called on exit from the summary buffer.
779 It will be called with point in the group buffer."
780   :group 'gnus-summary-exit
781   :type 'hook)
782
783 (defcustom gnus-summary-prepare-hook nil
784   "*A hook called after the summary buffer has been generated.
785 If you want to modify the summary buffer, you can use this hook."
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 (defcustom gnus-summary-prepared-hook nil
790   "*A hook called as the last thing after the summary buffer has been generated."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-generate-hook nil
795   "*A hook run just before generating the summary buffer.
796 This hook is commonly used to customize threading variables and the
797 like."
798   :group 'gnus-summary-various
799   :type 'hook)
800
801 (defcustom gnus-select-group-hook nil
802   "*A hook called when a newsgroup is selected.
803
804 If you'd like to simplify subjects like the
805 `gnus-summary-next-same-subject' command does, you can use the
806 following hook:
807
808  (add-hook gnus-select-group-hook
809            (lambda ()
810              (mapcar (lambda (header)
811                        (mail-header-set-subject
812                         header
813                         (gnus-simplify-subject
814                          (mail-header-subject header) 're-only)))
815                      gnus-newsgroup-headers)))"
816   :group 'gnus-group-select
817   :type 'hook)
818
819 (defcustom gnus-select-article-hook nil
820   "*A hook called when an article is selected."
821   :group 'gnus-summary-choose
822   :options '(gnus-agent-fetch-selected-article)
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook nil
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-article-move-hook nil
873   "*A hook called after an article is moved, copied, respooled, or crossposted."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-delete-hook nil
878   "*A hook called after an article is deleted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-expire-hook nil
883   "*A hook called after an article is expired."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-display-arrow
888   (and (fboundp 'display-graphic-p)
889        (display-graphic-p))
890   "*If non-nil, display an arrow highlighting the current article."
891   :version "21.1"
892   :group 'gnus-summary
893   :type 'boolean)
894
895 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
896   "Face used for highlighting the current article in the summary buffer."
897   :group 'gnus-summary-visual
898   :type 'face)
899
900 (defvar gnus-tmp-downloaded nil)
901
902 (defcustom gnus-summary-highlight
903   '(((eq mark gnus-canceled-mark)
904      . gnus-summary-cancelled-face)
905     ((and uncached (> score default-high))
906      . gnus-summary-high-undownloaded-face)
907     ((and uncached (< score default-low))
908      . gnus-summary-low-undownloaded-face)
909     (uncached
910      . gnus-summary-normal-undownloaded-face)
911     ((and (> score default-high)
912           (or (eq mark gnus-dormant-mark)
913               (eq mark gnus-ticked-mark)))
914      . gnus-summary-high-ticked-face)
915     ((and (< score default-low)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-low-ticked-face)
919     ((or (eq mark gnus-dormant-mark)
920          (eq mark gnus-ticked-mark))
921      . gnus-summary-normal-ticked-face)
922     ((and (> score default-high) (eq mark gnus-ancient-mark))
923      . gnus-summary-high-ancient-face)
924     ((and (< score default-low) (eq mark gnus-ancient-mark))
925      . gnus-summary-low-ancient-face)
926     ((eq mark gnus-ancient-mark)
927      . gnus-summary-normal-ancient-face)
928     ((and (> score default-high) (eq mark gnus-unread-mark))
929      . gnus-summary-high-unread-face)
930     ((and (< score default-low) (eq mark gnus-unread-mark))
931      . gnus-summary-low-unread-face)
932     ((eq mark gnus-unread-mark)
933      . gnus-summary-normal-unread-face)
934     ((> score default-high)
935      . gnus-summary-high-read-face)
936     ((< score default-low)
937      . gnus-summary-low-read-face)
938     (t
939      . gnus-summary-normal-read-face))
940   "*Controls the highlighting of summary buffer lines.
941
942 A list of (FORM . FACE) pairs.  When deciding how a a particular
943 summary line should be displayed, each form is evaluated.  The content
944 of the face field after the first true form is used.  You can change
945 how those summary lines are displayed, by editing the face field.
946
947 You can use the following variables in the FORM field.
948
949 score:        The article's score
950 default:      The default article score.
951 default-high: The default score for high scored articles.
952 default-low:  The default score for low scored articles.
953 below:        The score below which articles are automatically marked as read.
954 mark:         The articles mark."
955   :group 'gnus-summary-visual
956   :type '(repeat (cons (sexp :tag "Form" nil)
957                        face)))
958
959 (defcustom gnus-alter-header-function nil
960   "Function called to allow alteration of article header structures.
961 The function is called with one parameter, the article header vector,
962 which it may alter in any way."
963   :type '(choice (const :tag "None" nil)
964                  function)
965   :group 'gnus-summary)
966
967 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
968   "Variable that says which function should be used to decode a string with encoded words.")
969
970 (defcustom gnus-extra-headers '(To Newsgroups)
971   "*Extra headers to parse."
972   :version "21.1"
973   :group 'gnus-summary
974   :type '(repeat symbol))
975
976 (defcustom gnus-ignored-from-addresses
977   (and user-mail-address (regexp-quote user-mail-address))
978   "*Regexp of From headers that may be suppressed in favor of To headers."
979   :version "21.1"
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
984   "List of charsets that should be ignored.
985 When these charsets are used in the \"charset\" parameter, the
986 default charset will be used instead."
987   :version "21.1"
988   :type '(repeat symbol)
989   :group 'gnus-charset)
990
991 (gnus-define-group-parameter
992  ignored-charsets
993  :type list
994  :function-document
995  "Return the ignored charsets of GROUP."
996  :variable gnus-group-ignored-charsets-alist
997  :variable-default
998  '(("alt\\.chinese\\.text" iso-8859-1))
999  :variable-document
1000  "Alist of regexps (to match group names) and charsets that should be ignored.
1001 When these charsets are used in the \"charset\" parameter, the
1002 default charset will be used instead."
1003  :variable-group gnus-charset
1004  :variable-type '(repeat (cons (regexp :tag "Group")
1005                                (repeat symbol)))
1006  :parameter-type '(choice :tag "Ignored charsets"
1007                           :value nil
1008                           (repeat (symbol)))
1009  :parameter-document       "\
1010 List of charsets that should be ignored.
1011
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead.")
1014
1015 (defcustom gnus-group-highlight-words-alist nil
1016   "Alist of group regexps and highlight regexps.
1017 This variable uses the same syntax as `gnus-emphasis-alist'."
1018   :version "21.1"
1019   :type '(repeat (cons (regexp :tag "Group")
1020                        (repeat (list (regexp :tag "Highlight regexp")
1021                                      (number :tag "Group for entire word" 0)
1022                                      (number :tag "Group for displayed part" 0)
1023                                      (symbol :tag "Face"
1024                                              gnus-emphasis-highlight-words)))))
1025   :group 'gnus-summary-visual)
1026
1027 (defcustom gnus-summary-show-article-charset-alist
1028   nil
1029   "Alist of number and charset.
1030 The article will be shown with the charset corresponding to the
1031 numbered argument.
1032 For example: ((1 . cn-gb-2312) (2 . big5))."
1033   :version "21.1"
1034   :type '(repeat (cons (number :tag "Argument" 1)
1035                        (symbol :tag "Charset")))
1036   :group 'gnus-charset)
1037
1038 (defcustom gnus-preserve-marks t
1039   "Whether marks are preserved when moving, copying and respooling messages."
1040   :version "21.1"
1041   :type 'boolean
1042   :group 'gnus-summary-marks)
1043
1044 (defcustom gnus-alter-articles-to-read-function nil
1045   "Function to be called to alter the list of articles to be selected."
1046   :type '(choice (const nil) function)
1047   :group 'gnus-summary)
1048
1049 (defcustom gnus-orphan-score nil
1050   "*All orphans get this score added.  Set in the score file."
1051   :group 'gnus-score-default
1052   :type '(choice (const nil)
1053                  integer))
1054
1055 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1056   "*A regexp to match MIME parts when saving multiple parts of a
1057 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1058 This regexp will be used by default when prompting the user for which
1059 type of files to save."
1060   :group 'gnus-summary
1061   :type 'regexp)
1062
1063 (defcustom gnus-read-all-available-headers nil
1064   "Whether Gnus should parse all headers made available to it.
1065 This is mostly relevant for slow backends where the user may
1066 wish to widen the summary buffer to include all headers
1067 that were fetched.  Say, for nnultimate groups."
1068   :group 'gnus-summary
1069   :type '(choice boolean regexp))
1070
1071 (defcustom gnus-summary-muttprint-program "muttprint"
1072   "Command (and optional arguments) used to run Muttprint."
1073   :version "21.3"
1074   :group 'gnus-summary
1075   :type 'string)
1076
1077 (defcustom gnus-article-loose-mime nil
1078   "If non-nil, don't require MIME-Version header.
1079 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1080 supply the MIME-Version header or deliberately strip it From the mail.
1081 Set it to non-nil, Gnus will treat some articles as MIME even if
1082 the MIME-Version header is missed."
1083   :version "21.3"
1084   :type 'boolean
1085   :group 'gnus-article-mime)
1086
1087 (defcustom gnus-article-emulate-mime t
1088   "If non-nil, use MIME emulation for uuencode and the like.
1089 This means that Gnus will search message bodies for text that look
1090 like uuencoded bits, yEncoded bits, and so on, and present that using
1091 the normal Gnus MIME machinery."
1092   :type 'boolean
1093   :group 'gnus-article-mime)
1094
1095 ;;; Internal variables
1096
1097 (defvar gnus-summary-display-cache nil)
1098 (defvar gnus-article-mime-handles nil)
1099 (defvar gnus-article-decoded-p nil)
1100 (defvar gnus-article-charset nil)
1101 (defvar gnus-article-ignored-charsets nil)
1102 (defvar gnus-scores-exclude-files nil)
1103 (defvar gnus-page-broken nil)
1104
1105 (defvar gnus-original-article nil)
1106 (defvar gnus-article-internal-prepare-hook nil)
1107 (defvar gnus-newsgroup-process-stack nil)
1108
1109 (defvar gnus-thread-indent-array nil)
1110 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1111 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1112   "Function called to sort the articles within a thread after it has been gathered together.")
1113
1114 (defvar gnus-summary-save-parts-type-history nil)
1115 (defvar gnus-summary-save-parts-last-directory nil)
1116
1117 ;; Avoid highlighting in kill files.
1118 (defvar gnus-summary-inhibit-highlight nil)
1119 (defvar gnus-newsgroup-selected-overlay nil)
1120 (defvar gnus-inhibit-limiting nil)
1121 (defvar gnus-newsgroup-adaptive-score-file nil)
1122 (defvar gnus-current-score-file nil)
1123 (defvar gnus-current-move-group nil)
1124 (defvar gnus-current-copy-group nil)
1125 (defvar gnus-current-crosspost-group nil)
1126 (defvar gnus-newsgroup-display nil)
1127
1128 (defvar gnus-newsgroup-dependencies nil)
1129 (defvar gnus-newsgroup-adaptive nil)
1130 (defvar gnus-summary-display-article-function nil)
1131 (defvar gnus-summary-highlight-line-function nil
1132   "Function called after highlighting a summary line.")
1133
1134 (defvar gnus-summary-line-format-alist
1135   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1136     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1137     (?s gnus-tmp-subject-or-nil ?s)
1138     (?n gnus-tmp-name ?s)
1139     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1140         ?s)
1141     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1142             gnus-tmp-from) ?s)
1143     (?F gnus-tmp-from ?s)
1144     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1145     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1146     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1147     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1148     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1149     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1150     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1151     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1152     (?L gnus-tmp-lines ?s)
1153     (?O gnus-tmp-downloaded ?c)
1154     (?I gnus-tmp-indentation ?s)
1155     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1156     (?R gnus-tmp-replied ?c)
1157     (?\[ gnus-tmp-opening-bracket ?c)
1158     (?\] gnus-tmp-closing-bracket ?c)
1159     (?\> (make-string gnus-tmp-level ? ) ?s)
1160     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1161     (?i gnus-tmp-score ?d)
1162     (?z gnus-tmp-score-char ?c)
1163     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1164     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1165     (?U gnus-tmp-unread ?c)
1166     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1167         ?s)
1168     (?t (gnus-summary-number-of-articles-in-thread
1169          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1170         ?d)
1171     (?e (gnus-summary-number-of-articles-in-thread
1172          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1173         ?c)
1174     (?u gnus-tmp-user-defined ?s)
1175     (?P (gnus-pick-line-number) ?d)
1176     (?B gnus-tmp-thread-tree-header-string ?s)
1177     (user-date (gnus-user-date
1178                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1179   "An alist of format specifications that can appear in summary lines.
1180 These are paired with what variables they correspond with, along with
1181 the type of the variable (string, integer, character, etc).")
1182
1183 (defvar gnus-summary-dummy-line-format-alist
1184   `((?S gnus-tmp-subject ?s)
1185     (?N gnus-tmp-number ?d)
1186     (?u gnus-tmp-user-defined ?s)))
1187
1188 (defvar gnus-summary-mode-line-format-alist
1189   `((?G gnus-tmp-group-name ?s)
1190     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1191     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1192     (?A gnus-tmp-article-number ?d)
1193     (?Z gnus-tmp-unread-and-unselected ?s)
1194     (?V gnus-version ?s)
1195     (?U gnus-tmp-unread-and-unticked ?d)
1196     (?S gnus-tmp-subject ?s)
1197     (?e gnus-tmp-unselected ?d)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?d (length gnus-newsgroup-dormant) ?d)
1200     (?t (length gnus-newsgroup-marked) ?d)
1201     (?h (length gnus-newsgroup-spam-marked) ?d)
1202     (?r (length gnus-newsgroup-reads) ?d)
1203     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1204     (?E gnus-newsgroup-expunged-tally ?d)
1205     (?s (gnus-current-score-file-nondirectory) ?s)))
1206
1207 (defvar gnus-last-search-regexp nil
1208   "Default regexp for article search command.")
1209
1210 (defvar gnus-last-shell-command nil
1211   "Default shell command on article.")
1212
1213 (defvar gnus-newsgroup-agentized nil
1214   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1215 (defvar gnus-newsgroup-begin nil)
1216 (defvar gnus-newsgroup-end nil)
1217 (defvar gnus-newsgroup-last-rmail nil)
1218 (defvar gnus-newsgroup-last-mail nil)
1219 (defvar gnus-newsgroup-last-folder nil)
1220 (defvar gnus-newsgroup-last-file nil)
1221 (defvar gnus-newsgroup-auto-expire nil)
1222 (defvar gnus-newsgroup-active nil)
1223
1224 (defvar gnus-newsgroup-data nil)
1225 (defvar gnus-newsgroup-data-reverse nil)
1226 (defvar gnus-newsgroup-limit nil)
1227 (defvar gnus-newsgroup-limits nil)
1228
1229 (defvar gnus-newsgroup-unreads nil
1230   "Sorted list of unread articles in the current newsgroup.")
1231
1232 (defvar gnus-newsgroup-unselected nil
1233   "Sorted list of unselected unread articles in the current newsgroup.")
1234
1235 (defvar gnus-newsgroup-reads nil
1236   "Alist of read articles and article marks in the current newsgroup.")
1237
1238 (defvar gnus-newsgroup-expunged-tally nil)
1239
1240 (defvar gnus-newsgroup-marked nil
1241   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1242
1243 (defvar gnus-newsgroup-spam-marked nil
1244   "List of ranges of articles that have been marked as spam.")
1245
1246 (defvar gnus-newsgroup-killed nil
1247   "List of ranges of articles that have been through the scoring process.")
1248
1249 (defvar gnus-newsgroup-cached nil
1250   "Sorted list of articles that come from the article cache.")
1251
1252 (defvar gnus-newsgroup-saved nil
1253   "List of articles that have been saved.")
1254
1255 (defvar gnus-newsgroup-kill-headers nil)
1256
1257 (defvar gnus-newsgroup-replied nil
1258   "List of articles that have been replied to in the current newsgroup.")
1259
1260 (defvar gnus-newsgroup-forwarded nil
1261   "List of articles that have been forwarded in the current newsgroup.")
1262
1263 (defvar gnus-newsgroup-recent nil
1264   "List of articles that have are recent in the current newsgroup.")
1265
1266 (defvar gnus-newsgroup-expirable nil
1267   "Sorted list of articles in the current newsgroup that can be expired.")
1268
1269 (defvar gnus-newsgroup-processable nil
1270   "List of articles in the current newsgroup that can be processed.")
1271
1272 (defvar gnus-newsgroup-downloadable nil
1273   "Sorted list of articles in the current newsgroup that can be processed.")
1274
1275 (defvar gnus-newsgroup-unfetched nil
1276   "Sorted list of articles in the current newsgroup whose headers have
1277 not been fetched into the agent.
1278
1279 This list will always be a subset of gnus-newsgroup-undownloaded.")
1280
1281 (defvar gnus-newsgroup-undownloaded nil
1282   "List of articles in the current newsgroup that haven't been downloaded.")
1283
1284 (defvar gnus-newsgroup-unsendable nil
1285   "List of articles in the current newsgroup that won't be sent.")
1286
1287 (defvar gnus-newsgroup-bookmarks nil
1288   "List of articles in the current newsgroup that have bookmarks.")
1289
1290 (defvar gnus-newsgroup-dormant nil
1291   "Sorted list of dormant articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-unseen nil
1294   "List of unseen articles in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-seen nil
1297   "Range of seen articles in the current newsgroup.")
1298
1299 (defvar gnus-newsgroup-articles nil
1300   "List of articles in the current newsgroup.")
1301
1302 (defvar gnus-newsgroup-scored nil
1303   "List of scored articles in the current newsgroup.")
1304
1305 (defvar gnus-newsgroup-headers nil
1306   "List of article headers in the current newsgroup.")
1307
1308 (defvar gnus-newsgroup-threads nil)
1309
1310 (defvar gnus-newsgroup-prepared nil
1311   "Whether the current group has been prepared properly.")
1312
1313 (defvar gnus-newsgroup-ancient nil
1314   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1315
1316 (defvar gnus-newsgroup-sparse nil)
1317
1318 (defvar gnus-current-article nil)
1319 (defvar gnus-article-current nil)
1320 (defvar gnus-current-headers nil)
1321 (defvar gnus-have-all-headers nil)
1322 (defvar gnus-last-article nil)
1323 (defvar gnus-newsgroup-history nil)
1324 (defvar gnus-newsgroup-charset nil)
1325 (defvar gnus-newsgroup-ephemeral-charset nil)
1326 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1327
1328 (defvar gnus-article-before-search nil)
1329
1330 (defconst gnus-summary-local-variables
1331   '(gnus-newsgroup-name
1332     gnus-newsgroup-begin gnus-newsgroup-end
1333     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1334     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1335     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1336     gnus-newsgroup-unselected gnus-newsgroup-marked
1337     gnus-newsgroup-spam-marked
1338     gnus-newsgroup-reads gnus-newsgroup-saved
1339     gnus-newsgroup-replied gnus-newsgroup-forwarded
1340     gnus-newsgroup-recent
1341     gnus-newsgroup-expirable
1342     gnus-newsgroup-processable gnus-newsgroup-killed
1343     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1344     gnus-newsgroup-unfetched
1345     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1346     gnus-newsgroup-seen gnus-newsgroup-articles
1347     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1348     gnus-newsgroup-headers gnus-newsgroup-threads
1349     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1350     gnus-current-article gnus-current-headers gnus-have-all-headers
1351     gnus-last-article gnus-article-internal-prepare-hook
1352     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1353     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1354     gnus-thread-expunge-below
1355     gnus-score-alist gnus-current-score-file
1356     (gnus-summary-expunge-below . global)
1357     (gnus-summary-mark-below . global)
1358     (gnus-orphan-score . global)
1359     gnus-newsgroup-active gnus-scores-exclude-files
1360     gnus-newsgroup-history gnus-newsgroup-ancient
1361     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1362     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1363     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1364     (gnus-newsgroup-expunged-tally . 0)
1365     gnus-cache-removable-articles gnus-newsgroup-cached
1366     gnus-newsgroup-data gnus-newsgroup-data-reverse
1367     gnus-newsgroup-limit gnus-newsgroup-limits
1368     gnus-newsgroup-charset gnus-newsgroup-display)
1369   "Variables that are buffer-local to the summary buffers.")
1370
1371 (defvar gnus-newsgroup-variables nil
1372   "A list of variables that have separate values in different newsgroups.
1373 A list of newsgroup (summary buffer) local variables, or cons of
1374 variables and their default values (when the default values are not
1375 nil), that should be made global while the summary buffer is active.
1376 These variables can be used to set variables in the group parameters
1377 while still allowing them to affect operations done in other
1378 buffers. For example:
1379
1380 \(setq gnus-newsgroup-variables
1381      '(message-use-followup-to
1382        (gnus-visible-headers .
1383          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1384 ")
1385
1386 ;; Byte-compiler warning.
1387 (eval-when-compile
1388   ;; Bind features so that require will believe that gnus-sum has
1389   ;; already been loaded (avoids infinite recursion)
1390   (let ((features (cons 'gnus-sum features)))
1391     ;; Several of the declarations in gnus-sum are needed to load the
1392     ;; following files. Right now, these definitions have been
1393     ;; compiled but not defined (evaluated).  We could either do a
1394     ;; eval-and-compile about all of the declarations or evaluate the
1395     ;; source file.
1396     (if (boundp 'gnus-newsgroup-variables)
1397         nil
1398       (load "gnus-sum.el" t t t))
1399     (require 'gnus)
1400     (require 'gnus-agent)
1401     (require 'gnus-art)))
1402
1403 ;; MIME stuff.
1404
1405 (defvar gnus-decode-encoded-word-methods
1406   '(mail-decode-encoded-word-string)
1407   "List of methods used to decode encoded words.
1408
1409 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1410 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1411 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1412 whose names match REGEXP.
1413
1414 For example:
1415 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1416  mail-decode-encoded-word-string
1417  (\"chinese\" . rfc1843-decode-string))")
1418
1419 (defvar gnus-decode-encoded-word-methods-cache nil)
1420
1421 (defun gnus-multi-decode-encoded-word-string (string)
1422   "Apply the functions from `gnus-encoded-word-methods' that match."
1423   (unless (and gnus-decode-encoded-word-methods-cache
1424                (eq gnus-newsgroup-name
1425                    (car gnus-decode-encoded-word-methods-cache)))
1426     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1427     (mapcar (lambda (x)
1428               (if (symbolp x)
1429                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1430                 (if (and gnus-newsgroup-name
1431                          (string-match (car x) gnus-newsgroup-name))
1432                     (nconc gnus-decode-encoded-word-methods-cache
1433                            (list (cdr x))))))
1434             gnus-decode-encoded-word-methods))
1435   (let ((xlist gnus-decode-encoded-word-methods-cache))
1436     (pop xlist)
1437     (while xlist
1438       (setq string (funcall (pop xlist) string))))
1439   string)
1440
1441 ;; Subject simplification.
1442
1443 (defun gnus-simplify-whitespace (str)
1444   "Remove excessive whitespace from STR."
1445   ;; Multiple spaces.
1446   (while (string-match "[ \t][ \t]+" str)
1447     (setq str (concat (substring str 0 (match-beginning 0))
1448                         " "
1449                         (substring str (match-end 0)))))
1450   ;; Leading spaces.
1451   (when (string-match "^[ \t]+" str)
1452     (setq str (substring str (match-end 0))))
1453   ;; Trailing spaces.
1454   (when (string-match "[ \t]+$" str)
1455     (setq str (substring str 0 (match-beginning 0))))
1456   str)
1457
1458 (defun gnus-simplify-all-whitespace (str)
1459   "Remove all whitespace from STR."
1460   (while (string-match "[ \t\n]+" str)
1461     (setq str (replace-match "" nil nil str)))
1462   str)
1463
1464 (defsubst gnus-simplify-subject-re (subject)
1465   "Remove \"Re:\" from subject lines."
1466   (if (string-match message-subject-re-regexp subject)
1467       (substring subject (match-end 0))
1468     subject))
1469
1470 (defun gnus-simplify-subject (subject &optional re-only)
1471   "Remove `Re:' and words in parentheses.
1472 If RE-ONLY is non-nil, strip leading `Re:'s only."
1473   (let ((case-fold-search t))           ;Ignore case.
1474     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1475     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1476       (setq subject (substring subject (match-end 0))))
1477     ;; Remove uninteresting prefixes.
1478     (when (and (not re-only)
1479                gnus-simplify-ignored-prefixes
1480                (string-match gnus-simplify-ignored-prefixes subject))
1481       (setq subject (substring subject (match-end 0))))
1482     ;; Remove words in parentheses from end.
1483     (unless re-only
1484       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1485         (setq subject (substring subject 0 (match-beginning 0)))))
1486     ;; Return subject string.
1487     subject))
1488
1489 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1490 ;; all whitespace.
1491 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1492   (goto-char (point-min))
1493   (while (re-search-forward regexp nil t)
1494     (replace-match (or newtext ""))))
1495
1496 (defun gnus-simplify-buffer-fuzzy ()
1497   "Simplify string in the buffer fuzzily.
1498 The string in the accessible portion of the current buffer is simplified.
1499 It is assumed to be a single-line subject.
1500 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1501 matter is removed.  Additional things can be deleted by setting
1502 `gnus-simplify-subject-fuzzy-regexp'."
1503   (let ((case-fold-search t)
1504         (modified-tick))
1505     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1506
1507     (while (not (eq modified-tick (buffer-modified-tick)))
1508       (setq modified-tick (buffer-modified-tick))
1509       (cond
1510        ((listp gnus-simplify-subject-fuzzy-regexp)
1511         (mapcar 'gnus-simplify-buffer-fuzzy-step
1512                 gnus-simplify-subject-fuzzy-regexp))
1513        (gnus-simplify-subject-fuzzy-regexp
1514         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1515       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1516       (gnus-simplify-buffer-fuzzy-step
1517        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1518       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1519
1520     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1521     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1522     (gnus-simplify-buffer-fuzzy-step " $")
1523     (gnus-simplify-buffer-fuzzy-step "^ +")))
1524
1525 (defun gnus-simplify-subject-fuzzy (subject)
1526   "Simplify a subject string fuzzily.
1527 See `gnus-simplify-buffer-fuzzy' for details."
1528   (save-excursion
1529     (gnus-set-work-buffer)
1530     (let ((case-fold-search t))
1531       ;; Remove uninteresting prefixes.
1532       (when (and gnus-simplify-ignored-prefixes
1533                  (string-match gnus-simplify-ignored-prefixes subject))
1534         (setq subject (substring subject (match-end 0))))
1535       (insert subject)
1536       (inline (gnus-simplify-buffer-fuzzy))
1537       (buffer-string))))
1538
1539 (defsubst gnus-simplify-subject-fully (subject)
1540   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1541   (cond
1542    (gnus-simplify-subject-functions
1543     (gnus-map-function gnus-simplify-subject-functions subject))
1544    ((null gnus-summary-gather-subject-limit)
1545     (gnus-simplify-subject-re subject))
1546    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1547     (gnus-simplify-subject-fuzzy subject))
1548    ((numberp gnus-summary-gather-subject-limit)
1549     (gnus-limit-string (gnus-simplify-subject-re subject)
1550                        gnus-summary-gather-subject-limit))
1551    (t
1552     subject)))
1553
1554 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1555   "Check whether two subjects are equal.
1556 If optional argument SIMPLE-FIRST is t, first argument is already
1557 simplified."
1558   (cond
1559    ((null simple-first)
1560     (equal (gnus-simplify-subject-fully s1)
1561            (gnus-simplify-subject-fully s2)))
1562    (t
1563     (equal s1
1564            (gnus-simplify-subject-fully s2)))))
1565
1566 (defun gnus-summary-bubble-group ()
1567   "Increase the score of the current group.
1568 This is a handy function to add to `gnus-summary-exit-hook' to
1569 increase the score of each group you read."
1570   (gnus-group-add-score gnus-newsgroup-name))
1571
1572 \f
1573 ;;;
1574 ;;; Gnus summary mode
1575 ;;;
1576
1577 (put 'gnus-summary-mode 'mode-class 'special)
1578
1579 (defvar gnus-article-commands-menu)
1580
1581 ;; Non-orthogonal keys
1582
1583 (gnus-define-keys gnus-summary-mode-map
1584   " " gnus-summary-next-page
1585   "\177" gnus-summary-prev-page
1586   [delete] gnus-summary-prev-page
1587   [backspace] gnus-summary-prev-page
1588   "\r" gnus-summary-scroll-up
1589   "\M-\r" gnus-summary-scroll-down
1590   "n" gnus-summary-next-unread-article
1591   "p" gnus-summary-prev-unread-article
1592   "N" gnus-summary-next-article
1593   "P" gnus-summary-prev-article
1594   "\M-\C-n" gnus-summary-next-same-subject
1595   "\M-\C-p" gnus-summary-prev-same-subject
1596   "\M-n" gnus-summary-next-unread-subject
1597   "\M-p" gnus-summary-prev-unread-subject
1598   "." gnus-summary-first-unread-article
1599   "," gnus-summary-best-unread-article
1600   "\M-s" gnus-summary-search-article-forward
1601   "\M-r" gnus-summary-search-article-backward
1602   "<" gnus-summary-beginning-of-article
1603   ">" gnus-summary-end-of-article
1604   "j" gnus-summary-goto-article
1605   "^" gnus-summary-refer-parent-article
1606   "\M-^" gnus-summary-refer-article
1607   "u" gnus-summary-tick-article-forward
1608   "!" gnus-summary-tick-article-forward
1609   "U" gnus-summary-tick-article-backward
1610   "d" gnus-summary-mark-as-read-forward
1611   "D" gnus-summary-mark-as-read-backward
1612   "E" gnus-summary-mark-as-expirable
1613   "\M-u" gnus-summary-clear-mark-forward
1614   "\M-U" gnus-summary-clear-mark-backward
1615   "k" gnus-summary-kill-same-subject-and-select
1616   "\C-k" gnus-summary-kill-same-subject
1617   "\M-\C-k" gnus-summary-kill-thread
1618   "\M-\C-l" gnus-summary-lower-thread
1619   "e" gnus-summary-edit-article
1620   "#" gnus-summary-mark-as-processable
1621   "\M-#" gnus-summary-unmark-as-processable
1622   "\M-\C-t" gnus-summary-toggle-threads
1623   "\M-\C-s" gnus-summary-show-thread
1624   "\M-\C-h" gnus-summary-hide-thread
1625   "\M-\C-f" gnus-summary-next-thread
1626   "\M-\C-b" gnus-summary-prev-thread
1627   [(meta down)] gnus-summary-next-thread
1628   [(meta up)] gnus-summary-prev-thread
1629   "\M-\C-u" gnus-summary-up-thread
1630   "\M-\C-d" gnus-summary-down-thread
1631   "&" gnus-summary-execute-command
1632   "c" gnus-summary-catchup-and-exit
1633   "\C-w" gnus-summary-mark-region-as-read
1634   "\C-t" gnus-summary-toggle-truncation
1635   "?" gnus-summary-mark-as-dormant
1636   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1637   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1638   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1639   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1640   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1641   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1642   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1643   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1644   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1645   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1646   "=" gnus-summary-expand-window
1647   "\C-x\C-s" gnus-summary-reselect-current-group
1648   "\M-g" gnus-summary-rescan-group
1649   "w" gnus-summary-stop-page-breaking
1650   "\C-c\C-r" gnus-summary-caesar-message
1651   "f" gnus-summary-followup
1652   "F" gnus-summary-followup-with-original
1653   "C" gnus-summary-cancel-article
1654   "r" gnus-summary-reply
1655   "R" gnus-summary-reply-with-original
1656   "\C-c\C-f" gnus-summary-mail-forward
1657   "o" gnus-summary-save-article
1658   "\C-o" gnus-summary-save-article-mail
1659   "|" gnus-summary-pipe-output
1660   "\M-k" gnus-summary-edit-local-kill
1661   "\M-K" gnus-summary-edit-global-kill
1662   ;; "V" gnus-version
1663   "\C-c\C-d" gnus-summary-describe-group
1664   "q" gnus-summary-exit
1665   "Q" gnus-summary-exit-no-update
1666   "\C-c\C-i" gnus-info-find-node
1667   gnus-mouse-2 gnus-mouse-pick-article
1668   "m" gnus-summary-mail-other-window
1669   "a" gnus-summary-post-news
1670   "i" gnus-summary-news-other-window
1671   "x" gnus-summary-limit-to-unread
1672   "s" gnus-summary-isearch-article
1673   "t" gnus-summary-toggle-header
1674   "g" gnus-summary-show-article
1675   "l" gnus-summary-goto-last-article
1676   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1677   "\C-d" gnus-summary-enter-digest-group
1678   "\M-\C-d" gnus-summary-read-document
1679   "\M-\C-e" gnus-summary-edit-parameters
1680   "\M-\C-a" gnus-summary-customize-parameters
1681   "\C-c\C-b" gnus-bug
1682   "*" gnus-cache-enter-article
1683   "\M-*" gnus-cache-remove-article
1684   "\M-&" gnus-summary-universal-argument
1685   "\C-l" gnus-recenter
1686   "I" gnus-summary-increase-score
1687   "L" gnus-summary-lower-score
1688   "\M-i" gnus-symbolic-argument
1689   "h" gnus-summary-select-article-buffer
1690
1691   "b" gnus-article-view-part
1692   "\M-t" gnus-summary-toggle-display-buttonized
1693
1694   "V" gnus-summary-score-map
1695   "X" gnus-uu-extract-map
1696   "S" gnus-summary-send-map)
1697
1698 ;; Sort of orthogonal keymap
1699 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1700   "t" gnus-summary-tick-article-forward
1701   "!" gnus-summary-tick-article-forward
1702   "d" gnus-summary-mark-as-read-forward
1703   "r" gnus-summary-mark-as-read-forward
1704   "c" gnus-summary-clear-mark-forward
1705   " " gnus-summary-clear-mark-forward
1706   "e" gnus-summary-mark-as-expirable
1707   "x" gnus-summary-mark-as-expirable
1708   "?" gnus-summary-mark-as-dormant
1709   "b" gnus-summary-set-bookmark
1710   "B" gnus-summary-remove-bookmark
1711   "#" gnus-summary-mark-as-processable
1712   "\M-#" gnus-summary-unmark-as-processable
1713   "S" gnus-summary-limit-include-expunged
1714   "C" gnus-summary-catchup
1715   "H" gnus-summary-catchup-to-here
1716   "h" gnus-summary-catchup-from-here
1717   "\C-c" gnus-summary-catchup-all
1718   "k" gnus-summary-kill-same-subject-and-select
1719   "K" gnus-summary-kill-same-subject
1720   "P" gnus-uu-mark-map)
1721
1722 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1723   "c" gnus-summary-clear-above
1724   "u" gnus-summary-tick-above
1725   "m" gnus-summary-mark-above
1726   "k" gnus-summary-kill-below)
1727
1728 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1729   "/" gnus-summary-limit-to-subject
1730   "n" gnus-summary-limit-to-articles
1731   "w" gnus-summary-pop-limit
1732   "s" gnus-summary-limit-to-subject
1733   "a" gnus-summary-limit-to-author
1734   "u" gnus-summary-limit-to-unread
1735   "m" gnus-summary-limit-to-marks
1736   "M" gnus-summary-limit-exclude-marks
1737   "v" gnus-summary-limit-to-score
1738   "*" gnus-summary-limit-include-cached
1739   "D" gnus-summary-limit-include-dormant
1740   "T" gnus-summary-limit-include-thread
1741   "d" gnus-summary-limit-exclude-dormant
1742   "t" gnus-summary-limit-to-age
1743   "." gnus-summary-limit-to-unseen
1744   "x" gnus-summary-limit-to-extra
1745   "p" gnus-summary-limit-to-display-predicate
1746   "E" gnus-summary-limit-include-expunged
1747   "c" gnus-summary-limit-exclude-childless-dormant
1748   "C" gnus-summary-limit-mark-excluded-as-read
1749   "o" gnus-summary-insert-old-articles
1750   "N" gnus-summary-insert-new-articles)
1751
1752 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1753   "n" gnus-summary-next-unread-article
1754   "p" gnus-summary-prev-unread-article
1755   "N" gnus-summary-next-article
1756   "P" gnus-summary-prev-article
1757   "\C-n" gnus-summary-next-same-subject
1758   "\C-p" gnus-summary-prev-same-subject
1759   "\M-n" gnus-summary-next-unread-subject
1760   "\M-p" gnus-summary-prev-unread-subject
1761   "f" gnus-summary-first-unread-article
1762   "b" gnus-summary-best-unread-article
1763   "j" gnus-summary-goto-article
1764   "g" gnus-summary-goto-subject
1765   "l" gnus-summary-goto-last-article
1766   "o" gnus-summary-pop-article)
1767
1768 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1769   "k" gnus-summary-kill-thread
1770   "l" gnus-summary-lower-thread
1771   "i" gnus-summary-raise-thread
1772   "T" gnus-summary-toggle-threads
1773   "t" gnus-summary-rethread-current
1774   "^" gnus-summary-reparent-thread
1775   "s" gnus-summary-show-thread
1776   "S" gnus-summary-show-all-threads
1777   "h" gnus-summary-hide-thread
1778   "H" gnus-summary-hide-all-threads
1779   "n" gnus-summary-next-thread
1780   "p" gnus-summary-prev-thread
1781   "u" gnus-summary-up-thread
1782   "o" gnus-summary-top-thread
1783   "d" gnus-summary-down-thread
1784   "#" gnus-uu-mark-thread
1785   "\M-#" gnus-uu-unmark-thread)
1786
1787 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1788   "g" gnus-summary-prepare
1789   "c" gnus-summary-insert-cached-articles
1790   "d" gnus-summary-insert-dormant-articles)
1791
1792 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1793   "c" gnus-summary-catchup-and-exit
1794   "C" gnus-summary-catchup-all-and-exit
1795   "E" gnus-summary-exit-no-update
1796   "Q" gnus-summary-exit
1797   "Z" gnus-summary-exit
1798   "n" gnus-summary-catchup-and-goto-next-group
1799   "R" gnus-summary-reselect-current-group
1800   "G" gnus-summary-rescan-group
1801   "N" gnus-summary-next-group
1802   "s" gnus-summary-save-newsrc
1803   "P" gnus-summary-prev-group)
1804
1805 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1806   " " gnus-summary-next-page
1807   "n" gnus-summary-next-page
1808   "\177" gnus-summary-prev-page
1809   [delete] gnus-summary-prev-page
1810   "p" gnus-summary-prev-page
1811   "\r" gnus-summary-scroll-up
1812   "\M-\r" gnus-summary-scroll-down
1813   "<" gnus-summary-beginning-of-article
1814   ">" gnus-summary-end-of-article
1815   "b" gnus-summary-beginning-of-article
1816   "e" gnus-summary-end-of-article
1817   "^" gnus-summary-refer-parent-article
1818   "r" gnus-summary-refer-parent-article
1819   "D" gnus-summary-enter-digest-group
1820   "R" gnus-summary-refer-references
1821   "T" gnus-summary-refer-thread
1822   "g" gnus-summary-show-article
1823   "s" gnus-summary-isearch-article
1824   "P" gnus-summary-print-article
1825   "M" gnus-mailing-list-insinuate
1826   "t" gnus-article-babel)
1827
1828 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1829   "b" gnus-article-add-buttons
1830   "B" gnus-article-add-buttons-to-head
1831   "o" gnus-article-treat-overstrike
1832   "e" gnus-article-emphasize
1833   "w" gnus-article-fill-cited-article
1834   "Q" gnus-article-fill-long-lines
1835   "C" gnus-article-capitalize-sentences
1836   "c" gnus-article-remove-cr
1837   "q" gnus-article-de-quoted-unreadable
1838   "6" gnus-article-de-base64-unreadable
1839   "Z" gnus-article-decode-HZ
1840   "h" gnus-article-wash-html
1841   "u" gnus-article-unsplit-urls
1842   "s" gnus-summary-force-verify-and-decrypt
1843   "f" gnus-article-display-x-face
1844   "l" gnus-summary-stop-page-breaking
1845   "r" gnus-summary-caesar-message
1846   "m" gnus-summary-morse-message
1847   "t" gnus-summary-toggle-header
1848   "g" gnus-treat-smiley
1849   "v" gnus-summary-verbose-headers
1850   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1851   "p" gnus-article-verify-x-pgp-sig
1852   "d" gnus-article-treat-dumbquotes)
1853
1854 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1855   ;; mnemonic: deuglif*Y*
1856   "u" gnus-article-outlook-unwrap-lines
1857   "a" gnus-article-outlook-repair-attribution
1858   "c" gnus-article-outlook-rearrange-citation
1859   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1860
1861 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1862   "a" gnus-article-hide
1863   "h" gnus-article-hide-headers
1864   "b" gnus-article-hide-boring-headers
1865   "s" gnus-article-hide-signature
1866   "c" gnus-article-hide-citation
1867   "C" gnus-article-hide-citation-in-followups
1868   "l" gnus-article-hide-list-identifiers
1869   "B" gnus-article-strip-banner
1870   "P" gnus-article-hide-pem
1871   "\C-c" gnus-article-hide-citation-maybe)
1872
1873 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1874   "a" gnus-article-highlight
1875   "h" gnus-article-highlight-headers
1876   "c" gnus-article-highlight-citation
1877   "s" gnus-article-highlight-signature)
1878
1879 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1880   "f" gnus-article-treat-fold-headers
1881   "u" gnus-article-treat-unfold-headers
1882   "n" gnus-article-treat-fold-newsgroups)
1883
1884 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1885   "x" gnus-article-display-x-face
1886   "d" gnus-article-display-face
1887   "s" gnus-treat-smiley
1888   "D" gnus-article-remove-images
1889   "f" gnus-treat-from-picon
1890   "m" gnus-treat-mail-picon
1891   "n" gnus-treat-newsgroups-picon)
1892
1893 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1894   "w" gnus-article-decode-mime-words
1895   "c" gnus-article-decode-charset
1896   "v" gnus-mime-view-all-parts
1897   "b" gnus-article-view-part)
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              ("MIME"
2093               ["Words" gnus-article-decode-mime-words t]
2094               ["Charset" gnus-article-decode-charset t]
2095               ["QP" gnus-article-de-quoted-unreadable t]
2096               ["Base64" gnus-article-de-base64-unreadable t]
2097               ["View MIME buttons" gnus-summary-display-buttonized t]
2098               ["View all" gnus-mime-view-all-parts t]
2099               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2100               ["Encrypt body" gnus-article-encrypt-body
2101                :active (not (gnus-group-read-only-p))
2102                ,@(if (featurep 'xemacs) nil
2103                    '(:help "Encrypt the message body on disk"))]
2104               ["Extract all parts" gnus-summary-save-parts t]
2105               ("Multipart"
2106                ["Repair multipart" gnus-summary-repair-multipart t]
2107                ["Add buttons" gnus-summary-display-buttonized t]
2108                ["Pipe part" gnus-article-pipe-part t]
2109                ["Inline part" gnus-article-inline-part t]
2110                ["Encrypt body" gnus-article-encrypt-body
2111                 :active (not (gnus-group-read-only-p))
2112                ,@(if (featurep 'xemacs) nil
2113                    '(:help "Encrypt the message body on disk"))]
2114                ["View part externally" gnus-article-view-part-externally t]
2115                ["View part with charset" gnus-article-view-part-as-charset t]
2116                ["Copy part" gnus-article-copy-part t]
2117                ["Save part" gnus-article-save-part t]
2118                ["View part" gnus-article-view-part t]))
2119              ("Date"
2120               ["Local" gnus-article-date-local t]
2121               ["ISO8601" gnus-article-date-iso8601 t]
2122               ["UT" gnus-article-date-ut t]
2123               ["Original" gnus-article-date-original t]
2124               ["Lapsed" gnus-article-date-lapsed t]
2125               ["User-defined" gnus-article-date-user t])
2126              ("Display"
2127               ["Remove images" gnus-article-remove-images t]
2128               ["Toggle smiley" gnus-treat-smiley t]
2129               ["Show X-Face" gnus-article-display-x-face t]
2130               ["Show picons in From" gnus-treat-from-picon t]
2131               ["Show picons in mail headers" gnus-treat-mail-picon t]
2132               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2133               ("View as different encoding"
2134                ,@(gnus-summary-menu-split
2135                   (mapcar
2136                    (lambda (cs)
2137                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2138                      ;; forms for menu commands, we should provide intern'ed
2139                      ;; function symbols.
2140                      (let ((command (intern (format "\
2141 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2142                        (fset command
2143                              `(lambda ()
2144                                 (interactive)
2145                                 (let ((gnus-summary-show-article-charset-alist
2146                                        '((1 . ,cs))))
2147                                   (gnus-summary-show-article 1))))
2148                        `[,(symbol-name cs) ,command t]))
2149                    (sort (if (fboundp 'coding-system-list)
2150                              (coding-system-list)
2151                            (mapcar 'car mm-mime-mule-charset-alist))
2152                          'string<)))))
2153              ("Washing"
2154               ("Remove Blanks"
2155                ["Leading" gnus-article-strip-leading-blank-lines t]
2156                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2157                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2158                ["All of the above" gnus-article-strip-blank-lines t]
2159                ["All" gnus-article-strip-all-blank-lines t]
2160                ["Leading space" gnus-article-strip-leading-space t]
2161                ["Trailing space" gnus-article-strip-trailing-space t]
2162                ["Leading space in headers"
2163                 gnus-article-remove-leading-whitespace t])
2164               ["Overstrike" gnus-article-treat-overstrike t]
2165               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2166               ["Emphasis" gnus-article-emphasize t]
2167               ["Word wrap" gnus-article-fill-cited-article t]
2168               ["Fill long lines" gnus-article-fill-long-lines t]
2169               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2170               ["CR" gnus-article-remove-cr t]
2171               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2172               ["Base64" gnus-article-de-base64-unreadable t]
2173               ["Rot 13" gnus-summary-caesar-message
2174                ,@(if (featurep 'xemacs) '(t)
2175                    '(:help "\"Caesar rotate\" article by 13"))]
2176               ["Morse decode" gnus-summary-morse-message t]
2177               ["Unix pipe..." gnus-summary-pipe-message t]
2178               ["Add buttons" gnus-article-add-buttons t]
2179               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2180               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2181               ["Verbose header" gnus-summary-verbose-headers t]
2182               ["Toggle header" gnus-summary-toggle-header t]
2183               ["Unfold headers" gnus-article-treat-unfold-headers t]
2184               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2185               ["Html" gnus-article-wash-html t]
2186               ["URLs" gnus-article-unsplit-urls t]
2187               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2188               ["HZ" gnus-article-decode-HZ t]
2189               ("(Outlook) Deuglify"
2190                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2191                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2192                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2193                ["Full (Outlook) deuglify"
2194                 gnus-article-outlook-deuglify-article t])
2195               )
2196              ("Output"
2197               ["Save in default format" gnus-summary-save-article
2198                ,@(if (featurep 'xemacs) '(t)
2199                    '(:help "Save article using default method"))]
2200               ["Save in file" gnus-summary-save-article-file
2201                ,@(if (featurep 'xemacs) '(t)
2202                    '(:help "Save article in file"))]
2203               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2204               ["Save in MH folder" gnus-summary-save-article-folder t]
2205               ["Save in VM folder" gnus-summary-save-article-vm t]
2206               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2207               ["Save body in file" gnus-summary-save-article-body-file t]
2208               ["Pipe through a filter" gnus-summary-pipe-output t]
2209               ["Add to SOUP packet" gnus-soup-add-article t]
2210               ["Print with Muttprint" gnus-summary-muttprint t]
2211               ["Print" gnus-summary-print-article t])
2212              ("Backend"
2213               ["Respool article..." gnus-summary-respool-article t]
2214               ["Move article..." gnus-summary-move-article
2215                (gnus-check-backend-function
2216                 'request-move-article gnus-newsgroup-name)]
2217               ["Copy article..." gnus-summary-copy-article t]
2218               ["Crosspost article..." gnus-summary-crosspost-article
2219                (gnus-check-backend-function
2220                 'request-replace-article gnus-newsgroup-name)]
2221               ["Import file..." gnus-summary-import-article
2222                (gnus-check-backend-function
2223                 'request-accept-article gnus-newsgroup-name)]
2224               ["Create article..." gnus-summary-create-article
2225                (gnus-check-backend-function
2226                 'request-accept-article gnus-newsgroup-name)]
2227               ["Check if posted" gnus-summary-article-posted-p t]
2228               ["Edit article" gnus-summary-edit-article
2229                (not (gnus-group-read-only-p))]
2230               ["Delete article" gnus-summary-delete-article
2231                (gnus-check-backend-function
2232                 'request-expire-articles gnus-newsgroup-name)]
2233               ["Query respool" gnus-summary-respool-query t]
2234               ["Trace respool" gnus-summary-respool-trace t]
2235               ["Delete expirable articles" gnus-summary-expire-articles-now
2236                (gnus-check-backend-function
2237                 'request-expire-articles gnus-newsgroup-name)])
2238              ("Extract"
2239               ["Uudecode" gnus-uu-decode-uu
2240                ,@(if (featurep 'xemacs) '(t)
2241                    '(:help "Decode uuencoded article(s)"))]
2242               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2243               ["Unshar" gnus-uu-decode-unshar t]
2244               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2245               ["Save" gnus-uu-decode-save t]
2246               ["Binhex" gnus-uu-decode-binhex t]
2247               ["Postscript" gnus-uu-decode-postscript t]
2248               ["All MIME parts" gnus-summary-save-parts t])
2249              ("Cache"
2250               ["Enter article" gnus-cache-enter-article t]
2251               ["Remove article" gnus-cache-remove-article t])
2252              ["Translate" gnus-article-babel t]
2253              ["Select article buffer" gnus-summary-select-article-buffer t]
2254              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2255              ["Isearch article..." gnus-summary-isearch-article t]
2256              ["Beginning of the article" gnus-summary-beginning-of-article t]
2257              ["End of the article" gnus-summary-end-of-article t]
2258              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2259              ["Fetch referenced articles" gnus-summary-refer-references t]
2260              ["Fetch current thread" gnus-summary-refer-thread t]
2261              ["Fetch article with id..." gnus-summary-refer-article t]
2262              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2263              ["Redisplay" gnus-summary-show-article t]
2264              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2265       (easy-menu-define
2266         gnus-summary-article-menu gnus-summary-mode-map ""
2267         (cons "Article" innards))
2268
2269       (if (not (keymapp gnus-summary-article-menu))
2270           (easy-menu-define
2271             gnus-article-commands-menu gnus-article-mode-map ""
2272             (cons "Commands" innards))
2273         ;; in Emacs, don't share menu.
2274         (setq gnus-article-commands-menu
2275               (copy-keymap gnus-summary-article-menu))
2276         (define-key gnus-article-mode-map [menu-bar commands]
2277           (cons "Commands" gnus-article-commands-menu))))
2278
2279     (easy-menu-define
2280       gnus-summary-thread-menu gnus-summary-mode-map ""
2281       '("Threads"
2282         ["Find all messages in thread" gnus-summary-refer-thread t]
2283         ["Toggle threading" gnus-summary-toggle-threads t]
2284         ["Hide threads" gnus-summary-hide-all-threads t]
2285         ["Show threads" gnus-summary-show-all-threads t]
2286         ["Hide thread" gnus-summary-hide-thread t]
2287         ["Show thread" gnus-summary-show-thread t]
2288         ["Go to next thread" gnus-summary-next-thread t]
2289         ["Go to previous thread" gnus-summary-prev-thread t]
2290         ["Go down thread" gnus-summary-down-thread t]
2291         ["Go up thread" gnus-summary-up-thread t]
2292         ["Top of thread" gnus-summary-top-thread t]
2293         ["Mark thread as read" gnus-summary-kill-thread t]
2294         ["Lower thread score" gnus-summary-lower-thread t]
2295         ["Raise thread score" gnus-summary-raise-thread t]
2296         ["Rethread current" gnus-summary-rethread-current t]))
2297
2298     (easy-menu-define
2299       gnus-summary-post-menu gnus-summary-mode-map ""
2300       `("Post"
2301         ["Send a message (mail or news)" gnus-summary-post-news
2302          ,@(if (featurep 'xemacs) '(t)
2303              '(:help "Post an article"))]
2304         ["Followup" gnus-summary-followup
2305          ,@(if (featurep 'xemacs) '(t)
2306              '(:help "Post followup to this article"))]
2307         ["Followup and yank" gnus-summary-followup-with-original
2308          ,@(if (featurep 'xemacs) '(t)
2309              '(:help "Post followup to this article, quoting its contents"))]
2310         ["Supersede article" gnus-summary-supersede-article t]
2311         ["Cancel article" gnus-summary-cancel-article
2312          ,@(if (featurep 'xemacs) '(t)
2313              '(:help "Cancel an article you posted"))]
2314         ["Reply" gnus-summary-reply t]
2315         ["Reply and yank" gnus-summary-reply-with-original t]
2316         ["Wide reply" gnus-summary-wide-reply t]
2317         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2318          ,@(if (featurep 'xemacs) '(t)
2319              '(:help "Mail a reply, quoting this article"))]
2320         ["Very wide reply" gnus-summary-very-wide-reply t]
2321         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2322          ,@(if (featurep 'xemacs) '(t)
2323              '(:help "Mail a very wide reply, quoting this article"))]
2324         ["Mail forward" gnus-summary-mail-forward t]
2325         ["Post forward" gnus-summary-post-forward t]
2326         ["Digest and mail" gnus-uu-digest-mail-forward t]
2327         ["Digest and post" gnus-uu-digest-post-forward t]
2328         ["Resend message" gnus-summary-resend-message t]
2329         ["Resend message edit" gnus-summary-resend-message-edit t]
2330         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2331         ["Send a mail" gnus-summary-mail-other-window t]
2332         ["Create a local message" gnus-summary-news-other-window t]
2333         ["Uuencode and post" gnus-uu-post-news
2334          ,@(if (featurep 'xemacs) '(t)
2335              '(:help "Post a uuencoded article"))]
2336         ["Followup via news" gnus-summary-followup-to-mail t]
2337         ["Followup via news and yank"
2338          gnus-summary-followup-to-mail-with-original t]
2339         ;;("Draft"
2340         ;;["Send" gnus-summary-send-draft t]
2341         ;;["Send bounced" gnus-resend-bounced-mail t])
2342         ))
2343
2344     (cond
2345      ((not (keymapp gnus-summary-post-menu))
2346       (setq gnus-article-post-menu gnus-summary-post-menu))
2347      ((not gnus-article-post-menu)
2348       ;; Don't share post menu.
2349       (setq gnus-article-post-menu
2350             (copy-keymap gnus-summary-post-menu))))
2351     (define-key gnus-article-mode-map [menu-bar post]
2352       (cons "Post" gnus-article-post-menu))
2353
2354     (easy-menu-define
2355       gnus-summary-misc-menu gnus-summary-mode-map ""
2356       `("Gnus"
2357         ("Mark Read"
2358          ["Mark as read" gnus-summary-mark-as-read-forward t]
2359          ["Mark same subject and select"
2360           gnus-summary-kill-same-subject-and-select t]
2361          ["Mark same subject" gnus-summary-kill-same-subject t]
2362          ["Catchup" gnus-summary-catchup
2363           ,@(if (featurep 'xemacs) '(t)
2364               '(:help "Mark unread articles in this group as read"))]
2365          ["Catchup all" gnus-summary-catchup-all t]
2366          ["Catchup to here" gnus-summary-catchup-to-here t]
2367          ["Catchup from here" gnus-summary-catchup-from-here t]
2368          ["Catchup region" gnus-summary-mark-region-as-read 
2369           (gnus-mark-active-p)]
2370          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2371         ("Mark Various"
2372          ["Tick" gnus-summary-tick-article-forward t]
2373          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2374          ["Remove marks" gnus-summary-clear-mark-forward t]
2375          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2376          ["Set bookmark" gnus-summary-set-bookmark t]
2377          ["Remove bookmark" gnus-summary-remove-bookmark t])
2378         ("Limit to"
2379          ["Marks..." gnus-summary-limit-to-marks t]
2380          ["Subject..." gnus-summary-limit-to-subject t]
2381          ["Author..." gnus-summary-limit-to-author t]
2382          ["Age..." gnus-summary-limit-to-age t]
2383          ["Extra..." gnus-summary-limit-to-extra t]
2384          ["Score..." gnus-summary-limit-to-score t]
2385          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2386          ["Unread" gnus-summary-limit-to-unread t]
2387          ["Unseen" gnus-summary-limit-to-unseen t]
2388          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2389          ["Next articles" gnus-summary-limit-to-articles t]
2390          ["Pop limit" gnus-summary-pop-limit t]
2391          ["Show dormant" gnus-summary-limit-include-dormant t]
2392          ["Hide childless dormant"
2393           gnus-summary-limit-exclude-childless-dormant t]
2394          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2395          ["Hide marked" gnus-summary-limit-exclude-marks t]
2396          ["Show expunged" gnus-summary-limit-include-expunged t])
2397         ("Process Mark"
2398          ["Set mark" gnus-summary-mark-as-processable t]
2399          ["Remove mark" gnus-summary-unmark-as-processable t]
2400          ["Remove all marks" gnus-summary-unmark-all-processable t]
2401          ["Mark above" gnus-uu-mark-over t]
2402          ["Mark series" gnus-uu-mark-series t]
2403          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2404          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2405          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2406          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2407          ["Mark all" gnus-uu-mark-all t]
2408          ["Mark buffer" gnus-uu-mark-buffer t]
2409          ["Mark sparse" gnus-uu-mark-sparse t]
2410          ["Mark thread" gnus-uu-mark-thread t]
2411          ["Unmark thread" gnus-uu-unmark-thread t]
2412          ("Process Mark Sets"
2413           ["Kill" gnus-summary-kill-process-mark t]
2414           ["Yank" gnus-summary-yank-process-mark
2415            gnus-newsgroup-process-stack]
2416           ["Save" gnus-summary-save-process-mark t]
2417           ["Run command on marked..." gnus-summary-universal-argument t]))
2418         ("Scroll article"
2419          ["Page forward" gnus-summary-next-page
2420           ,@(if (featurep 'xemacs) '(t)
2421               '(:help "Show next page of article"))]
2422          ["Page backward" gnus-summary-prev-page
2423           ,@(if (featurep 'xemacs) '(t)
2424               '(:help "Show previous page of article"))]
2425          ["Line forward" gnus-summary-scroll-up t])
2426         ("Move"
2427          ["Next unread article" gnus-summary-next-unread-article t]
2428          ["Previous unread article" gnus-summary-prev-unread-article t]
2429          ["Next article" gnus-summary-next-article t]
2430          ["Previous article" gnus-summary-prev-article t]
2431          ["Next unread subject" gnus-summary-next-unread-subject t]
2432          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2433          ["Next article same subject" gnus-summary-next-same-subject t]
2434          ["Previous article same subject" gnus-summary-prev-same-subject t]
2435          ["First unread article" gnus-summary-first-unread-article t]
2436          ["Best unread article" gnus-summary-best-unread-article t]
2437          ["Go to subject number..." gnus-summary-goto-subject t]
2438          ["Go to article number..." gnus-summary-goto-article t]
2439          ["Go to the last article" gnus-summary-goto-last-article t]
2440          ["Pop article off history" gnus-summary-pop-article t])
2441         ("Sort"
2442          ["Sort by number" gnus-summary-sort-by-number t]
2443          ["Sort by author" gnus-summary-sort-by-author t]
2444          ["Sort by subject" gnus-summary-sort-by-subject t]
2445          ["Sort by date" gnus-summary-sort-by-date t]
2446          ["Sort by score" gnus-summary-sort-by-score t]
2447          ["Sort by lines" gnus-summary-sort-by-lines t]
2448          ["Sort by characters" gnus-summary-sort-by-chars t]
2449          ["Randomize" gnus-summary-sort-by-random t]
2450          ["Original sort" gnus-summary-sort-by-original t])
2451         ("Help"
2452          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2453          ["Describe group" gnus-summary-describe-group t]
2454          ["Fetch charter" gnus-group-fetch-charter
2455           ,@(if (featurep 'xemacs) nil
2456               '(:help "Display the charter of the current group"))]
2457          ["Fetch control message" gnus-group-fetch-control
2458           ,@(if (featurep 'xemacs) nil
2459               '(:help "Display the archived control message for the current group"))]
2460          ["Read manual" gnus-info-find-node t])
2461         ("Modes"
2462          ["Pick and read" gnus-pick-mode t]
2463          ["Binary" gnus-binary-mode t])
2464         ("Regeneration"
2465          ["Regenerate" gnus-summary-prepare t]
2466          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2467          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2468          ["Toggle threading" gnus-summary-toggle-threads t])
2469         ["See old articles" gnus-summary-insert-old-articles t]
2470         ["See new articles" gnus-summary-insert-new-articles t]
2471         ["Filter articles..." gnus-summary-execute-command t]
2472         ["Run command on articles..." gnus-summary-universal-argument t]
2473         ["Search articles forward..." gnus-summary-search-article-forward t]
2474         ["Search articles backward..." gnus-summary-search-article-backward t]
2475         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2476         ["Expand window" gnus-summary-expand-window t]
2477         ["Expire expirable articles" gnus-summary-expire-articles
2478          (gnus-check-backend-function
2479           'request-expire-articles gnus-newsgroup-name)]
2480         ["Edit local kill file" gnus-summary-edit-local-kill t]
2481         ["Edit main kill file" gnus-summary-edit-global-kill t]
2482         ["Edit group parameters" gnus-summary-edit-parameters t]
2483         ["Customize group parameters" gnus-summary-customize-parameters t]
2484         ["Send a bug report" gnus-bug t]
2485         ("Exit"
2486          ["Catchup and exit" gnus-summary-catchup-and-exit
2487           ,@(if (featurep 'xemacs) '(t)
2488               '(:help "Mark unread articles in this group as read, then exit"))]
2489          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2490          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2491          ["Exit group" gnus-summary-exit
2492           ,@(if (featurep 'xemacs) '(t)
2493               '(:help "Exit current group, return to group selection mode"))]
2494          ["Exit group without updating" gnus-summary-exit-no-update t]
2495          ["Exit and goto next group" gnus-summary-next-group t]
2496          ["Exit and goto prev group" gnus-summary-prev-group t]
2497          ["Reselect group" gnus-summary-reselect-current-group t]
2498          ["Rescan group" gnus-summary-rescan-group t]
2499          ["Update dribble" gnus-summary-save-newsrc t])))
2500
2501     (gnus-run-hooks 'gnus-summary-menu-hook)))
2502
2503 (defvar gnus-summary-tool-bar-map nil)
2504
2505 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2506 (defun gnus-summary-make-tool-bar ()
2507   (if (and (fboundp 'tool-bar-add-item-from-menu)
2508            (default-value 'tool-bar-mode)
2509            (not gnus-summary-tool-bar-map))
2510       (setq gnus-summary-tool-bar-map
2511             (let ((tool-bar-map (make-sparse-keymap))
2512                   (load-path (mm-image-load-path)))
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-post-news "post" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup "followup" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply "reply" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2543               tool-bar-map)))
2544   (if gnus-summary-tool-bar-map
2545       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2546
2547 (defun gnus-score-set-default (var value)
2548   "A version of set that updates the GNU Emacs menu-bar."
2549   (set var value)
2550   ;; It is the message that forces the active status to be updated.
2551   (message ""))
2552
2553 (defun gnus-make-score-map (type)
2554   "Make a summary score map of type TYPE."
2555   (if t
2556       nil
2557     (let ((headers '(("author" "from" string)
2558                      ("subject" "subject" string)
2559                      ("article body" "body" string)
2560                      ("article head" "head" string)
2561                      ("xref" "xref" string)
2562                      ("extra header" "extra" string)
2563                      ("lines" "lines" number)
2564                      ("followups to author" "followup" string)))
2565           (types '((number ("less than" <)
2566                            ("greater than" >)
2567                            ("equal" =))
2568                    (string ("substring" s)
2569                            ("exact string" e)
2570                            ("fuzzy string" f)
2571                            ("regexp" r))))
2572           (perms '(("temporary" (current-time-string))
2573                    ("permanent" nil)
2574                    ("immediate" now)))
2575           header)
2576       (list
2577        (apply
2578         'nconc
2579         (list
2580          (if (eq type 'lower)
2581              "Lower score"
2582            "Increase score"))
2583         (let (outh)
2584           (while headers
2585             (setq header (car headers))
2586             (setq outh
2587                   (cons
2588                    (apply
2589                     'nconc
2590                     (list (car header))
2591                     (let ((ts (cdr (assoc (nth 2 header) types)))
2592                           outt)
2593                       (while ts
2594                         (setq outt
2595                               (cons
2596                                (apply
2597                                 'nconc
2598                                 (list (caar ts))
2599                                 (let ((ps perms)
2600                                       outp)
2601                                   (while ps
2602                                     (setq outp
2603                                           (cons
2604                                            (vector
2605                                             (caar ps)
2606                                             (list
2607                                              'gnus-summary-score-entry
2608                                              (nth 1 header)
2609                                              (if (or (string= (nth 1 header)
2610                                                               "head")
2611                                                      (string= (nth 1 header)
2612                                                               "body"))
2613                                                  ""
2614                                                (list 'gnus-summary-header
2615                                                      (nth 1 header)))
2616                                              (list 'quote (nth 1 (car ts)))
2617                                              (list 'gnus-score-delta-default
2618                                                    nil)
2619                                              (nth 1 (car ps))
2620                                              t)
2621                                             t)
2622                                            outp))
2623                                     (setq ps (cdr ps)))
2624                                   (list (nreverse outp))))
2625                                outt))
2626                         (setq ts (cdr ts)))
2627                       (list (nreverse outt))))
2628                    outh))
2629             (setq headers (cdr headers)))
2630           (list (nreverse outh))))))))
2631
2632 \f
2633
2634 (defun gnus-summary-mode (&optional group)
2635   "Major mode for reading articles.
2636
2637 All normal editing commands are switched off.
2638 \\<gnus-summary-mode-map>
2639 Each line in this buffer represents one article.  To read an
2640 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2641 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2642 respectively.
2643
2644 You can also post articles and send mail from this buffer.  To
2645 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2646 of an article, type `\\[gnus-summary-reply]'.
2647
2648 There are approx. one gazillion commands you can execute in this
2649 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2650
2651 The following commands are available:
2652
2653 \\{gnus-summary-mode-map}"
2654   (interactive)
2655   (kill-all-local-variables)
2656   (when (gnus-visual-p 'summary-menu 'menu)
2657     (gnus-summary-make-menu-bar)
2658     (gnus-summary-make-tool-bar))
2659   (gnus-summary-make-local-variables)
2660   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2661     (gnus-summary-make-local-variables))
2662   (gnus-make-thread-indent-array)
2663   (gnus-simplify-mode-line)
2664   (setq major-mode 'gnus-summary-mode)
2665   (setq mode-name "Summary")
2666   (make-local-variable 'minor-mode-alist)
2667   (use-local-map gnus-summary-mode-map)
2668   (buffer-disable-undo)
2669   (setq buffer-read-only t)             ;Disable modification
2670   (setq truncate-lines t)
2671   (setq selective-display t)
2672   (setq selective-display-ellipses t)   ;Display `...'
2673   (gnus-summary-set-display-table)
2674   (gnus-set-default-directory)
2675   (setq gnus-newsgroup-name group)
2676   (make-local-variable 'gnus-summary-line-format)
2677   (make-local-variable 'gnus-summary-line-format-spec)
2678   (make-local-variable 'gnus-summary-dummy-line-format)
2679   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2680   (make-local-variable 'gnus-summary-mark-positions)
2681   (make-local-hook 'pre-command-hook)
2682   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2683   (gnus-run-hooks 'gnus-summary-mode-hook)
2684   (turn-on-gnus-mailing-list-mode)
2685   (mm-enable-multibyte)
2686   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2687   (gnus-update-summary-mark-positions))
2688
2689 (defun gnus-summary-make-local-variables ()
2690   "Make all the local summary buffer variables."
2691   (let (global)
2692     (dolist (local gnus-summary-local-variables)
2693       (if (consp local)
2694           (progn
2695             (if (eq (cdr local) 'global)
2696                 ;; Copy the global value of the variable.
2697                 (setq global (symbol-value (car local)))
2698               ;; Use the value from the list.
2699               (setq global (eval (cdr local))))
2700             (set (make-local-variable (car local)) global))
2701         ;; Simple nil-valued local variable.
2702         (set (make-local-variable local) nil)))))
2703
2704 (defun gnus-summary-clear-local-variables ()
2705   (let ((locals gnus-summary-local-variables))
2706     (while locals
2707       (if (consp (car locals))
2708           (and (vectorp (caar locals))
2709                (set (caar locals) nil))
2710         (and (vectorp (car locals))
2711              (set (car locals) nil)))
2712       (setq locals (cdr locals)))))
2713
2714 ;; Summary data functions.
2715
2716 (defmacro gnus-data-number (data)
2717   `(car ,data))
2718
2719 (defmacro gnus-data-set-number (data number)
2720   `(setcar ,data ,number))
2721
2722 (defmacro gnus-data-mark (data)
2723   `(nth 1 ,data))
2724
2725 (defmacro gnus-data-set-mark (data mark)
2726   `(setcar (nthcdr 1 ,data) ,mark))
2727
2728 (defmacro gnus-data-pos (data)
2729   `(nth 2 ,data))
2730
2731 (defmacro gnus-data-set-pos (data pos)
2732   `(setcar (nthcdr 2 ,data) ,pos))
2733
2734 (defmacro gnus-data-header (data)
2735   `(nth 3 ,data))
2736
2737 (defmacro gnus-data-set-header (data header)
2738   `(setf (nth 3 ,data) ,header))
2739
2740 (defmacro gnus-data-level (data)
2741   `(nth 4 ,data))
2742
2743 (defmacro gnus-data-unread-p (data)
2744   `(= (nth 1 ,data) gnus-unread-mark))
2745
2746 (defmacro gnus-data-read-p (data)
2747   `(/= (nth 1 ,data) gnus-unread-mark))
2748
2749 (defmacro gnus-data-pseudo-p (data)
2750   `(consp (nth 3 ,data)))
2751
2752 (defmacro gnus-data-find (number)
2753   `(assq ,number gnus-newsgroup-data))
2754
2755 (defmacro gnus-data-find-list (number &optional data)
2756   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2757      (memq (assq ,number bdata)
2758            bdata)))
2759
2760 (defmacro gnus-data-make (number mark pos header level)
2761   `(list ,number ,mark ,pos ,header ,level))
2762
2763 (defun gnus-data-enter (after-article number mark pos header level offset)
2764   (let ((data (gnus-data-find-list after-article)))
2765     (unless data
2766       (error "No such article: %d" after-article))
2767     (setcdr data (cons (gnus-data-make number mark pos header level)
2768                        (cdr data)))
2769     (setq gnus-newsgroup-data-reverse nil)
2770     (gnus-data-update-list (cddr data) offset)))
2771
2772 (defun gnus-data-enter-list (after-article list &optional offset)
2773   (when list
2774     (let ((data (and after-article (gnus-data-find-list after-article)))
2775           (ilist list))
2776       (if (not (or data
2777                    after-article))
2778           (let ((odata gnus-newsgroup-data))
2779             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2780             (when offset
2781               (gnus-data-update-list odata offset)))
2782       ;; Find the last element in the list to be spliced into the main
2783         ;; list.
2784         (while (cdr list)
2785           (setq list (cdr list)))
2786         (if (not data)
2787             (progn
2788               (setcdr list gnus-newsgroup-data)
2789               (setq gnus-newsgroup-data ilist)
2790               (when offset
2791                 (gnus-data-update-list (cdr list) offset)))
2792           (setcdr list (cdr data))
2793           (setcdr data ilist)
2794           (when offset
2795             (gnus-data-update-list (cdr list) offset))))
2796       (setq gnus-newsgroup-data-reverse nil))))
2797
2798 (defun gnus-data-remove (article &optional offset)
2799   (let ((data gnus-newsgroup-data))
2800     (if (= (gnus-data-number (car data)) article)
2801         (progn
2802           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2803                 gnus-newsgroup-data-reverse nil)
2804           (when offset
2805             (gnus-data-update-list gnus-newsgroup-data offset)))
2806       (while (cdr data)
2807         (when (= (gnus-data-number (cadr data)) article)
2808           (setcdr data (cddr data))
2809           (when offset
2810             (gnus-data-update-list (cdr data) offset))
2811           (setq data nil
2812                 gnus-newsgroup-data-reverse nil))
2813         (setq data (cdr data))))))
2814
2815 (defmacro gnus-data-list (backward)
2816   `(if ,backward
2817        (or gnus-newsgroup-data-reverse
2818            (setq gnus-newsgroup-data-reverse
2819                  (reverse gnus-newsgroup-data)))
2820      gnus-newsgroup-data))
2821
2822 (defun gnus-data-update-list (data offset)
2823   "Add OFFSET to the POS of all data entries in DATA."
2824   (setq gnus-newsgroup-data-reverse nil)
2825   (while data
2826     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2827     (setq data (cdr data))))
2828
2829 (defun gnus-summary-article-pseudo-p (article)
2830   "Say whether this article is a pseudo article or not."
2831   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2832
2833 (defmacro gnus-summary-article-sparse-p (article)
2834   "Say whether this article is a sparse article or not."
2835   `(memq ,article gnus-newsgroup-sparse))
2836
2837 (defmacro gnus-summary-article-ancient-p (article)
2838   "Say whether this article is a sparse article or not."
2839   `(memq ,article gnus-newsgroup-ancient))
2840
2841 (defun gnus-article-parent-p (number)
2842   "Say whether this article is a parent or not."
2843   (let ((data (gnus-data-find-list number)))
2844     (and (cdr data)              ; There has to be an article after...
2845          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2846             (gnus-data-level (nth 1 data))))))
2847
2848 (defun gnus-article-children (number)
2849   "Return a list of all children to NUMBER."
2850   (let* ((data (gnus-data-find-list number))
2851          (level (gnus-data-level (car data)))
2852          children)
2853     (setq data (cdr data))
2854     (while (and data
2855                 (= (gnus-data-level (car data)) (1+ level)))
2856       (push (gnus-data-number (car data)) children)
2857       (setq data (cdr data)))
2858     children))
2859
2860 (defmacro gnus-summary-skip-intangible ()
2861   "If the current article is intangible, then jump to a different article."
2862   '(let ((to (get-text-property (point) 'gnus-intangible)))
2863      (and to (gnus-summary-goto-subject to))))
2864
2865 (defmacro gnus-summary-article-intangible-p ()
2866   "Say whether this article is intangible or not."
2867   '(get-text-property (point) 'gnus-intangible))
2868
2869 (defun gnus-article-read-p (article)
2870   "Say whether ARTICLE is read or not."
2871   (not (or (memq article gnus-newsgroup-marked)
2872            (memq article gnus-newsgroup-spam-marked)
2873            (memq article gnus-newsgroup-unreads)
2874            (memq article gnus-newsgroup-unselected)
2875            (memq article gnus-newsgroup-dormant))))
2876
2877 ;; Some summary mode macros.
2878
2879 (defmacro gnus-summary-article-number ()
2880   "The article number of the article on the current line.
2881 If there isn't an article number here, then we return the current
2882 article number."
2883   '(progn
2884      (gnus-summary-skip-intangible)
2885      (or (get-text-property (point) 'gnus-number)
2886          (gnus-summary-last-subject))))
2887
2888 (defmacro gnus-summary-article-header (&optional number)
2889   "Return the header of article NUMBER."
2890   `(gnus-data-header (gnus-data-find
2891                       ,(or number '(gnus-summary-article-number)))))
2892
2893 (defmacro gnus-summary-thread-level (&optional number)
2894   "Return the level of thread that starts with article NUMBER."
2895   `(if (and (eq gnus-summary-make-false-root 'dummy)
2896             (get-text-property (point) 'gnus-intangible))
2897        0
2898      (gnus-data-level (gnus-data-find
2899                        ,(or number '(gnus-summary-article-number))))))
2900
2901 (defmacro gnus-summary-article-mark (&optional number)
2902   "Return the mark of article NUMBER."
2903   `(gnus-data-mark (gnus-data-find
2904                     ,(or number '(gnus-summary-article-number)))))
2905
2906 (defmacro gnus-summary-article-pos (&optional number)
2907   "Return the position of the line of article NUMBER."
2908   `(gnus-data-pos (gnus-data-find
2909                    ,(or number '(gnus-summary-article-number)))))
2910
2911 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2912 (defmacro gnus-summary-article-subject (&optional number)
2913   "Return current subject string or nil if nothing."
2914   `(let ((headers
2915           ,(if number
2916                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2917              '(gnus-data-header (assq (gnus-summary-article-number)
2918                                       gnus-newsgroup-data)))))
2919      (and headers
2920           (vectorp headers)
2921           (mail-header-subject headers))))
2922
2923 (defmacro gnus-summary-article-score (&optional number)
2924   "Return current article score."
2925   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2926                   gnus-newsgroup-scored))
2927        gnus-summary-default-score 0))
2928
2929 (defun gnus-summary-article-children (&optional number)
2930   "Return a list of article numbers that are children of article NUMBER."
2931   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2932          (level (gnus-data-level (car data)))
2933          l children)
2934     (while (and (setq data (cdr data))
2935                 (> (setq l (gnus-data-level (car data))) level))
2936       (and (= (1+ level) l)
2937            (push (gnus-data-number (car data))
2938                  children)))
2939     (nreverse children)))
2940
2941 (defun gnus-summary-article-parent (&optional number)
2942   "Return the article number of the parent of article NUMBER."
2943   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2944                                     (gnus-data-list t)))
2945          (level (gnus-data-level (car data))))
2946     (if (zerop level)
2947         ()                              ; This is a root.
2948       ;; We search until we find an article with a level less than
2949       ;; this one.  That function has to be the parent.
2950       (while (and (setq data (cdr data))
2951                   (not (< (gnus-data-level (car data)) level))))
2952       (and data (gnus-data-number (car data))))))
2953
2954 (defun gnus-unread-mark-p (mark)
2955   "Say whether MARK is the unread mark."
2956   (= mark gnus-unread-mark))
2957
2958 (defun gnus-read-mark-p (mark)
2959   "Say whether MARK is one of the marks that mark as read.
2960 This is all marks except unread, ticked, dormant, and expirable."
2961   (not (or (= mark gnus-unread-mark)
2962            (= mark gnus-ticked-mark)
2963            (= mark gnus-spam-mark)
2964            (= mark gnus-dormant-mark)
2965            (= mark gnus-expirable-mark))))
2966
2967 (defmacro gnus-article-mark (number)
2968   "Return the MARK of article NUMBER.
2969 This macro should only be used when computing the mark the \"first\"
2970 time; i.e., when generating the summary lines.  After that,
2971 `gnus-summary-article-mark' should be used to examine the
2972 marks of articles."
2973   `(cond
2974     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2975     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2976     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2977     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2978     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2979     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2980     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2981     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2982            gnus-ancient-mark))))
2983
2984 ;; Saving hidden threads.
2985
2986 (defmacro gnus-save-hidden-threads (&rest forms)
2987   "Save hidden threads, eval FORMS, and restore the hidden threads."
2988   (let ((config (make-symbol "config")))
2989     `(let ((,config (gnus-hidden-threads-configuration)))
2990        (unwind-protect
2991            (save-excursion
2992              ,@forms)
2993          (gnus-restore-hidden-threads-configuration ,config)))))
2994 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2995 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2996
2997 (defun gnus-data-compute-positions ()
2998   "Compute the positions of all articles."
2999   (setq gnus-newsgroup-data-reverse nil)
3000   (let ((data gnus-newsgroup-data))
3001     (save-excursion
3002       (gnus-save-hidden-threads
3003         (gnus-summary-show-all-threads)
3004         (goto-char (point-min))
3005         (while data
3006           (while (get-text-property (point) 'gnus-intangible)
3007             (forward-line 1))
3008           (gnus-data-set-pos (car data) (+ (point) 3))
3009           (setq data (cdr data))
3010           (forward-line 1))))))
3011
3012 (defun gnus-hidden-threads-configuration ()
3013   "Return the current hidden threads configuration."
3014   (save-excursion
3015     (let (config)
3016       (goto-char (point-min))
3017       (while (search-forward "\r" nil t)
3018         (push (1- (point)) config))
3019       config)))
3020
3021 (defun gnus-restore-hidden-threads-configuration (config)
3022   "Restore hidden threads configuration from CONFIG."
3023   (save-excursion
3024     (let (point buffer-read-only)
3025       (while (setq point (pop config))
3026         (when (and (< point (point-max))
3027                    (goto-char point)
3028                    (eq (char-after) ?\n))
3029           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3030
3031 ;; Various summary mode internalish functions.
3032
3033 (defun gnus-mouse-pick-article (e)
3034   (interactive "e")
3035   (mouse-set-point e)
3036   (gnus-summary-next-page nil t))
3037
3038 (defun gnus-summary-set-display-table ()
3039   "Change the display table.
3040 Odd characters have a tendency to mess
3041 up nicely formatted displays - we make all possible glyphs
3042 display only a single character."
3043
3044   ;; We start from the standard display table, if any.
3045   (let ((table (or (copy-sequence standard-display-table)
3046                    (make-display-table)))
3047         (i 32))
3048     ;; Nix out all the control chars...
3049     (while (>= (setq i (1- i)) 0)
3050       (aset table i [??]))
3051    ;; ... but not newline and cr, of course.  (cr is necessary for the
3052     ;; selective display).
3053     (aset table ?\n nil)
3054     (aset table ?\r nil)
3055     ;; We keep TAB as well.
3056     (aset table ?\t nil)
3057     ;; We nix out any glyphs over 126 that are not set already.
3058     (let ((i 256))
3059       (while (>= (setq i (1- i)) 127)
3060         ;; Only modify if the entry is nil.
3061         (unless (aref table i)
3062           (aset table i [??]))))
3063     (setq buffer-display-table table)))
3064
3065 (defun gnus-summary-set-article-display-arrow (pos)
3066   "Update the overlay arrow to point to line at position POS."
3067   (when (and gnus-summary-display-arrow
3068              (boundp 'overlay-arrow-position)
3069              (boundp 'overlay-arrow-string))
3070     (save-excursion
3071       (goto-char pos)
3072       (beginning-of-line)
3073       (unless overlay-arrow-position
3074         (setq overlay-arrow-position (make-marker)))
3075       (setq overlay-arrow-string "=>"
3076             overlay-arrow-position (set-marker overlay-arrow-position
3077                                                (point)
3078                                                (current-buffer))))))
3079
3080 (defun gnus-summary-setup-buffer (group)
3081   "Initialize summary buffer."
3082   (let ((buffer (gnus-summary-buffer-name group))
3083         (dead-name (concat "*Dead Summary "
3084                            (gnus-group-decoded-name group) "*")))
3085     ;; If a dead summary buffer exists, we kill it.
3086     (when (gnus-buffer-live-p dead-name)
3087       (gnus-kill-buffer dead-name))
3088     (if (get-buffer buffer)
3089         (progn
3090           (set-buffer buffer)
3091           (setq gnus-summary-buffer (current-buffer))
3092           (not gnus-newsgroup-prepared))
3093       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3094       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3095       (gnus-summary-mode group)
3096       (when gnus-carpal
3097         (gnus-carpal-setup-buffer 'summary))
3098       (unless gnus-single-article-buffer
3099         (make-local-variable 'gnus-article-buffer)
3100         (make-local-variable 'gnus-article-current)
3101         (make-local-variable 'gnus-original-article-buffer))
3102       (setq gnus-newsgroup-name group)
3103       ;; Set any local variables in the group parameters.
3104       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3105       t)))
3106
3107 (defun gnus-set-global-variables ()
3108   "Set the global equivalents of the buffer-local variables.
3109 They are set to the latest values they had.  These reflect the summary
3110 buffer that was in action when the last article was fetched."
3111   (when (eq major-mode 'gnus-summary-mode)
3112     (setq gnus-summary-buffer (current-buffer))
3113     (let ((name gnus-newsgroup-name)
3114           (marked gnus-newsgroup-marked)
3115           (spam gnus-newsgroup-spam-marked)
3116           (unread gnus-newsgroup-unreads)
3117           (headers gnus-current-headers)
3118           (data gnus-newsgroup-data)
3119           (summary gnus-summary-buffer)
3120           (article-buffer gnus-article-buffer)
3121           (original gnus-original-article-buffer)
3122           (gac gnus-article-current)
3123           (reffed gnus-reffed-article-number)
3124           (score-file gnus-current-score-file)
3125           (default-charset gnus-newsgroup-charset)
3126           vlist)
3127       (let ((locals gnus-newsgroup-variables))
3128         (while locals
3129           (if (consp (car locals))
3130               (push (eval (caar locals)) vlist)
3131             (push (eval (car locals)) vlist))
3132           (setq locals (cdr locals)))
3133         (setq vlist (nreverse vlist)))
3134       (save-excursion
3135         (set-buffer gnus-group-buffer)
3136         (setq gnus-newsgroup-name name
3137               gnus-newsgroup-marked marked
3138               gnus-newsgroup-spam-marked spam
3139               gnus-newsgroup-unreads unread
3140               gnus-current-headers headers
3141               gnus-newsgroup-data data
3142               gnus-article-current gac
3143               gnus-summary-buffer summary
3144               gnus-article-buffer article-buffer
3145               gnus-original-article-buffer original
3146               gnus-reffed-article-number reffed
3147               gnus-current-score-file score-file
3148               gnus-newsgroup-charset default-charset)
3149         (let ((locals gnus-newsgroup-variables))
3150           (while locals
3151             (if (consp (car locals))
3152                 (set (caar locals) (pop vlist))
3153               (set (car locals) (pop vlist)))
3154             (setq locals (cdr locals))))
3155         ;; The article buffer also has local variables.
3156         (when (gnus-buffer-live-p gnus-article-buffer)
3157           (set-buffer gnus-article-buffer)
3158           (setq gnus-summary-buffer summary))))))
3159
3160 (defun gnus-summary-article-unread-p (article)
3161   "Say whether ARTICLE is unread or not."
3162   (memq article gnus-newsgroup-unreads))
3163
3164 (defun gnus-summary-first-article-p (&optional article)
3165   "Return whether ARTICLE is the first article in the buffer."
3166   (if (not (setq article (or article (gnus-summary-article-number))))
3167       nil
3168     (eq article (caar gnus-newsgroup-data))))
3169
3170 (defun gnus-summary-last-article-p (&optional article)
3171   "Return whether ARTICLE is the last article in the buffer."
3172   (if (not (setq article (or article (gnus-summary-article-number))))
3173       ;; All non-existent numbers are the last article.  :-)
3174       t
3175     (not (cdr (gnus-data-find-list article)))))
3176
3177 (defun gnus-make-thread-indent-array ()
3178   (let ((n 200))
3179     (unless (and gnus-thread-indent-array
3180                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3181       (setq gnus-thread-indent-array (make-vector 201 "")
3182             gnus-thread-indent-array-level gnus-thread-indent-level)
3183       (while (>= n 0)
3184         (aset gnus-thread-indent-array n
3185               (make-string (* n gnus-thread-indent-level) ? ))
3186         (setq n (1- n))))))
3187
3188 (defun gnus-update-summary-mark-positions ()
3189   "Compute where the summary marks are to go."
3190   (save-excursion
3191     (when (gnus-buffer-exists-p gnus-summary-buffer)
3192       (set-buffer gnus-summary-buffer))
3193     (let ((gnus-replied-mark 129)
3194           (gnus-score-below-mark 130)
3195           (gnus-score-over-mark 130)
3196           (gnus-undownloaded-mark 131)
3197           (spec gnus-summary-line-format-spec)
3198           gnus-visual pos)
3199       (save-excursion
3200         (gnus-set-work-buffer)
3201         (let ((gnus-summary-line-format-spec spec)
3202               (gnus-newsgroup-downloadable '(0)))
3203           (gnus-summary-insert-line
3204            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3205            0 nil t 128 t nil "" nil 1)
3206           (goto-char (point-min))
3207           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3208                                              (- (point) (point-min) 1)))))
3209           (goto-char (point-min))
3210           (push (cons 'replied (and (search-forward "\201" nil t)
3211                                     (- (point) (point-min) 1)))
3212                 pos)
3213           (goto-char (point-min))
3214           (push (cons 'score (and (search-forward "\202" nil t)
3215                                   (- (point) (point-min) 1)))
3216                 pos)
3217           (goto-char (point-min))
3218           (push (cons 'download
3219                       (and (search-forward "\203" nil t)
3220                            (- (point) (point-min) 1)))
3221                 pos)))
3222       (setq gnus-summary-mark-positions pos))))
3223
3224 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3225   "Insert a dummy root in the summary buffer."
3226   (beginning-of-line)
3227   (gnus-add-text-properties
3228    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3229    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3230
3231 (defun gnus-summary-extract-address-component (from)
3232   (or (car (funcall gnus-extract-address-components from))
3233       from))
3234
3235 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3236   (let ((mail-parse-charset gnus-newsgroup-charset)
3237         ; Is it really necessary to do this next part for each summary line?
3238         ; Luckily, doesn't seem to slow things down much.
3239         (mail-parse-ignored-charsets
3240          (save-excursion (set-buffer gnus-summary-buffer)
3241                          gnus-newsgroup-ignored-charsets)))
3242     (or
3243      (and gnus-ignored-from-addresses
3244           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3245           (let ((extra-headers (mail-header-extra header))
3246                 to
3247                 newsgroups)
3248             (cond
3249              ((setq to (cdr (assq 'To extra-headers)))
3250               (concat "-> "
3251                       (inline
3252                         (gnus-summary-extract-address-component
3253                          (funcall gnus-decode-encoded-word-function to)))))
3254              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3255               (concat "=> " newsgroups)))))
3256      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3257
3258 (defun gnus-summary-insert-line (gnus-tmp-header
3259                                  gnus-tmp-level gnus-tmp-current
3260                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3261                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3262                                  &optional gnus-tmp-dummy gnus-tmp-score
3263                                  gnus-tmp-process)
3264   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3265          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3266          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3267          (gnus-tmp-score-char
3268           (if (or (null gnus-summary-default-score)
3269                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3270                       gnus-summary-zcore-fuzz))
3271               ?                         ;Whitespace
3272             (if (< gnus-tmp-score gnus-summary-default-score)
3273                 gnus-score-below-mark gnus-score-over-mark)))
3274          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3275          (gnus-tmp-replied
3276           (cond (gnus-tmp-process gnus-process-mark)
3277                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3278                  gnus-cached-mark)
3279                 (gnus-tmp-replied gnus-replied-mark)
3280                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3281                  gnus-forwarded-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3283                  gnus-saved-mark)
3284                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3285                  gnus-recent-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3287                  gnus-unseen-mark)
3288                 (t gnus-no-mark)))
3289          (gnus-tmp-downloaded
3290           (cond (undownloaded 
3291                  gnus-undownloaded-mark)
3292                 (gnus-newsgroup-agentized
3293                  gnus-downloaded-mark)
3294                 (t
3295                  gnus-no-mark)))
3296          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3297          (gnus-tmp-name
3298           (cond
3299            ((string-match "<[^>]+> *$" gnus-tmp-from)
3300             (let ((beg (match-beginning 0)))
3301               (or (and (string-match "^\".+\"" gnus-tmp-from)
3302                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3303                   (substring gnus-tmp-from 0 beg))))
3304            ((string-match "(.+)" gnus-tmp-from)
3305             (substring gnus-tmp-from
3306                        (1+ (match-beginning 0)) (1- (match-end 0))))
3307            (t gnus-tmp-from)))
3308          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3309          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3310          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3311          (buffer-read-only nil))
3312     (when (string= gnus-tmp-name "")
3313       (setq gnus-tmp-name gnus-tmp-from))
3314     (unless (numberp gnus-tmp-lines)
3315       (setq gnus-tmp-lines -1))
3316     (if (= gnus-tmp-lines -1)
3317         (setq gnus-tmp-lines "?")
3318       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3319       (gnus-put-text-property
3320      (point)
3321      (progn (eval gnus-summary-line-format-spec) (point))
3322        'gnus-number gnus-tmp-number)
3323     (when (gnus-visual-p 'summary-highlight 'highlight)
3324       (forward-line -1)
3325       (gnus-run-hooks 'gnus-summary-update-hook)
3326       (forward-line 1))))
3327
3328 (defun gnus-summary-update-line (&optional dont-update)
3329   "Update summary line after change."
3330   (when (and gnus-summary-default-score
3331              (not gnus-summary-inhibit-highlight))
3332     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3333            (article (gnus-summary-article-number))
3334            (score (gnus-summary-article-score article)))
3335       (unless dont-update
3336         (if (and gnus-summary-mark-below
3337                  (< (gnus-summary-article-score)
3338                     gnus-summary-mark-below))
3339             ;; This article has a low score, so we mark it as read.
3340             (when (memq article gnus-newsgroup-unreads)
3341               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3342           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3343             ;; This article was previously marked as read on account
3344             ;; of a low score, but now it has risen, so we mark it as
3345             ;; unread.
3346             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3347         (gnus-summary-update-mark
3348          (if (or (null gnus-summary-default-score)
3349                  (<= (abs (- score gnus-summary-default-score))
3350                      gnus-summary-zcore-fuzz))
3351              ?                          ;Whitespace
3352            (if (< score gnus-summary-default-score)
3353                gnus-score-below-mark gnus-score-over-mark))
3354          'score))
3355       ;; Do visual highlighting.
3356       (when (gnus-visual-p 'summary-highlight 'highlight)
3357         (gnus-run-hooks 'gnus-summary-update-hook)))))
3358
3359 (defvar gnus-tmp-new-adopts nil)
3360
3361 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3362   "Return the number of articles in THREAD.
3363 This may be 0 in some cases -- if none of the articles in
3364 the thread are to be displayed."
3365   (let* ((number
3366          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3367           (cond
3368            ((not (listp thread))
3369             1)
3370            ((and (consp thread) (cdr thread))
3371             (apply
3372              '+ 1 (mapcar
3373                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3374            ((null thread)
3375             1)
3376            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3377             1)
3378            (t 0))))
3379     (when (and level (zerop level) gnus-tmp-new-adopts)
3380       (incf number
3381             (apply '+ (mapcar
3382                        'gnus-summary-number-of-articles-in-thread
3383                        gnus-tmp-new-adopts))))
3384     (if char
3385         (if (> number 1) gnus-not-empty-thread-mark
3386           gnus-empty-thread-mark)
3387       number)))
3388
3389 (defsubst gnus-summary-line-message-size (head)
3390   "Return pretty-printed version of message size.
3391 This function is intended to be used in
3392 `gnus-summary-line-format-alist'."
3393   (let ((c (or (mail-header-chars head) -1)))
3394     (cond ((< c 0) "n/a")               ; chars not available
3395           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3396           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3397           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3398           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3399
3400
3401 (defun gnus-summary-set-local-parameters (group)
3402   "Go through the local params of GROUP and set all variable specs in that list."
3403   (let ((params (gnus-group-find-parameter group))
3404         (vars '(quit-config))           ; Ignore quit-config.
3405         elem)
3406     (while params
3407       (setq elem (car params)
3408             params (cdr params))
3409       (and (consp elem)                 ; Has to be a cons.
3410            (consp (cdr elem))           ; The cdr has to be a list.
3411            (symbolp (car elem))         ; Has to be a symbol in there.
3412            (not (memq (car elem) vars))
3413            (ignore-errors               ; So we set it.
3414              (push (car elem) vars)
3415              (make-local-variable (car elem))
3416              (set (car elem) (eval (nth 1 elem))))))))
3417
3418 (defun gnus-summary-read-group (group &optional show-all no-article
3419                                       kill-buffer no-display backward
3420                                       select-articles)
3421   "Start reading news in newsgroup GROUP.
3422 If SHOW-ALL is non-nil, already read articles are also listed.
3423 If NO-ARTICLE is non-nil, no article is selected initially.
3424 If NO-DISPLAY, don't generate a summary buffer."
3425   (let (result)
3426     (while (and group
3427                 (null (setq result
3428                             (let ((gnus-auto-select-next nil))
3429                               (or (gnus-summary-read-group-1
3430                                    group show-all no-article
3431                                    kill-buffer no-display
3432                                    select-articles)
3433                                   (setq show-all nil
3434                                         select-articles nil)))))
3435                 (eq gnus-auto-select-next 'quietly))
3436       (set-buffer gnus-group-buffer)
3437       ;; The entry function called above goes to the next
3438       ;; group automatically, so we go two groups back
3439       ;; if we are searching for the previous group.
3440       (when backward
3441         (gnus-group-prev-unread-group 2))
3442       (if (not (equal group (gnus-group-group-name)))
3443           (setq group (gnus-group-group-name))
3444         (setq group nil)))
3445     result))
3446
3447 (defun gnus-summary-read-group-1 (group show-all no-article
3448                                         kill-buffer no-display
3449                                         &optional select-articles)
3450   ;; Killed foreign groups can't be entered.
3451   ;;  (when (and (not (gnus-group-native-p group))
3452   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3453   ;;    (error "Dead non-native groups can't be entered"))
3454   (gnus-message 5 "Retrieving newsgroup: %s..."
3455                 (gnus-group-decoded-name group))
3456   (let* ((new-group (gnus-summary-setup-buffer group))
3457          (quit-config (gnus-group-quit-config group))
3458          (did-select (and new-group (gnus-select-newsgroup
3459                                      group show-all select-articles))))
3460     (cond
3461      ;; This summary buffer exists already, so we just select it.
3462      ((not new-group)
3463       (gnus-set-global-variables)
3464       (when kill-buffer
3465         (gnus-kill-or-deaden-summary kill-buffer))
3466       (gnus-configure-windows 'summary 'force)
3467       (gnus-set-mode-line 'summary)
3468       (gnus-summary-position-point)
3469       (message "")
3470       t)
3471      ;; We couldn't select this group.
3472      ((null did-select)
3473       (when (and (eq major-mode 'gnus-summary-mode)
3474                  (not (equal (current-buffer) kill-buffer)))
3475         (kill-buffer (current-buffer))
3476         (if (not quit-config)
3477             (progn
3478               ;; Update the info -- marks might need to be removed,
3479               ;; for instance.
3480               (gnus-summary-update-info)
3481               (set-buffer gnus-group-buffer)
3482               (gnus-group-jump-to-group group)
3483               (gnus-group-next-unread-group 1))
3484           (gnus-handle-ephemeral-exit quit-config)))
3485       (let ((grpinfo (gnus-get-info group)))
3486         (if (null (gnus-info-read grpinfo))
3487             (gnus-message 3 "Group %s contains no messages"
3488                           (gnus-group-decoded-name group))
3489           (gnus-message 3 "Can't select group")))
3490       nil)
3491      ;; The user did a `C-g' while prompting for number of articles,
3492      ;; so we exit this group.
3493      ((eq did-select 'quit)
3494       (and (eq major-mode 'gnus-summary-mode)
3495            (not (equal (current-buffer) kill-buffer))
3496            (kill-buffer (current-buffer)))
3497       (when kill-buffer
3498         (gnus-kill-or-deaden-summary kill-buffer))
3499       (if (not quit-config)
3500           (progn
3501             (set-buffer gnus-group-buffer)
3502             (gnus-group-jump-to-group group)
3503             (gnus-group-next-unread-group 1)
3504             (gnus-configure-windows 'group 'force))
3505         (gnus-handle-ephemeral-exit quit-config))
3506       ;; Finally signal the quit.
3507       (signal 'quit nil))
3508      ;; The group was successfully selected.
3509      (t
3510       (gnus-set-global-variables)
3511       ;; Save the active value in effect when the group was entered.
3512       (setq gnus-newsgroup-active
3513             (gnus-copy-sequence
3514              (gnus-active gnus-newsgroup-name)))
3515       ;; You can change the summary buffer in some way with this hook.
3516       (gnus-run-hooks 'gnus-select-group-hook)
3517       (gnus-update-format-specifications
3518        nil 'summary 'summary-mode 'summary-dummy)
3519       (gnus-update-summary-mark-positions)
3520       ;; Do score processing.
3521       (when gnus-use-scoring
3522         (gnus-possibly-score-headers))
3523       ;; Check whether to fill in the gaps in the threads.
3524       (when gnus-build-sparse-threads
3525         (gnus-build-sparse-threads))
3526       ;; Find the initial limit.
3527       (if gnus-show-threads
3528           (if show-all
3529               (let ((gnus-newsgroup-dormant nil))
3530                 (gnus-summary-initial-limit show-all))
3531             (gnus-summary-initial-limit show-all))
3532         ;; When unthreaded, all articles are always shown.
3533         (setq gnus-newsgroup-limit
3534               (mapcar
3535                (lambda (header) (mail-header-number header))
3536                gnus-newsgroup-headers)))
3537       ;; Generate the summary buffer.
3538       (unless no-display
3539         (gnus-summary-prepare))
3540       (when gnus-use-trees
3541         (gnus-tree-open group)
3542         (setq gnus-summary-highlight-line-function
3543               'gnus-tree-highlight-article))
3544       ;; If the summary buffer is empty, but there are some low-scored
3545       ;; articles or some excluded dormants, we include these in the
3546       ;; buffer.
3547       (when (and (zerop (buffer-size))
3548                  (not no-display))
3549         (cond (gnus-newsgroup-dormant
3550                (gnus-summary-limit-include-dormant))
3551               ((and gnus-newsgroup-scored show-all)
3552                (gnus-summary-limit-include-expunged t))))
3553       ;; Function `gnus-apply-kill-file' must be called in this hook.
3554       (gnus-run-hooks 'gnus-apply-kill-hook)
3555       (if (and (zerop (buffer-size))
3556                (not no-display))
3557           (progn
3558             ;; This newsgroup is empty.
3559             (gnus-summary-catchup-and-exit nil t)
3560             (gnus-message 6 "No unread news")
3561             (when kill-buffer
3562               (gnus-kill-or-deaden-summary kill-buffer))
3563             ;; Return nil from this function.
3564             nil)
3565         ;; Hide conversation thread subtrees.  We cannot do this in
3566         ;; gnus-summary-prepare-hook since kill processing may not
3567         ;; work with hidden articles.
3568         (gnus-summary-maybe-hide-threads)
3569         (when kill-buffer
3570           (gnus-kill-or-deaden-summary kill-buffer))
3571         (gnus-summary-auto-select-subject)
3572         ;; Show first unread article if requested.
3573         (if (and (not no-article)
3574                  (not no-display)
3575                  gnus-newsgroup-unreads
3576                  gnus-auto-select-first)
3577             (progn
3578               (gnus-configure-windows 'summary)
3579               (let ((art (gnus-summary-article-number)))
3580                 (unless (and (not gnus-plugged)
3581                              (or (memq art gnus-newsgroup-undownloaded)
3582                                  (memq art gnus-newsgroup-downloadable)))
3583                   (gnus-summary-goto-article art))))
3584           ;; Don't select any articles.
3585           (gnus-summary-position-point)
3586           (gnus-configure-windows 'summary 'force)
3587           (gnus-set-mode-line 'summary))
3588         (when (and gnus-auto-center-group
3589                    (get-buffer-window gnus-group-buffer t))
3590           ;; Gotta use windows, because recenter does weird stuff if
3591           ;; the current buffer ain't the displayed window.
3592           (let ((owin (selected-window)))
3593             (select-window (get-buffer-window gnus-group-buffer t))
3594             (when (gnus-group-goto-group group)
3595               (recenter))
3596             (select-window owin)))
3597         ;; Mark this buffer as "prepared".
3598         (setq gnus-newsgroup-prepared t)
3599         (gnus-run-hooks 'gnus-summary-prepared-hook)
3600         (unless (gnus-ephemeral-group-p group)
3601           (gnus-group-update-group group))
3602         t)))))
3603
3604 (defun gnus-summary-auto-select-subject ()
3605   "Select the subject line on initial group entry."
3606   (goto-char (point-min))
3607   (cond
3608    ((eq gnus-auto-select-subject 'best)
3609     (gnus-summary-best-unread-subject))
3610    ((eq gnus-auto-select-subject 'unread)
3611     (gnus-summary-first-unread-subject))
3612    ((eq gnus-auto-select-subject 'unseen)
3613     (gnus-summary-first-unseen-subject))
3614    ((eq gnus-auto-select-subject 'unseen-or-unread)
3615     (gnus-summary-first-unseen-or-unread-subject))
3616    ((eq gnus-auto-select-subject 'first)
3617     ;; Do nothing.
3618     )
3619    ((gnus-functionp gnus-auto-select-subject)
3620     (funcall gnus-auto-select-subject))))
3621
3622 (defun gnus-summary-prepare ()
3623   "Generate the summary buffer."
3624   (interactive)
3625   (let ((buffer-read-only nil))
3626     (erase-buffer)
3627     (setq gnus-newsgroup-data nil
3628           gnus-newsgroup-data-reverse nil)
3629     (gnus-run-hooks 'gnus-summary-generate-hook)
3630     ;; Generate the buffer, either with threads or without.
3631     (when gnus-newsgroup-headers
3632       (gnus-summary-prepare-threads
3633        (if gnus-show-threads
3634            (gnus-sort-gathered-threads
3635             (funcall gnus-summary-thread-gathering-function
3636                      (gnus-sort-threads
3637                       (gnus-cut-threads (gnus-make-threads)))))
3638          ;; Unthreaded display.
3639          (gnus-sort-articles gnus-newsgroup-headers))))
3640     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3641     ;; Call hooks for modifying summary buffer.
3642     (goto-char (point-min))
3643     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3644
3645 (defsubst gnus-general-simplify-subject (subject)
3646   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3647   (setq subject
3648         (cond
3649          ;; Truncate the subject.
3650          (gnus-simplify-subject-functions
3651           (gnus-map-function gnus-simplify-subject-functions subject))
3652          ((numberp gnus-summary-gather-subject-limit)
3653           (setq subject (gnus-simplify-subject-re subject))
3654           (if (> (length subject) gnus-summary-gather-subject-limit)
3655               (substring subject 0 gnus-summary-gather-subject-limit)
3656             subject))
3657          ;; Fuzzily simplify it.
3658          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3659           (gnus-simplify-subject-fuzzy subject))
3660          ;; Just remove the leading "Re:".
3661          (t
3662           (gnus-simplify-subject-re subject))))
3663
3664   (if (and gnus-summary-gather-exclude-subject
3665            (string-match gnus-summary-gather-exclude-subject subject))
3666       nil                         ; This article shouldn't be gathered
3667     subject))
3668
3669 (defun gnus-summary-simplify-subject-query ()
3670   "Query where the respool algorithm would put this article."
3671   (interactive)
3672   (gnus-summary-select-article)
3673   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3674
3675 (defun gnus-gather-threads-by-subject (threads)
3676   "Gather threads by looking at Subject headers."
3677   (if (not gnus-summary-make-false-root)
3678       threads
3679     (let ((hashtb (gnus-make-hashtable 1024))
3680           (prev threads)
3681           (result threads)
3682           subject hthread whole-subject)
3683       (while threads
3684         (setq subject (gnus-general-simplify-subject
3685                        (setq whole-subject (mail-header-subject
3686                                             (caar threads)))))
3687         (when subject
3688           (if (setq hthread (gnus-gethash subject hashtb))
3689               (progn
3690                 ;; We enter a dummy root into the thread, if we
3691                 ;; haven't done that already.
3692                 (unless (stringp (caar hthread))
3693                   (setcar hthread (list whole-subject (car hthread))))
3694                 ;; We add this new gathered thread to this gathered
3695                 ;; thread.
3696                 (setcdr (car hthread)
3697                         (nconc (cdar hthread) (list (car threads))))
3698                 ;; Remove it from the list of threads.
3699                 (setcdr prev (cdr threads))
3700                 (setq threads prev))
3701             ;; Enter this thread into the hash table.
3702             (gnus-sethash subject
3703                           (if gnus-summary-make-false-root-always
3704                               (progn
3705                                 ;; If you want a dummy root above all
3706                                 ;; threads...
3707                                 (setcar threads (list whole-subject
3708                                                       (car threads)))
3709                                 threads)
3710                             threads)
3711                           hashtb)))
3712         (setq prev threads)
3713         (setq threads (cdr threads)))
3714       result)))
3715
3716 (defun gnus-gather-threads-by-references (threads)
3717   "Gather threads by looking at References headers."
3718   (let ((idhashtb (gnus-make-hashtable 1024))
3719         (thhashtb (gnus-make-hashtable 1024))
3720         (prev threads)
3721         (result threads)
3722         ids references id gthread gid entered ref)
3723     (while threads
3724       (when (setq references (mail-header-references (caar threads)))
3725         (setq id (mail-header-id (caar threads))
3726               ids (inline (gnus-split-references references))
3727               entered nil)
3728         (while (setq ref (pop ids))
3729           (setq ids (delete ref ids))
3730           (if (not (setq gid (gnus-gethash ref idhashtb)))
3731               (progn
3732                 (gnus-sethash ref id idhashtb)
3733                 (gnus-sethash id threads thhashtb))
3734             (setq gthread (gnus-gethash gid thhashtb))
3735             (unless entered
3736               ;; We enter a dummy root into the thread, if we
3737               ;; haven't done that already.
3738               (unless (stringp (caar gthread))
3739                 (setcar gthread (list (mail-header-subject (caar gthread))
3740                                       (car gthread))))
3741               ;; We add this new gathered thread to this gathered
3742               ;; thread.
3743               (setcdr (car gthread)
3744                       (nconc (cdar gthread) (list (car threads)))))
3745             ;; Add it into the thread hash table.
3746             (gnus-sethash id gthread thhashtb)
3747             (setq entered t)
3748             ;; Remove it from the list of threads.
3749             (setcdr prev (cdr threads))
3750             (setq threads prev))))
3751       (setq prev threads)
3752       (setq threads (cdr threads)))
3753     result))
3754
3755 (defun gnus-sort-gathered-threads (threads)
3756   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3757   (let ((result threads))
3758     (while threads
3759       (when (stringp (caar threads))
3760         (setcdr (car threads)
3761                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3762       (setq threads (cdr threads)))
3763     result))
3764
3765 (defun gnus-thread-loop-p (root thread)
3766   "Say whether ROOT is in THREAD."
3767   (let ((stack (list thread))
3768         (infloop 0)
3769         th)
3770     (while (setq thread (pop stack))
3771       (setq th (cdr thread))
3772       (while (and th
3773                   (not (eq (caar th) root)))
3774         (pop th))
3775       (if th
3776           ;; We have found a loop.
3777           (let (ref-dep)
3778             (setcdr thread (delq (car th) (cdr thread)))
3779             (if (boundp (setq ref-dep (intern "none"
3780                                               gnus-newsgroup-dependencies)))
3781                 (setcdr (symbol-value ref-dep)
3782                         (nconc (cdr (symbol-value ref-dep))
3783                                (list (car th))))
3784               (set ref-dep (list nil (car th))))
3785             (setq infloop 1
3786                   stack nil))
3787         ;; Push all the subthreads onto the stack.
3788         (push (cdr thread) stack)))
3789     infloop))
3790
3791 (defun gnus-make-threads ()
3792   "Go through the dependency hashtb and find the roots.  Return all threads."
3793   (let (threads)
3794     (while (catch 'infloop
3795              (mapatoms
3796               (lambda (refs)
3797                 ;; Deal with self-referencing References loops.
3798                 (when (and (car (symbol-value refs))
3799                            (not (zerop
3800                                  (apply
3801                                   '+
3802                                   (mapcar
3803                                    (lambda (thread)
3804                                      (gnus-thread-loop-p
3805                                       (car (symbol-value refs)) thread))
3806                                    (cdr (symbol-value refs)))))))
3807                   (setq threads nil)
3808                   (throw 'infloop t))
3809                 (unless (car (symbol-value refs))
3810                   ;; These threads do not refer back to any other
3811                   ;; articles, so they're roots.
3812                   (setq threads (append (cdr (symbol-value refs)) threads))))
3813               gnus-newsgroup-dependencies)))
3814     threads))
3815
3816 ;; Build the thread tree.
3817 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3818   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3819
3820 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3821 if it was already present.
3822
3823 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3824 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3825 Message-IDs will be renamed to a unique Message-ID before being
3826 entered.
3827
3828 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3829   (let* ((id (mail-header-id header))
3830          (id-dep (and id (intern id dependencies)))
3831          parent-id ref ref-dep ref-header replaced)
3832     ;; Enter this `header' in the `dependencies' table.
3833     (cond
3834      ((not id-dep)
3835       (setq header nil))
3836      ;; The first two cases do the normal part: enter a new `header'
3837      ;; in the `dependencies' table.
3838      ((not (boundp id-dep))
3839       (set id-dep (list header)))
3840      ((null (car (symbol-value id-dep)))
3841       (setcar (symbol-value id-dep) header))
3842
3843      ;; From here the `header' was already present in the
3844      ;; `dependencies' table.
3845      (force-new
3846       ;; Overrides an existing entry;
3847       ;; just set the header part of the entry.
3848       (setcar (symbol-value id-dep) header)
3849       (setq replaced t))
3850
3851      ;; Renames the existing `header' to a unique Message-ID.
3852      ((not gnus-summary-ignore-duplicates)
3853       ;; An article with this Message-ID has already been seen.
3854       ;; We rename the Message-ID.
3855       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3856            (list header))
3857       (mail-header-set-id header id))
3858
3859      ;; The last case ignores an existing entry, except it adds any
3860      ;; additional Xrefs (in case the two articles came from different
3861      ;; servers.
3862      ;; Also sets `header' to `nil' meaning that the `dependencies'
3863      ;; table was *not* modified.
3864      (t
3865       (mail-header-set-xref
3866        (car (symbol-value id-dep))
3867        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3868                    "")
3869                (or (mail-header-xref header) "")))
3870       (setq header nil)))
3871
3872     (when (and header (not replaced))
3873       ;; First check that we are not creating a References loop.
3874       (setq parent-id (gnus-parent-id (mail-header-references header)))
3875       (setq ref parent-id)
3876       (while (and ref
3877                   (setq ref-dep (intern-soft ref dependencies))
3878                   (boundp ref-dep)
3879                   (setq ref-header (car (symbol-value ref-dep))))
3880         (if (string= id ref)
3881             ;; Yuk!  This is a reference loop.  Make the article be a
3882             ;; root article.
3883             (progn
3884               (mail-header-set-references (car (symbol-value id-dep)) "none")
3885               (setq ref nil)
3886               (setq parent-id nil))
3887           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3888       (setq ref-dep (intern (or parent-id "none") dependencies))
3889       (if (boundp ref-dep)
3890           (setcdr (symbol-value ref-dep)
3891                   (nconc (cdr (symbol-value ref-dep))
3892                          (list (symbol-value id-dep))))
3893         (set ref-dep (list nil (symbol-value id-dep)))))
3894     header))
3895
3896 (defun gnus-extract-message-id-from-in-reply-to (string)
3897   (if (string-match "<[^>]+>" string)
3898       (substring string (match-beginning 0) (match-end 0))
3899     nil))
3900
3901 (defun gnus-build-sparse-threads ()
3902   (let ((headers gnus-newsgroup-headers)
3903         (mail-parse-charset gnus-newsgroup-charset)
3904         (gnus-summary-ignore-duplicates t)
3905         header references generation relations
3906         subject child end new-child date)
3907     ;; First we create an alist of generations/relations, where
3908     ;; generations is how much we trust the relation, and the relation
3909     ;; is parent/child.
3910     (gnus-message 7 "Making sparse threads...")
3911     (save-excursion
3912       (nnheader-set-temp-buffer " *gnus sparse threads*")
3913       (while (setq header (pop headers))
3914         (when (and (setq references (mail-header-references header))
3915                    (not (string= references "")))
3916           (insert references)
3917           (setq child (mail-header-id header)
3918                 subject (mail-header-subject header)
3919                 date (mail-header-date header)
3920                 generation 0)
3921           (while (search-backward ">" nil t)
3922             (setq end (1+ (point)))
3923             (when (search-backward "<" nil t)
3924               (setq new-child (buffer-substring (point) end))
3925               (push (list (incf generation)
3926                           child (setq child new-child)
3927                           subject date)
3928                     relations)))
3929           (when child
3930             (push (list (1+ generation) child nil subject) relations))
3931           (erase-buffer)))
3932       (kill-buffer (current-buffer)))
3933     ;; Sort over trustworthiness.
3934     (mapcar
3935      (lambda (relation)
3936        (when (gnus-dependencies-add-header
3937               (make-full-mail-header
3938                gnus-reffed-article-number
3939                (nth 3 relation) "" (or (nth 4 relation) "")
3940                (nth 1 relation)
3941                (or (nth 2 relation) "") 0 0 "")
3942               gnus-newsgroup-dependencies nil)
3943          (push gnus-reffed-article-number gnus-newsgroup-limit)
3944          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3945          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3946                gnus-newsgroup-reads)
3947          (decf gnus-reffed-article-number)))
3948      (sort relations 'car-less-than-car))
3949     (gnus-message 7 "Making sparse threads...done")))
3950
3951 (defun gnus-build-old-threads ()
3952   ;; Look at all the articles that refer back to old articles, and
3953   ;; fetch the headers for the articles that aren't there.  This will
3954   ;; build complete threads - if the roots haven't been expired by the
3955   ;; server, that is.
3956   (let ((mail-parse-charset gnus-newsgroup-charset)
3957         id heads)
3958     (mapatoms
3959      (lambda (refs)
3960        (when (not (car (symbol-value refs)))
3961          (setq heads (cdr (symbol-value refs)))
3962          (while heads
3963            (if (memq (mail-header-number (caar heads))
3964                      gnus-newsgroup-dormant)
3965                (setq heads (cdr heads))
3966              (setq id (symbol-name refs))
3967              (while (and (setq id (gnus-build-get-header id))
3968                          (not (car (gnus-id-to-thread id)))))
3969              (setq heads nil)))))
3970      gnus-newsgroup-dependencies)))
3971
3972 ;; This function has to be called with point after the article number
3973 ;; on the beginning of the line.
3974 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3975   (let ((eol (gnus-point-at-eol))
3976         (buffer (current-buffer))
3977         header references in-reply-to)
3978
3979     ;; overview: [num subject from date id refs chars lines misc]
3980     (unwind-protect
3981         (let (x)
3982           (narrow-to-region (point) eol)
3983           (unless (eobp)
3984             (forward-char))
3985
3986           (setq header
3987                 (make-full-mail-header
3988                  number                 ; number
3989                  (condition-case ()     ; subject
3990                      (funcall gnus-decode-encoded-word-function
3991                               (setq x (nnheader-nov-field)))
3992                    (error x))
3993                  (condition-case ()     ; from
3994                      (funcall gnus-decode-encoded-word-function
3995                               (setq x (nnheader-nov-field)))
3996                    (error x))
3997                  (nnheader-nov-field)   ; date
3998                  (nnheader-nov-read-message-id) ; id
3999                  (setq references (nnheader-nov-field)) ; refs
4000                  (nnheader-nov-read-integer) ; chars
4001                  (nnheader-nov-read-integer) ; lines
4002                  (unless (eobp)
4003                    (if (looking-at "Xref: ")
4004                        (goto-char (match-end 0)))
4005                    (nnheader-nov-field)) ; Xref
4006                  (nnheader-nov-parse-extra)))) ; extra
4007
4008       (widen))
4009
4010     (when (and (string= references "")
4011                (setq in-reply-to (mail-header-extra header))
4012                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4013       (mail-header-set-references
4014        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4015
4016     (when gnus-alter-header-function
4017       (funcall gnus-alter-header-function header))
4018     (gnus-dependencies-add-header header dependencies force-new)))
4019
4020 (defun gnus-build-get-header (id)
4021   "Look through the buffer of NOV lines and find the header to ID.
4022 Enter this line into the dependencies hash table, and return
4023 the id of the parent article (if any)."
4024   (let ((deps gnus-newsgroup-dependencies)
4025         found header)
4026     (prog1
4027         (save-excursion
4028           (set-buffer nntp-server-buffer)
4029           (let ((case-fold-search nil))
4030             (goto-char (point-min))
4031             (while (and (not found)
4032                         (search-forward id nil t))
4033               (beginning-of-line)
4034               (setq found (looking-at
4035                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4036                                    (regexp-quote id))))
4037               (or found (beginning-of-line 2)))
4038             (when found
4039               (beginning-of-line)
4040               (and
4041                (setq header (gnus-nov-parse-line
4042                              (read (current-buffer)) deps))
4043                (gnus-parent-id (mail-header-references header))))))
4044       (when header
4045         (let ((number (mail-header-number header)))
4046           (push number gnus-newsgroup-limit)
4047           (push header gnus-newsgroup-headers)
4048           (if (memq number gnus-newsgroup-unselected)
4049               (progn
4050                 (setq gnus-newsgroup-unreads
4051                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4052                                                number))
4053                 (setq gnus-newsgroup-unselected
4054                       (delq number gnus-newsgroup-unselected)))
4055             (push number gnus-newsgroup-ancient)))))))
4056
4057 (defun gnus-build-all-threads ()
4058   "Read all the headers."
4059   (let ((gnus-summary-ignore-duplicates t)
4060         (mail-parse-charset gnus-newsgroup-charset)
4061         (dependencies gnus-newsgroup-dependencies)
4062         header article)
4063     (save-excursion
4064       (set-buffer nntp-server-buffer)
4065       (let ((case-fold-search nil))
4066         (goto-char (point-min))
4067         (while (not (eobp))
4068           (ignore-errors
4069             (setq article (read (current-buffer))
4070                   header (gnus-nov-parse-line article dependencies)))
4071           (when header
4072             (save-excursion
4073               (set-buffer gnus-summary-buffer)
4074               (push header gnus-newsgroup-headers)
4075               (if (memq (setq article (mail-header-number header))
4076                         gnus-newsgroup-unselected)
4077                   (progn
4078                     (setq gnus-newsgroup-unreads
4079                           (gnus-add-to-sorted-list
4080                            gnus-newsgroup-unreads article))
4081                     (setq gnus-newsgroup-unselected
4082                           (delq article gnus-newsgroup-unselected)))
4083                 (push article gnus-newsgroup-ancient)))
4084             (forward-line 1)))))))
4085
4086 (defun gnus-summary-update-article-line (article header)
4087   "Update the line for ARTICLE using HEADERS."
4088   (let* ((id (mail-header-id header))
4089          (thread (gnus-id-to-thread id)))
4090     (unless thread
4091       (error "Article in no thread"))
4092     ;; Update the thread.
4093     (setcar thread header)
4094     (gnus-summary-goto-subject article)
4095     (let* ((datal (gnus-data-find-list article))
4096            (data (car datal))
4097            (buffer-read-only nil)
4098            (level (gnus-summary-thread-level)))
4099       (gnus-delete-line)
4100       (let ((inserted (- (point)
4101                          (progn
4102                            (gnus-summary-insert-line
4103                             header level nil 
4104                             (memq article gnus-newsgroup-undownloaded)
4105                             (gnus-article-mark article)
4106                             (memq article gnus-newsgroup-replied)
4107                             (memq article gnus-newsgroup-expirable)
4108                             ;; Only insert the Subject string when it's different
4109                             ;; from the previous Subject string.
4110                             (if (and
4111                                  gnus-show-threads
4112                                  (gnus-subject-equal
4113                                   (condition-case ()
4114                                       (mail-header-subject
4115                                        (gnus-data-header
4116                                         (cadr
4117                                          (gnus-data-find-list
4118                                           article
4119                                           (gnus-data-list t)))))
4120                                     ;; Error on the side of excessive subjects.
4121                                     (error ""))
4122                                   (mail-header-subject header)))
4123                                 ""
4124                               (mail-header-subject header))
4125                             nil (cdr (assq article gnus-newsgroup-scored))
4126                             (memq article gnus-newsgroup-processable))
4127                            (point)))))
4128         (when (cdr datal)
4129           (gnus-data-update-list
4130            (cdr datal) 
4131            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4132
4133 (defun gnus-summary-update-article (article &optional iheader)
4134   "Update ARTICLE in the summary buffer."
4135   (set-buffer gnus-summary-buffer)
4136   (let* ((header (gnus-summary-article-header article))
4137          (id (mail-header-id header))
4138          (data (gnus-data-find article))
4139          (thread (gnus-id-to-thread id))
4140          (references (mail-header-references header))
4141          (parent
4142           (gnus-id-to-thread
4143            (or (gnus-parent-id
4144                 (when (and references
4145                            (not (equal "" references)))
4146                   references))
4147                "none")))
4148          (buffer-read-only nil)
4149          (old (car thread)))
4150     (when thread
4151       (unless iheader
4152         (setcar thread nil)
4153         (when parent
4154           (delq thread parent)))
4155       (if (gnus-summary-insert-subject id header)
4156           ;; Set the (possibly) new article number in the data structure.
4157           (gnus-data-set-number data (gnus-id-to-article id))
4158         (setcar thread old)
4159         nil))))
4160
4161 (defun gnus-rebuild-thread (id &optional line)
4162   "Rebuild the thread containing ID.
4163 If LINE, insert the rebuilt thread starting on line LINE."
4164   (let ((buffer-read-only nil)
4165         old-pos current thread data)
4166     (if (not gnus-show-threads)
4167         (setq thread (list (car (gnus-id-to-thread id))))
4168       ;; Get the thread this article is part of.
4169       (setq thread (gnus-remove-thread id)))
4170     (setq old-pos (gnus-point-at-bol))
4171     (setq current (save-excursion
4172                     (and (re-search-backward "[\r\n]" nil t)
4173                          (gnus-summary-article-number))))
4174     ;; If this is a gathered thread, we have to go some re-gathering.
4175     (when (stringp (car thread))
4176       (let ((subject (car thread))
4177             roots thr)
4178         (setq thread (cdr thread))
4179         (while thread
4180           (unless (memq (setq thr (gnus-id-to-thread
4181                                    (gnus-root-id
4182                                     (mail-header-id (caar thread)))))
4183                         roots)
4184             (push thr roots))
4185           (setq thread (cdr thread)))
4186         ;; We now have all (unique) roots.
4187         (if (= (length roots) 1)
4188             ;; All the loose roots are now one solid root.
4189             (setq thread (car roots))
4190           (setq thread (cons subject (gnus-sort-threads roots))))))
4191     (let (threads)
4192       ;; We then insert this thread into the summary buffer.
4193       (when line
4194         (goto-char (point-min))
4195         (forward-line (1- line)))
4196       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4197         (if gnus-show-threads
4198             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4199           (gnus-summary-prepare-unthreaded thread))
4200         (setq data (nreverse gnus-newsgroup-data))
4201         (setq threads gnus-newsgroup-threads))
4202       ;; We splice the new data into the data structure.
4203       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4204       ;;!!! then we want to insert at the beginning of the buffer.
4205       ;;!!! That happens to be true with Gnus now, but that may
4206       ;;!!! change in the future.  Perhaps.
4207       (gnus-data-enter-list
4208        (if line nil current) data (- (point) old-pos))
4209       (setq gnus-newsgroup-threads
4210             (nconc threads gnus-newsgroup-threads))
4211       (gnus-data-compute-positions))))
4212
4213 (defun gnus-number-to-header (number)
4214   "Return the header for article NUMBER."
4215   (let ((headers gnus-newsgroup-headers))
4216     (while (and headers
4217                 (not (= number (mail-header-number (car headers)))))
4218       (pop headers))
4219     (when headers
4220       (car headers))))
4221
4222 (defun gnus-parent-headers (in-headers &optional generation)
4223   "Return the headers of the GENERATIONeth parent of HEADERS."
4224   (unless generation
4225     (setq generation 1))
4226   (let ((parent t)
4227         (headers in-headers)
4228         references)
4229     (while (and parent
4230                 (not (zerop generation))
4231                 (setq references (mail-header-references headers)))
4232       (setq headers (if (and references
4233                              (setq parent (gnus-parent-id references)))
4234                         (car (gnus-id-to-thread parent))
4235                       nil))
4236       (decf generation))
4237     (and (not (eq headers in-headers))
4238          headers)))
4239
4240 (defun gnus-id-to-thread (id)
4241   "Return the (sub-)thread where ID appears."
4242   (gnus-gethash id gnus-newsgroup-dependencies))
4243
4244 (defun gnus-id-to-article (id)
4245   "Return the article number of ID."
4246   (let ((thread (gnus-id-to-thread id)))
4247     (when (and thread
4248                (car thread))
4249       (mail-header-number (car thread)))))
4250
4251 (defun gnus-id-to-header (id)
4252   "Return the article headers of ID."
4253   (car (gnus-id-to-thread id)))
4254
4255 (defun gnus-article-displayed-root-p (article)
4256   "Say whether ARTICLE is a root(ish) article."
4257   (let ((level (gnus-summary-thread-level article))
4258         (refs (mail-header-references  (gnus-summary-article-header article)))
4259         particle)
4260     (cond
4261      ((null level) nil)
4262      ((zerop level) t)
4263      ((null refs) t)
4264      ((null (gnus-parent-id refs)) t)
4265      ((and (= 1 level)
4266            (null (setq particle (gnus-id-to-article
4267                                  (gnus-parent-id refs))))
4268            (null (gnus-summary-thread-level particle)))))))
4269
4270 (defun gnus-root-id (id)
4271   "Return the id of the root of the thread where ID appears."
4272   (let (last-id prev)
4273     (while (and id (setq prev (car (gnus-id-to-thread id))))
4274       (setq last-id id
4275             id (gnus-parent-id (mail-header-references prev))))
4276     last-id))
4277
4278 (defun gnus-articles-in-thread (thread)
4279   "Return the list of articles in THREAD."
4280   (cons (mail-header-number (car thread))
4281         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4282
4283 (defun gnus-remove-thread (id &optional dont-remove)
4284   "Remove the thread that has ID in it."
4285   (let (headers thread last-id)
4286     ;; First go up in this thread until we find the root.
4287     (setq last-id (gnus-root-id id)
4288           headers (message-flatten-list (gnus-id-to-thread last-id)))
4289     ;; We have now found the real root of this thread.  It might have
4290     ;; been gathered into some loose thread, so we have to search
4291     ;; through the threads to find the thread we wanted.
4292     (let ((threads gnus-newsgroup-threads)
4293           sub)
4294       (while threads
4295         (setq sub (car threads))
4296         (if (stringp (car sub))
4297             ;; This is a gathered thread, so we look at the roots
4298             ;; below it to find whether this article is in this
4299             ;; gathered root.
4300             (progn
4301               (setq sub (cdr sub))
4302               (while sub
4303                 (when (member (caar sub) headers)
4304                   (setq thread (car threads)
4305                         threads nil
4306                         sub nil))
4307                 (setq sub (cdr sub))))
4308           ;; It's an ordinary thread, so we check it.
4309           (when (eq (car sub) (car headers))
4310             (setq thread sub
4311                   threads nil)))
4312         (setq threads (cdr threads)))
4313       ;; If this article is in no thread, then it's a root.
4314       (if thread
4315           (unless dont-remove
4316             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4317         (setq thread (gnus-id-to-thread last-id)))
4318       (when thread
4319         (prog1
4320             thread                      ; We return this thread.
4321           (unless dont-remove
4322             (if (stringp (car thread))
4323                 (progn
4324                   ;; If we use dummy roots, then we have to remove the
4325                   ;; dummy root as well.
4326                   (when (eq gnus-summary-make-false-root 'dummy)
4327                     ;; We go to the dummy root by going to
4328                     ;; the first sub-"thread", and then one line up.
4329                     (gnus-summary-goto-article
4330                      (mail-header-number (caadr thread)))
4331                     (forward-line -1)
4332                     (gnus-delete-line)
4333                     (gnus-data-compute-positions))
4334                   (setq thread (cdr thread))
4335                   (while thread
4336                     (gnus-remove-thread-1 (car thread))
4337                     (setq thread (cdr thread))))
4338               (gnus-remove-thread-1 thread))))))))
4339
4340 (defun gnus-remove-thread-1 (thread)
4341   "Remove the thread THREAD recursively."
4342   (let ((number (mail-header-number (pop thread)))
4343         d)
4344     (setq thread (reverse thread))
4345     (while thread
4346       (gnus-remove-thread-1 (pop thread)))
4347     (when (setq d (gnus-data-find number))
4348       (goto-char (gnus-data-pos d))
4349       (gnus-summary-show-thread)
4350       (gnus-data-remove
4351        number
4352        (- (gnus-point-at-bol)
4353           (prog1
4354               (1+ (gnus-point-at-eol))
4355             (gnus-delete-line)))))))
4356
4357 (defun gnus-sort-threads-1 (threads func)
4358   (sort (mapcar (lambda (thread)
4359                   (cons (car thread)
4360                         (and (cdr thread)
4361                              (gnus-sort-threads-1 (cdr thread) func))))
4362                 threads) func))
4363
4364 (defun gnus-sort-threads (threads)
4365   "Sort THREADS."
4366   (if (not gnus-thread-sort-functions)
4367       threads
4368     (gnus-message 8 "Sorting threads...")
4369     (let ((max-lisp-eval-depth 5000))
4370       (prog1 (gnus-sort-threads-1
4371          threads
4372          (gnus-make-sort-function gnus-thread-sort-functions))
4373         (gnus-message 8 "Sorting threads...done")))))
4374
4375 (defun gnus-sort-articles (articles)
4376   "Sort ARTICLES."
4377   (when gnus-article-sort-functions
4378     (gnus-message 7 "Sorting articles...")
4379     (prog1
4380         (setq gnus-newsgroup-headers
4381               (sort articles (gnus-make-sort-function
4382                               gnus-article-sort-functions)))
4383       (gnus-message 7 "Sorting articles...done"))))
4384
4385 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4386 (defmacro gnus-thread-header (thread)
4387   "Return header of first article in THREAD.
4388 Note that THREAD must never, ever be anything else than a variable -
4389 using some other form will lead to serious barfage."
4390   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4391   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4392   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4393         (vector thread) 2))
4394
4395 (defsubst gnus-article-sort-by-number (h1 h2)
4396   "Sort articles by article number."
4397   (< (mail-header-number h1)
4398      (mail-header-number h2)))
4399
4400 (defun gnus-thread-sort-by-number (h1 h2)
4401   "Sort threads by root article number."
4402   (gnus-article-sort-by-number
4403    (gnus-thread-header h1) (gnus-thread-header h2)))
4404
4405 (defsubst gnus-article-sort-by-random (h1 h2)
4406   "Sort articles by article number."
4407   (zerop (random 2)))
4408
4409 (defun gnus-thread-sort-by-random (h1 h2)
4410   "Sort threads by root article number."
4411   (gnus-article-sort-by-random
4412    (gnus-thread-header h1) (gnus-thread-header h2)))
4413
4414 (defsubst gnus-article-sort-by-lines (h1 h2)
4415   "Sort articles by article Lines header."
4416   (< (mail-header-lines h1)
4417      (mail-header-lines h2)))
4418
4419 (defun gnus-thread-sort-by-lines (h1 h2)
4420   "Sort threads by root article Lines header."
4421   (gnus-article-sort-by-lines
4422    (gnus-thread-header h1) (gnus-thread-header h2)))
4423
4424 (defsubst gnus-article-sort-by-chars (h1 h2)
4425   "Sort articles by octet length."
4426   (< (mail-header-chars h1)
4427      (mail-header-chars h2)))
4428
4429 (defun gnus-thread-sort-by-chars (h1 h2)
4430   "Sort threads by root article octet length."
4431   (gnus-article-sort-by-chars
4432    (gnus-thread-header h1) (gnus-thread-header h2)))
4433
4434 (defsubst gnus-article-sort-by-author (h1 h2)
4435   "Sort articles by root author."
4436   (string-lessp
4437    (let ((extract (funcall
4438                    gnus-extract-address-components
4439                    (mail-header-from h1))))
4440      (or (car extract) (cadr extract) ""))
4441    (let ((extract (funcall
4442                    gnus-extract-address-components
4443                    (mail-header-from h2))))
4444      (or (car extract) (cadr extract) ""))))
4445
4446 (defun gnus-thread-sort-by-author (h1 h2)
4447   "Sort threads by root author."
4448   (gnus-article-sort-by-author
4449    (gnus-thread-header h1)  (gnus-thread-header h2)))
4450
4451 (defsubst gnus-article-sort-by-subject (h1 h2)
4452   "Sort articles by root subject."
4453   (string-lessp
4454    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4455    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4456
4457 (defun gnus-thread-sort-by-subject (h1 h2)
4458   "Sort threads by root subject."
4459   (gnus-article-sort-by-subject
4460    (gnus-thread-header h1) (gnus-thread-header h2)))
4461
4462 (defsubst gnus-article-sort-by-date (h1 h2)
4463   "Sort articles by root article date."
4464   (time-less-p
4465    (gnus-date-get-time (mail-header-date h1))
4466    (gnus-date-get-time (mail-header-date h2))))
4467
4468 (defun gnus-thread-sort-by-date (h1 h2)
4469   "Sort threads by root article date."
4470   (gnus-article-sort-by-date
4471    (gnus-thread-header h1) (gnus-thread-header h2)))
4472
4473 (defsubst gnus-article-sort-by-score (h1 h2)
4474   "Sort articles by root article score.
4475 Unscored articles will be counted as having a score of zero."
4476   (> (or (cdr (assq (mail-header-number h1)
4477                     gnus-newsgroup-scored))
4478          gnus-summary-default-score 0)
4479      (or (cdr (assq (mail-header-number h2)
4480                     gnus-newsgroup-scored))
4481          gnus-summary-default-score 0)))
4482
4483 (defun gnus-thread-sort-by-score (h1 h2)
4484   "Sort threads by root article score."
4485   (gnus-article-sort-by-score
4486    (gnus-thread-header h1) (gnus-thread-header h2)))
4487
4488 (defun gnus-thread-sort-by-total-score (h1 h2)
4489   "Sort threads by the sum of all scores in the thread.
4490 Unscored articles will be counted as having a score of zero."
4491   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4492
4493 (defun gnus-thread-total-score (thread)
4494   ;; This function find the total score of THREAD.
4495   (cond
4496    ((null thread)
4497     0)
4498    ((consp thread)
4499     (if (stringp (car thread))
4500         (apply gnus-thread-score-function 0
4501                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4502       (gnus-thread-total-score-1 thread)))
4503    (t
4504     (gnus-thread-total-score-1 (list thread)))))
4505
4506 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4507   "Sort threads such that the thread with the most recently arrived article comes first."
4508   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4509
4510 (defun gnus-thread-highest-number (thread)
4511   "Return the highest article number in THREAD."
4512   (apply 'max (mapcar (lambda (header)
4513                         (mail-header-number header))
4514                       (message-flatten-list thread))))
4515
4516 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4517   "Sort threads such that the thread with the most recently dated article comes first."
4518   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4519
4520 (defun gnus-thread-latest-date (thread)
4521   "Return the highest article date in THREAD."
4522   (let ((previous-time 0))
4523     (apply 'max
4524            (mapcar
4525             (lambda (header)
4526               (setq previous-time
4527                     (time-to-seconds
4528                      (condition-case ()
4529                          (mail-header-parse-date (mail-header-date header))
4530                        (error previous-time)))))
4531             (sort
4532              (message-flatten-list thread)
4533              (lambda (h1 h2)
4534                (< (mail-header-number h1)
4535                   (mail-header-number h2))))))))
4536
4537 (defun gnus-thread-total-score-1 (root)
4538   ;; This function find the total score of the thread below ROOT.
4539   (setq root (car root))
4540   (apply gnus-thread-score-function
4541          (or (append
4542               (mapcar 'gnus-thread-total-score
4543                       (cdr (gnus-id-to-thread (mail-header-id root))))
4544               (when (> (mail-header-number root) 0)
4545                 (list (or (cdr (assq (mail-header-number root)
4546                                      gnus-newsgroup-scored))
4547                           gnus-summary-default-score 0))))
4548              (list gnus-summary-default-score)
4549              '(0))))
4550
4551 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4552 (defvar gnus-tmp-prev-subject nil)
4553 (defvar gnus-tmp-false-parent nil)
4554 (defvar gnus-tmp-root-expunged nil)
4555 (defvar gnus-tmp-dummy-line nil)
4556
4557 (eval-when-compile (defvar gnus-tmp-header))
4558 (defun gnus-extra-header (type &optional header)
4559   "Return the extra header of TYPE."
4560   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4561       ""))
4562
4563 (defvar gnus-tmp-thread-tree-header-string "")
4564
4565 (defcustom gnus-sum-thread-tree-root "> "
4566   "With %B spec, used for the root of a thread.
4567 If nil, use subject instead."
4568   :type 'string
4569   :group 'gnus-thread)
4570 (defcustom gnus-sum-thread-tree-single-indent ""
4571   "With %B spec, used for a thread with just one message.
4572 If nil, use subject instead."
4573   :type 'string
4574   :group 'gnus-thread)
4575 (defcustom gnus-sum-thread-tree-vertical "| "
4576   "With %B spec, used for drawing a vertical line."
4577   :type 'string
4578   :group 'gnus-thread)
4579 (defcustom gnus-sum-thread-tree-indent "  "
4580   "With %B spec, used for indenting."
4581   :type 'string
4582   :group 'gnus-thread)
4583 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4584   "With %B spec, used for a leaf with brothers."
4585   :type 'string
4586   :group 'gnus-thread)
4587 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4588   "With %B spec, used for a leaf without brothers."
4589   :type 'string
4590   :group 'gnus-thread)
4591
4592 (defun gnus-summary-prepare-threads (threads)
4593   "Prepare summary buffer from THREADS and indentation LEVEL.
4594 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4595 or a straight list of headers."
4596   (gnus-message 7 "Generating summary...")
4597
4598   (setq gnus-newsgroup-threads threads)
4599   (beginning-of-line)
4600
4601   (let ((gnus-tmp-level 0)
4602         (default-score (or gnus-summary-default-score 0))
4603         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4604         (building-line-count gnus-summary-display-while-building)
4605         (building-count (integerp gnus-summary-display-while-building))
4606         thread number subject stack state gnus-tmp-gathered beg-match
4607         new-roots gnus-tmp-new-adopts thread-end simp-subject
4608         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4609         gnus-tmp-replied gnus-tmp-subject-or-nil
4610         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4611         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4612         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4613         tree-stack)
4614
4615     (setq gnus-tmp-prev-subject nil
4616           gnus-tmp-thread-tree-header-string "")
4617
4618     (if (vectorp (car threads))
4619         ;; If this is a straight (sic) list of headers, then a
4620         ;; threaded summary display isn't required, so we just create
4621         ;; an unthreaded one.
4622         (gnus-summary-prepare-unthreaded threads)
4623
4624       ;; Do the threaded display.
4625
4626       (if gnus-summary-display-while-building
4627           (switch-to-buffer (buffer-name)))
4628       (while (or threads stack gnus-tmp-new-adopts new-roots)
4629
4630         (if (and (= gnus-tmp-level 0)
4631                  (or (not stack)
4632                      (= (caar stack) 0))
4633                  (not gnus-tmp-false-parent)
4634                  (or gnus-tmp-new-adopts new-roots))
4635             (if gnus-tmp-new-adopts
4636                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4637                       thread (list (car gnus-tmp-new-adopts))
4638                       gnus-tmp-header (caar thread)
4639                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4640               (when new-roots
4641                 (setq thread (list (car new-roots))
4642                       gnus-tmp-header (caar thread)
4643                       new-roots (cdr new-roots))))
4644
4645           (if threads
4646               ;; If there are some threads, we do them before the
4647               ;; threads on the stack.
4648               (setq thread threads
4649                     gnus-tmp-header (caar thread))
4650             ;; There were no current threads, so we pop something off
4651             ;; the stack.
4652             (setq state (car stack)
4653                   gnus-tmp-level (car state)
4654                   tree-stack (cadr state)
4655                   thread (caddr state)
4656                   stack (cdr stack)
4657                   gnus-tmp-header (caar thread))))
4658
4659         (setq gnus-tmp-false-parent nil)
4660         (setq gnus-tmp-root-expunged nil)
4661         (setq thread-end nil)
4662
4663         (if (stringp gnus-tmp-header)
4664             ;; The header is a dummy root.
4665             (cond
4666              ((eq gnus-summary-make-false-root 'adopt)
4667               ;; We let the first article adopt the rest.
4668               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4669                                                (cddar thread)))
4670               (setq gnus-tmp-gathered
4671                     (nconc (mapcar
4672                             (lambda (h) (mail-header-number (car h)))
4673                             (cddar thread))
4674                            gnus-tmp-gathered))
4675               (setq thread (cons (list (caar thread)
4676                                        (cadar thread))
4677                                  (cdr thread)))
4678               (setq gnus-tmp-level -1
4679                     gnus-tmp-false-parent t))
4680              ((eq gnus-summary-make-false-root 'empty)
4681               ;; We print adopted articles with empty subject fields.
4682               (setq gnus-tmp-gathered
4683                     (nconc (mapcar
4684                             (lambda (h) (mail-header-number (car h)))
4685                             (cddar thread))
4686                            gnus-tmp-gathered))
4687               (setq gnus-tmp-level -1))
4688              ((eq gnus-summary-make-false-root 'dummy)
4689               ;; We remember that we probably want to output a dummy
4690               ;; root.
4691               (setq gnus-tmp-dummy-line gnus-tmp-header)
4692               (setq gnus-tmp-prev-subject gnus-tmp-header))
4693              (t
4694               ;; We do not make a root for the gathered
4695               ;; sub-threads at all.
4696               (setq gnus-tmp-level -1)))
4697
4698           (setq number (mail-header-number gnus-tmp-header)
4699                 subject (mail-header-subject gnus-tmp-header)
4700                 simp-subject (gnus-simplify-subject-fully subject))
4701
4702           (cond
4703            ;; If the thread has changed subject, we might want to make
4704            ;; this subthread into a root.
4705            ((and (null gnus-thread-ignore-subject)
4706                  (not (zerop gnus-tmp-level))
4707                  gnus-tmp-prev-subject
4708                  (not (string= gnus-tmp-prev-subject simp-subject)))
4709             (setq new-roots (nconc new-roots (list (car thread)))
4710                   thread-end t
4711                   gnus-tmp-header nil))
4712            ;; If the article lies outside the current limit,
4713            ;; then we do not display it.
4714            ((not (memq number gnus-newsgroup-limit))
4715             (setq gnus-tmp-gathered
4716                   (nconc (mapcar
4717                           (lambda (h) (mail-header-number (car h)))
4718                           (cdar thread))
4719                          gnus-tmp-gathered))
4720             (setq gnus-tmp-new-adopts (if (cdar thread)
4721                                           (append gnus-tmp-new-adopts
4722                                                   (cdar thread))
4723                                         gnus-tmp-new-adopts)
4724                   thread-end t
4725                   gnus-tmp-header nil)
4726             (when (zerop gnus-tmp-level)
4727               (setq gnus-tmp-root-expunged t)))
4728            ;; Perhaps this article is to be marked as read?
4729            ((and gnus-summary-mark-below
4730                  (< (or (cdr (assq number gnus-newsgroup-scored))
4731                         default-score)
4732                     gnus-summary-mark-below)
4733                  ;; Don't touch sparse articles.
4734                  (not (gnus-summary-article-sparse-p number))
4735                  (not (gnus-summary-article-ancient-p number)))
4736             (setq gnus-newsgroup-unreads
4737                   (delq number gnus-newsgroup-unreads))
4738             (if gnus-newsgroup-auto-expire
4739                 (setq gnus-newsgroup-expirable
4740                       (gnus-add-to-sorted-list
4741                        gnus-newsgroup-expirable number))
4742               (push (cons number gnus-low-score-mark)
4743                     gnus-newsgroup-reads))))
4744
4745           (when gnus-tmp-header
4746             ;; We may have an old dummy line to output before this
4747             ;; article.
4748             (when (and gnus-tmp-dummy-line
4749                        (gnus-subject-equal
4750                         gnus-tmp-dummy-line
4751                         (mail-header-subject gnus-tmp-header)))
4752               (gnus-summary-insert-dummy-line
4753                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4754               (setq gnus-tmp-dummy-line nil))
4755
4756             ;; Compute the mark.
4757             (setq gnus-tmp-unread (gnus-article-mark number))
4758
4759             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4760                                   gnus-tmp-header gnus-tmp-level)
4761                   gnus-newsgroup-data)
4762
4763             ;; Actually insert the line.
4764             (setq
4765              gnus-tmp-subject-or-nil
4766              (cond
4767               ((and gnus-thread-ignore-subject
4768                     gnus-tmp-prev-subject
4769                     (not (string= gnus-tmp-prev-subject simp-subject)))
4770                subject)
4771               ((zerop gnus-tmp-level)
4772                (if (and (eq gnus-summary-make-false-root 'empty)
4773                         (memq number gnus-tmp-gathered)
4774                         gnus-tmp-prev-subject
4775                         (string= gnus-tmp-prev-subject simp-subject))
4776                    gnus-summary-same-subject
4777                  subject))
4778               (t gnus-summary-same-subject)))
4779             (if (and (eq gnus-summary-make-false-root 'adopt)
4780                      (= gnus-tmp-level 1)
4781                      (memq number gnus-tmp-gathered))
4782                 (setq gnus-tmp-opening-bracket ?\<
4783                       gnus-tmp-closing-bracket ?\>)
4784               (setq gnus-tmp-opening-bracket ?\[
4785                     gnus-tmp-closing-bracket ?\]))
4786             (setq
4787              gnus-tmp-indentation
4788              (aref gnus-thread-indent-array gnus-tmp-level)
4789              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4790              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4791                                 gnus-summary-default-score 0)
4792              gnus-tmp-score-char
4793              (if (or (null gnus-summary-default-score)
4794                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4795                          gnus-summary-zcore-fuzz))
4796                  ?                      ;Whitespace
4797                (if (< gnus-tmp-score gnus-summary-default-score)
4798                    gnus-score-below-mark gnus-score-over-mark))
4799              gnus-tmp-replied
4800              (cond ((memq number gnus-newsgroup-processable)
4801                     gnus-process-mark)
4802                    ((memq number gnus-newsgroup-cached)
4803                     gnus-cached-mark)
4804                    ((memq number gnus-newsgroup-replied)
4805                     gnus-replied-mark)
4806                    ((memq number gnus-newsgroup-forwarded)
4807                     gnus-forwarded-mark)
4808                    ((memq number gnus-newsgroup-saved)
4809                     gnus-saved-mark)
4810                    ((memq number gnus-newsgroup-recent)
4811                     gnus-recent-mark)
4812                    ((memq number gnus-newsgroup-unseen)
4813                     gnus-unseen-mark)
4814                    (t gnus-no-mark))
4815              gnus-tmp-downloaded
4816              (cond ((memq number gnus-newsgroup-undownloaded) 
4817                     gnus-undownloaded-mark)
4818                    (gnus-newsgroup-agentized
4819                     gnus-downloaded-mark)
4820                    (t
4821                     gnus-no-mark))
4822              gnus-tmp-from (mail-header-from gnus-tmp-header)
4823              gnus-tmp-name
4824              (cond
4825               ((string-match "<[^>]+> *$" gnus-tmp-from)
4826                (setq beg-match (match-beginning 0))
4827                (or (and (string-match "^\".+\"" gnus-tmp-from)
4828                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4829                    (substring gnus-tmp-from 0 beg-match)))
4830               ((string-match "(.+)" gnus-tmp-from)
4831                (substring gnus-tmp-from
4832                           (1+ (match-beginning 0)) (1- (match-end 0))))
4833               (t gnus-tmp-from))
4834
4835              ;; Do the %B string
4836              gnus-tmp-thread-tree-header-string
4837              (cond
4838               ((not gnus-show-threads) "")
4839               ((zerop gnus-tmp-level)
4840                (if (cdar thread)
4841                    (or gnus-sum-thread-tree-root subject)
4842                  (or gnus-sum-thread-tree-single-indent subject)))
4843               (t
4844                (concat (apply 'concat
4845                               (mapcar (lambda (item)
4846                                         (if (= item 1)
4847                                             gnus-sum-thread-tree-vertical
4848                                           gnus-sum-thread-tree-indent))
4849                                       (cdr (reverse tree-stack))))
4850                        (if (nth 1 thread)
4851                            gnus-sum-thread-tree-leaf-with-other
4852                          gnus-sum-thread-tree-single-leaf)))))
4853             (when (string= gnus-tmp-name "")
4854               (setq gnus-tmp-name gnus-tmp-from))
4855             (unless (numberp gnus-tmp-lines)
4856               (setq gnus-tmp-lines -1))
4857             (if (= gnus-tmp-lines -1)
4858                 (setq gnus-tmp-lines "?")
4859               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4860               (gnus-put-text-property
4861              (point)
4862              (progn (eval gnus-summary-line-format-spec) (point))
4863                'gnus-number number)
4864             (when gnus-visual-p
4865               (forward-line -1)
4866               (gnus-run-hooks 'gnus-summary-update-hook)
4867               (forward-line 1))
4868
4869             (setq gnus-tmp-prev-subject simp-subject)))
4870
4871         (when (nth 1 thread)
4872           (push (list (max 0 gnus-tmp-level)
4873                       (copy-list tree-stack)
4874                       (nthcdr 1 thread))
4875                 stack))
4876         (push (if (nth 1 thread) 1 0) tree-stack)
4877         (incf gnus-tmp-level)
4878         (setq threads (if thread-end nil (cdar thread)))
4879         (if gnus-summary-display-while-building
4880             (if building-count
4881                 (progn
4882                   ;; use a set frequency
4883                   (setq building-line-count (1- building-line-count))
4884                   (when (= building-line-count 0)
4885                     (sit-for 0)
4886                     (setq building-line-count
4887                           gnus-summary-display-while-building)))
4888               ;; always
4889               (sit-for 0)))
4890         (unless threads
4891           (setq gnus-tmp-level 0)))))
4892   (gnus-message 7 "Generating summary...done"))
4893
4894 (defun gnus-summary-prepare-unthreaded (headers)
4895   "Generate an unthreaded summary buffer based on HEADERS."
4896   (let (header number mark)
4897
4898     (beginning-of-line)
4899
4900     (while headers
4901       ;; We may have to root out some bad articles...
4902       (when (memq (setq number (mail-header-number
4903                                 (setq header (pop headers))))
4904                   gnus-newsgroup-limit)
4905         ;; Mark article as read when it has a low score.
4906         (when (and gnus-summary-mark-below
4907                    (< (or (cdr (assq number gnus-newsgroup-scored))
4908                           gnus-summary-default-score 0)
4909                       gnus-summary-mark-below)
4910                    (not (gnus-summary-article-ancient-p number)))
4911           (setq gnus-newsgroup-unreads
4912                 (delq number gnus-newsgroup-unreads))
4913           (if gnus-newsgroup-auto-expire
4914               (push number gnus-newsgroup-expirable)
4915             (push (cons number gnus-low-score-mark)
4916                   gnus-newsgroup-reads)))
4917
4918         (setq mark (gnus-article-mark number))
4919         (push (gnus-data-make number mark (1+ (point)) header 0)
4920               gnus-newsgroup-data)
4921         (gnus-summary-insert-line
4922          header 0 number
4923          (memq number gnus-newsgroup-undownloaded)
4924          mark (memq number gnus-newsgroup-replied)
4925          (memq number gnus-newsgroup-expirable)
4926          (mail-header-subject header) nil
4927          (cdr (assq number gnus-newsgroup-scored))
4928          (memq number gnus-newsgroup-processable))))))
4929
4930 (defun gnus-summary-remove-list-identifiers ()
4931   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4932   (let ((regexp (if (consp gnus-list-identifiers)
4933                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4934                   gnus-list-identifiers))
4935         changed subject)
4936     (when regexp
4937       (dolist (header gnus-newsgroup-headers)
4938         (setq subject (mail-header-subject header)
4939               changed nil)
4940         (while (string-match
4941                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4942                 subject)
4943           (setq subject
4944                 (concat (substring subject 0 (match-beginning 2))
4945                         (substring subject (match-end 0)))
4946                 changed t))
4947         (when (and changed
4948                    (string-match
4949                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4950           (setq subject
4951                 (concat (substring subject 0 (match-beginning 1))
4952                         (substring subject (match-end 1)))))
4953         (when changed
4954           (mail-header-set-subject header subject))))))
4955
4956 (defun gnus-fetch-headers (articles)
4957   "Fetch headers of ARTICLES."
4958   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4959     (gnus-message 5 "Fetching headers for %s..." name)
4960     (prog1
4961         (if (eq 'nov
4962                 (setq gnus-headers-retrieved-by
4963                       (gnus-retrieve-headers
4964                        articles gnus-newsgroup-name
4965                        ;; We might want to fetch old headers, but
4966                        ;; not if there is only 1 article.
4967                        (and (or (and
4968                                  (not (eq gnus-fetch-old-headers 'some))
4969                                  (not (numberp gnus-fetch-old-headers)))
4970                                 (> (length articles) 1))
4971                             gnus-fetch-old-headers))))
4972             (gnus-get-newsgroup-headers-xover
4973              articles nil nil gnus-newsgroup-name t)
4974           (gnus-get-newsgroup-headers))
4975       (gnus-message 5 "Fetching headers for %s...done" name))))
4976
4977 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4978   "Select newsgroup GROUP.
4979 If READ-ALL is non-nil, all articles in the group are selected.
4980 If SELECT-ARTICLES, only select those articles from GROUP."
4981   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4982          ;;!!! Dirty hack; should be removed.
4983          (gnus-summary-ignore-duplicates
4984           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4985               t
4986             gnus-summary-ignore-duplicates))
4987          (info (nth 2 entry))
4988          articles fetched-articles cached)
4989
4990     (unless (gnus-check-server
4991              (set (make-local-variable 'gnus-current-select-method)
4992                   (gnus-find-method-for-group group)))
4993       (error "Couldn't open server"))
4994
4995     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4996         (gnus-activate-group group)     ; Or we can activate it...
4997         (progn                          ; Or we bug out.
4998           (when (equal major-mode 'gnus-summary-mode)
4999             (gnus-kill-buffer (current-buffer)))
5000           (error "Couldn't activate group %s: %s"
5001                  group (gnus-status-message group))))
5002
5003     (unless (gnus-request-group group t)
5004       (when (equal major-mode 'gnus-summary-mode)
5005         (gnus-kill-buffer (current-buffer)))
5006       (error "Couldn't request group %s: %s"
5007              group (gnus-status-message group)))
5008
5009     (when gnus-agent
5010       ;; The agent may be storing articles that are no longer in the
5011       ;; server's active range.  If that is the case, the active range
5012       ;; needs to be expanded such that the agent's articles can be
5013       ;; included in the summary.
5014       (let* ((gnus-command-method (gnus-find-method-for-group group))
5015              (alist (gnus-agent-load-alist group))
5016              (active (gnus-active group)))
5017         (if (and (car alist)
5018                  (< (caar alist) (car active)))
5019             (gnus-set-active group (cons (caar alist) (cdr active))))))
5020
5021     (setq gnus-newsgroup-name group
5022           gnus-newsgroup-unselected nil
5023           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5024
5025     (let ((display (gnus-group-find-parameter group 'display)))
5026       (setq gnus-newsgroup-display
5027             (cond
5028              ((not (zerop (or (car-safe read-all) 0)))
5029               ;; The user entered the group with C-u SPC/RET, let's show
5030               ;; all articles.
5031               'gnus-not-ignore)
5032              ((eq display 'all)
5033               'gnus-not-ignore)
5034              ((arrayp display)
5035               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5036              ((numberp display)
5037               ;; The following is probably the "correct" solution, but
5038               ;; it makes Gnus fetch all headers and then limit the
5039               ;; articles (which is slow), so instead we hack the
5040               ;; select-articles parameter instead. -- Simon Josefsson
5041               ;; <jas@kth.se>
5042               ;;
5043               ;; (gnus-byte-compile
5044               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5045               ;;                         display)))))
5046               (setq select-articles
5047                     (gnus-uncompress-range
5048                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5049                              (if (> tmp 0)
5050                                  tmp
5051                                1))
5052                            (cdr (gnus-active group)))))
5053               nil)
5054              (t
5055               nil))))
5056
5057     (gnus-summary-setup-default-charset)
5058
5059     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5060     (when (gnus-virtual-group-p group)
5061       (setq cached gnus-newsgroup-cached))
5062
5063     (setq gnus-newsgroup-unreads
5064           (gnus-sorted-ndifference
5065            (gnus-sorted-ndifference gnus-newsgroup-unreads
5066                                     gnus-newsgroup-marked)
5067            gnus-newsgroup-dormant))
5068
5069     (setq gnus-newsgroup-processable nil)
5070
5071     (gnus-update-read-articles group gnus-newsgroup-unreads)
5072
5073     ;; Adjust and set lists of article marks.
5074     (when info
5075       (gnus-adjust-marked-articles info))
5076     (if (setq articles select-articles)
5077         (setq gnus-newsgroup-unselected
5078               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5079       (setq articles (gnus-articles-to-read group read-all)))
5080
5081     (cond
5082      ((null articles)
5083       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5084       'quit)
5085      ((eq articles 0) nil)
5086      (t
5087       ;; Init the dependencies hash table.
5088       (setq gnus-newsgroup-dependencies
5089             (gnus-make-hashtable (length articles)))
5090       (gnus-set-global-variables)
5091       ;; Retrieve the headers and read them in.
5092
5093       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5094
5095       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5096       (when cached
5097         (setq gnus-newsgroup-cached cached))
5098
5099       ;; Suppress duplicates?
5100       (when gnus-suppress-duplicates
5101         (gnus-dup-suppress-articles))
5102
5103       ;; Set the initial limit.
5104       (setq gnus-newsgroup-limit (copy-sequence articles))
5105       ;; Remove canceled articles from the list of unread articles.
5106       (setq fetched-articles
5107             (mapcar (lambda (headers) (mail-header-number headers))
5108                     gnus-newsgroup-headers))
5109       (setq gnus-newsgroup-articles fetched-articles)
5110       (setq gnus-newsgroup-unreads
5111             (gnus-sorted-nintersection
5112              gnus-newsgroup-unreads fetched-articles))
5113       (gnus-compute-unseen-list)
5114
5115       ;; Removed marked articles that do not exist.
5116       (gnus-update-missing-marks
5117        (gnus-sorted-difference articles fetched-articles))
5118       ;; We might want to build some more threads first.
5119       (when (and gnus-fetch-old-headers
5120                  (eq gnus-headers-retrieved-by 'nov))
5121         (if (eq gnus-fetch-old-headers 'invisible)
5122             (gnus-build-all-threads)
5123           (gnus-build-old-threads)))
5124       ;; Let the Gnus agent mark articles as read.
5125       (when gnus-agent
5126         (gnus-agent-get-undownloaded-list))
5127       ;; Remove list identifiers from subject
5128       (when gnus-list-identifiers
5129         (gnus-summary-remove-list-identifiers))
5130       ;; Check whether auto-expire is to be done in this group.
5131       (setq gnus-newsgroup-auto-expire
5132             (gnus-group-auto-expirable-p group))
5133       ;; Set up the article buffer now, if necessary.
5134       (unless gnus-single-article-buffer
5135         (gnus-article-setup-buffer))
5136       ;; First and last article in this newsgroup.
5137       (when gnus-newsgroup-headers
5138         (setq gnus-newsgroup-begin
5139               (mail-header-number (car gnus-newsgroup-headers))
5140               gnus-newsgroup-end
5141               (mail-header-number
5142                (gnus-last-element gnus-newsgroup-headers))))
5143       ;; GROUP is successfully selected.
5144       (or gnus-newsgroup-headers t)))))
5145
5146 (defun gnus-compute-unseen-list ()
5147   ;; The `seen' marks are treated specially.
5148   (if (not gnus-newsgroup-seen)
5149       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5150     (setq gnus-newsgroup-unseen
5151           (gnus-inverse-list-range-intersection
5152            gnus-newsgroup-articles gnus-newsgroup-seen))))
5153
5154 (defun gnus-summary-display-make-predicate (display)
5155   (require 'gnus-agent)
5156   (when (= (length display) 1)
5157     (setq display (car display)))
5158   (unless gnus-summary-display-cache
5159     (dolist (elem (append '((unread . unread)
5160                             (read . read)
5161                             (unseen . unseen))
5162                           gnus-article-mark-lists))
5163       (push (cons (cdr elem)
5164                   (gnus-byte-compile
5165                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5166             gnus-summary-display-cache)))
5167   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5168         (gnus-category-predicate-cache gnus-summary-display-cache))
5169     (gnus-get-predicate display)))
5170
5171 ;; Uses the dynamically bound `number' variable.
5172 (eval-when-compile
5173   (defvar number))
5174 (defun gnus-article-marked-p (type &optional article)
5175   (let ((article (or article number)))
5176     (cond
5177      ((eq type 'tick)
5178       (memq article gnus-newsgroup-marked))
5179      ((eq type 'spam)
5180       (memq article gnus-newsgroup-spam-marked))
5181      ((eq type 'unsend)
5182       (memq article gnus-newsgroup-unsendable))
5183      ((eq type 'undownload)
5184       (memq article gnus-newsgroup-undownloaded))
5185      ((eq type 'download)
5186       (memq article gnus-newsgroup-downloadable))
5187      ((eq type 'unread)
5188       (memq article gnus-newsgroup-unreads))
5189      ((eq type 'read)
5190       (memq article gnus-newsgroup-reads))
5191      ((eq type 'dormant)
5192       (memq article gnus-newsgroup-dormant) )
5193      ((eq type 'expire)
5194       (memq article gnus-newsgroup-expirable))
5195      ((eq type 'reply)
5196       (memq article gnus-newsgroup-replied))
5197      ((eq type 'killed)
5198       (memq article gnus-newsgroup-killed))
5199      ((eq type 'bookmark)
5200       (assq article gnus-newsgroup-bookmarks))
5201      ((eq type 'score)
5202       (assq article gnus-newsgroup-scored))
5203      ((eq type 'save)
5204       (memq article gnus-newsgroup-saved))
5205      ((eq type 'cache)
5206       (memq article gnus-newsgroup-cached))
5207      ((eq type 'forward)
5208       (memq article gnus-newsgroup-forwarded))
5209      ((eq type 'seen)
5210       (not (memq article gnus-newsgroup-unseen)))
5211      ((eq type 'recent)
5212       (memq article gnus-newsgroup-recent))
5213      (t t))))
5214
5215 (defun gnus-articles-to-read (group &optional read-all)
5216   "Find out what articles the user wants to read."
5217   (let* ((display (gnus-group-find-parameter group 'display))
5218          (articles
5219           ;; Select all articles if `read-all' is non-nil, or if there
5220           ;; are no unread articles.
5221           (if (or read-all
5222                   (and (zerop (length gnus-newsgroup-marked))
5223                        (zerop (length gnus-newsgroup-unreads)))
5224                   ;; Fetch all if the predicate is non-nil.
5225                   gnus-newsgroup-display)
5226               ;; We want to select the headers for all the articles in
5227               ;; the group, so we select either all the active
5228               ;; articles in the group, or (if that's nil), the
5229               ;; articles in the cache.
5230               (or
5231                (gnus-uncompress-range (gnus-active group))
5232                (gnus-cache-articles-in-group group))
5233             ;; Select only the "normal" subset of articles.
5234             (gnus-sorted-nunion
5235              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5236              gnus-newsgroup-unreads)))
5237          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5238          (scored (length scored-list))
5239          (number (length articles))
5240          (marked (+ (length gnus-newsgroup-marked)
5241                     (length gnus-newsgroup-dormant)))
5242          (select
5243           (cond
5244            ((numberp read-all)
5245             read-all)
5246            ((numberp gnus-newsgroup-display)
5247             gnus-newsgroup-display)
5248            (t
5249             (condition-case ()
5250                 (cond
5251                  ((and (or (<= scored marked) (= scored number))
5252                        (numberp gnus-large-newsgroup)
5253                        (> number gnus-large-newsgroup))
5254                   (let* ((cursor-in-echo-area nil)
5255                          (initial (gnus-parameter-large-newsgroup-initial
5256                                    gnus-newsgroup-name))
5257                          (input
5258                           (read-string
5259                            (format
5260                             "How many articles from %s (%s %d): "
5261                             (gnus-limit-string
5262                              (gnus-group-decoded-name gnus-newsgroup-name)
5263                              35)
5264                             (if initial "max" "default")
5265                             number)
5266                            (if initial
5267                                (cons (number-to-string initial)
5268                                      0)))))
5269                     (if (string-match "^[ \t]*$" input) number input)))
5270                  ((and (> scored marked) (< scored number)
5271                        (> (- scored number) 20))
5272                   (let ((input
5273                          (read-string
5274                           (format "%s %s (%d scored, %d total): "
5275                                   "How many articles from"
5276                                   (gnus-group-decoded-name group)
5277                                   scored number))))
5278                     (if (string-match "^[ \t]*$" input)
5279                         number input)))
5280                  (t number))
5281               (quit
5282                (message "Quit getting the articles to read")
5283                nil))))))
5284     (setq select (if (stringp select) (string-to-number select) select))
5285     (if (or (null select) (zerop select))
5286         select
5287       (if (and (not (zerop scored)) (<= (abs select) scored))
5288           (progn
5289             (setq articles (sort scored-list '<))
5290             (setq number (length articles)))
5291         (setq articles (copy-sequence articles)))
5292
5293       (when (< (abs select) number)
5294         (if (< select 0)
5295             ;; Select the N oldest articles.
5296             (setcdr (nthcdr (1- (abs select)) articles) nil)
5297           ;; Select the N most recent articles.
5298           (setq articles (nthcdr (- number select) articles))))
5299       (setq gnus-newsgroup-unselected
5300             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5301       (when gnus-alter-articles-to-read-function
5302         (setq articles
5303               (sort
5304                (funcall gnus-alter-articles-to-read-function
5305                         gnus-newsgroup-name articles)
5306                '<)))
5307       articles)))
5308
5309 (defun gnus-killed-articles (killed articles)
5310   (let (out)
5311     (while articles
5312       (when (inline (gnus-member-of-range (car articles) killed))
5313         (push (car articles) out))
5314       (setq articles (cdr articles)))
5315     out))
5316
5317 (defun gnus-uncompress-marks (marks)
5318   "Uncompress the mark ranges in MARKS."
5319   (let ((uncompressed '(score bookmark))
5320         out)
5321     (while marks
5322       (if (memq (caar marks) uncompressed)
5323           (push (car marks) out)
5324         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5325       (setq marks (cdr marks)))
5326     out))
5327
5328 (defun gnus-article-mark-to-type (mark)
5329   "Return the type of MARK."
5330   (or (cadr (assq mark gnus-article-special-mark-lists))
5331       'list))
5332
5333 (defun gnus-article-unpropagatable-p (mark)
5334   "Return whether MARK should be propagated to backend."
5335   (memq mark gnus-article-unpropagated-mark-lists))
5336
5337 (defun gnus-adjust-marked-articles (info)
5338   "Set all article lists and remove all marks that are no longer valid."
5339   (let* ((marked-lists (gnus-info-marks info))
5340          (active (gnus-active (gnus-info-group info)))
5341          (min (car active))
5342          (max (cdr active))
5343          (types gnus-article-mark-lists)
5344          marks var articles article mark mark-type)
5345
5346     (dolist (marks marked-lists)
5347       (setq mark (car marks)
5348             mark-type (gnus-article-mark-to-type mark)
5349             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5350
5351       ;; We set the variable according to the type of the marks list,
5352       ;; and then adjust the marks to a subset of the active articles.
5353       (cond
5354        ;; Adjust "simple" lists.
5355        ((eq mark-type 'list)
5356         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5357         (when (memq mark '(tick dormant expire reply save))
5358           (while articles
5359             (when (or (< (setq article (pop articles)) min) (> article max))
5360               (set var (delq article (symbol-value var)))))))
5361        ;; Adjust assocs.
5362        ((eq mark-type 'tuple)
5363         (set var (setq articles (cdr marks)))
5364         (when (not (listp (cdr (symbol-value var))))
5365           (set var (list (symbol-value var))))
5366         (when (not (listp (cdr articles)))
5367           (setq articles (list articles)))
5368         (while articles
5369           (when (or (not (consp (setq article (pop articles))))
5370                     (< (car article) min)
5371                     (> (car article) max))
5372             (set var (delq article (symbol-value var))))))
5373        ;; Adjust ranges (sloppily).
5374        ((eq mark-type 'range)
5375         (cond
5376          ((eq mark 'seen)
5377           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5378           ;; It should be (seen (NUM1 . NUM2)).
5379           (when (numberp (cddr marks))
5380             (setcdr marks (list (cdr marks))))
5381           (setq articles (cdr marks))
5382           (while (and articles
5383                       (or (and (consp (car articles))
5384                                (> min (cdar articles)))
5385                           (and (numberp (car articles))
5386                                (> min (car articles)))))
5387             (pop articles))
5388           (set var articles))))))))
5389
5390 (defun gnus-update-missing-marks (missing)
5391   "Go through the list of MISSING articles and remove them from the mark lists."
5392   (when missing
5393     (let (var m)
5394       ;; Go through all types.
5395       (dolist (elem gnus-article-mark-lists)
5396         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5397           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5398           (when (symbol-value var)
5399             ;; This list has articles.  So we delete all missing
5400             ;; articles from it.
5401             (setq m missing)
5402             (while m
5403               (set var (delq (pop m) (symbol-value var))))))))))
5404
5405 (defun gnus-update-marks ()
5406   "Enter the various lists of marked articles into the newsgroup info list."
5407   (let ((types gnus-article-mark-lists)
5408         (info (gnus-get-info gnus-newsgroup-name))
5409         type list newmarked symbol delta-marks)
5410     (when info
5411       ;; Add all marks lists to the list of marks lists.
5412       (while (setq type (pop types))
5413         (setq list (symbol-value
5414                     (setq symbol
5415                           (intern (format "gnus-newsgroup-%s" (car type))))))
5416
5417         (when list
5418           ;; Get rid of the entries of the articles that have the
5419           ;; default score.
5420           (when (and (eq (cdr type) 'score)
5421                      gnus-save-score
5422                      list)
5423             (let* ((arts list)
5424                    (prev (cons nil list))
5425                    (all prev))
5426               (while arts
5427                 (if (or (not (consp (car arts)))
5428                         (= (cdar arts) gnus-summary-default-score))
5429                     (setcdr prev (cdr arts))
5430                   (setq prev arts))
5431                 (setq arts (cdr arts)))
5432               (setq list (cdr all)))))
5433
5434         (when (eq (cdr type) 'seen)
5435           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5436
5437         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5438           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5439
5440         (when (and (gnus-check-backend-function
5441                     'request-set-mark gnus-newsgroup-name)
5442                    (not (gnus-article-unpropagatable-p (cdr type))))
5443           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5444                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5445                  (add (gnus-remove-from-range
5446                        (gnus-copy-sequence list) old)))
5447             (when add
5448               (push (list add 'add (list (cdr type))) delta-marks))
5449             (when del
5450               (push (list del 'del (list (cdr type))) delta-marks))))
5451
5452         (when list
5453           (push (cons (cdr type) list) newmarked)))
5454
5455       (when delta-marks
5456         (unless (gnus-check-group gnus-newsgroup-name)
5457           (error "Can't open server for %s" gnus-newsgroup-name))
5458         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5459
5460       ;; Enter these new marks into the info of the group.
5461       (if (nthcdr 3 info)
5462           (setcar (nthcdr 3 info) newmarked)
5463         ;; Add the marks lists to the end of the info.
5464         (when newmarked
5465           (setcdr (nthcdr 2 info) (list newmarked))))
5466
5467       ;; Cut off the end of the info if there's nothing else there.
5468       (let ((i 5))
5469         (while (and (> i 2)
5470                     (not (nth i info)))
5471           (when (nthcdr (decf i) info)
5472             (setcdr (nthcdr i info) nil)))))))
5473
5474 (defun gnus-set-mode-line (where)
5475   "Set the mode line of the article or summary buffers.
5476 If WHERE is `summary', the summary mode line format will be used."
5477   ;; Is this mode line one we keep updated?
5478   (when (and (memq where gnus-updated-mode-lines)
5479              (symbol-value
5480               (intern (format "gnus-%s-mode-line-format-spec" where))))
5481     (let (mode-string)
5482       (save-excursion
5483         ;; We evaluate this in the summary buffer since these
5484         ;; variables are buffer-local to that buffer.
5485         (set-buffer gnus-summary-buffer)
5486        ;; We bind all these variables that are used in the `eval' form
5487         ;; below.
5488         (let* ((mformat (symbol-value
5489                          (intern
5490                           (format "gnus-%s-mode-line-format-spec" where))))
5491                (gnus-tmp-group-name (gnus-group-decoded-name
5492                                      gnus-newsgroup-name))
5493                (gnus-tmp-article-number (or gnus-current-article 0))
5494                (gnus-tmp-unread gnus-newsgroup-unreads)
5495                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5496                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5497                (gnus-tmp-unread-and-unselected
5498                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5499                             (zerop gnus-tmp-unselected))
5500                        "")
5501                       ((zerop gnus-tmp-unselected)
5502                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5503                       (t (format "{%d(+%d) more}"
5504                                  gnus-tmp-unread-and-unticked
5505                                  gnus-tmp-unselected))))
5506                (gnus-tmp-subject
5507                 (if (and gnus-current-headers
5508                          (vectorp gnus-current-headers))
5509                     (gnus-mode-string-quote
5510                      (mail-header-subject gnus-current-headers))
5511                   ""))
5512                bufname-length max-len
5513                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5514           (setq mode-string (eval mformat))
5515           (setq bufname-length (if (string-match "%b" mode-string)
5516                                    (- (length
5517                                        (buffer-name
5518                                         (if (eq where 'summary)
5519                                             nil
5520                                           (get-buffer gnus-article-buffer))))
5521                                       2)
5522                                  0))
5523           (setq max-len (max 4 (if gnus-mode-non-string-length
5524                                    (- (window-width)
5525                                       gnus-mode-non-string-length
5526                                       bufname-length)
5527                                  (length mode-string))))
5528           ;; We might have to chop a bit of the string off...
5529           (when (> (length mode-string) max-len)
5530             (setq mode-string
5531                   (concat (truncate-string-to-width mode-string (- max-len 3))
5532                           "...")))
5533           ;; Pad the mode string a bit.
5534           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5535       ;; Update the mode line.
5536       (setq mode-line-buffer-identification
5537             (gnus-mode-line-buffer-identification (list mode-string)))
5538       (set-buffer-modified-p t))))
5539
5540 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5541   "Go through the HEADERS list and add all Xrefs to a hash table.
5542 The resulting hash table is returned, or nil if no Xrefs were found."
5543   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5544          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5545          (xref-hashtb (gnus-make-hashtable))
5546          start group entry number xrefs header)
5547     (while headers
5548       (setq header (pop headers))
5549       (when (and (setq xrefs (mail-header-xref header))
5550                  (not (memq (setq number (mail-header-number header))
5551                             unreads)))
5552         (setq start 0)
5553         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5554           (setq start (match-end 0))
5555           (setq group (if prefix
5556                           (concat prefix (substring xrefs (match-beginning 1)
5557                                                     (match-end 1)))
5558                         (substring xrefs (match-beginning 1) (match-end 1))))
5559           (setq number
5560                 (string-to-int (substring xrefs (match-beginning 2)
5561                                           (match-end 2))))
5562           (if (setq entry (gnus-gethash group xref-hashtb))
5563               (setcdr entry (cons number (cdr entry)))
5564             (gnus-sethash group (cons number nil) xref-hashtb)))))
5565     (and start xref-hashtb)))
5566
5567 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5568   "Look through all the headers and mark the Xrefs as read."
5569   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5570         name entry info xref-hashtb idlist method nth4)
5571     (save-excursion
5572       (set-buffer gnus-group-buffer)
5573       (when (setq xref-hashtb
5574                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5575         (mapatoms
5576          (lambda (group)
5577            (unless (string= from-newsgroup (setq name (symbol-name group)))
5578              (setq idlist (symbol-value group))
5579              ;; Dead groups are not updated.
5580              (and (prog1
5581                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5582                             info (nth 2 entry))
5583                     (when (stringp (setq nth4 (gnus-info-method info)))
5584                       (setq nth4 (gnus-server-to-method nth4))))
5585                   ;; Only do the xrefs if the group has the same
5586                   ;; select method as the group we have just read.
5587                   (or (gnus-methods-equal-p
5588                        nth4 (gnus-find-method-for-group from-newsgroup))
5589                       virtual
5590                       (equal nth4 (setq method (gnus-find-method-for-group
5591                                                 from-newsgroup)))
5592                       (and (equal (car nth4) (car method))
5593                            (equal (nth 1 nth4) (nth 1 method))))
5594                   gnus-use-cross-reference
5595                   (or (not (eq gnus-use-cross-reference t))
5596                       virtual
5597                       ;; Only do cross-references on subscribed
5598                       ;; groups, if that is what is wanted.
5599                       (<= (gnus-info-level info) gnus-level-subscribed))
5600                   (gnus-group-make-articles-read name idlist))))
5601          xref-hashtb)))))
5602
5603 (defun gnus-compute-read-articles (group articles)
5604   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5605          (info (nth 2 entry))
5606          (active (gnus-active group))
5607          ninfo)
5608     (when entry
5609       ;; First peel off all invalid article numbers.
5610       (when active
5611         (let ((ids articles)
5612               id first)
5613           (while (setq id (pop ids))
5614             (when (and first (> id (cdr active)))
5615               ;; We'll end up in this situation in one particular
5616               ;; obscure situation.  If you re-scan a group and get
5617               ;; a new article that is cross-posted to a different
5618               ;; group that has not been re-scanned, you might get
5619               ;; crossposted article that has a higher number than
5620               ;; Gnus believes possible.  So we re-activate this
5621               ;; group as well.  This might mean doing the
5622               ;; crossposting thingy will *increase* the number
5623               ;; of articles in some groups.  Tsk, tsk.
5624               (setq active (or (gnus-activate-group group) active)))
5625             (when (or (> id (cdr active))
5626                       (< id (car active)))
5627               (setq articles (delq id articles))))))
5628       ;; If the read list is nil, we init it.
5629       (if (and active
5630                (null (gnus-info-read info))
5631                (> (car active) 1))
5632           (setq ninfo (cons 1 (1- (car active))))
5633         (setq ninfo (gnus-info-read info)))
5634       ;; Then we add the read articles to the range.
5635       (gnus-add-to-range
5636        ninfo (setq articles (sort articles '<))))))
5637
5638 (defun gnus-group-make-articles-read (group articles)
5639   "Update the info of GROUP to say that ARTICLES are read."
5640   (let* ((num 0)
5641          (entry (gnus-gethash group gnus-newsrc-hashtb))
5642          (info (nth 2 entry))
5643          (active (gnus-active group))
5644          range)
5645     (when entry
5646       (setq range (gnus-compute-read-articles group articles))
5647       (save-excursion
5648         (set-buffer gnus-group-buffer)
5649         (gnus-undo-register
5650           `(progn
5651              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5652              (gnus-info-set-read ',info ',(gnus-info-read info))
5653              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5654              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5655              (gnus-group-update-group ,group t))))
5656       ;; Add the read articles to the range.
5657       (gnus-info-set-read info range)
5658       (gnus-request-set-mark group (list (list range 'add '(read))))
5659       ;; Then we have to re-compute how many unread
5660       ;; articles there are in this group.
5661       (when active
5662         (cond
5663          ((not range)
5664           (setq num (- (1+ (cdr active)) (car active))))
5665          ((not (listp (cdr range)))
5666           (setq num (- (cdr active) (- (1+ (cdr range))
5667                                        (car range)))))
5668          (t
5669           (while range
5670             (if (numberp (car range))
5671                 (setq num (1+ num))
5672               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5673             (setq range (cdr range)))
5674           (setq num (- (cdr active) num))))
5675         ;; Update the number of unread articles.
5676         (setcar entry num)
5677         ;; Update the group buffer.
5678         (unless (gnus-ephemeral-group-p group)
5679           (gnus-group-update-group group t))))))
5680
5681 (defvar gnus-newsgroup-none-id 0)
5682
5683 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5684   (let ((cur nntp-server-buffer)
5685         (dependencies
5686          (or dependencies
5687              (save-excursion (set-buffer gnus-summary-buffer)
5688                              gnus-newsgroup-dependencies)))
5689         headers id end ref
5690         (mail-parse-charset gnus-newsgroup-charset)
5691         (mail-parse-ignored-charsets
5692          (save-excursion (condition-case nil
5693                              (set-buffer gnus-summary-buffer)
5694                            (error))
5695                          gnus-newsgroup-ignored-charsets)))
5696     (save-excursion
5697       (set-buffer nntp-server-buffer)
5698       ;; Translate all TAB characters into SPACE characters.
5699       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5700       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5701       (gnus-run-hooks 'gnus-parse-headers-hook)
5702       (let ((case-fold-search t)
5703             in-reply-to header p lines chars)
5704         (goto-char (point-min))
5705         ;; Search to the beginning of the next header.  Error messages
5706         ;; do not begin with 2 or 3.
5707         (while (re-search-forward "^[23][0-9]+ " nil t)
5708           (setq id nil
5709                 ref nil)
5710           ;; This implementation of this function, with nine
5711           ;; search-forwards instead of the one re-search-forward and
5712           ;; a case (which basically was the old function) is actually
5713           ;; about twice as fast, even though it looks messier.  You
5714           ;; can't have everything, I guess.  Speed and elegance
5715           ;; doesn't always go hand in hand.
5716           (setq
5717            header
5718            (vector
5719             ;; Number.
5720             (prog1
5721                 (read cur)
5722               (end-of-line)
5723               (setq p (point))
5724               (narrow-to-region (point)
5725                                 (or (and (search-forward "\n.\n" nil t)
5726                                          (- (point) 2))
5727                                     (point))))
5728             ;; Subject.
5729             (progn
5730               (goto-char p)
5731               (if (search-forward "\nsubject:" nil t)
5732                   (funcall gnus-decode-encoded-word-function
5733                            (nnheader-header-value))
5734                 "(none)"))
5735             ;; From.
5736             (progn
5737               (goto-char p)
5738               (if (search-forward "\nfrom:" nil t)
5739                   (funcall gnus-decode-encoded-word-function
5740                            (nnheader-header-value))
5741                 "(nobody)"))
5742             ;; Date.
5743             (progn
5744               (goto-char p)
5745               (if (search-forward "\ndate:" nil t)
5746                   (nnheader-header-value) ""))
5747             ;; Message-ID.
5748             (progn
5749               (goto-char p)
5750               (setq id (if (re-search-forward
5751                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5752                            ;; We do it this way to make sure the Message-ID
5753                            ;; is (somewhat) syntactically valid.
5754                            (buffer-substring (match-beginning 1)
5755                                              (match-end 1))
5756                          ;; If there was no message-id, we just fake one
5757                          ;; to make subsequent routines simpler.
5758                          (nnheader-generate-fake-message-id))))
5759             ;; References.
5760             (progn
5761               (goto-char p)
5762               (if (search-forward "\nreferences:" nil t)
5763                   (progn
5764                     (setq end (point))
5765                     (prog1
5766                         (nnheader-header-value)
5767                       (setq ref
5768                             (buffer-substring
5769                              (progn
5770                                (end-of-line)
5771                                (search-backward ">" end t)
5772                                (1+ (point)))
5773                              (progn
5774                                (search-backward "<" end t)
5775                                (point))))))
5776                 ;; Get the references from the in-reply-to header if there
5777                 ;; were no references and the in-reply-to header looks
5778                 ;; promising.
5779                 (if (and (search-forward "\nin-reply-to:" nil t)
5780                          (setq in-reply-to (nnheader-header-value))
5781                          (string-match "<[^>]+>" in-reply-to))
5782                     (let (ref2)
5783                       (setq ref (substring in-reply-to (match-beginning 0)
5784                                            (match-end 0)))
5785                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5786                         (setq ref2 (substring in-reply-to (match-beginning 0)
5787                                               (match-end 0)))
5788                         (when (> (length ref2) (length ref))
5789                           (setq ref ref2)))
5790                       ref)
5791                   (setq ref nil))))
5792             ;; Chars.
5793             (progn
5794               (goto-char p)
5795               (if (search-forward "\nchars: " nil t)
5796                   (if (numberp (setq chars (ignore-errors (read cur))))
5797                       chars -1)
5798                 -1))
5799             ;; Lines.
5800             (progn
5801               (goto-char p)
5802               (if (search-forward "\nlines: " nil t)
5803                   (if (numberp (setq lines (ignore-errors (read cur))))
5804                       lines -1)
5805                 -1))
5806             ;; Xref.
5807             (progn
5808               (goto-char p)
5809               (and (search-forward "\nxref:" nil t)
5810                    (nnheader-header-value)))
5811             ;; Extra.
5812             (when gnus-extra-headers
5813               (let ((extra gnus-extra-headers)
5814                     out)
5815                 (while extra
5816                   (goto-char p)
5817                   (when (search-forward
5818                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5819                     (push (cons (car extra) (nnheader-header-value))
5820                           out))
5821                   (pop extra))
5822                 out))))
5823           (when (equal id ref)
5824             (setq ref nil))
5825
5826           (when gnus-alter-header-function
5827             (funcall gnus-alter-header-function header)
5828             (setq id (mail-header-id header)
5829                   ref (gnus-parent-id (mail-header-references header))))
5830
5831           (when (setq header
5832                       (gnus-dependencies-add-header
5833                        header dependencies force-new))
5834             (push header headers))
5835           (goto-char (point-max))
5836           (widen))
5837         (nreverse headers)))))
5838
5839 ;; Goes through the xover lines and returns a list of vectors
5840 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5841                                                   force-new dependencies
5842                                                   group also-fetch-heads)
5843   "Parse the news overview data in the server buffer.
5844 Return a list of headers that match SEQUENCE (see
5845 `nntp-retrieve-headers')."
5846   ;; Get the Xref when the users reads the articles since most/some
5847   ;; NNTP servers do not include Xrefs when using XOVER.
5848   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5849   (let ((mail-parse-charset gnus-newsgroup-charset)
5850         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5851         (cur nntp-server-buffer)
5852         (dependencies (or dependencies gnus-newsgroup-dependencies))
5853         (allp (cond
5854                ((eq gnus-read-all-available-headers t)
5855                 t)
5856                ((stringp gnus-read-all-available-headers)
5857                 (string-match gnus-read-all-available-headers group))
5858                (t
5859                 nil)))
5860         number headers header)
5861     (save-excursion
5862       (set-buffer nntp-server-buffer)
5863       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5864       ;; Allow the user to mangle the headers before parsing them.
5865       (gnus-run-hooks 'gnus-parse-headers-hook)
5866       (goto-char (point-min))
5867       (gnus-parse-without-error
5868         (while (and (or sequence allp)
5869                     (not (eobp)))
5870           (setq number (read cur))
5871           (when (not allp)
5872             (while (and sequence
5873                         (< (car sequence) number))
5874               (setq sequence (cdr sequence))))
5875           (when (and (or allp
5876                          (and sequence
5877                               (eq number (car sequence))))
5878                      (progn
5879                        (setq sequence (cdr sequence))
5880                        (setq header (inline
5881                                       (gnus-nov-parse-line
5882                                        number dependencies force-new)))))
5883             (push header headers))
5884           (forward-line 1)))
5885       ;; A common bug in inn is that if you have posted an article and
5886       ;; then retrieves the active file, it will answer correctly --
5887       ;; the new article is included.  However, a NOV entry for the
5888       ;; article may not have been generated yet, so this may fail.
5889       ;; We work around this problem by retrieving the last few
5890       ;; headers using HEAD.
5891       (if (or (not also-fetch-heads)
5892               (not sequence))
5893           ;; We (probably) got all the headers.
5894           (nreverse headers)
5895         (let ((gnus-nov-is-evil t))
5896           (nconc
5897            (nreverse headers)
5898            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5899              (gnus-get-newsgroup-headers))))))))
5900
5901 (defun gnus-article-get-xrefs ()
5902   "Fill in the Xref value in `gnus-current-headers', if necessary.
5903 This is meant to be called in `gnus-article-internal-prepare-hook'."
5904   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5905                                  gnus-current-headers)))
5906     (or (not gnus-use-cross-reference)
5907         (not headers)
5908         (and (mail-header-xref headers)
5909              (not (string= (mail-header-xref headers) "")))
5910         (let ((case-fold-search t)
5911               xref)
5912           (save-restriction
5913             (nnheader-narrow-to-headers)
5914             (goto-char (point-min))
5915             (when (or (and (not (eobp))
5916                            (eq (downcase (char-after)) ?x)
5917                            (looking-at "Xref:"))
5918                       (search-forward "\nXref:" nil t))
5919               (goto-char (1+ (match-end 0)))
5920               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5921               (mail-header-set-xref headers xref)))))))
5922
5923 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5924   "Find article ID and insert the summary line for that article.
5925 OLD-HEADER can either be a header or a line number to insert
5926 the subject line on."
5927   (let* ((line (and (numberp old-header) old-header))
5928          (old-header (and (vectorp old-header) old-header))
5929          (header (cond ((and old-header use-old-header)
5930                         old-header)
5931                        ((and (numberp id)
5932                              (gnus-number-to-header id))
5933                         (gnus-number-to-header id))
5934                        (t
5935                         (gnus-read-header id))))
5936          (number (and (numberp id) id))
5937          d)
5938     (when header
5939       ;; Rebuild the thread that this article is part of and go to the
5940       ;; article we have fetched.
5941       (when (and (not gnus-show-threads)
5942                  old-header)
5943         (when (and number
5944                    (setq d (gnus-data-find (mail-header-number old-header))))
5945           (goto-char (gnus-data-pos d))
5946           (gnus-data-remove
5947            number
5948            (- (gnus-point-at-bol)
5949               (prog1
5950                   (1+ (gnus-point-at-eol))
5951                 (gnus-delete-line))))))
5952       (when old-header
5953         (mail-header-set-number header (mail-header-number old-header)))
5954       (setq gnus-newsgroup-sparse
5955             (delq (setq number (mail-header-number header))
5956                   gnus-newsgroup-sparse))
5957       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5958       (push number gnus-newsgroup-limit)
5959       (gnus-rebuild-thread (mail-header-id header) line)
5960       (gnus-summary-goto-subject number nil t))
5961     (when (and (numberp number)
5962                (> number 0))
5963       ;; We have to update the boundaries even if we can't fetch the
5964       ;; article if ID is a number -- so that the next `P' or `N'
5965       ;; command will fetch the previous (or next) article even
5966       ;; if the one we tried to fetch this time has been canceled.
5967       (when (> number gnus-newsgroup-end)
5968         (setq gnus-newsgroup-end number))
5969       (when (< number gnus-newsgroup-begin)
5970         (setq gnus-newsgroup-begin number))
5971       (setq gnus-newsgroup-unselected
5972             (delq number gnus-newsgroup-unselected)))
5973     ;; Report back a success?
5974     (and header (mail-header-number header))))
5975
5976 ;;; Process/prefix in the summary buffer
5977
5978 (defun gnus-summary-work-articles (n)
5979   "Return a list of articles to be worked upon.
5980 The prefix argument, the list of process marked articles, and the
5981 current article will be taken into consideration."
5982   (save-excursion
5983     (set-buffer gnus-summary-buffer)
5984     (cond
5985      (n
5986       ;; A numerical prefix has been given.
5987       (setq n (prefix-numeric-value n))
5988       (let ((backward (< n 0))
5989             (n (abs (prefix-numeric-value n)))
5990             articles article)
5991         (save-excursion
5992           (while
5993               (and (> n 0)
5994                    (push (setq article (gnus-summary-article-number))
5995                          articles)
5996                    (if backward
5997                        (gnus-summary-find-prev nil article)
5998                      (gnus-summary-find-next nil article)))
5999             (decf n)))
6000         (nreverse articles)))
6001      ((and (gnus-region-active-p) (mark))
6002       (message "region active")
6003       ;; Work on the region between point and mark.
6004       (let ((max (max (point) (mark)))
6005             articles article)
6006         (save-excursion
6007           (goto-char (min (min (point) (mark))))
6008           (while
6009               (and
6010                (push (setq article (gnus-summary-article-number)) articles)
6011                (gnus-summary-find-next nil article)
6012                (< (point) max)))
6013           (nreverse articles))))
6014      (gnus-newsgroup-processable
6015       ;; There are process-marked articles present.
6016       ;; Save current state.
6017       (gnus-summary-save-process-mark)
6018       ;; Return the list.
6019       (reverse gnus-newsgroup-processable))
6020      (t
6021       ;; Just return the current article.
6022       (list (gnus-summary-article-number))))))
6023
6024 (defmacro gnus-summary-iterate (arg &rest forms)
6025   "Iterate over the process/prefixed articles and do FORMS.
6026 ARG is the interactive prefix given to the command.  FORMS will be
6027 executed with point over the summary line of the articles."
6028   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6029     `(let ((,articles (gnus-summary-work-articles ,arg)))
6030        (while ,articles
6031          (gnus-summary-goto-subject (car ,articles))
6032          ,@forms
6033          (pop ,articles)))))
6034
6035 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6036 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6037
6038 (defun gnus-summary-save-process-mark ()
6039   "Push the current set of process marked articles on the stack."
6040   (interactive)
6041   (push (copy-sequence gnus-newsgroup-processable)
6042         gnus-newsgroup-process-stack))
6043
6044 (defun gnus-summary-kill-process-mark ()
6045   "Push the current set of process marked articles on the stack and unmark."
6046   (interactive)
6047   (gnus-summary-save-process-mark)
6048   (gnus-summary-unmark-all-processable))
6049
6050 (defun gnus-summary-yank-process-mark ()
6051   "Pop the last process mark state off the stack and restore it."
6052   (interactive)
6053   (unless gnus-newsgroup-process-stack
6054     (error "Empty mark stack"))
6055   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6056
6057 (defun gnus-summary-process-mark-set (set)
6058   "Make SET into the current process marked articles."
6059   (gnus-summary-unmark-all-processable)
6060   (while set
6061     (gnus-summary-set-process-mark (pop set))))
6062
6063 ;;; Searching and stuff
6064
6065 (defun gnus-summary-search-group (&optional backward use-level)
6066   "Search for next unread newsgroup.
6067 If optional argument BACKWARD is non-nil, search backward instead."
6068   (save-excursion
6069     (set-buffer gnus-group-buffer)
6070     (when (gnus-group-search-forward
6071            backward nil (if use-level (gnus-group-group-level) nil))
6072       (gnus-group-group-name))))
6073
6074 (defun gnus-summary-best-group (&optional exclude-group)
6075   "Find the name of the best unread group.
6076 If EXCLUDE-GROUP, do not go to this group."
6077   (save-excursion
6078     (set-buffer gnus-group-buffer)
6079     (save-excursion
6080       (gnus-group-best-unread-group exclude-group))))
6081
6082 (defun gnus-summary-find-next (&optional unread article backward)
6083   (if backward (gnus-summary-find-prev unread article)
6084     (let* ((dummy (gnus-summary-article-intangible-p))
6085            (article (or article (gnus-summary-article-number)))
6086            (data (gnus-data-find-list article))
6087            result)
6088       (when (and (not dummy)
6089                  (or (not gnus-summary-check-current)
6090                      (not unread)
6091                      (not (gnus-data-unread-p (car data)))))
6092         (setq data (cdr data)))
6093       (when (setq result
6094                   (if unread
6095                       (progn
6096                         (while data
6097                           (unless (memq (gnus-data-number (car data)) 
6098                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6099                                                gnus-newsgroup-undownloaded)
6100                                               (gnus-plugged
6101                                                nil)
6102                                               ((eq gnus-auto-goto-ignores 'unfetched)
6103                                                gnus-newsgroup-unfetched)
6104                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6105                                                gnus-newsgroup-undownloaded)))
6106                             (when (gnus-data-unread-p (car data))
6107                               (setq result (car data)
6108                                     data nil)))
6109                           (setq data (cdr data)))
6110                         result)
6111                     (car data)))
6112         (goto-char (gnus-data-pos result))
6113         (gnus-data-number result)))))
6114
6115 (defun gnus-summary-find-prev (&optional unread article)
6116   (let* ((eobp (eobp))
6117          (article (or article (gnus-summary-article-number)))
6118          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6119          result)
6120     (when (and (not eobp)
6121                (or (not gnus-summary-check-current)
6122                    (not unread)
6123                    (not (gnus-data-unread-p (car data)))))
6124       (setq data (cdr data)))
6125     (when (setq result
6126                 (if unread
6127                     (progn
6128                       (while data
6129                         (unless (memq (gnus-data-number (car data))
6130                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6131                                              gnus-newsgroup-undownloaded)
6132                                             (gnus-plugged
6133                                              nil)
6134                                             ((eq gnus-auto-goto-ignores 'unfetched)
6135                                              gnus-newsgroup-unfetched)
6136                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6137                                              gnus-newsgroup-undownloaded)))
6138                           (when (gnus-data-unread-p (car data))
6139                             (setq result (car data)
6140                                   data nil)))
6141                         (setq data (cdr data)))
6142                       result)
6143                   (car data)))
6144       (goto-char (gnus-data-pos result))
6145       (gnus-data-number result))))
6146
6147 (defun gnus-summary-find-subject (subject &optional unread backward article)
6148   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6149          (article (or article (gnus-summary-article-number)))
6150          (articles (gnus-data-list backward))
6151          (arts (gnus-data-find-list article articles))
6152          result)
6153     (when (or (not gnus-summary-check-current)
6154               (not unread)
6155               (not (gnus-data-unread-p (car arts))))
6156       (setq arts (cdr arts)))
6157     (while arts
6158       (and (or (not unread)
6159                (gnus-data-unread-p (car arts)))
6160            (vectorp (gnus-data-header (car arts)))
6161            (gnus-subject-equal
6162             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6163            (setq result (car arts)
6164                  arts nil))
6165       (setq arts (cdr arts)))
6166     (and result
6167          (goto-char (gnus-data-pos result))
6168          (gnus-data-number result))))
6169
6170 (defun gnus-summary-search-forward (&optional unread subject backward)
6171   "Search forward for an article.
6172 If UNREAD, look for unread articles.  If SUBJECT, look for
6173 articles with that subject.  If BACKWARD, search backward instead."
6174   (cond (subject (gnus-summary-find-subject subject unread backward))
6175         (backward (gnus-summary-find-prev unread))
6176         (t (gnus-summary-find-next unread))))
6177
6178 (defun gnus-recenter (&optional n)
6179   "Center point in window and redisplay frame.
6180 Also do horizontal recentering."
6181   (interactive "P")
6182   (when (and gnus-auto-center-summary
6183              (not (eq gnus-auto-center-summary 'vertical)))
6184     (gnus-horizontal-recenter))
6185   (recenter n))
6186
6187 (defun gnus-summary-recenter ()
6188   "Center point in the summary window.
6189 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6190 displayed, no centering will be performed."
6191   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6192   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6193   (interactive)
6194   ;; The user has to want it.
6195   (when gnus-auto-center-summary
6196     (let* ((top (cond ((< (window-height) 4) 0)
6197                       ((< (window-height) 7) 1)
6198                       (t (if (numberp gnus-auto-center-summary)
6199                              gnus-auto-center-summary
6200                            2))))
6201            (height (1- (window-height)))
6202            (bottom (save-excursion (goto-char (point-max))
6203                                    (forward-line (- height))
6204                                    (point)))
6205            (window (get-buffer-window (current-buffer))))
6206       (when (get-buffer-window gnus-article-buffer)
6207         ;; Only do recentering when the article buffer is displayed,
6208         ;; Set the window start to either `bottom', which is the biggest
6209         ;; possible valid number, or the second line from the top,
6210         ;; whichever is the least.
6211         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6212           (if (> bottom top-pos)
6213               ;; Keep the second line from the top visible
6214               (set-window-start window top-pos t)
6215             ;; Try to keep the bottom line visible; if it's partially
6216             ;; obscured, either scroll one more line to make it fully
6217             ;; visible, or revert to using TOP-POS.
6218             (save-excursion
6219               (goto-char (point-max))
6220               (forward-line -1)
6221               (let ((last-line-start (point)))
6222                 (goto-char bottom)
6223                 (set-window-start window (point) t)
6224                 (when (not (pos-visible-in-window-p last-line-start window))
6225                   (forward-line 1)
6226                   (set-window-start window (min (point) top-pos) t)))))))
6227       ;; Do horizontal recentering while we're at it.
6228       (when (and (get-buffer-window (current-buffer) t)
6229                  (not (eq gnus-auto-center-summary 'vertical)))
6230         (let ((selected (selected-window)))
6231           (select-window (get-buffer-window (current-buffer) t))
6232           (gnus-summary-position-point)
6233           (gnus-horizontal-recenter)
6234           (select-window selected))))))
6235
6236 (defun gnus-summary-jump-to-group (newsgroup)
6237   "Move point to NEWSGROUP in group mode buffer."
6238   ;; Keep update point of group mode buffer if visible.
6239   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6240       (save-window-excursion
6241         ;; Take care of tree window mode.
6242         (when (get-buffer-window gnus-group-buffer)
6243           (pop-to-buffer gnus-group-buffer))
6244         (gnus-group-jump-to-group newsgroup))
6245     (save-excursion
6246       ;; Take care of tree window mode.
6247       (if (get-buffer-window gnus-group-buffer)
6248           (pop-to-buffer gnus-group-buffer)
6249         (set-buffer gnus-group-buffer))
6250       (gnus-group-jump-to-group newsgroup))))
6251
6252 ;; This function returns a list of article numbers based on the
6253 ;; difference between the ranges of read articles in this group and
6254 ;; the range of active articles.
6255 (defun gnus-list-of-unread-articles (group)
6256   (let* ((read (gnus-info-read (gnus-get-info group)))
6257          (active (or (gnus-active group) (gnus-activate-group group)))
6258          (last (cdr active))
6259          first nlast unread)
6260     ;; If none are read, then all are unread.
6261     (if (not read)
6262         (setq first (car active))
6263       ;; If the range of read articles is a single range, then the
6264       ;; first unread article is the article after the last read
6265       ;; article.  Sounds logical, doesn't it?
6266       (if (and (not (listp (cdr read)))
6267                (or (< (car read) (car active))
6268                    (progn (setq read (list read))
6269                           nil)))
6270           (setq first (max (car active) (1+ (cdr read))))
6271         ;; `read' is a list of ranges.
6272         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6273                                   (caar read)))
6274                   1)
6275           (setq first (car active)))
6276         (while read
6277           (when first
6278             (while (< first nlast)
6279               (push first unread)
6280               (setq first (1+ first))))
6281           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6282           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6283           (setq read (cdr read)))))
6284     ;; And add the last unread articles.
6285     (while (<= first last)
6286       (push first unread)
6287       (setq first (1+ first)))
6288     ;; Return the list of unread articles.
6289     (delq 0 (nreverse unread))))
6290
6291 (defun gnus-list-of-read-articles (group)
6292   "Return a list of unread, unticked and non-dormant articles."
6293   (let* ((info (gnus-get-info group))
6294          (marked (gnus-info-marks info))
6295          (active (gnus-active group)))
6296     (and info active
6297          (gnus-list-range-difference
6298           (gnus-list-range-difference
6299            (gnus-sorted-complement
6300             (gnus-uncompress-range active)
6301             (gnus-list-of-unread-articles group))
6302            (cdr (assq 'dormant marked)))
6303           (cdr (assq 'tick marked))))))
6304
6305 ;; Various summary commands
6306
6307 (defun gnus-summary-select-article-buffer ()
6308   "Reconfigure windows to show article buffer."
6309   (interactive)
6310   (if (not (gnus-buffer-live-p gnus-article-buffer))
6311       (error "There is no article buffer for this summary buffer")
6312     (gnus-configure-windows 'article)
6313     (select-window (get-buffer-window gnus-article-buffer))))
6314
6315 (defun gnus-summary-universal-argument (arg)
6316   "Perform any operation on all articles that are process/prefixed."
6317   (interactive "P")
6318   (let ((articles (gnus-summary-work-articles arg))
6319         func article)
6320     (if (eq
6321          (setq
6322           func
6323           (key-binding
6324            (read-key-sequence
6325             (substitute-command-keys
6326              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6327          'undefined)
6328         (gnus-error 1 "Undefined key")
6329       (save-excursion
6330         (while articles
6331           (gnus-summary-goto-subject (setq article (pop articles)))
6332           (let (gnus-newsgroup-processable)
6333             (command-execute func))
6334           (gnus-summary-remove-process-mark article)))))
6335   (gnus-summary-position-point))
6336
6337 (defun gnus-summary-toggle-truncation (&optional arg)
6338   "Toggle truncation of summary lines.
6339 With arg, turn line truncation on if arg is positive."
6340   (interactive "P")
6341   (setq truncate-lines
6342         (if (null arg) (not truncate-lines)
6343           (> (prefix-numeric-value arg) 0)))
6344   (redraw-display))
6345
6346 (defun gnus-summary-find-for-reselect ()
6347   "Return the number of an article to stay on across a reselect.
6348 The current article is considered, then following articles, then previous
6349 articles.  An article is sought which is not cancelled and isn't a temporary
6350 insertion from another group.  If there's no such then return a dummy 0."
6351   (let (found)
6352     (dolist (rev '(nil t))
6353       (unless found      ; don't demand the reverse list if we don't need it
6354         (let ((data (gnus-data-find-list
6355                      (gnus-summary-article-number) (gnus-data-list rev))))
6356           (while (and data (not found))
6357             (if (and (< 0 (gnus-data-number (car data)))
6358                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6359                 (setq found (gnus-data-number (car data))))
6360             (setq data (cdr data))))))
6361     (or found 0)))
6362
6363 (defun gnus-summary-reselect-current-group (&optional all rescan)
6364   "Exit and then reselect the current newsgroup.
6365 The prefix argument ALL means to select all articles."
6366   (interactive "P")
6367   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6368     (error "Ephemeral groups can't be reselected"))
6369   (let ((current-subject (gnus-summary-find-for-reselect))
6370         (group gnus-newsgroup-name))
6371     (setq gnus-newsgroup-begin nil)
6372     (gnus-summary-exit)
6373     ;; We have to adjust the point of group mode buffer because
6374     ;; point was moved to the next unread newsgroup by exiting.
6375     (gnus-summary-jump-to-group group)
6376     (when rescan
6377       (save-excursion
6378         (gnus-group-get-new-news-this-group 1)))
6379     (gnus-group-read-group all t)
6380     (gnus-summary-goto-subject current-subject nil t)))
6381
6382 (defun gnus-summary-rescan-group (&optional all)
6383   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6384   (interactive "P")
6385   (gnus-summary-reselect-current-group all t))
6386
6387 (defun gnus-summary-update-info (&optional non-destructive)
6388   (save-excursion
6389     (let ((group gnus-newsgroup-name))
6390       (when group
6391         (when gnus-newsgroup-kill-headers
6392           (setq gnus-newsgroup-killed
6393                 (gnus-compress-sequence
6394                  (gnus-sorted-union
6395                   (gnus-list-range-intersection
6396                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6397                   gnus-newsgroup-unreads)
6398                  t)))
6399         (unless (listp (cdr gnus-newsgroup-killed))
6400           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6401         (let ((headers gnus-newsgroup-headers))
6402           ;; Set the new ranges of read articles.
6403           (save-excursion
6404             (set-buffer gnus-group-buffer)
6405             (gnus-undo-force-boundary))
6406           (gnus-update-read-articles
6407            group (gnus-sorted-union
6408                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6409           ;; Set the current article marks.
6410           (let ((gnus-newsgroup-scored
6411                  (if (and (not gnus-save-score)
6412                           (not non-destructive))
6413                      nil
6414                    gnus-newsgroup-scored)))
6415             (save-excursion
6416               (gnus-update-marks)))
6417           ;; Do the cross-ref thing.
6418           (when gnus-use-cross-reference
6419             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6420           ;; Do not switch windows but change the buffer to work.
6421           (set-buffer gnus-group-buffer)
6422           (unless (gnus-ephemeral-group-p group)
6423             (gnus-group-update-group group)))))))
6424
6425 (defun gnus-summary-save-newsrc (&optional force)
6426   "Save the current number of read/marked articles in the dribble buffer.
6427 The dribble buffer will then be saved.
6428 If FORCE (the prefix), also save the .newsrc file(s)."
6429   (interactive "P")
6430   (gnus-summary-update-info t)
6431   (if force
6432       (gnus-save-newsrc-file)
6433     (gnus-dribble-save)))
6434
6435 (defun gnus-summary-exit (&optional temporary)
6436   "Exit reading current newsgroup, and then return to group selection mode.
6437 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6438   (interactive)
6439   (gnus-set-global-variables)
6440   (when (gnus-buffer-live-p gnus-article-buffer)
6441     (save-excursion
6442       (set-buffer gnus-article-buffer)
6443       (mm-destroy-parts gnus-article-mime-handles)
6444       ;; Set it to nil for safety reason.
6445       (setq gnus-article-mime-handle-alist nil)
6446       (setq gnus-article-mime-handles nil)))
6447   (gnus-kill-save-kill-buffer)
6448   (gnus-async-halt-prefetch)
6449   (let* ((group gnus-newsgroup-name)
6450          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6451          (gnus-group-is-exiting-p t)
6452          (mode major-mode)
6453          (group-point nil)
6454          (buf (current-buffer)))
6455     (unless quit-config
6456       ;; Do adaptive scoring, and possibly save score files.
6457       (when gnus-newsgroup-adaptive
6458         (gnus-score-adaptive))
6459       (when gnus-use-scoring
6460         (gnus-score-save)))
6461     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6462     ;; If we have several article buffers, we kill them at exit.
6463     (unless gnus-single-article-buffer
6464       (gnus-kill-buffer gnus-original-article-buffer)
6465       (setq gnus-article-current nil))
6466     (when gnus-use-cache
6467       (gnus-cache-possibly-remove-articles)
6468       (gnus-cache-save-buffers))
6469     (gnus-async-prefetch-remove-group group)
6470     (when gnus-suppress-duplicates
6471       (gnus-dup-enter-articles))
6472     (when gnus-use-trees
6473       (gnus-tree-close group))
6474     (when gnus-use-cache
6475       (gnus-cache-write-active))
6476     ;; Remove entries for this group.
6477     (nnmail-purge-split-history (gnus-group-real-name group))
6478     ;; Make all changes in this group permanent.
6479     (unless quit-config
6480       (gnus-run-hooks 'gnus-exit-group-hook)
6481       (gnus-summary-update-info))
6482     (gnus-close-group group)
6483     ;; Make sure where we were, and go to next newsgroup.
6484     (set-buffer gnus-group-buffer)
6485     (unless quit-config
6486       (gnus-group-jump-to-group group))
6487     (gnus-run-hooks 'gnus-summary-exit-hook)
6488     (unless (or quit-config
6489                 ;; If this group has disappeared from the summary
6490                 ;; buffer, don't skip forwards.
6491                 (not (string= group (gnus-group-group-name))))
6492       (gnus-group-next-unread-group 1))
6493     (setq group-point (point))
6494     (if temporary
6495         nil                             ;Nothing to do.
6496       ;; If we have several article buffers, we kill them at exit.
6497       (unless gnus-single-article-buffer
6498         (gnus-kill-buffer gnus-article-buffer)
6499         (gnus-kill-buffer gnus-original-article-buffer)
6500         (setq gnus-article-current nil))
6501       (set-buffer buf)
6502       (if (not gnus-kill-summary-on-exit)
6503           (progn
6504             (gnus-deaden-summary)
6505             (setq mode nil))
6506         ;; We set all buffer-local variables to nil.  It is unclear why
6507         ;; this is needed, but if we don't, buffer-local variables are
6508         ;; not garbage-collected, it seems.  This would the lead to en
6509         ;; ever-growing Emacs.
6510         (gnus-summary-clear-local-variables)
6511         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6512           (gnus-summary-clear-local-variables))
6513         (when (get-buffer gnus-article-buffer)
6514           (bury-buffer gnus-article-buffer))
6515         ;; We clear the global counterparts of the buffer-local
6516         ;; variables as well, just to be on the safe side.
6517         (set-buffer gnus-group-buffer)
6518         (gnus-summary-clear-local-variables)
6519         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6520           (gnus-summary-clear-local-variables))
6521         ;; Return to group mode buffer.
6522         (when (eq mode 'gnus-summary-mode)
6523           (gnus-kill-buffer buf)))
6524       (setq gnus-current-select-method gnus-select-method)
6525       (pop-to-buffer gnus-group-buffer)
6526       (if (not quit-config)
6527           (progn
6528             (goto-char group-point)
6529             (gnus-configure-windows 'group 'force))
6530         (gnus-handle-ephemeral-exit quit-config))
6531       ;; Clear the current group name.
6532       (unless quit-config
6533         (setq gnus-newsgroup-name nil)))))
6534
6535 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6536 (defun gnus-summary-exit-no-update (&optional no-questions)
6537   "Quit reading current newsgroup without updating read article info."
6538   (interactive)
6539   (let* ((group gnus-newsgroup-name)
6540          (gnus-group-is-exiting-p t)
6541          (gnus-group-is-exiting-without-update-p t)
6542          (quit-config (gnus-group-quit-config group)))
6543     (when (or no-questions
6544               gnus-expert-user
6545               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6546       (gnus-async-halt-prefetch)
6547       (run-hooks 'gnus-summary-prepare-exit-hook)
6548       (when (gnus-buffer-live-p gnus-article-buffer)
6549         (save-excursion
6550           (set-buffer gnus-article-buffer)
6551           (mm-destroy-parts gnus-article-mime-handles)
6552           ;; Set it to nil for safety reason.
6553           (setq gnus-article-mime-handle-alist nil)
6554           (setq gnus-article-mime-handles nil)))
6555       ;; If we have several article buffers, we kill them at exit.
6556       (unless gnus-single-article-buffer
6557         (gnus-kill-buffer gnus-article-buffer)
6558         (gnus-kill-buffer gnus-original-article-buffer)
6559         (setq gnus-article-current nil))
6560       (if (not gnus-kill-summary-on-exit)
6561           (gnus-deaden-summary)
6562         (gnus-close-group group)
6563         (gnus-summary-clear-local-variables)
6564         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6565           (gnus-summary-clear-local-variables))
6566         (set-buffer gnus-group-buffer)
6567         (gnus-summary-clear-local-variables)
6568         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6569           (gnus-summary-clear-local-variables))
6570         (gnus-kill-buffer gnus-summary-buffer))
6571       (unless gnus-single-article-buffer
6572         (setq gnus-article-current nil))
6573       (when gnus-use-trees
6574         (gnus-tree-close group))
6575       (gnus-async-prefetch-remove-group group)
6576       (when (get-buffer gnus-article-buffer)
6577         (bury-buffer gnus-article-buffer))
6578       ;; Return to the group buffer.
6579       (gnus-configure-windows 'group 'force)
6580       ;; Clear the current group name.
6581       (setq gnus-newsgroup-name nil)
6582       (unless (gnus-ephemeral-group-p group)
6583         (gnus-group-update-group group))
6584       (when (equal (gnus-group-group-name) group)
6585         (gnus-group-next-unread-group 1))
6586       (when quit-config
6587         (gnus-handle-ephemeral-exit quit-config)))))
6588
6589 (defun gnus-handle-ephemeral-exit (quit-config)
6590   "Handle movement when leaving an ephemeral group.
6591 The state which existed when entering the ephemeral is reset."
6592   (if (not (buffer-name (car quit-config)))
6593       (gnus-configure-windows 'group 'force)
6594     (set-buffer (car quit-config))
6595     (cond ((eq major-mode 'gnus-summary-mode)
6596            (gnus-set-global-variables))
6597           ((eq major-mode 'gnus-article-mode)
6598            (save-excursion
6599              ;; The `gnus-summary-buffer' variable may point
6600              ;; to the old summary buffer when using a single
6601              ;; article buffer.
6602              (unless (gnus-buffer-live-p gnus-summary-buffer)
6603                (set-buffer gnus-group-buffer))
6604              (set-buffer gnus-summary-buffer)
6605              (gnus-set-global-variables))))
6606     (if (or (eq (cdr quit-config) 'article)
6607             (eq (cdr quit-config) 'pick))
6608         (progn
6609           ;; The current article may be from the ephemeral group
6610           ;; thus it is best that we reload this article
6611           (gnus-summary-show-article)
6612           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6613               (gnus-configure-windows 'pick 'force)
6614             (gnus-configure-windows (cdr quit-config) 'force)))
6615       (gnus-configure-windows (cdr quit-config) 'force))
6616     (when (eq major-mode 'gnus-summary-mode)
6617       (gnus-summary-next-subject 1 nil t)
6618       (gnus-summary-recenter)
6619       (gnus-summary-position-point))))
6620
6621 ;;; Dead summaries.
6622
6623 (defvar gnus-dead-summary-mode-map nil)
6624
6625 (unless gnus-dead-summary-mode-map
6626   (setq gnus-dead-summary-mode-map (make-keymap))
6627   (suppress-keymap gnus-dead-summary-mode-map)
6628   (substitute-key-definition
6629    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6630   (dolist (key '("\C-d" "\r" "\177" [delete]))
6631     (define-key gnus-dead-summary-mode-map
6632       key 'gnus-summary-wake-up-the-dead))
6633   (dolist (key '("q" "Q"))
6634     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6635
6636 (defvar gnus-dead-summary-mode nil
6637   "Minor mode for Gnus summary buffers.")
6638
6639 (defun gnus-dead-summary-mode (&optional arg)
6640   "Minor mode for Gnus summary buffers."
6641   (interactive "P")
6642   (when (eq major-mode 'gnus-summary-mode)
6643     (make-local-variable 'gnus-dead-summary-mode)
6644     (setq gnus-dead-summary-mode
6645           (if (null arg) (not gnus-dead-summary-mode)
6646             (> (prefix-numeric-value arg) 0)))
6647     (when gnus-dead-summary-mode
6648       (gnus-add-minor-mode
6649        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6650
6651 (defun gnus-deaden-summary ()
6652   "Make the current summary buffer into a dead summary buffer."
6653   ;; Kill any previous dead summary buffer.
6654   (when (and gnus-dead-summary
6655              (buffer-name gnus-dead-summary))
6656     (save-excursion
6657       (set-buffer gnus-dead-summary)
6658       (when gnus-dead-summary-mode
6659         (kill-buffer (current-buffer)))))
6660   ;; Make this the current dead summary.
6661   (setq gnus-dead-summary (current-buffer))
6662   (gnus-dead-summary-mode 1)
6663   (let ((name (buffer-name)))
6664     (when (string-match "Summary" name)
6665       (rename-buffer
6666        (concat (substring name 0 (match-beginning 0)) "Dead "
6667                (substring name (match-beginning 0)))
6668        t)
6669       (bury-buffer))))
6670
6671 (defun gnus-kill-or-deaden-summary (buffer)
6672   "Kill or deaden the summary BUFFER."
6673   (save-excursion
6674     (when (and (buffer-name buffer)
6675                (not gnus-single-article-buffer))
6676       (save-excursion
6677         (set-buffer buffer)
6678         (gnus-kill-buffer gnus-article-buffer)
6679         (gnus-kill-buffer gnus-original-article-buffer)))
6680     (cond
6681      ;; Kill the buffer.
6682      (gnus-kill-summary-on-exit
6683       (when (and gnus-use-trees
6684                  (gnus-buffer-exists-p buffer))
6685         (save-excursion
6686           (set-buffer buffer)
6687           (gnus-tree-close gnus-newsgroup-name)))
6688       (gnus-kill-buffer buffer))
6689      ;; Deaden the buffer.
6690      ((gnus-buffer-exists-p buffer)
6691       (save-excursion
6692         (set-buffer buffer)
6693         (gnus-deaden-summary))))))
6694
6695 (defun gnus-summary-wake-up-the-dead (&rest args)
6696   "Wake up the dead summary buffer."
6697   (interactive)
6698   (gnus-dead-summary-mode -1)
6699   (let ((name (buffer-name)))
6700     (when (string-match "Dead " name)
6701       (rename-buffer
6702        (concat (substring name 0 (match-beginning 0))
6703                (substring name (match-end 0)))
6704        t)))
6705   (gnus-message 3 "This dead summary is now alive again"))
6706
6707 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6708 (defun gnus-summary-fetch-faq (&optional faq-dir)
6709   "Fetch the FAQ for the current group.
6710 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6711 in."
6712   (interactive
6713    (list
6714     (when current-prefix-arg
6715       (completing-read
6716        "FAQ dir: " (and (listp gnus-group-faq-directory)
6717                         (mapcar (lambda (file) (list file))
6718                                 gnus-group-faq-directory))))))
6719   (let (gnus-faq-buffer)
6720     (when (setq gnus-faq-buffer
6721                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6722       (gnus-configure-windows 'summary-faq))))
6723
6724 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6725 (defun gnus-summary-describe-group (&optional force)
6726   "Describe the current newsgroup."
6727   (interactive "P")
6728   (gnus-group-describe-group force gnus-newsgroup-name))
6729
6730 (defun gnus-summary-describe-briefly ()
6731   "Describe summary mode commands briefly."
6732   (interactive)
6733   (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")))
6734
6735 ;; Walking around group mode buffer from summary mode.
6736
6737 (defun gnus-summary-next-group (&optional no-article target-group backward)
6738   "Exit current newsgroup and then select next unread newsgroup.
6739 If prefix argument NO-ARTICLE is non-nil, no article is selected
6740 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6741 previous group instead."
6742   (interactive "P")
6743   ;; Stop pre-fetching.
6744   (gnus-async-halt-prefetch)
6745   (let ((current-group gnus-newsgroup-name)
6746         (current-buffer (current-buffer))
6747         entered)
6748     ;; First we semi-exit this group to update Xrefs and all variables.
6749     ;; We can't do a real exit, because the window conf must remain
6750     ;; the same in case the user is prompted for info, and we don't
6751     ;; want the window conf to change before that...
6752     (gnus-summary-exit t)
6753     (while (not entered)
6754       ;; Then we find what group we are supposed to enter.
6755       (set-buffer gnus-group-buffer)
6756       (gnus-group-jump-to-group current-group)
6757       (setq target-group
6758             (or target-group
6759                 (if (eq gnus-keep-same-level 'best)
6760                     (gnus-summary-best-group gnus-newsgroup-name)
6761                   (gnus-summary-search-group backward gnus-keep-same-level))))
6762       (if (not target-group)
6763           ;; There are no further groups, so we return to the group
6764           ;; buffer.
6765           (progn
6766             (gnus-message 5 "Returning to the group buffer")
6767             (setq entered t)
6768             (when (gnus-buffer-live-p current-buffer)
6769               (set-buffer current-buffer)
6770               (gnus-summary-exit))
6771             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6772         ;; We try to enter the target group.
6773         (gnus-group-jump-to-group target-group)
6774         (let ((unreads (gnus-group-group-unread)))
6775           (if (and (or (eq t unreads)
6776                        (and unreads (not (zerop unreads))))
6777                    (gnus-summary-read-group
6778                     target-group nil no-article
6779                     (and (buffer-name current-buffer) current-buffer)
6780                     nil backward))
6781               (setq entered t)
6782             (setq current-group target-group
6783                   target-group nil)))))))
6784
6785 (defun gnus-summary-prev-group (&optional no-article)
6786   "Exit current newsgroup and then select previous unread newsgroup.
6787 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6788   (interactive "P")
6789   (gnus-summary-next-group no-article nil t))
6790
6791 ;; Walking around summary lines.
6792
6793 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6794   "Go to the first subject satisfying any non-nil constraint.
6795 If UNREAD is non-nil, the article should be unread.
6796 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6797 If UNSEED is non-nil, the article should be unseen.
6798 Returns the article selected or nil if there are no matching articles."
6799   (interactive "P")
6800   (cond
6801    ;; Empty summary.
6802    ((null gnus-newsgroup-data)
6803     (gnus-message 3 "No articles in the group")
6804     nil)
6805    ;; Pick the first article.
6806    ((not (or unread undownloaded unseen))
6807     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6808     (gnus-data-number (car gnus-newsgroup-data)))
6809    ;; Find the first unread article.
6810    (t
6811     (let ((data gnus-newsgroup-data))
6812       (while (and data
6813                   (let ((num (gnus-data-number (car data))))
6814                     (or (memq num gnus-newsgroup-unfetched)
6815                         (not (or (and unread
6816                                       (memq num gnus-newsgroup-unreads))
6817                                  (and undownloaded
6818                                       (memq num gnus-newsgroup-undownloaded))
6819                                  (and unseen
6820                                       (memq num gnus-newsgroup-unseen)))))))
6821         (setq data (cdr data)))
6822       (prog1 
6823           (if data
6824               (progn
6825                 (goto-char (gnus-data-pos (car data)))
6826                 (gnus-data-number (car data)))
6827             (gnus-message 3 "No more%s articles"
6828                           (let* ((r (when unread " unread"))
6829                                  (d (when undownloaded " undownloaded"))
6830                                  (s (when unseen " unseen"))
6831                                  (l (delq nil (list r d s))))
6832                             (cond ((= 3 (length l))
6833                                    (concat r "," d ", or" s))
6834                                   ((= 2 (length l))
6835                                    (concat (car l) ", or" (cadr l)))
6836                                   ((= 1 (length l))
6837                                    (car l))
6838                                   (t
6839                                    ""))))
6840             nil
6841             )
6842         (gnus-summary-position-point))))))
6843
6844 (defun gnus-summary-next-subject (n &optional unread dont-display)
6845   "Go to next N'th summary line.
6846 If N is negative, go to the previous N'th subject line.
6847 If UNREAD is non-nil, only unread articles are selected.
6848 The difference between N and the actual number of steps taken is
6849 returned."
6850   (interactive "p")
6851   (let ((backward (< n 0))
6852         (n (abs n)))
6853     (while (and (> n 0)
6854                 (if backward
6855                     (gnus-summary-find-prev unread)
6856                   (gnus-summary-find-next unread)))
6857       (unless (zerop (setq n (1- n)))
6858         (gnus-summary-show-thread)))
6859     (when (/= 0 n)
6860       (gnus-message 7 "No more%s articles"
6861                     (if unread " unread" "")))
6862     (unless dont-display
6863       (gnus-summary-recenter)
6864       (gnus-summary-position-point))
6865     n))
6866
6867 (defun gnus-summary-next-unread-subject (n)
6868   "Go to next N'th unread summary line."
6869   (interactive "p")
6870   (gnus-summary-next-subject n t))
6871
6872 (defun gnus-summary-prev-subject (n &optional unread)
6873   "Go to previous N'th summary line.
6874 If optional argument UNREAD is non-nil, only unread article is selected."
6875   (interactive "p")
6876   (gnus-summary-next-subject (- n) unread))
6877
6878 (defun gnus-summary-prev-unread-subject (n)
6879   "Go to previous N'th unread summary line."
6880   (interactive "p")
6881   (gnus-summary-next-subject (- n) t))
6882
6883 (defun gnus-summary-goto-subjects (articles)
6884   "Insert the subject header for ARTICLES in the current buffer."
6885   (save-excursion
6886     (dolist (article articles)
6887       (gnus-summary-goto-subject article t)))
6888   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6889   (gnus-summary-position-point))
6890  
6891 (defun gnus-summary-goto-subject (article &optional force silent)
6892   "Go the subject line of ARTICLE.
6893 If FORCE, also allow jumping to articles not currently shown."
6894   (interactive "nArticle number: ")
6895   (unless (numberp article)
6896     (error "Article %s is not a number" article))
6897   (let ((b (point))
6898         (data (gnus-data-find article)))
6899     ;; We read in the article if we have to.
6900     (and (not data)
6901          force
6902          (gnus-summary-insert-subject
6903           article
6904           (if (or (numberp force) (vectorp force)) force)
6905           t)
6906          (setq data (gnus-data-find article)))
6907     (goto-char b)
6908     (if (not data)
6909         (progn
6910           (unless silent
6911             (gnus-message 3 "Can't find article %d" article))
6912           nil)
6913       (let ((pt (gnus-data-pos data)))
6914         (goto-char pt)
6915         (gnus-summary-set-article-display-arrow pt))
6916       (gnus-summary-position-point)
6917       article)))
6918
6919 ;; Walking around summary lines with displaying articles.
6920
6921 (defun gnus-summary-expand-window (&optional arg)
6922   "Make the summary buffer take up the entire Emacs frame.
6923 Given a prefix, will force an `article' buffer configuration."
6924   (interactive "P")
6925   (if arg
6926       (gnus-configure-windows 'article 'force)
6927     (gnus-configure-windows 'summary 'force)))
6928
6929 (defun gnus-summary-display-article (article &optional all-header)
6930   "Display ARTICLE in article buffer."
6931   (when (gnus-buffer-live-p gnus-article-buffer)
6932     (with-current-buffer gnus-article-buffer
6933       (mm-enable-multibyte)))
6934   (gnus-set-global-variables)
6935   (when (gnus-buffer-live-p gnus-article-buffer)
6936     (with-current-buffer gnus-article-buffer
6937       (setq gnus-article-charset gnus-newsgroup-charset)
6938       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6939       (mm-enable-multibyte)))
6940   (if (null article)
6941       nil
6942     (prog1
6943         (if gnus-summary-display-article-function
6944             (funcall gnus-summary-display-article-function article all-header)
6945           (gnus-article-prepare article all-header))
6946       (gnus-run-hooks 'gnus-select-article-hook)
6947       (when (and gnus-current-article
6948                  (not (zerop gnus-current-article)))
6949         (gnus-summary-goto-subject gnus-current-article))
6950       (gnus-summary-recenter)
6951       (when (and gnus-use-trees gnus-show-threads)
6952         (gnus-possibly-generate-tree article)
6953         (gnus-highlight-selected-tree article))
6954       ;; Successfully display article.
6955       (gnus-article-set-window-start
6956        (cdr (assq article gnus-newsgroup-bookmarks))))))
6957
6958 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6959   "Select the current article.
6960 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6961 non-nil, the article will be re-fetched even if it already present in
6962 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6963 be displayed."
6964   ;; Make sure we are in the summary buffer to work around bbdb bug.
6965   (unless (eq major-mode 'gnus-summary-mode)
6966     (set-buffer gnus-summary-buffer))
6967   (let ((article (or article (gnus-summary-article-number)))
6968         (all-headers (not (not all-headers))) ;Must be t or nil.
6969         gnus-summary-display-article-function)
6970     (and (not pseudo)
6971          (gnus-summary-article-pseudo-p article)
6972          (error "This is a pseudo-article"))
6973     (save-excursion
6974       (set-buffer gnus-summary-buffer)
6975       (if (or (and gnus-single-article-buffer
6976                    (or (null gnus-current-article)
6977                        (null gnus-article-current)
6978                        (null (get-buffer gnus-article-buffer))
6979                        (not (eq article (cdr gnus-article-current)))
6980                        (not (equal (car gnus-article-current)
6981                                    gnus-newsgroup-name))))
6982               (and (not gnus-single-article-buffer)
6983                    (or (null gnus-current-article)
6984                        (not (eq gnus-current-article article))))
6985               force)
6986           ;; The requested article is different from the current article.
6987           (progn
6988             (gnus-summary-display-article article all-headers)
6989             (when (gnus-buffer-live-p gnus-article-buffer)
6990               (with-current-buffer gnus-article-buffer
6991                 (if (not gnus-article-decoded-p) ;; a local variable
6992                     (mm-disable-multibyte))))
6993             (gnus-article-set-window-start
6994              (cdr (assq article gnus-newsgroup-bookmarks)))
6995             article)
6996         'old))))
6997
6998 (defun gnus-summary-force-verify-and-decrypt ()
6999   "Display buttons for signed/encrypted parts and verify/decrypt them."
7000   (interactive)
7001   (let ((mm-verify-option 'known)
7002         (mm-decrypt-option 'known)
7003         (gnus-buttonized-mime-types (append (list "multipart/signed"
7004                                                   "multipart/encrypted")
7005                                             gnus-buttonized-mime-types)))
7006     (gnus-summary-select-article nil 'force)))
7007
7008 (defun gnus-summary-set-current-mark (&optional current-mark)
7009   "Obsolete function."
7010   nil)
7011
7012 (defun gnus-summary-next-article (&optional unread subject backward push)
7013   "Select the next article.
7014 If UNREAD, only unread articles are selected.
7015 If SUBJECT, only articles with SUBJECT are selected.
7016 If BACKWARD, the previous article is selected instead of the next."
7017   (interactive "P")
7018   (cond
7019    ;; Is there such an article?
7020    ((and (gnus-summary-search-forward unread subject backward)
7021          (or (gnus-summary-display-article (gnus-summary-article-number))
7022              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7023     (gnus-summary-position-point))
7024    ;; If not, we try the first unread, if that is wanted.
7025    ((and subject
7026          gnus-auto-select-same
7027          (gnus-summary-first-unread-article))
7028     (gnus-summary-position-point)
7029     (gnus-message 6 "Wrapped"))
7030    ;; Try to get next/previous article not displayed in this group.
7031    ((and gnus-auto-extend-newsgroup
7032          (not unread) (not subject))
7033     (gnus-summary-goto-article
7034      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7035      nil (count-lines (point-min) (point))))
7036    ;; Go to next/previous group.
7037    (t
7038     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7039       (gnus-summary-jump-to-group gnus-newsgroup-name))
7040     (let ((cmd last-command-char)
7041           (point
7042            (save-excursion
7043              (set-buffer gnus-group-buffer)
7044              (point)))
7045           (group
7046            (if (eq gnus-keep-same-level 'best)
7047                (gnus-summary-best-group gnus-newsgroup-name)
7048              (gnus-summary-search-group backward gnus-keep-same-level))))
7049       ;; For some reason, the group window gets selected.  We change
7050       ;; it back.
7051       (select-window (get-buffer-window (current-buffer)))
7052       ;; Select next unread newsgroup automagically.
7053       (cond
7054        ((or (not gnus-auto-select-next)
7055             (not cmd))
7056         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7057        ((or (eq gnus-auto-select-next 'quietly)
7058             (and (eq gnus-auto-select-next 'slightly-quietly)
7059                  push)
7060             (and (eq gnus-auto-select-next 'almost-quietly)
7061                  (gnus-summary-last-article-p)))
7062         ;; Select quietly.
7063         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7064             (gnus-summary-exit)
7065           (gnus-message 7 "No more%s articles (%s)..."
7066                         (if unread " unread" "")
7067                         (if group (concat "selecting " group)
7068                           "exiting"))
7069           (gnus-summary-next-group nil group backward)))
7070        (t
7071         (when (gnus-key-press-event-p last-input-event)
7072           (gnus-summary-walk-group-buffer
7073            gnus-newsgroup-name cmd unread backward point))))))))
7074
7075 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7076   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7077                       (?\C-p (gnus-group-prev-unread-group 1))))
7078         (cursor-in-echo-area t)
7079         keve key group ended prompt)
7080     (save-excursion
7081       (set-buffer gnus-group-buffer)
7082       (goto-char start)
7083       (setq group
7084             (if (eq gnus-keep-same-level 'best)
7085                 (gnus-summary-best-group gnus-newsgroup-name)
7086               (gnus-summary-search-group backward gnus-keep-same-level))))
7087     (while (not ended)
7088       (setq prompt
7089             (format
7090              "No more%s articles%s " (if unread " unread" "")
7091              (if (and group
7092                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7093                  (format " (Type %s for %s [%s])"
7094                          (single-key-description cmd) group
7095                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7096                (format " (Type %s to exit %s)"
7097                        (single-key-description cmd)
7098                        gnus-newsgroup-name))))
7099       ;; Confirm auto selection.
7100       (setq key (car (setq keve (gnus-read-event-char prompt)))
7101             ended t)
7102       (cond
7103        ((assq key keystrokes)
7104         (let ((obuf (current-buffer)))
7105           (switch-to-buffer gnus-group-buffer)
7106           (when group
7107             (gnus-group-jump-to-group group))
7108           (eval (cadr (assq key keystrokes)))
7109           (setq group (gnus-group-group-name))
7110           (switch-to-buffer obuf))
7111         (setq ended nil))
7112        ((equal key cmd)
7113         (if (or (not group)
7114                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7115             (gnus-summary-exit)
7116           (gnus-summary-next-group nil group backward)))
7117        (t
7118         (push (cdr keve) unread-command-events))))))
7119
7120 (defun gnus-summary-next-unread-article ()
7121   "Select unread article after current one."
7122   (interactive)
7123   (gnus-summary-next-article
7124    (or (not (eq gnus-summary-goto-unread 'never))
7125        (gnus-summary-last-article-p (gnus-summary-article-number)))
7126    (and gnus-auto-select-same
7127         (gnus-summary-article-subject))))
7128
7129 (defun gnus-summary-prev-article (&optional unread subject)
7130   "Select the article after the current one.
7131 If UNREAD is non-nil, only unread articles are selected."
7132   (interactive "P")
7133   (gnus-summary-next-article unread subject t))
7134
7135 (defun gnus-summary-prev-unread-article ()
7136   "Select unread article before current one."
7137   (interactive)
7138   (gnus-summary-prev-article
7139    (or (not (eq gnus-summary-goto-unread 'never))
7140        (gnus-summary-first-article-p (gnus-summary-article-number)))
7141    (and gnus-auto-select-same
7142         (gnus-summary-article-subject))))
7143
7144 (defun gnus-summary-next-page (&optional lines circular stop)
7145   "Show next page of the selected article.
7146 If at the end of the current article, select the next article.
7147 LINES says how many lines should be scrolled up.
7148
7149 If CIRCULAR is non-nil, go to the start of the article instead of
7150 selecting the next article when reaching the end of the current
7151 article.
7152
7153 If STOP is non-nil, just stop when reaching the end of the message."
7154   (interactive "P")
7155   (setq gnus-summary-buffer (current-buffer))
7156   (gnus-set-global-variables)
7157   (let ((article (gnus-summary-article-number))
7158         (article-window (get-buffer-window gnus-article-buffer t))
7159         endp)
7160     ;; If the buffer is empty, we have no article.
7161     (unless article
7162       (error "No article to select"))
7163     (gnus-configure-windows 'article)
7164     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7165         (if (and (eq gnus-summary-goto-unread 'never)
7166                  (not (gnus-summary-last-article-p article)))
7167             (gnus-summary-next-article)
7168           (gnus-summary-next-unread-article))
7169       (if (or (null gnus-current-article)
7170               (null gnus-article-current)
7171               (/= article (cdr gnus-article-current))
7172               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7173           ;; Selected subject is different from current article's.
7174           (gnus-summary-display-article article)
7175         (when article-window
7176           (gnus-eval-in-buffer-window gnus-article-buffer
7177             (setq endp (or (gnus-article-next-page lines)
7178                            (gnus-article-only-boring-p))))
7179           (when endp
7180             (cond (stop
7181                    (gnus-message 3 "End of message"))
7182                   (circular
7183                    (gnus-summary-beginning-of-article))
7184                   (lines
7185                    (gnus-message 3 "End of message"))
7186                   ((null lines)
7187                    (if (and (eq gnus-summary-goto-unread 'never)
7188                             (not (gnus-summary-last-article-p article)))
7189                        (gnus-summary-next-article)
7190                      (gnus-summary-next-unread-article))))))))
7191     (gnus-summary-recenter)
7192     (gnus-summary-position-point)))
7193
7194 (defun gnus-summary-prev-page (&optional lines move)
7195   "Show previous page of selected article.
7196 Argument LINES specifies lines to be scrolled down.
7197 If MOVE, move to the previous unread article if point is at
7198 the beginning of the buffer."
7199   (interactive "P")
7200   (let ((article (gnus-summary-article-number))
7201         (article-window (get-buffer-window gnus-article-buffer t))
7202         endp)
7203     (gnus-configure-windows 'article)
7204     (if (or (null gnus-current-article)
7205             (null gnus-article-current)
7206             (/= article (cdr gnus-article-current))
7207             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7208         ;; Selected subject is different from current article's.
7209         (gnus-summary-display-article article)
7210       (gnus-summary-recenter)
7211       (when article-window
7212         (gnus-eval-in-buffer-window gnus-article-buffer
7213           (setq endp (gnus-article-prev-page lines)))
7214         (when (and move endp)
7215           (cond (lines
7216                  (gnus-message 3 "Beginning of message"))
7217                 ((null lines)
7218                  (if (and (eq gnus-summary-goto-unread 'never)
7219                           (not (gnus-summary-first-article-p article)))
7220                      (gnus-summary-prev-article)
7221                    (gnus-summary-prev-unread-article))))))))
7222   (gnus-summary-position-point))
7223
7224 (defun gnus-summary-prev-page-or-article (&optional lines)
7225   "Show previous page of selected article.
7226 Argument LINES specifies lines to be scrolled down.
7227 If at the beginning of the article, go to the next article."
7228   (interactive "P")
7229   (gnus-summary-prev-page lines t))
7230
7231 (defun gnus-summary-scroll-up (lines)
7232   "Scroll up (or down) one line current article.
7233 Argument LINES specifies lines to be scrolled up (or down if negative)."
7234   (interactive "p")
7235   (gnus-configure-windows 'article)
7236   (gnus-summary-show-thread)
7237   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7238     (gnus-eval-in-buffer-window gnus-article-buffer
7239       (cond ((> lines 0)
7240              (when (gnus-article-next-page lines)
7241                (gnus-message 3 "End of message")))
7242             ((< lines 0)
7243              (gnus-article-prev-page (- lines))))))
7244   (gnus-summary-recenter)
7245   (gnus-summary-position-point))
7246
7247 (defun gnus-summary-scroll-down (lines)
7248   "Scroll down (or up) one line current article.
7249 Argument LINES specifies lines to be scrolled down (or up if negative)."
7250   (interactive "p")
7251   (gnus-summary-scroll-up (- lines)))
7252
7253 (defun gnus-summary-next-same-subject ()
7254   "Select next article which has the same subject as current one."
7255   (interactive)
7256   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7257
7258 (defun gnus-summary-prev-same-subject ()
7259   "Select previous article which has the same subject as current one."
7260   (interactive)
7261   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7262
7263 (defun gnus-summary-next-unread-same-subject ()
7264   "Select next unread article which has the same subject as current one."
7265   (interactive)
7266   (gnus-summary-next-article t (gnus-summary-article-subject)))
7267
7268 (defun gnus-summary-prev-unread-same-subject ()
7269   "Select previous unread article which has the same subject as current one."
7270   (interactive)
7271   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7272
7273 (defun gnus-summary-first-unread-article ()
7274   "Select the first unread article.
7275 Return nil if there are no unread articles."
7276   (interactive)
7277   (prog1
7278       (when (gnus-summary-first-subject t)
7279         (gnus-summary-show-thread)
7280         (gnus-summary-first-subject t)
7281         (gnus-summary-display-article (gnus-summary-article-number)))
7282     (gnus-summary-position-point)))
7283
7284 (defun gnus-summary-first-unread-subject ()
7285   "Place the point on the subject line of the first unread article.
7286 Return nil if there are no unread articles."
7287   (interactive)
7288   (prog1
7289       (when (gnus-summary-first-subject t)
7290         (gnus-summary-show-thread)
7291         (gnus-summary-first-subject t))
7292     (gnus-summary-position-point)))
7293
7294 (defun gnus-summary-first-unseen-subject ()
7295   "Place the point on the subject line of the first unseen article.
7296 Return nil if there are no unseen articles."
7297   (interactive)
7298   (prog1
7299       (when (gnus-summary-first-subject nil nil t)
7300         (gnus-summary-show-thread)
7301         (gnus-summary-first-subject nil nil t))
7302     (gnus-summary-position-point)))
7303
7304 (defun gnus-summary-first-unseen-or-unread-subject ()
7305   "Place the point on the subject line of the first unseen article or,
7306 if all article have been seen, on the subject line of the first unread
7307 article."
7308   (interactive)
7309   (prog1
7310       (unless (when (gnus-summary-first-subject nil nil t)
7311                 (gnus-summary-show-thread)
7312                 (gnus-summary-first-subject nil nil t))
7313         (when (gnus-summary-first-subject t)
7314           (gnus-summary-show-thread)
7315           (gnus-summary-first-subject t)))
7316     (gnus-summary-position-point)))
7317
7318 (defun gnus-summary-first-article ()
7319   "Select the first article.
7320 Return nil if there are no articles."
7321   (interactive)
7322   (prog1
7323       (when (gnus-summary-first-subject)
7324         (gnus-summary-show-thread)
7325         (gnus-summary-first-subject)
7326         (gnus-summary-display-article (gnus-summary-article-number)))
7327     (gnus-summary-position-point)))
7328
7329 (defun gnus-summary-best-unread-article (&optional arg)
7330   "Select the unread article with the highest score.
7331 If given a prefix argument, select the next unread article that has a
7332 score higher than the default score."
7333   (interactive "P")
7334   (let ((article (if arg
7335                      (gnus-summary-better-unread-subject)
7336                    (gnus-summary-best-unread-subject))))
7337     (if article
7338         (gnus-summary-goto-article article)
7339       (error "No unread articles"))))
7340
7341 (defun gnus-summary-best-unread-subject ()
7342   "Select the unread subject with the highest score."
7343   (interactive)
7344   (let ((best -1000000)
7345         (data gnus-newsgroup-data)
7346         article score)
7347     (while data
7348       (and (gnus-data-unread-p (car data))
7349            (> (setq score
7350                     (gnus-summary-article-score (gnus-data-number (car data))))
7351               best)
7352            (setq best score
7353                  article (gnus-data-number (car data))))
7354       (setq data (cdr data)))
7355     (when article
7356       (gnus-summary-goto-subject article))
7357     (gnus-summary-position-point)
7358     article))
7359
7360 (defun gnus-summary-better-unread-subject ()
7361   "Select the first unread subject that has a score over the default score."
7362   (interactive)
7363   (let ((data gnus-newsgroup-data)
7364         article score)
7365     (while (and (setq article (gnus-data-number (car data)))
7366                 (or (gnus-data-read-p (car data))
7367                     (not (> (gnus-summary-article-score article)
7368                             gnus-summary-default-score))))
7369       (setq data (cdr data)))
7370     (when article
7371       (gnus-summary-goto-subject article))
7372     (gnus-summary-position-point)
7373     article))
7374
7375 (defun gnus-summary-last-subject ()
7376   "Go to the last displayed subject line in the group."
7377   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7378     (when article
7379       (gnus-summary-goto-subject article))))
7380
7381 (defun gnus-summary-goto-article (article &optional all-headers force)
7382   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7383 If ALL-HEADERS is non-nil, no header lines are hidden.
7384 If FORCE, go to the article even if it isn't displayed.  If FORCE
7385 is a number, it is the line the article is to be displayed on."
7386   (interactive
7387    (list
7388     (completing-read
7389      "Article number or Message-ID: "
7390      (mapcar (lambda (number) (list (int-to-string number)))
7391              gnus-newsgroup-limit))
7392     current-prefix-arg
7393     t))
7394   (prog1
7395       (if (and (stringp article)
7396                (string-match "@" article))
7397           (gnus-summary-refer-article article)
7398         (when (stringp article)
7399           (setq article (string-to-number article)))
7400         (if (gnus-summary-goto-subject article force)
7401             (gnus-summary-display-article article all-headers)
7402           (gnus-message 4 "Couldn't go to article %s" article) nil))
7403     (gnus-summary-position-point)))
7404
7405 (defun gnus-summary-goto-last-article ()
7406   "Go to the previously read article."
7407   (interactive)
7408   (prog1
7409       (when gnus-last-article
7410         (gnus-summary-goto-article gnus-last-article nil t))
7411     (gnus-summary-position-point)))
7412
7413 (defun gnus-summary-pop-article (number)
7414   "Pop one article off the history and go to the previous.
7415 NUMBER articles will be popped off."
7416   (interactive "p")
7417   (let (to)
7418     (setq gnus-newsgroup-history
7419           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7420     (if to
7421         (gnus-summary-goto-article (car to) nil t)
7422       (error "Article history empty")))
7423   (gnus-summary-position-point))
7424
7425 ;; Summary commands and functions for limiting the summary buffer.
7426
7427 (defun gnus-summary-limit-to-articles (n)
7428   "Limit the summary buffer to the next N articles.
7429 If not given a prefix, use the process marked articles instead."
7430   (interactive "P")
7431   (prog1
7432       (let ((articles (gnus-summary-work-articles n)))
7433         (setq gnus-newsgroup-processable nil)
7434         (gnus-summary-limit articles))
7435     (gnus-summary-position-point)))
7436
7437 (defun gnus-summary-pop-limit (&optional total)
7438   "Restore the previous limit.
7439 If given a prefix, remove all limits."
7440   (interactive "P")
7441   (when total
7442     (setq gnus-newsgroup-limits
7443           (list (mapcar (lambda (h) (mail-header-number h))
7444                         gnus-newsgroup-headers))))
7445   (unless gnus-newsgroup-limits
7446     (error "No limit to pop"))
7447   (prog1
7448       (gnus-summary-limit nil 'pop)
7449     (gnus-summary-position-point)))
7450
7451 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7452   "Limit the summary buffer to articles that have subjects that match a regexp.
7453 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7454   (interactive
7455    (list (read-string (if current-prefix-arg
7456                           "Exclude subject (regexp): "
7457                         "Limit to subject (regexp): "))
7458          nil current-prefix-arg))
7459   (unless header
7460     (setq header "subject"))
7461   (when (not (equal "" subject))
7462     (prog1
7463         (let ((articles (gnus-summary-find-matching
7464                          (or header "subject") subject 'all nil nil
7465                          not-matching)))
7466           (unless articles
7467             (error "Found no matches for \"%s\"" subject))
7468           (gnus-summary-limit articles))
7469       (gnus-summary-position-point))))
7470
7471 (defun gnus-summary-limit-to-author (from &optional not-matching)
7472   "Limit the summary buffer to articles that have authors that match a regexp.
7473 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7474   (interactive
7475    (list (read-string (if current-prefix-arg
7476                           "Exclude author (regexp): "
7477                         "Limit to author (regexp): "))
7478          current-prefix-arg))
7479   (gnus-summary-limit-to-subject from "from" not-matching))
7480
7481 (defun gnus-summary-limit-to-age (age &optional younger-p)
7482   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7483 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7484 articles that are younger than AGE days."
7485   (interactive
7486    (let ((younger current-prefix-arg)
7487          (days-got nil)
7488          days)
7489      (while (not days-got)
7490        (setq days (if younger
7491                       (read-string "Limit to articles younger than (in days, older when negative): ")
7492                     (read-string
7493                      "Limit to articles older than (in days, younger when negative): ")))
7494        (when (> (length days) 0)
7495          (setq days (read days)))
7496        (if (numberp days)
7497            (progn
7498              (setq days-got t)
7499              (if (< days 0)
7500                  (progn
7501                    (setq younger (not younger))
7502                    (setq days (* days -1)))))
7503          (message "Please enter a number.")
7504          (sleep-for 1)))
7505      (list days younger)))
7506   (prog1
7507       (let ((data gnus-newsgroup-data)
7508             (cutoff (days-to-time age))
7509             articles d date is-younger)
7510         (while (setq d (pop data))
7511           (when (and (vectorp (gnus-data-header d))
7512                      (setq date (mail-header-date (gnus-data-header d))))
7513             (setq is-younger (time-less-p
7514                               (time-since (condition-case ()
7515                                               (date-to-time date)
7516                                             (error '(0 0))))
7517                               cutoff))
7518             (when (if younger-p
7519                       is-younger
7520                     (not is-younger))
7521               (push (gnus-data-number d) articles))))
7522         (gnus-summary-limit (nreverse articles)))
7523     (gnus-summary-position-point)))
7524
7525 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7526   "Limit the summary buffer to articles that match an 'extra' header."
7527   (interactive
7528    (let ((header
7529           (intern
7530            (gnus-completing-read-with-default
7531             (symbol-name (car gnus-extra-headers))
7532             (if current-prefix-arg
7533                 "Exclude extra header:"
7534               "Limit extra header:")
7535             (mapcar (lambda (x)
7536                       (cons (symbol-name x) x))
7537                     gnus-extra-headers)
7538             nil
7539             t))))
7540      (list header
7541            (read-string (format "%s header %s (regexp): "
7542                                 (if current-prefix-arg "Exclude" "Limit to")
7543                                 header))
7544            current-prefix-arg)))
7545   (when (not (equal "" regexp))
7546     (prog1
7547         (let ((articles (gnus-summary-find-matching
7548                          (cons 'extra header) regexp 'all nil nil
7549                          not-matching)))
7550           (unless articles
7551             (error "Found no matches for \"%s\"" regexp))
7552           (gnus-summary-limit articles))
7553       (gnus-summary-position-point))))
7554
7555 (defun gnus-summary-limit-to-display-predicate ()
7556   "Limit the summary buffer to the predicated in the `display' group parameter."
7557   (interactive)
7558   (unless gnus-newsgroup-display
7559     (error "There is no `display' group parameter"))
7560   (let (articles)
7561     (dolist (number gnus-newsgroup-articles)
7562       (when (funcall gnus-newsgroup-display)
7563         (push number articles)))
7564     (gnus-summary-limit articles))
7565   (gnus-summary-position-point))
7566
7567 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7568 (make-obsolete
7569  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7570
7571 (defun gnus-summary-limit-to-unread (&optional all)
7572   "Limit the summary buffer to articles that are not marked as read.
7573 If ALL is non-nil, limit strictly to unread articles."
7574   (interactive "P")
7575   (if all
7576       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7577     (gnus-summary-limit-to-marks
7578      ;; Concat all the marks that say that an article is read and have
7579      ;; those removed.
7580      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7581            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7582            gnus-low-score-mark gnus-expirable-mark
7583            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7584            gnus-duplicate-mark gnus-souped-mark)
7585      'reverse)))
7586
7587 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7588 (make-obsolete 'gnus-summary-delete-marked-with
7589                'gnus-summary-limit-exclude-marks)
7590
7591 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7592   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7593 If REVERSE, limit the summary buffer to articles that are marked
7594 with MARKS.  MARKS can either be a string of marks or a list of marks.
7595 Returns how many articles were removed."
7596   (interactive "sMarks: ")
7597   (gnus-summary-limit-to-marks marks t))
7598
7599 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7600   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7601 If REVERSE (the prefix), limit the summary buffer to articles that are
7602 not marked with MARKS.  MARKS can either be a string of marks or a
7603 list of marks.
7604 Returns how many articles were removed."
7605   (interactive "sMarks: \nP")
7606   (prog1
7607       (let ((data gnus-newsgroup-data)
7608             (marks (if (listp marks) marks
7609                      (append marks nil))) ; Transform to list.
7610             articles)
7611         (while data
7612           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7613                   (memq (gnus-data-mark (car data)) marks))
7614             (push (gnus-data-number (car data)) articles))
7615           (setq data (cdr data)))
7616         (gnus-summary-limit articles))
7617     (gnus-summary-position-point)))
7618
7619 (defun gnus-summary-limit-to-score (score)
7620   "Limit to articles with score at or above SCORE."
7621   (interactive "NLimit to articles with score of at least: ")
7622   (let ((data gnus-newsgroup-data)
7623         articles)
7624     (while data
7625       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7626                 score)
7627         (push (gnus-data-number (car data)) articles))
7628       (setq data (cdr data)))
7629     (prog1
7630         (gnus-summary-limit articles)
7631       (gnus-summary-position-point))))
7632
7633 (defun gnus-summary-limit-to-unseen ()
7634   "Limit to unseen articles."
7635   (interactive)
7636   (prog1
7637       (gnus-summary-limit gnus-newsgroup-unseen)
7638     (gnus-summary-position-point)))
7639
7640 (defun gnus-summary-limit-include-thread (id)
7641   "Display all the hidden articles that is in the thread with ID in it.
7642 When called interactively, ID is the Message-ID of the current
7643 article."
7644   (interactive (list (mail-header-id (gnus-summary-article-header))))
7645   (let ((articles (gnus-articles-in-thread
7646                    (gnus-id-to-thread (gnus-root-id id)))))
7647     (prog1
7648         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7649       (gnus-summary-limit-include-matching-articles
7650        "subject"
7651        (regexp-quote (gnus-simplify-subject-re
7652                       (mail-header-subject (gnus-id-to-header id)))))
7653       (gnus-summary-position-point))))
7654
7655 (defun gnus-summary-limit-include-matching-articles (header regexp)
7656   "Display all the hidden articles that have HEADERs that match REGEXP."
7657   (interactive (list (read-string "Match on header: ")
7658                      (read-string "Regexp: ")))
7659   (let ((articles (gnus-find-matching-articles header regexp)))
7660     (prog1
7661         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7662       (gnus-summary-position-point))))
7663
7664 (defun gnus-summary-insert-dormant-articles ()
7665   "Insert all the dormat articles for this group into the current buffer."
7666   (interactive)
7667   (let ((gnus-verbose (max 6 gnus-verbose)))
7668     (if (not gnus-newsgroup-dormant)
7669         (gnus-message 3 "No cached articles for this group")
7670       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7671
7672 (defun gnus-summary-limit-include-dormant ()
7673   "Display all the hidden articles that are marked as dormant.
7674 Note that this command only works on a subset of the articles currently
7675 fetched for this group."
7676   (interactive)
7677   (unless gnus-newsgroup-dormant
7678     (error "There are no dormant articles in this group"))
7679   (prog1
7680       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7681     (gnus-summary-position-point)))
7682
7683 (defun gnus-summary-limit-exclude-dormant ()
7684   "Hide all dormant articles."
7685   (interactive)
7686   (prog1
7687       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7688     (gnus-summary-position-point)))
7689
7690 (defun gnus-summary-limit-exclude-childless-dormant ()
7691   "Hide all dormant articles that have no children."
7692   (interactive)
7693   (let ((data (gnus-data-list t))
7694         articles d children)
7695     ;; Find all articles that are either not dormant or have
7696     ;; children.
7697     (while (setq d (pop data))
7698       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7699                 (and (setq children
7700                            (gnus-article-children (gnus-data-number d)))
7701                      (let (found)
7702                        (while children
7703                          (when (memq (car children) articles)
7704                            (setq children nil
7705                                  found t))
7706                          (pop children))
7707                        found)))
7708         (push (gnus-data-number d) articles)))
7709     ;; Do the limiting.
7710     (prog1
7711         (gnus-summary-limit articles)
7712       (gnus-summary-position-point))))
7713
7714 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7715   "Mark all unread excluded articles as read.
7716 If ALL, mark even excluded ticked and dormants as read."
7717   (interactive "P")
7718   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7719   (let ((articles (gnus-sorted-ndifference
7720                    (sort
7721                     (mapcar (lambda (h) (mail-header-number h))
7722                             gnus-newsgroup-headers)
7723                     '<)
7724                    gnus-newsgroup-limit))
7725         article)
7726     (setq gnus-newsgroup-unreads
7727           (gnus-sorted-intersection gnus-newsgroup-unreads
7728                                     gnus-newsgroup-limit))
7729     (if all
7730         (setq gnus-newsgroup-dormant nil
7731               gnus-newsgroup-marked nil
7732               gnus-newsgroup-reads
7733               (nconc
7734                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7735                gnus-newsgroup-reads))
7736       (while (setq article (pop articles))
7737         (unless (or (memq article gnus-newsgroup-dormant)
7738                     (memq article gnus-newsgroup-marked))
7739           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7740
7741 (defun gnus-summary-limit (articles &optional pop)
7742   (if pop
7743       ;; We pop the previous limit off the stack and use that.
7744       (setq articles (car gnus-newsgroup-limits)
7745             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7746     ;; We use the new limit, so we push the old limit on the stack.
7747     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7748   ;; Set the limit.
7749   (setq gnus-newsgroup-limit articles)
7750   (let ((total (length gnus-newsgroup-data))
7751         (data (gnus-data-find-list (gnus-summary-article-number)))
7752         (gnus-summary-mark-below nil)   ; Inhibit this.
7753         found)
7754     ;; This will do all the work of generating the new summary buffer
7755     ;; according to the new limit.
7756     (gnus-summary-prepare)
7757     ;; Hide any threads, possibly.
7758     (gnus-summary-maybe-hide-threads)
7759     ;; Try to return to the article you were at, or one in the
7760     ;; neighborhood.
7761     (when data
7762       ;; We try to find some article after the current one.
7763       (while data
7764         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7765           (setq data nil
7766                 found t))
7767         (setq data (cdr data))))
7768     (unless found
7769       ;; If there is no data, that means that we were after the last
7770       ;; article.  The same goes when we can't find any articles
7771       ;; after the current one.
7772       (goto-char (point-max))
7773       (gnus-summary-find-prev))
7774     (gnus-set-mode-line 'summary)
7775     ;; We return how many articles were removed from the summary
7776     ;; buffer as a result of the new limit.
7777     (- total (length gnus-newsgroup-data))))
7778
7779 (defsubst gnus-invisible-cut-children (threads)
7780   (let ((num 0))
7781     (while threads
7782       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7783         (incf num))
7784       (pop threads))
7785     (< num 2)))
7786
7787 (defsubst gnus-cut-thread (thread)
7788   "Go forwards in the thread until we find an article that we want to display."
7789   (when (or (eq gnus-fetch-old-headers 'some)
7790             (eq gnus-fetch-old-headers 'invisible)
7791             (numberp gnus-fetch-old-headers)
7792             (eq gnus-build-sparse-threads 'some)
7793             (eq gnus-build-sparse-threads 'more))
7794     ;; Deal with old-fetched headers and sparse threads.
7795     (while (and
7796             thread
7797             (or
7798              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7799              (gnus-summary-article-ancient-p
7800               (mail-header-number (car thread))))
7801             (if (or (<= (length (cdr thread)) 1)
7802                     (eq gnus-fetch-old-headers 'invisible))
7803                 (setq gnus-newsgroup-limit
7804                       (delq (mail-header-number (car thread))
7805                             gnus-newsgroup-limit)
7806                       thread (cadr thread))
7807               (when (gnus-invisible-cut-children (cdr thread))
7808                 (let ((th (cdr thread)))
7809                   (while th
7810                     (if (memq (mail-header-number (caar th))
7811                               gnus-newsgroup-limit)
7812                         (setq thread (car th)
7813                               th nil)
7814                       (setq th (cdr th))))))))))
7815   thread)
7816
7817 (defun gnus-cut-threads (threads)
7818   "Cut off all uninteresting articles from the beginning of threads."
7819   (when (or (eq gnus-fetch-old-headers 'some)
7820             (eq gnus-fetch-old-headers 'invisible)
7821             (numberp gnus-fetch-old-headers)
7822             (eq gnus-build-sparse-threads 'some)
7823             (eq gnus-build-sparse-threads 'more))
7824     (let ((th threads))
7825       (while th
7826         (setcar th (gnus-cut-thread (car th)))
7827         (setq th (cdr th)))))
7828   ;; Remove nixed out threads.
7829   (delq nil threads))
7830
7831 (defun gnus-summary-initial-limit (&optional show-if-empty)
7832   "Figure out what the initial limit is supposed to be on group entry.
7833 This entails weeding out unwanted dormants, low-scored articles,
7834 fetch-old-headers verbiage, and so on."
7835   ;; Most groups have nothing to remove.
7836   (if (or gnus-inhibit-limiting
7837           (and (null gnus-newsgroup-dormant)
7838                (eq gnus-newsgroup-display 'gnus-not-ignore)
7839                (not (eq gnus-fetch-old-headers 'some))
7840                (not (numberp gnus-fetch-old-headers))
7841                (not (eq gnus-fetch-old-headers 'invisible))
7842                (null gnus-summary-expunge-below)
7843                (not (eq gnus-build-sparse-threads 'some))
7844                (not (eq gnus-build-sparse-threads 'more))
7845                (null gnus-thread-expunge-below)
7846                (not gnus-use-nocem)))
7847       ()                                ; Do nothing.
7848     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7849     (setq gnus-newsgroup-limit nil)
7850     (mapatoms
7851      (lambda (node)
7852        (unless (car (symbol-value node))
7853          ;; These threads have no parents -- they are roots.
7854          (let ((nodes (cdr (symbol-value node)))
7855                thread)
7856            (while nodes
7857              (if (and gnus-thread-expunge-below
7858                       (< (gnus-thread-total-score (car nodes))
7859                          gnus-thread-expunge-below))
7860                  (gnus-expunge-thread (pop nodes))
7861                (setq thread (pop nodes))
7862                (gnus-summary-limit-children thread))))))
7863      gnus-newsgroup-dependencies)
7864     ;; If this limitation resulted in an empty group, we might
7865     ;; pop the previous limit and use it instead.
7866     (when (and (not gnus-newsgroup-limit)
7867                show-if-empty)
7868       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7869     gnus-newsgroup-limit))
7870
7871 (defun gnus-summary-limit-children (thread)
7872   "Return 1 if this subthread is visible and 0 if it is not."
7873   ;; First we get the number of visible children to this thread.  This
7874   ;; is done by recursing down the thread using this function, so this
7875   ;; will really go down to a leaf article first, before slowly
7876   ;; working its way up towards the root.
7877   (when thread
7878     (let* ((max-lisp-eval-depth 5000)
7879            (children
7880            (if (cdr thread)
7881                (apply '+ (mapcar 'gnus-summary-limit-children
7882                                  (cdr thread)))
7883              0))
7884           (number (mail-header-number (car thread)))
7885           score)
7886       (if (and
7887            (not (memq number gnus-newsgroup-marked))
7888            (or
7889             ;; If this article is dormant and has absolutely no visible
7890             ;; children, then this article isn't visible.
7891             (and (memq number gnus-newsgroup-dormant)
7892                  (zerop children))
7893             ;; If this is "fetch-old-headered" and there is no
7894             ;; visible children, then we don't want this article.
7895             (and (or (eq gnus-fetch-old-headers 'some)
7896                      (numberp gnus-fetch-old-headers))
7897                  (gnus-summary-article-ancient-p number)
7898                  (zerop children))
7899             ;; If this is "fetch-old-headered" and `invisible', then
7900             ;; we don't want this article.
7901             (and (eq gnus-fetch-old-headers 'invisible)
7902                  (gnus-summary-article-ancient-p number))
7903             ;; If this is a sparsely inserted article with no children,
7904             ;; we don't want it.
7905             (and (eq gnus-build-sparse-threads 'some)
7906                  (gnus-summary-article-sparse-p number)
7907                  (zerop children))
7908             ;; If we use expunging, and this article is really
7909             ;; low-scored, then we don't want this article.
7910             (when (and gnus-summary-expunge-below
7911                        (< (setq score
7912                                 (or (cdr (assq number gnus-newsgroup-scored))
7913                                     gnus-summary-default-score))
7914                           gnus-summary-expunge-below))
7915               ;; We increase the expunge-tally here, but that has
7916               ;; nothing to do with the limits, really.
7917               (incf gnus-newsgroup-expunged-tally)
7918               ;; We also mark as read here, if that's wanted.
7919               (when (and gnus-summary-mark-below
7920                          (< score gnus-summary-mark-below))
7921                 (setq gnus-newsgroup-unreads
7922                       (delq number gnus-newsgroup-unreads))
7923                 (if gnus-newsgroup-auto-expire
7924                     (push number gnus-newsgroup-expirable)
7925                   (push (cons number gnus-low-score-mark)
7926                         gnus-newsgroup-reads)))
7927               t)
7928             ;; Do the `display' group parameter.
7929             (and gnus-newsgroup-display
7930                  (not (funcall gnus-newsgroup-display)))
7931             ;; Check NoCeM things.
7932             (if (and gnus-use-nocem
7933                      (gnus-nocem-unwanted-article-p
7934                       (mail-header-id (car thread))))
7935                 (progn
7936                   (setq gnus-newsgroup-unreads
7937                         (delq number gnus-newsgroup-unreads))
7938                   t))))
7939           ;; Nope, invisible article.
7940           0
7941         ;; Ok, this article is to be visible, so we add it to the limit
7942         ;; and return 1.
7943         (push number gnus-newsgroup-limit)
7944         1))))
7945
7946 (defun gnus-expunge-thread (thread)
7947   "Mark all articles in THREAD as read."
7948   (let* ((number (mail-header-number (car thread))))
7949     (incf gnus-newsgroup-expunged-tally)
7950     ;; We also mark as read here, if that's wanted.
7951     (setq gnus-newsgroup-unreads
7952           (delq number gnus-newsgroup-unreads))
7953     (if gnus-newsgroup-auto-expire
7954         (push number gnus-newsgroup-expirable)
7955       (push (cons number gnus-low-score-mark)
7956             gnus-newsgroup-reads)))
7957   ;; Go recursively through all subthreads.
7958   (mapcar 'gnus-expunge-thread (cdr thread)))
7959
7960 ;; Summary article oriented commands
7961
7962 (defun gnus-summary-refer-parent-article (n)
7963   "Refer parent article N times.
7964 If N is negative, go to ancestor -N instead.
7965 The difference between N and the number of articles fetched is returned."
7966   (interactive "p")
7967   (let ((skip 1)
7968         error header ref)
7969     (when (not (natnump n))
7970       (setq skip (abs n)
7971             n 1))
7972     (while (and (> n 0)
7973                 (not error))
7974       (setq header (gnus-summary-article-header))
7975       (if (and (eq (mail-header-number header)
7976                    (cdr gnus-article-current))
7977                (equal gnus-newsgroup-name
7978                       (car gnus-article-current)))
7979           ;; If we try to find the parent of the currently
7980           ;; displayed article, then we take a look at the actual
7981           ;; References header, since this is slightly more
7982           ;; reliable than the References field we got from the
7983           ;; server.
7984           (save-excursion
7985             (set-buffer gnus-original-article-buffer)
7986             (nnheader-narrow-to-headers)
7987             (unless (setq ref (message-fetch-field "references"))
7988               (setq ref (message-fetch-field "in-reply-to")))
7989             (widen))
7990         (setq ref
7991               ;; It's not the current article, so we take a bet on
7992               ;; the value we got from the server.
7993               (mail-header-references header)))
7994       (if (and ref
7995                (not (equal ref "")))
7996           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7997             (gnus-message 1 "Couldn't find parent"))
7998         (gnus-message 1 "No references in article %d"
7999                       (gnus-summary-article-number))
8000         (setq error t))
8001       (decf n))
8002     (gnus-summary-position-point)
8003     n))
8004
8005 (defun gnus-summary-refer-references ()
8006   "Fetch all articles mentioned in the References header.
8007 Return the number of articles fetched."
8008   (interactive)
8009   (let ((ref (mail-header-references (gnus-summary-article-header)))
8010         (current (gnus-summary-article-number))
8011         (n 0))
8012     (if (or (not ref)
8013             (equal ref ""))
8014         (error "No References in the current article")
8015       ;; For each Message-ID in the References header...
8016       (while (string-match "<[^>]*>" ref)
8017         (incf n)
8018         ;; ... fetch that article.
8019         (gnus-summary-refer-article
8020          (prog1 (match-string 0 ref)
8021            (setq ref (substring ref (match-end 0))))))
8022       (gnus-summary-goto-subject current)
8023       (gnus-summary-position-point)
8024       n)))
8025
8026 (defun gnus-summary-refer-thread (&optional limit)
8027   "Fetch all articles in the current thread.
8028 If LIMIT (the numerical prefix), fetch that many old headers instead
8029 of what's specified by the `gnus-refer-thread-limit' variable."
8030   (interactive "P")
8031   (let ((id (mail-header-id (gnus-summary-article-header)))
8032         (limit (if limit (prefix-numeric-value limit)
8033                  gnus-refer-thread-limit)))
8034     (unless (eq gnus-fetch-old-headers 'invisible)
8035       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8036       ;; Retrieve the headers and read them in.
8037       (if (eq (if (numberp limit)
8038                   (gnus-retrieve-headers
8039                    (list (min
8040                           (+ (mail-header-number
8041                               (gnus-summary-article-header))
8042                              limit)
8043                           gnus-newsgroup-end))
8044                    gnus-newsgroup-name (* limit 2))
8045                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8046                 ;; headers.
8047                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8048                                        gnus-newsgroup-name limit))
8049               'nov)
8050           (gnus-build-all-threads)
8051         (error "Can't fetch thread from backends that don't support NOV"))
8052       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8053     (gnus-summary-limit-include-thread id)))
8054
8055 (defun gnus-summary-refer-article (message-id)
8056   "Fetch an article specified by MESSAGE-ID."
8057   (interactive "sMessage-ID: ")
8058   (when (and (stringp message-id)
8059              (not (zerop (length message-id))))
8060     ;; Construct the correct Message-ID if necessary.
8061     ;; Suggested by tale@pawl.rpi.edu.
8062     (unless (string-match "^<" message-id)
8063       (setq message-id (concat "<" message-id)))
8064     (unless (string-match ">$" message-id)
8065       (setq message-id (concat message-id ">")))
8066     (let* ((header (gnus-id-to-header message-id))
8067            (sparse (and header
8068                         (gnus-summary-article-sparse-p
8069                          (mail-header-number header))
8070                         (memq (mail-header-number header)
8071                               gnus-newsgroup-limit)))
8072            number)
8073       (cond
8074        ;; If the article is present in the buffer we just go to it.
8075        ((and header
8076              (or (not (gnus-summary-article-sparse-p
8077                        (mail-header-number header)))
8078                  sparse))
8079         (prog1
8080             (gnus-summary-goto-article
8081              (mail-header-number header) nil t)
8082           (when sparse
8083             (gnus-summary-update-article (mail-header-number header)))))
8084        (t
8085         ;; We fetch the article.
8086         (catch 'found
8087           (dolist (gnus-override-method (gnus-refer-article-methods))
8088             (when (and (gnus-check-server gnus-override-method)
8089                        ;; Fetch the header,
8090                        (setq number (gnus-summary-insert-subject message-id)))
8091               ;; and display the article.
8092               (gnus-summary-select-article nil nil nil number)
8093               (throw 'found t)))
8094           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8095
8096 (defun gnus-refer-article-methods ()
8097   "Return a list of referable methods."
8098   (cond
8099    ;; No method, so we default to current and native.
8100    ((null gnus-refer-article-method)
8101     (list gnus-current-select-method gnus-select-method))
8102    ;; Current.
8103    ((eq 'current gnus-refer-article-method)
8104     (list gnus-current-select-method))
8105    ;; List of select methods.
8106    ((not (and (symbolp (car gnus-refer-article-method))
8107               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8108     (let (out)
8109       (dolist (method gnus-refer-article-method)
8110         (push (if (eq 'current method)
8111                   gnus-current-select-method
8112                 method)
8113               out))
8114       (nreverse out)))
8115    ;; One single select method.
8116    (t
8117     (list gnus-refer-article-method))))
8118
8119 (defun gnus-summary-edit-parameters ()
8120   "Edit the group parameters of the current group."
8121   (interactive)
8122   (gnus-group-edit-group gnus-newsgroup-name 'params))
8123
8124 (defun gnus-summary-customize-parameters ()
8125   "Customize the group parameters of the current group."
8126   (interactive)
8127   (gnus-group-customize gnus-newsgroup-name))
8128
8129 (defun gnus-summary-enter-digest-group (&optional force)
8130   "Enter an nndoc group based on the current article.
8131 If FORCE, force a digest interpretation.  If not, try
8132 to guess what the document format is."
8133   (interactive "P")
8134   (let ((conf gnus-current-window-configuration))
8135     (save-excursion
8136       (gnus-summary-select-article))
8137     (setq gnus-current-window-configuration conf)
8138     (let* ((name (format "%s-%d"
8139                          (gnus-group-prefixed-name
8140                           gnus-newsgroup-name (list 'nndoc ""))
8141                          (save-excursion
8142                            (set-buffer gnus-summary-buffer)
8143                            gnus-current-article)))
8144            (ogroup gnus-newsgroup-name)
8145            (params (append (gnus-info-params (gnus-get-info ogroup))
8146                            (list (cons 'to-group ogroup))
8147                            (list (cons 'save-article-group ogroup))))
8148            (case-fold-search t)
8149            (buf (current-buffer))
8150            dig to-address)
8151       (save-excursion
8152         (set-buffer gnus-original-article-buffer)
8153         ;; Have the digest group inherit the main mail address of
8154         ;; the parent article.
8155         (when (setq to-address (or (gnus-fetch-field "reply-to")
8156                                    (gnus-fetch-field "from")))
8157           (setq params (append
8158                         (list (cons 'to-address
8159                                     (funcall gnus-decode-encoded-word-function
8160                                              to-address))))))
8161         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8162         (insert-buffer-substring gnus-original-article-buffer)
8163         ;; Remove lines that may lead nndoc to misinterpret the
8164         ;; document type.
8165         (narrow-to-region
8166          (goto-char (point-min))
8167          (or (search-forward "\n\n" nil t) (point)))
8168         (goto-char (point-min))
8169         (delete-matching-lines "^Path:\\|^From ")
8170         (widen))
8171       (unwind-protect
8172           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8173                     (gnus-newsgroup-ephemeral-ignored-charsets
8174                      gnus-newsgroup-ignored-charsets))
8175                 (gnus-group-read-ephemeral-group
8176                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8177                               (nndoc-article-type
8178                                ,(if force 'mbox 'guess)))
8179                  t nil nil nil
8180                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8181                                                         "ADAPT")))))
8182               ;; Make all postings to this group go to the parent group.
8183               (nconc (gnus-info-params (gnus-get-info name))
8184                      params)
8185             ;; Couldn't select this doc group.
8186             (switch-to-buffer buf)
8187             (gnus-set-global-variables)
8188             (gnus-configure-windows 'summary)
8189             (gnus-message 3 "Article couldn't be entered?"))
8190         (kill-buffer dig)))))
8191
8192 (defun gnus-summary-read-document (n)
8193   "Open a new group based on the current article(s).
8194 This will allow you to read digests and other similar
8195 documents as newsgroups.
8196 Obeys the standard process/prefix convention."
8197   (interactive "P")
8198   (let* ((articles (gnus-summary-work-articles n))
8199          (ogroup gnus-newsgroup-name)
8200          (params (append (gnus-info-params (gnus-get-info ogroup))
8201                          (list (cons 'to-group ogroup))))
8202          article group egroup groups vgroup)
8203     (while (setq article (pop articles))
8204       (setq group (format "%s-%d" gnus-newsgroup-name article))
8205       (gnus-summary-remove-process-mark article)
8206       (when (gnus-summary-display-article article)
8207         (save-excursion
8208           (with-temp-buffer
8209             (insert-buffer-substring gnus-original-article-buffer)
8210             ;; Remove some headers that may lead nndoc to make
8211             ;; the wrong guess.
8212             (message-narrow-to-head)
8213             (goto-char (point-min))
8214             (delete-matching-lines "^\\(Path\\):\\|^From ")
8215             (widen)
8216             (if (setq egroup
8217                       (gnus-group-read-ephemeral-group
8218                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8219                                      (nndoc-article-type guess))
8220                        t nil t))
8221                 (progn
8222             ;; Make all postings to this group go to the parent group.
8223                   (nconc (gnus-info-params (gnus-get-info egroup))
8224                          params)
8225                   (push egroup groups))
8226               ;; Couldn't select this doc group.
8227               (gnus-error 3 "Article couldn't be entered"))))))
8228     ;; Now we have selected all the documents.
8229     (cond
8230      ((not groups)
8231       (error "None of the articles could be interpreted as documents"))
8232      ((gnus-group-read-ephemeral-group
8233        (setq vgroup (format
8234                      "nnvirtual:%s-%s" gnus-newsgroup-name
8235                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8236        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8237        t
8238        (cons (current-buffer) 'summary)))
8239      (t
8240       (error "Couldn't select virtual nndoc group")))))
8241
8242 (defun gnus-summary-isearch-article (&optional regexp-p)
8243   "Do incremental search forward on the current article.
8244 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8245   (interactive "P")
8246   (gnus-summary-select-article)
8247   (gnus-configure-windows 'article)
8248   (gnus-eval-in-buffer-window gnus-article-buffer
8249     (save-restriction
8250       (widen)
8251       (isearch-forward regexp-p))))
8252
8253 (defun gnus-summary-search-article-forward (regexp &optional backward)
8254   "Search for an article containing REGEXP forward.
8255 If BACKWARD, search backward instead."
8256   (interactive
8257    (list (read-string
8258           (format "Search article %s (regexp%s): "
8259                   (if current-prefix-arg "backward" "forward")
8260                   (if gnus-last-search-regexp
8261                       (concat ", default " gnus-last-search-regexp)
8262                     "")))
8263          current-prefix-arg))
8264   (if (string-equal regexp "")
8265       (setq regexp (or gnus-last-search-regexp ""))
8266     (setq gnus-last-search-regexp regexp)
8267     (setq gnus-article-before-search gnus-current-article))
8268   ;; Intentionally set gnus-last-article.
8269   (setq gnus-last-article gnus-article-before-search)
8270   (let ((gnus-last-article gnus-last-article))
8271     (if (gnus-summary-search-article regexp backward)
8272         (gnus-summary-show-thread)
8273       (error "Search failed: \"%s\"" regexp))))
8274
8275 (defun gnus-summary-search-article-backward (regexp)
8276   "Search for an article containing REGEXP backward."
8277   (interactive
8278    (list (read-string
8279           (format "Search article backward (regexp%s): "
8280                   (if gnus-last-search-regexp
8281                       (concat ", default " gnus-last-search-regexp)
8282                     "")))))
8283   (gnus-summary-search-article-forward regexp 'backward))
8284
8285 (defun gnus-summary-search-article (regexp &optional backward)
8286   "Search for an article containing REGEXP.
8287 Optional argument BACKWARD means do search for backward.
8288 `gnus-select-article-hook' is not called during the search."
8289   ;; We have to require this here to make sure that the following
8290   ;; dynamic binding isn't shadowed by autoloading.
8291   (require 'gnus-async)
8292   (require 'gnus-art)
8293   (let ((gnus-select-article-hook nil)  ;Disable hook.
8294         (gnus-article-prepare-hook nil)
8295         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8296         (gnus-use-article-prefetch nil)
8297         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8298         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8299         (gnus-visual nil)
8300         (gnus-keep-backlog nil)
8301         (gnus-break-pages nil)
8302         (gnus-summary-display-arrow nil)
8303         (gnus-updated-mode-lines nil)
8304         (gnus-auto-center-summary nil)
8305         (sum (current-buffer))
8306         (gnus-display-mime-function nil)
8307         (found nil)
8308         point)
8309     (gnus-save-hidden-threads
8310       (gnus-summary-select-article)
8311       (set-buffer gnus-article-buffer)
8312       (goto-char (window-point (get-buffer-window (current-buffer))))
8313       (when backward
8314         (forward-line -1))
8315       (while (not found)
8316         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8317         (if (if backward
8318                 (re-search-backward regexp nil t)
8319               (re-search-forward regexp nil t))
8320             ;; We found the regexp.
8321             (progn
8322               (setq found 'found)
8323               (beginning-of-line)
8324               (set-window-start
8325                (get-buffer-window (current-buffer))
8326                (point))
8327               (forward-line 1)
8328               (set-window-point
8329                (get-buffer-window (current-buffer))
8330                (point))
8331               (set-buffer sum)
8332               (setq point (point)))
8333           ;; We didn't find it, so we go to the next article.
8334           (set-buffer sum)
8335           (setq found 'not)
8336           (while (eq found 'not)
8337             (if (not (if backward (gnus-summary-find-prev)
8338                        (gnus-summary-find-next)))
8339                 ;; No more articles.
8340                 (setq found t)
8341               ;; Select the next article and adjust point.
8342               (unless (gnus-summary-article-sparse-p
8343                        (gnus-summary-article-number))
8344                 (setq found nil)
8345                 (gnus-summary-select-article)
8346                 (set-buffer gnus-article-buffer)
8347                 (widen)
8348                 (goto-char (if backward (point-max) (point-min))))))))
8349       (gnus-message 7 ""))
8350     ;; Return whether we found the regexp.
8351     (when (eq found 'found)
8352       (goto-char point)
8353       (gnus-summary-show-thread)
8354       (gnus-summary-goto-subject gnus-current-article)
8355       (gnus-summary-position-point)
8356       t)))
8357
8358 (defun gnus-find-matching-articles (header regexp)
8359   "Return a list of all articles that match REGEXP on HEADER.
8360 This search includes all articles in the current group that Gnus has
8361 fetched headers for, whether they are displayed or not."
8362   (let ((articles nil)
8363         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8364         (case-fold-search t))
8365     (dolist (header gnus-newsgroup-headers)
8366       (when (string-match regexp (funcall func header))
8367         (push (mail-header-number header) articles)))
8368     (nreverse articles)))
8369
8370 (defun gnus-summary-find-matching (header regexp &optional backward unread
8371                                           not-case-fold not-matching)
8372   "Return a list of all articles that match REGEXP on HEADER.
8373 The search stars on the current article and goes forwards unless
8374 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8375 If UNREAD is non-nil, only unread articles will
8376 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8377 in the comparisons. If NOT-MATCHING, return a list of all articles that
8378 not match REGEXP on HEADER."
8379   (let ((case-fold-search (not not-case-fold))
8380         articles d func)
8381     (if (consp header)
8382         (if (eq (car header) 'extra)
8383             (setq func
8384                   `(lambda (h)
8385                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8386                          "")))
8387           (error "%s is an invalid header" header))
8388       (unless (fboundp (intern (concat "mail-header-" header)))
8389         (error "%s is not a valid header" header))
8390       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8391     (dolist (d (if (eq backward 'all)
8392                    gnus-newsgroup-data
8393                  (gnus-data-find-list
8394                   (gnus-summary-article-number)
8395                   (gnus-data-list backward))))
8396       (when (and (or (not unread)       ; We want all articles...
8397                      (gnus-data-unread-p d)) ; Or just unreads.
8398                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8399                  (if not-matching
8400                      (not (string-match
8401                            regexp
8402                            (funcall func (gnus-data-header d))))
8403                    (string-match regexp
8404                                  (funcall func (gnus-data-header d)))))
8405         (push (gnus-data-number d) articles))) ; Success!
8406     (nreverse articles)))
8407
8408 (defun gnus-summary-execute-command (header regexp command &optional backward)
8409   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8410 If HEADER is an empty string (or nil), the match is done on the entire
8411 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8412   (interactive
8413    (list (let ((completion-ignore-case t))
8414            (completing-read
8415             "Header name: "
8416             (mapcar (lambda (header) (list (format "%s" header)))
8417                     (append
8418                      '("Number" "Subject" "From" "Lines" "Date"
8419                        "Message-ID" "Xref" "References" "Body")
8420                      gnus-extra-headers))
8421             nil 'require-match))
8422          (read-string "Regexp: ")
8423          (read-key-sequence "Command: ")
8424          current-prefix-arg))
8425   (when (equal header "Body")
8426     (setq header ""))
8427   ;; Hidden thread subtrees must be searched as well.
8428   (gnus-summary-show-all-threads)
8429   ;; We don't want to change current point nor window configuration.
8430   (save-excursion
8431     (save-window-excursion
8432       (let (gnus-visual
8433             gnus-treat-strip-trailing-blank-lines
8434             gnus-treat-strip-leading-blank-lines
8435             gnus-treat-strip-multiple-blank-lines
8436             gnus-treat-hide-boring-headers
8437             gnus-treat-fold-newsgroups
8438             gnus-article-prepare-hook)
8439         (gnus-message 6 "Executing %s..." (key-description command))
8440         ;; We'd like to execute COMMAND interactively so as to give arguments.
8441         (gnus-execute header regexp
8442                       `(call-interactively ',(key-binding command))
8443                       backward)
8444         (gnus-message 6 "Executing %s...done" (key-description command))))))
8445
8446 (defun gnus-summary-beginning-of-article ()
8447   "Scroll the article back to the beginning."
8448   (interactive)
8449   (gnus-summary-select-article)
8450   (gnus-configure-windows 'article)
8451   (gnus-eval-in-buffer-window gnus-article-buffer
8452     (widen)
8453     (goto-char (point-min))
8454     (when gnus-page-broken
8455       (gnus-narrow-to-page))))
8456
8457 (defun gnus-summary-end-of-article ()
8458   "Scroll to the end of the article."
8459   (interactive)
8460   (gnus-summary-select-article)
8461   (gnus-configure-windows 'article)
8462   (gnus-eval-in-buffer-window gnus-article-buffer
8463     (widen)
8464     (goto-char (point-max))
8465     (recenter -3)
8466     (when gnus-page-broken
8467       (gnus-narrow-to-page))))
8468
8469 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8470   "Truncate to LEN and quote all \"(\"'s in STRING."
8471   (gnus-replace-in-string (if (and len (> (length string) len))
8472                               (substring string 0 len)
8473                             string)
8474                           "[()]" "\\\\\\&"))
8475
8476 (defun gnus-summary-print-article (&optional filename n)
8477   "Generate and print a PostScript image of the N next (mail) articles.
8478
8479 If N is negative, print the N previous articles.  If N is nil and articles
8480 have been marked with the process mark, print these instead.
8481
8482 If the optional first argument FILENAME is nil, send the image to the
8483 printer.  If FILENAME is a string, save the PostScript image in a file with
8484 that name.  If FILENAME is a number, prompt the user for the name of the file
8485 to save in."
8486   (interactive (list (ps-print-preprint current-prefix-arg)))
8487   (dolist (article (gnus-summary-work-articles n))
8488     (gnus-summary-select-article nil nil 'pseudo article)
8489     (gnus-eval-in-buffer-window gnus-article-buffer
8490       (gnus-print-buffer))
8491     (gnus-summary-remove-process-mark article))
8492   (ps-despool filename))
8493
8494 (defun gnus-print-buffer ()
8495   (let ((buffer (generate-new-buffer " *print*")))
8496     (unwind-protect
8497         (progn
8498           (copy-to-buffer buffer (point-min) (point-max))
8499           (set-buffer buffer)
8500           (gnus-article-delete-invisible-text)
8501           (gnus-remove-text-with-property 'gnus-decoration)
8502           (when (gnus-visual-p 'article-highlight 'highlight)
8503             ;; Copy-to-buffer doesn't copy overlay.  So redo
8504             ;; highlight.
8505             (let ((gnus-article-buffer buffer))
8506               (gnus-article-highlight-citation t)
8507               (gnus-article-highlight-signature)))
8508           (let ((ps-left-header
8509                  (list
8510                   (concat "("
8511                           (gnus-summary-print-truncate-and-quote
8512                            (mail-header-subject gnus-current-headers)
8513                            66) ")")
8514                   (concat "("
8515                           (gnus-summary-print-truncate-and-quote
8516                            (mail-header-from gnus-current-headers)
8517                            45) ")")))
8518                 (ps-right-header
8519                  (list
8520                   "/pagenumberstring load"
8521                   (concat "("
8522                           (mail-header-date gnus-current-headers) ")"))))
8523             (gnus-run-hooks 'gnus-ps-print-hook)
8524             (save-excursion
8525               (if window-system
8526                   (ps-spool-buffer-with-faces)
8527                 (ps-spool-buffer)))))
8528       (kill-buffer buffer))))
8529
8530 (defun gnus-summary-show-article (&optional arg)
8531   "Force redisplaying of the current article.
8532 If ARG (the prefix) is a number, show the article with the charset
8533 defined in `gnus-summary-show-article-charset-alist', or the charset
8534 input.
8535 If ARG (the prefix) is non-nil and not a number, show the raw article
8536 without any article massaging functions being run.  Normally, the key strokes
8537 are `C-u g'."
8538   (interactive "P")
8539   (cond
8540    ((numberp arg)
8541     (gnus-summary-show-article t)
8542     (let ((gnus-newsgroup-charset
8543            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8544                (mm-read-coding-system
8545                 "View as charset: " ;; actually it is coding system.
8546                 (save-excursion
8547                   (set-buffer gnus-article-buffer)
8548                   (mm-detect-coding-region (point) (point-max))))))
8549           (gnus-newsgroup-ignored-charsets 'gnus-all))
8550       (gnus-summary-select-article nil 'force)
8551       (let ((deps gnus-newsgroup-dependencies)
8552             head header lines)
8553         (save-excursion
8554           (set-buffer gnus-original-article-buffer)
8555           (save-restriction
8556             (message-narrow-to-head)
8557             (setq head (buffer-string))
8558             (goto-char (point-min))
8559             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8560               (goto-char (point-max))
8561               (widen)
8562               (setq lines (1- (count-lines (point) (point-max))))))
8563           (with-temp-buffer
8564             (insert (format "211 %d Article retrieved.\n"
8565                             (cdr gnus-article-current)))
8566             (insert head)
8567             (if lines (insert (format "Lines: %d\n" lines)))
8568             (insert ".\n")
8569             (let ((nntp-server-buffer (current-buffer)))
8570               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8571         (gnus-data-set-header
8572          (gnus-data-find (cdr gnus-article-current))
8573          header)
8574         (gnus-summary-update-article-line
8575          (cdr gnus-article-current) header)
8576         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8577           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8578    ((not arg)
8579     ;; Select the article the normal way.
8580     (gnus-summary-select-article nil 'force))
8581    (t
8582     ;; We have to require this here to make sure that the following
8583     ;; dynamic binding isn't shadowed by autoloading.
8584     (require 'gnus-async)
8585     (require 'gnus-art)
8586     ;; Bind the article treatment functions to nil.
8587     (let ((gnus-have-all-headers t)
8588           gnus-article-prepare-hook
8589           gnus-article-decode-hook
8590           gnus-display-mime-function
8591           gnus-break-pages)
8592       ;; Destroy any MIME parts.
8593       (when (gnus-buffer-live-p gnus-article-buffer)
8594         (save-excursion
8595           (set-buffer gnus-article-buffer)
8596           (mm-destroy-parts gnus-article-mime-handles)
8597           ;; Set it to nil for safety reason.
8598           (setq gnus-article-mime-handle-alist nil)
8599           (setq gnus-article-mime-handles nil)))
8600       (gnus-summary-select-article nil 'force))))
8601   (gnus-summary-goto-subject gnus-current-article)
8602   (gnus-summary-position-point))
8603
8604 (defun gnus-summary-show-raw-article ()
8605   "Show the raw article without any article massaging functions being run."
8606   (interactive)
8607   (gnus-summary-show-article t))
8608
8609 (defun gnus-summary-verbose-headers (&optional arg)
8610   "Toggle permanent full header display.
8611 If ARG is a positive number, turn header display on.
8612 If ARG is a negative number, turn header display off."
8613   (interactive "P")
8614   (setq gnus-show-all-headers
8615         (cond ((or (not (numberp arg))
8616                    (zerop arg))
8617                (not gnus-show-all-headers))
8618               ((natnump arg)
8619                t)))
8620   (gnus-summary-show-article))
8621
8622 (defun gnus-summary-toggle-header (&optional arg)
8623   "Show the headers if they are hidden, or hide them if they are shown.
8624 If ARG is a positive number, show the entire header.
8625 If ARG is a negative number, hide the unwanted header lines."
8626   (interactive "P")
8627   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8628                      (get-buffer-window gnus-article-buffer t))))
8629     (with-current-buffer gnus-article-buffer
8630       (widen)
8631       (article-narrow-to-head)
8632       (let* ((buffer-read-only nil)
8633              (inhibit-point-motion-hooks t)
8634              (hidden (if (numberp arg)
8635                          (>= arg 0)
8636                        (gnus-article-hidden-text-p 'headers)))
8637              s e)
8638         (delete-region (point-min) (point-max))
8639         (with-current-buffer gnus-original-article-buffer
8640           (goto-char (setq s (point-min)))
8641           (setq e (if (search-forward "\n\n" nil t)
8642                       (1- (point))
8643                     (point-max))))
8644         (insert-buffer-substring gnus-original-article-buffer s e)
8645         (run-hooks 'gnus-article-decode-hook)
8646         (if hidden
8647             (let ((gnus-treat-hide-headers nil)
8648                   (gnus-treat-hide-boring-headers nil))
8649               (gnus-delete-wash-type 'headers)
8650               (gnus-treat-article 'head))
8651           (gnus-treat-article 'head))
8652         (widen)
8653         (if window
8654             (set-window-start window (goto-char (point-min))))
8655         (setq gnus-page-broken
8656               (when gnus-break-pages
8657                 (gnus-narrow-to-page)
8658                 t))
8659         (gnus-set-mode-line 'article)))))
8660
8661 (defun gnus-summary-show-all-headers ()
8662   "Make all header lines visible."
8663   (interactive)
8664   (gnus-summary-toggle-header 1))
8665
8666 (defun gnus-summary-caesar-message (&optional arg)
8667   "Caesar rotate the current article by 13.
8668 The numerical prefix specifies how many places to rotate each letter
8669 forward."
8670   (interactive "P")
8671   (gnus-summary-select-article)
8672   (let ((mail-header-separator ""))
8673     (gnus-eval-in-buffer-window gnus-article-buffer
8674       (save-restriction
8675         (widen)
8676         (let ((start (window-start))
8677               buffer-read-only)
8678           (message-caesar-buffer-body arg)
8679           (set-window-start (get-buffer-window (current-buffer)) start))))))
8680
8681 (autoload 'unmorse-region "morse"
8682   "Convert morse coded text in region to ordinary ASCII text."
8683   t)
8684
8685 (defun gnus-summary-morse-message (&optional arg)
8686   "Morse decode the current article."
8687   (interactive "P")
8688   (gnus-summary-select-article)
8689   (let ((mail-header-separator ""))
8690     (gnus-eval-in-buffer-window gnus-article-buffer
8691       (save-excursion
8692         (save-restriction
8693           (widen)
8694           (let ((pos (window-start))
8695                 buffer-read-only)
8696             (goto-char (point-min))
8697             (when (message-goto-body)
8698               (gnus-narrow-to-body))
8699             (goto-char (point-min))
8700             (while (re-search-forward "·" (point-max) t)
8701               (replace-match "."))
8702             (unmorse-region (point-min) (point-max))
8703             (widen)
8704             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8705
8706 (defun gnus-summary-stop-page-breaking ()
8707   "Stop page breaking in the current article."
8708   (interactive)
8709   (gnus-summary-select-article)
8710   (gnus-eval-in-buffer-window gnus-article-buffer
8711     (widen)
8712     (when (gnus-visual-p 'page-marker)
8713       (let ((buffer-read-only nil))
8714         (gnus-remove-text-with-property 'gnus-prev)
8715         (gnus-remove-text-with-property 'gnus-next))
8716       (setq gnus-page-broken nil))))
8717
8718 (defun gnus-summary-move-article (&optional n to-newsgroup
8719                                             select-method action)
8720   "Move the current article to a different newsgroup.
8721 If N is a positive number, move the N next articles.
8722 If N is a negative number, move the N previous articles.
8723 If N is nil and any articles have been marked with the process mark,
8724 move those articles instead.
8725 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8726 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8727 re-spool using this method.
8728
8729 When called interactively with TO-NEWSGROUP being nil, the value of
8730 the variable `gnus-move-split-methods' is used for finding a default
8731 for the target newsgroup.
8732
8733 For this function to work, both the current newsgroup and the
8734 newsgroup that you want to move to have to support the `request-move'
8735 and `request-accept' functions.
8736
8737 ACTION can be either `move' (the default), `crosspost' or `copy'."
8738   (interactive "P")
8739   (unless action
8740     (setq action 'move))
8741   ;; Check whether the source group supports the required functions.
8742   (cond ((and (eq action 'move)
8743               (not (gnus-check-backend-function
8744                     'request-move-article gnus-newsgroup-name)))
8745          (error "The current group does not support article moving"))
8746         ((and (eq action 'crosspost)
8747               (not (gnus-check-backend-function
8748                     'request-replace-article gnus-newsgroup-name)))
8749          (error "The current group does not support article editing")))
8750   (let ((articles (gnus-summary-work-articles n))
8751         (prefix (if (gnus-check-backend-function
8752                      'request-move-article gnus-newsgroup-name)
8753                     (gnus-group-real-prefix gnus-newsgroup-name)
8754                   ""))
8755         (names '((move "Move" "Moving")
8756                  (copy "Copy" "Copying")
8757                  (crosspost "Crosspost" "Crossposting")))
8758         (copy-buf (save-excursion
8759                     (nnheader-set-temp-buffer " *copy article*")))
8760         art-group to-method new-xref article to-groups)
8761     (unless (assq action names)
8762       (error "Unknown action %s" action))
8763     ;; Read the newsgroup name.
8764     (when (and (not to-newsgroup)
8765                (not select-method))
8766       (if (and gnus-move-split-methods
8767                (not
8768                 (and (memq gnus-current-article articles)
8769                      (gnus-buffer-live-p gnus-original-article-buffer))))
8770           ;; When `gnus-move-split-methods' is non-nil, we have to
8771           ;; select an article to give `gnus-read-move-group-name' an
8772           ;; opportunity to suggest an appropriate default.  However,
8773           ;; we needn't render or mark the article.
8774           (let ((gnus-display-mime-function nil)
8775                 (gnus-article-prepare-hook nil)
8776                 (gnus-mark-article-hook nil))
8777             (gnus-summary-select-article nil nil nil (car articles))))
8778       (setq to-newsgroup
8779             (gnus-read-move-group-name
8780              (cadr (assq action names))
8781              (symbol-value (intern (format "gnus-current-%s-group" action)))
8782              articles prefix))
8783       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8784     (setq to-method (or select-method
8785                         (gnus-server-to-method
8786                          (gnus-group-method to-newsgroup))))
8787     ;; Check the method we are to move this article to...
8788     (unless (gnus-check-backend-function
8789              'request-accept-article (car to-method))
8790       (error "%s does not support article copying" (car to-method)))
8791     (unless (gnus-check-server to-method)
8792       (error "Can't open server %s" (car to-method)))
8793     (gnus-message 6 "%s to %s: %s..."
8794                   (caddr (assq action names))
8795                   (or (car select-method) to-newsgroup) articles)
8796     (while articles
8797       (setq article (pop articles))
8798       (setq
8799        art-group
8800        (cond
8801         ;; Move the article.
8802         ((eq action 'move)
8803          ;; Remove this article from future suppression.
8804          (gnus-dup-unsuppress-article article)
8805          (gnus-request-move-article
8806           article                       ; Article to move
8807           gnus-newsgroup-name           ; From newsgroup
8808           (nth 1 (gnus-find-method-for-group
8809                   gnus-newsgroup-name)) ; Server
8810           (list 'gnus-request-accept-article
8811                 to-newsgroup (list 'quote select-method)
8812                 (not articles) t)       ; Accept form
8813           (not articles)))              ; Only save nov last time
8814         ;; Copy the article.
8815         ((eq action 'copy)
8816          (save-excursion
8817            (set-buffer copy-buf)
8818            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8819              (gnus-request-accept-article
8820               to-newsgroup select-method (not articles) t))))
8821         ;; Crosspost the article.
8822         ((eq action 'crosspost)
8823          (let ((xref (message-tokenize-header
8824                       (mail-header-xref (gnus-summary-article-header article))
8825                       " ")))
8826            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8827                                   ":" (number-to-string article)))
8828            (unless xref
8829              (setq xref (list (system-name))))
8830            (setq new-xref
8831                  (concat
8832                   (mapconcat 'identity
8833                              (delete "Xref:" (delete new-xref xref))
8834                              " ")
8835                   " " new-xref))
8836            (save-excursion
8837              (set-buffer copy-buf)
8838              ;; First put the article in the destination group.
8839              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8840              (when (consp (setq art-group
8841                                 (gnus-request-accept-article
8842                                  to-newsgroup select-method (not articles))))
8843                (setq new-xref (concat new-xref " " (car art-group)
8844                                       ":"
8845                                       (number-to-string (cdr art-group))))
8846                ;; Now we have the new Xrefs header, so we insert
8847                ;; it and replace the new article.
8848                (nnheader-replace-header "Xref" new-xref)
8849                (gnus-request-replace-article
8850                 (cdr art-group) to-newsgroup (current-buffer))
8851                art-group))))))
8852       (cond
8853        ((not art-group)
8854         (gnus-message 1 "Couldn't %s article %s: %s"
8855                       (cadr (assq action names)) article
8856                       (nnheader-get-report (car to-method))))
8857        ((eq art-group 'junk)
8858         (when (eq action 'move)
8859           (gnus-summary-mark-article article gnus-canceled-mark)
8860           (gnus-message 4 "Deleted article %s" article)
8861           ;; run the delete hook
8862           (run-hook-with-args 'gnus-summary-article-delete-hook
8863                               action
8864                               (gnus-data-header
8865                                (assoc article (gnus-data-list nil)))
8866                               gnus-newsgroup-name nil
8867                               select-method)))
8868        (t
8869         (let* ((pto-group (gnus-group-prefixed-name
8870                            (car art-group) to-method))
8871                (entry
8872                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8873                (info (nth 2 entry))
8874                (to-group (gnus-info-group info))
8875                to-marks)
8876           ;; Update the group that has been moved to.
8877           (when (and info
8878                      (memq action '(move copy)))
8879             (unless (member to-group to-groups)
8880               (push to-group to-groups))
8881
8882             (unless (memq article gnus-newsgroup-unreads)
8883               (push 'read to-marks)
8884               (gnus-info-set-read
8885                info (gnus-add-to-range (gnus-info-read info)
8886                                        (list (cdr art-group)))))
8887
8888             ;; See whether the article is to be put in the cache.
8889             (let ((marks gnus-article-mark-lists)
8890                   (to-article (cdr art-group)))
8891
8892               ;; Enter the article into the cache in the new group,
8893               ;; if that is required.
8894               (when gnus-use-cache
8895                 (gnus-cache-possibly-enter-article
8896                  to-group to-article
8897                  (memq article gnus-newsgroup-marked)
8898                  (memq article gnus-newsgroup-dormant)
8899                  (memq article gnus-newsgroup-unreads)))
8900
8901               (when gnus-preserve-marks
8902                 ;; Copy any marks over to the new group.
8903                 (when (and (equal to-group gnus-newsgroup-name)
8904                            (not (memq article gnus-newsgroup-unreads)))
8905                   ;; Mark this article as read in this group.
8906                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8907                   (setcdr (gnus-active to-group) to-article)
8908                   (setcdr gnus-newsgroup-active to-article))
8909
8910                 (while marks
8911                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8912                     (when (memq article (symbol-value
8913                                          (intern (format "gnus-newsgroup-%s"
8914                                                          (caar marks)))))
8915                       (push (cdar marks) to-marks)
8916                       ;; If the other group is the same as this group,
8917                       ;; then we have to add the mark to the list.
8918                       (when (equal to-group gnus-newsgroup-name)
8919                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8920                              (cons to-article
8921                                    (symbol-value
8922                                     (intern (format "gnus-newsgroup-%s"
8923                                                     (caar marks)))))))
8924                       ;; Copy the marks to other group.
8925                       (gnus-add-marked-articles
8926                        to-group (cdar marks) (list to-article) info)))
8927                   (setq marks (cdr marks)))
8928
8929                 (gnus-request-set-mark
8930                  to-group (list (list (list to-article) 'add to-marks))))
8931
8932               (gnus-dribble-enter
8933                (concat "(gnus-group-set-info '"
8934                        (gnus-prin1-to-string (gnus-get-info to-group))
8935                        ")"))))
8936
8937           ;; Update the Xref header in this article to point to
8938           ;; the new crossposted article we have just created.
8939           (when (eq action 'crosspost)
8940             (save-excursion
8941               (set-buffer copy-buf)
8942               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8943               (nnheader-replace-header "Xref" new-xref)
8944               (gnus-request-replace-article
8945                article gnus-newsgroup-name (current-buffer))))
8946
8947           ;; run the move/copy/crosspost/respool hook
8948           (run-hook-with-args 'gnus-summary-article-move-hook 
8949                               action
8950                               (gnus-data-header 
8951                                (assoc article (gnus-data-list nil)))
8952                               gnus-newsgroup-name
8953                               to-newsgroup
8954                               select-method))
8955
8956         ;;;!!!Why is this necessary?
8957         (set-buffer gnus-summary-buffer)
8958         
8959         (gnus-summary-goto-subject article)
8960         (when (eq action 'move)
8961           (gnus-summary-mark-article article gnus-canceled-mark))))
8962       (gnus-summary-remove-process-mark article))
8963     ;; Re-activate all groups that have been moved to.
8964     (save-excursion
8965       (set-buffer gnus-group-buffer)
8966       (let ((gnus-group-marked to-groups))
8967         (gnus-group-get-new-news-this-group nil t)))
8968
8969     (gnus-kill-buffer copy-buf)
8970     (gnus-summary-position-point)
8971     (gnus-set-mode-line 'summary)))
8972
8973 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8974   "Move the current article to a different newsgroup.
8975 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8976 When called interactively, if TO-NEWSGROUP is nil, use the value of
8977 the variable `gnus-move-split-methods' for finding a default target
8978 newsgroup.
8979 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8980 re-spool using this method."
8981   (interactive "P")
8982   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8983
8984 (defun gnus-summary-crosspost-article (&optional n)
8985   "Crosspost the current article to some other group."
8986   (interactive "P")
8987   (gnus-summary-move-article n nil nil 'crosspost))
8988
8989 (defcustom gnus-summary-respool-default-method nil
8990   "Default method type for respooling an article.
8991 If nil, use to the current newsgroup method."
8992   :type 'symbol
8993   :group 'gnus-summary-mail)
8994
8995 (defcustom gnus-summary-display-while-building nil
8996   "If not-nil, show and update the summary buffer as it's being built.
8997 If the value is t, update the buffer after every line is inserted.  If
8998 the value is an integer (N), update the display every N lines."
8999   :group 'gnus-thread
9000   :type '(choice (const :tag "off" nil)
9001                  number
9002                  (const :tag "frequently" t)))
9003
9004 (defun gnus-summary-respool-article (&optional n method)
9005   "Respool the current article.
9006 The article will be squeezed through the mail spooling process again,
9007 which means that it will be put in some mail newsgroup or other
9008 depending on `nnmail-split-methods'.
9009 If N is a positive number, respool the N next articles.
9010 If N is a negative number, respool the N previous articles.
9011 If N is nil and any articles have been marked with the process mark,
9012 respool those articles instead.
9013
9014 Respooling can be done both from mail groups and \"real\" newsgroups.
9015 In the former case, the articles in question will be moved from the
9016 current group into whatever groups they are destined to.  In the
9017 latter case, they will be copied into the relevant groups."
9018   (interactive
9019    (list current-prefix-arg
9020          (let* ((methods (gnus-methods-using 'respool))
9021                 (methname
9022                  (symbol-name (or gnus-summary-respool-default-method
9023                                   (car (gnus-find-method-for-group
9024                                         gnus-newsgroup-name)))))
9025                 (method
9026                  (gnus-completing-read-with-default
9027                   methname "What backend do you want to use when respooling?"
9028                   methods nil t nil 'gnus-mail-method-history))
9029                 ms)
9030            (cond
9031             ((zerop (length (setq ms (gnus-servers-using-backend
9032                                       (intern method)))))
9033              (list (intern method) ""))
9034             ((= 1 (length ms))
9035              (car ms))
9036             (t
9037              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9038                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9039                            ms-alist))))))))
9040   (unless method
9041     (error "No method given for respooling"))
9042   (if (assoc (symbol-name
9043               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9044              (gnus-methods-using 'respool))
9045       (gnus-summary-move-article n nil method)
9046     (gnus-summary-copy-article n nil method)))
9047
9048 (defun gnus-summary-import-article (file &optional edit)
9049   "Import an arbitrary file into a mail newsgroup."
9050   (interactive "fImport file: \nP")
9051   (let ((group gnus-newsgroup-name)
9052         (now (current-time))
9053         atts lines group-art)
9054     (unless (gnus-check-backend-function 'request-accept-article group)
9055       (error "%s does not support article importing" group))
9056     (or (file-readable-p file)
9057         (not (file-regular-p file))
9058         (error "Can't read %s" file))
9059     (save-excursion
9060       (set-buffer (gnus-get-buffer-create " *import file*"))
9061       (erase-buffer)
9062       (nnheader-insert-file-contents file)
9063       (goto-char (point-min))
9064       (if (nnheader-article-p)
9065           (save-restriction
9066             (goto-char (point-min))
9067             (search-forward "\n\n" nil t)
9068             (narrow-to-region (point-min) (1- (point)))
9069             (goto-char (point-min))
9070             (unless (re-search-forward "^date:" nil t)
9071               (goto-char (point-max))
9072               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9073        ;; This doesn't look like an article, so we fudge some headers.
9074         (setq atts (file-attributes file)
9075               lines (count-lines (point-min) (point-max)))
9076         (insert "From: " (read-string "From: ") "\n"
9077                 "Subject: " (read-string "Subject: ") "\n"
9078                 "Date: " (message-make-date (nth 5 atts)) "\n"
9079                 "Message-ID: " (message-make-message-id) "\n"
9080                 "Lines: " (int-to-string lines) "\n"
9081                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9082       (setq group-art (gnus-request-accept-article group nil t))
9083       (kill-buffer (current-buffer)))
9084     (setq gnus-newsgroup-active (gnus-activate-group group))
9085     (forward-line 1)
9086     (gnus-summary-goto-article (cdr group-art) nil t)
9087     (when edit
9088       (gnus-summary-edit-article))))
9089
9090 (defun gnus-summary-create-article ()
9091   "Create an article in a mail newsgroup."
9092   (interactive)
9093   (let ((group gnus-newsgroup-name)
9094         (now (current-time))
9095         group-art)
9096     (unless (gnus-check-backend-function 'request-accept-article group)
9097       (error "%s does not support article importing" group))
9098     (save-excursion
9099       (set-buffer (gnus-get-buffer-create " *import file*"))
9100       (erase-buffer)
9101       (goto-char (point-min))
9102       ;; This doesn't look like an article, so we fudge some headers.
9103       (insert "From: " (read-string "From: ") "\n"
9104               "Subject: " (read-string "Subject: ") "\n"
9105               "Date: " (message-make-date now) "\n"
9106               "Message-ID: " (message-make-message-id) "\n")
9107       (setq group-art (gnus-request-accept-article group nil t))
9108       (kill-buffer (current-buffer)))
9109     (setq gnus-newsgroup-active (gnus-activate-group group))
9110     (forward-line 1)
9111     (gnus-summary-goto-article (cdr group-art) nil t)
9112     (gnus-summary-edit-article)))
9113
9114 (defun gnus-summary-article-posted-p ()
9115   "Say whether the current (mail) article is available from news as well.
9116 This will be the case if the article has both been mailed and posted."
9117   (interactive)
9118   (let ((id (mail-header-references (gnus-summary-article-header)))
9119         (gnus-override-method (car (gnus-refer-article-methods))))
9120     (if (gnus-request-head id "")
9121         (gnus-message 2 "The current message was found on %s"
9122                       gnus-override-method)
9123       (gnus-message 2 "The current message couldn't be found on %s"
9124                     gnus-override-method)
9125       nil)))
9126
9127 (defun gnus-summary-expire-articles (&optional now)
9128   "Expire all articles that are marked as expirable in the current group."
9129   (interactive)
9130   (when (and (not gnus-group-is-exiting-without-update-p)
9131              (gnus-check-backend-function
9132               'request-expire-articles gnus-newsgroup-name))
9133     ;; This backend supports expiry.
9134     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9135            (expirable (if total
9136                           (progn
9137                             ;; We need to update the info for
9138                             ;; this group for `gnus-list-of-read-articles'
9139                             ;; to give us the right answer.
9140                             (gnus-run-hooks 'gnus-exit-group-hook)
9141                             (gnus-summary-update-info)
9142                             (gnus-list-of-read-articles gnus-newsgroup-name))
9143                         (setq gnus-newsgroup-expirable
9144                               (sort gnus-newsgroup-expirable '<))))
9145            (expiry-wait (if now 'immediate
9146                           (gnus-group-find-parameter
9147                            gnus-newsgroup-name 'expiry-wait)))
9148            (nnmail-expiry-target
9149             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9150                 nnmail-expiry-target))
9151            es)
9152       (when expirable
9153         ;; There are expirable articles in this group, so we run them
9154         ;; through the expiry process.
9155         (gnus-message 6 "Expiring articles...")
9156         (unless (gnus-check-group gnus-newsgroup-name)
9157           (error "Can't open server for %s" gnus-newsgroup-name))
9158         ;; The list of articles that weren't expired is returned.
9159         (save-excursion
9160           (if expiry-wait
9161               (let ((nnmail-expiry-wait-function nil)
9162                     (nnmail-expiry-wait expiry-wait))
9163                 (setq es (gnus-request-expire-articles
9164                           expirable gnus-newsgroup-name)))
9165             (setq es (gnus-request-expire-articles
9166                       expirable gnus-newsgroup-name)))
9167           (unless total
9168             (setq gnus-newsgroup-expirable es))
9169           ;; We go through the old list of expirable, and mark all
9170           ;; really expired articles as nonexistent.
9171           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9172             (let ((gnus-use-cache nil))
9173               (dolist (article expirable)
9174                 (when (and (not (memq article es))
9175                            (gnus-data-find article))
9176                   (gnus-summary-mark-article article gnus-canceled-mark)
9177                   (run-hook-with-args 'gnus-summary-article-expire-hook
9178                                       'delete
9179                                       (gnus-data-header
9180                                        (assoc article (gnus-data-list nil)))
9181                                       gnus-newsgroup-name
9182                                       nil
9183                                       nil))))))
9184         (gnus-message 6 "Expiring articles...done")))))
9185
9186 (defun gnus-summary-expire-articles-now ()
9187   "Expunge all expirable articles in the current group.
9188 This means that *all* articles that are marked as expirable will be
9189 deleted forever, right now."
9190   (interactive)
9191   (or gnus-expert-user
9192       (gnus-yes-or-no-p
9193        "Are you really, really, really sure you want to delete all these messages? ")
9194       (error "Phew!"))
9195   (gnus-summary-expire-articles t))
9196
9197 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9198 (defun gnus-summary-delete-article (&optional n)
9199   "Delete the N next (mail) articles.
9200 This command actually deletes articles.  This is not a marking
9201 command.  The article will disappear forever from your life, never to
9202 return.
9203 If N is negative, delete backwards.
9204 If N is nil and articles have been marked with the process mark,
9205 delete these instead."
9206   (interactive "P")
9207   (unless (gnus-check-backend-function 'request-expire-articles
9208                                        gnus-newsgroup-name)
9209     (error "The current newsgroup does not support article deletion"))
9210   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9211     (error "Couldn't open server"))
9212   ;; Compute the list of articles to delete.
9213   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9214         (nnmail-expiry-target 'delete)
9215         not-deleted)
9216     (if (and gnus-novice-user
9217              (not (gnus-yes-or-no-p
9218                    (format "Do you really want to delete %s forever? "
9219                            (if (> (length articles) 1)
9220                                (format "these %s articles" (length articles))
9221                              "this article")))))
9222         ()
9223       ;; Delete the articles.
9224       (setq not-deleted (gnus-request-expire-articles
9225                          articles gnus-newsgroup-name 'force))
9226       (while articles
9227         (gnus-summary-remove-process-mark (car articles))
9228         ;; The backend might not have been able to delete the article
9229         ;; after all.
9230         (unless (memq (car articles) not-deleted)
9231           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9232         (let* ((article (car articles))
9233                (id (mail-header-id (gnus-data-header 
9234                                     (assoc article (gnus-data-list nil))))))
9235           (run-hook-with-args 'gnus-summary-article-delete-hook
9236                               'delete id gnus-newsgroup-name nil
9237                               nil))
9238         (setq articles (cdr articles)))
9239       (when not-deleted
9240         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9241     (gnus-summary-position-point)
9242     (gnus-set-mode-line 'summary)
9243     not-deleted))
9244
9245 (defun gnus-summary-edit-article (&optional arg)
9246   "Edit the current article.
9247 This will have permanent effect only in mail groups.
9248 If ARG is nil, edit the decoded articles.
9249 If ARG is 1, edit the raw articles.
9250 If ARG is 2, edit the raw articles even in read-only groups.
9251 If ARG is 3, edit the articles with the current handles.
9252 Otherwise, allow editing of articles even in read-only
9253 groups."
9254   (interactive "P")
9255   (let (force raw current-handles)
9256     (cond
9257      ((null arg))
9258      ((eq arg 1)
9259       (setq raw t))
9260      ((eq arg 2)
9261       (setq raw t
9262             force t))
9263      ((eq arg 3)
9264       (setq current-handles
9265             (and (gnus-buffer-live-p gnus-article-buffer)
9266                  (with-current-buffer gnus-article-buffer
9267                    (prog1
9268                        gnus-article-mime-handles
9269                      (setq gnus-article-mime-handles nil))))))
9270      (t
9271       (setq force t)))
9272     (when (and raw (not force)
9273                (member gnus-newsgroup-name '("nndraft:delayed"
9274                                              "nndraft:drafts"
9275                                              "nndraft:queue")))
9276       (error "Can't edit the raw article in group %s"
9277              gnus-newsgroup-name))
9278     (save-excursion
9279       (set-buffer gnus-summary-buffer)
9280       (let ((mail-parse-charset gnus-newsgroup-charset)
9281             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9282         (gnus-set-global-variables)
9283         (when (and (not force)
9284                    (gnus-group-read-only-p))
9285           (error "The current newsgroup does not support article editing"))
9286         (gnus-summary-show-article t)
9287         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9288           (with-current-buffer gnus-article-buffer
9289             (mm-enable-multibyte)))
9290         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9291             (setq raw t))
9292         (gnus-article-edit-article
9293          (if raw 'ignore
9294            `(lambda ()
9295               (let ((mbl mml-buffer-list))
9296                 (setq mml-buffer-list nil)
9297                 (mime-to-mml ,'current-handles)
9298                 (let ((mbl1 mml-buffer-list))
9299                   (setq mml-buffer-list mbl)
9300                   (set (make-local-variable 'mml-buffer-list) mbl1))
9301                 (make-local-hook 'kill-buffer-hook)
9302                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9303          `(lambda (no-highlight)
9304             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9305                   (message-options message-options)
9306                   (message-options-set-recipient)
9307                   (mail-parse-ignored-charsets
9308                    ',gnus-newsgroup-ignored-charsets))
9309               ,(if (not raw) '(progn
9310                                 (mml-to-mime)
9311                                 (mml-destroy-buffers)
9312                                 (remove-hook 'kill-buffer-hook
9313                                              'mml-destroy-buffers t)
9314                                 (kill-local-variable 'mml-buffer-list)))
9315               (gnus-summary-edit-article-done
9316                ,(or (mail-header-references gnus-current-headers) "")
9317                ,(gnus-group-read-only-p)
9318                ,gnus-summary-buffer no-highlight))))))))
9319
9320 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9321
9322 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9323                                                  no-highlight)
9324   "Make edits to the current article permanent."
9325   (interactive)
9326   (save-excursion
9327    ;; The buffer restriction contains the entire article if it exists.
9328     (when (article-goto-body)
9329       (let ((lines (count-lines (point) (point-max)))
9330             (length (- (point-max) (point)))
9331             (case-fold-search t)
9332             (body (copy-marker (point))))
9333         (goto-char (point-min))
9334         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9335           (delete-region (match-beginning 1) (match-end 1))
9336           (insert (number-to-string length)))
9337         (goto-char (point-min))
9338         (when (re-search-forward
9339                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9340           (delete-region (match-beginning 1) (match-end 1))
9341           (insert (number-to-string length)))
9342         (goto-char (point-min))
9343         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9344           (delete-region (match-beginning 1) (match-end 1))
9345           (insert (number-to-string lines))))))
9346   ;; Replace the article.
9347   (let ((buf (current-buffer)))
9348     (with-temp-buffer
9349       (insert-buffer-substring buf)
9350
9351       (if (and (not read-only)
9352                (not (gnus-request-replace-article
9353                      (cdr gnus-article-current) (car gnus-article-current)
9354                      (current-buffer) t)))
9355           (error "Couldn't replace article")
9356         ;; Update the summary buffer.
9357         (if (and references
9358                  (equal (message-tokenize-header references " ")
9359                         (message-tokenize-header
9360                          (or (message-fetch-field "references") "") " ")))
9361             ;; We only have to update this line.
9362             (save-excursion
9363               (save-restriction
9364                 (message-narrow-to-head)
9365                 (let ((head (buffer-string))
9366                       header)
9367                   (with-temp-buffer
9368                     (insert (format "211 %d Article retrieved.\n"
9369                                     (cdr gnus-article-current)))
9370                     (insert head)
9371                     (insert ".\n")
9372                     (let ((nntp-server-buffer (current-buffer)))
9373                       (setq header (car (gnus-get-newsgroup-headers
9374                                          nil t))))
9375                     (save-excursion
9376                       (set-buffer gnus-summary-buffer)
9377                       (gnus-data-set-header
9378                        (gnus-data-find (cdr gnus-article-current))
9379                        header)
9380                       (gnus-summary-update-article-line
9381                        (cdr gnus-article-current) header)
9382                       (if (gnus-summary-goto-subject
9383                            (cdr gnus-article-current) nil t)
9384                           (gnus-summary-update-secondary-mark
9385                            (cdr gnus-article-current))))))))
9386           ;; Update threads.
9387           (set-buffer (or buffer gnus-summary-buffer))
9388           (gnus-summary-update-article (cdr gnus-article-current))
9389           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9390               (gnus-summary-update-secondary-mark
9391                (cdr gnus-article-current))))
9392         ;; Prettify the article buffer again.
9393         (unless no-highlight
9394           (save-excursion
9395             (set-buffer gnus-article-buffer)
9396             ;;;!!! Fix this -- article should be rehighlighted.
9397             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9398             (set-buffer gnus-original-article-buffer)
9399             (gnus-request-article
9400              (cdr gnus-article-current)
9401              (car gnus-article-current) (current-buffer))))
9402         ;; Prettify the summary buffer line.
9403         (when (gnus-visual-p 'summary-highlight 'highlight)
9404           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9405
9406 (defun gnus-summary-edit-wash (key)
9407   "Perform editing command KEY in the article buffer."
9408   (interactive
9409    (list
9410     (progn
9411       (message "%s" (concat (this-command-keys) "- "))
9412       (read-char))))
9413   (message "")
9414   (gnus-summary-edit-article)
9415   (execute-kbd-macro (concat (this-command-keys) key))
9416   (gnus-article-edit-done))
9417
9418 ;;; Respooling
9419
9420 (defun gnus-summary-respool-query (&optional silent trace)
9421   "Query where the respool algorithm would put this article."
9422   (interactive)
9423   (let (gnus-mark-article-hook)
9424     (gnus-summary-select-article)
9425     (save-excursion
9426       (set-buffer gnus-original-article-buffer)
9427       (save-restriction
9428         (message-narrow-to-head)
9429         (let ((groups (nnmail-article-group 'identity trace)))
9430           (unless silent
9431             (if groups
9432                 (message "This message would go to %s"
9433                          (mapconcat 'car groups ", "))
9434               (message "This message would go to no groups"))
9435             groups))))))
9436
9437 (defun gnus-summary-respool-trace ()
9438   "Trace where the respool algorithm would put this article.
9439 Display a buffer showing all fancy splitting patterns which matched."
9440   (interactive)
9441   (gnus-summary-respool-query nil t))
9442
9443 ;; Summary marking commands.
9444
9445 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9446   "Mark articles which has the same subject as read, and then select the next.
9447 If UNMARK is positive, remove any kind of mark.
9448 If UNMARK is negative, tick articles."
9449   (interactive "P")
9450   (when unmark
9451     (setq unmark (prefix-numeric-value unmark)))
9452   (let ((count
9453          (gnus-summary-mark-same-subject
9454           (gnus-summary-article-subject) unmark)))
9455     ;; Select next unread article.  If auto-select-same mode, should
9456     ;; select the first unread article.
9457     (gnus-summary-next-article t (and gnus-auto-select-same
9458                                       (gnus-summary-article-subject)))
9459     (gnus-message 7 "%d article%s marked as %s"
9460                   count (if (= count 1) " is" "s are")
9461                   (if unmark "unread" "read"))))
9462
9463 (defun gnus-summary-kill-same-subject (&optional unmark)
9464   "Mark articles which has the same subject as read.
9465 If UNMARK is positive, remove any kind of mark.
9466 If UNMARK is negative, tick articles."
9467   (interactive "P")
9468   (when unmark
9469     (setq unmark (prefix-numeric-value unmark)))
9470   (let ((count
9471          (gnus-summary-mark-same-subject
9472           (gnus-summary-article-subject) unmark)))
9473     ;; If marked as read, go to next unread subject.
9474     (when (null unmark)
9475       ;; Go to next unread subject.
9476       (gnus-summary-next-subject 1 t))
9477     (gnus-message 7 "%d articles are marked as %s"
9478                   count (if unmark "unread" "read"))))
9479
9480 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9481   "Mark articles with same SUBJECT as read, and return marked number.
9482 If optional argument UNMARK is positive, remove any kinds of marks.
9483 If optional argument UNMARK is negative, mark articles as unread instead."
9484   (let ((count 1))
9485     (save-excursion
9486       (cond
9487        ((null unmark)                   ; Mark as read.
9488         (while (and
9489                 (progn
9490                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9491                   (gnus-summary-show-thread) t)
9492                 (gnus-summary-find-subject subject))
9493           (setq count (1+ count))))
9494        ((> unmark 0)                    ; Tick.
9495         (while (and
9496                 (progn
9497                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9498                   (gnus-summary-show-thread) t)
9499                 (gnus-summary-find-subject subject))
9500           (setq count (1+ count))))
9501        (t                               ; Mark as unread.
9502         (while (and
9503                 (progn
9504                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9505                   (gnus-summary-show-thread) t)
9506                 (gnus-summary-find-subject subject))
9507           (setq count (1+ count)))))
9508       (gnus-set-mode-line 'summary)
9509       ;; Return the number of marked articles.
9510       count)))
9511
9512 (defun gnus-summary-mark-as-processable (n &optional unmark)
9513   "Set the process mark on the next N articles.
9514 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9515 the process mark instead.  The difference between N and the actual
9516 number of articles marked is returned."
9517   (interactive "P")
9518   (if (and (null n) (gnus-region-active-p))
9519       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9520     (setq n (prefix-numeric-value n))
9521     (let ((backward (< n 0))
9522           (n (abs n)))
9523       (while (and
9524               (> n 0)
9525               (if unmark
9526                   (gnus-summary-remove-process-mark
9527                    (gnus-summary-article-number))
9528                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9529               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9530         (setq n (1- n)))
9531       (when (/= 0 n)
9532         (gnus-message 7 "No more articles"))
9533       (gnus-summary-recenter)
9534       (gnus-summary-position-point)
9535       n)))
9536
9537 (defun gnus-summary-unmark-as-processable (n)
9538   "Remove the process mark from the next N articles.
9539 If N is negative, unmark backward instead.  The difference between N and
9540 the actual number of articles unmarked is returned."
9541   (interactive "P")
9542   (gnus-summary-mark-as-processable n t))
9543
9544 (defun gnus-summary-unmark-all-processable ()
9545   "Remove the process mark from all articles."
9546   (interactive)
9547   (save-excursion
9548     (while gnus-newsgroup-processable
9549       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9550   (gnus-summary-position-point))
9551
9552 (defun gnus-summary-add-mark (article type)
9553   "Mark ARTICLE with a mark of TYPE."
9554   (let ((vtype (car (assq type gnus-article-mark-lists)))
9555         var)
9556     (if (not vtype)
9557         (error "No such mark type: %s" type)
9558       (setq var (intern (format "gnus-newsgroup-%s" type)))
9559       (set var (cons article (symbol-value var)))
9560       (if (memq type '(processable cached replied forwarded recent saved))
9561           (gnus-summary-update-secondary-mark article)
9562         ;;; !!! This is bogus.  We should find out what primary
9563         ;;; !!! mark we want to set.
9564         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9565
9566 (defun gnus-summary-mark-as-expirable (n)
9567   "Mark N articles forward as expirable.
9568 If N is negative, mark backward instead.  The difference between N and
9569 the actual number of articles marked is returned."
9570   (interactive "p")
9571   (gnus-summary-mark-forward n gnus-expirable-mark))
9572
9573 (defun gnus-summary-mark-as-spam (n)
9574   "Mark N articles forward as spam.
9575 If N is negative, mark backward instead.  The difference between N and
9576 the actual number of articles marked is returned."
9577   (interactive "p")
9578   (gnus-summary-mark-forward n gnus-spam-mark))
9579
9580 (defun gnus-summary-mark-article-as-replied (article)
9581   "Mark ARTICLE as replied to and update the summary line.
9582 ARTICLE can also be a list of articles."
9583   (interactive (list (gnus-summary-article-number)))
9584   (let ((articles (if (listp article) article (list article))))
9585     (dolist (article articles)
9586       (unless (numberp article)
9587         (error "%s is not a number" article))
9588       (push article gnus-newsgroup-replied)
9589       (let ((buffer-read-only nil))
9590         (when (gnus-summary-goto-subject article nil t)
9591           (gnus-summary-update-secondary-mark article))))))
9592
9593 (defun gnus-summary-mark-article-as-forwarded (article)
9594   "Mark ARTICLE as forwarded and update the summary line.
9595 ARTICLE can also be a list of articles."
9596   (let ((articles (if (listp article) article (list article))))
9597     (dolist (article articles)
9598       (push article gnus-newsgroup-forwarded)
9599       (let ((buffer-read-only nil))
9600         (when (gnus-summary-goto-subject article nil t)
9601           (gnus-summary-update-secondary-mark article))))))
9602
9603 (defun gnus-summary-set-bookmark (article)
9604   "Set a bookmark in current article."
9605   (interactive (list (gnus-summary-article-number)))
9606   (when (or (not (get-buffer gnus-article-buffer))
9607             (not gnus-current-article)
9608             (not gnus-article-current)
9609             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9610     (error "No current article selected"))
9611   ;; Remove old bookmark, if one exists.
9612   (gnus-pull article gnus-newsgroup-bookmarks)
9613   ;; Set the new bookmark, which is on the form
9614   ;; (article-number . line-number-in-body).
9615   (push
9616    (cons article
9617          (save-excursion
9618            (set-buffer gnus-article-buffer)
9619            (count-lines
9620             (min (point)
9621                  (save-excursion
9622                    (article-goto-body)
9623                    (point)))
9624             (point))))
9625    gnus-newsgroup-bookmarks)
9626   (gnus-message 6 "A bookmark has been added to the current article."))
9627
9628 (defun gnus-summary-remove-bookmark (article)
9629   "Remove the bookmark from the current article."
9630   (interactive (list (gnus-summary-article-number)))
9631   ;; Remove old bookmark, if one exists.
9632   (if (not (assq article gnus-newsgroup-bookmarks))
9633       (gnus-message 6 "No bookmark in current article.")
9634     (gnus-pull article gnus-newsgroup-bookmarks)
9635     (gnus-message 6 "Removed bookmark.")))
9636
9637 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9638 (defun gnus-summary-mark-as-dormant (n)
9639   "Mark N articles forward as dormant.
9640 If N is negative, mark backward instead.  The difference between N and
9641 the actual number of articles marked is returned."
9642   (interactive "p")
9643   (gnus-summary-mark-forward n gnus-dormant-mark))
9644
9645 (defun gnus-summary-set-process-mark (article)
9646   "Set the process mark on ARTICLE and update the summary line."
9647   (setq gnus-newsgroup-processable
9648         (cons article
9649               (delq article gnus-newsgroup-processable)))
9650   (when (gnus-summary-goto-subject article)
9651     (gnus-summary-show-thread)
9652     (gnus-summary-goto-subject article)
9653     (gnus-summary-update-secondary-mark article)))
9654
9655 (defun gnus-summary-remove-process-mark (article)
9656   "Remove the process mark from ARTICLE and update the summary line."
9657   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9658   (when (gnus-summary-goto-subject article)
9659     (gnus-summary-show-thread)
9660     (gnus-summary-goto-subject article)
9661     (gnus-summary-update-secondary-mark article)))
9662
9663 (defun gnus-summary-set-saved-mark (article)
9664   "Set the process mark on ARTICLE and update the summary line."
9665   (push article gnus-newsgroup-saved)
9666   (when (gnus-summary-goto-subject article)
9667     (gnus-summary-update-secondary-mark article)))
9668
9669 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9670   "Mark N articles as read forwards.
9671 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9672 The difference between N and the actual number of articles marked is
9673 returned.
9674 If NO-EXPIRE, auto-expiry will be inhibited."
9675   (interactive "p")
9676   (gnus-summary-show-thread)
9677   (let ((backward (< n 0))
9678         (gnus-summary-goto-unread
9679          (and gnus-summary-goto-unread
9680               (not (eq gnus-summary-goto-unread 'never))
9681               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9682                                     gnus-ticked-mark gnus-dormant-mark)))))
9683         (n (abs n))
9684         (mark (or mark gnus-del-mark)))
9685     (while (and (> n 0)
9686                 (gnus-summary-mark-article nil mark no-expire)
9687                 (zerop (gnus-summary-next-subject
9688                         (if backward -1 1)
9689                         (and gnus-summary-goto-unread
9690                              (not (eq gnus-summary-goto-unread 'never)))
9691                         t)))
9692       (setq n (1- n)))
9693     (when (/= 0 n)
9694       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9695     (gnus-summary-recenter)
9696     (gnus-summary-position-point)
9697     (gnus-set-mode-line 'summary)
9698     n))
9699
9700 (defun gnus-summary-mark-article-as-read (mark)
9701   "Mark the current article quickly as read with MARK."
9702   (let ((article (gnus-summary-article-number)))
9703     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9704     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9705     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9706     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9707     (push (cons article mark) gnus-newsgroup-reads)
9708     ;; Possibly remove from cache, if that is used.
9709     (when gnus-use-cache
9710       (gnus-cache-enter-remove-article article))
9711     ;; Allow the backend to change the mark.
9712     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9713     ;; Check for auto-expiry.
9714     (when (and gnus-newsgroup-auto-expire
9715                (memq mark gnus-auto-expirable-marks))
9716       (setq mark gnus-expirable-mark)
9717       ;; Let the backend know about the mark change.
9718       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9719       (push article gnus-newsgroup-expirable))
9720     ;; Set the mark in the buffer.
9721     (gnus-summary-update-mark mark 'unread)
9722     t))
9723
9724 (defun gnus-summary-mark-article-as-unread (mark)
9725   "Mark the current article quickly as unread with MARK."
9726   (let* ((article (gnus-summary-article-number))
9727          (old-mark (gnus-summary-article-mark article)))
9728     ;; Allow the backend to change the mark.
9729     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9730     (if (eq mark old-mark)
9731         t
9732       (if (<= article 0)
9733           (progn
9734             (gnus-error 1 "Can't mark negative article numbers")
9735             nil)
9736         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9737         (setq gnus-newsgroup-spam-marked
9738               (delq article gnus-newsgroup-spam-marked))
9739         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9740         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9741         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9742         (cond ((= mark gnus-ticked-mark)
9743                (setq gnus-newsgroup-marked
9744                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9745                                               article)))
9746               ((= mark gnus-spam-mark)
9747                (setq gnus-newsgroup-spam-marked
9748                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9749                                               article)))
9750               ((= mark gnus-dormant-mark)
9751                (setq gnus-newsgroup-dormant
9752                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9753                                               article)))
9754               (t
9755                (setq gnus-newsgroup-unreads
9756                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9757                                               article))))
9758         (gnus-pull article gnus-newsgroup-reads)
9759
9760         ;; See whether the article is to be put in the cache.
9761         (and gnus-use-cache
9762              (vectorp (gnus-summary-article-header article))
9763              (save-excursion
9764                (gnus-cache-possibly-enter-article
9765                 gnus-newsgroup-name article
9766                 (= mark gnus-ticked-mark)
9767                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9768
9769         ;; Fix the mark.
9770         (gnus-summary-update-mark mark 'unread)
9771         t))))
9772
9773 (defun gnus-summary-mark-article (&optional article mark no-expire)
9774   "Mark ARTICLE with MARK.  MARK can be any character.
9775 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9776 `??' (dormant) and `?E' (expirable).
9777 If MARK is nil, then the default character `?r' is used.
9778 If ARTICLE is nil, then the article on the current line will be
9779 marked.
9780 If NO-EXPIRE, auto-expiry will be inhibited."
9781   ;; The mark might be a string.
9782   (when (stringp mark)
9783     (setq mark (aref mark 0)))
9784   ;; If no mark is given, then we check auto-expiring.
9785   (when (null mark)
9786     (setq mark gnus-del-mark))
9787   (when (and (not no-expire)
9788              gnus-newsgroup-auto-expire
9789              (memq mark gnus-auto-expirable-marks))
9790     (setq mark gnus-expirable-mark))
9791   (let ((article (or article (gnus-summary-article-number)))
9792         (old-mark (gnus-summary-article-mark article)))
9793     ;; Allow the backend to change the mark.
9794     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9795     (if (eq mark old-mark)
9796         t
9797       (unless article
9798         (error "No article on current line"))
9799       (if (not (if (or (= mark gnus-unread-mark)
9800                        (= mark gnus-ticked-mark)
9801                        (= mark gnus-spam-mark)
9802                        (= mark gnus-dormant-mark))
9803                    (gnus-mark-article-as-unread article mark)
9804                  (gnus-mark-article-as-read article mark)))
9805           t
9806         ;; See whether the article is to be put in the cache.
9807         (and gnus-use-cache
9808              (not (= mark gnus-canceled-mark))
9809              (vectorp (gnus-summary-article-header article))
9810              (save-excursion
9811                (gnus-cache-possibly-enter-article
9812                 gnus-newsgroup-name article
9813                 (= mark gnus-ticked-mark)
9814                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9815
9816         (when (gnus-summary-goto-subject article nil t)
9817           (let ((buffer-read-only nil))
9818             (gnus-summary-show-thread)
9819             ;; Fix the mark.
9820             (gnus-summary-update-mark mark 'unread)
9821             t))))))
9822
9823 (defun gnus-summary-update-secondary-mark (article)
9824   "Update the secondary (read, process, cache) mark."
9825   (gnus-summary-update-mark
9826    (cond ((memq article gnus-newsgroup-processable)
9827           gnus-process-mark)
9828          ((memq article gnus-newsgroup-cached)
9829           gnus-cached-mark)
9830          ((memq article gnus-newsgroup-replied)
9831           gnus-replied-mark)
9832          ((memq article gnus-newsgroup-forwarded)
9833           gnus-forwarded-mark)
9834          ((memq article gnus-newsgroup-saved)
9835           gnus-saved-mark)
9836          ((memq article gnus-newsgroup-recent)
9837           gnus-recent-mark)
9838          ((memq article gnus-newsgroup-unseen)
9839           gnus-unseen-mark)
9840          (t gnus-no-mark))
9841    'replied)
9842   (when (gnus-visual-p 'summary-highlight 'highlight)
9843     (gnus-run-hooks 'gnus-summary-update-hook))
9844   t)
9845
9846 (defun gnus-summary-update-download-mark (article)
9847   "Update the download mark."
9848   (gnus-summary-update-mark
9849    (cond ((memq article gnus-newsgroup-undownloaded) 
9850           gnus-undownloaded-mark)
9851          (gnus-newsgroup-agentized
9852           gnus-downloaded-mark)
9853          (t
9854           gnus-no-mark))
9855    'download)
9856   (gnus-summary-update-line t)
9857   t)
9858
9859 (defun gnus-summary-update-mark (mark type)
9860   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9861         (buffer-read-only nil))
9862     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9863     (when forward
9864       (when (looking-at "\r")
9865         (incf forward))
9866       (when (<= (+ forward (point)) (point-max))
9867         ;; Go to the right position on the line.
9868         (goto-char (+ forward (point)))
9869         ;; Replace the old mark with the new mark.
9870         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9871         ;; Optionally update the marks by some user rule.
9872         (when (eq type 'unread)
9873           (gnus-data-set-mark
9874            (gnus-data-find (gnus-summary-article-number)) mark)
9875           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9876
9877 (defun gnus-mark-article-as-read (article &optional mark)
9878   "Enter ARTICLE in the pertinent lists and remove it from others."
9879   ;; Make the article expirable.
9880   (let ((mark (or mark gnus-del-mark)))
9881     (setq gnus-newsgroup-expirable
9882           (if (= mark gnus-expirable-mark)
9883               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9884             (delq article gnus-newsgroup-expirable)))
9885     ;; Remove from unread and marked lists.
9886     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9887     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9888     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9889     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9890     (push (cons article mark) gnus-newsgroup-reads)
9891     ;; Possibly remove from cache, if that is used.
9892     (when gnus-use-cache
9893       (gnus-cache-enter-remove-article article))
9894     t))
9895
9896 (defun gnus-mark-article-as-unread (article &optional mark)
9897   "Enter ARTICLE in the pertinent lists and remove it from others."
9898   (let ((mark (or mark gnus-ticked-mark)))
9899     (if (<= article 0)
9900         (progn
9901           (gnus-error 1 "Can't mark negative article numbers")
9902           nil)
9903       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9904             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9905             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9906             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9907             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9908
9909       ;; Unsuppress duplicates?
9910       (when gnus-suppress-duplicates
9911         (gnus-dup-unsuppress-article article))
9912
9913       (cond ((= mark gnus-ticked-mark)
9914              (setq gnus-newsgroup-marked
9915                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9916             ((= mark gnus-spam-mark)
9917              (setq gnus-newsgroup-spam-marked
9918                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9919                                             article)))
9920             ((= mark gnus-dormant-mark)
9921              (setq gnus-newsgroup-dormant
9922                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9923             (t
9924              (setq gnus-newsgroup-unreads
9925                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9926       (gnus-pull article gnus-newsgroup-reads)
9927       t)))
9928
9929 (defalias 'gnus-summary-mark-as-unread-forward
9930   'gnus-summary-tick-article-forward)
9931 (make-obsolete 'gnus-summary-mark-as-unread-forward
9932                'gnus-summary-tick-article-forward)
9933 (defun gnus-summary-tick-article-forward (n)
9934   "Tick N articles forwards.
9935 If N is negative, tick backwards instead.
9936 The difference between N and the number of articles ticked is returned."
9937   (interactive "p")
9938   (gnus-summary-mark-forward n gnus-ticked-mark))
9939
9940 (defalias 'gnus-summary-mark-as-unread-backward
9941   'gnus-summary-tick-article-backward)
9942 (make-obsolete 'gnus-summary-mark-as-unread-backward
9943                'gnus-summary-tick-article-backward)
9944 (defun gnus-summary-tick-article-backward (n)
9945   "Tick N articles backwards.
9946 The difference between N and the number of articles ticked is returned."
9947   (interactive "p")
9948   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9949
9950 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9951 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9952 (defun gnus-summary-tick-article (&optional article clear-mark)
9953   "Mark current article as unread.
9954 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9955 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9956   (interactive)
9957   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9958                                        gnus-ticked-mark)))
9959
9960 (defun gnus-summary-mark-as-read-forward (n)
9961   "Mark N articles as read forwards.
9962 If N is negative, mark backwards instead.
9963 The difference between N and the actual number of articles marked is
9964 returned."
9965   (interactive "p")
9966   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9967
9968 (defun gnus-summary-mark-as-read-backward (n)
9969   "Mark the N articles as read backwards.
9970 The difference between N and the actual number of articles marked is
9971 returned."
9972   (interactive "p")
9973   (gnus-summary-mark-forward
9974    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9975
9976 (defun gnus-summary-mark-as-read (&optional article mark)
9977   "Mark current article as read.
9978 ARTICLE specifies the article to be marked as read.
9979 MARK specifies a string to be inserted at the beginning of the line."
9980   (gnus-summary-mark-article article mark))
9981
9982 (defun gnus-summary-clear-mark-forward (n)
9983   "Clear marks from N articles forward.
9984 If N is negative, clear backward instead.
9985 The difference between N and the number of marks cleared is returned."
9986   (interactive "p")
9987   (gnus-summary-mark-forward n gnus-unread-mark))
9988
9989 (defun gnus-summary-clear-mark-backward (n)
9990   "Clear marks from N articles backward.
9991 The difference between N and the number of marks cleared is returned."
9992   (interactive "p")
9993   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9994
9995 (defun gnus-summary-mark-unread-as-read ()
9996   "Intended to be used by `gnus-summary-mark-article-hook'."
9997   (when (memq gnus-current-article gnus-newsgroup-unreads)
9998     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9999
10000 (defun gnus-summary-mark-read-and-unread-as-read ()
10001   "Intended to be used by `gnus-summary-mark-article-hook'."
10002   (let ((mark (gnus-summary-article-mark)))
10003     (when (or (gnus-unread-mark-p mark)
10004               (gnus-read-mark-p mark))
10005       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
10006
10007 (defun gnus-summary-mark-unread-as-ticked ()
10008   "Intended to be used by `gnus-summary-mark-article-hook'."
10009   (when (memq gnus-current-article gnus-newsgroup-unreads)
10010     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10011
10012 (defun gnus-summary-mark-region-as-read (point mark all)
10013   "Mark all unread articles between point and mark as read.
10014 If given a prefix, mark all articles between point and mark as read,
10015 even ticked and dormant ones."
10016   (interactive "r\nP")
10017   (save-excursion
10018     (let (article)
10019       (goto-char point)
10020       (beginning-of-line)
10021       (while (and
10022               (< (point) mark)
10023               (progn
10024                 (when (or all
10025                           (memq (setq article (gnus-summary-article-number))
10026                                 gnus-newsgroup-unreads))
10027                   (gnus-summary-mark-article article gnus-del-mark))
10028                 t)
10029               (gnus-summary-find-next))))))
10030
10031 (defun gnus-summary-mark-below (score mark)
10032   "Mark articles with score less than SCORE with MARK."
10033   (interactive "P\ncMark: ")
10034   (setq score (if score
10035                   (prefix-numeric-value score)
10036                 (or gnus-summary-default-score 0)))
10037   (save-excursion
10038     (set-buffer gnus-summary-buffer)
10039     (goto-char (point-min))
10040     (while
10041         (progn
10042           (and (< (gnus-summary-article-score) score)
10043                (gnus-summary-mark-article nil mark))
10044           (gnus-summary-find-next)))))
10045
10046 (defun gnus-summary-kill-below (&optional score)
10047   "Mark articles with score below SCORE as read."
10048   (interactive "P")
10049   (gnus-summary-mark-below score gnus-killed-mark))
10050
10051 (defun gnus-summary-clear-above (&optional score)
10052   "Clear all marks from articles with score above SCORE."
10053   (interactive "P")
10054   (gnus-summary-mark-above score gnus-unread-mark))
10055
10056 (defun gnus-summary-tick-above (&optional score)
10057   "Tick all articles with score above SCORE."
10058   (interactive "P")
10059   (gnus-summary-mark-above score gnus-ticked-mark))
10060
10061 (defun gnus-summary-mark-above (score mark)
10062   "Mark articles with score over SCORE with MARK."
10063   (interactive "P\ncMark: ")
10064   (setq score (if score
10065                   (prefix-numeric-value score)
10066                 (or gnus-summary-default-score 0)))
10067   (save-excursion
10068     (set-buffer gnus-summary-buffer)
10069     (goto-char (point-min))
10070     (while (and (progn
10071                   (when (> (gnus-summary-article-score) score)
10072                     (gnus-summary-mark-article nil mark))
10073                   t)
10074                 (gnus-summary-find-next)))))
10075
10076 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10077 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10078 (defun gnus-summary-limit-include-expunged (&optional no-error)
10079   "Display all the hidden articles that were expunged for low scores."
10080   (interactive)
10081   (let ((buffer-read-only nil))
10082     (let ((scored gnus-newsgroup-scored)
10083           headers h)
10084       (while scored
10085         (unless (gnus-summary-article-header (caar scored))
10086           (and (setq h (gnus-number-to-header (caar scored)))
10087                (< (cdar scored) gnus-summary-expunge-below)
10088                (push h headers)))
10089         (setq scored (cdr scored)))
10090       (if (not headers)
10091           (when (not no-error)
10092             (error "No expunged articles hidden"))
10093         (goto-char (point-min))
10094         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10095         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10096         (mapcar (lambda (x) (push (mail-header-number x)
10097                                   gnus-newsgroup-limit))
10098                 headers)
10099         (gnus-summary-prepare-unthreaded (nreverse headers))
10100         (goto-char (point-min))
10101         (gnus-summary-position-point)
10102         t))))
10103
10104 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10105   "Mark all unread articles in this newsgroup as read.
10106 If prefix argument ALL is non-nil, ticked and dormant articles will
10107 also be marked as read.
10108 If QUIETLY is non-nil, no questions will be asked.
10109 If TO-HERE is non-nil, it should be a point in the buffer.  All
10110 articles before (after, if REVERSE is set) this point will be marked as read.
10111 Note that this function will only catch up the unread article
10112 in the current summary buffer limitation.
10113 The number of articles marked as read is returned."
10114   (interactive "P")
10115   (prog1
10116       (save-excursion
10117         (when (or quietly
10118                   (not gnus-interactive-catchup) ;Without confirmation?
10119                   gnus-expert-user
10120                   (gnus-y-or-n-p
10121                    (if all
10122                        "Mark absolutely all articles as read? "
10123                      "Mark all unread articles as read? ")))
10124           (if (and not-mark
10125                    (not gnus-newsgroup-adaptive)
10126                    (not gnus-newsgroup-auto-expire)
10127                    (not gnus-suppress-duplicates)
10128                    (or (not gnus-use-cache)
10129                        (eq gnus-use-cache 'passive)))
10130               (progn
10131                 (when all
10132                   (setq gnus-newsgroup-marked nil
10133                         gnus-newsgroup-spam-marked nil
10134                         gnus-newsgroup-dormant nil))
10135                 (setq gnus-newsgroup-unreads
10136                       (gnus-sorted-nunion
10137                        (gnus-intersection gnus-newsgroup-unreads
10138                                           gnus-newsgroup-downloadable)
10139                        gnus-newsgroup-unfetched)))
10140             ;; We actually mark all articles as canceled, which we
10141             ;; have to do when using auto-expiry or adaptive scoring.
10142             (gnus-summary-show-all-threads)
10143             (if (and to-here reverse)
10144                 (progn
10145                   (goto-char to-here)
10146                   (while (and
10147                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10148                           (gnus-summary-find-next (not all)))))
10149               (when (gnus-summary-first-subject (not all))
10150                 (while (and
10151                         (if to-here (< (point) to-here) t)
10152                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10153                         (gnus-summary-find-next (not all))))))
10154             (gnus-set-mode-line 'summary))
10155           t))
10156     (gnus-summary-position-point)))
10157
10158 (defun gnus-summary-catchup-to-here (&optional all)
10159   "Mark all unticked articles before the current one as read.
10160 If ALL is non-nil, also mark ticked and dormant articles as read."
10161   (interactive "P")
10162   (save-excursion
10163     (gnus-save-hidden-threads
10164       (let ((beg (point)))
10165         ;; We check that there are unread articles.
10166         (when (or all (gnus-summary-find-prev))
10167           (gnus-summary-catchup all t beg)))))
10168   (gnus-summary-position-point))
10169
10170 (defun gnus-summary-catchup-from-here (&optional all)
10171   "Mark all unticked articles after the current one as read.
10172 If ALL is non-nil, also mark ticked and dormant articles as read."
10173   (interactive "P")
10174   (save-excursion
10175     (gnus-save-hidden-threads
10176       (let ((beg (point)))
10177         ;; We check that there are unread articles.
10178         (when (or all (gnus-summary-find-next))
10179           (gnus-summary-catchup all t beg nil t)))))
10180
10181   (gnus-summary-position-point))
10182 (defun gnus-summary-catchup-all (&optional quietly)
10183   "Mark all articles in this newsgroup as read.
10184 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10185 instead, which marks only unread articles as read."
10186   (interactive "P")
10187   (gnus-summary-catchup t quietly))
10188
10189 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10190   "Mark all unread articles in this group as read, then exit.
10191 If prefix argument ALL is non-nil, all articles are marked as read.
10192 If QUIETLY is non-nil, no questions will be asked."
10193   (interactive "P")
10194   (when (gnus-summary-catchup all quietly nil 'fast)
10195     ;; Select next newsgroup or exit.
10196     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10197              (eq gnus-auto-select-next 'quietly))
10198         (gnus-summary-next-group nil)
10199       (gnus-summary-exit))))
10200
10201 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10202   "Mark all articles in this newsgroup as read, and then exit.
10203 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10204 instead, which marks only unread articles as read."
10205   (interactive "P")
10206   (gnus-summary-catchup-and-exit t quietly))
10207
10208 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10209   "Mark all articles in this group as read and select the next group.
10210 If given a prefix, mark all articles, unread as well as ticked, as
10211 read."
10212   (interactive "P")
10213   (save-excursion
10214     (gnus-summary-catchup all))
10215   (gnus-summary-next-group))
10216
10217 ;;;
10218 ;;; with article
10219 ;;;
10220
10221 (defmacro gnus-with-article (article &rest forms)
10222   "Select ARTICLE and perform FORMS in the original article buffer.
10223 Then replace the article with the result."
10224   `(progn
10225      ;; We don't want the article to be marked as read.
10226      (let (gnus-mark-article-hook)
10227        (gnus-summary-select-article t t nil ,article))
10228      (set-buffer gnus-original-article-buffer)
10229      ,@forms
10230      (if (not (gnus-check-backend-function
10231                'request-replace-article (car gnus-article-current)))
10232          (gnus-message 5 "Read-only group; not replacing")
10233        (unless (gnus-request-replace-article
10234                 ,article (car gnus-article-current)
10235                 (current-buffer) t)
10236          (error "Couldn't replace article")))
10237      ;; The cache and backlog have to be flushed somewhat.
10238      (when gnus-keep-backlog
10239        (gnus-backlog-remove-article
10240         (car gnus-article-current) (cdr gnus-article-current)))
10241      (when gnus-use-cache
10242        (gnus-cache-update-article
10243         (car gnus-article-current) (cdr gnus-article-current)))))
10244
10245 (put 'gnus-with-article 'lisp-indent-function 1)
10246 (put 'gnus-with-article 'edebug-form-spec '(form body))
10247
10248 ;; Thread-based commands.
10249
10250 (defun gnus-summary-articles-in-thread (&optional article)
10251   "Return a list of all articles in the current thread.
10252 If ARTICLE is non-nil, return all articles in the thread that starts
10253 with that article."
10254   (let* ((article (or article (gnus-summary-article-number)))
10255          (data (gnus-data-find-list article))
10256          (top-level (gnus-data-level (car data)))
10257          (top-subject
10258           (cond ((null gnus-thread-operation-ignore-subject)
10259                  (gnus-simplify-subject-re
10260                   (mail-header-subject (gnus-data-header (car data)))))
10261                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10262                  (gnus-simplify-subject-fuzzy
10263                   (mail-header-subject (gnus-data-header (car data)))))
10264                 (t nil)))
10265          (end-point (save-excursion
10266                       (if (gnus-summary-go-to-next-thread)
10267                           (point) (point-max))))
10268          articles)
10269     (while (and data
10270                 (< (gnus-data-pos (car data)) end-point))
10271       (when (or (not top-subject)
10272                 (string= top-subject
10273                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10274                              (gnus-simplify-subject-fuzzy
10275                               (mail-header-subject
10276                                (gnus-data-header (car data))))
10277                            (gnus-simplify-subject-re
10278                             (mail-header-subject
10279                              (gnus-data-header (car data)))))))
10280         (push (gnus-data-number (car data)) articles))
10281       (unless (and (setq data (cdr data))
10282                    (> (gnus-data-level (car data)) top-level))
10283         (setq data nil)))
10284     ;; Return the list of articles.
10285     (nreverse articles)))
10286
10287 (defun gnus-summary-rethread-current ()
10288   "Rethread the thread the current article is part of."
10289   (interactive)
10290   (let* ((gnus-show-threads t)
10291          (article (gnus-summary-article-number))
10292          (id (mail-header-id (gnus-summary-article-header)))
10293          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10294     (unless id
10295       (error "No article on the current line"))
10296     (gnus-rebuild-thread id)
10297     (gnus-summary-goto-subject article)))
10298
10299 (defun gnus-summary-reparent-thread ()
10300   "Make the current article child of the marked (or previous) article.
10301
10302 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10303 is non-nil or the Subject: of both articles are the same."
10304   (interactive)
10305   (unless (not (gnus-group-read-only-p))
10306     (error "The current newsgroup does not support article editing"))
10307   (unless (<= (length gnus-newsgroup-processable) 1)
10308     (error "No more than one article may be marked"))
10309   (save-window-excursion
10310     (let ((gnus-article-buffer " *reparent*")
10311           (current-article (gnus-summary-article-number))
10312           ;; First grab the marked article, otherwise one line up.
10313           (parent-article (if (not (null gnus-newsgroup-processable))
10314                               (car gnus-newsgroup-processable)
10315                             (save-excursion
10316                               (if (eq (forward-line -1) 0)
10317                                   (gnus-summary-article-number)
10318                                 (error "Beginning of summary buffer"))))))
10319       (unless (not (eq current-article parent-article))
10320         (error "An article may not be self-referential"))
10321       (let ((message-id (mail-header-id
10322                          (gnus-summary-article-header parent-article))))
10323         (unless (and message-id (not (equal message-id "")))
10324           (error "No message-id in desired parent"))
10325         (gnus-with-article current-article
10326           (save-restriction
10327             (goto-char (point-min))
10328             (message-narrow-to-head)
10329             (if (re-search-forward "^References: " nil t)
10330                 (progn
10331                   (re-search-forward "^[^ \t]" nil t)
10332                   (forward-line -1)
10333                   (end-of-line)
10334                   (insert " " message-id))
10335               (insert "References: " message-id "\n"))))
10336         (set-buffer gnus-summary-buffer)
10337         (gnus-summary-unmark-all-processable)
10338         (gnus-summary-update-article current-article)
10339         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10340             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10341         (gnus-summary-rethread-current)
10342         (gnus-message 3 "Article %d is now the child of article %d"
10343                       current-article parent-article)))))
10344
10345 (defun gnus-summary-toggle-threads (&optional arg)
10346   "Toggle showing conversation threads.
10347 If ARG is positive number, turn showing conversation threads on."
10348   (interactive "P")
10349   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10350     (setq gnus-show-threads
10351           (if (null arg) (not gnus-show-threads)
10352             (> (prefix-numeric-value arg) 0)))
10353     (gnus-summary-prepare)
10354     (gnus-summary-goto-subject current)
10355     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10356     (gnus-summary-position-point)))
10357
10358 (defun gnus-summary-show-all-threads ()
10359   "Show all threads."
10360   (interactive)
10361   (save-excursion
10362     (let ((buffer-read-only nil))
10363       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10364   (gnus-summary-position-point))
10365
10366 (defun gnus-summary-show-thread ()
10367   "Show thread subtrees.
10368 Returns nil if no thread was there to be shown."
10369   (interactive)
10370   (let ((buffer-read-only nil)
10371         (orig (point))
10372         (end (gnus-point-at-eol))
10373         ;; Leave point at bol
10374         (beg (progn (beginning-of-line) (point))))
10375     (prog1
10376         ;; Any hidden lines here?
10377         (search-forward "\r" end t)
10378       (subst-char-in-region beg end ?\^M ?\n t)
10379       (goto-char orig)
10380       (gnus-summary-position-point))))
10381
10382 (defun gnus-summary-maybe-hide-threads ()
10383   "If requested, hide the threads that should be hidden."
10384   (when (and gnus-show-threads
10385              gnus-thread-hide-subtree)
10386     (gnus-summary-hide-all-threads
10387      (if (or (consp gnus-thread-hide-subtree)
10388              (gnus-functionp gnus-thread-hide-subtree))
10389          (gnus-make-predicate gnus-thread-hide-subtree)
10390        nil))))
10391
10392 ;;; Hiding predicates.
10393
10394 (defun gnus-article-unread-p (header)
10395   (memq (mail-header-number header) gnus-newsgroup-unreads))
10396
10397 (defun gnus-article-unseen-p (header)
10398   (memq (mail-header-number header) gnus-newsgroup-unseen))
10399
10400 (defun gnus-map-articles (predicate articles)
10401   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10402   (apply 'gnus-or (mapcar predicate
10403                           (mapcar 'gnus-summary-article-header articles))))
10404
10405 (defun gnus-summary-hide-all-threads (&optional predicate)
10406   "Hide all thread subtrees.
10407 If PREDICATE is supplied, threads that satisfy this predicate
10408 will not be hidden."
10409   (interactive)
10410   (save-excursion
10411     (goto-char (point-min))
10412     (let ((end nil))
10413       (while (not end)
10414         (when (or (not predicate)
10415                   (gnus-map-articles
10416                    predicate (gnus-summary-article-children)))
10417             (gnus-summary-hide-thread))
10418         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10419   (gnus-summary-position-point))
10420
10421 (defun gnus-summary-hide-thread ()
10422   "Hide thread subtrees.
10423 If PREDICATE is supplied, threads that satisfy this predicate
10424 will not be hidden.
10425 Returns nil if no threads were there to be hidden."
10426   (interactive)
10427   (let ((buffer-read-only nil)
10428         (start (point))
10429         (article (gnus-summary-article-number)))
10430     (goto-char start)
10431     ;; Go forward until either the buffer ends or the subthread
10432     ;; ends.
10433     (when (and (not (eobp))
10434                (or (zerop (gnus-summary-next-thread 1 t))
10435                    (goto-char (point-max))))
10436       (prog1
10437           (if (and (> (point) start)
10438                    (search-backward "\n" start t))
10439               (progn
10440                 (subst-char-in-region start (point) ?\n ?\^M)
10441                 (gnus-summary-goto-subject article))
10442             (goto-char start)
10443             nil)))))
10444
10445 (defun gnus-summary-go-to-next-thread (&optional previous)
10446   "Go to the same level (or less) next thread.
10447 If PREVIOUS is non-nil, go to previous thread instead.
10448 Return the article number moved to, or nil if moving was impossible."
10449   (let ((level (gnus-summary-thread-level))
10450         (way (if previous -1 1))
10451         (beg (point)))
10452     (forward-line way)
10453     (while (and (not (eobp))
10454                 (< level (gnus-summary-thread-level)))
10455       (forward-line way))
10456     (if (eobp)
10457         (progn
10458           (goto-char beg)
10459           nil)
10460       (setq beg (point))
10461       (prog1
10462           (gnus-summary-article-number)
10463         (goto-char beg)))))
10464
10465 (defun gnus-summary-next-thread (n &optional silent)
10466   "Go to the same level next N'th thread.
10467 If N is negative, search backward instead.
10468 Returns the difference between N and the number of skips actually
10469 done.
10470
10471 If SILENT, don't output messages."
10472   (interactive "p")
10473   (let ((backward (< n 0))
10474         (n (abs n)))
10475     (while (and (> n 0)
10476                 (gnus-summary-go-to-next-thread backward))
10477       (decf n))
10478     (unless silent
10479       (gnus-summary-position-point))
10480     (when (and (not silent) (/= 0 n))
10481       (gnus-message 7 "No more threads"))
10482     n))
10483
10484 (defun gnus-summary-prev-thread (n)
10485   "Go to the same level previous N'th thread.
10486 Returns the difference between N and the number of skips actually
10487 done."
10488   (interactive "p")
10489   (gnus-summary-next-thread (- n)))
10490
10491 (defun gnus-summary-go-down-thread ()
10492   "Go down one level in the current thread."
10493   (let ((children (gnus-summary-article-children)))
10494     (when children
10495       (gnus-summary-goto-subject (car children)))))
10496
10497 (defun gnus-summary-go-up-thread ()
10498   "Go up one level in the current thread."
10499   (let ((parent (gnus-summary-article-parent)))
10500     (when parent
10501       (gnus-summary-goto-subject parent))))
10502
10503 (defun gnus-summary-down-thread (n)
10504   "Go down thread N steps.
10505 If N is negative, go up instead.
10506 Returns the difference between N and how many steps down that were
10507 taken."
10508   (interactive "p")
10509   (let ((up (< n 0))
10510         (n (abs n)))
10511     (while (and (> n 0)
10512                 (if up (gnus-summary-go-up-thread)
10513                   (gnus-summary-go-down-thread)))
10514       (setq n (1- n)))
10515     (gnus-summary-position-point)
10516     (when (/= 0 n)
10517       (gnus-message 7 "Can't go further"))
10518     n))
10519
10520 (defun gnus-summary-up-thread (n)
10521   "Go up thread N steps.
10522 If N is negative, go down instead.
10523 Returns the difference between N and how many steps down that were
10524 taken."
10525   (interactive "p")
10526   (gnus-summary-down-thread (- n)))
10527
10528 (defun gnus-summary-top-thread ()
10529   "Go to the top of the thread."
10530   (interactive)
10531   (while (gnus-summary-go-up-thread))
10532   (gnus-summary-article-number))
10533
10534 (defun gnus-summary-kill-thread (&optional unmark)
10535   "Mark articles under current thread as read.
10536 If the prefix argument is positive, remove any kinds of marks.
10537 If the prefix argument is negative, tick articles instead."
10538   (interactive "P")
10539   (when unmark
10540     (setq unmark (prefix-numeric-value unmark)))
10541   (let ((articles (gnus-summary-articles-in-thread)))
10542     (save-excursion
10543       ;; Expand the thread.
10544       (gnus-summary-show-thread)
10545       ;; Mark all the articles.
10546       (while articles
10547         (gnus-summary-goto-subject (car articles))
10548         (cond ((null unmark)
10549                (gnus-summary-mark-article-as-read gnus-killed-mark))
10550               ((> unmark 0)
10551                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10552               (t
10553                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10554         (setq articles (cdr articles))))
10555     ;; Hide killed subtrees.
10556     (and (null unmark)
10557          gnus-thread-hide-killed
10558          (gnus-summary-hide-thread))
10559     ;; If marked as read, go to next unread subject.
10560     (when (null unmark)
10561       ;; Go to next unread subject.
10562       (gnus-summary-next-subject 1 t)))
10563   (gnus-set-mode-line 'summary))
10564
10565 ;; Summary sorting commands
10566
10567 (defun gnus-summary-sort-by-number (&optional reverse)
10568   "Sort the summary buffer by article number.
10569 Argument REVERSE means reverse order."
10570   (interactive "P")
10571   (gnus-summary-sort 'number reverse))
10572
10573 (defun gnus-summary-sort-by-random (&optional reverse)
10574   "Randomize the order in the summary buffer.
10575 Argument REVERSE means to randomize in reverse order."
10576   (interactive "P")
10577   (gnus-summary-sort 'random reverse))
10578
10579 (defun gnus-summary-sort-by-author (&optional reverse)
10580   "Sort the summary buffer by author name alphabetically.
10581 If `case-fold-search' is non-nil, case of letters is ignored.
10582 Argument REVERSE means reverse order."
10583   (interactive "P")
10584   (gnus-summary-sort 'author reverse))
10585
10586 (defun gnus-summary-sort-by-subject (&optional reverse)
10587   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10588 If `case-fold-search' is non-nil, case of letters is ignored.
10589 Argument REVERSE means reverse order."
10590   (interactive "P")
10591   (gnus-summary-sort 'subject reverse))
10592
10593 (defun gnus-summary-sort-by-date (&optional reverse)
10594   "Sort the summary buffer by date.
10595 Argument REVERSE means reverse order."
10596   (interactive "P")
10597   (gnus-summary-sort 'date reverse))
10598
10599 (defun gnus-summary-sort-by-score (&optional reverse)
10600   "Sort the summary buffer by score.
10601 Argument REVERSE means reverse order."
10602   (interactive "P")
10603   (gnus-summary-sort 'score reverse))
10604
10605 (defun gnus-summary-sort-by-lines (&optional reverse)
10606   "Sort the summary buffer by the number of lines.
10607 Argument REVERSE means reverse order."
10608   (interactive "P")
10609   (gnus-summary-sort 'lines reverse))
10610
10611 (defun gnus-summary-sort-by-chars (&optional reverse)
10612   "Sort the summary buffer by article length.
10613 Argument REVERSE means reverse order."
10614   (interactive "P")
10615   (gnus-summary-sort 'chars reverse))
10616
10617 (defun gnus-summary-sort-by-original (&optional reverse)
10618   "Sort the summary buffer using the default sorting method.
10619 Argument REVERSE means reverse order."
10620   (interactive "P")
10621   (let* ((buffer-read-only)
10622          (gnus-summary-prepare-hook nil))
10623     ;; We do the sorting by regenerating the threads.
10624     (gnus-summary-prepare)
10625     ;; Hide subthreads if needed.
10626     (gnus-summary-maybe-hide-threads)))
10627
10628 (defun gnus-summary-sort (predicate reverse)
10629   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10630   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10631          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10632          (gnus-thread-sort-functions
10633           (if (not reverse)
10634               thread
10635             `(lambda (t1 t2)
10636                (,thread t2 t1))))
10637          (gnus-sort-gathered-threads-function
10638           gnus-thread-sort-functions)
10639          (gnus-article-sort-functions
10640           (if (not reverse)
10641               article
10642             `(lambda (t1 t2)
10643                (,article t2 t1))))
10644          (buffer-read-only)
10645          (gnus-summary-prepare-hook nil))
10646     ;; We do the sorting by regenerating the threads.
10647     (gnus-summary-prepare)
10648     ;; Hide subthreads if needed.
10649     (gnus-summary-maybe-hide-threads)))
10650
10651 ;; Summary saving commands.
10652
10653 (defun gnus-summary-save-article (&optional n not-saved)
10654   "Save the current article using the default saver function.
10655 If N is a positive number, save the N next articles.
10656 If N is a negative number, save the N previous articles.
10657 If N is nil and any articles have been marked with the process mark,
10658 save those articles instead.
10659 The variable `gnus-default-article-saver' specifies the saver function."
10660   (interactive "P")
10661   (let* ((articles (gnus-summary-work-articles n))
10662          (save-buffer (save-excursion
10663                         (nnheader-set-temp-buffer " *Gnus Save*")))
10664          (num (length articles))
10665          header file)
10666     (dolist (article articles)
10667       (setq header (gnus-summary-article-header article))
10668       (if (not (vectorp header))
10669           ;; This is a pseudo-article.
10670           (if (assq 'name header)
10671               (gnus-copy-file (cdr (assq 'name header)))
10672             (gnus-message 1 "Article %d is unsaveable" article))
10673         ;; This is a real article.
10674         (save-window-excursion
10675           (let ((gnus-display-mime-function nil)
10676                 (gnus-article-prepare-hook nil))
10677             (gnus-summary-select-article t nil nil article)))
10678         (save-excursion
10679           (set-buffer save-buffer)
10680           (erase-buffer)
10681           (insert-buffer-substring gnus-original-article-buffer))
10682         (setq file (gnus-article-save save-buffer file num))
10683         (gnus-summary-remove-process-mark article)
10684         (unless not-saved
10685           (gnus-summary-set-saved-mark article))))
10686     (gnus-kill-buffer save-buffer)
10687     (gnus-summary-position-point)
10688     (gnus-set-mode-line 'summary)
10689     n))
10690
10691 (defun gnus-summary-pipe-output (&optional arg headers)
10692   "Pipe the current article to a subprocess.
10693 If N is a positive number, pipe the N next articles.
10694 If N is a negative number, pipe the N previous articles.
10695 If N is nil and any articles have been marked with the process mark,
10696 pipe those articles instead.
10697 If HEADERS (the symbolic prefix), include the headers, too."
10698   (interactive (gnus-interactive "P\ny"))
10699   (require 'gnus-art)
10700   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10701         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10702     (gnus-summary-save-article arg t))
10703   (let ((buffer (get-buffer "*Shell Command Output*")))
10704     (when (and buffer
10705                (not (zerop (buffer-size buffer))))
10706       (gnus-configure-windows 'pipe))))
10707
10708 (defun gnus-summary-save-article-mail (&optional arg)
10709   "Append the current article to an mail file.
10710 If N is a positive number, save the N next articles.
10711 If N is a negative number, save the N previous articles.
10712 If N is nil and any articles have been marked with the process mark,
10713 save those articles instead."
10714   (interactive "P")
10715   (require 'gnus-art)
10716   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10717     (gnus-summary-save-article arg)))
10718
10719 (defun gnus-summary-save-article-rmail (&optional arg)
10720   "Append the current article to an rmail file.
10721 If N is a positive number, save the N next articles.
10722 If N is a negative number, save the N previous articles.
10723 If N is nil and any articles have been marked with the process mark,
10724 save those articles instead."
10725   (interactive "P")
10726   (require 'gnus-art)
10727   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10728     (gnus-summary-save-article arg)))
10729
10730 (defun gnus-summary-save-article-file (&optional arg)
10731   "Append the current article to a file.
10732 If N is a positive number, save the N next articles.
10733 If N is a negative number, save the N previous articles.
10734 If N is nil and any articles have been marked with the process mark,
10735 save those articles instead."
10736   (interactive "P")
10737   (require 'gnus-art)
10738   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10739     (gnus-summary-save-article arg)))
10740
10741 (defun gnus-summary-write-article-file (&optional arg)
10742   "Write the current article to a file, deleting the previous file.
10743 If N is a positive number, save the N next articles.
10744 If N is a negative number, save the N previous articles.
10745 If N is nil and any articles have been marked with the process mark,
10746 save those articles instead."
10747   (interactive "P")
10748   (require 'gnus-art)
10749   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10750     (gnus-summary-save-article arg)))
10751
10752 (defun gnus-summary-save-article-body-file (&optional arg)
10753   "Append the current article body to a file.
10754 If N is a positive number, save the N next articles.
10755 If N is a negative number, save the N previous articles.
10756 If N is nil and any articles have been marked with the process mark,
10757 save those articles instead."
10758   (interactive "P")
10759   (require 'gnus-art)
10760   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10761     (gnus-summary-save-article arg)))
10762
10763 (defun gnus-summary-muttprint (&optional arg)
10764   "Print the current article using Muttprint.
10765 If N is a positive number, save the N next articles.
10766 If N is a negative number, save the N previous articles.
10767 If N is nil and any articles have been marked with the process mark,
10768 save those articles instead."
10769   (interactive "P")
10770   (require 'gnus-art)
10771   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10772     (gnus-summary-save-article arg t)))
10773
10774 (defun gnus-summary-pipe-message (program)
10775   "Pipe the current article through PROGRAM."
10776   (interactive "sProgram: ")
10777   (gnus-summary-select-article)
10778   (let ((mail-header-separator ""))
10779     (gnus-eval-in-buffer-window gnus-article-buffer
10780       (save-restriction
10781         (widen)
10782         (let ((start (window-start))
10783               buffer-read-only)
10784           (message-pipe-buffer-body program)
10785           (set-window-start (get-buffer-window (current-buffer)) start))))))
10786
10787 (defun gnus-get-split-value (methods)
10788   "Return a value based on the split METHODS."
10789   (let (split-name method result match)
10790     (when methods
10791       (save-excursion
10792         (set-buffer gnus-original-article-buffer)
10793         (save-restriction
10794           (nnheader-narrow-to-headers)
10795           (while (and methods (not split-name))
10796             (goto-char (point-min))
10797             (setq method (pop methods))
10798             (setq match (car method))
10799             (when (cond
10800                    ((stringp match)
10801                     ;; Regular expression.
10802                     (ignore-errors
10803                       (re-search-forward match nil t)))
10804                    ((gnus-functionp match)
10805                     ;; Function.
10806                     (save-restriction
10807                       (widen)
10808                       (setq result (funcall match gnus-newsgroup-name))))
10809                    ((consp match)
10810                     ;; Form.
10811                     (save-restriction
10812                       (widen)
10813                       (setq result (eval match)))))
10814               (setq split-name (cdr method))
10815               (cond ((stringp result)
10816                      (push (expand-file-name
10817                             result gnus-article-save-directory)
10818                            split-name))
10819                     ((consp result)
10820                      (setq split-name (append result split-name)))))))))
10821     (nreverse split-name)))
10822
10823 (defun gnus-valid-move-group-p (group)
10824   (and (boundp group)
10825        (symbol-name group)
10826        (symbol-value group)
10827        (gnus-get-function (gnus-find-method-for-group
10828                            (symbol-name group)) 'request-accept-article t)))
10829
10830 (defun gnus-read-move-group-name (prompt default articles prefix)
10831   "Read a group name."
10832   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10833          (minibuffer-confirm-incomplete nil) ; XEmacs
10834          (prom
10835           (format "%s %s to:"
10836                   prompt
10837                   (if (> (length articles) 1)
10838                       (format "these %d articles" (length articles))
10839                     "this article")))
10840          (to-newsgroup
10841           (cond
10842            ((null split-name)
10843             (gnus-completing-read-with-default
10844              default prom
10845              gnus-active-hashtb
10846              'gnus-valid-move-group-p
10847              nil prefix
10848              'gnus-group-history))
10849            ((= 1 (length split-name))
10850             (gnus-completing-read-with-default
10851              (car split-name) prom
10852              gnus-active-hashtb
10853              'gnus-valid-move-group-p
10854              nil nil
10855              'gnus-group-history))
10856            (t
10857             (gnus-completing-read-with-default
10858              nil prom
10859              (mapcar (lambda (el) (list el))
10860                      (nreverse split-name))
10861              nil nil nil
10862              'gnus-group-history))))
10863          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10864     (when to-newsgroup
10865       (if (or (string= to-newsgroup "")
10866               (string= to-newsgroup prefix))
10867           (setq to-newsgroup default))
10868       (unless to-newsgroup
10869         (error "No group name entered"))
10870       (or (gnus-active to-newsgroup)
10871           (gnus-activate-group to-newsgroup nil nil to-method)
10872           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10873                                      to-newsgroup))
10874               (or (and (gnus-request-create-group to-newsgroup to-method)
10875                        (gnus-activate-group
10876                         to-newsgroup nil nil to-method)
10877                        (gnus-subscribe-group to-newsgroup))
10878                   (error "Couldn't create group %s" to-newsgroup)))
10879           (error "No such group: %s" to-newsgroup)))
10880     to-newsgroup))
10881
10882 (defun gnus-summary-save-parts (type dir n &optional reverse)
10883   "Save parts matching TYPE to DIR.
10884 If REVERSE, save parts that do not match TYPE."
10885   (interactive
10886    (list (read-string "Save parts of type: "
10887                       (or (car gnus-summary-save-parts-type-history)
10888                           gnus-summary-save-parts-default-mime)
10889                       'gnus-summary-save-parts-type-history)
10890          (setq gnus-summary-save-parts-last-directory
10891                (read-file-name "Save to directory: "
10892                                gnus-summary-save-parts-last-directory
10893                                nil t))
10894          current-prefix-arg))
10895   (gnus-summary-iterate n
10896     (let ((gnus-display-mime-function nil)
10897           (gnus-inhibit-treatment t))
10898       (gnus-summary-select-article))
10899     (save-excursion
10900       (set-buffer gnus-article-buffer)
10901       (let ((handles (or gnus-article-mime-handles
10902                          (mm-dissect-buffer nil gnus-article-loose-mime)
10903                          (and gnus-article-emulate-mime
10904                               (mm-uu-dissect)))))
10905         (when handles
10906           (gnus-summary-save-parts-1 type dir handles reverse)
10907           (unless gnus-article-mime-handles ;; Don't destroy this case.
10908             (mm-destroy-parts handles)))))))
10909
10910 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10911   (if (stringp (car handle))
10912       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10913               (cdr handle))
10914     (when (if reverse
10915               (not (string-match type (mm-handle-media-type handle)))
10916             (string-match type (mm-handle-media-type handle)))
10917       (let ((file (expand-file-name
10918                    (file-name-nondirectory
10919                     (or
10920                      (mail-content-type-get
10921                       (mm-handle-disposition handle) 'filename)
10922                      (concat gnus-newsgroup-name
10923                              "." (number-to-string
10924                                   (cdr gnus-article-current)))))
10925                    dir)))
10926         (unless (file-exists-p file)
10927           (mm-save-part-to-file handle file))))))
10928
10929 ;; Summary extract commands
10930
10931 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10932   (let ((buffer-read-only nil)
10933         (article (gnus-summary-article-number))
10934         after-article b e)
10935     (unless (gnus-summary-goto-subject article)
10936       (error "No such article: %d" article))
10937     (gnus-summary-position-point)
10938     ;; If all commands are to be bunched up on one line, we collect
10939     ;; them here.
10940     (unless gnus-view-pseudos-separately
10941       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10942             files action)
10943         (while ps
10944           (setq action (cdr (assq 'action (car ps))))
10945           (setq files (list (cdr (assq 'name (car ps)))))
10946           (while (and ps (cdr ps)
10947                       (string= (or action "1")
10948                                (or (cdr (assq 'action (cadr ps))) "2")))
10949             (push (cdr (assq 'name (cadr ps))) files)
10950             (setcdr ps (cddr ps)))
10951           (when files
10952             (when (not (string-match "%s" action))
10953               (push " " files))
10954             (push " " files)
10955             (when (assq 'execute (car ps))
10956               (setcdr (assq 'execute (car ps))
10957                       (funcall (if (string-match "%s" action)
10958                                    'format 'concat)
10959                                action
10960                                (mapconcat
10961                                 (lambda (f)
10962                                   (if (equal f " ")
10963                                       f
10964                                     (mm-quote-arg f)))
10965                                 files " ")))))
10966           (setq ps (cdr ps)))))
10967     (if (and gnus-view-pseudos (not not-view))
10968         (while pslist
10969           (when (assq 'execute (car pslist))
10970             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10971                                   (eq gnus-view-pseudos 'not-confirm)))
10972           (setq pslist (cdr pslist)))
10973       (save-excursion
10974         (while pslist
10975           (setq after-article (or (cdr (assq 'article (car pslist)))
10976                                   (gnus-summary-article-number)))
10977           (gnus-summary-goto-subject after-article)
10978           (forward-line 1)
10979           (setq b (point))
10980           (insert "    " (file-name-nondirectory
10981                           (cdr (assq 'name (car pslist))))
10982                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10983           (setq e (point))
10984           (forward-line -1)             ; back to `b'
10985           (gnus-add-text-properties
10986            b (1- e) (list 'gnus-number gnus-reffed-article-number
10987                           gnus-mouse-face-prop gnus-mouse-face))
10988           (gnus-data-enter
10989            after-article gnus-reffed-article-number
10990            gnus-unread-mark b (car pslist) 0 (- e b))
10991           (setq gnus-newsgroup-unreads
10992                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10993                                          gnus-reffed-article-number))
10994           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10995           (setq pslist (cdr pslist)))))))
10996
10997 (defun gnus-pseudos< (p1 p2)
10998   (let ((c1 (cdr (assq 'action p1)))
10999         (c2 (cdr (assq 'action p2))))
11000     (and c1 c2 (string< c1 c2))))
11001
11002 (defun gnus-request-pseudo-article (props)
11003   (cond ((assq 'execute props)
11004          (gnus-execute-command (cdr (assq 'execute props)))))
11005   (let ((gnus-current-article (gnus-summary-article-number)))
11006     (gnus-run-hooks 'gnus-mark-article-hook)))
11007
11008 (defun gnus-execute-command (command &optional automatic)
11009   (save-excursion
11010     (gnus-article-setup-buffer)
11011     (set-buffer gnus-article-buffer)
11012     (setq buffer-read-only nil)
11013     (let ((command (if automatic command
11014                      (read-string "Command: " (cons command 0)))))
11015       (erase-buffer)
11016       (insert "$ " command "\n\n")
11017       (if gnus-view-pseudo-asynchronously
11018           (start-process "gnus-execute" (current-buffer) shell-file-name
11019                          shell-command-switch command)
11020         (call-process shell-file-name nil t nil
11021                       shell-command-switch command)))))
11022
11023 ;; Summary kill commands.
11024
11025 (defun gnus-summary-edit-global-kill (article)
11026   "Edit the \"global\" kill file."
11027   (interactive (list (gnus-summary-article-number)))
11028   (gnus-group-edit-global-kill article))
11029
11030 (defun gnus-summary-edit-local-kill ()
11031   "Edit a local kill file applied to the current newsgroup."
11032   (interactive)
11033   (setq gnus-current-headers (gnus-summary-article-header))
11034   (gnus-group-edit-local-kill
11035    (gnus-summary-article-number) gnus-newsgroup-name))
11036
11037 ;;; Header reading.
11038
11039 (defun gnus-read-header (id &optional header)
11040   "Read the headers of article ID and enter them into the Gnus system."
11041   (let ((group gnus-newsgroup-name)
11042         (gnus-override-method
11043          (or
11044           gnus-override-method
11045           (and (gnus-news-group-p gnus-newsgroup-name)
11046                (car (gnus-refer-article-methods)))))
11047         where)
11048     ;; First we check to see whether the header in question is already
11049     ;; fetched.
11050     (if (stringp id)
11051         ;; This is a Message-ID.
11052         (setq header (or header (gnus-id-to-header id)))
11053       ;; This is an article number.
11054       (setq header (or header (gnus-summary-article-header id))))
11055     (if (and header
11056              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11057         ;; We have found the header.
11058         header
11059       ;; If this is a sparse article, we have to nix out its
11060       ;; previous entry in the thread hashtb.
11061       (when (and header
11062                  (gnus-summary-article-sparse-p (mail-header-number header)))
11063         (let* ((parent (gnus-parent-id (mail-header-references header)))
11064                (thread (and parent (gnus-id-to-thread parent))))
11065           (when thread
11066             (delq (assq header thread) thread))))
11067       ;; We have to really fetch the header to this article.
11068       (save-excursion
11069         (set-buffer nntp-server-buffer)
11070         (when (setq where (gnus-request-head id group))
11071           (nnheader-fold-continuation-lines)
11072           (goto-char (point-max))
11073           (insert ".\n")
11074           (goto-char (point-min))
11075           (insert "211 ")
11076           (princ (cond
11077                   ((numberp id) id)
11078                   ((cdr where) (cdr where))
11079                   (header (mail-header-number header))
11080                   (t gnus-reffed-article-number))
11081                  (current-buffer))
11082           (insert " Article retrieved.\n"))
11083         (if (or (not where)
11084                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11085             ()                          ; Malformed head.
11086           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11087             (when (and (stringp id)
11088                        (not (string= (gnus-group-real-name group)
11089                                      (car where))))
11090               ;; If we fetched by Message-ID and the article came
11091               ;; from a different group, we fudge some bogus article
11092               ;; numbers for this article.
11093               (mail-header-set-number header gnus-reffed-article-number))
11094             (save-excursion
11095               (set-buffer gnus-summary-buffer)
11096               (decf gnus-reffed-article-number)
11097               (gnus-remove-header (mail-header-number header))
11098               (push header gnus-newsgroup-headers)
11099               (setq gnus-current-headers header)
11100               (push (mail-header-number header) gnus-newsgroup-limit)))
11101           header)))))
11102
11103 (defun gnus-remove-header (number)
11104   "Remove header NUMBER from `gnus-newsgroup-headers'."
11105   (if (and gnus-newsgroup-headers
11106            (= number (mail-header-number (car gnus-newsgroup-headers))))
11107       (pop gnus-newsgroup-headers)
11108     (let ((headers gnus-newsgroup-headers))
11109       (while (and (cdr headers)
11110                   (not (= number (mail-header-number (cadr headers)))))
11111         (pop headers))
11112       (when (cdr headers)
11113         (setcdr headers (cddr headers))))))
11114
11115 ;;;
11116 ;;; summary highlights
11117 ;;;
11118
11119 (defun gnus-highlight-selected-summary ()
11120   "Highlight selected article in summary buffer."
11121   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11122   (when gnus-summary-selected-face
11123     (save-excursion
11124       (let* ((beg (gnus-point-at-bol))
11125              (end (gnus-point-at-eol))
11126              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11127              (from (if (get-text-property beg gnus-mouse-face-prop)
11128                        beg
11129                      (or (next-single-property-change
11130                           beg gnus-mouse-face-prop nil end)
11131                          beg)))
11132              (to
11133               (if (= from end)
11134                   (- from 2)
11135                 (or (next-single-property-change
11136                      from gnus-mouse-face-prop nil end)
11137                     end))))
11138         ;; If no mouse-face prop on line we will have to = from = end,
11139         ;; so we highlight the entire line instead.
11140         (when (= (+ to 2) from)
11141           (setq from beg)
11142           (setq to end))
11143         (if gnus-newsgroup-selected-overlay
11144             ;; Move old overlay.
11145             (gnus-move-overlay
11146              gnus-newsgroup-selected-overlay from to (current-buffer))
11147           ;; Create new overlay.
11148           (gnus-overlay-put
11149            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11150            'face gnus-summary-selected-face))))))
11151
11152 (defvar gnus-summary-highlight-line-cached nil)
11153 (defvar gnus-summary-highlight-line-trigger nil)
11154
11155 (defun gnus-summary-highlight-line-0 ()
11156   (if (and (eq gnus-summary-highlight-line-trigger 
11157                gnus-summary-highlight)
11158            gnus-summary-highlight-line-cached)
11159       gnus-summary-highlight-line-cached
11160     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11161           gnus-summary-highlight-line-cached
11162           (let* ((cond (list 'cond))
11163                  (c cond)
11164                  (list gnus-summary-highlight))
11165             (while list
11166               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11167                               nil))
11168               (setq c (cdr c)
11169                     list (cdr list)))
11170             (gnus-byte-compile (list 'lambda nil cond))))))
11171
11172 (defun gnus-summary-highlight-line ()
11173   "Highlight current line according to `gnus-summary-highlight'."
11174   (let* ((beg (gnus-point-at-bol))
11175          (article (or (gnus-summary-article-number) gnus-current-article))
11176          (score (or (cdr (assq article
11177                                gnus-newsgroup-scored))
11178                     gnus-summary-default-score 0))
11179          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11180          (inhibit-read-only t)
11181          (default gnus-summary-default-score)
11182          (default-high gnus-summary-default-high-score)
11183          (default-low gnus-summary-default-low-score)
11184          (uncached (memq article gnus-newsgroup-undownloaded))
11185          (downloaded (not uncached)))
11186     (let ((face (funcall (gnus-summary-highlight-line-0))))
11187       (unless (eq face (get-text-property beg 'face))
11188         (gnus-put-text-property-excluding-characters-with-faces
11189          beg (gnus-point-at-eol) 'face
11190          (setq face (if (boundp face) (symbol-value face) face)))
11191         (when gnus-summary-highlight-line-function
11192           (funcall gnus-summary-highlight-line-function article face))))))
11193
11194 (defun gnus-update-read-articles (group unread &optional compute)
11195   "Update the list of read articles in GROUP.
11196 UNREAD is a sorted list."
11197   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11198          (entry (gnus-gethash group gnus-newsrc-hashtb))
11199          (info (nth 2 entry))
11200          (prev 1)
11201          read)
11202     (if (or (not info) (not active))
11203         ;; There is no info on this group if it was, in fact,
11204         ;; killed.  Gnus stores no information on killed groups, so
11205         ;; there's nothing to be done.
11206         ;; One could store the information somewhere temporarily,
11207         ;; perhaps...  Hmmm...
11208         ()
11209       ;; Remove any negative articles numbers.
11210       (while (and unread (< (car unread) 0))
11211         (setq unread (cdr unread)))
11212       ;; Remove any expired article numbers
11213       (while (and unread (< (car unread) (car active)))
11214         (setq unread (cdr unread)))
11215       ;; Compute the ranges of read articles by looking at the list of
11216       ;; unread articles.
11217       (while unread
11218         (when (/= (car unread) prev)
11219           (push (if (= prev (1- (car unread))) prev
11220                   (cons prev (1- (car unread))))
11221                 read))
11222         (setq prev (1+ (car unread)))
11223         (setq unread (cdr unread)))
11224       (when (<= prev (cdr active))
11225         (push (cons prev (cdr active)) read))
11226       (setq read (if (> (length read) 1) (nreverse read) read))
11227       (if compute
11228           read
11229         (save-excursion
11230           (let (setmarkundo)
11231             ;; Propagate the read marks to the backend.
11232             (when (gnus-check-backend-function 'request-set-mark group)
11233               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11234                     (add (gnus-remove-from-range read (gnus-info-read info))))
11235                 (when (or add del)
11236                   (unless (gnus-check-group group)
11237                     (error "Can't open server for %s" group))
11238                   (gnus-request-set-mark
11239                    group (delq nil (list (if add (list add 'add '(read)))
11240                                          (if del (list del 'del '(read))))))
11241                   (setq setmarkundo
11242                         `(gnus-request-set-mark
11243                           ,group
11244                           ',(delq nil (list
11245                                        (if del (list del 'add '(read)))
11246                                        (if add (list add 'del '(read))))))))))
11247             (set-buffer gnus-group-buffer)
11248             (gnus-undo-register
11249               `(progn
11250                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11251                  (gnus-info-set-read ',info ',(gnus-info-read info))
11252                  (gnus-get-unread-articles-in-group ',info
11253                                                     (gnus-active ,group))
11254                  (gnus-group-update-group ,group t)
11255                  ,setmarkundo))))
11256         ;; Enter this list into the group info.
11257         (gnus-info-set-read info read)
11258         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11259         (gnus-get-unread-articles-in-group info (gnus-active group))
11260         t))))
11261
11262 (defun gnus-offer-save-summaries ()
11263   "Offer to save all active summary buffers."
11264   (let (buffers)
11265     ;; Go through all buffers and find all summaries.
11266     (dolist (buffer (buffer-list))
11267       (when (and (setq buffer (buffer-name buffer))
11268                  (string-match "Summary" buffer)
11269                  (save-excursion
11270                    (set-buffer buffer)
11271                    ;; We check that this is, indeed, a summary buffer.
11272                    (and (eq major-mode 'gnus-summary-mode)
11273                         ;; Also make sure this isn't bogus.
11274                         gnus-newsgroup-prepared
11275                         ;; Also make sure that this isn't a
11276                         ;; dead summary buffer.
11277                         (not gnus-dead-summary-mode))))
11278         (push buffer buffers)))
11279     ;; Go through all these summary buffers and offer to save them.
11280     (when buffers
11281       (save-excursion
11282         (map-y-or-n-p
11283          "Update summary buffer %s? "
11284          (lambda (buf)
11285            (switch-to-buffer buf)
11286            (gnus-summary-exit))
11287          buffers)))))
11288
11289 (defun gnus-summary-setup-default-charset ()
11290   "Setup newsgroup default charset."
11291   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11292       (setq gnus-newsgroup-charset nil)
11293     (let* ((ignored-charsets
11294             (or gnus-newsgroup-ephemeral-ignored-charsets
11295                 (append
11296                  (and gnus-newsgroup-name
11297                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11298                  gnus-newsgroup-ignored-charsets))))
11299       (setq gnus-newsgroup-charset
11300             (or gnus-newsgroup-ephemeral-charset
11301                 (and gnus-newsgroup-name
11302                      (gnus-parameter-charset gnus-newsgroup-name))
11303                 gnus-default-charset))
11304       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11305            ignored-charsets))))
11306
11307 ;;;
11308 ;;; Mime Commands
11309 ;;;
11310
11311 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11312   "Display the current article buffer fully MIME-buttonized.
11313 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11314 treated as multipart/mixed."
11315   (interactive "P")
11316   (require 'gnus-art)
11317   (let ((gnus-unbuttonized-mime-types nil)
11318         (gnus-mime-display-multipart-as-mixed show-all-parts))
11319     (gnus-summary-show-article)))
11320
11321 (defun gnus-summary-repair-multipart (article)
11322   "Add a Content-Type header to a multipart article without one."
11323   (interactive (list (gnus-summary-article-number)))
11324   (gnus-with-article article
11325     (message-narrow-to-head)
11326     (message-remove-header "Mime-Version")
11327     (goto-char (point-max))
11328     (insert "Mime-Version: 1.0\n")
11329     (widen)
11330     (when (search-forward "\n--" nil t)
11331       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11332         (message-narrow-to-head)
11333         (message-remove-header "Content-Type")
11334         (goto-char (point-max))
11335         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11336                         separator))
11337         (widen))))
11338   (let (gnus-mark-article-hook)
11339     (gnus-summary-select-article t t nil article)))
11340
11341 (defun gnus-summary-toggle-display-buttonized ()
11342   "Toggle the buttonizing of the article buffer."
11343   (interactive)
11344   (require 'gnus-art)
11345   (if (setq gnus-inhibit-mime-unbuttonizing
11346             (not gnus-inhibit-mime-unbuttonizing))
11347       (let ((gnus-unbuttonized-mime-types nil))
11348         (gnus-summary-show-article))
11349     (gnus-summary-show-article)))
11350
11351 ;;;
11352 ;;; Generic summary marking commands
11353 ;;;
11354
11355 (defvar gnus-summary-marking-alist
11356   '((read gnus-del-mark "d")
11357     (unread gnus-unread-mark "u")
11358     (ticked gnus-ticked-mark "!")
11359     (dormant gnus-dormant-mark "?")
11360     (expirable gnus-expirable-mark "e"))
11361   "An alist of names/marks/keystrokes.")
11362
11363 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11364 (defvar gnus-summary-mark-map)
11365
11366 (defun gnus-summary-make-all-marking-commands ()
11367   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11368   (dolist (elem gnus-summary-marking-alist)
11369     (apply 'gnus-summary-make-marking-command elem)))
11370
11371 (defun gnus-summary-make-marking-command (name mark keystroke)
11372   (let ((map (make-sparse-keymap)))
11373     (define-key gnus-summary-generic-mark-map keystroke map)
11374     (dolist (lway `((next "next" next nil "n")
11375                     (next-unread "next unread" next t "N")
11376                     (prev "previous" prev nil "p")
11377                     (prev-unread "previous unread" prev t "P")
11378                     (nomove "" nil nil ,keystroke)))
11379       (let ((func (gnus-summary-make-marking-command-1
11380                    mark (car lway) lway name)))
11381         (setq func (eval func))
11382         (define-key map (nth 4 lway) func)))))
11383
11384 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11385   `(defun ,(intern
11386             (format "gnus-summary-put-mark-as-%s%s"
11387                     name (if (eq way 'nomove)
11388                              ""
11389                            (concat "-" (symbol-name way)))))
11390      (n)
11391      ,(format
11392        "Mark the current article as %s%s.
11393 If N, the prefix, then repeat N times.
11394 If N is negative, move in reverse order.
11395 The difference between N and the actual number of articles marked is
11396 returned."
11397        name (cadr lway))
11398      (interactive "p")
11399      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11400
11401 (defun gnus-summary-generic-mark (n mark move unread)
11402   "Mark N articles with MARK."
11403   (unless (eq major-mode 'gnus-summary-mode)
11404     (error "This command can only be used in the summary buffer"))
11405   (gnus-summary-show-thread)
11406   (let ((nummove
11407          (cond
11408           ((eq move 'next) 1)
11409           ((eq move 'prev) -1)
11410           (t 0))))
11411     (if (zerop nummove)
11412         (setq n 1)
11413       (when (< n 0)
11414         (setq n (abs n)
11415               nummove (* -1 nummove))))
11416     (while (and (> n 0)
11417                 (gnus-summary-mark-article nil mark)
11418                 (zerop (gnus-summary-next-subject nummove unread t)))
11419       (setq n (1- n)))
11420     (when (/= 0 n)
11421       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11422     (gnus-summary-recenter)
11423     (gnus-summary-position-point)
11424     (gnus-set-mode-line 'summary)
11425     n))
11426
11427 (defun gnus-summary-insert-articles (articles)
11428   (when (setq articles
11429               (gnus-sorted-difference articles
11430                                       (mapcar (lambda (h)
11431                                                 (mail-header-number h))
11432                                               gnus-newsgroup-headers)))
11433     (setq gnus-newsgroup-headers
11434           (merge 'list
11435                  gnus-newsgroup-headers
11436                  (gnus-fetch-headers articles)
11437                  'gnus-article-sort-by-number))
11438     ;; Suppress duplicates?
11439     (when gnus-suppress-duplicates
11440       (gnus-dup-suppress-articles))
11441
11442     ;; We might want to build some more threads first.
11443     (when (and gnus-fetch-old-headers
11444                (eq gnus-headers-retrieved-by 'nov))
11445       (if (eq gnus-fetch-old-headers 'invisible)
11446           (gnus-build-all-threads)
11447         (gnus-build-old-threads)))
11448     ;; Let the Gnus agent mark articles as read.
11449     (when gnus-agent
11450       (gnus-agent-get-undownloaded-list))
11451     ;; Remove list identifiers from subject
11452     (when gnus-list-identifiers
11453       (gnus-summary-remove-list-identifiers))
11454     ;; First and last article in this newsgroup.
11455     (when gnus-newsgroup-headers
11456       (setq gnus-newsgroup-begin
11457             (mail-header-number (car gnus-newsgroup-headers))
11458             gnus-newsgroup-end
11459             (mail-header-number
11460              (gnus-last-element gnus-newsgroup-headers))))
11461     (when gnus-use-scoring
11462       (gnus-possibly-score-headers))))
11463
11464 (defun gnus-summary-insert-old-articles (&optional all)
11465   "Insert all old articles in this group.
11466 If ALL is non-nil, already read articles become readable.
11467 If ALL is a number, fetch this number of articles."
11468   (interactive "P")
11469   (prog1
11470       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11471             older len)
11472         (setq older
11473               ;; Some nntp servers lie about their active range.  When
11474               ;; this happens, the active range can be in the millions.
11475               ;; Use a compressed range to avoid creating a huge list.
11476               (gnus-range-difference (list gnus-newsgroup-active) old))
11477         (setq len (gnus-range-length older))
11478         (cond
11479          ((null older) nil)
11480          ((numberp all)
11481           (if (< all len)
11482               (let ((older-range (nreverse older)))
11483                 (setq older nil)
11484
11485                 (while (> all 0)
11486                   (let* ((r (pop older-range))
11487                          (min (if (numberp r) r (car r)))
11488                          (max (if (numberp r) r (cdr r))))
11489                     (while (and (<= min max)
11490                                 (> all 0))
11491                       (push max older)
11492                       (setq all (1- all)
11493                             max (1- max))))))
11494             (setq older (gnus-uncompress-range older))))
11495          (all
11496           (setq older (gnus-uncompress-range older)))
11497          (t
11498           (when (and (numberp gnus-large-newsgroup)
11499                    (> len gnus-large-newsgroup))
11500               (let* ((cursor-in-echo-area nil)
11501                      (initial (gnus-parameter-large-newsgroup-initial
11502                                gnus-newsgroup-name))
11503                      (input
11504                       (read-string
11505                        (format
11506                         "How many articles from %s (%s %d): "
11507                         (gnus-limit-string
11508                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11509                         (if initial "max" "default")
11510                         len)
11511                        (if initial
11512                            (cons (number-to-string initial)
11513                                  0)))))
11514                 (unless (string-match "^[ \t]*$" input)
11515                   (setq all (string-to-number input))
11516                   (if (< all len)
11517                       (let ((older-range (nreverse older)))
11518                         (setq older nil)
11519
11520                         (while (> all 0)
11521                           (let* ((r (pop older-range))
11522                                  (min (if (numberp r) r (car r)))
11523                                  (max (if (numberp r) r (cdr r))))
11524                             (while (and (<= min max)
11525                                         (> all 0))
11526                               (push max older)
11527                               (setq all (1- all)
11528                                     max (1- max))))))))))
11529           (setq older (gnus-uncompress-range older))))
11530         (if (not older)
11531             (message "No old news.")
11532           (gnus-summary-insert-articles older)
11533           (gnus-summary-limit (gnus-sorted-nunion old older))))
11534     (gnus-summary-position-point)))
11535
11536 (defun gnus-summary-insert-new-articles ()
11537   "Insert all new articles in this group."
11538   (interactive)
11539   (prog1
11540       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11541             (old-active gnus-newsgroup-active)
11542             (nnmail-fetched-sources (list t))
11543             i new)
11544         (setq gnus-newsgroup-active
11545               (gnus-activate-group gnus-newsgroup-name 'scan))
11546         (setq i (cdr gnus-newsgroup-active))
11547         (while (> i (cdr old-active))
11548           (push i new)
11549           (decf i))
11550         (if (not new)
11551             (message "No gnus is bad news.")
11552           (gnus-summary-insert-articles new)
11553           (setq gnus-newsgroup-unreads
11554                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11555           (gnus-summary-limit (gnus-sorted-nunion old new))))
11556     (gnus-summary-position-point)))
11557
11558 (gnus-summary-make-all-marking-commands)
11559
11560 (gnus-ems-redefine)
11561
11562 (provide 'gnus-sum)
11563
11564 (run-hooks 'gnus-sum-load-hook)
11565
11566 ;;; gnus-sum.el ends here